Examples
Each example is a script in examples/ with a model() function. Run one to open the model in the viewer and write a STEP file:
python examples/flange.py
Bracket
Mounting bracket: an L-profile extruded, with a rounded inner corner and holes in both flanges.

from smlibpy import *
def model() -> Part:
t = 8 # wall thickness
profile = Polygon((0, 0), (60, 0), (60, t), (t, t), (t, 50), (0, 50))
bracket = profile.on(Plane.front).extrude(-40) # front plane faces -Y; extrude backwards
bracket = bracket.fillet(bracket.edges().concave(), 6)
vertical = bracket.edges().parallel_to(Z)
bracket = bracket.fillet(vertical.min_by(X) + vertical.max_by(X), 4) # round both ends
base = bracket.faces().facing(Z).bottom() # the base flange's top face at z = t
upright = bracket.faces().facing(-X).one()
bracket -= (GridLocations(24, 20, 1, 2) * Pos(8) * Hole(3.2, t)).on(base)
bracket -= (GridLocations(20, 1, 2, 1) * Pos(0, 8) * CounterSinkHole(3.2, 6, t)).on(upright)
return bracket.named("bracket").colored("#7c93b5")
Flange
Pipe flange: a revolved cross-section with a bolt circle.

from smlibpy import *
def model() -> Part:
# half cross-section in the front plane (x = radius, y = height), revolved about Z
section = Polygon((25, 0), (60, 0), (60, 12), (35, 12), (35, 40), (25, 40))
flange = section.on(Plane.front).revolve(Z)
flange -= PolarLocations(47.5, 8) * Cylinder(5, 30)
flange = flange.fillet(flange.edges().concave(), 4) # neck to plate
flange = flange.chamfer(flange.faces().top().edges().circles(), 1)
return flange.named("flange").colored("#b08e6c")
Enclosure
Electronics enclosure: rounded box, shelled, with screw bosses and a matching lid.

from smlibpy import *
C, MIN = Align.CENTER, Align.MIN
def model() -> ShapeList:
w, d, h, wall = 100, 64, 32, 2
box = RoundedRectangle(w, d, 8).extrude(h)
box = box.fillet(box.faces().bottom().edges(), 4)
box = box.shell(wall, box.faces().top())
boss = Cylinder(4, h - wall, align=(C, C, MIN)) - Cylinder(1.4, h, align=(C, C, MIN))
box += (GridLocations(w - 16, d - 16, 2, 2) * boss).translate(0, 0, wall)
lid = RoundedRectangle(w, d, 8).extrude(wall)
lip = RoundedRectangle(w - 2 * wall - 0.4, d - 2 * wall - 0.4, 6).extrude(-3)
lid = (lid + lip) - GridLocations(w - 16, d - 16, 2, 2) * Cylinder(1.7, 20)
lid = lid.translate(0, 0, h + 15)
return ShapeList([box.named("box").colored("#5b6b7f"), lid.named("lid").colored("#d9925a")])
Pipe
A bent pipe: a ring profile swept along a path of lines and arcs, with flanges at the ends.

from smlibpy import *
def model() -> Part:
r = 30 # bend radius
path = Wire([
Line((0, 0, 0), (0, 0, 60)),
CenterArc((r, 60), r, 180, -90, plane=Plane.front),
Line((r, 0, 60 + r), (r + 80, 0, 60 + r)),
])
pipe = sweep(Circle(10) - Circle(8), path)
flange = (Circle(20) - Circle(8) - PolarLocations(15, 4) * Circle(2)).extrude(4)
start = flange.translate(0, 0, -4)
end = flange.on(Plane((r + 80, 0, 60 + r), z_dir=(1, 0, 0)))
return (pipe + start + end).named("pipe").colored("#79ad73")
Spring
Compression spring: a circle swept along a helix, with ground ends.

from smlibpy import *
def model() -> Part:
coil = sweep(Circle(1.2), Helix(pitch=6, height=48, radius=10))
coil = coil.split(Plane.XY.offset(1.2)).split(Plane.XY.offset(48 - 1.2), keep="below")
return coil.named("spring").colored("#9d87c7")
Bottle
The classic OpenCASCADE bottle, built with smlibpy.

from smlibpy import *
def model() -> Part:
width, thickness, height = 50.0, 30.0, 70.0
# half of the base outline: a straight flank, an arc, another flank
half = Wire([
Line((-width / 2, 0), (-width / 2, -thickness / 4)),
ThreePointArc((-width / 2, -thickness / 4), (0, -thickness / 2), (width / 2, -thickness / 4)),
Line((width / 2, -thickness / 4), (width / 2, 0)),
])
outline = half + half.mirror(Plane.XZ)
body = make_face(outline).extrude(height)
body = body.fillet(body.edges(), thickness / 12)
neck = Cylinder(thickness / 4, height / 10, align=(Align.CENTER, Align.CENTER, Align.MIN)).translate(0, 0, height)
bottle = body + neck
bottle = bottle.shell(thickness / 50, bottle.faces().top())
return bottle.named("bottle").colored("#6bb3b3")
Gear
Involute spur gear with a bore and a keyway.

import math
from smlibpy import *
def involute_tooth(module: float, teeth: int, pressure_angle: float = 20.0) -> Sketch:
rp = module * teeth / 2 # pitch radius
rb = rp * math.cos(math.radians(pressure_angle)) # base radius
ra = rp + module # tip radius
rr = rp - 1.25 * module # root radius
def involute(r: float) -> tuple[float, float]:
t = math.sqrt(max((r / rb) ** 2 - 1, 0.0))
return rb * (math.cos(t) + t * math.sin(t)), rb * (math.sin(t) - t * math.cos(t))
# half tooth thickness at the pitch circle is a quarter of the circular pitch
inv = lambda a: math.tan(a) - a # noqa: E731
half_angle = math.pi / (2 * teeth) + inv(math.radians(pressure_angle))
start = max(rb, rr)
pts = [involute(start + (ra - start) * i / 12) for i in range(13)]
rot = lambda p, a: (p[0] * math.cos(a) - p[1] * math.sin(a), p[0] * math.sin(a) + p[1] * math.cos(a)) # noqa: E731
right = [rot(p, -half_angle) for p in pts]
left = [(x, -y) for x, y in reversed(right)]
flank_r = Spline(*right)
flank_l = Spline(*left)
tip = ThreePointArc(right[-1], (ra, 0), left[0])
root_r, root_l = (rr * 0.98, right[0][1] * rr / right[0][0]), (rr * 0.98, left[-1][1] * rr / left[-1][0])
outline = Wire([Line(root_r, right[0]), flank_r, tip, flank_l, Line(left[-1], root_l), Line(root_l, root_r)])
return make_face(outline)
def model(module: float = 2.0, teeth: int = 24, width: float = 12.0, bore: float = 10.0) -> Part:
rr = module * teeth / 2 - 1.25 * module
tooth = involute_tooth(module, teeth)
profile = Circle(rr) + PolarLocations(0, teeth) * tooth
profile = profile - Circle(bore / 2) - Rectangle(3, 3).translate(bore / 2, 0)
gear = profile.extrude(width)
gear = gear.chamfer(gear.edges().circles().filter(lambda e: e.radius > bore), 0.5)
return gear.named("gear").colored("#d4b45a")