S::STARTUP Function: Postinitialization Execution (Concept)

AutoCAD

 
S::STARTUP Function: Postinitialization Execution
Concept Quick Reference
 
 
 

You can define an S::STARTUP function to perform any needed setup operations after the drawing is initialized.

The startup LISP files (acad.lsp, acaddoc.lsp, and MNL) are all loaded into memory before the drawing is completely initialized. Typically, this does not pose a problem, unless you want to use the command function, which is not guaranteed to work until after a drawing is initialized.

If the user-defined function S::STARTUP is included in an acad.lsp, acaddoc.lsp, or MNL file, it is called when you enter a new drawing or open an existing drawing. Thus, you can include a definition of S::STARTUP in the LISP startup file to perform any setup operations.

For example, if you want to override the standard HATCH command by adding a message and then switching to the BHATCH command, use an acaddoc.lsp file that contains the following:

(defun C:HATCH ( ) 
 (alert "Using the BHATCH command!")
 (princ "\nEnter OLDHATCH to get to real HATCH command.\n")
 (command "BHATCH")
 (princ)
) 
(defun C:OLDHATCH ( ) 
 (command ".HATCH")
 (princ)
) 
(defun-q S::STARTUP ( ) 
 (command "undefine" "hatch") 
 (princ "\nRedefined HATCH to BHATCH!\n")
)

Before the drawing is initialized, new definitions for HATCH and OLDHATCH are defined with the defun function. After the drawing is initialized, the S::STARTUP function is called and the standard definition of HATCH is undefined.

NoteTo be appended, the S::STARTUP function must have been defined with the defun-q function rather than defun.

Because an S::STARTUP function can be defined in many places (an acad.lsp, acaddoc.lsp, or MNL file or any other AutoLISP file loaded from any of these), it's possible to overwrite a previously defined S::STARTUP function. The following example shows one method of ensuring that your startup function works with other functions.

(defun-q MYSTARTUP ( )

... your startup function ...

)
(setq S::STARTUP (append S::STARTUP MYSTARTUP))

The previous code appends your startup function to that of an existing S::STARTUP function and then redefines the S::STARTUP function to include your startup code. This works properly regardless of the prior existence of an S::STARTUP function.