Sei sulla pagina 1di 2

Basic MicroStation Visual Basic for Applications

Lesson 1: Recording and Playing Back a Macro

You want to learn more about VBA? Visual Basic for Applications is
considered as one of the more accessible programming languages. Yet
to read documentation or beginner’s pages on how to start using VBA
doesn’t inspire confidence: technical terminology, programming jargon
and confusing examples puts most people off before they’ve started. VBA
offers great potential to those who can master the black art of
programming and expands the possibilities available within any VBA-
enabled software where the tools provided don’t quite go far enough. This
occasional series attempts to remove the stigma attached to beginning to
understand VBA for those with little or no programming experience. Even
though these articles offer insight and explanations of the common VBA
tools, I’d strongly recommend undertaking a short one-on-one training
course to compliment them, allowing you to underline the principles
outlined here and reinforce your understanding.

Starting off, you may not ever need to understand a thing about coding in
order to be able to produce a usable macro. For example, imagine you
want to draw a line in every drawing in the exactly the same location (you
might, OK?). All you need to do is to record a macro and then play it back
in each file.

So how do you go about recording a macro? Firstly you need to open the
Project Manager (from Utilities > Macro > Project Manager). If you don’t
have a project, create a new, blank one:

I’ve called mine “Training”, you can call yours what you like. It’s a good
idea to save it to the Workspace\Standards\VBA folder. You can change
the project “Name” independent of the filename by clicking in the Name
field.

What’s a project I hear you ask? It’s just a collection of macros. You can
have as many or as few macros (or modules) as you like in a project, kind
of like cells in a cell library. For a diagrammatic view of how projects,
modules and routines go together, check out the Anatomy of a VBA
Macro 101.

So you have a project, now to create the macro all you need to do is start
recording (the circle icon). Do what you need to do then hit Stop (the
square). Your macro is recorded.

A new macro will have been created for you called “macro1”. If you open
the VBA Editor you can see the code that has been generated from your
session. Mine - placing a SmartLine - is included below. You can copy
and paste it into your project if you want, but bear in mind you’ll need to
rename the macro if you already have a “macro1”.
Sub Macro1()
Dim startPoint As Point3d
Dim point As Point3d, point2 As Point3d

'Start a command
CadInputQueue.SendCommand "PLACE SMARTLINE "

'Coordinates are in master units


startPoint.X = 7.316122
startPoint.Y = -4.865692
startPoint.Z = 0#

'Send a data point to the current command


point.X = startPoint.X
point.Y = startPoint.Y
point.Z = startPoint.Z
CadInputQueue.SendDataPoint point, 1

'Send a data point to the current command


point.X = startPoint.X + 2.48087
point.Y = startPoint.Y + 3.0765
point.Z = startPoint.Z
CadInputQueue.SendDataPoint point, 1

'Send a data point to the current command


point.X = startPoint.X + 7.6503
point.Y = startPoint.Y + 1.9341
point.Z = startPoint.Z
CadInputQueue.SendDataPoint point, 1

'Send a reset to the current command


CadInputQueue.SendReset

CommandState.StartDefaultCommand
End Sub

You don’t need to worry about what all this means for now, we’ll get into
that later.

How do you play this back? Click on the Play button (do I need to
mention it’s the triangle?) and you’ll see a list of available macros appear.
You should see at least “Module1.Macro1”. If you have copied mine you’ll
see two, “Module1.Macro1” (yours) and “Module1.Macro2” (mine,
presuming you renamed it to Macro2).

Highlight the one you want and click run. Alternatively you can use the
keyin:

vba run module1.macro1

And that’s it. You have your first macro. Use the keyin along with Batch
Processor and you can run the macro on as many files as you want
automatically.

Note: The next time you start MicroStation you’ll need to load the project
first before you can run the macros. You can do this by ticking the Auto-
Load column in the Project Manager, or better still, set the configuration
variable MS_VBAAUTOLOADPROJECTS > “your project”. Use the >
sign to make sure you don’t unload any macros that are already set to
Auto-Load.

Next time, jargon.

Potrebbero piacerti anche