MicroStation VBA for AutoCAD Users

30 Apr, 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.

More News and Resources from Cadalyst Partners

For Mold Designers! Cadalyst has an area of our site focused on technologies and resources specific to the mold design professional. Sponsored by Siemens NX.  Visit the Equipped Mold Designer here!

For Architects! Cadalyst has an area of our site focused on technologies and resources specific to the building design professional. Sponsored by HP.  Visit the Equipped Architect here!