Scripted objects/cs: Difference between revisions
No edit summary |
(Updating to match new version of source page) |
||
(33 intermediate revisions by 3 users not shown) | |||
Line 1: | Line 1: | ||
<languages/> |
|||
{{TOCright}} |
|||
==Introduction== |
|||
<div class="mw-translate-fuzzy"> |
|||
Kromě standardních objektových typů jako jsou anotace, sítě a díly, nabízí FreeCAD skvělou možnost vytváření objektů 100% vytvořených skritpy Pythonu, které se nazývají Pythonovské objekty. Tyto objekty se chovají stejně jako jiné objekty FreeCADu a jsou ukládány a načítány automaticky při ukládání a otevírání souboru. |
Kromě standardních objektových typů jako jsou anotace, sítě a díly, nabízí FreeCAD skvělou možnost vytváření objektů 100% vytvořených skritpy Pythonu, které se nazývají Pythonovské objekty. Tyto objekty se chovají stejně jako jiné objekty FreeCADu a jsou ukládány a načítány automaticky při ukládání a otevírání souboru. |
||
</div> |
|||
<div class="mw-translate-fuzzy"> |
|||
Je třeba pochopit jednu zvláštnost, tyto objekty jsou ukládány ve FcStd souborech FreeCADu s pythonovským modulem [http://docs.python.org/2/library/json.html json]. Tento modul převede pythonovský objekt do řetězce, který je pak možno uložit v souboru. Při načítání naopak tento modul použije uložený řetězec ke znovuvytvoření původního objektu, při tom musí mít přístup ke zdrojovému kódu, který vytvoří objekt. To znamená, že když uložíte takový uživatelský objekt a pak jej otevíráte na počítači kde není pythonovský kód, tak nebude tento objekt vytvořen. Když tedy distribuujete takový objekt někomu jinému, musíte společně s ním distribuovat i pythonovský skript, který objekt vytváří. |
Je třeba pochopit jednu zvláštnost, tyto objekty jsou ukládány ve FcStd souborech FreeCADu s pythonovským modulem [http://docs.python.org/2/library/json.html json]. Tento modul převede pythonovský objekt do řetězce, který je pak možno uložit v souboru. Při načítání naopak tento modul použije uložený řetězec ke znovuvytvoření původního objektu, při tom musí mít přístup ke zdrojovému kódu, který vytvoří objekt. To znamená, že když uložíte takový uživatelský objekt a pak jej otevíráte na počítači kde není pythonovský kód, tak nebude tento objekt vytvořen. Když tedy distribuujete takový objekt někomu jinému, musíte společně s ním distribuovat i pythonovský skript, který objekt vytváří. |
||
</div> |
|||
'''Note''': It is possible to pack python code inside a FreeCAD file using json serializing with an App::PropertyPythonObject, but that code can never directly be run, and therefore has little use for our purpose here. |
|||
Pythonovský objekt má stejné pravidlo jako FreeCAD: Aplikace a GUI jsou odděleny do samostatných částí. Aplikační část, Document Object, definuje konstrukci objektu, zatímco část GUI, View Provider Object, definuje jak bude objekt zobrazen na displeji. View Provider Object, stejně jako další GUI objekty FreeCADu je dostupný pouze když FreeCAD běží se svým vlastním GUI. Pro vytvoření objektu je použitelných několik vlastností a metod. Vlastnosti musejí být některé z předdefinovaných typových vlastností, které nabízí FreeCAD a zobrazují se v dialogovém okně vlastností, takže mohou být uživatelem upravovány. Tímto způsobem jsou Pythonovské objekty správně a zcela parametrizovány. Můžete samostatně definovat vlastnosti objektu a jeho zobrazovacího objektu. |
|||
Pythonovský objekt má stejné pravidlo jako FreeCAD: Aplikace a GUI jsou odděleny do samostatných částí. Aplikační část, Document Object, definuje konstrukci objektu, zatímco část GUI, View Provider Object, definuje jak bude objekt zobrazen na displeji. View Provider Object, stejně jako další GUI objekty FreeCADu je dostupný pouze když FreeCAD běží se svým vlastním GUI. Pro vytvoření objektu je použitelných několik vlastností a metod. Vlastnosti musejí být některé z předdefinovaných typových vlastností, které nabízí FreeCAD a zobrazují se v dialogovém okně vlastností, takže mohou být uživatelem upravovány. Tímto způsobem jsou Pythonovské objekty správně a zcela parametrizovány. Můžete samostatně definovat vlastnosti objektu a jeho zobrazovacího objektu. |
|||
'''Rada:''' Ve starších verzích jsme používali pythonovský modul [http://docs.python.org/release/2.5/lib/module-cPickle.html cPickle]. Nicméně tento modul spouští libovolný kód a to může být bezpečnostní problém. Proto jsme přešli k pythonovskému modulu json. |
|||
<div class="mw-translate-fuzzy"> |
|||
== Základní příklad == |
== Základní příklad == |
||
Následující příklad najdete v souboru [http://free-cad.svn.sourceforge.net/viewvc/free-cad/trunk/src/Mod/TemplatePyMod/FeaturePython.py?view=markup src/Mod/TemplatePyMod/FeaturePython.py], společně s několika dalšími příklady: |
Následující příklad najdete v souboru [http://free-cad.svn.sourceforge.net/viewvc/free-cad/trunk/src/Mod/TemplatePyMod/FeaturePython.py?view=markup src/Mod/TemplatePyMod/FeaturePython.py], společně s několika dalšími příklady: |
||
</div> |
|||
<syntaxhighlight> |
|||
{{Code|code= |
|||
"Examples for a feature class and its view provider." |
|||
'''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): |
|||
import FreeCAD, FreeCADGui |
|||
'''Do something when doing a recomputation, this method is mandatory''' |
|||
from pivy import coin |
|||
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): |
|||
class Box: |
|||
'''Setup the scene sub-graph of the view provider, this method is mandatory''' |
|||
def __init__(self, obj): |
|||
self.shaded = coin.SoGroup() |
|||
"'''Add some custom properties to our box feature'''" |
|||
self.wireframe = coin.SoGroup() |
|||
obj.addProperty("App::PropertyLength","Length","Box","Length of the box").Length=1.0 |
|||
self.scale = coin.SoScale() |
|||
obj.addProperty("App::PropertyLength","Width","Box","Width of the box").Width=1.0 |
|||
self.color = coin.SoBaseColor() |
|||
obj.addProperty("App::PropertyLength","Height","Box", "Height of the box").Height=1.0 |
|||
obj.Proxy = self |
|||
data=coin.SoCube() |
|||
self.shaded.addChild(self.scale) |
|||
def onChanged(self, fp, prop): |
|||
self.shaded.addChild(self.color) |
|||
"'''Do something when a property has changed'''" |
|||
self.shaded.addChild(data) |
|||
FreeCAD.Console.PrintMessage("Change property: " + str(prop) + "\n") |
|||
obj.addDisplayMode(self.shaded,"Shaded"); |
|||
style=coin.SoDrawStyle() |
|||
def execute(self, fp): |
|||
style.style = coin.SoDrawStyle.LINES |
|||
"'''Do something when doing a recomputation, this method is mandatory'''" |
|||
self.wireframe.addChild(style) |
|||
FreeCAD.Console.PrintMessage("Recompute Python Box feature\n") |
|||
self.wireframe.addChild(self.scale) |
|||
self.wireframe.addChild(self.color) |
|||
class ViewProviderBox: |
|||
self.wireframe.addChild(data) |
|||
def __init__(self, obj): |
|||
obj.addDisplayMode(self.wireframe,"Wireframe"); |
|||
"'''Set this object to the proxy object of the actual view provider'''" |
|||
self.onChanged(obj,"Color") |
|||
obj.addProperty("App::PropertyColor","Color","Box","Color of the box").Color=(1.0,0.0,0.0) |
|||
obj.Proxy = self |
|||
def updateData(self, fp, prop): |
|||
'''If a property of the handled feature has changed we have the chance to handle this here''' |
|||
def attach(self, obj): |
|||
# fp is the handled feature, prop is the name of the property that has changed |
|||
"'''Setup the scene sub-graph of the view provider, this method is mandatory'''" |
|||
l = fp.getPropertyByName("Length") |
|||
self.shaded = coin.SoGroup() |
|||
w = fp.getPropertyByName("Width") |
|||
self.wireframe = coin.SoGroup() |
|||
h = fp.getPropertyByName("Height") |
|||
self.scale = coin.SoScale() |
|||
self.scale.scaleFactor.setValue(float(l),float(w),float(h)) |
|||
self.color = coin.SoBaseColor() |
|||
pass |
|||
data=coin.SoCube() |
|||
def getDisplayModes(self,obj): |
|||
self.shaded.addChild(self.scale) |
|||
'''Return a list of display modes.''' |
|||
self.shaded.addChild(self.color) |
|||
modes=[] |
|||
self.shaded.addChild(data) |
|||
modes.append("Shaded") |
|||
modes.append("Wireframe") |
|||
style=coin.SoDrawStyle() |
|||
return modes |
|||
style.style = coin.SoDrawStyle.LINES |
|||
self.wireframe.addChild(style) |
|||
def getDefaultDisplayMode(self): |
|||
self.wireframe.addChild(self.scale) |
|||
'''Return the name of the default display mode. It must be defined in getDisplayModes.''' |
|||
self.wireframe.addChild(self.color) |
|||
return "Shaded" |
|||
self.wireframe.addChild(data) |
|||
obj.addDisplayMode(self.wireframe,"Wireframe"); |
|||
def setDisplayMode(self,mode): |
|||
self.onChanged(obj,"Color") |
|||
'''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''' |
|||
def updateData(self, fp, prop): |
|||
return mode |
|||
"'''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 |
|||
def onChanged(self, vp, prop): |
|||
l = fp.getPropertyByName("Length") |
|||
'''Here we can do something when a single property got changed''' |
|||
w = fp.getPropertyByName("Width") |
|||
FreeCAD.Console.PrintMessage("Change property: " + str(prop) + "\n") |
|||
h = fp.getPropertyByName("Height") |
|||
if prop == "Color": |
|||
self.scale.scaleFactor.setValue(l,w,h) |
|||
c = vp.getPropertyByName("Color") |
|||
pass |
|||
self.color.rgb.setValue(c[0],c[1],c[2]) |
|||
def getDisplayModes(self,obj): |
|||
def getIcon(self): |
|||
"'''Return a list of display modes.'''" |
|||
'''Return the icon in XPM format which will appear in the tree view. This method is\ |
|||
modes=[] |
|||
optional and if not defined a default icon is shown.''' |
|||
modes.append("Shaded") |
|||
return """ |
|||
modes.append("Wireframe") |
|||
/* XPM */ |
|||
return modes |
|||
static const char * ViewProviderBox_xpm[] = { |
|||
"16 16 6 1", |
|||
def getDefaultDisplayMode(self): |
|||
" c None", |
|||
"'''Return the name of the default display mode. It must be defined in getDisplayModes.'''" |
|||
". c #141010", |
|||
return "Shaded" |
|||
"+ c #615BD2", |
|||
"@ c #C39D55", |
|||
def setDisplayMode(self,mode): |
|||
"# c #000000", |
|||
"'''Map the display mode defined in attach with those defined in getDisplayModes.\''' |
|||
"$ c #57C355", |
|||
'''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\''' |
|||
"#$$#$$$$$# ", |
|||
"#$$#$$$$$# ", |
|||
return """ |
|||
" #$#$$$$$# ", |
|||
/* XPM */ |
|||
" ##$$$$$# ", |
|||
static const char * ViewProviderBox_xpm[] = { |
|||
" ####### "}; |
|||
"16 16 6 1", |
|||
""" |
|||
" c None", |
|||
". c #141010", |
|||
def __getstate__(self): |
|||
"+ c #615BD2", |
|||
'''When saving the document this object gets stored using Python's json module.\ |
|||
"@ c #C39D55", |
|||
Since we have some un-serializable parts here -- the Coin stuff -- we must define this method\ |
|||
"# c #000000", |
|||
to return a tuple of all serializable objects or None.''' |
|||
"$ c #57C355", |
|||
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) |
|||
"#$$#$$$$$# ", |
|||
" #$#$$$$$# ", |
|||
makeBox() |
|||
" ##$$$$$# ", |
|||
" ####### "}; |
|||
}} |
|||
""" |
|||
=== Things to note === |
|||
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 __getstate__(self): |
|||
"'''When saving the document this object gets stored using Python's json module.\''' |
|||
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. |
|||
'''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.'''" |
|||
An explanation of {{incode|__getstate__}} and {{incode|__setstate__}} is in the forum thread [https://forum.freecadweb.org/viewtopic.php?f=18&t=44009&start=10#p377892 obj.Proxy.Type is a dict, not a string]. |
|||
return None |
|||
== Available methods == |
|||
def __setstate__(self,state): |
|||
See [[FeaturePython_methods|FeaturePython methods]] for the complete reference. |
|||
"'''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> |
|||
== Dostupné vlastnosti == |
== Dostupné vlastnosti == |
||
Vlastnosti jsou skutečné základní kameny pythonovských objektů. Jejich prostřednictvím je uživatel schopen pracovat s objektem. Po vytvoření Pythonovského objektu v dokumentu ( obj=FreeCAD.ActiveDocument.addObject("App::FeaturePython","Box") ), obdržíte seznam dostupných vlastností zadáním: |
Vlastnosti jsou skutečné základní kameny pythonovských objektů. Jejich prostřednictvím je uživatel schopen pracovat s objektem. Po vytvoření Pythonovského objektu v dokumentu ( obj=FreeCAD.ActiveDocument.addObject("App::FeaturePython","Box") ), obdržíte seznam dostupných vlastností zadáním: |
||
<syntaxhighlight> |
|||
{{Code|code= |
|||
obj.supportedProperties() |
|||
obj.supportedProperties() |
|||
</syntaxhighlight> |
|||
}} |
|||
<div class="mw-translate-fuzzy"> |
|||
Dostanete seznam dostupných vlastností: |
Dostanete seznam dostupných vlastností: |
||
</div> |
|||
<syntaxhighlight> |
|||
* [[FeaturePython Custom Properties#App::PropertyAcceleration|App::PropertyAcceleration]] |
|||
App::PropertyBool |
|||
* [[FeaturePython Custom Properties#App::PropertyAngle|App::PropertyAngle]] |
|||
App::PropertyFloat |
|||
* [[FeaturePython Custom Properties#App::PropertyArea|App::PropertyArea]] |
|||
App::PropertyFloatList |
|||
* [[FeaturePython Custom Properties#App::PropertyBool|App::PropertyBool]] |
|||
App::PropertyFloatConstraint |
|||
* [[FeaturePython Custom Properties#App::PropertyBoolList|App::PropertyBoolList]] |
|||
App::PropertyAngle |
|||
* [[FeaturePython Custom Properties#App::PropertyColor|App::PropertyColor]] |
|||
App::PropertyDistance |
|||
* [[FeaturePython Custom Properties#App::PropertyColorList|App::PropertyColorList]] |
|||
App::PropertyInteger |
|||
* [[FeaturePython Custom Properties#App::PropertyDirection|App::PropertyDirection]] |
|||
App::PropertyIntegerConstraint |
|||
* [[FeaturePython Custom Properties#App::PropertyDistance|App::PropertyDistance]] |
|||
App::PropertyPercent |
|||
* [[FeaturePython Custom Properties#App::PropertyEnumeration|App::PropertyEnumeration]] |
|||
* [[FeaturePython Custom Properties#App::PropertyExpressionEngine|App::PropertyExpressionEngine]] |
|||
App::PropertyIntegerList |
|||
* [[FeaturePython Custom Properties#App::PropertyFile|App::PropertyFile]] |
|||
App::PropertyString |
|||
* [[FeaturePython Custom Properties#App::PropertyFileIncluded|App::PropertyFileIncluded]] |
|||
App::PropertyStringList |
|||
* [[FeaturePython Custom Properties#App::PropertyFloat|App::PropertyFloat]] |
|||
App::PropertyLink |
|||
* [[FeaturePython Custom Properties#App::PropertyFloatConstraint|App::PropertyFloatConstraint]] |
|||
App::PropertyLinkList |
|||
* [[FeaturePython Custom Properties#App::PropertyFloatList|App::PropertyFloatList]] |
|||
App::PropertyMatrix |
|||
* [[FeaturePython Custom Properties#App::PropertyFont|App::PropertyFont]] |
|||
App::PropertyVector |
|||
* [[FeaturePython Custom Properties#App::PropertyForce|App::PropertyForce]] |
|||
App::PropertyVectorList |
|||
* [[FeaturePython Custom Properties#App::PropertyFrequency|App::PropertyFrequency]] |
|||
App::PropertyPlacement |
|||
* [[FeaturePython Custom Properties#App::PropertyInteger|App::PropertyInteger]] |
|||
App::PropertyPlacementLink |
|||
* [[FeaturePython Custom Properties#App::PropertyIntegerConstraint|App::PropertyIntegerConstraint]] |
|||
App::PropertyColor |
|||
* [[FeaturePython Custom Properties#App::PropertyIntegerList|App::PropertyIntegerList]] |
|||
App::PropertyColorList |
|||
* [[FeaturePython Custom Properties#App::PropertyIntegerSet|App::PropertyIntegerSet]] |
|||
App::PropertyMaterial |
|||
* [[FeaturePython Custom Properties#App::PropertyLength|App::PropertyLength]] |
|||
App::PropertyPath |
|||
* [[FeaturePython Custom Properties#App::PropertyLink|App::PropertyLink]] |
|||
App::PropertyFile |
|||
* [[FeaturePython Custom Properties#App::PropertyLinkChild|App::PropertyLinkChild]] |
|||
App::PropertyFileIncluded |
|||
* [[FeaturePython Custom Properties#App::PropertyLinkGlobal|App::PropertyLinkGlobal]] |
|||
Part::PropertyPartShape |
|||
* [[FeaturePython Custom Properties#App::PropertyLinkHidden|App::PropertyLinkHidden]] |
|||
Part::PropertyFilletContour |
|||
* [[FeaturePython Custom Properties#App::PropertyLinkList|App::PropertyLinkList]] |
|||
Part::PropertyCircle |
|||
* [[FeaturePython Custom Properties#App::PropertyLinkListChild|App::PropertyLinkListChild]] |
|||
</syntaxhighlight> |
|||
* [[FeaturePython Custom Properties#App::PropertyLinkListGlobal|App::PropertyLinkListGlobal]] |
|||
* [[FeaturePython Custom Properties#App::PropertyLinkListHidden|App::PropertyLinkListHidden]] |
|||
* [[FeaturePython Custom Properties#App::PropertyLinkSub|App::PropertyLinkSub]] |
|||
* [[FeaturePython Custom Properties#App::PropertyLinkSubChild|App::PropertyLinkSubChild]] |
|||
* [[FeaturePython Custom Properties#App::PropertyLinkSubGlobal|App::PropertyLinkSubGlobal]] |
|||
* [[FeaturePython Custom Properties#App::PropertyLinkSubHidden|App::PropertyLinkSubHidden]] |
|||
* [[FeaturePython Custom Properties#App::PropertyLinkSubList|App::PropertyLinkSubList]] |
|||
* [[FeaturePython Custom Properties#App::PropertyLinkSubListChild|App::PropertyLinkSubListChild]] |
|||
* [[FeaturePython Custom Properties#App::PropertyLinkSubListGlobal|App::PropertyLinkSubListGlobal]] |
|||
* [[FeaturePython Custom Properties#App::PropertyLinkSubListHidden|App::PropertyLinkSubListHidden]] |
|||
* [[FeaturePython Custom Properties#App::PropertyMap|App::PropertyMap]] |
|||
* [[FeaturePython Custom Properties#App::PropertyMaterial|App::PropertyMaterial]] |
|||
* [[FeaturePython Custom Properties#App::PropertyMaterialList|App::PropertyMaterialList]] |
|||
* [[FeaturePython Custom Properties#App::PropertyMatrix|App::PropertyMatrix]] |
|||
* [[FeaturePython Custom Properties#App::PropertyPath|App::PropertyPath]] |
|||
* [[FeaturePython Custom Properties#App::PropertyPercent|App::PropertyPercent]] |
|||
* [[FeaturePython Custom Properties#App::PropertyPersistentObject|App::PropertyPersistentObject]] |
|||
* [[FeaturePython Custom Properties#App::PropertyPlacement|App::PropertyPlacement]] |
|||
* [[FeaturePython Custom Properties#App::PropertyPlacementLink|App::PropertyPlacementLink]] |
|||
* [[FeaturePython Custom Properties#App::PropertyPlacementList|App::PropertyPlacementList]] |
|||
* [[FeaturePython Custom Properties#App::PropertyPosition|App::PropertyPosition]] |
|||
* [[FeaturePython Custom Properties#App::PropertyPrecision|App::PropertyPrecision]] |
|||
* [[FeaturePython Custom Properties#App::PropertyPressure|App::PropertyPressure]] |
|||
* [[FeaturePython Custom Properties#App::PropertyPythonObject|App::PropertyPythonObject]] |
|||
* [[FeaturePython Custom Properties#App::PropertyQuantity|App::PropertyQuantity]] |
|||
* [[FeaturePython Custom Properties#App::PropertyQuantityConstraint|App::PropertyQuantityConstraint]] |
|||
* [[FeaturePython Custom Properties#App::PropertySpeed|App::PropertySpeed]] |
|||
* [[FeaturePython Custom Properties#App::PropertyString|App::PropertyString]] |
|||
* [[FeaturePython Custom Properties#App::PropertyStringList|App::PropertyStringList]] |
|||
* [[FeaturePython Custom Properties#App::PropertyUUID|App::PropertyUUID]] |
|||
* [[FeaturePython Custom Properties#App::PropertyVacuumPermittivity|App::PropertyVacuumPermittivity]] |
|||
* [[FeaturePython Custom Properties#App::PropertyVector|App::PropertyVector]] |
|||
* [[FeaturePython Custom Properties#App::PropertyVectorDistance|App::PropertyVectorDistance]] |
|||
* [[FeaturePython Custom Properties#App::PropertyVectorList|App::PropertyVectorList]] |
|||
* [[FeaturePython Custom Properties#App::PropertyVolume|App::PropertyVolume]] |
|||
* [[FeaturePython Custom Properties#App::PropertyXLink|App::PropertyXLink]] |
|||
* [[FeaturePython Custom Properties#App::PropertyXLinkList|App::PropertyXLinkList]] |
|||
* [[FeaturePython Custom Properties#App::PropertyXLinkSub|App::PropertyXLinkSub]] |
|||
* [[FeaturePython Custom Properties#App::PropertyXLinkSubList|App::PropertyXLinkSubList]] |
|||
* [[FeaturePython Custom Properties#Mesh::PropertyCurvatureList|Mesh::PropertyCurvatureList]] |
|||
* [[FeaturePython Custom Properties#Mesh::PropertyMeshKernel|Mesh::PropertyMeshKernel]] |
|||
* [[FeaturePython Custom Properties#Mesh::PropertyNormalList|Mesh::PropertyNormalList]] |
|||
* [[FeaturePython Custom Properties#Part::PropertyFilletEdges|Part::PropertyFilletEdges]] |
|||
* [[FeaturePython Custom Properties#Part::PropertyGeometryList|Part::PropertyGeometryList]] |
|||
* [[FeaturePython Custom Properties#Part::PropertyPartShape|Part::PropertyPartShape]] |
|||
* [[FeaturePython Custom Properties#Part::PropertyShapeHistory|Part::PropertyShapeHistory]] |
|||
* [[FeaturePython Custom Properties#Path::PropertyPath|Path::PropertyPath]] |
|||
* [[FeaturePython Custom Properties#Path::PropertyTool|Path::PropertyTool]] |
|||
* [[FeaturePython Custom Properties#Path::PropertyTooltable|Path::PropertyTooltable]] |
|||
* [[FeaturePython Custom Properties#Sketcher::PropertyConstraintList|Sketcher::PropertyConstraintList]] |
|||
* [[FeaturePython Custom Properties#Spreadsheet::PropertyColumnWidths|Spreadsheet::PropertyColumnWidths]] |
|||
* [[FeaturePython Custom Properties#Spreadsheet::PropertyRowHeights|Spreadsheet::PropertyRowHeights]] |
|||
* [[FeaturePython Custom Properties#Spreadsheet::PropertySheet|Spreadsheet::PropertySheet]] |
|||
* [[FeaturePython Custom Properties#Spreadsheet::PropertySpreadsheetQuantity|Spreadsheet::PropertySpreadsheetQuantity]] |
|||
* [[FeaturePython Custom Properties#TechDraw::PropertyCenterLineList|TechDraw::PropertyCenterLineList]] |
|||
* [[FeaturePython Custom Properties#TechDraw::PropertyCosmeticEdgeList|TechDraw::PropertyCosmeticEdgeList]] |
|||
* [[FeaturePython Custom Properties#TechDraw::PropertyCosmeticVertexList|TechDraw::PropertyCosmeticVertexList]] |
|||
* [[FeaturePython Custom Properties#TechDraw::PropertyGeomFormatList|TechDraw::PropertyGeomFormatList]] |
|||
Když do uživatelského objektu přidáváte vlastnosti dejte pozor na:: |
Když do uživatelského objektu přidáváte vlastnosti dejte pozor na:: |
||
* Nepoužívejte znaky "<" a ">" v popisu vlastnosti (odděluje to části XML v souboru .fcstd) |
* Nepoužívejte znaky "<" a ">" v popisu vlastnosti (odděluje to části XML v souboru .fcstd) |
||
* Vlastnosti jsou uloženy podle abecedy ve .fcstd souboru. Máte-li ve vlastnostech tvar (shape), jakékoliv jméno vlastnosti, které je za "Shape" podle abecedy, bude nataženo až po tvaru, což může zapříčinit neočekávané chování. |
* Vlastnosti jsou uloženy podle abecedy ve .fcstd souboru. Máte-li ve vlastnostech tvar (shape), jakékoliv jméno vlastnosti, které je za "Shape" podle abecedy, bude nataženo až po tvaru, což může zapříčinit neočekávané chování. |
||
A complete list of property attributes can be seen in the [https://github.com/FreeCAD/FreeCAD/blob/master/src/App/PropertyStandard.h PropertyStandard C++ header file]. |
|||
For instance, if you want to allow the user to enter only a limited range of values (e.g. using PropertyIntegerConstraint), in Python you will assign a tuple containing not only the property value, but also the lower and upper limit as well as the stepsize, as below: |
|||
{{Code|code= |
|||
prop = (value, lower, upper, stepsize) |
|||
}} |
|||
==Typ vlastnosti== |
==Typ vlastnosti== |
||
Standardně mohou být vlastnosti upravovány. Je ale možné nastavit vlastnosti pouze ke čtení, třeba když má jenom zobrazovat výstup výsledku metody. Je možné také vlastnost skrýt. |
Standardně mohou být vlastnosti upravovány. Je ale možné nastavit vlastnosti pouze ke čtení, třeba když má jenom zobrazovat výstup výsledku metody. Je možné také vlastnost skrýt. |
||
Typ vlastnosti může být nastaven použitím |
Typ vlastnosti může být nastaven použitím |
||
<syntaxhighlight> |
|||
{{Code|code= |
|||
obj.setEditorMode("MyPropertyName", mode) |
|||
obj.setEditorMode("MyPropertyName", mode) |
|||
</syntaxhighlight> |
|||
}} |
|||
kde mode je malá celočíselná hodnota, které může být nastavena na: |
kde mode je malá celočíselná hodnota, které může být nastavena na: |
||
0 -- defaultní mód, čtení i zápis |
0 -- defaultní mód, čtení i zápis |
||
Line 192: | Line 286: | ||
2 -- skryto |
2 -- skryto |
||
The EditorModes are not set at FreeCAD file reload. This could to be done by the __setstate__ 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 PropertyEditor. They could 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): |
|||
{{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 |
|||
You can find these different property types defined in the [https://github.com/FreeCAD/FreeCAD/blob/master/src/App/PropertyContainer.h source code C++ header for PropertyContainer] |
|||
<div class="mw-translate-fuzzy"> |
|||
== Další složitější příklady == |
== Další složitější příklady == |
||
Tento příklad používá [[Part Module/cs|Modul Díl]] k vytvoření osmistěnu a potom |
Tento příklad používá [[Part Module/cs|Modul Díl]] k vytvoření osmistěnu a potom vytvoří pomocí Pivy jeho reprezentaci v Coinu. |
||
</div> |
|||
První je samotné vytvoření dokumentu: |
První je samotné vytvoření dokumentu: |
||
<syntaxhighlight> |
|||
{{Code|code= |
|||
import FreeCAD, FreeCADGui, Part |
|||
import FreeCAD, FreeCADGui, Part |
|||
import pivy |
|||
class Octahedron: |
|||
from pivy import coin |
|||
def __init__(self, obj): |
|||
"Add some custom properties to our box feature" |
|||
class Octahedron: |
|||
obj.addProperty("App::PropertyLength","Length","Octahedron","Length of the octahedron").Length=1.0 |
|||
def __init__(self, obj): |
|||
obj.addProperty("App::PropertyLength","Width","Octahedron","Width of the octahedron").Width=1.0 |
|||
"Add some custom properties to our box feature" |
|||
obj.addProperty("App::PropertyLength","Height","Octahedron", "Height of the octahedron").Height=1.0 |
|||
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.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 execute(self, fp): |
|||
obj.Proxy = self |
|||
# Define six vetices for the shape |
|||
v1 = FreeCAD.Vector(0,0,0) |
|||
def execute(self, fp): |
|||
v2 = FreeCAD.Vector(fp.Length,0,0) |
|||
# Define six vetices for the shape |
|||
v3 = FreeCAD.Vector(0,fp.Width,0) |
|||
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) |
|||
# Make the wires/faces |
|||
v6 = FreeCAD.Vector(fp.Length/2,fp.Width/2,-fp.Height/2) |
|||
f1 = self.make_face(v1,v2,v5) |
|||
f2 = self.make_face(v2,v4,v5) |
|||
# Make the wires/faces |
|||
f3 = self.make_face(v4,v3,v5) |
|||
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) |
|||
shell=Part.makeShell([f1,f2,f3,f4,f5,f6,f7,f8]) |
|||
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) |
|||
# helper mehod to create the faces |
|||
fp.Shape = solid |
|||
def make_face(self,v1,v2,v3): |
|||
wire = Part.makePolygon([v1,v2,v3,v1]) |
|||
# helper mehod to create the faces |
|||
face = Part.Face(wire) |
|||
def make_face(self,v1,v2,v3): |
|||
return face |
|||
wire = Part.makePolygon([v1,v2,v3,v1]) |
|||
</syntaxhighlight> |
|||
face = Part.Face(wire) |
|||
return face |
|||
}} |
|||
Pak máme objekt pro zobrazení (view provider object), zodpovědný za zobrazení objektu ve 3D: |
Pak máme objekt pro zobrazení (view provider object), zodpovědný za zobrazení objektu ve 3D: |
||
<syntaxhighlight> |
|||
{{Code|code= |
|||
class ViewProviderOctahedron: |
|||
class ViewProviderOctahedron: |
|||
def __init__(self, obj): |
|||
def __init__(self, obj): |
|||
"Set this object to the proxy object of the actual view provider" |
|||
"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.addProperty("App::PropertyColor","Color","Octahedron","Color of the octahedron").Color=(1.0,0.0,0.0) |
|||
obj.Proxy = self |
|||
obj.Proxy = self |
|||
def attach(self, obj): |
|||
def attach(self, obj): |
|||
"Setup the scene sub-graph of the view provider, this method is mandatory" |
|||
"Setup the scene sub-graph of the view provider, this method is mandatory" |
|||
self.shaded = coin.SoGroup() |
|||
self.shaded = coin.SoGroup() |
|||
self.wireframe = coin.SoGroup() |
|||
self.scale = coin.SoScale() |
|||
self.color = coin.SoBaseColor() |
|||
self.data=coin.SoCoordinate3() |
|||
self.data=coin.SoCoordinate3() |
|||
self.face=coin.SoIndexedLineSet() |
|||
self.shaded.addChild(self.scale) |
|||
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=coin.SoDrawStyle() |
|||
style.style = coin.SoDrawStyle.LINES |
|||
self.wireframe.addChild(style) |
|||
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") |
|||
self.onChanged(obj,"Color") |
|||
def updateData(self, fp, prop): |
|||
def updateData(self, fp, prop): |
|||
"If a property of the handled feature has changed we have the chance to handle this here" |
|||
"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) |
|||
self.data.point.set1Value(cnt,i.X,i.Y,i.Z) |
|||
cnt=cnt+1 |
|||
self.face.coordIndex.set1Value(0,0) |
|||
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(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(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(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(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(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(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(28,0) |
|||
self.face.coordIndex.set1Value(29,4) |
|||
self.face.coordIndex.set1Value(30,5) |
|||
self.face.coordIndex.set1Value(31,-1) |
|||
def getDisplayModes(self,obj): |
|||
def getDisplayModes(self,obj): |
|||
"Return a list of display modes." |
|||
modes |
"Return a list of display modes." |
||
modes=[] |
|||
modes.append("Shaded") |
|||
modes.append("Wireframe") |
|||
return modes |
|||
def getDefaultDisplayMode(self): |
|||
def getDefaultDisplayMode(self): |
|||
"Return the name of the default display mode. It must be defined in getDisplayModes." |
|||
"Return the name of the default display mode. It must be defined in getDisplayModes." |
|||
return "Shaded" |
|||
return "Shaded" |
|||
def setDisplayMode(self,mode): |
|||
def setDisplayMode(self,mode): |
|||
return mode |
|||
def onChanged(self, vp, prop): |
|||
def onChanged(self, vp, prop): |
|||
"Here we can do something when a single property got changed" |
|||
"Here we can do something when a single property got changed" |
|||
FreeCAD.Console.PrintMessage("Change property: " + str(prop) + "\n") |
|||
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): |
|||
def getIcon(self): |
|||
return """ |
|||
return """ |
|||
/* XPM */ |
|||
static const char * ViewProviderBox_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): |
|||
def __getstate__(self): |
|||
return None |
|||
return None |
|||
def __setstate__(self,state): |
|||
def __setstate__(self,state): |
|||
return None |
|||
return None |
|||
</syntaxhighlight> |
|||
}} |
|||
A nakonec, když je objekt i jeho zobrazení definováno, stačí ho už jen zavolat: |
A nakonec, když je objekt i jeho zobrazení definováno, stačí ho už jen zavolat: |
||
<syntaxhighlight> |
|||
{{Code|code= |
|||
FreeCAD.newDocument() |
|||
FreeCAD.newDocument() |
|||
a=FreeCAD.ActiveDocument.addObject("App::FeaturePython","Octahedron") |
|||
a=FreeCAD.ActiveDocument.addObject("App::FeaturePython","Octahedron") |
|||
Octahedron(a) |
|||
Octahedron(a) |
|||
ViewProviderOctahedron(a.ViewObject) |
|||
ViewProviderOctahedron(a.ViewObject) |
|||
</syntaxhighlight> |
|||
}} |
|||
<div class="mw-translate-fuzzy"> |
|||
== Zpřístupnění objektu k výběru == |
== Zpřístupnění objektu k výběru == |
||
Chcete-li aby bylo možné objekt vybrat nebo alespoň jeho část, kliknutím na něj v pohledu, musíte včlenit jeho Coin konstrukci do uzlu SoFCSelection. Má-li objekt komplexní zobrazení s widgety, anotacemi atd., můžete chtít včlenit do SoFCSelection pouze nějakou část. Všechno co je SoFCSelection je průběžně skenováno FreeCADem pro detekci výběru/předvýběru, takže je rozumné nepřetěžovat jej zbytečným skenováním. Tady je co byste měli zahrnout do self.face z příkladu nahoře. |
Chcete-li aby bylo možné objekt vybrat nebo alespoň jeho část, kliknutím na něj v pohledu, musíte včlenit jeho Coin konstrukci do uzlu SoFCSelection. Má-li objekt komplexní zobrazení s widgety, anotacemi atd., můžete chtít včlenit do SoFCSelection pouze nějakou část. Všechno co je SoFCSelection je průběžně skenováno FreeCADem pro detekci výběru/předvýběru, takže je rozumné nepřetěžovat jej zbytečným skenováním. Tady je co byste měli zahrnout do self.face z příkladu nahoře. |
||
</div> |
|||
<syntaxhighlight> |
|||
selectionNode = coin.SoType.fromName("SoFCSelection").createInstance() |
|||
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). |
|||
selectionNode.documentName.setValue(FreeCAD.ActiveDocument.Name) |
|||
selectionNode.objectName.setValue(obj.Object.Name) # here obj is the ViewObject, we need its associated App Object |
|||
Here is the molecule example above, adapted to make the elements of the molecule selectable: |
|||
selectionNode.subElementName.setValue("Face") |
|||
{{Code|code= |
|||
selectNode.addChild(self.face) |
|||
class Molecule: |
|||
... |
|||
def __init__(self, obj): |
|||
self.shaded.addChild(selectionNode) |
|||
''' Add two point properties ''' |
|||
self.wireframe.addChild(selectionNode) |
|||
obj.addProperty("App::PropertyVector","p1","Line","Start point") |
|||
</syntaxhighlight> |
|||
obj.addProperty("App::PropertyVector","p2","Line","End point").p2=FreeCAD.Vector(5,0,0) |
|||
Jednoduše vytvoříte uzel SoFCSelection, potom přidáte konstrukční uzly a potom přidáte hlavní uzel misto přímého přidávání konstrukčních uzlů. |
|||
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 __getstate__(self): |
|||
return None |
|||
def __setstate__(self,state): |
|||
return None |
|||
def makeMolecule(): |
|||
FreeCAD.newDocument() |
|||
a=FreeCAD.ActiveDocument.addObject("Part::FeaturePython","Molecule") |
|||
Molecule(a) |
|||
ViewProviderMolecule(a.ViewObject) |
|||
FreeCAD.ActiveDocument.recompute() |
|||
}} |
|||
== Práce s jednoduchými tvary == |
== Práce s jednoduchými tvary == |
||
Jestliže z parametrického |
Jestliže z parametrického objektu vychází jednoduchý tvar, není nutné používat zobrazovací objekt. Tvar bude zobrazován použitím standardního zobrazování tvarů ve FreeCADu. |
||
<syntaxhighlight> |
|||
{{Code|code= |
|||
class Line: |
|||
import FreeCAD as App |
|||
def __init__(self, obj): |
|||
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() |
|||
}} |
|||
Same code with use '''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> |
|||
{{docnav/cs|[[PyQt/cs|PyQt]]|[[Embedding FreeCAD/cs|Vkládání FreeCADu]]}} |
|||
class ViewProviderLine: |
|||
[[Category:Poweruser Documentation/cs|Kategorie:Dokumentace pokročilého uživatele]] |
|||
def __init__(self, obj): |
|||
[[Category:Python Code/cs|Kategorie:Python kódy]] |
|||
''' 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 == |
|||
You may have noticed that the examples above construct their scenegraphs in slightly different ways. Some use {{incode|obj.addDisplayMode(node, "modename")}} while others use {{incode|obj.SwitchNode.getChild(x).addChild(y)}}. |
|||
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 __getstate__(self): |
|||
return None |
|||
def __setstate__(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() |
|||
}} |
|||
<div class="mw-translate-fuzzy"> |
|||
{{docnav/cs|[[PySide/cs|PySide]]|[[Embedding FreeCAD/cs|Vkládání FreeCADu]]}} |
|||
</div> |
|||
Additional pages: |
|||
* [[Scripted_objects_saving_attributes|Scripted objects saving attributes]] |
|||
* [[Scripted_objects_migration|Scripted objects migration]] |
|||
* [[Scripted objects with attachment|Scripted objects with attachment]] |
|||
* [[Viewprovider|Viewproviders]] |
|||
Interesting forum threads about scripted objects: |
|||
* [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 __getstate__ and __setstate__], [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?] |
|||
In addition to the examples presented here have a look at FreeCAD source code [https://github.com/FreeCAD/FreeCAD/blob/master/src/Mod/TemplatePyMod/FeaturePython.py src/Mod/TemplatePyMod/FeaturePython.py] for more examples. |
|||
{{Powerdocnavi{{#translation:}}}} |
|||
[[Category:Developer Documentation{{#translation:}}]] |
|||
[[Category:Python Code{{#translation:}}]] |
|||
{{clear}} |
{{clear}} |
||
<languages/> |
Revision as of 21:23, 20 January 2021
Introduction
Kromě standardních objektových typů jako jsou anotace, sítě a díly, nabízí FreeCAD skvělou možnost vytváření objektů 100% vytvořených skritpy Pythonu, které se nazývají Pythonovské objekty. Tyto objekty se chovají stejně jako jiné objekty FreeCADu a jsou ukládány a načítány automaticky při ukládání a otevírání souboru.
Je třeba pochopit jednu zvláštnost, tyto objekty jsou ukládány ve FcStd souborech FreeCADu s pythonovským modulem json. Tento modul převede pythonovský objekt do řetězce, který je pak možno uložit v souboru. Při načítání naopak tento modul použije uložený řetězec ke znovuvytvoření původního objektu, při tom musí mít přístup ke zdrojovému kódu, který vytvoří objekt. To znamená, že když uložíte takový uživatelský objekt a pak jej otevíráte na počítači kde není pythonovský kód, tak nebude tento objekt vytvořen. Když tedy distribuujete takový objekt někomu jinému, musíte společně s ním distribuovat i pythonovský skript, který objekt vytváří.
Note: It is possible to pack python code inside a FreeCAD file using json serializing with an App::PropertyPythonObject, but that code can never directly be run, and therefore has little use for our purpose here.
Pythonovský objekt má stejné pravidlo jako FreeCAD: Aplikace a GUI jsou odděleny do samostatných částí. Aplikační část, Document Object, definuje konstrukci objektu, zatímco část GUI, View Provider Object, definuje jak bude objekt zobrazen na displeji. View Provider Object, stejně jako další GUI objekty FreeCADu je dostupný pouze když FreeCAD běží se svým vlastním GUI. Pro vytvoření objektu je použitelných několik vlastností a metod. Vlastnosti musejí být některé z předdefinovaných typových vlastností, které nabízí FreeCAD a zobrazují se v dialogovém okně vlastností, takže mohou být uživatelem upravovány. Tímto způsobem jsou Pythonovské objekty správně a zcela parametrizovány. Můžete samostatně definovat vlastnosti objektu a jeho zobrazovacího objektu.
Základní příklad
Následující příklad najdete v souboru src/Mod/TemplatePyMod/FeaturePython.py, společně s několika dalšími příklady:
'''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 __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)
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.
An explanation of __getstate__
and __setstate__
is in the forum thread obj.Proxy.Type is a dict, not a string.
Available methods
See FeaturePython methods for the complete reference.
Dostupné vlastnosti
Vlastnosti jsou skutečné základní kameny pythonovských objektů. Jejich prostřednictvím je uživatel schopen pracovat s objektem. Po vytvoření Pythonovského objektu v dokumentu ( obj=FreeCAD.ActiveDocument.addObject("App::FeaturePython","Box") ), obdržíte seznam dostupných vlastností zadáním:
obj.supportedProperties()
Dostanete seznam dostupných vlastností:
- App::PropertyAcceleration
- App::PropertyAngle
- App::PropertyArea
- App::PropertyBool
- App::PropertyBoolList
- App::PropertyColor
- App::PropertyColorList
- App::PropertyDirection
- App::PropertyDistance
- App::PropertyEnumeration
- App::PropertyExpressionEngine
- App::PropertyFile
- App::PropertyFileIncluded
- App::PropertyFloat
- App::PropertyFloatConstraint
- App::PropertyFloatList
- App::PropertyFont
- App::PropertyForce
- App::PropertyFrequency
- App::PropertyInteger
- App::PropertyIntegerConstraint
- App::PropertyIntegerList
- App::PropertyIntegerSet
- App::PropertyLength
- App::PropertyLink
- App::PropertyLinkChild
- App::PropertyLinkGlobal
- App::PropertyLinkHidden
- App::PropertyLinkList
- App::PropertyLinkListChild
- App::PropertyLinkListGlobal
- App::PropertyLinkListHidden
- App::PropertyLinkSub
- App::PropertyLinkSubChild
- App::PropertyLinkSubGlobal
- App::PropertyLinkSubHidden
- App::PropertyLinkSubList
- App::PropertyLinkSubListChild
- App::PropertyLinkSubListGlobal
- App::PropertyLinkSubListHidden
- App::PropertyMap
- App::PropertyMaterial
- App::PropertyMaterialList
- App::PropertyMatrix
- App::PropertyPath
- App::PropertyPercent
- App::PropertyPersistentObject
- App::PropertyPlacement
- App::PropertyPlacementLink
- App::PropertyPlacementList
- App::PropertyPosition
- App::PropertyPrecision
- App::PropertyPressure
- App::PropertyPythonObject
- App::PropertyQuantity
- App::PropertyQuantityConstraint
- App::PropertySpeed
- App::PropertyString
- App::PropertyStringList
- App::PropertyUUID
- App::PropertyVacuumPermittivity
- App::PropertyVector
- App::PropertyVectorDistance
- App::PropertyVectorList
- App::PropertyVolume
- App::PropertyXLink
- App::PropertyXLinkList
- App::PropertyXLinkSub
- App::PropertyXLinkSubList
- Mesh::PropertyCurvatureList
- Mesh::PropertyMeshKernel
- Mesh::PropertyNormalList
- Part::PropertyFilletEdges
- Part::PropertyGeometryList
- Part::PropertyPartShape
- Part::PropertyShapeHistory
- Path::PropertyPath
- Path::PropertyTool
- Path::PropertyTooltable
- Sketcher::PropertyConstraintList
- Spreadsheet::PropertyColumnWidths
- Spreadsheet::PropertyRowHeights
- Spreadsheet::PropertySheet
- Spreadsheet::PropertySpreadsheetQuantity
- TechDraw::PropertyCenterLineList
- TechDraw::PropertyCosmeticEdgeList
- TechDraw::PropertyCosmeticVertexList
- TechDraw::PropertyGeomFormatList
Když do uživatelského objektu přidáváte vlastnosti dejte pozor na::
- Nepoužívejte znaky "<" a ">" v popisu vlastnosti (odděluje to části XML v souboru .fcstd)
- Vlastnosti jsou uloženy podle abecedy ve .fcstd souboru. Máte-li ve vlastnostech tvar (shape), jakékoliv jméno vlastnosti, které je za "Shape" podle abecedy, bude nataženo až po tvaru, což může zapříčinit neočekávané chování.
A complete list of property attributes can be seen in the PropertyStandard C++ header file. For instance, if you want to allow the user to enter only a limited range of values (e.g. using PropertyIntegerConstraint), in Python you will assign a tuple containing not only the property value, but also the lower and upper limit as well as the stepsize, as below:
prop = (value, lower, upper, stepsize)
Typ vlastnosti
Standardně mohou být vlastnosti upravovány. Je ale možné nastavit vlastnosti pouze ke čtení, třeba když má jenom zobrazovat výstup výsledku metody. Je možné také vlastnost skrýt. Typ vlastnosti může být nastaven použitím
obj.setEditorMode("MyPropertyName", mode)
kde mode je malá celočíselná hodnota, které může být nastavena na:
0 -- defaultní mód, čtení i zápis 1 -- pouze čtení 2 -- skryto
The EditorModes are not set at FreeCAD file reload. This could to be done by the __setstate__ 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 PropertyEditor. They could 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
You can find these different property types defined in the source code C++ header for PropertyContainer
Další složitější příklady
Tento příklad používá Modul Díl k vytvoření osmistěnu a potom vytvoří pomocí Pivy jeho reprezentaci v Coinu.
První je samotné vytvoření dokumentu:
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
Pak máme objekt pro zobrazení (view provider object), zodpovědný za zobrazení objektu ve 3D:
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.SoIndexedLineSet()
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 __getstate__(self):
return None
def __setstate__(self,state):
return None
A nakonec, když je objekt i jeho zobrazení definováno, stačí ho už jen zavolat:
FreeCAD.newDocument()
a=FreeCAD.ActiveDocument.addObject("App::FeaturePython","Octahedron")
Octahedron(a)
ViewProviderOctahedron(a.ViewObject)
Zpřístupnění objektu k výběru
Chcete-li aby bylo možné objekt vybrat nebo alespoň jeho část, kliknutím na něj v pohledu, musíte včlenit jeho Coin konstrukci do uzlu SoFCSelection. Má-li objekt komplexní zobrazení s widgety, anotacemi atd., můžete chtít včlenit do SoFCSelection pouze nějakou část. Všechno co je SoFCSelection je průběžně skenováno FreeCADem pro detekci výběru/předvýběru, takže je rozumné nepřetěžovat jej zbytečným skenováním. Tady je co byste měli zahrnout do self.face z příkladu nahoře.
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).
Here is the molecule example above, adapted to make the elements of the molecule selectable:
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 __getstate__(self):
return None
def __setstate__(self,state):
return None
def makeMolecule():
FreeCAD.newDocument()
a=FreeCAD.ActiveDocument.addObject("Part::FeaturePython","Molecule")
Molecule(a)
ViewProviderMolecule(a.ViewObject)
FreeCAD.ActiveDocument.recompute()
Práce s jednoduchými tvary
Jestliže z parametrického objektu vychází jednoduchý tvar, není nutné používat zobrazovací objekt. Tvar bude zobrazován použitím standardního zobrazování tvarů ve FreeCADu.
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()
Same code with use 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
You may have noticed that the examples above construct their scenegraphs in slightly different ways. Some use obj.addDisplayMode(node, "modename")
while others use obj.SwitchNode.getChild(x).addChild(y)
.
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 __getstate__(self):
return None
def __setstate__(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()
Additional pages:
- Scripted objects saving attributes
- Scripted objects migration
- Scripted objects with attachment
- Viewproviders
Interesting forum threads about scripted objects:
- Python object attributes lost at load
- New FeaturePython is grey
- Explanation on __getstate__ and __setstate__, official documentation
- Eigenmode frequency always 0?
- how to implement python feature's setEdit properly?
In addition to the examples presented here have a look at FreeCAD source code src/Mod/TemplatePyMod/FeaturePython.py for more examples.
- 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