How-to guide·3D viewerJavaScript

Select and colour objects

Select objects by Qid or by query, listen to selection changes, choose SHIFT or AUTO multi-selection, and set, batch and reset colours through the ObjectStateManager.

Selection and colour live in the ObjectStateManager, not in the viewer: change them there and every connected viewer follows. Objects are addressed by Qid, or by a BQL query that the manager resolves on the server.

Before you start#

You have a viewer with a model loaded (Embed the viewer) and its objectStateManager. Every method below returns a promise that resolves when all viewers have applied the change.

Select#

JavaScript#
// Replace the selection by the walls, resolved on the server
await objectStateManager.setToSelectionSet("GET WHERE type = IFCWALL");

// Add and remove by Qid; any iterable of Qid works, so does a QidSet
await objectStateManager.addToSelectionSet([qid]);
await objectStateManager.removeFromSelectionSet([qid]);
await objectStateManager.clearSelection();

const selected = objectStateManager.getSelectedQids();   // a read-only QidSet

To react to what the user clicks, register a listener; the callbacks receive iterables of Qid:

JavaScript#
objectStateManager.addSelectionListener({
	addedToSelectionSet(qids) { /* … */ },
	removedFromSelectionSet(qids) { /* … */ },
	setToSelectionSet(qids, unselected) { /* … */ }
});

Multi-selection. The manager is created with {multiSelect: true} to allow more than one selected object at a time; then viewer.setMultiSelectMode(mode) decides how the mouse does it. "SHIFT" (the default): a plain click replaces the selection, Shift + click toggles the clicked object, a click on empty space clears everything. "AUTO": every click toggles the clicked object, empty space does nothing, and only your code can clear the selection.

JavaScript#
const objectStateManager = new ObjectStateManager(modelManager, {multiSelect: true});
// after initializeViewer()
viewer.setMultiSelectMode("AUTO");

Pass the mode as a string. The type definitions declare a MultiSelectMode enum, but the package exports no such value and the viewer compares strings.

Selection modes and looks. viewer.setSelectionMode(SELECTION_RECTANGULAR) lets the user draw a rectangle instead of clicking (SELECTION_DIRECT_PICKING is the default). viewer.setSelectionVisualization(new WireframeSelectionVisualization(color)) or new NoneSelectionVisualization() changes how a selected object is drawn; new FatWireframeSelectionVisualization(thickness, color) draws thicker edges, but only when the viewer was created with fatWireframes: {enabled: true} in its settings (otherwise it draws no edges at all); setHoverFaceColor and setHoverWireframeColor change the hover glow. Objects can be made unpickable with setPickableSet when the manager was created with {pickability: true}.

Colour#

JavaScript#
// One colour for a set; "#00FF00", "#F0", "red" and [r, g, b, a] (0..1) are all accepted
await objectStateManager.changeColor("GET WHERE type = IFCDOOR", "#FF0000");
await objectStateManager.changeColor([qid], [0, 0.5, 1, 0.4]);   // transparent blue

// Many colours at once, in one pass through the viewers
const changes = new ColorChangeMap();
changes.changeQid(qidA, "#7CB82F");
changes.changeQid(qidB, "#EC4339");
await objectStateManager.changeColorMulti(changes);
changes.dispose();

// Back to the model's own colours
await objectStateManager.resetColor([qid]);
await objectStateManager.resetAll();   // colours, visibility and selection, everything

changeColorMulti is the call to use for results of a query or a check: one call for thousands of objects instead of one per colour. resetAll restores the original visibility as well (it leaves openings, project, building, storeys and zones hidden, as on first load) and clears the selection.

The C and D keys in the viewer do nothing more than this: changeColor(getSelectedQids(), randomColour).

Check#

objectStateManager.getSelectedQids().size matches what you selected, and the coloured objects keep their colour when you orbit.