Fix missing python addProperty and removeProperty of Document object

This commit is contained in:
Florian Foinant-Willig
2023-03-16 22:48:59 +01:00
committed by wwmayer
parent b1c848b8db
commit a290fe8ae1
2 changed files with 72 additions and 24 deletions

View File

@@ -1,13 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<GenerateModel xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="generateMetaModel_Module.xsd">
<PythonExport
Father="PropertyContainerPy"
Name="DocumentPy"
Twin="Document"
TwinPointer="Document"
Include="App/Document.h"
Namespace="App"
FatherInclude="App/PropertyContainerPy.h"
<PythonExport
Father="PropertyContainerPy"
Name="DocumentPy"
Twin="Document"
TwinPointer="Document"
Include="App/Document.h"
Namespace="App"
FatherInclude="App/PropertyContainerPy.h"
FatherNamespace="App">
<Documentation>
<Author Licence="LGPL" Name="Juergen Riegel" EMail="FreeCAD@juergen-riegel.net" />
@@ -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.</UserDocu>
</Documentation>
</Methode>
<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="removeObject">
<Documentation>
<UserDocu>Remove an object from the document</UserDocu>
@@ -113,7 +130,7 @@ viewType (String): override the view provider type directly, only effective when
<Documentation>
<UserDocu>
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
<UserDocu>
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.
</UserDocu>

View File

@@ -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<DocumentObjectPy*>(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<DocumentObjectPy*>(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<DocumentObjectPy*>(obj)->getDocumentObjectPtr());
}
if (objs.empty())
objs = getDocumentPtr()->getObjects();
@@ -380,7 +411,7 @@ PyObject* DocumentPy::importLinks(PyObject *args)
for (size_t i=0;i<ret.size();++i)
tuple.setItem(i,Py::Object(ret[i]->getPyObject(),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<DocumentObjectPy*>(pyobj)->getDocumentObjectPtr();
if (pyobj)
obj = static_cast<DocumentObjectPy*>(pyobj)->getDocumentObjectPtr();
std::set<DocumentObject *> 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
}