How to Change a Dimension Element’s Settings Programmatically

If you have not caught up with Dimension Styles yet, and are still placing dimensions using just the active settings (Style: None), then you may have faced the challenge of having to propagate a change to all dimensions. Dimension styles make it really easy to do this, and we encourage you to use them. In this blog, I will explain how to do this programmatically in MicroStation VBA.

Say you have several dimensions in a sheet and you want to change all of them to use a text frame. You just have to write a few lines of simple code.

  1. Open Utilities > Macro > VBA Project Manager. 
  2. Start a new project and open it in Visual Basic Editor.
  3. Copy the code at the bottom of this blog. 



Here’s what the code is doing:

  1. Scan all graphical elements of the active model and find dimension elements
  2. Iterate through the list for each dimension element
  3. Get the dimension element’s dimension style
  4. Change the text frame type to Box
  5. Set the dimension style back on the dimension element
  6. Rewrite the dimension element and refresh it on the screen




 Sub ChangeDimensionTextFrame()
    Dim oDimStyle As DimensionStyle
    Dim oDim As DimensionElement
    ' Scan all graphical elements of the active model
    Set ee = ActiveModelReference.GraphicalElementCache.Scan
    Do While ee.MoveNext
        If ee.Current.IsDimensionElement Then
            Set oDim = ee.Current
            ' Get the dimension element's style object
            Set oDimStyle = oDim.DimensionStyle
            ' Change the setting in style
            oDimStyle.TextFrameType = MsdDimTextFrameTypeBox
            ' Set the dimension element's style object
            oDim.DimensionStyle = oDimStyle

        End If
End Sub


More blogs coming up, so be there...