Export to glTF
Export a model, or the objects a BQL condition selects, as binary glTF (GLB) for three.js, Blender, game engines and AR viewers; what is in the file, the one-time download URL, several models in one file, and exporting through a versions token.
BIM.works exports the triangulated geometry it generated for a model as glTF 2.0 in the binary container (GLB), the format three.js, Babylon.js, Blender, Unity, Unreal and most AR viewers read. A BQL condition limits the export to some of the objects.
Live demo#
Each export runs on the server and is fetched once. The preview is drawn from the file's own triangles and vertex colours, the panel reads its JSON chunk, and Save stores the bytes. Export picks the objects; BQL… takes a condition of your own. Through a versions token does the same export on a second client that has nothing but geometry:read on this one model.
Before you start#
A client whose credential has the geometry:read scope on the version, and the Vid of a processed IFC version. The export uses the geometry BIM.works stored at check-in; a point cloud or a PDF gives an empty file.
1. Export#
const url = await client.geometry3d.downloadGltf(["<version uuid of the first demo model>"], "type != IFCSPACE");
const glb = await (await fetch(url)).arrayBuffer();import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.Set;
try (InputStream glb = client.geometry3d.downloadGltf(Set.of(UUID.fromString("<version uuid of the first demo model>")), false, "type != IFCSPACE")) {
Files.copy(glb, Paths.get("model.glb"), StandardCopyOption.REPLACE_EXISTING);
}The condition is the WHERE part of a query, with or without GET qid WHERE in front of it: type = IFCDOOR and GET qid WHERE type = IFCDOOR export the same doors. Leave it out to export every object with geometry, the spaces included, which most viewers then draw as large translucent volumes; type != IFCSPACE leaves them out. A condition that selects nothing gives a valid file without meshes. Check a condition with Validate a query first: one the server cannot run fails the export.
In JavaScript, downloadGltf on Geometry3d answers a URL. In Java, downloadGltf on Geometry3dApi answers the stream itself and takes one more argument, onlyOutsideTriangles: true keeps only the triangles on the outside of the model, for a lighter shell. The .NET client has no glTF export.
Over HTTP the operation is ExportGeometry, which answers the file itself (model/gltf-binary) rather than a URL: models, the condition as query, only_outside_triangles, and colors, a colour per object (qid → [r, g, b, a], each 0 to 1) instead of the model's own.
Bearer <auth token>Authorization: Bearer <auth token>
Accept: application/jsoncurl -X POST "$BIMWORKS_URL/openapi/model/ExportGeometry" \
-H "Authorization: Bearer $BIMWORKS_TOKEN"const factory = new BimWorksClientFactory("https://bim.works");
const client = await factory.createClientWithApiToken("<your API token>");
// The objects a BQL condition selects, as binary glTF (GLB); leave the condition out for
// every object with geometry, spaces included. The answer is a download URL
const url = await client.geometry3d.downloadGltf(["<version uuid of the first demo model>"], "type != IFCSPACE");
// The URL needs no credentials and serves the file once: fetch it and keep the bytes
const response = await fetch(url);
const glb = await response.arrayBuffer();
console.log(response.headers.get("content-disposition"), glb.byteLength + " bytes");
// In a browser: offer the bytes as a file
const link = document.createElement("a");
link.href = URL.createObjectURL(new Blob([glb], {type: "model/gltf-binary"}));
link.download = "model.glb";
link.textContent = "Download the glTF";
document.body.appendChild(link);
Fetches the GLB once and offers it as a file. In Node.js, write the bytes with fs.writeFile instead.
2. Use the download URL once#
The URL points at <instance>/download?identifier=… and is a capability: it needs no credentials and answers to any origin, so a page can fetch it or a browser can open it. It serves the file once; a second request answers 404. Fetch it and keep the bytes, as the examples do, or export again. The Content-Disposition header names the file after the model, <file name>.glb.
3. Know what is in the file#
| Part | What |
|---|---|
| Nodes | A root node Axis Rotation turns the model's Z-up into glTF's Y-up. Under it, one node per model, named after the model's file, and under each the meshes, scaled by 0.001 because the coordinates are millimetres: a loader that applies the node transforms gets metres, Y up. |
| Meshes | All objects merged into a few meshes, split by transparency (alphaMode OPAQUE or BLEND) and by whether the geometry is two-sided. Triangles are not indexed. |
| Colours | The model's own colours as vertex colours (COLOR_0), with a plain material per mesh. |
| Objects | Not separate: the file has no per-object nodes or ids, so it cannot be picked by object. For a viewer that knows the objects, use the 3D viewer (Embed the viewer). |
const factory = new BimWorksClientFactory("https://bim.works");
const client = await factory.createClientWithApiToken("<your API token>");
// One GLB per IFC model in the root folder of the credential
const rootUuid = await client.fs.getRootNodeUuid();
const listing = await client.fs.listNodes(rootUuid);
for (const node of listing.nodes) {
if (node.folder || !node.last_version_uuid || !node.name.toLowerCase().endsWith(".ifc")) {
continue;
}
const url = await client.geometry3d.downloadGltf([node.last_version_uuid]);
const glb = await (await fetch(url)).arrayBuffer();
console.log(node.name + ": " + glb.byteLength + " bytes");
}
// Several versions in one call give one GLB with a node per model, in their common frame
const both = await client.geometry3d.downloadGltf(["<version uuid of the first demo model>", "<version uuid of the second demo model>"], "type = IFCWALL");
Several versions in one call give one GLB with a node per model, each in its own coordinates, so models that share an origin line up. The file is named after one of the models, not always the first of your list.
4. Export through a versions token#
To let a browser export a model without giving it your credential, mint a versions token on your server with geometry:read on that version only and hand its auth to the page (Create a versions token). The page can export and read the footprint; a query is refused with NON_AUTHORIZED_SCOPE.
const factory = new BimWorksClientFactory("https://bim.works");
// On your server, with your own credential: a token that may only read the geometry of this
// version, for five minutes
const serverClient = await factory.createClientWithApiToken("<your API token>");
const token = await serverClient.tokens.createVersionsToken(
["<version uuid of the first demo model>"], 5 * 60, null, false, false, ["geometry:read"]);
// In the browser, with token.auth: the export works, a query is refused with NON_AUTHORIZED_SCOPE
const browserClient = await factory.createClientWithAuth(token.auth);
const url = await browserClient.geometry3d.downloadGltf(["<version uuid of the first demo model>"], "type IN (IFCDOOR, IFCWINDOW)");
const glb = await (await fetch(url)).arrayBuffer();
Both halves are in one file here; in an application the first runs on your server and the second in the browser.
In the app#
With EXPORT_GLTF, Export → GLTF... in a model's menu opens a dialog that exports the loaded, the visible or the selected objects of the 3D viewer, in their original colours or in the colours the viewer gives them now, as a one-time download.
Check#
The file starts with the four bytes glTF, and the Khronos glTF validator or a drop into any glTF viewer shows the model standing upright at its real size.