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 graphic elements in a DGN model.
If you want to ask questions about C# development for MicroStation CONNECT, post your question to the MicroStation Programming Forum.
A DGN model is a container. It contains graphic elements, such as lines, arcs and shapes. If you want your code to do anything with MicroStation elements, you must understand ways to obtain those elements.
Enumerating a DGN model is one way to obtain elements (other ways are by using a fence, selection set or MicroStation's locate engine).
There are two ways to enumerate elements in a DGN model …
Scanning and enumeration offer similar functionality and performance. One benefit of scanning over enumeration is that the scanner lets you specify a spatial filter. That is, you can obtain elements that pass a range check. If you don't need a spatial filter then prefer to use an enumerator as described here.
There are classes that implement both techniques in the downloadable project.
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
and Bentley.DgnPlatformNET.Elements
namespaces.
Put the following using
statements in any C# code module that uses a class in those namespaces …
using Bentley.DgnPlatformNET; using Bentley.DgnPlatformNET.Elements;
If you have a background in VBA or MDL you are probably already familiar with MicroStation's scanner. The scanner is a software mechanism for iterating a DGN model and examining each element.
See the Model Scanning article for more advanced code to filter DGN elements.
We're using the Bentley.DgnPlatformNET.ScanCriteria
class in this example.
ScanCriteria
requires a DGN model reference to scan.
Usually, you want to seek graphic elements.
Here's what you need …
ScanCriteria criteria = new ScanCriteria(); criteria.SetDrawnElements(); criteria.SetModelRef(model_);
Now you're almost ready to scan the DGN model. But there's one last assignment to make — your callback function. The ScanCriteria.Scan() method requires a function reference that matches this delegate definition …
public delegate StatusInt ScanDelegate(Element element, DgnModelRef modelRef)
Your callback delegate function should match that signature. Here's a simple callback delegate …
public StatusInt ElementProcessor(Element el, DgnModelRef modelRef) { string s = $"Element ID {el.ElementId} type {el.ElementType}"; MessageCenter.Instance.ShowMessage(MessageType.Debug, s, s, MessageAlert.None); return StatusInt.Success; }
With that final piece in place, you're ready to scan …
criteria.Scan(ElementProcessor);
File Scanner.cs
in the ElementEnumerator
project
assembles the code in a single Scanner
class.
It's part of the downloadable project.
Enumeration, compared with scanning, is absurdly simple …
ModelElementsCollection elementsCollection = dgnModel.GetGraphicElements(); foreach (Element el in elementsCollection { string s = $"Element ID {el.ElementId} type {el.ElementType}"; MessageCenter.Instance.ShowMessage(MessageType.Debug, s.ToString(), s.ToString(), MessageAlert.None); }
That's it!
You'll note that there's no way to filter the ModelElementsCollection
.
It gives you all the elements in the specified model.
If you don't want them all, you must filter the collection after you've created it.
DgnModel.GetGraphicElements()
returns a ModelElementsCollection
, which is an alias of IEnumerable<Element>
.
File Enumerator.cs
in the ElementEnumerator
project
assembles the code in a single Enumerator
class.
It's part of the downloadable project.
This MicroStation AddIn has a command table, in file Commands.xml
.
The command handlers are in class KeyinCommands
.
You can key-in the commands below in MicroStation's key-in dialog.
Commands are not case-sensitive …
Command | Comment | |
---|---|---|
ELEMENTENUMERATOR | SCAN | Execute ScanCriteria.Scan() |
ELEMENTENUMERATOR | ENUMERATE | Execute the Enumerator |
After keying-in either of the above commands, observe MicroStation's Message Center. Make sure that the debug option is enabled (right-click in the Message Center to see its options). You will see a list of the element IDs and types discovered.
You can download the Element Enumerator Project Visual Studio project. The project is built using Visual Studio 2017 and .NET Framework 4.6.2 as required by MicroStation CONNECT Update 13. We believe that the project will also build with Visual Studio 2019.
The project is complete: it includes all source code, the command table XML file,
the Visual Studio solution file and is set up to build a DLL.
The DLL is placed in your ..\MicroStation\mdlapps
folder.
It's similar, in that respect, to the .NET examples delivered with the MicroStation CONNECT SDK.
Build the project, which will place file ElementEnumerator.DLL
in your ..\MicroStation\mdlapps
folder.
Start MicroStation and open the key-in window (from the Utilities ribbon group).
Key-in...
mdl load ElementEnumerator
You should see a message confirming that the application is loaded. Next, key-in either of the commands shown above. You will see a list of element IDs and type descriptions in the Message Center.
Post questions about MicroStation programming to the MicroStation Programming Forum.