Foundation (Python)ReferenceOperationsMove, copy & delete bodies

Move, copy & delete bodies

Renders the 3D model in your browser. The first run downloads the Python runtime (≈30 MB).
from cadbuildr.foundation import *


class MovedPegs(Part):
    """MoveBody moves (and rotates) a solid. Union a body with a moved copy of
    itself to duplicate it: here a peg, a copy 30 mm to the right, and a copy
    tipped over 90° about the Y axis."""

    def __init__(self):
        s = Sketch(self.xy())
        peg = Extrusion(Circle(s.origin, 5), 20)
        shifted = MoveBody(peg, Point3D(30, 0, 0))
        tipped = MoveBody(
            peg,
            Point3D(60, 0, 5),
            rotation_angle=90,
            rotation_axis=Point3D(0, 1, 0),
        )
        self.add_operation(BooleanOperation(peg, [shifted, tipped], BooleanOp.UNION))
        self.translate([-40, 0, 0])  # centre the three pegs in the viewer
        self.paint("tan")


show(MovedPegs())

MoveBody(shape, translation, rotation_angle=…, rotation_axis=…, rotation_origin=…) rotates a solid (degrees, about an axis through rotation_origin) and then translates it.

There is no separate “copy” operation: union a body with moved versions of itself, as the pegs above do with BooleanOperation(peg, [shifted, tipped], BooleanOp.UNION).

from cadbuildr.foundation import *


class MovedPegs(Part):
    """MoveBody moves (and rotates) a solid. Union a body with a moved copy of
    itself to duplicate it: here a peg, a copy 30 mm to the right, and a copy
    tipped over 90° about the Y axis."""

    def __init__(self):
        s = Sketch(self.xy())
        peg = Extrusion(Circle(s.origin, 5), 20)
        shifted = MoveBody(peg, Point3D(30, 0, 0))
        tipped = MoveBody(
            peg,
            Point3D(60, 0, 5),
            rotation_angle=90,
            rotation_axis=Point3D(0, 1, 0),
        )
        self.add_operation(BooleanOperation(peg, [shifted, tipped], BooleanOp.UNION))
        self.translate([-40, 0, 0])  # centre the three pegs in the viewer
        self.paint("tan")


show(MovedPegs())

Delete & keep bodies

A part can hold several separate bodies: features that don’t touch stay separate solids, and a cut can split one body in two. Pick which survive explicitly (Onshape: Delete part):

  • self.delete_bodies(bodies) (DeleteBodies) removes every body of the part built so far that shares volume with one of bodies;
  • self.keep_bodies(bodies) (KeepBodies) keeps only those bodies.

bodies is one operation or a list — usually features added to the part earlier (the value self.extrude(...) returns). A selector does not have to be in the part: any solid sharing volume with a body selects it, which is how the example below picks one half of the sawn ring. Bodies that a later feature merged are one body: deleting it deletes the whole merged solid. Later operations still apply on top of what is left.

A selector that touches no body, or a delete that would leave the part empty, is reported as an error.

Renders the 3D model in your browser. The first run downloads the Python runtime (≈30 MB).
from cadbuildr.foundation import *


class HalfClamp(Part):
    """A multi-body part: a ring sawn into two halves, plus a construction
    block. DeleteBodies drops the block; KeepBodies keeps the +Y half only,
    picked by a small marker solid that sits inside it."""

    def __init__(self, outer=20, inner=12, height=10, kerf=2):
        s = Sketch(self.xy())
        self.extrude(Circle(s.origin, outer), height)
        self.extrude(Circle(s.origin, inner), height, cut=True)
        # The saw cut splits the ring into two separate bodies.
        self.extrude(
            Rectangle.from_center_and_sides(s.origin, 2 * outer + 10, kerf),
            height,
            cut=True,
        )

        block = self.extrude(
            Rectangle.from_center_and_sides(Point(s, 0, -35), 30, 6), 4
        )
        self.delete_bodies(block)

        # A selector does not have to be in the part: any solid sharing volume
        # with a body selects it.
        marker = Extrusion(Circle(Point(s, 0, (outer + inner) / 2), 1), height)
        self.keep_bodies(marker)
        self.paint("tan")


show(HalfClamp())
from cadbuildr.foundation import *


class HalfClamp(Part):
    """A multi-body part: a ring sawn into two halves, plus a construction
    block. DeleteBodies drops the block; KeepBodies keeps the +Y half only,
    picked by a small marker solid that sits inside it."""

    def __init__(self, outer=20, inner=12, height=10, kerf=2):
        s = Sketch(self.xy())
        self.extrude(Circle(s.origin, outer), height)
        self.extrude(Circle(s.origin, inner), height, cut=True)
        # The saw cut splits the ring into two separate bodies.
        self.extrude(
            Rectangle.from_center_and_sides(s.origin, 2 * outer + 10, kerf),
            height,
            cut=True,
        )

        block = self.extrude(
            Rectangle.from_center_and_sides(Point(s, 0, -35), 30, 6), 4
        )
        self.delete_bodies(block)

        # A selector does not have to be in the part: any solid sharing volume
        # with a body selects it.
        marker = Extrusion(Circle(Point(s, 0, (outer + inner) / 2), 1), height)
        self.keep_bodies(marker)
        self.paint("tan")


show(HalfClamp())