| 1 |
/** |
| 2 |
* @defgroup APP App |
| 3 |
* @ingroup CORE |
| 4 |
* @brief The part of FreeCAD that works without GUI (console or server mode) |
| 5 |
* |
| 6 |
* @details It contains the App namespace and defines core concepts such as |
| 7 |
* @ref DocumentGroup "Document", @ref DocumentObjectGroup "Document Object", |
| 8 |
* @ref PropertyFramework "Property Framework", @ref ExpressionFramework |
| 9 |
* "Expression Framework", and the @ref ExtensionFramework "Extension |
| 10 |
* Framework". |
| 11 |
* |
| 12 |
* The largest difference between the functionality in @ref BASE "Base" |
| 13 |
* compared to %App is that %App introduces the notion of properties, both used |
| 14 |
* in @ref App::Document "Document" and @ref App::DocumentObject |
| 15 |
* "DocumentObject". In addition, %App has a representation of the running |
| 16 |
* @ref App::Application "Application". |
| 17 |
*/ |
| 18 |
|
| 19 |
/** |
| 20 |
* @defgroup DocumentGroup Document |
| 21 |
* @ingroup APP |
| 22 |
* @brief The class that represents a FreeCAD document |
| 23 |
* |
| 24 |
* This (besides the App::Application class) is the most important class in FreeCAD. |
| 25 |
* It contains all the data of the opened, saved, or newly created FreeCAD Document. |
| 26 |
* The App::Document manages the Undo and Redo mechanism and the linking of documents. |
| 27 |
* |
| 28 |
* Note: the documents are not free objects. They are completely handled by the |
| 29 |
* App::Application. Only the Application can Open or destroy a document. |
| 30 |
* |
| 31 |
* \section Exception Exception handling |
| 32 |
* As the document is the main data structure of FreeCAD we have to take a close |
| 33 |
* look at how Exceptions affect the integrity of the App::Document. |
| 34 |
* |
| 35 |
* \section UndoRedo Undo Redo an Transactions |
| 36 |
* Undo Redo handling is one of the major mechanism of a document in terms of |
| 37 |
* user friendliness and speed (no one will wait for Undo too long). |
| 38 |
* |
| 39 |
* \section Dependency Graph and dependency handling |
| 40 |
* The FreeCAD document handles the dependencies of its DocumentObjects with |
| 41 |
* an adjacency list. This gives the opportunity to calculate the shortest |
| 42 |
* recompute path. Also, it enables more complicated dependencies beyond trees. |
| 43 |
* |
| 44 |
* @see App::Application |
| 45 |
* @see App::DocumentObject |
| 46 |
*/ |
| 47 |
|
| 48 |
/** |
| 49 |
* @defgroup DocumentObjectGroup Document Object |
| 50 |
* @ingroup APP |
| 51 |
* @brief %Base class of all objects handled in the Document. |
| 52 |
*/ |
| 53 |
|
| 54 |
/** |
| 55 |
* @defgroup PropertyFramework Property framework |
| 56 |
* @ingroup APP |
| 57 |
* @brief System to access object properties. |
| 58 |
* |
| 59 |
* @section propframe_intro Introduction |
| 60 |
* |
| 61 |
* The property framework introduces an intricate system to access properties |
| 62 |
* of objects. It provides the ability to: |
| 63 |
* 1. define properties in a class and access them by name, |
| 64 |
* 2. add properties to a class at runtime and access them by name, and |
| 65 |
* 3. access properties of a class by name without knowing the class type. |
| 66 |
* |
| 67 |
* The first two points are similar to what the dynamic reflection framework of |
| 68 |
* C# or Java offer. The third point allows FreeCAD to have App::Property as a |
| 69 |
* generic interface to access properties. This is similar to the way that |
| 70 |
* Python allows to access properties of a class by name. |
| 71 |
* |
| 72 |
* This ability is introduced by the @ref App::PropertyContainer |
| 73 |
* "PropertyContainer" class and can be used by all derived classes. In |
| 74 |
* particular, there are two classes that inherit from @ref |
| 75 |
* App::PropertyContainer "PropertyContainer" which are @ref App::Document |
| 76 |
* "Document" and @ref App::DocumentObject "DocumentObject". These two classes |
| 77 |
* serve different purposes but are both able to hold properties. @ref |
| 78 |
* App::PropertyContainer "PropertyContainer" contains the shared logic to do |
| 79 |
* so. |
| 80 |
* |
| 81 |
* @section propframe_static_dynamic_props Static/Dynamic Properties |
| 82 |
* |
| 83 |
* In C++, it is possible to define properties as part of the class. These can |
| 84 |
* be considered "static" properties but this term is typically not used within |
| 85 |
* FreeCAD. Properties created in a class cannot be removed from a @ref |
| 86 |
* App::PropertyContainer "PropertyContainer". |
| 87 |
* |
| 88 |
* However, it is also possible to add properties to a @ref |
| 89 |
* App::PropertyContainer "PropertyContainer" at runtime. These properties are |
| 90 |
* called "dynamic properties" and these properties can be freely added and |
| 91 |
* removed by users. In Python, all properties are dynamic properties. |
| 92 |
* |
| 93 |
* @section propframe_mechanisms Mechanisms |
| 94 |
* |
| 95 |
* The macros `PROPERTY_HEADER` and `PROPERTY_SOURCE` (and variants) are used |
| 96 |
* to define a private static PropertyData member in the class. This data |
| 97 |
* structure contains a multi index that maps 1) the property name to the |
| 98 |
* property specifications @ref App::PropertyData::PropertySpec "PropertySpec", |
| 99 |
* allowing access of properties by name, and 2) maps the offset of a property |
| 100 |
* with respect to its container, allowing access to property specifications. |
| 101 |
* |
| 102 |
* The static function @ref App::PropertyContainer::getPropertyDataPtr() |
| 103 |
* "PropertyContainer::getPropertyDataPtr()" is used to access the class-wide |
| 104 |
* static @ref App::PropertyData "PropertyData" structure shared by all |
| 105 |
* instances of the class. The virtual function @ref |
| 106 |
* App::PropertyContainer::getPropertyData() |
| 107 |
* "PropertyContainer::getPropertyData()" allows access to the class-level |
| 108 |
* static PropertyData based on the dynamic type of the object, even when |
| 109 |
* downcasted. This allows for example a @ref App::PropertyInteger |
| 110 |
* "PropertyInteger*" downcasted to @ref App::Property "Property*" to access |
| 111 |
* all its properties. |
| 112 |
* |
| 113 |
* Since the @ref App::PropertyData "PropertyData" structure is static in the |
| 114 |
* class and maintains static information of properties, such as the group and |
| 115 |
* documentation, we need to be able to access a specific instance given a |
| 116 |
* property name. This is achieved by looking up the @ref |
| 117 |
* App::PropertyData::PropertySpec "PropertySpec" in the index based on the |
| 118 |
* property name. The property specification stores an offset to the property |
| 119 |
* address given an @ref App::PropertyData::OffsetBase "OffsetBase" which wraps |
| 120 |
* the address of a @ref App::PropertyContainer "PropertyContainer". See @ref |
| 121 |
* App::PropertyData::findProperty() "PropertyData::findProperty()" and @ref |
| 122 |
* App::PropertyData::OffsetBase::getOffsetTo() "OffsetBase::getOffsetTo()". |
| 123 |
* The offset of the property relative to the offset base gives us the address |
| 124 |
* of the property in the instance. Note that different values for properties |
| 125 |
* across property containers are stored in the @ref App::Property "Property" instances. |
| 126 |
* |
| 127 |
* The reverse is also needed: Given a property in a property container, it is |
| 128 |
* possible to compute the offset relative to the base of the property |
| 129 |
* container. The index in @ref App::PropertyData "PropertyData" allows us to |
| 130 |
* acquire the property spec and provides us with the static data associated |
| 131 |
* with the property. |
| 132 |
* |
| 133 |
* Dynamic properties are stored in their own @ref App::DynamicProperty |
| 134 |
* "DynamicProperty" container. It can be added with the function @ref |
| 135 |
* App::PropertyContainer::addDynamicProperty() |
| 136 |
* "PropertyContainer::addDynamicProperty()" and removed with @ref |
| 137 |
* App::PropertyContainer::removeDynamicProperty() |
| 138 |
* "PropertyContainer::removeDynamicProperty()". The rest of the interface of |
| 139 |
* dynamic properties is virtually the same. In general, in FreeCAD, it is |
| 140 |
* difficult for users to know whether a property is dynamic or static and they |
| 141 |
* typically do not need to be concerned with this distinction. |
| 142 |
* |
| 143 |
* @section Locking Dynamic Properties |
| 144 |
* |
| 145 |
* Since in Python all properties are dynamic properties, it is difficult to |
| 146 |
* understand whether properties are part of a Python class and are necessary |
| 147 |
* for the functioning of the class, or whether a user has added these |
| 148 |
* properties. Therefore, it is possible to indicate that a property is |
| 149 |
* "locked": |
| 150 |
* |
| 151 |
* @code |
| 152 |
* prop->setStatus(Property::LockDynamic, true); |
| 153 |
* @endcode |
| 154 |
* |
| 155 |
* In Python, this can be indicated in the `addProperty()` function: |
| 156 |
* |
| 157 |
* @code |
| 158 |
* addProperty(type: string, name: string, group="", doc="", |
| 159 |
* attr=0, read_only=False, hidden=False, |
| 160 |
* locked=False, enum_vals=[]) |
| 161 |
* @endcode |
| 162 |
* |
| 163 |
* The Property Framework makes it possible in the first place to make an |
| 164 |
* automatic mapping to python (e.g. in App::FeaturePy) and abstract editing |
| 165 |
* properties in Gui::PropertyEditor. |
| 166 |
* |
| 167 |
* @section Examples |
| 168 |
* |
| 169 |
* Here some little examples how to use the property framework: |
| 170 |
* |
| 171 |
* @code |
| 172 |
* // Acquire a property by name and return the value as a Python object. |
| 173 |
* Property *prop = feature->getPropertyByName(nameProperty); |
| 174 |
* if (prop) { |
| 175 |
* return prop->getPyObject(); |
| 176 |
* } |
| 177 |
* @endcode |
| 178 |
* |
| 179 |
* or: |
| 180 |
* |
| 181 |
* @code |
| 182 |
* // Restore properties from a reader. |
| 183 |
* void PropertyContainer::Restore(Base::Reader &reader) |
| 184 |
* { |
| 185 |
* reader.readElement("Properties"); |
| 186 |
* int Cnt = reader.getAttributeAsInteger("Count"); |
| 187 |
* |
| 188 |
* for(int i=0 ;i<Cnt ;i++) { |
| 189 |
* reader.readElement("Property"); |
| 190 |
* string PropName = reader.getAttribute("name"); |
| 191 |
* Property* prop = getPropertyByName(PropName.c_str()); |
| 192 |
* if(prop) { |
| 193 |
* prop->Restore(reader); |
| 194 |
* } |
| 195 |
* |
| 196 |
* reader.readEndElement("Property"); |
| 197 |
* } |
| 198 |
* reader.readEndElement("Properties"); |
| 199 |
* } |
| 200 |
* @endcode |
| 201 |
*/ |
| 202 |
|
| 203 |
/** |
| 204 |
* @defgroup ExpressionFramework Expressions framework |
| 205 |
* @ingroup APP |
| 206 |
* @brief The expression system allows users to write expressions and formulas that produce values |
| 207 |
*/ |
| 208 |
|
| 209 |
/** |
| 210 |
* @defgroup ExtensionFramework Extension framework |
| 211 |
* @ingroup APP |
| 212 |
* @brief The extension system provides a way to extend the functionality of Document Objects. |
| 213 |
* |
| 214 |
* The concept of extensions provides a way to extend the functionality of |
| 215 |
* objects in FreeCAD despite Python's limitations with multiple inheritance |
| 216 |
* (see below for an explanation). Extensions are FreeCAD objects that act |
| 217 |
* like regular objects in the sense that they have properties and class |
| 218 |
* methods to define their functionality. However, extensions are not exposed |
| 219 |
* as individual usable entities but are used to extend other objects. An |
| 220 |
* extended object obtains all the properties and methods of the extension. |
| 221 |
* |
| 222 |
* As such, it is like C++ multiple inheritance, which is indeed used to |
| 223 |
* achieve this on C++ side, but it provides a few important additional |
| 224 |
* functionalities as well: |
| 225 |
* |
| 226 |
* - Property persistence is handled: save and restore work out of the box. |
| 227 |
* - The objects Python API gets extended too with the extension Python API. |
| 228 |
* - Extensions can be added from C++ and Python, even from both together. |
| 229 |
* |
| 230 |
* The interoperability with Python is highly important since in FreeCAD, all |
| 231 |
* functionality should be easily accessible from both Python and C++. To |
| 232 |
* ensure this -- as already noted -- extensions can be added to an object from |
| 233 |
* Python. |
| 234 |
* |
| 235 |
* However, this means that it is not clear from the C++ object type whether an |
| 236 |
* extension was added or not: If added from C++, it becomes clear in the type |
| 237 |
* due to the use of multiple inheritance. If added from Python, it is a |
| 238 |
* runtime extension and not visible from the type. Hence, querying existing |
| 239 |
* extensions of an object and accessing its methods works not by type casting |
| 240 |
* but by the interface provided in ExtensionContainer. The default workflow |
| 241 |
* is to query whether an extension exists and then to get the extension |
| 242 |
* object. This interface always works the same, no matter if added from |
| 243 |
* Python or C++. |
| 244 |
* |
| 245 |
* @code |
| 246 |
* if (object->hasExtension(GroupExtension::getClassTypeId())) { |
| 247 |
* App::GroupExtension* group = object->getExtensionByType<GroupExtension>(); |
| 248 |
* group->hasObject(...); |
| 249 |
* } |
| 250 |
* @endcode |
| 251 |
* |
| 252 |
* To add an extension to an object, it must comply to a single restriction: it |
| 253 |
* must be derived from ExtensionContainer. This is important to allow adding |
| 254 |
* extensions from Python and also to access the universal extension API. As |
| 255 |
* DocumentObject itself derives from ExtensionContainer, this should be the |
| 256 |
* case automatically in most circumstances. |
| 257 |
* |
| 258 |
* Note that two small boilerplate changes are needed in addition to the |
| 259 |
* multiple inheritance when adding extensions from C++. |
| 260 |
* |
| 261 |
* 1. It must be ensured that the property and type registration is aware of |
| 262 |
* the extensions by using special macros. |
| 263 |
* 2. The extensions need to be initialised in the constructor. |
| 264 |
* |
| 265 |
* Here is a working example: |
| 266 |
* @code{.cpp} |
| 267 |
* class AppExport Part : public App::DocumentObject |
| 268 |
* , public App::FirstExtension |
| 269 |
* , public App::SecondExtension |
| 270 |
* { |
| 271 |
* PROPERTY_HEADER_WITH_EXTENSIONS(App::Part); |
| 272 |
* }; |
| 273 |
* |
| 274 |
* PROPERTY_SOURCE_WITH_EXTENSIONS(App::Part, App::DocumentObject) |
| 275 |
* |
| 276 |
* Part::Part(void) |
| 277 |
* { |
| 278 |
* FirstExtension::initExtension(this); |
| 279 |
* SecondExtension::initExtension(this); |
| 280 |
* } |
| 281 |
* @endcode |
| 282 |
* |
| 283 |
* From Python, adding an extension is easier: It must be simply registered to |
| 284 |
* a document object at object initialisation like done with properties. Note |
| 285 |
* that the special Python extension objects need to be added, not the C++ |
| 286 |
* objects. Normally the only difference in name is the additional "Python" at |
| 287 |
* the end of the extension name. |
| 288 |
* |
| 289 |
* @code{.py} |
| 290 |
* class Test(): |
| 291 |
* __init(self)__: |
| 292 |
* registerExtension("App::FirstExtensionPython", self) |
| 293 |
* registerExtension("App::SecondExtensionPython", self) |
| 294 |
* @endcode |
| 295 |
* |
| 296 |
* Extensions can provide methods that should be overridden by the extended |
| 297 |
* object for customisation of the extension behaviour. In C++ this is as |
| 298 |
* simple as overriding the provided virtual functions. In Python a class |
| 299 |
* method must be provided which has the same name as the method to override. |
| 300 |
* This method must not necessarily be in the object that is extended, it must |
| 301 |
* be in the object which is provided to the "registerExtension" call as second |
| 302 |
* argument. This second argument is used as a proxy and queried if the method |
| 303 |
* to override exists in this proxy before calling it. |
| 304 |
* |
| 305 |
* @section SecExtensionFrameworkCreateExtension Create an Extension |
| 306 |
* |
| 307 |
* An Extension is like every other FreeCAD object and is based on properties. |
| 308 |
* All information storage and persistence should be achieved by use of these |
| 309 |
* properties. Additionally, any number of methods can be added to provide |
| 310 |
* functionality related to the properties. There are three small differences |
| 311 |
* to normal objects: |
| 312 |
* |
| 313 |
* 1. They must be derived from this Extension class, |
| 314 |
* 2. Properties must be handled with special extension macros. |
| 315 |
* 3. Extensions must be initialised. |
| 316 |
* |
| 317 |
* The following code illustrates the basic setup of an extension: |
| 318 |
* |
| 319 |
* @code |
| 320 |
* class MyExtension : public Extension |
| 321 |
* { |
| 322 |
* EXTENSION_PROPERTY_HEADER(MyExtension); |
| 323 |
* PropertyInt MyProp; |
| 324 |
* virtual bool overridableMethod(DocumentObject* obj) {}; |
| 325 |
* }; |
| 326 |
* |
| 327 |
* EXTENSION_PROPERTY_SOURCE(App::MyExtension, App::Extension) |
| 328 |
* MyExtension::MyExtension() |
| 329 |
* { |
| 330 |
* EXTENSION_ADD_PROPERTY(MyProp, (0)) |
| 331 |
* initExtensionType(MyExtension::getExtensionClassTypeId()); |
| 332 |
* } |
| 333 |
* |
| 334 |
* using MyExtensionPython = ExtensionPythonT<MyExtension>; |
| 335 |
* @endcode |
| 336 |
* |
| 337 |
* The special Python extension type created above is important, as only those Python extensions |
| 338 |
* can be added to an object from Python. It does not work to add the C++ version directly there. |
| 339 |
* |
| 340 |
* Note that every method of the extension becomes part of the extended object when added from C++. |
| 341 |
* This means one should carefully design the API and make only necessary methods public or protected. |
| 342 |
* Every internal method should be private. |
| 343 |
* |
| 344 |
* The automatic availability of methods in the class does not hold for the |
| 345 |
* Python interface, only for C++ classes. This is common in the rest of |
| 346 |
* FreeCAD as well: there is no automatic creation of Python API from C++ |
| 347 |
* classes. |
| 348 |
* |
| 349 |
* Hence, the extension creator must also create a custom Python object of its |
| 350 |
* extension, which is the same for the normal FreeCAD Python object workflow. |
| 351 |
* There is nothing special at all for Python extension objects: the normal |
| 352 |
* `.pyi` and `PyImp.cpp` approach is used but note that it is important that |
| 353 |
* the object's father is the correct extension base class. Additionally, make |
| 354 |
* sure the extension returns the correct Python object in its |
| 355 |
* getExtensionPyObject() call. |
| 356 |
* |
| 357 |
* Every method that is created in the extension's Python counterpart will be |
| 358 |
* later added to the extended object. This happens automatically for both the |
| 359 |
* C++ and Python extension if getExtensionPyObject() returns the correct |
| 360 |
* Python object. This does not require extra work. |
| 361 |
* |
| 362 |
* A special case that needs to be handled for extensions is the possibility of |
| 363 |
* overridden methods. Often, it is desired to customise extension behaviour |
| 364 |
* by allowing the user to override methods provided by the extension. On the |
| 365 |
* C++ side, this is trivial: such methods are simply marked as "virtual" and |
| 366 |
* can then be overridden in any derived class. This is more involved for the |
| 367 |
* Python interface and here special care needs to be taken. |
| 368 |
* |
| 369 |
* As already shown above, one needs to create a special `ExtensionPythonT<>` |
| 370 |
* object for extension from Python. This is done exactly for the purpose of |
| 371 |
* allowing to have overridable methods. The ExtensionPythonT wrapper adds a |
| 372 |
* proxy property that holds a PyObject which itself will contain the |
| 373 |
* implementations for the overridden methods. This design is equal to the |
| 374 |
* ObjectPythonT<> design of normal document objects. As this wrapper inherits |
| 375 |
* the C++ extension class it, can also override the virtual functions the user |
| 376 |
* designed to be overridden. What it should do at a call of the virtual |
| 377 |
* method is to check if this method is implemented in the proxy object and if |
| 378 |
* so, call it, and if not, call the normal C++ version. It is the extension |
| 379 |
* creator's responsibility to implement this check and call behaviour for |
| 380 |
* every overridable method. This is done by creating a custom wrapper just |
| 381 |
* like ExtensionPythonT<> and overriding all virtual methods. |
| 382 |
* |
| 383 |
* @code |
| 384 |
* template<typename ExtensionT> class MyExtensionPythonT : public ExtensionT { |
| 385 |
* public: |
| 386 |
* |
| 387 |
* MyExtensionPythonT() {} |
| 388 |
* virtual ~MyExtensionPythonT() {} |
| 389 |
* |
| 390 |
* virtual bool overridableMethod(DocumentObject* obj) override { |
| 391 |
* Py::Object pyobj = Py::asObject(obj->getPyObject()); |
| 392 |
* EXTENSION_PROXY_ONEARG(allowObject, pyobj); |
| 393 |
* |
| 394 |
* if(result.isNone()) |
| 395 |
* ExtensionT::allowObject(obj); |
| 396 |
* |
| 397 |
* if(result.isBoolean()) |
| 398 |
* return result.isTrue(); |
| 399 |
* |
| 400 |
* return false; |
| 401 |
* }; |
| 402 |
* }; |
| 403 |
* @endcode |
| 404 |
* |
| 405 |
* @note As seen in the code there are multiple helper macros to ease the |
| 406 |
* repetitive work of querying and calling methods of the proxy object. See the |
| 407 |
* macro documentation for how to use them. |
| 408 |
* |
| 409 |
* To ensure that your wrapper is used when a extension is created from Python |
| 410 |
* the extension type must be exposed as follows: |
| 411 |
* |
| 412 |
* @code |
| 413 |
* using MyExtensionPython = ExtensionPythonT<MyExtensionPythonT<MyExtension>>; |
| 414 |
* @endcode |
| 415 |
* |
| 416 |
* This boilerplate is absolutely necessary to allow overridable methods in |
| 417 |
* Python and it is the extension creator's responsibility to ensure full |
| 418 |
* implementation. |
| 419 |
* |
| 420 |
* @section SecExtensionLimitiationsPython Limitations of Python |
| 421 |
* |
| 422 |
* Without this extension system, it would be challenging to extend |
| 423 |
* functionality in FreeCAD. Although C++ supports multiple inheritance, it is |
| 424 |
* not possible to use it in FreeCAD because it should be possible to expose |
| 425 |
* all objects to Python. |
| 426 |
* |
| 427 |
* However, using multiple parent classes in Python is currently not possible |
| 428 |
* with the default object approach. Moreover, it is basically impossible to |
| 429 |
* handle multiple inheritance in the C-API for Python extensions. |
| 430 |
*/ |
| 431 |
|
| 432 |
/** |
| 433 |
* @namespace App |
| 434 |
* @ingroup APP |
| 435 |
* @brief The namespace for the part of FreeCAD that works without GUI. |
| 436 |
* @details This namespace includes %Application services of FreeCAD that such as: |
| 437 |
* - The Application class |
| 438 |
* - The Document class |
| 439 |
* - The DocumentObject classes |
| 440 |
* - The Expression classes |
| 441 |
* - The Property classes |
| 442 |
* |
| 443 |
* For a more high-level discussion see the topic @ref APP "App". |
| 444 |
*/ |
| 445 |
|