How-to guide·2D viewerJavaScript

Show floor plans in the 2D viewer

Create a Viewer2d, list the storeys and show one, colour, select and label objects, load it with a versions token, and destroy and unload it again.

Warning

The 2D viewer is being phased out. A new application shows plans in the 3D viewer in orthographic mode: Show a floor plan.

Live demo#

Floor plans in the 2D 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 shows another floor. Colour walls, Select doors and Space labels go through the ObjectStateManager, as they would for the 3D viewer. Destroy removes the viewer and Create makes a new one, which draws the model that is still loaded.

Before you start#

Install @bimworks/viewer2d with @bimworks/viewercommon and @bimworks/jssdk (JavaScript and TypeScript). The credential needs fs:read, geometry:read and query:run on the models: loading a model and listing its storeys both run queries.

1. Create the viewer and show a storey#

const factory = new BimWorksClientFactory("https://bim.works");
const client = await factory.createClientWithApiToken("<your API token>");
const versionUuid = "<version uuid of the first demo model>";

// The same two managers as for the 3D viewer
const modelManager = new ModelManager(client);
const objectStateManager = new ObjectStateManager(modelManager);

// The 2D viewer sizes itself to its container: add the container to the page before init()
const viewer = new Viewer2d(objectStateManager);
document.getElementById("plan").appendChild(viewer.container);
await viewer.init();
viewer.start();

// A row of links, one per storey
const storeysDiv = document.createElement("div");
document.body.prepend(storeysDiv);
const listed = await client.bim.listBuildingStoreys([versionUuid]);
const storeys = listed.buildingStoreys.filter((storey) => storey.buildingStoreys.length > 0);

async function showStorey(storey) {
	const storeyQids = storey.buildingStoreys.map((buildingStorey) => buildingStorey.qid);
	await objectStateManager.setBuildingStoreysVisibleExclusive(storeyQids);
	await objectStateManager.viewFit(modelManager.getObjectQidsForBuildingStoreyQids(storeyQids, false), {margin: 5, animate: true});
}

for (const storey of storeys) {
	const link = document.createElement("a");
	link.href = "#";
	link.textContent = storey.name;
	link.addEventListener("click", (event) => {
		event.preventDefault();
		showStorey(storey);
	});
	storeysDiv.appendChild(link);
}

// Load the model, show its spaces (hidden by default), then show the ground floor (floor 0)
const version = await client.fs.getNodeVersion(versionUuid);
await modelManager.addModels([version]);
await objectStateManager.addToVisibleSet("GET qid WHERE type = IFCSPACE");
await showStorey(storeys.find((storey) => storey.floor == 0) || storeys[0]);

A page with a <div id="plan"> of a fixed height. init() is asynchronous; load models only after it has resolved.

  • new Viewer2d(objectStateManager, settings) creates the viewer; its container goes into the page before await viewer.init(), because init() measures it. start() starts drawing; stop() pauses it while the viewer is hidden.
  • client.bim.listBuildingStoreys(versionUuids) answers the storeys merged over the models, each with a name, an elevation, a floor number (0 is the ground floor) and the Qids of its storey objects in buildingStoreys (Show building storeys).
  • setBuildingStoreysVisibleExclusive(storeyQids) shows that storey; viewFit fits the plan to its objects. The 2D viewer draws one storey at a time.
  • Spaces (IFCSPACE) are hidden by default, as in the 3D viewer. addToVisibleSet("GET qid WHERE type = IFCSPACE") after loading shows them; they stay visible when you show another storey, and a viewer created later draws them too.

The first model is fitted when it loads (camera.fitOnLoad, on by default). viewFit also takes rotation, which turns the plan while fitting: "KEEP" (the default), "ORIGINAL", "WIDEST_HORIZONTAL", "MODEL_AXIS", or "OBJECT_AXIS" with the object in rotationObject.

2. Colour, select and hide#

The 2D viewer follows the ObjectStateManager, so the calls are the ones the 3D guides describe, with a BQL query or a set of Qids:

JavaScript#
await objectStateManager.changeColor("GET qid WHERE type IN (IFCWALL, IFCWALLSTANDARDCASE)", "red");
await objectStateManager.setToSelectionSet("GET qid WHERE type = IFCDOOR");
await objectStateManager.removeFromVisibleSet("GET qid WHERE type = IFCFURNISHINGELEMENT");
await objectStateManager.clearSelection();

A 3D viewer connected to the same ObjectStateManager shows the same state (Select and colour objects, Hide and isolate). The x-ray set is the one state the 2D viewer ignores.

3. Label objects#

// Text placement: AUTO turns a label to fit a long, narrow room; HORIZONTAL and VERTICAL fix it
const viewer = new Viewer2d(objectStateManager, {
	textPlacement: {orientation: "AUTO"}
});
// ... init(), start(), load the model, show the spaces and a storey as in the first example

// Labels are drawn on visible objects only: the first example made the spaces visible
const where = "type = IFCSPACE AND buildingStorey.attribute.Name = " + JSON.stringify(storey.name);
const spaces = await client.bim.query([versionUuid], "GET qid, attribute.Name, attribute.LongName WHERE " + where);

// One entry per object, one string per line; an empty array removes the label again
viewer.setLabels(spaces.map((space) => ({
	qid: space.qid,
	labels: [space.attribute.Name?.value, space.attribute.LongName?.value].filter(Boolean)
})));

// Show or hide all text at once
viewer.toggleShowText();

The label of a room that is too small for its text is not drawn; zoom in and it appears.

viewer.setLabels takes one {qid, labels} per object, labels being the lines of text. Labels are drawn on visible objects only, which is why the first example shows the spaces. Each call replaces the whole set, as objectStateManager.setLabels does: an object left out, and every object after resetLabels(), shows its own text again (a space its number, name and area). A label shows when its text fits inside the object, judged as the 3D viewer's plan labels judge it (how-to/viewer3d/show-a-floor-plan#room-labels).

4. Create, destroy and unload#

// The managers and the loaded model outlive the viewer
const modelManager = new ModelManager(client);
const objectStateManager = new ObjectStateManager(modelManager);
let viewer = null;

async function create() {
	if (viewer) {
		destroy();
	}
	viewer = new Viewer2d(objectStateManager);
	document.getElementById("plan").appendChild(viewer.container);
	await viewer.init();
	viewer.start();
	// A new viewer draws the models the ModelManager already has: load only the first time
	if (!modelManager.isVersionUuidLoaded(versionUuid)) {
		await modelManager.addModels([await client.fs.getNodeVersion(versionUuid)]);
	}
	await showStorey(groundFloor);
}

function destroy() {
	// Frees the WebGL resources; the container stays in the page until you remove it
	viewer.destroy();
	viewer.container.remove();
	viewer = null;
}

// Unloading is the ModelManager's job, for every viewer at once
async function unload() {
	await modelManager.removeModels(modelManager.getVersionsAsArray());
}

The model is loaded once; every new viewer draws what the ModelManager holds.

destroy() releases the viewer's WebGL resources; remove its container yourself. Unload with modelManager.removeModels(...), which unloads from every connected viewer.

5. Load with a versions token#

A page that shows your users their models signs in with a versions token your server creates, and asks for a new one when it expires. Nothing about this is specific to the 2D viewer: factory.createClientWithAuth(auth), then client.registerExpiredTokenHandler(...), as Create a versions token and Refresh an expired token describe, with the scopes listed above.

6. Settings#

Setting Default Meaning
textPlacement.orientation "AUTO" Kept for compatibility, without effect: labels are horizontal on screen however the plan is turned. The 3D viewer's plans can turn a label along a narrow room (planLabels orientation: "AUTO").
camera.fitOnLoad true Fit the plan when the first model has loaded.
canvas.autoSize true Follow the size of the container; give the container a size, also in a page that scrolls.
rendering.antialias true WebGL antialiasing.
realtimeSettings all true drawTriangles, drawWireframes, drawLines (the IFC lines, such as axes) and drawAdditionalLines.

At run time, toggleFaceRendering, toggleWireframeRendering, toggleIfcLineRendering and toggleDoorOpeningRendering switch what is drawn; toggleUserPanning, toggleUserZoom, toggleUserRotate and toggleUserSelection switch the mouse and touch interaction off, for example for a small plan in a page that scrolls. screenshot(options) returns the plan as a PNG Blob.

Check#

The ground floor appears with its spaces and fits the container; another storey replaces it; walls turn red and doors are selected in the 2D viewer and in any 3D viewer on the same ObjectStateManager.