Release SketchForge 0.5 edge tools

This commit is contained in:
Formsmith
2026-07-05 12:28:23 +03:00
parent f18bacf3ad
commit 5b0ea94e7b
38 changed files with 3973 additions and 519 deletions
@@ -1,7 +1,128 @@
"use client";
import { Check, LoaderCircle, X } from "lucide-react";
import { useEffect, useState, type CSSProperties } from "react";
import { Check, LoaderCircle, Minus, Plus, RotateCcw, X } from "lucide-react";
import { displayStepFromMillimeters, displayToMillimeters, formatMeasurementNumber, lengthDisplayUnit, millimetersToDisplay } from "@/lib/measurementUnits";
import type { CadModifierKind, CadModifierQuality } from "@/lib/cadModifierTypes";
import type { WorkplaneWorkspaceSettings } from "@/types/sketchforge";
const MIN_EDGE_MODIFIER_AMOUNT = 0.001;
const EDGE_MODIFIER_AMOUNT_STEP = 0.001;
function clamp(value: number, min: number, max: number) {
return Math.min(max, Math.max(min, value));
}
function formatSliderValue(value: number, accuracy: WorkplaneWorkspaceSettings["accuracy"], step: number) {
if (step >= 1) return String(Math.round(value));
return formatMeasurementNumber(value, accuracy, step);
}
type EdgeHistoryOption = {
id: string;
label: string;
targetName: string;
removesNewerCount: number;
};
function EdgeModifierSlider({
label,
value,
min,
max,
step,
unit,
workspace,
length = false,
onChange,
}: {
label: string;
value: number;
min: number;
max: number;
step: number;
unit?: string;
workspace: WorkplaneWorkspaceSettings;
length?: boolean;
onChange: (value: number) => void;
}) {
const safeMin = Number.isFinite(min) ? min : 0;
const safeMax = Math.max(safeMin, Number.isFinite(max) ? max : safeMin);
const actualValue = Number.isFinite(value) ? value : safeMin;
const controlValue = length ? millimetersToDisplay(actualValue, workspace) : actualValue;
const controlMin = length ? millimetersToDisplay(safeMin, workspace) : safeMin;
const controlMax = length ? millimetersToDisplay(safeMax, workspace) : safeMax;
const controlStep = length ? displayStepFromMillimeters(step, workspace) : step;
const sliderValue = clamp(controlValue, controlMin, controlMax);
const position = ((sliderValue - controlMin) / Math.max(Number.EPSILON, controlMax - controlMin)) * 100;
const [editing, setEditing] = useState(false);
const [draft, setDraft] = useState(formatSliderValue(controlValue, workspace.accuracy, controlStep));
const unitLabel = length ? lengthDisplayUnit(workspace).label : unit;
useEffect(() => {
if (!editing) {
setDraft(formatSliderValue(controlValue, workspace.accuracy, controlStep));
}
}, [controlStep, controlValue, editing, workspace.accuracy]);
const toModelValue = (nextValue: number) => length ? displayToMillimeters(nextValue, workspace) : nextValue;
const commitDraft = () => {
const next = Number(draft);
const finiteNext = Number.isFinite(next) ? next : controlValue;
onChange(clamp(toModelValue(finiteNext), safeMin, safeMax));
setEditing(false);
};
const handleSliderChange = (nextValue: number) => {
const next = clamp(Number.isFinite(nextValue) ? nextValue : controlMin, controlMin, controlMax);
onChange(clamp(toModelValue(next), safeMin, safeMax));
setDraft(formatSliderValue(next, workspace.accuracy, controlStep));
};
return (
<label className="edge-modifier-field edge-modifier-slider range-property" style={{ "--slider-pos": `${position}%` } as CSSProperties}>
<span className="range-property-header">
<span className="range-property-name">{label}</span>
<span className="range-value-control">
<input
type="number"
min={controlMin}
max={controlMax}
step={controlStep}
value={editing ? draft : formatSliderValue(controlValue, workspace.accuracy, controlStep)}
inputMode="decimal"
onFocus={() => {
setDraft(formatSliderValue(controlValue, workspace.accuracy, controlStep));
setEditing(true);
}}
onChange={(event) => setDraft(event.currentTarget.value)}
onBlur={commitDraft}
onKeyDown={(event) => {
if (event.key === "Enter") {
event.currentTarget.blur();
} else if (event.key === "Escape") {
setDraft(formatSliderValue(controlValue, workspace.accuracy, controlStep));
setEditing(false);
}
}}
/>
{unitLabel ? <span className="range-value-unit">{unitLabel}</span> : null}
</span>
</span>
<div className="range-control">
<input
type="range"
min={controlMin}
max={controlMax}
step={controlStep}
value={sliderValue}
onChange={(event) => handleSliderChange(Number(event.currentTarget.value))}
/>
</div>
</label>
);
}
export function EdgeModifierPanel({
kind,
@@ -10,12 +131,14 @@ export function EdgeModifierPanel({
chamferAngle,
quality,
sharpAngle,
workspace,
tangentChain,
preserveEdgeSize,
targetName,
groupedCount,
appliedFeatureCount,
reversibleFeatureCount,
historyOptions,
selectedCount,
availableCount,
busy,
@@ -28,7 +151,7 @@ export function EdgeModifierPanel({
onPreserveEdgeSizeChange,
onSelectAll,
onClear,
onRemoveLastFeature,
onRemoveFeature,
onApply,
onCancel,
}: {
@@ -38,12 +161,14 @@ export function EdgeModifierPanel({
chamferAngle: number;
quality: CadModifierQuality;
sharpAngle: number;
workspace: WorkplaneWorkspaceSettings;
tangentChain: boolean;
preserveEdgeSize: boolean;
targetName: string;
groupedCount: number;
appliedFeatureCount: number;
reversibleFeatureCount: number;
historyOptions: EdgeHistoryOption[];
selectedCount: number;
availableCount: number;
busy: boolean;
@@ -56,11 +181,14 @@ export function EdgeModifierPanel({
onPreserveEdgeSizeChange: (value: boolean) => void;
onSelectAll: () => void;
onClear: () => void;
onRemoveLastFeature: () => void;
onRemoveFeature: (id: string) => void;
onApply: () => void;
onCancel: () => void;
}) {
const [historyOpen, setHistoryOpen] = useState(false);
const title = kind === "fillet" ? "Fillet edges" : "Chamfer edges";
const amountMin = Math.min(MIN_EDGE_MODIFIER_AMOUNT, Math.max(Number.EPSILON, maxAmount));
const amountMax = Math.max(amountMin, maxAmount);
return (
<aside className="edge-modifier-panel" aria-label={title}>
<div className="edge-modifier-header">
@@ -87,55 +215,50 @@ export function EdgeModifierPanel({
{appliedFeatureCount > 0 ? (
<div className="edge-modifier-history-actions">
<button type="button" disabled={reversibleFeatureCount === 0} onClick={onRemoveLastFeature}>
Remove last edge feature
<button
className="edge-modifier-history-toggle"
type="button"
aria-expanded={historyOpen}
disabled={reversibleFeatureCount === 0}
onClick={() => setHistoryOpen((open) => !open)}
>
{historyOpen ? <Minus size={15} /> : <Plus size={15} />}
<span>Edge feature history</span>
</button>
{reversibleFeatureCount === 0 ? <span>Older edge features do not have stored undo history.</span> : null}
{historyOpen && historyOptions.length > 0 ? (
<div className="edge-modifier-history-list">
{historyOptions.map((option) => (
<button className="edge-modifier-history-item" type="button" key={option.id} onClick={() => onRemoveFeature(option.id)}>
<RotateCcw size={14} />
<span>
<strong>{option.label}</strong>
<small>
{option.targetName}
{option.removesNewerCount > 0 ? ` · also removes ${option.removesNewerCount} newer` : ""}
</small>
</span>
</button>
))}
</div>
) : null}
</div>
) : null}
<label className="edge-modifier-field">
<span>{kind === "fillet" ? "Radius" : "Distance"}</span>
<div className="edge-modifier-value-row">
<input
type="range"
min="0.05"
max={Math.max(0.05, maxAmount)}
step="0.05"
value={Math.min(amount, maxAmount)}
onChange={(event) => onAmountChange(Number(event.currentTarget.value))}
/>
<input
type="number"
min="0.05"
max={maxAmount}
step="0.05"
value={amount}
onChange={(event) => onAmountChange(Number(event.currentTarget.value))}
/>
<span>mm</span>
</div>
</label>
<EdgeModifierSlider
label={kind === "fillet" ? "Radius" : "Distance"}
value={amount}
min={amountMin}
max={amountMax}
step={EDGE_MODIFIER_AMOUNT_STEP}
workspace={workspace}
length
onChange={onAmountChange}
/>
{kind === "chamfer" ? (
<label className="edge-modifier-field">
<span>Angle</span>
<div className="edge-modifier-value-row">
<input type="range" min="5" max="85" step="1" value={chamferAngle} onChange={(event) => onChamferAngleChange(Number(event.currentTarget.value))} />
<input type="number" min="5" max="85" step="1" value={chamferAngle} onChange={(event) => onChamferAngleChange(Number(event.currentTarget.value))} />
<span>°</span>
</div>
</label>
) : null}
{kind === "chamfer" ? <EdgeModifierSlider label="Angle" value={chamferAngle} min={5} max={85} step={1} unit="deg" workspace={workspace} onChange={onChamferAngleChange} /> : null}
<label className="edge-modifier-field">
<span>Sharp-edge threshold</span>
<div className="edge-modifier-value-row">
<input type="range" min="1" max="120" step="1" value={sharpAngle} onChange={(event) => onSharpAngleChange(Number(event.currentTarget.value))} />
<input type="number" min="1" max="120" step="1" value={sharpAngle} onChange={(event) => onSharpAngleChange(Number(event.currentTarget.value))} />
<span>°</span>
</div>
</label>
<EdgeModifierSlider label="Sharp-edge threshold" value={sharpAngle} min={1} max={120} step={1} unit="deg" workspace={workspace} onChange={onSharpAngleChange} />
<label className="edge-modifier-check">
<input type="checkbox" checked={tangentChain} onChange={(event) => onTangentChainChange(event.currentTarget.checked)} />
@@ -1,10 +1,11 @@
"use client";
import { ChevronDown, ChevronUp, LockKeyhole, LockKeyholeOpen } from "lucide-react";
import { ChevronDown, ChevronUp, LockKeyhole, LockKeyholeOpen, Split } from "lucide-react";
import { useEffect, useState, type CSSProperties, type Dispatch, type SetStateAction } from "react";
import { ToolbarHideSelectedIcon } from "@/components/icons";
import { displayStepFromMillimeters, displayToMillimeters, formatMeasurementNumber, lengthDisplayUnit, millimetersToDisplay } from "@/lib/measurementUnits";
import { fallbackSolidColor, resizedShapeSize, shapeDepth, shapeWidth } from "@/lib/workplaneShapes";
import type { GridSize, MeasurementAccuracy, WorkplaneShape } from "@/types/sketchforge";
import type { GridSize, MeasurementAccuracy, WorkplaneShape, WorkplaneWorkspaceSettings } from "@/types/sketchforge";
const GRID_SIZES: GridSize[] = ["Off", "0.1 mm", "0.25 mm", "0.5 mm", "1.0 mm", "2.0 mm", "5.0 mm", "Brick"];
const MIN_SHAPE_SIZE = 0.01;
@@ -73,9 +74,13 @@ function clamp(value: number, min: number, max: number) {
return Math.min(max, Math.max(min, value));
}
function formatPanelNumber(value: number, accuracy: MeasurementAccuracy) {
const zeroThreshold = 0.5 * 10 ** -accuracy;
return (Math.abs(value) < zeroThreshold ? 0 : value).toFixed(accuracy);
function formatPropertyNumber(value: number, accuracy: MeasurementAccuracy, step: number) {
if (step >= 1) return String(Math.round(value));
return formatMeasurementNumber(value, accuracy, step);
}
function propertyUsesLengthUnit(label: string) {
return ["Radius", "Length", "Width", "Height", "Bevel", "Top Radius", "Base Radius", "Thickness"].includes(label);
}
function getShapeProperties(shape: WorkplaneShape, onUpdate: ShapeInspectorUpdate): ShapePropertyConfig[] {
@@ -92,8 +97,6 @@ function getShapeProperties(shape: WorkplaneShape, onUpdate: ShapeInspectorUpdat
if (shape.kind === "box") {
return [
{ label: "Radius", value: shape.radius ?? 0, min: 0, max: 10, onChange: (radius) => onUpdate({ radius }) },
{ label: "Steps", value: shape.steps ?? 10, min: 1, max: 64, step: 1, onChange: (steps) => onUpdate({ steps: Math.round(steps) }) },
{ label: "Length", value: depth, min: MIN_SHAPE_SIZE, max: 160, onChange: setDepth },
{ label: "Width", value: width, min: MIN_SHAPE_SIZE, max: 160, onChange: setWidth },
{ label: "Height", value: shape.height, min: MIN_SHAPE_SIZE, max: 160, onChange: setHeight },
@@ -197,22 +200,26 @@ export function ShapeInspector({
shape,
snap,
snapOpen,
accuracy,
workspace,
onUpdate,
onClose,
onSnapChange,
onSnapOpenChange,
onEditSketch,
canSeparateParts = false,
onSeparateParts,
}: {
shape: WorkplaneShape;
snap: GridSize;
snapOpen: boolean;
accuracy: MeasurementAccuracy;
workspace: WorkplaneWorkspaceSettings;
onUpdate: ShapeInspectorUpdate;
onClose: () => void;
onSnapChange: Dispatch<SetStateAction<GridSize>>;
onSnapOpenChange: Dispatch<SetStateAction<boolean>>;
onEditSketch?: () => void;
canSeparateParts?: boolean;
onSeparateParts?: () => void;
}) {
const solidColor = shape.hole ? fallbackSolidColor(shape) : shape.color;
const locked = Boolean(shape.locked);
@@ -310,6 +317,13 @@ export function ShapeInspector({
</button>
) : null}
{canSeparateParts && onSeparateParts ? (
<button className="inspector-action-button" type="button" disabled={locked} onClick={onSeparateParts}>
<Split size={17} strokeWidth={2.5} />
<span>Separate Parts</span>
</button>
) : null}
<div className="property-card">
<button
className="property-card-header"
@@ -330,7 +344,7 @@ export function ShapeInspector({
if (property.type === "select") {
return <SelectProperty key={property.label} {...property} disabled={locked} />;
}
return <RangeProperty key={property.label} {...property} accuracy={accuracy} disabled={locked} />;
return <RangeProperty key={property.label} {...property} workspace={workspace} disabled={locked} />;
})}
</div>
) : null}
@@ -386,76 +400,81 @@ function RangeProperty({
min,
max,
step = 0.01,
accuracy,
workspace,
disabled,
onChange,
}: RangePropertyConfig & { accuracy: MeasurementAccuracy; disabled?: boolean }) {
}: RangePropertyConfig & { workspace: WorkplaneWorkspaceSettings; disabled?: boolean }) {
const allowsAboveSliderMax = label === "Length" || label === "Width" || label === "Height";
const isLength = propertyUsesLengthUnit(label);
const accuracy = workspace.accuracy;
const actualValue = Math.max(min, Number.isFinite(value) ? value : min);
const sliderValue = clamp(actualValue, min, max);
const position = ((sliderValue - min) / Math.max(1, max - min)) * 100;
const controlValue = isLength ? millimetersToDisplay(actualValue, workspace) : actualValue;
const controlMin = isLength ? millimetersToDisplay(min, workspace) : min;
const controlMax = isLength ? millimetersToDisplay(max, workspace) : max;
const controlStep = isLength ? displayStepFromMillimeters(step, workspace) : step;
const sliderValue = clamp(controlValue, controlMin, controlMax);
const position = ((sliderValue - controlMin) / Math.max(Number.EPSILON, controlMax - controlMin)) * 100;
const [editing, setEditing] = useState(false);
const [draft, setDraft] = useState(formatPanelNumber(actualValue, accuracy));
const [draft, setDraft] = useState(formatPropertyNumber(controlValue, accuracy, controlStep));
const unit = isLength ? lengthDisplayUnit(workspace).label : null;
useEffect(() => {
if (!editing) {
setDraft(formatPanelNumber(actualValue, accuracy));
setDraft(formatPropertyNumber(controlValue, accuracy, controlStep));
}
}, [accuracy, actualValue, editing]);
}, [accuracy, controlStep, controlValue, editing]);
const toModelValue = (nextValue: number) => isLength ? displayToMillimeters(nextValue, workspace) : nextValue;
const commitDraft = () => {
const next = Number(draft);
const finiteNext = Number.isFinite(next) ? next : actualValue;
onChange(allowsAboveSliderMax ? Math.max(min, finiteNext) : clamp(finiteNext, min, max));
const finiteNext = Number.isFinite(next) ? next : controlValue;
const nextModelValue = toModelValue(finiteNext);
onChange(allowsAboveSliderMax ? Math.max(min, nextModelValue) : clamp(nextModelValue, min, max));
setEditing(false);
};
const handleSliderChange = (nextValue: number) => {
const next = clamp(Number.isFinite(nextValue) ? nextValue : controlMin, controlMin, controlMax);
onChange(clamp(toModelValue(next), min, max));
setDraft(formatPropertyNumber(next, accuracy, controlStep));
};
return (
<label className="range-property" style={{ "--slider-pos": `${position}%` } as CSSProperties}>
<span>{label}</span>
<div className="range-control">
{editing ? (
<span className="range-property-header">
<span className="range-property-name">{label}</span>
<span className="range-value-control">
<input
className="range-value-input"
type="number"
min={min}
max={allowsAboveSliderMax ? undefined : max}
step={step}
value={draft}
autoFocus
min={controlMin}
max={allowsAboveSliderMax ? undefined : controlMax}
step={controlStep}
value={editing ? draft : formatPropertyNumber(controlValue, accuracy, controlStep)}
disabled={disabled}
inputMode="decimal"
onFocus={() => {
setDraft(formatPropertyNumber(controlValue, accuracy, controlStep));
setEditing(true);
}}
onChange={(event) => setDraft(event.currentTarget.value)}
onBlur={commitDraft}
onKeyDown={(event) => {
if (event.key === "Enter") {
event.currentTarget.blur();
} else if (event.key === "Escape") {
setDraft(formatPanelNumber(actualValue, accuracy));
setDraft(formatPropertyNumber(controlValue, accuracy, controlStep));
setEditing(false);
}
}}
/>
) : (
<output
onDoubleClick={(event) => {
if (disabled) {
return;
}
event.preventDefault();
setDraft(formatPanelNumber(actualValue, accuracy));
setEditing(true);
}}
>
{formatPanelNumber(actualValue, accuracy)}
</output>
)}
{unit ? <span className="range-value-unit">{unit}</span> : null}
</span>
</span>
<div className="range-control">
<input
type="range"
min={min}
max={max}
step={step}
min={controlMin}
max={controlMax}
step={controlStep}
value={sliderValue}
disabled={disabled}
onChange={(event) => {
const next = Number(event.currentTarget.value);
onChange(clamp(Number.isFinite(next) ? next : min, min, max));
}}
onChange={(event) => handleSliderChange(Number(event.currentTarget.value))}
/>
</div>
</label>
@@ -2,6 +2,7 @@
import { Grid3X3, Palette, Ruler, X } from "lucide-react";
import { useState, type CSSProperties } from "react";
import { normalizeScaleForUnits, scaleOptionsForUnits, WORKSPACE_UNIT_OPTIONS } from "@/lib/measurementUnits";
import { DEFAULT_WORKPLANE_WORKSPACE } from "@/lib/workplaneSettings";
import type { GridSize, WorkplaneWorkspaceSettings } from "@/types/sketchforge";
@@ -51,9 +52,12 @@ export function WorkspaceSettingsModal({
}) {
const [defaultSaved, setDefaultSaved] = useState(false);
const [activeSection, setActiveSection] = useState<WorkspaceSettingsSection>("appearance");
const scaleOptions = scaleOptionsForUnits(workspace.units);
const scaleValue = normalizeScaleForUnits(workspace.units, workspace.scale);
const patchWorkspace = (patch: Partial<WorkspaceSettings>) => {
setDefaultSaved(false);
onWorkspaceChange({ ...workspace, ...patch });
const next = { ...workspace, ...patch };
onWorkspaceChange({ ...next, scale: normalizeScaleForUnits(next.units, next.scale) });
};
const setDimension = (key: "width" | "depth", value: string) => {
const next = clamp(Number.parseFloat(value) || DEFAULT_WORKPLANE_WORKSPACE[key], MIN_WORKSPACE_SIZE, MAX_WORKSPACE_SIZE);
@@ -151,13 +155,13 @@ export function WorkspaceSettingsModal({
<WorkspaceSelect
label="Units"
value={workspace.units}
options={["Metric (Default)", "Imperial", "Bricks"]}
options={WORKSPACE_UNIT_OPTIONS}
onChange={(units) => patchWorkspace({ units })}
/>
<WorkspaceSelect
label="Scale"
value={workspace.scale}
options={["1:1 (millimeters)", "1:10 (centimeters)", "1:100 (meters)", "1:1000 (meters)"]}
value={scaleValue}
options={scaleOptions}
onChange={(scale) => patchWorkspace({ scale })}
/>
<WorkspaceSelect