Tutorial·.NET.NET

.NET quick start

Add the BIM.works_SDK package, 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 .NET SDK. For a new integration, the .NET SDK generated from the OpenAPI document is the choice; its reference has a complete example for every operation (SDKs compares the two).

The .NET client is a .NET Standard 2.0 library, so it works from .NET Framework 4.6.1 up to current .NET. Its calls are synchronous and cover the tree, uploads, queries, tokens and geo referencing. This page takes you from an empty project to a program that uploads a model and queries it.

Install#

The package is BIM.works_SDK on nuget.org. Use the version of your server (the version pill at the top of this page is the server's):

Shell#
dotnet add package BIM.works_SDK --version 3.0.0-beta.192

or <PackageReference Include="BIM.works_SDK" Version="3.0.0-beta.192" /> in the project file. Everything lives in the BIM.works_SDK namespace. .NET covers pre-release versions.

Log in#

using System;
using System.Collections.Generic;
using System.Text.Json;
using BIM.works_SDK;

class QuickStart
{
    static void Main()
    {
        string model = @"path\to\model.ifc";

        // One factory per instance, one client per login; both are disposed with the using blocks
        using (BimWorksClientFactory factory = new BimWorksClientFactory(new Uri("https://bim.works")))
        using (BimWorksClient client = factory.CreateClientWithApiToken("<your API token>"))
        {
            Console.WriteLine("Connected: " + client.Info.GetApplicationVersion());

            // The root of an API token is the node it was created on; paths are relative to it
            Guid rootUuid = client.Fs.GetRootNodeUuid();
            foreach (TreeNode node in client.Fs.ListNodes(rootUuid))
            {
                Console.WriteLine((node.IsFolder ? "folder " : "model  ") + node.Name);
            }

            // An empty model node to upload into, then the upload itself
            Guid nodeUuid = client.Fs.CreateNode(rootUuid, "quick-start.ifc", false, InputFileType.IFC);
            UploadBuilder upload = new UploadBuilder(nodeUuid).SetPath(model);
            UploadResponse response = client.Upload.UploadModel(upload).GetUploadResponse();
            Guid versionUuid = response.treeNode.GetLastVersion().Uuid;
            Console.WriteLine("Uploaded as version " + versionUuid);

            // Query the new version: one row per object, every value wrapped in {"value": ...}
            JsonElement rows = client.Bim.Query(versionUuid, "GET qid, attribute.Name, quantity.*.Height UNIT m WHERE type = IFCDOOR LIMIT 10");
            foreach (JsonElement row in rows.EnumerateArray())
            {
                string name = row.GetProperty("attribute").GetProperty("Name").GetProperty("value").GetString();
                Console.WriteLine(row.GetProperty("qid").GetString() + " " + name);
            }
        }
    }
}

One BimWorksClientFactory per instance and one BimWorksClient per login; both are IDisposable, so the using blocks close the connection. CreateClientWithApiToken is synchronous and throws BimWorksException when the token is rejected.

The factory takes a Uri; the sub-APIs are properties of the client: Auth, Fs, Upload, Bim, Tokens, Gis, Actions, Info and Admin. client.Info.GetApplicationVersion() returns the server version as a JsonElement 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) returns the children as List<TreeNode>, each with Name, IsFolder, Path and Versions; GetLastVersion() gives the version to query and view. CreateNode(parentUuid, name, isFolder, InputFileType.FOLDER) adds a folder, CreateNodeIfNotExists the same without failing when the name is taken, and CreateNode(parentUuid, "model.ifc", false, InputFileType.IFC) an empty model node to upload into.

Upload#

An UploadBuilder names the target node and the source: new UploadBuilder(nodeUuid).SetPath(path) for a local file, PassThrough(uri, filesize) for a URL the server downloads itself, or a stream with filename and filesize. client.Upload.UploadModel(builder) returns an AsyncUploadTask; GetUploadResponse() waits for the server to process the file and returns the model node with its new version. Upload into an empty model node you created, as the example does, or into a folder, in which case a model node named after the file is created.

Query#

client.Bim.Query(versionUuid, bql) runs a BQL statement on one model, client.Bim.Query2(versionUuids, bql) on several; both return a JsonElement array with one object per row, every feature under its path and every value wrapped in an object with a value property (plus a unit when it has one). System.Text.Json reads it: row.GetProperty("attribute").GetProperty("Name").GetProperty("value").GetString(). The BQL course teaches the language.

Where to go from here#

client.Tokens.CreateVersionsToken mints a token that only sees the versions you name, the pattern for handing a model to a browser (Tokens and delegation): with the query:run scope the browser runs its own queries. client.QueryDelegated, the older way of letting a browser query, is retired (Delegated query). The .NET reference lists every type; the how-to guides show one task at a time, starting with Upload a model and Run a BQL query; Upload models concurrently from .NET uploads a whole batch at once.