Foundation (Python)How-to guidesConnect parts with joints & anchors

Connect parts with joints & anchors

Anchors and joints position components relative to each other without any transform math. They are the foundation-native way to express “this connects to that” — and the base layer domain libraries build richer connections on (LEGO studs, screwed connections, wood joinery, tube fittings).

Anchors

An anchor is a named connector frame on a part or assembly (a “mate connector” in Onshape terms). Convention: +Z is the mate axis / outward normal of the mating face, +X is the clocking direction.

from cadbuildr.foundation import Part, make_anchor
 
class Plate(Part):
    def __init__(self):
        # ... sketch + extrude 6mm thick plate ...
        self.add_anchor(make_anchor("mount", (10.0, 10.0, 6.0)))          # top face, +Z up
        self.add_anchor(make_anchor("underside", (0.0, 0.0, 0.0), z_down=True))

Look anchors up by name with part.anchor("mount"). Derive new anchors from existing ones — derived anchors follow their base through any assembly nesting:

a = plate.anchor("mount")
a.offset([16.0, 8.0, 0.0])       # translated in the anchor's local frame
a.rotated(math.pi / 2)           # spun about the mate axis
a.flipped()                      # 180° about X: reverses the mate axis

Joints

A joint connects a parent anchor (on something already placed — or the assembly itself) to a child anchor (on the component being placed) and computes the child’s position deterministically. There is no constraint solver: each joint places exactly one component.

from cadbuildr.foundation import Assembly, RigidJoint
 
assembly = Assembly()
assembly.add_component(base)
assembly.add_joint(RigidJoint(
    parent_anchor=base.anchor("top"),
    child_anchor=brick.anchor("bottom"),
))

By default (flip=True) the anchors mate face-to-face: the parent’s +Z opposes the child’s +Z, so two outward-facing surfaces land against each other. Pass flip=BoolParameter(value=False) to align the Z axes instead (useful for “attach frame” anchors like a Lego seat).

Ground a first component against the assembly origin with assembly.ground(component.anchor("base")).

Joint types

JointDegrees of freedom
RigidJointnone — fastened
RevoluteJointangle about +Z (hinge)
SliderJointoffset along +Z
CylindricalJointangle + offset
PlanarJointdx, dy in-plane + angle about the normal
BallJointorientation quaternion
PinSlotJointangle about +Z + offset along +X (the slot)
ScrewJointangle, with travel coupled by pitch

DOF values are plain parameters with optional limits, and setters re-resolve the placement:

hinge = RevoluteJoint(
    parent_anchor=frame.anchor("hinge"),
    child_anchor=door.anchor("hinge"),
    limits=JointLimits(min=FloatParameter(value=0.0), max=FloatParameter(value=2.1)),
)
assembly.add_joint(hinge)
hinge.set_angle(1.2)   # re-places the door; raises if outside limits

Joints store absolute placements, so set_angle / set_offset also re-place everything jointed downstream of the moved child (a knee after a hip, a rigid bracket on a moved link), including joints of the enclosing assemblies when the joint lives inside a placed sub-assembly. After editing an anchor by hand, call joint.resolve() (any joint type, RigidJoint included) or assembly.resolve() to re-place the joints that depend on it.

To couple joints — a pinion turning a gear, a lead screw driving a nut — add a relation; see Joint Relations.

Patterns and replicate

Repeat a placed component instead of adding each copy by hand — the assembly-level linear / circular pattern and “Replicate” of Onshape:

Renders the 3D model in your browser. The first run downloads the Python runtime (≈30 MB).
"""Assembly patterns: replicate, polar_pattern and linear_pattern.

* ``replicate`` drops a bolt on every anchor of a bolt circle. The bolt is
  not placed yet, so each copy is fastened by a ``RigidJoint`` from the
  anchor to the bolt's ``seat`` anchor.
* ``polar_pattern`` turns copies of a placed blade about +Z.
* ``linear_pattern`` lines up copies of a placed washer along +X.

Every copy is a new instance (the BOM counts it). A copy of a jointed
component gets the same joint, so it follows its parent if that moves.
"""

from cadbuildr.foundation import *

THICKNESS = 8.0


class Flange(Part):
    def __init__(self):
        s = Sketch(self.xy())
        self.extrude(Circle(s.origin, 50.0), THICKNESS)
        self.cut_extrude(Circle(s.origin, 8.0), THICKNESS)
        self.add_anchor(make_anchor("hub", (0, 0, THICKNESS)))
        self.paint("light_grey")


class Bolt(Part):
    def __init__(self):
        s = Sketch(self.xy())
        self.extrude(RegularPolygon(s.origin, n_sides=6, radius=5.0), 4.0)  # hex head
        self.add_anchor(make_anchor("seat", (0, 0, 0)))
        self.paint("dark_grey")


class Blade(Part):
    def __init__(self):
        s = Sketch(self.xy())
        self.extrude(Rectangle.from_center_and_sides(Point(s, 21, 0), 22, 8), 3.0)
        self.paint("orange")


class Washer(Part):
    def __init__(self):
        s = Sketch(self.xy())
        self.extrude(Circle(s.origin, 6.0), 2.0)
        self.cut_extrude(Circle(s.origin, 3.0), 2.0)
        self.paint("light_blue")


class PatternedFlange(Assembly):
    def __init__(self):
        flange = Flange()
        self.add_component(flange)

        bolt_circle = flange.anchor("hub").polar_pattern(6, radius=38.0)
        self.replicate(Bolt(), onto=bolt_circle, child_anchor="seat", flip=False)

        blade = Blade()
        self.add_component(blade, at=(0, 0, THICKNESS))
        self.polar_pattern(blade, 3)  # 3 blades, 120° apart

        washer = Washer()
        self.add_component(washer, at=(-36, -66, 0))
        self.linear_pattern(washer, [1, 0, 0], 4, 24.0)


show(PatternedFlange())
bolts = asm.replicate(Bolt(), onto=flange.anchor("hub").polar_pattern(6, radius=38.0),
                      child_anchor="seat", flip=False)
blades = asm.polar_pattern(blade, 3)                       # about +Z through the origin
rollers = asm.linear_pattern(roller, [1, 0, 0], 8, 40.0)   # direction, count, spacing (mm)
MethodPlaces
linear_pattern(component, direction, count, spacing)count instances, spacing mm apart along direction
polar_pattern(component, count, angle_deg=360, axis=(0, 0, 1), center=(0, 0, 0), about=None)count instances about axis through center, or about the +Z axis of the anchor about. A full turn spaces angle_deg / count, a partial span angle_deg / (count - 1)
replicate(component, onto, child_anchor=None, flip=True)one instance on each anchor of onto
  • Each method returns the instances, the original first. Every copy is a new instance (a deep copy): it shows up in the BOM and interference checks, and later edits to the original do not reach it.
  • Patterns copy a component that is already placed in the assembly. If add_component placed it, the copies get its pose moved by each step. If a joint placed it, each copy gets the same joint (type, values, limits) on a parent anchor derived from the original’s. The copies then follow the parent when it moves: pattern pins along an arm, turn the arm, and the pins turn with it.
  • replicate with a component that is not placed yet puts it on the first anchor and copies on the rest, each with a RigidJoint to its child_anchor. With a jointed component, it repeats that joint on every other anchor of onto.
  • Relations (add_relation) are not copied; relate the copies’ joints yourself.

Connections: joints that also shape parts

A Connection bundles one joint with geometry contributions to the connected parts (PartModifier) — drill the clearance hole, cut the mortise, cope the tube end. This is the hook domain libraries use to make one-liners that both place and machine:

from cadbuildr.stdlib.fasteners.connections import connect_with_screw
from cadbuildr_projects.woodworking import mortise_and_tenon
from cadbuildr.stdlib.profiles.tubes import tube_tee
 
connect_with_screw(assembly, DIN912Screw("M4", 16), plate.anchor("mount_1"))
mortise_and_tenon(assembly, rail, leg, face="px", at=550)
tube_tee(assembly, leg_tube, handrail, at=400)

Each of those single lines places the child component with a joint and applies the implied cuts (clearance bore / tenon shoulders + mortise pocket / saddle cope) through the parts’ normal operation path.

Building your own interface library

The extension pattern, end to end:

  1. Put anchors on your parts at their natural connection points (LegoBrick has top/bottom/seat; a screw has head_seat; a beam has ends and faces).
  2. Wrap the vocabulary of your domain in functions that derive anchors and add joints — interface.on_top_of(brick, at=(2, 3)) is just a RigidJoint on a derived stud anchor.
  3. Use Connection + PartModifier when the relationship implies machining, sketching cuts on anchor_plane(anchor).

See cadbuildr_projects/lego (StudInterface), cadbuildr/stdlib (fasteners + tubes), cadbuildr_projects/woodworking, and cadbuildr_projects/playground for a full structure composed exclusively of these one-liners.

Notes

  • A component can be placed once (one joint, or one add_component) — a second placement raises. Connect further components to its anchors.
  • Joints resolve at authoring time; the serialized DAG carries concrete frames plus the joints/anchors for provenance, so viewers need nothing special to render jointed assemblies.
  • translate() / rotate() on a component before jointing is overwritten: joints are absolute placements.