VBA TIP: Maximize the MicroStation ElementEnumerator

Published on November 4, 2014
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   

Tags:
Like this article? Share it!

2 Responses to “VBA TIP: Maximize the MicroStation ElementEnumerator”

  1. Dale Says:

    September 8th, 2015 at 3:55 pm

    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 Says:

    September 9th, 2015 at 8:12 am

    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 Comment

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

Privacy Policy