Bentley Communities
Bentley Communities
  • Site
  • User
  • Site
  • Search
  • User
  • Welcome
  • Products
  • Support
  • About
  • More
  • Cancel
MicroStation
  • Product Communities
  • More
MicroStation
MicroStation Wiki How To Write A Recursive Routine In MicroStation VBA [CS]
    • Sign in

    • +MicroStation Wiki
    • +Administration Wiki
    • +Annotations Wiki
    • +Bentley View Wiki
    • +MicroStation PowerDraft
    • -Programming Wiki
      • A class to help create and modify text element
      • A Complete Example
      • A MicroStation VBA Example With Bentley ProjectWise
      • AddIn Development Using VB.NET
      • C# .NET Template with IPrimitiveCommandEvents Class
      • Capturing Graphics in Dynamic Views
      • Compiling MDL Applications
      • Database Manipulation
      • Debugging Native Code MDL Applications
      • Developing Code in VBA
      • Developing MicroStation Applications For DWG Files
      • Drag and Drop in MicroStation
      • Error: "Cannot save changes to VBA project 'Support' because it is read-only"
      • Getting And Setting Elements Using Graphic Groups In VBA [CS]
      • Getting Started with Visual Basic
      • How To Write A Recursive Routine In MicroStation VBA [CS]
      • Introducing Segment3D Methods In MicroStation V8 2004 Edition
      • Known issues in MDL and MicroStationAPI
      • Launching VBA Applications As Initapps Or Dgnapps [CS]
      • Learning MicroStation Addins Step by Step
      • MDL - Getting Started With XAttributes In MicroStation V8 XM Edition
      • MDL - Native Code Application Development
      • MDL Or MicroStation BASIC Choosing The Right Tool [TN]
      • MFC Dialog And Native Window Support
      • Microsoft Office VBA Patch Utility
      • MicroStation BASIC FAQ
      • MicroStation BASIC Limits [FAQ]
      • MicroStation Developer Documentation and Example Code
      • MicroStation Programming Advice
      • MicroStation SDK
      • MicroStation V8 Programming Tools Readme
      • MicroStation V8 VBA Programming Resources [CS]
      • MicroStation V8 XM Edition View Code Updates
      • MicroStation VBA Resources Revisited [CS]
      • Migrating Dimension Code To MicroStation V8
      • Migrating MDL Applications To Native Code
      • Mouse Wheel Events And The Visual Basic 6.0 IDE
      • Porting MDL Applications To MicroStation V8
      • Reading Elements From A Microsoft Access Database With VBA [CS]
      • Running MDL Applications
      • Scanning For MicroStation V8 Elements In VBA [CS]
      • Unleash A Workspace Wizard Automating Workspace Creation With MicroStation V8 And VBA [CS]
      • Using VBA To Detect The Current Or Last Used Command
      • Using VBA To Programmatically Export A VBA Project [CS]
      • Using VBA To Programmatically Import A VBA Projects Components And References [CS]
      • VBA -- Creating A Complex Application
      • VBA Interface Error: failed creating the comp manager - 0x80040583
      • VBA interface error: unable to get IDE
      • vba recording
      • Working With Levels In MicroStation VBA [CS]
      • Writing An MDL Application To Output Custom Placemarks To Google Earth
      • [V8i C++] PointCloud IPointCloudChannelDisplayHandler
    • +Visualization Wiki

     
     Questions about this article, topic, or product? Click here. 

    How To Write A Recursive Routine In MicroStation VBA [CS]

     

    This Client Server article is republished in its entirety from 2003 for reference purposes.

    By Yuedong Bi, Developer Support Analyst, Bentley Corporate Office
    05 May 2003

     

    This article describes how to write a recursive routine to solve a complex problem by solving progressively simpler problems, until a final base case can be reached. There are many practical applications to apply the theory of recursion for any nested object in MicroStation objects such as cells, attachments, etc.

    What is recursive method?

    Recursive definitions are common in mathematics. Any series in which a term is defined using the values of earlier terms in the series is a recursive definition. For example, the sum of the first n integers can be defined as the value of n plus the sum of the first n-1 integers.

    Here is a recursive function to compute this sum:

    Function sumNumber (n As Integer) As Integer

    If n <= 1 Then ‘Base case

    sumNumber = 1

    Else ‘ the recursive call

    sumNumber = sumNumber(n - 1) + n

    End If

    End Function

     

    As illustrated above, you can see that a recursive function has two cases:

     

    1. The base case - If the problem is simple enough, it is solved without recursive calls. In the above example, the sum of the 1st integer is 1.

    2. The recursive call - The function calling itself to solve a simpler problem, as in the above example, would call itself by calculating the sum of the current n-1 integers.

    Recursive method usage in Microstation VBA

    As mentioned previously, a recursive function has many practical applications in MicroStation. For example, in the managing of nested cells, the base case is always a primitive element (such as line, shape, text, ellipse, etc.), which cannot be broken down to a simpler case to manage.

    For example:

    To change the color of a shape element, we can:

    Dim oShapeElement As ShapeElement
    ‘ ....... Get oShapeElement from scan,
    ‘ user pick from model, or create one
    oShapeElement.Color = 4 'Change element color
    ‘rewrite changes to its model.
    oShapeElement.Rewrite
    ‘Redraws this object on the view
    ShapeElement.Redraw msdDrawingModeNormal
    To change the font of a text element:

    Dim oFont As Font
    Set oFont = ActiveDesignFile.Fonts(3)
    With oTextElement
    .TextStyle.Font = oFont
    .Rewrite
    End With
    To process a cell element, the solution to apply would use an ElementEnumerator similar to:

    Dim oElEnum As ElementEnumerator
    Set oElEnum = oCellElement.GetSubElements
    Do While oElEnum.MoveNext
    ‘ ..... Process each element inside a cell
    Loop

    In a recursive call, the function will keep calling itself to solve a simpler problem until the base case has been satisfied.

    This code illustrates how a recursive function call is used to list all elements within a nested cell:

    Function processNestedCell (oCellElement As _

    CellElement) As Variant

    Dim oElEnum As ElementEnumerator

    Dim oSubElement As Element

    Set oElEnum = oCellElement.GetSubElements
    Do While oElEnum.MoveNext

    Set oSubElement = oElEnum.Current

    If oSubElement.Type = msdElementTypeCellHeader _

    Then

    Debug.Print "Nested Cell Name: " & _ oSubElement.AsCellElement.Name

    ' Recursive call

    processNestedCell oSubElement.AsCellElement

    Else

    ' Base case, not a cell element,

    ‘ If needed, you can check more type,

    ' Such as if type is text or text node

    ‘ do something, ....

    Debug.Print oSubElement.Type

    'More control on each type element.

    ‘Such as change Ellipse color

    If oSubElement.Type = _

    msdElementTypeEllipse Then

     

    oSubElement.AsEllipseElement.Color = 4

    'Change element color

    oSubElement.Rewrite

    oSubElement.Redraw msdDrawingModeNormal

    End If

    End If

    Loop

    End Function

     

    Upon receiving a list of elements within a complex nested cell you can now easily manage and process each individual base case elements, performing any required operation such as changing the symbology or properties.

    Note: If you are performing an operation that would attempt to change the size of a complex object it is recommended that you do not simply delete individual elements in cell. Instead, create a new cell with of the correct size and delete the old one.

    Where should I use recursion?

    Try taking these concepts further and see if you can use recursion to manage other complex element types in MicroStation such as reference files, etc.

    See Also

    Client Server Archive

    MicroStation Desktop TechNotes and FAQs

    Comments or Corrections?

    Bentley's Technical Support Group requests that you please confine any comments you have on this Wiki entry to this "Comments or Corrections?" section. THANK YOU!

     

    • Archived Client Server
    • client server
    • client server 2003
    • Share
    • History
    • More
    • Cancel
    • Elisa McGraw Created by Elisa McGraw
    • When: Wed, Jul 15 2009 12:17 PM
    • Dan Koval Last revision by Bentley Colleague Dan Koval
    • When: Mon, Aug 26 2013 9:07 AM
    • Revisions: 11
    • Comments: 1
    Recommended
    Related
    Communities
    • Home
    • Getting Started
    • Community Central
    • Products
    • Support
    • Secure File Upload
    • Feedback
    Support and Services
    • Home
    • Product Support
    • Downloads
    • Subscription Services Portal
    Training and Learning
    • Home
    • About Bentley Institute
    • My Learning History
    • Reference Books
    Social Media
    •    LinkedIn
    •    Facebook
    •    Twitter
    •    YouTube
    •    RSS Feed
    •    Email

    © 2021 Bentley Systems, Incorporated  |  Contact Us  |  Privacy |  Terms of Use  |  Cookies