Debian Codegraph @

src/App/DocumentPyImp.cpp

raw
1 /***************************************************************************
2 * Copyright (c) 2007 Jürgen Riegel <[email protected]> *
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 <Base/FileInfo.h>
24 #include <Base/Interpreter.h>
25 #include <Base/Stream.h>
26
27 #include "Document.h"
28 #include "DocumentObject.h"
29 #include "DocumentObjectPy.h"
30 #include "MergeDocuments.h"
31
32 // inclusion of the generated files (generated By DocumentPy.xml)
33 #include "DocumentPy.h"
34 #include "DocumentPy.cpp"
35 #include <boost/regex.hpp>
36 #include <Base/PyWrapParseTupleAndKeywords.h>
37
38 using namespace App;
39
40
41 PyObject* DocumentPy::addProperty(PyObject* args, PyObject* kwd)
42 {
43 char* sType {nullptr};
44 char* sName {nullptr};
45 char* sGroup {nullptr};
46 char* sDoc {nullptr};
47 short attr = 0;
48 std::string sDocStr;
49 PyObject *ro = Py_False, *hd = Py_False, *lk = Py_False;
50 PyObject* enumVals = nullptr;
51 static const std::array<const char*, 10> kwlist {"type",
52 "name",
53 "group",
54 "doc",
55 "attr",
56 "read_only",
57 "hidden",
58 "locked",
59 "enum_vals",
60 nullptr};
61 if (!Base::Wrapped_ParseTupleAndKeywords(args,
62 kwd,
63 "ss|sethO!O!O!O",
64 kwlist,
65 &sType,
66 &sName,
67 &sGroup,
68 "utf-8",
69 &sDoc,
70 &attr,
71 &PyBool_Type,
72 &ro,
73 &PyBool_Type,
74 &hd,
75 &PyBool_Type,
76 &lk,
77 &enumVals)) {
78 return nullptr;
79 }
80
81 if (sDoc) {
82 sDocStr = sDoc;
83 PyMem_Free(sDoc);
84 }
85
86 Property* prop = getDocumentPtr()->addDynamicProperty(sType,
87 sName,
88 sGroup,
89 sDocStr.c_str(),
90 attr,
91 Base::asBoolean(ro),
92 Base::asBoolean(hd));
93 prop->setStatus(Property::LockDynamic, Base::asBoolean(lk));
94
95 // enum support
96 auto* propEnum = dynamic_cast<App::PropertyEnumeration*>(prop);
97 if (propEnum && enumVals) {
98 propEnum->setPyObject(enumVals);
99 }
100
101 return Py::new_reference_to(this);
102 }
103
104 PyObject* DocumentPy::removeProperty(PyObject* args)
105 {
106 char* sName;
107 if (!PyArg_ParseTuple(args, "s", &sName)) {
108 return nullptr;
109 }
110
111 bool ok = getDocumentPtr()->removeDynamicProperty(sName);
112 return Py_BuildValue("O", (ok ? Py_True : Py_False));
113 }
114
115 // returns a string which represent the object e.g. when printed in python
116 std::string DocumentPy::representation() const
117 {
118 std::stringstream str;
119 str << "<Document '" << getDocumentPtr()->getName() << "' (" << getDocumentPtr()->Label.getValue() << ") >";
120
121 return str.str();
122 }
123
124 PyObject* DocumentPy::save(PyObject* args)
125 {
126 if (!PyArg_ParseTuple(args, "")) {
127 return nullptr;
128 }
129
130 PY_TRY
131 {
132 if (!getDocumentPtr()->save()) {
133 PyErr_SetString(PyExc_ValueError, "Object attribute 'FileName' is not set");
134 return nullptr;
135 }
136 }
137 PY_CATCH;
138
139 const char* filename = getDocumentPtr()->FileName.getValue();
140 Base::FileInfo fi(filename);
141 if (!fi.isReadable()) {
142 PyErr_Format(PyExc_IOError, "No such file or directory: '%s'", filename);
143 return nullptr;
144 }
145
146 Py_Return;
147 }
148
149 PyObject* DocumentPy::saveAs(PyObject* args)
150 {
151 char* fn;
152 if (!PyArg_ParseTuple(args, "et", "utf-8", &fn)) {
153 return nullptr;
154 }
155
156 std::string utf8Name = fn;
157 PyMem_Free(fn);
158
159 PY_TRY
160 {
161 getDocumentPtr()->saveAs(utf8Name.c_str());
162 Py_Return;
163 }
164 PY_CATCH
165 }
166
167 PyObject* DocumentPy::saveCopy(PyObject* args)
168 {
169 char* fn;
170 if (!PyArg_ParseTuple(args, "s", &fn)) {
171 return nullptr;
172 }
173
174 PY_TRY
175 {
176 getDocumentPtr()->saveCopy(fn);
177 Py_Return;
178 }
179 PY_CATCH
180 }
181
182 PyObject* DocumentPy::load(PyObject* args)
183 {
184 char* filename = nullptr;
185 if (!PyArg_ParseTuple(args, "s", &filename)) {
186 return nullptr;
187 }
188 if (!filename || *filename == '\0') {
189 PyErr_Format(PyExc_ValueError, "Path is empty");
190 return nullptr;
191 }
192
193 getDocumentPtr()->FileName.setValue(filename);
194 Base::FileInfo fi(filename);
195 if (!fi.isReadable()) {
196 PyErr_Format(PyExc_IOError, "No such file or directory: '%s'", filename);
197 return nullptr;
198 }
199 try {
200 getDocumentPtr()->restore();
201 }
202 catch (...) {
203 PyErr_Format(PyExc_IOError, "Reading from file '%s' failed", filename);
204 return nullptr;
205 }
206 Py_Return;
207 }
208
209 PyObject* DocumentPy::restore(PyObject* args)
210 {
211 if (!PyArg_ParseTuple(args, "")) {
212 return nullptr;
213 }
214 const char* filename = getDocumentPtr()->FileName.getValue();
215 if (!filename || *filename == '\0') {
216 PyErr_Format(PyExc_ValueError, "Object attribute 'FileName' is not set");
217 return nullptr;
218 }
219 Base::FileInfo fi(filename);
220 if (!fi.isReadable()) {
221 PyErr_Format(PyExc_IOError, "No such file or directory: '%s'", filename);
222 return nullptr;
223 }
224 try {
225 getDocumentPtr()->restore();
226 }
227 catch (...) {
228 PyErr_Format(PyExc_IOError, "Reading from file '%s' failed", filename);
229 return nullptr;
230 }
231 Py_Return;
232 }
233
234 PyObject* DocumentPy::isSaved(PyObject* args)
235 {
236 if (!PyArg_ParseTuple(args, "")) {
237 return nullptr;
238 }
239 bool ok = getDocumentPtr()->isSaved();
240 return Py::new_reference_to(Py::Boolean(ok));
241 }
242
243 PyObject* DocumentPy::getProgramVersion(PyObject* args)
244 {
245 if (!PyArg_ParseTuple(args, "")) {
246 return nullptr;
247 }
248 const char* version = getDocumentPtr()->getProgramVersion();
249 return Py::new_reference_to(Py::String(version));
250 }
251
252 PyObject* DocumentPy::getFileName(PyObject* args)
253 {
254 if (!PyArg_ParseTuple(args, "")) {
255 return nullptr;
256 }
257 const char* fn = getDocumentPtr()->getFileName();
258 return Py::new_reference_to(Py::String(fn));
259 }
260
261 PyObject* DocumentPy::getUniqueObjectName(PyObject* args)
262 {
263 char* sName;
264 if (!PyArg_ParseTuple(args, "s", &sName)) {
265 return nullptr;
266 }
267 PY_TRY
268 {
269 auto newName = getDocumentPtr()->getUniqueObjectName(sName);
270 return Py::new_reference_to(Py::String(newName));
271 }
272 PY_CATCH;
273 }
274
275 PyObject* DocumentPy::mergeProject(PyObject* args)
276 {
277 char* filename;
278 if (!PyArg_ParseTuple(args, "s", &filename)) {
279 return nullptr;
280 }
281
282 PY_TRY
283 {
284 Base::FileInfo fi(filename);
285 Base::ifstream str(fi, std::ios::in | std::ios::binary);
286 App::Document* doc = getDocumentPtr();
287 MergeDocuments md(doc);
288 md.importObjects(str);
289 Py_Return;
290 }
291 PY_CATCH;
292 }
293
294 PyObject* DocumentPy::exportGraphviz(PyObject* args)
295 {
296 char* fn = nullptr;
297 if (!PyArg_ParseTuple(args, "|s", &fn)) {
298 return nullptr;
299 }
300 if (fn) {
301 Base::FileInfo fi(fn);
302 Base::ofstream str(fi);
303 getDocumentPtr()->exportGraphviz(str);
304 str.close();
305 Py_Return;
306 }
307 else {
308 std::stringstream str;
309 getDocumentPtr()->exportGraphviz(str);
310 return PyUnicode_FromString(str.str().c_str());
311 }
312 }
313
314 PyObject* DocumentPy::addObject(PyObject* args, PyObject* kwd)
315 {
316 char *sType, *sName = nullptr, *sViewType = nullptr;
317 PyObject* obj = nullptr;
318 PyObject* view = nullptr;
319 PyObject* attach = Py_False;
320 static const std::array<const char*, 7>
321 kwlist {"type", "name", "objProxy", "viewProxy", "attach", "viewType", nullptr};
322 if (!Base::Wrapped_ParseTupleAndKeywords(args,
323 kwd,
324 "s|sOOO!s",
325 kwlist,
326 &sType,
327 &sName,
328 &obj,
329 &view,
330 &PyBool_Type,
331 &attach,
332 &sViewType)) {
333 return nullptr;
334 }
335
336 DocumentObject* pcFtr = nullptr;
337
338 if (!obj || !Base::asBoolean(attach)) {
339 pcFtr = getDocumentPtr()->addObject(sType, sName, true, sViewType);
340 }
341 else {
342 Base::Type type =
343 Base::Type::getTypeIfDerivedFrom(sType, DocumentObject::getClassTypeId(), true);
344 if (type.isBad()) {
345 std::stringstream str;
346 str << "'" << sType << "' is not a document object type";
347 throw Base::TypeError(str.str());
348 }
349 pcFtr = static_cast<DocumentObject*>(type.createInstance());
350 }
351 // the type instance could be a null pointer
352 if (!pcFtr) {
353 std::stringstream str;
354 str << "No document object found of type '" << sType << "'" << std::ends;
355 throw Py::TypeError(str.str());
356 }
357 // Allows one to hide the handling with Proxy in client python code
358 if (obj) {
359 try {
360 // the python binding class to the document object
361 Py::Object pyftr = Py::asObject(pcFtr->getPyObject());
362 // 'pyobj' is the python class with the implementation for DocumentObject
363 Py::Object pyobj(obj);
364 if (pyobj.hasAttr("__object__")) {
365 pyobj.setAttr("__object__", pyftr);
366 }
367 pyftr.setAttr("Proxy", pyobj);
368
369 if (Base::asBoolean(attach)) {
370 getDocumentPtr()->addObject(pcFtr, sName);
371
372 try {
373 Py::Callable method(pyobj.getAttr("attach"));
374 if (!method.isNone()) {
375 Py::TupleN arg(pyftr);
376 method.apply(arg);
377 }
378 }
379 catch (Py::Exception&) {
380 Base::PyException e;
381 e.reportException();
382 }
383 }
384
385 // if a document class is set we also need a view provider defined which must be
386 // something different to None
387 Py::Object pyvp;
388 if (view) {
389 pyvp = Py::Object(view);
390 }
391 if (pyvp.isNone()) {
392 pyvp = Py::Long(1);
393 }
394 // 'pyvp' is the python class with the implementation for ViewProvider
395 if (pyvp.hasAttr("__vobject__")) {
396 pyvp.setAttr("__vobject__", pyftr.getAttr("ViewObject"));
397 }
398
399 Py::Object pyprx(pyftr.getAttr("ViewObject"));
400 pyprx.setAttr("Proxy", pyvp);
401 return Py::new_reference_to(pyftr);
402 }
403 catch (Py::Exception& e) {
404 e.clear();
405 }
406 }
407 return pcFtr->getPyObject();
408 }
409
410 PyObject* DocumentPy::removeObject(PyObject* args)
411 {
412 char* sName;
413 if (!PyArg_ParseTuple(args, "s", &sName)) {
414 return nullptr;
415 }
416
417
418 DocumentObject* pcFtr = getDocumentPtr()->getObject(sName);
419 if (pcFtr) {
420 getDocumentPtr()->removeObject(sName);
421 Py_Return;
422 }
423 else {
424 std::stringstream str;
425 str << "No document object found with name '" << sName << "'" << std::ends;
426 throw Py::ValueError(str.str());
427 }
428 }
429
430 PyObject* DocumentPy::copyObject(PyObject* args, PyObject* kwd)
431 {
432 PyObject* obj;
433 PyObject* rec = Py_False;
434 PyObject* retAll = Py_False;
435 static constexpr std::array<const char*, 4> kwlist {"object", "recursive", "return_all", nullptr};
436 if (!Base::Wrapped_ParseTupleAndKeywords(args,
437 kwd,
438 "O|O!O!",
439 kwlist,
440 &obj,
441 &PyBool_Type,
442 &rec,
443 &PyBool_Type,
444 &retAll)) {
445 return nullptr;
446 }
447
448 std::vector<App::DocumentObject*> objs;
449 bool single = false;
450 if (PySequence_Check(obj)) {
451 Py::Sequence seq(obj);
452 for (Py_ssize_t i = 0; i < seq.size(); ++i) {
453 if (!PyObject_TypeCheck(seq[i].ptr(), &DocumentObjectPy::Type)) {
454 PyErr_SetString(PyExc_TypeError,
455 "Expect element in sequence to be of type document object");
456 return nullptr;
457 }
458 objs.push_back(static_cast<DocumentObjectPy*>(seq[i].ptr())->getDocumentObjectPtr());
459 }
460 }
461 else if (!PyObject_TypeCheck(obj, &DocumentObjectPy::Type)) {
462 PyErr_SetString(
463 PyExc_TypeError,
464 "Expect first argument to be either a document object or sequence of document objects");
465 return nullptr;
466 }
467 else {
468 objs.push_back(static_cast<DocumentObjectPy*>(obj)->getDocumentObjectPtr());
469 single = true;
470 }
471
472 PY_TRY
473 {
474 auto ret =
475 getDocumentPtr()->copyObject(objs, Base::asBoolean(rec), Base::asBoolean(retAll));
476 if (ret.size() == 1 && single) {
477 return ret[0]->getPyObject();
478 }
479
480 Py::Tuple tuple(ret.size());
481 for (size_t i = 0; i < ret.size(); ++i) {
482 tuple.setItem(i, Py::Object(ret[i]->getPyObject(), true));
483 }
484 return Py::new_reference_to(tuple);
485 }
486 PY_CATCH
487 }
488
489 PyObject* DocumentPy::importLinks(PyObject* args)
490 {
491 PyObject* obj = Py_None;
492 if (!PyArg_ParseTuple(args, "|O", &obj)) {
493 return nullptr;
494 }
495
496 std::vector<App::DocumentObject*> objs;
497 if (PySequence_Check(obj)) {
498 Py::Sequence seq(obj);
499 for (Py_ssize_t i = 0; i < seq.size(); ++i) {
500 if (!PyObject_TypeCheck(seq[i].ptr(), &DocumentObjectPy::Type)) {
501 PyErr_SetString(PyExc_TypeError,
502 "Expect element in sequence to be of type document object");
503 return nullptr;
504 }
505 objs.push_back(static_cast<DocumentObjectPy*>(seq[i].ptr())->getDocumentObjectPtr());
506 }
507 }
508 else {
509 Base::PyTypeCheck(&obj,
510 &DocumentObjectPy::Type,
511 "Expect first argument to be either a document object, sequence of "
512 "document objects or None");
513 if (obj) {
514 objs.push_back(static_cast<DocumentObjectPy*>(obj)->getDocumentObjectPtr());
515 }
516 }
517
518 if (objs.empty()) {
519 objs = getDocumentPtr()->getObjects();
520 }
521
522 PY_TRY
523 {
524 auto ret = getDocumentPtr()->importLinks(objs);
525
526 Py::Tuple tuple(ret.size());
527 for (size_t i = 0; i < ret.size(); ++i) {
528 tuple.setItem(i, Py::Object(ret[i]->getPyObject(), true));
529 }
530 return Py::new_reference_to(tuple);
531 }
532 PY_CATCH
533 }
534
535 PyObject* DocumentPy::moveObject(PyObject* args)
536 {
537 PyObject *obj, *rec = Py_False;
538 if (!PyArg_ParseTuple(args, "O!|O!", &(DocumentObjectPy::Type), &obj, &PyBool_Type, &rec)) {
539 return nullptr;
540 }
541
542 DocumentObjectPy* docObj = static_cast<DocumentObjectPy*>(obj);
543 DocumentObject* move =
544 getDocumentPtr()->moveObject(docObj->getDocumentObjectPtr(), Base::asBoolean(rec));
545 if (move) {
546 return move->getPyObject();
547 }
548 else {
549 std::string str("Failed to move the object");
550 throw Py::ValueError(str);
551 }
552 }
553
554 PyObject* DocumentPy::openTransaction(PyObject* args)
555 {
556 PyObject* value = nullptr;
557 if (!PyArg_ParseTuple(args, "|O", &value)) {
558 return nullptr;
559 }
560 std::string cmd;
561
562
563 if (!value) {
564 cmd = "<empty>";
565 }
566 else if (PyUnicode_Check(value)) {
567 cmd = PyUnicode_AsUTF8(value);
568 }
569 else {
570 PyErr_SetString(PyExc_TypeError, "string or unicode expected");
571 return nullptr;
572 }
573
574 getDocumentPtr()->openTransaction(cmd.c_str());
575 Py_Return;
576 }
577
578 PyObject* DocumentPy::abortTransaction(PyObject* args)
579 {
580 if (!PyArg_ParseTuple(args, "")) {
581 return nullptr;
582 }
583 getDocumentPtr()->abortTransaction();
584 Py_Return;
585 }
586
587 PyObject* DocumentPy::commitTransaction(PyObject* args)
588 {
589 if (!PyArg_ParseTuple(args, "")) {
590 return nullptr;
591 }
592 getDocumentPtr()->commitTransaction();
593 Py_Return;
594 }
595
596 Py::Boolean DocumentPy::getHasPendingTransaction() const
597 {
598 return {getDocumentPtr()->hasPendingTransaction()};
599 }
600
601 PyObject* DocumentPy::undo(PyObject* args)
602 {
603 if (!PyArg_ParseTuple(args, "")) {
604 return nullptr;
605 }
606 if (getDocumentPtr()->getAvailableUndos()) {
607 getDocumentPtr()->undo();
608 }
609 Py_Return;
610 }
611
612 PyObject* DocumentPy::redo(PyObject* args)
613 {
614 if (!PyArg_ParseTuple(args, "")) {
615 return nullptr;
616 }
617 if (getDocumentPtr()->getAvailableRedos()) {
618 getDocumentPtr()->redo();
619 }
620 Py_Return;
621 }
622
623 PyObject* DocumentPy::clearUndos(PyObject* args)
624 {
625 if (!PyArg_ParseTuple(args, "")) {
626 return nullptr;
627 }
628 getDocumentPtr()->clearUndos();
629 Py_Return;
630 }
631
632 PyObject* DocumentPy::clearDocument(PyObject* args)
633 {
634 if (!PyArg_ParseTuple(args, "")) {
635 return nullptr;
636 }
637 getDocumentPtr()->clearDocument();
638 Py_Return;
639 }
640
641 PyObject* DocumentPy::setClosable(PyObject* args)
642 {
643 PyObject* close;
644 if (!PyArg_ParseTuple(args, "O!", &PyBool_Type, &close)) {
645 return nullptr;
646 }
647 getDocumentPtr()->setClosable(Base::asBoolean(close));
648 Py_Return;
649 }
650
651 PyObject* DocumentPy::isClosable(PyObject* args)
652 {
653 if (!PyArg_ParseTuple(args, "")) {
654 return nullptr;
655 }
656 bool ok = getDocumentPtr()->isClosable();
657 return Py::new_reference_to(Py::Boolean(ok));
658 }
659
660 PyObject *DocumentPy::setAutoCreated(PyObject *args)
661 {
662 PyObject *autoCreated;
663 if (!PyArg_ParseTuple(args, "O!", &PyBool_Type, &autoCreated)) {
664 return nullptr;
665 }
666 bool value = (autoCreated == Py_True);
667 getDocumentPtr()->setAutoCreated(value);
668
669 Py_RETURN_NONE;
670 }
671
672 PyObject *DocumentPy::isAutoCreated(PyObject *args)
673 {
674 if (!PyArg_ParseTuple(args, "")) {
675 return nullptr;
676 }
677 bool ok = getDocumentPtr()->isAutoCreated();
678 return Py::new_reference_to(Py::Boolean(ok));
679 }
680
681 PyObject* DocumentPy::recompute(PyObject* args)
682 {
683 PyObject* pyobjs = Py_None;
684 PyObject* force = Py_False;
685 PyObject* checkCycle = Py_False;
686 if (!PyArg_ParseTuple(args,
687 "|OO!O!",
688 &pyobjs,
689 &PyBool_Type,
690 &force,
691 &PyBool_Type,
692 &checkCycle)) {
693 return nullptr;
694 }
695
696 PY_TRY
697 {
698 std::vector<App::DocumentObject*> objs;
699 if (pyobjs != Py_None) {
700 if (!PySequence_Check(pyobjs)) {
701 PyErr_SetString(PyExc_TypeError, "expect input of sequence of document objects");
702 return nullptr;
703 }
704
705 Py::Sequence seq(pyobjs);
706 for (Py_ssize_t i = 0; i < seq.size(); ++i) {
707 if (!PyObject_TypeCheck(seq[i].ptr(), &DocumentObjectPy::Type)) {
708 PyErr_SetString(PyExc_TypeError,
709 "Expect element in sequence to be of type document object");
710 return nullptr;
711 }
712 objs.push_back(
713 static_cast<DocumentObjectPy*>(seq[i].ptr())->getDocumentObjectPtr());
714 }
715 }
716
717 int options = 0;
718 if (Base::asBoolean(checkCycle)) {
719 options = Document::DepNoCycle;
720 }
721
722 int objectCount =
723 getDocumentPtr()->recompute(objs, Base::asBoolean(force), nullptr, options);
724
725 // Document::recompute() hides possibly raised Python exceptions by its features
726 // So, check if an error is set and return null if yes
727 if (PyErr_Occurred()) {
728 return nullptr;
729 }
730
731 return Py::new_reference_to(Py::Long(objectCount));
732 }
733 PY_CATCH;
734 }
735
736 PyObject* DocumentPy::mustExecute(PyObject* args)
737 {
738 if (!PyArg_ParseTuple(args, "")) {
739 return nullptr;
740 }
741 bool ok = getDocumentPtr()->mustExecute();
742 return Py::new_reference_to(Py::Boolean(ok));
743 }
744
745 PyObject* DocumentPy::isTouched(PyObject* args)
746 {
747 if (!PyArg_ParseTuple(args, "")) {
748 return nullptr;
749 }
750 bool ok = getDocumentPtr()->isTouched();
751 return Py::new_reference_to(Py::Boolean(ok));
752 }
753
754 PyObject* DocumentPy::purgeTouched(PyObject* args)
755 {
756 if (!PyArg_ParseTuple(args, "")) {
757 return nullptr;
758 }
759 getDocumentPtr()->purgeTouched();
760 Py_Return;
761 }
762
763 PyObject* DocumentPy::getObject(PyObject* args)
764 {
765 DocumentObject* obj = nullptr;
766
767 do {
768 char* name = nullptr;
769 if (PyArg_ParseTuple(args, "s", &name)) {
770 obj = getDocumentPtr()->getObject(name);
771 break;
772 }
773
774 PyErr_Clear();
775 long id = -1;
776 if (PyArg_ParseTuple(args, "l", &id)) {
777 obj = getDocumentPtr()->getObjectByID(id);
778 break;
779 }
780
781 PyErr_SetString(PyExc_TypeError, "a string or integer is required");
782 return nullptr;
783 } while (false);
784
785 if (obj) {
786 return obj->getPyObject();
787 }
788
789 Py_Return;
790 }
791
792 PyObject* DocumentPy::getObjectsByLabel(PyObject* args)
793 {
794 char* sName;
795 if (!PyArg_ParseTuple(args, "s", &sName)) {
796 return nullptr;
797 }
798
799 Py::List list;
800 std::string name = sName;
801 std::vector<DocumentObject*> objs = getDocumentPtr()->getObjects();
802 for (auto obj : objs) {
803 if (name == obj->Label.getValue()) {
804 list.append(Py::asObject(obj->getPyObject()));
805 }
806 }
807
808 return Py::new_reference_to(list);
809 }
810
811 PyObject* DocumentPy::findObjects(PyObject* args, PyObject* kwds)
812 {
813 const char *sType = "App::DocumentObject", *sName = nullptr, *sLabel = nullptr;
814 static const std::array<const char*, 4> kwlist {"Type", "Name", "Label", nullptr};
815 if (!Base::Wrapped_ParseTupleAndKeywords(args, kwds, "|sss", kwlist, &sType, &sName, &sLabel)) {
816 return nullptr;
817 }
818
819 Base::Type type =
820 Base::Type::getTypeIfDerivedFrom(sType, App::DocumentObject::getClassTypeId(), true);
821 if (type.isBad()) {
822 std::stringstream str;
823 str << "'" << sType << "' is not a document object type";
824 throw Base::TypeError(str.str());
825 }
826
827 std::vector<DocumentObject*> res;
828
829 try {
830 res = getDocumentPtr()->findObjects(type, sName, sLabel);
831 }
832 catch (const boost::regex_error& e) {
833 PyErr_SetString(PyExc_RuntimeError, e.what());
834 return nullptr;
835 }
836
837 Py_ssize_t index = 0;
838 PyObject* list = PyList_New((Py_ssize_t)res.size());
839 for (std::vector<DocumentObject*>::const_iterator It = res.begin(); It != res.end();
840 ++It, index++) {
841 PyList_SetItem(list, index, (*It)->getPyObject());
842 }
843 return list;
844 }
845
846 Py::Object DocumentPy::getActiveObject() const
847 {
848 DocumentObject* pcFtr = getDocumentPtr()->getActiveObject();
849 if (pcFtr) {
850 return Py::Object(pcFtr->getPyObject(), true);
851 }
852 return Py::None();
853 }
854
855 PyObject* DocumentPy::supportedTypes(PyObject* args)
856 {
857 if (!PyArg_ParseTuple(args, "")) {
858 return nullptr;
859 }
860
861 std::vector<Base::Type> ary;
862 Base::Type::getAllDerivedFrom(App::DocumentObject::getClassTypeId(), ary);
863 Py::List res;
864 for (const auto& it : ary) {
865 res.append(Py::String(it.getName()));
866 }
867 return Py::new_reference_to(res);
868 }
869
870 Py::List DocumentPy::getObjects() const
871 {
872 std::vector<DocumentObject*> objs = getDocumentPtr()->getObjects();
873 Py::List res;
874
875 for (auto obj : objs) {
876 // Note: Here we must force the Py::Object to own this Python object as getPyObject()
877 // increments the counter
878 res.append(Py::Object(obj->getPyObject(), true));
879 }
880
881 return res;
882 }
883
884 Py::List DocumentPy::getTopologicalSortedObjects() const
885 {
886 std::vector<DocumentObject*> objs = getDocumentPtr()->topologicalSort();
887 Py::List res;
888
889 for (auto obj : objs) {
890 // Note: Here we must force the Py::Object to own this Python object as getPyObject()
891 // increments the counter
892 res.append(Py::Object(obj->getPyObject(), true));
893 }
894
895 return res;
896 }
897
898 Py::List DocumentPy::getRootObjects() const
899 {
900 std::vector<DocumentObject*> objs = getDocumentPtr()->getRootObjects();
901 Py::List res;
902
903 for (auto obj : objs) {
904 // Note: Here we must force the Py::Object to own this Python object as getPyObject()
905 // increments the counter
906 res.append(Py::Object(obj->getPyObject(), true));
907 }
908
909 return res;
910 }
911
912 Py::List DocumentPy::getRootObjectsIgnoreLinks() const
913 {
914 std::vector<App::DocumentObject*> objs = getDocumentPtr()->getRootObjectsIgnoreLinks();
915 Py::List res;
916
917 for (auto obj : objs) {
918 // Note: Here we must force the Py::Object to own this Python object as getPyObject()
919 // increments the counter
920 res.append(Py::Object(obj->getPyObject(), true));
921 }
922
923 return res;
924 }
925
926 Py::Long DocumentPy::getUndoMode() const
927 {
928 return Py::Long(getDocumentPtr()->getUndoMode());
929 }
930
931 void DocumentPy::setUndoMode(Py::Long arg)
932 {
933 getDocumentPtr()->setUndoMode(arg);
934 }
935
936
937 Py::Long DocumentPy::getUndoRedoMemSize() const
938 {
939 return Py::Long((long)getDocumentPtr()->getUndoMemSize());
940 }
941
942 Py::Long DocumentPy::getUndoCount() const
943 {
944 return Py::Long((long)getDocumentPtr()->getAvailableUndos());
945 }
946
947 Py::Long DocumentPy::getRedoCount() const
948 {
949 return Py::Long((long)getDocumentPtr()->getAvailableRedos());
950 }
951
952 Py::List DocumentPy::getUndoNames() const
953 {
954 std::vector<std::string> vList = getDocumentPtr()->getAvailableUndoNames();
955 Py::List res;
956
957 for (const auto& It : vList) {
958 res.append(Py::String(It));
959 }
960
961 return res;
962 }
963
964 Py::List DocumentPy::getRedoNames() const
965 {
966 std::vector<std::string> vList = getDocumentPtr()->getAvailableRedoNames();
967 Py::List res;
968
969 for (const auto& It : vList) {
970 res.append(Py::String(It));
971 }
972
973 return res;
974 }
975
976 Py::String DocumentPy::getDependencyGraph() const
977 {
978 std::stringstream out;
979 getDocumentPtr()->exportGraphviz(out);
980 return {out.str()};
981 }
982
983 Py::String DocumentPy::getName() const
984 {
985 return {getDocumentPtr()->getName()};
986 }
987
988 Py::Boolean DocumentPy::getRecomputesFrozen() const
989 {
990 return {getDocumentPtr()->testStatus(Document::Status::SkipRecompute)};
991 }
992
993 void DocumentPy::setRecomputesFrozen(Py::Boolean arg)
994 {
995 getDocumentPtr()->setStatus(Document::Status::SkipRecompute, arg.isTrue());
996 }
997
998 PyObject* DocumentPy::getTempFileName(PyObject* args)
999 {
1000 PyObject* value;
1001 if (!PyArg_ParseTuple(args, "O", &value)) {
1002 return nullptr;
1003 }
1004
1005 std::string string;
1006 if (PyUnicode_Check(value)) {
1007 string = PyUnicode_AsUTF8(value);
1008 }
1009 else {
1010 std::string error = std::string("type must be a string!");
1011 error += value->ob_type->tp_name;
1012 throw Py::TypeError(error);
1013 }
1014
1015 // search for a temp file name in the document transient directory
1016 Base::FileInfo fileName(
1017 Base::FileInfo::getTempFileName(string.c_str(), getDocumentPtr()->TransientDir.getValue()));
1018 // delete the created file, we need only the name...
1019 fileName.deleteFile();
1020
1021 PyObject* p =
1022 PyUnicode_DecodeUTF8(fileName.filePath().c_str(), fileName.filePath().size(), nullptr);
1023 if (!p) {
1024 throw Base::UnicodeError("UTF8 conversion failure at PropertyString::getPyObject()");
1025 }
1026 return p;
1027 }
1028
1029 PyObject* DocumentPy::getCustomAttributes(const char* attr) const
1030 {
1031 // Note: Here we want to return only a document object if its
1032 // name matches 'attr'. However, it is possible to have an object
1033 // with the same name as an attribute. If so, we return 0 as other-
1034 // wise it wouldn't be possible to address this attribute any more.
1035 // The object must then be addressed by the getObject() method directly.
1036 App::Property* prop = getPropertyContainerPtr()->getPropertyByName(attr);
1037 if (prop) {
1038 return nullptr;
1039 }
1040 if (!this->ob_type->tp_dict) {
1041 if (PyType_Ready(this->ob_type) < 0) {
1042 return nullptr;
1043 }
1044 }
1045 PyObject* item = PyDict_GetItemString(this->ob_type->tp_dict, attr);
1046 if (item) {
1047 return nullptr;
1048 }
1049 // search for an object with this name
1050 DocumentObject* obj = getDocumentPtr()->getObject(attr);
1051 return (obj ? obj->getPyObject() : nullptr);
1052 }
1053
1054 int DocumentPy::setCustomAttributes(const char* attr, PyObject*)
1055 {
1056 // Note: Here we want to return only a document object if its
1057 // name matches 'attr'. However, it is possible to have an object
1058 // with the same name as an attribute. If so, we return 0 as other-
1059 // wise it wouldn't be possible to address this attribute any more.
1060 // The object must then be addressed by the getObject() method directly.
1061 App::Property* prop = getPropertyContainerPtr()->getPropertyByName(attr);
1062 if (prop) {
1063 return 0;
1064 }
1065 if (!this->ob_type->tp_dict) {
1066 if (PyType_Ready(this->ob_type) < 0) {
1067 return 0;
1068 }
1069 }
1070 PyObject* item = PyDict_GetItemString(this->ob_type->tp_dict, attr);
1071 if (item) {
1072 return 0;
1073 }
1074 DocumentObject* obj = getDocumentPtr()->getObject(attr);
1075 if (obj) {
1076 std::stringstream str;
1077 str << "'Document' object attribute '" << attr << "' must not be set this way" << std::ends;
1078 PyErr_SetString(PyExc_RuntimeError, str.str().c_str());
1079 return -1;
1080 }
1081
1082 return 0;
1083 }
1084
1085 PyObject* DocumentPy::getLinksTo(PyObject* args)
1086 {
1087 PyObject* pyobj = Py_None;
1088 int options = 0;
1089 short count = 0;
1090 if (!PyArg_ParseTuple(args, "|Oih", &pyobj, &options, &count)) {
1091 return nullptr;
1092 }
1093
1094 PY_TRY
1095 {
1096 Base::PyTypeCheck(&pyobj,
1097 &DocumentObjectPy::Type,
1098 "Expect the first argument of type document object");
1099 DocumentObject* obj = nullptr;
1100 if (pyobj) {
1101 obj = static_cast<DocumentObjectPy*>(pyobj)->getDocumentObjectPtr();
1102 }
1103
1104 std::set<DocumentObject*> links;
1105 getDocumentPtr()->getLinksTo(links, obj, options, count);
1106 Py::Tuple ret(links.size());
1107 int i = 0;
1108 for (auto o : links) {
1109 ret.setItem(i++, Py::Object(o->getPyObject(), true));
1110 }
1111
1112 return Py::new_reference_to(ret);
1113 }
1114 PY_CATCH
1115 }
1116
1117 Py::List DocumentPy::getInList() const
1118 {
1119 Py::List ret;
1120 auto lists = PropertyXLink::getDocumentInList(getDocumentPtr());
1121 if (lists.size() == 1) {
1122 for (auto doc : lists.begin()->second) {
1123 ret.append(Py::Object(doc->getPyObject(), true));
1124 }
1125 }
1126 return ret;
1127 }
1128
1129 Py::List DocumentPy::getOutList() const
1130 {
1131 Py::List ret;
1132 auto lists = PropertyXLink::getDocumentOutList(getDocumentPtr());
1133 if (lists.size() == 1) {
1134 for (auto doc : lists.begin()->second) {
1135 ret.append(Py::Object(doc->getPyObject(), true));
1136 }
1137 }
1138 return ret;
1139 }
1140
1141 PyObject* DocumentPy::getDependentDocuments(PyObject* args)
1142 {
1143 PyObject* sort = Py_True;
1144 if (!PyArg_ParseTuple(args, "|O!", &PyBool_Type, &sort)) {
1145 return nullptr;
1146 }
1147 PY_TRY
1148 {
1149 auto docs = getDocumentPtr()->getDependentDocuments(Base::asBoolean(sort));
1150 Py::List ret;
1151 for (auto doc : docs) {
1152 ret.append(Py::Object(doc->getPyObject(), true));
1153 }
1154 return Py::new_reference_to(ret);
1155 }
1156 PY_CATCH;
1157 }
1158
1159 Py::Boolean DocumentPy::getRestoring() const
1160 {
1161 return {getDocumentPtr()->testStatus(Document::Status::Restoring)};
1162 }
1163
1164 Py::Boolean DocumentPy::getPartial() const
1165 {
1166 return {getDocumentPtr()->testStatus(Document::Status::PartialDoc)};
1167 }
1168
1169 Py::Boolean DocumentPy::getImporting() const
1170 {
1171 return {getDocumentPtr()->testStatus(Document::Status::Importing)};
1172 }
1173
1174 Py::Boolean DocumentPy::getRecomputing() const
1175 {
1176 return {getDocumentPtr()->testStatus(Document::Status::Recomputing)};
1177 }
1178
1179 Py::Boolean DocumentPy::getTransacting() const
1180 {
1181 return {getDocumentPtr()->isPerformingTransaction()};
1182 }
1183
1184 Py::String DocumentPy::getOldLabel() const
1185 {
1186 return {getDocumentPtr()->getOldLabel()};
1187 }
1188
1189 Py::Boolean DocumentPy::getTemporary() const
1190 {
1191 return {getDocumentPtr()->testStatus(Document::TempDoc)};
1192 }