@rulecad/cad 0.7.0 → 0.7.1

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/AGENTS.md CHANGED
@@ -88,7 +88,8 @@ variable says which rule it comes from. Follow these steps; they're what makes a
88
88
  `analysis` to the object so `cad check` reports its stiffness; report those numbers with their load and material.
89
89
  - Solid parts (brackets, joints) are 3D problems: regions with a z, a named part as the space if it helps. Set
90
90
  `"overhang"` to the way up it will print, so the result needs no supports, and give horizontal bosses and holes a
91
- `teardrop`. Build with `v.shapes.optimisedSolid(...)` and `"engine": "manifold"`; look at the four views.
91
+ `teardrop` (`"down"`, `"up"`). Draw the part as it prints; if that isn't the problem's frame, give its `analysis` a
92
+ `rotate` and `move` back. Build with `v.shapes.optimisedSolid(...)` and `"engine": "manifold"`; look at the four views.
92
93
  - Say plainly that the analysis is coarse and the material values are data-sheet figures: anything that could hurt
93
94
  someone if it broke needs testing, not just a check.
94
95
 
package/README.md CHANGED
@@ -382,8 +382,9 @@ holes come out true.
382
382
 
383
383
  A problem whose regions have a z is solid. It has no thickness, and takes more kinds of region:
384
384
  - **A box:** `size`.
385
- - **A cylinder:** `r`, `length` and `axis`. Along x or y it can be a teardrop: `"teardrop": "-z"` for a boss,
386
- `"+z"` for a hole. It gets a 45° point, so it prints.
385
+ - **A cylinder:** `r`, `length` and `axis`. It can be a teardrop: `"teardrop": "down"` for a boss, `"up"` for a
386
+ hole. It gets a 45° point that way, so it prints. Up is the problem's `overhang` (+z when it's free), so the same
387
+ problem prints either way up; `"+y"` and the like point it a fixed way. Along its axis, it has no point.
387
388
  - **A ball:** `r`.
388
389
  - **A named part of another design in the project:** `{ "part": "envelope.body" }`.
389
390
 
@@ -392,8 +393,17 @@ without supports standing that way up (a variable holding `"+z"` or `"none"` wor
392
393
 
393
394
  `cad optimise` draws the result's four views in `.preview/`. A design builds the result with
394
395
  `v.shapes.optimisedSolid(v.problems[v.problem], { ops: v.ops })`, which adds the keep regions and cuts the avoid
395
- regions exactly. The surface is a big mesh, so set the object's `"engine": "manifold"`. An `analysis` of a solid part
396
- voxelises it on the problem's grid.
396
+ regions exactly. The surface is a big mesh, so set the object's `"engine": "manifold"`. With `"overhang"` set, the
397
+ surface is also kept within the printer's overhang limit: faces smoothing tipped past it are tipped back. An
398
+ `analysis` of a solid part voxelises it on the problem's grid.
399
+
400
+ A part prints in one orientation and is loaded in another. Draw it as it prints, and turn it back for its analysis:
401
+ `"rotate"` (degrees about x, then y, then z, as JSCAD's `rotate`) then `"move"` (mm) put it in the problem's frame.
402
+ Both take expressions, so a variant can lie the part down:
403
+
404
+ ```json
405
+ "analysis": [{ "problem": "bracket", "rotate": ["0 - 90 * side", 0, 0], "move": [0, "0 - side * bench.bracket_h / 2", 0] }]
406
+ ```
397
407
 
398
408
  Grids in 3D are solved by multigrid. A 16 × 16 × 40 grid (about 10,000 cubes) takes a second or two a step, so a whole
399
409
  optimisation takes a few minutes. The result is kept, so it's worked out once.
package/bin/cad.mjs CHANGED
@@ -146,13 +146,27 @@ async function optimiseProblem(ref) {
146
146
  }
147
147
 
148
148
  // An object's analyses for cad check: each problem's load cases on the part, and its volume against a target
149
+ function place(triangles, [rx, ry, rz], move) {
150
+ const [cx, sx, cy, sy, cz, sz] = [rx, ry, rz].flatMap((deg) => [Math.cos((deg * Math.PI) / 180), Math.sin((deg * Math.PI) / 180)]);
151
+ return triangles.map((t) => t.map((p) => {
152
+ let [x, y, z = 0] = p;
153
+ [y, z] = [y * cx - z * sx, y * sx + z * cx];
154
+ [x, z] = [x * cy + z * sy, -x * sy + z * cy];
155
+ [x, y] = [x * cz - y * sz, x * sz + y * cz];
156
+ return [x + move[0], y + move[1], z + (move[2] || 0)].slice(0, p.length);
157
+ }));
158
+ }
159
+
149
160
  async function analyses(b, d, tag) {
150
161
  const lookup = (n) => { const v = n.split('.').reduce((o, k) => (o == null ? o : o[k]), b.values); if (typeof v !== 'number') throw new Error(`analysis: "${n}" isn't a number`); return v; };
151
162
  const all = [];
152
163
  for (const a of b.object.analysis || []) all.push(...await (async () => {
153
164
  const part = a.part ? b.parts.find((p) => p.name === a.part) : b.parts.find((p) => p.role !== 'support');
154
165
  if (!part) return [{ level: 'fail', text: `analysis: no part "${a.part}"`, code: 'fea.part' }];
155
- const r = await analysePart(part.triangles, d.project, a.problem), m = r.material, limit = a.max_deflection != null ? evaluate(a.max_deflection, lookup) : null;
166
+ // A part drawn in its print orientation, turned back into the problem's frame: rotated (degrees about x, then y,
167
+ // then z, as JSCAD's rotate), then moved
168
+ const turned = a.rotate || a.move ? place(part.triangles, (a.rotate || [0, 0, 0]).map((x) => evaluate(x, lookup)), (a.move || [0, 0, 0]).map((x) => evaluate(x, lookup))) : part.triangles;
169
+ const r = await analysePart(turned, d.project, a.problem), m = r.material, limit = a.max_deflection != null ? evaluate(a.max_deflection, lookup) : null;
156
170
  let out = null;
157
171
  if (r.dim !== 3) { // a flat part's picture: its cells as analysed, with the supports and loads
158
172
  out = paths.preview(d.project, `${d.name}${tag}-analysis-${a.problem.replace(':', '-')}.svg`);
package/lib/optimise.mjs CHANGED
@@ -8,7 +8,7 @@ import fs from 'node:fs';
8
8
  import path from 'node:path';
9
9
  import crypto from 'node:crypto';
10
10
  import { host } from './host.mjs';
11
- import { paths } from './workspace.mjs';
11
+ import { paths, printerOf } from './workspace.mjs';
12
12
  import { resolve, evaluate } from './rules.mjs';
13
13
  import { located } from './errors.mjs';
14
14
  import { discretise, optimise, outlines, surface, area } from '@rulecad/topopt';
@@ -47,19 +47,21 @@ export async function readProblem(project, ref) {
47
47
  }
48
48
  return parts.get(ref);
49
49
  };
50
+ // The build direction: a direction ('+z'), or a variable holding one
51
+ const overhang = p.overhang ? (typeof values[p.overhang] === 'string' ? values[p.overhang] : p.overhang) : null;
52
+ // A teardrop's point: a direction, or up or down the build direction (+z, as drawn, when it's free)
53
+ const point = (t) => { const up = overhang && overhang !== 'none' ? overhang : '+z'; return t === 'up' ? up : t === 'down' ? (up[0] === '-' ? '+' : '-') + up[1] : t; };
50
54
  const region = async (r) => {
51
55
  if (r.part) return { triangles: await part(r.part), part: r.part };
52
56
  if (r.points) return { points: r.points.map(pt) };
53
57
  if (r.size) return { at: pt(r.at), size: r.size.map(ev) };
54
- if (r.length != null) return { at: pt(r.at), r: ev(r.r), length: ev(r.length), axis: r.axis || 'z', ...(r.teardrop ? { teardrop: r.teardrop } : {}) };
58
+ if (r.length != null) { const axis = r.axis || 'z', t = r.teardrop && point(r.teardrop); return { at: pt(r.at), r: ev(r.r), length: ev(r.length), axis, ...(t && t[1] !== axis ? { teardrop: t } : {}) }; } // along its axis, a teardrop has no point
55
59
  return { at: pt(r.at), ...(r.r != null ? { r: ev(r.r) } : {}) };
56
60
  };
57
61
  const regions = (list) => Promise.all([list || []].flat().map(region));
58
62
  const m = values[p.material];
59
63
  if (typeof m?.modulus !== 'number' || typeof m?.poisson !== 'number') throw new Error(`material ${p.material}: its rule set needs modulus (MPa) and poisson`);
60
64
  const space = await regions(p.space), dim = space[0].triangles || space[0].length != null || (space[0].size || space[0].at)?.length === 3 ? 3 : 2;
61
- // The build direction: a direction ('+z'), or a variable holding one
62
- const overhang = p.overhang ? (typeof values[p.overhang] === 'string' ? values[p.overhang] : p.overhang) : null;
63
65
  const problem = {
64
66
  space, keep: await regions(p.keep), avoid: await regions(p.avoid),
65
67
  supports: await Promise.all(p.supports.map(async (s) => ({ ...(await region(s)), fix: s.fix || 'xyz'.slice(0, dim) }))),
@@ -72,6 +74,8 @@ export async function readProblem(project, ref) {
72
74
  };
73
75
  if (dim === 2 && !(problem.thickness > 0)) throw new Error(`${path.basename(file)}: a flat (2D) problem needs a thickness`);
74
76
  const outline = { smooth: p.outline?.smooth ?? 4, minArea: p.outline?.min_area != null ? ev(p.outline.min_area) : problem.cell ** 2 * 2 };
77
+ // A printable solid's surface is kept within the printer's overhang limit too (smoothing can tip faces past it)
78
+ if (dim === 3 && problem.overhang) { const n = printerOf(project), pr = resolve({ uses: [n] }).values[n]; outline.overhang = { up: problem.overhang, angle: pr?.overhang ?? 45 }; }
75
79
  return { ref, name, variant, file, dim, title: p.title || name, material: { name: p.material, ...m }, problem, outline };
76
80
  }
77
81
 
@@ -99,7 +103,7 @@ export async function optimised(project, ref, { onStep, fresh = false } = {}) {
99
103
  })() : (() => {
100
104
  // The surface's level, found so the solid holds as much material as the optimised densities do (drawn at 0.5,
101
105
  // thin members come out thinner than they are)
102
- const want = o.density.reduce((t, d) => t + d, 0) * spec.grid.h ** 3, at = (iso) => surface(spec.grid, o.density, spec.origin, { smooth: r.outline.smooth, iso });
106
+ const want = o.density.reduce((t, d) => t + d, 0) * spec.grid.h ** 3, at = (iso) => surface(spec.grid, o.density, spec.origin, { smooth: r.outline.smooth, iso, overhang: r.outline.overhang });
103
107
  let lo = 0.2, hi = 0.8, best = at(0.5), iso = 0.5;
104
108
  for (let k = 0; k < 10 && Math.abs(meshVolume(best.vertices, best.faces) / want - 1) > 0.003; k++) {
105
109
  if (meshVolume(best.vertices, best.faces) < want) hi = iso; else lo = iso; // a lower level encloses more
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rulecad/cad",
3
- "version": "0.7.0",
3
+ "version": "0.7.1",
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": {
@@ -50,12 +50,12 @@
50
50
  "@rulecad/fit": "^0.2.1",
51
51
  "@rulecad/model-link": "^0.2.0",
52
52
  "@rulecad/versions": "^0.1.0",
53
- "@rulecad/shapes": "^0.4.0",
53
+ "@rulecad/shapes": "^0.4.1",
54
54
  "@rulecad/rules": "^0.3.0",
55
55
  "@jscad/modeling": "^2.12.0",
56
56
  "three": "^0.186.1",
57
57
  "@rulecad/fea": "^0.2.0",
58
- "@rulecad/topopt": "^0.2.0"
58
+ "@rulecad/topopt": "^0.2.1"
59
59
  },
60
60
  "peerDependencies": {
61
61
  "@rulecad/pcb": "^0.1.0 || ^0.2.0 || ^0.3.0"
@@ -59,7 +59,9 @@
59
59
  "part": { "type": "string", "description": "The part analysed (default: the first that isn't a support)" },
60
60
  "max_deflection": { "$ref": "#/$defs/expr", "description": "Fail if any load case moves its load further than this, in mm" },
61
61
  "volume": { "$ref": "#/$defs/expr", "description": "Fail if the part's volume differs from this (mm³) by more than tolerance" },
62
- "tolerance": { "type": "number", "description": "For volume: the share it may differ by (default 0.01)" }
62
+ "tolerance": { "type": "number", "description": "For volume: the share it may differ by (default 0.01)" },
63
+ "rotate": { "type": "array", "items": { "$ref": "#/$defs/expr" }, "minItems": 3, "maxItems": 3, "description": "Turns the part into the problem's frame first, for a part drawn as it prints: degrees about x, then y, then z (as JSCAD's rotate)" },
64
+ "move": { "type": "array", "items": { "$ref": "#/$defs/expr" }, "minItems": 2, "maxItems": 3, "description": "Then moves it, in mm" }
63
65
  }
64
66
  },
65
67
  "part": {
@@ -205,11 +205,8 @@
205
205
  "type": "string"
206
206
  },
207
207
  "teardrop": {
208
- "enum": [
209
- "-z",
210
- "+z"
211
- ],
212
- "description": "3D: a cylinder along x or y with a 45° point that way, so it prints without support: '-z' for a boss (its underside), '+z' for a hole (its roof)"
208
+ "enum": ["up", "down", "+z", "-z", "+y", "-y", "+x", "-x"],
209
+ "description": "3D: a 45° point on the cylinder that way, so it prints without support: down for a boss (its underside), up for a hole (its roof). up and down follow the problem's overhang (+z when it's free); along the cylinder's axis it has none"
213
210
  }
214
211
  }
215
212
  },
@@ -268,11 +265,8 @@
268
265
  "description": "Which ways it's held: x, y, z (default all)"
269
266
  },
270
267
  "teardrop": {
271
- "enum": [
272
- "-z",
273
- "+z"
274
- ],
275
- "description": "3D: a cylinder along x or y with a 45° point that way, so it prints without support: '-z' for a boss (its underside), '+z' for a hole (its roof)"
268
+ "enum": ["up", "down", "+z", "-z", "+y", "-y", "+x", "-x"],
269
+ "description": "3D: a 45° point on the cylinder that way, so it prints without support: down for a boss (its underside), up for a hole (its roof). up and down follow the problem's overhang (+z when it's free); along the cylinder's axis it has none"
276
270
  }
277
271
  }
278
272
  },
@@ -336,11 +330,8 @@
336
330
  "description": "Its load case (default load)"
337
331
  },
338
332
  "teardrop": {
339
- "enum": [
340
- "-z",
341
- "+z"
342
- ],
343
- "description": "3D: a cylinder along x or y with a 45° point that way, so it prints without support: '-z' for a boss (its underside), '+z' for a hole (its roof)"
333
+ "enum": ["up", "down", "+z", "-z", "+y", "-y", "+x", "-x"],
334
+ "description": "3D: a 45° point on the cylinder that way, so it prints without support: down for a boss (its underside), up for a hole (its roof). up and down follow the problem's overhang (+z when it's free); along the cylinder's axis it has none"
344
335
  }
345
336
  }
346
337
  }