Modularizing Your Code

AutoCAD Visual LISP

 
Modularizing Your Code
 
 
 

As a result of the work you did in Lesson 3, your gpmain.lsp file was getting rather large. This is not a problem for VLISP, but it is easier to maintain the code if you split things up into files containing logically related functions. It's also easier to debug your code. For example, if you have a single file with 150 functions, a single missing parenthesis can be difficult to find.

In the tutorial, the files will be organized as follows:

Tutorial file organization

File name

Contents

gp-io.lsp

All input and output (I/O) functions) such as getting user input. Also contains the AutoLISP code required for the dialog box interface you will be adding.

utils.lsp

Includes all generic functions that can be used again on other projects. Also contains load-time initializations.

gpdraw.lsp

All drawing routines—the code that actually creates the AutoCAD entities.

gpmain.lsp

The basic C:GPath function.

To split gpmain.lsp into four files

  1. Create a new file, then cut and paste the following functions from gpmain.lsp into the new file:
    • gp:getPointInput
    • gp:getDialogInput

    Save the new file in your working directory as gp-io.lsp.

  2. Create a new file, then cut and paste the following functions from gpmain.lsp into the new file:
    • Degrees->Radians
    • 3Dpoint->2Dpoint
    • gp:list->variantArray

    Also, at the beginning of the file, insert the lines of code to establish ActiveX functionality (vl-load-com) and commit global variable assignment (*ModelSpace*).

    Save the file as utils.lsp.

  3. Create a new file, then cut and paste the following function from gpmain.lsp into the new file:
    • gp:drawOutline

    Save this file as gpdraw.lsp.

  4. After stripping the code out of gpmain.lsp, save it and check it. Only the original function, C:GPath, should remain in the file.

Your VLISP desktop is starting to get crowded. You can minimize any window within VLISP and it stays accessible. Choose the Select Window button on the toolbar to choose a window from a list, or choose Window from the VLISP menu and select a window to view.