MicroStation Element Enumerator

Many MicroStation vba’s use the ElementScanCriteria to build a list of elements in a model that match a set of selection criteria. The ElementEnumerator object is then used to iterate through all of the elements in the list. The sub below is a common example of this.

Sub EE_Example()
Dim ee As ElementEnumerator
Dim es As ElementScanCriteria

'
' set element scan criteria to find only text elements
'
Set es = New ElementScanCriteria
es.ExcludeAllTypes
es.IncludeType msdElementTypeText

'
' set enumerator from active model
'
Set ee = ActiveModelReference.Scan(es)

'
' loop through array and replace text
'
Do While ee.MoveNext
ee.Current.AsTextElement.Text = "New Text"
ee.Current.Rewrite
Loop

End Sub

This can create problems if the elements are modified during this iteration as this list is dynamic. Modifying elements can affect the ordering of the list causing some elements to be processed multiple times and others to be skipped entirely. When you are required to modify elements it is safer to create an element array from the ElementEnumerator object and iterate through array. The sub below has been modified to use the array technique.

Sub EE_Example()
Dim ee As ElementEnumerator
Dim es As ElementScanCriteria
Dim elArray() As Element
Dim i As Long
Dim iStart As Long
Dim iEnd As Long

'
' set element scan criteria to find only text elements
'
Set es = New ElementScanCriteria
es.ExcludeAllTypes
es.IncludeType msdElementTypeText

'
' set enumerator from active model
'
Set ee = ActiveModelReference.Scan(es)

'
' get an element array of all elements found
'
elArray = ee.BuildArrayFromContents
iStart = LBound(elArray)
iEnd = UBound(elArray)

'
' loop through array and replace text
'
For i = iStart To iEnd
elArray(i).AsTextElement.Text = "New Text"
elArray(i).Rewrite
Next

End Sub

EnvisionCAD

EnvisionCAD

Since 1996, EnvisionCAD has been a nationally recognized leader in the configuration, customization, implementation, training & support for CAD software solutions. Our individualized approach has benefited private engineering firms and government agencies alike. Basic or advanced, we can help you get the most from your CAD technology.

Tags:

2 comments on “MicroStation Element Enumerator

  1. karpagalakshmi Reply

    I am trying to add this code in excel vba 2003 but i am getting compile error user defined tupe not defined

  2. Rod Wing Rod Wing Reply

    To use this code in Excel you must add a reference to the Bentley DGN object library. Once that is done you could open a dgn file in Excel extract text, coordinates, or other data from a dgn file and populate the cells of a worksheet.

Leave a Reply

Your email address will not be published. Required fields are marked *