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
Veteran
Posts: 9034
Joined: Fri Nov 09, 2018 5:38 pm
Contact:

Undocumented string processing in expressions : shall we document ?

Post by openBrain »

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 4489 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 150 times
User avatar
onekk
Veteran
Posts: 6144
Joined: Sat Jan 17, 2015 7:48 am
Contact:

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

Post by onekk »

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.
GitHub page: https://github.com/onekk/freecad-doc.
- In deep articles on FreeCAD.
- Learning how to model with scripting.
- Various other stuffs.

Blog: https://okkmkblog.wordpress.com/
openBrain
Veteran
Posts: 9034
Joined: Fri Nov 09, 2018 5:38 pm
Contact:

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

Post by openBrain »

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
Veteran
Posts: 6144
Joined: Sat Jan 17, 2015 7:48 am
Contact:

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

Post by onekk »

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.
GitHub page: https://github.com/onekk/freecad-doc.
- In deep articles on FreeCAD.
- Learning how to model with scripting.
- Various other stuffs.

Blog: https://okkmkblog.wordpress.com/
openBrain
Veteran
Posts: 9034
Joined: Fri Nov 09, 2018 5:38 pm
Contact:

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

Post by openBrain »

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
Veteran
Posts: 6144
Joined: Sat Jan 17, 2015 7:48 am
Contact:

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

Post by onekk »

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

I have totally misunderstood the topics sorry.

Regards

Carlo D.
GitHub page: https://github.com/onekk/freecad-doc.
- In deep articles on FreeCAD.
- Learning how to model with scripting.
- Various other stuffs.

Blog: https://okkmkblog.wordpress.com/
openBrain
Veteran
Posts: 9034
Joined: Fri Nov 09, 2018 5:38 pm
Contact:

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

Post by openBrain »

I made a draft documentation proposal in the wiki : https://wiki.freecadweb.org/Expressions ... nipulation
paullee
Veteran
Posts: 5097
Joined: Wed May 04, 2016 3:58 pm

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

Post by paullee »

Re-directed from a discussion in Help, this is very useful, thanks for the wiki ! :D
TheMarkster
Veteran
Posts: 5505
Joined: Thu Apr 05, 2018 1:53 am

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

Post by TheMarkster »

I say document it. If it breaks later and for whatever reason does not get fixed, then it can always be undocumented again.
openBrain
Veteran
Posts: 9034
Joined: Fri Nov 09, 2018 5:38 pm
Contact:

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

Post by openBrain »

TheMarkster wrote: Sat Jan 22, 2022 10:03 pm I say document it.
It's documented for about 6 months now. ;)
Post Reply