@rulecad/cad 0.7.56 → 0.7.58

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.
@@ -0,0 +1,313 @@
1
+ // Printed fittings in assemblies (@rulecad/frames' fitting.mjs): an item with "fitting" is a printed corner bracket,
2
+ // gusset or splice plate drawn from a profile's and its slot nuts' rules and sized from a named load by finite elements
3
+ // (their results kept in .cache/fitting). A join with "fitting": "printed" adds its own: placed from where the two beams
4
+ // are, its screws into slot nuts in both as fastens (so the fasten checks choose their lengths and count them), and its
5
+ // own stiffness in place of the join's estimate.
6
+ import fs from 'node:fs';
7
+ import path from 'node:path';
8
+ import crypto from 'node:crypto';
9
+ import * as frames from '@rulecad/frames';
10
+ const { bracketModel, bracket, plateModel, plate } = frames;
11
+ import { host } from './host.mjs';
12
+ import { paths } from './workspace.mjs';
13
+ import { named } from './parts.mjs';
14
+ import { givenFor } from './frames.mjs';
15
+ import { peekRuleSet } from './rules.mjs';
16
+ import { ops as booleans } from '@rulecad/shapes';
17
+ import jscad from '@jscad/modeling';
18
+
19
+ const KEYS = ['kind', 'profile', 'other', 'face', 'otherFace', 'shape', 'brace', 'load', 'count', 'thread', 'head', 'nut'];
20
+ // The printed fittings for panels, doors and beams' ends: each kind's model and solid in @rulecad/frames, whether it
21
+ // is drawn for a profile, and the parts it has (a hinge's two leaves, a levelling foot's body and pad: an item each)
22
+ const MORE = {
23
+ 'panel-clip': ['panelClipModel', 'panelClip'], 'panel-corner': ['panelCornerModel', 'panelCorner'], 'slot-cover': ['slotCoverModel', 'slotCover'], 'panel-edge': ['panelEdgeModel', 'panelEdge'],
24
+ 'gasket-strip': ['gasketStripModel', 'gasketStrip', { profile: false }], hinge: ['hingeModel', 'hinge', { parts: ['frame', 'door'] }], handle: ['handleModel', 'handle', { profile: false }], latch: ['latchModel', 'latch'],
25
+ 'magnet-catch': ['magnetCatchModel', 'magnetCatch'], 'end-cap': ['endCapModel', 'endCap'], foot: ['footModel', 'foot'], 'levelling-foot': ['levellingFootModel', 'levellingFoot', { parts: ['body', 'pad'] }],
26
+ 'castor-plate': ['castorPlateModel', 'castorPlate'], 'stacking-foot': ['stackingFootModel', 'stackingFoot'],
27
+ 'device-mount': ['deviceMountModel', 'deviceMount'], 'fan-mount': ['fanMountModel', 'fanMount'], 'drag-chain-end': ['dragChainEndModel', 'dragChainEnd'], 'din-rail': ['dinRailAdapterModel', 'dinRailAdapter'],
28
+ 'cable-clip': ['cableClipModel', 'cableClip'], 'tie-anchor': ['tieAnchorModel', 'tieAnchor'], 'spool-holder': ['spoolHolderModel', 'spoolHolder'],
29
+ 'rail-jig': ['railJigModel', 'railJig'], 'wheel-plate': ['wheelsModel', 'wheelPlate'], slider: ['sliderModel', 'slider'],
30
+ 'nut-carrier': ['carrierModel', 'carrier', { pose: { fit: 'slot', print: 'print' } }],
31
+ 'rod-holder': ['rodHolderModel', 'rodHolder'], 'rod-carriage': ['rodCarriageModel', 'rodCarriage'], 'motor-plate': ['motorPlateModel', 'motorPlate'], 'endstop-mount': ['endstopMountModel', 'endstopMount'],
32
+ };
33
+ // the kinds whose tongue or stem stands in the slot, inside the beam's envelope: the overlap check measures them without it
34
+ const TONGUED = ['panel-clip', 'magnet-catch', 'panel-corner', 'cable-clip', 'tie-anchor', 'spool-holder', 'rail-jig', 'slider', 'end-cap'];
35
+ // parts that lie in a slot by design (an in-slot panel edge, a slot cover's strip): only what stands above the face is
36
+ // measured by the overlap check, as a tongue is left out
37
+ const IN_SLOT = ['panel-edge', 'slot-cover'];
38
+ /** The kinds of fitting an item may name. */
39
+ export const FITTING_KINDS = ['bracket', 'gusset', 'splice', ...Object.keys(MORE)];
40
+ const shown = (v) => (v === undefined ? 'nothing' : JSON.stringify(v));
41
+ const strip = (e) => String(e?.message ?? e).replace(/^(bracketModel|plateModel|bracket|plate): /, '');
42
+
43
+ /** The finite elements' results kept on disk, as the motor mount's are: .cache/fitting/<hash>.json (CAD_NO_CACHE skips it). */
44
+ export const fittingCache = {
45
+ get(key) {
46
+ if (process.env.CAD_NO_CACHE) return undefined;
47
+ const file = paths.cache('fitting', `${crypto.createHash('sha256').update(key).digest('hex').slice(0, 24)}.json`);
48
+ try { return host.exists(file) ? JSON.parse(host.readText(file)) : undefined; } catch { return undefined; }
49
+ },
50
+ set(key, v) {
51
+ if (process.env.CAD_NO_CACHE) return;
52
+ try { host.writeText(paths.cache('fitting', `${crypto.createHash('sha256').update(key).digest('hex').slice(0, 24)}.json`), JSON.stringify(v)); } catch {} // (a cache that cannot be written is solved again next time)
53
+ },
54
+ };
55
+
56
+ /**
57
+ * The project's plastic for its printed fittings: pla or petg by name; another named in project.json is read from its
58
+ * own rule set (the workspace's rules/, then the kit's) where that gives strength, modulus and density, and refused by
59
+ * name where it does not. A project with no project.json or no material is PLA.
60
+ * @param {string} project @returns {string | object} pla, petg, or the material's rule set
61
+ */
62
+ export function plasticOf(project) {
63
+ let raw;
64
+ try { raw = JSON.parse(fs.readFileSync(path.join(paths.project(project), 'project.json'), 'utf8')).material; } catch { return 'pla'; }
65
+ const m = String(raw || 'pla').trim().toLowerCase().split(/\s/)[0];
66
+ if (['pla', 'petg'].includes(m)) return m;
67
+ let set = null; try { set = peekRuleSet(m); } catch {}
68
+ const lacks = ['strength', 'modulus', 'density'].filter((k) => !(typeof set?.rules?.[k]?.value === 'number'));
69
+ if (set && !lacks.length) return set;
70
+ throw new Error(`project ${project}: its material is ${JSON.stringify(raw)}, and printed fittings are sized from their plastic's strength, modulus and density: ${set ? `rules/${m}.json gives no ${lacks.join(', ')}` : `the kit has pla and petg, and there is no rules/${m}.json`}. Give rules/${m}.json with all three, or set the material to pla or petg`);
71
+ }
72
+
73
+ /**
74
+ * The printed parts that come with a fitting the assembly places one part of: a printed levelling foot's pad, where an
75
+ * item places its body and none its pad at the same address (a generated frame's feet). The pad is printed and screwed
76
+ * in by hand once the body is on, so it goes on a plate and on the list, and is not placed in the model.
77
+ * @param {object} s a built assembly @returns {{ id: string, of: string, fitting: object, grams: number }[]}
78
+ */
79
+ export function companions(s) {
80
+ const items = s.items ?? [], out = [];
81
+ for (const o of s.objects) {
82
+ if (o.unplaced || o.fitting?.fitting !== 'levelling-foot' || !o.fitting.printed) continue;
83
+ const it = items.find((x) => x.id === o.id);
84
+ if (!it?.fitting || (it.fitting.part ?? 'body') !== 'body') continue;
85
+ if (items.some((x) => x.fitting?.kind === 'levelling-foot' && x.fitting.part === 'pad' && JSON.stringify(x.at) === JSON.stringify(it.at))) continue;
86
+ const { pose, ...spec } = it.fitting;
87
+ out.push({ id: `${o.id}-pad`, of: o.id, fitting: { ...spec, part: 'pad' }, grams: o.fitting.padGrams });
88
+ }
89
+ return out;
90
+ }
91
+
92
+ /**
93
+ * A fitting's model from an item's "fitting" (or a join's), refused by name where a key is not one it takes.
94
+ * @param {object} spec { kind: bracket | gusset | splice, profile, other, face, otherFace, shape, brace, load, count, thread, head, nut }
95
+ * @param {object} context framesContext()'s @param {string | object} [material] pla, petg or a material's rule set (plasticOf) @param {string} [what] who asks
96
+ */
97
+ export function fittingModel(spec, context, material = 'pla', what = 'fitting') {
98
+ if (!spec || typeof spec !== 'object' || Array.isArray(spec)) throw new Error(`${what}: fitting is { "kind": "bracket", "profile": "2020-misumi-hfs5" } (got ${shown(spec)})`);
99
+ if (!FITTING_KINDS.includes(spec?.kind)) throw new Error(`${what}: fitting.kind is one of ${FITTING_KINDS.join(', ')} (got ${shown(spec?.kind)})`);
100
+ if (MORE[spec.kind]) return moreModel(spec, context, material, what);
101
+ for (const k of Object.keys(spec)) if (!KEYS.includes(k)) throw new Error(`${what}: a fitting has no "${k}": it takes ${KEYS.join(', ')}`);
102
+ const given = givenFor(context, spec.profile), o = { given, material, cache: fittingCache };
103
+ for (const k of ['thread', 'head', 'nut', 'load']) if (spec[k] != null) o[k] = spec[k];
104
+ try {
105
+ if (spec.kind === 'bracket') return bracketModel(spec.profile, { ...o, ...(spec.other ? { other: spec.other } : {}), ...(spec.face ? { face: spec.face } : {}), ...(spec.otherFace ? { otherFace: spec.otherFace } : {}), ...(spec.brace != null ? { brace: spec.brace } : {}) });
106
+ return plateModel(spec.profile, { ...o, shape: spec.kind === 'splice' ? 'splice' : spec.shape ?? 'L', ...(spec.other ? { other: spec.other } : {}), ...(spec.face ? { face: spec.face } : {}), ...(spec.count != null ? { count: spec.count } : {}) });
107
+ } catch (e) { throw new Error(`${what}: ${strip(e)}`); }
108
+ }
109
+
110
+ // A panel's, a door's or an end's fitting: its options are the model's own (refused by name there), its part chosen
111
+ function moreModel(spec, context, material, what) {
112
+ const [model, , o = {}] = MORE[spec.kind], { kind, profile, part, ...rest } = spec;
113
+ // (a device mount names its device's rule set, which is read here: its holes come from it)
114
+ if (kind === 'device-mount') {
115
+ if (typeof rest.device !== 'string') throw new Error(`${what}: a device mount names its device's rule set in "device", as supply-24v (got ${shown(rest.device)})`);
116
+ try { rest.device = peekRuleSet(rest.device); } catch (e) { throw new Error(`${what}: device: ${e.message}`); }
117
+ }
118
+ if (o.parts ? part != null && !o.parts.includes(part) : part != null) throw new Error(`${what}: ${o.parts ? `part is ${o.parts.join(' or ')}` : `a ${kind} has one part: leave part out`} (got ${shown(part)})`);
119
+ if (o.profile !== false && typeof profile !== 'string') throw new Error(`${what}: a ${kind} is drawn for a profile: give "profile", as 2020-misumi-hfs5 (got ${shown(profile)})`);
120
+ if (o.profile === false && profile != null) throw new Error(`${what}: a ${kind} is drawn for no profile: leave profile out`);
121
+ // (a gasket strip is PETG, which springs back, unless the item says otherwise; the rest take the project's plastic)
122
+ try {
123
+ const m = o.profile === false ? frames[model]({ ...(kind === 'gasket-strip' ? {} : { material }), ...rest }) : frames[model](profile, { given: givenFor(context, profile), material, ...rest });
124
+ // (a printed carrier in a slot is its own kind of model: named here as a fitting, with the nut or insert it holds to buy)
125
+ if (kind === 'nut-carrier') return { ...m, fitting: 'nut-carrier', hardware: [{ kind: m.holds === 'insert' ? 'insert' : 'nut', name: `${m.thread} ${m.holds === 'insert' ? 'heat-set insert' : 'hex nut'}`, thread: m.thread, count: m.count ?? 1, why: `held in the carrier, ${m.holds === 'insert' ? 'pressed in from below' : 'slid in from the side'}` }] };
126
+ return m;
127
+ } catch (e) { throw new Error(`${what}: ${String(e?.message ?? e).replace(/^\w+: /, '')}`); }
128
+ }
129
+
130
+ /**
131
+ * An item with "fitting", built: its solid in its fitted frame as a printed part (counted on cad list by its grams), its
132
+ * figures as values (br.thickness, br.grams, br.margin, br.stiffness), and the model kept for the joins' checks.
133
+ * @param {object} it the item @param {string} id @param {object} context @param {string} [material]
134
+ */
135
+ export async function fittingBuilt(it, id, context, material = 'pla') {
136
+ // "pose": "print" lays it as it prints, for a plate (cad plates writes it so); the model is the same either way
137
+ const { pose = 'fit', ...spec } = it.fitting ?? {};
138
+ if (pose !== 'fit' && pose !== 'print') throw new Error(`item ${id}: fitting.pose is fit (in its place, the default) or print (as it prints, for a plate) (got ${shown(pose)})`);
139
+ const m = fittingModel(spec, context, material, `item ${id}`);
140
+ // joined and cut by Manifold where the workspace has it, so the solid is watertight for the overlap check (JSCAD's
141
+ // booleans leave seams that Manifold cannot read, and JSCAD's own intersection then misreads a head in its seat)
142
+ let ops = null; try { ops = await booleans('manifold'); } catch {}
143
+ if (MORE[it.fitting.kind]) return moreBuilt(it, id, m, ops, pose);
144
+ const draw = (o) => { const f = m.fitting === 'bracket' ? bracket : plate; try { return f(m, { ...o, ...(ops ? { ops } : {}) }); } catch { return f(m, o); } };
145
+ const parts = named({ body: draw({ pose }) }, { body: { label: m.title } });
146
+ // its tongues stand in the slots, inside the beams' envelopes: the overlap check measures the body without them
147
+ if (pose === 'fit') parts[0].collide = named({ body: draw({ pose: 'fit', tongues: false }) }, {})[0].geometry;
148
+ // an anchor on each screw's axis at its head's seat, z out of the fitting toward the head: b1, b2 .. into b; a1 .. into a
149
+ const count = { a: 0, b: 0 }, anchors = Object.fromEntries(m.screws.map((sc) => [`${sc.leg}${++count[sc.leg]}`, { at: m.fitting === 'bracket' ? sc.at : [sc.at[0], sc.at[1], m.thickness], axis: sc.axis.map((c) => (c ? -c : 0)) }]));
150
+ const values = { thickness: m.thickness, width: m.width, grams: m.grams, margin: m.margin, stiffness: m.stiffness.bending, load: m.load.force, ...(m.leg ? { leg: m.leg } : { arm: m.arm }) };
151
+ const entries = Object.entries({ thickness: ['mm', 'sized from the load'], grams: ['g', 'its mass'], margin: ['×', 'its strength over the peak stress at the load, by finite elements'], stiffness: ['N mm/rad', 'its own bending stiffness about the joint, by finite elements'] })
152
+ .map(([k, [unit, rule]]) => ({ name: `${m.name}.${k}`, value: values[k], unit, rule: `The printed fitting's ${rule}`, kind: 'derived', source: '@rulecad/frames fitting.mjs', checked: '2026-11-17', set: m.name }));
153
+ const hash = crypto.createHash('sha256').update(JSON.stringify([it.fitting, material, m.name, m.thickness, m.grams])).digest('hex').slice(0, 16);
154
+ const b = { object: { name: m.name, title: m.title, role: 'part', version: '0.0.0', anchors }, values, entries, deps: [], units: {}, parts, polygons: parts.flatMap((p) => p.polygons), triangles: parts.flatMap((p) => p.triangles), hash };
155
+ return { key: `fitting:${id}:${hash}`, objName: `fitting:${m.name}`, b, version: '0.0.0', role: 'part', fitting: m };
156
+ }
157
+
158
+ // A panel's, a door's or an end's fitting built: fitted, its tongue (where it has one) left out of the overlap check;
159
+ // an anchor on each screw's axis at its head (b1, b2 .. for screws into a slot or a bore; d1, d2 .. for a hinge's
160
+ // door leaf's, through the door); a hinge's door leaf and a levelling foot's pad carry no findings or hardware of their own
161
+ function moreBuilt(it, id, m, ops, pose = 'fit') {
162
+ const [, solid, o = {}] = MORE[it.fitting.kind], part = it.fitting.part ?? o.parts?.[0], second = o.parts && part !== o.parts[0];
163
+ const drawn = o.pose?.[pose] ?? pose, draw = (x) => { const f = (y) => frames[solid](m, { pose: drawn, ...(part ? { part } : {}), ...x, ...y }); try { return f(ops ? { ops } : {}); } catch { return f({}); } };
164
+ const parts = named({ body: draw({}) }, { body: { label: `${m.title}${part ? `, ${part}` : ''}` } });
165
+ if (TONGUED.includes(m.fitting) && pose === 'fit') parts[0].collide = named({ body: draw({ tongues: false }) }, {})[0].geometry;
166
+ if (IN_SLOT.includes(m.fitting) && pose === 'fit') { const { intersect } = jscad.booleans, { cuboid } = jscad.primitives, above = intersect(parts[0].geometry, cuboid({ size: [1e4, 1e4, 1e3], center: [0, 0, 500] })); parts[0].collide = above; }
167
+ const list = it.fitting.kind === 'hinge' && part === 'door' ? m.door.map((q, k) => [`d${k + 1}`, q]) : (m.screws ?? []).filter((q) => q.into !== 'castor' && q.into !== 'pilot').map((q, k) => [`b${k + 1}`, q]);
168
+ const anchors = { ...Object.fromEntries(list.map(([n, q]) => [n, { at: q.at, axis: q.axis.map((c) => (c ? -c : 0)) }])), ...(m.anchors ?? {}) }; // (and its own: a mount's device and screws, a clip's cable)
169
+ const values = { grams: m.grams, ...(m.margin != null && Number.isFinite(m.margin) ? { margin: m.margin } : {}), ...(m.load?.force ? { load: m.load.force } : {}) };
170
+ const hash = crypto.createHash('sha256').update(JSON.stringify([it.fitting, m.name, m.grams, part])).digest('hex').slice(0, 16);
171
+ const b = { object: { name: `${m.name}${part ? `-${part}` : ''}`, title: m.title, role: 'part', version: '0.0.0', anchors }, values, entries: [], deps: [], units: {}, parts, polygons: parts.flatMap((p) => p.polygons), triangles: parts.flatMap((p) => p.triangles), hash };
172
+ const model = second ? { ...m, findings: [], hardware: [] } : m;
173
+ return { key: `fitting:${id}:${hash}`, objName: `fitting:${m.name}${part ? `-${part}` : ''}`, b, version: '0.0.0', role: 'part', fitting: model };
174
+ }
175
+
176
+ // ---------- a join's printed fitting, placed ----------
177
+ const col = (f, k) => [f[k], f[4 + k], f[8 + k]], org = (f) => [f[3], f[7], f[11]];
178
+ 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]];
179
+ 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]];
180
+ 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]);
181
+ 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]);
182
+ const mm = (x) => String(+Number(x).toFixed(2)), r3 = (x) => +x.toFixed(3);
183
+ // a place's face as slotSide takes it: top, or top.2 where the face has two slots
184
+ const faceOf = (B, place) => (B.places.filter((q) => q.face === place.face).length > 1 ? place.name : place.face);
185
+
186
+ /**
187
+ * Where a join's printed fitting goes, and what it adds: an item placed at an address on b, and a fasten for each leg
188
+ * or arm into its beam's slot. Refused by name: beams not placed by their ends, a's faces not square to b's axis, a
189
+ * bracket with no room on b.
190
+ * @param {object} j the join as written @param {number} i its index @param {{ ia: object, ib: object, A: object, B: object, m: object, ends: object }} beams
191
+ * @param {(x: any) => number} num @param {object} context @param {string} material @param {Set<string>} ids the item ids taken
192
+ * @returns {{ item: object, fasten: object[], model: object, stiffness: object }}
193
+ */
194
+ export function joinFitting(j, i, { ia, ib, A, B, m, ends, place: placeOf }, num, context, material, ids) {
195
+ const what = `join ${i + 1}`, label = j.note || `${j.kind} join of ${j.a} to ${j.b}`, bm = m.fitting?.bought ?? null, made = bm ? bm.set : `a printed ${j.kind}`;
196
+ const fa = placeOf(ia, num), fb = placeOf(ib, num);
197
+ if (!fa || !fb) throw new Error(`${what}: ${made} is placed from where the beams are: place ${!fa ? ia.id : ib.id} by its ends (from, to)`);
198
+ const free = (base) => { let n = base, k = 2; while (ids.has(n)) n = `${base}-${k++}`; ids.add(n); return n; };
199
+ // a bought anchor connector: its post along a's axis from b's face, at the place on b the join names
200
+ if (bm && j.kind === 'anchor') {
201
+ const id = free(`${ia.id}-${ib.id}-${j.kind}`);
202
+ return { item: { id, bought: bm.set, at: `${ib.id}.${m.b.place}@${r3(m.b.distance)}`, join: i }, fasten: [], model: bm };
203
+ }
204
+ // its screws, each by the set's thread, length and head, into the set's nut where it names one (a corner bracket's
205
+ // and a plate's); an inner connector's grub screws are part of it
206
+ const boughtFasten = (id, legs) => legs.map(([leg, beam, place]) => ({ screw: `${bm.thread}${bm.length != null ? `x${bm.length}` : ''} ${bm.head}`, through: [id], into: `${beam}.${place}`, at: bm.screws.filter((q) => q.leg === leg).map((_, k) => `${id}.${leg}${k + 1}`), note: `${label}: ${bm.set} into ${beam}`, join: i, ...(bm.nutKind ? { nut: bm.nutKind } : {}) }));
207
+ if (j.kind === 'bracket' || j.kind === 'hidden') {
208
+ const bp = B.places.find((q) => q.name === m.b.place), d = m.b.distance, ub = col(fb, 2), nb = turn(fb, [...bp.normal, 0]);
209
+ const P = world(fb, [bp.at[0], bp.at[1], d]), roomB = B.length - d, sign = roomB >= d ? 1 : -1, dir = ub.map((c) => c * sign);
210
+ // a's face that looks along b, toward the side with more room; of its slots, the one in the bracket's plane
211
+ const z = cross(dir, nb), faces = A.places.filter((q) => q.slot && dot(turn(fa, [...q.normal, 0]), dir) > 1 - 1e-6);
212
+ if (!faces.length) throw new Error(`${what}: a printed bracket lies on a face of ${ia.id} that looks along ${ib.id}, and none of ${ia.id}'s slotted faces does: turn ${ia.id} with roll so one faces along ${ib.id}`);
213
+ const ap = faces.map((q) => ({ q, Q: world(fa, [q.at[0], q.at[1], 0]) })).sort((x, y) => Math.abs(dot(sub(x.Q, P), z)) - Math.abs(dot(sub(y.Q, P), z)))[0];
214
+ const off = dot(sub(ap.Q, P), dir), at = d + sign * off, spec = { kind: 'bracket', profile: B.profile.name, ...(A.profile.name !== B.profile.name ? { other: A.profile.name } : {}), face: faceOf(B, bp), otherFace: faceOf(A, ap.q), ...(j.brace != null ? { brace: j.brace } : {}), ...(j.load != null ? { load: j.load } : {}), ...(j.nut ? { nut: j.nut } : {}) };
215
+ const fm = bm ?? fittingModel(spec, context, material, what), room = sign > 0 ? B.length - at : at, endA = ends.a.rest[0], aRoom = A.length, said = bm ? `${bm.set}'s` : 'a printed bracket\'s';
216
+ if (room < fm.leg - 1e-6) throw new Error(`${what}: ${said} ${mm(fm.leg)} mm leg runs ${mm(fm.leg - room)} mm past ${ib.id}'s end ${sign > 0 ? 'b' : 'a'}: it goes on the side of ${ia.id} with more of ${ib.id}, and there is only ${mm(room)} mm there. A gusset or a blind join will do`);
217
+ if (aRoom < (fm.legA ?? fm.leg) - 1e-6) throw new Error(`${what}: ${said} ${mm(fm.legA ?? fm.leg)} mm leg is longer than ${ia.id} (${mm(aRoom)} mm)`);
218
+ const id = free(`${ia.id}-${ib.id}-${bm ? j.kind : 'bracket'}`);
219
+ if (bm) {
220
+ const item = { id, bought: bm.set, at: `${ib.id}.${bp.name}@${r3(at)}`, rotate: sign > 0 ? [90, 0, 0] : [90, 0, 180], join: i };
221
+ return { item, fasten: j.kind === 'hidden' ? [] : boughtFasten(id, [['b', ib.id, bp.name], ['a', ia.id, ap.q.name]]), model: bm, side: sign > 0 ? 'b' : 'a', endA };
222
+ }
223
+ const item = { id, fitting: spec, at: `${ib.id}.${bp.name}@${r3(at)}`, rotate: sign > 0 ? [90, 0, 0] : [90, 0, 180], join: i };
224
+ const screw = `${fm.thread} ${fm.head}`;
225
+ // (each into the slot nut the bracket was drawn for, so the screw it was thickened for is the one chosen)
226
+ const fasten = [{ screw, through: [id], into: `${ib.id}.${bp.name}`, at: [`${id}.b1`], note: `${label}: the bracket into ${ib.id}`, join: i, nut: j.nut ?? fm.screws[0].nutKind },
227
+ { screw, through: [id], into: `${ia.id}.${ap.q.name}`, at: [`${id}.a1`], note: `${label}: the bracket into ${ia.id}`, join: i, nut: j.nut ?? fm.screws[1].nutKind }];
228
+ return { item, fasten, model: fm, side: sign > 0 ? 'b' : 'a', endA };
229
+ }
230
+ // a plate (a gusset, or a splice plate): on a face of each beam, the two in one plane. Its frame: x along b away from
231
+ // the joint (a gusset's toward b's end with more room; a splice's into b), y along a away from b (a splice's across),
232
+ // z out of the faces; it sits at an address on b, turned in that frame
233
+ const splice = j.kind === 'splice', ua = col(fa, 2).map((c) => (ends.a.rest[0] === 'a' ? c : -c)), ub = col(fb, 2);
234
+ const d = splice ? (m.b.end === 'a' ? 0 : B.length) : m.b.distance;
235
+ let x, y, n, shapeAt = null;
236
+ if (splice) {
237
+ x = ub.map((c) => (m.b.end === 'a' ? c : -c));
238
+ const bq = B.places.find((q) => q.slot && Math.abs(dot(turn(fb, [...q.normal, 0]), x)) < 1e-6 && A.places.some((r) => r.slot && dot(turn(fa, [...r.normal, 0]), turn(fb, [...q.normal, 0])) > 1 - 1e-6));
239
+ if (!bq) throw new Error(`${what}: a printed splice plate lies on a slotted face of both beams, in one plane, and ${ia.id} and ${ib.id} share none`);
240
+ n = turn(fb, [...bq.normal, 0]); y = cross(n, x);
241
+ } else {
242
+ const half = Math.max(A.profile.width, A.profile.height) / 2;
243
+ const room = { b: B.length - d - half, a: d - half };
244
+ shapeAt = { room };
245
+ x = ub.map((c) => (room.b >= room.a ? c : -c)); y = ua; n = cross(x, y);
246
+ }
247
+ // a gusset on the other face of b (j.face): the same L turned over its diagonal, its arm drawn for b along a and its
248
+ // arm drawn for a along b, so it lies on the faces looking along -n (a mirror image of the L is itself turned so)
249
+ let flip = false;
250
+ if (j.face != null) {
251
+ const want = B.places.find((q) => q.slot && (q.name === j.face || (q.face === j.face && q.name.endsWith('.1'))));
252
+ const along = want ? dot(turn(fb, [...want.normal, 0]), n) : 0;
253
+ if (!want || Math.abs(Math.abs(along) - 1) > 1e-6) {
254
+ const can = B.places.filter((q) => q.slot && Math.abs(Math.abs(dot(turn(fb, [...q.normal, 0]), n)) - 1) < 1e-6).map((q) => faceOf(B, q));
255
+ throw new Error(`${what}: face is the face of ${ib.id} a printed gusset lies on, square to ${ia.id}: ${[...new Set(can)].join(' or ') || 'none of its slotted faces is'} (got ${JSON.stringify(j.face)})`);
256
+ }
257
+ if (along < 0) { flip = true; n = n.map((c) => -c); }
258
+ }
259
+ const bq = B.places.find((q) => q.slot && dot(turn(fb, [...q.normal, 0]), n) > 1 - 1e-6), aq = A.places.find((q) => q.slot && dot(turn(fa, [...q.normal, 0]), n) > 1 - 1e-6);
260
+ if (!bq || !aq) throw new Error(`${what}: a printed ${splice ? 'splice plate' : 'gusset'} lies on a slotted face of both beams that looks the same way, and ${!bq ? ib.id : ia.id} has none that does: turn it with roll`);
261
+ const Pb = world(fb, [bq.at[0], bq.at[1], d]), Qa = world(fa, [aq.at[0], aq.at[1], 0]), apart = dot(sub(Qa, Pb), n);
262
+ if (Math.abs(apart) > 0.05) throw new Error(`${what}: a printed ${splice ? 'splice plate' : 'gusset'} lies flat on both beams, and ${ia.id}'s ${aq.face} face stands ${mm(Math.abs(apart))} mm ${apart > 0 ? 'proud of' : 'below'} ${ib.id}'s ${bq.face} face`);
263
+ const spec0 = { kind: splice ? 'splice' : 'gusset', profile: B.profile.name, ...(A.profile.name !== B.profile.name ? { other: A.profile.name } : {}), face: faceOf(B, bq), ...(j.load != null ? { load: j.load } : {}), ...(j.nut ? { nut: j.nut } : {}) };
264
+ let fm = bm ?? fittingModel({ ...spec0, ...(splice ? {} : { shape: 'L' }) }, context, material, what), shape = bm?.shape ?? 'L';
265
+ if (bm) {
266
+ // a bought plate's shape is its set's: a T needs room along b both sides of a, an L on one
267
+ const { room } = shapeAt ?? { room: { a: Infinity, b: Infinity } }, xs = fm.outline.map((q) => q[0]), reach = Math.max(...xs.map(Math.abs));
268
+ if (shape === 'T' && Math.min(room.a, room.b) + (A.profile.width / 2) < reach - 1e-6) throw new Error(`${what}: ${bm.set} is a T, ${mm(2 * reach)} mm along ${ib.id}, and there is ${mm(Math.min(room.a, room.b))} mm on one side of ${ia.id}: an L or a bracket will do`);
269
+ const id = free(`${ia.id}-${ib.id}-${splice ? 'splice' : 'gusset'}`), along = dot(x, ub) > 0;
270
+ const item = { id, bought: bm.set, at: `${ib.id}.${bq.name}@${r3(d)}`, ...(along ? {} : { rotate: [0, 0, 180] }), join: i };
271
+ return { item, fasten: boughtFasten(id, [['b', ib.id, bq.name], ['a', ia.id, aq.name]]), model: bm };
272
+ }
273
+ if (flip && (A.profile.name !== B.profile.name || aq.slot !== bq.slot)) throw new Error(`${what}: a printed gusset turned onto ${ib.id}'s ${j.face} face puts the arm drawn for ${ib.id}'s slot in ${ia.id}'s, and ${ia.id}'s ${aq.face} face (${aq.slot}) and ${ib.id}'s ${bq.face} face (${bq.slot}) differ: leave face out, or join with a bracket`);
274
+ if (!splice) {
275
+ const reach = fm.width / 2 + fm.arm, { room } = shapeAt;
276
+ const fits = Math.min(room.a, room.b) + fm.width / 2 >= reach;
277
+ if (j.shape === 'T' && flip) throw new Error(`${what}: shape T: a printed gusset turned onto ${ib.id}'s ${j.face} face is an L only (a T's bar would lie along ${ia.id}): give shape L, or leave face out`);
278
+ if (j.shape === 'T' && !fits) throw new Error(`${what}: shape T: a printed T gusset's bar runs ${mm(fm.arm)} mm along ${ib.id} on both sides of ${ia.id}, and there is ${mm(Math.min(room.a, room.b))} mm on one side: give shape L, or leave shape out`);
279
+ if (!flip && fits && j.shape !== 'L') { shape = 'T'; fm = fittingModel({ ...spec0, shape }, context, material, what); } // (an L only, turned over: a T's bar would lie along a)
280
+ else if (Math.max(room.a, room.b) + fm.width / 2 < reach - 1e-6) throw new Error(`${what}: a printed gusset's ${mm(fm.arm)} mm arm runs past ${ib.id}'s ends: there is ${mm(Math.max(room.a, room.b))} mm beside ${ia.id}, and it needs ${mm(fm.arm)}. A bracket will do`);
281
+ if (A.length < fm.arm - 1e-6) throw new Error(`${what}: a printed gusset's ${mm(fm.arm)} mm arm is longer than ${ia.id} (${mm(A.length)} mm)`);
282
+ } else if (Math.min(A.length, B.length) < fm.arm - 1e-6) throw new Error(`${what}: a printed splice plate's ${mm(fm.arm)} mm arms are longer than ${A.length < B.length ? ia.id : ib.id}`);
283
+ if (A.profile.name !== B.profile.name && faceOf(A, aq) !== faceOf(B, bq)) throw new Error(`${what}: a printed plate is drawn for one profile's slots, and ${ia.id} (${A.profile.name}) and ${ib.id} (${B.profile.name}) differ: a bracket takes two`);
284
+ const id = free(`${ia.id}-${ib.id}-${splice ? 'splice' : 'gusset'}`), along = dot(x, ub) > 0;
285
+ const spec = { ...spec0, ...(splice ? {} : { shape }) };
286
+ // (at b's face's anchor the item's x runs along b and its z out of the face; turned over, its x runs along a: a
287
+ // quarter turn about z, the way that keeps its arm drawn for b along a away from b)
288
+ const item = { id, fitting: spec, at: `${ib.id}.${bq.name}@${r3(d)}`, ...(flip ? { rotate: [0, 0, along ? -90 : 90] } : along ? {} : { rotate: [0, 0, 180] }), join: i };
289
+ const anchorsOf = (leg) => fm.screws.map((sc, k) => [sc, k]).filter(([sc]) => sc.leg === (flip ? (leg === 'a' ? 'b' : 'a') : leg)).map((_, k) => `${id}.${flip ? (leg === 'a' ? 'b' : 'a') : leg}${k + 1}`);
290
+ const fasten = [{ screw: `${fm.thread} ${fm.head}`, through: [id], into: `${ib.id}.${bq.name}`, at: anchorsOf('b'), note: `${label}: the ${splice ? 'splice plate' : 'gusset'} into ${ib.id}`, join: i, nut: j.nut ?? fm.screws[0].nutKind },
291
+ { screw: `${fm.thread} ${fm.head}`, through: [id], into: `${ia.id}.${aq.name}`, at: anchorsOf('a'), note: `${label}: the ${splice ? 'splice plate' : 'gusset'} into ${ia.id}`, join: i, nut: j.nut ?? fm.screws[0].nutKind }];
292
+ return { item, fasten, model: fm };
293
+ }
294
+
295
+ /**
296
+ * A join's stiffness once its printed fitting is drawn: the fitting's own bending stiffness by finite elements, times
297
+ * the share of it the joint keeps (the joins set's <kind>_printed_share: 1 until the joint rig measures it), its twist
298
+ * still the kind's, in the shape @rulecad/frames' joinStiffness gives.
299
+ * @param {object} k joinStiffness's @param {object} fm the fitting's model
300
+ */
301
+ export function fittedStiffness(k, fm) {
302
+ const share = k.share ?? { value: 1, kind: 'estimate' }, fe = fm.stiffness.bending, bending = fe * share.value;
303
+ return { ...k, bending, end: { rx: k.twist, ry: bending, rz: bending }, measured: { ...(k.measured ?? {}), bending: share.kind === 'measured' ? share.checked ?? null : null }, fitting: { name: fm.name, bending, fe, share: share.value, shareKind: share.kind, linear: fm.stiffness.linear, margin: fm.margin, grams: fm.grams, load: fm.load.force },
304
+ rests: [{ value: fe, unit: 'N mm/rad', kind: 'derived', source: `${fm.name}: by finite elements on the printed fitting as drawn; the slot nuts and lips not in it` }, ...(share.value !== 1 || share.kind === 'measured' ? [{ value: share.value, unit: '', kind: share.kind, source: share.source, ...(share.checked ? { checked: share.checked } : {}) }] : []), ...k.rests.slice(1)] };
305
+ }
306
+
307
+ /**
308
+ * A built assembly's printed fittings' own findings (fitting.size, fitting.margin at the named load, fitting.screw,
309
+ * fitting.stiffness, fitting.mass and the rest), each said of its item. @param {object} s a built assembly @param {Function} f the findings' sink
310
+ */
311
+ export function fittingFindings(s, f) {
312
+ for (const o of s.objects) if (o.fitting && !o.unplaced) for (const x of o.fitting.findings) f(x.level, `${o.id}: ${x.text}`, x.level === 'fail' || x.level === 'warn' ? `${o.id}: ${x.code.split('.')[1]}` : null, { code: x.code, ...(x.values ? { values: x.values } : {}), refs: { item: o.id, fitting: o.fitting.name, ...(x.refs ?? {}) } });
313
+ }