Skip to content

Commit 468269b

Browse files
authored
TemplatePyMod: fix DocumentObject for newer FreeCAD versions (FreeCAD#27662)
* TemplatePyMod: fix DocumentObject for newer FreeCAD versions original bug report: https://forum.freecad.org/viewtopic.php?p=873546 * TemplatePyMod: fix FeaturePython example for newer FreeCAD versions
1 parent 289411f commit 468269b

File tree

2 files changed

+31
-27
lines changed

2 files changed

+31
-27
lines changed

src/Mod/TemplatePyMod/DocumentObject.py

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,14 @@ def propertyChanged(self,prop):
2828

2929
def __getattr__(self, attr):
3030
if attr !="__object__" and hasattr(self.__object__,attr):
31-
return getattr(self.__object__,attr)
32-
else:
33-
return object.__getattribute__(self,attr)
31+
# Methods like "getSubObject" are called from the C++ code if they exist in
32+
# this class (DocumentObject).
33+
# Our __object__ also has a method with the same name ("getSubObject"),
34+
# but it does not require the extra first argument "obj" which is the same
35+
# as self.__object__. So we cannot map these methods 1:1.
36+
if attr not in ("getSubObject", "getSubObjects", "getLinkedObject"):
37+
return getattr(self.__object__,attr)
38+
return object.__getattribute__(self,attr)
3439
def __setattr__(self, attr, value):
3540
if attr !="__object__" and hasattr(self.__object__,attr):
3641
setattr(self.__object__,attr,value)
@@ -91,12 +96,11 @@ def touch(self):
9196
def purgeTouched(self):
9297
"removes the to-be-recomputed flag of this object"
9398
return self.__object__.purgeTouched()
94-
def __setstate__(self,value):
95-
"""allows saving custom attributes of this object as strings, so
96-
they can be saved when saving the FreeCAD document"""
99+
def loads(self,value):
100+
"""Called during document restore."""
97101
return None
98-
def __getstate__(self):
99-
"""reads values previously saved with __setstate__()"""
102+
def dumps(self):
103+
"""Called during document saving."""
100104
return None
101105
@property
102106
def PropertiesList(self):
@@ -224,12 +228,11 @@ def getGroupOfProperty(self,attr):
224228
def getDocumentationOfProperty(self,attr):
225229
"returns the documentation string of a given property"
226230
return self.__vobject__.getDocumentationOfProperty(attr)
227-
def __setstate__(self,value):
228-
"""allows saving custom attributes of this object as strings, so
229-
they can be saved when saving the FreeCAD document"""
231+
def loads(self,value):
232+
"""Called during document restore."""
230233
return None
231-
def __getstate__(self):
232-
"""reads values previously saved with __setstate__()"""
234+
def dumps(self):
235+
"""Called during document saving."""
233236
return None
234237
@property
235238
def Annotation(self):

src/Mod/TemplatePyMod/FeaturePython.py

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -96,14 +96,14 @@ def getIcon(self):
9696
" ####### "};
9797
"""
9898

99-
def __getstate__(self):
99+
def dumps(self):
100100
''' When saving the document this object gets stored using Python's cPickle module.
101101
Since we have some un-pickable here -- the Coin stuff -- we must define this method
102102
to return a tuple of all pickable objects or None.
103103
'''
104104
return None
105105

106-
def __setstate__(self,state):
106+
def loads(self,state):
107107
''' When restoring the pickled object from document we have the chance to set some
108108
internals here. Since no data were pickled nothing needs to be done here.
109109
'''
@@ -314,10 +314,10 @@ def getIcon(self):
314314
" ####### "};
315315
"""
316316

317-
def __getstate__(self):
317+
def dumps(self):
318318
return None
319319

320-
def __setstate__(self,state):
320+
def loads(self,state):
321321
return None
322322

323323
def makeOctahedron():
@@ -405,14 +405,14 @@ def getIcon(self):
405405
" ####### "};
406406
"""
407407

408-
def __getstate__(self):
408+
def dumps(self):
409409
''' When saving the document this object gets stored using Python's cPickle module.
410410
Since we have some un-pickable here -- the Coin stuff -- we must define this method
411411
to return a tuple of all pickable objects or None.
412412
'''
413413
return None
414414

415-
def __setstate__(self,state):
415+
def loads(self,state):
416416
''' When restoring the pickled object from document we have the chance to set some
417417
internals here. Since no data were pickled nothing needs to be done here.
418418
'''
@@ -498,14 +498,14 @@ def getIcon(self):
498498
" ####### "};
499499
"""
500500

501-
def __getstate__(self):
501+
def dumps(self):
502502
''' When saving the document this object gets stored using Python's cPickle module.
503503
Since we have some un-pickable here -- the Coin stuff -- we must define this method
504504
to return a tuple of all pickable objects or None.
505505
'''
506506
return None
507507

508-
def __setstate__(self,state):
508+
def loads(self,state):
509509
''' When restoring the pickled object from document we have the chance to set some
510510
internals here. Since no data were pickled nothing needs to be done here.
511511
'''
@@ -562,10 +562,10 @@ def updateData(self, fp, prop):
562562
p = fp.getPropertyByName("p2")
563563
self.trl2.translation=(p.x,p.y,p.z)
564564

565-
def __getstate__(self):
565+
def dumps(self):
566566
return None
567567

568-
def __setstate__(self,state):
568+
def loads(self,state):
569569
return None
570570

571571
def makeMolecule():
@@ -612,10 +612,10 @@ def updateData(self, fp, prop):
612612
self.coords.point.setValues(pts)
613613
self.lines.numVertices.setValues(ver)
614614

615-
def __getstate__(self):
615+
def dumps(self):
616616
return None
617617

618-
def __setstate__(self,state):
618+
def loads(self,state):
619619
return None
620620

621621
def makeCircleSet():
@@ -659,10 +659,10 @@ def __init__(self, obj):
659659
def updateData(self, fp, prop):
660660
print("prop updated:",prop)
661661

662-
def __getstate__(self):
662+
def dumps(self):
663663
return None
664664

665-
def __setstate__(self,state):
665+
def loads(self,state):
666666
return None
667667

668668
def makeEnumTest():
@@ -723,3 +723,4 @@ def makeDistanceBolt():
723723
DistanceBolt(bolt)
724724
bolt.ViewObject.Proxy=0
725725
doc.recompute()
726+

0 commit comments

Comments
 (0)