FeaturePython.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (c) Jürgen Riegel          (juergen.riegel@web.de) 2002     *
00003  *                                                                         *
00004  *   This file is part of the FreeCAD CAx development system.              *
00005  *                                                                         *
00006  *   This library is free software; you can redistribute it and/or         *
00007  *   modify it under the terms of the GNU Library General Public           *
00008  *   License as published by the Free Software Foundation; either          *
00009  *   version 2 of the License, or (at your option) any later version.      *
00010  *                                                                         *
00011  *   This library  is distributed in the hope that it will be useful,      *
00012  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00013  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00014  *   GNU Library General Public License for more details.                  *
00015  *                                                                         *
00016  *   You should have received a copy of the GNU Library General Public     *
00017  *   License along with this library; see the file COPYING.LIB. If not,    *
00018  *   write to the Free Software Foundation, Inc., 59 Temple Place,         *
00019  *   Suite 330, Boston, MA  02111-1307, USA                                *
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     // Run the execute method of the proxy object.
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; // extract the Python error text
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     // Run the execute method of the proxy object.
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; // extract the Python error text
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 // explicit template instantiation
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 // explicit template instantiation
00131 template class AppExport FeaturePythonT<GeoFeature>;
00132 }
00133 
00134 

Generated on Wed Nov 23 19:00:13 2011 for FreeCAD by  doxygen 1.6.1