[K4] STEP-Import im Kern: truck liefert flache Puffer, der Kern macht die Kontext-Meshes (ein Undo-Schritt), io/stepImport.ts gelöscht
This commit is contained in:
12 files changed
+242
-38
No files matched your search
+7
-3
@@ -54,7 +54,7 @@ import { UnsavedChangesDialog } from "./ui/UnsavedChangesDialog";
|
||||
import { RecoverAutosaveDialog } from "./ui/RecoverAutosaveDialog";
|
||||
import { Toast } from "./ui/Toast";
|
||||
import { isTauriRuntime, PROJECT_TEMPLATE_EXT } from "./native/projectFile";
|
||||
import { stepTextToImportedMeshes } from "./io/stepImport";
|
||||
import { readStepFlat } from "./engine/truckSolid";
|
||||
import { formatIfcElementsSummary, formatIfcReport, readIfcFlat } from "./io/ifcImport";
|
||||
import { IfcImportDialog, type IfcImportMode } from "./ui/IfcImportDialog";
|
||||
import { MeshImportHost } from "./ui/MeshImportDialog";
|
||||
@@ -3288,8 +3288,12 @@ export default function App() {
|
||||
if (!f) return;
|
||||
void f
|
||||
.text()
|
||||
.then((text) => stepTextToImportedMeshes(text, f.name))
|
||||
.then((objs) => addContextObjectsAndFit(objs))
|
||||
.then((text) => readStepFlat(text))
|
||||
.then((flat) => {
|
||||
// Der Kern macht je Körper ein Kontext-Mesh und übernimmt sie als EINEN Undo-Schritt ([K4] S4).
|
||||
const r = getState().commitImport((doc, base, now) => doc.importStep(base, flat, { fileName: f.name }, now));
|
||||
if (r) fitToContextObjects(addedItems(r.ops, "context") as ContextObject[]);
|
||||
})
|
||||
.catch((err) =>
|
||||
notify(`${t("step.import.failed")}: ${err instanceof Error ? err.message : String(err)}`, "error"),
|
||||
);
|
||||
|
||||
@@ -388,6 +388,11 @@ export class ProjectDocument {
|
||||
);
|
||||
}
|
||||
|
||||
/** STEP-Import (truck → Kern, [K4] S4): ein Kontext-Mesh je Körper, ein Undo-Schritt. */
|
||||
importStep(base: Project, flat: Float64Array, opts: { fileName: string }, now: number) {
|
||||
return this.commitCore<{ contextObjects: number }>(base, (h) => core().doc_import_step(h, flat, 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");
|
||||
|
||||
@@ -15,6 +15,19 @@ function combineSolids(project: typeof sampleProject, a: ExtrudedSolid, b: Extru
|
||||
import { extrudedSolidSpec } from "../plan/extrudedSolidSpec";
|
||||
import type { ExtrudedSolid } from "../model/types";
|
||||
|
||||
/** Flacher STEP-Puffer von truck (`[1, n, (np, ni, Positionen…, Indizes…)…]`) → Körper. */
|
||||
function stepBodies(f: Float64Array): Array<{ positions: number[]; indices: number[] }> {
|
||||
const out: Array<{ positions: number[]; indices: number[] }> = [];
|
||||
let at = 2;
|
||||
for (let k = 0; k < f[1]; k++) {
|
||||
const [np, ni] = [f[at], f[at + 1]];
|
||||
at += 2;
|
||||
out.push({ positions: Array.from(f.subarray(at, at + np)), indices: Array.from(f.subarray(at + np, at + np + ni)) });
|
||||
at += np + ni;
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
let mod: any;
|
||||
beforeAll(async () => {
|
||||
mod = await import("./pkgTruck/trucksolid.js");
|
||||
@@ -78,7 +91,7 @@ describe("trucksolid-WASM", () => {
|
||||
const wall = { kind: "extrudePlane", points: [0, 0, 5, 0, 5, 2.6, 0, 2.6], holes: [[1, 0.9, 2.2, 0.9, 2.2, 2.1, 1, 2.1]], origin: [0, 0, 0], uAxis: [1, 0, 0], vAxis: [0, 0, 1], depth: -0.3 };
|
||||
const cyl = { kind: "cylinder", cx: 10, cy: 0, z0: 0, r: 1, height: 2 };
|
||||
const step: string = mod.export_step(JSON.stringify({ solids: [wall, cyl], fileName: "t.stp" }));
|
||||
const back = JSON.parse(mod.import_step(step, 0.0005)) as Array<{ positions: number[]; indices: number[] }>;
|
||||
const back = stepBodies(mod.import_step_flat(step, 0.0005));
|
||||
expect(back).toHaveLength(2);
|
||||
const vols = back.map(volume).sort((a, b) => a - b);
|
||||
expect(vols[0]).toBeCloseTo((5 * 2.6 - 1.2 * 1.2) * 0.3, 1); // Wand
|
||||
|
||||
@@ -163,7 +163,10 @@ export async function exportStepSolids(
|
||||
return guarded((m) => m.export_step(JSON.stringify({ solids, fileName, organizationSystem: "dossier" })));
|
||||
}
|
||||
|
||||
/** Liest STEP-Text und liefert je Körper ein Dreiecks-Mesh (`relTol` relativ zur Diagonale). */
|
||||
export async function importStepMeshes(text: string, relTol = 0.0005): Promise<ExtrudedMesh[]> {
|
||||
return JSON.parse(await guarded((m) => m.import_step(text, relTol))) as ExtrudedMesh[];
|
||||
/**
|
||||
* Liest STEP-Text und liefert die tessellierten Körper als flachen Puffer für den Kern
|
||||
* (`ProjectDocument.importStep`; Aufbau `trucksolid::brep::step_meshes_flat`). `relTol` relativ zur Diagonale.
|
||||
*/
|
||||
export async function readStepFlat(text: string, relTol = 0.0005): Promise<Float64Array> {
|
||||
return guarded((m) => m.import_step_flat(text, relTol));
|
||||
}
|
||||
@@ -3,6 +3,19 @@ import { readFileSync } from "node:fs";
|
||||
import { sampleProject } from "../model/sampleProject";
|
||||
import { projectSolidSpecs, wallProfile } from "./exportStep";
|
||||
|
||||
/** Flacher STEP-Puffer von truck (`[1, n, (np, ni, Positionen…, Indizes…)…]`) → Körper. */
|
||||
function stepBodies(f: Float64Array): Array<{ positions: number[]; indices: number[] }> {
|
||||
const out: Array<{ positions: number[]; indices: number[] }> = [];
|
||||
let at = 2;
|
||||
for (let k = 0; k < f[1]; k++) {
|
||||
const [np, ni] = [f[at], f[at + 1]];
|
||||
at += 2;
|
||||
out.push({ positions: Array.from(f.subarray(at, at + np)), indices: Array.from(f.subarray(at + np, at + np + ni)) });
|
||||
at += np + ni;
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
describe("wallProfile", () => {
|
||||
it("Fenster im Inneren wird zum Loch, Tür am Wandfuss zur Kerbe im Aussenring", () => {
|
||||
const p = wallProfile(5, 2.6, [
|
||||
@@ -46,7 +59,7 @@ describe("STEP-Export Ende-zu-Ende (truck-WASM)", () => {
|
||||
const specs = projectSolidSpecs(sampleProject);
|
||||
const step: string = mod.export_step(JSON.stringify({ solids: specs, fileName: "demo.stp" }));
|
||||
expect(step.startsWith("ISO-10303-21;")).toBe(true);
|
||||
const back = JSON.parse(mod.import_step(step, 0.002)) as Array<{ positions: number[]; indices: number[] }>;
|
||||
const back = stepBodies(mod.import_step_flat(step, 0.002));
|
||||
expect(back.length).toBeGreaterThan(5);
|
||||
expect(back.every((m) => m.indices.length >= 12)).toBe(true);
|
||||
}, 60_000);
|
||||
|
||||
@@ -0,0 +1,50 @@
|
||||
// STEP-Import Ende-zu-Ende: truck liest (flacher Puffer) → der Kern macht Kontext-Meshes, ein Undo-Schritt.
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { readFileSync } from "node:fs";
|
||||
import { sampleProject } from "../model/sampleProject";
|
||||
import { documentFor } from "../core/document";
|
||||
import type { ImportedMesh, Project } from "../model/types";
|
||||
|
||||
const STEP_BOX = (x0: number, y0: number, z0: number, x1: number, y1: number, z1: number) => ({
|
||||
kind: "cuboid",
|
||||
min: [x0, y0, z0],
|
||||
max: [x1, y1, z1],
|
||||
});
|
||||
|
||||
const stripCode10 = (ls: Project["layers"]): Project["layers"] =>
|
||||
ls.filter((l) => l.code !== "10").map((l) => (l.children ? { ...l, children: stripCode10(l.children) } : l));
|
||||
|
||||
describe("STEP-Import im Kern", () => {
|
||||
it("zwei Körper → zwei Kontext-Meshes, Kategorie ergänzt, ein Undo-Schritt", async () => {
|
||||
const mod: any = await import("../engine/pkgTruck/trucksolid.js");
|
||||
await mod.default({ module_or_path: readFileSync("src/engine/pkgTruck/trucksolid_bg.wasm") });
|
||||
const step: string = mod.export_step(
|
||||
JSON.stringify({ solids: [STEP_BOX(0, 0, 0, 2, 1, 3), STEP_BOX(5, 0, 0, 6, 1, 1)], fileName: "k.stp" }),
|
||||
);
|
||||
const flat: Float64Array = mod.import_step_flat(step, 0.0005);
|
||||
|
||||
const base = { ...sampleProject, context: undefined, layers: stripCode10(sampleProject.layers) };
|
||||
const doc = documentFor(base);
|
||||
const r = doc.importStep(base, flat, { fileName: "Haus.stp" }, 1.7e12);
|
||||
expect("problems" in r).toBe(false);
|
||||
if ("problems" in r) return;
|
||||
expect(r.contextObjects).toBe(2);
|
||||
const ctx = (r.project.context ?? []) as ImportedMesh[];
|
||||
expect(ctx.map((m) => m.name)).toEqual(["Haus (1/2)", "Haus (2/2)"]);
|
||||
expect(ctx.every((m) => m.categoryCode === "10" && m.id.startsWith("imported-step-"))).toBe(true);
|
||||
expect(ctx[0].indices.length).toBe(36);
|
||||
expect(Math.max(...ctx[0].positions.filter((_, i) => i % 3 === 0))).toBeCloseTo(2, 5);
|
||||
expect(JSON.stringify(r.project.layers)).toContain('"code":"10"');
|
||||
expect(JSON.stringify(base.layers)).not.toContain('"code":"10"');
|
||||
// ein Undo-Schritt stellt das Projekt wieder her
|
||||
expect(doc.historyState().undo).toBe(1);
|
||||
const back = doc.undo(r.project);
|
||||
expect(back?.context ?? []).toEqual([]);
|
||||
expect(back?.layers).toEqual(base.layers);
|
||||
}, 120_000);
|
||||
|
||||
it("kaputter Puffer wird abgewiesen", () => {
|
||||
const doc = documentFor(sampleProject);
|
||||
expect(() => doc.importStep(sampleProject, new Float64Array([1, 3]), { fileName: "x.stp" }, 1)).toThrow(/Puffer/);
|
||||
});
|
||||
});
|
||||
@@ -1,24 +0,0 @@
|
||||
// STEP-Import (ISO 10303-42 B-Rep) über truck-stepio: jede Shell der Datei wird zu einem
|
||||
// tessellierten `ImportedMesh` des Kontexts (Kategorie „Import"), anwählbar wie jeder
|
||||
// andere importierte Körper. Koordinaten bleiben unverändert (Meter, Z = Höhe).
|
||||
|
||||
import { importStepMeshes } from "../engine/truckSolid";
|
||||
import { GENERIC_IMPORT_CATEGORY } from "../model/contextCategories";
|
||||
import type { ImportedMesh } from "../model/types";
|
||||
|
||||
let counter = 0;
|
||||
|
||||
/** Liest STEP-Text → ein `ImportedMesh` je Körper. Wirft bei unlesbarer Datei. */
|
||||
export async function stepTextToImportedMeshes(text: string, fileName: string): Promise<ImportedMesh[]> {
|
||||
const base = fileName.replace(/\.[^.]+$/, "") || "STEP";
|
||||
const meshes = await importStepMeshes(text);
|
||||
const stamp = Date.now().toString(36);
|
||||
return meshes.map((m, i) => ({
|
||||
id: `imported-step-${stamp}-${counter++}`,
|
||||
type: "importedMesh" as const,
|
||||
name: meshes.length > 1 ? `${base} (${i + 1}/${meshes.length})` : base,
|
||||
categoryCode: GENERIC_IMPORT_CATEGORY.code,
|
||||
positions: m.positions,
|
||||
indices: m.indices,
|
||||
}));
|
||||
}
|
||||
Reference in new issue
Block a user