Files
DOSSIER/src/export/exportIfc.ts
T
Claude 2fd3997def [B5] IFC-Export: Wandachse und IfcMaterialLayerSetUsage an Extrusions-Wänden
Extrusions-Wände bekommen ein Placement am Wandanfang (X = Achse, Y = linke
Normale), eine "Axis"-Darstellung (IfcPolyline) und eine IfcMaterialLayerSetUsage
(AXIS2, POSITIVE, Offset der Aussenschicht), damit Revit/ArchiCAD die Schichten
der Wandachse zuordnen. Öffnungen werden ins Wandsystem gerechnet. Test: Achse
und Schichtbereiche per ifcopenshell, auch für schräge, rückwärts gezeichnete Wände.

Claude-Session: https://claude.ai/code/session_01NTYBZBvLYCtugMJoKXaB67
2026-10-28 12:46:09 +00:00

841 lines
36 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.
// IFC4-Export (STEP Physical File / ISO-10303-21) des semantischen Modells.
//
// Seit [B5] (28.10.) schreibt Rust die Datei (`src-tauri/fileio/src/ifc_export.rs`, Format-Modul
// pkgFileIo): TS sammelt hier aus Projekt und Plan die Bauteile samt fertig gerechneter Geometrie
// (Wand-Bänder, Öffnungen, Dach-Meshes, Raumflächen) als flachen Puffer, Rust vergibt die Nummern und
// baut Entitäten, GUIDs, Typen mit Materialschichten, Psets und Beziehungen. Verlangt das geladene
// Format-Modul (`await loadFileio()`, wie `buildPlanDxf`). Der Download passiert im App-Layer.
//
// Abbildung:
// Project → IfcProject → IfcSite → IfcBuilding → je "floor"-Geschoss ein
// IfcBuildingStorey (IfcRelAggregates-Kette). Bauteile hängen über
// IfcRelContainedInSpatialStructure am jeweiligen Geschoss (verwaiste
// Geschossreferenzen fallen defensiv auf IfcBuilding zurück).
//
// Decken/Treppen/Extrusionen als IfcExtrudedAreaSolid (unser Modell IST
// Extrusion): Profil = IfcArbitraryClosedProfileDef(IfcPolyline) in der
// XY-Ebene, extrudiert entlang +Z. Die horizontale Objekt-Platzierungskette
// (Site/Building/Storey/Element) trägt bewusst NUR die Z-Verschiebung
// (Geschoss-Elevation); die Profilpunkte tragen direkt die Welt-X/Y-Koordinaten.
//
// • Wand → IfcWall aus denselben Schicht-Bändern wie das 3D (`pickGeometry`: Joins/
// Gehrungen, Decken-Dominanz, Durchgangs-Aussparungen bereits aufgelöst). Zwei Wege ([B5]):
// – Regelfall: UNGESCHNITTENER Körper, je Band ein IfcExtrudedAreaSolid (Rechteck × Höhe)
// in EINER SweptSolid-Darstellung; Fenster/Türen als IfcOpeningElement (IfcRelVoids
// Element), der Viewer zieht sie ab. Gewählt nur, wenn das exakt dasselbe ergibt wie das
// 3D: jedes Band ist ein Quader (keine schräge Oberkante) und seine Löcher sind genau die
// Öffnungen (`bandsMatchOpenings`).
// – sonst (Wand unter Dach mit schräger Oberkante, Laibungs-Rücksprung der innersten
// Schicht …): öffnungsgenaues Dreiecks-Mesh (IfcTriangulatedFaceSet) wie STL/OBJ
// (`plan/wallMeshCut.ts`, Löcher inkl. Laibungen schon ausgestanzt). Öffnung + Füllung
// werden trotzdem als Beziehung angelegt; die Öffnung liegt im Loch und zieht nichts ab.
// • Decke → IfcSlab (outline-Polygon, PredefinedType FLOOR).
// • Öffnung → IfcOpeningElement (Quader über die ganze Wanddicke, etwas vorstehend) in der
// Wand (IfcRelVoidsElement), darin IfcDoor/IfcWindow (IfcRelFillsElement) mit eigener
// Box-Geometrie; kein Blatt-/Rahmendetail (bewusste Vereinfachung).
// • Extrusion → IfcBuildingElementProxy aus points+height.
// • Dach → IfcRoof (Schicht-Meshes wie im 3D), Dachtyp → IfcRoofType mit Schichten.
// • Stütze → IfcColumn (Profil-Extrusion), Bauteil → IfcMaterial.
// • Raum → IfcSpace (im Geschoss aggregiert) mit Qto_SpaceBaseQuantities und SIA-416-Kategorie.
// • Treppe → IfcStair, GEOMETRISCH bewusst vereinfacht auf einen
// extrudierten Bounding-Footprint (Lauf-Rechteck bei "straight"; Achsen-
// ausgerichtete Bounding-Box der Kontrollpunkte bei "L"/"spiral") — die
// echte Stufengeometrie ist ausgelassen (siehe stairFootprint()).
//
// Material-Schichten: je Wand-/Deckentyp ein IfcWallType/IfcSlabType mit
// IfcMaterialLayerSet, die Exemplare erben ihn über IfcRelDefinesByType. Extrusions-Wände
// ([B5], 28.10.) tragen zusätzlich eine „Axis"-Darstellung (IfcPolyline entlang der Wandachse)
// und eine IfcMaterialLayerSetUsage (AXIS2, POSITIVE, Offset = Abstand der Aussenschicht von der
// Achse), damit Revit/ArchiCAD die Schichten der Achse zuordnen: das Wand-Placement sitzt am
// Wandanfang, X = Achse, Y = linke Normale (Rust rechnet die Welt-Profile ins Wandsystem). Mesh-
// Wände (schräge Oberkante, Laibungs-Rücksprung) haben weder Achse noch Usage.
//
// Eigenschaften ([B5]): Pset_WallCommon/SlabCommon/RoofCommon/ColumnCommon/DoorCommon/
// WindowCommon/SpaceCommon nur mit Werten, die das Modell hergibt (Reference = Typname,
// IsExternal beim Dach und bei SIA-416-AGF-Räumen, Glasanteil der Glastür). Kein U-Wert,
// keine Tragfunktion: dafür hat das Modell keine Felder.
//
// GUIDs ([B5]/[10]): deterministisch aus `<Projekt-id>/<Art>/<id>[/<Zusatz>]` über einen
// 128-Bit-Hash + Standard-IFC-GUID-Kompression (Zeichensatz 0-9,A-Z,a-z,_,$) — zweimal
// exportieren gibt dieselben GlobalIds, BIM-Koordination (Solibri, BIMcollab) erkennt die
// Bauteile wieder. Abgeleitete Entitäten (Rels, Typen, Psets) hängen an Element-/Geschoss-/
// Typ-ids, nie an STEP-Nummern. Der Hash lebt in Rust (`ifc_guid`).
//
// Bezeichner englisch, Kommentare deutsch (CONVENTIONS.md). Einheit: METER.
import type { Opening, Project, Stair, Vec2, Wall } from "../model/types";
import {
columnLabel,
getCeilingType,
getDoorType,
getWallType,
getWindowType,
openingLabel,
openingsOfWall,
} from "../model/types";
import { projectToWorldCoords } from "../model/geoRebase";
import { ceilingVerticalExtent, columnVerticalExtent, stairVerticalExtent } from "../model/wall";
import { pickGeometry } from "../plan/toWalls3d";
import type { RWall } from "../plan/toWalls3d";
import { isWatertight, wallCutMeshes, type CutMesh } from "../plan/wallMeshCut";
import { evaluateRoom } from "../geometry/roomArea";
import { columnFootprint } from "../geometry/column";
import { openingInterval, openingVerticalExtent } from "../geometry/opening";
import { emitRoofs } from "../plan/toWalls3d/roofs";
import { fileio } from "../engine/fileioWasm";
/** Deterministische 22-stellige IFC-GUID aus einem Namen (Rust, `ifc_export.rs::ifc_guid`). */
export function ifcGuid(name: string): string {
return fileio().ifc_guid(name);
}
/** STEP-String-Literal mit Apostrophen, Nicht-ASCII als `\X2\`/`\X4\` (Rust, `ifc_export.rs::ifc_string`). */
export function ifcString(s: string): string {
return fileio().ifc_string(s);
}
// ── Flacher Puffer (Gegenstück zu src-tauri/fileio/src/ifc_export.rs, dort die Kodierung) ─────
const FLAT_VERSION = 1;
const REC_WALL_SOLIDS = 1;
const REC_WALL_MESH = 2;
const REC_CEILING = 3;
const REC_OPENING = 4;
const REC_STAIR = 5;
const REC_EXTRUSION = 6;
const REC_SPACE = 7;
const REC_END_SPACES = 8;
const REC_ROOF = 9;
const REC_COLUMN = 10;
const REC_END_COLUMNS = 11;
/** Typart in der flachen Kodierung (0 Wand, 1 Decke, 2 Dach). */
type TypeKind = 0 | 1 | 2;
interface TypeRefIn {
kind: TypeKind;
id: string;
name: string;
predefined: string;
layers: { componentId: string; componentName: string; thickness: number }[];
}
/** Pset-Eigenschaft: Kennung, Art (IfcIdentifier, IfcBoolean, IfcPositiveRatioMeasure) und Wert. */
type PropIn = { name: string; ident: string } | { name: string; bool: boolean } | { name: string; ratio: number };
interface PsetIn {
name: string;
props: PropIn[];
}
/** Sammelt die Bauteile als Zahlenpuffer + Zeichenkettenliste. */
class FlatIfc {
private nums: number[] = [FLAT_VERSION];
private strings: string[] = [];
private str(s: string): number {
this.strings.push(s.replace(/\0/g, ""));
return this.strings.length - 1;
}
private optStr(s: string | null): number {
return s === null ? -1 : this.str(s);
}
private pts(pts: readonly Vec2[]): void {
this.nums.push(pts.length);
for (const p of pts) this.nums.push(p.x, p.y);
}
private mesh(positions: readonly number[], indices: readonly number[], closed: boolean): void {
this.nums.push(positions.length / 3);
for (let i = 0; i < positions.length; i++) this.nums.push(positions[i]);
this.nums.push(indices.length / 3);
for (let i = 0; i < indices.length; i++) this.nums.push(indices[i]);
this.nums.push(closed ? 1 : 0);
}
private typeRef(t: TypeRefIn | null): void {
if (!t) {
this.nums.push(0);
return;
}
this.nums.push(1, t.kind, this.str(t.id), this.str(t.name), this.str(t.predefined), t.layers.length);
for (const l of t.layers) this.nums.push(this.str(l.componentId), this.str(l.componentName), l.thickness);
}
private psets(list: readonly PsetIn[]): void {
this.nums.push(list.length);
for (const ps of list) {
this.nums.push(this.str(ps.name), ps.props.length);
for (const p of ps.props) {
if ("ident" in p) this.nums.push(this.str(p.name), 0, this.str(p.ident));
else if ("bool" in p) this.nums.push(this.str(p.name), 1, p.bool ? 1 : 0);
else this.nums.push(this.str(p.name), 2, p.ratio);
}
}
}
meta(m: {
projectId: string;
projectName: string;
now: number;
iso: string;
hasGeo: boolean;
refElevation: number | null;
storeys: { id: string; name: string; base: number }[];
}): void {
this.nums.push(
this.str(m.projectId),
this.str(m.projectName),
m.now,
this.str(m.iso),
m.hasGeo ? 1 : 0,
m.refElevation ?? Number.NaN,
m.storeys.length,
);
for (const s of m.storeys) this.nums.push(this.str(s.id), this.str(s.name), s.base);
}
/**
* Extrusions-Wand. `frame`: Wandanfang, Achsrichtung (Einheitsvektor), Länge und — wenn die Schichten
* des Typs genau den Wandquerschnitt füllen — der Abstand `offset` der ersten Schicht von der
* Wandachse entlang der linken Normalen (→ IfcMaterialLayerSetUsage).
*/
wallSolids(
structure: number,
id: string,
name: string,
ty: TypeRefIn | null,
psets: PsetIn[],
frame: { origin: Vec2; u: Vec2; length: number; offset: number | null },
solids: { pts: Vec2[]; zRel: number; depth: number }[],
): void {
this.nums.push(REC_WALL_SOLIDS, structure, this.str(id), this.str(name));
this.typeRef(ty);
this.psets(psets);
this.nums.push(1, frame.origin.x, frame.origin.y, frame.u.x, frame.u.y, frame.length, frame.offset === null ? 0 : 1, frame.offset ?? 0);
this.nums.push(solids.length);
for (const s of solids) {
this.pts(s.pts);
this.nums.push(s.zRel, s.depth);
}
}
wallMesh(structure: number, id: string, name: string, ty: TypeRefIn | null, psets: PsetIn[], positions: number[], indices: number[], closed: boolean): void {
this.nums.push(REC_WALL_MESH, structure, this.str(id), this.str(name));
this.typeRef(ty);
this.psets(psets);
this.mesh(positions, indices, closed);
}
ceiling(structure: number, id: string, name: string, ty: TypeRefIn | null, psets: PsetIn[], outline: Vec2[], zRel: number, depth: number): void {
this.nums.push(REC_CEILING, structure, this.str(id), this.str(name));
this.typeRef(ty);
this.psets(psets);
this.pts(outline);
this.nums.push(zRel, depth);
}
opening(o: {
wallId: string;
structure: number;
openingId: string;
isDoor: boolean;
fillName: string;
voidRect: Vec2[];
voidZRel: number;
depth: number;
fillRect: Vec2[];
width: number;
psets: PsetIn[];
}): void {
this.nums.push(REC_OPENING, this.str(o.wallId), o.structure, this.str(o.openingId), o.isDoor ? 1 : 0, this.str(o.fillName));
this.pts(o.voidRect);
this.nums.push(o.voidZRel, o.depth);
this.pts(o.fillRect);
this.nums.push(o.width);
this.psets(o.psets);
}
stair(structure: number, id: string, predefined: string, footprint: Vec2[], zRel: number, depth: number): void {
this.nums.push(REC_STAIR, structure, this.str(id), this.str(predefined));
this.pts(footprint);
this.nums.push(zRel, depth);
}
extrusion(structure: number, id: string, pts: Vec2[], zRel: number, depth: number): void {
this.nums.push(REC_EXTRUSION, structure, this.str(id));
this.pts(pts);
this.nums.push(zRel, depth);
}
space(s: {
structure: number;
id: string;
name: string;
longName: string;
external: boolean;
height: number;
boundary: Vec2[];
netArea: number;
grossArea: number;
siaCategory: string;
psets: PsetIn[];
}): void {
this.nums.push(REC_SPACE, s.structure, this.str(s.id), this.str(s.name), this.str(s.longName), s.external ? 1 : 0, s.height);
this.pts(s.boundary);
this.nums.push(s.netArea, s.grossArea, this.str(s.siaCategory));
this.psets(s.psets);
}
endSpaces(): void {
this.nums.push(REC_END_SPACES);
}
roof(structure: number, id: string, name: string, predefined: string, ty: TypeRefIn | null, psets: PsetIn[], positions: number[], indices: number[], closed: boolean): void {
this.nums.push(REC_ROOF, structure, this.str(id), this.str(name), this.str(predefined));
this.typeRef(ty);
this.psets(psets);
this.mesh(positions, indices, closed);
}
column(c: {
structure: number;
id: string;
label: string;
footprint: Vec2[];
zRel: number;
depth: number;
component: { id: string; name: string } | null;
psets: PsetIn[];
}): void {
this.nums.push(REC_COLUMN, c.structure, this.str(c.id), this.str(c.label));
this.pts(c.footprint);
this.nums.push(c.zRel, c.depth, this.optStr(c.component?.id ?? null), this.optStr(c.component?.name ?? null));
this.psets(c.psets);
}
endColumns(): void {
this.nums.push(REC_END_COLUMNS);
}
/** Argumente für die Rust-Bindung: `[Zahlenpuffer, Zeichenketten durch \0 getrennt]`. */
args(): [Float64Array, string] {
return [new Float64Array(this.nums), this.strings.join("\0")];
}
}
// ── Geometrie-Helfer (reines 2D-Vec2-Rechnen, Welt-Meter) ───────────────────
function sub(a: Vec2, b: Vec2): Vec2 {
return { x: a.x - b.x, y: a.y - b.y };
}
function normalize(v: Vec2): Vec2 {
const len = Math.hypot(v.x, v.y) || 1;
return { x: v.x / len, y: v.y / len };
}
function leftNormal(u: Vec2): Vec2 {
return { x: -u.y, y: u.x };
}
function addScaled(p: Vec2, d: Vec2, s: number): Vec2 {
return { x: p.x + d.x * s, y: p.y + d.y * s };
}
function dot(a: Vec2, b: Vec2): number {
return a.x * b.x + a.y * b.y;
}
/** Rechteck `[from,to]` entlang der Wandachse × `[nLo,nHi]` quer dazu (Welt-XY, CCW). */
function axisRect(wall: Wall, from: number, to: number, nLo: number, nHi: number): Vec2[] {
const u = normalize(sub(wall.end, wall.start));
const n = leftNormal(u);
const a = addScaled(wall.start, u, from);
const b = addScaled(wall.start, u, to);
return [addScaled(a, n, nLo), addScaled(b, n, nLo), addScaled(b, n, nHi), addScaled(a, n, nHi)];
}
/** Öffnung einer Wand, wie sie das 3D als Loch in die Bänder stanzt (Achs-Intervall + absolute Z). */
interface WallCutout {
opening: Opening;
from: number;
to: number;
zBottom: number;
zTop: number;
}
/** Toleranz des Rust-Kerns (`wall_bands.rs::EPS`) für „Öffnung trifft Band". */
const BAND_EPS = 1e-4;
/**
* Ob die Schicht-Bänder einer Wand exakt „Quader minus Öffnungen" sind: kein Band mit schräger
* Oberkante, und jedes Band trägt genau die Öffnungen als Löcher, die es in der Höhe trifft — in
* derselben Grösse (kein Laibungs-Rücksprung o. ä.). Dann ergibt Extrusion je Band minus
* IfcOpeningElement denselben Körper wie das 3D-Mesh.
*/
function bandsMatchOpenings(wall: Wall, bands: readonly RWall[], cutouts: readonly WallCutout[]): boolean {
const u = normalize(sub(wall.end, wall.start));
const byFrom = (p: number[], q: number[]) => p[0] - q[0] || p[2] - q[2];
for (const b of bands) {
if (b.topDrop && (Math.abs(b.topDrop[0]) > 1e-9 || Math.abs(b.topDrop[1]) > 1e-9)) return false;
const a = dot({ x: b.start[0] - wall.start.x, y: b.start[1] - wall.start.y }, u);
const zb = b.baseElevation;
const zt = b.baseElevation + b.height;
const expected = cutouts
.filter((h) => h.zTop > zb + BAND_EPS && h.zBottom < zt - BAND_EPS)
.map((h) => [h.from - a, h.to - a, h.zBottom, h.zTop])
.sort(byFrom);
const actual = (b.holes ?? []).map((h) => [h.from, h.to, h.zBottom, h.zTop]).sort(byFrom);
if (expected.length !== actual.length) return false;
for (let i = 0; i < expected.length; i++) {
for (let k = 0; k < 4; k++) if (!(Math.abs(expected[i][k] - actual[i][k]) <= 1e-6)) return false;
}
}
return true;
}
/**
* Vereinfachter Bounding-Footprint einer Treppe (bewusst NICHT die echte
* Stufen-/Podestkontur, siehe Dateikopf):
* • "straight" — echtes, ausgerichtetes Lauf-Rechteck (Länge × Breite).
* • "L"/"spiral" — achsenausgerichtete Bounding-Box der Kontrollpunkte
* (Start/Eckpunkt/Ende bzw. Wendel-Zentrum±Radius), um die halbe
* Laufbreite erweitert.
*/
function stairFootprint(stair: Stair): Vec2[] {
const halfW = Math.max(stair.width, 0) / 2;
if (stair.shape === "straight") {
const u = normalize(stair.dir);
const n = leftNormal(u);
const end = addScaled(stair.start, u, stair.runLength);
return [
addScaled(stair.start, n, -halfW),
addScaled(end, n, -halfW),
addScaled(end, n, halfW),
addScaled(stair.start, n, halfW),
];
}
const pts: Vec2[] = [stair.start];
const u = normalize(stair.dir);
const corner = addScaled(stair.start, u, stair.runLength);
pts.push(corner);
if (stair.shape === "L" && stair.run2Length && stair.turn) {
const n = leftNormal(u);
const turnDir: Vec2 = { x: n.x * stair.turn, y: n.y * stair.turn };
pts.push(addScaled(corner, turnDir, stair.run2Length));
}
if (stair.shape === "spiral" && stair.center) {
const r = (stair.radius ?? 0) + halfW;
const c = stair.center;
return [
{ x: c.x - r, y: c.y - r },
{ x: c.x + r, y: c.y - r },
{ x: c.x + r, y: c.y + r },
{ x: c.x - r, y: c.y + r },
];
}
let minX = Infinity;
let minY = Infinity;
let maxX = -Infinity;
let maxY = -Infinity;
for (const p of pts) {
minX = Math.min(minX, p.x);
minY = Math.min(minY, p.y);
maxX = Math.max(maxX, p.x);
maxY = Math.max(maxY, p.y);
}
minX -= halfW;
minY -= halfW;
maxX += halfW;
maxY += halfW;
return [
{ x: minX, y: minY },
{ x: maxX, y: minY },
{ x: maxX, y: maxY },
{ x: minX, y: maxY },
];
}
// ── IFC4-Export ──────────────────────────────────────────────────────────────
/** Wie weit der Öffnungskörper beidseits über die Wandflächen vorsteht (keine koplanaren Booleans). */
const OPENING_OVERSHOOT = 0.02;
const ROOF_PREDEFINED: Record<string, string> = {
flach: "FLAT_ROOF",
pult: "SHED_ROOF",
sattel: "GABLE_ROOF",
walm: "HIP_ROOF",
mansarde: "MANSARD_ROOF",
zelt: "HIPPED_GABLE_ROOF",
};
const identifier = (name: string, v: string): PropIn => ({ name, ident: v });
/**
* Baut aus einem Projekt einen vollständigen IFC4-SPF-String (STEP Physical
* File). Kein Datei-IO. Leeres Projekt ⇒ valider Minimal-IFC (Project/Site/Building, kein Crash).
* Verlangt das geladene Format-Modul (`await loadFileio()`).
*/
export function exportIfcSpf(projectIn: Project): string {
// Georeferenziert exportieren: ist ein Standort-Bezug gesetzt (geoAnchor),
// wird die gesamte Geometrie auf die ECHTEN LV95-Weltkoordinaten verschoben,
// damit die IFC-Datei in anderer Software lagerichtig sitzt — auch wenn das
// Modell lokal nahe (0,0) bearbeitet wurde (s. projectToWorldCoords / der
// „Neuer Bezugspunkt"-Workflow). Ohne Bezug bleibt alles lokal (Identität).
const project = projectToWorldCoords(projectIn);
const flat = new FlatIfc();
// Je "floor"-Geschoss ein IfcBuildingStorey (Elevation = baseElevation).
const floors = project.drawingLevels.filter((l) => l.kind === "floor");
const storeyIndexByFloorId = new Map<string, number>();
const storeyBase: number[] = [];
floors.forEach((floor, i) => {
storeyIndexByFloorId.set(floor.id, i);
storeyBase.push(floor.baseElevation ?? 0);
});
/** Geschoss → Index der Trägerstruktur, oder −1 = Gebäude (verwaiste floorId). */
const structureOf = (floorId: string): number => storeyIndexByFloorId.get(floorId) ?? -1;
const baseOf = (structure: number): number => (structure < 0 ? 0 : storeyBase[structure]);
// RefElevation (m ü. M.): reale Datumshöhe des Standorts aus dem Georeferenzierungs-Bezug (wie
// ArchiCADs Vermessungspunkt). Rein informativ — die Geometrie wird NICHT verschoben.
const refElevation = project.geoAnchor?.height;
flat.meta({
projectId: project.id,
projectName: project.name ?? "",
now: Math.floor(Date.now() / 1000),
iso: new Date().toISOString().replace(/\.\d+Z$/, ""),
hasGeo: !!project.geoAnchor,
refElevation: typeof refElevation === "number" ? refElevation : null,
storeys: floors.map((f, i) => ({ id: f.id, name: f.name, base: storeyBase[i] })),
});
const componentName = (componentId: string): string =>
project.components.find((c) => c.id === componentId)?.name ?? componentId;
const typeRef = (
kind: TypeKind,
predefined: string,
t: { id: string; name: string; layers: { componentId: string; thickness: number }[] },
): TypeRefIn => ({
kind,
id: t.id,
name: t.name,
predefined,
layers: t.layers.map((l) => ({ componentId: l.componentId, componentName: componentName(l.componentId), thickness: l.thickness })),
});
// ── Wände ──────────────────────────────────────────────────────────────
// Die Wand-Schicht-Bänder kommen aus DEMSELBEN geflachten Modell wie STL/OBJ
// (`pickGeometry`, Joins/Gehrungen + Öffnungs-`holes` bereits aufgelöst).
//
// Extrusions-Weg (s. Dateikopf): je Band ein Rechteck-Profil (Achse ± halbe Banddicke)
// von der Band-UK über die Bandhöhe; die Öffnungen schneidet der Viewer über RelVoids.
//
// Mesh-Weg: alle Bänder einer Wand-Id zu EINEM Face-Set. IFC-Koordinaten: das
// Mesh liegt in Welt (Modell-x, Höhe, Modell-y) mit Y-up → IFC (x, y, z=Höhe)
// mit Z-up, also (mx, mz, my); Z relativ zur Geschoss-UK, damit die Storey-
// Placement-Elevation nicht doppelt zählt.
// WICHTIG — WICKLUNG: der Achsen-Swap (x,y,z)→(x,z,y) ist eine REFLEXION
// (Determinante −1) und KEHRT die Dreiecks-Wicklung UM → aus aussen orientierten
// würden innen orientierte Normalen, der Viewer cullt dann die Vorderseiten und
// die Wand wirkt HOHL/offen. Deshalb wird beim Swap die Wicklung jedes Dreiecks
// umgedreht (i0,i2,i1). Watertightness (isWatertight) misst das anschliessend am
// fertigen IFC-Mesh → treibt das Closed-Flag des Face-Sets.
const bandsByWallId = new Map<string, RWall[]>();
for (const band of pickGeometry(project).walls) {
const list = bandsByWallId.get(band.wallId);
if (list) list.push(band);
else bandsByWallId.set(band.wallId, [band]);
}
const cutoutsOf = (wall: Wall): WallCutout[] => {
const out: WallCutout[] = [];
for (const opening of openingsOfWall(project, wall.id)) {
const iv = openingInterval(wall, opening);
if (!iv) continue;
const v = openingVerticalExtent(project, wall, opening);
out.push({ opening, from: iv.from, to: iv.to, zBottom: v.zBottom, zTop: v.zTop });
}
return out;
};
/** Name, Typ und Pset einer Wand (verwaister Wandtyp: Roh-ID als Name, kein Typ). */
const wallIdentity = (wall: Wall): { name: string; ty: TypeRefIn | null; psets: PsetIn[] } => {
try {
const wt = getWallType(project, wall);
return {
name: wt.name,
ty: typeRef(0, "STANDARD", wt),
psets: [{ name: "Pset_WallCommon", props: [identifier("Reference", wt.name)] }],
};
} catch {
return { name: wall.id, ty: null, psets: [] };
}
};
/** Ausdehnung aller Bänder quer zur Wandachse (entlang der linken Normalen ab `wall.start`). */
const crossExtent = (wall: Wall, bands: readonly RWall[]): { nLo: number; nHi: number } => {
const n = leftNormal(normalize(sub(wall.end, wall.start)));
let nLo = Infinity;
let nHi = -Infinity;
for (const b of bands) {
const off = dot({ x: b.start[0] - wall.start.x, y: b.start[1] - wall.start.y }, n);
nLo = Math.min(nLo, off - b.thickness / 2);
nHi = Math.max(nHi, off + b.thickness / 2);
}
return { nLo, nHi };
};
const meshWalls: { wall: Wall; bands: RWall[] }[] = [];
const hostExtent = new Map<string, { nLo: number; nHi: number }>();
const cutoutsByWallId = new Map<string, WallCutout[]>();
for (const wall of project.walls ?? []) {
const bands = (bandsByWallId.get(wall.id) ?? []).filter(
(b) => Math.hypot(b.end[0] - b.start[0], b.end[1] - b.start[1]) > 1e-9 && b.height > 1e-9 && b.thickness > 1e-9,
);
if (bands.length === 0) continue; // degenerierte Wand / keine Geometrie
const cutouts = cutoutsOf(wall);
cutoutsByWallId.set(wall.id, cutouts);
if (!bandsMatchOpenings(wall, bands, cutouts)) {
meshWalls.push({ wall, bands: bandsByWallId.get(wall.id)! });
continue;
}
const structure = structureOf(wall.floorId);
const solids = bands.map((b) => {
const s: Vec2 = { x: b.start[0], y: b.start[1] };
const e: Vec2 = { x: b.end[0], y: b.end[1] };
const n = leftNormal(normalize(sub(e, s)));
const h = b.thickness / 2;
const rect = [addScaled(s, n, -h), addScaled(e, n, -h), addScaled(e, n, h), addScaled(s, n, h)];
return { pts: rect, zRel: b.baseElevation - baseOf(structure), depth: b.height };
});
const id = wallIdentity(wall);
// Wandsystem: Placement am Wandanfang, X = Achse, Y = linke Normale; die Schichten des Typs
// stehen ab der Aussenfläche (negative Normale) nebeneinander — IfcMaterialLayerSetUsage nur,
// wenn ihre Summe dem Querschnitt der Bänder entspricht (sonst wäre der Offset gelogen).
const extent = crossExtent(wall, bands);
const layerSum = id.ty?.layers.reduce((a, l) => a + l.thickness, 0) ?? 0;
const usage = id.ty !== null && layerSum > 0 && Math.abs(extent.nHi - extent.nLo - layerSum) < 1e-3;
flat.wallSolids(
structure,
wall.id,
id.name,
id.ty,
id.psets,
{ origin: wall.start, u: normalize(sub(wall.end, wall.start)), length: Math.hypot(wall.end.x - wall.start.x, wall.end.y - wall.start.y), offset: usage ? extent.nLo : null },
solids,
);
hostExtent.set(wall.id, extent);
}
// Mesh-Weg: Loch-Ausschnitte aller betroffenen Bänder in EINEM Kern-Aufruf.
const meshBands = meshWalls.flatMap((m) => m.bands);
const cutOf = new Map<RWall, CutMesh>();
wallCutMeshes(meshBands).forEach((m, i) => cutOf.set(meshBands[i], m));
for (const { wall, bands } of meshWalls) {
const structure = structureOf(wall.floorId);
const positions: number[] = [];
const indices: number[] = [];
for (const band of bands) {
const cut = cutOf.get(band);
if (!cut) continue;
const base = positions.length / 3;
for (let i = 0; i < cut.positions.length; i += 3) {
positions.push(cut.positions[i], cut.positions[i + 2], cut.positions[i + 1] - baseOf(structure));
}
// Wicklung umkehren (Reflexions-Ausgleich, s. o.): (a,b,c) → (a,c,b).
for (let i = 0; i < cut.indices.length; i += 3) {
indices.push(base + cut.indices[i], base + cut.indices[i + 2], base + cut.indices[i + 1]);
}
}
if (indices.length === 0) continue;
// Closed=.T. NUR wenn das Mesh nachweislich ein dichtes, aussen orientiertes Volumen ist
// (Wände sind extrudierte Querschnitts-Polygone → geschlossene Körper); der Guard fängt echte
// Defekte ab (invertierte Wicklung → Volumen < 0 → `$`).
const closed = isWatertight({ positions, indices });
const id = wallIdentity(wall);
flat.wallMesh(structure, wall.id, id.name, id.ty, id.psets, positions, indices, closed);
hostExtent.set(wall.id, crossExtent(wall, bandsByWallId.get(wall.id)!));
}
// ── Decken ─────────────────────────────────────────────────────────────
for (const ceiling of project.ceilings ?? []) {
if (ceiling.outline.length < 3) continue;
const { zBottom, zTop } = ceilingVerticalExtent(project, ceiling);
const structure = structureOf(ceiling.floorId);
let name: string = ceiling.id;
let ty: TypeRefIn | null = null;
const psets: PsetIn[] = [];
try {
const ct = getCeilingType(project, ceiling);
name = ct.name;
ty = typeRef(1, "FLOOR", ct);
psets.push({ name: "Pset_SlabCommon", props: [identifier("Reference", ct.name)] });
} catch {
/* verwaister Deckentyp — Roh-ID als Name, kein Typ */
}
flat.ceiling(structure, ceiling.id, name, ty, psets, ceiling.outline, zBottom - baseOf(structure), zTop - zBottom);
}
// ── Öffnungen: IfcOpeningElement in der Wand, IfcDoor/IfcWindow darin ───
// Der Öffnungskörper ist ein Quader über das Achs-Intervall und die Höhe, wie sie das 3D
// stanzt (`openingInterval`/`openingVerticalExtent`), quer über alle Bänder der Wand plus
// OPENING_OVERSHOOT. Die Füllung (Tür/Fenster) sitzt im Öffnungs-Placement, über die
// Banddicke, ohne Blatt-/Rahmendetail.
for (const wall of project.walls ?? []) {
const host = hostExtent.get(wall.id);
if (!host) continue; // Wirtswand übersprungen (degeneriert)
const structure = structureOf(wall.floorId);
for (const c of cutoutsByWallId.get(wall.id) ?? []) {
const { opening } = c;
const depth = c.zTop - c.zBottom;
if (!(depth > 1e-6)) continue;
const isDoor = opening.kind === "door";
const props: PropIn[] = [];
let psetName: string;
if (isDoor) {
psetName = "Pset_DoorCommon";
const dt = getDoorType(project, opening);
if (dt) props.push(identifier("Reference", dt.name));
const ratio = dt?.glazingRatio;
if (dt?.leafStyle === "glas" && typeof ratio === "number" && ratio > 0 && ratio <= 1) {
props.push({ name: "GlazingAreaFraction", ratio });
}
} else {
psetName = "Pset_WindowCommon";
const wt = getWindowType(project, opening);
if (wt) props.push(identifier("Reference", wt.name));
}
flat.opening({
wallId: wall.id,
structure,
openingId: opening.id,
isDoor,
fillName: openingLabel(opening),
voidRect: axisRect(wall, c.from, c.to, host.nLo - OPENING_OVERSHOOT, host.nHi + OPENING_OVERSHOOT),
voidZRel: c.zBottom - baseOf(structure),
depth,
fillRect: axisRect(wall, c.from, c.to, host.nLo, host.nHi),
width: c.to - c.from,
psets: [{ name: psetName, props }],
});
}
}
// ── Treppen (vereinfachter Bounding-Footprint, siehe Dateikopf) ────────
for (const stair of project.stairs ?? []) {
const footprint = stairFootprint(stair);
if (footprint.length < 3) continue;
const { zBottom, zTop } = stairVerticalExtent(project, stair);
const structure = structureOf(stair.floorId);
const predefined = stair.shape === "straight" ? "STRAIGHT_RUN_STAIR" : stair.shape === "spiral" ? "SPIRAL_STAIR" : "QUARTER_TURN_STAIR";
flat.stair(structure, stair.id, predefined, footprint, zBottom - baseOf(structure), zTop - zBottom);
}
// ── Extrudierte Körper (truck-Integration) → IfcBuildingElementProxy ───
for (const solid of project.extrudedSolids ?? []) {
if (solid.points.length < 3) continue;
const floor = floors.find((f) => f.id === solid.levelId);
const base = floor?.baseElevation ?? 0;
const structure = structureOf(solid.levelId);
flat.extrusion(structure, solid.id, solid.points, base - baseOf(structure), solid.height);
}
// ── Räume → IfcSpace ───────────────────────────────────────────────────
// Je Raum ein IfcSpace (Name = Raumnummer, LongName = Raumname), als Extrusion der lichten
// Innenkontur über die lichte Geschosshöhe. Räume sind räumliche Elemente: sie werden im
// Geschoss AGGREGIERT (IfcRelAggregates), nicht enthalten. Dazu Basismengen
// (Qto_SpaceBaseQuantities), die SIA-416-Kategorie als Eigenschaft und Pset_SpaceCommon
// (AGF = Aussengeschossfläche ⇒ aussen, alles andere innen).
for (const room of project.rooms ?? []) {
if (room.boundary.length < 3) continue;
const structure = structureOf(room.floorId);
const floor = floors.find((f) => f.id === room.floorId);
const height = floor?.floorHeight && floor.floorHeight > 0 ? floor.floorHeight : 2.5;
const external = room.siaCategory === "AGF";
const area = evaluateRoom(room.boundary, room.siaCategory);
const common: PropIn[] = [];
if (room.stamp?.number) common.push(identifier("Reference", room.stamp.number));
common.push({ name: "IsExternal", bool: external });
flat.space({
structure,
id: room.id,
name: room.stamp?.number || room.id,
longName: room.name,
external,
height,
boundary: room.boundary,
netArea: area.netArea,
grossArea: area.grossArea,
siaCategory: room.siaCategory,
psets: [{ name: "Pset_SpaceCommon", props: common }],
});
}
flat.endSpaces();
// ── Dächer → IfcRoof ───────────────────────────────────────────────────
// Dieselben geschlossenen Schicht-Meshes wie das 3D (`emitRoofs`: Dachflächen je Schicht,
// an First/Grat gehrt), zu EINEM Face-Set je Dach vereint. Modell-Koordinaten sind bereits
// Z-oben (kein Achsentausch wie bei den Wänden); die Wicklung wird so gewählt, dass das Volumen
// positiv ist — nur dann ist das Face-Set als geschlossen markiert.
for (const roof of project.roofs ?? []) {
const structure = structureOf(roof.floorId);
const positions: number[] = [];
const indices: number[] = [];
for (const mesh of emitRoofs({ ...project, roofs: [roof] })) {
const base = positions.length / 3;
for (let i = 0; i < mesh.positions.length; i += 3) {
positions.push(mesh.positions[i], mesh.positions[i + 1], mesh.positions[i + 2] - baseOf(structure));
}
for (const ix of mesh.indices) indices.push(base + ix);
}
if (indices.length === 0) continue;
let closed = isWatertight({ positions, indices });
if (!closed) {
const flipped: number[] = [];
for (let i = 0; i < indices.length; i += 3) flipped.push(indices[i], indices[i + 2], indices[i + 1]);
if (isWatertight({ positions, indices: flipped })) {
indices.splice(0, indices.length, ...flipped);
closed = true;
}
}
const predefined = ROOF_PREDEFINED[roof.shape] ?? "NOTDEFINED";
const roofType = roof.roofTypeId ? (project.roofTypes ?? []).find((t) => t.id === roof.roofTypeId) : undefined;
// Ein Dach ist Gebäudehülle: IsExternal folgt aus der Bauteilart, nicht aus einer Annahme.
const props: PropIn[] = [];
if (roofType) props.push(identifier("Reference", roofType.name));
props.push({ name: "IsExternal", bool: true });
flat.roof(
structure,
roof.id,
roofType?.name ?? "Dach",
predefined,
roofType ? typeRef(2, predefined, roofType) : null,
[{ name: "Pset_RoofCommon", props }],
positions,
indices,
closed,
);
}
// ── Stützen → IfcColumn (Profil-Extrusion, Material = Bauteil) ──────────
for (const col of project.columns ?? []) {
const footprint = columnFootprint(col);
if (footprint.length < 3) continue;
const { zBottom, zTop } = columnVerticalExtent(project, col);
if (!(zTop > zBottom)) continue;
const structure = structureOf(col.floorId);
const label = columnLabel(col);
flat.column({
structure,
id: col.id,
label,
footprint,
zRel: zBottom - baseOf(structure),
depth: zTop - zBottom,
component: col.componentId ? { id: col.componentId, name: componentName(col.componentId) } : null,
psets: [{ name: "Pset_ColumnCommon", props: [identifier("Reference", label)] }],
});
}
flat.endColumns();
const m = fileio();
const [buf, strings] = flat.args();
const text = m.export_ifc_flat(buf, strings);
if (text.length === 0) {
const why = m.export_ifc_check_flat(buf, strings);
throw new Error(`IFC-Export fehlgeschlagen${why ? `: ${why}` : ""}`);
}
return text;
}