dynamic properties:

+ reimplment addDynamicProperty and removeDynamicProperty in TransactionalObject to raise exceptions
+ move addProperty, removeProperty and supportedProperties from ViewProviderPythonFeaturePy to ViewProviderPy
This commit is contained in:
wmayer
2016-11-08 19:55:17 +01:00
parent 1c5cc8bf62
commit 37339057f8
6 changed files with 112 additions and 79 deletions

View File

@@ -24,9 +24,11 @@
#include "PreCompiled.h"
#ifndef _PreComp_
# include <sstream>
#endif
#include <Base/Writer.h>
#include <Base/Exception.h>
#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());
}

View File

@@ -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);
};

View File

@@ -13,7 +13,29 @@
<Author Licence="LGPL" Name="Werner Mayer" EMail="wmayer@users.sourceforge.net" />
<UserDocu>This is the ViewProvider base class</UserDocu>
</Documentation>
<Methode Name="show">
<Methode Name="addProperty">
<Documentation>
<UserDocu>
addProperty(string, string) -- Add a generic property.
The first argument specifies the type, the second the
name of the property.
</UserDocu>
</Documentation>
</Methode>
<Methode Name="removeProperty">
<Documentation>
<UserDocu>
removeProperty(string) -- Remove a generic property.
Note, you can only remove user-defined properties but not built-in ones.
</UserDocu>
</Documentation>
</Methode>
<Methode Name="supportedProperties">
<Documentation>
<UserDocu>A list of supported property types</UserDocu>
</Documentation>
</Methode>
<Methode Name="show">
<Documentation>
<UserDocu>Show the object</UserDocu>
</Documentation>

View File

@@ -53,6 +53,71 @@ std::string ViewProviderPy::representation(void) const
return "<View provider object>";
}
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<Base::Type> ary;
Base::Type::getAllDerivedFrom(App::Property::getClassTypeId(), ary);
Py::List res;
for (std::vector<Base::Type>::iterator it = ary.begin(); it != ary.end(); ++it) {
Base::BaseClass *data = static_cast<Base::BaseClass*>(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

View File

@@ -13,27 +13,5 @@
<Author Licence="LGPL" Name="Werner Mayer" EMail="wmayer@users.sourceforge.net" />
<UserDocu>This is the view provider class for Python features</UserDocu>
</Documentation>
<Methode Name="addProperty">
<Documentation>
<UserDocu>
addProperty(string, string) -- Add a generic property.
The first argument specifies the type, the second the
name of the property.
</UserDocu>
</Documentation>
</Methode>
<Methode Name="removeProperty">
<Documentation>
<UserDocu>
removeProperty(string) -- Remove a generic property.
Note, you can only remove user-defined properties but not built-in ones.
</UserDocu>
</Documentation>
</Methode>
<Methode Name="supportedProperties">
<Documentation>
<UserDocu>A list of supported property types</UserDocu>
</Documentation>
</Methode>
</PythonExport>
</GenerateModel>

View File

@@ -39,62 +39,6 @@ std::string ViewProviderPythonFeaturePy::representation(void) const
return "<ViewProviderPythonFeature object>";
}
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<Base::Type> ary;
Base::Type::getAllDerivedFrom(App::Property::getClassTypeId(), ary);
Py::List res;
for (std::vector<Base::Type>::iterator it = ary.begin(); it != ary.end(); ++it) {
Base::BaseClass *data = static_cast<Base::BaseClass*>(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{