@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/frames.mjs CHANGED
@@ -5,6 +5,7 @@ import fs from 'node:fs';
5
5
  import path from 'node:path';
6
6
  import crypto from 'node:crypto';
7
7
  import * as frames from '@rulecad/frames';
8
+ import { readOwnText } from './host.mjs';
8
9
 
9
10
  // The workspace's own profile, slot, slot nut, material, thread and bought fitting sets, and its joins set (cad measure
10
11
  // --profile writes a draft profile there, with its outline in rules/outlines/; cad measured writes a measured figure
@@ -17,30 +18,82 @@ let seen = null;
17
18
  */
18
19
  export function framesContext(dir) {
19
20
  const out = path.join(dir, 'outlines');
20
- const stamp = (d) => { try { return fs.readdirSync(d).filter((f) => f.endsWith('.json')).sort().map((f) => `${f}:${fs.statSync(path.join(d, f)).mtimeMs}`).join('|'); } catch { return ''; } };
21
+ const stamp = (d) => {
22
+ try {
23
+ return fs
24
+ .readdirSync(d)
25
+ .filter((f) => f.endsWith('.json'))
26
+ .sort()
27
+ .map((f) => `${f}:${fs.statSync(path.join(d, f)).mtimeMs}`)
28
+ .join('|');
29
+ } catch {
30
+ return '';
31
+ }
32
+ };
21
33
  const key = `${dir}|${stamp(dir)}|${stamp(out)}`;
22
34
  if (seen?.key === key) return seen.context;
23
- const sets = {}, outlines = {}, text = [];
24
- for (const [d, into] of [[dir, sets], [out, outlines]]) {
25
- let files = []; try { files = fs.readdirSync(d).filter((f) => f.endsWith('.json')).sort(); } catch {}
35
+ const sets = {},
36
+ outlines = {},
37
+ text = [];
38
+ for (const [d, into] of [
39
+ [dir, sets],
40
+ [out, outlines],
41
+ ]) {
42
+ let files = [];
43
+ try {
44
+ files = fs
45
+ .readdirSync(d)
46
+ .filter((f) => f.endsWith('.json'))
47
+ .sort();
48
+ } catch {
49
+ // (no such folder: no sets from it)
50
+ }
26
51
  for (const f of files) {
27
52
  try {
28
- const raw = fs.readFileSync(path.join(d, f), 'utf8'), s = JSON.parse(raw);
53
+ const raw = readOwnText(path.join(d, f)), // (a set linked in from outside the workspace is not read)
54
+ s = JSON.parse(raw);
29
55
  // (a plastic's or a metal's set has no part: it is a material by its density, and the beam coupon measures into it)
30
- if (into === sets ? s?.rules && typeof s.rules === 'object' && (['profile', 'slot', 'slot_nut', 'material', 'thread', 'fitting', 'joins'].includes(s.part) || (s.part == null && typeof s.rules.density?.value === 'number')) : Array.isArray(s?.loops)) { into[s.name ?? f.slice(0, -5)] = s; text.push(raw); }
31
- } catch {} // (a broken set is cad rules check's to name)
56
+ if (
57
+ into === sets
58
+ ? s?.rules &&
59
+ typeof s.rules === 'object' &&
60
+ (['profile', 'slot', 'slot_nut', 'material', 'thread', 'fitting', 'joins'].includes(s.part) ||
61
+ (s.part == null && typeof s.rules.density?.value === 'number'))
62
+ : Array.isArray(s?.loops)
63
+ ) {
64
+ into[s.name ?? f.slice(0, -5)] = s;
65
+ text.push(raw);
66
+ }
67
+ } catch {
68
+ // (a broken set is cad rules check's to name)
69
+ }
32
70
  }
33
71
  }
34
- const context = { sets, outlines, hash: crypto.createHash('sha256').update(text.join('\n')).digest('hex').slice(0, 24) };
72
+ const context = {
73
+ sets,
74
+ outlines,
75
+ hash: crypto.createHash('sha256').update(text.join('\n')).digest('hex').slice(0, 24),
76
+ };
35
77
  seen = { key, context };
36
78
  return context;
37
79
  }
38
80
 
39
81
  // profileModel's third argument for a profile by name: the workspace's sets, and its measured outline if it has one
40
82
  export const givenFor = (context, profile, given = {}) => {
41
- const name = typeof profile === 'string' ? (context.sets?.[profile] ? profile : context.sets?.[`profile-${profile}`] ? `profile-${profile}` : null) : null;
83
+ const name =
84
+ typeof profile === 'string'
85
+ ? context.sets?.[profile]
86
+ ? profile
87
+ : context.sets?.[`profile-${profile}`]
88
+ ? `profile-${profile}`
89
+ : null
90
+ : null;
42
91
  const outline = name && context.outlines?.[name];
43
- return { ...given, sets: { ...(context.sets || {}), ...(given.sets || {}) }, ...(outline && !given.outline ? { outline } : {}) };
92
+ return {
93
+ ...given,
94
+ sets: { ...(context.sets || {}), ...(given.sets || {}) },
95
+ ...(outline && !given.outline ? { outline } : {}),
96
+ };
44
97
  };
45
98
 
46
99
  /**
@@ -53,13 +106,24 @@ export const givenFor = (context, profile, given = {}) => {
53
106
  export function designFrames(context = {}) {
54
107
  const bound = { ...frames };
55
108
  // (a name is looked up in given.sets before the kit's, with or without its profile- prefix)
56
- bound.profileModel = (which, change = {}, given = {}) => frames.profileModel(which, change, givenFor(context, which, given));
57
- bound.slotModel = (which, change = {}, given = {}) => frames.slotModel(which, change, { ...given, sets: { ...(context.sets || {}), ...(given.sets || {}) } });
58
- bound.beamModel = (profile, o = {}) => frames.beamModel(typeof profile === 'string' ? bound.profileModel(profile, {}, o.given) : profile, { ...o, given: givenFor(context, profile, o.given) });
59
- bound.beam = (m, o = {}) => frames.beam(m?.kind === 'beam' || m?.kind === 'profile' ? m : typeof m === 'string' ? bound.profileModel(m, {}, o.given) : m, o);
109
+ bound.profileModel = (which, change = {}, given = {}) =>
110
+ frames.profileModel(which, change, givenFor(context, which, given));
111
+ bound.slotModel = (which, change = {}, given = {}) =>
112
+ frames.slotModel(which, change, { ...given, sets: { ...(context.sets || {}), ...(given.sets || {}) } });
113
+ bound.beamModel = (profile, o = {}) =>
114
+ frames.beamModel(typeof profile === 'string' ? bound.profileModel(profile, {}, o.given) : profile, {
115
+ ...o,
116
+ given: givenFor(context, profile, o.given),
117
+ });
118
+ bound.beam = (m, o = {}) =>
119
+ frames.beam(
120
+ m?.kind === 'beam' || m?.kind === 'profile' ? m : typeof m === 'string' ? bound.profileModel(m, {}, o.given) : m,
121
+ o,
122
+ );
60
123
  // (a printed carrier drawn from the workspace's slot, as measured by caliper on the slot coupon)
61
- bound.carrierModel = (where, o = {}) => frames.carrierModel(where, { ...o, given: givenFor(context, where, o?.given) });
62
- bound.carrier = (m, o = {}) => frames.carrier(m, m?.kind === 'slot-carrier' ? o : { ...o, given: givenFor(context, m, o?.given) });
124
+ bound.carrierModel = (where, o = {}) =>
125
+ frames.carrierModel(where, { ...o, given: givenFor(context, where, o?.given) });
126
+ bound.carrier = (m, o = {}) =>
127
+ frames.carrier(m, m?.kind === 'slot-carrier' ? o : { ...o, given: givenFor(context, m, o?.given) });
63
128
  return bound;
64
129
  }
65
-