Finger-Tab Split Joint

A 3D-printed keyboard case that is too long for the print bed, cut in two across a comb of square fingers and screwed together through two lugs with brass inserts. The key pitch, the switch cutout, the plate thickness and the M3 thread are published figures. The screws and the inserts have true helical threads, and the explode parameter turns each screw once for each 0.5 mm that it comes out. The kernel bounds give the length of the whole case and of each half. The case, the 16 x 5 key grid, the fingers and the lugs are a display model.
Code
This is the full program. The playground runs it in your browser without a change.
import { box, compound, cutAll, cylinder, edgeFinder, extrude, fillet, fuseAll, getBounds, intersect, line, loft, polygon, rotate, translate, unwrap, wire } from 'brepjs/quick';
import { color } from 'brepjs/playground';
// Finger-tab split joint on a 3D-printed keyboard case. The case is too long for the print bed,
// so it is cut in two across a comb of fingers and screwed together through two lugs.
// The key pitch, the switch cutout, the plate thickness and the M3 thread are published figures.
// The case, the 16 x 5 key grid, the fingers and the lugs are a display model. Units are millimetres.
interface SplitParams {
explode?: number; // 0 = assembled, 1 = halves apart, screws out, plate lifted. Each screw turns once for each 0.5 mm.
fingers?: number; // fingers across the floor, an odd number from 3 to 9
plate?: boolean; // false hides the switch plate
}
type P2 = [number, number];
type P3 = [number, 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 PITCH = 19.05; // key pitch, 3/4 in
const CUTOUT = 14; // square switch hole in the plate
const PLATE = 1.5; // plate thickness
const M3 = { major: 3, minor: 2.459, pitch: 0.5 }; // ISO metric coarse thread, right-hand
// ---- display model
const KEYS = { columns: 16, rows: 5 };
const BED = 220; // print bed of the example printer
const CASE = { wall: 5, gap: 0.5, floor: 3, height: 20, corner: 4 };
const SEAM = { reach: 14, fit: 0.2 }; // finger length, and the side clearance of a printed fit
const LUG = { length: 9, width: 8, height: 6 }; // each half has one at each wall
const POST = { radius: 3.2, height: 6 }; // plate standoff
const INSERT = { radius: 2.2, length: 4 }; // brass heat-set insert, M3
const PLAY = 0.06; // radial clearance between the screw thread and the insert thread
const SCREW = { head: 2.75, headHeight: 3, clear: 1.7 }; // M3 socket head
const block = (x0: number, x1: number, y0: number, y1: number, z0: number, z1: number): Shape =>
own(box(x1 - x0, y1 - y0, z1 - z0, { at: [(x0 + x1) / 2, (y0 + y1) / 2, (z0 + z1) / 2] }));
const rodX = (r: number, x0: number, x1: number, y: number, z: number): Shape => own(cylinder(r, x1 - x0, { at: [x0, y, z], axis: [1, 0, 0] }));
const rodZ = (r: number, x: number, y: number, z0: number, z1: number): Shape => own(cylinder(r, z1 - z0, { at: [x, y, z0] }));
// One helical thread tooth about Z, from z0 to z1. 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 bore: ref is half a pitch away.
// The loft is smooth, so each flank is one helical face. A long loft drifts off the helix: a long thread is pieces of 4 turns.
const SECTIONS = 12; // sections of the loft in each turn
const SLACK = 0.998; // keeps the tooth of a screw a hair clear of its core
const BORE_SLACK = 0.99; // and the tooth in a bore clear of the wall
const helixTooth = (core: number, tip: number, pitch: number, z0: number, z1: number, ref: number): Shape => {
const root = tip > core ? core / SLACK : core * BORE_SLACK;
const crest = tip > core ? tip : tip / SLACK;
const half = 0.42 * pitch;
const piece = (from: number, to: number): Shape => {
const n = Math.max(2, Math.round(((to - from) / pitch) * SECTIONS));
const sections = Array.from({ length: n + 1 }, (_, j) => {
const z = from + ((to - from) * j) / n;
const a = 2 * Math.PI * ((z - ref) / pitch);
const at = (r: number, dz: number): P3 => [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 unwrap(loft(sections, { ruled: false })) as Shape;
};
const count = Math.max(1, Math.ceil((z1 - z0) / (4 * pitch) - 0.001));
const pieces = Array.from({ length: count }, (_, i) => piece(z0 + ((z1 - z0) * i) / count, z0 + ((z1 - z0) * (i + 1)) / count));
return count === 1 ? pieces[0]! : (compound(pieces.map(own)) as Shape);
};
function splitCase({ explode = 0.75, fingers = 5, plate = true }: SplitParams = {}) {
const lift = Math.min(1, Math.max(0, explode));
const count = Math.min(9, Math.max(3, 2 * Math.floor(fingers / 2) + 1));
const inner = { x: (KEYS.columns * PITCH) / 2 + CASE.gap, y: (KEYS.rows * PITCH) / 2 + CASE.gap };
const outer = { x: inner.x + CASE.wall, y: inner.y + CASE.wall };
const top = CASE.floor + POST.height; // underside of the plate
const half = SEAM.reach / 2;
// The whole case: a tray with round corners, standoffs for the plate and a lug block at each wall.
const blank = block(-outer.x, outer.x, -outer.y, outer.y, 0, CASE.height);
const tray = own(unwrap(cutAll(own(unwrap(fillet(blank, edgeFinder().inDirection('Z').findAll(blank), CASE.corner))), [block(-inner.x, inner.x, -inner.y, inner.y, CASE.floor, CASE.height + 1)]))) as Shape;
const posts: P2[] = [-7, -3, 3, 7].flatMap((u) => [-1.5, 1.5].map((v): P2 => [u * PITCH, v * PITCH])); // on key corners
const lugY = [-1, 1].map((s) => s * (inner.y - LUG.width / 2));
const axisZ = CASE.floor + LUG.height / 2;
const solid = own(unwrap(fuseAll([
tray,
...posts.map(([x, y]) => rodZ(POST.radius, x, y, CASE.floor - 1, top)),
...lugY.map((y) => block(half - LUG.length, half + LUG.length, y - LUG.width / 2, y + LUG.width / 2 + 1, CASE.floor - 1, CASE.floor + LUG.height)),
]))) as Shape;
const whole = own(unwrap(cutAll(solid, [
...posts.map(([x, y]) => rodZ(INSERT.radius, x, y, top - INSERT.length, top + 1)),
...lugY.flatMap((y) => [
rodX(SCREW.clear, half - LUG.length - 1, half, y, axisZ),
rodX(INSERT.radius, half, half + INSERT.length, y, axisZ),
rodX(SCREW.clear, half + INSERT.length, half + LUG.length + 1, y, axisZ),
]),
block(-100, -90, outer.y - CASE.wall - 1, outer.y + 1, CASE.floor + 1, CASE.floor + 4.5), // port in the back wall
]))) as Shape;
// The seam in plan: a square wave across the floor. It is straight at the walls and the lugs.
const flat = inner.y - LUG.width - 0.5; // the fingers stay between the lugs
const step = (2 * flat) / count;
const edges = [-outer.y - 1, ...Array.from({ length: count + 1 }, (_, i) => -flat + i * step), outer.y + 1];
// Segment i is a finger of the left half when i is even. fit narrows the fingers of the right half.
const wave = (side: 1 | -1): P3[] => {
const far = side * (outer.x + 1);
const pts: P3[] = [[far, edges[0]!, -1]];
for (let i = 0; i < edges.length - 1; i++) {
const left = i % 2 === 0;
const grow = side > 0 ? (left ? SEAM.fit : -SEAM.fit) : 0;
const y0 = i === 0 ? edges[0]! : edges[i]! - grow;
const y1 = i === edges.length - 2 ? edges[i + 1]! : edges[i + 1]! + grow;
pts.push([left ? half : -half, y0, -1], [left ? half : -half, y1, -1]);
}
pts.push([far, edges[edges.length - 1]!, -1]);
return pts;
};
const mask = (side: 1 | -1): Shape => own(unwrap(extrude(own(unwrap(polygon(wave(side)))), [0, 0, CASE.height + 2]))) as Shape;
const left = own(unwrap(intersect(whole, mask(-1)))) as Shape;
const right = own(unwrap(intersect(whole, mask(1)))) as Shape;
// Explode in the order of the work: the plate screws come out first, then the halves part and the plate moves aside.
const apart = (26 * Math.max(0, lift - 0.1)) / 0.9;
const up: P3 = [0, (70 * Math.max(0, lift - 0.2)) / 0.8, lift * 60];
const seamOut = 2 * apart + lift * 34; // travel of a seam screw out of its insert
const plateOut = lift * 76; // travel of a plate screw out of its insert
// Hardware: inserts in the standoffs and the right lugs, a screw through each pair of lugs, four feet.
// An insert has its open end at z = 0 and its thread half a pitch from the thread of the screw.
const bore = M3.major / 2 + PLAY;
const insert = own(compound([
own(unwrap(cutAll(own(cylinder(INSERT.radius, INSERT.length, { at: [0, 0, -INSERT.length] })), [own(cylinder(bore, INSERT.length + 2, { at: [0, 0, -INSERT.length - 1] }))]))),
own(helixTooth(bore, M3.minor / 2 + PLAY, M3.pitch, -INSERT.length + 0.25, -0.25, M3.pitch / 2)),
])) as Shape;
const hex = (z0: number, z1: number): Shape => own(unwrap(extrude(own(unwrap(polygon(Array.from({ length: 6 }, (_, i): P3 => [1.45 * Math.cos((i * Math.PI) / 3), 1.45 * Math.sin((i * Math.PI) / 3), z0])))), [0, 0, z1 - z0]))) as Shape;
// One screw along +Z with the seat of the head at z = 0. It is out of its thread by a distance, so it has turned.
const screw = (len: number, out: number): Shape => {
const body = own(unwrap(cutAll(own(unwrap(fuseAll([own(cylinder(SCREW.head, SCREW.headHeight)), own(cylinder(M3.minor / 2, len + 1, { at: [0, 0, -len] }))]))), [hex(SCREW.headHeight - 1.8, SCREW.headHeight + 1)]))) as Shape;
const tooth = own(helixTooth(M3.minor / 2, M3.major / 2, M3.pitch, -len + 0.25, -0.25, 0));
return own(rotate(own(compound([body, tooth])), (360 * out) / M3.pitch) as Shape);
};
const at = (s: Shape, v: P3): Shape => own(translate(s, v) as Shape);
const sideways = (s: Shape): Shape => own(rotate(s, -90, { axis: [0, 1, 0] }) as Shape); // +Z to -X
const seamScrew = sideways(screw(2 * LUG.length, seamOut));
const plateScrew = screw(PLATE + INSERT.length, plateOut);
const feet = [-1, 1].flatMap((sx) => [-1, 1].map((sy) => rodZ(7, sx * (outer.x - 18), sy * (outer.y - 14), -2.5, 0)));
// Switch plate: one square hole for each key of the grid, and a hole at each standoff.
const holes = Array.from({ length: KEYS.columns * KEYS.rows }, (_, i) => {
const x = ((i % KEYS.columns) - (KEYS.columns - 1) / 2) * PITCH;
const y = (Math.floor(i / KEYS.columns) - (KEYS.rows - 1) / 2) * PITCH;
return block(x - CUTOUT / 2, x + CUTOUT / 2, y - CUTOUT / 2, y + CUTOUT / 2, top - 1, top + PLATE + 1);
});
const sheet = block(-inner.x + CASE.gap, inner.x - CASE.gap, -inner.y + CASE.gap, inner.y - CASE.gap, top, top + PLATE);
const switchPlate = plate ? [own(unwrap(cutAll(sheet, [...holes, ...posts.map(([x, y]) => rodZ(SCREW.clear, x, y, top - 1, top + PLATE + 1))]))) as Shape] : [];
// The kernel bounds say if a part fits the bed.
const span = (s: Shape): number => getBounds(s).xMax - getBounds(s).xMin;
const lengths = [span(whole), span(left), span(right)];
// Explode along the long axis. Then turn the case so that its long side faces +X.
const show = (shapes: Shape[], v: P3): Shape => rotate(own(translate(own(compound(shapes)), v)), 90) as Shape;
const onLeft = <T,>(items: T[], x: (item: T) => number): T[] => items.filter((item) => x(item) < 0);
const postInserts = posts.map(([x, y]) => ({ x, shape: at(insert, [x, y, top]) }));
return {
left: show([left, ...feet.slice(0, 2)], [-apart, 0, 0]),
right: show([right, ...feet.slice(2)], [apart, 0, 0]),
inserts: [
show(onLeft(postInserts, (p) => p.x).map((p) => p.shape), [-apart, 0, 0]),
show([...onLeft(postInserts, (p) => -p.x).map((p) => p.shape), ...lugY.map((y) => at(sideways(insert), [half, y, axisZ]))], [apart, 0, 0]),
],
screws: [
show(lugY.map((y) => at(seamScrew, [half - LUG.length, y, axisZ])), [apart - seamOut, 0, 0]),
...(plate ? [show(posts.map(([x, y]) => at(plateScrew, [x, y, top + PLATE])), [0, up[1], plateOut])] : []),
],
plate: switchPlate.map((s) => show([s], up)),
lengths,
};
}
const part = freeing(() => splitCase());
console.log('Whole case: ' + part.lengths[0]!.toFixed(1) + ' mm, too long for a ' + BED + ' mm bed. Halves: ' + part.lengths[1]!.toFixed(1) + ' mm and ' + part.lengths[2]!.toFixed(1) + ' mm, from the kernel bounds.');
export default [
color(part.left, '#12b5b0'),
color(part.right, '#f5f2ea'),
...part.inserts.map((s) => color(s, '#d3a956')),
...part.screws.map((s) => color(s, '#48515c')),
...part.plate.map((s) => color(s, '#c3c9d1')),
];Functions used
box compound cutAll cylinder edgeFinder extrude fillet fuseAll getBounds intersect line loft polygon rotate translate unwrap wire color