AutoCAD

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.

RECORD A VBA MACRO IN MICROSTATION
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 
    MbePoint

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

‘   Send a data point to the current 
    command
    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
    MbeSendReset
   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.

RUN A VBA MACRO
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

Autodesk Technical Evangelist Lynn Allen guides you through a different AutoCAD feature in every edition of her popular "Circles and Lines" tutorial series. For even more AutoCAD how-to, check out Lynn's quick tips in the Cadalyst Video Gallery. Subscribe to Cadalyst's Tips & Tricks Tuesdays free e-newsletter and we'll notify you every time a new video tip is available. All exclusively from Cadalyst!
Follow Lynn on Twitter Follow Lynn on Twitter


Latest News from Cadalyst Partners
Feed
Place your DWG File on a Diet   18 Jun, 2013

I received an email from blog reader Steve Roberts with a suggested tip topic on how to reduce the size and clean up DWG drawing files using four...More>>Read all Between the Lines blog posts>>

Feed
Tech Toys 360: Pro-Form Le Tour de France Bike   18 Jun, 2013

The greatest bike race in the world is held June 19 to July 21 this year. Celebrating its 100th year, the Tour de France sends riders through more...More>>Read more PTC Creo blog posts>>

Feed
Update 3 now available for Revit 2013 Products - Revit Clinic   18 Jun, 2013

...More>>Read more BIMbuilder blog posts>>

Feed
Nice Design—What Does It Feel Like?   17 Jun, 2013

...More>>Read more SolidWorks Blog posts>>

Feed
LiveMap: The Augmented Reality Motorcycle Helmet of the Future?   18 Jun, 2013

Philippe Starck once famously said that there should be no cars on the road—-only motorcycles. While some soccer moms may disagree, his theory...More>>Read more SolidSmack blog posts>>

Poll
How many years' experience do you have using CAD software?
Less than two years
2–5 years
6–10 years
11–20 years
21+ years
Submit Vote




FREE Resources for CAD, BIM, and PLM Users





Subscribe Cadalyst Newsletters