Tutorial·Build a model viewerJavaScript

Select and colour

Select objects from code with a BQL condition, change colours, hide and show objects, and react to what the user selects.

Everything about an object that is not its geometry lives in the ObjectStateManager: selected or not, visible or hidden, original colour or an override. The viewer shows that state and feeds clicks back into it. Your code therefore never talks to the viewer about objects; it talks to the ObjectStateManager, and every viewer follows.

The rounds of this lesson start with the demo model loaded and framed. Besides client, modelManager, objectStateManager and viewer, your code has version, the model's Version, whose uuid is what a query runs on.

Selecting from code#

The selection methods accept a set of qids or a BQL condition, and the ObjectStateManager resolves the condition on the server:

JavaScript#
await objectStateManager.setToSelectionSet("type = IFCWINDOW");
await objectStateManager.addToSelectionSet(someQidSet);
await objectStateManager.clearSelection();

The condition is a BQL WHERE without the keyword, exactly what the BQL course teaches; type = IFCPRODUCT AND calculated.geometry.hasGeometry selects everything that can be seen. The demo model's storeys are named -1 fundering, 00 begane grond, 01 eerste verdieping and so on up to 04 dak.

GoalDemo model · Schependomlaan
Select every door on the ground floor, the storey named 00 begane grond.
Ready to use: client, modelManager, objectStateManager, viewer and version, the demo model, loaded.
await objectStateManager.setToSelectionSet("type = IFCDOOR");
Running the code needs JavaScript in this page; the hints and the solution are below.
Hints
  1. The condition is a BQL WHERE without the keyword; AND joins two conditions.
  2. buildingStorey.attribute.Name is the name of the storey an object is on; strings are in double quotes, so the whole condition is easiest in single quotes.
Show solution
await objectStateManager.setToSelectionSet('type = IFCDOOR AND buildingStorey.attribute.Name = "00 begane grond"');
Also accepted
const doors = await client.bim.queryQids([version.uuid], 'type = IFCDOOR AND buildingStorey.attribute.Name = "00 begane grond"');
await objectStateManager.setToSelectionSet(doors);
await objectStateManager.addToSelectionSet('buildingStorey.attribute.Name = "00 begane grond" AND type = IFCDOOR');

Press F in the viewer to fit the view to the selection, H to hide it.

Colours#

changeColor(queryOrQids, colour) overrides the colour of a set of objects, changeColorMulti(colorChangeMap) does it for several sets in one pass, which is much faster than a call per colour, and resetColor(queryOrQids) restores the original colours. Colours are CSS strings: #RRGGBB, #RRGGBBAA with transparency, or a name. A ColorChangeMap collects the changes: changes.changeQids(qids, colour) for each set, then one changeColorMulti(changes).

GoalDemo model · Schependomlaan
Colour the walls green, #00FF00, and the windows semi-transparent blue, #0000FF80.
Ready to use: client, modelManager, objectStateManager, viewer and version, the demo model, loaded.
const changes = new ColorChangeMap();
// one changes.changeQids(qids, colour) per set, then one changeColorMulti
Running the code needs JavaScript in this page; the hints and the solution are below.
Hints
  1. await client.bim.queryQids([version.uuid], "type = IFCWALL") returns the qids of the walls.
  2. changes.changeQids(qids, "#00FF00") records a colour for a set; await objectStateManager.changeColorMulti(changes) applies everything recorded.
  3. objectStateManager.changeColor("type = IFCWALL", "#00FF00") colours one set in one call, if you prefer that.
Show solution
const changes = new ColorChangeMap();
changes.changeQids(await client.bim.queryQids([version.uuid], "type = IFCWALL"), "#00FF00");
changes.changeQids(await client.bim.queryQids([version.uuid], "type = IFCWINDOW"), "#0000FF80");
await objectStateManager.changeColorMulti(changes);
Also accepted
await objectStateManager.changeColor("type = IFCWALL", "#00FF00");
await objectStateManager.changeColor("type = IFCWINDOW", "#0000FF80");
await objectStateManager.changeColor("type = IFCWALL", "lime");
await objectStateManager.changeColor("type = IFCWINDOW", "rgba(0, 0, 255, 0.5)");

Visibility#

Visibility is a set too. setVisibleSet(query) shows exactly the matching objects and hides the rest, addToVisibleSet and removeFromVisibleSet adjust the set, and setBuildingStoreysVisibleExclusive(storeyQids) shows one storey at a time (the storeys come from client.bim.listBuildingStoreys([versionUuid])). When a model loads, the ModelManager leaves the spaces, zones, annotations, space boundaries, openings and storeys hidden, because they would obscure the model (a model that consists of little else shows them anyway).

GoalDemo model · Schependomlaan
Look inside the building: hide the slabs and show the spaces.
Ready to use: client, modelManager, objectStateManager, viewer and version, the demo model, loaded.
await objectStateManager.removeFromVisibleSet("type = IFCWALL");
Running the code needs JavaScript in this page; the hints and the solution are below.
Hints
  1. Slabs are IFCSLAB, spaces IFCSPACE.
  2. removeFromVisibleSet hides what matches its condition, addToVisibleSet shows it.
Show solution
await objectStateManager.removeFromVisibleSet("type = IFCSLAB");
await objectStateManager.addToVisibleSet("type = IFCSPACE");
Also accepted
await objectStateManager.addToVisibleSet("type = IFCSPACE");
await objectStateManager.removeFromVisibleSet("type = IFCSLAB");

resetAll() puts colours and visibility back to how the model loaded.

Tip

The keyboard does the same things through the same ObjectStateManager: H hides the selection, C and D give it a random opaque or transparent colour, F fits the view to it. Anything your code does can be undone by the user, and the other way round, because both go through one state.

Reacting to selection#

objectStateManager.addSelectionListener(listener) registers an object with up to three methods. setToSelectionSet(selected, unselected) is called when the selection is replaced, as a plain click does, with the new set and the objects that left it; addedToSelectionSet(qids) and removedFromSelectionSet(qids) are called for incremental changes, which only happen when the ObjectStateManager allows multi-selection (the default) and the user Shift-clicks. Clicking empty space clears the selection, so expect an empty set.

The sets contain Qid objects, not strings. To ask the server about them, put them in a BQL condition: qid IN ("vid-oid", "vid-oid"); QidSet.toQidCondition() writes it for you. How multi-selection reacts to the mouse is a viewer setting: viewer.setMultiSelectMode(mode) switches between the default, where Shift adds to the selection and a plain click replaces it, and a toggle mode where every click adds or removes. Both values are on the Viewer3d reference.

In the next round nobody clicks: once your code has run, the page selects the windows of the first floor itself, the way a click would replace the selection, and checks what your listener did with it. Afterwards, click objects in the viewer yourself.

GoalDemo model · Schependomlaan
Whenever the selection is replaced, frame the selected objects.
Ready to use: client, modelManager, objectStateManager, viewer and version, the demo model, loaded.
objectStateManager.addSelectionListener({
	setToSelectionSet: (selected, unselected) => {
		console.log(selected.size + " object(s) selected");
	}
});
Running the code needs JavaScript in this page; the hints and the solution are below.
Hints
  1. The listener's setToSelectionSet receives the new selection, a QidSet, every time it is replaced.
  2. objectStateManager.viewFit(qids, {animate: true}) frames a set of objects in every viewer.
Show solution
objectStateManager.addSelectionListener({
	setToSelectionSet: (selected) => objectStateManager.viewFit(selected, {animate: true})
});
Also accepted
objectStateManager.addSelectionListener({
	setToSelectionSet: async (selected, unselected) => {
		console.log(selected.size + " object(s) selected");
		if (selected.size > 0) {
			await objectStateManager.viewFit(selected, {animate: true});
		}
	}
});

The whole page#

The selection listener of a page of your own often asks the server about what was selected. This program logs the class and the name of each selected object:

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

const modelManager = new ModelManager(client);
// With multiSelect the user can Shift-click to extend the selection
const objectStateManager = new ObjectStateManager(modelManager, {
	multiSelect: true
});
const viewer = new Viewer3d(objectStateManager);
await viewer.initializeViewer();
document.body.appendChild(viewer.container);

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

// Ask the server about a set of qids with a BQL condition
const describe = async (qids) => {
	if (qids.size == 0) {
		console.log("Nothing selected");
		return;
	}
	const list = Array.from(qids).map((qid) => '"' + qid + '"').join(", ");
	const results = await client.bim.query([version.uuid], "GET type, attribute.Name WHERE qid IN (" + list + ")");
	for (const object of results) {
		console.log(object.type + " " + object.attribute.Name.value);
	}
};

// Every method of a selection listener is optional
objectStateManager.addSelectionListener({
	setToSelectionSet: (selected, unselected) => describe(selected),
	addedToSelectionSet: (qids) => describe(qids),
	removedFromSelectionSet: (qids) => console.log(qids.size + " object(s) removed from the selection")
});
html, body {
	height: 100%;
	margin: 0;
}

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

/* The viewer's container has no size of its own: give it the remaining height */
.viewer3d {
	flex: 1;
	min-height: 0;
}

.viewer3d canvas {
	outline: none;
}

Click an object to select it, Shift-click to add to the selection. Each change runs a BQL query for the selected qids and logs the class and name of each object. The listener methods are optional: with multiSelect off only setToSelectionSet is ever called.

You can now#

  • Select objects from code with a BQL condition or a set of qids.
  • Change and combine colours.
  • Hide and show objects.
  • Listen for selection changes and act on them.

Next: Query and highlight, the last lesson.