| 1 |
/**************************************************************************** |
| 2 |
* Copyright (c) 2017 Zheng Lei (realthunder) <[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 <limits> |
| 24 |
|
| 25 |
#include <boost/algorithm/string/predicate.hpp> |
| 26 |
#include <boost/range.hpp> |
| 27 |
#include <boost/property_map/property_map.hpp> |
| 28 |
|
| 29 |
#include <Base/Tools.h> |
| 30 |
#include <Base/Uuid.h> |
| 31 |
|
| 32 |
#include "Application.h" |
| 33 |
#include "ElementNamingUtils.h" |
| 34 |
#include "ComplexGeoDataPy.h" |
| 35 |
#include "Document.h" |
| 36 |
#include "DocumentObserver.h" |
| 37 |
#include "GeoFeatureGroupExtension.h" |
| 38 |
#include "Link.h" |
| 39 |
#include "LinkBaseExtensionPy.h" |
| 40 |
|
| 41 |
// FIXME: ISO C++11 requires at least one argument for the "..." in a variadic macro |
| 42 |
#if defined(__clang__) |
| 43 |
#pragma clang diagnostic push |
| 44 |
#pragma clang diagnostic ignored "-Wgnu-zero-variadic-macro-arguments" |
| 45 |
#endif |
| 46 |
|
| 47 |
FC_LOG_LEVEL_INIT("App::Link", true, true) |
| 48 |
|
| 49 |
using namespace App; |
| 50 |
using namespace Base; |
| 51 |
namespace sp = std::placeholders; |
| 52 |
|
| 53 |
using CharRange = boost::iterator_range<const char*>; |
| 54 |
|
| 55 |
//////////////////////////////////////////////////////////////////////// |
| 56 |
|
| 57 |
/*[[[cog |
| 58 |
import LinkParams |
| 59 |
LinkParams.define() |
| 60 |
]]]*/ |
| 61 |
|
| 62 |
namespace |
| 63 |
{ |
| 64 |
|
| 65 |
// Auto generated code. See class document of LinkParams. |
| 66 |
class LinkParamsP: public ParameterGrp::ObserverType |
| 67 |
{ |
| 68 |
public: |
| 69 |
// Auto generated code. See class document of LinkParams. |
| 70 |
ParameterGrp::handle handle; |
| 71 |
|
| 72 |
// Auto generated code. See class document of LinkParams. |
| 73 |
std::unordered_map<const char*, void (*)(LinkParamsP*), App::CStringHasher, App::CStringHasher> |
| 74 |
funcs; |
| 75 |
|
| 76 |
bool CopyOnChangeApplyToAll; // Auto generated code. See class document of LinkParams. |
| 77 |
|
| 78 |
// Auto generated code. See class document of LinkParams. |
| 79 |
LinkParamsP() |
| 80 |
{ |
| 81 |
handle = App::GetApplication().GetParameterGroupByPath( |
| 82 |
"User parameter:BaseApp/Preferences/Link"); |
| 83 |
handle->Attach(this); |
| 84 |
|
| 85 |
CopyOnChangeApplyToAll = handle->GetBool("CopyOnChangeApplyToAll", true); |
| 86 |
funcs["CopyOnChangeApplyToAll"] = &LinkParamsP::updateCopyOnChangeApplyToAll; |
| 87 |
} |
| 88 |
|
| 89 |
// Auto generated code. See class document of LinkParams. |
| 90 |
~LinkParamsP() override = default; |
| 91 |
|
| 92 |
// Auto generated code. See class document of LinkParams. |
| 93 |
void OnChange(Base::Subject<const char*>&, const char* sReason) override |
| 94 |
{ |
| 95 |
if (!sReason) { |
| 96 |
return; |
| 97 |
} |
| 98 |
auto it = funcs.find(sReason); |
| 99 |
if (it == funcs.end()) { |
| 100 |
return; |
| 101 |
} |
| 102 |
it->second(this); |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
// Auto generated code. See class document of LinkParams. |
| 107 |
static void updateCopyOnChangeApplyToAll(LinkParamsP* self) |
| 108 |
{ |
| 109 |
self->CopyOnChangeApplyToAll = self->handle->GetBool("CopyOnChangeApplyToAll", true); |
| 110 |
} |
| 111 |
}; |
| 112 |
|
| 113 |
// Auto generated code. See class document of LinkParams. |
| 114 |
LinkParamsP* instance() |
| 115 |
{ |
| 116 |
static LinkParamsP* inst = new LinkParamsP; |
| 117 |
return inst; |
| 118 |
} |
| 119 |
|
| 120 |
} // Anonymous namespace |
| 121 |
|
| 122 |
// Auto generated code. See class document of LinkParams. |
| 123 |
ParameterGrp::handle LinkParams::getHandle() |
| 124 |
{ |
| 125 |
return instance()->handle; |
| 126 |
} |
| 127 |
|
| 128 |
// Auto generated code. See class document of LinkParams. |
| 129 |
const char* LinkParams::docCopyOnChangeApplyToAll() |
| 130 |
{ |
| 131 |
return QT_TRANSLATE_NOOP( |
| 132 |
"LinkParams", |
| 133 |
"Stores the last user choice of whether to apply CopyOnChange setup to all links\n" |
| 134 |
"that reference the same configurable object"); |
| 135 |
} |
| 136 |
|
| 137 |
// Auto generated code. See class document of LinkParams. |
| 138 |
const bool& LinkParams::getCopyOnChangeApplyToAll() |
| 139 |
{ |
| 140 |
return instance()->CopyOnChangeApplyToAll; |
| 141 |
} |
| 142 |
|
| 143 |
// Auto generated code. See class document of LinkParams. |
| 144 |
const bool& LinkParams::defaultCopyOnChangeApplyToAll() |
| 145 |
{ |
| 146 |
static const bool def = true; |
| 147 |
return def; |
| 148 |
} |
| 149 |
|
| 150 |
// Auto generated code. See class document of LinkParams. |
| 151 |
void LinkParams::setCopyOnChangeApplyToAll(const bool& v) |
| 152 |
{ |
| 153 |
instance()->handle->SetBool("CopyOnChangeApplyToAll", v); |
| 154 |
instance()->CopyOnChangeApplyToAll = v; |
| 155 |
} |
| 156 |
|
| 157 |
// Auto generated code. See class document of LinkParams. |
| 158 |
void LinkParams::removeCopyOnChangeApplyToAll() |
| 159 |
{ |
| 160 |
instance()->handle->RemoveBool("CopyOnChangeApplyToAll"); |
| 161 |
} |
| 162 |
//[[[end]]] |
| 163 |
|
| 164 |
/////////////////////////////////////////////////////////////////////////////// |
| 165 |
|
| 166 |
EXTENSION_PROPERTY_SOURCE(App::LinkBaseExtension, App::DocumentObjectExtension) |
| 167 |
|
| 168 |
LinkBaseExtension::LinkBaseExtension() |
| 169 |
{ |
| 170 |
initExtensionType(LinkBaseExtension::getExtensionClassTypeId()); |
| 171 |
EXTENSION_ADD_PROPERTY_TYPE(_LinkTouched, |
| 172 |
(false), |
| 173 |
" Link", |
| 174 |
PropertyType(Prop_Hidden | Prop_NoPersist), |
| 175 |
0); |
| 176 |
EXTENSION_ADD_PROPERTY_TYPE(_ChildCache, |
| 177 |
(), |
| 178 |
" Link", |
| 179 |
PropertyType(Prop_Hidden | Prop_NoPersist | Prop_ReadOnly), |
| 180 |
0); |
| 181 |
_ChildCache.setScope(LinkScope::Global); |
| 182 |
EXTENSION_ADD_PROPERTY_TYPE(_LinkOwner, |
| 183 |
(0), |
| 184 |
" Link", |
| 185 |
PropertyType(Prop_Hidden | Prop_Output), |
| 186 |
0); |
| 187 |
props.resize(PropMax, nullptr); |
| 188 |
} |
| 189 |
|
| 190 |
PyObject* LinkBaseExtension::getExtensionPyObject() |
| 191 |
{ |
| 192 |
if (ExtensionPythonObject.is(Py::_None())) { |
| 193 |
// ref counter is set to 1 |
| 194 |
ExtensionPythonObject = Py::Object(new LinkBaseExtensionPy(this), true); |
| 195 |
} |
| 196 |
return Py::new_reference_to(ExtensionPythonObject); |
| 197 |
} |
| 198 |
|
| 199 |
const std::vector<LinkBaseExtension::PropInfo>& LinkBaseExtension::getPropertyInfo() const |
| 200 |
{ |
| 201 |
static std::vector<LinkBaseExtension::PropInfo> PropsInfo; |
| 202 |
if (PropsInfo.empty()) { |
| 203 |
BOOST_PP_SEQ_FOR_EACH(LINK_PROP_INFO, PropsInfo, LINK_PARAMS); |
| 204 |
} |
| 205 |
return PropsInfo; |
| 206 |
} |
| 207 |
|
| 208 |
const LinkBaseExtension::PropInfoMap& LinkBaseExtension::getPropertyInfoMap() const |
| 209 |
{ |
| 210 |
static PropInfoMap PropsMap; |
| 211 |
if (PropsMap.empty()) { |
| 212 |
const auto& infos = getPropertyInfo(); |
| 213 |
for (const auto& info : infos) { |
| 214 |
PropsMap[info.name] = info; |
| 215 |
} |
| 216 |
} |
| 217 |
return PropsMap; |
| 218 |
} |
| 219 |
|
| 220 |
Property* LinkBaseExtension::getProperty(int idx) |
| 221 |
{ |
| 222 |
if (idx >= 0 && idx < (int)props.size()) { |
| 223 |
return props[idx]; |
| 224 |
} |
| 225 |
return nullptr; |
| 226 |
} |
| 227 |
|
| 228 |
Property* LinkBaseExtension::getProperty(const char* name) |
| 229 |
{ |
| 230 |
const auto& info = getPropertyInfoMap(); |
| 231 |
auto it = info.find(name); |
| 232 |
if (it == info.end()) { |
| 233 |
return nullptr; |
| 234 |
} |
| 235 |
return getProperty(it->second.index); |
| 236 |
} |
| 237 |
|
| 238 |
void LinkBaseExtension::setProperty(int idx, Property* prop) |
| 239 |
{ |
| 240 |
const auto& infos = getPropertyInfo(); |
| 241 |
if (idx < 0 || idx >= (int)infos.size()) { |
| 242 |
LINK_THROW(Base::RuntimeError, "App::LinkBaseExtension: property index out of range"); |
| 243 |
} |
| 244 |
|
| 245 |
if (props[idx]) { |
| 246 |
props[idx]->setStatus(Property::LockDynamic, false); |
| 247 |
props[idx] = nullptr; |
| 248 |
} |
| 249 |
if (!prop) { |
| 250 |
return; |
| 251 |
} |
| 252 |
if (!prop->isDerivedFrom(infos[idx].type)) { |
| 253 |
std::ostringstream str; |
| 254 |
str << "App::LinkBaseExtension: expected property type '" << infos[idx].type.getName() |
| 255 |
<< "', instead of '" << prop->getClassTypeId().getName() << "'"; |
| 256 |
LINK_THROW(Base::TypeError, str.str().c_str()); |
| 257 |
} |
| 258 |
|
| 259 |
props[idx] = prop; |
| 260 |
props[idx]->setStatus(Property::LockDynamic, true); |
| 261 |
|
| 262 |
switch (idx) { |
| 263 |
case PropLinkMode: { |
| 264 |
static const char* linkModeEnums[] = {"None", |
| 265 |
"Auto Delete", |
| 266 |
"Auto Link", |
| 267 |
"Auto Unlink", |
| 268 |
nullptr}; |
| 269 |
auto propLinkMode = static_cast<PropertyEnumeration*>(prop); |
| 270 |
if (!propLinkMode->hasEnums()) { |
| 271 |
propLinkMode->setEnums(linkModeEnums); |
| 272 |
} |
| 273 |
break; |
| 274 |
} |
| 275 |
case PropLinkCopyOnChange: { |
| 276 |
static const char* enums[] = {"Disabled", "Enabled", "Owned", "Tracking", nullptr}; |
| 277 |
auto propEnum = static_cast<PropertyEnumeration*>(prop); |
| 278 |
if (!propEnum->hasEnums()) { |
| 279 |
propEnum->setEnums(enums); |
| 280 |
} |
| 281 |
break; |
| 282 |
} |
| 283 |
case PropLinkCopyOnChangeSource: |
| 284 |
case PropLinkCopyOnChangeGroup: |
| 285 |
if (auto linkProp = freecad_cast<PropertyLinkBase*>(prop)) { |
| 286 |
linkProp->setScope(LinkScope::Global); |
| 287 |
} |
| 288 |
// fall through |
| 289 |
case PropLinkCopyOnChangeTouched: |
| 290 |
prop->setStatus(Property::Hidden, true); |
| 291 |
break; |
| 292 |
case PropLinkTransform: |
| 293 |
case PropLinkPlacement: |
| 294 |
case PropPlacement: |
| 295 |
if (getLinkTransformProperty() && getLinkPlacementProperty() |
| 296 |
&& getPlacementProperty()) { |
| 297 |
bool transform = getLinkTransformValue(); |
| 298 |
getPlacementProperty()->setStatus(Property::Hidden, transform); |
| 299 |
getLinkPlacementProperty()->setStatus(Property::Hidden, !transform); |
| 300 |
} |
| 301 |
break; |
| 302 |
case PropElementList: |
| 303 |
getElementListProperty()->setScope(LinkScope::Global); |
| 304 |
getElementListProperty()->setStatus(Property::Hidden, true); |
| 305 |
// fall through |
| 306 |
case PropLinkedObject: |
| 307 |
// Make ElementList as read-only if we are not a group (i.e. having |
| 308 |
// LinkedObject property), because it is for holding array elements. |
| 309 |
if (getElementListProperty()) { |
| 310 |
getElementListProperty()->setStatus(Property::Immutable, |
| 311 |
getLinkedObjectProperty() != nullptr); |
| 312 |
} |
| 313 |
if (auto linkProp = getLinkedObjectProperty()) { |
| 314 |
linkProp->setScope(LinkScope::Global); |
| 315 |
} |
| 316 |
break; |
| 317 |
case PropVisibilityList: |
| 318 |
getVisibilityListProperty()->setStatus(Property::Immutable, true); |
| 319 |
getVisibilityListProperty()->setStatus(Property::Hidden, true); |
| 320 |
break; |
| 321 |
} |
| 322 |
|
| 323 |
if (FC_LOG_INSTANCE.isEnabled(FC_LOGLEVEL_TRACE)) { |
| 324 |
const char* propName; |
| 325 |
if (!prop) { |
| 326 |
propName = "<null>"; |
| 327 |
} |
| 328 |
else { |
| 329 |
propName = prop->getName(); |
| 330 |
} |
| 331 |
if (!Property::isValidName(propName)) { |
| 332 |
propName = "?"; |
| 333 |
} |
| 334 |
FC_TRACE("set property " << infos[idx].name << ": " << propName); |
| 335 |
} |
| 336 |
} |
| 337 |
|
| 338 |
static const char _GroupPrefix[] = "Configuration ("; |
| 339 |
|
| 340 |
App::DocumentObjectExecReturn* LinkBaseExtension::extensionExecute() |
| 341 |
{ |
| 342 |
// The actual value of LinkTouched is not important, just to notify view |
| 343 |
// provider that the link (in fact, its dependents, i.e. linked ones) have |
| 344 |
// recomputed. |
| 345 |
_LinkTouched.touch(); |
| 346 |
|
| 347 |
if (getLinkedObjectProperty()) { |
| 348 |
DocumentObject* linked = getTrueLinkedObject(true); |
| 349 |
if (!linked) { |
| 350 |
std::ostringstream ss; |
| 351 |
ss << "Link broken!"; |
| 352 |
auto xlink = freecad_cast<PropertyXLink*>(getLinkedObjectProperty()); |
| 353 |
if (xlink) { |
| 354 |
const char* objname = xlink->getObjectName(); |
| 355 |
if (!Base::Tools::isNullOrEmpty(objname)) { |
| 356 |
ss << "\nObject: " << objname; |
| 357 |
} |
| 358 |
const char* filename = xlink->getFilePath(); |
| 359 |
if (!Base::Tools::isNullOrEmpty(filename)) { |
| 360 |
ss << "\nFile: " << filename; |
| 361 |
} |
| 362 |
} |
| 363 |
return new App::DocumentObjectExecReturn(ss.str().c_str()); |
| 364 |
} |
| 365 |
|
| 366 |
App::DocumentObject* container = getContainer(); |
| 367 |
auto source = getLinkCopyOnChangeSourceValue(); |
| 368 |
if (source && getLinkCopyOnChangeValue() == CopyOnChangeTracking |
| 369 |
&& getLinkCopyOnChangeTouchedValue()) { |
| 370 |
syncCopyOnChange(); |
| 371 |
} |
| 372 |
|
| 373 |
// the previous linked object could be deleted by syncCopyOnChange - #12281 |
| 374 |
linked = getTrueLinkedObject(true); |
| 375 |
if (!linked) { |
| 376 |
return new App::DocumentObjectExecReturn("Error in processing variable link"); |
| 377 |
} |
| 378 |
|
| 379 |
PropertyPythonObject* proxy = nullptr; |
| 380 |
if (getLinkExecuteProperty() && !boost::iequals(getLinkExecuteValue(), "none") |
| 381 |
&& (!_LinkOwner.getValue() |
| 382 |
|| !container->getDocument()->getObjectByID(_LinkOwner.getValue()))) { |
| 383 |
// Check if this is an element link. Do not invoke appLinkExecute() |
| 384 |
// if so, because it will be called from the link array. |
| 385 |
proxy = freecad_cast<PropertyPythonObject*>( |
| 386 |
linked->getPropertyByName("Proxy")); |
| 387 |
} |
| 388 |
if (proxy) { |
| 389 |
Base::PyGILStateLocker lock; |
| 390 |
const char* errMsg = "Linked proxy execute failed"; |
| 391 |
try { |
| 392 |
Py::Tuple args(3); |
| 393 |
Py::Object proxyValue = proxy->getValue(); |
| 394 |
const char* method = getLinkExecuteValue(); |
| 395 |
if (!method || !method[0]) { |
| 396 |
method = "appLinkExecute"; |
| 397 |
} |
| 398 |
if (proxyValue.hasAttr(method)) { |
| 399 |
Py::Object attr = proxyValue.getAttr(method); |
| 400 |
if (attr.ptr() && attr.isCallable()) { |
| 401 |
Py::Tuple args(4); |
| 402 |
args.setItem(0, Py::asObject(linked->getPyObject())); |
| 403 |
args.setItem(1, Py::asObject(container->getPyObject())); |
| 404 |
if (!_getElementCountValue()) { |
| 405 |
Py::Callable(attr).apply(args); |
| 406 |
} |
| 407 |
else { |
| 408 |
const auto& elements = _getElementListValue(); |
| 409 |
for (int i = 0; i < _getElementCountValue(); ++i) { |
| 410 |
args.setItem(2, Py::Long(i)); |
| 411 |
if (i < (int)elements.size()) { |
| 412 |
args.setItem(3, Py::asObject(elements[i]->getPyObject())); |
| 413 |
} |
| 414 |
else { |
| 415 |
args.setItem(3, Py::Object()); |
| 416 |
} |
| 417 |
Py::Callable(attr).apply(args); |
| 418 |
} |
| 419 |
} |
| 420 |
} |
| 421 |
} |
| 422 |
} |
| 423 |
catch (Py::Exception&) { |
| 424 |
Base::PyException e; |
| 425 |
e.reportException(); |
| 426 |
return new App::DocumentObjectExecReturn(errMsg); |
| 427 |
} |
| 428 |
catch (Base::Exception& e) { |
| 429 |
e.reportException(); |
| 430 |
return new App::DocumentObjectExecReturn(errMsg); |
| 431 |
} |
| 432 |
} |
| 433 |
|
| 434 |
auto parent = getContainer(); |
| 435 |
setupCopyOnChange(parent); |
| 436 |
|
| 437 |
if (hasCopyOnChange && getLinkCopyOnChangeValue() == CopyOnChangeDisabled) { |
| 438 |
hasCopyOnChange = false; |
| 439 |
std::vector<Property*> props; |
| 440 |
parent->getPropertyList(props); |
| 441 |
for (auto prop : props) { |
| 442 |
if (isCopyOnChangeProperty(parent, *prop)) { |
| 443 |
try { |
| 444 |
parent->removeDynamicProperty(prop->getName()); |
| 445 |
} |
| 446 |
catch (Base::Exception& e) { |
| 447 |
e.reportException(); |
| 448 |
} |
| 449 |
catch (...) { |
| 450 |
} |
| 451 |
} |
| 452 |
} |
| 453 |
} |
| 454 |
} |
| 455 |
return inherited::extensionExecute(); |
| 456 |
} |
| 457 |
|
| 458 |
short LinkBaseExtension::extensionMustExecute() |
| 459 |
{ |
| 460 |
auto link = getLink(); |
| 461 |
if (!link) { |
| 462 |
return 0; |
| 463 |
} |
| 464 |
return link->mustExecute(); |
| 465 |
} |
| 466 |
|
| 467 |
std::vector<App::DocumentObject*> |
| 468 |
LinkBaseExtension::getOnChangeCopyObjects(std::vector<App::DocumentObject*>* excludes, |
| 469 |
App::DocumentObject* src) |
| 470 |
{ |
| 471 |
auto parent = getContainer(); |
| 472 |
if (!src) { |
| 473 |
src = getLinkCopyOnChangeSourceValue(); |
| 474 |
} |
| 475 |
if (!src || getLinkCopyOnChangeValue() == CopyOnChangeDisabled) { |
| 476 |
return {}; |
| 477 |
} |
| 478 |
|
| 479 |
auto res = Document::getDependencyList({src}, Document::DepSort); |
| 480 |
for (auto it = res.begin(); it != res.end();) { |
| 481 |
auto obj = *it; |
| 482 |
if (obj == src) { |
| 483 |
++it; |
| 484 |
continue; |
| 485 |
} |
| 486 |
auto prop = |
| 487 |
freecad_cast<PropertyMap*>(obj->getPropertyByName("_CopyOnChangeControl")); |
| 488 |
static std::map<std::string, std::string> dummy; |
| 489 |
const auto& map = prop && prop->getContainer() == obj ? prop->getValues() : dummy; |
| 490 |
const char* v = ""; |
| 491 |
if (src->getDocument() != obj->getDocument()) { |
| 492 |
v = "-"; |
| 493 |
} |
| 494 |
auto iter = map.find("*"); |
| 495 |
if (iter != map.end()) { |
| 496 |
v = iter->second.c_str(); |
| 497 |
} |
| 498 |
else if ((iter = map.find(parent->getNameInDocument())) != map.end()) { |
| 499 |
v = iter->second.c_str(); |
| 500 |
} |
| 501 |
if (boost::equals(v, "-")) { |
| 502 |
if (excludes) { |
| 503 |
excludes->push_back(obj); |
| 504 |
} |
| 505 |
else { |
| 506 |
it = res.erase(it); |
| 507 |
continue; |
| 508 |
} |
| 509 |
} |
| 510 |
++it; |
| 511 |
} |
| 512 |
return res; |
| 513 |
} |
| 514 |
|
| 515 |
void LinkBaseExtension::setOnChangeCopyObject(App::DocumentObject* obj, OnChangeCopyOptions options) |
| 516 |
{ |
| 517 |
auto parent = getContainer(); |
| 518 |
Base::Flags<OnChangeCopyOptions> flags(options); |
| 519 |
bool exclude = flags.testFlag(OnChangeCopyOptions::Exclude); |
| 520 |
bool external = parent->getDocument() != obj->getDocument(); |
| 521 |
auto prop = |
| 522 |
freecad_cast<PropertyMap*>(obj->getPropertyByName("_CopyOnChangeControl")); |
| 523 |
|
| 524 |
if (external == exclude && !prop) { |
| 525 |
return; |
| 526 |
} |
| 527 |
|
| 528 |
if (!prop) { |
| 529 |
try { |
| 530 |
prop = static_cast<PropertyMap*>( |
| 531 |
obj->addDynamicProperty("App::PropertyMap", "_CopyOnChangeControl")); |
| 532 |
} |
| 533 |
catch (Base::Exception& e) { |
| 534 |
e.reportException(); |
| 535 |
} |
| 536 |
if (!prop) { |
| 537 |
FC_ERR("Failed to setup copy on change object " << obj->getFullName()); |
| 538 |
return; |
| 539 |
} |
| 540 |
} |
| 541 |
|
| 542 |
const char* key = flags.testFlag(OnChangeCopyOptions::ApplyAll) ? "*" : parent->getDagKey(); |
| 543 |
if (external) { |
| 544 |
prop->setValue(key, exclude ? "" : "+"); |
| 545 |
} |
| 546 |
else { |
| 547 |
prop->setValue(key, exclude ? "-" : ""); |
| 548 |
} |
| 549 |
} |
| 550 |
|
| 551 |
// The purpose of this function is to synchronize the mutated copy to the |
| 552 |
// original linked CopyOnChange object. It will make a new copy if any of the |
| 553 |
// non-CopyOnChange property of the original object has changed. |
| 554 |
void LinkBaseExtension::syncCopyOnChange() |
| 555 |
{ |
| 556 |
if (!getLinkCopyOnChangeValue()) { |
| 557 |
return; |
| 558 |
} |
| 559 |
auto linkProp = getLinkedObjectProperty(); |
| 560 |
auto srcProp = getLinkCopyOnChangeSourceProperty(); |
| 561 |
auto srcTouched = getLinkCopyOnChangeTouchedProperty(); |
| 562 |
if (!linkProp || !srcProp || !srcTouched || !srcProp->getValue() || !linkProp->getValue() |
| 563 |
|| srcProp->getValue() == linkProp->getValue()) { |
| 564 |
return; |
| 565 |
} |
| 566 |
|
| 567 |
auto parent = getContainer(); |
| 568 |
|
| 569 |
auto linked = linkProp->getValue(); |
| 570 |
|
| 571 |
std::vector<App::DocumentObjectT> oldObjs; |
| 572 |
std::vector<App::DocumentObject*> objs; |
| 573 |
|
| 574 |
// CopyOnChangeGroup is a hidden dynamic property for holding a LinkGroup |
| 575 |
// for holding the mutated copy of the original linked object and all its |
| 576 |
// dependencies. |
| 577 |
LinkGroup* copyOnChangeGroup = nullptr; |
| 578 |
if (auto prop = getLinkCopyOnChangeGroupProperty()) { |
| 579 |
copyOnChangeGroup = freecad_cast<LinkGroup*>(prop->getValue()); |
| 580 |
if (!copyOnChangeGroup) { |
| 581 |
// Create the LinkGroup if not exist |
| 582 |
auto group = new LinkGroup; |
| 583 |
group->LinkMode.setValue(LinkModeAutoDelete); |
| 584 |
parent->getDocument()->addObject(group, "CopyOnChangeGroup"); |
| 585 |
prop->setValue(group); |
| 586 |
} |
| 587 |
else { |
| 588 |
// If it exists, then obtain all copied objects. Note that we stores |
| 589 |
// the dynamic property _SourceUUID in oldObjs if possible, in order |
| 590 |
// to match the possible new copy later. |
| 591 |
objs = copyOnChangeGroup->ElementList.getValues(); |
| 592 |
for (auto obj : objs) { |
| 593 |
if (!obj->isAttachedToDocument()) { |
| 594 |
continue; |
| 595 |
} |
| 596 |
auto prop = |
| 597 |
freecad_cast<PropertyUUID*>(obj->getPropertyByName("_SourceUUID")); |
| 598 |
if (prop && prop->getContainer() == obj) { |
| 599 |
oldObjs.emplace_back(prop); |
| 600 |
} |
| 601 |
else { |
| 602 |
oldObjs.emplace_back(obj); |
| 603 |
} |
| 604 |
} |
| 605 |
std::sort(objs.begin(), objs.end()); |
| 606 |
} |
| 607 |
} |
| 608 |
|
| 609 |
// Obtain the original linked object and its dependency in depending order. |
| 610 |
// The last being the original linked object. |
| 611 |
auto srcObjs = getOnChangeCopyObjects(); |
| 612 |
// Refresh signal connection to monitor changes |
| 613 |
monitorOnChangeCopyObjects(srcObjs); |
| 614 |
|
| 615 |
// Copy the objects. Document::export/importObjects() (called by |
| 616 |
// copyObject()) will generate a _ObjectUUID for each source object and |
| 617 |
// match it with a _SourceUUID in the copy. |
| 618 |
auto copiedObjs = parent->getDocument()->copyObject(srcObjs); |
| 619 |
if (copiedObjs.empty()) { |
| 620 |
return; |
| 621 |
} |
| 622 |
|
| 623 |
// copyObject() will return copy in order of the same order of the input, |
| 624 |
// so the last object will be the copy of the original linked object |
| 625 |
auto newLinked = copiedObjs.back(); |
| 626 |
|
| 627 |
// We are copying from the original linked object and we've already mutated |
| 628 |
// it, so we need to copy all CopyOnChange properties from the mutated |
| 629 |
// object to the new copy. |
| 630 |
std::vector<App::Property*> propList; |
| 631 |
linked->getPropertyList(propList); |
| 632 |
for (auto prop : propList) { |
| 633 |
if (!prop->testStatus(Property::CopyOnChange) || prop->getContainer() != linked) { |
| 634 |
continue; |
| 635 |
} |
| 636 |
auto p = newLinked->getPropertyByName(prop->getName()); |
| 637 |
if (p && p->getTypeId() == prop->getTypeId()) { |
| 638 |
std::unique_ptr<Property> pCopy(prop->Copy()); |
| 639 |
p->Paste(*pCopy); |
| 640 |
} |
| 641 |
} |
| 642 |
|
| 643 |
if (copyOnChangeGroup) { |
| 644 |
// The order of the copied objects is in dependency order (because of |
| 645 |
// getOnChangeCopyObjects()). We reverse it here so that we can later |
| 646 |
// on delete it in reverse order to avoid error (because some parent |
| 647 |
// objects may want to delete their own children). |
| 648 |
std::reverse(copiedObjs.begin(), copiedObjs.end()); |
| 649 |
copyOnChangeGroup->ElementList.setValues(copiedObjs); |
| 650 |
} |
| 651 |
|
| 652 |
// Create a map to find the corresponding replacement of the new copies to |
| 653 |
// the mutated object. The reason for doing so is that we are copying from |
| 654 |
// the original linked object and its dependency, not the mutated objects |
| 655 |
// which are old copies. There could be arbitrary changes in the originals |
| 656 |
// which may add or remove or change depending orders, while the |
| 657 |
// replacement happens between the new and old copies. |
| 658 |
|
| 659 |
std::map<Base::Uuid, App::DocumentObjectT> newObjs; |
| 660 |
for (auto obj : copiedObjs) { |
| 661 |
auto prop = freecad_cast<PropertyUUID*>(obj->getPropertyByName("_SourceUUID")); |
| 662 |
if (prop) { |
| 663 |
newObjs.insert(std::make_pair(prop->getValue(), obj)); |
| 664 |
} |
| 665 |
} |
| 666 |
|
| 667 |
std::vector<std::pair<App::DocumentObject*, App::DocumentObject*>> replacements; |
| 668 |
for (const auto& objT : oldObjs) { |
| 669 |
auto prop = freecad_cast<PropertyUUID*>(objT.getProperty()); |
| 670 |
if (!prop) { |
| 671 |
continue; |
| 672 |
} |
| 673 |
auto it = newObjs.find(prop->getValue()); |
| 674 |
if (it == newObjs.end()) { |
| 675 |
continue; |
| 676 |
} |
| 677 |
auto oldObj = objT.getObject(); |
| 678 |
auto newObj = it->second.getObject(); |
| 679 |
if (oldObj && newObj) { |
| 680 |
replacements.emplace_back(oldObj, newObj); |
| 681 |
} |
| 682 |
} |
| 683 |
|
| 684 |
std::vector<std::pair<App::DocumentObjectT, std::unique_ptr<App::Property>>> propChanges; |
| 685 |
if (!replacements.empty()) { |
| 686 |
std::sort(copiedObjs.begin(), copiedObjs.end()); |
| 687 |
|
| 688 |
// Global search for links affected by the replacement. We accumulate |
| 689 |
// the changes in propChanges without applying, in order to avoid any |
| 690 |
// side effect of changing while searching. |
| 691 |
for (auto doc : App::GetApplication().getDocuments()) { |
| 692 |
for (auto o : doc->getObjects()) { |
| 693 |
if (o == parent || std::binary_search(objs.begin(), objs.end(), o) |
| 694 |
|| std::binary_search(copiedObjs.begin(), copiedObjs.end(), o)) { |
| 695 |
continue; |
| 696 |
} |
| 697 |
propList.clear(); |
| 698 |
o->getPropertyList(propList); |
| 699 |
for (auto prop : propList) { |
| 700 |
if (prop->getContainer() != o) { |
| 701 |
continue; |
| 702 |
} |
| 703 |
auto linkProp = freecad_cast<App::PropertyLinkBase*>(prop); |
| 704 |
if (!linkProp) { |
| 705 |
continue; |
| 706 |
} |
| 707 |
for (const auto& v : replacements) { |
| 708 |
std::unique_ptr<App::Property> copy( |
| 709 |
linkProp->CopyOnLinkReplace(parent, v.first, v.second)); |
| 710 |
if (!copy) { |
| 711 |
continue; |
| 712 |
} |
| 713 |
propChanges.emplace_back(App::DocumentObjectT(prop), std::move(copy)); |
| 714 |
} |
| 715 |
} |
| 716 |
} |
| 717 |
} |
| 718 |
} |
| 719 |
|
| 720 |
Base::StateLocker guard(pauseCopyOnChange); |
| 721 |
linkProp->setValue(newLinked); |
| 722 |
newLinked->Visibility.setValue(false); |
| 723 |
srcTouched->setValue(false); |
| 724 |
|
| 725 |
// Apply the global link changes. |
| 726 |
for (const auto& v : propChanges) { |
| 727 |
auto prop = v.first.getProperty(); |
| 728 |
if (prop) { |
| 729 |
prop->Paste(*v.second.get()); |
| 730 |
} |
| 731 |
} |
| 732 |
|
| 733 |
// Finally, remove all old copies. oldObjs stores type of DocumentObjectT |
| 734 |
// which stores the object name. Before removing, we need to make sure the |
| 735 |
// object exists, and it is not some new object with the same name, by |
| 736 |
// checking objs which stores DocumentObject pointer. |
| 737 |
for (const auto& objT : oldObjs) { |
| 738 |
auto obj = objT.getObject(); |
| 739 |
if (obj && std::binary_search(objs.begin(), objs.end(), obj)) { |
| 740 |
obj->getDocument()->removeObject(obj->getNameInDocument()); |
| 741 |
} |
| 742 |
} |
| 743 |
} |
| 744 |
|
| 745 |
bool LinkBaseExtension::isLinkedToConfigurableObject() const |
| 746 |
{ |
| 747 |
if (auto linked = getLinkedObjectValue()) { |
| 748 |
std::vector<App::Property*> propList; |
| 749 |
linked->getPropertyList(propList); |
| 750 |
for (auto prop : propList) { |
| 751 |
if (prop->testStatus(Property::CopyOnChange) && prop->getContainer() == linked) { |
| 752 |
return true; |
| 753 |
} |
| 754 |
} |
| 755 |
} |
| 756 |
return false; |
| 757 |
} |
| 758 |
|
| 759 |
bool LinkBaseExtension::isCopyOnChangeProperty(DocumentObject* obj, const App::Property& prop) |
| 760 |
{ |
| 761 |
if (obj != prop.getContainer() || !prop.testStatus(App::Property::PropDynamic)) { |
| 762 |
return false; |
| 763 |
} |
| 764 |
auto group = prop.getGroup(); |
| 765 |
return group && boost::starts_with(group, _GroupPrefix); |
| 766 |
} |
| 767 |
|
| 768 |
void LinkBaseExtension::setupCopyOnChange(DocumentObject* parent, bool checkSource) |
| 769 |
{ |
| 770 |
copyOnChangeConns.clear(); |
| 771 |
copyOnChangeSrcConns.clear(); |
| 772 |
|
| 773 |
auto linked = getTrueLinkedObject(false); |
| 774 |
if (!linked || getLinkCopyOnChangeValue() == CopyOnChangeDisabled) { |
| 775 |
return; |
| 776 |
} |
| 777 |
|
| 778 |
if (checkSource && !pauseCopyOnChange) { |
| 779 |
PropertyLink* source = getLinkCopyOnChangeSourceProperty(); |
| 780 |
if (source) { |
| 781 |
source->setValue(linked); |
| 782 |
if (auto touched = getLinkCopyOnChangeTouchedProperty()) { |
| 783 |
touched->setValue(false); |
| 784 |
} |
| 785 |
} |
| 786 |
} |
| 787 |
|
| 788 |
hasCopyOnChange = setupCopyOnChange(parent, linked, ©OnChangeConns, hasCopyOnChange); |
| 789 |
if (hasCopyOnChange && getLinkCopyOnChangeValue() == CopyOnChangeOwned && getLinkedObjectValue() |
| 790 |
&& getLinkedObjectValue() == getLinkCopyOnChangeSourceValue()) { |
| 791 |
makeCopyOnChange(); |
| 792 |
} |
| 793 |
} |
| 794 |
|
| 795 |
bool LinkBaseExtension::setupCopyOnChange( |
| 796 |
DocumentObject* parent, |
| 797 |
DocumentObject* linked, |
| 798 |
std::vector<boost::signals2::scoped_connection>* copyOnChangeConns, |
| 799 |
bool checkExisting) |
| 800 |
{ |
| 801 |
if (!parent || !linked) { |
| 802 |
return false; |
| 803 |
} |
| 804 |
|
| 805 |
bool res = false; |
| 806 |
|
| 807 |
std::unordered_map<Property*, Property*> newProps; |
| 808 |
std::vector<Property*> props; |
| 809 |
linked->getPropertyList(props); |
| 810 |
for (auto prop : props) { |
| 811 |
if (!prop->testStatus(Property::CopyOnChange) || prop->getContainer() != linked) { |
| 812 |
continue; |
| 813 |
} |
| 814 |
|
| 815 |
res = true; |
| 816 |
|
| 817 |
const char* linkedGroupName = prop->getGroup(); |
| 818 |
if (!linkedGroupName || !linkedGroupName[0]) { |
| 819 |
linkedGroupName = "Base"; |
| 820 |
} |
| 821 |
|
| 822 |
std::string groupName; |
| 823 |
groupName = _GroupPrefix; |
| 824 |
if (boost::starts_with(linkedGroupName, _GroupPrefix)) { |
| 825 |
groupName += linkedGroupName + sizeof(_GroupPrefix) - 1; |
| 826 |
} |
| 827 |
else { |
| 828 |
groupName += linkedGroupName; |
| 829 |
groupName += ")"; |
| 830 |
} |
| 831 |
|
| 832 |
auto p = parent->getPropertyByName(prop->getName()); |
| 833 |
if (p) { |
| 834 |
if (p->getContainer() != parent) { |
| 835 |
p = nullptr; |
| 836 |
} |
| 837 |
else { |
| 838 |
const char* otherGroupName = p->getGroup(); |
| 839 |
if (!otherGroupName || !boost::starts_with(otherGroupName, _GroupPrefix)) { |
| 840 |
FC_WARN(p->getFullName() |
| 841 |
<< " shadows another CopyOnChange property " << prop->getFullName()); |
| 842 |
continue; |
| 843 |
} |
| 844 |
if (p->getTypeId() != prop->getTypeId() || groupName != otherGroupName) { |
| 845 |
parent->removeDynamicProperty(p->getName()); |
| 846 |
p = nullptr; |
| 847 |
} |
| 848 |
} |
| 849 |
} |
| 850 |
if (!p) { |
| 851 |
p = parent->addDynamicProperty(prop->getTypeId().getName(), |
| 852 |
prop->getName(), |
| 853 |
groupName.c_str(), |
| 854 |
prop->getDocumentation()); |
| 855 |
std::unique_ptr<Property> pcopy(prop->Copy()); |
| 856 |
Base::ObjectStatusLocker<Property::Status, Property> guard(Property::User3, p); |
| 857 |
if (pcopy) { |
| 858 |
p->Paste(*pcopy); |
| 859 |
} |
| 860 |
p->setStatusValue(prop->getStatus()); |
| 861 |
} |
| 862 |
newProps[p] = prop; |
| 863 |
} |
| 864 |
|
| 865 |
if (checkExisting) { |
| 866 |
props.clear(); |
| 867 |
parent->getPropertyList(props); |
| 868 |
for (auto prop : props) { |
| 869 |
if (prop->getContainer() != parent) { |
| 870 |
continue; |
| 871 |
} |
| 872 |
auto gname = prop->getGroup(); |
| 873 |
if (!gname || !boost::starts_with(gname, _GroupPrefix)) { |
| 874 |
continue; |
| 875 |
} |
| 876 |
if (!newProps.contains(prop)) { |
| 877 |
parent->removeDynamicProperty(prop->getName()); |
| 878 |
} |
| 879 |
} |
| 880 |
} |
| 881 |
|
| 882 |
if (!copyOnChangeConns) { |
| 883 |
return res; |
| 884 |
} |
| 885 |
|
| 886 |
for (const auto& v : newProps) { |
| 887 |
// sync configuration properties |
| 888 |
copyOnChangeConns->push_back( |
| 889 |
v.second->signalChanged.connect([parent](const Property& prop) { |
| 890 |
if (!prop.testStatus(Property::CopyOnChange)) { |
| 891 |
return; |
| 892 |
} |
| 893 |
auto p = parent->getPropertyByName(prop.getName()); |
| 894 |
if (p && p->getTypeId() == prop.getTypeId()) { |
| 895 |
std::unique_ptr<Property> pcopy(prop.Copy()); |
| 896 |
// temperoray set Output to prevent touching |
| 897 |
p->setStatus(Property::Output, true); |
| 898 |
// temperoray block copy on change |
| 899 |
Base::ObjectStatusLocker<Property::Status, Property> guard(Property::User3, p); |
| 900 |
if (pcopy) { |
| 901 |
p->Paste(*pcopy); |
| 902 |
} |
| 903 |
p->setStatusValue(prop.getStatus()); |
| 904 |
} |
| 905 |
})); |
| 906 |
} |
| 907 |
|
| 908 |
return res; |
| 909 |
} |
| 910 |
|
| 911 |
void LinkBaseExtension::checkCopyOnChange(App::DocumentObject* parent, const App::Property& prop) |
| 912 |
{ |
| 913 |
if (!parent || !parent->getDocument() || parent->getDocument()->isPerformingTransaction()) { |
| 914 |
return; |
| 915 |
} |
| 916 |
|
| 917 |
auto linked = getLinkedObjectValue(); |
| 918 |
if (!linked || getLinkCopyOnChangeValue() == CopyOnChangeDisabled |
| 919 |
|| !isCopyOnChangeProperty(parent, prop)) { |
| 920 |
return; |
| 921 |
} |
| 922 |
|
| 923 |
if (getLinkCopyOnChangeValue() == CopyOnChangeOwned |
| 924 |
|| (getLinkCopyOnChangeValue() == CopyOnChangeTracking |
| 925 |
&& linked != getLinkCopyOnChangeSourceValue())) { |
| 926 |
auto p = linked->getPropertyByName(prop.getName()); |
| 927 |
if (p && p->getTypeId() == prop.getTypeId()) { |
| 928 |
std::unique_ptr<Property> pcopy(prop.Copy()); |
| 929 |
if (pcopy) { |
| 930 |
p->Paste(*pcopy); |
| 931 |
} |
| 932 |
} |
| 933 |
return; |
| 934 |
} |
| 935 |
|
| 936 |
auto linkedProp = linked->getPropertyByName(prop.getName()); |
| 937 |
if (!linkedProp || linkedProp->getTypeId() != prop.getTypeId() || linkedProp->isSame(prop)) { |
| 938 |
return; |
| 939 |
} |
| 940 |
|
| 941 |
auto copied = makeCopyOnChange(); |
| 942 |
if (copied) { |
| 943 |
linkedProp = copied->getPropertyByName(prop.getName()); |
| 944 |
if (linkedProp && linkedProp->getTypeId() == prop.getTypeId()) { |
| 945 |
std::unique_ptr<Property> pcopy(prop.Copy()); |
| 946 |
if (pcopy) { |
| 947 |
linkedProp->Paste(*pcopy); |
| 948 |
} |
| 949 |
} |
| 950 |
} |
| 951 |
} |
| 952 |
|
| 953 |
App::DocumentObject* LinkBaseExtension::makeCopyOnChange() |
| 954 |
{ |
| 955 |
auto linked = getLinkedObjectValue(); |
| 956 |
if (pauseCopyOnChange || !linked) { |
| 957 |
return nullptr; |
| 958 |
} |
| 959 |
auto parent = getContainer(); |
| 960 |
auto srcobjs = getOnChangeCopyObjects(nullptr, linked); |
| 961 |
for (auto obj : srcobjs) { |
| 962 |
if (obj->testStatus(App::PartialObject)) { |
| 963 |
FC_THROWM(Base::RuntimeError, |
| 964 |
"Cannot copy partial loaded object: " << obj->getFullName()); |
| 965 |
} |
| 966 |
} |
| 967 |
auto objs = parent->getDocument()->copyObject(srcobjs); |
| 968 |
if (objs.empty()) { |
| 969 |
return nullptr; |
| 970 |
} |
| 971 |
|
| 972 |
monitorOnChangeCopyObjects(srcobjs); |
| 973 |
|
| 974 |
linked = objs.back(); |
| 975 |
linked->Visibility.setValue(false); |
| 976 |
|
| 977 |
Base::StateLocker guard(pauseCopyOnChange); |
| 978 |
getLinkedObjectProperty()->setValue(linked); |
| 979 |
if (getLinkCopyOnChangeValue() == CopyOnChangeEnabled) { |
| 980 |
getLinkCopyOnChangeProperty()->setValue(CopyOnChangeOwned); |
| 981 |
} |
| 982 |
|
| 983 |
if (auto prop = getLinkCopyOnChangeGroupProperty()) { |
| 984 |
if (auto obj = prop->getValue()) { |
| 985 |
if (obj->isAttachedToDocument() && obj->getDocument()) { |
| 986 |
obj->getDocument()->removeObject(obj->getNameInDocument()); |
| 987 |
} |
| 988 |
} |
| 989 |
auto group = new LinkGroup; |
| 990 |
group->LinkMode.setValue(LinkModeAutoDelete); |
| 991 |
getContainer()->getDocument()->addObject(group, "CopyOnChangeGroup"); |
| 992 |
prop->setValue(group); |
| 993 |
|
| 994 |
// The order of the copied objects is in dependency order (because of |
| 995 |
// getOnChangeCopyObjects()). We reverse it here so that we can later |
| 996 |
// on delete it in reverse order to avoid error (because some parent |
| 997 |
// objects may want to delete their own children). |
| 998 |
std::reverse(objs.begin(), objs.end()); |
| 999 |
group->ElementList.setValues(objs); |
| 1000 |
} |
| 1001 |
|
| 1002 |
return linked; |
| 1003 |
} |
| 1004 |
|
| 1005 |
void LinkBaseExtension::monitorOnChangeCopyObjects(const std::vector<App::DocumentObject*>& objs) |
| 1006 |
{ |
| 1007 |
copyOnChangeSrcConns.clear(); |
| 1008 |
if (getLinkCopyOnChangeValue() == CopyOnChangeDisabled) { |
| 1009 |
return; |
| 1010 |
} |
| 1011 |
for (auto obj : objs) { |
| 1012 |
obj->setStatus(App::ObjectStatus::TouchOnColorChange, true); |
| 1013 |
copyOnChangeSrcConns.emplace_back( |
| 1014 |
obj->signalChanged.connect([this](const DocumentObject&, const Property&) { |
| 1015 |
if (auto prop = this->getLinkCopyOnChangeTouchedProperty()) { |
| 1016 |
if (this->getLinkCopyOnChangeValue() != CopyOnChangeDisabled) { |
| 1017 |
prop->setValue(true); |
| 1018 |
} |
| 1019 |
} |
| 1020 |
})); |
| 1021 |
} |
| 1022 |
} |
| 1023 |
|
| 1024 |
App::GroupExtension* LinkBaseExtension::linkedPlainGroup() const |
| 1025 |
{ |
| 1026 |
if (!mySubElements.empty() && !mySubElements[0].empty()) { |
| 1027 |
return nullptr; |
| 1028 |
} |
| 1029 |
auto linked = getTrueLinkedObject(false); |
| 1030 |
if (!linked) { |
| 1031 |
return nullptr; |
| 1032 |
} |
| 1033 |
return linked->getExtensionByType<GroupExtension>(true, false); |
| 1034 |
} |
| 1035 |
|
| 1036 |
App::PropertyLinkList* LinkBaseExtension::_getElementListProperty() const |
| 1037 |
{ |
| 1038 |
auto group = linkedPlainGroup(); |
| 1039 |
if (group) { |
| 1040 |
return &group->Group; |
| 1041 |
} |
| 1042 |
return const_cast<PropertyLinkList*>(getElementListProperty()); |
| 1043 |
} |
| 1044 |
|
| 1045 |
const std::vector<App::DocumentObject*>& LinkBaseExtension::_getElementListValue() const |
| 1046 |
{ |
| 1047 |
if (_ChildCache.getSize()) { |
| 1048 |
return _ChildCache.getValues(); |
| 1049 |
} |
| 1050 |
if (getElementListProperty()) { |
| 1051 |
return getElementListProperty()->getValues(); |
| 1052 |
} |
| 1053 |
static const std::vector<DocumentObject*> empty; |
| 1054 |
return empty; |
| 1055 |
} |
| 1056 |
|
| 1057 |
App::PropertyBool* LinkBaseExtension::_getShowElementProperty() const |
| 1058 |
{ |
| 1059 |
auto prop = getShowElementProperty(); |
| 1060 |
if (prop && !linkedPlainGroup()) { |
| 1061 |
return const_cast<App::PropertyBool*>(prop); |
| 1062 |
} |
| 1063 |
return nullptr; |
| 1064 |
} |
| 1065 |
|
| 1066 |
bool LinkBaseExtension::_getShowElementValue() const |
| 1067 |
{ |
| 1068 |
auto prop = _getShowElementProperty(); |
| 1069 |
if (prop) { |
| 1070 |
return prop->getValue(); |
| 1071 |
} |
| 1072 |
return true; |
| 1073 |
} |
| 1074 |
|
| 1075 |
App::PropertyInteger* LinkBaseExtension::_getElementCountProperty() const |
| 1076 |
{ |
| 1077 |
auto prop = getElementCountProperty(); |
| 1078 |
if (prop && !linkedPlainGroup()) { |
| 1079 |
return const_cast<App::PropertyInteger*>(prop); |
| 1080 |
} |
| 1081 |
return nullptr; |
| 1082 |
} |
| 1083 |
|
| 1084 |
int LinkBaseExtension::_getElementCountValue() const |
| 1085 |
{ |
| 1086 |
auto prop = _getElementCountProperty(); |
| 1087 |
if (prop) { |
| 1088 |
return prop->getValue(); |
| 1089 |
} |
| 1090 |
return 0; |
| 1091 |
} |
| 1092 |
|
| 1093 |
bool LinkBaseExtension::extensionHasChildElement() const |
| 1094 |
{ |
| 1095 |
if (!_getElementListValue().empty() || (_getElementCountValue() && _getShowElementValue())) { |
| 1096 |
return true; |
| 1097 |
} |
| 1098 |
if (getLinkClaimChildValue()) { |
| 1099 |
return false; |
| 1100 |
} |
| 1101 |
DocumentObject* linked = getTrueLinkedObject(false); |
| 1102 |
if (linked) { |
| 1103 |
if (linked->hasChildElement()) { |
| 1104 |
return true; |
| 1105 |
} |
| 1106 |
} |
| 1107 |
return false; |
| 1108 |
} |
| 1109 |
|
| 1110 |
int LinkBaseExtension::extensionSetElementVisible(const char* element, bool visible) |
| 1111 |
{ |
| 1112 |
int index = _getShowElementValue() ? getElementIndex(element) : getArrayIndex(element); |
| 1113 |
if (index >= 0) { |
| 1114 |
auto propElementVis = getVisibilityListProperty(); |
| 1115 |
if (!propElementVis || !element || !element[0]) { |
| 1116 |
return -1; |
| 1117 |
} |
| 1118 |
if (propElementVis->getSize() <= index) { |
| 1119 |
if (visible) { |
| 1120 |
return 1; |
| 1121 |
} |
| 1122 |
propElementVis->setSize(index + 1, true); |
| 1123 |
} |
| 1124 |
propElementVis->setStatus(Property::User3, true); |
| 1125 |
propElementVis->set1Value(index, visible); |
| 1126 |
propElementVis->setStatus(Property::User3, false); |
| 1127 |
const auto& elements = _getElementListValue(); |
| 1128 |
if (index < (int)elements.size()) { |
| 1129 |
if (!visible) { |
| 1130 |
myHiddenElements.insert(elements[index]); |
| 1131 |
} |
| 1132 |
else { |
| 1133 |
myHiddenElements.erase(elements[index]); |
| 1134 |
} |
| 1135 |
} |
| 1136 |
return 1; |
| 1137 |
} |
| 1138 |
DocumentObject* linked = getTrueLinkedObject(false); |
| 1139 |
if (linked) { |
| 1140 |
return linked->setElementVisible(element, visible); |
| 1141 |
} |
| 1142 |
return -1; |
| 1143 |
} |
| 1144 |
|
| 1145 |
int LinkBaseExtension::extensionIsElementVisible(const char* element) |
| 1146 |
{ |
| 1147 |
int index = _getShowElementValue() ? getElementIndex(element) : getArrayIndex(element); |
| 1148 |
if (index >= 0) { |
| 1149 |
auto propElementVis = getVisibilityListProperty(); |
| 1150 |
if (propElementVis) { |
| 1151 |
if (propElementVis->getSize() <= index || propElementVis->getValues()[index]) { |
| 1152 |
return 1; |
| 1153 |
} |
| 1154 |
return 0; |
| 1155 |
} |
| 1156 |
return -1; |
| 1157 |
} |
| 1158 |
DocumentObject* linked = getTrueLinkedObject(false); |
| 1159 |
if (linked) { |
| 1160 |
return linked->isElementVisible(element); |
| 1161 |
} |
| 1162 |
return -1; |
| 1163 |
} |
| 1164 |
|
| 1165 |
const DocumentObject* LinkBaseExtension::getContainer() const |
| 1166 |
{ |
| 1167 |
auto ext = getExtendedContainer(); |
| 1168 |
if (!ext || !ext->isDerivedFrom<DocumentObject>()) { |
| 1169 |
LINK_THROW(Base::RuntimeError, "Link: container not derived from document object"); |
| 1170 |
} |
| 1171 |
return static_cast<const DocumentObject*>(ext); |
| 1172 |
} |
| 1173 |
|
| 1174 |
DocumentObject* LinkBaseExtension::getContainer() |
| 1175 |
{ |
| 1176 |
auto ext = getExtendedContainer(); |
| 1177 |
if (!ext || !ext->isDerivedFrom<DocumentObject>()) { |
| 1178 |
LINK_THROW(Base::RuntimeError, "Link: container not derived from document object"); |
| 1179 |
} |
| 1180 |
return static_cast<DocumentObject*>(ext); |
| 1181 |
} |
| 1182 |
|
| 1183 |
DocumentObject* LinkBaseExtension::getLink(int depth) const |
| 1184 |
{ |
| 1185 |
if (!GetApplication().checkLinkDepth(depth, MessageOption::Error)) { |
| 1186 |
return nullptr; |
| 1187 |
} |
| 1188 |
if (getLinkedObjectProperty()) { |
| 1189 |
return getLinkedObjectValue(); |
| 1190 |
} |
| 1191 |
return nullptr; |
| 1192 |
} |
| 1193 |
|
| 1194 |
int LinkBaseExtension::getArrayIndex(const char* subname, const char** psubname) |
| 1195 |
{ |
| 1196 |
if (!subname || Data::isMappedElement(subname)) { |
| 1197 |
return -1; |
| 1198 |
} |
| 1199 |
const char* dot = strchr(subname, '.'); |
| 1200 |
if (!dot) { |
| 1201 |
dot = subname + strlen(subname); |
| 1202 |
} |
| 1203 |
if (dot == subname) { |
| 1204 |
return -1; |
| 1205 |
} |
| 1206 |
int idx = 0; |
| 1207 |
for (const char* c = subname; c != dot; ++c) { |
| 1208 |
if (!isdigit(*c)) { |
| 1209 |
return -1; |
| 1210 |
} |
| 1211 |
idx = idx * 10 + *c - '0'; |
| 1212 |
} |
| 1213 |
if (psubname) { |
| 1214 |
if (*dot) { |
| 1215 |
*psubname = dot + 1; |
| 1216 |
} |
| 1217 |
else { |
| 1218 |
*psubname = dot; |
| 1219 |
} |
| 1220 |
} |
| 1221 |
return idx; |
| 1222 |
} |
| 1223 |
|
| 1224 |
int LinkBaseExtension::getElementIndex(const char* subname, const char** psubname) const |
| 1225 |
{ |
| 1226 |
if (!subname || Data::isMappedElement(subname)) { |
| 1227 |
return -1; |
| 1228 |
} |
| 1229 |
int idx = -1; |
| 1230 |
const char* dot = strchr(subname, '.'); |
| 1231 |
if (!dot) { |
| 1232 |
dot = subname + strlen(subname); |
| 1233 |
} |
| 1234 |
|
| 1235 |
if (isdigit(subname[0])) { |
| 1236 |
// If the name start with digits, treat as index reference |
| 1237 |
idx = getArrayIndex(subname, nullptr); |
| 1238 |
if (idx < 0) { |
| 1239 |
return -1; |
| 1240 |
} |
| 1241 |
if (_getElementCountProperty()) { |
| 1242 |
if (idx >= _getElementCountValue()) { |
| 1243 |
return -1; |
| 1244 |
} |
| 1245 |
} |
| 1246 |
else if (idx >= (int)_getElementListValue().size()) { |
| 1247 |
return -1; |
| 1248 |
} |
| 1249 |
} |
| 1250 |
else if (!_getShowElementValue() && _getElementCountValue()) { |
| 1251 |
// If elements are collapsed, we check first for LinkElement naming |
| 1252 |
// pattern, which is the owner object name + "_i" + index |
| 1253 |
const char* name = subname[0] == '$' ? subname + 1 : subname; |
| 1254 |
auto owner = getContainer(); |
| 1255 |
if (owner && owner->isAttachedToDocument()) { |
| 1256 |
std::string ownerName(owner->getNameInDocument()); |
| 1257 |
ownerName += '_'; |
| 1258 |
if (boost::algorithm::starts_with(name, ownerName.c_str())) { |
| 1259 |
for (const char* txt = dot - 1; txt >= name + ownerName.size(); --txt) { |
| 1260 |
if (*txt == 'i') { |
| 1261 |
idx = getArrayIndex(txt + 1, nullptr); |
| 1262 |
if (idx < 0 || idx >= _getElementCountValue()) { |
| 1263 |
idx = -1; |
| 1264 |
} |
| 1265 |
break; |
| 1266 |
} |
| 1267 |
if (!isdigit(*txt)) { |
| 1268 |
break; |
| 1269 |
} |
| 1270 |
} |
| 1271 |
} |
| 1272 |
} |
| 1273 |
if (idx < 0) { |
| 1274 |
// Then check for the actual linked object's name or label, and |
| 1275 |
// redirect that reference to the first array element |
| 1276 |
auto linked = getTrueLinkedObject(false); |
| 1277 |
if (!linked || !linked->isAttachedToDocument()) { |
| 1278 |
return -1; |
| 1279 |
} |
| 1280 |
if (subname[0] == '$') { |
| 1281 |
CharRange sub(subname + 1, dot); |
| 1282 |
if (boost::equals(sub, linked->Label.getValue())) { |
| 1283 |
idx = 0; |
| 1284 |
} |
| 1285 |
} |
| 1286 |
else { |
| 1287 |
CharRange sub(subname, dot); |
| 1288 |
if (boost::equals(sub, linked->getNameInDocument())) { |
| 1289 |
idx = 0; |
| 1290 |
} |
| 1291 |
} |
| 1292 |
if (idx < 0) { |
| 1293 |
// Lastly, try to get sub object directly from the linked object |
| 1294 |
auto sobj = linked->getSubObject(std::string(subname, dot - subname + 1).c_str()); |
| 1295 |
if (!sobj) { |
| 1296 |
return -1; |
| 1297 |
} |
| 1298 |
if (psubname) { |
| 1299 |
*psubname = subname; |
| 1300 |
} |
| 1301 |
return 0; |
| 1302 |
} |
| 1303 |
} |
| 1304 |
} |
| 1305 |
else if (subname[0] != '$') { |
| 1306 |
// Try search by element objects' name |
| 1307 |
std::string name(subname, dot); |
| 1308 |
if (_ChildCache.getSize()) { |
| 1309 |
auto obj = _ChildCache.findUsingMap(name, &idx); |
| 1310 |
if (obj) { |
| 1311 |
auto group = obj->getExtensionByType<GroupExtension>(true, false); |
| 1312 |
if (group) { |
| 1313 |
int nidx = getElementIndex(dot + 1, psubname); |
| 1314 |
if (nidx >= 0) { |
| 1315 |
return nidx; |
| 1316 |
} |
| 1317 |
} |
| 1318 |
} |
| 1319 |
} |
| 1320 |
else if (getElementListProperty()) { |
| 1321 |
getElementListProperty()->find(name.c_str(), &idx); |
| 1322 |
} |
| 1323 |
if (idx < 0) { |
| 1324 |
return -1; |
| 1325 |
} |
| 1326 |
} |
| 1327 |
else { |
| 1328 |
// Try search by label if the reference name start with '$' |
| 1329 |
++subname; |
| 1330 |
std::string name(subname, dot - subname); |
| 1331 |
const auto& elements = _getElementListValue(); |
| 1332 |
if (enableLabelCache) { |
| 1333 |
if (myLabelCache.empty()) { |
| 1334 |
cacheChildLabel(1); |
| 1335 |
} |
| 1336 |
auto it = myLabelCache.find(name); |
| 1337 |
if (it == myLabelCache.end()) { |
| 1338 |
return -1; |
| 1339 |
} |
| 1340 |
idx = it->second; |
| 1341 |
} |
| 1342 |
else { |
| 1343 |
idx = 0; |
| 1344 |
for (auto element : elements) { |
| 1345 |
if (element->Label.getStrValue() == name) { |
| 1346 |
break; |
| 1347 |
} |
| 1348 |
++idx; |
| 1349 |
} |
| 1350 |
} |
| 1351 |
if (idx < 0 || idx >= (int)elements.size()) { |
| 1352 |
return -1; |
| 1353 |
} |
| 1354 |
auto obj = elements[idx]; |
| 1355 |
if (obj && _ChildCache.getSize()) { |
| 1356 |
auto group = obj->getExtensionByType<GroupExtension>(true, false); |
| 1357 |
if (group) { |
| 1358 |
int nidx = getElementIndex(dot + 1, psubname); |
| 1359 |
if (nidx >= 0) { |
| 1360 |
return nidx; |
| 1361 |
} |
| 1362 |
} |
| 1363 |
} |
| 1364 |
} |
| 1365 |
if (psubname) { |
| 1366 |
*psubname = dot[0] ? dot + 1 : dot; |
| 1367 |
} |
| 1368 |
return idx; |
| 1369 |
} |
| 1370 |
|
| 1371 |
void LinkBaseExtension::elementNameFromIndex(int idx, std::ostream& ss) const |
| 1372 |
{ |
| 1373 |
const auto& elements = _getElementListValue(); |
| 1374 |
if (idx < 0 || idx >= (int)elements.size()) { |
| 1375 |
return; |
| 1376 |
} |
| 1377 |
|
| 1378 |
auto obj = elements[idx]; |
| 1379 |
if (_ChildCache.getSize()) { |
| 1380 |
auto group = GroupExtension::getGroupOfObject(obj); |
| 1381 |
if (group && _ChildCache.find(group->getNameInDocument(), &idx)) { |
| 1382 |
elementNameFromIndex(idx, ss); |
| 1383 |
} |
| 1384 |
} |
| 1385 |
ss << obj->getNameInDocument() << '.'; |
| 1386 |
} |
| 1387 |
|
| 1388 |
Base::Vector3d LinkBaseExtension::getScaleVector() const |
| 1389 |
{ |
| 1390 |
if (getScaleVectorProperty()) { |
| 1391 |
return getScaleVectorValue(); |
| 1392 |
} |
| 1393 |
double s = getScaleValue(); |
| 1394 |
return Base::Vector3d(s, s, s); |
| 1395 |
} |
| 1396 |
|
| 1397 |
Base::Matrix4D LinkBaseExtension::getTransform(bool transform) const |
| 1398 |
{ |
| 1399 |
Base::Matrix4D mat; |
| 1400 |
if (transform) { |
| 1401 |
if (getLinkPlacementProperty()) { |
| 1402 |
mat = getLinkPlacementValue().toMatrix(); |
| 1403 |
} |
| 1404 |
else if (getPlacementProperty()) { |
| 1405 |
mat = getPlacementValue().toMatrix(); |
| 1406 |
} |
| 1407 |
} |
| 1408 |
if (getScaleProperty() || getScaleVectorProperty()) { |
| 1409 |
Base::Matrix4D s; |
| 1410 |
s.scale(getScaleVector()); |
| 1411 |
mat *= s; |
| 1412 |
} |
| 1413 |
return mat; |
| 1414 |
} |
| 1415 |
|
| 1416 |
bool LinkBaseExtension::extensionGetSubObjects(std::vector<std::string>& ret, int reason) const |
| 1417 |
{ |
| 1418 |
if (!getLinkedObjectProperty() && getElementListProperty()) { |
| 1419 |
for (auto obj : getElementListProperty()->getValues()) { |
| 1420 |
if (obj && obj->isAttachedToDocument()) { |
| 1421 |
std::string name(obj->getNameInDocument()); |
| 1422 |
name += '.'; |
| 1423 |
ret.push_back(name); |
| 1424 |
} |
| 1425 |
} |
| 1426 |
return true; |
| 1427 |
} |
| 1428 |
if (mySubElements.empty() || mySubElements[0].empty()) { |
| 1429 |
DocumentObject* linked = getTrueLinkedObject(true); |
| 1430 |
if (linked) { |
| 1431 |
if (!_getElementCountValue()) { |
| 1432 |
ret = linked->getSubObjects(reason); |
| 1433 |
} |
| 1434 |
else { |
| 1435 |
char index[30]; |
| 1436 |
for (int i = 0, count = _getElementCountValue(); i < count; ++i) { |
| 1437 |
snprintf(index, sizeof(index), "%d.", i); |
| 1438 |
ret.emplace_back(index); |
| 1439 |
} |
| 1440 |
} |
| 1441 |
} |
| 1442 |
} |
| 1443 |
else if (mySubElements.size() > 1) { |
| 1444 |
ret = mySubElements; |
| 1445 |
} |
| 1446 |
return true; |
| 1447 |
} |
| 1448 |
|
| 1449 |
bool LinkBaseExtension::extensionGetSubObject(DocumentObject*& ret, |
| 1450 |
const char* subname, |
| 1451 |
PyObject** pyObj, |
| 1452 |
Base::Matrix4D* mat, |
| 1453 |
bool transform, |
| 1454 |
int depth) const |
| 1455 |
{ |
| 1456 |
ret = nullptr; |
| 1457 |
auto obj = getContainer(); |
| 1458 |
if (!subname || !subname[0]) { |
| 1459 |
ret = const_cast<DocumentObject*>(obj); |
| 1460 |
Base::Matrix4D _mat; |
| 1461 |
if (mat) { |
| 1462 |
// 'mat' here is used as an output to return the accumulated |
| 1463 |
// transformation up until this object. Since 'subname' is empty |
| 1464 |
// here, it means the we are at the end of the hierarchy. We shall |
| 1465 |
// not include scale in the output transformation. |
| 1466 |
// |
| 1467 |
// Think of it this way, the transformation along object hierarchy |
| 1468 |
// is public, while transformation through linkage is private to |
| 1469 |
// link itself. |
| 1470 |
if (transform) { |
| 1471 |
if (getLinkPlacementProperty()) { |
| 1472 |
*mat *= getLinkPlacementValue().toMatrix(); |
| 1473 |
} |
| 1474 |
else if (getPlacementProperty()) { |
| 1475 |
*mat *= getPlacementValue().toMatrix(); |
| 1476 |
} |
| 1477 |
} |
| 1478 |
_mat = *mat; |
| 1479 |
} |
| 1480 |
|
| 1481 |
if (pyObj && !_getElementCountValue() && _getElementListValue().empty() |
| 1482 |
&& mySubElements.size() <= 1) { |
| 1483 |
// Scale will be included here |
| 1484 |
if (getScaleProperty() || getScaleVectorProperty()) { |
| 1485 |
Base::Matrix4D s; |
| 1486 |
s.scale(getScaleVector()); |
| 1487 |
_mat *= s; |
| 1488 |
} |
| 1489 |
auto linked = getTrueLinkedObject(false, &_mat, depth); |
| 1490 |
if (linked && linked != obj) { |
| 1491 |
linked->getSubObject(mySubElements.empty() ? nullptr |
| 1492 |
: mySubElements.front().c_str(), |
| 1493 |
pyObj, |
| 1494 |
&_mat, |
| 1495 |
false, |
| 1496 |
depth + 1); |
| 1497 |
checkGeoElementMap(obj, linked, pyObj, nullptr); |
| 1498 |
} |
| 1499 |
} |
| 1500 |
return true; |
| 1501 |
} |
| 1502 |
|
| 1503 |
if (mat) { |
| 1504 |
*mat *= getTransform(transform); |
| 1505 |
} |
| 1506 |
|
| 1507 |
// DocumentObject *element = 0; |
| 1508 |
bool isElement = false; |
| 1509 |
int idx = getElementIndex(subname, &subname); |
| 1510 |
if (idx >= 0) { |
| 1511 |
const auto& elements = _getElementListValue(); |
| 1512 |
if (!elements.empty()) { |
| 1513 |
if (idx >= (int)elements.size() || !elements[idx] |
| 1514 |
|| !elements[idx]->isAttachedToDocument()) { |
| 1515 |
return true; |
| 1516 |
} |
| 1517 |
ret = elements[idx]->getSubObject(subname, pyObj, mat, true, depth + 1); |
| 1518 |
// do not resolve the link if this element is the last referenced object |
| 1519 |
if (!subname || Data::isMappedElement(subname) || !strchr(subname, '.')) { |
| 1520 |
ret = elements[idx]; |
| 1521 |
} |
| 1522 |
return true; |
| 1523 |
} |
| 1524 |
|
| 1525 |
int elementCount = _getElementCountValue(); |
| 1526 |
if (idx >= elementCount) { |
| 1527 |
return true; |
| 1528 |
} |
| 1529 |
isElement = true; |
| 1530 |
if (mat) { |
| 1531 |
auto placementList = getPlacementListProperty(); |
| 1532 |
if (placementList && placementList->getSize() > idx) { |
| 1533 |
*mat *= (*placementList)[idx].toMatrix(); |
| 1534 |
} |
| 1535 |
auto scaleList = getScaleListProperty(); |
| 1536 |
if (scaleList && scaleList->getSize() > idx) { |
| 1537 |
Base::Matrix4D s; |
| 1538 |
s.scale((*scaleList)[idx]); |
| 1539 |
*mat *= s; |
| 1540 |
} |
| 1541 |
} |
| 1542 |
} |
| 1543 |
|
| 1544 |
auto linked = getTrueLinkedObject(false, mat, depth); |
| 1545 |
if (!linked || linked == obj) { |
| 1546 |
return true; |
| 1547 |
} |
| 1548 |
|
| 1549 |
Base::Matrix4D matNext; |
| 1550 |
|
| 1551 |
// Because of the addition of LinkClaimChild, the linked object may be |
| 1552 |
// claimed as the first child. Regardless of the current value of |
| 1553 |
// LinkClaimChild, we must accept sub-object path that contains the linked |
| 1554 |
// object, because other link property may store such reference. |
| 1555 |
if (const char* dot = strchr(subname, '.')) { |
| 1556 |
auto group = getLinkCopyOnChangeGroupValue(); |
| 1557 |
if (subname[0] == '$') { |
| 1558 |
CharRange sub(subname + 1, dot); |
| 1559 |
if (group && boost::equals(sub, group->Label.getValue())) { |
| 1560 |
linked = group; |
| 1561 |
} |
| 1562 |
else if (!boost::equals(sub, linked->Label.getValue())) { |
| 1563 |
dot = nullptr; |
| 1564 |
} |
| 1565 |
} |
| 1566 |
else { |
| 1567 |
CharRange sub(subname, dot); |
| 1568 |
if (group && boost::equals(sub, group->getNameInDocument())) { |
| 1569 |
linked = group; |
| 1570 |
} |
| 1571 |
else if (!boost::equals(sub, linked->getNameInDocument())) { |
| 1572 |
dot = nullptr; |
| 1573 |
} |
| 1574 |
} |
| 1575 |
if (dot) { |
| 1576 |
// Because of external linked object, It is possible for and |
| 1577 |
// child object to have the exact same internal name or label |
| 1578 |
// as the parent object. To resolve this potential ambiguity, |
| 1579 |
// try assuming the current subname is referring to the parent |
| 1580 |
// (i.e. the linked object), and if it fails, try again below. |
| 1581 |
if (mat) { |
| 1582 |
matNext = *mat; |
| 1583 |
} |
| 1584 |
ret = linked->getSubObject(dot + 1, pyObj, mat ? &matNext : nullptr, false, depth + 1); |
| 1585 |
if (ret && dot[1]) { |
| 1586 |
subname = dot + 1; |
| 1587 |
} |
| 1588 |
} |
| 1589 |
} |
| 1590 |
|
| 1591 |
if (!ret) { |
| 1592 |
if (mat) { |
| 1593 |
matNext = *mat; |
| 1594 |
} |
| 1595 |
ret = linked->getSubObject(subname, pyObj, mat ? &matNext : nullptr, false, depth + 1); |
| 1596 |
} |
| 1597 |
|
| 1598 |
std::string postfix; |
| 1599 |
if (ret) { |
| 1600 |
// do not resolve the link if we are the last referenced object |
| 1601 |
if (subname && !Data::isMappedElement(subname) && strchr(subname, '.')) { |
| 1602 |
if (mat) { |
| 1603 |
*mat = matNext; |
| 1604 |
} |
| 1605 |
} |
| 1606 |
// This is a useless check as 'element' is never set to a value other than null |
| 1607 |
// else if(element) { |
| 1608 |
// ret = element; |
| 1609 |
//} |
| 1610 |
else if (!isElement) { |
| 1611 |
ret = const_cast<DocumentObject*>(obj); |
| 1612 |
} |
| 1613 |
else { |
| 1614 |
if (idx) { |
| 1615 |
postfix = Data::POSTFIX_INDEX; |
| 1616 |
postfix += std::to_string(idx); |
| 1617 |
} |
| 1618 |
if (mat) { |
| 1619 |
*mat = matNext; |
| 1620 |
} |
| 1621 |
} |
| 1622 |
} |
| 1623 |
checkGeoElementMap(obj, linked, pyObj, !postfix.empty() ? postfix.c_str() : nullptr); |
| 1624 |
return true; |
| 1625 |
} |
| 1626 |
|
| 1627 |
void LinkBaseExtension::checkGeoElementMap(const App::DocumentObject* obj, |
| 1628 |
const App::DocumentObject* linked, |
| 1629 |
PyObject** pyObj, |
| 1630 |
const char* postfix) const |
| 1631 |
{ |
| 1632 |
if (!pyObj || !*pyObj || (!postfix && obj->getDocument() == linked->getDocument()) |
| 1633 |
|| !PyObject_TypeCheck(*pyObj, &Data::ComplexGeoDataPy::Type)) { |
| 1634 |
return; |
| 1635 |
} |
| 1636 |
|
| 1637 |
// auto geoData = static_cast<Data::ComplexGeoDataPy*>(*pyObj)->getComplexGeoDataPtr(); |
| 1638 |
// geoData->reTagElementMap(obj->getID(),obj->getDocument()->Hasher,postfix); |
| 1639 |
|
| 1640 |
auto geoData = static_cast<Data::ComplexGeoDataPy*>(*pyObj)->getComplexGeoDataPtr(); |
| 1641 |
std::string _postfix; |
| 1642 |
if (linked && obj && linked->getDocument() != obj->getDocument()) { |
| 1643 |
_postfix = Data::POSTFIX_EXTERNAL_TAG; |
| 1644 |
if (postfix) { |
| 1645 |
if (!boost::starts_with(postfix, Data::ComplexGeoData::elementMapPrefix())) { |
| 1646 |
_postfix += Data::ComplexGeoData::elementMapPrefix(); |
| 1647 |
} |
| 1648 |
_postfix += postfix; |
| 1649 |
} |
| 1650 |
postfix = _postfix.c_str(); |
| 1651 |
} |
| 1652 |
geoData->reTagElementMap(obj->getID(), obj->getDocument()->getStringHasher(), postfix); |
| 1653 |
} |
| 1654 |
|
| 1655 |
void LinkBaseExtension::onExtendedUnsetupObject() |
| 1656 |
{ |
| 1657 |
if (!getElementListProperty()) { |
| 1658 |
return; |
| 1659 |
} |
| 1660 |
detachElements(); |
| 1661 |
if (auto obj = getLinkCopyOnChangeGroupValue()) { |
| 1662 |
if (obj->isAttachedToDocument() && !obj->isRemoving()) { |
| 1663 |
obj->getDocument()->removeObject(obj->getNameInDocument()); |
| 1664 |
} |
| 1665 |
} |
| 1666 |
} |
| 1667 |
|
| 1668 |
DocumentObject* LinkBaseExtension::getTrueLinkedObject(bool recurse, |
| 1669 |
Base::Matrix4D* mat, |
| 1670 |
int depth, |
| 1671 |
bool noElement) const |
| 1672 |
{ |
| 1673 |
if (noElement && extensionIsDerivedFrom(LinkElement::getExtensionClassTypeId()) |
| 1674 |
&& !static_cast<const LinkElement*>(this)->canDelete()) { |
| 1675 |
return nullptr; |
| 1676 |
} |
| 1677 |
|
| 1678 |
auto ret = getLink(depth); |
| 1679 |
if (!ret) { |
| 1680 |
return nullptr; |
| 1681 |
} |
| 1682 |
bool transform = linkTransform(); |
| 1683 |
const char* subname = getSubName(); |
| 1684 |
if (subname || (mat && transform)) { |
| 1685 |
ret = ret->getSubObject(subname, nullptr, mat, transform, depth + 1); |
| 1686 |
transform = false; |
| 1687 |
} |
| 1688 |
if (ret && recurse) { |
| 1689 |
ret = ret->getLinkedObject(recurse, mat, transform, depth + 1); |
| 1690 |
} |
| 1691 |
if (ret && !ret->isAttachedToDocument()) { |
| 1692 |
return nullptr; |
| 1693 |
} |
| 1694 |
return ret; |
| 1695 |
} |
| 1696 |
|
| 1697 |
bool LinkBaseExtension::extensionGetLinkedObject(DocumentObject*& ret, |
| 1698 |
bool recurse, |
| 1699 |
Base::Matrix4D* mat, |
| 1700 |
bool transform, |
| 1701 |
int depth) const |
| 1702 |
{ |
| 1703 |
if (mat) { |
| 1704 |
*mat *= getTransform(transform); |
| 1705 |
} |
| 1706 |
ret = nullptr; |
| 1707 |
if (!_getElementCountValue()) { |
| 1708 |
ret = getTrueLinkedObject(recurse, mat, depth); |
| 1709 |
} |
| 1710 |
if (!ret) { |
| 1711 |
ret = const_cast<DocumentObject*>(getContainer()); |
| 1712 |
} |
| 1713 |
// always return true to indicate we've handled getLinkObject() call |
| 1714 |
return true; |
| 1715 |
} |
| 1716 |
|
| 1717 |
void LinkBaseExtension::extensionOnChanged(const Property* prop) |
| 1718 |
{ |
| 1719 |
auto parent = getContainer(); |
| 1720 |
if (parent && !parent->isRestoring() && prop && !prop->testStatus(Property::User3)) { |
| 1721 |
update(parent, prop); |
| 1722 |
} |
| 1723 |
inherited::extensionOnChanged(prop); |
| 1724 |
} |
| 1725 |
|
| 1726 |
void LinkBaseExtension::parseSubName() const |
| 1727 |
{ |
| 1728 |
// If user has ever linked to some sub-element, the Link shall always accept |
| 1729 |
// sub-element linking in the future, which affects how ViewProviderLink |
| 1730 |
// dropObjectEx() behave. So we will push an empty string later even if no |
| 1731 |
// sub-element linking this time. |
| 1732 |
bool hasSubElement = !mySubElements.empty(); |
| 1733 |
mySubElements.clear(); |
| 1734 |
mySubName.clear(); |
| 1735 |
auto xlink = freecad_cast<const PropertyXLink*>(getLinkedObjectProperty()); |
| 1736 |
if (!xlink || xlink->getSubValues().empty()) { |
| 1737 |
if (hasSubElement) { |
| 1738 |
mySubElements.emplace_back(""); |
| 1739 |
} |
| 1740 |
return; |
| 1741 |
} |
| 1742 |
const auto& subs = xlink->getSubValues(); |
| 1743 |
auto subname = subs.front().c_str(); |
| 1744 |
auto element = Data::findElementName(subname); |
| 1745 |
if (!element || !element[0]) { |
| 1746 |
mySubName = subs[0]; |
| 1747 |
if (hasSubElement) { |
| 1748 |
mySubElements.emplace_back(""); |
| 1749 |
} |
| 1750 |
return; |
| 1751 |
} |
| 1752 |
mySubElements.emplace_back(element); |
| 1753 |
mySubName = std::string(subname, element - subname); |
| 1754 |
for (std::size_t i = 1; i < subs.size(); ++i) { |
| 1755 |
auto& sub = subs[i]; |
| 1756 |
element = Data::findElementName(sub.c_str()); |
| 1757 |
if (!Base::Tools::isNullOrEmpty(element) && boost::starts_with(sub, mySubName)) { |
| 1758 |
mySubElements.emplace_back(element); |
| 1759 |
} |
| 1760 |
} |
| 1761 |
} |
| 1762 |
|
| 1763 |
void LinkBaseExtension::slotChangedPlainGroup(const App::DocumentObject& obj, |
| 1764 |
const App::Property& prop) |
| 1765 |
{ |
| 1766 |
auto group = obj.getExtensionByType<GroupExtension>(true, false); |
| 1767 |
if (group && &prop == &group->Group) { |
| 1768 |
updateGroup(); |
| 1769 |
} |
| 1770 |
} |
| 1771 |
|
| 1772 |
void LinkBaseExtension::updateGroup() |
| 1773 |
{ |
| 1774 |
std::vector<GroupExtension*> groups; |
| 1775 |
std::unordered_set<const App::DocumentObject*> groupSet; |
| 1776 |
auto group = linkedPlainGroup(); |
| 1777 |
if (group) { |
| 1778 |
groups.push_back(group); |
| 1779 |
groupSet.insert(group->getExtendedObject()); |
| 1780 |
} |
| 1781 |
else { |
| 1782 |
for (auto o : getElementListProperty()->getValues()) { |
| 1783 |
if (!o || !o->isAttachedToDocument()) { |
| 1784 |
continue; |
| 1785 |
} |
| 1786 |
auto ext = o->getExtensionByType<GroupExtension>(true, false); |
| 1787 |
if (ext) { |
| 1788 |
groups.push_back(ext); |
| 1789 |
groupSet.insert(o); |
| 1790 |
} |
| 1791 |
} |
| 1792 |
} |
| 1793 |
std::vector<App::DocumentObject*> children; |
| 1794 |
if (!groups.empty()) { |
| 1795 |
children = getElementListValue(); |
| 1796 |
std::set<DocumentObject*> childSet(children.begin(), children.end()); |
| 1797 |
for (auto ext : groups) { |
| 1798 |
auto group = ext->getExtendedObject(); |
| 1799 |
auto& conn = plainGroupConns[group]; |
| 1800 |
if (!conn.connected()) { |
| 1801 |
FC_LOG("new group connection " << getExtendedObject()->getFullName() << " -> " |
| 1802 |
<< group->getFullName()); |
| 1803 |
// NOLINTBEGIN |
| 1804 |
conn = group->signalChanged.connect( |
| 1805 |
std::bind(&LinkBaseExtension::slotChangedPlainGroup, this, sp::_1, sp::_2)); |
| 1806 |
// NOLINTEND |
| 1807 |
} |
| 1808 |
std::size_t count = children.size(); |
| 1809 |
ext->getAllChildren(children, childSet); |
| 1810 |
for (; count < children.size(); ++count) { |
| 1811 |
auto child = children[count]; |
| 1812 |
if (!child->getExtensionByType<GroupExtension>(true, false)) { |
| 1813 |
continue; |
| 1814 |
} |
| 1815 |
groupSet.insert(child); |
| 1816 |
auto& conn = plainGroupConns[child]; |
| 1817 |
if (!conn.connected()) { |
| 1818 |
FC_LOG("new group connection " << getExtendedObject()->getFullName() << " -> " |
| 1819 |
<< child->getFullName()); |
| 1820 |
// NOLINTBEGIN |
| 1821 |
conn = child->signalChanged.connect( |
| 1822 |
std::bind(&LinkBaseExtension::slotChangedPlainGroup, this, sp::_1, sp::_2)); |
| 1823 |
// NOLINTEND |
| 1824 |
} |
| 1825 |
} |
| 1826 |
} |
| 1827 |
} |
| 1828 |
for (auto it = plainGroupConns.begin(); it != plainGroupConns.end();) { |
| 1829 |
if (!groupSet.contains(it->first)) { |
| 1830 |
it = plainGroupConns.erase(it); |
| 1831 |
} |
| 1832 |
else { |
| 1833 |
++it; |
| 1834 |
} |
| 1835 |
} |
| 1836 |
if (children != _ChildCache.getValues()) { |
| 1837 |
_ChildCache.setValue(children); |
| 1838 |
} |
| 1839 |
} |
| 1840 |
|
| 1841 |
void LinkBaseExtension::update(App::DocumentObject* parent, const Property* prop) |
| 1842 |
{ |
| 1843 |
if (!prop) { |
| 1844 |
return; |
| 1845 |
} |
| 1846 |
|
| 1847 |
if (prop == getLinkPlacementProperty() || prop == getPlacementProperty()) { |
| 1848 |
auto src = getLinkPlacementProperty(); |
| 1849 |
auto dst = getPlacementProperty(); |
| 1850 |
if (src != prop) { |
| 1851 |
std::swap(src, dst); |
| 1852 |
} |
| 1853 |
if (src && dst) { |
| 1854 |
dst->setStatus(Property::User3, true); |
| 1855 |
dst->setValue(src->getValue()); |
| 1856 |
dst->setStatus(Property::User3, false); |
| 1857 |
} |
| 1858 |
} |
| 1859 |
else if (prop == getScaleProperty()) { |
| 1860 |
if (!prop->testStatus(Property::User3) && getScaleVectorProperty()) { |
| 1861 |
auto s = getScaleValue(); |
| 1862 |
auto p = getScaleVectorProperty(); |
| 1863 |
p->setStatus(Property::User3, true); |
| 1864 |
p->setValue(s, s, s); |
| 1865 |
p->setStatus(Property::User3, false); |
| 1866 |
} |
| 1867 |
} |
| 1868 |
else if (prop == getScaleVectorProperty()) { |
| 1869 |
if (!prop->testStatus(Property::User3) && getScaleProperty()) { |
| 1870 |
const auto& v = getScaleVectorValue(); |
| 1871 |
if (v.x == v.y && v.x == v.z) { |
| 1872 |
auto p = getScaleProperty(); |
| 1873 |
p->setStatus(Property::User3, true); |
| 1874 |
p->setValue(v.x); |
| 1875 |
p->setStatus(Property::User3, false); |
| 1876 |
} |
| 1877 |
} |
| 1878 |
} |
| 1879 |
else if (prop == _getShowElementProperty()) { |
| 1880 |
if (_getShowElementValue()) { |
| 1881 |
update(parent, _getElementCountProperty()); |
| 1882 |
} |
| 1883 |
else { |
| 1884 |
auto objs = getElementListValue(); |
| 1885 |
|
| 1886 |
// preserve element properties in ourself |
| 1887 |
std::vector<Base::Placement> placements; |
| 1888 |
placements.reserve(objs.size()); |
| 1889 |
std::vector<Base::Vector3d> scales; |
| 1890 |
scales.reserve(objs.size()); |
| 1891 |
for (auto obj : objs) { |
| 1892 |
auto element = freecad_cast<LinkElement*>(obj); |
| 1893 |
if (element) { |
| 1894 |
placements.push_back(element->Placement.getValue()); |
| 1895 |
scales.push_back(element->getScaleVector()); |
| 1896 |
} |
| 1897 |
else { |
| 1898 |
placements.emplace_back(); |
| 1899 |
scales.emplace_back(1, 1, 1); |
| 1900 |
} |
| 1901 |
} |
| 1902 |
// touch the property again to make sure view provider has been |
| 1903 |
// signaled before clearing the elements |
| 1904 |
getShowElementProperty()->setStatus(App::Property::User3, true); |
| 1905 |
getShowElementProperty()->touch(); |
| 1906 |
getShowElementProperty()->setStatus(App::Property::User3, false); |
| 1907 |
|
| 1908 |
getElementListProperty()->setValues(std::vector<App::DocumentObject*>()); |
| 1909 |
|
| 1910 |
if (getPlacementListProperty()) { |
| 1911 |
getPlacementListProperty()->setStatus(Property::User3, |
| 1912 |
getScaleListProperty() != nullptr); |
| 1913 |
getPlacementListProperty()->setValue(placements); |
| 1914 |
getPlacementListProperty()->setStatus(Property::User3, false); |
| 1915 |
} |
| 1916 |
if (getScaleListProperty()) { |
| 1917 |
getScaleListProperty()->setValue(scales); |
| 1918 |
} |
| 1919 |
|
| 1920 |
for (auto obj : objs) { |
| 1921 |
if (obj && obj->isAttachedToDocument()) { |
| 1922 |
obj->getDocument()->removeObject(obj->getNameInDocument()); |
| 1923 |
} |
| 1924 |
} |
| 1925 |
} |
| 1926 |
} |
| 1927 |
else if (prop == _getElementCountProperty()) { |
| 1928 |
size_t elementCount = getElementCountValue() < 0 ? 0 : (size_t)getElementCountValue(); |
| 1929 |
|
| 1930 |
auto propVis = getVisibilityListProperty(); |
| 1931 |
if (propVis) { |
| 1932 |
if (propVis->getSize() > (int)elementCount) { |
| 1933 |
propVis->setSize(getElementCountValue(), true); |
| 1934 |
} |
| 1935 |
} |
| 1936 |
|
| 1937 |
if (!_getShowElementValue()) { |
| 1938 |
if (getScaleListProperty()) { |
| 1939 |
auto scales = getScaleListValue(); |
| 1940 |
scales.resize(elementCount, Base::Vector3d(1, 1, 1)); |
| 1941 |
getScaleListProperty()->setStatus(Property::User3, true); |
| 1942 |
getScaleListProperty()->setValue(scales); |
| 1943 |
getScaleListProperty()->setStatus(Property::User3, false); |
| 1944 |
} |
| 1945 |
if (getPlacementListProperty()) { |
| 1946 |
auto placements = getPlacementListValue(); |
| 1947 |
if (placements.size() < elementCount) { |
| 1948 |
for (size_t i = placements.size(); i < elementCount; ++i) { |
| 1949 |
placements.emplace_back(Base::Vector3d(i % 10, (i / 10) % 10, i / 100), |
| 1950 |
Base::Rotation()); |
| 1951 |
} |
| 1952 |
} |
| 1953 |
else { |
| 1954 |
placements.resize(elementCount); |
| 1955 |
} |
| 1956 |
getPlacementListProperty()->setStatus(Property::User3, true); |
| 1957 |
getPlacementListProperty()->setValue(placements); |
| 1958 |
getPlacementListProperty()->setStatus(Property::User3, false); |
| 1959 |
} |
| 1960 |
} |
| 1961 |
else if (getElementListProperty()) { |
| 1962 |
auto objs = getElementListValue(); |
| 1963 |
if (elementCount > objs.size()) { |
| 1964 |
std::string name = parent->getNameInDocument(); |
| 1965 |
auto doc = parent->getDocument(); |
| 1966 |
name += "_i"; |
| 1967 |
name = doc->getUniqueObjectName(name.c_str()); |
| 1968 |
if (name[name.size() - 1] != 'i') { |
| 1969 |
name += "_i"; |
| 1970 |
} |
| 1971 |
auto offset = name.size(); |
| 1972 |
auto placementProp = getPlacementListProperty(); |
| 1973 |
auto scaleProp = getScaleListProperty(); |
| 1974 |
const auto& vis = getVisibilityListValue(); |
| 1975 |
|
| 1976 |
auto owner = getContainer(); |
| 1977 |
long ownerID = owner ? owner->getID() : 0; |
| 1978 |
|
| 1979 |
for (size_t i = objs.size(); i < elementCount; ++i) { |
| 1980 |
name.resize(offset); |
| 1981 |
name += std::to_string(i); |
| 1982 |
|
| 1983 |
// It is possible to have orphan LinkElement here due to, |
| 1984 |
// for example, undo and redo. So we try to re-claim the |
| 1985 |
// children element first. |
| 1986 |
auto obj = freecad_cast<LinkElement*>(doc->getObject(name.c_str())); |
| 1987 |
if (obj |
| 1988 |
&& (!obj->_LinkOwner.getValue() || obj->_LinkOwner.getValue() == ownerID)) { |
| 1989 |
obj->Visibility.setValue(false); |
| 1990 |
} |
| 1991 |
else { |
| 1992 |
obj = new LinkElement; |
| 1993 |
parent->getDocument()->addObject(obj, name.c_str()); |
| 1994 |
} |
| 1995 |
|
| 1996 |
if (vis.size() > i && !vis[i]) { |
| 1997 |
myHiddenElements.insert(obj); |
| 1998 |
} |
| 1999 |
|
| 2000 |
if (placementProp && placementProp->getSize() > (int)i) { |
| 2001 |
obj->Placement.setValue(placementProp->getValues()[i]); |
| 2002 |
} |
| 2003 |
else { |
| 2004 |
Base::Placement pla(Base::Vector3d(i % 10, (i / 10) % 10, i / 100), |
| 2005 |
Base::Rotation()); |
| 2006 |
obj->Placement.setValue(pla); |
| 2007 |
} |
| 2008 |
if (scaleProp && scaleProp->getSize() > (int)i) { |
| 2009 |
obj->Scale.setValue(scaleProp->getValues()[i].x); |
| 2010 |
} |
| 2011 |
else { |
| 2012 |
obj->Scale.setValue(1); |
| 2013 |
} |
| 2014 |
objs.push_back(obj); |
| 2015 |
} |
| 2016 |
if (getPlacementListProperty()) { |
| 2017 |
getPlacementListProperty()->setSize(0); |
| 2018 |
} |
| 2019 |
if (getScaleListProperty()) { |
| 2020 |
getScaleListProperty()->setSize(0); |
| 2021 |
} |
| 2022 |
|
| 2023 |
getElementListProperty()->setValue(objs); |
| 2024 |
} |
| 2025 |
else if (elementCount < objs.size()) { |
| 2026 |
std::vector<App::DocumentObject*> tmpObjs; |
| 2027 |
auto owner = getContainer(); |
| 2028 |
long ownerID = owner ? owner->getID() : 0; |
| 2029 |
while (objs.size() > elementCount) { |
| 2030 |
auto element = freecad_cast<LinkElement*>(objs.back()); |
| 2031 |
if (element && element->_LinkOwner.getValue() == ownerID) { |
| 2032 |
tmpObjs.push_back(objs.back()); |
| 2033 |
} |
| 2034 |
objs.pop_back(); |
| 2035 |
} |
| 2036 |
getElementListProperty()->setValue(objs); |
| 2037 |
for (auto obj : tmpObjs) { |
| 2038 |
if (obj && obj->isAttachedToDocument()) { |
| 2039 |
obj->getDocument()->removeObject(obj->getNameInDocument()); |
| 2040 |
} |
| 2041 |
} |
| 2042 |
} |
| 2043 |
} |
| 2044 |
} |
| 2045 |
else if (prop == getVisibilityListProperty()) { |
| 2046 |
if (_getShowElementValue()) { |
| 2047 |
const auto& elements = _getElementListValue(); |
| 2048 |
const auto& vis = getVisibilityListValue(); |
| 2049 |
myHiddenElements.clear(); |
| 2050 |
for (size_t i = 0; i < vis.size(); ++i) { |
| 2051 |
if (i >= elements.size()) { |
| 2052 |
break; |
| 2053 |
} |
| 2054 |
if (!vis[i]) { |
| 2055 |
myHiddenElements.insert(elements[i]); |
| 2056 |
} |
| 2057 |
} |
| 2058 |
} |
| 2059 |
} |
| 2060 |
else if (prop == getElementListProperty() || prop == &_ChildCache) { |
| 2061 |
|
| 2062 |
if (prop == getElementListProperty()) { |
| 2063 |
_ChildCache.setStatus(Property::User3, true); |
| 2064 |
updateGroup(); |
| 2065 |
_ChildCache.setStatus(Property::User3, false); |
| 2066 |
} |
| 2067 |
|
| 2068 |
const auto& elements = _getElementListValue(); |
| 2069 |
|
| 2070 |
if (enableLabelCache) { |
| 2071 |
myLabelCache.clear(); |
| 2072 |
} |
| 2073 |
|
| 2074 |
// Element list changed, we need to sychrnoize VisibilityList. |
| 2075 |
if (_getShowElementValue() && getVisibilityListProperty()) { |
| 2076 |
if (parent->getDocument()->isPerformingTransaction()) { |
| 2077 |
update(parent, getVisibilityListProperty()); |
| 2078 |
} |
| 2079 |
else { |
| 2080 |
boost::dynamic_bitset<> vis; |
| 2081 |
vis.resize(elements.size(), true); |
| 2082 |
std::unordered_set<const App::DocumentObject*> hiddenElements; |
| 2083 |
for (size_t i = 0; i < elements.size(); ++i) { |
| 2084 |
if (myHiddenElements.find(elements[i]) != myHiddenElements.end()) { |
| 2085 |
hiddenElements.insert(elements[i]); |
| 2086 |
vis[i] = false; |
| 2087 |
} |
| 2088 |
} |
| 2089 |
myHiddenElements.swap(hiddenElements); |
| 2090 |
if (vis != getVisibilityListValue()) { |
| 2091 |
auto propVis = getVisibilityListProperty(); |
| 2092 |
propVis->setStatus(Property::User3, true); |
| 2093 |
propVis->setValue(vis); |
| 2094 |
propVis->setStatus(Property::User3, false); |
| 2095 |
} |
| 2096 |
} |
| 2097 |
} |
| 2098 |
syncElementList(); |
| 2099 |
if (_getShowElementValue() && _getElementCountProperty() && getElementListProperty() |
| 2100 |
&& getElementCountValue() != getElementListProperty()->getSize()) { |
| 2101 |
getElementCountProperty()->setValue(getElementListProperty()->getSize()); |
| 2102 |
} |
| 2103 |
} |
| 2104 |
else if (prop == getLinkedObjectProperty()) { |
| 2105 |
auto group = linkedPlainGroup(); |
| 2106 |
if (getShowElementProperty()) { |
| 2107 |
getShowElementProperty()->setStatus(Property::Hidden, !!group); |
| 2108 |
} |
| 2109 |
if (getElementCountProperty()) { |
| 2110 |
getElementCountProperty()->setStatus(Property::Hidden, !!group); |
| 2111 |
} |
| 2112 |
if (group) { |
| 2113 |
updateGroup(); |
| 2114 |
} |
| 2115 |
else if (_ChildCache.getSize()) { |
| 2116 |
_ChildCache.setValue(); |
| 2117 |
} |
| 2118 |
parseSubName(); |
| 2119 |
syncElementList(); |
| 2120 |
|
| 2121 |
if (getLinkCopyOnChangeValue() == CopyOnChangeOwned && !pauseCopyOnChange |
| 2122 |
&& !parent->getDocument()->isPerformingTransaction()) { |
| 2123 |
getLinkCopyOnChangeProperty()->setValue(CopyOnChangeEnabled); |
| 2124 |
} |
| 2125 |
else { |
| 2126 |
setupCopyOnChange(parent, true); |
| 2127 |
} |
| 2128 |
} |
| 2129 |
else if (prop == getLinkCopyOnChangeProperty()) { |
| 2130 |
setupCopyOnChange(parent, getLinkCopyOnChangeSourceValue() == nullptr); |
| 2131 |
} |
| 2132 |
else if (prop == getLinkCopyOnChangeSourceProperty()) { |
| 2133 |
if (auto source = getLinkCopyOnChangeSourceValue()) { |
| 2134 |
this->connCopyOnChangeSource = source->signalChanged.connect( |
| 2135 |
[this](const DocumentObject& obj, const Property& prop) { |
| 2136 |
auto src = getLinkCopyOnChangeSourceValue(); |
| 2137 |
if (src != &obj || getLinkCopyOnChangeValue() == CopyOnChangeDisabled) { |
| 2138 |
return; |
| 2139 |
} |
| 2140 |
if (App::Document::isAnyRestoring() || obj.testStatus(ObjectStatus::NoTouch) |
| 2141 |
|| (prop.getType() & Prop_Output) || prop.testStatus(Property::Output)) { |
| 2142 |
return; |
| 2143 |
} |
| 2144 |
if (auto propTouch = getLinkCopyOnChangeTouchedProperty()) { |
| 2145 |
propTouch->setValue(true); |
| 2146 |
} |
| 2147 |
}); |
| 2148 |
} |
| 2149 |
else { |
| 2150 |
this->connCopyOnChangeSource.disconnect(); |
| 2151 |
} |
| 2152 |
} |
| 2153 |
else if (prop == getLinkTransformProperty()) { |
| 2154 |
auto linkPlacement = getLinkPlacementProperty(); |
| 2155 |
auto placement = getPlacementProperty(); |
| 2156 |
if (linkPlacement && placement) { |
| 2157 |
bool transform = getLinkTransformValue(); |
| 2158 |
placement->setStatus(Property::Hidden, transform); |
| 2159 |
linkPlacement->setStatus(Property::Hidden, !transform); |
| 2160 |
} |
| 2161 |
syncElementList(); |
| 2162 |
} |
| 2163 |
else { |
| 2164 |
checkCopyOnChange(parent, *prop); |
| 2165 |
} |
| 2166 |
} |
| 2167 |
|
| 2168 |
void LinkBaseExtension::cacheChildLabel(int enable) const |
| 2169 |
{ |
| 2170 |
enableLabelCache = enable ? true : false; |
| 2171 |
myLabelCache.clear(); |
| 2172 |
if (enable <= 0) { |
| 2173 |
return; |
| 2174 |
} |
| 2175 |
|
| 2176 |
int idx = 0; |
| 2177 |
for (auto child : _getElementListValue()) { |
| 2178 |
if (child && child->isAttachedToDocument()) { |
| 2179 |
myLabelCache[child->Label.getStrValue()] = idx; |
| 2180 |
} |
| 2181 |
++idx; |
| 2182 |
} |
| 2183 |
} |
| 2184 |
|
| 2185 |
bool LinkBaseExtension::linkTransform() const |
| 2186 |
{ |
| 2187 |
if (!getLinkTransformProperty() && !getLinkPlacementProperty() && !getPlacementProperty()) { |
| 2188 |
return true; |
| 2189 |
} |
| 2190 |
return getLinkTransformValue(); |
| 2191 |
} |
| 2192 |
|
| 2193 |
void LinkBaseExtension::syncElementList() |
| 2194 |
{ |
| 2195 |
auto transform = getLinkTransformProperty(); |
| 2196 |
auto link = getLinkedObjectProperty(); |
| 2197 |
auto xlink = freecad_cast<const PropertyXLink*>(link); |
| 2198 |
|
| 2199 |
auto owner = getContainer(); |
| 2200 |
auto ownerID = owner ? owner->getID() : 0; |
| 2201 |
auto elements = getElementListValue(); |
| 2202 |
for (auto i : elements) { |
| 2203 |
auto element = freecad_cast<LinkElement*>(i); |
| 2204 |
if (!element |
| 2205 |
|| (element->_LinkOwner.getValue() && element->_LinkOwner.getValue() != ownerID)) { |
| 2206 |
continue; |
| 2207 |
} |
| 2208 |
|
| 2209 |
element->_LinkOwner.setValue(ownerID); |
| 2210 |
|
| 2211 |
element->LinkTransform.setStatus(Property::Hidden, transform != nullptr); |
| 2212 |
element->LinkTransform.setStatus(Property::Immutable, transform != nullptr); |
| 2213 |
if (transform && element->LinkTransform.getValue() != transform->getValue()) { |
| 2214 |
element->LinkTransform.setValue(transform->getValue()); |
| 2215 |
} |
| 2216 |
|
| 2217 |
element->LinkedObject.setStatus(Property::Hidden, link != nullptr); |
| 2218 |
element->LinkedObject.setStatus(Property::Immutable, link != nullptr); |
| 2219 |
if (element->LinkCopyOnChange.getValue() == 2) { |
| 2220 |
continue; |
| 2221 |
} |
| 2222 |
if (xlink) { |
| 2223 |
if (element->LinkedObject.getValue() != xlink->getValue() |
| 2224 |
|| element->LinkedObject.getSubValues() != xlink->getSubValues()) { |
| 2225 |
element->LinkedObject.setValue(xlink->getValue(), xlink->getSubValues()); |
| 2226 |
} |
| 2227 |
} |
| 2228 |
else if (element->LinkedObject.getValue() != link->getValue() |
| 2229 |
|| !element->LinkedObject.getSubValues().empty()) { |
| 2230 |
element->setLink(-1, link->getValue()); |
| 2231 |
} |
| 2232 |
} |
| 2233 |
} |
| 2234 |
|
| 2235 |
void LinkBaseExtension::onExtendedDocumentRestored() |
| 2236 |
{ |
| 2237 |
inherited::onExtendedDocumentRestored(); |
| 2238 |
myHiddenElements.clear(); |
| 2239 |
auto parent = getContainer(); |
| 2240 |
if (!parent) { |
| 2241 |
return; |
| 2242 |
} |
| 2243 |
if (hasOldSubElement) { |
| 2244 |
hasOldSubElement = false; |
| 2245 |
// SubElements was stored as a PropertyStringList. It is now migrated to be |
| 2246 |
// stored inside PropertyXLink. |
| 2247 |
auto xlink = freecad_cast<PropertyXLink*>(getLinkedObjectProperty()); |
| 2248 |
if (!xlink) { |
| 2249 |
FC_ERR("Failed to restore SubElements for " << parent->getFullName()); |
| 2250 |
} |
| 2251 |
else if (!xlink->getValue()) { |
| 2252 |
FC_ERR("Discard SubElements of " << parent->getFullName() << " due to null link"); |
| 2253 |
} |
| 2254 |
else if (xlink->getSubValues().size() > 1) { |
| 2255 |
FC_ERR("Failed to restore SubElements for " << parent->getFullName() |
| 2256 |
<< " due to conflict subnames"); |
| 2257 |
} |
| 2258 |
else if (xlink->getSubValues().empty()) { |
| 2259 |
auto subs = xlink->getSubValues(); |
| 2260 |
xlink->setSubValues(std::move(subs)); |
| 2261 |
} |
| 2262 |
else { |
| 2263 |
std::set<std::string> subset(mySubElements.begin(), mySubElements.end()); |
| 2264 |
auto sub = xlink->getSubValues().front(); |
| 2265 |
auto element = Data::findElementName(sub.c_str()); |
| 2266 |
if (!Base::Tools::isNullOrEmpty(element)) { |
| 2267 |
subset.insert(element); |
| 2268 |
sub.resize(element - sub.c_str()); |
| 2269 |
} |
| 2270 |
std::vector<std::string> subs; |
| 2271 |
for (const auto& s : subset) { |
| 2272 |
subs.push_back(sub + s); |
| 2273 |
} |
| 2274 |
xlink->setSubValues(std::move(subs)); |
| 2275 |
} |
| 2276 |
} |
| 2277 |
if (getScaleVectorProperty() && getScaleProperty()) { |
| 2278 |
// Scale vector is added later. The code here is for migration. |
| 2279 |
const auto& v = getScaleVectorValue(); |
| 2280 |
double s = getScaleValue(); |
| 2281 |
if (v.x == v.y && v.x == v.z && v.x != s) { |
| 2282 |
getScaleVectorProperty()->setValue(s, s, s); |
| 2283 |
} |
| 2284 |
} |
| 2285 |
update(parent, getVisibilityListProperty()); |
| 2286 |
if (auto prop = getLinkedObjectProperty()) { |
| 2287 |
Base::StateLocker guard(pauseCopyOnChange); |
| 2288 |
update(parent, prop); |
| 2289 |
} |
| 2290 |
update(parent, getLinkCopyOnChangeSourceProperty()); |
| 2291 |
update(parent, getElementListProperty()); |
| 2292 |
if (getLinkCopyOnChangeValue() != CopyOnChangeDisabled) { |
| 2293 |
monitorOnChangeCopyObjects(getOnChangeCopyObjects()); |
| 2294 |
} |
| 2295 |
} |
| 2296 |
|
| 2297 |
void LinkBaseExtension::_handleChangedPropertyName(Base::XMLReader& reader, |
| 2298 |
const char* TypeName, |
| 2299 |
const char* PropName) |
| 2300 |
{ |
| 2301 |
if (strcmp(PropName, "SubElements") == 0 |
| 2302 |
&& strcmp(TypeName, PropertyStringList::getClassTypeId().getName()) == 0) { |
| 2303 |
PropertyStringList prop; |
| 2304 |
prop.setContainer(getContainer()); |
| 2305 |
prop.Restore(reader); |
| 2306 |
if (prop.getSize()) { |
| 2307 |
mySubElements = prop.getValues(); |
| 2308 |
hasOldSubElement = true; |
| 2309 |
} |
| 2310 |
} |
| 2311 |
} |
| 2312 |
|
| 2313 |
void LinkBaseExtension::setLink(int index, |
| 2314 |
DocumentObject* obj, |
| 2315 |
const char* subname, |
| 2316 |
const std::vector<std::string>& subElements) |
| 2317 |
{ |
| 2318 |
auto parent = getContainer(); |
| 2319 |
if (!parent) { |
| 2320 |
LINK_THROW(Base::RuntimeError, "No parent container"); |
| 2321 |
} |
| 2322 |
|
| 2323 |
if (obj && !App::Document::isAnyRestoring()) { |
| 2324 |
auto inSet = parent->getInListEx(true); |
| 2325 |
inSet.insert(parent); |
| 2326 |
if (inSet.find(obj) != inSet.end()) { |
| 2327 |
LINK_THROW(Base::RuntimeError, "Cyclic dependency"); |
| 2328 |
} |
| 2329 |
} |
| 2330 |
|
| 2331 |
auto linkProp = getLinkedObjectProperty(); |
| 2332 |
|
| 2333 |
// If we are a group (i.e. no LinkObject property), and the index is |
| 2334 |
// negative with a non-zero 'obj' assignment, we treat this as group |
| 2335 |
// expansion by changing the index to one pass the existing group size |
| 2336 |
if (index < 0 && obj && !linkProp && getElementListProperty()) { |
| 2337 |
index = getElementListProperty()->getSize(); |
| 2338 |
} |
| 2339 |
|
| 2340 |
if (index >= 0) { |
| 2341 |
// LinkGroup assignment |
| 2342 |
|
| 2343 |
if (linkProp || !getElementListProperty()) { |
| 2344 |
LINK_THROW(Base::RuntimeError, "Cannot set link element"); |
| 2345 |
} |
| 2346 |
|
| 2347 |
DocumentObject* old = nullptr; |
| 2348 |
const auto& elements = getElementListProperty()->getValues(); |
| 2349 |
if (!obj) { |
| 2350 |
if (index >= (int)elements.size()) { |
| 2351 |
LINK_THROW(Base::ValueError, "Link element index out of bound"); |
| 2352 |
} |
| 2353 |
std::vector<DocumentObject*> objs; |
| 2354 |
old = elements[index]; |
| 2355 |
for (int i = 0; i < (int)elements.size(); ++i) { |
| 2356 |
if (i != index) { |
| 2357 |
objs.push_back(elements[i]); |
| 2358 |
} |
| 2359 |
} |
| 2360 |
getElementListProperty()->setValue(objs); |
| 2361 |
} |
| 2362 |
else if (!obj->isAttachedToDocument()) { |
| 2363 |
LINK_THROW(Base::ValueError, "Invalid object"); |
| 2364 |
} |
| 2365 |
else { |
| 2366 |
if (index > (int)elements.size()) { |
| 2367 |
LINK_THROW(Base::ValueError, "Link element index out of bound"); |
| 2368 |
} |
| 2369 |
|
| 2370 |
if (index < (int)elements.size()) { |
| 2371 |
old = elements[index]; |
| 2372 |
} |
| 2373 |
|
| 2374 |
int idx = -1; |
| 2375 |
if (getLinkModeValue() >= LinkModeAutoLink || !Base::Tools::isNullOrEmpty(subname) |
| 2376 |
|| !subElements.empty() || obj->getDocument() != parent->getDocument() |
| 2377 |
|| (getElementListProperty()->find(obj->getNameInDocument(), &idx) |
| 2378 |
&& idx != index)) { |
| 2379 |
std::string name = parent->getDocument()->getUniqueObjectName("Link"); |
| 2380 |
auto link = new Link; |
| 2381 |
link->_LinkOwner.setValue(parent->getID()); |
| 2382 |
parent->getDocument()->addObject(link, name.c_str()); |
| 2383 |
link->setLink(-1, obj, subname, subElements); |
| 2384 |
auto linked = link->getTrueLinkedObject(true); |
| 2385 |
if (linked) { |
| 2386 |
link->Label.setValue(linked->Label.getValue()); |
| 2387 |
} |
| 2388 |
auto pla = |
| 2389 |
freecad_cast<PropertyPlacement*>(obj->getPropertyByName("Placement")); |
| 2390 |
if (pla) { |
| 2391 |
link->Placement.setValue(pla->getValue()); |
| 2392 |
} |
| 2393 |
link->Visibility.setValue(false); |
| 2394 |
obj = link; |
| 2395 |
} |
| 2396 |
|
| 2397 |
if (old == obj) { |
| 2398 |
return; |
| 2399 |
} |
| 2400 |
|
| 2401 |
getElementListProperty()->set1Value(index, obj); |
| 2402 |
} |
| 2403 |
detachElement(old); |
| 2404 |
return; |
| 2405 |
} |
| 2406 |
|
| 2407 |
if (!linkProp) { |
| 2408 |
// Reaching here means, we are group (i.e. no LinkedObject), and |
| 2409 |
// index<0, and 'obj' is zero. We shall clear the whole group |
| 2410 |
|
| 2411 |
if (obj || !getElementListProperty()) { |
| 2412 |
LINK_THROW(Base::RuntimeError, "No PropertyLink or PropertyLinkList configured"); |
| 2413 |
} |
| 2414 |
detachElements(); |
| 2415 |
return; |
| 2416 |
} |
| 2417 |
|
| 2418 |
// Here means we are assigning a Link |
| 2419 |
|
| 2420 |
auto xlink = freecad_cast<PropertyXLink*>(linkProp); |
| 2421 |
if (obj) { |
| 2422 |
if (!obj->isAttachedToDocument()) { |
| 2423 |
LINK_THROW(Base::ValueError, "Invalid document object"); |
| 2424 |
} |
| 2425 |
if (!xlink) { |
| 2426 |
if (parent && obj->getDocument() != parent->getDocument()) { |
| 2427 |
LINK_THROW(Base::ValueError, |
| 2428 |
"Cannot link to external object without PropertyXLink"); |
| 2429 |
} |
| 2430 |
} |
| 2431 |
} |
| 2432 |
|
| 2433 |
if (!xlink) { |
| 2434 |
if (!subElements.empty() || !Base::Tools::isNullOrEmpty(subname)) { |
| 2435 |
LINK_THROW(Base::RuntimeError, "SubName/SubElement link requires PropertyXLink"); |
| 2436 |
} |
| 2437 |
linkProp->setValue(obj); |
| 2438 |
return; |
| 2439 |
} |
| 2440 |
|
| 2441 |
std::vector<std::string> subs; |
| 2442 |
if (!subElements.empty()) { |
| 2443 |
subs.reserve(subElements.size()); |
| 2444 |
for (const auto& s : subElements) { |
| 2445 |
subs.emplace_back(subname ? subname : ""); |
| 2446 |
subs.back() += s; |
| 2447 |
} |
| 2448 |
} |
| 2449 |
else if (!Base::Tools::isNullOrEmpty(subname)) { |
| 2450 |
subs.emplace_back(subname); |
| 2451 |
} |
| 2452 |
xlink->setValue(obj, std::move(subs)); |
| 2453 |
} |
| 2454 |
|
| 2455 |
void LinkBaseExtension::detachElements() |
| 2456 |
{ |
| 2457 |
std::vector<App::DocumentObjectT> objs; |
| 2458 |
for (auto obj : getElementListValue()) { |
| 2459 |
objs.emplace_back(obj); |
| 2460 |
} |
| 2461 |
getElementListProperty()->setValue(); |
| 2462 |
for (const auto& objT : objs) { |
| 2463 |
detachElement(objT.getObject()); |
| 2464 |
} |
| 2465 |
} |
| 2466 |
|
| 2467 |
void LinkBaseExtension::detachElement(DocumentObject* obj) |
| 2468 |
{ |
| 2469 |
if (!obj || !obj->isAttachedToDocument() || obj->isRemoving()) { |
| 2470 |
return; |
| 2471 |
} |
| 2472 |
auto ext = obj->getExtensionByType<LinkBaseExtension>(true); |
| 2473 |
auto owner = getContainer(); |
| 2474 |
long ownerID = owner ? owner->getID() : 0; |
| 2475 |
if (getLinkModeValue() == LinkModeAutoUnlink) { |
| 2476 |
if (!ext || ext->_LinkOwner.getValue() != ownerID) { |
| 2477 |
return; |
| 2478 |
} |
| 2479 |
} |
| 2480 |
else if (getLinkModeValue() != LinkModeAutoDelete) { |
| 2481 |
if (ext && ext->_LinkOwner.getValue() == ownerID) { |
| 2482 |
ext->_LinkOwner.setValue(0); |
| 2483 |
} |
| 2484 |
return; |
| 2485 |
} |
| 2486 |
obj->getDocument()->removeObject(obj->getNameInDocument()); |
| 2487 |
} |
| 2488 |
|
| 2489 |
std::vector<App::DocumentObject*> LinkBaseExtension::getLinkedChildren(bool filter) const |
| 2490 |
{ |
| 2491 |
if (!filter) { |
| 2492 |
return _getElementListValue(); |
| 2493 |
} |
| 2494 |
std::vector<App::DocumentObject*> ret; |
| 2495 |
for (auto o : _getElementListValue()) { |
| 2496 |
if (!o->hasExtension(GroupExtension::getExtensionClassTypeId(), false)) { |
| 2497 |
ret.push_back(o); |
| 2498 |
} |
| 2499 |
} |
| 2500 |
return ret; |
| 2501 |
} |
| 2502 |
|
| 2503 |
const char* LinkBaseExtension::flattenSubname(const char* subname) const |
| 2504 |
{ |
| 2505 |
if (subname && _ChildCache.getSize()) { |
| 2506 |
const char* sub = subname; |
| 2507 |
std::string s; |
| 2508 |
for (const char* dot = strchr(sub, '.'); dot; sub = dot + 1, dot = strchr(sub, '.')) { |
| 2509 |
DocumentObject* obj = nullptr; |
| 2510 |
s.clear(); |
| 2511 |
s.append(sub, dot + 1); |
| 2512 |
extensionGetSubObject(obj, s.c_str()); |
| 2513 |
if (!obj) { |
| 2514 |
break; |
| 2515 |
} |
| 2516 |
if (!obj->hasExtension(GroupExtension::getExtensionClassTypeId(), false)) { |
| 2517 |
return sub; |
| 2518 |
} |
| 2519 |
} |
| 2520 |
} |
| 2521 |
return subname; |
| 2522 |
} |
| 2523 |
|
| 2524 |
void LinkBaseExtension::expandSubname(std::string& subname) const |
| 2525 |
{ |
| 2526 |
if (!_ChildCache.getSize()) { |
| 2527 |
return; |
| 2528 |
} |
| 2529 |
|
| 2530 |
const char* pos = nullptr; |
| 2531 |
int index = getElementIndex(subname.c_str(), &pos); |
| 2532 |
if (index < 0) { |
| 2533 |
return; |
| 2534 |
} |
| 2535 |
std::ostringstream ss; |
| 2536 |
elementNameFromIndex(index, ss); |
| 2537 |
ss << pos; |
| 2538 |
subname = ss.str(); |
| 2539 |
} |
| 2540 |
|
| 2541 |
static bool isExcludedProperties(const char* name) |
| 2542 |
{ |
| 2543 |
#define CHECK_EXCLUDE_PROP(_name) \ |
| 2544 |
if (strcmp(name, #_name) == 0) \ |
| 2545 |
return true; |
| 2546 |
CHECK_EXCLUDE_PROP(Shape); |
| 2547 |
CHECK_EXCLUDE_PROP(Proxy); |
| 2548 |
CHECK_EXCLUDE_PROP(Placement); |
| 2549 |
return false; |
| 2550 |
} |
| 2551 |
|
| 2552 |
Property* LinkBaseExtension::extensionGetPropertyByName(const char* name) const |
| 2553 |
{ |
| 2554 |
if (checkingProperty) { |
| 2555 |
return inherited::extensionGetPropertyByName(name); |
| 2556 |
} |
| 2557 |
Base::StateLocker guard(checkingProperty); |
| 2558 |
if (isExcludedProperties(name)) { |
| 2559 |
return nullptr; |
| 2560 |
} |
| 2561 |
auto owner = getContainer(); |
| 2562 |
if (owner) { |
| 2563 |
App::Property* prop = owner->getPropertyByName(name); |
| 2564 |
if (prop) { |
| 2565 |
return prop; |
| 2566 |
} |
| 2567 |
if (owner->canLinkProperties()) { |
| 2568 |
auto linked = getTrueLinkedObject(true); |
| 2569 |
if (linked) { |
| 2570 |
return linked->getPropertyByName(name); |
| 2571 |
} |
| 2572 |
} |
| 2573 |
} |
| 2574 |
return nullptr; |
| 2575 |
} |
| 2576 |
|
| 2577 |
bool LinkBaseExtension::isLinkMutated() const |
| 2578 |
{ |
| 2579 |
return getLinkCopyOnChangeValue() != CopyOnChangeDisabled && getLinkedObjectValue() |
| 2580 |
&& (!getLinkCopyOnChangeSourceValue() |
| 2581 |
|| (getLinkedObjectValue() != getLinkCopyOnChangeSourceValue())); |
| 2582 |
} |
| 2583 |
|
| 2584 |
/////////////////////////////////////////////////////////////////////////////////////////// |
| 2585 |
|
| 2586 |
namespace App |
| 2587 |
{ |
| 2588 |
EXTENSION_PROPERTY_SOURCE_TEMPLATE(App::LinkBaseExtensionPython, App::LinkBaseExtension) |
| 2589 |
|
| 2590 |
// explicit template instantiation |
| 2591 |
template class AppExport ExtensionPythonT<LinkBaseExtension>; |
| 2592 |
|
| 2593 |
} // namespace App |
| 2594 |
|
| 2595 |
////////////////////////////////////////////////////////////////////////////// |
| 2596 |
|
| 2597 |
EXTENSION_PROPERTY_SOURCE(App::LinkExtension, App::LinkBaseExtension) |
| 2598 |
|
| 2599 |
LinkExtension::LinkExtension() |
| 2600 |
{ |
| 2601 |
initExtensionType(LinkExtension::getExtensionClassTypeId()); |
| 2602 |
|
| 2603 |
LINK_PROPS_ADD_EXTENSION(LINK_PARAMS_EXT); |
| 2604 |
} |
| 2605 |
|
| 2606 |
/////////////////////////////////////////////////////////////////////////////////////////// |
| 2607 |
|
| 2608 |
namespace App |
| 2609 |
{ |
| 2610 |
EXTENSION_PROPERTY_SOURCE_TEMPLATE(App::LinkExtensionPython, App::LinkExtension) |
| 2611 |
|
| 2612 |
// explicit template instantiation |
| 2613 |
template class AppExport ExtensionPythonT<App::LinkExtension>; |
| 2614 |
|
| 2615 |
} // namespace App |
| 2616 |
|
| 2617 |
/////////////////////////////////////////////////////////////////////////////////////////// |
| 2618 |
|
| 2619 |
PROPERTY_SOURCE_WITH_EXTENSIONS(App::Link, App::DocumentObject) |
| 2620 |
|
| 2621 |
Link::Link() |
| 2622 |
{ |
| 2623 |
LINK_PROPS_ADD(LINK_PARAMS_LINK); |
| 2624 |
LinkExtension::initExtension(this); |
| 2625 |
static const PropertyIntegerConstraint::Constraints s_constraints = { |
| 2626 |
0, std::numeric_limits<int>::max(), 1}; |
| 2627 |
ElementCount.setConstraints(&s_constraints); |
| 2628 |
} |
| 2629 |
|
| 2630 |
bool Link::canLinkProperties() const |
| 2631 |
{ |
| 2632 |
return true; |
| 2633 |
} |
| 2634 |
|
| 2635 |
Base::Placement Link::getPlacementOf(const std::string& sub, DocumentObject* targetObj) |
| 2636 |
{ |
| 2637 |
if (isLinkGroup() && _getShowElementValue()) { |
| 2638 |
// In this case we have child document objects, the subname can be used. |
| 2639 |
// and in this case the Link itself acts as a normal DocumentObject |
| 2640 |
return DocumentObject::getPlacementOf(sub, targetObj); |
| 2641 |
} |
| 2642 |
|
| 2643 |
Base::Placement plc; |
| 2644 |
auto* propPlacement = dynamic_cast<App::PropertyPlacement*>(getPropertyByName("Placement")); |
| 2645 |
if (!propPlacement) { |
| 2646 |
return plc; |
| 2647 |
} |
| 2648 |
plc = propPlacement->getValue(); |
| 2649 |
|
| 2650 |
std::vector<std::string> names = Base::Tools::splitSubName(sub); |
| 2651 |
|
| 2652 |
if (names.empty() || this == targetObj || !getLinkedObject()) { |
| 2653 |
return plc; |
| 2654 |
} |
| 2655 |
|
| 2656 |
Document* doc = getLinkedObject()->getDocument(); |
| 2657 |
|
| 2658 |
if (isLinkGroup()) { |
| 2659 |
// !ShowElement, so there are no objects. |
| 2660 |
// The subname looks like : '1.pad.face3' or '1.face3' |
| 2661 |
// So names.front() is supposed to be an integer. If not something is wrong. |
| 2662 |
int i = -1; |
| 2663 |
try { |
| 2664 |
i = std::stoi(names.front()); |
| 2665 |
} |
| 2666 |
catch (...) { // Conversion failed (not an integer) |
| 2667 |
return plc; |
| 2668 |
} |
| 2669 |
|
| 2670 |
std::vector<Base::Placement> plcs = PlacementList.getValues(); |
| 2671 |
if (plcs.size() <= static_cast<size_t>(i)) { |
| 2672 |
return plc; |
| 2673 |
} |
| 2674 |
plc = plc * plcs[i]; |
| 2675 |
|
| 2676 |
// now that we handled the non-existing LinkElement, we can remove this subname and continue. |
| 2677 |
if (names.size() < 2) { // Subname was just "1", we are done. |
| 2678 |
return plc; |
| 2679 |
} |
| 2680 |
|
| 2681 |
DocumentObject* subObj = doc->getObject(names[1].c_str()); |
| 2682 |
if (!subObj) { |
| 2683 |
return plc; |
| 2684 |
} |
| 2685 |
|
| 2686 |
std::vector<std::string> newNames(names.begin() + 2, names.end()); |
| 2687 |
std::string newSub = Base::Tools::joinList(newNames, "."); |
| 2688 |
|
| 2689 |
return plc * subObj->getPlacementOf(newSub, targetObj); |
| 2690 |
} |
| 2691 |
|
| 2692 |
// case of a normal link |
| 2693 |
DocumentObject* subObj = doc->getObject(names.front().c_str()); |
| 2694 |
|
| 2695 |
if (!subObj) { |
| 2696 |
return plc; |
| 2697 |
} |
| 2698 |
|
| 2699 |
std::vector<std::string> newNames(names.begin() + 1, names.end()); |
| 2700 |
std::string newSub = Base::Tools::joinList(newNames, "."); |
| 2701 |
|
| 2702 |
return plc * subObj->getPlacementOf(newSub, targetObj); |
| 2703 |
} |
| 2704 |
|
| 2705 |
bool Link::isLink() const |
| 2706 |
{ |
| 2707 |
return ElementCount.getValue() == 0; |
| 2708 |
} |
| 2709 |
|
| 2710 |
bool Link::isLinkGroup() const |
| 2711 |
{ |
| 2712 |
return ElementCount.getValue() > 0; |
| 2713 |
} |
| 2714 |
|
| 2715 |
////////////////////////////////////////////////////////////////////////////////////////// |
| 2716 |
|
| 2717 |
namespace App |
| 2718 |
{ |
| 2719 |
PROPERTY_SOURCE_TEMPLATE(App::LinkPython, App::Link) |
| 2720 |
template<> |
| 2721 |
const char* App::LinkPython::getViewProviderName() const |
| 2722 |
{ |
| 2723 |
return "Gui::ViewProviderLinkPython"; |
| 2724 |
} |
| 2725 |
template class AppExport FeaturePythonT<App::Link>; |
| 2726 |
} // namespace App |
| 2727 |
|
| 2728 |
////////////////////////////////////////////////////////////////////////////////////////// |
| 2729 |
|
| 2730 |
PROPERTY_SOURCE_WITH_EXTENSIONS(App::LinkElement, App::DocumentObject) |
| 2731 |
|
| 2732 |
LinkElement::LinkElement() |
| 2733 |
{ |
| 2734 |
LINK_PROPS_ADD(LINK_PARAMS_ELEMENT); |
| 2735 |
LinkBaseExtension::initExtension(this); |
| 2736 |
} |
| 2737 |
|
| 2738 |
bool LinkElement::canDelete() const |
| 2739 |
{ |
| 2740 |
if (!_LinkOwner.getValue()) { |
| 2741 |
return true; |
| 2742 |
} |
| 2743 |
|
| 2744 |
auto owner = getContainer(); |
| 2745 |
return !owner || !owner->getDocument()->getObjectByID(_LinkOwner.getValue()); |
| 2746 |
} |
| 2747 |
|
| 2748 |
bool LinkElement::isLink() const |
| 2749 |
{ |
| 2750 |
return true; |
| 2751 |
} |
| 2752 |
|
| 2753 |
App::Link* LinkElement::getLinkGroup() const |
| 2754 |
{ |
| 2755 |
std::vector<App::DocumentObject*> inList = getInList(); |
| 2756 |
for (auto* obj : inList) { |
| 2757 |
auto* link = dynamic_cast<App::Link*>(obj); |
| 2758 |
if (!link) { |
| 2759 |
continue; |
| 2760 |
} |
| 2761 |
std::vector<App::DocumentObject*> elts = link->ElementList.getValues(); |
| 2762 |
for (auto* elt : elts) { |
| 2763 |
if (elt == this) { |
| 2764 |
return link; |
| 2765 |
} |
| 2766 |
} |
| 2767 |
} |
| 2768 |
return nullptr; |
| 2769 |
} |
| 2770 |
|
| 2771 |
Base::Placement LinkElement::getPlacementOf(const std::string& sub, DocumentObject* targetObj) |
| 2772 |
{ |
| 2773 |
Base::Placement plc; |
| 2774 |
auto* propPlacement = dynamic_cast<App::PropertyPlacement*>(getPropertyByName("Placement")); |
| 2775 |
if (!propPlacement) { |
| 2776 |
return plc; |
| 2777 |
} |
| 2778 |
plc = propPlacement->getValue(); |
| 2779 |
|
| 2780 |
std::vector<std::string> names = Base::Tools::splitSubName(sub); |
| 2781 |
|
| 2782 |
if (names.empty() || this == targetObj || !getLinkedObject()) { |
| 2783 |
return plc; |
| 2784 |
} |
| 2785 |
|
| 2786 |
Document* doc = getLinkedObject()->getDocument(); |
| 2787 |
|
| 2788 |
DocumentObject* subObj = doc->getObject(names.front().c_str()); |
| 2789 |
|
| 2790 |
if (!subObj) { |
| 2791 |
return plc; |
| 2792 |
} |
| 2793 |
|
| 2794 |
std::vector<std::string> newNames(names.begin() + 1, names.end()); |
| 2795 |
std::string newSub = Base::Tools::joinList(newNames, "."); |
| 2796 |
|
| 2797 |
return plc * subObj->getPlacementOf(newSub, targetObj); |
| 2798 |
} |
| 2799 |
|
| 2800 |
////////////////////////////////////////////////////////////////////////////////////////// |
| 2801 |
|
| 2802 |
namespace App |
| 2803 |
{ |
| 2804 |
PROPERTY_SOURCE_TEMPLATE(App::LinkElementPython, App::LinkElement) |
| 2805 |
template<> |
| 2806 |
const char* App::LinkElementPython::getViewProviderName() const |
| 2807 |
{ |
| 2808 |
return "Gui::ViewProviderLinkPython"; |
| 2809 |
} |
| 2810 |
template class AppExport FeaturePythonT<App::LinkElement>; |
| 2811 |
} // namespace App |
| 2812 |
|
| 2813 |
////////////////////////////////////////////////////////////////////////////////////////// |
| 2814 |
|
| 2815 |
PROPERTY_SOURCE_WITH_EXTENSIONS(App::LinkGroup, App::DocumentObject) |
| 2816 |
|
| 2817 |
LinkGroup::LinkGroup() |
| 2818 |
{ |
| 2819 |
LINK_PROPS_ADD(LINK_PARAMS_GROUP); |
| 2820 |
LinkBaseExtension::initExtension(this); |
| 2821 |
} |
| 2822 |
|
| 2823 |
////////////////////////////////////////////////////////////////////////////////////////// |
| 2824 |
|
| 2825 |
namespace App |
| 2826 |
{ |
| 2827 |
PROPERTY_SOURCE_TEMPLATE(App::LinkGroupPython, App::LinkGroup) |
| 2828 |
template<> |
| 2829 |
const char* App::LinkGroupPython::getViewProviderName() const |
| 2830 |
{ |
| 2831 |
return "Gui::ViewProviderLinkPython"; |
| 2832 |
} |
| 2833 |
template class AppExport FeaturePythonT<App::LinkGroup>; |
| 2834 |
} // namespace App |
| 2835 |
|
| 2836 |
#if defined(__clang__) |
| 2837 |
#pragma clang diagnostic pop |
| 2838 |
#endif |