Free Cad
Free Cad
• From the Python console, or from custom scripts, you can control FreeCAD
and make it perform very complex operations.
• There are several ways to use Python in FreeCAD:
– From the FreeCAD Python interpreter, where you can issue commands
in a "command line"-style interface.
– From macros, which are a convenient way to quickly add a missing tool
to the FreeCAD interface.
– Report view.
• There are two ways to write Python code in FreeCAD.
• In the console you write Python commands one by one, executing them
by pressing Enter, while macros can contain more complex code made up
of several lines, executed only when the macro is executed.
Python console
• To create a new empty document, type the following command in the
Python Console.
doc=FreeCAD.newDocument()
• Usually names that begin with a capital letter are attributes, they contain
a value, while names that begin with a lower case letter are functions (also
called methods), they "do something". Names that begin with an
underscore are usually there for the internal working of the module, and
you shouldn't care about them.
box=doc.addObject("Part::Box", "Box1")
doc.recompute()
box.Height
box.Height = 5
• Vectors
box.Placement
box.Placement.Base
box.Placement.Base = sumvec
The geometric properties of the cube, such as its dimensions, position, etc.
are stored in the Object. While its visual properties, such as its color, line
thickness, etc. are stored in the ViewObject.
vo = box.ViewObject
vo.Transparency = 80
vo.hide()
vo.show()
Modules
• The different FreeCAD modules are not automatically loaded in the Python
console. This is to avoid having a very slow startup. Modules are loaded
only when you need them.
import Part
Part
Mesh
• Meshes are a very simple kind of 3D object, used for example
by Sketchup, Blender and 3D Studio Max. They are composed of 3
elements: points (also called vertices), lines (also called edges) and faces.
• Mesh objects and FreeCAD objects are different things. You can see the
FreeCAD object as a container for a Mesh object. So in order to add a
mesh object to FreeCAD, we must first create a FreeCAD object and a
Mesh object, then add the Mesh object to the FreeCAD object:
doc=FreeCAD.newDocument()
import Mesh
mymesh = Mesh.createSphere()
mymesh.Facets
mymesh.Points
meshobj.Mesh = mymesh
doc.recompute()
Part
• The Part Module is the most powerful module in the whole of FreeCAD. It
allows you to create and manipulate BRep objects. BRep stands for
"Boundary Representation". A BRep object is defined by surfaces that
enclose and define an inner volume.
• The Part module works the same way as the Mesh module: You create a
FreeCAD object, a Part object, then add the Part object to the FreeCAD
object:
doc=FreeCAD.newDocument()
import Part
myshape = Part.makeSphere(10)
myshape.Volume
myshape.Area
shapeobj.Shape = myshape
doc.recompute()
#Part.show(myshape)
• FreeCAD features many more modules, such as Sketcher and Draft that
also create Part objects. These modules add additional parameters to the
objects created, or even implement a whole new way to handle the Part
geometry in them. Our box example above is a perfect example of a
parametric object. All you need to define the box is to specify the
parameters height, width and length. Based on those, the object will
automatically calculate its Part shape. FreeCAD allows you to create such
objects in Python.
doc=FreeCAD.newDocument()
import Draft
rec = Draft.makeRectangle(5, 2)
mvec = FreeCAD.Vector(4, 4, 0)
Draft.move(rec, mvec)
Draft.move(box, mvec)
Interface
• The FreeCAD user interface is made with Qt, a powerful graphical
interface system, responsible for drawing and handling all the controls,
menus, toolbars and buttons around the 3D view. Qt provides a
module, PySide, which allows Python to access and modify Qt interfaces
such as FreeCAD's.
import Part
V1 = Base.Vector(0, 10, 0)
V2 = Base.Vector(30, 10, 0)
V3 = Base.Vector(30, -10, 0)
V4 = Base.Vector(0, -10, 0)
# Create Arcs
VC1 = Base.Vector(-10, 0, 0)
VC2 = Base.Vector(40, 0, 0)
L1 = Part.LineSegment(V1, V2)
L2 = Part.LineSegment(V3, V4)
# Combine
W = Part.Wire(S1.Edges)
P = W.extrude(Base.Vector(0, 0, 10))
Show it all
Part.show(P)
Make methods
• You can easily create basic topological objects with the make...() methods
from the Part Module:
Part.show(b)
• makeBox(l, w, h) Makes a box located in p and pointing into the direction d
with the dimensions (l,w,h).
• makeCone(radius1, radius2, height) Makes a cone with the given radii and
height.
• makeLine((x1, y1, z1), (x2, y2, z2)) Makes a line from two points.
edge.Length
edge.CenterOfMass
Wire
• A wire is a multi-edge line and can be created from a list of edges or even a list
of wires:
edge1 = Part.makeLine((0, 0, 0), (10, 0, 0))
edge2 = Part.makeLine((10, 0, 0), (10, 10, 0))
wire1 = Part.Wire([edge1, edge2])
edge3 = Part.makeLine((10, 10, 0), (0, 10, 0))
edge4 = Part.makeLine((0, 10, 0), (0, 0, 0))
wire2 = Part.Wire([edge3, edge4])
wire3 = Part.Wire([wire1, wire2])
wire3.Edges
Part.show(wire3)
• wire3.Length
• wire3.CenterOfMass
• wire3.isClosed()
• wire2.isClosed()
Face
• Only faces created from closed wires will be valid.
face = Part.Face(wire3)
face.Area
face.CenterOfMass
face.Length
face.isValid()
sface = Part.Face(wire2)
face.isValid()
• Circle
circle = Part.makeCircle(10)
circle.Curve
• Arc
arc
arc_edge = arc.toShape()
Part.show(arc_edge)
• Arc as part of circle
arc = Part.Arc(circle,0,pi)
• Polygon
• Ellipse
Part.show(eli.toShape())
• torus = Part.makeTorus(10, 2)
• sphere = Part.makeSphere(10)
• myShape = Part.makeBox(2, 2, 2)
• myShape.translate(Base.Vector(2, 0, 0))
To rotate a shape, you need to specify the rotation center, the axis, and the
rotation angle:
The above code will rotate the shape 180 degrees around the Z Axis.
• Extrude
circle = Part.makeCircle(10)
wire = Part.Wire(circle)
disc = Part.Face(wire)
fuse = cylinder1.fuse(cylinder2)
• Subtract
diff = cylinder.cut(sphere)
• Intersection
common = cylinder1.common(cylinder2)
Ex Selection
import Part
Gui.SendMsgToActiveView("ViewFit")
for o in Gui.Selection.getSelectionEx():
print(o.ObjectName)
for s in o.SubElementNames:
print("name: ", s)
for s in o.SubObjects:
print("object: ", s)
length = 0.0
for o in Gui.Selection.getSelectionEx():
for s in o.SubObjects:
length += s.Length
aPnt1=Base.Vector(-myWidth / 2., 0, 0)
aPnt5=Base.Vector(myWidth / 2., 0, 0)
aArcOfCircle = Part.Arc(aPnt2, aPnt3, aPnt4)
aSegment1=Part.LineSegment(aPnt1, aPnt2)
aSegment2=Part.LineSegment(aPnt4, aPnt5)
aEdge1=aSegment1.toShape()
aEdge2=aArcOfCircle.toShape()
aEdge3=aSegment2.toShape()
aMirroredWire=aWire.copy()
aMirroredWire.transformShape(aTrsf)
myWireProfile=Part.Wire([aWire, aMirroredWire])
myFaceProfile=Part.Face(myWireProfile)
aPrismVec=Base.Vector(0, 0, myHeight)
myBody=myFaceProfile.extrude(aPrismVec)
myBody=myBody.makeFillet(myThickness / 12.0, myBody.Edges)
neckLocation=Base.Vector(0, 0, myHeight)
neckNormal=Base.Vector(0, 0, 1)
myNeckRadius = myThickness / 4.
myBody = myBody.fuse(myNeck)
return myBody
el = makeBottleTut()
Part.show(el)
Ex. Pierced Box
import Part, math
size = 10
poly = Part.makePolygon([(0, 0, 0), (size, 0, 0), (size, 0, size), (0, 0, size), (0, 0,
0)])
face1 = Part.Face(poly)
face2 = Part.Face(poly)
face3 = Part.Face(poly)
face4 = Part.Face(poly)
face5 = Part.Face(poly)
face6 = Part.Face(poly)
myMat = Base.Matrix()
myMat.rotateZ(math.pi / 2)
face2.transformShape(myMat)
face2.translate(Base.Vector(size, 0, 0))
myMat.rotateZ(math.pi / 2)
face3.transformShape(myMat)
face3.translate(Base.Vector(size, size, 0))
myMat.rotateZ(math.pi / 2)
face4.transformShape(myMat)
myMat = Base.Matrix()
myMat.rotateX(-math.pi / 2)
face5.transformShape(myMat)
face6.transformShape(myMat)
face6.translate(Base.Vector(0, 0, size))
mySolid = Part.makeSolid(myShell)
cut_part = mySolid.cut(myCyl)
Part.show(cut_part)
• To save
import Part
s.exportStep("test.stp")
• To open
import Part
s = Part.Shape()
s.read("test.stp")
Macros
• A macro is a Python script that can be added to a toolbar and launched via
a mouse click.