@rulecad/cad 0.7.6 → 0.7.7

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/README.md CHANGED
@@ -433,7 +433,9 @@ the problem, and optionally a limit:
433
433
  `cad check` then reports, for each load case, how far the part moves its load, its stiffness (N/mm) and its peak
434
434
  stress against the material's strength, and the part's volume and weight. With `max_deflection` a case that moves
435
435
  further fails; with `volume` a part whose volume is further than `tolerance` (a share) from it fails. A picture goes in
436
- `.preview/`.
436
+ `.preview/`. A twist (forces that add to nothing, such as four round a rim) has no direction to move along, so it's
437
+ reported as the angle the part turns through, in degrees, under its moment (N·m), and its torsional stiffness (N·m a
438
+ degree).
437
439
 
438
440
  On the problem's own cells the analysis is quick and coarse. A solid part's cubes are filled by the share of each the
439
441
  part takes up, so a wall between cell centres still counts, but thin walls on big cubes still come out too stiff when
package/bin/cad.mjs CHANGED
@@ -112,6 +112,9 @@ function report(findings, about = {}) {
112
112
  const say = (...a) => { if (!opt('json')) console.log(...a); }; // text output that --json leaves out
113
113
 
114
114
  const sig = (n) => (Number.isFinite(n) ? String(Number(n.toPrecision(3))) : String(n)); // three significant figures
115
+ // A case's stiffness, and in words: a twist turns (degrees under N·m, N·m a degree), anything else moves along its load
116
+ const stiffOf = (c) => (c.moment ? c.torsion : c.stiffness);
117
+ const howFar = (c) => (c.moment ? `turns ${sig(c.angle * 180 / Math.PI)}° under ${sig(c.moment / 1000)} N·m, ${sig(c.torsion / 1000 * Math.PI / 180)} N·m a degree` : `moves ${sig(c.deflection)} mm along its load, ${sig(c.stiffness)} N/mm`);
115
118
  // cad optimise: run a problem (with a progress line), then report it and draw it: a 2D layout as an SVG of its
116
119
  // densities, a 3D one as the preview's four views of its surface
117
120
  async function optimiseProblem(ref) {
@@ -136,7 +139,7 @@ async function optimiseProblem(ref) {
136
139
  // The page keeps showing the end for ten minutes (the process waits for it), so a finished run isn't a dead link
137
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)`); }
138
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}`;
139
- const lines = [`${p.material.name}${r.dim === 2 ? `, ${fmt(r.thickness)} mm thick` : ''}: ${r.cases.map((c) => `${c.name} ${sig(c.deflection)} mm (${sig(c.stiffness)} N/mm)`).join(', ')}`,
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(', ')}`,
140
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³)` : ''}`];
141
144
  let out;
142
145
  if (r.dim === 3) {
@@ -150,7 +153,7 @@ async function optimiseProblem(ref) {
150
153
  }
151
154
  if (opt('json')) return console.log(JSON.stringify((({ density, history, mesh, ...x }) => x)(r), null, 2));
152
155
  console.log(`${r.title}${r.cached ? ' (cached)' : ` (${fmt(r.seconds)} s)`}`);
153
- for (const c of r.cases) console.log(` ${c.name}: moves ${sig(c.deflection)} mm along its load, ${sig(c.stiffness)} N/mm`);
156
+ for (const c of r.cases) console.log(` ${c.name}: ${howFar(c)}`);
154
157
  if (r.stress) console.log(` peak stress ${sig(r.stress.peak)} MPa, the limit ${sig(r.stress.limit)} MPa${r.stress.peak > r.stress.limit * 1.02 ? ': over it (raise the volume, ease the limit, or use a smaller cell)' : ''}`);
155
158
  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}` : ''}`);
156
159
  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)`);
@@ -175,7 +178,7 @@ function place(triangles, [rx, ry, rz], move) {
175
178
  function reportText({ turn, object, title, version, a, r, m, strength, weaker, grid, limit, minSafety, records, found }) {
176
179
  const at = (p) => `(${p.map(fmt).join(', ')})`;
177
180
  const pct = (x) => `${x >= 0 ? '+' : ''}${fmt(x * 100)}%`;
178
- const rows = r.cases.map((c) => `| ${c.name} | ${fmt(c.force)} | ${sig(c.deflection)} | ${sig(c.stiffness)}${c.change ? ` (${pct(c.change.stiffness)})` : ''} | ${sig(c.stress)}${c.change ? ` (${pct(c.change.stress)})` : ''} at ${at(c.stressAt)} | ${strength ? `${sig(strength / c.stress)}×` : '–'}${m.fatigue ? ` (fatigue ${sig(m.fatigue / c.stress)}×)` : ''} | ${sig(c.movement)} at ${at(c.movementAt)} |`);
181
+ const rows = r.cases.map((c) => `| ${c.name} | ${c.moment ? `${sig(c.moment / 1000)} N·m | ${sig(c.angle * 180 / Math.PI)}° | ${sig(c.torsion / 1000 * Math.PI / 180)} N·m/°` : `${fmt(c.force)} | ${sig(c.deflection)} | ${sig(c.stiffness)}`}${c.change ? ` (${pct(c.change.stiffness)})` : ''} | ${sig(c.stress)}${c.change ? ` (${pct(c.change.stress)})` : ''} at ${at(c.stressAt)} | ${strength ? `${sig(strength / c.stress)}×` : '–'}${m.fatigue ? ` (fatigue ${sig(m.fatigue / c.stress)}×)` : ''} | ${sig(c.movement)} at ${at(c.movementAt)} |`);
179
182
  return [
180
183
  `# ${title}: analysed under ${a.problem}`, '',
181
184
  `${object} ${version || ''}. Analysed on ${grid}, ${m.name} (modulus ${fmt(m.modulus)} MPa${strength ? `, strength ${fmt(strength)} MPa${weaker ? ' across its layers' : ''}` : ''}${m.fatigue ? `, fatigue strength ${fmt(m.fatigue)} MPa` : ''}). Positions are in mm, in the problem's frame${turn ? ' (the part turned back from how it prints)' : ''}.${r.cases[0]?.change ? ` In brackets: the change from ${fmt(r.cases[0].change.cell)} mm cells. Stiffness that changes by 5% or less has settled; peak stress at a sharp inside corner keeps rising as cells shrink, so read it with its change.` : ' On the problem\'s own cells: a quick check. Give the analysis a smaller cell to verify it.'}`, '',
@@ -209,7 +212,7 @@ async function analyses(b, d, tag) {
209
212
  // theory it's endless there), so it's reported, not judged.
210
213
  if (cell) {
211
214
  const coarser = await analysePart(turned, d.project, a.problem, { cell: cell * 1.25 });
212
- r.cases.forEach((c, n) => { const o = coarser.cases[n]; c.change = { stiffness: c.stiffness / o.stiffness - 1, stress: c.stress / o.stress - 1, cell: coarser.grid.h }; });
215
+ r.cases.forEach((c, n) => { const o = coarser.cases[n]; c.change = { stiffness: stiffOf(c) / stiffOf(o) - 1, stress: c.stress / o.stress - 1, cell: coarser.grid.h }; });
213
216
  }
214
217
  const limit = a.max_deflection != null ? evaluate(a.max_deflection, lookup) : null;
215
218
  const minSafety = a.min_safety != null ? evaluate(a.min_safety, lookup) : null;
@@ -226,15 +229,15 @@ async function analyses(b, d, tag) {
226
229
  const at = (p) => `(${p.map(fmt).join(', ')})`;
227
230
  const found = r.cases.flatMap((c) => {
228
231
  const safety = strength ? strength / c.stress : null, fatigue = m.fatigue ? m.fatigue / c.stress : null;
229
- const text = `${a.problem} ${c.name}: moves ${sig(c.deflection)} mm under ${fmt(c.force)} N, ${sig(c.stiffness)} N/mm`;
230
- const values = { deflection: c.deflection, force: c.force, stiffness: c.stiffness, stress: c.stress, ...(limit != null ? { limit } : {}) };
232
+ const text = c.moment ? `${a.problem} ${c.name}: ${howFar(c)}` : `${a.problem} ${c.name}: moves ${sig(c.deflection)} mm under ${fmt(c.force)} N, ${sig(c.stiffness)} N/mm`;
233
+ const values = { ...(c.moment ? { angle: c.angle, moment: c.moment, torsion: c.torsion } : { deflection: c.deflection, force: c.force, stiffness: c.stiffness }), stress: c.stress, ...(limit != null ? { limit } : {}) };
231
234
  const refs = { problem: a.problem, part: part.name, ...(out ? { picture: path.relative(WS, out) } : {}) };
232
235
  const strong = `peak stress ${sig(c.stress)} MPa at ${at(c.stressAt)}${safety != null ? `: ${sig(safety)}× under ${m.name}'s ${weaker ? 'strength across its layers' : 'strength'}${fatigue != null ? `, ${sig(fatigue)}× under its fatigue strength` : ''}` : ''}`;
233
236
  const least = Math.min(safety ?? Infinity, fatigue ?? Infinity);
234
237
  const settled = c.change ? [{ level: Math.abs(c.change.stiffness) <= 0.05 ? 'pass' : 'warn', code: 'fea.settled', values: c.change,
235
238
  text: `${a.problem} ${c.name}: stiffness ${c.change.stiffness >= 0 ? '+' : ''}${fmt(c.change.stiffness * 100)}% from ${fmt(c.change.cell)} mm cells${Math.abs(c.change.stiffness) <= 0.05 ? ', settled (within 5%)' : ': not settled, use a smaller cell'}` }] : [];
236
239
  return [
237
- { level: limit == null ? 'info' : c.deflection <= limit ? 'pass' : 'fail', text: limit == null ? text : `${text} (at most ${fmt(limit)})`, code: 'fea.deflection', values, refs }, ...settled,
240
+ { level: limit == null || c.moment ? 'info' : c.deflection <= limit ? 'pass' : 'fail', text: limit == null ? text : `${text} (at most ${fmt(limit)})`, code: 'fea.deflection', values, refs }, ...settled,
238
241
  { level: minSafety == null ? 'info' : least >= minSafety ? 'pass' : 'fail', text: `${a.problem} ${c.name}: ${strong}${minSafety != null ? ` (at least ${fmt(minSafety)}×)` : ''}`, code: 'fea.strength',
239
242
  values: { stress: c.stress, at: c.stressAt, ...(safety != null ? { safety } : {}), ...(fatigue != null ? { fatigue } : {}), ...(minSafety != null ? { min: minSafety } : {}) }, refs },
240
243
  ];
package/lib/optimise.mjs CHANGED
@@ -11,7 +11,7 @@ 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 } from '@rulecad/topopt';
14
+ import { discretise, optimise, outlines, surface, area, bounds, resultant } from '@rulecad/topopt';
15
15
  import { grid as makeGrid, analyse, positions } from '@rulecad/fea';
16
16
 
17
17
  const VERSION = 2, source = (m) => fs.readFileSync(new URL(import.meta.resolve(m)), 'utf8'); // the solver's code: a change remakes results
@@ -177,8 +177,8 @@ export async function analysePart(triangles, project, ref, { cell } = {}) {
177
177
  });
178
178
  const a = analyse({ grid: g, density, material: problem.material, thickness: problem.thickness, fixed, loads });
179
179
  const cases = names.map((name, c) => {
180
- const size = Math.hypot(...problem.loads.filter((l) => l.case === name).reduce((s, l) => [s[0] + l.force[0], s[1] + l.force[1]], [0, 0]));
181
- return measures(name, size, a, c, density, { nx, ny, nz: 1, h }, [x0, y0]);
180
+ const { size, moment } = resultant(problem.loads.filter((l) => l.case === name));
181
+ return measures(name, size, moment, a, c, density, { nx, ny, nz: 1, h }, [x0, y0]);
182
182
  });
183
183
  return { problem: ref, dim: 2, material: r.material, critical: r.critical, cases, volume: volume(triangles), grid: { nx, ny, h }, origin: [x0, y0], density };
184
184
  }
@@ -190,20 +190,21 @@ function analyseSolid(triangles, r, cell) {
190
190
  const g = makeGrid(spec.grid), density = solidFill(triangles, spec.grid, spec.origin);
191
191
  if (!density.some((d) => d)) throw new Error('the part is smaller than one cell of the problem: use a smaller cell');
192
192
  const a = analyse({ grid: g, density, material: problem.material, fixed: spec.fixed, loads: spec.loads.map((l) => l.force) });
193
- const cases = spec.loads.map((l, c) => measures(l.name, l.size, a, c, density, spec.grid, spec.origin));
193
+ const cases = spec.loads.map((l, c) => measures(l.name, l.size, l.moment, a, c, density, spec.grid, spec.origin));
194
194
  return { problem: r.ref, dim: 3, material: r.material, critical: r.critical, cases, volume: volume(triangles), grid: spec.grid, origin: spec.origin, density };
195
195
  }
196
196
 
197
- // A load case's numbers: how far the load moves along itself (compliance over force) and the stiffness, the largest
197
+ // A load case's numbers: how far the load moves along itself (compliance over force) and the stiffness (a twist: the
198
+ // angle it turns, compliance over its moment, and the torsion, N·mm a radian), the largest
198
199
  // stress in the part and the centre of the cell it's in, and the largest movement of any point of the part and where
199
- function measures(name, force, a, c, density, { nx, ny, h }, origin) {
200
- const deflection = force ? a.compliance[c] / force : 0;
200
+ function measures(name, force, moment, a, c, density, { nx, ny, h }, origin) {
201
+ const deflection = force ? a.compliance[c] / force : 0, angle = moment ? a.compliance[c] / moment : 0;
201
202
  let most = 0, cellAt = 0;
202
203
  for (let e = 0; e < density.length; e++) if (density[e] >= 0.5 && a.stress[c][e] > most) { most = a.stress[c][e]; cellAt = e; }
203
204
  const index = [cellAt % nx, Math.floor(cellAt / nx) % ny, Math.floor(cellAt / (nx * ny))];
204
205
  const stressAt = origin.map((o, q) => round(o + (index[q] + 0.5) * h, 2)), d = a.deflection[c];
205
206
  return {
206
- name, force, deflection, stiffness: deflection ? force / deflection : Infinity, stress: most, stressAt,
207
+ name, ...(moment ? { moment, angle, torsion: moment / angle } : { force, deflection, stiffness: deflection ? force / deflection : Infinity }), stress: most, stressAt,
207
208
  movement: d.most, movementAt: d.at.map((x, q) => round(x + origin[q], 2)),
208
209
  };
209
210
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rulecad/cad",
3
- "version": "0.7.6",
3
+ "version": "0.7.7",
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": {