Debian Codegraph @

src/App/PropertyGeo.cpp

raw
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 <Base/MatrixPy.h>
24 #include <Base/PlacementPy.h>
25 #include <Base/Reader.h>
26
27 #include <Base/Quantity.h>
28 #include <Base/QuantityPy.h>
29 #include <Base/Rotation.h>
30 #include <Base/RotationPy.h>
31 #include <Base/Stream.h>
32 #include <Base/Tools.h>
33 #include <Base/VectorPy.h>
34 #include <Base/Writer.h>
35
36 #include "ComplexGeoData.h"
37 #include "Document.h"
38 #include "PropertyGeo.h"
39 #include "Placement.h"
40 #include "ObjectIdentifier.h"
41
42
43 using namespace App;
44 using namespace Base;
45 using namespace std;
46
47
48 //**************************************************************************
49 //**************************************************************************
50 // PropertyVector
51 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
52
53 TYPESYSTEM_SOURCE(App::PropertyVector, App::Property)
54
55 //**************************************************************************
56 // Construction/Destruction
57
58
59 PropertyVector::PropertyVector() = default;
60
61 PropertyVector::~PropertyVector() = default;
62
63 //**************************************************************************
64 // Base class implementer
65
66 void PropertyVector::setValue(const Base::Vector3d& vec)
67 {
68 aboutToSetValue();
69 _cVec = vec;
70 hasSetValue();
71 }
72
73 void PropertyVector::setValue(double x, double y, double z)
74 {
75 aboutToSetValue();
76 _cVec.Set(x, y, z);
77 hasSetValue();
78 }
79
80 const Base::Vector3d& PropertyVector::getValue() const
81 {
82 return _cVec;
83 }
84
85 PyObject* PropertyVector::getPyObject()
86 {
87 return new Base::VectorPy(_cVec);
88 }
89
90 void PropertyVector::setPyObject(PyObject* value)
91 {
92 if (PyObject_TypeCheck(value, &(Base::VectorPy::Type))) {
93 Base::VectorPy* pcObject = static_cast<Base::VectorPy*>(value);
94 Base::Vector3d* val = pcObject->getVectorPtr();
95 setValue(*val);
96 }
97 else if (PyTuple_Check(value) && PyTuple_Size(value) == 3) {
98 PyObject* item {};
99 Base::Vector3d cVec;
100 // x
101 item = PyTuple_GetItem(value, 0);
102 if (PyFloat_Check(item)) {
103 cVec.x = PyFloat_AsDouble(item);
104 }
105 else if (PyLong_Check(item)) {
106 cVec.x = (double)PyLong_AsLong(item);
107 }
108 else {
109 throw Base::TypeError("Not allowed type used in tuple (float expected)...");
110 }
111 // y
112 item = PyTuple_GetItem(value, 1);
113 if (PyFloat_Check(item)) {
114 cVec.y = PyFloat_AsDouble(item);
115 }
116 else if (PyLong_Check(item)) {
117 cVec.y = (double)PyLong_AsLong(item);
118 }
119 else {
120 throw Base::TypeError("Not allowed type used in tuple (float expected)...");
121 }
122 // z
123 item = PyTuple_GetItem(value, 2);
124 if (PyFloat_Check(item)) {
125 cVec.z = PyFloat_AsDouble(item);
126 }
127 else if (PyLong_Check(item)) {
128 cVec.z = (double)PyLong_AsLong(item);
129 }
130 else {
131 throw Base::TypeError("Not allowed type used in tuple (float expected)...");
132 }
133 setValue(cVec);
134 }
135 else {
136 std::string error = std::string("type must be 'Vector' or tuple of three floats, not ");
137 error += value->ob_type->tp_name;
138 throw Base::TypeError(error);
139 }
140 }
141
142 void PropertyVector::Save(Base::Writer& writer) const
143 {
144 // clang-format off
145 writer.Stream() << writer.ind()
146 << "<PropertyVector"
147 << " valueX=\"" << _cVec.x << "\""
148 << " valueY=\"" << _cVec.y << "\""
149 << " valueZ=\"" << _cVec.z << "\""
150 << "/>\n";
151 // clang-format on
152 }
153
154 void PropertyVector::Restore(Base::XMLReader& reader)
155 {
156 // read my Element
157 reader.readElement("PropertyVector");
158 // get the value of my Attribute
159 aboutToSetValue();
160 _cVec.x = reader.getAttribute<double>("valueX");
161 _cVec.y = reader.getAttribute<double>("valueY");
162 _cVec.z = reader.getAttribute<double>("valueZ");
163 hasSetValue();
164 }
165
166
167 Property* PropertyVector::Copy() const
168 {
169 PropertyVector* p = new PropertyVector();
170 p->_cVec = _cVec;
171 return p;
172 }
173
174 void PropertyVector::Paste(const Property& from)
175 {
176 aboutToSetValue();
177 _cVec = dynamic_cast<const PropertyVector&>(from)._cVec;
178 hasSetValue();
179 }
180
181 void PropertyVector::getPaths(std::vector<ObjectIdentifier>& paths) const
182 {
183 paths.push_back(ObjectIdentifier(*this)
184 << ObjectIdentifier::SimpleComponent(ObjectIdentifier::String("x")));
185 paths.push_back(ObjectIdentifier(*this)
186 << ObjectIdentifier::SimpleComponent(ObjectIdentifier::String("y")));
187 paths.push_back(ObjectIdentifier(*this)
188 << ObjectIdentifier::SimpleComponent(ObjectIdentifier::String("z")));
189 }
190
191 const boost::any PropertyVector::getPathValue(const ObjectIdentifier& path) const
192 {
193 Base::Unit unit = getUnit();
194 if (unit != Unit::One) {
195 std::string p = path.getSubPathStr();
196 if (p == ".x" || p == ".y" || p == ".z") {
197 // Convert double to quantity
198 return Base::Quantity(boost::any_cast<double>(Property::getPathValue(path)), unit);
199 }
200 }
201 return Property::getPathValue(path);
202 }
203
204 bool PropertyVector::getPyPathValue(const ObjectIdentifier& path, Py::Object& res) const
205 {
206 Base::Unit unit = getUnit();
207 if (unit == Unit::One) {
208 return false;
209 }
210
211 std::string p = path.getSubPathStr();
212 if (p == ".x") {
213 res = Py::asObject(new QuantityPy(new Quantity(getValue().x, unit)));
214 }
215 else if (p == ".y") {
216 res = Py::asObject(new QuantityPy(new Quantity(getValue().y, unit)));
217 }
218 else if (p == ".z") {
219 res = Py::asObject(new QuantityPy(new Quantity(getValue().z, unit)));
220 }
221 else {
222 return false;
223 }
224 return true;
225 }
226
227
228 //**************************************************************************
229 // PropertyVectorDistance
230 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
231
232 TYPESYSTEM_SOURCE(App::PropertyVectorDistance, App::PropertyVector)
233
234 //**************************************************************************
235 // Construction/Destruction
236
237
238 PropertyVectorDistance::PropertyVectorDistance() = default;
239
240 PropertyVectorDistance::~PropertyVectorDistance() = default;
241
242 //**************************************************************************
243 // PropertyPosition
244 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
245
246 TYPESYSTEM_SOURCE(App::PropertyPosition, App::PropertyVector)
247
248 //**************************************************************************
249 // Construction/Destruction
250
251
252 PropertyPosition::PropertyPosition() = default;
253
254 PropertyPosition::~PropertyPosition() = default;
255
256 //**************************************************************************
257 // PropertyPosition
258 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
259
260 TYPESYSTEM_SOURCE(App::PropertyDirection, App::PropertyVector)
261
262 //**************************************************************************
263 // Construction/Destruction
264
265
266 PropertyDirection::PropertyDirection() = default;
267
268 PropertyDirection::~PropertyDirection() = default;
269
270 //**************************************************************************
271 // PropertyVectorList
272 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
273
274 TYPESYSTEM_SOURCE(App::PropertyVectorList, App::PropertyLists)
275
276 //**************************************************************************
277 // Construction/Destruction
278
279 PropertyVectorList::PropertyVectorList() = default;
280
281 PropertyVectorList::~PropertyVectorList() = default;
282
283 //**************************************************************************
284 // Base class implementer
285
286 void PropertyVectorList::setValue(double x, double y, double z)
287 {
288 setValue(Base::Vector3d(x, y, z));
289 }
290
291 PyObject* PropertyVectorList::getPyObject()
292 {
293 PyObject* list = PyList_New(getSize());
294
295 for (int i = 0; i < getSize(); i++) {
296 PyList_SetItem(list, i, new VectorPy(_lValueList[i]));
297 }
298
299 return list;
300 }
301
302 Base::Vector3d PropertyVectorList::getPyValue(PyObject* item) const
303 {
304 PropertyVector val;
305 val.setPyObject(item);
306 return val.getValue();
307 }
308
309 void PropertyVectorList::Save(Base::Writer& writer) const
310 {
311 if (!writer.isForceXML()) {
312 writer.Stream() << writer.ind() << "<VectorList file=\"" << writer.addFile(getName(), this)
313 << "\"/>" << std::endl;
314 }
315 }
316
317 void PropertyVectorList::Restore(Base::XMLReader& reader)
318 {
319 reader.readElement("VectorList");
320 std::string file(reader.getAttribute<const char*>("file"));
321
322 if (!file.empty()) {
323 // initiate a file read
324 reader.addFile(file.c_str(), this);
325 }
326 }
327
328 void PropertyVectorList::SaveDocFile(Base::Writer& writer) const
329 {
330 Base::OutputStream str(writer.Stream());
331 uint32_t uCt = (uint32_t)getSize();
332 str << uCt;
333 if (!isSinglePrecision()) {
334 for (const auto& it : _lValueList) {
335 str << it.x << it.y << it.z;
336 }
337 }
338 else {
339 for (const auto& it : _lValueList) {
340 float x = (float)it.x;
341 float y = (float)it.y;
342 float z = (float)it.z;
343 str << x << y << z;
344 }
345 }
346 }
347
348 void PropertyVectorList::RestoreDocFile(Base::Reader& reader)
349 {
350 Base::InputStream str(reader);
351 uint32_t uCt = 0;
352 str >> uCt;
353 std::vector<Base::Vector3d> values(uCt);
354 if (!isSinglePrecision()) {
355 for (auto& it : values) {
356 str >> it.x >> it.y >> it.z;
357 }
358 }
359 else {
360 Base::Vector3f vec;
361 for (auto& it : values) {
362 str >> vec.x >> vec.y >> vec.z;
363 it.Set(vec.x, vec.y, vec.z);
364 }
365 }
366 setValues(values);
367 }
368
369 Property* PropertyVectorList::Copy() const
370 {
371 PropertyVectorList* p = new PropertyVectorList();
372 p->_lValueList = _lValueList;
373 return p;
374 }
375
376 void PropertyVectorList::Paste(const Property& from)
377 {
378 setValues(dynamic_cast<const PropertyVectorList&>(from)._lValueList);
379 }
380
381 unsigned int PropertyVectorList::getMemSize() const
382 {
383 return static_cast<unsigned int>(_lValueList.size() * sizeof(Base::Vector3d));
384 }
385
386 //**************************************************************************
387 //**************************************************************************
388 // PropertyMatrix
389 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
390
391 TYPESYSTEM_SOURCE(App::PropertyMatrix, App::Property)
392
393 //**************************************************************************
394 // Construction/Destruction
395
396
397 PropertyMatrix::PropertyMatrix() = default;
398
399
400 PropertyMatrix::~PropertyMatrix() = default;
401
402 //**************************************************************************
403 // Base class implementer
404
405
406 void PropertyMatrix::setValue(const Base::Matrix4D& mat)
407 {
408 aboutToSetValue();
409 _cMat = mat;
410 hasSetValue();
411 }
412
413
414 const Base::Matrix4D& PropertyMatrix::getValue() const
415 {
416 return _cMat;
417 }
418
419 PyObject* PropertyMatrix::getPyObject()
420 {
421 return new Base::MatrixPy(_cMat);
422 }
423
424 void PropertyMatrix::setPyObject(PyObject* value)
425 {
426 if (PyObject_TypeCheck(value, &(Base::MatrixPy::Type))) {
427 Base::MatrixPy* pcObject = static_cast<Base::MatrixPy*>(value);
428 setValue(pcObject->value());
429 }
430 else if (PyTuple_Check(value) && PyTuple_Size(value) == 16) {
431 PyObject* item;
432 Base::Matrix4D cMatrix;
433
434 const int dim = 4;
435 for (int x = 0; x < dim; x++) {
436 for (int y = 0; y < dim; y++) {
437 item = PyTuple_GetItem(value, x + y * dim);
438 if (PyFloat_Check(item)) {
439 cMatrix[x][y] = PyFloat_AsDouble(item);
440 }
441 else if (PyLong_Check(item)) {
442 cMatrix[x][y] = (double)PyLong_AsLong(item);
443 }
444 else {
445 throw Base::TypeError(
446 "Not allowed type used in matrix tuple (a number expected)...");
447 }
448 }
449 }
450
451 setValue(cMatrix);
452 }
453 else {
454 std::string error = std::string("type must be 'Matrix' or tuple of 16 float or int, not ");
455 error += value->ob_type->tp_name;
456 throw Base::TypeError(error);
457 }
458 }
459
460 void PropertyMatrix::Save(Base::Writer& writer) const
461 {
462 // clang-format off
463 writer.Stream() << writer.ind() << "<PropertyMatrix";
464 writer.Stream() << " a11=\"" << _cMat[0][0] << "\" a12=\"" << _cMat[0][1] << "\" a13=\"" << _cMat[0][2] << "\" a14=\"" << _cMat[0][3] << "\"";
465 writer.Stream() << " a21=\"" << _cMat[1][0] << "\" a22=\"" << _cMat[1][1] << "\" a23=\"" << _cMat[1][2] << "\" a24=\"" << _cMat[1][3] << "\"";
466 writer.Stream() << " a31=\"" << _cMat[2][0] << "\" a32=\"" << _cMat[2][1] << "\" a33=\"" << _cMat[2][2] << "\" a34=\"" << _cMat[2][3] << "\"";
467 writer.Stream() << " a41=\"" << _cMat[3][0] << "\" a42=\"" << _cMat[3][1] << "\" a43=\"" << _cMat[3][2] << "\" a44=\"" << _cMat[3][3] << "\"";
468 writer.Stream() <<"/>" << endl;
469 // clang-format on
470 }
471
472 void PropertyMatrix::Restore(Base::XMLReader& reader)
473 {
474 // read my Element
475 reader.readElement("PropertyMatrix");
476 // get the value of my Attribute
477 aboutToSetValue();
478 _cMat[0][0] = reader.getAttribute<double>("a11");
479 _cMat[0][1] = reader.getAttribute<double>("a12");
480 _cMat[0][2] = reader.getAttribute<double>("a13");
481 _cMat[0][3] = reader.getAttribute<double>("a14");
482
483 _cMat[1][0] = reader.getAttribute<double>("a21");
484 _cMat[1][1] = reader.getAttribute<double>("a22");
485 _cMat[1][2] = reader.getAttribute<double>("a23");
486 _cMat[1][3] = reader.getAttribute<double>("a24");
487
488 _cMat[2][0] = reader.getAttribute<double>("a31");
489 _cMat[2][1] = reader.getAttribute<double>("a32");
490 _cMat[2][2] = reader.getAttribute<double>("a33");
491 _cMat[2][3] = reader.getAttribute<double>("a34");
492
493 _cMat[3][0] = reader.getAttribute<double>("a41");
494 _cMat[3][1] = reader.getAttribute<double>("a42");
495 _cMat[3][2] = reader.getAttribute<double>("a43");
496 _cMat[3][3] = reader.getAttribute<double>("a44");
497 hasSetValue();
498 }
499
500
501 Property* PropertyMatrix::Copy() const
502 {
503 PropertyMatrix* p = new PropertyMatrix();
504 p->_cMat = _cMat;
505 return p;
506 }
507
508 void PropertyMatrix::Paste(const Property& from)
509 {
510 aboutToSetValue();
511 _cMat = dynamic_cast<const PropertyMatrix&>(from)._cMat;
512 hasSetValue();
513 }
514
515 //**************************************************************************
516 //**************************************************************************
517 // PropertyPlacement
518 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
519
520 TYPESYSTEM_SOURCE(App::PropertyPlacement, App::Property)
521
522 //**************************************************************************
523 // Construction/Destruction
524
525
526 PropertyPlacement::PropertyPlacement() = default;
527
528
529 PropertyPlacement::~PropertyPlacement() = default;
530
531 //**************************************************************************
532 // Base class implementer
533
534
535 void PropertyPlacement::setValue(const Base::Placement& pos)
536 {
537 aboutToSetValue();
538 _cPos = pos;
539 hasSetValue();
540 }
541
542 bool PropertyPlacement::setValueIfChanged(const Base::Placement& pos, double tol, double atol)
543 {
544 if (_cPos.getPosition().IsEqual(pos.getPosition(), tol)
545 && _cPos.getRotation().isSame(pos.getRotation(), atol)) {
546 return false;
547 }
548 setValue(pos);
549 return true;
550 }
551
552
553 const Base::Placement& PropertyPlacement::getValue() const
554 {
555 return _cPos;
556 }
557
558 void PropertyPlacement::getPaths(std::vector<ObjectIdentifier>& paths) const
559 {
560 paths.push_back(ObjectIdentifier(*this)
561 << ObjectIdentifier::SimpleComponent(ObjectIdentifier::String("Base"))
562 << ObjectIdentifier::SimpleComponent(ObjectIdentifier::String("x")));
563 paths.push_back(ObjectIdentifier(*this)
564 << ObjectIdentifier::SimpleComponent(ObjectIdentifier::String("Base"))
565 << ObjectIdentifier::SimpleComponent(ObjectIdentifier::String("y")));
566 paths.push_back(ObjectIdentifier(*this)
567 << ObjectIdentifier::SimpleComponent(ObjectIdentifier::String("Base"))
568 << ObjectIdentifier::SimpleComponent(ObjectIdentifier::String("z")));
569 paths.push_back(ObjectIdentifier(*this)
570 << ObjectIdentifier::SimpleComponent(ObjectIdentifier::String("Rotation"))
571 << ObjectIdentifier::SimpleComponent(ObjectIdentifier::String("Angle")));
572 paths.push_back(ObjectIdentifier(*this)
573 << ObjectIdentifier::SimpleComponent(ObjectIdentifier::String("Rotation"))
574 << ObjectIdentifier::SimpleComponent(ObjectIdentifier::String("Axis"))
575 << ObjectIdentifier::SimpleComponent(ObjectIdentifier::String("x")));
576 paths.push_back(ObjectIdentifier(*this)
577 << ObjectIdentifier::SimpleComponent(ObjectIdentifier::String("Rotation"))
578 << ObjectIdentifier::SimpleComponent(ObjectIdentifier::String("Axis"))
579 << ObjectIdentifier::SimpleComponent(ObjectIdentifier::String("y")));
580 paths.push_back(ObjectIdentifier(*this)
581 << ObjectIdentifier::SimpleComponent(ObjectIdentifier::String("Rotation"))
582 << ObjectIdentifier::SimpleComponent(ObjectIdentifier::String("Axis"))
583 << ObjectIdentifier::SimpleComponent(ObjectIdentifier::String("z")));
584 }
585
586 namespace
587 {
588 double toDouble(const boost::any& value)
589 {
590 double avalue {};
591
592 if (value.type() == typeid(Base::Quantity)) {
593 avalue = boost::any_cast<Base::Quantity>(value).getValue();
594 }
595 else if (value.type() == typeid(double)) {
596 avalue = boost::any_cast<double>(value);
597 }
598 else if (value.type() == typeid(int)) {
599 avalue = boost::any_cast<int>(value);
600 }
601 else if (value.type() == typeid(unsigned int)) {
602 avalue = boost::any_cast<unsigned int>(value);
603 }
604 else if (value.type() == typeid(short)) {
605 avalue = boost::any_cast<short>(value);
606 }
607 else if (value.type() == typeid(unsigned short)) {
608 avalue = boost::any_cast<unsigned short>(value);
609 }
610 else if (value.type() == typeid(long)) {
611 avalue = boost::any_cast<long>(value);
612 }
613 else if (value.type() == typeid(unsigned long)) {
614 avalue = boost::any_cast<unsigned long>(value);
615 }
616 else {
617 throw std::bad_cast();
618 }
619 return avalue;
620 }
621 } // namespace
622
623 void PropertyPlacement::setPathValue(const ObjectIdentifier& path, const boost::any& value)
624 {
625 auto updateAxis = [this](int index, double coord) {
626 Base::Vector3d axis;
627 double angle;
628 Base::Vector3d base = _cPos.getPosition();
629 Base::Rotation rot = _cPos.getRotation();
630 rot.getRawValue(axis, angle);
631 axis[index] = coord;
632 rot.setValue(axis, angle);
633 Base::Placement plm(base, rot);
634 setValue(plm);
635 };
636
637 auto updateYawPitchRoll = [this](int index, double angle) {
638 Base::Vector3d base = _cPos.getPosition();
639 Base::Rotation rot = _cPos.getRotation();
640 double yaw, pitch, roll;
641 rot.getYawPitchRoll(yaw, pitch, roll);
642 if (index == 0) {
643 if (angle < -180.0 || angle > 180.0) {
644 throw Base::ValueError("Yaw angle is out of range [-180, +180]");
645 }
646 yaw = angle;
647 }
648 else if (index == 1) {
649 if (angle < -90.0 || angle > 90.0) {
650 throw Base::ValueError("Pitch angle is out of range [-90, +90]");
651 }
652 pitch = angle;
653 }
654 else if (index == 2) {
655 if (angle < -180.0 || angle > 180.0) {
656 throw Base::ValueError("Roll angle is out of range [-180, +180]");
657 }
658 roll = angle;
659 }
660 rot.setYawPitchRoll(yaw, pitch, roll);
661 Base::Placement plm(base, rot);
662 setValue(plm);
663 };
664
665 std::string subpath = path.getSubPathStr();
666 if (subpath == ".Rotation.Angle") {
667 double avalue = toDouble(value);
668 Property::setPathValue(path, Base::toRadians(avalue));
669 }
670 else if (subpath == ".Rotation.Axis.x") {
671 updateAxis(0, toDouble(value));
672 }
673 else if (subpath == ".Rotation.Axis.y") {
674 updateAxis(1, toDouble(value));
675 }
676 else if (subpath == ".Rotation.Axis.z") {
677 updateAxis(2, toDouble(value));
678 }
679 else if (subpath == ".Rotation.Yaw") {
680 updateYawPitchRoll(0, toDouble(value));
681 }
682 else if (subpath == ".Rotation.Pitch") {
683 updateYawPitchRoll(1, toDouble(value));
684 }
685 else if (subpath == ".Rotation.Roll") {
686 updateYawPitchRoll(2, toDouble(value));
687 }
688 else {
689 Property::setPathValue(path, value);
690 }
691 }
692
693 const boost::any PropertyPlacement::getPathValue(const ObjectIdentifier& path) const
694 {
695 auto getAxis = [](const Base::Placement& plm) {
696 Base::Vector3d axis;
697 double angle;
698 const Base::Rotation& rot = plm.getRotation();
699 rot.getRawValue(axis, angle);
700 return axis;
701 };
702
703 auto getYawPitchRoll = [](const Base::Placement& plm) {
704 Base::Vector3d ypr;
705 const Base::Rotation& rot = plm.getRotation();
706 rot.getYawPitchRoll(ypr.x, ypr.y, ypr.z);
707 return ypr;
708 };
709
710 std::string p = path.getSubPathStr();
711
712 if (p == ".Rotation.Angle") {
713 // Convert angle to degrees
714 return Base::Quantity(
715 Base::toDegrees(boost::any_cast<double>(Property::getPathValue(path))),
716 Unit::Angle);
717 }
718 else if (p == ".Base.x" || p == ".Base.y" || p == ".Base.z") {
719 // Convert double to quantity
720 return Base::Quantity(boost::any_cast<double>(Property::getPathValue(path)), Unit::Length);
721 }
722 else if (p == ".Rotation.Axis.x") {
723 return getAxis(_cPos).x;
724 }
725 else if (p == ".Rotation.Axis.y") {
726 return getAxis(_cPos).y;
727 }
728 else if (p == ".Rotation.Axis.z") {
729 return getAxis(_cPos).z;
730 }
731 else if (p == ".Rotation.Yaw") {
732 return getYawPitchRoll(_cPos).x;
733 }
734 else if (p == ".Rotation.Pitch") {
735 return getYawPitchRoll(_cPos).y;
736 }
737 else if (p == ".Rotation.Roll") {
738 return getYawPitchRoll(_cPos).z;
739 }
740 else {
741 return Property::getPathValue(path);
742 }
743 }
744
745 bool PropertyPlacement::getPyPathValue(const ObjectIdentifier& path, Py::Object& res) const
746 {
747 auto getAxis = [](const Base::Placement& plm) {
748 Base::Vector3d axis;
749 double angle;
750 const Base::Rotation& rot = plm.getRotation();
751 rot.getRawValue(axis, angle);
752 return axis;
753 };
754
755 auto getYawPitchRoll = [](const Base::Placement& plm) {
756 Base::Vector3d ypr;
757 const Base::Rotation& rot = plm.getRotation();
758 rot.getYawPitchRoll(ypr.x, ypr.y, ypr.z);
759 return ypr;
760 };
761
762 std::string p = path.getSubPathStr();
763 if (p == ".Rotation.Angle") {
764 Base::Vector3d axis;
765 double angle;
766 _cPos.getRotation().getValue(axis, angle);
767 res = Py::asObject(new QuantityPy(new Quantity(Base::toDegrees(angle), Unit::Angle)));
768 return true;
769 }
770 else if (p == ".Base.x") {
771 res = Py::asObject(new QuantityPy(new Quantity(_cPos.getPosition().x, Unit::Length)));
772 return true;
773 }
774 else if (p == ".Base.y") {
775 res = Py::asObject(new QuantityPy(new Quantity(_cPos.getPosition().y, Unit::Length)));
776 return true;
777 }
778 else if (p == ".Base.z") {
779 res = Py::asObject(new QuantityPy(new Quantity(_cPos.getPosition().z, Unit::Length)));
780 return true;
781 }
782 else if (p == ".Rotation.Axis.x") {
783 res = Py::Float(getAxis(_cPos).x);
784 return true;
785 }
786 else if (p == ".Rotation.Axis.y") {
787 res = Py::Float(getAxis(_cPos).y);
788 return true;
789 }
790 else if (p == ".Rotation.Axis.z") {
791 res = Py::Float(getAxis(_cPos).z);
792 return true;
793 }
794 else if (p == ".Rotation.Yaw") {
795 res = Py::Float(getYawPitchRoll(_cPos).x);
796 return true;
797 }
798 else if (p == ".Rotation.Pitch") {
799 res = Py::Float(getYawPitchRoll(_cPos).y);
800 return true;
801 }
802 else if (p == ".Rotation.Roll") {
803 res = Py::Float(getYawPitchRoll(_cPos).z);
804 return true;
805 }
806
807 return false;
808 }
809
810 PyObject* PropertyPlacement::getPyObject()
811 {
812 return new Base::PlacementPy(new Base::Placement(_cPos));
813 }
814
815 void PropertyPlacement::setPyObject(PyObject* value)
816 {
817 if (PyObject_TypeCheck(value, &(Base::MatrixPy::Type))) {
818 Base::MatrixPy* pcObject = static_cast<Base::MatrixPy*>(value);
819 Base::Matrix4D mat = pcObject->value();
820 Base::Placement p;
821 p.fromMatrix(mat);
822 setValue(p);
823 }
824 else if (PyObject_TypeCheck(value, &(Base::PlacementPy::Type))) {
825 setValue(*static_cast<Base::PlacementPy*>(value)->getPlacementPtr());
826 }
827 else {
828 std::string error = std::string("type must be 'Matrix' or 'Placement', not ");
829 error += value->ob_type->tp_name;
830 throw Base::TypeError(error);
831 }
832 }
833
834 void PropertyPlacement::Save(Base::Writer& writer) const
835 {
836 // clang-format off
837 writer.Stream() << writer.ind() << "<PropertyPlacement";
838 writer.Stream() << " Px=\"" << _cPos.getPosition().x << "\""
839 << " Py=\"" << _cPos.getPosition().y << "\""
840 << " Pz=\"" << _cPos.getPosition().z << "\"";
841
842 writer.Stream() << " Q0=\"" << _cPos.getRotation()[0] << "\""
843 << " Q1=\"" << _cPos.getRotation()[1] << "\""
844 << " Q2=\"" << _cPos.getRotation()[2] << "\""
845 << " Q3=\"" << _cPos.getRotation()[3] << "\"";
846 Vector3d axis;
847 double rfAngle {};
848 _cPos.getRotation().getRawValue(axis, rfAngle);
849 writer.Stream() << " A=\"" << rfAngle << "\""
850 << " Ox=\"" << axis.x << "\""
851 << " Oy=\"" << axis.y << "\""
852 << " Oz=\"" << axis.z << "\"";
853 writer.Stream() << "/>" << std::endl;
854 // clang-format on
855 }
856
857 void PropertyPlacement::Restore(Base::XMLReader& reader)
858 {
859 // read my Element
860 reader.readElement("PropertyPlacement");
861 // get the value of my Attribute
862 aboutToSetValue();
863
864 if (reader.hasAttribute("A")) {
865 _cPos = Base::Placement(Vector3d(reader.getAttribute<double>("Px"),
866 reader.getAttribute<double>("Py"),
867 reader.getAttribute<double>("Pz")),
868 Rotation(Vector3d(reader.getAttribute<double>("Ox"),
869 reader.getAttribute<double>("Oy"),
870 reader.getAttribute<double>("Oz")),
871 reader.getAttribute<double>("A")));
872 }
873 else {
874 _cPos = Base::Placement(Vector3d(reader.getAttribute<double>("Px"),
875 reader.getAttribute<double>("Py"),
876 reader.getAttribute<double>("Pz")),
877 Rotation(reader.getAttribute<double>("Q0"),
878 reader.getAttribute<double>("Q1"),
879 reader.getAttribute<double>("Q2"),
880 reader.getAttribute<double>("Q3")));
881 }
882
883 hasSetValue();
884 }
885
886
887 Property* PropertyPlacement::Copy() const
888 {
889 PropertyPlacement* p = new PropertyPlacement();
890 p->_cPos = _cPos;
891 return p;
892 }
893
894 void PropertyPlacement::Paste(const Property& from)
895 {
896 aboutToSetValue();
897 _cPos = dynamic_cast<const PropertyPlacement&>(from)._cPos;
898 hasSetValue();
899 }
900
901
902 //**************************************************************************
903 // PropertyPlacementList
904 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
905
906 TYPESYSTEM_SOURCE(App::PropertyPlacementList, App::PropertyLists)
907
908 //**************************************************************************
909 // Construction/Destruction
910
911 PropertyPlacementList::PropertyPlacementList() = default;
912
913 PropertyPlacementList::~PropertyPlacementList() = default;
914
915 //**************************************************************************
916 // Base class implementer
917
918 PyObject* PropertyPlacementList::getPyObject()
919 {
920 PyObject* list = PyList_New(getSize());
921
922 for (int i = 0; i < getSize(); i++) {
923 PyList_SetItem(list, i, new Base::PlacementPy(new Base::Placement(_lValueList[i])));
924 }
925
926 return list;
927 }
928
929 Base::Placement PropertyPlacementList::getPyValue(PyObject* item) const
930 {
931 PropertyPlacement val;
932 val.setPyObject(item);
933 return val.getValue();
934 }
935
936 void PropertyPlacementList::Save(Base::Writer& writer) const
937 {
938 if (!writer.isForceXML()) {
939 writer.Stream() << writer.ind() << "<PlacementList file=\""
940 << writer.addFile(getName(), this) << "\"/>" << std::endl;
941 }
942 }
943
944 void PropertyPlacementList::Restore(Base::XMLReader& reader)
945 {
946 reader.readElement("PlacementList");
947 std::string file(reader.getAttribute<const char*>("file"));
948
949 if (!file.empty()) {
950 // initiate a file read
951 reader.addFile(file.c_str(), this);
952 }
953 }
954
955 void PropertyPlacementList::SaveDocFile(Base::Writer& writer) const
956 {
957 Base::OutputStream str(writer.Stream());
958 uint32_t uCt = (uint32_t)getSize();
959 str << uCt;
960 if (!isSinglePrecision()) {
961 for (const auto& it : _lValueList) {
962 str << it.getPosition().x << it.getPosition().y << it.getPosition().z
963 << it.getRotation()[0] << it.getRotation()[1] << it.getRotation()[2]
964 << it.getRotation()[3];
965 }
966 }
967 else {
968 for (const auto& it : _lValueList) {
969 float x = (float)it.getPosition().x;
970 float y = (float)it.getPosition().y;
971 float z = (float)it.getPosition().z;
972 float q0 = (float)it.getRotation()[0];
973 float q1 = (float)it.getRotation()[1];
974 float q2 = (float)it.getRotation()[2];
975 float q3 = (float)it.getRotation()[3];
976 str << x << y << z << q0 << q1 << q2 << q3;
977 }
978 }
979 }
980
981 void PropertyPlacementList::RestoreDocFile(Base::Reader& reader)
982 {
983 Base::InputStream str(reader);
984 uint32_t uCt = 0;
985 str >> uCt;
986 std::vector<Base::Placement> values(uCt);
987 if (!isSinglePrecision()) {
988 for (auto& it : values) {
989 Base::Vector3d pos;
990 double q0, q1, q2, q3;
991 str >> pos.x >> pos.y >> pos.z >> q0 >> q1 >> q2 >> q3;
992 Base::Rotation rot(q0, q1, q2, q3);
993 it.setPosition(pos);
994 it.setRotation(rot);
995 }
996 }
997 else {
998 float x {}, y {}, z {};
999 float q0 {}, q1 {}, q2 {}, q3 {};
1000 for (auto& it : values) {
1001 str >> x >> y >> z >> q0 >> q1 >> q2 >> q3;
1002 Base::Vector3d pos(x, y, z);
1003 Base::Rotation rot(q0, q1, q2, q3);
1004 it.setPosition(pos);
1005 it.setRotation(rot);
1006 }
1007 }
1008 setValues(values);
1009 }
1010
1011 Property* PropertyPlacementList::Copy() const
1012 {
1013 PropertyPlacementList* p = new PropertyPlacementList();
1014 p->_lValueList = _lValueList;
1015 return p;
1016 }
1017
1018 void PropertyPlacementList::Paste(const Property& from)
1019 {
1020 setValues(dynamic_cast<const PropertyPlacementList&>(from)._lValueList);
1021 }
1022
1023 unsigned int PropertyPlacementList::getMemSize() const
1024 {
1025 return static_cast<unsigned int>(_lValueList.size() * sizeof(Base::Vector3d));
1026 }
1027
1028
1029 //**************************************************************************
1030 //**************************************************************************
1031 // PropertyPlacement
1032 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1033
1034 TYPESYSTEM_SOURCE(App::PropertyPlacementLink, App::PropertyLink)
1035
1036 //**************************************************************************
1037 // Construction/Destruction
1038
1039
1040 PropertyPlacementLink::PropertyPlacementLink() = default;
1041
1042
1043 PropertyPlacementLink::~PropertyPlacementLink() = default;
1044
1045 App::Placement* PropertyPlacementLink::getPlacementObject() const
1046 {
1047 if (_pcLink->isDerivedFrom<App::Placement>()) {
1048 return dynamic_cast<App::Placement*>(_pcLink);
1049 }
1050 else {
1051 return nullptr;
1052 }
1053 }
1054
1055 //**************************************************************************
1056 // Base class implementer
1057
1058 Property* PropertyPlacementLink::Copy() const
1059 {
1060 PropertyPlacementLink* p = new PropertyPlacementLink();
1061 p->_pcLink = _pcLink;
1062 return p;
1063 }
1064
1065 void PropertyPlacementLink::Paste(const Property& from)
1066 {
1067 aboutToSetValue();
1068 _pcLink = dynamic_cast<const PropertyPlacementLink&>(from)._pcLink;
1069 hasSetValue();
1070 }
1071
1072 //**************************************************************************
1073 //**************************************************************************
1074 // PropertyRotation
1075 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1076
1077 TYPESYSTEM_SOURCE(App::PropertyRotation, App::Property)
1078
1079 PropertyRotation::PropertyRotation() = default;
1080
1081
1082 PropertyRotation::~PropertyRotation() = default;
1083
1084 void PropertyRotation::setValue(const Base::Rotation& rot)
1085 {
1086 aboutToSetValue();
1087 _rot = rot;
1088 hasSetValue();
1089 }
1090
1091 bool PropertyRotation::setValueIfChanged(const Base::Rotation& rot, double atol)
1092 {
1093 if (_rot.isSame(rot, atol)) {
1094 return false;
1095 }
1096
1097 setValue(rot);
1098 return true;
1099 }
1100
1101
1102 const Base::Rotation& PropertyRotation::getValue() const
1103 {
1104 return _rot;
1105 }
1106
1107 void PropertyRotation::getPaths(std::vector<ObjectIdentifier>& paths) const
1108 {
1109 paths.push_back(ObjectIdentifier(*this)
1110 << ObjectIdentifier::SimpleComponent(ObjectIdentifier::String("Angle")));
1111 paths.push_back(ObjectIdentifier(*this)
1112 << ObjectIdentifier::SimpleComponent(ObjectIdentifier::String("Axis"))
1113 << ObjectIdentifier::SimpleComponent(ObjectIdentifier::String("x")));
1114 paths.push_back(ObjectIdentifier(*this)
1115 << ObjectIdentifier::SimpleComponent(ObjectIdentifier::String("Axis"))
1116 << ObjectIdentifier::SimpleComponent(ObjectIdentifier::String("y")));
1117 paths.push_back(ObjectIdentifier(*this)
1118 << ObjectIdentifier::SimpleComponent(ObjectIdentifier::String("Axis"))
1119 << ObjectIdentifier::SimpleComponent(ObjectIdentifier::String("z")));
1120 }
1121
1122 void PropertyRotation::setPathValue(const ObjectIdentifier& path, const boost::any& value)
1123 {
1124 auto updateAxis = [this](int index, double coord) {
1125 Base::Vector3d axis;
1126 double angle;
1127 _rot.getRawValue(axis, angle);
1128
1129 axis[index] = coord;
1130 setValue(Base::Rotation {axis, angle});
1131 };
1132
1133 std::string subpath = path.getSubPathStr();
1134 if (subpath == ".Angle") {
1135 double avalue = toDouble(value);
1136 Property::setPathValue(path, Base::toRadians(avalue));
1137 }
1138 else if (subpath == ".Axis.x") {
1139 updateAxis(0, toDouble(value));
1140 }
1141 else if (subpath == ".Axis.y") {
1142 updateAxis(1, toDouble(value));
1143 }
1144 else if (subpath == ".Axis.z") {
1145 updateAxis(2, toDouble(value));
1146 }
1147 else {
1148 Property::setPathValue(path, value);
1149 }
1150 }
1151
1152 const boost::any PropertyRotation::getPathValue(const ObjectIdentifier& path) const
1153 {
1154 auto getAxis = [](const Base::Rotation& rot) {
1155 Base::Vector3d axis;
1156 double angle;
1157 rot.getRawValue(axis, angle);
1158 return axis;
1159 };
1160 std::string p = path.getSubPathStr();
1161
1162 if (p == ".Angle") {
1163 // Convert angle to degrees
1164 return Base::Quantity(
1165 Base::toDegrees(boost::any_cast<double>(Property::getPathValue(path))),
1166 Unit::Angle);
1167 }
1168 else if (p == ".Axis.x") {
1169 return getAxis(_rot).x;
1170 }
1171 else if (p == ".Axis.y") {
1172 return getAxis(_rot).y;
1173 }
1174 else if (p == ".Axis.z") {
1175 return getAxis(_rot).z;
1176 }
1177 else {
1178 return Property::getPathValue(path);
1179 }
1180 }
1181
1182 bool PropertyRotation::getPyPathValue(const ObjectIdentifier& path, Py::Object& res) const
1183 {
1184 auto getAxis = [](const Base::Rotation& rot) {
1185 Base::Vector3d axis;
1186 double angle;
1187 rot.getRawValue(axis, angle);
1188 return axis;
1189 };
1190
1191 std::string p = path.getSubPathStr();
1192 if (p == ".Angle") {
1193 Base::Vector3d axis;
1194 double angle;
1195 _rot.getValue(axis, angle);
1196 res = Py::asObject(new QuantityPy(new Quantity(Base::toDegrees(angle), Unit::Angle)));
1197 return true;
1198 }
1199 else if (p == ".Axis.x") {
1200 res = Py::Float(getAxis(_rot).x);
1201 return true;
1202 }
1203 else if (p == ".Axis.y") {
1204 res = Py::Float(getAxis(_rot).y);
1205 return true;
1206 }
1207 else if (p == ".Axis.z") {
1208 res = Py::Float(getAxis(_rot).z);
1209 return true;
1210 }
1211
1212 return false;
1213 }
1214
1215 PyObject* PropertyRotation::getPyObject()
1216 {
1217 return new Base::RotationPy(new Base::Rotation(_rot));
1218 }
1219
1220 void PropertyRotation::setPyObject(PyObject* value)
1221 {
1222 if (PyObject_TypeCheck(value, &(Base::MatrixPy::Type))) {
1223 Base::MatrixPy* object = static_cast<Base::MatrixPy*>(value);
1224 Base::Matrix4D mat = object->value();
1225 Base::Rotation p;
1226 p.setValue(mat);
1227 setValue(p);
1228 }
1229 else if (PyObject_TypeCheck(value, &(Base::RotationPy::Type))) {
1230 setValue(*static_cast<Base::RotationPy*>(value)->getRotationPtr());
1231 }
1232 else {
1233 std::string error = std::string("type must be 'Matrix' or 'Rotation', not ");
1234 error += value->ob_type->tp_name;
1235 throw Base::TypeError(error);
1236 }
1237 }
1238
1239 void PropertyRotation::Save(Base::Writer& writer) const
1240 {
1241 Vector3d axis;
1242 double rfAngle {};
1243 _rot.getRawValue(axis, rfAngle);
1244
1245 writer.Stream() << writer.ind() << "<PropertyRotation";
1246 writer.Stream() << " A=\"" << rfAngle << "\""
1247 << " Ox=\"" << axis.x << "\""
1248 << " Oy=\"" << axis.y << "\""
1249 << " Oz=\"" << axis.z << "\""
1250 << "/>\n";
1251 }
1252
1253 void PropertyRotation::Restore(Base::XMLReader& reader)
1254 {
1255 reader.readElement("PropertyRotation");
1256 aboutToSetValue();
1257
1258 _rot = Rotation(Vector3d(reader.getAttribute<double>("Ox"),
1259 reader.getAttribute<double>("Oy"),
1260 reader.getAttribute<double>("Oz")),
1261 reader.getAttribute<double>("A"));
1262 hasSetValue();
1263 }
1264
1265 Property* PropertyRotation::Copy() const
1266 {
1267 PropertyRotation* p = new PropertyRotation();
1268 p->_rot = _rot;
1269 return p;
1270 }
1271
1272 void PropertyRotation::Paste(const Property& from)
1273 {
1274 aboutToSetValue();
1275 _rot = dynamic_cast<const PropertyRotation&>(from)._rot;
1276 hasSetValue();
1277 }
1278
1279 // ------------------------------------------------------------
1280
1281 TYPESYSTEM_SOURCE_ABSTRACT(App::PropertyGeometry, App::Property)
1282
1283 PropertyGeometry::PropertyGeometry() = default;
1284
1285 PropertyGeometry::~PropertyGeometry() = default;
1286
1287 // ------------------------------------------------------------
1288
1289 TYPESYSTEM_SOURCE_ABSTRACT(App::PropertyComplexGeoData, App::PropertyGeometry)
1290
1291 PropertyComplexGeoData::PropertyComplexGeoData() = default;
1292
1293 PropertyComplexGeoData::~PropertyComplexGeoData() = default;
1294
1295 std::string PropertyComplexGeoData::getElementMapVersion(bool) const
1296 {
1297 auto data = getComplexData();
1298 if (!data) {
1299 return std::string();
1300 }
1301 return data->getElementMapVersion();
1302 }
1303
1304 bool PropertyComplexGeoData::checkElementMapVersion(const char* ver) const
1305 {
1306 auto data = getComplexData();
1307 if (!data) {
1308 return false;
1309 }
1310 return data->checkElementMapVersion(ver + 2);
1311 }
1312
1313
1314 void PropertyComplexGeoData::afterRestore()
1315 {
1316 auto data = getComplexData();
1317 if (data && data->isRestoreFailed()) {
1318 data->resetRestoreFailure();
1319 auto owner = freecad_cast<DocumentObject*>(getContainer());
1320 if (owner && owner->getDocument()
1321 && !owner->getDocument()->testStatus(App::Document::PartialDoc)) {
1322 owner->getDocument()->addRecomputeObject(owner);
1323 }
1324 }
1325 PropertyGeometry::afterRestore();
1326 }