Search code examples
autocadautocad-pluginautolisp

Running a function when the routine is loaded through appload (autoLISP)


What I'm trying to do is pretty simple, I have a routine and I want that, when someone loads it into autocad, a pop up appears on the screen with a little explaining of what it does. I know how to do the popup, but I have no idea on how to make it run specifically when the routine is loaded, any suggestions ?


Solution

  • This is actually very easily achieved: in short, you would simply include an alert expression outside of any defun expression within the AutoLISP file, such that the alert expression is evaluated immediately when the content of the AutoLISP file is evaluated on load.

    For example:

    (defun c:test ( )
        (princ "\nThis is the main function.")
        (princ)
    )
    
    (alert "Type \"test\" to run the main function.") ;; This will be displayed on load
    (princ)
    

    When the above AutoLISP file is loaded, the interpreter will read the content of the AutoLISP file and will immediately evaluate all AutoLISP expressions contained therein.

    As a result, the defun expression will be evaluated first and will define a function c:test which may then be executed at the AutoCAD command line as a result of the c: prefix.

    The alert expression will then be evaluated and will display a message box to the user, as desired.

    Finally, the closing (princ) expression will be evaluated and will return a null symbol to the command line so as to achieve a 'clean load'. If the final (princ) expression were to be omitted, the alert expression would return a value of nil to the command line.