Support for placement and vector math in expressions

Info about new community or project announcements, implemented features, classes, modules or APIs. Might get technical!
PLEASE DO NOT POST HELP REQUESTS OR OTHER DISCUSSIONS HERE!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
User avatar
DeepSOIC
Veteran
Posts: 7896
Joined: Fri Aug 29, 2014 12:45 am
Location: used to be Saint-Petersburg, Russia

Support for placement and vector math in expressions

Post by DeepSOIC »

Doesn't seem to be documented or announced yet, so:
Since FreeCAD 0.19.18350 (git commit 6b6c0a503ca129174790e5a3ebe2ab95b6e96597), thanks to Realthunder:

Vector, Placement and Matrix math is available in spreadsheets and expressions.
=Cylinder.Placement.Base * 2 to set a spreadsheet cell to have a vector value
=Cylinder.Placement to set a cell to a placement value
to create a vector/placement from scratch:
=create(<<vector>>, 2, 1, 2)
=create(<<rotation>>, create(<<vector>>, 0, 1, 0), 45)
=create(<<placement>>, some_vector_value, some_rotation_value)

Doesn't seem like there is a GUI way to bind expressions to placements and vectors. (Maybe in Asm4.) But it is possible with py console:
App.ActiveDocument.Cylinder.setExpression('Placement', 'Box.Placement') - to make placement of Cylinder follow placement of Box
App.ActiveDocument.Cylinder.setExpression('Placement', 'Box.Placement^0.5') - place cylinder halfway between box and origin (since 0.19.18519)

I picked these up from unit test code, check it out for more usage examples.
https://github.com/FreeCAD/FreeCAD/blob ... et.py#L876



realthunder wrote: Sat Aug 31, 2019 7:47 amDing!
@realthunder, can you add something to this?
User avatar
DeepSOIC
Veteran
Posts: 7896
Joined: Fri Aug 29, 2014 12:45 am
Location: used to be Saint-Petersburg, Russia

Re: Support for placement and vector math in expressions

Post by DeepSOIC »

Oh, of course, more info here, in pull-request thread:
https://forum.freecadweb.org/viewtopic.php?f=27&t=38974
realthunder
Veteran
Posts: 2190
Joined: Tue Jan 03, 2017 10:55 am

Re: Support for placement and vector math in expressions

Post by realthunder »

DeepSOIC wrote: Sat Oct 12, 2019 3:01 pm can you add something to this?
You mean GUI ways of binding expression, or just some more info in general? Anyway, the GUI way of binding is hidden by default as I consider this as an advanced feature. Maybe we shall just make it default.

expression.gif
expression.gif (389.78 KiB) Viewed 2496 times

There is also this batch expression edit feature I talked about in the other thread. You can use treeview context menu 'Expression actions' to copy expressions of either the selected object, all objects of the active document, or all objects of all documents. Paste them to any text editor and edit it and paste it back. Paste back only modifies existing expression or create new bindings, it will not delete bindings. Try this with spreadsheets that have cell styles or alias, and you'll find you can batch edit those properties as well.

expression-copy-paste.gif
expression-copy-paste.gif (802.21 KiB) Viewed 2496 times
Try Assembly3 with my custom build of FreeCAD at here.
And if you'd like to show your support, you can donate through patreon, liberapay, or paypal
vocx
Veteran
Posts: 5197
Joined: Thu Oct 18, 2018 9:18 pm

Re: Support for placement and vector math in expressions

Post by vocx »

realthunder wrote: Sat Oct 12, 2019 11:47 pm You mean GUI ways of binding expression, or just some more info in general? Anyway, the GUI way of binding is hidden by default as I consider this as an advanced feature. Maybe we shall just make it default.
...
I have a question. Are View properties (view provider) in the Property editor able to use Expressions?

I don't remember if this worked before or not.

In the Draft Dimension command the start and end points are defined as "App::PropertyVectorDistance". The position of the dimension text (ViewObject.TextPosition) is also defined by an "App::PropertyVectorDistance". However, in the second case, there is no formula editor.

I'd like to be able to set

Code: Select all

ViewObject.TextPosition.x = obj.End.x + 1mm
How would one use an object's property, from a viewprovider's property? Or vice versa?
Always add the important information to your posts if you need help. Also see Tutorials and Video tutorials.
To support the documentation effort, and code development, your donation is appreciated: liberapay.com/FreeCAD.
realthunder
Veteran
Posts: 2190
Joined: Tue Jan 03, 2017 10:55 am

Re: Support for placement and vector math in expressions

Post by realthunder »

vocx wrote: Mon Oct 14, 2019 12:24 am I have a question. Are View properties (view provider) in the Property editor able to use Expressions?
It is not possible at the moment. The expression bindings are stored and processed by a property called ExpressionEngine that is contained in every document object. There is no such thing in view provider. It is of course possible to extend expression engine to support view property, but it is going to take quite some effort. A much simpler way is to change the implementation of Dimension. Either move the property to object, or add a new one to object, and sync the value in ViewObject.updateData().
Try Assembly3 with my custom build of FreeCAD at here.
And if you'd like to show your support, you can donate through patreon, liberapay, or paypal
vocx
Veteran
Posts: 5197
Joined: Thu Oct 18, 2018 9:18 pm

Re: Support for placement and vector math in expressions

Post by vocx »

realthunder wrote: Mon Oct 14, 2019 12:47 am ...The expression bindings are stored and processed by a property called ExpressionEngine that is contained in every document object. There is no such thing in view provider. ...
I see. I never really thought about this until now. I'll see what makes most sense about changing the implementation.
Always add the important information to your posts if you need help. Also see Tutorials and Video tutorials.
To support the documentation effort, and code development, your donation is appreciated: liberapay.com/FreeCAD.
Post Reply