MicroStation .NET: Is MicroStation File?

  • Products
  • Services
  • All Publications
  • CONNECT Publications
  • Links
  • WishList
  • 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.

Be Communities

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

Test if MicroStation can Read a File

The purpose of the code below is to test whether a file can be opened by MicroStation. We're not testing Windows file attributes such as read-only. It answers the question: Is this a DGN format file?  MicroStation DGN documents (*.dgn), cell libraries (*.cel) and design libraries (*.dgnlib) are all DGN files.

Although the code loads a DGN file, it does not make it the active file. That is, the MicroStation operator sees no change in her view of the active DGN file when this code is executed.

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.

Implementation

Your C# project must reference the appropriate assemblies …

using Bentley.DgnPlatformNET;

Test a file to find whether it's compatible with MicroStation …

public bool IsDgnFile(string filePath)
{
  using (DgnDocument document = DgnDocument.CreateForLocalFile(filePath))
  using (DgnFileOwner owner = DgnFile.Create(document, DgnFileOpenMode.ReadOnly))
  {
    DgnFile candidate = owner.DgnFile;
    StatusInt openForWriteStatus;
    if (DgnFileStatus.Success == candidate.LoadDgnFile(out openForWriteStatus))
    {
      DgnFileFormatType format = DgnFileFormatType.Invalid;
      int majorVersion = 0;
      int minorVersion = 0;
      StatusInt getVersionStatus = candidate.GetVersion(out format, out majorVersion, out minorVersion);
      if (StatusInt.Success == getVersionStatus)
      {
        return (format != DgnFileFormatType.Invalid);
      }
    }
  }

  return false;
}

Acknowledgements

My thanks go to Jan Šlegr, a frequent contributor to the MicroStation Programming Forum. Jan offers indispensible advice in response to questions posted to the MicroStation Programming Forum. He helped to demystify the DgnPlatformNET API and suggested code changes that enabled me to improve IsDgnFile and write this article.

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