From dc53e6dce1eb7f7a67d0f448355d92e4ac086d7f Mon Sep 17 00:00:00 2001 From: wmayer Date: Tue, 3 May 2022 16:52:54 +0200 Subject: [PATCH] App: fix restoring the attributes of a dynamic property For more details see: https://forum.freecadweb.org/viewtopic.php?p=591465#p591465 --- src/App/DynamicProperty.cpp | 6 +++++- src/Mod/Test/Document.py | 24 ++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/src/App/DynamicProperty.cpp b/src/App/DynamicProperty.cpp index c049ce22b1..821d09c873 100644 --- a/src/App/DynamicProperty.cpp +++ b/src/App/DynamicProperty.cpp @@ -307,7 +307,10 @@ Property *DynamicProperty::restore(PropertyContainer &pc, doc = reader.getAttribute("doc"); if (reader.hasAttribute("attr")) { attr = reader.getAttribute("attr"); - if (attr) attribute = attr[0]-48; + if (attr) { + std::istringstream str(attr); + str >> attribute; + } } if (reader.hasAttribute("ro")) { ro = reader.getAttribute("ro"); @@ -317,6 +320,7 @@ Property *DynamicProperty::restore(PropertyContainer &pc, hide = reader.getAttribute("hide"); if (hide) hidden = (hide[0]-48) != 0; } + return addDynamicProperty(pc,TypeName, PropName, group, doc, attribute, readonly, hidden); } diff --git a/src/Mod/Test/Document.py b/src/Mod/Test/Document.py index 4acf2e8725..cd47ef3927 100644 --- a/src/Mod/Test/Document.py +++ b/src/Mod/Test/Document.py @@ -377,6 +377,30 @@ class DocumentBasicCases(unittest.TestCase): obj1.Link = obj2 self.assertEqual(obj1.MustExecute, False) + def testAttributeOfDynamicProperty(self): + obj = self.Doc.addObject("App::FeaturePython", "Obj") + # Prop_NoPersist is the enum with the highest value + max_value = FreeCAD.PropertyType.Prop_NoPersist + list_of_types = [] + for i in range(0, max_value + 1): + obj.addProperty("App::PropertyString", "String" + str(i), "", "", i) + list_of_types.append(obj.getTypeOfProperty("String" + str(i))) + + # saving and restoring + SaveName = tempfile.gettempdir() + os.sep + "CreateTest.FCStd" + self.Doc.saveAs(SaveName) + FreeCAD.closeDocument("CreateTest") + self.Doc = FreeCAD.open(SaveName) + + obj = self.Doc.ActiveObject + for i in range(0, max_value): + types = obj.getTypeOfProperty("String" + str(i)) + self.assertEqual(list_of_types[i], types) + + # A property with flag Prop_NoPersist won't be saved to the file + with self.assertRaises(AttributeError): + obj.getTypeOfProperty("String" + str(max_value)) + def testNotification_Issue2902Part2(self): o = self.Doc.addObject("App::FeatureTest","test")