FeaturePython.h

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (c) Jürgen Riegel          (juergen.riegel@web.de) 2006     *
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 
00025 #ifndef APP_FEATUREPYTHON_H
00026 #define APP_FEATUREPYTHON_H
00027 
00028 
00029 #include <Base/Writer.h>
00030 #include <App/GeoFeature.h>
00031 #include <App/DynamicProperty.h>
00032 #include <App/PropertyPythonObject.h>
00033 #include <App/PropertyGeo.h>
00034 #include <App/FeaturePythonPy.h>
00035 
00036 namespace App
00037 {
00038 
00039 class Property;
00040 class FeaturePythonPy;
00041 
00042 // Helper class to hide implementation details
00043 class AppExport FeaturePythonImp
00044 {
00045 public:
00046     FeaturePythonImp(App::DocumentObject*);
00047     ~FeaturePythonImp();
00048 
00049     DocumentObjectExecReturn *execute();
00050     void onChanged(const Property* prop);
00051 
00052 private:
00053     App::DocumentObject* object;
00054 };
00055 
00061 template <class FeatureT>
00062 class FeaturePythonT : public FeatureT
00063 {
00064     PROPERTY_HEADER(App::FeaturePythonT<FeatureT>);
00065 
00066 public:
00067     FeaturePythonT() {
00068         ADD_PROPERTY(Proxy,(Py::Object()));
00069         // cannot move this to the initializer list to avoid warning
00070         imp = new FeaturePythonImp(this);
00071         props = new DynamicProperty(this);
00072     }
00073     virtual ~FeaturePythonT() {
00074         delete imp;
00075         delete props;
00076     }
00077 
00080     short mustExecute() const {
00081         if (this->isTouched())
00082             return 1;
00083         return FeatureT::mustExecute();
00084     }
00086     virtual DocumentObjectExecReturn *execute(void) {
00087         return imp->execute();
00088     }
00090     virtual const char* getViewProviderName(void) const {
00091         return FeatureT::getViewProviderName();
00092         //return "Gui::ViewProviderPythonFeature";
00093     }
00094 
00097     Property* addDynamicProperty(
00098         const char* type, const char* name=0,
00099         const char* group=0, const char* doc=0,
00100         short attr=0, bool ro=false, bool hidden=false) {
00101         return props->addDynamicProperty(type, name, group, doc, attr, ro, hidden);
00102     }
00103     std::vector<std::string> getDynamicPropertyNames() const {
00104         return props->getDynamicPropertyNames();
00105     }
00106     Property *getDynamicPropertyByName(const char* name) const {
00107         return props->getDynamicPropertyByName(name);
00108     }
00109     virtual void addDynamicProperties(const PropertyContainer* cont) {
00110         return props->addDynamicProperties(cont);
00111     }
00113     virtual void getPropertyList(std::vector<Property*> &List) const {
00114         props->getPropertyList(List);
00115     }
00117     void getPropertyMap(std::map<std::string,Property*> &Map) const {
00118         props->getPropertyMap(Map);
00119     }
00121     virtual Property *getPropertyByName(const char* name) const {
00122         return props->getPropertyByName(name);
00123     }
00125     virtual const char* getName(const Property* prop) const {
00126         return props->getName(prop);
00127     }
00129 
00132 
00133     short getPropertyType(const Property* prop) const {
00134         return props->getPropertyType(prop);
00135     }
00137     short getPropertyType(const char *name) const {
00138         return props->getPropertyType(name);
00139     }
00141     const char* getPropertyGroup(const Property* prop) const {
00142         return props->getPropertyGroup(prop);
00143     }
00145     const char* getPropertyGroup(const char *name) const {
00146         return props->getPropertyGroup(name);
00147     }
00149     const char* getPropertyDocumentation(const Property* prop) const {
00150         return props->getPropertyDocumentation(prop);
00151     }
00153     const char* getPropertyDocumentation(const char *name) const {
00154         return props->getPropertyDocumentation(name);
00155     }
00157     bool isReadOnly(const Property* prop) const {
00158         return props->isReadOnly(prop);
00159     }
00161     bool isReadOnly(const char *name) const {
00162         return props->isReadOnly(name);
00163     }
00165     bool isHidden(const Property* prop) const {
00166         return props->isHidden(prop);
00167     }
00169     bool isHidden(const char *name) const {
00170         return props->isHidden(name);
00171     }
00173 
00176     void Save (Base::Writer &writer) const {
00177         writer.ObjectName = this->getNameInDocument();
00178         props->Save(writer);
00179     }
00180     void Restore(Base::XMLReader &reader) {
00181         props->Restore(reader);
00182     }
00184 
00185     PyObject *getPyObject(void) {
00186         if (FeatureT::PythonObject.is(Py::_None())) {
00187             // ref counter is set to 1
00188             FeatureT::PythonObject = Py::Object(new FeaturePythonPy(this),true);
00189         }
00190         return Py::new_reference_to(FeatureT::PythonObject);
00191     }
00192     void setPyObject(PyObject *obj) {
00193         if (obj)
00194             FeatureT::PythonObject = obj;
00195         else
00196             FeatureT::PythonObject = Py::None();
00197     }
00198 
00199     friend class FeaturePythonPy;
00200 
00201 protected:
00202     virtual void onChanged(const Property* prop) {
00203         imp->onChanged(prop);
00204         FeatureT::onChanged(prop);
00205     }
00206 
00207 private:
00208     FeaturePythonImp* imp;
00209     DynamicProperty* props;
00210     PropertyPythonObject Proxy;
00211 };
00212 
00213 // Special Feature-Python classes
00214 typedef FeaturePythonT<DocumentObject> FeaturePython;
00215 typedef FeaturePythonT<GeoFeature    > GeometryPython;
00216 
00217 } //namespace App
00218 
00219 #endif // APP_FEATUREPYTHON_H

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