From 8fbbfb29d84faffd3408e6d90d6c0e2a9e57f79b Mon Sep 17 00:00:00 2001 From: Markus Lampert Date: Thu, 31 Dec 2020 20:06:02 -0800 Subject: [PATCH] Expose PropertyEnumeration values list to python. --- src/App/PropertyContainerPy.xml | 5 +++++ src/App/PropertyContainerPyImp.cpp | 29 ++++++++++++++++++++++++- src/Mod/TemplatePyMod/DocumentObject.py | 3 +++ src/Mod/Test/Document.py | 2 ++ 4 files changed, 38 insertions(+), 1 deletion(-) diff --git a/src/App/PropertyContainerPy.xml b/src/App/PropertyContainerPy.xml index fbccd2525831..061709405bcd 100644 --- a/src/App/PropertyContainerPy.xml +++ b/src/App/PropertyContainerPy.xml @@ -97,6 +97,11 @@ text names of the status. Return the documentation string of the property of this class. + + + Return all enumeration strings of the property of this class or None if not a PropertyEnumeration. + + Dumps the content of the property, both the XML representation as well as the additional datafiles diff --git a/src/App/PropertyContainerPyImp.cpp b/src/App/PropertyContainerPyImp.cpp index 98132fac1ea3..a493ac811d6e 100644 --- a/src/App/PropertyContainerPyImp.cpp +++ b/src/App/PropertyContainerPyImp.cpp @@ -357,7 +357,34 @@ PyObject* PropertyContainerPy::getDocumentationOfProperty(PyObject *args) else return Py::new_reference_to(Py::String("")); } - + +PyObject* PropertyContainerPy::getEnumerationsOfProperty(PyObject *args) +{ + char *pstr; + if (!PyArg_ParseTuple(args, "s", &pstr)) // convert args: Python->C + return NULL; // NULL triggers exception + + Property* prop = getPropertyContainerPtr()->getPropertyByName(pstr); + if (!prop) { + PyErr_Format(PyExc_AttributeError, "Property container has no property '%s'", pstr); + return 0; + } + + PropertyEnumeration *enumProp = dynamic_cast(prop); + if (!enumProp) { + Py_INCREF(Py_None); + return Py_None; + } + + std::vector enumerations = enumProp->getEnumVector(); + + Py::List ret; + for (std::vector::const_iterator it = enumerations.begin(); it != enumerations.end(); ++it) { + ret.append(Py::String(*it)); + } + return Py::new_reference_to(ret); +} + Py::List PropertyContainerPy::getPropertiesList(void) const { Py::List ret; diff --git a/src/Mod/TemplatePyMod/DocumentObject.py b/src/Mod/TemplatePyMod/DocumentObject.py index a140348c1807..d2bf44267d5b 100644 --- a/src/Mod/TemplatePyMod/DocumentObject.py +++ b/src/Mod/TemplatePyMod/DocumentObject.py @@ -80,6 +80,9 @@ def getGroupOfProperty(self,attr): def getDocumentationOfProperty(self,attr): "returns the documentation string of a given property" return self.__object__.getDocumentationOfProperty(attr) + def getEnumerationsOfProperty(self,attr): + "returns the documentation string of a given property" + return self.__object__.getEnumerationsOfProperty(attr) def touch(self): "marks this object to be recomputed" return self.__object__.touch() diff --git a/src/Mod/Test/Document.py b/src/Mod/Test/Document.py index 0f285314b6e2..5abee2e9e32b 100644 --- a/src/Mod/Test/Document.py +++ b/src/Mod/Test/Document.py @@ -143,6 +143,7 @@ def testObjects(self): self.failUnless(not L1.getDocumentationOfProperty("Source1") == "") self.failUnless(L1.getGroupOfProperty("Source1") == "Feature Test") self.failUnless(L1.getTypeOfProperty("Source1") == []) + self.failUnless(L1.getEnumerationsOfProperty("Source1") is None) # test the constraint types ( both are constraint to percent range) @@ -170,6 +171,7 @@ def testObjects(self): FreeCAD.Console.PrintLog(" exception thrown, OK\n") else: self.fail("no exception thrown") + self.failUnless(sorted(L1.getEnumerationsOfProperty('Enum')) == sorted(['Zero', 'One', 'Two', 'Three', 'Four'])) #self.failUnless(L1.IntegerList == [4711] ) #f = L1.FloatList