diff --git a/src/App/CMakeLists.txt b/src/App/CMakeLists.txt index c55ade764..c19c09f11 100644 --- a/src/App/CMakeLists.txt +++ b/src/App/CMakeLists.txt @@ -39,7 +39,6 @@ endif(WIN32) generate_from_xml(DocumentPy) generate_from_xml(DocumentObjectPy) generate_from_xml(DocumentObjectGroupPy) -generate_from_xml(FeaturePythonPy) generate_from_xml(ComplexGeoDataPy) generate_from_xml(PropertyContainerPy) generate_from_xml(MaterialPy) @@ -51,7 +50,6 @@ SET(FreeCADApp_XML_SRCS DocumentObjectGroupPy.xml DocumentObjectPy.xml DocumentPy.xml - FeaturePythonPy.xml PropertyContainerPy.xml ComplexGeoDataPy.xml MaterialPy.xml @@ -71,7 +69,6 @@ SET(Document_CPP_SRCS DocumentObserverPython.cpp DocumentPyImp.cpp FeaturePython.cpp - FeaturePythonPyImp.cpp FeatureTest.cpp GeoFeature.cpp InventorObject.cpp @@ -91,6 +88,8 @@ SET(Document_HPP_SRCS DocumentObserver.h DocumentObserverPython.h FeaturePython.h + FeaturePythonPyImp.h + FeaturePythonPyImp.inl FeatureTest.h GeoFeature.h InventorObject.h diff --git a/src/App/DocumentObjectGroup.cpp b/src/App/DocumentObjectGroup.cpp index 1c16e0e51..d6781d6be 100644 --- a/src/App/DocumentObjectGroup.cpp +++ b/src/App/DocumentObjectGroup.cpp @@ -29,6 +29,7 @@ #include "DocumentObjectGroup.h" #include "DocumentObjectGroupPy.h" #include "Document.h" +#include "FeaturePythonPyImp.h" using namespace App; @@ -187,6 +188,13 @@ PROPERTY_SOURCE_TEMPLATE(App::DocumentObjectGroupPython, App::DocumentObjectGrou template<> const char* App::DocumentObjectGroupPython::getViewProviderName(void) const { return "Gui::ViewProviderDocumentObjectGroupPython"; } +template<> PyObject* App::DocumentObjectGroupPython::getPyObject(void) { + if (PythonObject.is(Py::_None())) { + // ref counter is set to 1 + PythonObject = Py::Object(new FeaturePythonPyT(this),true); + } + return Py::new_reference_to(PythonObject); +} /// @endcond // explicit template instantiation diff --git a/src/App/FeaturePython.cpp b/src/App/FeaturePython.cpp index 9d377894b..a4b1a0e12 100644 --- a/src/App/FeaturePython.cpp +++ b/src/App/FeaturePython.cpp @@ -32,7 +32,7 @@ #include #include "FeaturePython.h" -#include "FeaturePythonPy.h" +#include "FeaturePythonPyImp.h" using namespace App; @@ -109,13 +109,26 @@ void FeaturePythonImp::onChanged(const Property* prop) } } +PyObject *FeaturePythonImp::getPyObject(void) +{ + // ref counter is set to 1 + return new FeaturePythonPyT(object); +} + // --------------------------------------------------------- - + namespace App { PROPERTY_SOURCE_TEMPLATE(App::FeaturePython, App::DocumentObject) template<> const char* App::FeaturePython::getViewProviderName(void) const { return "Gui::ViewProviderPythonFeature"; } +template<> PyObject* App::FeaturePython::getPyObject(void) { + if (PythonObject.is(Py::_None())) { + // ref counter is set to 1 + PythonObject = Py::Object(new FeaturePythonPyT(this),true); + } + return Py::new_reference_to(PythonObject); +} // explicit template instantiation template class AppExport FeaturePythonT; } diff --git a/src/App/FeaturePython.h b/src/App/FeaturePython.h index b45fa3436..bffb4d5b7 100644 --- a/src/App/FeaturePython.h +++ b/src/App/FeaturePython.h @@ -31,13 +31,11 @@ #include #include #include -#include namespace App { class Property; -class FeaturePythonPy; // Helper class to hide implementation details class AppExport FeaturePythonImp @@ -48,6 +46,7 @@ public: DocumentObjectExecReturn *execute(); void onChanged(const Property* prop); + PyObject *getPyObject(void); private: App::DocumentObject* object; @@ -188,7 +187,7 @@ public: PyObject *getPyObject(void) { if (FeatureT::PythonObject.is(Py::_None())) { // ref counter is set to 1 - FeatureT::PythonObject = Py::Object(new FeaturePythonPy(this),true); + FeatureT::PythonObject = Py::Object(imp->getPyObject(),true); } return Py::new_reference_to(FeatureT::PythonObject); } @@ -199,8 +198,6 @@ public: FeatureT::PythonObject = Py::None(); } - friend class FeaturePythonPy; - protected: virtual void onChanged(const Property* prop) { imp->onChanged(prop); diff --git a/src/App/FeaturePythonPy.xml b/src/App/FeaturePythonPy.xml deleted file mode 100644 index 0b58cb5a7..000000000 --- a/src/App/FeaturePythonPy.xml +++ /dev/null @@ -1,37 +0,0 @@ - - - - - - This is the father of all Feature classes - - - - addProperty(string, string) -- Add a generic property. -The first argument specifies the type, the second the -name of the property. - - - - - - removeProperty(string) -- Remove a generic property. -Note, you can only remove user-defined properties but not built-in ones. - - - - - - A list of supported property types - - - - diff --git a/src/App/FeaturePythonPyImp.cpp b/src/App/FeaturePythonPyImp.cpp deleted file mode 100644 index 3a6fb3958..000000000 --- a/src/App/FeaturePythonPyImp.cpp +++ /dev/null @@ -1,135 +0,0 @@ -/*************************************************************************** - * Copyright (c) 2007 Werner Mayer * - * * - * 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 * - * * - ***************************************************************************/ - - -#include "PreCompiled.h" - -#ifndef _PreComp_ -# include -#endif - -#include "FeaturePython.h" - -// inclusion of the generated files (generated out of FeaturePythonPy.xml) -#include "FeaturePythonPy.h" -#include "FeaturePythonPy.cpp" - -using namespace App; - -// returns a string which represent the object e.g. when printed in python -std::string FeaturePythonPy::representation(void) const -{ - return std::string(""); -} - -PyObject* FeaturePythonPy::addProperty(PyObject *args) -{ - char *sType,*sName=0,*sGroup=0,*sDoc=0; - short attr=0; - PyObject *ro = Py_False, *hd = Py_False; - if (!PyArg_ParseTuple(args, "s|ssshO!O!", &sType,&sName,&sGroup,&sDoc,&attr, - &PyBool_Type, &ro, &PyBool_Type, &hd)) // convert args: Python->C - return NULL; // NULL triggers exception - - Property* prop=0; - prop = getFeaturePythonPtr()->addDynamicProperty(sType,sName,sGroup,sDoc,attr, - PyObject_IsTrue(ro) ? true : false, PyObject_IsTrue(hd) ? true : false); - - if (!prop) { - std::stringstream str; - str << "No property found of type '" << sType << "'" << std::ends; - throw Py::Exception(PyExc_Exception,str.str()); - } - - return Py::new_reference_to(this); -} - -PyObject* FeaturePythonPy::removeProperty(PyObject *args) -{ - char *sName; - if (!PyArg_ParseTuple(args, "s", &sName)) - return NULL; - - bool ok = getFeaturePythonPtr()->removeDynamicProperty(sName); - return Py_BuildValue("O", (ok ? Py_True : Py_False)); -} - -PyObject* FeaturePythonPy::supportedProperties(PyObject *args) -{ - if (!PyArg_ParseTuple(args, "")) // convert args: Python->C - return NULL; // NULL triggers exception - - std::vector ary; - Base::Type::getAllDerivedFrom(App::Property::getClassTypeId(), ary); - Py::List res; - for (std::vector::iterator it = ary.begin(); it != ary.end(); ++it) { - Base::BaseClass *data = static_cast(it->createInstance()); - if (data) { - delete data; - res.append(Py::String(it->getName())); - } - } - return Py::new_reference_to(res); -} - -PyObject *FeaturePythonPy::getCustomAttributes(const char* attr) const -{ - PY_TRY{ - if (Base::streq(attr, "__dict__")){ - PyObject* dict = DocumentObjectPy::getCustomAttributes(attr); - if (dict){ - std::vector Props = getFeaturePythonPtr()->getDynamicPropertyNames(); - for (std::vector::const_iterator it = Props.begin(); it != Props.end(); ++it) - PyDict_SetItem(dict, PyString_FromString(it->c_str()), PyString_FromString("")); - } - return dict; - } - - // search for dynamic property - Property* prop = getFeaturePythonPtr()->getDynamicPropertyByName(attr); - if (prop) return prop->getPyObject(); - } PY_CATCH; - - return 0; -} - -int FeaturePythonPy::setCustomAttributes(const char* attr, PyObject *value) -{ - // search for dynamic property - Property* prop = getFeaturePythonPtr()->getDynamicPropertyByName(attr); - - if (!prop) - return DocumentObjectPy::setCustomAttributes(attr, value); - else { - try { - prop->setPyObject(value); - } catch (Base::Exception &exc) { - PyErr_Format(PyExc_AttributeError, "Attribute (Name: %s) error: '%s' ", attr, exc.what()); - return -1; - } catch (...) { - PyErr_Format(PyExc_AttributeError, "Unknown error in attribute %s", attr); - return -1; - } - - return 1; - } -} diff --git a/src/App/FeaturePythonPyImp.h b/src/App/FeaturePythonPyImp.h new file mode 100644 index 000000000..9bd0e368f --- /dev/null +++ b/src/App/FeaturePythonPyImp.h @@ -0,0 +1,78 @@ +/*************************************************************************** + * Copyright (c) 2013 Werner Mayer * + * * + * 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_FEATUREPYTHONPYIMP_H +#define APP_FEATUREPYTHONPYIMP_H + +#include +#include + +namespace App +{ + +/** + * @author Werner Mayer + */ +template +class FeaturePythonPyT : public FeaturePyT +{ +public: + static PyTypeObject Type; + static PyMethodDef Methods[]; + +public: + FeaturePythonPyT(DocumentObject *pcObject, PyTypeObject *T = &Type); + virtual ~FeaturePythonPyT(); + + /** @name callbacks and implementers for the python object methods */ + //@{ + /// callback for the addProperty() method + static PyObject * staticCallback_addProperty (PyObject *self, PyObject *args); + /// implementer for the addProperty() method + PyObject* addProperty(PyObject *args); + /// callback for the removeProperty() method + static PyObject * staticCallback_removeProperty (PyObject *self, PyObject *args); + /// implementer for the removeProperty() method + PyObject* removeProperty(PyObject *args); + /// callback for the supportedProperties() method + static PyObject * staticCallback_supportedProperties (PyObject *self, PyObject *args); + /// implementer for the supportedProperties() method + PyObject* supportedProperties(PyObject *args); + //@} + + /// getter method for special attributes (e.g. dynamic ones) + PyObject *getCustomAttributes(const char* attr) const; + /// setter for special attributes (e.g. dynamic ones) + int setCustomAttributes(const char* attr, PyObject *obj); + PyObject *_getattr(char *attr); // __getattr__ function + int _setattr(char *attr, PyObject *value); // __setattr__ function + +protected: + +private: +}; + +} //namespace App + +#include "FeaturePythonPyImp.inl" + +#endif // APP_FEATUREPYTHONPYIMP_H diff --git a/src/App/FeaturePythonPyImp.inl b/src/App/FeaturePythonPyImp.inl new file mode 100644 index 000000000..78aa389d5 --- /dev/null +++ b/src/App/FeaturePythonPyImp.inl @@ -0,0 +1,379 @@ +/*************************************************************************** + * Copyright (c) 2013 Werner Mayer * + * * + * 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 * + * * + ***************************************************************************/ + + +namespace App +{ + +/// Type structure of FeaturePythonPyT +template +PyTypeObject FeaturePythonPyT::Type = { + PyObject_HEAD_INIT(&PyType_Type) + 0, /*ob_size*/ + "FeaturePython", /*tp_name*/ + sizeof(FeaturePythonPyT), /*tp_basicsize*/ + 0, /*tp_itemsize*/ + /* methods */ + FeaturePyT::PyDestructor, /*tp_dealloc*/ + 0, /*tp_print*/ + FeaturePyT::__getattr, /*tp_getattr*/ + FeaturePyT::__setattr, /*tp_setattr*/ + 0, /*tp_compare*/ + 0, /*tp_repr*/ + 0, /*tp_as_number*/ + 0, /*tp_as_sequence*/ + 0, /*tp_as_mapping*/ + 0, /*tp_hash*/ + 0, /*tp_call */ + 0, /*tp_str */ + 0, /*tp_getattro*/ + 0, /*tp_setattro*/ + /* --- Functions to access object as input/output buffer ---------*/ + 0, /* tp_as_buffer */ + /* --- Flags to define presence of optional/expanded features */ + Py_TPFLAGS_BASETYPE|Py_TPFLAGS_HAVE_CLASS, /*tp_flags */ + "This is the father of all Feature classes", /*tp_doc */ + 0, /*tp_traverse */ + 0, /*tp_clear */ + 0, /*tp_richcompare */ + 0, /*tp_weaklistoffset */ + 0, /*tp_iter */ + 0, /*tp_iternext */ + App::FeaturePythonPyT::Methods, /*tp_methods */ + 0, /*tp_members */ + 0, /*tp_getset */ + &FeaturePyT::Type, /*tp_base */ + 0, /*tp_dict */ + 0, /*tp_descr_get */ + 0, /*tp_descr_set */ + 0, /*tp_dictoffset */ + FeaturePyT::__PyInit, /*tp_init */ + 0, /*tp_alloc */ + 0, /*tp_new */ + 0, /*tp_free Low-level free-memory routine */ + 0, /*tp_is_gc For PyObject_IS_GC */ + 0, /*tp_bases */ + 0, /*tp_mro method resolution order */ + 0, /*tp_cache */ + 0, /*tp_subclasses */ + 0, /*tp_weaklist */ + 0 /*tp_del */ +}; + +/// Methods structure of FeaturePythonPyT +template +PyMethodDef FeaturePythonPyT::Methods[] = { + {"addProperty", + (PyCFunction) staticCallback_addProperty, + METH_VARARGS, + "addProperty(string, string) -- Add a generic property.\nThe first argument specifies the type, the second the\nname of the property.\n " + }, + {"removeProperty", + (PyCFunction) staticCallback_removeProperty, + METH_VARARGS, + "removeProperty(string) -- Remove a generic property.\nNote, you can only remove user-defined properties but not built-in ones.\n " + }, + {"supportedProperties", + (PyCFunction) staticCallback_supportedProperties, + METH_VARARGS, + "A list of supported property types" + }, + {NULL, NULL, 0, NULL} /* Sentinel */ +}; + +template +PyObject * FeaturePythonPyT::staticCallback_addProperty (PyObject *self, PyObject *args) +{ + // test if twin object not allready deleted + if (!static_cast(self)->isValid()){ + PyErr_SetString(PyExc_ReferenceError, "This object is already deleted most likely through closing a document. This reference is no longer valid!"); + return NULL; + } + + // test if object is set Const + if (static_cast(self)->isConst()){ + PyErr_SetString(PyExc_ReferenceError, "This object is immutable, you can not set any attribute or call a non const method"); + return NULL; + } + + try { + PyObject* ret = static_cast(self)->addProperty(args); + if (ret != 0) + static_cast(self)->startNotify(); + return ret; + } + catch(const Base::Exception& e) { + std::string str; + str += "FreeCAD exception thrown ("; + str += e.what(); + str += ")"; + e.ReportException(); + PyErr_SetString(PyExc_Exception,str.c_str()); + return NULL; + } + catch(const Py::Exception&) { + // The exception text is already set + return NULL; + } +} + +template +PyObject * FeaturePythonPyT::staticCallback_removeProperty (PyObject *self, PyObject *args) +{ + // test if twin object not allready deleted + if (!static_cast(self)->isValid()){ + PyErr_SetString(PyExc_ReferenceError, "This object is already deleted most likely through closing a document. This reference is no longer valid!"); + return NULL; + } + + // test if object is set Const + if (static_cast(self)->isConst()){ + PyErr_SetString(PyExc_ReferenceError, "This object is immutable, you can not set any attribute or call a non const method"); + return NULL; + } + + try { + PyObject* ret = static_cast(self)->removeProperty(args); + if (ret != 0) + static_cast(self)->startNotify(); + return ret; + } + catch(const Base::Exception& e) { + std::string str; + str += "FreeCAD exception thrown ("; + str += e.what(); + str += ")"; + e.ReportException(); + PyErr_SetString(PyExc_Exception,str.c_str()); + return NULL; + } + catch(const Py::Exception&) { + // The exception text is already set + return NULL; + } +} + +template +PyObject * FeaturePythonPyT::staticCallback_supportedProperties (PyObject *self, PyObject *args) +{ + // test if twin object not allready deleted + if (!static_cast(self)->isValid()){ + PyErr_SetString(PyExc_ReferenceError, "This object is already deleted most likely through closing a document. This reference is no longer valid!"); + return NULL; + } + + // test if object is set Const + if (static_cast(self)->isConst()){ + PyErr_SetString(PyExc_ReferenceError, "This object is immutable, you can not set any attribute or call a non const method"); + return NULL; + } + + try { + PyObject* ret = static_cast(self)->supportedProperties(args); + if (ret != 0) + static_cast(self)->startNotify(); + return ret; + } + catch(const Base::Exception& e) { + std::string str; + str += "FreeCAD exception thrown ("; + str += e.what(); + str += ")"; + e.ReportException(); + PyErr_SetString(PyExc_Exception,str.c_str()); + return NULL; + } + catch(const Py::Exception&) { + // The exception text is already set + return NULL; + } +} + +template +FeaturePythonPyT::FeaturePythonPyT(DocumentObject *pcObject, PyTypeObject *T) + : FeaturePyT(reinterpret_cast(pcObject), T) +{ +} + +template +FeaturePythonPyT::~FeaturePythonPyT() +{ +} + +template +PyObject *FeaturePythonPyT::_getattr(char *attr) +{ + try { + // getter method for special Attributes (e.g. dynamic ones) + PyObject *r = getCustomAttributes(attr); + if(r) return r; + } + catch(const Base::Exception& e) {// catch the FreeCAD exceptions + std::string str; + str += "FreeCAD exception thrown ("; + str += e.what(); + str += ")"; + e.ReportException(); + PyErr_SetString(PyExc_Exception,str.c_str()); + return NULL; + } + catch(const Py::Exception&) { + // The exception text is already set + return NULL; + } + + PyObject *rvalue = Py_FindMethod(Methods, this, attr); + if (rvalue == NULL) { + PyErr_Clear(); + return FeaturePyT::_getattr(attr); + } + else { + return rvalue; + } +} + +template +int FeaturePythonPyT::_setattr(char *attr, PyObject *value) +{ + try { + // setter for special Attributes (e.g. dynamic ones) + int r = setCustomAttributes(attr, value); + if(r==1) return 0; + } + catch(const Base::Exception& e) { // catch the FreeCAD exceptions + std::string str; + str += "FreeCAD exception thrown ("; + str += e.what(); + str += ")"; + e.ReportException(); + PyErr_SetString(PyExc_Exception,str.c_str()); + return -1; + } + catch(const Py::Exception&) { + // The exception text is already set + return -1; + } + + return FeaturePyT::_setattr(attr, value); +} + +// ------------------------------------------------------------- + +template +PyObject* FeaturePythonPyT::addProperty(PyObject *args) +{ + char *sType,*sName=0,*sGroup=0,*sDoc=0; + short attr=0; + PyObject *ro = Py_False, *hd = Py_False; + if (!PyArg_ParseTuple(args, "s|ssshO!O!", &sType,&sName,&sGroup,&sDoc,&attr, + &PyBool_Type, &ro, &PyBool_Type, &hd)) // convert args: Python->C + return NULL; // NULL triggers exception + + Property* prop=0; + prop = FeaturePyT::getDocumentObjectPtr()->addDynamicProperty(sType,sName,sGroup,sDoc,attr, + PyObject_IsTrue(ro) ? true : false, PyObject_IsTrue(hd) ? true : false); + + if (!prop) { + std::stringstream str; + str << "No property found of type '" << sType << "'" << std::ends; + throw Py::Exception(PyExc_Exception,str.str()); + } + + return Py::new_reference_to(this); +} + +template +PyObject* FeaturePythonPyT::removeProperty(PyObject *args) +{ + char *sName; + if (!PyArg_ParseTuple(args, "s", &sName)) + return NULL; + + bool ok = FeaturePyT::getDocumentObjectPtr()->removeDynamicProperty(sName); + return Py_BuildValue("O", (ok ? Py_True : Py_False)); +} + +template +PyObject* FeaturePythonPyT::supportedProperties(PyObject *args) +{ + if (!PyArg_ParseTuple(args, "")) // convert args: Python->C + return NULL; // NULL triggers exception + + std::vector ary; + Base::Type::getAllDerivedFrom(App::Property::getClassTypeId(), ary); + Py::List res; + for (std::vector::iterator it = ary.begin(); it != ary.end(); ++it) { + Base::BaseClass *data = static_cast(it->createInstance()); + if (data) { + delete data; + res.append(Py::String(it->getName())); + } + } + return Py::new_reference_to(res); +} + +template +PyObject *FeaturePythonPyT::getCustomAttributes(const char* attr) const +{ + PY_TRY{ + if (Base::streq(attr, "__dict__")){ + PyObject* dict = FeaturePyT::getCustomAttributes(attr); + if (dict){ + std::vector Props = FeaturePyT::getDocumentObjectPtr()->getDynamicPropertyNames(); + for (std::vector::const_iterator it = Props.begin(); it != Props.end(); ++it) + PyDict_SetItem(dict, PyString_FromString(it->c_str()), PyString_FromString("")); + } + return dict; + } + + // search for dynamic property + Property* prop = FeaturePyT::getDocumentObjectPtr()->getDynamicPropertyByName(attr); + if (prop) return prop->getPyObject(); + } PY_CATCH; + + return 0; +} + +template +int FeaturePythonPyT::setCustomAttributes(const char* attr, PyObject *value) +{ + // search for dynamic property + Property* prop = FeaturePyT::getDocumentObjectPtr()->getDynamicPropertyByName(attr); + + if (!prop) + return FeaturePyT::setCustomAttributes(attr, value); + else { + try { + prop->setPyObject(value); + } catch (Base::Exception &exc) { + PyErr_Format(PyExc_AttributeError, "Attribute (Name: %s) error: '%s' ", attr, exc.what()); + return -1; + } catch (...) { + PyErr_Format(PyExc_AttributeError, "Unknown error in attribute %s", attr); + return -1; + } + + return 1; + } +} + +} //namespace App diff --git a/src/Mod/Mesh/App/CMakeLists.txt b/src/Mod/Mesh/App/CMakeLists.txt index 97e285680..cd3aaa070 100644 --- a/src/Mod/Mesh/App/CMakeLists.txt +++ b/src/Mod/Mesh/App/CMakeLists.txt @@ -27,14 +27,12 @@ set(Mesh_LIBS generate_from_xml(FacetPy) generate_from_xml(MeshFeaturePy) -generate_from_xml(FeaturePythonPy) generate_from_xml(MeshPointPy) generate_from_xml(MeshPy) SET(Mesh_XML_SRCS FacetPy.xml MeshFeaturePy.xml - FeaturePythonPy.xml MeshPointPy.xml MeshPy.xml ) @@ -322,7 +320,6 @@ SET(Mesh_SRCS MeshFeature.cpp MeshFeature.h MeshFeaturePyImp.cpp - FeaturePythonPyImp.cpp MeshPointPyImp.cpp MeshPoint.h MeshProperties.cpp diff --git a/src/Mod/Mesh/App/FeaturePythonPy.xml b/src/Mod/Mesh/App/FeaturePythonPy.xml deleted file mode 100644 index 06c6736f5..000000000 --- a/src/Mod/Mesh/App/FeaturePythonPy.xml +++ /dev/null @@ -1,30 +0,0 @@ - - - - - - This is the father of all Feature classes - - - - addProperty(string, string) -- Add a generic property. -The first argument specifies the type, the second the -name of the property. - - - - - - A list of supported property types - - - - diff --git a/src/Mod/Mesh/App/FeaturePythonPyImp.cpp b/src/Mod/Mesh/App/FeaturePythonPyImp.cpp deleted file mode 100644 index 2e985e2bb..000000000 --- a/src/Mod/Mesh/App/FeaturePythonPyImp.cpp +++ /dev/null @@ -1,123 +0,0 @@ -/*************************************************************************** - * Copyright (c) 2010 Werner Mayer * - * * - * 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 * - * * - ***************************************************************************/ - - -#include "PreCompiled.h" - -#ifndef _PreComp_ -# include -#endif - -// inclusion of the generated files (generated out of FeaturePythonPy.xml) -#include "FeaturePythonPy.h" -#include "FeaturePythonPy.cpp" - -using namespace Mesh; - -// returns a string which represent the object e.g. when printed in python -std::string FeaturePythonPy::representation(void) const -{ - return std::string(""); -} - -PyObject* FeaturePythonPy::addProperty(PyObject *args) -{ - char *sType,*sName=0,*sGroup=0,*sDoc=0; - short attr=0; - PyObject *ro = Py_False, *hd = Py_False; - if (!PyArg_ParseTuple(args, "s|ssshO!O!", &sType,&sName,&sGroup,&sDoc,&attr, - &PyBool_Type, &ro, &PyBool_Type, &hd)) // convert args: Python->C - return NULL; // NULL triggers exception - - App::Property* prop=0; - prop = getFeaturePtr()->addDynamicProperty(sType,sName,sGroup,sDoc,attr, - PyObject_IsTrue(ro) ? true : false, PyObject_IsTrue(hd) ? true : false); - - if (!prop) { - std::stringstream str; - str << "No property found of type '" << sType << "'" << std::ends; - throw Py::Exception(PyExc_Exception,str.str()); - } - - return Py::new_reference_to(this); -} - -PyObject* FeaturePythonPy::supportedProperties(PyObject *args) -{ - if (!PyArg_ParseTuple(args, "")) // convert args: Python->C - return NULL; // NULL triggers exception - - std::vector ary; - Base::Type::getAllDerivedFrom(App::Property::getClassTypeId(), ary); - Py::List res; - for (std::vector::iterator it = ary.begin(); it != ary.end(); ++it) { - Base::BaseClass *data = static_cast(it->createInstance()); - if (data) { - delete data; - res.append(Py::String(it->getName())); - } - } - return Py::new_reference_to(res); -} - -PyObject *FeaturePythonPy::getCustomAttributes(const char* attr) const -{ - PY_TRY{ - if (Base::streq(attr, "__dict__")){ - PyObject* dict = DocumentObjectPy::getCustomAttributes(attr); - if (dict){ - std::vector Props = getFeaturePtr()->getDynamicPropertyNames(); - for (std::vector::const_iterator it = Props.begin(); it != Props.end(); ++it) - PyDict_SetItem(dict, PyString_FromString(it->c_str()), PyString_FromString("")); - } - return dict; - } - - // search for dynamic property - App::Property* prop = getFeaturePtr()->getDynamicPropertyByName(attr); - if (prop) return prop->getPyObject(); - } PY_CATCH; - - return 0; -} - -int FeaturePythonPy::setCustomAttributes(const char* attr, PyObject *value) -{ - // search for dynamic property - App::Property* prop = getFeaturePtr()->getDynamicPropertyByName(attr); - - if (!prop) - return DocumentObjectPy::setCustomAttributes(attr, value); - else { - try { - prop->setPyObject(value); - } catch (Base::Exception &exc) { - PyErr_Format(PyExc_AttributeError, "Attribute (Name: %s) error: '%s' ", attr, exc.what()); - return -1; - } catch (...) { - PyErr_Format(PyExc_AttributeError, "Unknown error in attribute %s", attr); - return -1; - } - - return 1; - } -} diff --git a/src/Mod/Mesh/App/MeshFeature.cpp b/src/Mod/Mesh/App/MeshFeature.cpp index e98c28794..0604812a9 100644 --- a/src/Mod/Mesh/App/MeshFeature.cpp +++ b/src/Mod/Mesh/App/MeshFeature.cpp @@ -31,12 +31,12 @@ #include #include #include +#include #include "Core/MeshIO.h" #include "MeshFeature.h" #include "MeshFeaturePy.h" -#include "FeaturePythonPy.h" using namespace Mesh; @@ -105,7 +105,7 @@ template<> const char* Mesh::FeaturePython::getViewProviderName(void) const { template<> PyObject* Mesh::FeaturePython::getPyObject(void) { if (PythonObject.is(Py::_None())) { // ref counter is set to 1 - PythonObject = Py::Object(new Mesh::FeaturePythonPy(this),true); + PythonObject = Py::Object(new FeaturePythonPyT(this),true); } return Py::new_reference_to(PythonObject); } diff --git a/src/Mod/Part/App/CMakeLists.txt b/src/Mod/Part/App/CMakeLists.txt index 079a52a3e..9a62fb6fa 100644 --- a/src/Mod/Part/App/CMakeLists.txt +++ b/src/Mod/Part/App/CMakeLists.txt @@ -49,7 +49,6 @@ generate_from_xml(RectangularTrimmedSurfacePy) generate_from_xml(SurfaceOfExtrusionPy) generate_from_xml(SurfaceOfRevolutionPy) generate_from_xml(PartFeaturePy) -generate_from_xml(FeaturePythonPy) generate_from_xml(Part2DObjectPy) generate_from_xml(OffsetSurfacePy) generate_from_xml(OffsetSurfacePy) @@ -185,8 +184,6 @@ SET(Python_SRCS SurfaceOfRevolutionPyImp.cpp PartFeaturePy.xml PartFeaturePyImp.cpp - FeaturePythonPy.xml - FeaturePythonPyImp.cpp Part2DObjectPy.xml Part2DObjectPyImp.cpp TopoShapePy.xml diff --git a/src/Mod/Part/App/FeaturePythonPy.xml b/src/Mod/Part/App/FeaturePythonPy.xml deleted file mode 100644 index 992c320d9..000000000 --- a/src/Mod/Part/App/FeaturePythonPy.xml +++ /dev/null @@ -1,30 +0,0 @@ - - - - - - This is the father of all Feature classes - - - - addProperty(string, string) -- Add a generic property. -The first argument specifies the type, the second the -name of the property. - - - - - - A list of supported property types - - - - diff --git a/src/Mod/Part/App/FeaturePythonPyImp.cpp b/src/Mod/Part/App/FeaturePythonPyImp.cpp deleted file mode 100644 index 2a2f76cd6..000000000 --- a/src/Mod/Part/App/FeaturePythonPyImp.cpp +++ /dev/null @@ -1,123 +0,0 @@ -/*************************************************************************** - * Copyright (c) 2007 Werner Mayer * - * * - * 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 * - * * - ***************************************************************************/ - - -#include "PreCompiled.h" - -#include "Mod/Part/App/PartFeature.h" - -// inclusion of the generated files (generated out of FeaturePythonPy.xml) -#include "FeaturePythonPy.h" -#include "FeaturePythonPy.cpp" - -using namespace Part; - -// returns a string which represents the object e.g. when printed in python -std::string FeaturePythonPy::representation(void) const -{ - return std::string(""); -} - -PyObject* FeaturePythonPy::addProperty(PyObject *args) -{ - char *sType,*sName=0,*sGroup=0,*sDoc=0; - short attr=0; - PyObject *ro = Py_False, *hd = Py_False; - if (!PyArg_ParseTuple(args, "s|ssshO!O!", &sType,&sName,&sGroup,&sDoc,&attr, - &PyBool_Type, &ro, &PyBool_Type, &hd)) // convert args: Python->C - return NULL; // NULL triggers exception - - App::Property* prop=0; - prop = getFeaturePythonPtr()->addDynamicProperty(sType,sName,sGroup,sDoc,attr, - PyObject_IsTrue(ro) ? true : false, PyObject_IsTrue(hd) ? true : false); - - if (!prop) { - std::stringstream str; - str << "No property found of type '" << sType << "'" << std::ends; - throw Py::Exception(PyExc_Exception,str.str()); - } - - return Py::new_reference_to(this); -} - -PyObject* FeaturePythonPy::supportedProperties(PyObject *args) -{ - if (!PyArg_ParseTuple(args, "")) // convert args: Python->C - return NULL; // NULL triggers exception - - std::vector ary; - Base::Type::getAllDerivedFrom(App::Property::getClassTypeId(), ary); - Py::List res; - for (std::vector::iterator it = ary.begin(); it != ary.end(); ++it) { - Base::BaseClass *data = static_cast(it->createInstance()); - if (data) { - delete data; - res.append(Py::String(it->getName())); - } - } - return Py::new_reference_to(res); -} - -PyObject *FeaturePythonPy::getCustomAttributes(const char* attr) const -{ - PY_TRY{ - if (Base::streq(attr, "__dict__")){ - PyObject* dict = PartFeaturePy::getCustomAttributes(attr); - if (dict){ - std::vector Props = getFeaturePythonPtr()->getDynamicPropertyNames(); - for (std::vector::const_iterator it = Props.begin(); it != Props.end(); ++it) - PyDict_SetItem(dict, PyString_FromString(it->c_str()), PyString_FromString("")); - } - return dict; - } - - // search for dynamic property - App::Property* prop = getFeaturePythonPtr()->getDynamicPropertyByName(attr); - if (prop) return prop->getPyObject(); - } PY_CATCH; - - return 0; -} - -int FeaturePythonPy::setCustomAttributes(const char* attr, PyObject *value) -{ - // search for dynamic property - App::Property* prop = getFeaturePythonPtr()->getDynamicPropertyByName(attr); - - if (!prop) - return PartFeaturePy::setCustomAttributes(attr, value); - else { - try { - prop->setPyObject(value); - } - catch (Base::Exception &exc) { - PyErr_Format(PyExc_AttributeError, "Attribute (Name: %s) error: '%s' ", attr, exc.what()); - return -1; - } - catch (...) { - PyErr_Format(PyExc_AttributeError, "Unknown error in attribute %s", attr); - return -1; - } - - return 1; - } -} diff --git a/src/Mod/Part/App/Part2DObject.cpp b/src/Mod/Part/App/Part2DObject.cpp index 9d6d324e3..4afbc3bad 100644 --- a/src/Mod/Part/App/Part2DObject.cpp +++ b/src/Mod/Part/App/Part2DObject.cpp @@ -44,7 +44,7 @@ #endif - +#include #include "Part2DObject.h" #include "Geometry.h" diff --git a/src/Mod/Part/App/PartFeature.cpp b/src/Mod/Part/App/PartFeature.cpp index 7c133ba79..eab7cd915 100644 --- a/src/Mod/Part/App/PartFeature.cpp +++ b/src/Mod/Part/App/PartFeature.cpp @@ -50,10 +50,10 @@ #include #include #include +#include #include "PartFeature.h" #include "PartFeaturePy.h" -#include "FeaturePythonPy.h" using namespace Part; @@ -274,13 +274,12 @@ PROPERTY_SOURCE_TEMPLATE(Part::FeaturePython, Part::Feature) template<> const char* Part::FeaturePython::getViewProviderName(void) const { return "PartGui::ViewProviderPython"; } - template<> PyObject* Part::FeaturePython::getPyObject(void) { - if (PythonObject.is(Py::_None())){ + if (PythonObject.is(Py::_None())) { // ref counter is set to 1 - PythonObject = Py::Object(new Part::FeaturePythonPy(this),true); + PythonObject = Py::Object(new FeaturePythonPyT(this),true); } - return Py::new_reference_to(PythonObject); + return Py::new_reference_to(PythonObject); } /// @endcond diff --git a/src/Mod/PartDesign/App/Feature.cpp b/src/Mod/PartDesign/App/Feature.cpp index aa4cd3267..9fa351f04 100644 --- a/src/Mod/PartDesign/App/Feature.cpp +++ b/src/Mod/PartDesign/App/Feature.cpp @@ -32,6 +32,7 @@ #endif +#include #include "Feature.h" diff --git a/src/Mod/PartDesign/App/FeatureDraft.cpp b/src/Mod/PartDesign/App/FeatureDraft.cpp index 422c1a827..b2684efd3 100644 --- a/src/Mod/PartDesign/App/FeatureDraft.cpp +++ b/src/Mod/PartDesign/App/FeatureDraft.cpp @@ -53,9 +53,10 @@ #include "FeatureDraft.h" -using namespace PartDesign; - #include +#include + +using namespace PartDesign; PROPERTY_SOURCE(PartDesign::Draft, PartDesign::DressUp) diff --git a/src/Mod/PartDesign/App/FeatureGroove.cpp b/src/Mod/PartDesign/App/FeatureGroove.cpp index ea2d5391b..e3344e70a 100644 --- a/src/Mod/PartDesign/App/FeatureGroove.cpp +++ b/src/Mod/PartDesign/App/FeatureGroove.cpp @@ -39,6 +39,7 @@ #endif #include +#include #include #include diff --git a/src/Mod/PartDesign/App/FeatureLinearPattern.cpp b/src/Mod/PartDesign/App/FeatureLinearPattern.cpp index 09f98141b..bcb8173f4 100644 --- a/src/Mod/PartDesign/App/FeatureLinearPattern.cpp +++ b/src/Mod/PartDesign/App/FeatureLinearPattern.cpp @@ -35,6 +35,7 @@ #include "FeatureLinearPattern.h" #include +#include #include #include diff --git a/src/Mod/PartDesign/App/FeatureMirrored.cpp b/src/Mod/PartDesign/App/FeatureMirrored.cpp index ddf655c71..0206134bf 100644 --- a/src/Mod/PartDesign/App/FeatureMirrored.cpp +++ b/src/Mod/PartDesign/App/FeatureMirrored.cpp @@ -31,6 +31,7 @@ #endif +#include #include "FeatureMirrored.h" #include #include diff --git a/src/Mod/PartDesign/App/FeatureMultiTransform.cpp b/src/Mod/PartDesign/App/FeatureMultiTransform.cpp index fcb10733c..a6dc28a5e 100644 --- a/src/Mod/PartDesign/App/FeatureMultiTransform.cpp +++ b/src/Mod/PartDesign/App/FeatureMultiTransform.cpp @@ -35,6 +35,7 @@ #include #include +#include using namespace PartDesign; diff --git a/src/Mod/PartDesign/App/FeaturePad.cpp b/src/Mod/PartDesign/App/FeaturePad.cpp index f2f99784e..d50551519 100644 --- a/src/Mod/PartDesign/App/FeaturePad.cpp +++ b/src/Mod/PartDesign/App/FeaturePad.cpp @@ -40,6 +40,7 @@ # include #endif +#include #include #include diff --git a/src/Mod/PartDesign/App/FeaturePocket.cpp b/src/Mod/PartDesign/App/FeaturePocket.cpp index ea9a05df6..2570edddb 100644 --- a/src/Mod/PartDesign/App/FeaturePocket.cpp +++ b/src/Mod/PartDesign/App/FeaturePocket.cpp @@ -43,6 +43,7 @@ # include #endif +#include #include #include diff --git a/src/Mod/PartDesign/App/FeaturePolarPattern.cpp b/src/Mod/PartDesign/App/FeaturePolarPattern.cpp index b4346954c..d9d49a046 100644 --- a/src/Mod/PartDesign/App/FeaturePolarPattern.cpp +++ b/src/Mod/PartDesign/App/FeaturePolarPattern.cpp @@ -33,6 +33,7 @@ #include "FeaturePolarPattern.h" #include +#include #include #include #include diff --git a/src/Mod/PartDesign/App/FeatureRevolution.cpp b/src/Mod/PartDesign/App/FeatureRevolution.cpp index 790f21cf8..afb3667f7 100644 --- a/src/Mod/PartDesign/App/FeatureRevolution.cpp +++ b/src/Mod/PartDesign/App/FeatureRevolution.cpp @@ -39,6 +39,7 @@ #endif #include +#include #include #include diff --git a/src/Mod/PartDesign/App/FeatureScaled.cpp b/src/Mod/PartDesign/App/FeatureScaled.cpp index 0473f0394..d1b3aee40 100644 --- a/src/Mod/PartDesign/App/FeatureScaled.cpp +++ b/src/Mod/PartDesign/App/FeatureScaled.cpp @@ -33,9 +33,10 @@ #include "FeatureSubtractive.h" #include -using namespace PartDesign; - #include +#include + +using namespace PartDesign; namespace PartDesign { diff --git a/src/Mod/PartDesign/App/FeatureSketchBased.cpp b/src/Mod/PartDesign/App/FeatureSketchBased.cpp index 4f75e06d5..c5c856761 100644 --- a/src/Mod/PartDesign/App/FeatureSketchBased.cpp +++ b/src/Mod/PartDesign/App/FeatureSketchBased.cpp @@ -64,6 +64,7 @@ #include #include +#include #include "FeatureSketchBased.h" using namespace PartDesign; diff --git a/src/Mod/PartDesign/App/FeatureTransformed.cpp b/src/Mod/PartDesign/App/FeatureTransformed.cpp index 74399db01..b7ecf6638 100644 --- a/src/Mod/PartDesign/App/FeatureTransformed.cpp +++ b/src/Mod/PartDesign/App/FeatureTransformed.cpp @@ -42,6 +42,7 @@ #include "FeatureMirrored.h" #include +#include using namespace PartDesign;