FreeCAD Power User Skill
This skill equips AI agents to write idiomatic, high-performance, and crash-resilient Python code for FreeCAD.
Based on the official FreeCAD Documentation: Power users hub.
The FreeCAD Python Architecture
FreeCAD has a dual-layer architecture:
FreeCAD (aliased as App): The core application module. Manages documents, geometric data, parametric dependency graph, and properties. Operates without any GUI (usable headlessly).
FreeCADGui (aliased as Gui): The presentation layer. Manages 3D scene representation (Coin3D/Pivy scenegraph), selection, Qt/PySide windows, task panels, and colors.
import FreeCAD as App
import FreeCADGui as Gui # Only import when GUI is present!
import Part
import math
[!IMPORTANT]
Headless Safety:
Never import FreeCADGui or call Gui. methods in scripts intended to run headlessly, in background workers, or in automated unit tests. FreeCAD can run with freecadcmd or FreeCAD --console. Check App.GuiUp before calling GUI code:
if App.GuiUp:
import FreeCADGui as Gui
Gui.ActiveDocument.ActiveView.fitAll()
Essential Patterns & Idioms
1. Document Management & Transactions
Always handle document creation and recomputation properly:
import FreeCAD as App
# Get or create active document
doc = App.ActiveDocument
if not doc:
doc = App.newDocument("ParametricDesign")
# Wrap modifications in a transaction for clean Undo/Redo
doc.openTransaction("Generate Model")
try:
# ... create or edit objects ...
doc.recompute()
doc.commitTransaction()
except Exception as e:
doc.abortTransaction()
raise e
2. Geometry Creation: Part vs Sketcher
Direct B-Rep with the Part Module (Fast, Precise, Headless-Friendly):
import FreeCAD as App
import Part
doc = App.ActiveDocument or App.newDocument()
# Create geometric primitives in memory
box = Part.makeBox(100, 50, 20)
cylinder = Part.makeCylinder(10, 30, App.Vector(50, 25, 0), App.Vector(0, 0, 1))
# Boolean Cut
result_shape = box.cut(cylinder)
# Add to document
feature = doc.addObject("Part::Feature", "CutPlate")
feature.Shape = result_shape
doc.recompute()
Parametric Sketch + PartDesign Body:
import FreeCAD as App
import Sketcher
doc = App.ActiveDocument or App.newDocument()
body = doc.addObject("PartDesign::Body", "Body")
sketch = body.newObject("Sketcher::SketchObject", "Sketch")
sketch.Support = (doc.XY_Plane, [""])
sketch.MapMode = "FlatFace"
# Add geometry (Line segment)
sketch.addGeometry(Part.LineSegment(App.Vector(0, 0, 0), App.Vector(50, 0, 0)), False)
# Add constraints
sketch.addConstraint(Sketcher.Constraint('Coincident', 0, 1, -1, 1)) # Vertex 1 to origin
doc.recompute()
3. Scripted Objects (FeaturePython)
A FeaturePython object creates a completely custom parametric object in the tree view that automatically recomputes whenever its properties change.
Core Rules:
- Store logic in a python proxy class.
- Implement
execute(self, obj) to rebuild geometry when properties change.
- Implement
onChanged(self, obj, prop) to validate property updates.
- Store state in
App::Property* fields, NOT inside self instance attributes unless implementing __getstate__ / __setstate__ (dumps and loads).
Detailed References
Consult the deep-dive manuals in references/:
- Python API Full Catalog: Complete index of all 58 Python API documentation pages, scripting modules (
App, Gui, Part, Draft, Mesh, Sketcher, TechDraw, FEM), and classes.
- Macro Recipes & Automation Catalog: Comprehensive catalog of 243 official macros, snippets, recipes, and automation scripts.
- Python API Quick Reference: Document methods, property types, Vector/Matrix math, units, and transactions.
- Topological Data Scripting: Deep dive into OpenCASCADE
Part::TopoShape, vertices, wires, faces, shells, and boolean cuts.
- Scripted Objects (
FeaturePython): Production template with properties, recompute lifecycle, ViewProvider, and serialization.
- PySide & GUI Integration: Custom commands, toolbars, modal dialogs, task panels, and Coin3D/Pivy scene graph.
- Headless Execution & Batch Automation: Running FreeCAD from CLI, Python virtualenvs, Docker, and MCP servers.
Offline Documentation Search Tool
To query methods, parameters, and macro examples across all 2,595+ documentation files:
python scripts/freecad_doc_query.py "<method_or_macro_name>"
Code Examples
Check examples/ for ready-to-run templates:
1---2name: freecad-power-user3description: >-4 Advanced Python scripting, automation, macro creation, and headless execution for FreeCAD.5 Covers FreeCAD API (`FreeCAD`/`App`, `FreeCADGui`/`Gui`, `Part`, `Draft`, `Sketcher`),6 topological B-Rep scripting (OpenCASCADE shapes, wires, faces, booleans), custom parametric7 scripted objects (`FeaturePython` and ViewProviders with `execute`, `onChanged`, and pickle8 `dumps`/`loads`), PySide/Qt UI dialogs, and running FreeCAD headlessly in scripts or CI/RPC.9 Use this skill whenever generating FreeCAD Python code, writing or debugging macros,10 creating custom parametric plugins, or automating CAD operations.11metadata:12 source: https://skillmd.com/skills/glotoff/freecad-power-user13---1415# FreeCAD Power User Skill1617This skill equips AI agents to write idiomatic, high-performance, and crash-resilient Python code for FreeCAD.1819Based on the official [FreeCAD Documentation: Power users hub](https://github.com/FreeCAD/FreeCAD-documentation/blob/main/wiki/Power_users_hub.md).2021---2223## The FreeCAD Python Architecture2425FreeCAD has a dual-layer architecture:261. **`FreeCAD` (aliased as `App`)**: The core application module. Manages documents, geometric data, parametric dependency graph, and properties. Operates without any GUI (usable headlessly).272. **`FreeCADGui` (aliased as `Gui`)**: The presentation layer. Manages 3D scene representation (Coin3D/Pivy scenegraph), selection, Qt/PySide windows, task panels, and colors.2829```python30import FreeCAD as App31import FreeCADGui as Gui # Only import when GUI is present!32import Part33import math34```3536> [!IMPORTANT]37> **Headless Safety**:38> Never import `FreeCADGui` or call `Gui.` methods in scripts intended to run headlessly, in background workers, or in automated unit tests. FreeCAD can run with `freecadcmd` or `FreeCAD --console`. Check `App.GuiUp` before calling GUI code:39> ```python40> if App.GuiUp:41> import FreeCADGui as Gui42> Gui.ActiveDocument.ActiveView.fitAll()43> ```4445---4647## Essential Patterns & Idioms4849### 1. Document Management & Transactions50Always handle document creation and recomputation properly:5152```python53import FreeCAD as App5455# Get or create active document56doc = App.ActiveDocument57if not doc:58 doc = App.newDocument("ParametricDesign")5960# Wrap modifications in a transaction for clean Undo/Redo61doc.openTransaction("Generate Model")62try:63 # ... create or edit objects ...64 doc.recompute()65 doc.commitTransaction()66except Exception as e:67 doc.abortTransaction()68 raise e69```7071### 2. Geometry Creation: Part vs Sketcher7273#### Direct B-Rep with the `Part` Module (Fast, Precise, Headless-Friendly):74```python75import FreeCAD as App76import Part7778doc = App.ActiveDocument or App.newDocument()7980# Create geometric primitives in memory81box = Part.makeBox(100, 50, 20)82cylinder = Part.makeCylinder(10, 30, App.Vector(50, 25, 0), App.Vector(0, 0, 1))8384# Boolean Cut85result_shape = box.cut(cylinder)8687# Add to document88feature = doc.addObject("Part::Feature", "CutPlate")89feature.Shape = result_shape90doc.recompute()91```9293#### Parametric Sketch + PartDesign Body:94```python95import FreeCAD as App96import Sketcher9798doc = App.ActiveDocument or App.newDocument()99body = doc.addObject("PartDesign::Body", "Body")100sketch = body.newObject("Sketcher::SketchObject", "Sketch")101sketch.Support = (doc.XY_Plane, [""])102sketch.MapMode = "FlatFace"103104# Add geometry (Line segment)105sketch.addGeometry(Part.LineSegment(App.Vector(0, 0, 0), App.Vector(50, 0, 0)), False)106# Add constraints107sketch.addConstraint(Sketcher.Constraint('Coincident', 0, 1, -1, 1)) # Vertex 1 to origin108doc.recompute()109```110111### 3. Scripted Objects (`FeaturePython`)112A `FeaturePython` object creates a completely custom parametric object in the tree view that automatically recomputes whenever its properties change.113114**Core Rules**:115- Store logic in a python proxy class.116- Implement `execute(self, obj)` to rebuild geometry when properties change.117- Implement `onChanged(self, obj, prop)` to validate property updates.118- Store state in `App::Property*` fields, NOT inside `self` instance attributes unless implementing `__getstate__` / `__setstate__` (`dumps` and `loads`).119120---121122## Detailed References123124Consult the deep-dive manuals in `references/`:125- [Python API Full Catalog](./references/python_api_full_catalog.md): Complete index of all 58 Python API documentation pages, scripting modules (`App`, `Gui`, `Part`, `Draft`, `Mesh`, `Sketcher`, `TechDraw`, `FEM`), and classes.126- [Macro Recipes & Automation Catalog](./references/macros_recipes_catalog.md): Comprehensive catalog of 243 official macros, snippets, recipes, and automation scripts.127- [Python API Quick Reference](./references/python_api_reference.md): Document methods, property types, Vector/Matrix math, units, and transactions.128- [Topological Data Scripting](./references/topological_scripting.md): Deep dive into OpenCASCADE `Part::TopoShape`, vertices, wires, faces, shells, and boolean cuts.129- [Scripted Objects (`FeaturePython`)](./references/scripted_objects.md): Production template with properties, recompute lifecycle, ViewProvider, and serialization.130- [PySide & GUI Integration](./references/pyside_gui_integration.md): Custom commands, toolbars, modal dialogs, task panels, and Coin3D/Pivy scene graph.131- [Headless Execution & Batch Automation](./references/headless_execution.md): Running FreeCAD from CLI, Python virtualenvs, Docker, and MCP servers.132133### Offline Documentation Search Tool134To query methods, parameters, and macro examples across all 2,595+ documentation files:135```bash136python scripts/freecad_doc_query.py "<method_or_macro_name>"137```138139## Code Examples140141Check `examples/` for ready-to-run templates:142- [create_box_and_holes.py](./examples/create_box_and_holes.py): Parametric solid creation with holes.143- [custom_feature_python.py](./examples/custom_feature_python.py): Robust, full-featured scripted object class.144- [custom_macro.FCMacro](./examples/custom_macro.FCMacro): Standard macro with undo transaction and GUI feedback.