1
/***************************************************************************
2
* Copyright (c) J�rgen Riegel (juergen.riegel@web.de) 2007 *
4
* This file is part of the FreeCAD CAx development system. *
6
* This library is free software; you can redistribute it and/or *
7
* modify it under the terms of the GNU Library General Public *
8
* License as published by the Free Software Foundation; either *
9
* version 2 of the License, or (at your option) any later version. *
11
* This library is distributed in the hope that it will be useful, *
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14
* GNU Library General Public License for more details. *
16
* You should have received a copy of the GNU Library General Public *
17
* License along with this library; see the file COPYING.LIB. If not, *
18
* write to the Free Software Foundation, Inc., 59 Temple Place, *
19
* Suite 330, Boston, MA 02111-1307, USA *
21
***************************************************************************/
24
#include "PreCompiled.h"
30
#include "PropertyContainer.h"
33
// inclution of the generated files (generated out of PropertyContainerPy.xml)
34
#include "PropertyContainerPy.h"
35
#include "PropertyContainerPy.cpp"
39
// returns a string which represent the object e.g. when printed in python
40
const char *PropertyContainerPy::representation(void) const
42
return "<property container>";
45
PyObject* PropertyContainerPy::getPropertyByName(PyObject *args)
48
if (!PyArg_ParseTuple(args, "s", &pstr)) // convert args: Python->C
49
return NULL; // NULL triggers exception
50
App::Property* prop = getPropertyContainerPtr()->getPropertyByName(pstr);
52
return prop->getPyObject();
55
PyErr_Format(PyExc_AttributeError, "Property container has no property '%s'", pstr);
60
PyObject* PropertyContainerPy::getTypeOfProperty(PyObject *args)
64
if (!PyArg_ParseTuple(args, "s", &pstr)) // convert args: Python->C
65
return NULL; // NULL triggers exception
67
short Type = getPropertyContainerPtr()->getPropertyType(pstr);
69
if (Type & Prop_Hidden)
70
ret.append(Py::String("Hidden"));
71
if (Type & Prop_ReadOnly)
72
ret.append(Py::String("ReadOnly"));
73
if (Type & Prop_Output)
74
ret.append(Py::String("Output"));
75
if (Type & Prop_Transient)
76
ret.append(Py::String("Transient"));
78
return Py::new_reference_to(ret);
81
PyObject* PropertyContainerPy::getGroupOfProperty(PyObject *args)
84
if (!PyArg_ParseTuple(args, "s", &pstr)) // convert args: Python->C
85
return NULL; // NULL triggers exception
87
const char* Group = getPropertyContainerPtr()->getPropertyGroup(pstr);
89
return Py::new_reference_to(Py::String(Group));
91
return Py::new_reference_to(Py::String(""));
94
PyObject* PropertyContainerPy::getDocumentationOfProperty(PyObject *args)
97
if (!PyArg_ParseTuple(args, "s", &pstr)) // convert args: Python->C
98
return NULL; // NULL triggers exception
100
const char* Group = getPropertyContainerPtr()->getPropertyDocumentation(pstr);
102
return Py::new_reference_to(Py::String(Group));
104
return Py::new_reference_to(Py::String(""));
107
Py::List PropertyContainerPy::getPropertiesList(void) const
110
std::map<std::string,Property*> Map;
112
getPropertyContainerPtr()->getPropertyMap(Map);
114
for (std::map<std::string,Property*>::const_iterator It=Map.begin();It!=Map.end();++It)
115
ret.append(Py::String(It->first));
120
PyObject *PropertyContainerPy::getCustomAttributes(const char* attr) const
122
// search in PropertyList
123
Property *prop = getPropertyContainerPtr()->getPropertyByName(attr);
125
PyObject* pyobj = prop->getPyObject();
126
if (!pyobj && PyErr_Occurred()) {
127
// the Python exception is already set
128
throw Py::Exception();
132
else if (Base::streq(attr, "__dict__")) {
133
// get the properties to the C++ PropertyContainer class
134
std::map<std::string,App::Property*> Map;
135
getPropertyContainerPtr()->getPropertyMap(Map);
136
PyObject *dict = PyDict_New();
138
for ( std::map<std::string,App::Property*>::iterator it = Map.begin(); it != Map.end(); ++it )
139
PyDict_SetItem(dict, PyString_FromString(it->first.c_str()), PyString_FromString(""));
140
if (PyErr_Occurred()) {
151
int PropertyContainerPy::setCustomAttributes(const char* attr, PyObject *obj)
153
// search in PropertyList
154
Property *prop = getPropertyContainerPtr()->getPropertyByName(attr);
156
// Read-only attributes must not be set over its Python interface
157
short Type = getPropertyContainerPtr()->getPropertyType(prop);
158
if (Type & Prop_ReadOnly) {
160
s << "Object attribute '" << attr << "' is read-only";
161
throw Py::AttributeError(s.str());
164
prop->setPyObject(obj);