@rulecad/shapes
Shape helpers for printed parts: text that prints, anchors on a face, clean cuts, and an enclosure kit.
npm install @rulecad/shapes
Try it
The enclosure kit's own code, running here: a macropad's case as a shell with a gabled opening for the USB cable, its lid (printed face down; shown lifted and turned over), and a dovetail rail. Its four screw holes are named by their thread, so they come from @rulecad/fixings: countersink('M2', …) reads the M2 rule set and the printer's, and draws with this package's countersink.
Reference
Helpers for JSCAD designs that get printed. JSCAD is a peer dependency: use the one you already have.
Every helper here takes plain dimensions in mm and holds no screw size of its own. Anything named by a thread (a hole for an M3 screw, the seat for its head, its nut or its insert) comes from @rulecad/fixings, which reads the thread's rule set and the printer's and calls this package's countersink, teardropHole, nutPocket and wallOpening with the figures. So fixings.countersink('M3', ...) and countersink({ hole, head, ... }) draw the same cone; the first also says where each size came from. Shapes never imports fixings, so the two have no cycle.
import jscad from '@jscad/modeling';
import { text, anchor, cut } from '@rulecad/shapes';
const line = 0.42; // your printer's line width
const legend = text({ text: 'Esc', height: 3.5, stroke: line * 2 }); // a 2D outline
const [x, y] = anchor({ width: 18, depth: 18 }, [8, 4], 'tl'); // where it goes on an 18 mm face
const inlay = jscad.transforms.translate([x, y, 0], jscad.extrusions.extrudeLinear({ height: 0.6 }, legend));
const cap = cut(body, inlay); // only the faces near the legend are cut
text({ text, height, stroke, segments })uses JSCAD's single-line font, scaled so a capital isheighttall. Every line is thickened tostrokewide, so make it whole print lines. Punctuation keeps its size, and the arrows the font lacks (↑ ↓ ← →) are drawn in the same stroke.anchor({ width, depth }, [w, d], at)places somethingw×don a face at one of nine anchors:tl tc tr / ml mc mr / bl bc br. The face is centred on 0, with y to the back.cut(a, b, margin)issubtract(a, b), but only the faces nearbare cut. JSCAD's subtract splits every face ofaalongb's planes, however far away, and those splits can leave hairline gaps that slicers dislike.size(geom2)gives a 2D outline's[width, depth].
Enclosures
The pieces cases are made of, drawn one way everywhere, so a family of parts looks like one system. Footprints are in x and y, centred on 0, z up from the bed; every slope is 45 degrees, so they print without supports as drawn.
import { shell, lid, dovetail, gabled } from '@rulecad/shapes';
const box = shell({ width: 72, height: 72, depth: 17, round: 6, wall: 2.1, floor: 1.6, bottom: 0.4 }); // open at the top
const front = lid({ width: 72, height: 72, round: 6, thick: 2.4, edge: 2, wall: 2.1, fit: 0.2, lip: 2, ring: 1.26 }); // printed face down
const rail = extrudeLinear({ height: 17 }, dovetail({ base: 6, height: 1.6 })); // and its slot: dovetail({ ..., gap: 0.4 })
const window = gabled([-1, 1], [-1, 4], [10, 13]); // an opening in an upright wall
outline(w, h, { round, bevel }): a rectangle with rounded or cut corners (2D).slab({ width, height, depth, round, bevel, bottom, top }): a block on that outline, its lower and upper edges sloped in bybottom(against elephant's foot) andtop(a bevelled front edge).shell({ ...slab, wall, floor }): a slab hollowed fromfloorup, open at the top.lid({ width, height, round, bevel, thick, edge, wall, fit, lip, ring }): a lid printed face down, its front edge sloped byedge, with a lipringwide that dropslipdeep inside a shell's walls,fitaway. Place it turned over (an assembly item's"rotate": [180, 0, 0]).dovetail({ base, height, gap }): a rail's profile rising from y = 0, flanks at 45 degrees; withgap, the slot that takes it,gapwider each side and deeper.gabled([x0, x1], [y0, y1], [z0, z1], axis): a box with a 45 degree gabled roof, for openings in upright walls.diamond(size, axis, length): a square hole turned 45 degrees, for small holes in upright walls.vents({ count, width, pitch, length, depth }): slots side by side along x.
Seats for bought parts
The printed side of a bearing or a shaft. Each one prints without supports in the orientation it is drawn for, and takes its sizes from rules: the bearing's from its rule set, the fit from the printer's (hole_comp, fit, press, slip).
import { bearingSeat, seatSize, shaftBore, plainBearing, pulleyBore, shaftShoulder, clipGroove } from '@rulecad/shapes'
const seat = bearingSeat(v['608'], { fit: 'press', printer: v.x1c, at: [0, 0, 12], into: '-z', through: true, relief: 5 })
seat.seat.pocket // 22.05: the diameter as drawn; clearance -0.05 once printed
plainBearing(8, { length: 10, printer: v.x1c, at: [0, 0, 10], into: '-z', through: true }) // a bore to subtract
plainBearing(8, { length: 10, wall: 2, printer: v.x1c }) // a bush, standing up, to add
shaftShoulder(8, { shoulder: 12, height: 3, at: [0, 0, 20] }) // a collar: flat top, 45 degree underside
clipGroove(8, { width: 1, depth: 0.5, at: [0, 0, 30] }) // a groove: flat lower face, sloped top
| Figure | How it is worked out |
|---|---|
| Pocket, as drawn | outer + hole_comp - press (or + slip, or + a clearance you give, in mm across) |
Ledge (lip) | a quarter of the races' radial depth by default, so it carries the outer race only; at most half |
| Shaft bore, as drawn | shaft + 2 × the fit each side + hole_comp (shaftBore): slide 0.05 mm (measured), press -0.02 mm (estimate), run the printer's fit, or a gap in mm |
| Plain bore, as drawn | a shaft bore at run by default: the printer's fit each side |
Every bore for a bought shaft reads its fit from one place, shaftBore and its table SHAFT_FITS: plainBearing, pulleyBore and the motor's bore in @rulecad/motors. So a new measurement of the slide fit moves them all at once. The slide fit was measured on the fit bar's D-bores (examples/bench/projects/benchmarks/MEASURE.md row 3, PLA on the X1C, 2026-11-10): a 5 mm shaft slides comfortably in a bore drawn 5.2 mm, is tight in 5.1 mm and will not go in 5.0 mm.
shaftBore(5, { printer: v.x1c }) // { diameter: 5.2, each: 0.05, kind: 'measured', ... }: a hub that slides on
shaftBore(5, { fit: 'press', across: 4.5, printer: v.x1c }) // 5.06 across, 4.56 across a D flat
pulleyBore({ bore: 5 }, { length: 12, printer: v.x1c }) // 5.2: a pulley's grub screw bears on the shaft
plainBearing(8, { length: 10, printer: v.x1c }) // 8.5: a bore that turns on its shaft, at run
lm8uuPocket, nutTrap, pulleyBore and beltClamp are the same idea for a linear bearing, a lead-screw nut's flange and body, a pulley on its shaft and a GT2 belt's grip. Of these only nutTrap has screws, the ones that hold the nut's flange. Their hole is a plain figure too: hole, the hole as drawn, from fixings (whose diameter already has the printer's hole_comp in it), or else the nut's own hole_diameter (mount_hole_diameter in its rule set) plus hole_comp. Given neither, nutTrap refuses: there is no default screw.
const screws = fixings.holeModel('M3', { printer: v.x1c }) // an M3 clearance hole, as drawn
nutTrap(v['t8-nut'], { hole: screws.diameter, printer: v.x1c, at: [0, 0, 20] }) // or leave hole out: the nut's own 3.5 mm
The screws stay out of this package on purpose. Shapes cannot read a thread's rule set without depending on fixings, and fixings already depends on shapes, so a seat takes the screw's figure and the design passes it from fixings.
press and slip are estimates until they are measured with the seats coupon in the bench project: print it once per filament, press a bearing into each pocket, and write the one that holds without cracking into the printer's rules.
Print-aware features
Holes, pockets and ribs drawn so they print without supports with the part lying as drawn (z up from the bed). Each starts at a point on a face (at) and goes into the material (into: '+x', '-x', '+y', '-y', '+z' up from the bed, '-z' down from a top). Sizes come from your rules; nothing is built in. For a screw by its thread, call the same seats through fixings (fixings.countersink('M3', { length: 16, at, into }), fixings.nutPocket('M3', ...), fixings.hole('M3', { into: '+x', depth: 20 })): they read the thread's figures and the printer's, and draw with these. The calls below are for sizes you have in hand.
import { countersink, teardropHole, nutPocket, crushRibs, wallOpening } from '@rulecad/shapes'
countersink({ hole: v.hole, head: v.head, length: 16, at: [x, y, 0], into: '+z' }) // flat head flush with the face
teardropHole({ d: v.hole, length: 20, at: [-10, 0, 5], into: '+x' }) // across a part: a 45° pointed roof
nutPocket({ flats: v.flats, height: v.height, hole: v.hole, at: [x, y, 0], into: '+z' })
crushRibs({ at: [[-9.4, 5], [9.4, 5]], into: [[1, 0], [-1, 0]], z: [3, 5], radius: 0.84, protrude: 0.5 }) // to add
wallOpening({ width: 12.75, height: 6.9, depth: v.wall, at: [x, 0, z], into: '-y', bridge: v.x1c.bridge })
- A countersink from the side (
into±x or ±y) is a teardrop: its round parts get a 45° ridge on top. - A nut pocket going up from the bed gets a 45° hexagonal roof to the hole; across a part, a flat top (a short bridge).
- An opening wider than the printer's
bridgerises at 45° each side to a flat that wide. - Cutters start a little outside the face they enter by, so no faces lie on each other.
Booleans: JSCAD or Manifold
ops(engine) gives union, subtract and intersect behind one interface. Designs get it as v.ops, so they say what they mean and the workspace picks the engine: JSCAD's own (the default, exactly as before) or Manifold (much faster, always watertight), with "engine": "manifold" in an object's JSON or CAD_ENGINE=manifold for every design. Manifold needs npm install manifold-3d in the workspace.
export function main (v) {
return v.ops.subtract(body, holes, pockets) // arrays are fine, as with JSCAD
}
With Manifold, gap(a, b, within) gives the least distance between two solids' surfaces (Manifold's minGap), in mm: 0 where they touch or cross, within where nothing is nearer than that. cad checks parts printed in place with it. JSCAD has no distance query, so gap on the JSCAD engine refuses by name.
A shape Manifold can't read as a closed solid falls back to JSCAD for that operation (stats.fallbacks counts them).