Tutorial·JavaJava

Java quick start

Add the client to a Maven project, log in with an API token, list your tree, upload a model and query it, in one program.

Classic SDK

This tutorial uses the classic, hand-written Java SDK. For a new integration, the Java SDK generated from the OpenAPI document is the choice; its reference has a complete example for every operation (SDKs compares the two).

The Java client talks to BIM.works over its own transport (HTTP by default, HTTP/2 where the server offers it over HTTPS; WebSocket only when you pass new ClientSettings(ConnectionType.WEB_SOCKET) to the factory), not over the OpenAPI, and covers the whole API. This page takes you from an empty Maven project to a program that uploads a model and queries it. Java 17 or later is required.

Install#

The client is nl.bimbase.bimworks:client, published on the BIM.works Maven repository, which needs the credentials your BIM.works contact gives you. Use the version of your server (the version pill at the top of this page is the server's).

XML#
<dependencies>
    <dependency>
        <groupId>nl.bimbase.bimworks</groupId>
        <artifactId>client</artifactId>
        <version>3.0.0-beta.192</version>
    </dependency>
</dependencies>
<repositories>
    <repository>
        <id>BIM.works</id>
        <url>https://maven.bim.works/artifactory/BIM.works</url>
    </repository>
</repositories>

The credentials go in ~/.m2/settings.xml under a <server> with the same id. When the client's Jetty clashes with your own, use clientshaded instead, which relocates it; it does not relocate Jackson or Guava. Java has the details, the Gradle form and the legacy client.

Log in#

import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Set;
import java.util.UUID;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;

import nl.bimbase.bimworks.client.BimWorksClient;
import nl.bimbase.bimworks.client.BimWorksClientFactory;
import nl.bimbase.bimworks.client.TreeNode;
import nl.bimbase.bimworks.client.UploadModelResponse;
import nl.bimbase.bimworks.client.upload.UploadBuilder;

public class QuickStart {

	public static void main(String[] args) throws Exception {
		Path model = Paths.get("path/to/model.ifc");

		// One factory per instance, one client per login; both close with the try block
		try (BimWorksClientFactory factory = new BimWorksClientFactory("https://bim.works");
				BimWorksClient client = factory.createClientWithApiToken("<your API token>")) {

			ObjectNode version = client.getApplicationVersion();
			System.out.println("Connected to BIM.works " + version.get("version_string").asText());

			// The root of an API token is the node it was created on; paths are relative to it
			UUID rootUuid = client.fs.getRootNodeUuid();
			ObjectNode listing = client.fs.listNodes(rootUuid, false);
			for (JsonNode node : listing.get("nodes")) {
				System.out.println((node.get("folder").asBoolean() ? "folder " : "model  ") + node.get("name").asText());
			}

			// Upload into a folder: a model node named after the file is created (or a new version added)
			UUID folderUuid = client.fs.createNodeIfNotExists(rootUuid, "Quick start", true, null);
			UploadModelResponse upload = client.upload.uploadModel(new UploadBuilder(folderUuid).fromPath(model));
			UUID versionUuid = upload.getNewVersionUuid();
			TreeNode modelNode = upload.getTreeNode();
			System.out.println("Uploaded " + modelNode.getName() + " as version " + versionUuid);

			// Query the new version: one row per object, every value wrapped in {"value": ...}
			ArrayNode rows = client.bim.query("GET qid, attribute.Name, quantity.*.Height UNIT m WHERE type = IFCDOOR LIMIT 10", versionUuid);
			for (JsonNode row : rows) {
				System.out.println(row.get("qid").asText() + " " + row.get("attribute").get("Name").get("value").asText());
			}
			long doors = client.bim.queryCount("GET qid WHERE type = IFCDOOR", Set.of(versionUuid));
			System.out.println(doors + " doors");
		}
	}
}

One BimWorksClientFactory per instance and one BimWorksClient per login; both are AutoCloseable, so the try-with-resources closes the connection when the program ends. The upload waits until the server has stored the file and created the version, so the query on the next line finds a fully indexed model.

factory.createClientWithApiToken(token) logs in and returns a client, or throws BimWorksException with an error code (AuthenticationError.INVALID_API_TOKEN and friends) when it cannot. The client can be as short- or long-lived as you need; the sub-APIs (client.fs, client.upload, client.bim, client.tokens, client.modelChecks, client.gis, …) are fields on it. client.getApplicationVersion() returns the server version as JSON and is a good first call to confirm the address.

List the tree#

client.fs.getRootNodeUuid() is the node your token was created on; every path is relative to it, so client.fs.getNodeByPath("/") is the same node as a TreeNode. client.fs.listNodes(uuid, false) returns the children in a nodes array of JSON objects (the false leaves deleted nodes out); a node that holds a model has last_version_uuid. createNode(parentUuid, name, isFolder) adds a folder or an empty model node; createNodeIfNotExists does the same without failing when the name is taken.

Upload#

client.upload.uploadModel(new UploadBuilder(nodeUuid).fromPath(path)) uploads a file into a folder node; a new model node named after the file is created in it, or a new version when a model with that name exists. The builder also takes a File, an InputStream with its size, a byte[], or a URL the server downloads itself (urlPassThrough, for files on S3 or Azure), and options such as forceReprocessing, validationLevel and a progressListener. The call blocks until the server has processed the upload and returns an UploadModelResponse; getNewVersionUuid() is the version to query and view. A duplicate file is detected by hash and not transferred again.

Query#

client.bim.query(bql, versionUuid...) runs a BQL statement and returns the rows as a Jackson ArrayNode, one object per row, with every feature under its path and every value wrapped in an object with a value field (and a unit when it has one). client.bim.queryCount(bql, Set.of(versionUuid)) returns only the number of matching objects, client.bim.queryQids(bql, versions) only their qids, and client.bim.validate(bql) tells you whether the server accepts a query without running it. The BQL course teaches the language.

Where to go from here#

The same client runs checks (client.modelChecks.runChecks, runIdsCheck), downloads BCF (client.modelChecks.downloadCheckResultsAsBcf), mints tokens for browsers (client.tokens.createVersionsToken), reads and writes geo references (client.gis) and exports IFC, glTF and CityGML. The Java reference lists every type; the how-to guides show one task at a time, starting with Upload a model and Run a BQL query.