Scripted objects/de: Difference between revisions
Renatorivo (talk | contribs) (Created page with "== Andere komplexere Beispiele == Dieses Beispiel macht von Part Module Gebrauch, um ein Oktaeder zu schaffen, erstellt dann seine coin-Darstellung mit pivy. Zuerst erste...") |
No edit summary |
||
(123 intermediate revisions by 9 users not shown) | |||
Line 1: | Line 1: | ||
<languages/> |
|||
Außer den Standardobjektarten wie Anmerkungen, Mesh-und Teile-Objekten, bietet FreeCAD auch die erstaunliche Möglichkeit an, 100%-ige python-Skript-Objekte zu erstellen, genannt Python Features. Diese Objekte verhalten sich genau so wie jedes andere Objekt in FreeCAD und werden automatisch in Dateien gespeichert und wiederhergestellt, wenn laden / speichern ausgeführt wird. |
|||
{{Docnav/de |
|||
Eine Besonderheit muss verstanden werden, jene Gegenstände werden in FreeCAD FcStd Dateien gespeichert mit python's [http://docs.python.org/release/2.5/lib/module-cPickle.html cPickle]-modul. Das Modul bildet aus einem Python-Objekt einen String, der dann einer gespeicherten Datei hinzugefügt werden kann. Beim Dateiöffnen, verwendet das cPickle Modul diese Zeichenfolge , um das ursprüngliche Objekt neu zu erstellen, vorausgesetzt, es hat den Zugriff auf den Quellcode, welcher das Objekt erstellt hat. Dies bedeutet, dass wenn Sie ein solches benutzerdefiniertes Objekt speichern und öffnen es auf einer Maschine, wo der Python-Code, der das Objekt erzeugt hat, ist nicht vorhanden, wird das Objekt nicht neu erstellt werden. |
|||
|[[Topological_data_scripting/de|Topologische Daten skripten]] |
|||
|[[Scenegraph/de|Szenengraph]] |
|||
}} |
|||
{{TOCright}} |
|||
Python-Features folgen der gleichen Regeln wie alle FreeCAD Features: sie werden in App und GUI Teile getrennt. Der App Teil, das Document Object, definiert die Geometrie unseres Objektes, während der GUI-Teil, das View-Objekt, definiert, wie das Objekt auf dem Bildschirm gezeichnet wird. Das View-Objekt, ist wie jedes andere FreeCAD Feature nur verfügbar, wenn Sie FreeCAD in seiner eigenen GUI ausführen. Es gibt mehrere Eigenschaften und Methoden, die verfügbar sind, um Ihr Objekt zu erstellen. Eigenschaften muss von einem der in FreeCAD vordefinierten Typen sein, um im Eigenschafts-View-Fenster zu erscheinen, wo sie vom Benutzer editiert werden können. |
|||
<span id="Introduction"></span> |
|||
Auf diese Weise sind FeaturePython-Objekte wirklich und völlig parametrisch. Sie können die Eigenschaften für den Gegenstand und sein ViewObjekt getrennt definieren. |
|||
==Einführung== |
|||
Neben den Standard-Objekttypen wie Beschriftungen, Netze und Bauteilobjekte bietet FreeCAD auch die erstaunliche Möglichkeit, 100% mit Python geschriebene parametrische Objekte zu erstellen, die als [[App_FeaturePython|Python-Objekt]] (App FeaturePython-Objekt) bezeichnet werden. Diese Objekte verhalten sich genau wie jedes andere FreeCAD-Objekt und werden beim Speichern/Laden von Dateien automatisch gespeichert und wiederhergestellt. |
|||
== Grundlegendes Beispiel == |
|||
Das folgende Beispiel kann in der [http://free-cad.svn.sourceforge.net/viewvc/free-cad/trunk/src/Mod/TemplatePyMod/FeaturePython.py?view=markup src/Mod/TemplatePyMod/FeaturePython.py]-Datei gefunden werden, zusammen mit einigen anderen Beispielen: |
|||
<syntaxhighlight> |
|||
"Examples for a feature class and its view provider." |
|||
import FreeCAD, FreeCADGui |
|||
from pivy import coin |
|||
class Box: |
|||
def __init__(self, obj): |
|||
"'''Add some custom properties to our box feature'''" |
|||
obj.addProperty("App::PropertyLength","Length","Box","Length of the box").Length=1.0 |
|||
obj.addProperty("App::PropertyLength","Width","Box","Width of the box").Width=1.0 |
|||
obj.addProperty("App::PropertyLength","Height","Box", "Height of the box").Height=1.0 |
|||
obj.Proxy = self |
|||
def onChanged(self, fp, prop): |
|||
"'''Do something when a property has changed'''" |
|||
FreeCAD.Console.PrintMessage("Change property: " + str(prop) + "\n") |
|||
def execute(self, fp): |
|||
"'''Do something when doing a recomputation, this method is mandatory'''" |
|||
FreeCAD.Console.PrintMessage("Recompute Python Box feature\n") |
|||
class ViewProviderBox: |
|||
def __init__(self, obj): |
|||
"'''Set this object to the proxy object of the actual view provider'''" |
|||
obj.addProperty("App::PropertyColor","Color","Box","Color of the box").Color=(1.0,0.0,0.0) |
|||
obj.Proxy = self |
|||
def attach(self, obj): |
|||
"'''Setup the scene sub-graph of the view provider, this method is mandatory'''" |
|||
self.shaded = coin.SoGroup() |
|||
self.wireframe = coin.SoGroup() |
|||
self.scale = coin.SoScale() |
|||
self.color = coin.SoBaseColor() |
|||
data=coin.SoCube() |
|||
self.shaded.addChild(self.scale) |
|||
self.shaded.addChild(self.color) |
|||
self.shaded.addChild(data) |
|||
obj.addDisplayMode(self.shaded,"Shaded"); |
|||
style=coin.SoDrawStyle() |
|||
style.style = coin.SoDrawStyle.LINES |
|||
self.wireframe.addChild(style) |
|||
self.wireframe.addChild(self.scale) |
|||
self.wireframe.addChild(self.color) |
|||
self.wireframe.addChild(data) |
|||
obj.addDisplayMode(self.wireframe,"Wireframe"); |
|||
self.onChanged(obj,"Color") |
|||
def updateData(self, fp, prop): |
|||
"'''If a property of the handled feature has changed we have the chance to handle this here'''" |
|||
# fp is the handled feature, prop is the name of the property that has changed |
|||
l = fp.getPropertyByName("Length") |
|||
w = fp.getPropertyByName("Width") |
|||
h = fp.getPropertyByName("Height") |
|||
self.scale.scaleFactor.setValue(l,w,h) |
|||
pass |
|||
def getDisplayModes(self,obj): |
|||
"'''Return a list of display modes.'''" |
|||
modes=[] |
|||
modes.append("Shaded") |
|||
modes.append("Wireframe") |
|||
return modes |
|||
def getDefaultDisplayMode(self): |
|||
"'''Return the name of the default display mode. It must be defined in getDisplayModes.'''" |
|||
return "Shaded" |
|||
def setDisplayMode(self,mode): |
|||
"'''Map the display mode defined in attach with those defined in getDisplayModes.\''' |
|||
'''Since they have the same names nothing needs to be done. This method is optional'''" |
|||
return mode |
|||
def onChanged(self, vp, prop): |
|||
"'''Here we can do something when a single property got changed'''" |
|||
FreeCAD.Console.PrintMessage("Change property: " + str(prop) + "\n") |
|||
if prop == "Color": |
|||
c = vp.getPropertyByName("Color") |
|||
self.color.rgb.setValue(c[0],c[1],c[2]) |
|||
def getIcon(self): |
|||
"'''Return the icon in XPM format which will appear in the tree view. This method is\''' |
|||
'''optional and if not defined a default icon is shown.'''" |
|||
return """ |
|||
/* XPM */ |
|||
static const char * ViewProviderBox_xpm[] = { |
|||
"16 16 6 1", |
|||
" c None", |
|||
". c #141010", |
|||
"+ c #615BD2", |
|||
"@ c #C39D55", |
|||
"# c #000000", |
|||
"$ c #57C355", |
|||
" ........", |
|||
" ......++..+..", |
|||
" .@@@@.++..++.", |
|||
" .@@@@.++..++.", |
|||
" .@@ .++++++.", |
|||
" ..@@ .++..++.", |
|||
"###@@@@ .++..++.", |
|||
"##$.@@$#.++++++.", |
|||
"#$#$.$$$........", |
|||
"#$$####### ", |
|||
"#$$#$$$$$# ", |
|||
"#$$#$$$$$# ", |
|||
"#$$#$$$$$# ", |
|||
" #$#$$$$$# ", |
|||
" ##$$$$$# ", |
|||
" ####### "}; |
|||
""" |
|||
def __getstate__(self): |
|||
"'''When saving the document this object gets stored using Python's json module.\''' |
|||
'''Since we have some un-serializable parts here -- the Coin stuff -- we must define this method\''' |
|||
'''to return a tuple of all serializable objects or None.'''" |
|||
return None |
|||
def __setstate__(self,state): |
|||
"'''When restoring the serialized object from document we have the chance to set some internals here.\''' |
|||
'''Since no data were serialized nothing needs to be done here.'''" |
|||
return None |
|||
def makeBox(): |
|||
FreeCAD.newDocument() |
|||
a=FreeCAD.ActiveDocument.addObject("App::FeaturePython","Box") |
|||
Box(a) |
|||
ViewProviderBox(a.ViewObject) |
|||
</syntaxhighlight> |
|||
== Verfügbare Eigenschaften == |
|||
Eine Besonderheit muss beachtet werden: Aus Sicherheitsgründen enthalten FreeCAD Dateien niemals eingebetteten Code. Der Python Code, den du zum Erstellen von parametrischen Objekten schreibst, wird niemals innerhalb einer Datei gespeichert. Das bedeutet, dass, wenn du eine Datei, die ein solches Objekt enthält, auf einem anderen Rechner öffnest, wenn dieser Python Code auf diesem Rechner nicht verfügbar ist, das Objekt nicht vollständig neu erstellt wird. Wenn du solche Objekte an andere weitergibst, musst du auch dein Python Skript weitergeben, z. B. als [[Macros/de|Makro]]. |
|||
Eigenschaften sind die wahren Bausteine von FeaturePython-Gegenständen. Durch ist der Benutzer im Stande, mit einem Objekt zu interagieren und es zu ändern. Nach dem Erstellen eines neuen FeaturePython-Objekts in Ihrem Dokument( a=FreeCAD.ActiveDocument.addObject("App::FeaturePython","Box") ), können eine Liste der verfügbaren Eigenschaften bekommen, indem Sie folgendes eingeben: |
|||
<syntaxhighlight> |
|||
obj.supportedProperties() |
|||
</syntaxhighlight> |
|||
Sie werden eine Liste von verfügbaren Eigenschaften bekommen mit: |
|||
<syntaxhighlight> |
|||
App::PropertyBool |
|||
App::PropertyFloat |
|||
App::PropertyFloatList |
|||
App::PropertyFloatConstraint |
|||
App::PropertyAngle |
|||
App::PropertyDistance |
|||
App::PropertyInteger |
|||
App::PropertyIntegerConstraint |
|||
App::PropertyPercent |
|||
App::PropertyEnumeration |
|||
App::PropertyIntegerList |
|||
App::PropertyString |
|||
App::PropertyStringList |
|||
App::PropertyLink |
|||
App::PropertyLinkList |
|||
App::PropertyMatrix |
|||
App::PropertyVector |
|||
App::PropertyVectorList |
|||
App::PropertyPlacement |
|||
App::PropertyPlacementLink |
|||
App::PropertyColor |
|||
App::PropertyColorList |
|||
App::PropertyMaterial |
|||
App::PropertyPath |
|||
App::PropertyFile |
|||
App::PropertyFileIncluded |
|||
Part::PropertyPartShape |
|||
Part::PropertyFilletContour |
|||
Part::PropertyCircle |
|||
</syntaxhighlight> |
|||
Beim Hinzufügen von Eigenschaften zu benutzerdefinierten Objekte, achten Sie bitte auf folgendes: |
|||
'''Hinweis''': Es ist möglich, Python Code in eine FreeCAD Datei zu packen, indem man json mit einem App::PropertyPythonObject serialisiert, aber dieser Code kann nie direkt ausgeführt werden und hat daher für unseren Zweck hier wenig Nutzen. |
|||
* Verwenden Sie keine Zeichen "<" oder ">" in den Eigenschaftes-Beschreibungen (das würde den XML-Teil in der .Fcstd-Datei zerbrechen) |
|||
* Eigenschaften werden alphabetisch in einer .fcstd Datei gespeichert. Wenn Sie eine Form("Shape") in Ihren Eigenschaften haben, wird jede Eigenschaft, deren Name in alphabetischen Reihenfolge nach "Shape" kommt, auch nach der Form geladen, was zu seltsamen Verhaltensweisen führen kann. |
|||
[[App_FeaturePython/de|Python-Objekte]] folgen der gleichen Regel wie alle FreeCAD Funktionen: Sie sind in App- und einen GUI-Teile getrennt. Der App-Teil, das Dokument-Objekt, definiert die Geometrie unseres Objekts, während sein GUI-Teil, das View-Provider-Objekt, definiert, wie das Objekt auf dem Bildschirm daegestellt wird. Das Ansichtsprovider-Objekt ist, wie jede andere FreeCAD-Funktion, nur verfügbar, wenn FreeCAD in seiner eigenen GUI ausgeführt wird. Es stehen mehrere Eigenschaften und Methoden zur Verfügung, um ein Objekt zu erstellen. Die Eigenschaften müssen zu einem der vordefinierten Eigenschaftstypen gehören, die FreeCAD anbietet und werden im Eigenschaften Ansichtsfenster angezeigt, damit sie vom Benutzer bearbeitet werden können. Auf diese Weise sind FeaturePython-Objekte wirklich und vollständig parametrisch. Du kannst Eigenschaften für das Objekt und sein Ansichtsobjekt getrennt definieren. |
|||
<span id="Basic_example"></span> |
|||
== Andere komplexere Beispiele == |
|||
==Grundlegendes Beispiel== |
|||
Das folgende Beispiel ist zusammen mit einigen anderen Beispielen in der Datei [https://github.com/FreeCAD/FreeCAD/blob/master/src/Mod/TemplatePyMod/FeaturePython.py src/Mod/TemplatePyMod/FeaturePython.py] zu finden: |
|||
Dieses Beispiel macht von [[Part Module]] Gebrauch, um ein Oktaeder zu schaffen, erstellt dann seine coin-Darstellung mit pivy. |
|||
Zuerst erstellen wir das Document-Objekt selbst: |
|||
<syntaxhighlight> |
|||
obj.setEditorMode("MyPropertyName", mode) |
|||
</syntaxhighlight> |
|||
where mode is a short int that can be set to: |
|||
0 -- default mode, read and write |
|||
1 -- read-only |
|||
2 -- hidden |
|||
{{Code|code= |
|||
== Other more complex example == |
|||
'''Examples for a feature class and its view provider.''' |
|||
This example makes use of the [[Part Module]] to create an octahedron, then creates its coin representation with pivy. |
|||
import FreeCAD, FreeCADGui |
|||
First is the Document object itself: |
|||
from pivy import coin |
|||
<syntaxhighlight> |
|||
import FreeCAD, FreeCADGui, Part |
|||
class Box: |
|||
def __init__(self, obj): |
|||
class Octahedron: |
|||
'''Add some custom properties to our box feature''' |
|||
def __init__(self, obj): |
|||
obj.addProperty("App::PropertyLength", "Length", "Box", "Length of the box").Length = 1.0 |
|||
"Add some custom properties to our box feature" |
|||
obj.addProperty("App::PropertyLength", "Width", "Box", "Width of the box").Width = 1.0 |
|||
obj.addProperty("App::PropertyLength", "Height", "Box", "Height of the box").Height = 1.0 |
|||
obj.Proxy = self |
|||
obj.addProperty("App::PropertyLength","Height","Octahedron", "Height of the octahedron").Height=1.0 |
|||
obj.addProperty("Part::PropertyPartShape","Shape","Octahedron", "Shape of the octahedron") |
|||
def onChanged(self, fp, prop): |
|||
obj.Proxy = self |
|||
'''Do something when a property has changed''' |
|||
FreeCAD.Console.PrintMessage("Change property: " + str(prop) + "\n") |
|||
def execute(self, fp): |
|||
# Define six vetices for the shape |
|||
def execute(self, fp): |
|||
v1 = FreeCAD.Vector(0,0,0) |
|||
'''Do something when doing a recomputation, this method is mandatory''' |
|||
v2 = FreeCAD.Vector(fp.Length,0,0) |
|||
FreeCAD.Console.PrintMessage("Recompute Python Box feature\n") |
|||
v3 = FreeCAD.Vector(0,fp.Width,0) |
|||
v4 = FreeCAD.Vector(fp.Length,fp.Width,0) |
|||
class ViewProviderBox: |
|||
v5 = FreeCAD.Vector(fp.Length/2,fp.Width/2,fp.Height/2) |
|||
def __init__(self, obj): |
|||
v6 = FreeCAD.Vector(fp.Length/2,fp.Width/2,-fp.Height/2) |
|||
'''Set this object to the proxy object of the actual view provider''' |
|||
obj.addProperty("App::PropertyColor","Color", "Box", "Color of the box").Color = (1.0, 0.0, 0.0) |
|||
# Make the wires/faces |
|||
obj.Proxy = self |
|||
f2 = self.make_face(v2,v4,v5) |
|||
def attach(self, obj): |
|||
'''Setup the scene sub-graph of the view provider, this method is mandatory''' |
|||
f4 = self.make_face(v3,v1,v5) |
|||
self.shaded = coin.SoGroup() |
|||
self.wireframe = coin.SoGroup() |
|||
self.scale = coin.SoScale() |
|||
self.color = coin.SoBaseColor() |
|||
shell=Part.makeShell([f1,f2,f3,f4,f5,f6,f7,f8]) |
|||
data=coin.SoCube() |
|||
self.shaded.addChild(self.scale) |
|||
self.shaded.addChild(self.color) |
|||
self.shaded.addChild(data) |
|||
# helper mehod to create the faces |
|||
obj.addDisplayMode(self.shaded, "Shaded"); |
|||
style=coin.SoDrawStyle() |
|||
style.style = coin.SoDrawStyle.LINES |
|||
self.wireframe.addChild(style) |
|||
self.wireframe.addChild(self.scale) |
|||
</syntaxhighlight> |
|||
self.wireframe.addChild(self.color) |
|||
Then, we have the view provider object, responsible for showing the object in the 3D scene: |
|||
self.wireframe.addChild(data) |
|||
<syntaxhighlight> |
|||
obj.addDisplayMode(self.wireframe, "Wireframe"); |
|||
class ViewProviderOctahedron: |
|||
self.onChanged(obj,"Color") |
|||
"Set this object to the proxy object of the actual view provider" |
|||
def updateData(self, fp, prop): |
|||
obj.addProperty("App::PropertyColor","Color","Octahedron","Color of the octahedron").Color=(1.0,0.0,0.0) |
|||
'''If a property of the handled feature has changed we have the chance to handle this here''' |
|||
obj.Proxy = self |
|||
# fp is the handled feature, prop is the name of the property that has changed |
|||
l = fp.getPropertyByName("Length") |
|||
def attach(self, obj): |
|||
w = fp.getPropertyByName("Width") |
|||
"Setup the scene sub-graph of the view provider, this method is mandatory" |
|||
h = fp.getPropertyByName("Height") |
|||
self.scale.scaleFactor.setValue(float(l), float(w), float(h)) |
|||
pass |
|||
self.color = coin.SoBaseColor() |
|||
def getDisplayModes(self,obj): |
|||
'''Return a list of display modes.''' |
|||
self.data=coin.SoCoordinate3() |
|||
modes=[] |
|||
modes.append("Shaded") |
|||
modes.append("Wireframe") |
|||
return modes |
|||
self.shaded.addChild(self.color) |
|||
self.shaded.addChild(self.data) |
|||
def getDefaultDisplayMode(self): |
|||
'''Return the name of the default display mode. It must be defined in getDisplayModes.''' |
|||
obj.addDisplayMode(self.shaded,"Shaded"); |
|||
return "Shaded" |
|||
style.style = coin.SoDrawStyle.LINES |
|||
def setDisplayMode(self,mode): |
|||
self.wireframe.addChild(style) |
|||
'''Map the display mode defined in attach with those defined in getDisplayModes.\ |
|||
self.wireframe.addChild(self.scale) |
|||
Since they have the same names nothing needs to be done. This method is optional''' |
|||
self.wireframe.addChild(self.color) |
|||
return mode |
|||
self.wireframe.addChild(self.data) |
|||
self.wireframe.addChild(self.face) |
|||
def onChanged(self, vp, prop): |
|||
obj.addDisplayMode(self.wireframe,"Wireframe"); |
|||
'''Here we can do something when a single property got changed''' |
|||
self.onChanged(obj,"Color") |
|||
FreeCAD.Console.PrintMessage("Change property: " + str(prop) + "\n") |
|||
if prop == "Color": |
|||
c = vp.getPropertyByName("Color") |
|||
"If a property of the handled feature has changed we have the chance to handle this here" |
|||
self.color.rgb.setValue(c[0], c[1], c[2]) |
|||
# fp is the handled feature, prop is the name of the property that has changed |
|||
if prop == "Shape": |
|||
def getIcon(self): |
|||
s = fp.getPropertyByName("Shape") |
|||
'''Return the icon in XPM format which will appear in the tree view. This method is\ |
|||
self.data.point.setNum(6) |
|||
optional and if not defined a default icon is shown.''' |
|||
cnt=0 |
|||
return """ |
|||
/* XPM */ |
|||
static const char * ViewProviderBox_xpm[] = { |
|||
"16 16 6 1", |
|||
" c None", |
|||
". c #141010", |
|||
"+ c #615BD2", |
|||
"@ c #C39D55", |
|||
"# c #000000", |
|||
"$ c #57C355", |
|||
" ........", |
|||
" ......++..+..", |
|||
" .@@@@.++..++.", |
|||
" .@@@@.++..++.", |
|||
" .@@ .++++++.", |
|||
" ..@@ .++..++.", |
|||
"###@@@@ .++..++.", |
|||
"##$.@@$#.++++++.", |
|||
"#$#$.$$$........", |
|||
"#$$####### ", |
|||
"#$$#$$$$$# ", |
|||
"#$$#$$$$$# ", |
|||
"#$$#$$$$$# ", |
|||
" #$#$$$$$# ", |
|||
" ##$$$$$# ", |
|||
" ####### "}; |
|||
""" |
|||
self.face.coordIndex.set1Value(19,-1) |
|||
def dumps(self): |
|||
'''When saving the document this object gets stored using Python's json module.\ |
|||
self.face.coordIndex.set1Value(20,3) |
|||
Since we have some un-serializable parts here -- the Coin stuff -- we must define this method\ |
|||
self.face.coordIndex.set1Value(21,1) |
|||
to return a tuple of all serializable objects or None.''' |
|||
self.face.coordIndex.set1Value(22,5) |
|||
return None |
|||
self.face.coordIndex.set1Value(23,-1) |
|||
def loads(self,state): |
|||
self.face.coordIndex.set1Value(24,4) |
|||
'''When restoring the serialized object from document we have the chance to set some internals here.\ |
|||
self.face.coordIndex.set1Value(25,3) |
|||
Since no data were serialized nothing needs to be done here.''' |
|||
self.face.coordIndex.set1Value(26,5) |
|||
return None |
|||
self.face.coordIndex.set1Value(27,-1) |
|||
def makeBox(): |
|||
self.face.coordIndex.set1Value(28,0) |
|||
FreeCAD.newDocument() |
|||
self.face.coordIndex.set1Value(29,4) |
|||
a=FreeCAD.ActiveDocument.addObject("App::FeaturePython", "Box") |
|||
self.face.coordIndex.set1Value(30,5) |
|||
Box(a) |
|||
self.face.coordIndex.set1Value(31,-1) |
|||
ViewProviderBox(a.ViewObject) |
|||
def getDisplayModes(self,obj): |
|||
makeBox() |
|||
"Return a list of display modes." |
|||
}} |
|||
modes=[] |
|||
modes.append("Shaded") |
|||
=== Things to note === |
|||
modes.append("Wireframe") |
|||
return modes |
|||
If your object relies on being recomputed as soon as it is created, you must do this manually in the {{incode|__init__}} function as it is not called automatically. This example does not require it because the {{incode|onChanged}} method of the {{incode|Box}} class has the same effect as the {{incode|execute}} function, but the examples below rely on being recomputed before anything is displayed in the 3D view. In the examples, this is done manually with {{incode|ActiveDocument.recompute()}} but in more complex scenarios you need to decide where to recompute either the whole document or the FeaturePython object. |
|||
def getDefaultDisplayMode(self): |
|||
This example produces a number of exception stack traces in the report view window. This is because the {{incode|onChanged}} method of the {{incode|Box}} class is called each time a property is added in {{incode|__init__}}. When the first one is added, the Width and Height properties don't exist yet and so the attempt to access them fails. |
|||
"Return the name of the default display mode. It must be defined in getDisplayModes." |
|||
return "Shaded" |
|||
Eine Erklärung zu {{incode|__getstate__}} und {{incode|__setstate__}}, die durch {{incode|dumps}} und {{incode|loads}} ersetzt wurden, befindet sich im Forumsbeitrag [https://forum.freecadweb.org/viewtopic.php?f=18&t=44009&start=10#p377892 obj.Proxy.Type is a dict, not a string]. |
|||
def setDisplayMode(self,mode): |
|||
{{incode|obj.addProperty(...)}} returns {{incode|obj}}, so that the value of the property can be set on the same line: |
|||
return mode |
|||
{{Code|code= |
|||
def onChanged(self, vp, prop): |
|||
obj.addProperty("App::PropertyLength", "Length", "Box", "Length of the box").Length = 1.0 |
|||
"Here we can do something when a single property got changed" |
|||
}} |
|||
FreeCAD.Console.PrintMessage("Change property: " + str(prop) + "\n") |
|||
if prop == "Color": |
|||
Which is equivalent to: |
|||
c = vp.getPropertyByName("Color") |
|||
self.color.rgb.setValue(c[0],c[1],c[2]) |
|||
{{Code|code= |
|||
obj.addProperty("App::PropertyLength", "Length", "Box", "Length of the box") |
|||
def getIcon(self): |
|||
obj.Length = 1.0 |
|||
return """ |
|||
}} |
|||
/* XPM */ |
|||
static const char * ViewProviderBox_xpm[] = { |
|||
<span id="Other_more_complex_example"></span> |
|||
"16 16 6 1", |
|||
==Andere komplexere Beispiele== |
|||
" c None", |
|||
". c #141010", |
|||
Dieses Beispiel verwendet das [[Part_Workbench/de|Part-Modul]], um ein Oktaeder zu erstellen und dann seine Coin-Darstellung mit pivy. |
|||
"+ c #615BD2", |
|||
"@ c #C39D55", |
|||
Das Erste ist das Documentobjekt selbst: |
|||
"# c #000000", |
|||
"$ c #57C355", |
|||
{{Code|code= |
|||
" ........", |
|||
import FreeCAD, FreeCADGui, Part |
|||
" ......++..+..", |
|||
import pivy |
|||
" .@@@@.++..++.", |
|||
from pivy import coin |
|||
" .@@@@.++..++.", |
|||
" .@@ .++++++.", |
|||
class Octahedron: |
|||
" ..@@ .++..++.", |
|||
def __init__(self, obj): |
|||
"###@@@@ .++..++.", |
|||
"Add some custom properties to our box feature" |
|||
obj.addProperty("App::PropertyLength","Length","Octahedron","Length of the octahedron").Length=1.0 |
|||
"#$#$.$$$........", |
|||
obj.addProperty("App::PropertyLength","Width","Octahedron","Width of the octahedron").Width=1.0 |
|||
"#$$####### ", |
|||
obj.addProperty("App::PropertyLength","Height","Octahedron", "Height of the octahedron").Height=1.0 |
|||
"#$$#$$$$$# ", |
|||
obj.addProperty("Part::PropertyPartShape","Shape","Octahedron", "Shape of the octahedron") |
|||
"#$$#$$$$$# ", |
|||
obj.Proxy = self |
|||
"#$$#$$$$$# ", |
|||
" #$#$$$$$# ", |
|||
def execute(self, fp): |
|||
" ##$$$$$# ", |
|||
# Define six vetices for the shape |
|||
v1 = FreeCAD.Vector(0,0,0) |
|||
v2 = FreeCAD.Vector(fp.Length,0,0) |
|||
v3 = FreeCAD.Vector(0,fp.Width,0) |
|||
v4 = FreeCAD.Vector(fp.Length,fp.Width,0) |
|||
return None |
|||
v5 = FreeCAD.Vector(fp.Length/2,fp.Width/2,fp.Height/2) |
|||
v6 = FreeCAD.Vector(fp.Length/2,fp.Width/2,-fp.Height/2) |
|||
def __setstate__(self,state): |
|||
return None |
|||
# Make the wires/faces |
|||
</syntaxhighlight> |
|||
f1 = self.make_face(v1,v2,v5) |
|||
Finally, once our object and its viewobject are defined, we just need to call them: |
|||
f2 = self.make_face(v2,v4,v5) |
|||
<syntaxhighlight> |
|||
f3 = self.make_face(v4,v3,v5) |
|||
f4 = self.make_face(v3,v1,v5) |
|||
a=FreeCAD.ActiveDocument.addObject("App::FeaturePython","Octahedron") |
|||
f5 = self.make_face(v2,v1,v6) |
|||
f6 = self.make_face(v4,v2,v6) |
|||
ViewProviderOctahedron(a.ViewObject) |
|||
f7 = self.make_face(v3,v4,v6) |
|||
</syntaxhighlight> |
|||
f8 = self.make_face(v1,v3,v6) |
|||
== Making objects selectable == |
|||
shell=Part.makeShell([f1,f2,f3,f4,f5,f6,f7,f8]) |
|||
If you want to make your object selectable, or at least part of it, by clicking on it in the viewport, you must include its coin geometry inside a SoFCSelection node. If your object has complex representation, with widgets, annotations, etc, you might want to include only a part of it in a SoFCSelection. Everything that is a SoFCSelection is constantly scanned by FreeCAD to detect selection/preselection, so it makes sense try not to overload it with unneeded scanning. This is what you would do to include a self.face from the example above: |
|||
solid=Part.makeSolid(shell) |
|||
<syntaxhighlight> |
|||
fp.Shape = solid |
|||
selectionNode = coin.SoType.fromName("SoFCSelection").createInstance() |
|||
selectionNode.documentName.setValue(FreeCAD.ActiveDocument.Name) |
|||
# helper mehod to create the faces |
|||
selectionNode.objectName.setValue(obj.Object.Name) # here obj is the ViewObject, we need its associated App Object |
|||
def make_face(self,v1,v2,v3): |
|||
selectionNode.subElementName.setValue("Face") |
|||
wire = Part.makePolygon([v1,v2,v3,v1]) |
|||
selectNode.addChild(self.face) |
|||
face = Part.Face(wire) |
|||
... |
|||
return face |
|||
self.shaded.addChild(selectionNode) |
|||
}} |
|||
self.wireframe.addChild(selectionNode) |
|||
</syntaxhighlight> |
|||
Dann haben wir das View Provider Objekt, das für die Darstellung des Objekts in der 3D Szene verantwortlich ist: |
|||
Simply, you create a SoFCSelection node, then you add your geometry nodes to it, then you add it to your main node, instead of adding your geometry nodes directly. |
|||
{{Code|code= |
|||
class ViewProviderOctahedron: |
|||
def __init__(self, obj): |
|||
"Set this object to the proxy object of the actual view provider" |
|||
obj.addProperty("App::PropertyColor","Color","Octahedron","Color of the octahedron").Color=(1.0,0.0,0.0) |
|||
obj.Proxy = self |
|||
def attach(self, obj): |
|||
"Setup the scene sub-graph of the view provider, this method is mandatory" |
|||
self.shaded = coin.SoGroup() |
|||
self.wireframe = coin.SoGroup() |
|||
self.scale = coin.SoScale() |
|||
self.color = coin.SoBaseColor() |
|||
self.data=coin.SoCoordinate3() |
|||
self.face=coin.SoIndexedFaceSet() |
|||
self.shaded.addChild(self.scale) |
|||
self.shaded.addChild(self.color) |
|||
self.shaded.addChild(self.data) |
|||
self.shaded.addChild(self.face) |
|||
obj.addDisplayMode(self.shaded,"Shaded"); |
|||
style=coin.SoDrawStyle() |
|||
style.style = coin.SoDrawStyle.LINES |
|||
self.wireframe.addChild(style) |
|||
self.wireframe.addChild(self.scale) |
|||
self.wireframe.addChild(self.color) |
|||
self.wireframe.addChild(self.data) |
|||
self.wireframe.addChild(self.face) |
|||
obj.addDisplayMode(self.wireframe,"Wireframe"); |
|||
self.onChanged(obj,"Color") |
|||
def updateData(self, fp, prop): |
|||
"If a property of the handled feature has changed we have the chance to handle this here" |
|||
# fp is the handled feature, prop is the name of the property that has changed |
|||
if prop == "Shape": |
|||
s = fp.getPropertyByName("Shape") |
|||
self.data.point.setNum(6) |
|||
cnt=0 |
|||
for i in s.Vertexes: |
|||
self.data.point.set1Value(cnt,i.X,i.Y,i.Z) |
|||
cnt=cnt+1 |
|||
self.face.coordIndex.set1Value(0,0) |
|||
self.face.coordIndex.set1Value(1,1) |
|||
self.face.coordIndex.set1Value(2,2) |
|||
self.face.coordIndex.set1Value(3,-1) |
|||
self.face.coordIndex.set1Value(4,1) |
|||
self.face.coordIndex.set1Value(5,3) |
|||
self.face.coordIndex.set1Value(6,2) |
|||
self.face.coordIndex.set1Value(7,-1) |
|||
self.face.coordIndex.set1Value(8,3) |
|||
self.face.coordIndex.set1Value(9,4) |
|||
self.face.coordIndex.set1Value(10,2) |
|||
self.face.coordIndex.set1Value(11,-1) |
|||
self.face.coordIndex.set1Value(12,4) |
|||
self.face.coordIndex.set1Value(13,0) |
|||
self.face.coordIndex.set1Value(14,2) |
|||
self.face.coordIndex.set1Value(15,-1) |
|||
self.face.coordIndex.set1Value(16,1) |
|||
self.face.coordIndex.set1Value(17,0) |
|||
self.face.coordIndex.set1Value(18,5) |
|||
self.face.coordIndex.set1Value(19,-1) |
|||
self.face.coordIndex.set1Value(20,3) |
|||
self.face.coordIndex.set1Value(21,1) |
|||
self.face.coordIndex.set1Value(22,5) |
|||
self.face.coordIndex.set1Value(23,-1) |
|||
self.face.coordIndex.set1Value(24,4) |
|||
self.face.coordIndex.set1Value(25,3) |
|||
self.face.coordIndex.set1Value(26,5) |
|||
self.face.coordIndex.set1Value(27,-1) |
|||
self.face.coordIndex.set1Value(28,0) |
|||
self.face.coordIndex.set1Value(29,4) |
|||
self.face.coordIndex.set1Value(30,5) |
|||
self.face.coordIndex.set1Value(31,-1) |
|||
def getDisplayModes(self,obj): |
|||
"Return a list of display modes." |
|||
modes=[] |
|||
modes.append("Shaded") |
|||
modes.append("Wireframe") |
|||
return modes |
|||
def getDefaultDisplayMode(self): |
|||
"Return the name of the default display mode. It must be defined in getDisplayModes." |
|||
return "Shaded" |
|||
def setDisplayMode(self,mode): |
|||
return mode |
|||
def onChanged(self, vp, prop): |
|||
"Here we can do something when a single property got changed" |
|||
FreeCAD.Console.PrintMessage("Change property: " + str(prop) + "\n") |
|||
if prop == "Color": |
|||
c = vp.getPropertyByName("Color") |
|||
self.color.rgb.setValue(c[0],c[1],c[2]) |
|||
def getIcon(self): |
|||
return """ |
|||
/* XPM */ |
|||
static const char * ViewProviderBox_xpm[] = { |
|||
"16 16 6 1", |
|||
" c None", |
|||
". c #141010", |
|||
"+ c #615BD2", |
|||
"@ c #C39D55", |
|||
"# c #000000", |
|||
"$ c #57C355", |
|||
" ........", |
|||
" ......++..+..", |
|||
" .@@@@.++..++.", |
|||
" .@@@@.++..++.", |
|||
" .@@ .++++++.", |
|||
" ..@@ .++..++.", |
|||
"###@@@@ .++..++.", |
|||
"##$.@@$#.++++++.", |
|||
"#$#$.$$$........", |
|||
"#$$####### ", |
|||
"#$$#$$$$$# ", |
|||
"#$$#$$$$$# ", |
|||
"#$$#$$$$$# ", |
|||
" #$#$$$$$# ", |
|||
" ##$$$$$# ", |
|||
" ####### "}; |
|||
""" |
|||
def dumps(self): |
|||
return None |
|||
def loads(self,state): |
|||
return None |
|||
}} |
|||
Schließlich, sobald unser Objekt und sein Viewobjekt definiert sind, müssen wir sie nur noch aufrufen (Der Code der Octahedron Klasse und der Viewprovider Klasse könnte direkt in die FreeCAD Python Konsole kopiert werden): |
|||
{{Code|code= |
|||
FreeCAD.newDocument() |
|||
a=FreeCAD.ActiveDocument.addObject("App::FeaturePython","Octahedron") |
|||
Octahedron(a) |
|||
ViewProviderOctahedron(a.ViewObject) |
|||
}} |
|||
<span id="Making_objects_selectable"></span> |
|||
== Objekte auswählbar machen == |
|||
Wenn du dein Objekt oder zumindest einen Teil davon durch Anklicken im Ansichtsfenster auswählbar machen möchtest, musst du seine Coin-Geometrie in einen SoFCSelection-Knoten aufnehmen. Wenn dein Objekt eine komplexe Darstellung hat, mit Widgets, Anmerkungen usw., möchtest du vielleicht nur einen Teil davon in eine SoFCSelection einschließen. Alles, was eine SoFCSelection ist, wird von FreeCAD ständig gescannt, um Auswahl/Vorwahl zu erkennen, daher ist es sinnvoll, es nicht mit unnötigem Scannen zu überlasten. |
|||
Once the parts of the scenegraph that are to be selectable are inside SoFCSelection nodes, you then need to provide two methods to handle the selection path. The selection path can take the form of a string giving the names of each element in the path, or of an array of scenegraph objects. The two methods you provide are {{incode|getDetailPath}}, which converts from a string path to an array of scenegraph objects, and {{incode|getElementPicked}}, which takes an element which has been clicked on in the scenegraph and returns its string name (note, not its string path). |
|||
Hier ist das obige Molekülbeispiel, angepasst, um die Elemente des Moleküls auswählbar zu machen: |
|||
{{Code|code= |
|||
class Molecule: |
|||
def __init__(self, obj): |
|||
''' Add two point properties ''' |
|||
obj.addProperty("App::PropertyVector","p1","Line","Start point") |
|||
obj.addProperty("App::PropertyVector","p2","Line","End point").p2=FreeCAD.Vector(5,0,0) |
|||
obj.Proxy = self |
|||
def onChanged(self, fp, prop): |
|||
if prop == "p1" or prop == "p2": |
|||
''' Print the name of the property that has changed ''' |
|||
fp.Shape = Part.makeLine(fp.p1,fp.p2) |
|||
def execute(self, fp): |
|||
''' Print a short message when doing a recomputation, this method is mandatory ''' |
|||
fp.Shape = Part.makeLine(fp.p1,fp.p2) |
|||
class ViewProviderMolecule: |
|||
def __init__(self, obj): |
|||
''' Set this object to the proxy object of the actual view provider ''' |
|||
obj.Proxy = self |
|||
self.ViewObject = obj |
|||
sep1=coin.SoSeparator() |
|||
sel1 = coin.SoType.fromName('SoFCSelection').createInstance() |
|||
# sel1.policy.setValue(coin.SoSelection.SHIFT) |
|||
sel1.ref() |
|||
sep1.addChild(sel1) |
|||
self.trl1=coin.SoTranslation() |
|||
sel1.addChild(self.trl1) |
|||
sel1.addChild(coin.SoSphere()) |
|||
sep2=coin.SoSeparator() |
|||
sel2 = coin.SoType.fromName('SoFCSelection').createInstance() |
|||
sel2.ref() |
|||
sep2.addChild(sel2) |
|||
self.trl2=coin.SoTranslation() |
|||
sel2.addChild(self.trl2) |
|||
sel2.addChild(coin.SoSphere()) |
|||
obj.RootNode.addChild(sep1) |
|||
obj.RootNode.addChild(sep2) |
|||
self.updateData(obj.Object, 'p2') |
|||
self.sel1 = sel1 |
|||
self.sel2 = sel2 |
|||
def getDetailPath(self, subname, path, append): |
|||
vobj = self.ViewObject |
|||
if append: |
|||
path.append(vobj.RootNode) |
|||
path.append(vobj.SwitchNode) |
|||
mode = vobj.SwitchNode.whichChild.getValue() |
|||
if mode >= 0: |
|||
mode = vobj.SwitchNode.getChild(mode) |
|||
path.append(mode) |
|||
sub = Part.splitSubname(subname)[-1] |
|||
if sub == 'Atom1': |
|||
path.append(self.sel1) |
|||
elif sub == 'Atom2': |
|||
path.append(self.sel2) |
|||
else: |
|||
path.append(mode.getChild(0)) |
|||
return True |
|||
def getElementPicked(self, pp): |
|||
path = pp.getPath() |
|||
if path.findNode(self.sel1) >= 0: |
|||
return 'Atom1' |
|||
if path.findNode(self.sel2) >= 0: |
|||
return 'Atom2' |
|||
raise NotImplementedError |
|||
def updateData(self, fp, prop): |
|||
"If a property of the handled feature has changed we have the chance to handle this here" |
|||
# fp is the handled feature, prop is the name of the property that has changed |
|||
if prop == "p1": |
|||
p = fp.getPropertyByName("p1") |
|||
self.trl1.translation=(p.x,p.y,p.z) |
|||
elif prop == "p2": |
|||
p = fp.getPropertyByName("p2") |
|||
self.trl2.translation=(p.x,p.y,p.z) |
|||
def dumps(self): |
|||
return None |
|||
def loads(self,state): |
|||
return None |
|||
def makeMolecule(): |
|||
FreeCAD.newDocument() |
|||
a=FreeCAD.ActiveDocument.addObject("Part::FeaturePython","Molecule") |
|||
Molecule(a) |
|||
ViewProviderMolecule(a.ViewObject) |
|||
FreeCAD.ActiveDocument.recompute() |
|||
}} |
|||
== Working with simple shapes == |
== Working with simple shapes == |
||
If your parametric object simply outputs a shape, you don't need to use a view provider object. The shape will be displayed using FreeCAD's standard shape representation: |
|||
Wenn ein parametrisches Objekt einfach nur eine Form ausgibt, muss man keinen View-Provider-Objekt verwenden. Die Form wird mit FreeCADs Standard-Form-Darstellung angezeigt. |
|||
<syntaxhighlight> |
|||
class Line: |
|||
{{Code|code= |
|||
def __init__(self, obj): |
|||
import FreeCAD as App |
|||
import FreeCADGui |
|||
import FreeCAD |
|||
import Part |
|||
class Line: |
|||
def __init__(self, obj): |
|||
'''"App two point properties" ''' |
|||
obj.addProperty("App::PropertyVector","p1","Line","Start point") |
|||
obj.addProperty("App::PropertyVector","p2","Line","End point").p2=FreeCAD.Vector(1,0,0) |
|||
obj.Proxy = self |
|||
def execute(self, fp): |
|||
'''"Print a short message when doing a recomputation, this method is mandatory" ''' |
|||
fp.Shape = Part.makeLine(fp.p1,fp.p2) |
|||
a=FreeCAD.ActiveDocument.addObject("Part::FeaturePython","Line") |
|||
Line(a) |
|||
a.ViewObject.Proxy=0 # just set it to something different from None (this assignment is needed to run an internal notification) |
|||
FreeCAD.ActiveDocument.recompute() |
|||
}} |
|||
Gleicher Code unter Verwendung von '''ViewProviderLine''' |
|||
{{Code|code= |
|||
import FreeCAD as App |
|||
import FreeCADGui |
|||
import FreeCAD |
|||
import Part |
|||
class Line: |
|||
def __init__(self, obj): |
|||
'''"App two point properties" ''' |
'''"App two point properties" ''' |
||
obj.addProperty("App::PropertyVector","p1","Line","Start point") |
obj.addProperty("App::PropertyVector","p1","Line","Start point") |
||
obj.addProperty("App::PropertyVector","p2","Line","End point").p2=FreeCAD.Vector( |
obj.addProperty("App::PropertyVector","p2","Line","End point").p2=FreeCAD.Vector(100,0,0) |
||
obj.Proxy = self |
obj.Proxy = self |
||
def execute(self, fp): |
|||
'''"Print a short message when doing a recomputation, this method is mandatory" ''' |
|||
fp.Shape = Part.makeLine(fp.p1,fp.p2) |
|||
a=FreeCAD.ActiveDocument.addObject("Part::FeaturePython","Line") |
|||
Line(a) |
|||
a.ViewObject.Proxy=0 # just set it to something different from None (this assignment is needed to run an internal notification) |
|||
FreeCAD.ActiveDocument.recompute() |
|||
</syntaxhighlight> |
|||
def execute(self, fp): |
|||
{{docnav|PyQt|Embedding FreeCAD}} |
|||
'''"Print a short message when doing a recomputation, this method is mandatory" ''' |
|||
fp.Shape = Part.makeLine(fp.p1,fp.p2) |
|||
class ViewProviderLine: |
|||
[[Category:Poweruser Documentation]] |
|||
def __init__(self, obj): |
|||
[[Category:Python Code]] |
|||
''' Set this object to the proxy object of the actual view provider ''' |
|||
<languages/> |
|||
obj.Proxy = self |
|||
def getDefaultDisplayMode(self): |
|||
''' Return the name of the default display mode. It must be defined in getDisplayModes. ''' |
|||
return "Flat Lines" |
|||
a=FreeCAD.ActiveDocument.addObject("Part::FeaturePython","Line") |
|||
Line(a) |
|||
ViewProviderLine(a.ViewObject) |
|||
App.ActiveDocument.recompute() |
|||
}} |
|||
== Scenegraph Structure == |
|||
Du hast vielleicht bemerkt, dass die obigen Beispiele deine Szenengraphen auf leicht unterschiedliche Weise aufbauen. Einige verwenden {{incode|obj.addDisplayMode(node, "modename")}}, während andere {{incode|obj.SwitchNode.getChild(x).addChild(y)}} verwenden. |
|||
Each feature in a FreeCAD document is based the following scenegraph structure: |
|||
{{Code|code= |
|||
RootNode |
|||
\- SwitchNode |
|||
\- Shaded |
|||
- Wireframe |
|||
- etc |
|||
}} |
|||
The {{incode|SwitchNode}} displays only one of its children, depending on which display mode is selection in FreeCAD. |
|||
The examples which use {{incode|addDisplayMode}} are constructing their scenegraphs solely out of coin3d scenegraph elements. Under the covers, {{incode|addDisplayMode}} adds a new child to the {{incode|SwitchNode}}; the name of that node will match the display mode it was passed. |
|||
The examples which use {{incode|SwitchNode.getChild(x).addChild}} also construct part of their geometry using functions from the Part workbench, such as {{incode|1=fp.Shape = Part.makeLine(fp.p1,fp.p2)}}. This constructs the different display mode scenegraphs under the {{incode|SwitchNode}}; when we later come to add coin3d elements to the scenegraph, we need to add them to the existing display mode scenegraphs using {{incode|addChild}} rather than creating a new child of the {{incode|SwitchNode}}. |
|||
When using {{incode|addDisplayMode()}} to add geometry to the scenegraph, each display mode should have its own node which is passed to {{incode|addDisplayMode()}}; don't reuse the same node for this. Doing so will confuse the selection mechanism. It's okay if each display mode's node has the same geometry nodes added below it, just the root of each display mode needs to be distinct. |
|||
Here is the above molecule example, adapted to be drawn only with Coin3D scenegraph objects instead of using objects from the Part workbench: |
|||
{{Code|code= |
|||
import Part |
|||
from pivy import coin |
|||
class Molecule: |
|||
def __init__(self, obj): |
|||
''' Add two point properties ''' |
|||
obj.addProperty("App::PropertyVector","p1","Line","Start point") |
|||
obj.addProperty("App::PropertyVector","p2","Line","End point").p2=FreeCAD.Vector(5,0,0) |
|||
obj.Proxy = self |
|||
def onChanged(self, fp, prop): |
|||
pass |
|||
def execute(self, fp): |
|||
''' Print a short message when doing a recomputation, this method is mandatory ''' |
|||
pass |
|||
class ViewProviderMolecule: |
|||
def __init__(self, obj): |
|||
''' Set this object to the proxy object of the actual view provider ''' |
|||
self.constructed = False |
|||
obj.Proxy = self |
|||
self.ViewObject = obj |
|||
def attach(self, obj): |
|||
material = coin.SoMaterial() |
|||
material.diffuseColor = (1.0, 0.0, 0.0) |
|||
material.emissiveColor = (1.0, 0.0, 0.0) |
|||
drawStyle = coin.SoDrawStyle() |
|||
drawStyle.pointSize.setValue(10) |
|||
drawStyle.style = coin.SoDrawStyle.LINES |
|||
wireframe = coin.SoGroup() |
|||
shaded = coin.SoGroup() |
|||
self.wireframe = wireframe |
|||
self.shaded = shaded |
|||
self.coords = coin.SoCoordinate3() |
|||
self.coords.point.setValues(0, 2, [FreeCAD.Vector(0, 0, 0), FreeCAD.Vector(1, 0, 0)]) |
|||
wireframe += self.coords |
|||
wireframe += drawStyle |
|||
wireframe += material |
|||
shaded += self.coords |
|||
shaded += drawStyle |
|||
shaded += material |
|||
g = coin.SoGroup() |
|||
sel1 = coin.SoType.fromName('SoFCSelection').createInstance() |
|||
sel1.style = 'EMISSIVE_DIFFUSE' |
|||
p1 = coin.SoType.fromName('SoIndexedPointSet').createInstance() |
|||
p1.coordIndex.set1Value(0, 0) |
|||
sel1 += p1 |
|||
g += sel1 |
|||
wireframe += g |
|||
shaded += g |
|||
g = coin.SoGroup() |
|||
sel2 = coin.SoType.fromName('SoFCSelection').createInstance() |
|||
sel2.style = 'EMISSIVE_DIFFUSE' |
|||
p2 = coin.SoType.fromName('SoIndexedPointSet').createInstance() |
|||
p2.coordIndex.set1Value(0, 1) |
|||
sel2 += p2 |
|||
g += sel2 |
|||
wireframe += g |
|||
shaded += g |
|||
g = coin.SoGroup() |
|||
sel3 = coin.SoType.fromName('SoFCSelection').createInstance() |
|||
sel3.style = 'EMISSIVE_DIFFUSE' |
|||
p3 = coin.SoType.fromName('SoIndexedLineSet').createInstance() |
|||
p3.coordIndex.setValues(0, 2, [0, 1]) |
|||
sel3 += p3 |
|||
g += sel3 |
|||
wireframe += g |
|||
shaded += g |
|||
obj.addDisplayMode(wireframe, 'Wireframe') |
|||
obj.addDisplayMode(shaded, 'Shaded') |
|||
self.sel1 = sel1 |
|||
self.sel2 = sel2 |
|||
self.sel3 = sel3 |
|||
self.constructed = True |
|||
self.updateData(obj.Object, 'p2') |
|||
def getDetailPath(self, subname, path, append): |
|||
vobj = self.ViewObject |
|||
if append: |
|||
path.append(vobj.RootNode) |
|||
path.append(vobj.SwitchNode) |
|||
mode = vobj.SwitchNode.whichChild.getValue() |
|||
FreeCAD.Console.PrintWarning("getDetailPath: mode {} is active\n".format(mode)) |
|||
if mode >= 0: |
|||
mode = vobj.SwitchNode.getChild(mode) |
|||
path.append(mode) |
|||
sub = Part.splitSubname(subname)[-1] |
|||
print(sub) |
|||
if sub == 'Atom1': |
|||
path.append(self.sel1) |
|||
elif sub == 'Atom2': |
|||
path.append(self.sel2) |
|||
elif sub == 'Line': |
|||
path.append(self.sel3) |
|||
else: |
|||
path.append(mode.getChild(0)) |
|||
return True |
|||
def getElementPicked(self, pp): |
|||
path = pp.getPath() |
|||
if path.findNode(self.sel1) >= 0: |
|||
return 'Atom1' |
|||
if path.findNode(self.sel2) >= 0: |
|||
return 'Atom2' |
|||
if path.findNode(self.sel3) >= 0: |
|||
return 'Line' |
|||
raise NotImplementedError |
|||
def updateData(self, fp, prop): |
|||
"If a property of the handled feature has changed we have the chance to handle this here" |
|||
# fp is the handled feature, prop is the name of the property that has changed |
|||
if not self.constructed: |
|||
return |
|||
if prop == "p1": |
|||
p = fp.getPropertyByName("p1") |
|||
self.coords.point.set1Value(0, p) |
|||
elif prop == "p2": |
|||
p = fp.getPropertyByName("p2") |
|||
self.coords.point.set1Value(1, p) |
|||
def getDisplayModes(self, obj): |
|||
return ['Wireframe', 'Shaded'] |
|||
def getDefaultDisplayMode(self): |
|||
return 'Shaded' |
|||
def setDisplayMode(self, mode): |
|||
return mode |
|||
def dumps(self): |
|||
return None |
|||
def loads(self,state): |
|||
return None |
|||
def makeMolecule(): |
|||
FreeCAD.newDocument() |
|||
a=FreeCAD.ActiveDocument.addObject("App::FeaturePython","Molecule") |
|||
Molecule(a) |
|||
b=ViewProviderMolecule(a.ViewObject) |
|||
a.touch() |
|||
FreeCAD.ActiveDocument.recompute() |
|||
return a,b |
|||
a,b = makeMolecule() |
|||
}} |
|||
== Part Design scripted objects == |
|||
When making scripted objects in Part Design the process is similar to the scripted objects discussed above, but with a few additional considerations. We must handle 2 shape properties, one for the shape we see in the 3D view and another for the shape used by the pattern tools, such as polar pattern features. The object shapes also needs to be fused to any existing material already in the Body (or cut from it in the case of Subtractive features). And we must account for the placement and attachment of our objects a little bit differently. |
|||
Part Design scripted solid object features should be based on either PartDesign::FeaturePython, PartDesign::FeatureAdditivePython, or PartDesign::FeatureSubtractivePython rather than Part::FeaturePython. Only the Additive and Subtractive variants can be used in pattern features, and if based on Part::FeaturePython when the user drops the object into a Part Design Body it becomes a BaseFeature rather than being treated by the Body as a native Part Design object. Note: all of these are expected to be solids, so if you are making a non-solid feature it should be based on Part::FeaturePython or else the next feature in the tree will attempt to fuse to as a solid and it will fail. |
|||
Here is a simple example of making a Tube primitive, similar to the Tube primitive in Part Workbench except this one will be a Part Design solid feature object. For this we will 2 separate files: pdtube.FCMacro and pdtube.py. The .FCMacro file will be executed by the user to create the object. The .py file will hold the class definitions, imported by the .FCMacro. The reason for doing it this way is to maintain the parametric nature of the object after restarting FreeCAD and opening a document containing one of our Tubes. |
|||
First, the class definition file: |
|||
{{Code|code= |
|||
# -*- coding: utf-8 -*- |
|||
#classes should go in pdtube.py |
|||
import FreeCAD, FreeCADGui, Part |
|||
class PDTube: |
|||
def __init__(self,obj): |
|||
obj.addProperty("App::PropertyLength","Radius1","Tube","Radius1").Radius1 = 5 |
|||
obj.addProperty("App::PropertyLength","Radius2","Tube","Radius2").Radius2 = 10 |
|||
obj.addProperty("App::PropertyLength","Height","Tube","Height of tube").Height = 10 |
|||
self.makeAttachable(obj) |
|||
obj.Proxy = self |
|||
def makeAttachable(self, obj): |
|||
if int(FreeCAD.Version()[1]) >= 19: |
|||
obj.addExtension('Part::AttachExtensionPython') |
|||
else: |
|||
obj.addExtension('Part::AttachExtensionPython', obj) |
|||
obj.setEditorMode('Placement', 0) #non-readonly non-hidden |
|||
def execute(self,fp): |
|||
outer_cylinder = Part.makeCylinder(fp.Radius2, fp.Height) |
|||
inner_cylinder = Part.makeCylinder(fp.Radius1, fp.Height) |
|||
if fp.Radius1 == fp.Radius2: #just make cylinder |
|||
tube_shape = outer_cylinder |
|||
elif fp.Radius1 < fp.Radius2: |
|||
tube_shape = outer_cylinder.cut(inner_cylinder) |
|||
else: #invert rather than error out |
|||
tube_shape = inner_cylinder.cut(outer_cylinder) |
|||
if not hasattr(fp, "positionBySupport"): |
|||
self.makeAttachable(fp) |
|||
fp.positionBySupport() |
|||
tube_shape.Placement = fp.Placement |
|||
#BaseFeature (shape property of type Part::PropertyPartShape) is provided for us |
|||
#with the PartDesign::FeaturePython and related classes, but it might be empty |
|||
#if our object is the first object in the tree. it's a good idea to check |
|||
#for its existence in case we want to make type Part::FeaturePython, which won't have it |
|||
if hasattr(fp, "BaseFeature") and fp.BaseFeature != None: |
|||
if "Subtractive" in fp.TypeId: |
|||
full_shape = fp.BaseFeature.Shape.cut(tube_shape) |
|||
else: |
|||
full_shape = fp.BaseFeature.Shape.fuse(tube_shape) |
|||
full_shape.transformShape(fp.Placement.inverse().toMatrix(), True) #borrowed from gears workbench |
|||
fp.Shape = full_shape |
|||
else: |
|||
fp.Shape = tube_shape |
|||
if hasattr(fp,"AddSubShape"): #PartDesign::FeatureAdditivePython and |
|||
#PartDesign::FeatureSubtractivePython have this |
|||
#property but PartDesign::FeaturePython does not |
|||
#It is the shape used for copying in pattern features |
|||
#for example in making a polar pattern |
|||
tube_shape.transformShape(fp.Placement.inverse().toMatrix(), True) |
|||
fp.AddSubShape = tube_shape |
|||
class PDTubeVP: |
|||
def __init__(self, obj): |
|||
'''Set this object to the proxy object of the actual view provider''' |
|||
obj.Proxy = self |
|||
def attach(self,vobj): |
|||
self.vobj = vobj |
|||
def updateData(self, fp, prop): |
|||
'''If a property of the handled feature has changed we have the chance to handle this here''' |
|||
pass |
|||
def getDisplayModes(self,obj): |
|||
'''Return a list of display modes.''' |
|||
modes=[] |
|||
modes.append("Flat Lines") |
|||
modes.append("Shaded") |
|||
modes.append("Wireframe") |
|||
return modes |
|||
def getDefaultDisplayMode(self): |
|||
'''Return the name of the default display mode. It must be defined in getDisplayModes.''' |
|||
return "Flat Lines" |
|||
def setDisplayMode(self,mode): |
|||
'''Map the display mode defined in attach with those defined in getDisplayModes.\ |
|||
Since they have the same names nothing needs to be done. This method is optional''' |
|||
return mode |
|||
def onChanged(self, vp, prop): |
|||
'''Here we can do something when a single property got changed''' |
|||
#FreeCAD.Console.PrintMessage("Change property: " + str(prop) + "\n") |
|||
pass |
|||
def getIcon(self): |
|||
'''Return the icon in XPM format which will appear in the tree view. This method is\ |
|||
optional and if not defined a default icon is shown.''' |
|||
return """ |
|||
/* XPM */ |
|||
static const char * ViewProviderBox_xpm[] = { |
|||
"16 16 6 1", |
|||
" c None", |
|||
". c #141010", |
|||
"+ c #615BD2", |
|||
"@ c #C39D55", |
|||
"# c #000000", |
|||
"$ c #57C355", |
|||
" ........", |
|||
" ......++..+..", |
|||
" .@@@@.++..++.", |
|||
" .@@@@.++..++.", |
|||
" .@@ .++++++.", |
|||
" ..@@ .++..++.", |
|||
"###@@@@ .++..++.", |
|||
"##$.@@$#.++++++.", |
|||
"#$#$.$$$........", |
|||
"#$$####### ", |
|||
"#$$#$$$$$# ", |
|||
"#$$#$$$$$# ", |
|||
"#$$#$$$$$# ", |
|||
" #$#$$$$$# ", |
|||
" ##$$$$$# ", |
|||
" ####### "}; |
|||
""" |
|||
def dumps(self): |
|||
'''When saving the document this object gets stored using Python's json module.\ |
|||
Since we have some un-serializable parts here -- the Coin stuff -- we must define this method\ |
|||
to return a tuple of all serializable objects or None.''' |
|||
return None |
|||
def loads(self,state): |
|||
'''When restoring the serialized object from document we have the chance to set some internals here.\ |
|||
Since no data were serialized nothing needs to be done here.''' |
|||
return None |
|||
}} |
|||
And now the macro file to create the object: |
|||
{{Code|code= |
|||
# -*- coding: utf-8 -*- |
|||
#pdtube.FCMacro |
|||
import pdtube |
|||
#above line needed if the class definitions above are place in another file: PDTube.py |
|||
#this is needed if the tube object is to remain parametric after restarting FreeCAD and loading |
|||
#a document containing the object |
|||
body = FreeCADGui.ActiveDocument.ActiveView.getActiveObject("pdbody") |
|||
if not body: |
|||
FreeCAD.Console.PrintError("No active body.\n") |
|||
else: |
|||
from PySide import QtGui |
|||
window = FreeCADGui.getMainWindow() |
|||
items = ["Additive","Subtractive","Neither additive nor subtractive"] |
|||
item,ok =QtGui.QInputDialog.getItem(window,"Select tube type","Select whether you want additive, subtractive, or neither:",items,0,False) |
|||
if ok: |
|||
if item == items[0]: |
|||
className = "PartDesign::FeatureAdditivePython" |
|||
elif item == items[1]: |
|||
className = "PartDesign::FeatureSubtractivePython" |
|||
else: |
|||
className = "PartDesign::FeaturePython" #not usable in pattern features, such as polar pattern |
|||
tube = FreeCAD.ActiveDocument.addObject(className,"Tube") |
|||
pdtube.PDTube(tube) |
|||
pdtube.PDTubeVP(tube.ViewObject) |
|||
body.addObject(tube) #optionally we can also use body.insertObject() for placing at particular place in tree |
|||
}} |
|||
== Available object types == |
|||
The list of all object types you can create with {{incode|FreeCAD.ActiveDocument.addObject()}} can be obtained with {{incode|FreeCAD.ActiveDocument.supportedTypes()}}. Only object types with a name ending in {{incode|Python}} can be used for scripted objects. These are listed here (for FreeCAD v0.21): |
|||
* {{incode|App::DocumentObjectGroupPython}} |
|||
* {{incode|App::FeaturePython}} |
|||
* {{incode|App::GeometryPython}} |
|||
* {{incode|App::LinkElementPython}} |
|||
* {{incode|App::LinkGroupPython}} |
|||
* {{incode|App::LinkPython}} |
|||
* {{incode|App::MaterialObjectPython}} |
|||
* {{incode|App::PlacementPython}} |
|||
* {{incode|Part::CustomFeaturePython}} |
|||
* {{incode|Part::FeaturePython}} |
|||
* {{incode|Part::Part2DObjectPython}} |
|||
* {{incode|Path::FeatureAreaPython}} |
|||
* {{incode|Path::FeatureAreaViewPython}} |
|||
* {{incode|Path::FeatureCompoundPython}} |
|||
* {{incode|Path::FeaturePython}} |
|||
* {{incode|Path::FeatureShapePython}} |
|||
* {{incode|Sketcher::SketchObjectPython}} |
|||
* {{incode|TechDraw::DrawComplexSectionPython}} |
|||
* {{incode|TechDraw::DrawLeaderLinePython}} |
|||
* {{incode|TechDraw::DrawPagePython}} |
|||
* {{incode|TechDraw::DrawRichAnnoPython}} |
|||
* {{incode|TechDraw::DrawTemplatePython}} |
|||
* {{incode|TechDraw::DrawTilePython}} |
|||
* {{incode|TechDraw::DrawTileWeldPython}} |
|||
* {{incode|TechDraw::DrawViewPartPython}} |
|||
* {{incode|TechDraw::DrawViewPython}} |
|||
* {{incode|TechDraw::DrawViewSectionPython}} |
|||
* {{incode|TechDraw::DrawViewSymbolPython}} |
|||
* {{incode|TechDraw::DrawWeldSymbolPython}} |
|||
<span id="Available_methods"></span> |
|||
==Vorhandene Methoden== |
|||
Siehe [[FeaturePython_methods/de|FeaturePython Methoden]] für eine vollständige Referenz. |
|||
<span id="Available_properties"></span> |
|||
==Vorhandene Eigenschaften== |
|||
Eigenschaften sind die wahren Bausteine von FeaturePython-Objekten. Durch sie kann man mit einem Objekt interagieren und es ändern. Nach dem Erstellen eines neuen FeaturePython-Objekts in einem Dokument, kann man eine Liste der vorhandenen Eigenschaften bekommen: |
|||
{{Code|code= |
|||
obj = FreeCAD.ActiveDocument.addObject("App::FeaturePython", "Box") |
|||
obj.supportedProperties() |
|||
}} |
|||
Siehe [[FeaturePython_Custom_Properties/de|FunktionsPython benutzerdefinierte Eigenschaften]] für einen Überblick: |
|||
Beim Hinzufügen von Eigenschaften zu benutzerdefinierten Objekten bitte folgendes beachten: |
|||
* Nicht die Zeichen {{Incode|<}} oder {{Incode| >}} in den Beschreibungen der Eigenschaften verwenden (das würde die XML-Teile in der .FCStd-Datei auseinanderbrechen) |
|||
* Eigenschaften werden alphabetisch in einer .FCStd-Datei gespeichert. Befindet sich eine Form (shape) in den Eigenschaften, wird jede Eigenschaft, deren Name in alphabetischen Reihenfolge nach "Shape" kommt, auch NACH der Form geladen, was zu seltsamen Verhaltensweisen führen kann. |
|||
Die Eigenschaften sind in der C++ header-Datei [https://github.com/FreeCAD/FreeCAD/blob/master/src/App/PropertyStandard.h PropertyStandard.h] festgelegt. |
|||
<span id="Property_types"></span> |
|||
===Arten von Eigenschaften=== |
|||
Standardmäßig können Eigenschaften aktualisiert werden, aber es ist möglich, die Eigenschaften auf schreibgeschützt zu setzen, wenn man zum Beispiel das Ergebnis einer Methode anzeigen möchte. Es ist auch möglich, eine Eigenschaft auszublenden. Die Art der Eigenschaft kann festgelegt werden mit: |
|||
{{Code|code= |
|||
obj.setEditorMode("MyPropertyName", mode) |
|||
}} |
|||
wobei mode ein kurzer int ist, der gesetzt werden kann auf: |
|||
0 -- Standardmodus, Lesen und Schreiben |
|||
1 -- Nur-Lesen |
|||
2 -- Versteckt |
|||
The mode can also be set using a list of strings, e.g. {{incode|obj.setEditorMode("Placement", ["ReadOnly", "Hidden"])}}. |
|||
The EditorModes are not set at FreeCAD file reload. This could to be done by the {{Incode|loads}} function. See http://forum.freecadweb.org/viewtopic.php?f=18&t=13460&start=10#p108072. By using the {{Incode|setEditorMode}} the properties are only read-only in the [[Property_editor|Property editor]]. They can still be changed from Python. To really make them read-only the setting has to be passed directly inside the {{Incode|addProperty}} function. See http://forum.freecadweb.org/viewtopic.php?f=18&t=13460&start=20#p109709 for an example. |
|||
Using the direct setting in the {{Incode|addProperty}} function, you also have more possibilities. In particular, an interesting one is mark a property as an output property. This way FreeCAD won't mark the feature as touched when changing it (so no need to recompute). |
|||
Example of output property (see also https://forum.freecadweb.org/viewtopic.php?t=24928): |
|||
{{Code|code= |
|||
obj.addProperty("App::PropertyString", "MyCustomProperty", "", "", 8) |
|||
}} |
|||
The property types that can be set at last parameter of the addProperty function are: |
|||
0 -- Prop_None, No special property type |
|||
1 -- Prop_ReadOnly, Property is read-only in the editor |
|||
2 -- Prop_Transient, Property won't be saved to file |
|||
4 -- Prop_Hidden, Property won't appear in the editor |
|||
8 -- Prop_Output, Modified property doesn't touch its parent container |
|||
16 -- Prop_NoRecompute, Modified property doesn't touch its container for recompute |
|||
32 -- Prop_NoPersist, Property won't be saved to file at all |
|||
The property types are defined in the [https://github.com/FreeCAD/FreeCAD/blob/master/src/App/PropertyContainer.h PropertyContainer C++ header file]. |
|||
== Available extensions == |
|||
The list of available extensions can be obtained with {{incode|grep -RI EXTENSION_PROPERTY_SOURCE_TEMPLATE}} in the repository of the source code and is given here (for FreeCAD v0.21). |
|||
For objects: |
|||
* {{incode|App::GeoFeatureGroupExtensionPython}} |
|||
* {{incode|App::GroupExtensionPython}} |
|||
* {{incode|App::LinkBaseExtensionPython}} |
|||
* {{incode|App::LinkExtensionPython}} |
|||
* {{incode|App::OriginGroupExtensionPython}} |
|||
* {{incode|Part::AttachExtensionPython}} |
|||
* {{incode|TechDraw::CosmeticExtensionPython}} |
|||
For view objects: |
|||
* {{incode|Gui::ViewProviderExtensionPython}} |
|||
* {{incode|Gui::ViewProviderGeoFeatureGroupExtensionPython}} |
|||
* {{incode|Gui::ViewProviderGroupExtensionPython}} |
|||
* {{incode|Gui::ViewProviderOriginGroupExtensionPython}} |
|||
* {{incode|PartGui::ViewProviderAttachExtensionPython}} |
|||
* {{incode|PartGui::ViewProviderSplineExtensionPython}} |
|||
There exist other extensions but they do not work as-is: |
|||
* {{incode|App::ExtensionPython}} |
|||
* {{incode|TechDrawGui::ViewProviderCosmeticExtensionPython}} |
|||
* {{incode|TechDrawGui::ViewProviderDrawingViewExtensionPython}} |
|||
* {{incode|TechDrawGui::ViewProviderPageExtensionPython}} |
|||
* {{incode|TechDrawGui::ViewProviderTemplateExtensionPython}} |
|||
<span id="Further_information"></span> |
|||
== Weitere Informationen == |
|||
Weitere Seiten: |
|||
* [[Scripted_objects_saving_attributes/de|Skriptgesteuerte Objekte, die Attribute speichern]] |
|||
* [[Scripted_objects_migration/de|Migration von geskripteten Objekten]] |
|||
* [[Scripted objects with attachment/de|Geskriptete Objekte mit Anhang]] |
|||
* [[Viewprovider/de|Ansichtsanbieter]] |
|||
Interessante Forenbeiträge über geskriptete Objekte: |
|||
* [http://forum.freecadweb.org/viewtopic.php?f=22&t=13740 Python object attributes lost at load] |
|||
* [http://forum.freecadweb.org/viewtopic.php?t=12139 New FeaturePython is grey] |
|||
* [https://forum.freecadweb.org/viewtopic.php?f=18&t=44009 Explanation on dumps and loads], [https://docs.python.org/3/library/pickle.html#object.__getstate__ official documentation] |
|||
* [https://forum.freecadweb.org/viewtopic.php?f=18&t=13460&start=20#p109709 Eigenmode frequency always 0?] |
|||
* [https://forum.freecadweb.org/viewtopic.php?f=22&t=21330 how to implement python feature's setEdit properly?] |
|||
Zusätzlich zu den hier vorgestellten Beispielen solltedz du einen Blick in den FreeCAD Quellcode [https://github.com/FreeCAD/FreeCAD/blob/master/src/Mod/TemplatePyMod/FeaturePython.py src/Mod/TemplatePyMod/FeaturePython.py] für weitere Beispiele werfen. |
|||
{{Docnav/de |
|||
|[[Topological_data_scripting/de|Topologische Daten skripten]] |
|||
|[[Scenegraph/de|Szenengraph]] |
|||
}} |
|||
{{Powerdocnavi{{#translation:}}}} |
|||
[[Category:Developer Documentation{{#translation:}}]] |
|||
[[Category:Python Code{{#translation:}}]] |
Latest revision as of 14:37, 3 February 2024
Einführung
Neben den Standard-Objekttypen wie Beschriftungen, Netze und Bauteilobjekte bietet FreeCAD auch die erstaunliche Möglichkeit, 100% mit Python geschriebene parametrische Objekte zu erstellen, die als Python-Objekt (App FeaturePython-Objekt) bezeichnet werden. Diese Objekte verhalten sich genau wie jedes andere FreeCAD-Objekt und werden beim Speichern/Laden von Dateien automatisch gespeichert und wiederhergestellt.
Eine Besonderheit muss beachtet werden: Aus Sicherheitsgründen enthalten FreeCAD Dateien niemals eingebetteten Code. Der Python Code, den du zum Erstellen von parametrischen Objekten schreibst, wird niemals innerhalb einer Datei gespeichert. Das bedeutet, dass, wenn du eine Datei, die ein solches Objekt enthält, auf einem anderen Rechner öffnest, wenn dieser Python Code auf diesem Rechner nicht verfügbar ist, das Objekt nicht vollständig neu erstellt wird. Wenn du solche Objekte an andere weitergibst, musst du auch dein Python Skript weitergeben, z. B. als Makro.
Hinweis: Es ist möglich, Python Code in eine FreeCAD Datei zu packen, indem man json mit einem App::PropertyPythonObject serialisiert, aber dieser Code kann nie direkt ausgeführt werden und hat daher für unseren Zweck hier wenig Nutzen.
Python-Objekte folgen der gleichen Regel wie alle FreeCAD Funktionen: Sie sind in App- und einen GUI-Teile getrennt. Der App-Teil, das Dokument-Objekt, definiert die Geometrie unseres Objekts, während sein GUI-Teil, das View-Provider-Objekt, definiert, wie das Objekt auf dem Bildschirm daegestellt wird. Das Ansichtsprovider-Objekt ist, wie jede andere FreeCAD-Funktion, nur verfügbar, wenn FreeCAD in seiner eigenen GUI ausgeführt wird. Es stehen mehrere Eigenschaften und Methoden zur Verfügung, um ein Objekt zu erstellen. Die Eigenschaften müssen zu einem der vordefinierten Eigenschaftstypen gehören, die FreeCAD anbietet und werden im Eigenschaften Ansichtsfenster angezeigt, damit sie vom Benutzer bearbeitet werden können. Auf diese Weise sind FeaturePython-Objekte wirklich und vollständig parametrisch. Du kannst Eigenschaften für das Objekt und sein Ansichtsobjekt getrennt definieren.
Grundlegendes Beispiel
Das folgende Beispiel ist zusammen mit einigen anderen Beispielen in der Datei src/Mod/TemplatePyMod/FeaturePython.py zu finden:
'''Examples for a feature class and its view provider.'''
import FreeCAD, FreeCADGui
from pivy import coin
class Box:
def __init__(self, obj):
'''Add some custom properties to our box feature'''
obj.addProperty("App::PropertyLength", "Length", "Box", "Length of the box").Length = 1.0
obj.addProperty("App::PropertyLength", "Width", "Box", "Width of the box").Width = 1.0
obj.addProperty("App::PropertyLength", "Height", "Box", "Height of the box").Height = 1.0
obj.Proxy = self
def onChanged(self, fp, prop):
'''Do something when a property has changed'''
FreeCAD.Console.PrintMessage("Change property: " + str(prop) + "\n")
def execute(self, fp):
'''Do something when doing a recomputation, this method is mandatory'''
FreeCAD.Console.PrintMessage("Recompute Python Box feature\n")
class ViewProviderBox:
def __init__(self, obj):
'''Set this object to the proxy object of the actual view provider'''
obj.addProperty("App::PropertyColor","Color", "Box", "Color of the box").Color = (1.0, 0.0, 0.0)
obj.Proxy = self
def attach(self, obj):
'''Setup the scene sub-graph of the view provider, this method is mandatory'''
self.shaded = coin.SoGroup()
self.wireframe = coin.SoGroup()
self.scale = coin.SoScale()
self.color = coin.SoBaseColor()
data=coin.SoCube()
self.shaded.addChild(self.scale)
self.shaded.addChild(self.color)
self.shaded.addChild(data)
obj.addDisplayMode(self.shaded, "Shaded");
style=coin.SoDrawStyle()
style.style = coin.SoDrawStyle.LINES
self.wireframe.addChild(style)
self.wireframe.addChild(self.scale)
self.wireframe.addChild(self.color)
self.wireframe.addChild(data)
obj.addDisplayMode(self.wireframe, "Wireframe");
self.onChanged(obj,"Color")
def updateData(self, fp, prop):
'''If a property of the handled feature has changed we have the chance to handle this here'''
# fp is the handled feature, prop is the name of the property that has changed
l = fp.getPropertyByName("Length")
w = fp.getPropertyByName("Width")
h = fp.getPropertyByName("Height")
self.scale.scaleFactor.setValue(float(l), float(w), float(h))
pass
def getDisplayModes(self,obj):
'''Return a list of display modes.'''
modes=[]
modes.append("Shaded")
modes.append("Wireframe")
return modes
def getDefaultDisplayMode(self):
'''Return the name of the default display mode. It must be defined in getDisplayModes.'''
return "Shaded"
def setDisplayMode(self,mode):
'''Map the display mode defined in attach with those defined in getDisplayModes.\
Since they have the same names nothing needs to be done. This method is optional'''
return mode
def onChanged(self, vp, prop):
'''Here we can do something when a single property got changed'''
FreeCAD.Console.PrintMessage("Change property: " + str(prop) + "\n")
if prop == "Color":
c = vp.getPropertyByName("Color")
self.color.rgb.setValue(c[0], c[1], c[2])
def getIcon(self):
'''Return the icon in XPM format which will appear in the tree view. This method is\
optional and if not defined a default icon is shown.'''
return """
/* XPM */
static const char * ViewProviderBox_xpm[] = {
"16 16 6 1",
" c None",
". c #141010",
"+ c #615BD2",
"@ c #C39D55",
"# c #000000",
"$ c #57C355",
" ........",
" ......++..+..",
" .@@@@.++..++.",
" .@@@@.++..++.",
" .@@ .++++++.",
" ..@@ .++..++.",
"###@@@@ .++..++.",
"##$.@@$#.++++++.",
"#$#$.$$$........",
"#$$####### ",
"#$$#$$$$$# ",
"#$$#$$$$$# ",
"#$$#$$$$$# ",
" #$#$$$$$# ",
" ##$$$$$# ",
" ####### "};
"""
def dumps(self):
'''When saving the document this object gets stored using Python's json module.\
Since we have some un-serializable parts here -- the Coin stuff -- we must define this method\
to return a tuple of all serializable objects or None.'''
return None
def loads(self,state):
'''When restoring the serialized object from document we have the chance to set some internals here.\
Since no data were serialized nothing needs to be done here.'''
return None
def makeBox():
FreeCAD.newDocument()
a=FreeCAD.ActiveDocument.addObject("App::FeaturePython", "Box")
Box(a)
ViewProviderBox(a.ViewObject)
makeBox()
Things to note
If your object relies on being recomputed as soon as it is created, you must do this manually in the __init__
function as it is not called automatically. This example does not require it because the onChanged
method of the Box
class has the same effect as the execute
function, but the examples below rely on being recomputed before anything is displayed in the 3D view. In the examples, this is done manually with ActiveDocument.recompute()
but in more complex scenarios you need to decide where to recompute either the whole document or the FeaturePython object.
This example produces a number of exception stack traces in the report view window. This is because the onChanged
method of the Box
class is called each time a property is added in __init__
. When the first one is added, the Width and Height properties don't exist yet and so the attempt to access them fails.
Eine Erklärung zu __getstate__
und __setstate__
, die durch dumps
und loads
ersetzt wurden, befindet sich im Forumsbeitrag obj.Proxy.Type is a dict, not a string.
obj.addProperty(...)
returns obj
, so that the value of the property can be set on the same line:
obj.addProperty("App::PropertyLength", "Length", "Box", "Length of the box").Length = 1.0
Which is equivalent to:
obj.addProperty("App::PropertyLength", "Length", "Box", "Length of the box")
obj.Length = 1.0
Andere komplexere Beispiele
Dieses Beispiel verwendet das Part-Modul, um ein Oktaeder zu erstellen und dann seine Coin-Darstellung mit pivy.
Das Erste ist das Documentobjekt selbst:
import FreeCAD, FreeCADGui, Part
import pivy
from pivy import coin
class Octahedron:
def __init__(self, obj):
"Add some custom properties to our box feature"
obj.addProperty("App::PropertyLength","Length","Octahedron","Length of the octahedron").Length=1.0
obj.addProperty("App::PropertyLength","Width","Octahedron","Width of the octahedron").Width=1.0
obj.addProperty("App::PropertyLength","Height","Octahedron", "Height of the octahedron").Height=1.0
obj.addProperty("Part::PropertyPartShape","Shape","Octahedron", "Shape of the octahedron")
obj.Proxy = self
def execute(self, fp):
# Define six vetices for the shape
v1 = FreeCAD.Vector(0,0,0)
v2 = FreeCAD.Vector(fp.Length,0,0)
v3 = FreeCAD.Vector(0,fp.Width,0)
v4 = FreeCAD.Vector(fp.Length,fp.Width,0)
v5 = FreeCAD.Vector(fp.Length/2,fp.Width/2,fp.Height/2)
v6 = FreeCAD.Vector(fp.Length/2,fp.Width/2,-fp.Height/2)
# Make the wires/faces
f1 = self.make_face(v1,v2,v5)
f2 = self.make_face(v2,v4,v5)
f3 = self.make_face(v4,v3,v5)
f4 = self.make_face(v3,v1,v5)
f5 = self.make_face(v2,v1,v6)
f6 = self.make_face(v4,v2,v6)
f7 = self.make_face(v3,v4,v6)
f8 = self.make_face(v1,v3,v6)
shell=Part.makeShell([f1,f2,f3,f4,f5,f6,f7,f8])
solid=Part.makeSolid(shell)
fp.Shape = solid
# helper mehod to create the faces
def make_face(self,v1,v2,v3):
wire = Part.makePolygon([v1,v2,v3,v1])
face = Part.Face(wire)
return face
Dann haben wir das View Provider Objekt, das für die Darstellung des Objekts in der 3D Szene verantwortlich ist:
class ViewProviderOctahedron:
def __init__(self, obj):
"Set this object to the proxy object of the actual view provider"
obj.addProperty("App::PropertyColor","Color","Octahedron","Color of the octahedron").Color=(1.0,0.0,0.0)
obj.Proxy = self
def attach(self, obj):
"Setup the scene sub-graph of the view provider, this method is mandatory"
self.shaded = coin.SoGroup()
self.wireframe = coin.SoGroup()
self.scale = coin.SoScale()
self.color = coin.SoBaseColor()
self.data=coin.SoCoordinate3()
self.face=coin.SoIndexedFaceSet()
self.shaded.addChild(self.scale)
self.shaded.addChild(self.color)
self.shaded.addChild(self.data)
self.shaded.addChild(self.face)
obj.addDisplayMode(self.shaded,"Shaded");
style=coin.SoDrawStyle()
style.style = coin.SoDrawStyle.LINES
self.wireframe.addChild(style)
self.wireframe.addChild(self.scale)
self.wireframe.addChild(self.color)
self.wireframe.addChild(self.data)
self.wireframe.addChild(self.face)
obj.addDisplayMode(self.wireframe,"Wireframe");
self.onChanged(obj,"Color")
def updateData(self, fp, prop):
"If a property of the handled feature has changed we have the chance to handle this here"
# fp is the handled feature, prop is the name of the property that has changed
if prop == "Shape":
s = fp.getPropertyByName("Shape")
self.data.point.setNum(6)
cnt=0
for i in s.Vertexes:
self.data.point.set1Value(cnt,i.X,i.Y,i.Z)
cnt=cnt+1
self.face.coordIndex.set1Value(0,0)
self.face.coordIndex.set1Value(1,1)
self.face.coordIndex.set1Value(2,2)
self.face.coordIndex.set1Value(3,-1)
self.face.coordIndex.set1Value(4,1)
self.face.coordIndex.set1Value(5,3)
self.face.coordIndex.set1Value(6,2)
self.face.coordIndex.set1Value(7,-1)
self.face.coordIndex.set1Value(8,3)
self.face.coordIndex.set1Value(9,4)
self.face.coordIndex.set1Value(10,2)
self.face.coordIndex.set1Value(11,-1)
self.face.coordIndex.set1Value(12,4)
self.face.coordIndex.set1Value(13,0)
self.face.coordIndex.set1Value(14,2)
self.face.coordIndex.set1Value(15,-1)
self.face.coordIndex.set1Value(16,1)
self.face.coordIndex.set1Value(17,0)
self.face.coordIndex.set1Value(18,5)
self.face.coordIndex.set1Value(19,-1)
self.face.coordIndex.set1Value(20,3)
self.face.coordIndex.set1Value(21,1)
self.face.coordIndex.set1Value(22,5)
self.face.coordIndex.set1Value(23,-1)
self.face.coordIndex.set1Value(24,4)
self.face.coordIndex.set1Value(25,3)
self.face.coordIndex.set1Value(26,5)
self.face.coordIndex.set1Value(27,-1)
self.face.coordIndex.set1Value(28,0)
self.face.coordIndex.set1Value(29,4)
self.face.coordIndex.set1Value(30,5)
self.face.coordIndex.set1Value(31,-1)
def getDisplayModes(self,obj):
"Return a list of display modes."
modes=[]
modes.append("Shaded")
modes.append("Wireframe")
return modes
def getDefaultDisplayMode(self):
"Return the name of the default display mode. It must be defined in getDisplayModes."
return "Shaded"
def setDisplayMode(self,mode):
return mode
def onChanged(self, vp, prop):
"Here we can do something when a single property got changed"
FreeCAD.Console.PrintMessage("Change property: " + str(prop) + "\n")
if prop == "Color":
c = vp.getPropertyByName("Color")
self.color.rgb.setValue(c[0],c[1],c[2])
def getIcon(self):
return """
/* XPM */
static const char * ViewProviderBox_xpm[] = {
"16 16 6 1",
" c None",
". c #141010",
"+ c #615BD2",
"@ c #C39D55",
"# c #000000",
"$ c #57C355",
" ........",
" ......++..+..",
" .@@@@.++..++.",
" .@@@@.++..++.",
" .@@ .++++++.",
" ..@@ .++..++.",
"###@@@@ .++..++.",
"##$.@@$#.++++++.",
"#$#$.$$$........",
"#$$####### ",
"#$$#$$$$$# ",
"#$$#$$$$$# ",
"#$$#$$$$$# ",
" #$#$$$$$# ",
" ##$$$$$# ",
" ####### "};
"""
def dumps(self):
return None
def loads(self,state):
return None
Schließlich, sobald unser Objekt und sein Viewobjekt definiert sind, müssen wir sie nur noch aufrufen (Der Code der Octahedron Klasse und der Viewprovider Klasse könnte direkt in die FreeCAD Python Konsole kopiert werden):
FreeCAD.newDocument()
a=FreeCAD.ActiveDocument.addObject("App::FeaturePython","Octahedron")
Octahedron(a)
ViewProviderOctahedron(a.ViewObject)
Objekte auswählbar machen
Wenn du dein Objekt oder zumindest einen Teil davon durch Anklicken im Ansichtsfenster auswählbar machen möchtest, musst du seine Coin-Geometrie in einen SoFCSelection-Knoten aufnehmen. Wenn dein Objekt eine komplexe Darstellung hat, mit Widgets, Anmerkungen usw., möchtest du vielleicht nur einen Teil davon in eine SoFCSelection einschließen. Alles, was eine SoFCSelection ist, wird von FreeCAD ständig gescannt, um Auswahl/Vorwahl zu erkennen, daher ist es sinnvoll, es nicht mit unnötigem Scannen zu überlasten.
Once the parts of the scenegraph that are to be selectable are inside SoFCSelection nodes, you then need to provide two methods to handle the selection path. The selection path can take the form of a string giving the names of each element in the path, or of an array of scenegraph objects. The two methods you provide are getDetailPath
, which converts from a string path to an array of scenegraph objects, and getElementPicked
, which takes an element which has been clicked on in the scenegraph and returns its string name (note, not its string path).
Hier ist das obige Molekülbeispiel, angepasst, um die Elemente des Moleküls auswählbar zu machen:
class Molecule:
def __init__(self, obj):
''' Add two point properties '''
obj.addProperty("App::PropertyVector","p1","Line","Start point")
obj.addProperty("App::PropertyVector","p2","Line","End point").p2=FreeCAD.Vector(5,0,0)
obj.Proxy = self
def onChanged(self, fp, prop):
if prop == "p1" or prop == "p2":
''' Print the name of the property that has changed '''
fp.Shape = Part.makeLine(fp.p1,fp.p2)
def execute(self, fp):
''' Print a short message when doing a recomputation, this method is mandatory '''
fp.Shape = Part.makeLine(fp.p1,fp.p2)
class ViewProviderMolecule:
def __init__(self, obj):
''' Set this object to the proxy object of the actual view provider '''
obj.Proxy = self
self.ViewObject = obj
sep1=coin.SoSeparator()
sel1 = coin.SoType.fromName('SoFCSelection').createInstance()
# sel1.policy.setValue(coin.SoSelection.SHIFT)
sel1.ref()
sep1.addChild(sel1)
self.trl1=coin.SoTranslation()
sel1.addChild(self.trl1)
sel1.addChild(coin.SoSphere())
sep2=coin.SoSeparator()
sel2 = coin.SoType.fromName('SoFCSelection').createInstance()
sel2.ref()
sep2.addChild(sel2)
self.trl2=coin.SoTranslation()
sel2.addChild(self.trl2)
sel2.addChild(coin.SoSphere())
obj.RootNode.addChild(sep1)
obj.RootNode.addChild(sep2)
self.updateData(obj.Object, 'p2')
self.sel1 = sel1
self.sel2 = sel2
def getDetailPath(self, subname, path, append):
vobj = self.ViewObject
if append:
path.append(vobj.RootNode)
path.append(vobj.SwitchNode)
mode = vobj.SwitchNode.whichChild.getValue()
if mode >= 0:
mode = vobj.SwitchNode.getChild(mode)
path.append(mode)
sub = Part.splitSubname(subname)[-1]
if sub == 'Atom1':
path.append(self.sel1)
elif sub == 'Atom2':
path.append(self.sel2)
else:
path.append(mode.getChild(0))
return True
def getElementPicked(self, pp):
path = pp.getPath()
if path.findNode(self.sel1) >= 0:
return 'Atom1'
if path.findNode(self.sel2) >= 0:
return 'Atom2'
raise NotImplementedError
def updateData(self, fp, prop):
"If a property of the handled feature has changed we have the chance to handle this here"
# fp is the handled feature, prop is the name of the property that has changed
if prop == "p1":
p = fp.getPropertyByName("p1")
self.trl1.translation=(p.x,p.y,p.z)
elif prop == "p2":
p = fp.getPropertyByName("p2")
self.trl2.translation=(p.x,p.y,p.z)
def dumps(self):
return None
def loads(self,state):
return None
def makeMolecule():
FreeCAD.newDocument()
a=FreeCAD.ActiveDocument.addObject("Part::FeaturePython","Molecule")
Molecule(a)
ViewProviderMolecule(a.ViewObject)
FreeCAD.ActiveDocument.recompute()
Working with simple shapes
Wenn ein parametrisches Objekt einfach nur eine Form ausgibt, muss man keinen View-Provider-Objekt verwenden. Die Form wird mit FreeCADs Standard-Form-Darstellung angezeigt.
import FreeCAD as App
import FreeCADGui
import FreeCAD
import Part
class Line:
def __init__(self, obj):
'''"App two point properties" '''
obj.addProperty("App::PropertyVector","p1","Line","Start point")
obj.addProperty("App::PropertyVector","p2","Line","End point").p2=FreeCAD.Vector(1,0,0)
obj.Proxy = self
def execute(self, fp):
'''"Print a short message when doing a recomputation, this method is mandatory" '''
fp.Shape = Part.makeLine(fp.p1,fp.p2)
a=FreeCAD.ActiveDocument.addObject("Part::FeaturePython","Line")
Line(a)
a.ViewObject.Proxy=0 # just set it to something different from None (this assignment is needed to run an internal notification)
FreeCAD.ActiveDocument.recompute()
Gleicher Code unter Verwendung von ViewProviderLine
import FreeCAD as App
import FreeCADGui
import FreeCAD
import Part
class Line:
def __init__(self, obj):
'''"App two point properties" '''
obj.addProperty("App::PropertyVector","p1","Line","Start point")
obj.addProperty("App::PropertyVector","p2","Line","End point").p2=FreeCAD.Vector(100,0,0)
obj.Proxy = self
def execute(self, fp):
'''"Print a short message when doing a recomputation, this method is mandatory" '''
fp.Shape = Part.makeLine(fp.p1,fp.p2)
class ViewProviderLine:
def __init__(self, obj):
''' Set this object to the proxy object of the actual view provider '''
obj.Proxy = self
def getDefaultDisplayMode(self):
''' Return the name of the default display mode. It must be defined in getDisplayModes. '''
return "Flat Lines"
a=FreeCAD.ActiveDocument.addObject("Part::FeaturePython","Line")
Line(a)
ViewProviderLine(a.ViewObject)
App.ActiveDocument.recompute()
Scenegraph Structure
Du hast vielleicht bemerkt, dass die obigen Beispiele deine Szenengraphen auf leicht unterschiedliche Weise aufbauen. Einige verwenden obj.addDisplayMode(node, "modename")
, während andere obj.SwitchNode.getChild(x).addChild(y)
verwenden.
Each feature in a FreeCAD document is based the following scenegraph structure:
RootNode
\- SwitchNode
\- Shaded
- Wireframe
- etc
The SwitchNode
displays only one of its children, depending on which display mode is selection in FreeCAD.
The examples which use addDisplayMode
are constructing their scenegraphs solely out of coin3d scenegraph elements. Under the covers, addDisplayMode
adds a new child to the SwitchNode
; the name of that node will match the display mode it was passed.
The examples which use SwitchNode.getChild(x).addChild
also construct part of their geometry using functions from the Part workbench, such as fp.Shape = Part.makeLine(fp.p1,fp.p2)
. This constructs the different display mode scenegraphs under the SwitchNode
; when we later come to add coin3d elements to the scenegraph, we need to add them to the existing display mode scenegraphs using addChild
rather than creating a new child of the SwitchNode
.
When using addDisplayMode()
to add geometry to the scenegraph, each display mode should have its own node which is passed to addDisplayMode()
; don't reuse the same node for this. Doing so will confuse the selection mechanism. It's okay if each display mode's node has the same geometry nodes added below it, just the root of each display mode needs to be distinct.
Here is the above molecule example, adapted to be drawn only with Coin3D scenegraph objects instead of using objects from the Part workbench:
import Part
from pivy import coin
class Molecule:
def __init__(self, obj):
''' Add two point properties '''
obj.addProperty("App::PropertyVector","p1","Line","Start point")
obj.addProperty("App::PropertyVector","p2","Line","End point").p2=FreeCAD.Vector(5,0,0)
obj.Proxy = self
def onChanged(self, fp, prop):
pass
def execute(self, fp):
''' Print a short message when doing a recomputation, this method is mandatory '''
pass
class ViewProviderMolecule:
def __init__(self, obj):
''' Set this object to the proxy object of the actual view provider '''
self.constructed = False
obj.Proxy = self
self.ViewObject = obj
def attach(self, obj):
material = coin.SoMaterial()
material.diffuseColor = (1.0, 0.0, 0.0)
material.emissiveColor = (1.0, 0.0, 0.0)
drawStyle = coin.SoDrawStyle()
drawStyle.pointSize.setValue(10)
drawStyle.style = coin.SoDrawStyle.LINES
wireframe = coin.SoGroup()
shaded = coin.SoGroup()
self.wireframe = wireframe
self.shaded = shaded
self.coords = coin.SoCoordinate3()
self.coords.point.setValues(0, 2, [FreeCAD.Vector(0, 0, 0), FreeCAD.Vector(1, 0, 0)])
wireframe += self.coords
wireframe += drawStyle
wireframe += material
shaded += self.coords
shaded += drawStyle
shaded += material
g = coin.SoGroup()
sel1 = coin.SoType.fromName('SoFCSelection').createInstance()
sel1.style = 'EMISSIVE_DIFFUSE'
p1 = coin.SoType.fromName('SoIndexedPointSet').createInstance()
p1.coordIndex.set1Value(0, 0)
sel1 += p1
g += sel1
wireframe += g
shaded += g
g = coin.SoGroup()
sel2 = coin.SoType.fromName('SoFCSelection').createInstance()
sel2.style = 'EMISSIVE_DIFFUSE'
p2 = coin.SoType.fromName('SoIndexedPointSet').createInstance()
p2.coordIndex.set1Value(0, 1)
sel2 += p2
g += sel2
wireframe += g
shaded += g
g = coin.SoGroup()
sel3 = coin.SoType.fromName('SoFCSelection').createInstance()
sel3.style = 'EMISSIVE_DIFFUSE'
p3 = coin.SoType.fromName('SoIndexedLineSet').createInstance()
p3.coordIndex.setValues(0, 2, [0, 1])
sel3 += p3
g += sel3
wireframe += g
shaded += g
obj.addDisplayMode(wireframe, 'Wireframe')
obj.addDisplayMode(shaded, 'Shaded')
self.sel1 = sel1
self.sel2 = sel2
self.sel3 = sel3
self.constructed = True
self.updateData(obj.Object, 'p2')
def getDetailPath(self, subname, path, append):
vobj = self.ViewObject
if append:
path.append(vobj.RootNode)
path.append(vobj.SwitchNode)
mode = vobj.SwitchNode.whichChild.getValue()
FreeCAD.Console.PrintWarning("getDetailPath: mode {} is active\n".format(mode))
if mode >= 0:
mode = vobj.SwitchNode.getChild(mode)
path.append(mode)
sub = Part.splitSubname(subname)[-1]
print(sub)
if sub == 'Atom1':
path.append(self.sel1)
elif sub == 'Atom2':
path.append(self.sel2)
elif sub == 'Line':
path.append(self.sel3)
else:
path.append(mode.getChild(0))
return True
def getElementPicked(self, pp):
path = pp.getPath()
if path.findNode(self.sel1) >= 0:
return 'Atom1'
if path.findNode(self.sel2) >= 0:
return 'Atom2'
if path.findNode(self.sel3) >= 0:
return 'Line'
raise NotImplementedError
def updateData(self, fp, prop):
"If a property of the handled feature has changed we have the chance to handle this here"
# fp is the handled feature, prop is the name of the property that has changed
if not self.constructed:
return
if prop == "p1":
p = fp.getPropertyByName("p1")
self.coords.point.set1Value(0, p)
elif prop == "p2":
p = fp.getPropertyByName("p2")
self.coords.point.set1Value(1, p)
def getDisplayModes(self, obj):
return ['Wireframe', 'Shaded']
def getDefaultDisplayMode(self):
return 'Shaded'
def setDisplayMode(self, mode):
return mode
def dumps(self):
return None
def loads(self,state):
return None
def makeMolecule():
FreeCAD.newDocument()
a=FreeCAD.ActiveDocument.addObject("App::FeaturePython","Molecule")
Molecule(a)
b=ViewProviderMolecule(a.ViewObject)
a.touch()
FreeCAD.ActiveDocument.recompute()
return a,b
a,b = makeMolecule()
Part Design scripted objects
When making scripted objects in Part Design the process is similar to the scripted objects discussed above, but with a few additional considerations. We must handle 2 shape properties, one for the shape we see in the 3D view and another for the shape used by the pattern tools, such as polar pattern features. The object shapes also needs to be fused to any existing material already in the Body (or cut from it in the case of Subtractive features). And we must account for the placement and attachment of our objects a little bit differently.
Part Design scripted solid object features should be based on either PartDesign::FeaturePython, PartDesign::FeatureAdditivePython, or PartDesign::FeatureSubtractivePython rather than Part::FeaturePython. Only the Additive and Subtractive variants can be used in pattern features, and if based on Part::FeaturePython when the user drops the object into a Part Design Body it becomes a BaseFeature rather than being treated by the Body as a native Part Design object. Note: all of these are expected to be solids, so if you are making a non-solid feature it should be based on Part::FeaturePython or else the next feature in the tree will attempt to fuse to as a solid and it will fail.
Here is a simple example of making a Tube primitive, similar to the Tube primitive in Part Workbench except this one will be a Part Design solid feature object. For this we will 2 separate files: pdtube.FCMacro and pdtube.py. The .FCMacro file will be executed by the user to create the object. The .py file will hold the class definitions, imported by the .FCMacro. The reason for doing it this way is to maintain the parametric nature of the object after restarting FreeCAD and opening a document containing one of our Tubes.
First, the class definition file:
# -*- coding: utf-8 -*-
#classes should go in pdtube.py
import FreeCAD, FreeCADGui, Part
class PDTube:
def __init__(self,obj):
obj.addProperty("App::PropertyLength","Radius1","Tube","Radius1").Radius1 = 5
obj.addProperty("App::PropertyLength","Radius2","Tube","Radius2").Radius2 = 10
obj.addProperty("App::PropertyLength","Height","Tube","Height of tube").Height = 10
self.makeAttachable(obj)
obj.Proxy = self
def makeAttachable(self, obj):
if int(FreeCAD.Version()[1]) >= 19:
obj.addExtension('Part::AttachExtensionPython')
else:
obj.addExtension('Part::AttachExtensionPython', obj)
obj.setEditorMode('Placement', 0) #non-readonly non-hidden
def execute(self,fp):
outer_cylinder = Part.makeCylinder(fp.Radius2, fp.Height)
inner_cylinder = Part.makeCylinder(fp.Radius1, fp.Height)
if fp.Radius1 == fp.Radius2: #just make cylinder
tube_shape = outer_cylinder
elif fp.Radius1 < fp.Radius2:
tube_shape = outer_cylinder.cut(inner_cylinder)
else: #invert rather than error out
tube_shape = inner_cylinder.cut(outer_cylinder)
if not hasattr(fp, "positionBySupport"):
self.makeAttachable(fp)
fp.positionBySupport()
tube_shape.Placement = fp.Placement
#BaseFeature (shape property of type Part::PropertyPartShape) is provided for us
#with the PartDesign::FeaturePython and related classes, but it might be empty
#if our object is the first object in the tree. it's a good idea to check
#for its existence in case we want to make type Part::FeaturePython, which won't have it
if hasattr(fp, "BaseFeature") and fp.BaseFeature != None:
if "Subtractive" in fp.TypeId:
full_shape = fp.BaseFeature.Shape.cut(tube_shape)
else:
full_shape = fp.BaseFeature.Shape.fuse(tube_shape)
full_shape.transformShape(fp.Placement.inverse().toMatrix(), True) #borrowed from gears workbench
fp.Shape = full_shape
else:
fp.Shape = tube_shape
if hasattr(fp,"AddSubShape"): #PartDesign::FeatureAdditivePython and
#PartDesign::FeatureSubtractivePython have this
#property but PartDesign::FeaturePython does not
#It is the shape used for copying in pattern features
#for example in making a polar pattern
tube_shape.transformShape(fp.Placement.inverse().toMatrix(), True)
fp.AddSubShape = tube_shape
class PDTubeVP:
def __init__(self, obj):
'''Set this object to the proxy object of the actual view provider'''
obj.Proxy = self
def attach(self,vobj):
self.vobj = vobj
def updateData(self, fp, prop):
'''If a property of the handled feature has changed we have the chance to handle this here'''
pass
def getDisplayModes(self,obj):
'''Return a list of display modes.'''
modes=[]
modes.append("Flat Lines")
modes.append("Shaded")
modes.append("Wireframe")
return modes
def getDefaultDisplayMode(self):
'''Return the name of the default display mode. It must be defined in getDisplayModes.'''
return "Flat Lines"
def setDisplayMode(self,mode):
'''Map the display mode defined in attach with those defined in getDisplayModes.\
Since they have the same names nothing needs to be done. This method is optional'''
return mode
def onChanged(self, vp, prop):
'''Here we can do something when a single property got changed'''
#FreeCAD.Console.PrintMessage("Change property: " + str(prop) + "\n")
pass
def getIcon(self):
'''Return the icon in XPM format which will appear in the tree view. This method is\
optional and if not defined a default icon is shown.'''
return """
/* XPM */
static const char * ViewProviderBox_xpm[] = {
"16 16 6 1",
" c None",
". c #141010",
"+ c #615BD2",
"@ c #C39D55",
"# c #000000",
"$ c #57C355",
" ........",
" ......++..+..",
" .@@@@.++..++.",
" .@@@@.++..++.",
" .@@ .++++++.",
" ..@@ .++..++.",
"###@@@@ .++..++.",
"##$.@@$#.++++++.",
"#$#$.$$$........",
"#$$####### ",
"#$$#$$$$$# ",
"#$$#$$$$$# ",
"#$$#$$$$$# ",
" #$#$$$$$# ",
" ##$$$$$# ",
" ####### "};
"""
def dumps(self):
'''When saving the document this object gets stored using Python's json module.\
Since we have some un-serializable parts here -- the Coin stuff -- we must define this method\
to return a tuple of all serializable objects or None.'''
return None
def loads(self,state):
'''When restoring the serialized object from document we have the chance to set some internals here.\
Since no data were serialized nothing needs to be done here.'''
return None
And now the macro file to create the object:
# -*- coding: utf-8 -*-
#pdtube.FCMacro
import pdtube
#above line needed if the class definitions above are place in another file: PDTube.py
#this is needed if the tube object is to remain parametric after restarting FreeCAD and loading
#a document containing the object
body = FreeCADGui.ActiveDocument.ActiveView.getActiveObject("pdbody")
if not body:
FreeCAD.Console.PrintError("No active body.\n")
else:
from PySide import QtGui
window = FreeCADGui.getMainWindow()
items = ["Additive","Subtractive","Neither additive nor subtractive"]
item,ok =QtGui.QInputDialog.getItem(window,"Select tube type","Select whether you want additive, subtractive, or neither:",items,0,False)
if ok:
if item == items[0]:
className = "PartDesign::FeatureAdditivePython"
elif item == items[1]:
className = "PartDesign::FeatureSubtractivePython"
else:
className = "PartDesign::FeaturePython" #not usable in pattern features, such as polar pattern
tube = FreeCAD.ActiveDocument.addObject(className,"Tube")
pdtube.PDTube(tube)
pdtube.PDTubeVP(tube.ViewObject)
body.addObject(tube) #optionally we can also use body.insertObject() for placing at particular place in tree
Available object types
The list of all object types you can create with FreeCAD.ActiveDocument.addObject()
can be obtained with FreeCAD.ActiveDocument.supportedTypes()
. Only object types with a name ending in Python
can be used for scripted objects. These are listed here (for FreeCAD v0.21):
App::DocumentObjectGroupPython
App::FeaturePython
App::GeometryPython
App::LinkElementPython
App::LinkGroupPython
App::LinkPython
App::MaterialObjectPython
App::PlacementPython
Part::CustomFeaturePython
Part::FeaturePython
Part::Part2DObjectPython
Path::FeatureAreaPython
Path::FeatureAreaViewPython
Path::FeatureCompoundPython
Path::FeaturePython
Path::FeatureShapePython
Sketcher::SketchObjectPython
TechDraw::DrawComplexSectionPython
TechDraw::DrawLeaderLinePython
TechDraw::DrawPagePython
TechDraw::DrawRichAnnoPython
TechDraw::DrawTemplatePython
TechDraw::DrawTilePython
TechDraw::DrawTileWeldPython
TechDraw::DrawViewPartPython
TechDraw::DrawViewPython
TechDraw::DrawViewSectionPython
TechDraw::DrawViewSymbolPython
TechDraw::DrawWeldSymbolPython
Vorhandene Methoden
Siehe FeaturePython Methoden für eine vollständige Referenz.
Vorhandene Eigenschaften
Eigenschaften sind die wahren Bausteine von FeaturePython-Objekten. Durch sie kann man mit einem Objekt interagieren und es ändern. Nach dem Erstellen eines neuen FeaturePython-Objekts in einem Dokument, kann man eine Liste der vorhandenen Eigenschaften bekommen:
obj = FreeCAD.ActiveDocument.addObject("App::FeaturePython", "Box")
obj.supportedProperties()
Siehe FunktionsPython benutzerdefinierte Eigenschaften für einen Überblick:
Beim Hinzufügen von Eigenschaften zu benutzerdefinierten Objekten bitte folgendes beachten:
- Nicht die Zeichen
<
oder>
in den Beschreibungen der Eigenschaften verwenden (das würde die XML-Teile in der .FCStd-Datei auseinanderbrechen) - Eigenschaften werden alphabetisch in einer .FCStd-Datei gespeichert. Befindet sich eine Form (shape) in den Eigenschaften, wird jede Eigenschaft, deren Name in alphabetischen Reihenfolge nach "Shape" kommt, auch NACH der Form geladen, was zu seltsamen Verhaltensweisen führen kann.
Die Eigenschaften sind in der C++ header-Datei PropertyStandard.h festgelegt.
Arten von Eigenschaften
Standardmäßig können Eigenschaften aktualisiert werden, aber es ist möglich, die Eigenschaften auf schreibgeschützt zu setzen, wenn man zum Beispiel das Ergebnis einer Methode anzeigen möchte. Es ist auch möglich, eine Eigenschaft auszublenden. Die Art der Eigenschaft kann festgelegt werden mit:
obj.setEditorMode("MyPropertyName", mode)
wobei mode ein kurzer int ist, der gesetzt werden kann auf:
0 -- Standardmodus, Lesen und Schreiben 1 -- Nur-Lesen 2 -- Versteckt
The mode can also be set using a list of strings, e.g. obj.setEditorMode("Placement", ["ReadOnly", "Hidden"])
.
The EditorModes are not set at FreeCAD file reload. This could to be done by the loads
function. See http://forum.freecadweb.org/viewtopic.php?f=18&t=13460&start=10#p108072. By using the setEditorMode
the properties are only read-only in the Property editor. They can still be changed from Python. To really make them read-only the setting has to be passed directly inside the addProperty
function. See http://forum.freecadweb.org/viewtopic.php?f=18&t=13460&start=20#p109709 for an example.
Using the direct setting in the addProperty
function, you also have more possibilities. In particular, an interesting one is mark a property as an output property. This way FreeCAD won't mark the feature as touched when changing it (so no need to recompute).
Example of output property (see also https://forum.freecadweb.org/viewtopic.php?t=24928):
obj.addProperty("App::PropertyString", "MyCustomProperty", "", "", 8)
The property types that can be set at last parameter of the addProperty function are:
0 -- Prop_None, No special property type 1 -- Prop_ReadOnly, Property is read-only in the editor 2 -- Prop_Transient, Property won't be saved to file 4 -- Prop_Hidden, Property won't appear in the editor 8 -- Prop_Output, Modified property doesn't touch its parent container 16 -- Prop_NoRecompute, Modified property doesn't touch its container for recompute 32 -- Prop_NoPersist, Property won't be saved to file at all
The property types are defined in the PropertyContainer C++ header file.
Available extensions
The list of available extensions can be obtained with grep -RI EXTENSION_PROPERTY_SOURCE_TEMPLATE
in the repository of the source code and is given here (for FreeCAD v0.21).
For objects:
App::GeoFeatureGroupExtensionPython
App::GroupExtensionPython
App::LinkBaseExtensionPython
App::LinkExtensionPython
App::OriginGroupExtensionPython
Part::AttachExtensionPython
TechDraw::CosmeticExtensionPython
For view objects:
Gui::ViewProviderExtensionPython
Gui::ViewProviderGeoFeatureGroupExtensionPython
Gui::ViewProviderGroupExtensionPython
Gui::ViewProviderOriginGroupExtensionPython
PartGui::ViewProviderAttachExtensionPython
PartGui::ViewProviderSplineExtensionPython
There exist other extensions but they do not work as-is:
App::ExtensionPython
TechDrawGui::ViewProviderCosmeticExtensionPython
TechDrawGui::ViewProviderDrawingViewExtensionPython
TechDrawGui::ViewProviderPageExtensionPython
TechDrawGui::ViewProviderTemplateExtensionPython
Weitere Informationen
Weitere Seiten:
- Skriptgesteuerte Objekte, die Attribute speichern
- Migration von geskripteten Objekten
- Geskriptete Objekte mit Anhang
- Ansichtsanbieter
Interessante Forenbeiträge über geskriptete Objekte:
- Python object attributes lost at load
- New FeaturePython is grey
- Explanation on dumps and loads, official documentation
- Eigenmode frequency always 0?
- how to implement python feature's setEdit properly?
Zusätzlich zu den hier vorgestellten Beispielen solltedz du einen Blick in den FreeCAD Quellcode src/Mod/TemplatePyMod/FeaturePython.py für weitere Beispiele werfen.
- FreeCAD scripting: Python, Introduction to Python, Python scripting tutorial, FreeCAD Scripting Basics
- Modules: Builtin modules, Units, Quantity
- Workbenches: Workbench creation, Gui Commands, Commands, Installing more workbenches
- Meshes and Parts: Mesh Scripting, Topological data scripting, Mesh to Part, PythonOCC
- Parametric objects: Scripted objects, Viewproviders (Custom icon in tree view)
- Scenegraph: Coin (Inventor) scenegraph, Pivy
- Graphical interface: Interface creation, Interface creation completely in Python (1, 2, 3, 4, 5), PySide, PySide examples beginner, intermediate, advanced
- Macros: Macros, How to install macros
- Embedding: Embedding FreeCAD, Embedding FreeCADGui
- Other: Expressions, Code snippets, Line drawing function, FreeCAD vector math library (deprecated)
- Hubs: User hub, Power users hub, Developer hub