From a13b1f221d4f26a7e6cef70ba32d3072d1624ea5 Mon Sep 17 00:00:00 2001 From: wangdequan Date: Fri, 13 Nov 2026 22:53:15 +0800 Subject: [PATCH] Apply unit preference to property display Co-Authored-By: Claude Code --- .../current/desktop-parity-continuation.md | 7 +++++-- web/src/project-shell.tsx | 16 ++++++++++++---- web/src/shell/preferences-dialog.tsx | 1 + web/src/shell/units.ts | 19 +++++++++++++++++++ 4 files changed, 37 insertions(+), 6 deletions(-) create mode 100644 web/src/shell/units.ts diff --git a/web/docs/current/desktop-parity-continuation.md b/web/docs/current/desktop-parity-continuation.md index f2824878..b40b6a49 100644 --- a/web/docs/current/desktop-parity-continuation.md +++ b/web/docs/current/desktop-parity-continuation.md @@ -62,8 +62,11 @@ - **底部停靠面板**:`shell/bottom-dock.tsx` + Windows 菜单入口。 - Selection view 显示当前选择对象;Report view 保留最近 50 条状态; - 实测打开/切换正常,0 pageError;截图 `bottom-dock.png`。 -- 说明:原生 `BaseApp/Preferences` 参数同步与单位制驱动显示待 P3; - 快捷键仍仅 localStorage;Python 控制台尚未接入。 +- **单位制已驱动属性显示**:`shell/units.ts`;Preferences 修改后广播 + `freecad-preferences-changed`,属性面板 Bounds 标签与数值、Volume 按 mm/m/in/cm + 换算显示。实测 mm → m 切换生效,0 pageError。 + - 输入框仍以 mm 写入内核,显示层换算;原生 `BaseApp/Preferences` 同步待 P3。 +- 说明:快捷键仍仅 localStorage;Python 控制台尚未接入。 - 非 Part 菜单命令改为打开 **React TaskFrame 模态**(`web/src/shell/task-frame.tsx`), iframe 复用 `/workbench.html?module=&command=` 的现有任务面板; BIM_TogglePanels 实测弹出 "Bottom panels" 任务并可点按钮,0 pageError; diff --git a/web/src/project-shell.tsx b/web/src/project-shell.tsx index bdc9224a..f98a27f8 100644 --- a/web/src/project-shell.tsx +++ b/web/src/project-shell.tsx @@ -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>(() => 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>({}); const [csvPanel, setCsvPanel] = useState<{ id: string; mode: "import" | "export"; selected?: string; done(result: Record): void } | null>(null); @@ -3125,11 +3133,11 @@ function App() {
} - {typeof selectedObject?.properties.volume === "number" ?
Volume{selectedObject.properties.volume.toFixed(6)}
: null} + {typeof selectedObject?.properties.volume === "number" ?
Volume{formatVolume(selectedObject.properties.volume, unitSystem)}
: null} {Array.isArray(selectedObject?.properties.bounds) && selectedObject.properties.bounds.length === 6 && -
Bounds (mm) +
Bounds ({unitInfo(unitSystem).symbol}) {selectedObject.properties.bounds.map((value, index) => - {["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)} )}
}
{active || "none"}{objects.length} objects
diff --git a/web/src/shell/preferences-dialog.tsx b/web/src/shell/preferences-dialog.tsx index 25af8b0f..4f504cac 100644 --- a/web/src/shell/preferences-dialog.tsx +++ b/web/src/shell/preferences-dialog.tsx @@ -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 = (key: K, value: WebPreferences[K]) => setPreferences(previous => ({ ...previous, [key]: value })); diff --git a/web/src/shell/units.ts b/web/src/shell/units.ts new file mode 100644 index 00000000..787d0df0 --- /dev/null +++ b/web/src/shell/units.ts @@ -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}³`; +}