App/FeaturePythonPyImp.cpp

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

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