"use client"; import { Check, CircleDotDashed, Download, X } from "lucide-react"; import type manifoldModule from "manifold-3d"; import type { ManifoldToplevel } from "manifold-3d"; import { useCallback, useEffect, useMemo, useRef, useState } from "react"; import { ADDITION, Brush, Evaluator, HOLLOW_INTERSECTION, HOLLOW_SUBTRACTION, INTERSECTION, SUBTRACTION, type CSGOperation } from "three-bvh-csg"; import * as THREE from "three"; import { TextGeometry } from "three/examples/jsm/geometries/TextGeometry.js"; import { RoundedBoxGeometry } from "three/examples/jsm/geometries/RoundedBoxGeometry.js"; import { FontLoader, type Font, type FontData } from "three/examples/jsm/loaders/FontLoader.js"; import { SVGLoader } from "three/examples/jsm/loaders/SVGLoader.js"; import droidMonoFontJson from "three/examples/fonts/droid/droid_sans_mono_regular.typeface.json"; import droidSansBoldFontJson from "three/examples/fonts/droid/droid_sans_bold.typeface.json"; import droidSerifBoldFontJson from "three/examples/fonts/droid/droid_serif_bold.typeface.json"; import gentilisBoldFontJson from "three/examples/fonts/gentilis_bold.typeface.json"; import helvetikerBoldFontJson from "three/examples/fonts/helvetiker_bold.typeface.json"; import optimerBoldFontJson from "three/examples/fonts/optimer_bold.typeface.json"; import { manifoldModuleSource } from "@/generated/manifoldModuleSource"; import { manifoldWasmBase64 } from "@/generated/manifoldWasmBase64"; import { ToolbarAlignIcon, ToolbarChamferIcon, ToolbarCaretDownIcon, ToolbarCopyIcon, ToolbarDuplicateIcon, ToolbarDropToWorkplaneIcon, ToolbarExportIcon, ToolbarGroupIcon, ToolbarHideSelectedIcon, ToolbarHomeIcon, ToolbarImportIcon, ToolbarIntersectionIcon, ToolbarFilletIcon, ToolbarMirrorIcon, ToolbarPasteIcon, ToolbarRedoIcon, ToolbarSnapGridIcon, ToolbarSettingsIcon, ToolbarShapeAddIcon, ToolbarTrashIcon, ToolbarUngroupIcon, ToolbarUndoIcon, ToolbarVectorExportIcon, ToolbarWorkplaneIcon, } from "./icons"; import { WorkplaneViewport } from "./WorkplaneViewport"; import { SketchWorkspace, type SketchMeasurement, type SketchSelection, type SketchTool } from "./SketchWorkspace"; import { EdgeModifierPanel } from "./workplane/EdgeModifierPanel"; import { ScrewHolePanel } from "./workplane/ScrewHolePanel"; import { canonicalizeShape, cleanNearZero, cleanRotationDegrees, fallbackSolidColor, mirroredAxisCount, mirrorSign, normalizeDegrees, preservesEdgeTreatmentSize, resizedImportedMeshPositions, serializeShapesForSync, shapeDepth, shapeWidth, workplaneShapesEqual, } from "@/lib/workplaneShapes"; import { bakeCadMetadataForShapeTransform, cadBrepTransformForShape, cadModifierPrimitiveForAnalyticBox, cadModifierPrimitiveForBakedShape } from "@/lib/cadBakeMetadata"; import { createLocalId } from "@/lib/localIds"; import { projectExportFileName } from "@/lib/exportNames"; import { createScrewHoleShape, DEFAULT_SCREW_HOLE_CONFIG, type ScrewHoleConfig } from "@/lib/screwHoles"; import { makeShapeFromAsset, sceneShape, toolbarShapeAssets, type ToolbarShapeAsset } from "@/lib/shapeCatalog"; import { importedShapeFromStl, importExtensionSupported } from "@/lib/stlImport"; import { normalizeWorkspaceSettings } from "@/lib/workplaneSettings"; import { SKETCHFORGE_MCP_POLL_MS, SKETCHFORGE_MCP_ROUTE, type SketchForgeMcpCommand, type SketchForgeMcpSceneSummary, type SketchForgeMcpShapeSummary, type SketchForgeMcpViewFace, } from "@/lib/sketchforgeMcpProtocol"; import type { CadModifierComponentMesh, CadModifierDisplayEdge, CadModifierEdge, CadModifierKind, CadModifierMeshPart, CadModifierPrimitivePart, CadModifierQuality, CadModifierWorkerRequest, CadModifierWorkerResponse } from "@/lib/cadModifierTypes"; import type { AlignAxis, AlignHandleStatus, AlignTarget, GridSize, ShapeAsset, SketchImage, SketchPoint, SketchProfile, SketchSegment, WorkplaneShape, WorkplaneWorkspaceSettings } from "@/types/sketchforge"; export { importedShapeFromStl }; type TopPanel = "import" | "export" | "tips" | "profile" | "settings" | null; type ExportFormat = "stl" | "obj"; type ToolbarMode = "geometry" | "sketch"; type Vec3 = [number, number, number]; type MeshData = { name: string; vertices: Vec3[]; faces: [number, number, number][] }; type Cuboid = { minX: number; maxX: number; minY: number; maxY: number; minZ: number; maxZ: number }; type ShapeUpdatePatch = Partial & { bakeTransform?: boolean }; type WithoutRequestId = T extends unknown ? Omit : never; type CadModifierWorkerPayload = WithoutRequestId; type EdgeModifierSession = { kind: CadModifierKind; edges: CadModifierEdge[]; selectedEdgeIds: number[]; amount: number; sharpAngle: number; chamferAngle: number; quality: CadModifierQuality; tangentChain: boolean; preserveEdgeSize: boolean; busy: boolean; prepared: boolean; error: string | null; preview: WorkplaneShape | null; componentPreviews: EdgeModifierComponentPreview[]; }; type EdgeModifierComponentPreview = { owner: number; shape: WorkplaneShape; }; type EdgeFeatureRevertOption = { id: string; entryId: string; path: number[]; label: string; targetName: string; createdAt: number; removesNewerCount: number; }; type ManifoldSolid = ReturnType; type DownloadResult = { mode: "browser" } | { mode: "folder"; path: string }; type GroupBuildResult = { group: WorkplaneShape | null; booleanSelection: WorkplaneShape[]; hasSolid: boolean; hasHole: boolean; hasImportedMesh: boolean; consumed: boolean; failureNotice: string; }; type IntersectionAttempt = | { status: "success"; group: WorkplaneShape } | { status: "empty" } | { status: "unsupported" }; type IntersectionBuildResult = { group: WorkplaneShape | null; empty: boolean; failureNotice: string; }; type BooleanAutomationMode = "before" | "after" | "ungroup"; type BooleanAutomationResult = { ok: boolean; caseId: string; label: string; mode: BooleanAutomationMode; notice: string; shapeCount: number; selectedCount: number; triangleCount?: number; groupedCount?: number; groupId?: string; error?: string; }; const DOWNLOAD_MODE_STORAGE_KEY = "sketchForge.downloadMode"; const DOWNLOAD_FOLDER_STORAGE_KEY = "sketchForge.downloadFolder"; const SHARED_CLIPBOARD_STORAGE_KEY = "sketchForge.clipboard"; const SYSTEM_CLIPBOARD_PREFIX = "SKETCHFORGE3D/1\n"; const STATIC_EXPORT_BUILD = process.env.NEXT_PUBLIC_STATIC_EXPORT === "true"; declare global { interface Window { __sketchforgeBooleanTest?: BooleanAutomationResult; __sketchforgeBooleanTestImage?: string; sketchforgeCaptureCanvas?: () => string; sketchforgeCaptureView?: (face?: SketchForgeMcpViewFace) => Promise | string; } } const CUTTER_PADDING = 0.05; const POINT_TOLERANCE = 0.0001; const CUTTER_RESIDUAL_INSET = CUTTER_PADDING * 0.4; const MIN_SHAPE_DIMENSION = 0.01; const MODEL_DIMENSION_PRECISION = 3; const IMPORTED_EXACT_BOOLEAN_TRIANGLE_LIMIT = 150000; const COPLANAR_BOOLEAN_RESCUE_DEGREES = 0.02; const NORMAL_SELECTION_CAD_EDGE_MIN_ANGLE = 60; const MIN_EDGE_MODIFIER_AMOUNT = 0.001; const SEPARATE_PARTS_VERTEX_TOLERANCE = 0.0005; const svgLoader = new SVGLoader(); const booleanFontLoader = new FontLoader(); const booleanTextFonts: Record = { Multilanguage: booleanFontLoader.parse(helvetikerBoldFontJson as FontData), Sans: booleanFontLoader.parse(droidSansBoldFontJson as FontData), Serif: booleanFontLoader.parse(droidSerifBoldFontJson as FontData), Script: booleanFontLoader.parse(gentilisBoldFontJson as FontData), Monospace: booleanFontLoader.parse(droidMonoFontJson as FontData), Rounded: booleanFontLoader.parse(optimerBoldFontJson as FontData), Stencil: booleanFontLoader.parse(helvetikerBoldFontJson as FontData), }; let manifoldRuntimePromise: Promise | null = null; function emptySketchProfile(): SketchProfile { return { points: [], segments: [], images: [] }; } function cloneSketchProfile(profile: SketchProfile): SketchProfile { return { points: profile.points.map((point) => ({ ...point, handleIn: point.handleIn ? { ...point.handleIn } : undefined, handleOut: point.handleOut ? { ...point.handleOut } : undefined, })), segments: profile.segments.map((segment) => ({ ...segment })), images: (profile.images ?? []).map((image) => ({ ...image })), }; } type OrderedSketchStep = { segment: SketchProfile["segments"][number]; from: SketchPoint; to: SketchPoint }; type OrderedSketchPath = { points: SketchPoint[]; steps: OrderedSketchStep[]; closed: boolean }; function orderedSketchPaths(profile: SketchProfile): OrderedSketchPath[] { const pointById = new Map(profile.points.map((point) => [point.id, point])); const adjacency = new Map>(); profile.points.forEach((point) => adjacency.set(point.id, [])); const validSegments = profile.segments.filter((segment) => { if (!pointById.has(segment.startId) || !pointById.has(segment.endId) || segment.startId === segment.endId) return; adjacency.get(segment.startId)?.push({ pointId: segment.endId, segment }); adjacency.get(segment.endId)?.push({ pointId: segment.startId, segment }); return true; }); const unvisited = new Set(validSegments.map((segment) => segment.id)); const paths: OrderedSketchPath[] = []; while (unvisited.size > 0) { const seedId = unvisited.values().next().value as string | undefined; const seed = validSegments.find((segment) => segment.id === seedId); if (!seed) break; const componentIds = new Set(); const queue = [seed.startId, seed.endId]; while (queue.length > 0) { const id = queue.pop(); if (!id || componentIds.has(id)) continue; componentIds.add(id); adjacency.get(id)?.forEach((entry) => queue.push(entry.pointId)); } const startId = [...componentIds].find((id) => (adjacency.get(id)?.filter((entry) => unvisited.has(entry.segment.id)).length ?? 0) === 1) ?? seed.startId; const first = pointById.get(startId); if (!first) { unvisited.delete(seed.id); continue; } const points = [first]; const steps: OrderedSketchStep[] = []; let currentId = startId; for (let guard = 0; guard <= validSegments.length; guard += 1) { const edge = adjacency.get(currentId)?.find((entry) => unvisited.has(entry.segment.id)); if (!edge) break; const from = pointById.get(currentId); const to = pointById.get(edge.pointId); if (!from || !to) break; unvisited.delete(edge.segment.id); steps.push({ segment: edge.segment, from, to }); currentId = to.id; if (currentId === startId) break; points.push(to); } paths.push({ points, steps, closed: currentId === startId && steps.length >= 3 }); } return paths; } function withSmoothSketchHandles(profile: SketchProfile) { const next = cloneSketchProfile(profile); const points = new Map(next.points.map((point) => [point.id, point])); orderedSketchPaths(next).forEach((path) => { path.points.forEach((sourcePoint, index) => { const point = points.get(sourcePoint.id); if (!point) return; const previous = path.closed ? path.points[(index - 1 + path.points.length) % path.points.length] : path.points[Math.max(0, index - 1)]; const following = path.closed ? path.points[(index + 1) % path.points.length] : path.points[Math.min(path.points.length - 1, index + 1)]; const tangentX = (following.x - previous.x) / 6; const tangentZ = (following.z - previous.z) / 6; point.handleIn = { x: point.x - tangentX, z: point.z - tangentZ }; point.handleOut = { x: point.x + tangentX, z: point.z + tangentZ }; point.mode = "smooth"; }); }); return next; } function pointInSketchPolygon(point: THREE.Vector2, polygon: THREE.Vector2[]) { let inside = false; for (let index = 0, previous = polygon.length - 1; index < polygon.length; previous = index, index += 1) { const currentPoint = polygon[index]; const previousPoint = polygon[previous]; const crosses = currentPoint.y > point.y !== previousPoint.y > point.y; if (crosses && point.x < ((previousPoint.x - currentPoint.x) * (point.y - currentPoint.y)) / (previousPoint.y - currentPoint.y) + currentPoint.x) { inside = !inside; } } return inside; } function shapeFromSketchProfile(profile: SketchProfile, height: number, existing?: WorkplaneShape | null) { const closedPaths = orderedSketchPaths(profile).filter((path) => path.closed); if (closedPaths.length === 0) return null; const profilePoints = closedPaths.flatMap((path) => path.points); const minX = Math.min(...profilePoints.map((point) => point.x)); const maxX = Math.max(...profilePoints.map((point) => point.x)); const minZ = Math.min(...profilePoints.map((point) => point.z)); const maxZ = Math.max(...profilePoints.map((point) => point.z)); const centerX = (minX + maxX) / 2; const centerZ = (minZ + maxZ) / 2; const width = Math.max(0.01, maxX - minX); const depth = Math.max(0.01, maxZ - minZ); const safeHeight = Math.max(0.01, height); const outlineRecords = closedPaths.map((path) => { const outline = new THREE.Shape(); const first = path.points[0]; outline.moveTo(first.x - centerX, -(first.z - centerZ)); path.steps.forEach(({ segment, from, to }) => { const forward = segment.startId === from.id; const control1 = forward ? from.handleOut : from.handleIn; const control2 = forward ? to.handleIn : to.handleOut; if (segment.kind !== "line" && control1 && control2) { outline.bezierCurveTo( control1.x - centerX, -(control1.z - centerZ), control2.x - centerX, -(control2.z - centerZ), to.x - centerX, -(to.z - centerZ), ); } else { outline.lineTo(to.x - centerX, -(to.z - centerZ)); } }); outline.closePath(); const polygon = outline.extractPoints(16).shape; return { outline, polygon, area: Math.abs(THREE.ShapeUtils.area(polygon)) }; }); const sortedOutlines = [...outlineRecords].sort((a, b) => b.area - a.area); const outlines: THREE.Shape[] = []; sortedOutlines.forEach((record) => { const sample = record.polygon[0]; const parent = sample ? sortedOutlines .filter((candidate) => candidate !== record && candidate.area > record.area && pointInSketchPolygon(sample, candidate.polygon)) .sort((a, b) => a.area - b.area)[0] : undefined; if (parent) parent.outline.holes.push(record.outline); else outlines.push(record.outline); }); const hasCurves = profile.segments.some((segment) => segment.kind === "bezier" || segment.kind === "smooth"); const longestHandle = profile.points.reduce((longest, point) => Math.max( longest, point.handleIn ? Math.hypot(point.handleIn.x - point.x, point.handleIn.z - point.z) : 0, point.handleOut ? Math.hypot(point.handleOut.x - point.x, point.handleOut.z - point.z) : 0, ), 0); const curveScale = Math.max(width, depth, longestHandle * 2); const curveSegments = hasCurves ? Math.min(256, Math.max(32, Math.ceil(curveScale * 1.25))) : 1; const geometry = new THREE.ExtrudeGeometry(outlines, { depth: safeHeight, bevelEnabled: false, steps: 1, curveSegments }); geometry.rotateX(-Math.PI / 2); geometry.computeVertexNormals(); geometry.computeBoundingBox(); const geometryBox = geometry.boundingBox; const meshWidth = Math.max(0.01, geometryBox ? geometryBox.max.x - geometryBox.min.x : width); const meshDepth = Math.max(0.01, geometryBox ? geometryBox.max.z - geometryBox.min.z : depth); const meshCenterX = centerX + (geometryBox ? (geometryBox.min.x + geometryBox.max.x) / 2 : 0); const meshCenterZ = centerZ + (geometryBox ? (geometryBox.min.z + geometryBox.max.z) / 2 : 0); const meshGeometry = geometry.index ? geometry.toNonIndexed() : geometry; const position = meshGeometry.getAttribute("position"); const normal = meshGeometry.getAttribute("normal"); const positions = Array.from(position.array as ArrayLike); const normals = normal ? Array.from(normal.array as ArrayLike) : undefined; if (meshGeometry !== geometry) meshGeometry.dispose(); geometry.dispose(); return canonicalizeShape({ id: existing?.id ?? createLocalId("sketch-extrusion"), name: existing?.name ?? "Sketch extrusion", kind: "mesh", color: existing?.color ?? "#d41721", hole: existing?.hole, x: meshCenterX, z: meshCenterZ, elevation: 0, size: Math.max(meshWidth, meshDepth), width: meshWidth, depth: meshDepth, height: safeHeight, rotation: 0, importedMesh: { positions, normals, baseWidth: meshWidth, baseDepth: meshDepth, baseHeight: safeHeight, triangleCount: Math.floor(positions.length / 9), sourceFormat: "json", }, sketchProfile: cloneSketchProfile(profile), } satisfies WorkplaneShape); } function cleanModelDimension(value: number) { return Math.max(MIN_SHAPE_DIMENSION, Number(value.toFixed(MODEL_DIMENSION_PRECISION))); } function meshYawDegrees(shape: WorkplaneShape) { const isRoundPrimitive = !shape.importedMesh && (shape.kind === "cylinder" || shape.kind === "cone"); const isCircular = Math.abs(shapeWidth(shape) - shapeDepth(shape)) < 0.0005; // A circular cylinder/cone is invariant around Y. Ignoring that purely visual // yaw keeps its tessellated export at the requested diameter after grouping. return isRoundPrimitive && isCircular ? 0 : shape.rotation; } function parseClipboardShapes(serialized: string) { try { const parsed = JSON.parse(serialized); if (!Array.isArray(parsed)) { return []; } return parsed.flatMap((shape: Partial) => { const { name, kind, color } = shape; if (typeof name !== "string" || typeof kind !== "string" || typeof color !== "string") { return []; } return [canonicalizeShape(sceneShape({ ...shape, name, kind, color }))]; }); } catch { return []; } } function readSharedClipboard() { if (typeof window === "undefined") { return []; } return parseClipboardShapes(window.localStorage.getItem(SHARED_CLIPBOARD_STORAGE_KEY) ?? "[]"); } async function readSystemClipboard() { if (typeof navigator === "undefined" || !navigator.clipboard?.readText) { return []; } try { const value = await navigator.clipboard.readText(); return value.startsWith(SYSTEM_CLIPBOARD_PREFIX) ? parseClipboardShapes(value.slice(SYSTEM_CLIPBOARD_PREFIX.length)) : []; } catch { return []; } } function copyTextWithSelectionFallback(value: string) { if (typeof document === "undefined" || typeof document.execCommand !== "function") { return; } const previousFocus = document.activeElement instanceof HTMLElement ? document.activeElement : null; const textarea = document.createElement("textarea"); textarea.value = value; textarea.readOnly = true; textarea.setAttribute("aria-hidden", "true"); textarea.style.position = "fixed"; textarea.style.left = "-10000px"; textarea.style.top = "0"; textarea.style.opacity = "0"; document.body.appendChild(textarea); textarea.select(); try { document.execCommand("copy"); } catch { // The modern Clipboard API below may still succeed. } textarea.remove(); previousFocus?.focus({ preventScroll: true }); } function writeSharedClipboard(shapes: WorkplaneShape[]) { if (typeof window === "undefined") { return; } const serialized = serializeShapesForSync(shapes); try { window.localStorage.setItem(SHARED_CLIPBOARD_STORAGE_KEY, serialized); } catch { // The system clipboard can still carry large models if local storage is full. } const systemPayload = `${SYSTEM_CLIPBOARD_PREFIX}${serialized}`; copyTextWithSelectionFallback(systemPayload); if (navigator.clipboard?.writeText) { void navigator.clipboard.writeText(systemPayload).catch(() => { // Same-origin tabs still have the local-storage fallback. }); } } function base64ToUint8Array(value: string) { const binary = window.atob(value); const bytes = new Uint8Array(binary.length); for (let i = 0; i < binary.length; i += 1) { bytes[i] = binary.charCodeAt(i); } return bytes; } async function importBundledManifoldModule() { const blobUrl = URL.createObjectURL(new Blob([manifoldModuleSource], { type: "text/javascript" })); try { return (await import(/* webpackIgnore: true */ blobUrl)) as { default: typeof manifoldModule }; } finally { URL.revokeObjectURL(blobUrl); } } function getManifoldRuntime() { const assetBase = typeof window === "undefined" ? "/" : new URL(".", window.location.href).href; const isFileBuild = typeof window !== "undefined" && window.location.protocol === "file:"; const manifoldScriptUrl = new URL("manifold.js", assetBase).href; const runtimeModule = isFileBuild ? importBundledManifoldModule().then((module) => module.default) : import(/* webpackIgnore: true */ manifoldScriptUrl).then((module) => (module as { default: typeof manifoldModule }).default); manifoldRuntimePromise ??= runtimeModule .then((module) => { if (isFileBuild) { return (module as unknown as (config: { wasmBinary: Uint8Array }) => Promise)({ wasmBinary: base64ToUint8Array(manifoldWasmBase64), }); } return module({ locateFile: ((file: string) => (file.endsWith(".wasm") ? new URL("manifold.wasm", assetBase).href : new URL(file, assetBase).href)) as () => string, }); }) .then((runtime) => { runtime.setup(); return runtime; }); return manifoldRuntimePromise; } function stlBoxTrianglePositions(width: number, depth: number, height: number) { const x = width / 2; const z = depth / 2; const vertices: Vec3[] = [ [-x, 0, -z], [x, 0, -z], [x, 0, z], [-x, 0, z], [-x, height, -z], [x, height, -z], [x, height, z], [-x, height, z], ]; const faces: [number, number, number][] = [ [0, 1, 2], [0, 2, 3], [4, 6, 5], [4, 7, 6], [0, 5, 1], [0, 4, 5], [1, 6, 2], [1, 5, 6], [2, 7, 3], [2, 6, 7], [3, 4, 0], [3, 7, 4], ]; return faces.flatMap((face) => face.flatMap((index) => vertices[index])); } function automationSolidBox(overrides: Partial = {}) { return sceneShape({ id: "solid-cube", name: "Solid cube", kind: "box", color: "#d41721", x: 0, z: 0, width: 28, depth: 28, height: 28, ...overrides, }); } function automationHoleBox(overrides: Partial = {}) { return sceneShape({ id: "hole-cube", name: "Hole cube", kind: "box", color: "#b8c2cc", hole: true, x: 0, z: 0, elevation: -4, width: 13, depth: 40, height: 36, ...overrides, }); } function automationImportedStlBox(overrides: Partial = {}) { const width = overrides.width ?? 28; const depth = overrides.depth ?? 28; const height = overrides.height ?? 28; return sceneShape({ id: "imported-stl-cube", name: "Imported STL cube", kind: "mesh", color: "#0098c7", x: 0, z: 0, width, depth, height, importedMesh: { positions: stlBoxTrianglePositions(width, depth, height), baseWidth: width, baseDepth: depth, baseHeight: height, triangleCount: 12, sourceFormat: "stl", }, ...overrides, }); } function automationHoleStlBox(overrides: Partial = {}) { return automationImportedStlBox({ id: "hole-stl", name: "Hole STL", color: "#b8c2cc", hole: true, elevation: -4, width: 13, depth: 40, height: 38, ...overrides, }); } function automationImportedStlFromShapes(id: string, name: string, color: string, parts: WorkplaneShape[], overrides: Partial = {}) { const vertices: Vec3[] = []; const faces: [number, number, number][] = []; parts.forEach((part) => appendMeshData(vertices, faces, meshForShape(part))); const bounds = boundsForCuboids([{ minX: 0, maxX: 0, minY: 0, maxY: 0, minZ: 0, maxZ: 0 }, ...parts.map(meshAabb)]); const centerX = (bounds.minX + bounds.maxX) / 2; const centerZ = (bounds.minZ + bounds.maxZ) / 2; const width = Math.max(1, bounds.maxX - bounds.minX); const depth = Math.max(1, bounds.maxZ - bounds.minZ); const height = Math.max(1, bounds.maxY - bounds.minY); const positions: number[] = []; faces.forEach(([ai, bi, ci]) => { [vertices[ai], vertices[bi], vertices[ci]].forEach(([x, y, z]) => { positions.push(x - centerX, y - bounds.minY, z - centerZ); }); }); return sceneShape({ id, name, kind: "mesh", color, x: centerX, z: centerZ, elevation: bounds.minY, width, depth, height, importedMesh: { positions, baseWidth: width, baseDepth: depth, baseHeight: height, triangleCount: faces.length, sourceFormat: "stl", }, ...overrides, }); } function automationRaspberryPiStl(overrides: Partial = {}) { const parts: WorkplaneShape[] = [ sceneShape({ id: "raspi-board", name: "Board", kind: "box", color: "#1f9f5f", width: 70, depth: 48, height: 3, elevation: 0 }), sceneShape({ id: "raspi-soc", name: "Main chip", kind: "box", color: "#30343b", x: -8, z: 0, width: 15, depth: 15, height: 3.2, elevation: 3 }), sceneShape({ id: "raspi-memory", name: "Memory chip", kind: "box", color: "#2b2e34", x: 10, z: 1, width: 11, depth: 13, height: 2.8, elevation: 3 }), sceneShape({ id: "raspi-usb-a", name: "USB block", kind: "box", color: "#b9c1c9", x: 23, z: -13, width: 17, depth: 11, height: 9, elevation: 3 }), sceneShape({ id: "raspi-usb-b", name: "USB block", kind: "box", color: "#b9c1c9", x: 23, z: 4, width: 17, depth: 11, height: 9, elevation: 3 }), sceneShape({ id: "raspi-ethernet", name: "Ethernet jack", kind: "box", color: "#c4c9ce", x: 23, z: 18, width: 18, depth: 13, height: 11, elevation: 3 }), sceneShape({ id: "raspi-hdmi", name: "HDMI", kind: "box", color: "#c9c0b2", x: -15, z: -22, width: 16, depth: 5, height: 4, elevation: 3 }), sceneShape({ id: "raspi-camera", name: "Camera connector", kind: "box", color: "#2b2e34", x: -28, z: 4, width: 5, depth: 20, height: 3, elevation: 3 }), sceneShape({ id: "raspi-mount-a", name: "Mount", kind: "cylinder", color: "#1f9f5f", x: -29, z: -17, width: 6, depth: 6, height: 3.4, elevation: 0, sides: 32 }), sceneShape({ id: "raspi-mount-b", name: "Mount", kind: "cylinder", color: "#1f9f5f", x: 29, z: -17, width: 6, depth: 6, height: 3.4, elevation: 0, sides: 32 }), sceneShape({ id: "raspi-mount-c", name: "Mount", kind: "cylinder", color: "#1f9f5f", x: -29, z: 17, width: 6, depth: 6, height: 3.4, elevation: 0, sides: 32 }), sceneShape({ id: "raspi-mount-d", name: "Mount", kind: "cylinder", color: "#1f9f5f", x: 29, z: 17, width: 6, depth: 6, height: 3.4, elevation: 0, sides: 32 }), ...Array.from({ length: 14 }, (_, index) => sceneShape({ id: `raspi-pin-${index}`, name: "GPIO pin", kind: "box", color: "#e2b94f", x: -29 + index * 4, z: 23, width: 1.6, depth: 2.6, height: 6, elevation: 3, }), ), ]; return automationImportedStlFromShapes("raspberry-pi-stl", "Raspberry Pi-like STL", "#0098c7", parts, overrides); } const booleanAutomationShapeConfigs: Record< string, { name: string; kind: WorkplaneShape["kind"]; color: string; width?: number; depth?: number; height?: number; props?: Partial; } > = { cube: { name: "Cube", kind: "box", color: "#d41721" }, cylinder: { name: "Cylinder", kind: "cylinder", color: "#d97813", props: { sides: 96, segments: 1 } }, sphere: { name: "Sphere", kind: "sphere", color: "#0098c7", props: { steps: 28, sides: 56 } }, cone: { name: "Cone", kind: "cone", color: "#6e2786", props: { sides: 96, topRadius: 0, baseRadius: 14 } }, pyramid: { name: "Pyramid", kind: "pyramid", color: "#f2cf10", props: { sides: 4 } }, wedge: { name: "Wedge", kind: "wedge", color: "#33983d" }, text: { name: "Text", kind: "text", color: "#cf101b", width: 34, depth: 18, height: 28, props: { text: "T", font: "Sans" } }, "round-roof": { name: "Round Roof", kind: "roundRoof", color: "#67c4ce", props: { sides: 64 } }, "half-sphere": { name: "Half Sphere", kind: "halfSphere", color: "#c9009a", props: { steps: 32 } }, torus: { name: "Torus", kind: "torus", color: "#0098c7", width: 34, depth: 34, height: 8, props: { sides: 96 } }, tube: { name: "Tube", kind: "tube", color: "#ce7013", width: 34, depth: 34, height: 28, props: { bevel: 6, sides: 96 } }, }; function automationShape(key: string, overrides: Partial = {}) { const config = booleanAutomationShapeConfigs[key]; if (!config) { return null; } const width = overrides.width ?? config.width ?? 28; const depth = overrides.depth ?? config.depth ?? 28; const height = overrides.height ?? config.height ?? 28; return sceneShape({ id: `${overrides.hole ? "hole" : "solid"}-${key}`, name: config.name, kind: config.kind, color: config.color, x: 0, z: 0, width, depth, height, size: Math.max(width, depth), ...config.props, ...overrides, }); } function automationHoleShape(key: string, overrides: Partial = {}) { const shape = automationShape(key, { hole: true, color: "#b8c2cc", elevation: key === "torus" ? 18 : -3, rotation: 27, width: key === "text" ? 32 : key === "torus" || key === "tube" ? 34 : 24, depth: key === "text" ? 17 : key === "torus" || key === "tube" ? 34 : 24, height: key === "torus" ? 12 : 34, ...overrides, }); return shape ? withHoleMode(shape, true) : null; } function automationNormalGroupedObject(overrides: Partial = {}) { const cube = automationShape("cube", { id: "normal-group-cube", x: -9, width: 18, depth: 24, height: 26 }); const cylinder = automationShape("cylinder", { id: "normal-group-cylinder", x: 10, width: 20, depth: 20, height: 28 }); if (!cube || !cylinder) { return null; } const group = groupedShape([cube, cylinder]); return group ? { ...group, id: "normal-group", name: "Normal grouped object", ...overrides } : null; } function automationSelectionOutlineRegressionShape() { const geometries: THREE.BufferGeometry[] = [ new RoundedBoxGeometry(30, 20, 20, 8, 4).translate(-15, 10, 0), new THREE.BoxGeometry(16, 20, 20).translate(18, 10, 0), ]; const positions: number[] = []; const normals: number[] = []; geometries.forEach((geometry) => { const nonIndexed = geometry.index ? geometry.toNonIndexed() : geometry; nonIndexed.computeVertexNormals(); positions.push(...Array.from(nonIndexed.getAttribute("position").array as ArrayLike)); normals.push(...Array.from(nonIndexed.getAttribute("normal").array as ArrayLike)); if (nonIndexed !== geometry) { nonIndexed.dispose(); } geometry.dispose(); }); return canonicalizeShape( sceneShape({ id: "selection-outline-regression", name: "Selection outline regression", kind: "mesh", color: "#d41721", x: 0, z: 0, width: 56, depth: 20, height: 20, size: 56, importedMesh: { positions, normals, baseWidth: 56, baseDepth: 20, baseHeight: 20, triangleCount: Math.floor(positions.length / 9), sourceFormat: "json", }, groupedShapes: [ sceneShape({ id: "rounded-child", name: "Rounded child", kind: "box", color: "#d41721", x: -15, width: 30, depth: 20, height: 20, radius: 4 }), sceneShape({ id: "box-child", name: "Box child", kind: "box", color: "#d41721", x: 18, width: 16, depth: 20, height: 20 }), ], }), ); } function booleanAutomationDynamicScene(caseId: string): { label: string; shapes: WorkplaneShape[] } | null { const requestedKeys = Object.keys(booleanAutomationShapeConfigs).filter((key) => key !== "cube" && key !== "cylinder"); const allNormalKeys = Object.keys(booleanAutomationShapeConfigs); for (const key of requestedKeys) { if (caseId === `${key}-rot-hole`) { const solid = automationShape(key); return solid ? { label: `${solid.name} + rotated hole cube`, shapes: [solid, automationHoleBox({ rotation: 32 })], } : null; } if (caseId === `${key}-hole-cube`) { const hole = automationHoleShape(key); return hole ? { label: `${hole.name} hole + solid cube`, shapes: [automationSolidBox({ width: 36, depth: 36, height: 30 }), hole], } : null; } if (caseId === `${key}-hole-stl`) { const hole = automationHoleShape(key); return hole ? { label: `${hole.name} hole + imported STL`, shapes: [automationImportedStlBox({ width: 36, depth: 36, height: 30 }), hole], } : null; } } for (const key of allNormalKeys) { if (caseId === `hole-stl-${key}`) { const solid = automationShape(key, { width: key === "text" ? 42 : undefined, depth: key === "text" ? 20 : undefined }); return solid ? { label: `rotated hole STL + ${solid.name}`, shapes: [ solid, automationHoleStlBox({ id: `hole-stl-${key}`, rotation: 29, rotationZ: 8, }), ], } : null; } if (caseId === `straight-hole-stl-${key}`) { const solid = automationShape(key, { width: key === "text" ? 42 : undefined, depth: key === "text" ? 20 : undefined }); return solid ? { label: `non-rotated hole STL + ${solid.name}`, shapes: [ solid, automationHoleStlBox({ id: `straight-hole-stl-${key}`, rotation: 0, rotationZ: 0, }), ], } : null; } } return null; } function booleanAutomationScene(caseId: string): { label: string; shapes: WorkplaneShape[] } | null { const rotatedHole = () => automationHoleBox({ rotation: 32 }); if (caseId === "selection-outline-regression") { return { label: "segmented rounded mesh selection outline", shapes: [automationSelectionOutlineRegressionShape()], }; } if (caseId === "locked-align-pair") { return { label: "locked alignment reference pair", shapes: [ sceneShape({ id: "locked-anchor", name: "Locked cube", kind: "box", color: "#d41721", x: 24, z: 10, width: 20, depth: 20, height: 20, locked: true }), sceneShape({ id: "moving-cube", name: "Moving cube", kind: "box", color: "#ef7f1a", x: -24, z: -18, width: 12, depth: 12, height: 12 }), ], }; } if (caseId === "normal-group") { const group = groupedShape([ sceneShape({ id: "modifier-base", name: "Base", kind: "box", color: "#d41721", width: 54, depth: 38, height: 7 }), sceneShape({ id: "modifier-upright", name: "Upright", kind: "box", color: "#d41721", x: 8, width: 14, depth: 14, height: 40, elevation: 4 }), sceneShape({ id: "modifier-rail", name: "Rail", kind: "box", color: "#d41721", x: -7, z: 5, width: 32, depth: 10, height: 13, elevation: 4 }), ]); return group ? { label: "overlapping normal solid group", shapes: [group] } : null; } if (caseId === "straight-hole-stl-group") { const group = automationNormalGroupedObject(); return group ? { label: "normal grouped object + non-rotated hole STL", shapes: [group, automationHoleStlBox({ id: "straight-hole-stl-group", width: 24, depth: 42 })], } : null; } if (caseId === "straight-hole-stl-mixed-group") { const group = automationNormalGroupedObject({ x: 12 }); const cube = automationShape("cube", { id: "mixed-solid-cube", x: -14, width: 24, depth: 26, height: 28 }); return group && cube ? { label: "cube + normal grouped object + non-rotated hole STL", shapes: [cube, group, automationHoleStlBox({ id: "straight-hole-stl-mixed-group", width: 48, depth: 42 })], } : null; } if (caseId === "raspi-stl-hole") { return { label: "Raspberry Pi-like STL + non-rotated hole cube", shapes: [ automationRaspberryPiStl(), automationHoleBox({ id: "raspi-hole-cube", x: -2, z: 2, width: 18, depth: 56, height: 20, elevation: -2, rotation: 0, rotationZ: 0 }), ], }; } if (caseId === "raspi-stl-rot-hole") { return { label: "Raspberry Pi-like STL + rotated hole cube", shapes: [ automationRaspberryPiStl(), automationHoleBox({ id: "raspi-rot-hole-cube", x: -2, z: 2, width: 18, depth: 56, height: 20, elevation: -2, rotation: 28, rotationZ: 8 }), ], }; } if (caseId === "raspi-stl-hole-stl") { return { label: "Raspberry Pi-like STL + non-rotated hole STL", shapes: [ automationRaspberryPiStl(), automationHoleStlBox({ id: "raspi-hole-stl", x: -2, z: 2, width: 18, depth: 56, height: 20, elevation: -2, rotation: 0, rotationZ: 0 }), ], }; } if (caseId === "raspi-stl-rot-hole-stl") { return { label: "Raspberry Pi-like STL + rotated hole STL", shapes: [ automationRaspberryPiStl(), automationHoleStlBox({ id: "raspi-rot-hole-stl", x: -2, z: 2, width: 18, depth: 56, height: 20, elevation: -2, rotation: 28, rotationZ: 8 }), ], }; } const cases: Record = { "cube-hole": { label: "solid cube + non-rotated hole cube", shapes: [automationSolidBox(), automationHoleBox()], }, "cube-rot-hole": { label: "solid cube + rotated hole cube", shapes: [automationSolidBox(), rotatedHole()], }, "stl-hole": { label: "STL + non-rotated hole cube", shapes: [automationImportedStlBox(), automationHoleBox()], }, "stl-rot-hole": { label: "STL + rotated hole cube", shapes: [automationImportedStlBox(), rotatedHole()], }, "rot-stl-hole": { label: "rotated STL + hole cube", shapes: [automationImportedStlBox({ rotation: 28, rotationZ: 8 }), automationHoleBox()], }, "rot-stl-rot-hole": { label: "rotated STL + rotated hole cube", shapes: [automationImportedStlBox({ rotation: 28, rotationZ: 8 }), rotatedHole()], }, "cylinder-rot-hole": { label: "cylinder + rotated hole cube", shapes: [automationSolidBox({ id: "solid-cylinder", name: "Cylinder", kind: "cylinder", color: "#d97813", sides: 48 }), rotatedHole()], }, "sphere-rot-hole": { label: "sphere + rotated hole cube", shapes: [automationSolidBox({ id: "solid-sphere", name: "Sphere", kind: "sphere", color: "#0098c7", sides: 48 }), rotatedHole()], }, "cone-rot-hole": { label: "cone + rotated hole cube", shapes: [ automationSolidBox({ id: "solid-cone", name: "Cone", kind: "cone", color: "#6e2786", sides: 64, topRadius: 0, baseRadius: 14 }), rotatedHole(), ], }, "pyramid-rot-hole": { label: "pyramid + rotated hole cube", shapes: [automationSolidBox({ id: "solid-pyramid", name: "Pyramid", kind: "pyramid", color: "#f2cf10", sides: 4 }), rotatedHole()], }, }; return cases[caseId] ?? booleanAutomationDynamicScene(caseId); } function makeHouseScene(): WorkplaneShape[] { return [ sceneShape({ name: "Grass base", kind: "box", color: "#4f9b58", x: 0, z: 0, width: 118, depth: 92, height: 1 }), sceneShape({ name: "House body", kind: "box", color: "#e7c49a", x: 0, z: 2, width: 52, depth: 42, height: 34, elevation: 1 }), sceneShape({ name: "Gable roof", kind: "roof", color: "#a83c32", x: 0, z: 2, width: 66, depth: 54, height: 23, elevation: 35 }), sceneShape({ name: "Chimney", kind: "box", color: "#7f3328", x: 17, z: -9, width: 8, depth: 8, height: 18, elevation: 45 }), sceneShape({ name: "Front door", kind: "box", color: "#6d4427", x: 0, z: -20.4, width: 12, depth: 1.4, height: 19, elevation: 1.5 }), sceneShape({ name: "Door knob", kind: "sphere", color: "#e0b23f", x: 4.2, z: -21.6, width: 2.2, depth: 2.2, height: 2.2, elevation: 11 }), sceneShape({ name: "Left front window", kind: "box", color: "#6fc8e8", x: -16, z: -20.7, width: 10, depth: 1.2, height: 8, elevation: 18 }), sceneShape({ name: "Right front window", kind: "box", color: "#6fc8e8", x: 16, z: -20.7, width: 10, depth: 1.2, height: 8, elevation: 18 }), sceneShape({ name: "Left side window", kind: "box", color: "#6fc8e8", x: -26.2, z: 8, width: 10, depth: 1.2, height: 8, elevation: 18, rotation: 90 }), sceneShape({ name: "Right side window", kind: "box", color: "#6fc8e8", x: 26.2, z: 8, width: 10, depth: 1.2, height: 8, elevation: 18, rotation: 90 }), sceneShape({ name: "Porch step", kind: "box", color: "#9d9b91", x: 0, z: -28, width: 24, depth: 10, height: 2, elevation: 1 }), sceneShape({ name: "Walkway", kind: "box", color: "#b8b4a8", x: 0, z: -50, width: 12, depth: 36, height: 0.8, elevation: 0.2 }), sceneShape({ name: "Tree trunk", kind: "cylinder", color: "#7b4a2b", x: -42, z: 22, width: 7, depth: 7, height: 18, elevation: 1, sides: 18 }), sceneShape({ name: "Tree crown", kind: "sphere", color: "#2f8e45", x: -42, z: 22, width: 24, depth: 24, height: 22, elevation: 18 }), sceneShape({ name: "Mailbox post", kind: "box", color: "#5a4b3d", x: 32, z: -42, width: 3, depth: 3, height: 12, elevation: 1 }), sceneShape({ name: "Mailbox", kind: "roundRoof", color: "#2e6ca8", x: 32, z: -42, width: 13, depth: 8, height: 7, elevation: 13, rotation: 90 }), ]; } function makeBlockPerfScene(count = 500): WorkplaneShape[] { const safeCount = Math.max(1, Math.min(5000, Math.floor(count))); const columns = Math.ceil(Math.sqrt(safeCount)); const spacing = 7; const offset = ((columns - 1) * spacing) / 2; const colors = ["#d41721", "#d97813", "#f2cf10", "#33983d", "#0098c7", "#294c93"]; return Array.from({ length: safeCount }, (_, index) => { const column = index % columns; const row = Math.floor(index / columns); return sceneShape({ id: `perf-block-${index + 1}`, name: `Perf block ${index + 1}`, kind: "box", color: colors[index % colors.length], x: column * spacing - offset, z: row * spacing - offset, width: 5, depth: 5, height: 5, }); }); } function withHoleMode(shape: WorkplaneShape, hole: boolean, parentColor?: string): WorkplaneShape { const color = hole ? "#b8c2cc" : (parentColor ?? fallbackSolidColor(shape)); return { ...shape, hole, color, groupedShapes: shape.groupedShapes?.map((child) => withHoleMode(child, hole)), }; } function sanitizeName(name: string) { return name.replace(/[^a-z0-9_-]+/gi, "_") || "shape"; } function meshDataToCadTransfer(mesh: MeshData) { const positions = new Float32Array(mesh.vertices.length * 3); mesh.vertices.forEach((vertex, index) => positions.set(vertex, index * 3)); const indices = new Uint32Array(mesh.faces.length * 3); mesh.faces.forEach((face, index) => indices.set(face, index * 3)); return { positions, indices }; } function shapeFromCadMesh( source: WorkplaneShape, positions: Float32Array, normals: Float32Array, indices: Uint32Array, brep: string, ): WorkplaneShape | null { if (positions.length < 9 || indices.length < 3) return null; let minX = Number.POSITIVE_INFINITY; let minY = Number.POSITIVE_INFINITY; let minZ = Number.POSITIVE_INFINITY; let maxX = Number.NEGATIVE_INFINITY; let maxY = Number.NEGATIVE_INFINITY; let maxZ = Number.NEGATIVE_INFINITY; for (let index = 0; index < positions.length; index += 3) { minX = Math.min(minX, positions[index]); minY = Math.min(minY, positions[index + 1]); minZ = Math.min(minZ, positions[index + 2]); maxX = Math.max(maxX, positions[index]); maxY = Math.max(maxY, positions[index + 1]); maxZ = Math.max(maxZ, positions[index + 2]); } if (![minX, minY, minZ, maxX, maxY, maxZ].every(Number.isFinite)) return null; const centerX = (minX + maxX) / 2; const centerZ = (minZ + maxZ) / 2; const rawWidth = Math.max(MIN_SHAPE_DIMENSION, maxX - minX); const rawHeight = Math.max(MIN_SHAPE_DIMENSION, maxY - minY); const rawDepth = Math.max(MIN_SHAPE_DIMENSION, maxZ - minZ); const flattenedPositions: number[] = []; const flattenedNormals: number[] = []; for (let index = 0; index < indices.length; index += 1) { const vertex = indices[index] * 3; flattenedPositions.push(positions[vertex] - centerX, positions[vertex + 1] - minY, positions[vertex + 2] - centerZ); if (normals.length >= vertex + 3) flattenedNormals.push(normals[vertex], normals[vertex + 1], normals[vertex + 2]); } const width = cleanModelDimension(rawWidth); const height = cleanModelDimension(rawHeight); const depth = cleanModelDimension(rawDepth); return canonicalizeShape({ ...source, kind: "mesh", x: cleanNearZero(centerX, 0.0005), z: cleanNearZero(centerZ, 0.0005), elevation: cleanNearZero(minY, 0.0005), width, depth, height, size: Math.max(width, depth), rotation: 0, rotationX: 0, rotationZ: 0, mirrorX: undefined, mirrorY: undefined, mirrorZ: undefined, radius: undefined, importedMesh: { positions: flattenedPositions, normals: flattenedNormals.length === flattenedPositions.length ? flattenedNormals : undefined, baseWidth: rawWidth, baseDepth: rawDepth, baseHeight: rawHeight, triangleCount: Math.floor(indices.length / 3), sourceFormat: "json", }, imagePlate: undefined, cadBrep: brep, cadBrepFrame: { x: cleanNearZero(centerX, 0.0005), z: cleanNearZero(centerZ, 0.0005), elevation: cleanNearZero(minY, 0.0005), width, depth, height, }, cadPrimitiveFrame: undefined, }); } function cadEdgeEndpoint(edge: CadModifierEdge, end: "start" | "end") { const offset = end === "start" ? 0 : edge.points.length - 3; return new THREE.Vector3(edge.points[offset], edge.points[offset + 1], edge.points[offset + 2]); } function cadEdgeTangentAt(edge: CadModifierEdge, endpoint: THREE.Vector3) { const start = cadEdgeEndpoint(edge, "start"); const end = cadEdgeEndpoint(edge, "end"); if (endpoint.distanceToSquared(start) <= endpoint.distanceToSquared(end)) { const next = new THREE.Vector3(edge.points[3], edge.points[4], edge.points[5]); return next.sub(start).normalize(); } const offset = Math.max(0, edge.points.length - 6); const previous = new THREE.Vector3(edge.points[offset], edge.points[offset + 1], edge.points[offset + 2]); return previous.sub(end).normalize(); } function tangentCadEdgeChain(edges: CadModifierEdge[], startId: number, allowedIds: Set) { const edgeById = new Map(edges.map((edge) => [edge.id, edge])); const selected = new Set([startId]); const queue = [startId]; const tolerance = 0.025; while (queue.length > 0) { const id = queue.shift() as number; const edge = edgeById.get(id); if (!edge) continue; const endpoints = [cadEdgeEndpoint(edge, "start"), cadEdgeEndpoint(edge, "end")]; edges.forEach((candidate) => { if (selected.has(candidate.id) || !allowedIds.has(candidate.id)) return; const candidateEndpoints = [cadEdgeEndpoint(candidate, "start"), cadEdgeEndpoint(candidate, "end")]; const shared = endpoints.find((point) => candidateEndpoints.some((other) => point.distanceTo(other) <= tolerance)); if (!shared) return; const a = cadEdgeTangentAt(edge, shared); const b = cadEdgeTangentAt(candidate, shared); const deviation = (Math.acos(Math.max(-1, Math.min(1, Math.abs(a.dot(b))))) * 180) / Math.PI; if (deviation <= 16) { selected.add(candidate.id); queue.push(candidate.id); } }); } return [...selected]; } function selectableCadModifierEdge(edge: CadModifierEdge, sharpAngle: number) { return edge.selectable && edge.manifold && !edge.boundary && edge.angle + 1e-3 >= sharpAngle; } function cadDisplayEdgesAfterTreatment(shape: WorkplaneShape, session: EdgeModifierSession) { const removed = new Set(session.selectedEdgeIds); const elevation = shape.elevation ?? 0; return session.edges .filter((edge) => { const effectiveAngle = Math.min(edge.angle, 180 - edge.angle); return edge.manifold && !edge.boundary && effectiveAngle + 1e-3 >= Math.max(session.sharpAngle, NORMAL_SELECTION_CAD_EDGE_MIN_ANGLE) && !removed.has(edge.id); }) .map((edge) => ({ points: edge.points.map((value, index) => { if (index % 3 === 0) return value - shape.x; if (index % 3 === 1) return value - elevation; return value - shape.z; }), })); } function cadDisplayEdgesForShape(shape: WorkplaneShape, edges: CadModifierDisplayEdge[]) { const elevation = shape.elevation ?? 0; return edges .filter((edge) => edge.points.length >= 6) .map((edge) => ({ points: edge.points.map((value, index) => { if (index % 3 === 0) return value - shape.x; if (index % 3 === 1) return value - elevation; return value - shape.z; }), })); } function cadModifierComponentPreviews(sourceParts: WorkplaneShape[], components: CadModifierComponentMesh[] | undefined): EdgeModifierComponentPreview[] { if (!components?.length) return []; const previews: EdgeModifierComponentPreview[] = []; components.forEach((component) => { const source = sourceParts[component.owner] ?? sourceParts[0]; if (!source) return; const shape = shapeFromCadMesh(source, component.positions, component.normals, component.indices, component.brep); if (!shape) return; previews.push({ owner: component.owner, shape: canonicalizeShape({ ...shape, cadDisplayEdges: cadDisplayEdgesForShape(shape, component.displayEdges), cadDisplayEdgesVersion: 2 as const, }), }); }); return previews; } function cloneWorkplaneShapeSnapshot(shape: WorkplaneShape): WorkplaneShape { return JSON.parse(JSON.stringify(canonicalizeShape(shape))) as WorkplaneShape; } function edgeTreatmentLabel(feature: NonNullable[number]) { const size = `${Number(feature.amount.toFixed(2))} mm`; return `${feature.kind === "fillet" ? "fillet" : "chamfer"} (${size}, ${feature.edgeCount} edge${feature.edgeCount === 1 ? "" : "s"})`; } function shapeWithEdgeTreatmentRecord( shape: WorkplaneShape, before: WorkplaneShape, feature: NonNullable[number], preserveEdgeSize: boolean, createdAt: number, ) { return canonicalizeShape({ ...shape, edgeResizeMode: preserveEdgeSize ? "preserve" : "scale", edgeTreatments: [ ...(before.edgeTreatments ?? []), { ...feature, }, ], edgeTreatmentHistory: [ ...(before.edgeTreatmentHistory ?? []), { id: createLocalId("edge-history"), createdAt, feature, before: cloneWorkplaneShapeSnapshot(before), }, ], }); } function shapeCenterDistance(a: WorkplaneShape, b: WorkplaneShape) { const ax = a.x; const ay = (a.elevation ?? 0) + a.height / 2; const az = a.z; const bx = b.x; const by = (b.elevation ?? 0) + b.height / 2; const bz = b.z; return Math.hypot(ax - bx, ay - by, az - bz); } function shapeDimensionDistance(a: WorkplaneShape, b: WorkplaneShape) { return Math.hypot(shapeWidth(a) - shapeWidth(b), a.height - b.height, shapeDepth(a) - shapeDepth(b)); } function matchCadComponentsToSources(sourceParts: WorkplaneShape[], componentPreviews: EdgeModifierComponentPreview[]) { const candidates = componentPreviews.flatMap((component, componentIndex) => sourceParts.map((source, sourceIndex) => ({ component, componentIndex, sourceIndex, score: shapeCenterDistance(component.shape, source) + shapeDimensionDistance(component.shape, source) * 0.25, })), ); candidates.sort((a, b) => a.score - b.score); const usedComponents = new Set(); const usedSources = new Set(); const ownerToSourceIndex = new Map(); candidates.forEach((candidate) => { if (usedComponents.has(candidate.componentIndex) || usedSources.has(candidate.sourceIndex)) return; usedComponents.add(candidate.componentIndex); usedSources.add(candidate.sourceIndex); ownerToSourceIndex.set(candidate.component.owner, candidate.sourceIndex); }); return ownerToSourceIndex; } function groupedShapeWithComponentEdgeTreatment( base: WorkplaneShape, preview: WorkplaneShape, sourceParts: WorkplaneShape[], session: EdgeModifierSession, feature: NonNullable[number], createdAt: number, ) { if (!base.groupedShapes?.length || sourceParts.length < 2 || session.componentPreviews.length === 0) { return null; } const edgeById = new Map(session.edges.map((edge) => [edge.id, edge])); const ownerEdgeCounts = new Map(); session.selectedEdgeIds.forEach((edgeId) => { const owner = edgeById.get(edgeId)?.owner; if (typeof owner !== "number") return; ownerEdgeCounts.set(owner, (ownerEdgeCounts.get(owner) ?? 0) + 1); }); if (ownerEdgeCounts.size === 0) { return null; } const ownerToSourceIndex = matchCadComponentsToSources(sourceParts, session.componentPreviews); const componentByOwner = new Map(session.componentPreviews.map((component) => [component.owner, component])); const updatedSources = [...sourceParts]; let changed = false; ownerEdgeCounts.forEach((edgeCount, owner) => { const sourceIndex = ownerToSourceIndex.get(owner); const component = componentByOwner.get(owner); if (sourceIndex === undefined || !component) return; const source = sourceParts[sourceIndex]; const ownerFeature = { ...feature, edgeCount }; const retargeted = canonicalizeShape({ ...component.shape, id: source.id, name: source.name, color: source.color, hole: source.hole || undefined, locked: source.locked, hidden: source.hidden, groupedShapes: source.groupedShapes, groupedBaseWidth: source.groupedBaseWidth, groupedBaseDepth: source.groupedBaseDepth, groupedBaseHeight: source.groupedBaseHeight, }); updatedSources[sourceIndex] = shapeWithEdgeTreatmentRecord(retargeted, source, ownerFeature, session.preserveEdgeSize, createdAt); changed = true; }); if (!changed) { return null; } const elevation = preview.elevation ?? 0; return canonicalizeShape({ ...preview, edgeResizeMode: session.preserveEdgeSize ? "preserve" : "scale", edgeTreatments: base.edgeTreatments, edgeTreatmentHistory: base.edgeTreatmentHistory, groupedBaseWidth: shapeWidth(preview), groupedBaseDepth: shapeDepth(preview), groupedBaseHeight: preview.height, groupedShapes: updatedSources.map((shape) => cloneAsGroupChild(shape, preview.x, preview.z, elevation)), }); } function edgeTreatmentFeatureCount(shape: WorkplaneShape): number { return (shape.edgeTreatments?.length ?? 0) + (shape.groupedShapes?.reduce((total, child) => total + edgeTreatmentFeatureCount(child), 0) ?? 0); } function reversibleEdgeTreatmentCount(shape: WorkplaneShape): number { return (shape.edgeTreatmentHistory?.length ?? 0) + (shape.groupedShapes?.reduce((total, child) => total + reversibleEdgeTreatmentCount(child), 0) ?? 0); } function edgeTreatmentHistoryOptions(shape: WorkplaneShape, path: number[] = [], targetName = shape.name): EdgeFeatureRevertOption[] { const ownHistory = shape.edgeTreatmentHistory ?? []; const ownOptions = ownHistory.map((entry, index) => ({ id: `${path.length ? path.join(".") : "root"}:${entry.id}`, entryId: entry.id, path, label: edgeTreatmentLabel(entry.feature), targetName, createdAt: entry.createdAt, removesNewerCount: Math.max(0, ownHistory.length - index - 1), })); const childOptions = (shape.groupedShapes ?? []).flatMap((child, index) => edgeTreatmentHistoryOptions(child, [...path, index], `${targetName} / ${child.name}`), ); return [...ownOptions, ...childOptions].sort((a, b) => b.createdAt - a.createdAt); } function restoreOwnLastEdgeTreatment(shape: WorkplaneShape, entry: NonNullable[number]) { const before = cloneWorkplaneShapeSnapshot(entry.before); return canonicalizeShape({ ...before, id: shape.id, name: shape.name, color: shape.color, hole: shape.hole || undefined, x: shape.x, z: shape.z, elevation: shape.elevation, width: shapeWidth(shape), depth: shapeDepth(shape), height: shape.height, size: shape.size, rotation: shape.rotation, rotationX: shape.rotationX, rotationZ: shape.rotationZ, mirrorX: shape.mirrorX, mirrorY: shape.mirrorY, mirrorZ: shape.mirrorZ, locked: shape.locked, hidden: shape.hidden, }); } async function restoreEdgeTreatmentInShape(shape: WorkplaneShape, path: number[], entryId: string): Promise<{ shape: WorkplaneShape; label: string } | null> { if (path.length === 0) { const entry = (shape.edgeTreatmentHistory ?? []).find((candidate) => candidate.id === entryId); return entry ? { shape: restoreOwnLastEdgeTreatment(shape, entry), label: edgeTreatmentLabel(entry.feature) } : null; } if (!shape.groupedShapes?.length) { return null; } const [childIndex, ...restPath] = path; const restoredChildren = restoreGroupedChildren(shape); const child = restoredChildren[childIndex]; if (!child) { return null; } const restoredChild = await restoreEdgeTreatmentInShape(child, restPath, entryId); if (!restoredChild) { return null; } restoredChildren[childIndex] = restoredChild.shape; const rebuilt = await buildGroupedShapeFromSelection(restoredChildren); if (!rebuilt.group) { return null; } return { shape: canonicalizeShape({ ...rebuilt.group, id: shape.id, name: shape.name, hole: shape.hole || rebuilt.group.hole, locked: shape.locked, hidden: shape.hidden, }), label: restoredChild.label, }; } function transformMesh(mesh: MeshData, shape: WorkplaneShape): MeshData { const centerY = shape.height / 2; const matrix = new THREE.Matrix4().makeRotationFromEuler( new THREE.Euler( THREE.MathUtils.degToRad(shape.rotationX ?? 0), THREE.MathUtils.degToRad(meshYawDegrees(shape)), THREE.MathUtils.degToRad(shape.rotationZ ?? 0), "XYZ", ), ); const mirrorX = mirrorSign(shape.mirrorX); const mirrorY = mirrorSign(shape.mirrorY); const mirrorZ = mirrorSign(shape.mirrorZ); const reversedWinding = mirroredAxisCount(shape) % 2 === 1; return { ...mesh, vertices: mesh.vertices.map(([x, y, z]) => { const vertex = new THREE.Vector3(x * mirrorX, (y - centerY) * mirrorY, z * mirrorZ).applyMatrix4(matrix); return [vertex.x + shape.x, vertex.y + (shape.elevation ?? 0) + centerY, vertex.z + shape.z] as Vec3; }), faces: reversedWinding ? mesh.faces.map(([a, b, c]) => [a, c, b] as [number, number, number]) : mesh.faces, }; } function boxMesh(shape: WorkplaneShape): MeshData { const width = shapeWidth(shape); const depth = shapeDepth(shape); const height = shape.height; const x = width / 2; const z = depth / 2; return { name: sanitizeName(shape.name), vertices: [ [-x, 0, -z], [x, 0, -z], [x, 0, z], [-x, 0, z], [-x, height, -z], [x, height, -z], [x, height, z], [-x, height, z], ], faces: [ [0, 2, 1], [0, 3, 2], [4, 5, 6], [4, 6, 7], [0, 1, 5], [0, 5, 4], [1, 2, 6], [1, 6, 5], [2, 3, 7], [2, 7, 6], [3, 0, 4], [3, 4, 7], ], }; } function cylinderMesh(shape: WorkplaneShape, sides = 96, topRadiusScale = 1): MeshData { const width = shapeWidth(shape); const depth = shapeDepth(shape); const height = shape.height; const vertices: Vec3[] = [[0, 0, 0], [0, height, 0]]; for (let i = 0; i < sides; i += 1) { const angle = (i / sides) * Math.PI * 2; vertices.push([(Math.cos(angle) * width) / 2, 0, (Math.sin(angle) * depth) / 2]); vertices.push([(Math.cos(angle) * width * topRadiusScale) / 2, height, (Math.sin(angle) * depth * topRadiusScale) / 2]); } const faces: [number, number, number][] = []; for (let i = 0; i < sides; i += 1) { const next = (i + 1) % sides; const b0 = 2 + i * 2; const t0 = b0 + 1; const b1 = 2 + next * 2; const t1 = b1 + 1; faces.push([0, b1, b0]); if (topRadiusScale > 0) { faces.push([1, t0, t1]); faces.push([b0, b1, t1], [b0, t1, t0]); } else { faces.push([b0, b1, t0]); } } return { name: sanitizeName(shape.name), vertices, faces }; } function sphereMesh(shape: WorkplaneShape): MeshData { const lat = 12; const lon = 32; const width = shapeWidth(shape); const depth = shapeDepth(shape); const height = shape.height; const vertices: Vec3[] = []; for (let yStep = 0; yStep <= lat; yStep += 1) { const theta = (yStep / lat) * Math.PI; const y = height / 2 + Math.cos(theta) * (height / 2); const ring = Math.sin(theta); for (let xStep = 0; xStep < lon; xStep += 1) { const phi = (xStep / lon) * Math.PI * 2; vertices.push([(Math.cos(phi) * width * ring) / 2, y, (Math.sin(phi) * depth * ring) / 2]); } } const faces: [number, number, number][] = []; for (let yStep = 0; yStep < lat; yStep += 1) { for (let xStep = 0; xStep < lon; xStep += 1) { const next = (xStep + 1) % lon; const a = yStep * lon + xStep; const b = yStep * lon + next; const c = (yStep + 1) * lon + next; const d = (yStep + 1) * lon + xStep; faces.push([a, d, c], [a, c, b]); } } return { name: sanitizeName(shape.name), vertices, faces }; } function clampNumber(value: number, min: number, max: number) { return Math.min(max, Math.max(min, value)); } function bufferGeometryToMeshData(name: string, geometry: THREE.BufferGeometry): MeshData { const prepared = geometry.index ? geometry.toNonIndexed() : geometry; prepared.computeVertexNormals(); prepared.computeBoundingBox(); const minY = prepared.boundingBox?.min.y ?? 0; if (Math.abs(minY) > 0.000001) { prepared.translate(0, -minY, 0); prepared.computeBoundingBox(); } const position = prepared.getAttribute("position"); const vertices: Vec3[] = []; const faces: [number, number, number][] = []; for (let i = 0; i < position.count; i += 1) { vertices.push([position.getX(i), position.getY(i), position.getZ(i)]); } for (let i = 0; i + 2 < position.count; i += 3) { faces.push([i, i + 1, i + 2]); } if (prepared !== geometry) { prepared.dispose(); } geometry.dispose(); return { name, vertices, faces }; } function createBooleanRoofGeometry(width: number, height: number, depth: number) { const w = width / 2; const d = depth / 2; const vertices = new Float32Array([ -w, 0, -d, w, 0, -d, 0, height, -d, -w, 0, d, w, 0, d, 0, height, d, ]); const indices = [ 0, 2, 1, 3, 4, 5, 0, 1, 4, 0, 4, 3, 0, 3, 5, 0, 5, 2, 1, 2, 5, 1, 5, 4, ]; const geometry = new THREE.BufferGeometry(); geometry.setAttribute("position", new THREE.BufferAttribute(vertices, 3)); geometry.setIndex(indices); return geometry; } function createBooleanWedgeGeometry(width: number, height: number, depth: number) { const w = width / 2; const d = depth / 2; const vertices = new Float32Array([ -w, 0, -d, w, 0, -d, w, height, -d, -w, 0, d, w, 0, d, w, height, d, ]); const indices = [ 0, 2, 1, 3, 4, 5, 0, 1, 4, 0, 4, 3, 1, 2, 5, 1, 5, 4, 0, 3, 5, 0, 5, 2, ]; const geometry = new THREE.BufferGeometry(); geometry.setAttribute("position", new THREE.BufferAttribute(vertices, 3)); geometry.setIndex(indices); return geometry; } function createBooleanPyramidGeometry(width: number, height: number, depth: number, sides = 4) { const count = Math.max(3, Math.round(sides)); if (count !== 4) { const radius = Math.min(width, depth) / 2; const geometry = new THREE.ConeGeometry(radius, height, count); geometry.translate(0, height / 2, 0); return geometry; } const w = width / 2; const d = depth / 2; const vertices = new Float32Array([ -w, 0, -d, w, 0, -d, w, 0, d, -w, 0, d, 0, height, 0, ]); const indices = [ 0, 1, 2, 0, 2, 3, 0, 4, 1, 1, 4, 2, 2, 4, 3, 3, 4, 0, ]; const geometry = new THREE.BufferGeometry(); geometry.setAttribute("position", new THREE.BufferAttribute(vertices, 3)); geometry.setIndex(indices); return geometry; } function createBooleanRoundRoofGeometry(width: number, height: number, depth: number, sides = 64) { const radius = width / 2; const segments = Math.max(4, Math.round(sides)); const shape = new THREE.Shape(); shape.moveTo(-radius, 0); shape.absarc(0, 0, radius, Math.PI, 0, true); shape.lineTo(-radius, 0); shape.closePath(); const geometry = new THREE.ExtrudeGeometry(shape, { depth, bevelEnabled: false, steps: 1, curveSegments: segments }); geometry.translate(0, 0, -depth / 2); geometry.scale(1, height / Math.max(0.001, radius), 1); return geometry; } function createBooleanHalfSphereGeometry(width: number, height: number, depth: number, steps = 32) { const lon = Math.max(8, Math.round(steps) * 2); const lat = Math.max(4, Math.round(steps / 2)); const rx = width / 2; const rz = depth / 2; const positions: number[] = []; const point = (latIndex: number, lonIndex: number): Vec3 => { const theta = (latIndex / lat) * (Math.PI / 2); const phi = ((lonIndex % lon) / lon) * Math.PI * 2; const ring = Math.sin(theta); return [Math.cos(phi) * rx * ring, Math.cos(theta) * height, Math.sin(phi) * rz * ring]; }; const addTri = (a: Vec3, b: Vec3, c: Vec3) => positions.push(...a, ...b, ...c); const top: Vec3 = [0, height, 0]; for (let xStep = 0; xStep < lon; xStep += 1) { addTri(top, point(1, xStep + 1), point(1, xStep)); } for (let yStep = 1; yStep < lat; yStep += 1) { for (let xStep = 0; xStep < lon; xStep += 1) { const next = xStep + 1; const a = point(yStep, xStep); const b = point(yStep, next); const c = point(yStep + 1, next); const d = point(yStep + 1, xStep); addTri(a, c, d); addTri(a, b, c); } } const bottomCenter: Vec3 = [0, 0, 0]; const capPoint = (lonIndex: number): Vec3 => { const phi = ((lonIndex % lon) / lon) * Math.PI * 2; return [Math.cos(phi) * rx, 0, Math.sin(phi) * rz]; }; for (let xStep = 0; xStep < lon; xStep += 1) { addTri(bottomCenter, capPoint(xStep), capPoint(xStep + 1)); } const geometry = new THREE.BufferGeometry(); geometry.setAttribute("position", new THREE.Float32BufferAttribute(positions, 3)); geometry.computeVertexNormals(); return geometry; } function createBooleanTorusGeometry(width: number, height: number, depth: number) { const tubeRadius = Math.max(0.1, height / 2); const majorRadius = Math.max(0.2, Math.min(width, depth) / 2 - tubeRadius); const geometry = new THREE.TorusGeometry(majorRadius, tubeRadius, 36, 144); geometry.rotateX(Math.PI / 2); const outerDiameter = (majorRadius + tubeRadius) * 2; geometry.scale(width / Math.max(0.001, outerDiameter), 1, depth / Math.max(0.001, outerDiameter)); return geometry; } function createBooleanHollowCylinderGeometry(width: number, height: number, depth: number, thickness: number, segments = 96) { const outerX = width / 2; const outerZ = depth / 2; const safeThickness = clampNumber(thickness, 0.1, Math.max(0.1, Math.min(outerX, outerZ) - 0.1)); const innerX = Math.max(0.1, outerX - safeThickness); const innerZ = Math.max(0.1, outerZ - safeThickness); const count = Math.max(12, Math.round(segments)); const positions: number[] = []; const point = (rx: number, rz: number, y: number, index: number): Vec3 => { const angle = (index / count) * Math.PI * 2; return [Math.cos(angle) * rx, y, Math.sin(angle) * rz]; }; const addTri = (a: Vec3, b: Vec3, c: Vec3) => positions.push(...a, ...b, ...c); const addQuad = (a: Vec3, b: Vec3, c: Vec3, d: Vec3) => { addTri(a, b, c); addTri(a, c, d); }; for (let index = 0; index < count; index += 1) { const next = index + 1; const ob0 = point(outerX, outerZ, 0, index); const ob1 = point(outerX, outerZ, 0, next); const ot0 = point(outerX, outerZ, height, index); const ot1 = point(outerX, outerZ, height, next); const ib0 = point(innerX, innerZ, 0, index); const ib1 = point(innerX, innerZ, 0, next); const it0 = point(innerX, innerZ, height, index); const it1 = point(innerX, innerZ, height, next); addQuad(ob0, ot0, ot1, ob1); addQuad(ib1, it1, it0, ib0); addQuad(ot0, it0, it1, ot1); addQuad(ob0, ob1, ib1, ib0); } const geometry = new THREE.BufferGeometry(); geometry.setAttribute("position", new THREE.Float32BufferAttribute(positions, 3)); return geometry; } function createBooleanTextGeometry(shape: WorkplaneShape) { const text = (shape.text ?? "TEXT").trim() || " "; const bevel = clampNumber(shape.bevel ?? 0, 0, 8); const fontName = shape.font ?? "Multilanguage"; const geometry = new TextGeometry(text, { font: booleanTextFonts[fontName] ?? booleanTextFonts.Multilanguage, size: 20, depth: shape.height, curveSegments: fontName === "Stencil" ? 1 : 8, bevelEnabled: bevel > 0, bevelThickness: bevel * 0.22, bevelSize: bevel * 0.16, bevelSegments: Math.max(1, shape.segments ?? 0), }); geometry.computeBoundingBox(); const box = geometry.boundingBox; if (box) { const textWidth = Math.max(1, box.max.x - box.min.x); const textDepth = Math.max(1, box.max.y - box.min.y); const scale = Math.min(shapeWidth(shape) / textWidth, shapeDepth(shape) / textDepth); geometry.scale(scale, scale, 1); } geometry.rotateX(-Math.PI / 2); geometry.computeBoundingBox(); const rotatedBox = geometry.boundingBox; if (rotatedBox) { geometry.translate( -(rotatedBox.min.x + rotatedBox.max.x) / 2, -rotatedBox.min.y, -(rotatedBox.min.z + rotatedBox.max.z) / 2, ); } return geometry; } function geometryMeshForShape(shape: WorkplaneShape): MeshData | null { const width = shapeWidth(shape); const depth = shapeDepth(shape); const height = shape.height; const size = Math.min(width, depth); let geometry: THREE.BufferGeometry | null = null; switch (shape.kind) { case "box": geometry = shape.radius && shape.radius > 0 ? new RoundedBoxGeometry(width, height, depth, Math.max(1, shape.steps ?? 10), shape.radius) : new THREE.BoxGeometry(width, height, depth); break; case "cylinder": geometry = new THREE.CylinderGeometry(1, 1, height, shape.sides ?? 96, shape.segments ?? 1); geometry.scale(width / 2, 1, depth / 2); break; case "sphere": geometry = new THREE.SphereGeometry(1, Math.max(8, (shape.steps ?? 24) * 2), Math.max(6, shape.steps ?? 24)); geometry.scale(width / 2, height / 2, depth / 2); break; case "cone": { const baseRadius = shape.baseRadius ?? width / 2; geometry = new THREE.CylinderGeometry(shape.topRadius ?? 0, baseRadius, height, shape.sides ?? 96); geometry.scale(1, 1, depth / Math.max(0.001, width)); break; } case "pyramid": geometry = createBooleanPyramidGeometry(width, height, depth, shape.sides ?? 4); break; case "roof": geometry = createBooleanRoofGeometry(width, height, depth); break; case "roundRoof": geometry = createBooleanRoundRoofGeometry(width, height, depth, shape.sides ?? 64); break; case "halfSphere": geometry = createBooleanHalfSphereGeometry(width, height, depth, shape.steps ?? 32); break; case "torus": geometry = createBooleanTorusGeometry(width, height, depth); break; case "ring": case "tube": geometry = createBooleanHollowCylinderGeometry(width, height, depth, shape.bevel ?? 4, 144); break; case "wedge": geometry = createBooleanWedgeGeometry(width, height, depth); break; case "polygon": geometry = new THREE.CylinderGeometry(1, 1, height, 6); geometry.scale(width / 2, 1, depth / 2); break; case "icosahedron": geometry = new THREE.IcosahedronGeometry(size / 2, 1); geometry.translate(0, height / 2, 0); break; case "text": geometry = createBooleanTextGeometry(shape); break; case "scribble": geometry = new THREE.TorusKnotGeometry(size * 0.22, size * 0.055, 120, 12); geometry.translate(0, height / 2, 0); break; case "sketch": default: geometry = new THREE.BoxGeometry(size, Math.max(3, height * 0.35), size * 0.72); break; } return geometry ? bufferGeometryToMeshData(sanitizeName(shape.name), geometry) : null; } function meshForShape(shape: WorkplaneShape): MeshData { if (shape.kind === "mesh" && shape.importedMesh) { return importedMeshForShape(shape); } if (shape.groupedShapes?.length) { const vertices: Vec3[] = []; const faces: [number, number, number][] = []; shape.groupedShapes.filter((child) => !child.hidden).forEach((child) => { const childMesh = meshForShape(child); appendMeshData(vertices, faces, childMesh); }); return transformMesh({ name: sanitizeName(shape.name), vertices, faces }, shape); } const raw = geometryMeshForShape(shape) ?? (shape.kind === "cylinder" || shape.kind === "tube" || shape.kind === "ring" || shape.kind === "torus" ? cylinderMesh(shape, shape.sides ?? 96) : shape.kind === "cone" ? cylinderMesh(shape, shape.sides ?? 96, shape.baseRadius ? (shape.topRadius ?? 0) / shape.baseRadius : 0) : shape.kind === "sphere" || shape.kind === "halfSphere" ? sphereMesh(shape) : shape.kind === "pyramid" ? cylinderMesh(shape, shape.sides ?? 4, 0) : boxMesh(shape)); return transformMesh(raw, shape); } function appendMeshData(vertices: Vec3[], faces: [number, number, number][], mesh: MeshData) { const offset = vertices.length; for (let i = 0; i < mesh.vertices.length; i += 1) { vertices.push(mesh.vertices[i]); } for (let i = 0; i < mesh.faces.length; i += 1) { const [a, b, c] = mesh.faces[i]; faces.push([a + offset, b + offset, c + offset]); } } function importedMeshForShape(shape: WorkplaneShape): MeshData { const mesh = shape.importedMesh; if (!mesh || mesh.positions.length < 9) { return transformMesh(boxMesh(shape), shape); } const resizedPositions = resizedImportedMeshPositions(shape); const vertices: Vec3[] = []; for (let i = 0; i < resizedPositions.length; i += 3) { vertices.push([resizedPositions[i], resizedPositions[i + 1], resizedPositions[i + 2]]); } const faces: [number, number, number][] = []; for (let i = 0; i + 2 < vertices.length; i += 3) { faces.push([i, i + 1, i + 2]); } return transformMesh({ name: sanitizeName(shape.name), vertices, faces }, shape); } function shapeHasTransformToBake(shape: WorkplaneShape) { return ( Math.abs(cleanRotationDegrees(shape.rotation ?? 0, 3)) > 0 || Math.abs(cleanRotationDegrees(shape.rotationX ?? 0, 3)) > 0 || Math.abs(cleanRotationDegrees(shape.rotationZ ?? 0, 3)) > 0 || Boolean(shape.mirrorX || shape.mirrorY || shape.mirrorZ) ); } function cadModifierPrimitiveForShape(shape: WorkplaneShape): CadModifierPrimitivePart | null { return cadModifierPrimitiveForBakedShape(shape) ?? (shapeHasTransformToBake(shape) ? cadModifierPrimitiveForAnalyticBox(shape) : null); } function bakeShapeTransformIntoMesh(shape: WorkplaneShape): WorkplaneShape { if (!shapeHasTransformToBake(shape)) { return shape; } const mesh = meshForShape(shape); if (mesh.vertices.length < 3 || mesh.faces.length < 1) { return shape; } let minX = Number.POSITIVE_INFINITY; let minY = Number.POSITIVE_INFINITY; let minZ = Number.POSITIVE_INFINITY; let maxX = Number.NEGATIVE_INFINITY; let maxY = Number.NEGATIVE_INFINITY; let maxZ = Number.NEGATIVE_INFINITY; mesh.vertices.forEach(([x, y, z]) => { minX = Math.min(minX, x); minY = Math.min(minY, y); minZ = Math.min(minZ, z); maxX = Math.max(maxX, x); maxY = Math.max(maxY, y); maxZ = Math.max(maxZ, z); }); if (![minX, minY, minZ, maxX, maxY, maxZ].every(Number.isFinite)) { return shape; } const centerX = (minX + maxX) / 2; const centerZ = (minZ + maxZ) / 2; const rawWidth = Math.max(MIN_SHAPE_DIMENSION, maxX - minX); const rawHeight = Math.max(MIN_SHAPE_DIMENSION, maxY - minY); const rawDepth = Math.max(MIN_SHAPE_DIMENSION, maxZ - minZ); const width = cleanModelDimension(rawWidth); const height = cleanModelDimension(rawHeight); const depth = cleanModelDimension(rawDepth); const positions: number[] = []; const bakedCadMetadata = bakeCadMetadataForShapeTransform(shape, { centerX, minY, centerZ, width, depth, height, yawDegrees: meshYawDegrees(shape) }); mesh.faces.forEach(([ai, bi, ci]) => { [mesh.vertices[ai], mesh.vertices[bi], mesh.vertices[ci]].forEach(([x, y, z]) => { positions.push(x - centerX, y - minY, z - centerZ); }); }); return { ...shape, kind: "mesh", x: cleanNearZero(centerX, 0.0005), z: cleanNearZero(centerZ, 0.0005), elevation: cleanNearZero(minY, 0.0005), width, depth, height, size: Math.max(width, depth), rotation: 0, rotationX: 0, rotationZ: 0, mirrorX: undefined, mirrorY: undefined, mirrorZ: undefined, importedMesh: { positions, baseWidth: rawWidth, baseDepth: rawDepth, baseHeight: rawHeight, triangleCount: mesh.faces.length, sourceFormat: "json", }, ...bakedCadMetadata, imagePlate: undefined, groupedShapes: undefined, groupedBaseWidth: undefined, groupedBaseDepth: undefined, groupedBaseHeight: undefined, }; } function importedShapeFromSvg(fileName: string, source: string): WorkplaneShape { const parsed = svgLoader.parse(source); const rawPositions: number[] = []; parsed.paths.forEach((path) => { SVGLoader.createShapes(path).forEach((svgShape) => { const rawGeometry = new THREE.ExtrudeGeometry(svgShape, { depth: 4, bevelEnabled: false, curveSegments: 16, steps: 1, }); rawGeometry.rotateX(-Math.PI / 2); const geometry = rawGeometry.index ? rawGeometry.toNonIndexed() : rawGeometry; const position = geometry.getAttribute("position"); for (let i = 0; i < position.count; i += 1) { rawPositions.push(position.getX(i), position.getY(i), position.getZ(i)); } }); }); if (rawPositions.length < 9) { throw new Error("SVG has no readable filled paths"); } let minX = Number.POSITIVE_INFINITY; let minY = Number.POSITIVE_INFINITY; let minZ = Number.POSITIVE_INFINITY; let maxX = Number.NEGATIVE_INFINITY; let maxY = Number.NEGATIVE_INFINITY; let maxZ = Number.NEGATIVE_INFINITY; for (let i = 0; i < rawPositions.length; i += 3) { minX = Math.min(minX, rawPositions[i]); minY = Math.min(minY, rawPositions[i + 1]); minZ = Math.min(minZ, rawPositions[i + 2]); maxX = Math.max(maxX, rawPositions[i]); maxY = Math.max(maxY, rawPositions[i + 1]); maxZ = Math.max(maxZ, rawPositions[i + 2]); } const centerX = (minX + maxX) / 2; const centerZ = (minZ + maxZ) / 2; const width = Math.max(1, maxX - minX); const height = Math.max(1, maxY - minY); const depth = Math.max(1, maxZ - minZ); const positions: number[] = []; for (let i = 0; i < rawPositions.length; i += 3) { positions.push(rawPositions[i] - centerX, rawPositions[i + 1] - minY, rawPositions[i + 2] - centerZ); } return { id: createLocalId("uploaded-svg"), name: fileName.replace(/\.[^.]+$/, "") || "Imported SVG", kind: "mesh", color: "#0098c7", x: 10, z: -10, size: Math.max(width, depth), width, depth, height, rotation: 0, rotationX: 0, rotationZ: 0, importedMesh: { positions, baseWidth: width, baseDepth: depth, baseHeight: height, triangleCount: Math.floor(positions.length / 9), sourceFormat: "svg", }, locked: false, hidden: false, }; } function readFileAsDataUrl(file: File) { return new Promise((resolve, reject) => { const reader = new FileReader(); reader.addEventListener("load", () => { if (typeof reader.result === "string") { resolve(reader.result); } else { reject(new Error("Image could not be read")); } }); reader.addEventListener("error", () => reject(new Error("Image could not be read"))); reader.readAsDataURL(file); }); } function loadImageElement(dataUrl: string) { return new Promise((resolve, reject) => { const image = new Image(); image.addEventListener("load", () => resolve(image)); image.addEventListener("error", () => reject(new Error("Image could not be decoded"))); image.src = dataUrl; }); } async function prepareImportedImage(file: File) { const sourceUrl = await readFileAsDataUrl(file); const image = await loadImageElement(sourceUrl); const pixelWidth = image.naturalWidth || image.width; const pixelHeight = image.naturalHeight || image.height; if (!pixelWidth || !pixelHeight) { throw new Error("Image has no readable dimensions"); } const maxTextureSide = 2048; const textureScale = Math.min(1, maxTextureSide / Math.max(pixelWidth, pixelHeight)); if (textureScale >= 1) { return { dataUrl: sourceUrl, mimeType: file.type || "image/png", pixelWidth, pixelHeight, }; } const canvas = document.createElement("canvas"); canvas.width = Math.max(1, Math.round(pixelWidth * textureScale)); canvas.height = Math.max(1, Math.round(pixelHeight * textureScale)); const context = canvas.getContext("2d"); if (!context) { throw new Error("Image could not be prepared"); } context.drawImage(image, 0, 0, canvas.width, canvas.height); const mimeType = file.type === "image/jpeg" || file.type === "image/webp" ? file.type : "image/png"; return { dataUrl: canvas.toDataURL(mimeType, 0.92), mimeType, pixelWidth, pixelHeight, }; } function imagePlateDimensions(pixelWidth: number, pixelHeight: number) { const aspect = pixelWidth / Math.max(1, pixelHeight); const targetMax = 72; const minVisibleSide = 14; const maxAllowedSide = 110; let width = aspect >= 1 ? targetMax : targetMax * aspect; let depth = aspect >= 1 ? targetMax / aspect : targetMax; const minSide = Math.min(width, depth); if (minSide < minVisibleSide) { const boost = minVisibleSide / Math.max(0.001, minSide); width *= boost; depth *= boost; } const maxSide = Math.max(width, depth); if (maxSide > maxAllowedSide) { const shrink = maxAllowedSide / maxSide; width *= shrink; depth *= shrink; } return { width: Number(width.toFixed(2)), depth: Number(depth.toFixed(2)), height: 1.6, }; } async function importedShapeFromImage(file: File): Promise { const imagePlate = await prepareImportedImage(file); const dimensions = imagePlateDimensions(imagePlate.pixelWidth, imagePlate.pixelHeight); return { id: createLocalId("uploaded-image"), name: file.name.replace(/\.[^.]+$/, "") || "Imported Image", kind: "box", color: "#f4f7f9", x: 10, z: -10, size: Math.max(dimensions.width, dimensions.depth), width: dimensions.width, depth: dimensions.depth, height: dimensions.height, elevation: 0, rotation: 0, rotationX: 0, rotationZ: 0, radius: 0, steps: 1, imagePlate, locked: false, hidden: false, }; } function normalFor(a: Vec3, b: Vec3, c: Vec3): Vec3 { const ux = b[0] - a[0]; const uy = b[1] - a[1]; const uz = b[2] - a[2]; const vx = c[0] - a[0]; const vy = c[1] - a[1]; const vz = c[2] - a[2]; const nx = uy * vz - uz * vy; const ny = uz * vx - ux * vz; const nz = ux * vy - uy * vx; const length = Math.hypot(nx, ny, nz) || 1; return [nx / length, ny / length, nz / length]; } function toStl(meshes: MeshData[]) { const lines = ["solid sketchforge_design"]; meshes.forEach((mesh) => { mesh.faces.forEach(([ai, bi, ci]) => { const a = mesh.vertices[ai]; const b = mesh.vertices[bi]; const c = mesh.vertices[ci]; const n = normalFor(a, b, c); lines.push(` facet normal ${n[0]} ${n[1]} ${n[2]}`); lines.push(" outer loop"); lines.push(` vertex ${a[0]} ${a[1]} ${a[2]}`); lines.push(` vertex ${b[0]} ${b[1]} ${b[2]}`); lines.push(` vertex ${c[0]} ${c[1]} ${c[2]}`); lines.push(" endloop"); lines.push(" endfacet"); }); }); lines.push("endsolid sketchforge_design"); return lines.join("\n"); } function toObj(meshes: MeshData[]) { const lines = ["# SketchForge OBJ export"]; let offset = 1; meshes.forEach((mesh) => { lines.push(`o ${mesh.name}`); mesh.vertices.forEach(([x, y, z]) => lines.push(`v ${x} ${y} ${z}`)); mesh.faces.forEach(([a, b, c]) => lines.push(`f ${a + offset} ${b + offset} ${c + offset}`)); offset += mesh.vertices.length; }); return lines.join("\n"); } function triggerBrowserDownload(filename: string, content: string, type: string) { const blob = new Blob([content], { type }); const url = URL.createObjectURL(blob); const link = document.createElement("a"); link.href = url; link.download = filename; link.style.display = "none"; document.body.appendChild(link); link.click(); link.remove(); window.setTimeout(() => URL.revokeObjectURL(url), 1000); } async function downloadTextFile(filename: string, content: string, type: string): Promise { const mode = window.localStorage.getItem(DOWNLOAD_MODE_STORAGE_KEY); const folder = window.localStorage.getItem(DOWNLOAD_FOLDER_STORAGE_KEY)?.trim() ?? ""; if (!STATIC_EXPORT_BUILD && mode === "folder" && folder) { const response = await fetch("/api/local-download", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ content, filename, folder }), }); const payload = (await response.json().catch(() => null)) as { error?: string; path?: string } | null; if (!response.ok || !payload?.path) { throw new Error(payload?.error ?? "Could not save export"); } return { mode: "folder", path: payload.path }; } triggerBrowserDownload(filename, content, type); return { mode: "browser" }; } function shapeAabb(shape: WorkplaneShape): Cuboid { const halfWidth = shapeWidth(shape) / 2; const halfDepth = shapeDepth(shape) / 2; return { minX: shape.x - halfWidth, maxX: shape.x + halfWidth, minY: shape.elevation ?? 0, maxY: (shape.elevation ?? 0) + shape.height, minZ: shape.z - halfDepth, maxZ: shape.z + halfDepth, }; } function boundsForShapes(shapes: WorkplaneShape[]): Cuboid { const bounds = shapes.map(meshAabb); return boundsForCuboids(bounds); } function boundsForCuboids(bounds: Cuboid[]): Cuboid { return { minX: Math.min(...bounds.map((box) => box.minX)), maxX: Math.max(...bounds.map((box) => box.maxX)), minY: Math.min(...bounds.map((box) => box.minY)), maxY: Math.max(...bounds.map((box) => box.maxY)), minZ: Math.min(...bounds.map((box) => box.minZ)), maxZ: Math.max(...bounds.map((box) => box.maxZ)), }; } function dropPatchForShape(shape: WorkplaneShape, targetY: number): Partial { const bounds = meshAabb(shape); const delta = targetY - bounds.minY; const nextElevation = (shape.elevation ?? 0) + delta; return { elevation: Math.abs(nextElevation) < 0.0005 ? 0 : Number(nextElevation.toFixed(4)) }; } function meshAabb(shape: WorkplaneShape): Cuboid { const mesh = meshForShape(shape); if (mesh.vertices.length === 0) { return shapeAabb(shape); } let minX = Number.POSITIVE_INFINITY; let minY = Number.POSITIVE_INFINITY; let minZ = Number.POSITIVE_INFINITY; let maxX = Number.NEGATIVE_INFINITY; let maxY = Number.NEGATIVE_INFINITY; let maxZ = Number.NEGATIVE_INFINITY; mesh.vertices.forEach(([x, y, z]) => { minX = Math.min(minX, x); minY = Math.min(minY, y); minZ = Math.min(minZ, z); maxX = Math.max(maxX, x); maxY = Math.max(maxY, y); maxZ = Math.max(maxZ, z); }); if (![minX, minY, minZ, maxX, maxY, maxZ].every(Number.isFinite)) { return shapeAabb(shape); } return { minX, maxX, minY, maxY, minZ, maxZ }; } const ALIGN_EPSILON = 0.0005; const ALIGN_AXES: AlignAxis[] = ["x", "y", "z"]; const ALIGN_TARGETS: AlignTarget[] = ["min", "center", "max"]; function alignCoordinate(bounds: Cuboid, axis: AlignAxis, target: AlignTarget) { const min = axis === "x" ? bounds.minX : axis === "y" ? bounds.minY : bounds.minZ; const max = axis === "x" ? bounds.maxX : axis === "y" ? bounds.maxY : bounds.maxZ; if (target === "min") { return min; } if (target === "max") { return max; } return (min + max) / 2; } function alignmentLabel(axis: AlignAxis, target: AlignTarget) { if (axis === "x") { return target === "min" ? "left" : target === "max" ? "right" : "center"; } if (axis === "z") { return target === "min" ? "front" : target === "max" ? "back" : "middle"; } return target === "min" ? "bottom" : target === "max" ? "top" : "middle"; } function alignmentStatuses(selection: WorkplaneShape[], anchorId: string | null): AlignHandleStatus[] { if (selection.length < 2) { return []; } const boundsById = new Map(selection.map((shape) => [shape.id, meshAabb(shape)])); const anchorBounds = anchorId ? boundsById.get(anchorId) ?? null : null; const referenceBounds = anchorBounds ?? boundsForCuboids(Array.from(boundsById.values())); return ALIGN_AXES.flatMap((axis) => ALIGN_TARGETS.map((target) => { const targetValue = alignCoordinate(referenceBounds, axis, target); const aligned = selection.every((shape) => { const bounds = boundsById.get(shape.id); return bounds ? Math.abs(alignCoordinate(bounds, axis, target) - targetValue) <= ALIGN_EPSILON : true; }); const wouldMove = selection.some((shape) => { if (shape.locked || shape.id === anchorId) { return false; } const bounds = boundsById.get(shape.id); return bounds ? Math.abs(alignCoordinate(bounds, axis, target) - targetValue) > ALIGN_EPSILON : false; }); const label = alignmentLabel(axis, target); return { axis, target, aligned, disabled: !wouldMove, title: aligned ? `Already aligned ${label}` : `Align ${label}`, }; }), ); } function alignedShapesForSelection( shapes: WorkplaneShape[], selectedIds: string[], selectedShapes: WorkplaneShape[], anchorId: string | null, axis: AlignAxis, target: AlignTarget, ) { const selected = new Set(selectedIds); const boundsById = new Map(selectedShapes.map((shape) => [shape.id, meshAabb(shape)])); const anchorBounds = anchorId ? boundsById.get(anchorId) ?? null : null; const referenceBounds = anchorBounds ?? boundsForCuboids(Array.from(boundsById.values())); const targetValue = alignCoordinate(referenceBounds, axis, target); let moved = 0; const nextShapes = shapes.map((shape) => { if (!selected.has(shape.id) || shape.locked || shape.id === anchorId) { return shape; } const bounds = boundsById.get(shape.id); if (!bounds) { return shape; } const delta = targetValue - alignCoordinate(bounds, axis, target); if (Math.abs(delta) <= ALIGN_EPSILON) { return shape; } moved += 1; if (axis === "x") { return { ...shape, x: cleanNearZero(Number((shape.x + delta).toFixed(4)), ALIGN_EPSILON) }; } if (axis === "z") { return { ...shape, z: cleanNearZero(Number((shape.z + delta).toFixed(4)), ALIGN_EPSILON) }; } return { ...shape, elevation: cleanNearZero(Number(((shape.elevation ?? 0) + delta).toFixed(4)), ALIGN_EPSILON) }; }); return { nextShapes, moved }; } function effectiveAlignmentAnchorId(selection: WorkplaneShape[], requestedAnchorId: string | null) { return selection.find((shape) => shape.locked)?.id ?? (requestedAnchorId && selection.some((shape) => shape.id === requestedAnchorId) ? requestedAnchorId : null); } function mirrorAxisLabel(axis: AlignAxis) { return axis === "x" ? "left-right" : axis === "z" ? "front-back" : "top-bottom"; } function mirrorFlagPatch(shape: WorkplaneShape, axis: AlignAxis) { if (axis === "x") { return { mirrorX: !shape.mirrorX }; } if (axis === "z") { return { mirrorZ: !shape.mirrorZ }; } return { mirrorY: !shape.mirrorY }; } function reflectionMatrixForAxis(axis: AlignAxis) { return new THREE.Matrix4().makeScale(axis === "x" ? -1 : 1, axis === "y" ? -1 : 1, axis === "z" ? -1 : 1); } function mirroredShapePatch(shape: WorkplaneShape, axis: AlignAxis, pivot: number): Partial { const centerY = (shape.elevation ?? 0) + shape.height / 2; const nextCenter = axis === "x" ? 2 * pivot - shape.x : axis === "z" ? 2 * pivot - shape.z : 2 * pivot - centerY; const worldReflection = reflectionMatrixForAxis(axis); const localReflection = reflectionMatrixForAxis(axis); const currentRotation = new THREE.Matrix4().makeRotationFromQuaternion(quaternionForShape(shape)); const nextRotationMatrix = worldReflection.multiply(currentRotation).multiply(localReflection); const nextQuaternion = new THREE.Quaternion().setFromRotationMatrix(nextRotationMatrix); const rotationPatch = rotationFromQuaternion(nextQuaternion); const positionPatch = axis === "x" ? { x: cleanNearZero(Number(nextCenter.toFixed(4)), ALIGN_EPSILON) } : axis === "z" ? { z: cleanNearZero(Number(nextCenter.toFixed(4)), ALIGN_EPSILON) } : { elevation: cleanNearZero(Number((nextCenter - shape.height / 2).toFixed(4)), ALIGN_EPSILON) }; return { ...shape, ...positionPatch, ...rotationPatch, ...mirrorFlagPatch(shape, axis), }; } function mirroredShapesForSelection(shapes: WorkplaneShape[], selectedIds: string[], selectedShapes: WorkplaneShape[], axis: AlignAxis) { if (selectedShapes.length === 0) { return { nextShapes: shapes, moved: 0 }; } const selected = new Set(selectedIds); const selectionBounds = boundsForShapes(selectedShapes); const pivot = axis === "x" ? (selectionBounds.minX + selectionBounds.maxX) / 2 : axis === "z" ? (selectionBounds.minZ + selectionBounds.maxZ) / 2 : (selectionBounds.minY + selectionBounds.maxY) / 2; let moved = 0; const nextShapes = shapes.map((shape) => { if (!selected.has(shape.id) || shape.locked) { return shape; } moved += 1; return { ...shape, ...mirroredShapePatch(shape, axis, pivot), }; }); return { nextShapes, moved }; } function geometryFromMeshData(mesh: MeshData) { const positions: number[] = []; mesh.faces.forEach(([ai, bi, ci]) => { [mesh.vertices[ai], mesh.vertices[bi], mesh.vertices[ci]].forEach(([x, y, z]) => { positions.push(x, y, z); }); }); const geometry = new THREE.BufferGeometry(); geometry.setAttribute("position", new THREE.Float32BufferAttribute(positions, 3)); geometry.computeVertexNormals(); return geometry; } function positionsFromGeometryDrawRange(geometry: THREE.BufferGeometry) { const position = geometry.getAttribute("position"); if (!position) { return []; } const positions: number[] = []; const drawStart = Math.max(0, Math.floor(geometry.drawRange.start || 0)); if (geometry.index) { const index = geometry.index; const drawCount = Number.isFinite(geometry.drawRange.count) ? Math.max(0, Math.floor(geometry.drawRange.count)) : index.count - drawStart; const end = Math.min(index.count, drawStart + drawCount); for (let i = drawStart; i + 2 < end; i += 3) { for (let offset = 0; offset < 3; offset += 1) { const vertexIndex = index.getX(i + offset); positions.push(position.getX(vertexIndex), position.getY(vertexIndex), position.getZ(vertexIndex)); } } return positions; } const drawCount = Number.isFinite(geometry.drawRange.count) ? Math.max(0, Math.floor(geometry.drawRange.count)) : position.count - drawStart; const end = Math.min(position.count, drawStart + drawCount); for (let i = drawStart; i + 2 < end; i += 3) { positions.push( position.getX(i), position.getY(i), position.getZ(i), position.getX(i + 1), position.getY(i + 1), position.getZ(i + 1), position.getX(i + 2), position.getY(i + 2), position.getZ(i + 2), ); } return positions; } function boundsForPositions(positions: number[]): Cuboid | null { if (positions.length < 9) { return null; } let minX = Number.POSITIVE_INFINITY; let minY = Number.POSITIVE_INFINITY; let minZ = Number.POSITIVE_INFINITY; let maxX = Number.NEGATIVE_INFINITY; let maxY = Number.NEGATIVE_INFINITY; let maxZ = Number.NEGATIVE_INFINITY; for (let i = 0; i < positions.length; i += 3) { const x = positions[i]; const y = positions[i + 1]; const z = positions[i + 2]; minX = Math.min(minX, x); minY = Math.min(minY, y); minZ = Math.min(minZ, z); maxX = Math.max(maxX, x); maxY = Math.max(maxY, y); maxZ = Math.max(maxZ, z); } return [minX, minY, minZ, maxX, maxY, maxZ].every(Number.isFinite) ? { minX, maxX, minY, maxY, minZ, maxZ } : null; } function quantizedPointKey([x, y, z]: Vec3, tolerance: number) { return [x, y, z].map((value) => Math.round(value / tolerance)).join(","); } function triangleSignature(points: Vec3[], tolerance: number) { return points.map((point) => quantizedPointKey(point, tolerance)).sort().join("|"); } function addSignature(signatures: Map, signature: string) { signatures.set(signature, (signatures.get(signature) ?? 0) + 1); } function meshSignatureMap(mesh: MeshData, tolerance: number) { const signatures = new Map(); mesh.faces.forEach(([ai, bi, ci]) => { addSignature(signatures, triangleSignature([mesh.vertices[ai], mesh.vertices[bi], mesh.vertices[ci]], tolerance)); }); return signatures; } function positionsSignatureMap(positions: number[], tolerance: number) { const signatures = new Map(); for (let i = 0; i + 8 < positions.length; i += 9) { addSignature( signatures, triangleSignature( [ [positions[i], positions[i + 1], positions[i + 2]], [positions[i + 3], positions[i + 4], positions[i + 5]], [positions[i + 6], positions[i + 7], positions[i + 8]], ], tolerance, ), ); } return signatures; } function signatureMapsDiffer(a: Map, b: Map) { if (a.size !== b.size) { return true; } for (const [signature, count] of a) { if (b.get(signature) !== count) { return true; } } return false; } function positionsDifferFromMeshData(positions: number[], mesh: MeshData, tolerance = 0.0005) { if (Math.floor(positions.length / 9) !== mesh.faces.length) { return true; } return signatureMapsDiffer(positionsSignatureMap(positions, tolerance), meshSignatureMap(mesh, tolerance)); } function geometryDiffersFromMeshData(geometry: THREE.BufferGeometry, mesh: MeshData, tolerance = 0.0005) { return positionsDifferFromMeshData(positionsFromGeometryDrawRange(geometry), mesh, tolerance); } function sortedEdgeKey(a: Vec3, b: Vec3, tolerance: number) { const ak = quantizedPointKey(a, tolerance); const bk = quantizedPointKey(b, tolerance); return ak < bk ? `${ak}|${bk}` : `${bk}|${ak}`; } function edgeMidpoint(a: Vec3, b: Vec3): Vec3 { return [(a[0] + b[0]) / 2, (a[1] + b[1]) / 2, (a[2] + b[2]) / 2]; } function addBoundaryEdge(edges: Map, a: Vec3, b: Vec3, tolerance: number) { const key = sortedEdgeKey(a, b, tolerance); const existing = edges.get(key); if (existing) { existing.count += 1; } else { edges.set(key, { count: 1, midpoint: edgeMidpoint(a, b) }); } } function positionsBoundaryEdges(positions: number[], tolerance = 0.0005) { const edges = new Map(); for (let i = 0; i + 8 < positions.length; i += 9) { const a: Vec3 = [positions[i], positions[i + 1], positions[i + 2]]; const b: Vec3 = [positions[i + 3], positions[i + 4], positions[i + 5]]; const c: Vec3 = [positions[i + 6], positions[i + 7], positions[i + 8]]; addBoundaryEdge(edges, a, b, tolerance); addBoundaryEdge(edges, b, c, tolerance); addBoundaryEdge(edges, c, a, tolerance); } return Array.from(edges.values()).filter((edge) => edge.count === 1); } function meshDataPositions(mesh: MeshData) { const positions: number[] = []; mesh.faces.forEach(([ai, bi, ci]) => { [mesh.vertices[ai], mesh.vertices[bi], mesh.vertices[ci]].forEach(([x, y, z]) => { positions.push(x, y, z); }); }); return positions; } function meshFaceComponents(mesh: MeshData, tolerance = SEPARATE_PARTS_VERTEX_TOLERANCE) { if (mesh.faces.length === 0) return []; const keysByFace = mesh.faces.map((face) => face.map((vertexIndex) => quantizedPointKey(mesh.vertices[vertexIndex], tolerance))); const facesByVertex = new Map(); keysByFace.forEach((keys, faceIndex) => { keys.forEach((key) => { const current = facesByVertex.get(key); if (current) { current.push(faceIndex); } else { facesByVertex.set(key, [faceIndex]); } }); }); const visited = new Uint8Array(mesh.faces.length); const components: number[][] = []; for (let faceIndex = 0; faceIndex < mesh.faces.length; faceIndex += 1) { if (visited[faceIndex]) continue; const component: number[] = []; const queue = [faceIndex]; visited[faceIndex] = 1; while (queue.length > 0) { const current = queue.pop() as number; component.push(current); keysByFace[current].forEach((key) => { const neighbors = facesByVertex.get(key); if (!neighbors) return; facesByVertex.delete(key); neighbors.forEach((neighbor) => { if (visited[neighbor]) return; visited[neighbor] = 1; queue.push(neighbor); }); }); } components.push(component); } return components; } function meshComponentShape(source: WorkplaneShape, mesh: MeshData, faceIndices: number[], partIndex: number, totalParts: number): WorkplaneShape | null { const worldPositions: number[] = []; faceIndices.forEach((faceIndex) => { const face = mesh.faces[faceIndex]; if (!face) return; face.forEach((vertexIndex) => { const vertex = mesh.vertices[vertexIndex]; if (vertex) worldPositions.push(vertex[0], vertex[1], vertex[2]); }); }); const bounds = boundsForPositions(worldPositions); if (!bounds) return null; const centerX = (bounds.minX + bounds.maxX) / 2; const centerZ = (bounds.minZ + bounds.maxZ) / 2; const rawWidth = Math.max(MIN_SHAPE_DIMENSION, bounds.maxX - bounds.minX); const rawHeight = Math.max(MIN_SHAPE_DIMENSION, bounds.maxY - bounds.minY); const rawDepth = Math.max(MIN_SHAPE_DIMENSION, bounds.maxZ - bounds.minZ); const width = cleanModelDimension(rawWidth); const height = cleanModelDimension(rawHeight); const depth = cleanModelDimension(rawDepth); const positions = worldPositions.map((value, index) => { if (index % 3 === 0) return value - centerX; if (index % 3 === 1) return value - bounds.minY; return value - centerZ; }); return canonicalizeShape({ id: createLocalId(`${source.id}-part`), name: totalParts > 1 ? `${source.name} Part ${partIndex + 1}` : source.name, kind: "mesh", color: source.color, hole: source.hole || undefined, x: cleanNearZero(centerX, 0.0005), z: cleanNearZero(centerZ, 0.0005), elevation: cleanNearZero(bounds.minY, 0.0005), size: Math.max(width, depth), width, depth, height, rotation: 0, rotationX: 0, rotationZ: 0, importedMesh: { positions, baseWidth: rawWidth, baseDepth: rawDepth, baseHeight: rawHeight, triangleCount: Math.floor(positions.length / 9), sourceFormat: "json", }, locked: false, hidden: source.hidden, }); } function separateMeshParts(shape: WorkplaneShape) { const mesh = meshForShape(shape); const components = meshFaceComponents(mesh).filter((component) => component.length > 0); if (components.length <= 1) return []; return components .map((component, index) => meshComponentShape(shape, mesh, component, index, components.length)) .filter((part): part is WorkplaneShape => Boolean(part)); } function separablePartCount(shape: WorkplaneShape) { if (shape.locked || shape.hole) return 0; if (shape.groupedShapes?.length && !shape.importedMesh) return shape.groupedShapes.length; const mesh = meshForShape(shape); return meshFaceComponents(mesh).length; } function separateShapeParts(shape: WorkplaneShape) { if (shape.locked || shape.hole) return []; if (shape.groupedShapes?.length && !shape.importedMesh) { const restored = restoreGroupedChildren(shape); return restored.length > 1 ? restored : []; } return separateMeshParts(shape); } function cutBoundaryEdgeCount(positions: number[], cutters: WorkplaneShape[]) { if (cutters.length === 0) { return 0; } return positionsBoundaryEdges(positions).filter((edge) => cutters.some((cutter) => pointInsideHoleShape(edge.midpoint, cutter))).length; } function introducesOpenCutBoundary(resultPositions: number[], sourceMesh: MeshData, cutters: WorkplaneShape[]) { const resultCutBoundaries = cutBoundaryEdgeCount(resultPositions, cutters); if (resultCutBoundaries === 0) { return false; } const sourceCutBoundaries = cutBoundaryEdgeCount(meshDataPositions(sourceMesh), cutters); return resultCutBoundaries > sourceCutBoundaries + Math.max(4, Math.floor(sourceCutBoundaries * 0.25)); } function cuboidFromBox3(box: THREE.Box3): Cuboid { return { minX: box.min.x, maxX: box.max.x, minY: box.min.y, maxY: box.max.y, minZ: box.min.z, maxZ: box.max.z, }; } function paddedCutterShape(shape: WorkplaneShape): WorkplaneShape { const width = shapeWidth(shape) + CUTTER_PADDING * 2; const depth = shapeDepth(shape) + CUTTER_PADDING * 2; const height = shape.height + CUTTER_PADDING * 2; return { ...shape, width, depth, height, size: Math.max(width, depth), elevation: (shape.elevation ?? 0) - CUTTER_PADDING, baseRadius: shape.baseRadius ? shape.baseRadius + CUTTER_PADDING : shape.baseRadius, }; } function brushFromShape(shape: WorkplaneShape, cutter = false) { const brush = new Brush(geometryFromMeshData(meshForShape(cutter ? paddedCutterShape(shape) : shape))); brush.updateMatrixWorld(true); return brush; } function positiveCuboid(cuboid: Cuboid) { return cuboid.maxX - cuboid.minX > 0.01 && cuboid.maxY - cuboid.minY > 0.01 && cuboid.maxZ - cuboid.minZ > 0.01; } function subtractCuboid(source: Cuboid, cutter: Cuboid): Cuboid[] { const overlap = { minX: Math.max(source.minX, cutter.minX), maxX: Math.min(source.maxX, cutter.maxX), minY: Math.max(source.minY, cutter.minY), maxY: Math.min(source.maxY, cutter.maxY), minZ: Math.max(source.minZ, cutter.minZ), maxZ: Math.min(source.maxZ, cutter.maxZ), }; if (!positiveCuboid(overlap)) { return [source]; } return [ { ...source, maxX: overlap.minX }, { ...source, minX: overlap.maxX }, { minX: overlap.minX, maxX: overlap.maxX, minY: source.minY, maxY: source.maxY, minZ: source.minZ, maxZ: overlap.minZ }, { minX: overlap.minX, maxX: overlap.maxX, minY: source.minY, maxY: source.maxY, minZ: overlap.maxZ, maxZ: source.maxZ }, { minX: overlap.minX, maxX: overlap.maxX, minY: source.minY, maxY: overlap.minY, minZ: overlap.minZ, maxZ: overlap.maxZ }, { minX: overlap.minX, maxX: overlap.maxX, minY: overlap.maxY, maxY: source.maxY, minZ: overlap.minZ, maxZ: overlap.maxZ }, ].filter(positiveCuboid); } function cuboidsOverlap(a: Cuboid, b: Cuboid) { return ( Math.min(a.maxX, b.maxX) - Math.max(a.minX, b.minX) > 0.01 && Math.min(a.maxY, b.maxY) - Math.max(a.minY, b.minY) > 0.01 && Math.min(a.maxZ, b.maxZ) - Math.max(a.minZ, b.minZ) > 0.01 ); } function hasSolidHoleOverlap(solids: WorkplaneShape[], holes: WorkplaneShape[]) { const solidBounds = solids.map(meshAabb); const holeBounds = holes.map((hole) => meshAabb(paddedCutterShape(hole))); return solidBounds.some((solid) => holeBounds.some((hole) => cuboidsOverlap(solid, hole))); } function pointInsideCuboid(point: Vec3, cuboid: Cuboid, inset = -POINT_TOLERANCE) { const minX = cuboid.minX + inset; const maxX = cuboid.maxX - inset; const minY = cuboid.minY + inset; const maxY = cuboid.maxY - inset; const minZ = cuboid.minZ + inset; const maxZ = cuboid.maxZ - inset; return ( minX <= maxX && minY <= maxY && minZ <= maxZ && point[0] >= minX && point[0] <= maxX && point[1] >= minY && point[1] <= maxY && point[2] >= minZ && point[2] <= maxZ ); } function pointInsideHoleShape(point: Vec3, shape: WorkplaneShape, strictInterior = false) { if (shape.importedMesh || shape.groupedShapes?.length) { return pointInsideCuboid(point, meshAabb(shape), strictInterior ? CUTTER_RESIDUAL_INSET : -POINT_TOLERANCE); } const centerY = shape.height / 2; const inverse = new THREE.Matrix4() .makeRotationFromEuler( new THREE.Euler( THREE.MathUtils.degToRad(shape.rotationX ?? 0), THREE.MathUtils.degToRad(shape.rotation), THREE.MathUtils.degToRad(shape.rotationZ ?? 0), "XYZ", ), ) .invert(); const local = new THREE.Vector3(point[0] - shape.x, point[1] - (shape.elevation ?? 0) - centerY, point[2] - shape.z).applyMatrix4(inverse); const localY = local.y + centerY; const halfWidth = shapeWidth(shape) / 2; const halfDepth = shapeDepth(shape) / 2; if (strictInterior) { const yInset = Math.min(CUTTER_RESIDUAL_INSET, shape.height * 0.25); const xInset = Math.min(CUTTER_RESIDUAL_INSET, halfWidth * 0.25); const zInset = Math.min(CUTTER_RESIDUAL_INSET, halfDepth * 0.25); const innerHalfWidth = halfWidth - xInset; const innerHalfDepth = halfDepth - zInset; if (innerHalfWidth <= 0 || innerHalfDepth <= 0 || localY <= yInset || localY >= shape.height - yInset) { return false; } if (shape.kind === "cylinder" || shape.kind === "sphere" || shape.kind === "halfSphere" || shape.kind === "cone" || shape.kind === "torus" || shape.kind === "tube" || shape.kind === "ring") { const nx = local.x / Math.max(POINT_TOLERANCE, innerHalfWidth); const nz = local.z / Math.max(POINT_TOLERANCE, innerHalfDepth); return nx * nx + nz * nz < 1; } return Math.abs(local.x) < innerHalfWidth && Math.abs(local.z) < innerHalfDepth; } const insideHeight = localY >= -POINT_TOLERANCE && localY <= shape.height + POINT_TOLERANCE; if (!insideHeight) { return false; } if (shape.kind === "cylinder" || shape.kind === "sphere" || shape.kind === "halfSphere" || shape.kind === "cone" || shape.kind === "torus" || shape.kind === "tube" || shape.kind === "ring") { const nx = local.x / Math.max(POINT_TOLERANCE, halfWidth); const nz = local.z / Math.max(POINT_TOLERANCE, halfDepth); return nx * nx + nz * nz <= 1.0001; } return Math.abs(local.x) <= halfWidth + POINT_TOLERANCE && Math.abs(local.z) <= halfDepth + POINT_TOLERANCE; } function triangleCentroid([a, b, c]: Vec3[]): Vec3 { return [(a[0] + b[0] + c[0]) / 3, (a[1] + b[1] + c[1]) / 3, (a[2] + b[2] + c[2]) / 3]; } function midpoint(a: Vec3, b: Vec3): Vec3 { return [(a[0] + b[0]) / 2, (a[1] + b[1]) / 2, (a[2] + b[2]) / 2]; } function triangleAabb([a, b, c]: Vec3[]): Cuboid { return { minX: Math.min(a[0], b[0], c[0]), maxX: Math.max(a[0], b[0], c[0]), minY: Math.min(a[1], b[1], c[1]), maxY: Math.max(a[1], b[1], c[1]), minZ: Math.min(a[2], b[2], c[2]), maxZ: Math.max(a[2], b[2], c[2]), }; } function polygonAabb(points: Vec3[]): Cuboid { return points.reduce( (bounds, [x, y, z]) => ({ minX: Math.min(bounds.minX, x), maxX: Math.max(bounds.maxX, x), minY: Math.min(bounds.minY, y), maxY: Math.max(bounds.maxY, y), minZ: Math.min(bounds.minZ, z), maxZ: Math.max(bounds.maxZ, z), }), { minX: Number.POSITIVE_INFINITY, maxX: Number.NEGATIVE_INFINITY, minY: Number.POSITIVE_INFINITY, maxY: Number.NEGATIVE_INFINITY, minZ: Number.POSITIVE_INFINITY, maxZ: Number.NEGATIVE_INFINITY, }, ); } function cuboidsTouch(a: Cuboid, b: Cuboid, tolerance = 0.0001) { return ( Math.min(a.maxX, b.maxX) + tolerance >= Math.max(a.minX, b.minX) && Math.min(a.maxY, b.maxY) + tolerance >= Math.max(a.minY, b.minY) && Math.min(a.maxZ, b.maxZ) + tolerance >= Math.max(a.minZ, b.minZ) ); } function triangleTouchesHoleShape(triangle: Vec3[], hole: WorkplaneShape, holeBounds: Cuboid) { const bounds = triangleAabb(triangle); if (!cuboidsTouch(bounds, holeBounds)) { return false; } const [a, b, c] = triangle; const samples = [a, b, c, triangleCentroid(triangle), midpoint(a, b), midpoint(b, c), midpoint(c, a)]; if (samples.some((point) => pointInsideHoleShape(point, hole))) { return true; } // Imported STLs are often open triangle soups. A cutter can cross a small triangle // without catching any sampled point, so tiny overlapping triangles are clipped too. const triangleSpan = Math.max(bounds.maxX - bounds.minX, bounds.maxY - bounds.minY, bounds.maxZ - bounds.minZ); const cutterSpan = Math.max(holeBounds.maxX - holeBounds.minX, holeBounds.maxY - holeBounds.minY, holeBounds.maxZ - holeBounds.minZ); return triangleSpan <= cutterSpan * 0.35; } function cutterTouchedTriangleCount(mesh: MeshData, cutters: WorkplaneShape[]) { const cutterInfo = cutters.map((cutter) => ({ shape: cutter, bounds: meshAabb(cutter) })); return mesh.faces.reduce((total, [ai, bi, ci]) => { const triangle = [mesh.vertices[ai], mesh.vertices[bi], mesh.vertices[ci]]; return total + (cutterInfo.some((cutter) => triangleTouchesHoleShape(triangle, cutter.shape, cutter.bounds)) ? 1 : 0); }, 0); } function isAxisAlignedBoxCutter(shape: WorkplaneShape) { const rotation = Math.abs(normalizeDegrees(shape.rotation)); const rotationX = Math.abs(normalizeDegrees(shape.rotationX ?? 0)); const rotationZ = Math.abs(normalizeDegrees(shape.rotationZ ?? 0)); const straightY = rotation < 0.001 || Math.abs(rotation - 180) < 0.001 || Math.abs(rotation - 360) < 0.001; const straightX = rotationX < 0.001 || Math.abs(rotationX - 180) < 0.001 || Math.abs(rotationX - 360) < 0.001; const straightZ = rotationZ < 0.001 || Math.abs(rotationZ - 180) < 0.001 || Math.abs(rotationZ - 360) < 0.001; return shape.kind === "box" && straightX && straightY && straightZ; } type ClipPlane = { axis: 0 | 1 | 2; value: number; keepGreater: boolean }; function clipDistance(point: Vec3, plane: ClipPlane) { return plane.keepGreater ? point[plane.axis] - plane.value : plane.value - point[plane.axis]; } function interpolateVec3(a: Vec3, b: Vec3, t: number): Vec3 { return [a[0] + (b[0] - a[0]) * t, a[1] + (b[1] - a[1]) * t, a[2] + (b[2] - a[2]) * t]; } function clipPolygonByPlane(polygon: Vec3[], plane: ClipPlane, keepInside: boolean) { if (polygon.length < 3) { return []; } const clipped: Vec3[] = []; const isKept = (distance: number) => (keepInside ? distance >= -0.0001 : distance <= 0.0001); for (let i = 0; i < polygon.length; i += 1) { const current = polygon[i]; const next = polygon[(i + 1) % polygon.length]; const currentDistance = clipDistance(current, plane); const nextDistance = clipDistance(next, plane); const currentKept = isKept(currentDistance); const nextKept = isKept(nextDistance); if (currentKept) { clipped.push(current); } if (currentKept !== nextKept) { const denom = currentDistance - nextDistance; const t = Math.abs(denom) > 0.000001 ? currentDistance / denom : 0; clipped.push(interpolateVec3(current, next, t)); } } return clipped; } function subtractCuboidFromPolygon(polygon: Vec3[], cuboid: Cuboid) { const planes: ClipPlane[] = [ { axis: 0, value: cuboid.minX, keepGreater: true }, { axis: 0, value: cuboid.maxX, keepGreater: false }, { axis: 1, value: cuboid.minY, keepGreater: true }, { axis: 1, value: cuboid.maxY, keepGreater: false }, { axis: 2, value: cuboid.minZ, keepGreater: true }, { axis: 2, value: cuboid.maxZ, keepGreater: false }, ]; let pending = [polygon]; const outsidePieces: Vec3[][] = []; for (const plane of planes) { const nextPending: Vec3[][] = []; pending.forEach((piece) => { const outside = clipPolygonByPlane(piece, plane, false); if (outside.length >= 3) { outsidePieces.push(outside); } const inside = clipPolygonByPlane(piece, plane, true); if (inside.length >= 3) { nextPending.push(inside); } }); pending = nextPending; if (pending.length === 0) { break; } } return outsidePieces; } function triangulatePolygonToPositions(polygon: Vec3[], positions: number[]) { if (polygon.length < 3) { return; } const first = polygon[0]; for (let i = 1; i < polygon.length - 1; i += 1) { const b = polygon[i]; const c = polygon[i + 1]; positions.push(first[0], first[1], first[2], b[0], b[1], b[2], c[0], c[1], c[2]); } } function addQuadToPositions(positions: number[], a: Vec3, b: Vec3, c: Vec3, d: Vec3) { positions.push(a[0], a[1], a[2], b[0], b[1], b[2], c[0], c[1], c[2]); positions.push(a[0], a[1], a[2], c[0], c[1], c[2], d[0], d[1], d[2]); } type HoleWallSide = "minX" | "maxX" | "minZ" | "maxZ"; type HoleWallSegment = { a: Vec3; b: Vec3; minCross: number; maxCross: number; avgY: number; key: string }; function localCutWallBaseY(segments: HoleWallSegment[], minY: number, maxY: number) { const ys = segments .flatMap((segment) => [segment.a[1], segment.b[1], segment.avgY]) .filter((value) => value >= minY - 0.001 && value <= maxY + 0.001) .sort((a, b) => a - b); if (ys.length < 2) { return minY; } let largestGap = 0; let gapIndex = -1; const minimumGap = Math.max(0.25, (maxY - minY) * 0.08); for (let i = 1; i < ys.length; i += 1) { const gap = ys[i] - ys[i - 1]; if (gap > largestGap) { largestGap = gap; gapIndex = i; } } if (gapIndex > 0 && largestGap > minimumGap) { return ys[gapIndex - 1]; } return ys[Math.max(0, Math.floor(ys.length * 0.12))]; } function clipSegmentToRect(a: Vec3, b: Vec3, crossAxis: 0 | 1 | 2, crossMin: number, crossMax: number, minY: number, maxY: number): [Vec3, Vec3] | null { let t0 = 0; let t1 = 1; const clipRange = (start: number, end: number, min: number, max: number) => { const delta = end - start; if (Math.abs(delta) < 0.000001) { return start >= min - 0.0001 && start <= max + 0.0001; } const ta = (min - start) / delta; const tb = (max - start) / delta; t0 = Math.max(t0, Math.min(ta, tb)); t1 = Math.min(t1, Math.max(ta, tb)); return t0 <= t1 + 0.0001; }; if (!clipRange(a[crossAxis], b[crossAxis], crossMin, crossMax) || !clipRange(a[1], b[1], minY, maxY)) { return null; } const start = interpolateVec3(a, b, Math.max(0, Math.min(1, t0))); const end = interpolateVec3(a, b, Math.max(0, Math.min(1, t1))); return Math.hypot(start[0] - end[0], start[1] - end[1], start[2] - end[2]) > 0.01 ? [start, end] : null; } function trianglePlaneSegment(triangle: Vec3[], axis: 0 | 1 | 2, plane: number): [Vec3, Vec3] | null { const points: Vec3[] = []; const addPoint = (point: Vec3) => { if (!points.some((existing) => Math.hypot(existing[0] - point[0], existing[1] - point[1], existing[2] - point[2]) < 0.0001)) { points.push(point); } }; for (let i = 0; i < 3; i += 1) { const a = triangle[i]; const b = triangle[(i + 1) % 3]; const da = a[axis] - plane; const db = b[axis] - plane; if (Math.abs(da) <= 0.0001) { addPoint(a); } if (Math.abs(db) <= 0.0001) { addPoint(b); } if (da * db < -0.00000001) { addPoint(interpolateVec3(a, b, da / (da - db))); } } if (points.length < 2) { return null; } let best: [Vec3, Vec3] = [points[0], points[1]]; let bestDistance = 0; for (let i = 0; i < points.length; i += 1) { for (let j = i + 1; j < points.length; j += 1) { const distance = Math.hypot(points[i][0] - points[j][0], points[i][1] - points[j][1], points[i][2] - points[j][2]); if (distance > bestDistance) { bestDistance = distance; best = [points[i], points[j]]; } } } return bestDistance > 0.01 ? best : null; } function addLocalHoleWallSegments(positions: number[], sourceMesh: MeshData, hole: Cuboid, solidBounds: Cuboid, side: HoleWallSide) { const axis = side === "minX" || side === "maxX" ? 0 : 2; const crossAxis = axis === 0 ? 2 : 0; const plane = side === "minX" ? Math.max(hole.minX, solidBounds.minX) : side === "maxX" ? Math.min(hole.maxX, solidBounds.maxX) : side === "minZ" ? Math.max(hole.minZ, solidBounds.minZ) : Math.min(hole.maxZ, solidBounds.maxZ); const crossMin = axis === 0 ? Math.max(hole.minZ, solidBounds.minZ) : Math.max(hole.minX, solidBounds.minX); const crossMax = axis === 0 ? Math.min(hole.maxZ, solidBounds.maxZ) : Math.min(hole.maxX, solidBounds.maxX); const minY = Math.max(hole.minY, solidBounds.minY); const maxY = Math.min(hole.maxY, solidBounds.maxY); const crossLength = crossMax - crossMin; if (crossLength <= 0.01 || maxY - minY <= 0.01) { return; } const sideTolerance = Math.max(0.0001, Math.min(hole.maxX - hole.minX, hole.maxZ - hole.minZ) * 0.0001); const seen = new Set(); const segmentKey = (a: Vec3, b: Vec3) => { const toKey = (point: Vec3) => `${Math.round(point[0] * 1000)},${Math.round(point[1] * 1000)},${Math.round(point[2] * 1000)}`; const ak = toKey(a); const bk = toKey(b); return ak < bk ? `${ak}|${bk}` : `${bk}|${ak}`; }; const segments: HoleWallSegment[] = []; sourceMesh.faces.forEach(([ai, bi, ci]) => { const triangle = [sourceMesh.vertices[ai], sourceMesh.vertices[bi], sourceMesh.vertices[ci]]; const bounds = polygonAabb(triangle); const minSide = axis === 0 ? bounds.minX : bounds.minZ; const maxSide = axis === 0 ? bounds.maxX : bounds.maxZ; const minCross = crossAxis === 0 ? bounds.minX : bounds.minZ; const maxCross = crossAxis === 0 ? bounds.maxX : bounds.maxZ; if (maxSide < plane - sideTolerance || minSide > plane + sideTolerance || maxCross < crossMin || minCross > crossMax || bounds.maxY < hole.minY || bounds.minY > hole.maxY) { return; } const rawSegment = trianglePlaneSegment(triangle, axis, plane); if (!rawSegment) { return; } const clipped = clipSegmentToRect(rawSegment[0], rawSegment[1], crossAxis, crossMin, crossMax, minY, maxY); if (!clipped) { return; } const [a, b] = clipped; if (Math.max(a[1], b[1]) <= minY + 0.01) { return; } const key = segmentKey(a, b); if (seen.has(key)) { return; } seen.add(key); segments.push({ a, b, minCross: Math.min(a[crossAxis], b[crossAxis]), maxCross: Math.max(a[crossAxis], b[crossAxis]), avgY: (a[1] + b[1]) / 2, key, }); }); const yTolerance = Math.max(0.03, (maxY - minY) * 0.01); const baseY = Math.max(minY, Math.min(maxY, localCutWallBaseY(segments, minY, maxY))); const minimumCrossSpan = Math.max(0.04, crossLength * 0.002); segments.forEach((segment) => { if (segment.maxCross - segment.minCross < minimumCrossSpan || Math.max(segment.a[1], segment.b[1]) - baseY <= yTolerance) { return; } const baseA: Vec3 = [segment.a[0], baseY, segment.a[2]]; const baseB: Vec3 = [segment.b[0], baseY, segment.b[2]]; addQuadToPositions(positions, segment.a, segment.b, baseB, baseA); }); } function addBoxHoleInteriorFaces(positions: number[], hole: Cuboid, sourceMesh: MeshData, solidBounds: Cuboid) { const x0 = Math.max(hole.minX, solidBounds.minX); const x1 = Math.min(hole.maxX, solidBounds.maxX); const z0 = Math.max(hole.minZ, solidBounds.minZ); const z1 = Math.min(hole.maxZ, solidBounds.maxZ); if (x1 - x0 <= 0.01 || z1 - z0 <= 0.01) { return; } addLocalHoleWallSegments(positions, sourceMesh, hole, solidBounds, "minX"); addLocalHoleWallSegments(positions, sourceMesh, hole, solidBounds, "maxX"); addLocalHoleWallSegments(positions, sourceMesh, hole, solidBounds, "minZ"); addLocalHoleWallSegments(positions, sourceMesh, hole, solidBounds, "maxZ"); } function cuboidsToMesh(name: string, cuboids: Cuboid[], centerX: number, centerZ: number, baseY = 0): MeshData { const vertices: Vec3[] = []; const faces: [number, number, number][] = []; const uniqueSorted = (values: number[]) => values .slice() .sort((a, b) => a - b) .filter((value, index, sorted) => index === 0 || Math.abs(value - sorted[index - 1]) > 0.0001); const xs = uniqueSorted(cuboids.flatMap((cuboid) => [cuboid.minX, cuboid.maxX])); const ys = uniqueSorted(cuboids.flatMap((cuboid) => [cuboid.minY, cuboid.maxY])); const zs = uniqueSorted(cuboids.flatMap((cuboid) => [cuboid.minZ, cuboid.maxZ])); const filled = new Set(); const cellKey = (x: number, y: number, z: number) => `${x}:${y}:${z}`; for (let xi = 0; xi < xs.length - 1; xi += 1) { for (let yi = 0; yi < ys.length - 1; yi += 1) { for (let zi = 0; zi < zs.length - 1; zi += 1) { const cx = (xs[xi] + xs[xi + 1]) / 2; const cy = (ys[yi] + ys[yi + 1]) / 2; const cz = (zs[zi] + zs[zi + 1]) / 2; const inside = cuboids.some( (cuboid) => cx > cuboid.minX + 0.0001 && cx < cuboid.maxX - 0.0001 && cy > cuboid.minY + 0.0001 && cy < cuboid.maxY - 0.0001 && cz > cuboid.minZ + 0.0001 && cz < cuboid.maxZ - 0.0001, ); if (inside) { filled.add(cellKey(xi, yi, zi)); } } } } const isFilled = (x: number, y: number, z: number) => filled.has(cellKey(x, y, z)); const addQuad = (points: Vec3[]) => { const offset = vertices.length; vertices.push(...points); faces.push([offset, offset + 1, offset + 2], [offset, offset + 2, offset + 3]); }; for (let xi = 0; xi < xs.length - 1; xi += 1) { for (let yi = 0; yi < ys.length - 1; yi += 1) { for (let zi = 0; zi < zs.length - 1; zi += 1) { if (!isFilled(xi, yi, zi)) { continue; } const x0 = xs[xi] - centerX; const x1 = xs[xi + 1] - centerX; const y0 = ys[yi] - baseY; const y1 = ys[yi + 1] - baseY; const z0 = zs[zi] - centerZ; const z1 = zs[zi + 1] - centerZ; if (!isFilled(xi - 1, yi, zi)) addQuad([[x0, y0, z0], [x0, y0, z1], [x0, y1, z1], [x0, y1, z0]]); if (!isFilled(xi + 1, yi, zi)) addQuad([[x1, y0, z0], [x1, y1, z0], [x1, y1, z1], [x1, y0, z1]]); if (!isFilled(xi, yi - 1, zi)) addQuad([[x0, y0, z0], [x1, y0, z0], [x1, y0, z1], [x0, y0, z1]]); if (!isFilled(xi, yi + 1, zi)) addQuad([[x0, y1, z0], [x0, y1, z1], [x1, y1, z1], [x1, y1, z0]]); if (!isFilled(xi, yi, zi - 1)) addQuad([[x0, y0, z0], [x0, y1, z0], [x1, y1, z0], [x1, y0, z0]]); if (!isFilled(xi, yi, zi + 1)) addQuad([[x0, y0, z1], [x1, y0, z1], [x1, y1, z1], [x0, y1, z1]]); } } } return { name, vertices, faces }; } function booleanMeshShape(selection: WorkplaneShape[]): WorkplaneShape | null { const solids = selection.filter((shape) => !shape.hole && !shape.locked); const holes = selection.filter((shape) => shape.hole); if (solids.length === 0 || holes.length === 0) { return null; } try { const sourceTriangleCount = solids.reduce((total, solid) => total + meshForShape(solid).faces.length, 0); const overlappingCut = hasSolidHoleOverlap(solids, holes); const evaluator = new Evaluator(); evaluator.useGroups = false; evaluator.attributes = ["position", "normal"]; let result = brushFromShape(solids[0]); solids.slice(1).forEach((solid) => { result = evaluator.evaluate(result, brushFromShape(solid), ADDITION); }); holes.forEach((hole) => { result = evaluator.evaluate(result, brushFromShape(hole, true), SUBTRACTION); }); const resultPositions = positionsFromGeometryDrawRange(result.geometry); const groupBounds = boundsForPositions(resultPositions); if (!groupBounds) { return null; } const centerX = (groupBounds.minX + groupBounds.maxX) / 2; const centerZ = (groupBounds.minZ + groupBounds.maxZ) / 2; const minY = groupBounds.minY; const rawWidth = Math.max(MIN_SHAPE_DIMENSION, groupBounds.maxX - groupBounds.minX); const rawHeight = Math.max(MIN_SHAPE_DIMENSION, groupBounds.maxY - groupBounds.minY); const rawDepth = Math.max(MIN_SHAPE_DIMENSION, groupBounds.maxZ - groupBounds.minZ); const width = cleanModelDimension(rawWidth); const height = cleanModelDimension(rawHeight); const depth = cleanModelDimension(rawDepth); const positions: number[] = []; for (let i = 0; i < resultPositions.length; i += 3) { positions.push(resultPositions[i] - centerX, resultPositions[i + 1] - minY, resultPositions[i + 2] - centerZ); } const firstSolid = solids[0]; const nextTriangleCount = Math.floor(positions.length / 9); if (overlappingCut && Math.abs(nextTriangleCount - sourceTriangleCount) <= 1) { return null; } return { id: createLocalId("grouped-boolean"), name: "Group", kind: "mesh", color: firstSolid.color, x: centerX, z: centerZ, elevation: minY, size: Math.max(width, depth), width, depth, height, rotation: 0, importedMesh: { positions, baseWidth: rawWidth, baseDepth: rawDepth, baseHeight: rawHeight, triangleCount: nextTriangleCount, sourceFormat: "json", }, groupedBaseWidth: width, groupedBaseDepth: depth, groupedBaseHeight: height, groupedShapes: selection.map((shape) => cloneAsGroupChild(shape, centerX, centerZ, minY)), locked: false, hidden: false, }; } catch { return null; } } function resultGeometryToMeshShape( selection: WorkplaneShape[], solids: WorkplaneShape[], geometry: THREE.BufferGeometry, idPrefix: string, ): WorkplaneShape | null { const resultPositions = positionsFromGeometryDrawRange(geometry); const groupBounds = boundsForPositions(resultPositions); if (!groupBounds) { return null; } const centerX = (groupBounds.minX + groupBounds.maxX) / 2; const centerZ = (groupBounds.minZ + groupBounds.maxZ) / 2; const minY = groupBounds.minY; const rawWidth = Math.max(MIN_SHAPE_DIMENSION, groupBounds.maxX - groupBounds.minX); const rawHeight = Math.max(MIN_SHAPE_DIMENSION, groupBounds.maxY - groupBounds.minY); const rawDepth = Math.max(MIN_SHAPE_DIMENSION, groupBounds.maxZ - groupBounds.minZ); const width = cleanModelDimension(rawWidth); const height = cleanModelDimension(rawHeight); const depth = cleanModelDimension(rawDepth); const positions: number[] = []; for (let i = 0; i < resultPositions.length; i += 3) { positions.push(resultPositions[i] - centerX, resultPositions[i + 1] - minY, resultPositions[i + 2] - centerZ); } const firstSolid = solids[0]; return { id: createLocalId(idPrefix), name: "Group", kind: "mesh", color: firstSolid.color, x: centerX, z: centerZ, elevation: minY, size: Math.max(width, depth), width, depth, height, rotation: 0, importedMesh: { positions, baseWidth: rawWidth, baseDepth: rawDepth, baseHeight: rawHeight, triangleCount: Math.floor(positions.length / 9), sourceFormat: "json", }, groupedBaseWidth: width, groupedBaseDepth: depth, groupedBaseHeight: height, groupedShapes: selection.map((shape) => cloneAsGroupChild(shape, centerX, centerZ, minY)), locked: false, hidden: false, }; } function isUsableBooleanGroup(group: WorkplaneShape | null, sourceTriangleCount = 0, enforceMinimumTriangles = true) { if (!group?.importedMesh) { return false; } const positions = group.importedMesh.positions; const triangleCount = group.importedMesh.triangleCount; const dimensions = [group.width, group.height, group.depth, group.size, group.x, group.z, group.elevation ?? 0]; if (positions.length < 9 || triangleCount < 1 || positions.some((value) => !Number.isFinite(value)) || dimensions.some((value) => !Number.isFinite(value))) { return false; } const minTriangles = enforceMinimumTriangles && sourceTriangleCount > 0 ? Math.max(2, Math.min(48, Math.floor(sourceTriangleCount * 0.004))) : 2; return triangleCount >= minTriangles && group.width > 0.01 && group.height > 0.01 && group.depth > 0.01; } function looksLikeUnchangedBooleanResult(group: WorkplaneShape | null, sourceTriangleCount: number, requireChanged = true) { if (!group?.importedMesh) { return true; } if (!requireChanged) { return false; } const sameTriangles = Math.abs(group.importedMesh.triangleCount - sourceTriangleCount) <= 1; return sameTriangles; } function shapeContainsImportedMesh(shape: WorkplaneShape): boolean { return Boolean(shape.importedMesh) || Boolean(shape.groupedShapes?.some(shapeContainsImportedMesh)); } function shapeIsImportedHole(shape: WorkplaneShape): boolean { return Boolean(shape.hole) && shapeContainsImportedMesh(shape); } function coplanarRescueCutterShape(shape: WorkplaneShape): WorkplaneShape { if (!shapeIsImportedHole(shape) || hasNonZeroRotation(shape)) { return shape; } return { ...shape, rotation: shape.rotation + COPLANAR_BOOLEAN_RESCUE_DEGREES, rotationZ: (shape.rotationZ ?? 0) + COPLANAR_BOOLEAN_RESCUE_DEGREES, }; } function cloneAsGroupChild(shape: WorkplaneShape, centerX: number, centerZ: number, minY: number): WorkplaneShape { return { ...shape, id: createLocalId(`${shape.id}-group-child`), x: shape.x - centerX, z: shape.z - centerZ, elevation: (shape.elevation ?? 0) - minY, }; } function mergedSolidMeshData(solids: WorkplaneShape[]) { const mergedSolidMesh: MeshData = { name: "ImportedBooleanSource", vertices: [], faces: [] }; solids.forEach((solid) => { appendMeshData(mergedSolidMesh.vertices, mergedSolidMesh.faces, meshForShape(solid)); }); return mergedSolidMesh; } function meshDataToManifoldMesh(runtime: ManifoldToplevel, mesh: MeshData) { const vertProperties = new Float32Array(mesh.vertices.length * 3); mesh.vertices.forEach(([x, y, z], index) => { vertProperties[index * 3] = x; vertProperties[index * 3 + 1] = y; vertProperties[index * 3 + 2] = z; }); const triVerts = new Uint32Array(mesh.faces.length * 3); mesh.faces.forEach(([a, b, c], index) => { triVerts[index * 3] = a; triVerts[index * 3 + 1] = b; triVerts[index * 3 + 2] = c; }); const manifoldMesh = new runtime.Mesh({ numProp: 3, vertProperties, triVerts, tolerance: 0.0001, }); manifoldMesh.merge(); return manifoldMesh; } function boxBoundsToManifold(runtime: ManifoldToplevel, bounds: Cuboid, created: ManifoldSolid[]) { const width = bounds.maxX - bounds.minX; const height = bounds.maxY - bounds.minY; const depth = bounds.maxZ - bounds.minZ; if (width <= 0.0001 || height <= 0.0001 || depth <= 0.0001) { return null; } const box = runtime.Manifold.cube([width, height, depth]); created.push(box); const moved = box.translate([bounds.minX, bounds.minY, bounds.minZ]); if (moved !== box && moved) { created.push(moved); } return moved; } function trackManifold(created: ManifoldSolid[], value: T): T { if (value) { created.push(value); } return value; } function manifoldTransformFromMatrix(matrix: THREE.Matrix4) { return matrix.elements as unknown as Parameters[0]; } function shapeRotationQuaternion(shape: WorkplaneShape) { return new THREE.Quaternion().setFromEuler( new THREE.Euler( THREE.MathUtils.degToRad(shape.rotationX ?? 0), THREE.MathUtils.degToRad(meshYawDegrees(shape)), THREE.MathUtils.degToRad(shape.rotationZ ?? 0), "XYZ", ), ); } function primitiveTransformMatrix(shape: WorkplaneShape, scale: THREE.Vector3, alignRotation?: THREE.Euler) { const center = new THREE.Vector3(shape.x, (shape.elevation ?? 0) + shape.height / 2, shape.z); const matrix = new THREE.Matrix4().compose(center, shapeRotationQuaternion(shape), new THREE.Vector3(1, 1, 1)); if (alignRotation) { matrix.multiply(new THREE.Matrix4().makeRotationFromEuler(alignRotation)); } matrix.multiply(new THREE.Matrix4().makeScale(scale.x, scale.y, scale.z)); return matrix; } function transformedPrimitiveManifold(runtime: ManifoldToplevel, primitive: ManifoldSolid, matrix: THREE.Matrix4, created: ManifoldSolid[]) { trackManifold(created, primitive); return trackManifold(created, primitive.transform(manifoldTransformFromMatrix(matrix))); } function primitiveManifoldForShape(runtime: ManifoldToplevel, shape: WorkplaneShape, created: ManifoldSolid[]) { const width = shapeWidth(shape); const depth = shapeDepth(shape); const height = shape.height; if (width <= 0.0001 || depth <= 0.0001 || height <= 0.0001) { return null; } if (shape.kind === "box") { return transformedPrimitiveManifold(runtime, runtime.Manifold.cube(1, true), primitiveTransformMatrix(shape, new THREE.Vector3(width, height, depth)), created); } if (shape.kind === "sphere") { return transformedPrimitiveManifold( runtime, runtime.Manifold.sphere(1, shape.sides ?? 32), primitiveTransformMatrix(shape, new THREE.Vector3(width / 2, height / 2, depth / 2)), created, ); } if (shape.kind === "cylinder" || shape.kind === "cone") { const sides = shape.sides ?? 96; const topRadiusScale = shape.kind === "cone" ? shape.baseRadius ? (shape.topRadius ?? 0) / shape.baseRadius : 0 : 1; return transformedPrimitiveManifold( runtime, runtime.Manifold.cylinder(1, 1, topRadiusScale, sides, true), primitiveTransformMatrix(shape, new THREE.Vector3(width / 2, depth / 2, height), new THREE.Euler(-Math.PI / 2, 0, 0, "XYZ")), created, ); } return null; } function shapeToManifoldSolid(runtime: ManifoldToplevel, shape: WorkplaneShape, created: ManifoldSolid[], useBoxPrimitive = false) { if (useBoxPrimitive && isAxisAlignedBoxCutter(shape)) { return primitiveManifoldForShape(runtime, shape, created) ?? boxBoundsToManifold(runtime, meshAabb(shape), created); } const primitive = primitiveManifoldForShape(runtime, shape, created); if (primitive) { return primitive; } const mesh = meshDataToManifoldMesh(runtime, meshForShape(shape)); try { return runtime.Manifold.ofMesh(mesh); } finally { disposeManifold(mesh); } } function shapesToManifoldUnion(runtime: ManifoldToplevel, shapes: WorkplaneShape[], created: ManifoldSolid[], useBoxPrimitive = false) { const parts: ManifoldSolid[] = []; for (const shape of shapes) { const part = shapeToManifoldSolid(runtime, shape, created, useBoxPrimitive); if (!part || part.status() !== "NoError" || part.numTri() < 1) { disposeManifold(part); return null; } parts.push(part); created.push(part); } if (parts.length === 0) { return null; } if (parts.length === 1) { return parts[0]; } const union = runtime.Manifold.union(parts); created.push(union); return union.status() === "NoError" && union.numTri() > 0 ? union : null; } function manifoldMeshToPositions(mesh: InstanceType) { const positions: number[] = []; const numProp = mesh.numProp; for (let i = 0; i < mesh.triVerts.length; i += 1) { const vertexIndex = mesh.triVerts[i]; const offset = vertexIndex * numProp; positions.push(mesh.vertProperties[offset], mesh.vertProperties[offset + 1], mesh.vertProperties[offset + 2]); } return positions; } function positionsInteriorTriangleCount(positions: number[], cutters: WorkplaneShape[], strictInterior = false) { let count = 0; for (let i = 0; i + 8 < positions.length; i += 9) { const centroid: Vec3 = [ (positions[i] + positions[i + 3] + positions[i + 6]) / 3, (positions[i + 1] + positions[i + 4] + positions[i + 7]) / 3, (positions[i + 2] + positions[i + 5] + positions[i + 8]) / 3, ]; if (cutters.some((cutter) => pointInsideHoleShape(centroid, cutter, strictInterior))) { count += 1; } } return count; } function meshPositionsToGroupShape(selection: WorkplaneShape[], solids: WorkplaneShape[], positions: number[], idPrefix: string): WorkplaneShape | null { if (positions.length < 9) { return null; } let minX = Number.POSITIVE_INFINITY; let minY = Number.POSITIVE_INFINITY; let minZ = Number.POSITIVE_INFINITY; let maxX = Number.NEGATIVE_INFINITY; let maxY = Number.NEGATIVE_INFINITY; let maxZ = Number.NEGATIVE_INFINITY; for (let i = 0; i < positions.length; i += 3) { const x = positions[i]; const y = positions[i + 1]; const z = positions[i + 2]; minX = Math.min(minX, x); minY = Math.min(minY, y); minZ = Math.min(minZ, z); maxX = Math.max(maxX, x); maxY = Math.max(maxY, y); maxZ = Math.max(maxZ, z); } if (![minX, minY, minZ, maxX, maxY, maxZ].every(Number.isFinite)) { return null; } const centerX = (minX + maxX) / 2; const centerZ = (minZ + maxZ) / 2; const rawWidth = Math.max(MIN_SHAPE_DIMENSION, maxX - minX); const rawHeight = Math.max(MIN_SHAPE_DIMENSION, maxY - minY); const rawDepth = Math.max(MIN_SHAPE_DIMENSION, maxZ - minZ); const width = cleanModelDimension(rawWidth); const height = cleanModelDimension(rawHeight); const depth = cleanModelDimension(rawDepth); const normalizedPositions: number[] = []; for (let i = 0; i < positions.length; i += 3) { normalizedPositions.push(positions[i] - centerX, positions[i + 1] - minY, positions[i + 2] - centerZ); } const firstSolid = solids[0]; return { id: createLocalId(idPrefix), name: "Group", kind: "mesh", color: firstSolid.color, x: centerX, z: centerZ, elevation: minY, size: Math.max(width, depth), width, depth, height, rotation: 0, importedMesh: { positions: normalizedPositions, baseWidth: rawWidth, baseDepth: rawDepth, baseHeight: rawHeight, triangleCount: Math.floor(normalizedPositions.length / 9), sourceFormat: "json", }, groupedBaseWidth: width, groupedBaseDepth: depth, groupedBaseHeight: height, groupedShapes: selection.map((shape) => cloneAsGroupChild(shape, centerX, centerZ, minY)), locked: false, hidden: false, }; } function disposeManifold(value: unknown) { (value as { delete?: () => void } | null)?.delete?.(); } async function manifoldBooleanMeshShape(selection: WorkplaneShape[], options: { requireImported?: boolean; idPrefix?: string } = {}): Promise { // GROUPING SAFETY NOTE FOR FUTURE AGENTS: // Imported STL + hole grouping stays on exact boolean first. Rotated cutters // are validated against their real oriented volume, not their broad AABB. const solids = selection.filter((shape) => !shape.hole && !shape.locked); const holes = selection.filter((shape) => shape.hole); if (solids.length === 0 || holes.length === 0 || (options.requireImported !== false && !selection.some((shape) => Boolean(shape.importedMesh)))) { return null; } const sourceMesh = mergedSolidMeshData(solids); const cutterTriangleCount = holes.reduce((total, hole) => total + meshForShape(hole).faces.length, 0); if (sourceMesh.faces.length + cutterTriangleCount > IMPORTED_EXACT_BOOLEAN_TRIANGLE_LIMIT) { return null; } const cutterShapes = holes.map(paddedCutterShape); const residualValidationShapes = holes; const sourceInteriorTriangles = cutterInteriorTriangleCount(sourceMesh, cutterShapes); const sourceTouchedTriangles = cutterTouchedTriangleCount(sourceMesh, cutterShapes); const sourceCutTriangles = Math.max(sourceInteriorTriangles, sourceTouchedTriangles); const created: ManifoldSolid[] = []; let result: ManifoldSolid | null = null; try { const runtime = await getManifoldRuntime(); const solid = shapesToManifoldUnion(runtime, solids, created, true); const cutterSolid = shapesToManifoldUnion(runtime, holes.map(paddedCutterShape), created, true); if (!solid || !cutterSolid) { return null; } result = solid.subtract(cutterSolid); created.push(result); if (result.status() !== "NoError" || result.numTri() < 1) { return null; } const outputMesh = result.getMesh(); const positions = manifoldMeshToPositions(outputMesh); const resultChanged = positionsDifferFromMeshData(positions, sourceMesh); if (!resultChanged) { return null; } const hasImportedOperand = selection.some((shape) => Boolean(shape.importedMesh)); const canUseResidualInteriorValidation = !hasImportedOperand && holes.every((hole) => hole.kind === "box" && !hole.importedMesh && !hole.groupedShapes?.length); if (canUseResidualInteriorValidation) { const remainingInteriorTriangles = positionsInteriorTriangleCount(positions, residualValidationShapes, true); if (sourceCutTriangles > 0 && remainingInteriorTriangles > Math.max(12, Math.floor(sourceCutTriangles * 0.35))) { return null; } } const group = meshPositionsToGroupShape(selection, solids, positions, options.idPrefix ?? "grouped-manifold-cut"); const usable = isUsableBooleanGroup(group, sourceMesh.faces.length); const changedEnough = sourceCutTriangles > 0 || !looksLikeUnchangedBooleanResult(group, sourceMesh.faces.length, true); if (!usable || !changedEnough) { return null; } return group; } catch { return null; } finally { Array.from(new Set(created)).forEach(disposeManifold); } } async function manifoldUnionMeshShape(selection: WorkplaneShape[]): Promise { const solids = selection.filter((shape) => !shape.hole && !shape.locked); if (solids.length < 2 || !selection.some((shape) => Boolean(shape.importedMesh))) { return null; } const mergedSourceMesh = mergedSolidMeshData(solids); if (mergedSourceMesh.faces.length > IMPORTED_EXACT_BOOLEAN_TRIANGLE_LIMIT) { return null; } const created: ManifoldSolid[] = []; let result: ManifoldSolid | null = null; try { const runtime = await getManifoldRuntime(); result = shapesToManifoldUnion(runtime, solids, created, true); if (!result) { return null; } if (result.status() !== "NoError" || result.numTri() < 1) { return null; } const outputMesh = result.getMesh(); const positions = manifoldMeshToPositions(outputMesh); const group = meshPositionsToGroupShape(selection, solids, positions, "grouped-manifold-union"); return isUsableBooleanGroup(group, mergedSourceMesh.faces.length, false) ? group : null; } catch { return null; } finally { Array.from(new Set(created)).forEach(disposeManifold); } } function asIntersectionGroup(group: WorkplaneShape): WorkplaneShape { return { ...group, name: "Intersection", hole: false, }; } async function manifoldIntersectionMeshShape(selection: WorkplaneShape[]): Promise { const solids = selection.filter((shape) => !shape.hole && !shape.locked); const holes = selection.filter((shape) => shape.hole && !shape.locked); if (solids.length === 0 || holes.length === 0) { return { status: "unsupported" }; } const sourceTriangleCount = selection.reduce((total, shape) => total + meshForShape(shape).faces.length, 0); if (sourceTriangleCount > IMPORTED_EXACT_BOOLEAN_TRIANGLE_LIMIT) { return { status: "unsupported" }; } const created: ManifoldSolid[] = []; try { const runtime = await getManifoldRuntime(); const solid = shapesToManifoldUnion(runtime, solids, created, true); const hole = shapesToManifoldUnion(runtime, holes, created, true); if (!solid || !hole) { return { status: "unsupported" }; } const result = solid.intersect(hole); created.push(result); if (result.status() !== "NoError") { return { status: "unsupported" }; } if (result.numTri() < 1) { return { status: "empty" }; } const outputMesh = result.getMesh(); const positions = manifoldMeshToPositions(outputMesh); const group = meshPositionsToGroupShape(selection, solids, positions, "grouped-manifold-intersection"); return group && isUsableBooleanGroup(group, sourceTriangleCount, false) ? { status: "success", group: asIntersectionGroup(group) } : { status: "unsupported" }; } catch { return { status: "unsupported" }; } finally { Array.from(new Set(created)).forEach(disposeManifold); } } function bvhIntersectionMeshShape(selection: WorkplaneShape[], operation: CSGOperation, idPrefix: string): IntersectionAttempt { const solids = selection.filter((shape) => !shape.hole && !shape.locked); const holes = selection.filter((shape) => shape.hole && !shape.locked); if (solids.length === 0 || holes.length === 0) { return { status: "unsupported" }; } try { const evaluator = new Evaluator(); evaluator.useGroups = false; evaluator.attributes = ["position", "normal"]; (evaluator as Evaluator & { useCDTClipping: boolean }).useCDTClipping = true; let solidResult = brushFromShape(solids[0]); solids.slice(1).forEach((solid) => { solidResult = evaluator.evaluate(solidResult, brushFromShape(solid), ADDITION); solidResult.updateMatrixWorld(true); }); let holeResult = brushFromShape(holes[0]); holes.slice(1).forEach((hole) => { holeResult = evaluator.evaluate(holeResult, brushFromShape(hole), ADDITION); holeResult.updateMatrixWorld(true); }); const result = evaluator.evaluate(solidResult, holeResult, operation); result.updateMatrixWorld(true); if (positionsFromGeometryDrawRange(result.geometry).length < 9) { return { status: "empty" }; } const sourceTriangleCount = solids.reduce((total, solid) => total + meshForShape(solid).faces.length, 0); const group = resultGeometryToMeshShape(selection, solids, result.geometry, idPrefix); return group && isUsableBooleanGroup(group, sourceTriangleCount, false) ? { status: "success", group: asIntersectionGroup(group) } : { status: "unsupported" }; } catch { return { status: "unsupported" }; } } async function buildIntersectionShapeFromSelection(groupable: WorkplaneShape[]): Promise { const booleanSelection = expandGroupsForBoolean(groupable); const solids = booleanSelection.filter((shape) => !shape.hole && !shape.locked); const holes = booleanSelection.filter((shape) => shape.hole && !shape.locked); if (solids.length === 0 || holes.length === 0) { return { group: null, empty: false, failureNotice: "Select at least one solid and one hole for Intersection", }; } if (!hasSolidHoleOverlap(solids, holes)) { return { group: null, empty: true, failureNotice: "" }; } const manifoldAttempt = await manifoldIntersectionMeshShape(booleanSelection); if (manifoldAttempt.status === "success") { return { group: manifoldAttempt.group, empty: false, failureNotice: "" }; } if (manifoldAttempt.status === "empty") { return { group: null, empty: true, failureNotice: "" }; } const exactAttempt = bvhIntersectionMeshShape(booleanSelection, INTERSECTION, "grouped-intersection"); if (exactAttempt.status === "success") { return { group: exactAttempt.group, empty: false, failureNotice: "" }; } const hasImportedMesh = booleanSelection.some((shape) => Boolean(shape.importedMesh)); if (exactAttempt.status === "empty" && !hasImportedMesh) { return { group: null, empty: true, failureNotice: "" }; } const hollowAttempt = bvhIntersectionMeshShape(booleanSelection, HOLLOW_INTERSECTION, "grouped-hollow-intersection"); if (hollowAttempt.status === "success") { return { group: hollowAttempt.group, empty: false, failureNotice: "" }; } if (hollowAttempt.status === "empty" || exactAttempt.status === "empty") { return { group: null, empty: true, failureNotice: "" }; } return { group: null, empty: false, failureNotice: "Could not calculate this Intersection cleanly", }; } function cutterInteriorTriangleCount(mesh: MeshData, cutters: WorkplaneShape[]) { return mesh.faces.reduce((total, [ai, bi, ci]) => { const centroid = triangleCentroid([mesh.vertices[ai], mesh.vertices[bi], mesh.vertices[ci]]); return total + (cutters.some((cutter) => pointInsideHoleShape(centroid, cutter)) ? 1 : 0); }, 0); } function geometryInteriorTriangleCount(geometry: THREE.BufferGeometry, cutters: WorkplaneShape[], strictInterior = false) { const positions = positionsFromGeometryDrawRange(geometry); let count = 0; for (let i = 0; i + 8 < positions.length; i += 9) { const centroid: Vec3 = [ (positions[i] + positions[i + 3] + positions[i + 6]) / 3, (positions[i + 1] + positions[i + 4] + positions[i + 7]) / 3, (positions[i + 2] + positions[i + 5] + positions[i + 8]) / 3, ]; if (cutters.some((cutter) => pointInsideHoleShape(centroid, cutter, strictInterior))) { count += 1; } } return count; } function clearsImportedCutVolume(geometry: THREE.BufferGeometry, sourceInteriorTriangles: number, cutters: WorkplaneShape[]) { if (sourceInteriorTriangles <= 0 || cutters.length === 0) { return true; } const remainingInteriorTriangles = geometryInteriorTriangleCount(geometry, cutters, true); return remainingInteriorTriangles <= Math.max(4, Math.floor(sourceInteriorTriangles * 0.05)); } function importedBooleanMeshShape(selection: WorkplaneShape[]): WorkplaneShape | null { const solids = selection.filter((shape) => !shape.hole && !shape.locked); const holes = selection.filter((shape) => shape.hole); if (solids.length === 0 || holes.length === 0 || !selection.some((shape) => Boolean(shape.importedMesh))) { return null; } const mergedSolidMesh = mergedSolidMeshData(solids); const sourceTriangleCount = mergedSolidMesh.faces.length; const cutterTriangleCount = holes.reduce((total, hole) => total + meshForShape(hole).faces.length, 0); if (sourceTriangleCount + cutterTriangleCount > IMPORTED_EXACT_BOOLEAN_TRIANGLE_LIMIT) { return null; } const cutterShapes = holes.map(paddedCutterShape); const hasImportedHole = holes.some(shapeIsImportedHole); const hasStraightImportedHole = holes.some((hole) => shapeIsImportedHole(hole) && !hasNonZeroRotation(hole)); const sourceInteriorTriangles = cutterInteriorTriangleCount(mergedSolidMesh, cutterShapes); const sourceTouchedTriangles = cutterTouchedTriangleCount(mergedSolidMesh, cutterShapes); const sourceCutTriangles = Math.max(sourceInteriorTriangles, sourceTouchedTriangles); const baseAttempts: Array<{ operation: CSGOperation; idPrefix: string; rescueCoplanar?: boolean }> = [ // Imported STLs are often not watertight. Hollow subtraction still lets the hole bite into triangle meshes. { operation: HOLLOW_SUBTRACTION, idPrefix: "grouped-import-hollow-cut" }, { operation: SUBTRACTION, idPrefix: "grouped-import-cut" }, ]; const attempts = hasStraightImportedHole ? [ ...baseAttempts, { operation: HOLLOW_SUBTRACTION, idPrefix: "grouped-import-rescue-hollow-cut", rescueCoplanar: true }, { operation: SUBTRACTION, idPrefix: "grouped-import-rescue-cut", rescueCoplanar: true }, ] : baseAttempts; for (const attempt of attempts) { try { const evaluator = new Evaluator(); evaluator.useGroups = false; evaluator.attributes = ["position", "normal"]; (evaluator as Evaluator & { useCDTClipping: boolean }).useCDTClipping = true; let result = new Brush(geometryFromMeshData(mergedSolidMesh)); result.updateMatrixWorld(true); const operationHoles = attempt.rescueCoplanar ? holes.map(coplanarRescueCutterShape) : holes; operationHoles.forEach((hole) => { result = evaluator.evaluate(result, brushFromShape(hole, true), attempt.operation); result.updateMatrixWorld(true); }); const group = resultGeometryToMeshShape(selection, solids, result.geometry, attempt.idPrefix); const resultPositions = positionsFromGeometryDrawRange(result.geometry); const resultChanged = geometryDiffersFromMeshData(result.geometry, mergedSolidMesh); const hasOpenCutBoundary = hasImportedHole && introducesOpenCutBoundary(resultPositions, mergedSolidMesh, operationHoles.map(paddedCutterShape)); if ( isUsableBooleanGroup(group, sourceTriangleCount) && (sourceCutTriangles > 0 ? resultChanged : !looksLikeUnchangedBooleanResult(group, sourceTriangleCount, true)) && !hasOpenCutBoundary && clearsImportedCutVolume(result.geometry, sourceCutTriangles, operationHoles) ) { return group; } } catch { // Try the next boolean operation before giving up. } } return null; } function boxedBooleanMeshShape(selection: WorkplaneShape[]): WorkplaneShape | null { const solids = selection.filter((shape) => !shape.hole && shape.kind === "box" && !shape.locked); const holes = selection.filter((shape) => shape.hole && shape.kind === "box"); if (solids.length === 0 || holes.length === 0) { return null; } const cutters = holes.map((hole) => shapeAabb(paddedCutterShape(hole))); const cuboids = solids.flatMap((solid) => cutters.reduce((parts, cutter) => parts.flatMap((part) => subtractCuboid(part, cutter)), [shapeAabb(solid)])); if (cuboids.length === 0) { return null; } const groupBounds = boundsForCuboids(cuboids); const centerX = (groupBounds.minX + groupBounds.maxX) / 2; const centerZ = (groupBounds.minZ + groupBounds.maxZ) / 2; const width = Math.max(MIN_SHAPE_DIMENSION, groupBounds.maxX - groupBounds.minX); const minY = groupBounds.minY; const height = Math.max(MIN_SHAPE_DIMENSION, groupBounds.maxY - groupBounds.minY); const depth = Math.max(MIN_SHAPE_DIMENSION, groupBounds.maxZ - groupBounds.minZ); const mesh = cuboidsToMesh("Group", cuboids, centerX, centerZ, minY); const positions = mesh.faces.flatMap(([ai, bi, ci]) => [mesh.vertices[ai], mesh.vertices[bi], mesh.vertices[ci]]).flat(); const firstSolid = solids[0]; return { id: createLocalId("grouped-boolean"), name: "Group", kind: "mesh", color: firstSolid.color, x: centerX, z: centerZ, elevation: minY, size: Math.max(width, depth), width, depth, height, rotation: 0, importedMesh: { positions, baseWidth: width, baseDepth: depth, baseHeight: height, triangleCount: Math.floor(positions.length / 9), sourceFormat: "json", }, groupedBaseWidth: width, groupedBaseDepth: depth, groupedBaseHeight: height, groupedShapes: selection.map((shape) => cloneAsGroupChild(shape, centerX, centerZ, minY)), locked: false, hidden: false, }; } function aabbBooleanMeshShape(selection: WorkplaneShape[]): WorkplaneShape | null { const solids = selection.filter((shape) => !shape.hole && !shape.locked); const holes = selection.filter((shape) => shape.hole); if (solids.length === 0 || holes.length === 0) { return null; } const solidBounds = solids.map(meshAabb); const cutterBounds = holes.map((hole) => meshAabb(paddedCutterShape(hole))); const cuboids = solidBounds.flatMap((solid) => cutterBounds.reduce((parts, cutter) => parts.flatMap((part) => subtractCuboid(part, cutter)), [solid])); if (cuboids.length === 0) { return null; } const groupBounds = boundsForCuboids(cuboids); const centerX = (groupBounds.minX + groupBounds.maxX) / 2; const centerZ = (groupBounds.minZ + groupBounds.maxZ) / 2; const width = Math.max(MIN_SHAPE_DIMENSION, groupBounds.maxX - groupBounds.minX); const minY = groupBounds.minY; const height = Math.max(MIN_SHAPE_DIMENSION, groupBounds.maxY - groupBounds.minY); const depth = Math.max(MIN_SHAPE_DIMENSION, groupBounds.maxZ - groupBounds.minZ); const mesh = cuboidsToMesh("Group", cuboids, centerX, centerZ, minY); const positions = mesh.faces.flatMap(([ai, bi, ci]) => [mesh.vertices[ai], mesh.vertices[bi], mesh.vertices[ci]]).flat(); const firstSolid = solids[0]; return { id: createLocalId("grouped-boolean"), name: "Group", kind: "mesh", color: firstSolid.color, x: centerX, z: centerZ, elevation: minY, size: Math.max(width, depth), width, depth, height, rotation: 0, importedMesh: { positions, baseWidth: width, baseDepth: depth, baseHeight: height, triangleCount: Math.floor(positions.length / 9), sourceFormat: "json", }, groupedBaseWidth: width, groupedBaseDepth: depth, groupedBaseHeight: height, groupedShapes: selection.map((shape) => cloneAsGroupChild(shape, centerX, centerZ, minY)), locked: false, hidden: false, }; } function hollowClipMeshShape(selection: WorkplaneShape[]): WorkplaneShape | null { const solids = selection.filter((shape) => !shape.hole && !shape.locked); const holes = selection .filter((shape) => shape.hole) .map(paddedCutterShape) .map((shape) => ({ shape, bounds: meshAabb(shape) })); if (solids.length === 0 || holes.length === 0) { return null; } const sourceMesh = mergedSolidMeshData(solids); const sourceBounds = boundsForCuboids(solids.map(meshAabb)); const canPlaneClip = holes.every((hole) => isAxisAlignedBoxCutter(hole.shape)); const positions: number[] = []; let removedTriangles = 0; if (canPlaneClip) { sourceMesh.faces.forEach(([ai, bi, ci]) => { let fragments: Vec3[][] = [[sourceMesh.vertices[ai], sourceMesh.vertices[bi], sourceMesh.vertices[ci]]]; holes.forEach((hole) => { const nextFragments: Vec3[][] = []; fragments.forEach((fragment) => { if (!cuboidsTouch(polygonAabb(fragment), hole.bounds)) { nextFragments.push(fragment); return; } const clipped = subtractCuboidFromPolygon(fragment, hole.bounds); if ( clipped.length !== 1 || clipped[0].length !== fragment.length || clipped[0].some((point, index) => point.some((value, axis) => Math.abs(value - fragment[index][axis]) > 0.0001)) ) { removedTriangles += 1; } clipped.forEach((piece) => nextFragments.push(piece)); }); fragments = nextFragments; }); fragments.forEach((fragment) => triangulatePolygonToPositions(fragment, positions)); }); holes.forEach((hole) => addBoxHoleInteriorFaces(positions, hole.bounds, sourceMesh, sourceBounds)); } else { sourceMesh.faces.forEach(([ai, bi, ci]) => { const triangle = [sourceMesh.vertices[ai], sourceMesh.vertices[bi], sourceMesh.vertices[ci]]; if (holes.some((hole) => triangleTouchesHoleShape(triangle, hole.shape, hole.bounds))) { removedTriangles += 1; return; } triangle.forEach(([x, y, z]) => { positions.push(x, y, z); }); }); } let minX = Number.POSITIVE_INFINITY; let minY = Number.POSITIVE_INFINITY; let minZ = Number.POSITIVE_INFINITY; let maxX = Number.NEGATIVE_INFINITY; let maxY = Number.NEGATIVE_INFINITY; let maxZ = Number.NEGATIVE_INFINITY; for (let i = 0; i < positions.length; i += 3) { const x = positions[i]; const y = positions[i + 1]; const z = positions[i + 2]; minX = Math.min(minX, x); minY = Math.min(minY, y); minZ = Math.min(minZ, z); maxX = Math.max(maxX, x); maxY = Math.max(maxY, y); maxZ = Math.max(maxZ, z); } if (removedTriangles === 0 || positions.length < 9 || ![minX, minY, minZ, maxX, maxY, maxZ].every(Number.isFinite)) { return null; } const centerX = (minX + maxX) / 2; const centerZ = (minZ + maxZ) / 2; const rawWidth = Math.max(MIN_SHAPE_DIMENSION, maxX - minX); const rawHeight = Math.max(MIN_SHAPE_DIMENSION, maxY - minY); const rawDepth = Math.max(MIN_SHAPE_DIMENSION, maxZ - minZ); const width = cleanModelDimension(rawWidth); const height = cleanModelDimension(rawHeight); const depth = cleanModelDimension(rawDepth); const normalizedPositions: number[] = []; for (let i = 0; i < positions.length; i += 3) { normalizedPositions.push(positions[i] - centerX, positions[i + 1] - minY, positions[i + 2] - centerZ); } const firstSolid = solids[0]; return { id: createLocalId("grouped-import-clip"), name: "Group", kind: "mesh", color: firstSolid.color, x: centerX, z: centerZ, elevation: minY, size: Math.max(width, depth), width, depth, height, rotation: 0, importedMesh: { positions: normalizedPositions, baseWidth: rawWidth, baseDepth: rawDepth, baseHeight: rawHeight, triangleCount: Math.floor(normalizedPositions.length / 9), sourceFormat: "json", }, groupedBaseWidth: width, groupedBaseDepth: depth, groupedBaseHeight: height, groupedShapes: selection.map((shape) => cloneAsGroupChild(shape, centerX, centerZ, minY)), locked: false, hidden: false, }; } function cutFullyConsumesSolids(selection: WorkplaneShape[]) { const solids = selection.filter((shape) => !shape.hole && !shape.locked); const holes = selection.filter((shape) => shape.hole).map(paddedCutterShape); if (solids.length === 0 || holes.length === 0) { return false; } const sourceMesh = mergedSolidMeshData(solids); if (sourceMesh.faces.length === 0 || !hasSolidHoleOverlap(solids, holes)) { return false; } return sourceMesh.faces.every(([ai, bi, ci]) => { const triangle = [sourceMesh.vertices[ai], sourceMesh.vertices[bi], sourceMesh.vertices[ci]]; const centroid: Vec3 = [ (triangle[0][0] + triangle[1][0] + triangle[2][0]) / 3, (triangle[0][1] + triangle[1][1] + triangle[2][1]) / 3, (triangle[0][2] + triangle[1][2] + triangle[2][2]) / 3, ]; return holes.some((hole) => pointInsideHoleShape(centroid, hole)); }); } function mergedMeshShape(selection: WorkplaneShape[]): WorkplaneShape | null { const groupable = selection.filter((shape) => !shape.locked); if (groupable.length < 2) { return null; } // Keep imported STL/SVG groups as a baked mesh. The viewport child-group path rescales children to a wrapper box. const vertices: Vec3[] = []; const faces: [number, number, number][] = []; groupable.map(meshForShape).forEach((mesh) => { appendMeshData(vertices, faces, mesh); }); if (vertices.length < 3 || faces.length < 1) { return null; } let minX = Number.POSITIVE_INFINITY; let minY = Number.POSITIVE_INFINITY; let minZ = Number.POSITIVE_INFINITY; let maxX = Number.NEGATIVE_INFINITY; let maxY = Number.NEGATIVE_INFINITY; let maxZ = Number.NEGATIVE_INFINITY; vertices.forEach(([x, y, z]) => { minX = Math.min(minX, x); minY = Math.min(minY, y); minZ = Math.min(minZ, z); maxX = Math.max(maxX, x); maxY = Math.max(maxY, y); maxZ = Math.max(maxZ, z); }); if (![minX, minY, minZ, maxX, maxY, maxZ].every(Number.isFinite)) { return null; } const centerX = (minX + maxX) / 2; const centerZ = (minZ + maxZ) / 2; const rawWidth = Math.max(MIN_SHAPE_DIMENSION, maxX - minX); const rawHeight = Math.max(MIN_SHAPE_DIMENSION, maxY - minY); const rawDepth = Math.max(MIN_SHAPE_DIMENSION, maxZ - minZ); const width = cleanModelDimension(rawWidth); const height = cleanModelDimension(rawHeight); const depth = cleanModelDimension(rawDepth); const positions: number[] = []; faces.forEach(([ai, bi, ci]) => { [vertices[ai], vertices[bi], vertices[ci]].forEach(([x, y, z]) => { positions.push(x - centerX, y - minY, z - centerZ); }); }); const firstSolid = groupable.find((shape) => !shape.hole) ?? groupable[0]; const holeOnly = groupable.every((shape) => shape.hole); return { id: createLocalId("grouped-mesh"), name: "Group", kind: "mesh", color: holeOnly ? "#b8c2cc" : firstSolid.color, hole: holeOnly, x: centerX, z: centerZ, elevation: minY, size: Math.max(width, depth), width, depth, height, rotation: 0, importedMesh: { positions, baseWidth: rawWidth, baseDepth: rawDepth, baseHeight: rawHeight, triangleCount: faces.length, sourceFormat: "json", }, groupedBaseWidth: width, groupedBaseDepth: depth, groupedBaseHeight: height, groupedShapes: groupable.map((shape) => cloneAsGroupChild(shape, centerX, centerZ, minY)), locked: false, hidden: false, }; } function groupedShape(selection: WorkplaneShape[]): WorkplaneShape | null { const groupable = selection.filter((shape) => !shape.locked); if (groupable.length < 2) { return null; } const groupBounds = boundsForShapes(groupable); const minX = groupBounds.minX; const maxX = groupBounds.maxX; const minY = groupBounds.minY; const maxY = groupBounds.maxY; const minZ = groupBounds.minZ; const maxZ = groupBounds.maxZ; const centerX = (minX + maxX) / 2; const centerZ = (minZ + maxZ) / 2; const width = cleanModelDimension(Math.max(MIN_SHAPE_DIMENSION, maxX - minX)); const depth = cleanModelDimension(Math.max(MIN_SHAPE_DIMENSION, maxZ - minZ)); const height = cleanModelDimension(Math.max(MIN_SHAPE_DIMENSION, maxY - minY)); const firstSolid = groupable.find((shape) => !shape.hole) ?? groupable[0]; const holeOnly = groupable.every((shape) => shape.hole); return { id: createLocalId("group"), name: "Group", kind: "mesh", color: firstSolid.color, hole: holeOnly, x: centerX, z: centerZ, elevation: minY, size: Math.max(width, depth), width, depth, height, rotation: 0, groupedBaseWidth: width, groupedBaseDepth: depth, groupedBaseHeight: height, groupedShapes: groupable.map((shape) => cloneAsGroupChild(shape, centerX, centerZ, minY)), locked: false, hidden: false, }; } function localGroupBounds(children: WorkplaneShape[]): Cuboid { return boundsForShapes(children); } function quaternionForShape(shape: WorkplaneShape) { return new THREE.Quaternion().setFromEuler( new THREE.Euler( THREE.MathUtils.degToRad(shape.rotationX ?? 0), THREE.MathUtils.degToRad(shape.rotation), THREE.MathUtils.degToRad(shape.rotationZ ?? 0), "XYZ", ), ); } function rotationFromQuaternion(quaternion: THREE.Quaternion) { const euler = new THREE.Euler().setFromQuaternion(quaternion, "XYZ"); return { rotationX: cleanRotationDegrees(THREE.MathUtils.radToDeg(euler.x)), rotation: cleanRotationDegrees(THREE.MathUtils.radToDeg(euler.y)), rotationZ: cleanRotationDegrees(THREE.MathUtils.radToDeg(euler.z)), }; } function cleanShapePatch(patch: ShapeUpdatePatch): Partial { const { bakeTransform: _bakeTransform, ...rest } = patch; const next = { ...rest }; if (typeof next.rotation === "number") { next.rotation = cleanRotationDegrees(next.rotation, 1); } if (typeof next.rotationX === "number") { next.rotationX = cleanRotationDegrees(next.rotationX, 1); } if (typeof next.rotationZ === "number") { next.rotationZ = cleanRotationDegrees(next.rotationZ, 1); } return next; } function restoreGroupedChildren(group: WorkplaneShape): WorkplaneShape[] { const children = group.groupedShapes ?? []; if (children.length === 0) { return []; } const bounds = localGroupBounds(children); const baseWidth = group.groupedBaseWidth ?? Math.max(0.001, bounds.maxX - bounds.minX); const baseHeight = group.groupedBaseHeight ?? Math.max(0.001, bounds.maxY - bounds.minY); const baseDepth = group.groupedBaseDepth ?? Math.max(0.001, bounds.maxZ - bounds.minZ); const sx = shapeWidth(group) / Math.max(0.001, baseWidth); const sy = group.height / Math.max(0.001, baseHeight); const sz = shapeDepth(group) / Math.max(0.001, baseDepth); const groupQuaternion = quaternionForShape(group); const groupReflection = new THREE.Matrix4().makeScale(mirrorSign(group.mirrorX), mirrorSign(group.mirrorY), mirrorSign(group.mirrorZ)); const groupCenter = new THREE.Vector3(group.x, (group.elevation ?? 0) + group.height / 2, group.z); return children.map((child) => { const width = shapeWidth(child) * sx; const depth = shapeDepth(child) * sz; const height = child.height * sy; const localCenter = new THREE.Vector3( child.x * sx * mirrorSign(group.mirrorX), (((child.elevation ?? 0) + child.height / 2) * sy - group.height / 2) * mirrorSign(group.mirrorY), child.z * sz * mirrorSign(group.mirrorZ), ).applyQuaternion(groupQuaternion); const worldCenter = groupCenter.clone().add(localCenter); const childRotationMatrix = new THREE.Matrix4() .makeRotationFromQuaternion(groupQuaternion) .multiply(groupReflection) .multiply(new THREE.Matrix4().makeRotationFromQuaternion(quaternionForShape(child))) .multiply(groupReflection); const childRotation = rotationFromQuaternion(new THREE.Quaternion().setFromRotationMatrix(childRotationMatrix)); const restored: WorkplaneShape = { ...child, id: createLocalId(`${child.id}-ungroup`), x: worldCenter.x, z: worldCenter.z, elevation: worldCenter.y - height / 2, width, depth, height, size: (width + depth) / 2, rotation: childRotation.rotation, rotationX: childRotation.rotationX, rotationZ: childRotation.rotationZ, mirrorX: Boolean(child.mirrorX) !== Boolean(group.mirrorX) || undefined, mirrorY: Boolean(child.mirrorY) !== Boolean(group.mirrorY) || undefined, mirrorZ: Boolean(child.mirrorZ) !== Boolean(group.mirrorZ) || undefined, hidden: group.hidden ? true : child.hidden, }; return canonicalizeShape(group.hole ? withHoleMode(restored, true) : restored); }); } function expandGroupsForBoolean(selection: WorkplaneShape[]): WorkplaneShape[] { return selection.flatMap((shape) => { if (shape.importedMesh) { return [shape]; } return shape.groupedShapes?.length ? restoreGroupedChildren(shape) : [shape]; }); } function expandGroupsForBoxBoolean(selection: WorkplaneShape[]): WorkplaneShape[] { return selection.flatMap((shape) => (shape.groupedShapes?.length ? restoreGroupedChildren(shape) : [shape])); } function canUseBoxBoolean(selection: WorkplaneShape[]) { return selection.every(isAxisAlignedBoxCutter); } function hasNonZeroRotation(shape: WorkplaneShape) { const rotation = Math.abs(normalizeDegrees(shape.rotation)); const rotationX = Math.abs(normalizeDegrees(shape.rotationX ?? 0)); const rotationZ = Math.abs(normalizeDegrees(shape.rotationZ ?? 0)); return [rotation, rotationX, rotationZ].some((value) => value > 0.001 && Math.abs(value - 360) > 0.001); } async function buildGroupedShapeFromSelection(groupable: WorkplaneShape[]): Promise { const booleanSelection = expandGroupsForBoolean(groupable); const hasSolid = booleanSelection.some((shape) => !shape.hole); const hasHole = booleanSelection.some((shape) => shape.hole); const hasImportedMesh = booleanSelection.some((shape) => Boolean(shape.importedMesh)); const boxBooleanSelection = hasSolid && hasHole ? expandGroupsForBoxBoolean(groupable) : []; const cleanBoxGroup = canUseBoxBoolean(boxBooleanSelection) ? boxedBooleanMeshShape(boxBooleanSelection) : null; const manifoldCutGroup = hasSolid && hasHole ? await manifoldBooleanMeshShape(booleanSelection, { requireImported: false }) : null; const manifoldImportedMerge = hasImportedMesh && hasSolid && !hasHole ? await manifoldUnionMeshShape(booleanSelection) : null; const exactImportedGroup = hasImportedMesh && hasSolid && hasHole ? manifoldCutGroup ?? importedBooleanMeshShape(booleanSelection) : null; const bakedImportedMerge = hasImportedMesh && !(hasSolid && hasHole) ? manifoldImportedMerge ?? mergedMeshShape(booleanSelection) : null; const group = hasSolid && hasHole ? cleanBoxGroup ?? exactImportedGroup ?? (hasImportedMesh ? null : manifoldCutGroup ?? booleanMeshShape(booleanSelection)) : hasImportedMesh ? bakedImportedMerge ?? groupedShape(groupable) : groupedShape(groupable); const consumed = !group && hasSolid && hasHole && cutFullyConsumesSolids(booleanSelection); return { group, booleanSelection, hasSolid, hasHole, hasImportedMesh, consumed, failureNotice: hasImportedMesh && hasSolid && hasHole ? "Could not cut this imported mesh cleanly" : hasSolid && hasHole ? "Could not cut this selection" : "Could not group this selection", }; } function debugShapeSummary(shape: WorkplaneShape): Record { return { id: shape.id, name: shape.name, kind: shape.kind, hole: Boolean(shape.hole), x: Number(shape.x.toFixed(3)), z: Number(shape.z.toFixed(3)), elevation: Number((shape.elevation ?? 0).toFixed(3)), width: Number(shapeWidth(shape).toFixed(3)), depth: Number(shapeDepth(shape).toFixed(3)), height: Number(shape.height.toFixed(3)), rotation: Number(shape.rotation.toFixed(3)), rotationX: Number((shape.rotationX ?? 0).toFixed(3)), rotationZ: Number((shape.rotationZ ?? 0).toFixed(3)), mirrorX: Boolean(shape.mirrorX), mirrorY: Boolean(shape.mirrorY), mirrorZ: Boolean(shape.mirrorZ), importedTriangles: shape.importedMesh?.triangleCount ?? 0, imagePlate: shape.imagePlate ? `${shape.imagePlate.pixelWidth}x${shape.imagePlate.pixelHeight}` : null, edgeTreatments: shape.edgeTreatments ?? [], cadDisplayEdgeCount: shape.cadDisplayEdges?.length ?? null, cadDisplayEdgesVersion: shape.cadDisplayEdgesVersion ?? null, edgeResizeMode: shape.edgeResizeMode ?? "scale", cadBrepLength: shape.cadBrep?.length ?? 0, cadPrimitiveKind: shape.cadPrimitiveFrame?.kind ?? null, groupedCount: shape.groupedShapes?.length ?? 0, children: shape.groupedShapes?.map(debugShapeSummary) ?? [], }; } function compactShapeSummary(shape: WorkplaneShape, index: number) { const childSummary = shape.groupedShapes ?.map((child) => `${child.kind}${child.hole ? "H" : "S"}${child.importedMesh ? "I" : ""}`) .join("+"); return [ `${index}:${shape.kind}${shape.hole ? "H" : "S"}${shape.importedMesh ? "I" : ""}${shape.imagePlate ? "P" : ""}`, `g${shape.groupedShapes?.length ?? 0}`, `tri${shape.importedMesh?.triangleCount ?? 0}`, `edge${shape.edgeTreatments?.map((feature) => `${feature.kind}:${feature.amount}:${feature.edgeCount}:${feature.chamferAngle ?? ""}`).join("|") ?? ""}`, `viewEdges${shape.cadDisplayEdges?.length ?? "auto"}v${shape.cadDisplayEdgesVersion ?? 0}`, `edgeResize${shape.edgeResizeMode ?? "scale"}`, `brep${shape.cadBrep?.length ?? 0}`, `prim${shape.cadPrimitiveFrame ? `${shape.cadPrimitiveFrame.kind}:${shape.cadPrimitiveFrame.width}:${shape.cadPrimitiveFrame.depth}:${shape.cadPrimitiveFrame.height}` : ""}`, `p${Number(shape.x.toFixed(2))},${Number(shape.z.toFixed(2))},${Number((shape.elevation ?? 0).toFixed(2))}`, `d${Number(shapeWidth(shape).toFixed(2))}x${Number(shapeDepth(shape).toFixed(2))}x${Number(shape.height.toFixed(2))}`, `r${Number((shape.rotationX ?? 0).toFixed(1))},${Number(shape.rotation.toFixed(1))},${Number((shape.rotationZ ?? 0).toFixed(1))}`, `m${shape.mirrorX ? "x" : ""}${shape.mirrorY ? "y" : ""}${shape.mirrorZ ? "z" : ""}`, childSummary ? `c[${childSummary}]` : "c[]", ].join(","); } function mcpShapeSummary(shape: WorkplaneShape): SketchForgeMcpShapeSummary { return { id: shape.id, name: shape.name, kind: shape.kind, color: shape.color, hole: Boolean(shape.hole), locked: Boolean(shape.locked), hidden: Boolean(shape.hidden), position: { x: shape.x, z: shape.z, elevation: shape.elevation ?? 0, }, dimensions: { width: shapeWidth(shape), depth: shapeDepth(shape), height: shape.height, size: shape.size, }, rotation: { x: shape.rotationX ?? 0, y: shape.rotation, z: shape.rotationZ ?? 0, }, mirror: { x: Boolean(shape.mirrorX), y: Boolean(shape.mirrorY), z: Boolean(shape.mirrorZ), }, edgeTreatments: shape.edgeTreatments ?? [], groupedCount: shape.groupedShapes?.length ?? 0, importedTriangles: shape.importedMesh?.triangleCount ?? 0, cadDisplayEdgeCount: shape.cadDisplayEdges?.length ?? null, sketchPointCount: shape.sketchProfile?.points.length ?? 0, sketchSegmentCount: shape.sketchProfile?.segments.length ?? 0, children: shape.groupedShapes?.map(mcpShapeSummary), }; } function defaultMcpSketchProfile(width: number, depth: number): SketchProfile { const halfWidth = Math.max(0.01, width) / 2; const halfDepth = Math.max(0.01, depth) / 2; const pointIds = ["mcp-sketch-a", "mcp-sketch-b", "mcp-sketch-c", "mcp-sketch-d"].map((prefix) => createLocalId(prefix)); return { points: [ { id: pointIds[0], x: -halfWidth, z: -halfDepth, mode: "corner" }, { id: pointIds[1], x: halfWidth, z: -halfDepth, mode: "corner" }, { id: pointIds[2], x: halfWidth, z: halfDepth, mode: "corner" }, { id: pointIds[3], x: -halfWidth, z: halfDepth, mode: "corner" }, ], segments: [ { id: createLocalId("mcp-sketch-segment"), startId: pointIds[0], endId: pointIds[1], kind: "line" }, { id: createLocalId("mcp-sketch-segment"), startId: pointIds[1], endId: pointIds[2], kind: "line" }, { id: createLocalId("mcp-sketch-segment"), startId: pointIds[2], endId: pointIds[3], kind: "line" }, { id: createLocalId("mcp-sketch-segment"), startId: pointIds[3], endId: pointIds[0], kind: "line" }, ], images: [], }; } function mcpNumber(value: unknown, fallback: number) { return typeof value === "number" && Number.isFinite(value) ? value : fallback; } function mcpString(value: unknown, fallback: string) { return typeof value === "string" && value.trim() ? value : fallback; } function mcpStringArray(value: unknown): string[] { if (Array.isArray(value)) { return value.filter((entry): entry is string => typeof entry === "string" && entry.length > 0); } return typeof value === "string" && value.length > 0 ? [value] : []; } function mcpNumberArray(value: unknown): number[] { return Array.isArray(value) ? value.filter((entry): entry is number => typeof entry === "number" && Number.isInteger(entry)) : []; } function mcpFiniteNumberArray(value: unknown): number[] { return Array.isArray(value) ? value.filter((entry): entry is number => typeof entry === "number" && Number.isFinite(entry)) : []; } function readMcpEditorIdentity() { const storageKey = "sketchforge.mcp.editorIdentity"; try { const existing = JSON.parse(window.sessionStorage.getItem(storageKey) ?? "null") as { editorId?: unknown; editorNumber?: unknown } | null; if (typeof existing?.editorId === "string" && typeof existing.editorNumber === "number") { return { editorId: existing.editorId, editorNumber: existing.editorNumber }; } } catch { // Session identity is best-effort; fall through and create a new one. } const randomValues = new Uint32Array(1); window.crypto?.getRandomValues?.(randomValues); const editorNumber = 10000 + ((randomValues[0] || Math.floor(Math.random() * 90000)) % 90000); const editorId = window.crypto?.randomUUID?.() ?? `sketchforge-editor-${Date.now()}-${Math.random().toString(36).slice(2)}`; const identity = { editorId, editorNumber }; try { window.sessionStorage.setItem(storageKey, JSON.stringify(identity)); } catch { // Private browsing can block sessionStorage; the in-memory identity is enough for this tab. } return identity; } function projectShapesFingerprint(shapes: WorkplaneShape[]) { return shapes .map((shape, index) => [ compactShapeSummary(shape, index), `id${shape.id}`, `n${shape.name}`, `clr${shape.color}`, `txt${shape.text ?? ""}`, `screw${shape.screwHole ? JSON.stringify(shape.screwHole) : ""}`, `mesh${shape.importedMesh?.positions.length ?? 0}:${shape.importedMesh?.normals?.length ?? 0}`, `cadFrame${shape.cadBrepFrame ? JSON.stringify({ x: Number(shape.cadBrepFrame.x.toFixed(6)), z: Number(shape.cadBrepFrame.z.toFixed(6)), elevation: Number(shape.cadBrepFrame.elevation.toFixed(6)), width: Number(shape.cadBrepFrame.width.toFixed(6)), depth: Number(shape.cadBrepFrame.depth.toFixed(6)), height: Number(shape.cadBrepFrame.height.toFixed(6)), sourceTransform: shape.cadBrepFrame.sourceTransform?.map((value) => Number(value.toFixed(9))) ?? null, }) : ""}`, `primitiveFrame${shape.cadPrimitiveFrame ? JSON.stringify({ kind: shape.cadPrimitiveFrame.kind, width: Number(shape.cadPrimitiveFrame.width.toFixed(6)), depth: Number(shape.cadPrimitiveFrame.depth.toFixed(6)), height: Number(shape.cadPrimitiveFrame.height.toFixed(6)), frame: { x: Number(shape.cadPrimitiveFrame.frame.x.toFixed(6)), z: Number(shape.cadPrimitiveFrame.frame.z.toFixed(6)), elevation: Number(shape.cadPrimitiveFrame.frame.elevation.toFixed(6)), width: Number(shape.cadPrimitiveFrame.frame.width.toFixed(6)), depth: Number(shape.cadPrimitiveFrame.frame.depth.toFixed(6)), height: Number(shape.cadPrimitiveFrame.frame.height.toFixed(6)), sourceTransform: shape.cadPrimitiveFrame.frame.sourceTransform?.map((value) => Number(value.toFixed(9))) ?? null, }, }) : ""}`, `sketch${shape.sketchProfile ? JSON.stringify({ points: shape.sketchProfile.points, segments: shape.sketchProfile.segments, images: (shape.sketchProfile.images ?? []).map((image) => ({ id: image.id, name: image.name, x: image.x, z: image.z, width: image.width, depth: image.depth, opacity: image.opacity, lockAspect: image.lockAspect, sourceLength: image.dataUrl.length, })), }) : ""}`, ].join(","), ) .join(";"); } export function SketchForgeEditor({ initialShapes = [], initialSnap, initialWorkspace, onHome, onProjectShapesChange, onProjectSnapshot, onProjectWorkspaceChange, projectId, projectName = "SketchForge design", projectRevision = 0, }: { initialShapes?: WorkplaneShape[]; initialSnap?: GridSize; initialWorkspace?: WorkplaneWorkspaceSettings; onHome?: () => void; onProjectShapesChange?: (snapshot: { projectId: string; shapes: WorkplaneShape[] }) => void; onProjectSnapshot?: (snapshot: { image: string; projectId: string; shapes: number }) => void; onProjectWorkspaceChange?: (snapshot: { projectId: string; workspace: WorkplaneWorkspaceSettings; snap: GridSize }) => void; projectId?: string | null; projectName?: string; projectRevision?: number; } = {}) { const [shapes, setShapes] = useState(() => initialShapes.map(canonicalizeShape)); const [selectedIds, setSelectedIds] = useState([]); const [clipboard, setClipboard] = useState([]); const [systemClipboardSupported, setSystemClipboardSupported] = useState(false); const [history, setHistory] = useState([[]]); const [historyIndex, setHistoryIndex] = useState(0); const [placementElevation, setPlacementElevation] = useState(0); const [workspaceSettings, setWorkspaceSettings] = useState(() => normalizeWorkspaceSettings(initialWorkspace)); const [workplaneMode, setWorkplaneMode] = useState(false); const [menuOpen, setMenuOpen] = useState(false); const [topPanel, setTopPanel] = useState(null); const [screwHolePanelOpen, setScrewHolePanelOpen] = useState(false); const [screwHoleConfig, setScrewHoleConfig] = useState(DEFAULT_SCREW_HOLE_CONFIG); const [screwHoleTargetId, setScrewHoleTargetId] = useState(null); const [screwHolePlacementActive, setScrewHolePlacementActive] = useState(false); const [screwHolePlacedIds, setScrewHolePlacedIds] = useState([]); const [stepExporting, setStepExporting] = useState(false); const [alignMode, setAlignMode] = useState(false); const [alignAnchorId, setAlignAnchorId] = useState(null); const [alignPreview, setAlignPreview] = useState<{ axis: AlignAxis; target: AlignTarget } | null>(null); const [mirrorMode, setMirrorMode] = useState(false); const [mirrorPreviewAxis, setMirrorPreviewAxis] = useState(null); const [activeMode, setActiveMode] = useState("3D Design"); const [notice, setNotice] = useState("Ready"); const fileInputRef = useRef(null); const sketchImageInputRef = useRef(null); const booleanAutomationRunRef = useRef(null); const projectHydratingRef = useRef(false); const projectInteractionActiveRef = useRef(false); const pendingProjectShapesRef = useRef(null); const projectSyncTimerRef = useRef(null); const lastProjectShapesSyncRef = useRef(""); const lastProjectShapesEchoRef = useRef(null); const lastProjectIdRef = useRef(null); const projectSnapshotRunRef = useRef(0); const shapesRef = useRef(shapes); const selectedIdsRef = useRef(selectedIds); const workspaceSettingsRef = useRef(workspaceSettings); const noticeRef = useRef(notice); const projectInfoRef = useRef({ projectId: projectId ?? null, projectName }); const historyIndexRef = useRef(historyIndex); const interactionHistoryStartRef = useRef(""); const interactionHistoryChangedRef = useRef(false); const interactionHistoryTimerRef = useRef(null); const [projectInteractionActive, setProjectInteractionActive] = useState(false); const [toolbarMode, setToolbarMode] = useState("geometry"); const [sketchActive, setSketchActive] = useState(false); const [sketchTool, setSketchTool] = useState("line"); const [sketchProfile, setSketchProfile] = useState(() => emptySketchProfile()); const [sketchHistory, setSketchHistory] = useState([emptySketchProfile()]); const [sketchHistoryIndex, setSketchHistoryIndex] = useState(0); const [sketchActivePointId, setSketchActivePointId] = useState(null); const [sketchSelection, setSketchSelection] = useState(null); const [sketchMeasureStart, setSketchMeasureStart] = useState(null); const [sketchMeasurement, setSketchMeasurement] = useState(null); const [editingSketchShapeId, setEditingSketchShapeId] = useState(null); const [edgeModifier, setEdgeModifier] = useState(null); const edgeModifierRef = useRef(null); const cadModifierWorkerRef = useRef(null); const cadModifierPendingRef = useRef(new Map void; reject: (error: Error) => void; timer: number; }>()); const cadModifierRequestRef = useRef(0); const cadModifierPrepareRef = useRef(0); const cadModifierLatestPreviewRef = useRef(0); const cadModifierBaseShapeRef = useRef(null); const cadModifierBaseFingerprintRef = useRef(""); const cadModifierSourcePartsRef = useRef([]); const lastMcpErrorRef = useRef(null); const executeMcpCommandRef = useRef<((command: SketchForgeMcpCommand) => Promise) | null>(null); useEffect(() => { const worker = new Worker(new URL("../workers/cadModifier.worker.ts", import.meta.url), { type: "module" }); cadModifierWorkerRef.current = worker; worker.onmessage = (event: MessageEvent) => { const message = event.data; const pending = cadModifierPendingRef.current.get(message.requestId); if (pending) { window.clearTimeout(pending.timer); cadModifierPendingRef.current.delete(message.requestId); if (message.type === "error") { pending.reject(new Error(message.message)); } else { pending.resolve(message); } return; } if (message.type === "ready") { if (message.requestId !== cadModifierPrepareRef.current) return; setEdgeModifier((current) => current ? { ...current, edges: message.edges, selectedEdgeIds: [], busy: false, prepared: true, error: message.selectableEdgeIds.length ? null : "No sharp manifold edges were found at this threshold", } : current); if (message.selectableEdgeIds.length) setNotice("Select highlighted edges, then adjust the preview"); return; } if (message.type === "preview") { if (message.requestId !== cadModifierLatestPreviewRef.current) return; const base = cadModifierBaseShapeRef.current; const sourceParts = cadModifierSourcePartsRef.current.length ? cadModifierSourcePartsRef.current : (base ? [base] : []); const rawPreview = base ? shapeFromCadMesh(base, message.positions, message.normals, message.indices, message.brep) : null; const preview = rawPreview ? { ...rawPreview, cadDisplayEdges: cadDisplayEdgesForShape(rawPreview, message.displayEdges), cadDisplayEdgesVersion: 2 as const, } : null; const componentPreviews = cadModifierComponentPreviews(sourceParts, message.components); setEdgeModifier((current) => current ? { ...current, preview, componentPreviews, busy: false, error: preview ? null : "The CAD kernel returned an empty edge treatment", } : current); if (preview) setNotice("Edge treatment preview ready"); return; } if (message.type === "error") { if (message.requestId < cadModifierLatestPreviewRef.current) return; if (message.resetSession) { const requestId = cadModifierRequestRef.current + 1; cadModifierRequestRef.current = requestId; cadModifierLatestPreviewRef.current = requestId; cadModifierPrepareRef.current = requestId; cadModifierBaseShapeRef.current = null; cadModifierBaseFingerprintRef.current = ""; cadModifierSourcePartsRef.current = []; setEdgeModifier(null); setNotice(message.message); return; } setEdgeModifier((current) => current ? { ...current, busy: false, preview: null, error: message.message } : current); setNotice("Edge treatment needs adjustment"); } }; worker.onerror = () => { setEdgeModifier((current) => current ? { ...current, busy: false, preview: null, error: "The CAD worker could not start" } : current); }; return () => { cadModifierPendingRef.current.forEach((pending) => { window.clearTimeout(pending.timer); pending.reject(new Error("The CAD worker was closed")); }); cadModifierPendingRef.current.clear(); worker.terminate(); cadModifierWorkerRef.current = null; }; }, []); const invalidateCadModifierSession = useCallback(() => { const wasActive = cadModifierBaseShapeRef.current !== null; if (!wasActive) return false; const requestId = cadModifierRequestRef.current + 1; cadModifierRequestRef.current = requestId; cadModifierLatestPreviewRef.current = requestId; cadModifierPrepareRef.current = requestId; cadModifierWorkerRef.current?.postMessage({ type: "dispose", requestId } satisfies CadModifierWorkerRequest); cadModifierBaseShapeRef.current = null; cadModifierBaseFingerprintRef.current = ""; cadModifierSourcePartsRef.current = []; setEdgeModifier(null); return true; }, []); useEffect(() => { const warmBooleanRuntime = () => { void getManifoldRuntime().catch(() => { // Allow a real grouping action to retry if an idle preload was interrupted. manifoldRuntimePromise = null; }); }; if ("requestIdleCallback" in window) { const idleId = window.requestIdleCallback(warmBooleanRuntime, { timeout: 1500 }); return () => window.cancelIdleCallback(idleId); } const timer = globalThis.setTimeout(warmBooleanRuntime, 250); return () => globalThis.clearTimeout(timer); }, []); useEffect(() => { const applyTitles = () => { document.querySelectorAll("button").forEach((button) => { if (button.title) { return; } const label = button.getAttribute("aria-label") ?? button.textContent?.trim(); if (label) { button.title = label.replace(/\s+/g, " "); } }); }; applyTitles(); const observer = new MutationObserver(applyTitles); observer.observe(document.body, { childList: true, subtree: true, attributes: true, attributeFilter: ["aria-label"] }); return () => observer.disconnect(); }, []); useEffect(() => { setSystemClipboardSupported(Boolean(navigator.clipboard)); setClipboard(readSharedClipboard()); const onStorage = (event: StorageEvent) => { if (event.key === SHARED_CLIPBOARD_STORAGE_KEY) { setClipboard(readSharedClipboard()); } }; window.addEventListener("storage", onStorage); return () => window.removeEventListener("storage", onStorage); }, []); useEffect(() => { shapesRef.current = shapes; }, [shapes]); useEffect(() => { selectedIdsRef.current = selectedIds; }, [selectedIds]); useEffect(() => { workspaceSettingsRef.current = workspaceSettings; }, [workspaceSettings]); useEffect(() => { noticeRef.current = notice; }, [notice]); useEffect(() => { projectInfoRef.current = { projectId: projectId ?? null, projectName }; }, [projectId, projectName]); useEffect(() => { historyIndexRef.current = historyIndex; }, [historyIndex]); useEffect(() => { edgeModifierRef.current = edgeModifier; }, [edgeModifier]); useEffect(() => { setWorkspaceSettings(normalizeWorkspaceSettings(initialWorkspace)); }, [initialWorkspace]); const selectedShapes = useMemo(() => shapes.filter((shape) => selectedIds.includes(shape.id)), [selectedIds, shapes]); const selectedShape = selectedShapes.at(-1) ?? null; const hasSelection = selectedShapes.length > 0; const selectedScrewHoleTarget = useMemo( () => selectedShapes.length === 1 && selectedShape && !selectedShape.hole && !selectedShape.locked ? selectedShape : null, [selectedShape, selectedShapes.length], ); const screwHoleTarget = useMemo( () => screwHoleTargetId ? shapes.find((shape) => shape.id === screwHoleTargetId && !shape.hole && !shape.locked) ?? null : null, [screwHoleTargetId, shapes], ); const screwHolePlacement = useMemo(() => { if (!screwHolePlacementActive || !screwHoleTarget) return null; const depth = screwHoleConfig.depthMode === "through" ? Math.max(0.5, screwHoleTarget.height + 0.4) : screwHoleConfig.depth; return { config: { ...screwHoleConfig, depth }, elevation: (screwHoleTarget.elevation ?? 0) + screwHoleTarget.height - depth }; }, [screwHoleConfig, screwHolePlacementActive, screwHoleTarget]); const modifierAvailableEdgeIds = useMemo( () => edgeModifier ? edgeModifier.edges.filter((edge) => selectableCadModifierEdge(edge, edgeModifier.sharpAngle)).map((edge) => edge.id) : [], [edgeModifier?.edges, edgeModifier?.sharpAngle], ); const edgeModifierMaxAmount = useMemo(() => { const source = cadModifierBaseShapeRef.current ?? selectedShape; if (!source) return 10; const smallestDimension = Math.min(shapeWidth(source), shapeDepth(source), source.height); return Math.max(MIN_EDGE_MODIFIER_AMOUNT, smallestDimension * 0.99); }, [edgeModifier, selectedShape]); const selectedEdgeFeatureCount = useMemo(() => selectedShape ? edgeTreatmentFeatureCount(selectedShape) : 0, [selectedShape]); const selectedReversibleEdgeFeatureCount = useMemo(() => selectedShape ? reversibleEdgeTreatmentCount(selectedShape) : 0, [selectedShape]); const selectedEdgeHistoryOptions = useMemo(() => selectedShape ? edgeTreatmentHistoryOptions(selectedShape) : [], [selectedShape]); const canSeparateSelectedParts = useMemo( () => selectedShapes.length === 1 && Boolean(selectedShape && separablePartCount(selectedShape) > 1), [selectedShape, selectedShapes.length], ); const toggleModifierEdge = useCallback((id: number, singleEdge = false) => { setEdgeModifier((current) => { if (!current) return current; const allowed = new Set(current.edges.filter((edge) => selectableCadModifierEdge(edge, current.sharpAngle)).map((edge) => edge.id)); if (!allowed.has(id)) return current; const ids = current.tangentChain && !singleEdge ? tangentCadEdgeChain(current.edges, id, allowed) : [id]; const next = new Set(current.selectedEdgeIds); const remove = ids.every((edgeId) => next.has(edgeId)); ids.forEach((edgeId) => remove ? next.delete(edgeId) : next.add(edgeId)); return { ...current, selectedEdgeIds: [...next], preview: null, busy: next.size > 0, error: next.size ? null : "Select at least one highlighted edge" }; }); }, []); const exportableShapeCount = useMemo(() => (hasSelection ? selectedShapes : shapes).filter((shape) => !shape.hole).length, [hasSelection, selectedShapes, shapes]); const exportScopeLabel = hasSelection ? "selected" : "total"; const effectiveAlignAnchorId = useMemo( () => effectiveAlignmentAnchorId(selectedShapes, alignAnchorId), [alignAnchorId, selectedShapes], ); const alignHandleStatuses = useMemo(() => (alignMode ? alignmentStatuses(selectedShapes, effectiveAlignAnchorId) : []), [alignMode, effectiveAlignAnchorId, selectedShapes]); const viewportShapes = useMemo( () => edgeModifier?.preview && cadModifierBaseShapeRef.current ? shapes.map((shape) => shape.id === cadModifierBaseShapeRef.current?.id ? edgeModifier.preview as WorkplaneShape : shape) : alignMode && alignPreview ? alignedShapesForSelection(shapes, selectedIds, selectedShapes, effectiveAlignAnchorId, alignPreview.axis, alignPreview.target).nextShapes : mirrorMode && mirrorPreviewAxis ? mirroredShapesForSelection(shapes, selectedIds, selectedShapes, mirrorPreviewAxis).nextShapes : shapes, [alignMode, alignPreview, edgeModifier?.preview, effectiveAlignAnchorId, mirrorMode, mirrorPreviewAxis, selectedIds, selectedShapes, shapes], ); const debugState = useMemo( () => JSON.stringify({ notice, selectedIds, shapeCount: shapes.length, shapes: shapes.map(debugShapeSummary), }), [notice, selectedIds, shapes], ); const compactDebugState = useMemo( () => `notice=${notice};selected=${selectedIds.length};count=${shapes.length};${shapes.map(compactShapeSummary).join(";")}`, [notice, selectedIds, shapes], ); useEffect(() => { if (!projectId || !onProjectSnapshot || typeof window === "undefined") { return; } if (projectInteractionActive) { return; } const runId = projectSnapshotRunRef.current + 1; projectSnapshotRunRef.current = runId; const capture = () => { if (projectSnapshotRunRef.current !== runId) { return; } const image = window.sketchforgeCaptureCanvas?.(); if (image && image.length > 100) { onProjectSnapshot({ image, projectId, shapes: shapes.length }); } }; const firstTimer = window.setTimeout(capture, 850); const secondTimer = window.setTimeout(capture, 1500); return () => { window.clearTimeout(firstTimer); window.clearTimeout(secondTimer); }; }, [onProjectSnapshot, projectId, projectInteractionActive, shapes]); useEffect(() => { if (selectedShapes.length < 2) { setAlignMode(false); setAlignAnchorId(null); setAlignPreview(null); } if (alignAnchorId && !selectedIds.includes(alignAnchorId)) { setAlignAnchorId(null); setAlignPreview(null); } if (selectedShapes.length === 0) { setMirrorMode(false); setMirrorPreviewAxis(null); } }, [alignAnchorId, selectedIds, selectedShapes.length]); const syncProjectShapes = useCallback( (nextShapes: WorkplaneShape[]) => { if (!projectId || !onProjectShapesChange) { return; } if (projectInteractionActiveRef.current) { pendingProjectShapesRef.current = nextShapes.map(canonicalizeShape); if (projectSyncTimerRef.current !== null) { window.clearTimeout(projectSyncTimerRef.current); projectSyncTimerRef.current = null; } return; } const canonicalNext = nextShapes.map(canonicalizeShape); const serialized = projectShapesFingerprint(canonicalNext); if (lastProjectShapesSyncRef.current === serialized) { return; } if (projectSyncTimerRef.current !== null) { window.clearTimeout(projectSyncTimerRef.current); } projectSyncTimerRef.current = window.setTimeout(() => { lastProjectShapesSyncRef.current = serialized; lastProjectShapesEchoRef.current = serialized; onProjectShapesChange({ projectId, shapes: canonicalNext }); projectSyncTimerRef.current = null; }, 120); }, [onProjectShapesChange, projectId], ); const finalizeInteractionHistory = useCallback(() => { const startFingerprint = interactionHistoryStartRef.current; const hadChanges = interactionHistoryChangedRef.current; interactionHistoryStartRef.current = ""; interactionHistoryChangedRef.current = false; if (!hadChanges) { return; } const canonicalNext = shapesRef.current.map(canonicalizeShape); const nextFingerprint = projectShapesFingerprint(canonicalNext); if (!startFingerprint || startFingerprint === nextFingerprint) { return; } setHistory((current) => { const historyIndex = Math.min(historyIndexRef.current, Math.max(0, current.length - 1)); const trimmed = current.slice(0, historyIndex + 1); const latestHistory = trimmed.at(-1) ?? []; if (projectShapesFingerprint(latestHistory) === nextFingerprint) { historyIndexRef.current = Math.max(0, trimmed.length - 1); setHistoryIndex(historyIndexRef.current); return trimmed; } const nextHistory = [...trimmed, canonicalNext]; historyIndexRef.current = nextHistory.length - 1; setHistoryIndex(historyIndexRef.current); return nextHistory; }); }, []); useEffect(() => { if (projectInteractionActive || !pendingProjectShapesRef.current) { return; } const pendingShapes = pendingProjectShapesRef.current; pendingProjectShapesRef.current = null; const timer = window.setTimeout(() => syncProjectShapes(pendingShapes), 180); return () => window.clearTimeout(timer); }, [projectInteractionActive, syncProjectShapes]); const updateProjectInteractionActive = useCallback( (active: boolean) => { if (active) { if (interactionHistoryTimerRef.current !== null) { window.clearTimeout(interactionHistoryTimerRef.current); interactionHistoryTimerRef.current = null; } if (!projectInteractionActiveRef.current) { interactionHistoryStartRef.current = projectShapesFingerprint(shapesRef.current); interactionHistoryChangedRef.current = false; } projectInteractionActiveRef.current = true; setProjectInteractionActive((current) => (current ? current : true)); return; } projectInteractionActiveRef.current = false; setProjectInteractionActive((current) => (current ? false : current)); if (interactionHistoryTimerRef.current !== null) { window.clearTimeout(interactionHistoryTimerRef.current); } interactionHistoryTimerRef.current = window.setTimeout(() => { interactionHistoryTimerRef.current = null; finalizeInteractionHistory(); }, 0); }, [finalizeInteractionHistory], ); const updateProjectWorkspaceSettings = useCallback( (settings: { workspace: WorkplaneWorkspaceSettings; snap: GridSize }) => { setWorkspaceSettings(settings.workspace); if (!projectId || !onProjectWorkspaceChange) { return; } onProjectWorkspaceChange({ projectId, ...settings }); }, [onProjectWorkspaceChange, projectId], ); const commitShapes = useCallback( (next: WorkplaneShape[], nextSelection: string | string[] | null = selectedIds, message?: string) => { const canonicalNext = next.map(canonicalizeShape); const requestedSelection = Array.isArray(nextSelection) ? nextSelection : nextSelection ? [nextSelection] : []; const validSelection = requestedSelection.filter((id, index) => requestedSelection.indexOf(id) === index && canonicalNext.some((shape) => shape.id === id)); shapesRef.current = canonicalNext; setShapes(canonicalNext); setSelectedIds(validSelection); setHistory((current) => { const trimmed = current.slice(0, historyIndex + 1); historyIndexRef.current = trimmed.length; setHistoryIndex(historyIndexRef.current); return [...trimmed, canonicalNext]; }); if (message) { setNotice(message); } syncProjectShapes(canonicalNext); }, [historyIndex, selectedIds, syncProjectShapes], ); const removeEdgeTreatment = useCallback(async (optionId: string) => { if (!selectedShape) { setNotice("Select a shape with an edge feature first"); return; } if (selectedShape.locked) { setNotice("Unlock the shape before removing an edge feature"); return; } const option = selectedEdgeHistoryOptions.find((candidate) => candidate.id === optionId); if (!option) { setNotice("Choose an edge feature to remove"); return; } const restored = await restoreEdgeTreatmentInShape(selectedShape, option.path, option.entryId); if (!restored) { setNotice(selectedEdgeFeatureCount > 0 ? "This edge feature has no stored undo history" : "No edge feature to remove"); return; } invalidateCadModifierSession(); commitShapes( shapes.map((shape) => shape.id === selectedShape.id ? restored.shape : shape), restored.shape.id, `Removed ${restored.label}`, ); setNotice(`Removed ${restored.label}`); }, [commitShapes, invalidateCadModifierSession, selectedEdgeFeatureCount, selectedEdgeHistoryOptions, selectedShape, shapes]); const commitSketchProfile = useCallback( (next: SketchProfile, message?: string) => { const snapshot = cloneSketchProfile(next); setSketchProfile(snapshot); setSketchHistory((current) => { const trimmed = current.slice(0, sketchHistoryIndex + 1); return [...trimmed, cloneSketchProfile(snapshot)]; }); setSketchHistoryIndex(sketchHistoryIndex + 1); if (message) setNotice(message); }, [sketchHistoryIndex], ); const beginSketch = useCallback((profile?: SketchProfile, editingId: string | null = null) => { const initial = cloneSketchProfile(profile ?? emptySketchProfile()); setToolbarMode("sketch"); setSketchActive(true); setSketchTool(profile?.segments.length ? "select" : "line"); setSketchProfile(initial); setSketchHistory([cloneSketchProfile(initial)]); setSketchHistoryIndex(0); setSketchActivePointId(null); setSketchSelection(null); setSketchMeasureStart(null); setSketchMeasurement(null); setEditingSketchShapeId(editingId); setNotice(editingId ? "Editing sketch profile" : "Sketch started: place the first point"); }, []); const beginSketchEdit = useCallback(() => { if (selectedShapes.length !== 1 || !selectedShape?.sketchProfile) { setNotice("Select one shape created from a sketch to edit it"); return; } beginSketch(selectedShape.sketchProfile, selectedShape.id); }, [beginSketch, selectedShape, selectedShapes.length]); const cancelSketch = useCallback(() => { setSketchActive(false); setSketchActivePointId(null); setSketchSelection(null); setSketchMeasureStart(null); setSketchMeasurement(null); setEditingSketchShapeId(null); setNotice("Sketch cancelled"); }, []); const sketchUndo = useCallback(() => { if (sketchHistoryIndex <= 0) { setNotice("Nothing to undo in this sketch"); return; } const nextIndex = sketchHistoryIndex - 1; setSketchHistoryIndex(nextIndex); setSketchProfile(cloneSketchProfile(sketchHistory[nextIndex] ?? emptySketchProfile())); setSketchActivePointId(null); setSketchSelection(null); setNotice("Sketch undo"); }, [sketchHistory, sketchHistoryIndex]); const sketchRedo = useCallback(() => { if (sketchHistoryIndex >= sketchHistory.length - 1) { setNotice("Nothing to redo in this sketch"); return; } const nextIndex = sketchHistoryIndex + 1; setSketchHistoryIndex(nextIndex); setSketchProfile(cloneSketchProfile(sketchHistory[nextIndex] ?? emptySketchProfile())); setSketchActivePointId(null); setSketchSelection(null); setNotice("Sketch redo"); }, [sketchHistory, sketchHistoryIndex]); const setActiveSketchTool = useCallback((tool: SketchTool) => { setSketchTool(tool); setSketchActivePointId(null); setSketchSelection(null); if (tool !== "measure") setSketchMeasureStart(null); const messages: Record = { line: "Line: click points to draw straight segments", bezier: "Bézier: click and drag points to pull curve handles", smooth: "Smooth curve: click points to build a flowing path", select: "Select: edit sketch geometry or place and scale reference images", refine: "Refine: click a segment to add a point, or a point to remove it", erase: "Erase: click a point or segment to remove it", measure: "Measure: choose two points", }; setNotice(messages[tool]); }, []); const measureSketchPoint = useCallback( (point: SketchPoint) => { if (!sketchMeasureStart) { setSketchMeasureStart({ ...point }); setSketchMeasurement(null); setNotice("Choose the second measurement point"); return; } const measurement = { start: { ...sketchMeasureStart }, end: { ...point } }; setSketchMeasurement(measurement); setSketchMeasureStart(null); setNotice(`Measured ${Number(Math.hypot(measurement.end.x - measurement.start.x, measurement.end.z - measurement.start.z).toFixed(2))} mm`); }, [sketchMeasureStart], ); const clearSketchMeasurement = useCallback(() => { setSketchMeasureStart(null); setSketchMeasurement(null); setNotice("Sketch measurement removed"); }, []); const connectSketchPoint = useCallback( (pointId: string, profile = sketchProfile) => { if (!["line", "bezier", "smooth"].includes(sketchTool)) return profile; const curveKind = sketchTool as NonNullable; if (!sketchActivePointId) { setSketchActivePointId(pointId); setSketchSelection({ kind: "point", id: pointId }); return profile; } if (sketchActivePointId === pointId) return profile; const duplicate = profile.segments.some( (segment) => (segment.startId === sketchActivePointId && segment.endId === pointId) || (segment.startId === pointId && segment.endId === sketchActivePointId), ); const next = duplicate ? profile : { ...profile, segments: [...profile.segments, { id: createLocalId("sketch-segment"), startId: sketchActivePointId, endId: pointId, kind: curveKind }], }; const smoothed = sketchTool === "smooth" ? withSmoothSketchHandles(next) : next; const closed = orderedSketchPaths(smoothed).some((path) => path.closed && path.steps.some((step) => step.segment.startId === sketchActivePointId || step.segment.endId === sketchActivePointId)); if (!duplicate) commitSketchProfile(smoothed, closed ? "Profile closed—edit the path or finish the sketch" : "Sketch segment added"); setSketchActivePointId(closed ? null : pointId); setSketchSelection({ kind: "point", id: pointId }); if (closed) setSketchTool("select"); return smoothed; }, [commitSketchProfile, sketchActivePointId, sketchProfile, sketchTool], ); const addSketchPlanePoint = useCallback( (position: { x: number; z: number }, handles?: { handleIn: { x: number; z: number }; handleOut: { x: number; z: number } }) => { if (sketchTool === "measure") { measureSketchPoint({ id: "measure", ...position }); return; } if (!["line", "bezier", "smooth"].includes(sketchTool)) return; const curveKind = sketchTool as NonNullable; const existing = sketchProfile.points.find((point) => Math.hypot(point.x - position.x, point.z - position.z) < 0.0001); if (existing) { connectSketchPoint(existing.id); return; } const point: SketchPoint = { id: createLocalId("sketch-point"), ...position, ...(handles ?? {}), mode: sketchTool === "line" ? "corner" : sketchTool === "smooth" ? "smooth" : handles ? "smooth" : "corner", }; const next: SketchProfile = { ...sketchProfile, points: [...sketchProfile.points, point] }; if (sketchActivePointId) { next.segments = [...next.segments, { id: createLocalId("sketch-segment"), startId: sketchActivePointId, endId: point.id, kind: curveKind }]; } const prepared = sketchTool === "smooth" ? withSmoothSketchHandles(next) : next; commitSketchProfile(prepared, sketchActivePointId ? "Sketch point and segment added" : "Sketch point added"); setSketchActivePointId(point.id); setSketchSelection({ kind: "point", id: point.id }); }, [commitSketchProfile, connectSketchPoint, measureSketchPoint, sketchActivePointId, sketchProfile, sketchTool], ); const pressSketchPoint = useCallback( (id: string) => { const point = sketchProfile.points.find((entry) => entry.id === id); if (!point) return; if (sketchTool === "measure") { measureSketchPoint(point); setSketchSelection({ kind: "point", id }); return; } if (sketchTool === "select") { setSketchSelection({ kind: "point", id }); setSketchActivePointId(null); return; } connectSketchPoint(id); }, [connectSketchPoint, measureSketchPoint, sketchProfile.points, sketchTool], ); const deleteSketchPoint = useCallback( (id: string) => { const connected = sketchProfile.segments.filter((segment) => segment.startId === id || segment.endId === id); const neighboringIds = connected.map((segment) => segment.startId === id ? segment.endId : segment.startId); const remainingSegments = sketchProfile.segments.filter((segment) => segment.startId !== id && segment.endId !== id); if (neighboringIds.length === 2 && neighboringIds[0] !== neighboringIds[1]) { const duplicate = remainingSegments.some((segment) => (segment.startId === neighboringIds[0] && segment.endId === neighboringIds[1]) || (segment.startId === neighboringIds[1] && segment.endId === neighboringIds[0]), ); if (!duplicate) { remainingSegments.push({ id: createLocalId("sketch-segment"), startId: neighboringIds[0], endId: neighboringIds[1], kind: connected.every((segment) => segment.kind === "line") ? "line" : connected.some((segment) => segment.kind === "smooth") ? "smooth" : "bezier", }); } } const next = { ...sketchProfile, points: sketchProfile.points.filter((point) => point.id !== id), segments: remainingSegments, }; commitSketchProfile(next.segments.some((segment) => segment.kind === "smooth") ? withSmoothSketchHandles(next) : next, "Sketch point removed"); if (sketchActivePointId === id) setSketchActivePointId(null); setSketchSelection(null); }, [commitSketchProfile, sketchActivePointId, sketchProfile], ); const deleteSketchSegment = useCallback( (id: string) => { commitSketchProfile({ ...sketchProfile, segments: sketchProfile.segments.filter((segment) => segment.id !== id) }, "Sketch line removed"); setSketchActivePointId(null); setSketchSelection(null); }, [commitSketchProfile, sketchProfile], ); const updateSketchImage = useCallback((id: string, patch: Partial, message = "Sketch image updated") => { const image = (sketchProfile.images ?? []).find((entry) => entry.id === id); if (!image) return; commitSketchProfile({ ...sketchProfile, images: (sketchProfile.images ?? []).map((entry) => entry.id === id ? { ...entry, ...patch } : entry), }, message); setSketchSelection({ kind: "image", id }); setSketchActivePointId(null); }, [commitSketchProfile, sketchProfile]); const deleteSketchImage = useCallback((id: string) => { if (!(sketchProfile.images ?? []).some((image) => image.id === id)) return; commitSketchProfile({ ...sketchProfile, images: (sketchProfile.images ?? []).filter((image) => image.id !== id), }, "Sketch image removed"); setSketchSelection(null); }, [commitSketchProfile, sketchProfile]); const addSketchImageFile = useCallback(async (file: File) => { if (!sketchActive || sketchTool !== "select") { setNotice("Choose Select before adding a sketch image"); return; } if (!file.type.startsWith("image/")) { setNotice("Choose a PNG, JPG, WebP, GIF, or other image file"); return; } try { const prepared = await prepareImportedImage(file); const dimensions = imagePlateDimensions(prepared.pixelWidth, prepared.pixelHeight); const image: SketchImage = { id: createLocalId("sketch-image"), name: file.name.replace(/\.[^.]+$/, "") || "Sketch image", ...prepared, x: 0, z: 0, width: dimensions.width, depth: dimensions.depth, opacity: 0.55, lockAspect: true, }; commitSketchProfile({ ...sketchProfile, images: [...(sketchProfile.images ?? []), image] }, `Added ${file.name} to the sketch`); setSketchSelection({ kind: "image", id: image.id }); setSketchActivePointId(null); } catch (error) { setNotice(error instanceof Error ? error.message : "The sketch image could not be added"); } }, [commitSketchProfile, sketchActive, sketchProfile, sketchTool]); const deleteSelectedSketchEntity = useCallback(() => { if (!sketchSelection) { setNotice("Select a sketch point or segment to remove it"); return; } if (sketchSelection.kind === "point") deleteSketchPoint(sketchSelection.id); else if (sketchSelection.kind === "segment") deleteSketchSegment(sketchSelection.id); else if (sketchSelection.kind === "image") deleteSketchImage(sketchSelection.id); else { const pointIds = new Set(sketchSelection.pointIds); const segmentIds = new Set(sketchSelection.segmentIds); const imageIds = new Set(sketchSelection.imageIds ?? []); commitSketchProfile({ ...sketchProfile, points: sketchProfile.points.filter((point) => !pointIds.has(point.id)), segments: sketchProfile.segments.filter((segment) => !segmentIds.has(segment.id) && !pointIds.has(segment.startId) && !pointIds.has(segment.endId)), images: (sketchProfile.images ?? []).filter((image) => !imageIds.has(image.id)), }, "Selected sketch geometry removed"); setSketchActivePointId(null); setSketchSelection(null); } }, [commitSketchProfile, deleteSketchImage, deleteSketchPoint, deleteSketchSegment, sketchProfile, sketchSelection]); const moveSketchPoint = useCallback((id: string, position: { x: number; z: number }) => { const current = sketchProfile.points.find((point) => point.id === id); if (!current) return; const deltaX = position.x - current.x; const deltaZ = position.z - current.z; const next = { ...sketchProfile, points: sketchProfile.points.map((point) => point.id === id ? { ...point, ...position, handleIn: point.handleIn ? { x: point.handleIn.x + deltaX, z: point.handleIn.z + deltaZ } : undefined, handleOut: point.handleOut ? { x: point.handleOut.x + deltaX, z: point.handleOut.z + deltaZ } : undefined, } : point), }; commitSketchProfile(next, "Sketch point moved"); }, [commitSketchProfile, sketchProfile]); const moveSketchHandle = useCallback((id: string, handle: "in" | "out", position: { x: number; z: number }) => { const next = cloneSketchProfile(sketchProfile); const point = next.points.find((entry) => entry.id === id); if (!point) return; if (handle === "in") point.handleIn = { ...position }; else point.handleOut = { ...position }; if (point.mode === "smooth") { const opposite = { x: point.x * 2 - position.x, z: point.z * 2 - position.z }; if (handle === "in") point.handleOut = opposite; else point.handleIn = opposite; } commitSketchProfile(next, "Curve handle adjusted"); }, [commitSketchProfile, sketchProfile]); const setSketchPointMode = useCallback((id: string, mode: "corner" | "smooth" | "split") => { let next = cloneSketchProfile(sketchProfile); const point = next.points.find((entry) => entry.id === id); if (!point) return; point.mode = mode; if (mode === "corner") { point.handleIn = undefined; point.handleOut = undefined; next.segments = next.segments.map((segment) => segment.startId === id || segment.endId === id ? { ...segment, kind: "line" } : segment); } else { next.segments = next.segments.map((segment) => segment.startId === id || segment.endId === id ? { ...segment, kind: "bezier" } : segment); if (!point.handleIn || !point.handleOut) next = withSmoothSketchHandles(next); const updated = next.points.find((entry) => entry.id === id); if (updated) updated.mode = mode; } commitSketchProfile(next, mode === "corner" ? "Made corner" : mode === "smooth" ? "Made smooth" : "Curve handles split"); }, [commitSketchProfile, sketchProfile]); const insertSketchPoint = useCallback((segmentId: string, position: { x: number; z: number }) => { const segment = sketchProfile.segments.find((entry) => entry.id === segmentId); if (!segment) return; const point: SketchPoint = { id: createLocalId("sketch-point"), ...position, mode: segment.kind === "line" ? "corner" : "smooth" }; let next: SketchProfile = { ...sketchProfile, points: [...sketchProfile.points, point], segments: sketchProfile.segments.flatMap((entry) => entry.id === segmentId ? [ { ...entry, id: createLocalId("sketch-segment"), endId: point.id }, { ...entry, id: createLocalId("sketch-segment"), startId: point.id }, ] : [entry]), }; if (segment.kind === "smooth") next = withSmoothSketchHandles(next); commitSketchProfile(next, "Point added to path"); setSketchSelection({ kind: "point", id: point.id }); setSketchTool("select"); }, [commitSketchProfile, sketchProfile]); const finishSketch = useCallback(() => { const existing = editingSketchShapeId ? shapes.find((shape) => shape.id === editingSketchShapeId) ?? null : null; const height = existing?.height ?? 10; const extruded = shapeFromSketchProfile(sketchProfile, height, existing); if (!extruded) { setNotice("Close at least one profile before finishing the sketch"); return; } const nextShapes = existing ? shapes.map((shape) => (shape.id === existing.id ? extruded : shape)) : [...shapes, extruded]; commitShapes(nextShapes, extruded.id, existing ? "Sketch updated" : "Sketch created at 10 mm height"); setSketchActive(false); setEditingSketchShapeId(null); setToolbarMode("geometry"); }, [commitShapes, editingSketchShapeId, shapes, sketchProfile]); useEffect(() => { if (!projectId) { lastProjectIdRef.current = null; lastProjectShapesSyncRef.current = ""; lastProjectShapesEchoRef.current = null; return; } if (lastProjectIdRef.current !== projectId) { lastProjectIdRef.current = projectId; lastProjectShapesSyncRef.current = ""; lastProjectShapesEchoRef.current = null; } const incoming = initialShapes.map(canonicalizeShape); const incomingSerialized = projectShapesFingerprint(incoming); // The parent echoes shapes after a local save; rehydrating that echo can reset active transform state. if (lastProjectShapesEchoRef.current !== null && incomingSerialized === lastProjectShapesEchoRef.current) { lastProjectShapesSyncRef.current = incomingSerialized; return; } if (projectInteractionActiveRef.current) { return; } lastProjectShapesSyncRef.current = incomingSerialized; if (projectSyncTimerRef.current !== null) { window.clearTimeout(projectSyncTimerRef.current); projectSyncTimerRef.current = null; } if (incomingSerialized === projectShapesFingerprint(shapes)) { return; } projectHydratingRef.current = true; setShapes(incoming); setSelectedIds([]); setHistory([incoming]); setHistoryIndex(0); setNotice(incoming.length ? "Project synced" : "Ready"); }, [initialShapes, projectId, projectRevision]); useEffect(() => { if (!projectId || !onProjectShapesChange) { return; } if (projectHydratingRef.current) { projectHydratingRef.current = false; return; } syncProjectShapes(shapes); }, [onProjectShapesChange, projectId, shapes, syncProjectShapes]); useEffect(() => { return () => { if (projectSyncTimerRef.current !== null) { window.clearTimeout(projectSyncTimerRef.current); } if (interactionHistoryTimerRef.current !== null) { window.clearTimeout(interactionHistoryTimerRef.current); } }; }, []); const addShape = useCallback( (asset: ShapeAsset, point?: { x: number; z: number; elevation?: number }) => { const nextShape = makeShapeFromAsset(asset, point ?? { x: 0, z: 0, elevation: placementElevation }); commitShapes([...shapes, nextShape], nextShape.id, `${asset.name} added`); }, [commitShapes, placementElevation, shapes], ); const openScrewHolePanel = useCallback(() => { if (selectedShape?.screwHole) setScrewHoleConfig(selectedShape.screwHole); setScrewHolePanelOpen(true); setTopPanel(null); setMenuOpen(false); setEdgeModifier(null); }, [selectedShape]); const startScrewHolePlacement = useCallback(() => { const target = selectedScrewHoleTarget ?? screwHoleTarget; if (!target) { setNotice("Select one unlocked solid body before placing a screw hole"); return; } if (target.id !== screwHoleTargetId) setScrewHolePlacedIds([]); setScrewHoleTargetId(target.id); setScrewHolePlacementActive(true); setWorkplaneMode(false); setAlignMode(false); setMirrorMode(false); setEdgeModifier(null); setNotice(`Screw hole: click ${target.name} to place a cutter; Esc finishes`); }, [screwHoleTarget, screwHoleTargetId, selectedScrewHoleTarget]); const finishScrewHolePlacement = useCallback(() => { setScrewHolePlacementActive(false); if (screwHoleTargetId && screwHolePlacedIds.length) { const readyToGroup = [screwHoleTargetId, ...screwHolePlacedIds].filter((id) => shapesRef.current.some((shape) => shape.id === id)); selectedIdsRef.current = readyToGroup; setSelectedIds(readyToGroup); } setNotice(screwHolePlacedIds.length ? `${screwHolePlacedIds.length} screw hole${screwHolePlacedIds.length === 1 ? "" : "s"} ready to group` : "Screw hole placement finished"); }, [screwHolePlacedIds, screwHoleTargetId]); const closeScrewHolePanel = useCallback(() => { setScrewHolePlacementActive(false); setScrewHolePanelOpen(false); setScrewHoleTargetId(null); setScrewHolePlacedIds([]); }, []); const placeScrewHole = useCallback((point: { x: number; z: number }) => { if (!screwHoleTarget || !screwHolePlacement) { setScrewHolePlacementActive(false); setNotice("Screw hole placement stopped because its target is no longer available"); return; } const nextHole = createScrewHoleShape(screwHolePlacement.config, { ...point, elevation: screwHolePlacement.elevation }); commitShapes([...shapes, nextHole], nextHole.id, `Placed ${nextHole.name}`); setScrewHolePlacedIds((current) => [...current, nextHole.id]); setNotice(`Screw hole placed on ${screwHoleTarget.name}; its Hole mode is selected for editing`); }, [commitShapes, screwHolePlacement, screwHoleTarget, shapes]); useEffect(() => { if (!screwHolePlacementActive) return; const finishOnEscape = (event: KeyboardEvent) => { if (event.key === "Escape") { event.preventDefault(); finishScrewHolePlacement(); } }; window.addEventListener("keydown", finishOnEscape); return () => window.removeEventListener("keydown", finishOnEscape); }, [finishScrewHolePlacement, screwHolePlacementActive]); const updateShape = useCallback( (id: string, patch: ShapeUpdatePatch) => { const bakeTransform = Boolean(patch.bakeTransform); const cleanedPatch = cleanShapePatch(patch); const applyPatch = (current: WorkplaneShape[]) => { let changed = false; const next = current.map((shape) => { if (shape.id !== id) { return shape; } const patched = { ...shape, ...cleanedPatch }; const canonicalBase = canonicalizeShape("hole" in cleanedPatch ? withHoleMode(patched, Boolean(cleanedPatch.hole), cleanedPatch.color) : patched); const canonical = bakeTransform ? canonicalizeShape(bakeShapeTransformIntoMesh(canonicalBase)) : canonicalBase; if (workplaneShapesEqual(shape, canonical)) { return shape; } changed = true; return canonical; }); return { changed, next }; }; if (projectInteractionActiveRef.current) { setShapes((current) => { const { changed, next } = applyPatch(current); if (!changed) { return current; } interactionHistoryChangedRef.current = true; shapesRef.current = next; return next; }); return; } const { changed, next } = applyPatch(shapes); if (changed) { commitShapes(next, selectedIds); } }, [commitShapes, selectedIds, shapes], ); const deleteSelected = useCallback(() => { if (!hasSelection) { setNotice("Select a shape first"); return; } const selected = new Set(selectedIds); commitShapes( shapes.filter((shape) => !selected.has(shape.id)), [], `Deleted ${selected.size} selected shape${selected.size === 1 ? "" : "s"}`, ); }, [commitShapes, hasSelection, selectedIds, shapes]); const duplicateSelected = useCallback(() => { if (!hasSelection) { setNotice("Select a shape first"); return; } const duplicates = selectedShapes.map((shape) => ({ ...shape, id: createLocalId(`${shape.id}-copy`), x: Math.min(110, shape.x + 8), z: Math.min(110, shape.z + 8), })); commitShapes([...shapes, ...duplicates], duplicates.map((shape) => shape.id), `Duplicated ${duplicates.length} shape${duplicates.length === 1 ? "" : "s"}`); }, [commitShapes, hasSelection, selectedShapes, shapes]); const copySelected = useCallback(() => { if (!hasSelection) { setNotice("Select a shape first"); return; } setClipboard(selectedShapes); writeSharedClipboard(selectedShapes); setNotice(`Copied ${selectedShapes.length} shape${selectedShapes.length === 1 ? "" : "s"}`); }, [hasSelection, selectedShapes]); const pasteShape = useCallback(async () => { const systemClipboard = await readSystemClipboard(); const sharedClipboard = readSharedClipboard(); const sourceClipboard = systemClipboard.length > 0 ? systemClipboard : sharedClipboard.length > 0 ? sharedClipboard : clipboard; if (sourceClipboard.length === 0) { setNotice("SketchForge clipboard is empty"); return; } if (serializeShapesForSync(sourceClipboard) !== serializeShapesForSync(clipboard)) { setClipboard(sourceClipboard); } const pasted = sourceClipboard.map((shape) => ({ ...shape, id: createLocalId(`${shape.id}-paste`), x: Math.min(110, shape.x + 12), z: Math.min(110, shape.z + 12), })); commitShapes([...shapesRef.current, ...pasted], pasted.map((shape) => shape.id), `Pasted ${pasted.length} shape${pasted.length === 1 ? "" : "s"}`); }, [clipboard, commitShapes]); const undo = useCallback(() => { const modifierCancelled = invalidateCadModifierSession(); if (historyIndex <= 0) { setNotice(modifierCancelled ? "Edge modifier cancelled" : "Nothing to undo"); return; } const nextIndex = historyIndex - 1; const nextShapes = (history[nextIndex] ?? []).map(canonicalizeShape); historyIndexRef.current = nextIndex; shapesRef.current = nextShapes; setHistoryIndex(nextIndex); setShapes(nextShapes); setSelectedIds((current) => current.filter((id) => nextShapes.some((shape) => shape.id === id))); syncProjectShapes(nextShapes); setNotice(modifierCancelled ? "Edge modifier cancelled · Undo" : "Undo"); }, [history, historyIndex, invalidateCadModifierSession, syncProjectShapes]); const redo = useCallback(() => { if (historyIndex >= history.length - 1) { setNotice("Nothing to redo"); return; } const modifierCancelled = invalidateCadModifierSession(); const nextIndex = historyIndex + 1; const nextShapes = (history[nextIndex] ?? []).map(canonicalizeShape); historyIndexRef.current = nextIndex; shapesRef.current = nextShapes; setHistoryIndex(nextIndex); setShapes(nextShapes); setSelectedIds((current) => current.filter((id) => nextShapes.some((shape) => shape.id === id))); syncProjectShapes(nextShapes); setNotice(modifierCancelled ? "Edge modifier cancelled · Redo" : "Redo"); }, [history, historyIndex, invalidateCadModifierSession, syncProjectShapes]); const toggleAlignMode = useCallback(() => { if (selectedShapes.length < 2) { setNotice("Select at least two shapes to align"); return; } setAlignMode((active) => { const next = !active; setAlignPreview(null); if (next) { setMirrorMode(false); setMirrorPreviewAxis(null); } setNotice(next ? "Align: choose a dot, or click a selected shape to anchor it" : "Align cancelled"); return next; }); }, [selectedShapes.length]); const chooseAlignAnchor = useCallback( (id: string) => { if (!selectedIds.includes(id)) { return; } const shape = shapes.find((entry) => entry.id === id); const lockedAnchor = selectedShapes.find((entry) => entry.locked); if (lockedAnchor && lockedAnchor.id !== id) { setNotice(`Align anchor: ${lockedAnchor.name} (locked)`); return; } setAlignAnchorId(id); setAlignPreview(null); setNotice(shape ? `Align anchor: ${shape.name}` : "Align anchor set"); }, [selectedIds, selectedShapes, shapes], ); const alignSelectionTo = useCallback( (axis: AlignAxis, target: AlignTarget) => { if (selectedShapes.length < 2) { setNotice("Select at least two shapes to align"); return; } const { nextShapes, moved } = alignedShapesForSelection(shapes, selectedIds, selectedShapes, effectiveAlignAnchorId, axis, target); setAlignPreview(null); if (moved === 0) { setNotice("Already aligned"); return; } commitShapes(nextShapes, selectedIds, `Aligned ${moved} shape${moved === 1 ? "" : "s"} ${alignmentLabel(axis, target)}`); }, [commitShapes, effectiveAlignAnchorId, selectedIds, selectedShapes, shapes], ); const previewAlignSelection = useCallback((axis: AlignAxis, target: AlignTarget) => { setAlignPreview({ axis, target }); }, []); const clearAlignPreview = useCallback(() => { setAlignPreview(null); }, []); const toggleMirrorMode = useCallback(() => { if (!hasSelection) { setNotice("Select a shape first"); return; } setMirrorMode((active) => { const next = !active; setMirrorPreviewAxis(null); if (next) { setAlignMode(false); setAlignAnchorId(null); setAlignPreview(null); } setNotice(next ? "Mirror: choose an axis arrow" : "Mirror cancelled"); return next; }); }, [hasSelection]); const mirrorSelectionAcross = useCallback( (axis: AlignAxis) => { if (!hasSelection) { setNotice("Select a shape first"); return; } const { nextShapes, moved } = mirroredShapesForSelection(shapes, selectedIds, selectedShapes, axis); setMirrorPreviewAxis(null); if (moved === 0) { setNotice("Nothing to mirror"); return; } commitShapes(nextShapes, selectedIds, `Mirrored ${moved} shape${moved === 1 ? "" : "s"} ${mirrorAxisLabel(axis)}`); }, [commitShapes, hasSelection, selectedIds, selectedShapes, shapes], ); const previewMirrorSelection = useCallback((axis: AlignAxis) => { setMirrorPreviewAxis(axis); }, []); const clearMirrorPreview = useCallback(() => { setMirrorPreviewAxis(null); }, []); const postCadModifierRequest = useCallback((request: CadModifierWorkerPayload, transfer: Transferable[] = []) => { const requestId = cadModifierRequestRef.current + 1; cadModifierRequestRef.current = requestId; cadModifierWorkerRef.current?.postMessage({ ...request, requestId } as CadModifierWorkerRequest, transfer); return requestId; }, []); const postCadModifierRequestAsync = useCallback((request: CadModifierWorkerPayload, transfer: Transferable[] = [], timeoutMs = 20000) => { const worker = cadModifierWorkerRef.current; if (!worker) { return Promise.reject(new Error("The CAD worker is not ready")); } const requestId = cadModifierRequestRef.current + 1; cadModifierRequestRef.current = requestId; return new Promise((resolve, reject) => { const timer = window.setTimeout(() => { cadModifierPendingRef.current.delete(requestId); reject(new Error("Timed out waiting for the CAD worker")); }, timeoutMs); cadModifierPendingRef.current.set(requestId, { resolve, reject, timer }); worker.postMessage({ ...request, requestId } as CadModifierWorkerRequest, transfer); }); }, []); const cancelEdgeModifier = useCallback(() => { invalidateCadModifierSession(); setNotice("Edge modifier cancelled"); }, [invalidateCadModifierSession]); const startEdgeModifier = useCallback((kind: CadModifierKind) => { if (selectedShapes.length !== 1 || !selectedShape || selectedShape.locked || selectedShape.hole) { setNotice(`Select one unlocked solid to ${kind}`); return; } const appliedEdgeTreatmentCount = edgeTreatmentFeatureCount(selectedShape); const hasAppliedEdgeTreatment = Boolean(selectedShape.importedMesh && selectedShape.edgeTreatments?.length); const sourceParts = selectedShape.groupedShapes?.length && !hasAppliedEdgeTreatment ? restoreGroupedChildren(selectedShape) : [selectedShape]; const partInputs: Array<{ shape: WorkplaneShape; mesh?: MeshData; brep?: string; brepTransform?: number[]; primitive?: CadModifierPrimitivePart }> = sourceParts.map((shape) => { const frame = shape.cadBrepFrame; const preserveNeedsRetessellation = preservesEdgeTreatmentSize(shape) && Boolean(frame) && ( Math.abs(shapeWidth(shape) - (frame?.width ?? shapeWidth(shape))) > 1e-6 || Math.abs(shapeDepth(shape) - (frame?.depth ?? shapeDepth(shape))) > 1e-6 || Math.abs(shape.height - (frame?.height ?? shape.height)) > 1e-6 ); const primitive = cadModifierPrimitiveForShape(shape); if (primitive) return { shape, primitive }; return shape.cadBrep && frame && !preserveNeedsRetessellation ? { shape, brep: shape.cadBrep, brepTransform: cadBrepTransformForShape(shape) } : { shape, mesh: meshForShape(shape) }; }); const triangleCount = partInputs.reduce((total, part) => total + (part.mesh?.faces.length ?? 0), 0); if (triangleCount === 0 && partInputs.every((part) => !part.brep && !part.primitive)) { setNotice("The selected object has no printable surface"); return; } if (triangleCount > 180_000) { setNotice("This mesh is too dense for interactive edge treatment. Simplify it below 180,000 triangles first."); return; } const amount = Math.max(MIN_EDGE_MODIFIER_AMOUNT, Math.min(1, shapeWidth(selectedShape) / 6, shapeDepth(selectedShape) / 6, selectedShape.height / 6)); cadModifierBaseShapeRef.current = selectedShape; cadModifierBaseFingerprintRef.current = projectShapesFingerprint([selectedShape]); cadModifierSourcePartsRef.current = sourceParts; setAlignMode(false); setMirrorMode(false); setEdgeModifier({ kind, edges: [], selectedEdgeIds: [], amount, sharpAngle: 25, chamferAngle: 45, quality: "standard", tangentChain: true, preserveEdgeSize: selectedShape.edgeResizeMode === "preserve", busy: true, prepared: false, error: null, preview: null, componentPreviews: [], }); setNotice(`Preparing ${kind} edges in the CAD worker`); const parts: CadModifierMeshPart[] = partInputs.map((part) => { if (part.brep) return { brep: part.brep, brepTransform: part.brepTransform, hole: Boolean(part.shape.hole) }; if (part.primitive) return { primitive: part.primitive, hole: Boolean(part.shape.hole) }; return { ...meshDataToCadTransfer(part.mesh as MeshData), hole: Boolean(part.shape.hole) }; }); cadModifierPrepareRef.current = postCadModifierRequest({ type: "prepare", parts, sharpAngle: 25, suppressTreatmentDetailEdges: appliedEdgeTreatmentCount > 0, }, parts.flatMap((part) => part.positions && part.indices ? [part.positions.buffer, part.indices.buffer] : [])); }, [postCadModifierRequest, selectedShape, selectedShapes.length]); const prepareCadModifierForMcp = useCallback(async (shape: WorkplaneShape, sharpAngle: number) => { if (shape.locked || shape.hole) { throw new Error("Select one unlocked solid object for edge treatment"); } const appliedEdgeTreatmentCount = edgeTreatmentFeatureCount(shape); const hasAppliedEdgeTreatment = Boolean(shape.importedMesh && shape.edgeTreatments?.length); const sourceParts = shape.groupedShapes?.length && !hasAppliedEdgeTreatment ? restoreGroupedChildren(shape) : [shape]; const partInputs: Array<{ shape: WorkplaneShape; mesh?: MeshData; brep?: string; brepTransform?: number[]; primitive?: CadModifierPrimitivePart }> = sourceParts.map((partShape) => { const frame = partShape.cadBrepFrame; const preserveNeedsRetessellation = preservesEdgeTreatmentSize(partShape) && Boolean(frame) && ( Math.abs(shapeWidth(partShape) - (frame?.width ?? shapeWidth(partShape))) > 1e-6 || Math.abs(shapeDepth(partShape) - (frame?.depth ?? shapeDepth(partShape))) > 1e-6 || Math.abs(partShape.height - (frame?.height ?? partShape.height)) > 1e-6 ); const primitive = cadModifierPrimitiveForShape(partShape); if (primitive) return { shape: partShape, primitive }; return partShape.cadBrep && frame && !preserveNeedsRetessellation ? { shape: partShape, brep: partShape.cadBrep, brepTransform: cadBrepTransformForShape(partShape) } : { shape: partShape, mesh: meshForShape(partShape) }; }); const triangleCount = partInputs.reduce((total, part) => total + (part.mesh?.faces.length ?? 0), 0); if (triangleCount === 0 && partInputs.every((part) => !part.brep && !part.primitive)) { throw new Error("The selected object has no printable surface"); } if (triangleCount > 180_000) { throw new Error("This mesh is too dense for interactive edge treatment. Simplify it below 180,000 triangles first."); } const parts: CadModifierMeshPart[] = partInputs.map((part) => { if (part.brep) return { brep: part.brep, brepTransform: part.brepTransform, hole: Boolean(part.shape.hole) }; if (part.primitive) return { primitive: part.primitive, hole: Boolean(part.shape.hole) }; return { ...meshDataToCadTransfer(part.mesh as MeshData), hole: Boolean(part.shape.hole) }; }); const transfer = parts.flatMap((part) => part.positions && part.indices ? [part.positions.buffer as Transferable, part.indices.buffer as Transferable] : []); const response = await postCadModifierRequestAsync({ type: "prepare", parts, sharpAngle, suppressTreatmentDetailEdges: appliedEdgeTreatmentCount > 0, }, transfer); if (response.type !== "ready") { throw new Error("The CAD worker did not return an edge list"); } return { response, sourceParts }; }, [postCadModifierRequestAsync]); const applyCadModifierForMcp = useCallback(async ( shape: WorkplaneShape, params: Record, ) => { invalidateCadModifierSession(); const kind: CadModifierKind = params.kind === "fillet" ? "fillet" : "chamfer"; const sharpAngle = Math.max(1, Math.min(120, mcpNumber(params.sharpAngle, 25))); const amount = Math.max(MIN_EDGE_MODIFIER_AMOUNT, mcpNumber(params.amount, 1)); const chamferAngle = Math.max(5, Math.min(85, mcpNumber(params.chamferAngle, 45))); const quality: CadModifierQuality = params.quality === "draft" || params.quality === "fine" ? params.quality : "standard"; const preserveEdgeSize = typeof params.preserveEdgeSize === "boolean" ? params.preserveEdgeSize : shape.edgeResizeMode === "preserve"; const { response, sourceParts } = await prepareCadModifierForMcp(shape, sharpAngle); const selectableIds = response.edges.filter((edge) => selectableCadModifierEdge(edge, sharpAngle)).map((edge) => edge.id); const requestedIds = mcpNumberArray(params.edgeIds); const selectedEdgeIds = params.edgeIds === "all" || params.allEdges === true ? selectableIds : requestedIds.filter((edgeId) => selectableIds.includes(edgeId)); const missingIds = requestedIds.filter((edgeId) => !selectableIds.includes(edgeId)); if (selectedEdgeIds.length === 0) { throw new Error("Select at least one valid highlighted edge ID"); } if (missingIds.length > 0) { throw new Error(`These edge IDs are not selectable at the current threshold: ${missingIds.join(", ")}`); } const previewResponse = await postCadModifierRequestAsync({ type: "preview", kind, edgeIds: selectedEdgeIds, amount, quality, chamferAngle, }, [], 30000); if (previewResponse.type !== "preview") { throw new Error("The CAD worker did not return an edge preview"); } const rawPreview = shapeFromCadMesh(shape, previewResponse.positions, previewResponse.normals, previewResponse.indices, previewResponse.brep); if (!rawPreview) { throw new Error("The CAD kernel returned an empty edge treatment"); } const preview = canonicalizeShape({ ...rawPreview, cadDisplayEdges: cadDisplayEdgesForShape(rawPreview, previewResponse.displayEdges), cadDisplayEdgesVersion: 2 as const, }); const feature = { kind, amount, edgeCount: selectedEdgeIds.length, ...(kind === "chamfer" ? { chamferAngle } : {}), } satisfies NonNullable[number]; const session: EdgeModifierSession = { kind, edges: response.edges, selectedEdgeIds, amount, sharpAngle, chamferAngle, quality, tangentChain: false, preserveEdgeSize, busy: false, prepared: true, error: null, preview, componentPreviews: cadModifierComponentPreviews(sourceParts, previewResponse.components), }; const createdAt = Date.now(); const groupedModifiedShape = groupedShapeWithComponentEdgeTreatment(shape, preview, sourceParts, session, feature, createdAt); const modifiedShape = groupedModifiedShape ?? shapeWithEdgeTreatmentRecord(preview, shape, feature, preserveEdgeSize, createdAt); commitShapes( shapesRef.current.map((candidate) => candidate.id === shape.id ? modifiedShape : candidate), modifiedShape.id, `${kind === "fillet" ? "Filleted" : "Chamfered"} ${selectedEdgeIds.length} edge${selectedEdgeIds.length === 1 ? "" : "s"} by MCP`, ); return { object: mcpShapeSummary(modifiedShape), selectedEdgeIds, selectableEdgeIds: selectableIds, }; }, [commitShapes, invalidateCadModifierSession, prepareCadModifierForMcp, postCadModifierRequestAsync]); useEffect(() => { const base = cadModifierBaseShapeRef.current; if (!edgeModifier || !base) return; const current = shapes.find((shape) => shape.id === base.id); if (current && projectShapesFingerprint([current]) === cadModifierBaseFingerprintRef.current) return; invalidateCadModifierSession(); setNotice("Edge modifier cancelled because the object changed"); }, [edgeModifier, invalidateCadModifierSession, shapes]); const applyEdgeModifier = useCallback(() => { const base = cadModifierBaseShapeRef.current; if (!edgeModifier?.preview || !base) { setNotice("Wait for a valid edge preview before applying"); return; } const label = edgeModifier.kind === "fillet" ? "Filleted" : "Chamfered"; const feature = { kind: edgeModifier.kind, amount: edgeModifier.amount, edgeCount: edgeModifier.selectedEdgeIds.length, ...(edgeModifier.kind === "chamfer" ? { chamferAngle: edgeModifier.chamferAngle } : {}), } satisfies NonNullable[number]; const createdAt = Date.now(); const previewShape = canonicalizeShape({ ...edgeModifier.preview, cadDisplayEdges: edgeModifier.preview.cadDisplayEdges?.length ? edgeModifier.preview.cadDisplayEdges : cadDisplayEdgesAfterTreatment(edgeModifier.preview, edgeModifier), cadDisplayEdgesVersion: 2, }); const groupedModifiedShape = groupedShapeWithComponentEdgeTreatment( base, previewShape, cadModifierSourcePartsRef.current, edgeModifier, feature, createdAt, ); const modifiedShape: WorkplaneShape = groupedModifiedShape ?? shapeWithEdgeTreatmentRecord( previewShape, base, feature, edgeModifier.preserveEdgeSize, createdAt, ); commitShapes( shapes.map((shape) => shape.id === base.id ? modifiedShape : shape), base.id, `${label} ${edgeModifier.selectedEdgeIds.length} edge${edgeModifier.selectedEdgeIds.length === 1 ? "" : "s"}`, ); invalidateCadModifierSession(); }, [commitShapes, edgeModifier, invalidateCadModifierSession, shapes]); useEffect(() => { if (!edgeModifier) return; const onKeyDown = (event: KeyboardEvent) => { if (event.key === "Escape") { event.preventDefault(); cancelEdgeModifier(); } else if (event.key === "Enter" && edgeModifier.preview && !edgeModifier.busy && !edgeModifier.error) { event.preventDefault(); applyEdgeModifier(); } }; window.addEventListener("keydown", onKeyDown); return () => window.removeEventListener("keydown", onKeyDown); }, [applyEdgeModifier, cancelEdgeModifier, edgeModifier]); useEffect(() => { if (!edgeModifier?.prepared || edgeModifier.selectedEdgeIds.length === 0) return; const worker = cadModifierWorkerRef.current; if (!worker) return; const timer = window.setTimeout(() => { const requestId = postCadModifierRequest({ type: "preview", kind: edgeModifier.kind, edgeIds: edgeModifier.selectedEdgeIds, amount: edgeModifier.amount, quality: edgeModifier.quality, chamferAngle: edgeModifier.chamferAngle, }); cadModifierLatestPreviewRef.current = requestId; setEdgeModifier((current) => current ? { ...current, busy: true, error: null } : current); }, 120); return () => window.clearTimeout(timer); }, [edgeModifier?.amount, edgeModifier?.chamferAngle, edgeModifier?.kind, edgeModifier?.prepared, edgeModifier?.quality, edgeModifier?.selectedEdgeIds, postCadModifierRequest]); const snapSelected = useCallback(() => { if (!hasSelection) { setNotice("Select a shape first"); return; } const selected = new Set(selectedIds); const grid = 1; const snapValue = (value: number) => Math.round(value / grid) * grid; commitShapes( shapes.map((shape) => selected.has(shape.id) && !shape.locked ? { ...shape, x: snapValue(shape.x), z: snapValue(shape.z), elevation: snapValue(shape.elevation ?? 0), width: Math.max(grid, snapValue(shape.width)), depth: Math.max(grid, snapValue(shape.depth)), height: Math.max(grid, snapValue(shape.height)), size: Math.max(grid, snapValue(shape.size)), } : shape, ), selectedIds, `Snapped ${selectedShapes.length} shape${selectedShapes.length === 1 ? "" : "s"} to 1 mm grid`, ); }, [commitShapes, hasSelection, selectedIds, selectedShapes.length, shapes]); const toggleHidden = useCallback(() => { if (!hasSelection) { setNotice("Select a shape first"); return; } const selected = new Set(selectedIds); const shouldHide = selectedShapes.some((shape) => !shape.hidden); commitShapes( shapes.map((shape) => (selected.has(shape.id) && !shape.locked ? { ...shape, hidden: shouldHide } : shape)), selectedIds, shouldHide ? "Selection hidden" : "Selection visible", ); }, [commitShapes, hasSelection, selectedIds, selectedShapes, shapes]); const showHidden = useCallback(() => { const hiddenCount = shapes.filter((shape) => shape.hidden).length; if (hiddenCount === 0) { setNotice("No hidden shapes"); return; } commitShapes( shapes.map((shape) => ({ ...shape, hidden: false })), selectedIds, `Showed ${hiddenCount} hidden shape${hiddenCount === 1 ? "" : "s"}`, ); }, [commitShapes, selectedIds, shapes]); const toggleLocked = useCallback(() => { if (!hasSelection) { setNotice("Select a shape first"); return; } const selected = new Set(selectedIds); const shouldLock = selectedShapes.some((shape) => !shape.locked); commitShapes( shapes.map((shape) => (selected.has(shape.id) ? { ...shape, locked: shouldLock } : shape)), selectedIds, shouldLock ? "Selection locked" : "Selection unlocked", ); }, [commitShapes, hasSelection, selectedIds, selectedShapes, shapes]); const setSelectionHoleMode = useCallback( (hole: boolean) => { if (!hasSelection) { setNotice("Select a shape first"); return; } const selected = new Set(selectedIds); commitShapes( shapes.map((shape) => selected.has(shape.id) && !shape.locked ? withHoleMode(shape, hole) : shape, ), selectedIds, hole ? "Changed selection to hole" : "Changed selection to solid", ); }, [commitShapes, hasSelection, selectedIds, shapes], ); const cutSelected = useCallback(() => { if (!hasSelection) { setNotice("Select a shape first"); return; } const selected = new Set(selectedIds); setClipboard(selectedShapes); writeSharedClipboard(selectedShapes); commitShapes( shapes.filter((shape) => !selected.has(shape.id)), [], `Cut ${selectedShapes.length} shape${selectedShapes.length === 1 ? "" : "s"}`, ); }, [commitShapes, hasSelection, selectedIds, selectedShapes, shapes]); const raiseSelected = useCallback( (delta: number) => { if (!hasSelection) { return; } const selected = new Set(selectedIds); commitShapes( shapes.map((shape) => selected.has(shape.id) && !shape.locked ? { ...shape, elevation: Math.max(0, Math.min(180, (shape.elevation ?? 0) + delta)), } : shape, ), selectedIds, delta > 0 ? "Moved selection up" : "Moved selection down", ); }, [commitShapes, hasSelection, selectedIds, shapes], ); const dropSelectedToWorkplane = useCallback(() => { if (!hasSelection) { setNotice("Select a shape first"); return; } const selected = new Set(selectedIds); commitShapes( shapes.map((shape) => (selected.has(shape.id) && !shape.locked ? { ...shape, ...dropPatchForShape(shape, placementElevation) } : shape)), selectedIds, placementElevation === 0 ? "Dropped selection to the workplane" : `Dropped selection to ${placementElevation.toFixed(2)} mm workplane`, ); }, [commitShapes, hasSelection, placementElevation, selectedIds, shapes]); const activateWorkplaneTool = useCallback(() => { setWorkplaneMode((active) => { const next = !active; setNotice(next ? "Workplane tool: click a shape top or empty grid" : "Workplane tool cancelled"); return next; }); }, []); const setPlacementWorkplane = useCallback((elevation: number, source: "shape" | "base") => { setPlacementElevation(elevation); setNotice(source === "shape" ? `Workplane set to ${elevation.toFixed(2)} mm` : "Workplane reset to base"); }, []); const groupSelected = useCallback(async () => { if (selectedShapes.length < 2) { setNotice("Select at least two shapes to group"); return; } if (selectedShapes.some((shape) => shape.locked)) { setNotice("Unlock every selected shape before grouping"); return; } const result = await buildGroupedShapeFromSelection(selectedShapes); const { group } = result; if (!group) { if (result.consumed) { const selected = new Set(selectedIds); commitShapes(shapes.filter((shape) => !selected.has(shape.id)), null, "Grouped: hole consumed solid"); return; } setNotice(result.failureNotice); return; } const selected = new Set(selectedIds); commitShapes([...shapes.filter((shape) => !selected.has(shape.id)), group], group.id, `Grouped ${selectedShapes.length} shapes`); }, [commitShapes, selectedIds, selectedShapes, shapes]); const intersectSelected = useCallback(async () => { const groupable = selectedShapes.filter((shape) => !shape.locked); const hasSolid = groupable.some((shape) => !shape.hole); const hasHole = groupable.some((shape) => shape.hole); if (!hasSolid || !hasHole) { setNotice("Select at least one solid and one hole for Intersection"); return; } const result = await buildIntersectionShapeFromSelection(groupable); if (!result.group && !result.empty) { setNotice(result.failureNotice); return; } const operandIds = new Set(groupable.map((shape) => shape.id)); const remainingShapes = shapes.filter((shape) => !operandIds.has(shape.id)); if (result.empty) { commitShapes(remainingShapes, null, "Intersection is empty"); return; } const intersection = result.group; if (!intersection) { return; } commitShapes([...remainingShapes, intersection], intersection.id, `Intersected ${groupable.length} shapes`); }, [commitShapes, selectedShapes, shapes]); const ungroupSelected = useCallback(() => { const groups = selectedShapes.filter((shape) => shape.groupedShapes?.length); if (groups.length === 0) { setNotice("Select a group first"); return; } const groupIds = new Set(groups.map((shape) => shape.id)); const restored = groups.flatMap(restoreGroupedChildren); commitShapes([...shapes.filter((shape) => !groupIds.has(shape.id)), ...restored], restored.map((shape) => shape.id), `Ungrouped ${groups.length} group${groups.length === 1 ? "" : "s"}`); }, [commitShapes, selectedShapes, shapes]); const separateSelectedParts = useCallback(() => { if (selectedShapes.length !== 1 || !selectedShape) { setNotice("Select one object to separate"); return; } if (selectedShape.locked) { setNotice("Unlock the object before separating parts"); return; } const parts = separateShapeParts(selectedShape); if (parts.length <= 1) { setNotice("The selected object has only one connected part"); return; } commitShapes( [...shapes.filter((shape) => shape.id !== selectedShape.id), ...parts], parts.map((shape) => shape.id), `Separated ${parts.length} parts`, ); }, [commitShapes, selectedShape, selectedShapes.length, shapes]); const mcpSceneSnapshot = useCallback((includeRawShapes = false): SketchForgeMcpSceneSummary & { rawShapes?: WorkplaneShape[] } => { const projectInfo = projectInfoRef.current; const currentShapes = shapesRef.current; return { projectId: projectInfo.projectId, projectName: projectInfo.projectName, notice: noticeRef.current, selectedIds: selectedIdsRef.current, shapeCount: currentShapes.length, workspace: workspaceSettingsRef.current, snap: initialSnap ?? null, shapes: currentShapes.map(mcpShapeSummary), ...(includeRawShapes ? { rawShapes: currentShapes.map((shape) => canonicalizeShape(shape)) } : {}), }; }, [initialSnap]); const executeMcpCommand = useCallback(async (command: SketchForgeMcpCommand): Promise => { const params = command.params ?? {}; const currentShapes = () => shapesRef.current; const findShape = (id: unknown) => (typeof id === "string" ? currentShapes().find((shape) => shape.id === id) ?? null : null); try { lastMcpErrorRef.current = null; if (command.action === "get_scene") { return mcpSceneSnapshot(params.includeRawShapes === true); } if (command.action === "list_objects") { return { objects: currentShapes().map(mcpShapeSummary) }; } if (command.action === "select_objects") { const requestedIds = mcpStringArray(params.ids ?? params.id); const validIds = requestedIds.filter((id) => currentShapes().some((shape) => shape.id === id)); setSelectedIds(validIds); selectedIdsRef.current = validIds; setNotice(validIds.length ? `MCP selected ${validIds.length} object${validIds.length === 1 ? "" : "s"}` : "MCP cleared selection"); return { selectedIds: validIds, objects: currentShapes().filter((shape) => validIds.includes(shape.id)).map(mcpShapeSummary) }; } if (command.action === "delete_objects") { const requestedIds = mcpStringArray(params.ids ?? params.id); const ids = requestedIds.length ? new Set(requestedIds) : new Set(selectedIdsRef.current); const deleted = currentShapes().filter((shape) => ids.has(shape.id)); if (deleted.length === 0) throw new Error("No matching objects to delete"); commitShapes(currentShapes().filter((shape) => !ids.has(shape.id)), [], `MCP deleted ${deleted.length} object${deleted.length === 1 ? "" : "s"}`); selectedIdsRef.current = []; return { deletedIds: deleted.map((shape) => shape.id), deletedCount: deleted.length }; } if (command.action === "create_shape") { const rawKind = mcpString(params.kind, "box"); const kind = rawKind === "cube" ? "box" : rawKind; const width = Math.max(MIN_SHAPE_DIMENSION, mcpNumber(params.width ?? params.size, 20)); const depth = Math.max(MIN_SHAPE_DIMENSION, mcpNumber(params.depth ?? params.size, rawKind === "cube" ? width : 20)); const height = Math.max(MIN_SHAPE_DIMENSION, mcpNumber(params.height ?? params.size, rawKind === "cube" ? width : 20)); const x = mcpNumber(params.x, 0); const z = mcpNumber(params.z, 0); const elevation = mcpNumber(params.elevation, placementElevation); const color = mcpString(params.color, kind === "cylinder" ? "#d97813" : "#d41721"); const name = mcpString(params.name, kind === "cylinder" ? "Cylinder" : kind === "sketch" ? "Sketch extrusion" : rawKind === "cube" ? "Cube" : "Box"); let shape: WorkplaneShape; if (kind === "sketch") { const profile = defaultMcpSketchProfile(width, depth); const extruded = shapeFromSketchProfile(profile, height); if (!extruded) throw new Error("Could not create the sketch profile"); shape = canonicalizeShape({ ...extruded, name, color, x, z, elevation, rotation: mcpNumber(params.rotation, 0) }); } else if (kind === "box" || kind === "cylinder") { shape = sceneShape({ name, kind, color, x, z, elevation, width, depth, height, size: Math.max(width, depth), rotation: mcpNumber(params.rotation, 0), rotationX: mcpNumber(params.rotationX, 0), rotationZ: mcpNumber(params.rotationZ, 0), sides: kind === "cylinder" ? Math.max(3, Math.floor(mcpNumber(params.sides, 96))) : undefined, }); } else { throw new Error("MCP create_shape currently supports box, cube, cylinder, and sketch"); } commitShapes([...currentShapes(), shape], shape.id, `${shape.name} added by MCP`); return { object: mcpShapeSummary(shape) }; } if (command.action === "import_mesh") { const positions = mcpFiniteNumberArray(params.positions); const normals = mcpFiniteNumberArray(params.normals); if (positions.length < 9 || positions.length % 9 !== 0) { throw new Error("import_mesh requires positions as triangle xyz values"); } let minX = Number.POSITIVE_INFINITY; let maxX = Number.NEGATIVE_INFINITY; let minY = Number.POSITIVE_INFINITY; let maxY = Number.NEGATIVE_INFINITY; let minZ = Number.POSITIVE_INFINITY; let maxZ = Number.NEGATIVE_INFINITY; for (let index = 0; index < positions.length; index += 3) { const x = positions[index]; const y = positions[index + 1]; const z = positions[index + 2]; minX = Math.min(minX, x); maxX = Math.max(maxX, x); minY = Math.min(minY, y); maxY = Math.max(maxY, y); minZ = Math.min(minZ, z); maxZ = Math.max(maxZ, z); } const width = Math.max(MIN_SHAPE_DIMENSION, mcpNumber(params.width, maxX - minX)); const depth = Math.max(MIN_SHAPE_DIMENSION, mcpNumber(params.depth, maxZ - minZ)); const height = Math.max(MIN_SHAPE_DIMENSION, mcpNumber(params.height, maxY - minY)); const shape = canonicalizeShape({ id: createLocalId("mcp-imported-mesh"), name: mcpString(params.name, "Imported mesh"), kind: "mesh", color: mcpString(params.color, "#9bd7f0"), x: mcpNumber(params.x, 0), z: mcpNumber(params.z, 0), elevation: mcpNumber(params.elevation, 0), size: Math.max(width, depth), width, depth, height, rotation: mcpNumber(params.rotation, 0), rotationX: mcpNumber(params.rotationX, 0), rotationZ: mcpNumber(params.rotationZ, 0), importedMesh: { positions, normals: normals.length === positions.length ? normals : undefined, baseWidth: width, baseDepth: depth, baseHeight: height, triangleCount: Math.floor(positions.length / 9), sourceFormat: "json", }, locked: false, hidden: false, } satisfies WorkplaneShape); commitShapes([...currentShapes(), shape], shape.id, `${shape.name} imported by MCP`); return { object: mcpShapeSummary(shape) }; } if (command.action === "update_object") { const target = findShape(params.id); if (!target) throw new Error("Object not found"); if (target.locked) throw new Error("Unlock the object before updating it"); const patch: ShapeUpdatePatch = {}; (["x", "z", "elevation", "width", "depth", "height", "size", "rotation", "rotationX", "rotationZ"] as const).forEach((key) => { if (typeof params[key] === "number" && Number.isFinite(params[key])) { patch[key] = params[key]; } }); if (typeof params.color === "string") patch.color = params.color; if (typeof params.name === "string") patch.name = params.name; if (typeof params.hole === "boolean") patch.hole = params.hole; const nextShapes = currentShapes().map((shape) => { if (shape.id !== target.id) return shape; const patched = { ...shape, ...cleanShapePatch(patch) }; const width = shapeWidth(patched); const depth = shapeDepth(patched); return canonicalizeShape({ ...patched, size: Math.max(width, depth) }); }); const updated = nextShapes.find((shape) => shape.id === target.id) as WorkplaneShape; commitShapes(nextShapes, target.id, `${updated.name} updated by MCP`); return { object: mcpShapeSummary(updated) }; } if (command.action === "align_objects") { const axis = params.axis === "x" || params.axis === "y" || params.axis === "z" ? params.axis : null; const target = params.target === "min" || params.target === "center" || params.target === "max" ? params.target : null; if (!axis || !target) throw new Error("align_objects requires axis x/y/z and target min/center/max"); const requestedIds = mcpStringArray(params.ids); const ids = requestedIds.length ? requestedIds : selectedIdsRef.current; const selectedForAlign = currentShapes().filter((shape) => ids.includes(shape.id)); if (selectedForAlign.length < 2) throw new Error("Select at least two objects to align"); const validIds = selectedForAlign.map((shape) => shape.id); const requestedAnchorId = typeof params.anchorId === "string" ? params.anchorId : null; const anchorId = effectiveAlignmentAnchorId(selectedForAlign, requestedAnchorId); const { nextShapes, moved } = alignedShapesForSelection(currentShapes(), validIds, selectedForAlign, anchorId, axis, target); if (moved === 0) { setSelectedIds(validIds); selectedIdsRef.current = validIds; setNotice(`MCP alignment already ${alignmentLabel(axis, target)}`); return { moved, selectedIds: validIds, anchorId, objects: selectedForAlign.map(mcpShapeSummary), }; } commitShapes(nextShapes, validIds, `MCP aligned ${moved} object${moved === 1 ? "" : "s"} ${alignmentLabel(axis, target)}`); return { moved, selectedIds: validIds, anchorId, objects: nextShapes.filter((shape) => validIds.includes(shape.id)).map(mcpShapeSummary), }; } if (command.action === "group_objects") { const ids = new Set(mcpStringArray(params.ids)); const groupable = currentShapes().filter((shape) => ids.has(shape.id)); if (groupable.length < 2) throw new Error("Select at least two objects to group"); if (groupable.some((shape) => shape.locked)) throw new Error("Unlock every selected object before grouping"); const result = await buildGroupedShapeFromSelection(groupable); if (!result.group) { if (result.consumed) { commitShapes(currentShapes().filter((shape) => !ids.has(shape.id)), null, "MCP group consumed solid"); return { consumed: true, objects: currentShapes().filter((shape) => !ids.has(shape.id)).map(mcpShapeSummary) }; } throw new Error(result.failureNotice); } commitShapes([...currentShapes().filter((shape) => !ids.has(shape.id)), result.group], result.group.id, `MCP grouped ${groupable.length} objects`); return { object: mcpShapeSummary(result.group) }; } if (command.action === "ungroup_objects") { const requestedIds = mcpStringArray(params.ids ?? params.id); const ids = requestedIds.length ? new Set(requestedIds) : new Set(selectedIdsRef.current); const groups = currentShapes().filter((shape) => ids.has(shape.id) && shape.groupedShapes?.length); if (groups.length === 0) throw new Error("Select at least one group to ungroup"); const groupIds = new Set(groups.map((shape) => shape.id)); const restored = groups.flatMap(restoreGroupedChildren); commitShapes([...currentShapes().filter((shape) => !groupIds.has(shape.id)), ...restored], restored.map((shape) => shape.id), `MCP ungrouped ${groups.length} group${groups.length === 1 ? "" : "s"}`); return { objects: restored.map(mcpShapeSummary) }; } if (command.action === "boolean_cut") { const solidIds = new Set(mcpStringArray(params.solidIds ?? params.solids)); const holeIds = new Set(mcpStringArray(params.holeIds ?? params.holes)); const operandIds = new Set([...solidIds, ...holeIds]); const operands = currentShapes() .filter((shape) => operandIds.has(shape.id)) .map((shape) => holeIds.has(shape.id) ? withHoleMode(shape, true) : withHoleMode(shape, false)); if (!operands.some((shape) => !shape.hole) || !operands.some((shape) => shape.hole)) { throw new Error("Provide at least one solidId and one holeId for boolean_cut"); } if (operands.some((shape) => shape.locked)) { throw new Error("Unlock every boolean operand before cutting"); } const result = await buildGroupedShapeFromSelection(operands); const remainingShapes = currentShapes().filter((shape) => !operandIds.has(shape.id)); if (result.consumed) { commitShapes(remainingShapes, null, "MCP cut consumed solid"); return { consumed: true }; } if (!result.group) { throw new Error(result.failureNotice); } commitShapes([...remainingShapes, result.group], result.group.id, "MCP boolean cut complete"); return { object: mcpShapeSummary(result.group) }; } if (command.action === "separate_parts") { const target = findShape(params.id) ?? (selectedIdsRef.current.length === 1 ? findShape(selectedIdsRef.current[0]) : null); if (!target) throw new Error("Select one object to separate"); if (target.locked) throw new Error("Unlock the object before separating parts"); const parts = separateShapeParts(target); if (parts.length <= 1) throw new Error("The selected object has only one connected part"); commitShapes([...currentShapes().filter((shape) => shape.id !== target.id), ...parts], parts.map((shape) => shape.id), `MCP separated ${parts.length} parts`); return { objects: parts.map(mcpShapeSummary) }; } if (command.action === "list_edges") { const target = findShape(params.id); if (!target) throw new Error("Object not found"); invalidateCadModifierSession(); const sharpAngle = Math.max(1, Math.min(120, mcpNumber(params.sharpAngle, 25))); const { response } = await prepareCadModifierForMcp(target, sharpAngle); const selectableEdgeIds = response.edges.filter((edge) => selectableCadModifierEdge(edge, sharpAngle)).map((edge) => edge.id); return { object: mcpShapeSummary(target), sharpAngle, selectableEdgeIds, edges: response.edges }; } if (command.action === "apply_edge_treatment") { const target = findShape(params.id); if (!target) throw new Error("Object not found"); return applyCadModifierForMcp(target, params); } if (command.action === "inspect_errors") { return { notice: noticeRef.current, edgeModifierError: edgeModifierRef.current?.error ?? null, lastMcpError: lastMcpErrorRef.current, }; } if (command.action === "capture_image") { const face = mcpString(params.face, "current") as SketchForgeMcpViewFace; const image = await (window.sketchforgeCaptureView?.(face) ?? window.sketchforgeCaptureCanvas?.() ?? ""); if (!image || image.length < 100) { throw new Error("The SketchForge viewport did not return an image"); } return { face, dataUrl: image, bytesApprox: Math.floor(image.length * 0.75) }; } throw new Error(`Unknown MCP command: ${command.action}`); } catch (error) { const message = error instanceof Error ? error.message : String(error); lastMcpErrorRef.current = message; setNotice(message); throw error; } }, [ applyCadModifierForMcp, commitShapes, initialSnap, invalidateCadModifierSession, mcpSceneSnapshot, placementElevation, prepareCadModifierForMcp, ]); useEffect(() => { executeMcpCommandRef.current = executeMcpCommand; }, [executeMcpCommand]); useEffect(() => { if (process.env.NODE_ENV === "production" || typeof window === "undefined") { return; } if (!["localhost", "127.0.0.1", "::1"].includes(window.location.hostname)) { return; } const identity = readMcpEditorIdentity(); let stopped = false; let polling = false; const heartbeat = () => { const projectInfo = projectInfoRef.current; const currentShapes = shapesRef.current; void fetch(SKETCHFORGE_MCP_ROUTE, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ type: "heartbeat", editor: { ...identity, projectId: projectInfo.projectId, projectName: projectInfo.projectName, url: window.location.href, focused: document.visibilityState === "visible" && document.hasFocus(), shapeCount: currentShapes.length, selectedCount: selectedIdsRef.current.length, notice: noticeRef.current, lastError: edgeModifierRef.current?.error ?? lastMcpErrorRef.current, }, }), }).catch(() => undefined); }; const submitResult = (commandId: string, ok: boolean, data?: unknown, error?: string) => { void fetch(SKETCHFORGE_MCP_ROUTE, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ type: "result", editorId: identity.editorId, result: { commandId, ok, data, error, completedAt: Date.now() }, }), }).catch(() => undefined); }; const poll = async () => { if (polling || stopped) return; polling = true; try { const response = await fetch(SKETCHFORGE_MCP_ROUTE, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ type: "poll", editorId: identity.editorId }), }); const payload = (await response.json().catch(() => null)) as { command?: SketchForgeMcpCommand | null } | null; const command = payload?.command; if (command) { try { const data = await executeMcpCommandRef.current?.(command); submitResult(command.id, true, data); } catch (error) { submitResult(command.id, false, undefined, error instanceof Error ? error.message : String(error)); } } } catch { // The local bridge may not exist while static builds or tests render the editor. } finally { polling = false; } }; heartbeat(); void poll(); const heartbeatTimer = window.setInterval(heartbeat, 1000); const pollTimer = window.setInterval(() => void poll(), SKETCHFORGE_MCP_POLL_MS); window.addEventListener("focus", heartbeat); document.addEventListener("visibilitychange", heartbeat); return () => { stopped = true; window.clearInterval(heartbeatTimer); window.clearInterval(pollTimer); window.removeEventListener("focus", heartbeat); document.removeEventListener("visibilitychange", heartbeat); }; }, []); useEffect(() => { if (process.env.NODE_ENV === "production" || typeof window === "undefined") { return; } if (!["localhost", "127.0.0.1", "::1"].includes(window.location.hostname)) { return; } const params = new URLSearchParams(window.location.search); const caseId = params.get("codexBooleanCase"); if (!caseId) { return; } const modeParam = params.get("codexBooleanMode"); const mode: BooleanAutomationMode = modeParam === "before" || modeParam === "ungroup" ? modeParam : "after"; const runKey = `${caseId}:${mode}`; if (booleanAutomationRunRef.current === runKey) { return; } booleanAutomationRunRef.current = runKey; document.body.dataset.codexBooleanTestDone = "running"; delete document.body.dataset.codexBooleanTestResult; delete document.body.dataset.codexBooleanTestImageReady; delete document.body.dataset.codexBooleanTestScreenshotPath; const finish = (detail: BooleanAutomationResult) => { window.__sketchforgeBooleanTest = detail; const captureCanvas = () => { try { const canvas = document.querySelector("canvas") as HTMLCanvasElement | null; const image = window.sketchforgeCaptureCanvas?.() ?? canvas?.toDataURL("image/png") ?? ""; if (image.length > 100) { window.__sketchforgeBooleanTestImage = image; document.body.dataset.codexBooleanTestImageReady = "true"; void fetch("/api/codex-screenshot", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ name: `boolean-${detail.caseId}-${detail.mode}.png`, dataUrl: image }), }) .then((response) => (response.ok ? response.json() : null)) .then((payload: { path?: string } | null) => { if (payload?.path) { document.body.dataset.codexBooleanTestScreenshotPath = payload.path; } }) .catch(() => { document.body.dataset.codexBooleanTestImageReady = "false"; }); } } catch { document.body.dataset.codexBooleanTestImageReady = "false"; } }; const publish = () => { document.body.dataset.codexBooleanTestDone = detail.ok ? "true" : "false"; document.body.dataset.codexBooleanTestResult = JSON.stringify(detail); window.dispatchEvent(new CustomEvent("sketchforge:boolean-test-done", { detail })); }; publish(); window.setTimeout(publish, 100); window.setTimeout(captureCanvas, 1000); window.setTimeout(captureCanvas, 1800); }; const run = async () => { const testCase = booleanAutomationScene(caseId); if (!testCase) { finish({ ok: false, caseId, label: "Unknown boolean test", mode, notice: "Unknown boolean automation case", shapeCount: 0, selectedCount: 0, error: `Unknown boolean automation case: ${caseId}`, }); return; } const ids = testCase.shapes.map((shape) => shape.id); if (mode === "before") { const noticeText = `Boolean test before: ${testCase.label}`; commitShapes(testCase.shapes, ids, noticeText); finish({ ok: true, caseId, label: testCase.label, mode, notice: noticeText, shapeCount: testCase.shapes.length, selectedCount: ids.length, }); return; } const result = await buildGroupedShapeFromSelection(testCase.shapes); if (!result.group) { const noticeText = result.consumed ? "Grouped: hole consumed solid" : result.failureNotice; commitShapes(result.consumed ? [] : testCase.shapes, result.consumed ? [] : ids, noticeText); finish({ ok: result.consumed, caseId, label: testCase.label, mode, notice: noticeText, shapeCount: result.consumed ? 0 : testCase.shapes.length, selectedCount: result.consumed ? 0 : ids.length, error: result.consumed ? undefined : result.failureNotice, }); return; } const triangleCount = result.group.importedMesh?.triangleCount ?? meshForShape(result.group).faces.length; const groupedCount = result.group.groupedShapes?.length ?? 0; if (mode === "ungroup") { const restored = restoreGroupedChildren(result.group); const noticeText = `Boolean test ungrouped: ${testCase.label}`; commitShapes(restored, restored.map((shape) => shape.id), noticeText); finish({ ok: restored.length === groupedCount, caseId, label: testCase.label, mode, notice: noticeText, shapeCount: restored.length, selectedCount: restored.length, triangleCount, groupedCount, groupId: result.group.id, }); return; } const noticeText = `Boolean test after: ${testCase.label}`; commitShapes([result.group], result.group.id, noticeText); finish({ ok: true, caseId, label: testCase.label, mode, notice: noticeText, shapeCount: 1, selectedCount: 1, triangleCount, groupedCount, groupId: result.group.id, }); }; void run(); }, [commitShapes]); const exportDesign = useCallback((format: ExportFormat) => { const sourceShapes = hasSelection ? selectedShapes : shapes; const exportable = sourceShapes.filter((shape) => !shape.hole); if (exportable.length === 0) { setNotice(hasSelection ? "Select at least one solid shape before exporting" : "Add a solid shape before exporting"); return; } const meshes = exportable.map(meshForShape); const selectedNotice = `Exported ${exportable.length} selected shape${exportable.length === 1 ? "" : "s"}`; const finishNotice = (label: string, result: DownloadResult) => { if (result.mode === "folder") { setNotice(`Saved ${label} to ${result.path}`); return; } setNotice(hasSelection ? `${selectedNotice} as ${label}` : `Exported ${label}`); }; const failNotice = (label: string, error: unknown) => { setNotice(error instanceof Error ? error.message : `Could not export ${label}`); }; if (format === "stl") { void downloadTextFile(projectExportFileName(projectName, "stl"), toStl(meshes), "model/stl") .then((result) => finishNotice("STL", result)) .catch((error: unknown) => failNotice("STL", error)); return; } void downloadTextFile(projectExportFileName(projectName, "obj"), toObj(meshes), "text/plain") .then((result) => finishNotice("OBJ", result)) .catch((error: unknown) => failNotice("OBJ", error)); }, [hasSelection, projectName, selectedShapes, shapes]); const exportStepDesign = useCallback(async () => { if (stepExporting) { return; } const sourceShapes = hasSelection ? selectedShapes : shapes; if (sourceShapes.some((shape) => shape.hole) && !sourceShapes.some((shape) => !shape.hole)) { setNotice("Select at least one solid shape before exporting STEP"); return; } setStepExporting(true); setNotice("Building B-Rep… first STEP export loads the OpenCascade kernel (~22 MB), one time per session"); try { const { exportShapesToStep } = await import("@/lib/stepExport"); const { blob, exportedCount, skipped } = await exportShapesToStep(sourceShapes); const text = await blob.text(); const result = await downloadTextFile(projectExportFileName(projectName, "step"), text, "application/step"); const skipNote = skipped.length > 0 ? `; skipped ${skipped.length} non-primitive shape${skipped.length === 1 ? "" : "s"}` : ""; if (result.mode === "folder") { setNotice(`Saved STEP (${exportedCount} bod${exportedCount === 1 ? "y" : "ies"}) to ${result.path}${skipNote}`); } else { setNotice(`Exported STEP B-Rep with ${exportedCount} bod${exportedCount === 1 ? "y" : "ies"}${skipNote}`); } } catch (error: unknown) { setNotice(error instanceof Error ? error.message : "Could not export STEP"); } finally { setStepExporting(false); } }, [hasSelection, projectName, selectedShapes, shapes, stepExporting]); const clearDesign = useCallback(() => { commitShapes([], [], "New empty design"); setClipboard([]); setMenuOpen(false); setTopPanel(null); }, [commitShapes]); const createHouseScene = useCallback( (replace = true) => { const house = makeHouseScene(); const next = replace ? house : [...shapes, ...house]; commitShapes(next, house.map((shape) => shape.id), "House scene created"); setMenuOpen(false); setTopPanel(null); return house; }, [commitShapes, shapes], ); const createPerfScene = useCallback( (count = 500) => { const scene = makeBlockPerfScene(count); commitShapes(scene, [], `Performance scene: ${scene.length} blocks`); setMenuOpen(false); setTopPanel(null); return scene; }, [commitShapes], ); const saveDesign = useCallback(() => { setNotice(`Saved design with ${shapes.length} shape${shapes.length === 1 ? "" : "s"}`); setMenuOpen(false); }, [shapes.length]); const makeCopy = useCallback(() => { if (shapes.length === 0) { setNotice("Nothing to copy yet"); setMenuOpen(false); return; } const copies = shapes.map((shape) => ({ ...shape, id: createLocalId(`${shape.id}-copy`), x: Math.min(110, shape.x + 12), z: Math.min(110, shape.z + 12), })); commitShapes([...shapes, ...copies], copies.map((shape) => shape.id), "Made a copy of the design"); setMenuOpen(false); }, [commitShapes, shapes]); const selectFile = useCallback(async (file: File) => { const isStep = /\.(step|stp)$/i.test(file.name); if (!isStep && !importExtensionSupported(file.name)) { setNotice("Unsupported file type. Use STL or STEP."); return; } try { let nextShape: WorkplaneShape; if (isStep) { setNotice("Reading STEP… first import loads the OpenCascade kernel (~22 MB), one time per session"); const { importedShapeFromStep } = await import("@/lib/stepImport"); nextShape = await importedShapeFromStep(file.name, await file.arrayBuffer()); } else { nextShape = importedShapeFromStl(file.name, await file.arrayBuffer()); } commitShapes([...shapes, nextShape], nextShape.id, `Imported ${file.name}`); setTopPanel(null); } catch (error) { setNotice(error instanceof Error ? error.message : `Could not import ${file.name}`); } }, [commitShapes, shapes]); const selectFiles = useCallback( (files: FileList | File[]) => { const file = Array.from(files)[0]; if (file) { void selectFile(file); } }, [selectFile], ); const selectShape = useCallback((id: string | string[] | null, mode: "replace" | "toggle" = "replace") => { setSelectedIds((current) => { if (Array.isArray(id)) { const unique = id.filter((entry, index) => id.indexOf(entry) === index); return mode === "toggle" ? unique.reduce((next, entry) => (next.includes(entry) ? next.filter((selected) => selected !== entry) : [...next, entry]), current) : unique; } if (!id) { return mode === "toggle" ? current : []; } if (mode === "toggle") { return current.includes(id) ? current.filter((entry) => entry !== id) : [...current, id]; } return [id]; }); }, []); const nudgeSelected = useCallback( (deltaX: number, deltaZ: number) => { if (!hasSelection) { return; } const selected = new Set(selectedIds); commitShapes( shapes.map((shape) => selected.has(shape.id) && !shape.locked ? { ...shape, x: Math.max(-110, Math.min(110, shape.x + deltaX)), z: Math.max(-110, Math.min(110, shape.z + deltaZ)), } : shape, ), selectedIds, `Moved ${selectedShapes.length} shape${selectedShapes.length === 1 ? "" : "s"}`, ); }, [commitShapes, hasSelection, selectedIds, selectedShapes.length, shapes], ); useEffect(() => { const isTypingTarget = (target: EventTarget | null) => { if (!(target instanceof HTMLElement)) { return false; } return target.isContentEditable || ["INPUT", "TEXTAREA", "SELECT"].includes(target.tagName); }; const handleKeyDown = (event: KeyboardEvent) => { if (isTypingTarget(event.target)) { return; } const key = event.key.toLowerCase(); const shortcut = event.ctrlKey || event.metaKey; if (sketchActive && toolbarMode === "sketch") { if (event.key === "Escape") { event.preventDefault(); setSketchActivePointId(null); setSketchSelection(null); setNotice("Current sketch chain cleared"); } else if (event.key === "Delete" || event.key === "Backspace") { event.preventDefault(); if (sketchSelection) deleteSelectedSketchEntity(); else if (sketchMeasurement) clearSketchMeasurement(); else deleteSelectedSketchEntity(); } else if (shortcut && key === "z") { event.preventDefault(); if (event.shiftKey) sketchRedo(); else sketchUndo(); } else if (shortcut && key === "y") { event.preventDefault(); sketchRedo(); } return; } if (event.key === "Escape") { setSelectedIds([]); setNotice("Selection cleared"); return; } if (event.key === "Delete" || event.key === "Backspace") { event.preventDefault(); deleteSelected(); return; } if (shortcut && key === "z") { event.preventDefault(); if (event.shiftKey) { redo(); } else { undo(); } return; } if (shortcut && key === "y") { event.preventDefault(); redo(); return; } if (shortcut && key === "c") { event.preventDefault(); copySelected(); return; } if (shortcut && key === "x") { event.preventDefault(); cutSelected(); return; } if (shortcut && key === "v") { event.preventDefault(); pasteShape(); return; } if (shortcut && key === "d") { event.preventDefault(); duplicateSelected(); return; } if (shortcut && key === "a") { event.preventDefault(); setSelectedIds(shapes.filter((shape) => !shape.hidden).map((shape) => shape.id)); setNotice("Selected all visible shapes"); return; } if (shortcut && key === "g") { event.preventDefault(); if (event.shiftKey) { ungroupSelected(); } else { groupSelected(); } return; } if (shortcut && key === "l") { event.preventDefault(); toggleLocked(); return; } if (shortcut && key === "h") { event.preventDefault(); if (event.shiftKey) { showHidden(); } else { toggleHidden(); } return; } const step = event.shiftKey ? 5 : 1; if (shortcut && event.key === "ArrowUp") { event.preventDefault(); raiseSelected(step); } else if (shortcut && event.key === "ArrowDown") { event.preventDefault(); raiseSelected(-step); } else if (event.key === "ArrowLeft") { event.preventDefault(); nudgeSelected(-step, 0); } else if (event.key === "ArrowRight") { event.preventDefault(); nudgeSelected(step, 0); } else if (event.key === "ArrowUp") { event.preventDefault(); nudgeSelected(0, -step); } else if (event.key === "ArrowDown") { event.preventDefault(); nudgeSelected(0, step); } else if (key === "d" && hasSelection) { event.preventDefault(); dropSelectedToWorkplane(); } else if (key === "h") { event.preventDefault(); setSelectionHoleMode(true); } else if (key === "s") { event.preventDefault(); setSelectionHoleMode(false); } else if (key === "l") { event.preventDefault(); toggleAlignMode(); } else if (key === "m") { event.preventDefault(); toggleMirrorMode(); } }; window.addEventListener("keydown", handleKeyDown); return () => window.removeEventListener("keydown", handleKeyDown); }, [ commitShapes, clearSketchMeasurement, copySelected, cutSelected, deleteSelected, deleteSelectedSketchEntity, duplicateSelected, dropSelectedToWorkplane, groupSelected, hasSelection, nudgeSelected, pasteShape, raiseSelected, redo, sketchActive, sketchRedo, sketchMeasurement, sketchSelection, sketchUndo, setSelectionHoleMode, showHidden, toggleAlignMode, toggleHidden, toggleMirrorMode, toggleLocked, toolbarMode, undo, ungroupSelected, ]); return (
{ setToolbarMode(mode); setTopPanel(null); setMenuOpen(false); }} canUndo={historyIndex > 0 || Boolean(edgeModifier)} canRedo={historyIndex < history.length - 1} canGroup={selectedShapes.length > 1 && selectedShapes.every((shape) => !shape.locked)} canIntersect={selectedShapes.some((shape) => !shape.locked && !shape.hole) && selectedShapes.some((shape) => !shape.locked && Boolean(shape.hole))} canUngroup={selectedShapes.some((shape) => Boolean(shape.groupedShapes?.length))} hasClipboard={clipboard.length > 0 || systemClipboardSupported} hasSelection={hasSelection} alignMode={alignMode} canAlign={selectedShapes.length > 1} canEdgeModify={selectedShapes.length === 1 && Boolean(selectedShape && !selectedShape.locked && !selectedShape.hole)} edgeModifierKind={edgeModifier?.kind ?? null} mirrorMode={mirrorMode} sketchActive={sketchActive} sketchTool={sketchTool} sketchCanUndo={sketchHistoryIndex > 0} sketchCanRedo={sketchHistoryIndex < sketchHistory.length - 1} canEditSketch={selectedShapes.length === 1 && Boolean(selectedShape?.sketchProfile)} onStartSketch={() => beginSketch()} onEditSketch={beginSketchEdit} onSketchTool={setActiveSketchTool} onSketchImage={() => { if (sketchTool !== "select") { setNotice("Choose Select before adding a sketch image"); return; } sketchImageInputRef.current?.click(); }} onSketchUndo={sketchUndo} onSketchRedo={sketchRedo} onSketchFinish={finishSketch} onSketchCancel={cancelSketch} onHome={onHome} onAlign={toggleAlignMode} onChamfer={() => edgeModifier?.kind === "chamfer" ? cancelEdgeModifier() : startEdgeModifier("chamfer")} onCopy={copySelected} onDelete={deleteSelected} onDuplicate={duplicateSelected} onDropToWorkplane={dropSelectedToWorkplane} onGroup={groupSelected} onIntersect={intersectSelected} onFillet={() => edgeModifier?.kind === "fillet" ? cancelEdgeModifier() : startEdgeModifier("fillet")} onMirror={toggleMirrorMode} onPaste={pasteShape} onRedo={redo} onSnap={snapSelected} onTips={() => { setTopPanel(topPanel === "tips" ? null : "tips"); setMenuOpen(false); }} onToggleHidden={toggleHidden} onUngroup={ungroupSelected} onUndo={undo} onWorkplaneTool={activateWorkplaneTool} workplaneMode={workplaneMode} onOpenScrewHole={openScrewHolePanel} onTopPanel={(panel) => { setTopPanel(panel); setMenuOpen(false); }} onAddShape={(shape) => { addShape(shape); setTopPanel(null); setMenuOpen(false); }} />
{toolbarMode === "sketch" && sketchActive ? ( shape.id !== editingSketchShapeId)} tool={sketchTool} activePointId={sketchActivePointId} selected={sketchSelection} measurement={sketchMeasurement} pendingMeasurementStart={sketchMeasureStart} initialSnap={initialSnap} initialWorkspace={initialWorkspace} onPlanePoint={addSketchPlanePoint} onPointPress={pressSketchPoint} onSelectSegment={(id) => { setSketchSelection({ kind: "segment", id }); setSketchActivePointId(null); }} onSelectMany={(pointIds, segmentIds, imageIds) => { setSketchSelection(pointIds.length || segmentIds.length || imageIds.length ? { kind: "multiple", pointIds, segmentIds, imageIds } : null); setSketchActivePointId(null); const count = pointIds.length + segmentIds.length + imageIds.length; setNotice(count ? `Selected ${count} sketch item${count === 1 ? "" : "s"}` : "Sketch selection cleared"); }} onSelectImage={(id) => { setSketchSelection({ kind: "image", id }); setSketchActivePointId(null); setNotice("Sketch image selected"); }} onUpdateImage={updateSketchImage} onDeleteImage={deleteSketchImage} onDeletePoint={deleteSketchPoint} onDeleteSegment={deleteSketchSegment} onMovePoint={moveSketchPoint} onMoveHandle={moveSketchHandle} onInsertPoint={insertSketchPoint} onSetPointMode={setSketchPointMode} onClearMeasurement={clearSketchMeasurement} /> ) : ( modifierAvailableEdgeIds.includes(edge.id)) ?? []} selectedModifierEdgeIds={edgeModifier?.selectedEdgeIds ?? []} onModifierEdgeToggle={toggleModifierEdge} /> )}
{edgeModifier ? ( setEdgeModifier((current) => current ? { ...current, amount: Math.max(MIN_EDGE_MODIFIER_AMOUNT, Math.min(edgeModifierMaxAmount, value)), preview: null, busy: true, error: null } : current)} onChamferAngleChange={(value) => setEdgeModifier((current) => current ? { ...current, chamferAngle: Math.max(5, Math.min(85, value)), preview: null, busy: true, error: null } : current)} onQualityChange={(quality) => setEdgeModifier((current) => current ? { ...current, quality, preview: null, busy: true, error: null } : current)} onSharpAngleChange={(sharpAngle) => setEdgeModifier((current) => { if (!current) return current; const nextAngle = Math.max(1, Math.min(120, sharpAngle)); const availableIds = new Set(current.edges .filter((edge) => selectableCadModifierEdge(edge, nextAngle)) .map((edge) => edge.id)); const selectedEdgeIds = current.selectedEdgeIds.filter((edgeId) => availableIds.has(edgeId)); return { ...current, sharpAngle: nextAngle, selectedEdgeIds, preview: null, busy: selectedEdgeIds.length > 0, error: availableIds.size === 0 ? "No sharp edges match this threshold" : selectedEdgeIds.length ? null : "Select at least one highlighted edge", }; })} onTangentChainChange={(tangentChain) => setEdgeModifier((current) => current ? { ...current, tangentChain } : current)} onPreserveEdgeSizeChange={(preserveEdgeSize) => setEdgeModifier((current) => current ? { ...current, preserveEdgeSize } : current)} onSelectAll={() => setEdgeModifier((current) => current ? { ...current, selectedEdgeIds: modifierAvailableEdgeIds, preview: null, busy: modifierAvailableEdgeIds.length > 0, error: modifierAvailableEdgeIds.length ? null : current.error } : current)} onClear={() => setEdgeModifier((current) => current ? { ...current, selectedEdgeIds: [], preview: null, busy: false, error: "Select at least one highlighted edge" } : current)} onRemoveFeature={removeEdgeTreatment} onApply={applyEdgeModifier} onCancel={cancelEdgeModifier} /> ) : null} {screwHolePanelOpen ? ( setScrewHoleConfig((current) => ({ ...current, ...patch }))} onStartPlacement={startScrewHolePlacement} onFinishPlacement={finishScrewHolePlacement} onClose={closeScrewHolePanel} /> ) : null} {topPanel ? ( setTopPanel(null)} onExport={exportDesign} onExportStep={exportStepDesign} stepExporting={stepExporting} onImportFiles={selectFiles} onPickFile={() => fileInputRef.current?.click()} onNotice={setNotice} /> ) : null} { const file = event.currentTarget.files?.[0]; if (file) void addSketchImageFile(file); event.currentTarget.value = ""; }} /> { if (event.currentTarget.files) { selectFiles(event.currentTarget.files); } event.currentTarget.value = ""; }} />
{notice}
); } const sketchReferenceIcons = { line: "sketch-tool-line.png", bezier: "sketch-tool-bezier-curve.png", smooth: "sketch-tool-smooth-curve.png", select: "sketch-tool-select.png", image: "sketch-tool-add-image.png", refine: "sketch-tool-add-or-remove-points.png", erase: "sketch-tool-erase.png", measure: "sketch-tool-measure.png", sketchTo3d: "sketch-tool-sketch-to-3d.png", editSketchTo3d: "sketch-tool-edit-sketch-to-3d.png", } as const; type SketchReferenceIconName = keyof typeof sketchReferenceIcons; function SketchReferenceIcon({ name }: { name: SketchReferenceIconName }) { return ( ); } function SecondaryToolbar({ toolbarMode, onToolbarModeChange, alignMode, canAlign, canEdgeModify, edgeModifierKind, canGroup, canIntersect, canRedo, canUngroup, canUndo, hasClipboard, hasSelection, mirrorMode, sketchActive, sketchTool, sketchCanUndo, sketchCanRedo, canEditSketch, onStartSketch, onEditSketch, onSketchTool, onSketchImage, onSketchUndo, onSketchRedo, onSketchFinish, onSketchCancel, onHome, onAlign, onChamfer, onCopy, onDelete, onDuplicate, onDropToWorkplane, onGroup, onIntersect, onFillet, onMirror, onPaste, onRedo, onSnap, onTips, onToggleHidden, onUngroup, onUndo, onWorkplaneTool, workplaneMode, onOpenScrewHole, onTopPanel, onAddShape, }: { toolbarMode: ToolbarMode; onToolbarModeChange: (mode: ToolbarMode) => void; alignMode: boolean; canAlign: boolean; canEdgeModify: boolean; edgeModifierKind: CadModifierKind | null; canGroup: boolean; canIntersect: boolean; canRedo: boolean; canUngroup: boolean; canUndo: boolean; hasClipboard: boolean; hasSelection: boolean; mirrorMode: boolean; sketchActive: boolean; sketchTool: SketchTool; sketchCanUndo: boolean; sketchCanRedo: boolean; canEditSketch: boolean; onStartSketch: () => void; onEditSketch: () => void; onSketchTool: (tool: SketchTool) => void; onSketchImage: () => void; onSketchUndo: () => void; onSketchRedo: () => void; onSketchFinish: () => void; onSketchCancel: () => void; onHome?: () => void; onAlign: () => void; onChamfer: () => void; onCopy: () => void; onDelete: () => void; onDuplicate: () => void; onDropToWorkplane: () => void; onGroup: () => void; onIntersect: () => void; onFillet: () => void; onMirror: () => void; onPaste: () => void; onRedo: () => void; onSnap: () => void; onTips: () => void; onToggleHidden: () => void; onUngroup: () => void; onUndo: () => void; onWorkplaneTool: () => void; workplaneMode: boolean; onOpenScrewHole: () => void; onTopPanel: (panel: TopPanel) => void; onAddShape: (shape: ShapeAsset) => void; }) { const [shapesOpen, setShapesOpen] = useState(false); const touchShapeStartRef = useRef<{ id: string; x: number; y: number } | null>(null); const suppressNextShapeClickRef = useRef(false); const selectToolbarMode = (mode: "geometry" | "sketch") => { setShapesOpen(false); onTopPanel(null); onToolbarModeChange(mode); }; const addShapeFromMenu = (shape: ShapeAsset) => { onAddShape(shape); setShapesOpen(false); }; const openScrewHoleFromMenu = () => { onOpenScrewHole(); setShapesOpen(false); }; const leftTools = [ { label: "Copy", icon: ToolbarCopyIcon, action: onCopy, enabled: hasSelection }, { label: "Paste", icon: ToolbarPasteIcon, action: onPaste, enabled: hasClipboard }, { label: "Duplicate", icon: ToolbarDuplicateIcon, action: onDuplicate, enabled: hasSelection }, { label: "Delete", icon: ToolbarTrashIcon, action: onDelete, enabled: hasSelection }, { label: "Undo", icon: ToolbarUndoIcon, action: onUndo, enabled: canUndo }, { label: "Redo", icon: ToolbarRedoIcon, action: onRedo, enabled: canRedo }, ]; const visibilityTools = [ { label: "Hide selected", icon: ToolbarHideSelectedIcon, action: onToggleHidden, enabled: hasSelection }, { label: "Visibility options", icon: ToolbarCaretDownIcon, action: onTips, enabled: hasSelection }, ]; const combineTools = [ { label: "Group", icon: ToolbarGroupIcon, action: onGroup, enabled: canGroup }, { label: "Ungroup", icon: ToolbarUngroupIcon, action: onUngroup, enabled: canUngroup }, { label: "Boolean Intersection", icon: ToolbarIntersectionIcon, action: onIntersect, enabled: canIntersect }, ]; const modifyTools = [ { label: "Align", icon: ToolbarAlignIcon, action: onAlign, enabled: canAlign, active: alignMode }, { label: "Mirror", icon: ToolbarMirrorIcon, action: onMirror, enabled: hasSelection, active: mirrorMode }, { label: "Snap to grid", icon: ToolbarSnapGridIcon, action: onSnap, enabled: hasSelection }, { label: "Chamfer", icon: ToolbarChamferIcon, action: onChamfer, enabled: canEdgeModify, active: edgeModifierKind === "chamfer" }, { label: "Fillet", icon: ToolbarFilletIcon, action: onFillet, enabled: canEdgeModify, active: edgeModifierKind === "fillet" }, ]; const arrangeTools = [ { label: "Workplane", icon: ToolbarWorkplaneIcon, action: onWorkplaneTool, enabled: true, active: workplaneMode }, { label: "Drop to workplane", icon: ToolbarDropToWorkplaneIcon, action: onDropToWorkplane, enabled: hasSelection }, ]; const renderToolButton = (tool: (typeof leftTools)[number] | (typeof visibilityTools)[number] | (typeof combineTools)[number] | (typeof modifyTools)[number] | (typeof arrangeTools)[number]) => { const { icon: Icon, action, enabled, label } = tool; const active = "active" in tool && Boolean(tool.active); return ( ); }; return (
{toolbarMode === "geometry" ? ( <> {onHome ? (
Home
) : null}
Clipboard
{leftTools.slice(0, 4).map(renderToolButton)}
History
{leftTools.slice(4).map(renderToolButton)}
Shapes
{shapesOpen ? (
Basic Shapes
{toolbarShapeAssets.map((shape) => ( ))}
Manufacturing
) : null}
Visibility
{visibilityTools.map(renderToolButton)}
Combine
{combineTools.map(renderToolButton)}
Modify
{modifyTools.map(renderToolButton)}
Arrange
{arrangeTools.map(renderToolButton)}
Output
) : (
{sketchActive ? ( <>
Draw
Select
History
Inspect
Finish
) : (
Create
)}
)}
); } function TopActionPanel({ panel, shapeCount, scopeLabel, onClose, onExport, onExportStep, stepExporting, onImportFiles, onPickFile, onNotice, }: { panel: Exclude; shapeCount: number; scopeLabel: "selected" | "total"; onClose: () => void; onExport: (format: ExportFormat) => void; onExportStep: () => void; stepExporting: boolean; onImportFiles: (files: FileList | File[]) => void; onPickFile: () => void; onNotice: (message: string) => void; }) { const title = panel === "profile" ? "Profile" : panel === "settings" ? "Settings" : panel === "tips" ? "Tips" : panel === "export" ? "Export" : "Import"; return (
{title}
{panel === "import" ? (
) : null} {panel === "export" ? (

{shapeCount} {scopeLabel} solid shape{shapeCount === 1 ? "" : "s"} ready to export.

STEP keeps boxes, cylinders, spheres and cones as exact CAD geometry (OpenCascade). Other shapes are skipped.

) : null} {panel === "tips" ? (

Click a shape to select it. Use the inspector for dimensions, rotation, solid/hole, color, duplicate, and delete.

) : null} {panel === "settings" ? (

Workspace preferences

) : null} {panel === "profile" ? (
) : null}
); }