ODA IFC SDK Developer's Guide > Get Started with IFC SDK > Basic Concepts of IFC SDK Usage
Basic Concepts of IFC SDK Usage

This document describes the basic concepts of using IFC SDK.

Initialization and Deinitialization

As usual for ODA products, before starting to use IFC SDK functionality, you need to initialize it. To initialize IFC SDK, use the odIfcInitialize(); function which is declared in the Include/IfcCore.h header file. This function loads the appropriate modules that provide IFC SDK functionality. It accepts two parameters:

  • bInitIfcGeomResource — A flag that determines whether geometry functionality should be initialized as well. Required to work with geometry objects.
  • modelerType — Determines which type of geometry is supported. By default, facet geometry is supported.

Before calling the odIfcInitialize(); function, sequentially call the odrxInitialize() function to get access to Rx-functionality and the odDbRootInitialize() function to initialize drawing database functionality:


odrxInitialize(&svcs);
odDbRootInitialize();

odIfcInitialize(true);
    

To clear up the environment before finishing an IFC SDK based application, call the odIfcUnnitialize() function. If you called the odrxInitialize() or (and) the odDbRootInitialize() function previously, don't forget to call the odrxUninitialize() or (and) the odDbRootUninitialize() functions. Please note that you have to keep the calling sequence when de-initializing IFC functionality:


odIfcUninitialize();

odDbRootUninitialize();
odrxUninitialize();
    

Working with an IFC Database

To create a new IFC database, call the createDatabase() method of the customer service class instance declared in the Ifc/Examples/Common/IfcExamplesCommon.h header file. The MyServices class is based on ExSystemServices and OdExIfcHostAppServices classes, which implement platform-based functionality for IFC SDK based applications:


class MyServices : public ExSystemServices, public OdExIfcHostAppServices
{
protected:
  ODRX_USING_HEAP_OPERATORS(ExSystemServices);
  OdGsDevicePtr gsBitmapDevice(OdRxObject* /*pViewObj*/ = NULL,
    OdDbBaseDatabase* /*pDb*/ = NULL,
    OdUInt32 /*flags*/ = 0)
  {
    try
    {
      OdGsModulePtr pGsModule = ::odrxDynamicLinker()->loadModule(OdWinBitmapModuleName);
      return pGsModule->createBitmapDevice();
    }
    catch(const OdError&)
    {
    }
    return OdGsDevicePtr();
  }

public:

  virtual OdString findFile(
    const OdString& filename,
    OdDbBaseDatabase* pDb,
    FindFileHint hint)
  {
    return filename;
  }

  virtual const OdString program()
  {
    return OdString::kEmpty;
  }

  virtual const OdString product()
  {
    return OdString::kEmpty;
  }

  virtual const OdString companyName()
  {
    return OdString::kEmpty;
  }

  virtual bool ttfFileNameByDescriptor(
    const OdTtfDescriptor& description,
    OdString& filename)
  {
    return false;
  }

  virtual OdString getAlternateFontName() const
  {
    return OdString::kEmpty;
  }

  virtual OdString getFontMapFileName() const
  {
    return OdString::kEmpty;
  }

  virtual OdString getPreferableFont(
    const OdString& fontName,
    OdFontType fontType)
  {
    return OdString::kEmpty;
  }

  virtual OdString getSubstituteFont(
    const OdString& fontName,
    OdFontType fontType) 
  {
    return OdString::kEmpty;
  }

};
    

The createDatabase() method returns a smart pointer to the OdIfcFile object. Using this smart pointer, call the readFile() method to load the contents of an .ifc file to the created object.


    
      OdStaticRxObject< MyServices > svcs;
      OdString ifcFileName(argv[1]);

      OdIfc::OdIfcFilePtr pDatabase = svcs.createDatabase(kScmUndefined);
      OdResult res = pDatabase->readFile(ifcFileName);

      if (res == eOk)
      {
        odPrintConsoleString(L"\nFile opened successfully.");
      }
      else
      {
        odPrintConsoleString(L"\nFile open error.");
      }
    

To write the contents of an IFC file object, use the writeFile() method of the OdIfcFile object:


    OdStaticRxObject < MyServices > svcs;
    
    OdIfc::OdIfcFilePtr pDb = svcs.createDatabase();
    if (!pDb.isNull())
    {
      
      //Add here some functionality for filling the header section and model objects

      pDb->writeFile(szTargetFileName, true);
    }

    

Working with Header Sections and Models

When you have a valid instance of the OdIfcFile class, you can manage the header section and model objects of the IFC database.

To get access to the IFC database header section:

  1. Retrieve the repository object:
    
    OdDAI::RepositoryPtr  pRepository = pDb->getRepository();
            
  2. Get the header section object from the repository object and check if it is valid:
    
    OdDAI::OdHeaderSectionPtr headerSection = pRepository->getHeaderSection();
    
    if (headerSection.isNull())
    {
      ODA_ASSERT(0 && "Header is not valid.");
      throw OdError(eNullEntityPointer);
    }
            
  3. Initialize the header section:
    
    headerSection->initialize();
            
  4. Add the .ifc file description to the header section:
    
    OdDAI::OdFileDescriptionAuto* fileDescription = getHeaderFromSection(headerSection);
    OdArray descriptionCollection;
    descriptionCollection.push_back("ViewDefinition [CoordinationView]");
    fileDescription->setDescriptionAggr(descriptionCollection);
    fileDescription->setImplementationLevel("2;1");
            
  5. Add the .ifc file name and its timestamp:
    
    OdDAI::OdFileNameAuto* fileName = getHeaderFromSection(headerSection);
    fileName->setName("TestFile.ifc");
    fileName->setTimeStamp("2019-02-23T12:17:26");
            
  6. Define the schema of the .ifc file:
    
    OdDAI::OdFileSchemaAuto* fileSchema = getHeaderFromSection(headerSection);
    OdArray schemCollection;
    schemCollection.push_back("IFC2X3");
    fileSchema->setSchemaIdentifiersAggr(schemCollection);
            

To proceed with any operation with an IFC model, you need to have a smart pointer to its object within an IFC database. To get it, use a model provider object from the OdDAI interface:


OdDAI::Utils::ModelProviderRO  modelProvider(pDb->getRepository());
OdIfc::OdIfcModelPtr model = modelProvider.model();

if (model.isNull() || model->underlyingSchemaName().find("IFC2X3") != 0)
{
  ODA_ASSERT(0 && "Model is not valid.");
  throw OdError(eNullEntityPointer);
}
    

Using the smart pointer returned by the model() method of the repository object, you are able to handle the IFC model the way you want through the OdDAI interface.

See Also

Library Dependencies of IFC SDK

Download IFC SDK

Copyright © 2002 – 2020. Open Design Alliance. All rights reserved.