"""Hex nut trap: a hexagonal prism to cut a pocket for a metric hexagon nut with, DIN 934 or ISO 4032 nut sizes. Axis along Z, the top face on the XY plane, the prism running down for the depth; two corners of the hexagon lie on the X axis. Dimensions in the README.""" import math from steve import Model thread = "M3" # Thread nut = "DIN 934" # Nut standard: DIN 934 or ISO 4032 clearance = 0 # mm Gap added on each flat (0: the nut's own largest width across flats) depth = 0 # mm Depth (0: the nut's largest height m) # thread: (s nom = max, m nom = max) in mm, the nut's width across flats and height. # DIN 934: Westfield Fasteners product specification "DIN 934 - Full Hex Nuts (Standard Pitch)", Table 1, Dimensions & # Tolerances according to DIN 934 (PDF dated 2026-01-02, page 1, https://westfieldfasteners.co.uk/Standards/Nut-Hex-M.pdf). # ISO 4032: the table "according to EN ISO 4032" at https://www.fasteners.eu/standards/DIN/934/ (rows m max. and s nominal = # max.; read 2026-11-14), the same values as the metric-nut part's ISO 4032 table. NUTS = { "DIN 934": {"M1.6": (3.2, 1.3), "M2": (4, 1.6), "M2.5": (5, 2), "M3": (5.5, 2.4), "M3.5": (6, 2.8), "M4": (7, 3.2), "M5": (8, 4), "M6": (10, 5), "M8": (13, 6.5), "M10": (17, 8), "M12": (19, 10)}, "ISO 4032": {"M1.6": (3.2, 1.3), "M2": (4, 1.6), "M2.5": (5, 2), "M3": (5.5, 2.4), "M3.5": (6, 2.8), "M4": (7, 3.2), "M5": (8, 4.7), "M6": (10, 5.2), "M8": (13, 6.8), "M10": (16, 8.4), "M12": (18, 10.8)}, } if nut not in NUTS: raise ValueError(f"no nut standard {nut!r}: {', '.join(NUTS)}") if thread not in NUTS[nut]: raise ValueError(f"no {nut} nut {thread!r} here: {', '.join(NUTS[nut])}") if clearance < 0 or depth < 0: raise ValueError("clearance and depth can't be negative") s, m = NUTS[nut][thread] flats = s + 2 * clearance deep = depth if depth > 0 else m corner = flats / math.sqrt(3) # centre to corner of a hexagon with this width across flats outline = [(corner * math.cos(math.radians(60 * i)), corner * math.sin(math.radians(60 * i))) for i in range(6)] model = Model() with model.sketch("trap_sketch", on="XY", name="Nut trap") as sk: sk.polyline("trap_outline", outline, closed=True) model.extrude("trap", "trap_sketch", distance=-deep, name="Nut trap") result = model.result()