release: publish v0.9.0 modeling update

This commit is contained in:
Formsmith
2026-08-02 17:38:53 +03:00
parent ab2f34aa80
commit c5f34422e4
28 changed files with 3099 additions and 402 deletions
+14 -1
View File
@@ -2,6 +2,7 @@ import type { CadModifierEdge } from "@/lib/cadModifierTypes";
export const CAD_MODIFIER_RUNTIME_BASE = "/occt";
export const CAD_MODIFIER_REQUEST_TIMEOUT_MS = 30_000;
export const CAD_MODIFIER_MAX_PREPARE_TIMEOUT_MS = 180_000;
export const CAD_MODIFIER_MAX_SHARP_ANGLE = 90;
export type CadModifierRequestPhase = "prepare" | "preview";
@@ -62,11 +63,23 @@ export function edgeModifierSelectionStatus(prepared: boolean, selectedCount: nu
return prepared ? `${selectedCount} of ${availableCount} sharp edges selected` : "Preparing edges\u2026";
}
export function cadModifierPrepareTimeoutMs(meshTriangleCount: number) {
if (!Number.isFinite(meshTriangleCount) || meshTriangleCount <= 0) {
return CAD_MODIFIER_REQUEST_TIMEOUT_MS;
}
const normalizedTriangleCount = Math.max(0, Math.floor(meshTriangleCount));
const meshPreparationBudget = 45_000 + normalizedTriangleCount * 0.75;
return Math.min(
CAD_MODIFIER_MAX_PREPARE_TIMEOUT_MS,
Math.max(60_000, Math.ceil(meshPreparationBudget)),
);
}
export function cadModifierTimeoutMessage(phase: CadModifierRequestPhase) {
if (phase === "preview") {
return "The edge preview timed out. Cancel the tool and try again.";
}
return "Edge preparation timed out. Update to Firefox 121+, Chrome/Brave 114+, or Safari 17.2+, then try again.";
return "Edge preparation timed out. This mesh needs more CAD processing than the interactive limit allows. Try a repaired or lower-detail STL.";
}
export function cadModifierWorkerFailureMessage() {
+182
View File
@@ -0,0 +1,182 @@
export type MoveDimensionAxis = "x" | "z";
export type MoveDimensionScreenPoint = {
x: number;
y: number;
};
export type MoveDimensionLine = {
axis: MoveDimensionAxis;
value: number;
label: string;
x1: number;
y1: number;
x2: number;
y2: number;
labelX: number;
labelY: number;
};
export type MoveDimensionGuide = {
axis: MoveDimensionAxis;
x1: number;
y1: number;
x2: number;
y2: number;
};
export type MoveDimensionOverlayData = {
width: number;
height: number;
lines: MoveDimensionLine[];
guides: MoveDimensionGuide[];
};
type MoveDimensionWorldPoint = {
x: number;
y: number;
z: number;
};
type CreateMoveDimensionOverlayOptions = {
originX: number;
originZ: number;
planeY: number;
deltaX: number;
deltaZ: number;
accuracy: number;
width: number;
height: number;
project: (point: MoveDimensionWorldPoint) => MoveDimensionScreenPoint;
};
function clamp(value: number, min: number, max: number) {
return Math.min(max, Math.max(min, value));
}
export function formatMoveDimension(value: number, accuracy: number) {
const zeroThreshold = 0.5 * 10 ** -accuracy;
const normalized = Math.abs(value) < zeroThreshold ? 0 : value;
return normalized.toFixed(accuracy);
}
function dimensionLabelPosition(
origin: MoveDimensionScreenPoint,
endpoint: MoveDimensionScreenPoint,
otherEndpoint: MoveDimensionScreenPoint,
hasOtherDimension: boolean,
width: number,
height: number,
) {
const midpoint = {
x: (origin.x + endpoint.x) / 2,
y: (origin.y + endpoint.y) / 2,
};
const dx = endpoint.x - origin.x;
const dy = endpoint.y - origin.y;
const length = Math.max(1, Math.hypot(dx, dy));
const normal = { x: -dy / length, y: dx / length };
const towardOther = (otherEndpoint.x - midpoint.x) * normal.x + (otherEndpoint.y - midpoint.y) * normal.y;
const side = hasOtherDimension && Math.abs(towardOther) > 0.5
? towardOther >= 0 ? -1 : 1
: 1;
const offset = 30;
return {
x: clamp(midpoint.x + normal.x * offset * side, 48, Math.max(48, width - 48)),
y: clamp(midpoint.y + normal.y * offset * side, 24, Math.max(24, height - 24)),
};
}
function dimensionLineStart(origin: MoveDimensionScreenPoint, endpoint: MoveDimensionScreenPoint) {
const dx = endpoint.x - origin.x;
const dy = endpoint.y - origin.y;
const length = Math.max(1, Math.hypot(dx, dy));
const overrun = 12;
return {
x: origin.x - (dx / length) * overrun,
y: origin.y - (dy / length) * overrun,
};
}
export function createMoveDimensionOverlay({
originX,
originZ,
planeY,
deltaX,
deltaZ,
accuracy,
width,
height,
project,
}: CreateMoveDimensionOverlayOptions): MoveDimensionOverlayData | null {
const zeroThreshold = 0.5 * 10 ** -accuracy;
if (Math.abs(deltaX) < zeroThreshold && Math.abs(deltaZ) < zeroThreshold) {
return null;
}
const safeScreenPoint = (point: MoveDimensionScreenPoint) => ({
x: Number.isFinite(point.x) ? point.x : width / 2,
y: Number.isFinite(point.y) ? point.y : height / 2,
});
const origin = safeScreenPoint(project({ x: originX, y: planeY, z: originZ }));
const xEndpoint = safeScreenPoint(project({ x: originX + deltaX, y: planeY, z: originZ }));
const zEndpoint = safeScreenPoint(project({ x: originX, y: planeY, z: originZ + deltaZ }));
const currentAnchor = safeScreenPoint(project({ x: originX + deltaX, y: planeY, z: originZ + deltaZ }));
const lines: MoveDimensionLine[] = [];
const guides: MoveDimensionGuide[] = [];
const hasXDimension = Math.abs(deltaX) >= zeroThreshold;
const hasZDimension = Math.abs(deltaZ) >= zeroThreshold;
if (hasXDimension) {
const label = dimensionLabelPosition(origin, xEndpoint, zEndpoint, hasZDimension, width, height);
const start = dimensionLineStart(origin, xEndpoint);
lines.push({
axis: "x",
value: deltaX,
label: formatMoveDimension(deltaX, accuracy),
x1: start.x,
y1: start.y,
x2: xEndpoint.x,
y2: xEndpoint.y,
labelX: label.x,
labelY: label.y,
});
if (Math.hypot(currentAnchor.x - xEndpoint.x, currentAnchor.y - xEndpoint.y) >= 1) {
guides.push({
axis: "x",
x1: xEndpoint.x,
y1: xEndpoint.y,
x2: currentAnchor.x,
y2: currentAnchor.y,
});
}
}
if (hasZDimension) {
const label = dimensionLabelPosition(origin, zEndpoint, xEndpoint, hasXDimension, width, height);
const start = dimensionLineStart(origin, zEndpoint);
lines.push({
axis: "z",
value: deltaZ,
label: formatMoveDimension(deltaZ, accuracy),
x1: start.x,
y1: start.y,
x2: zEndpoint.x,
y2: zEndpoint.y,
labelX: label.x,
labelY: label.y,
});
if (Math.hypot(currentAnchor.x - zEndpoint.x, currentAnchor.y - zEndpoint.y) >= 1) {
guides.push({
axis: "z",
x1: zEndpoint.x,
y1: zEndpoint.y,
x2: currentAnchor.x,
y2: currentAnchor.y,
});
}
}
return lines.length > 0 ? { width, height, lines, guides } : null;
}
+197
View File
@@ -0,0 +1,197 @@
import * as THREE from "three";
import type { WorkplaneShape } from "@/types/sketchforge";
export type PlacementPoint = { x: number; y: number; z: number };
export type PlacementWorkplane = {
origin: PlacementPoint;
normal: PlacementPoint;
xAxis: PlacementPoint;
zAxis: PlacementPoint;
};
const EPSILON = 1e-8;
function vector(point: PlacementPoint) {
return new THREE.Vector3(point.x, point.y, point.z);
}
function point(value: THREE.Vector3): PlacementPoint {
return {
x: value.x === 0 ? 0 : value.x,
y: value.y === 0 ? 0 : value.y,
z: value.z === 0 ? 0 : value.z,
};
}
function clean(value: number) {
const rounded = Number(value.toFixed(6));
return Math.abs(rounded) < 1e-6 ? 0 : rounded;
}
function finitePoint(value: unknown): PlacementPoint | null {
if (!value || typeof value !== "object") return null;
const candidate = value as Partial<PlacementPoint>;
if (![candidate.x, candidate.y, candidate.z].every((coordinate) => typeof coordinate === "number" && Number.isFinite(coordinate))) {
return null;
}
return { x: candidate.x as number, y: candidate.y as number, z: candidate.z as number };
}
export function horizontalPlacementWorkplane(elevation = 0): PlacementWorkplane {
return {
origin: { x: 0, y: Number.isFinite(elevation) ? elevation : 0, z: 0 },
normal: { x: 0, y: 1, z: 0 },
xAxis: { x: 1, y: 0, z: 0 },
zAxis: { x: 0, y: 0, z: 1 },
};
}
export function normalizePlacementWorkplane(value: unknown, fallbackElevation = 0): PlacementWorkplane {
if (!value || typeof value !== "object") return horizontalPlacementWorkplane(fallbackElevation);
const candidate = value as Partial<PlacementWorkplane>;
const origin = finitePoint(candidate.origin);
const normal = finitePoint(candidate.normal);
const xAxis = finitePoint(candidate.xAxis);
if (!origin || !normal || !xAxis || vector(normal).lengthSq() < EPSILON || vector(xAxis).lengthSq() < EPSILON) {
return horizontalPlacementWorkplane(fallbackElevation);
}
return placementWorkplaneFromSurface(origin, normal, xAxis);
}
export function placementWorkplaneFingerprint(workplane: PlacementWorkplane) {
return [
workplane.origin.x, workplane.origin.y, workplane.origin.z,
workplane.normal.x, workplane.normal.y, workplane.normal.z,
workplane.xAxis.x, workplane.xAxis.y, workplane.xAxis.z,
workplane.zAxis.x, workplane.zAxis.y, workplane.zAxis.z,
].map(clean).join(":");
}
export function placementWorkplaneFromSurface(
origin: PlacementPoint,
surfaceNormal: PlacementPoint,
tangentHint: PlacementPoint,
reverse = false,
): PlacementWorkplane {
const normal = vector(surfaceNormal).normalize();
if (reverse) normal.negate();
let xAxis = vector(tangentHint).projectOnPlane(normal);
if (xAxis.lengthSq() < EPSILON) {
const candidates = [
new THREE.Vector3(1, 0, 0),
new THREE.Vector3(0, 0, 1),
new THREE.Vector3(0, 1, 0),
];
xAxis = candidates.reduce((best, candidate) => {
const projected = candidate.clone().projectOnPlane(normal);
return projected.lengthSq() > best.lengthSq() ? projected : best;
}, new THREE.Vector3());
}
xAxis.normalize();
const zAxis = xAxis.clone().cross(normal).normalize();
return {
origin: { x: origin.x, y: origin.y, z: origin.z },
normal: point(normal),
xAxis: point(xAxis),
zAxis: point(zAxis),
};
}
export function snapPlacementWorkplaneOrigin(
workplane: PlacementWorkplane,
step: number,
referenceOrigin: PlacementPoint = { x: 0, y: 0, z: 0 },
): PlacementWorkplane {
if (!Number.isFinite(step) || step <= 0) return workplane;
const normal = vector(workplane.normal);
const xAxis = vector(workplane.xAxis);
const zAxis = vector(workplane.zAxis);
const origin = vector(workplane.origin);
const reference = vector(referenceOrigin);
const anchor = reference.clone().addScaledVector(
normal,
origin.clone().sub(reference).dot(normal),
);
const offset = origin.clone().sub(anchor);
const snappedOrigin = anchor
.addScaledVector(xAxis, Math.round(offset.dot(xAxis) / step) * step)
.addScaledVector(zAxis, Math.round(offset.dot(zAxis) / step) * step);
return {
...workplane,
origin: point(snappedOrigin),
};
}
export function placementWorkplaneQuaternion(workplane: PlacementWorkplane) {
const basis = new THREE.Matrix4().makeBasis(
vector(workplane.xAxis),
vector(workplane.normal),
vector(workplane.zAxis),
);
return new THREE.Quaternion().setFromRotationMatrix(basis).normalize();
}
export function placementWorkplaneCoordinates(workplane: PlacementWorkplane, worldPoint: PlacementPoint) {
const relative = vector(worldPoint).sub(vector(workplane.origin));
return {
x: relative.dot(vector(workplane.xAxis)),
y: relative.dot(vector(workplane.normal)),
z: relative.dot(vector(workplane.zAxis)),
};
}
export function placementWorkplanePoint(workplane: PlacementWorkplane, x: number, z: number): PlacementPoint {
const world = vector(workplane.origin)
.addScaledVector(vector(workplane.xAxis), x)
.addScaledVector(vector(workplane.zAxis), z);
return point(world);
}
export function placementPatchForNewShape(
shape: Pick<WorkplaneShape, "height">,
workplane: PlacementWorkplane,
basePoint: PlacementPoint = workplane.origin,
): Pick<WorkplaneShape, "x" | "z" | "elevation" | "rotation" | "rotationX" | "rotationZ"> {
const quaternion = placementWorkplaneQuaternion(workplane);
const euler = new THREE.Euler().setFromQuaternion(quaternion, "XYZ");
const center = vector(basePoint).addScaledVector(vector(workplane.normal), shape.height / 2);
return {
x: clean(center.x),
z: clean(center.z),
elevation: clean(center.y - shape.height / 2),
rotation: clean(THREE.MathUtils.radToDeg(euler.y)),
rotationX: clean(THREE.MathUtils.radToDeg(euler.x)),
rotationZ: clean(THREE.MathUtils.radToDeg(euler.z)),
};
}
export function translationToWorkplane(
workplane: PlacementWorkplane,
worldVertices: PlacementPoint[],
): PlacementPoint {
if (worldVertices.length === 0) return { x: 0, y: 0, z: 0 };
const origin = vector(workplane.origin);
const normal = vector(workplane.normal);
const minimumDistance = worldVertices.reduce(
(minimum, vertex) => Math.min(minimum, vector(vertex).sub(origin).dot(normal)),
Number.POSITIVE_INFINITY,
);
const translation = normal.multiplyScalar(-minimumDistance);
return point(translation);
}
export function placementWorkplaneIsBase(workplane: PlacementWorkplane) {
return (
Math.abs(workplane.origin.x) < 1e-6
&& Math.abs(workplane.origin.y) < 1e-6
&& Math.abs(workplane.origin.z) < 1e-6
&& Math.abs(workplane.normal.x) < 1e-6
&& Math.abs(workplane.normal.y - 1) < 1e-6
&& Math.abs(workplane.normal.z) < 1e-6
);
}
@@ -0,0 +1,37 @@
export type RegularPolygonFootprintScale = {
x: number;
z: number;
offsetX: number;
offsetZ: number;
};
export function regularPolygonFootprintScale(
width: number,
depth: number,
sides: number,
): RegularPolygonFootprintScale {
const count = Math.max(3, Math.round(sides));
let minX = Number.POSITIVE_INFINITY;
let maxX = Number.NEGATIVE_INFINITY;
let minZ = Number.POSITIVE_INFINITY;
let maxZ = Number.NEGATIVE_INFINITY;
for (let index = 0; index < count; index += 1) {
const angle = (index / count) * Math.PI * 2;
const x = Math.sin(angle);
const z = Math.cos(angle);
minX = Math.min(minX, x);
maxX = Math.max(maxX, x);
minZ = Math.min(minZ, z);
maxZ = Math.max(maxZ, z);
}
const x = Math.max(0.001, width) / Math.max(0.001, maxX - minX);
const z = Math.max(0.001, depth) / Math.max(0.001, maxZ - minZ);
return {
x,
z,
offsetX: -((minX + maxX) / 2) * x,
offsetZ: -((minZ + maxZ) / 2) * z,
};
}
+36
View File
@@ -0,0 +1,36 @@
import {
placementPatchForNewShape,
placementWorkplanePoint,
type PlacementWorkplane,
} from "@/lib/placementWorkplane";
import { canonicalizeShape } from "@/lib/workplaneShapes";
import type { WorkplaneShape } from "@/types/sketchforge";
export function placeSketchExtrusion(
shape: WorkplaneShape,
workplane: PlacementWorkplane,
existing?: WorkplaneShape | null,
) {
if (existing) {
return canonicalizeShape({
...shape,
x: existing.x,
z: existing.z,
elevation: existing.elevation ?? 0,
rotation: existing.rotation,
rotationX: existing.rotationX ?? 0,
rotationZ: existing.rotationZ ?? 0,
mirrorX: existing.mirrorX,
mirrorY: existing.mirrorY,
mirrorZ: existing.mirrorZ,
locked: existing.locked,
hidden: existing.hidden,
});
}
const basePoint = placementWorkplanePoint(workplane, shape.x, shape.z);
return canonicalizeShape({
...shape,
...placementPatchForNewShape(shape, workplane, basePoint),
});
}
+30 -2
View File
@@ -1,5 +1,6 @@
import { strFromU8, strToU8, unzip, zip, type AsyncZippable } from "fflate";
import { editorHistoryEntry, hydrateEditorHistoryState, type EditorHistoryEntry } from "@/lib/editorHistory";
import { normalizePlacementWorkplane, placementWorkplaneIsBase, type PlacementWorkplane } from "@/lib/placementWorkplane";
import { normalizeProjectAsset, sha256Hex } from "@/lib/projectAssets";
import { canonicalizeShape } from "@/lib/workplaneShapes";
import { importedShapeFromStl } from "@/lib/stlImport";
@@ -10,7 +11,7 @@ import type { GridSize, ProjectAsset, ProjectAssetSourceFormat, SketchOperation,
export const SKF_SCHEMA_ID = "com.sketchforge.project";
export const SKF_FORMAT_VERSION = 1;
export const SKF_MINIMUM_READER_VERSION = 1;
export const SKF_CREATED_WITH_VERSION = "0.7.0";
export const SKF_CREATED_WITH_VERSION = "0.9.0";
export const SKF_MEDIA_TYPE = "application/vnd.sketchforge.project+zip";
export const SKF_LIMITS = {
@@ -120,6 +121,8 @@ export type SkfProjectDocumentV1 = {
snapGrid: GridSize;
selectedWorkplaneId: string;
placementElevation: number;
placementWorkplane?: PlacementWorkplane;
sketchPlacementWorkplane?: PlacementWorkplane;
};
};
@@ -135,6 +138,8 @@ export type SkfProjectExportInput = {
workspace: WorkplaneWorkspaceSettings;
snapGrid: GridSize;
placementElevation: number;
placementWorkplane?: PlacementWorkplane;
sketchPlacementWorkplane?: PlacementWorkplane;
compressionLevel?: 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9;
};
@@ -150,6 +155,8 @@ export type SkfRestoredProject = {
workspace: WorkplaneWorkspaceSettings;
snapGrid: GridSize;
placementElevation: number;
placementWorkplane: PlacementWorkplane;
sketchPlacementWorkplane: PlacementWorkplane;
migratedFromVersion?: number;
};
@@ -659,7 +666,9 @@ export async function exportSkfProject(input: SkfProjectExportInput) {
const indexes = activeProjectIndexes(activeState);
const now = Date.now();
const placementElevation = Number.isFinite(input.placementElevation) ? input.placementElevation : 0;
const selectedWorkplaneId = Math.abs(placementElevation) < 1e-9 ? "workplane-base" : "workplane-active";
const placementWorkplane = normalizePlacementWorkplane(input.placementWorkplane, placementElevation);
const sketchPlacementWorkplane = normalizePlacementWorkplane(input.sketchPlacementWorkplane);
const selectedWorkplaneId = placementWorkplaneIsBase(placementWorkplane) ? "workplane-base" : "workplane-active";
const document: SkfProjectDocumentV1 = {
schema: SKF_SCHEMA_ID,
formatVersion: SKF_FORMAT_VERSION,
@@ -689,6 +698,8 @@ export async function exportSkfProject(input: SkfProjectExportInput) {
snapGrid: normalizeSnapGrid(input.snapGrid),
selectedWorkplaneId,
placementElevation,
placementWorkplane,
sketchPlacementWorkplane,
},
};
builder.files["project.json"] = strToU8(`${JSON.stringify(document, null, 2)}\n`);
@@ -973,6 +984,16 @@ async function validateDocumentAndAssets(raw: unknown, files: ArchiveFiles) {
validateFeatureGraph(document.features, activeObjectIds);
const editor = objectRecord(document.editor, "editor");
finiteNumber(editor.placementElevation, "editor.placementElevation");
for (const fieldName of ["placementWorkplane", "sketchPlacementWorkplane"] as const) {
if (editor[fieldName] === undefined) continue;
const workplane = objectRecord(editor[fieldName], `editor.${fieldName}`);
for (const field of ["origin", "normal", "xAxis", "zAxis"] as const) {
const coordinates = objectRecord(workplane[field], `editor.${fieldName}.${field}`);
finiteNumber(coordinates.x, `editor.${fieldName}.${field}.x`);
finiteNumber(coordinates.y, `editor.${fieldName}.${field}.y`);
finiteNumber(coordinates.z, `editor.${fieldName}.${field}.z`);
}
}
return { document, assetById, stateById };
}
@@ -1118,6 +1139,8 @@ async function restoreV1(document: SkfProjectDocumentV1, assetById: Map<string,
workspace: normalizeWorkspaceSettings(document.editor.workspace),
snapGrid: normalizeSnapGrid(document.editor.snapGrid),
placementElevation: document.editor.placementElevation,
placementWorkplane: normalizePlacementWorkplane(document.editor.placementWorkplane, document.editor.placementElevation),
sketchPlacementWorkplane: normalizePlacementWorkplane(document.editor.sketchPlacementWorkplane),
} satisfies SkfRestoredProject;
}
@@ -1142,6 +1165,11 @@ function migrateV0(raw: Record<string, unknown>): SkfRestoredProject {
workspace: normalizeWorkspaceSettings(raw.workspace),
snapGrid: normalizeSnapGrid(raw.snapGrid),
placementElevation: typeof raw.placementElevation === "number" && Number.isFinite(raw.placementElevation) ? raw.placementElevation : 0,
placementWorkplane: normalizePlacementWorkplane(
raw.placementWorkplane,
typeof raw.placementElevation === "number" && Number.isFinite(raw.placementElevation) ? raw.placementElevation : 0,
),
sketchPlacementWorkplane: normalizePlacementWorkplane(raw.sketchPlacementWorkplane),
migratedFromVersion: 0,
};
}
+1 -1
View File
@@ -19,7 +19,7 @@ export const DEFAULT_WORKPLANE_WORKSPACE: WorkplaneWorkspaceSettings = {
units: "Metric (Default)",
scale: "1:1 (millimeters)",
accuracy: 2,
historyLimit: "unlimited",
historyLimit: 100,
};
const snapGridOptions: GridSize[] = ["Off", "0.1 mm", "0.25 mm", "0.5 mm", "1.0 mm", "2.0 mm", "5.0 mm", "Brick"];
+1 -1
View File
@@ -124,7 +124,7 @@ export function fallbackSolidColor(shape: WorkplaneShape) {
}
export function withHoleMode(shape: WorkplaneShape, hole: boolean, parentColor?: string): WorkplaneShape {
const color = hole ? "#b8c2cc" : (parentColor ?? fallbackSolidColor(shape));
const color = parentColor ?? shape.color;
return {
...shape,
hole,