Nov 29, 2012 ... EnvisionCAD is a provider of training solutions for MicroStation® and select
InRoads® products. ... All portions of this manual including printed ...
EnvisionCAD – Education
MicroStation® VBA Fundamentals MicroStation® V8i (SELECTseries)
Exercise Workbook
Prepared by: EnvisionCAD Copyright © 2012
EnvisionCAD– About Us EnvisionCAD is a provider of training solutions for MicroStation® and select InRoads® products. We also use our expertise to assist organizations managing data and workflow tasks via consulting and/or outsourced CAD management roles. Additionally, we provide visualization services for design concepts and alternatives. Services include but are not limited to: Illustrations, solar studies, line-of-sight studies, and animations. We offer training at our headquarters in Madison, WI, at satellite locations around the country, on-site at your location or by virtual classrooms. For additional information about our services, prices, or training schedules, visit our website at EnvisionCAD.com or call 1-608-836-3903.
EnvisionCAD– Guaranteed Training Our guarantee is simple.
Any student may retake any class, at any time, and for any reason without additional charge.
Bentley Systems, Inc. Bentley is a technology provider for software solutions used to design, engineer, build, and operate large constructed assets such as roadways, bridges, buildings, industrial facilities, power plants, and utility networks. Bentley's reach spans the globe, serving more than 300,000 professionals and more than 20,000 companies and organizations. To contact Bentley visit www.Bentley.com or call 1-800-BENTLEY.
Page ii
Do Not Duplicate ©2012
EnvisionCAD www.EnvisionCAD.com
Version 1.0 Dated 11/29/2012
Copyright Warning: Copyright 2012 The Envision Group, Inc. dba EnvisionCAD Madison, Wisconsin. ALL RIGHTS RESERVED WORLDWIDE. All portions of this manual including printed material, electronic data, notes, and supplemental handouts, unless otherwise specified, are the exclusive property of EnvisionCAD and are protected under United States and international copyright laws. No part of this document may be reproduced, adapted, translated, stored in a retrieval system or transmitted in any form or by any means, electronic, mechanical, photocopying, recording, or otherwise, without the prior written permission of the publisher. We vigorously protect our copyright interests. By possessing this material, you agree that any infringement that you commit shall be considered a willful infringement and agree that the fair and reasonable fee for infringing use of any portion thereof shall be no less than US $5,000 for each infringement. Please note that all copies of this material, whether provided or sold, are documented and logged. Please respect our copyright protections and encourage others to do the same. These materials were generated with a considerable investment of time and effort. Illegally copying this material deprives us of the compensation necessary for us to continue to provide you with a high level of service and the means to produce additional material for your benefit. To discuss reproduction rights or to report a copyright violation or concern, E-mail
[email protected], or call 1-608-836-3903.
Limitation of Liability EnvisionCAD is not liable for any damages suffered resulting from use of this material. EnvisionCAD is not liable for any indirect, incidental, punitive, special or consequential damage (including loss of business, revenue, profits, use, data or other economic advantage) however it arises through use of, or use of information contained in, these materials. You agree to hold the publisher of this material harmless from, and you agree not to sue the publisher for any claims arising from the use of this material or instruction from this material.
Trademarks MicroStation, InRoads, InRoads Site, InRoads Survey, InRoads Bridge, InRoads Storm & Sanitary, InRoads Rail, SelectCAD, GEOPAK and MX Roads are registered trademarks of Bentley Systems, Inc. AutoCAD is a registered trademark of Autodesk, Inc. Windows is a registered trademark of Microsoft Corporation. Other brands and product names are trademarks of their respective owners. Version 1.0 Dated 11/29/2012
EnvisionCAD www.EnvisionCAD.com
Page iii Do Not Duplicate ©2012
Introduction Welcome to MicroStation VBA Fundamentals for MicroStation V8i (SELECTseries). This course is for non-programmers to learn how to create their own MicroStation VBA macros to increase productivity for repetitive tasks and add their own commands to the MicroStation environment. The class will cover use of the VBA editor including designing VBA user forms, modules, and classes. EnvisionCAD and Bentley assume no responsibility for errors that may appear in this software or occur as a result of its use.
Document Conventions Several conventions are used throughout this document to indicate actions to be taken or to highlight important information. The conventions are as follows:
Page iv
Item
Meaning
Place Text
a command name or a file to select
Tools > Options
a command path to select – usually from a pulldown menus
Key-In
a prompt to enter data with the keyboard
Document name
italics in this style are used when referring to another document
Note:
Text information about a command or process to pay particular attention to
Emphasis
an important word or phrase
1. Numbered Steps
an action to be performed as part of a lab activities
or Data
press the data button on the mouse
or Reset
press the reset button on the mouse
or Tentative
press the tentative button on the mouse
CadInputQueue
programming code
Do Not Duplicate ©2012
EnvisionCAD www.EnvisionCAD.com
Version 1.0 Dated 11/29/2012
Table of Contents TABLE OF CONTENTS EnvisionCAD– About Us ............................................................................. ii EnvisionCAD– Guaranteed Training ......................................................... ii Bentley Systems, Inc. .................................................................................... ii Copyright...................................................................................................... iii Limitation of Liability ................................................................................. iii Trademarks .................................................................................................. iii Introduction ................................................................................................. iv Document Conventions ............................................................................... iv
1. VBA Basics ......................................................................................1 Workspace Configuration Variables ...........................................................1 General Rules/Tips for MVBA .....................................................................1 VBA Project Manager Dialog.......................................................................2 Creating a New MVBA..................................................................................................... 2 Record a MVBA ................................................................................................................ 2 Edit the MVBA.................................................................................................................. 3 Run a MVBA ..................................................................................................................... 4 Autoload a MVBA ............................................................................................................ 4
Run a MVBA from a Key-in.........................................................................4 The VBA Editor .............................................................................................5 VBA Project Explorer ...................................................................................................... 5 Properties Window ........................................................................................................... 5 Edit Toolbar ...................................................................................................................... 6 Special Characters ............................................................................................................ 9 Using VBA Help .............................................................................................................. 10 Protecting VBA’s ............................................................................................................ 13
Message Boxes ..............................................................................................14 Lab 1 – VBA Basics .....................................................................................15 Objectives ........................................................................................................................ 15 Open the Design File ....................................................................................................... 15 Record a Macro ............................................................................................................... 15 Review the VBA Editor .................................................................................................. 17 Display a Message Box.................................................................................................... 20
2. Variables, Arrays and Collections ............................................. 23 Variables .......................................................................................................23 Declaration ...................................................................................................................... 23 Variable Naming ............................................................................................................. 23 Common VBA Data Types ............................................................................................. 24 Common MicroStation Data Types ............................................................................... 26 Constants ......................................................................................................................... 27 Scope ................................................................................................................................ 28 Enumerations .................................................................................................................. 28 Converting Between Data Types ................................................................................... 29
Version 1.0 Dated 11/29/2012
EnvisionCAD EnvisionCAD.com
Page v
Do Not Duplicate ©2012
Table of Contents User-Defined Types .....................................................................................29 Arrays ...........................................................................................................30 Declaration.......................................................................................................................30 Resizing ............................................................................................................................30 Checking Array Boundaries...........................................................................................30 Multi-Dimensional Arrays..............................................................................................31
Collections ....................................................................................................31 Using the With Statement ...........................................................................32 Declare all your variables ...........................................................................33 Lab 2 – Variables, Arrays and Collections ...............................................34 Objectives.........................................................................................................................34 Open the Design File .......................................................................................................34 Configure VBA to Require Variable Declaration ........................................................34 Work with different variable types................................................................................37
3. Procedures .................................................................................... 43 Subroutines ..................................................................................................43 Functions ......................................................................................................43 Optional Arguments ....................................................................................43 By Reference or By Value ...........................................................................44 Calling Subroutines and Functions ............................................................44 Lab 3- Working with Procedures ...............................................................45 Objectives.........................................................................................................................45 Open the VBA Project ....................................................................................................45 Multiple macros in a project file ....................................................................................46 Work with Public and Private Procedures ...................................................................46
4. Decision Structures and Loops ................................................... 49 Conditional Expressions .............................................................................49 Arithmetic Operators .....................................................................................................49 Comparison Operators ...................................................................................................49 Logical Operators ...........................................................................................................49
Decision Structures ......................................................................................50 If…Then Statements .......................................................................................................50 IIf Statement ....................................................................................................................51 Select Case Statements....................................................................................................52
Loops .............................................................................................................53 For...Next .........................................................................................................................53 Do…Loop .........................................................................................................................54
Lab 4 – Decision Structures and Loops .....................................................55 Objectives.........................................................................................................................55 Open the VBA File ..........................................................................................................55 Iterate through an array using For…Next loop ...........................................................56 Multiple return values from a single function call .......................................................57 Review function library ..................................................................................................59
Page vi
Do Not Duplicate ©2012
EnvisionCAD www.EnvisionCAD.com
Version 1.0 Dated 11/29/2012
Table of Contents 5. User Forms ................................................................................... 61 Creating UserForms ....................................................................................61 Form Properties ...........................................................................................62 UserForm Toolbox.......................................................................................63 Select Objects .................................................................................................................. 64 Label................................................................................................................................. 64 TextBox ............................................................................................................................ 64 ComboBox ....................................................................................................................... 65 ListBox ............................................................................................................................. 65 CheckBox ......................................................................................................................... 65 OptionButton................................................................................................................... 66 Frame ............................................................................................................................... 66 CommandButton ............................................................................................................. 66
Programming Control Events ....................................................................67 Click Event ...................................................................................................................... 67 Other Control Events ..................................................................................................... 68 Verifying UserForm Text Inputs ................................................................................... 68 Interface Related Code Only .......................................................................................... 69
UserForm Management ..............................................................................70 Show ................................................................................................................................. 70 Hide .................................................................................................................................. 70 Unload .............................................................................................................................. 71 UserForms Collection ..................................................................................................... 71 Modal vs. NonModal....................................................................................................... 72
More on Message Boxes ..............................................................................73 InputBox .......................................................................................................74 Lab 5 – User Forms .....................................................................................75 Objectives ........................................................................................................................ 75 Open the VBA File .......................................................................................................... 75 Design a UserForm ......................................................................................................... 76 Program Control Events ................................................................................................ 84
6. String Manipulations .................................................................. 88 String Comparisons .....................................................................................88 Len .................................................................................................................................... 88 StrComp........................................................................................................................... 88
Conversions ..................................................................................................89 LCase$ ............................................................................................................................. 89 UCase$ ............................................................................................................................. 89 StrConv ............................................................................................................................ 89 StrReverse........................................................................................................................ 89
Search and Replace .....................................................................................90 InStr ................................................................................................................................. 90 InStrRev ........................................................................................................................... 90 Replace ............................................................................................................................. 90
Version 1.0 Dated 11/29/2012
EnvisionCAD EnvisionCAD.com
Page vii
Do Not Duplicate ©2012
Table of Contents Split and Join ...............................................................................................91 Split ..................................................................................................................................91 Join ...................................................................................................................................91 & .......................................................................................................................................91
Trimming......................................................................................................92 Trim$................................................................................................................................92 LTrim$ .............................................................................................................................92 RTrim$ .............................................................................................................................92
Extracting Parts of a String ........................................................................93 Left$ .................................................................................................................................93 Right$ ...............................................................................................................................93 Mid$ .................................................................................................................................93
Formatting....................................................................................................93 Format$ ............................................................................................................................93
Lab 6 – String Manipulations .....................................................................94 Objectives.........................................................................................................................94 Open the VBA File ..........................................................................................................94 Update the UserForm .....................................................................................................95 Perform additional string parsing .................................................................................95
7. Interacting With MicroStation................................................... 97 CadInputQueue ...........................................................................................97 GetInput ...........................................................................................................................97 SendCommand ................................................................................................................98 SendDataPoint .................................................................................................................98 SendKeyin ........................................................................................................................98 SendLastInput .................................................................................................................99 SendReset .........................................................................................................................99 SendTentativePoint .........................................................................................................99
CommandState ............................................................................................99 LastDataPoint ..................................................................................................................99 LastView ..........................................................................................................................99 StartDefaultCommand ...................................................................................................99
Messages .....................................................................................................100 ShowCommand .............................................................................................................100 ShowError .....................................................................................................................100 ShowPrompt ..................................................................................................................100 ShowStatus.....................................................................................................................100 ShowMessage .................................................................................................................100
Tool Settings Keyins ..................................................................................101 Passing Arguments to a Macro ................................................................102 Lab 7A – Changing Elements to Bylevel Symbology .............................103 Objectives.......................................................................................................................103 Open the VBA File ........................................................................................................103 Update the Macro..........................................................................................................104
Lab 7B – Create a Print Macro ................................................................105 Page viii
Do Not Duplicate ©2012
EnvisionCAD www.EnvisionCAD.com
Version 1.0 Dated 11/29/2012
Table of Contents Objectives ...................................................................................................................... 105 Open the VBA File ........................................................................................................ 105 Review the Macros ........................................................................................................ 106 Create a UserForm ....................................................................................................... 106 Program the CommandButtons ................................................................................... 108
8. MicroStation Properties ........................................................... 109 DesignFile ...................................................................................................109 ActiveDesignFile............................................................................................................ 109
ModelReference .........................................................................................112 ActiveModelReference .................................................................................................. 112
Graphics Settings .......................................................................................115 ActiveSettings ................................................................................................................ 115
Workspace ..................................................................................................118 ActiveWorkspace .......................................................................................................... 118
Windows Settings.......................................................................................119 Lab 8A – MicroStation Settings ...............................................................120 Objectives ...................................................................................................................... 120 Open the VBA File ........................................................................................................ 120 Review the Macro ......................................................................................................... 121 Modify the Macro ......................................................................................................... 121
Lab 8B – Updated Print Macro ................................................................124 Objectives ...................................................................................................................... 124 Open the VBA File ........................................................................................................ 124 Review the Code ............................................................................................................ 125 Update the Macro ......................................................................................................... 127
9. Element Processing.................................................................... 128 Element Enumerator .................................................................................128 Build From Element Scan Criteria.............................................................................. 128 Build From Selection Set .............................................................................................. 129 Build From Fence.......................................................................................................... 129 Build From Complex Element ..................................................................................... 129 Process Element Enumerator ...................................................................................... 129
Recursion ....................................................................................................131 General Element Properties and Methods ..............................................132 IsGraphical .................................................................................................................... 132 Type ................................................................................................................................ 132 Level ............................................................................................................................... 132 Color............................................................................................................................... 132 Line Style ....................................................................................................................... 132 Line Weight ................................................................................................................... 132 DisplayPriority .............................................................................................................. 133 Transparency ................................................................................................................ 133 GraphicGroup ............................................................................................................... 133 Range ............................................................................................................................. 133
Version 1.0 Dated 11/29/2012
EnvisionCAD EnvisionCAD.com
Page ix
Do Not Duplicate ©2012
Table of Contents ConstructVertexList .....................................................................................................133 Mirror ............................................................................................................................133 Mirror3D .......................................................................................................................133 Move ...............................................................................................................................134 Rotate .............................................................................................................................134 RotateAboutZ ................................................................................................................134 ScaleAll ..........................................................................................................................134 ScaleUniform .................................................................................................................134 Rewrite ...........................................................................................................................134 Redraw ...........................................................................................................................134
Lab 9 – Element Processing ......................................................................135 Objectives.......................................................................................................................135 Open the Design File .....................................................................................................135 Run a Macro to Select and Modify Elements .............................................................135 Modify a Macro to Select and Modify a different set of Elements............................136
10.Creating New Elements............................................................. 141 Line Elements.............................................................................................141 Arc Elements ..............................................................................................141 Ellipse Elements .........................................................................................142 Shape Elements ..........................................................................................142 Text Elements.............................................................................................142 TextNode Elements ....................................................................................142 Cell Elements .............................................................................................143 ComplexString Elements ..........................................................................143 ComplexShape Elements...........................................................................143 Lab 10 – Creating Elements .....................................................................144 Objectives.......................................................................................................................144 Open the VBA File ........................................................................................................144 Review the Macro..........................................................................................................145 Modify the Macro..........................................................................................................145
11.Autorun a VBA .......................................................................... 147 Module Containing OnProjectLoad ............................................................................147 Class Module Containing Event Handler ...................................................................148
Lab 11 – Autorun a VBA ..........................................................................149 Objectives.......................................................................................................................149 Open the Design File .....................................................................................................149 AutoLoad a Macro to run when MicroStation starts.................................................149 Configure a macro to update all dgn libraries every time a design file is opened ..152
12.Error Handling and Debugging ............................................... 155 Error Handling ..........................................................................................155 The Error Object ..........................................................................................................155 Basic Error Handling....................................................................................................156 Advanced Error Handling ............................................................................................157
Page x
Do Not Duplicate ©2012
EnvisionCAD www.EnvisionCAD.com
Version 1.0 Dated 11/29/2012
Table of Contents Other uses for On Error GoTo .................................................................................... 159
Debugging ...................................................................................................160 Debug Toolbar............................................................................................................... 160 Debug Function ............................................................................................................. 164
Lab 12 – Error Handling and Debugging ...............................................165 Objectives ...................................................................................................................... 165 Open the VBA File ........................................................................................................ 165 Trap Errors ................................................................................................................... 166 Use the Debug tools ....................................................................................................... 167
Version 1.0 Dated 11/29/2012
EnvisionCAD EnvisionCAD.com
Page xi
Do Not Duplicate ©2012
Eac hmanual i nc l udesadat as ett hatc anbe downl oadedf r om ourwebs i t e. www. Env i s i onCAD. c om/ c our s ewar e
Cont ac tusi fy ouhav eanyques t i ons . i nf o@Env i s i onCAD. c om