@rulecad/cad 0.7.97 → 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 (86) hide show
  1. package/AGENTS.md +26 -10
  2. package/CHANGELOG.md +442 -2
  3. package/EXAMPLES.md +48 -42
  4. package/README.md +226 -73
  5. package/TUTORIAL.md +2 -2
  6. package/bin/cad.mjs +2253 -514
  7. package/lib/animate.mjs +173 -49
  8. package/lib/assembly.mjs +1854 -437
  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 +734 -113
  17. package/lib/drive.mjs +187 -63
  18. package/lib/edit.mjs +900 -191
  19. package/lib/errors.mjs +16 -3
  20. package/lib/explain.mjs +62 -13
  21. package/lib/fasten.mjs +2710 -502
  22. package/lib/fittings.mjs +715 -121
  23. package/lib/fixings.mjs +21 -9
  24. package/lib/framebuild.mjs +792 -176
  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 +149 -18
  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 +3 -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 +1022 -212
  55. package/lib/motorsvg.mjs +114 -32
  56. package/lib/names.mjs +41 -0
  57. package/lib/optimise.mjs +979 -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 +224 -36
  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 +38 -4
  68. package/lib/sandbox-hooks.mjs +154 -12
  69. package/lib/sandbox-preload.mjs +2 -2
  70. package/lib/sandbox-run.mjs +127 -12
  71. package/lib/sandbox.mjs +632 -35
  72. package/lib/schema.mjs +43 -11
  73. package/lib/seats.mjs +227 -49
  74. package/lib/services.mjs +261 -52
  75. package/lib/sheet.mjs +851 -178
  76. package/lib/slotnuts.mjs +213 -34
  77. package/lib/targets/gist.mjs +4 -1
  78. package/lib/targets.mjs +42 -6
  79. package/lib/task.mjs +376 -79
  80. package/lib/versions.mjs +113 -28
  81. package/lib/wires.mjs +228 -45
  82. package/lib/workspace.mjs +216 -17
  83. package/package.json +19 -19
  84. package/schemas/assembly.schema.json +2 -2
  85. package/schemas/object.schema.json +1 -1
  86. package/schemas/project.schema.json +1 -1
package/lib/framegen.mjs CHANGED
@@ -9,7 +9,18 @@ import { beamPlace, framesContext } from './beams.mjs';
9
9
  import { givenFor } from './frames.mjs';
10
10
 
11
11
  /** The keys of "frame" that ask for a generated frame; the rest (supports, limit, rack ..) say how it is solved. */
12
- export const FRAME_GEN_KEYS = ['kind', 'inside', 'outside', 'profile', 'join', 'feet', 'caps', 'braces', 'panels', 'shelves'];
12
+ export const FRAME_GEN_KEYS = [
13
+ 'kind',
14
+ 'inside',
15
+ 'outside',
16
+ 'profile',
17
+ 'join',
18
+ 'feet',
19
+ 'caps',
20
+ 'braces',
21
+ 'panels',
22
+ 'shelves',
23
+ ];
13
24
  const shown = (v) => (v === undefined ? 'nothing' : JSON.stringify(v));
14
25
  const r3 = (x) => +x.toFixed(3);
15
26
 
@@ -24,21 +35,36 @@ export function frameOf(a, num, context = framesContext()) {
24
35
  if (!f || typeof f !== 'object' || Array.isArray(f)) return null;
25
36
  if (f.kind == null) {
26
37
  const stray = FRAME_GEN_KEYS.filter((k) => f[k] != null);
27
- if (stray.length) throw new Error(`frame: ${stray.join(', ')} ask${stray.length === 1 ? 's' : ''} for a generated frame, and it names no kind: give "kind": ${Object.keys(FRAME_KINDS).join(', ')}`);
38
+ if (stray.length)
39
+ throw new Error(
40
+ `frame: ${stray.join(', ')} ask${stray.length === 1 ? 's' : ''} for a generated frame, and it names no kind: give "kind": ${Object.keys(FRAME_KINDS).join(', ')}`,
41
+ );
28
42
  return null;
29
43
  }
30
44
  const o = {};
31
45
  for (const k of FRAME_GEN_KEYS.slice(1)) if (f[k] != null) o[k] = f[k];
32
- for (const k of ['inside', 'outside']) if (o[k] != null) { if (!Array.isArray(o[k])) throw new Error(`frame: ${k} is [width, depth, height] in mm (got ${shown(o[k])})`); o[k] = o[k].map((x) => num(x)); }
46
+ for (const k of ['inside', 'outside'])
47
+ if (o[k] != null) {
48
+ if (!Array.isArray(o[k])) throw new Error(`frame: ${k} is [width, depth, height] in mm (got ${shown(o[k])})`);
49
+ o[k] = o[k].map((x) => num(x));
50
+ }
33
51
  if (o.panels?.thickness != null) o.panels = { ...o.panels, thickness: num(o.panels.thickness) };
34
52
  let m;
35
- try { m = frameModel(f.kind, { ...o, given: givenFor(context, f.profile) }); } catch (e) { throw new Error(`frame: ${String(e.message).replace(/^frameModel: /, '')}`); }
53
+ try {
54
+ m = frameModel(f.kind, { ...o, given: givenFor(context, f.profile) });
55
+ } catch (e) {
56
+ throw new Error(`frame: ${String(e.message).replace(/^frameModel: /, '')}`);
57
+ }
36
58
  // a portal lies in one plane: standing on its two feet, a foot holds an upright's end from moving and not from turning,
37
59
  // so nothing holds it out of that plane and it tips over sideways under any load. It checks clean once its feet are
38
60
  // held down (bolted to a bench or a wall), so where nothing says how it is held, it is refused here, before any check
39
61
  if (m.kind === 'portal' && f.supports == null) {
40
- const feet = m.beams.filter((b) => Math.min(b.from[2], b.to[2]) < 1e-6).map((b) => `"${b.id}.${b.from[2] <= b.to[2] ? 'a' : 'b'}": "fixed"`);
41
- throw new Error(`frame: a portal stands in one plane (x-z) on its two feet, and a foot stops an upright's end moving but not turning, so nothing holds the portal out of that plane: on its feet alone it tips over sideways (frame.buckling fails it, frame.rack finds it free along y). Say how its feet are held down, bolted to a bench or a wall: "supports": { ${feet.join(', ')} }; or, to stand free, generate a table or a box`);
62
+ const feet = m.beams
63
+ .filter((b) => Math.min(b.from[2], b.to[2]) < 1e-6)
64
+ .map((b) => `"${b.id}.${b.from[2] <= b.to[2] ? 'a' : 'b'}": "fixed"`);
65
+ throw new Error(
66
+ `frame: a portal stands in one plane (x-z) on its two feet, and a foot stops an upright's end moving but not turning, so nothing holds the portal out of that plane: on its feet alone it tips over sideways (frame.buckling fails it, frame.rack finds it free along y). Say how its feet are held down, bolted to a bench or a wall: "supports": { ${feet.join(', ')} }; or, to stand free, generate a table or a box`,
67
+ );
42
68
  }
43
69
  return m;
44
70
  }
@@ -50,27 +76,57 @@ export function frameOf(a, num, context = framesContext()) {
50
76
  * @returns {{ items: object[], joins: object[], fasten: object[], joints: object[], poses: Record<string, object> | null }}
51
77
  */
52
78
  export function frameParts(m, a, num) {
53
- const profile = m.profile, items = [], fasten = [];
79
+ const profile = m.profile,
80
+ items = [],
81
+ fasten = [];
54
82
  for (const b of m.beams) items.push({ id: b.id, beam: { profile }, from: b.from, to: b.to });
55
83
  for (const e of m.ends) {
56
- items.push(e.fitting ? { id: e.id, fitting: { kind: e.fitting, profile }, at: e.at } : { id: e.id, bought: e.bought, at: e.at });
84
+ items.push(
85
+ e.fitting
86
+ ? { id: e.id, fitting: { kind: e.fitting, profile, ...(e.with ?? {}) }, at: e.at }
87
+ : { id: e.id, bought: e.bought, at: e.at },
88
+ );
57
89
  if (!e.tap) continue;
58
- const [beamId] = e.at.split('.'), bi = items.find((x) => x.id === beamId);
90
+ const [beamId] = e.at.split('.'),
91
+ bi = items.find((x) => x.id === beamId);
59
92
  bi.beam.ops = [...(bi.beam.ops ?? []), { ...e.tap }];
60
- fasten.push({ screw: e.screw, through: [e.id], into: beamId, at: [`${e.id}.b1`], note: `${e.id} screwed into ${beamId}'s end ${e.tap.at}` });
93
+ fasten.push({
94
+ screw: e.screw,
95
+ through: [e.id],
96
+ into: beamId,
97
+ at: [`${e.id}.b1`],
98
+ note: `${e.id} screwed into ${beamId}'s end ${e.tap.at}`,
99
+ });
61
100
  }
62
- for (const p of m.panels) items.push({ id: p.id, panel: { sheet: p.sheet, thickness: p.thickness, bay: p.bay, out: p.out, nut: p.nut } });
101
+ for (const p of m.panels)
102
+ items.push({ id: p.id, panel: { sheet: p.sheet, thickness: p.thickness, bay: p.bay, out: p.out, nut: p.nut } });
63
103
  const joints = [];
64
104
  let poses = null;
65
105
  if (m.moving) {
66
106
  // the moving beam on a sliding joint from its track's top face: both anchors where g's bottom meets the track at the
67
107
  // start of its travel, z along the travel, x up, so g lies with its top up and its length across
68
- const g = m.moving, track = items.find((x) => x.id === g.track), F = beamPlace(track, num), h = m.width / 2;
69
- const col = (k) => [F[k], F[4 + k], F[8 + k]], o = [F[3], F[7], F[11]], dot = (u, v) => u[0] * v[0] + u[1] * v[1] + u[2] * v[2];
70
- const local = (v, point = true) => { const d = point ? v.map((c, k) => c - o[k]) : v; return [0, 1, 2].map((k) => r3(dot(col(k), d))); };
108
+ const g = m.moving,
109
+ track = items.find((x) => x.id === g.track),
110
+ F = beamPlace(track, num),
111
+ h = m.width / 2;
112
+ const col = (k) => [F[k], F[4 + k], F[8 + k]],
113
+ o = [F[3], F[7], F[11]],
114
+ dot = (u, v) => u[0] * v[0] + u[1] * v[1] + u[2] * v[2];
115
+ const local = (v, point = true) => {
116
+ const d = point ? v.map((c, k) => c - o[k]) : v;
117
+ return [0, 1, 2].map((k) => r3(dot(col(k), d)));
118
+ };
71
119
  const meet = [track.from[0], g.from[1], g.from[2] - h];
72
120
  items.splice(m.beams.length, 0, { id: g.id, beam: { profile, length: g.length } });
73
- joints.push({ id: 'gantry', type: 'prismatic', parent: { item: track.id, at: local(meet), axis: local(g.along, false), ref: local([0, 0, 1], false) }, child: { item: g.id, at: [0, -h, r3(track.from[0] - g.from[0])], axis: [1, 0, 0], ref: [0, 1, 0] }, min: 0, max: g.travel, label: 'Gantry' });
121
+ joints.push({
122
+ id: 'gantry',
123
+ type: 'prismatic',
124
+ parent: { item: track.id, at: local(meet), axis: local(g.along, false), ref: local([0, 0, 1], false) },
125
+ child: { item: g.id, at: [0, -h, r3(track.from[0] - g.from[0])], axis: [1, 0, 0], ref: [0, 1, 0] },
126
+ min: 0,
127
+ max: g.travel,
128
+ label: 'Gantry',
129
+ });
74
130
  poses = { 'gantry-front': { gantry: 0 }, 'gantry-back': { gantry: g.travel } };
75
131
  }
76
132
  return { items, joins: m.joins.map((j) => ({ ...j })), fasten, joints, poses };
@@ -86,16 +142,29 @@ export function frameParts(m, a, num) {
86
142
  export function expandFrame(a, num, context = framesContext()) {
87
143
  const m = frameOf(a, num, context);
88
144
  if (!m) return { a, frame: null };
89
- const parts = frameParts(m, a, num), own = new Set((a.items || []).map((it) => it?.id));
145
+ const parts = frameParts(m, a, num),
146
+ own = new Set((a.items || []).map((it) => it?.id));
90
147
  const clash = parts.items.filter((it) => own.has(it.id)).map((it) => it.id);
91
- if (clash.length) throw new Error(`frame: the ${m.kind} names its parts ${parts.items.slice(0, 3).map((x) => x.id).join(', ')} .., and this assembly already has ${clash.join(', ')}: rename ${clash.length > 1 ? 'them' : 'it'}, or write the frame out (cad frame --expand) and edit it there`);
92
- if (parts.joints.length && (a.joints || []).some((j) => j.id === 'gantry')) throw new Error('frame: a gantry\'s moving beam slides on a joint named gantry, and this assembly already has one: rename it');
148
+ if (clash.length)
149
+ throw new Error(
150
+ `frame: the ${m.kind} names its parts ${parts.items
151
+ .slice(0, 3)
152
+ .map((x) => x.id)
153
+ .join(
154
+ ', ',
155
+ )} .., and this assembly already has ${clash.join(', ')}: rename ${clash.length > 1 ? 'them' : 'it'}, or write the frame out (cad frame --expand) and edit it there`,
156
+ );
157
+ if (parts.joints.length && (a.joints || []).some((j) => j.id === 'gantry'))
158
+ throw new Error(
159
+ "frame: a gantry's moving beam slides on a joint named gantry, and this assembly already has one: rename it",
160
+ );
93
161
  const rest = Object.fromEntries(Object.entries(a.frame).filter(([k]) => !FRAME_GEN_KEYS.includes(k)));
94
162
  const out = { ...a, items: [...parts.items, ...(a.items || [])], joins: [...parts.joins, ...(a.joins || [])] };
95
163
  if (parts.fasten.length) out.fasten = [...parts.fasten, ...(a.fasten || [])];
96
164
  if (parts.joints.length) out.joints = [...parts.joints, ...(a.joints || [])];
97
165
  if (parts.poses && !a.poses) out.poses = parts.poses;
98
- if (Object.keys(rest).length) out.frame = rest; else delete out.frame;
166
+ if (Object.keys(rest).length) out.frame = rest;
167
+ else delete out.frame;
99
168
  return { a: out, frame: m };
100
169
  }
101
170
 
@@ -103,9 +172,18 @@ export function expandFrame(a, num, context = framesContext()) {
103
172
  export function frameText(m) {
104
173
  const s = (v) => v.map((x) => +x.toFixed(2)).join(' x ');
105
174
  const size = m.kind === 'portal' ? (v) => s([v[0], v[2]]) : s;
106
- const lines = [`a ${m.kind} of ${m.profile} (${m.width} mm), joined ${m.join.kind}${m.join.fitting ? ` (${m.join.fitting})` : ''}: ${m.arrangement === 'face' ? 'the upright beams run through and the rest butt against their faces' : 'every beam butts into a corner node'}`,
107
- `inside ${size(m.inside)} mm, outside ${size(m.outside)} mm${m.shelves ? `; ${m.shelves} shelves, each bay ${+m.bay.toFixed(2)} mm clear` : ''}${m.lift ? `; the bottom ring stands ${+m.lift.toFixed(2)} mm up the uprights, so its joins' holes clear the feet's tapped ends` : ''}`];
175
+ const lines = [
176
+ `a ${m.kind} of ${m.profile} (${m.width} mm), joined ${m.join.kind}${m.join.fitting ? ` (${m.join.fitting})` : ''}: ${m.arrangement === 'face' ? 'the upright beams run through and the rest butt against their faces' : 'every beam butts into a corner node'}`,
177
+ `inside ${size(m.inside)} mm, outside ${size(m.outside)} mm${m.shelves ? `; ${m.shelves} shelves, each bay ${+m.bay.toFixed(2)} mm clear` : ''}${m.lift ? `; the bottom ring stands ${+m.lift.toFixed(2)} mm up the uprights, so its joins' holes clear the feet's tapped ends` : ''}`,
178
+ ];
108
179
  for (const l of m.lengths) lines.push(` ${l.count} x ${+l.length.toFixed(2)} mm: ${l.ids.join(', ')}`);
109
- lines.push(`${m.joins.length} joins (${[...new Set(m.joins.map((j) => j.kind))].join(', ')}); ${m.ends.length} end fittings${m.ends.length ? ` (${[...new Set(m.ends.map((e) => e.fitting ?? e.bought))].join(', ')})` : ''}${m.panels.length ? `; ${m.panels.length} panels` : ''}${m.moving ? `; g rides ${m.moving.track} through ${m.moving.travel} mm on the joint gantry` : ''}`);
180
+ // (a brace is a printed bracket added beside the join asked for at each corner of a braced side, or, where the join is
181
+ // a printed bracket already, its web)
182
+ const braced = m.braced ?? 0,
183
+ webbed = m.join.kind === 'bracket' && m.join.fitting === 'printed',
184
+ asked = m.joins.length - (webbed ? 0 : braced);
185
+ lines.push(
186
+ `${asked} joins (${m.join.kind})${braced ? (webbed ? `, ${braced} of them braced: their brackets drawn with a web across the inside corner` : `, ${braced} braced with printed brackets`) : ''}; ${m.ends.length} end fittings${m.ends.length ? ` (${[...new Set(m.ends.map((e) => e.fitting ?? e.bought))].join(', ')})` : ''}${m.panels.length ? `; ${m.panels.length} panels` : ''}${m.moving ? `; g rides ${m.moving.track} through ${m.moving.travel} mm on the joint gantry` : ''}`,
187
+ );
110
188
  return lines;
111
189
  }
package/lib/frameheat.mjs CHANGED
@@ -1,7 +1,8 @@
1
- // frame.heat: a frame that runs warm. Its beams grow with the air round them by their alloy's expansion, so a frame
2
- // squared and measured at the workshop's temperature is larger when it runs in a warm enclosure; and each printed
3
- // fitting is held to its plastic's softening point (the heat deflection temperature its set gives), at the air's
4
- // temperature or at a heater's where it is within that heater's reach.
1
+ // frame.heat: a frame that runs warm. Its beams grow with the air round them by their material's expansion (the
2
+ // expansion rule of its alloy's or plastic's set), so a frame squared and measured at the workshop's temperature is
3
+ // larger when it runs in a warm enclosure; and each printed fitting and each printed beam is held to its plastic's
4
+ // softening point (the heat deflection temperature its set gives), at the air's temperature or at a heater's where it
5
+ // is within that heater's reach.
5
6
  // "frame": { "heat": { "air": 45, "built": 20, "heaters": [{ "name": "bed", "at": [150, 150, 60], "temperature": 60, "reach": 40 }] } }
6
7
  // Growth: ΔL = α L ΔT, with ΔT = air - built, for each beam and for the frame's extent each way.
7
8
  import { placeItems } from '@rulecad/model-link';
@@ -12,36 +13,96 @@ const shown = (v) => JSON.stringify(v) ?? String(v);
12
13
 
13
14
  /** The working figures of frame.heat, each with what it rests on. */
14
15
  export const FRAME_HEAT = {
15
- expansion: { value: 23.4e-6, unit: '1/K', rule: 'How much an aluminium beam grows for each degree it warms, a share of its length, where its alloy\'s set gives no expansion: the figure for 6063, taken for the 6000 series extrusions the profiles are made of', kind: 'community', source: '6063 aluminium alloy, Wikipedia\'s table (2.34e-5 /K); 6063-T5 suppliers\' sheets give 23.4 µm/m °C over 20 to 100 °C (Alliance, Aerospace Metals) and Hydro\'s EN AW-6063 sheet 23.5, found by search and not each opened', checked: '2026-11-17' },
16
- built: { value: 20, unit: '°C', rule: 'The air a frame is built, squared and measured in, where the assembly does not say', kind: 'default', source: 'A working figure of @rulecad/cad: a room', checked: '2026-11-17' },
17
- margin: { value: 10, unit: '°C', rule: 'A printed fitting within this many degrees under its plastic\'s softening point is warned of: the heat deflection temperature is measured under a light load (0.45 MPa), and a fitting under its working load creeps sooner', kind: 'estimate', source: 'A working figure of @rulecad/cad, without a test behind it', checked: '2026-11-17' },
16
+ expansion: {
17
+ value: 23.4e-6,
18
+ unit: '1/K',
19
+ rule: "How much a beam grows for each degree it warms, a share of its length, where its material's set gives no expansion (every set of the kit's alloys and plastics gives one; a workspace's own may not): the figure for 6063, taken for the 6000 series extrusions the profiles are made of",
20
+ kind: 'community',
21
+ source:
22
+ "6063 aluminium alloy, Wikipedia's table (2.34e-5 /K); 6063-T5 suppliers' sheets give 23.4 µm/m °C over 20 to 100 °C (Alliance, Aerospace Metals) and Hydro's EN AW-6063 sheet 23.5, found by search and not each opened",
23
+ checked: '2026-11-17',
24
+ },
25
+ built: {
26
+ value: 20,
27
+ unit: '°C',
28
+ rule: 'The air a frame is built, squared and measured in, where the assembly does not say',
29
+ kind: 'default',
30
+ source: 'A working figure of @rulecad/cad: a room',
31
+ checked: '2026-11-17',
32
+ },
33
+ margin: {
34
+ value: 10,
35
+ unit: '°C',
36
+ rule: "A printed fitting or beam within this many degrees under its plastic's softening point is warned of: the heat deflection temperature is measured under a light load (0.45 MPa), and a fitting under its working load creeps sooner",
37
+ kind: 'estimate',
38
+ source: 'A working figure of @rulecad/cad, without a test behind it',
39
+ checked: '2026-11-17',
40
+ },
18
41
  };
19
42
 
20
43
  // frame.heat as written, checked by name: null where it is not given
21
44
  function heatOf(s) {
22
45
  const h = s.a.frame?.heat;
23
46
  if (h == null) return null;
24
- if (typeof h !== 'object' || Array.isArray(h)) throw new Error(`frame.heat is { "air": 45, "built": 20, "heaters": [...] }: degrees C (got ${shown(h)})`);
25
- for (const k of Object.keys(h)) if (!['air', 'built', 'heaters'].includes(k)) throw new Error(`frame.heat has no "${k}": it takes air, built and heaters`);
26
- const deg = (v, what) => { const x = s.num(v); if (!(Number.isFinite(x) && x > -100 && x < 500)) throw new Error(`frame.heat.${what} is a temperature in degrees C (got ${shown(v)})`); return x; };
27
- const air = h.air != null ? deg(h.air, 'air') : null, built = h.built != null ? deg(h.built, 'built') : FRAME_HEAT.built.value;
28
- if (h.heaters != null && !Array.isArray(h.heaters)) throw new Error(`frame.heat.heaters is a list of { name, at, temperature, reach } (got ${shown(h.heaters)})`);
47
+ if (typeof h !== 'object' || Array.isArray(h))
48
+ throw new Error(`frame.heat is { "air": 45, "built": 20, "heaters": [...] }: degrees C (got ${shown(h)})`);
49
+ for (const k of Object.keys(h))
50
+ if (!['air', 'built', 'heaters'].includes(k))
51
+ throw new Error(`frame.heat has no "${k}": it takes air, built and heaters`);
52
+ const deg = (v, what) => {
53
+ const x = s.num(v);
54
+ if (!(Number.isFinite(x) && x > -100 && x < 500))
55
+ throw new Error(`frame.heat.${what} is a temperature in degrees C (got ${shown(v)})`);
56
+ return x;
57
+ };
58
+ const air = h.air != null ? deg(h.air, 'air') : null,
59
+ built = h.built != null ? deg(h.built, 'built') : FRAME_HEAT.built.value;
60
+ if (h.heaters != null && !Array.isArray(h.heaters))
61
+ throw new Error(`frame.heat.heaters is a list of { name, at, temperature, reach } (got ${shown(h.heaters)})`);
29
62
  const heaters = (h.heaters ?? []).map((x, i) => {
30
63
  const what = `frame.heat.heaters ${i + 1}`;
31
- if (!x || typeof x !== 'object' || !Array.isArray(x.at) || x.at.length !== 3) throw new Error(`${what} is { name, at: [x, y, z], temperature, reach } (got ${shown(x)})`);
64
+ if (!x || typeof x !== 'object' || !Array.isArray(x.at) || x.at.length !== 3)
65
+ throw new Error(`${what} is { name, at: [x, y, z], temperature, reach } (got ${shown(x)})`);
32
66
  const reach = s.num(x.reach);
33
- if (!(reach > 0)) throw new Error(`${what}: reach is how far from its middle a part is taken at its temperature, mm above 0 (got ${shown(x.reach)})`);
34
- return { name: x.name ?? `heater ${i + 1}`, at: x.at.map((v) => s.num(v)), temperature: deg(x.temperature, `heaters ${i + 1}.temperature`), reach };
67
+ if (!(reach > 0))
68
+ throw new Error(
69
+ `${what}: reach is how far from its middle a part is taken at its temperature, mm above 0 (got ${shown(x.reach)})`,
70
+ );
71
+ return {
72
+ name: x.name ?? `heater ${i + 1}`,
73
+ at: x.at.map((v) => s.num(v)),
74
+ temperature: deg(x.temperature, `heaters ${i + 1}.temperature`),
75
+ reach,
76
+ };
35
77
  });
36
- if (air == null && !heaters.length) throw new Error('frame.heat gives neither air nor heaters: give the air round the frame when it runs ("air": 45), a heater, or both');
78
+ if (air == null && !heaters.length)
79
+ throw new Error(
80
+ 'frame.heat gives neither air nor heaters: give the air round the frame when it runs ("air": 45), a heater, or both',
81
+ );
37
82
  return { air, built, heaters, airGiven: air != null };
38
83
  }
39
84
 
85
+ // A beam's material: a printed beam's model keeps its plastic's name, and its set's figures stay on its profile
86
+ function materialOf(o) {
87
+ const own = o.beam.material,
88
+ pm = o.beam.profile?.material;
89
+ return {
90
+ name: own?.name ?? pm?.name ?? null,
91
+ figures: own?.figures ?? (pm && (!own || own.name === pm.name) ? pm.figures : null),
92
+ };
93
+ }
94
+ // what the printed parts held to softening are, in a word: fittings, beams, or parts where both
95
+ const kindOf = (rows) => (rows.every((x) => x.beam) ? 'beam' : rows.some((x) => x.beam) ? 'part' : 'fitting');
96
+
40
97
  // The nearest point of an item's drawn parts to a point, and how far: its bounding box as drawn
41
98
  function nearest(s, id, P) {
42
- const tri = s.parts.filter((p) => p.item === id).flatMap((p) => p.triangles ?? []).flat();
99
+ const tri = s.parts
100
+ .filter((p) => p.item === id)
101
+ .flatMap((p) => p.triangles ?? [])
102
+ .flat();
43
103
  if (!tri.length) return null;
44
- const lo = [0, 1, 2].map((k) => Math.min(...tri.map((v) => v[k]))), hi = [0, 1, 2].map((k) => Math.max(...tri.map((v) => v[k])));
104
+ const lo = [0, 1, 2].map((k) => Math.min(...tri.map((v) => v[k]))),
105
+ hi = [0, 1, 2].map((k) => Math.max(...tri.map((v) => v[k])));
45
106
  return Math.hypot(...[0, 1, 2].map((k) => Math.max(lo[k] - P[k], 0, P[k] - hi[k])));
46
107
  }
47
108
 
@@ -51,44 +112,166 @@ function nearest(s, id, P) {
51
112
  */
52
113
  export function frameHeatChecks(s, f) {
53
114
  let h;
54
- try { h = heatOf(s); } catch (e) { f('fail', e.message, 'frame.heat: refused', { code: 'frame.heat' }); return; }
115
+ try {
116
+ h = heatOf(s);
117
+ } catch (e) {
118
+ f('fail', e.message, 'frame.heat: refused', { code: 'frame.heat' });
119
+ return;
120
+ }
55
121
  if (!h) return;
56
122
  // the beams grow
57
123
  const beams = s.objects.filter((o) => o.beam && !o.unplaced);
58
124
  if (beams.length && h.airGiven) {
59
- const placed = placeItems(s.motion, s.jointValues), at = Object.fromEntries(s.motion.items.map((x, i) => [x[0], placed[i]])), dT = h.air - h.built, ends = [];
125
+ const placed = placeItems(s.motion, s.jointValues),
126
+ at = Object.fromEntries(s.motion.items.map((x, i) => [x[0], placed[i]])),
127
+ dT = h.air - h.built,
128
+ ends = [];
60
129
  const rows = beams.map((o) => {
61
- const W = at[o.id], m = o.beam.material ?? o.beam.profile.material, own = m?.figures?.expansion?.value, alpha = typeof own === 'number' ? own : FRAME_HEAT.expansion.value;
62
- if (W) { const A = [W[3], W[7], W[11]], d = [W[2], W[6], W[10]]; ends.push(A, A.map((x, k) => x + d[k] * o.beam.length)); }
63
- return { id: o.id, length: o.beam.length, alpha, from: typeof own === 'number' ? `${m.name}.expansion` : 'FRAME_HEAT.expansion', grows: alpha * o.beam.length * dT };
130
+ const W = at[o.id],
131
+ m = materialOf(o),
132
+ own = m.figures?.expansion?.value,
133
+ alpha = typeof own === 'number' ? own : FRAME_HEAT.expansion.value;
134
+ if (W) {
135
+ const A = [W[3], W[7], W[11]],
136
+ d = [W[2], W[6], W[10]];
137
+ ends.push(
138
+ A,
139
+ A.map((x, k) => x + d[k] * o.beam.length),
140
+ );
141
+ }
142
+ return {
143
+ id: o.id,
144
+ length: o.beam.length,
145
+ alpha,
146
+ material: m.name,
147
+ from: typeof own === 'number' ? `${m.name}.expansion` : 'FRAME_HEAT.expansion',
148
+ grows: alpha * o.beam.length * dT,
149
+ };
64
150
  });
65
- const alpha = Math.max(...rows.map((x) => x.alpha)), size = [0, 1, 2].map((k) => (ends.length ? Math.max(...ends.map((p) => p[k])) - Math.min(...ends.map((p) => p[k])) : 0)), grows = size.map((x) => alpha * x * dT);
66
- const most = rows.reduce((a, b) => (Math.abs(b.grows) > Math.abs(a.grows) ? b : a)), kinds = [...new Set(rows.map((x) => x.from))];
67
- f('info', `at ${n3(h.air, 1)} °C the frame is ${n3(Math.abs(dT), 1)} °C ${dT >= 0 ? 'warmer' : 'cooler'} than the ${n3(h.built, 1)} °C it was built in, so it ${dT >= 0 ? 'grows' : 'shrinks'} by ${size.map((x, k) => (x > 1e-6 ? `${n3(Math.abs(grows[k]), 3)} mm over the ${n3(x, 0)} mm its beams span in ${'xyz'[k]}` : null)).filter(Boolean).join(', ')}; the longest beam, ${most.id} (${n3(most.length, 0)} mm), by ${n3(Math.abs(most.grows), 3)} mm. A frame of one alloy grows alike every way, so it stays square; a rail or rod of another metal on it grows by its own figure, and the difference bows or slides it`,
68
- null, { code: 'frame.heat', values: { air: h.air, built: h.built, change: dT, expansion: alpha, size: size.map((x) => +x.toFixed(2)), grows: grows.map((x) => +x.toFixed(4)), beams: rows.map((x) => ({ id: x.id, grows: +x.grows.toFixed(4) })) }, refs: { expansion: kinds, built: s.a.frame.heat.built != null ? 'frame.heat.built' : 'FRAME_HEAT.built' } });
151
+ const alpha = Math.max(...rows.map((x) => x.alpha)),
152
+ size = [0, 1, 2].map((k) =>
153
+ ends.length ? Math.max(...ends.map((p) => p[k])) - Math.min(...ends.map((p) => p[k])) : 0,
154
+ ),
155
+ grows = size.map((x) => alpha * x * dT);
156
+ const most = rows.reduce((a, b) => (Math.abs(b.grows) > Math.abs(a.grows) ? b : a)),
157
+ kinds = [...new Set(rows.map((x) => x.from))],
158
+ alphas = [...new Map(rows.map((x) => [x.alpha, x])).values()].sort((a, b) => a.alpha - b.alpha);
159
+ // one material grows alike every way; beams of several grow apart, each by its own figure
160
+ const apart =
161
+ alphas.length > 1
162
+ ? `its beams are of more than one material, which grow apart: ${alphas.map((x) => `${x.material ?? 'one of no set'} by ${n3(x.alpha * 1e6, 1)} x 10⁻⁶ /K`).join(', ')}, so over ${n3(most.length, 0)} mm the difference is ${n3((alphas[alphas.length - 1].alpha - alphas[0].alpha) * most.length * Math.abs(dT), 3)} mm, which bows or slides the joins between them`
163
+ : 'a frame of one material grows alike every way, so it stays square; a rail or rod of another metal on it grows by its own figure, and the difference bows or slides it';
164
+ f(
165
+ 'info',
166
+ `at ${n3(h.air, 1)} °C the frame is ${n3(Math.abs(dT), 1)} °C ${dT >= 0 ? 'warmer' : 'cooler'} than the ${n3(h.built, 1)} °C it was built in, so it ${dT >= 0 ? 'grows' : 'shrinks'} by ${size
167
+ .map((x, k) =>
168
+ x > 1e-6 ? `${n3(Math.abs(grows[k]), 3)} mm over the ${n3(x, 0)} mm its beams span in ${'xyz'[k]}` : null,
169
+ )
170
+ .filter(Boolean)
171
+ .join(
172
+ ', ',
173
+ )}; the longest beam, ${most.id} (${n3(most.length, 0)} mm), by ${n3(Math.abs(most.grows), 3)} mm. ${apart[0].toUpperCase()}${apart.slice(1)}`,
174
+ null,
175
+ {
176
+ code: 'frame.heat',
177
+ values: {
178
+ air: h.air,
179
+ built: h.built,
180
+ change: dT,
181
+ expansion: alpha,
182
+ expansions: alphas.map((x) => x.alpha),
183
+ size: size.map((x) => +x.toFixed(2)),
184
+ grows: grows.map((x) => +x.toFixed(4)),
185
+ beams: rows.map((x) => ({ id: x.id, grows: +x.grows.toFixed(4) })),
186
+ },
187
+ refs: { expansion: kinds, built: s.a.frame.heat.built != null ? 'frame.heat.built' : 'FRAME_HEAT.built' },
188
+ },
189
+ );
69
190
  }
70
- // each printed fitting against its plastic's softening point
191
+ // each printed fitting and each printed beam against its plastic's softening point
71
192
  const rows = [];
72
193
  for (const o of s.objects) {
73
- if (!o.fitting || o.unplaced || o.fitting.bought) continue;
74
- const mat = o.fitting.material ?? 'pla';
75
- let soft = null; try { soft = peekRuleSet(mat).rules?.softening ?? null; } catch {}
76
- let temp = h.air ?? h.built, by = h.airGiven ? 'the air' : 'the room', near = null;
77
- for (const x of h.heaters) { const d = nearest(s, o.id, x.at); if (d != null && d <= x.reach && x.temperature > temp) { temp = x.temperature; by = `${x.name}, ${n3(d, 1)} mm away`; near = x.name; } }
78
- rows.push({ id: o.id, fitting: o.fitting.name, mat, soft, temp, by, near });
194
+ const beam = o.beam?.printed && !o.unplaced;
195
+ if (!beam && (!o.fitting || o.unplaced || o.fitting.bought)) continue;
196
+ const m = beam ? materialOf(o) : null,
197
+ mat = beam ? m.name : (o.fitting.material ?? 'pla');
198
+ let soft = m?.figures?.softening ?? null;
199
+ if (!soft)
200
+ try {
201
+ soft = peekRuleSet(mat).rules?.softening ?? null;
202
+ } catch {
203
+ // (the material has no rule set to read: no softening figure for it)
204
+ }
205
+ let temp = h.air ?? h.built,
206
+ by = h.airGiven ? 'the air' : 'the room',
207
+ near = null;
208
+ for (const x of h.heaters) {
209
+ const d = nearest(s, o.id, x.at);
210
+ if (d != null && d <= x.reach && x.temperature > temp) {
211
+ temp = x.temperature;
212
+ by = `${x.name}, ${n3(d, 1)} mm away`;
213
+ near = x.name;
214
+ }
215
+ }
216
+ rows.push({
217
+ id: o.id,
218
+ fitting: beam ? `${o.beam.profile?.name ?? o.beam.name}, a printed beam` : o.fitting.name,
219
+ beam,
220
+ mat,
221
+ soft,
222
+ temp,
223
+ by,
224
+ near,
225
+ });
79
226
  }
80
- const margin = FRAME_HEAT.margin.value, out = [];
227
+ const margin = FRAME_HEAT.margin.value,
228
+ out = [];
81
229
  for (const r of rows) {
82
- if (typeof r.soft?.value !== 'number') { f('info', `${r.id}: ${r.mat} gives no softening point, so ${n3(r.temp, 1)} °C (${r.by}) is not checked against it: give its set a softening rule`, null, { code: 'frame.heat', refs: { item: r.id, material: r.mat } }); continue; }
83
- const spare = r.soft.value - r.temp, level = spare <= 0 ? 'fail' : spare < margin ? 'warn' : 'pass';
230
+ if (typeof r.soft?.value !== 'number') {
231
+ f(
232
+ 'info',
233
+ `${r.id}: ${r.mat} gives no softening point, so ${n3(r.temp, 1)} °C (${r.by}) is not checked against it: give its set a softening rule`,
234
+ null,
235
+ { code: 'frame.heat', refs: { item: r.id, material: r.mat } },
236
+ );
237
+ continue;
238
+ }
239
+ const spare = r.soft.value - r.temp,
240
+ level = spare <= 0 ? 'fail' : spare < margin ? 'warn' : 'pass';
84
241
  const x = { ...r, spare, level };
85
- if (level === 'pass') { out.push(x); continue; }
86
- f(level, `${r.id} (${r.fitting}, ${r.mat.toUpperCase()}) works at ${n3(r.temp, 1)} °C (${r.by}), ${level === 'fail' ? `${n3(-spare, 1)} °C over` : `only ${n3(spare, 1)} °C under`} its ${n3(r.soft.value, 1)} °C softening point (${r.soft.rule.split(':')[0]}): it ${level === 'fail' ? 'softens and creeps under its load' : 'creeps under its load sooner than the figure says'}. Print it in a plastic that softens higher, or move it from the heat`,
87
- `${r.id}: ${level === 'fail' ? 'too hot' : 'near softening'}`, { code: 'frame.heat', values: { temperature: r.temp, softening: r.soft.value, spare: +spare.toFixed(2), margin }, refs: { item: r.id, material: r.mat, heater: r.near, softening: `${r.mat}.softening`, rule: r.soft.kind } });
242
+ if (level === 'pass') {
243
+ out.push(x);
244
+ continue;
245
+ }
246
+ f(
247
+ level,
248
+ `${r.id} (${r.fitting}, ${r.mat.toUpperCase()}) works at ${n3(r.temp, 1)} °C (${r.by}), ${level === 'fail' ? `${n3(-spare, 1)} °C over` : `only ${n3(spare, 1)} °C under`} its ${n3(r.soft.value, 1)} °C softening point (${r.soft.rule.split(':')[0]}): it ${level === 'fail' ? 'softens and creeps under its load' : 'creeps under its load sooner than the figure says'}. Print it in a plastic that softens higher${r.beam ? ', make it a metal beam,' : ''} or move it from the heat`,
249
+ `${r.id}: ${level === 'fail' ? 'too hot' : 'near softening'}`,
250
+ {
251
+ code: 'frame.heat',
252
+ values: { temperature: r.temp, softening: r.soft.value, spare: +spare.toFixed(2), margin },
253
+ refs: {
254
+ item: r.id,
255
+ material: r.mat,
256
+ heater: r.near,
257
+ softening: `${r.mat}.softening`,
258
+ rule: r.soft.kind,
259
+ ...(r.beam ? { beam: true } : {}),
260
+ },
261
+ },
262
+ );
88
263
  }
89
264
  if (out.length) {
90
265
  const low = out.reduce((a, b) => (b.spare < a.spare ? b : a));
91
- f('pass', `${out.length === rows.length ? (out.length > 1 ? 'every printed fitting' : 'its printed fitting') : `${out.length} printed fitting${out.length > 1 ? 's' : ''}`} stay${out.length > 1 && out.length !== rows.length ? '' : 's'} at least ${margin} °C under its plastic's softening point: the nearest is ${low.id} (${low.mat.toUpperCase()}), ${n3(low.temp, 1)} °C (${low.by}) against ${n3(low.soft.value, 1)} °C`, null,
92
- { code: 'frame.heat', values: { fittings: out.length, spare: +low.spare.toFixed(2), margin }, refs: { item: low.id, items: out.map((x) => x.id), softening: `${low.mat}.softening` } });
266
+ f(
267
+ 'pass',
268
+ `${out.length === rows.length ? (out.length > 1 ? `every printed ${kindOf(out)}` : `its printed ${kindOf(out)}`) : `${out.length} printed ${kindOf(out)}${out.length > 1 ? 's' : ''}`} stay${out.length > 1 && out.length !== rows.length ? '' : 's'} at least ${margin} °C under its plastic's softening point: the nearest is ${low.id} (${low.mat.toUpperCase()}), ${n3(low.temp, 1)} °C (${low.by}) against ${n3(low.soft.value, 1)} °C`,
269
+ null,
270
+ {
271
+ code: 'frame.heat',
272
+ values: { fittings: out.length, spare: +low.spare.toFixed(2), margin },
273
+ refs: { item: low.id, items: out.map((x) => x.id), softening: `${low.mat}.softening` },
274
+ },
275
+ );
93
276
  }
94
277
  }