---
title: Select and colour
summary: Select objects from code with a BQL condition, change colours, hide and show objects, and react to what the user selects.
order: 30
languages: [javascript]
terms: [qid, object-state-manager, bql]
see-also: [tutorials/javascript-viewer/load-a-model, tutorials/javascript-viewer/query-and-highlight, how-to/viewer3d/select-and-colour-objects, tutorials/bql/index]
---

# Select and colour

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 [[term:qid|qids]] or a BQL condition, and the ObjectStateManager resolves the condition on the server:

```javascript {#select-from-code}
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](../bql/index.md) 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`.

```lesson {#lesson-1}
goal: Select every door on the ground floor, the storey named `00 begane grond`.
language: javascript
setup: model
check: selection
starter: |
  await objectStateManager.setToSelectionSet("type = IFCDOOR");
hints: |
  - The condition is a BQL `WHERE` without the keyword; `AND` joins two conditions.
  - `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.
solution: |
  await objectStateManager.setToSelectionSet('type = IFCDOOR AND buildingStorey.attribute.Name = "00 begane grond"');
accept: |
  - 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)`.

```lesson {#lesson-2}
goal: Colour the walls green, `#00FF00`, and the windows semi-transparent blue, `#0000FF80`.
language: javascript
setup: model
check: colours
starter: |
  const changes = new ColorChangeMap();
  // one changes.changeQids(qids, colour) per set, then one changeColorMulti
hints: |
  - `await client.bim.queryQids([version.uuid], "type = IFCWALL")` returns the qids of the walls.
  - `changes.changeQids(qids, "#00FF00")` records a colour for a set; `await objectStateManager.changeColorMulti(changes)` applies everything recorded.
  - `objectStateManager.changeColor("type = IFCWALL", "#00FF00")` colours one set in one call, if you prefer that.
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);
accept: |
  - 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).

```lesson {#lesson-3}
goal: Look inside the building: hide the slabs and show the spaces.
language: javascript
setup: model
check: visibility
starter: |
  await objectStateManager.removeFromVisibleSet("type = IFCWALL");
hints: |
  - Slabs are `IFCSLAB`, spaces `IFCSPACE`.
  - `removeFromVisibleSet` hides what matches its condition, `addToVisibleSet` shows it.
solution: |
  await objectStateManager.removeFromVisibleSet("type = IFCSLAB");
  await objectStateManager.addToVisibleSet("type = IFCSPACE");
accept: |
  - 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 [[page:reference/viewer3d|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.

```lesson {#lesson-4}
goal: Whenever the selection is replaced, frame the selected objects.
language: javascript
setup: model
check: fit
after: select type = IFCWINDOW AND buildingStorey.attribute.Name = "01 eerste verdieping"
starter: |
  objectStateManager.addSelectionListener({
  	setToSelectionSet: (selected, unselected) => {
  		console.log(selected.size + " object(s) selected");
  	}
  });
hints: |
  - The listener's `setToSelectionSet` receives the new selection, a `QidSet`, every time it is replaced.
  - `objectStateManager.viewFit(qids, {animate: true})` frames a set of objects in every viewer.
solution: |
  objectStateManager.addSelectionListener({
  	setToSelectionSet: (selected) => objectStateManager.viewFit(selected, {animate: true})
  });
accept: |
  - 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:

```example
name: viewer-selection
code: javascript/viewer-selection.js
run: none
css: javascript/viewer-embed.css
notes: |
  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.
highlight: 17-33
```

## 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](query-and-highlight.md), the last lesson.
