Tutorial·Learn BQL by doingJavaScript

Units and parameters

Convert units per column with UNIT or per statement with USING, write unit literals in conditions, and bind values at run time with $parameters.

Quantities have units, and models disagree about them: the demo model stores lengths in millimetres and areas in square metres, the next model may be in metres and feet. BQL lets you say which unit you want, in the result and in the condition, and leaves the conversion to the server.

USING for the whole statement#

USING, the last clause of a statement, sets the unit of every value of that dimension in the result:

BQL#
GET qid, attribute.Name, quantity.*.Height, quantity.*.Length WHERE type = IFCWALL USING LENGTH_UNIT = m

The value in the result is in metres, whatever the model stored, and the row says both: value and unit are what you asked for, original_value and original_unit what the file had — 0.51 m next to 510 mm. A value that is already in the target unit comes back unchanged and without the original_* fields, which is why converting the volumes of this model to m3 changes nothing: the file already stores them in cubic metres. The clauses are LENGTH_UNIT, AREA_UNIT, VOLUME_UNIT, MASS_UNIT and TEMPERATURE_UNIT, in any order, each at most once: USING LENGTH_UNIT = m AREA_UNIT = m2.

UNIT on a column#

UNIT after a single GET item converts that column and ignores USING for it:

BQL#
GET qid, attribute.Name, calculated.quantity.volume UNIT m3 WHERE type = IFCWALL

UNIT belongs to a single feature; an aggregate cannot carry it (SUM(x) UNIT m3 is a syntax error) — convert the whole result with USING instead. It works on any numeric value: quantity.*.NetVolume UNIT m3 and aabb.height UNIT m convert an exported quantity and a bounding-box side the same way.

USING with aggregates#

USING also converts what an aggregate computed, which is the only way to choose the unit of a SUM or an AVG:

BQL#
GET typeObject.attribute.Name, SUM(quantity.*.NetVolume) AS Volume
WHERE type = IFCWALL GROUP BY typeObject.attribute.Name
USING VOLUME_UNIT = m3

Use UNIT for one column and USING for the statement rather than both on the same value. The units are listed in UNIT and USING and parameters.

Warning

UNIT, USING and the unit literals in conditions take every unit of the grammar, imperial units and litres included (using#units).

Units in conditions#

A numeric literal in WHERE takes a unit too, and the comparison converts before it compares. quantity.*.Height BETWEEN 2 m AND 3 m and quantity.*.Height BETWEEN 2000 mm AND 3000 mm select the same walls. Both sides of a comparison may be expressions: quantity.*.NetVolume > calculated.quantity.volume * 1.1 compares two features, units resolved on both sides. Give a literal its unit: a number without one is compared as it is, without conversion (unit#literals).

Parameters#

A $name may stand in for any literal: = $x, > $min, IN $values, type IN $types, qid IN $selection, BETWEEN $low AND $high. The value is supplied when the query runs, by the parameter panel of a stored query in the app, or by the parameters object of the query call in the JavaScript SDK and the Java request API:

BQL#
GET qid, attribute.Name WHERE type IN $types AND attribute.Name CONTAINS $text
const factory = new BimWorksClientFactory("https://bim.works");
const client = await factory.createClientWithApiToken("<your API token>");

const query = "GET qid, attribute.Name WHERE type IN $types AND attribute.Name CONTAINS $text";
const parameters = {
	types: ["IFCWALL", "IFCSLAB"],
	text: "vloer"
};
// query(models, query, useCache, forceGetQid, parameters)
const results = await client.bim.query(["<version uuid of the first demo model>"], query, false, false, parameters);
console.log(results.length + " objects");
for (const object of results) {
	console.log(object.qid + ": " + object.attribute.Name.value);
}

The parameters object maps names to values: a list of class names for type IN $types, a string for CONTAINS $text. The same query runs with different values without being edited, and the values are never spliced into the text, so a user-supplied string cannot change the query. Use query parameters shows the Java form; the .NET SDK, the PHP client and the HTTP Query operation have no parameters field yet.

A query with an unbound parameter is valid but matches nothing for that condition, so a parameter is only useful together with the values: the last round of this lesson declares $text and the page binds it before it runs your query.

Your turn#

GoalDemo model · Schependomlaan
List every wall with its name, its height and its length, both lengths in metres.
GET qid, attribute.Name, quantity.*.Height, quantity.*.Length WHERE type = IFCWALL
Editing and checking need JavaScript; the solution is below, and every query runs in the app's Actions tree too.
Hints
  1. USING is the last clause of the statement.
  2. Both quantities are lengths, so one LENGTH_UNIT clause covers them.
  3. The model stores millimetres, so the numbers have to change.
Show solution
GET qid, attribute.Name, quantity.*.Height, quantity.*.Length WHERE type = IFCWALL USING LENGTH_UNIT = m
Also accepted
GET attribute.Name, qid, quantity.*.Length, quantity.*.Height WHERE type = IFCWALL USING LENGTH_UNIT = m

Read one converted leaf in the JSON: value and unit are the metres you asked for, original_value and original_unit the millimetres the file stored.

GoalDemo model · Schependomlaan
List every wall with its name and the volume BIM.works computes from its geometry, in cubic metres, converted on the column itself rather than with USING.
GET qid, attribute.Name, calculated.quantity.volume WHERE type = IFCWALL
Editing and checking need JavaScript; the solution is below, and every query runs in the app's Actions tree too.
Hints
  1. UNIT follows the GET item it converts.
  2. The unit for cubic metres is m3.
Show solution
GET qid, attribute.Name, calculated.quantity.volume UNIT m3 WHERE type = IFCWALL
Also accepted
GET qid, calculated.quantity.volume UNIT m3, attribute.Name WHERE type = IFCWALL
GoalDemo model · Schependomlaan
List the walls between 2 and 3 metres high, with their height in metres.
GET qid, attribute.Name, quantity.*.Height WHERE type = IFCWALL
Editing and checking need JavaScript; the solution is below, and every query runs in the app's Actions tree too.
Hints
  1. BETWEEN takes two bounds joined by AND, and each bound may carry a unit.
  2. The model stores millimetres, so the result needs a conversion of its own.
Show solution
GET qid, attribute.Name, quantity.*.Height WHERE type = IFCWALL AND quantity.*.Height BETWEEN 2 m AND 3 m USING LENGTH_UNIT = m
Also accepted
GET qid, attribute.Name, quantity.*.Height WHERE type = IFCWALL AND quantity.*.Height BETWEEN 2000 mm AND 3000 mm USING LENGTH_UNIT = m
GET qid, attribute.Name, quantity.*.Height WHERE type = IFCWALL AND quantity.*.Height >= 2 m AND quantity.*.Height <= 3 m USING LENGTH_UNIT = m
GoalDemo model · Schependomlaan
List the spaces whose LongName contains a word supplied at run time, with their name and LongName — this round binds $text to "kamer" (room).
GET qid, attribute.Name, attribute.LongName WHERE type = IFCSPACE AND attribute.LongName CONTAINS "kamer"
Editing and checking need JavaScript; the solution is below, and every query runs in the app's Actions tree too.
Hints
  1. A parameter is a $ followed by its name, in the place of the literal.
  2. The name matters: this round binds $text.
  3. Nothing else about the query changes.
Show solution
GET qid, attribute.Name, attribute.LongName WHERE type = IFCSPACE AND attribute.LongName CONTAINS $text
Also accepted
GET qid, attribute.LongName, attribute.Name WHERE type = IFCSPACE AND attribute.LongName CONTAINS $text

Every round of this course binds the parameters it declares, so $text arrives with a value. In your own code the values travel next to the query — client.bim.query(models, query, false, false, {text: "kamer"}) in JavaScript — and are inserted into the parsed query, never into its text, so a value from a user can never change what the query does.

Note

A parameter name may not be a word the grammar uses: $name and $level are syntax errors, $text and $storey are fine. A parameter behind a relation (buildingStorey.attribute.Name = $storey) binds like any other.

You can now#

  • Convert one column with UNIT and a whole result with USING.
  • Write conditions with unit literals and compare features with each other.
  • Parameterise a query with $name and bind the values from an SDK.

Next: Calculated values, the quantities BIM.works works out from the geometry itself.