Guidelines for Event Handlers

AutoCAD ActiveX

 
Guidelines for Event Handlers
 
 
 

It is important to remember that events simply provide information on the state or activities taking place within AutoCAD. Although event handlers can be written to respond to those events, AutoCAD is often in the middle of processing commands when the event handler is triggered. Event handlers, therefore, have some restrictions on what they can do if they are to provide safe operations in conjunction with AutoCAD and its database.

  • Do not rely on the sequence of events.

    When writing event handlers, do not rely on the sequence of events to happen in the exact order you think they occur. For example, if you issue an OPEN command, the events BeginCommand, BeginOpen, EndOpen, and EndCommand will all be triggered. However, they may not occur in that order. The only event sequence you can safely rely on is that a Begin event will occur before the corresponding End event. In the previous example, the events may get triggered in the following order: BeginCommand, BeginOpen, EndCommand, and EndOpen, or even BeginCommand, EndCommand, BeginOpen, and EndOpen.

  • Do not rely on the sequence of operations.

    If you delete object1 and then object2, do not rely on the fact that you will receive the ObjectErased event for object1 and then for object2. You may receive the ObjectErased event for object2 first.

  • Do not attempt any interactive functions from an event handler.

    Attempting to execute interactive functions from within an event handler can cause serious problems, as AutoCAD may still be processing a command at the time the event is triggered. Therefore, you should always avoid the use of input-acquisition methods such as GetPoint, GetEntity, GetKeyword, and so on, as well as selection set operations and the SendCommand method from within event handlers.

  • Do not launch a dialog box from within an event handler.

    Dialog boxes are considered interactive functions and can interfere with the current operation of AutoCAD. Message boxes and alert boxes are not considered interactive and can be issued safely; however issuing a message box within an event handler for the BeginModal, EndModal, Activate, Deactivate, and BeginRightClick events results in unexpected sequencing.

  • You can write data to any object in the database, except the object that issued the event.

    Obviously, any object causing an event to be triggered could still be open for use with AutoCAD and the operation currently in progress. Therefore, avoid writing any information to an object from an event handler for the same object. However, you can safely read information from the object triggering an event. For example, suppose you have a floor that is filled with tiles and you create an event handler attached to the border of the floor. If you change the size of the floor, the event handler will automatically add or subtract tiles to fill the new area. The event handler will be able to read the new area of the border, but it cannot attempt any changes on the border itself.

  • Do not perform any action from an event handler that will trigger the same event.

    If you perform the same action in an event handler that triggers that same event, you will create an infinite loop. For example, you should never attempt to open a drawing from within the BeginOpen event, or AutoCAD will simply continue to open more drawings until the maximum number of open drawings is reached.

  • Remember that no events will be fired while AutoCAD is displaying a modal dialog box.