Undocumented string processing in expressions : shall we document ?

Have some feature requests, feedback, cool stuff to share, or want to know where FreeCAD is going? This is the place.
Forum rules
Be nice to others! Read the FreeCAD code of conduct!
openBrain
Posts: 6041
Joined: Fri Nov 09, 2018 5:38 pm

Undocumented string processing in expressions : shall we document ?

Postby openBrain » Sat May 15, 2021 9:23 am

Hi all,

There is a cool feature in expressions about string processing that is, AFAIK, undocumented.
Actually is appears that expressions are able to process strings in the old fashion Python way (% formatting) using the '<< >>' delimiters.
A typical example would be

Code: Select all

<<The length of the cube is : %s>> % Box.Length
that would return (with a default cube)

Code: Select all

The length of the cube is : 10.0 mm
Only one specifier is possible inside delimiters, but string concatenating is also possible using '+', and all Python specifiers are available.
So with a default cube and 3 decimals as a precision

Code: Select all

<<Box (%s) : >> % Box.Label + <<L = %g mm : >> % Box.Length + <<W = %.2f mm>> % Box.Width
would expand in

Code: Select all

Box (Cube) : L = 10 mm : W = 10.00 mm
This to me opens a bunch of new possibilities. Obvious one is for part engraving as demonstrated below:
ExprString.gif
ExprString.gif (108.13 KiB) Viewed 976 times
I also attach the file, so you can play with (I used this possibility both in conditional expressions in spreadsheet, and in the text of the shapestring) -- you may have to set the font path in the shapestring so file works --.

Now the question is : can it be considered as a really consciously coded feature that we will maintain, and so we can document ? Or is this just an unexpected side effect of the expression engine parser that we are absolutely not sure it will work again in the future (so better not document) ?

Anyway, great kudos to @Forthman that bring this possibility into the light :
Forthman wrote: Fri Apr 30, 2021 9:49 am
Attachments
ExprString.FCStd
(188.95 KiB) Downloaded 21 times
User avatar
onekk
Posts: 1011
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: Undocumented string processing in expressions : shall we document ?

Postby onekk » Mon May 17, 2021 7:44 am

I think that spekaing of Spreadsheet is an "expected feature", as it is a "standard" behaviour that you could refer to a value with column and row "numbers".

The side effect that you could refer also with "Object properties" in a CAD environment is in my opinion a "must have" feature.

Having also the ability to use "Python string expressions" is a good thing, are all the "fomratting" options implemented?

i.e if I write:

Code: Select all

<<value %.3f>> % Box.Length 
it will work?


Regards

Carlo D.
Il mio Blog: https://onekk-maker.blogspot.com/

Introduzione a FreeCAD in Italiano: https://github.com/onekk/freecad-doc
openBrain
Posts: 6041
Joined: Fri Nov 09, 2018 5:38 pm

Re: Undocumented string processing in expressions : shall we document ?

Postby openBrain » Mon May 17, 2021 2:40 pm

onekk wrote: Mon May 17, 2021 7:44 am I think that spekaing of Spreadsheet is an "expected feature", as it is a "standard" behaviour that you could refer to a value with column and row "numbers".

The point is only about ability to use %-style string formatting in Expressions. ;)

i.e if I write:

Code: Select all

<<value %.3f>> % Box.Length 
it will work?
That's it. The whole Python string formatting is available.
User avatar
onekk
Posts: 1011
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: Undocumented string processing in expressions : shall we document ?

Postby onekk » Mon May 17, 2021 3:43 pm

Sorry for having not explained better, in my precendet post.

I intended that referring to something other than text in a spreadsheet cell, is a "standard behaviour", but that it is very interesting that you could refer other than the usual way (column, row reference), also with "Object properties".

Thanks for the clarification, it is something to note as it will be useful in some cases.

Regards

Carlo D.
Il mio Blog: https://onekk-maker.blogspot.com/

Introduzione a FreeCAD in Italiano: https://github.com/onekk/freecad-doc
openBrain
Posts: 6041
Joined: Fri Nov 09, 2018 5:38 pm

Re: Undocumented string processing in expressions : shall we document ?

Postby openBrain » Mon May 17, 2021 4:39 pm

onekk wrote: Mon May 17, 2021 3:43 pm I intended that referring to something other than text in a spreadsheet cell, is a "standard behaviour", but that it is very interesting that you could refer other than the usual way (column, row reference), also with "Object properties".
The topic is neither about Spreadsheet specifically (a spreadsheet cell is basically an expression line) nor about the current capability of Expression engine to reference other objects properties. It's purely about the (AFAIK undocumented) ability to use string formatting in expressions. ;)
User avatar
onekk
Posts: 1011
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: Undocumented string processing in expressions : shall we document ?

Postby onekk » Mon May 17, 2021 8:10 pm

I have to see how spreadsheet and expression engine is implemented, thanks for clarification.

I have totally misunderstood the topics sorry.

Regards

Carlo D.
Il mio Blog: https://onekk-maker.blogspot.com/

Introduzione a FreeCAD in Italiano: https://github.com/onekk/freecad-doc
openBrain
Posts: 6041
Joined: Fri Nov 09, 2018 5:38 pm

Re: Undocumented string processing in expressions : shall we document ?

Postby openBrain » Thu Jun 03, 2021 12:45 pm

I made a draft documentation proposal in the wiki : https://wiki.freecadweb.org/Expressions ... nipulation