@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/bought.mjs CHANGED
@@ -6,25 +6,50 @@ import fs from 'node:fs';
6
6
  import jscad from '@jscad/modeling';
7
7
  import { peekRuleSet as loadRuleSet } from './rules.mjs';
8
8
  import { RULE_DIRS } from './workspace.mjs';
9
+ import { readJson } from './host.mjs';
9
10
  import { named } from './parts.mjs';
10
11
  import { boughtFittingModel, boughtFitting } from '@rulecad/frames';
11
12
 
12
- const { cuboid, cylinder, polygon } = jscad.primitives, { translate } = jscad.transforms, { union, subtract } = jscad.booleans, { extrudeRotate } = jscad.extrusions;
13
+ const { cuboid, cylinder, polygon } = jscad.primitives,
14
+ { translate } = jscad.transforms,
15
+ { union, subtract } = jscad.booleans,
16
+ { extrudeRotate } = jscad.extrusions;
13
17
  const SEG = 48;
14
18
 
15
19
  // Rule sets that are bought parts, for saying what there is
16
20
  export function boughtSets() {
17
21
  const out = new Map();
18
- for (const d of RULE_DIRS) if (fs.existsSync(d)) for (const f of fs.readdirSync(d)) {
19
- if (!f.endsWith('.json') || f === 'vocabulary.json' || f === 'package.json') continue;
20
- try { const set = JSON.parse(fs.readFileSync(`${d}/${f}`, 'utf8')); if (set.part && !out.has(set.name)) out.set(set.name, set.part); } catch {}
21
- }
22
+ for (const d of RULE_DIRS)
23
+ if (fs.existsSync(d))
24
+ for (const f of fs.readdirSync(d)) {
25
+ if (!f.endsWith('.json') || f === 'vocabulary.json' || f === 'package.json') continue;
26
+ try {
27
+ const set = readJson(`${d}/${f}`);
28
+ if (set.part && !out.has(set.name)) out.set(set.name, set.part);
29
+ } catch {
30
+ // (a rule set that can't be read is cad rules check's to name: here it is just not a bought part)
31
+ }
32
+ }
22
33
  return out;
23
34
  }
24
35
 
25
36
  // A reference item the assembly can place, from its parts, anchors and figures: what a motor and any bought part share
26
37
  export function referenceBuilt({ id, key, objName, name, parts, anchors, entries, values, extra = {} }) {
27
- const b = { object: { name, title: name, role: 'reference', version: '0.0.0', anchors }, values, entries, deps: [], units: {}, parts, polygons: parts.flatMap((p) => p.polygons), triangles: parts.flatMap((p) => p.triangles), hash: crypto.createHash('sha256').update(JSON.stringify([values, entries.map((e) => e.name)])).digest('hex').slice(0, 16) };
38
+ const b = {
39
+ object: { name, title: name, role: 'reference', version: '0.0.0', anchors },
40
+ values,
41
+ entries,
42
+ deps: [],
43
+ units: {},
44
+ parts,
45
+ polygons: parts.flatMap((p) => p.polygons),
46
+ triangles: parts.flatMap((p) => p.triangles),
47
+ hash: crypto
48
+ .createHash('sha256')
49
+ .update(JSON.stringify([values, entries.map((e) => e.name)]))
50
+ .digest('hex')
51
+ .slice(0, 16),
52
+ };
28
53
  return { key: `${key}:${id}:${b.hash}`, objName, b, version: '0.0.0', role: 'reference', ...extra };
29
54
  }
30
55
 
@@ -33,52 +58,185 @@ const box = (w, d, h, x = 0, y = 0, z0 = 0) => translate([x, y, z0 + h / 2], cub
33
58
 
34
59
  // What each kind of part looks like, from its figures. Each returns { parts: { name: solid }, anchors }
35
60
  const SHAPES = {
36
- bearing(v, set) {
37
- const r = v.bore / 2, R = v.outer_diameter / 2, w = v.width, c = Math.min(v.chamfer ?? 0.2, (R - r) / 4, w / 4);
38
- const flange = v.flange_diameter ? v.flange_diameter / 2 : 0, f = v.flange_width ?? 0;
61
+ bearing(v) {
62
+ const r = v.bore / 2,
63
+ R = v.outer_diameter / 2,
64
+ w = v.width,
65
+ c = Math.min(v.chamfer ?? 0.2, (R - r) / 4, w / 4);
66
+ const flange = v.flange_diameter ? v.flange_diameter / 2 : 0,
67
+ f = v.flange_width ?? 0;
39
68
  const pts = flange
40
- ? [[r + c, 0], [R - c, 0], [R, c], [R, w - f], [flange, w - f], [flange, w], [r + c, w], [r, w - c], [r, c]]
41
- : [[r + c, 0], [R - c, 0], [R, c], [R, w - c], [R - c, w], [r + c, w], [r, w - c], [r, c]];
42
- return { parts: { body: extrudeRotate({ segments: SEG }, polygon({ points: pts })) }, anchors: { bore: z([0, 0, w / 2]), outer: z([0, 0, w / 2]), face: z([0, 0, 0]), top: z([0, 0, w]) } };
69
+ ? [
70
+ [r + c, 0],
71
+ [R - c, 0],
72
+ [R, c],
73
+ [R, w - f],
74
+ [flange, w - f],
75
+ [flange, w],
76
+ [r + c, w],
77
+ [r, w - c],
78
+ [r, c],
79
+ ]
80
+ : [
81
+ [r + c, 0],
82
+ [R - c, 0],
83
+ [R, c],
84
+ [R, w - c],
85
+ [R - c, w],
86
+ [r + c, w],
87
+ [r, w - c],
88
+ [r, c],
89
+ ];
90
+ return {
91
+ parts: { body: extrudeRotate({ segments: SEG }, polygon({ points: pts })) },
92
+ anchors: { bore: z([0, 0, w / 2]), outer: z([0, 0, w / 2]), face: z([0, 0, 0]), top: z([0, 0, w]) },
93
+ };
43
94
  },
44
95
  switch(v) {
45
- const w = v.body_width, d = v.body_depth, h = v.body_length, holes = (v.mount_hole_spacing ?? 0) / 2, up = h / 2;
46
- const pin = (x) => translate([x, 0, up], jscad.transforms.rotateX(Math.PI / 2, cylinder({ radius: (v.mount_hole_diameter ?? 2.4) / 2, height: d + 2, segments: 24 })));
96
+ const w = v.body_width,
97
+ d = v.body_depth,
98
+ h = v.body_length,
99
+ holes = (v.mount_hole_spacing ?? 0) / 2,
100
+ up = h / 2;
101
+ const pin = (x) =>
102
+ translate(
103
+ [x, 0, up],
104
+ jscad.transforms.rotateX(
105
+ Math.PI / 2,
106
+ cylinder({ radius: (v.mount_hole_diameter ?? 2.4) / 2, height: d + 2, segments: 24 }),
107
+ ),
108
+ );
47
109
  const body = holes ? subtract(box(w, d, h), pin(-holes), pin(holes)) : box(w, d, h);
48
- return { parts: { body, lever: box(w, Math.min(4, d), 0.4, w * 0.1, 0, h + 2) }, anchors: { lever: z([w * 0.6, 0, h + 2]), left: z([-holes, 0, up], [0, 1, 0]), right: z([holes, 0, up], [0, 1, 0]), base: z([0, 0, 0]) } };
110
+ return {
111
+ parts: { body, lever: box(w, Math.min(4, d), 0.4, w * 0.1, 0, h + 2) },
112
+ anchors: {
113
+ lever: z([w * 0.6, 0, h + 2]),
114
+ left: z([-holes, 0, up], [0, 1, 0]),
115
+ right: z([holes, 0, up], [0, 1, 0]),
116
+ base: z([0, 0, 0]),
117
+ },
118
+ };
49
119
  },
50
120
  board(v, set) {
51
- const w = v.body_width, d = v.body_depth, t = v.board_thickness ?? 1.6;
52
- const axes = [...new Set(Object.keys(set.rules).map((k) => /^step_pin_(\w+)$/.exec(k)?.[1]).filter(Boolean))].map((a) => a.toUpperCase());
121
+ const w = v.body_width,
122
+ d = v.body_depth,
123
+ t = v.board_thickness ?? 1.6;
124
+ const axes = [
125
+ ...new Set(
126
+ Object.keys(set.rules)
127
+ .map((k) => /^step_pin_(\w+)$/.exec(k)?.[1])
128
+ .filter(Boolean),
129
+ ),
130
+ ].map((a) => a.toUpperCase());
53
131
  // Where a board's axis connectors are along its edge is for routing a wire, no closer than that
54
- const sockets = Object.fromEntries(axes.map((a, i) => [a, z([-w / 2 + ((i + 0.5) * w) / axes.length, d / 2, t], [0, 1, 0])]));
132
+ const sockets = Object.fromEntries(
133
+ axes.map((a, i) => [a, z([-w / 2 + ((i + 0.5) * w) / axes.length, d / 2, t], [0, 1, 0])]),
134
+ );
55
135
  // its mounting holes, where its set gives them, drawn through it so a screw's clearance is measured in them
56
- const flat = set.rules.mount_holes?.value, holes = Array.isArray(flat) && v.mount_hole_diameter ? Array.from({ length: Math.floor(flat.length / 2) }, (_, i) => [flat[2 * i], flat[2 * i + 1]]) : [];
57
- const body = holes.length ? subtract(box(w, d, t), ...holes.map(([x, y]) => translate([x, y, t / 2], cylinder({ radius: v.mount_hole_diameter / 2, height: t + 2, segments: 32 })))) : box(w, d, t);
58
- return { parts: { body }, anchors: { centre: z([0, 0, t / 2]), top: z([0, 0, t]), bottom: z([0, 0, 0]), ...sockets } };
136
+ const flat = set.rules.mount_holes?.value,
137
+ holes =
138
+ Array.isArray(flat) && v.mount_hole_diameter
139
+ ? Array.from({ length: Math.floor(flat.length / 2) }, (_, i) => [flat[2 * i], flat[2 * i + 1]])
140
+ : [];
141
+ const body = holes.length
142
+ ? subtract(
143
+ box(w, d, t),
144
+ ...holes.map(([x, y]) =>
145
+ translate([x, y, t / 2], cylinder({ radius: v.mount_hole_diameter / 2, height: t + 2, segments: 32 })),
146
+ ),
147
+ )
148
+ : box(w, d, t);
149
+ return {
150
+ parts: { body },
151
+ anchors: { centre: z([0, 0, t / 2]), top: z([0, 0, t]), bottom: z([0, 0, 0]), ...sockets },
152
+ };
59
153
  },
60
154
  driver(v) {
61
- const w = v.body_width, d = v.body_depth, t = 1.6, row = v.row_spacing / 2, pins = v.pin_count / 2, pitch = v.pin_pitch, run = pitch * (pins - 1), pin = 0.64, drop = 8;
62
- const rows = [-row, row].flatMap((y) => Array.from({ length: pins }, (_, i) => box(pin, pin, drop + t, -run / 2 + i * pitch, y, -drop)));
63
- return { parts: { body: union(box(w, d, t), box(5, 5, 1, 0, 0, t)), pins: union(rows) }, anchors: { header: z([0, 0, 0]), top: z([0, 0, t]) } };
155
+ const w = v.body_width,
156
+ d = v.body_depth,
157
+ t = 1.6,
158
+ row = v.row_spacing / 2,
159
+ pins = v.pin_count / 2,
160
+ pitch = v.pin_pitch,
161
+ run = pitch * (pins - 1),
162
+ pin = 0.64,
163
+ drop = 8;
164
+ const rows = [-row, row].flatMap((y) =>
165
+ Array.from({ length: pins }, (_, i) => box(pin, pin, drop + t, -run / 2 + i * pitch, y, -drop)),
166
+ );
167
+ return {
168
+ parts: { body: union(box(w, d, t), box(5, 5, 1, 0, 0, t)), pins: union(rows) },
169
+ anchors: { header: z([0, 0, 0]), top: z([0, 0, t]) },
170
+ };
171
+ },
172
+ encoder(v, set) {
173
+ return SHAPES.board(v, set);
174
+ },
175
+ linear_bearing(v) {
176
+ // bore along z, from z = 0
177
+ const r = v.bore / 2,
178
+ R = v.outer_diameter / 2,
179
+ L = v.length,
180
+ c = 0.3;
181
+ return {
182
+ parts: {
183
+ body: extrudeRotate(
184
+ { segments: SEG },
185
+ polygon({
186
+ points: [
187
+ [r + c, 0],
188
+ [R - c, 0],
189
+ [R, c],
190
+ [R, L - c],
191
+ [R - c, L],
192
+ [r + c, L],
193
+ [r, L - c],
194
+ [r, c],
195
+ ],
196
+ }),
197
+ ),
198
+ },
199
+ anchors: { bore: z([0, 0, L / 2]), face: z([0, 0, 0]), top: z([0, 0, L]) },
200
+ };
64
201
  },
65
- encoder(v, set) { return SHAPES.board(v, set); },
66
- linear_bearing(v) { // bore along z, from z = 0
67
- const r = v.bore / 2, R = v.outer_diameter / 2, L = v.length, c = 0.3;
68
- return { parts: { body: extrudeRotate({ segments: SEG }, polygon({ points: [[r + c, 0], [R - c, 0], [R, c], [R, L - c], [R - c, L], [r + c, L], [r, L - c], [r, c]] })) }, anchors: { bore: z([0, 0, L / 2]), face: z([0, 0, 0]), top: z([0, 0, L]) } };
202
+ rod(v) {
203
+ return {
204
+ parts: {
205
+ body: translate([0, 0, v.length / 2], cylinder({ radius: v.diameter / 2, height: v.length, segments: SEG })),
206
+ },
207
+ anchors: { bottom: z([0, 0, 0]), top: z([0, 0, v.length]) },
208
+ };
69
209
  },
70
- rod(v) { return { parts: { body: translate([0, 0, v.length / 2], cylinder({ radius: v.diameter / 2, height: v.length, segments: SEG })) }, anchors: { bottom: z([0, 0, 0]), top: z([0, 0, v.length]) } }; },
71
210
  // a bought fitting for a frame (@rulecad/frames' bought.mjs): drawn in the printed fittings' frames, an anchor on
72
211
  // each screw's axis at its head's seat (b1, b2 .. into b; a1 .. into a); one that lies in the slots (an inner or an
73
212
  // anchor connector) is inside the beams by design, so the overlap check leaves it out
74
213
  fitting(v, set) {
75
- const m = boughtFittingModel({ name: set.name, part: 'fitting', rules: set.rules }), count = { a: 0, b: 0 };
76
- const anchors = Object.fromEntries(m.screws.map((q) => [`${q.leg}${++count[q.leg]}`, z(q.at, q.axis.map((c) => (c ? -c : 0)))]));
77
- return { parts: { body: boughtFitting(m, { pose: 'fit' }) }, anchors: { origin: z([0, 0, 0]), ...anchors }, inside: ['hidden', 'anchor'].includes(m.fitting), model: m };
214
+ const m = boughtFittingModel({ name: set.name, part: 'fitting', rules: set.rules }),
215
+ count = { a: 0, b: 0 };
216
+ const anchors = Object.fromEntries(
217
+ m.screws.map((q) => [
218
+ `${q.leg}${++count[q.leg]}`,
219
+ z(
220
+ q.at,
221
+ q.axis.map((c) => (c ? -c : 0)),
222
+ ),
223
+ ]),
224
+ );
225
+ return {
226
+ parts: { body: boughtFitting(m, { pose: 'fit' }) },
227
+ anchors: { origin: z([0, 0, 0]), ...anchors },
228
+ inside: ['hidden', 'anchor'].includes(m.fitting),
229
+ model: m,
230
+ };
78
231
  },
79
232
  supply(v) {
80
- const w = v.body_width, d = v.body_depth, h = v.body_length;
81
- return { parts: { body: box(w, d, h) }, anchors: { base: z([0, 0, 0]), top: z([0, 0, h]), terminals: z([0, -d / 2, h / 2], [0, -1, 0]) } };
233
+ const w = v.body_width,
234
+ d = v.body_depth,
235
+ h = v.body_length;
236
+ return {
237
+ parts: { body: box(w, d, h) },
238
+ anchors: { base: z([0, 0, 0]), top: z([0, 0, h]), terminals: z([0, -d / 2, h / 2], [0, -1, 0]) },
239
+ };
82
240
  },
83
241
  };
84
242
 
@@ -91,7 +249,21 @@ export function boughtModel(name, variant) {
91
249
  const vs = set.variants || {};
92
250
  if (!vs[variant]) throw new Error(`${name} has no variant "${variant}" (${Object.keys(vs).join(', ') || 'none'})`);
93
251
  // (its values take the variant's kind and source; its rules, where it has them, are whole rules with their own)
94
- rules = { ...set.rules, ...Object.fromEntries(Object.entries(vs[variant].values ?? {}).map(([k, value]) => [k, { ...set.rules[k], value, kind: vs[variant].kind ?? set.rules[k]?.kind, source: vs[variant].source ?? set.rules[k]?.source }])), ...(vs[variant].rules ?? {}) };
252
+ rules = {
253
+ ...set.rules,
254
+ ...Object.fromEntries(
255
+ Object.entries(vs[variant].values ?? {}).map(([k, value]) => [
256
+ k,
257
+ {
258
+ ...set.rules[k],
259
+ value,
260
+ kind: vs[variant].kind ?? set.rules[k]?.kind,
261
+ source: vs[variant].source ?? set.rules[k]?.source,
262
+ },
263
+ ]),
264
+ ),
265
+ ...(vs[variant].rules ?? {}),
266
+ };
95
267
  }
96
268
  return { name, part: set.part, variant: variant ?? null, rules };
97
269
  }
@@ -100,17 +272,65 @@ export function boughtModel(name, variant) {
100
272
  export function boughtBuilt(spec, it, num, id) {
101
273
  const m = boughtModel(spec, it.variant);
102
274
  // Figures that differ from the set's, by name: a smaller supply, a measured bearing
103
- for (const [k, x] of Object.entries(it.set || {})) { if (!m.rules[k]) throw new Error(`item ${id}: ${spec} has no figure "${k}" (${Object.keys(m.rules).join(', ')})`); m.rules = { ...m.rules, [k]: { ...m.rules[k], value: num(x), kind: 'estimate', source: 'set on this item' } }; }
104
- if (!SHAPES[m.part]) throw new Error(`item ${id}: ${spec} is a ${m.part}: bought parts are ${Object.keys(SHAPES).join(', ')}, and a motor goes in "motor"`);
105
- const values = Object.fromEntries(Object.entries(m.rules).filter(([, r]) => typeof r.value === 'number').map(([k, r]) => [k, r.value]));
275
+ for (const [k, x] of Object.entries(it.set || {})) {
276
+ if (!m.rules[k]) throw new Error(`item ${id}: ${spec} has no figure "${k}" (${Object.keys(m.rules).join(', ')})`);
277
+ m.rules = { ...m.rules, [k]: { ...m.rules[k], value: num(x), kind: 'estimate', source: 'set on this item' } };
278
+ }
279
+ if (!SHAPES[m.part])
280
+ throw new Error(
281
+ `item ${id}: ${spec} is a ${m.part}: bought parts are ${Object.keys(SHAPES).join(', ')}, and a motor goes in "motor"`,
282
+ );
283
+ const values = Object.fromEntries(
284
+ Object.entries(m.rules)
285
+ .filter(([, r]) => typeof r.value === 'number')
286
+ .map(([k, r]) => [k, r.value]),
287
+ );
106
288
  let shape;
107
- try { shape = SHAPES[m.part](values, m); } catch (e) { throw new Error(`item ${id}: ${spec} doesn't say enough to draw it: ${e.message}`); }
108
- const missing = Object.entries(shape.anchors).filter(([, a]) => a.at.some((c) => !Number.isFinite(c))).map(([k]) => k);
289
+ try {
290
+ shape = SHAPES[m.part](values, m);
291
+ } catch (e) {
292
+ throw new Error(`item ${id}: ${spec} doesn't say enough to draw it: ${e.message}`);
293
+ }
294
+ const missing = Object.entries(shape.anchors)
295
+ .filter(([, a]) => a.at.some((c) => !Number.isFinite(c)))
296
+ .map(([k]) => k);
109
297
  if (missing.length) throw new Error(`item ${id}: ${spec}: its rules don't place the anchors ${missing.join(', ')}`);
110
- const parts = named(shape.parts, Object.fromEntries(Object.keys(shape.parts).map((k) => [k, { label: `${m.name} ${k}` }])));
298
+ const parts = named(
299
+ shape.parts,
300
+ Object.fromEntries(Object.keys(shape.parts).map((k) => [k, { label: `${m.name} ${k}` }])),
301
+ );
111
302
  if (shape.inside) parts[0].collide = jscad.geometries.geom3.create(); // (in the slots, inside the beams by design)
112
- const entries = Object.entries(m.rules).map(([k, r]) => ({ name: `${m.name}.${k}`, value: r.value, unit: r.unit, rule: r.rule, kind: r.kind, source: r.source, checked: r.checked, set: m.name }));
113
- return referenceBuilt({ id, key: 'bought', objName: `bought:${m.name}`, name: m.name, parts, anchors: shape.anchors, entries, values, extra: { bought: { set: m.name, variant: m.variant, part: m.part, mass: values.mass ?? null, mount: mountOf(m), ...(m.rules.cost ? { cost: { value: m.rules.cost.value, unit: m.rules.cost.unit } } : {}), ...(shape.model ? { fitting: shape.model } : {}) } } });
303
+ const entries = Object.entries(m.rules).map(([k, r]) => ({
304
+ name: `${m.name}.${k}`,
305
+ value: r.value,
306
+ unit: r.unit,
307
+ rule: r.rule,
308
+ kind: r.kind,
309
+ source: r.source,
310
+ checked: r.checked,
311
+ set: m.name,
312
+ }));
313
+ return referenceBuilt({
314
+ id,
315
+ key: 'bought',
316
+ objName: `bought:${m.name}`,
317
+ name: m.name,
318
+ parts,
319
+ anchors: shape.anchors,
320
+ entries,
321
+ values,
322
+ extra: {
323
+ bought: {
324
+ set: m.name,
325
+ variant: m.variant,
326
+ part: m.part,
327
+ mass: values.mass ?? null,
328
+ mount: mountOf(m),
329
+ ...(m.rules.cost ? { cost: { value: m.rules.cost.value, unit: m.rules.cost.unit } } : {}),
330
+ ...(shape.model ? { fitting: shape.model } : {}),
331
+ },
332
+ },
333
+ });
114
334
  }
115
335
 
116
336
  /**
@@ -119,11 +339,22 @@ export function boughtBuilt(spec, it, num, id) {
119
339
  * diameter plain ones (a board's). @param {{ name: string, part: string, rules: object }} m boughtModel's
120
340
  */
121
341
  export function mountOf(m) {
122
- const r = m.rules, flat = r.mount_holes?.value; // (x then y for each hole, as the vocabulary keeps a list)
342
+ const r = m.rules,
343
+ flat = r.mount_holes?.value; // (x then y for each hole, as the vocabulary keeps a list)
123
344
  if (!Array.isArray(flat) || !flat.length || flat.length % 2) return null;
124
345
  const holes = Array.from({ length: flat.length / 2 }, (_, i) => [flat[2 * i], flat[2 * i + 1]]);
125
- return { set: m.name, holes, thread: typeof r.mount_hole_thread?.value === 'string' ? r.mount_hole_thread.value : null, reach: r.mount_hole_reach?.value ?? null, diameter: r.mount_hole_diameter?.value ?? null,
126
- thickness: m.part === 'board' ? r.board_thickness?.value ?? 1.6 : null, body: [r.body_width?.value, r.body_depth?.value], height: m.part === 'board' ? r.board_thickness?.value ?? 1.6 : r.body_length?.value ?? null, kind: r.mount_holes.kind, source: r.mount_holes.source };
346
+ return {
347
+ set: m.name,
348
+ holes,
349
+ thread: typeof r.mount_hole_thread?.value === 'string' ? r.mount_hole_thread.value : null,
350
+ reach: r.mount_hole_reach?.value ?? null,
351
+ diameter: r.mount_hole_diameter?.value ?? null,
352
+ thickness: m.part === 'board' ? (r.board_thickness?.value ?? 1.6) : null,
353
+ body: [r.body_width?.value, r.body_depth?.value],
354
+ height: m.part === 'board' ? (r.board_thickness?.value ?? 1.6) : (r.body_length?.value ?? null),
355
+ kind: r.mount_holes.kind,
356
+ source: r.mount_holes.source,
357
+ };
127
358
  }
128
359
 
129
360
  // One line in the check for each bought item, so the shopping list and the wiring can read it
@@ -131,11 +362,36 @@ export function boughtFindings(s, f) {
131
362
  // a frame's bought fitting placed at a beam's address (an end cap at x1.a, a foot, a hinge on a face): does it fit
132
363
  // that beam's profile (its size, its slot family, a foot's stem against the end's bore)
133
364
  const items = new Map((s.a?.items ?? []).map((it) => [it.id, it]));
134
- for (const o of s.objects) if (o.bought?.fitting && !o.unplaced && items.get(o.id)?.join == null) {
135
- const at = items.get(o.id)?.at, beam = typeof at === 'string' ? s.objects.find((x) => x.id === at.trim().split('.')[0])?.beam : null;
136
- if (!beam) continue;
137
- for (const x of boughtFittingModel(o.bought.fitting.set, { profile: beam.profile, ...(s.frames ? { given: s.frames } : {}) }).findings.filter((y) => y.code === 'fitting.fit'))
138
- f(x.level, `${o.id}: ${x.text}`, x.level === 'fail' ? `${o.id}: fit` : null, { code: x.code, ...(x.values ? { values: x.values } : {}), refs: { item: o.id, beam: beam.name, ...x.refs } });
139
- }
140
- for (const o of s.objects) if (o.bought) f('info', `${o.id}: ${o.bought.set}${o.bought.variant ? ` (${o.bought.variant})` : ''}${o.bought.length != null ? ` x ${Number(o.bought.length.toFixed(2))} mm` : ''}, a ${o.bought.part}`, null, { code: 'bought.item', refs: { item: o.id, set: o.bought.set, variant: o.bought.variant, part: o.bought.part, ...(o.bought.length != null ? { length: o.bought.length } : {}) } });
365
+ for (const o of s.objects)
366
+ if (o.bought?.fitting && !o.unplaced && items.get(o.id)?.join == null) {
367
+ const at = items.get(o.id)?.at,
368
+ beam = typeof at === 'string' ? s.objects.find((x) => x.id === at.trim().split('.')[0])?.beam : null;
369
+ if (!beam) continue;
370
+ for (const x of boughtFittingModel(o.bought.fitting.set, {
371
+ profile: beam.profile,
372
+ ...(s.frames ? { given: s.frames } : {}),
373
+ }).findings.filter((y) => y.code === 'fitting.fit'))
374
+ f(x.level, `${o.id}: ${x.text}`, x.level === 'fail' ? `${o.id}: fit` : null, {
375
+ code: x.code,
376
+ ...(x.values ? { values: x.values } : {}),
377
+ refs: { item: o.id, beam: beam.name, ...x.refs },
378
+ });
379
+ }
380
+ for (const o of s.objects)
381
+ if (o.bought)
382
+ f(
383
+ 'info',
384
+ `${o.id}: ${o.bought.set}${o.bought.variant ? ` (${o.bought.variant})` : ''}${o.bought.length != null ? ` x ${Number(o.bought.length.toFixed(2))} mm` : ''}, a ${o.bought.part}`,
385
+ null,
386
+ {
387
+ code: 'bought.item',
388
+ refs: {
389
+ item: o.id,
390
+ set: o.bought.set,
391
+ variant: o.bought.variant,
392
+ part: o.bought.part,
393
+ ...(o.bought.length != null ? { length: o.bought.length } : {}),
394
+ },
395
+ },
396
+ );
141
397
  }