Questions similar to this appear on the Be Communities. This problem appeared in the MicroStation Programming Forum.

Q

A MicroStation text elements contain any type of text: letters, numbers or punctuation. By numeric text we mean those text elements that display numeric data, and only numeric data. For example, the following are considered, for the purpose of this article, to be numeric …

Examples of non-numeric text are …

Introduction

This article describes a MicroStation VBA project SumNumericText.mvba. The project includes several modules …

  1. The Main entry point in modMain
  2. A UserForm named frmSumNumericText
  3. A text placement class that Implements IPrimitiveCommandEvents
Sum Numeric Text UserForm

Selection Set

A MicroStation selection set contains those elements that a user has selected using one of the selection idioms. You can select elements within a rectangle you draw with the mouse, or add elements to a selection set using MicroStation's choose element tool.

This utility expects to find numeric text elements in a MicroStation selection set. If there is no selection set, then it does nothing. It considers only text elements and ignores other types of element. Optionally, you can include text node elements. Of the text elements it finds, it operates only on those that contain text considered to be numeric.

Performing Arithmetic on Text Elements

This utility works with numeric text elements. For each numeric text element, it attempts to convert the text content to a number. It accumulates the values found in each text element in the selection set. It displays the total in the Sum Numeric Text  UserForm.

Load the SumNumericText.mvba project from MicroStation's Utilities|Macros|Project Manager menu. Open MicroStation's keyin window, and run the VBA macro using this keyin …
vba run [SumNumericText]modMain.Main
The Sum Numeric Text UserForm appears …

Sum Numeric Text UserForm showing computed total

There are three text boxes that show …

  1. The arithmetic total of the numbers found from text elements in a selection set
  2. The number of numeric text elements found
  3. The average value of the numbers found

Initially all three text boxes show zero, as you would expect.

There is a check box labelled Include Text Nodes. Set the check if you want to include numeric text found in text node elements. Clear the check if you want to exclude text in text node elements.

Harvesting Numeric Text Elements

This utility works with numeric text elements. The elements may be embedded in a text node element or they may be individual text elements.

  1. Create a select set that contains at least one numeric text element
      Optionally, set or clear the Include Text Nodes check box
  2. Press the Harvest Data button

SumNumericText gets each numeric text element in the selection set, extracts its value, and accumulates the total. It displays the total in the UserForm.

In this example, the user has selected four text elements, having content 123, 456, 789 and abc. The user has also selected a text node element that contains three text elements having content 111, 222 and 333. You can see that the number of numeric text elements found is six (because abc is not numeric) and the calculated total is 2,034. The calculated average is 339.0.

Place the Total as a new Text Element

Press the Place Total button to create a new text element in your DGN model. The text element contains the same text as that shown in the Total box in the UserForm.

Sum Numeric Text with total placed as text element

Download the SumNumericText VBA Project

Download SumNumericText.ZIP

Download SumNumericText.ZIP (which contains SumNumericText.mvba) and run it by following these instructions …

  1. Copy SumNumericText.mvba to one of the folders in your computer's VBA search path (MS_VBASEARCHDIRECTORIES).
    C:\ProgramData\Bentley\Organization\Standards\Macros is a good choice
  2. Open MicroStation's key-in window from the Utilities menu
  3. Start the utility with the following key-in …
    vba run [SumNumericText].modMain.Main

You will see the Sum Numeric Text UserForm.

Questions

Post questions about MicroStation programming to the MicroStation Programming Forum.