Label Lines with VBA

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

Q How do I label a line with its length?

Q How do I label a shape with its area and perimeter?

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. AreaAnnotator™ lets you annotate area objects with labels that update automatically if you modify the shape.

In contrast, the Low Cost Labeller mentioned below creates a label that updates dynamically.

Example Labels

Here's a line-string element created in a MicroStation DGN model using the built-in tools …

Line String with no Label

Here's the same line-string element after labelling with the VBA macro described here …

Line String with Label

Here's a shape element after labelling with the VBA macro described here …

Shape 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 LineElement, LineStringElement or ShapeElement
  2. Create a label for the element by formatting its length or perimeter 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.

History

MicroStation enthusiast Massimo Callegher wrote a MicroStation BASIC macro many years ago. MicroStation CONNECT does not include BASIC, so I seized the opportunity to write the VBA macro described here.

This 21st-century macro was written in response to a question in the Be Communities MicroStation Forum. It is supplied as freeware: you can use the macro for your own purposes whether private or commercial. LA Solutions offers the project as-is without any warranty of fitness for purpose. It should work with both MicroStation CONNECT and MicroStation V8i.

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.

Low Cost Labeller

The Low Cost Labeller is an application for MicroStation CONNECT. It provides a tool to label a line with its length. Low Cost Labeller goes beyond the capabilities of the VBA project described here. While this VBA project creates a static text label, Low Cost Labeller creates a label that includes a Text Field that displays the line's Length property. When you modify the line, the label updates automatically.

Low Cost Labeller is freeware!

Download LabelLines.ZIP

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

vba run [LabelLines]modMain.Main

Questions

Post questions about MicroStation programming to the MicroStation Programming Forum.