@rulecad/cad 0.7.70 → 0.7.75

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/CHANGELOG.md CHANGED
@@ -5,6 +5,25 @@ print (a hole's diameter, a seat's depth, a part's outline). Each release lists
5
5
  its reason. A **fasten** is an entry in an assembly's `"fasten"` list: one screwed joint, naming the screw by thread
6
6
  and head, the parts it passes through and the part it ends in.
7
7
 
8
+ ## 0.7.75: parts milled, cast or printed in a powder bed
9
+ ### Added
10
+
11
+ - Problems take how the part is made: `"mill": "+z"` (milled from that side, no undercuts), `"cast": "+z"` or
12
+ `"cast": { "draw": "z", "parting": 6 }` (a mould drawn off one way, or two halves from a parting plane), with
13
+ `"symmetry": { "mirror": "y", "turns": 4, "axis": "z" }` and `"extrude": "x"` for any process, and
14
+ `"voids": "refused"` for a metal powder bed print. The problem schema has each field.
15
+ - `cad optimise` says how a milled or cast part is made and counts its undercuts (none unless an avoid region sits
16
+ behind material), says what is mirrored or extruded, and fails on a problem that refuses voids with each enclosed
17
+ void's volume and centre. `--json` gives `process`, `undercuts`, `symmetry`, `extrude`, `grey` and `voids`.
18
+ - `cad optimise` warns when more than a tenth of a milled, cast, symmetric or extruded result is grey (neither empty
19
+ nor solid), and refuses a symmetry that swaps the side the part is made from for its opposite.
20
+ - Rule sets `mill-3axis`, `mould-injection` and `dmls` (from @rulecad/rules) to read the processes' limits from.
21
+
22
+ ### Changed
23
+
24
+ - Every optimised result is made again once, since topopt's solver file changed; a problem without the new fields
25
+ gets the same densities as before. No printed size moved.
26
+
8
27
  ## 0.7.70: printed bolts into slot nuts, a slotted snap pot
9
28
  ### Fixed
10
29
 
package/README.md CHANGED
@@ -1522,6 +1522,24 @@ A problem whose regions have a z is solid. It has no thickness, and takes more k
1522
1522
  Supports and loads can be any region; a force has x, y and z. `"overhang": "+z"` keeps only material that prints
1523
1523
  without supports standing that way up (a variable holding `"+z"` or `"none"` works too, so a variant can switch it).
1524
1524
 
1525
+ A part made another way says so in place of `overhang` (one process to a problem; two are refused by name):
1526
+ - **Milled:** `"mill": "+z"` (the tool cuts down from above). No undercuts: a cell is as solid as every cell
1527
+ between it and the tool. `cad optimise` says `milled from +z: no undercuts`, or counts the cells an avoid region
1528
+ or the space's own edge hides behind material. The `mill-3axis` rule set holds the walls, corners and pockets a 3-axis machine manages.
1529
+ - **Cast or moulded:** `"cast": "+z"` for a mould drawn off one way, or `"cast": { "draw": "z", "parting": 6 }` for
1530
+ two halves drawn off both ways from a parting plane (mm along the axis; the space's middle when left out). Draft
1531
+ (`mould-injection`: a degree or so) is less than a cell, so it is left to the drawing.
1532
+ - **Printed in a metal powder bed:** `"overhang": "+z"` with `"voids": "refused"`. Powder left in an enclosed void
1533
+ can't be got out, so `cad optimise` fails with each void's volume and centre: open it with an avoid region (an
1534
+ escape hole, `dmls.escape_hole` wide) or allow voids. The `dmls` rule set holds the process's limits.
1535
+
1536
+ Any of them can also be `"symmetry": { "mirror": "y" }` (mirrored across the grid's middle planes; `"turns": 2` or
1537
+ `4` about `"axis"` for half or quarter turns) or `"extrude": "x"` (one section all along x, a part cut from a bar).
1538
+ A mirror or turn that would swap the side the part is made from for its opposite is refused by name (a mould in two
1539
+ halves parted at the middle may be mirrored across its draw). With a process or a tie, `cad optimise` warns when more
1540
+ than a tenth of the design is grey (neither empty nor solid): the outline drawn is then a guess. A 2D section milled
1541
+ or cast across its own plane often comes out so; give the problem in 3D or make it from another side.
1542
+
1525
1543
  `cad optimise` draws the result's four views in `.preview/`. A design builds the result with
1526
1544
  `v.shapes.optimisedSolid(v.problems[v.problem], { ops: v.ops })`, which adds the keep regions and cuts the avoid
1527
1545
  regions exactly. The surface is a big mesh, so set the object's `"engine": "manifold"`. With `"overhang"` set, the
package/bin/cad.mjs CHANGED
@@ -255,7 +255,8 @@ async function optimiseProblem(ref) {
255
255
  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}`;
256
256
  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(', ')}`,
257
257
  `${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` : ''}${r.cleared ? `; sliced as the printer will, ${r.cleared} ${r.cleared === 1 ? 'cell' : 'cells'} under islands emptied` : ''}`,
258
- ...(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`] : [])];
258
+ ...(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`] : []),
259
+ ...(r.process ? [processLine(r)] : []), ...(r.voids?.length ? [voidsLine(r)] : [])];
259
260
  let out;
260
261
  if (r.dim === 3) {
261
262
  out = paths.preview(project, `${name.replace(':', '-')}-optimised.png`);
@@ -274,8 +275,28 @@ async function optimiseProblem(ref) {
274
275
  if (r.dim === 3) console.log(` ${fmt(r.volume * 100)}% of the space filled: ${fmt(r.solid / 1000)} cm³ (${fmt(r.solid / 1000 * (p.material.density || 0))} g of ${p.material.name})${r.overhang ? `, printable built ${r.overhang}` : ''}`);
275
276
  else console.log(` ${fmt(r.volume * 100)}% of the space filled, outline ${fmt(r.area)} mm² (${fmt(r.area * r.thickness / 1000)} cm³ before bosses and holes are made exact)`);
276
277
  console.log(` ${cells} cells of ${fmt(r.grid.h)} mm, ${r.steps} steps${r.converged ? ', settled' : ': stopped before it settled (raise steps)'}`);
278
+ if (r.process) console.log(` ${processLine(r)}`);
279
+ if (r.symmetry || r.extrude) console.log(` ${[r.symmetry?.mirror ? `mirrored across ${[...r.symmetry.mirror].join(' and ')}` : '', r.symmetry?.turns ? `the same each ${r.symmetry.turns === 4 ? 'quarter' : 'half'} turn about ${r.symmetry.axis || 'z'}` : '', r.extrude ? `one section along ${r.extrude}` : ''].filter(Boolean).join(', ')}`);
280
+ if (r.grey > 0.1) console.log(` warning: ${greyLine(r)}`);
281
+ if (r.voids?.length) { console.log(` error: ${voidsLine(r)}`); process.exitCode = 1; }
277
282
  console.log(out);
278
283
  }
284
+ // How a milled or cast result is made, and its undercuts (none, unless an avoid region or the space's own shape sits
285
+ // behind material: the optimiser keeps every cell as solid as those on its way out, but can't fill a cell it mustn't)
286
+ function processLine(r) {
287
+ const how = r.process.mill ? `milled from ${r.process.mill}` : typeof r.process.cast === 'string' ? `cast, drawn off ${r.process.cast}` : `cast in two halves drawn off along ${r.process.cast.draw} from ${fmt(r.process.cast.parting)} mm`;
288
+ return r.undercuts ? `${how}: ${r.undercuts} ${r.undercuts === 1 ? 'cell is' : 'cells are'} undercut (an avoid region, or the edge of the space, with material between it and the ${r.process.mill ? 'tool' : 'draw'}: move it, or make it from that side)` : `${how}: no undercuts`;
289
+ }
290
+ // A grey result (warned of above a tenth): its design cells between a tenth and nine tenths solid. A clear design has
291
+ // a few per cent, on its surfaces (the 3D cantilever milled from above 5.3%, free 2D cantilevers 1 to 3%): a tenth
292
+ // is a choice from those, an estimate
293
+ function greyLine(r) {
294
+ return `${fmt(r.grey * 100)}% of the design is grey (neither empty nor solid): the outline drawn at half density is a guess, not a part. A section milled, cast or extruded across its own plane often comes out so; give the problem in 3D, make it from another side, or allow more steps`;
295
+ }
296
+ // Enclosed voids, refused by the problem (powder that can't get out, a trapped core)
297
+ function voidsLine(r) {
298
+ return `${r.voids.length} enclosed ${r.voids.length === 1 ? 'void' : 'voids'} (${r.voids.map((v) => `${sig(v.volume)} ${r.dim === 3 ? 'mm³' : 'mm²'} at ${v.at.map(fmt).join(', ')}`).join('; ')}): powder or a core can't get out: open ${r.voids.length === 1 ? 'it' : 'each'} to the outside with an avoid region (an escape hole), or allow voids`;
299
+ }
279
300
 
280
301
  // An object's analyses for cad check: each problem's load cases on the part, and its volume against a target
281
302
  function place(triangles, [rx, ry, rz], move) {
package/lib/optimise.mjs CHANGED
@@ -11,7 +11,7 @@ import { host, readJson } 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, printable, refine, despike } from '@rulecad/topopt';
14
+ import { discretise, optimise, outlines, surface, area, bounds, resultant, printable, refine, despike, undercuts, voids, grey } from '@rulecad/topopt';
15
15
  import { islands } from '@rulecad/printcheck';
16
16
  import { threadsBackend } from '@rulecad/fea/threads'; // the solver's products shared over the machine's cores
17
17
  import { grid as makeGrid, analyse, positions } from '@rulecad/fea';
@@ -77,6 +77,9 @@ export async function readProblem(project, ref) {
77
77
  ...(p.filter != null ? { filter: ev(p.filter) } : {}), ...(p.penal != null ? { penal: ev(p.penal) } : {}),
78
78
  ...(p.projection != null ? { projection: p.projection } : {}), ...(p.steps != null ? { steps: p.steps } : {}),
79
79
  ...(overhang && overhang !== 'none' ? { overhang } : {}),
80
+ // How it's made other than printed: milled from a side, cast in a mould; symmetric, extruded (topopt's filters)
81
+ ...(p.mill ? { mill: p.mill } : {}), ...(p.cast ? { cast: typeof p.cast === 'string' ? p.cast : { draw: p.cast.draw, ...(p.cast.parting != null ? { parting: ev(p.cast.parting) } : {}) } } : {}),
82
+ ...(p.symmetry ? { symmetry: p.symmetry } : {}), ...(p.extrude ? { extrude: p.extrude } : {}),
80
83
  // A mechanism: where the output should move, along one axis, against a spring; a spring the input pushes against
81
84
  ...(p.output ? { output: { ...(await region(p.output)), direction: pt(p.output.direction), ...(p.output.spring != null ? { spring: ev(p.output.spring) } : {}) } } : {}),
82
85
  ...(p.input_spring != null ? { input_spring: ev(p.input_spring) } : {}),
@@ -87,7 +90,7 @@ export async function readProblem(project, ref) {
87
90
  // (outline.tip: faces the smoothing left steeper than the limit are tipped back to it too; off unless asked, since
88
91
  // tipped corners leave dips that slice as islands, and the layers step out no further for it)
89
92
  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, layer: pr?.layer ?? 0.2, line: pr?.line ?? 0.42, ...(p.outline?.tip ? { tip: true } : {}) }; problem.bridge = outline.overhang.bridge; } // (the optimiser bridges half that)
90
- return { ref, name, variant, file, dim, title: p.title || name, material: { name: p.material, ...m }, problem, outline, critical: !!p.critical };
93
+ return { ref, name, variant, file, dim, title: p.title || name, material: { name: p.material, ...m }, problem, outline, critical: !!p.critical, voids: p.voids || 'allowed' };
91
94
  }
92
95
 
93
96
  /**
@@ -105,6 +108,23 @@ function holdEmpty(spec) {
105
108
  for (let m = 0; m < g.nodes; m++) if (!on[m]) for (let a = 0; a < dim; a++) spec.fixed[dim * m + a] = 1;
106
109
  return spec;
107
110
  }
111
+ // How a result is made, and what that process can't make in it: for a milled or cast part, the cells a tool or mould
112
+ // half could not reach (undercuts, none by construction unless an avoid region or the space's own shape leaves material between a cell and its side), how grey it is; where enclosed voids are
113
+ // refused (a powder bed print, a trapped core), each void with its volume (mm³, or mm² in 2D) and where it is
114
+ function made(r, spec, density) {
115
+ const p = r.problem, out = {};
116
+ if (p.mill || p.cast) {
117
+ const c = spec.cast, from = p.mill || (typeof c === 'string' ? c : c.draw), u = undercuts(spec.grid, density, { from, ...(c && typeof c === 'object' ? { parting: c.parting } : {}) });
118
+ out.process = p.mill ? { mill: p.mill } : { cast: typeof c === 'string' ? c : { draw: c.draw, parting: round(c.at, 3) } };
119
+ out.undercuts = u.count;
120
+ }
121
+ if (p.symmetry) out.symmetry = p.symmetry;
122
+ if (p.extrude) out.extrude = p.extrude;
123
+ // How grey it came out: a process or a tie can leave no clear part (a 2D section milled from its side)
124
+ if (p.mill || p.cast || p.symmetry || p.extrude) out.grey = round(grey(density, spec.passive), 3);
125
+ if (r.voids === 'refused') out.voids = voids(spec.grid, density, { origin: spec.origin }).map((v) => ({ volume: round(v.volume, 3), at: v.at.map((x) => round(x, 3)) }));
126
+ return out;
127
+ }
108
128
  const digest = (x) => crypto.createHash('sha256').update(JSON.stringify(x)).digest('hex');
109
129
  /**
110
130
  * The code an optimisation's densities are made by, each piece by name: this file's VERSION, topopt's solver (its file
@@ -126,7 +146,7 @@ export async function optimised(project, ref, { onStep, onRun, fresh = false } =
126
146
  // Two keeps: the densities, by the problem and the code that decides them (the slow part), and the shape drawn from
127
147
  // them, by everything: a change to how shapes are drawn redraws them without optimising again
128
148
  // The shape's key holds this file too, so a change to how cad draws a result (anchored below) redraws it
129
- 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`);
149
+ const hash = digest([VERSION, r.problem, r.outline, r.voids, topopt, source('@rulecad/fea'), fs.readFileSync(new URL(import.meta.url), 'utf8')]), file = paths.cache('optimise', `${hash}.json`);
130
150
  if (!fresh && !process.env.CAD_NO_CACHE && host.exists(file)) return { ...JSON.parse(host.readText(file)), cached: true };
131
151
  const spec = holdEmpty(discretise(r.problem)), densities = paths.cache('densities', `${densitiesKey(r.problem)}.json`);
132
152
  // A mark that this problem has been optimised before (by any solver), so a run can say whether it's the solver that changed
@@ -206,6 +226,7 @@ export async function optimised(project, ref, { onStep, onRun, fresh = false } =
206
226
  const result = {
207
227
  problem: ref, title: r.title, hash, dim: r.dim, material: r.material.name, ...(r.dim === 2 ? { thickness: r.problem.thickness } : {}),
208
228
  grid: spec.grid, origin: spec.origin, regions: { keep: named(spec.regions.keep), avoid: named(spec.regions.avoid) }, ...shape,
229
+ ...made(r, spec, o.density),
209
230
  volume: o.volume, compliance: o.compliance, cases: o.cases, ...(o.unsupported ? { unsupported: o.unsupported } : {}), ...(o.mechanism ? { mechanism: o.mechanism } : {}), ...(o.stress ? { stress: o.stress } : {}), overhang: r.problem.overhang || null,
210
231
  steps: o.steps, converged: o.converged, seconds: o.seconds, history: o.history, density: Array.from(o.density, (d) => round(d, 3)),
211
232
  supports: named(r.problem.supports), loads: named(r.problem.loads),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rulecad/cad",
3
- "version": "0.7.70",
3
+ "version": "0.7.75",
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": {
@@ -65,11 +65,11 @@
65
65
  "@rulecad/model-link": "^0.3.2",
66
66
  "@rulecad/versions": "^0.1.6",
67
67
  "@rulecad/shapes": "^0.4.6",
68
- "@rulecad/rules": "^0.3.20",
68
+ "@rulecad/rules": "^0.3.22",
69
69
  "@jscad/modeling": "^2.12.0",
70
70
  "three": "^0.186.1",
71
71
  "@rulecad/fea": "^0.2.11",
72
- "@rulecad/topopt": "^0.3.0",
72
+ "@rulecad/topopt": "^0.3.4",
73
73
  "@rulecad/flexures": "^0.5.4",
74
74
  "@rulecad/gears": "^0.3.4",
75
75
  "@rulecad/motors": "^0.1.6",
@@ -155,6 +155,44 @@
155
155
  "type": "string",
156
156
  "description": "3D: printable this way up ('+z', '-y', …), or a variable holding it, or 'none': only material supported from below at 45° or less"
157
157
  },
158
+ "symmetry": {
159
+ "type": "object",
160
+ "additionalProperties": false,
161
+ "description": "The same either side of the grid's middle planes, and each half or quarter turn about its middle. The grid is centred on the space and keep regions (in 3D, except along an overhang's build direction). Keep and avoid regions stay as given. A mirror or turn that would swap the side a part is milled from, the way a mould is drawn off or the build direction for its opposite (or a quarter turn that moves it onto another axis) is refused by name; a mould in two halves parted at the grid's middle can be mirrored or half turned across its draw.",
162
+ "properties": {
163
+ "mirror": { "type": "string", "pattern": "^[xyz]{1,3}$", "description": "The axes mirrored across: 'x', 'xy', 'xyz' (2D: x and y)" },
164
+ "turns": { "enum": [2, 4], "description": "The same each half turn (2) or quarter turn (4) about the middle; a quarter turn needs the grid as wide both ways across its axis" },
165
+ "axis": { "enum": ["x", "y", "z"], "description": "3D: the axis turns are about (default z)" }
166
+ }
167
+ },
168
+ "extrude": {
169
+ "enum": ["x", "y", "z"],
170
+ "description": "One section all along this axis: a part cut from a bar, an extrusion or a plate of the profile"
171
+ },
172
+ "mill": {
173
+ "type": "string",
174
+ "pattern": "^[+-][xyz]$",
175
+ "description": "Milled from this side ('+z': the tool cuts down from above): no undercuts, so a cell is as solid as every cell between it and the tool. Not with overhang or cast. Rules: mill-3axis"
176
+ },
177
+ "cast": {
178
+ "description": "Cast or moulded: in a mould drawn off one way ('+z'), or in two halves drawn off both ways along an axis from a parting plane. No undercuts from the draw; draft is left to the drawing. Not with overhang or mill. Rules: mould-injection",
179
+ "oneOf": [
180
+ { "type": "string", "pattern": "^[+-][xyz]$" },
181
+ {
182
+ "type": "object",
183
+ "additionalProperties": false,
184
+ "required": ["draw"],
185
+ "properties": {
186
+ "draw": { "enum": ["x", "y", "z"], "description": "The axis the halves are drawn off along" },
187
+ "parting": { "$ref": "#/$defs/expr", "description": "The parting plane, mm along draw (default the space's middle)" }
188
+ }
189
+ }
190
+ ]
191
+ },
192
+ "voids": {
193
+ "enum": ["allowed", "refused"],
194
+ "description": "Enclosed voids in the result: allowed (the default), or refused, for metal powder bed printing (powder left inside can't be got out) or a casting's trapped core: cad optimise then reports each as an error with its volume and where it is. Rules: dmls"
195
+ },
158
196
  "output": {
159
197
  "type": "object",
160
198
  "additionalProperties": false,