@rulecad/cad 0.7.55 → 0.7.56

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.
package/lib/motors.mjs CHANGED
@@ -246,12 +246,24 @@ function mountPlate(s, d, W) {
246
246
  return { fit, other, parts, inv, t, plate, hd };
247
247
  }
248
248
 
249
+ // The screws a motor is held by on a plate this thick: mountModel's (from @rulecad/fixings), at a length sold, the
250
+ // longest that does not take them past mountModel's length into the motor's own thread, or through plain holes the
251
+ // shortest that reaches; or the length the motor item names ("screw": "M3x8 cap"). Where the wall at the holes (t) is
252
+ // thinner than the plate round them, the heads are sunk in counterbores (mount()'s, as deep as the head is high), and
253
+ // the screws are chosen for the plate under them
254
+ function screwsFor(d, plate, t = plate) {
255
+ const here = mountModel(d.model, { plate, ...(t < plate - 0.5 ? { counterbore: true } : {}), ...(d.spec.screw ? { screw: d.spec.screw } : {}) }), fx = here.screw.model, L = here.screwLength, sold = fx.figures.lengths?.value;
256
+ const length = fx.length ?? (!Array.isArray(sold) || !sold.length ? L : d.model.mount.thread ? [...sold].reverse().find((l) => l <= L) ?? sold[0] : sold.find((l) => l >= L) ?? sold.at(-1));
257
+ return { here, fx, length, wanted: L, name: `${fx.thread}x${length} ${fx.head.form}` };
258
+ }
259
+
249
260
  /**
250
261
  * The screws that hold each motor to the part it is fastened to by a fit with "fastens" (a motor named in a "fasten"
251
- * has its screws counted there): as many as its mounting holes, the length mountModel works out for the plate as drawn,
252
- * taken to a length sold (the longest that does not pass it into the motor's own thread; through plain holes, the shortest that reaches).
262
+ * has its screws counted there): as many as its mounting holes, @rulecad/fixings' screw on the thread mountModel takes,
263
+ * at the length mountModel works out for the plate as drawn taken to a length sold (the longest that does not pass it
264
+ * into the motor's own thread; through plain holes, the shortest that reaches), or the length the motor item names.
253
265
  * @param {object} s a built assembly @param {{ placeItems: Function, jointValues: Function }} helpers
254
- * @returns {{ item: string, other: string, thread: string, length: number, wanted: number, count: number }[]}
266
+ * @returns {{ item: string, other: string, fit: object, thread: string, head: string, name: string, key: number | null, length: number, wanted: number, count: number }[]}
255
267
  */
256
268
  export function motorScrews(s, { placeItems, jointValues }) {
257
269
  const drives = s.motion?.drives || [], top = (n) => String(n).split(/[.@]/)[0];
@@ -261,11 +273,9 @@ export function motorScrews(s, { placeItems, jointValues }) {
261
273
  for (const d of drives) {
262
274
  if (named.has(d.id)) continue;
263
275
  const p = mountPlate(s, d, worlds[d.item]); if (!p || p.t === Infinity) continue;
264
- const here = mountModel(d.model, { plate: +p.plate.toFixed(2), ...(d.spec.screw ? { screw: d.spec.screw } : {}) }), thread = here.screw?.thread, L = here.screwLength;
265
- if (!thread || !(L > 0)) continue;
266
- let sold = null; try { sold = peekRuleSet(thread.toLowerCase().replace('.', '_')).rules.lengths?.value ?? null; } catch {}
267
- const length = !sold ? L : d.model.mount.thread ? [...sold].reverse().find((l) => l <= L) ?? sold[0] : sold.find((l) => l >= L) ?? sold.at(-1);
268
- out.push({ item: d.id, other: p.other, thread, length, wanted: L, count: d.model.mount.holes.length });
276
+ const { fx, length, wanted, name } = screwsFor(d, +p.plate.toFixed(2), +p.t.toFixed(2));
277
+ if (!(wanted > 0)) continue;
278
+ out.push({ item: d.id, other: p.other, fit: p.fit, thread: fx.thread, head: fx.head.form, name, key: fx.head.key ?? null, length, wanted, count: d.model.mount.holes.length });
269
279
  }
270
280
  return out;
271
281
  }
@@ -275,8 +285,9 @@ function mountCheck(s, d, f, W, avail, materialName, frame) {
275
285
  if (!at) return;
276
286
  const { other, parts, inv, t, plate, hd } = at;
277
287
  if (t === Infinity) return f('warn', `${d.id}: couldn't find the plate at its screw holes in ${other}`, `${d.id}: mount not found`, { code: 'motor.mount' });
278
- const here = mountModel(m, { plate: +plate.toFixed(2), ...(d.spec.screw ? { screw: d.spec.screw } : {}) }), mat = material(materialName) || material('pla'), each = Math.max(...screwGroup(m.mount.holes, { moment: [0, 0, avail] }).screws.map((x) => x.shear)), bearing = each / (hd * t), margin = mat.strength / bearing;
279
- for (const x of here.findings) f(x.level, `${d.id} on ${other}: ${x.text}`, x.level === 'pass' ? null : `${d.id}: ${x.code.split('.')[1]}`, { code: 'motor.mount' });
288
+ // the screws as bought (screwsFor), so the engagement mountModel checks is the bought screw's
289
+ const sunk = t < plate - 0.5, here = mountModel(m, { plate: +plate.toFixed(2), ...(sunk ? { counterbore: true } : {}), screw: screwsFor(d, +plate.toFixed(2), +t.toFixed(2)).name }), mat = material(materialName) || material('pla'), each = Math.max(...screwGroup(m.mount.holes, { moment: [0, 0, avail] }).screws.map((x) => x.shear)), bearing = each / (hd * t), margin = mat.strength / bearing;
290
+ for (const x of here.findings) f(x.level, `${d.id} on ${other}: ${x.text}`, x.level === 'pass' ? null : `${d.id}: ${x.code.split('.')[1]}`, { code: 'motor.mount', ...(x.values ? { values: x.values } : {}), ...(x.refs ? { refs: { item: d.id, ...x.refs } } : {}) });
280
291
  f(margin < 2 ? 'fail' : 'pass', `${d.id} on ${other}: ${n3(plate, 2)} mm of plate at the screws, each taking ${n3(each, 1)} N against the torque (bearing ${n3(bearing, 1)} MPa, ${n3(margin, 0)} times under ${mat.name.toUpperCase()}'s ${mat.strength} MPa)`, margin < 2 ? `${d.id}: screws pull through` : null, { code: 'motor.mount', values: { plate, bearing, margin } });
281
292
  mountBends(s, d, f, W, avail, mat, { other, parts, inv, plate, hd, ...frame });
282
293
  }
package/lib/order.mjs CHANGED
@@ -11,6 +11,7 @@ import fs from 'node:fs';
11
11
  import { placeItems, jointValues, mul } from '@rulecad/model-link';
12
12
  import { host } from './host.mjs';
13
13
  import { paths } from './workspace.mjs';
14
+ import { motorScrews } from './motors.mjs';
14
15
 
15
16
  const top = (n) => String(n).split(/[.@]/)[0];
16
17
  const round = (v) => v.map((x) => (Math.abs(x) < 1e-9 ? 0 : +x.toFixed(6)));
@@ -197,11 +198,13 @@ export function assemblyOrder(s) {
197
198
  const seq = [], lay = (off) => { for (const x of [...off].reverse()) { if (x.group) { lay(x.sub.off); seq.push({ group: x.group, d: x.d }); } else seq.push(x); } };
198
199
  lay(apart.off);
199
200
  const at = new Map(), steps = [];
201
+ // a motor's mounting screws, on the fit that fastens it (motorScrews): counted, with the key that turns them
202
+ const motorFits = new Map(motorScrews(s, { placeItems, jointValues }).map((m) => [m.fit, { fastener: m.name, count: m.count, ...(m.key != null ? { key: `${+m.key.toFixed(3)} mm hex key` } : {}) }]));
200
203
  seq.forEach((x, i) => { for (const g of x.group ?? [x.p]) at.set(g.id, i); });
201
204
  for (const [i, x] of seq.entries()) {
202
205
  if (x.group) { steps.push({ items: x.group.map((g) => g.id), object: null, dir: round(x.d), fasteners: [], together: true }); continue; }
203
206
  const { p, d } = x, list = [...new Set(s.fits.filter((f) => [...names(f.a), ...names(f.b)].includes(p.id) && (f.fastens || f.seat != null || f.fastener) && [...names(f.a), ...names(f.b)].every((n) => (at.get(n) ?? 0) <= i)))];
204
- steps.push({ items: [p.id], object: p.object, dir: round(d), fasteners: list.map((f) => ({ fit: f.note || f.label || `${f.a} to ${f.b}`, ...(f.fastener ? { fastener: f.fastener } : {}), ...(f.count != null ? { count: f.num(f.count) } : {}), ...(f.key ? { key: f.key } : {}), ...(f.tighten ? { tighten: f.tighten } : {}) })) });
207
+ steps.push({ items: [p.id], object: p.object, dir: round(d), fasteners: list.map((f) => ({ fit: f.note || f.label || `${f.a} to ${f.b}`, ...(f.fastener ? { fastener: f.fastener } : {}), ...(f.count != null ? { count: f.num(f.count) } : {}), ...(f.key ? { key: f.key } : {}), ...(f.tighten ? { tighten: f.tighten } : {}), ...(!f.fastener ? motorFits.get(f) : {}) })) });
205
208
  }
206
209
  // Copies of one thing fitted the same way, one after another, are one step
207
210
  const merged = [];
package/lib/schema.mjs CHANGED
@@ -27,9 +27,10 @@ export function validate(value, s, root = s, at = '') {
27
27
  if (s.$ref) return validate(value, s.$ref.replace('#/$defs/', '').split('/').reduce((o, k) => o[k], root.$defs), root, at);
28
28
  if (s.anyOf) {
29
29
  const each = s.anyOf.map((x) => validate(value, x, root, at));
30
- if (each.some((e) => !e.length)) return [];
31
30
  // Report the alternative that got furthest (the fewest problems), so a near miss says what's wrong with it
32
- return each.sort((a, b) => a.length - b.length)[0];
31
+ const best = each.some((e) => !e.length) ? [] : each.sort((a, b) => a.length - b.length)[0];
32
+ if (Object.keys(s).every((k) => k === 'anyOf' || k === 'description')) return best;
33
+ out.push(...best); // beside other keywords, as JSON Schema has it: each applies (a fit: a and b, or holes and into)
33
34
  }
34
35
  if (s.type) {
35
36
  const types = [].concat(s.type);
package/lib/sheet.mjs CHANGED
@@ -27,6 +27,22 @@ function fileOf(name) {
27
27
  return file;
28
28
  }
29
29
 
30
+ // What a check notes and never fails on. An alias: a name the vocabulary shares across parts (screw_clearance, which a
31
+ // motor's mount reads) that a set gives to one of its own rules in "interface". A copy: a workspace's own copy of a
32
+ // set the kit has (cad copies a set into rules/ the first time a design uses it), which is used as it is, even after
33
+ // the kit's set gains figures: it is told which, so a design that wants one knows where it went.
34
+ function notesOf(set, file) {
35
+ const notes = [], kitDir = RULE_DIRS.at(-1), name = path.basename(file, '.json');
36
+ for (const [shared, own] of Object.entries(set.interface || {})) if (set.rules?.[own]) notes.push({ level: 'info', code: 'rules.alias', text: `${own} is also read as ${shared}, the vocabulary's name for it`, rule: own, values: { alias: shared } });
37
+ const kitFile = path.join(kitDir, `${name}.json`);
38
+ if (path.dirname(path.resolve(file)) !== path.resolve(kitDir) && fs.existsSync(kitFile) && set.rules && typeof set.rules === 'object') {
39
+ const kit = readSet(kitFile), added = Object.keys(kit.rules || {}).filter((k) => !(k in set.rules));
40
+ if (added.length) notes.push({ level: 'info', code: 'rules.new', text: `your copy is used as it is; the kit's ${name} now also has ${added.length > 8 ? `${added.slice(0, 8).join(', ')} and ${added.length - 8} more (--json lists them)` : added.join(', ')}: copy any you want into rules/${name}.json, or delete it to read the kit's own`, values: { count: added.length }, refs: { rules: added } });
41
+ }
42
+ return notes;
43
+ }
44
+
45
+
30
46
  // What a profile's model reads beside its own set: any other set by name (its slot family, a thread, its material),
31
47
  // the workspace's first, and a measured outline in outlines/<name>.json beside the set, held to its schema
32
48
  function given(name, file, all = allSets()) {
@@ -57,13 +73,14 @@ function partFindings(set, name, file, all) {
57
73
  return out;
58
74
  }
59
75
 
60
- /** Every named set checked (or all of them): [{ name, file, findings }]. A finding's level is fail, warn or info */
76
+ /** Every named set checked (or all of them): [{ name, file, findings, notes? }]. A finding's level is fail, warn or info;
77
+ * notes: what is said and never fails */
61
78
  export function checkSets(names = []) {
62
79
  const v = vocabulary(), all = allSets();
63
80
  return (names.length ? names.map((n) => [n, fileOf(n)]) : [...all]).map(([name, file]) => {
64
- const set = readSet(file), findings = checkRuleSet(set, v);
81
+ const set = readSet(file), findings = checkRuleSet(set, v), notes = notesOf(set, file);
65
82
  if (!findings.length && ['profile', 'slot', 'slot_nut'].includes(set.part)) findings.push(...partFindings(set, name, file, all));
66
- return { name, file, findings };
83
+ return { name, file, findings, ...(notes.length ? { notes } : {}) };
67
84
  });
68
85
  }
69
86
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rulecad/cad",
3
- "version": "0.7.55",
3
+ "version": "0.7.56",
4
4
  "description": "The cad command: JSCAD designs from traceable rules, checked for printing and fit, versioned by what fits, shared as links and exported as Bambu 3MF.",
5
5
  "type": "module",
6
6
  "bin": {
@@ -30,6 +30,7 @@
30
30
  "schemas",
31
31
  "templates",
32
32
  "README.md",
33
+ "CHANGELOG.md",
33
34
  "TUTORIAL.md",
34
35
  "EXAMPLES.md",
35
36
  "LICENSE",
@@ -63,16 +64,16 @@
63
64
  "@rulecad/fit": "^0.4.0",
64
65
  "@rulecad/model-link": "^0.3.2",
65
66
  "@rulecad/versions": "^0.1.6",
66
- "@rulecad/shapes": "^0.4.4",
67
- "@rulecad/rules": "^0.3.14",
67
+ "@rulecad/shapes": "^0.4.5",
68
+ "@rulecad/rules": "^0.3.15",
68
69
  "@jscad/modeling": "^2.12.0",
69
70
  "three": "^0.186.1",
70
71
  "@rulecad/fea": "^0.2.1",
71
72
  "@rulecad/topopt": "^0.3.0",
72
73
  "@rulecad/flexures": "^0.5.0",
73
74
  "@rulecad/gears": "^0.3.2",
74
- "@rulecad/motors": "^0.1.4",
75
- "@rulecad/fixings": "^0.1.3",
75
+ "@rulecad/motors": "^0.1.6",
76
+ "@rulecad/fixings": "^0.1.4",
76
77
  "@rulecad/robots": "^0.1.1",
77
78
  "@rulecad/frames": "^0.1.7"
78
79
  },
@@ -17,7 +17,7 @@
17
17
  "requires": { "type": "object", "additionalProperties": { "type": "string" }, "description": "object → semver range" },
18
18
  "items": { "type": "array", "items": { "$ref": "#/$defs/item" } },
19
19
  "fits": { "type": "array", "items": { "$ref": "#/$defs/fit" } },
20
- "fasten": { "type": "array", "description": "Screwed joints: each names its screw, the parts it passes through in order, and the part it ends in. The holes are read from the parts as drawn and each screw followed along one axis; a fasten holds its parts as a fit that fastens does, and lines their holes up as a holes fit does", "items": { "$ref": "#/$defs/fasten" } },
20
+ "fasten": { "type": "array", "description": "Screwed joints: each names its screw, the parts it passes through in order, and the part it ends in. The holes are read from the parts as drawn and each screw followed along one axis; a fasten holds its parts as a fit that fastens does, lines their holes up as a holes fit does, and puts its screws on the shopping list. Draw the holes with v.fixings, and name the screw with no length: the check chooses one", "items": { "$ref": "#/$defs/fasten" }, "defaultSnippets": [{ "label": "fasten", "description": "A screwed joint", "body": [{ "screw": "${1:M3 cap}", "through": ["${2:bracket}"], "into": "${3:base}", "at": ["${4:bracket.h1}", "${5:bracket.h2}"] }] }] },
21
21
  "joints": { "type": "array", "items": { "$ref": "#/$defs/joint" } },
22
22
  "loops": { "type": "array", "items": { "$ref": "#/$defs/loop" } },
23
23
  "planetary": { "type": "array", "items": { "$ref": "#/$defs/planetary" } },
@@ -72,7 +72,7 @@
72
72
  },
73
73
  "fit": {
74
74
  "type": "object",
75
- "required": ["a", "b"],
75
+ "anyOf": [{ "required": ["a", "b"] }, { "required": ["holes", "into"] }],
76
76
  "additionalProperties": false,
77
77
  "properties": {
78
78
  "a": { "$ref": "#/$defs/names" },
@@ -81,8 +81,8 @@
81
81
  "radius": { "$ref": "#/$defs/expr", "description": "With engage: how near the joint's axis to look (mm, default 10)" },
82
82
  "shaft": { "anyOf": [{ "type": "boolean" }, { "enum": ["slide", "press"] }], "description": "a or b is a motor, and the other takes its shaft: reads the bore from the part as drawn" },
83
83
  "seat": { "anyOf": [{ "type": "boolean" }, { "enum": ["press", "slip"] }, { "type": "number" }], "description": "a or b is a bought bearing, and the other its printed seat: reads the pocket as drawn against the bearing's rules and the printer's fit (fit.seat)" },
84
- "fastens": { "type": "boolean", "description": "a and b are fastened together (pins, screws, a press): what holds a part that has no joint" },
85
- "fastener": { "type": "string", "description": "What does the fastening (an M3 × 12 screw): named on the shopping list and the assembly step" },
84
+ "fastens": { "type": "boolean", "description": "a and b are fastened together (pins, a press, a clip): what holds a part that has no joint. Where screws hold them, use a fasten instead: it knows the screw, checks its holes and lists it" },
85
+ "fastener": { "type": "string", "description": "What does the fastening, by hand (two push pins): named on the shopping list and the assembly step. A screw is a fasten's, which chooses its length and counts it" },
86
86
  "count": { "$ref": "#/$defs/expr", "description": "With fastener: how many" },
87
87
  "access": { "type": "string", "description": "<item>.<anchor>: where the screw's tool comes from, its z axis pointing out along the screw. Whatever stands on that line goes on after the screw; if nothing can, assembly.order says so" },
88
88
  "min": { "$ref": "#/$defs/expr" },
@@ -90,11 +90,11 @@
90
90
  "note": { "type": "string" },
91
91
  "label": { "type": "string" },
92
92
  "poses": { "type": "array", "items": { "type": "string" } },
93
- "holes": { "type": "boolean" },
94
- "depth": { "$ref": "#/$defs/expr" },
95
- "direction": { "enum": ["up", "down"] },
96
- "ring": { "$ref": "#/$defs/expr" },
97
- "into": { "type": "string" },
93
+ "holes": { "type": "string", "description": "A holes fit: a circuit board's part (pcb.board) whose mounting holes are followed down into the part named by into. Where the board is screwed there, a fasten with \"holes\" says it and knows the screw: cad check suggests it, cad add fasten --from-fits writes it" },
94
+ "depth": { "$ref": "#/$defs/expr", "description": "With holes: how far each screw's path must be clear (mm)" },
95
+ "direction": { "enum": ["up", "down"], "description": "With holes: up for screws from below" },
96
+ "ring": { "$ref": "#/$defs/expr", "description": "With holes: how far round each screw there must be material (mm, default the hole's diameter)" },
97
+ "into": { "type": "string", "description": "With holes: the part the screws go into" },
98
98
  "within": { "$ref": "#/$defs/expr", "description": "With an anchor name (item@anchor): only faces this close to it (mm, default 10)" }
99
99
  }
100
100
  },
@@ -118,8 +118,13 @@
118
118
  "address": { "type": "string", "pattern": "^[a-z][\\w-]*(\\.[\\w-]+)+(@-?\\d+(\\.\\d+)?%?)?$", "description": "An anchor by name, <item>.<anchor>; on a beam an end (x1.a, x1.b), an end bore (x1.a.1), or a face with a distance from end a (x1.top@120), from end b (x1.top@-20) or a share of the length (x1.top@50%), the second slot of a face x1.top.2@120" },
119
119
  "fasten": {
120
120
  "type": "object",
121
+ "description": "A screwed joint: the screw by thread and head, the parts it passes in order, the part it ends in, and where the screws go (at, an anchor each, or holes, a part's own holes)",
121
122
  "required": ["screw", "through", "into"],
122
123
  "additionalProperties": false,
124
+ "defaultSnippets": [
125
+ { "label": "screws at anchors", "body": { "screw": "${1:M3 cap}", "through": ["${2:bracket}"], "into": "${3:base}", "at": ["${4:bracket.h1}", "${5:bracket.h2}"] } },
126
+ { "label": "screws in a part's own holes", "body": { "screw": "${1:M3 cap}", "through": ["${2:lid}"], "into": "${3:box}", "holes": "${4:lid.body}" } }
127
+ ],
123
128
  "properties": {
124
129
  "screw": { "type": "string", "description": "The screw, by its thread and head: M3 cap, M4 button, M3x12 countersunk, 6-32 UNC cap (the names @rulecad/fixings reads). With no length in the name, the shortest sold length that fits is chosen" },
125
130
  "through": { "type": "array", "items": { "type": "string" }, "description": "The parts the screw passes through, in order from under its head: items (bracket) or item.part (bracket.body). Each needs a clearance hole for the thread" },
@@ -13,7 +13,7 @@
13
13
  "version": { "type": "string", "pattern": "^\\d+\\.\\d+\\.\\d+" },
14
14
  "description": { "type": "string" },
15
15
  "note": { "type": "string" },
16
- "uses": { "type": "array", "items": { "type": "string" }, "description": "Rule sets (x1c, m3) and other objects (project/object, optionally @version)" },
16
+ "uses": { "type": "array", "items": { "type": "string" }, "description": "Rule sets (x1c, m3) and other objects (project/object, optionally @version). A screw's holes need none listed: v.fixings reads the thread's set, the printer and the material itself" },
17
17
  "material": { "type": "string" },
18
18
  "orientation": { "type": "string", "description": "How it sits on the bed, and anything the printer or the person assembling it should know" },
19
19
  "role": { "enum": ["part", "reference", "board"], "description": "reference: drawn faint and never printed (a switch, a tablet); board: a circuit board" },
@@ -48,7 +48,7 @@
48
48
  "required": ["value"],
49
49
  "additionalProperties": false,
50
50
  "properties": {
51
- "value": { "description": "A number, an expression (x1c.line * 3), or for typed variables a name or text", "type": ["number", "string", "boolean", "array", "object"] },
51
+ "value": { "description": "A number, an expression (x1c.line * 3), or for typed variables a name or text. A hole for a screw, a nut or an insert is no typed number: draw it with v.fixings in the .js (v.fixings.hole('M3', { kind: 'pilot' })), and say how parts screw together with a fasten in the assembly", "type": ["number", "string", "boolean", "array", "object"] },
52
52
  "unit": { "type": "string" },
53
53
  "rule": { "type": "string", "description": "Why the value is what it is" },
54
54
  "label": { "type": "string" },