MicroStation VBA for AutoCAD Users

1 May, 2003 By: Barry Bowen

Common language automates tasks.

CAD departments often must exchange drawing files between AutoCAD and MicroStation. You may use subcontractors who work with MicroStation or have clients who require final as-builts in MicroStation. However it happens, when you find yourself in this situation, VBA (Visual BASIC for Applications) can help automate manual tasks.

MicroStation can't execute AutoLISP programs, but it does include a VBA interface similar to AutoCAD's. Both programs use Microsoft's Visual BASIC editor, where you can create VBA macros and more complex VBA projects with dialog box user interfaces. If you are new to MicroStation and not familiar with its commands, enter the VBA Macro Recorder.

This feature is provided with MicroStation to help you write VBA macros more quickly and easily. Although the code that it records may not be suitable for immediate playback, you can later revise the code to be more user friendly. This feature is extremely useful when I'm not familiar with a command structure. Autodesk should consider it for AutoCAD's future.

From the MicroStation Utilities menu, choose Macro | Create Basic Macro. In the Create Macro dialog box, name the macro, type a

Prototype VBA Program
‘ Place Line between two points
Sub main
    Dim startPoint As MbePoint
    Dim point As MbePoint, point2 As 

‘   Coordinates are in master units
    startPoint.x = 4.910987#
    startPoint.y = 24.905862#
    startPoint.z = 0.000000#

‘   Send a data point to the current 
    point.x = startPoint.x
    point.y = startPoint.y
    point.z = startPoint.z
    MbeSendDataPoint point, 1%

‘   Start a command
    MbeSendCommand “PLACE SMARTLINE “

    point.x = startPoint.x - 0.035987#
    point.y = startPoint.y + 0.281638#
    point.z = startPoint.z
    MbeSendDataPoint point, 1%

    point.x = startPoint.x + 5.776513#
    point.y = startPoint.y + 0.281638#
    point.z = startPoint.z
    MbeSendDataPoint point, 1%

‘   Send a reset to the current command
   End Sub                
description, select the location (drive and directory) for the macro, then select OK.

The recording process begins with the VCR-like buttons on the macro control panel. MicroStation now watches and records all your actions. Start performing the actions you want to record-selecting tools, placing elements, opening and closing dialog boxes, and so forth.

When you have performed all of the actions to be recorded, click the stop button in the control panel or select the pause button to temporarily suspend recording. Once recording stops, the control panel closes and the macro program ends.

This is the first step in creating a prototype VBA program that you can shape into its final form using the VBA editor. As an example, I placed a smartline between two randomly selected points, which was recorded in the code in the box at right.

As you can see, the macro generator records the exact locations of the points selected for the line. Every time the macro runs, the line appears in the exact same location. Therefore, to make the program more usable, you must edit the basic program.

To edit the macro, select Utilities | Macro | MicroStation Basic. The Macros dialog box appears with a list of macros for execution or editing. Select the macro you want, and then click Edit to open the Basic Editor that shows the recorded code.

Once you write the macro, you run it to execute the instructions. This is the same as playing back a VBA macro that was recorded using the Record feature. From within the editor, select the Run menu, then choose Run Sub | User Form.

That's it—MicroStation makes it easy to get started creating VBA macros without knowing the MicroStation BASIC language extensions. In future articles we'll explore the VBA connection in more detail.

AutoCAD Tips!

Lynn Allen

In her easy-to-follow, friendly style, long-time Cadalyst contributing editor Lynn Allen guides you through a new feature or time-saving trick in every episode of her popular AutoCAD Video Tips. Subscribe to the free Cadalyst Video Picks newsletter, and we'll notify you every time a new video tip is published. All exclusively from Cadalyst!

Follow Lynn on TwitterFollow Lynn on Twitter

Do you use social media — such as Facebook or Twitter updates, YouTube videos, or discussion forums — for work-related purposes?
Yes, I regularly use such resources for work-related purposes.
Yes, but on a limited or infrequent basis.
No, because my employer frowns upon or prohibits doing so.
No, because I don’t have the time or interest.
Submit Vote

Download Cadalyst Magazine Special Edition