Label Lines with VBA

Questions similar to these appear in the Be Communities MicroStation Programming Forum.

Q How do I label a contour with its height?

A This article describes a MicroStation VBA project that …

The macro lets a user either pick a single line or shape to be labelled, or works with the contents of a fence or selection set to label multiple lines or shapes.

Label Lines UserForm

Note that a label created using this VBA tool is static. The label doesn't update automatically if you modify a line or shape — the label stays the same. To update a label that was created using this tool, you must delete the label then run the tool again.

Dynamic Labels

If you want to create a label that updates automatically when a line or shape is modified, then use a Text Field. Unfortunately, VBA doesn't support (as of Update 17.1) text fields. Alternatively, use one of our TagWorks products.

Example Labels

Here's a set of contours created in a MicroStation DGN model using the built-in tools. The contours are a mix of lines, line-strings and shape elements …

Contours with no Label

Here's the same set of contours after labelling a couple with the VBA macro described here …

Contours with Label

Interactive Tool

Decide how you want to align the label relative to the element or view. Choose an option from the Rotation ComboBox …

The tool works interactively like this …

  1. Prompt user to pick a contour (LineElement, LineStringElement or ShapeElement)
  2. Create a label for the element by formatting its height (Z-value of the first point) as a human-friendly string
  3. If Snap Label to Line is checked …
    • Create the label automatically as a new TextElement
  4. Otherwise
    • Prompt user to place the label as a new TextElement
If MatchElement is selected, then the new label is rotated to match the nearest line segment. If View is selected, then the new label is rotated to match the active view. Otherwise, the label is created with no rotation.

Batch Tool

When a MicroStation selection set or fence is active the macro works in batch mode. It obtains an ElementEnumerator from the selection set or fence, then works through that collection, labelling each LineElement, LineStringElement or ShapeElement. The label is created at the mid-point of the LineElement or nearest segment of a LineStringElement or ShapeElement.

Labelled Line-String Aligned with Segment

When working in batch mode, there is no opportunity for the operator to place the label manually. The Snap Label to Line check box is checked but disabled.

TextStyle

This tool creates a text element and snaps it to the chosen contour. What you see depends on the text style. If you want to obscure the contour line beneath the text, then choose a text style having an opaque background.

Notes about MicroStation

Reports

MicroStation Reports let you harvest DGN data. For example, you can report line lengths — here's step-by-step guide to creating a Linear Report. You can export a Report to a CSV file, which you can open with Microsoft Excel …

Linear Report opened in Excel

Text Fields

A Text Field can display DGN element and other properties. The advantage of a Text Field over plain text is that a Text Field updates automatically when its related element is changed. In contrast, a label created using a plain old TextElement, such as those created by this Label Line tool, does not update automatically.

Download LabelContours.ZIP

Download the Label Contours ZIP archive, and unpack it to a suitable location such as \Workspace\Standards\macros. Start the macro using the key-in …

vba run [LabelContours]modMain.Main

Questions

Post questions about MicroStation programming to the MicroStation Programming Forum.