---
title: Query and highlight
summary: Run a BQL query from the page, colour the objects it returns, dim the rest, show only the result and frame it.
order: 40
languages: [javascript]
terms: [bql, qid, object-state-manager]
see-also: [tutorials/javascript-viewer/select-and-colour, tutorials/bql/index, how-to/query/run-a-bql-query, how-to/viewer3d/select-and-colour-objects]
---

# Query and highlight

The last lesson joins the two halves of this site: a BQL query decides which objects matter, the ObjectStateManager shows them. The rounds start, as in the previous lesson, with the demo model loaded and `version` at hand.

## Two ways to run a query

`client.bim.queryQids(models, query)` returns only the qids of the matching objects, as a `QidSet`, whatever the query selects. It is the call to use when the result goes into the ObjectStateManager, because that is all the ObjectStateManager needs, and it is cheap: no feature values travel over the wire. A `QidSet` has a `size`.

`client.bim.query(models, query)` returns the rows, with every feature the `GET` asked for, as the [BQL course](../bql/index.md) showed. Use it when you want to display values next to the model, for instance in a table beside the viewer. Both take the same `models` argument, an array of version UUIDs; `modelManager.getVersionUuidsArray()` gives you the loaded ones, so a query always runs against what the viewer shows.

```lesson {#lesson-1}
goal: Return the number of external walls, the walls whose `IsExternal` property is `true`.
language: javascript
setup: model
check: result
starter: |
  const walls = await client.bim.queryQids([version.uuid], "type = IFCWALL");
  return walls.size;
hints: |
  - Only the condition has to change: `AND` adds a second one.
  - `property.*.IsExternal = true` matches the property in whichever set carries it.
solution: |
  const walls = await client.bim.queryQids([version.uuid], "type = IFCWALL AND property.*.IsExternal = true");
  return walls.size;
accept: |
  - const walls = await client.bim.queryQids(modelManager.getVersionUuidsArray(), "type = IFCWALL AND property.Pset_WallCommon.IsExternal = true");
    return walls.size;
  - const rows = await client.bim.query([version.uuid], "GET qid WHERE type = IFCWALL AND property.*.IsExternal = true");
    return rows.length;
```

When the values come from a user and the query text should stay fixed, use parameters rather than splicing the value into the text: `client.bim.queryQids(models, "type IN $types", false, {types: [...]})`, as [the BQL lesson on parameters](../bql/units-and-parameters.md) shows.

## Highlighting

The classic highlight dims the whole model first, then colours the matches, in two sequential `changeColor` calls: the second call wins for the objects it covers. `modelManager.getAllQids()` is every object of the loaded models. Then the camera frames the matches with `objectStateManager.viewFit(qids, {animate: true})`, and selecting them lets the user continue with the keyboard (`F` refits, `H` hides them).

```lesson {#lesson-2}
goal: Highlight the doors of the ground floor: colour them orange, `#FF6D00`, turn everything else light grey and half transparent, `#C8C8C880`, and frame the doors.
language: javascript
setup: model
check: colours, fit
starter: |
  const doors = await client.bim.queryQids([version.uuid], 'type = IFCDOOR AND buildingStorey.attribute.Name = "00 begane grond"');
  await objectStateManager.changeColor(doors, "#FF6D00");
hints: |
  - Dim everything first, then colour the doors: the later `changeColor` wins for the objects both cover.
  - `modelManager.getAllQids()` is every object of the loaded models.
  - `await objectStateManager.viewFit(doors, {animate: true})` frames the doors.
solution: |
  const doors = await client.bim.queryQids([version.uuid], 'type = IFCDOOR AND buildingStorey.attribute.Name = "00 begane grond"');
  await objectStateManager.changeColor(modelManager.getAllQids(), "#C8C8C880");
  await objectStateManager.changeColor(doors, "#FF6D00");
  await objectStateManager.viewFit(doors, {animate: true});
accept: |
  - const doors = await client.bim.queryQids(modelManager.getVersionUuidsArray(), 'type = IFCDOOR AND buildingStorey.attribute.Name = "00 begane grond"');
    const changes = new ColorChangeMap();
    changes.changeQids(modelManager.getAllQids(), "#C8C8C880");
    changes.changeQids(doors, "#FF6D00");
    await objectStateManager.changeColorMulti(changes);
    await objectStateManager.setToSelectionSet(doors);
    await objectStateManager.viewFit(doors);
```

## Only the result

Hiding everything except the result is the other classic: `objectStateManager.setVisibleSet(qids)` shows exactly those objects and hides the rest in one call, and `resetAll()` brings the rest back.

```lesson {#lesson-3}
goal: Show only the windows of the first floor, `01 eerste verdieping`, and frame them.
language: javascript
setup: model
check: visibility, fit
starter: |
  const windows = await client.bim.queryQids([version.uuid], 'type = IFCWINDOW AND buildingStorey.attribute.Name = "01 eerste verdieping"');
  await objectStateManager.viewFit(windows, {animate: true});
hints: |
  - Framing the windows does not hide anything else.
  - `await objectStateManager.setVisibleSet(windows)` shows exactly the windows and hides the rest.
solution: |
  const windows = await client.bim.queryQids([version.uuid], 'type = IFCWINDOW AND buildingStorey.attribute.Name = "01 eerste verdieping"');
  await objectStateManager.setVisibleSet(windows);
  await objectStateManager.viewFit(windows, {animate: true});
accept: |
  - const windows = await client.bim.queryQids([version.uuid], 'type = IFCWINDOW AND buildingStorey.attribute.Name = "01 eerste verdieping"');
    const others = modelManager.getAllQids();
    for (const qid of windows) {
    	others.delete(qid);
    }
    await objectStateManager.removeFromVisibleSet(others);
    await objectStateManager.viewFit(windows, {animate: true});
```

A query that matches nothing is a normal outcome: `qids.size` is `0`, and a page should say so instead of fitting the camera to an empty set. An invalid query rejects the promise with the server's error message; `client.bim.validateQuery(query)` checks a query without running it when you want to show an error while the user types.

## The whole page

The rounds as one page of your own: a text box takes a condition, and Run highlights what it matches.

```example
name: viewer-query-highlight
code: javascript/viewer-query-highlight.js
run: none
css: javascript/viewer-query-highlight.css
notes: |
  Type a BQL condition and press Run. The matching objects turn orange, everything else goes light grey and half transparent, the camera frames the result and the status shows how many objects matched. Try `type = IFCSPACE` (spaces are hidden by default; the example shows them first), `property.*.IsExternal = true`, or `quantity.*.Height > 2.5 m`.
highlight: 22-24,27-35
```

## Where to go from here

You have written the pieces of a page that logs in, loads models, follows the user's selection and answers BQL queries visually. The how-to guides take single features further: [[page:how-to/viewer3d/select-and-colour-objects]], [[page:how-to/viewer3d/navigation-modes]] and the rest of the [[page:how-to/viewer3d/embed-the-viewer|viewer guides]]. The 2D viewer connects to the same ObjectStateManager, so adding a floor plan next to the 3D view is a few lines; the how-to guides show it. And every query you write here can become a stored query in the app, where users run it without your page.

## You can now

- Run a BQL query from the page and get its result as qids or as rows.
- Highlight the result in the viewer and dim or hide the rest.
- Fit the camera to a set of objects.
- Handle empty results and invalid queries.
