AutoCAD 2004 VBA: A Programmer's Reference978-1-4302-0787... · 2017. 8. 23. · AutoCAD 2004 VBA:...
Transcript of AutoCAD 2004 VBA: A Programmer's Reference978-1-4302-0787... · 2017. 8. 23. · AutoCAD 2004 VBA:...
AutoCAD 2004 VBA: A Programmer's Reference
JOE SUTPHIN
APress Media, LLC
AutoCAD 2004 VBA: A Programmer's Reference Copyright @ 2004 by Joe Sutphin Originally published by Apress in 2004
All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage or retrieval system, without the prior written permission of the copyright owner and the publisher.
ISBN 978-1-59059-272-4 ISBN 978-1-4302-0787-0 (eBook) DOI 10.1007/978-1-4302-0787-0
Trademarked names may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, we use the names only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark.
Technical Reviewer: David Stein
Editorial Board: Dan Appleman, Craig Berry; Gary Cornell, Tony Davis, Steven Rycroft, Julian Skinner, Martin Streicher, Jim Sumser, Karen Watterson, Gavin Wray, John Zukowski
Assistant Publisher: Grace Wong
Project Manager: Sofia Marchant
Copy Editors: Nicole LeClerc, Jim Grey, Ami Knox
Production Manager: Kari Brooks
Production Editor: Kelly Wmquist
Proofreader: Lori Bring
Compositor: Molly Sharp
Indexer: Valerie Hanes Perry
Artist: April Milne
Cover Designer: Kurt Krames
Manufacturing Manager: Tom Debolski
In the United States: phone I-BOO-SPRINGER, email [email protected]. or visit http:/ /www.springer-ny.com. Outside the United States: fax +49 6221 345229, email orders@springer • de, or visit http://www.springer.de.
For information on translations, please contact Apress directly at 2560 Ninth Street, Suite 219, Berkeley, CA 94710. Phone 510-549-5930, fax 510-549-5939, email [email protected], or visit http://www.apress.com.
The information in this book is distributed on an "as is" basis, without warranty. Although every precaution has been taken in the preparation of this work, neither the author{s) nor Apress shall have any liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by the information contained in this work.
This book is dedicated to my wife, Grace. Without her, I would not be able to accomplish the task of writing a book.
Contents at a Glance
Chapter 1
Chapter 2
Chapter 3
Chapter 4
Chapter 5
Chapter 6
Chapter 7
Chapter 8
Chapter 9
Chapter 10
Chapter 11
Chapter 12
Chapter 13
Chapter 14
Chapter 15
Chapter 16
Chapter 17
Chapter 18
Chapter 19
Appendix A
Appendix B
Appendix C
Appendix 0
The VBA Integrated Development
Environment (VBAIDE) ................................ 1
Introduction to Visual Basic Programming ........ 29
Application Elements ............................... 69
AutoCAD Events ...................................... 87
User Preferences .................................... 97
Controlling Layers and Linetypes ................ 109
User Interaction and the Utility Object ........ 137
Drawing Objects .................................... 177
Creating 3-D Objects .............................. 215
Editing Objects .................................... 253
Dimensions and Annotations ....................... 283
Selection Sets and Groups ........................ 317
Blocks, Attributes, and External References .... 347
Views and Viewports .............................. .389
Layout and Plot Configurations .................. ,409
Controlling Menus and Toolbars ................... 431
Drawing Sec uri ty ................................... 465
Using the Windows API ............................. 473
Connecting to External Applications ............. 487
AutoCAD Object Summary ............................ 501
AutoCAD Constants Reference ...................... 763
System Variables ................................... 807
AutoCAD 2000, 2002, and 2004
Object Model Cross Reference ..................... 837
Index ............................................... 873
v
Contents
About the Author ................................................ xiii
Acknowledgements ................................................... xv
Introduction ..................................................... xvii
Chapter 1 The VBA Integrated Development Environment (VBAIDE) ............................ 1
Visual Basic Concepts .............................................. 1 Starting the Editor ............................................... .4 Exploring the User Interface ..................................... .4 Managing Projects ................................................. 11 Using the Text Editor ............................................. 24 Summary ............................................................. 27
Chapter 2 Introduction to Visual Basic Programming ..................... 29
Variables ........................................................... 29 Modules ............................................................. 39 Procedures ......................................................... 41 Control Structures ............................................... .44 Loop Structures .................................................... 48 Nested Control Structures ........................................ 52 Exiting a Control Structure ...................................... 53 Exiting a Sub or Function Procedure ............................. 54 Application Writing Techniques ................................... 55 Overview of Object-Oriented Programming ........................ 57 Summary ............................................................. 68
Chapter 3 Application Elements ........................... 69
Designing a UserForm .............................................. 69 Adding a Control to a Form ....................................... 72 Visual Basic ActiveX Controls .................................... 75 Additional ActiveX Controls ...................................... 83 Summary ............................................................. 86
vii
Contents
Chapter 4 AutoCAD Events ................................... 87
Application-Level Events .......................................... 87 Document-Level Events ............................................. 90 Object-Level Events ............................................... 94 Summary ............................................................. 95
Chapter 5 User Preferences ................................ 97
Getting and Setting Support Path(s) ............................. 99 Controlling Cursor Size ......................................... 100 Getting and Setting the AutoSavelnterval Property ............ l0l Getting and Setting the Drawing Template File Path .......... 101 Getting and Setting Plotter Configuration Path Settings ..... 102 Getting and Setting the File SaveAs Type ...................... 103 Enabling or Disabling the Startup Dialog Box ................. 105 Saving and Retrieving Personal Preferences .................... 106 User Preferences Changes in AutoCAD 2004 ...................... 107 Summary ............................................................ 108
Chapter 6 Controlling layers and linetypes ......... 109
Layers ............................................................. 109 Linetypes ......................................................... 125 Summary ............................................................ 136
Chapter 7 User Interaction and the Utility Object ............................ 137
The Utility Object ............................................... 137 Summary ............................................................ 175
Chapter 8 Drawing Obj ects ................................ 177
Controlling the Drawing Space ................................... 177 Creating Objects ................................................. 179 Summary ............................................................ 213
viii
Contents
Chapter 9 Creating 3-D Objects ......................... 215
The 3DSolid Object ............................................... 215 Creating Simple Solid Objects ................................... 217 Creating Elliptical 3-D Objects ................................ 227 Creating Extruded and Revolved Objects ........................ 232 Editing Solids .................................................... 240 Analyzing Solids: Mass Properties .............................. 249 Summary ............................................................ 251
Chapter 10 Editing Objects ................................ 253
Editing with Methods ............................................. 253 Editing with Properties ......................................... 276 Summary ............................................................ 282
Chapter 11 Dimensions and Annotations ................. 283
Dimensions ........................................................ 283 The Tolerance Object ............................................. 300 Annotations ....................................................... 303 Summary ............................................................ 316
Chapter 12 Selection Sets and Groups .................. 317
Selection Sets .................................................... 317 Groups ............................................................. 340 Summary ............................................................ 346
Chapter 13 Blocks, Attributes, and External References ........................... 347
Blocks and Block References ..................................... 348 Attributes ........................................................ 376 Summary ............................................................ 387
Chapter 14 Views and Viewports ........................... 389
Views .............................................................. 389 Viewports ......................................................... 394 Summary ............................................................ 407
ix
Contents
Chapter 15 Layout and Plot Configurations ............ 409
The Plot Object .................................................. 409 Summary ............................................................ 429
Chapter 16 Controlling Menus and Toolbars ............ 431
The MenuGroups Collection ...................................... .432 The MenuGroup Object ............................................ .435 Summary ............................................................ 464
Chapter 17 Drawing Security ............................... 465
Digital Signatures ............................................... 465 Password Protection ............................................. .471 Summary ............................................................ 472
Chapter 18 Using the Windows API. ....................... 473
Declarations ...................................................... 473 Windows Data Structures ......................................... 474 Dll to Visual Basic Calling Conventions ....................... 474 Specifying the library .......................................... .476 The Major Windows Dlls .......................................... 477 Working with Windows API Procedures That Use Strings ....... .478 Passing Arguments by Value or by Reference ................... 478 learning by Example .............................................. 479 Summary ............................................................ 486
Chapter 19 Connecting to External Applications ..... 487
Making the Connection ........................................... .487 Connecting to Microsoft Excel ................................... 490 Connecting to Microsoft Word .................................... 492 Connecting to a Microsoft Access Database ..................... 494 Summary ............................................................ 499
Appendix A AutoCAD Object Summary ....................... 501
AutoCAD Object Reference ........................................ 518
x
Contents
Appendix B AutoCAD Constants Reference ................ 763
AC3DPolylineType .................................................. 763 AcActiveSpace ..................................................... 763 AcAlignment ....................................................... 764 AcAlignmentPointAcquisition ..................................... 765 AcAngleUnits ...................................................... 765 AcARXDemandLoad ................................................... 766 AcAttachmentPoint ................................................ 766 AcAttributeMode ................................................... 767 AcBooleanType ..................................................... 768 AcColor (and the Colorlndex Property
of the AcCmColor Object) ...................................... 768 AcCmColor ......................................................... 769 AcCoordinateSystem ............................................... 770 AcDimArrowheadType ............................................... 770 AcDimCenterType ................................................... 771 AcDimFit ........................................................... 772 AcDimFractionType ................................................ 772 AcDimHorizontalJustification .................................... 773 AcDimLUnits ....................................................... 773 AcDimPrecision .................................................... 774 AcDimTextMovement ................................................ 775 AcDimToleranceJustify ............................................ 775 AcDimToleranceMethod ............................................. 776 AcDimUnits ........................................................ 776 AcDimVerticalJ ustification ...................................... 777 AcDrawingAreaSCMCommand .......................................... 778 AcDrawingAreaSCMDefault .......................................... 778 AcDrawingAreaSCMEdi t ............................................. 779 AcDrawingDirection ............................................... 779 AcEnti tyName ...................................................... 780 AcExtendOption .................................................... 781 AcHatchObjectType ................................................ 782 AcHatchStyle ...................................................... 782 AcHorizontalAlignment ............................................ 783 AclnsertUni ts ..................................................... 784 AcISOPenWidth ..................................................... 785 AcKeyboardAccelerator ............................................ 785 AcKeyboardPriority ............................................... 786 AcLeaderType ...................................................... 786 AcLineSpacingStyle ............................................... 787 AcLineWeight ...................................................... 787 AcLoopType ........................................................ 789
xi
Contents
AcMenuFileType .................................................... 789 AcMenuGroupType ................................................... 789 AcMenuItemType .................................................... 790 AcOleQuali ty ...................................................... 790 AcPatternType ..................................................... 790 AcPlotPaperUnits .................................................. 791 AcPlotPolicy ...................................................... 791 AcPlotRotation .................................................... 792 AcPlotScale ....................................................... 792 AcPlotType ........................................................ 794 AcPolylineType .................................................... 794 AcPolymeshType .................................................... 795 AcPreviewMode ..................................................... 795 AcPrinterSpoolAlert .............................................. 796 ACProxylmage ...................................................... 796 AcRegenType ....................................................... 796 AcSaveAsType ...................................................... 797 AcSelect ........................................................... 798 AcTextFontStyle ................................................... 799 AcTextGenerationFlag ............................................. 799 AcToolbarDockStatus .............................................. 800 AcToolbarItemType ................................................ 800 AcUnits ............................................................ 801 AcVerticalAlignment .............................................. 801 AcViewportScale ................................................... 802 AcViewportSplitType .............................................. 803 AcWindowState ..................................................... 804 AcXRefDemandLoad .................................................. 805 AcZoomScaleType ................................................... 805
Appendix C System Variables ............................... 807
Appendix D AutoCAD 2000, 2002, and 2004 Object Model Cross Reference .............. 837
Objects Comparison ............................................... 837 Methods Comparison ............................................... 841 Properties Comparison ............................................ 850 Events Comparison ................................................ 869
Index ............................................................ 873
xii
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 over 15 years, with the last 10 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 titled AutoCAD 2000 VBA Programmer's Reference.
xiii
Acknowledgments
There are many, many people to thank on this project. Please forgive me if I miss mentioning your name or misspell it. First, the people at Apress. Without them I would not have had the opportunity to create this work. Thanks to Gary Cornell and Dan Appleman for their initial acceptance and input, Beth Christmas for coordinating things, and Karen Watterson for continuing to give me opportunities for achievement.
A great big thank you to the editorial and production staff: Grace Wong, Sofia Marchant, Nicole LeClerc, Jim Grey, Ami Knox, and KellyWinquist. Thanks to Doris Wong for putting up with my demands for the web site content, and thanks to Jessica Dolcourt for coordinating the book cover. I enjoyed working with each of you.
A special thanks to my technical reviewer, David Stein. A special thank you to my children, Stephen, Clair, David, and Emily, for
understanding when daddy had to work. A special thank you to Rod Ellis for helping me with the picture for this book. Lastly, thanks to those who helped and whose names I forgot to include
here. To everyone in the AutoCAD community with whom I've had the pleasure of crossing paths, from the bottom of my heart, thanks!
xv
Introduction
THIS BOOK PROVIDES A CONCISE GUIDE for programmers to the kind of customization you can achieve with AutoCAD 2004. It demonstrates the use of AutoCAD through short code examples written in Visual Basic for Applications (VBA). It also includes a full reference section for quick reference on all the events, methods, and properties are available with AutoCAD.
What Is AutoCAD?
So, what is AutoCAD? First released in 1982 under the name Micro CAD, AutoCAD has become a powerful tool for drafting and design purposes. AutoCAD 2004 incorporates many new features to enhance flexibility and drawing control. To reflect this extra functionality, many new ActiveX objects, properties, methods, and events have been included for improved programmability.
What Is This Book About?
This book is about AutoCAD 2004 and how to use AutoCAD VBA in your applications to handle all your drawing tasks more efficiently. It shows you how to programmatically control the creation and editing of individual drawing objects, manipulate linetypes and layers, control text and dimension styles, and much more. As you encounter each of these topics, you'll cover all the associated objects including their properties, methods, and events.
By interfacing into AutoCAD, you can exploit all of AutoCAD's functionality that would have taken you a long time to write yourself. This book will first help you learn how to make use of this functionality. Then it will become a handy reference for later when you have a question that you just can't remember the answer to.
This book offers you, the reader, an easy-to-follow tutorial and reference to AutoCAD 2004, by splitting the whole topic into neat and intuitive segments. This also makes it easier to find specific information later when you need it (Le., when you're coding real-world applications). To aid you in finding this information as quickly as possible, a comprehensive quick reference section is included. The reference section lists all of the properties, methods, and events of each of the AutoCAD 2004 objects.
xvii
Introduction
xviii
This book is divided into three main parts:
• The first part (Chapters 1 through 3) provides a rapid introduction to AutoCAD, its object model, and VBA, and it explains the notation and commands particular to AutoCAD VBA projects.
• The middle part (Chapters 4 through 19) supplies a detailed breakdown of most of the object model, covering common tasks complete with many and varied code examples demonstrating the use of the relevant objects' methods and properties.
• Finally, there's a quick reference section (Appendixes A through D), which gives you a description of all the members of all AutoCAD objects at a glance.
Who Is This Book For?
The book is a reference guide for AutoCAD programmers, and it's primarily designed to explain and demonstrate the features of AutoCAD 2004. As such, this isn't a beginner's guide, though if you've programmed in any language that can interface with other COM objects, you should be able to easily understand and use this book.
In particular, the book is aimed at programmers who use AutoCAD for daily tasks and can see the benefits of customizing and automating these tasks. Programming techniques needed to create and modify AutoCAD drawings, customize preferences, query and set system variables, and so on using the built-in VBA are presented.
AutoCAD may be customized to virtually any degree of sophistication. If you can think it up, then I bet you can use AutoCAD VBA to help you achieve your goal.
Tell Us What You Think
We've worked hard on this book to make it enjoyable and useful. Our best reward would be to hear from you that you liked it and that it was worth the money you paid for it. We've done our best to try to understand and match your expectations.
Please let us know what you think about it. Tell us what you liked best and what we could have done better. Uyou think this is just a marketing gimmick, then test us out-drop us a line! We'll answer, and we'll take whatever you say under consideration for future editions. The easiest way to do so is to send e-mail [email protected].
You can also find more details about Apress on our web site. There you'll find the code from our latest books, sneak previews of forthcoming titles, and information about the authors and the editors. You can order Apress titles or find out where your nearest local bookstore with Apress titles is located. The address of our site is http://www.apress.com.
Customer Support
If you find a mistake in the book, your first port of call should be the errata page for this book on our web site. You can see any errata already posted there or submit your own.
If you can't find an answer there, send an e-mail [email protected] telling us about the problem. We'll do everything we can to answer promptly. Please remember to let us know the book your query relates to and, if possible, the page number as well. This will help us to reply to you quickly.
Introduction
xix