Skip to content

Declarative Steel Frame ​

Declarative Steel Frame, rendered in the brepjs playground
Families · 272 lines▶ Run it in the playground

A steel frame on two storeys, authored as one tree of families and projected through familiesToBim: pad footings, HE 200 A columns, IPE 300 primary beams, IPE 200 joists and purlins, a deck, guard rails, a gable roof and a switchback stair in an open well. Each member has a GlobalId derived from its key path, so the order of the tree does not change the exported IFC. The kernel takes off the steel and compares it with the catalogue masses. The sections, their masses and the stair figures are published. The grid, the stacked framing, the deck and the roof are a display model.

Code ​

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

ts
import { compound, csg, measureVolume, unwrap } from 'brepjs/quick';
import { color, present } from 'brepjs/playground';
import { family, el, resolve, tTranslate, type Element } from 'brepjs-families';
import { familiesToBim, placedSolids, toIfc } from 'brepjs-bim';

// A steel frame on two storeys, authored as one tree of families and projected through familiesToBim.
// Units are millimetres, Z is up. The sections, their masses and the stair figures are published.
// The grid, the stacked framing, the deck and the roof are a display model.
interface FrameParams {
  bays?: number; // 6 m bays along X: the stair is in the last one
  roofed?: number; // bays that have their roof, from the west end
}

type Vec3 = readonly [number, number, number];
type Shape = Parameters<typeof compound>[0][number];
type Section = { kind: 'I_BEAM'; overallWidth: number; overallDepth: number; flangeThickness: number; webThickness: number };
type Profile = Section | { kind: 'RECTANGULAR'; width: number; height: number };
const X: Vec3 = [1, 0, 0];
const Y: Vec3 = [0, 1, 0];

// ---- published figures
const HEA200: Section = { kind: 'I_BEAM', overallWidth: 200, overallDepth: 190, flangeThickness: 10, webThickness: 6.5 };
const IPE300: Section = { kind: 'I_BEAM', overallWidth: 150, overallDepth: 300, flangeThickness: 10.7, webThickness: 7.1 };
const IPE200: Section = { kind: 'I_BEAM', overallWidth: 100, overallDepth: 200, flangeThickness: 8.5, webThickness: 5.6 };
const KG_PER_M: Record<number, number> = { 190: 42.3, 300: 42.2, 200: 22.4 }; // catalogue masses, by section depth
const STEEL = 7850e-9; // kg for each cubic millimetre
const STAIR = { rise: 170, going: 280, risers: 10, landing: 1200 }; // general access stair: 12 risers at most in a flight

// ---- display model
const BAY = 6000; // along X
const DEPTH = 6000; // along Y
const STOREY = 2 * STAIR.risers * STAIR.rise; // floor to floor
const JOIST = 2000; // spacing of the secondary beams
const DECK = 130;
const PLATE = 20;
const POST: Profile = { kind: 'RECTANGULAR', width: 100, height: 100 };
const GUARD = { height: 1100, thickness: 50 };

// Section outline centred on the member axis. The root radii of the rolled sections are left out.
const outline = (p: Profile): ReadonlyArray<readonly [number, number]> => {
  if (p.kind === 'RECTANGULAR') {
    const [w, h] = [p.width / 2, p.height / 2];
    return [[-w, -h], [w, -h], [w, h], [-w, h]];
  }
  const [w, d, t] = [p.overallWidth / 2, p.overallDepth / 2, p.webThickness / 2];
  const f = d - p.flangeThickness;
  return [[-w, -d], [w, -d], [w, -f], [t, -f], [t, f], [w, f], [w, d], [-w, d], [-w, f], [-t, f], [-t, -f], [-w, -f]];
};

// The families are the registry starters (npx brepjs add column beam slab roof stair), without their
// zod schemas. The prop names feed the IFC specs 1:1, and each render makes the same solid as its spec.
const Column = family<{ height: number; profile: Profile; at: Vec3; materialName: string }>('Column', (p) =>
  el('Geometry', {
    node: csg.extrude(csg.polygon(outline(p.profile).map(([u, v]): [number, number, number] => [u, v, 0])), [0, 0, p.height]),
    transform: [tTranslate(p.at)],
  })
);

// A beam runs from its origin along axisX. The width of its section goes along axisZ x axisX.
const Beam = family<{ length: number; profile: Profile; at: Vec3; axisX: Vec3; predefinedType: 'BEAM' | 'JOIST' | 'PURLIN'; materialName: string }>('Beam', (p) => {
  const a = p.axisX;
  const section = outline(p.profile).map(([u, v]): [number, number, number] => [-u * a[1], u * a[0], v]);
  return el('Geometry', { node: csg.extrude(csg.polygon(section), [a[0] * p.length, a[1] * p.length, 0]), transform: [tTranslate(p.at)] });
});

const Slab = family<{ length: number; width: number; thickness: number; at: Vec3; predefinedType: 'FLOOR' | 'LANDING' | 'BASESLAB'; materialName: string }>(
  'Slab',
  (p) => el('Box', { size: [p.length, p.width, p.thickness], transform: [tTranslate(p.at)] })
);

// The pitch prop makes the IFC roof a gable: a house section in the local YZ plane, swept along X.
const Roof = family<{ length: number; width: number; thickness: number; at: Vec3; predefinedType: 'GABLE_ROOF'; pitch: number; materialName: string }>('Roof', (p) => {
  const ridge = (p.width / 2) * Math.tan((p.pitch * Math.PI) / 180);
  const section = csg.polygon([[0, 0, 0], [0, p.width, 0], [0, p.width, p.thickness], [0, p.width / 2, p.thickness + ridge], [0, 0, p.thickness]]);
  return el('Geometry', { node: csg.extrude(section, [p.length, 0, 0]), transform: [tTranslate(p.at)] });
});

// One stepped solid for each flight. A flight has its own placement, so a switchback is two flights
// that look in opposite directions. The width of a flight goes along axisZ x axisX.
type Flight = { width: number; riserHeight: number; treadLength: number; numberOfRisers: number; origin: Vec3; axisX: Vec3; axisZ: Vec3; materialName: string };
const Stair = family<{ flights: readonly Flight[]; at: Vec3; predefinedType: 'HALF_TURN_STAIR'; materialName: string }>('Stair', (p) => {
  const flight = (f: Flight): csg.IRNode => {
    const at = (run: number, z: number): [number, number, number] => [f.origin[0] + run * f.axisX[0], f.origin[1] + run * f.axisX[1], f.origin[2] + z];
    const pts = [at(0, 0)];
    for (let i = 1; i <= f.numberOfRisers; i++) pts.push(at((i - 1) * f.treadLength, i * f.riserHeight), at(i * f.treadLength, i * f.riserHeight));
    pts.push(at(f.numberOfRisers * f.treadLength, 0));
    return csg.extrude(csg.polygon(pts), [-f.axisX[1] * f.width, f.axisX[0] * f.width, 0]);
  };
  return el('Geometry', { node: csg.compound(p.flights.map(flight)), transform: [tTranslate(p.at)] });
});

const Storey = family<{ elevation: number; items: readonly Element[] }>('Storey', (p) => el('Group', {}, p.items));
const Frame = family<{ storeys: readonly Element[] }>('Frame', (p) => el('Group', {}, p.storeys));

function tree(bays: number, roofed: number) {
  const length = bays * BAY;
  const grid = Array.from({ length: bays + 1 }, (_, i) => [0, DEPTH].map((y): [string, number, number] => [i + '-' + (y > 0 ? 'n' : 's'), i * BAY, y])).flat();
  const lines = Array.from({ length: (bays * BAY) / JOIST + 1 }, (_, k) => k * JOIST);
  const [hw, hd] = [HEA200.overallWidth / 2, HEA200.overallDepth / 2]; // half the column section
  const steel = { materialName: 'Steel S355' };
  // Primary beams run between the column faces. zc is the level of the beam axis in its storey.
  const primaries = (zc: number): Element[] =>
    grid.filter(([, x]) => x < length).map(([key, x, y]) => Beam({ key: 'beam-' + key, length: BAY - 2 * hw, profile: IPE300, at: [x + hw, y, zc], axisX: X, predefinedType: 'BEAM', ...steel }));
  const well = { x0: length - JOIST + 50, x1: length - 50, top: 400 + STAIR.landing + STAIR.risers * STAIR.going }; // the stair strip
  const rail = { ...GUARD, infill: 'POSTED' as const, predefinedType: 'GUARDRAIL' as const, ...steel };
  const g = GUARD.thickness;
  return resolve(
    Frame({
      key: 'frame',
      storeys: [
        Storey({
          key: 'level-0',
          elevation: 0,
          items: [
            Slab({ key: 'ground', length: length + 800, width: DEPTH + 800, thickness: 150, at: [-400, -400, -150], predefinedType: 'BASESLAB', materialName: 'Concrete' }),
            ...grid.flatMap(([key, x, y]) => [
              Footing({ key: 'footing-' + key, length: 1400, width: 1400, thickness: 500, at: [x - 700, y - 700, -650], predefinedType: 'PAD_FOOTING', materialName: 'Concrete' }),
              BasePlate({ key: 'plate-' + key, size: [340, 340, PLATE], at: [x - 170, y - 170, 0], ...steel }),
              Column({ key: 'col-' + key, height: 2 * STOREY - 200 - PLATE, profile: HEA200, at: [x, y, PLATE], ...steel }),
            ]),
            ...stair(well.x0, well.x1),
          ],
        }),
        Storey({
          key: 'level-1',
          elevation: STOREY,
          items: [
            ...primaries(-DECK - 200 - 150),
            // Joists bear on the primary beams. On a column line they stop at the column faces.
            ...lines.map((x, k) =>
              x % BAY === 0
                ? Beam({ key: 'joist-' + k, length: DEPTH - 2 * hd, profile: IPE200, at: [x, hd, -DECK - 100], axisX: Y, predefinedType: 'JOIST', ...steel })
                : Beam({ key: 'joist-' + k, length: DEPTH + 150, profile: IPE200, at: [x, -75, -DECK - 100], axisX: Y, predefinedType: 'JOIST', ...steel })
            ),
            // The deck stops at the stair strip, so the flights are in an open well.
            Slab({ key: 'deck', length: well.x0 + 50, width: DEPTH - 2 * hd, thickness: DECK, at: [-50, hd, -DECK], predefinedType: 'FLOOR', materialName: 'Concrete' }),
            Slab({ key: 'deck-stair', length: JOIST, width: DEPTH - hd - well.top, thickness: DECK, at: [well.x0, well.top, -DECK], predefinedType: 'FLOOR', materialName: 'Concrete' }),
            Railing({ key: 'guard-south', ...rail, length: well.x0 + 50, at: [-50, hd, 0], axisX: X }),
            Railing({ key: 'guard-north', ...rail, length: length + 100, at: [-50, DEPTH - hd - g, 0], axisX: X }),
            Railing({ key: 'guard-west', ...rail, length: DEPTH - 2 * hd - 2 * g, at: [0, hd + g, 0], axisX: Y }),
            Railing({ key: 'guard-well', ...rail, length: well.top - hd - g, at: [well.x0, hd + g, 0], axisX: Y }),
            Railing({ key: 'guard-east', ...rail, length: DEPTH - hd - g - well.top, at: [length + 50, well.top, 0], axisX: Y }),
          ],
        }),
        Storey({
          key: 'level-2',
          elevation: 2 * STOREY,
          items: [
            ...primaries(-200 - 150),
            ...lines.map((x, k) => Beam({ key: 'purlin-' + k, length: DEPTH + 150, profile: IPE200, at: [x, -75, -100], axisX: Y, predefinedType: 'PURLIN', ...steel })),
            ...(roofed > 0
              ? [Roof({ key: 'roof', length: roofed * BAY + 400, width: DEPTH + 750, thickness: 80, at: [-350, -375, 0], predefinedType: 'GABLE_ROOF', pitch: 8, materialName: 'Insulated panel' })]
              : []),
          ],
        }),
      ],
    })
  );
}

function frame({ bays = 2, roofed = 1 }: FrameParams = {}) {
  const count = Math.max(1, Math.round(bays));
  // Project onto a BimModel: storeys, footings, columns, beams, slabs, roof, stair and railings, each
  // with a GlobalId derived from its key path. BasePlate has no IFC mapping and goes out as a proxy.
  const ev = new csg.Evaluator();
  const projected = familiesToBim(tree(count, Math.min(count, Math.max(0, Math.round(roofed)))), {
    project: { name: 'Declarative steel frame', projectId: 'families-structure' },
    siteName: 'Site',
    buildingName: 'Block A',
    proxyEvaluator: ev,
  });
  if (!projected.ok) throw projected.error;
  const { model, idByKeyPath } = projected.value;
  ev[Symbol.dispose]();

  // Draw each element where its IFC placement says, in the frame of its storey, and take off the steel.
  const groups = new Map<string, Shape[]>();
  const temp: Shape[] = [];
  let kernelKg = 0;
  let catalogueKg = 0;
  for (const [path, id] of idByKeyPath) {
    const element = model.getElement(id);
    if (!element) continue;
    const level = Number(path.split('/')[1]?.slice(-1) ?? 0);
    const solids = unwrap(placedSolids(element, { parentFrame: { origin: [0, 0, level * STOREY], axisX: [1, 0, 0], axisZ: [0, 0, 1] } }));
    temp.push(...solids);
    let tone: string = element.category;
    if (element.category === 'BEAM' || element.category === 'COLUMN') {
      const profile = element.spec.profile;
      const kg = profile.kind === 'I_BEAM' ? KG_PER_M[profile.overallDepth] : undefined;
      if (kg !== undefined) {
        kernelKg += solids.reduce((sum, s) => sum + unwrap(measureVolume(s)), 0) * STEEL;
        catalogueKg += (kg * (element.category === 'BEAM' ? element.spec.length : element.spec.height)) / 1000; // length in metres
      }
      if (element.category === 'BEAM' && element.spec.predefinedType !== 'BEAM') tone = 'JOIST';
    }
    if (solids.length > 0) groups.set(tone, [...(groups.get(tone) ?? []), ...solids]);
  }
  const sample = 'frame/level-1/joist-1';
  const sampleId = idByKeyPath.get(sample);
  const guid = sampleId === undefined ? undefined : model.getElement(sampleId)?.guid;
  console.log(model.getColumns().length + ' columns, ' + model.getBeams().length + ' beams, ' + model.getSlabs().length + ' slabs, ' + model.getRailings().length + ' railings, ' + model.getStairs().length + ' stair, ' + model.getRoofs().length + ' roof');
  console.log(sample + ' has the GlobalId ' + guid + ': it comes from the key path, not from the order');
  console.log('Steel: ' + (kernelKg / 1000).toFixed(2) + ' t from the kernel volumes. The catalogue masses give ' + (catalogueKg / 1000).toFixed(2) + ' t: the model has no root radii.');

  const bodies = [...groups.entries()].map(([tone, shapes]) => color(compound(shapes), COLORS[tone] ?? '#c3c9d1'));
  temp.forEach((s) => s.delete()); // compound() copied the solids
  return { model, bodies };
}

const COLORS: Record<string, string> = {
  COLUMN: '#ff5a1f',
  BEAM: '#ff5a1f',
  JOIST: '#c3c9d1',
  SLAB: '#f5f2ea',
  FOOTING: '#b9b4a8',
  STAIR: '#d8d4c8',
  RAILING: '#12b5b0',
  ROOF: '#48515c',
  PROXY: '#48515c',
};

// ---- the rest of the families

const Footing = family<{ length: number; width: number; thickness: number; at: Vec3; predefinedType: 'PAD_FOOTING'; materialName: string }>('Footing', (p) =>
  el('Box', { size: [p.length, p.width, p.thickness], transform: [tTranslate(p.at)] })
);
const BasePlate = family<{ size: Vec3; at: Vec3; materialName: string }>('BasePlate', (p) => el('Box', { size: p.size, transform: [tTranslate(p.at)] }));

// A guard rail runs from its origin along axisX, with its thickness along axisZ x axisX.
const Railing = family<{ length: number; height: number; thickness: number; at: Vec3; axisX: Vec3; infill: 'POSTED'; predefinedType: 'GUARDRAIL'; materialName: string }>('Railing', (p) => {
  const a = p.axisX;
  const pt = (u: number, v: number): [number, number, number] => [u * a[0] - v * a[1], u * a[1] + v * a[0], 0];
  const base = csg.polygon([pt(0, 0), pt(p.length, 0), pt(p.length, p.thickness), pt(0, p.thickness)]);
  return el('Geometry', { node: csg.extrude(base, [0, 0, p.height]), transform: [tTranslate(p.at)] });
});

// Switchback stair between two joists: up to a landing on two posts, then back to the deck.
function stair(x0: number, x1: number): Element[] {
  const half = STAIR.risers * STAIR.rise;
  const y0 = 400; // near edge of the landing
  const y1 = y0 + STAIR.landing;
  const run = STAIR.risers * STAIR.going;
  const flight = { width: (x1 - x0 - 100) / 2, riserHeight: STAIR.rise, treadLength: STAIR.going, numberOfRisers: STAIR.risers, axisZ: [0, 0, 1] as Vec3, materialName: 'Concrete' };
  const post = (key: string, x: number): Element => Column({ key, height: half - 150, profile: POST, at: [x, y0 + 50, 0], materialName: 'Steel S355' });
  return [
    Stair({
      key: 'stair',
      at: [0, 0, 0],
      predefinedType: 'HALF_TURN_STAIR',
      materialName: 'Concrete',
      flights: [
        { ...flight, origin: [x0, y1 + run, 0], axisX: [0, -1, 0] },
        { ...flight, origin: [x1, y1, half], axisX: [0, 1, 0] },
      ],
    }),
    Slab({ key: 'landing', length: x1 - x0, width: STAIR.landing, thickness: 150, at: [x0, y0, half - 150], predefinedType: 'LANDING', materialName: 'Concrete' }),
    post('landing-post-w', x0 + 50),
    post('landing-post-e', x1 - 50),
  ];
}

const built = frame();

export default present(built.bodies, {
  bimTree: built.model.toTreeSummary(),
  ifc: async () => {
    const r = await toIfc(built.model, { applicationName: 'brepjs playground', applicationVersion: '1.0' });
    if (!r.ok) throw r.error;
    return r.value;
  },
});

Functions used ​

compound csg measureVolume unwrap color present family el resolve tTranslate familiesToBim placedSolids toIfc

▶ Run Declarative Steel Frame in the playground

Released under the Apache 2.0 License.