Expressions... again...

Here's the place for discussion related to coding in FreeCAD, C++ or Python. Design, interfaces and structures.
User avatar
sgrogan
Posts: 5412
Joined: Wed Oct 22, 2014 5:02 pm

Re: Expressions... again...

Postby sgrogan » Tue Sep 22, 2015 8:55 pm

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.
ickby
Posts: 2921
Joined: Wed Oct 05, 2011 7:36 am

Re: Expressions... again...

Postby ickby » Fri Sep 25, 2015 4:13 am

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: 598
Joined: Tue Jan 29, 2013 10:35 pm

Re: Expressions... again...

Postby eivindkvedalen » Fri Sep 25, 2015 6:06 am

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
Posts: 2921
Joined: Wed Oct 05, 2011 7:36 am

Re: Expressions... again...

Postby ickby » Fri Sep 25, 2015 6:18 am

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
pablogil
Posts: 751
Joined: Wed Nov 26, 2014 3:19 pm
Location: Badajoz (Spain)
Contact:

Re: Expressions... again...

Postby pablogil » Fri Sep 25, 2015 2:57 pm

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: 598
Joined: Tue Jan 29, 2013 10:35 pm

Re: Expressions... again...

Postby eivindkvedalen » Fri Sep 25, 2015 7:56 pm

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...

Postby brusk » Thu Nov 26, 2015 11:23 am

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
Posts: 4399
Joined: Tue Nov 12, 2013 4:06 pm
Contact:

Re: Expressions... again...

Postby microelly2 » Thu Nov 26, 2015 2:16 pm

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...

Postby brusk » Thu Nov 26, 2015 2:26 pm

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: 598
Joined: Tue Jan 29, 2013 10:35 pm

Re: Expressions... again...

Postby eivindkvedalen » Thu Nov 26, 2015 6:59 pm

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.