Introduction

The MicroStation Development Library (MDL) and MicroStationAPI provide APIs for developers wanting to create custom applications for MicroStation® from Bentley Systems. We create a MicroStation application as a DLL, written using C++ and built with the Microsoft C++ compiler and linker provided with Visual Studio.

When editing your source code, you can choose whether to use Microsoft Visual Studio, Microsoft Visual Studio Code, or one of your favourite text editors.

When building your app, you can use Visual Studio or the Bentley Systems make (bmake) tools.

Iterating Models in a DGN File

On the Iterators main page I provide an introduction to the various enumerators and iterators available to the C++ programmer in the MicroStationAPI. Here, I'll focus on ways to enumerate the DGN models contained in a DGN file.

Collection Enumeration Diagram
Object Description Example
ModelIndex Each DGN file has a list of models, stored in the model index ModelIndex Example
Loaded Models Collection A list of DGN models that are currently loaded Reachable Model Collection Example

ModelIndex Example

#include <DgnPlatform/DgnFileIO/dgnitemindex.h>
ModelIndex const& modelIndex { ISessionMgr::GetActiveDgnFile ()->GetModelIndex () }
///	This method reveals all models, whether loaded or not
for (ModelIndexItemCR item: modelIndex)
{
  /* do something with ModelIndexItem */
}

DGNLib ModelIndex Example

If you want to enumerate models in a design library (DGNLib) then you must do a little more work. A DGNLib is not an active file or reference, and its model collection is not necessarily loaded when you visit it in your code. In Bentley developer terminology, its caches are not filled. Consequently, you must ensure that its models are at least partially loaded when you examine them.

The following example will enumerate models found in a DGN file, whether that file be the active DGN file or a DGNLib …

void EnumerateModels (DgnFileP pDgnFile)
{
  const bool FillCache        {true};
  const bool LoadRefs         {true};
  const bool ProcessAffected  {true};
  for (ModelIndexItemCR item: pDgnFile->GetModelIndex ())
  {
    ModelId      modelId {item.GetModelId ()};
    StatusInt    status  {0};
    DgnModelP    model    = pDgnFile->LoadRootModelById (&status, modelId, FillCache, !LoadRefs, !ProcessAffected);
    ModelInfoCP	 pInfo    = model->GetModelInfoCP ();
    //... do something with model
  }
}

Reachable Model Collection

#include <DgnPlatform/DgnFile.h>

DgnFileR file = *ISessionMgr::GetActiveDgnFile ();
///	If models aren't loaded, this method doesn't reveal them
for (DgnModelP model: file.GetLoadedModelsCollection ())
{
  // do something with model 
}

Legacy Iteration using MDL

The mdlModelIterator_api is a typical C-style function family. The coder — that's you, dear reader — is responsible for memory-management.

DgnFileP dgnFile = /* get DGN file from somewhere */
DgnIndexIteratorP pDgnIndexIterator = mdlModelIterator_create (dgnFile);
while (pDgnIndexIterator)
{
  DgnIndexItemP indexItem = mdlModelIterator_getNext (pDgnIndexIterator);
  // do something with DgnIndexItem 
}
/* don't forget to deallocate memory */
mdlModelIterator_free (pDgnIndexIterator);

Back to the Iterator main page.

Questions

Post questions about C++ and the MicroStationAPI to the MicroStation Programming Forum.