DgnPlatformNet |
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.
If you want to ask questions about C# development for MicroStation CONNECT, post your question to the MicroStation Programming Forum.
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.
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.
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; }
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.
Post questions about MicroStation programming to the MicroStation Programming Forum.