How-to guide·QueryJavaScriptRequires AI

LLM assistance

Turn a sentence into a BQL query or a BVL visualisation with TextToAction, run it on the server with Query or ResolveBvl, and show the result in the 3D viewer, all through the OpenAPI.

A user types colour the external walls red and the model in your viewer turns red. That takes three calls: a language model turns the sentence into an action, the server runs that action against the model, and the viewer shows the answer. This guide makes the three calls through the OpenAPI, so they work from any language; only the last step runs in the browser.

Step Call Answer
1. Text → action TextToAction A BQL query or a BVL visualisation, as code
2. Run it on the server Query for BQL, ResolveBvl for BVL Records, or the objects to show, hide, select and colour
3. Show it in the viewer none: the 3D viewer's ObjectStateManager and BvlHelper The objects coloured and framed

Try it#

The demo below makes the three calls against the demo model with your account's demo API token. Type a sentence or pick an example, then Run all three steps, or go one step at a time: every card shows its request and the answer, and its POST /openapi/… line links to the operation.

Text to action: translate, run, showLive demo
Loads the demo model in this page with the demo API token of your account, and asks the language model of this instance: open BIM.works and sign in once in this browser.

The language model writes the action, so the same sentence can come back worded differently. Edit the code in step 2 and run it again to see what changes. Without the AI feature on your account, or AI on the instance, step 1 is dimmed; type BQL or BVL in step 2 yourself and run it. A query only shows objects when its records carry a qid; the demo offers to add it.

Before you start#

  • AI on the instance (ai.enabled, with a model under ai.text_to_actions) and the AI feature on the account behind your token, switched on per account or organisation in the account settings. Without them TextToAction answers 403, for example Scope ai:use requires the AI user feature; steps 2 and 3 do not need them.
  • An auth token with the scopes ai:use (step 1), query:run (BQL) and actions:read (BVL). Log in with an API token gets one from an API token with LoginWithApiToken; every call below sends it as Authorization: Bearer <token> (Authentication and scopes).
  • The version UUID of the model; the examples use the first demo model.

1. Turn the text into an action#

Send the sentence to TextToAction. models is optional context: the language model may run aggregate queries on those models first, to use the property names they really carry. The server chooses the connection with ai.text_to_action_tools: mcp (the default, requiring ai.mcp_server_enabled and a reachable server) or inline (queries run internally, with no inbound connection required). language (BQL, BVL or BCL) forces the kind of action; without it the model picks one from the wording.

POST/openapi/ai/TextToActionTextToAction reference
Authorization
Bearer <auth token>
Content-Type
application/json
Body
1 2 3 4
Authorization: Bearer <auth token>
Content-Type: application/json
Accept: application/json
curl -X POST "$BIMWORKS_URL/openapi/ai/TextToAction" \
  -H "Authorization: Bearer $BIMWORKS_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
  "input": "Colour the external walls red",
  "models": [{"type": "VersionModelIdentifier", "uuid": BIM_WORKS_FIRST_MODEL}]
}'
Send to see the response

The answer is the action, parsed and formatted by the server:

JSON#
{
  "language": "BVL",
  "query": "if (type = IFCWALL AND property.*.IsExternal = true) {\n    COLOR \"red\";\n}\n",
  "main_type": "BVL",
  "description": "Colour the external walls red"
}
  • language says which call runs query: BQL goes to Query, BVL to ResolveBvl, and BCL, a check in BCL, to Check.
  • Code that does not parse, or an answer in the wrong format (for example an invented JSON action block), goes back to the model once with correction feedback. If the second answer still has invalid code or format, or the model answers in another language than the language you asked for, the call fails with 502 and the message says what happened, the model's own words included. Show that message to the user and let them rephrase.
  • Nothing runs and nothing is saved as an action; the instance logs the sentence and the answer of every successful call. main_type is the action type to use when you do save it in the actions tree.

2. Run the action on the server#

Send query as it came. For BQL that is Query, the call of Run a BQL query:

POST/openapi/ifc/QueryQuery reference
Authorization
Bearer <auth token>
Content-Type
application/json
Body
1 2 3 4
Authorization: Bearer <auth token>
Content-Type: application/json
Accept: application/json
curl -X POST "$BIMWORKS_URL/openapi/ifc/Query" \
  -H "Authorization: Bearer $BIMWORKS_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
  "models": [{"type": "VersionModelIdentifier", "uuid": BIM_WORKS_FIRST_MODEL}],
  "query": "GET qid, type, attribute.Name WHERE type = IFCDOOR"
}'
Send to see the response

For BVL it is ResolveBvl. The server evaluates every condition of the script and answers with what the script does: sets of objects to show, hide, select and frame, a set per colour, labels and annotations.

POST/openapi/ifc/ResolveBvlResolveBvl reference
Authorization
Bearer <auth token>
Content-Type
application/json
Body
1 2 3 4
Authorization: Bearer <auth token>
Content-Type: application/json
Accept: application/json
curl -X POST "$BIMWORKS_URL/openapi/ifc/ResolveBvl" \
  -H "Authorization: Bearer $BIMWORKS_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
  "models": [{"type": "VersionModelIdentifier", "uuid": BIM_WORKS_FIRST_MODEL}],
  "bvl": "if (type = IFCWALL AND property.*.IsExternal = true) { COLOR \"red\" VIEWFIT; }"
}'
Send to see the response
JSON#
{
  "color": {"#ff0000ff": {"797e993c-a87f-4aa7-9587-c59d30ec5c4e": [1316, 1402]}},
  "viewFit": {"797e993c-a87f-4aa7-9587-c59d30ec5c4e": [1316, 1402]},
  "force": true
}

Every set is keyed by version UUID and lists object ids, so 797e993c-…-1316 is the qid of the first wall; a colour is #rrggbbaa, the last byte its opacity. The script language itself is on BVL.

3. Show the result in the viewer#

This step runs in the browser, with a viewer set up as in Embed the viewer. A BVL answer goes to the viewer as it is; BvlHelper of @bimworks/viewercommon applies every key of it to the ObjectStateManager:

JavaScript#
import {BvlHelper} from "@bimworks/viewercommon";

await objectStateManager.resetAll();   // undo what the previous answer did
await new BvlHelper(objectStateManager, viewer).processResults([resolved]);

A BQL answer is records, so you decide what showing it means. Colouring the objects and framing them works for any query whose records carry a qid:

JavaScript#
import {QidSet} from "@bimworks/jssdk";
import {ColorChangeMap} from "@bimworks/viewercommon";

const qids = new QidSet();
const colours = new ColorChangeMap();
for (const record of records) {
	if (record.qid) {
		qids.add(record.qid);
		colours.changeQid(record.qid, "#E68523");
	}
}
await objectStateManager.resetAll();
await objectStateManager.changeColorMulti(colours);
await objectStateManager.viewFit(qids, {animate: true});

A query without qid in its GET list (a count per type, say) has no object to point at: ask for qid, or COLLECT(qid) next to a GROUP BY to get the objects of every group. More on colours and selection in Select and colour objects.

Check#

With Colour the external walls red the demo answers with BVL in step 1, step 2 lists one colour with the external walls under it, and step 3 turns those walls red in the viewer. List every door with its name answers with BQL; when the records carry a qid, step 3 colours and frames the doors.

Since 3.0