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