| 1 |
/*************************************************************************** |
| 2 |
* Copyright (c) 2015 Eivind Kvedalen <[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 <cassert> |
| 24 |
#include <limits> |
| 25 |
|
| 26 |
#include <boost/algorithm/string/predicate.hpp> |
| 27 |
|
| 28 |
#include <App/DocumentObjectPy.h> |
| 29 |
#include <Base/GeometryPyCXX.h> |
| 30 |
#include <Base/Tools.h> |
| 31 |
#include <Base/Interpreter.h> |
| 32 |
#include <Base/QuantityPy.h> |
| 33 |
#include <Base/Console.h> |
| 34 |
#include <Base/Reader.h> |
| 35 |
#include <CXX/Objects.hxx> |
| 36 |
|
| 37 |
#include "ObjectIdentifier.h" |
| 38 |
#include "Application.h" |
| 39 |
#include "Document.h" |
| 40 |
#include "ExpressionParser.h" |
| 41 |
#include "Link.h" |
| 42 |
#include "Property.h" |
| 43 |
|
| 44 |
|
| 45 |
FC_LOG_LEVEL_INIT("Expression", true, true) |
| 46 |
|
| 47 |
using namespace App; |
| 48 |
using namespace Base; |
| 49 |
|
| 50 |
std::string App::quote(const std::string& input, bool toPython) |
| 51 |
{ |
| 52 |
std::stringstream output; |
| 53 |
|
| 54 |
std::string::const_iterator cur = input.begin(); |
| 55 |
std::string::const_iterator end = input.end(); |
| 56 |
|
| 57 |
output << (toPython ? "'" : "<<"); |
| 58 |
while (cur != end) { |
| 59 |
switch (*cur) { |
| 60 |
case '\t': |
| 61 |
output << "\\t"; |
| 62 |
break; |
| 63 |
case '\n': |
| 64 |
output << "\\n"; |
| 65 |
break; |
| 66 |
case '\r': |
| 67 |
output << "\\r"; |
| 68 |
break; |
| 69 |
case '\\': |
| 70 |
output << "\\\\"; |
| 71 |
break; |
| 72 |
case '\'': |
| 73 |
output << "\\'"; |
| 74 |
break; |
| 75 |
case '"': |
| 76 |
output << "\\\""; |
| 77 |
break; |
| 78 |
case '>': |
| 79 |
output << (toPython ? ">" : "\\>"); |
| 80 |
break; |
| 81 |
default: |
| 82 |
output << *cur; |
| 83 |
} |
| 84 |
++cur; |
| 85 |
} |
| 86 |
output << (toPython ? "'" : ">>"); |
| 87 |
|
| 88 |
return output.str(); |
| 89 |
} |
| 90 |
|
| 91 |
|
| 92 |
ObjectIdentifier::ObjectIdentifier(const App::PropertyContainer* _owner, |
| 93 |
const std::string& property, |
| 94 |
int index) |
| 95 |
: owner(nullptr) |
| 96 |
, documentNameSet(false) |
| 97 |
, documentObjectNameSet(false) |
| 98 |
, localProperty(false) |
| 99 |
, _hash(0) |
| 100 |
{ |
| 101 |
if (_owner) { |
| 102 |
const DocumentObject* docObj = freecad_cast<const DocumentObject*>(_owner); |
| 103 |
if (!docObj) { |
| 104 |
FC_THROWM(Base::RuntimeError, "Property must be owned by a document object."); |
| 105 |
} |
| 106 |
owner = const_cast<DocumentObject*>(docObj); |
| 107 |
|
| 108 |
if (!property.empty()) { |
| 109 |
setDocumentObjectName(docObj); |
| 110 |
} |
| 111 |
} |
| 112 |
if (!property.empty()) { |
| 113 |
addComponent(SimpleComponent(property)); |
| 114 |
if (index != std::numeric_limits<int>::max()) { |
| 115 |
addComponent(ArrayComponent(index)); |
| 116 |
} |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
ObjectIdentifier::ObjectIdentifier(const App::PropertyContainer* _owner, bool localProperty) |
| 121 |
: owner(nullptr) |
| 122 |
, documentNameSet(false) |
| 123 |
, documentObjectNameSet(false) |
| 124 |
, localProperty(localProperty) |
| 125 |
, _hash(0) |
| 126 |
{ |
| 127 |
if (_owner) { |
| 128 |
const DocumentObject* docObj = freecad_cast<const DocumentObject*>(_owner); |
| 129 |
if (!docObj) { |
| 130 |
FC_THROWM(Base::RuntimeError, "Property must be owned by a document object."); |
| 131 |
} |
| 132 |
owner = const_cast<DocumentObject*>(docObj); |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
ObjectIdentifier::ObjectIdentifier(const Property& prop, int index) |
| 137 |
: owner(nullptr) |
| 138 |
, documentNameSet(false) |
| 139 |
, documentObjectNameSet(false) |
| 140 |
, localProperty(false) |
| 141 |
, _hash(0) |
| 142 |
{ |
| 143 |
DocumentObject* docObj = freecad_cast<DocumentObject*>(prop.getContainer()); |
| 144 |
|
| 145 |
if (!docObj) { |
| 146 |
FC_THROWM(Base::TypeError, "Property must be owned by a document object."); |
| 147 |
} |
| 148 |
if (!prop.hasName()) { |
| 149 |
FC_THROWM(Base::RuntimeError, "Property must have a name."); |
| 150 |
} |
| 151 |
|
| 152 |
owner = const_cast<DocumentObject*>(docObj); |
| 153 |
|
| 154 |
setDocumentObjectName(docObj); |
| 155 |
|
| 156 |
addComponent(SimpleComponent(String(prop.getName()))); |
| 157 |
if (index != std::numeric_limits<int>::max()) { |
| 158 |
addComponent(ArrayComponent(index)); |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
std::string App::ObjectIdentifier::getPropertyName() const |
| 163 |
{ |
| 164 |
ResolveResults result(*this); |
| 165 |
|
| 166 |
assert(result.propertyIndex >= 0 |
| 167 |
&& static_cast<std::size_t>(result.propertyIndex) < components.size()); |
| 168 |
|
| 169 |
return components[result.propertyIndex].getName(); |
| 170 |
} |
| 171 |
|
| 172 |
const App::ObjectIdentifier::Component& App::ObjectIdentifier::getPropertyComponent(int i, |
| 173 |
int* idx) const |
| 174 |
{ |
| 175 |
ResolveResults result(*this); |
| 176 |
|
| 177 |
i += result.propertyIndex; |
| 178 |
if (i < 0 || i >= static_cast<int>(components.size())) { |
| 179 |
FC_THROWM(Base::ValueError, "Invalid property component index"); |
| 180 |
} |
| 181 |
|
| 182 |
if (idx) { |
| 183 |
*idx = i; |
| 184 |
} |
| 185 |
|
| 186 |
return components[i]; |
| 187 |
} |
| 188 |
|
| 189 |
void App::ObjectIdentifier::setComponent(int idx, Component&& comp) |
| 190 |
{ |
| 191 |
if (idx < 0 || idx >= static_cast<int>(components.size())) { |
| 192 |
FC_THROWM(Base::ValueError, "Invalid component index"); |
| 193 |
} |
| 194 |
components[idx] = std::move(comp); |
| 195 |
_cache.clear(); |
| 196 |
} |
| 197 |
|
| 198 |
void App::ObjectIdentifier::setComponent(int idx, const Component& comp) |
| 199 |
{ |
| 200 |
setComponent(idx, Component(comp)); |
| 201 |
} |
| 202 |
|
| 203 |
std::vector<ObjectIdentifier::Component> ObjectIdentifier::getPropertyComponents() const |
| 204 |
{ |
| 205 |
if (components.size() <= 1 || documentObjectName.getString().empty()) { |
| 206 |
return components; |
| 207 |
} |
| 208 |
ResolveResults result(*this); |
| 209 |
if (result.propertyIndex == 0) { |
| 210 |
return components; |
| 211 |
} |
| 212 |
std::vector<ObjectIdentifier::Component> res; |
| 213 |
res.insert(res.end(), components.begin() + result.propertyIndex, components.end()); |
| 214 |
return res; |
| 215 |
} |
| 216 |
|
| 217 |
bool ObjectIdentifier::operator==(const ObjectIdentifier& other) const |
| 218 |
{ |
| 219 |
return owner == other.owner && toString() == other.toString(); |
| 220 |
} |
| 221 |
|
| 222 |
bool ObjectIdentifier::operator!=(const ObjectIdentifier& other) const |
| 223 |
{ |
| 224 |
return !(operator==)(other); |
| 225 |
} |
| 226 |
|
| 227 |
bool ObjectIdentifier::operator<(const ObjectIdentifier& other) const |
| 228 |
{ |
| 229 |
if (owner < other.owner) { |
| 230 |
return true; |
| 231 |
} |
| 232 |
if (owner > other.owner) { |
| 233 |
return false; |
| 234 |
} |
| 235 |
return toString() < other.toString(); |
| 236 |
} |
| 237 |
|
| 238 |
int ObjectIdentifier::numComponents() const |
| 239 |
{ |
| 240 |
return components.size(); |
| 241 |
} |
| 242 |
|
| 243 |
int ObjectIdentifier::numSubComponents() const |
| 244 |
{ |
| 245 |
ResolveResults result(*this); |
| 246 |
|
| 247 |
return components.size() - result.propertyIndex; |
| 248 |
} |
| 249 |
|
| 250 |
bool ObjectIdentifier::verify(const App::Property& prop, bool silent) const |
| 251 |
{ |
| 252 |
ResolveResults result(*this); |
| 253 |
if (components.size() - result.propertyIndex != 1) { |
| 254 |
if (silent) { |
| 255 |
return false; |
| 256 |
} |
| 257 |
FC_THROWM(Base::ValueError, "Invalid property path: single component expected"); |
| 258 |
} |
| 259 |
if (!components[result.propertyIndex].isSimple()) { |
| 260 |
if (silent) { |
| 261 |
return false; |
| 262 |
} |
| 263 |
FC_THROWM(Base::ValueError, "Invalid property path: simple component expected"); |
| 264 |
} |
| 265 |
const std::string& name = components[result.propertyIndex].getName(); |
| 266 |
CellAddress addr; |
| 267 |
bool isAddress = addr.parseAbsoluteAddress(name.c_str()); |
| 268 |
if ((isAddress && addr.toString(CellAddress::Cell::ShowRowColumn) != prop.getName()) |
| 269 |
|| (!isAddress && name != prop.getName())) { |
| 270 |
if (silent) { |
| 271 |
return false; |
| 272 |
} |
| 273 |
FC_THROWM(Base::ValueError, "Invalid property path: name mismatch"); |
| 274 |
} |
| 275 |
return true; |
| 276 |
} |
| 277 |
|
| 278 |
const std::string& ObjectIdentifier::toString() const |
| 279 |
{ |
| 280 |
if (!_cache.empty() || !owner) { |
| 281 |
return _cache; |
| 282 |
} |
| 283 |
|
| 284 |
std::ostringstream s; |
| 285 |
ResolveResults result(*this); |
| 286 |
|
| 287 |
if (result.propertyIndex >= (int)components.size()) { |
| 288 |
return _cache; |
| 289 |
} |
| 290 |
|
| 291 |
if (localProperty |
| 292 |
|| (result.resolvedProperty && result.resolvedDocumentObject == owner |
| 293 |
&& components.size() > 1 && components[1].isSimple() && result.propertyIndex == 0)) { |
| 294 |
s << '.'; |
| 295 |
} |
| 296 |
else if (documentNameSet && !documentName.getString().empty()) { |
| 297 |
if (documentObjectNameSet && !documentObjectName.getString().empty()) { |
| 298 |
s << documentName.toString() << "#" << documentObjectName.toString() << '.'; |
| 299 |
} |
| 300 |
else if (!result.resolvedDocumentObjectName.getString().empty()) { |
| 301 |
s << documentName.toString() << "#" << result.resolvedDocumentObjectName.toString() |
| 302 |
<< '.'; |
| 303 |
} |
| 304 |
} |
| 305 |
else if (documentObjectNameSet && !documentObjectName.getString().empty()) { |
| 306 |
s << documentObjectName.toString() << '.'; |
| 307 |
} |
| 308 |
else if (result.propertyIndex > 0) { |
| 309 |
components[0].toString(s); |
| 310 |
s << '.'; |
| 311 |
} |
| 312 |
|
| 313 |
if (!subObjectName.getString().empty()) { |
| 314 |
s << subObjectName.toString() << '.'; |
| 315 |
} |
| 316 |
|
| 317 |
s << components[result.propertyIndex].getName(); |
| 318 |
getSubPathStr(s, result); |
| 319 |
const_cast<ObjectIdentifier*>(this)->_cache = s.str(); |
| 320 |
return _cache; |
| 321 |
} |
| 322 |
|
| 323 |
std::string ObjectIdentifier::toPersistentString() const |
| 324 |
{ |
| 325 |
|
| 326 |
if (!owner) { |
| 327 |
return {}; |
| 328 |
} |
| 329 |
|
| 330 |
std::ostringstream s; |
| 331 |
ResolveResults result(*this); |
| 332 |
|
| 333 |
if (result.propertyIndex >= (int)components.size()) { |
| 334 |
return {}; |
| 335 |
} |
| 336 |
|
| 337 |
if (localProperty |
| 338 |
|| (result.resolvedProperty && result.resolvedDocumentObject == owner |
| 339 |
&& components.size() > 1 && components[1].isSimple() && result.propertyIndex == 0)) { |
| 340 |
s << '.'; |
| 341 |
} |
| 342 |
else if (result.resolvedDocumentObject && result.resolvedDocumentObject != owner |
| 343 |
&& result.resolvedDocumentObject->isExporting()) { |
| 344 |
s << result.resolvedDocumentObject->getExportName(true); |
| 345 |
if (documentObjectName.isRealString()) { |
| 346 |
s << '@'; |
| 347 |
} |
| 348 |
s << '.'; |
| 349 |
} |
| 350 |
else if (documentNameSet && !documentName.getString().empty()) { |
| 351 |
if (documentObjectNameSet && !documentObjectName.getString().empty()) { |
| 352 |
s << documentName.toString() << "#" << documentObjectName.toString() << '.'; |
| 353 |
} |
| 354 |
else if (!result.resolvedDocumentObjectName.getString().empty()) { |
| 355 |
s << documentName.toString() << "#" << result.resolvedDocumentObjectName.toString() |
| 356 |
<< '.'; |
| 357 |
} |
| 358 |
} |
| 359 |
else if (documentObjectNameSet && !documentObjectName.getString().empty()) { |
| 360 |
s << documentObjectName.toString() << '.'; |
| 361 |
} |
| 362 |
else if (result.propertyIndex > 0) { |
| 363 |
components[0].toString(s); |
| 364 |
s << '.'; |
| 365 |
} |
| 366 |
|
| 367 |
if (!subObjectName.getString().empty()) { |
| 368 |
const char* subname = subObjectName.getString().c_str(); |
| 369 |
std::string exportName; |
| 370 |
s << String(PropertyLinkBase::exportSubName(exportName, |
| 371 |
result.resolvedDocumentObject, |
| 372 |
subname), |
| 373 |
true) |
| 374 |
.toString() |
| 375 |
<< '.'; |
| 376 |
} |
| 377 |
|
| 378 |
s << components[result.propertyIndex].getName(); |
| 379 |
getSubPathStr(s, result); |
| 380 |
return s.str(); |
| 381 |
} |
| 382 |
|
| 383 |
std::size_t ObjectIdentifier::hash() const |
| 384 |
{ |
| 385 |
if (_hash && !_cache.empty()) { |
| 386 |
return _hash; |
| 387 |
} |
| 388 |
const_cast<ObjectIdentifier*>(this)->_hash = boost::hash_value(toString()); |
| 389 |
return _hash; |
| 390 |
} |
| 391 |
|
| 392 |
bool ObjectIdentifier::replaceObject(ObjectIdentifier& res, |
| 393 |
const App::DocumentObject* parent, |
| 394 |
App::DocumentObject* oldObj, |
| 395 |
App::DocumentObject* newObj) const |
| 396 |
{ |
| 397 |
ResolveResults result(*this); |
| 398 |
|
| 399 |
if (!result.resolvedDocumentObject) { |
| 400 |
return false; |
| 401 |
} |
| 402 |
|
| 403 |
auto r = PropertyLinkBase::tryReplaceLink(owner, |
| 404 |
result.resolvedDocumentObject, |
| 405 |
parent, |
| 406 |
oldObj, |
| 407 |
newObj, |
| 408 |
subObjectName.getString().c_str()); |
| 409 |
|
| 410 |
if (!r.first) { |
| 411 |
return false; |
| 412 |
} |
| 413 |
|
| 414 |
res = *this; |
| 415 |
if (r.first != result.resolvedDocumentObject) { |
| 416 |
if (r.first->getDocument() != owner->getDocument()) { |
| 417 |
auto doc = r.first->getDocument(); |
| 418 |
bool useLabel = res.documentName.isRealString(); |
| 419 |
const char* name = useLabel ? doc->Label.getValue() : doc->getName(); |
| 420 |
res.setDocumentName(String(name, useLabel), true); |
| 421 |
} |
| 422 |
if (documentObjectName.isRealString()) { |
| 423 |
res.documentObjectName = String(r.first->Label.getValue(), true); |
| 424 |
} |
| 425 |
else { |
| 426 |
res.documentObjectName = String(r.first->getNameInDocument(), false, true); |
| 427 |
} |
| 428 |
} |
| 429 |
res.subObjectName = String(r.second, true); |
| 430 |
res._cache.clear(); |
| 431 |
res.shadowSub.newName.clear(); |
| 432 |
res.shadowSub.oldName.clear(); |
| 433 |
return true; |
| 434 |
} |
| 435 |
|
| 436 |
std::string ObjectIdentifier::toEscapedString() const |
| 437 |
{ |
| 438 |
return Base::Tools::escapedUnicodeFromUtf8(toString().c_str()); |
| 439 |
} |
| 440 |
|
| 441 |
bool ObjectIdentifier::updateLabelReference(const App::DocumentObject* obj, |
| 442 |
const std::string& ref, |
| 443 |
const char* newLabel) |
| 444 |
{ |
| 445 |
if (!owner) { |
| 446 |
return false; |
| 447 |
} |
| 448 |
|
| 449 |
ResolveResults result(*this); |
| 450 |
|
| 451 |
if (!subObjectName.getString().empty() && result.resolvedDocumentObject) { |
| 452 |
std::string sub = PropertyLinkBase::updateLabelReference(result.resolvedDocumentObject, |
| 453 |
subObjectName.getString().c_str(), |
| 454 |
obj, |
| 455 |
ref, |
| 456 |
newLabel); |
| 457 |
if (!sub.empty()) { |
| 458 |
subObjectName = String(sub, true); |
| 459 |
_cache.clear(); |
| 460 |
return true; |
| 461 |
} |
| 462 |
} |
| 463 |
|
| 464 |
if (result.resolvedDocument != obj->getDocument()) { |
| 465 |
return false; |
| 466 |
} |
| 467 |
|
| 468 |
if (!documentObjectName.getString().empty()) { |
| 469 |
if (documentObjectName.isForceIdentifier()) { |
| 470 |
return false; |
| 471 |
} |
| 472 |
|
| 473 |
if (!documentObjectName.isRealString() |
| 474 |
&& documentObjectName.getString() == obj->getNameInDocument()) { |
| 475 |
return false; |
| 476 |
} |
| 477 |
|
| 478 |
if (documentObjectName.getString() != obj->Label.getValue()) { |
| 479 |
return false; |
| 480 |
} |
| 481 |
|
| 482 |
documentObjectName = ObjectIdentifier::String(newLabel, true); |
| 483 |
|
| 484 |
_cache.clear(); |
| 485 |
return true; |
| 486 |
} |
| 487 |
|
| 488 |
if (result.resolvedDocumentObject == obj && result.propertyIndex == 1 |
| 489 |
&& result.resolvedDocumentObjectName.isRealString() |
| 490 |
&& result.resolvedDocumentObjectName.getString() == obj->Label.getValue()) { |
| 491 |
components[0].name = ObjectIdentifier::String(newLabel, true); |
| 492 |
_cache.clear(); |
| 493 |
return true; |
| 494 |
} |
| 495 |
|
| 496 |
// If object identifier uses the label then resolving the document object will fail. |
| 497 |
// So, it must be checked if using the new label will succeed |
| 498 |
if (components.size() > 1 && components[0].getName() == obj->Label.getValue()) { |
| 499 |
ObjectIdentifier id(*this); |
| 500 |
id.components[0].name.str = newLabel; |
| 501 |
|
| 502 |
ResolveResults result(id); |
| 503 |
|
| 504 |
if (result.propertyIndex == 1 && result.resolvedDocumentObject == obj) { |
| 505 |
components[0].name = id.components[0].name; |
| 506 |
_cache.clear(); |
| 507 |
return true; |
| 508 |
} |
| 509 |
} |
| 510 |
|
| 511 |
return false; |
| 512 |
} |
| 513 |
|
| 514 |
bool ObjectIdentifier::relabeledDocument(ExpressionVisitor& v, |
| 515 |
const std::string& oldLabel, |
| 516 |
const std::string& newLabel) |
| 517 |
{ |
| 518 |
if (documentNameSet && documentName.isRealString() && documentName.getString() == oldLabel) { |
| 519 |
v.aboutToChange(); |
| 520 |
documentName = String(newLabel, true); |
| 521 |
_cache.clear(); |
| 522 |
return true; |
| 523 |
} |
| 524 |
return false; |
| 525 |
} |
| 526 |
|
| 527 |
void ObjectIdentifier::getSubPathStr(std::ostream& s, |
| 528 |
const ResolveResults& result, |
| 529 |
bool toPython) const |
| 530 |
{ |
| 531 |
std::vector<Component>::const_iterator i = components.begin() + result.propertyIndex + 1; |
| 532 |
while (i != components.end()) { |
| 533 |
if (i->isSimple()) { |
| 534 |
s << '.'; |
| 535 |
} |
| 536 |
i->toString(s, toPython); |
| 537 |
++i; |
| 538 |
} |
| 539 |
} |
| 540 |
|
| 541 |
std::string ObjectIdentifier::getSubPathStr(bool toPython) const |
| 542 |
{ |
| 543 |
std::ostringstream ss; |
| 544 |
getSubPathStr(ss, ResolveResults(*this), toPython); |
| 545 |
return ss.str(); |
| 546 |
} |
| 547 |
|
| 548 |
|
| 549 |
ObjectIdentifier::Component::Component(const String& _name, |
| 550 |
ObjectIdentifier::Component::typeEnum _type, |
| 551 |
int _begin, |
| 552 |
int _end, |
| 553 |
int _step) |
| 554 |
: name(_name) |
| 555 |
, type(_type) |
| 556 |
, begin(_begin) |
| 557 |
, end(_end) |
| 558 |
, step(_step) |
| 559 |
{} |
| 560 |
|
| 561 |
ObjectIdentifier::Component::Component(String&& _name, |
| 562 |
ObjectIdentifier::Component::typeEnum _type, |
| 563 |
int _begin, |
| 564 |
int _end, |
| 565 |
int _step) |
| 566 |
: name(std::move(_name)) |
| 567 |
, type(_type) |
| 568 |
, begin(_begin) |
| 569 |
, end(_end) |
| 570 |
, step(_step) |
| 571 |
{} |
| 572 |
|
| 573 |
size_t ObjectIdentifier::Component::getIndex(size_t count) const |
| 574 |
{ |
| 575 |
if (begin >= 0) { |
| 576 |
if (begin < (int)count) { |
| 577 |
return begin; |
| 578 |
} |
| 579 |
} |
| 580 |
else { |
| 581 |
int idx = begin + (int)count; |
| 582 |
if (idx >= 0) { |
| 583 |
return idx; |
| 584 |
} |
| 585 |
} |
| 586 |
FC_THROWM(Base::IndexError, "Array out of bound: " << begin << ", " << count); |
| 587 |
} |
| 588 |
|
| 589 |
Py::Object ObjectIdentifier::Component::get(const Py::Object& pyobj) const |
| 590 |
{ |
| 591 |
Py::Object res; |
| 592 |
if (isSimple()) { |
| 593 |
if (!pyobj.hasAttr(getName())) { |
| 594 |
FC_THROWM(Base::AttributeError, "No attribute named '" << getName() << "'"); |
| 595 |
} |
| 596 |
res = pyobj.getAttr(getName()); |
| 597 |
} |
| 598 |
else if (isArray()) { |
| 599 |
if (pyobj.isMapping()) { |
| 600 |
res = Py::Mapping(pyobj).getItem(Py::Long(begin)); |
| 601 |
} |
| 602 |
else { |
| 603 |
res = Py::Sequence(pyobj).getItem(begin); |
| 604 |
} |
| 605 |
} |
| 606 |
else if (isMap()) { |
| 607 |
res = Py::Mapping(pyobj).getItem(getName()); |
| 608 |
} |
| 609 |
else { |
| 610 |
assert(isRange()); |
| 611 |
constexpr int max = std::numeric_limits<int>::max(); |
| 612 |
Py::Object slice(PySlice_New(Py::Long(begin).ptr(), |
| 613 |
end != max ? Py::Long(end).ptr() : nullptr, |
| 614 |
step != 1 ? Py::Long(step).ptr() : nullptr), |
| 615 |
true); |
| 616 |
PyObject* r = PyObject_GetItem(pyobj.ptr(), slice.ptr()); |
| 617 |
if (!r) { |
| 618 |
Base::PyException::throwException(); |
| 619 |
} |
| 620 |
res = Py::asObject(r); |
| 621 |
} |
| 622 |
if (!res.ptr()) { |
| 623 |
Base::PyException::throwException(); |
| 624 |
} |
| 625 |
if (PyModule_Check(res.ptr()) && !ExpressionParser::isModuleImported(res.ptr())) { |
| 626 |
FC_THROWM(Base::RuntimeError, "Module '" << getName() << "' access denied."); |
| 627 |
} |
| 628 |
return res; |
| 629 |
} |
| 630 |
|
| 631 |
void ObjectIdentifier::Component::set(Py::Object& pyobj, const Py::Object& value) const |
| 632 |
{ |
| 633 |
if (isSimple()) { |
| 634 |
if (PyObject_SetAttrString(*pyobj, getName().c_str(), *value) == -1) { |
| 635 |
Base::PyException::throwException(); |
| 636 |
} |
| 637 |
} |
| 638 |
else if (isArray()) { |
| 639 |
if (pyobj.isMapping()) { |
| 640 |
Py::Mapping(pyobj).setItem(Py::Long(begin), value); |
| 641 |
} |
| 642 |
else { |
| 643 |
Py::Sequence(pyobj).setItem(begin, value); |
| 644 |
} |
| 645 |
} |
| 646 |
else if (isMap()) { |
| 647 |
Py::Mapping(pyobj).setItem(getName(), value); |
| 648 |
} |
| 649 |
else { |
| 650 |
assert(isRange()); |
| 651 |
constexpr int max = std::numeric_limits<int>::max(); |
| 652 |
Py::Object slice(PySlice_New(Py::Long(begin).ptr(), |
| 653 |
end != max ? Py::Long(end).ptr() : nullptr, |
| 654 |
step != 1 ? Py::Long(step).ptr() : nullptr), |
| 655 |
true); |
| 656 |
if (PyObject_SetItem(pyobj.ptr(), slice.ptr(), value.ptr()) < 0) { |
| 657 |
Base::PyException::throwException(); |
| 658 |
} |
| 659 |
} |
| 660 |
} |
| 661 |
|
| 662 |
void ObjectIdentifier::Component::del(Py::Object& pyobj) const |
| 663 |
{ |
| 664 |
if (isSimple()) { |
| 665 |
pyobj.delAttr(getName()); |
| 666 |
} |
| 667 |
else if (isArray()) { |
| 668 |
if (pyobj.isMapping()) { |
| 669 |
Py::Mapping(pyobj).delItem(Py::Long(begin)); |
| 670 |
} |
| 671 |
else { |
| 672 |
PySequence_DelItem(pyobj.ptr(), begin); |
| 673 |
} |
| 674 |
} |
| 675 |
else if (isMap()) { |
| 676 |
Py::Mapping(pyobj).delItem(getName()); |
| 677 |
} |
| 678 |
else { |
| 679 |
assert(isRange()); |
| 680 |
constexpr int max = std::numeric_limits<int>::max(); |
| 681 |
Py::Object slice(PySlice_New(Py::Long(begin).ptr(), |
| 682 |
end != max ? Py::Long(end).ptr() : nullptr, |
| 683 |
step != 1 ? Py::Long(step).ptr() : nullptr), |
| 684 |
true); |
| 685 |
if (PyObject_DelItem(pyobj.ptr(), slice.ptr()) < 0) { |
| 686 |
Base::PyException::throwException(); |
| 687 |
} |
| 688 |
} |
| 689 |
} |
| 690 |
|
| 691 |
ObjectIdentifier::Component ObjectIdentifier::Component::SimpleComponent(const char* _component) |
| 692 |
{ |
| 693 |
return Component(String(_component)); |
| 694 |
} |
| 695 |
|
| 696 |
ObjectIdentifier::Component |
| 697 |
ObjectIdentifier::Component::SimpleComponent(const ObjectIdentifier::String& _component) |
| 698 |
{ |
| 699 |
return Component(_component); |
| 700 |
} |
| 701 |
|
| 702 |
ObjectIdentifier::Component |
| 703 |
ObjectIdentifier::Component::SimpleComponent(ObjectIdentifier::String&& _component) |
| 704 |
{ |
| 705 |
return Component(std::move(_component)); |
| 706 |
} |
| 707 |
|
| 708 |
ObjectIdentifier::Component ObjectIdentifier::Component::ArrayComponent(int _index) |
| 709 |
{ |
| 710 |
return Component(String(), Component::ARRAY, _index); |
| 711 |
} |
| 712 |
|
| 713 |
ObjectIdentifier::Component ObjectIdentifier::Component::MapComponent(const String& _key) |
| 714 |
{ |
| 715 |
return Component(_key, Component::MAP); |
| 716 |
} |
| 717 |
|
| 718 |
ObjectIdentifier::Component ObjectIdentifier::Component::MapComponent(String&& _key) |
| 719 |
{ |
| 720 |
return Component(std::move(_key), Component::MAP); |
| 721 |
} |
| 722 |
|
| 723 |
ObjectIdentifier::Component |
| 724 |
ObjectIdentifier::Component::RangeComponent(int _begin, int _end, int _step) |
| 725 |
{ |
| 726 |
return Component(String(), Component::RANGE, _begin, _end, _step); |
| 727 |
} |
| 728 |
|
| 729 |
bool ObjectIdentifier::Component::operator==(const ObjectIdentifier::Component& other) const |
| 730 |
{ |
| 731 |
if (type != other.type) { |
| 732 |
return false; |
| 733 |
} |
| 734 |
|
| 735 |
switch (type) { |
| 736 |
case SIMPLE: |
| 737 |
case MAP: |
| 738 |
return name == other.name; |
| 739 |
case ARRAY: |
| 740 |
return begin == other.begin; |
| 741 |
case RANGE: |
| 742 |
return begin == other.begin && end == other.end && step == other.step; |
| 743 |
default: |
| 744 |
assert(0); |
| 745 |
return false; |
| 746 |
} |
| 747 |
} |
| 748 |
|
| 749 |
void ObjectIdentifier::Component::toString(std::ostream& ss, bool toPython) const |
| 750 |
{ |
| 751 |
switch (type) { |
| 752 |
case Component::SIMPLE: |
| 753 |
ss << name.getString(); |
| 754 |
break; |
| 755 |
case Component::MAP: |
| 756 |
ss << "[" << name.toString(toPython) << "]"; |
| 757 |
break; |
| 758 |
case Component::ARRAY: |
| 759 |
ss << "[" << begin << "]"; |
| 760 |
break; |
| 761 |
case Component::RANGE: |
| 762 |
ss << '['; |
| 763 |
if (begin != std::numeric_limits<int>::max()) { |
| 764 |
ss << begin; |
| 765 |
} |
| 766 |
ss << ':'; |
| 767 |
if (end != std::numeric_limits<int>::max()) { |
| 768 |
ss << end; |
| 769 |
} |
| 770 |
if (step != 1) { |
| 771 |
ss << ':' << step; |
| 772 |
} |
| 773 |
ss << ']'; |
| 774 |
break; |
| 775 |
default: |
| 776 |
assert(0); |
| 777 |
} |
| 778 |
} |
| 779 |
|
| 780 |
enum ResolveFlags |
| 781 |
{ |
| 782 |
ResolveByIdentifier, |
| 783 |
ResolveByLabel, |
| 784 |
ResolveAmbiguous, |
| 785 |
}; |
| 786 |
|
| 787 |
App::DocumentObject* ObjectIdentifier::getDocumentObject(const App::Document* doc, |
| 788 |
const String& name, |
| 789 |
std::bitset<32>& flags) |
| 790 |
{ |
| 791 |
DocumentObject* objectById = nullptr; |
| 792 |
DocumentObject* objectByLabel = nullptr; |
| 793 |
|
| 794 |
if (!name.isRealString()) { |
| 795 |
// No object found with matching label, try using name directly |
| 796 |
objectById = doc->getObject(static_cast<const char*>(name)); |
| 797 |
|
| 798 |
if (objectById) { |
| 799 |
flags.set(ResolveByIdentifier); |
| 800 |
return objectById; |
| 801 |
} |
| 802 |
if (name.isForceIdentifier()) { |
| 803 |
return nullptr; |
| 804 |
} |
| 805 |
} |
| 806 |
|
| 807 |
std::vector<DocumentObject*> docObjects = doc->getObjects(); |
| 808 |
for (auto docObject : docObjects) { |
| 809 |
if (strcmp(docObject->Label.getValue(), static_cast<const char*>(name)) == 0) { |
| 810 |
// Found object with matching label |
| 811 |
if (objectByLabel) { |
| 812 |
FC_WARN("duplicate object label " << doc->getName() << '#' |
| 813 |
<< static_cast<const char*>(name)); |
| 814 |
return nullptr; |
| 815 |
} |
| 816 |
objectByLabel = docObject; |
| 817 |
} |
| 818 |
} |
| 819 |
|
| 820 |
if (!objectByLabel && !objectById) { // Not found at all |
| 821 |
return nullptr; |
| 822 |
} |
| 823 |
else if (!objectByLabel) { // Found by name |
| 824 |
flags.set(ResolveByIdentifier); |
| 825 |
return objectById; |
| 826 |
} |
| 827 |
else if (!objectById) { // Found by label |
| 828 |
flags.set(ResolveByLabel); |
| 829 |
return objectByLabel; |
| 830 |
} |
| 831 |
else if (objectByLabel == objectById) { // Found by both name and label, same object |
| 832 |
flags.set(ResolveByIdentifier); |
| 833 |
flags.set(ResolveByLabel); |
| 834 |
return objectByLabel; |
| 835 |
} |
| 836 |
else { |
| 837 |
flags.set(ResolveAmbiguous); |
| 838 |
return nullptr; // Found by both name and label, two different objects |
| 839 |
} |
| 840 |
} |
| 841 |
|
| 842 |
void ObjectIdentifier::resolve(ResolveResults& results) const |
| 843 |
{ |
| 844 |
if (!owner) { |
| 845 |
return; |
| 846 |
} |
| 847 |
|
| 848 |
bool docAmbiguous = false; |
| 849 |
|
| 850 |
/* Document name specified? */ |
| 851 |
if (!documentName.getString().empty()) { |
| 852 |
results.resolvedDocument = getDocument(documentName, &docAmbiguous); |
| 853 |
results.resolvedDocumentName = documentName; |
| 854 |
} |
| 855 |
else { |
| 856 |
results.resolvedDocument = owner->getDocument(); |
| 857 |
results.resolvedDocumentName = String(results.resolvedDocument->getName(), false, true); |
| 858 |
} |
| 859 |
|
| 860 |
results.subObjectName = subObjectName; |
| 861 |
results.propertyName = ""; |
| 862 |
results.propertyIndex = 0; |
| 863 |
|
| 864 |
// Assume document name and object name from owner if not found |
| 865 |
if (!results.resolvedDocument) { |
| 866 |
if (!documentName.getString().empty()) { |
| 867 |
if (docAmbiguous) { |
| 868 |
results.flags.set(ResolveAmbiguous); |
| 869 |
} |
| 870 |
return; |
| 871 |
} |
| 872 |
|
| 873 |
results.resolvedDocument = owner->getDocument(); |
| 874 |
if (!results.resolvedDocument) { |
| 875 |
return; |
| 876 |
} |
| 877 |
} |
| 878 |
|
| 879 |
results.resolvedDocumentName = String(results.resolvedDocument->getName(), false, true); |
| 880 |
|
| 881 |
/* Document object name specified? */ |
| 882 |
if (!documentObjectName.getString().empty()) { |
| 883 |
results.resolvedDocumentObjectName = documentObjectName; |
| 884 |
results.resolvedDocumentObject = |
| 885 |
getDocumentObject(results.resolvedDocument, documentObjectName, results.flags); |
| 886 |
if (!results.resolvedDocumentObject) { |
| 887 |
return; |
| 888 |
} |
| 889 |
|
| 890 |
if (components.empty()) { |
| 891 |
return; |
| 892 |
} |
| 893 |
|
| 894 |
results.propertyName = components[0].name.getString(); |
| 895 |
results.propertyIndex = 0; |
| 896 |
results.getProperty(*this); |
| 897 |
} |
| 898 |
else { |
| 899 |
/* Document object name not specified, resolve from path */ |
| 900 |
|
| 901 |
/* One component? */ |
| 902 |
if (components.size() == 1 || (components.size() > 1 && !components[0].isSimple())) { |
| 903 |
/* Yes -- then this must be a property, so we get the document object's name from the |
| 904 |
* owner */ |
| 905 |
results.resolvedDocumentObjectName = String(owner->getNameInDocument(), false, true); |
| 906 |
results.resolvedDocumentObject = owner; |
| 907 |
results.propertyName = components[0].name.getString(); |
| 908 |
results.propertyIndex = 0; |
| 909 |
results.getProperty(*this); |
| 910 |
} |
| 911 |
else if (components.size() >= 2) { |
| 912 |
/* No -- */ |
| 913 |
if (!components[0].isSimple()) { |
| 914 |
return; |
| 915 |
} |
| 916 |
|
| 917 |
results.resolvedDocumentObject = |
| 918 |
getDocumentObject(results.resolvedDocument, components[0].name, results.flags); |
| 919 |
|
| 920 |
/* Possible to resolve component to a document object? */ |
| 921 |
if (results.resolvedDocumentObject) { |
| 922 |
/* Yes */ |
| 923 |
results.resolvedDocumentObjectName = |
| 924 |
String {components[0].name.getString(), |
| 925 |
false, |
| 926 |
results.flags.test(ResolveByIdentifier)}; |
| 927 |
results.propertyName = components[1].name.getString(); |
| 928 |
results.propertyIndex = 1; |
| 929 |
results.getProperty(*this); |
| 930 |
if (!results.resolvedProperty) { |
| 931 |
// If the second component is not a property name, try to |
| 932 |
// interpret the first component as the property name. |
| 933 |
DocumentObject* sobj = nullptr; |
| 934 |
results.resolvedProperty = |
| 935 |
resolveProperty(owner, |
| 936 |
components[0].name.toString().c_str(), |
| 937 |
sobj, |
| 938 |
results.propertyType); |
| 939 |
if (results.resolvedProperty) { |
| 940 |
results.propertyName = components[0].name.getString(); |
| 941 |
results.resolvedDocument = owner->getDocument(); |
| 942 |
results.resolvedDocumentName = |
| 943 |
String(results.resolvedDocument->getName(), false, true); |
| 944 |
results.resolvedDocumentObjectName = |
| 945 |
String(owner->getNameInDocument(), false, true); |
| 946 |
results.resolvedDocumentObject = owner; |
| 947 |
results.resolvedSubObject = sobj; |
| 948 |
results.propertyIndex = 0; |
| 949 |
} |
| 950 |
} |
| 951 |
} |
| 952 |
else if (documentName.getString().empty()) { |
| 953 |
/* No, assume component is a property, and get document object's name from owner */ |
| 954 |
results.resolvedDocument = owner->getDocument(); |
| 955 |
results.resolvedDocumentName = |
| 956 |
String(results.resolvedDocument->getName(), false, true); |
| 957 |
results.resolvedDocumentObjectName = |
| 958 |
String(owner->getNameInDocument(), false, true); |
| 959 |
results.resolvedDocumentObject = |
| 960 |
owner->getDocument()->getObject(owner->getNameInDocument()); |
| 961 |
results.propertyIndex = 0; |
| 962 |
results.propertyName = components[results.propertyIndex].name.getString(); |
| 963 |
results.getProperty(*this); |
| 964 |
} |
| 965 |
} |
| 966 |
else { |
| 967 |
return; |
| 968 |
} |
| 969 |
} |
| 970 |
} |
| 971 |
|
| 972 |
Document* ObjectIdentifier::getDocument(String name, bool* ambiguous) const |
| 973 |
{ |
| 974 |
if (name.getString().empty()) { |
| 975 |
name = getDocumentName(); |
| 976 |
} |
| 977 |
|
| 978 |
App::Document* docById = nullptr; |
| 979 |
|
| 980 |
if (!name.isRealString()) { |
| 981 |
docById = App::GetApplication().getDocument(name.toString().c_str()); |
| 982 |
if (name.isForceIdentifier()) { |
| 983 |
return docById; |
| 984 |
} |
| 985 |
} |
| 986 |
|
| 987 |
App::Document* docByLabel = nullptr; |
| 988 |
const std::vector<App::Document*> docs = App::GetApplication().getDocuments(); |
| 989 |
|
| 990 |
for (auto doc : docs) { |
| 991 |
if (doc->Label.getValue() == name.getString()) { |
| 992 |
/* Multiple hits for same label? */ |
| 993 |
if (docByLabel) { |
| 994 |
if (ambiguous) { |
| 995 |
*ambiguous = true; |
| 996 |
} |
| 997 |
return nullptr; |
| 998 |
} |
| 999 |
docByLabel = doc; |
| 1000 |
} |
| 1001 |
} |
| 1002 |
|
| 1003 |
/* Not found on id? */ |
| 1004 |
if (!docById) { |
| 1005 |
return docByLabel; // Either not found at all, or on label |
| 1006 |
} |
| 1007 |
else { |
| 1008 |
/* Not found on label? */ |
| 1009 |
if (!docByLabel) { /* Then return doc by id */ |
| 1010 |
return docById; |
| 1011 |
} |
| 1012 |
|
| 1013 |
/* docByLabel and docById could be equal; that is ok */ |
| 1014 |
if (docByLabel == docById) { |
| 1015 |
return docById; |
| 1016 |
} |
| 1017 |
if (ambiguous) { |
| 1018 |
*ambiguous = true; |
| 1019 |
} |
| 1020 |
return nullptr; |
| 1021 |
} |
| 1022 |
} |
| 1023 |
|
| 1024 |
DocumentObject* ObjectIdentifier::getDocumentObject() const |
| 1025 |
{ |
| 1026 |
const App::Document* doc = getDocument(); |
| 1027 |
std::bitset<32> dummy; |
| 1028 |
|
| 1029 |
if (!doc) { |
| 1030 |
return nullptr; |
| 1031 |
} |
| 1032 |
|
| 1033 |
ResolveResults result(*this); |
| 1034 |
|
| 1035 |
return getDocumentObject(doc, result.resolvedDocumentObjectName, dummy); |
| 1036 |
} |
| 1037 |
|
| 1038 |
|
| 1039 |
enum PseudoPropertyType |
| 1040 |
{ |
| 1041 |
PseudoNone, |
| 1042 |
PseudoShape, |
| 1043 |
PseudoPlacement, |
| 1044 |
PseudoMatrix, |
| 1045 |
PseudoLinkPlacement, |
| 1046 |
PseudoLinkMatrix, |
| 1047 |
PseudoSelf, |
| 1048 |
PseudoApp, |
| 1049 |
PseudoPart, |
| 1050 |
PseudoRegex, |
| 1051 |
PseudoBuiltins, |
| 1052 |
PseudoMath, |
| 1053 |
PseudoCollections, |
| 1054 |
PseudoGui, |
| 1055 |
PseudoCadquery, |
| 1056 |
}; |
| 1057 |
|
| 1058 |
void ObjectIdentifier::getDepLabels(std::vector<std::string>& labels) const |
| 1059 |
{ |
| 1060 |
getDepLabels(ResolveResults(*this), labels); |
| 1061 |
} |
| 1062 |
|
| 1063 |
void ObjectIdentifier::getDepLabels(const ResolveResults& result, |
| 1064 |
std::vector<std::string>& labels) const |
| 1065 |
{ |
| 1066 |
if (!documentObjectName.getString().empty()) { |
| 1067 |
if (documentObjectName.isRealString()) { |
| 1068 |
labels.push_back(documentObjectName.getString()); |
| 1069 |
} |
| 1070 |
} |
| 1071 |
else if (result.propertyIndex == 1) { |
| 1072 |
labels.push_back(components[0].name.getString()); |
| 1073 |
} |
| 1074 |
if (!subObjectName.getString().empty()) { |
| 1075 |
PropertyLinkBase::getLabelReferences(labels, subObjectName.getString().c_str()); |
| 1076 |
} |
| 1077 |
} |
| 1078 |
|
| 1079 |
ObjectIdentifier::Dependencies ObjectIdentifier::getDep(bool needProps, |
| 1080 |
std::vector<std::string>* labels) const |
| 1081 |
{ |
| 1082 |
Dependencies deps; |
| 1083 |
getDep(deps, needProps, labels); |
| 1084 |
return deps; |
| 1085 |
} |
| 1086 |
|
| 1087 |
void ObjectIdentifier::getDep(Dependencies& deps, |
| 1088 |
bool needProps, |
| 1089 |
std::vector<std::string>* labels) const |
| 1090 |
{ |
| 1091 |
ResolveResults result(*this); |
| 1092 |
if (labels) { |
| 1093 |
getDepLabels(result, *labels); |
| 1094 |
} |
| 1095 |
|
| 1096 |
if (!result.resolvedDocumentObject) { |
| 1097 |
return; |
| 1098 |
} |
| 1099 |
|
| 1100 |
if (!needProps) { |
| 1101 |
deps[result.resolvedDocumentObject]; |
| 1102 |
return; |
| 1103 |
} |
| 1104 |
|
| 1105 |
if (!result.resolvedProperty) { |
| 1106 |
if (!result.propertyName.empty()) { |
| 1107 |
deps[result.resolvedDocumentObject].insert(result.propertyName); |
| 1108 |
} |
| 1109 |
return; |
| 1110 |
} |
| 1111 |
|
| 1112 |
Base::PyGILStateLocker lock; |
| 1113 |
try { |
| 1114 |
access(result, nullptr, &deps); |
| 1115 |
} |
| 1116 |
catch (Py::Exception& e) { |
| 1117 |
e.clear(); |
| 1118 |
} |
| 1119 |
catch (Base::Exception&) { |
| 1120 |
} |
| 1121 |
} |
| 1122 |
|
| 1123 |
std::vector<std::string> ObjectIdentifier::getStringList() const |
| 1124 |
{ |
| 1125 |
std::vector<std::string> l; |
| 1126 |
ResolveResults result(*this); |
| 1127 |
|
| 1128 |
if (!result.resolvedProperty || result.resolvedDocumentObject != owner) { |
| 1129 |
if (documentNameSet) { |
| 1130 |
l.push_back(documentName.toString()); |
| 1131 |
} |
| 1132 |
|
| 1133 |
if (documentObjectNameSet) { |
| 1134 |
l.push_back(documentObjectName.toString()); |
| 1135 |
} |
| 1136 |
} |
| 1137 |
if (!subObjectName.getString().empty()) { |
| 1138 |
l.back() += subObjectName.toString(); |
| 1139 |
} |
| 1140 |
std::vector<Component>::const_iterator i = components.begin(); |
| 1141 |
while (i != components.end()) { |
| 1142 |
std::ostringstream ss; |
| 1143 |
i->toString(ss); |
| 1144 |
l.push_back(ss.str()); |
| 1145 |
++i; |
| 1146 |
} |
| 1147 |
|
| 1148 |
return l; |
| 1149 |
} |
| 1150 |
|
| 1151 |
ObjectIdentifier ObjectIdentifier::relativeTo(const ObjectIdentifier& other) const |
| 1152 |
{ |
| 1153 |
ObjectIdentifier result(other.getOwner()); |
| 1154 |
ResolveResults thisresult(*this); |
| 1155 |
ResolveResults otherresult(other); |
| 1156 |
|
| 1157 |
if (otherresult.resolvedDocument != thisresult.resolvedDocument) { |
| 1158 |
result.setDocumentName(std::move(thisresult.resolvedDocumentName), true); |
| 1159 |
} |
| 1160 |
if (otherresult.resolvedDocumentObject != thisresult.resolvedDocumentObject) { |
| 1161 |
result.setDocumentObjectName(std::move(thisresult.resolvedDocumentObjectName), |
| 1162 |
true, |
| 1163 |
String(subObjectName)); |
| 1164 |
} |
| 1165 |
|
| 1166 |
for (std::size_t i = thisresult.propertyIndex; i < components.size(); ++i) { |
| 1167 |
result << components[i]; |
| 1168 |
} |
| 1169 |
|
| 1170 |
return result; |
| 1171 |
} |
| 1172 |
|
| 1173 |
ObjectIdentifier ObjectIdentifier::parse(const DocumentObject* docObj, const std::string& str) |
| 1174 |
{ |
| 1175 |
std::unique_ptr<Expression> expr(ExpressionParser::parse(docObj, str.c_str())); |
| 1176 |
VariableExpression* v = freecad_cast<VariableExpression*>(expr.get()); |
| 1177 |
|
| 1178 |
if (v) { |
| 1179 |
return v->getPath(); |
| 1180 |
} |
| 1181 |
else { |
| 1182 |
FC_THROWM(Base::RuntimeError, "Invalid property specification."); |
| 1183 |
} |
| 1184 |
} |
| 1185 |
|
| 1186 |
std::string ObjectIdentifier::resolveErrorString() const |
| 1187 |
{ |
| 1188 |
ResolveResults result(*this); |
| 1189 |
|
| 1190 |
return result.resolveErrorString(); |
| 1191 |
} |
| 1192 |
|
| 1193 |
ObjectIdentifier& ObjectIdentifier::operator<<(const ObjectIdentifier::Component& value) |
| 1194 |
{ |
| 1195 |
components.push_back(value); |
| 1196 |
_cache.clear(); |
| 1197 |
return *this; |
| 1198 |
} |
| 1199 |
|
| 1200 |
ObjectIdentifier& ObjectIdentifier::operator<<(ObjectIdentifier::Component&& value) |
| 1201 |
{ |
| 1202 |
components.push_back(std::move(value)); |
| 1203 |
_cache.clear(); |
| 1204 |
return *this; |
| 1205 |
} |
| 1206 |
|
| 1207 |
|
| 1208 |
Property* ObjectIdentifier::getProperty(int* ptype) const |
| 1209 |
{ |
| 1210 |
ResolveResults result(*this); |
| 1211 |
if (ptype) { |
| 1212 |
*ptype = result.propertyType; |
| 1213 |
} |
| 1214 |
return result.resolvedProperty; |
| 1215 |
} |
| 1216 |
|
| 1217 |
Property* ObjectIdentifier::resolveProperty(const App::DocumentObject* obj, |
| 1218 |
const char* propertyName, |
| 1219 |
App::DocumentObject*& sobj, |
| 1220 |
int& ptype) const |
| 1221 |
{ |
| 1222 |
if (obj && !subObjectName.getString().empty()) { |
| 1223 |
sobj = obj->getSubObject(subObjectName.toString().c_str()); |
| 1224 |
obj = sobj; |
| 1225 |
} |
| 1226 |
if (!obj) { |
| 1227 |
return nullptr; |
| 1228 |
} |
| 1229 |
|
| 1230 |
static std::unordered_map<const char*, int, CStringHasher, CStringHasher> _props = { |
| 1231 |
{"_shape", PseudoShape}, |
| 1232 |
{"_pla", PseudoPlacement}, |
| 1233 |
{"_matrix", PseudoMatrix}, |
| 1234 |
{"__pla", PseudoLinkPlacement}, |
| 1235 |
{"__matrix", PseudoLinkMatrix}, |
| 1236 |
{"_self", PseudoSelf}, |
| 1237 |
{"_app", PseudoApp}, |
| 1238 |
{"_part", PseudoPart}, |
| 1239 |
{"_re", PseudoRegex}, |
| 1240 |
{"_py", PseudoBuiltins}, |
| 1241 |
{"_math", PseudoMath}, |
| 1242 |
{"_coll", PseudoCollections}, |
| 1243 |
{"_gui", PseudoGui}, |
| 1244 |
{"_cq", PseudoCadquery}, |
| 1245 |
}; |
| 1246 |
auto it = _props.find(propertyName); |
| 1247 |
if (it == _props.end()) { |
| 1248 |
ptype = PseudoNone; |
| 1249 |
} |
| 1250 |
else { |
| 1251 |
ptype = it->second; |
| 1252 |
if (ptype != PseudoShape && !subObjectName.getString().empty() |
| 1253 |
&& !boost::ends_with(subObjectName.getString(), ".")) { |
| 1254 |
return nullptr; |
| 1255 |
} |
| 1256 |
return &const_cast<App::DocumentObject*>(obj)->Label; // fake the property |
| 1257 |
} |
| 1258 |
|
| 1259 |
return obj->getPropertyByName(propertyName); |
| 1260 |
} |
| 1261 |
|
| 1262 |
|
| 1263 |
ObjectIdentifier ObjectIdentifier::canonicalPath() const |
| 1264 |
{ |
| 1265 |
ObjectIdentifier res(*this); |
| 1266 |
ResolveResults result(res); |
| 1267 |
if (result.resolvedDocumentObject && result.resolvedDocumentObject != owner) { |
| 1268 |
res.owner = result.resolvedDocumentObject; |
| 1269 |
res._cache.clear(); |
| 1270 |
} |
| 1271 |
res.resolveAmbiguity(result); |
| 1272 |
if (!result.resolvedProperty || result.propertyType != PseudoNone) { |
| 1273 |
return res; |
| 1274 |
} |
| 1275 |
return result.resolvedProperty->canonicalPath(res); |
| 1276 |
} |
| 1277 |
|
| 1278 |
static const std::map<std::string, std::string>* _DocumentMap; |
| 1279 |
ObjectIdentifier::DocumentMapper::DocumentMapper(const std::map<std::string, std::string>& map) |
| 1280 |
{ |
| 1281 |
assert(!_DocumentMap); |
| 1282 |
_DocumentMap = ↦ |
| 1283 |
} |
| 1284 |
|
| 1285 |
ObjectIdentifier::DocumentMapper::~DocumentMapper() |
| 1286 |
{ |
| 1287 |
_DocumentMap = nullptr; |
| 1288 |
} |
| 1289 |
|
| 1290 |
void ObjectIdentifier::setDocumentName(ObjectIdentifier::String&& name, bool force) |
| 1291 |
{ |
| 1292 |
if (name.getString().empty()) { |
| 1293 |
force = false; |
| 1294 |
} |
| 1295 |
documentNameSet = force; |
| 1296 |
_cache.clear(); |
| 1297 |
if (!name.getString().empty() && _DocumentMap) { |
| 1298 |
if (name.isRealString()) { |
| 1299 |
auto iter = _DocumentMap->find(name.toString()); |
| 1300 |
if (iter != _DocumentMap->end()) { |
| 1301 |
documentName = String(iter->second, true); |
| 1302 |
return; |
| 1303 |
} |
| 1304 |
} |
| 1305 |
else { |
| 1306 |
auto iter = _DocumentMap->find(name.getString()); |
| 1307 |
if (iter != _DocumentMap->end()) { |
| 1308 |
documentName = String(iter->second, false, true); |
| 1309 |
return; |
| 1310 |
} |
| 1311 |
} |
| 1312 |
} |
| 1313 |
documentName = std::move(name); |
| 1314 |
} |
| 1315 |
|
| 1316 |
ObjectIdentifier::String ObjectIdentifier::getDocumentName() const |
| 1317 |
{ |
| 1318 |
ResolveResults result(*this); |
| 1319 |
|
| 1320 |
return result.resolvedDocumentName; |
| 1321 |
} |
| 1322 |
|
| 1323 |
void ObjectIdentifier::setDocumentObjectName(ObjectIdentifier::String&& name, |
| 1324 |
bool force, |
| 1325 |
ObjectIdentifier::String&& subname, |
| 1326 |
bool checkImport) |
| 1327 |
{ |
| 1328 |
if (checkImport) { |
| 1329 |
name.checkImport(owner); |
| 1330 |
subname.checkImport(owner, nullptr, &name); |
| 1331 |
} |
| 1332 |
|
| 1333 |
documentObjectName = std::move(name); |
| 1334 |
documentObjectNameSet = force; |
| 1335 |
subObjectName = std::move(subname); |
| 1336 |
|
| 1337 |
_cache.clear(); |
| 1338 |
} |
| 1339 |
|
| 1340 |
void ObjectIdentifier::setDocumentObjectName(const App::DocumentObject* obj, |
| 1341 |
bool force, |
| 1342 |
ObjectIdentifier::String&& subname, |
| 1343 |
bool checkImport) |
| 1344 |
{ |
| 1345 |
if (!owner || !obj || !obj->isAttachedToDocument() || !obj->getDocument()) { |
| 1346 |
FC_THROWM(Base::RuntimeError, "invalid object"); |
| 1347 |
} |
| 1348 |
|
| 1349 |
if (checkImport) { |
| 1350 |
subname.checkImport(owner, obj); |
| 1351 |
} |
| 1352 |
|
| 1353 |
if (obj == owner) { |
| 1354 |
force = false; |
| 1355 |
} |
| 1356 |
else { |
| 1357 |
localProperty = false; |
| 1358 |
} |
| 1359 |
if (obj->getDocument() == owner->getDocument()) { |
| 1360 |
setDocumentName(String()); |
| 1361 |
} |
| 1362 |
else if (!documentNameSet) { |
| 1363 |
if (obj->getDocument() == owner->getDocument()) { |
| 1364 |
setDocumentName(String()); |
| 1365 |
} |
| 1366 |
else { |
| 1367 |
documentNameSet = true; |
| 1368 |
documentName = String(obj->getDocument()->getName(), false, true); |
| 1369 |
} |
| 1370 |
} |
| 1371 |
else if (documentName.isRealString()) { |
| 1372 |
documentName = String(obj->getDocument()->Label.getStrValue(), true); |
| 1373 |
} |
| 1374 |
else { |
| 1375 |
documentName = String(obj->getDocument()->getName(), false, true); |
| 1376 |
} |
| 1377 |
|
| 1378 |
documentObjectNameSet = force; |
| 1379 |
documentObjectName = String(obj->getNameInDocument(), false, true); |
| 1380 |
subObjectName = std::move(subname); |
| 1381 |
|
| 1382 |
_cache.clear(); |
| 1383 |
} |
| 1384 |
|
| 1385 |
|
| 1386 |
ObjectIdentifier::String ObjectIdentifier::getDocumentObjectName() const |
| 1387 |
{ |
| 1388 |
ResolveResults result(*this); |
| 1389 |
|
| 1390 |
return result.resolvedDocumentObjectName; |
| 1391 |
} |
| 1392 |
|
| 1393 |
bool ObjectIdentifier::hasDocumentObjectName(bool forced) const |
| 1394 |
{ |
| 1395 |
return !documentObjectName.getString().empty() && (!forced || documentObjectNameSet); |
| 1396 |
} |
| 1397 |
|
| 1398 |
std::string ObjectIdentifier::String::toString(bool toPython) const |
| 1399 |
{ |
| 1400 |
if (isRealString()) { |
| 1401 |
return quote(str, toPython); |
| 1402 |
} |
| 1403 |
else { |
| 1404 |
return str; |
| 1405 |
} |
| 1406 |
} |
| 1407 |
|
| 1408 |
void ObjectIdentifier::String::checkImport(const App::DocumentObject* owner, |
| 1409 |
const App::DocumentObject* obj, |
| 1410 |
const String* objName) |
| 1411 |
{ |
| 1412 |
if (owner && owner->getDocument() && !str.empty() |
| 1413 |
&& ExpressionParser::ExpressionImporter::reader()) { |
| 1414 |
auto reader = ExpressionParser::ExpressionImporter::reader(); |
| 1415 |
if (obj || objName) { |
| 1416 |
bool restoreLabel = false; |
| 1417 |
str = PropertyLinkBase::importSubName(*reader, str.c_str(), restoreLabel); |
| 1418 |
if (restoreLabel) { |
| 1419 |
if (!obj) { |
| 1420 |
std::bitset<32> flags; |
| 1421 |
obj = getDocumentObject(owner->getDocument(), *objName, flags); |
| 1422 |
if (!obj) { |
| 1423 |
FC_ERR("Cannot find object " << objName->toString()); |
| 1424 |
} |
| 1425 |
} |
| 1426 |
|
| 1427 |
if (obj) { |
| 1428 |
PropertyLinkBase::restoreLabelReference(obj, str); |
| 1429 |
} |
| 1430 |
} |
| 1431 |
} |
| 1432 |
else if (str.back() != '@') { |
| 1433 |
str = reader->getName(str.c_str()); |
| 1434 |
} |
| 1435 |
else { |
| 1436 |
str.resize(str.size() - 1); |
| 1437 |
auto mapped = reader->getName(str.c_str()); |
| 1438 |
auto objForMapped = owner->getDocument()->getObject(mapped); |
| 1439 |
if (!objForMapped) { |
| 1440 |
FC_ERR("Cannot find object " << str); |
| 1441 |
} |
| 1442 |
else { |
| 1443 |
isString = true; |
| 1444 |
forceIdentifier = false; |
| 1445 |
str = objForMapped->Label.getValue(); |
| 1446 |
} |
| 1447 |
} |
| 1448 |
} |
| 1449 |
} |
| 1450 |
|
| 1451 |
Py::Object |
| 1452 |
ObjectIdentifier::access(const ResolveResults& result, const Py::Object* value, Dependencies* deps) const |
| 1453 |
{ |
| 1454 |
if (!result.resolvedDocumentObject || !result.resolvedProperty |
| 1455 |
|| (!subObjectName.getString().empty() && !result.resolvedSubObject)) { |
| 1456 |
FC_THROWM(Base::RuntimeError, result.resolveErrorString() << " in '" << toString() << "'"); |
| 1457 |
} |
| 1458 |
|
| 1459 |
Py::Object pyobj; |
| 1460 |
int ptype = result.propertyType; |
| 1461 |
|
| 1462 |
// NOTE! We do not keep reference of the imported module, assuming once |
| 1463 |
// imported they'll live (because of sys.modules) till the application |
| 1464 |
// dies. |
| 1465 |
#define GET_MODULE(_name) \ |
| 1466 |
do { \ |
| 1467 |
static PyObject* pymod; \ |
| 1468 |
if (!pymod) { \ |
| 1469 |
pymod = PyImport_ImportModule(#_name); \ |
| 1470 |
if (!pymod) \ |
| 1471 |
Base::PyException::throwException(); \ |
| 1472 |
else \ |
| 1473 |
Py_DECREF(pymod); \ |
| 1474 |
} \ |
| 1475 |
pyobj = Py::Object(pymod); \ |
| 1476 |
} while (0) |
| 1477 |
|
| 1478 |
size_t idx = result.propertyIndex + 1; |
| 1479 |
switch (ptype) { |
| 1480 |
case PseudoApp: |
| 1481 |
GET_MODULE(FreeCAD); |
| 1482 |
break; |
| 1483 |
case PseudoGui: |
| 1484 |
GET_MODULE(FreeCADGui); |
| 1485 |
break; |
| 1486 |
case PseudoPart: |
| 1487 |
GET_MODULE(Part); |
| 1488 |
break; |
| 1489 |
case PseudoCadquery: |
| 1490 |
GET_MODULE(freecad.fc_cadquery); |
| 1491 |
break; |
| 1492 |
case PseudoRegex: |
| 1493 |
GET_MODULE(re); |
| 1494 |
break; |
| 1495 |
case PseudoBuiltins: |
| 1496 |
GET_MODULE(builtins); |
| 1497 |
break; |
| 1498 |
case PseudoMath: |
| 1499 |
GET_MODULE(math); |
| 1500 |
break; |
| 1501 |
case PseudoCollections: |
| 1502 |
GET_MODULE(collections); |
| 1503 |
break; |
| 1504 |
case PseudoShape: { |
| 1505 |
GET_MODULE(Part); |
| 1506 |
Py::Callable func(pyobj.getAttr("getShape")); |
| 1507 |
Py::Tuple tuple(1); |
| 1508 |
tuple.setItem(0, Py::Object(result.resolvedDocumentObject->getPyObject(), true)); |
| 1509 |
if (result.subObjectName.getString().empty()) { |
| 1510 |
pyobj = func.apply(tuple); |
| 1511 |
} |
| 1512 |
else { |
| 1513 |
Py::Dict dict; |
| 1514 |
dict.setItem("subname", Py::String(result.subObjectName.getString())); |
| 1515 |
dict.setItem("needSubElement", Py::True()); |
| 1516 |
pyobj = func.apply(tuple, dict); |
| 1517 |
} |
| 1518 |
break; |
| 1519 |
} |
| 1520 |
default: { |
| 1521 |
Base::Matrix4D mat; |
| 1522 |
auto obj = result.resolvedDocumentObject; |
| 1523 |
switch (ptype) { |
| 1524 |
case PseudoPlacement: |
| 1525 |
case PseudoMatrix: |
| 1526 |
case PseudoLinkPlacement: |
| 1527 |
case PseudoLinkMatrix: |
| 1528 |
obj->getSubObject(result.subObjectName.getString().c_str(), nullptr, &mat); |
| 1529 |
break; |
| 1530 |
default: |
| 1531 |
break; |
| 1532 |
} |
| 1533 |
if (result.resolvedSubObject) { |
| 1534 |
obj = result.resolvedSubObject; |
| 1535 |
} |
| 1536 |
switch (ptype) { |
| 1537 |
case PseudoPlacement: |
| 1538 |
pyobj = Py::Placement(Base::Placement(mat)); |
| 1539 |
break; |
| 1540 |
case PseudoMatrix: |
| 1541 |
pyobj = Py::Matrix(mat); |
| 1542 |
break; |
| 1543 |
case PseudoLinkPlacement: |
| 1544 |
case PseudoLinkMatrix: { |
| 1545 |
auto linked = obj->getLinkedObject(true, &mat, false); |
| 1546 |
if (!linked || linked == obj) { |
| 1547 |
auto ext = obj->getExtensionByType<App::LinkBaseExtension>(true); |
| 1548 |
if (ext) { |
| 1549 |
ext->getTrueLinkedObject(true, &mat); |
| 1550 |
} |
| 1551 |
} |
| 1552 |
if (ptype == PseudoLinkPlacement) { |
| 1553 |
pyobj = Py::Placement(Base::Placement(mat)); |
| 1554 |
} |
| 1555 |
else { |
| 1556 |
pyobj = Py::Matrix(mat); |
| 1557 |
} |
| 1558 |
break; |
| 1559 |
} |
| 1560 |
case PseudoSelf: |
| 1561 |
pyobj = Py::Object(obj->getPyObject(), true); |
| 1562 |
break; |
| 1563 |
default: { |
| 1564 |
// NOTE! We cannot directly call Property::getPyObject(), but |
| 1565 |
// instead, must obtain the property's python object through |
| 1566 |
// DocumentObjectPy::getAttr(). Because, PyObjectBase has internal |
| 1567 |
// attribute tracking only if we obtain attribute through |
| 1568 |
// getAttr(). Without attribute tracking, we can't do things like |
| 1569 |
// |
| 1570 |
// obj.Placement.Base.x = 10. |
| 1571 |
// |
| 1572 |
// What happens is that the when Python interpreter calls |
| 1573 |
// |
| 1574 |
// Base.setAttr('x', 10), |
| 1575 |
// |
| 1576 |
// PyObjectBase will lookup Base's parent, i.e. Placement, and call |
| 1577 |
// |
| 1578 |
// Placement.setAttr('Base', Base), |
| 1579 |
// |
| 1580 |
// and in turn calls |
| 1581 |
// |
| 1582 |
// obj.setAttr('Placement',Placement) |
| 1583 |
// |
| 1584 |
// The tracking logic is implemented in PyObjectBase::__getattro/__setattro |
| 1585 |
|
| 1586 |
auto container = result.resolvedProperty->getContainer(); |
| 1587 |
if (container && container != result.resolvedDocumentObject |
| 1588 |
&& container != result.resolvedSubObject) { |
| 1589 |
if (!container->isDerivedFrom<DocumentObject>()) { |
| 1590 |
FC_WARN("Invalid property container"); |
| 1591 |
} |
| 1592 |
else { |
| 1593 |
obj = static_cast<DocumentObject*>(container); |
| 1594 |
} |
| 1595 |
} |
| 1596 |
pyobj = Py::Object(obj->getPyObject(), true); |
| 1597 |
idx = result.propertyIndex; |
| 1598 |
break; |
| 1599 |
} |
| 1600 |
} |
| 1601 |
} |
| 1602 |
} |
| 1603 |
|
| 1604 |
auto setPropDep = [deps](DocumentObject* obj, Property* prop, const char* propName) { |
| 1605 |
if (!deps || !obj) { |
| 1606 |
return; |
| 1607 |
} |
| 1608 |
if (prop && prop->getContainer() != obj) { |
| 1609 |
auto linkTouched = |
| 1610 |
freecad_cast<PropertyBool*>(obj->getPropertyByName("_LinkTouched")); |
| 1611 |
if (linkTouched) { |
| 1612 |
propName = linkTouched->getName(); |
| 1613 |
} |
| 1614 |
else { |
| 1615 |
auto propOwner = freecad_cast<DocumentObject*>(prop->getContainer()); |
| 1616 |
if (propOwner) { |
| 1617 |
obj = propOwner; |
| 1618 |
} |
| 1619 |
else { |
| 1620 |
propName = nullptr; |
| 1621 |
} |
| 1622 |
} |
| 1623 |
} |
| 1624 |
auto& propset = (*deps)[obj]; |
| 1625 |
// inserting a blank name in the propset indicates the dependency is |
| 1626 |
// on all properties of the corresponding object. |
| 1627 |
if (propset.size() != 1 || !propset.begin()->empty()) { |
| 1628 |
if (!propName) { |
| 1629 |
propset.clear(); |
| 1630 |
propset.insert(""); |
| 1631 |
} |
| 1632 |
else { |
| 1633 |
propset.insert(propName); |
| 1634 |
} |
| 1635 |
} |
| 1636 |
return; |
| 1637 |
}; |
| 1638 |
|
| 1639 |
App::DocumentObject* lastObj = result.resolvedDocumentObject; |
| 1640 |
if (result.resolvedSubObject) { |
| 1641 |
setPropDep(lastObj, nullptr, nullptr); |
| 1642 |
lastObj = result.resolvedSubObject; |
| 1643 |
} |
| 1644 |
if (ptype == PseudoNone) { |
| 1645 |
setPropDep(lastObj, result.resolvedProperty, result.resolvedProperty->getName()); |
| 1646 |
} |
| 1647 |
else { |
| 1648 |
setPropDep(lastObj, nullptr, nullptr); |
| 1649 |
} |
| 1650 |
lastObj = nullptr; |
| 1651 |
|
| 1652 |
if (components.empty()) { |
| 1653 |
return pyobj; |
| 1654 |
} |
| 1655 |
|
| 1656 |
size_t count = components.size(); |
| 1657 |
if (value) { |
| 1658 |
--count; |
| 1659 |
} |
| 1660 |
assert(idx <= count); |
| 1661 |
|
| 1662 |
for (; idx < count; ++idx) { |
| 1663 |
if (PyObject_TypeCheck(*pyobj, &DocumentObjectPy::Type)) { |
| 1664 |
lastObj = static_cast<DocumentObjectPy*>(*pyobj)->getDocumentObjectPtr(); |
| 1665 |
} |
| 1666 |
else if (lastObj) { |
| 1667 |
const char* attr = components[idx].getName().c_str(); |
| 1668 |
auto prop = lastObj->getPropertyByName(attr); |
| 1669 |
setPropDep(lastObj, prop, attr); |
| 1670 |
lastObj = nullptr; |
| 1671 |
} |
| 1672 |
pyobj = components[idx].get(pyobj); |
| 1673 |
} |
| 1674 |
if (value) { |
| 1675 |
components[idx].set(pyobj, *value); |
| 1676 |
return Py::Object(); |
| 1677 |
} |
| 1678 |
return pyobj; |
| 1679 |
} |
| 1680 |
|
| 1681 |
App::any ObjectIdentifier::getValue(bool pathValue, bool* isPseudoProperty) const |
| 1682 |
{ |
| 1683 |
ResolveResults rs(*this); |
| 1684 |
|
| 1685 |
if (isPseudoProperty) { |
| 1686 |
*isPseudoProperty = rs.propertyType != PseudoNone; |
| 1687 |
if (rs.propertyType == PseudoSelf && isLocalProperty() |
| 1688 |
&& rs.propertyIndex + 1 < (int)components.size() |
| 1689 |
&& owner->getPropertyByName(components[rs.propertyIndex + 1].getName().c_str())) { |
| 1690 |
*isPseudoProperty = false; |
| 1691 |
} |
| 1692 |
} |
| 1693 |
|
| 1694 |
if (rs.resolvedProperty && rs.propertyType == PseudoNone && pathValue) { |
| 1695 |
return rs.resolvedProperty->getPathValue(*this); |
| 1696 |
} |
| 1697 |
|
| 1698 |
Base::PyGILStateLocker lock; |
| 1699 |
try { |
| 1700 |
return pyObjectToAny(access(rs)); |
| 1701 |
} |
| 1702 |
catch (Py::Exception&) { |
| 1703 |
Base::PyException::throwException(); |
| 1704 |
} |
| 1705 |
return {}; |
| 1706 |
} |
| 1707 |
|
| 1708 |
Py::Object ObjectIdentifier::getPyValue(bool pathValue, bool* isPseudoProperty) const |
| 1709 |
{ |
| 1710 |
ResolveResults rs(*this); |
| 1711 |
|
| 1712 |
if (isPseudoProperty) { |
| 1713 |
*isPseudoProperty = rs.propertyType != PseudoNone; |
| 1714 |
if (rs.propertyType == PseudoSelf && isLocalProperty() |
| 1715 |
&& rs.propertyIndex + 1 < (int)components.size() |
| 1716 |
&& owner->getPropertyByName(components[rs.propertyIndex + 1].getName().c_str())) { |
| 1717 |
*isPseudoProperty = false; |
| 1718 |
} |
| 1719 |
} |
| 1720 |
|
| 1721 |
if (rs.resolvedProperty && rs.propertyType == PseudoNone && pathValue) { |
| 1722 |
Py::Object res; |
| 1723 |
if (rs.resolvedProperty->getPyPathValue(*this, res)) { |
| 1724 |
return res; |
| 1725 |
} |
| 1726 |
} |
| 1727 |
|
| 1728 |
try { |
| 1729 |
return access(rs); |
| 1730 |
} |
| 1731 |
catch (Py::Exception&) { |
| 1732 |
Base::PyException::throwException(); |
| 1733 |
} |
| 1734 |
return Py::Object(); |
| 1735 |
} |
| 1736 |
|
| 1737 |
void ObjectIdentifier::setValue(const App::any& value) const |
| 1738 |
{ |
| 1739 |
std::stringstream ss; |
| 1740 |
ResolveResults rs(*this); |
| 1741 |
if (rs.propertyType) { |
| 1742 |
FC_THROWM(Base::RuntimeError, "Cannot set pseudo property"); |
| 1743 |
} |
| 1744 |
|
| 1745 |
Base::PyGILStateLocker lock; |
| 1746 |
try { |
| 1747 |
Py::Object pyvalue = pyObjectFromAny(value); |
| 1748 |
access(rs, &pyvalue); |
| 1749 |
} |
| 1750 |
catch (Py::Exception&) { |
| 1751 |
Base::PyException::throwException(); |
| 1752 |
} |
| 1753 |
} |
| 1754 |
|
| 1755 |
const std::string& ObjectIdentifier::getSubObjectName(bool newStyle) const |
| 1756 |
{ |
| 1757 |
if (newStyle && !shadowSub.newName.empty()) { |
| 1758 |
return shadowSub.newName; |
| 1759 |
} |
| 1760 |
if (!shadowSub.oldName.empty()) { |
| 1761 |
return shadowSub.oldName; |
| 1762 |
} |
| 1763 |
return subObjectName.getString(); |
| 1764 |
} |
| 1765 |
|
| 1766 |
const std::string& ObjectIdentifier::getSubObjectName() const |
| 1767 |
{ |
| 1768 |
return subObjectName.getString(); |
| 1769 |
} |
| 1770 |
|
| 1771 |
void ObjectIdentifier::importSubNames(const ObjectIdentifier::SubNameMap& subNameMap) |
| 1772 |
{ |
| 1773 |
if (!owner || !owner->getDocument()) { |
| 1774 |
return; |
| 1775 |
} |
| 1776 |
ResolveResults result(*this); |
| 1777 |
auto it = subNameMap.find(std::make_pair(result.resolvedDocumentObject, std::string())); |
| 1778 |
if (it != subNameMap.end()) { |
| 1779 |
auto obj = owner->getDocument()->getObject(it->second.c_str()); |
| 1780 |
if (!obj) { |
| 1781 |
FC_ERR("Failed to find import object " << it->second << " from " |
| 1782 |
<< result.resolvedDocumentObject->getFullName()); |
| 1783 |
return; |
| 1784 |
} |
| 1785 |
documentNameSet = false; |
| 1786 |
documentName.str.clear(); |
| 1787 |
if (documentObjectName.isRealString()) { |
| 1788 |
documentObjectName.str = obj->Label.getValue(); |
| 1789 |
} |
| 1790 |
else { |
| 1791 |
documentObjectName.str = obj->getNameInDocument(); |
| 1792 |
} |
| 1793 |
_cache.clear(); |
| 1794 |
} |
| 1795 |
if (subObjectName.getString().empty()) { |
| 1796 |
return; |
| 1797 |
} |
| 1798 |
it = subNameMap.find(std::make_pair(result.resolvedDocumentObject, subObjectName.str)); |
| 1799 |
if (it == subNameMap.end()) { |
| 1800 |
return; |
| 1801 |
} |
| 1802 |
subObjectName = String(it->second, true); |
| 1803 |
_cache.clear(); |
| 1804 |
shadowSub.newName.clear(); |
| 1805 |
shadowSub.oldName.clear(); |
| 1806 |
} |
| 1807 |
|
| 1808 |
bool ObjectIdentifier::updateElementReference(ExpressionVisitor& v, |
| 1809 |
App::DocumentObject* feature, |
| 1810 |
bool reverse) |
| 1811 |
{ |
| 1812 |
assert(v.getPropertyLink()); |
| 1813 |
if (subObjectName.getString().empty()) { |
| 1814 |
return false; |
| 1815 |
} |
| 1816 |
|
| 1817 |
ResolveResults result(*this); |
| 1818 |
if (!result.resolvedSubObject) { |
| 1819 |
return false; |
| 1820 |
} |
| 1821 |
if (v.getPropertyLink()->_updateElementReference(feature, |
| 1822 |
result.resolvedDocumentObject, |
| 1823 |
subObjectName.str, |
| 1824 |
shadowSub, |
| 1825 |
reverse)) { |
| 1826 |
_cache.clear(); |
| 1827 |
v.aboutToChange(); |
| 1828 |
return true; |
| 1829 |
} |
| 1830 |
return false; |
| 1831 |
} |
| 1832 |
|
| 1833 |
bool ObjectIdentifier::adjustLinks(ExpressionVisitor& v, |
| 1834 |
const std::set<App::DocumentObject*>& inList) |
| 1835 |
{ |
| 1836 |
ResolveResults result(*this); |
| 1837 |
if (!result.resolvedDocumentObject) { |
| 1838 |
return false; |
| 1839 |
} |
| 1840 |
if (result.resolvedSubObject) { |
| 1841 |
PropertyLinkSub prop; |
| 1842 |
prop.setValue(result.resolvedDocumentObject, {subObjectName.getString()}); |
| 1843 |
if (prop.adjustLink(inList)) { |
| 1844 |
v.aboutToChange(); |
| 1845 |
documentObjectName = String(prop.getValue()->getNameInDocument(), false, true); |
| 1846 |
subObjectName = String(prop.getSubValues().front(), true); |
| 1847 |
_cache.clear(); |
| 1848 |
return true; |
| 1849 |
} |
| 1850 |
} |
| 1851 |
return false; |
| 1852 |
} |
| 1853 |
|
| 1854 |
bool ObjectIdentifier::isTouched() const |
| 1855 |
{ |
| 1856 |
try { |
| 1857 |
ResolveResults result(*this); |
| 1858 |
if (result.resolvedProperty) { |
| 1859 |
if (result.propertyType == PseudoNone) { |
| 1860 |
return result.resolvedProperty->isTouched(); |
| 1861 |
} |
| 1862 |
else { |
| 1863 |
return result.resolvedDocumentObject->isTouched(); |
| 1864 |
} |
| 1865 |
} |
| 1866 |
} |
| 1867 |
catch (...) { |
| 1868 |
} |
| 1869 |
return false; |
| 1870 |
} |
| 1871 |
|
| 1872 |
void ObjectIdentifier::resolveAmbiguity() |
| 1873 |
{ |
| 1874 |
if (!owner || !owner->isAttachedToDocument() || isLocalProperty() |
| 1875 |
|| (documentObjectNameSet && !documentObjectName.getString().empty() |
| 1876 |
&& (documentObjectName.isRealString() || documentObjectName.isForceIdentifier()))) { |
| 1877 |
return; |
| 1878 |
} |
| 1879 |
|
| 1880 |
ResolveResults result(*this); |
| 1881 |
resolveAmbiguity(result); |
| 1882 |
} |
| 1883 |
|
| 1884 |
void ObjectIdentifier::resolveAmbiguity(const ResolveResults& result) |
| 1885 |
{ |
| 1886 |
|
| 1887 |
if (!result.resolvedDocumentObject) { |
| 1888 |
return; |
| 1889 |
} |
| 1890 |
|
| 1891 |
if (result.propertyIndex == 1) { |
| 1892 |
components.erase(components.begin()); |
| 1893 |
} |
| 1894 |
|
| 1895 |
String subname = subObjectName; |
| 1896 |
if (result.resolvedDocumentObject == owner) { |
| 1897 |
setDocumentObjectName(owner, false, std::move(subname)); |
| 1898 |
} |
| 1899 |
else if (result.flags.test(ResolveByIdentifier)) { |
| 1900 |
setDocumentObjectName(result.resolvedDocumentObject, true, std::move(subname)); |
| 1901 |
} |
| 1902 |
else { |
| 1903 |
setDocumentObjectName( |
| 1904 |
String(result.resolvedDocumentObject->Label.getStrValue(), true, false), |
| 1905 |
true, |
| 1906 |
std::move(subname)); |
| 1907 |
} |
| 1908 |
|
| 1909 |
if (result.resolvedDocumentObject->getDocument() == owner->getDocument()) { |
| 1910 |
setDocumentName(String()); |
| 1911 |
} |
| 1912 |
} |
| 1913 |
|
| 1914 |
ObjectIdentifier::ResolveResults::ResolveResults(const ObjectIdentifier& oi) |
| 1915 |
: propertyType(PseudoNone) |
| 1916 |
{ |
| 1917 |
oi.resolve(*this); |
| 1918 |
} |
| 1919 |
|
| 1920 |
std::string ObjectIdentifier::ResolveResults::resolveErrorString() const |
| 1921 |
{ |
| 1922 |
std::ostringstream ss; |
| 1923 |
if (!resolvedDocument) { |
| 1924 |
if (flags.test(ResolveAmbiguous)) { |
| 1925 |
ss << "Ambiguous document name/label '" << resolvedDocumentName.getString() << "'"; |
| 1926 |
} |
| 1927 |
else { |
| 1928 |
ss << "Document '" << resolvedDocumentName.toString() << "' not found"; |
| 1929 |
} |
| 1930 |
} |
| 1931 |
else if (!resolvedDocumentObject) { |
| 1932 |
if (flags.test(ResolveAmbiguous)) { |
| 1933 |
ss << "Ambiguous document object name '" << resolvedDocumentObjectName.getString() |
| 1934 |
<< "'"; |
| 1935 |
} |
| 1936 |
else { |
| 1937 |
ss << "Document object '" << resolvedDocumentObjectName.toString() << "' not found"; |
| 1938 |
} |
| 1939 |
} |
| 1940 |
else if (!subObjectName.getString().empty() && !resolvedSubObject) { |
| 1941 |
ss << "Sub-object '" << resolvedDocumentObjectName.getString() << '.' |
| 1942 |
<< subObjectName.toString() << "' not found"; |
| 1943 |
} |
| 1944 |
else if (!resolvedProperty) { |
| 1945 |
if (propertyType != PseudoShape && !subObjectName.getString().empty() |
| 1946 |
&& !boost::ends_with(subObjectName.getString(), ".")) { |
| 1947 |
ss << "Non geometry subname reference must end with '.'"; |
| 1948 |
} |
| 1949 |
else { |
| 1950 |
ss << "Property '" << propertyName << "' not found"; |
| 1951 |
} |
| 1952 |
} |
| 1953 |
|
| 1954 |
return ss.str(); |
| 1955 |
} |
| 1956 |
|
| 1957 |
void ObjectIdentifier::ResolveResults::getProperty(const ObjectIdentifier& oi) |
| 1958 |
{ |
| 1959 |
resolvedProperty = oi.resolveProperty(resolvedDocumentObject, |
| 1960 |
propertyName.c_str(), |
| 1961 |
resolvedSubObject, |
| 1962 |
propertyType); |
| 1963 |
} |