FeaturePython.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include "PreCompiled.h"
00025 #ifndef _PreComp_
00026 # include <sstream>
00027 #endif
00028
00029
00030 #include <Base/Console.h>
00031 #include <Base/Interpreter.h>
00032 #include <Base/Reader.h>
00033
00034 #include "FeaturePython.h"
00035 #include "FeaturePythonPy.h"
00036
00037 using namespace App;
00038
00039 FeaturePythonImp::FeaturePythonImp(App::DocumentObject* o) : object(o)
00040 {
00041 }
00042
00043 FeaturePythonImp::~FeaturePythonImp()
00044 {
00045 }
00046
00047 DocumentObjectExecReturn *FeaturePythonImp::execute()
00048 {
00049
00050 Base::PyGILStateLocker lock;
00051 try {
00052 Property* proxy = object->getPropertyByName("Proxy");
00053 if (proxy && proxy->getTypeId() == PropertyPythonObject::getClassTypeId()) {
00054 Py::Object feature = static_cast<PropertyPythonObject*>(proxy)->getValue();
00055 if (feature.hasAttr("__object__")) {
00056 Py::Callable method(feature.getAttr(std::string("execute")));
00057 Py::Tuple args(0);
00058 method.apply(args);
00059 }
00060 else {
00061 Py::Callable method(feature.getAttr(std::string("execute")));
00062 Py::Tuple args(1);
00063 args.setItem(0, Py::Object(object->getPyObject(), true));
00064 method.apply(args);
00065 }
00066 }
00067 }
00068 catch (Py::Exception&) {
00069 Base::PyException e;
00070 std::stringstream str;
00071 str << object->Label.getValue() << ": " << e.what();
00072 return new App::DocumentObjectExecReturn(str.str());
00073 }
00074
00075 return DocumentObject::StdReturn;
00076 }
00077
00078 void FeaturePythonImp::onChanged(const Property* prop)
00079 {
00080
00081 Base::PyGILStateLocker lock;
00082 try {
00083 Property* proxy = object->getPropertyByName("Proxy");
00084 if (proxy && proxy->getTypeId() == PropertyPythonObject::getClassTypeId()) {
00085 Py::Object feature = static_cast<PropertyPythonObject*>(proxy)->getValue();
00086 if (feature.hasAttr(std::string("onChanged"))) {
00087 if (feature.hasAttr("__object__")) {
00088 Py::Callable method(feature.getAttr(std::string("onChanged")));
00089 Py::Tuple args(1);
00090 std::string prop_name = object->getName(prop);
00091 args.setItem(0, Py::String(prop_name));
00092 method.apply(args);
00093 }
00094 else {
00095 Py::Callable method(feature.getAttr(std::string("onChanged")));
00096 Py::Tuple args(2);
00097 args.setItem(0, Py::Object(object->getPyObject(), true));
00098 std::string prop_name = object->getName(prop);
00099 args.setItem(1, Py::String(prop_name));
00100 method.apply(args);
00101 }
00102 }
00103 }
00104 }
00105 catch (Py::Exception&) {
00106 Base::PyException e;
00107 Base::Console().Error("FeaturePython::onChanged (%s): %s\n",
00108 object->Label.getValue(), e.what());
00109 }
00110 }
00111
00112
00113
00114 namespace App {
00115 PROPERTY_SOURCE_TEMPLATE(App::FeaturePython, App::DocumentObject)
00116 template<> const char* App::FeaturePython::getViewProviderName(void) const {
00117 return "Gui::ViewProviderPythonFeature";
00118 }
00119
00120 template class AppExport FeaturePythonT<DocumentObject>;
00121 }
00122
00123
00124
00125 namespace App {
00126 PROPERTY_SOURCE_TEMPLATE(App::GeometryPython, App::GeoFeature)
00127 template<> const char* App::GeometryPython::getViewProviderName(void) const {
00128 return "Gui::ViewProviderPythonGeometry";
00129 }
00130
00131 template class AppExport FeaturePythonT<GeoFeature>;
00132 }
00133
00134