You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
importFreeCADimportPartclassMyParametricObject:
"""Proxy class for a custom parametric object."""def__init__(self, obj):
"""Initialize and add properties."""obj.Proxy=selfself.Type="MyParametricObject"# Add custom propertiesobj.addProperty("App::PropertyLength", "Length", "Dimensions",
"The length of the object").Length=10.0obj.addProperty("App::PropertyLength", "Width", "Dimensions",
"The width of the object").Width=10.0obj.addProperty("App::PropertyLength", "Height", "Dimensions",
"The height of the object").Height=5.0obj.addProperty("App::PropertyBool", "Chamfered", "Options",
"Apply chamfer to edges").Chamfered=Falseobj.addProperty("App::PropertyLength", "ChamferSize", "Options",
"Size of chamfer").ChamferSize=1.0defexecute(self, obj):
"""Called when the document is recomputed. Build the shape here."""shape=Part.makeBox(obj.Length, obj.Width, obj.Height)
ifobj.Chamferedandobj.ChamferSize>0:
shape=shape.makeChamfer(obj.ChamferSize, shape.Edges)
obj.Shape=shapedefonChanged(self, obj, prop):
"""Called when any property changes."""ifprop=="Chamfered":
# Show/hide ChamferSize based on Chamfered toggleifobj.Chamfered:
obj.setPropertyStatus("ChamferSize", "-Hidden")
else:
obj.setPropertyStatus("ChamferSize", "Hidden")
defonDocumentRestored(self, obj):
"""Called when the document is loaded. Re-initialize if needed."""self.Type="MyParametricObject"def__getstate__(self):
"""Serialize the proxy (for saving .FCStd)."""return {"Type": self.Type}
def__setstate__(self, state):
"""Deserialize the proxy (for loading .FCStd)."""ifstate:
self.Type=state.get("Type", "MyParametricObject")
ViewProvider — Complete Template
importFreeCADGuifrompivyimportcoinclassViewProviderMyObject:
"""Controls how the object appears in the 3D view and tree."""def__init__(self, vobj):
vobj.Proxy=self# Add view properties if needed# vobj.addProperty("App::PropertyColor", "Color", "Display", "Object color")defattach(self, vobj):
"""Called when the view provider is attached to the view object."""self.Object=vobj.Objectself.standard=coin.SoGroup()
vobj.addDisplayMode(self.standard, "Standard")
defgetDisplayModes(self, vobj):
"""Return available display modes."""return ["Standard"]
defgetDefaultDisplayMode(self):
"""Return the default display mode."""return"Standard"defsetDisplayMode(self, mode):
returnmodedefgetIcon(self):
"""Return the icon path for the tree view."""return":/icons/Part_Box.svg"# Or return an XPM string, or path to a .svg/.png filedefupdateData(self, obj, prop):
"""Called when the model object's data changes."""passdefonChanged(self, vobj, prop):
"""Called when a view property changes."""passdefdoubleClicked(self, vobj):
"""Called on double-click in the tree."""# Open a task panel, for examplereturnTruedefsetupContextMenu(self, vobj, menu):
"""Add items to the right-click context menu."""action=menu.addAction("My Action")
action.triggered.connect(lambda: self._myAction(vobj))
def_myAction(self, vobj):
FreeCAD.Console.PrintMessage("Context menu action triggered\n")
defclaimChildren(self):
"""Return list of child objects to show in tree hierarchy."""# return [self.Object.BaseFeature] if hasattr(self.Object, "BaseFeature") else []return []
def__getstate__(self):
returnNonedef__setstate__(self, state):
returnNone
Creating the Object
defmakeMyObject(name="MyObject"):
"""Factory function to create the parametric object."""doc=FreeCAD.ActiveDocumentifdocisNone:
doc=FreeCAD.newDocument()
obj=doc.addObject("Part::FeaturePython", name)
MyParametricObject(obj)
ifFreeCAD.GuiUp:
ViewProviderMyObject(obj.ViewObject)
doc.recompute()
returnobj# Usageobj=makeMyObject("ChamferedBlock")
obj.Length=20.0obj.Chamfered=TrueFreeCAD.ActiveDocument.recompute()
Complete Property Type Reference
Numeric Properties
Type
Python
Notes
App::PropertyInteger
int
Standard integer
App::PropertyFloat
float
Standard float
App::PropertyLength
float
Length with units (mm)
App::PropertyDistance
float
Distance (can be negative)
App::PropertyAngle
float
Angle in degrees
App::PropertyArea
float
Area with units
App::PropertyVolume
float
Volume with units
App::PropertySpeed
float
Speed with units
App::PropertyAcceleration
float
Acceleration
App::PropertyForce
float
Force
App::PropertyPressure
float
Pressure
App::PropertyPercent
int
0-100 integer
App::PropertyQuantity
Quantity
Generic unit-aware value
App::PropertyIntegerConstraint
(val,min,max,step)
Bounded integer
App::PropertyFloatConstraint
(val,min,max,step)
Bounded float
String/Path Properties
Type
Python
Notes
App::PropertyString
str
Text string
App::PropertyFont
str
Font name
App::PropertyFile
str
File path
App::PropertyFileIncluded
str
Embedded file
App::PropertyPath
str
Directory path
Boolean and Enumeration
Type
Python
Notes
App::PropertyBool
bool
True/False
App::PropertyEnumeration
list/str
Dropdown; set list then value
# Enumeration usageobj.addProperty("App::PropertyEnumeration", "Style", "Options", "Style choice")
obj.Style= ["Solid", "Wireframe", "Points"] # set choices FIRSTobj.Style="Solid"# then set value
Geometric Properties
Type
Python
Notes
App::PropertyVector
FreeCAD.Vector
3D vector
App::PropertyVectorList
[Vector,...]
List of vectors
App::PropertyPlacement
Placement
Position + rotation
App::PropertyMatrix
Matrix
4x4 matrix
App::PropertyVectorDistance
Vector
Vector with units
App::PropertyPosition
Vector
Position with units
App::PropertyDirection
Vector
Direction vector
Link Properties
Type
Python
Notes
App::PropertyLink
obj ref
Link to one object
App::PropertyLinkList
[obj,...]
Link to multiple objects
App::PropertyLinkSub
(obj, [subs])
Link with sub-elements
App::PropertyLinkSubList
[(obj,[subs]),...]
Multiple link+subs
App::PropertyLinkChild
obj ref
Claimed child link
App::PropertyLinkListChild
[obj,...]
Multiple claimed children
Shape and Material
Type
Python
Notes
Part::PropertyPartShape
Part.Shape
Full shape
App::PropertyColor
(r,g,b)
Color (0.0-1.0)
App::PropertyColorList
[(r,g,b),...]
Color per element
App::PropertyMaterial
Material
Material definition
Container Properties
Type
Python
Notes
App::PropertyPythonObject
any
Serializable Python object
App::PropertyIntegerList
[int,...]
List of integers
App::PropertyFloatList
[float,...]
List of floats
App::PropertyStringList
[str,...]
List of strings
App::PropertyBoolList
[bool,...]
List of booleans
App::PropertyMap
{str:str}
String dictionary
Object Dependency Tracking
# InList: objects that reference this objectobj.InList# [objects referencing obj]obj.InListRecursive# all ancestors# OutList: objects this object referencesobj.OutList# [objects obj references]obj.OutListRecursive# all descendants