Defining Callback Functions

AutoCAD AutoLISP & Visual LISP

 
Defining Callback Functions
 
 
 

To add reactor functionality to your application, you first need to write a callback function that performs the tasks needed at the time of the reactor event. After you define a callback function, you link the function to an event by creating a reactor object.

A callback function is a regular AutoLISP function, which you define using defun. However, there are some restrictions on what you can do in a callback function. You cannot call AutoCAD commands using the command function. Also, to access drawing objects, you must use ActiveX® functions; entget and entmod are not allowed inside callback functions. See Reactor Use Guidelines for more information.

Callback functions for all reactors, other than Object reactors, must be defined to accept two arguments:

  • The first argument identifies the Reactor object that called the function.
  • The second argument is a list of parameters set by AutoCAD.

The following example shows a function named saveDrawingInfo, which displays file path and size information. This function will be attached to a DWG Editor reactor that will fire when an AutoCAD drawing is saved.

(defun saveDrawingInfo (calling-reactor commandInfo / dwgname filesize)
    (vl-load-com)
    (setq dwgname (cadr commandInfo)
        filesize (vl-file-size dwgname)
  )
  (alert (strcat "The file size of " dwgname " is "
                 (itoa filesize) " bytes."
          )
  )
  (princ)
)

In this example, the calling-reactor variable identifies the reactor that invoked the function. The function retrieves the drawing name from the commandInfo parameter, then uses the vl-file-size function to retrieve the size of the drawing. Finally, the function displays the information in an alert box in the AutoCAD window.

The parameters passed to a callback function depend on the type of event associated with the function. For example, saveDrawingInfo will be associated with a saveComplete event. This event indicates that a Save command has been completed. For saveComplete events, AutoCAD passes the callback function a string containing the name of the file the drawing was saved in. On the other hand, a callback function that reacts to changes to system variables (sysVarChanged event) receives a parameter list containing the name of a system variable (a string) and a flag indicating if the change was successful. You can find a list of events for each reactor type, and the parameters associated with each event, in the AutoLISP Reference. The events are listed under the description of the functions used to define each type of reactor.

AutoCAD comes with two predefined callback functions. You can use these functions when testing your reactors:

  • vlr-beep-reaction is a simple function that beeps your PC.
  • vlr-trace-reaction prints a list of arguments to the VLISP Trace window each time a reactor fires this callback function.