This article is written for an audience of Visual Basic for Applications (VBA) programmers. Specifically, VBA programmers wanting to write code for Bentley Systems MicroStation® VBA.
Questions similar to this appear on the Bentley Discussion Groups.
Q How can I measure a LineElement
?
A
We can write a locator class that lets a user select a LineElement
,
then measure the length of the selected element.
This article shows how to start a Locator class from a UserForm
,
locate a LineElement
,
then display the line's length on the UserForm
.
Q Communication with a class …
Implements ILocateCommandEvents
?
A A class object can include private variables that you can assign using a class Property
.
For example, if you have a UserForm
that starts a locator class, then you can set a property in your
locator that informs it about its originator.
The locator class can then call methods of the originator.
This article shows how to create a Locator class from a UserForm
,
then set a reference to the UserForm
in the locator class.
The locator class can subsequently user that reference to call methods or properties of the UserForm
.
A line is represented by a LineElement
object.
One of its properties is Length
(in the model's master units).
We need to locate a LineElement
in a model and
display its length on a form.
From the user's perspective, the sequence of operation is …
UserForm
shows
LineElement
UserForm
As a VBA programmer, you'll see that the UserForm
creates the locator class
and sets a reference to itself.
The locator class then has a mechanism to call methods & properties of its originator.
Next, the code invokes CommandState.StartLocate
that starts the locate events.
When the user has identified a valid element, the locator class
sets the UserForm
's Length property.
The locator is a VBA class module that
Implements ILocateCommandEvents
.
We customise the event handlers to accept only LineElement
s.
We include a reference to the UserForm
that called the class,
then use that form's methods to show the length of the located element.
The key to inter-class communication is the member variable that holds a reference to the originating class …
' ---------------------------------------------------------------------
' Member variable stores a reference to the UserForm that called this
' locate class
' ---------------------------------------------------------------------
Private m_oOriginator As frmLocator
' ---------------------------------------------------------------------
' Property lets us assign our form to this variable
' e.g. Set oLocator.Originator = Me
' ---------------------------------------------------------------------
Public Property Set Originator(ByRef oForm As frmLocator)
Set m_oOriginator = oForm
End Property
The UserForm
is a VBA form module.
The Measure Line button has a _Click
event that creates the locator class,
sets the property that references itself,
then starts the locate operation …
Private Sub cmdMeasure_Click()
' Start a new locate command by invoking our class clsLocator
Dim oLocator As New clsLocator
Set oLocator.Originator = Me
CommandState.StartLocate oLocator
End Sub
The VBA project has three modules …
modMain
that contains the project entry point subroutine Main
clsLocator
that Implements ILocateCommandEvents
frmLocator
that provides a user interface
Start the tool using the MicroStation keyin: vba run [MeasureLine]modMain.Main
You can download the Measure Line MVBA project.
The project includes the MVBA project MeasureLine.mvba
.
C:\Program Files\Bentley\Workspace\Standards\vba
vba run [MeasureLine]modMain.Main