| 1 |
/*************************************************************************** |
| 2 |
* Copyright (c) 2002 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 <algorithm> |
| 24 |
#include <set> |
| 25 |
#include <limits> |
| 26 |
#include <memory> |
| 27 |
#include <list> |
| 28 |
#include <map> |
| 29 |
#include <string> |
| 30 |
#include <vector> |
| 31 |
|
| 32 |
#include <boost/algorithm/string/predicate.hpp> |
| 33 |
#include <boost/math/special_functions/round.hpp> |
| 34 |
|
| 35 |
#include <Base/Console.h> |
| 36 |
#include <Base/Exception.h> |
| 37 |
#include <Base/Interpreter.h> |
| 38 |
#include <Base/ProgramVersion.h> |
| 39 |
#include <Base/Reader.h> |
| 40 |
#include <Base/Writer.h> |
| 41 |
#include <Base/Quantity.h> |
| 42 |
#include <Base/Stream.h> |
| 43 |
#include <Base/Tools.h> |
| 44 |
#include <Base/PyWrapParseTupleAndKeywords.h> |
| 45 |
|
| 46 |
#include "PropertyStandard.h" |
| 47 |
#include "Application.h" |
| 48 |
#include "Document.h" |
| 49 |
#include "DocumentObject.h" |
| 50 |
#include "MaterialPy.h" |
| 51 |
#include "ObjectIdentifier.h" |
| 52 |
|
| 53 |
|
| 54 |
using namespace App; |
| 55 |
using namespace Base; |
| 56 |
using namespace std; |
| 57 |
|
| 58 |
|
| 59 |
//************************************************************************** |
| 60 |
//************************************************************************** |
| 61 |
// PropertyInteger |
| 62 |
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| 63 |
|
| 64 |
TYPESYSTEM_SOURCE(App::PropertyInteger, App::Property) |
| 65 |
|
| 66 |
//************************************************************************** |
| 67 |
// Construction/Destruction |
| 68 |
|
| 69 |
|
| 70 |
PropertyInteger::PropertyInteger() |
| 71 |
{ |
| 72 |
_lValue = 0; |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
PropertyInteger::~PropertyInteger() = default; |
| 77 |
|
| 78 |
//************************************************************************** |
| 79 |
// Base class implementer |
| 80 |
|
| 81 |
|
| 82 |
void PropertyInteger::setValue(long lValue) |
| 83 |
{ |
| 84 |
aboutToSetValue(); |
| 85 |
_lValue = lValue; |
| 86 |
hasSetValue(); |
| 87 |
} |
| 88 |
|
| 89 |
long PropertyInteger::getValue() const |
| 90 |
{ |
| 91 |
return _lValue; |
| 92 |
} |
| 93 |
|
| 94 |
PyObject* PropertyInteger::getPyObject() |
| 95 |
{ |
| 96 |
return Py_BuildValue("l", _lValue); |
| 97 |
} |
| 98 |
|
| 99 |
void PropertyInteger::setPyObject(PyObject* value) |
| 100 |
{ |
| 101 |
if (PyLong_Check(value)) { |
| 102 |
aboutToSetValue(); |
| 103 |
_lValue = PyLong_AsLong(value); |
| 104 |
hasSetValue(); |
| 105 |
} |
| 106 |
else { |
| 107 |
std::string error = std::string("type must be int, not "); |
| 108 |
error += value->ob_type->tp_name; |
| 109 |
throw Base::TypeError(error); |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
void PropertyInteger::Save(Base::Writer& writer) const |
| 114 |
{ |
| 115 |
writer.Stream() << writer.ind() << "<Integer value=\"" << _lValue << "\"/>" << std::endl; |
| 116 |
} |
| 117 |
|
| 118 |
void PropertyInteger::Restore(Base::XMLReader& reader) |
| 119 |
{ |
| 120 |
// read my Element |
| 121 |
reader.readElement("Integer"); |
| 122 |
// get the value of my Attribute |
| 123 |
setValue(reader.getAttribute<long>("value")); |
| 124 |
} |
| 125 |
|
| 126 |
Property* PropertyInteger::Copy() const |
| 127 |
{ |
| 128 |
PropertyInteger* p = new PropertyInteger(); |
| 129 |
p->_lValue = _lValue; |
| 130 |
return p; |
| 131 |
} |
| 132 |
|
| 133 |
void PropertyInteger::Paste(const Property& from) |
| 134 |
{ |
| 135 |
aboutToSetValue(); |
| 136 |
_lValue = dynamic_cast<const PropertyInteger&>(from)._lValue; |
| 137 |
hasSetValue(); |
| 138 |
} |
| 139 |
|
| 140 |
void PropertyInteger::setPathValue(const ObjectIdentifier& path, const boost::any& value) |
| 141 |
{ |
| 142 |
verifyPath(path); |
| 143 |
|
| 144 |
if (value.type() == typeid(long)) { |
| 145 |
setValue(boost::any_cast<long>(value)); |
| 146 |
} |
| 147 |
else if (value.type() == typeid(int)) { |
| 148 |
setValue(boost::any_cast<int>(value)); |
| 149 |
} |
| 150 |
else if (value.type() == typeid(double)) { |
| 151 |
setValue(boost::math::round(boost::any_cast<double>(value))); |
| 152 |
} |
| 153 |
else if (value.type() == typeid(float)) { |
| 154 |
setValue(boost::math::round(boost::any_cast<float>(value))); |
| 155 |
} |
| 156 |
else if (value.type() == typeid(Quantity)) { |
| 157 |
setValue(boost::math::round(boost::any_cast<Quantity>(value).getValue())); |
| 158 |
} |
| 159 |
else { |
| 160 |
throw bad_cast(); |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
|
| 165 |
//************************************************************************** |
| 166 |
//************************************************************************** |
| 167 |
// PropertyPath |
| 168 |
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| 169 |
|
| 170 |
TYPESYSTEM_SOURCE(App::PropertyPath, App::Property) |
| 171 |
|
| 172 |
//************************************************************************** |
| 173 |
// Construction/Destruction |
| 174 |
|
| 175 |
PropertyPath::PropertyPath() = default; |
| 176 |
|
| 177 |
PropertyPath::~PropertyPath() = default; |
| 178 |
|
| 179 |
|
| 180 |
//************************************************************************** |
| 181 |
// Base class implementer |
| 182 |
|
| 183 |
|
| 184 |
//************************************************************************** |
| 185 |
// Setter/getter for the property |
| 186 |
|
| 187 |
void PropertyPath::setValue(const std::filesystem::path& Path) |
| 188 |
{ |
| 189 |
aboutToSetValue(); |
| 190 |
_cValue = Path; |
| 191 |
hasSetValue(); |
| 192 |
} |
| 193 |
|
| 194 |
void PropertyPath::setValue(const char* Path) |
| 195 |
{ |
| 196 |
aboutToSetValue(); |
| 197 |
_cValue = std::filesystem::path(Path); |
| 198 |
hasSetValue(); |
| 199 |
} |
| 200 |
|
| 201 |
const std::filesystem::path& PropertyPath::getValue() const |
| 202 |
{ |
| 203 |
return _cValue; |
| 204 |
} |
| 205 |
|
| 206 |
PyObject* PropertyPath::getPyObject() |
| 207 |
{ |
| 208 |
#if (BOOST_FILESYSTEM_VERSION == 2) |
| 209 |
std::string str = _cValue.native_file_string(); |
| 210 |
#else |
| 211 |
std::string str = _cValue.string(); |
| 212 |
#endif |
| 213 |
|
| 214 |
// Returns a new reference, don't increment it! |
| 215 |
PyObject* p = PyUnicode_DecodeUTF8(str.c_str(), str.size(), nullptr); |
| 216 |
if (!p) { |
| 217 |
throw Base::UnicodeError("UTF8 conversion failure at PropertyPath::getPyObject()"); |
| 218 |
} |
| 219 |
return p; |
| 220 |
} |
| 221 |
|
| 222 |
void PropertyPath::setPyObject(PyObject* value) |
| 223 |
{ |
| 224 |
std::string path; |
| 225 |
if (PyUnicode_Check(value)) { |
| 226 |
path = PyUnicode_AsUTF8(value); |
| 227 |
} |
| 228 |
else { |
| 229 |
std::string error = std::string("type must be str or unicode, not "); |
| 230 |
error += value->ob_type->tp_name; |
| 231 |
throw Base::TypeError(error); |
| 232 |
} |
| 233 |
|
| 234 |
// assign the path |
| 235 |
setValue(path.c_str()); |
| 236 |
} |
| 237 |
|
| 238 |
|
| 239 |
void PropertyPath::Save(Base::Writer& writer) const |
| 240 |
{ |
| 241 |
std::string val = encodeAttribute(_cValue.string()); |
| 242 |
writer.Stream() << writer.ind() << "<Path value=\"" << val << "\"/>" << std::endl; |
| 243 |
} |
| 244 |
|
| 245 |
void PropertyPath::Restore(Base::XMLReader& reader) |
| 246 |
{ |
| 247 |
// read my Element |
| 248 |
reader.readElement("Path"); |
| 249 |
// get the value of my Attribute |
| 250 |
setValue(reader.getAttribute<const char*>("value")); |
| 251 |
} |
| 252 |
|
| 253 |
Property* PropertyPath::Copy() const |
| 254 |
{ |
| 255 |
PropertyPath* p = new PropertyPath(); |
| 256 |
p->_cValue = _cValue; |
| 257 |
return p; |
| 258 |
} |
| 259 |
|
| 260 |
void PropertyPath::Paste(const Property& from) |
| 261 |
{ |
| 262 |
aboutToSetValue(); |
| 263 |
_cValue = dynamic_cast<const PropertyPath&>(from)._cValue; |
| 264 |
hasSetValue(); |
| 265 |
} |
| 266 |
|
| 267 |
unsigned int PropertyPath::getMemSize() const |
| 268 |
{ |
| 269 |
return static_cast<unsigned int>(_cValue.string().size()); |
| 270 |
} |
| 271 |
|
| 272 |
//************************************************************************** |
| 273 |
//************************************************************************** |
| 274 |
// PropertyEnumeration |
| 275 |
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| 276 |
|
| 277 |
TYPESYSTEM_SOURCE(App::PropertyEnumeration, App::Property) |
| 278 |
|
| 279 |
//************************************************************************** |
| 280 |
// Construction/Destruction |
| 281 |
|
| 282 |
|
| 283 |
PropertyEnumeration::PropertyEnumeration() |
| 284 |
{ |
| 285 |
_editorTypeName = "Gui::PropertyEditor::PropertyEnumItem"; |
| 286 |
} |
| 287 |
|
| 288 |
PropertyEnumeration::PropertyEnumeration(const App::Enumeration& e) |
| 289 |
{ |
| 290 |
_enum = e; |
| 291 |
} |
| 292 |
|
| 293 |
PropertyEnumeration::~PropertyEnumeration() = default; |
| 294 |
|
| 295 |
void PropertyEnumeration::setEnums(const char** plEnums) |
| 296 |
{ |
| 297 |
// For backward compatibility, if the property container is not attached to |
| 298 |
// any document (i.e. its full name starts with '?'), do not notify, or |
| 299 |
// else existing code may crash. |
| 300 |
bool notify = !boost::starts_with(getFullName(), "?"); |
| 301 |
if (notify) { |
| 302 |
aboutToSetValue(); |
| 303 |
} |
| 304 |
_enum.setEnums(plEnums); |
| 305 |
if (notify) { |
| 306 |
hasSetValue(); |
| 307 |
} |
| 308 |
} |
| 309 |
|
| 310 |
void PropertyEnumeration::setEnums(const std::vector<std::string>& Enums) |
| 311 |
{ |
| 312 |
setEnumVector(Enums); |
| 313 |
} |
| 314 |
|
| 315 |
void PropertyEnumeration::setValue(const char* value) |
| 316 |
{ |
| 317 |
aboutToSetValue(); |
| 318 |
_enum.setValue(value); |
| 319 |
hasSetValue(); |
| 320 |
} |
| 321 |
|
| 322 |
void PropertyEnumeration::setValue(long value) |
| 323 |
{ |
| 324 |
aboutToSetValue(); |
| 325 |
_enum.setValue(value); |
| 326 |
hasSetValue(); |
| 327 |
} |
| 328 |
|
| 329 |
void PropertyEnumeration::setValue(const Enumeration& source) |
| 330 |
{ |
| 331 |
aboutToSetValue(); |
| 332 |
_enum = source; |
| 333 |
hasSetValue(); |
| 334 |
} |
| 335 |
|
| 336 |
long PropertyEnumeration::getValue() const |
| 337 |
{ |
| 338 |
return _enum.getInt(); |
| 339 |
} |
| 340 |
|
| 341 |
bool PropertyEnumeration::isValue(const char* value) const |
| 342 |
{ |
| 343 |
return _enum.isValue(value); |
| 344 |
} |
| 345 |
|
| 346 |
bool PropertyEnumeration::isPartOf(const char* value) const |
| 347 |
{ |
| 348 |
return _enum.contains(value); |
| 349 |
} |
| 350 |
|
| 351 |
const char* PropertyEnumeration::getValueAsString() const |
| 352 |
{ |
| 353 |
if (!_enum.isValid()) { |
| 354 |
throw Base::RuntimeError("Cannot get value from invalid enumeration"); |
| 355 |
} |
| 356 |
return _enum.getCStr(); |
| 357 |
} |
| 358 |
|
| 359 |
const Enumeration& PropertyEnumeration::getEnum() const |
| 360 |
{ |
| 361 |
return _enum; |
| 362 |
} |
| 363 |
|
| 364 |
std::vector<std::string> PropertyEnumeration::getEnumVector() const |
| 365 |
{ |
| 366 |
return _enum.getEnumVector(); |
| 367 |
} |
| 368 |
|
| 369 |
void PropertyEnumeration::setEnumVector(const std::vector<std::string>& values) |
| 370 |
{ |
| 371 |
// For backward compatibility, if the property container is not attached to |
| 372 |
// any document (i.e. its full name starts with '?'), do not notify, or |
| 373 |
// else existing code may crash. |
| 374 |
bool notify = !boost::starts_with(getFullName(), "?"); |
| 375 |
if (notify) { |
| 376 |
aboutToSetValue(); |
| 377 |
} |
| 378 |
_enum.setEnums(values); |
| 379 |
if (notify) { |
| 380 |
hasSetValue(); |
| 381 |
} |
| 382 |
} |
| 383 |
|
| 384 |
bool PropertyEnumeration::hasEnums() const |
| 385 |
{ |
| 386 |
return _enum.hasEnums(); |
| 387 |
} |
| 388 |
|
| 389 |
bool PropertyEnumeration::isValid() const |
| 390 |
{ |
| 391 |
return _enum.isValid(); |
| 392 |
} |
| 393 |
|
| 394 |
void PropertyEnumeration::Save(Base::Writer& writer) const |
| 395 |
{ |
| 396 |
writer.Stream() << writer.ind() << "<Integer value=\"" << _enum.getInt() << "\""; |
| 397 |
if (_enum.isCustom()) { |
| 398 |
writer.Stream() << " CustomEnum=\"true\""; |
| 399 |
} |
| 400 |
writer.Stream() << "/>" << std::endl; |
| 401 |
if (_enum.isCustom()) { |
| 402 |
std::vector<std::string> items = getEnumVector(); |
| 403 |
writer.Stream() << writer.ind() << "<CustomEnumList count=\"" << items.size() << "\">" |
| 404 |
<< endl; |
| 405 |
writer.incInd(); |
| 406 |
for (auto& item : items) { |
| 407 |
std::string val = encodeAttribute(item); |
| 408 |
writer.Stream() << writer.ind() << "<Enum value=\"" << val << "\"/>" << endl; |
| 409 |
} |
| 410 |
writer.decInd(); |
| 411 |
writer.Stream() << writer.ind() << "</CustomEnumList>" << endl; |
| 412 |
} |
| 413 |
} |
| 414 |
|
| 415 |
void PropertyEnumeration::Restore(Base::XMLReader& reader) |
| 416 |
{ |
| 417 |
// read my Element |
| 418 |
reader.readElement("Integer"); |
| 419 |
// get the value of my Attribute |
| 420 |
long val = reader.getAttribute<long>("value"); |
| 421 |
|
| 422 |
aboutToSetValue(); |
| 423 |
|
| 424 |
if (reader.hasAttribute("CustomEnum")) { |
| 425 |
reader.readElement("CustomEnumList"); |
| 426 |
int count = reader.getAttribute<long>("count"); |
| 427 |
std::vector<std::string> values(count); |
| 428 |
|
| 429 |
for (int i = 0; i < count; i++) { |
| 430 |
reader.readElement("Enum"); |
| 431 |
values[i] = reader.getAttribute<const char*>("value"); |
| 432 |
} |
| 433 |
|
| 434 |
reader.readEndElement("CustomEnumList"); |
| 435 |
|
| 436 |
_enum.setEnums(values); |
| 437 |
} |
| 438 |
|
| 439 |
if (val < 0) { |
| 440 |
// If the enum is empty at this stage do not print a warning |
| 441 |
if (_enum.hasEnums()) { |
| 442 |
Base::Console().developerWarning(std::string("PropertyEnumeration"), |
| 443 |
"Enumeration index %d is out of range, ignore it\n", |
| 444 |
val); |
| 445 |
} |
| 446 |
val = getValue(); |
| 447 |
} |
| 448 |
|
| 449 |
_enum.setValue(val); |
| 450 |
hasSetValue(); |
| 451 |
} |
| 452 |
|
| 453 |
PyObject* PropertyEnumeration::getPyObject() |
| 454 |
{ |
| 455 |
if (!_enum.isValid()) { |
| 456 |
Py_Return; |
| 457 |
} |
| 458 |
|
| 459 |
return Py_BuildValue("s", getValueAsString()); |
| 460 |
} |
| 461 |
|
| 462 |
void PropertyEnumeration::setPyObject(PyObject* value) |
| 463 |
{ |
| 464 |
if (PyLong_Check(value)) { |
| 465 |
long val = PyLong_AsLong(value); |
| 466 |
if (_enum.isValid()) { |
| 467 |
aboutToSetValue(); |
| 468 |
_enum.setValue(val, true); |
| 469 |
hasSetValue(); |
| 470 |
} |
| 471 |
return; |
| 472 |
} |
| 473 |
else if (PyUnicode_Check(value)) { |
| 474 |
std::string str = PyUnicode_AsUTF8(value); |
| 475 |
if (_enum.contains(str.c_str())) { |
| 476 |
aboutToSetValue(); |
| 477 |
_enum.setValue(str); |
| 478 |
hasSetValue(); |
| 479 |
} |
| 480 |
else { |
| 481 |
FC_THROWM(Base::ValueError, |
| 482 |
"'" << str << "' is not part of the enumeration in " << getFullName()); |
| 483 |
} |
| 484 |
return; |
| 485 |
} |
| 486 |
else if (PySequence_Check(value)) { |
| 487 |
|
| 488 |
try { |
| 489 |
std::vector<std::string> values; |
| 490 |
|
| 491 |
int idx = -1; |
| 492 |
Py::Sequence seq(value); |
| 493 |
|
| 494 |
if (seq.size() == 2) { |
| 495 |
Py::Object v(seq[0].ptr()); |
| 496 |
if (!v.isString() && v.isSequence()) { |
| 497 |
idx = Py::Long(seq[1].ptr()); |
| 498 |
seq = v; |
| 499 |
} |
| 500 |
} |
| 501 |
|
| 502 |
values.resize(seq.size()); |
| 503 |
|
| 504 |
for (int i = 0; i < seq.size(); ++i) { |
| 505 |
values[i] = Py::Object(seq[i].ptr()).as_string(); |
| 506 |
} |
| 507 |
|
| 508 |
aboutToSetValue(); |
| 509 |
_enum.setEnums(values); |
| 510 |
if (idx >= 0) { |
| 511 |
_enum.setValue(idx, true); |
| 512 |
} |
| 513 |
hasSetValue(); |
| 514 |
return; |
| 515 |
} |
| 516 |
catch (Py::Exception&) { |
| 517 |
Base::PyException e; |
| 518 |
e.reportException(); |
| 519 |
} |
| 520 |
} |
| 521 |
|
| 522 |
FC_THROWM(Base::TypeError, |
| 523 |
"PropertyEnumeration " |
| 524 |
<< getFullName() |
| 525 |
<< " expects type to be int, string, or list(string), or list(list, int)"); |
| 526 |
} |
| 527 |
|
| 528 |
Property* PropertyEnumeration::Copy() const |
| 529 |
{ |
| 530 |
return new PropertyEnumeration(_enum); |
| 531 |
} |
| 532 |
|
| 533 |
void PropertyEnumeration::Paste(const Property& from) |
| 534 |
{ |
| 535 |
const PropertyEnumeration& prop = dynamic_cast<const PropertyEnumeration&>(from); |
| 536 |
setValue(prop._enum); |
| 537 |
} |
| 538 |
|
| 539 |
void PropertyEnumeration::setPathValue(const ObjectIdentifier&, const boost::any& value) |
| 540 |
{ |
| 541 |
if (value.type() == typeid(int)) { |
| 542 |
setValue(boost::any_cast<int>(value)); |
| 543 |
} |
| 544 |
else if (value.type() == typeid(long)) { |
| 545 |
setValue(boost::any_cast<long>(value)); |
| 546 |
} |
| 547 |
else if (value.type() == typeid(double)) { |
| 548 |
setValue(boost::any_cast<double>(value)); |
| 549 |
} |
| 550 |
else if (value.type() == typeid(float)) { |
| 551 |
setValue(boost::any_cast<float>(value)); |
| 552 |
} |
| 553 |
else if (value.type() == typeid(short)) { |
| 554 |
setValue(boost::any_cast<short>(value)); |
| 555 |
} |
| 556 |
else if (value.type() == typeid(std::string)) { |
| 557 |
setValue(boost::any_cast<std::string>(value).c_str()); |
| 558 |
} |
| 559 |
else if (value.type() == typeid(char*)) { |
| 560 |
setValue(boost::any_cast<char*>(value)); |
| 561 |
} |
| 562 |
else if (value.type() == typeid(const char*)) { |
| 563 |
setValue(boost::any_cast<const char*>(value)); |
| 564 |
} |
| 565 |
else { |
| 566 |
Base::PyGILStateLocker lock; |
| 567 |
Py::Object pyValue = pyObjectFromAny(value); |
| 568 |
setPyObject(pyValue.ptr()); |
| 569 |
} |
| 570 |
} |
| 571 |
|
| 572 |
bool PropertyEnumeration::setPyPathValue(const ObjectIdentifier&, const Py::Object& value) |
| 573 |
{ |
| 574 |
setPyObject(value.ptr()); |
| 575 |
return true; |
| 576 |
} |
| 577 |
|
| 578 |
const boost::any PropertyEnumeration::getPathValue(const ObjectIdentifier& path) const |
| 579 |
{ |
| 580 |
std::string p = path.getSubPathStr(); |
| 581 |
if (p == ".Enum" || p == ".All") { |
| 582 |
Base::PyGILStateLocker lock; |
| 583 |
Py::Object res; |
| 584 |
getPyPathValue(path, res); |
| 585 |
return pyObjectToAny(res, false); |
| 586 |
} |
| 587 |
else if (p == ".String") { |
| 588 |
auto v = getValueAsString(); |
| 589 |
return std::string(v ? v : ""); |
| 590 |
} |
| 591 |
else { |
| 592 |
return getValue(); |
| 593 |
} |
| 594 |
} |
| 595 |
|
| 596 |
bool PropertyEnumeration::getPyPathValue(const ObjectIdentifier& path, Py::Object& r) const |
| 597 |
{ |
| 598 |
std::string p = path.getSubPathStr(); |
| 599 |
if (p == ".Enum" || p == ".All") { |
| 600 |
Base::PyGILStateLocker lock; |
| 601 |
auto maxEnumValue = _enum.maxValue(); |
| 602 |
if (maxEnumValue < 0) { |
| 603 |
return false; // The enum is invalid |
| 604 |
} |
| 605 |
Py::Tuple res(maxEnumValue + 1); |
| 606 |
std::vector<std::string> enums = _enum.getEnumVector(); |
| 607 |
PropertyString tmp; |
| 608 |
for (int i = 0; i < int(enums.size()); ++i) { |
| 609 |
tmp.setValue(enums[i]); |
| 610 |
res.setItem(i, Py::asObject(tmp.getPyObject())); |
| 611 |
} |
| 612 |
if (p == ".Enum") { |
| 613 |
r = res; |
| 614 |
} |
| 615 |
else { |
| 616 |
Py::Tuple tuple(2); |
| 617 |
tuple.setItem(0, res); |
| 618 |
tuple.setItem(1, Py::Long(getValue())); |
| 619 |
r = tuple; |
| 620 |
} |
| 621 |
} |
| 622 |
else if (p == ".String") { |
| 623 |
auto v = getValueAsString(); |
| 624 |
r = Py::String(v ? v : ""); |
| 625 |
} |
| 626 |
else { |
| 627 |
r = Py::Long(getValue()); |
| 628 |
} |
| 629 |
return true; |
| 630 |
} |
| 631 |
|
| 632 |
//************************************************************************** |
| 633 |
//************************************************************************** |
| 634 |
// PropertyIntegerConstraint |
| 635 |
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| 636 |
|
| 637 |
TYPESYSTEM_SOURCE(App::PropertyIntegerConstraint, App::PropertyInteger) |
| 638 |
|
| 639 |
//************************************************************************** |
| 640 |
// Construction/Destruction |
| 641 |
|
| 642 |
|
| 643 |
PropertyIntegerConstraint::PropertyIntegerConstraint() = default; |
| 644 |
|
| 645 |
PropertyIntegerConstraint::~PropertyIntegerConstraint() |
| 646 |
{ |
| 647 |
if (_ConstStruct && _ConstStruct->isDeletable()) { |
| 648 |
delete _ConstStruct; |
| 649 |
} |
| 650 |
} |
| 651 |
|
| 652 |
void PropertyIntegerConstraint::setConstraints(const Constraints* sConstrain) |
| 653 |
{ |
| 654 |
if (_ConstStruct != sConstrain) { |
| 655 |
if (_ConstStruct && _ConstStruct->isDeletable()) { |
| 656 |
delete _ConstStruct; |
| 657 |
} |
| 658 |
} |
| 659 |
|
| 660 |
_ConstStruct = sConstrain; |
| 661 |
} |
| 662 |
|
| 663 |
const PropertyIntegerConstraint::Constraints* PropertyIntegerConstraint::getConstraints() const |
| 664 |
{ |
| 665 |
return _ConstStruct; |
| 666 |
} |
| 667 |
|
| 668 |
long PropertyIntegerConstraint::getMinimum() const |
| 669 |
{ |
| 670 |
if (_ConstStruct) { |
| 671 |
return _ConstStruct->LowerBound; |
| 672 |
} |
| 673 |
// return the min of int, not long |
| 674 |
return std::numeric_limits<int>::lowest(); |
| 675 |
} |
| 676 |
|
| 677 |
long PropertyIntegerConstraint::getMaximum() const |
| 678 |
{ |
| 679 |
if (_ConstStruct) { |
| 680 |
return _ConstStruct->UpperBound; |
| 681 |
} |
| 682 |
// return the max of int, not long |
| 683 |
return std::numeric_limits<int>::max(); |
| 684 |
} |
| 685 |
|
| 686 |
long PropertyIntegerConstraint::getStepSize() const |
| 687 |
{ |
| 688 |
if (_ConstStruct) { |
| 689 |
return _ConstStruct->StepSize; |
| 690 |
} |
| 691 |
return 1; |
| 692 |
} |
| 693 |
|
| 694 |
void PropertyIntegerConstraint::setPyObject(PyObject* value) |
| 695 |
{ |
| 696 |
if (PyLong_Check(value)) { |
| 697 |
long temp = PyLong_AsLong(value); |
| 698 |
if (_ConstStruct) { |
| 699 |
if (temp > _ConstStruct->UpperBound) { |
| 700 |
temp = _ConstStruct->UpperBound; |
| 701 |
} |
| 702 |
else if (temp < _ConstStruct->LowerBound) { |
| 703 |
temp = _ConstStruct->LowerBound; |
| 704 |
} |
| 705 |
} |
| 706 |
|
| 707 |
aboutToSetValue(); |
| 708 |
_lValue = temp; |
| 709 |
hasSetValue(); |
| 710 |
} |
| 711 |
else { |
| 712 |
long valConstr[] = {0, |
| 713 |
std::numeric_limits<int>::lowest(), |
| 714 |
std::numeric_limits<int>::max(), |
| 715 |
1}; |
| 716 |
|
| 717 |
if (PyDict_Check(value)) { |
| 718 |
Py::Tuple dummy; |
| 719 |
static const std::array<const char*, 5> kw = {"value", |
| 720 |
"min", |
| 721 |
"max", |
| 722 |
"step", |
| 723 |
nullptr}; |
| 724 |
|
| 725 |
if (!Base::Wrapped_ParseTupleAndKeywords(dummy.ptr(), |
| 726 |
value, |
| 727 |
"l|lll", |
| 728 |
kw, |
| 729 |
&(valConstr[0]), |
| 730 |
&(valConstr[1]), |
| 731 |
&(valConstr[2]), |
| 732 |
&(valConstr[3]))) { |
| 733 |
throw Py::Exception(); |
| 734 |
} |
| 735 |
} |
| 736 |
else if (PyTuple_Check(value)) { |
| 737 |
if (!PyArg_ParseTuple(value, |
| 738 |
"llll", |
| 739 |
&(valConstr[0]), |
| 740 |
&(valConstr[1]), |
| 741 |
&(valConstr[2]), |
| 742 |
&(valConstr[3]))) { |
| 743 |
throw Py::Exception(); |
| 744 |
} |
| 745 |
} |
| 746 |
else { |
| 747 |
std::string error = std::string("type must be int, dict or tuple, not "); |
| 748 |
error += value->ob_type->tp_name; |
| 749 |
throw Base::TypeError(error); |
| 750 |
} |
| 751 |
|
| 752 |
Constraints* c = new Constraints(); |
| 753 |
c->setDeletable(true); |
| 754 |
c->LowerBound = valConstr[1]; |
| 755 |
c->UpperBound = valConstr[2]; |
| 756 |
c->StepSize = std::max<long>(1, valConstr[3]); |
| 757 |
if (valConstr[0] > c->UpperBound) { |
| 758 |
valConstr[0] = c->UpperBound; |
| 759 |
} |
| 760 |
else if (valConstr[0] < c->LowerBound) { |
| 761 |
valConstr[0] = c->LowerBound; |
| 762 |
} |
| 763 |
setConstraints(c); |
| 764 |
|
| 765 |
aboutToSetValue(); |
| 766 |
_lValue = valConstr[0]; |
| 767 |
hasSetValue(); |
| 768 |
} |
| 769 |
} |
| 770 |
|
| 771 |
//************************************************************************** |
| 772 |
//************************************************************************** |
| 773 |
// PropertyPercent |
| 774 |
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| 775 |
|
| 776 |
TYPESYSTEM_SOURCE(App::PropertyPercent, App::PropertyIntegerConstraint) |
| 777 |
|
| 778 |
const PropertyIntegerConstraint::Constraints percent = {0, 100, 1}; |
| 779 |
|
| 780 |
//************************************************************************** |
| 781 |
// Construction/Destruction |
| 782 |
|
| 783 |
|
| 784 |
PropertyPercent::PropertyPercent() |
| 785 |
{ |
| 786 |
_ConstStruct = &percent; |
| 787 |
} |
| 788 |
|
| 789 |
PropertyPercent::~PropertyPercent() = default; |
| 790 |
|
| 791 |
//************************************************************************** |
| 792 |
//************************************************************************** |
| 793 |
// PropertyIntegerList |
| 794 |
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| 795 |
|
| 796 |
TYPESYSTEM_SOURCE(App::PropertyIntegerList, App::PropertyLists) |
| 797 |
|
| 798 |
//************************************************************************** |
| 799 |
// Construction/Destruction |
| 800 |
|
| 801 |
|
| 802 |
PropertyIntegerList::PropertyIntegerList() = default; |
| 803 |
|
| 804 |
PropertyIntegerList::~PropertyIntegerList() = default; |
| 805 |
|
| 806 |
//************************************************************************** |
| 807 |
// Base class implementer |
| 808 |
|
| 809 |
PyObject* PropertyIntegerList::getPyObject() |
| 810 |
{ |
| 811 |
PyObject* list = PyList_New(getSize()); |
| 812 |
for (int i = 0; i < getSize(); i++) { |
| 813 |
PyList_SetItem(list, i, PyLong_FromLong(_lValueList[i])); |
| 814 |
} |
| 815 |
return list; |
| 816 |
} |
| 817 |
|
| 818 |
long PropertyIntegerList::getPyValue(PyObject* item) const |
| 819 |
{ |
| 820 |
if (PyLong_Check(item)) { |
| 821 |
return PyLong_AsLong(item); |
| 822 |
} |
| 823 |
std::string error = std::string("type in list must be int, not "); |
| 824 |
error += item->ob_type->tp_name; |
| 825 |
throw Base::TypeError(error); |
| 826 |
} |
| 827 |
|
| 828 |
void PropertyIntegerList::Save(Base::Writer& writer) const |
| 829 |
{ |
| 830 |
writer.Stream() << writer.ind() << "<IntegerList count=\"" << getSize() << "\">" << endl; |
| 831 |
writer.incInd(); |
| 832 |
for (int i = 0; i < getSize(); i++) { |
| 833 |
writer.Stream() << writer.ind() << "<I v=\"" << _lValueList[i] << "\"/>" << endl; |
| 834 |
}; |
| 835 |
writer.decInd(); |
| 836 |
writer.Stream() << writer.ind() << "</IntegerList>" << endl; |
| 837 |
} |
| 838 |
|
| 839 |
void PropertyIntegerList::Restore(Base::XMLReader& reader) |
| 840 |
{ |
| 841 |
// read my Element |
| 842 |
reader.readElement("IntegerList"); |
| 843 |
// get the value of my Attribute |
| 844 |
int count = reader.getAttribute<long>("count"); |
| 845 |
|
| 846 |
std::vector<long> values(count); |
| 847 |
for (int i = 0; i < count; i++) { |
| 848 |
reader.readElement("I"); |
| 849 |
values[i] = reader.getAttribute<long>("v"); |
| 850 |
} |
| 851 |
|
| 852 |
reader.readEndElement("IntegerList"); |
| 853 |
|
| 854 |
// assignment |
| 855 |
setValues(values); |
| 856 |
} |
| 857 |
|
| 858 |
Property* PropertyIntegerList::Copy() const |
| 859 |
{ |
| 860 |
PropertyIntegerList* p = new PropertyIntegerList(); |
| 861 |
p->_lValueList = _lValueList; |
| 862 |
return p; |
| 863 |
} |
| 864 |
|
| 865 |
void PropertyIntegerList::Paste(const Property& from) |
| 866 |
{ |
| 867 |
setValues(dynamic_cast<const PropertyIntegerList&>(from)._lValueList); |
| 868 |
} |
| 869 |
|
| 870 |
unsigned int PropertyIntegerList::getMemSize() const |
| 871 |
{ |
| 872 |
return static_cast<unsigned int>(_lValueList.size() * sizeof(long)); |
| 873 |
} |
| 874 |
|
| 875 |
|
| 876 |
//************************************************************************** |
| 877 |
//************************************************************************** |
| 878 |
// PropertyIntegerSet |
| 879 |
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| 880 |
|
| 881 |
TYPESYSTEM_SOURCE(App::PropertyIntegerSet, App::Property) |
| 882 |
|
| 883 |
//************************************************************************** |
| 884 |
// Construction/Destruction |
| 885 |
|
| 886 |
|
| 887 |
PropertyIntegerSet::PropertyIntegerSet() = default; |
| 888 |
|
| 889 |
PropertyIntegerSet::~PropertyIntegerSet() = default; |
| 890 |
|
| 891 |
|
| 892 |
//************************************************************************** |
| 893 |
// Base class implementer |
| 894 |
|
| 895 |
void PropertyIntegerSet::setValue(long lValue) |
| 896 |
{ |
| 897 |
aboutToSetValue(); |
| 898 |
_lValueSet.clear(); |
| 899 |
_lValueSet.insert(lValue); |
| 900 |
hasSetValue(); |
| 901 |
} |
| 902 |
|
| 903 |
void PropertyIntegerSet::setValues(const std::set<long>& values) |
| 904 |
{ |
| 905 |
aboutToSetValue(); |
| 906 |
_lValueSet = values; |
| 907 |
hasSetValue(); |
| 908 |
} |
| 909 |
|
| 910 |
PyObject* PropertyIntegerSet::getPyObject() |
| 911 |
{ |
| 912 |
PyObject* set = PySet_New(nullptr); |
| 913 |
for (long it : _lValueSet) { |
| 914 |
PySet_Add(set, PyLong_FromLong(it)); |
| 915 |
} |
| 916 |
return set; |
| 917 |
} |
| 918 |
|
| 919 |
void PropertyIntegerSet::setPyObject(PyObject* value) |
| 920 |
{ |
| 921 |
if (PySequence_Check(value)) { |
| 922 |
|
| 923 |
Py::Sequence sequence(value); |
| 924 |
Py_ssize_t nSize = sequence.size(); |
| 925 |
std::set<long> values; |
| 926 |
|
| 927 |
for (Py_ssize_t i = 0; i < nSize; ++i) { |
| 928 |
Py::Object item = sequence.getItem(i); |
| 929 |
if (!PyLong_Check(item.ptr())) { |
| 930 |
std::string error = std::string("type in list must be int, not "); |
| 931 |
error += item.ptr()->ob_type->tp_name; |
| 932 |
throw Base::TypeError(error); |
| 933 |
} |
| 934 |
values.insert(PyLong_AsLong(item.ptr())); |
| 935 |
} |
| 936 |
|
| 937 |
setValues(values); |
| 938 |
} |
| 939 |
else if (PyLong_Check(value)) { |
| 940 |
setValue(PyLong_AsLong(value)); |
| 941 |
} |
| 942 |
else { |
| 943 |
std::string error = std::string("type must be int or list of int, not "); |
| 944 |
error += value->ob_type->tp_name; |
| 945 |
throw Base::TypeError(error); |
| 946 |
} |
| 947 |
} |
| 948 |
|
| 949 |
void PropertyIntegerSet::Save(Base::Writer& writer) const |
| 950 |
{ |
| 951 |
writer.Stream() << writer.ind() << "<IntegerSet count=\"" << _lValueSet.size() << "\">" << endl; |
| 952 |
writer.incInd(); |
| 953 |
for (long it : _lValueSet) { |
| 954 |
writer.Stream() << writer.ind() << "<I v=\"" << it << "\"/>" << endl; |
| 955 |
}; |
| 956 |
writer.decInd(); |
| 957 |
writer.Stream() << writer.ind() << "</IntegerSet>" << endl; |
| 958 |
} |
| 959 |
|
| 960 |
void PropertyIntegerSet::Restore(Base::XMLReader& reader) |
| 961 |
{ |
| 962 |
// read my Element |
| 963 |
reader.readElement("IntegerSet"); |
| 964 |
// get the value of my Attribute |
| 965 |
int count = reader.getAttribute<long>("count"); |
| 966 |
|
| 967 |
std::set<long> values; |
| 968 |
for (int i = 0; i < count; i++) { |
| 969 |
reader.readElement("I"); |
| 970 |
values.insert(reader.getAttribute<long>("v")); |
| 971 |
} |
| 972 |
|
| 973 |
reader.readEndElement("IntegerSet"); |
| 974 |
|
| 975 |
// assignment |
| 976 |
setValues(values); |
| 977 |
} |
| 978 |
|
| 979 |
Property* PropertyIntegerSet::Copy() const |
| 980 |
{ |
| 981 |
PropertyIntegerSet* p = new PropertyIntegerSet(); |
| 982 |
p->_lValueSet = _lValueSet; |
| 983 |
return p; |
| 984 |
} |
| 985 |
|
| 986 |
void PropertyIntegerSet::Paste(const Property& from) |
| 987 |
{ |
| 988 |
aboutToSetValue(); |
| 989 |
_lValueSet = dynamic_cast<const PropertyIntegerSet&>(from)._lValueSet; |
| 990 |
hasSetValue(); |
| 991 |
} |
| 992 |
|
| 993 |
unsigned int PropertyIntegerSet::getMemSize() const |
| 994 |
{ |
| 995 |
return static_cast<unsigned int>(_lValueSet.size() * sizeof(long)); |
| 996 |
} |
| 997 |
|
| 998 |
|
| 999 |
//************************************************************************** |
| 1000 |
//************************************************************************** |
| 1001 |
// PropertyFloat |
| 1002 |
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| 1003 |
|
| 1004 |
TYPESYSTEM_SOURCE(App::PropertyFloat, App::Property) |
| 1005 |
|
| 1006 |
//************************************************************************** |
| 1007 |
// Construction/Destruction |
| 1008 |
|
| 1009 |
|
| 1010 |
PropertyFloat::PropertyFloat() |
| 1011 |
{ |
| 1012 |
_dValue = 0.0; |
| 1013 |
} |
| 1014 |
|
| 1015 |
PropertyFloat::~PropertyFloat() = default; |
| 1016 |
|
| 1017 |
//************************************************************************** |
| 1018 |
// Base class implementer |
| 1019 |
|
| 1020 |
void PropertyFloat::setValue(double lValue) |
| 1021 |
{ |
| 1022 |
aboutToSetValue(); |
| 1023 |
_dValue = lValue; |
| 1024 |
hasSetValue(); |
| 1025 |
} |
| 1026 |
|
| 1027 |
double PropertyFloat::getValue() const |
| 1028 |
{ |
| 1029 |
return _dValue; |
| 1030 |
} |
| 1031 |
|
| 1032 |
PyObject* PropertyFloat::getPyObject() |
| 1033 |
{ |
| 1034 |
return Py_BuildValue("d", _dValue); |
| 1035 |
} |
| 1036 |
|
| 1037 |
void PropertyFloat::setPyObject(PyObject* value) |
| 1038 |
{ |
| 1039 |
if (PyFloat_Check(value)) { |
| 1040 |
aboutToSetValue(); |
| 1041 |
_dValue = PyFloat_AsDouble(value); |
| 1042 |
hasSetValue(); |
| 1043 |
} |
| 1044 |
else if (PyLong_Check(value)) { |
| 1045 |
aboutToSetValue(); |
| 1046 |
_dValue = PyLong_AsLong(value); |
| 1047 |
hasSetValue(); |
| 1048 |
} |
| 1049 |
else { |
| 1050 |
std::string error = std::string("type must be float or int, not "); |
| 1051 |
error += value->ob_type->tp_name; |
| 1052 |
throw Base::TypeError(error); |
| 1053 |
} |
| 1054 |
} |
| 1055 |
|
| 1056 |
void PropertyFloat::Save(Base::Writer& writer) const |
| 1057 |
{ |
| 1058 |
writer.Stream() << writer.ind() << "<Float value=\"" << _dValue << "\"/>" << std::endl; |
| 1059 |
} |
| 1060 |
|
| 1061 |
void PropertyFloat::Restore(Base::XMLReader& reader) |
| 1062 |
{ |
| 1063 |
// read my Element |
| 1064 |
reader.readElement("Float"); |
| 1065 |
// get the value of my Attribute |
| 1066 |
setValue(reader.getAttribute<double>("value")); |
| 1067 |
} |
| 1068 |
|
| 1069 |
Property* PropertyFloat::Copy() const |
| 1070 |
{ |
| 1071 |
PropertyFloat* p = new PropertyFloat(); |
| 1072 |
p->_dValue = _dValue; |
| 1073 |
return p; |
| 1074 |
} |
| 1075 |
|
| 1076 |
void PropertyFloat::Paste(const Property& from) |
| 1077 |
{ |
| 1078 |
aboutToSetValue(); |
| 1079 |
_dValue = dynamic_cast<const PropertyFloat&>(from)._dValue; |
| 1080 |
hasSetValue(); |
| 1081 |
} |
| 1082 |
|
| 1083 |
void PropertyFloat::setPathValue(const ObjectIdentifier& path, const boost::any& value) |
| 1084 |
{ |
| 1085 |
verifyPath(path); |
| 1086 |
|
| 1087 |
if (value.type() == typeid(long)) { |
| 1088 |
setValue(boost::any_cast<long>(value)); |
| 1089 |
} |
| 1090 |
else if (value.type() == typeid(unsigned long)) { |
| 1091 |
setValue(boost::any_cast<unsigned long>(value)); |
| 1092 |
} |
| 1093 |
else if (value.type() == typeid(int)) { |
| 1094 |
setValue(boost::any_cast<int>(value)); |
| 1095 |
} |
| 1096 |
else if (value.type() == typeid(double)) { |
| 1097 |
setValue(boost::any_cast<double>(value)); |
| 1098 |
} |
| 1099 |
else if (value.type() == typeid(float)) { |
| 1100 |
setValue(boost::any_cast<float>(value)); |
| 1101 |
} |
| 1102 |
else if (value.type() == typeid(Quantity)) { |
| 1103 |
setValue((boost::any_cast<Quantity>(value)).getValue()); |
| 1104 |
} |
| 1105 |
else { |
| 1106 |
throw bad_cast(); |
| 1107 |
} |
| 1108 |
} |
| 1109 |
|
| 1110 |
const boost::any PropertyFloat::getPathValue(const ObjectIdentifier& path) const |
| 1111 |
{ |
| 1112 |
verifyPath(path); |
| 1113 |
return _dValue; |
| 1114 |
} |
| 1115 |
|
| 1116 |
//************************************************************************** |
| 1117 |
//************************************************************************** |
| 1118 |
// PropertyFloatConstraint |
| 1119 |
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| 1120 |
|
| 1121 |
TYPESYSTEM_SOURCE(App::PropertyFloatConstraint, App::PropertyFloat) |
| 1122 |
|
| 1123 |
//************************************************************************** |
| 1124 |
// Construction/Destruction |
| 1125 |
|
| 1126 |
|
| 1127 |
PropertyFloatConstraint::PropertyFloatConstraint() = default; |
| 1128 |
|
| 1129 |
PropertyFloatConstraint::~PropertyFloatConstraint() |
| 1130 |
{ |
| 1131 |
if (_ConstStruct && _ConstStruct->isDeletable()) { |
| 1132 |
delete _ConstStruct; |
| 1133 |
} |
| 1134 |
} |
| 1135 |
|
| 1136 |
void PropertyFloatConstraint::setConstraints(const Constraints* sConstrain) |
| 1137 |
{ |
| 1138 |
if (_ConstStruct != sConstrain) { |
| 1139 |
if (_ConstStruct && _ConstStruct->isDeletable()) { |
| 1140 |
delete _ConstStruct; |
| 1141 |
} |
| 1142 |
} |
| 1143 |
_ConstStruct = sConstrain; |
| 1144 |
} |
| 1145 |
|
| 1146 |
const PropertyFloatConstraint::Constraints* PropertyFloatConstraint::getConstraints() const |
| 1147 |
{ |
| 1148 |
return _ConstStruct; |
| 1149 |
} |
| 1150 |
|
| 1151 |
double PropertyFloatConstraint::getMinimum() const |
| 1152 |
{ |
| 1153 |
if (_ConstStruct) { |
| 1154 |
return _ConstStruct->LowerBound; |
| 1155 |
} |
| 1156 |
return std::numeric_limits<double>::lowest(); |
| 1157 |
} |
| 1158 |
|
| 1159 |
double PropertyFloatConstraint::getMaximum() const |
| 1160 |
{ |
| 1161 |
if (_ConstStruct) { |
| 1162 |
return _ConstStruct->UpperBound; |
| 1163 |
} |
| 1164 |
return std::numeric_limits<double>::max(); |
| 1165 |
} |
| 1166 |
|
| 1167 |
double PropertyFloatConstraint::getStepSize() const |
| 1168 |
{ |
| 1169 |
if (_ConstStruct) { |
| 1170 |
return _ConstStruct->StepSize; |
| 1171 |
} |
| 1172 |
return 1.0; |
| 1173 |
} |
| 1174 |
|
| 1175 |
void PropertyFloatConstraint::setPyObject(PyObject* value) |
| 1176 |
{ |
| 1177 |
if (PyFloat_Check(value)) { |
| 1178 |
double temp = PyFloat_AsDouble(value); |
| 1179 |
if (_ConstStruct) { |
| 1180 |
if (temp > _ConstStruct->UpperBound) { |
| 1181 |
temp = _ConstStruct->UpperBound; |
| 1182 |
} |
| 1183 |
else if (temp < _ConstStruct->LowerBound) { |
| 1184 |
temp = _ConstStruct->LowerBound; |
| 1185 |
} |
| 1186 |
} |
| 1187 |
|
| 1188 |
aboutToSetValue(); |
| 1189 |
_dValue = temp; |
| 1190 |
hasSetValue(); |
| 1191 |
} |
| 1192 |
else if (PyLong_Check(value)) { |
| 1193 |
double temp = static_cast<double>(PyLong_AsLong(value)); |
| 1194 |
if (_ConstStruct) { |
| 1195 |
if (temp > _ConstStruct->UpperBound) { |
| 1196 |
temp = _ConstStruct->UpperBound; |
| 1197 |
} |
| 1198 |
else if (temp < _ConstStruct->LowerBound) { |
| 1199 |
temp = _ConstStruct->LowerBound; |
| 1200 |
} |
| 1201 |
} |
| 1202 |
|
| 1203 |
aboutToSetValue(); |
| 1204 |
_dValue = temp; |
| 1205 |
hasSetValue(); |
| 1206 |
} |
| 1207 |
else { |
| 1208 |
double valConstr[] = {0.0, |
| 1209 |
std::numeric_limits<double>::lowest(), |
| 1210 |
std::numeric_limits<double>::max(), |
| 1211 |
1.0}; |
| 1212 |
|
| 1213 |
if (PyDict_Check(value)) { |
| 1214 |
Py::Tuple dummy; |
| 1215 |
static const std::array<const char*, 5> kw = {"value", |
| 1216 |
"min", |
| 1217 |
"max", |
| 1218 |
"step", |
| 1219 |
nullptr}; |
| 1220 |
|
| 1221 |
if (!Base::Wrapped_ParseTupleAndKeywords(dummy.ptr(), |
| 1222 |
value, |
| 1223 |
"d|ddd", |
| 1224 |
kw, |
| 1225 |
&(valConstr[0]), |
| 1226 |
&(valConstr[1]), |
| 1227 |
&(valConstr[2]), |
| 1228 |
&(valConstr[3]))) { |
| 1229 |
throw Py::Exception(); |
| 1230 |
} |
| 1231 |
} |
| 1232 |
else if (PyTuple_Check(value)) { |
| 1233 |
if (!PyArg_ParseTuple(value, |
| 1234 |
"dddd", |
| 1235 |
&(valConstr[0]), |
| 1236 |
&(valConstr[1]), |
| 1237 |
&(valConstr[2]), |
| 1238 |
&(valConstr[3]))) { |
| 1239 |
throw Py::Exception(); |
| 1240 |
} |
| 1241 |
} |
| 1242 |
else { |
| 1243 |
std::string error = std::string("type must be float, dict or tuple, not "); |
| 1244 |
error += value->ob_type->tp_name; |
| 1245 |
throw Base::TypeError(error); |
| 1246 |
} |
| 1247 |
|
| 1248 |
double stepSize = valConstr[3]; |
| 1249 |
// need a value > 0 |
| 1250 |
if (stepSize < std::numeric_limits<double>::epsilon()) { |
| 1251 |
throw Base::ValueError("Step size must be greater than zero"); |
| 1252 |
} |
| 1253 |
|
| 1254 |
Constraints* c = new Constraints(); |
| 1255 |
c->setDeletable(true); |
| 1256 |
c->LowerBound = valConstr[1]; |
| 1257 |
c->UpperBound = valConstr[2]; |
| 1258 |
c->StepSize = stepSize; |
| 1259 |
if (valConstr[0] > c->UpperBound) { |
| 1260 |
valConstr[0] = c->UpperBound; |
| 1261 |
} |
| 1262 |
else if (valConstr[0] < c->LowerBound) { |
| 1263 |
valConstr[0] = c->LowerBound; |
| 1264 |
} |
| 1265 |
setConstraints(c); |
| 1266 |
|
| 1267 |
aboutToSetValue(); |
| 1268 |
_dValue = valConstr[0]; |
| 1269 |
hasSetValue(); |
| 1270 |
} |
| 1271 |
} |
| 1272 |
|
| 1273 |
//************************************************************************** |
| 1274 |
// PropertyPrecision |
| 1275 |
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| 1276 |
|
| 1277 |
TYPESYSTEM_SOURCE(App::PropertyPrecision, App::PropertyFloatConstraint) |
| 1278 |
|
| 1279 |
//************************************************************************** |
| 1280 |
// Construction/Destruction |
| 1281 |
// |
| 1282 |
const PropertyFloatConstraint::Constraints PrecisionStandard = { |
| 1283 |
0.0, std::numeric_limits<double>::max(), 0.001}; |
| 1284 |
|
| 1285 |
PropertyPrecision::PropertyPrecision() |
| 1286 |
{ |
| 1287 |
setConstraints(&PrecisionStandard); |
| 1288 |
} |
| 1289 |
|
| 1290 |
PropertyPrecision::~PropertyPrecision() = default; |
| 1291 |
|
| 1292 |
|
| 1293 |
//************************************************************************** |
| 1294 |
// PropertyFloatList |
| 1295 |
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| 1296 |
|
| 1297 |
TYPESYSTEM_SOURCE(App::PropertyFloatList, App::PropertyLists) |
| 1298 |
|
| 1299 |
//************************************************************************** |
| 1300 |
// Construction/Destruction |
| 1301 |
|
| 1302 |
|
| 1303 |
PropertyFloatList::PropertyFloatList() = default; |
| 1304 |
|
| 1305 |
PropertyFloatList::~PropertyFloatList() = default; |
| 1306 |
|
| 1307 |
//************************************************************************** |
| 1308 |
// Base class implementer |
| 1309 |
|
| 1310 |
PyObject* PropertyFloatList::getPyObject() |
| 1311 |
{ |
| 1312 |
PyObject* list = PyList_New(getSize()); |
| 1313 |
for (int i = 0; i < getSize(); i++) { |
| 1314 |
PyList_SetItem(list, i, PyFloat_FromDouble(_lValueList[i])); |
| 1315 |
} |
| 1316 |
return list; |
| 1317 |
} |
| 1318 |
|
| 1319 |
double PropertyFloatList::getPyValue(PyObject* item) const |
| 1320 |
{ |
| 1321 |
if (PyFloat_Check(item)) { |
| 1322 |
return PyFloat_AsDouble(item); |
| 1323 |
} |
| 1324 |
else if (PyLong_Check(item)) { |
| 1325 |
return static_cast<double>(PyLong_AsLong(item)); |
| 1326 |
} |
| 1327 |
else { |
| 1328 |
std::string error = std::string("type in list must be float, not "); |
| 1329 |
error += item->ob_type->tp_name; |
| 1330 |
throw Base::TypeError(error); |
| 1331 |
} |
| 1332 |
} |
| 1333 |
|
| 1334 |
void PropertyFloatList::Save(Base::Writer& writer) const |
| 1335 |
{ |
| 1336 |
if (writer.isForceXML()) { |
| 1337 |
writer.Stream() << writer.ind() << "<FloatList count=\"" << getSize() << "\">" << endl; |
| 1338 |
writer.incInd(); |
| 1339 |
for (int i = 0; i < getSize(); i++) { |
| 1340 |
writer.Stream() << writer.ind() << "<F v=\"" << _lValueList[i] << "\"/>" << endl; |
| 1341 |
}; |
| 1342 |
writer.decInd(); |
| 1343 |
writer.Stream() << writer.ind() << "</FloatList>" << endl; |
| 1344 |
} |
| 1345 |
else { |
| 1346 |
writer.Stream() << writer.ind() << "<FloatList file=\"" |
| 1347 |
<< (getSize() ? writer.addFile(getName(), this) : "") << "\"/>" |
| 1348 |
<< std::endl; |
| 1349 |
} |
| 1350 |
} |
| 1351 |
|
| 1352 |
void PropertyFloatList::Restore(Base::XMLReader& reader) |
| 1353 |
{ |
| 1354 |
reader.readElement("FloatList"); |
| 1355 |
string file(reader.getAttribute<const char*>("file")); |
| 1356 |
|
| 1357 |
if (!file.empty()) { |
| 1358 |
// initiate a file read |
| 1359 |
reader.addFile(file.c_str(), this); |
| 1360 |
} |
| 1361 |
} |
| 1362 |
|
| 1363 |
void PropertyFloatList::SaveDocFile(Base::Writer& writer) const |
| 1364 |
{ |
| 1365 |
Base::OutputStream str(writer.Stream()); |
| 1366 |
uint32_t uCt = (uint32_t)getSize(); |
| 1367 |
str << uCt; |
| 1368 |
if (!isSinglePrecision()) { |
| 1369 |
for (double it : _lValueList) { |
| 1370 |
str << it; |
| 1371 |
} |
| 1372 |
} |
| 1373 |
else { |
| 1374 |
for (double it : _lValueList) { |
| 1375 |
float v = static_cast<float>(it); |
| 1376 |
str << v; |
| 1377 |
} |
| 1378 |
} |
| 1379 |
} |
| 1380 |
|
| 1381 |
void PropertyFloatList::RestoreDocFile(Base::Reader& reader) |
| 1382 |
{ |
| 1383 |
Base::InputStream str(reader); |
| 1384 |
uint32_t uCt = 0; |
| 1385 |
str >> uCt; |
| 1386 |
std::vector<double> values(uCt); |
| 1387 |
if (!isSinglePrecision()) { |
| 1388 |
for (double& it : values) { |
| 1389 |
str >> it; |
| 1390 |
} |
| 1391 |
} |
| 1392 |
else { |
| 1393 |
for (double& it : values) { |
| 1394 |
float val; |
| 1395 |
str >> val; |
| 1396 |
it = val; |
| 1397 |
} |
| 1398 |
} |
| 1399 |
setValues(values); |
| 1400 |
} |
| 1401 |
|
| 1402 |
Property* PropertyFloatList::Copy() const |
| 1403 |
{ |
| 1404 |
PropertyFloatList* p = new PropertyFloatList(); |
| 1405 |
p->_lValueList = _lValueList; |
| 1406 |
return p; |
| 1407 |
} |
| 1408 |
|
| 1409 |
void PropertyFloatList::Paste(const Property& from) |
| 1410 |
{ |
| 1411 |
setValues(dynamic_cast<const PropertyFloatList&>(from)._lValueList); |
| 1412 |
} |
| 1413 |
|
| 1414 |
unsigned int PropertyFloatList::getMemSize() const |
| 1415 |
{ |
| 1416 |
return static_cast<unsigned int>(_lValueList.size() * sizeof(double)); |
| 1417 |
} |
| 1418 |
|
| 1419 |
//************************************************************************** |
| 1420 |
//************************************************************************** |
| 1421 |
// PropertyString |
| 1422 |
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| 1423 |
|
| 1424 |
TYPESYSTEM_SOURCE(App::PropertyString, App::Property) |
| 1425 |
|
| 1426 |
PropertyString::PropertyString() = default; |
| 1427 |
|
| 1428 |
PropertyString::~PropertyString() = default; |
| 1429 |
|
| 1430 |
void PropertyString::setValue(const char* newValue) |
| 1431 |
{ |
| 1432 |
if (!newValue) { |
| 1433 |
return; |
| 1434 |
} |
| 1435 |
|
| 1436 |
if (_cValue == newValue) { |
| 1437 |
return; |
| 1438 |
} |
| 1439 |
|
| 1440 |
std::vector<std::pair<Property*, std::unique_ptr<Property>>> propChanges; |
| 1441 |
std::string newValueStr = newValue; |
| 1442 |
auto obj = freecad_cast<DocumentObject*>(getContainer()); |
| 1443 |
bool commit = false; |
| 1444 |
|
| 1445 |
if (obj && this == &obj->Label) { |
| 1446 |
propChanges = obj->onProposedLabelChange(newValueStr); |
| 1447 |
if (_cValue == newValueStr) { |
| 1448 |
// OnProposedLabelChange has changed the new value to what the current value is |
| 1449 |
return; |
| 1450 |
} |
| 1451 |
if (!propChanges.empty() && !GetApplication().getActiveTransaction()) { |
| 1452 |
commit = true; |
| 1453 |
std::ostringstream str; |
| 1454 |
str << "Change " << obj->getNameInDocument() << ".Label"; |
| 1455 |
GetApplication().setActiveTransaction(str.str().c_str()); |
| 1456 |
} |
| 1457 |
} |
| 1458 |
|
| 1459 |
aboutToSetValue(); |
| 1460 |
_cValue = newValueStr; |
| 1461 |
hasSetValue(); |
| 1462 |
|
| 1463 |
for (auto& change : propChanges) { |
| 1464 |
change.first->Paste(*change.second); |
| 1465 |
} |
| 1466 |
|
| 1467 |
if (commit) { |
| 1468 |
GetApplication().closeActiveTransaction(); |
| 1469 |
} |
| 1470 |
} |
| 1471 |
|
| 1472 |
void PropertyString::setValue(const std::string& sString) |
| 1473 |
{ |
| 1474 |
setValue(sString.c_str()); |
| 1475 |
} |
| 1476 |
|
| 1477 |
const char* PropertyString::getValue() const |
| 1478 |
{ |
| 1479 |
return _cValue.c_str(); |
| 1480 |
} |
| 1481 |
|
| 1482 |
PyObject* PropertyString::getPyObject() |
| 1483 |
{ |
| 1484 |
PyObject* p = PyUnicode_DecodeUTF8(_cValue.c_str(), _cValue.size(), nullptr); |
| 1485 |
if (!p) { |
| 1486 |
throw Base::UnicodeError("UTF8 conversion failure at PropertyString::getPyObject()"); |
| 1487 |
} |
| 1488 |
return p; |
| 1489 |
} |
| 1490 |
|
| 1491 |
void PropertyString::setPyObject(PyObject* value) |
| 1492 |
{ |
| 1493 |
std::string string; |
| 1494 |
if (PyUnicode_Check(value)) { |
| 1495 |
string = PyUnicode_AsUTF8(value); |
| 1496 |
} |
| 1497 |
else { |
| 1498 |
std::string error = std::string("type must be str or unicode, not "); |
| 1499 |
error += value->ob_type->tp_name; |
| 1500 |
throw Base::TypeError(error); |
| 1501 |
} |
| 1502 |
|
| 1503 |
// assign the string |
| 1504 |
setValue(string); |
| 1505 |
} |
| 1506 |
|
| 1507 |
void PropertyString::Save(Base::Writer& writer) const |
| 1508 |
{ |
| 1509 |
auto verifyXMLString = [this](std::string& input) { |
| 1510 |
const std::string output = this->validateXMLString(input); |
| 1511 |
if (output != input) { |
| 1512 |
Base::Console().warning("XML output: Validate invalid string:\n'%s'\n'%s'\n", |
| 1513 |
input, output); |
| 1514 |
} |
| 1515 |
return output; |
| 1516 |
}; |
| 1517 |
std::string val; |
| 1518 |
auto obj = freecad_cast<DocumentObject*>(getContainer()); |
| 1519 |
writer.Stream() << writer.ind() << "<String "; |
| 1520 |
bool exported = false; |
| 1521 |
if (obj && obj->isAttachedToDocument() && obj->isExporting() && &obj->Label == this) { |
| 1522 |
if (obj->allowDuplicateLabel()) { |
| 1523 |
writer.Stream() << "restore=\"1\" "; |
| 1524 |
} |
| 1525 |
else if (_cValue == obj->getNameInDocument()) { |
| 1526 |
writer.Stream() << "restore=\"0\" "; |
| 1527 |
val = encodeAttribute(obj->getExportName()); |
| 1528 |
val = verifyXMLString(val); |
| 1529 |
exported = true; |
| 1530 |
} |
| 1531 |
} |
| 1532 |
if (!exported) { |
| 1533 |
val = encodeAttribute(_cValue); |
| 1534 |
val = verifyXMLString(val); |
| 1535 |
} |
| 1536 |
writer.Stream() << "value=\"" << val << "\"/>" << std::endl; |
| 1537 |
} |
| 1538 |
|
| 1539 |
void PropertyString::Restore(Base::XMLReader& reader) |
| 1540 |
{ |
| 1541 |
// read my Element |
| 1542 |
reader.readElement("String"); |
| 1543 |
// get the value of my Attribute |
| 1544 |
auto obj = freecad_cast<DocumentObject*>(getContainer()); |
| 1545 |
if (obj && &obj->Label == this) { |
| 1546 |
if (reader.hasAttribute("restore")) { |
| 1547 |
int restore = reader.getAttribute<long>("restore"); |
| 1548 |
if (restore == 1) { |
| 1549 |
aboutToSetValue(); |
| 1550 |
_cValue = reader.getAttribute<const char*>("value"); |
| 1551 |
hasSetValue(); |
| 1552 |
} |
| 1553 |
else { |
| 1554 |
setValue(reader.getName(reader.getAttribute<const char*>("value"))); |
| 1555 |
} |
| 1556 |
} |
| 1557 |
else { |
| 1558 |
setValue(reader.getAttribute<const char*>("value")); |
| 1559 |
} |
| 1560 |
} |
| 1561 |
else { |
| 1562 |
setValue(reader.getAttribute<const char*>("value")); |
| 1563 |
} |
| 1564 |
} |
| 1565 |
|
| 1566 |
Property* PropertyString::Copy() const |
| 1567 |
{ |
| 1568 |
PropertyString* p = new PropertyString(); |
| 1569 |
p->_cValue = _cValue; |
| 1570 |
return p; |
| 1571 |
} |
| 1572 |
|
| 1573 |
void PropertyString::Paste(const Property& from) |
| 1574 |
{ |
| 1575 |
setValue(dynamic_cast<const PropertyString&>(from)._cValue); |
| 1576 |
} |
| 1577 |
|
| 1578 |
unsigned int PropertyString::getMemSize() const |
| 1579 |
{ |
| 1580 |
return static_cast<unsigned int>(_cValue.size()); |
| 1581 |
} |
| 1582 |
|
| 1583 |
void PropertyString::setPathValue(const ObjectIdentifier& path, const boost::any& value) |
| 1584 |
{ |
| 1585 |
verifyPath(path); |
| 1586 |
if (value.type() == typeid(bool)) { |
| 1587 |
setValue(boost::any_cast<bool>(value) ? "True" : "False"); |
| 1588 |
} |
| 1589 |
else if (value.type() == typeid(int)) { |
| 1590 |
setValue(std::to_string(boost::any_cast<int>(value))); |
| 1591 |
} |
| 1592 |
else if (value.type() == typeid(long)) { |
| 1593 |
setValue(std::to_string(boost::any_cast<long>(value))); |
| 1594 |
} |
| 1595 |
else if (value.type() == typeid(double)) { |
| 1596 |
setValue(std::to_string(App::any_cast<double>(value))); |
| 1597 |
} |
| 1598 |
else if (value.type() == typeid(float)) { |
| 1599 |
setValue(std::to_string(App::any_cast<float>(value))); |
| 1600 |
} |
| 1601 |
else if (value.type() == typeid(Quantity)) { |
| 1602 |
setValue(boost::any_cast<Quantity>(value).getUserString().c_str()); |
| 1603 |
} |
| 1604 |
else if (value.type() == typeid(std::string)) { |
| 1605 |
setValue(boost::any_cast<const std::string &>(value)); |
| 1606 |
} |
| 1607 |
else { |
| 1608 |
Base::PyGILStateLocker lock; |
| 1609 |
setValue(pyObjectFromAny(value).as_string()); |
| 1610 |
} |
| 1611 |
} |
| 1612 |
|
| 1613 |
const boost::any PropertyString::getPathValue(const ObjectIdentifier& path) const |
| 1614 |
{ |
| 1615 |
verifyPath(path); |
| 1616 |
return _cValue; |
| 1617 |
} |
| 1618 |
|
| 1619 |
//************************************************************************** |
| 1620 |
//************************************************************************** |
| 1621 |
// PropertyUUID |
| 1622 |
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| 1623 |
|
| 1624 |
TYPESYSTEM_SOURCE(App::PropertyUUID, App::Property) |
| 1625 |
|
| 1626 |
PropertyUUID::PropertyUUID() = default; |
| 1627 |
|
| 1628 |
PropertyUUID::~PropertyUUID() = default; |
| 1629 |
|
| 1630 |
void PropertyUUID::setValue(const Base::Uuid& id) |
| 1631 |
{ |
| 1632 |
aboutToSetValue(); |
| 1633 |
_uuid = id; |
| 1634 |
hasSetValue(); |
| 1635 |
} |
| 1636 |
|
| 1637 |
void PropertyUUID::setValue(const char* sString) |
| 1638 |
{ |
| 1639 |
if (sString) { |
| 1640 |
aboutToSetValue(); |
| 1641 |
_uuid.setValue(sString); |
| 1642 |
hasSetValue(); |
| 1643 |
} |
| 1644 |
} |
| 1645 |
|
| 1646 |
void PropertyUUID::setValue(const std::string& sString) |
| 1647 |
{ |
| 1648 |
aboutToSetValue(); |
| 1649 |
_uuid.setValue(sString); |
| 1650 |
hasSetValue(); |
| 1651 |
} |
| 1652 |
|
| 1653 |
const std::string& PropertyUUID::getValueStr() const |
| 1654 |
{ |
| 1655 |
return _uuid.getValue(); |
| 1656 |
} |
| 1657 |
|
| 1658 |
const Base::Uuid& PropertyUUID::getValue() const |
| 1659 |
{ |
| 1660 |
return _uuid; |
| 1661 |
} |
| 1662 |
|
| 1663 |
PyObject* PropertyUUID::getPyObject() |
| 1664 |
{ |
| 1665 |
PyObject* p = PyUnicode_FromString(_uuid.getValue().c_str()); |
| 1666 |
return p; |
| 1667 |
} |
| 1668 |
|
| 1669 |
void PropertyUUID::setPyObject(PyObject* value) |
| 1670 |
{ |
| 1671 |
std::string string; |
| 1672 |
if (PyUnicode_Check(value)) { |
| 1673 |
string = PyUnicode_AsUTF8(value); |
| 1674 |
} |
| 1675 |
else { |
| 1676 |
std::string error = std::string("type must be unicode or str, not "); |
| 1677 |
error += value->ob_type->tp_name; |
| 1678 |
throw Base::TypeError(error); |
| 1679 |
} |
| 1680 |
|
| 1681 |
try { |
| 1682 |
// assign the string |
| 1683 |
Base::Uuid uid; |
| 1684 |
uid.setValue(string); |
| 1685 |
setValue(uid); |
| 1686 |
} |
| 1687 |
catch (const std::exception& e) { |
| 1688 |
throw Base::RuntimeError(e.what()); |
| 1689 |
} |
| 1690 |
} |
| 1691 |
|
| 1692 |
void PropertyUUID::Save(Base::Writer& writer) const |
| 1693 |
{ |
| 1694 |
writer.Stream() << writer.ind() << "<Uuid value=\"" << _uuid.getValue() << "\"/>" << std::endl; |
| 1695 |
} |
| 1696 |
|
| 1697 |
void PropertyUUID::Restore(Base::XMLReader& reader) |
| 1698 |
{ |
| 1699 |
// read my Element |
| 1700 |
reader.readElement("Uuid"); |
| 1701 |
// get the value of my Attribute |
| 1702 |
setValue(reader.getAttribute<const char*>("value")); |
| 1703 |
} |
| 1704 |
|
| 1705 |
Property* PropertyUUID::Copy() const |
| 1706 |
{ |
| 1707 |
PropertyUUID* p = new PropertyUUID(); |
| 1708 |
p->_uuid = _uuid; |
| 1709 |
return p; |
| 1710 |
} |
| 1711 |
|
| 1712 |
void PropertyUUID::Paste(const Property& from) |
| 1713 |
{ |
| 1714 |
aboutToSetValue(); |
| 1715 |
_uuid = dynamic_cast<const PropertyUUID&>(from)._uuid; |
| 1716 |
hasSetValue(); |
| 1717 |
} |
| 1718 |
|
| 1719 |
unsigned int PropertyUUID::getMemSize() const |
| 1720 |
{ |
| 1721 |
return static_cast<unsigned int>(sizeof(_uuid)); |
| 1722 |
} |
| 1723 |
|
| 1724 |
//************************************************************************** |
| 1725 |
// PropertyFont |
| 1726 |
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| 1727 |
|
| 1728 |
TYPESYSTEM_SOURCE(App::PropertyFont, App::PropertyString) |
| 1729 |
|
| 1730 |
PropertyFont::PropertyFont() = default; |
| 1731 |
|
| 1732 |
PropertyFont::~PropertyFont() = default; |
| 1733 |
|
| 1734 |
//************************************************************************** |
| 1735 |
// PropertyStringList |
| 1736 |
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| 1737 |
|
| 1738 |
TYPESYSTEM_SOURCE(App::PropertyStringList, App::PropertyLists) |
| 1739 |
|
| 1740 |
PropertyStringList::PropertyStringList() = default; |
| 1741 |
|
| 1742 |
PropertyStringList::~PropertyStringList() = default; |
| 1743 |
|
| 1744 |
//************************************************************************** |
| 1745 |
// Base class implementer |
| 1746 |
|
| 1747 |
void PropertyStringList::setValues(const std::list<std::string>& lValue) |
| 1748 |
{ |
| 1749 |
std::vector<std::string> vals; |
| 1750 |
vals.reserve(lValue.size()); |
| 1751 |
for (const auto& v : lValue) { |
| 1752 |
vals.push_back(v); |
| 1753 |
} |
| 1754 |
setValues(vals); |
| 1755 |
} |
| 1756 |
|
| 1757 |
PyObject* PropertyStringList::getPyObject() |
| 1758 |
{ |
| 1759 |
PyObject* list = PyList_New(getSize()); |
| 1760 |
|
| 1761 |
for (int i = 0; i < getSize(); i++) { |
| 1762 |
PyObject* item = |
| 1763 |
PyUnicode_DecodeUTF8(_lValueList[i].c_str(), _lValueList[i].size(), nullptr); |
| 1764 |
if (!item) { |
| 1765 |
Py_DECREF(list); |
| 1766 |
throw Base::UnicodeError( |
| 1767 |
"UTF8 conversion failure at PropertyStringList::getPyObject()"); |
| 1768 |
} |
| 1769 |
PyList_SetItem(list, i, item); |
| 1770 |
} |
| 1771 |
|
| 1772 |
return list; |
| 1773 |
} |
| 1774 |
|
| 1775 |
std::string PropertyStringList::getPyValue(PyObject* item) const |
| 1776 |
{ |
| 1777 |
std::string ret; |
| 1778 |
if (PyUnicode_Check(item)) { |
| 1779 |
ret = PyUnicode_AsUTF8(item); |
| 1780 |
} |
| 1781 |
else if (PyBytes_Check(item)) { |
| 1782 |
ret = PyBytes_AsString(item); |
| 1783 |
} |
| 1784 |
else { |
| 1785 |
std::string error = std::string("type in list must be str or unicode, not "); |
| 1786 |
error += item->ob_type->tp_name; |
| 1787 |
throw Base::TypeError(error); |
| 1788 |
} |
| 1789 |
return ret; |
| 1790 |
} |
| 1791 |
|
| 1792 |
unsigned int PropertyStringList::getMemSize() const |
| 1793 |
{ |
| 1794 |
size_t size = 0; |
| 1795 |
for (int i = 0; i < getSize(); i++) { |
| 1796 |
size += _lValueList[i].size(); |
| 1797 |
} |
| 1798 |
return static_cast<unsigned int>(size); |
| 1799 |
} |
| 1800 |
|
| 1801 |
void PropertyStringList::Save(Base::Writer& writer) const |
| 1802 |
{ |
| 1803 |
writer.Stream() << writer.ind() << "<StringList count=\"" << getSize() << "\">" << endl; |
| 1804 |
writer.incInd(); |
| 1805 |
for (int i = 0; i < getSize(); i++) { |
| 1806 |
std::string val = encodeAttribute(_lValueList[i]); |
| 1807 |
writer.Stream() << writer.ind() << "<String value=\"" << val << "\"/>" << endl; |
| 1808 |
} |
| 1809 |
writer.decInd(); |
| 1810 |
writer.Stream() << writer.ind() << "</StringList>" << endl; |
| 1811 |
} |
| 1812 |
|
| 1813 |
void PropertyStringList::Restore(Base::XMLReader& reader) |
| 1814 |
{ |
| 1815 |
// read my Element |
| 1816 |
reader.readElement("StringList"); |
| 1817 |
// get the value of my Attribute |
| 1818 |
int count = reader.getAttribute<long>("count"); |
| 1819 |
|
| 1820 |
std::vector<std::string> values(count); |
| 1821 |
for (int i = 0; i < count; i++) { |
| 1822 |
reader.readElement("String"); |
| 1823 |
values[i] = reader.getAttribute<const char*>("value"); |
| 1824 |
} |
| 1825 |
|
| 1826 |
reader.readEndElement("StringList"); |
| 1827 |
|
| 1828 |
// assignment |
| 1829 |
setValues(values); |
| 1830 |
} |
| 1831 |
|
| 1832 |
Property* PropertyStringList::Copy() const |
| 1833 |
{ |
| 1834 |
PropertyStringList* p = new PropertyStringList(); |
| 1835 |
p->_lValueList = _lValueList; |
| 1836 |
return p; |
| 1837 |
} |
| 1838 |
|
| 1839 |
void PropertyStringList::Paste(const Property& from) |
| 1840 |
{ |
| 1841 |
setValues(dynamic_cast<const PropertyStringList&>(from)._lValueList); |
| 1842 |
} |
| 1843 |
|
| 1844 |
|
| 1845 |
//************************************************************************** |
| 1846 |
// PropertyMap |
| 1847 |
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| 1848 |
|
| 1849 |
TYPESYSTEM_SOURCE(App::PropertyMap, App::Property) |
| 1850 |
|
| 1851 |
PropertyMap::PropertyMap() = default; |
| 1852 |
|
| 1853 |
PropertyMap::~PropertyMap() = default; |
| 1854 |
|
| 1855 |
//************************************************************************** |
| 1856 |
// Base class implementer |
| 1857 |
|
| 1858 |
|
| 1859 |
int PropertyMap::getSize() const |
| 1860 |
{ |
| 1861 |
return static_cast<int>(_lValueList.size()); |
| 1862 |
} |
| 1863 |
|
| 1864 |
void PropertyMap::setValue(const std::string& key, const std::string& value) |
| 1865 |
{ |
| 1866 |
aboutToSetValue(); |
| 1867 |
_lValueList[key] = value; |
| 1868 |
hasSetValue(); |
| 1869 |
} |
| 1870 |
|
| 1871 |
void PropertyMap::setValues(const std::map<std::string, std::string>& map) |
| 1872 |
{ |
| 1873 |
aboutToSetValue(); |
| 1874 |
_lValueList = map; |
| 1875 |
hasSetValue(); |
| 1876 |
} |
| 1877 |
|
| 1878 |
const std::string& PropertyMap::operator[](const std::string& key) const |
| 1879 |
{ |
| 1880 |
static std::string empty; |
| 1881 |
auto it = _lValueList.find(key); |
| 1882 |
if (it != _lValueList.end()) { |
| 1883 |
return it->second; |
| 1884 |
} |
| 1885 |
return empty; |
| 1886 |
} |
| 1887 |
|
| 1888 |
PyObject* PropertyMap::getPyObject() |
| 1889 |
{ |
| 1890 |
PyObject* dict = PyDict_New(); |
| 1891 |
|
| 1892 |
for (auto it = _lValueList.begin(); it != _lValueList.end(); ++it) { |
| 1893 |
PyObject* item = PyUnicode_DecodeUTF8(it->second.c_str(), it->second.size(), nullptr); |
| 1894 |
if (!item) { |
| 1895 |
Py_DECREF(dict); |
| 1896 |
throw Base::UnicodeError("UTF8 conversion failure at PropertyMap::getPyObject()"); |
| 1897 |
} |
| 1898 |
PyDict_SetItemString(dict, it->first.c_str(), item); |
| 1899 |
Py_DECREF(item); |
| 1900 |
} |
| 1901 |
|
| 1902 |
return dict; |
| 1903 |
} |
| 1904 |
|
| 1905 |
void PropertyMap::setPyObject(PyObject* value) |
| 1906 |
{ |
| 1907 |
if (PyMapping_Check(value)) { |
| 1908 |
std::map<std::string, std::string> values; |
| 1909 |
// get key and item list |
| 1910 |
PyObject* keyList = PyMapping_Keys(value); |
| 1911 |
PyObject* itemList = PyMapping_Values(value); |
| 1912 |
Py_ssize_t nSize = PyList_Size(keyList); |
| 1913 |
|
| 1914 |
for (Py_ssize_t i = 0; i < nSize; ++i) { |
| 1915 |
// check on the key: |
| 1916 |
std::string keyStr; |
| 1917 |
PyObject* key = PyList_GetItem(keyList, i); |
| 1918 |
if (PyUnicode_Check(key)) { |
| 1919 |
keyStr = PyUnicode_AsUTF8(key); |
| 1920 |
} |
| 1921 |
else { |
| 1922 |
std::string error("type of the key need to be string, not "); |
| 1923 |
error += key->ob_type->tp_name; |
| 1924 |
throw Base::TypeError(error); |
| 1925 |
} |
| 1926 |
|
| 1927 |
// check on the item: |
| 1928 |
PyObject* item = PyList_GetItem(itemList, i); |
| 1929 |
if (PyUnicode_Check(item)) { |
| 1930 |
values[keyStr] = PyUnicode_AsUTF8(item); |
| 1931 |
} |
| 1932 |
else { |
| 1933 |
std::string error("type in values must be string, not "); |
| 1934 |
error += item->ob_type->tp_name; |
| 1935 |
throw Base::TypeError(error); |
| 1936 |
} |
| 1937 |
} |
| 1938 |
|
| 1939 |
Py_XDECREF(itemList); |
| 1940 |
Py_XDECREF(keyList); |
| 1941 |
|
| 1942 |
setValues(values); |
| 1943 |
} |
| 1944 |
else { |
| 1945 |
std::string error("type must be a dict or object with mapping protocol, not "); |
| 1946 |
error += value->ob_type->tp_name; |
| 1947 |
throw Base::TypeError(error); |
| 1948 |
} |
| 1949 |
} |
| 1950 |
|
| 1951 |
unsigned int PropertyMap::getMemSize() const |
| 1952 |
{ |
| 1953 |
size_t size = 0; |
| 1954 |
for (const auto& it : _lValueList) { |
| 1955 |
size += it.second.size(); |
| 1956 |
size += it.first.size(); |
| 1957 |
} |
| 1958 |
return size; |
| 1959 |
} |
| 1960 |
|
| 1961 |
void PropertyMap::Save(Base::Writer& writer) const |
| 1962 |
{ |
| 1963 |
writer.Stream() << writer.ind() << "<Map count=\"" << getSize() << "\">" << endl; |
| 1964 |
writer.incInd(); |
| 1965 |
for (const auto& it : _lValueList) { |
| 1966 |
writer.Stream() << writer.ind() << "<Item key=\"" << encodeAttribute(it.first) |
| 1967 |
<< "\" value=\"" << encodeAttribute(it.second) << "\"/>" << endl; |
| 1968 |
} |
| 1969 |
|
| 1970 |
writer.decInd(); |
| 1971 |
writer.Stream() << writer.ind() << "</Map>" << endl; |
| 1972 |
} |
| 1973 |
|
| 1974 |
void PropertyMap::Restore(Base::XMLReader& reader) |
| 1975 |
{ |
| 1976 |
// read my Element |
| 1977 |
reader.readElement("Map"); |
| 1978 |
// get the value of my Attribute |
| 1979 |
int count = reader.getAttribute<long>("count"); |
| 1980 |
|
| 1981 |
std::map<std::string, std::string> values; |
| 1982 |
for (int i = 0; i < count; i++) { |
| 1983 |
reader.readElement("Item"); |
| 1984 |
values[reader.getAttribute<const char*>("key")] = reader.getAttribute<const char*>("value"); |
| 1985 |
} |
| 1986 |
|
| 1987 |
reader.readEndElement("Map"); |
| 1988 |
|
| 1989 |
// assignment |
| 1990 |
setValues(values); |
| 1991 |
} |
| 1992 |
|
| 1993 |
Property* PropertyMap::Copy() const |
| 1994 |
{ |
| 1995 |
PropertyMap* p = new PropertyMap(); |
| 1996 |
p->_lValueList = _lValueList; |
| 1997 |
return p; |
| 1998 |
} |
| 1999 |
|
| 2000 |
void PropertyMap::Paste(const Property& from) |
| 2001 |
{ |
| 2002 |
aboutToSetValue(); |
| 2003 |
_lValueList = dynamic_cast<const PropertyMap&>(from)._lValueList; |
| 2004 |
hasSetValue(); |
| 2005 |
} |
| 2006 |
|
| 2007 |
|
| 2008 |
//************************************************************************** |
| 2009 |
//************************************************************************** |
| 2010 |
// PropertyBool |
| 2011 |
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| 2012 |
|
| 2013 |
TYPESYSTEM_SOURCE(App::PropertyBool, App::Property) |
| 2014 |
|
| 2015 |
//************************************************************************** |
| 2016 |
// Construction/Destruction |
| 2017 |
|
| 2018 |
PropertyBool::PropertyBool() |
| 2019 |
{ |
| 2020 |
_lValue = false; |
| 2021 |
} |
| 2022 |
|
| 2023 |
PropertyBool::~PropertyBool() = default; |
| 2024 |
|
| 2025 |
//************************************************************************** |
| 2026 |
// Setter/getter for the property |
| 2027 |
|
| 2028 |
void PropertyBool::setValue(bool lValue) |
| 2029 |
{ |
| 2030 |
aboutToSetValue(); |
| 2031 |
_lValue = lValue; |
| 2032 |
hasSetValue(); |
| 2033 |
} |
| 2034 |
|
| 2035 |
bool PropertyBool::getValue() const |
| 2036 |
{ |
| 2037 |
return _lValue; |
| 2038 |
} |
| 2039 |
|
| 2040 |
PyObject* PropertyBool::getPyObject() |
| 2041 |
{ |
| 2042 |
return PyBool_FromLong(_lValue ? 1 : 0); |
| 2043 |
} |
| 2044 |
|
| 2045 |
void PropertyBool::setPyObject(PyObject* value) |
| 2046 |
{ |
| 2047 |
if (PyBool_Check(value) || PyLong_Check(value)) { |
| 2048 |
setValue(Base::asBoolean(value)); |
| 2049 |
} |
| 2050 |
else { |
| 2051 |
std::string error = std::string("type must be bool, not "); |
| 2052 |
error += value->ob_type->tp_name; |
| 2053 |
throw Base::TypeError(error); |
| 2054 |
} |
| 2055 |
} |
| 2056 |
|
| 2057 |
void PropertyBool::Save(Base::Writer& writer) const |
| 2058 |
{ |
| 2059 |
writer.Stream() << writer.ind() << "<Bool value=\""; |
| 2060 |
if (_lValue) { |
| 2061 |
writer.Stream() << "true" << "\"/>"; |
| 2062 |
} |
| 2063 |
else { |
| 2064 |
writer.Stream() << "false" << "\"/>"; |
| 2065 |
} |
| 2066 |
writer.Stream() << std::endl; |
| 2067 |
} |
| 2068 |
|
| 2069 |
void PropertyBool::Restore(Base::XMLReader& reader) |
| 2070 |
{ |
| 2071 |
// read my Element |
| 2072 |
reader.readElement("Bool"); |
| 2073 |
// get the value of my Attribute |
| 2074 |
string b = reader.getAttribute<const char*>("value"); |
| 2075 |
(b == "true") ? setValue(true) : setValue(false); |
| 2076 |
} |
| 2077 |
|
| 2078 |
|
| 2079 |
Property* PropertyBool::Copy() const |
| 2080 |
{ |
| 2081 |
PropertyBool* p = new PropertyBool(); |
| 2082 |
p->_lValue = _lValue; |
| 2083 |
return p; |
| 2084 |
} |
| 2085 |
|
| 2086 |
void PropertyBool::Paste(const Property& from) |
| 2087 |
{ |
| 2088 |
aboutToSetValue(); |
| 2089 |
_lValue = dynamic_cast<const PropertyBool&>(from)._lValue; |
| 2090 |
hasSetValue(); |
| 2091 |
} |
| 2092 |
|
| 2093 |
void PropertyBool::setPathValue(const ObjectIdentifier& path, const boost::any& value) |
| 2094 |
{ |
| 2095 |
verifyPath(path); |
| 2096 |
|
| 2097 |
if (value.type() == typeid(bool)) { |
| 2098 |
setValue(boost::any_cast<bool>(value)); |
| 2099 |
} |
| 2100 |
else if (value.type() == typeid(int)) { |
| 2101 |
setValue(boost::any_cast<int>(value) != 0); |
| 2102 |
} |
| 2103 |
else if (value.type() == typeid(long)) { |
| 2104 |
setValue(boost::any_cast<long>(value) != 0); |
| 2105 |
} |
| 2106 |
else if (value.type() == typeid(double)) { |
| 2107 |
setValue(boost::math::round(boost::any_cast<double>(value))); |
| 2108 |
} |
| 2109 |
else if (value.type() == typeid(float)) { |
| 2110 |
setValue(boost::math::round(boost::any_cast<float>(value))); |
| 2111 |
} |
| 2112 |
else if (value.type() == typeid(Quantity)) { |
| 2113 |
setValue(boost::any_cast<Quantity>(value).getValue() != 0); |
| 2114 |
} |
| 2115 |
else { |
| 2116 |
throw bad_cast(); |
| 2117 |
} |
| 2118 |
} |
| 2119 |
|
| 2120 |
const boost::any PropertyBool::getPathValue(const ObjectIdentifier& path) const |
| 2121 |
{ |
| 2122 |
verifyPath(path); |
| 2123 |
|
| 2124 |
return _lValue; |
| 2125 |
} |
| 2126 |
|
| 2127 |
//************************************************************************** |
| 2128 |
//************************************************************************** |
| 2129 |
// PropertyBoolList |
| 2130 |
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| 2131 |
|
| 2132 |
TYPESYSTEM_SOURCE(App::PropertyBoolList, App::PropertyLists) |
| 2133 |
|
| 2134 |
//************************************************************************** |
| 2135 |
// Construction/Destruction |
| 2136 |
|
| 2137 |
|
| 2138 |
PropertyBoolList::PropertyBoolList() = default; |
| 2139 |
|
| 2140 |
PropertyBoolList::~PropertyBoolList() = default; |
| 2141 |
|
| 2142 |
//************************************************************************** |
| 2143 |
// Base class implementer |
| 2144 |
|
| 2145 |
PyObject* PropertyBoolList::getPyObject() |
| 2146 |
{ |
| 2147 |
PyObject* tuple = PyTuple_New(getSize()); |
| 2148 |
for (int i = 0; i < getSize(); i++) { |
| 2149 |
bool v = _lValueList[i]; |
| 2150 |
if (v) { |
| 2151 |
PyTuple_SetItem(tuple, i, PyBool_FromLong(1)); |
| 2152 |
} |
| 2153 |
else { |
| 2154 |
PyTuple_SetItem(tuple, i, PyBool_FromLong(0)); |
| 2155 |
} |
| 2156 |
} |
| 2157 |
return tuple; |
| 2158 |
} |
| 2159 |
|
| 2160 |
void PropertyBoolList::setPyObject(PyObject* value) |
| 2161 |
{ |
| 2162 |
// string is also a sequence and must be treated differently |
| 2163 |
std::string str; |
| 2164 |
if (PyUnicode_Check(value)) { |
| 2165 |
str = PyUnicode_AsUTF8(value); |
| 2166 |
boost::dynamic_bitset<> values(str); |
| 2167 |
setValues(values); |
| 2168 |
} |
| 2169 |
else { |
| 2170 |
inherited::setPyObject(value); |
| 2171 |
} |
| 2172 |
} |
| 2173 |
|
| 2174 |
bool PropertyBoolList::getPyValue(PyObject* item) const |
| 2175 |
{ |
| 2176 |
if (PyBool_Check(item)) { |
| 2177 |
return Base::asBoolean(item); |
| 2178 |
} |
| 2179 |
else if (PyLong_Check(item)) { |
| 2180 |
return (PyLong_AsLong(item) ? true : false); |
| 2181 |
} |
| 2182 |
else { |
| 2183 |
std::string error = std::string("type in list must be bool or int, not "); |
| 2184 |
error += item->ob_type->tp_name; |
| 2185 |
throw Base::TypeError(error); |
| 2186 |
} |
| 2187 |
} |
| 2188 |
|
| 2189 |
void PropertyBoolList::Save(Base::Writer& writer) const |
| 2190 |
{ |
| 2191 |
writer.Stream() << writer.ind() << "<BoolList value=\""; |
| 2192 |
std::string bitset; |
| 2193 |
boost::to_string(_lValueList, bitset); |
| 2194 |
writer.Stream() << bitset << "\"/>"; |
| 2195 |
writer.Stream() << std::endl; |
| 2196 |
} |
| 2197 |
|
| 2198 |
void PropertyBoolList::Restore(Base::XMLReader& reader) |
| 2199 |
{ |
| 2200 |
// read my Element |
| 2201 |
reader.readElement("BoolList"); |
| 2202 |
// get the value of my Attribute |
| 2203 |
string str = reader.getAttribute<const char*>("value"); |
| 2204 |
boost::dynamic_bitset<> bitset(str); |
| 2205 |
setValues(bitset); |
| 2206 |
} |
| 2207 |
|
| 2208 |
Property* PropertyBoolList::Copy() const |
| 2209 |
{ |
| 2210 |
PropertyBoolList* p = new PropertyBoolList(); |
| 2211 |
p->_lValueList = _lValueList; |
| 2212 |
return p; |
| 2213 |
} |
| 2214 |
|
| 2215 |
void PropertyBoolList::Paste(const Property& from) |
| 2216 |
{ |
| 2217 |
setValues(dynamic_cast<const PropertyBoolList&>(from)._lValueList); |
| 2218 |
} |
| 2219 |
|
| 2220 |
unsigned int PropertyBoolList::getMemSize() const |
| 2221 |
{ |
| 2222 |
return static_cast<unsigned int>(_lValueList.size()); |
| 2223 |
} |
| 2224 |
|
| 2225 |
//************************************************************************** |
| 2226 |
//************************************************************************** |
| 2227 |
// PropertyColor |
| 2228 |
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| 2229 |
|
| 2230 |
namespace |
| 2231 |
{ |
| 2232 |
/// The definition of "alpha" was corrected in FreeCAD 1.1 -- returns true if the reader is working |
| 2233 |
/// on a file that pre-dates that correction. |
| 2234 |
bool readerRequiresAlphaConversion(const Base::XMLReader &reader) |
| 2235 |
{ |
| 2236 |
return Base::getVersion(reader.ProgramVersion) < Base::Version::v1_1; |
| 2237 |
} |
| 2238 |
|
| 2239 |
/// Given a material, invert the alpha channel of all of its colors. |
| 2240 |
void convertAlphaInMaterial(App::Material& material) |
| 2241 |
{ |
| 2242 |
material.ambientColor.a = 1.0F - material.ambientColor.a; |
| 2243 |
material.diffuseColor.a = 1.0F - material.diffuseColor.a; |
| 2244 |
material.specularColor.a = 1.0F - material.specularColor.a; |
| 2245 |
material.emissiveColor.a = 1.0F - material.emissiveColor.a; |
| 2246 |
} |
| 2247 |
} |
| 2248 |
|
| 2249 |
TYPESYSTEM_SOURCE(App::PropertyColor, App::Property) |
| 2250 |
|
| 2251 |
//************************************************************************** |
| 2252 |
// Construction/Destruction |
| 2253 |
|
| 2254 |
PropertyColor::PropertyColor() = default; |
| 2255 |
|
| 2256 |
PropertyColor::~PropertyColor() = default; |
| 2257 |
|
| 2258 |
//************************************************************************** |
| 2259 |
// Base class implementer |
| 2260 |
|
| 2261 |
void PropertyColor::setValue(const Base::Color& col) |
| 2262 |
{ |
| 2263 |
aboutToSetValue(); |
| 2264 |
_cCol = col; |
| 2265 |
hasSetValue(); |
| 2266 |
} |
| 2267 |
|
| 2268 |
void PropertyColor::setValue(uint32_t rgba) |
| 2269 |
{ |
| 2270 |
aboutToSetValue(); |
| 2271 |
_cCol.setPackedValue(rgba); |
| 2272 |
hasSetValue(); |
| 2273 |
} |
| 2274 |
|
| 2275 |
void PropertyColor::setValue(float r, float g, float b, float a) |
| 2276 |
{ |
| 2277 |
aboutToSetValue(); |
| 2278 |
_cCol.set(r, g, b, a); |
| 2279 |
hasSetValue(); |
| 2280 |
} |
| 2281 |
|
| 2282 |
const Base::Color& PropertyColor::getValue() const |
| 2283 |
{ |
| 2284 |
return _cCol; |
| 2285 |
} |
| 2286 |
|
| 2287 |
PyObject* PropertyColor::getPyObject() |
| 2288 |
{ |
| 2289 |
PyObject* rgba = PyTuple_New(4); |
| 2290 |
PyObject* r = PyFloat_FromDouble(_cCol.r); |
| 2291 |
PyObject* g = PyFloat_FromDouble(_cCol.g); |
| 2292 |
PyObject* b = PyFloat_FromDouble(_cCol.b); |
| 2293 |
PyObject* a = PyFloat_FromDouble(_cCol.a); |
| 2294 |
|
| 2295 |
PyTuple_SetItem(rgba, 0, r); |
| 2296 |
PyTuple_SetItem(rgba, 1, g); |
| 2297 |
PyTuple_SetItem(rgba, 2, b); |
| 2298 |
PyTuple_SetItem(rgba, 3, a); |
| 2299 |
|
| 2300 |
return rgba; |
| 2301 |
} |
| 2302 |
|
| 2303 |
void PropertyColor::setPyObject(PyObject* value) |
| 2304 |
{ |
| 2305 |
Base::Color cCol; |
| 2306 |
if (PyTuple_Check(value) && (PyTuple_Size(value) == 3 || PyTuple_Size(value) == 4)) { |
| 2307 |
PyObject* item; |
| 2308 |
item = PyTuple_GetItem(value, 0); |
| 2309 |
if (PyFloat_Check(item)) { |
| 2310 |
cCol.r = (float)PyFloat_AsDouble(item); |
| 2311 |
item = PyTuple_GetItem(value, 1); |
| 2312 |
if (PyFloat_Check(item)) { |
| 2313 |
cCol.g = (float)PyFloat_AsDouble(item); |
| 2314 |
} |
| 2315 |
else { |
| 2316 |
throw Base::TypeError("Type in tuple must be consistent (float)"); |
| 2317 |
} |
| 2318 |
item = PyTuple_GetItem(value, 2); |
| 2319 |
if (PyFloat_Check(item)) { |
| 2320 |
cCol.b = (float)PyFloat_AsDouble(item); |
| 2321 |
} |
| 2322 |
else { |
| 2323 |
throw Base::TypeError("Type in tuple must be consistent (float)"); |
| 2324 |
} |
| 2325 |
if (PyTuple_Size(value) == 4) { |
| 2326 |
item = PyTuple_GetItem(value, 3); |
| 2327 |
if (PyFloat_Check(item)) { |
| 2328 |
cCol.a = (float)PyFloat_AsDouble(item); |
| 2329 |
} |
| 2330 |
else { |
| 2331 |
throw Base::TypeError("Type in tuple must be consistent (float)"); |
| 2332 |
} |
| 2333 |
} |
| 2334 |
} |
| 2335 |
else if (PyLong_Check(item)) { |
| 2336 |
cCol.r = PyLong_AsLong(item) / 255.0; |
| 2337 |
item = PyTuple_GetItem(value, 1); |
| 2338 |
if (PyLong_Check(item)) { |
| 2339 |
cCol.g = PyLong_AsLong(item) / 255.0; |
| 2340 |
} |
| 2341 |
else { |
| 2342 |
throw Base::TypeError("Type in tuple must be consistent (integer)"); |
| 2343 |
} |
| 2344 |
item = PyTuple_GetItem(value, 2); |
| 2345 |
if (PyLong_Check(item)) { |
| 2346 |
cCol.b = PyLong_AsLong(item) / 255.0; |
| 2347 |
} |
| 2348 |
else { |
| 2349 |
throw Base::TypeError("Type in tuple must be consistent (integer)"); |
| 2350 |
} |
| 2351 |
if (PyTuple_Size(value) == 4) { |
| 2352 |
item = PyTuple_GetItem(value, 3); |
| 2353 |
if (PyLong_Check(item)) { |
| 2354 |
cCol.a = PyLong_AsLong(item) / 255.0; |
| 2355 |
} |
| 2356 |
else { |
| 2357 |
throw Base::TypeError("Type in tuple must be consistent (integer)"); |
| 2358 |
} |
| 2359 |
} |
| 2360 |
} |
| 2361 |
else { |
| 2362 |
throw Base::TypeError("Type in tuple must be float or integer"); |
| 2363 |
} |
| 2364 |
} |
| 2365 |
else if (PyLong_Check(value)) { |
| 2366 |
cCol.setPackedValue(PyLong_AsUnsignedLong(value)); |
| 2367 |
} |
| 2368 |
else { |
| 2369 |
std::string error = |
| 2370 |
std::string("type must be integer or tuple of float or tuple integer, not "); |
| 2371 |
error += value->ob_type->tp_name; |
| 2372 |
throw Base::TypeError(error); |
| 2373 |
} |
| 2374 |
|
| 2375 |
setValue(cCol); |
| 2376 |
} |
| 2377 |
|
| 2378 |
void PropertyColor::Save(Base::Writer& writer) const |
| 2379 |
{ |
| 2380 |
writer.Stream() << writer.ind() << "<PropertyColor value=\"" << _cCol.getPackedValue() << "\"/>" |
| 2381 |
<< endl; |
| 2382 |
} |
| 2383 |
|
| 2384 |
void PropertyColor::Restore(Base::XMLReader& reader) |
| 2385 |
{ |
| 2386 |
// read my Element |
| 2387 |
reader.readElement("PropertyColor"); |
| 2388 |
// get the value of my Attribute |
| 2389 |
unsigned long rgba = reader.getAttribute<unsigned long>("value"); |
| 2390 |
if (readerRequiresAlphaConversion(reader)) { |
| 2391 |
// Convert transparency / alpha value |
| 2392 |
constexpr unsigned long alphaMax = 0xff; |
| 2393 |
unsigned long alpha = alphaMax - (rgba & alphaMax); |
| 2394 |
rgba = rgba - (rgba & alphaMax) + alpha; |
| 2395 |
} |
| 2396 |
setValue(rgba); |
| 2397 |
} |
| 2398 |
|
| 2399 |
Property* PropertyColor::Copy() const |
| 2400 |
{ |
| 2401 |
PropertyColor* p = new PropertyColor(); |
| 2402 |
p->_cCol = _cCol; |
| 2403 |
return p; |
| 2404 |
} |
| 2405 |
|
| 2406 |
void PropertyColor::Paste(const Property& from) |
| 2407 |
{ |
| 2408 |
aboutToSetValue(); |
| 2409 |
_cCol = dynamic_cast<const PropertyColor&>(from)._cCol; |
| 2410 |
hasSetValue(); |
| 2411 |
} |
| 2412 |
|
| 2413 |
//************************************************************************** |
| 2414 |
// PropertyColorList |
| 2415 |
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| 2416 |
|
| 2417 |
TYPESYSTEM_SOURCE(App::PropertyColorList, App::PropertyLists) |
| 2418 |
|
| 2419 |
//************************************************************************** |
| 2420 |
// Construction/Destruction |
| 2421 |
|
| 2422 |
PropertyColorList::PropertyColorList() = default; |
| 2423 |
|
| 2424 |
PropertyColorList::~PropertyColorList() = default; |
| 2425 |
|
| 2426 |
//************************************************************************** |
| 2427 |
// Base class implementer |
| 2428 |
|
| 2429 |
PyObject* PropertyColorList::getPyObject() |
| 2430 |
{ |
| 2431 |
PyObject* list = PyList_New(getSize()); |
| 2432 |
|
| 2433 |
for (int i = 0; i < getSize(); i++) { |
| 2434 |
PyObject* rgba = PyTuple_New(4); |
| 2435 |
PyObject* r = PyFloat_FromDouble(_lValueList[i].r); |
| 2436 |
PyObject* g = PyFloat_FromDouble(_lValueList[i].g); |
| 2437 |
PyObject* b = PyFloat_FromDouble(_lValueList[i].b); |
| 2438 |
PyObject* a = PyFloat_FromDouble(_lValueList[i].a); |
| 2439 |
|
| 2440 |
PyTuple_SetItem(rgba, 0, r); |
| 2441 |
PyTuple_SetItem(rgba, 1, g); |
| 2442 |
PyTuple_SetItem(rgba, 2, b); |
| 2443 |
PyTuple_SetItem(rgba, 3, a); |
| 2444 |
|
| 2445 |
PyList_SetItem(list, i, rgba); |
| 2446 |
} |
| 2447 |
|
| 2448 |
return list; |
| 2449 |
} |
| 2450 |
|
| 2451 |
Base::Color PropertyColorList::getPyValue(PyObject* item) const |
| 2452 |
{ |
| 2453 |
PropertyColor col; |
| 2454 |
col.setPyObject(item); |
| 2455 |
return col.getValue(); |
| 2456 |
} |
| 2457 |
|
| 2458 |
void PropertyColorList::Save(Base::Writer& writer) const |
| 2459 |
{ |
| 2460 |
if (!writer.isForceXML()) { |
| 2461 |
writer.Stream() << writer.ind() << "<ColorList file=\"" |
| 2462 |
<< (getSize() ? writer.addFile(getName(), this) : "") << "\"/>" |
| 2463 |
<< std::endl; |
| 2464 |
} |
| 2465 |
} |
| 2466 |
|
| 2467 |
void PropertyColorList::Restore(Base::XMLReader& reader) |
| 2468 |
{ |
| 2469 |
reader.readElement("ColorList"); |
| 2470 |
if (reader.hasAttribute("file")) { |
| 2471 |
std::string file(reader.getAttribute<const char*>("file")); |
| 2472 |
|
| 2473 |
if (!file.empty()) { |
| 2474 |
// initiate a file read |
| 2475 |
reader.addFile(file.c_str(), this); |
| 2476 |
} |
| 2477 |
|
| 2478 |
requiresAlphaConversion = readerRequiresAlphaConversion(reader); |
| 2479 |
} |
| 2480 |
} |
| 2481 |
|
| 2482 |
void PropertyColorList::SaveDocFile(Base::Writer& writer) const |
| 2483 |
{ |
| 2484 |
Base::OutputStream str(writer.Stream()); |
| 2485 |
uint32_t uCt = (uint32_t)getSize(); |
| 2486 |
str << uCt; |
| 2487 |
for (auto it : _lValueList) { |
| 2488 |
str << it.getPackedValue(); |
| 2489 |
} |
| 2490 |
} |
| 2491 |
|
| 2492 |
void PropertyColorList::RestoreDocFile(Base::Reader& reader) |
| 2493 |
{ |
| 2494 |
Base::InputStream str(reader); |
| 2495 |
uint32_t uCt = 0; |
| 2496 |
str >> uCt; |
| 2497 |
std::vector<Base::Color> values(uCt); |
| 2498 |
uint32_t value; // must be 32 bit long |
| 2499 |
for (auto& it : values) { |
| 2500 |
str >> value; |
| 2501 |
it.setPackedValue(value); |
| 2502 |
} |
| 2503 |
if (requiresAlphaConversion) { |
| 2504 |
for (auto& it : values) { |
| 2505 |
it.a = 1.0F - it.a; |
| 2506 |
} |
| 2507 |
} |
| 2508 |
setValues(values); |
| 2509 |
} |
| 2510 |
|
| 2511 |
Property* PropertyColorList::Copy() const |
| 2512 |
{ |
| 2513 |
PropertyColorList* p = new PropertyColorList(); |
| 2514 |
p->_lValueList = _lValueList; |
| 2515 |
return p; |
| 2516 |
} |
| 2517 |
|
| 2518 |
void PropertyColorList::Paste(const Property& from) |
| 2519 |
{ |
| 2520 |
setValues(dynamic_cast<const PropertyColorList&>(from)._lValueList); |
| 2521 |
} |
| 2522 |
|
| 2523 |
unsigned int PropertyColorList::getMemSize() const |
| 2524 |
{ |
| 2525 |
return static_cast<unsigned int>(_lValueList.size() * sizeof(Base::Color)); |
| 2526 |
} |
| 2527 |
|
| 2528 |
//************************************************************************** |
| 2529 |
//************************************************************************** |
| 2530 |
// PropertyMaterial |
| 2531 |
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| 2532 |
|
| 2533 |
TYPESYSTEM_SOURCE(App::PropertyMaterial, App::Property) |
| 2534 |
|
| 2535 |
PropertyMaterial::PropertyMaterial() = default; |
| 2536 |
|
| 2537 |
PropertyMaterial::~PropertyMaterial() = default; |
| 2538 |
|
| 2539 |
void PropertyMaterial::setValue(const Material& mat) |
| 2540 |
{ |
| 2541 |
aboutToSetValue(); |
| 2542 |
_cMat = mat; |
| 2543 |
hasSetValue(); |
| 2544 |
} |
| 2545 |
|
| 2546 |
void PropertyMaterial::setValue(const Base::Color& col) |
| 2547 |
{ |
| 2548 |
setDiffuseColor(col); |
| 2549 |
} |
| 2550 |
|
| 2551 |
void PropertyMaterial::setValue(float r, float g, float b, float a) |
| 2552 |
{ |
| 2553 |
setDiffuseColor(r, g, b, a); |
| 2554 |
} |
| 2555 |
|
| 2556 |
void PropertyMaterial::setValue(uint32_t rgba) |
| 2557 |
{ |
| 2558 |
setDiffuseColor(rgba); |
| 2559 |
} |
| 2560 |
|
| 2561 |
const Material& PropertyMaterial::getValue() const |
| 2562 |
{ |
| 2563 |
return _cMat; |
| 2564 |
} |
| 2565 |
|
| 2566 |
void PropertyMaterial::setAmbientColor(const Base::Color& col) |
| 2567 |
{ |
| 2568 |
aboutToSetValue(); |
| 2569 |
_cMat.ambientColor = col; |
| 2570 |
hasSetValue(); |
| 2571 |
} |
| 2572 |
|
| 2573 |
void PropertyMaterial::setAmbientColor(float r, float g, float b, float a) |
| 2574 |
{ |
| 2575 |
aboutToSetValue(); |
| 2576 |
_cMat.ambientColor.set(r, g, b, a); |
| 2577 |
hasSetValue(); |
| 2578 |
} |
| 2579 |
|
| 2580 |
void PropertyMaterial::setAmbientColor(uint32_t rgba) |
| 2581 |
{ |
| 2582 |
aboutToSetValue(); |
| 2583 |
_cMat.ambientColor.setPackedValue(rgba); |
| 2584 |
hasSetValue(); |
| 2585 |
} |
| 2586 |
|
| 2587 |
void PropertyMaterial::setDiffuseColor(const Base::Color& col) |
| 2588 |
{ |
| 2589 |
aboutToSetValue(); |
| 2590 |
_cMat.diffuseColor = col; |
| 2591 |
hasSetValue(); |
| 2592 |
} |
| 2593 |
|
| 2594 |
void PropertyMaterial::setDiffuseColor(float r, float g, float b, float a) |
| 2595 |
{ |
| 2596 |
aboutToSetValue(); |
| 2597 |
_cMat.diffuseColor.set(r, g, b, a); |
| 2598 |
hasSetValue(); |
| 2599 |
} |
| 2600 |
|
| 2601 |
void PropertyMaterial::setDiffuseColor(uint32_t rgba) |
| 2602 |
{ |
| 2603 |
aboutToSetValue(); |
| 2604 |
_cMat.diffuseColor.setPackedValue(rgba); |
| 2605 |
hasSetValue(); |
| 2606 |
} |
| 2607 |
|
| 2608 |
void PropertyMaterial::setSpecularColor(const Base::Color& col) |
| 2609 |
{ |
| 2610 |
aboutToSetValue(); |
| 2611 |
_cMat.specularColor = col; |
| 2612 |
hasSetValue(); |
| 2613 |
} |
| 2614 |
|
| 2615 |
void PropertyMaterial::setSpecularColor(float r, float g, float b, float a) |
| 2616 |
{ |
| 2617 |
aboutToSetValue(); |
| 2618 |
_cMat.specularColor.set(r, g, b, a); |
| 2619 |
hasSetValue(); |
| 2620 |
} |
| 2621 |
|
| 2622 |
void PropertyMaterial::setSpecularColor(uint32_t rgba) |
| 2623 |
{ |
| 2624 |
aboutToSetValue(); |
| 2625 |
_cMat.specularColor.setPackedValue(rgba); |
| 2626 |
hasSetValue(); |
| 2627 |
} |
| 2628 |
|
| 2629 |
void PropertyMaterial::setEmissiveColor(const Base::Color& col) |
| 2630 |
{ |
| 2631 |
aboutToSetValue(); |
| 2632 |
_cMat.emissiveColor = col; |
| 2633 |
hasSetValue(); |
| 2634 |
} |
| 2635 |
|
| 2636 |
void PropertyMaterial::setEmissiveColor(float r, float g, float b, float a) |
| 2637 |
{ |
| 2638 |
aboutToSetValue(); |
| 2639 |
_cMat.emissiveColor.set(r, g, b, a); |
| 2640 |
hasSetValue(); |
| 2641 |
} |
| 2642 |
|
| 2643 |
void PropertyMaterial::setEmissiveColor(uint32_t rgba) |
| 2644 |
{ |
| 2645 |
aboutToSetValue(); |
| 2646 |
_cMat.emissiveColor.setPackedValue(rgba); |
| 2647 |
hasSetValue(); |
| 2648 |
} |
| 2649 |
|
| 2650 |
void PropertyMaterial::setShininess(float val) |
| 2651 |
{ |
| 2652 |
aboutToSetValue(); |
| 2653 |
_cMat.shininess = val; |
| 2654 |
hasSetValue(); |
| 2655 |
} |
| 2656 |
|
| 2657 |
void PropertyMaterial::setTransparency(float val) |
| 2658 |
{ |
| 2659 |
aboutToSetValue(); |
| 2660 |
_cMat.transparency = val; |
| 2661 |
hasSetValue(); |
| 2662 |
} |
| 2663 |
|
| 2664 |
const Base::Color& PropertyMaterial::getAmbientColor() const |
| 2665 |
{ |
| 2666 |
return _cMat.ambientColor; |
| 2667 |
} |
| 2668 |
|
| 2669 |
const Base::Color& PropertyMaterial::getDiffuseColor() const |
| 2670 |
{ |
| 2671 |
return _cMat.diffuseColor; |
| 2672 |
} |
| 2673 |
|
| 2674 |
const Base::Color& PropertyMaterial::getSpecularColor() const |
| 2675 |
{ |
| 2676 |
return _cMat.specularColor; |
| 2677 |
} |
| 2678 |
|
| 2679 |
const Base::Color& PropertyMaterial::getEmissiveColor() const |
| 2680 |
{ |
| 2681 |
return _cMat.emissiveColor; |
| 2682 |
} |
| 2683 |
|
| 2684 |
double PropertyMaterial::getShininess() const |
| 2685 |
{ |
| 2686 |
return _cMat.shininess; |
| 2687 |
} |
| 2688 |
|
| 2689 |
double PropertyMaterial::getTransparency() const |
| 2690 |
{ |
| 2691 |
return _cMat.transparency; |
| 2692 |
} |
| 2693 |
|
| 2694 |
PyObject* PropertyMaterial::getPyObject() |
| 2695 |
{ |
| 2696 |
return new MaterialPy(new Material(_cMat)); |
| 2697 |
} |
| 2698 |
|
| 2699 |
void PropertyMaterial::setPyObject(PyObject* value) |
| 2700 |
{ |
| 2701 |
if (PyObject_TypeCheck(value, &(MaterialPy::Type))) { |
| 2702 |
setValue(*static_cast<MaterialPy*>(value)->getMaterialPtr()); |
| 2703 |
} |
| 2704 |
else { |
| 2705 |
setValue(MaterialPy::toColor(value)); |
| 2706 |
} |
| 2707 |
} |
| 2708 |
|
| 2709 |
void PropertyMaterial::Save(Base::Writer& writer) const |
| 2710 |
{ |
| 2711 |
// clang-format off |
| 2712 |
writer.Stream() << writer.ind() |
| 2713 |
<< "<PropertyMaterial ambientColor=\"" << _cMat.ambientColor.getPackedValue() |
| 2714 |
<< "\" diffuseColor=\"" << _cMat.diffuseColor.getPackedValue() |
| 2715 |
<< "\" specularColor=\"" << _cMat.specularColor.getPackedValue() |
| 2716 |
<< "\" emissiveColor=\"" << _cMat.emissiveColor.getPackedValue() |
| 2717 |
<< "\" shininess=\"" << _cMat.shininess |
| 2718 |
<< "\" transparency=\"" << _cMat.transparency |
| 2719 |
<< "\" image=\"" << _cMat.image |
| 2720 |
<< "\" imagePath=\"" << _cMat.imagePath |
| 2721 |
<< "\" uuid=\"" << _cMat.uuid |
| 2722 |
<< "\"/>" << std::endl; |
| 2723 |
// clang-format on |
| 2724 |
} |
| 2725 |
|
| 2726 |
void PropertyMaterial::Restore(Base::XMLReader& reader) |
| 2727 |
{ |
| 2728 |
// read my Element |
| 2729 |
reader.readElement("PropertyMaterial"); |
| 2730 |
// get the value of my Attribute |
| 2731 |
aboutToSetValue(); |
| 2732 |
_cMat.ambientColor.setPackedValue(reader.getAttribute<unsigned long>("ambientColor")); |
| 2733 |
_cMat.diffuseColor.setPackedValue(reader.getAttribute<unsigned long>("diffuseColor")); |
| 2734 |
_cMat.specularColor.setPackedValue(reader.getAttribute<unsigned long>("specularColor")); |
| 2735 |
_cMat.emissiveColor.setPackedValue(reader.getAttribute<unsigned long>("emissiveColor")); |
| 2736 |
_cMat.shininess = (float)reader.getAttribute<double>("shininess"); |
| 2737 |
_cMat.transparency = (float)reader.getAttribute<double>("transparency"); |
| 2738 |
if (readerRequiresAlphaConversion(reader)) { |
| 2739 |
convertAlphaInMaterial(_cMat); |
| 2740 |
} |
| 2741 |
if (reader.hasAttribute("image")) { |
| 2742 |
_cMat.image = reader.getAttribute<const char*>("image"); |
| 2743 |
} |
| 2744 |
if (reader.hasAttribute("imagePath")) { |
| 2745 |
_cMat.imagePath = reader.getAttribute<const char*>("imagePath"); |
| 2746 |
} |
| 2747 |
if (reader.hasAttribute("uuid")) { |
| 2748 |
_cMat.uuid = reader.getAttribute<const char*>("uuid"); |
| 2749 |
} |
| 2750 |
hasSetValue(); |
| 2751 |
} |
| 2752 |
|
| 2753 |
const char* PropertyMaterial::getEditorName() const |
| 2754 |
{ |
| 2755 |
if (testStatus(MaterialEdit)) { |
| 2756 |
return "Gui::PropertyEditor::PropertyMaterialItem"; |
| 2757 |
} |
| 2758 |
return ""; |
| 2759 |
} |
| 2760 |
|
| 2761 |
Property* PropertyMaterial::Copy() const |
| 2762 |
{ |
| 2763 |
PropertyMaterial* p = new PropertyMaterial(); |
| 2764 |
p->_cMat = _cMat; |
| 2765 |
return p; |
| 2766 |
} |
| 2767 |
|
| 2768 |
void PropertyMaterial::Paste(const Property& from) |
| 2769 |
{ |
| 2770 |
aboutToSetValue(); |
| 2771 |
_cMat = dynamic_cast<const PropertyMaterial&>(from)._cMat; |
| 2772 |
hasSetValue(); |
| 2773 |
} |
| 2774 |
|
| 2775 |
//************************************************************************** |
| 2776 |
// PropertyMaterialList |
| 2777 |
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| 2778 |
|
| 2779 |
TYPESYSTEM_SOURCE(App::PropertyMaterialList, App::PropertyLists) |
| 2780 |
|
| 2781 |
//************************************************************************** |
| 2782 |
// Construction/Destruction |
| 2783 |
|
| 2784 |
PropertyMaterialList::PropertyMaterialList() |
| 2785 |
{ |
| 2786 |
setMinimumSizeOne(); |
| 2787 |
} |
| 2788 |
|
| 2789 |
PropertyMaterialList::~PropertyMaterialList() = default; |
| 2790 |
|
| 2791 |
//************************************************************************** |
| 2792 |
// Base class implementer |
| 2793 |
|
| 2794 |
void PropertyMaterialList::setValues(const std::vector<App::Material>& newValues) |
| 2795 |
{ |
| 2796 |
if (!newValues.empty()) { |
| 2797 |
PropertyListsT<Material>::setValues(newValues); |
| 2798 |
} |
| 2799 |
else { |
| 2800 |
aboutToSetValue(); |
| 2801 |
setSize(1); |
| 2802 |
hasSetValue(); |
| 2803 |
} |
| 2804 |
} |
| 2805 |
|
| 2806 |
PyObject* PropertyMaterialList::getPyObject() |
| 2807 |
{ |
| 2808 |
Py::Tuple tuple(getSize()); |
| 2809 |
|
| 2810 |
for (int i = 0; i < getSize(); i++) { |
| 2811 |
tuple.setItem(i, Py::asObject(new MaterialPy(new Material(_lValueList[i])))); |
| 2812 |
} |
| 2813 |
|
| 2814 |
return Py::new_reference_to(tuple); |
| 2815 |
} |
| 2816 |
|
| 2817 |
void PropertyMaterialList::verifyIndex(int index) const |
| 2818 |
{ |
| 2819 |
int size = getSize(); |
| 2820 |
if (index < -1 || index > size) { |
| 2821 |
throw Base::RuntimeError("index out of bound"); |
| 2822 |
} |
| 2823 |
} |
| 2824 |
|
| 2825 |
void PropertyMaterialList::setMinimumSizeOne() |
| 2826 |
{ |
| 2827 |
int size = getSize(); |
| 2828 |
if (size < 1) { |
| 2829 |
setSize(1); |
| 2830 |
} |
| 2831 |
} |
| 2832 |
|
| 2833 |
int PropertyMaterialList::resizeByOneIfNeeded(int index) |
| 2834 |
{ |
| 2835 |
int size = getSize(); |
| 2836 |
if (index == -1 || index == size) { |
| 2837 |
index = size; |
| 2838 |
setSize(size + 1); |
| 2839 |
} |
| 2840 |
|
| 2841 |
return index; |
| 2842 |
} |
| 2843 |
|
| 2844 |
void PropertyMaterialList::setValue() |
| 2845 |
{ |
| 2846 |
Material empty; |
| 2847 |
setValue(empty); |
| 2848 |
} |
| 2849 |
|
| 2850 |
void PropertyMaterialList::setValue(const Material& mat) |
| 2851 |
{ |
| 2852 |
aboutToSetValue(); |
| 2853 |
setSize(1); |
| 2854 |
for (auto& material : _lValueList) { |
| 2855 |
material = mat; |
| 2856 |
} |
| 2857 |
hasSetValue(); |
| 2858 |
} |
| 2859 |
|
| 2860 |
void PropertyMaterialList::setValue(int index, const Material& mat) |
| 2861 |
{ |
| 2862 |
verifyIndex(index); |
| 2863 |
|
| 2864 |
aboutToSetValue(); |
| 2865 |
index = resizeByOneIfNeeded(index); |
| 2866 |
_lValueList[index] = mat; |
| 2867 |
hasSetValue(); |
| 2868 |
} |
| 2869 |
|
| 2870 |
void PropertyMaterialList::setAmbientColor(const Base::Color& col) |
| 2871 |
{ |
| 2872 |
aboutToSetValue(); |
| 2873 |
setMinimumSizeOne(); |
| 2874 |
for (auto& material : _lValueList) { |
| 2875 |
material.ambientColor = col; |
| 2876 |
} |
| 2877 |
hasSetValue(); |
| 2878 |
} |
| 2879 |
|
| 2880 |
void PropertyMaterialList::setAmbientColor(float r, float g, float b, float a) |
| 2881 |
{ |
| 2882 |
aboutToSetValue(); |
| 2883 |
setMinimumSizeOne(); |
| 2884 |
for (auto& material : _lValueList) { |
| 2885 |
material.ambientColor.set(r, g, b, a); |
| 2886 |
} |
| 2887 |
hasSetValue(); |
| 2888 |
} |
| 2889 |
|
| 2890 |
void PropertyMaterialList::setAmbientColor(uint32_t rgba) |
| 2891 |
{ |
| 2892 |
aboutToSetValue(); |
| 2893 |
setMinimumSizeOne(); |
| 2894 |
for (auto& material : _lValueList) { |
| 2895 |
material.ambientColor.setPackedValue(rgba); |
| 2896 |
} |
| 2897 |
hasSetValue(); |
| 2898 |
} |
| 2899 |
|
| 2900 |
void PropertyMaterialList::setAmbientColor(int index, const Base::Color& col) |
| 2901 |
{ |
| 2902 |
verifyIndex(index); |
| 2903 |
|
| 2904 |
aboutToSetValue(); |
| 2905 |
index = resizeByOneIfNeeded(index); |
| 2906 |
_lValueList[index].ambientColor = col; |
| 2907 |
hasSetValue(); |
| 2908 |
} |
| 2909 |
|
| 2910 |
void PropertyMaterialList::setAmbientColor(int index, float r, float g, float b, float a) |
| 2911 |
{ |
| 2912 |
verifyIndex(index); |
| 2913 |
|
| 2914 |
aboutToSetValue(); |
| 2915 |
index = resizeByOneIfNeeded(index); |
| 2916 |
_lValueList[index].ambientColor.set(r, g, b, a); |
| 2917 |
hasSetValue(); |
| 2918 |
} |
| 2919 |
|
| 2920 |
void PropertyMaterialList::setAmbientColor(int index, uint32_t rgba) |
| 2921 |
{ |
| 2922 |
verifyIndex(index); |
| 2923 |
|
| 2924 |
aboutToSetValue(); |
| 2925 |
index = resizeByOneIfNeeded(index); |
| 2926 |
_lValueList[index].ambientColor.setPackedValue(rgba); |
| 2927 |
hasSetValue(); |
| 2928 |
} |
| 2929 |
|
| 2930 |
void PropertyMaterialList::setDiffuseColor(const Base::Color& col) |
| 2931 |
{ |
| 2932 |
aboutToSetValue(); |
| 2933 |
setMinimumSizeOne(); |
| 2934 |
for (auto& material : _lValueList) { |
| 2935 |
material.diffuseColor = col; |
| 2936 |
} |
| 2937 |
hasSetValue(); |
| 2938 |
} |
| 2939 |
|
| 2940 |
void PropertyMaterialList::setDiffuseColor(float r, float g, float b, float a) |
| 2941 |
{ |
| 2942 |
aboutToSetValue(); |
| 2943 |
setMinimumSizeOne(); |
| 2944 |
for (auto& material : _lValueList) { |
| 2945 |
material.diffuseColor.set(r, g, b, a); |
| 2946 |
} |
| 2947 |
hasSetValue(); |
| 2948 |
} |
| 2949 |
|
| 2950 |
void PropertyMaterialList::setDiffuseColor(uint32_t rgba) |
| 2951 |
{ |
| 2952 |
aboutToSetValue(); |
| 2953 |
setMinimumSizeOne(); |
| 2954 |
for (auto& material : _lValueList) { |
| 2955 |
material.diffuseColor.setPackedValue(rgba); |
| 2956 |
} |
| 2957 |
hasSetValue(); |
| 2958 |
} |
| 2959 |
|
| 2960 |
void PropertyMaterialList::setDiffuseColor(int index, const Base::Color& col) |
| 2961 |
{ |
| 2962 |
verifyIndex(index); |
| 2963 |
|
| 2964 |
aboutToSetValue(); |
| 2965 |
index = resizeByOneIfNeeded(index); |
| 2966 |
_lValueList[index].diffuseColor = col; |
| 2967 |
hasSetValue(); |
| 2968 |
} |
| 2969 |
|
| 2970 |
void PropertyMaterialList::setDiffuseColor(int index, float r, float g, float b, float a) |
| 2971 |
{ |
| 2972 |
verifyIndex(index); |
| 2973 |
|
| 2974 |
aboutToSetValue(); |
| 2975 |
index = resizeByOneIfNeeded(index); |
| 2976 |
_lValueList[index].diffuseColor.set(r, g, b, a); |
| 2977 |
hasSetValue(); |
| 2978 |
} |
| 2979 |
|
| 2980 |
void PropertyMaterialList::setDiffuseColor(int index, uint32_t rgba) |
| 2981 |
{ |
| 2982 |
verifyIndex(index); |
| 2983 |
|
| 2984 |
aboutToSetValue(); |
| 2985 |
index = resizeByOneIfNeeded(index); |
| 2986 |
_lValueList[index].diffuseColor.setPackedValue(rgba); |
| 2987 |
hasSetValue(); |
| 2988 |
} |
| 2989 |
|
| 2990 |
void PropertyMaterialList::setDiffuseColors(const std::vector<Base::Color>& colors) |
| 2991 |
{ |
| 2992 |
aboutToSetValue(); |
| 2993 |
setSize(colors.size(), _lValueList[0]); |
| 2994 |
|
| 2995 |
for (std::size_t i = 0; i < colors.size(); i++) { |
| 2996 |
_lValueList[i].diffuseColor = colors[i]; |
| 2997 |
} |
| 2998 |
hasSetValue(); |
| 2999 |
} |
| 3000 |
|
| 3001 |
void PropertyMaterialList::setSpecularColor(const Base::Color& col) |
| 3002 |
{ |
| 3003 |
aboutToSetValue(); |
| 3004 |
setMinimumSizeOne(); |
| 3005 |
for (auto& material : _lValueList) { |
| 3006 |
material.specularColor = col; |
| 3007 |
} |
| 3008 |
hasSetValue(); |
| 3009 |
} |
| 3010 |
|
| 3011 |
void PropertyMaterialList::setSpecularColor(float r, float g, float b, float a) |
| 3012 |
{ |
| 3013 |
aboutToSetValue(); |
| 3014 |
setMinimumSizeOne(); |
| 3015 |
for (auto& material : _lValueList) { |
| 3016 |
material.specularColor.set(r, g, b, a); |
| 3017 |
} |
| 3018 |
hasSetValue(); |
| 3019 |
} |
| 3020 |
|
| 3021 |
void PropertyMaterialList::setSpecularColor(uint32_t rgba) |
| 3022 |
{ |
| 3023 |
aboutToSetValue(); |
| 3024 |
setMinimumSizeOne(); |
| 3025 |
for (auto& material : _lValueList) { |
| 3026 |
material.specularColor.setPackedValue(rgba); |
| 3027 |
} |
| 3028 |
hasSetValue(); |
| 3029 |
} |
| 3030 |
|
| 3031 |
void PropertyMaterialList::setSpecularColor(int index, const Base::Color& col) |
| 3032 |
{ |
| 3033 |
verifyIndex(index); |
| 3034 |
|
| 3035 |
aboutToSetValue(); |
| 3036 |
index = resizeByOneIfNeeded(index); |
| 3037 |
_lValueList[index].specularColor = col; |
| 3038 |
hasSetValue(); |
| 3039 |
} |
| 3040 |
|
| 3041 |
void PropertyMaterialList::setSpecularColor(int index, float r, float g, float b, float a) |
| 3042 |
{ |
| 3043 |
verifyIndex(index); |
| 3044 |
|
| 3045 |
aboutToSetValue(); |
| 3046 |
index = resizeByOneIfNeeded(index); |
| 3047 |
_lValueList[index].specularColor.set(r, g, b, a); |
| 3048 |
hasSetValue(); |
| 3049 |
} |
| 3050 |
|
| 3051 |
void PropertyMaterialList::setSpecularColor(int index, uint32_t rgba) |
| 3052 |
{ |
| 3053 |
verifyIndex(index); |
| 3054 |
|
| 3055 |
aboutToSetValue(); |
| 3056 |
index = resizeByOneIfNeeded(index); |
| 3057 |
_lValueList[index].specularColor.setPackedValue(rgba); |
| 3058 |
hasSetValue(); |
| 3059 |
} |
| 3060 |
|
| 3061 |
void PropertyMaterialList::setEmissiveColor(const Base::Color& col) |
| 3062 |
{ |
| 3063 |
aboutToSetValue(); |
| 3064 |
setMinimumSizeOne(); |
| 3065 |
for (auto& material : _lValueList) { |
| 3066 |
material.emissiveColor = col; |
| 3067 |
} |
| 3068 |
hasSetValue(); |
| 3069 |
} |
| 3070 |
|
| 3071 |
void PropertyMaterialList::setEmissiveColor(float r, float g, float b, float a) |
| 3072 |
{ |
| 3073 |
aboutToSetValue(); |
| 3074 |
setMinimumSizeOne(); |
| 3075 |
for (auto& material : _lValueList) { |
| 3076 |
material.emissiveColor.set(r, g, b, a); |
| 3077 |
} |
| 3078 |
hasSetValue(); |
| 3079 |
} |
| 3080 |
|
| 3081 |
void PropertyMaterialList::setEmissiveColor(uint32_t rgba) |
| 3082 |
{ |
| 3083 |
aboutToSetValue(); |
| 3084 |
setMinimumSizeOne(); |
| 3085 |
for (auto& material : _lValueList) { |
| 3086 |
material.emissiveColor.setPackedValue(rgba); |
| 3087 |
} |
| 3088 |
hasSetValue(); |
| 3089 |
} |
| 3090 |
|
| 3091 |
void PropertyMaterialList::setEmissiveColor(int index, const Base::Color& col) |
| 3092 |
{ |
| 3093 |
verifyIndex(index); |
| 3094 |
|
| 3095 |
aboutToSetValue(); |
| 3096 |
index = resizeByOneIfNeeded(index); |
| 3097 |
_lValueList[index].emissiveColor = col; |
| 3098 |
hasSetValue(); |
| 3099 |
} |
| 3100 |
|
| 3101 |
void PropertyMaterialList::setEmissiveColor(int index, float r, float g, float b, float a) |
| 3102 |
{ |
| 3103 |
verifyIndex(index); |
| 3104 |
|
| 3105 |
aboutToSetValue(); |
| 3106 |
index = resizeByOneIfNeeded(index); |
| 3107 |
_lValueList[index].emissiveColor.set(r, g, b, a); |
| 3108 |
hasSetValue(); |
| 3109 |
} |
| 3110 |
|
| 3111 |
void PropertyMaterialList::setEmissiveColor(int index, uint32_t rgba) |
| 3112 |
{ |
| 3113 |
verifyIndex(index); |
| 3114 |
|
| 3115 |
aboutToSetValue(); |
| 3116 |
index = resizeByOneIfNeeded(index); |
| 3117 |
_lValueList[index].emissiveColor.setPackedValue(rgba); |
| 3118 |
hasSetValue(); |
| 3119 |
} |
| 3120 |
|
| 3121 |
void PropertyMaterialList::setShininess(float val) |
| 3122 |
{ |
| 3123 |
aboutToSetValue(); |
| 3124 |
setMinimumSizeOne(); |
| 3125 |
for (auto& material : _lValueList) { |
| 3126 |
material.shininess = val; |
| 3127 |
} |
| 3128 |
hasSetValue(); |
| 3129 |
} |
| 3130 |
|
| 3131 |
void PropertyMaterialList::setShininess(int index, float val) |
| 3132 |
{ |
| 3133 |
verifyIndex(index); |
| 3134 |
|
| 3135 |
aboutToSetValue(); |
| 3136 |
index = resizeByOneIfNeeded(index); |
| 3137 |
_lValueList[index].shininess = val; |
| 3138 |
hasSetValue(); |
| 3139 |
} |
| 3140 |
|
| 3141 |
void PropertyMaterialList::setTransparency(float val) |
| 3142 |
{ |
| 3143 |
aboutToSetValue(); |
| 3144 |
setMinimumSizeOne(); |
| 3145 |
for (auto& material : _lValueList) { |
| 3146 |
material.transparency = val; |
| 3147 |
} |
| 3148 |
hasSetValue(); |
| 3149 |
} |
| 3150 |
|
| 3151 |
void PropertyMaterialList::setTransparency(int index, float val) |
| 3152 |
{ |
| 3153 |
verifyIndex(index); |
| 3154 |
|
| 3155 |
aboutToSetValue(); |
| 3156 |
index = resizeByOneIfNeeded(index); |
| 3157 |
_lValueList[index].transparency = val; |
| 3158 |
hasSetValue(); |
| 3159 |
} |
| 3160 |
|
| 3161 |
void PropertyMaterialList::setTransparencies(const std::vector<float>& transparencies) |
| 3162 |
{ |
| 3163 |
aboutToSetValue(); |
| 3164 |
setSize(transparencies.size(), _lValueList[0]); |
| 3165 |
|
| 3166 |
for (std::size_t i = 0; i < transparencies.size(); i++) { |
| 3167 |
_lValueList[i].transparency = transparencies[i]; |
| 3168 |
} |
| 3169 |
hasSetValue(); |
| 3170 |
} |
| 3171 |
|
| 3172 |
const Base::Color& PropertyMaterialList::getAmbientColor() const |
| 3173 |
{ |
| 3174 |
return _lValueList[0].ambientColor; |
| 3175 |
} |
| 3176 |
|
| 3177 |
const Base::Color& PropertyMaterialList::getAmbientColor(int index) const |
| 3178 |
{ |
| 3179 |
return _lValueList[index].ambientColor; |
| 3180 |
} |
| 3181 |
|
| 3182 |
const Base::Color& PropertyMaterialList::getDiffuseColor() const |
| 3183 |
{ |
| 3184 |
return _lValueList[0].diffuseColor; |
| 3185 |
} |
| 3186 |
|
| 3187 |
const Base::Color& PropertyMaterialList::getDiffuseColor(int index) const |
| 3188 |
{ |
| 3189 |
return _lValueList[index].diffuseColor; |
| 3190 |
} |
| 3191 |
|
| 3192 |
std::vector<Base::Color> PropertyMaterialList::getDiffuseColors() const |
| 3193 |
{ |
| 3194 |
std::vector<Base::Color> list; |
| 3195 |
list.reserve(_lValueList.size()); |
| 3196 |
for (auto& material : _lValueList) { |
| 3197 |
list.push_back(material.diffuseColor); |
| 3198 |
} |
| 3199 |
|
| 3200 |
return list; |
| 3201 |
} |
| 3202 |
|
| 3203 |
const Base::Color& PropertyMaterialList::getSpecularColor() const |
| 3204 |
{ |
| 3205 |
return _lValueList[0].specularColor; |
| 3206 |
} |
| 3207 |
|
| 3208 |
const Base::Color& PropertyMaterialList::getSpecularColor(int index) const |
| 3209 |
{ |
| 3210 |
return _lValueList[index].specularColor; |
| 3211 |
} |
| 3212 |
|
| 3213 |
const Base::Color& PropertyMaterialList::getEmissiveColor() const |
| 3214 |
{ |
| 3215 |
return _lValueList[0].emissiveColor; |
| 3216 |
} |
| 3217 |
|
| 3218 |
const Base::Color& PropertyMaterialList::getEmissiveColor(int index) const |
| 3219 |
{ |
| 3220 |
return _lValueList[index].emissiveColor; |
| 3221 |
} |
| 3222 |
|
| 3223 |
float PropertyMaterialList::getShininess() const |
| 3224 |
{ |
| 3225 |
return _lValueList[0].shininess; |
| 3226 |
} |
| 3227 |
|
| 3228 |
float PropertyMaterialList::getShininess(int index) const |
| 3229 |
{ |
| 3230 |
return _lValueList[index].shininess; |
| 3231 |
} |
| 3232 |
|
| 3233 |
float PropertyMaterialList::getTransparency() const |
| 3234 |
{ |
| 3235 |
return _lValueList[0].transparency; |
| 3236 |
} |
| 3237 |
|
| 3238 |
float PropertyMaterialList::getTransparency(int index) const |
| 3239 |
{ |
| 3240 |
return _lValueList[index].transparency; |
| 3241 |
} |
| 3242 |
|
| 3243 |
std::vector<float> PropertyMaterialList::getTransparencies() const |
| 3244 |
{ |
| 3245 |
std::vector<float> list; |
| 3246 |
list.reserve(_lValueList.size()); |
| 3247 |
for (auto& material : _lValueList) { |
| 3248 |
list.push_back(material.transparency); |
| 3249 |
} |
| 3250 |
|
| 3251 |
return list; |
| 3252 |
} |
| 3253 |
|
| 3254 |
Material PropertyMaterialList::getPyValue(PyObject* value) const |
| 3255 |
{ |
| 3256 |
if (PyObject_TypeCheck(value, &(MaterialPy::Type))) { |
| 3257 |
return *static_cast<MaterialPy*>(value)->getMaterialPtr(); |
| 3258 |
} |
| 3259 |
else { |
| 3260 |
std::string error = std::string("type must be 'Material', not "); |
| 3261 |
error += value->ob_type->tp_name; |
| 3262 |
throw Base::TypeError(error); |
| 3263 |
} |
| 3264 |
} |
| 3265 |
|
| 3266 |
void PropertyMaterialList::Save(Base::Writer& writer) const |
| 3267 |
{ |
| 3268 |
if (!writer.isForceXML()) { |
| 3269 |
writer.Stream() << writer.ind() << "<MaterialList file=\"" |
| 3270 |
<< (getSize() ? writer.addFile(getName(), this) : "") << "\"" |
| 3271 |
<< " version=\"3\"/>" << std::endl; |
| 3272 |
} |
| 3273 |
} |
| 3274 |
|
| 3275 |
void PropertyMaterialList::Restore(Base::XMLReader& reader) |
| 3276 |
{ |
| 3277 |
reader.readElement("MaterialList"); |
| 3278 |
if (reader.hasAttribute("file")) { |
| 3279 |
std::string file(reader.getAttribute<const char*>("file")); |
| 3280 |
if (reader.hasAttribute("version")) { |
| 3281 |
formatVersion = static_cast<Format>(reader.getAttribute<long>("version")); |
| 3282 |
} |
| 3283 |
|
| 3284 |
if (!file.empty()) { |
| 3285 |
// initiate a file read |
| 3286 |
reader.addFile(file.c_str(), this); |
| 3287 |
} |
| 3288 |
|
| 3289 |
requiresAlphaConversion = readerRequiresAlphaConversion(reader); |
| 3290 |
} |
| 3291 |
} |
| 3292 |
|
| 3293 |
void PropertyMaterialList::SaveDocFile(Base::Writer& writer) const |
| 3294 |
{ |
| 3295 |
Base::OutputStream str(writer.Stream()); |
| 3296 |
uint32_t uCt = (uint32_t)getSize(); |
| 3297 |
str << uCt; |
| 3298 |
for (const auto& it : _lValueList) { |
| 3299 |
str << it.ambientColor.getPackedValue(); |
| 3300 |
str << it.diffuseColor.getPackedValue(); |
| 3301 |
str << it.specularColor.getPackedValue(); |
| 3302 |
str << it.emissiveColor.getPackedValue(); |
| 3303 |
str << it.shininess; |
| 3304 |
str << it.transparency; |
| 3305 |
} |
| 3306 |
|
| 3307 |
// Apply the latest changes last for backwards compatibility |
| 3308 |
for (const auto& it : _lValueList) { |
| 3309 |
writeString(str, it.image); |
| 3310 |
writeString(str, it.imagePath); |
| 3311 |
writeString(str, it.uuid); |
| 3312 |
} |
| 3313 |
} |
| 3314 |
|
| 3315 |
void PropertyMaterialList::writeString(Base::OutputStream& str, const std::string& value) const |
| 3316 |
{ |
| 3317 |
uint32_t uCt = (uint32_t)value.size(); |
| 3318 |
str << uCt; |
| 3319 |
str.write(value.c_str(), uCt); |
| 3320 |
} |
| 3321 |
|
| 3322 |
void PropertyMaterialList::RestoreDocFile(Base::Reader& reader) |
| 3323 |
{ |
| 3324 |
Base::InputStream str(reader); |
| 3325 |
if (formatVersion == Version_2) { |
| 3326 |
// V2 is same as V0 |
| 3327 |
uint32_t count = 0; |
| 3328 |
str >> count; |
| 3329 |
RestoreDocFileV0(count, reader); |
| 3330 |
} |
| 3331 |
else if (formatVersion == Version_3) { |
| 3332 |
// Default to the latest |
| 3333 |
RestoreDocFileV3(reader); |
| 3334 |
} |
| 3335 |
else { |
| 3336 |
int32_t version; |
| 3337 |
str >> version; |
| 3338 |
if (version < 0) { |
| 3339 |
// This was a failed attempt at versioning, but is included |
| 3340 |
// to support files created during development. In can be removed |
| 3341 |
// in a future release once dev files are migrated. |
| 3342 |
uint32_t count = 0; |
| 3343 |
str >> count; |
| 3344 |
RestoreDocFileV0(count, reader); |
| 3345 |
} |
| 3346 |
else { |
| 3347 |
uint32_t uCt = static_cast<uint32_t>(version); |
| 3348 |
RestoreDocFileV0(uCt, reader); |
| 3349 |
} |
| 3350 |
} |
| 3351 |
} |
| 3352 |
|
| 3353 |
void PropertyMaterialList::RestoreDocFileV0(uint32_t count, Base::Reader& reader) |
| 3354 |
{ |
| 3355 |
Base::InputStream str(reader); |
| 3356 |
std::vector<Material> values(count); |
| 3357 |
uint32_t value {}; // must be 32 bit long |
| 3358 |
float valueF {}; |
| 3359 |
for (auto& it : values) { |
| 3360 |
str >> value; |
| 3361 |
it.ambientColor.setPackedValue(value); |
| 3362 |
str >> value; |
| 3363 |
it.diffuseColor.setPackedValue(value); |
| 3364 |
str >> value; |
| 3365 |
it.specularColor.setPackedValue(value); |
| 3366 |
str >> value; |
| 3367 |
it.emissiveColor.setPackedValue(value); |
| 3368 |
str >> valueF; |
| 3369 |
it.shininess = valueF; |
| 3370 |
str >> valueF; |
| 3371 |
it.transparency = valueF; |
| 3372 |
} |
| 3373 |
convertAlpha(values); |
| 3374 |
setValues(values); |
| 3375 |
} |
| 3376 |
|
| 3377 |
void PropertyMaterialList::RestoreDocFileV3(Base::Reader& reader) |
| 3378 |
{ |
| 3379 |
Base::InputStream str(reader); |
| 3380 |
uint32_t count = 0; |
| 3381 |
str >> count; |
| 3382 |
std::vector<Material> values(count); |
| 3383 |
uint32_t value {}; // must be 32 bit long |
| 3384 |
float valueF {}; |
| 3385 |
for (auto& it : values) { |
| 3386 |
str >> value; |
| 3387 |
it.ambientColor.setPackedValue(value); |
| 3388 |
str >> value; |
| 3389 |
it.diffuseColor.setPackedValue(value); |
| 3390 |
str >> value; |
| 3391 |
it.specularColor.setPackedValue(value); |
| 3392 |
str >> value; |
| 3393 |
it.emissiveColor.setPackedValue(value); |
| 3394 |
str >> valueF; |
| 3395 |
it.shininess = valueF; |
| 3396 |
str >> valueF; |
| 3397 |
it.transparency = valueF; |
| 3398 |
} |
| 3399 |
for (auto& it : values) { |
| 3400 |
readString(str, it.image); |
| 3401 |
readString(str, it.imagePath); |
| 3402 |
readString(str, it.uuid); |
| 3403 |
} |
| 3404 |
convertAlpha(values); |
| 3405 |
setValues(values); |
| 3406 |
} |
| 3407 |
|
| 3408 |
void PropertyMaterialList::convertAlpha(std::vector<App::Material>& materials) const |
| 3409 |
{ |
| 3410 |
if (requiresAlphaConversion) { |
| 3411 |
std::ranges::for_each(materials, convertAlphaInMaterial); |
| 3412 |
} |
| 3413 |
} |
| 3414 |
|
| 3415 |
void PropertyMaterialList::readString(Base::InputStream& str, std::string& value) |
| 3416 |
{ |
| 3417 |
uint32_t uCt {}; |
| 3418 |
str >> uCt; |
| 3419 |
|
| 3420 |
std::vector<char> temp(uCt); |
| 3421 |
str.read(temp.data(), uCt); |
| 3422 |
value.assign(temp.data(), temp.size()); |
| 3423 |
} |
| 3424 |
|
| 3425 |
const char* PropertyMaterialList::getEditorName() const |
| 3426 |
{ |
| 3427 |
if (testStatus(NoMaterialListEdit)) { |
| 3428 |
return ""; |
| 3429 |
} |
| 3430 |
return "Gui::PropertyEditor::PropertyMaterialListItem"; |
| 3431 |
} |
| 3432 |
|
| 3433 |
Property* PropertyMaterialList::Copy() const |
| 3434 |
{ |
| 3435 |
PropertyMaterialList* p = new PropertyMaterialList(); |
| 3436 |
p->_lValueList = _lValueList; |
| 3437 |
return p; |
| 3438 |
} |
| 3439 |
|
| 3440 |
void PropertyMaterialList::Paste(const Property& from) |
| 3441 |
{ |
| 3442 |
setValues(dynamic_cast<const PropertyMaterialList&>(from)._lValueList); |
| 3443 |
} |
| 3444 |
|
| 3445 |
unsigned int PropertyMaterialList::getMemSize() const |
| 3446 |
{ |
| 3447 |
return static_cast<unsigned int>(_lValueList.size() * sizeof(Material)); |
| 3448 |
} |
| 3449 |
|
| 3450 |
//************************************************************************** |
| 3451 |
// PropertyPersistentObject |
| 3452 |
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
| 3453 |
|
| 3454 |
TYPESYSTEM_SOURCE(App::PropertyPersistentObject, App::PropertyString) |
| 3455 |
|
| 3456 |
PyObject* PropertyPersistentObject::getPyObject() |
| 3457 |
{ |
| 3458 |
if (_pObject) { |
| 3459 |
return _pObject->getPyObject(); |
| 3460 |
} |
| 3461 |
return inherited::getPyObject(); |
| 3462 |
} |
| 3463 |
|
| 3464 |
void PropertyPersistentObject::Save(Base::Writer& writer) const |
| 3465 |
{ |
| 3466 |
inherited::Save(writer); |
| 3467 |
writer.Stream() << writer.ind() << "<PersistentObject>" << std::endl; |
| 3468 |
if (_pObject) { |
| 3469 |
writer.incInd(); |
| 3470 |
_pObject->Save(writer); |
| 3471 |
writer.decInd(); |
| 3472 |
} |
| 3473 |
writer.Stream() << writer.ind() << "</PersistentObject>" << std::endl; |
| 3474 |
} |
| 3475 |
|
| 3476 |
void PropertyPersistentObject::Restore(Base::XMLReader& reader) |
| 3477 |
{ |
| 3478 |
inherited::Restore(reader); |
| 3479 |
reader.readElement("PersistentObject"); |
| 3480 |
if (_pObject) { |
| 3481 |
_pObject->Restore(reader); |
| 3482 |
} |
| 3483 |
reader.readEndElement("PersistentObject"); |
| 3484 |
} |
| 3485 |
|
| 3486 |
Property* PropertyPersistentObject::Copy() const |
| 3487 |
{ |
| 3488 |
auto* p = new PropertyPersistentObject(); |
| 3489 |
p->_cValue = _cValue; |
| 3490 |
p->_pObject = _pObject; |
| 3491 |
return p; |
| 3492 |
} |
| 3493 |
|
| 3494 |
void PropertyPersistentObject::Paste(const Property& from) |
| 3495 |
{ |
| 3496 |
const auto& prop = dynamic_cast<const PropertyPersistentObject&>(from); |
| 3497 |
if (_cValue != prop._cValue || _pObject != prop._pObject) { |
| 3498 |
aboutToSetValue(); |
| 3499 |
_cValue = prop._cValue; |
| 3500 |
_pObject = prop._pObject; |
| 3501 |
hasSetValue(); |
| 3502 |
} |
| 3503 |
} |
| 3504 |
|
| 3505 |
unsigned int PropertyPersistentObject::getMemSize() const |
| 3506 |
{ |
| 3507 |
auto size = inherited::getMemSize(); |
| 3508 |
if (_pObject) { |
| 3509 |
size += _pObject->getMemSize(); |
| 3510 |
} |
| 3511 |
return size; |
| 3512 |
} |
| 3513 |
|
| 3514 |
void PropertyPersistentObject::setValue(const char* type) |
| 3515 |
{ |
| 3516 |
if (!Base::Tools::isNullOrEmpty(type)) { |
| 3517 |
Base::Type t = Base::Type::getTypeIfDerivedFrom(type, Persistence::getClassTypeId()); |
| 3518 |
if (t.isBad()) { |
| 3519 |
throw Base::TypeError("Invalid type or type must be derived from Base::Persistence"); |
| 3520 |
} |
| 3521 |
if (_pObject && _pObject->getTypeId() == t) { |
| 3522 |
return; |
| 3523 |
} |
| 3524 |
} |
| 3525 |
aboutToSetValue(); |
| 3526 |
_pObject.reset(); |
| 3527 |
_cValue = type; |
| 3528 |
if (type[0]) { |
| 3529 |
_pObject.reset(static_cast<Base::Persistence*>(Base::Type::createInstanceByName(type))); |
| 3530 |
} |
| 3531 |
hasSetValue(); |
| 3532 |
} |