FoundationPartsSketchesSketch operations

Sketch operations

Helpers that return ordinary sketch shapes, ready to extrude or cut:

HelperResult
polar_copies(shape, center, count, angle=360)copies rotated about a point
linear_copies(shape, dx, dy, count)copies shifted by a fixed step
mirror(shape, line)mirror image across a line (Point, Line, Circle, Polygon-like)
Trace(points, width)a constant-width stroke along a polyline, with round joins

The plate above cuts six radial slots, a triangle and its mirror image, a row of four holes, and engraves a 2 mm deep zig-zag Trace into its top face.

from cadbuildr.foundation import *


class SketchOpsPlate(Part):
    """Sketch helpers: polar_copies, mirror, linear_copies and Trace.

    All of them return ordinary sketch shapes, so they are cut out of the plate
    with one Extrusion each.
    """

    def __init__(self, thickness=5):
        s = Sketch(self.xy())
        self.add_operation(
            Extrusion(Rectangle.from_center_and_sides(s.origin, 120, 70), thickness)
        )

        # Six radial slots spread around a circle.
        slot = Rectangle.from_center_and_sides(Point(s, -30, 18), 4, 10)
        slots = polar_copies(slot, Point(s, -30, 0), 6)
        self.add_operation(Extrusion(slots, thickness, cut=True))

        # A triangle and its mirror image across the vertical line x = 10.
        tri = Triangle(Point(s, 18, 10), Point(s, 30, 10), Point(s, 18, 25))
        axis = Line(Point(s, 10, -30), Point(s, 10, 30))
        self.add_operation(Extrusion([tri, mirror(tri, axis)], thickness, cut=True))

        # A row of four round holes.
        self.add_operation(
            Extrusion(
                linear_copies(Circle(Point(s, -45, -25), 3), 12, 0, 4),
                thickness,
                cut=True,
            )
        )

        # A 3 mm wide, 2 mm deep zig-zag groove engraved in the top face.
        top = Sketch(self.xy().get_parallel_plane(thickness))
        path = [
            Point(top, 15, -25),
            Point(top, 25, -10),
            Point(top, 35, -25),
            Point(top, 45, -10),
        ]
        self.add_operation(Extrusion(Trace(path, 3), -2, cut=True))
        self.paint("medium_azure")


show(SketchOpsPlate())

Trim, extend, intersect

HelperResult
intersect(l1, l2)the Point where the two (infinite) lines cross
trim_corner(l1, l2)both lines trimmed or extended to meet exactly at that point
extend(line, start=0, end=0)the line lengthened at either end (negative values shorten it)

Draw rough construction lines, snap them into exact corners, then build the outline from the cleaned-up lines:

from cadbuildr.foundation import *


class TrimmedFrame(Part):
    """Sketch trim / extend: two rough lines are trimmed into an exact corner
    with `trim_corner`, a third is lengthened with `extend`, and `intersect`
    finds where they meet — then the outline is closed and extruded."""

    def __init__(self):
        s = Sketch(self.xy())
        # Rough construction lines that overshoot / fall short of each other.
        bottom = Line(Point(s, -5, 0), Point(s, 35, 0))
        slope = Line(Point(s, 40, 3), Point(s, 10, 30))
        bottom, slope = trim_corner(bottom, slope)
        left = extend(
            Line(Point(s, 0, 2), Point(s, 0, 20)), start=2
        )  # now starts at y = 0
        top_corner = intersect(left, slope)
        outline = Polygon(
            lines=[
                Line(left.p1, bottom.p2),
                Line(bottom.p2, top_corner),
                Line(top_corner, left.p1),
            ]
        )
        self.add_operation(Extrusion(outline, 4))
        self.paint("dark_orange")


show(TrimmedFrame())