Files
supergeometry.app/tests/variablesflange.test.ts
superGeometry 6e30ce44e0 Variables, gone through while building a pipe flange that every size of is a variable: what did not work, mended (asked for: "go into variables and do the same thing while building something complex that makes sense to have variables").
The flange, built by hand in the app: ten variables (outer size, bore, thickness, hub wall, the hub's size worked out from bore and wall, hub height, how many bolt holes, their size, the bolt circle worked out from the others, the chamfer); a disc, a hub on its top face, a bore through both, a bolt hole placed by a variable, patterned by a variable and cut, a chamfer. Its volume is the formula's to the hundredth of a cubic millimetre, and stays so through every change of every variable.
What that found:
- A variable's name typed into a size on the line was made the number it came to, and the link never kept. And a size that followed a variable, opened and closed again, was cut loose from it. (The line is the only way to type a size since it became the only UI.) Now the formula is kept; a size that follows one says so ("od / 2 = 60 mm"), its box opens on the formula, closed unchanged it does nothing, and it is not dragged sideways (a finger sliding off it would cut it loose). The box has ƒx: one of the project's variables, or a new one from what is typed. What can't be read is said, with the name no variable has.
- The project's unit changed, every plain number in a formula was read in the new one: a flange 160 mm across was 160 inches. Nothing changes size now: each formula that would is given the unit its plain lengths were typed in ("160 mm", "od / 2 + 5 mm"); a count, a factor and an angle are left as they are. And the unit is the project's own before its formulas are read (it was set after, so the model was a step behind).
- A chain of variables stopped at eight deep: the ninth "couldn't be evaluated". Any depth now; one that uses itself, round any number of others, is refused. Each is worked out once (a variable used twice by each of thirty built on one another was a thousand million readings).
- More to write formulas with: floor, ceil, round (how many fit: floor(length / pitch)), min and max of several, asin, acos, atan.
- New Variable: a value refused closed the window with what was typed gone - it stays to put right. A name that differs from another only in its capitals is refused. A name no variable has is named. An angle (30 deg) is taken, as it was in the list. One made again by the same name says it changed. Deleting: capitals count (W gone is not w gone); another variable that still uses it is named; a point placed by it keeps its place.
- A variable with no place of its own had no tag in the view: a point placed by one, the count of a ring of holes cut into the body, one that only other variables are made from. All ten of the flange's have one.
Not done, found on the way: a pattern started from a hole's face copies the whole body (holes by the count of a variable are a pin, patterned, then cut - as here); a sketch pattern's count is a plain number.
In the app: the ten variables made in the window one after another; names refused (sin, max, m, 2x), a value that uses itself, an unknown name, a formula cut short, a number too big; the flange built step by step with each size typed as its variable; nine changes of variables in the list of them all, each volume the formula's; 0, 1 and 2.6 holes, a bore bigger than the flange, a thickness below zero, each put back; undo and redo; inches, centimetres and back; a size changed from its tag in the view; a pattern's count edited to n + 2; the typing box at phone width.
Tests: variablesflange 22 (the hand-built project: its variables changed alone, together, to formulas, to awkward values and back, and through three units); unitchange 22; units 55 (27 new); vartags 20; the rest as before.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
2026-11-21 10:52:17 +02:00

80 lines
5.3 KiB
TypeScript

// Run: npx tsx tests/variablesflange.test.ts
// A pipe flange built by hand in the app, every size of it a variable (outer size, bore, thickness, hub, bolt
// holes and how many, the chamfer). Each variable changed - alone, several at once, to awkward values and back - and
// the whole of it has to follow: its volume is what the formula gives, to the hundredth of a cubic millimetre.
import { readFileSync } from "node:fs";
import { after, bodies, check, done, health, near, volume } from "./kit";
import { resolveDoc } from "../src/doc/params";
import { keepSizes } from "../src/doc/unitChange";
import type { Feature, ProjectDoc, Units } from "../src/doc/types";
import type { Shape3D } from "replicad";
const fresh = () => JSON.parse(readFileSync(new URL("./fixtures/flange-variables.json", import.meta.url), "utf8")) as ProjectDoc;
type V = { od: number; bore: number; thick: number; wall: number; hubH: number; n: number; boltD: number; cham: number };
/** The flange's volume: the disc, the hub, the bore through both, n bolt holes, the chamfer round the top. */
const formula = (v: V) => {
const hubD = v.bore + 2 * v.wall;
return (Math.PI / 4) * ((v.od ** 2 - v.bore ** 2) * v.thick + (hubD ** 2 - v.bore ** 2) * v.hubH - v.n * v.boltD ** 2 * v.thick) - Math.PI * v.cham ** 2 * (v.od / 2 - v.cham / 3);
};
let doc = fresh();
const now: V = { od: 160, bore: 40, thick: 20, wall: 10, hubH: 45, n: 6, boltD: 13, cham: 3 };
const put = (name: string, expr: string) => (doc.variables.find((v) => v.name === name)!.expr = expr);
async function built(): Promise<{ vol: number; sound: boolean; count: number; errors: string[]; problems: string[] }> {
const problems = resolveDoc(doc);
const { errors } = await after(doc.features as Feature[]);
const all = [...bodies().values()] as Shape3D[];
return { vol: all.reduce((s, b) => s + volume(b), 0), sound: all.every((b) => health(b).healthy), count: all.length, errors, problems };
}
async function expect(name: string, change: Partial<Record<keyof V, string>>, values: Partial<V>): Promise<void> {
for (const [k, e] of Object.entries(change)) put(k, e as string);
Object.assign(now, values);
const t = performance.now();
const r = await built();
const want = formula(now);
check(name, near(r.vol, want, 0.02) && r.sound && r.count === 1 && !r.errors.length && !r.problems.length, `${r.vol.toFixed(2)} mm³ (the formula: ${want.toFixed(2)}), ${Math.round(performance.now() - t)} ms${r.errors.length ? " " + r.errors.join("; ") : ""}${r.problems.length ? " " + r.problems.join("; ") : ""}`);
}
await expect("as it was built", {}, {});
await expect("eight bolt holes", { n: "8" }, { n: 8 });
await expect("three", { n: "3" }, { n: 3 });
await expect("twelve", { n: "12" }, { n: 12 });
await expect("a bigger flange: the bolt circle goes out with it", { od: "220 mm" }, { od: 220 });
await expect("thicker: the hub rides up, the bore and the holes still go through", { thick: "32 mm" }, { thick: 32 });
await expect("a smaller bore: the hub follows it", { bore: "25 mm" }, { bore: 25 });
await expect("a thicker hub wall", { wall: "18 mm" }, { wall: 18 });
await expect("a short hub, small bolts, a small chamfer", { hubH: "6 mm", boltD: "6.5 mm", cham: "0.5 mm" }, { hubH: 6, boltD: 6.5, cham: 0.5 });
await expect("everything at once", { od: "300 mm", bore: "110 mm", thick: "24 mm", wall: "14 mm", hubH: "60 mm", n: "16", boltD: "18 mm", cham: "4 mm" }, { od: 300, bore: 110, thick: 24, wall: 14, hubH: 60, n: 16, boltD: 18, cham: 4 });
// A value given as a formula of the others, and in another unit.
await expect("the thickness a quarter of the bore; the bolts half an inch", { thick: "bore / 4 - 2 mm", boltD: "0.5 in" }, { thick: 110 / 4 - 2, boltD: 12.7 });
await expect("the count worked out: as many as fit 45 apart round the bolt circle", { n: "floor(pi * pcd / 45 mm)" }, { n: Math.floor((Math.PI * ((300 + 110 + 28) / 2)) / 45) });
// Awkward values, then back: nothing is left broken.
put("n", "0");
let r = await built();
check("no holes at all (0): it says so at the pattern, and nothing else", r.errors.length === 1 && /count/i.test(r.errors[0]), r.errors);
put("bore", "400 mm");
put("n", "6");
r = await built();
check("a bore bigger than the flange: still a sound body, no crash", r.count >= 1 && r.sound, `${r.count} bodies, ${r.errors.join("; ")}`);
await expect("and back: as the formula says again", { bore: "110 mm", n: "16", thick: "24 mm", boltD: "18 mm" }, { bore: 110, n: 16, thick: 24, boltD: 18 });
// The project's unit changed: nothing changes size, in any unit and back.
doc = fresh();
Object.assign(now, { od: 160, bore: 40, thick: 20, wall: 10, hubH: 45, n: 6, boltD: 13, cham: 3 });
for (const v of doc.variables) v.expr = v.expr.replace(/^(\d+(?:\.\d+)?) mm$/, "$1");
put("wall", "10 mm");
let unit: Units = "mm";
for (const to of ["in", "cm", "mm"] as Units[]) {
const k = keepSizes(doc, unit, to);
doc.units = unit = to;
await expect(`the unit changed to ${to}: the same flange`, {}, {});
check(` (no formula lost; n still a plain 6)`, !k.lost && doc.variables.find((v) => v.name === "n")!.expr === "6", doc.variables.map((v) => `${v.name}=${v.expr}`).join(", "));
}
// And a size typed in the new unit is in that unit.
doc.units = "in";
await expect("in inches, a plain 8 typed for the flange is 8 inches", { od: "8" }, { od: 203.2 });
doc.units = "mm";
done();