/
FeaturePython.h
334 lines (288 loc) · 11.3 KB
/
FeaturePython.h
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
/***************************************************************************
* Copyright (c) Jürgen Riegel (juergen.riegel@web.de) 2006 *
* *
* 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 *
* *
***************************************************************************/
#ifndef APP_FEATUREPYTHON_H
#define APP_FEATUREPYTHON_H
#include <Base/Exception.h>
#include <Base/Writer.h>
#include <App/GeoFeature.h>
#include <App/DynamicProperty.h>
#include <App/PropertyPythonObject.h>
#include <App/PropertyGeo.h>
namespace App
{
class Property;
// Helper class to hide implementation details
class AppExport FeaturePythonImp
{
public:
FeaturePythonImp(App::DocumentObject*);
~FeaturePythonImp();
bool execute();
bool mustExecute() const;
void onBeforeChange(const Property* prop);
bool onBeforeChangeLabel(std::string &newLabel);
void onChanged(const Property* prop);
void onDocumentRestored();
bool allowOverrideViewProviderName() const;
std::string getViewProviderName();
PyObject *getPyObject(void);
bool getSubObject(App::DocumentObject *&ret, const char *subname, PyObject **pyObj,
Base::Matrix4D *mat, bool transform, int depth) const;
bool getSubObjects(std::vector<std::string> &ret, int reason) const;
bool getLinkedObject(App::DocumentObject *&ret, bool recurse,
Base::Matrix4D *mat, bool transform, int depth) const;
int canLinkProperties() const;
int allowDuplicateLabel() const;
int redirectSubName(std::ostringstream &ss,
App::DocumentObject *topParent, App::DocumentObject *child) const;
int canLoadPartial() const;
/// return true to activate tree view group object handling
int hasChildElement() const;
/// Get sub-element visibility
int isElementVisible(const char *) const;
/// Set sub-element visibility
int setElementVisible(const char *, bool);
private:
App::DocumentObject* object;
bool has__object__;
#define FC_PY_FEATURE_PYTHON \
FC_PY_ELEMENT(execute)\
FC_PY_ELEMENT(mustExecute)\
FC_PY_ELEMENT(onBeforeChange)\
FC_PY_ELEMENT(onBeforeChangeLabel)\
FC_PY_ELEMENT(onChanged)\
FC_PY_ELEMENT(onDocumentRestored)\
FC_PY_ELEMENT(getViewProviderName)\
FC_PY_ELEMENT(getSubObject)\
FC_PY_ELEMENT(getSubObjects)\
FC_PY_ELEMENT(getLinkedObject)\
FC_PY_ELEMENT(canLinkProperties)\
FC_PY_ELEMENT(allowDuplicateLabel)\
FC_PY_ELEMENT(redirectSubName)\
FC_PY_ELEMENT(canLoadPartial)\
FC_PY_ELEMENT(hasChildElement)\
FC_PY_ELEMENT(isElementVisible)\
FC_PY_ELEMENT(setElementVisible)
#define FC_PY_ELEMENT_DEFINE(_name) \
Py::Object py_##_name;
#define FC_PY_ELEMENT_INIT(_name) \
FC_PY_GetCallable(pyobj,#_name,py_##_name);\
if(!py_##_name.isNone()) {\
PyObject *pyRecursive = PyObject_GetAttrString(pyobj, \
"__allow_recursive_" #_name);\
if(!pyRecursive) {\
PyErr_Clear();\
_Flags.set(FlagAllowRecursive_##_name, false);\
}else{\
_Flags.set(FlagAllowRecursive_##_name, PyObject_IsTrue(pyRecursive));\
Py_DECREF(pyRecursive);\
}\
}
#define FC_PY_ELEMENT_FLAG(_name) \
FlagCalling_##_name,\
FlagAllowRecursive_##_name,
#define _FC_PY_CALL_CHECK(_name,_ret) \
if((!_Flags.test(FlagAllowRecursive_##_name) \
&& _Flags.test(FlagCalling_##_name)) \
|| py_##_name.isNone()) \
{\
_ret;\
}\
Base::BitsetLocker<Flags> guard(_Flags, FlagCalling_##_name);
#undef FC_PY_ELEMENT
#define FC_PY_ELEMENT(_name) FC_PY_ELEMENT_DEFINE(_name)
FC_PY_FEATURE_PYTHON
#undef FC_PY_ELEMENT
#define FC_PY_ELEMENT(_name) FC_PY_ELEMENT_FLAG(_name)
enum Flag {
FC_PY_FEATURE_PYTHON
FlagMax,
};
typedef std::bitset<FlagMax> Flags;
mutable Flags _Flags;
public:
void init(PyObject *pyobj);
};
/**
* Generic Python feature class which allows to behave every DocumentObject
* derived class as Python feature -- simply by subclassing.
* @author Werner Mayer
*/
template <class FeatureT>
class FeaturePythonT : public FeatureT
{
PROPERTY_HEADER_WITH_OVERRIDE(App::FeaturePythonT<FeatureT>);
public:
FeaturePythonT() {
ADD_PROPERTY(Proxy,(Py::Object()));
// cannot move this to the initializer list to avoid warning
imp = new FeaturePythonImp(this);
}
virtual ~FeaturePythonT() {
delete imp;
}
/** @name methods override DocumentObject */
//@{
short mustExecute() const override {
if (this->isTouched())
return 1;
auto ret = FeatureT::mustExecute();
if(ret) return ret;
return imp->mustExecute()?1:0;
}
/// recalculate the Feature
virtual DocumentObjectExecReturn *execute(void) override {
try {
bool handled = imp->execute();
if (!handled)
return FeatureT::execute();
}
catch (const Base::Exception& e) {
return new App::DocumentObjectExecReturn(e.what());
}
return DocumentObject::StdReturn;
}
virtual bool allowOverrideViewProviderName() const {
return imp->allowOverrideViewProviderName();
}
virtual const char* getViewProviderNameOverride(void) const override {
viewProviderName = imp->getViewProviderName();
if(viewProviderName.size())
return viewProviderName.c_str();
return FeatureT::getViewProviderNameOverride();
}
/// returns the type name of the ViewProvider
virtual const char* getViewProviderName(void) const override {
return FeatureT::getViewProviderName();
//return "Gui::ViewProviderPythonFeature";
}
virtual App::DocumentObject *getSubObject(const char *subname, PyObject **pyObj,
Base::Matrix4D *mat, bool transform, int depth) const override
{
App::DocumentObject *ret = 0;
if(imp->getSubObject(ret,subname,pyObj,mat,transform,depth))
return ret;
return FeatureT::getSubObject(subname,pyObj,mat,transform,depth);
}
virtual std::vector<std::string> getSubObjects(int reason=0) const override {
std::vector<std::string> ret;
if(imp->getSubObjects(ret,reason))
return ret;
return FeatureT::getSubObjects(reason);
}
virtual App::DocumentObject *getLinkedObject(bool recurse,
Base::Matrix4D *mat, bool transform, int depth) const override
{
App::DocumentObject *ret = 0;
if(imp->getLinkedObject(ret,recurse,mat,transform,depth))
return ret;
return FeatureT::getLinkedObject(recurse,mat,transform,depth);
}
/// return true to activate tree view group object handling
virtual bool hasChildElement() const override {
int ret = imp->hasChildElement();
if(ret<0)
return FeatureT::hasChildElement();
return ret?true:false;
}
/// Get sub-element visibility
virtual int isElementVisible(const char *element) const override {
int ret = imp->isElementVisible(element);
if(ret == -2)
return FeatureT::isElementVisible(element);
return ret;
}
/// Set sub-element visibility
virtual int setElementVisible(const char *element, bool visible) override {
int ret = imp->setElementVisible(element,visible);
if(ret == -2)
return FeatureT::setElementVisible(element,visible);
return ret;
}
virtual bool canLinkProperties() const override {
int ret = imp->canLinkProperties();
if(ret < 0)
return FeatureT::canLinkProperties();
return ret?true:false;
}
virtual bool allowDuplicateLabel() const override {
int ret = imp->allowDuplicateLabel();
if(ret < 0)
return FeatureT::allowDuplicateLabel();
return ret?true:false;
}
virtual bool redirectSubName(std::ostringstream &ss,
App::DocumentObject *topParent, App::DocumentObject *child) const override
{
int ret = imp->redirectSubName(ss,topParent,child);
if(ret < 0)
return FeatureT::redirectSubName(ss,topParent,child);
return ret?true:false;
}
virtual int canLoadPartial() const override {
int ret = imp->canLoadPartial();
if(ret>=0)
return ret;
return FeatureT::canLoadPartial();
}
PyObject *getPyObject(void) override {
if (FeatureT::PythonObject.is(Py::_None())) {
// ref counter is set to 1
FeatureT::PythonObject = Py::Object(imp->getPyObject(),true);
}
return Py::new_reference_to(FeatureT::PythonObject);
}
void setPyObject(PyObject *obj) override {
if (obj)
FeatureT::PythonObject = obj;
else
FeatureT::PythonObject = Py::None();
}
protected:
virtual void onBeforeChange(const Property* prop) override {
FeatureT::onBeforeChange(prop);
imp->onBeforeChange(prop);
}
virtual void onBeforeChangeLabel(std::string &newLabel) override{
if(!imp->onBeforeChangeLabel(newLabel))
FeatureT::onBeforeChangeLabel(newLabel);
}
virtual void onChanged(const Property* prop) override {
if(prop == &Proxy)
imp->init(Proxy.getValue().ptr());
imp->onChanged(prop);
FeatureT::onChanged(prop);
}
virtual void onDocumentRestored() override {
imp->onDocumentRestored();
FeatureT::onDocumentRestored();
}
private:
FeaturePythonImp* imp;
PropertyPythonObject Proxy;
mutable std::string viewProviderName;
};
// Special Feature-Python classes
typedef FeaturePythonT<DocumentObject> FeaturePython;
typedef FeaturePythonT<GeoFeature > GeometryPython;
} //namespace App
#endif // APP_FEATUREPYTHON_H