Improve editor history, CAD tools, and ruler workflows

This commit is contained in:
Formsmith
2026-07-14 09:43:37 +03:00
parent 20731043e4
commit 400c4bf86e
23 changed files with 1727 additions and 388 deletions
+1
View File
@@ -1,5 +1,6 @@
export const CAD_MODIFIER_RUNTIME_BASE = "/occt";
export const CAD_MODIFIER_REQUEST_TIMEOUT_MS = 30_000;
export const CAD_MODIFIER_MAX_SHARP_ANGLE = 90;
export type CadModifierRequestPhase = "prepare" | "preview";
+71 -1
View File
@@ -1,12 +1,68 @@
import { canonicalizeShape } from "@/lib/workplaneShapes";
import type { EdgeTreatmentHistoryEntry, WorkplaneShape } from "@/types/sketchforge";
export function cloneWorkplaneShapeSnapshot(shape: WorkplaneShape): WorkplaneShape {
export function edgeTreatmentAppliedFrame(shape: WorkplaneShape): NonNullable<EdgeTreatmentHistoryEntry["appliedFrame"]> {
return {
x: shape.x,
z: shape.z,
elevation: shape.elevation ?? 0,
width: shape.width,
depth: shape.depth,
height: shape.height,
rotation: shape.rotation ?? 0,
rotationX: shape.rotationX ?? 0,
rotationZ: shape.rotationZ ?? 0,
mirrorX: Boolean(shape.mirrorX),
mirrorY: Boolean(shape.mirrorY),
mirrorZ: Boolean(shape.mirrorZ),
};
}
function cloneJsonShape(shape: WorkplaneShape): WorkplaneShape {
return JSON.parse(JSON.stringify(canonicalizeShape(shape))) as WorkplaneShape;
}
function compactSnapshotHistory(shape: WorkplaneShape, stripOwnHistory: boolean): WorkplaneShape {
const next: WorkplaneShape = {
...shape,
groupedShapes: shape.groupedShapes?.map((child) => compactSnapshotHistory(child, false)),
};
if (stripOwnHistory) {
delete next.edgeTreatmentHistory;
} else if (shape.edgeTreatmentHistory) {
next.edgeTreatmentHistory = shape.edgeTreatmentHistory.map((entry) => ({
...entry,
before: compactSnapshotHistory(entry.before, true),
}));
}
return next;
}
export function cloneWorkplaneShapeSnapshot(shape: WorkplaneShape): WorkplaneShape {
return canonicalizeShape(compactSnapshotHistory(cloneJsonShape(shape), true));
}
export function compactEdgeTreatmentHistory(history: EdgeTreatmentHistoryEntry[] | undefined): EdgeTreatmentHistoryEntry[] {
return (history ?? []).map((entry) => ({
...entry,
feature: { ...entry.feature },
before: cloneWorkplaneShapeSnapshot(entry.before),
}));
}
export function restoreShapeBeforeEdgeTreatment(shape: WorkplaneShape, entry: EdgeTreatmentHistoryEntry) {
const before = cloneWorkplaneShapeSnapshot(entry.before);
const entryIndex = (shape.edgeTreatmentHistory ?? []).findIndex((candidate) => candidate.id === entry.id);
const earlierHistory = entryIndex > 0
? compactEdgeTreatmentHistory(shape.edgeTreatmentHistory?.slice(0, entryIndex))
: undefined;
const applied = entry.appliedFrame;
const width = applied ? before.width * shape.width / Math.max(0.001, applied.width) : before.width;
const depth = applied ? before.depth * shape.depth / Math.max(0.001, applied.depth) : before.depth;
const height = applied ? before.height * shape.height / Math.max(0.001, applied.height) : before.height;
const toggleMirror = (beforeValue: boolean | undefined, currentValue: boolean | undefined, appliedValue: boolean | undefined) => (
Boolean(beforeValue) !== (Boolean(currentValue) !== Boolean(appliedValue))
) || undefined;
return canonicalizeShape({
...before,
id: shape.id,
@@ -15,5 +71,19 @@ export function restoreShapeBeforeEdgeTreatment(shape: WorkplaneShape, entry: Ed
hole: shape.hole || undefined,
locked: shape.locked,
hidden: shape.hidden,
x: applied ? before.x + shape.x - applied.x : before.x,
z: applied ? before.z + shape.z - applied.z : before.z,
elevation: applied ? (before.elevation ?? 0) + (shape.elevation ?? 0) - applied.elevation : before.elevation,
width,
depth,
height,
size: Math.max(width, depth),
rotation: applied ? (before.rotation ?? 0) + (shape.rotation ?? 0) - applied.rotation : before.rotation,
rotationX: applied ? (before.rotationX ?? 0) + (shape.rotationX ?? 0) - applied.rotationX : before.rotationX,
rotationZ: applied ? (before.rotationZ ?? 0) + (shape.rotationZ ?? 0) - applied.rotationZ : before.rotationZ,
mirrorX: applied ? toggleMirror(before.mirrorX, shape.mirrorX, applied.mirrorX) : before.mirrorX,
mirrorY: applied ? toggleMirror(before.mirrorY, shape.mirrorY, applied.mirrorY) : before.mirrorY,
mirrorZ: applied ? toggleMirror(before.mirrorZ, shape.mirrorZ, applied.mirrorZ) : before.mirrorZ,
edgeTreatmentHistory: earlierHistory?.length ? earlierHistory : undefined,
});
}
+107
View File
@@ -0,0 +1,107 @@
import type { WorkplaneShape } from "@/types/sketchforge";
import { canonicalizeShape, serializeShapesForSync } from "@/lib/workplaneShapes";
export const MAX_EDITOR_HISTORY_ENTRIES = 100;
export const MAX_EDITOR_HISTORY_BYTES = 64 * 1024 * 1024;
export type EditorHistoryEntry = {
shapes: WorkplaneShape[];
selectedIds: string[];
fingerprint: string;
estimatedBytes: number;
};
export type EditorHistoryState = {
entries: EditorHistoryEntry[];
index: number;
};
export function serializedSceneSignature(shapes: WorkplaneShape[]) {
const serialized = serializeShapesForSync(shapes);
let hashA = 2166136261;
let hashB = 5381;
for (let index = 0; index < serialized.length; index += 1) {
const code = serialized.charCodeAt(index);
hashA = Math.imul(hashA ^ code, 16777619);
hashB = Math.imul(hashB, 33) ^ code;
}
return {
fingerprint: `${serialized.length}:${hashA >>> 0}:${hashB >>> 0}`,
estimatedBytes: serialized.length * 2,
};
}
export function projectShapesFingerprint(shapes: WorkplaneShape[]) {
return serializedSceneSignature(shapes).fingerprint;
}
export function editorHistoryEntry(shapes: WorkplaneShape[], selectedIds: string[]): EditorHistoryEntry {
const canonicalShapes = shapes.map(canonicalizeShape);
const validSelection = selectedIds.filter((id, index) => selectedIds.indexOf(id) === index && canonicalShapes.some((shape) => shape.id === id));
return {
shapes: canonicalShapes,
selectedIds: validSelection,
...serializedSceneSignature(canonicalShapes),
};
}
export function boundedEditorHistory(entries: EditorHistoryEntry[]) {
const bounded = entries.slice(-MAX_EDITOR_HISTORY_ENTRIES);
let totalBytes = bounded.reduce((total, entry) => total + entry.estimatedBytes, 0);
while (bounded.length > 2 && totalBytes > MAX_EDITOR_HISTORY_BYTES) {
const removed = bounded.shift();
totalBytes -= removed?.estimatedBytes ?? 0;
}
return bounded;
}
export function appendEditorHistorySnapshot(entries: EditorHistoryEntry[], requestedIndex: number, entry: EditorHistoryEntry) {
const index = Math.min(Math.max(0, requestedIndex), Math.max(0, entries.length - 1));
const current = entries[index];
if (current?.fingerprint === entry.fingerprint) {
const selectionChanged = current.selectedIds.join("\0") !== entry.selectedIds.join("\0");
return {
entries: selectionChanged ? entries.map((candidate, candidateIndex) => candidateIndex === index ? { ...candidate, selectedIds: entry.selectedIds } : candidate) : entries,
index,
changed: false,
};
}
const nextEntries = boundedEditorHistory([...entries.slice(0, index + 1), entry]);
return { entries: nextEntries, index: nextEntries.length - 1, changed: true };
}
export function hydrateEditorHistoryState(
currentShapes: WorkplaneShape[],
storedEntries: EditorHistoryEntry[] | undefined,
requestedIndex: number | undefined,
): EditorHistoryState {
const fallback = editorHistoryEntry(currentShapes, []);
if (!Array.isArray(storedEntries) || storedEntries.length === 0) {
return { entries: [fallback], index: 0 };
}
try {
const normalized = storedEntries.map((entry) =>
editorHistoryEntry(
Array.isArray(entry?.shapes) ? entry.shapes : [],
Array.isArray(entry?.selectedIds) ? entry.selectedIds.filter((id): id is string => typeof id === "string") : [],
),
);
const index = Number.isInteger(requestedIndex)
? Math.min(Math.max(0, requestedIndex as number), normalized.length - 1)
: normalized.length - 1;
if (normalized[index]?.fingerprint !== fallback.fingerprint) {
return { entries: [fallback], index: 0 };
}
const entries = boundedEditorHistory(normalized);
const boundedIndex = index - (normalized.length - entries.length);
if (boundedIndex < 0 || boundedIndex >= entries.length) {
return { entries: [fallback], index: 0 };
}
return { entries, index: boundedIndex };
} catch {
return { entries: [fallback], index: 0 };
}
}
+10
View File
@@ -0,0 +1,10 @@
export const DEFAULT_SPHERE_STEPS = 24;
export function sphereTessellation(steps = DEFAULT_SPHERE_STEPS) {
const normalizedSteps = Math.max(6, Math.round(steps));
return {
widthSegments: Math.max(8, normalizedSteps * 2),
heightSegments: Math.max(6, normalizedSteps),
};
}
+10
View File
@@ -64,3 +64,13 @@ export function normalizeWorkspaceSettings(value: unknown, fallback: WorkplaneWo
export function workplaneSettingsFingerprint(workspace: WorkplaneWorkspaceSettings, snapGrid: GridSize) {
return JSON.stringify({ workspace, snapGrid });
}
export function workspaceHydrationSyncDecision(pendingFingerprint: string | null, currentFingerprint: string) {
if (pendingFingerprint === null) {
return { shouldSync: true, pendingFingerprint: null };
}
return {
shouldSync: false,
pendingFingerprint: currentFingerprint === pendingFingerprint ? null : pendingFingerprint,
};
}
+8 -2
View File
@@ -29,8 +29,14 @@ export function shapeDepth(shape: WorkplaneShape) {
return shape.depth ?? shape.size;
}
function edgeTreatmentPreserveZone(shape: WorkplaneShape): number {
const own = Math.max(...(shape.edgeTreatments ?? []).map((feature) => feature.amount), 0);
const child = Math.max(...(shape.groupedShapes ?? []).map(edgeTreatmentPreserveZone), 0);
return Math.max(own, child);
}
export function preservesEdgeTreatmentSize(shape: WorkplaneShape) {
return shape.edgeResizeMode === "preserve" && Boolean(shape.importedMesh && shape.edgeTreatments?.length);
return shape.edgeResizeMode === "preserve" && Boolean(shape.importedMesh && edgeTreatmentPreserveZone(shape) > 0);
}
function edgePreservedCoordinate(value: number, baseSize: number, targetSize: number, centered: boolean, requestedZone: number) {
@@ -57,7 +63,7 @@ export function resizedImportedCoordinates(shape: WorkplaneShape, sourcePosition
const depth = shapeDepth(shape);
const height = shape.height;
const preserve = preservesEdgeTreatmentSize(shape);
const zone = preserve ? Math.max(...(shape.edgeTreatments ?? []).map((feature) => feature.amount), 0) : 0;
const zone = preserve ? edgeTreatmentPreserveZone(shape) : 0;
const positions = new Array<number>(sourcePositions.length);
for (let index = 0; index + 2 < sourcePositions.length; index += 3) {
if (preserve) {