Converting Models to XKT with convert2xkt
The xeokit SDK enables us to load large, full-precision BIM and AEC models over the Web in seconds and view them in the browser. To do this, we pre-convert our models into xeokit's native XKT **format. The XKT format compresses model geometry and metadata into a compact payload that a xeokit viewer can load quickly while using minimal browser resources.
In this mini-tutorial, we introduce convert2xkt
, a new JavaScript function and CLI tool that converts several 3D model formats into XKT format.
We provide convert2xkt
as an additional conversion option for your models, alongside our existing standard open source IFC→XKT conversion pipeline.
Note that the pipeline in this tutorial only converts IFC element structure, and does not convert property sets. For property sets, we recommend Converting IFC Models to XKT using ifc2gltf.
- Introduction
- What's IFC?
- What's XKT?
- convert2xkt
- Features
- Resources
- Installing convert2xkt
- Converting Models on the CLI
- Converting Models in JavaScript
Introduction
What's IFC?
The Industry Foundation Classes (IFC) data model is used to describe architectural, building and construction industry data.
IFC defines an entity-relationship model consisting of various entities organized into a object-based inheritance model, with classes representing various building elements such as IfcWall, geometry such as IfcExtrudedAreaSolid, and basic constructs such as IfcCartesianPoint.
The data model is developed by buildingSMART to facilitate interoperability in the AEC industry, and is a commonly used open collaboration format in BIM based projects.
An IFC file is usually provided as an IFC-SPF file ("STEP-file") , which is a text format in which each line represents an entity that instantiates a class within the data model.
What's XKT?
XKT is xeokit's native 3D model format, which compresses IFC and CAD models into a compact binary payload from which we can load large numbers of objects over the Web in seconds, at full geometric precision.
An XKT file also contains metadata on its objects, which enables a xeokit viewer to classify and manipulate them according to their types. That's useful for building things like object tree view UIs, automatic building storey plan views, and so on.
For each object/element, the metadata stores:
- Name,
- Element ID,
- Element IFC type
- Parent element ID
The metadata also stores IFC property sets, some of which may be associated with objects/elements.
In this tutorial, we'll use convert2xkt
to convert an IFC 4 model into an XKT file, which we'll then load into a xeokit Viewer. For our IFC file, we'll use one of the Revit Sample Project Files from AutoDesk. When that's converted and loaded, it will look like the example below.
convert2xkt
Features
- Converts models to XKT in a single step, without intermediate file formats or 3rd-party tools
- Converts several source formats, including IFC, CityJSON, LAS and glTF
- Converts IFC metadata, including property sets
- Run from both CLI and JavaScript, on Node.js
Resources
- Source code: https://github.com/xeokit/xeokit-convert
- API Docs: https://xeokit.github.io/xeokit-convert/docs/
- Benchmarks: https://xeokit.github.io/xeokit-convert/perfTests/
- XKT specification: https://github.com/xeokit/xeokit-convert/tree/master/specs
Installing convert2xkt
Using git and npm, clone and install our xeokit-convert
repository, which contains the convert2xkt
tool that we'll use to convert our models into XKT. Be sure to use the latest versions of both xeokit-convert
and xeokit-sdk
.
git clone https://github.com/xeokit/xeokit-convert.git
cd xeokit-convert
npm install
Converting Models on the CLI
To get started with convert2xkt
, let's convert an IFC model at the command line.
First, we'll get the tool to print out some usage info:
node convert2xkt.js -h
Usage: convert2xkt [options]
Options:
-v, --version output the version number
-s, --source [file] path to source file
-f, --format [string] source file format (optional); supported formats are gltf, ifc, laz, las, pcd, ply, stl and cityjson
-m, --metamodel [file] path to source metamodel JSON file (optional)
-o, --output [file] path to target .xkt file; creates directories on path automatically if not existing
-p, --properties [file] path to target directory for object property files; creates directories on path automatically if not existing
-l, --log enable logging
-h, --help output usage information
Then we'll go ahead and convert our model. We'll use the tool's logging option to see what's happening during conversion.
convert2xkt.js -s rac_sample_project.ifc -o rac_sample_project.ifc.xkt -l
[convert2xkt] Reading input file: rac_sample_project.ifc
[convert2xkt] Input file size: 45316.70 kB
[convert2xkt] Converting...
[convert2xkt] Converted objects: 5563
[convert2xkt] Converted geometries: 2871
[convert2xkt] Converted triangles: 283238
[convert2xkt] Converted vertices: 743996
[convert2xkt] Converted to: XKT v9
[convert2xkt] XKT size: 1776.94 kB
[convert2xkt] Compression ratio: 25.50
[convert2xkt] Conversion time: 31.85 s
[convert2xkt] Writing XKT file: rac_sample_project.ifc.xkt
If NodeJS throws an out-of-memory exception, try setting a larger heap size, eg. node --max-old-space-size=25192 convert2xkt.js -s myModel.ifc -o myModel.ifc.xkt -l
Now we've got our XKT model - it's compressed 25x and took 31 seconds to convert. Let's view it in the browser using xeokit, which should load this model in ~3-4s over a decent network connection.
import {Viewer, XKTLoaderPlugin} from "https://cdn.jsdelivr.net/npm/@xeokit/xeokit-sdk/dist/xeokit-sdk.es.min.js";
const viewer = new Viewer({
canvasId: "myCanvas"
});
const xktLoader = new XKTLoaderPlugin(viewer);
const modelNode = xktLoader.load({
id: "myModel",
src: "./rme_sample_project.ifc.xkt"
});
Converting Models in JavaScript
Let's use convert2xkt
from within a Node.js script to convert several different models to XKT. We'll convert models from CityJSON, IFC, LAZ and glTF.
// TODO: fix converter require import
const convert2xkt = require("@xeokit/xeokit-xkt-utils/dist/convert2xkt.cjs.js");
// CityJSON -> XKT
await convert2xkt({
source: "./DenHaag_01.json",
target: "./DenHaag_01.json.xkt"
});
// IFC -> XKT
await convert2xkt({
source: "./rme_sample_project.ifc",
target: "./rme_sample_project.ifc.xkt"
});
// LAZ -> XKT
await convert2xkt({
source: "./autzen.laz",
target: "./autzen.laz.xkt"
});
// glTF -> XKT
await convert2xkt({
source: "./schependomlaan.gltf",
target: "./schependomlaan.gltf.xkt"
});