VBA: How to Start Scripting in MicroStation

Today we’re going to jump into the wildly specific world of MicroStation, which is yet another software that’s automatable with Visual Basic for Applications.

Just like with Excel, the easiest way to start scripting in MicroStation is to record a macro of yourself doing a task, and then let the program write the VBA for you. From there, you can customize.

Never done that before? No worries–we’re gonna do a walk through.

Step 1: Record a Macro

Fire up a new DGN file. Then mosey to the Utilities tab. There’s a group there called Macros where we’ll be working. Hit Record.

A suggestion before you continue. Start with something simple. Draw some shapes. Play with colors and line weights. For the purposes of this example, I’m just plotting some circles. Then hit Stop. Like so.

Okay, when you hit stop, MicroStation is going to open a dialog box to save the macro as a bmr file. That’s where you can name it.

Step 2: Promote the macro to VBA

Going back to the Macros, you’ll see the name now with a little piece of paper next to it in a dropdown menu. The pencil icon next to it is an Edit button.

If you click that Edit button, it’ll give you a breakdown of what was recorded, and you can actually do some customization there. BUT we’re going to skip that for the time being and go straight to the “Promote macro to VBA” button. This one will create an mvba file.

You’ll notice the name is now BmrCircle, and the little icon is a little dialog box instead of a piece of paper.

An important sidenote here. You might be wondering where the heck all this gets saved.

Short answer? Probably in your local Documents folder.

Long answer? There’s a configuration variable where you can set the location for Microstation to save bmr and mvba files.

If you go to File->Settings->Configuration->Configuration Variables, you’ll get a dialog box where you can check and change the file paths where macros get saved. It starts out as your Documents folder.

End of sidenote.

Step 3: Edit the VBA

Now that you’ve got an mvba file, you can hit Edit (pencil icon) and get the Visual Basic Editor. You’ll see a module called ModCircle with a subroutine called BmrCircle() inside it. That’s where the script lives. And luckily, it comes complete with comments in green that make it somewhat more readable.

This one is very simple–it’s essentially telling MicroStation to start the Place Circle dialog box with SendKeyin “Place Circle Icon”. The point.X, point.Y, and point.Z lines prior to those data points tell MicroStation exactly where to click. The CadInputQueue.SendDataPoint point, 1 lines are mouse clicks.

From here, you can customize and run things like loops to pull off some fun tricks.

A Really Simple Example

For instance, you could have MicroStation insert 10 circles, each with a radius 1 meter longer than the last by only adjusting the point.X variable within a For loop.

Now, if you go back and make sure BmrCircle is in the dropdown menu, you can hit Play, and the result looks like this:

A Slightly More Complicated Example with User Input

That last one had hard coded coordinates at (0,0,0) for our circles to radiate around. But what if we wanted to have them radiate around a spot the user clicks? You can do that with a few quick changes.

The lines below to tell MicroStation it should either wait for a left click (data point) or right click (reset). From there, you can use If statements to guide the logic and message.point to get the coordinates of the click.

Dim message As CadInputMessage
Set message =CatInputQueue.GetInput(msdCadInputTypeDataPoint, msdCadInputTypeReset)
If message.InputType = msdCadInputTypeDataPoint Then
'Do this
ElseIf message.InputType = msdCadInputTypeReset Then
'Do this
End If

Here’s what that looks like with the rest of the code plugged in:

Now when you press Play, you can click at any point, and MicroStation will generate a bunch of circles around it.

More tutorials coming soon.

One Reply to “VBA: How to Start Scripting in MicroStation”

  1. my favorite subject. i am in hiatus right now, but have been working on driving cad with vba a lot in the past. all kinds of geometrical applications are possible, graphing equations etc. it seems like a high school course book could be written on the subject combining geometry, code and cad. its too bad the cad and code companies have let the name vba come to be “deprecated” because there is so much than can be done so easily.

    Liked by 1 person

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: