evalExpression as a class/static method

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Zoltan
Posts: 62
Joined: Wed Jul 07, 2021 10:08 am

Re: evalExpression as a class/static method

Post by Zoltan »

wmayer wrote: Thu Sep 23, 2021 1:15 pm Here you will find the implementation of evalExpression() and as you can see the first parameter passed to Expression::parse() is getDocumentObjectPtr() that points to an instance of a DocumentObject.

As a test I have replaced getDocumentObjectPtr() with nullptr and calling

Code: Select all

obj.evalExpression("2+2+")
succeeded. So, this means it's possible that an expression can be evaluated outside the context of a DocumentObject instance and thus a class method should work, too.
But I still cannot call the evalExpression method on a DocumentObject class as shown in my previous post.
wmayer
Founder
Posts: 20307
Joined: Thu Feb 19, 2009 10:32 am
Contact:

Re: evalExpression as a class/static method

Post by wmayer »

git commit bfe7a7a120

This allows you to write:

Code: Select all

App.DocumentObject.evalExpression("2+2")
Now create a sketch and draw a rectangle. Set a length constraint for a vertical line and call it "Height". Set a length constraint for a horizontal line and set the constraint .Constraints.Height and set its name to "Width".

Now run this code:

Code: Select all

s=App.ActiveDocument.Sketch
s.evalExpression("2+2") # OK

s.evalExpression(s.ExpressionEngine[0][0]) # OK

# As expected it raises an error because the expression requires an instance of a DocumentObject
App.DocumentObject.evalExpression(s.ExpressionEngine[0][0])
Zoltan
Posts: 62
Joined: Wed Jul 07, 2021 10:08 am

Re: evalExpression as a class/static method

Post by Zoltan »

I cannot compile FreeCAD on my system, so I use the AppImage version (the Realthunder fork). Then I guess I will have to wait until the next release of FreeCAD.
TheMarkster
Veteran
Posts: 5513
Joined: Thu Apr 05, 2018 1:53 am

Re: evalExpression as a class/static method

Post by TheMarkster »

Zoltan wrote: Thu Sep 23, 2021 1:27 pm
Here is a GIF that demonstrates it.
6yfRUbGTWd.gif
What I would really like to be able to do is create a sketch in memory not in the tree
When you create a document object and add it to the document, it will appear in the tree view (unless you hide it). If you want to work "in-memory", do not associate your custom class object with the Proxy field of a document object.
Thanks for the video. Now I see the property tooltip. I was looking for it in the context menu before.

But I don't get the part about the Proxy. This simple code produces an object that shows up on the tree even though I haven't set the Proxy to anything. Unless I uncomment the last line, which hides it. Can this be modified to not be shown in the tree without using ShowInTree = False?

Code: Select all

class Invisible:
    def __init__(self,obj):
        obj.addProperty("App::PropertyFloat","float1").float1 = 0.5

fp = App.ActiveDocument.addObject("App::FeaturePython","invisible")
Invisible(fp)
#fp.ViewObject.ShowInTree = False
Zoltan
Posts: 62
Joined: Wed Jul 07, 2021 10:08 am

Re: evalExpression as a class/static method

Post by Zoltan »

What I meant by Proxy is the following idea. In the FreeCAD GUI you interact with your custom Python class by i) creating a document object (that you can show or hide as discussed before) and ii) adding your custom Python class object to the Proxy field of the document object. For an example, see the Curves workbench.

If you don't want any interaction with FreeCAD (for instance, you create a helper class), just use a simple Python class (so don't pass a document object to its initializer). Since you haven't created a document object, it won't be shown in the tree view.
Post Reply