IFC Round Trip

Export a pavilion to IFC, read the file back with fromIfc, and show the source model and the model from the file side by side. Each solid of the second pavilion comes from the exported file, so a difference between the writer and the importer is visible. Its colour is the geometry fidelity of each element: teal for a parametric solid, orange for a mesh that the importer sewed into a solid. The console reports the fidelity counts, the GlobalIds that survived, and the largest volume difference that the kernel measures between a source body and its copy. The pavilion is a display model.
Code
This is the full program. The playground runs it in your browser without a change.
import { BimModel, disposeImportedModel, fromIfc, placedSolids, toIfc } from 'brepjs-bim';
import { box, compound, measureVolume, translate, unwrap } from 'brepjs/quick';
import { color, present } from 'brepjs/playground';
// IFC round trip: write a model to IFC, read the file back with fromIfc, and show the two side by side.
// The pavilion at y = 0 is the source model. The second one is made only from the file, and its colour is
// the fidelity of each body: teal is a parametric solid, orange is a mesh that was sewn into a solid.
// The console has the counts. The pavilion is a display model. Units are millimetres.
interface TripParams {
show?: 'both' | 'source' | 'file'; // which pavilion to show
gap?: number; // clear distance between the two roofs. -6900 lays the copy from the file on the source
pitch?: number; // degrees of the roof, in the source model and so also in the file
}
type P3 = [number, number, number];
type Made = ReturnType<BimModel['addWall']>;
type Id = Extract<Made, { ok: true }>['value'];
type Body = ReturnType<typeof box>;
type Tone = 'wall' | 'slab' | 'roof' | 'steel' | 'glass' | 'door' | 'ground';
const X: P3 = [1, 0, 0], Y: P3 = [0, 1, 0], Z: P3 = [0, 0, 1];
// ---- display model: the plan of the pavilion. The front and its porch look toward +X
const ROOM = { depth: 4200, length: 6000, height: 2700, wall: 200 };
const PORCH = 1800, DECK = 300, EAVES = 450;
const SOURCE: Record<Tone, string> = { wall: '#f5f2ea', slab: '#c3c9d1', roof: '#48515c', steel: '#8a99ad', glass: '#9fc3d4', door: '#d3a956', ground: '#6b7480' };
const FIDELITY: Record<string, string> = { PARAMETRIC: '#12b5b0', TESSELLATED_MANIFOLD: '#ff5a1f', TESSELLATED_LOSSY: '#d3a956', NONE: '#48515c' };
// ---- THE LESSON. Try trip({ show: 'file' }), trip({ gap: -6900 }) or trip({ pitch: 45 }).
const run = await trip();
console.log('exported ' + run.bytes.length + ' bytes, schema ' + run.schema);
console.log('read back ' + run.read + ' elements with a body: ' + run.fidelity);
console.log(run.survived + ' of ' + run.read + ' imported GlobalIds match the source model');
console.log('largest volume difference between a source body and its body from the file: ' + run.worst.toFixed(4) + ' %');
async function trip({ show = 'both', gap = 1500, pitch = 25 }: TripParams = {}) {
const source = pavilion(pitch);
const bytes = unwrap(await toIfc(source.model, { applicationName: 'brepjs playground', applicationVersion: '1.0' }));
const back = unwrap(await fromIfc(bytes));
// An opening comes back as an element with the solid of its void. It is a hole, so it is not shown or counted.
const bodies = back.elements.filter((e) => e.category !== 'OPENING' && e.geometry.solids.length > 0);
// Fidelity is in the report for each element. PARAMETRIC: the importer made a true B-Rep solid again from the
// profile and the extrusion in the file. TESSELLATED_MANIFOLD: it sewed a mesh into a closed solid.
const tally = new Map<string, number>();
for (const e of bodies) tally.set(e.geometry.fidelity, (tally.get(e.geometry.fidelity) ?? 0) + 1);
// The GlobalId is the identity contract across the file: each id that the writer made must come back.
const ids = new Set(source.model.getAllElements().map((el) => el.guid));
const survived = bodies.filter((e) => ids.has(e.guid)).length;
const volumes = source.volumes();
// The kernel measured each source body. The importer gives the volume of the body that it made.
let worst = 0;
for (const e of bodies) {
const was = volumes.get(e.guid);
if (was !== undefined && e.geometry.volumeMm3 !== null) worst = Math.max(worst, (100 * Math.abs(e.geometry.volumeMm3 - was)) / was);
}
// The imported model owns the kernel memory of each solid that it read. The view takes a moved copy of each
// one, and then the imported model is released, as in a real application.
const shift = ROOM.length + 2 * EAVES + gap;
const groups = new Map<string, Body[]>();
for (const e of bodies) groups.set(e.geometry.fidelity, [...(groups.get(e.geometry.fidelity) ?? []), ...e.geometry.solids.map((s) => translate(s, [0, shift, 0]) as Body)]);
const fromFile = [...groups].map(([fidelity, list]) => color(compound(list), FIDELITY[fidelity] ?? '#48515c'));
for (const list of groups.values()) list.forEach((s) => s.delete());
disposeImportedModel(back);
const shown = [...(show === 'file' ? [] : source.view()), ...(show === 'source' ? [] : fromFile)];
source.release();
const fidelity = [...tally].map(([name, n]) => n + ' ' + name).join(', ');
return { model: source.model, bytes, schema: back.schema, read: bodies.length, fidelity, survived, worst, shown };
}
// ---- THE SCENERY: a pavilion with a porch, on a deck with two steps.
function pavilion(pitch: number) {
const model = new BimModel();
model.init({ name: 'Round trip', projectId: 'round-trip-demo' });
const site = unwrap(model.addSite({ name: 'Site' }));
const building = unwrap(model.addBuilding({ name: 'Pavilion' }));
const storey = unwrap(model.addStorey({ name: 'Ground Floor', elevation: 0 }));
const project = model.getProject();
if (project) model.aggregate(project.localId, site);
model.aggregate(site, building);
model.aggregate(building, storey);
const placed: { id: Id; tone: Tone }[] = [];
const owned: Body[] = [];
const put = (tone: Tone, made: Made): Id => {
const id = unwrap(made);
model.placeIn(id, storey);
placed.push({ id, tone });
return id;
};
const fill = (tone: Tone, name: string, lo: P3, hi: P3): Id => {
const unit = box(hi[0] - lo[0], hi[1] - lo[1], hi[2] - lo[2]);
owned.push(unit);
return put(tone, model.addProxy({ name, solid: translate(unit, lo) }));
};
const { depth: A, length: B, height: H, wall: T } = ROOM;
const front = A + PORCH; // the edge of the deck
put('ground', model.addSlab({ length: front + 1900, width: B + 1400, thickness: 150, origin: [-700, -700, -DECK - 150], axisX: X, axisZ: Z, predefinedType: 'BASESLAB', materialName: 'Paving' }));
put('slab', model.addSlab({ length: front + 150, width: B + 300, thickness: DECK, origin: [-150, -150, -DECK], axisX: X, axisZ: Z, predefinedType: 'FLOOR', materialName: 'Concrete' }));
// Four walls. A wall goes toward its local Y from its axis, so each axis is on the outer face.
const wall = { height: H, thickness: T, axisZ: Z, materialName: 'Masonry', isExternal: true };
const face = put('wall', model.addWall({ ...wall, length: B, origin: [A, 0, 0], axisX: Y }));
put('wall', model.addWall({ ...wall, length: B, origin: [0, B, 0], axisX: [0, -1, 0] }));
const south = put('wall', model.addWall({ ...wall, length: A - 2 * T, origin: [T, 0, 0], axisX: X }));
const north = put('wall', model.addWall({ ...wall, length: A - 2 * T, origin: [A - T, B, 0], axisX: [-1, 0, 0] }));
// The openings: a door and a window in the front, and a window in each end. A door or a window has no body
// in the source model. The writer gives each one a panel in the inner 100 mm of its wall, so the file has four
// more bodies than the source view. The leaf and the glass are proxies in the outer part of each opening.
put('door', model.addDoor({ wallLocalId: face, width: 1000, height: 2100, offsetAlongWall: 900, offsetFromFloor: 0, materialName: 'Timber' }));
fill('door', 'Door leaf', [A - 70, 900, 0], [A - 20, 1900, 2100]);
put('glass', model.addWindow({ wallLocalId: face, width: 1800, height: 1200, offsetAlongWall: 3000, offsetFromFloor: 900, materialName: 'Aluminium' }));
fill('glass', 'Glazing', [A - 60, 3000, 900], [A - 30, 4800, 2100]);
const side = (A - 2 * T - 1500) / 2;
for (const [id, y] of [[south, 40], [north, B - 70]] as const) {
put('glass', model.addWindow({ wallLocalId: id, width: 1500, height: 1200, offsetAlongWall: side, offsetFromFloor: 900, materialName: 'Aluminium' }));
fill('glass', 'Glazing', [T + side, y, 900], [T + side + 1500, y + 30, 2100]);
}
// The porch: three round posts with a square beam, a guard rail, and two steps up to the deck.
const steel = { materialName: 'Steel S355' };
for (const y of [150, B / 2, B - 150]) put('steel', model.addColumn({ height: H - 150, profile: { kind: 'CIRCLE_HOLLOW', radius: 57, wallThickness: 5 }, origin: [front - 200, y, 0], axisX: X, axisZ: Z, ...steel }));
put('steel', model.addBeam({ length: B, profile: { kind: 'RECTANGLE_HOLLOW', xDim: 150, yDim: 150, wallThickness: 6 }, origin: [front - 200, 0, H - 75], axisX: Y, axisZ: Z, ...steel }));
const rail = { height: 1000, thickness: 40, axisX: Y, axisZ: Z, infill: 'POSTED', predefinedType: 'GUARDRAIL', ...steel } as const;
put('steel', model.addRailing({ ...rail, length: 500, origin: [front, 250, 0] }));
put('steel', model.addRailing({ ...rail, length: 3500, origin: [front, 2250, 0] }));
for (const n of [1, 2]) put('slab', model.addSlab({ length: 300 * n, width: 1500, thickness: DECK / 2, origin: [front, 750, -(DECK * n) / 2], axisX: X, axisZ: Z, predefinedType: 'LANDING', materialName: 'Concrete' }));
// One gable roof over the room and the porch. Its width goes toward -X from the front eaves.
put('roof', model.addRoof({ length: B + 2 * EAVES, width: front + 2 * EAVES - 300, thickness: 150, origin: [front + EAVES - 300, -EAVES, H], axisX: Y, axisZ: Z, predefinedType: 'GABLE_ROOF', pitch, materialName: 'Slate' }));
const elements = new Map(model.getAllElements().map((el) => [el.localId, el]));
const solidsOf = (id: Id): Body[] => {
const list = unwrap(placedSolids(elements.get(id) as never)) as Body[];
owned.push(...list);
return list;
};
// The volume of each source body, by GlobalId.
const volumes = () => new Map(placed.map((p) => [elements.get(p.id)?.guid ?? '', solidsOf(p.id).reduce((sum, s) => sum + unwrap(measureVolume(s)), 0)] as const).filter(([, v]) => v > 0));
const view = () => {
const groups = new Map<Tone, Body[]>();
for (const p of placed) groups.set(p.tone, [...(groups.get(p.tone) ?? []), ...solidsOf(p.id)]);
return [...groups].map(([tone, list]) => color(compound(list), SOURCE[tone]));
};
const release = (): void => owned.splice(0).forEach((s) => s.delete());
return { model, volumes, view, release };
}
export default present(run.shown, {
bimTree: run.model.toTreeSummary(),
ifc: () => run.bytes,
});Functions used
disposeImportedModel fromIfc placedSolids toIfc box compound measureVolume translate unwrap color present