How-to guide·3D viewerJavaScript

Measure areas, angles and with the laser

Start the distance, path, point, area, angle and laser measurements from code, draw stored measurements again, and listen to what the user measures.

The viewer's measuring object starts the interactive tools the BIM.works app has in its ruler menu: distance, path and point; area, of a polygon you click or of a face you hover, gross, net or as its hull; angle, at a vertex; and the 3-axis laser and vertical laser, which fire from a face and label the distance to whatever they hit. How each tool behaves for the user, click by click, is on Measure in the 3D viewer; this guide is about driving them from code.

Live demo#

MeasuringLive demo
Loads the demo model in this page with the demo API token of your account: open BIM.works and sign in once in this browser.

Start with Area: click the corners of a floor or a wall, Enter finishes; or hover a face and click once to take its polygon. Press O to cycle the area between gross, net (openings subtracted) and hull. Angle needs exactly three clicks. With 3-axis laser, move over a wall or a floor: the beams follow the surface under the cursor and read the room around it, and a click puts the laser down. Hover a placed laser to get its delete button and the chevrons that step a beam to the next surface behind the one it stopped at; drag a chevron along the beam to skip several. Every finished measurement is listed in the panel.

Before you start#

You have a viewer with a model loaded (Embed the viewer). The lasers measure the triangles of the model, which the viewer reads back from the GPU on first use; set preLoadTriangles in the viewer settings to load them with the model instead, so the first laser is immediate (reference/viewer3d#preloadtriangles).

Steps#

  1. Start a tool from a button or a menu of your own:

    const factory = new BimWorksClientFactory("https://bim.works");
    const client = await factory.createClientWithApiToken("<your API token>");
    const versionUuid = "<version uuid of the first demo model>";
    
    const modelManager = new ModelManager(client);
    const objectStateManager = new ObjectStateManager(modelManager);
    const viewer = new Viewer3d(objectStateManager);
    
    const toolbar = document.createElement("div");
    const startDistanceMeasurementButton = document.createElement("button");
    startDistanceMeasurementButton.innerHTML = "Start Distance";
    const startPathMeasurementButton = document.createElement("button");
    startPathMeasurementButton.innerHTML = "Start Path";
    const startAreaMeasurementButton = document.createElement("button");
    startAreaMeasurementButton.innerHTML = "Start Area";
    const startAngleMeasurementButton = document.createElement("button");
    startAngleMeasurementButton.innerHTML = "Start Angle";
    const startObjectDistanceMeasurementButton = document.createElement("button");
    startObjectDistanceMeasurementButton.innerHTML = "Start Object distance";
    const startLaserMeasurementButton = document.createElement("button");
    startLaserMeasurementButton.innerHTML = "Start 3-axis laser";
    const startVerticalLaserMeasurementButton = document.createElement("button");
    startVerticalLaserMeasurementButton.innerHTML = "Start vertical laser";
    const stopMeasurementButton = document.createElement("button");
    stopMeasurementButton.innerHTML = "Stop";
    const clearMeasurementsButton = document.createElement("button");
    clearMeasurementsButton.innerHTML = "Clear";
    
    toolbar.appendChild(startDistanceMeasurementButton);
    toolbar.appendChild(startPathMeasurementButton);
    toolbar.appendChild(startAreaMeasurementButton);
    toolbar.appendChild(startAngleMeasurementButton);
    toolbar.appendChild(startObjectDistanceMeasurementButton);
    toolbar.appendChild(startLaserMeasurementButton);
    toolbar.appendChild(startVerticalLaserMeasurementButton);
    toolbar.appendChild(stopMeasurementButton);
    toolbar.appendChild(clearMeasurementsButton);
    
    document.body.style["flex-direction"] = "column";
    document.body.appendChild(toolbar);
    
    await viewer.initializeViewer();
    document.body.appendChild(viewer.container);
    
    startDistanceMeasurementButton.addEventListener("click", () => {
    	viewer.measuring.startDistanceMeasurement();
    });
    
    startPathMeasurementButton.addEventListener("click", () => {
    	viewer.measuring.startPathMeasurement();
    });
    
    startAreaMeasurementButton.addEventListener("click", () => {
    	viewer.measuring.startAreaMeasurement();
    });
    
    startAngleMeasurementButton.addEventListener("click", () => {
    	viewer.measuring.startAngleMeasurement();
    });
    
    // Click two objects (or select two first): the shortest distance between their surfaces is
    // drawn between the two closest points. measureObjectDistance(qidA, qidB) computes the same
    // thing without interaction
    startObjectDistanceMeasurementButton.addEventListener("click", () => {
    	viewer.measuring.startObjectDistanceMeasurement();
    });
    
    // The laser measures against the triangles of the model, so the first call reads them back
    // from the GPU: it is async, and the viewer option preLoadTriangles avoids the wait
    startLaserMeasurementButton.addEventListener("click", async () => {
    	await viewer.measuring.startLaserMeasurement();
    });
    
    startVerticalLaserMeasurementButton.addEventListener("click", async () => {
    	await viewer.measuring.startVerticalLaserMeasurement();
    });
    
    stopMeasurementButton.addEventListener("click", () => {
    	viewer.measuring.stopActiveMeasurement();
    });
    
    clearMeasurementsButton.addEventListener("click", () => {
    	viewer.measuring.removeAllMeasurements();
    });
    
    client.fs.getNodeVersion(versionUuid).then((version) => {
    	modelManager.addModels([version]);
    });
    

    One button per measurement kind, plus stop and clear. The two laser calls are async — the first one reads the triangles back from the GPU, which the preLoadTriangles viewer option does with the model instead. The measuring object also has addAreaMeasurement(id, coordinates, holes, mode) and addAngleMeasurement(id, coordinates) to draw measurements from stored coordinates, and addListener for the committed, selected and deleted events.

    Call Tool
    startDistanceMeasurement(), startPathMeasurement(), startPointMeasurement() A distance between two points, a path through several, a single point.
    startAreaMeasurement() The area of a polygon: the first three points fix a plane, later points snap to it, Enter finishes with three or more corners; or a face of the model, with its openings, in one click. setAreaMode chooses gross, net or hull.
    startAngleMeasurement() Three points, the angle in degrees between 0 and 180 at the middle one; finishes on the third click.
    startLaserMeasurement() The 3-axis laser: six beams from a point on a face, both ways along the face normal and the two axes of the model's bounding box projected onto the face. Async.
    startVerticalLaserMeasurement() The same instrument with only the vertical axis, for a floor-to-ceiling height. Async.
    startObjectDistanceMeasurement() The shortest distance between two objects (Measure the distance between two objects).
  2. Restore stored measurements. addDistanceMeasurement, addPathMeasurement, addAreaMeasurement(id, coordinates, holes, mode) and addAngleMeasurement(id, coordinates) draw a measurement from coordinates you kept, without user input.

  3. Listen. addListener reports the committed, selected and deleted events; every measurement is an object with an id that can be listed, selected and removed from code. getActiveTool() says which tool runs, setSnapping switches vertex snapping.

What every tool shares#

Vertex snapping (Shift toggles it), Enter finishes, Escape cancels, Delete removes the selected measurement. The points of a finished distance, path, point, area or angle measurement can be dragged to a new position (a distance measured along a face's normal stays along that normal). viewer.viewFitMeasurement(measurement) fits the camera on a measurement, the Zoom to of the app's list.

A click tool of your own#

Three viewer calls are enough for a tool that waits for a click on the model, such as placing a note:

  • viewer.setClickHandler(handler) takes the clicks: while it is set, a click calls handler({qid, shiftKey}) instead of changing the selection (qid is null over empty space), and the object under the pointer is highlighted as a target. viewer.clearClickHandler() hands the clicks back to the selection.
  • await viewer.pick(x, y) returns what is under a canvas position in CSS pixels, without selecting it: the qid, the coordinate hit and the face normal (both null when no surface was hit). The click handler is not given the position, so keep the last pointer position from your own pointermove listener on the canvas and pick there.
  • viewer.selectVertex(callback) arms the vertex and edge snapping of the measuring tools, with its hover dot, for one click. The callback gets the snapped point as position, in the coordinates of pick and of an annotation's position, and the edge it snapped onto (or null); a click with nothing to snap to calls it with null and leaves it armed. cancel() on the returned object disarms it.

Limits#

An area whose ring self-intersects is drawn but its label is hidden. A corner that is snapped off the plane of the polygon is rejected. The net mode needs openings in the polygon and the hull mode a concave ring; otherwise the mode reads back as gross. A laser measures the triangles of the model, so the first one after a model loads waits for that data unless preLoadTriangles is on; and, unlike an area or an angle, it has no coordinates to store and restore — it is placed by pointing, not from code.

Check#

Start an area, click three corners of a floor and press Enter: the area appears at the centroid in m², and the listener receives the committed measurement.

Since 3.0