Debian Codegraph @

src/Gui/DocumentPyImp.cpp

raw
1 /***************************************************************************
2 * Copyright (c) 2007 Werner Mayer <wmayer[at]users.sourceforge.net> *
3 * *
4 * This file is part of the FreeCAD CAx development system. *
5 * *
6 * This library is free software; you can redistribute it and/or *
7 * modify it under the terms of the GNU Library General Public *
8 * License as published by the Free Software Foundation; either *
9 * version 2 of the License, or (at your option) any later version. *
10 * *
11 * This library is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU Library General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU Library General Public *
17 * License along with this library; see the file COPYING.LIB. If not, *
18 * write to the Free Software Foundation, Inc., 59 Temple Place, *
19 * Suite 330, Boston, MA 02111-1307, USA *
20 * *
21 ***************************************************************************/
22
23 #include <sstream>
24
25 #include <App/Document.h>
26 #include <Base/Matrix.h>
27 #include <Base/MatrixPy.h>
28 #include <Base/Stream.h>
29
30 #include "Application.h"
31 #include "MergeDocuments.h"
32 #include "MDIView.h"
33 #include "ViewProviderExtern.h"
34
35 // inclusion of the generated files (generated out of DocumentPy.pyi)
36 #include "DocumentPy.h"
37 #include "DocumentPy.cpp"
38 #include <App/DocumentObjectPy.h>
39 #include "Tree.h"
40 #include "ViewProviderDocumentObject.h"
41 #include "ViewProviderDocumentObjectPy.h"
42 #include "ViewProviderPy.h"
43 #include "ViewProviderDocumentObjectPy.h"
44
45
46 using namespace Gui;
47
48 // returns a string which represent the object e.g. when printed in python
49 std::string DocumentPy::representation() const
50 {
51 std::stringstream str;
52 str << "<GUI Document object for " << getDocumentPtr()->getDocument()->getName() << ">";
53
54 return str.str();
55 }
56
57
58 PyObject* DocumentPy::show(PyObject* args)
59 {
60 char* psFeatStr;
61 if (!PyArg_ParseTuple(args, "s;Name of the Feature to show have to be given!", &psFeatStr)) {
62 return nullptr;
63 }
64
65 PY_TRY
66 {
67 getDocumentPtr()->setShow(psFeatStr);
68 Py_Return;
69 }
70 PY_CATCH;
71 }
72
73 PyObject* DocumentPy::hide(PyObject* args)
74 {
75 char* psFeatStr;
76 if (!PyArg_ParseTuple(args, "s;Name of the Feature to hide have to be given!", &psFeatStr)) {
77 return nullptr;
78 }
79
80 PY_TRY
81 {
82 getDocumentPtr()->setHide(psFeatStr);
83 Py_Return;
84 }
85 PY_CATCH;
86 }
87
88 PyObject* DocumentPy::setPos(PyObject* args)
89 {
90 char* psFeatStr;
91 Base::Matrix4D mat;
92 PyObject* pcMatObj;
93 if (!PyArg_ParseTuple(
94 args,
95 "sO!;Name of the Feature and the transformation matrix have to be given!",
96 &psFeatStr,
97 &(Base::MatrixPy::Type),
98 &pcMatObj
99 )) {
100 return nullptr;
101 }
102
103 mat = static_cast<Base::MatrixPy*>(pcMatObj)->value();
104
105 PY_TRY
106 {
107 getDocumentPtr()->setPos(psFeatStr, mat);
108 Py_Return;
109 }
110 PY_CATCH;
111 }
112
113 PyObject* DocumentPy::setEdit(PyObject* args)
114 {
115 char* psFeatStr;
116 int mod = 0;
117 char* subname = nullptr;
118 ViewProvider* vp = nullptr;
119 App::DocumentObject* obj = nullptr;
120
121 // by name
122 if (PyArg_ParseTuple(args, "s|is", &psFeatStr, &mod, &subname)) {
123 obj = getDocumentPtr()->getDocument()->getObject(psFeatStr);
124 if (!obj) {
125 PyErr_Format(Base::PyExc_FC_GeneralError, "No such object found in document: '%s'", psFeatStr);
126 return nullptr;
127 }
128 }
129 else {
130 PyErr_Clear();
131 PyObject* pyObj;
132 if (!PyArg_ParseTuple(args, "O|is", &pyObj, &mod, &subname)) {
133 return nullptr;
134 }
135
136 if (PyObject_TypeCheck(pyObj, &App::DocumentObjectPy::Type)) {
137 obj = static_cast<App::DocumentObjectPy*>(pyObj)->getDocumentObjectPtr();
138 }
139 else if (PyObject_TypeCheck(pyObj, &ViewProviderPy::Type)) {
140 vp = static_cast<ViewProviderPy*>(pyObj)->getViewProviderPtr();
141 }
142 else {
143 PyErr_SetString(
144 PyExc_TypeError,
145 "Expect the first argument to be string, DocumentObject or ViewObject"
146 );
147 return nullptr;
148 }
149 }
150
151 if (!vp) {
152 if (!obj || !obj->isAttachedToDocument()
153 || !(vp = Application::Instance->getViewProvider(obj))) {
154 PyErr_SetString(PyExc_ValueError, "Invalid document object");
155 return nullptr;
156 }
157 }
158
159 bool ok = getDocumentPtr()->setEdit(vp, mod, subname);
160
161 return PyBool_FromLong(ok ? 1 : 0);
162 }
163
164 PyObject* DocumentPy::getInEdit(PyObject* args)
165 {
166 if (!PyArg_ParseTuple(args, "")) {
167 return nullptr;
168 }
169
170 ViewProvider* vp = getDocumentPtr()->getInEdit();
171 if (vp) {
172 return vp->getPyObject();
173 }
174
175 Py_Return;
176 }
177
178 PyObject* DocumentPy::resetEdit(PyObject* args)
179 {
180 if (!PyArg_ParseTuple(args, "")) {
181 return nullptr;
182 }
183
184 getDocumentPtr()->resetEdit();
185
186 Py_Return;
187 }
188
189 PyObject* DocumentPy::addAnnotation(PyObject* args)
190 {
191 char *psAnnoName, *psFileName, *psModName = nullptr;
192 if (!PyArg_ParseTuple(
193 args,
194 "ss|s;Name of the Annotation and a file name have to be given!",
195 &psAnnoName,
196 &psFileName,
197 &psModName
198 )) {
199 return nullptr;
200 }
201
202 PY_TRY
203 {
204 auto pcExt = new ViewProviderExtern();
205
206 pcExt->setModeByFile(psModName ? psModName : "Main", psFileName);
207 pcExt->adjustDocumentName(getDocumentPtr()->getDocument()->getName());
208 getDocumentPtr()->setAnnotationViewProvider(psAnnoName, pcExt);
209
210 Py_Return;
211 }
212 PY_CATCH;
213 }
214
215 PyObject* DocumentPy::update(PyObject* args)
216 {
217 if (!PyArg_ParseTuple(args, "")) {
218 return nullptr;
219 }
220
221 PY_TRY
222 {
223 getDocumentPtr()->onUpdate();
224 Py_Return;
225 }
226 PY_CATCH;
227 }
228
229 PyObject* DocumentPy::getObject(PyObject* args)
230 {
231 char* sName;
232 if (!PyArg_ParseTuple(args, "s", &sName)) {
233 return nullptr;
234 }
235
236 PY_TRY
237 {
238 ViewProvider* pcView = getDocumentPtr()->getViewProviderByName(sName);
239 if (pcView) {
240 return pcView->getPyObject();
241 }
242 else {
243 Py_Return;
244 }
245 }
246 PY_CATCH;
247 }
248
249 PyObject* DocumentPy::activeObject(PyObject* args)
250 {
251 if (!PyArg_ParseTuple(args, "")) {
252 return nullptr;
253 }
254
255 PY_TRY
256 {
257 App::DocumentObject* pcFtr = getDocumentPtr()->getDocument()->getActiveObject();
258 if (pcFtr) {
259 ViewProvider* pcView = getDocumentPtr()->getViewProvider(pcFtr);
260 return pcView->getPyObject();
261 }
262 else {
263 Py_Return;
264 }
265 }
266 PY_CATCH;
267 }
268
269 PyObject* DocumentPy::activeView(PyObject* args)
270 {
271 if (!PyArg_ParseTuple(args, "")) {
272 return nullptr;
273 }
274
275 PY_TRY
276 {
277 Gui::MDIView* pcView = getDocumentPtr()->getActiveView();
278 if (pcView) {
279 // already incremented in getPyObject().
280 return pcView->getPyObject();
281 }
282 else {
283 Py_Return;
284 }
285 }
286 PY_CATCH;
287 }
288
289 PyObject* DocumentPy::createView(PyObject* args)
290 {
291 char* sType;
292 if (!PyArg_ParseTuple(args, "s", &sType)) {
293 return nullptr;
294 }
295
296 Base::Type type = Base::Type::fromName(sType);
297 if (type.isBad()) {
298 PyErr_Format(PyExc_TypeError, "'%s' is not a valid type", sType);
299 return nullptr;
300 }
301
302 PY_TRY
303 {
304 Gui::MDIView* pcView = getDocumentPtr()->createView(type);
305 if (pcView) {
306 return pcView->getPyObject();
307 }
308 else {
309 Py_Return;
310 }
311 }
312 PY_CATCH;
313 }
314
315 PyObject* DocumentPy::mdiViewsOfType(PyObject* args) const
316 {
317 char* sType;
318 if (!PyArg_ParseTuple(args, "s", &sType)) {
319 return nullptr;
320 }
321
322 Base::Type type = Base::Type::fromName(sType);
323 if (type.isBad()) {
324 PyErr_Format(PyExc_TypeError, "'%s' is not a valid type", sType);
325 return nullptr;
326 }
327
328 PY_TRY
329 {
330 std::list<Gui::MDIView*> views = getDocumentPtr()->getMDIViewsOfType(type);
331 Py::List list;
332 for (auto it : views) {
333 list.append(Py::asObject(it->getPyObject()));
334 }
335 return Py::new_reference_to(list);
336 }
337 PY_CATCH;
338 }
339
340 PyObject* DocumentPy::save(PyObject* args)
341 {
342 if (!PyArg_ParseTuple(args, "")) {
343 return nullptr;
344 }
345
346 PY_TRY
347 {
348 bool ok = getDocumentPtr()->save();
349 return Py::new_reference_to(Py::Boolean(ok));
350 }
351 PY_CATCH;
352 }
353
354 PyObject* DocumentPy::saveAs(PyObject* args)
355 {
356 if (!PyArg_ParseTuple(args, "")) {
357 return nullptr;
358 }
359
360 PY_TRY
361 {
362 bool ok = getDocumentPtr()->saveAs();
363 return Py::new_reference_to(Py::Boolean(ok));
364 }
365 PY_CATCH;
366 }
367
368 PyObject* DocumentPy::sendMsgToViews(PyObject* args)
369 {
370 char* msg;
371 if (!PyArg_ParseTuple(args, "s", &msg)) {
372 return nullptr;
373 }
374
375 PY_TRY
376 {
377 getDocumentPtr()->sendMsgToViews(msg);
378 Py_Return;
379 }
380 PY_CATCH;
381 }
382
383 PyObject* DocumentPy::mergeProject(PyObject* args)
384 {
385 char* filename;
386 if (!PyArg_ParseTuple(args, "s", &filename)) {
387 return nullptr;
388 }
389
390 PY_TRY
391 {
392 Base::FileInfo fi(filename);
393 Base::ifstream str(fi, std::ios::in | std::ios::binary);
394 App::Document* doc = getDocumentPtr()->getDocument();
395 MergeDocuments md(doc);
396 md.importObjects(str);
397
398 Py_Return;
399 }
400 PY_CATCH;
401 }
402
403 PyObject* DocumentPy::toggleTreeItem(PyObject* args)
404 {
405 PyObject* object;
406 const char* subname = nullptr;
407 int mod = 0;
408 if (!PyArg_ParseTuple(args, "O!|is", &(App::DocumentObjectPy::Type), &object, &mod, &subname)) {
409 return nullptr;
410 }
411
412 App::DocumentObject* Object = static_cast<App::DocumentObjectPy*>(object)->getDocumentObjectPtr();
413 App::DocumentObject* parent = nullptr;
414 if (subname) {
415 App::DocumentObject* sobj = Object->getSubObject(subname);
416 if (!sobj) {
417 PyErr_SetString(PyExc_ValueError, "Subobject not found");
418 return nullptr;
419 }
420
421 parent = Object;
422 Object = sobj;
423 }
424
425 // get the gui document of the Assembly Item
426 // ActiveAppDoc = Item->getDocument();
427 // ActiveGuiDoc = Gui::Application::Instance->getDocument(getDocumentPtr());
428 auto ActiveVp = dynamic_cast<Gui::ViewProviderDocumentObject*>(
429 getDocumentPtr()->getViewProvider(Object)
430 );
431 switch (mod) {
432 case 0:
433 getDocumentPtr()->signalExpandObject(*ActiveVp, TreeItemMode::ToggleItem, parent, subname);
434 break;
435 case 1:
436 getDocumentPtr()->signalExpandObject(*ActiveVp, TreeItemMode::CollapseItem, parent, subname);
437 break;
438 case 2:
439 getDocumentPtr()->signalExpandObject(*ActiveVp, TreeItemMode::ExpandItem, parent, subname);
440 break;
441 case 3:
442 getDocumentPtr()->signalExpandObject(*ActiveVp, TreeItemMode::ExpandPath, parent, subname);
443 break;
444 default:
445 PyErr_SetString(PyExc_ValueError, "Item mode out of range");
446 return nullptr;
447 }
448
449 Py_Return;
450 }
451
452 PyObject* DocumentPy::scrollToTreeItem(PyObject* args)
453 {
454 PyObject* view;
455 if (!PyArg_ParseTuple(args, "O!", &(Gui::ViewProviderDocumentObjectPy::Type), &view)) {
456 return nullptr;
457 }
458
459 Gui::ViewProviderDocumentObject* vp
460 = static_cast<ViewProviderDocumentObjectPy*>(view)->getViewProviderDocumentObjectPtr();
461 getDocumentPtr()->signalScrollToObject(*vp);
462
463 Py_Return;
464 }
465
466 PyObject* DocumentPy::toggleInSceneGraph(PyObject* args)
467 {
468 PyObject* view;
469 if (!PyArg_ParseTuple(args, "O!", &(Gui::ViewProviderPy::Type), &view)) {
470 return nullptr;
471 }
472
473 Gui::ViewProvider* vp = static_cast<ViewProviderPy*>(view)->getViewProviderPtr();
474 getDocumentPtr()->toggleInSceneGraph(vp);
475
476 Py_Return;
477 }
478
479 Py::Object DocumentPy::getActiveObject() const
480 {
481 App::DocumentObject* object = getDocumentPtr()->getDocument()->getActiveObject();
482 if (object) {
483 ViewProvider* viewObj = getDocumentPtr()->getViewProvider(object);
484 return Py::Object(viewObj->getPyObject(), true);
485 }
486 else {
487 return Py::None();
488 }
489 }
490
491 void DocumentPy::setActiveObject(Py::Object /*arg*/)
492 {
493 throw Py::AttributeError("'Document' object attribute 'ActiveObject' is read-only");
494 }
495
496 Py::Object DocumentPy::getActiveView() const
497 {
498 Gui::MDIView* view = getDocumentPtr()->getActiveView();
499 if (view) {
500 // already incremented in getPyObject().
501 return Py::Object(view->getPyObject(), true);
502 }
503 else {
504 return Py::None();
505 }
506 }
507
508 void DocumentPy::setActiveView(Py::Object /*arg*/)
509 {
510 throw Py::AttributeError("'Document' object attribute 'ActiveView' is read-only");
511 }
512
513 Py::Object DocumentPy::getDocument() const
514 {
515 App::Document* doc = getDocumentPtr()->getDocument();
516 if (doc) {
517 // already incremented in getPyObject().
518 return Py::Object(doc->getPyObject(), true);
519 }
520 else {
521 return Py::None();
522 }
523 }
524
525 Py::Object DocumentPy::getEditingTransform() const
526 {
527 return Py::asObject(
528 new Base::MatrixPy(new Base::Matrix4D(getDocumentPtr()->getEditingTransform()))
529 );
530 }
531
532 void DocumentPy::setEditingTransform(Py::Object arg)
533 {
534 if (!PyObject_TypeCheck(arg.ptr(), &Base::MatrixPy::Type)) {
535 throw Py::TypeError("Expecting type of matrix");
536 }
537
538 getDocumentPtr()->setEditingTransform(*static_cast<Base::MatrixPy*>(arg.ptr())->getMatrixPtr());
539 }
540
541 Py::Object DocumentPy::getInEditInfo() const
542 {
543 ViewProviderDocumentObject* vp = nullptr;
544 std::string subname, subelement;
545 int mode = 0;
546 getDocumentPtr()->getInEdit(&vp, &subname, &mode, &subelement);
547 if (!vp || !vp->getObject() || !vp->getObject()->isAttachedToDocument()) {
548 return Py::None();
549 }
550
551 return Py::TupleN(
552 Py::Object(vp->getObject()->getPyObject(), true),
553 Py::String(subname),
554 Py::String(subelement),
555 Py::Long(mode)
556 );
557 }
558
559 void DocumentPy::setInEditInfo(Py::Object arg)
560 {
561 PyObject* pyobj;
562 const char* subname;
563 if (!PyArg_ParseTuple(arg.ptr(), "O!s", &Gui::ViewProviderDocumentObjectPy::Type, &pyobj, &subname)) {
564 throw Py::Exception();
565 }
566
567 getDocumentPtr()->setInEdit(
568 static_cast<ViewProviderDocumentObjectPy*>(pyobj)->getViewProviderDocumentObjectPtr(),
569 subname
570 );
571 }
572
573 Py::Long DocumentPy::getEditMode() const
574 {
575 int mode = -1;
576 getDocumentPtr()->getInEdit(nullptr, nullptr, &mode);
577
578 return Py::Long(mode);
579 }
580
581 Py::Boolean DocumentPy::getTransacting() const
582 {
583 return {getDocumentPtr()->isPerformingTransaction()};
584 }
585
586 Py::Boolean DocumentPy::getModified() const
587 {
588 return {getDocumentPtr()->isModified()};
589 }
590
591 void DocumentPy::setModified(Py::Boolean arg)
592 {
593 getDocumentPtr()->setModified(arg);
594 }
595
596 Py::List DocumentPy::getTreeRootObjects() const
597 {
598 std::vector<App::DocumentObject*> objs = getDocumentPtr()->getTreeRootObjects();
599 Py::List res;
600
601 for (auto obj : objs) {
602 // Note: Here we must force the Py::Object to own this Python object as getPyObject()
603 // increments the counter
604 res.append(Py::Object(obj->getPyObject(), true));
605 }
606
607 return res;
608 }
609
610
611 PyObject* DocumentPy::getCustomAttributes(const char* attr) const
612 {
613 // Note: Here we want to return only a document object if its
614 // name matches 'attr'. However, it is possible to have an object
615 // with the same name as an attribute. If so, we return 0 as other-
616 // wise it wouldn't be possible to address this attribute any more.
617 // The object must then be addressed by the getObject() method directly.
618 if (!this->ob_type->tp_dict) {
619 if (PyType_Ready(this->ob_type) < 0) {
620 return nullptr;
621 }
622 }
623
624 PyObject* item = PyDict_GetItemString(this->ob_type->tp_dict, attr);
625 if (item) {
626 return nullptr;
627 }
628
629 // search for an object with this name
630 ViewProvider* obj = getDocumentPtr()->getViewProviderByName(attr);
631
632 return (obj ? obj->getPyObject() : nullptr);
633 }
634
635 int DocumentPy::setCustomAttributes(const char* attr, PyObject*)
636 {
637 // Note: Here we want to return only a document object if its
638 // name matches 'attr'. However, it is possible to have an object
639 // with the same name as an attribute. If so, we return 0 as other-
640 // wise it wouldn't be possible to address this attribute any more.
641 // The object must then be addressed by the getObject() method directly.
642 if (!this->ob_type->tp_dict) {
643 if (PyType_Ready(this->ob_type) < 0) {
644 return 0;
645 }
646 }
647
648 PyObject* item = PyDict_GetItemString(this->ob_type->tp_dict, attr);
649 if (item) {
650 return 0;
651 }
652
653 ViewProvider* obj = getDocumentPtr()->getViewProviderByName(attr);
654 if (obj) {
655 std::stringstream str;
656 str << "'Document' object attribute '" << attr << "' must not be set this way" << std::ends;
657 throw Py::AttributeError(str.str());
658 }
659
660 return 0;
661 }