Responding to AutoCAD Messages

There are four categories of messages that AutoCAD sends to ObjectARX applications:

The following five tables describe the messages that AutoCAD sends to ObjectARX applications. The first table lists messages sent to all applications.

Messages sent to all applications

Message

Description

kInitAppMsg

Sent when the ObjectARX application is loaded to open communications between AutoCAD and the application.

kUnloadAppMsg

Sent when the ObjectARX application is unloaded (either when the user unloads the application or when AutoCAD itself is terminated). Closes files and performs cleanup operations.

kLoadDwgMsg

Sent once when the drawing is opened. Then, if the application registers any functions with AutoLISP, AutoCAD sends this message once for each drawing loaded into the editor. The AutoCAD editor is fully initialized at this point, and all global functions are available.

However, you cannot use an acedCommandS()/acedCommandC() function from a kLoadDwgMsg.

kSaveMsg

Sent when AutoCAD is saving the drawing because a SAVE, SAVEAS, NEW, or OPEN command is entered.

kUnloadDwgMsg

Sent (in the reverse order of load time) when the user quits a drawing session.

kPreQuitMsg

Sent when AutoCAD quits, but before it begins to unload all ObjectARX applications.

The next table lists messages that AutoCAD sends to applications that have registered an AutoLISP function with acedDefun():

Messages sent only if the application has registered an AutoLISP function

Message

Description

kInvkSubrMsg

Sent to invoke functions registered using acedDefun().

kEndMsg

Sent only when the END command is entered and there are changes that need to be saved (when dbmod != 0). kEndMsg is not sent for a NEW or OPEN, instead, kSaveMsg and kLoadDwgMsg are sent. For END, if dbmod = 0, then kQuitMsg is sent instead of kEndMsg.

kQuitMsg

Sent when AutoCAD quits (ends without saving) the drawing because a QUIT command was entered. The kQuitMsg can also be received with the END command, as noted above. If the END command is sent and dbmod = 0, then kQuitMsg is sent.

Note: kQuitMsg can be sent before kPreQuitMsg.

kCfgMsg

Sent when AutoCAD returns from the configuration program, and used only for a change to the display driver.

The next table lists the messages that an application receives if it has registered a service with ObjectARX.

Messages only received by applications that have registered a service

Message

Description

kDependencyMsg

Sent when the ObjectARX application has registered an AcRxService object and the dependency count on that service changes from 0 to 1.

kNoDependencyMsg

Sent when the ObjectARX application has registered an AcRxService object and the dependency count on that service changes from 1 to 0.

The next table lists the messages that an application needs to respond to if it is using ActiveX Automation on Windows. See COM and ActiveX Automation.

Messages only responded to by applications that use ActiveX Automation (Windows only)

Message

Description

kOleUnloadAppMsg

Sent to determine if the application can be unloaded (that is, none of its ActiveX objects or interfaces are being referenced by other applications).

See the rxdefs.h file where these enumeration constants are defined by the AppMsgCode type declaration.

You will need to decide which messages your ObjectARX application will respond to. The following table describes recommended actions upon receipt of a given message.

ObjectARX application reactions to AutoCAD messages

Message

Recommended Actions

kInitAppMsg

Do register services, classes, AcEd commands and reactors, and AcRxDynamicLinker reactors. Initialize application's system resources, such as devices and windows. Perform all one-time early initialization. AcRx, AcEd, and AcGe are all active. Store the value of the pkt parameter if you want to unlock and relock your application.

Don't expect device drivers to be initialized, any user interface resources to be active, applications to be loaded in a particular order, AutoLISP to be present, or any databases to be open. Calls involving any of these assumptions will result in an error condition, sometimes fatal. AcDb and AcGi libraries are generally not yet active, although related AcRx and other structures are in place.

kUnloadAppMsg

Do perform final system resource cleanup. Anything started or created in kInitAppMsg should now be stopped or destroyed.

Don't expect things to be any different from the description of kInitAppMsg. AutoCAD could be mostly dismantled by the time this call is made, except for the libraries listed as active in the kInitAppMsg Do description.

kOleUnloadAppMsg

This message should be responded to only by applications using ActiveX Automation on Windows.

Do respond with AcRx::kRetOK, if the application can be unloaded (none of its ActiveX objects or interfaces are being referenced by other applications). If it cannot be unloaded, respond with AcRx::kRetError.

kLoadDwgMsg

Do perform initialization relevant to the current drawing edit session. AcDb, AcGi, and the user interface API are all now active. Whether anything has been done to the drawing is not specified. All AutoCAD-supplied APIs are now active. You can perform AutoLISP function registration at this time, and initialize the user interface. Other operations to perform now include polling AutoCAD drivers and querying AcEditorReactor events if you want the earliest possible access to acdbHostApplicationServices()->workingDatabase().

Don't do anything you would not want to happen for every drawing edit session. Assume this message is sent more than once per program execution.

kUnloadDwgMsg

Do release or clean up everything started or registered in response to kLoadDwgMsg code. Release all AcDb reactors, excluding persistent reactors.

Don't release system resources that are not tied to an edit session, or clean up AcRx classes, AcEd reactors, or commands; they remain valid across edit sessions.

kDependencyMsg

Do perform any actions that are necessary for your application when other applications become dependent on it, such as locking your application so that it cannot be unloaded.

kNoDependencyMsg

Do perform any actions that are necessary for your application when there are no longer any other applications dependent on yours, such as unlocking your application so that it can be unloaded by the user if desired.

kInvkSubrMsg

Do invoke the functions registered with acedDefun(). Determine the function by making a call to acedGetFuncode(). Return values with acedRetxxx().

Don't do much here except function invocation.

kPreQuitMsg

Do unload any dependencies (applications, DLLs, and so on) that your application controls to ensure that they are unloaded before your application.

kEndMsg

kCfgMsg

kQuitMsg

kSaveMsg

Do consider using the AcEditorReactor event callbacks as an alternative to responding to these messages.

Don't respond to these messages if you're responding to the equivalent event callbacks made through AcEditorReactor.