Sunteți pe pagina 1din 7

AutoCAD VBA

Programming
TOOLS AND TECHNIQUES

John Gibband Bill Kramer

Freeman
fBooks
San Francisco
Table of Contents

Introduction xiii
Chapter 1: The AutoCAD VBA Environment 1
AutoCAD Programming Solutions 1
How AutoLISP Differs from VBA 3
Visual BASIC versus Visual BASIC for Applications 7
Essential VBA Vocabulary and Concepts 8
Getting Comfortable with the IDE 12
Starting the VBA IDE 13
What's in the IDE? 14
Developing Applications with VBA inside AutoCad 17

Chapter 2: VBA Application Jump-Start 20


Description of the Application 20
Start Your IDE 21
Design the Form 21
First Lines of Program Code 23
Form Properties 24
Add a List Box Control 25
Programming the Starting Sequence 26
Global Variable Declarations 27
Counting Blocks Macro 28
Test Run the Main Macro 33
List Box Reactor Function 34
Test the Program 37
Final Test 38

Chapter 3: Understanding the AutoCAD Object-Oriented Database 39


Object-Oriented Programming Introduction 39
What's an Object? 41
The Management Features of Objects 42
Object Concept Example 43
Computer Science Description of Objects 46
How Objects Are Stored in a Computer 47
About Creating New Objects 49
The AutoCAD Object Tree 49
The Document Object 52
Collection Objects 53
The Block Collection Object 53
Entity Objects 54
Learning about Objects in VBA 56
Chapter 4: The AutoCAD Object Model 59
Methods, Properties, and Events 59
Exploration of the AutoCAD Object Tree 60
Getting to the Application Object 61
The Application Object 62
Accessing the Document Object in VB 66
The Documents Collection 67
Document Object Methods 70
Document Object Properties 71
Document Object Events 74
Event Programming Example 77
Utility Object Functions 78
Collections of Objects 83
Entity Objects 85
Curved Entity Objects 89
Text Entity Objects 93
Preferences 94
Example Sequence 96

Chapter 5: Working with Entity Collection Objects 102


The Collection Object 102
Selection Sets Collection 103
Building a Collection of Entity Objects 104
Accessing Selection Set Members 106
More Selection Set Methods and Properties 107
Selection Set Collection Iterations 108
ModelSpace and PaperSpace Collections 111
Even More Selection Set Methods 112
Filtering the Selection Set 117
Refining the Filters - Numeric Tests 120
String Tests in Filters 123
Combining Multiple Tests 124
Selection Set Maintenance Issues 128
Working with Collections 130

Chapter 6: Drawing Tables 131


AutoCAD Drawing Tables 131
Tables Are Collections 132
The Dictionary Collection 133
The DimStyles Collection 134
The Groups Collection 136
Groups versus Dictionaries 138
The Layer Collection 139
The Linetypes Collection 142
The Registered Apps Collection 143
The Text Styles Collection 143
The UCS Collection 145
The Views Collection 147
The View Ports Collection 148
View Port Object 150
The Paper Space View Port Object 152
The Blocks Collection 154
Creating a New Block Definition 155
Reading a Block Definition 156
Working with Attribute Objects in a Block 162

Chapter 7: Working with Other Applications 167


Object Models 167
Interface to Microsoft Excel 169
Excel Application 1-Simple Database 170
Link to Excel 171
Variable Declarations 172
Excel Linkage Function 173
Closing the Workbook 175
Searching the Worksheet 176
Another Example Interface to Microsoft Excel 179
Using Handies 184
Object ID versus Handle 186
Linking with Other Applications 187
Storing Data in AutoCAD 188
Dictionary Object 188
Xrecord Objects 189
Accessing a Dictionary 189
Xrecord Contents 190
Reading Xrecords 192
Group Codes in Xrecords 193
Extended Data 194
Simple Xdata Example 196
Chapter 8: Managing Multiple Projects 201
Putting Complete Applications Together 201
Loading Projects to Run 204
Embedded Projects 205
Projects on Disk 205
Re-using Code 207
Running a Macro from Another Project 208
Sharing Data between Projects 210
Late and Early Binding in VBA 211
Multiple Projects in Release 14 212

Chapter9: API Calls from VBA 214


What's an API? 214
The API Viewer 216
INI File Calls 217
How the PrivateProfileString APIs Work 218
Registry Calls 225
The Registry Editor 226
The VBA Registry Functions 227
System Calls 230
Computer Name 232
User's Login Name 232
System and Local Time 233
The Windows Temporary Directory 234
System Environmental Variable 234
Free Disk Space 235
Conclusion 236

Chapter 10: VBA Toolbox Routines 237


Definition of Terms 238
Toolbox Overview 238
Acquiring Data Items from a Data String 239
Add Back Slashes Result to a Path String 240
Add Trailing Back Slash to a String Path 241
Convert an Object's Entity Type to English 242
Count the Number of Occurrences of a Delimiter
in a String 243
Create a Directory Path 244
Determine the Tense of a Word 245
Display a Fatal Error Message 246
Find a File 247
Find a Line in a List Box Control 248
Find an Application Window 249
Format a Point in a String 250
Get the Current View's Corner Points 251
Get the Extension from a String 252
Get the Left Substring from a String 253
Get the Path from a String 254
Get the Right Substring from a String 255
Päd a String with Spaces 256
Remove a String's Null Terminator 257
Replace all Requested Characters in a String
with Another Character 258
Retrieve the System's Windows Directory 259
Show a Progress Spinner 260
Strip an Extension from a String 262
Strip the Path from a String 263
Verify a Control Exists on a Form 264
Verify a File's Existence 265
Verify a Listbox Item Exists 266

Chapterll: VB versus VBA 267


Quick Comparison of VB and VBA 268
Which Language for Which Situation? 272
Differences at the System Level 273
Porting Issues — from VB to VBA 273
Porting Issues — from VBA to VB 274
Attaching Your VB Application to the AutoCAD
Object Model 274
VB and VBA Help 276
VB and VBA Help Conclusion 287

Chapter 12: Gotcha's and Tips 288


Divergent Data Type Comparison 289
Using Control Keys to Maneuver in VBA 291
Class Modules in VBA 293
The Special Character for Date and Format 295
Named and Optional Arguments 296
Using an Array of Doubles versus a Variant as an Array . . . 298
Passing an Array to an Object's Methods 300
Collections and Procedures 300
Short Circuit Evaluation 302
Accessing Objects on the Same Level
as Your Current Object 303
Listbox Columns 305
Multiselect in a Listbox 308
Variable Name Shortcuts 309
Command Line Workaround 309
Passing Forms and Controls as Parameters 311
Multipage Form Controls 312
Conclusion 312

Chapter 13: Programming Style 313


Commenting Your Code 314
Keeping Code Concise and Simple 316
Proper Use of Subroutines and Functions 318
Scope, Precedence, and Life of Variables 319
Error Trapping 322
Do You Need to Change Your Programming Style
for VBA? 323
Event-Driven versus Procedural-Driven Language 324
The Hungarian Naming Convention 326
Conclusion 330

Chapter 14: Class Modules and Objects 331


Not All Objects Are Equal 331
The VBA Class Module 332
Objects Example 1: Extended Data Manipulations 334
Making a Class Module 335
Properties 337
Methods 341
The Extended Data Example Project 345
Extended Data Object Conclusion 348
Objects Example 2: Hole Chart 349
An Object in an Object 350
The Inner Object 352
The Hole Charting Example Project 355
Hole Chart Application Conclusion 357
Creating Your Own Object 357

S-ar putea să vă placă și