Skip to content

Switchback Stair ​

Switchback Stair, rendered in the brepjs playground
BIM · 186 lines▶ Run it in the playground

A stair core as a BIM model: a half-turn stair for each storey, with two flights that go opposite ways, a half landing at the turn, a wall with a door at each floor, railings on the landings and a balustrade along each flight. The stair is an IfcStair with no solid of its own, and placedSolids() gives its flights. The rise, the going, the flight length, the widths and the heights of handrail and guarding follow the limits of a building code for a general access stair, and the kernel measures the concrete. The wall, the doors and the shape of the balustrade are a display model.

Code ​

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

ts
import { BimModel, placedSolids, toIfc } from 'brepjs-bim';
import { box, clone, compound, cutAll, cylinder, extrude, fuseAll, getSolids, measureVolume, polygon, translate, unwrap } from 'brepjs/quick';
import { color, present } from 'brepjs/playground';

// Stair core as a BIM model: a half-turn stair for each storey. Two flights go opposite ways in two lanes with a
// half landing at the turn. The stair is an IfcStair with no solid of its own: placedSolids() gives its flights.
// The rise, the going, the flight length, the widths and the heights of handrail and guarding are the limits of
// a building code for a general access stair. The wall, the door and the shape of the balustrade are a display
// model. Units are millimetres. The flights go along Y, and the open side of the core looks to +X.
interface StairParams {
  storeys?: number; // 1 to 4
  storeyHeight?: number; // floor to floor, 2400 to 4080
  width?: number; // width of a flight
}

// ---- published figures (general access stair)
const RISE = { min: 150, max: 170 };
const GOING = 300; // 250 to 400 is allowed
const COMFORT = { min: 550, max: 700 }; // twice the rise plus the going
const MAX_RISERS = 12; // in one flight
const MIN_WIDTH = 1200; // between walls. A landing is as long as the flight is wide, or longer
const HANDRAIL = 950; // above the pitch line: 900 to 1000
const GUARD = 1100; // guarding of a landing

// ---- display model
const WELL = 200, WAIST = 150, SLAB = 200, WALL = 200;
const RAIL = { post: 16, top: 22, mid: 12, inset: 60, guard: 50 };
const DOOR = { width: 1010, height: 2110 };
const COLORS = { stair: '#d8d4c8', landing: '#c2bdaf', wall: '#a8a294', rail: '#12b5b0', frame: '#f5f2ea', leaf: '#ff5a1f', ground: '#8d887c' };
type Tone = keyof typeof COLORS;
type P3 = [number, number, number];
type Body = ReturnType<typeof box>;
type Id = Extract<ReturnType<BimModel['addStair']>, { ok: true }>['value']; // the id of an element in the model
const X: P3 = [1, 0, 0], Y: P3 = [0, 1, 0], Z: P3 = [0, 0, 1], BACK: P3 = [0, -1, 0];

function stairCore({ storeys = 2, storeyHeight = 3600, width = MIN_WIDTH }: StairParams = {}) {
  const model = new BimModel(), owned: { delete(): void }[] = [];
  const own = <T extends { delete(): void }>(s: T): T => { owned.push(s); return s; };
  try {
    model.init({ name: 'Stair core' });
    const site = unwrap(model.addSite({ name: 'Site' }));
    const building = unwrap(model.addBuilding({ name: 'Building' }));
    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 tones = new Map<Id, Tone>();
    const put = (tone: Tone, made: ReturnType<BimModel['addStair']>): Id => {
      const id = unwrap(made);
      model.placeIn(id, storey);
      tones.set(id, tone);
      return id;
    };
    const concrete = { materialName: 'Concrete' };

    // The storey height gives the steps: an even number of equal risers, each 170 mm or less, in two flights.
    const levels = Math.min(Math.max(Math.round(storeys), 1), 4), H = Math.min(Math.max(storeyHeight, 2400), 2 * MAX_RISERS * RISE.max);
    const risers = 2 * Math.ceil(H / 2 / RISE.max), rise = H / risers, n = risers / 2, run = n * GOING;
    const W = Math.max(width, MIN_WIDTH), across = 2 * W + WELL, foot = WAIST / Math.cos(Math.atan2(rise, GOING)) * GOING / rise;
    const landing = W + foot, far = run + landing; // a landing also takes the foot of the flight that starts on it

    // The stair of one storey. A flight is a stepped solid that goes up along its axisX, with its width on the left.
    // Flight 1 goes up along +Y in the lane on the +X side. Flight 2 comes back along -Y in the other lane.
    const flight = { width: W, riserHeight: rise, treadLength: GOING, numberOfRisers: n, axisZ: Z, ...concrete };
    const stairs: Id[] = [];
    for (let level = 0; level < levels; level++) {
      const z = level * H;
      stairs.push(put('stair', model.addStair({
        name: 'Stair ' + (level + 1),
        predefinedType: 'HALF_TURN_STAIR',
        flights: [{ ...flight, origin: [across, 0, z], axisX: Y }, { ...flight, origin: [0, run + foot, z + H / 2], axisX: BACK }],
        ...concrete,
      })));
      // The half landing at the turn. Both flights meet it.
      put('landing', model.addSlab({ length: across, width: landing, thickness: SLAB, origin: [0, run, z + H / 2 - SLAB], axisX: X, axisZ: Z, predefinedType: 'LANDING', ...concrete }));
      // The landing at the floor over it, where flight 2 arrives.
      put('landing', model.addSlab({ length: across, width: landing, thickness: SLAB, origin: [0, foot - landing, z + H - SLAB], axisX: X, axisZ: Z, predefinedType: 'LANDING', ...concrete }));
    }

    const kit = { model, put, own, levels, H, W, across, run, foot, landing, far, rise, n };
    enclose(kit);
    guard(kit);

    // The view. placedSolids() gives the two flights of each stair in their places. A flight in the model is solid
    // down to its foot. The view cuts the waist under each flight, as a concrete stair has it.
    const groups: Record<Tone, Body[]> = { stair: [], landing: [], wall: [], rail: [], frame: [], leaf: [], ground: [] };
    const drop = WAIST / Math.cos(Math.atan2(rise, GOING));
    let concreteM3 = 0;
    for (const el of model.getAllElements()) {
      const tone = tones.get(el.localId);
      if (!tone) continue;
      const placed = unwrap(placedSolids(el)).map((body) => own(body));
      if (tone === 'stair' || tone === 'landing') concreteM3 += placed.reduce((sum, body) => sum + unwrap(measureVolume(body)), 0) / 1e9;
      const level = stairs.indexOf(el.localId);
      if (level < 0) { groups[tone].push(...placed); continue; }
      placed.forEach((body, i) => {
        const z = level * H + i * H / 2, y0 = i === 0 ? 0 : run + foot, way = i === 0 ? 1 : -1, x0 = i === 0 ? W + WELL : 0;
        const under = own(unwrap(polygon([[x0 - 1, y0 + way * foot, z - 1], [x0 - 1, y0 + way * run + way, z - 1], [x0 - 1, y0 + way * run + way, z + n * rise - drop], [x0 - 1, y0 + way * foot, z]])));
        groups.stair.push(own(unwrap(cutAll(body, [own(unwrap(extrude(under, [W + 2, 0, 0])))]))));
      });
    }
    const shown = (Object.keys(COLORS) as Tone[]).filter((tone) => groups[tone].length > 0).map((tone) => color(compound(groups[tone]), COLORS[tone]));
    return { model, shown, concreteM3, risers, rise, comfort: 2 * rise + GOING, pitch: Math.atan2(rise, GOING) * 180 / Math.PI };
  } catch (error) {
    model[Symbol.dispose]();
    throw error;
  } finally {
    owned.splice(0).forEach((body) => body.delete());
  }
}

type Kit = {
  model: BimModel; put: (tone: Tone, made: ReturnType<BimModel['addStair']>) => Id; own: <T extends { delete(): void }>(s: T) => T;
  levels: number; H: number; W: number; across: number; run: number; foot: number; landing: number; far: number; rise: number; n: number;
};

// The core: a ground slab, the wall behind the stair and a door from each floor landing. The other three sides
// stay open in this model.
function enclose({ model, put, own, levels, H, across, foot, landing, far }: Kit): void {
  const one = (shape: Parameters<typeof getSolids>[0]): Body => unwrap(clone(getSolids(own(shape))[0]!)) as Body;
  const y0 = foot - landing, length = far - y0;
  put('ground', model.addSlab({ length: across + WALL + 300, width: length + 600, thickness: 250, origin: [-WALL, y0 - 300, -250], axisX: X, axisZ: Z, predefinedType: 'BASESLAB', materialName: 'Concrete' }));
  // The wall is along +Y, so its thickness goes to -X.
  const wall = put('wall', model.addWall({ length, height: levels * H + DOOR.height + 300, thickness: WALL, origin: [0, y0, 0], axisX: Y, axisZ: Z, materialName: 'Concrete', loadBearing: true }));
  for (let level = 0; level <= levels; level++) {
    const z = level * H, at = 150, d0 = -WALL + 40, d1 = d0 + 100, jamb = 45, head = 45;
    put('frame', model.addDoor({ wallLocalId: wall, width: DOOR.width, height: DOOR.height, offsetAlongWall: at, offsetFromFloor: z, materialName: 'Steel', fireRating: 'FD30' }));
    const block = (u0: number, u1: number, x0: number, x1: number, z0: number, z1: number): Body => box(x1 - x0, u1 - u0, z1 - z0, { at: [(x0 + x1) / 2, y0 + at + (u0 + u1) / 2, z + (z0 + z1) / 2] });
    const w = DOOR.width, h = DOOR.height;
    put('frame', model.addProxy({ name: 'Door frame', solid: one(unwrap(fuseAll([own(block(0, jamb, d0, d1, 0, h)), own(block(w - jamb, w, d0, d1, 0, h)), own(block(jamb, w - jamb, d0, d1, h - head, h))]))), materialName: 'Steel' }));
    const light = own(block(w - jamb - 330, w - jamb - 130, d1 - 60, d1 - 10, 1250, 1750));
    put('leaf', model.addProxy({ name: 'Fire door leaf', solid: one(unwrap(cutAll(own(block(jamb + 3, w - jamb - 3, d1 - 55, d1 - 11, 8, h - head - 3)), [light]))), materialName: 'Timber, painted' }));
  }
}

// Guarding: an IfcRailing on the open edges of each landing, and a balustrade with a handrail along each flight.
// A railing is level, so the balustrade of a flight is a proxy.
function guard({ model, put, own, levels, H, W, across, run, foot, landing, far, rise, n }: Kit): void {
  const one = (shape: Parameters<typeof getSolids>[0]): Body => unwrap(clone(getSolids(own(shape))[0]!)) as Body;
  const t = RAIL.guard;
  // A railing from one point to the next at level z. Its thickness is on the left of its direction.
  const rail = (from: [number, number], to: [number, number], z: number): void => {
    const length = Math.hypot(to[0] - from[0], to[1] - from[1]);
    put('rail', model.addRailing({ length, height: GUARD, thickness: t, origin: [from[0], from[1], z], axisX: [(to[0] - from[0]) / length, (to[1] - from[1]) / length, 0], axisZ: Z, predefinedType: 'GUARDRAIL', infill: 'POSTED', materialName: 'Steel' }));
  };
  // The balustrade of one flight: posts on the treads, a handrail and a mid rail. u goes up the flight, and the
  // pitch line joins the nosings.
  const balustrade = (place: (u: number, z: number) => P3, way: 1 | -1): Body => {
    const slope = rise / GOING, pitch = (u: number): number => rise + u * slope, axis: P3 = [0, way * GOING, rise];
    const bar = (radius: number, over: number): Body => {
      const from = place(GOING / 2 - 60, pitch(GOING / 2 - 60) + over), reach = (n - 1) * GOING + 120;
      return own(cylinder(radius, reach * Math.hypot(1, slope), { at: from, axis }));
    };
    const posts = Array.from({ length: n }, (_, i) => i).filter((i) => i % 2 === 0 || i === n - 1).map((i) => {
      const u = (i + 0.5) * GOING, top = pitch(u) + HANDRAIL - RAIL.top;
      return own(cylinder(RAIL.post, top - (i + 1) * rise, { at: place(u, (i + 1) * rise) }));
    });
    return one(unwrap(fuseAll([bar(RAIL.top, HANDRAIL - RAIL.top), bar(RAIL.mid, HANDRAIL / 2), ...posts])));
  };
  const first = own(balustrade((u, z) => [across - RAIL.inset, u, z], 1));
  const second = own(balustrade((u, z) => [W - RAIL.inset, run + foot - u, H / 2 + z], -1));
  const y0 = foot - landing;
  for (let level = 0; level < levels; level++) {
    const z = level * H;
    put('rail', model.addProxy({ name: 'Balustrade', solid: translate(first, [0, 0, z]), materialName: 'Steel' }));
    put('rail', model.addProxy({ name: 'Balustrade', solid: translate(second, [0, 0, z]), materialName: 'Steel' }));
    // Half landing: the +X edge and the far end.
    rail([across, run], [across, far - t], z + H / 2);
    rail([across, far], [0, far], z + H / 2);
    // Floor landing over it: the near end and the +X edge. On the top floor the edge over the flights is closed too.
    const top = level === levels - 1;
    rail([0, y0], [across, y0], z + H);
    rail([across, y0 + t], [across, top ? foot - t : 0], z + H);
    if (top) rail([across, foot], [W, foot], z + H);
  }
}

// Try stairCore({ storeys: 3 }), stairCore({ storeyHeight: 3000 }) or stairCore({ width: 1500 }).
const scene = stairCore();
console.log(scene.risers + ' risers of ' + scene.rise.toFixed(1) + ' mm for each storey, going ' + GOING + ' mm, pitch ' + scene.pitch.toFixed(1) + ' degrees. 2R + G = ' + scene.comfort.toFixed(0) + ' mm, and ' + COMFORT.min + ' to ' + COMFORT.max + ' is allowed. The kernel measures ' + scene.concreteM3.toFixed(2) + ' m3 of concrete in the flights and the landings of the model.');
// The tree panel shows the IFC structure. The IFC button saves the model as a file.
export default present(scene.shown, {
  bimTree: scene.model.toTreeSummary(),
  ifc: async () => unwrap(await toIfc(scene.model, { applicationName: 'brepjs playground', applicationVersion: '1.0' })),
});

Functions used ​

placedSolids toIfc box clone compound cutAll cylinder extrude fuseAll getSolids measureVolume polygon translate unwrap color present

▶ Run Switchback Stair in the playground

Released under the Apache 2.0 License.