Free Programming Books
Free download ebooks on computer and programming

Free Ebook "AutoCAD 2006 VBA: A Programmer's Reference" Sample Chapter

AutoCAD 2006 VBA...
Free download Chapter 4: AutoCAD Events
Download chapter

This book will help you take full advantage of the VBA programming environment within AutoCAD 2006. Whether you want to automate routine tasks, or create sophisticated applications or design that can be generated programmatically, this book will empower you to make AutoCAD work for you.

This book is also suitable if you're a power user who wants to make use of the advanced features of AutoCAD 2006 VBA, and you need a fast reference for the full AutoCAD object model. You'll learn to run Autodesk design tasks in the embedded VBA environment. This book is loaded with source code examples, and gives you-no matter your skill level-the necessary tools to accomplish your AutoCAD 2006 automation tasks.

< < prev next > >

Document-Level Events

Changes to a document or its contents result in document-level events. Adding or editing objects and regeneration of the drawing are just some examples of document-level events. Unlike application-level events, document-level events are available by default in the ThisDrawing module of an AutoCAD project.

The BeginCommand and EndCommand Events

When you issue an AutoCAD command such as LINE or DIM, the BeginCommand event is triggered. Any code that you've written inside the BeginCommand event procedure is then executed. Once the code associated with the BeginCommand event has finished executing and after the command itself has finished, the EndCommand event is triggered. Now any code that you've written inside the EndCommand event procedure executes. You may have code associated with either, both, or neither of the events. If a command is canceled prior to completion, such as when a user presses the Esc key, it doesn't fire the EndCommand event.

The BeginOpen and EndOpen Events

When AutoCAD receives a request to open an existing drawing file, the BeginOpen event is triggered. Once AutoCAD has finished loading the drawing file and it's visible, an EndOpen event occurs. One possible use of this event procedure would be to store all the current system variables before you open a drawing. Then once the drawing is opened, you restore the system variables back to their previous values.

When AutoCAD receives a request to create a new drawing file, a slightly different sequence of events occurs. The BeginOpen event occurs as before, and then the BeginSave event occurs (see the section "The BeginSave and EndSave Events" for details). This is followed by an EndSave event and finally an EndOpen event.

The BeginClose and BeginDocClose Events

The BeginClose event is triggered upon the closing of a drawing session within AutoCAD. Be careful when you use this event! If you attempt to perform a lengthy task, it can frustrate users due to slowness, or it could even result in serious problems with AutoCAD, causing it to become unstable, lock up, or crash entirely. The BeginDocClose event is similar to the BeginClose event. However, the BeginDocClose event allows you to cancel the Close command.

The Activate and Deactivate Events

The Activate event is triggered when a drawing window gains focus. When only one drawing is opened in AutoCAD, it will always have focus. When multiple drawings are opened, this event is triggered when switching between drawing windows. The drawing window that loses focus triggers the Deactivate event. Normally, the Deactivate event is triggered just before the Activate event as drawing window focus is switched.

Keep in mind that the Deactivate event indicates the drawing has lost focus. Firing off a procedure as a result of this event might not be a good idea, as it may not complete its task until the drawing regains focus (indicated by another Activate event). You can develop programs to work with what is called a zero document state, meaning there are no drawings opened. Consult the Autodesk developer guide for more information on this topic.

The BeginSave and EndSave Events

Immediately before AutoCAD begins to save the current drawing, the BeginSave event is triggered. Once AutoCAD has completed saving the drawing file, the EndSave event occurs. You might use the BeginSave event to query whether or not the user wants to purge his or her drawing before saving it. You could use the EndSave event to reinitialize standard layers, linetypes, and text styles that may have been purged because they weren't currently being used. As you can see, AutoCAD has provided some very useful events that greatly enhance its controllability.