@rulecad/cad 0.8.0 → 0.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (83) hide show
  1. package/AGENTS.md +24 -10
  2. package/CHANGELOG.md +404 -0
  3. package/EXAMPLES.md +40 -38
  4. package/README.md +193 -67
  5. package/TUTORIAL.md +2 -2
  6. package/bin/cad.mjs +2250 -512
  7. package/lib/animate.mjs +173 -49
  8. package/lib/assembly.mjs +1847 -434
  9. package/lib/beams.mjs +256 -52
  10. package/lib/belts.mjs +66 -15
  11. package/lib/board-work.mjs +12 -0
  12. package/lib/bought.mjs +309 -53
  13. package/lib/buildpage.mjs +179 -29
  14. package/lib/carried.mjs +201 -0
  15. package/lib/clone.mjs +194 -42
  16. package/lib/design.mjs +649 -121
  17. package/lib/drive.mjs +187 -63
  18. package/lib/edit.mjs +899 -191
  19. package/lib/errors.mjs +16 -3
  20. package/lib/explain.mjs +62 -13
  21. package/lib/fasten.mjs +2702 -507
  22. package/lib/fittings.mjs +715 -121
  23. package/lib/fixings.mjs +21 -9
  24. package/lib/framebuild.mjs +790 -175
  25. package/lib/framecheck.mjs +1901 -368
  26. package/lib/framegen.mjs +100 -22
  27. package/lib/frameheat.mjs +225 -42
  28. package/lib/frameprint.mjs +513 -108
  29. package/lib/frames.mjs +81 -17
  30. package/lib/framesize.mjs +792 -120
  31. package/lib/frametip.mjs +235 -0
  32. package/lib/gait.mjs +135 -23
  33. package/lib/grip.mjs +62 -13
  34. package/lib/guides.mjs +1403 -230
  35. package/lib/host.mjs +123 -21
  36. package/lib/inplace.mjs +171 -36
  37. package/lib/jigs.mjs +360 -78
  38. package/lib/joins.mjs +697 -118
  39. package/lib/jsonedit.mjs +88 -26
  40. package/lib/kept.mjs +84 -0
  41. package/lib/kit.mjs +438 -68
  42. package/lib/layout.mjs +2 -1
  43. package/lib/links.mjs +149 -42
  44. package/lib/list.mjs +326 -64
  45. package/lib/live-guard.mjs +68 -0
  46. package/lib/live-shape.mjs +96 -24
  47. package/lib/live.mjs +189 -59
  48. package/lib/loads.mjs +579 -130
  49. package/lib/machine.mjs +372 -74
  50. package/lib/magnets.mjs +162 -44
  51. package/lib/measured.mjs +417 -94
  52. package/lib/mesh.mjs +526 -119
  53. package/lib/meshcmd.mjs +215 -60
  54. package/lib/motors.mjs +1021 -213
  55. package/lib/motorsvg.mjs +114 -32
  56. package/lib/names.mjs +3 -1
  57. package/lib/optimise.mjs +978 -283
  58. package/lib/order.mjs +519 -112
  59. package/lib/panels.mjs +482 -94
  60. package/lib/parts.mjs +78 -23
  61. package/lib/pins.mjs +163 -44
  62. package/lib/plates.mjs +223 -35
  63. package/lib/player.mjs +37 -9
  64. package/lib/power.mjs +64 -13
  65. package/lib/preview.mjs +83 -16
  66. package/lib/profilemeasure.mjs +572 -129
  67. package/lib/rules.mjs +15 -5
  68. package/lib/sandbox-hooks.mjs +77 -19
  69. package/lib/sandbox-run.mjs +117 -18
  70. package/lib/sandbox.mjs +509 -60
  71. package/lib/schema.mjs +42 -11
  72. package/lib/seats.mjs +227 -49
  73. package/lib/services.mjs +261 -52
  74. package/lib/sheet.mjs +851 -178
  75. package/lib/slotnuts.mjs +213 -34
  76. package/lib/targets/gist.mjs +4 -1
  77. package/lib/targets.mjs +22 -4
  78. package/lib/task.mjs +376 -79
  79. package/lib/versions.mjs +111 -28
  80. package/lib/wires.mjs +228 -45
  81. package/lib/workspace.mjs +197 -12
  82. package/package.json +19 -19
  83. package/schemas/assembly.schema.json +2 -2
package/lib/parts.mjs CHANGED
@@ -6,9 +6,23 @@ import { CadError, near } from './errors.mjs';
6
6
  // A design's result split into its shapes and, for a lamina mechanism (@rulecad/flexures lamina()), what its checks
7
7
  // found and how it moves: plain data, so it crosses the sandbox and the cache with the parts.
8
8
  export function unpack(made) {
9
- const isLamina = made && typeof made === 'object' && !Array.isArray(made) && made.mechanism && made.parts && Array.isArray(made.findings);
9
+ const isLamina =
10
+ made &&
11
+ typeof made === 'object' &&
12
+ !Array.isArray(made) &&
13
+ made.mechanism &&
14
+ made.parts &&
15
+ Array.isArray(made.findings);
10
16
  if (!isLamina) return { shapes: made, mechanism: null };
11
- return { shapes: made.parts, mechanism: { ...made.mechanism, findings: made.findings, energy: made.energy, ...(made.frames ? { frames: made.frames } : {}) } };
17
+ return {
18
+ shapes: made.parts,
19
+ mechanism: {
20
+ ...made.mechanism,
21
+ findings: made.findings,
22
+ energy: made.energy,
23
+ ...(made.frames ? { frames: made.frames } : {}),
24
+ },
25
+ };
12
26
  }
13
27
 
14
28
  // What main(v) is given: the values and the tool's helpers (extras). Rules the code reads directly
@@ -16,14 +30,34 @@ export function unpack(made) {
16
30
  // refused: a misspelt v.widht would otherwise come through as undefined, and quietly draw another shape. `optional`
17
31
  // names may be absent: the display's own inputs, and what only some designs get (layouts, problems, the board engine).
18
32
  export function watch(values, extras = {}, optional = []) {
19
- const read = new Set(), may = new Set([...optional, 'layouts', 'problems', 'pcb', 'then', 'toJSON']);
20
- const given = { ...Object.fromEntries(Object.entries(values).map(([k, x]) => [k, x && typeof x === 'object'
21
- ? new Proxy(x, { get: (t, r) => { if (typeof r === 'string' && r in t) read.add(`${k}.${r}`); return t[r]; } }) : x])), ...extras };
22
- const v = new Proxy(given, { get: (t, r) => {
23
- if (typeof r !== 'string' || r in t || may.has(r)) return t[r];
24
- const guess = near(r, Object.keys(t));
25
- throw new CadError('design.unknown', `v.${r}: the design has no variable "${r}"${guess ? ` (did you mean "${guess}"?)` : ''}. Add it to the .json's variables, or its rule set to "uses"`);
26
- } });
33
+ const read = new Set(),
34
+ may = new Set([...optional, 'layouts', 'problems', 'pcb', 'then', 'toJSON']);
35
+ const given = {
36
+ ...Object.fromEntries(
37
+ Object.entries(values).map(([k, x]) => [
38
+ k,
39
+ x && typeof x === 'object'
40
+ ? new Proxy(x, {
41
+ get: (t, r) => {
42
+ if (typeof r === 'string' && r in t) read.add(`${k}.${r}`);
43
+ return t[r];
44
+ },
45
+ })
46
+ : x,
47
+ ]),
48
+ ),
49
+ ...extras,
50
+ };
51
+ const v = new Proxy(given, {
52
+ get: (t, r) => {
53
+ if (typeof r !== 'string' || r in t || may.has(r)) return t[r];
54
+ const guess = near(r, Object.keys(t));
55
+ throw new CadError(
56
+ 'design.unknown',
57
+ `v.${r}: the design has no variable "${r}"${guess ? ` (did you mean "${guess}"?)` : ''}. Add it to the .json's variables, or its rule set to "uses"`,
58
+ );
59
+ },
60
+ });
27
61
  return { v, read };
28
62
  }
29
63
 
@@ -32,26 +66,47 @@ export function named(result, meta) {
32
66
  const isShape = (r) => Array.isArray(r) || jscad.geometries.geom3.isA(r) || jscad.geometries.geom2.isA(r);
33
67
  const entries = isShape(result) ? [['body', result]] : Object.entries(result || {});
34
68
  const empty = (g) => g == null || (Array.isArray(g) && !g.length);
35
- if (!entries.some(([, g]) => !empty(g))) throw new CadError('design.empty', 'main() returned no shape: return a JSCAD solid, or named parts ({ cap, pillar })');
36
- for (const k of Object.keys(meta)) if (!meta[k].optional && !entries.some(([n, g]) => n === k && !empty(g))) throw new Error(`parts in the .json lists "${k}", but main() returns no such part`);
37
- return entries.filter(([name, g]) => !(empty(g) && meta[name]?.optional)).map(([name, g]) => {
38
- const m = meta[name] || {};
39
- if (m.role && !['part', 'support'].includes(m.role)) throw new Error(`part ${name}: role is part or support`);
40
- // The solid for booleans (fit checks) is JSCAD's own, exactly; a cached build restores the very same faces
41
- const geometry = solid(g);
42
- return { name, role: m.role || 'part', label: m.label || name, ...(m.filament ? { filament: m.filament } : {}), ...(m.mirror === false ? { mirror: false } : {}),
43
- geometry, polygons: polygons(g), triangles: polygons(g, { triangulate: true }) };
44
- });
69
+ if (!entries.some(([, g]) => !empty(g)))
70
+ throw new CadError(
71
+ 'design.empty',
72
+ 'main() returned no shape: return a JSCAD solid, or named parts ({ cap, pillar })',
73
+ );
74
+ for (const k of Object.keys(meta))
75
+ if (!meta[k].optional && !entries.some(([n, g]) => n === k && !empty(g)))
76
+ throw new Error(`parts in the .json lists "${k}", but main() returns no such part`);
77
+ return entries
78
+ .filter(([name, g]) => !(empty(g) && meta[name]?.optional))
79
+ .map(([name, g]) => {
80
+ const m = meta[name] || {};
81
+ if (m.role && !['part', 'support'].includes(m.role)) throw new Error(`part ${name}: role is part or support`);
82
+ // The solid for booleans (fit checks) is JSCAD's own, exactly; a cached build restores the very same faces
83
+ const geometry = solid(g);
84
+ return {
85
+ name,
86
+ role: m.role || 'part',
87
+ label: m.label || name,
88
+ ...(m.filament ? { filament: m.filament } : {}),
89
+ ...(m.mirror === false ? { mirror: false } : {}),
90
+ geometry,
91
+ polygons: polygons(g),
92
+ triangles: polygons(g, { triangulate: true }),
93
+ };
94
+ });
45
95
  }
46
96
 
47
97
  // A part's result as one JSCAD solid with its transforms applied (several solids become one set of faces), and back from
48
98
  // the exact faces a cache keeps: the same numbers, so booleans on a cached build agree with a fresh one.
49
99
  const { geom3, poly3 } = jscad.geometries;
50
- export const solid = (g) => geom3.create([g].flat(Infinity).filter((x) => geom3.isA(x)).flatMap((x) => geom3.toPolygons(x)));
100
+ export const solid = (g) =>
101
+ geom3.create(
102
+ [g]
103
+ .flat(Infinity)
104
+ .filter((x) => geom3.isA(x))
105
+ .flatMap((x) => geom3.toPolygons(x)),
106
+ );
51
107
  export const faces = (g) => geom3.toPolygons(g).map((p) => p.vertices.map((v) => [v[0], v[1], v[2]]));
52
108
  export const fromFaces = (list) => geom3.create(list.map((vs) => poly3.create(vs)));
53
109
 
54
-
55
110
  // Any JSCAD result -> polygons (corner lists), transforms applied. triangulate: also split faces at T-junctions (a
56
111
  // corner of one face in the middle of a neighbour's edge, left by booleans) so every edge pairs up, as slicers want.
57
112
  export function polygons(result, { triangulate = false } = {}) {
@@ -65,4 +120,4 @@ export function polygons(result, { triangulate = false } = {}) {
65
120
  }
66
121
  if (!out.length) throw new Error('the design has no 3D shapes');
67
122
  return out;
68
- }
123
+ }
package/lib/pins.mjs CHANGED
@@ -13,8 +13,12 @@ export { magnetFit } from './magnets.mjs';
13
13
 
14
14
  const { vec3 } = jscad.maths;
15
15
 
16
- const mm = (n) => String(Number(Number(n).toFixed(3))), r3 = (x) => +x.toFixed(3);
17
- const unit = (v) => { const l = Math.hypot(...v); return v.map((c) => c / l); };
16
+ const mm = (n) => String(Number(Number(n).toFixed(3))),
17
+ r3 = (x) => +x.toFixed(3);
18
+ const unit = (v) => {
19
+ const l = Math.hypot(...v);
20
+ return v.map((c) => c / l);
21
+ };
18
22
  const top = (n) => String(n).split(/[.@]/)[0];
19
23
  /** How far a hole as drawn may be from the pin's hole and still be taken for it (mm across): fasten's own tolerance, at most. */
20
24
  const TOL = FASTEN.size.value;
@@ -39,12 +43,26 @@ const PIN_KEYS = ['pin', 'count', 'fit', 'load', 'pull'];
39
43
  export function pinOfFit(x, project) {
40
44
  if (!x.fastener || typeof x.fastener !== 'object') return x;
41
45
  if ('magnet' in x.fastener) return magnetOfFit(x, fixingsOf(project).F); // (magnets: lib/magnets.mjs)
42
- const p = x.fastener, label = x.note || x.label || `${x.a} / ${x.b}`;
43
- if (typeof p.pin !== 'string' || !p.pin.trim()) throw new Error(`fit ${label}: fastener as an object names pins: { "pin": "snap pin 4x6", "count": 2 } (got ${JSON.stringify(p)})`);
46
+ const p = x.fastener,
47
+ label = x.note || x.label || `${x.a} / ${x.b}`;
48
+ if (typeof p.pin !== 'string' || !p.pin.trim())
49
+ throw new Error(
50
+ `fit ${label}: fastener as an object names pins: { "pin": "snap pin 4x6", "count": 2 } (got ${JSON.stringify(p)})`,
51
+ );
44
52
  const stray = Object.keys(p).filter((k) => !PIN_KEYS.includes(k));
45
- if (stray.length) throw new Error(`fit ${label}: pins have no "${stray.join('", "')}": a fastener naming pins takes ${PIN_KEYS.join(', ')} (the assembly schema's pins)`);
46
- if (x.count != null && p.count != null) throw new Error(`fit ${label}: give the pins' count once, in the fastener ({ "pin": ..., "count": 2 })`);
47
- return { fastens: true, ...x, pin: p, fastener: p.pin.trim(), ...(p.count != null || x.count != null ? { count: p.count ?? x.count } : {}) };
53
+ if (stray.length)
54
+ throw new Error(
55
+ `fit ${label}: pins have no "${stray.join('", "')}": a fastener naming pins takes ${PIN_KEYS.join(', ')} (the assembly schema's pins)`,
56
+ );
57
+ if (x.count != null && p.count != null)
58
+ throw new Error(`fit ${label}: give the pins' count once, in the fastener ({ "pin": ..., "count": 2 })`);
59
+ return {
60
+ fastens: true,
61
+ ...x,
62
+ pin: p,
63
+ fastener: p.pin.trim(),
64
+ ...(p.count != null || x.count != null ? { count: p.count ?? x.count } : {}),
65
+ };
48
66
  }
49
67
 
50
68
  /**
@@ -52,73 +70,174 @@ export function pinOfFit(x, project) {
52
70
  * @param {object} x the fit, with pin (pinOfFit) @param {object} s the built assembly @param {Function} f the findings' sink, as checkAssembly's
53
71
  */
54
72
  export function pinFit(x, s, f) {
55
- const label = x.note || x.label || `${x.a} to ${x.b}`, code = 'fit.pin', p = x.pin, refs = { a: x.a, b: x.b, pin: x.fastener };
56
- const fail = (text, short, more = {}) => f('fail', `${label}: ${text}`, short ?? `${label}: pins`, { code, refs, ...more });
73
+ const label = x.note || x.label || `${x.a} to ${x.b}`,
74
+ code = 'fit.pin',
75
+ p = x.pin,
76
+ refs = { a: x.a, b: x.b, pin: x.fastener };
77
+ const fail = (text, short, more = {}) =>
78
+ f('fail', `${label}: ${text}`, short ?? `${label}: pins`, { code, refs, ...more });
57
79
  const { F, material } = fixingsOf(s.project);
58
- const count = x.count != null ? x.num(x.count) : 1, load = p.load != null ? x.num(p.load) : null;
59
- if (!(Number.isInteger(count) && count >= 1)) return fail(`count is how many pins, a whole number from 1 (got ${count})`);
80
+ const count = x.count != null ? x.num(x.count) : 1,
81
+ load = p.load != null ? x.num(p.load) : null;
82
+ if (!(Number.isInteger(count) && count >= 1))
83
+ return fail(`count is how many pins, a whole number from 1 (got ${count})`);
60
84
  let m0;
61
- try { m0 = F.pinModel(x.fastener, { material, ...(p.fit ? { fit: p.fit } : {}) }); } catch (e) { return fail(e.message.replace(/^pinModel: /, '')); }
85
+ try {
86
+ m0 = F.pinModel(x.fastener, { material, ...(p.fit ? { fit: p.fit } : {}) });
87
+ } catch (e) {
88
+ return fail(e.message.replace(/^pinModel: /, ''));
89
+ }
62
90
  const named = (q, n) => q.name === n || q.leaf === n || q.item === n || q.name.startsWith(`${n}.`);
63
- const A = s.parts.filter((q) => !q.hardware && named(q, top(x.a))), B = s.parts.filter((q) => !q.hardware && named(q, top(x.b)));
91
+ const A = s.parts.filter((q) => !q.hardware && named(q, top(x.a))),
92
+ B = s.parts.filter((q) => !q.hardware && named(q, top(x.b)));
64
93
  if (!A.length || !B.length) return fail(`no part "${A.length ? x.b : x.a}"`, `${label}: no part`);
65
- const R = m0.diameter + 2, wanted = plainHoles(F, x.fastener, material, m0, p.fit);
94
+ const R = m0.diameter + 2,
95
+ wanted = plainHoles(F, x.fastener, material, m0, p.fit);
66
96
  // A part measured along a world axis: where its material and its holes lie along it, and the holes' size
67
97
  const run = (parts, O, D) => {
68
- const segs = parts.flatMap((q) => profile(q.triangles, O, D, R).segs).filter((g) => g.solid || g.hole).sort((a, b) => a.z0 - b.z0);
98
+ const segs = parts
99
+ .flatMap((q) => profile(q.triangles, O, D, R).segs)
100
+ .filter((g) => g.solid || g.hole)
101
+ .sort((a, b) => a.z0 - b.z0);
69
102
  if (!segs.length) return null;
70
- const holes = segs.filter((g) => g.hole), size = (g) => (g.a.loop?.shape === 'round' && g.a.loop.d != null ? Math.min(g.a.loop.d, g.b.loop?.d ?? Infinity) : 2 * Math.min(g.a.free, g.b.free));
71
- return { lo: segs[0].z0, hi: segs.at(-1).z1, segs, hole: holes.length ? Math.min(...holes.map(size)) : null, solidAt: segs.find((g) => g.solid) ?? null };
103
+ const holes = segs.filter((g) => g.hole),
104
+ size = (g) =>
105
+ g.a.loop?.shape === 'round' && g.a.loop.d != null
106
+ ? Math.min(g.a.loop.d, g.b.loop?.d ?? Infinity)
107
+ : 2 * Math.min(g.a.free, g.b.free);
108
+ return {
109
+ lo: segs[0].z0,
110
+ hi: segs.at(-1).z1,
111
+ segs,
112
+ hole: holes.length ? Math.min(...holes.map(size)) : null,
113
+ solidAt: segs.find((g) => g.solid) ?? null,
114
+ };
72
115
  };
73
116
  // The pins' places: round holes in a about the pin's hole (a teardrop lying on its side is one: holesIn leaves its
74
117
  // roof out of the round), on an axis that goes on into b
75
118
  const axes = [];
76
- for (const q of A) for (const h of holesIn(q)) {
77
- if (Math.abs(h.d - m0.hole.diameter) > LOOK && !wanted.some((w) => Math.abs(h.d - w.diameter) <= LOOK)) continue;
78
- const O = q.m ? vec3.transform([0, 0, 0], h.at, q.m) : h.at, e = q.m ? vec3.transform([0, 0, 0], h.at.map((c, i) => c + h.axis[i]), q.m) : h.at.map((c, i) => c + h.axis[i]), D = unit(e.map((c, i) => c - O[i]));
79
- const a = run(A, O, D), b = run(B, O, D);
80
- if (!a || !b) continue;
81
- const flip = (a.lo + a.hi) / 2 > (b.lo + b.hi) / 2; // (along from a into b)
82
- const Df = flip ? D.map((c) => -c) : D;
83
- axes.push({ at: O.map(r3), D: Df, a: flip ? run(A, O, Df) : a, b: flip ? run(B, O, Df) : b });
84
- }
85
- if (axes.length < count) return fail(`${count} × ${m0.name} wanted, and ${axes.length ? `only ${axes.length} hole${axes.length > 1 ? 's' : ''} about its ${mm(m0.hole.diameter)} mm hole ${axes.length > 1 ? 'lead' : 'leads'}` : `no hole about its ${mm(m0.hole.diameter)} mm hole leads`} from ${x.a} into ${x.b}: draw the holes with v.fixings.pinHole`, `${label}: holes missing`, { values: { count, found: axes.length } });
119
+ for (const q of A)
120
+ for (const h of holesIn(q)) {
121
+ if (Math.abs(h.d - m0.hole.diameter) > LOOK && !wanted.some((w) => Math.abs(h.d - w.diameter) <= LOOK)) continue;
122
+ const O = q.m ? vec3.transform([0, 0, 0], h.at, q.m) : h.at,
123
+ e = q.m
124
+ ? vec3.transform(
125
+ [0, 0, 0],
126
+ h.at.map((c, i) => c + h.axis[i]),
127
+ q.m,
128
+ )
129
+ : h.at.map((c, i) => c + h.axis[i]),
130
+ D = unit(e.map((c, i) => c - O[i]));
131
+ const a = run(A, O, D),
132
+ b = run(B, O, D);
133
+ if (!a || !b) continue;
134
+ const flip = (a.lo + a.hi) / 2 > (b.lo + b.hi) / 2; // (along from a into b)
135
+ const Df = flip ? D.map((c) => -c) : D;
136
+ axes.push({ at: O.map(r3), D: Df, a: flip ? run(A, O, Df) : a, b: flip ? run(B, O, Df) : b });
137
+ }
138
+ if (axes.length < count)
139
+ return fail(
140
+ `${count} × ${m0.name} wanted, and ${axes.length ? `only ${axes.length} hole${axes.length > 1 ? 's' : ''} about its ${mm(m0.hole.diameter)} mm hole ${axes.length > 1 ? 'lead' : 'leads'}` : `no hole about its ${mm(m0.hole.diameter)} mm hole leads`} from ${x.a} into ${x.b}: draw the holes with v.fixings.pinHole`,
141
+ `${label}: holes missing`,
142
+ { values: { count, found: axes.length } },
143
+ );
86
144
  let bad = 0;
87
145
  const worst = { margin: Infinity, m: null };
88
146
  for (const [i, ax] of axes.entries()) {
89
- const where = `pin ${i + 1} at ${ax.at.map(mm).join(', ')}`, at = ax.at;
147
+ const where = `pin ${i + 1} at ${ax.at.map(mm).join(', ')}`,
148
+ at = ax.at;
90
149
  // the holes: each part's held to the pin's
91
- for (const [part, r] of [[x.a, ax.a], [x.b, ax.b]]) {
150
+ for (const [part, r] of [
151
+ [x.a, ax.a],
152
+ [x.b, ax.b],
153
+ ]) {
92
154
  const d = r.hole;
93
- if (d == null) { bad++; fail(`${where}: ${part} is solid on the pin's axis: it has no hole there for ${m0.name}`, `${label}: no hole in ${part}`, { at, values: { part } }); continue; }
155
+ if (d == null) {
156
+ bad++;
157
+ fail(
158
+ `${where}: ${part} is solid on the pin's axis: it has no hole there for ${m0.name}`,
159
+ `${label}: no hole in ${part}`,
160
+ { at, values: { part } },
161
+ );
162
+ continue;
163
+ }
94
164
  const hit = wanted.find((w) => Math.abs(d - w.diameter) <= w.tol);
95
165
  if (hit) continue;
96
- const w = m0.snap ? m0.hole : wanted.find((v) => v.fit === (p.fit ?? 'press')), small = d < Math.min(...(m0.snap ? [m0.hole.diameter] : wanted.map((v) => v.diameter)));
97
- bad++; fail(`${where}: ${part}'s hole is drawn ${mm(r3(d))} mm, and ${m0.name} wants ${m0.snap ? `${mm(m0.hole.diameter)} mm (${m0.hole.rules.join(' and ')})` : wanted.map((v) => `${mm(v.diameter)} mm for a ${v.fit} fit (${v.rule})`).join(' or ')}: ${small ? 'too small, it will not go in' : 'too big, it will be loose'}`,
98
- `${label}: hole ${small ? 'small' : 'big'} in ${part}`, { at, values: { part, drawn: r3(d), wanted: w.diameter } });
166
+ const w = m0.snap ? m0.hole : wanted.find((v) => v.fit === (p.fit ?? 'press')),
167
+ small = d < Math.min(...(m0.snap ? [m0.hole.diameter] : wanted.map((v) => v.diameter)));
168
+ bad++;
169
+ fail(
170
+ `${where}: ${part}'s hole is drawn ${mm(r3(d))} mm, and ${m0.name} wants ${m0.snap ? `${mm(m0.hole.diameter)} mm (${m0.hole.rules.join(' and ')})` : wanted.map((v) => `${mm(v.diameter)} mm for a ${v.fit} fit (${v.rule})`).join(' or ')}: ${small ? 'too small, it will not go in' : 'too big, it will be loose'}`,
171
+ `${label}: hole ${small ? 'small' : 'big'} in ${part}`,
172
+ { at, values: { part, drawn: r3(d), wanted: w.diameter } },
173
+ );
99
174
  }
100
175
  // the length: the room from a's outer face to where b closes (a blind hole's floor) or ends
101
- const end = ax.b.solidAt ? ax.b.solidAt.z0 : ax.b.hi, room = r3(end - ax.a.lo);
102
- if (m0.snap && ax.b.solidAt) { bad++; fail(`${where}: ${x.b}'s hole is blind ${mm(r3(ax.b.solidAt.z0 - ax.b.lo))} mm deep: a snap pin's barb clicks out past the far face, so its socket goes through`, `${label}: socket blind`, { at }); continue; }
176
+ const end = ax.b.solidAt ? ax.b.solidAt.z0 : ax.b.hi,
177
+ room = r3(end - ax.a.lo);
178
+ if (m0.snap && ax.b.solidAt) {
179
+ bad++;
180
+ fail(
181
+ `${where}: ${x.b}'s hole is blind ${mm(r3(ax.b.solidAt.z0 - ax.b.lo))} mm deep: a snap pin's barb clicks out past the far face, so its socket goes through`,
182
+ `${label}: socket blind`,
183
+ { at },
184
+ );
185
+ continue;
186
+ }
103
187
  const grip = r3(Math.max(0.1, Math.min(ax.a.hi - ax.a.lo, end - ax.b.lo)));
104
188
  let m;
105
- try { m = F.pinModel(x.fastener, { material, thickness: room, grip, ...(p.fit ? { fit: p.fit } : {}), ...(load != null ? { load: load / count } : {}), ...(p.pull != null ? { pull: x.num(p.pull) / count } : {}) }); }
106
- catch (e) { bad++; fail(`${where} in ${x.a} and ${x.b}: ${e.message.replace(/^pinModel: /, '')}`, `${label}: ${/too long/.test(e.message) ? 'pin too long' : /too short/.test(e.message) ? 'pin too short' : 'pin'}`, { at, values: { length: m0.length, room } }); continue; }
189
+ try {
190
+ m = F.pinModel(x.fastener, {
191
+ material,
192
+ thickness: room,
193
+ grip,
194
+ ...(p.fit ? { fit: p.fit } : {}),
195
+ ...(load != null ? { load: load / count } : {}),
196
+ ...(p.pull != null ? { pull: x.num(p.pull) / count } : {}),
197
+ });
198
+ } catch (e) {
199
+ bad++;
200
+ fail(
201
+ `${where} in ${x.a} and ${x.b}: ${e.message.replace(/^pinModel: /, '')}`,
202
+ `${label}: ${/too long/.test(e.message) ? 'pin too long' : /too short/.test(e.message) ? 'pin too short' : 'pin'}`,
203
+ { at, values: { length: m0.length, room } },
204
+ );
205
+ continue;
206
+ }
107
207
  const s1 = m.findings.find((g) => g.code === 'pin.strength');
108
208
  if (m.margin != null && m.margin < worst.margin) Object.assign(worst, { margin: m.margin, m, s1, where });
109
209
  if (load == null && i === 0) worst.s1 ??= s1;
110
210
  }
111
211
  if (bad) return;
112
- const say = m0.snap ? `in sockets drawn ${mm(m0.hole.diameter)} mm (${m0.hole.rules.join(', ')})` : `in holes drawn to ${wanted.map((v) => `${v.fit} ${mm(v.diameter)} mm`).join(' or ')}`;
113
- f('pass', `${label}: ${count} × ${m0.name} ${say}, each the right length for ${x.a} and ${x.b}`, null, { code, refs, values: { count, hole: m0.hole.diameter, grams: m0.grams } });
114
- if (worst.m) f(worst.s1.level, `${label}: ${worst.where}: ${worst.s1.text}`, worst.s1.level === 'pass' ? null : `${label}: pins weak`, { code: 'fit.pin.load', refs, values: worst.s1.values });
115
- else if (worst.s1) f('info', `${label}: ${worst.s1.text}`, null, { code: 'fit.pin.load', refs, values: worst.s1.values });
212
+ const say = m0.snap
213
+ ? `in sockets drawn ${mm(m0.hole.diameter)} mm (${m0.hole.rules.join(', ')})`
214
+ : `in holes drawn to ${wanted.map((v) => `${v.fit} ${mm(v.diameter)} mm`).join(' or ')}`;
215
+ f('pass', `${label}: ${count} × ${m0.name} ${say}, each the right length for ${x.a} and ${x.b}`, null, {
216
+ code,
217
+ refs,
218
+ values: { count, hole: m0.hole.diameter, grams: m0.grams },
219
+ });
220
+ if (worst.m)
221
+ f(
222
+ worst.s1.level,
223
+ `${label}: ${worst.where}: ${worst.s1.text}`,
224
+ worst.s1.level === 'pass' ? null : `${label}: pins weak`,
225
+ { code: 'fit.pin.load', refs, values: worst.s1.values },
226
+ );
227
+ else if (worst.s1)
228
+ f('info', `${label}: ${worst.s1.text}`, null, { code: 'fit.pin.load', refs, values: worst.s1.values });
116
229
  }
117
230
  // The holes a plain pin may sit in: press and slip, or the one fit asked; a snap pin's is its socket's. Each carries
118
231
  // the tolerance it is matched to (tolOf)
119
232
  function plainHoles(F, name, material, m0, fit) {
120
- const all = m0.snap ? [{ fit: 'snap', diameter: m0.hole.diameter, rule: m0.hole.rules.join(', ') }]
121
- : ['press', 'slip'].map((k) => { const h = F.pinModel(name, { material, fit: k }).hole; return { fit: k, diameter: h.diameter, rule: h.rules.join(', ') }; });
233
+ const all = m0.snap
234
+ ? [{ fit: 'snap', diameter: m0.hole.diameter, rule: m0.hole.rules.join(', ') }]
235
+ : ['press', 'slip'].map((k) => {
236
+ const h = F.pinModel(name, { material, fit: k }).hole;
237
+ return { fit: k, diameter: h.diameter, rule: h.rules.join(', ') };
238
+ });
122
239
  // each hole's tolerance from its step to the pin's bare diameter and to the other fit's hole, whichever fit is asked
123
- return all.map((w) => ({ ...w, tol: tolOf(w.diameter, [m0.diameter, ...all.filter((o) => o !== w).map((o) => o.diameter)]) })).filter((w) => !fit || m0.snap || w.fit === fit);
240
+ return all
241
+ .map((w) => ({ ...w, tol: tolOf(w.diameter, [m0.diameter, ...all.filter((o) => o !== w).map((o) => o.diameter)]) }))
242
+ .filter((w) => !fit || m0.snap || w.fit === fit);
124
243
  }