@rulecad/cad 0.7.47 → 0.7.53

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/lib/fasten.mjs ADDED
@@ -0,0 +1,824 @@
1
+ // Screwed joints in assemblies: "fasten": [{ "screw": "M3 cap", "through": ["bracket"], "into": "base",
2
+ // "at": ["bracket.h1", "bracket.h2"] | "holes": "bracket.body", "nut", "washer", "length", "torque", "note" }]
3
+ // One relation for what a "fastens" fit and a holes fit said apart, and it knows the screw. For each screw the parts
4
+ // are read as drawn: a line is followed from under the head, through each part in `through`, into `into`, and what
5
+ // lies round that line is measured from the meshes (profile, below). From those measures: the holes share the axis
6
+ // (fasten.holes), each part passed has a clearance hole for the thread (fasten.clearance), the far end is a pilot, a
7
+ // tapped or printed thread, an insert's hole or a nut's seat, or a nut is given (fasten.end), the length is chosen or
8
+ // checked (fasten.length), enough thread is engaged for what it is in (fasten.engagement), and the head has a flat
9
+ // seat that takes its pressure (fasten.seat). Every figure for the thread comes from @rulecad/fixings, bound to the
10
+ // workspace's rule sets and the project's printer, as v.fixings is. The screws, nuts, washers and inserts are placed
11
+ // as reference parts (hardware, below): faint, checked for overlap, and carried into previews and links.
12
+ import crypto from 'node:crypto';
13
+ import path from 'node:path';
14
+ import { fixingsFor, DEFAULTS, screwGroup } from '@rulecad/fixings';
15
+ import { placeItems, jointValues } from '@rulecad/model-link';
16
+ import { massesOf } from './loads.mjs';
17
+ import { mountTorque } from './motors.mjs';
18
+ import { jscad, fixingsContext } from './design.mjs';
19
+ import { named } from './parts.mjs';
20
+ import { paths } from './workspace.mjs';
21
+ import { peekRuleSet } from './rules.mjs';
22
+ import { orderOf, isBuilt } from './order.mjs';
23
+
24
+ const { mat4, vec3 } = jscad.maths;
25
+ const mm = (n) => String(Number(Number(n).toFixed(3))), r3 = (x) => +x.toFixed(3);
26
+ const dot = (a, b) => a[0] * b[0] + a[1] * b[1] + a[2] * b[2], add = (a, b, k = 1) => [a[0] + b[0] * k, a[1] + b[1] * k, a[2] + b[2] * k];
27
+ 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]];
28
+ const unit = (v) => { const l = Math.hypot(...v); return v.map((c) => c / l); };
29
+ const fan = (poly) => (poly.length === 3 ? [poly] : poly.slice(1, -1).map((_, i) => [poly[0], poly[i + 1], poly[i + 2]]));
30
+ const top = (n) => String(n).split(/[.@]/)[0];
31
+ const EPS = 1e-4; // mm: under this, two lengths read from a mesh are the same
32
+ const an = (t) => (/^(?:[aeiou8]|M\d)/i.test(String(t)) ? 'an' : 'a'); // (an M3 screw, a 6-32 screw)
33
+
34
+ /** The working figures of the fasten checks that no rule set holds, each with what it rests on. */
35
+ export const FASTEN = {
36
+ align: { value: 0.05, unit: 'mm', rule: 'Holes share an axis when their centres lie this near it: the play of floating point and of a hole drawn in flats, far under any clearance', kind: 'default', source: 'A working figure of @rulecad/cad', checked: '2026-11-15' },
37
+ size: { value: 0.06, unit: 'mm', rule: 'A hole as drawn is taken for a thread\'s pilot, tap, insert or clearance hole when its diameter is within this of that figure', kind: 'default', source: 'A working figure of @rulecad/cad: under the least step between two of a thread\'s holes (M2 tap 1.7 mm, pilot 1.9 mm)', checked: '2026-11-15' },
38
+ proud: { value: 1, unit: '× diameter', rule: 'A screw\'s tip may stand up to its own diameter past its nut or the far face of what it is threaded through: more is a longer screw than the joint needs, and it catches', kind: 'default', source: 'A working figure of @rulecad/cad: no standard was found for it', checked: '2026-11-15' },
39
+ countersink: { value: 2, unit: '°', rule: 'A countersink drawn in flat faces is taken for the head\'s angle when it is within this of it', kind: 'default', source: 'A working figure of @rulecad/cad', checked: '2026-11-15' },
40
+ };
41
+
42
+ // ---------- a part measured along an axis ----------
43
+ // Coordinates about an axis: z along it from O, x and y across it
44
+ function frameOf(O, D) {
45
+ const z = unit(D), seed = Math.abs(z[0]) < 0.9 ? [1, 0, 0] : [0, 1, 0], x = unit(cross(seed, z)), y = cross(z, x);
46
+ return { x, y, z, to: (p) => { const q = [p[0] - O[0], p[1] - O[1], p[2] - O[2]]; return [dot(q, x), dot(q, y), dot(q, z)]; } };
47
+ }
48
+ // A slice across the axis at z, as 2D segments. Each end is worked out from its edge's lower corner, so the two faces
49
+ // that share an edge give the very same point, and the segments join end to end exactly.
50
+ function slice(tris, z) {
51
+ const segs = [];
52
+ for (const t of tris) {
53
+ const c = [];
54
+ for (let i = 0; i < 3; i++) {
55
+ let a = t[i], b = t[(i + 1) % 3];
56
+ if ((a[2] - z) * (b[2] - z) >= 0) continue;
57
+ if (a[2] > b[2] || (a[2] === b[2] && (a[0] > b[0] || (a[0] === b[0] && a[1] > b[1])))) [a, b] = [b, a];
58
+ const u = (z - a[2]) / (b[2] - a[2]); c.push([a[0] + u * (b[0] - a[0]), a[1] + u * (b[1] - a[1])]);
59
+ }
60
+ if (c.length === 2) segs.push(c);
61
+ }
62
+ return segs;
63
+ }
64
+ const toSeg = (p, q) => { // how far the axis (the origin) is from a segment
65
+ const ex = q[0] - p[0], ey = q[1] - p[1], l2 = ex * ex + ey * ey, u = l2 ? Math.max(0, Math.min(1, -(p[0] * ex + p[1] * ey) / l2)) : 0;
66
+ return Math.hypot(p[0] + u * ex, p[1] + u * ey);
67
+ };
68
+ // The circle through a set of points (Kasa's fit: least squares on x² + y² + Dx + Ey + F), or null where they lie in a line
69
+ function circle(pts) {
70
+ let sx = 0, sy = 0, sxx = 0, syy = 0, sxy = 0, sxz = 0, syz = 0, sz = 0; const n = pts.length;
71
+ for (const [x, y] of pts) { const z = x * x + y * y; sx += x; sy += y; sxx += x * x; syy += y * y; sxy += x * y; sxz += x * z; syz += y * z; sz += z; }
72
+ // [sxx sxy sx; sxy syy sy; sx sy n] [D E F]' = -[sxz syz sz]'
73
+ const A = [[sxx, sxy, sx, -sxz], [sxy, syy, sy, -syz], [sx, sy, n, -sz]];
74
+ for (let i = 0; i < 3; i++) {
75
+ let p = i; for (let k = i + 1; k < 3; k++) if (Math.abs(A[k][i]) > Math.abs(A[p][i])) p = k;
76
+ if (Math.abs(A[p][i]) < 1e-12) return null;
77
+ [A[i], A[p]] = [A[p], A[i]];
78
+ for (let k = 0; k < 3; k++) if (k !== i) { const f = A[k][i] / A[i][i]; for (let j = i; j < 4; j++) A[k][j] -= f * A[i][j]; }
79
+ }
80
+ const D = A[0][3] / A[0][0], E = A[1][3] / A[1][1], F = A[2][3] / A[2][2], c = [-D / 2, -E / 2], r2 = c[0] * c[0] + c[1] * c[1] - F;
81
+ return r2 > 0 ? { c, r: Math.sqrt(r2) } : null;
82
+ }
83
+ // Points as a round: the circle they lie on once the few that do not (a teardrop's roof) are left out, and whether
84
+ // they go all the way round it
85
+ function roundOf(pts) {
86
+ if (pts.length < 3) return null;
87
+ let fit = circle(pts); if (!fit) return null;
88
+ const off = (f, p) => Math.abs(Math.hypot(p[0] - f.c[0], p[1] - f.c[1]) - f.r), slack = (f) => Math.max(0.02 * f.r, 0.005);
89
+ let on = pts.filter((p) => off(fit, p) <= slack(fit));
90
+ if (on.length < pts.length && on.length >= 6) { const again = circle(on); if (again) { fit = again; on = pts.filter((p) => off(fit, p) <= slack(fit)); } }
91
+ if (on.length < 3 || on.length < pts.length * 0.7) return null;
92
+ const worst = Math.max(...on.map((p) => off(fit, p)));
93
+ if (worst > 0.004 * fit.r + 0.002) return null;
94
+ const angles = on.map((p) => Math.atan2(p[1] - fit.c[1], p[0] - fit.c[0])).sort((a, b) => a - b);
95
+ let gap = angles[0] + 2 * Math.PI - angles.at(-1); for (let i = 1; i < angles.length; i++) gap = Math.max(gap, angles[i] - angles[i - 1]);
96
+ if (gap > (on.length <= 8 ? 2.2 : 1.75)) return null; // (a square's corners are 90° apart; a round with a roof has one gap of 90°)
97
+ return { ...fit, points: on.length, all: on.length === pts.length, corner: on[0] };
98
+ }
99
+ // What lies round the axis in one slice: how near the nearest wall comes (free), and the outline that wall belongs
100
+ // to: whether it closes round the axis (the axis is in a hole), its centre and diameter where it is a round or a
101
+ // regular polygon (a nut's seat), and how far that centre is from the axis
102
+ function measure(segs, R) {
103
+ let best = -1, free = Infinity;
104
+ segs.forEach(([p, q], i) => { const d = toSeg(p, q); if (d < free) { free = d; best = i; } });
105
+ if (best < 0 || free > R) return { free: Infinity, loop: null };
106
+ const key = (p) => `${Math.round(p[0] * 1e6)},${Math.round(p[1] * 1e6)}`, at = new Map();
107
+ segs.forEach((s, i) => { for (const p of s) { const k = key(p); if (!at.has(k)) at.set(k, []); at.get(k).push(i); } });
108
+ const mine = new Set([best]), todo = [best];
109
+ while (todo.length) for (const p of segs[todo.pop()]) for (const j of at.get(key(p))) if (!mine.has(j)) { mine.add(j); todo.push(j); }
110
+ const pts = new Map(); let closed = true, inside = false;
111
+ for (const i of mine) {
112
+ const [p, q] = segs[i];
113
+ for (const e of [p, q]) { const k = key(e); pts.set(k, e); if (at.get(k).filter((j) => mine.has(j)).length !== 2) closed = false; }
114
+ if ((p[1] > 0) !== (q[1] > 0) && p[0] + ((0 - p[1]) / (q[1] - p[1])) * (q[0] - p[0]) > 0) inside = !inside; // (a ray from the axis along +x)
115
+ }
116
+ // corners only: a point where the outline runs straight on is a seam between two faces, not a corner
117
+ const list = !closed ? [] : [...pts].filter(([k, e]) => {
118
+ const [p, q] = at.get(k).filter((j) => mine.has(j)).map((j) => (key(segs[j][0]) === k ? segs[j][1] : segs[j][0]));
119
+ const ux = e[0] - p[0], uy = e[1] - p[1], vx = q[0] - e[0], vy = q[1] - e[1];
120
+ return Math.abs(ux * vy - uy * vx) > 2e-3 * Math.hypot(ux, uy) * Math.hypot(vx, vy); // (0.1°: a seam left by a boolean lies within its snap of the line; a round's corners turn by degrees)
121
+ }).map(([, e]) => e), round = closed ? roundOf(list) : null;
122
+ let shape = 'other', d = null, flats = null, offset = null, c = null, corner = null;
123
+ if (round) {
124
+ c = round.c; d = 2 * round.r; offset = Math.hypot(c[0], c[1]);
125
+ let least = Infinity; for (const i of mine) { const [p, q] = segs[i]; least = Math.min(least, toSeg([p[0] - c[0], p[1] - c[1]], [q[0] - c[0], q[1] - c[1]])); }
126
+ flats = 2 * least;
127
+ const sides = round.all ? Math.round(Math.PI / Math.acos(Math.min(1, least / round.r))) : 0; // (a regular polygon's flats lie at r cos(π / n))
128
+ shape = round.all && sides === 6 ? 'hex' : round.all && sides === 4 ? 'square' : 'round';
129
+ corner = round.corner;
130
+ }
131
+ const [bp, bq] = segs[best], bl = Math.hypot(bq[0] - bp[0], bq[1] - bp[1]), along = bl > 0 ? [(bq[0] - bp[0]) / bl, (bq[1] - bp[1]) / bl] : null; // (the nearest wall's run: a nut's corners point along a slot's walls)
132
+ return { free, loop: { closed, inside: closed && inside, shape, d, flats, offset, c, corner, along } };
133
+ }
134
+ const same = (a, b) => {
135
+ if (!a.loop || !b.loop) return !a.loop && !b.loop;
136
+ const near = (x, y) => (x == null || y == null ? x == y : Math.abs(x - y) < 1e-3); // (a fit through corners snapped to a boolean's grid wanders by less)
137
+ return near(a.free, b.free) && a.loop.shape === b.loop.shape && a.loop.inside === b.loop.inside && near(a.loop.d, b.loop.d) && near(a.loop.offset, b.loop.offset);
138
+ };
139
+
140
+ /**
141
+ * A part measured along an axis: the stretches along it, each with what lies round the axis there.
142
+ * @param {number[][][]} tris the part's faces (triangles, or polygons that are fanned) @param {number[]} O a point on the axis @param {number[]} D its direction
143
+ * @param {number} R how far from the axis to look, mm
144
+ * @returns {{ segs: { z0: number, z1: number, solid: boolean, hole: boolean, a: object, b: object }[], near: number[][][], x: number[], y: number[] }}
145
+ * segs: in order along the axis (z from O along D). solid: the axis is in material; hole: it is in a space the part
146
+ * closes round; a, b: the measure just inside each end ({ free, loop }, see measure): equal on a straight bore, apart on a cone.
147
+ * near: the faces within R, in the axis's own coordinates, for further slices; x, y: the directions across the axis
148
+ */
149
+ export function profile(tris, O, D, R) {
150
+ const f = frameOf(O, D), near = [], hits = [], cuts = new Set();
151
+ for (const poly of tris) for (const t of fan(poly)) {
152
+ const q = t.map(f.to);
153
+ if (Math.min(q[0][0], q[1][0], q[2][0]) > R || Math.max(q[0][0], q[1][0], q[2][0]) < -R || Math.min(q[0][1], q[1][1], q[2][1]) > R || Math.max(q[0][1], q[1][1], q[2][1]) < -R) continue;
154
+ near.push(q);
155
+ for (const p of q) cuts.add(Math.round(p[2] * 1e6) / 1e6);
156
+ // where the axis itself goes through this face, and which way the face looks
157
+ const [a, b, c] = q, det = (b[0] - a[0]) * (c[1] - a[1]) - (c[0] - a[0]) * (b[1] - a[1]);
158
+ if (Math.abs(det) < 1e-12) continue;
159
+ const l1 = (b[0] * c[1] - c[0] * b[1]) / det, l2 = (c[0] * a[1] - a[0] * c[1]) / det, l3 = 1 - l1 - l2;
160
+ if (l1 >= -1e-9 && l2 >= -1e-9 && l3 >= -1e-9) hits.push([l1 * a[2] + l2 * b[2] + l3 * c[2], Math.sign(det)]);
161
+ }
162
+ // The axis is in material between a face it enters by (looking back along it) and one it leaves by. Faces that
163
+ // meet on the axis (a hole's floor drawn as a fan, a cone's point) are met together, and count once.
164
+ hits.sort((p, q) => p[0] - q[0]);
165
+ const walls = [];
166
+ for (let i = 0; i < hits.length;) { let j = i, net = 0; while (j < hits.length && hits[j][0] - hits[i][0] < 1e-6) net += hits[j++][1]; if (net) walls.push([hits[i][0], net > 0 ? -1 : 1]); i = j; }
167
+ const solidAt = (z) => { let depth = 0; for (const [w, s] of walls) { if (w > z) break; depth += s; } return depth > 0; };
168
+ let zs = [...cuts].sort((a, b) => a - b);
169
+ if (zs.length > 600) { const lo = zs[0], hi = zs.at(-1); zs = Array.from({ length: 401 }, (_, i) => lo + ((hi - lo) * i) / 400); } // (a drawn thread: sampled evenly)
170
+ const segs = [];
171
+ for (let i = 0; i + 1 < zs.length; i++) {
172
+ const z0 = zs[i], z1 = zs[i + 1], len = z1 - z0; if (len < 1e-5) continue;
173
+ const e = Math.min(1e-3, len / 4), a = measure(slice(near, z0 + e), R), b = measure(slice(near, z1 - e), R), solid = solidAt((z0 + z1) / 2);
174
+ const hole = !solid && !!a.loop?.inside && !!b.loop?.inside, last = segs.at(-1);
175
+ if (last && last.solid === solid && Math.abs(last.z1 - z0) < 1e-4 && same(last.a, last.b) && same(a, b) && same(last.b, a)) last.z1 = z1;
176
+ else segs.push({ z0, z1, solid, hole, a, b });
177
+ }
178
+ return { segs, near, x: f.x, y: f.y };
179
+ }
180
+
181
+ // A placed part measured along a world axis, in the part's own frame, so the answer holds wherever the part is put
182
+ // (every pose, every copy): kept by what the part is built from and where the axis lies in it
183
+ const kept = new Map();
184
+ function partProfile(p, O, D, R) {
185
+ if (!p.m || !p.localTriangles) return profile(p.triangles, O, D, R);
186
+ const inv = mat4.invert(mat4.create(), p.m), o = vec3.transform([0, 0, 0], O, inv), e = vec3.transform([0, 0, 0], add(O, D), inv), d = [e[0] - o[0], e[1] - o[1], e[2] - o[2]];
187
+ const key = `${p.hash ?? p.shape}|${[...o, ...d, R].map((x) => Math.round(x * 1e6) / 1e6).join(',')}`;
188
+ if (!kept.has(key)) { if (kept.size > 4000) kept.clear(); kept.set(key, profile(p.localTriangles, o, d, R)); }
189
+ return kept.get(key);
190
+ }
191
+ // The stretches of several parts as one run along the axis, turned to run the other way where asked
192
+ function along(parts, O, D, R, flip) {
193
+ const segs = parts.flatMap((p) => { const pr = partProfile(p, O, D, R); return pr.segs.map((s) => ({ ...s, part: p, of: pr })); });
194
+ const out = flip ? segs.map((s) => ({ ...s, z0: -s.z1, z1: -s.z0, a: s.b, b: s.a })) : segs;
195
+ return out.sort((a, b) => a.z0 - b.z0);
196
+ }
197
+ const reach = (segs) => { const on = segs.filter((s) => s.solid || s.hole); return on.length ? [Math.min(...on.map((s) => s.z0)), Math.max(...on.map((s) => s.z1))] : null; };
198
+ // Whether there is material at each of a ring of points round the axis in one slice: counted from the axis outwards,
199
+ // each wall crossed changes it. `from`: whether the axis itself is in material there
200
+ function ring(near, z, radius, from, n = 24) {
201
+ const segs = slice(near, z); let solid = 0;
202
+ for (let k = 0; k < n; k++) {
203
+ const a = ((k + 0.37) * 2 * Math.PI) / n, dx = Math.cos(a), dy = Math.sin(a); let inside = from;
204
+ for (const [p, q] of segs) {
205
+ const ex = q[0] - p[0], ey = q[1] - p[1], den = dx * ey - dy * ex; if (Math.abs(den) < 1e-12) continue;
206
+ const t = (p[0] * ey - p[1] * ex) / den, u = (p[0] * dy - p[1] * dx) / den;
207
+ if (t > 1e-9 && t < radius && u >= 0 && u < 1) inside = !inside;
208
+ }
209
+ if (inside) solid++;
210
+ }
211
+ return solid / n;
212
+ }
213
+
214
+ // ---------- the holes of a part, where a relation names a part and not anchors ----------
215
+ // Round holes found in a part as drawn, along each of its own three axes: walls that stand along the axis, joined
216
+ // where they share a corner, kept where their corners lie on one circle and the walls face its centre.
217
+ const found = new Map();
218
+ function holesIn(p) {
219
+ const key = p.hash ?? p.shape;
220
+ if (found.has(key)) return found.get(key);
221
+ const out = [], tris = (p.localTriangles ?? p.triangles).flatMap(fan);
222
+ for (let k = 0; k < 3; k++) {
223
+ const [u, w] = k === 0 ? [1, 2] : k === 1 ? [2, 0] : [0, 1]; // (u × w = k, so a normal's sense carries over)
224
+ const id = new Map(), parent = [], pts = [], span = [], face = [];
225
+ const find = (i) => { while (parent[i] !== i) { parent[i] = parent[parent[i]]; i = parent[i]; } return i; };
226
+ const node = (v) => { const q = `${Math.round(v[u] * 1e4)},${Math.round(v[w] * 1e4)}`; if (!id.has(q)) { id.set(q, pts.length); parent.push(pts.length); pts.push([v[u], v[w]]); span.push([v[k], v[k]]); face.push(null); } const i = id.get(q); span[i][0] = Math.min(span[i][0], v[k]); span[i][1] = Math.max(span[i][1], v[k]); return i; };
227
+ for (const t of tris) {
228
+ const n = cross([t[1][0] - t[0][0], t[1][1] - t[0][1], t[1][2] - t[0][2]], [t[2][0] - t[0][0], t[2][1] - t[0][1], t[2][2] - t[0][2]]), l = Math.hypot(...n);
229
+ if (!(l > 1e-12) || Math.abs(n[k]) > 1e-4 * l) continue; // (a wall: it stands along the axis)
230
+ const [a, b, c] = t.map(node); parent[find(a)] = find(b); parent[find(c)] = find(b);
231
+ face[a] ||= [n[u] / l, n[w] / l];
232
+ }
233
+ const groups = new Map();
234
+ pts.forEach((_, i) => { const r = find(i); if (!groups.has(r)) groups.set(r, []); groups.get(r).push(i); });
235
+ for (const g of groups.values()) {
236
+ if (g.length < 8) continue;
237
+ const round = roundOf(g.map((i) => pts[i])); if (!round || round.r < 0.4 || round.r > 20) continue;
238
+ const i = g.find((j) => face[j]), inward = face[i][0] * (round.c[0] - pts[i][0]) + face[i][1] * (round.c[1] - pts[i][1]) > 0;
239
+ if (!inward) continue; // (a boss's outside, not a hole)
240
+ const lo = Math.min(...g.map((j) => span[j][0])), hi = Math.max(...g.map((j) => span[j][1]));
241
+ const twin = out.find((h) => h.k === k && Math.hypot(h.c[0] - round.c[0], h.c[1] - round.c[1]) < 0.02);
242
+ if (twin) { twin.d = Math.min(twin.d, 2 * round.r); twin.lo = Math.min(twin.lo, lo); twin.hi = Math.max(twin.hi, hi); } else out.push({ k, c: round.c, d: 2 * round.r, lo, hi });
243
+ }
244
+ }
245
+ const holes = out.map((h) => { const [u, w] = h.k === 0 ? [1, 2] : h.k === 1 ? [2, 0] : [0, 1], at = [0, 0, 0], axis = [0, 0, 0]; at[u] = h.c[0]; at[w] = h.c[1]; at[h.k] = (h.lo + h.hi) / 2; axis[h.k] = 1; return { at, axis, d: h.d }; })
246
+ .sort((a, b) => a.at[0] - b.at[0] || a.at[1] - b.at[1] || a.at[2] - b.at[2]);
247
+ found.set(key, holes);
248
+ return holes;
249
+ }
250
+
251
+ // ---------- the key's way to the head ----------
252
+ // The tool a head is turned with, from the hexkey rule set: a hex key in a socket head (its size from the thread's set,
253
+ // as the screw model gives it), a socket or a spanner on a hexagon head. radius: the room it needs round the screw's
254
+ // axis; reach: how far over the head that room is needed (a key's long arm, put in along the axis)
255
+ function toolFor(model, form, fx) {
256
+ let set; try { set = peekRuleSet('hexkey').rules; } catch { return null; }
257
+ const sizes = set.sizes.value, at = (s) => { const i = sizes.findIndex((k) => k >= s - 1e-6); return i < 0 ? sizes.length - 1 : i; };
258
+ if (form === 'hex') {
259
+ const flats = fx.figures.hex_head_flats?.value ?? model.headDiameter, i = at(flats);
260
+ return { name: `${mm(flats)} mm socket or spanner`, size: flats, radius: flats / Math.cos(Math.PI / 6) / 2 + set.socket_wall.value, reach: set.long_arm.value[i], tilt: 0, rests: ['hexkey.socket_wall', 'hexkey.long_arm'] };
261
+ }
262
+ const s = model.key; if (s == null) return null;
263
+ const i = at(s), exact = Math.abs(sizes[i] - s) < 1e-6;
264
+ return { name: `${mm(s)} mm hex key`, size: s, radius: (exact ? set.corners.value[i] : s / Math.cos(Math.PI / 6)) / 2, reach: set.long_arm.value[i], tilt: set.ball_end_tilt?.value ?? 0, rests: ['hexkey.corners', 'hexkey.long_arm', 'hexkey.ball_end_tilt'] };
265
+ }
266
+ // What stands in a tool's way: every part with material within its radius of the line from P along U, up to its
267
+ // reach, each with how far over the head it is first met
268
+ function inTheWay(parts, P, U, radius, reach) {
269
+ const out = new Map();
270
+ for (const p of parts) {
271
+ for (const seg of partProfile(p, P, U, radius).segs) {
272
+ if (seg.z1 <= 1e-3 || seg.z0 >= reach) continue;
273
+ if (seg.solid || Math.min(seg.a.free, seg.b.free) < radius - 1e-3) { const z = Math.max(0, seg.z0); if (!out.has(p.item) || out.get(p.item) > z) out.set(p.item, z); break; }
274
+ }
275
+ }
276
+ return [...out].map(([item, z]) => ({ item, z: r3(z) })).sort((a, b) => a.z - b.z);
277
+ }
278
+ // A screw's access: its tool, what stands on the straight line over its head, and the tilt a ball end reaches it at where that is blocked
279
+ function accessOf(parts, tool, P, U) {
280
+ const lift = 0.05, start = add(P, U, lift), blockers = inTheWay(parts, start, U, tool.radius, tool.reach).map((b) => ({ ...b, z: r3(b.z + lift) })); // (from just over the head, so the head's own seat is not met)
281
+ let tilt = null;
282
+ if (blockers.length && tool.tilt > 0) {
283
+ const f = frameOf(start, U), t = (tool.tilt * Math.PI) / 180;
284
+ for (let k = 0; k < 8 && tilt == null; k++) { const a = (k * Math.PI) / 4, W = unit([0, 1, 2].map((c) => U[c] * Math.cos(t) + (f.x[c] * Math.cos(a) + f.y[c] * Math.sin(a)) * Math.sin(t))); if (!inTheWay(parts, start, W, tool.radius, tool.reach).length) tilt = tool.tilt; }
285
+ }
286
+ return { tool: { name: tool.name, size: tool.size, radius: r3(tool.radius), reach: tool.reach, rests: tool.rests }, top: P.map(r3), axis: U.map(r3), blockers, tilt };
287
+ }
288
+
289
+ // ---------- the project's fixings ----------
290
+ const bound = new Map();
291
+ function fixingsOf(project) {
292
+ const { context, hash } = fixingsContext({ project, dir: paths.project(project) });
293
+ if (bound.get(project)?.hash !== hash) bound.set(project, { hash, F: fixingsFor({ ...(Object.keys(context.sets).length ? { sets: context.sets } : {}), ...(context.printer ? { printer: context.printer } : {}) }), material: context.material, printer: context.printer?.name ?? 'x1c' });
294
+ return bound.get(project);
295
+ }
296
+ // A piece of hardware as a part's shape, made once for each name: the light solid of @rulecad/fixings, its faces, a hash
297
+ const shapes = new Map();
298
+ function shapeOf(F, kind, m, o) {
299
+ const key = `${kind}:${m.name}:${JSON.stringify(o)}`;
300
+ if (!shapes.has(key)) {
301
+ const g = F[kind](m, o), [p] = named({ body: g }, { body: { label: g.model.name } });
302
+ shapes.set(key, { geometry: p.geometry, polygons: p.polygons, triangles: p.triangles, model: g.model, hash: `fixing:${crypto.createHash('sha256').update(JSON.stringify([key, p.polygons])).digest('hex').slice(0, 16)}` });
303
+ }
304
+ return shapes.get(key);
305
+ }
306
+ // Where a piece of hardware stands in the world: its own z along Z from P, its x towards X where one is given
307
+ function standing(P, Z, X) {
308
+ const z = unit(Z); let x = X && unit(add(X, z, -dot(X, z)));
309
+ if (!x || !x.every(Number.isFinite)) { const seed = Math.abs(z[0]) < 0.9 ? [1, 0, 0] : [0, 1, 0]; x = unit(cross(seed, z)); }
310
+ const y = cross(z, x);
311
+ return [x[0], x[1], x[2], 0, y[0], y[1], y[2], 0, z[0], z[1], z[2], 0, P[0], P[1], P[2], 1];
312
+ }
313
+
314
+ const NUTS = ['hex', 'square', 'lock'];
315
+
316
+ /**
317
+ * An assembly's fasten relations, worked out from its parts as they stand: each screw followed along its axis, what
318
+ * was measured, what was found, and the hardware to place.
319
+ * @param {{ a: object, project: string, parts: object[], objects: object[], motion: object, num: Function }} s the assembly as built (parts placed; hardware parts among them are passed over)
320
+ * @param {number[][]} worlds each motion item's place (3 × 4 by rows), as placeItems gives it for the pose the parts stand in
321
+ * @param {{ access?: boolean }} [opts] access: follow each screw's key from its head (the build pose)
322
+ * @returns {{ relations: object[], hardware: object[], fits: object[] }} relations: one for each entry of "fasten", with its screws
323
+ * (each with `access` where asked: its tool, and what stands over its head); hardware: every screw, nut, washer and insert to place;
324
+ * fits: the "fastens" fits the relations stand for, the screws' lines with their key and tightening
325
+ */
326
+ export function fastenOf(s, worlds, opts = {}) {
327
+ const list = s.a.fasten || [];
328
+ if (!list.length) return { relations: [], hardware: [], fits: [] };
329
+ const { F, material, printer } = fixingsOf(s.project), parts = s.parts.filter((p) => !p.hardware), ids = s.motion.items.map((x) => x[0]);
330
+ const isNamed = (p, n) => p.name === n || p.leaf === n || p.item === n || p.name.startsWith(`${n}.`);
331
+ const pick = (n) => parts.filter((p) => isNamed(p, n) && p.role !== 'support');
332
+ const world = (k, pt) => [0, 1, 2].map((r) => worlds[k][4 * r] * pt[0] + worlds[k][4 * r + 1] * pt[1] + worlds[k][4 * r + 2] * pt[2] + worlds[k][4 * r + 3]);
333
+ const turned = (k, v) => [0, 1, 2].map((r) => worlds[k][4 * r] * v[0] + worlds[k][4 * r + 1] * v[1] + worlds[k][4 * r + 2] * v[2]);
334
+ const rowsToMat4 = (r) => [r[0], r[4], r[8], 0, r[1], r[5], r[9], 0, r[2], r[6], r[10], 0, r[3], r[7], r[11], 1];
335
+ const relations = [], hardware = [], fits = [];
336
+
337
+ list.forEach((x, k) => {
338
+ const what = `fasten ${k + 1}`, through = x.through || [];
339
+ // Refused by name before anything is measured: the screw, the parts, where the screws go
340
+ let fx;
341
+ try { fx = F.fixingModel(String(x.screw), {}, { anyLength: true }); } catch (e) { throw new Error(`${what}: ${e.message}`); }
342
+ if (!['screw', 'thread'].includes(fx.item)) throw new Error(`${what}: "screw" names a screw by its thread and head ("M3 cap", "M4x12 button"): ${x.screw} is ${/^[aeiou]/.test(fx.item) ? 'an' : 'a'} ${fx.item}`);
343
+ const form = fx.head?.form ?? (fx.made.heads.includes('cap') ? 'cap' : fx.made.heads[0]);
344
+ if (!Array.isArray(through)) throw new Error(`${what}: "through" lists the parts the screw passes, in order from under its head (["bracket"])`);
345
+ if (typeof x.into !== 'string' || !x.into) throw new Error(`${what}: "into" names the part the screw ends in`);
346
+ const members = [...through, x.into].map((n) => { const ps = pick(n); if (!ps.length) throw new Error(`${what}: no part "${n}" (items: ${ids.join(', ')})`); return { name: n, parts: ps }; });
347
+ if ((x.at == null) === (x.holes == null)) throw new Error(`${what}: give "at" (an anchor for each screw: ["${top(through[0] ?? x.into)}.h1"]) or "holes" (a part whose holes are the screws' places), one of the two`);
348
+ if (x.nut != null && x.nut !== false && x.nut !== true && !NUTS.includes(x.nut)) throw new Error(`${what}: "nut" is true (a hexagon nut) or ${NUTS.join(', ')} (got ${JSON.stringify(x.nut)}): a slot nut comes with a frame's beam`);
349
+ const wantNut = x.nut === true ? 'hex' : x.nut || null;
350
+ if (form === 'set' && (through.length || wantNut || x.washer)) throw new Error(`${what}: a set screw goes into one part whole: give it "into" alone (no through, nut or washer)`);
351
+ if (form !== 'set' && !through.length) throw new Error(`${what}: "through" names what the screw holds down, the part under its head: only a set screw has none`);
352
+ const given = fx.length ?? (x.length != null ? s.num(x.length) : null);
353
+ if (fx.length != null && x.length != null && Math.abs(s.num(x.length) - fx.length) > 1e-9) throw new Error(`${what}: ${x.screw} gives the length, and "length" says ${s.num(x.length)}: give it once`);
354
+ if (given != null && !(given > 0)) throw new Error(`${what}: "length" is the screw's length, above 0 mm (got ${given})`);
355
+ const torque = x.torque != null ? s.num(x.torque) : null;
356
+ if (torque != null && !(torque > 0)) throw new Error(`${what}: "torque" is the tightening torque, above 0 N m (got ${torque})`);
357
+ let head = null, washer = null;
358
+ if (form !== 'set') { const m = F.screwModel(fx, { length: fx.length ?? 10, head: form, anyLength: true }); head = { form, across: m.headDiameter, height: m.headHeight, angle: fx.head?.angle ?? fx.figures.countersunk_angle?.value ?? 90 }; }
359
+ if (x.washer) { try { washer = F.washerModel(fx.thread); } catch (e) { throw new Error(`${what}: ${e.message}`); } }
360
+ const nutModel = (kind) => F.nutModel(fx.thread, { kind });
361
+ if (wantNut) { try { nutModel(wantNut); } catch (e) { throw new Error(`${what}: ${e.message}`); } }
362
+ const R = Math.max(head?.across ?? fx.major, washer?.diameter ?? 0, fx.major * 2.2) / 2 + 1.5;
363
+
364
+ // A motor item ends the screws in its own tapped mounting holes, which its light solid does not draw
365
+ const drive = (s.motion.drives || []).find((d) => d.id === top(x.into)) ?? null, Wm = drive ? worlds[drive.item] : null;
366
+ const onMount = (O, D) => {
367
+ if (!drive?.model.mount?.thread || !drive.model.mount.holes?.length) return null;
368
+ const q = [O[0] - Wm[3], O[1] - Wm[7], O[2] - Wm[11]], l = [Wm[0] * q[0] + Wm[4] * q[1] + Wm[8] * q[2], Wm[1] * q[0] + Wm[5] * q[1] + Wm[9] * q[2]];
369
+ if (Math.abs(Math.abs(dot(unit(D), [Wm[2], Wm[6], Wm[10]])) - 1) > 1e-6) return null;
370
+ const h = drive.model.mount.holes.find(([hx, hy]) => Math.hypot(l[0] - hx, l[1] - hy) <= FASTEN.align.value);
371
+ return h ? { at: h, thread: drive.model.mount.thread, depth: drive.model.mount.depth, model: drive.model.name } : null;
372
+ };
373
+
374
+ // The screws' axes: an anchor each, or the holes of a part
375
+ const axes = [];
376
+ if (x.at) s.motion.fasten[k].forEach((an, i) => {
377
+ const fr = an.frame, P = [fr[3], fr[7], fr[11]], Z = [fr[2], fr[6], fr[10]];
378
+ axes.push({ name: an.name ?? `screw ${i + 1}`, O: an.item < 0 ? P : world(an.item, P), D: unit(an.item < 0 ? Z : turned(an.item, Z)) });
379
+ });
380
+ else {
381
+ const ps = pick(x.holes); if (!ps.length) throw new Error(`${what}: "holes" names a part: no part "${x.holes}" (items: ${ids.join(', ')})`);
382
+ const others = members.filter((m) => !m.parts.some((p) => ps.includes(p)));
383
+ for (const p of ps) {
384
+ const board = s.objects.find((o) => o.id === p.leaf)?.built?.circuit;
385
+ const local = board ? board.filter((e) => e.type === 'pcb_hole' && !e.pcb_component_id).map((h) => ({ at: [h.x, h.y, 0], axis: [0, 0, 1], d: h.hole_diameter }))
386
+ : holesIn(p).filter((h) => h.d >= fx.major * 0.6 && h.d <= Math.max((head?.across ?? fx.major * 2) + 1, fx.major * 1.6));
387
+ for (const h of local) {
388
+ const O = vec3.transform([0, 0, 0], h.at, p.m), e = vec3.transform([0, 0, 0], add(h.at, h.axis), p.m), D = unit([e[0] - O[0], e[1] - O[1], e[2] - O[2]]);
389
+ // a hole of this part is a screw's place where its axis goes on to another part of the joint
390
+ if (others.length && !others.some((m) => reach(along(m.parts, O, D, R, false)))) continue;
391
+ if (drive && !onMount(O, D)) continue; // (into a motor: only its mounting holes, not the hole its boss sits in)
392
+ axes.push({ name: `hole at ${mm(h.at[0])}, ${mm(h.at[1])}${Math.abs(h.at[2]) > 1e-6 && !board ? `, ${mm(h.at[2])}` : ''}`, O, D });
393
+ }
394
+ }
395
+ }
396
+ const label = x.note || (through.length ? `${through.join(' and ')} to ${x.into}` : `in ${x.into}`);
397
+ const rel = { index: k, what, label, screw: String(x.screw), thread: fx.thread, form, through, into: x.into, screws: [], general: [] };
398
+ if (!axes.length) rel.general.push({ code: 'fasten.holes', level: 'fail', text: `no holes found in ${x.holes} that lead on to ${[...through, x.into].filter((n) => n !== x.holes).join(' or ') || 'another part'}: draw them with v.fixings, or place each screw with "at"` });
399
+
400
+ const matOf = (m) => { // what a member is made of: the project's plastic for a printed part, its own word for a reference
401
+ const p = m.parts[0], own = s.objects.find((o) => o.id === p.leaf)?.built?.object?.material;
402
+ const name = own ? String(own).trim().toLowerCase().split(/\s/)[0] : p.role === 'reference' ? null : material;
403
+ if (!name) return { name: null, cls: null, known: false };
404
+ try { const e = F.engagement(fx, { depth: 1, material: name }); return { name, cls: e.class, known: true }; } catch { return { name, cls: p.role === 'reference' ? null : 'plastic', known: false }; }
405
+ };
406
+ const drawn = (kind) => { try { return F.holeModel(fx, { kind }); } catch { return null; } };
407
+ const clr = { close: drawn('close'), normal: drawn('clearance'), loose: drawn('loose') }, sizeTol = FASTEN.size.value;
408
+
409
+ axes.forEach((ax, i) => {
410
+ const sc = { index: i, name: ax.name, found: {}, hardware: [] }, say = (code, level, text, values) => { (sc.found[code] ||= []).push({ level, text, ...(values ? { values } : {}) }); };
411
+ rel.screws.push(sc);
412
+ // Which way the screw goes: from the part under its head towards the part it ends in (a set screw: against its anchor)
413
+ const raw = members.map((m) => reach(along(m.parts, ax.O, ax.D, R, false)));
414
+ const missing = members.filter((_, j) => !raw[j]);
415
+ sc.at = ax.O; sc.between = [ax.O, ax.O];
416
+ if (missing.length) return say('fasten.holes', 'fail', `${missing.map((m) => m.name).join(' and ')} ${missing.length > 1 ? 'are' : 'is'} nowhere on the screw's axis (nothing of ${missing.length > 1 ? 'them' : 'it'} within ${mm(R)} mm of it): the parts are not where the joint needs them`);
417
+ const mid = (r) => (r[0] + r[1]) / 2, flip = members.length > 1 ? mid(raw.at(-1)) < mid(raw[0]) : true;
418
+ const D = flip ? ax.D.map((c) => -c) : ax.D, O = ax.O, at = (z) => add(O, D, z);
419
+ const runs = members.map((m) => ({ ...m, segs: along(m.parts, O, ax.D, R, flip) }));
420
+ for (const r of runs) r.reach = reach(r.segs);
421
+ const order = runs.slice(1).find((r, j) => mid(r.reach) < mid(runs[j].reach));
422
+ if (order) return say('fasten.holes', 'fail', `${order.name} lies before ${runs[runs.indexOf(order) - 1].name} along the screw: "through" lists the parts in order from under the head, and "into" comes last`);
423
+ const tol = 1e-3, B = runs.at(-1), A = through.length ? runs[0] : null, headR = head ? head.across / 2 : 0;
424
+ const offAxis = (seg) => (seg.a.loop?.offset != null && seg.a.loop.shape === 'round' ? seg.a.loop.offset : null);
425
+ const solidSays = (r, seg) => { const o = offAxis(seg); return `the axis meets solid ${r.name} ${mm(seg.z1 - seg.z0)} mm deep${o != null ? `: its nearest hole is ${mm(o)} mm from the axis` : ': no hole lies near'}`; };
426
+ let bad = false, worst = 0;
427
+ const size = (seg) => (seg.a.loop.shape === 'round' && seg.a.loop.d != null ? Math.min(seg.a.loop.d, seg.b.loop?.d ?? Infinity) : 2 * Math.min(seg.a.free, seg.b.free));
428
+
429
+ // The seat: where the hole first comes in under the head's width
430
+ let zSeat = A ? null : B.reach[0], cone = null, flat = true, proud = 0, bore0 = 0;
431
+ if (A) {
432
+ for (const [n, seg] of A.segs.entries()) {
433
+ if (seg.solid) { say('fasten.holes', 'fail', solidSays(A, seg)); bad = true; break; }
434
+ if (!seg.hole) continue;
435
+ const fa = seg.a.free, fb = seg.b.free;
436
+ if (fa >= headR - tol && fb >= headR - tol) continue; // (a counterbore: the head goes down it)
437
+ bore0 = n;
438
+ if (fa >= headR - tol) { zSeat = seg.z0 + ((fa - headR) / (fa - fb)) * (seg.z1 - seg.z0); cone = seg; flat = false; } // (a cone that narrows past the head's width: a countersink)
439
+ else if (form === 'countersunk') { // a countersunk head on a hole narrower than itself: it rides high
440
+ const slope = fa > fb + tol ? (fa - fb) / (seg.z1 - seg.z0) : Math.tan(((head.angle / 2) * Math.PI) / 180);
441
+ if (fa > fb + tol) cone = seg;
442
+ proud = (headR - fa) / slope; zSeat = seg.z0 - proud; flat = false;
443
+ } else zSeat = seg.z0;
444
+ break;
445
+ }
446
+ if (zSeat == null && !bad) { say('fasten.holes', 'fail', `${A.name} has no hole on the screw's axis for its head to sit on`); bad = true; }
447
+ }
448
+ if (bad) return;
449
+ const wt = washer ? washer.thickness : 0, zHead = zSeat - wt;
450
+ sc.at = at(zHead); sc.between = [at(zHead), at(zHead + (given ?? fx.major * 3))];
451
+
452
+ // Each part passed through: a hole all the way, wide enough for the thread, on the axis
453
+ const cl = [];
454
+ for (const [j, r] of runs.slice(0, -1).entries()) {
455
+ const segs = r.segs.filter((seg, n) => (j ? true : n >= bore0) && seg !== cone && seg.z1 > zSeat + tol), stop = segs.find((seg) => seg.solid);
456
+ if (stop) { say('fasten.holes', 'fail', solidSays(r, stop)); bad = true; continue; }
457
+ const holes = segs.filter((seg) => seg.hole);
458
+ if (!holes.length) { say('fasten.holes', 'fail', `${r.name} has no hole on the screw's axis`); bad = true; continue; }
459
+ const narrow = holes.reduce((m, seg) => (size(seg) < size(m) ? seg : m), holes[0]), d = size(narrow);
460
+ for (const seg of holes) { const o = offAxis(seg); if (o != null && o > worst) worst = o; if (o != null && o > FASTEN.align.value) { say('fasten.holes', 'fail', `${r.name}'s hole is ${mm(o)} mm off the screw's axis: the holes do not line up`, { distance: r3(o) }); bad = true; break; } }
461
+ cl.push({ name: r.name, d, free: 2 * Math.min(narrow.a.free, narrow.b.free) });
462
+ }
463
+ // The far end: the hole in the last part, from its mouth
464
+ const first = B.segs.find((seg) => seg.solid || seg.hole);
465
+ let end = null;
466
+ const mh = first.solid ? onMount(O, D) : null;
467
+ if (mh) {
468
+ if (mh.thread !== fx.thread) { say('fasten.end', 'fail', `${x.into}'s mounting holes are ${mh.thread} threads, and the screw is ${fx.thread}: use ${an(mh.thread)} ${mh.thread} screw`); bad = true; }
469
+ else end = { mouth: first.z0, d: fx.major, free: fx.major, depth: mh.depth, blind: true, far: first.z0 + mh.depth, pocket: null, main: first, motor: mh };
470
+ } else if (first.solid) { const o = offAxis(first); say('fasten.holes', 'fail', `${solidSays(B, first)}${o != null ? ': the holes do not line up' : ''}`, o != null ? { distance: r3(o) } : undefined); bad = true; }
471
+ else {
472
+ const n0 = B.segs.indexOf(first), mouth = first.z0; let n = n0;
473
+ while (B.segs[n]?.hole && B.segs[n].a.free > B.segs[n].b.free + tol) n++; // (a lead-in at the mouth)
474
+ const main = B.segs[n]?.hole ? B.segs[n] : first, d = size(main), free = Math.min(main.a.free, main.b.free);
475
+ let stop = main.z1, pocket = null, blind = false, m = B.segs.indexOf(main) + 1;
476
+ for (; m < B.segs.length; m++) {
477
+ const seg = B.segs[m];
478
+ if (seg.solid) { blind = true; break; }
479
+ if (!seg.hole) { // a slot open to the side across the screw, the hole going on past it: a nut's slot (v.fixings.nutTrap)
480
+ if (!pocket && seg.a.free > free + 0.3 && same(seg.a, seg.b) && B.segs.slice(m + 1).some((s) => s.hole)) pocket = seg;
481
+ continue;
482
+ }
483
+ if (Math.min(seg.a.free, seg.b.free) < free - tol) { blind = true; break; } // (it narrows: a hole's point, a step)
484
+ if (seg.a.free > free + 0.3 && same(seg.a, seg.b) && !pocket) pocket = seg; // (it widens to a seat on the far side)
485
+ if (!pocket) stop = seg.z1;
486
+ }
487
+ const o = offAxis(main); if (o != null && o > worst) worst = o;
488
+ if (o != null && o > FASTEN.align.value) { say('fasten.holes', 'fail', `${B.name}'s hole is ${mm(o)} mm off the screw's axis: the holes do not line up`, { distance: r3(o) }); bad = true; }
489
+ end = { mouth, d, free: 2 * free, depth: (blind ? B.segs[m].z0 : stop) - mouth, blind, far: B.reach[1], pocket, main };
490
+ }
491
+ if (!bad) say('fasten.holes', 'pass', `one axis from under the head ${through.length ? `through ${through.join(', ')} ` : ''}into ${x.into}${worst > 1e-4 ? ` (the holes within ${mm(worst)} mm of it)` : ''}`, { distance: r3(worst) });
492
+
493
+ // fasten.clearance: what each hole passed measures, against the thread's clearance hole as drawn on this printer
494
+ if (cl.length) {
495
+ if (!clr.normal) say('fasten.clearance', 'info', `${fx.thread} has no clearance hole in its rule set (${fx.set}): the holes it passes are not held to one`);
496
+ else for (const c of cl) {
497
+ const lo = (clr.close ?? clr.normal).diameter, hi = (clr.loose ?? clr.normal).diameter, want = clr.normal, basis = `${want.basis.rule} ${mm(want.nominal)} mm${want.comp ? ` and ${printer}.hole_comp ${mm(want.comp)} mm` : ''}`;
498
+ const values = { diameter: r3(c.d), wanted: want.diameter, least: lo, most: hi };
499
+ if (c.d < lo - sizeTol) say('fasten.clearance', 'fail', `${c.name}'s hole measures ${mm(c.d)} mm where ${an(fx.thread)} ${fx.thread} screw passes through: a clearance hole should be ${mm(want.diameter)} mm as drawn (${basis}), ${mm(lo)} mm at the closest`, values);
500
+ else if (c.d > hi + sizeTol) say('fasten.clearance', 'warn', `${c.name}'s hole measures ${mm(c.d)} mm, wider than the loosest clearance hole for ${fx.thread}, ${mm(hi)} mm as drawn: the screw does not place the part, and the head has less to sit on (${mm(want.diameter)} mm is the normal hole)`, values);
501
+ else say('fasten.clearance', 'pass', `${c.name}'s hole measures ${mm(c.d)} mm, ${an(fx.thread)} ${fx.thread} clearance hole as drawn (${Math.abs(c.d - want.diameter) <= sizeTol ? basis : `between the close ${mm(lo)} mm and the loose ${mm(hi)} mm; the normal hole is ${mm(want.diameter)} mm`})`, values);
502
+ }
503
+ }
504
+ if (!end) return;
505
+
506
+ // fasten.end: what holds the screw there
507
+ const kinds = [['pilot', 'a pilot the screw forms its own thread in'], ['tap', 'a hole to tap'], ['insert', 'a heat-set insert\'s hole']].map(([kind, says]) => ({ kind, says, hole: drawn(kind) })).filter((c) => c.hole);
508
+ const isClearance = clr.normal && end.d >= (clr.close ?? clr.normal).diameter - sizeTol && end.d <= (clr.loose ?? clr.normal).diameter + sizeTol;
509
+ const options = () => [...kinds.map((c) => `${c.says} (${mm(c.hole.diameter)} mm)`), ...(fx.made.nuts.length ? ['a nut\'s seat on a clearance hole, or a clearance hole with "nut"'] : [])].join(', ');
510
+ const endValues = { diameter: r3(end.d), depth: r3(end.depth) };
511
+ let seatNut = null;
512
+ if (end.pocket && !end.blind) { // a seat drawn on the far side: whose nut is it
513
+ const sh = end.pocket.a.loop.shape, across = sh === 'hex' || sh === 'square' ? end.pocket.a.loop.flats : 2 * end.pocket.a.free;
514
+ for (const kind of sh === 'square' ? ['square'] : ['hex', 'lock']) { try { const n = nutModel(kind); if (across >= n.flats - 0.05 && across <= n.flats + 1 && (!seatNut || wantNut === kind)) seatNut = { kind, model: n, across }; } catch {} }
515
+ }
516
+ if (end.motor) { end.kind = 'tap'; say('fasten.end', 'pass', `ends in ${x.into}'s own ${fx.thread} thread, ${mm(end.depth)} mm deep (${end.motor.model}.mount_hole_depth)`, endValues); }
517
+ else if (wantNut || seatNut) {
518
+ const kind = wantNut ?? seatNut.kind, nm = nutModel(kind);
519
+ if (end.blind) say('fasten.end', 'fail', `a nut is given, and ${x.into}'s hole is blind, ${mm(end.depth)} mm deep: a nut holds a screw that comes out of the far side`, endValues);
520
+ else if (!isClearance) say('fasten.end', 'fail', `a nut holds this screw, so ${x.into} is passed through: its hole measures ${mm(end.d)} mm, and a clearance hole should be ${mm(clr.normal?.diameter ?? fx.major)} mm as drawn`, endValues);
521
+ else {
522
+ const zNut = end.pocket && seatNut ? end.pocket.z0 : end.far;
523
+ end.kind = 'nut'; end.nut = { kind, model: nm, z: zNut, seated: !!(end.pocket && seatNut), corner: end.pocket && seatNut && (end.pocket.a.loop.corner || end.pocket.a.loop.along) ? { seg: end.pocket } : null };
524
+ say('fasten.end', 'pass', `ends in ${an(nm.name)} ${nm.name} ${end.nut.seated ? `in its seat in ${x.into}, ${mm(seatNut.across)} mm across the flats for the nut's ${mm(nm.flats)} mm` : `on the far face of ${x.into}`}, past a ${mm(end.d)} mm clearance hole`, { ...endValues, flats: nm.flats });
525
+ }
526
+ } else {
527
+ const hit = kinds.filter((c) => Math.abs(end.d - c.hole.diameter) <= sizeTol).sort((p, q) => Math.abs(end.d - p.hole.diameter) - Math.abs(end.d - q.hole.diameter))[0];
528
+ let threaded = null;
529
+ if (!hit && end.main.a.loop.shape !== 'round') { try { const t = F.threadModel(fx, { internal: true, length: Math.max(end.depth, fx.pitch * 2) }); if (end.free > fx.major - 2 * fx.pitch && end.free < fx.major + 1) threaded = t; } catch {} }
530
+ if (hit) {
531
+ end.kind = hit.kind;
532
+ const basis = hit.hole.basis, trust = basis.kind === 'measured' ? 'measured' : basis.kind === 'spec' ? 'a standard\'s figure' : `${/^[aeiou]/.test(basis.kind) ? 'an' : 'a'} ${basis.kind} figure`;
533
+ say('fasten.end', 'pass', `ends in ${x.into}, in ${hit.says}: ${mm(end.d)} mm as drawn (${basis.rule}, ${trust}), ${end.blind ? `${mm(end.depth)} mm deep` : `through its ${mm(end.depth)} mm`}`, endValues);
534
+ if (hit.kind === 'insert') { try { end.insert = F.insertModel(fx.thread); } catch { end.insert = null; } }
535
+ } else if (threaded) { end.kind = 'thread'; say('fasten.end', 'pass', `ends in ${x.into}, in a thread drawn to print, ${mm(end.free)} mm across its crests, ${end.blind ? `${mm(end.depth)} mm deep` : `through its ${mm(end.depth)} mm`}`, endValues); }
536
+ else if (isClearance) say('fasten.end', 'fail', `nothing holds the screw in ${x.into}: its hole measures ${mm(end.d)} mm, a clearance hole. The far end should be ${options()}`, endValues);
537
+ else say('fasten.end', 'fail', `${x.into}'s hole measures ${mm(end.d)} mm, which is no hole for ${fx.thread}: the far end should be ${options()}`, endValues);
538
+ }
539
+
540
+ // fasten.length: the length chosen, or the one given held to the stack as measured
541
+ const zGrip = end.kind === 'nut' ? end.nut.z : end.mouth, grip = zGrip - zHead, stack = zGrip - zSeat, intoMat = matOf(B), tipRoom = DEFAULTS.tip_room.value;
542
+ sc.grip = r3(grip);
543
+ let pick = null, none = null, need = null;
544
+ if (end.motor && form !== 'set') { // the longest sold length that stops short of the motor's thread's end, as motor.mount chooses
545
+ const sold = fx.figures.lengths?.value || [], room = end.depth - tipRoom, L0 = [...sold].reverse().find((l) => l > grip + EPS && l - grip <= room + EPS);
546
+ if (L0) pick = { name: F.screwModel(fx, { length: L0, head: form }).name, length: L0, engaged: r3(L0 - grip), spare: r3(room - (L0 - grip)) };
547
+ else none = `no ${fx.thread} ${form} screw that is sold goes ${mm(grip)} mm under its head into ${x.into} and stops within its ${mm(end.depth)} mm of thread, ${mm(tipRoom)} mm kept at its tip`;
548
+ need = pick ? pick.engaged : null;
549
+ } else if (end.kind && form !== 'set' && stack > 1e-6) {
550
+ const into = { kind: end.kind, ...(end.kind === 'nut' ? { nut: end.nut.kind } : end.blind ? (end.kind === 'insert' ? { room: Math.max(end.depth - tipRoom, end.insert?.length ?? 0) } : { depth: end.depth }) : { through: true }), ...(['tap', 'thread'].includes(end.kind) && intoMat.cls ? { material: intoMat.known ? intoMat.name : intoMat.cls } : {}) };
551
+ try { pick = F.lengthFor(r3(stack), fx, { into, head: form, ...(washer ? { washer: true } : {}) }); need = pick.need; } catch (e) { none = e.message.replace(/^lengthFor: /, ''); }
552
+ }
553
+ // how much thread the far end offers, and how much the rule for it asks
554
+ const avail = !end.kind ? 0 : end.kind === 'nut' ? end.nut.model.height : end.kind === 'insert' ? Math.min(end.insert?.length ?? end.depth, end.depth) : end.depth;
555
+ if (need == null && end.kind) {
556
+ if (end.kind === 'nut') need = end.nut.model.height;
557
+ else if (end.kind === 'insert') need = Math.min(DEFAULTS.insert_engagement.value * fx.major, end.insert?.length ?? Infinity);
558
+ else if (end.kind === 'pilot' && fx.figures.pilot_depth) need = fx.figures.pilot_depth.value;
559
+ else { try { need = F.engagement(fx, { depth: 1, material: end.kind === 'pilot' ? 'plastic' : intoMat.known ? intoMat.name : intoMat.cls ?? 'plastic' }).need; } catch { need = null; } }
560
+ }
561
+ let L = given ?? pick?.length ?? null, sold = true;
562
+ if (form === 'set' && L == null) L = Math.max(1, Math.floor(end.depth));
563
+ if (L == null) { const want = grip + (need ?? fx.major), listed = fx.figures.lengths?.value; L = (Array.isArray(listed) && listed.find((l) => l >= want - 1e-9)) || Math.ceil(want); } // (none fits: drawn at the nearest, so the picture still shows the joint)
564
+ let model;
565
+ try { model = F.screwModel(fx, { length: L, head: form }); } catch (e) { sold = false; model = F.screwModel(fx, { length: L, head: form, anyLength: true }); if (given != null) say('fasten.length', 'fail', e.message.replace(/^screw: /, '')); }
566
+ const inEnd = L - grip, zTip = zHead + L;
567
+ sc.dir = D; sc.engaged = end.kind ? r3(Math.max(0, Math.min(inEnd, avail))) : 0; sc.endKind = end.kind ?? null; sc.intoMaterial = intoMat.known ? intoMat.name : null; sc.bear = (washer ? washer.diameter : head?.across ?? fx.major * 2) / 2;
568
+ sc.length = L; sc.model = model; sc.between = [at(zHead), at(zTip)]; sc.end = end.kind ?? null;
569
+ const lenValues = { length: L, grip: r3(grip), into: r3(inEnd) }, under = `${mm(grip)} mm under its head${washer ? ' (its washer with it)' : ''}`;
570
+ const wouldDo = pick && pick.length !== L ? `${pick.name} would do` : null;
571
+ if (given == null) {
572
+ if (pick) say('fasten.length', 'pass', `${pick.name} chosen, the ${end.motor ? 'longest sold that stops short of the end of the thread' : 'shortest sold that fits'}: ${under} and ${mm(pick.engaged)} mm into ${x.into}${end.kind === 'nut' ? '\'s nut' : ''}${pick.spare != null ? `, ${mm(pick.spare)} mm short of bottoming out` : ''}`, lenValues);
573
+ else if (none) say('fasten.length', 'fail', `no length chosen: ${none}`, lenValues);
574
+ else if (form !== 'set') say('fasten.length', 'info', `no length chosen until the far end is one a screw holds in: ${model.name} is drawn`, lenValues);
575
+ } else if (inEnd <= EPS) say('fasten.length', 'fail', `${model.name} does not reach ${x.into}: it is ${mm(L)} mm long and there is ${under}${wouldDo ? `. ${wouldDo}` : ''}`, lenValues);
576
+ else if (end.blind && inEnd > end.depth - (end.kind === 'insert' ? 0 : tipRoom) + EPS) say('fasten.length', 'fail', `${model.name} bottoms out: with ${under} it goes ${mm(inEnd)} mm into a hole ${mm(end.depth)} mm deep${end.kind === 'insert' ? '' : `, where ${mm(tipRoom)} mm is kept at its tip`}, so it tightens on its tip and not on its head${wouldDo ? `. ${wouldDo}` : `. Deepen the hole to ${mm(inEnd + tipRoom)} mm, or use a shorter screw`}`, { ...lenValues, depth: r3(end.depth) });
577
+ else {
578
+ const far = end.kind === 'nut' ? end.nut.z + end.nut.model.height : end.far, out = end.blind ? 0 : zTip - far, most = FASTEN.proud.value * fx.major;
579
+ if (out > most + EPS) say('fasten.length', 'warn', `${model.name} stands ${mm(out)} mm proud of ${end.kind === 'nut' ? 'its nut' : `${x.into}'s far face`}, more than its own diameter${wouldDo ? `: ${wouldDo}` : ''}`, { ...lenValues, proud: r3(out) });
580
+ else if (sold) say('fasten.length', 'pass', `${model.name} reaches: ${under}, ${mm(inEnd)} mm into ${x.into}${end.kind === 'nut' ? '\'s nut' : ''}${end.blind ? `, ${mm(end.depth - inEnd)} mm short of the hole's end` : out > 1e-6 ? `, its tip ${mm(out)} mm proud` : ''}`, lenValues);
581
+ }
582
+
583
+ // fasten.engagement: thread engaged at the far end, against the rule for what it is in
584
+ if (end.motor && form !== 'set') { const e = Math.max(0, Math.min(inEnd, end.depth)); say('fasten.engagement', 'pass', `${mm(e)} mm of ${x.into}'s own thread, ${mm(+(e / fx.pitch).toFixed(1))} turns of its ${mm(end.depth)} mm: a bought part's thread, held to its depth and to no engagement rule`, { engaged: r3(e), depth: end.depth, turns: +(e / fx.pitch).toFixed(1) }); }
585
+ else if (end.kind && need != null && form !== 'set') {
586
+ const engaged = Math.max(0, Math.min(inEnd, avail)), ok = engaged >= need - EPS, values = { engaged: r3(engaged), need: r3(need), turns: +(engaged / fx.pitch).toFixed(1) };
587
+ const what2 = end.kind === 'nut' ? `its ${end.nut.model.name}, ${mm(avail)} mm high` : end.kind === 'insert' ? `the insert, ${mm(avail)} mm long` : `${intoMat.name ? `${intoMat.name}${intoMat.cls && intoMat.known ? ` (a${intoMat.cls === 'aluminium' ? 'n' : ''} ${intoMat.cls})` : ''}` : end.kind === 'pilot' ? 'plastic' : x.into}`;
588
+ const rule = end.kind === 'nut' ? 'a nut wants its whole height engaged' : end.kind === 'insert' ? `fixings.insert_engagement asks a diameter of thread or the whole insert, ${mm(need)} mm` : end.kind === 'pilot' && fx.figures.pilot_depth ? `${fx.set}.pilot_depth asks ${mm(need)} mm` : `engagement.${end.kind === 'pilot' ? 'plastic' : intoMat.cls ?? 'plastic'} asks ${mm(need / fx.major)} diameters, ${mm(need)} mm`;
589
+ if (ok) say('fasten.engagement', 'pass', `${mm(engaged)} mm of thread in ${what2}, ${mm(values.turns)} turns: ${rule}`, values);
590
+ else {
591
+ const cure = avail < need - EPS ? (end.kind === 'insert' || end.kind === 'nut' ? `there is only ${mm(avail)} mm of it` : `the hole offers ${mm(avail)} mm: ${end.blind ? `deepen it to ${mm(need + tipRoom)} mm` : `${x.into} wants ${mm(need)} mm of thread on the axis`}`)
592
+ : pick && pick.length > L ? `${pick.name} would do` : none ? none : `a screw ${mm(grip + need)} mm or longer would do`;
593
+ say('fasten.engagement', 'fail', `${model.name} engages ${mm(engaged)} mm of thread in ${what2}, ${mm(values.turns)} turns, where ${rule}: ${cure}`, values);
594
+ }
595
+ } else if (end.kind && form !== 'set') say('fasten.engagement', 'info', `nothing says what ${x.into} is made of, so the thread engaged in it is not held to a rule: give its object a "material"`);
596
+
597
+ // fasten.seat: a flat seat under the head, as wide as the head and square to the axis, and what it takes
598
+ if (A) {
599
+ const seatPart = A.segs[bore0], near = seatPart.of.near, zLocal = (z) => (flip ? -z : z), bearR = (washer ? washer.diameter : head.across) / 2, boreR = seatPart.a.free, seatMat = matOf(A); // (boreR: the hole's mouth at the seat, a lead-in with it)
600
+ const e = 0.02;
601
+ if (form === 'countersunk') {
602
+ if (!cone) say('fasten.seat', 'fail', `a countersunk head sits in a ${mm(head.angle)}° countersink, and ${A.name}'s hole has a square edge: the head stands ${mm(proud)} mm proud on a line of contact. Draw it with v.fixings.countersink`, { proud: r3(proud) });
603
+ else {
604
+ const ang = (2 * Math.atan((cone.a.free - cone.b.free) / (cone.z1 - cone.z0)) * 180) / Math.PI, okAngle = Math.abs(ang - head.angle) <= FASTEN.countersink.value;
605
+ if (!okAngle) say('fasten.seat', 'fail', `${A.name}'s countersink is ${mm(+ang.toFixed(1))}°, and ${fx.thread}'s countersunk head is ${mm(head.angle)}°: the head bears on a line, not on its cone`, { angle: +ang.toFixed(1), wanted: head.angle });
606
+ else if (proud > 0.05) say('fasten.seat', 'warn', `${A.name}'s countersink is ${mm(2 * cone.a.free)} mm across at its mouth, and the head is ${mm(head.across)} mm: the head stands ${mm(proud)} mm proud`, { proud: r3(proud), angle: +ang.toFixed(1) });
607
+ else say('fasten.seat', 'pass', `the countersunk head sits in a ${mm(+ang.toFixed(1))}° countersink in ${A.name}, its top ${zSeat - A.reach[0] > 0.01 ? `${mm(zSeat - A.reach[0])} mm under the face` : 'flush with the face'}`, { angle: +ang.toFixed(1), sunk: r3(zSeat - A.reach[0]) });
608
+ }
609
+ } else {
610
+ const under = Math.min(ring(near, zLocal(zSeat + e), boreR + 0.6 * (bearR - boreR), false), ring(near, zLocal(zSeat + e), bearR - e, false)), over = Math.max(ring(near, zLocal(zSeat - e), boreR + 0.6 * (bearR - boreR), false), ring(near, zLocal(zSeat - e), bearR - e, false));
611
+ const whole = under > 1 - 1e-9 && over < 1e-9, values = { under: r3(under), across: 2 * bearR, hole: r3(2 * boreR) };
612
+ if (!flat || !whole) say('fasten.seat', 'fail', flat && washer && over > 1e-9 && zSeat - A.reach[0] > 0.01 ? `the washer, ${mm(2 * bearR)} mm across, does not go down ${A.name}'s counterbore to the seat: the counterbore is drawn for the head alone (${mm(head.across)} mm)`
613
+ : !flat || over > 1e-9 ? `the ${washer ? 'washer' : 'head'} has no flat seat on ${A.name}: the face under it is not square to the screw (${cone ? 'a cone' : `material stands above the seat on ${Math.round(over * 100)}% of the way round`})` : `the ${washer ? 'washer' : 'head'} overhangs its seat on ${A.name}: there is material under ${Math.round(under * 100)}% of the ring out to its ${mm(2 * bearR)} mm, where a flat seat as wide as the ${washer ? 'washer' : 'head'} is asked`, values);
614
+ else {
615
+ // what the seat takes: the ring under the head as drawn, against the strength of what it sits on
616
+ let st = null;
617
+ if (seatMat.name && seatMat.known) { try { st = F.seating(fx, { head: form, material: seatMat.name, ...(torque != null ? { torque } : {}) }); } catch { st = null; } }
618
+ const sunk = zSeat - A.reach[0], where = `a flat seat on ${A.name}${sunk > 0.01 ? `, ${mm(sunk)} mm down its counterbore` : ''}, whole out to the ${washer ? 'washer' : 'head'}'s ${mm(2 * bearR)} mm`;
619
+ if (!st) say('fasten.seat', 'pass', `${where}${seatMat.name ? ` (no strength is known for ${seatMat.name}, so the pressure under it is not checked)` : ''}`, values);
620
+ else {
621
+ const D2 = st.head === 'hex' ? head.across * Math.cos(Math.PI / 6) : head.across, area = (d) => (Math.PI / 4) * (d * d - (2 * boreR) ** 2), bare = area(D2), onWasher = st.washer ? area(st.washer.diameter) : null, mine = washer ? onWasher : bare;
622
+ const takes = st.limit * mine, K = DEFAULTS.nut_factor.value, torqueOf = (N) => (K * fx.major * N) / 1000, v2 = { ...values, area: +mine.toFixed(2), limit: st.limit, force: +takes.toFixed(1), torque: +torqueOf(takes).toFixed(3) };
623
+ sc.most = +torqueOf(takes).toFixed(2); sc.seatForce = takes;
624
+ if (torque == null) say('fasten.seat', 'pass', `${where}; on ${st.material} (${mm(st.limit)} MPa) its ${mm(+mine.toFixed(1))} mm² takes ${mm(+takes.toFixed(0))} N, a tightening of ${mm(+torqueOf(takes).toFixed(2))} N m${!washer && onWasher && sunk <= 0.01 ? `: the ${fx.thread} washer would raise that to ${mm(+(st.limit * onWasher).toFixed(0))} N (${mm(+torqueOf(st.limit * onWasher).toFixed(2))} N m)` : ''}`, v2);
625
+ else {
626
+ const p = st.force / mine, within = p <= st.limit + 1e-9, pw = onWasher ? st.force / onWasher : null;
627
+ if (within) say('fasten.seat', 'pass', `${where}; at ${mm(torque)} N m the screw clamps with ${mm(st.force)} N, ${mm(+p.toFixed(1))} MPa under the ${washer ? 'washer' : 'head'} on ${st.material}, whose strength is ${mm(st.limit)} MPa`, { ...v2, pressure: +p.toFixed(1) });
628
+ else say('fasten.seat', 'fail', `at ${mm(torque)} N m the screw clamps with ${mm(st.force)} N: ${mm(+p.toFixed(1))} MPa under the ${washer ? 'washer' : 'head'} on ${st.material}, whose strength is ${mm(st.limit)} MPa. ${!washer && pw != null && pw <= st.limit && sunk <= 0.01 ? `The ${fx.thread} washer, ${mm(st.washer.diameter)} mm across, brings it to ${mm(+pw.toFixed(1))} MPa, within: give "washer": true` : `The seat takes ${mm(+takes.toFixed(0))} N (${mm(+torqueOf(takes).toFixed(2))} N m): tighten to that, or spread the load wider`}`, { ...v2, pressure: +p.toFixed(1), ...(pw != null ? { washer: +pw.toFixed(1) } : {}) });
629
+ }
630
+ }
631
+ }
632
+ }
633
+ }
634
+
635
+ // The hardware, each piece where it sits: its own frame, the item it moves with, the way it comes out, what it may overlap
636
+ const host = (r) => r.parts[0], up = D.map((c) => -c), piece = (kind, m, o, P, Z, r, lift, allow, X) => sc.hardware.push({ kind, shape: shapeOf(F, kind, m, o), M: standing(P, Z, X), host: host(r), lift, allow });
637
+ const ends = [...new Set(B.parts.map((p) => p.leaf))];
638
+ if (A) piece('screw', fx, { length: L, head: form, anyLength: true }, at(zHead), up, A, up, ends);
639
+ else piece('screw', fx, { length: L, head: form, anyLength: true }, at(zSeat), up, B, up, ends);
640
+ if (washer) piece('washer', fx.thread, {}, at(zSeat), up, A, up, []);
641
+ if (end.kind === 'nut') {
642
+ let X = null;
643
+ if (end.nut.corner) { const lp = end.nut.corner.seg.a.loop, pr = end.nut.corner.seg.of, v = lp.corner ? [lp.corner[0] - lp.c[0], lp.corner[1] - lp.c[1]] : lp.along, local = add(pr.x.map((c) => c * v[0]), pr.y, v[1]), pm = end.nut.corner.seg.part.m; X = pm ? [0, 1, 2].map((r) => pm[r] * local[0] + pm[4 + r] * local[1] + pm[8 + r] * local[2]) : local; }
644
+ piece('nut', fx.thread, { kind: end.nut.kind }, at(end.nut.z), D, B, D, [], X);
645
+ }
646
+ if (end.kind === 'insert' && end.insert) piece('insert', fx.thread, {}, at(end.mouth), up, B, up, ends);
647
+ // fasten.access: the key's way to the head, along its axis, with every other part in place (said by fastenChecks, with the order)
648
+ const tool = toolFor(model, form, fx);
649
+ sc.tool = tool?.name ?? null;
650
+ if (opts.access && tool) sc.access = accessOf(parts, tool, add(A ? at(zHead) : at(zSeat), up, model.top), up);
651
+ });
652
+
653
+ // The pieces of this relation, named, and the fits it stands for
654
+ const count = { screw: 0, nut: 0, washer: 0, insert: 0 };
655
+ for (const sc of rel.screws) for (const h of sc.hardware) {
656
+ const id = `fasten-${k + 1}-${h.kind}-${sc.index + 1}`, hostIndex = ids.indexOf(h.host.item), hostWorld = rowsToMat4(worlds[hostIndex]);
657
+ count[h.kind]++;
658
+ const others = sc.hardware.filter((o) => o !== h).map((o) => `fasten-${k + 1}-${o.kind}-${sc.index + 1}`); // (a screw in its own nut, washer and insert)
659
+ const hm = h.shape.model;
660
+ hardware.push({ id, kind: h.kind, name: hm.name, thread: fx.thread, fasten: k, screw: sc.index, grams: hm.mass, label, length: hm.length ?? null, head: h.kind === 'screw' ? hm.head : h.kind === 'nut' ? hm.kind : null, shape: h.shape, host: hostIndex, hostItem: h.host.item, with: h.host.object || h.host.item,
661
+ local: mat4.multiply(mat4.create(), mat4.invert(mat4.create(), hostWorld), h.M), lift: vec3.transform([0, 0, 0], h.lift, mat4.invert(mat4.create(), [...hostWorld.slice(0, 12), 0, 0, 0, 1])), allow: [...h.allow, ...others] });
662
+ }
663
+ const names = new Map();
664
+ for (const h of hardware.filter((o) => o.fasten === k)) names.set(h.name, (names.get(h.name) ?? 0) + 1);
665
+ // the screws' lines carry the key that turns them and how tight: the torque given, or the most the seat under the head takes
666
+ const keys = [...new Set(rel.screws.map((sc) => sc.tool).filter(Boolean))], most = rel.screws.map((sc) => sc.most).filter((t) => t != null);
667
+ const tighten = torque != null ? { torque, given: true } : most.length ? { torque: Math.min(...most), given: false } : null;
668
+ const lines = [...names].map(([fastener, n]) => ({ fastener, count: n, ...(rel.screws.some((sc) => sc.model?.name === fastener) ? { ...(keys.length ? { key: keys.join(' or ') } : {}), ...(tighten ? { tighten } : {}) } : {}) }));
669
+ through.forEach((t, j) => fits.push({ a: t, b: x.into, fastens: true, silent: true, fasten: k, note: x.note || `${t} fastened to ${x.into}`, ...(j === 0 && lines[0] ? lines[0] : {}) }));
670
+ if (!through.length && lines[0]) fits.push({ a: x.into, b: x.into, silent: true, fasten: k, note: label, ...lines[0] });
671
+ for (const l of lines.slice(1)) fits.push({ a: through[0] ?? x.into, b: x.into, silent: true, fasten: k, note: x.note || `${through[0] ?? x.into} fastened to ${x.into}`, ...l });
672
+ rel.count = count; rel.torque = torque;
673
+ rel.load = loadOf(s, worlds, rel, list, { F, fx, ids });
674
+ if (rel.load?.tighten != null && torque == null) for (const ft of fits.filter((q) => q.fasten === k && q.tighten)) ft.tighten = { torque: rel.load.tighten, given: false, load: true };
675
+ relations.push(rel);
676
+ });
677
+ return { relations, hardware, fits };
678
+ }
679
+
680
+ // ---------- fasten.load: what a joint carries, shared between its screws ----------
681
+ const G = 9.80665; // m/s²
682
+ const sub3 = (p, q) => [p[0] - q[0], p[1] - q[1], p[2] - q[2]], scale3 = (p, k) => p.map((c) => c * k), rowsAt = (W, p) => [0, 1, 2].map((r) => W[4 * r] * p[0] + W[4 * r + 1] * p[1] + W[4 * r + 2] * p[2] + W[4 * r + 3]);
683
+ // The items a joint holds up: those left with no way to the ground (the first item) once the joint's own links are cut.
684
+ // Links are the motion's joints, fits that fasten and the other fasten relations; null where both sides keep a way.
685
+ function beyondOf(s, rel, list, ids) {
686
+ const idx = (n) => ids.indexOf(top(n)), members = new Set([...rel.through, rel.into].map(idx)), links = [];
687
+ for (const j of s.motion.joints || []) links.push([j.parent, j.child]);
688
+ for (const x of s.a.fits || []) if (x.fastens) links.push([idx(x.a), idx(x.b)]);
689
+ list.forEach((x, k) => { if (k !== rel.index) for (const t of x.through || []) links.push([idx(t), idx(x.into)]); });
690
+ const cut = links.filter(([p, q]) => p >= -1 && q >= 0 && !(members.has(p) && members.has(q))), reach = (from) => {
691
+ const seen = new Set(from);
692
+ for (let grew = true; grew;) { grew = false; for (const [p, q] of cut) { if (seen.has(p) && !seen.has(q)) { seen.add(q); grew = true; } if (seen.has(q) && !seen.has(p)) { seen.add(p); grew = true; } } }
693
+ return seen;
694
+ };
695
+ const ground = reach([-1, 0]), loose = [...members].filter((m) => !ground.has(m));
696
+ if (!loose.length || loose.length === members.size) return null;
697
+ return reach(loose);
698
+ }
699
+ /**
700
+ * The load on one fasten relation and each screw's share of it: the masses of the parts it holds up and the "loads"
701
+ * hung on them, under the assembly's gravity, and the torque of a motor among them whose reaction it takes; each
702
+ * screw against its strength (screwLoad). Null where nothing loads the joint (no "loads", no motor beyond it).
703
+ */
704
+ function loadOf(s, worlds, rel, list, { F, fx, ids }) {
705
+ const ok = rel.screws.filter((sc) => sc.dir && sc.at), loads = (s.motion.loads || []).filter((l) => l.mass != null);
706
+ if (!ok.length || ok.length < rel.screws.length) return null;
707
+ const beyond = beyondOf(s, rel, list, ids);
708
+ const drives = (s.motion.drives || []).filter((d) => beyond?.has(d.item) && d.joint && (() => { const j = s.motion.joints.find((q) => q.id === d.joint); return j && !beyond.has(j.child); })());
709
+ if (!loads.length && !drives.length) return null;
710
+ if (!beyond) return { closed: true };
711
+ const u = unit(ok[0].dir);
712
+ if (ok.some((sc) => Math.abs(dot(unit(sc.dir), u) - 1) > 1e-6)) return { skew: true };
713
+ const headSide = rel.through.some((t) => beyond.has(ids.indexOf(top(t)))), n = headSide ? u.map((c) => -c) : u; // n: the way the held part comes off
714
+ const g = s.motion.gravity || [0, 0, -1], gl = Math.hypot(...g), down = g.map((c) => c / gl);
715
+ let Fw = [0, 0, 0], Mw = [0, 0, 0], kg = 0;
716
+ const hang = (mass, X) => { const f = scale3(down, mass * G); Fw = add(Fw, f); Mw = add(Mw, cross(X, f)); kg += mass; };
717
+ const masses = massesOf(s, { placeItems, jointValues });
718
+ for (const m of masses) if (beyond.has(m.item)) { const d = (s.motion.drives || []).find((q) => q.item === m.item), mass = m.kg || (d ? d.model.mass ?? 0 : 0); if (mass > 0) hang(mass, rowsAt(worlds[m.item], m.centre)); }
719
+ const hung = loads.filter((l) => beyond.has(l.item));
720
+ for (const l of hung) hang(l.mass, rowsAt(worlds[l.item], [l.frame[3], l.frame[7], l.frame[11]]));
721
+ // the joint's frame: e1, e2 across it, n along the screws; places about the screws' centroid
722
+ const c = ok.reduce((t, sc) => add(t, sc.at, 1 / ok.length), [0, 0, 0]), seed = Math.abs(n[0]) < 0.9 ? [1, 0, 0] : [0, 1, 0], e1 = unit(cross(seed, n)), e2 = cross(n, e1);
723
+ const places = ok.map((sc) => { const q = sub3(sc.at, c); return [dot(q, e1), dot(q, e2)]; }), local = (v) => [dot(v, e1), dot(v, e2), dot(v, n)];
724
+ const Mc = sub3(Mw, cross(c, Fw)), pivot = Math.min(...ok.map((sc) => sc.bear)); // N mm about the centroid
725
+ const torques = drives.map((d) => { const W = worlds[d.item], T = mountTorque(d), axis = [W[2], W[6], W[10]]; return { id: d.id, torque: T, about: T * dot(axis, n), M: scale3(axis, T * 1000) }; });
726
+ const Mt = torques.reduce((t, q) => add(t, q.M), [0, 0, 0]);
727
+ // the worst of the motor turning either way; each screw against its strength at its share
728
+ const cases = (torques.length ? [1, -1] : [1]).map((sign) => screwGroup(places, { force: local(Fw), moment: local(add(Mc, Mt, sign)).map((m) => m / 1000), pivot }));
729
+ const screws = ok.map((sc, i) => {
730
+ const pick = cases.map((gp) => gp.screws[i]).reduce((a, b) => (b.tension > a.tension + 1e-9 || (Math.abs(b.tension - a.tension) <= 1e-9 && b.shear > a.shear) ? b : a));
731
+ const threaded = ['pilot', 'tap', 'thread'].includes(sc.endKind) && sc.intoMaterial && sc.engaged > 0;
732
+ const r = F.screwLoad(fx, { tension: Math.max(0, pick.tension), shear: pick.shear, ...(threaded ? { depth: sc.engaged, material: sc.intoMaterial } : ['insert', 'nut'].includes(sc.endKind) ? { end: sc.endKind } : {}), ...(rel.torque != null ? { torque: rel.torque } : sc.seatForce ? { most: sc.seatForce } : {}) });
733
+ return { sc, tension: pick.tension, shear: pick.shear, r };
734
+ });
735
+ const share = torques.length ? Math.max(...screwGroup(places, { moment: [0, 0, torques.reduce((t, q) => t + q.about, 0)] }).screws.map((x) => x.shear)) : null;
736
+ const worst = screws.reduce((a, b) => (b.r.margin < a.r.margin ? b : a)), slip = screws.filter((x) => x.r.margins.slip != null).reduce((a, b) => (!a || b.r.margins.slip < a.r.margins.slip ? b : a), null);
737
+ const tighten = rel.torque == null ? Math.min(...screws.map((x) => x.r.preload.torque)) : null;
738
+ const rests = [...new Map(screws.flatMap((x) => x.r.rests).filter((q) => q.kind === 'estimate' || q.kind === 'community').map((q) => [q.name, q])).values()].map((q) => q.name);
739
+ return { beyond: [...beyond].filter((k) => k >= 0).map((k) => ids[k]), kg, hung: hung.length, force: Fw, torques, share, screws, worst, slip, tighten, rests, line: cases[0].line, turns: cases.map((gp) => gp.turns).find((t) => t != null) ?? null };
740
+ }
741
+
742
+ /**
743
+ * A piece of hardware as a placed part, where its item stands.
744
+ * @param {object} h a piece from fastenOf's hardware @param {number[]} hostWorld its item's place (a JSCAD mat4)
745
+ * @returns {object} a part as buildAssembly makes them: a reference, with `hardware` ({ kind, fasten, screw }), `moves` (the
746
+ * item it moves with), `lift` (the way it comes out, for an exploded view) and `allow` (the leaves it may overlap: what it is threaded into)
747
+ */
748
+ export function hardwarePart(h, hostWorld) {
749
+ const m = mat4.multiply(mat4.create(), hostWorld, h.local), move = (polys) => polys.map((poly) => poly.map((v) => vec3.transform([0, 0, 0], v, m)));
750
+ const lift = vec3.transform([0, 0, 0], h.lift, [...hostWorld.slice(0, 12), 0, 0, 0, 1]);
751
+ return { name: `${h.id}.body`, item: h.id, leaf: h.id, object: h.name, role: 'reference', label: `${h.id}: ${h.name}`, part: 'body',
752
+ geometry: jscad.transforms.transform(m, h.shape.geometry), polygons: move(h.shape.polygons), triangles: move(h.shape.triangles),
753
+ shape: `${h.shape.hash}:body`, hash: `${h.shape.hash}:body`, local: h.shape.polygons, localTriangles: h.shape.triangles, m,
754
+ hardware: { kind: h.kind, name: h.name, fasten: h.fasten, screw: h.screw, thread: h.thread, grams: h.grams, from: h.label, length: h.length, head: h.head }, moves: h.hostItem, with: h.with, lift, allow: h.allow };
755
+ }
756
+
757
+ const CODES = ['fasten.holes', 'fasten.clearance', 'fasten.end', 'fasten.length', 'fasten.engagement', 'fasten.seat', 'fasten.access'];
758
+ // fasten.access for one screw, with the order of assembly: a part over the head that goes on later is no trouble
759
+ function accessSays(sc, rel, order) {
760
+ const acc = sc.access, t = acc.tool, values = { key: t.size, radius: t.radius, reach: t.reach };
761
+ if (!acc.blockers.length) return { level: 'pass', text: `${an(t.name)} ${t.name} reaches the head along its axis: nothing within ${mm(t.radius)} mm of it for ${mm(t.reach)} mm over the head (${t.rests[0]}, ${t.rests[1]})`, values };
762
+ const names = (list) => list.map((b) => `${b.item} (${mm(b.z)} mm over the head)`).join(' and ');
763
+ const stuck = order?.stuck?.fasten === rel.index && order.stuck.screws?.includes(sc.index) ? order.stuck : null;
764
+ if (stuck) {
765
+ const b = acc.blockers.find((x) => x.item === stuck.wall) ?? acc.blockers[0], tail = stuck.text.split(`${stuck.wall} stands over it`)[1] ?? '';
766
+ return { level: 'fail', text: `${an(t.name)} ${t.name} cannot reach the head: ${b.item} stands over it, ${mm(b.z)} mm up the screw's axis${tail}${t.size && acc.tilt == null && /hex key/.test(t.name) ? `, and a ball-end key tilted ${mm(peekRuleSet('hexkey').rules.ball_end_tilt.value)}° does not clear it either` : ''}. Move the screw, open a way through ${b.item}, or fit ${b.item} after`, values: { ...values, distance: b.z }, wall: b.item };
767
+ }
768
+ const step = (id) => order?.steps?.find((st) => st.items.includes(id))?.n, last = Math.max(...[...rel.through, rel.into].map((n) => step(top(n)) ?? Infinity));
769
+ const after = order && !order.stuck && acc.blockers.every((b) => step(b.item) > last);
770
+ if (acc.tilt != null && !after) return { level: 'warn', text: `only a ball-end ${t.name} reaches the head, tilted up to ${mm(acc.tilt)}° (hexkey.ball_end_tilt): ${names(acc.blockers)} stand${acc.blockers.length > 1 ? '' : 's'} on the straight line, and a ball end takes less torque than a straight key`, values: { ...values, tilt: acc.tilt } };
771
+ return { level: 'pass', text: `${an(t.name)} ${t.name} reaches the head before ${acc.blockers.map((b) => `${b.item}${step(b.item) ? ` (step ${step(b.item)})` : ''}`).join(' and ')} goes on over it`, values: { ...values, distance: acc.blockers[0].z } };
772
+ }
773
+ const RANK = { fail: 0, warn: 1, info: 2, pass: 3 };
774
+ // fasten.load for one relation: what it carries, the screw that takes the most against its strength, and slipping
775
+ function loadSays(s, f, rel, lead) {
776
+ const L = rel.load; if (!L) return;
777
+ const refs = { fasten: rel.index, screw: rel.screw, through: rel.through, into: rel.into, screws: rel.screws.map((sc) => sc.name), ...(s.pose ? { pose: s.pose } : {}) };
778
+ if (L.closed) return f('info', `${lead}: ${[...rel.through, rel.into].join(' and ')} are held to the ground by other ways too, so what this joint carries is not shared out to its screws`, null, { code: 'fasten.load', refs });
779
+ if (L.skew) return f('info', `${lead}: the screws do not stand parallel, so the load is not shared out between them`, null, { code: 'fasten.load', refs });
780
+ const n = rel.screws.length, w = L.worst, who = (x) => (n > 1 ? `screw ${x.sc.index + 1} (${x.sc.name})` : 'the screw'), r1 = (x) => +x.toFixed(1);
781
+ const equal = L.torques.length && L.screws.every((x) => Math.abs(x.shear - L.screws[0].shear) < 1e-6);
782
+ const motor = L.torques.map((q) => `${q.id}'s ${mm(+q.torque.toFixed(3))} N m about its shaft (what it gives at its working speed), ${mm(r1(L.share))} N ${n > 1 ? 'on each screw' : 'on the screw'} from that alone`).join(' and ');
783
+ const carries = `${L.beyond.join(', ')}, ${mm(+L.kg.toFixed(3))} kg${L.hung ? ` with the load${L.hung > 1 ? 's' : ''} hung on ${L.beyond.length > 1 ? 'them' : 'it'}` : ''}${motor ? `, and ${motor}` : ''}`;
784
+ const x0 = w.r.findings[0], rests = L.rests.length ? ` It rests on estimates: ${L.rests.join(', ')}` : '';
785
+ const values = { margin: w.r.margin === Infinity ? null : w.r.margin, tension: r1(Math.max(0, w.tension)), shear: r1(w.shear), preload: w.r.preload.force, torque: w.r.preload.torque, kg: +L.kg.toFixed(4), ...(L.share != null ? { share: L.share } : {}),
786
+ screws: L.screws.map((x) => ({ screw: x.sc.name, tension: r1(Math.max(0, x.tension)), shear: r1(x.shear), margin: x.r.margin === Infinity ? null : x.r.margin })) };
787
+ f(x0.level, `${lead} carry ${carries}: ${who(w)} takes the most: ${x0.text}${L.tighten != null && rel.torque == null ? `. Tighten to ${mm(L.tighten)} N m` : ''}.${rests}`, x0.level === 'pass' ? null : `${rel.label}: load`,
788
+ { code: 'fasten.load', at: w.sc.at.map(r3), between: w.sc.between.map((p) => p.map(r3)), values, refs: { ...refs, rests: L.rests, ...(equal ? { each: true } : {}) } });
789
+ if (L.slip) { const y = L.slip.r.findings[1]; f(y.level, `${lead}: ${who(L.slip)}: ${y.text}`, y.level === 'pass' ? null : `${rel.label}: slips`, { code: 'fasten.load', at: L.slip.sc.at.map(r3), values: y.values, refs }); }
790
+ if (L.turns != null) f('warn', `${lead}: a twist of ${mm(L.turns)} N m about the one screw is held by friction alone: a second screw would hold it by shear`, `${rel.label}: turns`, { code: 'fasten.load', values: { turns: L.turns }, refs });
791
+ }
792
+ /**
793
+ * The fasten findings of an assembly, in its findings shape: each relation's screws said together where they agree,
794
+ * one by one where they do not, each with `at` (under the head) and `between` (head to tip) so a viewer points at the screw.
795
+ * @param {object} s a built assembly (or one of its poses) @param {Function} f the check's f(level, text, card, extra)
796
+ * @param {{ placeItems: Function }} helpers
797
+ */
798
+ export function fastenChecks(s, f, { placeItems }) {
799
+ if (!s.a.fasten?.length) return;
800
+ const r = s.fastened ?? fastenOf(s, placeItems(s.motion, s.jointValues));
801
+ // the key's way, in the build pose: with the order, which places a part that covers a head after the screw
802
+ const built = s.pose == null || s.pose === s.motion.pose, wanted = built && r.relations.some((rel) => rel.screws.some((sc) => sc.access?.blockers.length));
803
+ const order = wanted && isBuilt(s) ? orderOf(s) : null, access = new Map();
804
+ if (built) for (const rel of r.relations) for (const sc of rel.screws) if (sc.access) access.set(sc, [accessSays(sc, rel, order)]);
805
+ for (const rel of r.relations) {
806
+ const n = rel.screws.length, lead = `${rel.label}, ${n === 1 ? `${an(rel.screw)} ${rel.screw} screw` : `${n} ${rel.screw} screws`}`;
807
+ for (const g of rel.general) f(g.level, `${rel.label}: ${g.text}`, `${rel.label}: no screws`, { code: g.code, refs: { fasten: rel.index, through: rel.through, into: rel.into } });
808
+ for (const code of CODES) {
809
+ const groups = new Map();
810
+ for (const sc of rel.screws) for (const x of (code === 'fasten.access' ? access.get(sc) : sc.found[code]) || []) {
811
+ const key = `${x.level}|${x.text}`;
812
+ if (!groups.has(key)) groups.set(key, { ...x, screws: [] });
813
+ groups.get(key).screws.push(sc);
814
+ }
815
+ for (const g of [...groups.values()].sort((a, b) => RANK[a.level] - RANK[b.level])) {
816
+ const all = g.screws.length === n, first = g.screws[0], who = all ? lead : `${rel.label}, ${g.screws.map((sc) => `screw ${sc.index + 1} (${sc.name})`).join(', ')}`;
817
+ f(g.level, `${who}: ${g.text}`, g.level === 'pass' || g.level === 'info' ? null : `${rel.label}: ${code.split('.')[1]}`,
818
+ { code, at: first.at.map(r3), between: first.between.map((p) => p.map(r3)), ...(g.values ? { values: g.values } : {}),
819
+ refs: { ...(g.wall ? { wall: g.wall } : {}), fasten: rel.index, screw: rel.screw, through: rel.through, into: rel.into, screws: g.screws.map((sc) => sc.name), ...(g.screws.length > 1 ? { places: g.screws.map((sc) => sc.at.map(r3)) } : {}), ...(s.pose ? { pose: s.pose } : {}) } });
820
+ }
821
+ }
822
+ loadSays(s, f, rel, lead);
823
+ }
824
+ }