diff --git a/src/App/DocumentPy.xml b/src/App/DocumentPy.xml index 14e6f89db6..5be462037f 100644 --- a/src/App/DocumentPy.xml +++ b/src/App/DocumentPy.xml @@ -1,13 +1,13 @@ - @@ -104,6 +104,23 @@ attach (Boolean): if True, then bind the document object first before adding to viewType (String): override the view provider type directly, only effective when attach is False. + + + + 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. + + + Remove an object from the document @@ -113,7 +130,7 @@ viewType (String): override the view provider type directly, only effective when copyObject(object, with_dependencies=False, return_all=False) -Copy an object or objects from another document to this document. +Copy an object or objects from another document to this document. object: can either a single object or sequence of objects with_dependencies: if True, all internal dependent objects are copied too. @@ -127,7 +144,7 @@ return_all: if True, return all copied objects, or else return only the copied moveObject(object, bool with_dependencies = False) Transfers an object from another document to this document. - + object: can either a single object or sequence of objects with_dependencies: if True, all internal dependent objects are copied too. diff --git a/src/App/DocumentPyImp.cpp b/src/App/DocumentPyImp.cpp index 697127c086..874b2a8c5a 100644 --- a/src/App/DocumentPyImp.cpp +++ b/src/App/DocumentPyImp.cpp @@ -40,6 +40,37 @@ using namespace App; +PyObject* DocumentPy::addProperty(PyObject *args) +{ + char *sType,*sName=nullptr,*sGroup=nullptr,*sDoc=nullptr; + 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)) + return nullptr; + + if (sDoc) { + sDocStr = sDoc; + PyMem_Free(sDoc); + } + + getDocumentPtr()->addDynamicProperty(sType,sName,sGroup,sDocStr.c_str(),attr, + Base::asBoolean(ro), Base::asBoolean(hd)); + + return Py::new_reference_to(this); +} + +PyObject* DocumentPy::removeProperty(PyObject *args) +{ + char *sName; + if (!PyArg_ParseTuple(args, "s", &sName)) + return nullptr; + + bool ok = getDocumentPtr()->removeDynamicProperty(sName); + return Py_BuildValue("O", (ok ? Py_True : Py_False)); +} + // returns a string which represent the object e.g. when printed in python std::string DocumentPy::representation() const { @@ -363,13 +394,13 @@ PyObject* DocumentPy::importLinks(PyObject *args) objs.push_back(static_cast(seq[i].ptr())->getDocumentObjectPtr()); } } - else { - Base::PyTypeCheck(&obj, &DocumentObjectPy::Type, - "Expect first argument to be either a document object, sequence of document objects or None"); - if (obj) - objs.push_back(static_cast(obj)->getDocumentObjectPtr()); - } - + else { + Base::PyTypeCheck(&obj, &DocumentObjectPy::Type, + "Expect first argument to be either a document object, sequence of document objects or None"); + if (obj) + objs.push_back(static_cast(obj)->getDocumentObjectPtr()); + } + if (objs.empty()) objs = getDocumentPtr()->getObjects(); @@ -380,7 +411,7 @@ PyObject* DocumentPy::importLinks(PyObject *args) for (size_t i=0;igetPyObject(),true)); return Py::new_reference_to(tuple); - } + } PY_CATCH } @@ -857,20 +888,20 @@ PyObject* DocumentPy::getLinksTo(PyObject *args) return nullptr; PY_TRY { - Base::PyTypeCheck(&pyobj, &DocumentObjectPy::Type, "Expect the first argument of type document object"); + Base::PyTypeCheck(&pyobj, &DocumentObjectPy::Type, "Expect the first argument of type document object"); DocumentObject *obj = nullptr; - if (pyobj) - obj = static_cast(pyobj)->getDocumentObjectPtr(); - + if (pyobj) + obj = static_cast(pyobj)->getDocumentObjectPtr(); + std::set links; getDocumentPtr()->getLinksTo(links,obj,options,count); Py::Tuple ret(links.size()); int i=0; for (auto o : links) ret.setItem(i++,Py::Object(o->getPyObject(),true)); - + return Py::new_reference_to(ret); - } + } PY_CATCH }