Skip to content
This repository has been archived by the owner on Feb 9, 2021. It is now read-only.

Latest commit

 

History

History
1096 lines (795 loc) · 38.2 KB

examples.rst

File metadata and controls

1096 lines (795 loc) · 38.2 KB

CadQuery Examples

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 !

Note

You may want to work through these examples by pasting the text into a scratchpad on the live website. If you do, make sure to take these steps so that they work:

  1. paste the content into the build() method, properly intented, and
  2. 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.

Note

We strongly recommend installing FreeCAD, and the cadquery-freecad-module, so that you can work along with these examples interactively. See installation for more info.

Warning

  • You have to have an svg capable browser to view these!

List of Examples

Simple Rectangular Plate

Just about the simplest possible example, a rectangular box

Api References

  • :pyWorkplane !
  • :pyWorkplane.box !

Plate with Hole

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 at the center of the face. The default hole depth is through the entire part.

Api References

  • :pyWorkplane.hole !
  • :pyWorkplane.box
  • :pyWorkplane.box

An extruded prismatic solid

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.

Api References

  • :pyWorkplane.circle !
  • :pyWorkplane.rect !
  • :pyWorkplane.extrude !
  • :pyWorkplane

Building Profiles using lines and arcs

Sometimes you need to build complex profiles using lines and arcs. This example builds a prismatic solid from 2-d operations.

2-d operations maintain a current point, which is initially at the origin. Use close() to finish a closed curve.

Api References

  • :pyWorkplane.threePointArc !
  • :pyWorkplane.lineTo !
  • :pyWorkplane.extrude
  • :pyWorkplane

Moving The Current working point

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.

Api References

  • :pyWorkplane.center !
  • :pyWorkplane
  • :pyWorkplane.circle
  • :pyWorkplane.rect
  • :pyWorkplane.extrude

Using Point Lists

Sometimes you need to create a number of features at various locations, and using :pyWorkplane.center is too cumbersome.

You can use a list of points to construct multiple objects at once. Most construction methods, like :pyWorkplane.circle and :pyWorkplane.rect, will operate on multiple points if they are on the stack

Api References

  • :pyWorkplane.points !
  • :pyWorkplane
  • :pyWorkplane.circle
  • :pyWorkplane.extrude

Polygons

You can create polygons for each stack point if you would like. Useful in 3d printers whos firmware does not correct for small hole sizes.

Api References

  • :pyWorkplane.polygon !
  • :pyWorkplane.pushPoints
  • :pyWorkplane.box

Polylines

:pyWorkplane.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.

Api References

  • :pyWorkplane.polyline !
  • :pyWorkplane
  • :pyWorkplane.mirrorY
  • :pyWorkplane.extrude

Defining an Edge with a Spline

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

Api References

  • :pyWorkplane.spline !
  • :pyWorkplane
  • :pyWorkplane.close
  • :pyWorkplane.lineTo
  • :pyWorkplane.extrude

Mirroring Symmetric Geometry

You can mirror 2-d geometry when your shape is symmetric. In this example we also introduce horizontal and vertical lines, which make for slightly easier coding.

Api References

  • :pyWorkplane.hLine !
  • :pyWorkplane.vLine !
  • :pyWorkplane.hLineTo !
  • :pyWorkplane.mirrorY !
  • :pyWorkplane.mirrorX !
  • :pyWorkplane
  • :pyWorkplane.extrude

Mirroring 3D Objects

Api References

  • :pyWorkplane.moveTo
  • :pyWorkplane.lineTo
  • :pyWorkplane.threePointArc
  • :pyWorkplane.extrude
  • :pyWorkplane.mirror
  • :pyWorkplane.union
  • :pyCQ.rotate

Creating Workplanes on Faces

This example shows how to locate a new workplane on the face of a previously created feature.

Note

Using workplanes in this way are a key feature of CadQuery. Unlike 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

The :pyWorkplane.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 the origin of new workplanes are located at the center of a face by default.

Api References

  • :pyWorkplane.faces !
  • :pyStringSyntaxSelector !
  • selector_reference !
  • :pyWorkplane.workplane
  • :pyWorkplane.box
  • :pyWorkplane

Locating a Workplane on a vertex

Normally, the :pyWorkplane.workplane method requires a face to be selected. But if a vertex is selected immediately after a face, :pyWorkplane.workplane 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 :pyWorkplane.cutThruAll, which makes a cut through the entire part, no matter how deep the part is

Api References

  • :pyWorkplane.cutThruAll !
  • selector_reference !
  • :pyWorkplane.vertices !
  • :pyWorkplane.box
  • :pyWorkplane
  • :pyStringSyntaxSelector !

Offset Workplanes

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!

Api References

  • :pyWorkplane.extrude
  • selector_reference !
  • :pyWorkplane.box
  • :pyWorkplane

Rotated Workplanes

You can create a rotated work plane by specifying angles of rotation relative to another workplane

Api References

  • :pyWorkplane.transformed !
  • :pyWorkplane.box
  • :pyWorkplane.rect
  • :pyWorkplane.faces

Using construction Geometry

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 Construction Geometry

In the example below, a rectangle is drawn, and its vertices are used to locate a set of holes.

Api References

  • :pyWorkplane.rect (forConstruction=True)
  • selector_reference
  • :pyWorkplane.workplane
  • :pyWorkplane.box
  • :pyWorkplane.hole
  • :pyWorkplane

Shelling To Create Thin features

Shelling converts a solid object into a shell of uniform thickness. To shell an object, one or more faces are removed, and then the inside of the solid is 'hollowed out' to make the shell.

Api References

  • :pyWorkplane.shell !
  • :pyWorkplane.box
  • :pyWorkplane.faces
  • :pyWorkplane

Making Lofts

A loft is a solid swept through a set of wires. This example creates lofted section between a rectangle and a circular section.

Api References

  • :pyWorkplane.loft !
  • :pyWorkplane.box
  • :pyWorkplane.faces
  • :pyWorkplane.circle
  • :pyWorkplane.rect

Making Counter-bored and counter-sunk holes

Counterbored and countersunk holes are so common that CadQuery creates macros to create them in a single step.

Similar to :pyWorkplane.hole , these functions operate on a list of points as well as a single point.

Api References

  • :pyWorkplane.cboreHole !
  • :pyWorkplane.cskHole !
  • :pyWorkplane.box
  • :pyWorkplane.rect
  • :pyWorkplane.workplane
  • :pyWorkplane.vertices
  • :pyWorkplane.faces
  • :pyWorkplane

Rounding Corners with Fillet

Filleting is done by selecting the edges of a solid, and using the fillet function.

Here we fillet all of the edges of a simple plate.

Api References

  • :pyWorkplane.fillet !
  • :pyWorkplane.box
  • :pyWorkplane.edges
  • :pyWorkplane

A Parametric Bearing Pillow Block

Combining a few basic functions, its possible to make a very good parametric bearing pillow block, with just a few lines of code.

Splitting an Object

You can split an object using a workplane, and retain either or both halves

Api References

  • :pyWorkplane.split !
  • :pyWorkplane.box
  • :pyWorkplane.circle
  • :pyWorkplane.cutThruAll
  • :pyWorkplane.workplane
  • :pyWorkplane

The Classic OCC Bottle

CadQuery is based on the OpenCascade.org (OCC) modeling Kernel. Those who are familiar with OCC know about the famous 'bottle' example. http://www.opencascade.org/org/gettingstarted/appli/

A pythonOCC version is listed here

http://code.google.com/p/pythonocc/source/browse/trunk/src/examples/Tools/InteractiveViewer/scripts/Bottle.py?r=1046

Of course one difference between this sample and the OCC version is the length. This sample is one of the longer ones at 13 lines, but that's very short compared to the pythonOCC version, which is 10x longer!

Api References

  • :pyWorkplane.extrude
  • :pyWorkplane.mirrorX
  • :pyWorkplane.threePointArc
  • :pyWorkplane.workplane
  • :pyWorkplane.vertices
  • :pyWorkplane.vLine
  • :pyWorkplane.faces
  • :pyWorkplane

A Parametric Enclosure

Api References

  • :pyWorkplane.circle
  • :pyWorkplane.rect
  • :pyWorkplane.extrude
  • :pyWorkplane.box
  • :pyCQ.all
  • :pyCQ.faces
  • :pyCQ.vertices
  • :pyCQ.edges
  • :pyCQ.workplane
  • :pyWorkplane.fillet
  • :pyWorkplane.cut
  • :pyWorkplane.combineSolids
  • :pyWorkplane.rotateAboutCenter
  • :pyWorkplane.cboreHole
  • :pyWorkplane.cskHole
  • :pyWorkplane.hole

Lego Brick

This script will produce any size regular rectangular Lego(TM) brick. Its only tricky because of the logic regarding the underside of the brick.

Braille Example

Panel With Various Connector Holes