@rulecad/cad 0.7.55 → 0.7.57
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/AGENTS.md +73 -7
- package/CHANGELOG.md +149 -0
- package/EXAMPLES.md +942 -268
- package/README.md +366 -16
- package/TUTORIAL.md +27 -15
- package/bin/cad.mjs +54 -14
- package/lib/assembly.mjs +75 -16
- package/lib/beams.mjs +2 -2
- package/lib/bought.mjs +41 -5
- package/lib/buildpage.mjs +8 -3
- package/lib/design.mjs +6 -4
- package/lib/edit.mjs +36 -5
- package/lib/fasten.mjs +322 -55
- package/lib/fittings.mjs +271 -0
- package/lib/framebuild.mjs +407 -0
- package/lib/frames.mjs +4 -3
- package/lib/guides.mjs +354 -0
- package/lib/jigs.mjs +237 -0
- package/lib/joins.mjs +259 -0
- package/lib/jsonedit.mjs +19 -0
- package/lib/list.mjs +24 -8
- package/lib/loads.mjs +9 -2
- package/lib/measured.mjs +64 -17
- package/lib/motors.mjs +28 -13
- package/lib/order.mjs +23 -12
- package/lib/panels.mjs +220 -0
- package/lib/plates.mjs +11 -4
- package/lib/schema.mjs +3 -2
- package/lib/services.mjs +132 -0
- package/lib/sheet.mjs +20 -3
- package/lib/slotnuts.mjs +159 -0
- package/lib/task.mjs +4 -2
- package/lib/wires.mjs +2 -1
- package/package.json +8 -7
- package/schemas/assembly.schema.json +41 -14
- package/schemas/object.schema.json +2 -2
package/lib/panels.mjs
ADDED
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
// Panels in assemblies (@rulecad/frames' panel.mjs and door.mjs): an item with "panel" is a sheet cut to the bay of a
|
|
2
|
+
// frame it closes: { "sheet": "acrylic", "thickness": 3, "bay": ["x1", "z1", "x2", "z2"], "out": [0, -1, 0] }. The bay
|
|
3
|
+
// is four beams placed by their ends, two each way, their slotted faces that look along "out" in one plane. Before the
|
|
4
|
+
// items are built, each panel is worked out: its size from the beams' slots, the item placed on that plane, and its
|
|
5
|
+
// printed clips placed at a pitch along each edge, each with a fasten into its beam's slot. With "door" it is a door:
|
|
6
|
+
// it stands off the faces on printed hinges along one beam, turns on a revolute joint (poses <id>-shut and <id>-open),
|
|
7
|
+
// has a handle and a magnet catch, and the screws through it are fastens with their holes cut in it. Units: mm.
|
|
8
|
+
import { panelModel, panel, panelClipModel, hingeModel, latchModel, magnetCatchModel, handleModel, PANEL_DEFAULTS, DOOR_DEFAULTS } from '@rulecad/frames';
|
|
9
|
+
import { beamOf, beamPlace, framesContext } from './beams.mjs';
|
|
10
|
+
import { givenFor } from './frames.mjs';
|
|
11
|
+
import { named } from './parts.mjs';
|
|
12
|
+
import { referenceBuilt } from './bought.mjs';
|
|
13
|
+
import { ops as booleans } from '@rulecad/shapes';
|
|
14
|
+
|
|
15
|
+
const KEYS = ['sheet', 'thickness', 'bay', 'out', 'pitch', 'nut', 'load', 'door'];
|
|
16
|
+
const DOOR_KEYS = ['hinge', 'open', 'handle', 'catch', 'latch', 'load'];
|
|
17
|
+
const shown = (v) => (v === undefined ? 'nothing' : JSON.stringify(v));
|
|
18
|
+
const mm = (x) => String(+Number(x).toFixed(2)), r3 = (x) => +x.toFixed(3);
|
|
19
|
+
const dot = (a, b) => a[0] * b[0] + a[1] * b[1] + a[2] * b[2], sub = (a, b) => [a[0] - b[0], a[1] - b[1], a[2] - b[2]], add = (a, b, k = 1) => [a[0] + k * b[0], a[1] + k * b[1], a[2] + k * b[2]];
|
|
20
|
+
const cross = (a, b) => [a[1] * b[2] - a[2] * b[1], a[2] * b[0] - a[0] * b[2], a[0] * b[1] - a[1] * b[0]];
|
|
21
|
+
const unit = (v) => { const l = Math.hypot(...v); return v.map((c) => c / l); };
|
|
22
|
+
const col = (f, k) => [f[k], f[4 + k], f[8 + k]], org = (f) => [f[3], f[7], f[11]];
|
|
23
|
+
const world = (f, p) => [0, 1, 2].map((k) => f[4 * k] * p[0] + f[4 * k + 1] * p[1] + f[4 * k + 2] * p[2] + f[4 * k + 3]);
|
|
24
|
+
const turn = (f, v) => [0, 1, 2].map((k) => f[4 * k] * v[0] + f[4 * k + 1] * v[1] + f[4 * k + 2] * v[2]);
|
|
25
|
+
// a world point and direction in a frame's own coordinates (its rows' transpose)
|
|
26
|
+
const local = (f, p) => { const q = sub(p, org(f)); return [0, 1, 2].map((k) => r3(dot(col(f, k), q))); };
|
|
27
|
+
const localDir = (f, v) => [0, 1, 2].map((k) => r3(dot(col(f, k), v)));
|
|
28
|
+
const near = (a, b) => Math.abs(a - b) < 1e-6;
|
|
29
|
+
|
|
30
|
+
/** The working figures of panels in assemblies, each with what it rests on. */
|
|
31
|
+
export const PANEL_CHECK = {
|
|
32
|
+
plane: { value: 0.05, unit: 'mm', rule: 'A bay\'s four faces lie in one plane when they are within this of each other: a panel lies flat on all four', kind: 'default', source: 'A working figure of @rulecad/cad, the same as a join\'s flush', checked: '2026-11-17' },
|
|
33
|
+
latch_probe: { value: 0.5, unit: '°', rule: 'A door with its latch shut is also swept this far off shut, finer than the sweep\'s steps: the latch\'s tab stands just over the door\'s edge, so the door hits it within its first degree and a coarse step would jump past it', kind: 'default', source: 'A working figure of @rulecad/cad: the panels test sees the hit from 0.5° on a 400 x 300 mm door', checked: '2026-11-17' },
|
|
34
|
+
inset: { value: 2, unit: 'mm', rule: 'A clip or a hinge stands this far clear of the beam that crosses the end of its beam, past that beam\'s half width or the reach of its clips', kind: 'default', source: 'A working figure of @rulecad/cad', checked: '2026-11-17' },
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* An assembly's panels worked out before its items are built: each panel item placed on its bay's plane, with its
|
|
39
|
+
* size and holes; its clips (or a door's hinges, handle and catch) added as items with their fastens; a door's joint
|
|
40
|
+
* and its two poses. Refused by name: a bay that is not four beams round a rectangle, faces out of one plane, a
|
|
41
|
+
* beam with no slotted face that looks along "out", and what @rulecad/frames refuses.
|
|
42
|
+
* @param {object} a the assembly as read @param {(x: any) => number} num @param {object} [context] framesContext()'s @param {string} [material]
|
|
43
|
+
* @returns {{ a: object, panels: object[] }}
|
|
44
|
+
*/
|
|
45
|
+
export function expandPanels(a, num, context = framesContext(), material = 'pla') {
|
|
46
|
+
const items = a.items || [], list = items.filter((it) => it.panel);
|
|
47
|
+
if (!list.length) return { a, panels: [] };
|
|
48
|
+
const beams = new Map(items.filter((it) => it.beam).map((it) => [it.id, it])), ids = new Set(items.map((it) => it.id)), models = new Map();
|
|
49
|
+
const modelOf = (id) => { if (!models.has(id)) models.set(id, beamOf(beams.get(id), num, context).model); return models.get(id); };
|
|
50
|
+
const free = (base) => { let n = base, k = 2; while (ids.has(n)) n = `${base}-${k++}`; ids.add(n); return n; };
|
|
51
|
+
const added = [], fasten = [], joints = [], poses = {}, panels = [], replaced = new Map();
|
|
52
|
+
for (const it of list) {
|
|
53
|
+
const what = `item ${it.id}`, P = it.panel;
|
|
54
|
+
if (!P || typeof P !== 'object' || Array.isArray(P)) throw new Error(`${what}: panel is { "sheet": "acrylic", "thickness": 3, "bay": ["x1", "z1", "x2", "z2"], "out": [0, -1, 0] } (got ${shown(P)})`);
|
|
55
|
+
for (const k of Object.keys(P)) if (!KEYS.includes(k)) throw new Error(`${what}: a panel has no "${k}": it takes ${KEYS.join(', ')}`);
|
|
56
|
+
if (it.at != null || it.rotate != null || it.repeat != null) throw new Error(`${what}: a panel is placed by its bay: leave out at, rotate and repeat`);
|
|
57
|
+
const T = typeof P.thickness === 'number' ? P.thickness : num(P.thickness ?? NaN);
|
|
58
|
+
if (!(Array.isArray(P.bay) && P.bay.length === 4)) throw new Error(`${what}: bay is the four beams round it, in order, as ["x1", "z1", "x2", "z2"] (got ${shown(P.bay)})`);
|
|
59
|
+
for (const b of P.bay) if (!beams.has(b)) throw new Error(`${what}: bay names beam items of this assembly, and ${b} is ${items.some((x) => x.id === b) ? 'not a beam' : 'not an item'} (beams: ${[...beams.keys()].join(', ') || 'none'})`);
|
|
60
|
+
if (new Set(P.bay).size !== 4) throw new Error(`${what}: bay is four different beams (got ${shown(P.bay)})`);
|
|
61
|
+
if (!(Array.isArray(P.out) && P.out.length === 3 && Math.hypot(...P.out.map(num)) > 0)) throw new Error(`${what}: out is the way the panel's face looks, [x, y, z], square to the bay (got ${shown(P.out)})`);
|
|
62
|
+
const out = unit(P.out.map(num));
|
|
63
|
+
// each beam: its frame, its slotted face looking along out, that face's plane and its slot's line
|
|
64
|
+
const edges = P.bay.map((id) => {
|
|
65
|
+
const it2 = beams.get(id), f = beamPlace(it2, num), B = modelOf(id);
|
|
66
|
+
if (!f) throw new Error(`${what}: a panel's bay is worked out from where its beams are: place ${id} by its ends (from, to)`);
|
|
67
|
+
const d = col(f, 2);
|
|
68
|
+
if (Math.abs(dot(d, out)) > 1e-6) throw new Error(`${what}: ${id} runs across the bay's plane, not in it: its beams lie square to out`);
|
|
69
|
+
const place = B.places.find((q) => q.slot && dot(turn(f, [...q.normal, 0]), out) > 1 - 1e-6);
|
|
70
|
+
if (!place) throw new Error(`${what}: ${id} has no slotted face that looks along out: turn it with roll so one does`);
|
|
71
|
+
const S = world(f, [place.at[0], place.at[1], 0]);
|
|
72
|
+
return { id, f, B, d, place, S, h: dot(S, out), half: Math.max(B.profile.width, B.profile.height) / 2, profile: B.profile.name, length: B.length };
|
|
73
|
+
});
|
|
74
|
+
const h = edges[0].h, off = edges.find((e) => Math.abs(e.h - h) > PANEL_CHECK.plane.value);
|
|
75
|
+
if (off) throw new Error(`${what}: the bay's faces are not in one plane: ${off.id}'s stands ${mm(Math.abs(off.h - h))} mm ${off.h > h ? 'proud of' : 'below'} ${edges[0].id}'s`);
|
|
76
|
+
const u = edges[0].d, v = cross(out, u), along = (e) => (Math.abs(dot(e.d, u)) > 1 - 1e-6 ? 'u' : Math.abs(dot(e.d, v)) > 1 - 1e-6 ? 'v' : null);
|
|
77
|
+
const us = edges.filter((e) => along(e) === 'u'), vs = edges.filter((e) => along(e) === 'v');
|
|
78
|
+
if (us.length !== 2 || vs.length !== 2) throw new Error(`${what}: a bay is four beams round a rectangle, two each way: ${P.bay.join(', ')} run ${us.length} along ${edges[0].id} and ${vs.length} across it`);
|
|
79
|
+
// each edge: the axis across it (u or v), its slot line's coordinate on that axis, and the way into the bay
|
|
80
|
+
for (const [pair, ax] of [[us, v], [vs, u]]) {
|
|
81
|
+
const c = pair.map((e) => dot(e.S, ax)), lo = c[0] < c[1] ? 0 : 1;
|
|
82
|
+
if (Math.abs(c[0] - c[1]) < 1) throw new Error(`${what}: ${pair[0].id} and ${pair[1].id} lie on one line: a bay has two beams each way, apart`);
|
|
83
|
+
pair.forEach((e, k) => Object.assign(e, { ax, c: c[k], in: k === lo ? 1 : -1 }));
|
|
84
|
+
}
|
|
85
|
+
const span = (e) => { const other = e.ax === v ? vs : us, q = e.ax === v ? u : v, cs = other.map((o) => dot(o.S, q)).sort((x, y) => x - y); return { q, lo: cs[0], hi: cs[1], half: Math.max(...other.map((o) => o.half)) }; };
|
|
86
|
+
const sheet = P.sheet, given = (e) => givenFor(context, e.profile);
|
|
87
|
+
const door = P.door != null ? doorSpec(what, P.door, P.bay, num) : null;
|
|
88
|
+
// where each edge's panel edge goes, from its slot line into the bay
|
|
89
|
+
let clipOf = null, hm = null, edgeIn = {};
|
|
90
|
+
const clipFor = (e) => { try { return panelClipModel(e.profile, { sheet: T, given: given(e), material, ...(P.nut ? { nut: P.nut } : {}), ...(P.load != null ? { load: num(P.load) } : {}) }); } catch (x) { throw new Error(`${what}: ${String(x.message).replace(/^\w+: /, '')}`); } };
|
|
91
|
+
try { panelModel(sheet, T, { size: [1, 1], corner: 0 }); } catch (x) { throw new Error(`${what}: ${String(x.message).replace(/^panelModel: /, '')}`); }
|
|
92
|
+
if (door) {
|
|
93
|
+
const H = edges.find((e) => e.id === door.hinge);
|
|
94
|
+
try { hm = hingeModel(H.profile, { sheet: T, given: given(H), material, ...(door.load != null ? { load: door.load } : {}) }); } catch (x) { throw new Error(`${what}: ${String(x.message).replace(/^\w+: /, '')}`); }
|
|
95
|
+
const probe = latchModel(H.profile, { sheet: T, standoff: hm.standoff, given: given(H), material }), clip = clipFor(H);
|
|
96
|
+
for (const e of edges) edgeIn[e.id] = e.id === door.hinge ? hm.edge : r3(Math.max(clip.edge, probe.edge));
|
|
97
|
+
} else { clipOf = new Map(edges.map((e) => [e.id, clipFor(e)])); for (const e of edges) edgeIn[e.id] = clipOf.get(e.id).edge; }
|
|
98
|
+
// the panel's rectangle in (u, v): between its edges, each set in from its slot line
|
|
99
|
+
const lim = (pair) => { const [p, q] = pair[0].in > 0 ? pair : [pair[1], pair[0]]; return [p.c + edgeIn[p.id], q.c - edgeIn[q.id]]; };
|
|
100
|
+
const [u0, u1] = lim(vs), [v0, v1] = lim(us);
|
|
101
|
+
if (!(u1 - u0 > 1 && v1 - v0 > 1)) throw new Error(`${what}: the bay is too small for a panel held at its edges: ${mm(u1 - u0)} x ${mm(v1 - v0)} mm between them`);
|
|
102
|
+
const lift = door ? hm.standoff : 0, O = add(add(add([0, 0, 0], u, u0), v, v0), out, h + lift), F = { u, v, w: out, O };
|
|
103
|
+
const toPanel = (p) => [r3(dot(sub(p, O), u)), r3(dot(sub(p, O), v)), r3(dot(sub(p, O), out))], dirPanel = (q) => [r3(dot(q, u)), r3(dot(q, v)), r3(dot(q, out))];
|
|
104
|
+
const holes = [], anchors = {}, mine = [];
|
|
105
|
+
// a part on an edge's beam at a distance along it: its address, turned so its y looks into the bay
|
|
106
|
+
const onBeam = (e, at, part) => {
|
|
107
|
+
const dist = r3(dot(sub(at, org(e.f)), e.d)), yIn = e.ax.map((c) => c * e.in), ylocal = cross(out, e.d);
|
|
108
|
+
return { at: `${e.id}.${e.place.name}@${dist}`, ...(dot(ylocal, yIn) > 0 ? {} : { rotate: [0, 0, 180] }), dist, x: dot(ylocal, yIn) > 0 ? e.d : e.d.map((c) => -c), part };
|
|
109
|
+
};
|
|
110
|
+
const pointOn = (e, s) => add(add(add([0, 0, 0], e.ax, e.c), span(e).q, s), out, h); // the slot line at s along the edge
|
|
111
|
+
const record = { id: it.id, sheet, thickness: T, door: !!door, items: [], fasten: [] };
|
|
112
|
+
if (!door) {
|
|
113
|
+
const pitch = P.pitch != null ? num(P.pitch) : PANEL_DEFAULTS.pitch.value;
|
|
114
|
+
if (!(pitch > 0)) throw new Error(`${what}: pitch is the most between clips along an edge, in mm, above 0 (got ${shown(P.pitch)})`);
|
|
115
|
+
for (const e of edges) {
|
|
116
|
+
const cm = clipOf.get(e.id), sp = span(e), clear = Math.max(sp.half, ...edges.filter((o) => o.ax !== e.ax).map((o) => clipOf.get(o.id).reach)) + cm.width / 2 + PANEL_CHECK.inset.value, lo = sp.lo + clear, hi = sp.hi - clear; // (clear of the beams across its ends and of their clips)
|
|
117
|
+
if (hi < lo) throw new Error(`${what}: ${e.id} is too short for a clip between the beams across its ends`);
|
|
118
|
+
const n = Math.max(2, Math.ceil((hi - lo) / pitch) + 1);
|
|
119
|
+
for (let k = 0; k < n; k++) {
|
|
120
|
+
const s = n === 1 ? (lo + hi) / 2 : lo + ((hi - lo) * k) / (n - 1), place = onBeam(e, pointOn(e, s)), id = free(`${it.id}-clip`);
|
|
121
|
+
const spec = { kind: 'panel-clip', profile: e.profile, sheet: T, ...(P.nut ? { nut: P.nut } : {}), ...(P.load != null ? { load: num(P.load) } : {}) };
|
|
122
|
+
added.push({ id, fitting: spec, at: place.at, ...(place.rotate ? { rotate: place.rotate } : {}), panelOf: it.id });
|
|
123
|
+
fasten.push({ screw: `${cm.thread} ${cm.head}`, through: [id], into: `${e.id}.${e.place.name}`, at: [`${id}.b1`], note: `${it.id}: a clip into ${e.id}`, nut: cm.screws[0].nutKind, panelOf: it.id });
|
|
124
|
+
record.items.push(id);
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
} else {
|
|
128
|
+
const H = edges.find((e) => e.id === door.hinge), L = edges.find((e) => e.ax === H.ax && e !== H), sp = span(H);
|
|
129
|
+
const axis = cross(H.ax.map((c) => c * H.in), out), lo = sp.lo + sp.half + hm.length / 2 + PANEL_CHECK.inset.value, hi = sp.hi - sp.half - hm.length / 2 - PANEL_CHECK.inset.value;
|
|
130
|
+
if (hi < lo + hm.length) throw new Error(`${what}: ${H.id} is too short for two ${mm(hm.length)} mm hinges between the beams across its ends`);
|
|
131
|
+
const jid = `${it.id}-hinge`, mid = (lo + hi) / 2;
|
|
132
|
+
[lo, hi].forEach((s, k) => {
|
|
133
|
+
const P0 = pointOn(H, s), place = onBeam(H, P0), fid = free(`${it.id}-hinge-${k + 1}`), did = free(`${it.id}-leaf-${k + 1}`);
|
|
134
|
+
added.push({ id: fid, fitting: { kind: 'hinge', profile: H.profile, sheet: T, part: 'frame', ...(door.load != null ? { load: door.load } : {}) }, at: place.at, ...(place.rotate ? { rotate: place.rotate } : {}), panelOf: it.id });
|
|
135
|
+
fasten.push({ screw: `${hm.thread} button`, through: [fid], into: `${H.id}.${H.place.name}`, at: [`${fid}.b1`, `${fid}.b2`], note: `${it.id}: hinge ${k + 1} into ${H.id}`, nut: hm.screws[0].nutKind, panelOf: it.id });
|
|
136
|
+
// the door's leaf on the door, at an anchor of the door where the hinge's frame is: x along the axis, z out
|
|
137
|
+
anchors[`h${k + 1}`] = { at: toPanel(P0), axis: [0, 0, 1], ref: dirPanel(axis) };
|
|
138
|
+
added.push({ id: did, fitting: { kind: 'hinge', profile: H.profile, sheet: T, part: 'door', ...(door.load != null ? { load: door.load } : {}) }, at: `${it.id}.h${k + 1}`, panelOf: it.id });
|
|
139
|
+
const yIn = H.ax.map((c) => c * H.in);
|
|
140
|
+
hm.door.forEach((q, j) => {
|
|
141
|
+
const W = add(add(add(P0, axis, q.at[0]), yIn, q.at[1]), out, q.at[2]), p = toPanel(W);
|
|
142
|
+
holes.push({ thread: hm.screw, at: [p[0], p[1]] });
|
|
143
|
+
anchors[`d${k + 1}${j + 1}`] = { at: p, axis: [0, 0, -1] };
|
|
144
|
+
});
|
|
145
|
+
fasten.push({ screw: `${hm.screw} button`, through: [did], into: it.id, at: hm.door.map((_, j) => `${did}.d${j + 1}`), nut: true, note: `${it.id}: hinge ${k + 1}'s leaf through the door`, panelOf: it.id });
|
|
146
|
+
record.items.push(fid, did);
|
|
147
|
+
});
|
|
148
|
+
// the joint: about the hinges' axis, its parent the hinge beam, the door's anchor "pivot" on it
|
|
149
|
+
const Pax = add(add(add(pointOn(H, mid), H.ax.map((c) => c * H.in), hm.axis[0]), out, hm.axis[1]), [0, 0, 0]);
|
|
150
|
+
anchors.pivot = { at: toPanel(Pax), axis: dirPanel(axis), ref: [0, 0, 1] };
|
|
151
|
+
joints.push({ id: jid, type: 'revolute', parent: { item: H.id, at: local(H.f, Pax), axis: localDir(H.f, axis), ref: localDir(H.f, out) }, child: `${it.id}.pivot`, min: 0, max: door.open, label: `${it.id} opens` });
|
|
152
|
+
poses[`${it.id}-shut`] = { [jid]: 0 }; poses[`${it.id}-open`] = { [jid]: door.open };
|
|
153
|
+
// the handle on the door's outer face, in from the latch side; screwed from inside into its feet
|
|
154
|
+
if (door.handle) {
|
|
155
|
+
const hd = handleModel({ sheet: T, material }), hid = free(`${it.id}-handle`), inward = L.ax.map((c) => c * L.in), at = add(add(pointOn(L, mid), inward, edgeIn[L.id] + 20), out, lift + T), p = toPanel(at);
|
|
156
|
+
const ref = cross(axis, out); // (the handle's x across it, so its y runs along the latch side)
|
|
157
|
+
anchors.handle = { at: p, axis: [0, 0, 1], ref: dirPanel(ref) };
|
|
158
|
+
added.push({ id: hid, fitting: { kind: 'handle', sheet: T }, at: `${it.id}.handle`, panelOf: it.id });
|
|
159
|
+
[-1, 1].forEach((sgn, j) => { const q = add(at, axis, (sgn * hd.span) / 2), pp = toPanel(q); holes.push({ thread: hd.thread, at: [pp[0], pp[1]] }); anchors[`k${j + 1}`] = { at: [pp[0], pp[1], 0], axis: [0, 0, 1] }; });
|
|
160
|
+
fasten.push({ screw: `${hd.thread} button`, through: [it.id], into: hid, at: [`${it.id}.k1`, `${it.id}.k2`], note: `${it.id}: the handle`, panelOf: it.id });
|
|
161
|
+
record.items.push(hid);
|
|
162
|
+
}
|
|
163
|
+
// a magnet catch under the door's edge on the latch beam, and a turn-button latch where asked
|
|
164
|
+
for (const [want, kind] of [[door.catch, 'magnet-catch'], [door.latch, 'latch']]) {
|
|
165
|
+
if (!want) continue;
|
|
166
|
+
const cid = free(`${it.id}-${kind === 'latch' ? 'latch' : 'catch'}`), place = onBeam(L, pointOn(L, kind === 'latch' ? mid + 40 : mid));
|
|
167
|
+
const spec = { kind, profile: L.profile, standoff: hm.standoff, edge: edgeIn[L.id], ...(kind === 'latch' ? { sheet: T } : {}) };
|
|
168
|
+
const cm = kind === 'latch' ? latchModel(L.profile, { sheet: T, standoff: hm.standoff, edge: edgeIn[L.id], given: given(L), material }) : magnetCatchModel(L.profile, { standoff: hm.standoff, edge: edgeIn[L.id], given: given(L), material });
|
|
169
|
+
added.push({ id: cid, fitting: spec, at: place.at, ...(place.rotate ? { rotate: place.rotate } : {}), panelOf: it.id });
|
|
170
|
+
fasten.push({ screw: `${cm.thread} button`, through: [cid], into: `${L.id}.${L.place.name}`, at: [`${cid}.b1`], note: `${it.id}: the ${kind.replace('-', ' ')} into ${L.id}`, nut: cm.screws[0].nutKind, panelOf: it.id });
|
|
171
|
+
record.items.push(cid);
|
|
172
|
+
if (kind === 'latch') Object.assign(record, { joint: jid, latch: cid });
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
const pm = panelModel(sheet, T, { size: [r3(u1 - u0), r3(v1 - v0)], holes });
|
|
176
|
+
record.model = pm; record.size = pm.size; panels.push(record);
|
|
177
|
+
const placed = { ...it, panel: { ...P }, _panel: { model: pm, anchors, frame: F } };
|
|
178
|
+
if (!door) joints.push({ id: `${it.id}-bay`, type: 'fixed', parent: { at: O.map(r3), axis: out, ref: u }, child: { item: it.id, at: [0, 0, 0], axis: [0, 0, 1], ref: [1, 0, 0] } });
|
|
179
|
+
replaced.set(it.id, placed);
|
|
180
|
+
}
|
|
181
|
+
const first = (a.fasten || []).length;
|
|
182
|
+
const out = { ...a, items: [...items.map((it) => replaced.get(it.id) ?? it), ...added], ...(fasten.length ? { fasten: [...(a.fasten || []), ...fasten] } : {}), joints: [...(a.joints || []), ...joints], poses: { ...(a.poses || {}), ...poses } };
|
|
183
|
+
for (const r of panels) r.fasten = fasten.map((x, k) => (x.panelOf === r.id ? first + k : -1)).filter((k) => k >= 0);
|
|
184
|
+
return { a: out, panels };
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
function doorSpec(what, d, bay, num) {
|
|
188
|
+
if (!d || typeof d !== 'object' || Array.isArray(d)) throw new Error(`${what}: door is { "hinge": "${bay[0]}", "open": 120 } (got ${shown(d)})`);
|
|
189
|
+
for (const k of Object.keys(d)) if (!DOOR_KEYS.includes(k)) throw new Error(`${what}: a door has no "${k}": it takes ${DOOR_KEYS.join(', ')}`);
|
|
190
|
+
if (!bay.includes(d.hinge)) throw new Error(`${what}: door.hinge is the beam of the bay its hinges go on, one of ${bay.join(', ')} (got ${shown(d.hinge)})`);
|
|
191
|
+
const open = d.open != null ? num(d.open) : DOOR_DEFAULTS.open.value;
|
|
192
|
+
if (!(open > 0 && open <= 180)) throw new Error(`${what}: door.open is how far it opens, in degrees, above 0 and up to 180 (got ${shown(d.open)})`);
|
|
193
|
+
for (const k of ['handle', 'catch', 'latch']) if (d[k] != null && typeof d[k] !== 'boolean') throw new Error(`${what}: door.${k} is true or false (got ${shown(d[k])})`);
|
|
194
|
+
return { hinge: d.hinge, open, handle: d.handle ?? true, catch: d.catch ?? true, latch: d.latch ?? false, ...(d.load != null ? { load: num(d.load) } : {}) };
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
/**
|
|
198
|
+
* An item with "panel", built once expandPanels has worked it out: the sheet as a bought reference (the list counts
|
|
199
|
+
* it by its sheet, thickness and size), its anchors (a door's pivot, hinge, screw and handle places), and its
|
|
200
|
+
* figures as values (door.width, door.height, door.grams). @param {object} it @param {string} id
|
|
201
|
+
*/
|
|
202
|
+
export async function panelBuilt(it, id) {
|
|
203
|
+
if (!it._panel) throw new Error(`item ${id}: a panel is worked out from its bay before it is built`);
|
|
204
|
+
let ops = null; try { ops = await booleans('manifold'); } catch {} // (cut by Manifold where installed, so the overlap check reads it exactly)
|
|
205
|
+
const { model: m, anchors } = it._panel, parts = named({ body: (() => { try { return panel(m, ops ? { ops } : {}); } catch { return panel(m); } })() }, { body: { label: m.title } });
|
|
206
|
+
const values = { width: m.size[0], height: m.size[1], thickness: m.thickness, grams: m.grams };
|
|
207
|
+
return referenceBuilt({ id, key: 'panel', objName: `panel:${m.name}`, name: m.name, parts, anchors, entries: [], values,
|
|
208
|
+
extra: { panel: m, bought: { set: `${m.sheet} sheet, ${mm(m.thickness)} mm`, variant: null, part: 'panel', mass: m.grams / 1000, size: m.size } } });
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
/**
|
|
212
|
+
* A built assembly's panel findings: panel.size for each (its sheet, size and mass, and the clips or door parts that
|
|
213
|
+
* hold it), and panel.thickness where the sheet is not commonly stocked. @param {object} s @param {Function} f
|
|
214
|
+
*/
|
|
215
|
+
export function panelFindings(s, f) {
|
|
216
|
+
for (const r of s.panels ?? []) {
|
|
217
|
+
const held = r.items.length;
|
|
218
|
+
for (const x of r.model.findings) f(x.level, `${r.id}: ${x.text}${x.code === 'panel.size' ? `; ${r.door ? `a door on ${r.items.filter((k) => /-hinge-\d+$/.test(k)).length} hinges` : `held by ${held} clips`}` : ''}`, x.level === 'warn' || x.level === 'fail' ? `${r.id}: ${x.code}` : null, { code: x.code, values: { ...(x.values ?? {}), ...(x.code === 'panel.size' ? { items: r.items } : {}) }, refs: { item: r.id } });
|
|
219
|
+
}
|
|
220
|
+
}
|
package/lib/plates.mjs
CHANGED
|
@@ -10,6 +10,8 @@ import { peekRuleSet } from './rules.mjs';
|
|
|
10
10
|
import { CadError } from './errors.mjs';
|
|
11
11
|
import { assemblyPath, buildAssembly, checkAssembly } from './assembly.mjs';
|
|
12
12
|
import { printedParts } from './list.mjs';
|
|
13
|
+
import { fittingBuilt, plasticOf } from './fittings.mjs';
|
|
14
|
+
import { framesContext } from './beams.mjs';
|
|
13
15
|
|
|
14
16
|
const n3 = (x) => +x.toFixed(3);
|
|
15
17
|
|
|
@@ -21,13 +23,18 @@ function rulesOf(project, gap) {
|
|
|
21
23
|
}
|
|
22
24
|
|
|
23
25
|
// Every printed thing to go on a plate: built as printed, its filaments, how big
|
|
26
|
+
// (a printed fitting, a join's, a guide's or an item's own, goes on as its item's fitting laid as it prints: "pose": "print")
|
|
24
27
|
async function plateParts(s) {
|
|
25
|
-
const out = [];
|
|
28
|
+
const out = [], items = new Map((s.items ?? []).map((it) => [it.id, it]));
|
|
26
29
|
for (const o of s.objects) {
|
|
27
30
|
if (o.unplaced || o.role !== 'part' || !o.objName) continue;
|
|
28
|
-
const
|
|
31
|
+
const it = o.fitting ? items.get(o.id) : null;
|
|
32
|
+
if (o.fitting && !it?.fitting) continue;
|
|
33
|
+
const fitting = it ? { ...it.fitting, pose: 'print' } : null;
|
|
34
|
+
const b = fitting ? (await fittingBuilt({ ...it, fitting }, o.id, framesContext(), plasticOf(s.project))).b : await build({ ...locate(`${s.project}/${o.objName}`), ...(o.variant ? { variant: o.variant } : {}) }, o.set || {});
|
|
35
|
+
const tris = b.parts.flatMap((p) => p.triangles), { lo, hi } = bounds(tris);
|
|
29
36
|
const fil = [...new Set(b.parts.filter((p) => p.role !== 'support').map((p) => p.filament || 'base'))].sort().join('+') || 'base';
|
|
30
|
-
out.push({ id: o.id.replace(/\./g, '_'), source: o.id, object: o.objName, variant: o.variant, set: o.set || {}, filament: fil, hash: b.hash, lo, size: [hi[0] - lo[0], hi[1] - lo[1]], height: hi[2] - lo[2] });
|
|
37
|
+
out.push({ id: o.id.replace(/\./g, '_'), source: o.id, object: o.objName, ...(fitting ? { fitting } : {}), variant: o.variant, set: o.set || {}, filament: fil, hash: b.hash, lo, size: [hi[0] - lo[0], hi[1] - lo[1]], height: hi[2] - lo[2] });
|
|
31
38
|
}
|
|
32
39
|
return out;
|
|
33
40
|
}
|
|
@@ -61,7 +68,7 @@ function plateJson(s, plate, { bed, gap }) {
|
|
|
61
68
|
const origin = (bed - 2 * gap) / 2;
|
|
62
69
|
return { title: `${s.a.title || s.name}: ${plate.filament}, plate ${plate.n}`, kind: 'plate', note: `Generated by cad plates from ${s.name}. Hand edits are replaced when a part on it changes.`,
|
|
63
70
|
packed: { of: s.name, filament: plate.filament, gap, parts: Object.fromEntries(plate.items.map(({ p }) => [p.id, p.hash])) },
|
|
64
|
-
items: plate.items.map(({ p, x, y }) => ({ id: p.id, object: p.object, ...(p.variant ? { variant: p.variant } : {}), ...(Object.keys(p.set).length ? { set: p.set } : {}), at: [n3(x - origin - p.lo[0]), n3(y - origin - p.lo[1]), n3(-p.lo[2])] })) };
|
|
71
|
+
items: plate.items.map(({ p, x, y }) => ({ id: p.id, ...(p.fitting ? { fitting: p.fitting } : { object: p.object }), ...(p.variant ? { variant: p.variant } : {}), ...(Object.keys(p.set).length ? { set: p.set } : {}), at: [n3(x - origin - p.lo[0]), n3(y - origin - p.lo[1]), n3(-p.lo[2])] })) };
|
|
65
72
|
}
|
|
66
73
|
|
|
67
74
|
/**
|
package/lib/schema.mjs
CHANGED
|
@@ -27,9 +27,10 @@ export function validate(value, s, root = s, at = '') {
|
|
|
27
27
|
if (s.$ref) return validate(value, s.$ref.replace('#/$defs/', '').split('/').reduce((o, k) => o[k], root.$defs), root, at);
|
|
28
28
|
if (s.anyOf) {
|
|
29
29
|
const each = s.anyOf.map((x) => validate(value, x, root, at));
|
|
30
|
-
if (each.some((e) => !e.length)) return [];
|
|
31
30
|
// Report the alternative that got furthest (the fewest problems), so a near miss says what's wrong with it
|
|
32
|
-
|
|
31
|
+
const best = each.some((e) => !e.length) ? [] : each.sort((a, b) => a.length - b.length)[0];
|
|
32
|
+
if (Object.keys(s).every((k) => k === 'anyOf' || k === 'description')) return best;
|
|
33
|
+
out.push(...best); // beside other keywords, as JSON Schema has it: each applies (a fit: a and b, or holes and into)
|
|
33
34
|
}
|
|
34
35
|
if (s.type) {
|
|
35
36
|
const types = [].concat(s.type);
|
package/lib/services.mjs
ADDED
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
// A machine's services on its frame (@rulecad/frames' services.mjs): a bought board or supply with "mount" sits on a
|
|
2
|
+
// printed mount at an address on a beam's slotted face, its holes read from its own rule set,
|
|
3
|
+
// { "id": "psu", "bought": "supply-24v", "variant": "LRS-50-24", "mount": { "on": "rail.top@200" } }
|
|
4
|
+
// and a wire may run along a beam's slot, held by printed cable clips at a pitch,
|
|
5
|
+
// "via": [{ "along": "rail.top", "from": 20, "to": 380, "pitch": 100 }]
|
|
6
|
+
// Before the items are built, each mount is added as a fitting item with its fastens (into the slot, and into or
|
|
7
|
+
// through the device) and the device placed on it; each run's clips are added at their places along the slot and the
|
|
8
|
+
// wire routed through them, so its length follows the beam. Units: mm.
|
|
9
|
+
import { beamAnchor, runClips, SERVICE_DEFAULTS } from '@rulecad/frames';
|
|
10
|
+
import { beamOf, framesContext } from './beams.mjs';
|
|
11
|
+
import { fittingModel } from './fittings.mjs';
|
|
12
|
+
import { boughtSets } from './bought.mjs';
|
|
13
|
+
|
|
14
|
+
const MOUNT_KEYS = ['on', 'turn', 'offset', 'standoff', 'load', 'nut', 'head', 'screw'];
|
|
15
|
+
const RUN_KEYS = ['along', 'from', 'to', 'pitch', 'cable'];
|
|
16
|
+
const shown = (v) => (v === undefined ? 'nothing' : JSON.stringify(v));
|
|
17
|
+
const mm = (x) => String(+Number(x).toFixed(2)), r3 = (x) => +x.toFixed(3);
|
|
18
|
+
|
|
19
|
+
// a slotted place on a beam by its face's name (top, top.2) and the beam's model, refused by name
|
|
20
|
+
function slotPlace(what, id, B, name) {
|
|
21
|
+
const q = B.places.find((x) => x.name === name || (x.face === name && x.index === 1));
|
|
22
|
+
if (!q) throw new Error(`${what}: ${id} has no face "${name}": it has ${[...new Set(B.places.map((x) => x.face))].join(', ')}`);
|
|
23
|
+
if (!q.slot) throw new Error(`${what}: ${id}'s ${name} face is closed: a mount or a clip goes into a slot`);
|
|
24
|
+
return q;
|
|
25
|
+
}
|
|
26
|
+
// the face a fitting is drawn for: top, or top.2 where the face has two slots
|
|
27
|
+
const faceOf = (B, q) => (B.places.filter((x) => x.face === q.face).length > 1 ? q.name : q.face);
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* An assembly's mounts and slot runs worked out before its items are built: each bought item with "mount" placed on a
|
|
31
|
+
* printed device mount at its address, the mount's screws into the slot and the device's screws as fastens; each wire's
|
|
32
|
+
* run along a slot as cable clips at a pitch, the wire routed through them. Refused by name: a mount on what is not a
|
|
33
|
+
* bought board or supply, an item with both at and mount, an address that is not on a slotted face of a beam, a run past
|
|
34
|
+
* a beam's end, and what @rulecad/frames refuses.
|
|
35
|
+
* @param {object} a the assembly as read @param {(x: any) => number} num @param {object} [context] framesContext()'s @param {string} [material]
|
|
36
|
+
* @returns {{ a: object, mounts: object[], runs: object[] }}
|
|
37
|
+
*/
|
|
38
|
+
export function expandServices(a, num, context = framesContext(), material = 'pla') {
|
|
39
|
+
const items = a.items || [], mounted = items.filter((it) => it.mount != null), runs0 = (a.wires || []).some((w) => (w.via ?? []).some((v) => v && typeof v === 'object'));
|
|
40
|
+
if (!mounted.length && !runs0) return { a, mounts: [], runs: [] };
|
|
41
|
+
const beams = new Map(items.filter((it) => it.beam).map((it) => [it.id, it])), ids = new Set(items.map((it) => it.id)), models = new Map();
|
|
42
|
+
const modelOf = (id) => { if (!models.has(id)) models.set(id, beamOf(beams.get(id), num, context).model); return models.get(id); };
|
|
43
|
+
const free = (base) => { let n = base, k = 2; while (ids.has(n)) n = `${base}-${k++}`; ids.add(n); return n; };
|
|
44
|
+
const beamOfAddress = (what, ref, key) => {
|
|
45
|
+
const s = String(ref ?? ''), i = s.indexOf('.');
|
|
46
|
+
if (i < 0) throw new Error(`${what}: ${key} is a beam's slotted face${key === 'on' ? ' and a distance, as rail.top@200' : ', as rail.top'} (got ${shown(ref)})`);
|
|
47
|
+
const id = s.slice(0, i), rest = s.slice(i + 1);
|
|
48
|
+
if (!beams.has(id)) throw new Error(`${what}: ${key} names a beam item of this assembly, and ${id} is ${items.some((x) => x.id === id) ? 'not a beam' : 'not an item'} (beams: ${[...beams.keys()].join(', ') || 'none'})`);
|
|
49
|
+
return { id, rest, B: modelOf(id) };
|
|
50
|
+
};
|
|
51
|
+
const added = [], fasten = [], fits = [], mounts = [], runs = [], plates = [], replaced = new Map(), ahead = new Map();
|
|
52
|
+
for (const it of mounted) {
|
|
53
|
+
const what = `item ${it.id}`, M = it.mount;
|
|
54
|
+
if (!M || typeof M !== 'object' || Array.isArray(M)) throw new Error(`${what}: mount is { "on": "rail.top@200" }, an address on a beam's slotted face (got ${shown(M)})`);
|
|
55
|
+
for (const k of Object.keys(M)) if (!MOUNT_KEYS.includes(k)) throw new Error(`${what}: a mount has no "${k}": it takes ${MOUNT_KEYS.join(', ')}`);
|
|
56
|
+
if (!it.bought) throw new Error(`${what}: mount holds a bought board or supply by the holes its rule set gives: give "bought"`);
|
|
57
|
+
const part = boughtSets().get(it.bought);
|
|
58
|
+
if (!['board', 'supply', 'encoder'].includes(part)) throw new Error(`${what}: mount holds a bought board or supply, and ${it.bought} is ${part ? `a ${part}` : 'no bought part'}`);
|
|
59
|
+
if (it.at != null || it.rotate != null || it.repeat != null) throw new Error(`${what}: a mounted item is placed by its mount: leave out at, rotate and repeat`);
|
|
60
|
+
const { id: bid, rest, B } = beamOfAddress(what, M.on, 'on'), [face, dist] = rest.split('@');
|
|
61
|
+
if (dist == null) throw new Error(`${what}: on is a beam's face and a distance along it, as ${bid}.${face}@200 (got ${shown(M.on)})`);
|
|
62
|
+
try { beamAnchor(B, rest); } catch (e) { throw new Error(`${what}: on: ${String(e.message).replace(/^beamAnchor: /, '')}`); }
|
|
63
|
+
const q = slotPlace(what, bid, B, face), mid = free(`${it.id}-mount`);
|
|
64
|
+
const spec = { kind: 'device-mount', profile: B.profile.name, face: faceOf(B, q), device: it.bought, ...(it.variant != null ? { variant: it.variant } : {}),
|
|
65
|
+
...(M.turn != null ? { turn: M.turn } : {}), ...(M.offset != null ? { offset: num(M.offset) } : {}), ...(M.standoff != null ? { standoff: num(M.standoff) } : {}), ...(M.load != null ? { load: num(M.load) } : {}),
|
|
66
|
+
...(M.nut != null ? { nut: M.nut } : {}), ...(M.head != null ? { head: M.head } : {}), ...(M.screw != null ? { screw: M.screw } : {}) };
|
|
67
|
+
const m = fittingModel(spec, context, material, what), D = m.device;
|
|
68
|
+
ahead.set(it.id, { id: mid, fitting: spec, at: `${bid}.${rest}`, mountOf: it.id }); // (listed just before its device: of two parts as near the base, the earlier goes on first)
|
|
69
|
+
fasten.push({ screw: `${m.thread} ${m.head}`, through: [mid], into: `${bid}.${q.name}`, at: m.screws.map((_, k) => `${mid}.b${k + 1}`), nut: m.screws[0].nutKind, note: `${it.id}'s mount into ${bid}`, mountOf: it.id });
|
|
70
|
+
const at = D.holes.map((_, k) => `${mid}.d${k + 1}`);
|
|
71
|
+
fasten.push(m.fix === 'tapped'
|
|
72
|
+
? { screw: `${D.thread} ${D.head}`, through: [mid], into: it.id, at, note: `${it.id} on its mount, from under it`, mountOf: it.id }
|
|
73
|
+
: { screw: `${D.thread} ${D.head}`, through: [it.id], into: mid, at, note: `${it.id} on its mount`, mountOf: it.id });
|
|
74
|
+
replaced.set(it.id, { ...it, at: `${mid}.device` });
|
|
75
|
+
// the stretch of the slot its plate covers (either way along, as its outline reaches): a run's clips keep off it
|
|
76
|
+
const reachX = Math.max(...m.outline.map((pt) => Math.abs(pt[0]))), d0 = num(dist), at0 = d0 < 0 ? B.length + d0 : d0;
|
|
77
|
+
plates.push({ mount: mid, item: it.id, beam: bid, slot: q.name, lo: r3(at0 - reachX), hi: r3(at0 + reachX) });
|
|
78
|
+
mounts.push({ id: it.id, mount: mid, beam: bid, on: M.on, set: it.bought, variant: it.variant ?? null, fix: m.fix, holes: D.holes.length, thread: D.thread, length: D.length, grams: m.grams, margin: m.margin });
|
|
79
|
+
}
|
|
80
|
+
// wires: each run along a slot becomes clips, and the wire goes through each clip's cable anchor in turn
|
|
81
|
+
const wires = (a.wires || []).map((w, wi) => {
|
|
82
|
+
if (!(w.via ?? []).some((v) => v && typeof v === 'object')) return w;
|
|
83
|
+
const via = [];
|
|
84
|
+
for (const v of w.via) {
|
|
85
|
+
if (!v || typeof v !== 'object') { via.push(v); continue; }
|
|
86
|
+
const what = `wire ${w.id || wi + 1}`;
|
|
87
|
+
if (Array.isArray(v)) throw new Error(`${what}: a run in via is { "along": "rail.top", "from": 20, "to": 380 } (got ${shown(v)})`);
|
|
88
|
+
for (const k of Object.keys(v)) if (!RUN_KEYS.includes(k)) throw new Error(`${what}: a run along a slot has no "${k}": it takes ${RUN_KEYS.join(', ')}`);
|
|
89
|
+
const { id: bid, rest, B } = beamOfAddress(what, v.along, 'along');
|
|
90
|
+
if (rest.includes('@')) throw new Error(`${what}: along names the face (${bid}.${rest.split('@')[0]}); the run's ends are from and to, in mm from end a`);
|
|
91
|
+
const q = slotPlace(what, bid, B, rest);
|
|
92
|
+
if (v.from == null || v.to == null) throw new Error(`${what}: a run along ${v.along} gives from and to, in mm from ${bid}'s end a`);
|
|
93
|
+
const from = num(v.from), to = num(v.to), L = B.length;
|
|
94
|
+
for (const [k, x] of [['from', from], ['to', to]]) if (!(Number.isFinite(x) && x >= 0 && x <= L)) throw new Error(`${what}: a run's ${k} is from 0 to ${mm(L)} mm along ${bid} (got ${shown(v[k])})`);
|
|
95
|
+
let clips;
|
|
96
|
+
try { clips = runClips(Math.abs(to - from), v.pitch != null ? { pitch: num(v.pitch) } : {}); } catch (e) { throw new Error(`${what}: ${String(e.message).replace(/^runClips: /, '')}`); }
|
|
97
|
+
const sgn = to >= from ? 1 : -1, cable = v.cable != null ? num(v.cable) : SERVICE_DEFAULTS.cable.value, list = [];
|
|
98
|
+
// a mount on the same slot: no clip under its plate; one each side of it instead, and the wire said to go round it
|
|
99
|
+
const cw = fittingModel({ kind: 'cable-clip', profile: B.profile.name, face: faceOf(B, q), cable }, context, material, what).width, keep = cw / 2 + SERVICE_DEFAULTS.gap.value;
|
|
100
|
+
const rlo = Math.min(from, to), rhi = Math.max(from, to), round = plates.filter((pl) => pl.beam === bid && pl.slot === q.name && pl.lo - keep < rhi && pl.hi + keep > rlo).map((pl) => ({ ...pl, lo: r3(pl.lo - keep), hi: r3(pl.hi + keep) }));
|
|
101
|
+
let ds = clips.at.map((s) => r3(from + sgn * s));
|
|
102
|
+
if (round.length) {
|
|
103
|
+
ds = ds.filter((d) => !round.some((pl) => d > pl.lo && d < pl.hi));
|
|
104
|
+
for (const pl of round) for (const e of [pl.lo, pl.hi]) if (e >= rlo - 1e-9 && e <= rhi + 1e-9 && !round.some((o) => e > o.lo + 1e-9 && e < o.hi - 1e-9) && !ds.some((d) => Math.abs(d - e) < cw)) ds.push(e);
|
|
105
|
+
ds.sort((x, y) => sgn * (x - y));
|
|
106
|
+
if (!ds.length) throw new Error(`${what}: a run along ${bid}.${q.name} from ${mm(from)} to ${mm(to)} mm lies under ${round.map((pl) => pl.mount).join(' and ')}: no clip has room there; run it along another face`);
|
|
107
|
+
}
|
|
108
|
+
for (const d of ds) {
|
|
109
|
+
const cid = free(`${w.id || `w${wi + 1}`}-clip`);
|
|
110
|
+
added.push({ id: cid, fitting: { kind: 'cable-clip', profile: B.profile.name, face: faceOf(B, q), cable }, at: `${bid}.${q.name}@${d}`, runOf: w.id || `w${wi + 1}` });
|
|
111
|
+
fits.push({ a: cid, b: bid, fastens: true, note: `${cid}: snapped into ${bid}'s ${q.name} slot, its barbs under the lips` }); // (no screw: its barbs hold it)
|
|
112
|
+
via.push(`${cid}.cable`); list.push(cid);
|
|
113
|
+
}
|
|
114
|
+
runs.push({ wire: w.id || `w${wi + 1}`, along: `${bid}.${q.name}`, from, to, length: r3(Math.abs(to - from)), clips: list, count: list.length, pitch: clips.pitch, round: round.map((pl) => ({ mount: pl.mount, item: pl.item, from: pl.lo, to: pl.hi })), most: v.pitch != null ? num(v.pitch) : SERVICE_DEFAULTS.pitch.value, cable });
|
|
115
|
+
}
|
|
116
|
+
return { ...w, via };
|
|
117
|
+
});
|
|
118
|
+
const out = { ...a, items: [...items.flatMap((it) => [...(ahead.has(it.id) ? [ahead.get(it.id)] : []), replaced.get(it.id) ?? it]), ...added], ...(fasten.length ? { fasten: [...(a.fasten || []), ...fasten] } : {}), ...(runs.length ? { wires } : {}), ...(fits.length ? { fits: [...(a.fits || []), ...fits] } : {}) };
|
|
119
|
+
return { a: out, mounts, runs };
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* A built assembly's service findings: service.mount for each mounted device (its set, its holes and screws, the
|
|
124
|
+
* mount's grams and margin) and wire.run for each run along a slot (its length and its clips at their pitch).
|
|
125
|
+
* @param {object} s a built assembly @param {Function} f the findings' sink
|
|
126
|
+
*/
|
|
127
|
+
export function serviceFindings(s, f) {
|
|
128
|
+
for (const m of s.mounts ?? []) f('info', `${m.id}: on a printed mount (${m.mount}) at ${m.on}, held by its ${m.holes} ${m.fix === 'tapped' ? `tapped ${m.thread} holes from ${m.set}'s rule set, screwed up from under the mount` : `holes from ${m.set}'s rule set, ${m.thread} screws down into the mount`}; the mount ${mm(m.grams)} g, ${mm(m.margin)}× its load`, null, { code: 'service.mount', refs: { item: m.id, mount: m.mount, beam: m.beam, set: m.set }, values: { holes: m.holes, length: m.length, grams: m.grams, margin: m.margin } });
|
|
129
|
+
for (const r of s.runs ?? []) f('info', `${r.wire}: ${mm(r.length)} mm along ${r.along} from ${mm(r.from)} to ${mm(r.to)} mm, held by ${r.count} cable clip${r.count > 1 ? 's' : ''} ${r.count > 1 ? (r.round?.length ? `no more than ${mm(r.most)} mm apart but across ${r.round.map((x) => `${x.mount} (${mm(x.to - x.from)} mm, a clip each side)`).join(' and ')},` : `${mm(r.pitch)} mm apart (no more than ${mm(r.most)})`) : ''} for a ${mm(r.cable)} mm bundle`.trimEnd(), null, { code: 'wire.run', refs: { wire: r.wire, along: r.along, clips: r.clips }, values: { length: r.length, clips: r.count, pitch: r.pitch } });
|
|
130
|
+
// a run under a mount's plate: its clips are set either side, and the wire cannot lie in the slot under the plate
|
|
131
|
+
for (const r of s.runs ?? []) for (const x of r.round ?? []) f('warn', `${r.wire}: its run along ${r.along} passes ${x.mount} (${x.item}'s mount) from ${mm(x.from)} to ${mm(x.to)} mm: no clip goes under the plate, so one is set each side of it, and the wire must go round the mount there: run it along another face, or end the run before the mount`, `${r.wire}: round ${x.mount}`, { code: 'wire.run', refs: { wire: r.wire, along: r.along, mount: x.mount, item: x.item }, values: { from: x.from, to: x.to } });
|
|
132
|
+
}
|
package/lib/sheet.mjs
CHANGED
|
@@ -27,6 +27,22 @@ function fileOf(name) {
|
|
|
27
27
|
return file;
|
|
28
28
|
}
|
|
29
29
|
|
|
30
|
+
// What a check notes and never fails on. An alias: a name the vocabulary shares across parts (screw_clearance, which a
|
|
31
|
+
// motor's mount reads) that a set gives to one of its own rules in "interface". A copy: a workspace's own copy of a
|
|
32
|
+
// set the kit has (cad copies a set into rules/ the first time a design uses it), which is used as it is, even after
|
|
33
|
+
// the kit's set gains figures: it is told which, so a design that wants one knows where it went.
|
|
34
|
+
function notesOf(set, file) {
|
|
35
|
+
const notes = [], kitDir = RULE_DIRS.at(-1), name = path.basename(file, '.json');
|
|
36
|
+
for (const [shared, own] of Object.entries(set.interface || {})) if (set.rules?.[own]) notes.push({ level: 'info', code: 'rules.alias', text: `${own} is also read as ${shared}, the vocabulary's name for it`, rule: own, values: { alias: shared } });
|
|
37
|
+
const kitFile = path.join(kitDir, `${name}.json`);
|
|
38
|
+
if (path.dirname(path.resolve(file)) !== path.resolve(kitDir) && fs.existsSync(kitFile) && set.rules && typeof set.rules === 'object') {
|
|
39
|
+
const kit = readSet(kitFile), added = Object.keys(kit.rules || {}).filter((k) => !(k in set.rules));
|
|
40
|
+
if (added.length) notes.push({ level: 'info', code: 'rules.new', text: `your copy is used as it is; the kit's ${name} now also has ${added.length > 8 ? `${added.slice(0, 8).join(', ')} and ${added.length - 8} more (--json lists them)` : added.join(', ')}: copy any you want into rules/${name}.json, or delete it to read the kit's own`, values: { count: added.length }, refs: { rules: added } });
|
|
41
|
+
}
|
|
42
|
+
return notes;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
|
|
30
46
|
// What a profile's model reads beside its own set: any other set by name (its slot family, a thread, its material),
|
|
31
47
|
// the workspace's first, and a measured outline in outlines/<name>.json beside the set, held to its schema
|
|
32
48
|
function given(name, file, all = allSets()) {
|
|
@@ -57,13 +73,14 @@ function partFindings(set, name, file, all) {
|
|
|
57
73
|
return out;
|
|
58
74
|
}
|
|
59
75
|
|
|
60
|
-
/** Every named set checked (or all of them): [{ name, file, findings }]. A finding's level is fail, warn or info
|
|
76
|
+
/** Every named set checked (or all of them): [{ name, file, findings, notes? }]. A finding's level is fail, warn or info;
|
|
77
|
+
* notes: what is said and never fails */
|
|
61
78
|
export function checkSets(names = []) {
|
|
62
79
|
const v = vocabulary(), all = allSets();
|
|
63
80
|
return (names.length ? names.map((n) => [n, fileOf(n)]) : [...all]).map(([name, file]) => {
|
|
64
|
-
const set = readSet(file), findings = checkRuleSet(set, v);
|
|
81
|
+
const set = readSet(file), findings = checkRuleSet(set, v), notes = notesOf(set, file);
|
|
65
82
|
if (!findings.length && ['profile', 'slot', 'slot_nut'].includes(set.part)) findings.push(...partFindings(set, name, file, all));
|
|
66
|
-
return { name, file, findings };
|
|
83
|
+
return { name, file, findings, ...(notes.length ? { notes } : {}) };
|
|
67
84
|
});
|
|
68
85
|
}
|
|
69
86
|
|