| 1 |
/*************************************************************************** |
| 2 |
* Copyright (c) 2006 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 |
|
| 24 |
#ifndef APP_FEATUREPYTHON_H |
| 25 |
#define APP_FEATUREPYTHON_H |
| 26 |
|
| 27 |
#include <App/GeoFeature.h> |
| 28 |
#include <App/PropertyPythonObject.h> |
| 29 |
|
| 30 |
|
| 31 |
namespace App |
| 32 |
{ |
| 33 |
|
| 34 |
class Property; |
| 35 |
|
| 36 |
// Helper class to hide implementation details |
| 37 |
class AppExport FeaturePythonImp |
| 38 |
{ |
| 39 |
public: |
| 40 |
enum ValueT |
| 41 |
{ |
| 42 |
NotImplemented = 0, // not handled |
| 43 |
Accepted = 1, // handled and accepted |
| 44 |
Rejected = 2 // handled and rejected |
| 45 |
}; |
| 46 |
|
| 47 |
explicit FeaturePythonImp(App::DocumentObject*); |
| 48 |
~FeaturePythonImp(); |
| 49 |
|
| 50 |
bool execute(); |
| 51 |
bool mustExecute() const; |
| 52 |
void onBeforeChange(const Property* prop); |
| 53 |
bool onBeforeChangeLabel(std::string& newLabel); |
| 54 |
void onChanged(const Property* prop); |
| 55 |
void onDocumentRestored(); |
| 56 |
void unsetupObject(); |
| 57 |
std::string getViewProviderName(); |
| 58 |
PyObject* getPyObject(); |
| 59 |
|
| 60 |
bool getSubObject(App::DocumentObject*& ret, |
| 61 |
const char* subname, |
| 62 |
PyObject** pyObj, |
| 63 |
Base::Matrix4D* mat, |
| 64 |
bool transform, |
| 65 |
int depth) const; |
| 66 |
|
| 67 |
bool getSubObjects(std::vector<std::string>& ret, int reason) const; |
| 68 |
|
| 69 |
bool getLinkedObject(App::DocumentObject*& ret, |
| 70 |
bool recurse, |
| 71 |
Base::Matrix4D* mat, |
| 72 |
bool transform, |
| 73 |
int depth) const; |
| 74 |
|
| 75 |
ValueT canLinkProperties() const; |
| 76 |
|
| 77 |
ValueT allowDuplicateLabel() const; |
| 78 |
|
| 79 |
ValueT redirectSubName(std::ostringstream& ss, |
| 80 |
App::DocumentObject* topParent, |
| 81 |
App::DocumentObject* child) const; |
| 82 |
|
| 83 |
int canLoadPartial() const; |
| 84 |
|
| 85 |
/// return true to activate tree view group object handling |
| 86 |
ValueT hasChildElement() const; |
| 87 |
/// Get sub-element visibility |
| 88 |
int isElementVisible(const char*) const; |
| 89 |
/// Set sub-element visibility |
| 90 |
int setElementVisible(const char*, bool); |
| 91 |
|
| 92 |
bool editProperty(const char* propName); |
| 93 |
|
| 94 |
ValueT isLink() const; |
| 95 |
ValueT isLinkGroup() const; |
| 96 |
|
| 97 |
bool getPlacementOf(Base::Placement& ret, const char* subname, App::DocumentObject* target) const; |
| 98 |
|
| 99 |
private: |
| 100 |
App::DocumentObject* object; |
| 101 |
bool has__object__ {false}; |
| 102 |
|
| 103 |
#define FC_PY_FEATURE_PYTHON \ |
| 104 |
FC_PY_ELEMENT(execute) \ |
| 105 |
FC_PY_ELEMENT(mustExecute) \ |
| 106 |
FC_PY_ELEMENT(onBeforeChange) \ |
| 107 |
FC_PY_ELEMENT(onBeforeChangeLabel) \ |
| 108 |
FC_PY_ELEMENT(onChanged) \ |
| 109 |
FC_PY_ELEMENT(onDocumentRestored) \ |
| 110 |
FC_PY_ELEMENT(unsetupObject) \ |
| 111 |
FC_PY_ELEMENT(getViewProviderName) \ |
| 112 |
FC_PY_ELEMENT(getSubObject) \ |
| 113 |
FC_PY_ELEMENT(getSubObjects) \ |
| 114 |
FC_PY_ELEMENT(getLinkedObject) \ |
| 115 |
FC_PY_ELEMENT(canLinkProperties) \ |
| 116 |
FC_PY_ELEMENT(allowDuplicateLabel) \ |
| 117 |
FC_PY_ELEMENT(redirectSubName) \ |
| 118 |
FC_PY_ELEMENT(canLoadPartial) \ |
| 119 |
FC_PY_ELEMENT(hasChildElement) \ |
| 120 |
FC_PY_ELEMENT(isElementVisible) \ |
| 121 |
FC_PY_ELEMENT(setElementVisible) \ |
| 122 |
FC_PY_ELEMENT(editProperty) \ |
| 123 |
FC_PY_ELEMENT(isLink) \ |
| 124 |
FC_PY_ELEMENT(isLinkGroup) \ |
| 125 |
FC_PY_ELEMENT(getPlacementOf) |
| 126 |
|
| 127 |
#define FC_PY_ELEMENT_DEFINE(_name) Py::Object py_##_name; |
| 128 |
|
| 129 |
#define FC_PY_ELEMENT_INIT(_name) \ |
| 130 |
FC_PY_GetCallable(pyobj, #_name, py_##_name); \ |
| 131 |
if (!py_##_name.isNone()) { \ |
| 132 |
PyObject* pyRecursive = PyObject_GetAttrString(pyobj, "__allow_recursive_" #_name); \ |
| 133 |
if (!pyRecursive) { \ |
| 134 |
PyErr_Clear(); \ |
| 135 |
_Flags.set(FlagAllowRecursive_##_name, false); \ |
| 136 |
} \ |
| 137 |
else { \ |
| 138 |
_Flags.set(FlagAllowRecursive_##_name, PyObject_IsTrue(pyRecursive)); \ |
| 139 |
Py_DECREF(pyRecursive); \ |
| 140 |
} \ |
| 141 |
} |
| 142 |
|
| 143 |
#define FC_PY_ELEMENT_FLAG(_name) FlagCalling_##_name, FlagAllowRecursive_##_name, |
| 144 |
|
| 145 |
#define _FC_PY_CALL_CHECK(_name, _ret) \ |
| 146 |
if ((!_Flags.test(FlagAllowRecursive_##_name) && _Flags.test(FlagCalling_##_name)) \ |
| 147 |
|| py_##_name.isNone()) { \ |
| 148 |
_ret; \ |
| 149 |
} \ |
| 150 |
Base::BitsetLocker<Flags> guard(_Flags, FlagCalling_##_name); |
| 151 |
|
| 152 |
#undef FC_PY_ELEMENT |
| 153 |
#define FC_PY_ELEMENT(_name) FC_PY_ELEMENT_DEFINE(_name) |
| 154 |
|
| 155 |
FC_PY_FEATURE_PYTHON |
| 156 |
|
| 157 |
#undef FC_PY_ELEMENT |
| 158 |
#define FC_PY_ELEMENT(_name) FC_PY_ELEMENT_FLAG(_name) |
| 159 |
|
| 160 |
enum Flag |
| 161 |
{ |
| 162 |
FC_PY_FEATURE_PYTHON FlagMax, |
| 163 |
}; |
| 164 |
|
| 165 |
using Flags = std::bitset<FlagMax>; |
| 166 |
mutable Flags _Flags; |
| 167 |
|
| 168 |
public: |
| 169 |
void init(PyObject* pyobj); |
| 170 |
}; |
| 171 |
|
| 172 |
/** |
| 173 |
* Generic Python feature class which allows one to behave every DocumentObject |
| 174 |
* derived class as Python feature -- simply by subclassing. |
| 175 |
* @author Werner Mayer |
| 176 |
*/ |
| 177 |
template<class FeatureT> |
| 178 |
class FeaturePythonT: public FeatureT |
| 179 |
{ |
| 180 |
PROPERTY_HEADER_WITH_OVERRIDE(App::FeaturePythonT<FeatureT>); |
| 181 |
|
| 182 |
public: |
| 183 |
FeaturePythonT() |
| 184 |
{ |
| 185 |
ADD_PROPERTY(Proxy, (Py::Object())); |
| 186 |
// cannot move this to the initializer list to avoid warning |
| 187 |
imp = new FeaturePythonImp(this); |
| 188 |
} |
| 189 |
~FeaturePythonT() override |
| 190 |
{ |
| 191 |
delete imp; |
| 192 |
} |
| 193 |
|
| 194 |
/** @name methods override DocumentObject */ |
| 195 |
//@{ |
| 196 |
short mustExecute() const override |
| 197 |
{ |
| 198 |
if (this->isTouched()) { |
| 199 |
return 1; |
| 200 |
} |
| 201 |
auto ret = FeatureT::mustExecute(); |
| 202 |
if (ret) { |
| 203 |
return ret; |
| 204 |
} |
| 205 |
return imp->mustExecute() ? 1 : 0; |
| 206 |
} |
| 207 |
/// recalculate the Feature |
| 208 |
DocumentObjectExecReturn* execute() override |
| 209 |
{ |
| 210 |
try { |
| 211 |
bool handled = imp->execute(); |
| 212 |
if (!handled) { |
| 213 |
return FeatureT::execute(); |
| 214 |
} |
| 215 |
} |
| 216 |
catch (const Base::Exception& e) { |
| 217 |
return new App::DocumentObjectExecReturn(e.what()); |
| 218 |
} |
| 219 |
return DocumentObject::StdReturn; |
| 220 |
} |
| 221 |
const char* getViewProviderNameOverride() const override |
| 222 |
{ |
| 223 |
viewProviderName = imp->getViewProviderName(); |
| 224 |
if (!viewProviderName.empty()) { |
| 225 |
return viewProviderName.c_str(); |
| 226 |
} |
| 227 |
return FeatureT::getViewProviderNameOverride(); |
| 228 |
} |
| 229 |
/// returns the type name of the ViewProvider |
| 230 |
const char* getViewProviderName() const override |
| 231 |
{ |
| 232 |
return FeatureT::getViewProviderName(); |
| 233 |
} |
| 234 |
|
| 235 |
App::DocumentObject* getSubObject(const char* subname, |
| 236 |
PyObject** pyObj, |
| 237 |
Base::Matrix4D* mat, |
| 238 |
bool transform, |
| 239 |
int depth) const override |
| 240 |
{ |
| 241 |
App::DocumentObject* ret = nullptr; |
| 242 |
if (imp->getSubObject(ret, subname, pyObj, mat, transform, depth)) { |
| 243 |
return ret; |
| 244 |
} |
| 245 |
return FeatureT::getSubObject(subname, pyObj, mat, transform, depth); |
| 246 |
} |
| 247 |
|
| 248 |
std::vector<std::string> getSubObjects(int reason = 0) const override |
| 249 |
{ |
| 250 |
std::vector<std::string> ret; |
| 251 |
if (imp->getSubObjects(ret, reason)) { |
| 252 |
return ret; |
| 253 |
} |
| 254 |
return FeatureT::getSubObjects(reason); |
| 255 |
} |
| 256 |
|
| 257 |
App::DocumentObject* |
| 258 |
getLinkedObject(bool recurse, Base::Matrix4D* mat, bool transform, int depth) const override |
| 259 |
{ |
| 260 |
App::DocumentObject* ret = nullptr; |
| 261 |
if (imp->getLinkedObject(ret, recurse, mat, transform, depth)) { |
| 262 |
return ret; |
| 263 |
} |
| 264 |
return FeatureT::getLinkedObject(recurse, mat, transform, depth); |
| 265 |
} |
| 266 |
|
| 267 |
/// return true to activate tree view group object handling |
| 268 |
bool hasChildElement() const override |
| 269 |
{ |
| 270 |
switch (imp->hasChildElement()) { |
| 271 |
case FeaturePythonImp::Accepted: |
| 272 |
return true; |
| 273 |
case FeaturePythonImp::Rejected: |
| 274 |
return false; |
| 275 |
default: |
| 276 |
return FeatureT::hasChildElement(); |
| 277 |
} |
| 278 |
} |
| 279 |
/// Get sub-element visibility |
| 280 |
int isElementVisible(const char* element) const override |
| 281 |
{ |
| 282 |
int ret = imp->isElementVisible(element); |
| 283 |
if (ret == -2) { |
| 284 |
return FeatureT::isElementVisible(element); |
| 285 |
} |
| 286 |
return ret; |
| 287 |
} |
| 288 |
/// Set sub-element visibility |
| 289 |
int setElementVisible(const char* element, bool visible) override |
| 290 |
{ |
| 291 |
int ret = imp->setElementVisible(element, visible); |
| 292 |
if (ret == -2) { |
| 293 |
return FeatureT::setElementVisible(element, visible); |
| 294 |
} |
| 295 |
return ret; |
| 296 |
} |
| 297 |
|
| 298 |
bool canLinkProperties() const override |
| 299 |
{ |
| 300 |
switch (imp->canLinkProperties()) { |
| 301 |
case FeaturePythonImp::Accepted: |
| 302 |
return true; |
| 303 |
case FeaturePythonImp::Rejected: |
| 304 |
return false; |
| 305 |
default: |
| 306 |
return FeatureT::canLinkProperties(); |
| 307 |
} |
| 308 |
} |
| 309 |
|
| 310 |
bool allowDuplicateLabel() const override |
| 311 |
{ |
| 312 |
switch (imp->allowDuplicateLabel()) { |
| 313 |
case FeaturePythonImp::Accepted: |
| 314 |
return true; |
| 315 |
case FeaturePythonImp::Rejected: |
| 316 |
return false; |
| 317 |
default: |
| 318 |
return FeatureT::allowDuplicateLabel(); |
| 319 |
} |
| 320 |
} |
| 321 |
|
| 322 |
bool redirectSubName(std::ostringstream& ss, |
| 323 |
App::DocumentObject* topParent, |
| 324 |
App::DocumentObject* child) const override |
| 325 |
{ |
| 326 |
switch (imp->redirectSubName(ss, topParent, child)) { |
| 327 |
case FeaturePythonImp::Accepted: |
| 328 |
return true; |
| 329 |
case FeaturePythonImp::Rejected: |
| 330 |
return false; |
| 331 |
default: |
| 332 |
return FeatureT::redirectSubName(ss, topParent, child); |
| 333 |
} |
| 334 |
} |
| 335 |
|
| 336 |
int canLoadPartial() const override |
| 337 |
{ |
| 338 |
int ret = imp->canLoadPartial(); |
| 339 |
if (ret >= 0) { |
| 340 |
return ret; |
| 341 |
} |
| 342 |
return FeatureT::canLoadPartial(); |
| 343 |
} |
| 344 |
|
| 345 |
/** |
| 346 |
* @brief Called when a property is edited by the user. |
| 347 |
* |
| 348 |
* This override first attempts to handle the property edit in the |
| 349 |
* associated Python object. |
| 350 |
* |
| 351 |
* @param[in] propName The name of the property to be edited. |
| 352 |
*/ |
| 353 |
void editProperty(const char* propName) override |
| 354 |
{ |
| 355 |
if (!imp->editProperty(propName)) { |
| 356 |
FeatureT::editProperty(propName); |
| 357 |
} |
| 358 |
} |
| 359 |
|
| 360 |
bool isLink() const override |
| 361 |
{ |
| 362 |
switch (imp->isLink()) { |
| 363 |
case FeaturePythonImp::Accepted: |
| 364 |
return true; |
| 365 |
case FeaturePythonImp::Rejected: |
| 366 |
return false; |
| 367 |
default: |
| 368 |
return FeatureT::isLink(); |
| 369 |
} |
| 370 |
} |
| 371 |
|
| 372 |
bool isLinkGroup() const override |
| 373 |
{ |
| 374 |
switch (imp->isLinkGroup()) { |
| 375 |
case FeaturePythonImp::Accepted: |
| 376 |
return true; |
| 377 |
case FeaturePythonImp::Rejected: |
| 378 |
return false; |
| 379 |
default: |
| 380 |
return FeatureT::isLinkGroup(); |
| 381 |
} |
| 382 |
} |
| 383 |
|
| 384 |
Base::Placement getPlacementOf(const std::string& sub, DocumentObject* targetObj = nullptr) override |
| 385 |
{ |
| 386 |
Base::Placement ret; |
| 387 |
// Try to call the python implementation first |
| 388 |
if (imp->getPlacementOf(ret, sub.c_str(), targetObj)) { |
| 389 |
return ret; |
| 390 |
} |
| 391 |
// Fallback to C++ implementation |
| 392 |
return FeatureT::getPlacementOf(sub, targetObj); |
| 393 |
} |
| 394 |
|
| 395 |
PyObject* getPyObject() override |
| 396 |
{ |
| 397 |
if (FeatureT::PythonObject.is(Py::_None())) { |
| 398 |
// ref counter is set to 1 |
| 399 |
FeatureT::PythonObject = Py::Object(imp->getPyObject(), true); |
| 400 |
} |
| 401 |
return Py::new_reference_to(FeatureT::PythonObject); |
| 402 |
} |
| 403 |
void setPyObject(PyObject* obj) override |
| 404 |
{ |
| 405 |
if (obj) { |
| 406 |
FeatureT::PythonObject = obj; |
| 407 |
} |
| 408 |
else { |
| 409 |
FeatureT::PythonObject = Py::None(); |
| 410 |
} |
| 411 |
} |
| 412 |
|
| 413 |
protected: |
| 414 |
void onBeforeChange(const Property* prop) override |
| 415 |
{ |
| 416 |
FeatureT::onBeforeChange(prop); |
| 417 |
imp->onBeforeChange(prop); |
| 418 |
} |
| 419 |
void onBeforeChangeLabel(std::string& newLabel) override |
| 420 |
{ |
| 421 |
if (!imp->onBeforeChangeLabel(newLabel)) { |
| 422 |
FeatureT::onBeforeChangeLabel(newLabel); |
| 423 |
} |
| 424 |
} |
| 425 |
void onChanged(const Property* prop) override |
| 426 |
{ |
| 427 |
if (prop == &Proxy) { |
| 428 |
imp->init(Proxy.getValue().ptr()); |
| 429 |
} |
| 430 |
imp->onChanged(prop); |
| 431 |
FeatureT::onChanged(prop); |
| 432 |
} |
| 433 |
void onDocumentRestored() override |
| 434 |
{ |
| 435 |
imp->onDocumentRestored(); |
| 436 |
FeatureT::onDocumentRestored(); |
| 437 |
} |
| 438 |
void unsetupObject() override |
| 439 |
{ |
| 440 |
imp->unsetupObject(); |
| 441 |
FeatureT::unsetupObject(); |
| 442 |
} |
| 443 |
|
| 444 |
public: |
| 445 |
FeaturePythonT(const FeaturePythonT&) = delete; |
| 446 |
FeaturePythonT(FeaturePythonT&&) = delete; |
| 447 |
FeaturePythonT& operator=(const FeaturePythonT&) = delete; |
| 448 |
FeaturePythonT& operator=(FeaturePythonT&&) = delete; |
| 449 |
|
| 450 |
private: |
| 451 |
FeaturePythonImp* imp; |
| 452 |
PropertyPythonObject Proxy; |
| 453 |
mutable std::string viewProviderName; |
| 454 |
}; |
| 455 |
|
| 456 |
// Special Feature-Python classes |
| 457 |
using FeaturePython = FeaturePythonT<DocumentObject>; |
| 458 |
using GeometryPython = FeaturePythonT<GeoFeature>; |
| 459 |
|
| 460 |
} // namespace App |
| 461 |
|
| 462 |
#endif // APP_FEATUREPYTHON_H |