MicroStation .NET: Model Enumeration

  • Products
  • Services
  • All Publications
  • CONNECT Publications
  • Links
  • WishList
  • LA Solutions
  • LA Solutions Home Page
LA Solutions
tel: +44 1398 361 800
DgnPlatformNet

Introduction

This article is for C# developers who want to write an AddIn for MicroStation CONNECT. It is an introduction to the coding required to enumerate DGN models contained in a DGN file.

Collection Enumeration Diagram

Be Communities

If you want to ask questions about C# development for MicroStation CONNECT, post your question to the MicroStation Programming Forum.

Open a DGN File for Reading

The purpose of the code below is to obtain model information from a DGN file or cell library. We don't want to open the file to edit any model — opening the file for read-only is fine. A consequence of this approach is that the user does not see the file we've opened: MicroStation continues to show the active DGN model with no change.

IDisposable

A pecularity of this code is the requirement to create a DgnFileOwner as an intermediate step. The DgnPlatformNET tells us that DgnFileOwner implements IDisposable and moreover that we must call DgnFileOwner.Dispose() when finished. The exception-safe way to ensure that happens is to wrap the object in a using statement.

Models and Cells

A DGN file is a container. It contains one or more models. Each DGN model contains graphic elements.

A DGN file can be a cell library, in which case the models it contains are marked 'can be placed as cell' by the library designer. My requirement to find the cells in a cell library prompted this article: I wanted to enumerate the cells in a DGN cell library, which is the same process as enumerating the models in a DGN file.

The process to enumerate models is …

  • Open a DGN file for reading
  • Get the list of models in the DGN file

The process to enumerate cells is similar …

  • Open a DGN cell library for reading
  • Get the list of models in the library
  • Check whether the model is intended to be used as a cell

Namespaces

MicroStation .NET code lives in the Bentley namespace, or a namespace within the scope of Bentley. In this example we're using classes and methods in the Bentley.DgnPlatformNET namespace. Put the following using statements in any C# code module that uses a class in those namespaces …

using Bentley.DgnPlatformNET;

Open a DGN file for Reading

This is a multi-step process (if anyone can tell me a simpler way, I'll be happy) …

  • Create a DgnDocument
  • Create a read-only DgnFileOwner
  • Get the DgnFile from the DgnFileOwner
  • Get a ModelIndexCollection from the DgnFile
  • Do something with each ModelIndex in the ModelIndexCollection
  • Dispose of the DgnFileOwner

Here's the code that implements the above …

using (DgnDocument document = DgnDocument.CreateForLocalFile(fileName))
  using (DgnFileOwner dgnFileOwner = DgnFile.Create(document, DgnFileOpenMode.ReadOnly))
  {
    DgnFile file = dgnFileOwner.DgnFile;
    StatusInt status;
    file.LoadDgnFile(out status);
    ModelIndexCollection models = file.GetModelIndexCollection();
    foreach (ModelIndex model in models)
    {
     msg = $"Model '{model.Name}' Descr '{model.Description}' in DGN file '{fileName}'";
     MessageCenter.Instance.ShowMessage(MessageType.Debug, msg, msg, MessageAlert.None);
    }
    // dgnFileOwner.Dispose() called automatically when wrapped in using statement
  }

Open a Cell Library for Reading

This is a multi-step process similar to the above …

  • Create a DgnDocument
  • Create a read-only DgnFileOwner
  • Get the DgnFile from the DgnFileOwner
  • Get a ModelIndexCollection from the DgnFile
  • Do something with each ModelIndex in the ModelIndexCollection
    • For example, check the the model can be used as a cell
  • Dispose of the DgnFileOwner

Here's the code that implements the above …

using (DgnDocument document = DgnDocument.CreateForLocalFile(fileName))
  using (DgnFileOwner dgnFileOwner = DgnFile.Create(document, DgnFileOpenMode.ReadOnly))
  {
    DgnFile cellLib = dgnFileOwner.DgnFile;
    StatusInt status;
    cellLib.LoadDgnFile(out status);
    ModelIndexCollection models = cellLib.GetModelIndexCollection();
    foreach (ModelIndex model in models)
    {
    if (CellPlacementOptions.CanBePlacedAsCell == model.CellPlacementOptions)
    {
      msg = $"Cell '{model.Name}' Descr '{model.Description}' in Lib '{fileName}'";
      MessageCenter.Instance.ShowMessage(MessageType.Debug, msg, msg, MessageAlert.None);
      ++nCells;
    }
    }
    // dgnFileOwner.Dispose() called automatically when wrapped in using statement
  }

Questions

Post questions about MicroStation programming to the MicroStation Programming Forum.


Bentley Technology Partner

Trademarks

All trademarks are acknowledged. Trademarks may be registered in some jurisdictions.
e-mail Contact LA Solutions using this enquiry form.

Home
Home
Updated on 15-Jul-2020 Copyright © 2020…2025 Jon Summers