Skip to content

Validated Export ​

Validated Export, rendered in the brepjs playground
BIM · 174 lines▶ Run it in the playground

One toIfcValidated call exports the model and runs the full set of checkers: referential integrity before the file is written, then schema, round trip, geometry validity and the buildingSMART gherkin rules. This steel-framed block has two faults on purpose. A column that is in no storey makes the integrity gate block the export: the view paints the column that the gate names in orange, and the code repairs it. A project with no coordinate reference system gives the GRF003 warning. The export runs against IFC4 and IFC4X3, and the Files button saves the full report. The sections are published sizes, and the block is a display model.

Code ​

This is the full program. The playground runs it in your browser without a change.

ts
import { BimModel, countBySeverity, placedSolids, toIfcValidated } from 'brepjs-bim';
import { box, compound, translate, unwrap } from 'brepjs/quick';
import { color, present } from 'brepjs/playground';

// Validated export: toIfcValidated writes the IFC file and runs the full set of checkers in one call.
// The model is a steel-framed block with two faults put in on purpose. One column is in no storey: the
// integrity gate blocks the export and names it, and the view paints it orange. The project has no
// coordinate reference system: the buildingSMART rule GRF003 gives a warning, and the file is still good.
// The console has the report. web-ifc also logs Invalid ExpressID lines when it saves a model with openings:
// that is its own log, not a finding.
interface CheckParams {
  storeys?: number; // the number of storeys of the frame
  stray?: boolean; // false puts each column in its storey, so the gate has nothing to block
  georeference?: boolean; // true gives the project a coordinate reference system, which clears GRF003
}

type P3 = [number, number, number];
type Made = ReturnType<BimModel['addColumn']>;
type Id = Extract<Made, { ok: true }>['value'];
type Body = ReturnType<typeof box>;
type Finding = { severity: string; code: string; message: string; entity?: string | number };
type Tone = 'steel' | 'slab' | 'wall' | 'ground' | 'glass' | 'rail' | 'named';
const X: P3 = [1, 0, 0], Y: P3 = [0, 1, 0], Z: P3 = [0, 0, 1];

// ---- published figures: rolled sections of the European tables, drawn without their root radii
const HE200B = { kind: 'I_BEAM', overallWidth: 200, overallDepth: 200, flangeThickness: 15, webThickness: 9 } as const;
const IPE300 = { kind: 'I_BEAM', overallWidth: 150, overallDepth: 300, flangeThickness: 10.7, webThickness: 7.1 } as const;
const IPE240 = { kind: 'I_BEAM', overallWidth: 120, overallDepth: 240, flangeThickness: 9.8, webThickness: 6.2 } as const;

// ---- display model
const GRID = { x: 6000, y: 6000, bays: [2, 3], storey: 3600 };
const SLAB = { depth: 200, edge: 300 };
const GUARD = { height: 1100, bar: 40 };
const COLORS: Record<Tone, string> = { steel: '#c3c9d1', slab: '#f5f2ea', wall: '#d9d3c7', ground: '#48515c', glass: '#9fc3d4', rail: '#2b3038', named: '#ff5a1f' };

// A severity summary is the useful read of a report. Findings have a severity, and are never thrown.
const report = (label: string, issues: readonly Finding[]): void => {
  const counts = { error: 0, warning: 0, info: 0 };
  for (const i of issues) counts[i.severity as 'error' | 'warning' | 'info'] += 1;
  console.log(label + ': ' + counts.error + ' errors, ' + counts.warning + ' warnings, ' + counts.info + ' info');
  for (const i of issues.slice(0, 4)) console.log('   [' + i.severity + '] ' + i.code + ' ' + i.message);
};

// ---- THE LESSON. Try block({ storeys: 3 }), block({ stray: false }) or block({ georeference: true }).
const site = block();
const meta = { applicationName: 'brepjs playground', applicationVersion: '1.0' };

// Only a referential-integrity failure makes toIfcValidated return Err. Its cause is the list of findings,
// and each finding names its element. Here the repair puts the named element in the ground storey.
let ifc4 = await toIfcValidated(site.model, meta);
const named: Id[] = [];
if (!ifc4.ok && ifc4.error.code === 'INTEGRITY_FAILURE') {
  for (const finding of ifc4.error.cause as readonly Finding[]) {
    console.log('blocked: [' + finding.severity + '] ' + finding.code + ' ' + finding.message);
    named.push(finding.entity as Id);
    site.model.placeIn(finding.entity as Id, site.levels[0] as Id);
  }
  ifc4 = await toIfcValidated(site.model, meta);
}
// The export now passes the gate. The schema, round-trip, geometry and gherkin findings come back in the report
// with bytes that a reader can open, so the caller decides if the file can ship.
const checked = unwrap(ifc4);
report('IFC4', checked.report.issues);

// The same model against the newer schema: ifcSchema is the only change. countBySeverity is the short tally.
const newer = unwrap(await toIfcValidated(site.model, { ...meta, ifcSchema: 'IFC4X3' }));
const x3 = countBySeverity(newer.report);
console.log('IFC4X3: ' + x3.error + ' errors, ' + x3.warning + ' warnings, ' + x3.info + ' info');

// ---- THE SCENERY: a steel frame on pad footings, with a floor slab, a masonry wall at the back and guard rails.
function block({ storeys = 2, stray = true, georeference = false }: CheckParams = {}) {
  const model = new BimModel();
  // A project with no crs gives GRF003. The eastings and northings are the map position of the origin, in metres.
  model.init({ name: 'Validated building', projectId: 'validation-demo', ...(georeference ? { crs: { name: 'EPSG:25832', eastings: 566000, northings: 5933000 } } : {}) });
  const ground = unwrap(model.addSite({ name: 'Site' }));
  const building = unwrap(model.addBuilding({ name: 'Building' }));
  const project = model.getProject();
  if (project) model.aggregate(project.localId, ground);
  model.aggregate(ground, building);
  const levels = Array.from({ length: storeys + 1 }, (_, i) => {
    const id = unwrap(model.addStorey({ name: i === storeys ? 'Roof' : i === 0 ? 'Ground floor' : 'Floor ' + i, elevation: i * GRID.storey }));
    model.aggregate(building, id);
    return id;
  });
  const placed: { id: Id; level: number; tone: Tone }[] = [];
  const put = (level: number, tone: Tone, made: Made, contained = true): Id => {
    const id = unwrap(made);
    if (contained) model.placeIn(id, levels[level] as Id);
    placed.push({ id, level, tone });
    return id;
  };
  const steel = { materialName: 'Steel S355' }, concrete = { materialName: 'Concrete C30/37' };
  const [nx, ny] = GRID.bays as [number, number];
  const LENGTH = nx * GRID.x, WIDTH = ny * GRID.y, e = SLAB.edge;
  const xs = Array.from({ length: nx + 1 }, (_, i) => i * GRID.x), ys = Array.from({ length: ny + 1 }, (_, j) => j * GRID.y);
  const owned: Body[] = [];
  const pane = (level: number, 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(level, 'glass', model.addProxy({ name: 'Glazing', solid: translate(unit, lo), materialName: 'Glass' }));
  };

  for (let level = 0; level <= storeys; level++) {
    if (level === 0) {
      put(0, 'ground', model.addSlab({ length: LENGTH + 2400, width: WIDTH + 2400, thickness: 150, origin: [-1200, -1200, -150], axisX: X, axisZ: Z, predefinedType: 'BASESLAB', ...concrete }));
      for (const x of xs) for (const y of ys) put(0, 'ground', model.addFooting({ length: 1400, width: 1400, thickness: 500, origin: [x - 700, y - 700, -650], axisX: X, axisZ: Z, predefinedType: 'PAD_FOOTING', ...concrete }));
    } else {
      // The beams of a level are in the storey on top of them. The primary beams stop at the column faces, and
      // the secondary beams at the third points stop at the flanges of the primary beams.
      const z = -SLAB.depth - IPE300.overallDepth / 2, flange = IPE300.overallWidth / 2;
      for (const x of xs) for (const y of ys.slice(0, -1)) put(level, 'steel', model.addBeam({ length: GRID.y - 200, profile: IPE300, origin: [x, y + 100, z], axisX: Y, axisZ: Z, ...steel }));
      for (const y of ys) for (const x of xs.slice(0, -1)) put(level, 'steel', model.addBeam({ length: GRID.x - 200, profile: IPE300, origin: [x + 100, y, z], axisX: X, axisZ: Z, ...steel }));
      for (const y of ys.slice(0, -1)) for (const third of [1, 2]) for (const x of xs.slice(0, -1))
        put(level, 'steel', model.addBeam({ length: GRID.x - 2 * flange, profile: IPE240, origin: [x + flange, y + (third * GRID.y) / 3, -SLAB.depth - IPE240.overallDepth / 2], axisX: X, axisZ: Z, predefinedType: 'JOIST', ...steel }));
      // The roof steel is open: its deck is not in the model yet. Each floor has its slab and its guard rails.
      if (level === storeys) continue;
      put(level, 'slab', model.addSlab({ length: LENGTH + 2 * e, width: WIDTH + 2 * e, thickness: SLAB.depth, origin: [-e, -e, -SLAB.depth], axisX: X, axisZ: Z, predefinedType: 'FLOOR', ...concrete }));
      // Guard rails on the open edges. A railing goes toward its local Y, as a wall does.
      const rail = { height: GUARD.height, thickness: GUARD.bar, axisZ: Z, infill: 'POSTED', predefinedType: 'GUARDRAIL', ...steel } as const;
      const run = (from: P3, axisX: P3, total: number, parts: number): void => {
        for (let k = 0; k < parts; k++) {
          const at = (k * total) / parts;
          put(level, 'rail', model.addRailing({ ...rail, length: total / parts, origin: [from[0] + axisX[0] * at, from[1] + axisX[1] * at, 0], axisX }));
        }
      };
      run([LENGTH + e, -e, 0], Y, WIDTH + 2 * e, ny);
      run([-e, -e, 0], X, LENGTH + 2 * e - GUARD.bar, nx);
      run([LENGTH + e - GUARD.bar, WIDTH + e, 0], [-1, 0, 0], LENGTH + 2 * e - GUARD.bar, nx);
    }
    if (level === storeys) continue;
    // Columns from the floor to the slab over it. The stray column is made, but it is not put in its storey.
    for (const x of xs) for (const y of ys) {
      const lost = stray && level === 0 && x === LENGTH && y === 2 * GRID.y;
      put(level, 'steel', model.addColumn({ height: GRID.storey - SLAB.depth, profile: HE200B, origin: [x, y, 0], axisX: X, axisZ: Z, ...steel }), !lost);
    }
    // A masonry panel with a window in each bay of the back. The glass is a proxy in the opening, clear of the
    // panel that the IFC writer gives each window in the 100 mm of the wall that is farthest from the wall axis.
    const tall = GRID.storey - SLAB.depth - IPE300.overallDepth;
    for (const y of ys.slice(0, -1)) {
      const wall = put(level, 'wall', model.addWall({ length: GRID.y - 200, height: tall, thickness: 200, origin: [100, y + 100, 0], axisX: Y, axisZ: Z, materialName: 'Blockwork', isExternal: true }));
      put(level, 'glass', model.addWindow({ wallLocalId: wall, width: 3000, height: 1500, offsetAlongWall: 1400, offsetFromFloor: 900, materialName: 'Aluminium' }));
      pane(level, [20, y + 1500, 900], [50, y + 4500, 2400]);
    }
  }

  // The view: one compound for each tone. An element that the gate named gets the tone of a finding.
  const view = (flagged: readonly Id[]) => {
    const elements = new Map(model.getAllElements().map((el) => [el.localId, el]));
    const groups = new Map<Tone, Body[]>();
    for (const p of placed) {
      const list = unwrap(placedSolids(elements.get(p.id) as never, { parentFrame: { origin: [0, 0, p.level * GRID.storey], axisX: X, axisZ: Z } })) as Body[];
      owned.push(...list);
      const tone = flagged.includes(p.id) ? 'named' : p.tone;
      groups.set(tone, [...(groups.get(tone) ?? []), ...list]);
    }
    const shown = [...groups].map(([tone, list]) => color(compound(list), COLORS[tone]));
    owned.splice(0).forEach((s) => s.delete());
    return shown;
  };
  return { model, levels, view };
}

export default present(site.view(named), {
  bimTree: site.model.toTreeSummary(),
  ifc: () => checked.bytes,
  // The Files button saves the full report. The console shows the summary.
  files: () => [
    {
      name: 'validation-report.json',
      data: JSON.stringify({ blocked: named.length, ifc4: checked.report.issues, ifc4x3: newer.report.issues }, null, 2),
      mime: 'application/json',
    },
  ],
});

Functions used ​

countBySeverity placedSolids toIfcValidated box compound translate unwrap color present

▶ Run Validated Export in the playground

Released under the Apache 2.0 License.