Hemmed Parts Tray

A parts tray from 16 gauge steel with a hemmed safe edge, in cutaway: the top of each wall folds back on itself, and the cut shows the section of two hems. Set the type of the hem to closed, open, teardrop or rolled. The gauge and the M6 bolts with their helical thread are published figures. The kernel gives the sheet that one hem takes, unfolds the tray to the flat blank and measures its mass. The size, the handles, the feet and the hardware in the tray are a display model. The DXF button downloads the flat blank.
Code
This is the full program. The playground runs it in your browser without a change.
import { box, compound, cut, cylinder, extrude, line, loft, measureVolume, polygon, revolve, rotate, translate, unwrap, wire } from 'brepjs/quick';
import { addPolygonCutout, author, flatPatternToPolylines, hem, relieveCorner, toDXF, unfold } from 'brepjs-sheetmetal';
import { color, present } from 'brepjs/playground';
// Parts tray with a hemmed safe edge, from 16 gauge steel, in cutaway. The top of each wall folds back on itself:
// the hem hides the sharp cut edge and makes the wall stiff. The cut shows the section of two hems.
// The gauge, the density and the M6 bolts are published figures. The size, the handles, the feet and the hardware in the tray are a display model.
interface TrayParams {
fold?: number; // 0 to 1: how far the four walls are folded
type?: 'closed' | 'open' | 'teardrop' | 'rolled'; // the shape of the hem
cutaway?: number; // 0 to 0.8: the part of the length to cut away (0 = whole tray)
}
type P2 = [number, number];
type P3 = [number, number, number];
type Shape = ReturnType<typeof box>;
type Part = Extract<ReturnType<typeof author>, { ok: true }>['value'];
// 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 T = 1.519; // 16 gauge steel sheet, 0.0598 inch
const STEEL = 7.85e-3; // grams per cubic millimetre
const M6 = { d: 6, minor: 4.917, pitch: 1, flats: 10, head: 4, length: 30, thread: 18 }; // hex bolt with a part thread, metric coarse, right hand
// ---- display model
const RULE = { innerRadius: 1.5, kFactor: 0.44 }; // air bend
const TRAY = { length: 150, width: 100, wall: 26 };
const HEM = { length: 8, inset: 6 }; // the return leg, and the distance from each end of a wall
const HANDLE = { length: 44, width: 12, y: 13 }; // a slot in each end wall
const FOOT = { radius: 7, height: 5, inset: 16 };
const made = (r: ReturnType<typeof author>): Part => {
const p = unwrap(r);
if (p.solid) own(p.solid);
return p;
};
// An obround slot along X, as a polygon.
const slot = (x: number, y: number, length: number, width: number): P2[] => {
const r = width / 2, d = length / 2 - r, n = 8;
const end = (cx: number, from: number): P2[] => Array.from({ length: n + 1 }, (_, i): P2 => [cx + r * Math.cos(from + (i * Math.PI) / n), y + r * Math.sin(from + (i * Math.PI) / n)]);
return [...end(x + d, -Math.PI / 2), ...end(x - d, Math.PI / 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 a bore.
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;
};
// The tray: four walls with a relief at each corner, a hem on each wall, and a handle slot in each end wall.
function tray(type: NonNullable<TrayParams['type']>, angle: number): Part {
const wall = { length: TRAY.wall, angleDeg: angle, rule: RULE };
let p = made(author({ thickness: T, base: { length: TRAY.length, width: TRAY.width }, flanges: [
{ id: 'left', side: 'xmin', ...wall }, { id: 'right', side: 'xmax', ...wall }, { id: 'front', side: 'ymin', ...wall }, { id: 'back', side: 'ymax', ...wall },
] }));
for (const [a, b] of [['left', 'front'], ['front', 'right'], ['right', 'back'], ['back', 'left']]) p = made(relieveCorner(p, a!, b!));
for (const [region, edge] of [['left', TRAY.width], ['right', TRAY.width], ['front', TRAY.length], ['back', TRAY.length]] as const) {
p = made(hem(p, { region, side: 'ymax', type, length: HEM.length, rule: RULE, offset: HEM.inset, width: edge - 2 * HEM.inset }));
}
for (const region of ['left', 'right']) p = made(addPolygonCutout(p, region, slot(TRAY.width / 2, HANDLE.y, HANDLE.length, HANDLE.width)));
return p;
}
function hemmedTray({ fold = 1, type = 'open', cutaway = 0.3 }: TrayParams = {}) {
const part = tray(type, 90 * Math.min(1, Math.max(0.02, fold)));
// The report and the flat blank come from the full tray, also when the view is part folded.
const full = fold < 1 ? tray(type, 90) : part;
const flat = unwrap(unfold(full));
[flat.pattern.outline, ...flat.pattern.holes, ...flat.pattern.bendLines.map((l) => l.line)].forEach((handle) => own(handle));
const overlay2d = flatPatternToPolylines(flat.pattern);
const xs = overlay2d.outline.map((p) => p[0]), ys = overlay2d.outline.map((p) => p[1]);
const info = {
dxf: unwrap(toDXF(flat.pattern)), overlay2d, type,
size: [Math.max(...xs) - Math.min(...xs), Math.max(...ys) - Math.min(...ys)],
hem: full.hems?.[0]?.developedLength ?? 0, allowance: flat.report.bends[0]?.allowance ?? 0,
grams: unwrap(measureVolume(full.solid as Shape)) * STEEL,
};
// Four rubber feet under the base.
const foot = own(unwrap(revolve(own(unwrap(polygon([[0, 0, 0], [FOOT.radius, 0, 0], [FOOT.radius - 1.5, 0, -FOOT.height], [0, 0, -FOOT.height]]))))) as Shape);
const spots: P2[] = [FOOT.inset, TRAY.length - FOOT.inset].flatMap((x) => [FOOT.inset, TRAY.width - FOOT.inset].map((y): P2 => [x, y]));
// The cutaway takes one end off the tray, and the feet under that end.
const edge = TRAY.length * (1 - Math.min(0.8, Math.max(0, cutaway)));
const feet = spots.filter(([x]) => cutaway <= 0 || x + FOOT.radius < edge).map(([x, y]) => own(translate(foot, [x, y, 0])) as Shape);
const far = own(translate(own(box(400, 600, 400)), [edge, -300, -200])) as Shape;
const steel = cutaway > 0 ? (unwrap(cut(part.solid as Shape, far)) as Shape) : (translate(part.solid as Shape, [0, 0, 0]) as Shape);
// Hardware in the tray, at the end that stays: three M6 x 30 hex bolts and a stack of washers.
// A bolt is made along Z: the head, the plain shank, then the core with one helical tooth of 18 turns. It lies on a flat of its head.
const r = M6.flats / Math.sqrt(3), plain = M6.length - M6.thread;
const bolt = own(rotate(own(compound([
own(unwrap(extrude(own(unwrap(polygon(Array.from({ length: 6 }, (_, i): P3 => [r * Math.cos((i * Math.PI) / 3), r * Math.sin((i * Math.PI) / 3), -M6.head])))), [0, 0, M6.head]))),
own(cylinder(M6.d / 2, plain)), own(cylinder(M6.minor / 2, M6.thread, { at: [0, 0, plain] })), helixTooth(M6.minor / 2, M6.d / 2, M6.pitch, plain, M6.length, 0),
])), -90, { axis: [1, 0, 0] })) as Shape;
const inTray = (x: number): boolean => cutaway <= 0 || x + 6 < edge;
const bolts = [28, 46, 64].filter(inTray).map((x) => own(translate(bolt, [x, 22 + M6.head, T + M6.flats / 2])) as Shape);
const washer = own(unwrap(revolve(own(unwrap(polygon([[3.2, 0, 0], [6, 0, 0], [6, 0, 1.6], [3.2, 0, 1.6]]))))) as Shape);
const washers = [0, 1, 2, 3].filter(() => inTray(46)).map((i) => own(translate(washer, [46, 78, T + i * 1.6])) as Shape);
const group = (list: Shape[]): Shape[] => (list.length ? [compound(list) as Shape] : []);
return { steel, feet: group(feet), parts: group([...bolts, ...washers]), ...info };
}
// Try hemmedTray({ type: 'teardrop' }), hemmedTray({ fold: 0.5 }) or hemmedTray({ cutaway: 0 }).
const model = freeing(() => hemmedTray());
console.log(
'A hem of the type ' + model.type + ' takes ' + model.hem.toFixed(2) + ' mm of sheet. Each wall bend has an allowance of ' + model.allowance.toFixed(2) + ' mm. ' +
'Flat blank: ' + model.size.map((v) => v.toFixed(1)).join(' x ') + ' mm. Mass of the tray: ' + model.grams.toFixed(0) + ' g from the kernel volume.',
);
// The DXF button downloads the flat blank of the whole tray, with the bend lines of the walls and the hems.
export default present([color(model.steel, '#12b5b0'), ...model.feet.map((s) => color(s, '#2b2f36')), ...model.parts.map((s) => color(s, '#e9ebee'))], { dxf: model.dxf, overlay2d: model.overlay2d });Functions used
box compound cut cylinder extrude line loft measureVolume polygon revolve rotate translate unwrap wire addPolygonCutout author flatPatternToPolylines hem relieveCorner toDXF unfold color present