| name | freecad-automation |
| description | Automate FreeCAD operations using Python scripting within FreeCAD's environment. Use for parametric modeling, macros, and batch processing in the FreeCAD application. Requires FreeCAD installation with Python console access. |
FreeCAD Automation
Automate FreeCAD through its Python scripting interface.
Environment Setup
FreeCAD scripts run inside FreeCAD's Python environment:
import FreeCAD
import Part
import Draft
# Create new document
doc = FreeCAD.newDocument("MyProject")
Creating Geometry
import Part
# Create box
box = Part.makeBox(10, 10, 5)
# Create cylinder
cylinder = Part.makeCylinder(5, 20)
# Create sphere
sphere = Part.makeSphere(10)
# Add to document
Part.show(box)
Boolean Operations
import Part
box = Part.makeBox(20, 20, 20)
cyl = Part.makeCylinder(5, 30)
# Subtract
result = box.cut(cyl)
# Union
result = box.fuse(cyl)
# Intersect
result = box.common(cyl)
Part.show(result)
Transformations
import FreeCAD
# Translation
shape.translate(FreeCAD.Vector(10, 0, 0))
# Rotation
shape.rotate(FreeCAD.Vector(0,0,0), FreeCAD.Vector(0,0,1), 45)
# Create placement
placement = FreeCAD.Placement(
FreeCAD.Vector(10, 20, 0),
FreeCAD.Rotation(FreeCAD.Vector(0,0,1), 45)
)
obj.Placement = placement
Export
import Part
import Mesh
# Export to STEP
shape.exportStep("output.step")
# Export to STL
mesh = doc.addObject("Mesh::Feature", "Mesh")
mesh.Mesh = Mesh.Mesh(shape.tessellate(0.1))
mesh.Mesh.write("output.stl")
Running Macros
# Command line execution
freecad -c script.py
# Or from Python console in FreeCAD
exec(open("script.py").read())