Tutorial·Build a model viewerJavaScript

Load a model

Resolve a version, load it through the ModelManager, load a second model and frame it, and replace what is loaded.

A model in BIM.works is a Version of a Node; the viewer loads versions. From this lesson on the rounds start with what the first lesson built: a client, a modelManager, an objectStateManager and an empty viewer on the page. Your code loads the models.

One model#

The version UUID of the first demo model, BIM_WORKS_FIRST_MODEL, stands in for the model you want to show. Two things are easy to miss:

  • The ModelManager wants a complete Version, one that carries im_metadata, the description of the processed geometry. client.fs.getNodeVersion(uuid) (or getNodeVersions([…]) for several) always returns one, and so does the lastVersion of a node returned by listNodes or getNodeByPath; the version summaries in tree change notifications do not, and a version that is still being processed has no im_metadata yet. Passing an incomplete version to addModels throws IncompleteVersionError; when in doubt, resolve the UUID.
  • addModels resolves when the model is fully loaded, not when loading starts. Code after the await can rely on every object being present; a fit-to-view or a colour change before that point would apply to an empty scene.
GoalStarts with · an empty viewer
Load the first demo model into the viewer.
Ready to use: client, modelManager, objectStateManager and viewer, with nothing loaded yet.
// BIM_WORKS_FIRST_MODEL is the version UUID of the first demo model
const uuid = BIM_WORKS_FIRST_MODEL;
Running the code needs JavaScript in this page; the hints and the solution are below.
Hints
  1. The ModelManager loads Version objects, not UUIDs: await client.fs.getNodeVersion(uuid) resolves one.
  2. await modelManager.addModels([version]) loads it into every viewer connected to the ObjectStateManager.
Show solution
const version = await client.fs.getNodeVersion(BIM_WORKS_FIRST_MODEL);
await modelManager.addModels([version]);
Also accepted
const [version] = await client.fs.getNodeVersions([BIM_WORKS_FIRST_MODEL]);
await modelManager.setModels([version]);

The progress handler passed as the viewer's second argument is an object whose methods are all optional: versionLoadProgress(version, percentage) fires while geometry streams in, versionLoaded(version) once at the end and versionUnloaded(version) when a model is removed. The viewer in the rounds has one that writes to its status line.

Finding a model to load#

The UUID in the rounds comes from the page; in an application it comes from the tree. client.fs.getRootNodeUuid() is the node your token was created on, client.fs.listNodes(uuid) its children, and client.fs.getNodeByPath("/Demo/Schependomlaan/IFC Schependomlaan.ifc") a node by its path relative to that root. A node that holds a model has last_version_uuid, which is what you resolve and load. List the tree shows the whole tree walk; Identifiers explains why models are addressed by version and objects by Qid.

Two models, and the camera#

modelManager.addModels([a, b]) loads several models at once. In the default LOCAL mode every model sits on its own coordinates, so unrelated models can overlap or lie far apart, and after loading the camera frames whatever it framed before. Two calls fit the camera: objectStateManager.viewFitModels(versionUuids, {animate: true}) frames whole models, objectStateManager.viewFit(qids, {animate: true}) frames a set of objects (lesson four). Both go through the ObjectStateManager so that every connected viewer moves.

GoalStarts with · an empty viewer
Load both demo models, and frame the second one.
Ready to use: client, modelManager, objectStateManager and viewer, with nothing loaded yet.
const first = await client.fs.getNodeVersion(BIM_WORKS_FIRST_MODEL);
await modelManager.addModels([first]);
Running the code needs JavaScript in this page; the hints and the solution are below.
Hints
  1. BIM_WORKS_SECOND_MODEL is the version UUID of the second demo model; it is resolved like the first.
  2. addModels takes a list: both versions can go in one call.
  3. objectStateManager.viewFitModels([second.uuid], {animate: true}) frames one model; it takes version UUIDs.
Show solution
const first = await client.fs.getNodeVersion(BIM_WORKS_FIRST_MODEL);
const second = await client.fs.getNodeVersion(BIM_WORKS_SECOND_MODEL);
await modelManager.addModels([first, second]);
await objectStateManager.viewFitModels([second.uuid], {animate: true});
Also accepted
const [first, second] = await client.fs.getNodeVersions([BIM_WORKS_FIRST_MODEL, BIM_WORKS_SECOND_MODEL]);
await modelManager.addModels([first, second]);
await objectStateManager.viewFitModels([second.uuid], {animate: true});
const first = await client.fs.getNodeVersion(BIM_WORKS_FIRST_MODEL);
await modelManager.addModels([first]);
const second = await client.fs.getNodeVersion(BIM_WORKS_SECOND_MODEL);
await modelManager.addModels([second]);
await objectStateManager.viewFitModels([second.uuid]);
Tip

Models that carry a geo reference can be shown at their real relative positions with viewer.setMode("GEO"). The call returns false when a loaded model has no geo reference; LOCAL is the default and always available.

Replacing what is loaded#

modelManager.removeModels([version]) unloads a model, and modelManager.setModels([...]) replaces the whole set: it loads what is missing and unloads what is no longer listed. Always go through the ModelManager, never through the viewer directly: the ModelManager is what knows which versions are loaded, and a version it still considers loaded is not loaded again.

The next round starts with the first demo model loaded; version is its Version.

GoalDemo model · Schependomlaan
Show the second demo model instead of the first.
Ready to use: client, modelManager, objectStateManager, viewer and version, the demo model, loaded.
// version is the first demo model, loaded
const second = await client.fs.getNodeVersion(BIM_WORKS_SECOND_MODEL);
await modelManager.addModels([second]);
Running the code needs JavaScript in this page; the hints and the solution are below.
Hints
  1. Adding the second model leaves the first one loaded.
  2. modelManager.removeModels([version]) unloads the first model.
  3. modelManager.setModels([second]) does both in one call.
Show solution
const second = await client.fs.getNodeVersion(BIM_WORKS_SECOND_MODEL);
await modelManager.setModels([second]);
Also accepted
const second = await client.fs.getNodeVersion(BIM_WORKS_SECOND_MODEL);
await modelManager.removeModels([version]);
await modelManager.addModels([second]);

The whole page#

The rounds as one program of your own, with a button that unloads and loads the second model again:

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);
const viewer = new Viewer3d(objectStateManager);
await viewer.initializeViewer();

const toolbar = document.createElement("div");
toolbar.classList.add("toolbar");
document.body.appendChild(toolbar);
document.body.appendChild(viewer.container);

// Resolve both versions in one call and load them together
const [first, second] = await client.fs.getNodeVersions(["<version uuid of the first demo model>", "<version uuid of the second demo model>"]);
await modelManager.addModels([first, second]);

// In LOCAL mode each model sits on its own coordinates: frame the first one
await objectStateManager.viewFitModels([first.uuid], {animate: true});

// Unload and reload the second model through the ModelManager
let secondLoaded = true;
const toggle = document.createElement("button");
toggle.textContent = "Hide " + second.node_name;
toggle.addEventListener("click", async () => {
	if (secondLoaded) {
		await modelManager.removeModels([second]);
	} else {
		await modelManager.addModels([second]);
	}
	secondLoaded = !secondLoaded;
	toggle.textContent = (secondLoaded ? "Hide " : "Show ") + second.node_name;
});
toolbar.appendChild(toggle);

const fit = document.createElement("button");
fit.textContent = "Fit " + first.node_name;
fit.addEventListener("click", () => {
	objectStateManager.viewFitModels([first.uuid], {animate: true});
});
toolbar.appendChild(fit);
html, body {
	height: 100%;
	margin: 0;
}

body {
	display: flex;
	flex-direction: column;
}

.toolbar {
	display: flex;
	gap: 8px;
	padding: 8px;
	background: #eeeeee;
}

.viewer3d {
	flex: 1;
	min-height: 0;
}

.viewer3d canvas {
	outline: none;
}

Both demo models load into the same viewer and the camera frames the first. The first button removes and re-adds the second model through the ModelManager, the second frames the first model again.

You can now#

  • Resolve a version UUID to a Version and load it.
  • Know when loading is complete, and follow its progress.
  • Walk the tree to find a model's last version.
  • Load several models, frame one of them, and replace what is loaded.

Next: Select and colour.