Gui: Clean up code in DocumentPyImp.cpp

This commit is contained in:
marioalexis
2022-02-05 16:02:29 -03:00
committed by wwmayer
parent 942618ce72
commit 63878880ef

View File

@@ -59,46 +59,48 @@ std::string DocumentPy::representation(void) const
}
PyObject* DocumentPy::show(PyObject *args)
PyObject* DocumentPy::show(PyObject *args)
{
char *psFeatStr;
if (!PyArg_ParseTuple(args, "s;Name of the Feature to show have to be given!",&psFeatStr)) // convert args: Python->C
return NULL; // NULL triggers exception
if (!PyArg_ParseTuple(args, "s;Name of the Feature to show have to be given!", &psFeatStr))
return nullptr;
PY_TRY {
getDocumentPtr()->setShow(psFeatStr);
Py_Return;
} PY_CATCH;
}
PY_CATCH;
}
PyObject* DocumentPy::hide(PyObject *args)
PyObject* DocumentPy::hide(PyObject *args)
{
char *psFeatStr;
if (!PyArg_ParseTuple(args, "s;Name of the Feature to hide have to be given!",&psFeatStr)) // convert args: Python->C
return NULL; // NULL triggers exception
if (!PyArg_ParseTuple(args, "s;Name of the Feature to hide have to be given!", &psFeatStr))
return nullptr;
PY_TRY {
getDocumentPtr()->setHide(psFeatStr);
Py_Return;
} PY_CATCH;
}
PY_CATCH;
}
PyObject* DocumentPy::setPos(PyObject *args)
PyObject* DocumentPy::setPos(PyObject *args)
{
char *psFeatStr;
Base::Matrix4D mat;
PyObject *pcMatObj;
if (!PyArg_ParseTuple(args, "sO!;Name of the Feature and the transformation matrix have to be given!",
&psFeatStr,
&(Base::MatrixPy::Type), &pcMatObj)) // convert args: Python->C
return NULL; // NULL triggers exception
&psFeatStr, &(Base::MatrixPy::Type), &pcMatObj))
return nullptr;
mat = static_cast<Base::MatrixPy*>(pcMatObj)->value();
PY_TRY {
getDocumentPtr()->setPos(psFeatStr,mat);
Py_Return;
} PY_CATCH;
}
PY_CATCH;
}
PyObject* DocumentPy::setEdit(PyObject *args)
@@ -150,113 +152,118 @@ PyObject* DocumentPy::setEdit(PyObject *args)
PyObject* DocumentPy::getInEdit(PyObject *args)
{
if (!PyArg_ParseTuple(args, ""))
return NULL;
return nullptr;
ViewProvider* vp = getDocumentPtr()->getInEdit();
if (vp) {
if (vp)
return vp->getPyObject();
}
Py_Return;
}
PyObject* DocumentPy::resetEdit(PyObject *args)
{
if (!PyArg_ParseTuple(args, ";No arguments allowed")) // convert args: Python->C
return NULL; // NULL triggers exception
if (!PyArg_ParseTuple(args, ""))
return nullptr;
getDocumentPtr()->resetEdit();
Py_Return;
}
PyObject* DocumentPy::addAnnotation(PyObject *args)
PyObject* DocumentPy::addAnnotation(PyObject *args)
{
char *psAnnoName,*psFileName,*psModName=0;
if (!PyArg_ParseTuple(args, "ss|s;Name of the Annotation and a file name have to be given!",&psAnnoName,&psFileName,&psModName)) // convert args: Python->C
return NULL; // NULL triggers exception
char *psAnnoName,*psFileName,*psModName = nullptr;
if (!PyArg_ParseTuple(args, "ss|s;Name of the Annotation and a file name have to be given!",
&psAnnoName,&psFileName,&psModName))
return nullptr;
PY_TRY {
ViewProviderExtern *pcExt = new ViewProviderExtern();
pcExt->setModeByFile(psModName?psModName:"Main",psFileName);
pcExt->setModeByFile(psModName ? psModName : "Main", psFileName);
pcExt->adjustDocumentName(getDocumentPtr()->getDocument()->getName());
getDocumentPtr()->setAnnotationViewProvider(psAnnoName,pcExt);
Py_Return;
} PY_CATCH;
}
PY_CATCH;
}
PyObject* DocumentPy::update(PyObject *args)
PyObject* DocumentPy::update(PyObject *args)
{
if (!PyArg_ParseTuple(args, "")) // convert args: Python->C
return NULL; // NULL triggers exception
if (!PyArg_ParseTuple(args, ""))
return nullptr;
PY_TRY {
getDocumentPtr()->onUpdate();
Py_Return;
} PY_CATCH;
}
PY_CATCH;
}
PyObject* DocumentPy::getObject(PyObject *args)
PyObject* DocumentPy::getObject(PyObject *args)
{
char *sName;
if (!PyArg_ParseTuple(args, "s",&sName)) // convert args: Python->C
return NULL; // NULL triggers exception
if (!PyArg_ParseTuple(args, "s", &sName))
return nullptr;
PY_TRY {
ViewProvider *pcView = getDocumentPtr()->getViewProviderByName(sName);
if (pcView)
return pcView->getPyObject();
else {
else
Py_Return;
}
} PY_CATCH;
}
PY_CATCH;
}
PyObject* DocumentPy::activeObject(PyObject *args)
PyObject* DocumentPy::activeObject(PyObject *args)
{
if (!PyArg_ParseTuple(args, "")) // convert args: Python->C
return NULL; // NULL triggers exception
if (!PyArg_ParseTuple(args, ""))
return nullptr;
PY_TRY {
App::DocumentObject *pcFtr = getDocumentPtr()->getDocument()->getActiveObject();
if (pcFtr) {
ViewProvider *pcView = getDocumentPtr()->getViewProvider(pcFtr);
return pcView->getPyObject();
} else {
}
else {
Py_Return;
}
} PY_CATCH;
}
PY_CATCH;
}
PyObject* DocumentPy::activeView(PyObject *args)
PyObject* DocumentPy::activeView(PyObject *args)
{
if (!PyArg_ParseTuple(args, "")) // convert args: Python->C
return NULL; // NULL triggers exception
if (!PyArg_ParseTuple(args, ""))
return nullptr;
PY_TRY {
Gui::MDIView *pcView = getDocumentPtr()->getActiveView();
if (pcView){
// already incremented in getPyObject().
return pcView->getPyObject();
} else {
}
else {
Py_Return;
}
} PY_CATCH;
}
PY_CATCH;
}
PyObject* DocumentPy::mdiViewsOfType(PyObject *args)
PyObject* DocumentPy::mdiViewsOfType(PyObject *args)
{
char* sType;
if (!PyArg_ParseTuple(args, "s", &sType)) // convert args: Python->C
return NULL; // NULL triggers exception
if (!PyArg_ParseTuple(args, "s", &sType))
return nullptr;
Base::Type type = Base::Type::fromName(sType);
if (type == Base::Type::badType()) {
if (type.isBad()) {
PyErr_Format(Base::BaseExceptionFreeCADError, "'%s' is not a valid type", sType);
return NULL;
return nullptr;
}
PY_TRY {
@@ -265,26 +272,28 @@ PyObject* DocumentPy::mdiViewsOfType(PyObject *args)
for (std::list<Gui::MDIView*>::iterator it = views.begin(); it != views.end(); ++it)
list.append(Py::asObject((*it)->getPyObject()));
return Py::new_reference_to(list);
} PY_CATCH;
}
PY_CATCH;
}
PyObject* DocumentPy::sendMsgToViews(PyObject *args)
PyObject* DocumentPy::sendMsgToViews(PyObject *args)
{
char* msg;
if (!PyArg_ParseTuple(args, "s", &msg)) // convert args: Python->C
return NULL; // NULL triggers exception
if (!PyArg_ParseTuple(args, "s", &msg))
return nullptr;
PY_TRY {
getDocumentPtr()->sendMsgToViews(msg);
Py_Return;
} PY_CATCH;
}
PY_CATCH;
}
PyObject* DocumentPy::mergeProject(PyObject *args)
{
char* filename;
if (!PyArg_ParseTuple(args, "s", &filename)) // convert args: Python->C
return NULL; // NULL triggers exception
if (!PyArg_ParseTuple(args, "s", &filename))
return nullptr;
PY_TRY {
Base::FileInfo fi(filename);
@@ -292,41 +301,53 @@ PyObject* DocumentPy::mergeProject(PyObject *args)
App::Document* doc = getDocumentPtr()->getDocument();
MergeDocuments md(doc);
md.importObjects(str);
Py_Return;
} PY_CATCH;
}
PY_CATCH;
}
PyObject* DocumentPy::toggleTreeItem(PyObject *args)
{
PyObject *object=0;
const char *subname=0;
PyObject *object;
const char *subname = nullptr;
int mod = 0;
if (PyArg_ParseTuple(args,"O!|is",&(App::DocumentObjectPy::Type), &object,&mod,&subname)) {
App::DocumentObject* Object = static_cast<App::DocumentObjectPy*>(object)->getDocumentObjectPtr();
// Should be set!
assert(Object);
if (!PyArg_ParseTuple(args,"O!|is",&(App::DocumentObjectPy::Type), &object,&mod,&subname))
return nullptr;
App::DocumentObject *parent = 0;
if (subname) {
auto sobj = Object->getSubObject(subname);
if (!sobj)
throw Py::RuntimeError("Sub-object not found");
parent = Object;
Object = sobj;
App::DocumentObject* Object = static_cast<App::DocumentObjectPy*>(object)->getDocumentObjectPtr();
App::DocumentObject* parent = nullptr;
if (subname) {
App::DocumentObject* sobj = Object->getSubObject(subname);
if (!sobj) {
PyErr_SetString(PyExc_ValueError, "Subobject not found");
return nullptr;
}
// get the gui document of the Assembly Item
//ActiveAppDoc = Item->getDocument();
//ActiveGuiDoc = Gui::Application::Instance->getDocument(getDocumentPtr());
Gui::ViewProviderDocumentObject* ActiveVp = dynamic_cast<Gui::ViewProviderDocumentObject*> (getDocumentPtr()->getViewProvider(Object));
assert(ActiveVp);
switch (mod) {
case 0: getDocumentPtr()->signalExpandObject(*ActiveVp, TreeItemMode::ToggleItem, parent, subname); break;
case 1: getDocumentPtr()->signalExpandObject(*ActiveVp, TreeItemMode::CollapseItem, parent, subname); break;
case 2: getDocumentPtr()->signalExpandObject(*ActiveVp, TreeItemMode::ExpandItem, parent, subname); break;
case 3: getDocumentPtr()->signalExpandObject(*ActiveVp, TreeItemMode::ExpandPath, parent, subname); break;
default: break;
}
parent = Object;
Object = sobj;
}
// get the gui document of the Assembly Item
//ActiveAppDoc = Item->getDocument();
//ActiveGuiDoc = Gui::Application::Instance->getDocument(getDocumentPtr());
Gui::ViewProviderDocumentObject* ActiveVp = dynamic_cast<Gui::ViewProviderDocumentObject*>(getDocumentPtr()->getViewProvider(Object));
switch (mod) {
case 0:
getDocumentPtr()->signalExpandObject(*ActiveVp, TreeItemMode::ToggleItem, parent, subname);
break;
case 1:
getDocumentPtr()->signalExpandObject(*ActiveVp, TreeItemMode::CollapseItem, parent, subname);
break;
case 2:
getDocumentPtr()->signalExpandObject(*ActiveVp, TreeItemMode::ExpandItem, parent, subname);
break;
case 3:
getDocumentPtr()->signalExpandObject(*ActiveVp, TreeItemMode::ExpandPath, parent, subname);
break;
default:
PyErr_SetString(PyExc_ValueError, "Item mode out of range");
return nullptr;
}
Py_Return;
@@ -336,21 +357,23 @@ PyObject* DocumentPy::scrollToTreeItem(PyObject *args)
{
PyObject *view;
if (!PyArg_ParseTuple(args,"O!",&(Gui::ViewProviderDocumentObjectPy::Type), &view))
return 0;
return nullptr;
Gui::ViewProviderDocumentObject* vp = static_cast<ViewProviderDocumentObjectPy*>
(view)->getViewProviderDocumentObjectPtr();
getDocumentPtr()->signalScrollToObject(*vp);
Py_Return;
}
PyObject* DocumentPy::toggleInSceneGraph(PyObject *args) {
PyObject *view;
if (!PyArg_ParseTuple(args,"O!",&(Gui::ViewProviderPy::Type), &view))
return 0;
return nullptr;
Gui::ViewProvider* vp = static_cast<ViewProviderPy*>(view)->getViewProviderPtr();
getDocumentPtr()->toggleInSceneGraph(vp);
Py_Return;
}
@@ -360,12 +383,13 @@ Py::Object DocumentPy::getActiveObject(void) const
if (object) {
ViewProvider *viewObj = getDocumentPtr()->getViewProvider(object);
return Py::Object(viewObj->getPyObject(), true);
} else {
}
else {
return Py::None();
}
}
void DocumentPy::setActiveObject(Py::Object /*arg*/)
void DocumentPy::setActiveObject(Py::Object /*arg*/)
{
throw Py::AttributeError("'Document' object attribute 'ActiveObject' is read-only");
}
@@ -376,12 +400,13 @@ Py::Object DocumentPy::getActiveView(void) const
if (view) {
// already incremented in getPyObject().
return Py::Object(view->getPyObject(), true);
} else {
}
else {
return Py::None();
}
}
void DocumentPy::setActiveView(Py::Object /*arg*/)
void DocumentPy::setActiveView(Py::Object /*arg*/)
{
throw Py::AttributeError("'Document' object attribute 'ActiveView' is read-only");
}
@@ -392,51 +417,61 @@ Py::Object DocumentPy::getDocument(void) const
if (doc) {
// already incremented in getPyObject().
return Py::Object(doc->getPyObject(), true);
} else {
}
else {
return Py::None();
}
}
Py::Object DocumentPy::getEditingTransform(void) const {
Py::Object DocumentPy::getEditingTransform(void) const
{
return Py::asObject(new Base::MatrixPy(new Base::Matrix4D(
getDocumentPtr()->getEditingTransform())));
}
void DocumentPy::setEditingTransform(Py::Object arg) {
void DocumentPy::setEditingTransform(Py::Object arg)
{
if (!PyObject_TypeCheck(arg.ptr(),&Base::MatrixPy::Type))
throw Py::TypeError("Expecting type of matrix");
getDocumentPtr()->setEditingTransform(
*static_cast<Base::MatrixPy*>(arg.ptr())->getMatrixPtr());
}
Py::Object DocumentPy::getInEditInfo(void) const {
ViewProviderDocumentObject *vp = 0;
ViewProviderDocumentObject *vp = nullptr;
std::string subname,subelement;
int mode = 0;
getDocumentPtr()->getInEdit(&vp,&subname,&mode,&subelement);
if (!vp || !vp->getObject() || !vp->getObject()->getNameInDocument())
return Py::None();
return Py::TupleN(Py::Object(vp->getObject()->getPyObject(),true),
Py::String(subname),Py::String(subelement),Py::Int(mode));
}
void DocumentPy::setInEditInfo(Py::Object arg) {
PyObject *pyobj = 0;
const char *subname = 0;
if (!PyArg_ParseTuple(arg.ptr(), "O!s",
&Gui::ViewProviderDocumentObjectPy::Type, &pyobj,&subname))
void DocumentPy::setInEditInfo(Py::Object arg)
{
PyObject *pyobj;
const char *subname;
if (!PyArg_ParseTuple(arg.ptr(), "O!s", &Gui::ViewProviderDocumentObjectPy::Type,
&pyobj, &subname))
throw Py::Exception();
getDocumentPtr()->setInEdit(static_cast<ViewProviderDocumentObjectPy*>(
pyobj)->getViewProviderDocumentObjectPtr(),subname);
}
Py::Int DocumentPy::getEditMode(void) const {
Py::Int DocumentPy::getEditMode(void) const
{
int mode = -1;
getDocumentPtr()->getInEdit(0,0,&mode);
return Py::Int(mode);
}
Py::Boolean DocumentPy::getTransacting() const {
Py::Boolean DocumentPy::getTransacting() const
{
return Py::Boolean(getDocumentPtr()->isPerformingTransaction());
}
@@ -452,15 +487,19 @@ PyObject *DocumentPy::getCustomAttributes(const char* attr) const
// with the same name as an attribute. If so, we return 0 as other-
// wise it wouldn't be possible to address this attribute any more.
// The object must then be addressed by the getObject() method directly.
if (this->ob_type->tp_dict == NULL) {
if (!this->ob_type->tp_dict) {
if (PyType_Ready(this->ob_type) < 0)
return 0;
return nullptr;
}
PyObject* item = PyDict_GetItemString(this->ob_type->tp_dict, attr);
if (item) return 0;
if (item)
return nullptr;
// search for an object with this name
ViewProvider* obj = getDocumentPtr()->getViewProviderByName(attr);
return (obj ? obj->getPyObject() : 0);
return (obj ? obj->getPyObject() : nullptr);
}
int DocumentPy::setCustomAttributes(const char* attr, PyObject *)
@@ -470,12 +509,15 @@ int DocumentPy::setCustomAttributes(const char* attr, PyObject *)
// with the same name as an attribute. If so, we return 0 as other-
// wise it wouldn't be possible to address this attribute any more.
// The object must then be addressed by the getObject() method directly.
if (this->ob_type->tp_dict == NULL) {
if (!this->ob_type->tp_dict) {
if (PyType_Ready(this->ob_type) < 0)
return 0;
}
PyObject* item = PyDict_GetItemString(this->ob_type->tp_dict, attr);
if (item) return 0;
if (item)
return 0;
ViewProvider* obj = getDocumentPtr()->getViewProviderByName(attr);
if (obj) {
std::stringstream str;
@@ -486,5 +528,3 @@ int DocumentPy::setCustomAttributes(const char* attr, PyObject *)
return 0;
}