Skip to content

Room Schedule and Zones ​

Room Schedule and Zones, rendered in the brepjs playground
BIM · 248 lines▶ Run it in the playground

The room programme of a two-bedroom flat. Each room is an IfcSpace with a name, an area and a volume, and two sets of zones group the spaces: one by use and one by ventilation. The console prints the room schedule with the total of each zone, and compares the areas that the kernel measures with the published space standard for a flat of this size. The plan, the walls and the furniture 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, compound, cut, cylinder, intersect, measureVolume, sphere, unwrap } from 'brepjs/quick';
import { color, present } from 'brepjs/playground';

// The room programme of a two-bedroom flat. Each room is an IfcSpace with a name, an area and a volume,
// and zones group the spaces. A space can be in more than one zone: here one set by use and one by air.
// Units are millimetres. The origin is the inside corner of the flat, X is east, Y is north, Z is up.
interface FlatParams {
  zoneBy?: 'use' | 'air'; // the set of zones that colours the floors
  volumes?: boolean; // show each space as its full volume, without the furniture
  cutHeight?: number; // where the view cuts the walls. 2500 shows them whole
}

// ---- published figures: the space standard for a flat of two bedrooms and four persons on one storey
const STANDARD = { gia: 70, storage: 2, doubleBedroom: 11.5, bedroomWidth: 2.75, ceiling: 2.3 }; // m2 and m

// ---- display model
const H = 2500; // floor to ceiling
// name, x, y, length, depth, zone by use, zone by air (supply to the dry rooms, extract from the wet rooms)
const PROGRAMME: [string, number, number, number, number, string, string][] = [
  ['Bedroom 1', 0, 3700, 3600, 3500, 'Sleeping', 'Supply'],
  ['Bedroom 2', 0, 0, 3600, 3600, 'Sleeping', 'Supply'],
  ['Living and kitchen', 4900, 0, 5100, 5300, 'Living', 'Supply'],
  ['Hall', 3700, 2000, 1100, 5200, 'Circulation', 'Transfer'],
  ['Store', 3700, 0, 1100, 1900, 'Service', 'Transfer'],
  ['Bathroom', 4900, 5400, 2300, 1800, 'Service', 'Extract'],
  ['Utility', 7300, 5400, 2700, 1800, 'Service', 'Extract'],
  ['Balcony', 4920, -1780, 5060, 1480, 'Outdoor', '-'], // inside its guard, and in no air zone
];
const FLAT = { x: 10000, y: 7200, wall: 300, partition: 100, slab: 250 };
const BALCONY = { x: 4900, y: -1800, length: 5100, width: 1500 };
const TINT: Record<string, string> = {
  Sleeping: '#6f8fd8', Living: '#12b5b0', Circulation: '#c3c9d1', Service: '#d3a956', Outdoor: '#79b46f',
  Supply: '#12b5b0', Extract: '#ff5a1f', Transfer: '#c3c9d1', '-': '#79b46f',
};
const FLOOR = 12; // the view shows the lowest 12 mm of each space as a coloured floor. The furniture stands on it

type P3 = [number, number, number];
type Body = ReturnType<typeof box>;
type Id = Extract<ReturnType<BimModel['addSlab']>, { ok: true }>['value'];
const X: P3 = [1, 0, 0], Y: P3 = [0, 1, 0], Z: P3 = [0, 0, 1];

// The model stays alive for the IFC file. Each temporary solid is freed, also when the build throws.
function flat({ zoneBy = 'use', volumes = false, cutHeight = 1200 }: FlatParams = {}) {
  const model = new BimModel(), owned: Body[] = [];
  const own = <T extends Body>(s: T): T => { owned.push(s); return s; };
  try {
    model.init({ name: 'Space programme', projectId: 'spaces-demo' });
    const site = unwrap(model.addSite({ name: 'Site' }));
    const building = unwrap(model.addBuilding({ name: 'Block A' }));
    const storey = unwrap(model.addStorey({ name: 'Level 1', elevation: 0 }));
    const project = model.getProject();
    if (project) model.aggregate(project.localId, site);
    model.aggregate(site, building);
    model.aggregate(building, storey);

    // ---- the lesson. One space for each room of the programme.
    const spaceIds = PROGRAMME.map(([name, x, y, length, width, use]) => {
      const space = unwrap(model.addSpace({ name, length, width, height: H, origin: [x, y, 0], axisX: X, axisZ: Z, materialName: 'Air', predefinedType: use === 'Outdoor' ? 'EXTERNAL' : 'INTERNAL' }));
      model.placeIn(space, storey);
      return space;
    });
    // Zones cut across the plan. assignToGroup is the IfcRelAssignsToGroup relationship, and each space gets two.
    for (const column of [5, 6] as const) for (const name of new Set(PROGRAMME.map((p) => p[column]))) {
      if (name === '-') continue;
      const zone = unwrap(model.addZone({ name, longName: name + (column === 5 ? ' rooms' : ' air'), objectType: column === 5 ? 'Use' : 'Ventilation' }));
      model.assignToGroup(zone, spaceIds.filter((_, i) => PROGRAMME[i]![column] === name));
    }
    // One more space for the gross internal area: all of the floor inside the outer walls.
    const gross = unwrap(model.addSpace({ name: 'Flat 1', longName: 'Gross internal area', length: FLAT.x, width: FLAT.y, height: H, origin: [0, 0, 0], axisX: X, axisZ: Z, materialName: 'Air', predefinedType: 'GFA' }));
    model.placeIn(gross, storey);

    // The schedule. The kernel measures the volume of each space, and the area is the volume / the height.
    // Find the row of a space by its name: a schedule that depends on the order of the model is easy to break.
    const byName = new Map(PROGRAMME.map((p) => [p[0], p]));
    const floors: Record<string, Body[]> = {}, area = new Map<string, number>(), zoneArea = new Map<string, number>();
    console.log('room                use          air        area m2  volume m3');
    for (const el of model.getSpaces()) {
      const [solid] = unwrap(placedSolids(el));
      if (!solid) continue;
      own(solid);
      const m3 = unwrap(measureVolume(solid)) / 1e9, m2 = m3 / (H / 1000), row = byName.get(el.spec.name);
      area.set(el.spec.name, m2);
      if (!row) continue; // the gross internal area is not a room
      console.log(row[0].padEnd(20) + row[5].padEnd(13) + row[6].padEnd(10) + m2.toFixed(1).padStart(8) + m3.toFixed(1).padStart(11));
      const zone = zoneBy === 'use' ? row[5] : row[6];
      if (zone !== '-') zoneArea.set(zone, (zoneArea.get(zone) ?? 0) + m2);
      (floors[zone] ??= []).push(volumes ? solid : own(unwrap(intersect(solid, own(box(row[3], row[4], FLOOR, { at: [row[1] + row[3] / 2, row[2] + row[4] / 2, FLOOR / 2] }))))));
    }
    const total = [...zoneArea.values()].reduce((a, b) => a + b, 0);
    for (const [zone, m2] of zoneArea) console.log('  ' + zone.padEnd(12) + m2.toFixed(1).padStart(7) + ' m2  (' + ((m2 / total) * 100).toFixed(0) + '%)');
    const check = (label: string, value: number, least: number, unit: string): void => console.log('  ' + label.padEnd(24) + value.toFixed(1).padStart(6) + unit + ' (standard: ' + least + unit + ' or more) ' + (value >= least ? 'ok' : 'SHORT'));
    check('gross internal area', area.get('Flat 1') ?? 0, STANDARD.gia, ' m2');
    check('Bedroom 1', area.get('Bedroom 1') ?? 0, STANDARD.doubleBedroom, ' m2');
    check('Bedroom 2', area.get('Bedroom 2') ?? 0, STANDARD.doubleBedroom, ' m2');
    check('Bedroom 1 width', PROGRAMME[0]![4] / 1000, STANDARD.bedroomWidth, ' m');
    check('built-in storage', area.get('Store') ?? 0, STANDARD.storage, ' m2');
    check('ceiling height', H / 1000, STANDARD.ceiling, ' m');

    // ---- the view: the coloured spaces, the building around them, and the furniture.
    const shown = Object.entries(floors).map(([zone, list]) => color(compound(list), TINT[zone] ?? '#c3c9d1'));
    const holes = shell(model, storey);
    const keep = own(box(40000, 40000, cutHeight + FLAT.slab, { at: [0, 0, (cutHeight - FLAT.slab) / 2] }));
    const built: Record<string, Body[]> = {};
    for (const el of model.getAllElements()) {
      if (el.category !== 'WALL' && el.category !== 'SLAB' && el.category !== 'RAILING') continue;
      for (const s of unwrap(placedSolids(el))) (built[el.category] ??= []).push(el.category === 'WALL' && cutHeight < H ? own(unwrap(intersect(own(s), keep))) : own(s));
    }
    shown.push(color(compound(built.WALL ?? []), '#f5f2ea'), color(compound(built.SLAB ?? []), '#b9b3a6'), color(compound(built.RAILING ?? []), '#9fc3d4'));
    shown.push(...openings(holes, Math.min(cutHeight, H), !volumes, own));
    if (!volumes) shown.push(...furniture(Math.min(cutHeight, H) - FLOOR, own));
    return { model, shown };
  } catch (error) {
    model[Symbol.dispose]();
    throw error;
  } finally {
    owned.splice(0).forEach((s) => s.delete());
  }
}

// Show the floors by use. Try flat({ zoneBy: 'air' }), flat({ volumes: true }), or flat({ cutHeight: 2500 }).
const home = flat();
export default present(home.shown, {
  bimTree: home.model.toTreeSummary(),
  ifc: async () => unwrap(await toIfc(home.model, { applicationName: 'brepjs playground', applicationVersion: '1.0' })),
});

// ---- the building: a slab, the outer walls, the partitions, a balcony, and a door or a window in each opening.
type Hole = { along: 'x' | 'y'; x0: number; y0: number; t: number; at: number; width: number; sill: number; height: number; leaf: -1 | 0 | 1; hinge: 0 | 1 };
function shell(model: BimModel, storey: Id): Hole[] {
  const holes: Hole[] = [];
  const put = (made: ReturnType<BimModel['addSlab']>) => { const id = unwrap(made); model.placeIn(id, storey); return id; };
  // A wall from its rectangle on the plan. Each opening: [distance along, width, sill, height, side of the leaf, hinge end].
  const wall = (x0: number, y0: number, x1: number, y1: number, materialName: string, cuts: [number, number, number, number, -1 | 0 | 1, 0 | 1][] = []): void => {
    const along = x1 - x0 > y1 - y0 ? 'x' : 'y', t = along === 'x' ? y1 - y0 : x1 - x0, isExternal = t === FLAT.wall;
    // The thickness of a wall is on the left of its direction: +Y for a wall along X, -X for a wall along Y.
    const id = put(model.addWall({ length: along === 'x' ? x1 - x0 : y1 - y0, height: H, thickness: t, origin: along === 'x' ? [x0, y0, 0] : [x1, y0, 0], axisX: along === 'x' ? X : Y, axisZ: Z, materialName, isExternal }));
    for (const [at, width, sill, height, leaf, hinge] of cuts) {
      const hole = { wallLocalId: id, width, height, offsetAlongWall: at - (along === 'x' ? x0 : y0), offsetFromFloor: sill, isExternal };
      put(sill > 0 ? model.addWindow({ ...hole, materialName: 'Glazing' }) : model.addDoor({ ...hole, materialName: isExternal && leaf === 0 ? 'Glazing' : 'Timber door' }));
      holes.push({ along, x0, y0, t, at, width, sill, height, leaf, hinge });
    }
  };
  const { x: LX, y: LY, wall: E, partition: P } = FLAT, outer = 'Brick cavity wall', inner = 'Plasterboard partition';
  put(model.addSlab({ length: LX + 2 * E, width: LY + 2 * E, thickness: FLAT.slab, origin: [-E, -E, -FLAT.slab], axisX: X, axisZ: Z, predefinedType: 'FLOOR', materialName: 'Concrete' }));
  wall(-E, -E, LX + E, 0, outer, [[900, 1800, 900, 1200, 0, 0], [5600, 2400, 0, 2100, 0, 0], [8500, 1200, 900, 1200, 0, 0]]); // south: the facade
  wall(-E, LY, LX + E, LY + E, outer, [[3745, 1010, 0, 2100, -1, 0]]); // north: the access deck and the front door
  wall(-E, 0, 0, LY, outer, [[1000, 1200, 900, 1200, 0, 0], [4500, 1800, 900, 1200, 0, 0]]); // west: the end of the block
  wall(LX, 0, LX + E, LY, outer); // east: the party wall
  wall(3600, 0, 3700, LY, inner, [[2500, 900, 0, 2040, -1, 0], [3900, 900, 0, 2040, -1, 0]]);
  wall(4800, 0, 4900, LY, inner, [[2600, 900, 0, 2040, 1, 1], [5700, 900, 0, 2040, 1, 0]]);
  wall(0, 3600, 3600, 3600 + P, inner);
  wall(3700, 1900, 4800, 1900 + P, inner, [[3800, 900, 0, 2040, 1, 0]]);
  wall(4900, 5300, LX, 5300 + P, inner, [[8000, 900, 0, 2040, 1, 0]]);
  wall(7200, 5400, 7300, LY, inner);
  // The balcony: a slab with a glass guard on three sides.
  const { x: bx, y: by, length: bl, width: bw } = BALCONY;
  put(model.addSlab({ length: bl, width: bw, thickness: 200, origin: [bx, by, -200], axisX: X, axisZ: Z, predefinedType: 'FLOOR', materialName: 'Concrete' }));
  const guard = { height: 1100, thickness: 20, axisZ: Z, predefinedType: 'BALUSTRADE', materialName: 'Laminated glass' } as const;
  put(model.addRailing({ ...guard, length: bl, origin: [bx, by, 0], axisX: X }));
  put(model.addRailing({ ...guard, length: bw - 20, origin: [bx + 20, by + 20, 0], axisX: Y }));
  put(model.addRailing({ ...guard, length: bw - 20, origin: [bx + bl, by + 20, 0], axisX: Y }));
  return holes;
}

// ---- display only: a frame and glass in each window, and an open leaf at each door.
function openings(holes: Hole[], top: number, open: boolean, own: <T extends Body>(s: T) => T) {
  const frames: Body[] = [], panes: Body[] = [], leaves: Body[] = [];
  // A box in the frame of a wall: u along it, v across it from its first face, z up.
  const piece = (h: Hole, u0: number, u1: number, v0: number, v1: number, z0: number, z1: number): Body =>
    own(h.along === 'x' ? box(u1 - u0, v1 - v0, z1 - z0, { at: [(u0 + u1) / 2, h.y0 + (v0 + v1) / 2, (z0 + z1) / 2] }) : box(v1 - v0, u1 - u0, z1 - z0, { at: [h.x0 + (v0 + v1) / 2, (u0 + u1) / 2, (z0 + z1) / 2] }));
  for (const h of holes) {
    const a = h.at, b = h.at + h.width, z1 = Math.min(h.sill + h.height, top), m = 50, mid = h.t / 2;
    if (z1 <= h.sill) continue;
    if (h.leaf !== 0) {
      // A door leaf, open at 90 degrees on the side of its room.
      const u = h.hinge === 0 ? a : b - 40, v0 = h.leaf > 0 ? h.t : -h.width;
      if (open) leaves.push(piece(h, u, u + 40, v0, v0 + h.width, FLOOR, z1));
      continue;
    }
    frames.push(piece(h, a, a + m, mid - 35, mid + 35, h.sill, z1), piece(h, b - m, b, mid - 35, mid + 35, h.sill, z1));
    if (h.sill > 0) frames.push(piece(h, a + m, b - m, mid - 35, mid + 35, h.sill, h.sill + m));
    const lights = Math.max(1, Math.round(h.width / 900)), pitch = (h.width - m) / lights, low = h.sill > 0 ? h.sill + m : h.sill;
    for (let i = 0; i < lights; i++) {
      if (i > 0) frames.push(piece(h, a + i * pitch, a + i * pitch + m, mid - 35, mid + 35, low, z1));
      panes.push(piece(h, a + m + i * pitch, a + (i + 1) * pitch, mid - 12, mid + 12, low, z1));
    }
  }
  const parts: [Body[], string][] = [[frames, '#48515c'], [panes, '#9fc3d4'], [leaves, '#e3dfd3']];
  return parts.filter(([list]) => list.length > 0).map(([list, tint]) => color(compound(list), tint));
}

// ---- display only: the furniture, cut at the same height as the walls.
// Each box is: tone, then x0, y0, z0, x1, y1, z1 above the floor.
function furniture(top: number, own: <T extends Body>(s: T) => T) {
  const tones: Record<string, Body[]> = { white: [], wood: [], dark: [], accent: [], steel: [], green: [] };
  const b = (tone: string, x0: number, y0: number, z0: number, x1: number, y1: number, z1: number): void => {
    const z = Math.min(z1, top);
    if (z > z0) tones[tone]!.push(own(box(x1 - x0, y1 - y0, z - z0, { at: [(x0 + x1) / 2, (y0 + y1) / 2, FLOOR + (z0 + z) / 2] })));
  };
  const round = (tone: string, x: number, y: number, z0: number, r: number, h: number): void => {
    if (top > z0) tones[tone]!.push(own(cylinder(r, Math.min(h, top - z0), { at: [x, y, FLOOR + z0] })));
  };
  // A bed with its head at y1: a base, a mattress, a cover and the pillows.
  const bed = (x0: number, y0: number, x1: number, y1: number): void => {
    b('wood', x0, y0, 0, x1, y1 - 50, 280); b('wood', x0, y1 - 50, 0, x1, y1, 950); b('white', x0 + 20, y0 + 20, 280, x1 - 20, y1 - 50, 480);
    b('accent', x0 + 20, y0 + 20, 480, x1 - 20, y1 - 750, 530);
    const n = x1 - x0 > 1200 ? 2 : 1, w = (x1 - x0 - 40) / n;
    for (let i = 0; i < n; i++) b('white', x0 + 60 + i * w, y1 - 550, 480, x0 + i * w + w - 20, y1 - 130, 600);
  };
  // A chair: a seat on a block, with its back on the side that (dx, dy) points to.
  const chair = (x: number, y: number, dx: number, dy: number): void => {
    b('dark', x - 200, y - 200, 0, x + 200, y + 200, 450);
    b('dark', x + (dx < 0 ? -200 : dx > 0 ? 160 : -200), y + (dy < 0 ? -200 : dy > 0 ? 160 : -200), 450, x + (dx > 0 ? 200 : dx < 0 ? -160 : 200), y + (dy > 0 ? 200 : dy < 0 ? -160 : 200), 850);
  };
  // Bedroom 1: a double bed, two bedside tables and a wardrobe.
  bed(1050, 5200, 2550, 7200); b('wood', 550, 6750, 0, 1000, 7200, 500); b('wood', 2600, 6750, 0, 3050, 7200, 500); b('wood', 200, 3700, 0, 2000, 4300, 2000);
  // Bedroom 2: two single beds, a desk at the window and a wardrobe.
  bed(150, 1700, 1050, 3600); bed(1500, 1700, 2400, 3600); b('wood', 1075, 3200, 0, 1475, 3600, 500);
  b('wood', 1100, 0, 700, 2500, 600, 740); b('wood', 1100, 0, 0, 1150, 600, 700); b('wood', 2450, 0, 0, 2500, 600, 700); chair(1800, 850, 0, 1); b('wood', 3000, 100, 0, 3600, 1500, 2000);
  // Living: a sofa, a rug, a low table, the screen, and a plant.
  b('accent', 5600, 450, 0, 7450, 2350, 8); b('wood', 6200, 900, 8, 6800, 1900, 380);
  b('dark', 7500, 400, 0, 8400, 2400, 420); b('dark', 8200, 400, 420, 8400, 2400, 800); b('dark', 7500, 400, 420, 8200, 600, 620); b('dark', 7500, 2200, 420, 8200, 2400, 620);
  b('wood', 4900, 500, 0, 5350, 2300, 450); b('dark', 4930, 800, 450, 4980, 2000, 1150);
  round('wood', 9300, 600, 0, 220, 400); tones.green!.push(own(sphere(380, { at: [9300, 600, FLOOR + 780] })));
  // Dining: a table and four chairs.
  b('wood', 5800, 3900, 700, 7200, 4700, 740);
  for (const [x, y] of [[5850, 3950], [7100, 3950], [5850, 4600], [7100, 4600]] as const) b('wood', x, y, 0, x + 50, y + 50, 700);
  chair(6150, 3650, 0, -1); chair(6850, 3650, 0, -1); chair(6150, 4950, 0, 1); chair(6850, 4950, 0, 1);
  // Kitchen: base units with a worktop, a sink, a hob, a tall fridge, and an island with two stools.
  b('white', 9400, 2300, 0, 10000, 4700, 870); b('wood', 9380, 2300, 870, 10000, 4700, 910); b('steel', 9400, 4700, 0, 10000, 5300, 2000);
  b('steel', 9500, 2500, 910, 9900, 3100, 916); b('dark', 9480, 3600, 910, 9920, 4300, 918);
  for (const [x, y] of [[9590, 3780], [9810, 3780], [9590, 4120], [9810, 4120]] as const) round('steel', x, y, 918, 75, 14);
  b('white', 7950, 2900, 0, 8550, 4700, 870); b('wood', 7930, 2880, 870, 8570, 4720, 910);
  for (const y of [3350, 4250]) { round('steel', 7750, y, 0, 25, 620); round('accent', 7750, y, 620, 170, 40); }
  // Bathroom: a bath, a basin and a WC. Utility: a washer, a hot water cylinder and a shelf unit.
  tones.white!.push(own(unwrap(cut(own(box(1700, 700, 550, { at: [6350, 6850, FLOOR + 275] })), own(box(1540, 540, 480, { at: [6350, 6850, FLOOR + 310] }))))));
  b('white', 5900, 5400, 700, 6400, 5800, 850); b('white', 6080, 5400, 0, 6220, 5560, 700);
  b('white', 7000, 5650, 380, 7200, 6050, 800); round('white', 6810, 5850, 0, 190, 400);
  b('white', 9400, 6600, 0, 10000, 7200, 850); b('dark', 9380, 6700, 250, 9400, 7100, 650); round('steel', 7700, 6800, 0, 275, 1500); b('wood', 7300, 5450, 0, 7600, 6350, 1100);
  // Store: two shelf units. Balcony: a table, two chairs and a planter.
  b('wood', 3700, 100, 0, 4100, 1750, 1100); b('wood', 4450, 100, 0, 4800, 1000, 1100);
  round('steel', 6000, -1050, 0, 30, 700); round('wood', 6000, -1050, 700, 350, 30); chair(5350, -1050, -1, 0); chair(6650, -1050, 1, 0);
  b('dark', 8600, -1750, 0, 9950, -1350, 400);
  for (const x of [8850, 9275, 9700]) tones.green!.push(own(sphere(200, { at: [x, -1550, FLOOR + 600] })));
  const tint: Record<string, string> = { white: '#f5f2ea', wood: '#b98a5a', dark: '#48515c', accent: '#ff5a1f', steel: '#c3c9d1', green: '#4f9a5c' };
  return Object.entries(tones).map(([tone, list]) => color(compound(list), tint[tone]!));
}

Functions used ​

placedSolids toIfc box compound cut cylinder intersect measureVolume sphere unwrap color present

▶ Run Room Schedule and Zones in the playground

Released under the Apache 2.0 License.