MicroStation VBA Fundamentals - MicroStation V8i - EnvisionCAD

of 14 /14

Embed Size (px)

Transcript of MicroStation VBA Fundamentals - MicroStation V8i - EnvisionCAD

MicroStation VBA Fundamentals - MicroStation V8i (SELECTseries) Exercise Workbook
Copyright © 2012
Page ii EnvisionCAD Version 1.0 Do Not Duplicate ©2012 www.EnvisionCAD.com Dated 11/29/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.
Version 1.0 EnvisionCAD Page iii Dated 11/29/2012 www.EnvisionCAD.com Do Not Duplicate ©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.
Page iv EnvisionCAD Version 1.0 Do Not Duplicate ©2012 www.EnvisionCAD.com Dated 11/29/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:
Item
a command name or a file to select
Tools > Options a command path to select – usually from a pull- down 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
<D> or Data press the data button on the mouse
<R> or Reset press the reset button on the mouse
<T> or Tentative CadInputQueue
programming code
Table of Contents
Version 1.0 EnvisionCAD Page v Dated 11/29/2012 EnvisionCAD.com Do Not Duplicate ©2012
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
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
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
Table of Contents
Page vi EnvisionCAD Version 1.0 Do Not Duplicate ©2012 www.EnvisionCAD.com Dated 11/29/2012
User-Defined Types .....................................................................................29
3. Procedures .................................................................................... 43
Calling Subroutines and Functions ............................................................44
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
Table of Contents
Version 1.0 EnvisionCAD Page vii Dated 11/29/2012 EnvisionCAD.com Do Not Duplicate ©2012
5. User Forms ................................................................................... 61
UserForm Management ..............................................................................70 Show ................................................................................................................................. 70 Hide .................................................................................................................................. 70 Unload .............................................................................................................................. 71 UserForms Collection ..................................................................................................... 71 Modal vs. NonModal ....................................................................................................... 72
6. String Manipulations .................................................................. 88
Page viii EnvisionCAD Version 1.0 Do Not Duplicate ©2012 www.EnvisionCAD.com Dated 11/29/2012
Split and Join ...............................................................................................91 Split .................................................................................................................................. 91 Join ................................................................................................................................... 91 & ....................................................................................................................................... 91
CadInputQueue ...........................................................................................97 GetInput ........................................................................................................................... 97 SendCommand ................................................................................................................ 98 SendDataPoint ................................................................................................................. 98 SendKeyin ........................................................................................................................ 98 SendLastInput ................................................................................................................. 99 SendReset ......................................................................................................................... 99 SendTentativePoint ......................................................................................................... 99
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
Table of Contents
Version 1.0 EnvisionCAD Page ix Dated 11/29/2012 EnvisionCAD.com Do Not Duplicate ©2012
Objectives ...................................................................................................................... 105 Open the VBA File ........................................................................................................ 105 Review the Macros ........................................................................................................ 106 Create a UserForm ....................................................................................................... 106 Program the CommandButtons ................................................................................... 108
8. MicroStation Properties ........................................................... 109
Recursion ....................................................................................................131
Table of Contents
Page x EnvisionCAD Version 1.0 Do Not Duplicate ©2012 www.EnvisionCAD.com Dated 11/29/2012
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
Version 1.0 EnvisionCAD Page xi Dated 11/29/2012 EnvisionCAD.com Do Not Duplicate ©2012
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
Each manual includes a dataset that can be downloaded from our website.
www.EnvisionCAD.com/courseware
The VBA Editor
Variables
Integer
Long
Single
Double
String
Boolean
Date
Variant
Object
DesignFile
ModelReference
Level
Settings
Geometric related constants
3. Procedures
Example
Conditional Expressions
Decision Structures
5. User Forms
More on Message Boxes
Formatting
Lab 7A – Changing Elements to Bylevel Symbology
Lab 7B – Create a Print Macro
8. MicroStation Properties
9. Element Processing
Lab 9 – Element Processing
10. Creating New Elements
Resume Next
Step 3 – Implement the Error Handler
Debugging
Run
Break
Stop
Training Files
Back Cover