VBA TIP: Maximize the MicroStation ElementEnumerator

Written by Rod Wing
 

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
        Case ActiveModelReference.AnyElementsSelected
            Set ee = ActiveModelReference.GetSelectedElements
        ' Process fence contents if fence is defined
        Case ActiveDesignFile.Fence.IsDefined
            Set ee = ActiveDesignFile.Fence.GetContents
        ' Build EE using scan criteria
        Case Else
            Dim es As ElementScanCriteria
            Set es = New ElementScanCriteria
            With es
                .ExcludeAllTypes
                .IncludeType msdElementTypeText
            End With
            Set ee = ActiveModelReference.Scan(es)
    End Select
    ' 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
        elArray(i).Rewrite
    Next
End Sub
Last Month’s Tips:
 
Corridor Modeled Using AutoCAD Solids          RealDWG Chart          Inroads Roadway Designer Display References          IFF Statement
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 ◊
EnvisionCAD Group EnvisionCAD YouTube Channel   

Rod Wing

Rod is the Senior Systems Analyst at EnvisionCAD and has extensive experience in GIS, cartography, plotting, scanning, raster editing/manipulation, publishing, and file/data translations. He instructs MicroStation classes ranging from fundamentals to advanced.

Tags:

2 comments on “VBA TIP: Maximize the MicroStation ElementEnumerator

  1. Dale Reply

    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:

    While ee.MoveNext
    ee.Current.Color = idColor
    ee.Current.Rewrite
    Wend

  2. Rod Wing Reply

    Dale,
    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.

Leave a Reply

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