@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/slotnuts.mjs CHANGED
@@ -8,10 +8,12 @@
8
8
  // frame.preload: the nuts with a way in that a join closes and none that stays open (the other end capped): they
9
9
  // go into the slot before that join is made. cad order and the build page list them at the step that makes it.
10
10
  import { nutFor } from '@rulecad/frames';
11
+ import { fixingModel } from '@rulecad/fixings';
11
12
  import { framesContext } from './beams.mjs';
12
13
  import { givenFor } from './frames.mjs';
13
14
 
14
- const mm = (x) => String(+Number(x).toFixed(2)), r3 = (x) => +x.toFixed(3);
15
+ const mm = (x) => String(+Number(x).toFixed(2)),
16
+ r3 = (x) => +x.toFixed(3);
15
17
  const EPS = 1e-3; // mm: a closure this near a nut is where the nut is, not beside it
16
18
  const THROUGH = ['drop-in', 'roll-in', 'hammer', 't-bolt']; // (the kinds that go in through the opening, in the order a stand-in is offered)
17
19
  const list = (xs) => (xs.length < 2 ? xs.join('') : `${xs.slice(0, -1).join(', ')} and ${xs.at(-1)}`);
@@ -19,18 +21,22 @@ const an = (t) => (/^(?:[aeiou8]|M\d)/i.test(String(t)) ? 'an' : 'a');
19
21
 
20
22
  /**
21
23
  * Each fasten's nuts in one slot: the relation, the beam and its slot (place: top.1, as joins name it; named: top, as
22
- * said where the face has one slot), the nut, its kind and entry, the part it holds, and where along the beam each is.
24
+ * said where the face has one slot), the nut, its kind and entry, the part it holds, where along the beam each screw is
25
+ * (at) and the stretch of slot each nut takes, its length either side of its screw (spans).
23
26
  * @param {object} s a built assembly
24
- * @returns {{ rel: object, item: string, place: string, named: string, nut: string, kind: string, entry: string, thread: string, part: string, at: number[], screws: object[] }[]}
27
+ * @returns {{ rel: object, item: string, place: string, named: string, nut: string, kind: string, entry: string, thread: string, part: string, at: number[], spans: number[][], screws: object[] }[]}
25
28
  */
26
29
  export function nutGroupsOf(s) {
27
30
  const out = new Map();
28
- for (const rel of s.fastened?.relations ?? []) for (const sc of rel.screws) {
29
- if (!sc.slot) continue;
30
- const key = `${rel.index}|${sc.slot.item}|${sc.slot.place}`; // (place: top.1, as joins name it; named: top, as said where the face has one slot)
31
- if (!out.has(key)) out.set(key, { rel, ...sc.slot, at: [], screws: [] });
32
- out.get(key).at.push(sc.slot.distance); out.get(key).screws.push(sc);
33
- }
31
+ for (const rel of s.fastened?.relations ?? [])
32
+ for (const sc of rel.screws) {
33
+ if (!sc.slot) continue;
34
+ const key = `${rel.index}|${sc.slot.item}|${sc.slot.place}`; // (place: top.1, as joins name it; named: top, as said where the face has one slot)
35
+ if (!out.has(key)) out.set(key, { rel, ...sc.slot, at: [], spans: [], screws: [] });
36
+ out.get(key).at.push(sc.slot.distance);
37
+ out.get(key).spans.push(sc.slot.span ?? [sc.slot.distance, sc.slot.distance]);
38
+ out.get(key).screws.push(sc);
39
+ }
34
40
  return [...out.values()];
35
41
  }
36
42
 
@@ -40,11 +46,14 @@ export function nutGroupsOf(s) {
40
46
  * @returns {{ at: number, end: string|null, join?: object, head?: boolean, item?: string }[]}
41
47
  */
42
48
  export function closuresOf(s, item, place) {
43
- const L = s.objects.find((o) => o.id === item)?.beam?.length ?? 0, out = [], endAt = (e) => (e === 'a' ? 0 : L);
49
+ const L = s.objects.find((o) => o.id === item)?.beam?.length ?? 0,
50
+ out = [],
51
+ endAt = (e) => (e === 'a' ? 0 : L);
44
52
  for (const j of s.joins ?? []) {
45
53
  if (j.items[0] === item) out.push({ at: endAt(j.end[0]), end: j.end[0], join: j });
46
54
  if (j.items[1] === item && j.model.b.end) out.push({ at: endAt(j.model.b.end[0]), end: j.model.b.end[0], join: j });
47
- if (j.items[1] === item && j.kind === 'blind' && j.model.b.place === place) out.push({ at: j.model.b.distance, end: null, join: j, head: true });
55
+ if (j.items[1] === item && j.kind === 'blind' && j.model.b.place === place)
56
+ out.push({ at: j.model.b.distance, end: null, join: j, head: true });
48
57
  }
49
58
  for (const it of s.a.items || []) {
50
59
  const m = typeof it.at === 'string' ? /^([\w-]+)\.([ab])(\.|$)/.exec(it.at.trim()) : null;
@@ -57,17 +66,75 @@ export function closuresOf(s, item, place) {
57
66
  // join before an item where both stand at one place). A way is closed by a join where any closure on it is a join, not
58
67
  // only the nearest: a head between the nuts and an end closed further along closes it as much as that end
59
68
  function waysOf(s, item, place, lo, hi) {
60
- const cs = closuresOf(s, item, place), near = (far) => (p, q) => (far ? q.at - p.at : p.at - q.at) || (q.join ? 1 : 0) - (p.join ? 1 : 0);
61
- const all = { a: cs.filter((c) => c.at < lo - EPS).sort(near(true)), b: cs.filter((c) => c.at > hi + EPS).sort(near(false)) };
62
- return { a: all.a[0] ?? null, b: all.b[0] ?? null, all, join: { a: all.a.find((c) => c.join) ?? null, b: all.b.find((c) => c.join) ?? null } };
69
+ const cs = closuresOf(s, item, place),
70
+ near = (far) => (p, q) => (far ? q.at - p.at : p.at - q.at) || (q.join ? 1 : 0) - (p.join ? 1 : 0);
71
+ const all = {
72
+ a: cs.filter((c) => c.at < lo - EPS).sort(near(true)),
73
+ b: cs.filter((c) => c.at > hi + EPS).sort(near(false)),
74
+ };
75
+ return {
76
+ a: all.a[0] ?? null,
77
+ b: all.b[0] ?? null,
78
+ all,
79
+ join: { a: all.a.find((c) => c.join) ?? null, b: all.b.find((c) => c.join) ?? null },
80
+ };
81
+ }
82
+ const closerSays = (g, w) =>
83
+ w.head
84
+ ? `the head of the ${w.join.label} at ${mm(w.at)} mm`
85
+ : w.join
86
+ ? `${g.item}'s end ${w.end} (the ${w.join.label})`
87
+ : `${g.item}'s end ${w.end} (${w.item} placed there)`;
88
+ // Where nuts in one slot are in each other's way: each nut takes its length of the slot, so two whose stretches
89
+ // overlap cannot both be there, and nor can a nut and the head of a blind join sitting in that slot. Each crowding is
90
+ // the nut, its stretch, and what it runs into (another nut, or a join's head) with how far
91
+ function crowdedOf(s, groups) {
92
+ const out = [],
93
+ slots = new Map();
94
+ for (const g of groups)
95
+ g.spans.forEach((span, i) => {
96
+ const key = `${g.item}|${g.place}`;
97
+ if (!slots.has(key)) slots.set(key, []);
98
+ slots.get(key).push({ g, i, at: g.at[i], span });
99
+ });
100
+ for (const nuts of slots.values()) {
101
+ nuts.sort((p, q) => p.span[0] - q.span[0]);
102
+ for (let i = 0; i < nuts.length; i++)
103
+ for (let j = i + 1; j < nuts.length && nuts[j].span[0] < nuts[i].span[1] - EPS; j++)
104
+ out.push({ nut: nuts[j], other: nuts[i], by: nuts[i].span[1] - nuts[j].span[0] });
105
+ const { g } = nuts[0];
106
+ for (const c of closuresOf(s, g.item, g.place).filter((q) => q.head)) {
107
+ // (the head is no point either: it takes its own width of the slot, half either side of the join's screw)
108
+ const half = headAcross(c.join) / 2,
109
+ lo = c.at - half,
110
+ hi = c.at + half;
111
+ for (const n of nuts)
112
+ if (lo < n.span[1] - EPS && hi > n.span[0] + EPS)
113
+ out.push({ nut: n, head: c, across: 2 * half, by: Math.min(hi - n.span[0], n.span[1] - lo) });
114
+ }
115
+ }
116
+ return out;
117
+ }
118
+ // How wide a blind join's screw head is across, from its thread set (0 where the screw does not read)
119
+ function headAcross(join) {
120
+ try {
121
+ return fixingModel(String(join.model.screws[0].screw), {}, { anyLength: true }).head?.across ?? 0;
122
+ } catch {
123
+ return 0; // (a screw the kit cannot read is said by the join's own checks)
124
+ }
63
125
  }
64
- const closerSays = (g, w) => (w.head ? `the head of the ${w.join.label} at ${mm(w.at)} mm` : w.join ? `${g.item}'s end ${w.end} (the ${w.join.label})` : `${g.item}'s end ${w.end} (${w.item} placed there)`);
65
126
  const nutsSay = (g) => `${g.at.length} ${g.kind} nut${g.at.length > 1 ? 's' : ''} (${g.nut})`;
66
127
 
67
128
  // The nut of that thread made for the slot that goes in through its opening: a drop-in first, then the other kinds
68
129
  function throughNut(s, g) {
69
130
  const b = s.objects.find((o) => o.id === g.item)?.beam;
70
- for (const kind of THROUGH) { try { return nutFor(b.profile, g.place, g.thread, { kind, given: givenFor(framesContext(), b.profile.name) }); } catch {} }
131
+ for (const kind of THROUGH) {
132
+ try {
133
+ return nutFor(b.profile, g.place, g.thread, { kind, given: givenFor(framesContext(), b.profile.name) });
134
+ } catch {
135
+ // (this kind of nut doesn't fit the slot: the next kind is tried)
136
+ }
137
+ }
71
138
  return null;
72
139
  }
73
140
 
@@ -86,16 +153,45 @@ export function preloadOf(s) {
86
153
  if (!w.a || !w.b || (w.join.a && w.join.b) || (!w.join.a && !w.join.b)) continue; // (a way stays open; or joins close both: frame.nut's; or only items placed at the ends do, which go on after the nuts)
87
154
  // every join on that way closes it (a head nearer the end as much as the nearest), so the nuts go in before the first
88
155
  // made; an item at that end (a foot, a cap) is off until then too
89
- const way = w.join.a ? 'a' : 'b', other = way === 'a' ? 'b' : 'a', by = w.join[way], ends = w.all[other].filter((c) => c.item), caps = w.all[way].filter((c) => c.item);
156
+ const way = w.join.a ? 'a' : 'b',
157
+ other = way === 'a' ? 'b' : 'a',
158
+ by = w.join[way],
159
+ ends = w.all[other].filter((c) => c.item),
160
+ caps = w.all[way].filter((c) => c.item);
90
161
  const closers = [...new Map(w.all[way].filter((c) => c.join).map((c) => [c.join.index, c])).values()];
91
- const end = closers.find((c) => c.end)?.end ?? null, heads = closers.filter((c) => c.head);
92
- const from = closers.length > 1 || !end ? `from end ${way}${heads.length ? `, past ${list(heads.map((c) => `${mm(c.at)}`).reverse())} mm` : ''}` : `through its end ${end}`;
162
+ const end = closers.find((c) => c.end)?.end ?? null,
163
+ heads = closers.filter((c) => c.head);
164
+ const from =
165
+ closers.length > 1 || !end
166
+ ? `from end ${way}${heads.length ? `, past ${list(heads.map((c) => `${mm(c.at)}`).reverse())} mm` : ''}`
167
+ : `through its end ${end}`;
93
168
  const capped = caps.length ? ` (${list(caps.map((c) => c.item))} off until then)` : '';
94
- const before = closers.length > 1 ? `before the first made of ${list(closers.map((c) => `the ${c.join.label}`).reverse())}, each of which closes that way` : `before the ${by.join.label} closes it`;
95
- const or = ends.length ? ` (or through end ${other} before ${list(ends.map((c) => c.item))} go${ends.length > 1 ? '' : 'es'} on)` : '';
169
+ const before =
170
+ closers.length > 1
171
+ ? `before the first made of ${list(closers.map((c) => `the ${c.join.label}`).reverse())}, each of which closes that way`
172
+ : `before the ${by.join.label} closes it`;
173
+ const or = ends.length
174
+ ? ` (or through end ${other} before ${list(ends.map((c) => c.item))} go${ends.length > 1 ? '' : 'es'} on)`
175
+ : '';
96
176
  const how = `${from}${capped}, ${before}${or}`;
97
- out.push({ fasten: g.rel.index, label: g.rel.label, item: g.item, place: g.place, named: g.named, part: g.part, nut: g.nut, count: g.at.length, end: by.end, way, at: by.at, join: by.join, closers: closers.map((c) => c.join), other: ends[0] ?? null, how,
98
- text: `load ${g.at.length} × ${g.nut} into ${g.item}'s ${g.named} slot ${from}${capped}, for ${g.part}, ${before}${or}` });
177
+ out.push({
178
+ fasten: g.rel.index,
179
+ label: g.rel.label,
180
+ item: g.item,
181
+ place: g.place,
182
+ named: g.named,
183
+ part: g.part,
184
+ nut: g.nut,
185
+ count: g.at.length,
186
+ end: by.end,
187
+ way,
188
+ at: by.at,
189
+ join: by.join,
190
+ closers: closers.map((c) => c.join),
191
+ other: ends[0] ?? null,
192
+ how,
193
+ text: `load ${g.at.length} × ${g.nut} into ${g.item}'s ${g.named} slot ${from}${capped}, for ${g.part}, ${before}${or}`,
194
+ });
99
195
  }
100
196
  return out;
101
197
  }
@@ -111,7 +207,21 @@ export function trappedOf(s) {
111
207
  for (const g of nutGroupsOf(s)) {
112
208
  if (g.entry !== 'end') continue;
113
209
  const w = waysOf(s, g.item, g.place, Math.min(...g.at), Math.max(...g.at));
114
- if (w.join.a && w.join.b) out.push({ fasten: g.rel.index, label: g.rel.label, item: g.item, place: g.place, named: g.named, nut: g.nut, kind: g.kind, count: g.at.length, part: g.part, at: g.at, joins: [w.join.a.join, w.join.b.join], alternative: throughNut(s, g) });
210
+ if (w.join.a && w.join.b)
211
+ out.push({
212
+ fasten: g.rel.index,
213
+ label: g.rel.label,
214
+ item: g.item,
215
+ place: g.place,
216
+ named: g.named,
217
+ nut: g.nut,
218
+ kind: g.kind,
219
+ count: g.at.length,
220
+ part: g.part,
221
+ at: g.at,
222
+ joins: [w.join.a.join, w.join.b.join],
223
+ alternative: throughNut(s, g),
224
+ });
115
225
  }
116
226
  return out;
117
227
  }
@@ -123,22 +233,90 @@ export function slotNutChecks(s, f) {
123
233
  const groups = nutGroupsOf(s);
124
234
  if (!groups.length) return;
125
235
  const pre = preloadOf(s);
236
+ for (const c of crowdedOf(s, groups)) {
237
+ const { g, at, span } = c.nut,
238
+ where = `${g.item}'s ${g.named} slot`,
239
+ sc = g.screws[c.nut.i],
240
+ len = span[1] - span[0];
241
+ const what = c.head
242
+ ? `the head of the ${c.head.join.label}, which sits in that slot at ${mm(c.head.at)} mm${c.across ? ` (${mm(c.across)} mm across)` : ''}, by ${mm(c.by)} mm`
243
+ : `the nut at ${mm(c.other.at)} mm${c.other.g.rel !== g.rel ? ` (${c.other.g.rel.label})` : ''}, by ${mm(c.by)} mm`;
244
+ f(
245
+ 'fail',
246
+ `${g.rel.label}: the nut at ${mm(at)} mm in ${where} (${g.nut}, ${mm(len)} mm long, from ${mm(span[0])} to ${mm(span[1])} mm along it) runs into ${what}: two things cannot take the same stretch of slot. Move ${g.part}'s screw at least ${mm(c.by)} mm along, or ${c.head ? 'the join' : "space the screws a nut's length apart"}`,
247
+ `${g.rel.label}: nut`,
248
+ {
249
+ code: 'frame.nut',
250
+ at: sc.at.map(r3),
251
+ values: { along: at, span: span.map(r3), by: r3(c.by) },
252
+ refs: {
253
+ fasten: g.rel.index,
254
+ item: g.item,
255
+ place: g.place,
256
+ nut: g.nut,
257
+ ...(c.head ? { join: c.head.join.index } : { other: { fasten: c.other.g.rel.index, along: c.other.at } }),
258
+ },
259
+ },
260
+ );
261
+ }
126
262
  for (const g of groups) {
127
- const lo = Math.min(...g.at), hi = Math.max(...g.at), where = `${g.item}'s ${g.named} slot`, first = g.screws[0];
263
+ const lo = Math.min(...g.at),
264
+ hi = Math.max(...g.at),
265
+ where = `${g.item}'s ${g.named} slot`,
266
+ first = g.screws[0];
128
267
  const refs = { fasten: g.rel.index, item: g.item, place: g.place, nut: g.nut, kind: g.kind, along: g.at };
129
- if (g.entry !== 'end') { f('pass', `${g.rel.label}: ${nutsSay(g)} go${g.at.length > 1 ? '' : 'es'} into ${where} through its opening, anywhere along it`, null, { code: 'frame.nut', at: first.at.map(r3), refs }); continue; }
268
+ if (g.entry !== 'end') {
269
+ f(
270
+ 'pass',
271
+ `${g.rel.label}: ${nutsSay(g)} go${g.at.length > 1 ? '' : 'es'} into ${where} through its opening, anywhere along it`,
272
+ null,
273
+ { code: 'frame.nut', at: first.at.map(r3), refs },
274
+ );
275
+ continue;
276
+ }
130
277
  const w = waysOf(s, g.item, g.place, lo, hi);
131
278
  if (w.join.a && w.join.b) {
132
- const alt = throughNut(s, g), joins = [w.join.a.join.index, w.join.b.join.index];
133
- f('fail', `${g.rel.label}: ${nutsSay(g)} go${g.at.length > 1 ? '' : 'es'} into ${where} at ${g.at.map(mm).join(' and ')} mm, between ${closerSays(g, w.join.a)} and ${closerSays(g, w.join.b)}, which close it at both ends: a ${g.kind} nut goes in only from an open end. ${alt ? `${alt.name} (${an(alt.thread)} ${alt.thread} ${alt.nutKind} nut) goes in through the opening anywhere: give "nut": "${alt.nutKind}"` : `No ${g.thread} nut made for ${g.slot} goes in through its opening: load these before the later of the two joins, or move them to a slot with an open end`}`,
134
- `${g.rel.label}: nut`, { code: 'frame.nut', at: first.at.map(r3), values: { nuts: g.at.length }, refs: { ...refs, joins, ...(alt ? { alternative: alt.name, kind_needed: alt.nutKind } : {}) } });
279
+ const alt = throughNut(s, g),
280
+ joins = [w.join.a.join.index, w.join.b.join.index];
281
+ f(
282
+ 'fail',
283
+ `${g.rel.label}: ${nutsSay(g)} go${g.at.length > 1 ? '' : 'es'} into ${where} at ${g.at.map(mm).join(' and ')} mm, between ${closerSays(g, w.join.a)} and ${closerSays(g, w.join.b)}, which close it at both ends: a ${g.kind} nut goes in only from an open end. ${alt ? `${alt.name} (${an(alt.thread)} ${alt.thread} ${alt.nutKind} nut) goes in through the opening anywhere: give "nut": "${alt.nutKind}"` : `No ${g.thread} nut made for ${g.slot} goes in through its opening: load these before the later of the two joins, or move them to a slot with an open end`}`,
284
+ `${g.rel.label}: nut`,
285
+ {
286
+ code: 'frame.nut',
287
+ at: first.at.map(r3),
288
+ values: { nuts: g.at.length },
289
+ refs: { ...refs, joins, ...(alt ? { alternative: alt.name, kind_needed: alt.nutKind } : {}) },
290
+ },
291
+ );
135
292
  continue;
136
293
  }
137
- const open = !w.a ? 'a' : !w.b ? 'b' : null, pl = open ? null : pre.find((p) => p.fasten === g.rel.index && p.item === g.item && p.place === g.place);
138
- f('pass', `${g.rel.label}: ${nutsSay(g)} go${g.at.length > 1 ? '' : 'es'} into ${where} ${open ? `from its end ${open}, which nothing closes` : pl ? `before a join closes its way, ${pl.how} (frame.preload)` : `from an end before ${list(['a', 'b'].map((e) => w.all[e].filter((c) => c.item).map((c) => c.item)).flat())} go on`}`, null, { code: 'frame.nut', at: first.at.map(r3), refs });
294
+ const open = !w.a ? 'a' : !w.b ? 'b' : null,
295
+ pl = open ? null : pre.find((p) => p.fasten === g.rel.index && p.item === g.item && p.place === g.place);
296
+ f(
297
+ 'pass',
298
+ `${g.rel.label}: ${nutsSay(g)} go${g.at.length > 1 ? '' : 'es'} into ${where} ${open ? `from its end ${open}, which nothing closes` : pl ? `before a join closes its way, ${pl.how} (frame.preload)` : `from an end before ${list(['a', 'b'].map((e) => w.all[e].filter((c) => c.item).map((c) => c.item)).flat())} go on`}`,
299
+ null,
300
+ { code: 'frame.nut', at: first.at.map(r3), refs },
301
+ );
139
302
  }
140
- for (const p of pre) f('info', `${p.label}: ${p.text}`, null, { code: 'frame.preload', values: { nuts: p.count }, refs: { fasten: p.fasten, item: p.item, place: p.place, nut: p.nut, join: p.join.index, joins: p.closers.map((j) => j.index), end: p.end, way: p.way } });
141
- if (!pre.length) f('pass', 'no slot nut has to be in its slot before a join closes it', null, { code: 'frame.preload' });
303
+ for (const p of pre)
304
+ f('info', `${p.label}: ${p.text}`, null, {
305
+ code: 'frame.preload',
306
+ values: { nuts: p.count },
307
+ refs: {
308
+ fasten: p.fasten,
309
+ item: p.item,
310
+ place: p.place,
311
+ nut: p.nut,
312
+ join: p.join.index,
313
+ joins: p.closers.map((j) => j.index),
314
+ end: p.end,
315
+ way: p.way,
316
+ },
317
+ });
318
+ if (!pre.length)
319
+ f('pass', 'no slot nut has to be in its slot before a join closes it', null, { code: 'frame.preload' });
142
320
  }
143
321
 
144
322
  /**
@@ -150,7 +328,8 @@ export function withPreload(s, r) {
150
328
  if (!r?.steps?.length) return r;
151
329
  const pre = preloadOf(s);
152
330
  if (!pre.length) return r;
153
- const steps = r.steps.map((st) => ({ ...st })), stepOf = (id) => steps.findIndex((st) => st.items.includes(id));
331
+ const steps = r.steps.map((st) => ({ ...st })),
332
+ stepOf = (id) => steps.findIndex((st) => st.items.includes(id));
154
333
  for (const p of pre) {
155
334
  const i = Math.min(...p.closers.map((j) => Math.max(0, ...j.items.map(stepOf)))); // (the step that makes the first of the joins on the way)
156
335
  (steps[i].load ||= []).push(p.text);
@@ -2,6 +2,9 @@
2
2
  import { execFileSync } from 'node:child_process';
3
3
 
4
4
  export async function shorten(data, { desc }) {
5
- const out = execFileSync('gh', ['gist', 'create', '--filename', 'model.txt', '--desc', desc, '-'], { input: data, encoding: 'utf8' }).trim();
5
+ const out = execFileSync('gh', ['gist', 'create', '--filename', 'model.txt', '--desc', desc, '-'], {
6
+ input: data,
7
+ encoding: 'utf8',
8
+ }).trim();
6
9
  return out.split('/').pop();
7
10
  }
package/lib/targets.mjs CHANGED
@@ -7,6 +7,7 @@
7
7
  // mesh: { polygons } (one solid) or { shapes, place } (each shape once, then a 3 × 4 transform per copy);
8
8
  // card: { title, items } (short lines) to go with it
9
9
  // shorten: export async function shorten(data, { desc }) stores a model link's data and returns an ID for #s=<id>
10
+ import fs from 'node:fs';
10
11
  import path from 'node:path';
11
12
  import { pathToFileURL } from 'node:url';
12
13
  import { WS, config, needTrust, isOwnTarget, OWN_TARGETS } from './workspace.mjs';
@@ -27,12 +28,29 @@ export async function target(kind) {
27
28
  if (isOwnTarget(name)) href = OWN_TARGETS[name];
28
29
  else {
29
30
  needTrust(); // (the workspace's own code, run in the tool's process: only once it is trusted)
30
- if (typeof name !== 'string') throw new Error(`the ${kind} target in cad.config.json should be the name of a module file, not ${JSON.stringify(name)}`);
31
- const file = path.resolve(WS, name), rel = path.relative(WS, file);
31
+ if (typeof name !== 'string')
32
+ throw new Error(
33
+ `the ${kind} target in cad.config.json should be the name of a module file, not ${JSON.stringify(name)}`,
34
+ );
35
+ // (by real paths, as design.mjs mayRunHere: /var and /private/var are one folder, and a link out is out)
36
+ const real = (f) => {
37
+ try {
38
+ return fs.realpathSync(f);
39
+ } catch {
40
+ return path.resolve(f);
41
+ }
42
+ },
43
+ file = path.resolve(WS, name),
44
+ rel = path.relative(real(WS), real(file));
32
45
  if (!rel || rel === '..' || rel.startsWith(`..${path.sep}`) || path.isAbsolute(rel)) {
33
- throw Object.assign(new Error(`the ${kind} target ${name} (cad.config.json) is outside this workspace (${WS}), so it isn't run. Put the module in the workspace's folder and name it from there`), { code: 'workspace.outside' });
46
+ throw Object.assign(
47
+ new Error(
48
+ `the ${kind} target ${name} (cad.config.json) is outside this workspace (${WS})${real(file) !== file ? `: it leads to ${real(file)}` : ''}, so it isn't run. Put the module in the workspace's folder and name it from there (a copy, not a link to one elsewhere)`,
49
+ ),
50
+ { code: 'workspace.outside' },
51
+ );
34
52
  }
35
- href = pathToFileURL(file).href;
53
+ href = pathToFileURL(real(file)).href;
36
54
  }
37
55
  const mod = await import(href);
38
56
  if (typeof mod[kind] !== 'function') throw new Error(`target ${name} has no ${kind}()`);