1
/***************************************************************************
2
* Copyright (c) 2007 Werner Mayer <wmayer[at]users.sourceforge.net> *
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"
26
#include "DocumentObject.h"
28
// inclusion of the generated files (generated out of GroupExtensionPy.xml)
29
#include "GroupExtensionPy.h"
30
#include "GroupExtensionPy.cpp"
31
#include "DocumentObjectPy.h"
36
// returns a string which represent the object e.g. when printed in python
37
std::string GroupExtensionPy::representation() const
39
return {"<group extension object>"};
42
PyObject* GroupExtensionPy::newObject(PyObject *args)
44
char *sType,*sName=nullptr;
45
if (!PyArg_ParseTuple(args, "s|s", &sType,&sName))
48
DocumentObject *object = getGroupExtensionPtr()->addObject(sType, sName);
50
return object->getPyObject();
53
PyErr_Format(PyExc_TypeError, "Cannot create object of type '%s'", sType);
58
PyObject* GroupExtensionPy::addObject(PyObject *args)
61
if (!PyArg_ParseTuple(args, "O!", &(DocumentObjectPy::Type), &object))
64
DocumentObjectPy* docObj = static_cast<DocumentObjectPy*>(object);
65
if (!docObj->getDocumentObjectPtr() || !docObj->getDocumentObjectPtr()->isAttachedToDocument()) {
66
PyErr_SetString(Base::PyExc_FC_GeneralError, "Cannot add an invalid object");
70
if (docObj->getDocumentObjectPtr()->getDocument() != getGroupExtensionPtr()->getExtendedObject()->getDocument()) {
71
PyErr_SetString(Base::PyExc_FC_GeneralError, "Cannot add an object from another document to this group");
74
if (docObj->getDocumentObjectPtr() == this->getGroupExtensionPtr()->getExtendedObject()) {
75
PyErr_SetString(Base::PyExc_FC_GeneralError, "Cannot add a group object to itself");
78
if (docObj->getDocumentObjectPtr()->hasExtension(GroupExtension::getExtensionClassTypeId())) {
79
App::GroupExtension* docGrp = docObj->getDocumentObjectPtr()->getExtensionByType<GroupExtension>();
80
if (docGrp->hasObject(getGroupExtensionPtr()->getExtendedObject())) {
81
PyErr_SetString(Base::PyExc_FC_GeneralError, "Cannot add a group object to a child group");
86
GroupExtension* grp = getGroupExtensionPtr();
88
auto vec = grp->addObject(docObj->getDocumentObjectPtr());
90
for (App::DocumentObject* obj : vec)
91
list.append(Py::asObject(obj->getPyObject()));
93
return Py::new_reference_to(list);
96
PyObject* GroupExtensionPy::addObjects(PyObject *args) {
99
if (!PyArg_ParseTuple(args, "O", &object))
102
if (PyTuple_Check(object) || PyList_Check(object)) {
103
Py::Sequence list(object);
104
Py::Sequence::size_type size = list.size();
105
std::vector<DocumentObject*> values;
108
for (Py::Sequence::size_type i = 0; i < size; i++) {
109
Py::Object item = list[i];
110
if (!PyObject_TypeCheck(*item, &(DocumentObjectPy::Type))) {
111
std::string error = std::string("type in list must be 'DocumentObject', not ");
112
error += (*item)->ob_type->tp_name;
113
throw Base::TypeError(error);
116
values[i] = static_cast<DocumentObjectPy*>(*item)->getDocumentObjectPtr();
119
GroupExtension* grp = getGroupExtensionPtr();
120
auto vec = grp->addObjects(values);
122
for (App::DocumentObject* obj : vec)
123
result.append(Py::asObject(obj->getPyObject()));
125
return Py::new_reference_to(result);
128
std::string error = std::string("type must be list of 'DocumentObject', not ");
129
error += object->ob_type->tp_name;
130
throw Base::TypeError(error);
133
PyObject* GroupExtensionPy::setObjects(PyObject *args) {
136
if (!PyArg_ParseTuple(args, "O", &object))
139
if (PyTuple_Check(object) || PyList_Check(object)) {
140
Py::Sequence list(object);
141
Py::Sequence::size_type size = list.size();
142
std::vector<DocumentObject*> values;
145
for (Py::Sequence::size_type i = 0; i < size; i++) {
146
Py::Object item = list[i];
147
if (!PyObject_TypeCheck(*item, &(DocumentObjectPy::Type))) {
148
std::string error = std::string("type in list must be 'DocumentObject', not ");
149
error += (*item)->ob_type->tp_name;
150
throw Base::TypeError(error);
153
values[i] = static_cast<DocumentObjectPy*>(*item)->getDocumentObjectPtr();
156
GroupExtension* grp = getGroupExtensionPtr();
157
auto vec = grp->setObjects(values);
159
for (App::DocumentObject* obj : vec)
160
result.append(Py::asObject(obj->getPyObject()));
162
return Py::new_reference_to(result);
165
std::string error = std::string("type must be list of 'DocumentObject', not ");
166
error += object->ob_type->tp_name;
167
throw Base::TypeError(error);
170
PyObject* GroupExtensionPy::removeObject(PyObject *args)
173
if (!PyArg_ParseTuple(args, "O!", &(DocumentObjectPy::Type), &object))
176
DocumentObjectPy* docObj = static_cast<DocumentObjectPy*>(object);
177
if (!docObj->getDocumentObjectPtr() || !docObj->getDocumentObjectPtr()->isAttachedToDocument()) {
178
PyErr_SetString(Base::PyExc_FC_GeneralError, "Cannot remove an invalid object");
181
if (docObj->getDocumentObjectPtr()->getDocument() != getGroupExtensionPtr()->getExtendedObject()->getDocument()) {
182
PyErr_SetString(Base::PyExc_FC_GeneralError, "Cannot remove an object from another document from this group");
186
GroupExtension* grp = getGroupExtensionPtr();
188
auto vec = grp->removeObject(docObj->getDocumentObjectPtr());
190
for (App::DocumentObject* obj : vec)
191
list.append(Py::asObject(obj->getPyObject()));
193
return Py::new_reference_to(list);
196
PyObject* GroupExtensionPy::removeObjects(PyObject *args) {
199
if (!PyArg_ParseTuple(args, "O", &object))
202
if (PyTuple_Check(object) || PyList_Check(object)) {
203
Py::Sequence list(object);
204
Py::Sequence::size_type size = list.size();
205
std::vector<DocumentObject*> values;
208
for (Py::Sequence::size_type i = 0; i < size; i++) {
209
Py::Object item = list[i];
210
if (!PyObject_TypeCheck(*item, &(DocumentObjectPy::Type))) {
211
std::string error = std::string("type in list must be 'DocumentObject', not ");
212
error += (*item)->ob_type->tp_name;
213
throw Base::TypeError(error);
216
values[i] = static_cast<DocumentObjectPy*>(*item)->getDocumentObjectPtr();
219
GroupExtension* grp = getGroupExtensionPtr();
220
auto vec = grp->removeObjects(values);
222
for (App::DocumentObject* obj : vec)
223
result.append(Py::asObject(obj->getPyObject()));
225
return Py::new_reference_to(result);
228
std::string error = std::string("type must be list of 'DocumentObject', not ");
229
error += object->ob_type->tp_name;
230
throw Base::TypeError(error);
233
PyObject* GroupExtensionPy::removeObjectsFromDocument(PyObject *args)
235
if (!PyArg_ParseTuple(args, ""))
238
getGroupExtensionPtr()->removeObjectsFromDocument();
242
PyObject* GroupExtensionPy::getObject(PyObject *args)
245
if (!PyArg_ParseTuple(args, "s", &pcName))
248
DocumentObject* obj = getGroupExtensionPtr()->getObject(pcName);
250
return obj->getPyObject();
256
PyObject* GroupExtensionPy::hasObject(PyObject *args)
259
PyObject *recursivePy = Py_False;
260
if (!PyArg_ParseTuple(args, "O!|O!", &(DocumentObjectPy::Type), &object, &PyBool_Type, &recursivePy))
263
DocumentObjectPy* docObj = static_cast<DocumentObjectPy*>(object);
264
bool recursive = Base::asBoolean(recursivePy);
265
if (!docObj->getDocumentObjectPtr() || !docObj->getDocumentObjectPtr()->isAttachedToDocument()) {
266
PyErr_SetString(Base::PyExc_FC_GeneralError, "Cannot check an invalid object");
269
if (docObj->getDocumentObjectPtr()->getDocument() != getGroupExtensionPtr()->getExtendedObject()->getDocument()) {
270
PyErr_SetString(Base::PyExc_FC_GeneralError, "Cannot check an object from another document with this group");
274
bool v = getGroupExtensionPtr()->hasObject(docObj->getDocumentObjectPtr(), recursive);
275
return PyBool_FromLong(v ? 1 : 0);
278
PyObject *GroupExtensionPy::getCustomAttributes(const char* /*attr*/) const
283
int GroupExtensionPy::setCustomAttributes(const char* /*attr*/, PyObject* /*obj*/)