FreeCAD
313 строк · 12.6 Кб
1# FreeCAD module providing base classes for document objects and view provider
2# (c) 2011 Werner Mayer LGPL
3
4import FreeCAD
5
6class DocumentObject(object):
7"""The Document object is the base class for all FreeCAD objects."""
8
9def __init__(self):
10self.__object__=None
11self.initialised=False
12#------------------------------Methods for the user to override :
13
14def execute(self):
15"this method is executed on object creation and whenever the document is recomputed"
16raise NotImplementedError("Not yet implemented")
17
18def init(self):
19#will be called just after object creation, you can use this for example to create properties
20pass
21def propertyChanged(self,prop):
22#will be called each time a property is changed
23pass
24#--------------------------------
25
26
27def __getattr__(self, attr):
28if attr !="__object__" and hasattr(self.__object__,attr):
29return getattr(self.__object__,attr)
30else:
31return object.__getattribute__(self,attr)
32def __setattr__(self, attr, value):
33if attr !="__object__" and hasattr(self.__object__,attr):
34setattr(self.__object__,attr,value)
35else:
36object.__setattr__(self,attr,value)
37def onChanged(self,prop):
38if prop=="Proxy":
39#recreate the functions in the __object__
40d = self.__class__.__dict__
41for key in d:
42item = d[key]
43#check if the function is valid
44if hasattr(item, '__call__') and key!="onChanged" and key!="execute" and key!="init" and key[0]!="_":
45#check if the function doesn't already exist in the object:
46if not(hasattr(self.__object__,key)):
47#add a link to the Proxy function in the __object__ :
48self.addProperty("App::PropertyPythonObject", key, "", "",2)
49setattr(self.__object__,key,getattr(self,key))
50else:
51FreeCAD.Console.PrintWarning('!!! The function : "'+key+'" already exist in the object, cannot override. !!!\n')
52#call the init function
53if hasattr(self,'initialised'):
54if self.initialised==False:
55self.init()
56self.initialised = True
57self.propertyChanged(prop)
58def addProperty(self,typ,name='',group='',doc='',attr=0,readonly=False,hidden=False):
59"adds a new property to this object"
60return self.__object__.addProperty(typ,name,group,doc,attr,readonly,hidden)
61def supportedProperties(self):
62"lists the property types supported by this object"
63return self.__object__.supportedProperties()
64def isDerivedFrom(self, obj):
65"""returns True if this object is derived from the given C++ class, for
66example Part::Feature"""
67return self.__object__.isDerivedFrom(obj)
68def getAllDerivedFrom(self):
69"returns all parent C++ classes of this object"
70return self.__object__.getAllDerivedFrom()
71def getProperty(self,attr):
72"returns the value of a given property"
73return self.__object__.getPropertyByName(attr)
74def getTypeOfProperty(self,attr):
75"returns the type of a given property"
76return self.__object__.getTypeOfProperty(attr)
77def getGroupOfProperty(self,attr):
78"returns the group of a given property"
79return self.__object__.getGroupOfProperty(attr)
80def getDocumentationOfProperty(self,attr):
81"returns the documentation string of a given property"
82return self.__object__.getDocumentationOfProperty(attr)
83def getEnumerationsOfProperty(self,attr):
84"returns the documentation string of a given property"
85return self.__object__.getEnumerationsOfProperty(attr)
86def touch(self):
87"marks this object to be recomputed"
88return self.__object__.touch()
89def purgeTouched(self):
90"removes the to-be-recomputed flag of this object"
91return self.__object__.purgeTouched()
92def __setstate__(self,value):
93"""allows to save custom attributes of this object as strings, so
94they can be saved when saving the FreeCAD document"""
95return None
96def __getstate__(self):
97"""reads values previously saved with __setstate__()"""
98return None
99@property
100def PropertiesList(self):
101"lists the current properties of this object"
102return self.__object__.PropertiesList
103@property
104def Type(self):
105"shows the C++ class of this object"
106return self.__object__.Type
107@property
108def Module(self):
109"gives the module this object is defined in"
110return self.__object__.Module
111@property
112def Content(self):
113"""shows the contents of the properties of this object as an xml string.
114This is the content that is saved when the file is saved by FreeCAD"""
115return self.__object__.Content
116@property
117def MemSize(self):
118"shows the amount of memory this object uses"
119return self.__object__.MemSize
120@property
121def Name(self):
122"the name ofthis object, unique in the FreeCAD document"
123return self.__object__.Name
124@property
125def Document(self):
126"the document this object is part of"
127return self.__object__.Document
128@property
129def State(self):
130"shows if this object is valid (presents no errors)"
131return self.__object__.State
132@property
133def ViewObject(self):
134return self.__object__.ViewObject
135@ViewObject.setter
136def ViewObject(self,value):
137"""returns or sets the ViewObject associated with this object. Returns
138None if FreeCAD is running in console mode"""
139self.__object__.ViewObject=value
140@property
141def InList(self):
142"lists the parents of this object"
143return self.__object__.InList
144@property
145def OutList(self):
146"lists the children of this object"
147return self.__object__.OutList
148
149
150
151
152class ViewProvider(object):
153"""The ViewProvider is the counterpart of the DocumentObject in
154the GUI space. It is only present when FreeCAD runs in GUI mode.
155It contains all that is needed to represent the DocumentObject in
156the 3D view and the FreeCAD interface"""
157def __init__(self):
158self.__vobject__=None
159#def getIcon(self):
160# return ""
161#def claimChildren(self):
162# return self.__vobject__.Object.OutList
163#def setEdit(self,mode):
164# return False
165#def unsetEdit(self,mode):
166# return False
167#def attach(self):
168# return None
169#def updateData(self, prop):
170# return None
171#def onChanged(self, prop):
172# return None
173def addDisplayMode(self,node,mode):
174"adds a coin node as a display mode to this object"
175self.__vobject__.addDisplayMode(node,mode)
176#def getDefaultDisplayMode(self):
177# return ""
178#def getDisplayModes(self):
179# return []
180#def setDisplayMode(self,mode):
181# return mode
182def addProperty(self,type,name='',group='',doc='',attr=0,readonly=False,hidden=False):
183"adds a new property to this object"
184self.__vobject__.addProperty(type,name,group,doc,attr,readonly,hidden)
185def update(self):
186"this method is executed whenever any of the properties of this ViewProvider changes"
187self.__vobject__.update()
188def show(self):
189"switches this object to visible"
190self.__vobject__.show()
191def hide(self):
192"switches this object to invisible"
193self.__vobject__.hide()
194def isVisible(self):
195"shows whether this object is visible or invisible"
196return self.__vobject__.isVisible()
197def toString(self):
198"returns a string representation of the coin node of this object"
199return self.__vobject__.toString()
200def setTransformation(self,trsf):
201"defines a transformation for this object"
202return self.__vobject__.setTransformation(trsf)
203def supportedProperties(self):
204"lists the property types this ViewProvider supports"
205return self.__vobject__.supportedProperties()
206def isDerivedFrom(self, obj):
207"""returns True if this object is derived from the given C++ class, for
208example Part::Feature"""
209return self.__vobject__.isDerivedFrom(obj)
210def getAllDerivedFrom(self):
211"returns all parent C++ classes of this object"
212return self.__vobject__.getAllDerivedFrom()
213def getProperty(self,attr):
214"returns the value of a given property"
215return self.__vobject__.getPropertyByName(attr)
216def getTypeOfProperty(self,attr):
217"returns the type of a given property"
218return self.__vobject__.getTypeOfProperty(attr)
219def getGroupOfProperty(self,attr):
220"returns the group of a given property"
221return self.__vobject__.getGroupOfProperty(attr)
222def getDocumentationOfProperty(self,attr):
223"returns the documentation string of a given property"
224return self.__vobject__.getDocumentationOfProperty(attr)
225def __setstate__(self,value):
226"""allows to save custom attributes of this object as strings, so
227they can be saved when saving the FreeCAD document"""
228return None
229def __getstate__(self):
230"""reads values previously saved with __setstate__()"""
231return None
232@property
233def Annotation(self):
234"returns the Annotation coin node of this object"
235return self.__vobject__.Annotation
236@property
237def RootNode(self):
238"returns the Root coin node of this object"
239return self.__vobject__.RootNode
240@property
241def DisplayModes(self):
242"lists the display modes of this object"
243return self.__vobject__.listDisplayModes()
244@property
245def PropertiesList(self):
246"lists the current properties of this object"
247return self.__vobject__.PropertiesList
248@property
249def Type(self):
250"shows the C++ class of this object"
251return self.__vobject__.Type
252@property
253def Module(self):
254"gives the module this object is defined in"
255return self.__vobject__.Module
256@property
257def Content(self):
258"""shows the contents of the properties of this object as an xml string.
259This is the content that is saved when the file is saved by FreeCAD"""
260return self.__vobject__.Content
261@property
262def MemSize(self):
263"shows the amount of memory this object uses"
264return self.__vobject__.MemSize
265@property
266def Object(self):
267"returns the DocumentObject this ViewProvider is associated to"
268return self.__vobject__.Object
269
270
271#Example :
272import Part
273
274class Box(DocumentObject):
275#type :
276type = "Part::FeaturePython"
277
278#-----------------------------INIT----------------------------------------
279def init(self):
280self.addProperty("App::PropertyLength","Length","Box","Length of the box").Length=1.0
281self.addProperty("App::PropertyLength","Width","Box","Width of the box").Width=1.0
282self.addProperty("App::PropertyLength","Height","Box", "Height of the box").Height=1.0
283
284#-----------------------------BEHAVIOR------------------------------------
285def propertyChanged(self,prop):
286FreeCAD.Console.PrintMessage("Box property changed : "+ prop+ "\n")
287if prop == "Length" or prop == "Width" or prop == "Height":
288self._recomputeShape()
289
290def execute(self):
291FreeCAD.Console.PrintMessage("Recompute Python Box feature\n")
292self._recomputeShape()
293
294#---------------------------PUBLIC FUNCTIONS-------------------------------
295#These functions will be present in the object
296def customFunctionSetLength(self,attr):
297self.Length = attr
298self._privateFunctionExample(attr)
299
300#---------------------------PRIVATE FUNCTIONS------------------------------
301#These function won't be present in the object (begin with '_')
302def _privateFunctionExample(self,attr):
303FreeCAD.Console.PrintMessage("The length : "+str(attr)+"\n")
304
305def _recomputeShape(self):
306if hasattr(self,"Length") and hasattr(self,"Width") and hasattr(self,"Height"):
307self.Shape = Part.makeBox(self.Length,self.Width,self.Height)
308
309
310def makeBox():
311FreeCAD.newDocument()
312box = FreeCAD.ActiveDocument.addObject(Box.type,"MyBox",Box(),None)
313box.customFunctionSetLength(4)
314