Apply unit preference to property display

Co-Authored-By: Claude Code <noreply@anthropic.com>
This commit is contained in:
2026-11-13 22:53:15 +08:00
parent 26c0963363
commit a13b1f221d
4 changed files with 37 additions and 6 deletions

View File

@@ -86,7 +86,8 @@ import { ParityWorkbenchMenu } from "./shell/workbench-menu.js";
import { TaskFrame } from "./shell/task-frame.js";
import { ParityWorkbenchToolbars } from "./shell/workbench-toolbars.js";
import { UtilityMenus } from "./shell/utility-menus.js";
import { PreferencesDialog } from "./shell/preferences-dialog.js";
import { PreferencesDialog, loadWebPreferences } from "./shell/preferences-dialog.js";
import { formatLength, formatVolume, unitInfo } from "./shell/units.js";
import { ShortcutEditor, ShortcutManager } from "./shell/shortcut-manager.js";
import { BottomDock } from "./shell/bottom-dock.js";
import { DocumentTree, type DocumentTreeNode } from "./document-tree.js";
@@ -320,8 +321,15 @@ function App() {
const [workbenches, setWorkbenches] = useState<Record<string, "Part" | "Sketcher" | "PartDesign" | "Spreadsheet" | "Surface" | "Assembly" | "Draft" | "Architecture" | "TechDraw" | "CAM">>(() => initialWorkbench ? { [initialDocumentId]: initialWorkbench } : {});
const workbench = workbenches[active] ?? "Part";
useEffect(() => { if (status) setReportLog(previous => previous[0] === status ? previous : [status, ...previous].slice(0, 50)); }, [status]);
useEffect(() => {
const refresh = () => setUnitSystem(loadWebPreferences().unitSystem);
window.addEventListener("freecad-preferences-changed", refresh);
window.addEventListener("storage", refresh);
return () => { window.removeEventListener("freecad-preferences-changed", refresh); window.removeEventListener("storage", refresh); };
}, []);
const [parityTask, setParityTask] = useState<{ module: string; command: string } | null>(null);
const [preferencesOpen, setPreferencesOpen] = useState(false);
const [unitSystem, setUnitSystem] = useState(() => loadWebPreferences().unitSystem);
const [shortcutsOpen, setShortcutsOpen] = useState(false);
const [selectedSheets, setSelectedSheets] = useState<Record<string, string>>({});
const [csvPanel, setCsvPanel] = useState<{ id: string; mode: "import" | "export"; selected?: string; done(result: Record<string, unknown>): void } | null>(null);
@@ -3125,11 +3133,11 @@ function App() {
<div className="primitive-actions"><button type="button" data-create-primitive onClick={() => void createOrEditPrimitive(false)}><ToolIcon nodes={Plus} />创建{primitives[primitive].label}</button>
<button type="button" data-update-primitive disabled={selectedObject?.type !== `Part::${primitive}`} onClick={() => void createOrEditPrimitive(true)}><ToolIcon nodes={RefreshCw} />重算</button></div>
</fieldset>}
{typeof selectedObject?.properties.volume === "number" ? <div className="stat"><strong>Volume</strong><small data-native-volume>{selectedObject.properties.volume.toFixed(6)}</small></div> : null}
{typeof selectedObject?.properties.volume === "number" ? <div className="stat"><strong>Volume</strong><small data-native-volume>{formatVolume(selectedObject.properties.volume, unitSystem)}</small></div> : null}
{Array.isArray(selectedObject?.properties.bounds) && selectedObject.properties.bounds.length === 6 &&
<div className="stat"><strong>Bounds (mm)</strong><small data-native-bounds={JSON.stringify(selectedObject.properties.bounds)}>
<div className="stat"><strong>Bounds ({unitInfo(unitSystem).symbol})</strong><small data-native-bounds={JSON.stringify(selectedObject.properties.bounds)}>
{selectedObject.properties.bounds.map((value, index) => <span key={index} style={{ display: "block" }}>
{["Min X", "Min Y", "Min Z", "Max X", "Max Y", "Max Z"][index]}: {Number(value).toFixed(6)}
{["Min X", "Min Y", "Min Z", "Max X", "Max Y", "Max Z"][index]}: {formatLength(Number(value), unitSystem, 4)}
</span>)}
</small></div>}
<div className="stat"><strong>{active || "none"}</strong><small>{objects.length} objects</small></div>

View File

@@ -36,6 +36,7 @@ export function PreferencesDialog({ onClose }: { onClose: () => void }) {
useEffect(() => {
localStorage.setItem(KEY, JSON.stringify(preferences));
if (hydrated.current) void saveSettings("web-preferences.json", preferences);
window.dispatchEvent(new Event("freecad-preferences-changed"));
}, [preferences]);
const update = <K extends keyof WebPreferences>(key: K, value: WebPreferences[K]) =>
setPreferences(previous => ({ ...previous, [key]: value }));

19
web/src/shell/units.ts Normal file
View File

@@ -0,0 +1,19 @@
export type UnitInfo = { symbol: string; lengthFactor: number; volumeFactor: number };
export function unitInfo(unitSystem: string): UnitInfo {
if (unitSystem.startsWith("MKS")) return { symbol: "m", lengthFactor: 0.001, volumeFactor: 1e-9 };
if (unitSystem.startsWith("Imperial")) return { symbol: "in", lengthFactor: 1 / 25.4, volumeFactor: 1 / (25.4 ** 3) };
if (unitSystem.startsWith("Building US")) return { symbol: "in", lengthFactor: 1 / 25.4, volumeFactor: 1 / (25.4 ** 3) };
if (unitSystem.startsWith("Building cm")) return { symbol: "cm", lengthFactor: 0.1, volumeFactor: 0.001 };
return { symbol: "mm", lengthFactor: 1, volumeFactor: 1 };
}
export function formatLength(valueMm: number, unitSystem: string, digits = 3) {
const info = unitInfo(unitSystem);
return `${(valueMm * info.lengthFactor).toFixed(digits)} ${info.symbol}`;
}
export function formatVolume(valueMm3: number, unitSystem: string, digits = 4) {
const info = unitInfo(unitSystem);
return `${(valueMm3 * info.volumeFactor).toFixed(digits)} ${info.symbol}³`;
}