Gridfinity Bin

A Gridfinity bin with dividers, label tabs and a scoop, on a section of magnet baseplate with a second bin and one free socket. The 42 mm grid, the 7 mm height unit, the foot, socket and stacking lip profiles, the magnet and screw holes and the M3 thread are published figures. The loose screws and nuts in the bays have true helical threads. The kernel measures the size of the bin and its plastic volume. The walls, the tabs, the scoop, the labels and the hardware in the bays 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, extrude, face, fuseAll, getBounds, line, loft, measureVolume, polygon, revolve, rotate, threePointArc, translate, unwrap, wire } from 'brepjs/quick';
import { color } from 'brepjs/playground';
// Gridfinity bin with dividers, label tabs and a scoop, on a section of magnet baseplate.
// The grid, the height unit, the foot and lip profiles, the holes and the M3 thread are published figures.
// The walls, the tabs, the scoop, the labels, the plate floor and the hardware are a display model. Units are millimetres.
interface BinParams {
cols?: number; // grid cells across (Y)
rows?: number; // grid cells front to back (X)
units?: number; // height in units of 7 mm, 2 or more. Try 6
lift?: number; // millimetres to lift the bins off the plate (0 = seated). Try 15
}
type P2 = [number, number];
type P3 = [number, number, number];
type Shape = ReturnType<typeof box>;
// 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 (Gridfinity)
const GRID = 42; // cell pitch
const UNIT = 7; // height unit
const BIN = { size: 41.5, radius: 3.75 }; // one cell of a bin: 0.25 clearance on each side
const FOOT: P2[] = [[2.95, 0], [2.15, 0.8], [2.15, 2.6], [0, 4.75]]; // [inset, z]: 0.8 at 45 deg, 1.8 up, 2.15 at 45 deg
const SOCKET: P2[] = [[2.85, 0], [2.15, 0.7], [2.15, 2.5], [0, 4.65]]; // baseplate: 0.7 at 45 deg, 1.8 up, 2.15 at 45 deg
const SOCKET_RADIUS = 4;
const LIP = { slopes: [0.7, 1.8, 1.9], fillet: 0.6 }; // stacking lip: 0.7 at 45 deg, 1.8 up, 1.9 at 45 deg, round tip
const HOLE = { pitch: 26, magnet: 6.5, magnetDepth: 2.4, screw: 3 }; // four holes in each cell
const MAGNET = { diameter: 6, height: 2 };
const PLA = 1.24; // grams per cubic centimetre
const M3 = { major: 3, minor: 2.459, pitch: 0.5, head: 5.5, headHeight: 3, socket: 2.5, length: 10, nutFlats: 5.5, nutHeight: 2.4 }; // cap screw and hex nut, metric coarse thread, right hand
// ---- display model
const WALL = 0.95;
const DIVIDER = 1.2;
const COMPARTMENTS = 3;
const TAB = { depth: 15.85, angle: 36, tip: 1.2 };
const LIP_SUPPORT = 1.2;
const SCREW_DEPTH = 6;
const FLOOR = 3.6; // plate under the sockets
const LABEL = 0.3;
// A rounded rectangle wire at height z.
const ring = (cx: number, cy: number, w: number, d: number, r: number, z: number) => {
const x0 = cx - w / 2, x1 = cx + w / 2, y0 = cy - d / 2, y1 = cy + d / 2, k = r * (1 - Math.SQRT1_2);
const corner = (ax: number, ay: number, sx: number, sy: number, first: boolean) => {
const a: P3 = [ax, ay + sy * r, z], b: P3 = [ax + sx * r, ay, z], m: P3 = [ax + sx * k, ay + sy * k, z];
return own(first ? threePointArc(a, m, b) : threePointArc(b, m, a));
};
return own(unwrap(wire([
own(line([x0 + r, y0, z], [x1 - r, y0, z])), corner(x1, y0, -1, 1, false),
own(line([x1, y0 + r, z], [x1, y1 - r, z])), corner(x1, y1, -1, -1, true),
own(line([x1 - r, y1, z], [x0 + r, y1, z])), corner(x0, y1, 1, -1, false),
own(line([x0, y1 - r, z], [x0, y0 + r, z])), corner(x0, y0, 1, 1, true),
])));
};
// A stack of rounded rectangles, each inset from the size w x d, with straight walls between them.
const stack = (cx: number, cy: number, w: number, d: number, radius: number, steps: P2[]): Shape =>
own(unwrap(loft(steps.map(([inset, z]) => ring(cx, cy, w - 2 * inset, d - 2 * inset, Math.max(radius - inset, 0.05), z)), { ruled: true })) as Shape);
// A profile in the (x, z) plane, pulled along Y. The point at index mid, if given, is the middle of an arc.
const prism = (pts: P2[], y0: number, y1: number, mid = -1): Shape => {
const p = pts.map(([x, z]): P3 => [x, y0, z]);
const edges = p.flatMap((a, i) => (i === mid ? [] : [own(i + 1 === mid ? threePointArc(a, p[mid]!, p[(i + 2) % p.length]!) : line(a, p[(i + 1) % p.length]!))]));
return own(unwrap(extrude(own(unwrap(face(own(unwrap(wire(edges))) as never))), [0, y1 - y0, 0])) as Shape);
};
const lathe = (pts: P2[]): Shape =>
own(unwrap(revolve(own(unwrap(polygon(pts.map(([r, z]): P3 => [r, 0, z])))), { axis: [0, 0, 1] })) as Shape);
const at = (s: Shape, x: number, y: number, z = 0): Shape => own(translate(s, [x, y, z]) as Shape);
// Cell centres of a grid, and the four hole positions of each cell.
const grid = (cols: number, rows: number, x0: number, y0: number): P2[] =>
Array.from({ length: rows * cols }, (_, k): P2 => [x0 + Math.floor(k / cols) * GRID, y0 + (k % cols) * GRID]);
const corners = (cells: P2[]): P2[] =>
cells.flatMap(([x, y]) => [-1, 1].flatMap((u) => [-1, 1].map((v): P2 => [x + (u * HOLE.pitch) / 2, y + (v * HOLE.pitch) / 2])));
// One helical thread tooth about +Z, from z = from to z = to. Its crest passes the +X side at z = ref.
// The tooth is a row of smooth lofts of 4 turns or less, end to end on one helix: a long loft is slow to mesh.
// tip > core is the thread of a screw on a core of that radius. tip < core is the thread in the bore of a nut.
const SECTIONS = 12; // loft sections in each turn
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): Shape => {
const root = tip > core ? core / SLACK : core * SLACK;
const crest = tip > core ? tip : tip / SLACK;
const half = 0.42 * pitch;
const start = from + half, span = to - from - 2 * half, pieces = Math.ceil(span / (4 * pitch));
const n = Math.max(2, Math.round((span / pieces / pitch) * SECTIONS)); // sections in each piece
const section = (z: number) => {
const a = (2 * Math.PI * (z - ref)) / pitch;
const pts = [[root, -half], [crest, 0], [root, half]].map(([r, dz]): P3 => [r! * Math.cos(a), r! * Math.sin(a), z + dz!]);
return own(unwrap(wire(pts.map((q, i) => own(line(q, pts[(i + 1) % 3]!))))));
};
const lofts = Array.from({ length: pieces }, (_, k) => own(unwrap(loft(Array.from({ length: n + 1 }, (_, j) => section(start + (span * (k + j / n)) / pieces)), { ruled: false }))));
return own(compound(lofts) as Shape);
};
const hexagon = (flats: number, z0: number, height: number): Shape =>
own(unwrap(extrude(own(unwrap(polygon(Array.from({ length: 6 }, (_, i): P3 => [(flats / Math.sqrt(3)) * Math.cos((i * Math.PI) / 3), (flats / Math.sqrt(3)) * Math.sin((i * Math.PI) / 3), z0])))), height)) as Shape);
// Points on an arc in a profile, from one angle to another in degrees.
const arc = (cx: number, cz: number, r: number, from: number, to: number, n: number): P2[] =>
Array.from({ length: n + 1 }, (_, i): P2 => {
const a = ((from + ((to - from) * i) / n) * Math.PI) / 180;
return [cx + r * Math.cos(a), cz + r * Math.sin(a)];
});
// One bin of cols x rows cells and a height of units, with its lowest cell centre at (x0, y0).
function makeBin(cols: number, rows: number, units: number, bays: number, x0: number, y0: number) {
const H = units * UNIT; // top of the wall. The lip stands above it
const W = { x: rows * GRID - 0.5, y: cols * GRID - 0.5 };
const mx = x0 + ((rows - 1) * GRID) / 2, my = y0 + ((cols - 1) * GRID) / 2; // centre of the bin
const cells = grid(cols, rows, x0, y0);
// Outside: one foot for each cell, then the wall up to the round tip of the lip.
const [l0, l1, l2] = [LIP.slopes[0]!, LIP.slopes[1]!, LIP.slopes[2]!];
const tip = H + l0 + l1 + l2; // where the lip slope meets the outside face
const zc = tip - LIP.fillet * (1 + Math.SQRT2); // centre height of the tip fillet
const feet = cells.map(([x, y]) => stack(x, y, BIN.size, BIN.size, BIN.radius, FOOT));
const wall = stack(mx, my, W.x, W.y, BIN.radius, [[0, FOOT[3]![1]], ...arc(LIP.fillet, zc, LIP.fillet, 180, 90, 4)]);
const outside = own(unwrap(fuseAll([wall, ...feet])) as Shape);
// Inside: the cavity under the lip profile, less the dividers, the tab and the scoop.
const inner = l0 + l2; // the lip overhangs the wall by 2.6
const cavity = stack(mx, my, W.x, W.y, BIN.radius, [
[WALL, UNIT], [WALL, H - LIP_SUPPORT - (inner - WALL)], [inner, H - LIP_SUPPORT], [inner, H], [l2, H + l0], [l2, H + l0 + l1],
...arc(LIP.fillet, zc, LIP.fillet, 45, 80, 2), [LIP.fillet * (1 + Math.cos((80 * Math.PI) / 180)), zc + LIP.fillet + 1],
]);
const xf = mx + W.x / 2 - WALL, xb = mx - W.x / 2 + inner; // inside faces: tab wall, scoop wall
const ya = my - W.y / 2 - 1, yb = my + W.y / 2 + 1;
const drop = Math.tan((TAB.angle * Math.PI) / 180) * (TAB.depth + 1);
const tab = prism([[xf + 1, H], [xf - TAB.depth, H], [xf - TAB.depth, H - TAB.tip], [xf + 1, H - TAB.tip - drop]], ya, yb);
const R = Math.min(H - UNIT - 4, 16); // scoop radius
const k = R * (1 - Math.SQRT1_2);
const scoop = prism([[xb - 4, UNIT - 1], [xb + R, UNIT - 1], [xb + R, UNIT], [xb + k, UNIT + k], [xb, UNIT + R], [xb, H], [xb - 4, H]], ya, yb, 3);
const span = (W.y - 2 * WALL - (bays - 1) * DIVIDER) / bays; // width of one compartment
const bay = (n: number): number => my - W.y / 2 + WALL + n * (span + DIVIDER); // its low Y
const dividers = Array.from({ length: bays - 1 }, (_, n) => at(own(box(W.x, DIVIDER, H - UNIT + 1)), mx - W.x / 2, bay(n) + span, UNIT - 1));
const hollow = own(unwrap(cutAll(cavity, [tab, scoop, ...dividers])) as Shape);
// Holes under each foot: a magnet pocket and a screw hole.
const drill = lathe([[0, -1], [HOLE.magnet / 2, -1], [HOLE.magnet / 2, HOLE.magnetDepth], [HOLE.screw / 2, HOLE.magnetDepth], [HOLE.screw / 2, SCREW_DEPTH], [0, SCREW_DEPTH]]);
const holes = corners(cells);
const solid = own(unwrap(cutAll(outside, [hollow, ...holes.map(([x, y]) => at(drill, x, y))])) as Shape);
// A paper label on the tab of each compartment.
const reach = xf - TAB.depth + 1.5;
const labels = Array.from({ length: bays }, (_, n) => at(own(box(mx + W.x / 2 - inner - 1 - reach, span - 5, LABEL)), reach, bay(n) + 2.5, H));
return { solid, holes, labels, span, bay, floor: [xb + R + 1, xf - TAB.depth - 1] as P2, room: H - UNIT };
}
function gridfinity({ cols = 2, rows = 2, units = 3, lift = 0 }: BinParams = {}) {
const high = Math.max(2, Math.round(units));
const x0 = (-rows * GRID) / 2, y0 = (-(cols - 1) * GRID) / 2; // the plate has one more row of sockets, in front of the bin
const main = makeBin(cols, rows, high, COMPARTMENTS, x0, y0);
const small = makeBin(1, 1, high, 1, x0 + rows * GRID, y0 + (cols - 1) * GRID); // a 1 x 1 bin in the front row
const up = (s: Shape): Shape => translate(s, [0, 0, lift]) as Shape;
// Baseplate: a socket for each cell, with a magnet pocket and a screw hole in each corner.
const depth = SOCKET[3]![1];
const outline = ring(0, 0, (rows + 1) * GRID, cols * GRID, SOCKET_RADIUS, -FLOOR);
const slab = own(unwrap(extrude(own(unwrap(face(outline as never))), FLOOR + depth)) as Shape);
const cells = grid(cols, rows + 1, x0, y0);
const sockets = cells.map(([x, y]) => stack(x, y, GRID, GRID, SOCKET_RADIUS, [...SOCKET, [0, depth + 1]]));
const pocket = lathe([[0, -FLOOR - 1], [HOLE.screw / 2, -FLOOR - 1], [HOLE.screw / 2, -HOLE.magnetDepth], [HOLE.magnet / 2, -HOLE.magnetDepth], [HOLE.magnet / 2, 1], [0, 1]]);
const base = unwrap(cutAll(slab, [...sockets, ...corners(cells).map(([x, y]) => at(pocket, x, y))])) as Shape;
// Magnets: pressed to the end of each pocket, so a pair stays 0.4 apart.
const magnet = own(cylinder(MAGNET.diameter / 2, MAGNET.height));
const gap = HOLE.magnetDepth - MAGNET.height;
const magnets = [...corners(cells).map(([x, y]) => at(magnet, x, y, -HOLE.magnetDepth)), ...[...main.holes, ...small.holes].map(([x, y]) => at(magnet, x, y, gap + lift))];
// Hardware on the floor of the bays, clear of the scoop and the tab: screws, bearings, nuts, and again.
const [fx, fx1] = main.floor;
const rowsOf = (pitch: number, n: number): number[] => Array.from({ length: n }, (_, i) => main.span / 2 + (i - (n - 1) / 2) * pitch);
const fits = (length: number, pitch: number): number => Math.max(0, Math.floor((length + 0.01) / pitch));
// Loose M3 x 10 cap screws: a hex socket, and one helical tooth on the core. The nuts have the mating tooth, half a pitch away.
const k = M3.headHeight, end = k + M3.length;
const screwBody = own(unwrap(cut(lathe([[0, 0], [M3.head / 2, 0], [M3.head / 2, k], [M3.minor / 2, k], [M3.minor / 2, end], [0, end]]), hexagon(M3.socket, -1, 2.6))) as Shape);
const screw = own(rotate(own(compound([screwBody, helixTooth(M3.minor / 2, M3.major / 2, M3.pitch, k, end, 0)]) as Shape), 90, { axis: [0, 1, 0] }) as Shape);
const bearing = lathe([[4, 0], [5.9, 0], [5.9, 0.6], [8.8, 0.6], [8.8, 0], [11, 0], [11, 7], [8.8, 7], [8.8, 6.4], [5.9, 6.4], [5.9, 7], [4, 7]]);
const nutBody = own(unwrap(cut(hexagon(M3.nutFlats, 0, M3.nutHeight), own(cylinder(M3.major / 2, M3.nutHeight)))) as Shape);
const nut = own(compound([nutBody, helixTooth(M3.major / 2, M3.minor / 2, M3.pitch, 0, M3.nutHeight, M3.pitch / 2)]) as Shape);
const dark: Shape[] = [], bright: Shape[] = [];
for (let n = 0; n < COMPARTMENTS; n++) {
const y = main.bay(n), kind = n % 3;
if (kind === 0) for (let i = 0; i < fits(fx1 - fx, 15); i++) for (const dy of rowsOf(6.5, fits(main.span - 1, 6.5))) dark.push(at(screw, fx + 1 + i * 15, y + dy, UNIT + M3.head / 2 + lift));
if (kind === 1) for (let i = 0; i < fits(fx1 - fx, 22.5); i++) for (let k = 0; k < Math.min(2, fits(main.room, 7)); k++) if (main.span > 23) bright.push(at(bearing, fx + 11.25 + i * 22.5, y + main.span / 2, UNIT + 7 * k + lift));
if (kind === 2) for (let i = 0; i < fits(fx1 - fx, 7.5); i++) for (const dy of rowsOf(7.5, fits(main.span - 1, 7.5))) bright.push(at(own(rotate(nut, 17 * (i + dy)) as Shape), fx + 3.75 + i * 7.5, y + dy, UNIT + lift));
}
return {
base, bin: up(main.solid), small: up(small.solid),
hardware: [dark, bright].map((group) => (group.length ? [compound(group) as Shape] : [])), // an empty bay has no hardware
magnets: compound(magnets) as Shape,
labels: compound([...main.labels, ...small.labels].map((s) => own(up(s)))) as Shape,
cm3: unwrap(measureVolume(main.solid)) / 1000, size: getBounds(main.solid), cols, rows, units: high, span: main.span,
};
}
const part = freeing(() => gridfinity());
const b = part.size;
console.log(
'Bin ' + part.cols + ' x ' + part.rows + ' x ' + part.units + ': ' + (b.yMax - b.yMin).toFixed(1) + ' x ' + (b.xMax - b.xMin).toFixed(1) + ' x ' + (b.zMax - b.zMin).toFixed(2) +
' mm from the kernel bounds. ' + part.cm3.toFixed(1) + ' cm3 of plastic, ' + (part.cm3 * PLA).toFixed(0) + ' g if printed solid in PLA. ' + COMPARTMENTS + ' compartments of ' + part.span.toFixed(1) + ' mm.',
);
export default [
color(part.base, '#48515c'), color(part.bin, '#12b5b0'), color(part.small, '#ff5a1f'), color(part.magnets, '#c3c9d1'), color(part.labels, '#f5f2ea'),
...part.hardware[0]!.map((s) => color(s, '#2b2f36')), ...part.hardware[1]!.map((s) => color(s, '#c3c9d1')),
];Functions used
box compound cut cutAll cylinder extrude face fuseAll getBounds line loft measureVolume polygon revolve rotate threePointArc translate unwrap wire color