| 1 |
/*************************************************************************** |
| 2 |
* Copyright (c) 2007 Jürgen Riegel <[email protected]> * |
| 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 |
#include <sstream> |
| 24 |
|
| 25 |
#include "PropertyContainer.h" |
| 26 |
#include "Property.h" |
| 27 |
#include "DocumentObject.h" |
| 28 |
#include <Base/PyWrapParseTupleAndKeywords.h> |
| 29 |
|
| 30 |
#include <boost/iostreams/device/array.hpp> |
| 31 |
#include <boost/iostreams/stream.hpp> |
| 32 |
|
| 33 |
#include "PropertyUnits.h" |
| 34 |
|
| 35 |
// inclusion of the generated files (generated out of PropertyContainerPy.xml) |
| 36 |
#include "PropertyContainerPy.h" |
| 37 |
#include "PropertyContainerPy.cpp" |
| 38 |
|
| 39 |
FC_LOG_LEVEL_INIT("Property", true, 2) |
| 40 |
|
| 41 |
using namespace App; |
| 42 |
|
| 43 |
// returns a string which represent the object e.g. when printed in python |
| 44 |
std::string PropertyContainerPy::representation() const |
| 45 |
{ |
| 46 |
return {"<property container>"}; |
| 47 |
} |
| 48 |
|
| 49 |
PyObject* PropertyContainerPy::getPropertyByName(PyObject* args) |
| 50 |
{ |
| 51 |
char* pstr {}; |
| 52 |
int checkOwner = 0; |
| 53 |
if (!PyArg_ParseTuple(args, "s|i", &pstr, &checkOwner)) { |
| 54 |
return nullptr; |
| 55 |
} |
| 56 |
|
| 57 |
if (checkOwner < 0 || checkOwner > 2) { |
| 58 |
PyErr_SetString(PyExc_ValueError, "'checkOwner' expected in the range [0, 2]"); |
| 59 |
return nullptr; |
| 60 |
} |
| 61 |
|
| 62 |
App::Property* prop = getPropertyContainerPtr()->getPropertyByName(pstr); |
| 63 |
if (!prop) { |
| 64 |
PyErr_Format(Base::PyExc_FC_PropertyError, "Property container has no property '%s'", pstr); |
| 65 |
return nullptr; |
| 66 |
} |
| 67 |
|
| 68 |
if (!checkOwner || (checkOwner == 1 && prop->getContainer() == getPropertyContainerPtr())) { |
| 69 |
return prop->getPyObject(); |
| 70 |
} |
| 71 |
|
| 72 |
Py::TupleN res(Py::asObject(prop->getContainer()->getPyObject()), |
| 73 |
Py::asObject(prop->getPyObject())); |
| 74 |
|
| 75 |
return Py::new_reference_to(res); |
| 76 |
} |
| 77 |
|
| 78 |
PyObject* PropertyContainerPy::getPropertyTouchList(PyObject* args) |
| 79 |
{ |
| 80 |
char* pstr {}; |
| 81 |
if (!PyArg_ParseTuple(args, "s", &pstr)) { |
| 82 |
return nullptr; |
| 83 |
} |
| 84 |
|
| 85 |
App::Property* prop = getPropertyContainerPtr()->getPropertyByName(pstr); |
| 86 |
if (prop && prop->isDerivedFrom<PropertyLists>()) { |
| 87 |
const auto& touched = static_cast<PropertyLists*>(prop)->getTouchList(); |
| 88 |
Py::Tuple ret(touched.size()); |
| 89 |
int i = 0; |
| 90 |
for (int idx : touched) { |
| 91 |
ret.setItem(i++, Py::Long(idx)); |
| 92 |
} |
| 93 |
return Py::new_reference_to(ret); |
| 94 |
} |
| 95 |
if (!prop) { |
| 96 |
PyErr_Format(PyExc_AttributeError, "Property container has no property '%s'", pstr); |
| 97 |
return nullptr; |
| 98 |
} |
| 99 |
|
| 100 |
PyErr_Format(PyExc_AttributeError, "Property '%s' is not of list type", pstr); |
| 101 |
return nullptr; |
| 102 |
} |
| 103 |
|
| 104 |
PyObject* PropertyContainerPy::getTypeOfProperty(PyObject* args) |
| 105 |
{ |
| 106 |
Py::List ret; |
| 107 |
char* pstr {}; |
| 108 |
if (!PyArg_ParseTuple(args, "s", &pstr)) { |
| 109 |
return nullptr; |
| 110 |
} |
| 111 |
|
| 112 |
Property* prop = getPropertyContainerPtr()->getPropertyByName(pstr); |
| 113 |
if (!prop) { |
| 114 |
PyErr_Format(PyExc_AttributeError, "Property container has no property '%s'", pstr); |
| 115 |
return nullptr; |
| 116 |
} |
| 117 |
|
| 118 |
short Type = prop->getType(); |
| 119 |
if (Type & Prop_ReadOnly) { |
| 120 |
ret.append(Py::String("ReadOnly")); |
| 121 |
} |
| 122 |
if (Type & Prop_Transient) { |
| 123 |
ret.append(Py::String("Transient")); |
| 124 |
} |
| 125 |
if (Type & Prop_Hidden) { |
| 126 |
ret.append(Py::String("Hidden")); |
| 127 |
} |
| 128 |
if (Type & Prop_Output) { |
| 129 |
ret.append(Py::String("Output")); |
| 130 |
} |
| 131 |
if (Type & Prop_NoRecompute) { |
| 132 |
ret.append(Py::String("NoRecompute")); |
| 133 |
} |
| 134 |
if (Type & Prop_NoPersist) { |
| 135 |
ret.append(Py::String("NoPersist")); |
| 136 |
} |
| 137 |
|
| 138 |
return Py::new_reference_to(ret); |
| 139 |
} |
| 140 |
|
| 141 |
PyObject* PropertyContainerPy::getTypeIdOfProperty(PyObject* args) |
| 142 |
{ |
| 143 |
char* pstr {}; |
| 144 |
if (!PyArg_ParseTuple(args, "s", &pstr)) { |
| 145 |
return nullptr; |
| 146 |
} |
| 147 |
|
| 148 |
Property* prop = getPropertyContainerPtr()->getPropertyByName(pstr); |
| 149 |
if (!prop) { |
| 150 |
PyErr_Format(PyExc_AttributeError, "Property container has no property '%s'", pstr); |
| 151 |
return nullptr; |
| 152 |
} |
| 153 |
|
| 154 |
Py::String str(prop->getTypeId().getName()); |
| 155 |
return Py::new_reference_to(str); |
| 156 |
} |
| 157 |
|
| 158 |
PyObject* PropertyContainerPy::setEditorMode(PyObject* args) |
| 159 |
{ |
| 160 |
char* name {}; |
| 161 |
short type {}; |
| 162 |
if (PyArg_ParseTuple(args, "sh", &name, &type)) { |
| 163 |
App::Property* prop = getPropertyContainerPtr()->getPropertyByName(name); |
| 164 |
if (!prop) { |
| 165 |
PyErr_Format(PyExc_AttributeError, "Property container has no property '%s'", name); |
| 166 |
return nullptr; |
| 167 |
} |
| 168 |
|
| 169 |
std::bitset<32> status(prop->getStatus()); |
| 170 |
status.set(Property::ReadOnly, (type & 1) > 0); |
| 171 |
status.set(Property::Hidden, (type & 2) > 0); |
| 172 |
prop->setStatusValue(status.to_ulong()); |
| 173 |
|
| 174 |
Py_Return; |
| 175 |
} |
| 176 |
|
| 177 |
PyErr_Clear(); |
| 178 |
PyObject* iter {}; |
| 179 |
if (PyArg_ParseTuple(args, "sO", &name, &iter)) { |
| 180 |
if (PyTuple_Check(iter) || PyList_Check(iter)) { |
| 181 |
Py::Sequence seq(iter); |
| 182 |
App::Property* prop = getPropertyContainerPtr()->getPropertyByName(name); |
| 183 |
if (!prop) { |
| 184 |
PyErr_Format(PyExc_AttributeError, "Property container has no property '%s'", name); |
| 185 |
return nullptr; |
| 186 |
} |
| 187 |
|
| 188 |
// reset all bits first |
| 189 |
std::bitset<32> status(prop->getStatus()); |
| 190 |
status.reset(Property::ReadOnly); |
| 191 |
status.reset(Property::Hidden); |
| 192 |
for (Py::Sequence::iterator it = seq.begin(); it != seq.end(); ++it) { |
| 193 |
std::string str = static_cast<std::string>(Py::String(*it)); |
| 194 |
if (str == "ReadOnly") { |
| 195 |
status.set(Property::ReadOnly); |
| 196 |
} |
| 197 |
else if (str == "Hidden") { |
| 198 |
status.set(Property::Hidden); |
| 199 |
} |
| 200 |
} |
| 201 |
prop->setStatusValue(status.to_ulong()); |
| 202 |
|
| 203 |
Py_Return; |
| 204 |
} |
| 205 |
} |
| 206 |
|
| 207 |
PyErr_SetString(PyExc_TypeError, |
| 208 |
"First argument must be str, second can be int, list or tuple"); |
| 209 |
return nullptr; |
| 210 |
} |
| 211 |
|
| 212 |
static const std::map<std::string, int>& getStatusMap() |
| 213 |
{ |
| 214 |
static std::map<std::string, int> statusMap; |
| 215 |
if (statusMap.empty()) { |
| 216 |
statusMap["Immutable"] = Property::Immutable; |
| 217 |
statusMap["ReadOnly"] = Property::ReadOnly; |
| 218 |
statusMap["Hidden"] = Property::Hidden; |
| 219 |
statusMap["Transient"] = Property::Transient; |
| 220 |
statusMap["MaterialEdit"] = Property::MaterialEdit; |
| 221 |
statusMap["NoMaterialListEdit"] = Property::NoMaterialListEdit; |
| 222 |
statusMap["Output"] = Property::Output; |
| 223 |
statusMap["LockDynamic"] = Property::LockDynamic; |
| 224 |
statusMap["NoModify"] = Property::NoModify; |
| 225 |
statusMap["PartialTrigger"] = Property::PartialTrigger; |
| 226 |
statusMap["NoRecompute"] = Property::NoRecompute; |
| 227 |
statusMap["CopyOnChange"] = Property::CopyOnChange; |
| 228 |
statusMap["UserEdit"] = Property::UserEdit; |
| 229 |
} |
| 230 |
return statusMap; |
| 231 |
} |
| 232 |
|
| 233 |
PyObject* PropertyContainerPy::setPropertyStatus(PyObject* args) |
| 234 |
{ |
| 235 |
char* name {}; |
| 236 |
PyObject* pyValue {}; |
| 237 |
if (!PyArg_ParseTuple(args, "sO", &name, &pyValue)) { |
| 238 |
return nullptr; |
| 239 |
} |
| 240 |
|
| 241 |
App::Property* prop = getPropertyContainerPtr()->getPropertyByName(name); |
| 242 |
if (!prop) { |
| 243 |
PyErr_Format(PyExc_AttributeError, "Property container has no property '%s'", name); |
| 244 |
return nullptr; |
| 245 |
} |
| 246 |
|
| 247 |
auto linkProp = freecad_cast<App::PropertyLinkBase*>(prop); |
| 248 |
std::bitset<32> status(prop->getStatus()); |
| 249 |
|
| 250 |
std::vector<Py::Object> items; |
| 251 |
if (PyList_Check(pyValue) || PyTuple_Check(pyValue)) { |
| 252 |
Py::Sequence seq(pyValue); |
| 253 |
for (const auto& it : seq) { |
| 254 |
items.emplace_back(it); |
| 255 |
} |
| 256 |
} |
| 257 |
else { |
| 258 |
items.emplace_back(pyValue); |
| 259 |
} |
| 260 |
|
| 261 |
for (const auto& item : items) { |
| 262 |
bool value = true; |
| 263 |
if (item.isString()) { |
| 264 |
const auto& statusMap = getStatusMap(); |
| 265 |
auto v = static_cast<std::string>(Py::String(item)); |
| 266 |
if (v.size() > 1 && v[0] == '-') { |
| 267 |
value = false; |
| 268 |
v = v.substr(1); |
| 269 |
} |
| 270 |
auto it = statusMap.find(v); |
| 271 |
if (it == statusMap.end()) { |
| 272 |
if (linkProp && v == "AllowPartial") { |
| 273 |
linkProp->setAllowPartial(value); |
| 274 |
continue; |
| 275 |
} |
| 276 |
|
| 277 |
auto lengthProp = freecad_cast<App::PropertyLength*>(prop); |
| 278 |
if (lengthProp && v == "AllowNegativeValues") { |
| 279 |
lengthProp->enableNegative(value); |
| 280 |
continue; |
| 281 |
} |
| 282 |
|
| 283 |
PyErr_Format(PyExc_ValueError, "Unknown property status '%s'", v.c_str()); |
| 284 |
return nullptr; |
| 285 |
} |
| 286 |
|
| 287 |
status.set(it->second, value); |
| 288 |
} |
| 289 |
else if (item.isNumeric()) { |
| 290 |
int v = Py::Long(item); |
| 291 |
if (v < 0) { |
| 292 |
value = false; |
| 293 |
v = -v; |
| 294 |
} |
| 295 |
if (v == 0 || v > 31) { |
| 296 |
PyErr_Format(PyExc_ValueError, "Status value out of range '%d'", v); |
| 297 |
return nullptr; |
| 298 |
} |
| 299 |
status.set(v, value); |
| 300 |
} |
| 301 |
else { |
| 302 |
PyErr_SetString(PyExc_TypeError, "Expects status type to be Int or String"); |
| 303 |
return nullptr; |
| 304 |
} |
| 305 |
} |
| 306 |
|
| 307 |
prop->setStatusValue(status.to_ulong()); |
| 308 |
Py_Return; |
| 309 |
} |
| 310 |
|
| 311 |
PyObject* PropertyContainerPy::getPropertyStatus(PyObject* args) |
| 312 |
{ |
| 313 |
const char* name = ""; |
| 314 |
if (!PyArg_ParseTuple(args, "|s", &name)) { |
| 315 |
return nullptr; |
| 316 |
} |
| 317 |
|
| 318 |
Py::List ret; |
| 319 |
const auto& statusMap = getStatusMap(); |
| 320 |
if (!name[0]) { |
| 321 |
for (auto& v : statusMap) { |
| 322 |
ret.append(Py::String(v.first.c_str())); |
| 323 |
} |
| 324 |
} |
| 325 |
else { |
| 326 |
App::Property* prop = getPropertyContainerPtr()->getPropertyByName(name); |
| 327 |
if (!prop) { |
| 328 |
PyErr_Format(PyExc_AttributeError, "Property container has no property '%s'", name); |
| 329 |
return nullptr; |
| 330 |
} |
| 331 |
|
| 332 |
auto linkProp = freecad_cast<App::PropertyLinkBase*>(prop); |
| 333 |
if (linkProp && linkProp->testFlag(App::PropertyLinkBase::LinkAllowPartial)) { |
| 334 |
ret.append(Py::String("AllowPartial")); |
| 335 |
} |
| 336 |
|
| 337 |
std::bitset<32> bits(prop->getStatus()); |
| 338 |
for (size_t i = 1; i < bits.size(); ++i) { |
| 339 |
if (!bits[i]) { |
| 340 |
continue; |
| 341 |
} |
| 342 |
bool found = false; |
| 343 |
for (auto& v : statusMap) { |
| 344 |
if (v.second == static_cast<int>(i)) { |
| 345 |
ret.append(Py::String(v.first.c_str())); |
| 346 |
found = true; |
| 347 |
break; |
| 348 |
} |
| 349 |
} |
| 350 |
if (!found) { |
| 351 |
ret.append(Py::Long(static_cast<long>(i))); |
| 352 |
} |
| 353 |
} |
| 354 |
} |
| 355 |
return Py::new_reference_to(ret); |
| 356 |
} |
| 357 |
|
| 358 |
PyObject* PropertyContainerPy::getEditorMode(PyObject* args) |
| 359 |
{ |
| 360 |
char* name {}; |
| 361 |
if (!PyArg_ParseTuple(args, "s", &name)) { |
| 362 |
return nullptr; |
| 363 |
} |
| 364 |
|
| 365 |
App::Property* prop = getPropertyContainerPtr()->getPropertyByName(name); |
| 366 |
if (!prop) { |
| 367 |
PyErr_Format(PyExc_AttributeError, "Property container has no property '%s'", name); |
| 368 |
return nullptr; |
| 369 |
} |
| 370 |
|
| 371 |
Py::List ret; |
| 372 |
if (prop) { |
| 373 |
short Type = prop->getType(); |
| 374 |
if ((prop->testStatus(Property::ReadOnly)) || (Type & Prop_ReadOnly)) { |
| 375 |
ret.append(Py::String("ReadOnly")); |
| 376 |
} |
| 377 |
if ((prop->testStatus(Property::Hidden)) || (Type & Prop_Hidden)) { |
| 378 |
ret.append(Py::String("Hidden")); |
| 379 |
} |
| 380 |
} |
| 381 |
return Py::new_reference_to(ret); |
| 382 |
} |
| 383 |
|
| 384 |
PyObject* PropertyContainerPy::getGroupOfProperty(PyObject* args) |
| 385 |
{ |
| 386 |
char* pstr {}; |
| 387 |
if (!PyArg_ParseTuple(args, "s", &pstr)) { |
| 388 |
return nullptr; |
| 389 |
} |
| 390 |
|
| 391 |
Property* prop = getPropertyContainerPtr()->getPropertyByName(pstr); |
| 392 |
if (!prop) { |
| 393 |
PyErr_Format(PyExc_AttributeError, "Property container has no property '%s'", pstr); |
| 394 |
return nullptr; |
| 395 |
} |
| 396 |
|
| 397 |
const char* Group = getPropertyContainerPtr()->getPropertyGroup(prop); |
| 398 |
if (Group) { |
| 399 |
return Py::new_reference_to(Py::String(Group)); |
| 400 |
} |
| 401 |
else { |
| 402 |
return Py::new_reference_to(Py::String("")); |
| 403 |
} |
| 404 |
} |
| 405 |
|
| 406 |
PyObject* PropertyContainerPy::setGroupOfProperty(PyObject* args) |
| 407 |
{ |
| 408 |
char* pstr {}; |
| 409 |
char* group {}; |
| 410 |
if (!PyArg_ParseTuple(args, "ss", &pstr, &group)) { |
| 411 |
return nullptr; |
| 412 |
} |
| 413 |
|
| 414 |
PY_TRY |
| 415 |
{ |
| 416 |
Property* prop = getPropertyContainerPtr()->getDynamicPropertyByName(pstr); |
| 417 |
if (!prop) { |
| 418 |
PyErr_Format(PyExc_AttributeError, |
| 419 |
"Property container has no dynamic property '%s'", |
| 420 |
pstr); |
| 421 |
return nullptr; |
| 422 |
} |
| 423 |
prop->getContainer()->changeDynamicProperty(prop, group, nullptr); |
| 424 |
Py_Return; |
| 425 |
} |
| 426 |
PY_CATCH |
| 427 |
} |
| 428 |
|
| 429 |
|
| 430 |
PyObject* PropertyContainerPy::getDocumentationOfProperty(PyObject* args) |
| 431 |
{ |
| 432 |
char* pstr {}; |
| 433 |
if (!PyArg_ParseTuple(args, "s", &pstr)) { |
| 434 |
return nullptr; |
| 435 |
} |
| 436 |
|
| 437 |
Property* prop = getPropertyContainerPtr()->getPropertyByName(pstr); |
| 438 |
if (!prop) { |
| 439 |
PyErr_Format(PyExc_AttributeError, "Property container has no property '%s'", pstr); |
| 440 |
return nullptr; |
| 441 |
} |
| 442 |
|
| 443 |
const char* docstr = getPropertyContainerPtr()->getPropertyDocumentation(prop); |
| 444 |
if (docstr) { |
| 445 |
return Py::new_reference_to(Py::String(docstr)); |
| 446 |
} |
| 447 |
else { |
| 448 |
return Py::new_reference_to(Py::String("")); |
| 449 |
} |
| 450 |
} |
| 451 |
|
| 452 |
PyObject* PropertyContainerPy::setDocumentationOfProperty(PyObject* args) |
| 453 |
{ |
| 454 |
char* pstr {}; |
| 455 |
char* doc {}; |
| 456 |
if (!PyArg_ParseTuple(args, "ss", &pstr, &doc)) { |
| 457 |
return nullptr; |
| 458 |
} |
| 459 |
|
| 460 |
PY_TRY |
| 461 |
{ |
| 462 |
Property* prop = getPropertyContainerPtr()->getDynamicPropertyByName(pstr); |
| 463 |
if (!prop) { |
| 464 |
PyErr_Format(PyExc_AttributeError, |
| 465 |
"Property container has no dynamic property '%s'", |
| 466 |
pstr); |
| 467 |
return nullptr; |
| 468 |
} |
| 469 |
prop->getContainer()->changeDynamicProperty(prop, nullptr, doc); |
| 470 |
Py_Return; |
| 471 |
} |
| 472 |
PY_CATCH |
| 473 |
} |
| 474 |
|
| 475 |
PyObject* PropertyContainerPy::getEnumerationsOfProperty(PyObject* args) |
| 476 |
{ |
| 477 |
char* pstr {}; |
| 478 |
if (!PyArg_ParseTuple(args, "s", &pstr)) { |
| 479 |
return nullptr; |
| 480 |
} |
| 481 |
|
| 482 |
Property* prop = getPropertyContainerPtr()->getPropertyByName(pstr); |
| 483 |
if (!prop) { |
| 484 |
PyErr_Format(PyExc_AttributeError, "Property container has no property '%s'", pstr); |
| 485 |
return nullptr; |
| 486 |
} |
| 487 |
|
| 488 |
PropertyEnumeration* enumProp = freecad_cast<PropertyEnumeration*>(prop); |
| 489 |
if (!enumProp) { |
| 490 |
Py_Return; |
| 491 |
} |
| 492 |
|
| 493 |
std::vector<std::string> enumerations = enumProp->getEnumVector(); |
| 494 |
Py::List ret; |
| 495 |
for (const auto& it : enumerations) { |
| 496 |
ret.append(Py::String(it)); |
| 497 |
} |
| 498 |
return Py::new_reference_to(ret); |
| 499 |
} |
| 500 |
|
| 501 |
Py::List PropertyContainerPy::getPropertiesList() const |
| 502 |
{ |
| 503 |
Py::List ret; |
| 504 |
std::map<std::string, Property*> Map; |
| 505 |
|
| 506 |
getPropertyContainerPtr()->getPropertyMap(Map); |
| 507 |
|
| 508 |
for (const auto& It : Map) { |
| 509 |
ret.append(Py::String(It.first)); |
| 510 |
} |
| 511 |
|
| 512 |
return ret; |
| 513 |
} |
| 514 |
|
| 515 |
|
| 516 |
PyObject* PropertyContainerPy::dumpPropertyContent(PyObject* args, PyObject* kwds) const |
| 517 |
{ |
| 518 |
int compression = 3; |
| 519 |
const char* property {}; |
| 520 |
static const std::array<const char*, 3> kwds_def {"Property", "Compression", nullptr}; |
| 521 |
PyErr_Clear(); |
| 522 |
if (!Base::Wrapped_ParseTupleAndKeywords(args, |
| 523 |
kwds, |
| 524 |
"s|i", |
| 525 |
kwds_def, |
| 526 |
&property, |
| 527 |
&compression)) { |
| 528 |
return nullptr; |
| 529 |
} |
| 530 |
|
| 531 |
Property* prop = getPropertyContainerPtr()->getPropertyByName(property); |
| 532 |
if (!prop) { |
| 533 |
PyErr_Format(PyExc_AttributeError, "Property container has no property '%s'", property); |
| 534 |
return nullptr; |
| 535 |
} |
| 536 |
|
| 537 |
// setup the stream. the in flag is needed to make "read" work |
| 538 |
std::stringstream stream(std::stringstream::out | std::stringstream::in |
| 539 |
| std::stringstream::binary); |
| 540 |
try { |
| 541 |
prop->dumpToStream(stream, compression); |
| 542 |
} |
| 543 |
catch (...) { |
| 544 |
PyErr_SetString(PyExc_IOError, "Unable to parse content into binary representation"); |
| 545 |
return nullptr; |
| 546 |
} |
| 547 |
|
| 548 |
// build the byte array with correct size |
| 549 |
if (!stream.seekp(0, std::stringstream::end)) { |
| 550 |
PyErr_SetString(PyExc_IOError, "Unable to find end of stream"); |
| 551 |
return nullptr; |
| 552 |
} |
| 553 |
|
| 554 |
std::stringstream::pos_type offset = stream.tellp(); |
| 555 |
if (!stream.seekg(0, std::stringstream::beg)) { |
| 556 |
PyErr_SetString(PyExc_IOError, "Unable to find begin of stream"); |
| 557 |
return nullptr; |
| 558 |
} |
| 559 |
|
| 560 |
PyObject* ba = PyByteArray_FromStringAndSize(nullptr, offset); |
| 561 |
|
| 562 |
// use the buffer protocol to access the underlying array and write into it |
| 563 |
Py_buffer buf = Py_buffer(); |
| 564 |
PyObject_GetBuffer(ba, &buf, PyBUF_WRITABLE); |
| 565 |
try { |
| 566 |
if (!stream.read((char*)buf.buf, offset)) { |
| 567 |
PyErr_SetString(PyExc_IOError, "Error copying data into byte array"); |
| 568 |
return nullptr; |
| 569 |
} |
| 570 |
PyBuffer_Release(&buf); |
| 571 |
} |
| 572 |
catch (...) { |
| 573 |
PyBuffer_Release(&buf); |
| 574 |
PyErr_SetString(PyExc_IOError, "Error copying data into byte array"); |
| 575 |
return nullptr; |
| 576 |
} |
| 577 |
|
| 578 |
return ba; |
| 579 |
} |
| 580 |
|
| 581 |
PyObject* PropertyContainerPy::restorePropertyContent(PyObject* args) |
| 582 |
{ |
| 583 |
PyObject* buffer {}; |
| 584 |
char* property {}; |
| 585 |
if (!PyArg_ParseTuple(args, "sO", &property, &buffer)) { |
| 586 |
return nullptr; |
| 587 |
} |
| 588 |
|
| 589 |
Property* prop = getPropertyContainerPtr()->getPropertyByName(property); |
| 590 |
if (!prop) { |
| 591 |
PyErr_Format(PyExc_AttributeError, "Property container has no property '%s'", property); |
| 592 |
return nullptr; |
| 593 |
} |
| 594 |
|
| 595 |
// check if it really is a buffer |
| 596 |
if (!PyObject_CheckBuffer(buffer)) { |
| 597 |
PyErr_SetString(PyExc_TypeError, "Must be a buffer object"); |
| 598 |
return nullptr; |
| 599 |
} |
| 600 |
|
| 601 |
Py_buffer buf; |
| 602 |
if (PyObject_GetBuffer(buffer, &buf, PyBUF_SIMPLE) < 0) { |
| 603 |
return nullptr; |
| 604 |
} |
| 605 |
|
| 606 |
if (!PyBuffer_IsContiguous(&buf, 'C')) { |
| 607 |
PyErr_SetString(PyExc_TypeError, "Buffer must be contiguous"); |
| 608 |
return nullptr; |
| 609 |
} |
| 610 |
|
| 611 |
// check if it really is a buffer |
| 612 |
try { |
| 613 |
using Device = boost::iostreams::basic_array_source<char>; |
| 614 |
boost::iostreams::stream<Device> stream((char*)buf.buf, buf.len); |
| 615 |
prop->restoreFromStream(stream); |
| 616 |
} |
| 617 |
catch (...) { |
| 618 |
PyErr_SetString(PyExc_IOError, "Unable to restore content"); |
| 619 |
return nullptr; |
| 620 |
} |
| 621 |
|
| 622 |
Py_Return; |
| 623 |
} |
| 624 |
|
| 625 |
PyObject* PropertyContainerPy::getCustomAttributes(const char* attr) const |
| 626 |
{ |
| 627 |
// search in PropertyList |
| 628 |
if (FC_LOG_INSTANCE.level() > FC_LOGLEVEL_TRACE) { |
| 629 |
FC_TRACE("Get property " << attr); |
| 630 |
} |
| 631 |
Property* prop = getPropertyContainerPtr()->getPropertyByName(attr); |
| 632 |
if (prop) { |
| 633 |
PyObject* pyobj = prop->getPyObject(); |
| 634 |
if (!pyobj && PyErr_Occurred()) { |
| 635 |
// the Python exception is already set |
| 636 |
throw Py::Exception(); |
| 637 |
} |
| 638 |
return pyobj; |
| 639 |
} |
| 640 |
if (Base::streq(attr, "__dict__")) { |
| 641 |
// get the properties to the C++ PropertyContainer class |
| 642 |
std::map<std::string, App::Property*> Map; |
| 643 |
getPropertyContainerPtr()->getPropertyMap(Map); |
| 644 |
|
| 645 |
Py::Dict dict; |
| 646 |
for (const auto& it : Map) { |
| 647 |
dict.setItem(it.first, Py::String("")); |
| 648 |
} |
| 649 |
return Py::new_reference_to(dict); |
| 650 |
} |
| 651 |
/// FIXME: For v0.20: Do not use stuff from Part module here! |
| 652 |
if (Base::streq(attr, "Shape") |
| 653 |
&& getPropertyContainerPtr()->isDerivedFrom<App::DocumentObject>()) { |
| 654 |
// Special treatment of Shape property |
| 655 |
static PyObject* _getShape = nullptr; |
| 656 |
if (!_getShape) { |
| 657 |
_getShape = Py_None; |
| 658 |
PyObject* mod = PyImport_ImportModule("Part"); |
| 659 |
if (!mod) { |
| 660 |
PyErr_Clear(); |
| 661 |
} |
| 662 |
else { |
| 663 |
Py::Object pyMod = Py::asObject(mod); |
| 664 |
if (pyMod.hasAttr("getShape")) { |
| 665 |
_getShape = Py::new_reference_to(pyMod.getAttr("getShape")); |
| 666 |
} |
| 667 |
} |
| 668 |
} |
| 669 |
if (_getShape != Py_None) { |
| 670 |
Py::Tuple args(1); |
| 671 |
args.setItem(0, Py::Object(const_cast<PropertyContainerPy*>(this))); |
| 672 |
auto res = PyObject_CallObject(_getShape, args.ptr()); |
| 673 |
if (!res) { |
| 674 |
PyErr_Clear(); |
| 675 |
} |
| 676 |
else { |
| 677 |
Py::Object pyres(res, true); |
| 678 |
if (pyres.hasAttr("isNull")) { |
| 679 |
Py::Callable func(pyres.getAttr("isNull")); |
| 680 |
if (!func.apply().isTrue()) { |
| 681 |
return Py::new_reference_to(res); |
| 682 |
} |
| 683 |
} |
| 684 |
} |
| 685 |
} |
| 686 |
} |
| 687 |
|
| 688 |
return nullptr; |
| 689 |
} |
| 690 |
|
| 691 |
int PropertyContainerPy::setCustomAttributes(const char* attr, PyObject* obj) |
| 692 |
{ |
| 693 |
// search in PropertyList |
| 694 |
Property* prop = getPropertyContainerPtr()->getPropertyByName(attr); |
| 695 |
if (prop) { |
| 696 |
// Read-only attributes must not be set over its Python interface |
| 697 |
if (prop->testStatus(Property::Immutable)) { |
| 698 |
std::stringstream s; |
| 699 |
s << "Object attribute '" << attr << "' is read-only"; |
| 700 |
throw Py::AttributeError(s.str()); |
| 701 |
} |
| 702 |
|
| 703 |
FC_TRACE("Set property " << prop->getFullName()); |
| 704 |
prop->setPyObject(obj); |
| 705 |
return 1; |
| 706 |
} |
| 707 |
|
| 708 |
return 0; |
| 709 |
} |
| 710 |
|
| 711 |
PyObject* PropertyContainerPy::renameProperty(PyObject* args) const |
| 712 |
{ |
| 713 |
char* oldName {}; |
| 714 |
char* newName {}; |
| 715 |
if (PyArg_ParseTuple(args, "ss", &oldName, &newName) == 0) { |
| 716 |
return nullptr; |
| 717 |
} |
| 718 |
|
| 719 |
Property* prop = getPropertyContainerPtr()->getDynamicPropertyByName(oldName); |
| 720 |
if (!prop) { |
| 721 |
PyErr_Format(PyExc_AttributeError, "Property container has no dynamic property '%s'", oldName); |
| 722 |
return nullptr; |
| 723 |
} |
| 724 |
|
| 725 |
PY_TRY |
| 726 |
{ |
| 727 |
prop->getContainer()->renameDynamicProperty(prop, newName); |
| 728 |
Py_Return; |
| 729 |
} |
| 730 |
PY_CATCH |
| 731 |
} |
| 732 |
|