@rulecad/cad 0.7.9 → 0.7.11

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
@@ -90,6 +90,9 @@ variable says which rule it comes from. Follow these steps; they're what makes a
90
90
  `"overhang"` to the way up it will print, so the result needs no supports, and give horizontal bosses and holes a
91
91
  `teardrop` (`"down"`, `"up"`). Draw the part as it prints; if that isn't the problem's frame, give its `analysis` a
92
92
  `rotate` and `move` back. Build with `v.shapes.optimisedSolid(...)` and `"engine": "manifold"`; look at the four views.
93
+ The result is made printable before it's drawn (hanging cells emptied, tips lifted, loose bits dropped), but a kept
94
+ region is never emptied: if `cad optimise` says part of one has nothing holding it up, give the part more material
95
+ to stand it on, or keep less. `cad check` tests the part for one piece and nothing starting in mid-air.
93
96
  - Before reporting numbers for a final shape, verify them: give the `analysis` a smaller `cell` (solids: about a
94
97
  third of the problem's) and check the stiffness has settled (`fea.settled`). Report the verified numbers, the safety
95
98
  factor, and where the peak stress is.
package/README.md CHANGED
@@ -369,8 +369,15 @@ with numbers as expressions like an object's (schema: `schemas/problem.schema.js
369
369
  gives, and draws the layout in `.preview/`. The result is kept, keyed by everything it's made from, so it's only
370
370
  worked out again when something it depends on changes.
371
371
 
372
- A 3D result keeps only the pieces that touch a support, a load or a kept region. A few grey cells away from the rest
373
- can close into a piece of their own, which would print as a stray bit. `cad optimise` says how many it dropped.
372
+ A 3D result with a build direction is made printable before it's drawn: built up layer by layer from the bed, every
373
+ cell that nothing holds up (an icicle, a loose bit, a sheet hanging under an edge: whatever the optimiser's smooth
374
+ overhang filter let through) is emptied, with whatever rested on it; a bridge may run out half the printer's span.
375
+ Kept regions always stay, and the optimiser is made to hold them up: a kept cell with nothing under it costs it as
376
+ much as the whole part's compliance. If there wasn't material enough to stand a kept region on, `cad optimise` says
377
+ how much of it is left hanging. Then the drawn surface is tipped back to the printer's overhang limit and any tip left
378
+ hanging in mid-air is lifted, and only the pieces that touch a support, a load or a kept region are kept (a few grey
379
+ cells away from the rest can close into a stray bit). `cad optimise` says how many cells it emptied and pieces it
380
+ dropped, and `cad check` tests the part for one piece and nothing starting in mid-air.
374
381
 
375
382
  `cad optimise <project>/<problem> --watch [port]` runs it afresh and serves a live view at `http://127.0.0.1:8090/`:
376
383
  the densities as they change (a 3D part as a surface you can turn, and from three sides), a chart of its compliance and how much it still changes, with a scrubber to look back at any earlier step, its run time and time left, the machine it runs on, and a button that stops the run. It shows the end for ten minutes after the run finishes.
@@ -418,8 +425,11 @@ Both take expressions, so a variant can lie the part down:
418
425
  "analysis": [{ "problem": "bracket", "rotate": ["0 - 90 * side", 0, 0], "move": [0, "0 - side * bench.bracket_h / 2", 0] }]
419
426
  ```
420
427
 
421
- 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
422
- optimisation takes a few minutes. The result is kept, so it's worked out once.
428
+ Grids in 3D are solved by multigrid, with the solver's products shared over the machine's cores. A 16 × 16 × 40 grid
429
+ (about 10,000 cubes) takes a second or two a step, a 32 × 32 × 40 one (41,000 cubes) about five seconds on an eight-core
430
+ laptop and a 40 × 40 × 48 one (77,000 cubes) about thirteen, so a whole optimisation of a few hundred steps takes from a
431
+ few minutes to an hour. Finer cells cost more than their number: each step's solve takes longer to settle too. The
432
+ result is kept, so it's worked out once.
423
433
 
424
434
  ### Analysis
425
435
 
package/bin/cad.mjs CHANGED
@@ -140,7 +140,8 @@ async function optimiseProblem(ref) {
140
140
  if (view) { view.close(`finished: ${r.steps} steps${r.converged ? '' : ', not settled'}`, { hold: 10 * 60e3 }); say(`the live view shows the result for ten more minutes (Ctrl-C to stop it)`); }
141
141
  const p = await readProblem(project, name), cells = r.dim === 3 ? `${r.grid.nx} × ${r.grid.ny} × ${r.grid.nz}` : `${r.grid.nx} × ${r.grid.ny}`;
142
142
  const lines = [`${p.material.name}${r.dim === 2 ? `, ${fmt(r.thickness)} mm thick` : ''}: ${r.cases.map((c) => c.moment ? `${c.name} ${sig(c.angle * 180 / Math.PI)}° (${sig(c.torsion / 1000 * Math.PI / 180)} N·m/°)` : `${c.name} ${sig(c.deflection)} mm (${sig(c.stiffness)} N/mm)`).join(', ')}`,
143
- `${fmt(r.volume * 100)}% of the space, ${cells} cells of ${fmt(r.grid.h)} mm, ${r.steps} steps${r.converged ? '' : ' (stopped: not settled)'}${r.overhang ? `, printable built ${r.overhang}` : ''}${r.stress ? `, peak stress ${sig(r.stress.peak)} MPa (limit ${sig(r.stress.limit)})` : ''}${r.dropped ? `; ${r.dropped.length} loose ${r.dropped.length === 1 ? 'piece' : 'pieces'} dropped (${sig(r.dropped.reduce((t, v) => t + v, 0))} mm³)` : ''}`];
143
+ `${fmt(r.volume * 100)}% of the space, ${cells} cells of ${fmt(r.grid.h)} mm, ${r.steps} steps${r.converged ? '' : ' (stopped: not settled)'}${r.overhang ? `, printable built ${r.overhang}` : ''}${r.stress ? `, peak stress ${sig(r.stress.peak)} MPa (limit ${sig(r.stress.limit)})` : ''}${r.dropped ? `; ${r.dropped.length} loose ${r.dropped.length === 1 ? 'piece' : 'pieces'} dropped (${sig(r.dropped.reduce((t, v) => t + v, 0))} mm³)` : ''}${r.emptied ? `; ${r.emptied} ${r.emptied === 1 ? 'cell' : 'cells'} that wouldn't print emptied` : ''}`,
144
+ ...(r.unsupported > 0.01 ? [`${fmt(r.unsupported * 100)}% of the kept regions has nothing holding it up (at ${r.overhang ? 'the printer\'s overhang limit' : '45°'} or less): give the part more material to stand them on, or keep less`] : [])];
144
145
  let out;
145
146
  if (r.dim === 3) {
146
147
  out = paths.preview(project, `${name.replace(':', '-')}-optimised.png`);
package/lib/optimise.mjs CHANGED
@@ -11,7 +11,8 @@ import { host } from './host.mjs';
11
11
  import { paths, printerOf } from './workspace.mjs';
12
12
  import { resolve, evaluate } from './rules.mjs';
13
13
  import { located } from './errors.mjs';
14
- import { discretise, optimise, outlines, surface, area, bounds, resultant } from '@rulecad/topopt';
14
+ import { discretise, optimise, outlines, surface, area, bounds, resultant, printable } from '@rulecad/topopt';
15
+ import { threadsBackend } from '@rulecad/fea/threads'; // the solver's products shared over the machine's cores
15
16
  import { grid as makeGrid, analyse, positions } from '@rulecad/fea';
16
17
 
17
18
  const VERSION = 2, source = (m) => fs.readFileSync(new URL(import.meta.resolve(m)), 'utf8'); // the solver's code: a change remakes results
@@ -75,7 +76,7 @@ export async function readProblem(project, ref) {
75
76
  if (dim === 2 && !(problem.thickness > 0)) throw new Error(`${path.basename(file)}: a flat (2D) problem needs a thickness`);
76
77
  const outline = { smooth: p.outline?.smooth ?? 4, minArea: p.outline?.min_area != null ? ev(p.outline.min_area) : problem.cell ** 2 * 2 };
77
78
  // 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 }; }
79
+ if (dim === 3 && problem.overhang) { const n = printerOf(project), pr = resolve({ uses: [n] }).values[n]; outline.overhang = { up: problem.overhang, angle: pr?.overhang ?? 45, bridge: pr?.bridge ?? 5 }; }
79
80
  return { ref, name, variant, file, dim, title: p.title || name, material: { name: p.material, ...m }, problem, outline, critical: !!p.critical };
80
81
  }
81
82
 
@@ -94,7 +95,7 @@ export async function optimised(project, ref, { onStep, fresh = false } = {}) {
94
95
  const hash = digest([VERSION, r.problem, r.outline, topopt, source('@rulecad/fea'), fs.readFileSync(new URL(import.meta.url), 'utf8')]), file = paths.cache('optimise', `${hash}.json`);
95
96
  if (!fresh && !process.env.CAD_NO_CACHE && host.exists(file)) return { ...JSON.parse(host.readText(file)), cached: true };
96
97
  const spec = discretise(r.problem), densities = paths.cache('densities', `${digest([VERSION, r.problem, solver, source('@rulecad/fea')])}.json`);
97
- const o = !fresh && !process.env.CAD_NO_CACHE && host.exists(densities) ? (({ density, ...x }) => ({ ...x, density: Float64Array.from(density) }))(JSON.parse(host.readText(densities))) : optimise(spec, { onStep: onStep && ((st) => onStep({ ...st, grid: spec.grid })) });
98
+ const o = !fresh && !process.env.CAD_NO_CACHE && host.exists(densities) ? (({ density, ...x }) => ({ ...x, density: Float64Array.from(density) }))(JSON.parse(host.readText(densities))) : optimise(spec, { backend: threadsBackend, onStep: onStep && ((st) => onStep({ ...st, grid: spec.grid })) });
98
99
  if (!host.exists(densities)) host.writeText(densities, JSON.stringify({ ...o, density: Array.from(o.density) }));
99
100
  // Regions as data: a part's triangles are named, not copied
100
101
  const named = (list) => list.map(({ triangles, ...x }) => x);
@@ -104,7 +105,16 @@ export async function optimised(project, ref, { onStep, fresh = false } = {}) {
104
105
  })() : (() => {
105
106
  // The surface's level, found so the solid holds as much material as the optimised densities do (drawn at 0.5,
106
107
  // thin members come out thinner than they are)
107
- 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 });
108
+ // Printed without supports: before it's drawn at a level, whatever wouldn't print at that level (hanging with nothing
109
+ // under it) is emptied (printable, in topopt); kept regions stay. A bridge may run out half the printer's span.
110
+ const ov = r.outline.overhang, keep = Uint8Array.from(spec.passive, (p) => (p === 1 ? 1 : 0)), emptied = new Map();
111
+ const clean = (iso) => {
112
+ if (!ov) return o.density;
113
+ const c = printable(spec.grid, o.density, { up: ov.up, iso, keep, reach: Math.floor(ov.bridge / 2 / spec.grid.h) });
114
+ emptied.set(iso, c.emptied);
115
+ return c.density;
116
+ };
117
+ const want = o.density.reduce((t, d) => t + d, 0) * spec.grid.h ** 3, at = (iso) => surface(spec.grid, clean(iso), spec.origin, { smooth: r.outline.smooth, iso, overhang: ov });
108
118
  let lo = 0.2, hi = 0.8, best = at(0.5), iso = 0.5;
109
119
  for (let k = 0; k < 10 && Math.abs(meshVolume(best.vertices, best.faces) / want - 1) > 0.003; k++) {
110
120
  if (meshVolume(best.vertices, best.faces) < want) hi = iso; else lo = iso; // a lower level encloses more
@@ -112,12 +122,12 @@ export async function optimised(project, ref, { onStep, fresh = false } = {}) {
112
122
  }
113
123
  const anchors = [...r.problem.supports, ...r.problem.loads, ...(r.problem.keep || [])].map(bounds);
114
124
  const { vertices, faces, dropped } = anchored(best, anchors, spec.grid.h);
115
- return { mesh: { vertices: vertices.map((v) => v.map((c) => round(c, 3))), faces }, solid: meshVolume(vertices, faces), iso, ...(dropped.length ? { dropped } : {}) };
125
+ return { mesh: { vertices: vertices.map((v) => v.map((c) => round(c, 3))), faces }, solid: meshVolume(vertices, faces), iso, ...(dropped.length ? { dropped } : {}), ...(emptied.get(iso) ? { emptied: emptied.get(iso) } : {}) };
116
126
  })();
117
127
  const result = {
118
128
  problem: ref, title: r.title, hash, dim: r.dim, material: r.material.name, ...(r.dim === 2 ? { thickness: r.problem.thickness } : {}),
119
129
  grid: spec.grid, origin: spec.origin, regions: { keep: named(spec.regions.keep), avoid: named(spec.regions.avoid) }, ...shape,
120
- volume: o.volume, compliance: o.compliance, cases: o.cases, ...(o.stress ? { stress: o.stress } : {}), overhang: r.problem.overhang || null,
130
+ volume: o.volume, compliance: o.compliance, cases: o.cases, ...(o.unsupported ? { unsupported: o.unsupported } : {}), ...(o.stress ? { stress: o.stress } : {}), overhang: r.problem.overhang || null,
121
131
  steps: o.steps, converged: o.converged, seconds: o.seconds, history: o.history, density: Array.from(o.density, (d) => round(d, 3)),
122
132
  supports: named(r.problem.supports), loads: named(r.problem.loads),
123
133
  };
@@ -189,7 +199,7 @@ function analyseSolid(triangles, r, cell) {
189
199
  const { problem } = r, spec = discretise({ ...problem, cell, space: { triangles }, keep: [], avoid: [], volume: 1 });
190
200
  const g = makeGrid(spec.grid), density = solidFill(triangles, spec.grid, spec.origin);
191
201
  if (!density.some((d) => d)) throw new Error('the part is smaller than one cell of the problem: use a smaller cell');
192
- const a = analyse({ grid: g, density, material: problem.material, fixed: spec.fixed, loads: spec.loads.map((l) => l.force) });
202
+ const a = analyse({ grid: g, density, material: problem.material, fixed: spec.fixed, loads: spec.loads.map((l) => l.force), backend: threadsBackend });
193
203
  const cases = spec.loads.map((l, c) => measures(l.name, l.size, l.moment, a, c, density, spec.grid, spec.origin));
194
204
  return { problem: r.ref, dim: 3, material: r.material, critical: r.critical, cases, volume: volume(triangles), grid: spec.grid, origin: spec.origin, density };
195
205
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rulecad/cad",
3
- "version": "0.7.9",
3
+ "version": "0.7.11",
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": {