Make FeaturePython Python binding a template class to avoid code duplication

This commit is contained in:
wmayer 2013-03-22 00:34:18 +01:00
parent 9da66cbb02
commit ce0e6e5cef
30 changed files with 508 additions and 506 deletions

View File

@ -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

View File

@ -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<App::DocumentObjectGroupPy>(this),true);
}
return Py::new_reference_to(PythonObject);
}
/// @endcond
// explicit template instantiation

View File

@ -32,7 +32,7 @@
#include <Base/Reader.h>
#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<DocumentObjectPy>(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<DocumentObjectPy>(this),true);
}
return Py::new_reference_to(PythonObject);
}
// explicit template instantiation
template class AppExport FeaturePythonT<DocumentObject>;
}

View File

@ -31,13 +31,11 @@
#include <App/DynamicProperty.h>
#include <App/PropertyPythonObject.h>
#include <App/PropertyGeo.h>
#include <App/FeaturePythonPy.h>
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);

View File

@ -1,37 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<GenerateModel xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="generateMetaModel_Module.xsd">
<PythonExport
Father="DocumentObjectPy"
Name="FeaturePythonPy"
Twin="FeaturePython"
TwinPointer="DocumentObject"
Include="App/FeaturePython.h"
Namespace="App"
FatherInclude="App/DocumentObjectPy.h"
FatherNamespace="App">
<Documentation>
<Author Licence="LGPL" Name="Werner Mayer" EMail="wmayer@users.sourceforge.net" />
<UserDocu>This is the father of all Feature classes</UserDocu>
</Documentation>
<Methode Name="addProperty">
<Documentation>
<UserDocu>addProperty(string, string) -- Add a generic property.
The first argument specifies the type, the second the
name of the property.
</UserDocu>
</Documentation>
</Methode>
<Methode Name="removeProperty">
<Documentation>
<UserDocu>removeProperty(string) -- Remove a generic property.
Note, you can only remove user-defined properties but not built-in ones.
</UserDocu>
</Documentation>
</Methode>
<Methode Name="supportedProperties">
<Documentation>
<UserDocu>A list of supported property types</UserDocu>
</Documentation>
</Methode>
</PythonExport>
</GenerateModel>

View File

@ -1,135 +0,0 @@
/***************************************************************************
* Copyright (c) 2007 Werner Mayer <wmayer[at]users.sourceforge.net> *
* *
* 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 <sstream>
#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("<Python feature object>");
}
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<Base::Type> ary;
Base::Type::getAllDerivedFrom(App::Property::getClassTypeId(), ary);
Py::List res;
for (std::vector<Base::Type>::iterator it = ary.begin(); it != ary.end(); ++it) {
Base::BaseClass *data = static_cast<Base::BaseClass*>(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<std::string> Props = getFeaturePythonPtr()->getDynamicPropertyNames();
for (std::vector<std::string>::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;
}
}

View File

@ -0,0 +1,78 @@
/***************************************************************************
* Copyright (c) 2013 Werner Mayer <wmayer[at]users.sourceforge.net> *
* *
* 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 <Base/Console.h>
#include <App/DocumentObjectPy.h>
namespace App
{
/**
* @author Werner Mayer
*/
template <class FeaturePyT>
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

View File

@ -0,0 +1,379 @@
/***************************************************************************
* Copyright (c) 2013 Werner Mayer <wmayer[at]users.sourceforge.net> *
* *
* 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<class FeaturePyT>
PyTypeObject FeaturePythonPyT<FeaturePyT>::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<FeaturePyT>::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<class FeaturePyT>
PyMethodDef FeaturePythonPyT<FeaturePyT>::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<class FeaturePyT>
PyObject * FeaturePythonPyT<FeaturePyT>::staticCallback_addProperty (PyObject *self, PyObject *args)
{
// test if twin object not allready deleted
if (!static_cast<Base::PyObjectBase*>(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<Base::PyObjectBase*>(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<FeaturePythonPyT*>(self)->addProperty(args);
if (ret != 0)
static_cast<FeaturePythonPyT*>(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<class FeaturePyT>
PyObject * FeaturePythonPyT<FeaturePyT>::staticCallback_removeProperty (PyObject *self, PyObject *args)
{
// test if twin object not allready deleted
if (!static_cast<Base::PyObjectBase*>(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<Base::PyObjectBase*>(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<FeaturePythonPyT*>(self)->removeProperty(args);
if (ret != 0)
static_cast<FeaturePythonPyT*>(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<class FeaturePyT>
PyObject * FeaturePythonPyT<FeaturePyT>::staticCallback_supportedProperties (PyObject *self, PyObject *args)
{
// test if twin object not allready deleted
if (!static_cast<Base::PyObjectBase*>(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<Base::PyObjectBase*>(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<FeaturePythonPyT*>(self)->supportedProperties(args);
if (ret != 0)
static_cast<FeaturePythonPyT*>(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<class FeaturePyT>
FeaturePythonPyT<FeaturePyT>::FeaturePythonPyT(DocumentObject *pcObject, PyTypeObject *T)
: FeaturePyT(reinterpret_cast<typename FeaturePyT::PointerType>(pcObject), T)
{
}
template<class FeaturePyT>
FeaturePythonPyT<FeaturePyT>::~FeaturePythonPyT()
{
}
template<class FeaturePyT>
PyObject *FeaturePythonPyT<FeaturePyT>::_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<class FeaturePyT>
int FeaturePythonPyT<FeaturePyT>::_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<class FeaturePyT>
PyObject* FeaturePythonPyT<FeaturePyT>::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<class FeaturePyT>
PyObject* FeaturePythonPyT<FeaturePyT>::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<class FeaturePyT>
PyObject* FeaturePythonPyT<FeaturePyT>::supportedProperties(PyObject *args)
{
if (!PyArg_ParseTuple(args, "")) // convert args: Python->C
return NULL; // NULL triggers exception
std::vector<Base::Type> ary;
Base::Type::getAllDerivedFrom(App::Property::getClassTypeId(), ary);
Py::List res;
for (std::vector<Base::Type>::iterator it = ary.begin(); it != ary.end(); ++it) {
Base::BaseClass *data = static_cast<Base::BaseClass*>(it->createInstance());
if (data) {
delete data;
res.append(Py::String(it->getName()));
}
}
return Py::new_reference_to(res);
}
template<class FeaturePyT>
PyObject *FeaturePythonPyT<FeaturePyT>::getCustomAttributes(const char* attr) const
{
PY_TRY{
if (Base::streq(attr, "__dict__")){
PyObject* dict = FeaturePyT::getCustomAttributes(attr);
if (dict){
std::vector<std::string> Props = FeaturePyT::getDocumentObjectPtr()->getDynamicPropertyNames();
for (std::vector<std::string>::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<class FeaturePyT>
int FeaturePythonPyT<FeaturePyT>::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

View File

@ -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

View File

@ -1,30 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<GenerateModel xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="generateMetaModel_Module.xsd">
<PythonExport
Father="MeshFeaturePy"
Name="FeaturePythonPy"
Twin="Feature"
TwinPointer="Feature"
Include="Mod/Mesh/App/MeshFeature.h"
Namespace="Mesh"
FatherInclude="Mod/Mesh/App/MeshFeaturePy.h"
FatherNamespace="Mesh">
<Documentation>
<Author Licence="LGPL" Name="Werner Mayer" EMail="wmayer@users.sourceforge.net" />
<UserDocu>This is the father of all Feature classes</UserDocu>
</Documentation>
<Methode Name="addProperty">
<Documentation>
<UserDocu>addProperty(string, string) -- Add a generic property.
The first argument specifies the type, the second the
name of the property.
</UserDocu>
</Documentation>
</Methode>
<Methode Name="supportedProperties">
<Documentation>
<UserDocu>A list of supported property types</UserDocu>
</Documentation>
</Methode>
</PythonExport>
</GenerateModel>

View File

@ -1,123 +0,0 @@
/***************************************************************************
* Copyright (c) 2010 Werner Mayer <wmayer[at]users.sourceforge.net> *
* *
* 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 <sstream>
#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("<Python feature object>");
}
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<Base::Type> ary;
Base::Type::getAllDerivedFrom(App::Property::getClassTypeId(), ary);
Py::List res;
for (std::vector<Base::Type>::iterator it = ary.begin(); it != ary.end(); ++it) {
Base::BaseClass *data = static_cast<Base::BaseClass*>(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<std::string> Props = getFeaturePtr()->getDynamicPropertyNames();
for (std::vector<std::string>::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;
}
}

View File

@ -31,12 +31,12 @@
#include <Base/Exception.h>
#include <Base/Reader.h>
#include <Base/Writer.h>
#include <App/FeaturePythonPyImp.h>
#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<Mesh::MeshFeaturePy>(this),true);
}
return Py::new_reference_to(PythonObject);
}

View File

@ -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

View File

@ -1,30 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<GenerateModel xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="generateMetaModel_Module.xsd">
<PythonExport
Father="PartFeaturePy"
Name="FeaturePythonPy"
Twin="FeaturePython"
TwinPointer="FeaturePython"
Include="Mod/Part/App/PartFeature.h"
Namespace="Part"
FatherInclude="Mod/Part/App/PartFeaturePy.h"
FatherNamespace="Part">
<Documentation>
<Author Licence="LGPL" Name="Werner Mayer" EMail="wmayer@users.sourceforge.net" />
<UserDocu>This is the father of all Feature classes</UserDocu>
</Documentation>
<Methode Name="addProperty">
<Documentation>
<UserDocu>addProperty(string, string) -- Add a generic property.
The first argument specifies the type, the second the
name of the property.
</UserDocu>
</Documentation>
</Methode>
<Methode Name="supportedProperties">
<Documentation>
<UserDocu>A list of supported property types</UserDocu>
</Documentation>
</Methode>
</PythonExport>
</GenerateModel>

View File

@ -1,123 +0,0 @@
/***************************************************************************
* Copyright (c) 2007 Werner Mayer <wmayer[at]users.sourceforge.net> *
* *
* 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("<FeaturePython object>");
}
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<Base::Type> ary;
Base::Type::getAllDerivedFrom(App::Property::getClassTypeId(), ary);
Py::List res;
for (std::vector<Base::Type>::iterator it = ary.begin(); it != ary.end(); ++it) {
Base::BaseClass *data = static_cast<Base::BaseClass*>(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<std::string> Props = getFeaturePythonPtr()->getDynamicPropertyNames();
for (std::vector<std::string>::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;
}
}

View File

@ -44,7 +44,7 @@
#endif
#include <Base/Exception.h>
#include "Part2DObject.h"
#include "Geometry.h"

View File

@ -50,10 +50,10 @@
#include <Base/Stream.h>
#include <Base/Placement.h>
#include <Base/Rotation.h>
#include <App/FeaturePythonPyImp.h>
#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<Part::PartFeaturePy>(this),true);
}
return Py::new_reference_to(PythonObject);
return Py::new_reference_to(PythonObject);
}
/// @endcond

View File

@ -32,6 +32,7 @@
#endif
#include <Base/Exception.h>
#include "Feature.h"

View File

@ -53,9 +53,10 @@
#include "FeatureDraft.h"
using namespace PartDesign;
#include <Base/Console.h>
#include <Base/Exception.h>
using namespace PartDesign;
PROPERTY_SOURCE(PartDesign::Draft, PartDesign::DressUp)

View File

@ -39,6 +39,7 @@
#endif
#include <Base/Axis.h>
#include <Base/Exception.h>
#include <Base/Placement.h>
#include <Base/Tools.h>

View File

@ -35,6 +35,7 @@
#include "FeatureLinearPattern.h"
#include <Base/Axis.h>
#include <Base/Exception.h>
#include <Mod/Part/App/TopoShape.h>
#include <Mod/Part/App/Part2DObject.h>

View File

@ -31,6 +31,7 @@
#endif
#include <Base/Exception.h>
#include "FeatureMirrored.h"
#include <Mod/Part/App/TopoShape.h>
#include <Mod/Part/App/Part2DObject.h>

View File

@ -35,6 +35,7 @@
#include <Mod/Part/App/TopoShape.h>
#include <Base/Console.h>
#include <Base/Exception.h>
using namespace PartDesign;

View File

@ -40,6 +40,7 @@
# include <BRepAlgoAPI_Common.hxx>
#endif
#include <Base/Exception.h>
#include <Base/Placement.h>
#include <App/Document.h>

View File

@ -43,6 +43,7 @@
# include <BRepAlgoAPI_Common.hxx>
#endif
#include <Base/Exception.h>
#include <Base/Placement.h>
#include <App/Document.h>

View File

@ -33,6 +33,7 @@
#include "FeaturePolarPattern.h"
#include <Base/Axis.h>
#include <Base/Exception.h>
#include <Base/Tools.h>
#include <Mod/Part/App/TopoShape.h>
#include <Mod/Part/App/Part2DObject.h>

View File

@ -39,6 +39,7 @@
#endif
#include <Base/Axis.h>
#include <Base/Exception.h>
#include <Base/Placement.h>
#include <Base/Tools.h>

View File

@ -33,9 +33,10 @@
#include "FeatureSubtractive.h"
#include <Mod/Part/App/TopoShape.h>
using namespace PartDesign;
#include <Base/Console.h>
#include <Base/Exception.h>
using namespace PartDesign;
namespace PartDesign {

View File

@ -64,6 +64,7 @@
#include <TopExp.hxx>
#include <TopTools_IndexedDataMapOfShapeListOfShape.hxx>
#include <Base/Exception.h>
#include "FeatureSketchBased.h"
using namespace PartDesign;

View File

@ -42,6 +42,7 @@
#include "FeatureMirrored.h"
#include <Base/Console.h>
#include <Base/Exception.h>
using namespace PartDesign;