| 1 |
/*************************************************************************** |
| 2 |
* Copyright (c) 2007 Werner Mayer <wmayer[at]users.sourceforge.net> * |
| 3 |
* * |
| 4 |
* This file is part of the FreeCAD CAx development system. * |
| 5 |
* * |
| 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. * |
| 10 |
* * |
| 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. * |
| 15 |
* * |
| 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 * |
| 20 |
* * |
| 21 |
***************************************************************************/ |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
#include "DocumentObject.h" |
| 26 |
|
| 27 |
// inclusion of the generated files (generated out of GroupExtensionPy.xml) |
| 28 |
#include "GroupExtensionPy.h" |
| 29 |
#include "GroupExtensionPy.cpp" |
| 30 |
#include "DocumentObjectPy.h" |
| 31 |
|
| 32 |
|
| 33 |
using namespace App; |
| 34 |
|
| 35 |
// returns a string which represent the object e.g. when printed in python |
| 36 |
std::string GroupExtensionPy::representation() const |
| 37 |
{ |
| 38 |
return {"<group extension object>"}; |
| 39 |
} |
| 40 |
|
| 41 |
PyObject* GroupExtensionPy::newObject(PyObject* args) |
| 42 |
{ |
| 43 |
char *sType, *sName = nullptr; |
| 44 |
if (!PyArg_ParseTuple(args, "s|s", &sType, &sName)) { |
| 45 |
return nullptr; |
| 46 |
} |
| 47 |
|
| 48 |
DocumentObject* object = getGroupExtensionPtr()->addObject(sType, sName); |
| 49 |
if (object) { |
| 50 |
return object->getPyObject(); |
| 51 |
} |
| 52 |
else { |
| 53 |
PyErr_Format(PyExc_TypeError, "Cannot create object of type '%s'", sType); |
| 54 |
return nullptr; |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
PyObject* GroupExtensionPy::addObject(PyObject* args) |
| 59 |
{ |
| 60 |
PyObject* object; |
| 61 |
if (!PyArg_ParseTuple(args, "O!", &(DocumentObjectPy::Type), &object)) { |
| 62 |
return nullptr; |
| 63 |
} |
| 64 |
|
| 65 |
DocumentObjectPy* docObj = static_cast<DocumentObjectPy*>(object); |
| 66 |
if (!docObj->getDocumentObjectPtr() |
| 67 |
|| !docObj->getDocumentObjectPtr()->isAttachedToDocument()) { |
| 68 |
PyErr_SetString(Base::PyExc_FC_GeneralError, "Cannot add an invalid object"); |
| 69 |
return nullptr; |
| 70 |
} |
| 71 |
|
| 72 |
if (docObj->getDocumentObjectPtr()->getDocument() |
| 73 |
!= getGroupExtensionPtr()->getExtendedObject()->getDocument()) { |
| 74 |
PyErr_SetString(Base::PyExc_FC_GeneralError, |
| 75 |
"Cannot add an object from another document to this group"); |
| 76 |
return nullptr; |
| 77 |
} |
| 78 |
if (docObj->getDocumentObjectPtr() == this->getGroupExtensionPtr()->getExtendedObject()) { |
| 79 |
PyErr_SetString(Base::PyExc_FC_GeneralError, "Cannot add a group object to itself"); |
| 80 |
return nullptr; |
| 81 |
} |
| 82 |
if (docObj->getDocumentObjectPtr()->hasExtension(GroupExtension::getExtensionClassTypeId())) { |
| 83 |
App::GroupExtension* docGrp = |
| 84 |
docObj->getDocumentObjectPtr()->getExtensionByType<GroupExtension>(); |
| 85 |
if (docGrp->hasObject(getGroupExtensionPtr()->getExtendedObject())) { |
| 86 |
PyErr_SetString(Base::PyExc_FC_GeneralError, |
| 87 |
"Cannot add a group object to a child group"); |
| 88 |
return nullptr; |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
GroupExtension* grp = getGroupExtensionPtr(); |
| 93 |
|
| 94 |
auto vec = grp->addObject(docObj->getDocumentObjectPtr()); |
| 95 |
Py::List list; |
| 96 |
for (App::DocumentObject* obj : vec) { |
| 97 |
list.append(Py::asObject(obj->getPyObject())); |
| 98 |
} |
| 99 |
|
| 100 |
return Py::new_reference_to(list); |
| 101 |
} |
| 102 |
|
| 103 |
PyObject* GroupExtensionPy::addObjects(PyObject* args) |
| 104 |
{ |
| 105 |
|
| 106 |
PyObject* object; |
| 107 |
if (!PyArg_ParseTuple(args, "O", &object)) { |
| 108 |
return nullptr; |
| 109 |
} |
| 110 |
|
| 111 |
if (PyTuple_Check(object) || PyList_Check(object)) { |
| 112 |
Py::Sequence list(object); |
| 113 |
Py::Sequence::size_type size = list.size(); |
| 114 |
std::vector<DocumentObject*> values; |
| 115 |
values.resize(size); |
| 116 |
|
| 117 |
for (Py::Sequence::size_type i = 0; i < size; i++) { |
| 118 |
Py::Object item = list[i]; |
| 119 |
if (!PyObject_TypeCheck(*item, &(DocumentObjectPy::Type))) { |
| 120 |
std::string error = std::string("type in list must be 'DocumentObject', not "); |
| 121 |
error += (*item)->ob_type->tp_name; |
| 122 |
throw Base::TypeError(error); |
| 123 |
} |
| 124 |
|
| 125 |
values[i] = static_cast<DocumentObjectPy*>(*item)->getDocumentObjectPtr(); |
| 126 |
} |
| 127 |
|
| 128 |
GroupExtension* grp = getGroupExtensionPtr(); |
| 129 |
auto vec = grp->addObjects(values); |
| 130 |
Py::List result; |
| 131 |
for (App::DocumentObject* obj : vec) { |
| 132 |
result.append(Py::asObject(obj->getPyObject())); |
| 133 |
} |
| 134 |
|
| 135 |
return Py::new_reference_to(result); |
| 136 |
} |
| 137 |
|
| 138 |
std::string error = std::string("type must be list of 'DocumentObject', not "); |
| 139 |
error += object->ob_type->tp_name; |
| 140 |
throw Base::TypeError(error); |
| 141 |
} |
| 142 |
|
| 143 |
PyObject* GroupExtensionPy::setObjects(PyObject* args) |
| 144 |
{ |
| 145 |
|
| 146 |
PyObject* object; |
| 147 |
if (!PyArg_ParseTuple(args, "O", &object)) { |
| 148 |
return nullptr; |
| 149 |
} |
| 150 |
|
| 151 |
if (PyTuple_Check(object) || PyList_Check(object)) { |
| 152 |
Py::Sequence list(object); |
| 153 |
Py::Sequence::size_type size = list.size(); |
| 154 |
std::vector<DocumentObject*> values; |
| 155 |
values.resize(size); |
| 156 |
|
| 157 |
for (Py::Sequence::size_type i = 0; i < size; i++) { |
| 158 |
Py::Object item = list[i]; |
| 159 |
if (!PyObject_TypeCheck(*item, &(DocumentObjectPy::Type))) { |
| 160 |
std::string error = std::string("type in list must be 'DocumentObject', not "); |
| 161 |
error += (*item)->ob_type->tp_name; |
| 162 |
throw Base::TypeError(error); |
| 163 |
} |
| 164 |
|
| 165 |
values[i] = static_cast<DocumentObjectPy*>(*item)->getDocumentObjectPtr(); |
| 166 |
} |
| 167 |
|
| 168 |
GroupExtension* grp = getGroupExtensionPtr(); |
| 169 |
auto vec = grp->setObjects(values); |
| 170 |
Py::List result; |
| 171 |
for (App::DocumentObject* obj : vec) { |
| 172 |
result.append(Py::asObject(obj->getPyObject())); |
| 173 |
} |
| 174 |
|
| 175 |
return Py::new_reference_to(result); |
| 176 |
} |
| 177 |
|
| 178 |
std::string error = std::string("type must be list of 'DocumentObject', not "); |
| 179 |
error += object->ob_type->tp_name; |
| 180 |
throw Base::TypeError(error); |
| 181 |
} |
| 182 |
|
| 183 |
PyObject* GroupExtensionPy::removeObject(PyObject* args) |
| 184 |
{ |
| 185 |
PyObject* object; |
| 186 |
if (!PyArg_ParseTuple(args, "O!", &(DocumentObjectPy::Type), &object)) { |
| 187 |
return nullptr; |
| 188 |
} |
| 189 |
|
| 190 |
DocumentObjectPy* docObj = static_cast<DocumentObjectPy*>(object); |
| 191 |
if (!docObj->getDocumentObjectPtr() |
| 192 |
|| !docObj->getDocumentObjectPtr()->isAttachedToDocument()) { |
| 193 |
PyErr_SetString(Base::PyExc_FC_GeneralError, "Cannot remove an invalid object"); |
| 194 |
return nullptr; |
| 195 |
} |
| 196 |
if (docObj->getDocumentObjectPtr()->getDocument() |
| 197 |
!= getGroupExtensionPtr()->getExtendedObject()->getDocument()) { |
| 198 |
PyErr_SetString(Base::PyExc_FC_GeneralError, |
| 199 |
"Cannot remove an object from another document from this group"); |
| 200 |
return nullptr; |
| 201 |
} |
| 202 |
|
| 203 |
GroupExtension* grp = getGroupExtensionPtr(); |
| 204 |
|
| 205 |
auto vec = grp->removeObject(docObj->getDocumentObjectPtr()); |
| 206 |
Py::List list; |
| 207 |
for (App::DocumentObject* obj : vec) { |
| 208 |
list.append(Py::asObject(obj->getPyObject())); |
| 209 |
} |
| 210 |
|
| 211 |
return Py::new_reference_to(list); |
| 212 |
} |
| 213 |
|
| 214 |
PyObject* GroupExtensionPy::removeObjects(PyObject* args) |
| 215 |
{ |
| 216 |
|
| 217 |
PyObject* object; |
| 218 |
if (!PyArg_ParseTuple(args, "O", &object)) { |
| 219 |
return nullptr; |
| 220 |
} |
| 221 |
|
| 222 |
if (PyTuple_Check(object) || PyList_Check(object)) { |
| 223 |
Py::Sequence list(object); |
| 224 |
Py::Sequence::size_type size = list.size(); |
| 225 |
std::vector<DocumentObject*> values; |
| 226 |
values.resize(size); |
| 227 |
|
| 228 |
for (Py::Sequence::size_type i = 0; i < size; i++) { |
| 229 |
Py::Object item = list[i]; |
| 230 |
if (!PyObject_TypeCheck(*item, &(DocumentObjectPy::Type))) { |
| 231 |
std::string error = std::string("type in list must be 'DocumentObject', not "); |
| 232 |
error += (*item)->ob_type->tp_name; |
| 233 |
throw Base::TypeError(error); |
| 234 |
} |
| 235 |
|
| 236 |
values[i] = static_cast<DocumentObjectPy*>(*item)->getDocumentObjectPtr(); |
| 237 |
} |
| 238 |
|
| 239 |
GroupExtension* grp = getGroupExtensionPtr(); |
| 240 |
auto vec = grp->removeObjects(values); |
| 241 |
Py::List result; |
| 242 |
for (App::DocumentObject* obj : vec) { |
| 243 |
result.append(Py::asObject(obj->getPyObject())); |
| 244 |
} |
| 245 |
|
| 246 |
return Py::new_reference_to(result); |
| 247 |
} |
| 248 |
|
| 249 |
std::string error = std::string("type must be list of 'DocumentObject', not "); |
| 250 |
error += object->ob_type->tp_name; |
| 251 |
throw Base::TypeError(error); |
| 252 |
} |
| 253 |
|
| 254 |
PyObject* GroupExtensionPy::removeObjectsFromDocument(PyObject* args) |
| 255 |
{ |
| 256 |
if (!PyArg_ParseTuple(args, "")) { |
| 257 |
return nullptr; |
| 258 |
} |
| 259 |
|
| 260 |
getGroupExtensionPtr()->removeObjectsFromDocument(); |
| 261 |
Py_Return; |
| 262 |
} |
| 263 |
|
| 264 |
PyObject* GroupExtensionPy::getObject(PyObject* args) |
| 265 |
{ |
| 266 |
char* pcName; |
| 267 |
if (!PyArg_ParseTuple(args, "s", &pcName)) { |
| 268 |
return nullptr; |
| 269 |
} |
| 270 |
|
| 271 |
DocumentObject* obj = getGroupExtensionPtr()->getObject(pcName); |
| 272 |
if (obj) { |
| 273 |
return obj->getPyObject(); |
| 274 |
} |
| 275 |
else { |
| 276 |
Py_Return; |
| 277 |
} |
| 278 |
} |
| 279 |
|
| 280 |
PyObject* GroupExtensionPy::getObjectsOfType(PyObject* args) |
| 281 |
{ |
| 282 |
char* pcName; |
| 283 |
if (!PyArg_ParseTuple(args, "s", &pcName)) { |
| 284 |
return nullptr; |
| 285 |
} |
| 286 |
|
| 287 |
std::vector<DocumentObject*> objs = getGroupExtensionPtr()->getObjectsOfType(Base::Type::fromName(pcName)); |
| 288 |
Py::List result; |
| 289 |
for (App::DocumentObject* obj : objs) { |
| 290 |
result.append(Py::asObject(obj->getPyObject())); |
| 291 |
} |
| 292 |
|
| 293 |
return Py::new_reference_to(result); |
| 294 |
} |
| 295 |
|
| 296 |
PyObject* GroupExtensionPy::hasObject(PyObject* args) |
| 297 |
{ |
| 298 |
PyObject* object; |
| 299 |
PyObject* recursivePy = Py_False; |
| 300 |
if (!PyArg_ParseTuple(args, |
| 301 |
"O!|O!", |
| 302 |
&(DocumentObjectPy::Type), |
| 303 |
&object, |
| 304 |
&PyBool_Type, |
| 305 |
&recursivePy)) { |
| 306 |
return nullptr; |
| 307 |
} |
| 308 |
|
| 309 |
DocumentObjectPy* docObj = static_cast<DocumentObjectPy*>(object); |
| 310 |
bool recursive = Base::asBoolean(recursivePy); |
| 311 |
if (!docObj->getDocumentObjectPtr() |
| 312 |
|| !docObj->getDocumentObjectPtr()->isAttachedToDocument()) { |
| 313 |
PyErr_SetString(Base::PyExc_FC_GeneralError, "Cannot check an invalid object"); |
| 314 |
return nullptr; |
| 315 |
} |
| 316 |
if (docObj->getDocumentObjectPtr()->getDocument() |
| 317 |
!= getGroupExtensionPtr()->getExtendedObject()->getDocument()) { |
| 318 |
PyErr_SetString(Base::PyExc_FC_GeneralError, |
| 319 |
"Cannot check an object from another document with this group"); |
| 320 |
return nullptr; |
| 321 |
} |
| 322 |
|
| 323 |
bool v = getGroupExtensionPtr()->hasObject(docObj->getDocumentObjectPtr(), recursive); |
| 324 |
return PyBool_FromLong(v ? 1 : 0); |
| 325 |
} |
| 326 |
|
| 327 |
PyObject* GroupExtensionPy::getCustomAttributes(const char* /*attr*/) const |
| 328 |
{ |
| 329 |
return nullptr; |
| 330 |
} |
| 331 |
|
| 332 |
int GroupExtensionPy::setCustomAttributes(const char* /*attr*/, PyObject* /*obj*/) |
| 333 |
{ |
| 334 |
return 0; |
| 335 |
} |
| 336 |
|
| 337 |
// def allowObject(self, obj: Any) -> bool: |
| 338 |
PyObject* GroupExtensionPy::allowObject(PyObject* args) |
| 339 |
{ |
| 340 |
PyObject* object; |
| 341 |
if (!PyArg_ParseTuple(args, "O!", &(DocumentObjectPy::Type), &object)) { |
| 342 |
return nullptr; |
| 343 |
} |
| 344 |
|
| 345 |
auto* docObj = static_cast<DocumentObjectPy*>(object); |
| 346 |
if (!docObj->getDocumentObjectPtr() |
| 347 |
|| !docObj->getDocumentObjectPtr()->isAttachedToDocument()) { |
| 348 |
PyErr_SetString(Base::PyExc_FC_GeneralError, "Cannot check an invalid object"); |
| 349 |
return nullptr; |
| 350 |
} |
| 351 |
if (docObj->getDocumentObjectPtr()->getDocument() |
| 352 |
!= getGroupExtensionPtr()->getExtendedObject()->getDocument()) { |
| 353 |
PyErr_SetString(Base::PyExc_FC_GeneralError, |
| 354 |
"Cannot check an object from another document from this group"); |
| 355 |
return nullptr; |
| 356 |
} |
| 357 |
|
| 358 |
GroupExtension* grp = getGroupExtensionPtr(); |
| 359 |
|
| 360 |
bool allowed = grp->allowObject(docObj->getDocumentObjectPtr()); |
| 361 |
return PyBool_FromLong(allowed ? 1 : 0); |
| 362 |
} |