[K4] S4 Import: Umwandlung DXF/DWG/PDF, IFC und Meshes im Kern, Paritätstests neben der TS-Fassung

Kern-Module import_drawing/import_ifc/import_context, Bindungen web/import.rs (doc_import_drawing,
doc_import_ifc, doc_import_mesh, Zusammenfassung, Leseansicht, pdf_page_info), ProjectDocument.importDrawing/
importIfc/importMesh. Vorübergehend: io/importParity.test.ts, io/importBench.test.ts.
This commit is contained in:
Claude committed 2026-10-29 14:00:13 +00:00
1 parent 23993e9607
commit 65e7f3a2ea
13 files changed
+3193

No files matched your search

+77
View File
@@ -13,6 +13,41 @@ import { fieldOps } from "./opsBuild";
import { opsMutation } from "./opsBuild";
import type { ProjectMutation } from "./opsBuild";
/**
* Gelesene Zeichnungs-Datei für den Kern ([K4] S4): Puffer des Format-Moduls, je Stück (paralleles Lesen
* grosser DXF) hintereinander als `[n, len_0 … len_{n−1}, daten …]`, die String-Tabellen je Stück, und wie
* der Puffer zu lesen ist (DXF/DWG bzw. PDF-Seite mit Massstab).
*/
export interface ImportSource {
geom: Float64Array;
strings: string[][];
source: { kind: "drawing" } | { kind: "pdf"; scale: number; texts?: boolean };
}
/** Entscheidung des Import-Dialogs (bzw. CLI) für {@link ProjectDocument.importDrawing}; Texte schon übersetzt. */
export interface DrawingImportDecision {
target: { kind: "existing"; levelId: string } | { kind: "new"; name: string };
categoryMode: "byLayer" | "active";
activeCategoryCode: string;
importMeshes: boolean;
/** Mitte des Ausschnitts (Platzierung „viewCenter"); fehlt = an den Datei-Koordinaten. */
viewCenter?: [number, number];
/** Name einer neuen Zeichnung ohne Eingabe, `{n}` = Nummer. */
levelNameTemplate?: string;
/** Elternkategorie der neuen Layer-Kategorien. */
parentName?: string;
/** Name neuer Linienstile aus PDF-Strichmustern, `{pattern}` = Muster. */
dashStyleTemplate?: string;
}
/** Rohergebnis des IFC-Lesers (`fileio`): Bericht (JSON), Positionen, Indizes, Zahlen der Bauteil-Erkennung. */
export interface IfcFlatInput {
meta: string;
positions: Float64Array;
indices: Uint32Array;
elementData?: Float64Array;
}
/** Ergebnis des Ladens einer Projektdatei ({@link ProjectDocument.loadFile}). */
export type LoadFileResult =
| { ok: true; project: Project; warnings: string[]; migratedFrom: number | null }
@@ -308,6 +343,48 @@ export class ProjectDocument {
return { project, ops: r.ops };
}
/**
* Kern-Aufruf, der Ops rechnet und gleich als Benutzer-Änderung anwendet (Importe, [K4] S4): `call`
* bekommt das Handle und liefert `{ops, …}`, `{problems}` oder einen Fehlertext mit `\u0001` vorn (wirft).
* Die Leseansicht wird mit den angewendeten Ops nachgeführt; die übrigen Felder kommen unverändert zurück.
*/
private commitCore<R extends object>(base: Project, call: (h: number) => string): ({ project: Project; ops: CoreOp[] } & R) | { problems: string[] } {
this.ensure(base);
if (!this.handle) return { problems: ["Projekt-Dokument im Kern nicht offen."] };
const out = call(this.handle);
if (out.startsWith("\u0001")) throw new Error(out.slice(1));
const r = fromMarkedJson<({ ops: CoreOp[] } & R) | { problems: string[] }>(out);
if ("problems" in r) return r;
const project = r.ops.length ? applyOps(base, r.ops) : base;
this.mirror = project;
return { ...r, project };
}
/**
* Zeichnungs-Import (DXF/DWG/PDF, [K4] S4): der Kern wandelt die gelesene Datei (`src`, Puffer des
* Format-Moduls) nach der Entscheidung des Dialogs in Ops (Zeichnungsebene, Kategorien je Layer,
* Linienstile, 2D-Zeichnungen, Kontext-Meshes) und wendet sie als EINEN Undo-Schritt an.
*/
importDrawing(base: Project, src: ImportSource, decision: DrawingImportDecision, now: number) {
return this.commitCore<{ levelId: string; drawings: number; meshes: number }>(base, (h) =>
core().doc_import_drawing(h, src.geom, JSON.stringify(src.strings), JSON.stringify(src.source), JSON.stringify(decision), "", now),
);
}
/** IFC-Import ([B3]/[B4], [K4] S4): Kontext-Meshes bzw. (`opts.elements`) Bauteile, ein Undo-Schritt. */
importIfc(base: Project, flat: IfcFlatInput, opts: { fileName: string; group?: string; elements?: boolean }, now: number) {
return this.commitCore<{ report: Record<string, unknown>; summary: Record<string, number> | null; kept: Record<string, number>; contextObjects: number }>(base, (h) =>
core().doc_import_ifc(h, flat.meta, flat.positions, flat.indices, flat.elementData ?? new Float64Array(0), JSON.stringify(opts), "", now),
);
}
/** Mesh-Import (OBJ/STL/glTF/PLY, [C11], [K4] S4): Kontext-Meshes, ein Undo-Schritt. */
importMesh(base: Project, flat: { flat: Float64Array; strings: string[] }, opts: { fileName: string; placement?: string; group?: string }, now: number) {
return this.commitCore<{ report: Record<string, unknown>; contextObjects: number }>(base, (h) =>
core().doc_import_mesh(h, flat.flat, JSON.stringify(flat.strings), JSON.stringify(opts), "", now),
);
}
/** Abfrage eines Befehls zwischen den Schritten (`doc_cmd_query`: Treffer, Vorschau-Formen), JSON-Antwort. */
cmdQuery<T>(req: object): T {
if (!this.handle) throw new Error("Kern-Abfrage „cmdQuery“: Dokument nicht offen");
+88
View File
@@ -0,0 +1,88 @@
// VORÜBERGEHEND ([K4] S4): Zeitmessung Kataster-Import alt (TS-Umwandlung + Commit) ↔ neu (Kern).
import "../engine/fileioWasm.setup";
import { describe, it } from "vitest";
import { existsSync, readFileSync, writeFileSync } from "node:fs";
import { join } from "node:path";
import { applyOps } from "../core/ops";
import type { CoreOp } from "../core/ops";
import { addItems, fieldOps } from "../core/opsBuild";
import { ProjectDocument } from "../core/document";
import { sampleProject } from "../model/sampleProject";
import { flattenCategories } from "../model/types";
import { decodeImport, importDrawingFlat } from "./dxfParser";
import { countContours, distinctImportLayers, distinctLayers, planImportCategories } from "./dxfToDrawings";
import { buildDrawings } from "../ui/ImportDialog";
import { planDashLineStyles } from "./pdfLineStyles";
import { packChunks, importSummary } from "./importSource";
import { core } from "../engine/coreWasm";
const FX = process.env.DOSSIER_PARITY_FX ?? "";
const file = join(FX, "kataster150k.dxf");
describe.skipIf(!existsSync(file))("Zeitmessung Kataster 150k", { timeout: 600_000 }, () => {
it("alt ↔ neu", () => {
const bytes = new Uint8Array(readFileSync(file));
const runs = Number(process.env.BENCH_RUNS ?? 3);
for (let run = 0; run < runs; run++) {
let t0 = performance.now();
const flat = importDrawingFlat(bytes);
const tParse = performance.now() - t0;
if (process.env.BENCH_DUMP) {
writeFileSync(join(FX, "k150.geom"), new Uint8Array(flat.geom.buffer, flat.geom.byteOffset, flat.geom.byteLength));
writeFileSync(join(FX, "k150.strings.json"), JSON.stringify(flat.strings));
}
// alt
t0 = performance.now();
const parsed = decodeImport(flat.geom, flat.strings);
void [countContours(parsed.contours), distinctLayers(parsed.contours)];
const tOldDecode = performance.now() - t0;
t0 = performance.now();
const base = sampleProject;
const levelId = "drawing-1";
const ops: CoreOp[] = addItems("drawingLevels", [{ id: levelId, name: "K", kind: "drawing", visible: true, locked: false }]);
let p = applyOps(base, ops);
const plan = planImportCategories(distinctImportLayers(parsed.contours, parsed.texts ?? []), flattenCategories(base.layers), "Import", parsed.layerColors, parsed.layerLineWeights);
const lops = fieldOps(p, "layers", [...p.layers, plan.parent!]);
p = applyOps(p, lops);
const dash = planDashLineStyles(parsed.layerDashes, base.lineStyles);
const drawings = buildDrawings({ target: { kind: "existing", levelId }, categoryMode: "byLayer", importMeshes: false, placement: "origin", parsed }, levelId, base, "20", plan.codeByLayer, dash.styleByLayer);
const all = [...ops, ...lops, ...addItems("drawings2d", drawings)];
const tOldConvert = performance.now() - t0;
const docOld = new ProjectDocument(base);
t0 = performance.now();
const rOld = docOld.commit(base, all, undefined, Date.now());
const tOldCommit = performance.now() - t0;
if ("problems" in rOld) throw new Error(rOld.problems[0]);
docOld.close();
// neu
t0 = performance.now();
const src = packChunks([flat]);
const summary = importSummary(src);
const tNewSummary = performance.now() - t0;
const docNew = new ProjectDocument(base);
t0 = performance.now();
const r = docNew.importDrawing(base, src, { target: { kind: "new", name: "K" }, categoryMode: "byLayer", activeCategoryCode: "20", importMeshes: false, parentName: "Import" }, Date.now());
const tNewImport = performance.now() - t0;
if ("problems" in r) throw new Error(r.problems[0]);
// Aufteilung: Kern-Aufruf, JSON.parse, applyOps.
const docX = new ProjectDocument(base);
void docX.json();
t0 = performance.now();
const out = core().doc_import_drawing(docX.id, src.geom, JSON.stringify(src.strings), JSON.stringify(src.source), JSON.stringify({ target: { kind: "new", name: "K" }, categoryMode: "byLayer", activeCategoryCode: "20", importMeshes: false, parentName: "Import" }), "", Date.now());
const tCore = performance.now() - t0;
t0 = performance.now();
const parsedOut = JSON.parse(out) as { ops: CoreOp[] };
const tParseOut = performance.now() - t0;
t0 = performance.now();
applyOps(base, parsedOut.ops);
const tApply = performance.now() - t0;
console.log(` Kern ${tCore.toFixed(0)} ms (${(out.length / 1e6).toFixed(1)} MB JSON), JSON.parse ${tParseOut.toFixed(0)} ms, applyOps ${tApply.toFixed(0)} ms`);
docX.close();
docNew.close();
const f = (x: number) => x.toFixed(0).padStart(5);
console.log(
`Lauf ${run}: ${summary.contours + summary.texts} Elemente | Lesen ${f(tParse)} ms | alt: Dekodieren ${f(tOldDecode)} + Umwandeln ${f(tOldConvert)} + Commit ${f(tOldCommit)} = ${f(tOldDecode + tOldConvert + tOldCommit)} ms | neu: Zusammenfassung ${f(tNewSummary)} + Import ${f(tNewImport)} = ${f(tNewSummary + tNewImport)} ms`,
);
}
});
});
+379
View File
@@ -0,0 +1,379 @@
// VORÜBERGEHEND ([K4] S4): Gleichheit alte TS-Umwandlung ↔ Kern-Import (`ProjectDocument.importDrawing/
// importIfc/importMesh`). Gleiches Projekt (bis auf erzeugte ids), gleiche Zusammenfassung/Berichte. Wird mit
// der TS-Fassung gelöscht. Fixtures: `DOSSIER_PARITY_FX` (Verzeichnis), sonst übersprungen.
import "../engine/fileioWasm.setup";
import { describe, expect, it } from "vitest";
import { existsSync, readFileSync, readdirSync } from "node:fs";
import { join } from "node:path";
import { applyOps } from "../core/ops";
import type { CoreOp } from "../core/ops";
import { addItems, fieldOps } from "../core/opsBuild";
import { ProjectDocument } from "../core/document";
import type { DrawingImportDecision } from "../core/document";
import { emptyProject, sampleProject } from "../model/sampleProject";
import type { ContextObject, ContourSet, Drawing2D, ImportedMesh, ImportedText, Project, Vec2 } from "../model/types";
import { flattenCategories } from "../model/types";
import { ensureContextCategories } from "../model/contextCategories";
import { projectGeoOrigin } from "../model/geoRebase";
import { decodeImport, importDrawingFlat, parseDxf, type DxfImportResult } from "./dxfParser";
import { countContours, distinctImportLayers, distinctLayers, planImportCategories } from "./dxfToDrawings";
import { buildDrawings } from "../ui/ImportDialog";
import { planDashLineStyles } from "./pdfLineStyles";
import { pdfPageInfo, pdfPageToImport, readPdfVectorPage } from "./pdfVector";
import { packChunks, importSummary } from "./importSource";
import { fileioLoaded } from "../engine/fileioWasm";
import { t } from "../i18n";
import { ifcElementsToProject, readIfcElements } from "./ifcElements";
import { ifcToImportedMeshes, importIfcFlat, readIfc } from "./ifcImport";
import { decodeMesh, importMeshFlat, meshObjectsToImportedMeshes, type MeshUnit, type MeshUp } from "./meshImport";
import { core } from "../engine/coreWasm";
const FX = process.env.DOSSIER_PARITY_FX ?? "";
const have = FX !== "" && existsSync(FX);
const fx = (n: string) => new Uint8Array(readFileSync(join(FX, n)));
/** Erzeugte ids (Zeit + Zähler) durch Platzhalter in Reihenfolge des ersten Auftretens ersetzen. */
function normalize(v: unknown): string {
const text = JSON.stringify(v);
const map = new Map<string, string>();
return text.replace(
/"((?:c\d+-)?imported-\d+-\d+|dxf2d-\d+-\d+|drawing-\d+|ifc-[a-z]+-[0-9a-z]+-\d+|imported-ifc-[0-9a-z]+-\d+|imported-mesh-[0-9a-z]+-\d+)"/g,
(_, id: string) => {
if (!map.has(id)) map.set(id, `#${map.size}`);
return `"${map.get(id)}"`;
},
);
}
/** Schlüssel sortiert (Leseansicht ↔ Dokument: Felder mit `undefined` behalten in TS ihre Stelle). */
function canon(v: unknown): unknown {
if (Array.isArray(v)) return v.map(canon);
if (v && typeof v === "object") return Object.fromEntries(Object.keys(v).sort().filter((k) => (v as Record<string, unknown>)[k] !== undefined).map((k) => [k, canon((v as Record<string, unknown>)[k])]));
return v;
}
// ── Alte App-Fassung (`useProjectFileActions.runImportSteps`, Kopie der privaten Helfer) ─────────
function drawingsBBox(drawings: Drawing2D[]) {
let minX = Infinity, minY = Infinity, maxX = -Infinity, maxY = -Infinity;
const acc = (x: number, y: number) => {
minX = Math.min(minX, x); minY = Math.min(minY, y);
maxX = Math.max(maxX, x); maxY = Math.max(maxY, y);
};
for (const d of drawings) {
const g = d.geom;
if (g.shape === "line") { acc(g.a.x, g.a.y); acc(g.b.x, g.b.y); }
else if (g.shape === "polyline") for (const p of g.pts) acc(p.x, p.y);
else if (g.shape === "circle" || g.shape === "arc") { acc(g.center.x - g.r, g.center.y - g.r); acc(g.center.x + g.r, g.center.y + g.r); }
else if (g.shape === "text") acc(g.at.x, g.at.y);
}
return Number.isFinite(minX) ? { minX, minY, maxX, maxY } : null;
}
function translateDrawing(d: Drawing2D, dx: number, dy: number): Drawing2D {
const mv = (p: Vec2): Vec2 => ({ x: p.x + dx, y: p.y + dy });
const g = d.geom;
switch (g.shape) {
case "line": return { ...d, geom: { ...g, a: mv(g.a), b: mv(g.b) } };
case "polyline": return { ...d, geom: { ...g, pts: g.pts.map(mv) } };
case "circle": return { ...d, geom: { ...g, center: mv(g.center) } };
case "arc": return { ...d, geom: { ...g, center: mv(g.center) } };
case "text": return { ...d, geom: { ...g, at: mv(g.at) } };
default: return d;
}
}
interface Case {
target: DrawingImportDecision["target"];
categoryMode: "byLayer" | "active";
importMeshes: boolean;
viewCenter?: [number, number];
}
function oldApp(project: Project, parsed: DxfImportResult, c: Case, active: string, parentName: string): Project {
let p = project;
const apply = (ops: CoreOp[]) => {
p = applyOps(p, ops);
};
let levelId: string;
if (c.target.kind === "new") {
const id = `drawing-${Date.now()}`;
const n = p.drawingLevels.filter((z) => z.kind === "drawing").length + 1;
apply(addItems("drawingLevels", [{ id, name: c.target.name.trim() || t("default.drawingName", { n }), kind: "drawing", visible: true, locked: false }]));
levelId = id;
} else levelId = c.target.levelId;
let codeByLayer: Map<string, string> | undefined;
if (c.categoryMode === "byLayer") {
const plan = planImportCategories(distinctImportLayers(parsed.contours, parsed.texts ?? []), flattenCategories(project.layers), parentName, parsed.layerColors, parsed.layerLineWeights);
codeByLayer = plan.codeByLayer;
if (plan.parent) apply(fieldOps(p, "layers", [...p.layers, plan.parent]));
}
const dashPlan = planDashLineStyles(parsed.layerDashes, project.lineStyles);
if (dashPlan.created.length) apply(addItems("lineStyles", dashPlan.created.filter((x) => !p.lineStyles.some((l) => l.id === x.id))));
let drawings = buildDrawings(
{ target: c.target, categoryMode: c.categoryMode, importMeshes: c.importMeshes, placement: c.viewCenter ? "viewCenter" : "origin", parsed },
levelId,
project,
active,
codeByLayer,
dashPlan.styleByLayer,
);
if (c.viewCenter && drawings.length) {
const bb = drawingsBBox(drawings);
if (bb) {
const dx = c.viewCenter[0] - (bb.minX + bb.maxX) / 2;
const dy = c.viewCenter[1] - (bb.minY + bb.maxY) / 2;
drawings = drawings.map((d) => translateDrawing(d, dx, dy));
}
}
if (drawings.length) apply(addItems("drawings2d", drawings));
if (c.importMeshes && parsed.meshes.length) {
const added = parsed.meshes as ContextObject[];
const codes = added.map((o) => o.categoryCode).filter((x): x is string => !!x);
apply([...addItems("context", added), ...fieldOps(p, "layers", ensureContextCategories(p.layers, codes))]);
}
return p;
}
function oldSummary(parsed: DxfImportResult) {
const texts = parsed.texts ?? [];
return {
contours: countContours(parsed.contours),
texts: texts.length,
meshes: parsed.meshes.length,
layers: distinctLayers(parsed.contours),
diagnostics: parsed.diagnostics,
...(parsed.insUnits !== undefined ? { insUnits: parsed.insUnits } : {}),
...(parsed.unitScale !== undefined ? { unitScale: parsed.unitScale } : {}),
...(parsed.layouts !== undefined ? { layouts: parsed.layouts } : {}),
...(parsed.layout !== undefined ? { layout: parsed.layout } : {}),
recovered: parsed.recovered ?? false,
};
}
const LEVEL_TEMPLATE = t("default.drawingName", { n: "{n}" });
const DASH_TEMPLATE = t("import.pdf.dashStyleName", { pattern: "{pattern}" });
function compareDrawing(project: Project, parsed: DxfImportResult, src: ReturnType<typeof packChunks>, c: Case, label: string) {
const active = project.layers[0].code;
const parentName = `Import ${label}`;
const old = oldApp(project, parsed, c, active, parentName);
const doc = new ProjectDocument(project);
const r = doc.importDrawing(project, src, { ...c, activeCategoryCode: active, levelNameTemplate: LEVEL_TEMPLATE, parentName, dashStyleTemplate: DASH_TEMPLATE }, Date.now());
if ("problems" in r) throw new Error(`${label}: ${r.problems.join("; ")}`);
expect(normalize(r.project), label).toBe(normalize(old));
// Kern-Dokument = Leseansicht.
expect(normalize(canon(JSON.parse(doc.json()))), `${label} (Dokument)`).toBe(normalize(canon(r.project)));
doc.close();
}
const CASES: Case[] = [
{ target: { kind: "new", name: "" }, categoryMode: "byLayer", importMeshes: true },
{ target: { kind: "new", name: " Plan " }, categoryMode: "active", importMeshes: false, viewCenter: [12.5, -3.25] },
{ target: { kind: "existing", levelId: "eg" }, categoryMode: "byLayer", importMeshes: false, viewCenter: [0, 0] },
];
const BASES: [string, Project][] = [
["leer", emptyProject],
["beispiel", sampleProject],
];
function dxfFiles(): string[] {
return readdirSync(FX).filter((n) => /\.(dxf|dwg)$/i.test(n) && !n.startsWith("kataster150k"));
}
describe.skipIf(!have)("Parität Zeichnungs-Import (DXF/DWG/PDF)", { timeout: 600_000 }, () => {
it("DXF/DWG: Projekt und Zusammenfassung gleich", () => {
let n = 0;
for (const name of dxfFiles()) {
const bytes = fx(name);
const flat = importDrawingFlat(bytes);
const parsed = decodeImport(flat.geom, flat.strings);
const src = packChunks([flat]);
expect(importSummary(src), name).toEqual(JSON.parse(JSON.stringify(oldSummary(parsed))));
for (const [bn, base] of BASES) {
const levelId = base.drawingLevels[0].id;
for (const c of CASES) {
const cc = c.target.kind === "existing" ? { ...c, target: { kind: "existing" as const, levelId } } : c;
compareDrawing(base, parsed, src, cc, `${name}/${bn}/${JSON.stringify(cc)}`);
n++;
}
}
}
expect(n).toBeGreaterThan(20);
});
it("Layouts (Papierbereich)", () => {
const bytes = fx("layouts.dxf");
const layouts = parseDxf(bytes).layouts ?? [];
expect(layouts.length).toBeGreaterThan(0);
for (const l of layouts) {
const flat = importDrawingFlat(bytes, l.name);
const parsed = decodeImport(flat.geom, flat.strings);
const src = packChunks([flat]);
expect(importSummary(src)).toEqual(JSON.parse(JSON.stringify(oldSummary(parsed))));
compareDrawing(sampleProject, parsed, src, CASES[0], `layout ${l.name}`);
}
});
it("parallel gelesene Stücke (grosse DXF)", () => {
for (const name of ["kataster150k.dxf"]) {
const bytes = fx(name);
const offsets = Array.from(fileioLoaded()!.dxf_split_offsets(bytes, 4));
expect(offsets.length, name).toBeGreaterThanOrEqual(3);
const closer = new TextEncoder().encode("0\nENDSEC\n0\nEOF\n");
const pre = bytes.subarray(0, offsets[0]);
const chunks: Uint8Array[] = [];
for (let i = 0; i + 1 < offsets.length; i++) {
const body = bytes.subarray(offsets[i], offsets[i + 1]);
const c = new Uint8Array(pre.length + body.length + closer.length);
c.set(pre, 0);
c.set(body, pre.length);
c.set(closer, pre.length + body.length);
chunks.push(c);
}
const flats = chunks.map((c) => importDrawingFlat(c));
const parts = flats.map((f, i) => decodeImport(f.geom, f.strings, `c${i}-`));
const merged = mergeResults(parts);
const src = packChunks(flats);
expect(importSummary(src), name).toEqual(JSON.parse(JSON.stringify(oldSummary(merged))));
compareDrawing(sampleProject, merged, src, CASES[0], `${name} Stücke`);
compareDrawing(emptyProject, merged, src, CASES[1], `${name} Stücke aktiv`);
}
});
it("Vektor-PDF: Seiten, Massstäbe, Texte, Strichmuster", () => {
for (const name of ["plan.pdf", "plan-rot.pdf"]) {
const bytes = fx(name);
const res = fileioLoaded()!.pdf_read_page(bytes, 0);
const flat = { geom: res.geom(), strings: res.strings() };
res.free();
const page = readPdfVectorPage(bytes, 0);
// pdf-info
expect(JSON.parse(core().pdf_page_info(flat.geom, JSON.stringify(flat.strings)))).toEqual(JSON.parse(JSON.stringify(pdfPageInfo(page))));
for (const scale of [100, 50, 1, 333.3]) {
for (const texts of [true, false]) {
const parsed = pdfPageToImport(page, scale, { texts });
const src = packChunks([flat], { kind: "pdf", scale, texts });
expect(importSummary(src)).toEqual(JSON.parse(JSON.stringify(oldSummary(parsed))));
for (const [bn, base] of BASES) {
for (const c of CASES) {
const cc = c.target.kind === "existing" ? { ...c, target: { kind: "existing" as const, levelId: base.drawingLevels[0].id } } : c;
compareDrawing(base, parsed, src, cc, `${name}/${scale}/${texts}/${bn}`);
}
}
}
}
// Projekt mit schon passendem Linienstil: wird wiederverwendet.
const withStyle: Project = { ...sampleProject, lineStyles: [...sampleProject.lineStyles, { id: "x", name: "x", weight: 0.1, color: "#000", dash: [1.05, 0.7] }] };
compareDrawing(withStyle, pdfPageToImport(page, 100), packChunks([flat], { kind: "pdf", scale: 100 }), CASES[0], `${name} Stil`);
}
});
});
function mergeResults(results: DxfImportResult[]): DxfImportResult {
const meshes: ImportedMesh[] = [];
const contours: ContourSet[] = [];
const texts: ImportedText[] = [];
const layerColors: Record<string, string> = {};
const diagnostics = { entityCounts: {} as Record<string, number>, total: 0 };
for (const r of results) {
for (const m of r.meshes) meshes.push(m);
for (const c of r.contours) contours.push(c);
for (const tt of r.texts ?? []) texts.push(tt);
Object.assign(layerColors, r.layerColors);
for (const [k, n] of Object.entries(r.diagnostics?.entityCounts ?? {})) diagnostics.entityCounts[k] = (diagnostics.entityCounts[k] ?? 0) + n;
diagnostics.total += r.diagnostics?.total ?? 0;
}
return { meshes, contours, texts, layerColors, diagnostics, insUnits: results[0]?.insUnits, unitScale: results[0]?.unitScale, layouts: results[0]?.layouts };
}
// ── IFC ──────────────────────────────────────────────────────────────────────
function contextOps(project: Project, meshes: NonNullable<Project["context"]>): CoreOp[] {
return [
...(project.context ? addItems("context", meshes) : fieldOps(project, "context", meshes)),
...fieldOps(project, "layers", ensureContextCategories(project.layers, meshes.flatMap((m) => (m.categoryCode ? [m.categoryCode] : [])))),
];
}
const geoProject: Project = { ...sampleProject, geoAnchor: { lv95: { e: 2600000, n: 1200000 }, model: { x: 0, y: 0 } } } as Project;
const IFC_BASES: [string, Project][] = [["leer", emptyProject], ["beispiel", sampleProject], ["geo", geoProject], ["ohneListen", { ...emptyProject, context: undefined, rooms: undefined, columns: undefined, ceilingTypes: undefined } as unknown as Project]];
describe.skipIf(!have)("Parität IFC-Import", { timeout: 600_000 }, () => {
it("Bauteile und Kontext-Meshes: Projekt, Zusammenfassung, Bericht gleich", async () => {
const files = readdirSync(FX).filter((n) => n.endsWith(".ifc"));
expect(files.length).toBeGreaterThanOrEqual(5);
for (const name of files) {
const bytes = fx(name);
const flat = await importIfcFlat(bytes, true);
const { products, report, elements } = await readIfcElements(bytes);
for (const [bn, base] of IFC_BASES) {
for (const group of ["auto", "product", "class"] as const) {
const label = `${name}/${bn}/${group}`;
const old = ifcElementsToProject(base, products, elements, { fileName: name, geoOrigin: projectGeoOrigin(base), group });
const doc = new ProjectDocument(base);
const r = doc.importIfc(base, flat, { fileName: name, group, elements: true }, Date.now());
if ("problems" in r) throw new Error(`${label}: ${r.problems.join("; ")}`);
expect(normalize(r.project), label).toBe(normalize(old.project));
expect(r.summary, label).toEqual(old.summary);
expect(r.kept, label).toEqual(elements.kept);
// CLI `import-ifc --elements`: Bericht wie früher `{ ...report, products: undefined, elements: summary, kept }`.
expect(JSON.stringify({ ...r.report, products: undefined, byClass: undefined, triangles: undefined, kept: r.kept })).toBe(
JSON.stringify({ ...report, products: undefined, elements: old.summary, kept: elements.kept }),
);
expect(normalize(canon(JSON.parse(doc.json()))), `${label} (Dokument)`).toBe(normalize(canon(r.project)));
doc.close();
// Nur Kontext-Meshes ([B3]).
const m = await readIfc(bytes);
const meshes = ifcToImportedMeshes(m.products, name, { geoOrigin: projectGeoOrigin(base), group });
const oldMesh = applyOps(base, contextOps(base, meshes));
const flat2 = await importIfcFlat(bytes, false);
const doc2 = new ProjectDocument(base);
const r2 = doc2.importIfc(base, flat2, { fileName: name, group }, Date.now());
if ("problems" in r2) throw new Error(r2.problems.join("; "));
expect(normalize(r2.project), `${label} Meshes`).toBe(normalize(oldMesh));
const byClass: Record<string, number> = {};
for (const p of m.products) byClass[p.ifcClass] = (byClass[p.ifcClass] ?? 0) + 1;
const triangles = m.products.reduce((s, p) => s + p.indices.length / 3, 0);
expect(r2.report).toEqual(JSON.parse(JSON.stringify({ ...m.report, byClass, triangles })));
expect(r2.contextObjects).toBe(meshes.length);
doc2.close();
}
}
}
});
});
// ── Meshes ───────────────────────────────────────────────────────────────────
describe.skipIf(!have)("Parität Mesh-Import", { timeout: 600_000 }, () => {
it("OBJ/STL/glTF/GLB/PLY: Projekt und Bericht gleich", async () => {
const files = readdirSync(FX).filter((n) => /\.(obj|stl|glb|gltf|ply)$/i.test(n));
expect(files.length).toBeGreaterThanOrEqual(6);
const sidecars = (n: string) => (n === "gruppen.obj" ? [{ name: "farben.mtl", bytes: fx("farben.mtl") }] : []);
for (const name of files) {
for (const [unit, up] of [["m", "auto"], ["mm", "y"], ["cm", "z"]] as [MeshUnit, MeshUp][]) {
const flat = await importMeshFlat(fx(name), name, { unit, up, maxTriangles: 0, files: sidecars(name) });
const { objects, report } = decodeMesh(flat);
expect(JSON.parse(core().mesh_import_report(flat.flat, JSON.stringify(flat.strings)))).toEqual(JSON.parse(JSON.stringify(report)));
for (const [bn, base] of [["beispiel", sampleProject], ["geo", geoProject], ["leer", { ...emptyProject, context: undefined } as unknown as Project]] as [string, Project][]) {
for (const placement of ["center", "original"] as const) {
for (const group of ["auto", "object", "merge"] as const) {
const label = `${name}/${unit}/${bn}/${placement}/${group}`;
const meshes = meshObjectsToImportedMeshes(objects, name, { placement, geoOrigin: projectGeoOrigin(base), group });
const old = applyOps(base, contextOps(base, meshes));
const doc = new ProjectDocument(base);
const r = doc.importMesh(base, flat, { fileName: name, placement, group }, Date.now());
if ("problems" in r) throw new Error(`${label}: ${r.problems.join("; ")}`);
expect(normalize(r.project), label).toBe(normalize(old));
expect(r.contextObjects).toBe(meshes.length);
doc.close();
}
}
}
}
}
});
});
+90
View File
@@ -0,0 +1,90 @@
// Gelesene Import-Datei (DXF/DWG/PDF) als Eingabe für den Kern ([K4] S4): das Format-Modul (`pkgFileIo`,
// `src-tauri/fileio`) liest die Datei in flache Puffer, der Kern (`core/src/import_drawing.rs`) macht daraus
// die Zusammenfassung für den Dialog und beim Bestätigen die Ops (`ProjectDocument.importDrawing`). TS hält
// nur die Puffer (keine Umwandlung in Konturen/Zeichnungen mehr).
import type { ImportedMesh } from "../model/types";
import { core } from "../engine/coreWasm";
import type { ImportSource } from "../core/document";
export type { ImportSource };
/** Diagnose: gelesene Entity-Typen (Typ → Anzahl) und Gesamtzahl. */
export interface ImportDiagnostics {
entityCounts: Record<string, number>;
total: number;
}
/** Was der Import-Dialog von der gelesenen Datei zeigt (`import_drawing::summary_json`). */
export interface ImportSummary {
contours: number;
texts: number;
meshes: number;
/** Layer der Konturen (in Fundreihenfolge). */
layers: string[];
diagnostics: ImportDiagnostics;
/** `$INSUNITS` der Datei (0 = ohne Einheit, 4 = mm …). */
insUnits?: number;
/** Meter je Zeicheneinheit (schon umgerechnet; PDF: Meter je Punkt beim gewählten Massstab). */
unitScale?: number;
/** Papierbereich-Layouts der Datei (ohne „Model"); `entities` = Zeichnungs-Entities. */
layouts?: { name: string; entities: number }[];
/** Name des gelesenen Layouts; fehlt beim Modellraum. */
layout?: string;
/** Die Datei war beschädigt/abgeschnitten und wurde nur teilweise gelesen. */
recovered: boolean;
}
/** Gelesene Datei + Zusammenfassung (Zustand des Import-Dialogs). */
export interface ParsedImport {
src: ImportSource;
summary: ImportSummary;
}
/** Ein gelesenes Stück (flacher Puffer + String-Tabelle des Format-Moduls). */
export interface ImportChunk {
geom: Float64Array;
strings: string[];
}
/** Stücke zu einer Kern-Eingabe zusammenpacken: `[n, len_0 … len_{n−1}, daten …]`. */
export function packChunks(chunks: readonly ImportChunk[], source: ImportSource["source"] = { kind: "drawing" }): ImportSource {
const n = chunks.length;
const total = chunks.reduce((s, c) => s + c.geom.length, 0);
const geom = new Float64Array(1 + n + total);
geom[0] = n;
let at = 1 + n;
chunks.forEach((c, i) => {
geom[1 + i] = c.geom.length;
geom.set(c.geom, at);
at += c.geom.length;
});
return { geom, strings: chunks.map((c) => c.strings), source };
}
function call<T>(out: string): T {
if (out.startsWith("\u0001")) throw new Error(out.slice(1));
return JSON.parse(out) as T;
}
/** Zusammenfassung für den Dialog (Kern). Wirft bei kaputtem Puffer. */
export function importSummary(src: ImportSource): ImportSummary {
return call(core().import_drawing_summary(src.geom, JSON.stringify(src.strings), JSON.stringify(src.source)));
}
/** Gelesene Stücke → Dialog-Zustand. */
export function parsedImport(chunks: readonly ImportChunk[], source?: ImportSource["source"]): ParsedImport {
const src = packChunks(chunks, source);
return { src, summary: importSummary(src) };
}
/** Leerer Import (Fehlerfall im Dialog). */
export function emptyImport(): ParsedImport {
const src = packChunks([]);
return { src, summary: { contours: 0, texts: 0, meshes: 0, layers: [], diagnostics: { entityCounts: {}, total: 0 }, recovered: false } };
}
/** Nur die Meshes als Kontext-Objekte (z. B. swissBUILDINGS3D-Kacheln, je Ebene ein Mesh). */
export function importMeshes(src: ImportSource, now = Date.now()): ImportedMesh[] {
return call(core().import_drawing_meshes(src.geom, JSON.stringify(src.strings), JSON.stringify(src.source), now));
}