Contents

About the Author xxv

About the Technical Reviewers xxvii

Acknowledgments xxix

Introduction xxxi

■chapter 1 The VBA Integrated Development Environment

(VBAIDE) 1

Visual Basic Concepts 1

Windows, Events, and Messages 1

Event-Driven vs. Procedural Programming 2

Developing Your Applications Interactively 3

Starting the Editor 3

Exploring the User Interface 4

The Project Explorer 5

The Code Window 5

The Properties Window 6

The Object and Procedure Boxes 6

The Immediate Window 7

The Options Dialog Box 8

Managing Projects 9

Project Structure 9

Creating, Opening, and Saving Projects 10

Adding, Saving, and Removing Files 12

Adding ActiveX Controls and Code Components 13

The Object Browser 15

VBARUN and the Macros Dialog Box 17

Overview of AutoCAD VBA Commands 22

Summary 22

chapter 2 Introduction to Visual Basic Programming 23

Variables 23

Declaring Variables 23

Variable Scope and Lifetime 25

Constants 27

Data Types 27

Introduction to Arrays 29

Modules 31

UserForm 31

Procedures 32

Calling Procedures 34

Passing Arguments to Procedures 34

Control Structures 34

Decision Structures 34

Select Case 36

Loop Structures 37

Nested Control Structures 40

Exiting a Control Structure 41

Exiting a Sub or Function Procedure 42

Application Writing Techniques 43

Writing Statements on Multiple Lines 43

Combining Statements on a Single Line 43

Adding Comments to Your Code 44

Overview of Object-Oriented Programming 44

Objects and Classes 44

Object Data 45

Private Variables 45

Public Variables 45

An Object's Behavior 45

Debugging Basics 46

Summary 53

chapter 3 Application Elements 55

Designing a UserForm 55

Adding a UserForm to Your Application 55

Setting UserForm Properties 56

Adding a Control to a Form 58

Visual Basic ActiveX Controls 60

Label 60

TextBox 61

ComboBox 61

ListBox 62

CheckBox 62

OptionButton 63

ToggleButton 63

Frame 64

CommandButton 64

Additional ActiveX Controls 64

TabStrip 64

MultiPage 65

ScrollBar 65

SpinButton 65

Image 65

Summary 66

CHAPTER 4 AutoCAD Events 67

Application-Level Events 67

Document-Level Events 70

The BeginCommand and EndCommand Events 71

The BeginOpen and EndOpen Events 72

The BeginClose and BeginDocClose Events 72

The Activate and Deactivate Events 73

The BeginSave and EndSave Events 73

Object-Level Events 73

Summary 74

chapter 5 User Preferences 75

Getting and Setting Support Path(s) 77

Controlling Cursor Size 78

Getting and Setting the AutoSaveInterval Property 78

Getting and Setting the Drawing Template File Path 79

Getting and Setting the Printer Support Path 79

Getting and Setting the File SaveAs Type 80

Enabling and Disabling the Startup Dialog Box 82

Saving and Retrieving Personal Preferences 83

User Preferences Changes in AutoCAD 2004 84

Summary 85

chapter 6 Controlling Layers and Linetypes 87

Layers 87

Accessing Layers 88

Iterating Layers 88

Checking for Existing Layers 89

Creating a New Layer 91

Making a Layer Active 92

Turning a Layer On/Off 94

Setting a Layer to Be Frozen or Thawed 95

Locking/Unlocking a Layer 95

Making Layers Plottable 96

Renaming a Layer 96

Deleting a Layer 97

Getting a Layer's Handle 98

Layer Colors 98

Layer Linetypes 99

Layer Lineweights 100

Linetypes 100

Accessing Linetypes 101

Checking for Existing Linetypes 102

Loading a Linetype 103

Making a Linetype Active 105

Renaming a Linetype 106

Deleting a Linetype 107

Getting a Linetype's Handle 108

Changing a Linetype's Description 108

Scaling Linetypes 109

Summary 110

chapter 7 User Interaction and the Utility Object 111

Interface Methods 111

Input Methods and Dialogs 111

The Prompt Method 113

The InitializeUserInput Method 113

The GetXXX Methods 115

Handling Errors in User Input 128

Conversion Methods 129

The AngleToReal Method 129

The AngleToString Method 130

The DistanceToReal Method 130

The RealToString Method 131

The AngleFromXAxis Method 132

The PolarPoint Method 133

The TranslateCoordinates Method 134

Internet Methods 136

The IsURL Method 136

The LaunchBrowserDialog Method 137

The GetRemoteFile Method 139

The IsRemoteFile Method 140

The PutRemoteFile Method 140

Summary 141

chapter 8 Drawing Objects 143

Controlling the Drawing Space 143

The ModelSpace and PaperSpace Collections 144

Creating Objects 145

Circular Objects 145

Line Objects 150

Other Objects of Interest 162

Summary 172

chapter 9 Creating 3-D Objects 173

Understanding the 3DSolid Object 173

Creating Simple Solid Objects 174

The Box 174

The Cone 176

The Cylinder 177

The Sphere 179

The Torus 180

The Wedge 181

Creating Elliptical 3-D Objects 183

The Elliptical Cone 183

The Elliptical Cylinder 185

Creating Extruded and Revolved Objects 186

The Extruded Solid 187

The Extruded Solid Along a Path 189

The Revolved Solid 191

Editing Solids 194

Boolean Operations 194

Interference Operation 196

Slicing Solids 198

Sectioning Solids 199

Analyzing Solids: Mass Properties 201

Summary 203

chapter 10 Editing Objects 205

Editing with Methods 205

Copying Objects 206

Deleting Objects 206

Exploding Objects 207

Highlighting Entities 208

Mirroring Objects 209

Moving Objects 212

Offsetting Objects 213

Rotating Objects 215

Scaling Objects 218

Object Arrays 219

Editing with Properties 223

Changing an Object's Color 223

Changing an Object's TrueColor Property 225

Changing an Object's Color Properties 226

Changing an Object's Layer 226

Changing an Object's Linetype 227

Changing an Object's Visibility 228

The Update Method 229

Summary 229

chapter 11 Dimensions and Annotations 231

Working with Dimensions 231

Using the DimStyle Object 231

Setting Dimension Styles 232

Using the CopyFrom Method 232

Using Dimension Styles 233

Creating Dimensions 235

Using the Tolerance Object 245

Working with Annotations 248

Using the TextStyle Object 248

Using a TextStyle 254

Adding Annotations 256

Using the Leader Object 256

Summary 258

chapter 12 Selection Sets and Groups 259

Selection Sets 259

Adding a SelectionSet Object 259

Accessing and Iterating Selection Sets 260

Selecting Entities 261

Adding and Removing Items 273

The Clear, Delete, and Erase Methods 276

The PickFirstSelectionSet Property 277

Groups 278

Adding a Group Object 279

Accessing and Iterating Groups 279

Adding and Removing Items 280

The Delete Method 282

Summary 283

chapter 13 Blocks, Attributes, and External References 285

Blocks and Block References 286

Accessing Block Objects 288

Creating Blocks 289

Defining and Manipulating Blocks 294

Using MInsertBlock Objects 301

Using External References 305

Attributes 310

Creating Attributes 310

Manipulating Attribute References 315

Summary 320

chapter 14 Views and Viewports 321

Views 321

Creating a View 322

Setting a View as Current 324

Deleting a View 324

Viewports 325

The Model-Space Viewport 325

The Paper-Space Viewport 331

Summary 336

chapter 15 Layout and Plot Configurations 337

The Plot Object 337

Plotting Your Drawing 338

Plot Configurations 343

Controlling Plot Parameters 347

Summary 354

chapter 16 Controlling Menus and Toolbars 355

The MenuGroups Collection 356

Loading Menu Groups 356

The MenuGroup Object 359

Saving Menu Groups 361

Unloading Menu Groups 362

Accelerator Keys 362

Changing the Menu Bar 362

Editing Menus 369

Editing Toolbars 376

Summary 382

chapter 17 Drawing Security 383

Digital Signatures 383

The Action Property 384

The Algorithm Property 385

The Issuer Property 385

The ProviderName Property 386

The SerialNumber Property 386

The Subject Property 386

The TimeServer Property 387

Password Protection 387

Summary 389

chapter 18 Using the Windows API 391

Declarations 391

Windows Data Structures 392

Visual Basic-to-DLL Calling Conventions 392

Specifying the Library 393

The Major Windows DLLs 394

Working with Windows API Procedures That Use Strings 394

Passing Arguments by Value or by Reference 395

Learning by Example 395

OpenFile Common Control Dialog Replacement for VBA 395

SaveAsFile Common Control Dialog Replacement for VBA . . . . 398 Retrieving the Status of the Caps Lock, Num Lock, and Scroll Lock Keys 400

Summary 402

chapter 19 Connecting to External Applications 403

Making the Connection 403

Connecting to Microsoft Excel 405

Creating a New Workbook 405

Creating a New Worksheet 405

Accessing a Worksheet 406

Writing and Reading Cells 406

Saving and Exiting Excel 406

Connecting to Microsoft Word 407

Creating a New Document 407

Adding Text to the Document 407

Setting Page Orientation 407

Setting Margins 407

Setting the Document Header and Footer 408

Saving and Exiting Word 408

Connecting to a Microsoft Access Database 408

Connecting to a Database File 408

Retrieving a Set of Records 409

Writing Values to the Database File 410

Closing the Connection 410

Working with Other Databases 410

Connectivity Automation Objects 411

Advanced Database Issues 411

Working with Services and Other APIs 411

Summary 413

chapter 20 Creating Tables 415

The AddTable Method 415

The RegenerateTableSuppressed Property 416

The GetText Method 416

The SetText Method 417

The GetTextHeight Method 418

The SetTextHeight Method 419

The InsertRows Method 420

The InsertColumns Method 421

Putting It All Together 423

Summary 426

chapter 21 The SummaryInfo Object 427

Overview 428

Properties 428

The Author Property 428

The RevisionNumber Property 429

The Subject Property 429

The Title Property 430

Adding Custom SummaryInfo 430

The AddCustomInfo Method 430

The GetCustomByKey Method 431

The NumCustomInfo Method 432

The GetCustomByIndex Method 432

The RemoveCustomByKey Method 433

The RemoveCustomByIndex Method 433

The SetCustomByKey Method 434

The SetCustomByIndex Method 434

Summary 435

chapter 22 An Illustrative VBA Application 437

Start Building the Application 438

Writing the Code 438

Initial Declarations 438

Create a Session of Microsoft Word 439

Create a New Document in Word 440

Create a Table with the Word Document 440

Create Column Headings 440

Populate the Table with AutoCAD Layer Data 441

Helper Functions 442

Adjust the Fonts 444

Sort the Table 444

Autofit Column Text 445

Add Page Header and Footer 445

Print the Report 446

Summary 446

appendix A AutoCAD Object Summary 447

AutoCAD Collections 447

The Application Property 448

The AcadObject Object 448

The AcadEntity Object 451

AcadDimension Object 454

AutoCAD Object Reference 457

Acad3DFace Object 457

Acad3DPolyline Object 459

Acad3DSolid Object 460

AcadApplication Object 461

AcadArc Object 465

AcadAttribute Object 466

AcadAttributeReference Object 468

AcadBlock Object 469

AcadBlockReference Object 476

AcadBlocks Collection 477

AcadCircle Object 478

AcadDatabase Object 479

AcadDatabasePreferences Object 480

AcadDictionaries Collection 482

AcadDictionary Object 483

AcadDim3PointAngular Object 484

AcadDimAligned Object 486

AcadDimAngular Object 491

AcadDimArcLength Object 492

AcadDimDiametric Object 497

AcadDimOrdinate Object 501

AcadDimRadial Object 504

AcadDimRadialLarge Object 508

AcadDimRotated Object 513

AcadDimStyle Object 517

AcadDimStyles Collection 518

AcadDocument Object 519

AcadDocuments Collection 525

AcadDynamicBlockReferenceProperty Object 526

AcadEllipse Object 527

AcadExternalReference Object 528

AcadFileDependency Object 529

AcadFileDependencies Object 529

AcadGroup Object 530

AcadGroups Collection 531

AcadHatch Object 531

AcadHyperlink Object 534

AcadHyperlinks Collection 535

AcadIDPair Object 535

AcadLayer Object 536

AcadLayers Collection 537

AcadLayout and AcadPlotConfiguration Objects 538

AcadLayouts and AcadPlotConfigurations Collections 541

AcadLeader Object 542

AcadLine Object 544

AcadLineType Object 545

AcadLineTypes Collection 546

AcadLWPolyline Object 547

AcadMenuBar Collection 548

AcadMenuGroup Object 549

AcadMenuGroups Collection 550

AcadMInsertBlock Object 551

AcadMLine Object 552

AcadModelSpace Collection 553

AcadMText Object 553

AcadPaperSpace Collection 555

AcadPlot Object 555

AcadPlotConfiguration Object 557

AcadPlotConfigurations Collection 557

AcadPoint Object 557

AcadPolyfaceMesh Object 558

AcadPolygonMesh Object 559

AcadPolyline Object 561

AcadPopupMenu Object 563

AcadPopupMenuItem Object 565

AcadPopupMenus Collection 566

AcadPreferences Object 567

AcadPreferencesDisplay Object 568

AcadPreferencesDrafting Object 570

AcadPreferencesFiles Object 572

AcadPreferencesOpenSave Object 574

AcadPreferencesOutput Object 575

AcadPreferencesProfiles Object 576

AcadPreferencesSelection Object 577

AcadPreferencesSystem Object 578

AcadPreferencesUser Object 579

AcadPViewport Object 580

AcadRasterImage Object 583

AcadRay Object 585

AcadRegion Object 586

AcadRegisteredApplication Object 587

AcadRegisteredApplications Collection 587

AcadSelectionSet Object 588

AcadSelectionSets Collection 590

AcadShape Object 590

AcadSolid Object 591

AcadSortEntsTable Object 592

AcadSpline Object 593

AcadState Object 595

AcadSummaryInfo Object 596

AcadTable Object 597

AcadTableStyle Object 604

AcadText Object 606

AcadTextStyle Object 607

AcadTextStyles Collection 609

AcadTolerance Object 609

AcadToolbar Object 611

AcadToolbarItem Object 613

AcadToolbars Collection 614

AcadTrace Object 615

AcadUCS Object 616

AcadUCSs Collection 617

AcadUtility Object 618

AcadView Object 621

AcadViewport Object 622

AcadViewports Collection 624

AcadViews Collection 625

AcadXline Object 626

AcadXRecord Object 626

SecurityParams Object 627

AcCmColor Object 628

LayerStateManager Object 629

appendix B AutoCAD Constants Reference 631

Ac3DPolylineType 631

AcActiveSpace 631

ACAD_LWEIGHT 631

AcAlignment 632

AcAlignmentPointAcquisition 633

AcAngleUnits 633

AcARXDemandLoad 634

AcAttachmentPoint 634

AcAttributeMode 634

AcBlockScaling 635

AcBooleanType 635

AcCellAlignment 635

AcCellEdgeMask 636

AcCellType 636

AcColor 637

AcColorMethod 638

AcCoordinateSystem 638

AcDimArcLengthSymbol 639

AcDimArrowheadType 639

AcDimCenterType 640

AcDimFit 640

AcDimFractionType 640

AcDimHorizontalJustification 641

AcDimLUnits 641

AcDimPrecision 641

AcDimTextMovement 642

AcDimToleranceJustify 642

AcDimToleranceMethod 642

AcDimUnits 643

AcDimVerticalJustification 643

AcDrawingAreaSCMCommand 644

AcDrawingAreaSCMDefault 644

AcDrawingAreaSCMEdit 644

AcDrawingDirection 645

AcDynamicBlockReferenceProperty 645

AcEntityName 645

AcExtendOption 646

AcGradientPatternType 647

AcGridLineType 647

AcHatchObjectType 647

AcHatchStyle 648

AcHorizontalAlignment 648

AcInsertUnits 648

AcISOPenWidth 649

AcKeyboardAccelerator 650

AcKeyboardPriority 650

AcLeaderType 650

AcLineSpacingStyle 650

AcLineWeight 651

AcLoopType 652

AcMenuFileType 652

AcMenuGroupType 652

AcMenuItemType 652

AcMLineJustification 653

AcOleQuality 653

AcPatternType 653

AcPlotPaperUnits 654

AcPlotPolicy 654

AcPlotRotation 654

AcPlotScale 654

AcPlotType 656

AcPolylineType 656

AcPolymeshType 656

AcPreviewMode 657

AcPrinterSpoolAlert 657

AcProxyImage 657

AcRegenType 657

AcRotationAngle 658

AcRowType 658

AcSaveAsType 659

AcSelect 659

AcSelectType 660

AcShadePlot 660

AcTableDirection 661

AcTableStyleOverrides 661

AcTextFontStyle 665

AcTextGenerationFlag 665

AcToolbarDockStatus 666

AcToolbarItemType 666

AcUnits 666

AcVerticalAlignment 667

AcViewportScale 667

AcViewportSplitType 668

AcWindowState 669

AcXRefDemandLoad 669

AcZoomScaleType 669

appendix C System Variables 671

The GetVariable Method 671

The SetVariable Method 672

INDEX 697

About the Author

■JOE SUTPHIN's background includes more than 25 years in the machinery manufacturing industry. He has more than 18 years of CAD experience with 12+ years of AutoCAD-specific experience. Joe is an Autodesk-registered developer, and his work has appeared in the pages of Cadence and Cadalyst magazines. He has been programming for nearly 20 years, with the last 15 years being Visual Basic-specific experience. In 1998 he collaborated with Microsoft on a Visual Basic application case study. He is the author of the best-selling book AutoCAD 2000 VBA Programmer's Reference.

Was this article helpful?

0 0

Post a comment