Interesting Tech Projects
Python CAD Scripting
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.
Print article | This entry was posted by Andy on June 24, 2012 at 3:25 pm, and is filed under 3D Printing, Software Engineering. Follow any responses to this post through RSS 2.0. Both comments and pings are currently closed. |
Comments are closed.