Understanding Namespaces
 
 
 

The concept of namespaces was introduced to prevent applications running in one drawing window from unintentionally affecting applications running in other windows. A namespace is a LISP environment containing a set of symbols (for example, variables and functions). Each open AutoCAD drawing document has its own namespace. Variables and functions defined in one document namespace are isolated from variables and functions defined in other namespaces.

You can see how this works by trying a simple example.

To see the effect of multiple namespaces

  1. Open two new drawings in AutoCAD.
  2. Choose Window Tile Vertically from the AutoCAD menu. You should see two open document windows side by side within the main AutoCAD window:

    The document's title bar indicates which window is currently active. In the preceding example, Drawing1.dwg is the current document.

  3. Enter the following at the Command prompt:
     (setq draw1foo "I am drawing 1")
    

    This sets the draw1foo variable to a string.

  4. Activate Drawing2.dwg (click in the window's title bar).
  5. See if draw1foo contains the value you just set for it:
    Command: !draw1foo
    
    nil
    

    The variable is nil because it has not been set in this document's namespace; you set it in the namespace belonging to Drawing1.dwg.

  6. Enter the following at the Command prompt:
     (setq draw2foo "I too am a drawing, but number 2")
    

    This sets the draw2foo variable to a string.

  7. Activate Drawing1.dwg.
  8. Test the values of variables draw1foo and draw2foo:
    Command: !draw1foo
    
    "I am drawing 1"
    
    Command: !draw2foo
    
    nil
    

    The draw1foo variable contains the value you set for it, but draw2foo is nil because you did not set it to a value in the current namespace; you set a different variable of the same name in Drawing2.dwg's namespace.

VLISP provides ways for you to share variables between namespaces, but you must take explicit action to do so. (See Sharing Data Between Namespaces.)

Like variables, functions defined in an AutoLISP file are known only to the document that was active when the file was loaded. The functions in the file are loaded in the current document's namespace and are known only to that document.

To see how functions are affected by multiple namespaces

  1. Load a LISP file from either the AutoCAD Command prompt or the VLISP Console prompt. For example:
    (load "yinyang.lsp")
    
  2. Invoke the function.
  3. Open a second drawing window.
  4. With the second drawing window active, try invoking the function again. The response will be an error message saying the function is not defined.

You can use the vl-load-all function to load the contents of an AutoLISP file into all AutoCAD drawing documents. For example, the following command causes the contents of the yinyang.lsp file to be loaded into all open documents, and into any documents opened later in the AutoCAD session:

(vl-load-all "yinyang.lsp")

The vl-load-all function is useful for testing new functions in multiple documents, but in general you should use acaddoc.lsp to load files that are needed in every AutoCAD document.