PropertyEnum in Python feature

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!
Post Reply
User avatar
yorik
Founder
Posts: 13640
Joined: Tue Feb 17, 2009 9:16 pm
Location: Brussels
Contact:

PropertyEnum in Python feature

Post by yorik »

Hi,
I tried to use a PropertyEnum in a part python feature (for ex. try to draw a Draft rectangle or wire, in its view properties there are "Fill Style" and "Line Style" properties), but when you save the document and open it again, the contents of that property is lost.

Is there something special to do?

Cheers
Y
wmayer
Founder
Posts: 20243
Joined: Thu Feb 19, 2009 10:32 am
Contact:

Re: PropertyEnum in Python feature

Post by wmayer »

This was a bug which is fixed in revision 3420.
Is there something special to do?
When assigning custom enumeration values from within Python we didn't store them to the XML file. This should now work.
User avatar
yorik
Founder
Posts: 13640
Joined: Tue Feb 17, 2009 9:16 pm
Location: Brussels
Contact:

Re: PropertyEnum in Python feature

Post by yorik »

Thanks for the fix Werner, it works OK!
User avatar
yorik
Founder
Posts: 13640
Joined: Tue Feb 17, 2009 9:16 pm
Location: Brussels
Contact:

Re: PropertyEnum in Python feature

Post by yorik »

Hm I think I spoke too soon...
There is a problem but I cannot find what's going wrong...
Try making 1 or 2 draft rectangles, and save the file? It crashes on reopening it...
The culprits are 2 PropertyEnums of those rectangles, (FillStyle and LineStyle)...
But I cannot figure out what's going on, I tried removing one of the properties, then it works,
or changing the name of FillStyle for another one (surprisingly it then works if there is only ONE rectangle)
Those 2 properties are of the viewobject, not the object...
Using your example in TemplatePyMod works, but it has no view provider, I suppose the problem is there?

Can you have a look?

Thanks
Y
User avatar
yorik
Founder
Posts: 13640
Joined: Tue Feb 17, 2009 9:16 pm
Location: Brussels
Contact:

Re: PropertyEnum in Python feature

Post by yorik »

Here is a piece of code that reproduces the problem. I just adapted your EnumTest example to give it a view provider.
The object gets created correctly, but if you save the doc freecad crashes on reopening the file

Code: Select all

class EnumTest:
        def __init__(self, obj):
                ''' Add enum properties '''
                obj.addProperty("App::PropertyEnumeration","Enum","","Enumeration").Enum=["One","Two","Three"]
                obj.addProperty("App::PropertyEnumeration","Enum2","","Enumeration2").Enum2=["One","Two","Three"]
                obj.Proxy = self

        def execute(self, fp):
                return                                                                            
                                                                                                  
class ViewProviderEnumTest:
        def __init__(self, obj):
                ''' Set this object to the proxy object of the actual view provider '''           
                obj.addProperty("App::PropertyEnumeration","Enum3","","Enumeration3").Enum3=["One","Two","Three"]
                obj.addProperty("App::PropertyEnumeration","Enum4","","Enumeration4").Enum4=["One","Two","Three"]
                obj.Proxy = self

        def updateData(self, fp, prop):
                        print "prop updated:",prop

        def __getstate__(self):
                return None

        def __setstate__(self,state):
                return None


def makeEnumTest():
        a=FreeCAD.ActiveDocument.addObject("Part::FeaturePython","Enum")
        EnumTest(a)
        ViewProviderEnumTest(a.ViewObject)
wmayer
Founder
Posts: 20243
Joined: Thu Feb 19, 2009 10:32 am
Contact:

Re: PropertyEnum in Python feature

Post by wmayer »

This was a critical bug in the XML reader class which didn't reset attributes of an element after reading them in. Just a wonder that this bug didn't appear earlier... This is fixed in revision 3427.
User avatar
yorik
Founder
Posts: 13640
Joined: Tue Feb 17, 2009 9:16 pm
Location: Brussels
Contact:

Re: PropertyEnum in Python feature

Post by yorik »

Okay, that solved it! Thanks Werner!
Post Reply