The examples on this page can help you learn how to build objects with CadQuery.
They are organized from simple to complex, so working through them in order is the best way to absorb them.
Each example lists the API elements used in the example for easy reference. Items introduced in the example are marked with a !
You may want to work through these examples by pasting the text into a code editor in a CadQuery . If you do, make sure to take these steps so that they work:
paste the content into the build() method, properly indented, and
add the line ‘return result’ at the end. The samples below are autogenerated, but they use a different syntax than the models on the website need to be.
You have to have an svg capable browser to view these!
List of Examples
Just about the simplest possible example, a rectangular box
result = cadquery.Workplane("front").box(2.0, 2.0, 0.5)
A rectangular box, but with a hole added.
“>Z” selects the top most face of the resulting box. The hole is located in the center because the default origin of a working plane is the projected origin of the last Workplane, the last Workplane having origin at (0,0,0) the projection is at the center of the face. The default hole depth is through the entire part.
# The dimensions of the box. These can be modified rather than changing the # object's code directly. length = 80.0 height = 60.0 thickness = 10.0 center_hole_dia = 22.0 # Create a box based on the dimensions above and add a 22mm center hole result = (cq.Workplane("XY").box(length, height, thickness) .faces(">Z").workplane().hole(center_hole_dia))
Build a prismatic solid using extrusion. After a drawing operation, the center of the previous object is placed on the stack, and is the reference for the next operation. So in this case, the rect() is drawn centered on the previously draw circle.
By default, rectangles and circles are centered around the previous working point.
result = cq.Workplane("front").circle(2.0).rect(0.5, 0.75).extrude(0.5)
Sometimes you need to build complex profiles using lines and arcs. This example builds a prismatic solid from 2D operations.
2D operations maintain a current point, which is initially at the origin. Use close() to finish a closed curve.
result = (cq.Workplane("front").lineTo(2.0, 0).lineTo(2.0, 1.0).threePointArc((1.0, 1.5), (0.0, 1.0)) .close().extrude(0.25))
In this example, a closed profile is required, with some interior features as well.
This example also demonstrates using multiple lines of code instead of longer chained commands, though of course in this case it was possible to do it in one long line as well.
A new work plane center can be established at any point.
result = cq.Workplane("front").circle(3.0) # current point is the center of the circle, at (0, 0) result = result.center(1.5, 0.0).rect(0.5, 0.5) # new work center is (1.5, 0.0) result = result.center(-1.5, 1.5).circle(0.25) # new work center is (0.0, 1.5). # The new center is specified relative to the previous center, not global coordinates! result = result.extrude(0.25)
Sometimes you need to create a number of features at various locations, and using
is too cumbersome.
You can use a list of points to construct multiple objects at once. Most construction methods,
Workplane.rect(), will operate on multiple points if they are on the stack
r = cq.Workplane("front").circle(2.0) # make base r = r.pushPoints([(1.5, 0), (0, 1.5), (-1.5, 0), (0, -1.5)]) # now four points are on the stack r = r.circle(0.25) # circle will operate on all four points result = r.extrude(0.125) # make prism
You can create polygons for each stack point if you would like. Useful in 3d printers whose firmware does not correct for small hole sizes.
result = (cq.Workplane("front").box(3.0, 4.0, 0.25).pushPoints([(0, 0.75), (0, -0.75)]) .polygon(6, 1.0).cutThruAll())
Workplane.polyline() allows creating a shape from a large number of chained points connected by lines.
This example uses a polyline to create one half of an i-beam shape, which is mirrored to create the final profile.
(L, H, W, t) = (100.0, 20.0, 20.0, 1.0) pts = [ (0, H/2.0), (W/2.0, H/2.0), (W/2.0, (H/2.0 - t)), (t/2.0, (H/2.0 - t)), (t/2.0, (t - H/2.0)), (W/2.0, (t - H/2.0)), (W/2.0, H/-2.0), (0, H/-2.0) ] result = cq.Workplane("front").polyline(pts).mirrorY().extrude(L)
This example defines a side using a spline curve through a collection of points. Useful when you have an edge that needs a complex profile
s = cq.Workplane("XY") sPnts = [ (2.75, 1.5), (2.5, 1.75), (2.0, 1.5), (1.5, 1.0), (1.0, 1.25), (0.5, 1.0), (0, 1.0) ] r = s.lineTo(3.0, 0).lineTo(3.0, 1.0).spline(sPnts, includeCurrent=True).close() result = r.extrude(0.5)
You can mirror 2D geometry when your shape is symmetric. In this example we also introduce horizontal and vertical lines, which make for slightly easier coding.
r = cq.Workplane("front").hLine(1.0) # 1.0 is the distance, not coordinate r = r.vLine(0.5).hLine(-0.25).vLine(-0.25).hLineTo(0.0) # hLineTo allows using xCoordinate not distance result = r.mirrorY().extrude(0.25) # mirror the geometry and extrude
result0 = (cadquery.Workplane("XY") .moveTo(10, 0) .lineTo(5, 0) .threePointArc((3.9393, 0.4393), (3.5, 1.5)) .threePointArc((3.0607, 2.5607), (2, 3)) .lineTo(1.5, 3) .threePointArc((0.4393, 3.4393), (0, 4.5)) .lineTo(0, 13.5) .threePointArc((0.4393, 14.5607), (1.5, 15)) .lineTo(28, 15) .lineTo(28, 13.5) .lineTo(24, 13.5) .lineTo(24, 11.5) .lineTo(27, 11.5) .lineTo(27, 10) .lineTo(22, 10) .lineTo(22, 13.2) .lineTo(14.5, 13.2) .lineTo(14.5, 10) .lineTo(12.5, 10) .lineTo(12.5, 13.2) .lineTo(5.5, 13.2) .lineTo(5.5, 2) .threePointArc((5.793, 1.293), (6.5, 1)) .lineTo(10, 1) .close()) result = result0.extrude(100) result = result.rotate((0, 0, 0), (1, 0, 0), 90) result = result.translate(result.val().BoundingBox().center.multiply(-1)) mirXY_neg = result.mirror(mirrorPlane="XY", basePointVector=(0, 0, -30)) mirXY_pos = result.mirror(mirrorPlane="XY", basePointVector=(0, 0, 30)) mirZY_neg = result.mirror(mirrorPlane="ZY", basePointVector=(-30, 0, 0)) mirZY_pos = result.mirror(mirrorPlane="ZY", basePointVector=(30, 0, 0)) result = result.union(mirXY_neg).union(mirXY_pos).union(mirZY_neg).union(mirZY_pos)
This example shows how you can mirror about a selected face. It also shows how the resulting mirrored object can be unioned immediately with the referenced mirror geometry.
result = (cq.Workplane("XY") .line(0, 1) .line(1, 0) .line(0, -.5) .close() .extrude(1)) result = result.mirror(result.faces(">X"), union=True)
This example shows how to locate a new workplane on the face of a previously created feature.
Using workplanes in this way are a key feature of CadQuery. Unlike a typical 3d scripting language, using work planes frees you from tracking the position of various features in variables, and allows the model to adjust itself with removing redundant dimensions
Workplane.faces() method allows you to select the faces of a resulting solid. It
accepts a selector string or object, that allows you to target a single face, and make a workplane
oriented on that face.
Keep in mind that by default the origin of a new workplane is calculated by forming a plane from the
selected face and projecting the previous origin onto that plane. This behaviour can be changed
through the centerOption argument of
result = cq.Workplane("front").box(2, 3, 0.5) # make a basic prism result = result.faces(">Z").workplane().hole(0.5) # find the top-most face and make a hole
Workplane.workplane() method requires a face to be selected. But if a vertex
is selected immediately after a face,
Workplane.workplane() with the centerOption
argument set to CenterOfMass will locate the workplane on the face, with the origin at the vertex
instead of at the center of the face
The example also introduces
Workplane.cutThruAll(), which makes a cut through the entire
part, no matter how deep the part is.
result = cq.Workplane("front").box(3,2, 0.5) # make a basic prism result = result.faces(">Z").vertices("<XY").workplane(centerOption="CenterOfMass") # select the lower left vertex and make a workplane result = result.circle(1.0).cutThruAll() # cut the corner out
Workplanes do not have to lie exactly on a face. When you make a workplane, you can define it at an offset from an existing face.
This example uses an offset workplane to make a compound object, which is perfectly valid!
result = cq.Workplane("front").box(3, 2, 0.5) # make a basic prism result = result.faces("<X").workplane(offset=0.75) # workplane is offset from the object surface result = result.circle(1.0).extrude(0.5) # disc
An existing CQ object can copy a workplane from another CQ object.
result = (cq.Workplane("front").circle(1).extrude(10) # make a cylinder # We want to make a second cylinder perpendicular to the first, # but we have no face to base the workplane off .copyWorkplane( # create a temporary object with the required workplane cq.Workplane("right", origin=(-5, 0, 0)) ).circle(1).extrude(10))
You can create a rotated work plane by specifying angles of rotation relative to another workplane
result = (cq.Workplane("front").box(4.0, 4.0, 0.25).faces(">Z").workplane() .transformed(offset=cq.Vector(0, -1.5, 1.0),rotate=cq.Vector(60, 0, 0)) .rect(1.5, 1.5, forConstruction=True).vertices().hole(0.25))
You can draw shapes to use the vertices as points to locate other features. Features that are used to
locate other features, rather than to create them, are called
In the example below, a rectangle is drawn, and its vertices are used to locate a set of holes.
result = (cq.Workplane("front").box(2, 2, 0.5).faces(">Z").workplane() .rect(1.5, 1.5, forConstruction=True).vertices().hole(0.125))
Shelling converts a solid object into a shell of uniform thickness.
To shell an object and ‘hollow out’ the inside pass a negative thickness parameter
Workplane.shell() method of a shape.
result = cq.Workplane("front").box(2, 2, 2).shell(-0.1)
A positive thickness parameter wraps an object with filleted outside edges and the original object will be the ‘hollowed out’ portion.
result = cq.Workplane("front").box(2, 2, 2).shell(0.1)
Use face selectors to select a face to be removed from the resulting hollow shape.
result = cq.Workplane("front").box(2, 2, 2).faces("+Z").shell(0.1)
Multiple faces can be removed using more complex selectors.
result = ( cq.Workplane("front") .box(2, 2, 2) .faces("+Z or -X or +X") .shell(0.1) )