File size: 7,983 Bytes
985c397 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 | # SPDX-License-Identifier: LGPL-2.1-or-later
# ***************************************************************************
# * Copyright (c) 2009, 2010 Yorik van Havre <yorik@uncreated.net> *
# * Copyright (c) 2009, 2010 Ken Cline <cline@frii.com> *
# * Copyright (c) 2019 Eliud Cabrera Castillo <e.cabrera-castillo@tum.de> *
# * *
# * This program is free software; you can redistribute it and/or modify *
# * it under the terms of the GNU Lesser General Public License (LGPL) *
# * as published by the Free Software Foundation; either version 2 of *
# * the License, or (at your option) any later version. *
# * for detail see the LICENCE text file. *
# * *
# * This program is distributed in the hope that it will be useful, *
# * but WITHOUT ANY WARRANTY; without even the implied warranty of *
# * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
# * GNU Library General Public License for more details. *
# * *
# * You should have received a copy of the GNU Library General Public *
# * License along with this program; if not, write to the Free Software *
# * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 *
# * USA *
# * *
# ***************************************************************************
"""Provides the object code for the base Draft object."""
## @package base
# \ingroup draftobjects
# \brief Provides the object code for the base Draft object.
## \addtogroup draftobjects
# @{
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"):
# This class is assigned to the Proxy attribute
if obj:
obj.Proxy = self
self.Type = tp
def onDocumentRestored(self, obj):
# Object properties are updated when the document is opened.
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
# For some objects a dummy Placement property change (new and old
# Placement are the same) is used in cases where a full recompute is
# required. This function should then return `False`. The common
# denominator seems to be a non-empty OutList.
# https://github.com/FreeCAD/FreeCAD/issues/8771
# https://forum.freecad.org/viewtopic.php?t=82436
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"}
# Alias for compatibility with v0.18 and earlier
_DraftObject = DraftObject
## @}
|