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 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.
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.
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 …
The process to enumerate cells is similar …
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;
This is a multi-step process (if anyone can tell me a simpler way, I'll be happy) …
DgnDocument
DgnFileOwner
DgnFile
from the DgnFileOwner
ModelIndexCollection
from the DgnFile
ModelIndex
in the ModelIndexCollection
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
}
This is a multi-step process similar to the above …
DgnDocument
DgnFileOwner
DgnFile
from the DgnFileOwner
ModelIndexCollection
from the DgnFile
ModelIndex
in the ModelIndexCollection
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
}
Post questions about MicroStation programming to the MicroStation Programming Forum.