Welcome to - Optimize Building · PDF fileWelcome to: Customizing MicroStation via VBA...

25
Welcome to: Customizing MicroStation via VBA Bentley Bash 2011 Created by Mike Lazear Archway Systems, Inc.

Transcript of Welcome to - Optimize Building · PDF fileWelcome to: Customizing MicroStation via VBA...

Page 1: Welcome to - Optimize Building  · PDF fileWelcome to: Customizing MicroStation via VBA Bentley Bash 2011 Created by Mike Lazear Archway Systems, Inc

Welcome to:Customizing MicroStation via VBA

Bentley Bash 2011

Created by Mike Lazear

Archway Systems, Inc.

Page 2: Welcome to - Optimize Building  · PDF fileWelcome to: Customizing MicroStation via VBA Bentley Bash 2011 Created by Mike Lazear Archway Systems, Inc

Survey

1. Have you run a macro in MicroStation?

2. Recorded or created a macro in MicroStation?

3. Any programming experience?

4. If so, what languages?

Page 3: Welcome to - Optimize Building  · PDF fileWelcome to: Customizing MicroStation via VBA Bentley Bash 2011 Created by Mike Lazear Archway Systems, Inc

What we will cover today

• What is an MVBA file and what’s in it

• Overview/mechanics of using VBA

• Record a macro, run a macro

• Getting Assistance

• Interacting with MicroStation

• Scanning a Model

• Manipulating Elements

Page 4: Welcome to - Optimize Building  · PDF fileWelcome to: Customizing MicroStation via VBA Bentley Bash 2011 Created by Mike Lazear Archway Systems, Inc

What is an MVBA file and what’s in it

• MVBA stands for MicroStation VBA - VBA stands for Visual Basic for Applications

Module1

Module2

Module3

Class1

Class2

Form1

Form2

MVBA is a single file, called a project, that contains:

Modules, classes and forms

Page 5: Welcome to - Optimize Building  · PDF fileWelcome to: Customizing MicroStation via VBA Bentley Bash 2011 Created by Mike Lazear Archway Systems, Inc

Overview/mechanics of VBA

Page 6: Welcome to - Optimize Building  · PDF fileWelcome to: Customizing MicroStation via VBA Bentley Bash 2011 Created by Mike Lazear Archway Systems, Inc

Overview/mechanics of VBA

Project Manager shows only Projects

Visual Basic

Editor (IDE)

Shows Projects

and contents:

Modules,

Classes, Forms

Macros

shows only

Subs from

Modules

that have

no params

Page 7: Welcome to - Optimize Building  · PDF fileWelcome to: Customizing MicroStation via VBA Bentley Bash 2011 Created by Mike Lazear Archway Systems, Inc

VBA Project Manager

Automatically load the projectCreate new project

Open Existing Project

Unload Project

Save Project

Open Editor

Run Macro

Start Recording

Stop Recording

Pause Recording

Page 8: Welcome to - Optimize Building  · PDF fileWelcome to: Customizing MicroStation via VBA Bentley Bash 2011 Created by Mike Lazear Archway Systems, Inc

VBA Editor(also know as IDE – Integrated Development Environment)

Projects Window Main Editor Window

Properties Window

Immediate Window

Watches Window

Page 9: Welcome to - Optimize Building  · PDF fileWelcome to: Customizing MicroStation via VBA Bentley Bash 2011 Created by Mike Lazear Archway Systems, Inc

Macros

Runs highlighted Macro (same as Play)

Cancels out of this dialog (same as Red X)

Starts debugging for the highlighted macro

Edits the highlighted macro

Deletes the highlighted macro from module

List of all Public Subs (that have no params), in all Modules, from the Project(s) selected below

Page 10: Welcome to - Optimize Building  · PDF fileWelcome to: Customizing MicroStation via VBA Bentley Bash 2011 Created by Mike Lazear Archway Systems, Inc

Modules

• Modules contain:

– Declarations (variables)• Const pi = 3.14159265358979

• Private count As Integer

• Public oElement As Element

– Subroutines• Sub subWithNoParams()

• Sub subWithParams(count As Integer, total As Integer)

– Functions (exactly like subroutine but return a value)

• Function calcHigherValue(val1 As Double, val2 As Double) As Double

• See WhatIsInAModule module (bash2011 project)

Page 11: Welcome to - Optimize Building  · PDF fileWelcome to: Customizing MicroStation via VBA Bentley Bash 2011 Created by Mike Lazear Archway Systems, Inc

Recording a Macro

• Open the Project Manager

• Highlight a project where the macro should be saved

• Click the red Record circle icon to start the record

• Perform whatever operations you wish to record

• Click the white Stop Record square (next to record icon)

• See Starter module (bash2011 project)

Page 12: Welcome to - Optimize Building  · PDF fileWelcome to: Customizing MicroStation via VBA Bentley Bash 2011 Created by Mike Lazear Archway Systems, Inc

Run a Macro

• From Macros: (Alt-F8 to bring it up if necessary)

– Find the macro to run in the “Macro name” list

– Either double-click the name or highlight and click Run

• From VB Editor: (Alt-F11 to bring it up if necessary)

– Navigate to the project/module where your macro lives

– Click somewhere inside the macro.

– Click play button on toolbar (may have to open Debug toolbar)• NOTE: if you click outside of the macro and then click play the Macros window will come up

• From Project Manager:

– Click the play button (arrow) and it will bring up the Macro window

– Follow steps (From Macros) above

Page 13: Welcome to - Optimize Building  · PDF fileWelcome to: Customizing MicroStation via VBA Bentley Bash 2011 Created by Mike Lazear Archway Systems, Inc

Getting Assistance

• F1 for help – Be aware there are 3 different help files that can be launched.– MicroStation help, VBA help, MicroStation VBA help

• Examples are a gold mine!

• F2 in VB Editor for Object Browser

• MicroStation on the web– www.bentley.com

– Click on community (near top)

– Click on discussion groups (on left side)

– Click on web browser (within page)

– Click on bentley.microstation.v8.vba

– Search for content you are interested in

Page 14: Welcome to - Optimize Building  · PDF fileWelcome to: Customizing MicroStation via VBA Bentley Bash 2011 Created by Mike Lazear Archway Systems, Inc

Visual Basic Objects

• PropertiesThings that describe the object such as color, filled and border.

• MethodsThings that we can do with the object (i.e. functions we can call) such as delete, copy, change size.

• EventsNotification that something happened to our object. The object was clicked on. The object was moved. The object was deleted.

Page 15: Welcome to - Optimize Building  · PDF fileWelcome to: Customizing MicroStation via VBA Bentley Bash 2011 Created by Mike Lazear Archway Systems, Inc

VB Objects ExampleProperties (things that describe)

Color is silver

Receiver is red

Touch tone

Stationary

Text on screen

Methods (things we can do)

Answer incoming call

Make an outgoing call

Hang up a call

Events (things we react to)

Someone calls

A call waiting comes in

operator asks for money

Someone calls us (Event). We

answer the phone (Method). Text

on the screen shows their phone #

(property)

Page 16: Welcome to - Optimize Building  · PDF fileWelcome to: Customizing MicroStation via VBA Bentley Bash 2011 Created by Mike Lazear Archway Systems, Inc

MicroStation objects

• Application (basically the root of all objects)

• ActiveDesignFile

• ActiveModelReference

• ActiveSettings

• Element

– LineElement, EllipseElement, etc.

– Color, LineWeight, IsGraphical properties

Page 17: Welcome to - Optimize Building  · PDF fileWelcome to: Customizing MicroStation via VBA Bentley Bash 2011 Created by Mike Lazear Archway Systems, Inc

Interacting with MicroStation• See MessagesAndInput module for examples of how to communicate to the user.

– ShowStatus

– ShowCommand

– ShowPrompt

– MsgBox

– InputBox

• Recording and editing a macro to be more interactive

– CadInputQueue

• SendKeyin

• SendDataPoint

• GetInput

• SendReset

• Debugging

– Breakpoints, watch window, immediate window, etc.

Page 18: Welcome to - Optimize Building  · PDF fileWelcome to: Customizing MicroStation via VBA Bentley Bash 2011 Created by Mike Lazear Archway Systems, Inc

Record an action

1 Switch to Model “Interaction”

2 Unload all VBA projects except the Default project in the VBA Project Manager.

3 In Project Manager create a new project named MoveElement.

4 Select the MoveElement project, then click Start Record.

5 Returning to MicroStation, select the Move Element tool from the Manipulate tool box

6 Identify the element labeled “VALVE 1”.

7 Place the element a distance from its original location. Be sure to move it in both the X and the Y direction.

(For this exercise the actual distance moved is not as important as the fact that you moved the object.)

8 Reset to release the element.

9 Click Stop Record.

10 Switch to the Visual Basic Editor to examine the new module.

Double‐click MoveElement > Modules > Module1

Page 19: Welcome to - Optimize Building  · PDF fileWelcome to: Customizing MicroStation via VBA Bentley Bash 2011 Created by Mike Lazear Archway Systems, Inc

Editing to be more interactive(change code to the following)

1 Sub Move50cm()

2 Dim startPoint As Point3d

3 Dim endPoint As Point3d

4 Dim message As CadInputMessage

5

6 ShowStatus "Move command using VBA"

7 ShowCommand "Move Element with macro"

8 ShowPrompt "Identify Element"

9

10 Set message = CadInputQueue.GetInput(msdCadInputTypeDataPoint, msdCadInputTypeReset)

11

12 If message.InputType = msdCadInputTypeDataPoint Then

13 ' Start a command

14 CadInputQueue.SendCommand "MOVE ICON"

15

16 startPoint = message.point

17 endPoint = startPoint

18 endPoint.Y = endPoint.Y - 0.5

19

20 CadInputQueue.SendDataPoint startPoint, 1

21 CadInputQueue.SendDataPoint endPoint, 1

22 ElseIf message.InputType = msdCadInputTypeReset Then

23 CommandState.StartDefaultCommand

24 End If

25

26 CommandState.StartDefaultCommand

27 End Sub

Page 20: Welcome to - Optimize Building  · PDF fileWelcome to: Customizing MicroStation via VBA Bentley Bash 2011 Created by Mike Lazear Archway Systems, Inc

Scanning a Model(Ways to scan)

• Scan all elements in a model

• Scan selected elements

• Scan elements in a fence

• Scan specific type of elements

• See Scanning module (bash2011 project)

Page 21: Welcome to - Optimize Building  · PDF fileWelcome to: Customizing MicroStation via VBA Bentley Bash 2011 Created by Mike Lazear Archway Systems, Inc

Manipulating Elements

• Change Element properties

– Color, LineWeight, LineStyle, etc.

• Element.ReWrite

– Save changes made to element

• Creating new elements

– CreateEllipseElement2(…)

– ActiveModelReference.AddElement myEllipse

• See ManipulatingElements module

Page 22: Welcome to - Optimize Building  · PDF fileWelcome to: Customizing MicroStation via VBA Bentley Bash 2011 Created by Mike Lazear Archway Systems, Inc

Review & Wrap up

• Key points you learned:– Mechanics of how to work with VBA

– How to record and run a macro

– Basic building blocks of interacting and manipulating elements

• Questions & answers

• Thank you for attending the Customizing MicroStation via VBA session

Page 23: Welcome to - Optimize Building  · PDF fileWelcome to: Customizing MicroStation via VBA Bentley Bash 2011 Created by Mike Lazear Archway Systems, Inc

Other Useful stuff

• Modules, Classes, Forms

• Interfaces & Implements

Page 24: Welcome to - Optimize Building  · PDF fileWelcome to: Customizing MicroStation via VBA Bentley Bash 2011 Created by Mike Lazear Archway Systems, Inc

Modules, Classes, Forms

• Module– Contain subroutines & functions only

– Routines can be Private or Public

• Classes– Contain Properties, Methods and Events

– The MicroStation Objects are instances of predefined classes

– Must create an instance (object) of a user class to call its code

• Forms– Visual dialogs

– Similar to Classes they can contain Properties & Methods

– controls placed on form have predefined events

Page 25: Welcome to - Optimize Building  · PDF fileWelcome to: Customizing MicroStation via VBA Bentley Bash 2011 Created by Mike Lazear Archway Systems, Inc

Programming Resources• If you have never programmed

To understand computer programming concepts we recommend “How Computer Programming Works” which can be found at www.desaware.com

• If you have done some programming but don’t know Visual Basic

Personally, I’ve never found a good general book for learning Visual Basic but there is an excellent and inexpensive web site that has videos demonstrating features and functionality. www.LearnVisualStudio.NET

• The on-line documentation for Visual Basic is excellent

• Learning MicroStation VBA by Jerry WintersBentley Institute Press ISBN: 0-9714141-8-1