How-to guide·3D viewerJavaScript

Switch between models

Load several models once and switch between them — show one at a time, highlight one with the others grey, or swap the loaded model for another — without reloading.

Discipline models are loaded together and looked at one at a time. The ModelManager keeps every loaded model and can hide and show whole models; the ObjectStateManager colours them.

Live demo#

One model at a timeLive 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.

Both S1 models are loaded once. Model picks one; Show only hides the other, Highlight keeps it in translucent grey. The last button unloads the electrical model and loads another one in its place.

Before you start#

You have a viewer (Embed the viewer) and the Vids of the models; List the tree finds them in a folder.

Steps#

// The viewer, modelManager and objectStateManager of the embed example
// Load every model of a folder once; listings carry summary versions, so get the full ones
const listing = await client.fs.listNodesByPath("/");
const versionUuids = listing.nodes.filter((node) => !node.folder && node.last_version_uuid && node.name.toLowerCase().endsWith(".ifc")).map((node) => node.last_version_uuid);
const versions = await Promise.all(versionUuids.map((vid) => client.fs.getNodeVersion(vid)));
await modelManager.addModels(versions);

// Show one model: the others stay loaded, so switching back is instant
async function showOnly(versionUuid) {
	await modelManager.showModels(versionUuids);
	await modelManager.hideModels(versionUuids.filter((vid) => vid != versionUuid));
	await objectStateManager.viewFit(modelManager.getModel(versionUuid).qids, {animate: true});
}

// Or keep the others visible, grey and translucent, around the one in its own colours
async function highlight(versionUuid) {
	await modelManager.showModels(versionUuids);
	await objectStateManager.resetColor(modelManager.getAllQids());
	const others = [];
	for (const vid of versionUuids) {
		if (vid != versionUuid) {
			others.push(...modelManager.getModel(vid).qids);
		}
	}
	await objectStateManager.changeColor(others, [0.6, 0.6, 0.6, 0.25]);
	await objectStateManager.viewFit(modelManager.getModel(versionUuid).qids, {animate: true});
}

// Everything back
async function showAll() {
	await modelManager.showModels(versionUuids);
	await objectStateManager.resetColor(modelManager.getAllQids());
	await objectStateManager.viewFit(modelManager.getAllQids(), {animate: true});
}

// Or keep one model in memory at a time: setModels unloads what is not in the list
async function swap(versionUuid) {
	await modelManager.setModels([await client.fs.getNodeVersion(versionUuid)]);
}

Four ways to switch; a page usually needs one or two of them.

  1. Load them once. addModels takes complete versions: a listing gives summaries, so getNodeVersion each Vid first.
  2. Show one. modelManager.hideModels(vids) and showModels(vids) hide and show whole loaded models; nothing is reloaded. viewer.setModelVisible(vid, visible) does the same for one viewer only.
  3. Or highlight one. Colour the objects of the other models grey and translucent with changeColor and give the chosen one its own colours back with resetColor. modelManager.getModel(vid).qids are the objects of a model that have geometry.
  4. Or swap. To keep one model in memory at a time, modelManager.setModels([version]) unloads every model that is not in the list and loads the ones that are not loaded yet; removeModels and addModels do the same in two steps.

Check#

Switching between loaded models is instant; the network panel of the browser shows no geometry download until a model is swapped in.