Tutorial·Build a model viewerJavaScript

Query and highlight

Run a BQL query from the page, colour the objects it returns, dim the rest, show only the result and frame it.

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 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.

GoalDemo model · Schependomlaan
Return the number of external walls, the walls whose IsExternal property is true.
Ready to use: client, modelManager, objectStateManager, viewer and version, the demo model, loaded.
const walls = await client.bim.queryQids([version.uuid], "type = IFCWALL");
return walls.size;
Running the code needs JavaScript in this page; the hints and the solution are below.
Hints
  1. Only the condition has to change: AND adds a second one.
  2. property.*.IsExternal = true matches the property in whichever set carries it.
Show solution
const walls = await client.bim.queryQids([version.uuid], "type = IFCWALL AND property.*.IsExternal = true");
return walls.size;
Also accepted
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 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).

GoalDemo model · Schependomlaan
Highlight the doors of the ground floor: colour them orange, #FF6D00, turn everything else light grey and half transparent, #C8C8C880, and frame the doors.
Ready to use: client, modelManager, objectStateManager, viewer and version, the demo model, loaded.
const doors = await client.bim.queryQids([version.uuid], 'type = IFCDOOR AND buildingStorey.attribute.Name = "00 begane grond"');
await objectStateManager.changeColor(doors, "#FF6D00");
Running the code needs JavaScript in this page; the hints and the solution are below.
Hints
  1. Dim everything first, then colour the doors: the later changeColor wins for the objects both cover.
  2. modelManager.getAllQids() is every object of the loaded models.
  3. await objectStateManager.viewFit(doors, {animate: true}) frames the doors.
Show 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});
Also accepted
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.

GoalDemo model · Schependomlaan
Show only the windows of the first floor, 01 eerste verdieping, and frame them.
Ready to use: client, modelManager, objectStateManager, viewer and version, the demo model, loaded.
const windows = await client.bim.queryQids([version.uuid], 'type = IFCWINDOW AND buildingStorey.attribute.Name = "01 eerste verdieping"');
await objectStateManager.viewFit(windows, {animate: true});
Running the code needs JavaScript in this page; the hints and the solution are below.
Hints
  1. Framing the windows does not hide anything else.
  2. await objectStateManager.setVisibleSet(windows) shows exactly the windows and hides the rest.
Show 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});
Also accepted
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.

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

const modelManager = new ModelManager(client);
const objectStateManager = new ObjectStateManager(modelManager, {multiSelect: true});
const viewer = new Viewer3d(objectStateManager);
await viewer.initializeViewer();

// A toolbar with a query box, a Run button and a status line
const toolbar = document.createElement("div");
toolbar.classList.add("toolbar");
const input = document.createElement("input");
input.value = 'type = IFCDOOR AND buildingStorey.attribute.Name = "00 begane grond"';
const run = document.createElement("button");
run.textContent = "Run";
const status = document.createElement("span");
toolbar.append(input, run, status);
document.body.appendChild(toolbar);
document.body.appendChild(viewer.container);

const version = await client.fs.getNodeVersion("<version uuid of the first demo model>");
await modelManager.addModels([version]);
// Spaces are hidden by default; show them so a query on IFCSPACE has something to highlight
await objectStateManager.addToVisibleSet("type = IFCSPACE");

const highlight = async (condition) => {
	try {
		// Only the qids come back: that is all the ObjectStateManager needs
		const qids = await client.bim.queryQids(modelManager.getVersionUuidsArray(), condition);
		status.textContent = qids.size + " object(s)";
		// Dim everything, then colour the matches: the second call wins for those objects
		await objectStateManager.changeColor(modelManager.getAllQids(), "#C8C8C880");
		if (qids.size == 0) {
			return;
		}
		await objectStateManager.changeColor(qids, "#FF6D00");
		await objectStateManager.setToSelectionSet(qids);
		await objectStateManager.viewFit(qids, {animate: true});
	} catch (error) {
		status.textContent = "Invalid query: " + (error.message || error);
	}
};

run.addEventListener("click", () => highlight(input.value));
input.addEventListener("keydown", (event) => {
	if (event.key == "Enter") {
		highlight(input.value);
	}
});
await highlight(input.value);
html, body {
	height: 100%;
	margin: 0;
}

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

.toolbar {
	display: flex;
	align-items: center;
	gap: 8px;
	padding: 8px;
	background: #eeeeee;
}

.toolbar input {
	flex: 1;
	font-family: monospace;
	padding: 4px 6px;
}

.viewer3d {
	flex: 1;
	min-height: 0;
}

.viewer3d canvas {
	outline: none;
}

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.

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: Select and colour objects, Navigation modes and the rest of the 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.