Files
DOSSIER/src/ui/MeshImportDialog.tsx
T
Claude 3ec5f42609 [121] App.tsx zerlegt, Oberleiste/Statusleiste/Befehlszeile/Werkzeuge memoisiert
Herausgelöst: Befehls-Engine (state/useCommandEngineHost), 3D-Griffe (state/useGrip3dEditing),
Layout-Blatt (state/useSheetPane), Darstellungs-Resolver (state/useDisplayResolvers).
Stabile Handler über useEvent/useEventBundle; memo für TopBar (nur noch projectName),
AppMenuBar, ViewRibbonTab, StatusBar, CommandLine, Werkzeug-/Befehls-Icons, Overlays.
Panels: PanelHostProvider mit Store, usePanelHostSelect/usePanelHostAction — die
Werkzeug-Palette abonniert nur ihre Felder. Wand-Änderung: 413 → 291 Renders.
2026-11-03 20:50:00 +00:00

220 lines
9.0 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// 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<Uint8Array> {
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<MeshUnit>("m");
const [up, setUp] = useState<MeshUp>("auto");
const [placement, setPlacement] = useState<MeshPlacement>("center");
const [busy, setBusy] = useState(true);
const [error, setError] = useState<string | null>(null);
const [result, setResult] = useState<MeshRead | null>(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 (
<div className="imp-overlay" onClick={() => closeMeshImport()}>
<div className="imp-dialog" role="dialog" aria-modal="true" aria-label={t("mesh.import.title")} onClick={(e) => e.stopPropagation()}>
<header className="imp-head">
<span className="imp-head-title">{t("mesh.import.title")}</span>
<button className="imp-close" onClick={() => closeMeshImport()} aria-label={t("export.close")}>
×
</button>
</header>
<div className="imp-body">
<div className="imp-file">
<span className="imp-file-name" title={file.name}>
{file.name} · {(file.size / 1e6).toFixed(1)} MB
</span>
</div>
{sidecars.length > 0 && <div className="imp-note">{t("mesh.importDialog.files", { list: sidecars.map((f) => f.name).join(", ") })}</div>}
<section className="imp-section">
<div className="imp-section-title">{t("mesh.importDialog.unit")}</div>
<Dropdown
value={isGltf ? "m" : unit}
onChange={(v) => 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 && <div className="imp-note">{t("mesh.importDialog.unitFixed")}</div>}
</section>
<section className="imp-section">
<div className="imp-section-title">{t("mesh.importDialog.up")}</div>
{(["auto", "z", "y"] as const).map((u) => (
<label key={u} className="imp-radio">
<input type="radio" name="mesh-up" disabled={isGltf} checked={(isGltf ? "y" : up) === u} onChange={() => setUp(u)} />
<span>{t(`mesh.importDialog.up.${u}`)}</span>
</label>
))}
{isGltf && <div className="imp-note">{t("mesh.importDialog.upFixed")}</div>}
</section>
<section className="imp-section">
<div className="imp-section-title">{t("mesh.importDialog.placement")}</div>
{(["center", "original"] as const).map((p) => (
<label key={p} className="imp-radio">
<input type="radio" name="mesh-placement" checked={placement === p} onChange={() => setPlacement(p)} />
<span>{t(`mesh.importDialog.placement.${p}`)}</span>
</label>
))}
</section>
{busy && <div className="imp-note">{t("mesh.importDialog.reading")}</div>}
{error && <div className="imp-warn">{error}</div>}
{r && dims && !busy && (
<section className="imp-section" data-testid="mesh-report">
<div className={r.upAuto && !r.upCertain ? "imp-warn" : "imp-note"}>
{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]),
})}
</div>
{r.truncated && <div className="imp-warn">{t("mesh.import.truncated")}</div>}
{r.warnings.map((w) => (
<div key={w} className="imp-note">
{w}
</div>
))}
</section>
)}
</div>
<footer className="imp-foot">
<button className="imp-btn" onClick={() => closeMeshImport()}>
{t("export.cancel")}
</button>
<button className="imp-btn primary" onClick={onDo} disabled={!result || busy}>
{t("mesh.importDialog.import")}
</button>
</footer>
</div>
</div>
);
}
/** 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 (
<>
<input
ref={registerMeshFileInput}
type="file"
multiple
accept={[...MESH_EXTENSIONS, ...MESH_SIDECAR_EXTENSIONS].map((e) => "." + 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 && <MeshImportDialog key={req.file.name + req.file.size + req.file.lastModified} req={req} {...props} />}
</>
);
});