Customizing MicroStation using VBA
Transcription
Customizing MicroStation using VBA
Welcome to: Customizing MicroStation via VBA Bentley Bash 2011 Created by Mike Lazear Archway Systems, Inc. Survey 1. 2. 3. 4. Have you run a macro in MicroStation? Recorded or created a macro in MicroStation? Any programming experience? If so, what languages? 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 What is an MVBA file and what’s in it • MVBA stands for MicroStation VBA - VBA stands for Visual Basic for Applications MVBA is a single file, called a project, that contains: Modules, classes and forms Module1 Class1 Module2 Class2 Module3 Form1 Form2 Overview/mechanics of VBA Overview/mechanics of VBA Macros shows only Subs from Modules that have no params Project Manager shows only Projects Visual Basic Editor (IDE) Shows Projects and contents: Modules, Classes, Forms VBA Project Manager Open Editor Save Project Unload Project Open Existing Project Create new project Run Macro Start Recording Stop Recording Pause Recording Automatically load the project VBA Editor (also know as IDE – Integrated Development Environment) Projects Window Main Editor Window Immediate Window Properties Window Watches Window Macros List of all Public Subs (that have no params), in all Modules, from the Project(s) selected below 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 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) 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) 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 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 Visual Basic Objects • Properties Things that describe the object such as color, filled and border. • Methods Things that we can do with the object (i.e. functions we can call) such as delete, copy, change size. • Events Notification that something happened to our object. The object was clicked on. The object was moved. The object was deleted. VB Objects Example Properties (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) MicroStation objects • • • • • Application (basically the root of all objects) ActiveDesignFile ActiveModelReference ActiveSettings Element – LineElement, EllipseElement, etc. – Color, LineWeight, IsGraphical properties 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. 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 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 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) 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 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 Other Useful stuff • Modules, Classes, Forms • Interfaces & Implements 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 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 Winters Bentley Institute Press ISBN: 0-9714141-8-1