diff --git a/src/App/TransactionalObject.cpp b/src/App/TransactionalObject.cpp index 0fc2e3a916..a5729edd5b 100644 --- a/src/App/TransactionalObject.cpp +++ b/src/App/TransactionalObject.cpp @@ -24,9 +24,11 @@ #include "PreCompiled.h" #ifndef _PreComp_ +# include #endif #include +#include #include "Document.h" #include "TransactionalObject.h" @@ -58,3 +60,19 @@ void TransactionalObject::onBeforeChangeProperty(Document *doc, const Property * { doc->onBeforeChangeProperty(this, prop); } + +App::Property* TransactionalObject::addDynamicProperty(const char*, const char*, + const char*, const char*, + short, bool, bool) +{ + std::stringstream str; + str << "Type " << this->getTypeId().getName() << " cannot dynamically add properties"; + throw Base::RuntimeError(str.str()); +} + +bool TransactionalObject::removeDynamicProperty(const char*) +{ + std::stringstream str; + str << "Type " << this->getTypeId().getName() << " cannot dynamically remove properties"; + throw Base::RuntimeError(str.str()); +} diff --git a/src/App/TransactionalObject.h b/src/App/TransactionalObject.h index ecdcb82693..50e0747ac5 100644 --- a/src/App/TransactionalObject.h +++ b/src/App/TransactionalObject.h @@ -45,6 +45,12 @@ public: virtual bool isAttachedToDocument() const; virtual const char* detachFromDocument(); + virtual App::Property* addDynamicProperty( + const char*, const char* = 0, + const char* = 0, const char* = 0, + short = 0, bool = false, bool = false); + virtual bool removeDynamicProperty(const char*); + protected: void onBeforeChangeProperty(Document *doc, const Property *prop); }; diff --git a/src/Gui/ViewProviderPy.xml b/src/Gui/ViewProviderPy.xml index da0a277bc1..9087834a4b 100644 --- a/src/Gui/ViewProviderPy.xml +++ b/src/Gui/ViewProviderPy.xml @@ -13,7 +13,29 @@ This is the ViewProvider base class - + + + + addProperty(string, string) -- Add a generic property. + The first argument specifies the type, the second the + name of the property. + + + + + + + removeProperty(string) -- Remove a generic property. + Note, you can only remove user-defined properties but not built-in ones. + + + + + + A list of supported property types + + + Show the object diff --git a/src/Gui/ViewProviderPyImp.cpp b/src/Gui/ViewProviderPyImp.cpp index 1cc3c2d576..ca40ca0e71 100644 --- a/src/Gui/ViewProviderPyImp.cpp +++ b/src/Gui/ViewProviderPyImp.cpp @@ -53,6 +53,71 @@ std::string ViewProviderPy::representation(void) const return ""; } +PyObject* ViewProviderPy::addProperty(PyObject *args) +{ + char *sType,*sName=0,*sGroup=0,*sDoc=0; + short attr=0; + std::string sDocStr; + PyObject *ro = Py_False, *hd = Py_False; + if (!PyArg_ParseTuple(args, "s|ssethO!O!", &sType,&sName,&sGroup,"utf-8",&sDoc,&attr, + &PyBool_Type, &ro, &PyBool_Type, &hd)) // convert args: Python->C + return NULL; // NULL triggers exception + + if (sDoc) { + sDocStr = sDoc; + PyMem_Free(sDoc); + } + + App::Property* prop=0; + try { + prop = getViewProviderPtr()->addDynamicProperty(sType,sName,sGroup,sDocStr.c_str(),attr, + PyObject_IsTrue(ro) ? true : false, PyObject_IsTrue(hd) ? true : false); + } + catch (const Base::Exception& e) { + throw Py::RuntimeError(e.what()); + } + if (!prop) { + std::stringstream str; + str << "No property found of type '" << sType << "'" << std::ends; + throw Py::Exception(Base::BaseExceptionFreeCADError,str.str()); + } + + return Py::new_reference_to(this); +} + +PyObject* ViewProviderPy::removeProperty(PyObject *args) +{ + char *sName; + if (!PyArg_ParseTuple(args, "s", &sName)) + return NULL; + + try { + bool ok = getViewProviderPtr()->removeDynamicProperty(sName); + return Py_BuildValue("O", (ok ? Py_True : Py_False)); + } + catch (const Base::Exception& e) { + throw Py::RuntimeError(e.what()); + } +} + +PyObject* ViewProviderPy::supportedProperties(PyObject *args) +{ + if (!PyArg_ParseTuple(args, "")) // convert args: Python->C + return NULL; // NULL triggers exception + + std::vector ary; + Base::Type::getAllDerivedFrom(App::Property::getClassTypeId(), ary); + Py::List res; + for (std::vector::iterator it = ary.begin(); it != ary.end(); ++it) { + Base::BaseClass *data = static_cast(it->createInstance()); + if (data) { + delete data; + res.append(Py::String(it->getName())); + } + } + return Py::new_reference_to(res); +} + PyObject* ViewProviderPy::show(PyObject *args) { if (!PyArg_ParseTuple(args, "")) // convert args: Python->C diff --git a/src/Gui/ViewProviderPythonFeaturePy.xml b/src/Gui/ViewProviderPythonFeaturePy.xml index e7b882de28..19727c0e66 100644 --- a/src/Gui/ViewProviderPythonFeaturePy.xml +++ b/src/Gui/ViewProviderPythonFeaturePy.xml @@ -13,27 +13,5 @@ This is the view provider class for Python features - - - - addProperty(string, string) -- Add a generic property. - The first argument specifies the type, the second the - name of the property. - - - - - - - removeProperty(string) -- Remove a generic property. - Note, you can only remove user-defined properties but not built-in ones. - - - - - - A list of supported property types - - diff --git a/src/Gui/ViewProviderPythonFeaturePyImp.cpp b/src/Gui/ViewProviderPythonFeaturePyImp.cpp index c13cc5f5a8..d8d5338441 100644 --- a/src/Gui/ViewProviderPythonFeaturePyImp.cpp +++ b/src/Gui/ViewProviderPythonFeaturePyImp.cpp @@ -39,62 +39,6 @@ std::string ViewProviderPythonFeaturePy::representation(void) const return ""; } -PyObject* ViewProviderPythonFeaturePy::addProperty(PyObject *args) -{ - char *sType,*sName=0,*sGroup=0,*sDoc=0; - short attr=0; - std::string sDocStr; - PyObject *ro = Py_False, *hd = Py_False; - if (!PyArg_ParseTuple(args, "s|ssethO!O!", &sType,&sName,&sGroup,"utf-8",&sDoc,&attr, - &PyBool_Type, &ro, &PyBool_Type, &hd)) // convert args: Python->C - return NULL; // NULL triggers exception - - if (sDoc) { - sDocStr = sDoc; - PyMem_Free(sDoc); - } - - App::Property* prop=0; - prop = getViewProviderPythonFeaturePtr()->addDynamicProperty(sType,sName,sGroup,sDocStr.c_str(),attr, - PyObject_IsTrue(ro) ? true : false, PyObject_IsTrue(hd) ? true : false); - - if (!prop) { - std::stringstream str; - str << "No property found of type '" << sType << "'" << std::ends; - throw Py::Exception(Base::BaseExceptionFreeCADError,str.str()); - } - - return Py::new_reference_to(this); -} - -PyObject* ViewProviderPythonFeaturePy::removeProperty(PyObject *args) -{ - char *sName; - if (!PyArg_ParseTuple(args, "s", &sName)) - return NULL; - - bool ok = getViewProviderPythonFeaturePtr()->removeDynamicProperty(sName); - return Py_BuildValue("O", (ok ? Py_True : Py_False)); -} - -PyObject* ViewProviderPythonFeaturePy::supportedProperties(PyObject *args) -{ - if (!PyArg_ParseTuple(args, "")) // convert args: Python->C - return NULL; // NULL triggers exception - - std::vector ary; - Base::Type::getAllDerivedFrom(App::Property::getClassTypeId(), ary); - Py::List res; - for (std::vector::iterator it = ary.begin(); it != ary.end(); ++it) { - Base::BaseClass *data = static_cast(it->createInstance()); - if (data) { - delete data; - res.append(Py::String(it->getName())); - } - } - return Py::new_reference_to(res); -} - PyObject *ViewProviderPythonFeaturePy::getCustomAttributes(const char* attr) const { PY_TRY{