File size: 54,271 Bytes
985c397 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 | // SPDX-License-Identifier: LGPL-2.1-or-later
/***************************************************************************
* Copyright (c) 2011 Jürgen Riegel <juergen.riegel@web.de> *
* Copyright (c) 2011 Werner Mayer <wmayer[at]users.sourceforge.net> *
* *
* This file is part of the FreeCAD CAx development system. *
* *
* This library is free software; you can redistribute it and/or *
* modify it under the terms of the GNU Library General Public *
* License as published by the Free Software Foundation; either *
* version 2 of the License, or (at your option) any later version. *
* *
* This library is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU Library General Public License for more details. *
* *
* You should have received a copy of the GNU Library General Public *
* License along with this library; see the file COPYING.LIB. If not, *
* write to the Free Software Foundation, Inc., 59 Temple Place, *
* Suite 330, Boston, MA 02111-1307, USA *
* *
***************************************************************************/
#include <stack>
#include <memory>
#include <map>
#include <set>
#include <vector>
#include <string>
#include <Base/Console.h>
#include <Base/Matrix.h>
#include <Base/Placement.h>
#include <Base/Tools.h>
#include <Base/Writer.h>
#include "Expression.h"
#include "Application.h"
#include "ElementNamingUtils.h"
#include "Document.h"
#include "DocumentObject.h"
#include "DocumentObjectPy.h"
#include "DocumentObjectExtension.h"
#include "DocumentObjectGroup.h"
#include "GeoFeatureGroupExtension.h"
#include "Link.h"
#include "ObjectIdentifier.h"
#include "PropertyExpressionEngine.h"
#include "PropertyLinks.h"
FC_LOG_LEVEL_INIT("App", true, true)
using namespace App;
PROPERTY_SOURCE(App::DocumentObject, App::TransactionalObject)
DocumentObjectExecReturn* DocumentObject::StdReturn = nullptr;
//===========================================================================
// DocumentObject
//===========================================================================
DocumentObject::DocumentObject()
: ExpressionEngine()
{
// define Label of type 'Output' to avoid being marked as touched after relabeling
ADD_PROPERTY_TYPE(Label, ("Unnamed"), "Base", Prop_Output, "User name of the object (UTF8)");
ADD_PROPERTY_TYPE(Label2, (""), "Base", Prop_Hidden, "User description of the object (UTF8)");
Label2.setStatus(App::Property::Output, true);
ADD_PROPERTY_TYPE(ExpressionEngine, (), "Base", Prop_Hidden, "Property expressions");
ADD_PROPERTY(Visibility, (true));
// default set Visibility status to hidden and output (no touch) for
// compatibitily reason. We use setStatus instead of PropertyType to
// allow user to change its status later
Visibility.setStatus(Property::Output, true);
Visibility.setStatus(Property::Hidden, true);
Visibility.setStatus(Property::NoModify, true);
}
DocumentObject::~DocumentObject()
{
if (!PythonObject.is(Py::_None())) {
Base::PyGILStateLocker lock;
// Remark: The API of Py::Object has been changed to set whether the wrapper owns the passed
// Python object or not. In the constructor we forced the wrapper to own the object so we
// need not to dec'ref the Python object any more. But we must still invalidate the Python
// object because it need not to be destructed right now because the interpreter can own
// several references to it.
Base::PyObjectBase* obj = static_cast<Base::PyObjectBase*>(PythonObject.ptr());
// Call before decrementing the reference counter, otherwise a heap error can occur
obj->setInvalid();
}
}
void DocumentObject::printInvalidLinks() const
{
try {
// Get objects that have invalid link scope, and print their names.
// Truncate the invalid object list name strings for readability, if they happen to be very
// long.
std::vector<App::DocumentObject*> invalid_linkobjs;
std::string objnames, scopenames;
GeoFeatureGroupExtension::getInvalidLinkObjects(this, invalid_linkobjs);
for (auto& obj : invalid_linkobjs) {
objnames += obj->getNameInDocument();
objnames += " ";
for (auto& scope : obj->getParents()) {
if (scopenames.length() > 80) {
scopenames += "... ";
break;
}
scopenames += scope.first->getNameInDocument();
scopenames += " ";
}
if (objnames.length() > 80) {
objnames += "... ";
break;
}
}
if (objnames.empty()) {
objnames = "N/A";
}
else {
objnames.pop_back();
}
if (scopenames.empty()) {
scopenames = "N/A";
}
else {
scopenames.pop_back();
}
Base::Console().warning("%s: Link(s) to object(s) '%s' go out of the allowed scope '%s'. "
"Instead, the linked object(s) reside within '%s'.\n",
getTypeId().getName(),
objnames.c_str(),
getNameInDocument(),
scopenames.c_str());
}
catch (const Base::Exception& e) {
e.reportException();
}
}
App::DocumentObjectExecReturn* DocumentObject::recompute()
{
// check if the links are valid before making the recompute
if (!GeoFeatureGroupExtension::areLinksValid(this)) {
printInvalidLinks();
}
// set/unset the execution bit
Base::ObjectStatusLocker<ObjectStatus, DocumentObject> exe(App::Recompute, this);
// mark the object to recompute its extensions
this->setStatus(App::RecomputeExtension, true);
auto ret = this->execute();
if (ret == StdReturn) {
// most feature classes don't call the execute() method of its base class
// so execute the extensions now
if (this->testStatus(App::RecomputeExtension)) {
ret = executeExtensions();
}
}
return ret;
}
DocumentObjectExecReturn* DocumentObject::execute()
{
return executeExtensions();
}
App::DocumentObjectExecReturn* DocumentObject::executeExtensions()
{
// execute extensions but stop on error
this->setStatus(App::RecomputeExtension, false); // reset the flag
auto vector = getExtensionsDerivedFromType<App::DocumentObjectExtension>();
for (auto ext : vector) {
auto ret = ext->extensionExecute();
if (ret != StdReturn) {
return ret;
}
}
return StdReturn;
}
bool DocumentObject::recomputeFeature(bool recursive)
{
Document* doc = this->getDocument();
if (doc) {
return doc->recomputeFeature(this, recursive);
}
return isValid();
}
/**
* @brief Set this document object touched.
* Touching a document object does not mean to recompute it, it only means that
* other document objects that link it (i.e. its InList) will be recomputed.
* If it should be forced to recompute a document object then use
* \ref enforceRecompute() instead.
*/
void DocumentObject::touch(bool noRecompute)
{
if (!noRecompute) {
StatusBits.set(ObjectStatus::Enforce);
}
StatusBits.set(ObjectStatus::Touch);
if (_pDoc) {
_pDoc->signalTouchedObject(*this);
}
}
/**
* @brief Set this document object freezed.
* A freezed document object does not recompute ever.
*/
void DocumentObject::freeze()
{
StatusBits.set(ObjectStatus::Freeze);
// store read-only property names
this->readOnlyProperties.clear();
std::vector<std::pair<const char*, Property*>> list;
static_cast<App::PropertyContainer*>(this)->getPropertyNamedList(list);
for (auto pair: list){
if (pair.second->isReadOnly()){
this->readOnlyProperties.push_back(pair.first);
} else {
pair.second->setReadOnly(true);
}
}
// use the signalTouchedObject to refresh the Gui
if (_pDoc) {
_pDoc->signalTouchedObject(*this);
}
}
/**
* @brief Set this document object unfreezed.
* A freezed document object does not recompute ever.
*/
void DocumentObject::unfreeze(bool noRecompute)
{
StatusBits.reset(ObjectStatus::Freeze);
// reset read-only property status
std::vector<std::pair<const char*, Property*>> list;
static_cast<App::PropertyContainer*>(this)->getPropertyNamedList(list);
for (auto pair: list){
if (! std::count(readOnlyProperties.begin(), readOnlyProperties.end(), pair.first)){
pair.second->setReadOnly(false);
}
}
touch(noRecompute);
}
/**
* @brief Check whether the document object is touched or not.
* @return true if document object is touched, false if not.
*/
bool DocumentObject::isTouched() const
{
return ExpressionEngine.isTouched() || StatusBits.test(ObjectStatus::Touch);
}
/**
* @brief Enforces this document object to be recomputed.
* This can be useful to recompute the feature without
* having to change one of its input properties.
*/
void DocumentObject::enforceRecompute()
{
touch(false);
}
/**
* @brief Check whether the document object must be recomputed or not.
* This means that the 'Enforce' flag is set or that \ref mustExecute()
* returns a value > 0.
* @return true if document object must be recomputed, false if not.
*/
bool DocumentObject::mustRecompute() const
{
if (StatusBits.test(ObjectStatus::Freeze)) {
return false;
}
if (StatusBits.test(ObjectStatus::Enforce)) {
return true;
}
return mustExecute() > 0;
}
short DocumentObject::mustExecute() const
{
if (ExpressionEngine.isTouched()) {
return 1;
}
// ask all extensions
auto vector = getExtensionsDerivedFromType<App::DocumentObjectExtension>();
for (auto ext : vector) {
if (ext->extensionMustExecute()) {
return 1;
}
}
return 0;
}
const char* DocumentObject::getStatusString() const
{
if (isError()) {
const char* text = getDocument()->getErrorDescription(this);
return text ? text : "Error";
}
else if (isFreezed()){
return "Freezed";
}
else if (isTouched()) {
return "Touched";
}
else {
return "Valid";
}
}
std::string DocumentObject::getFullName() const
{
if (!getDocument() || !isAttachedToDocument()) {
return "?";
}
std::string name(getDocument()->getName());
name += '#';
name += *pcNameInDocument;
return name;
}
std::string DocumentObject::getFullLabel() const
{
if (!getDocument()) {
return "?";
}
auto name = getDocument()->Label.getStrValue();
name += "#";
name += Label.getStrValue();
return name;
}
const char* DocumentObject::getDagKey() const
{
if (!pcNameInDocument) {
return nullptr;
}
return pcNameInDocument->c_str();
}
const char* DocumentObject::getNameInDocument() const
{
// Note: It can happen that we query the internal name of an object even if it is not
// part of a document (anymore). This is the case e.g. if we have a reference in Python
// to an object that has been removed from the document. In this case we should rather
// return 0.
// assert(pcNameInDocument);
if (!pcNameInDocument) {
return nullptr;
}
return pcNameInDocument->c_str();
}
int DocumentObject::isExporting() const
{
if (!getDocument() || !isAttachedToDocument()) {
return 0;
}
return getDocument()->isExporting(this);
}
std::string DocumentObject::getExportName(bool forced) const
{
if (!isAttachedToDocument()) {
return {};
}
if (!forced && !isExporting()) {
return *pcNameInDocument;
}
// '@' is an invalid character for an internal name, which ensures the
// following returned name will be unique in any document. Saving external
// object like that shall only happens in Document::exportObjects(). We
// shall strip out this '@' and the following document name during restoring.
return *pcNameInDocument + '@' + getDocument()->getName();
}
bool DocumentObject::isAttachedToDocument() const
{
return (pcNameInDocument != nullptr);
}
const char* DocumentObject::detachFromDocument()
{
const std::string* name = pcNameInDocument;
pcNameInDocument = nullptr;
return name ? name->c_str() : nullptr;
}
const std::vector<DocumentObject*>& DocumentObject::getOutList() const
{
if (!_outListCached) {
_outList.clear();
getOutList(0, _outList);
_outListCached = true;
}
return _outList;
}
std::vector<DocumentObject*> DocumentObject::getOutList(int options) const
{
std::vector<DocumentObject*> res;
getOutList(options, res);
return res;
}
void DocumentObject::getOutList(int options, std::vector<DocumentObject*>& res) const
{
if (_outListCached && !options) {
res.insert(res.end(), _outList.begin(), _outList.end());
return;
}
std::vector<Property*> props;
getPropertyList(props);
bool noHidden = !!(options & OutListNoHidden);
std::size_t size = res.size();
for (auto prop : props) {
auto link = freecad_cast<PropertyLinkBase*>(prop);
if (link) {
link->getLinks(res, noHidden);
}
}
if (!(options & OutListNoExpression)) {
ExpressionEngine.getLinks(res);
}
if (options & OutListNoXLinked) {
for (auto it = res.begin() + size; it != res.end();) {
auto obj = *it;
if (obj && obj->getDocument() != getDocument()) {
it = res.erase(it);
}
else {
++it;
}
}
}
}
std::vector<App::DocumentObject*> DocumentObject::getOutListOfProperty(App::Property* prop) const
{
std::vector<DocumentObject*> ret;
if (!prop || prop->getContainer() != this) {
return ret;
}
auto link = freecad_cast<PropertyLinkBase*>(prop);
if (link) {
link->getLinks(ret);
}
return ret;
}
const std::vector<App::DocumentObject*>& DocumentObject::getInList() const
{
return _inList;
}
// The original algorithm is highly inefficient in some special case.
// Considering an object is linked by every other objects. After excluding this
// object, there is another object linked by every other of the remaining
// objects, and so on. The vector 'result' above will be of magnitude n^2.
// Even if we replace the vector with a set, we still need to visit that amount
// of objects. And this may not be the worst case. getInListEx() has no such
// problem.
std::vector<App::DocumentObject*> DocumentObject::getInListRecursive() const
{
std::set<App::DocumentObject*> inSet;
std::vector<App::DocumentObject*> res;
getInListEx(inSet, true, &res);
return res;
}
// More efficient algorithm to find the recursive inList of an object,
// including possible external parents. One shortcoming of this algorithm is
// it does not detect cyclic reference, althgouth it won't crash either.
void DocumentObject::getInListEx(std::set<App::DocumentObject*>& inSet,
bool recursive,
std::vector<App::DocumentObject*>* inList) const
{
if (!recursive) {
inSet.insert(_inList.begin(), _inList.end());
if (inList) {
*inList = _inList;
}
return;
}
std::stack<DocumentObject*> pendings;
pendings.push(const_cast<DocumentObject*>(this));
while (!pendings.empty()) {
auto obj = pendings.top();
pendings.pop();
for (auto o : obj->getInList()) {
if (o && o->isAttachedToDocument() && inSet.insert(o).second) {
pendings.push(o);
if (inList) {
inList->push_back(o);
}
}
}
}
}
std::set<App::DocumentObject*> DocumentObject::getInListEx(bool recursive) const
{
std::set<App::DocumentObject*> ret;
getInListEx(ret, recursive);
return ret;
}
void _getOutListRecursive(std::set<DocumentObject*>& objSet,
const DocumentObject* obj,
const DocumentObject* checkObj,
int depth)
{
for (const auto objIt : obj->getOutList()) {
// if the check object is in the recursive inList we have a cycle!
if (objIt == checkObj || depth <= 0) {
throw Base::BadGraphError(
"DocumentObject::getOutListRecursive(): cyclic dependency detected!");
}
// if the element was already in the set then there is no need to process it again
auto pair = objSet.insert(objIt);
if (pair.second) {
_getOutListRecursive(objSet, objIt, checkObj, depth - 1);
}
}
}
std::vector<App::DocumentObject*> DocumentObject::getOutListRecursive() const
{
// number of objects in document is a good estimate in result size
int maxDepth = GetApplication().checkLinkDepth(0);
std::set<App::DocumentObject*> result;
// using a recursive helper to collect all OutLists
_getOutListRecursive(result, this, this, maxDepth);
std::vector<App::DocumentObject*> array;
array.insert(array.begin(), result.begin(), result.end());
return array;
}
// helper for isInInListRecursive()
bool _isInInListRecursive(const DocumentObject* act, const DocumentObject* checkObj, int depth)
{
for (auto obj : act->getInList()) {
if (obj == checkObj) {
return true;
}
// if we reach the depth limit we have a cycle!
if (depth <= 0) {
throw Base::BadGraphError(
"DocumentObject::isInInListRecursive(): cyclic dependency detected!");
}
if (_isInInListRecursive(obj, checkObj, depth - 1)) {
return true;
}
}
return false;
}
bool DocumentObject::isInInListRecursive(DocumentObject* linkTo) const
{
return this == linkTo || getInListEx(true).contains(linkTo);
}
bool DocumentObject::isInInList(DocumentObject* linkTo) const
{
if (std::ranges::find(_inList, linkTo) != _inList.end()) {
return true;
}
else {
return false;
}
}
// helper for isInOutListRecursive()
bool _isInOutListRecursive(const DocumentObject* act, const DocumentObject* checkObj, int depth)
{
for (auto obj : act->getOutList()) {
if (obj == checkObj) {
return true;
}
// if we reach the depth limit we have a cycle!
if (depth <= 0) {
throw Base::BadGraphError(
"DocumentObject::isInOutListRecursive(): cyclic dependency detected!");
}
if (_isInOutListRecursive(obj, checkObj, depth - 1)) {
return true;
}
}
return false;
}
bool DocumentObject::isInOutListRecursive(DocumentObject* linkTo) const
{
int maxDepth = getDocument()->countObjects() + 2;
return _isInOutListRecursive(this, linkTo, maxDepth);
}
std::vector<std::list<App::DocumentObject*>>
DocumentObject::getPathsByOutList(App::DocumentObject* to) const
{
return _pDoc->getPathsByOutList(this, to);
}
DocumentObjectGroup* DocumentObject::getGroup() const
{
return freecad_cast<DocumentObjectGroup*>(GroupExtension::getGroupOfObject(this));
}
bool DocumentObject::testIfLinkDAGCompatible(DocumentObject* linkTo) const
{
std::vector<App::DocumentObject*> linkTo_in_vector;
linkTo_in_vector.push_back(linkTo);
return this->testIfLinkDAGCompatible(linkTo_in_vector);
}
bool DocumentObject::testIfLinkDAGCompatible(const std::vector<DocumentObject*>& linksTo) const
{
auto inLists = getInListEx(true);
inLists.emplace(const_cast<DocumentObject*>(this));
for (auto obj : linksTo) {
if (inLists.contains(obj)) {
return false;
}
}
return true;
}
bool DocumentObject::testIfLinkDAGCompatible(PropertyLinkSubList& linksTo) const
{
const std::vector<App::DocumentObject*>& linksTo_in_vector = linksTo.getValues();
return this->testIfLinkDAGCompatible(linksTo_in_vector);
}
bool DocumentObject::testIfLinkDAGCompatible(PropertyLinkSub& linkTo) const
{
std::vector<App::DocumentObject*> linkTo_in_vector;
linkTo_in_vector.reserve(1);
linkTo_in_vector.push_back(linkTo.getValue());
return this->testIfLinkDAGCompatible(linkTo_in_vector);
}
void DocumentObject::onLostLinkToObject(DocumentObject*)
{}
App::Document* DocumentObject::getDocument() const
{
return _pDoc;
}
void DocumentObject::setDocument(App::Document* doc)
{
_pDoc = doc;
onSettingDocument();
}
bool DocumentObject::removeDynamicProperty(const char* name)
{
if (!_pDoc || testStatus(ObjectStatus::Destroy)) {
return false;
}
Property* prop = getDynamicPropertyByName(name);
if (!prop || prop->testStatus(App::Property::LockDynamic)) {
return false;
}
if (prop->isDerivedFrom<PropertyLinkBase>()) {
clearOutListCache();
}
_pDoc->addOrRemovePropertyOfObject(this, prop, false);
auto expressions = ExpressionEngine.getExpressions();
std::vector<App::ObjectIdentifier> removeExpr;
for (const auto& it : expressions) {
if (it.first.getProperty() == prop) {
removeExpr.push_back(it.first);
}
}
for (const auto& it : removeExpr) {
ExpressionEngine.setValue(it, std::shared_ptr<Expression>());
}
return TransactionalObject::removeDynamicProperty(name);
}
bool DocumentObject::renameDynamicProperty(Property* prop, const char* name)
{
std::string oldName = prop->getName();
auto expressions = ExpressionEngine.getExpressions();
std::vector<std::shared_ptr<Expression>> expressionsToMove;
std::vector<App::ObjectIdentifier> idsWithExprsToRemove;
for (const auto& [id, expr] : expressions) {
if (id.getProperty() == prop) {
idsWithExprsToRemove.push_back(id);
expressionsToMove.emplace_back(expr->copy());
}
}
for (const auto& it : idsWithExprsToRemove) {
ExpressionEngine.setValue(it, std::shared_ptr<Expression>());
}
bool renamed = TransactionalObject::renameDynamicProperty(prop, name);
if (renamed && _pDoc) {
_pDoc->renamePropertyOfObject(this, prop, oldName.c_str());
}
App::ObjectIdentifier idNewProp(prop->getContainer(), std::string(name));
for (auto& exprToMove : expressionsToMove) {
ExpressionEngine.setValue(idNewProp, exprToMove);
}
return renamed;
}
App::Property* DocumentObject::addDynamicProperty(const char* type,
const char* name,
const char* group,
const char* doc,
short attr,
bool ro,
bool hidden)
{
auto prop = TransactionalObject::addDynamicProperty(type, name, group, doc, attr, ro, hidden);
if (prop && _pDoc) {
_pDoc->addOrRemovePropertyOfObject(this, prop, true);
}
return prop;
}
void DocumentObject::onBeforeChange(const Property* prop)
{
if (isFreezed() && prop != &Visibility) {
return;
}
// Store current name in oldLabel, to be able to easily retrieve old name of document object later
// when renaming expressions.
if (prop == &Label)
oldLabel = Label.getStrValue();
if (_pDoc){
onBeforeChangeProperty(_pDoc, prop);
}
signalBeforeChange(*this, *prop);
}
std::vector<std::pair<Property*, std::unique_ptr<Property>>>
DocumentObject::onProposedLabelChange(std::string& newLabel)
{
// Note that this work can't be done in onBeforeChangeLabel because FeaturePython overrides this
// method and does not initially base-call it.
// We re only called if the new label differs from the old one, and our code to check duplicates
// may not work if this is not the case.
std::string oldLabel = Label.getStrValue();
assert(newLabel != oldLabel);
if (!isAttachedToDocument()) {
return {};
}
App::Document* doc = getDocument();
if (doc->isPerformingTransaction()
|| (doc->testStatus(App::Document::Restoring)
&& !doc->testStatus(App::Document::Importing))) {
return {};
}
static ParameterGrp::handle _hPGrp;
if (!_hPGrp) {
_hPGrp = App::GetApplication().GetParameterGroupByPath("User parameter:BaseApp/Preferences/Document");
}
if (doc && !newLabel.empty() && !_hPGrp->GetBool("DuplicateLabels") && !allowDuplicateLabel()
&& doc->containsLabel(newLabel)) {
// The label already exists but settings are such that duplicate labels should not be assigned.
std::string objName = getNameInDocument();
if (!doc->containsLabel(objName) && doc->haveSameBaseName(objName, newLabel)) {
// The object name is not already a Label and the base name of the proposed label
// equals the base name of the object Name, so we use the object Name as the replacement Label.
newLabel = objName;
}
else {
// Otherwise we generate a unique Label using newLabel as a prototype name. In doing so,
// we must also act as if the current value of the property is not an existing Label
// entry.
// We deregister the old label so it does not interfere with making the new label,
// and re-register it after. This is probably a bit less efficient that having a special
// make-unique-label-as-if-this-one-did-not-exist method, but such a method would be a real
// ugly wart.
doc->unregisterLabel(oldLabel);
newLabel = doc->makeUniqueLabel(newLabel);
doc->registerLabel(oldLabel);
}
}
// Despite our efforts to make a unique label, onBeforeLabelChange can change it.
onBeforeChangeLabel(newLabel);
if (oldLabel == newLabel || getDocument()->testStatus(App::Document::Restoring)) {
// Don't update label reference if we are restoring or if the label is unchanged.
// When importing (which also counts as restoring), it is possible the
// new object changes its label. However, we cannot update label
// references here, because object being restored is not based on
// dependency order. It can only be done in afterRestore().
//
// See PropertyLinkBase::restoreLabelReference() for more details.
return {};
}
return PropertyLinkBase::updateLabelReferences(this, newLabel.c_str());
}
void DocumentObject::onEarlyChange(const Property* prop)
{
if (isFreezed() && prop != &Visibility) {
return;
}
if (GetApplication().isClosingAll()) {
return;
}
if (!GetApplication().isRestoring() && !prop->testStatus(Property::PartialTrigger)
&& getDocument() && getDocument()->testStatus(Document::PartialDoc)) {
static App::Document* warnedDoc;
if (warnedDoc != getDocument()) {
warnedDoc = getDocument();
FC_WARN("Changes to partial loaded document will not be saved: " << getFullName() << '.'
<< prop->getName());
}
}
signalEarlyChanged(*this, *prop);
}
/// get called by the container when a Property was changed
void DocumentObject::onChanged(const Property* prop)
{
if (prop == &Label && _pDoc && _pDoc->containsObject(this) && oldLabel != Label.getStrValue()) {
_pDoc->unregisterLabel(oldLabel);
_pDoc->registerLabel(Label.getStrValue());
}
if (isFreezed() && prop != &Visibility) {
return;
}
if (GetApplication().isClosingAll()) {
return;
}
if (!GetApplication().isRestoring() && !prop->testStatus(Property::PartialTrigger)
&& getDocument() && getDocument()->testStatus(Document::PartialDoc)) {
static App::Document* warnedDoc;
if (warnedDoc != getDocument()) {
warnedDoc = getDocument();
FC_WARN("Changes to partial loaded document will not be saved: " << getFullName() << '.'
<< prop->getName());
}
}
// Delay signaling view provider until the document object has handled the
// change
// if (_pDoc)
// _pDoc->onChangedProperty(this,prop);
if (prop == &Label && _pDoc && oldLabel != Label.getStrValue()) {
_pDoc->signalRelabelObject(*this);
}
// set object touched if it is an input property
if (!testStatus(ObjectStatus::NoTouch) && !(prop->getType() & Prop_Output)
&& !prop->testStatus(Property::Output)) {
if (!StatusBits.test(ObjectStatus::Touch)) {
FC_TRACE("touch '" << getFullName() << "' on change of '" << prop->getName() << "'");
StatusBits.set(ObjectStatus::Touch);
}
// must execute on document recompute
if (!(prop->getType() & Prop_NoRecompute)) {
StatusBits.set(ObjectStatus::Enforce);
}
}
// call the parent for appropriate handling
TransactionalObject::onChanged(prop);
// Now signal the view provider
if (_pDoc) {
_pDoc->onChangedProperty(this, prop);
}
signalChanged(*this, *prop);
}
void DocumentObject::clearOutListCache() const
{
_outList.clear();
_outListMap.clear();
_outListCached = false;
}
PyObject* DocumentObject::getPyObject()
{
if (PythonObject.is(Py::_None())) {
// ref counter is set to 1
PythonObject = Py::Object(new DocumentObjectPy(this), true);
}
return Py::new_reference_to(PythonObject);
}
DocumentObject* DocumentObject::getSubObject(const char* subname,
PyObject** pyObj,
Base::Matrix4D* mat,
bool transform,
int depth) const
{
DocumentObject* ret = nullptr;
auto exts = getExtensionsDerivedFromType<App::DocumentObjectExtension>();
for (auto ext : exts) {
if (ext->extensionGetSubObject(ret, subname, pyObj, mat, transform, depth)) {
return ret;
}
}
std::string name;
const char* dot = nullptr;
if (!subname || !(dot = strchr(subname, '.'))) {
ret = const_cast<DocumentObject*>(this);
}
else if (subname[0] == '$') {
name = std::string(subname + 1, dot);
for (auto obj : getOutList()) {
if (name == obj->Label.getValue()) {
ret = obj;
break;
}
}
}
else {
name = std::string(subname, dot);
const auto& outList = getOutList();
if (outList.size() != _outListMap.size()) {
_outListMap.clear();
for (auto obj : outList) {
_outListMap[obj->getDagKey()] = obj;
}
}
auto it = _outListMap.find(name.c_str());
if (it != _outListMap.end()) {
ret = it->second;
}
}
// TODO: By right, normal object's placement does not transform its sub
// objects (think of the claimed children of a Fusion). But I do think we
// should change that.
if (transform && mat) {
auto pla = freecad_cast<PropertyPlacement*>(getPropertyByName("Placement"));
if (pla) {
*mat *= pla->getValue().toMatrix();
}
}
if (ret && dot) {
return ret->getSubObject(dot + 1, pyObj, mat, true, depth + 1);
}
return ret;
}
namespace
{
std::vector<DocumentObject*>
getSubObjectListFlatten(const std::vector<App::DocumentObject*>& resNotFlatten,
std::vector<int>* const subsizes,
const App::DocumentObject* sobj,
App::DocumentObject** container,
bool& lastChild)
{
auto res {resNotFlatten};
auto linked = sobj->getLinkedObject();
if (*container) {
auto grp = App::GeoFeatureGroupExtension::getGroupOfObject(linked);
if (grp != *container) {
*container = nullptr;
}
else {
if (lastChild && !res.empty()) {
res.pop_back();
if (subsizes) {
subsizes->pop_back();
}
}
lastChild = true;
}
}
if (linked->getExtensionByType<App::GeoFeatureGroupExtension>(true)) {
*container = linked;
lastChild = false;
}
else if (linked != sobj || sobj->hasChildElement()) {
// Check for Link or LinkGroup
*container = nullptr;
}
else if (auto ext = sobj->getExtensionByType<LinkBaseExtension>(true)) {
// check for Link array
if (ext->getElementCountValue() != 0) {
*container = nullptr;
}
}
return res;
}
} // namespace
std::vector<DocumentObject*> DocumentObject::getSubObjectList(const char* subname,
std::vector<int>* const subsizes,
bool flatten) const
{
std::vector<DocumentObject*> res;
res.push_back(const_cast<DocumentObject*>(this));
if (subsizes) {
subsizes->push_back(0);
}
if (!subname || (subname[0] == '\0')) {
return res;
}
auto element = Data::findElementName(subname);
std::string sub(subname, element - subname);
App::DocumentObject* container = nullptr;
bool lastChild = false;
if (flatten) {
auto linked = getLinkedObject();
if (linked->getExtensionByType<App::GeoFeatureGroupExtension>(true)) {
container = const_cast<DocumentObject*>(this);
}
else if (auto grp = App::GeoFeatureGroupExtension::getGroupOfObject(linked)) {
container = grp;
lastChild = true;
}
}
for (auto pos = sub.find('.'); pos != std::string::npos; pos = sub.find('.', pos + 1)) {
char subTail = sub[pos + 1];
sub[pos + 1] = '\0';
auto sobj = getSubObject(sub.c_str());
if (!sobj || !sobj->isAttachedToDocument()) {
continue;
}
if (flatten) {
res = getSubObjectListFlatten(res, subsizes, sobj, &container, lastChild);
}
res.push_back(sobj);
if (subsizes) {
subsizes->push_back((int)pos + 1);
}
sub[pos + 1] = subTail;
}
return res;
}
std::vector<std::string> DocumentObject::getSubObjects(int reason) const
{
std::vector<std::string> ret;
auto exts = getExtensionsDerivedFromType<App::DocumentObjectExtension>();
for (auto ext : exts) {
if (ext->extensionGetSubObjects(ret, reason)) {
return ret;
}
}
return ret;
}
std::vector<std::pair<App::DocumentObject*, std::string>>
DocumentObject::getParents(int depth) const
{
std::vector<std::pair<App::DocumentObject*, std::string>> ret;
if (!isAttachedToDocument() || !GetApplication().checkLinkDepth(depth, MessageOption::Throw)) {
return ret;
}
std::string name(getNameInDocument());
name += ".";
for (auto parent : getInList()) {
if (!parent || !parent->isAttachedToDocument()) {
continue;
}
if (!parent->hasChildElement()
&& !parent->hasExtension(GeoFeatureGroupExtension::getExtensionClassTypeId())) {
continue;
}
if (!parent->getSubObject(name.c_str())) {
continue;
}
auto links = GetApplication().getLinksTo(parent, App::GetLinkRecursive);
links.insert(parent);
for (auto parent : links) {
auto parents = parent->getParents(depth + 1);
if (parents.empty()) {
parents.emplace_back(parent, std::string());
}
for (auto& v : parents) {
ret.emplace_back(v.first, v.second + name);
}
}
}
return ret;
}
App::DocumentObject* DocumentObject::getFirstParent() const
{
for (auto obj : getInList()) {
if (obj->hasExtension(App::GroupExtension::getExtensionClassTypeId(), true)) {
return obj;
}
}
return nullptr;
}
DocumentObject* DocumentObject::getLinkedObject(bool recursive,
Base::Matrix4D* mat,
bool transform,
int depth) const
{
DocumentObject* ret = nullptr;
auto exts = getExtensionsDerivedFromType<App::DocumentObjectExtension>();
for (auto ext : exts) {
if (ext->extensionGetLinkedObject(ret, recursive, mat, transform, depth)) {
return ret;
}
}
if (transform && mat) {
auto pla = freecad_cast<PropertyPlacement*>(getPropertyByName("Placement"));
if (pla) {
*mat *= pla->getValue().toMatrix();
}
}
return const_cast<DocumentObject*>(this);
}
void DocumentObject::Save(Base::Writer& writer) const
{
if (this->isFreezed()) {
throw Base::AbortException("At least one object is frozen, unable to save.");
}
if (this->isAttachedToDocument()){
writer.ObjectName = this->getNameInDocument();
}
App::ExtensionContainer::Save(writer);
}
/**
* @brief Associate the expression \expr with the object identifier \a path in this document object.
* @param path Target object identifier for the result of the expression
* @param expr Expression tree
*/
void DocumentObject::setExpression(const ObjectIdentifier& path, std::shared_ptr<Expression> expr)
{
ExpressionEngine.setValue(path, std::move(expr));
}
/**
* @brief Clear the expression of the object identifier \a path in this document object.
* @param path Target object identifier
*/
void DocumentObject::clearExpression(const ObjectIdentifier& path)
{
setExpression(path, std::shared_ptr<Expression>());
}
/**
* @brief Get expression information associated with \a path.
* @param path Object identifier
* @return Expression info, containing expression and optional comment.
*/
const PropertyExpressionEngine::ExpressionInfo
DocumentObject::getExpression(const ObjectIdentifier& path) const
{
boost::any value = ExpressionEngine.getPathValue(path);
if (value.type() == typeid(PropertyExpressionEngine::ExpressionInfo)) {
return boost::any_cast<PropertyExpressionEngine::ExpressionInfo>(value);
}
else {
return PropertyExpressionEngine::ExpressionInfo();
}
}
/**
* @brief Invoke ExpressionEngine's renameObjectIdentifier, to possibly rewrite expressions using
* the \a paths map with current and new identifiers.
*
* @param paths
*/
void DocumentObject::renameObjectIdentifiers(
const std::map<ObjectIdentifier, ObjectIdentifier>& paths)
{
ExpressionEngine.renameObjectIdentifiers(paths);
}
void DocumentObject::onDocumentRestored()
{
// call all extensions
auto vector = getExtensionsDerivedFromType<App::DocumentObjectExtension>();
for (auto ext : vector) {
ext->onExtendedDocumentRestored();
}
if (Visibility.testStatus(Property::Output)) {
Visibility.setStatus(Property::NoModify, true);
}
}
void DocumentObject::restoreFinished()
{
// some link type property cannot restore link information until other
// objects has been restored. For example, PropertyExpressionEngine and
// PropertySheet with expression containing label reference.
// So on document load they are handled in Document::afterRestore, but if the user
// use dumpContent and restoreContent then they need to be handled here.
std::vector<App::Property*> props;
getPropertyList(props);
for (auto prop : props) {
prop->afterRestore();
}
}
void DocumentObject::onUndoRedoFinished()
{}
void DocumentObject::onSettingDocument()
{
// call all extensions
auto vector = getExtensionsDerivedFromType<App::DocumentObjectExtension>();
for (auto ext : vector) {
ext->onExtendedSettingDocument();
}
}
void DocumentObject::setupObject()
{
// call all extensions
auto vector = getExtensionsDerivedFromType<App::DocumentObjectExtension>();
for (auto ext : vector) {
ext->onExtendedSetupObject();
}
}
void DocumentObject::unsetupObject()
{
// call all extensions
auto vector = getExtensionsDerivedFromType<App::DocumentObjectExtension>();
for (auto ext : vector) {
ext->onExtendedUnsetupObject();
}
}
void App::DocumentObject::_removeBackLink(DocumentObject* rmvObj)
{
// do not use erase-remove idom, as this erases ALL entries that match. we only want to remove a
// single one.
auto it = std::ranges::find(_inList, rmvObj);
if (it != _inList.end()) {
_inList.erase(it);
}
}
void App::DocumentObject::_addBackLink(DocumentObject* newObj)
{
// we need to add all links, even if they are available multiple times. The reason for this is
// the removal: If a link loses this object it removes the backlink. If we would have added it
// only once this removal would clear the object from the inlist, even though there may be other
// link properties from this object that link to us.
_inList.push_back(newObj);
}
int DocumentObject::setElementVisible(const char* element, bool visible)
{
for (auto ext : getExtensionsDerivedFromType<DocumentObjectExtension>()) {
int ret = ext->extensionSetElementVisible(element, visible);
if (ret >= 0) {
return ret;
}
}
return -1;
}
int DocumentObject::isElementVisible(const char* element) const
{
for (auto ext : getExtensionsDerivedFromType<DocumentObjectExtension>()) {
int ret = ext->extensionIsElementVisible(element);
if (ret >= 0) {
return ret;
}
}
return -1;
}
bool DocumentObject::hasChildElement() const
{
for (auto ext : getExtensionsDerivedFromType<DocumentObjectExtension>()) {
if (ext->extensionHasChildElement()) {
return true;
}
}
return false;
}
DocumentObject* DocumentObject::resolve(const char* subname,
App::DocumentObject** parent,
std::string* childName,
const char** subElement,
PyObject** pyObj,
Base::Matrix4D* pmat,
bool transform,
int depth) const
{
auto self = const_cast<DocumentObject*>(this);
if (parent) {
*parent = nullptr;
}
if (subElement) {
*subElement = nullptr;
}
auto obj = getSubObject(subname, pyObj, pmat, transform, depth);
if (!obj || !subname || *subname == 0) {
return self;
}
if (!parent && !subElement) {
return obj;
}
// NOTE, the convention of '.' separated SubName demands a mandatory ending
// '.' for each object name in SubName, even if there is no subelement
// following it. So finding the last dot will give us the end of the last
// object name.
const char* dot = nullptr;
if (Data::isMappedElement(subname) || !(dot = strrchr(subname, '.')) || dot == subname) {
if (subElement) {
*subElement = dot ? dot + 1 : subname;
}
return obj; // this means no parent object reference in SubName
}
if (parent) {
*parent = self;
}
bool elementMapChecked = false;
const char* lastDot = dot;
for (--dot;; --dot) {
// check for the second last dot, which is the end of the last parent object
if (*dot == '.' || dot == subname) {
// We can't get parent object by its name, because the object may be
// externally linked (i.e. in a different document). So go through
// getSubObject again.
if (!elementMapChecked) {
elementMapChecked = true;
const char* sub = dot == subname ? dot : dot + 1;
if (Data::isMappedElement(sub)) {
lastDot = dot;
if (dot == subname) {
break;
}
else {
continue;
}
}
}
if (dot == subname) {
break;
}
auto sobj = getSubObject(std::string(subname, dot - subname + 1).c_str());
if (sobj != obj) {
if (parent) {
// Link/LinkGroup has special visibility handling of plain
// group, so keep ascending
if (!sobj->hasExtension(GroupExtension::getExtensionClassTypeId(), false)) {
*parent = sobj;
break;
}
for (auto ddot = dot - 1; ddot != subname; --ddot) {
if (*ddot != '.') {
continue;
}
auto sobj = getSubObject(std::string(subname, ddot - subname + 1).c_str());
if (!sobj->hasExtension(GroupExtension::getExtensionClassTypeId(), false)) {
*parent = sobj;
break;
}
}
}
break;
}
}
}
if (childName && lastDot != dot) {
if (*dot == '.') {
++dot;
}
const char* nextDot = strchr(dot, '.');
assert(nextDot);
*childName = std::string(dot, nextDot - dot);
}
if (subElement) {
*subElement = *lastDot == '.' ? lastDot + 1 : lastDot;
}
return obj;
}
DocumentObject* DocumentObject::resolveRelativeLink(std::string& subname,
DocumentObject*& link,
std::string& linkSub) const
{
if (!link || !link->isAttachedToDocument() || !isAttachedToDocument()) {
return nullptr;
}
auto ret = const_cast<DocumentObject*>(this);
if (link != ret) {
auto sub = subname.c_str();
auto nextsub = sub;
for (auto dot = strchr(nextsub, '.'); dot; nextsub = dot + 1, dot = strchr(nextsub, '.')) {
std::string subcheck(sub, nextsub - sub);
subcheck += link->getNameInDocument();
subcheck += '.';
if (getSubObject(subcheck.c_str()) == link) {
ret = getSubObject(std::string(sub, dot + 1 - sub).c_str());
if (!ret) {
return nullptr;
}
subname = std::string(dot + 1);
break;
}
}
return ret;
}
size_t pos = 0, linkPos = 0;
std::string linkssub, ssub;
do {
linkPos = linkSub.find('.', linkPos);
if (linkPos == std::string::npos) {
link = nullptr;
return nullptr;
}
++linkPos;
pos = subname.find('.', pos);
if (pos == std::string::npos) {
subname.clear();
ret = nullptr;
break;
}
++pos;
} while (subname.compare(0, pos, linkSub, 0, linkPos) == 0);
if (pos != std::string::npos) {
ret = getSubObject(subname.substr(0, pos).c_str());
if (!ret) {
link = nullptr;
return nullptr;
}
subname = subname.substr(pos);
}
if (linkPos) {
link = link->getSubObject(linkSub.substr(0, linkPos).c_str());
if (!link) {
return nullptr;
}
linkSub = linkSub.substr(linkPos);
}
return ret;
}
bool DocumentObject::adjustRelativeLinks(const std::set<App::DocumentObject*>& inList,
std::set<App::DocumentObject*>* visited)
{
if (visited) {
visited->insert(this);
}
bool touched = false;
std::vector<Property*> props;
getPropertyList(props);
for (auto prop : props) {
auto linkProp = freecad_cast<PropertyLinkBase*>(prop);
if (linkProp && linkProp->adjustLink(inList)) {
touched = true;
}
}
if (visited) {
for (auto obj : getOutList()) {
if (!visited->count(obj)) {
if (obj->adjustRelativeLinks(inList, visited)) {
touched = true;
}
}
}
}
return touched;
}
std::string DocumentObject::getElementMapVersion(const App::Property* _prop, bool restored) const
{
auto prop = freecad_cast<const PropertyComplexGeoData*>(_prop);
if (!prop) {
return std::string();
}
return prop->getElementMapVersion(restored);
}
bool DocumentObject::checkElementMapVersion(const App::Property* _prop, const char* ver) const
{
auto prop = freecad_cast<const PropertyComplexGeoData*>(_prop);
if (!prop) {
return false;
}
return prop->checkElementMapVersion(ver);
}
const std::string& DocumentObject::hiddenMarker()
{
static std::string marker("!hide");
return marker;
}
const char* DocumentObject::hasHiddenMarker(const char* subname)
{
if (!subname) {
return nullptr;
}
const char* marker = strrchr(subname, '.');
if (!marker) {
marker = subname;
}
else {
++marker;
}
return hiddenMarker() == marker ? marker : nullptr;
}
bool DocumentObject::redirectSubName(std::ostringstream&, DocumentObject*, DocumentObject*) const
{
return false;
}
void DocumentObject::onPropertyStatusChanged(const Property& prop, unsigned long oldStatus)
{
(void)oldStatus;
if (!Document::isAnyRestoring() && isAttachedToDocument() && getDocument()) {
getDocument()->signalChangePropertyEditor(*getDocument(), prop);
}
}
Base::Placement DocumentObject::getPlacementOf(const std::string& sub, DocumentObject* targetObj)
{
Base::Placement plc;
auto* propPlacement = freecad_cast<App::PropertyPlacement*>(getPropertyByName("Placement"));
if (propPlacement) {
// If the object has no placement (like a Group), plc stays identity so we can proceed.
plc = propPlacement->getValue();
}
std::vector<std::string> names = Base::Tools::splitSubName(sub);
if (names.empty() || this == targetObj) {
return plc;
}
DocumentObject* subObj = getDocument()->getObject(names.front().c_str());
if (!subObj) {
return plc;
}
std::vector<std::string> newNames(names.begin() + 1, names.end());
std::string newSub = Base::Tools::joinList(newNames, ".");
return plc * subObj->getPlacementOf(newSub, targetObj);
}
Base::Placement DocumentObject::getPlacement() const
{
Base::Placement plc;
if (auto* prop = getPlacementProperty()) {
plc = prop->getValue();
}
return plc;
}
App::PropertyPlacement* DocumentObject::getPlacementProperty() const
{
if (auto linkExtension = getExtensionByType<App::LinkBaseExtension>(true)) {
if (auto linkPlacementProp = linkExtension->getLinkPlacementProperty()) {
return linkPlacementProp;
}
return linkExtension->getPlacementProperty();
}
return getPropertyByName<App::PropertyPlacement>("Placement");
}
|