How-to guide·3D viewerJavaScript

Show a floor plan

Turn the 3D viewer into a floor plan — one storey, an orthographic camera looking down, 2D mode (a drag turns the plan, nothing tilts), the ceilings hidden and every room labelled with its number, name and area — the replacement for the 2D viewer.

A floor plan is the 3D viewer with four settings: an orthographic camera, one storey visible, the camera above it looking down, and 2D mode, in which a drag turns the plan instead of orbiting. This is how to show plans in new applications: the 2D viewer, which drew plans the server generated, is being phased out (2D viewer). The 3D plan shows the same objects with the same selection, colours and annotations as the 3D view, because it is the 3D view. A plan beside a 3D view is a second viewer on the same ModelManager, which shares the loaded models (Show models in several viewports).

Live demo#

A storey as a plan in the 3D viewerLive demo
Loads the demo model in this page with the demo API token of your account: open BIM.works and sign in once in this browser.

Storey picks the floor; press a point and go around it to turn the plan (it snaps square), right-drag or Shift-drag to pan, scroll to zoom. Text picks the rooms' labels (number, name and floor area in every room the text fits, on by default), room tags (annotations) or nothing. 3D switches the same viewer back to a perspective view of the whole model, Plan returns to the plan.

Before you start#

You have a viewer with a model loaded (Embed the viewer). The model needs building storeys; most IFC models have them.

Steps#

const factory = new BimWorksClientFactory("https://bim.works");
const client = await factory.createClientWithApiToken("<your API token>");

const modelManager = new ModelManager(client);
const objectStateManager = new ObjectStateManager(modelManager);

// A navigation mode for plans: the left button turns the plan (2D mode, below), the other
// buttons and Shift-drag pan, the wheel zooms and the keyboard does not move the camera
const viewer = new Viewer3d(objectStateManager, undefined, {
	navigationMode: "plan",
	navigationModes: {
		plan: {
			extends: "default",
			keyboardNavigation: false,
			keyBindings: {
				lookAround: [],
				pan: [{button: 1}, {button: 2}, {button: 0, shiftKey: true}]
			}
		}
	}
});
await viewer.initializeViewer();
document.getElementById("viewer").appendChild(viewer.container);

const version = await client.fs.getNodeVersion("<version uuid of the first demo model>");
await modelManager.addModels([version]);

// A field of view of 0 makes the camera orthographic
viewer.getCamera().fov = 0;
// 2D mode: going around the press point turns the plan about it, snapping square; nothing tilts
viewer.set2dMode(true);

// One entry per storey, merged over the models you pass; "[No BS]" has no storey objects
const listed = await client.bim.listBuildingStoreys([version.uuid]);
const storeys = listed.buildingStoreys
	.filter((storey) => storey.buildingStoreys.length > 0)
	.sort((a, b) => a.elevation - b.elevation);

async function showStorey(storey) {
	const storeyQids = storey.buildingStoreys.map((buildingStorey) => buildingStorey.qid);
	// Only this storey, openings left out
	await objectStateManager.setBuildingStoreysVisibleExclusive(storeyQids);
	// Ceilings would cover the rooms
	await objectStateManager.removeFromVisibleSet(modelManager.getTypeQids("IFCCOVERING"));
	// The storey's grid lines (IFCANNOTATION) are not drawn and reach beyond the building: leave them out of the fit
	const gridLines = modelManager.getTypeQids("IFCANNOTATION");
	const objects = [...modelManager.getObjectQidsForBuildingStoreyQids(storeyQids, false)].filter((qid) => !gridLines?.has(qid));
	// From above, filling the view with 2% to spare on each side: viewDirection points from the objects to the camera
	await objectStateManager.viewFit(objects, {viewDirection: [0, 0, 1], margin: 2, animate: true});
}

// A storey picker, starting on the ground floor (floor 0)
const select = document.createElement("select");
storeys.forEach((storey, index) => select.add(new Option(storey.name, index)));
select.addEventListener("change", () => showStorey(storeys[select.value]));
document.body.prepend(select);

const ground = storeys.find((storey) => storey.floor == 0) || storeys[0];
select.value = storeys.indexOf(ground);
await showStorey(ground);

A page with a <div id="viewer"> of a fixed height. The storey picker starts on floor 0, the ground floor.

  1. Define a plan navigation mode. A custom mode that extends default, with no binding for lookAround and with the middle and right buttons and Shift + left drag panning (Navigation modes). The left button keeps orbit, which 2D mode turns into turning the plan. The wheel still zooms. viewer.startNavigationMode("plan") and viewer.startNavigationMode("default") switch between plan and 3D at run time.
  2. Switch to 2D mode. viewer.set2dMode(true) makes the camera orthographic and turns it to look straight down at what is in the middle of the view, at the same scale; set2dMode(false) turns it back to the perspective view it left. A camera you made orthographic yourself (viewer.getCamera().fov = 0, as the example does; any positive field of view below 120 switches back to perspective) stays orthographic when you leave 2D mode. In 2D mode the left button (one finger) turns the plan about the vertical axis through the point you press, without tilting it, and a pan moves the plan exactly with the pointer, as a sheet of paper under your hand. The press places the pivot; going around it turns the plan by the pointer's angle around that point, however far out you are. A cross through the pivot shows level and plumb, and the plan snaps where the model's own axes (0°, 90°, 180°, 270°) or the building's walls (the axes of the model's oriented bounding box) stand level with it; hold Shift while turning with the mouse to turn freely. A drawing on its own (a PDF or DWG) is in 2D mode by itself; set2dMode(null) lets the model decide again, is2dMode() tells which mode the viewer is in and add2dModeListener(listener) hears it change. set2dMode(true, {side: "front"}) shows an elevation instead (also "back", "left" and "right"; "bottom" looks up at the plan from below): upright, and the left button pans it; get2dSide() tells the side.
  3. List the storeys. client.bim.listBuildingStoreys(versionUuids) answers one entry per storey, merged over the models you pass: the storeys of an architectural and an installation model with the same name, GlobalId or elevation become one entry. Each has a name, an elevation, a floor number (0 for the ground floor) and buildingStoreys, the Qids of the storey objects in each model. An entry named [No BS] holds the objects that are in no storey (Show building storeys).
  4. Show one storey. setBuildingStoreysVisibleExclusive(storeyQids) on the ObjectStateManager makes the objects of those storeys the only visible ones, openings left out. Hide what covers the plan from above, usually the suspended ceilings (IFCCOVERING).
  5. Look down on it. viewFit with viewDirection: [0, 0, 1] puts the camera above the objects with north (the model's +Y) up. In 2D mode a fit keeps the plan turned as it is; rotation turns it as the 2D viewer's fit does: "ORIGINAL" puts the model's +X to the right, "MODEL_AXIS" lays the building's main axis across, "OBJECT_AXIS" the main axis of rotationObject. The direction points from the objects to the camera. With the orthographic camera the fit sizes the view to the bounding box of the objects, so the storey fills the width or the height of the viewer; margin keeps that percentage of the view free on each side. Fit what the plan draws: the objects of a storey can include its grid lines (IFCANNOTATION), which are not drawn and reach metres beyond the building.

Room labels#

In 2D mode, and in 3D views from above, the viewer writes the number, the name and the floor area of every space into the room, as the 2D viewer did, without a line of code: where the text fits, horizontal on screen however the plan is turned, a fixed size on screen. Zoomed out, a room that is too small for its text shows none; zoom in and it comes. The spaces themselves can stay hidden: a room's text shows while its storey does.

The labels of the ObjectStateManager are drawn the same way, on any object: setLabels([{qid, labels}]), one string per line, as for the 2D viewer:

// After showStorey(storey): every room already carries its number, name and floor area where the text fits

// Text of your own, on any object: one string per line. On a space it replaces the space's own text
const desks = await client.bim.query([version.uuid], "GET qid WHERE type = IFCFURNISHINGELEMENT");
await objectStateManager.setLabels(desks.map((desk, index) => ({qid: desk.qid, labels: ["Desk " + (index + 1)]})));

// A larger font; labels that no longer fit hide until you zoom in
viewer.fontScaling = 17;

// The plan as a PNG, with its labels
const png = await viewer.screenshot({includeText: true, backgroundColor: "#FFFFFF"});

// resetLabels() takes your text away again and the rooms get theirs back; setPlanLabelsEnabled(false) hides all of it
await objectStateManager.resetLabels();

Runs after showStorey(storey) of the example above.

  • Fit. A label shows when its text fits inside its object: inside the object's rectangle (its oriented bounding box) when the object fills it, otherwise inside the largest circle that fits in the object's floor area, which the server computed at check-in. So a label in an L-shaped room sits in the room, not in the corner of its box.
  • Your text wins. A label you set replaces a space's own text; resetLabels() brings that back.
  • Hidden where covered. A label shows only where nothing is drawn above it: with two storeys shown, the upper one's floor covers the lower one's labels, and a roof covers every room under it.
  • From an angle. In a 3D view looking down up to 70° from the vertical, a label lies in its room like text on paper: at the height of the room's top (the cut of a section plane, or the top of the walls), foreshortened, still reading left to right. Flatter, it fades and goes. height: "bottom" in the planLabels setting puts it on the floor instead, where walls and furniture in front cover it; setPlanLabelsHeight("bottom") and setPlanLabelsHeight("top") switch a running viewer between the two.
  • Settings. setPlanLabelsEnabled(false) hides all of them, fontScaling sets the size in pixels (15 by default), and the planLabels setting turns off the spaces' own text (spaces: false) or leaves out the last lines of a label that does not fit (dropLines: true).
  • Corridors. With orientation: "AUTO" in that setting, a label that does not fit across a narrow room runs along it instead, as on a drawing: turned with the plan, but never upside down, and bottom to top where the room stands upright.
  • Screenshots. screenshot({includeText: true}) draws them into the image (Take a screenshot in the browser).

Room tags#

Room tags are cards rather than text in the plan: an annotation layer with the roomTag template puts the number, the name and the net floor area on every space of the storey, at any zoom (how-to/viewer3d/annotations#room-tags). Turn the labels off under them, setPlanLabelsEnabled(false), or the rooms carry both:

// After showStorey(storey): a room tag on every space of the storey

// The rooms' own labels (number, name, area) would show under the tags
viewer.setPlanLabelsEnabled(false);

const where = "type = IFCSPACE AND buildingStorey.attribute.Name = " + JSON.stringify(storey.name);

// Spaces are hidden by default, and an annotation shows only while its object does
await objectStateManager.addToVisibleSet("GET qid WHERE " + where);
// Almost clear, so the floor and the furniture show through the rooms
await objectStateManager.changeColor("GET qid WHERE " + where, "#FFFFFF33");

// Number, name and net floor area of each room; layer.remove() takes them away again
const layer = objectStateManager.addAnnotationLayer({
	template: "roomTag",
	group: "rooms",
	query: {where}
});

Runs after showStorey(storey) of the example above. The colour leaves the spaces almost clear; without it they cover the floor in their own colour.

Any other annotation works on a plan too: text, icons, values from properties (Annotate a model).

Cut the plan#

A floor plan is cut about 1.2 m above the floor, so walls, doors and windows read as cut and what stands higher goes. viewer.setPlanCut(z) removes everything above height z (mm, in the render frame) and closes the cut with end caps, as a section plane does. It is none of the six section planes: it is not listed in getSectionPlanes().getPlanes(), not drawn and leaves the user all six. setPlanCut(null) removes it, getPlanCut() reads it. An annotation on an object that the cut keeps part of is drawn on the cut, over its object, even when its anchor lies above the cut (on top of a window's box). The cut hides the annotations of objects it removes whole. The storey's floor in the render frame is its elevation from listBuildingStoreys, times the model's length unit (viewer.getModelLengthUnitMm(vid)), through viewer.modelToMeasurementCoordinates(vid, [0, 0, z]) and viewer.measurementToRenderCoordinates: cut at that height plus 1200. The cut stays when you leave 2D mode; remove it there when the plan was what it was for.

Compared with the 2D viewer#

2D viewer Plan in the 3D viewer
new Viewer2d(objectStateManager), init(), start() new Viewer3d(...) with a plan navigation mode and set2dMode(true)
setBuildingStoreysVisibleExclusive The same call
setLabels([{qid, labels}]), labels on the spaces The same call, drawn inside the objects; the spaces' own labels without code
Plans generated on the server, drawn flat The 3D geometry seen from above: furniture, stairs and sloped surfaces as they are
Its own viewer next to a 3D view The same viewer switches between plan and 3D; a second Viewer3d on the same ObjectStateManager gives both side by side (Show models in several viewports; in the BIM.works app, two viewers with the 3D camera on the plan: Connect two viewers)

Limits#

  • A label half behind a wall shows whole or not at all: it is hidden when its centre or two of its corners are covered.
  • Without a cut the plan shows each object of the storey from above, so a wall is its top face and a tall cupboard hides what is under it. Cut it as a floor plan is drawn (how-to/viewer3d/show-a-floor-plan#cut), or export a vector drawing on the server (Export a vector drawing).

Check#

The storey fills the view from above, the rooms carry their number, name and area where the text fits, a left drag around the press point turns the plan without tilting it (the text stays horizontal) and snaps it square, a right drag moves it, and 3D (fov = 45, set2dMode(null) and the default mode) brings back the perspective view of the same viewer.