Vba Foundations, Part 11: Paperspace
Vba Foundations, Part 11: Paperspace
Application Events: These events are directly related to changes in AutoCADs state and the application
environment. These changes include changes to the AutoCAD window itself such as the user minimizing or
maximizing the window. Additional examples include documents being opened, saved, printed, and closed.
Other integrated applications such as an AutoLisp or Arx routine when called, loaded, cancelled, and
unloaded are also tracked as Application Level events. Note: Application Level Events are not automatically
loaded when a VBA routine is run. See the following segment for the steps required to create, load, and
initialize an Application Level Event.
To Load or Enable Application Events:
1.)
2.)
3.)
4.)
Now we need to connect the class to our Application. We can do this from any module. So lets
add the following code to the general declarations area of a new module we will insert into our
VBA Project. The general declarations area is simply the top of the module.
Dim clsEvents As New myEventClass
5.)
Now give your module a meaningful name such as EventClassLoadingModule and add the
following sub routine as shown below:
Sub InitializeEvents()
Set clsEvents.App = ThisDrawing.Application
May/June 2003
PaperSpace
p. 1
6.)
Now to get this class working! Simply call your InitializeEvents subroutine. How? Many
ways, we can put a Public sub in our Module to call it out or in ThisDrawing or on a form or
??? Can you think of other ways?
Public Sub Kick_IT_Off()
Call InitializeEvents
End Sub
7.)
Once this Event controller is initialized, by moving the mouse cursor into the above sub and
pressing the F5 key, you can return to the myEventClass class code window and start
setting up individual handlers for the event triggers that occur related to the Application itself.
Do this by selecting the App object in the Object pulldown and then select the desired trigger
from the available options in the procedures drop down to the right of the object drop down.
This action will result in the creation of skeleton code as shown below
(App_SysVarChanged) ready for you to add code to.
(See graphic Below).
Please be aware that unlike Visual Lisp Reactors, AutoCADs VBA enabled application level events are not
persistent. In other words, they must be enabled for VBA and any other Applications that might wish to make use
of them. Once these application level events have been enabled, you will have a wide range of events available to
you. See the chart below for a list of the application level events available and when they are triggered.
May/June 2003
PaperSpace
p. 2
EndCommand
AppDeactivate
EndLISP
ARXLoaded
EndModal
ARXUnloaded
EndOpen
BeginCommand
EndPlot
BeginFileDrop
EndSave
BeginLISP
LISPCancelled
BeginModal
NewDrawing
BeginOpen
SysVarChanged
BeginPlot
WindowChanged
BeginQuit
WindowMovedOrResized
BeginSave
Document Events: These events are directly tied to the drawings opened by AutoCAD. Specifically, a
document event is triggered when changes occur within or to an AutoCAD drawing (*.dwg), the objects this
drawing contains, or the window it is displayed in. Some of the document level events are duplicates of the
application events allowing you, the programmer, additional control and notification down to the specific file.
Examples of Overlapping Document Events include: Begin and End events for saving, closing, plotting, and
running Lisp routines. Additional Document Level Events include: Document Window changes, Object level
changes, and Shortcut menu notifications. Note: Document Level Events ARE automatically loaded in the
ThisDrawing module, which is contained in every VBA routine by default. We will not look at enabling
Document level events from outside of VBA in this article, but the procedure is well documented in the
online help. If still stumped, please post your question to the VBA guild, or email me directly for the
answer.
May/June 2003
PaperSpace
p. 3
Activate the ThisDrawing class in the VBA editor by double clicking on it in the
project explorer.
2.)
Select the AcadDocument object in the Object pulldown and then select the desired
trigger from the available options in the procedures drop down to the right of the object
drop down. (See Graphic)
3.)
This action will result in the creation of skeleton code to be filled in prior to activating.
Option Explicit
Private Sub AcadDocument_BeginSave(ByVal FileName As
String)
End Sub
Did you notice how as soon as you selected the AcadDocument object in the left
combo box that the skeleton code shown above was inserted into your drawing? Why
was the Begin Save Event chosen above all others? If we sneak a peak into the Type
Library File for AutoCAD we can see that the Begin Save event is simply listed as
the first choice, even though it is not listed first in the selection box in VBA. (See
Graphic Top of Next Page)
Note: Event handlers created using this method apply to the current active drawing.
(TypeLibrary View)
May/June 2003
PaperSpace
p. 4
EndCommand
BeginClose
EndLISP
BeginCommand
EndPlot
EndSave
EndShortcutMenu
BeginLISP
BeginPlot
LayoutSwitched
BeginRightClick
LISPCancelled
BeginSave
ObjectAdded
ObjectErased
ObjectModified
SelectionChanged
WindowChanged
WindowMovedOrResized
BeginDoubleClick
Deactivate
May/June 2003
PaperSpace
p. 5
Object Events: This type of event is triggered when individual object types are modified. Although, Object
Level Events are limited to a single event, modified, they can be declared for virtually any of the 40+ types of
objects used in AutoCAD or any of the vertical products. Note: Object Level Events are not automatically
loaded when a VBA routine is run. They can be initialized in the same manner as the Application Level
Events.
To Enable Object Level Events inside of AutoCAD:
Create a class module and declare an object with events as a particular type of AcadObject. To
actually work this Event, then you must connect to your new class by creating an instance of your Class
and creating an instance of the object then setting your class instance equal to the object. Here is how
you do that for a Circle object:
1.)
May/June 2003
Move your mouse to the Project Browser, select your project with the left mouse button
and then right click while the mouse is directly over the highlighted project. From the
right click menu choose Insert and select Class Module from the pop up menu
shown. (See Graphic Below)
PaperSpace
p. 6
2.)
Now select your new class module in the Project Browser so that it is highlighted then
move your mouse to the Properties Window and rename your class module to
something meaningful such as: MyObjectEventClass. (See Graphic Below)
3.)
With your class module still selected in the Project Browser hit your F7 function key
to switch to the Code Window. Within the code window for the Object Event Class add
the following statement:
Option Explicit
Public WithEvents Object As AcadCircle
4.)
Now insert a module into your project using the same method as shown in step 1 and 2
above (make sure you select a module instead of a class this time). Now rename your
module to something meaningful such as: MyObjectEventModule (See Graphic Below)
5.)
With your Module still selected, switch to the code window and insert the following
code into your Module. The first segment goes at the very top of the code window in the
area known as the General Declarations area.
Option Explicit
Dim oCircle As New MyObjectEventClass
May/June 2003
PaperSpace
p. 7
6.)
7.)
Now BEFORE YOU TRY TO RUN THE PRECEDING CODE switch back to your
class module and add the following Event Handlerfor the Modified event. (See
Graphic Below)
Note: You must provide an event handler for all objects enabled for the Modified event.
If you do not provide a handler, VBA may terminate unexpectedly.
May/June 2003
PaperSpace
p. 8
8.)
Now you can safely run the initialize event to create a circle and connect that
particular circle to your event handler. Now switch to AutoCADs window and zoom in
on your new circle that was just added to your drawing. Select the circle and move it.
Did you see the message box pop up with the new center point? Pretty neat and not a lot of code either?
I have included an event handler for our ever popular Purge-N-Save-N-Close (PSC) macro. Take a look at the
code I have included in the following three listings on the next pages.
This code once activated will add right click functionality to our PSC macro and add a Drawing switcher that you
may find is a little bit easier than switching using the Window pull down method. Watch out for word wrap in
the code listings belowa lot of code coming up right now!!!! If you experience problems copying and pasting
the code in the listings below, send me an email with RTCLICK in the subject and Ill return a working macro
file to you!
Listing 1 From a Class Module named: myEventClass
Option Explicit
Sub InitializeEvents()
Set clsEvents.App = ThisDrawing.Application
End Sub
May/June 2003
PaperSpace
p. 9
May/June 2003
PaperSpace
p.
10
May/June 2003
PaperSpace
p. 11
May/June 2003
PaperSpace
p.
12
End If
End Sub
May/June 2003
PaperSpace
p.
13
May/June 2003
PaperSpace
p.
14
'has VBA 5, then request the VBA 5 version of Split by email or do a google
'search for the function.
Dim varArFile 'Declare Variant to hold array values
Dim intCnt As Long 'Declare counter to determine count of folders/drives
If Not FileName = "" Then
varArFile = Split(FileName, "\", -1, vbTextCompare)
intCnt = UBound(varArFile) - LBound(varArFile)
StripPathFromName = varArFile(intCnt)
End If
End Function
May/June 2003
PaperSpace
p.
15
End Sub
All right, so now we have a better understanding of what an Event is and what types of Events are available, and
weve learned how can we make use of them. For this we turned to our old friend the Sub Routine or
procedure. A subroutine that was written specifically to respond to an event is called an Event Handler. The
example code shown above for the three types of events was Event Handler type of code. These Event Handlers
are executed automatically every time their associated event or trigger is encountered. This works precisely as
one would think. The only other thing we need to know about Event Handlers is that some of the Events, when
triggered, will also pass in parameters to the Event handler. Parameters such as the Command name, passed by
value as a string, are passed into both the AcadDocument_BeginCommand and
AcadDocument_EndCommand event handlers. These parameters are passed in to the associated handlers as a
means to provide additional information about the trigger or event that occurred.
In order to use Events in a controlled and safe manner, we must follow some recommendations and rules.
Because Event handlers are notifications in real time of events or triggers that occur in the application or
document, they are often triggered in the middle of another command or activity so care must be exercised in how
we respond to them. The following rules are provided for your use:
1. Do not rely on the sequence of events. Associated Begin and End triggers will follow each other in the
correct order, however, performing a Save command may or may not call the BeginCommand
before the BeginSave event. Likewise, the EndCommand and EndSave events are not guaranteed
to follow each other in any consistent manner. You can rely on the fact that a BeginSave will always
May/June 2003
PaperSpace
p.
16
May/June 2003
PaperSpace
p. 17