Mitered Motor Bracket

A mounting bracket for a NEMA 17 stepper motor, folded from one blank of 14 gauge steel: a face and two ribs fold up from the base, and each rib meets the face in a mitered corner. The gauge, the motor face, its hole pattern, its shaft and the screw threads are published figures. The M3 screws turn into the tapped holes of the motor and the M5 screws into nuts, with true helical threads. The kernel unfolds the bracket to the flat blank, with the bend allowance, and measures the mass. The motor and the pulley 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, clone, compound, cutAll, cylinder, extrude, line, loft, measureVolume, polygon, revolve, rotate, translate, unwrap, wire } from 'brepjs/quick';
import { addPolygonCutout, author, flatPatternToPolylines, miterCorner, toDXF, unfold } from 'brepjs-sheetmetal';
import { color, present } from 'brepjs/playground';
// Mounting bracket for a NEMA 17 stepper motor, folded from one blank of 14 gauge steel.
// The face and two ribs fold up from the base. Each rib meets the face in a mitered corner, with a gap for the weld.
// The gauge, the motor face, its hole pattern, its shaft and the screws are published figures. The rest is a display model.
interface BracketParams {
fold?: number; // 0 to 1: how far each bend is folded. Below 1 the example shows only the sheet, before the miter cuts
rib?: number; // height of the ribs in millimetres. Try 30
}
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.897; // 14 gauge steel sheet, 0.0747 inch
const STEEL = 7.85e-3; // grams per cubic millimetre
const MOTOR = { face: 42.3, holes: 31, screw: 3, shaft: 5, tapped: 4.5 }; // NEMA 17: four tapped M3 holes on a square of 31 mm
const M3 = { d: 3, minor: 2.459, pitch: 0.5, head: 5.5, k: 3, socket: 2.5, length: 6 }; // socket head cap screw, metric coarse thread, right hand
const M5 = { d: 5, minor: 4.134, pitch: 0.8, head: 8.5, k: 5, socket: 4, length: 8, nutFlats: 8, nutHeight: 4.7 }; // cap screw and hex nut
// ---- display model
const RULE = { innerRadius: 2, kFactor: 0.44 }; // air bend
const S = T + RULE.innerRadius; // a 90 degree bend takes this much of each leg
const BASE = { length: 62, width: 56 };
const FACE = 52; // flat height of the face
const AXIS = S + FACE / 2; // height of the motor axis
const PILOT = { diameter: 22, height: 2 }; // the boss on the motor that centres it in the face
const BODY = { front: 9, stack: 20, rear: 11, shaft: 24 }; // a motor of 40 mm
const MITER = 1; // gap setting of the miter cut
const SLOTS = [14, 42]; // Y of the two slots in the base
const PLAY = 0.08; // radial play between a screw thread and its mating thread
const made = (r: ReturnType<typeof author>): Part => {
const p = unwrap(r);
if (p.solid) own(p.solid);
return p;
};
const ngon = (x: number, y: number, r: number, n: number): P2[] =>
Array.from({ length: n }, (_, i): P2 => [x + r * Math.cos((i * 2 * Math.PI) / n), y + r * Math.sin((i * 2 * Math.PI) / n)]);
// 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)];
};
// The blank: the face and the ribs fold from the base.
function blank(rib: number, angle: number): Part {
const bend = { angleDeg: angle, rule: RULE };
let p = made(author({ thickness: T, base: BASE, flanges: [
{ id: 'face', side: 'xmin', length: FACE, ...bend },
{ id: 'ribLeft', side: 'ymin', length: rib, ...bend }, { id: 'ribRight', side: 'ymax', length: rib, ...bend },
] }));
// The miter is a cut on a vertical plane through the corner, so it is correct only at 90 degrees.
if (angle === 90) for (const id of ['ribLeft', 'ribRight']) p = made(miterCorner(p, id, 'face', MITER));
// Base: two slots for the fixing screws. Face: the bore for the pilot and the four motor holes.
for (const y of SLOTS) p = made(addPolygonCutout(p, 'base', slot(36, y, 26, 5.5)));
const mid = BASE.width / 2, h = MOTOR.holes / 2;
p = made(addPolygonCutout(p, 'face', ngon(mid, FACE / 2, PILOT.diameter / 2 + 0.25, 40)));
for (const [u, v] of [[-h, -h], [h, -h], [h, h], [-h, h]]) p = made(addPolygonCutout(p, 'face', ngon(mid + u!, FACE / 2 + v!, MOTOR.screw / 2 + 0.2, 16)));
return p;
}
const block = (x0: number, y0: number, z0: number, x1: number, y1: number, z1: number): Shape => own(translate(own(box(x1 - x0, y1 - y0, z1 - z0)), [x0, y0, z0])) as Shape;
const rod = (r: number, x0: number, x1: number, y: number, z: number): Shape => own(cylinder(r, x1 - x0, { at: [x0, y, z], axis: [1, 0, 0] })) as Shape;
// A solid of revolution about the X axis through (y, z). The profile is [x, r].
const turned = (profile: P2[], y: number, z: number): Shape =>
own(translate(own(unwrap(revolve(own(unwrap(polygon(profile.map(([x, r]): P3 => [x, r, 0])))), { axis: [1, 0, 0] }))), [0, y, z])) as Shape;
// 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 thread of a screw between two heights, and the mating thread of a nut or a tapped hole, half a pitch out of phase.
type Thread = { d: number; minor: number; pitch: number };
const screwTooth = (m: Thread, z0: number, z1: number): Shape => helixTooth(m.minor / 2, m.d / 2, m.pitch, z0, z1, 0);
const nutTooth = (m: Thread, z0: number, z1: number): Shape => helixTooth(m.d / 2 + PLAY, m.minor / 2 + PLAY, m.pitch, z0, z1, m.pitch / 2);
const hexPrism = (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])))), [0, 0, height]))) as Shape;
// A socket head cap screw along -Z, with the seat of its head at z = 0 and thread on its full length.
const capScrew = (m: typeof M3): Shape => {
const profile: P3[] = [[0, 0, m.k], [m.head / 2, 0, m.k], [m.head / 2, 0, 0], [m.minor / 2, 0, 0], [m.minor / 2, 0, -m.length], [0, 0, -m.length]];
const body = own(unwrap(cutAll(own(unwrap(revolve(own(unwrap(polygon(profile)))))) as Shape, [hexPrism(m.socket, m.k * 0.4, m.k)]))) as Shape;
return own(compound([body, screwTooth(m, -m.length, 0)])) as Shape;
};
// The motor is behind the face. Its pilot sits in the bore, and four screws go through the face into it.
function hardware() {
const mid = BASE.width / 2, f = MOTOR.face / 2, h = MOTOR.holes / 2, x = -S;
const spots: P2[] = [[-h, -h], [h, -h], [h, h], [-h, h]];
// A square section with cut corners, from x0 to x1.
const frame = (x0: number, x1: number, corner: number): Shape => {
const pts: P2[] = [[f - corner, -f], [f, -f + corner], [f, f - corner], [f - corner, f], [-f + corner, f], [-f, f - corner], [-f, -f + corner], [-f + corner, -f]];
return own(unwrap(extrude(own(unwrap(polygon(pts.map(([y, z]): P3 => [x0, mid + y, AXIS + z])))), [x1 - x0, 0, 0]))) as Shape;
};
const x1 = x - BODY.front, x2 = x1 - BODY.stack, x3 = x2 - BODY.rear;
const front = own(unwrap(cutAll(frame(x1, x, 4.5), spots.map(([u, v]) => rod(M3.d / 2 + PLAY, x - MOTOR.tapped, x, mid + u, AXIS + v))))) as Shape;
const alloy = [front, frame(x3, x2, 4.5), turned([[x, 0], [x, PILOT.diameter / 2], [x + PILOT.height, PILOT.diameter / 2], [x + PILOT.height, 0]], mid, AXIS)];
const dark = [frame(x2, x1, 3), block(x3, mid - 8, AXIS + f, x3 + 10, mid + 8, AXIS + f + 5.5)];
// The shaft, with a flat for the set screws of the pulley.
const shaft = own(unwrap(cutAll(rod(MOTOR.shaft / 2, x + PILOT.height, x + BODY.shaft, mid, AXIS), [block(x + 9, mid - 3, AXIS + 2, x + 30, mid + 3, AXIS + 3)]))) as Shape;
// A belt pulley with two flanges. Its bore is cut with the shaft, so the flat stays free.
const p0 = x + 9.5;
const pulleyBlank = turned([[p0, 0], [p0, 8], [p0 + 7.5, 8], [p0 + 7.5, 6.5], [p0 + 8.5, 6.5], [p0 + 8.5, 6], [p0 + 15, 6], [p0 + 15, 6.5], [p0 + 16, 6.5], [p0 + 16, 0]], mid, AXIS);
const pulley = own(unwrap(cutAll(pulleyBlank, [rod(MOTOR.shaft / 2, p0 - 1, p0 + 17, mid, AXIS), rod(1.5, 0, 9, 0, 0)]))) as Shape;
// Socket head screws with helical threads: four M3 x 6 into the tapped holes of the motor, and two M5 x 8 with a washer
// and a nut in the slots of the base. Each mating thread is made in the frame of its screw, so the two stay in mesh.
const alongX = (s: Shape): Shape => own(rotate(s, 90, { axis: [0, 1, 0] })) as Shape; // the head toward +X
const m3 = alongX(capScrew(M3));
const m3Hole = alongX(nutTooth(M3, -T - MOTOR.tapped, -T));
const m5 = capScrew(M5);
const nutTop = -1 - T; // under the washer and the sheet
const nutBody = own(unwrap(cutAll(hexPrism(M5.nutFlats, nutTop - M5.nutHeight, M5.nutHeight), [own(cylinder(M5.d / 2 + PLAY, M5.nutHeight + 2, { at: [0, 0, nutTop - M5.nutHeight - 1] }))]))) as Shape;
const nut = own(compound([nutBody, nutTooth(M5, nutTop - M5.nutHeight, nutTop)])) as Shape;
const washer = own(unwrap(revolve(own(unwrap(polygon([[2.65, 0, 0], [5, 0, 0], [5, 0, 1], [2.65, 0, 1]])))))) as Shape;
const steel = [
shaft, pulley,
...spots.map(([u, v]) => own(translate(m3, [x + T, mid + u, AXIS + v])) as Shape),
...SLOTS.flatMap((y) => [washer, m5, nut].map((s, i) => own(translate(s, [44, y, i === 0 ? T : T + 1])) as Shape)),
];
alloy.push(...spots.map(([u, v]) => own(translate(m3Hole, [x + T, mid + u, AXIS + v])) as Shape));
return { alloy: compound(alloy) as Shape, dark: compound(dark) as Shape, steel: compound(steel) as Shape };
}
function bracket({ fold = 1, rib = 18 }: BracketParams = {}) {
const part = blank(rib, 90 * Math.min(1, Math.max(0.02, fold)));
// The report and the flat blank come from the full bracket, also when the view is part folded.
const full = fold < 1 ? blank(rib, 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 info = {
dxf: unwrap(toDXF(flat.pattern)), overlay2d: flatPatternToPolylines(flat.pattern),
size: flat.report.totalFlatSize, allowance: flat.report.bends[0]?.allowance ?? 0, bends: flat.report.bends.length,
grams: unwrap(measureVolume(full.solid as Shape)) * STEEL,
};
const sheet = unwrap(clone(part.solid as Shape)) as Shape;
return fold < 1 ? { sheet, ...info } : { sheet, ...hardware(), ...info };
}
// Try bracket({ fold: 0.5 }) to see the blank half folded, or bracket({ rib: 30 }).
const model = freeing(() => bracket());
console.log(
model.bends + ' bends, each with an allowance of ' + model.allowance.toFixed(2) + ' mm. Flat blank: ' + model.size.map((v) => v.toFixed(1)).join(' x ') + ' mm. ' +
'Mass of the bracket: ' + model.grams.toFixed(0) + ' g from the kernel volume.',
);
// The DXF button downloads the flat blank with its bend lines, miters, slots and holes.
export default present(
[color(model.sheet, '#12b5b0'), ...('alloy' in model ? [color(model.alloy, '#c3c9d1'), color(model.dark, '#2b2f36'), color(model.steel, '#e9ebee')] : [])],
{ dxf: model.dxf, overlay2d: model.overlay2d },
);Functions used
box clone compound cutAll cylinder extrude line loft measureVolume polygon revolve rotate translate unwrap wire addPolygonCutout author flatPatternToPolylines miterCorner toDXF unfold color present