23
#include "PreCompiled.h"
29
#include <App/Document.h>
30
#include <Base/Matrix.h>
31
#include <Base/MatrixPy.h>
32
#include <Base/Stream.h>
34
#include "Application.h"
35
#include "MergeDocuments.h"
37
#include "ViewProviderExtern.h"
40
#include "DocumentPy.h"
41
#include "DocumentPy.cpp"
42
#include <App/DocumentObjectPy.h>
44
#include "ViewProviderDocumentObject.h"
45
#include "ViewProviderDocumentObjectPy.h"
46
#include "ViewProviderPy.h"
47
#include "ViewProviderDocumentObjectPy.h"
53
std::string DocumentPy::representation() const
55
std::stringstream str;
56
str << "<GUI Document object at " << getDocumentPtr() << ">";
62
PyObject* DocumentPy::show(PyObject *args)
65
if (!PyArg_ParseTuple(args, "s;Name of the Feature to show have to be given!", &psFeatStr))
69
getDocumentPtr()->setShow(psFeatStr);
75
PyObject* DocumentPy::hide(PyObject *args)
78
if (!PyArg_ParseTuple(args, "s;Name of the Feature to hide have to be given!", &psFeatStr))
82
getDocumentPtr()->setHide(psFeatStr);
88
PyObject* DocumentPy::setPos(PyObject *args)
93
if (!PyArg_ParseTuple(args, "sO!;Name of the Feature and the transformation matrix have to be given!",
94
&psFeatStr, &(Base::MatrixPy::Type), &pcMatObj))
97
mat = static_cast<Base::MatrixPy*>(pcMatObj)->value();
100
getDocumentPtr()->setPos(psFeatStr,mat);
106
PyObject* DocumentPy::setEdit(PyObject *args)
110
char *subname = nullptr;
111
ViewProvider *vp = nullptr;
112
App::DocumentObject *obj = nullptr;
115
if (PyArg_ParseTuple(args, "s|is", &psFeatStr,&mod,&subname)) {
116
obj = getDocumentPtr()->getDocument()->getObject(psFeatStr);
118
PyErr_Format(Base::PyExc_FC_GeneralError, "No such object found in document: '%s'", psFeatStr);
125
if (!PyArg_ParseTuple(args, "O|is", &pyObj,&mod,&subname))
128
if (PyObject_TypeCheck(pyObj,&App::DocumentObjectPy::Type)) {
129
obj = static_cast<App::DocumentObjectPy*>(pyObj)->getDocumentObjectPtr();
131
else if (PyObject_TypeCheck(pyObj,&ViewProviderPy::Type)) {
132
vp = static_cast<ViewProviderPy*>(pyObj)->getViewProviderPtr();
135
PyErr_SetString(PyExc_TypeError,"Expect the first argument to be string, DocumentObject or ViewObject");
141
if (!obj || !obj->isAttachedToDocument() || !(vp=Application::Instance->getViewProvider(obj))) {
142
PyErr_SetString(PyExc_ValueError,"Invalid document object");
147
bool ok = getDocumentPtr()->setEdit(vp,mod,subname);
149
return PyBool_FromLong(ok ? 1 : 0);
152
PyObject* DocumentPy::getInEdit(PyObject *args)
154
if (!PyArg_ParseTuple(args, ""))
157
ViewProvider* vp = getDocumentPtr()->getInEdit();
159
return vp->getPyObject();
164
PyObject* DocumentPy::resetEdit(PyObject *args)
166
if (!PyArg_ParseTuple(args, ""))
169
getDocumentPtr()->resetEdit();
174
PyObject* DocumentPy::addAnnotation(PyObject *args)
176
char *psAnnoName,*psFileName,*psModName = nullptr;
177
if (!PyArg_ParseTuple(args, "ss|s;Name of the Annotation and a file name have to be given!",
178
&psAnnoName,&psFileName,&psModName))
182
auto pcExt = new ViewProviderExtern();
184
pcExt->setModeByFile(psModName ? psModName : "Main", psFileName);
185
pcExt->adjustDocumentName(getDocumentPtr()->getDocument()->getName());
186
getDocumentPtr()->setAnnotationViewProvider(psAnnoName,pcExt);
193
PyObject* DocumentPy::update(PyObject *args)
195
if (!PyArg_ParseTuple(args, ""))
199
getDocumentPtr()->onUpdate();
205
PyObject* DocumentPy::getObject(PyObject *args)
208
if (!PyArg_ParseTuple(args, "s", &sName))
212
ViewProvider *pcView = getDocumentPtr()->getViewProviderByName(sName);
214
return pcView->getPyObject();
221
PyObject* DocumentPy::activeObject(PyObject *args)
223
if (!PyArg_ParseTuple(args, ""))
227
App::DocumentObject *pcFtr = getDocumentPtr()->getDocument()->getActiveObject();
229
ViewProvider *pcView = getDocumentPtr()->getViewProvider(pcFtr);
230
return pcView->getPyObject();
239
PyObject* DocumentPy::activeView(PyObject *args)
241
if (!PyArg_ParseTuple(args, ""))
245
Gui::MDIView *pcView = getDocumentPtr()->getActiveView();
248
return pcView->getPyObject();
257
PyObject* DocumentPy::mdiViewsOfType(PyObject *args)
260
if (!PyArg_ParseTuple(args, "s", &sType))
263
Base::Type type = Base::Type::fromName(sType);
265
PyErr_Format(PyExc_TypeError, "'%s' is not a valid type", sType);
270
std::list<Gui::MDIView*> views = getDocumentPtr()->getMDIViewsOfType(type);
272
for (auto it : views)
273
list.append(Py::asObject(it->getPyObject()));
274
return Py::new_reference_to(list);
279
PyObject* DocumentPy::save(PyObject *args)
281
if (!PyArg_ParseTuple(args, ""))
285
bool ok = getDocumentPtr()->save();
286
return Py::new_reference_to(Py::Boolean(ok));
291
PyObject* DocumentPy::saveAs(PyObject *args)
293
if (!PyArg_ParseTuple(args, ""))
297
bool ok = getDocumentPtr()->saveAs();
298
return Py::new_reference_to(Py::Boolean(ok));
303
PyObject* DocumentPy::sendMsgToViews(PyObject *args)
306
if (!PyArg_ParseTuple(args, "s", &msg))
310
getDocumentPtr()->sendMsgToViews(msg);
316
PyObject* DocumentPy::mergeProject(PyObject *args)
319
if (!PyArg_ParseTuple(args, "s", &filename))
323
Base::FileInfo fi(filename);
324
Base::ifstream str(fi, std::ios::in | std::ios::binary);
325
App::Document* doc = getDocumentPtr()->getDocument();
326
MergeDocuments md(doc);
327
md.importObjects(str);
334
PyObject* DocumentPy::toggleTreeItem(PyObject *args)
337
const char *subname = nullptr;
339
if (!PyArg_ParseTuple(args,"O!|is",&(App::DocumentObjectPy::Type), &object,&mod,&subname))
342
App::DocumentObject* Object = static_cast<App::DocumentObjectPy*>(object)->getDocumentObjectPtr();
343
App::DocumentObject* parent = nullptr;
345
App::DocumentObject* sobj = Object->getSubObject(subname);
347
PyErr_SetString(PyExc_ValueError, "Subobject not found");
358
auto ActiveVp = dynamic_cast<Gui::ViewProviderDocumentObject*>(getDocumentPtr()->getViewProvider(Object));
361
getDocumentPtr()->signalExpandObject(*ActiveVp, TreeItemMode::ToggleItem, parent, subname);
364
getDocumentPtr()->signalExpandObject(*ActiveVp, TreeItemMode::CollapseItem, parent, subname);
367
getDocumentPtr()->signalExpandObject(*ActiveVp, TreeItemMode::ExpandItem, parent, subname);
370
getDocumentPtr()->signalExpandObject(*ActiveVp, TreeItemMode::ExpandPath, parent, subname);
373
PyErr_SetString(PyExc_ValueError, "Item mode out of range");
380
PyObject* DocumentPy::scrollToTreeItem(PyObject *args)
383
if (!PyArg_ParseTuple(args,"O!",&(Gui::ViewProviderDocumentObjectPy::Type), &view))
386
Gui::ViewProviderDocumentObject* vp = static_cast<ViewProviderDocumentObjectPy*>
387
(view)->getViewProviderDocumentObjectPtr();
388
getDocumentPtr()->signalScrollToObject(*vp);
393
PyObject* DocumentPy::toggleInSceneGraph(PyObject *args) {
395
if (!PyArg_ParseTuple(args,"O!",&(Gui::ViewProviderPy::Type), &view))
398
Gui::ViewProvider* vp = static_cast<ViewProviderPy*>(view)->getViewProviderPtr();
399
getDocumentPtr()->toggleInSceneGraph(vp);
404
Py::Object DocumentPy::getActiveObject() const
406
App::DocumentObject *object = getDocumentPtr()->getDocument()->getActiveObject();
408
ViewProvider *viewObj = getDocumentPtr()->getViewProvider(object);
409
return Py::Object(viewObj->getPyObject(), true);
416
void DocumentPy::setActiveObject(Py::Object )
418
throw Py::AttributeError("'Document' object attribute 'ActiveObject' is read-only");
421
Py::Object DocumentPy::getActiveView() const
423
Gui::MDIView *view = getDocumentPtr()->getActiveView();
426
return Py::Object(view->getPyObject(), true);
433
void DocumentPy::setActiveView(Py::Object )
435
throw Py::AttributeError("'Document' object attribute 'ActiveView' is read-only");
438
Py::Object DocumentPy::getDocument() const
440
App::Document *doc = getDocumentPtr()->getDocument();
443
return Py::Object(doc->getPyObject(), true);
450
Py::Object DocumentPy::getEditingTransform() const
452
return Py::asObject(new Base::MatrixPy(new Base::Matrix4D(
453
getDocumentPtr()->getEditingTransform())));
456
void DocumentPy::setEditingTransform(Py::Object arg)
458
if (!PyObject_TypeCheck(arg.ptr(),&Base::MatrixPy::Type))
459
throw Py::TypeError("Expecting type of matrix");
461
getDocumentPtr()->setEditingTransform(
462
*static_cast<Base::MatrixPy*>(arg.ptr())->getMatrixPtr());
465
Py::Object DocumentPy::getInEditInfo() const {
466
ViewProviderDocumentObject *vp = nullptr;
467
std::string subname,subelement;
469
getDocumentPtr()->getInEdit(&vp,&subname,&mode,&subelement);
470
if (!vp || !vp->getObject() || !vp->getObject()->isAttachedToDocument())
473
return Py::TupleN(Py::Object(vp->getObject()->getPyObject(),true),
474
Py::String(subname),Py::String(subelement),Py::Int(mode));
477
void DocumentPy::setInEditInfo(Py::Object arg)
481
if (!PyArg_ParseTuple(arg.ptr(), "O!s", &Gui::ViewProviderDocumentObjectPy::Type,
483
throw Py::Exception();
485
getDocumentPtr()->setInEdit(static_cast<ViewProviderDocumentObjectPy*>(
486
pyobj)->getViewProviderDocumentObjectPtr(),subname);
489
Py::Int DocumentPy::getEditMode() const
492
getDocumentPtr()->getInEdit(nullptr,nullptr,&mode);
494
return Py::Int(mode);
497
Py::Boolean DocumentPy::getTransacting() const
499
return {getDocumentPtr()->isPerformingTransaction()};
502
Py::Boolean DocumentPy::getModified() const
504
return {getDocumentPtr()->isModified()};
507
void DocumentPy::setModified(Py::Boolean arg)
509
getDocumentPtr()->setModified(arg);
512
Py::List DocumentPy::getTreeRootObjects() const
514
std::vector<App::DocumentObject*> objs = getDocumentPtr()->getTreeRootObjects();
517
for (auto obj : objs) {
519
res.append(Py::Object(obj->getPyObject(), true));
526
PyObject *DocumentPy::getCustomAttributes(const char* attr) const
533
if (!this->ob_type->tp_dict) {
534
if (PyType_Ready(this->ob_type) < 0)
538
PyObject* item = PyDict_GetItemString(this->ob_type->tp_dict, attr);
543
ViewProvider* obj = getDocumentPtr()->getViewProviderByName(attr);
545
return (obj ? obj->getPyObject() : nullptr);
548
int DocumentPy::setCustomAttributes(const char* attr, PyObject *)
555
if (!this->ob_type->tp_dict) {
556
if (PyType_Ready(this->ob_type) < 0)
560
PyObject* item = PyDict_GetItemString(this->ob_type->tp_dict, attr);
564
ViewProvider* obj = getDocumentPtr()->getViewProviderByName(attr);
566
std::stringstream str;
567
str << "'Document' object attribute '" << attr
568
<< "' must not be set this way" << std::ends;
569
throw Py::AttributeError(str.str());