How-to guide·3D viewerJavaScript

Align PDF floor plans

Put PDF floor plans under the building in the 3D viewer — two points clicked on the sheet and in the model, in two viewers side by side, give the alignment of one plan, and the storey elevations place the plans of the other floors.

A PDF uploads like a model, and the 3D viewer shows it as a flat sheet: the page at its own size, in millimetres, at the origin. To put a floor plan under the building, the sheet needs an Alignment, one matrix stored on its Version that scales it to the building's millimetres, turns it and moves it onto its floor. This guide computes that matrix from two points clicked on the sheet and in the model, in two viewers side by side, and then places the plans of the other floors from the storey elevations, without clicking again.

Live demo#

The plan of floor 1 next to the S1 buildingLive demo
Uses the S1 floor plans (S1/PDF) and the S1 building in the Demo folder of your account with its demo API token: open BIM.works and sign in once in this browser. The demo stores the alignment of the plans in your Demo folder; Clear alignments clears it.

Pick V1, then click a corner of the building on the plan and the same corner in the model; Pick V2 does the same for a second corner, far from the first. Align stores the matrix on the plan and loads the plan under floor 01. Place the other floors gives the plans of floors 2 to 5 the same matrix, raised to their storeys, and shows the building up to floor 05. Plan, 3D and Floor change the right-hand view.

Before you start#

Steps#

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

// Two viewers, each with a ModelManager and an ObjectStateManager of its own: the sheet on the left,
// the building on the right
async function createViewer(elementId) {
	const modelManager = new ModelManager(client);
	const objectStateManager = new ObjectStateManager(modelManager);
	const viewer = new Viewer3d(objectStateManager);
	await viewer.initializeViewer();
	document.getElementById(elementId).appendChild(viewer.container);
	return {viewer, modelManager, objectStateManager};
}
const sheetView = await createViewer("sheet");
const buildingView = await createViewer("building");

// The first PDF is the plan of floor 1, the others are the plans of the floors above it
const [sheet, ...otherSheets] = await Promise.all(["<version uuid of a PDF>"].map((vid) => client.fs.getNodeVersion(vid)));
const building = await client.fs.getNodeVersion("<version uuid of the S1 demo model>");

// The points are picked on the sheet as drawn: clear an alignment stored earlier, it would move the sheet
await client.geometry3d.setAlignment(sheet.uuid, null);
await sheetView.modelManager.addModels([sheet]);
await buildingView.modelManager.addModels([building]);

// The storeys give the height of each floor, in millimetres
const {buildingStoreys} = await client.bim.listBuildingStoreys([building.uuid]);
const elevationOf = (floor) => buildingStoreys.find((storey) => storey.floor === floor).elevation;

// Waits for a click on a point in the viewer; a click on nothing calls back with null and keeps waiting
function pickPoint(view) {
	return new Promise((resolve) => {
		view.viewer.selectVertex((vertex) => {
			if (vertex) {
				// The point in the model's own millimetres, before its alignment
				resolve(vertex.withoutAlignment);
			}
		});
	});
}

// Two pairs: a corner clicked on the sheet and the same corner in the model, then a second one far from it
const pairs = [];
for (let i = 0; i < 2; i++) {
	const [onSheet, inModel] = await Promise.all([pickPoint(sheetView), pickPoint(buildingView)]);
	pairs.push({onSheet, inModel});
}

// The sheet is flat at z = 0. In the model the floor's height replaces the height of the clicked corners,
// so the sheet lies flat on its floor, 20 mm above it so the floor does not cover it
const floor = 1;
const level = elevationOf(floor) + 20;
const [one, two] = pairs;
const alignment = await client.geometry3d.calculateAlignment(
	[one.inModel[0], one.inModel[1], level], [one.onSheet[0], one.onSheet[1], 0],
	[two.inModel[0], two.inModel[1], level], [two.onSheet[0], two.onSheet[1], 0]);

// Store it on the sheet, then load the sheet next to the building: it lands on its floor
await client.geometry3d.setAlignment(sheet.uuid, alignment);
await buildingView.modelManager.addModels([sheet]);

// The other floors are drawn on the same paper at the same scale: the same matrix, raised by the difference
// in storey elevation. The drawing number names the floor: "UO103 Plattegrond verdieping 3.pdf" is floor 3
for (const other of otherSheets) {
	const otherFloor = Number(/^UO1(\d\d)/.exec(other.node_name)[1]);
	const raise = mat4.fromTranslation(mat4.create(), [0, 0, elevationOf(otherFloor) - elevationOf(floor)]);
	// First the alignment of floor 1, then up
	const matrix = mat4.multiply(mat4.create(), raise, mat4.fromValues(...alignment));
	await client.geometry3d.setAlignment(other.uuid, matrix);
	await buildingView.modelManager.addModels([other]);
}

A page with a <div id="sheet"> and a <div id="building"> side by side, mat4 from @bimworks/viewercommon. The first PDF is the plan of floor 1; the clicks happen in the order of the loop, any viewer first.

  1. Two viewers. Each viewer gets a ModelManager and an ObjectStateManager of its own, so the left one loads only the sheet and the right one only the building. Two viewers on one ObjectStateManager show the same models.
  2. Start from the sheet as drawn. Clear the sheet's alignment before loading it (setAlignment(vid, null)): a stored one moves the sheet in the left viewer too, and the clicks would land on the moved sheet.
  3. Pick the points. viewer.selectVertex(callback) waits for one click on a model and calls back with the point, snapped to a nearby vertex or edge. withoutAlignment is the point in the model's own millimetres, before its alignment: what CalculateAlignment takes. A click on nothing calls back with null and keeps waiting; cancel() on the returned selector stops it. Pick the second pair far from the first: the rotation and the scale come from the line between the two.
  4. Compute the matrix. client.geometry3d.calculateAlignment(point1a, point1b, point2a, point2b) returns the matrix that maps the b points, on the sheet, onto the a points, in the model. With two pairs it moves, turns and scales: the scale is the drawing's, 50 for a plan at 1:50, because the sheet is paper in millimetres. The sheet lies at z = 0; give the model's points the height of the floor, so the sheet lies flat on it whatever height the clicked corners have.
  5. Store it and show the sheet. setAlignment(vid, matrix) stores the matrix on the version; a viewer that loads the version afterwards places it with it. A viewer that already has the version loaded keeps the old placement until viewer.updateModel(vid) or a reload.
  6. Place the other floors. Sheets of one set share the paper, the scale and the place of the building on the page, so the plan of another floor needs the same matrix raised by the difference in elevation: mat4.multiply(out, raise, alignment) applies the alignment first, then the translation. listBuildingStoreys gives each storey's floor number and its elevation in millimetres (Show building storeys); the drawing number names the floor a sheet shows.

A sheet on a storey plan#

setBuildingStoreysVisibleExclusive hides every object outside the storeys, the sheets included: their objects belong to no storey, and a PDF has no Qids to show them again with. To show a storey with its sheet, hide the building's other objects instead and leave the sheets alone:

JavaScript#
const onStorey = buildingView.modelManager.getObjectQidsForBuildingStoreyQids(storeyQids, false);
const others = [...buildingView.modelManager.getModel(building.uuid).qids].filter((qid) => !onStorey.has(qid));
await buildingView.objectStateManager.removeFromVisibleSet(others);
// one sheet at a time: the others stay loaded
buildingView.viewer.setModelVisible(otherSheet.uuid, false);

Hide the slabs (IFCSLAB) and ceilings (IFCCOVERING) on a plan seen from above, or they cover the sheet (Show a floor plan).

Limits#

  • The other floors line up only when their sheets have the layout and the scale of the aligned one. A sheet laid out differently, or at another scale, needs points of its own.
  • A sheet is the whole page, title block and margins included, so it reaches well beyond the building.
  • Two pairs cannot mirror or skew a sheet. A third pair (point3a, point3b) gives CalculateAlignment a full linear transform (Set the alignment).
  • The BIM.works UI has the same tool for any two models: Align models, with the alignment in the node popup (MODEL_ALIGNMENT, Alignment).

Check#

The sheet's walls lie under the model's walls in the plan of its floor, and in 3D every sheet lies on its own floor. GetAlignment on a placed sheet returns the stored matrix, with the floor's elevation plus 20 mm in element 14.