Interesting Tech Projects
3D Printing
Python CAD Scripting
Jun 24th
FreeCAD supports Python as a scripting language which allows the creation of 3D parts based on variables. This is great for building libraries which support customizable part creation.
As a first attempt I created the following test script. When executed in FreeCAD 0.13 it creates a rod 10mm x 10mm x 50mm with a notch in it. It then removes a cube measuring 4mm x 4mm x 4mm from one end.
Here is what the hierarchy looks like:
And here is the script:
import FreeCAD, FreeCADGui, Part
App = FreeCAD
doc = App.activeDocument()
rodbasepts = list()
rodbasepts.append(App.Vector(0, 0, 0))
rodbasepts.append(App.Vector(0, 10, 0))
rodbasepts.append(App.Vector(10, 10, 0))
rodbasepts.append(App.Vector(10, 0, 0))
rodbasepts.append(App.Vector(6, 0, 0))
rodbasepts.append(App.Vector(6, 2, 0))
rodbasepts.append(App.Vector(4, 2, 0))
rodbasepts.append(App.Vector(4, 0, 0))
rodbasepts.append(App.Vector(0, 0, 0))
rodbase = Part.makePolygon(rodbasepts)
rodbasefilled = Part.Face(rodbase)
rod = rodbasefilled.extrude(App.Vector(0, 0, 50))
#rod.Placement = App.Placement(App.Vector(10, 0, 0), App.Rotation(0, 0, 0, 1))
rodpart = doc.addObject("Part::Feature", "rod")
rodpart.Shape = rod
cubebasepts = list()
cubebasepts.append(App.Vector(0, 0, 0))
cubebasepts.append(App.Vector(0, 4, 0))
cubebasepts.append(App.Vector(4, 4, 0))
cubebasepts.append(App.Vector(4, 0, 0))
cubebasepts.append(App.Vector(0, 0, 0))
cubebase = Part.makePolygon(cubebasepts)
cubebasefilled = Part.Face(cubebase)
cube = cubebasefilled.extrude(App.Vector(0, 0, 4))
cubepart = doc.addObject("Part::Feature", "cube")
cubepart.Shape = cube
cut = doc.addObject("Part::Cut", 'Rod minus cube')
cut.Base = rodpart
cut.Tool = cubepart
rodpart.ViewObject.hide()
cubepart.ViewObject.hide()
doc.recompute()
To use create a new Part and then copy and paste the script into the Python Console inside FreeCAD.

