Query multiple models
Run one query over several versions, tell the results apart by version, and select the models under a folder over HTTP.
For a project view that spans the architectural, structural and services models, or any question across a set of files. A query takes a list of versions and answers with one merged list of records; the qid of each record says which version it came from.
Before you start#
- A login with the
query:runscope and read rights on every version; a delegated token must name all of them (Create a versions token). - The version UUIDs (List the tree).
1. Pass several versions#
const objects = await client.bim.query(["<version uuid of the first demo model>", "<version uuid of the second demo model>"],
"GET qid, attribute.Name WHERE type = IFCDOOR");ArrayNode objects = client.bim.query("GET qid, attribute.Name WHERE type = IFCDOOR",
UUID.fromString("<version uuid of the first demo model>"), UUID.fromString("<version uuid of the second demo model>"));ISet<Guid> versions = Collections.CreateHashSet(Guid.Parse("<version uuid of the first demo model>"), Guid.Parse("<version uuid of the second demo model>"));
JsonElement objects = client.Bim.Query2(versions, "GET qid, attribute.Name WHERE type = IFCDOOR");import nl.bimbase.bimworks.sdk.api.AuthApi;
import nl.bimbase.bimworks.sdk.api.IfcApi;
import nl.bimbase.bimworks.sdk.client.ApiClient;
import nl.bimbase.bimworks.sdk.model.Auth;
import nl.bimbase.bimworks.sdk.model.LoginWithApiTokenRequest;
import nl.bimbase.bimworks.sdk.model.QueryRequest;
public class Example {
public static void main(String[] args) throws Exception {
// 1. Connect to BIM.works and log in with your API token
ApiClient client = new ApiClient();
client.updateBaseUri("https://bim.works/openapi");
Auth auth = new AuthApi(client)
.loginWithApiToken(new LoginWithApiTokenRequest().token("<your API token>"))
.getAuth();
client.setRequestInterceptor(builder -> builder.setHeader("Authorization", "Bearer " + auth.getBase64()));
// 2. The request
QueryRequest request = client.getObjectMapper().readValue("""
{
"models": [
{
"type": "VersionModelIdentifier",
"uuid": "<version uuid of the first demo model>"
},
{
"type": "VersionModelIdentifier",
"uuid": "<version uuid of the second demo model>"
}
],
"query": "GET qid, attribute.Name WHERE type = IFCDOOR"
}
""", QueryRequest.class);
// 3. The call
var result = new IfcApi(client).query(request);
// 4. The result
System.out.println(client.getObjectMapper().writerWithDefaultPrettyPrinter().writeValueAsString(result));
}
}using System.Text.Json;
using BIMworks.Sdk.Api;
using BIMworks.Sdk.Client;
using BIMworks.Sdk.Extensions;
using BIMworks.Sdk.Model;
using Microsoft.Extensions.DependencyInjection;
// 1. Connect to BIM.works and log in with your API token
IServiceProvider Connect(string bearer)
{
var services = new ServiceCollection();
services.AddApi(options =>
{
options.AddTokens(new BearerToken(bearer));
options.AddApiHttpClients(http =>
{
http.BaseAddress = new Uri("https://bim.works" + "/openapi");
http.Timeout = Timeout.InfiniteTimeSpan;
});
});
return services.BuildServiceProvider();
}
var login = await Connect("").GetRequiredService<IAuthApi>().LoginWithApiTokenAsync(new LoginWithApiTokenRequest("<your API token>"));
var auth = login.Ok()?.Auth ?? throw new HttpRequestException(login.RawContent, null, login.StatusCode);
var client = Connect(auth.Base64);
var json = client.GetRequiredService<JsonSerializerOptionsProvider>().Options;
// 2. The request
var request = JsonSerializer.Deserialize<QueryRequest>("""
{
"models": [
{
"type": "VersionModelIdentifier",
"uuid": "<version uuid of the first demo model>"
},
{
"type": "VersionModelIdentifier",
"uuid": "<version uuid of the second demo model>"
}
],
"query": "GET qid, attribute.Name WHERE type = IFCDOOR"
}
""", json);
// 3. The call
var response = await client.GetRequiredService<IIFCApi>().QueryAsync(request);
// 4. The result
var result = response.Ok() ?? throw new HttpRequestException(response.RawContent, null, response.StatusCode);
Console.WriteLine(JsonSerializer.Serialize(result, json));import { AuthApi, Configuration, IFCApi, QueryRequestFromJSON } from "@bimworks/sdk";
// 1. Connect to BIM.works and log in with your API token
const basePath = "https://bim.works/openapi";
const auth = await new AuthApi(new Configuration({ basePath }))
.loginWithApiToken({ loginWithApiTokenRequest: { token: "<your API token>" } });
const client = new Configuration({ basePath, accessToken: typeof auth === "string" ? auth : auth.base64 });
// 2. The request
const request = QueryRequestFromJSON({
"models": [
{
"type": "VersionModelIdentifier",
"uuid": "<version uuid of the first demo model>"
},
{
"type": "VersionModelIdentifier",
"uuid": "<version uuid of the second demo model>"
}
],
"query": "GET qid, attribute.Name WHERE type = IFCDOOR"
});
// 3. The call
const result = await new IFCApi(client).query({ queryRequest: request });
// 4. The result
console.log(JSON.stringify(result, null, 2));<?php
require __DIR__ . '/vendor/autoload.php';
use BIMworks\Api\AuthApi;
use BIMworks\Api\IFCApi;
use BIMworks\Configuration;
use BIMworks\Model\LoginWithApiTokenRequest;
use BIMworks\Model\QueryRequest;
use BIMworks\ObjectSerializer;
// 1. Connect to BIM.works and log in with your API token
$client = (new Configuration())->setHost('https://bim.works/openapi');
$auth = (new AuthApi(config: $client))->loginWithApiToken(new LoginWithApiTokenRequest(['token' => "<your API token>"]));
$client->setAccessToken($auth->getBase64());
// 2. The request
$request = ObjectSerializer::deserialize(<<<'JSON'
{
"models": [
{
"type": "VersionModelIdentifier",
"uuid": "<version uuid of the first demo model>"
},
{
"type": "VersionModelIdentifier",
"uuid": "<version uuid of the second demo model>"
}
],
"query": "GET qid, attribute.Name WHERE type = IFCDOOR"
}
JSON, QueryRequest::class);
// 3. The call
$result = (new IFCApi(config: $client))->query($request);
// 4. The result
echo json_encode($result, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES), PHP_EOL;import json
import bimworks
# 1. Connect to BIM.works and log in with your API token
client = bimworks.ApiClient(bimworks.Configuration(host="https://bim.works/openapi"))
auth = bimworks.AuthApi(client).login_with_api_token(
bimworks.LoginWithApiTokenRequest(token="<your API token>"))
client.configuration.access_token = auth.actual_instance.var_base64
# 2. The request
request = bimworks.QueryRequest.from_json(r"""
{
"models": [
{
"type": "VersionModelIdentifier",
"uuid": "<version uuid of the first demo model>"
},
{
"type": "VersionModelIdentifier",
"uuid": "<version uuid of the second demo model>"
}
],
"query": "GET qid, attribute.Name WHERE type = IFCDOOR"
}
""")
# 3. The call
result = bimworks.IFCApi(client).query(request)
# 4. The result
print(json.dumps(client.sanitize_for_serialization(result), indent=2))The same methods as Run a BQL query: query on Bim, BimApi.query on BimApi, Bim.Query2 on Bim, IFCApi::query in PHP SDK, Query over HTTP.
2. Tell the results apart#
Every record's qid starts with the vid of its version. Group by it to get per-model results:
const perVersion = new Map();
for (const object of objects) {
const vid = object.qid.vid;
perVersion.set(vid, (perVersion.get(vid) || 0) + 1);
}Map<String, Integer> perVersion = new HashMap<>();
for (JsonNode object : objects) {
String qid = object.get("qid").asText();
String vid = qid.substring(0, qid.lastIndexOf('-')); // a vid is a UUID with dashes, the oid follows the last dash
perVersion.merge(vid, 1, Integer::sum);
}Aggregates run over the union: GET type, COUNT(qid) GROUP BY type counts across all versions, and SUM, MIN and MAX likewise. Relations do not cross model boundaries: buildingStorey.attribute.Name is each object's storey in its own model.
3. Select models by folder over HTTP#
The HTTP Query operation accepts, besides version identifiers, a NodeModelIdentifier: the models of a node, with recursive: true every model below a folder. This spares the caller a listing when a folder holds a project:
Bearer <auth token>application/jsonAuthorization: Bearer <auth token>
Content-Type: application/json
Accept: application/jsoncurl -X POST "$BIMWORKS_URL/openapi/ifc/Query" \
-H "Authorization: Bearer $BIMWORKS_TOKEN" \
-H "Content-Type: application/json" \
-d '{
"models": [{"type": "NodeModelIdentifier", "uuid": "<folder uuid>", "recursive": true}],
"query": "GET type, COUNT(qid) GROUP BY type"
}'Use it with care on large trees: every model below the folder is loaded for the query. The SDKs take version UUIDs only; list the folder first (List the tree).
Check#
queryCount (Count results) of GET qid WHERE type = IFCDOOR on both versions equals the sum of the two single-version counts, and the per-version map from step 2 has exactly two keys, the two vids.