Release SketchForge 0.5 edge tools

This commit is contained in:
Formsmith
2026-07-05 12:28:23 +03:00
parent f18bacf3ad
commit 5b0ea94e7b
38 changed files with 3973 additions and 519 deletions
+188
View File
@@ -0,0 +1,188 @@
import { describe, expect, it } from "vitest";
import {
bakeCadMetadataForShapeTransform,
cadBrepTransformForShape,
cadModifierPrimitiveForAnalyticBox,
cadModifierPrimitiveForBakedShape,
} from "@/lib/cadBakeMetadata";
import type { WorkplaneShape } from "@/types/sketchforge";
function expectTransformClose(actual: number[] | undefined, expected: number[]) {
expect(actual).toHaveLength(expected.length);
expected.forEach((value, index) => {
expect(actual?.[index]).toBeCloseTo(value, 6);
});
}
function treatedMeshShape(overrides: Partial<WorkplaneShape> = {}): WorkplaneShape {
return {
id: "treated-mesh",
name: "Treated Mesh",
kind: "mesh",
color: "#d41721",
x: 10,
z: 0,
elevation: 5,
size: 2,
width: 2,
depth: 2,
height: 2,
rotation: 0,
rotationX: 0,
rotationZ: 0,
importedMesh: {
positions: [-1, 0, 0, 1, 0, 0, 0, 2, 0],
baseWidth: 2,
baseDepth: 2,
baseHeight: 2,
triangleCount: 1,
sourceFormat: "json",
},
edgeTreatments: [{ kind: "fillet", amount: 0.5, edgeCount: 1 }],
cadDisplayEdges: [{ points: [-1, 0, 0, 1, 0, 0] }],
cadDisplayEdgesVersion: 2,
cadBrep: "stored-brep-before-rotation",
cadBrepFrame: {
x: 10,
z: 0,
elevation: 5,
width: 2,
depth: 2,
height: 2,
},
locked: false,
hidden: false,
...overrides,
};
}
function boxShape(overrides: Partial<WorkplaneShape> = {}): WorkplaneShape {
return {
id: "box",
name: "Box",
kind: "box",
color: "#d41721",
x: 4,
z: -6,
elevation: 2,
size: 20,
width: 20,
depth: 18,
height: 16,
rotation: 32,
rotationX: 18,
rotationZ: 24,
locked: false,
hidden: false,
...overrides,
};
}
describe("SketchForge transform baking", () => {
it("preserves exact BREP and rebases CAD display edges after wheel rotation", () => {
const shape = treatedMeshShape({ rotationZ: 90 });
const baked = bakeCadMetadataForShapeTransform(shape, {
centerX: 10,
minY: 5,
centerZ: 0,
width: 2,
depth: 2,
height: 2,
yawDegrees: 0,
});
const expectedTransform = [0, -1, 0, 16, 1, 0, 0, -4, 0, 0, 1, 0];
expect(baked.cadBrep).toBe("stored-brep-before-rotation");
expect(baked.cadBrepFrame).toMatchObject({
x: 10,
z: 0,
elevation: 5,
width: 2,
depth: 2,
height: 2,
});
expectTransformClose(baked.cadBrepFrame?.sourceTransform, expectedTransform);
expect(baked.cadDisplayEdgesVersion).toBe(2);
expect(baked.cadDisplayEdges?.[0].points).toEqual([1, 0, 0, 1, 2, 0]);
const bakedShape: WorkplaneShape = {
...shape,
...baked,
x: 10,
z: 0,
elevation: 5,
width: 2,
depth: 2,
height: 2,
size: 2,
rotation: 0,
rotationX: 0,
rotationZ: 0,
};
expectTransformClose(cadBrepTransformForShape(bakedShape), expectedTransform);
});
it("preserves an analytic box primitive when wheel rotation bakes it to a mesh", () => {
const shape = boxShape();
const directPrimitive = cadModifierPrimitiveForAnalyticBox(shape);
expect(directPrimitive?.transform).toBeDefined();
const baked = bakeCadMetadataForShapeTransform(shape, {
centerX: 4.5,
minY: -2,
centerZ: -5.5,
width: 27,
depth: 26,
height: 25,
yawDegrees: 32,
});
expect(baked.cadPrimitiveFrame).toMatchObject({
kind: "box",
width: 20,
depth: 18,
height: 16,
frame: {
x: 4.5,
z: -5.5,
elevation: -2,
width: 27,
depth: 26,
height: 25,
},
});
expectTransformClose(baked.cadPrimitiveFrame?.frame.sourceTransform, directPrimitive?.transform ?? []);
const bakedShape: WorkplaneShape = {
...shape,
...baked,
kind: "mesh",
x: 4.5,
z: -5.5,
elevation: -2,
width: 27,
depth: 26,
height: 25,
size: 27,
rotation: 0,
rotationX: 0,
rotationZ: 0,
importedMesh: {
positions: [-1, 0, 0, 1, 0, 0, 0, 1, 0],
baseWidth: 27,
baseDepth: 26,
baseHeight: 25,
triangleCount: 1,
sourceFormat: "json",
},
};
const restoredPrimitive = cadModifierPrimitiveForBakedShape(bakedShape);
expect(restoredPrimitive).toMatchObject({
kind: "box",
width: 20,
depth: 18,
height: 16,
});
expectTransformClose(restoredPrimitive?.transform, directPrimitive?.transform ?? []);
});
});
+25 -1
View File
@@ -1,5 +1,6 @@
import { describe, expect, it } from "vitest";
import type { WorkplaneWorkspaceSettings } from "@/types/sketchforge";
import { formatMeasurementNumber, lengthDisplayUnit, millimetersToDisplay, normalizeScaleForUnits, scaleOptionsForUnits } from "@/lib/measurementUnits";
import { DEFAULT_SNAP_GRID, DEFAULT_WORKPLANE_WORKSPACE, normalizeSnapGrid, normalizeWorkspaceSettings, workplaneSettingsFingerprint } from "@/lib/workplaneSettings";
describe("workplane settings helpers", () => {
@@ -47,13 +48,36 @@ describe("workplane settings helpers", () => {
showGrid: false,
cruiseShapes: false,
units: "Bricks",
scale: "1:10 (centimeters)",
scale: "1:1 (studs)",
accuracy: 3,
});
expect(normalizeWorkspaceSettings({ accuracy: 9 }, fallback).accuracy).toBe(fallback.accuracy);
});
it("keeps scale options in the selected unit family", () => {
expect(scaleOptionsForUnits("Metric (Default)")).toEqual(["1:1 (millimeters)", "1:10 (centimeters)", "1:1000 (meters)"]);
expect(normalizeScaleForUnits("Metric (Default)", "1:100 (meters)")).toBe("1:1000 (meters)");
expect(scaleOptionsForUnits("Imperial")).toEqual(["1:1 (inches)", "1:1 (feet)"]);
expect(normalizeScaleForUnits("Imperial", "1:100 (meters)")).toBe("1:1 (inches)");
expect(normalizeWorkspaceSettings({ units: "Imperial", scale: "1:100 (meters)" }).scale).toBe("1:1 (inches)");
});
it("changes display units without scaling model values", () => {
expect(lengthDisplayUnit({ units: "Metric (Default)", scale: "1:1000 (meters)" }).label).toBe("m");
expect(millimetersToDisplay(20, { units: "Metric (Default)", scale: "1:1 (millimeters)" })).toBe(20);
expect(millimetersToDisplay(20, { units: "Metric (Default)", scale: "1:1000 (meters)" })).toBe(0.02);
expect(millimetersToDisplay(20, { units: "Metric (Default)", scale: "1:100 (meters)" })).toBe(0.02);
expect(Number(millimetersToDisplay(25.4, { units: "Imperial", scale: "1:1 (inches)" }).toFixed(4))).toBe(1);
expect(Number(millimetersToDisplay(304.8, { units: "Imperial", scale: "1:1 (feet)" }).toFixed(4))).toBe(1);
});
it("uses accuracy as display precision", () => {
expect(formatMeasurementNumber(20, 1, 0.01)).toBe("20.0");
expect(formatMeasurementNumber(20, 3, 0.01)).toBe("20.000");
expect(formatMeasurementNumber(0.0004, 1, 0.001)).toBe("0.0004");
});
it("fingerprints workspace and snap settings together", () => {
const base = workplaneSettingsFingerprint(DEFAULT_WORKPLANE_WORKSPACE, "1.0 mm");
const changedSnap = workplaneSettingsFingerprint(DEFAULT_WORKPLANE_WORKSPACE, "5.0 mm");