Mod/Mesh/App/FeaturePythonPyImp.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
00026 #ifndef _PreComp_
00027 # include <sstream>
00028 #endif
00029
00030
00031 #include "FeaturePythonPy.h"
00032 #include "FeaturePythonPy.cpp"
00033
00034 using namespace Mesh;
00035
00036
00037 std::string FeaturePythonPy::representation(void) const
00038 {
00039 return std::string("<Python feature object>");
00040 }
00041
00042 PyObject* FeaturePythonPy::addProperty(PyObject *args)
00043 {
00044 char *sType,*sName=0,*sGroup=0,*sDoc=0;
00045 short attr=0;
00046 PyObject *ro = Py_False, *hd = Py_False;
00047 if (!PyArg_ParseTuple(args, "s|ssshO!O!", &sType,&sName,&sGroup,&sDoc,&attr,
00048 &PyBool_Type, &ro, &PyBool_Type, &hd))
00049 return NULL;
00050
00051 App::Property* prop=0;
00052 prop = getFeaturePtr()->addDynamicProperty(sType,sName,sGroup,sDoc,attr,ro==Py_True,hd==Py_True);
00053
00054 if (!prop) {
00055 std::stringstream str;
00056 str << "No property found of type '" << sType << "'" << std::ends;
00057 throw Py::Exception(PyExc_Exception,str.str());
00058 }
00059
00060 return Py::new_reference_to(this);
00061 }
00062
00063 PyObject* FeaturePythonPy::supportedProperties(PyObject *args)
00064 {
00065 if (!PyArg_ParseTuple(args, ""))
00066 return NULL;
00067
00068 std::vector<Base::Type> ary;
00069 Base::Type::getAllDerivedFrom(App::Property::getClassTypeId(), ary);
00070 Py::List res;
00071 for (std::vector<Base::Type>::iterator it = ary.begin(); it != ary.end(); ++it) {
00072 Base::BaseClass *data = static_cast<Base::BaseClass*>(it->createInstance());
00073 if (data) {
00074 delete data;
00075 res.append(Py::String(it->getName()));
00076 }
00077 }
00078 return Py::new_reference_to(res);
00079 }
00080
00081 PyObject *FeaturePythonPy::getCustomAttributes(const char* attr) const
00082 {
00083 PY_TRY{
00084 if (Base::streq(attr, "__dict__")){
00085 PyObject* dict = DocumentObjectPy::getCustomAttributes(attr);
00086 if (dict){
00087 std::vector<std::string> Props = getFeaturePtr()->getDynamicPropertyNames();
00088 for (std::vector<std::string>::const_iterator it = Props.begin(); it != Props.end(); ++it)
00089 PyDict_SetItem(dict, PyString_FromString(it->c_str()), PyString_FromString(""));
00090 }
00091 return dict;
00092 }
00093
00094
00095 App::Property* prop = getFeaturePtr()->getDynamicPropertyByName(attr);
00096 if (prop) return prop->getPyObject();
00097 } PY_CATCH;
00098
00099 return 0;
00100 }
00101
00102 int FeaturePythonPy::setCustomAttributes(const char* attr, PyObject *value)
00103 {
00104
00105 App::Property* prop = getFeaturePtr()->getDynamicPropertyByName(attr);
00106
00107 if (!prop)
00108 return DocumentObjectPy::setCustomAttributes(attr, value);
00109 else {
00110 try {
00111 prop->setPyObject(value);
00112 } catch (Base::Exception &exc) {
00113 PyErr_Format(PyExc_AttributeError, "Attribute (Name: %s) error: '%s' ", attr, exc.what());
00114 return -1;
00115 } catch (...) {
00116 PyErr_Format(PyExc_AttributeError, "Unknown error in attribute %s", attr);
00117 return -1;
00118 }
00119
00120 return 1;
00121 }
00122 }