Expressions... again...

Here's the place for discussion related to coding in FreeCAD, C++ or Python. Design, interfaces and structures.
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
User avatar
sgrogan
Veteran
Posts: 6499
Joined: Wed Oct 22, 2014 5:02 pm

Re: Expressions... again...

Post by sgrogan »

eivindkvedalen wrote:I'll post a pull request when I have added some more documention, and my current pull request to remove warnings is merged ;)
@Eivind
Congratulation on this and THANK YOU!
This was one of the most requested features. I admire your perseverance and dedication to this.
"fight the good fight"
ickby
Veteran
Posts: 3116
Joined: Wed Oct 05, 2011 7:36 am

Re: Expressions... again...

Post by ickby »

Hello Eivind,

I'm looking into binding the expressions of properties in the property editor. One question I can't find anything about: is it possible to bind expressions to a properties subvalue? For example if I have a quantify spin box for each component of a vector x y and z, can I bind this somehow? This would be handy for placements in the property editor.
eivindkvedalen
Posts: 602
Joined: Tue Jan 29, 2013 10:35 pm

Re: Expressions... again...

Post by eivindkvedalen »

ickby wrote:Hello Eivind,

I'm looking into binding the expressions of properties in the property editor. One question I can't find anything about: is it possible to bind expressions to a properties subvalue? For example if I have a quantify spin box for each component of a vector x y and z, can I bind this somehow? This would be handy for placements in the property editor.
Hi, this is possible using the ExpressionBinding::bind(const App::ObjectIdentifier & _path) method. You need to construct an ObjectIdentifier object, which you can do either with ObjectIdentifier::parse(...), or you can construct it like I have done in PropertyConstraintList::createPath(int ConstrNbr). The first one actually invokes the parser, so the second method is preferred as that is just object construction, even though it is a bit cumbersome to write. In your case, it would be something like

App::ObjectIdentifier(docObj)
<< App::ObjectIdentifier::Component::SimpleComponent(App::ObjectIdentifier::String("Placement"))
<< App::ObjectIdentifier::Component::SimpleComponent(App::ObjectIdentifier::String("Base"))
<< App::ObjectIdentifier::Component::SimpleComponent(App::ObjectIdentifier::String("x"))

to access the x value of the placement vector.

Eivind
ickby
Veteran
Posts: 3116
Joined: Wed Oct 05, 2011 7:36 am

Re: Expressions... again...

Post by ickby »

thanks, this looks really easy! I will see how well this integrates into the property editor, but my first experiments seems to work very well
User avatar
pablogil
Posts: 882
Joined: Wed Nov 26, 2014 3:19 pm
Location: Badajoz (Spain)
Contact:

Re: Expressions... again...

Post by pablogil »

eivindkvedalen wrote:
ickby wrote:Hello Eivind,

I'm looking into binding the expressions of properties in the property editor. One question I can't find anything about: is it possible to bind expressions to a properties subvalue? For example if I have a quantify spin box for each component of a vector x y and z, can I bind this somehow? This would be handy for placements in the property editor.
Hi, this is possible using the ExpressionBinding::bind(const App::ObjectIdentifier & _path) method. You need to construct an ObjectIdentifier object, which you can do either with ObjectIdentifier::parse(...), or you can construct it like I have done in PropertyConstraintList::createPath(int ConstrNbr). The first one actually invokes the parser, so the second method is preferred as that is just object construction, even though it is a bit cumbersome to write. In your case, it would be something like

App::ObjectIdentifier(docObj)
<< App::ObjectIdentifier::Component::SimpleComponent(App::ObjectIdentifier::String("Placement"))
<< App::ObjectIdentifier::Component::SimpleComponent(App::ObjectIdentifier::String("Base"))
<< App::ObjectIdentifier::Component::SimpleComponent(App::ObjectIdentifier::String("x"))

to access the x value of the placement vector.

Eivind
Could you please write or record a simple tutorial on now to do this? I'm totally newbie about this kind of "complex expressions" but it feels so powerful...
Dark and Light stylesheets v2.0 to theme your FreeCAD UI, more information here
eivindkvedalen
Posts: 602
Joined: Tue Jan 29, 2013 10:35 pm

Re: Expressions... again...

Post by eivindkvedalen »

pablogil wrote:
Could you please write or record a simple tutorial on now to do this? I'm totally newbie about this kind of "complex expressions" but it feels so powerful...
What ickby and I were discussing is low-level access to properties from C++. He has started on the user interface to make expressions available in the property editor for regular users. However, for the time being, it is possible to set expressions on arbitrary properties (or sub-properties) from Python by using the setExpression function. For example, to set the placement vector, you can issue something like

App.ActiveDocument.Sketch.setExpression('Placement.Base.x', '20mm +2mm')

If you want to extend FreeCAD in C++, I suggest that you look at the PartDesign and Sketcher modules first, and also the Expression classes in App. The gui part is mainly the QuantitySpinBox and ExpressionBinding classes. Those are the two that have their GUI updated to support expressions.

For user documentation, please also see http://www.freecadweb.org/wiki/index.ph ... xpressions

Eivind
User avatar
brusk
Posts: 52
Joined: Wed Feb 18, 2015 4:33 pm

Re: Expressions... again...

Post by brusk »

Thank you so much for Expressions, Eivind!

This is how I always wanted to use FreeCAD :D !

Two notes:

1) When editing an expression I can close the autocomplete drop down and the expression text field with the return key. But it stops working then, when only the constraints editor (the one where it's possible to specify the name of the constraint) is showing, even though the Ok-button is blue, indicating that it's the default button. It's a minor thing and maybe not even related to your work, it could just be nice to be able to press enter, enter, enter instead of having to reach for the mouse.

2) By accident I created a copy of the spreadsheet I am pulling values into expressions from. After I deleted the copy suddenly all my sketches involving expressions was showing errors. Inspecting them I found that my spreadsheet, "Sheet", had been renamed to "Sheet001" in all the expressions. And since I had deleted the copy they were no longer valid. Is that how it's supposed to work?
User avatar
microelly2
Veteran
Posts: 4688
Joined: Tue Nov 12, 2013 4:06 pm
Contact:

Re: Expressions... again...

Post by microelly2 »

eivindkvedalen wrote: However, for the time being, it is possible to set expressions on arbitrary properties (or sub-properties) from Python by using the setExpression function. For example, to set the placement vector, you can issue something like

App.ActiveDocument.Sketch.setExpression('Placement.Base.x', '20mm +2mm')
Very nice to use Interface. I used it in my animations and it works fine.
It's still not documented but it makes a lot of things much easier.
Is there already a tutorial for this function I did not see?
Will you extend it for other datatypes too?
User avatar
brusk
Posts: 52
Joined: Wed Feb 18, 2015 4:33 pm

Re: Expressions... again...

Post by brusk »

brusk wrote:When editing an expression I can close the autocomplete drop down and the expression text field with the return key. But it stops working then, when only the constraints editor (the one where it's possible to specify the name of the constraint) is showing, even though the Ok-button is blue, indicating that it's the default button. It's a minor thing and maybe not even related to your work, it could just be nice to be able to press enter, enter, enter instead of having to reach for the mouse.
Ah, one just needs to press tab once to be able to use the enter key in the end :) .
eivindkvedalen
Posts: 602
Joined: Tue Jan 29, 2013 10:35 pm

Re: Expressions... again...

Post by eivindkvedalen »

brusk wrote:2) By accident I created a copy of the spreadsheet I am pulling values into expressions from. After I deleted the copy suddenly all my sketches involving expressions was showing errors. Inspecting them I found that my spreadsheet, "Sheet", had been renamed to "Sheet001" in all the expressions. And since I had deleted the copy they were no longer valid. Is that how it's supposed to work?
No, but I'm not able to recreate the problem here... :(

EDIT: Sometimes I hit some weird behaviour when I delete objects. I know this is related to the expressions somehow, but have so far not been able to recreate it consistently here. Any help on recreating the problem above would be appreciated :)

Eivind
Last edited by eivindkvedalen on Sun Nov 29, 2015 12:26 pm, edited 1 time in total.
Post Reply