@rulecad/cad 0.8.0 → 0.10.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 (102) hide show
  1. package/AGENTS.md +45 -16
  2. package/CHANGELOG.md +724 -0
  3. package/EXAMPLES.md +61 -54
  4. package/README.md +376 -98
  5. package/TUTORIAL.md +2 -2
  6. package/bin/cad.mjs +2664 -541
  7. package/lib/animate.mjs +173 -49
  8. package/lib/assembly.mjs +1883 -435
  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/cache-file.mjs +105 -0
  15. package/lib/carried.mjs +201 -0
  16. package/lib/clone.mjs +194 -42
  17. package/lib/code-text.mjs +28 -0
  18. package/lib/design-imports.mjs +127 -0
  19. package/lib/design.mjs +871 -147
  20. package/lib/designshapes.mjs +142 -0
  21. package/lib/drive.mjs +187 -63
  22. package/lib/edit.mjs +922 -192
  23. package/lib/errors.mjs +16 -3
  24. package/lib/estimates.mjs +51 -0
  25. package/lib/explain.mjs +62 -13
  26. package/lib/fasten.mjs +2708 -507
  27. package/lib/fe-key.mjs +29 -0
  28. package/lib/fittings.mjs +721 -122
  29. package/lib/fixings.mjs +38 -13
  30. package/lib/framebuild.mjs +790 -175
  31. package/lib/framecheck.mjs +1901 -368
  32. package/lib/framegen.mjs +108 -22
  33. package/lib/frameheat.mjs +225 -42
  34. package/lib/frameprint.mjs +513 -108
  35. package/lib/frames.mjs +86 -18
  36. package/lib/framesize.mjs +719 -120
  37. package/lib/frametip.mjs +235 -0
  38. package/lib/gait.mjs +135 -23
  39. package/lib/grip.mjs +62 -13
  40. package/lib/guides.mjs +1403 -230
  41. package/lib/host.mjs +129 -23
  42. package/lib/inplace.mjs +171 -36
  43. package/lib/jigs.mjs +360 -78
  44. package/lib/joins.mjs +697 -118
  45. package/lib/jsonedit.mjs +88 -26
  46. package/lib/kept.mjs +84 -0
  47. package/lib/kit.mjs +439 -69
  48. package/lib/layout.mjs +2 -1
  49. package/lib/links.mjs +149 -42
  50. package/lib/list.mjs +334 -64
  51. package/lib/live-guard.mjs +68 -0
  52. package/lib/live-shape.mjs +96 -24
  53. package/lib/live.mjs +189 -59
  54. package/lib/loads.mjs +597 -136
  55. package/lib/machine.mjs +372 -74
  56. package/lib/magnets.mjs +162 -44
  57. package/lib/measured.mjs +1080 -117
  58. package/lib/mesh.mjs +526 -119
  59. package/lib/meshcmd.mjs +215 -60
  60. package/lib/motors.mjs +1002 -214
  61. package/lib/motorsvg.mjs +114 -32
  62. package/lib/names.mjs +18 -1
  63. package/lib/optimise.mjs +991 -287
  64. package/lib/order.mjs +515 -114
  65. package/lib/panels.mjs +482 -94
  66. package/lib/parts.mjs +81 -23
  67. package/lib/pins.mjs +163 -44
  68. package/lib/plates.mjs +223 -35
  69. package/lib/player.mjs +37 -9
  70. package/lib/power.mjs +64 -13
  71. package/lib/preview.mjs +83 -16
  72. package/lib/profilemeasure.mjs +572 -129
  73. package/lib/rules.mjs +17 -6
  74. package/lib/sandbox-hooks.mjs +179 -27
  75. package/lib/sandbox-lock.mjs +225 -0
  76. package/lib/sandbox-preload.mjs +2 -2
  77. package/lib/sandbox-run.mjs +122 -25
  78. package/lib/sandbox.mjs +667 -75
  79. package/lib/schema.mjs +42 -11
  80. package/lib/seats.mjs +227 -49
  81. package/lib/services.mjs +261 -52
  82. package/lib/sheet.mjs +963 -179
  83. package/lib/slotnuts.mjs +213 -34
  84. package/lib/targets/gist.mjs +4 -1
  85. package/lib/targets.mjs +22 -4
  86. package/lib/task.mjs +376 -79
  87. package/lib/versions.mjs +285 -39
  88. package/lib/wires.mjs +228 -45
  89. package/lib/workspace.mjs +200 -14
  90. package/package.json +19 -19
  91. package/schemas/assembly.schema.json +2 -2
  92. package/schemas/project.schema.json +6 -0
  93. package/templates/coupons/MEASURE.md +109 -0
  94. package/templates/coupons/assemblies/fixings-test.json +20 -0
  95. package/templates/coupons/assemblies/slot-test.json +11 -0
  96. package/templates/coupons/objects/fixings-coupon.js +88 -0
  97. package/templates/coupons/objects/fixings-coupon.json +26 -0
  98. package/templates/coupons/objects/holds-coupon.js +130 -0
  99. package/templates/coupons/objects/holds-coupon.json +42 -0
  100. package/templates/coupons/objects/slot-coupon.js +89 -0
  101. package/templates/coupons/objects/slot-coupon.json +29 -0
  102. package/templates/coupons/project.json +10 -0
package/lib/schema.mjs CHANGED
@@ -15,7 +15,8 @@ export function schema(name) {
15
15
  }
16
16
 
17
17
  const typeOf = (v) => (v === null ? 'null' : Array.isArray(v) ? 'array' : Number.isInteger(v) ? 'integer' : typeof v);
18
- const fits = (v, t) => t === typeOf(v) || (t === 'number' && typeof v === 'number') || (t === 'integer' && Number.isInteger(v));
18
+ const fits = (v, t) =>
19
+ t === typeOf(v) || (t === 'number' && typeof v === 'number') || (t === 'integer' && Number.isInteger(v));
19
20
 
20
21
  /**
21
22
  * Problems with a value against a schema: [{ path, message }], empty when it's valid.
@@ -23,8 +24,19 @@ const fits = (v, t) => t === typeOf(v) || (t === 'number' && typeof v === 'numbe
23
24
  * @returns {{ path: string, message: string }[]}
24
25
  */
25
26
  export function validate(value, s, root = s, at = '') {
26
- const out = [], here = at || '(top)', p = (k) => (at ? `${at}.${k}` : String(k));
27
- if (s.$ref) return validate(value, s.$ref.replace('#/$defs/', '').split('/').reduce((o, k) => o[k], root.$defs), root, at);
27
+ const out = [],
28
+ here = at || '(top)',
29
+ p = (k) => (at ? `${at}.${k}` : String(k));
30
+ if (s.$ref)
31
+ return validate(
32
+ value,
33
+ s.$ref
34
+ .replace('#/$defs/', '')
35
+ .split('/')
36
+ .reduce((o, k) => o[k], root.$defs),
37
+ root,
38
+ at,
39
+ );
28
40
  if (s.anyOf) {
29
41
  const each = s.anyOf.map((x) => validate(value, x, root, at));
30
42
  // Report the alternative that got furthest (the fewest problems), so a near miss says what's wrong with it
@@ -34,23 +46,37 @@ export function validate(value, s, root = s, at = '') {
34
46
  }
35
47
  if (s.type) {
36
48
  const types = [].concat(s.type);
37
- if (!types.some((t) => fits(value, t))) return [{ path: here, message: `should be ${types.join(' or ')}, not ${typeOf(value)}` }];
49
+ if (!types.some((t) => fits(value, t)))
50
+ return [{ path: here, message: `should be ${types.join(' or ')}, not ${typeOf(value)}` }];
38
51
  }
39
- if (s.enum && !s.enum.includes(value)) out.push({ path: here, message: `should be one of ${s.enum.map((x) => JSON.stringify(x)).join(', ')} (got ${JSON.stringify(value)})` });
52
+ if (s.enum && !s.enum.includes(value))
53
+ out.push({
54
+ path: here,
55
+ message: `should be one of ${s.enum.map((x) => JSON.stringify(x)).join(', ')} (got ${JSON.stringify(value)})`,
56
+ });
40
57
  // (a pattern with a description says what it wants in words; one without, the pattern itself)
41
- if (s.pattern && typeof value === 'string' && !new RegExp(s.pattern).test(value)) out.push({ path: here, message: `"${value}" isn't in the expected form: ${s.description || s.pattern}` });
58
+ if (s.pattern && typeof value === 'string' && !new RegExp(s.pattern).test(value))
59
+ out.push({ path: here, message: `"${value}" isn't in the expected form: ${s.description || s.pattern}` });
42
60
  if (typeOf(value) === 'object') {
43
61
  for (const k of s.required || []) if (!(k in value)) out.push({ path: here, message: `needs "${k}"` });
44
62
  const props = s.properties || {};
45
63
  for (const [k, v] of Object.entries(value)) {
46
64
  if (props[k]) out.push(...validate(v, props[k], root, p(k)));
47
- else if (s.additionalProperties === false) { const guess = near(k, Object.keys(props)); out.push({ path: p(k), message: `not allowed here${guess ? ` (did you mean "${guess}"?)` : ` (allowed: ${Object.keys(props).join(', ')})`}` }); }
48
- else if (s.additionalProperties && typeof s.additionalProperties === 'object') out.push(...validate(v, s.additionalProperties, root, p(k)));
65
+ else if (s.additionalProperties === false) {
66
+ const guess = near(k, Object.keys(props));
67
+ out.push({
68
+ path: p(k),
69
+ message: `not allowed here${guess ? ` (did you mean "${guess}"?)` : ` (allowed: ${Object.keys(props).join(', ')})`}`,
70
+ });
71
+ } else if (s.additionalProperties && typeof s.additionalProperties === 'object')
72
+ out.push(...validate(v, s.additionalProperties, root, p(k)));
49
73
  }
50
74
  }
51
75
  if (Array.isArray(value)) {
52
- if (s.minItems != null && value.length < s.minItems) out.push({ path: here, message: `needs at least ${s.minItems} entries` });
53
- if (s.maxItems != null && value.length > s.maxItems) out.push({ path: here, message: `takes at most ${s.maxItems} entries` });
76
+ if (s.minItems != null && value.length < s.minItems)
77
+ out.push({ path: here, message: `needs at least ${s.minItems} entries` });
78
+ if (s.maxItems != null && value.length > s.maxItems)
79
+ out.push({ path: here, message: `takes at most ${s.maxItems} entries` });
54
80
  value.forEach((v, i) => {
55
81
  const item = s.prefixItems?.[i] || s.items;
56
82
  if (item) out.push(...validate(v, item, root, `${at}[${i}]`));
@@ -61,5 +87,10 @@ export function validate(value, s, root = s, at = '') {
61
87
 
62
88
  /** Problems with a file's JSON as findings (code schema), for cad check. @param {any} json @param {'object' | 'assembly' | 'project' | 'rules' | 'layout' | 'problem'} kind @param {string} file */
63
89
  export function schemaFindings(json, kind, file) {
64
- return validate(json, schema(kind)).map((x) => ({ level: 'fail', text: `${file}: ${x.path}: ${x.message}`, code: 'schema', refs: { file, path: x.path } }));
90
+ return validate(json, schema(kind)).map((x) => ({
91
+ level: 'fail',
92
+ text: `${file}: ${x.path}: ${x.message}`,
93
+ code: 'schema',
94
+ refs: { file, path: x.path },
95
+ }));
65
96
  }
package/lib/seats.mjs CHANGED
@@ -4,125 +4,303 @@ import { createRequire } from 'node:module';
4
4
 
5
5
  const require = createRequire(import.meta.url);
6
6
  const n3 = (x, d = 3) => String(Number(x.toFixed(d)));
7
- const sub = (a, b) => [a[0] - b[0], a[1] - b[1], a[2] - b[2]], dot = (a, b) => a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
7
+ const sub = (a, b) => [a[0] - b[0], a[1] - b[1], a[2] - b[2]],
8
+ dot = (a, b) => a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
8
9
  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]];
9
- const unit = (v) => { const l = Math.hypot(...v); return v.map((c) => c / l); };
10
+ const unit = (v) => {
11
+ const l = Math.hypot(...v);
12
+ return v.map((c) => c / l);
13
+ };
10
14
 
11
15
  // Coordinates about an axis: z along it from O, x and y across it
12
16
  export function axisFrame(O, Z) {
13
- const z = unit(Z), seed = Math.abs(z[0]) < 0.9 ? [1, 0, 0] : [0, 1, 0], x = unit(cross(seed, z)), y = cross(z, x);
14
- return (p) => { const q = sub(p, O); return [dot(q, x), dot(q, y), dot(q, z)]; };
17
+ const z = unit(Z),
18
+ seed = Math.abs(z[0]) < 0.9 ? [1, 0, 0] : [0, 1, 0],
19
+ x = unit(cross(seed, z)),
20
+ y = cross(z, x);
21
+ return (p) => {
22
+ const q = sub(p, O);
23
+ return [dot(q, x), dot(q, y), dot(q, z)];
24
+ };
15
25
  }
16
26
  // A slice across the axis at z, as 2D segments
17
27
  export function sliceAt(tris, z) {
18
28
  const segs = [];
19
29
  for (const t of tris) {
20
30
  const c = [];
21
- for (let i = 0; i < 3; i++) { const a = t[i], b = t[(i + 1) % 3]; if ((a[2] - z) * (b[2] - z) < 0) { 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])]); } }
31
+ for (let i = 0; i < 3; i++) {
32
+ const a = t[i],
33
+ b = t[(i + 1) % 3];
34
+ if ((a[2] - z) * (b[2] - z) < 0) {
35
+ const u = (z - a[2]) / (b[2] - a[2]);
36
+ c.push([a[0] + u * (b[0] - a[0]), a[1] + u * (b[1] - a[1])]);
37
+ }
38
+ }
22
39
  if (c.length === 2) segs.push(c);
23
40
  }
24
41
  return segs;
25
42
  }
26
43
  // How far out from the axis a ray crosses the slice's walls
27
44
  export function rayHits(segs, ang) {
28
- const dx = Math.cos(ang), dy = Math.sin(ang), out = [];
45
+ const dx = Math.cos(ang),
46
+ dy = Math.sin(ang),
47
+ out = [];
29
48
  for (const [p, q] of segs) {
30
- const ex = q[0] - p[0], ey = q[1] - p[1], den = dx * ey - dy * ex;
49
+ const ex = q[0] - p[0],
50
+ ey = q[1] - p[1],
51
+ den = dx * ey - dy * ex;
31
52
  if (Math.abs(den) < 1e-12) continue;
32
- const t = (p[0] * ey - p[1] * ex) / den, u = (p[0] * dy - p[1] * dx) / den;
53
+ const t = (p[0] * ey - p[1] * ex) / den,
54
+ u = (p[0] * dy - p[1] * dx) / den;
33
55
  if (t > 1e-6 && u >= 0 && u < 1) out.push(t);
34
56
  }
35
57
  return out.sort((a, b) => a - b);
36
58
  }
37
- const RAYS = 48, FACETS = 0.02; // mm: what a round drawn in facets is short of its true size
59
+ const RAYS = 48,
60
+ FACETS = 0.02; // mm: what a round drawn in facets is short of its true size
38
61
  // At one slice: is the axis in solid (a pin) or in a space (a bore), and the first wall out, each way round
39
62
  function round(segs) {
40
63
  if (segs.length < 3) return null;
41
- let solid = 0, bore = 0; const first = [];
42
- for (let k = 0; k < RAYS; k++) { const h = rayHits(segs, ((k + 0.37) * 2 * Math.PI) / RAYS); if (!h.length) return null; (h.length % 2 ? solid++ : bore++); first.push(h[0]); }
64
+ let solid = 0,
65
+ bore = 0;
66
+ const first = [];
67
+ for (let k = 0; k < RAYS; k++) {
68
+ const h = rayHits(segs, ((k + 0.37) * 2 * Math.PI) / RAYS);
69
+ if (!h.length) return null;
70
+ h.length % 2 ? solid++ : bore++;
71
+ first.push(h[0]);
72
+ }
43
73
  return { pin: solid > bore, first };
44
74
  }
45
- const span = (tris, r) => { let lo = Infinity, hi = -Infinity; for (const t of tris) for (const p of t) if (Math.hypot(p[0], p[1]) < r) { lo = Math.min(lo, p[2]); hi = Math.max(hi, p[2]); } return [lo, hi]; };
75
+ const span = (tris, r) => {
76
+ let lo = Infinity,
77
+ hi = -Infinity;
78
+ for (const t of tris)
79
+ for (const p of t)
80
+ if (Math.hypot(p[0], p[1]) < r) {
81
+ lo = Math.min(lo, p[2]);
82
+ hi = Math.max(hi, p[2]);
83
+ }
84
+ return [lo, hi];
85
+ };
46
86
 
47
87
  // The running gap of a revolute: where one side is a pin and the other a bore round it, the least gap each side as drawn
48
88
  // (over a few slices along the length they share) and how far along it runs. null: nothing there turns on a pin
49
89
  export function runningGap(aTris, bTris) {
50
- const reach = 40, [la, ha] = span(aTris, reach), [lb, hb] = span(bTris, reach), lo = Math.max(la, lb), hi = Math.min(ha, hb);
90
+ const reach = 40,
91
+ [la, ha] = span(aTris, reach),
92
+ [lb, hb] = span(bTris, reach),
93
+ lo = Math.max(la, lb),
94
+ hi = Math.min(ha, hb);
51
95
  if (!(hi - lo > 0.2)) return null;
52
- let least = null, along = 0, step = (hi - lo) / 8;
96
+ let least = null,
97
+ along = 0,
98
+ step = (hi - lo) / 8;
53
99
  for (let k = 0; k < 8; k++) {
54
- const z = lo + step * (k + 0.5), A = round(sliceAt(aTris, z)), B = round(sliceAt(bTris, z));
100
+ const z = lo + step * (k + 0.5),
101
+ A = round(sliceAt(aTris, z)),
102
+ B = round(sliceAt(bTris, z));
55
103
  if (!A || !B || A.pin === B.pin) continue;
56
- const pin = A.pin ? A : B, bore = A.pin ? B : A;
104
+ const pin = A.pin ? A : B,
105
+ bore = A.pin ? B : A;
57
106
  // A real pin is round about the axis: the widest and narrowest of it agree
58
107
  if (Math.max(...pin.first) - Math.min(...pin.first) > 0.5) continue;
59
108
  const gap = Math.min(...bore.first.map((r, i) => r - pin.first[i]));
60
109
  if (gap > 2) continue; // room, not a running fit
61
110
  along += step;
62
- if (least == null || gap < least.gap) least = { gap, z, pin: Math.max(...pin.first), bore: Math.min(...bore.first), pinOn: A.pin ? 'a' : 'b' };
111
+ if (least == null || gap < least.gap)
112
+ least = { gap, z, pin: Math.max(...pin.first), bore: Math.min(...bore.first), pinOn: A.pin ? 'a' : 'b' };
63
113
  }
64
114
  return least && { ...least, length: along };
65
115
  }
66
116
 
67
117
  // What the project prints on, for the fits to hold a gap against: a rule set the assembly reads, or the kit's default
68
118
  export function printerRules(s) {
69
- const pick = (name) => { try { return Object.fromEntries(Object.entries(require(`@rulecad/rules/${name}.json`).rules).map(([k, r]) => [k, r.value])); } catch { return null; } };
119
+ const pick = (name) => {
120
+ try {
121
+ return Object.fromEntries(
122
+ Object.entries(require(`@rulecad/rules/${name}.json`).rules).map(([k, r]) => [k, r.value]),
123
+ );
124
+ } catch {
125
+ return null;
126
+ }
127
+ };
70
128
  return { ...pick('x1c'), ...(s.values?.x1c && typeof s.values.x1c === 'object' ? s.values.x1c : {}) };
71
129
  }
72
130
 
73
131
  // joint.running: for each revolute, the gap where its child turns on a pin or in a bore. A printed bore comes out
74
132
  // hole_comp smaller across, so half of that comes off the gap each side; what's left is held against the printer's fit
75
133
  export function runningChecks(s, f, { axisOf, placeItems, jointValues, mul }) {
76
- const motion = s.motion; if (!motion?.joints?.length) return;
77
- const worlds = placeItems(motion, s.jointValues ?? jointValues(motion, motion.pose ?? undefined)), ids = motion.items.map((x) => x[0]);
78
- const P = printerRules(s), fit = P.fit ?? 0.2, comp = P.hole_comp ?? 0.1;
134
+ const motion = s.motion;
135
+ if (!motion?.joints?.length) return;
136
+ const worlds = placeItems(motion, s.jointValues ?? jointValues(motion, motion.pose ?? undefined)),
137
+ ids = motion.items.map((x) => x[0]);
138
+ const P = printerRules(s),
139
+ fit = P.fit ?? 0.2,
140
+ comp = P.hole_comp ?? 0.1;
79
141
  const refs = new Set(s.objects.filter((o) => o.role === 'reference').map((o) => String(o.id).split('.')[0]));
80
142
  const partsOf = (id) => s.parts.filter((p) => p.item === id || p.leaf === id);
81
143
  for (const j of motion.joints) {
82
144
  if (j.type !== 'revolute' || j.parent < 0) continue;
83
- const a = ids[j.parent], b = ids[j.child];
145
+ const a = ids[j.parent],
146
+ b = ids[j.child];
84
147
  if (refs.has(a) || refs.has(b)) continue; // a bought part's seat is fit.seat's business
85
- const pa = partsOf(a), pb = partsOf(b);
148
+ const pa = partsOf(a),
149
+ pb = partsOf(b);
86
150
  if (!pa.length || !pb.length) continue;
87
- const { O, Z } = axisOf(motion, worlds, j, mul), to = axisFrame(O, Z), g = runningGap(pa.flatMap((p) => p.triangles).map((t) => t.map(to)), pb.flatMap((p) => p.triangles).map((t) => t.map(to)));
151
+ const { O, Z } = axisOf(motion, worlds, j, mul),
152
+ to = axisFrame(O, Z),
153
+ g = runningGap(
154
+ pa.flatMap((p) => p.triangles).map((t) => t.map(to)),
155
+ pb.flatMap((p) => p.triangles).map((t) => t.map(to)),
156
+ );
88
157
  if (!g) continue;
89
- const printed = g.gap - comp / 2, at = O.map((c, i) => c + unit(Z)[i] * g.z), pin = g.pinOn === 'a' ? a : b, bore = g.pinOn === 'a' ? b : a, refsOut = { joint: j.id, pin, bore };
90
- const values = { drawn: g.gap, printed, fit, length: g.length }, words = `${bore} on ${pin}'s ${n3(g.pin * 2, 2)} mm pin: ${n3(g.gap)} mm each side as drawn, about ${n3(printed)} mm printed`;
91
- if (printed <= 0) f('fail', `${j.id}: ${words} (a printed bore comes out ${comp} mm small across): it won't turn. The bore wants ${n3(2 * (g.pin + fit) + comp, 2)} mm`, `${j.id}: no running gap`, { code: 'joint.running', at, refs: refsOut, values });
92
- else if (printed < fit - FACETS) f('warn', `${j.id}: ${words}, under the printer's ${fit} mm fit: expect it to bind. The bore wants ${n3(2 * (g.pin + fit) + comp, 2)} mm`, `${j.id}: tight on its pin`, { code: 'joint.running', at, refs: refsOut, values });
158
+ const printed = g.gap - comp / 2,
159
+ at = O.map((c, i) => c + unit(Z)[i] * g.z),
160
+ pin = g.pinOn === 'a' ? a : b,
161
+ bore = g.pinOn === 'a' ? b : a,
162
+ refsOut = { joint: j.id, pin, bore };
163
+ const values = { drawn: g.gap, printed, fit, length: g.length },
164
+ words = `${bore} on ${pin}'s ${n3(g.pin * 2, 2)} mm pin: ${n3(g.gap)} mm each side as drawn, about ${n3(printed)} mm printed`;
165
+ if (printed <= 0)
166
+ f(
167
+ 'fail',
168
+ `${j.id}: ${words} (a printed bore comes out ${comp} mm small across): it won't turn. The bore wants ${n3(2 * (g.pin + fit) + comp, 2)} mm`,
169
+ `${j.id}: no running gap`,
170
+ { code: 'joint.running', at, refs: refsOut, values },
171
+ );
172
+ else if (printed < fit - FACETS)
173
+ f(
174
+ 'warn',
175
+ `${j.id}: ${words}, under the printer's ${fit} mm fit: expect it to bind. The bore wants ${n3(2 * (g.pin + fit) + comp, 2)} mm`,
176
+ `${j.id}: tight on its pin`,
177
+ { code: 'joint.running', at, refs: refsOut, values },
178
+ );
93
179
  else f('pass', `${j.id}: ${words}`, null, { code: 'joint.running', at, refs: refsOut, values });
94
180
  }
95
181
  }
96
182
 
97
183
  // fit.seat: a printed pocket against the bought bearing in it, read from the part as drawn, so a pocket drawn by hand is
98
184
  // caught as well as one from bearingSeat. { "a": "b1", "b": "housing", "seat": "press" | "slip" | clearance in mm }
99
- export function seatFit(x, s, f, { placeItems, jointValues, mul, seatSize }) {
100
- const top = (n) => String(n).split(/[.@]/)[0], o = s.objects.find((y) => y.bought?.part === 'bearing' && (y.id === top(x.a) || y.id === top(x.b))), label = x.note || `${x.a} in ${x.b}`, code = 'fit.seat';
101
- if (!o) return f('fail', `${label}: a seat holds a bought bearing, and neither ${x.a} nor ${x.b} is one`, `${label}: no bearing`, { code });
102
- const other = top(x.a) === o.id ? top(x.b) : top(x.a), parts = s.parts.filter((p) => p.item === other || p.leaf === other);
185
+ export function seatFit(x, s, f, { placeItems, jointValues, seatSize }) {
186
+ const top = (n) => String(n).split(/[.@]/)[0],
187
+ o = s.objects.find((y) => y.bought?.part === 'bearing' && (y.id === top(x.a) || y.id === top(x.b))),
188
+ label = x.note || `${x.a} in ${x.b}`,
189
+ code = 'fit.seat';
190
+ if (!o)
191
+ return f(
192
+ 'fail',
193
+ `${label}: a seat holds a bought bearing, and neither ${x.a} nor ${x.b} is one`,
194
+ `${label}: no bearing`,
195
+ { code },
196
+ );
197
+ const other = top(x.a) === o.id ? top(x.b) : top(x.a),
198
+ parts = s.parts.filter((p) => p.item === other || p.leaf === other);
103
199
  if (!parts.length) return f('fail', `${label}: no part "${other}"`, null, { code });
104
- const motion = s.motion, ids = motion.items.map((y) => y[0]), W = placeItems(motion, s.jointValues ?? jointValues(motion, motion.pose ?? undefined))[ids.indexOf(o.id)];
105
- const inv = (p) => { const q = [p[0] - W[3], p[1] - W[7], p[2] - W[11]]; return [W[0] * q[0] + W[4] * q[1] + W[8] * q[2], W[1] * q[0] + W[5] * q[1] + W[9] * q[2], W[2] * q[0] + W[6] * q[1] + W[10] * q[2]]; };
106
- const tris = parts.flatMap((p) => p.triangles).map((t) => t.map(inv)), v = o.built.values, w = v.width, R = v.outer_diameter / 2, r = v.bore / 2;
107
- const printer = printerRules(s), want = seatSize(v, { fit: x.seat === true ? 'press' : x.seat, printer }), refs = { item: o.id, part: other };
108
- const firstWall = (z) => { const segs = sliceAt(tris, z); if (segs.length < 3) return null; const h = []; for (let k = 0; k < RAYS; k++) { const a = rayHits(segs, ((k + 0.37) * 2 * Math.PI) / RAYS); if (!a.length) return null; h.push(a[0]); } return h; };
200
+ const motion = s.motion,
201
+ ids = motion.items.map((y) => y[0]),
202
+ W = placeItems(motion, s.jointValues ?? jointValues(motion, motion.pose ?? undefined))[ids.indexOf(o.id)];
203
+ const inv = (p) => {
204
+ const q = [p[0] - W[3], p[1] - W[7], p[2] - W[11]];
205
+ return [
206
+ W[0] * q[0] + W[4] * q[1] + W[8] * q[2],
207
+ W[1] * q[0] + W[5] * q[1] + W[9] * q[2],
208
+ W[2] * q[0] + W[6] * q[1] + W[10] * q[2],
209
+ ];
210
+ };
211
+ const tris = parts.flatMap((p) => p.triangles).map((t) => t.map(inv)),
212
+ v = o.built.values,
213
+ w = v.width,
214
+ R = v.outer_diameter / 2,
215
+ r = v.bore / 2;
216
+ const printer = printerRules(s),
217
+ want = seatSize(v, { fit: x.seat === true ? 'press' : x.seat, printer }),
218
+ refs = { item: o.id, part: other };
219
+ const firstWall = (z) => {
220
+ const segs = sliceAt(tris, z);
221
+ if (segs.length < 3) return null;
222
+ const h = [];
223
+ for (let k = 0; k < RAYS; k++) {
224
+ const a = rayHits(segs, ((k + 0.37) * 2 * Math.PI) / RAYS);
225
+ if (!a.length) return null;
226
+ h.push(a[0]);
227
+ }
228
+ return h;
229
+ };
109
230
  const mid = firstWall(w / 2);
110
- if (!mid) return f('fail', `${label}: halfway along ${o.bought.set}, ${other} doesn't go round it`, `${label}: no seat`, { code, refs });
111
- const drawn = 2 * Math.max(...mid), diff = drawn - want.pocket, values = { drawn, wanted: want.pocket, difference: diff, comp: want.comp, clearance: want.clearance };
231
+ if (!mid)
232
+ return f('fail', `${label}: halfway along ${o.bought.set}, ${other} doesn't go round it`, `${label}: no seat`, {
233
+ code,
234
+ refs,
235
+ });
236
+ const drawn = 2 * Math.max(...mid),
237
+ diff = drawn - want.pocket,
238
+ values = { drawn, wanted: want.pocket, difference: diff, comp: want.comp, clearance: want.clearance };
112
239
  const inMaterial = 2 * Math.min(...mid) < 2 * r + 0.1;
113
- if (inMaterial) return f('fail', `${label}: ${other} is solid where the bearing goes (the wall is ${n3(Math.min(...mid), 2)} mm from its axis, inside the ${v.bore} mm bore)`, `${label}: no pocket`, { code, refs, values });
240
+ if (inMaterial)
241
+ return f(
242
+ 'fail',
243
+ `${label}: ${other} is solid where the bearing goes (the wall is ${n3(Math.min(...mid), 2)} mm from its axis, inside the ${v.bore} mm bore)`,
244
+ `${label}: no pocket`,
245
+ { code, refs, values },
246
+ );
114
247
  const say = `${o.bought.set} (${n3(2 * R, 2)} mm) in a pocket drawn ${n3(drawn, 3)} mm`;
115
- if (Math.abs(diff) > 0.15) f('fail', `${label}: ${say}, and a ${typeof want.fit === 'string' ? want.fit : `${want.fit} mm`} fit on this printer wants ${n3(want.pocket, 3)} mm: ${n3(Math.abs(diff), 2)} mm too ${diff < 0 ? 'small: it will not press in' : 'big: it will rattle'}`, `${label}: seat ${diff < 0 ? 'small' : 'big'}`, { code, refs, values });
116
- else if (Math.abs(diff) > 0.05) f('warn', `${label}: ${say}, ${n3(diff, 3)} mm from the ${n3(want.pocket, 3)} mm a ${typeof want.fit === 'string' ? want.fit : `${want.fit} mm`} fit wants`, `${label}: seat off`, { code, refs, values });
117
- else f('pass', `${label}: ${say}, a ${typeof want.fit === 'string' ? want.fit : `${want.fit} mm`} fit (printed about ${n3(want.clearance, 3)} mm)`, null, { code, refs, values });
248
+ if (Math.abs(diff) > 0.15)
249
+ f(
250
+ 'fail',
251
+ `${label}: ${say}, and a ${typeof want.fit === 'string' ? want.fit : `${want.fit} mm`} fit on this printer wants ${n3(want.pocket, 3)} mm: ${n3(Math.abs(diff), 2)} mm too ${diff < 0 ? 'small: it will not press in' : 'big: it will rattle'}`,
252
+ `${label}: seat ${diff < 0 ? 'small' : 'big'}`,
253
+ { code, refs, values },
254
+ );
255
+ else if (Math.abs(diff) > 0.05)
256
+ f(
257
+ 'warn',
258
+ `${label}: ${say}, ${n3(diff, 3)} mm from the ${n3(want.pocket, 3)} mm a ${typeof want.fit === 'string' ? want.fit : `${want.fit} mm`} fit wants`,
259
+ `${label}: seat off`,
260
+ { code, refs, values },
261
+ );
262
+ else
263
+ f(
264
+ 'pass',
265
+ `${label}: ${say}, a ${typeof want.fit === 'string' ? want.fit : `${want.fit} mm`} fit (printed about ${n3(want.clearance, 3)} mm)`,
266
+ null,
267
+ { code, refs, values },
268
+ );
118
269
  // How much of the race the wall holds, and what stops it
119
- const levels = 9, held = Array.from({ length: levels }, (_, k) => firstWall(w * (k + 0.5) / levels)).filter((h) => h && Math.min(...h) < R + 0.5).length / levels;
120
- if (held < 0.8) f('warn', `${label}: the pocket holds only ${n3(held * w, 1)} mm of the ${w} mm race: the bearing can tip`, `${label}: shallow`, { code: 'fit.seat', refs, values: { held: held * w, width: w } });
121
- const ledges = [firstWall(-0.3), firstWall(w + 0.3)].map((h) => (h ? Math.min(...h) : null)).filter((h) => h != null && h < R - 0.1);
122
- if (!ledges.length) f('warn', `${label}: nothing stops the bearing at either face: draw a lip`, `${label}: no lip`, { code: 'fit.seat', refs });
270
+ const levels = 9,
271
+ held =
272
+ Array.from({ length: levels }, (_, k) => firstWall((w * (k + 0.5)) / levels)).filter(
273
+ (h) => h && Math.min(...h) < R + 0.5,
274
+ ).length / levels;
275
+ if (held < 0.8)
276
+ f(
277
+ 'warn',
278
+ `${label}: the pocket holds only ${n3(held * w, 1)} mm of the ${w} mm race: the bearing can tip`,
279
+ `${label}: shallow`,
280
+ { code: 'fit.seat', refs, values: { held: held * w, width: w } },
281
+ );
282
+ const ledges = [firstWall(-0.3), firstWall(w + 0.3)]
283
+ .map((h) => (h ? Math.min(...h) : null))
284
+ .filter((h) => h != null && h < R - 0.1);
285
+ if (!ledges.length)
286
+ f('warn', `${label}: nothing stops the bearing at either face: draw a lip`, `${label}: no lip`, {
287
+ code: 'fit.seat',
288
+ refs,
289
+ });
123
290
  else {
124
291
  const lip = Math.max(...ledges);
125
- if (lip < r + (R - r) / 2) f('fail', `${label}: the lip comes in to ${n3(2 * lip, 2)} mm across, onto the inner race (which turns): it wants ${n3(2 * r + (R - r), 2)} mm or more`, `${label}: lip on inner race`, { code: 'fit.seat', refs, values: { lip: 2 * lip } });
126
- else f('pass', `${label}: a ${n3(R - lip, 2)} mm lip stops the outer race and clears the inner`, null, { code: 'fit.seat', refs, values: { lip: 2 * lip } });
292
+ if (lip < r + (R - r) / 2)
293
+ f(
294
+ 'fail',
295
+ `${label}: the lip comes in to ${n3(2 * lip, 2)} mm across, onto the inner race (which turns): it wants ${n3(2 * r + (R - r), 2)} mm or more`,
296
+ `${label}: lip on inner race`,
297
+ { code: 'fit.seat', refs, values: { lip: 2 * lip } },
298
+ );
299
+ else
300
+ f('pass', `${label}: a ${n3(R - lip, 2)} mm lip stops the outer race and clears the inner`, null, {
301
+ code: 'fit.seat',
302
+ refs,
303
+ values: { lip: 2 * lip },
304
+ });
127
305
  }
128
306
  }