How-to guide·RenderingJavaScript

Render a screenshot on the server

Ask the server for an image of a model version from the viewpoint of the 3D viewer, which the camera gives in the metres the API works in.

For a thumbnail, a report image or a snapshot to attach to an issue, rendered from the same geometry the viewers show, on the server, without a browser. The server renders on a GPU or, where the instance is set up for it, on a software device; the image is the same, only the time differs (Configure the render backend).

Live demo#

Render the current viewpoint on the serverLive 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.

Set up a viewpoint in the viewer and press Render this viewpoint on the server. The image the server returns appears in the panel with the time it took, queue included. On a GPU instance that is a second or two; on a CPU instance, longer.

Steps#

  1. Take the camera's viewpoint: viewer.getCamera().getViewpoint() gives its position and direction in the measurement frame and in metres, which is what the API takes.
  2. Ask for the image with client.render.render(versionUuid, size, viewpoints); it resolves to the URL of the image.
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);

await viewer.initializeViewer();

document.body.style["display"] = "flex";
document.body.style["flex-direction"] = "column";

const buttonsDiv = document.createElement("div");
document.body.appendChild(buttonsDiv);

const bottomDiv = document.createElement("div");
bottomDiv.style["display"] = "flex";
bottomDiv.style["flex-direction"] = "row";
bottomDiv.style["flex"] = "1";
document.body.appendChild(bottomDiv);

const screenshotButton = document.createElement("button");
screenshotButton.innerText = "Snapshot";
screenshotButton.addEventListener("click", async () => {
	// The API takes metres in the measurement frame: getViewpoint's position, and the target
	const viewpoint = viewer.getCamera().getViewpoint();
	const position = viewpoint.position;
	const target = vec3.add(vec3.create(), viewpoint.position, viewpoint.direction);

	try {
		const url = await client.render.render(versionUuid, {
			pixelWidth: 300,
			pixelHeight: 300
		}, [{
			position: position,
			target: target
		}]);
		const image = document.createElement("img");
		image.style["width"] = "300px";
		image.style["height"] = "300px";
		image.style["margin"] = "10px";
		image.src = url;
		screenshotsInner.prepend(image);
	} catch (e) {
		console.error(e);		
	}
});
buttonsDiv.appendChild(screenshotButton);

const screenshotsDiv = document.createElement("div");
screenshotsDiv.style["overflow"] = "auto";
screenshotsDiv.style["display"] = "flex";
screenshotsDiv.style["flex-direction"] = "column";
screenshotsDiv.style["width"] = "320px";
screenshotsDiv.style["background-color"] = "#EEEEEE";
// The viewer takes the width the column of screenshots leaves
viewer.container.style["flex"] = "1";
bottomDiv.appendChild(viewer.container);
bottomDiv.appendChild(screenshotsDiv);

const screenshotsInner = document.createElement("div");
screenshotsInner.style["min-height"] = "min-content";
screenshotsInner.style["display"] = "flex";
screenshotsInner.style["flex-direction"] = "column";
screenshotsDiv.appendChild(screenshotsInner);

const version = await client.fs.getNodeVersion(versionUuid);
modelManager.addModels([version]).then(() => {
	// Done
});

Takes the viewpoint of the viewer's camera (metres, in the measurement frame, as the API works) and asks the server for a 300 × 300 image from it.

The report and BCF operations return images rendered the same way.

Over HTTP#

Render answers the PNG itself. Give a camera (a position and a target or direction, optionally up, in metres in the model's coordinates, as BCF has them) or a view, one of the named angles such as iso_front_right or top, which frames the whole model; size defaults to 300 × 300. With neither camera, view nor size it answers the screenshot the check-in stored, and renders the default view when there is none.

POST/openapi/model/RenderRender reference
Authorization
Bearer <auth token>
Authorization: Bearer <auth token>
Accept: application/json
curl -X POST "$BIMWORKS_URL/openapi/model/Render" \
  -H "Authorization: Bearer $BIMWORKS_TOKEN"
Send to see the response

AugmentBcf takes a BCF 2.1 file and a version_uuid and answers the file with a snapshot rendered for every viewpoint that has a perspective camera, for issues kept in your own system.

Limits#

On an instance that renders on the CPU an image takes ten to fifty times longer than on a GPU; plan for it when a report has many images.

Check#

The image shows the model from the same direction as the viewer, 300 by 300 pixels.

Since 3.0