Two-Part Project Enclosure

An ABS project box with a Eurocard controller board on hex standoffs: chips, relays, terminal blocks, a heat sink and a coin cell. The four M3 lid screws turn out of their tapped posts, one turn for each pitch, as the lid lifts, and a cutaway opens the lid. The board size, the 2.54 mm lead grid, the M3 screws with their thread and the CR2032 cell are published figures. The kernel measures the mass of the two ABS shells. The box and the parts on the board are a display model.
Code
This is the full program. The playground runs it in your browser without a change.
ts
import { box, compound, cut, cutAll, cylinder, edgeFinder, extrude, fillet, fuseAll, line, loft, measureVolume, polygon, revolve, rotate, sphere, translate, unwrap, wire } from 'brepjs/quick';
import { color } from 'brepjs/playground';
// Two-part ABS project enclosure with a Eurocard controller board inside. Z is up, units are millimetres.
// The board size, the hole grid, the M3 screws with their thread and the coin cell are published figures.
// The box, the lid joint and the parts on the board are a display model.
interface EnclosureParams {
explode?: number; // 0 = closed, 1 = lid lifted. The lid screws turn out of the posts, one turn for each pitch
cutaway?: number; // part of the lid length to cut away from the +X end (0 = whole lid)
standoff?: number; // height of the board above the floor, mm
}
type P2 = [number, number];
type Shape = ReturnType<typeof cylinder>;
// Kernel shapes live in WASM memory. Every temporary goes through own(). freeing() runs a build and then frees them all.
const owned: { delete(): void }[] = [];
const own = <T extends { delete(): void }>(s: T): T => {
owned.push(s);
return s;
};
const freeing = <T,>(build: () => T): T => { try { return build(); } finally { owned.splice(0).forEach((s) => s.delete()); } }; // also when the build throws
// ---- published figures
const BOARD = { length: 160, width: 100, thick: 1.6 }; // Eurocard, IEC 60297-3
const GRID = 2.54; // lead pitch of the through-hole parts
const M3 = { major: 3, minor: 2.459, head: 5.6, headHeight: 2.4, pitch: 0.5, clearance: 3.4 }; // metric coarse thread, ISO 7045 pan head, ISO 273 hole
const CELL = { diameter: 20, height: 3.2 }; // CR2032, IEC 60086
const ABS_DENSITY = 1.04e-3; // grams per cubic millimetre
// ---- display model
const GAP = 6; // board edge to wall
const WALL = 2.5;
const FLOOR = 2.5;
const CAP = 3; // lid top
const BASE_H = 30;
const LID_H = 20;
const CORNER = 6;
const TONGUE = { width: 1.2, height: 2, slack: 0.15 }; // joint between base rim and lid
const GROMMET = { bore: 3.5, neck: 5, flange: 7.5, lip: 1.5 }; // cable entries in the +X wall
const POST = { radius: 3.5, inset: 2.5, depth: 10 }; // corner screw posts, tapped M3
const PLAY = 0.08; // radial play between a screw thread and its tapped hole
const SCREW_LEN = 25;
const BOARD_SCREW = 6;
const CLAMP = { from: 5.2, to: 8.8, screw: 2.8, slot: 0.6 }; // threaded clamp and slotted screw of a terminal, above the board
const HOLE_INSET = 5; // board mounting holes from the board edges
const HEX = 5.5; // standoff, across flats
const LIFT = 46; // lid travel at explode = 1
// Box with rounded vertical edges. Its centre is on the Z axis, its bottom is at z.
const tub = (l: number, w: number, h: number, z: number, r: number): Shape => {
const blk = own(box(l, w, h, { at: [0, 0, z + h / 2] }));
return unwrap(fillet(blk, edgeFinder().inDirection('Z'), r)) as Shape;
};
// Box from its centre in X and Y and its bottom in Z.
const brick = (l: number, w: number, h: number, x: number, y: number, z: number): Shape => box(l, w, h, { at: [x, y, z + h / 2] });
// A closed profile in the (r, z) half-plane, revolved about Z.
const lathe = (pts: P2[]): Shape => {
const profile = own(unwrap(polygon(pts.map(([r, z]): [number, number, number] => [r, 0, z]))));
return unwrap(revolve(profile, { axis: [0, 0, 1] })) as Shape;
};
// A closed outline in the XY plane, extruded up from z.
const slab = (pts: P2[], z: number, h: number): Shape => {
const face = own(unwrap(polygon(pts.map(([x, y]): [number, number, number] => [x, y, z]))));
return unwrap(extrude(face, h)) as Shape;
};
const volume = (s: Shape): number => unwrap(measureVolume(s));
// A helical thread tooth about +Z, from z = from to z = to. Its crest passes the +X side at z = ref.
// tip > core is the thread of a screw on a core, tip < core is the thread in a hole. hand 1 is a right-hand thread.
const FACETS = 12; // sections of the smooth loft in each turn. Each flank is one helical face
const SLACK = 0.998; // keeps the tooth a hair clear of its core or its bore
const helixTooth = (core: number, tip: number, pitch: number, from: number, to: number, ref: number, hand: 1 | -1 = 1): Shape => {
const root = tip > core ? core / SLACK : core * SLACK;
const crest = tip > core ? tip : tip / SLACK;
const half = 0.42 * pitch;
const n = Math.max(2, Math.round(((to - from) / pitch) * FACETS));
const sections = Array.from({ length: n + 1 }, (_, j) => {
const z = from + ((to - from) * j) / n;
const a = hand * 2 * Math.PI * ((z - ref) / pitch);
const at = (r: number, dz: number): [number, number, number] => [r * Math.cos(a), r * Math.sin(a), z + dz];
const pts = [at(root, -half), at(crest, 0), at(root, half)];
return own(unwrap(wire(pts.map((p, i) => own(line(p, pts[(i + 1) % 3]!))))));
});
return own(unwrap(loft(sections, { ruled: false }))) as Shape;
};
const FLANK = 0.42 * M3.pitch; // half width of a tooth at its root
const RC = M3.minor / 2;
const RM = M3.major / 2;
// The thread of a tapped M3 hole from z = from to z = to. It is half a pitch from the thread of a screw on the same axis.
const tapped = (from: number, to: number): Shape => helixTooth(RM + PLAY, RC + PLAY, M3.pitch, from + FLANK, to - FLANK, M3.pitch / 2);
// Pan-head screw, head top at z = 0: a cross recess, a plain shank, and a core with one helical tooth.
const screw = (length: number, threaded: number): Shape => {
const k = M3.headHeight;
const end = -k - length;
const start = end + threaded;
const pts: P2[] = [[0, 0], [M3.head / 2 - 0.9, 0], [M3.head / 2, -0.9], [M3.head / 2, -k], [RM, -k], [RM, start], [RC, start], [RC, end + 0.2], [RC - 0.2, end], [0, end]];
const cross = [own(brick(3.4, 0.7, 1.4, 0, 0, -1.2)), own(brick(0.7, 3.4, 1.4, 0, 0, -1.2))];
return compound([own(unwrap(cutAll(own(lathe(pts)), cross)) as Shape), helixTooth(RC, RM, M3.pitch, end + 0.2 + FLANK, start - FLANK, 0)]) as Shape;
};
function enclosure({ explode = 0.55, cutaway = 0.5, standoff = 10 }: EnclosureParams = {}) {
const il = BOARD.length + 2 * GAP;
const iw = BOARD.width + 2 * GAP;
const ol = il + 2 * WALL;
const ow = iw + 2 * WALL;
const px = il / 2 - POST.inset;
const py = iw / 2 - POST.inset;
const posts: P2[] = [[px, py], [-px, py], [-px, -py], [px, -py]];
const lift = explode * LIFT;
// Base: a tray, a tongue on its rim and four corner posts with tapped holes.
const cavity = own(tub(il, iw, BASE_H + LID_H, FLOOR, CORNER - WALL));
const tongueBlank = own(tub(il + 2 * TONGUE.width, iw + 2 * TONGUE.width, TONGUE.height, BASE_H, CORNER - WALL + TONGUE.width));
const rim = own(unwrap(fuseAll([own(tub(ol, ow, BASE_H, 0, CORNER)), tongueBlank])) as Shape);
const tray = own(unwrap(cut(rim, cavity)) as Shape);
const solidBase = own(unwrap(fuseAll([tray, ...posts.map(([x, y]) => own(cylinder(POST.radius, BASE_H - 1, { at: [x, y, 1] })))])) as Shape);
const entries: P2[] = [[-24, BASE_H * 0.6], [30, BASE_H * 0.6]]; // [y, z], in line with the terminal blocks
const tray2 = own(unwrap(cutAll(solidBase, [
...posts.map(([x, y]) => own(cylinder(RM + PLAY, POST.depth + 1, { at: [x, y, BASE_H - POST.depth] }))),
...entries.map(([y, z]) => own(cylinder(GROMMET.neck, WALL + 2, { at: [il / 2 - 1, y, z], axis: [1, 0, 0] }))),
])) as Shape);
// Rubber grommets: a neck through the wall and a lip on each side of it.
const g = GROMMET;
const ring = own(lathe([[g.bore, -g.lip], [g.flange, -g.lip], [g.flange, 0], [g.neck, 0], [g.neck, WALL], [g.flange, WALL], [g.flange, WALL + g.lip], [g.bore, WALL + g.lip]]));
const grommets = entries.map(([y, z]) => own(translate(own(rotate(ring, 90, { axis: [0, 1, 0] })), [il / 2, y, z])));
const feet = [1, -1].flatMap((sx) => [1, -1].map((sy) => cylinder(6, 2.5, { at: [sx * (ol / 2 - 14), sy * (ow / 2 - 14), -2.5] })));
// Lid: a cap with a skirt, posts down to the base posts, counterbores and a label recess.
const seat = BASE_H + LID_H - (M3.headHeight + 0.2);
const lidBlank = own(tub(ol, ow, LID_H + 4, BASE_H - 4, CORNER));
const soft = own(unwrap(fillet(lidBlank, edgeFinder().not((f) => f.inDirection('Z')), 1.5)) as Shape);
const lidTools = [
own(tub(ol + 2, ow + 2, 5, BASE_H - 5, CORNER)), // trim to the joint plane
own(tub(il + 2 * (TONGUE.width + TONGUE.slack), iw + 2 * (TONGUE.width + TONGUE.slack), TONGUE.height + TONGUE.slack, BASE_H, CORNER - WALL + TONGUE.width)),
own(tub(BOARD.length * 0.42, BOARD.width * 0.55, 1, BASE_H + LID_H - 0.6, 2)), // label recess
...posts.flatMap(([x, y]) => [
own(cylinder(M3.clearance / 2, LID_H + 2, { at: [x, y, BASE_H - 1] })),
own(cylinder(M3.head / 2 + 0.4, LID_H, { at: [x, y, seat] })),
]),
];
const lidHollow = own(tub(il, iw, LID_H - CAP + 1, BASE_H - 1, CORNER - WALL));
const lidShell = own(unwrap(cut(soft, lidHollow)) as Shape);
const lidPosts = posts.map(([x, y]) => own(cylinder(POST.radius, LID_H - 1, { at: [x, y, BASE_H] })));
const lidWhole = own(unwrap(cutAll(own(unwrap(fuseAll([lidShell, ...lidPosts])) as Shape), lidTools)) as Shape);
const grams = (volume(tray2) + volume(lidWhole)) * ABS_DENSITY;
const keep = ol / 2 - cutaway * ol;
const lidCut = cutaway > 0 ? own(unwrap(cut(lidWhole, own(brick(ol, ow + 2, LID_H + 6, keep + ol / 2, 0, BASE_H - 3)))) as Shape) : lidWhole;
const lid = translate(lidCut, [0, 0, lift]);
// Lid screws: one master, one copy for each post that the cut lid still covers. A screw that rises turns in its thread.
const rise = lift * 1.3;
const headTop = seat + M3.headHeight;
const lidScrew = own(rotate(own(screw(SCREW_LEN, 12)), (360 * rise) / M3.pitch));
const lidScrews = posts.filter(([x]) => cutaway <= 0 || x < keep).map(([x, y]) => own(translate(lidScrew, [x, y, headTop + rise])));
const postThread = tapped(BASE_H - POST.depth + 1 - headTop, BASE_H - headTop);
const base = compound([tray2, ...posts.map(([x, y]) => own(translate(postThread, [x, y, headTop])))]) as Shape;
// Board on four hex standoffs with tapped holes, held by short screws.
const zb = FLOOR + standoff;
const top = zb + BOARD.thick;
const hx = BOARD.length / 2 - HOLE_INSET;
const hy = BOARD.width / 2 - HOLE_INSET;
const holes: P2[] = [[hx, hy], [-hx, hy], [-hx, -hy], [hx, -hy]];
const board = unwrap(cutAll(own(brick(BOARD.length, BOARD.width, BOARD.thick, 0, 0, zb)), holes.map(([x, y]) => own(cylinder(1.6, 4, { at: [x, y, zb - 1] }))))) as Shape;
const hexR = HEX / Math.sqrt(3);
const hexPts = Array.from({ length: 6 }, (_, i): P2 => [hexR * Math.cos((i * Math.PI) / 3), hexR * Math.sin((i * Math.PI) / 3)]);
const reach = Math.min(BOARD_SCREW, BOARD.thick + standoff - 1) - BOARD.thick; // screw length inside the standoff
const hexBar = own(unwrap(cut(own(slab(hexPts, FLOOR, standoff)), own(cylinder(RM + PLAY, reach + 1, { at: [0, 0, zb - reach - 0.6] })))) as Shape);
const tappedBar = own(compound([hexBar, own(translate(tapped(-M3.headHeight - BOARD.thick - reach - 0.6, -M3.headHeight - BOARD.thick), [0, 0, top + M3.headHeight]))]) as Shape);
const standoffs = holes.map(([x, y]) => own(translate(tappedBar, [x, y, 0])));
const boardScrew = own(screw(BOARD.thick + reach, BOARD.thick + reach - 0.6));
const boardScrews = holes.map(([x, y]) => own(translate(boardScrew, [x, y, top + M3.headHeight])));
// Parts on the board, sorted by colour. Positions are board coordinates.
const dark: Shape[] = [];
const steel: Shape[] = [...lidScrews, ...boardScrews];
const brass: Shape[] = [...standoffs];
const orange: Shape[] = [];
const white: Shape[] = [];
const part = (list: Shape[], s: Shape): void => { list.push(own(s)); };
// Dual in-line package along X: a body with a notch and two rows of leads on the grid.
const dip = (pins: number, x: number, y: number, row: number): void => {
const n = pins / 2;
const len = n * GRID + 1;
const body = own(brick(len, row - 1.6, 3.6, x, y, top + 0.8));
part(dark, unwrap(cut(body, own(cylinder(1.2, 1, { at: [x - len / 2, y, top + 3.9] })))) as Shape);
for (let i = 0; i < n; i++) for (const side of [1, -1]) part(steel, brick(1.2, 1, 2.6, x + (i - (n - 1) / 2) * GRID, y + side * (row / 2 - 0.3), top));
};
dip(40, 10, 6, 6 * GRID);
dip(16, 0, -22, 3 * GRID);
dip(16, 27, -22, 3 * GRID);
dip(28, -46, -8, 6 * GRID);
// Screw terminal blocks on the +X edge, 5.08 mm pitch. Each way has a slotted M3 screw in a threaded steel clamp.
const c = CLAMP;
const clampTube = own(unwrap(cut(own(cylinder(1.9, c.to - c.from, { at: [0, 0, c.from] })), own(cylinder(RM + PLAY, c.to - c.from + 2, { at: [0, 0, c.from - 1] })))) as Shape);
const stub = own(unwrap(cut(own(cylinder(RC, c.screw, { at: [0, 0, c.to - c.screw] })), own(brick(4, 0.6, 1, 0, 0, c.to - c.slot)))) as Shape);
const clampSet = own(compound([clampTube, tapped(c.from, c.to), stub, helixTooth(RC, RM, M3.pitch, c.to - c.screw + FLANK, c.to - c.slot - FLANK, 0)]) as Shape);
const terminal = (ways: number, y: number): void => {
const pitch = 2 * GRID;
const x = BOARD.length / 2 - 5.5;
const at = (i: number): number => y + (i - (ways - 1) / 2) * pitch;
const tools = Array.from({ length: ways }, (_, i) => [
own(cylinder(1.5, 5, { at: [x, at(i), top + 3.6], axis: [1, 0, 0] })),
own(cylinder(1.9, 6, { at: [x - 1, at(i), top + 5] })),
]).flat();
part(orange, unwrap(cutAll(own(brick(8, ways * pitch, 10, x, y, top)), tools)) as Shape);
for (let i = 0; i < ways; i++) part(steel, translate(clampSet, [x - 1, at(i), top]) as Shape);
};
terminal(6, -24);
terminal(3, 30);
// Electrolytic capacitors: a sleeve and an aluminium top.
for (const [x, y, r, h] of [[53, 36, 5, 12.5], [41, 37, 4, 11], [61, -2, 3.2, 8], [-22, 42, 5, 12.5]] as const) {
part(dark, cylinder(r, h - 0.4, { at: [x, y, top] }));
part(steel, cylinder(r - 0.6, 0.4, { at: [x, y, top + h - 0.4] }));
}
// Voltage regulator on a finned heat sink.
const fins: P2[] = [[-12, 0], [12, 0], [12, 9]];
for (let i = 0; i < 5; i++) fins.push([10 - i * 4.4, 9], [10 - i * 4.4, 2.5], [7.6 - i * 4.4, 2.5], [7.6 - i * 4.4, 9]);
fins.push([-12, 9]);
part(steel, slab(fins.map(([a, b]): P2 => [a + 24, b + 33]), top, 24));
part(steel, unwrap(cut(own(brick(10, 1.3, 16, 24, 32.35, top + 4)), own(cylinder(1.8, 3, { at: [24, 31, top + 17], axis: [0, 1, 0] })))) as Shape);
part(dark, brick(10, 3.2, 9, 24, 30.1, top + 4));
for (const dx of [-GRID, 0, GRID]) part(steel, brick(0.8, 0.5, 4, 24 + dx, 30.6, top));
// Relays, crystal, coin cell in its holder, indicator lamps, resistors, pin header and a push button.
for (const y of [-17, -36]) part(orange, unwrap(fillet(own(brick(19, 15, 15.5, 56, y, top)), edgeFinder().inDirection('Z'), 1)) as Shape);
part(steel, unwrap(fillet(own(brick(4.7, 11, 3.5, 47, 6, top)), edgeFinder().inDirection('Z'), 2.2)) as Shape);
part(dark, unwrap(cut(own(cylinder(CELL.diameter / 2 + 1.5, 2.6, { at: [-52, 30, top] })), own(cylinder(CELL.diameter / 2, 3, { at: [-52, 30, top + 1] })))) as Shape);
part(steel, cylinder(CELL.diameter / 2, CELL.height, { at: [-52, 30, top + 1] }));
[0, 1, 2].forEach((i) => {
const lamp = i === 1 ? white : orange;
part(lamp, unwrap(fuseAll([own(cylinder(1.5, 4, { at: [73, 4 + i * 6, top] })), own(sphere(1.5, { at: [73, 4 + i * 6, top + 4] }))])) as Shape);
});
for (let i = 0; i < 8; i++) part(brass, cylinder(1.15, 6.3, { at: [-8 - i * 3.2, 27, top + 1.15], axis: [0, 1, 0] }));
part(dark, brick(20 * GRID, 2 * GRID, 2.5, 10, -44, top));
for (let i = 0; i < 20; i++) for (const row of [-0.5, 0.5]) part(brass, brick(0.64, 0.64, 6, 10 + (i - 9.5) * GRID, -44 + row * GRID, top + 2.5));
part(dark, brick(6, 6, 3.5, -62, -38, top));
part(orange, cylinder(1.75, 1.5, { at: [-62, -38, top + 3.5] }));
return {
base,
feet: compound([...feet.map(own), ...grommets]) as Shape,
lid,
board,
groups: [dark, steel, brass, orange, white].map((list) => compound(list) as Shape),
grams,
};
}
const built = freeing(() => enclosure());
console.log('ABS shells: ' + built.grams.toFixed(0) + ' g from the kernel volumes of the base and the whole lid.');
export default [
color(built.base, '#c3c9d1'),
color(built.feet, '#22262b'),
color(built.lid, '#f5f2ea'),
color(built.board, '#12b5b0'),
...built.groups.map((g, i) => color(g, ['#22262b', '#e6e9ee', '#d3a956', '#ff5a1f', '#f5f2ea'][i]!)),
];Functions used
box compound cut cutAll cylinder edgeFinder extrude fillet fuseAll line loft measureVolume polygon revolve rotate sphere translate unwrap wire color