Skip to content

Instantly share code, notes, and snippets.

@medicationforall
Created September 1, 2022 12:10
Show Gist options
  • Save medicationforall/ecb256d1f37a0c64ba4eac9d0f80e3fe to your computer and use it in GitHub Desktop.
Save medicationforall/ecb256d1f37a0c64ba4eac9d0f80e3fe to your computer and use it in GitHub Desktop.

Cadquery Workplane and the Stack

Goal of the video

Go through workplane documentation and explain it's concepts.

Pre-requisites


Instantiation

Create a 2d workplane on the XY axis with one point starting at 0,0,0

result = cq.Workplane("XY")

01

Let's append a sphere at the center point

result = cq.Workplane("XY")
result = result.sphere(1)

02
The circle is placed centered relative to the initial point at 0,0,0.

Center

Workplane.center

If we move where the center point on the workplane resides the circle will be placed at the new destination.

result = cq.Workplane("XY").center(3,3)
result = result.sphere(1)

03


The Stack

By default a workplane has one point on the stack, and all operations are applied against that point.

To prove that out let's add a box to the workplane.

result = cq.Workplane("XY").center(3,2)
result = result.sphere(1).box(1,1,2.5)

04

Because the workplane is in relative dimensions if we change initial axis and the created solids are rotated from our perspective but really the workplane has now shifted.

result = cq.Workplane("XZ").center(3,2)
result = result.sphere(1).box(1,1,2.5)

05

Chaining

Notice that we are calling box off of sphere?
This is chaining

we could also rewrite our code look like this.

result = cq.Workplane("YZ").center(3,2).sphere(1).box(1,1,2.5)

OR

result = (
    cq.Workplane("YZ")
    .center(3,2)
    .sphere(1)
    .box(1,1,2.5)
    )

Lets move the box independent of the sphere.

result = (
    cq.Workplane("YZ")
    .center(3,2)
    .sphere(1)
    .workplane(offset=2)
    .box(1,1,2.5)
    )

06

calling workplane off of the sphere; makes a new copy of the existing workplane and sets the original workplane as the new workplane's parent.

We then offset the new workplanes origin point height by 2mm on the z axis which is being tranlated to the y axis in the final output.

Expanding the chain

  1. Move back to the XY axis on the initial workplane
  2. Lets add a third workplane with a box
result = (
    cq.Workplane("XY")
    .center(3,2)
    .sphere(1)
    .workplane(offset=2)
    .box(1,1,2.5)
    .workplane(offset=4.5)
    .box(2,2,2.5)
    )

07

  • Tag the middle box as center.
  • We'll then refer back to the center workplane and add a box.
result = (
    cq.Workplane("XY")
    .center(3,2)
    .sphere(1)
    .workplane(offset=2)
    .box(1,1,2.5).tag("center")
    .box(1,1,2.5)
    .workplane(offset=4.5)
    .box(2,2,2.5)
    .workplaneFromTagged("center")
    .box(1,5,1)
    )

09

The return from the last box operation in the chain is the center tagged workplane, and to prove that lets move the origin point on the center plane and add another box.

result = (
    cq.Workplane("XY")
    .center(3,2)
    .sphere(1)
    .workplane(offset=2)
    .box(1,1,2.5).tag("center")
    .workplane(offset=4.5)
    .box(2,2,2.5)
    .workplaneFromTagged("center")
    .box(1,5,1)
    .center(5,5)
    .box(5,1,1)
    )

10

You can traverse up and down the stack relative to any child on the stack.
Stack Methods

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment