VBA TIP: Maximize the MicroStation ElementEnumerator
The MicroStation ElementEnumerator object can be populated from a variety of sources: scan criteria, fences, selection sets, and complex elements. Once the ElementEnumerator object is set it can be used to iterate through all of the elements in the list and modify them if necessary.
Developing general purpose, reusable, routines to process ElementEnumerators can make your programs more flexible, shorten development time, and make them much easier to maintain.
The code below illustrates the different ways to build an ElementEnumerator in one routine then pass it to another for processing. The EnumerateExample Sub shows the different methods to build an ElementEnumerator. It then calls the ChangeColor sub which takes an ElementEnumerator and a color number as arguments to change the color of all the elements in the enumerator. This example can be expanded to define ElemenentEnumerators in varying modules and routines then call different routines to process for the desired result.
Sub EnumerateExample(Optional elComplex As ComplexElement = Nothing)
Dim ee As ElementEnumerator
Select Case True
' process components of a complex element if defined
Case Not (elComplex Is Nothing)
Set ee = elComplex.GetSubElements
' Use active selection set if any elements selected
Set ee = ActiveModelReference.GetSelectedElements
' Process fence contents if fence is defined
Set ee = ActiveDesignFile.Fence.GetContents
' Build EE using scan criteria
Dim es As ElementScanCriteria
Set es = New ElementScanCriteria
Set ee = ActiveModelReference.Scan(es)
' Pass the ElementEnumerator off to a different sub/function for processing
ChangeColor ee, 1
End Sub ' ' '
' This sub changes the color of all elements in the enumerator, no matter how the enumerator was created. ' It can easily be modified to change other element properties '
Sub ChangeColor(ee as ElementEnumerator, idColor as Long)
Dim elArray() as Element
Dim i as Long
Dim iStart as Long
Dim iEnd as Long
' create an element array from enumerator
elArray = ee.BuildArrayFromContents
iStart = LBound(elArray)
iEnd = UBound(elArray)
' loop through array and change color
For i = iStart To iEnd
elArray(i).Color = idColor
End SubLast Month’s Tips: Civil 3D Tip: 2D MicroStation Tip: Selecting InRoads Tip: Display VBA Tip: Pick
Measurements on 3D Objects your DWG Version References in Roadway Designer the Right Element Don’t want to miss out on other great information? Subscribe to this blog or our monthly eNewsletter now! Learn More ◊ Contact us today ◊ Newsletter ◊
2 Responses to “VBA TIP: Maximize the MicroStation ElementEnumerator”
- Dale Says:
Once the ElementEnumerator is defined, what is the purpose for creating an array from it, and defining the LBound and UBound of that array? Why not just use the following:
ee.Current.Color = idColor
- Rod Wing Says:
That is a very good question.
The ElementEnumerator is dynamic. Performing an Element.Rewrite has the potential to reorder the elements in the design file which in turn would reorder the elements found using ee.MoveNext. This could cause some of the elements in your selection to be processed multiple times and others not at all. Creating an element array eliminates that possibility as the elements will not not change position within the array.
If you are not modifying the elements I would agree that the ee.MoveNext is the way to go.