// 3D-Modell-Import (OBJ/STL/glTF/GLB/PLY → Kontext-Meshes): verstecktes Datei-Feld + Dialog mit Einheit, // Achse und Platzierung. Einmal in App eingehängt (`MeshImportHost`); geöffnet über Menü // (`openMeshFileDialog`) oder Drag & Drop (`requestMeshImport`). Die Datei wird beim Öffnen und bei jeder // Änderung von Einheit/Achse im Web-Worker gelesen (`readMesh`); der Dialog zeigt die erkannte Achse und die // Ausdehnung (beides aus dem Kern), „Importieren" übernimmt das zuletzt gelesene Ergebnis (kein zweites Lesen). // // Muster: PointCloudImportDialog (abgedunkeltes Overlay, Esc/Hintergrund schliessen). import { useEffect, useRef, useState, memo } from "react"; import { t } from "../i18n"; import { Dropdown } from "./Dropdown"; import { MESH_EXTENSIONS, MESH_SIDECAR_EXTENSIONS, MESH_UNITS, MISSING_EXTERNAL_PREFIX, NO_FACES_PREFIX, readMesh, } from "../io/meshImport"; import type { MeshPlacement, MeshRead, MeshUnit, MeshUp } from "../io/meshImport"; import { closeMeshImport, registerMeshFileInput, requestMeshImport, splitMeshFiles, useMeshImportRequest } from "../state/meshImportStore"; import type { MeshImportRequest } from "../state/meshImportStore"; import { requestPointCloudImport } from "../state/pointCloudStore"; export interface MeshImportHostProps { /** Übernehmen: der Kern macht aus dem gelesenen Modell Kontext-Meshes (`ProjectDocument.importMesh`). */ onImport: (read: MeshRead, fileName: string, placement: MeshPlacement) => void; notify: (message: string, kind?: "info" | "warning" | "error") => void; } const fmt = (n: number) => (Math.abs(n) >= 100 ? n.toFixed(0) : n.toFixed(1)); /** Namen der fehlenden Begleitdateien aus der Rust-Meldung. */ function missingList(message: string): string { const m = /fehlen: (.*?) \(mit der/.exec(message); return m ? m[1] : message; } async function bytesOf(f: File): Promise { return new Uint8Array(await f.arrayBuffer()); } function MeshImportDialog({ req, onImport, notify }: { req: MeshImportRequest } & MeshImportHostProps) { const { file, sidecars } = req; const isGltf = /\.(gltf|glb)$/i.test(file.name); const [unit, setUnit] = useState("m"); const [up, setUp] = useState("auto"); const [placement, setPlacement] = useState("center"); const [busy, setBusy] = useState(true); const [error, setError] = useState(null); const [result, setResult] = useState(null); const seq = useRef(0); // Dateiinhalt einmal laden, nicht bei jeder Änderung. const cache = useRef<{ main: Uint8Array; files: { name: string; bytes: Uint8Array }[] } | null>(null); useEffect(() => { const onKey = (e: KeyboardEvent) => { if (e.key === "Escape") closeMeshImport(); }; window.addEventListener("keydown", onKey); return () => window.removeEventListener("keydown", onKey); }, []); useEffect(() => { const id = ++seq.current; setBusy(true); setError(null); void (async () => { try { cache.current ??= { main: await bytesOf(file), files: await Promise.all(sidecars.map(async (f) => ({ name: f.name, bytes: await bytesOf(f) }))), }; const r = await readMesh(cache.current.main, file.name, { unit, up, files: cache.current.files }); if (id !== seq.current) return; setResult(r); } catch (e) { if (id !== seq.current) return; const msg = e instanceof Error ? e.message : String(e); if (msg.startsWith(NO_FACES_PREFIX)) { // PLY ohne Flächen = Punktwolke: Dialog wechseln. closeMeshImport(); notify(t("mesh.import.noFaces"), "info"); requestPointCloudImport(file); return; } setResult(null); setError( msg.startsWith(MISSING_EXTERNAL_PREFIX) ? t("mesh.import.missingExternal", { list: missingList(msg) }) : `${t("mesh.import.failed")}: ${msg}`, ); } finally { if (id === seq.current) setBusy(false); } })(); }, [file, sidecars, unit, up]); const dims = result?.extent ?? null; const r = result?.report; const onDo = () => { if (!result) return; closeMeshImport(); onImport(result, file.name, placement); }; return (
closeMeshImport()}>
e.stopPropagation()}>
{t("mesh.import.title")}
{file.name} · {(file.size / 1e6).toFixed(1)} MB
{sidecars.length > 0 &&
{t("mesh.importDialog.files", { list: sidecars.map((f) => f.name).join(", ") })}
}
{t("mesh.importDialog.unit")}
setUnit(v as MeshUnit)} title={t("mesh.importDialog.unit")} options={MESH_UNITS.map((u) => ({ value: u, label: t(`mesh.importDialog.unit.${u}`), disabled: isGltf && u !== "m" }))} /> {isGltf &&
{t("mesh.importDialog.unitFixed")}
}
{t("mesh.importDialog.up")}
{(["auto", "z", "y"] as const).map((u) => ( ))} {isGltf &&
{t("mesh.importDialog.upFixed")}
}
{t("mesh.importDialog.placement")}
{(["center", "original"] as const).map((p) => ( ))}
{busy &&
{t("mesh.importDialog.reading")}
} {error &&
{error}
} {r && dims && !busy && (
{r.upAuto && !r.upCertain ? t("mesh.importDialog.detectedUncertain", { axis: r.up.toUpperCase(), n: r.objects, tris: r.triangles }) : t("mesh.importDialog.detected", { axis: r.up.toUpperCase(), n: r.objects, tris: r.triangles, size: fmt(dims[0]), depth: fmt(dims[1]), height: fmt(dims[2]), })}
{r.truncated &&
{t("mesh.import.truncated")}
} {r.warnings.map((w) => (
{w}
))}
)}
); } /** Verstecktes Datei-Feld (mehrere Dateien: Modell + `.mtl`/`.bin`) + Dialog; einmal in der App einhängen. */ export const MeshImportHost = memo(function MeshImportHost(props: MeshImportHostProps) { const req = useMeshImportRequest(); return ( <> "." + e).join(",")} style={{ display: "none" }} data-testid="mesh-input" onChange={(e) => { const files = Array.from(e.target.files ?? []); e.target.value = ""; const r = splitMeshFiles(files); if (r) requestMeshImport(r.file, r.sidecars); }} /> {req && } ); });