Getting Started with IFC SDK

Igor Egorychev

June 06, 2019

Recently added to the BIM Suite is IFC SDK, with support for reading and rendering IFC 2x3 files, navigating loaded IFC model entities, and viewing read-only entity attributes. This article describes how to get started using the SDK.

The initial IFC SDK release is based on the currently available ODA SDKs version 19.12.

Initializing ODA IFC SDK

To initialize IFC SDK modules, call the OdResult odIfcInitialize(bool bInitIfcGeomResource /*= true*/) function which is declared in the IfcCore.h header file.

Rendering geometry entities isn’t always needed (for example, just walking along IfcModel might be the only required task), so the parameter bInitIfcGeomResource is provided to avoid geometric module initialization. If calling the function with a predefined parameter value, the modules IfcCore.dll and IfcGeom.dll are loaded. IfcCore initialization also loads Spf.tx, which contains the routines and base classes needed for low-level object-model-independent work with STEP physical files.

Reading .ifc Files

To read .ifc file content, OdIfcHostAppServices::readFile() should be called. The method returns a pointer to an instance of the OdIfcFile class which contains the database of loaded IFC entities (an IFC model). The OdIfcFile class is declared in the “IfcFile.h” header inside the OdIfc namespace.

The header section container of the .ifc file can be accessed by calling the OdSpf::OdSpfHeaderSectionPtr OdIfcFile::getHeaderSection() method. The header section contains information fields about the model in the .ifc file, about authors/owners of the model, and so on. Information about the file data section schema is also kept in the header section:

OdIfc::OdIfcFilePtr pIfcFile = svcs.readFile(pBuffer);
OdDAI::OdSpfHeaderSectionPtr pHeader = pIfcFile >getHeaderSection(fileName);
OdDAI::OdSpfFileSchemaPtr pSchema = pHeader >getEntityByType(headerFileSchema);
OdString dataSectionSchema = pSchema->at(0);

To get the main entity container of OdIfcFile, its method OdIfcModelPtr OdIfcFile::getModel() should be called. Currently ODA IFC SDK supports the Ifc2x3 schema only, so getModel() returns NULL when attempting to load an unsupported file.

OdIfcModelPtr pModel = pIfcFile->getModel();

A set of IfcGeometricalContexts to generate geometry should be determined first. Let’s create an iterator along entity instances derived from the Ifc2x3 Express Schema entity type called IfcGeometricRepresentationContext in the loaded OdIfcModel:


OdDAIObjectIds ids = pModel->getEntityExtent(“IfcGeometricRepresentationContext”);

OdIfcHandles selContexts;
for (OdDAIObjectIds::iterator it = ids.begin(), end = ids.end(); it != end; ++it){
  OdIfc::OdIfcEntityPtr pInst = it->OpenObject();
  OdAnsiString strContextType;
  if (pInst->getAttr("ContextType") >> strContextType)
  {

If the value of the property named “ContextType” was extracted from an entity instance and was successfully assigned to strContextType (types are in coincidence), continue. We need to find all contexts and subcontexts of the desired types: the “Model” for contexts and “Body” for subcontexts that contain 3D geometrical representations for elements of the model.

            if (strContextType.compare("Model") == 0)
            {
              selContexts.append(*it);
            }

            OdDAIObjectIds hasSubContexts;
            if (pInst->getAttr("HasSubContexts") >> hasSubContexts)
            {
              for (OdDAIObjectIds::iterator itSub = hasSubContexts.begin(), endSub = hasSubContexts.end();
                itSub != endSub; ++itSub)
              {
                OdIfc::OdIfcEntityPtr pSubCtx = itSub->openObject();
                OdAnsiString strContextIdentifier;
                if (pSubCtx->getAttr("ContextIdentifier") >> strContextIdentifier &&
                  strContextIdentifier == "Body")
                {
                  selContexts.append(*itSub);
                }
              }
            }
          }

Building and Rendering Geometry Entities

Fill the array of IfcGeometricRepresentationContext/SubContext handles to build geometry. “Model”/”Body” contexts/subcontexts are enough to get 3D entities; those names are predefined by the IFC specification, and we don’t need the other types of GeometricContexts/SubContexts (they can still keep schematical representations of the model’s drawable items, or something else).

pModel->setContextSelection(selContexts);

Build geometry entities for rendering. This method composes OdGiDrawable entities for the loaded IFC model:

pModel->composeEntities ();

If the result of OdIfcFile::setGeomResource() is eOk, the geometry is successfully generated and can be rendered using GsDevice; the root entity for drawing is OdIfcProject:

OdIfcObjectId idProject = pIfcFile->getProjectId();

OdGiContextForIfcDatabasePtr pIfcContext = OdGiContextForIfcDatabase::createObject();
pIfcContext->setDatabase(pIfcFile);
pIfcContext->enableGsModel(true);

OdGsDevicePtr pDevice = …;

OdGsDevicePtr pIfcDevice = OdIfcGsManager::setupActiveLayoutViews(idProject, pDevice .get(), pIfcContext);
OdGsView* pView = pDevice->viewAt(0);
pView->setMode(...);

Process the rendering:

pDevice->update(OdGsView::kFlatShaded);

Deinitialize the modules by calling OdResult odIfcUninitialize().

More Information

Details and live demo: IFC SDK
Download a trial: Coming Soon

The extended examples of working with ODA BimIfc SDK can be found in Visualize/Examples/Ifc2Visualize, this is an example of geometry rendering using ODA Visualize SDK, and in Ifc/Exports/Ifc2Dwg, this is a sample of exporting the geometry from loaded IFC file into the .dwg drawing as a set of polyface meshes. Two additional simple example applications are provided also: ExIfcReadFile, just for read the desired Ifc2x3 file, and ExIfcDump to collect entities statistics by walking along directory structure and extracting information from loaded Ifc models.

And if you’re already using or evaluating IFC SDK, examples are located in the Visualize/Examples installation folder:

  • Ifc2Visualize — Shows geometry rendering from .ifc files using ODA Visualize SDK.
  • Ifc/Exports/Ifc2Dwg — Exports geometry from a loaded .ifc file into a .dwg drawing as a set of polyface meshes.
  • ExIfcReadFile — Reads an Ifc2x3 file.
  • ExIfcDump — Collects entity statistics by walking along the directory structure and extracting information from loaded IFC models.
  • ExIfcModelFiller – creates a valid IFC model with geometrical entities
  • ExIfcWriteFile – loads a file and resaves it into different file.