Acknowledgments
I have to give a very special thanks to all the great folks at Sybex, especially Willem Knibbe, for working on and helping to get this project off the ground after a few years of talking about it.
The next two people I would like to thank are Mary Ellen Schutz and Dassi Zeidel, the development and production editors on this book; you two made sure I stayed on track and delivered a high-quality book. I also want to thank Liz Welch (copyeditor), Candace Cunningham (proof-reader), and Ted Laux (indexer) for the work you all did on this book.
Thanks to all the folks at Autodesk, who put in the long hours and are dedicated to the work they do on the Autodesk® AutoCAD® product. I cannot forget one of the most important individuals on this book, my technical editor, Richard Lawrence. Richard is a great friend who I met many years ago at Autodesk University. He is a passionate and driven user of AutoCAD and is always looking to improve the way he uses AutoCAD. Richard, I appreciate everything that you have done to make this book better. Congrats on making it through your fi rst book as a technical editor.
Introduction
Welcome to AutoCAD Platform Customization: VBA! Have you ever thought to yourself, why doesn’t the Autodesk® AutoCAD® program include every feature I need? Why isn’t it stream lined for the type of work I perform? If so, you are not alone. AutoCAD at its core is a drafting platform that, through programming, can be shaped and molded to more effi ciently complete the tasks you perform on a daily basis and enhance your company’s workfl ows. Take a deep breath. I did just mention programming, but programming isn’t something to fear. At fi rst, just the idea of programming makes many people want to run in the opposite direction—myself included. The productivity gains are what propelled me forward. Programming isn’t all that different from anything else you’ve tried doing for the fi rst time.
In many ways, learning to program is much like learning a foreign language. For many new to Visual Basic for Applications (VBA), the starting place is learning the basics: the syntax of the programming language and how to leverage commands and system variables. Executing commands and working with system variables using the SendCommand and PostCommand methods can be a quick way to get started and become comfortable with VBA. After you are comfortable with the syntax of VBA and the SendCommand and PostCommand functions, you can begin to learn how to access the AutoCAD Object library to develop more complex and robust programs.
About Th is Book
AutoCAD Platform Customization: VBA provides you with an understanding of the VBA programming language and how it can be used in combination with the AutoCAD Object library to improve your productivity. This book is designed to be more than just an introduction to VBA and the AutoCAD Object library; it is a resource that can be used time and again when developing VBA programs for use with AutoCAD. As you page through this book, you will notice that it contains sample code and exercises that are based on real-world solutions.
This book is the third and fi nal book in a series that focuses on customizing and programming AutoCAD. The three-book series as a whole is known as AutoCAD Platform
Customization: User Interface, AutoLISP, VBA, and Beyond, which will be available as a printed book in 2015. Book 1 in the series, AutoCAD Platform Customization: User Interface and Beyond, was published in early 2014 and focused on CAD standards and general customization of AutoCAD; Book 2, AutoCAD Platform Customization: AutoLISP, was published in mid-2014 and covers the AutoLISP programming language.
Is Th is Book for You?
AutoCAD Platform Customization: VBA covers many aspects of VBA programming for AutoCAD on Windows. If any of the following are true, this book will be useful to you:
- You want to develop and load custom programs with the VBA programming language for use in the AutoCAD drawing environment.
- You want to automate the creation and manipulation of drawing objects.
- You want to automate repetitive tasks.
- You want to help manage and enforce CAD standards for your company.
NOTE VBA programming isn’t supported for AutoCAD on Mac OS.
VBA in AutoCAD
VBA is often overlooked as one of the options available to extend the AutoCAD program. There
is no additional software to purchase, but you must download and install a release-specifi c secondary component to use VBA. You can leverage VBA to perform simple tasks, such as inserting a title block with a specifi c insertion point, scale, and rotation and placing the block reference on a specifi c layer. To perform the same tasks manually, end users would have to fi rst set a layer as current, choose the block they want to insert, and specify the properties of the block, which in the case of a title block are almost always the same.
The VBA programming language and AutoCAD Object library can be used to do the following:
- Create and manipulate graphical objects in a drawing, such as lines, circles, and arcs
- Create and manipulate nongraphical objects in a drawing, such as layers, dimension styles, and named views
- Perform mathematical and geometric calculations
- Request input from or display messages to the user at the Command prompt
- Interact with fi les and directories in the operating system
- Read from and write to external fi les
- Connect to applications that support ActiveX and COM
- Display user forms and get input from the end user
VBA code statements are entered into the Visual Basic Editor and stored in a DVB fi le. Once a VBA project has been loaded, you can execute the macros through the Macros dialog box.
Unlike standard AutoCAD commands, macros cannot be executed from the Command prompt, but once executed, a macro can prompt users for values at the Command prompt or with a user form. It is possible to execute a macro from a command macro that is activated with a command button displayed in the AutoCAD user interface or as a tool on a tool palette.
Contents at a Glance
Introduction
Chapter 1 • Understanding the AutoCAD VBA Environment
Chapter 2 • Understanding Visual Basic for Applications
Chapter 3 • Interacting with the Application and Documents Objects
Chapter 4 • Creating and Modifying Drawing Objects
Chapter 5 • Interacting with the User and Controlling the Current View
Chapter 6 • Annotating Objects
Chapter 7 • Working with Blocks and External References
Chapter 8 • Outputting Drawings
Chapter 9 • Storing and Retrieving Custom Data
Chapter 10 • Modifying the Application and Working with Events
Chapter 11 • Creating and Displaying User Forms
Chapter 12 • Communicating with Other Applications
Chapter 13 • Handling Errors and Deploying VBA Projects
Index