Compiling AutoLISP Files - Part 1
- See also:
- Compiling AutoLISP Files - Part 2
Before we start with various methods of compiling AutoLisp files, let's have a look at the different file types we'll encounter during this Tutorial :
|LSP||AutoLisp Program Source file|
|DCL||Contains definitions of AutoCAD Dialog Boxes|
|FAS||Compiled AutoLisp Program|
|VLX||Executable Visual Lisp Module|
|PRV||Defines the files and options used to build a vlx module|
Each time you load AutoLISP source code, the code is translated into instructions the computer understands (executable code). The advantage of having source code translated each time you load it is that you can make a change and immediately try it out. This is useful for quickly testing new code, and for debugging that code.
Once you are sure your program is working correctly, translating AutoLISP source code each time it loads is time-consuming. VLISP provides a compiler that generates executable machine code files from your source files. These executable files are known as FAS files. Because the executable files contain only machine-readable code, the source code you spent weeks or months developing remains hidden even if you distribute your program to thousands of users. Even strings and symbol names are encrypted by the VLISP file compiler.
VLISP also provides features for packaging more complex AutoLISP applications into VLISP executable (VLX) files. VLX files can include additional resources files, such as VBA, DCL and TXT files, as well as compiled AutoLISP code.
So, let's try and give you a digest of what we've just discussed :
- If you have a single AutoLisp file, compile it as a FAS file.
- If you have an AutoLisp file with dependencies such as DCL or TXT files, compile it as an Executable Visual Lisp Module VLX file.
Let's start off this Tutorial by compiling a single AutoLisp file.
Copy and paste this into Notepad and save it as "Slot.lsp" :
(defun C:SLOT (/ oldsnap diam lngth pt1 pt2 pt3 pt4 pt5 pt6) (setvar "CMDECHO" 0) (setvar "BLIPMODE" 0) (setq oldsnap (getvar "OSMODE")) (setq diam (getdist "\nSlot Diameter : ") lngth (getdist "\nSlot Length : ")) (while (setq pt1 (getpoint "\nInsertion point: ")) (setvar "OSMODE" 0) (setq pt2 (polar pt1 0.0 (/ (- lngth diam) 2.0)) pt3 (polar pt2 (/ pi 2.0) (/ diam 4.0)) pt4 (polar pt3 pi (- lngth diam)) pt5 (polar pt4 (* pi 1.5) (/ diam 2.0)) pt6 (polar pt5 0.0 (- lngth diam))) (command "PLINE" pt3 "W" (/ diam 2.0) "" pt4 "ARC" pt5 "LINE" pt6 "ARC" "CLOSE") (setvar "OSMODE" oldsnap) );while (princ) );defun (princ)
Now fire up AutoCAD and open the Visual Lisp Editor. Type this at the console prompt :
_$ (vlisp-compile 'st "slot.lsp") T
Have look at the Build Output window :
; (COMPILE-FILES st (D:/drawings/slot.lsp)) [Analyzing file "D:/drawings/slot.lsp"] .. [COMPILING D:/drawings/slot.lsp] ;;C:SLOT ; [FASDUMPING object format -> "D:/drawings/slot.fas"] ; Compilation complete.
During compilation, the compiler prints function names and various messages about each stage of compilation. The first stage is syntax and lexical checking of the source code. If the compiler encounters errors, it issues messages and halts the compilation process. The compiler issues warnings if it encounters expressions it considers dangerous, such as redefining existing AutoLISP functions or assigning new values to protected symbols. If the compiler displays warning or error messages, you can view and edit the source code that caused these messages by double-clicking on the message in the Build Output window.
If compilation is successful, as in the example above, the Build Output window displays the name of the compiled output file. This file should be located in the same directory as your AutoLisp source file. Let's have a look at the syntax for (vlisp-compile) :
Compiles AutoLISP source code into a FAS file
(vlisp-compile 'mode filename [out-filename])
Note: The Visual LISP IDE must be open in order for vlisp-compile to work.
The compiler mode, which can be one of the following symbols :
|st||Standard build mode|
|lsm||Optimize and link indirectly|
|lsa||Optimize and link directly|
A string identifying the AutoLISP source file. If the source file is in the AutoCAD Support File Search Path, you can omit the path when specifying the file name. If you omit the file extension, .lsp is assumed.
A string identifying the compiled output file. If you do not specify an output file, vlisp-compile names the output with the same name as the input file, but replaces the extension with .fas.
Note that if you specify an output file name but do not specify a path name for either the input or the output file, vlisp-compile places the output file in the AutoCAD install directory.
Return Values :
T, if compilation is successful, nil otherwise.
Assuming that slot.lsp resides in a directory that is in the AutoCAD Support File Search Path, the following command compiles this program :
_$ (vlisp-compile 'st "slot.lsp") T
The output file is named slot.fas and resides in the same directory as the source file.
The following command compiles slot.lsp and names the output file Slot-1.fas :
(vlisp-compile 'st "slot.lsp" "slot-1.fas")
Note that the output file from the previous command resides in the AutoCAD install directory, not the directory where slot.lsp resides. The following command compiles slot.lsp and directs the output file to the c:\my documents directory :
(vlisp-compile 'st "slot.lsp" "c:/my documents/slot-1.fas)
This last example identifies the full path of the file to be compiled :
(vlisp-compile 'st "c:/program files/acad2000/Sample/slot.lsp")
The output file from this command is named slot.fas and resides in the same directory as the input file.
In Part 2 we'll be having a look at creating compiled AutoLisp files using multiple AutoLisp files and dependency files such as DCL files.