Build a model viewer

Four lessons in which you write the code of a 3D viewer application yourself — put the viewer on a page, load models, colour and select objects, highlight the result of a query — each round run and checked in a live viewer on the page.

This course builds a small web application around the 3D viewer, one step per lesson, and you write the code. Every lesson ends in rounds: the goal says what the viewer has to show, you edit the code until it does, and the page runs your code in a live 3D viewer, right under the editor, and the lesson's own solution next to it. The two are compared on what they leave behind in the viewer, never on the text of the code, so any code that gets there passes.

What you will learn#

Lesson You can do this afterwards
Embed the viewer Create a client, the two managers and a Viewer3d, put it on a page and choose its settings.
Load a model Load one or more versions, frame one of them, and replace what is loaded.
Select and colour Select from code, change colours and visibility, and react to what the user clicks.
Query and highlight Run a BQL query from the page and show its result in the viewer.

The lessons build on each other; follow them in order the first time. The BQL course is a good companion: the conditions you hand the viewer here are the WHERE of a BQL statement.

The four objects#

Every viewer application is built from the same four objects, created in this order:

  1. A BimWorksClient, from a BimWorksClientFactory and your token. It talks to the server.
  2. A ModelManager, which knows which versions are loaded and which qids belong to which model.
  3. An ObjectStateManager, which holds the state of every object that is not the geometry: visible or hidden, coloured or original, selected or not. It has no user interface of its own.
  4. A Viewer3d, which renders what the ObjectStateManager says and reports what the user does back to it.

The split is what makes several views agree: a second Viewer3d, or a Viewer2d, connected to the same ObjectStateManager shows the same selection and colours without any code of yours. Geometry and the viewers explains the design.

How a round works#

A round is the block with Goal at its top. Under the goal it says what is ready before your code runs: in the first lesson nothing at all, in the second a client, the two managers and an empty viewer, from the third on the demo model as well, loaded. Your code is the body of an async function, so await works anywhere in it and return hands a value back.

  • Run (or Ctrl+Enter) runs your code in the viewer that opens in the round, and the lesson's solution next to it, out of sight. PASSED means your code left the viewer in the state the goal describes; TRY AGAIN says what is different: no model loaded, 12 objects selected where the solution selects 205, the walls in another colour, the camera not fitted. An error stops the code and comes back with its message and the line it happened on, and what your code writes with console.log shows under the viewer.
  • Hint reveals one hint at a time.
  • Reset puts the starter code back.

Before every run the viewer goes back to the round's starting point: what the previous run loaded, coloured, hid, selected or listened to is undone. A round checks only what its goal is about — the viewer itself, the models, the selection, the colours, the visibility, the camera or the value your code returns — so code that also does something else still passes. The solution stays hidden until you have run wrong code twice.

The code uses the names a page of your own would have. The classes of the packages are there as they are (BimWorksClientFactory, ModelManager, ObjectStateManager, Viewer3d, ColorChangeMap, QidSet); BIM_WORKS_ADDRESS is your instance, BIM_WORKS_API_TOKEN the demo API token of your account, and BIM_WORKS_FIRST_MODEL and BIM_WORKS_SECOND_MODEL the version UUIDs of the two demo models, IFC Schependomlaan.ifc and the AC11 Institute; document.body is the page the viewer sits on. In your own project the classes are imports and the placeholders your own values; the first lesson shows the page around the code.

Note

The rounds run in your own instance with the demo API token BIM.works keeps in this browser when you sign in. Open BIM.works once in this browser and they work; without it the goals, the hints and the solutions are still there to read.

Your progress is kept in this browser (per instance), not on the server: the dot next to each lesson in the rail shows what is passed, in progress or untouched, and the panel at the top of this page counts the rounds and can forget them again.

For a page of your own#

The rounds need nothing from you. A page of your own needs an API token for an account that can read your models (Create an API token) and the JavaScript packages (JavaScript and TypeScript).

Warning

The lessons create the client with createClientWithApiToken in the page, which is fine while the page is yours alone. A page other people open must never contain the API token: your backend keeps it and gives each visitor a short-lived versions token, and the page creates its client with createClientWithAuth. Show your users' models in your web app builds that; everything the lessons do afterwards works the same on either client.

Note

A BIM.works instance does not serve the viewer's modules for other pages to import, so a page of your own bundles the npm packages or serves them from its own site (how-to/viewer3d/embed-the-viewer#same-origin-module-urls-and-workers). A file opened from disk cannot import them.

The viewer how-to guides pick up where the course ends, one task each.

Pages#

  • Embed the viewerCreate the client, the ModelManager, the ObjectStateManager and a Viewer3d, initialise it, put it on the page and choose its settings.
  • Load a modelResolve a version, load it through the ModelManager, load a second model and frame it, and replace what is loaded.
  • Select and colourSelect objects from code with a BQL condition, change colours, hide and show objects, and react to what the user selects.
  • Query and highlightRun a BQL query from the page, colour the objects it returns, dim the rest, show only the result and frame it.