ODA IFC SDK Developer's Guide > Get Started with IFC SDK > Create Your First IFC SDK Application > Create Your First IFC SDK Application: Details
Create Your First IFC SDK Application: Details

IFC SDK First Application Source Code

Return to the main guide

Below is the full content of the ExIfcFirstApp.cpp source file from the IFC SDK first application project.


//System includes
#include <iostream>
#include <cstdlib>
 //IFC SDK includes
#include <IfcExamplesCommon.h>
#include <IfcCore.h>
#include <OdaCommon.h>
#include <StaticRxObject.h>
#include <RxDynamicModule.h>
 #include <ExPrintConsole.h>
 #include <daiHeaderSection.h>
#include <daiHeaderEntities.h>
 DBROOT_EXPORT void odDbRootInitialize();
DBROOT_EXPORT void odDbRootUninitialize();
 enum AppResult
{
  arOk = 0,
  arIncorrectUsage,
  arFileOpenError,
  arEmptySectionHeader,
  arInvalidSchema,
  arUnsupportedSchema,
  arEmptyModel,
  arOdError,
  arUnexpectedError
};
 int main(int argc, char* argv[])
{
  setlocale(LC_TIME, ""); // set current user locale (not OD_T("C")), for strftime
   OdStaticRxObject <MyServices> svcs;
   odrxInitialize(&svcs);
   odDbRootInitialize();
   odIfcInitialize(false);
   svcs.disableProgressMeterOutput(true);
   odPrintConsoleString(L"\nIFC SDK First Application. Copyright (c) 2020, Open Design Alliance\n");
  odPrintConsoleString(L"\nExIfcFirstApp application developed using %ls ver %ls", svcs.product().c_str(), svcs.versionString().c_str());
   if (argc != 2)
  {
    odPrintConsoleString(L"\n\nusage: ExIfcFirstApp ");
    odPrintConsoleString(L"\n a full path to the input IFC file\n Press any key to finish...");
    getchar();
    return arIncorrectUsage;
  }
   {
    OdIfcFilePtr pDatabase;
    try
    {
       OdString ifcFileName(argv[1]);
       pDatabase = svcs.createDatabase(InitialSchema::kScmUndefined);
      OdResult res = pDatabase->readFile(ifcFileName);
       if (res == eOk)
      {
        odPrintConsoleString(L"\nFile opened successfully (%s).", ifcFileName.c_str());
      }
      else
      {
        odPrintConsoleString(L"\nFile open error (error code: %d). Press any key to finish...", res);
        getchar();
        pDatabase = NULL;
        return arFileOpenError;
      }
       //get header section
      OdDAI::OdHeaderSectionPtr headerSection = pDatabase->getHeaderSection();
       if (!headerSection.isNull())
      {
        //get and check the schema
        OdDAI::ApplicationInstancePtr fileSchema = headerSection->getEntityByType(OdDAI::kFileSchema);
         OdArray<OdAnsiString> schemas;
        if (fileSchema->getAttrCaseInsensitive("schema_identifiers") >> schemas)
        {
          for (const auto& schemaName : schemas)
          {
            OdDAI::SchemaPtr schema = oddaiGetSchema(schemaName);
            if (schema.isNull())
            {
              odPrintConsoleString(L"\nUnsupported schema %hs. Press any key to finish...", schemaName.c_str());
              getchar();
              pDatabase = NULL;
              return arUnsupportedSchema;
            }
            odPrintConsoleString(L"\nIFC File schema: %hs\n", schemaName.c_str());
          }
        }
      }
      else 
      {
        odPrintConsoleString(L"\nError getting header section from the %s file! Press any key to finish...", ifcFileName.c_str());
        getchar();
        pDatabase = NULL;
        return arEmptySectionHeader;
      }
       OdIfc::OdIfcEntityPtr  pInst;
      OdIfcModelPtr          pModel = pDatabase->getModel();
       if (pModel.isNull())
      {
        odPrintConsoleString(L"\nAn unexpected error occurred while opening the IFC file! Press any key to finish...");
        getchar();
        pDatabase = NULL;
        return arEmptyModel;
      }
       odPrintConsoleString(L"Model entities: \n");
       OdDAI::InstanceIteratorPtr it = pModel->newIterator();
      unsigned int entIdx;
       for (entIdx = 0; !it->done(); it->step(), ++entIdx)
      {
        //opens an entity
        pInst = it->id().openObject();
        pInst = it->id().openObject();
         if (!pInst.isNull())
        {
          odPrintConsoleString(L"Entity %d: \n", entIdx);
          odPrintConsoleString(L"\tEntity handle (corresponds to the STEP-id) = %d\n", it->id().getHandle());
          odPrintConsoleString(L"\tEntity type code = %d\n", pInst->type());
          odPrintConsoleString(L"\tEntity type name = %hs\n", pInst->typeName().c_str());
           if (pInst->isKindOf(OdIfc::kIfcRepresentationItem) || pInst->isKindOf(OdIfc::kIfcProfileDef))
            odPrintConsoleString(L"\tEntity is kind of kGeom\n");
          else
            odPrintConsoleString(L"\tEntity is kind of kBIM\n");
        }
      }
       odPrintConsoleString(L"Found entities: %d\n", entIdx);
       //Finalize the process, OdIfcFile and underlying header section and Model will be released.
      pDatabase = NULL;
     }
    catch (OdError& e)
    {
      odPrintConsoleString(L"\nError occurred: %ls! Press any key to finish...", e.description().c_str());
      pDatabase = NULL;
      return arOdError;
    }
    catch (...)
    {
      odPrintConsoleString(L"\n\nUnexpected error.");
      pDatabase = NULL;
      return arUnexpectedError;
    }
  }
   odIfcUninitialize();
  odDbRootUninitialize();
  ::odrxUninitialize();
   return arOk;
}
    

Return to the main guide

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