/
DocumentObjectPyImp.cpp
185 lines (164 loc) · 6.5 KB
/
DocumentObjectPyImp.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/***************************************************************************
* Copyright (c) Jürgen Riegel (juergen.riegel@web.de) 2007 *
* *
* This file is part of the FreeCAD CAx development system. *
* *
* This library is free software; you can redistribute it and/or *
* modify it under the terms of the GNU Library General Public *
* License as published by the Free Software Foundation; either *
* version 2 of the License, or (at your option) any later version. *
* *
* This library is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU Library General Public License for more details. *
* *
* You should have received a copy of the GNU Library General Public *
* License along with this library; see the file COPYING.LIB. If not, *
* write to the Free Software Foundation, Inc., 59 Temple Place, *
* Suite 330, Boston, MA 02111-1307, USA *
* *
***************************************************************************/
#include "PreCompiled.h"
#include "DocumentObject.h"
#include "Document.h"
// inclusion of the generated files (generated out of DocumentObjectPy.xml)
#include "DocumentObjectPy.h"
#include "DocumentObjectPy.cpp"
using namespace App;
// returns a string which represent the object e.g. when printed in python
std::string DocumentObjectPy::representation(void) const
{
return std::string("<Document object>");
}
Py::String DocumentObjectPy::getName(void) const
{
DocumentObject* object = this->getDocumentObjectPtr();
const char* internal = object->getNameInDocument();
if (!internal) {
throw Py::RuntimeError(std::string("This object is currently not part of a document"));
}
return Py::String(std::string(internal));
}
Py::Object DocumentObjectPy::getDocument(void) const
{
DocumentObject* object = this->getDocumentObjectPtr();
Document* doc = object->getDocument();
if (!doc) {
return Py::None();
}
else {
return Py::Object(doc->getPyObject(), true);
}
}
PyObject* DocumentObjectPy::touch(PyObject * args)
{
if (!PyArg_ParseTuple(args, "")) // convert args: Python->C
return NULL; // NULL triggers exception
getDocumentObjectPtr()->touch();
Py_Return;
}
PyObject* DocumentObjectPy::purgeTouched(PyObject * args)
{
if (!PyArg_ParseTuple(args, "")) // convert args: Python->C
return NULL; // NULL triggers exception
getDocumentObjectPtr()->purgeTouched();
Py_Return;
}
Py::List DocumentObjectPy::getState(void) const
{
DocumentObject* object = this->getDocumentObjectPtr();
Py::List list;
bool uptodate = true;
if (object->isTouched()) {
uptodate = false;
list.append(Py::String("Touched"));
}
if (object->isError()) {
uptodate = false;
list.append(Py::String("Invalid"));
}
if (object->isRecomputing()) {
uptodate = false;
list.append(Py::String("Recompute"));
}
if (object->isRestoring()) {
uptodate = false;
list.append(Py::String("Restore"));
}
if (uptodate) {
list.append(Py::String("Up-to-date"));
}
return list;
}
Py::Object DocumentObjectPy::getViewObject(void) const
{
try {
Py::Module module(PyImport_ImportModule("FreeCADGui"),true);
if (!module.hasAttr("getDocument")) {
// in v0.14+, the GUI module can be loaded in console mode (but doesn't have all its document methods)
return Py::None();
}
Py::Callable method(module.getAttr("getDocument"));
Py::Tuple arg(1);
arg.setItem(0, Py::String(getDocumentObjectPtr()->getDocument()->getName()));
Py::Object doc = method.apply(arg);
method = doc.getAttr("getObject");
arg.setItem(0, Py::String(getDocumentObjectPtr()->getNameInDocument()));
Py::Object obj = method.apply(arg);
return obj;
}
catch (Py::Exception& e) {
if (PyErr_ExceptionMatches(PyExc_ImportError)) {
// the GUI is not up, hence None is returned
e.clear();
return Py::None();
}
// FreeCADGui is loaded, so there must be wrong something else
throw; // re-throw
}
}
Py::List DocumentObjectPy::getInList(void) const
{
Py::List ret;
std::vector<DocumentObject*> list = getDocumentObjectPtr()->getInList();
for (std::vector<DocumentObject*>::iterator It=list.begin();It!=list.end();++It)
ret.append(Py::Object((*It)->getPyObject(), true));
return ret;
}
Py::List DocumentObjectPy::getOutList(void) const
{
Py::List ret;
std::vector<DocumentObject*> list = getDocumentObjectPtr()->getOutList();
for (std::vector<DocumentObject*>::iterator It=list.begin();It!=list.end();++It)
ret.append(Py::Object((*It)->getPyObject(), true));
return ret;
}
PyObject *DocumentObjectPy::getCustomAttributes(const char* /*attr*/) const
{
return 0;
}
int DocumentObjectPy::setCustomAttributes(const char* attr, PyObject *obj)
{
// search in PropertyList
Property *prop = getDocumentObjectPtr()->getPropertyByName(attr);
if (prop) {
// Read-only attributes must not be set over its Python interface
short Type = getDocumentObjectPtr()->getPropertyType(prop);
if (Type & Prop_ReadOnly) {
std::stringstream s;
s << "'DocumentObject' attribute '" << attr << "' is read-only";
throw Py::AttributeError(s.str());
}
try {
prop->setPyObject(obj);
}
catch (const Base::TypeError& e) {
std::stringstream s;
s << "Property '" << prop->getName() << "': " << e.what();
throw Py::TypeError(s.str());
}
return 1;
}
return 0;
}