@rulecad/cad 0.8.0 → 0.9.0
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 +24 -10
- package/CHANGELOG.md +404 -0
- package/EXAMPLES.md +40 -38
- package/README.md +193 -67
- package/TUTORIAL.md +2 -2
- package/bin/cad.mjs +2250 -512
- package/lib/animate.mjs +173 -49
- package/lib/assembly.mjs +1847 -434
- package/lib/beams.mjs +256 -52
- package/lib/belts.mjs +66 -15
- package/lib/board-work.mjs +12 -0
- package/lib/bought.mjs +309 -53
- package/lib/buildpage.mjs +179 -29
- package/lib/carried.mjs +201 -0
- package/lib/clone.mjs +194 -42
- package/lib/design.mjs +649 -121
- package/lib/drive.mjs +187 -63
- package/lib/edit.mjs +899 -191
- package/lib/errors.mjs +16 -3
- package/lib/explain.mjs +62 -13
- package/lib/fasten.mjs +2702 -507
- package/lib/fittings.mjs +715 -121
- package/lib/fixings.mjs +21 -9
- package/lib/framebuild.mjs +790 -175
- package/lib/framecheck.mjs +1901 -368
- package/lib/framegen.mjs +100 -22
- package/lib/frameheat.mjs +225 -42
- package/lib/frameprint.mjs +513 -108
- package/lib/frames.mjs +81 -17
- package/lib/framesize.mjs +792 -120
- package/lib/frametip.mjs +235 -0
- package/lib/gait.mjs +135 -23
- package/lib/grip.mjs +62 -13
- package/lib/guides.mjs +1403 -230
- package/lib/host.mjs +123 -21
- package/lib/inplace.mjs +171 -36
- package/lib/jigs.mjs +360 -78
- package/lib/joins.mjs +697 -118
- package/lib/jsonedit.mjs +88 -26
- package/lib/kept.mjs +84 -0
- package/lib/kit.mjs +438 -68
- package/lib/layout.mjs +2 -1
- package/lib/links.mjs +149 -42
- package/lib/list.mjs +326 -64
- package/lib/live-guard.mjs +68 -0
- package/lib/live-shape.mjs +96 -24
- package/lib/live.mjs +189 -59
- package/lib/loads.mjs +579 -130
- package/lib/machine.mjs +372 -74
- package/lib/magnets.mjs +162 -44
- package/lib/measured.mjs +417 -94
- package/lib/mesh.mjs +526 -119
- package/lib/meshcmd.mjs +215 -60
- package/lib/motors.mjs +1021 -213
- package/lib/motorsvg.mjs +114 -32
- package/lib/names.mjs +3 -1
- package/lib/optimise.mjs +978 -283
- package/lib/order.mjs +519 -112
- package/lib/panels.mjs +482 -94
- package/lib/parts.mjs +78 -23
- package/lib/pins.mjs +163 -44
- package/lib/plates.mjs +223 -35
- package/lib/player.mjs +37 -9
- package/lib/power.mjs +64 -13
- package/lib/preview.mjs +83 -16
- package/lib/profilemeasure.mjs +572 -129
- package/lib/rules.mjs +15 -5
- package/lib/sandbox-hooks.mjs +77 -19
- package/lib/sandbox-run.mjs +117 -18
- package/lib/sandbox.mjs +509 -60
- package/lib/schema.mjs +42 -11
- package/lib/seats.mjs +227 -49
- package/lib/services.mjs +261 -52
- package/lib/sheet.mjs +851 -178
- package/lib/slotnuts.mjs +213 -34
- package/lib/targets/gist.mjs +4 -1
- package/lib/targets.mjs +22 -4
- package/lib/task.mjs +376 -79
- package/lib/versions.mjs +111 -28
- package/lib/wires.mjs +228 -45
- package/lib/workspace.mjs +197 -12
- package/package.json +19 -19
- package/schemas/assembly.schema.json +2 -2
package/lib/panels.mjs
CHANGED
|
@@ -5,7 +5,17 @@
|
|
|
5
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
6
|
// it stands off the faces on printed hinges along one beam, turns on a revolute joint (poses <id>-shut and <id>-open),
|
|
7
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 {
|
|
8
|
+
import {
|
|
9
|
+
panelModel,
|
|
10
|
+
panel,
|
|
11
|
+
panelClipModel,
|
|
12
|
+
hingeModel,
|
|
13
|
+
latchModel,
|
|
14
|
+
magnetCatchModel,
|
|
15
|
+
handleModel,
|
|
16
|
+
PANEL_DEFAULTS,
|
|
17
|
+
DOOR_DEFAULTS,
|
|
18
|
+
} from '@rulecad/frames';
|
|
9
19
|
import { beamOf, beamPlace, framesContext } from './beams.mjs';
|
|
10
20
|
import { givenFor } from './frames.mjs';
|
|
11
21
|
import { named } from './parts.mjs';
|
|
@@ -15,23 +25,54 @@ import { ops as booleans } from '@rulecad/shapes';
|
|
|
15
25
|
const KEYS = ['sheet', 'thickness', 'bay', 'out', 'pitch', 'nut', 'load', 'door'];
|
|
16
26
|
const DOOR_KEYS = ['hinge', 'open', 'handle', 'catch', 'latch', 'load'];
|
|
17
27
|
const shown = (v) => (v === undefined ? 'nothing' : JSON.stringify(v));
|
|
18
|
-
const mm = (x) => String(+Number(x).toFixed(2)),
|
|
19
|
-
|
|
28
|
+
const mm = (x) => String(+Number(x).toFixed(2)),
|
|
29
|
+
r3 = (x) => +x.toFixed(3);
|
|
30
|
+
const dot = (a, b) => a[0] * b[0] + a[1] * b[1] + a[2] * b[2],
|
|
31
|
+
sub = (a, b) => [a[0] - b[0], a[1] - b[1], a[2] - b[2]],
|
|
32
|
+
add = (a, b, k = 1) => [a[0] + k * b[0], a[1] + k * b[1], a[2] + k * b[2]];
|
|
20
33
|
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) => {
|
|
22
|
-
const
|
|
23
|
-
|
|
34
|
+
const unit = (v) => {
|
|
35
|
+
const l = Math.hypot(...v);
|
|
36
|
+
return v.map((c) => c / l);
|
|
37
|
+
};
|
|
38
|
+
const col = (f, k) => [f[k], f[4 + k], f[8 + k]],
|
|
39
|
+
org = (f) => [f[3], f[7], f[11]];
|
|
40
|
+
const world = (f, p) =>
|
|
41
|
+
[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
42
|
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
43
|
// a world point and direction in a frame's own coordinates (its rows' transpose)
|
|
26
|
-
const local = (f, p) => {
|
|
44
|
+
const local = (f, p) => {
|
|
45
|
+
const q = sub(p, org(f));
|
|
46
|
+
return [0, 1, 2].map((k) => r3(dot(col(f, k), q)));
|
|
47
|
+
};
|
|
27
48
|
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
49
|
|
|
30
50
|
/** The working figures of panels in assemblies, each with what it rests on. */
|
|
31
51
|
export const PANEL_CHECK = {
|
|
32
|
-
plane: {
|
|
33
|
-
|
|
34
|
-
|
|
52
|
+
plane: {
|
|
53
|
+
value: 0.05,
|
|
54
|
+
unit: 'mm',
|
|
55
|
+
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",
|
|
56
|
+
kind: 'default',
|
|
57
|
+
source: "A working figure of @rulecad/cad, the same as a join's flush",
|
|
58
|
+
checked: '2026-11-17',
|
|
59
|
+
},
|
|
60
|
+
latch_probe: {
|
|
61
|
+
value: 0.5,
|
|
62
|
+
unit: '°',
|
|
63
|
+
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",
|
|
64
|
+
kind: 'default',
|
|
65
|
+
source: 'A working figure of @rulecad/cad: the panels test sees the hit from 0.5° on a 400 x 300 mm door',
|
|
66
|
+
checked: '2026-11-17',
|
|
67
|
+
},
|
|
68
|
+
inset: {
|
|
69
|
+
value: 2,
|
|
70
|
+
unit: 'mm',
|
|
71
|
+
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",
|
|
72
|
+
kind: 'default',
|
|
73
|
+
source: 'A working figure of @rulecad/cad',
|
|
74
|
+
checked: '2026-11-17',
|
|
75
|
+
},
|
|
35
76
|
};
|
|
36
77
|
|
|
37
78
|
/**
|
|
@@ -43,155 +84,457 @@ export const PANEL_CHECK = {
|
|
|
43
84
|
* @returns {{ a: object, panels: object[] }}
|
|
44
85
|
*/
|
|
45
86
|
export function expandPanels(a, num, context = framesContext(), material = 'pla') {
|
|
46
|
-
const items = a.items || [],
|
|
87
|
+
const items = a.items || [],
|
|
88
|
+
list = items.filter((it) => it.panel);
|
|
47
89
|
if (!list.length) return { a, panels: [] };
|
|
48
|
-
const beams = new Map(items.filter((it) => it.beam).map((it) => [it.id, it])),
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
const
|
|
90
|
+
const beams = new Map(items.filter((it) => it.beam).map((it) => [it.id, it])),
|
|
91
|
+
ids = new Set(items.map((it) => it.id)),
|
|
92
|
+
models = new Map();
|
|
93
|
+
const modelOf = (id) => {
|
|
94
|
+
if (!models.has(id)) models.set(id, beamOf(beams.get(id), num, context).model);
|
|
95
|
+
return models.get(id);
|
|
96
|
+
};
|
|
97
|
+
const free = (base) => {
|
|
98
|
+
let n = base,
|
|
99
|
+
k = 2;
|
|
100
|
+
while (ids.has(n)) n = `${base}-${k++}`;
|
|
101
|
+
ids.add(n);
|
|
102
|
+
return n;
|
|
103
|
+
};
|
|
104
|
+
const added = [],
|
|
105
|
+
fasten = [],
|
|
106
|
+
joints = [],
|
|
107
|
+
poses = {},
|
|
108
|
+
panels = [],
|
|
109
|
+
replaced = new Map();
|
|
52
110
|
for (const it of list) {
|
|
53
|
-
const what = `item ${it.id}`,
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
111
|
+
const what = `item ${it.id}`,
|
|
112
|
+
P = it.panel;
|
|
113
|
+
if (!P || typeof P !== 'object' || Array.isArray(P))
|
|
114
|
+
throw new Error(
|
|
115
|
+
`${what}: panel is { "sheet": "acrylic", "thickness": 3, "bay": ["x1", "z1", "x2", "z2"], "out": [0, -1, 0] } (got ${shown(P)})`,
|
|
116
|
+
);
|
|
117
|
+
for (const k of Object.keys(P))
|
|
118
|
+
if (!KEYS.includes(k)) throw new Error(`${what}: a panel has no "${k}": it takes ${KEYS.join(', ')}`);
|
|
119
|
+
if (it.at != null || it.rotate != null || it.repeat != null)
|
|
120
|
+
throw new Error(`${what}: a panel is placed by its bay: leave out at, rotate and repeat`);
|
|
57
121
|
const T = typeof P.thickness === 'number' ? P.thickness : num(P.thickness ?? NaN);
|
|
58
|
-
if (!(Array.isArray(P.bay) && P.bay.length === 4))
|
|
59
|
-
|
|
122
|
+
if (!(Array.isArray(P.bay) && P.bay.length === 4))
|
|
123
|
+
throw new Error(
|
|
124
|
+
`${what}: bay is the four beams round it, in order, as ["x1", "z1", "x2", "z2"] (got ${shown(P.bay)})`,
|
|
125
|
+
);
|
|
126
|
+
for (const b of P.bay)
|
|
127
|
+
if (!beams.has(b))
|
|
128
|
+
throw new Error(
|
|
129
|
+
`${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'})`,
|
|
130
|
+
);
|
|
60
131
|
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))
|
|
132
|
+
if (!(Array.isArray(P.out) && P.out.length === 3 && Math.hypot(...P.out.map(num)) > 0))
|
|
133
|
+
throw new Error(
|
|
134
|
+
`${what}: out is the way the panel's face looks, [x, y, z], square to the bay (got ${shown(P.out)})`,
|
|
135
|
+
);
|
|
62
136
|
const out = unit(P.out.map(num));
|
|
63
137
|
// each beam: its frame, its slotted face looking along out, that face's plane and its slot's line
|
|
64
138
|
const edges = P.bay.map((id) => {
|
|
65
|
-
const it2 = beams.get(id),
|
|
66
|
-
|
|
139
|
+
const it2 = beams.get(id),
|
|
140
|
+
f = beamPlace(it2, num),
|
|
141
|
+
B = modelOf(id);
|
|
142
|
+
if (!f)
|
|
143
|
+
throw new Error(
|
|
144
|
+
`${what}: a panel's bay is worked out from where its beams are: place ${id} by its ends (from, to)`,
|
|
145
|
+
);
|
|
67
146
|
const d = col(f, 2);
|
|
68
|
-
if (Math.abs(dot(d, out)) > 1e-6)
|
|
147
|
+
if (Math.abs(dot(d, out)) > 1e-6)
|
|
148
|
+
throw new Error(`${what}: ${id} runs across the bay's plane, not in it: its beams lie square to out`);
|
|
69
149
|
const place = B.places.find((q) => q.slot && dot(turn(f, [...q.normal, 0]), out) > 1 - 1e-6);
|
|
70
|
-
if (!place)
|
|
150
|
+
if (!place)
|
|
151
|
+
throw new Error(`${what}: ${id} has no slotted face that looks along out: turn it with roll so one does`);
|
|
71
152
|
const S = world(f, [place.at[0], place.at[1], 0]);
|
|
72
|
-
return {
|
|
153
|
+
return {
|
|
154
|
+
id,
|
|
155
|
+
f,
|
|
156
|
+
B,
|
|
157
|
+
d,
|
|
158
|
+
place,
|
|
159
|
+
S,
|
|
160
|
+
h: dot(S, out),
|
|
161
|
+
half: Math.max(B.profile.width, B.profile.height) / 2,
|
|
162
|
+
profile: B.profile.name,
|
|
163
|
+
length: B.length,
|
|
164
|
+
};
|
|
73
165
|
});
|
|
74
|
-
const h = edges[0].h,
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
166
|
+
const h = edges[0].h,
|
|
167
|
+
off = edges.find((e) => Math.abs(e.h - h) > PANEL_CHECK.plane.value);
|
|
168
|
+
if (off)
|
|
169
|
+
throw new Error(
|
|
170
|
+
`${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`,
|
|
171
|
+
);
|
|
172
|
+
const u = edges[0].d,
|
|
173
|
+
v = cross(out, u),
|
|
174
|
+
along = (e) => (Math.abs(dot(e.d, u)) > 1 - 1e-6 ? 'u' : Math.abs(dot(e.d, v)) > 1 - 1e-6 ? 'v' : null);
|
|
175
|
+
const us = edges.filter((e) => along(e) === 'u'),
|
|
176
|
+
vs = edges.filter((e) => along(e) === 'v');
|
|
177
|
+
if (us.length !== 2 || vs.length !== 2)
|
|
178
|
+
throw new Error(
|
|
179
|
+
`${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`,
|
|
180
|
+
);
|
|
79
181
|
// 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 [
|
|
81
|
-
|
|
82
|
-
|
|
182
|
+
for (const [pair, ax] of [
|
|
183
|
+
[us, v],
|
|
184
|
+
[vs, u],
|
|
185
|
+
]) {
|
|
186
|
+
const c = pair.map((e) => dot(e.S, ax)),
|
|
187
|
+
lo = c[0] < c[1] ? 0 : 1;
|
|
188
|
+
if (Math.abs(c[0] - c[1]) < 1)
|
|
189
|
+
throw new Error(
|
|
190
|
+
`${what}: ${pair[0].id} and ${pair[1].id} lie on one line: a bay has two beams each way, apart`,
|
|
191
|
+
);
|
|
83
192
|
pair.forEach((e, k) => Object.assign(e, { ax, c: c[k], in: k === lo ? 1 : -1 }));
|
|
84
193
|
}
|
|
85
|
-
const span = (e) => {
|
|
86
|
-
|
|
194
|
+
const span = (e) => {
|
|
195
|
+
const other = e.ax === v ? vs : us,
|
|
196
|
+
q = e.ax === v ? u : v,
|
|
197
|
+
cs = other.map((o) => dot(o.S, q)).sort((x, y) => x - y);
|
|
198
|
+
return { q, lo: cs[0], hi: cs[1], half: Math.max(...other.map((o) => o.half)) };
|
|
199
|
+
};
|
|
200
|
+
const sheet = P.sheet,
|
|
201
|
+
given = (e) => givenFor(context, e.profile);
|
|
87
202
|
const door = P.door != null ? doorSpec(what, P.door, P.bay, num) : null;
|
|
88
203
|
// where each edge's panel edge goes, from its slot line into the bay
|
|
89
|
-
let clipOf = null,
|
|
90
|
-
|
|
91
|
-
|
|
204
|
+
let clipOf = null,
|
|
205
|
+
hm = null,
|
|
206
|
+
edgeIn = {};
|
|
207
|
+
const clipFor = (e) => {
|
|
208
|
+
try {
|
|
209
|
+
return panelClipModel(e.profile, {
|
|
210
|
+
sheet: T,
|
|
211
|
+
given: given(e),
|
|
212
|
+
material,
|
|
213
|
+
...(P.nut ? { nut: P.nut } : {}),
|
|
214
|
+
...(P.load != null ? { load: num(P.load) } : {}),
|
|
215
|
+
});
|
|
216
|
+
} catch (x) {
|
|
217
|
+
throw new Error(`${what}: ${String(x.message).replace(/^\w+: /, '')}`);
|
|
218
|
+
}
|
|
219
|
+
};
|
|
220
|
+
try {
|
|
221
|
+
panelModel(sheet, T, { size: [1, 1], corner: 0 });
|
|
222
|
+
} catch (x) {
|
|
223
|
+
throw new Error(`${what}: ${String(x.message).replace(/^panelModel: /, '')}`);
|
|
224
|
+
}
|
|
92
225
|
if (door) {
|
|
93
226
|
const H = edges.find((e) => e.id === door.hinge);
|
|
94
|
-
try {
|
|
95
|
-
|
|
227
|
+
try {
|
|
228
|
+
hm = hingeModel(H.profile, {
|
|
229
|
+
sheet: T,
|
|
230
|
+
given: given(H),
|
|
231
|
+
material,
|
|
232
|
+
...(door.load != null ? { load: door.load } : {}),
|
|
233
|
+
});
|
|
234
|
+
} catch (x) {
|
|
235
|
+
throw new Error(`${what}: ${String(x.message).replace(/^\w+: /, '')}`);
|
|
236
|
+
}
|
|
237
|
+
const probe = latchModel(H.profile, { sheet: T, standoff: hm.standoff, given: given(H), material }),
|
|
238
|
+
clip = clipFor(H);
|
|
96
239
|
for (const e of edges) edgeIn[e.id] = e.id === door.hinge ? hm.edge : r3(Math.max(clip.edge, probe.edge));
|
|
97
|
-
} else {
|
|
240
|
+
} else {
|
|
241
|
+
clipOf = new Map(edges.map((e) => [e.id, clipFor(e)]));
|
|
242
|
+
for (const e of edges) edgeIn[e.id] = clipOf.get(e.id).edge;
|
|
243
|
+
}
|
|
98
244
|
// the panel's rectangle in (u, v): between its edges, each set in from its slot line
|
|
99
|
-
const lim = (pair) => {
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
const
|
|
104
|
-
|
|
245
|
+
const lim = (pair) => {
|
|
246
|
+
const [p, q] = pair[0].in > 0 ? pair : [pair[1], pair[0]];
|
|
247
|
+
return [p.c + edgeIn[p.id], q.c - edgeIn[q.id]];
|
|
248
|
+
};
|
|
249
|
+
const [u0, u1] = lim(vs),
|
|
250
|
+
[v0, v1] = lim(us);
|
|
251
|
+
if (!(u1 - u0 > 1 && v1 - v0 > 1))
|
|
252
|
+
throw new Error(
|
|
253
|
+
`${what}: the bay is too small for a panel held at its edges: ${mm(u1 - u0)} x ${mm(v1 - v0)} mm between them`,
|
|
254
|
+
);
|
|
255
|
+
const lift = door ? hm.standoff : 0,
|
|
256
|
+
O = add(add(add([0, 0, 0], u, u0), v, v0), out, h + lift),
|
|
257
|
+
F = { u, v, w: out, O };
|
|
258
|
+
const toPanel = (p) => [r3(dot(sub(p, O), u)), r3(dot(sub(p, O), v)), r3(dot(sub(p, O), out))],
|
|
259
|
+
dirPanel = (q) => [r3(dot(q, u)), r3(dot(q, v)), r3(dot(q, out))];
|
|
260
|
+
const holes = [],
|
|
261
|
+
anchors = {};
|
|
105
262
|
// a part on an edge's beam at a distance along it: its address, turned so its y looks into the bay
|
|
106
263
|
const onBeam = (e, at, part) => {
|
|
107
|
-
const dist = r3(dot(sub(at, org(e.f)), e.d)),
|
|
108
|
-
|
|
264
|
+
const dist = r3(dot(sub(at, org(e.f)), e.d)),
|
|
265
|
+
yIn = e.ax.map((c) => c * e.in),
|
|
266
|
+
ylocal = cross(out, e.d);
|
|
267
|
+
return {
|
|
268
|
+
at: `${e.id}.${e.place.name}@${dist}`,
|
|
269
|
+
...(dot(ylocal, yIn) > 0 ? {} : { rotate: [0, 0, 180] }),
|
|
270
|
+
dist,
|
|
271
|
+
x: dot(ylocal, yIn) > 0 ? e.d : e.d.map((c) => -c),
|
|
272
|
+
part,
|
|
273
|
+
};
|
|
109
274
|
};
|
|
110
275
|
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
276
|
const record = { id: it.id, sheet, thickness: T, door: !!door, bay: [...P.bay], items: [], fasten: [] };
|
|
112
277
|
if (!door) {
|
|
113
278
|
const pitch = P.pitch != null ? num(P.pitch) : PANEL_DEFAULTS.pitch.value;
|
|
114
|
-
if (!(pitch > 0))
|
|
279
|
+
if (!(pitch > 0))
|
|
280
|
+
throw new Error(
|
|
281
|
+
`${what}: pitch is the most between clips along an edge, in mm, above 0 (got ${shown(P.pitch)})`,
|
|
282
|
+
);
|
|
115
283
|
for (const e of edges) {
|
|
116
|
-
const cm = clipOf.get(e.id),
|
|
284
|
+
const cm = clipOf.get(e.id),
|
|
285
|
+
sp = span(e),
|
|
286
|
+
clear =
|
|
287
|
+
Math.max(sp.half, ...edges.filter((o) => o.ax !== e.ax).map((o) => clipOf.get(o.id).reach)) +
|
|
288
|
+
cm.width / 2 +
|
|
289
|
+
PANEL_CHECK.inset.value,
|
|
290
|
+
lo = sp.lo + clear,
|
|
291
|
+
hi = sp.hi - clear; // (clear of the beams across its ends and of their clips)
|
|
117
292
|
if (hi < lo) throw new Error(`${what}: ${e.id} is too short for a clip between the beams across its ends`);
|
|
118
293
|
const n = Math.max(2, Math.ceil((hi - lo) / pitch) + 1);
|
|
119
294
|
for (let k = 0; k < n; k++) {
|
|
120
|
-
const s = n === 1 ? (lo + hi) / 2 : lo + ((hi - lo) * k) / (n - 1),
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
295
|
+
const s = n === 1 ? (lo + hi) / 2 : lo + ((hi - lo) * k) / (n - 1),
|
|
296
|
+
place = onBeam(e, pointOn(e, s)),
|
|
297
|
+
id = free(`${it.id}-clip`);
|
|
298
|
+
const spec = {
|
|
299
|
+
kind: 'panel-clip',
|
|
300
|
+
profile: e.profile,
|
|
301
|
+
sheet: T,
|
|
302
|
+
...(P.nut ? { nut: P.nut } : {}),
|
|
303
|
+
...(P.load != null ? { load: num(P.load) } : {}),
|
|
304
|
+
};
|
|
305
|
+
added.push({
|
|
306
|
+
id,
|
|
307
|
+
fitting: spec,
|
|
308
|
+
at: place.at,
|
|
309
|
+
...(place.rotate ? { rotate: place.rotate } : {}),
|
|
310
|
+
panelOf: it.id,
|
|
311
|
+
});
|
|
312
|
+
fasten.push({
|
|
313
|
+
screw: `${cm.thread} ${cm.head}`,
|
|
314
|
+
through: [id],
|
|
315
|
+
into: `${e.id}.${e.place.name}`,
|
|
316
|
+
at: [`${id}.b1`],
|
|
317
|
+
note: `${it.id}: a clip into ${e.id}`,
|
|
318
|
+
nut: cm.screws[0].nutKind,
|
|
319
|
+
panelOf: it.id,
|
|
320
|
+
});
|
|
124
321
|
record.items.push(id);
|
|
125
322
|
}
|
|
126
323
|
}
|
|
127
324
|
} else {
|
|
128
|
-
const H = edges.find((e) => e.id === door.hinge),
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
const
|
|
325
|
+
const H = edges.find((e) => e.id === door.hinge),
|
|
326
|
+
L = edges.find((e) => e.ax === H.ax && e !== H),
|
|
327
|
+
sp = span(H);
|
|
328
|
+
const axis = cross(
|
|
329
|
+
H.ax.map((c) => c * H.in),
|
|
330
|
+
out,
|
|
331
|
+
),
|
|
332
|
+
lo = sp.lo + sp.half + hm.length / 2 + PANEL_CHECK.inset.value,
|
|
333
|
+
hi = sp.hi - sp.half - hm.length / 2 - PANEL_CHECK.inset.value;
|
|
334
|
+
if (hi < lo + hm.length)
|
|
335
|
+
throw new Error(
|
|
336
|
+
`${what}: ${H.id} is too short for two ${mm(hm.length)} mm hinges between the beams across its ends`,
|
|
337
|
+
);
|
|
338
|
+
const jid = `${it.id}-hinge`,
|
|
339
|
+
mid = (lo + hi) / 2;
|
|
132
340
|
[lo, hi].forEach((s, k) => {
|
|
133
|
-
const P0 = pointOn(H, s),
|
|
134
|
-
|
|
135
|
-
|
|
341
|
+
const P0 = pointOn(H, s),
|
|
342
|
+
place = onBeam(H, P0),
|
|
343
|
+
fid = free(`${it.id}-hinge-${k + 1}`),
|
|
344
|
+
did = free(`${it.id}-leaf-${k + 1}`);
|
|
345
|
+
added.push({
|
|
346
|
+
id: fid,
|
|
347
|
+
fitting: {
|
|
348
|
+
kind: 'hinge',
|
|
349
|
+
profile: H.profile,
|
|
350
|
+
sheet: T,
|
|
351
|
+
part: 'frame',
|
|
352
|
+
...(door.load != null ? { load: door.load } : {}),
|
|
353
|
+
},
|
|
354
|
+
at: place.at,
|
|
355
|
+
...(place.rotate ? { rotate: place.rotate } : {}),
|
|
356
|
+
panelOf: it.id,
|
|
357
|
+
});
|
|
358
|
+
fasten.push({
|
|
359
|
+
screw: `${hm.thread} button`,
|
|
360
|
+
through: [fid],
|
|
361
|
+
into: `${H.id}.${H.place.name}`,
|
|
362
|
+
at: [`${fid}.b1`, `${fid}.b2`],
|
|
363
|
+
note: `${it.id}: hinge ${k + 1} into ${H.id}`,
|
|
364
|
+
nut: hm.screws[0].nutKind,
|
|
365
|
+
panelOf: it.id,
|
|
366
|
+
});
|
|
136
367
|
// 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
368
|
anchors[`h${k + 1}`] = { at: toPanel(P0), axis: [0, 0, 1], ref: dirPanel(axis) };
|
|
138
|
-
added.push({
|
|
369
|
+
added.push({
|
|
370
|
+
id: did,
|
|
371
|
+
fitting: {
|
|
372
|
+
kind: 'hinge',
|
|
373
|
+
profile: H.profile,
|
|
374
|
+
sheet: T,
|
|
375
|
+
part: 'door',
|
|
376
|
+
...(door.load != null ? { load: door.load } : {}),
|
|
377
|
+
},
|
|
378
|
+
at: `${it.id}.h${k + 1}`,
|
|
379
|
+
panelOf: it.id,
|
|
380
|
+
});
|
|
139
381
|
const yIn = H.ax.map((c) => c * H.in);
|
|
140
382
|
hm.door.forEach((q, j) => {
|
|
141
|
-
const W = add(add(add(P0, axis, q.at[0]), yIn, q.at[1]), out, q.at[2]),
|
|
383
|
+
const W = add(add(add(P0, axis, q.at[0]), yIn, q.at[1]), out, q.at[2]),
|
|
384
|
+
p = toPanel(W);
|
|
142
385
|
holes.push({ thread: hm.screw, at: [p[0], p[1]] });
|
|
143
386
|
anchors[`d${k + 1}${j + 1}`] = { at: p, axis: [0, 0, -1] };
|
|
144
387
|
});
|
|
145
|
-
fasten.push({
|
|
388
|
+
fasten.push({
|
|
389
|
+
screw: `${hm.screw} button`,
|
|
390
|
+
through: [did],
|
|
391
|
+
into: it.id,
|
|
392
|
+
at: hm.door.map((_, j) => `${did}.d${j + 1}`),
|
|
393
|
+
nut: true,
|
|
394
|
+
note: `${it.id}: hinge ${k + 1}'s leaf through the door`,
|
|
395
|
+
panelOf: it.id,
|
|
396
|
+
});
|
|
146
397
|
record.items.push(fid, did);
|
|
147
398
|
});
|
|
148
399
|
// the joint: about the hinges' axis, its parent the hinge beam, the door's anchor "pivot" on it
|
|
149
|
-
const Pax = add(
|
|
400
|
+
const Pax = add(
|
|
401
|
+
add(
|
|
402
|
+
add(
|
|
403
|
+
pointOn(H, mid),
|
|
404
|
+
H.ax.map((c) => c * H.in),
|
|
405
|
+
hm.axis[0],
|
|
406
|
+
),
|
|
407
|
+
out,
|
|
408
|
+
hm.axis[1],
|
|
409
|
+
),
|
|
410
|
+
[0, 0, 0],
|
|
411
|
+
);
|
|
150
412
|
anchors.pivot = { at: toPanel(Pax), axis: dirPanel(axis), ref: [0, 0, 1] };
|
|
151
|
-
joints.push({
|
|
152
|
-
|
|
413
|
+
joints.push({
|
|
414
|
+
id: jid,
|
|
415
|
+
type: 'revolute',
|
|
416
|
+
parent: { item: H.id, at: local(H.f, Pax), axis: localDir(H.f, axis), ref: localDir(H.f, out) },
|
|
417
|
+
child: `${it.id}.pivot`,
|
|
418
|
+
min: 0,
|
|
419
|
+
max: door.open,
|
|
420
|
+
label: `${it.id} opens`,
|
|
421
|
+
});
|
|
422
|
+
poses[`${it.id}-shut`] = { [jid]: 0 };
|
|
423
|
+
poses[`${it.id}-open`] = { [jid]: door.open };
|
|
153
424
|
// the handle on the door's outer face, in from the latch side; screwed from inside into its feet
|
|
154
425
|
if (door.handle) {
|
|
155
|
-
const hd = handleModel({ sheet: T, material }),
|
|
426
|
+
const hd = handleModel({ sheet: T, material }),
|
|
427
|
+
hid = free(`${it.id}-handle`),
|
|
428
|
+
inward = L.ax.map((c) => c * L.in),
|
|
429
|
+
at = add(add(pointOn(L, mid), inward, edgeIn[L.id] + 20), out, lift + T),
|
|
430
|
+
p = toPanel(at);
|
|
156
431
|
const ref = cross(axis, out); // (the handle's x across it, so its y runs along the latch side)
|
|
157
432
|
anchors.handle = { at: p, axis: [0, 0, 1], ref: dirPanel(ref) };
|
|
158
433
|
added.push({ id: hid, fitting: { kind: 'handle', sheet: T }, at: `${it.id}.handle`, panelOf: it.id });
|
|
159
|
-
[-1, 1].forEach((sgn, j) => {
|
|
160
|
-
|
|
434
|
+
[-1, 1].forEach((sgn, j) => {
|
|
435
|
+
const q = add(at, axis, (sgn * hd.span) / 2),
|
|
436
|
+
pp = toPanel(q);
|
|
437
|
+
holes.push({ thread: hd.thread, at: [pp[0], pp[1]] });
|
|
438
|
+
anchors[`k${j + 1}`] = { at: [pp[0], pp[1], 0], axis: [0, 0, 1] };
|
|
439
|
+
});
|
|
440
|
+
fasten.push({
|
|
441
|
+
screw: `${hd.thread} button`,
|
|
442
|
+
through: [it.id],
|
|
443
|
+
into: hid,
|
|
444
|
+
at: [`${it.id}.k1`, `${it.id}.k2`],
|
|
445
|
+
note: `${it.id}: the handle`,
|
|
446
|
+
panelOf: it.id,
|
|
447
|
+
});
|
|
161
448
|
record.items.push(hid);
|
|
162
449
|
}
|
|
163
450
|
// 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 [
|
|
451
|
+
for (const [want, kind] of [
|
|
452
|
+
[door.catch, 'magnet-catch'],
|
|
453
|
+
[door.latch, 'latch'],
|
|
454
|
+
]) {
|
|
165
455
|
if (!want) continue;
|
|
166
|
-
const cid = free(`${it.id}-${kind === 'latch' ? 'latch' : 'catch'}`),
|
|
167
|
-
|
|
168
|
-
const
|
|
169
|
-
|
|
170
|
-
|
|
456
|
+
const cid = free(`${it.id}-${kind === 'latch' ? 'latch' : 'catch'}`),
|
|
457
|
+
place = onBeam(L, pointOn(L, kind === 'latch' ? mid + 40 : mid));
|
|
458
|
+
const spec = {
|
|
459
|
+
kind,
|
|
460
|
+
profile: L.profile,
|
|
461
|
+
standoff: hm.standoff,
|
|
462
|
+
edge: edgeIn[L.id],
|
|
463
|
+
...(kind === 'latch' ? { sheet: T } : {}),
|
|
464
|
+
};
|
|
465
|
+
const cm =
|
|
466
|
+
kind === 'latch'
|
|
467
|
+
? latchModel(L.profile, { sheet: T, standoff: hm.standoff, edge: edgeIn[L.id], given: given(L), material })
|
|
468
|
+
: magnetCatchModel(L.profile, { standoff: hm.standoff, edge: edgeIn[L.id], given: given(L), material });
|
|
469
|
+
added.push({
|
|
470
|
+
id: cid,
|
|
471
|
+
fitting: spec,
|
|
472
|
+
at: place.at,
|
|
473
|
+
...(place.rotate ? { rotate: place.rotate } : {}),
|
|
474
|
+
panelOf: it.id,
|
|
475
|
+
});
|
|
476
|
+
fasten.push({
|
|
477
|
+
screw: `${cm.thread} button`,
|
|
478
|
+
through: [cid],
|
|
479
|
+
into: `${L.id}.${L.place.name}`,
|
|
480
|
+
at: [`${cid}.b1`],
|
|
481
|
+
note: `${it.id}: the ${kind.replace('-', ' ')} into ${L.id}`,
|
|
482
|
+
nut: cm.screws[0].nutKind,
|
|
483
|
+
panelOf: it.id,
|
|
484
|
+
});
|
|
171
485
|
record.items.push(cid);
|
|
172
486
|
if (kind === 'latch') Object.assign(record, { joint: jid, latch: cid });
|
|
173
487
|
}
|
|
174
488
|
}
|
|
175
489
|
const pm = panelModel(sheet, T, { size: [r3(u1 - u0), r3(v1 - v0)], holes });
|
|
176
|
-
record.model = pm;
|
|
490
|
+
record.model = pm;
|
|
491
|
+
record.size = pm.size;
|
|
492
|
+
panels.push(record);
|
|
177
493
|
const placed = { ...it, panel: { ...P }, _panel: { model: pm, anchors, frame: F } };
|
|
178
|
-
if (!door)
|
|
494
|
+
if (!door)
|
|
495
|
+
joints.push({
|
|
496
|
+
id: `${it.id}-bay`,
|
|
497
|
+
type: 'fixed',
|
|
498
|
+
parent: { at: O.map(r3), axis: out, ref: u },
|
|
499
|
+
child: { item: it.id, at: [0, 0, 0], axis: [0, 0, 1], ref: [1, 0, 0] },
|
|
500
|
+
});
|
|
179
501
|
replaced.set(it.id, placed);
|
|
180
502
|
}
|
|
181
503
|
const first = (a.fasten || []).length;
|
|
182
|
-
const out = {
|
|
504
|
+
const out = {
|
|
505
|
+
...a,
|
|
506
|
+
items: [...items.map((it) => replaced.get(it.id) ?? it), ...added],
|
|
507
|
+
...(fasten.length ? { fasten: [...(a.fasten || []), ...fasten] } : {}),
|
|
508
|
+
joints: [...(a.joints || []), ...joints],
|
|
509
|
+
poses: { ...(a.poses || {}), ...poses },
|
|
510
|
+
};
|
|
183
511
|
for (const r of panels) r.fasten = fasten.map((x, k) => (x.panelOf === r.id ? first + k : -1)).filter((k) => k >= 0);
|
|
184
512
|
return { a: out, panels };
|
|
185
513
|
}
|
|
186
514
|
|
|
187
515
|
function doorSpec(what, d, bay, num) {
|
|
188
|
-
if (!d || typeof d !== 'object' || Array.isArray(d))
|
|
189
|
-
|
|
190
|
-
|
|
516
|
+
if (!d || typeof d !== 'object' || Array.isArray(d))
|
|
517
|
+
throw new Error(`${what}: door is { "hinge": "${bay[0]}", "open": 120 } (got ${shown(d)})`);
|
|
518
|
+
for (const k of Object.keys(d))
|
|
519
|
+
if (!DOOR_KEYS.includes(k)) throw new Error(`${what}: a door has no "${k}": it takes ${DOOR_KEYS.join(', ')}`);
|
|
520
|
+
if (!bay.includes(d.hinge))
|
|
521
|
+
throw new Error(
|
|
522
|
+
`${what}: door.hinge is the beam of the bay its hinges go on, one of ${bay.join(', ')} (got ${shown(d.hinge)})`,
|
|
523
|
+
);
|
|
191
524
|
const open = d.open != null ? num(d.open) : DOOR_DEFAULTS.open.value;
|
|
192
|
-
if (!(open > 0 && open <= 180))
|
|
193
|
-
|
|
194
|
-
|
|
525
|
+
if (!(open > 0 && open <= 180))
|
|
526
|
+
throw new Error(`${what}: door.open is how far it opens, in degrees, above 0 and up to 180 (got ${shown(d.open)})`);
|
|
527
|
+
for (const k of ['handle', 'catch', 'latch'])
|
|
528
|
+
if (d[k] != null && typeof d[k] !== 'boolean')
|
|
529
|
+
throw new Error(`${what}: door.${k} is true or false (got ${shown(d[k])})`);
|
|
530
|
+
return {
|
|
531
|
+
hinge: d.hinge,
|
|
532
|
+
open,
|
|
533
|
+
handle: d.handle ?? true,
|
|
534
|
+
catch: d.catch ?? true,
|
|
535
|
+
latch: d.latch ?? false,
|
|
536
|
+
...(d.load != null ? { load: num(d.load) } : {}),
|
|
537
|
+
};
|
|
195
538
|
}
|
|
196
539
|
|
|
197
540
|
/**
|
|
@@ -201,11 +544,46 @@ function doorSpec(what, d, bay, num) {
|
|
|
201
544
|
*/
|
|
202
545
|
export async function panelBuilt(it, id) {
|
|
203
546
|
if (!it._panel) throw new Error(`item ${id}: a panel is worked out from its bay before it is built`);
|
|
204
|
-
let ops = null;
|
|
205
|
-
|
|
547
|
+
let ops = null;
|
|
548
|
+
try {
|
|
549
|
+
ops = await booleans('manifold');
|
|
550
|
+
} catch {
|
|
551
|
+
// (cut by Manifold where installed, so the overlap check reads it exactly; JSCAD's booleans otherwise)
|
|
552
|
+
}
|
|
553
|
+
const { model: m, anchors } = it._panel,
|
|
554
|
+
parts = named(
|
|
555
|
+
{
|
|
556
|
+
body: (() => {
|
|
557
|
+
try {
|
|
558
|
+
return panel(m, ops ? { ops } : {});
|
|
559
|
+
} catch {
|
|
560
|
+
return panel(m);
|
|
561
|
+
}
|
|
562
|
+
})(),
|
|
563
|
+
},
|
|
564
|
+
{ body: { label: m.title } },
|
|
565
|
+
);
|
|
206
566
|
const values = { width: m.size[0], height: m.size[1], thickness: m.thickness, grams: m.grams };
|
|
207
|
-
return referenceBuilt({
|
|
208
|
-
|
|
567
|
+
return referenceBuilt({
|
|
568
|
+
id,
|
|
569
|
+
key: 'panel',
|
|
570
|
+
objName: `panel:${m.name}`,
|
|
571
|
+
name: m.name,
|
|
572
|
+
parts,
|
|
573
|
+
anchors,
|
|
574
|
+
entries: [],
|
|
575
|
+
values,
|
|
576
|
+
extra: {
|
|
577
|
+
panel: m,
|
|
578
|
+
bought: {
|
|
579
|
+
set: `${m.sheet} sheet, ${mm(m.thickness)} mm`,
|
|
580
|
+
variant: null,
|
|
581
|
+
part: 'panel',
|
|
582
|
+
mass: m.grams / 1000,
|
|
583
|
+
size: m.size,
|
|
584
|
+
},
|
|
585
|
+
},
|
|
586
|
+
});
|
|
209
587
|
}
|
|
210
588
|
|
|
211
589
|
/**
|
|
@@ -215,6 +593,16 @@ export async function panelBuilt(it, id) {
|
|
|
215
593
|
export function panelFindings(s, f) {
|
|
216
594
|
for (const r of s.panels ?? []) {
|
|
217
595
|
const held = r.items.length;
|
|
218
|
-
for (const x of r.model.findings)
|
|
596
|
+
for (const x of r.model.findings)
|
|
597
|
+
f(
|
|
598
|
+
x.level,
|
|
599
|
+
`${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`}` : ''}`,
|
|
600
|
+
x.level === 'warn' || x.level === 'fail' ? `${r.id}: ${x.code}` : null,
|
|
601
|
+
{
|
|
602
|
+
code: x.code,
|
|
603
|
+
values: { ...(x.values ?? {}), ...(x.code === 'panel.size' ? { items: r.items } : {}) },
|
|
604
|
+
refs: { item: r.id },
|
|
605
|
+
},
|
|
606
|
+
);
|
|
219
607
|
}
|
|
220
608
|
}
|