How to get units information from IFC model?

  1. FAQ
  2. »
  3. IFC

Units information can be accessed through IfcProject.UnitsInContext attribute using C++ API in following way:

// 1. Using OdIfcFile
OdDAIObjectId idProject = pIfcFile->getProjectId();

// 2. No OdIfcFile way, work with model only
OdDAI::ModelPtr model = pIfcFile->getModel();
const OdDAIObjectIds &projectExtent = model->getEntityExtent("ifcproject")->getArray();
if (projectExtent.size())
{
  idProject = projectExtent[0];
}

if (idProject.isValid())
{
  OdDAI::ApplicationInstancePtr project = idProject.openObject();

  OdDAIObjectId idUnitsInContext;
  project->getAttr("unitsincontext") >> idUnitsInContext;
  if (idUnitsInContext.isValid())
  {
    OdDAI::ApplicationInstancePtr unitAssignment = idUnitsInContext.openObject();
    OdDAI::Set *units = nullptr;
    unitAssignment->getAttr("units") >> units;
    if (!units->isNil())
    {
      for (const OdDAI::Select &selUnit : units->getArray())
      {
        OdDAIObjectId idUnit = selUnit.getHandle();
        OdDAI::ApplicationInstancePtr unit = idUnit.openObject();

        const char *unitType = nullptr;
        unit->getAttr("unittype") >> unitType;

        const char *prefix = nullptr;
        unit->getAttr("prefix") >> prefix;

        const char *name = nullptr;
        unit->getAttr("name") >> name;

        oddaiPrintConsoleString(L"\n#%u=%hs:\n UnitType: %hs\n Prefix: %hs\n Name: %hs\n",
          (OdUInt64)idUnit.getHandle(), unit->typeName().c_str(), unitType, prefix, name);
      }
    }
  }
}