Making Functions Available to Documents

AutoCAD AutoLISP & Visual LISP

 
Making Functions Available to Documents
 
 
 

By default, functions defined in a separate-namespace VLX are not exposed to the document namespace from which the VLX is loaded. You must use the vl-doc-export function to expose functions to document namespaces. When issued from a VLX that runs in its own namespace, vl-doc-export exposes the specified function to any document namespace that loads the VLX. The vl-doc-export function accepts a single argument, a symbol identifying the function name. For example, look at the following code:

(vl-doc-export 'kertrats)
(defun kertrats ()
  (princ "This function goes nowhere") 
)

This example defines the kertrats function, which simply prints a message. The defun for the function is preceded by a vl-doc-export call that causes the function to be exported to the document namespace.

To see how vl-doc-export works in a separate-namespace VLX

  1. In a VLISP text editor window, copy the following code into a file:
    (defun kertrats ()
      (princ "This function goes nowhere") 
    )

    Note that this code does not contain a call to vl-doc-export.

  2. Save the file you just created.
  3. Use the VLISP Make Application wizard to build a VLX from your program file. Specify the following wizard options:
    • Wizard mode: Expert
    • Application name: doctest
    • Application options: Separate-namespace
    • Compilation options: Optimize
  4. From either the AutoCAD Command prompt or the VLISP Console window prompt, load the doctest VLX file.
  5. Try running the kertrats function.

    You should receive an error message indicating the function is not defined.

  6. Add the following line of code to the beginning of your program file:
    (vl-doc-export 'kertrats)
  7. Save the file, then rebuild the application.
  8. Use vl-unload-vlx to unload the VLX, then load and run the VLX again. This time, kertrats should run successfully.

You can issue a vl-doc-export call outside the context of a separate-namespace VLX application, but it has no effect.

The vl-list-loaded-vlx function returns a list of all separate-namespace applications associated with the current document. For example:

_$ (vl-list-loaded-vlx)
(DOCTEST)

To determine what functions have been exported from a separate-namespace application into the current document, use vl-list-exported-functions. When calling this function, you must pass it a string naming the application you are checking. For example, the following command returns a list of the functions exported by the doctest application:

_$ (vl-list-exported-functions "doctest")
("KERTRATS")

The results show that a single function, kertrats, was exported from doctest to the current document's namespace.

NoteCurrently, if separate namespace VLX A associated with document A loads separate namespace VLX B, then all of VLX B's exported functions are automatically defined in document A. Note also that VLX B's exported functions are not defined in VLX A until VLX A issues an explicit import. (See Making Separate-Namespace Functions Available to Other VLX Applications.)