| 1 |
/*************************************************************************** |
| 2 |
* Copyright (c) 2011 Jürgen Riegel <[email protected]> * |
| 3 |
* Copyright (c) 2011 Werner Mayer <wmayer[at]users.sourceforge.net> * |
| 4 |
* * |
| 5 |
* This file is part of the FreeCAD CAx development system. * |
| 6 |
* * |
| 7 |
* This library is free software; you can redistribute it and/or * |
| 8 |
* modify it under the terms of the GNU Library General Public * |
| 9 |
* License as published by the Free Software Foundation; either * |
| 10 |
* version 2 of the License, or (at your option) any later version. * |
| 11 |
* * |
| 12 |
* This library is distributed in the hope that it will be useful, * |
| 13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 15 |
* GNU Library General Public License for more details. * |
| 16 |
* * |
| 17 |
* You should have received a copy of the GNU Library General Public * |
| 18 |
* License along with this library; see the file COPYING.LIB. If not, * |
| 19 |
* write to the Free Software Foundation, Inc., 59 Temple Place, * |
| 20 |
* Suite 330, Boston, MA 02111-1307, USA * |
| 21 |
* * |
| 22 |
***************************************************************************/ |
| 23 |
|
| 24 |
|
| 25 |
#ifndef SRC_APP_DOCUMENTOBJECT_H_ |
| 26 |
#define SRC_APP_DOCUMENTOBJECT_H_ |
| 27 |
|
| 28 |
#include <App/TransactionalObject.h> |
| 29 |
#include <App/PropertyExpressionEngine.h> |
| 30 |
#include <App/PropertyGeo.h> |
| 31 |
#include <App/PropertyLinks.h> |
| 32 |
#include <App/PropertyStandard.h> |
| 33 |
#include <Base/SmartPtrPy.h> |
| 34 |
#include <Base/Placement.h> |
| 35 |
|
| 36 |
#include <bitset> |
| 37 |
#include <unordered_map> |
| 38 |
#include <memory> |
| 39 |
#include <map> |
| 40 |
#include <set> |
| 41 |
#include <string> |
| 42 |
#include <vector> |
| 43 |
|
| 44 |
namespace Base |
| 45 |
{ |
| 46 |
class Matrix4D; |
| 47 |
} |
| 48 |
|
| 49 |
namespace App |
| 50 |
{ |
| 51 |
class Document; |
| 52 |
class DocumentObjectGroup; |
| 53 |
class DocumentObjectPy; |
| 54 |
class Expression; |
| 55 |
|
| 56 |
// clang-format off |
| 57 |
enum ObjectStatus |
| 58 |
{ |
| 59 |
Touch = 0, |
| 60 |
Error = 1, |
| 61 |
New = 2, |
| 62 |
Recompute = 3, // set when the object is currently being recomputed |
| 63 |
Restore = 4, |
| 64 |
Remove = 5, |
| 65 |
PythonCall = 6, |
| 66 |
Destroy = 7, |
| 67 |
Enforce = 8, |
| 68 |
Recompute2 = 9, // set when the object is being recomputed in the second pass |
| 69 |
PartialObject = 10, |
| 70 |
PendingRecompute = 11, // set by Document, indicating the object is in recomputation queue |
| 71 |
ObjImporting = 13, // Mark the object as importing |
| 72 |
NoTouch = 14, // no touch on any property change |
| 73 |
GeoExcluded = 15, // mark as a member but not claimed by GeoFeatureGroup |
| 74 |
Expand = 16, // indicate the object's tree item expansion status |
| 75 |
NoAutoExpand = 17, // disable tree item auto expand on selection for this object |
| 76 |
PendingTransactionUpdate = 18, // mark that the object expects a call to onUndoRedoFinished() after transaction is finished. |
| 77 |
RecomputeExtension = 19, // mark the object to recompute its extensions |
| 78 |
TouchOnColorChange = 20, // inform view provider touch object on color change |
| 79 |
Freeze = 21, // do not recompute ever |
| 80 |
}; |
| 81 |
// clang-format on |
| 82 |
|
| 83 |
/** Return object for feature execution |
| 84 |
*/ |
| 85 |
class AppExport DocumentObjectExecReturn |
| 86 |
{ |
| 87 |
public: |
| 88 |
explicit DocumentObjectExecReturn(const std::string& sWhy, |
| 89 |
DocumentObject* WhichObject = nullptr) |
| 90 |
: Why(sWhy) |
| 91 |
, Which(WhichObject) |
| 92 |
{} |
| 93 |
explicit DocumentObjectExecReturn(const char* sWhy, DocumentObject* WhichObject = nullptr) |
| 94 |
: Which(WhichObject) |
| 95 |
{ |
| 96 |
if (sWhy) { |
| 97 |
Why = sWhy; |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
std::string Why; |
| 102 |
DocumentObject* Which; |
| 103 |
}; |
| 104 |
|
| 105 |
|
| 106 |
/** |
| 107 |
* @brief %Base class of all objects handled in the @ref App::Document "Document". |
| 108 |
* @ingroup DocObject |
| 109 |
* @details For a more high-level overview see topic @ref DocObject "Document Object". |
| 110 |
*/ |
| 111 |
class AppExport DocumentObject: public App::TransactionalObject |
| 112 |
{ |
| 113 |
PROPERTY_HEADER_WITH_OVERRIDE(App::DocumentObject); |
| 114 |
|
| 115 |
private: |
| 116 |
// store read-only property names at freeze |
| 117 |
// in order to retablish correct status at unfreeze |
| 118 |
std::vector<const char*> readOnlyProperties; |
| 119 |
|
| 120 |
public: |
| 121 |
PropertyString Label; |
| 122 |
PropertyString Label2; |
| 123 |
PropertyExpressionEngine ExpressionEngine; |
| 124 |
|
| 125 |
/// Allow control visibility status in App name space |
| 126 |
PropertyBool Visibility; |
| 127 |
|
| 128 |
// clang-format off |
| 129 |
/// signal before changing a property of this object |
| 130 |
boost::signals2::signal<void(const App::DocumentObject&, const App::Property&)> signalBeforeChange; |
| 131 |
/// signal on changed property of this object |
| 132 |
boost::signals2::signal<void(const App::DocumentObject&, const App::Property&)> signalChanged; |
| 133 |
/// signal on changed property of this object before document scoped signalChangedObject |
| 134 |
boost::signals2::signal<void(const App::DocumentObject&, const App::Property&)> signalEarlyChanged; |
| 135 |
// clang-format on |
| 136 |
|
| 137 |
/// returns the type name of the ViewProvider |
| 138 |
virtual const char* getViewProviderName() const |
| 139 |
{ |
| 140 |
return ""; |
| 141 |
} |
| 142 |
/** |
| 143 |
* This function is introduced to allow Python feature override its view provider. |
| 144 |
* The default implementation just returns \ref getViewProviderName(). |
| 145 |
* |
| 146 |
* The core will only accept the overridden view provider if it returns |
| 147 |
* true when calling Gui::ViewProviderDocumentObject::allowOverride(obj). |
| 148 |
* If not, the view provider will be reverted to the one returned from \ref |
| 149 |
* getViewProviderName(). |
| 150 |
*/ |
| 151 |
virtual const char* getViewProviderNameOverride() const |
| 152 |
{ |
| 153 |
return getViewProviderName(); |
| 154 |
} |
| 155 |
|
| 156 |
/// Constructor |
| 157 |
DocumentObject(); |
| 158 |
~DocumentObject() override; |
| 159 |
|
| 160 |
/// returns a value that uniquely identifies this DocumentObject. |
| 161 |
const char* getDagKey() const; |
| 162 |
/// returns the name which is set in the document for this object (not the name property!) |
| 163 |
const char* getNameInDocument() const; |
| 164 |
/// Return the object ID that is unique within its owner document |
| 165 |
long getID() const |
| 166 |
{ |
| 167 |
return _Id; |
| 168 |
} |
| 169 |
/// returns the name that is safe to be exported to other document |
| 170 |
std::string getExportName(bool forced = false) const; |
| 171 |
/// Return the object full name of the form DocName#ObjName |
| 172 |
std::string getFullName() const override; |
| 173 |
/// Return the object full label in the form DocName#ObjName |
| 174 |
std::string getFullLabel() const; |
| 175 |
bool isAttachedToDocument() const override; |
| 176 |
const char* detachFromDocument() override; |
| 177 |
/// gets the document in which this Object is handled |
| 178 |
App::Document* getDocument() const; |
| 179 |
|
| 180 |
/** Set the property touched -> changed, cause recomputation in Update() |
| 181 |
*/ |
| 182 |
//@{ |
| 183 |
/// set this document object touched (cause recomputation on dependent features) |
| 184 |
void touch(bool noRecompute = false); |
| 185 |
/// test if this document object is touched |
| 186 |
bool isTouched() const; |
| 187 |
/// Enforce this document object to be recomputed |
| 188 |
void enforceRecompute(); |
| 189 |
/// Test if this document object must be recomputed |
| 190 |
bool mustRecompute() const; |
| 191 |
/// reset this document object touched |
| 192 |
void purgeTouched() |
| 193 |
{ |
| 194 |
StatusBits.reset(ObjectStatus::Touch); |
| 195 |
StatusBits.reset(ObjectStatus::Enforce); |
| 196 |
setPropertyStatus(0, false); |
| 197 |
} |
| 198 |
/// set this feature to error |
| 199 |
bool isError() const |
| 200 |
{ |
| 201 |
return StatusBits.test(ObjectStatus::Error); |
| 202 |
} |
| 203 |
bool isValid() const |
| 204 |
{ |
| 205 |
return !StatusBits.test(ObjectStatus::Error); |
| 206 |
} |
| 207 |
/// remove the error from the object |
| 208 |
void purgeError() |
| 209 |
{ |
| 210 |
StatusBits.reset(ObjectStatus::Error); |
| 211 |
} |
| 212 |
/// returns true if this objects is currently recomputing |
| 213 |
bool isRecomputing() const |
| 214 |
{ |
| 215 |
return StatusBits.test(ObjectStatus::Recompute); |
| 216 |
} |
| 217 |
/// returns true if this objects is currently restoring from file |
| 218 |
bool isRestoring() const |
| 219 |
{ |
| 220 |
return StatusBits.test(ObjectStatus::Restore); |
| 221 |
} |
| 222 |
/// returns true if this objects is currently removed from the document |
| 223 |
bool isRemoving() const |
| 224 |
{ |
| 225 |
return StatusBits.test(ObjectStatus::Remove); |
| 226 |
} |
| 227 |
/// set this document object freezed (prevent recomputation) |
| 228 |
void freeze(); |
| 229 |
/// set this document object unfreezed (and touch it) |
| 230 |
void unfreeze(bool noRecompute = false); |
| 231 |
/// returns true if this objects is currently freezed |
| 232 |
bool isFreezed() const |
| 233 |
{ |
| 234 |
return StatusBits.test(ObjectStatus::Freeze); |
| 235 |
} |
| 236 |
/// return the status bits |
| 237 |
unsigned long getStatus() const |
| 238 |
{ |
| 239 |
return StatusBits.to_ulong(); |
| 240 |
} |
| 241 |
bool testStatus(ObjectStatus pos) const |
| 242 |
{ |
| 243 |
return StatusBits.test(size_t(pos)); |
| 244 |
} |
| 245 |
void setStatus(ObjectStatus pos, bool on) |
| 246 |
{ |
| 247 |
StatusBits.set(size_t(pos), on); |
| 248 |
} |
| 249 |
//@} |
| 250 |
|
| 251 |
int isExporting() const; |
| 252 |
|
| 253 |
/** Child element handling |
| 254 |
*/ |
| 255 |
//@{ |
| 256 |
/** Set sub-element visibility |
| 257 |
* |
| 258 |
* For performance reason, \c element must not contain any further |
| 259 |
* sub-elements, i.e. there should be no '.' inside \c element. |
| 260 |
* |
| 261 |
* @return -1 if element visibility is not supported, 0 if element is not |
| 262 |
* found, 1 if success |
| 263 |
*/ |
| 264 |
virtual int setElementVisible(const char* element, bool visible); |
| 265 |
|
| 266 |
/** Get sub-element visibility |
| 267 |
* |
| 268 |
* @return -1 if element visibility is not supported or element not found, 0 |
| 269 |
* if element is invisible, or else 1 |
| 270 |
*/ |
| 271 |
virtual int isElementVisible(const char* element) const; |
| 272 |
|
| 273 |
/// return true to activate tree view group object handling and element visibility |
| 274 |
virtual bool hasChildElement() const; |
| 275 |
//@} |
| 276 |
|
| 277 |
|
| 278 |
/** DAG handling |
| 279 |
This part of the interface deals with viewing the document as |
| 280 |
a DAG (directed acyclic graph). |
| 281 |
*/ |
| 282 |
//@{ |
| 283 |
/// OutList options |
| 284 |
enum OutListOption |
| 285 |
{ |
| 286 |
/// Do not include link from expression engine |
| 287 |
OutListNoExpression = 1, |
| 288 |
/// Do not hide any link (i.e. include links with LinkScopeHidden) |
| 289 |
OutListNoHidden = 2, |
| 290 |
/// Do not include link from PropertyXLink |
| 291 |
OutListNoXLinked = 4, |
| 292 |
}; |
| 293 |
/// returns a list of objects this object is pointing to by Links |
| 294 |
const std::vector<App::DocumentObject*>& getOutList() const; |
| 295 |
std::vector<App::DocumentObject*> getOutList(int option) const; |
| 296 |
void getOutList(int option, std::vector<App::DocumentObject*>& res) const; |
| 297 |
|
| 298 |
/// returns a list of objects linked by the property |
| 299 |
std::vector<App::DocumentObject*> getOutListOfProperty(App::Property*) const; |
| 300 |
/// returns a list of objects this object is pointing to by Links and all further descended |
| 301 |
std::vector<App::DocumentObject*> getOutListRecursive() const; |
| 302 |
/// clear internal out list cache |
| 303 |
void clearOutListCache() const; |
| 304 |
/// get all possible paths from this to another object following the OutList |
| 305 |
std::vector<std::list<App::DocumentObject*>> getPathsByOutList(App::DocumentObject* to) const; |
| 306 |
/// get all objects link to this object |
| 307 |
const std::vector<App::DocumentObject*>& getInList() const; |
| 308 |
/// get all objects link directly or indirectly to this object |
| 309 |
std::vector<App::DocumentObject*> getInListRecursive() const; |
| 310 |
/** Get a set of all objects linking to this object, including possible external parent objects |
| 311 |
* |
| 312 |
* @param inSet [out]: a set containing all objects linking to this object. |
| 313 |
* @param recursive [in]: whether to obtain recursive in list |
| 314 |
* @param inList [in, out]: optional pointer to a vector holding the output |
| 315 |
* objects, with the furthest linking object ordered last. |
| 316 |
*/ |
| 317 |
void getInListEx(std::set<App::DocumentObject*>& inSet, |
| 318 |
bool recursive, |
| 319 |
std::vector<App::DocumentObject*>* inList = nullptr) const; |
| 320 |
/** Return a set of all objects linking to this object, including possible external parent |
| 321 |
* objects |
| 322 |
* @param recursive [in]: whether to obtain recursive in list |
| 323 |
*/ |
| 324 |
std::set<App::DocumentObject*> getInListEx(bool recursive) const; |
| 325 |
|
| 326 |
/// get group if object is part of a group, otherwise 0 is returned |
| 327 |
DocumentObjectGroup* getGroup() const; |
| 328 |
|
| 329 |
/// test if this object is in the InList and recursive further down |
| 330 |
bool isInInListRecursive(DocumentObject* objToTest) const; |
| 331 |
/// test if this object is directly (non recursive) in the InList |
| 332 |
bool isInInList(DocumentObject* objToTest) const; |
| 333 |
/// test if the given object is in the OutList and recursive further down |
| 334 |
bool isInOutListRecursive(DocumentObject* objToTest) const; |
| 335 |
/// test if this object is directly (non recursive) in the OutList |
| 336 |
bool isInOutList(DocumentObject* objToTest) const; |
| 337 |
/// internal, used by PropertyLink to maintain DAG back links |
| 338 |
void _removeBackLink(DocumentObject*); |
| 339 |
/// internal, used by PropertyLink to maintain DAG back links |
| 340 |
void _addBackLink(DocumentObject*); |
| 341 |
//@} |
| 342 |
|
| 343 |
/** |
| 344 |
* @brief testIfLinkIsDAG tests a link that is about to be created for |
| 345 |
* circular references. |
| 346 |
* @param objToLinkIn (input). The object this object is to depend on after |
| 347 |
* the link is going to be created. |
| 348 |
* @return true if link can be created (no cycles will be made). False if |
| 349 |
* the link will cause a circular dependency and break recomputes. Throws an |
| 350 |
* error if the document already has a circular dependency. |
| 351 |
* That is, if the return is true, the link is allowed. |
| 352 |
*/ |
| 353 |
bool testIfLinkDAGCompatible(DocumentObject* linkTo) const; |
| 354 |
bool testIfLinkDAGCompatible(const std::vector<DocumentObject*>& linksTo) const; |
| 355 |
bool testIfLinkDAGCompatible(App::PropertyLinkSubList& linksTo) const; |
| 356 |
bool testIfLinkDAGCompatible(App::PropertyLinkSub& linkTo) const; |
| 357 |
|
| 358 |
/** Return the element map version of the geometry data stored in the given property |
| 359 |
* |
| 360 |
* @param prop: the geometry property to query for element map version |
| 361 |
* @param restored: whether to query for the restored element map version. |
| 362 |
* In case of version upgrade, the restored version may |
| 363 |
* be different from the current version. |
| 364 |
* |
| 365 |
* @return Return the element map version string. |
| 366 |
*/ |
| 367 |
virtual std::string getElementMapVersion(const App::Property* prop, |
| 368 |
bool restored = false) const; |
| 369 |
|
| 370 |
/// Return true to signal re-generation of geometry element names |
| 371 |
virtual bool checkElementMapVersion(const App::Property* prop, const char* ver) const; |
| 372 |
|
| 373 |
public: |
| 374 |
/** mustExecute |
| 375 |
* We call this method to check if the object was modified to |
| 376 |
* be invoked. If the object label or an argument is modified. |
| 377 |
* If we must recompute the object - to call the method execute(). |
| 378 |
* 0: no recomputation is needed |
| 379 |
* 1: recomputation needed |
| 380 |
* |
| 381 |
* @remark If an object is marked as 'touched' then this does not |
| 382 |
* necessarily mean that it will be recomputed. It only means that all |
| 383 |
* objects that link it (i.e. its InList) will be recomputed. |
| 384 |
*/ |
| 385 |
virtual short mustExecute() const; |
| 386 |
|
| 387 |
/** Recompute only this feature |
| 388 |
* |
| 389 |
* @param recursive: set to true to recompute any dependent objects as well |
| 390 |
*/ |
| 391 |
bool recomputeFeature(bool recursive = false); |
| 392 |
|
| 393 |
/// get the status Message |
| 394 |
const char* getStatusString() const; |
| 395 |
|
| 396 |
/** Called in case of losing a link |
| 397 |
* Get called by the document when a object got deleted a link property of this |
| 398 |
* object ist pointing to. The standard behaviour of the DocumentObject implementation |
| 399 |
* is to reset the links to nothing. You may override this method to implement |
| 400 |
* additional or different behavior. |
| 401 |
*/ |
| 402 |
virtual void onLostLinkToObject(DocumentObject*); |
| 403 |
PyObject* getPyObject() override; |
| 404 |
|
| 405 |
/** Get the sub element/object by name |
| 406 |
* |
| 407 |
* @param subname: a string which is dot separated name to refer to a sub |
| 408 |
* element or object. An empty string can be used to refer to the object |
| 409 |
* itself |
| 410 |
* |
| 411 |
* @param pyObj: if non zero, returns the python object corresponding to |
| 412 |
* this sub object. The actual type of this python object is implementation |
| 413 |
* dependent. For example, The current implementation of Part::Feature will |
| 414 |
* return the TopoShapePy, event if there is no sub-element reference, in |
| 415 |
* which case it returns the whole shape. |
| 416 |
* |
| 417 |
* @param mat: If non zero, it is used as the current transformation matrix |
| 418 |
* on input. And output as the accumulated transformation up until and |
| 419 |
* include the transformation applied by the final object reference in \c |
| 420 |
* subname. For Part::Feature, the transformation is applied to the |
| 421 |
* TopoShape inside \c pyObj before returning. |
| 422 |
* |
| 423 |
* @param transform: if false, then it will not apply the object's own |
| 424 |
* transformation to \c mat, which lets you override the object's placement |
| 425 |
* (and possibly scale). |
| 426 |
* |
| 427 |
* @param depth: depth limitation as hint for cyclic link detection |
| 428 |
* |
| 429 |
* @return The last document object referred in subname. If subname is empty, |
| 430 |
* then it shall return itself. If subname is invalid, then it shall return |
| 431 |
* zero. |
| 432 |
*/ |
| 433 |
virtual DocumentObject* getSubObject(const char* subname, |
| 434 |
PyObject** pyObj = nullptr, |
| 435 |
Base::Matrix4D* mat = nullptr, |
| 436 |
bool transform = true, |
| 437 |
int depth = 0) const; |
| 438 |
|
| 439 |
/** Return a list of objects referenced by a given subname including this object |
| 440 |
* @param subname: the sub name path |
| 441 |
* @param subsizes: optional sub name sizes for each returned object, that is, |
| 442 |
* ret[i] = getSubObject(std::string(subname, subsizes[i]).c_str()); |
| 443 |
* @param flatten: whether to flatten the object hierarchies that belong to |
| 444 |
* the same geo feature group, e.g. (Part.Fusion.Box -> Part.Box) |
| 445 |
* |
| 446 |
* @return Return a list of objects along the path. |
| 447 |
*/ |
| 448 |
std::vector<DocumentObject*> getSubObjectList(const char* subname, |
| 449 |
std::vector<int>* subsizes = nullptr, |
| 450 |
bool flatten = false) const; |
| 451 |
|
| 452 |
/// reason of calling getSubObjects() |
| 453 |
enum GSReason |
| 454 |
{ |
| 455 |
/// default, mostly for exporting shape objects |
| 456 |
GS_DEFAULT, |
| 457 |
/// for element selection |
| 458 |
GS_SELECT, |
| 459 |
}; |
| 460 |
|
| 461 |
/** Return name reference of all sub-objects |
| 462 |
* |
| 463 |
* @param reason: indicate reason of obtaining the sub objects |
| 464 |
* |
| 465 |
* The default implementation returns all object references in |
| 466 |
* PropertyLink, and PropertyLinkList, if any |
| 467 |
* |
| 468 |
* @return Return a vector of subname references for all sub-objects. In |
| 469 |
* most cases, the name returned will be the object name plus an ending |
| 470 |
* '.', which can be passed directly to getSubObject() to retrieve the |
| 471 |
* name. The reason to return the name reference instead of the sub object |
| 472 |
* itself is because there may be no real sub object, or the sub object |
| 473 |
* need special transformation. For example, sub objects of an array type |
| 474 |
* of object. |
| 475 |
*/ |
| 476 |
virtual std::vector<std::string> getSubObjects(int reason = 0) const; |
| 477 |
|
| 478 |
/// Obtain top parents and subnames of this object using its InList |
| 479 |
std::vector<std::pair<App::DocumentObject*, std::string>> getParents(int depth = 0) const; |
| 480 |
|
| 481 |
/// Obtain the first parent group of this object |
| 482 |
App::DocumentObject* getFirstParent() const; |
| 483 |
|
| 484 |
/** Return the linked object with optional transformation |
| 485 |
* |
| 486 |
* @param recurse: If false, return the immediate linked object, or else |
| 487 |
* recursively call this function to return the final linked object. |
| 488 |
* |
| 489 |
* @param mat: If non zero, it is used as the current transformation matrix |
| 490 |
* on input. And output as the accumulated transformation till the final |
| 491 |
* linked object. |
| 492 |
* |
| 493 |
* @param transform: if false, then it will not accumulate the object's own |
| 494 |
* placement into \c mat, which lets you override the object's placement. |
| 495 |
* |
| 496 |
* @return Return the linked object. This function must return itself if the |
| 497 |
* it is not a link or the link is invalid. |
| 498 |
*/ |
| 499 |
virtual DocumentObject* getLinkedObject(bool recurse = true, |
| 500 |
Base::Matrix4D* mat = nullptr, |
| 501 |
bool transform = false, |
| 502 |
int depth = 0) const; |
| 503 |
|
| 504 |
/* Return true to cause PropertyView to show linked object's property */ |
| 505 |
virtual bool canLinkProperties() const |
| 506 |
{ |
| 507 |
return true; |
| 508 |
} |
| 509 |
|
| 510 |
/* Return whether this object is a link */ |
| 511 |
virtual bool isLink() const |
| 512 |
{ |
| 513 |
return false; |
| 514 |
} |
| 515 |
|
| 516 |
/* Return whether this object is a link group */ |
| 517 |
virtual bool isLinkGroup() const |
| 518 |
{ |
| 519 |
return false; |
| 520 |
} |
| 521 |
|
| 522 |
/* Return true to bypass duplicate label checking */ |
| 523 |
virtual bool allowDuplicateLabel() const |
| 524 |
{ |
| 525 |
return false; |
| 526 |
} |
| 527 |
/// Handle Label changes, including forcing unique label values, |
| 528 |
/// signalling OnBeforeLabelChange, and arranging to update linked references, |
| 529 |
/// on the assumption that after returning the label will indeed be changed to |
| 530 |
/// the (altered) value of newLabel. |
| 531 |
/// Returns a vector of referenging (linking) properties as produced by |
| 532 |
/// PropertyLinkBase::updateLabelReferences which is needed for undo/redo purposes. |
| 533 |
std::vector<std::pair<Property*, std::unique_ptr<Property>>> |
| 534 |
onProposedLabelChange(std::string& newLabel); |
| 535 |
/*** Called to let object itself control relabeling |
| 536 |
* |
| 537 |
* @param newLabel: input as the new label, which can be modified by object itself |
| 538 |
* |
| 539 |
* This function is called before onBeforeChange() |
| 540 |
*/ |
| 541 |
virtual void onBeforeChangeLabel(std::string& newLabel) |
| 542 |
{ |
| 543 |
(void)newLabel; |
| 544 |
} |
| 545 |
|
| 546 |
friend class Document; |
| 547 |
friend class Transaction; |
| 548 |
friend class ObjectExecution; |
| 549 |
|
| 550 |
static DocumentObjectExecReturn* StdReturn; |
| 551 |
|
| 552 |
void Save(Base::Writer& writer) const override; |
| 553 |
|
| 554 |
/* Expression support */ |
| 555 |
|
| 556 |
virtual void setExpression(const ObjectIdentifier& path, std::shared_ptr<App::Expression> expr); |
| 557 |
|
| 558 |
void clearExpression(const ObjectIdentifier& path); |
| 559 |
|
| 560 |
virtual const PropertyExpressionEngine::ExpressionInfo |
| 561 |
getExpression(const ObjectIdentifier& path) const; |
| 562 |
|
| 563 |
virtual void |
| 564 |
renameObjectIdentifiers(const std::map<App::ObjectIdentifier, App::ObjectIdentifier>& paths); |
| 565 |
|
| 566 |
const std::string& getOldLabel() const |
| 567 |
{ |
| 568 |
return oldLabel; |
| 569 |
} |
| 570 |
|
| 571 |
const char* getViewProviderNameStored() const |
| 572 |
{ |
| 573 |
return _pcViewProviderName.c_str(); |
| 574 |
} |
| 575 |
|
| 576 |
bool removeDynamicProperty(const char* prop) override; |
| 577 |
|
| 578 |
bool renameDynamicProperty(Property *prop, const char *name) override; |
| 579 |
|
| 580 |
App::Property* addDynamicProperty(const char* type, |
| 581 |
const char* name = nullptr, |
| 582 |
const char* group = nullptr, |
| 583 |
const char* doc = nullptr, |
| 584 |
short attr = 0, |
| 585 |
bool ro = false, |
| 586 |
bool hidden = false) override; |
| 587 |
|
| 588 |
/** Resolve the last document object referenced in the subname |
| 589 |
* |
| 590 |
* @param subname: dot separated subname |
| 591 |
* @param parent: return the direct parent of the object |
| 592 |
* @param childName: return child name to be passed to is/setElementVisible() |
| 593 |
* @param subElement: return non-object sub-element name if found. The |
| 594 |
* pointer is guaranteed to be within the buffer pointed to by 'subname' |
| 595 |
* |
| 596 |
* @sa getSubObject() |
| 597 |
* @return Returns the last referenced document object in the subname. If no |
| 598 |
* such object in subname, return pObject. |
| 599 |
*/ |
| 600 |
App::DocumentObject* resolve(const char* subname, |
| 601 |
App::DocumentObject** parent = nullptr, |
| 602 |
std::string* childName = nullptr, |
| 603 |
const char** subElement = nullptr, |
| 604 |
PyObject** pyObj = nullptr, |
| 605 |
Base::Matrix4D* mat = nullptr, |
| 606 |
bool transform = true, |
| 607 |
int depth = 0) const; |
| 608 |
|
| 609 |
/** Resolve a link reference that is relative to this object reference |
| 610 |
* |
| 611 |
* @param subname: on input, this is the subname reference to the object |
| 612 |
* that is to be assigned a link. On output, the reference may be offset |
| 613 |
* to be rid of any common parent. |
| 614 |
* @param link: on input, this is the top parent of the link reference. On |
| 615 |
* output, it may be altered to one of its child to be rid off any common |
| 616 |
* parent. |
| 617 |
* @param linkSub: on input, this the subname of the link reference. On |
| 618 |
* output, it may be offset to be rid off any common parent. |
| 619 |
* |
| 620 |
* @return The corrected top parent of the object that is to be assigned the |
| 621 |
* link. If the output 'subname' is empty, then return the object itself. |
| 622 |
* |
| 623 |
* To avoid any cyclic reference, an object must not be assign a link to any |
| 624 |
* of the object in its parent. This function can be used to resolve any |
| 625 |
* common parents of an object and its link target. |
| 626 |
* |
| 627 |
* For example, with the following object hierarchy |
| 628 |
* |
| 629 |
* Group |
| 630 |
* |--Group001 |
| 631 |
* | |--Box |
| 632 |
* | |--Cylinder |
| 633 |
* |--Group002 |
| 634 |
* |--Box001 |
| 635 |
* |--Cylinder001 |
| 636 |
* |
| 637 |
* If you want add a link of Group.Group002.Box001 to Group.Group001, you |
| 638 |
* can call with the following parameter (which are usually obtained from |
| 639 |
* Selection.getSelectionEx(), check usage in TreeWidget::onDropEvent()): |
| 640 |
* std::string subname("Group002."); |
| 641 |
* auto link = Group; |
| 642 |
* std::string linkSub("Group001.Box001."); |
| 643 |
* parent = Group.resolveRelativeLink(subname,link,linkSub); |
| 644 |
* |
| 645 |
* The resolving result is as follow: |
| 646 |
* return -> Group001 |
| 647 |
* subname -> "" |
| 648 |
* link -> Group002 |
| 649 |
* linkSub -> "Box001." |
| 650 |
* |
| 651 |
* The common parent 'Group' is removed. |
| 652 |
*/ |
| 653 |
App::DocumentObject* resolveRelativeLink(std::string& subname, |
| 654 |
App::DocumentObject*& link, |
| 655 |
std::string& linkSub) const; |
| 656 |
|
| 657 |
/** Called to adjust link properties to avoid cyclic links |
| 658 |
* |
| 659 |
* @param inList: the recursive in-list of the future parent object, |
| 660 |
* including the parent itself. |
| 661 |
* @param visited: optional set holding the visited objects. If null then |
| 662 |
* only this object is adjusted, or else all object inside the out-list of |
| 663 |
* this object will be checked. |
| 664 |
* |
| 665 |
* @return Return whether the object has been modified |
| 666 |
* |
| 667 |
* This function tries to adjust any relative link properties (i.e. link |
| 668 |
* properties that can hold subnames) to avoid cyclic when added to the |
| 669 |
* future parent. |
| 670 |
*/ |
| 671 |
virtual bool adjustRelativeLinks(const std::set<App::DocumentObject*>& inList, |
| 672 |
std::set<App::DocumentObject*>* visited = nullptr); |
| 673 |
|
| 674 |
/** allow partial loading of dependent objects |
| 675 |
* |
| 676 |
* @return Returns 0 means do not support partial loading. 1 means allow |
| 677 |
* dependent objects to be partially loaded, i.e. only create, but not |
| 678 |
* restored. 2 means this object itself can be partially loaded. |
| 679 |
*/ |
| 680 |
virtual int canLoadPartial() const |
| 681 |
{ |
| 682 |
return 0; |
| 683 |
} |
| 684 |
|
| 685 |
virtual void onUpdateElementReference(const Property*) |
| 686 |
{} |
| 687 |
|
| 688 |
/** Allow object to redirect a subname path |
| 689 |
* |
| 690 |
* @param ss: input as the current subname path from \a topParent leading |
| 691 |
* just before this object, i.e. ends at the parent of this object. This |
| 692 |
* function should append its own name to this path, or redirect the |
| 693 |
* subname to other place. |
| 694 |
* @param topParent: top parent of this subname path |
| 695 |
* @param child: the immediate child object in the path |
| 696 |
* |
| 697 |
* This function is called by tree view to generate a subname path when an |
| 698 |
* item is selected in the tree. Document object can use this function to |
| 699 |
* redirect the selection to some other objects. |
| 700 |
*/ |
| 701 |
virtual bool |
| 702 |
redirectSubName(std::ostringstream& ss, DocumentObject* topParent, DocumentObject* child) const; |
| 703 |
|
| 704 |
/** Special marker to mark the object as hidden |
| 705 |
* |
| 706 |
* It is used by Gui::ViewProvider::getElementColors(), but exposed here |
| 707 |
* for convenience |
| 708 |
*/ |
| 709 |
static const std::string& hiddenMarker(); |
| 710 |
/// Check if the subname reference ends with hidden marker. |
| 711 |
static const char* hasHiddenMarker(const char* subname); |
| 712 |
|
| 713 |
/* Find the placement of a target object as seen from this. |
| 714 |
If no targetObj given, the last object found in the subname is used as target. |
| 715 |
*/ |
| 716 |
virtual Base::Placement getPlacementOf(const std::string& sub, DocumentObject* targetObj = nullptr); |
| 717 |
|
| 718 |
/* Returns the Placement property value if any.*/ |
| 719 |
virtual Base::Placement getPlacement() const; |
| 720 |
|
| 721 |
/* Returns the Placement property to use if any*/ |
| 722 |
virtual App::PropertyPlacement* getPlacementProperty() const; |
| 723 |
|
| 724 |
protected: |
| 725 |
/// recompute only this object |
| 726 |
virtual App::DocumentObjectExecReturn* recompute(); |
| 727 |
/** get called by the document to recompute this feature |
| 728 |
* Normally this method get called in the processing of |
| 729 |
* Document::recompute(). |
| 730 |
* In execute() the output properties get recomputed |
| 731 |
* with the data from linked objects and objects own |
| 732 |
* properties. |
| 733 |
*/ |
| 734 |
virtual App::DocumentObjectExecReturn* execute(); |
| 735 |
|
| 736 |
/** |
| 737 |
* Executes the extensions of a document object. |
| 738 |
*/ |
| 739 |
App::DocumentObjectExecReturn* executeExtensions(); |
| 740 |
|
| 741 |
/** Status bits of the document object |
| 742 |
* The first 8 bits are used for the base system the rest can be used in |
| 743 |
* descendent classes to mark special statuses on the objects. |
| 744 |
* The bits and their meaning are listed below: |
| 745 |
* 0 - object is marked as 'touched' |
| 746 |
* 1 - object is marked as 'erroneous' |
| 747 |
* 2 - object is marked as 'new' |
| 748 |
* 3 - object is marked as 'recompute', i.e. the object gets recomputed now |
| 749 |
* 4 - object is marked as 'restoring', i.e. the object gets loaded at the moment |
| 750 |
* 5 - object is marked as 'deleting', i.e. the object gets deleted at the moment |
| 751 |
* 6 - reserved |
| 752 |
* 7 - reserved |
| 753 |
* 16 - object is marked as 'expanded' in the tree view |
| 754 |
*/ |
| 755 |
std::bitset<32> StatusBits; |
| 756 |
|
| 757 |
void setError() |
| 758 |
{ |
| 759 |
StatusBits.set(ObjectStatus::Error); |
| 760 |
} |
| 761 |
void resetError() |
| 762 |
{ |
| 763 |
StatusBits.reset(ObjectStatus::Error); |
| 764 |
} |
| 765 |
void setDocument(App::Document* doc); |
| 766 |
|
| 767 |
/// get called before the value is changed |
| 768 |
void onBeforeChange(const Property* prop) override; |
| 769 |
/// get called by the container when a property was changed |
| 770 |
void onChanged(const Property* prop) override; |
| 771 |
/// get called by the container when a property was changed |
| 772 |
void onEarlyChange(const Property* prop) override; |
| 773 |
/// get called after a document has been fully restored |
| 774 |
virtual void onDocumentRestored(); |
| 775 |
/// get called after an object finishes restoreContent. |
| 776 |
void restoreFinished() override; |
| 777 |
/// get called after an undo/redo transaction is finished |
| 778 |
virtual void onUndoRedoFinished(); |
| 779 |
/// get called after setting the document |
| 780 |
virtual void onSettingDocument(); |
| 781 |
/// get called after a brand new object was created |
| 782 |
virtual void setupObject(); |
| 783 |
/// get called when object is going to be removed from the document |
| 784 |
virtual void unsetupObject(); |
| 785 |
|
| 786 |
/// get called when a property status has changed |
| 787 |
void onPropertyStatusChanged(const Property& prop, unsigned long oldStatus) override; |
| 788 |
|
| 789 |
private: |
| 790 |
void printInvalidLinks() const; |
| 791 |
|
| 792 |
/// python object of this class and all descendent |
| 793 |
protected: // attributes |
| 794 |
Py::SmartPtr PythonObject; |
| 795 |
/// pointer to the document this object belongs to |
| 796 |
App::Document* _pDoc {nullptr}; |
| 797 |
|
| 798 |
/// Old label; used for renaming expressions |
| 799 |
std::string oldLabel; |
| 800 |
|
| 801 |
private: |
| 802 |
// pointer to the document name string (for performance) |
| 803 |
const std::string* pcNameInDocument {nullptr}; |
| 804 |
|
| 805 |
// accessed by App::Document to record and restore the correct view provider type |
| 806 |
std::string _pcViewProviderName; |
| 807 |
|
| 808 |
// unique identifier (among a document) of this object. |
| 809 |
long _Id {0}; |
| 810 |
|
| 811 |
private: |
| 812 |
// Back pointer to all the fathers in a DAG of the document |
| 813 |
// this is used by the document (via friend) to have a effective DAG handling |
| 814 |
std::vector<App::DocumentObject*> _inList; |
| 815 |
mutable std::vector<App::DocumentObject*> _outList; |
| 816 |
mutable std::unordered_map<const char*, App::DocumentObject*, CStringHasher, CStringHasher> |
| 817 |
_outListMap; |
| 818 |
mutable bool _outListCached = false; |
| 819 |
}; |
| 820 |
|
| 821 |
} // namespace App |
| 822 |
|
| 823 |
#endif // SRC_APP_DOCUMENTOBJECT_H_ |