Joint Relations
A relation couples the value of one joint to the value of another — the “mate relations” of Onshape. Turn a pinion and the gear it meshes with turns by the gear ratio; the rack under it slides by the pitch-circle arc.
The four relations
Every relation reads one degree of freedom of a driver joint and writes one degree of freedom of a driven joint:
| Relation | Driver → driven | Rule |
|---|---|---|
GearRelation(driver, driven, ratio) | angle → angle | driven = ratio · driver |
RackPinionRelation(pinion, rack, pitch_radius) | angle → offset | rack = pitch_radius · pinion (2π·r mm per turn) |
ScrewRelation(screw, nut, pitch) | angle → offset | nut = pitch · screw / 2π (one pitch per turn) |
LinearRelation(driver, driven, ratio) | offset → offset | driven = ratio · driver |
- Angles (radians) come from
RevoluteJoint,CylindricalJoint,ScrewJointandPinSlotJoint; offsets (mm) fromSliderJoint,CylindricalJointandPinSlotJoint. ratio,pitch_radiusandpitchare positive.reverse=Trueturns or slides the driven joint the other way.offsetis the driven value when the driver is at 0 — e.g. half a tooth of phase so two gears mesh at angle 0.ScrewRelationwithout anutcouples aCylindricalJoint’s own offset to its own angle.
from cadbuildr.foundation import GearRelation, RackPinionRelation, RevoluteJoint, SliderJoint
drive = RevoluteJoint(asm.anchor("pinion_axle"), pinion.anchor("axis"))
driven = RevoluteJoint(asm.anchor("gear_axle"), gear.anchor("axis"))
slide = SliderJoint(asm.anchor("rack_way"), rack.anchor("slide"))
for joint in (drive, driven, slide):
asm.add_joint(joint)
asm.add_relation(GearRelation(driver=drive, driven=driven, ratio=12 / 24, reverse=True))
asm.add_relation(RackPinionRelation(pinion=drive, rack=slide, pitch_radius=12.0))
drive.set_angle(math.radians(20)) # the gear and the rack follow
driven.set_angle(-0.1) # works too: the pinion is back-solvedHow relations behave
- Both joints must be placed (
add_joint) beforeadd_relation. The relation is enforced at once: the driven joint jumps to the value its driver dictates. - Either end can be set.
set_angle/set_offseton a driven joint back-solves its driver, then recomputes everything that driver drives — relations chain into gear trains. - Downstream joints follow. A part jointed onto a driven gear moves with
it, through sub-assemblies too, exactly as after a direct
set_angle. - Limits apply to every joint a change moves. If the driven joint would
leave its
JointLimits, the call raises and nothing moves. - No conflicts, no cycles. A joint value follows at most one relation (chain them instead: A drives B, B drives C), and relations cannot loop back to their driver — both raise when you add the relation.
assembly.resolve()re-applies the assembly’s relations, e.g. after a joint value was edited by hand.
Relations, like joints, resolve when the Python runs: the serialized DAG carries the resulting component frames plus the relation nodes, so viewers and kernels need nothing special.
Not yet supported
Parallel, tangent and width mates are not available: they constrain geometry rather than joint values, which needs a constraint solver.