@rulecad/cad 0.8.0 → 0.10.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (102) hide show
  1. package/AGENTS.md +45 -16
  2. package/CHANGELOG.md +724 -0
  3. package/EXAMPLES.md +61 -54
  4. package/README.md +376 -98
  5. package/TUTORIAL.md +2 -2
  6. package/bin/cad.mjs +2664 -541
  7. package/lib/animate.mjs +173 -49
  8. package/lib/assembly.mjs +1883 -435
  9. package/lib/beams.mjs +256 -52
  10. package/lib/belts.mjs +66 -15
  11. package/lib/board-work.mjs +12 -0
  12. package/lib/bought.mjs +309 -53
  13. package/lib/buildpage.mjs +179 -29
  14. package/lib/cache-file.mjs +105 -0
  15. package/lib/carried.mjs +201 -0
  16. package/lib/clone.mjs +194 -42
  17. package/lib/code-text.mjs +28 -0
  18. package/lib/design-imports.mjs +127 -0
  19. package/lib/design.mjs +871 -147
  20. package/lib/designshapes.mjs +142 -0
  21. package/lib/drive.mjs +187 -63
  22. package/lib/edit.mjs +922 -192
  23. package/lib/errors.mjs +16 -3
  24. package/lib/estimates.mjs +51 -0
  25. package/lib/explain.mjs +62 -13
  26. package/lib/fasten.mjs +2708 -507
  27. package/lib/fe-key.mjs +29 -0
  28. package/lib/fittings.mjs +721 -122
  29. package/lib/fixings.mjs +38 -13
  30. package/lib/framebuild.mjs +790 -175
  31. package/lib/framecheck.mjs +1901 -368
  32. package/lib/framegen.mjs +108 -22
  33. package/lib/frameheat.mjs +225 -42
  34. package/lib/frameprint.mjs +513 -108
  35. package/lib/frames.mjs +86 -18
  36. package/lib/framesize.mjs +719 -120
  37. package/lib/frametip.mjs +235 -0
  38. package/lib/gait.mjs +135 -23
  39. package/lib/grip.mjs +62 -13
  40. package/lib/guides.mjs +1403 -230
  41. package/lib/host.mjs +129 -23
  42. package/lib/inplace.mjs +171 -36
  43. package/lib/jigs.mjs +360 -78
  44. package/lib/joins.mjs +697 -118
  45. package/lib/jsonedit.mjs +88 -26
  46. package/lib/kept.mjs +84 -0
  47. package/lib/kit.mjs +439 -69
  48. package/lib/layout.mjs +2 -1
  49. package/lib/links.mjs +149 -42
  50. package/lib/list.mjs +334 -64
  51. package/lib/live-guard.mjs +68 -0
  52. package/lib/live-shape.mjs +96 -24
  53. package/lib/live.mjs +189 -59
  54. package/lib/loads.mjs +597 -136
  55. package/lib/machine.mjs +372 -74
  56. package/lib/magnets.mjs +162 -44
  57. package/lib/measured.mjs +1080 -117
  58. package/lib/mesh.mjs +526 -119
  59. package/lib/meshcmd.mjs +215 -60
  60. package/lib/motors.mjs +1002 -214
  61. package/lib/motorsvg.mjs +114 -32
  62. package/lib/names.mjs +18 -1
  63. package/lib/optimise.mjs +991 -287
  64. package/lib/order.mjs +515 -114
  65. package/lib/panels.mjs +482 -94
  66. package/lib/parts.mjs +81 -23
  67. package/lib/pins.mjs +163 -44
  68. package/lib/plates.mjs +223 -35
  69. package/lib/player.mjs +37 -9
  70. package/lib/power.mjs +64 -13
  71. package/lib/preview.mjs +83 -16
  72. package/lib/profilemeasure.mjs +572 -129
  73. package/lib/rules.mjs +17 -6
  74. package/lib/sandbox-hooks.mjs +179 -27
  75. package/lib/sandbox-lock.mjs +225 -0
  76. package/lib/sandbox-preload.mjs +2 -2
  77. package/lib/sandbox-run.mjs +122 -25
  78. package/lib/sandbox.mjs +667 -75
  79. package/lib/schema.mjs +42 -11
  80. package/lib/seats.mjs +227 -49
  81. package/lib/services.mjs +261 -52
  82. package/lib/sheet.mjs +963 -179
  83. package/lib/slotnuts.mjs +213 -34
  84. package/lib/targets/gist.mjs +4 -1
  85. package/lib/targets.mjs +22 -4
  86. package/lib/task.mjs +376 -79
  87. package/lib/versions.mjs +285 -39
  88. package/lib/wires.mjs +228 -45
  89. package/lib/workspace.mjs +200 -14
  90. package/package.json +19 -19
  91. package/schemas/assembly.schema.json +2 -2
  92. package/schemas/project.schema.json +6 -0
  93. package/templates/coupons/MEASURE.md +109 -0
  94. package/templates/coupons/assemblies/fixings-test.json +20 -0
  95. package/templates/coupons/assemblies/slot-test.json +11 -0
  96. package/templates/coupons/objects/fixings-coupon.js +88 -0
  97. package/templates/coupons/objects/fixings-coupon.json +26 -0
  98. package/templates/coupons/objects/holds-coupon.js +130 -0
  99. package/templates/coupons/objects/holds-coupon.json +42 -0
  100. package/templates/coupons/objects/slot-coupon.js +89 -0
  101. package/templates/coupons/objects/slot-coupon.json +29 -0
  102. package/templates/coupons/project.json +10 -0
package/lib/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
@@ -13,34 +14,90 @@ let seen = null;
13
14
  /**
14
15
  * What @rulecad/frames is handed from a workspace: its own rule sets by name, and the measured outlines beside them.
15
16
  * @param {string} dir the workspace's rules/ folder
16
- * @returns {{ sets: Record<string, object>, outlines: Record<string, object>, hash: string }}
17
+ * @returns {{ sets: Record<string, object>, outlines: Record<string, object>, hash: string, files: string[] }} files: the
18
+ * paths of the files those sets and outlines were read from
17
19
  */
18
20
  export function framesContext(dir) {
19
21
  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 ''; } };
22
+ const stamp = (d) => {
23
+ try {
24
+ return fs
25
+ .readdirSync(d)
26
+ .filter((f) => f.endsWith('.json'))
27
+ .sort()
28
+ .map((f) => `${f}:${fs.statSync(path.join(d, f)).mtimeMs}`)
29
+ .join('|');
30
+ } catch {
31
+ return '';
32
+ }
33
+ };
21
34
  const key = `${dir}|${stamp(dir)}|${stamp(out)}`;
22
35
  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 {}
36
+ const sets = {},
37
+ outlines = {},
38
+ text = [],
39
+ from = [];
40
+ for (const [d, into] of [
41
+ [dir, sets],
42
+ [out, outlines],
43
+ ]) {
44
+ let files = [];
45
+ try {
46
+ files = fs
47
+ .readdirSync(d)
48
+ .filter((f) => f.endsWith('.json'))
49
+ .sort();
50
+ } catch {
51
+ // (no such folder: no sets from it)
52
+ }
26
53
  for (const f of files) {
27
54
  try {
28
- const raw = fs.readFileSync(path.join(d, f), 'utf8'), s = JSON.parse(raw);
55
+ const raw = readOwnText(path.join(d, f)), // (a set linked in from outside the workspace is not read)
56
+ s = JSON.parse(raw);
29
57
  // (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)
58
+ if (
59
+ into === sets
60
+ ? s?.rules &&
61
+ typeof s.rules === 'object' &&
62
+ (['profile', 'slot', 'slot_nut', 'material', 'thread', 'fitting', 'joins'].includes(s.part) ||
63
+ (s.part == null && typeof s.rules.density?.value === 'number'))
64
+ : Array.isArray(s?.loops)
65
+ ) {
66
+ into[s.name ?? f.slice(0, -5)] = s;
67
+ text.push(raw);
68
+ from.push(path.join(d, f));
69
+ }
70
+ } catch {
71
+ // (a broken set is cad rules check's to name)
72
+ }
32
73
  }
33
74
  }
34
- const context = { sets, outlines, hash: crypto.createHash('sha256').update(text.join('\n')).digest('hex').slice(0, 24) };
75
+ const context = {
76
+ sets,
77
+ outlines,
78
+ hash: crypto.createHash('sha256').update(text.join('\n')).digest('hex').slice(0, 24),
79
+ files: from, // (the files of those sets and outlines: a release records them, lib/versions.mjs)
80
+ };
35
81
  seen = { key, context };
36
82
  return context;
37
83
  }
38
84
 
39
85
  // profileModel's third argument for a profile by name: the workspace's sets, and its measured outline if it has one
40
86
  export const givenFor = (context, profile, given = {}) => {
41
- const name = typeof profile === 'string' ? (context.sets?.[profile] ? profile : context.sets?.[`profile-${profile}`] ? `profile-${profile}` : null) : null;
87
+ const name =
88
+ typeof profile === 'string'
89
+ ? context.sets?.[profile]
90
+ ? profile
91
+ : context.sets?.[`profile-${profile}`]
92
+ ? `profile-${profile}`
93
+ : null
94
+ : null;
42
95
  const outline = name && context.outlines?.[name];
43
- return { ...given, sets: { ...(context.sets || {}), ...(given.sets || {}) }, ...(outline && !given.outline ? { outline } : {}) };
96
+ return {
97
+ ...given,
98
+ sets: { ...(context.sets || {}), ...(given.sets || {}) },
99
+ ...(outline && !given.outline ? { outline } : {}),
100
+ };
44
101
  };
45
102
 
46
103
  /**
@@ -53,13 +110,24 @@ export const givenFor = (context, profile, given = {}) => {
53
110
  export function designFrames(context = {}) {
54
111
  const bound = { ...frames };
55
112
  // (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);
113
+ bound.profileModel = (which, change = {}, given = {}) =>
114
+ frames.profileModel(which, change, givenFor(context, which, given));
115
+ bound.slotModel = (which, change = {}, given = {}) =>
116
+ frames.slotModel(which, change, { ...given, sets: { ...(context.sets || {}), ...(given.sets || {}) } });
117
+ bound.beamModel = (profile, o = {}) =>
118
+ frames.beamModel(typeof profile === 'string' ? bound.profileModel(profile, {}, o.given) : profile, {
119
+ ...o,
120
+ given: givenFor(context, profile, o.given),
121
+ });
122
+ bound.beam = (m, o = {}) =>
123
+ frames.beam(
124
+ m?.kind === 'beam' || m?.kind === 'profile' ? m : typeof m === 'string' ? bound.profileModel(m, {}, o.given) : m,
125
+ o,
126
+ );
60
127
  // (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) });
128
+ bound.carrierModel = (where, o = {}) =>
129
+ frames.carrierModel(where, { ...o, given: givenFor(context, where, o?.given) });
130
+ bound.carrier = (m, o = {}) =>
131
+ frames.carrier(m, m?.kind === 'slot-carrier' ? o : { ...o, given: givenFor(context, m, o?.given) });
63
132
  return bound;
64
133
  }
65
-