| | |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | """Provides the object code for the base Draft object.""" |
| | |
| | |
| | |
| |
|
| | |
| | |
| |
|
| |
|
| | class DraftObject(object): |
| | """The base class for Draft objects. |
| | |
| | Parameters |
| | ---------- |
| | obj : a base C++ object |
| | The base object instantiated during creation, |
| | which commonly may be of types `Part::Part2DObjectPython`, |
| | `Part::FeaturePython`, or `App::FeaturePython`. |
| | |
| | >>> obj = App.ActiveDocument.addObject('Part::Part2DObjectPython') |
| | >>> DraftObject(obj) |
| | |
| | This class instance is stored in the `Proxy` attribute |
| | of the base object. |
| | :: |
| | obj.Proxy = self |
| | |
| | tp : str, optional |
| | It defaults to `'Unknown'`. |
| | It indicates the type of this scripted object, |
| | which will be assigned to the Proxy's `Type` attribute. |
| | |
| | This is useful to distinguish different types of scripted objects |
| | that may be derived from the same C++ class. |
| | |
| | Attributes |
| | ---------- |
| | Type : str |
| | It indicates the type of scripted object. |
| | Normally `Type = tp`. |
| | |
| | All objects have a `TypeId` attribute, but this attribute |
| | refers to the C++ class only. Using the `Type` attribute |
| | allows distinguishing among various types of objects |
| | derived from the same C++ class. |
| | |
| | >>> print(obj.TypeId, "->", obj.Proxy.Type) |
| | Part::Part2DObjectPython -> Circle |
| | |
| | This class attribute is accessible through the `Proxy` object: |
| | `obj.Proxy.Type`. |
| | """ |
| |
|
| | def __init__(self, obj, tp="Unknown"): |
| | |
| | if obj: |
| | obj.Proxy = self |
| | self.Type = tp |
| |
|
| | def onDocumentRestored(self, obj): |
| | |
| | self.props_changed_clear() |
| |
|
| | def dumps(self): |
| | """Return a tuple of all serializable objects or None. |
| | |
| | When saving the document this object gets stored |
| | using Python's `json` module. |
| | |
| | Override this method to define the serializable objects to return. |
| | |
| | By default it returns the `Type` attribute. |
| | |
| | Returns |
| | ------- |
| | str |
| | Returns the value of `Type` |
| | """ |
| | return self.Type |
| |
|
| | def loads(self, state): |
| | """Set some internal properties for all restored objects. |
| | |
| | When a document is restored this method is used to set some properties |
| | for the objects stored with `json`. |
| | |
| | Override this method to define the properties to change for the |
| | restored serialized objects. |
| | |
| | By default the `Type` was serialized, so `state` holds this value, |
| | which is re-assigned to the `Type` attribute. |
| | :: |
| | self.Type = state |
| | |
| | Parameters |
| | ---------- |
| | state : state |
| | A serialized object. |
| | """ |
| | if state: |
| | self.Type = state |
| |
|
| | def execute(self, obj): |
| | """Run this method when the object is created or recomputed. |
| | |
| | Override this method to produce effects when the object |
| | is newly created, and whenever the document is recomputed. |
| | |
| | By default it does nothing. |
| | |
| | Parameters |
| | ---------- |
| | obj : the scripted object. |
| | This commonly may be of types `Part::Part2DObjectPython`, |
| | `Part::FeaturePython`, or `App::FeaturePython`. |
| | """ |
| | pass |
| |
|
| | def onChanged(self, obj, prop): |
| | """Run this method when a property is changed. |
| | |
| | Override this method to handle the behavior |
| | of the object depending on changes that occur to its properties. |
| | |
| | By default it does nothing. |
| | |
| | Parameters |
| | ---------- |
| | obj : the scripted object. |
| | This commonly may be of types `Part::Part2DObjectPython`, |
| | `Part::FeaturePython`, or `App::FeaturePython`. |
| | |
| | prop : str |
| | Name of the property that was modified. |
| | """ |
| | pass |
| |
|
| | def props_changed_store(self, prop): |
| | """Store the name of the property that will be changed in the |
| | self.props_changed list. |
| | |
| | The function should be called at the start of onChanged or onBeforeChange. |
| | |
| | The list is used to detect Placement-only changes. In such cases the |
| | Shape of an object does not need to be updated. Not updating the Shape |
| | avoids Uno/Redo issues for the Windows version and is also more efficient. |
| | """ |
| | if not hasattr(self, "props_changed"): |
| | self.props_changed = [prop] |
| | else: |
| | self.props_changed.append(prop) |
| |
|
| | def props_changed_clear(self): |
| | """Remove the self.props_changed attribute if it exists. |
| | |
| | The function should be called just before execute returns. |
| | """ |
| | if hasattr(self, "props_changed"): |
| | delattr(self, "props_changed") |
| |
|
| | def props_changed_placement_only(self, obj=None): |
| | """Return `True` if the self.props_changed list, after removing |
| | `_LinkTouched`, `Shape`, `Density`, `Volume` and `Mass` items, |
| | only contains `Placement` items. |
| | |
| | Parameters |
| | ---------- |
| | obj : the scripted object. Need only be supplied if the Shape of obj |
| | is, or can be, derived from other objects. |
| | |
| | """ |
| | if not hasattr(self, "props_changed"): |
| | return False |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | if obj is not None and obj.OutList: |
| | return False |
| |
|
| | props = set(self.props_changed) |
| | for prop in ("_LinkTouched", "Shape", "Density", "Volume", "Mass"): |
| | if prop in props: |
| | props.remove(prop) |
| | return props == {"Placement"} |
| |
|
| |
|
| | |
| | _DraftObject = DraftObject |
| |
|
| | |
| |
|