CH 3
CH 3
6
he Visual LISP environment was introduced into AutoCAD 2000 to fa-
shows the Visual LISP for AutoCAD window that appears when the user
enters the VLISP command on the command line. Another form of this command
is VLIDE. The same window is displayed if you choose Visual LISP Editor from
The Visual LISP for AutoCAD window is an entire shell within AutoCAD, with its
own menus and toolbars. The toolbars can be docked into fixed positions (Fig. 3.1)
The working area contains windows for opening text files, the Visual LISP Console
for connection with AutoCAD, and the Trace window for program debugging. The
types of editable files are: LISP program files, dialog boxes DCL files, database
Fig. 3.2. Floating toolbars in the Visual LISP for AutoCAD window
context.
items:
r Save As r Print
228 AutoCAD Developer's Guide to Visual LISP
The name of an item implies its purpose. The Revert item lets you restore the
contents of the file in the active window, just like the Reopen item. The Make
Application item is discussed in Section 3.4.3.
The first 7 items of the menu are typical for a Windows 95/98 application. The
The first 2 operations find in the text the parenthesis (either closing or opening)
that matches the one immediately after or before the cursor. The last 2 operations
perform similar tasks, but highlight the text inside the parentheses as well.
r Find r Bookmarks
Let's talk about the items that are not quite as self-evident. After choosing the
Complete Word by Match item, the Visual LISP editor finishes an already-begun
word using as a guide the last word found in the current window of the program
being edited that has a similar beginning. Complete Word by Apropos offers word
endings from the AutoLISP symbol list, which includes the built-in function
Bookmarks are useful for navigation through the text. To set a bookmark, to re-
move it, and to move to another bookmark, use the Toggle Bookmark, Next
Bookmark, Previous Bookmark, and Clear All Bookmarks operations from the
Bookmarks item. The Select to Next Bookmark and Select to Previous Bookmark
operations of the same item ease the task of selecting large portions of text.
program runs, and for work with toolbars of the Visual LISP for AutoCAD window.
r Apropos Window
The Inspect item allows you to call special Inspect windows, which display the
The name of an object to inspect (or an expression) should be selected in the pro-
gram text beforehand. If nothing has been selected, a name is requested before
The Trace Stack item displays a window of the same name. The last operations
r Copy to Trace/Log copies the text to a special Trace window, which, like
If you select a line in the Trace stack window, right-clicking on it will get you a
context menu with additional operations that can be performed on the trace stack
elements: Inspect, Print, Function Symbol, Copy, Local Variables, Source Posi-
tion, and Call Point Source (the context menu size can vary).
The Error Trace item in the View menu displays a window of the same name
After selecting a line in the Error trace window, a context menu for getting addi-
context menu includes the same items as the one in the Trace stack window. Fig.
The Symbol Service item of the View menu opens a window of the same name
(Fig. 3.12). The window functions similar to the Inspect one, but for AutoLISP symbols.
There are 2 fields in the window, Name and Value, as well as the Flags group of
checkboxes. The fields names are self-explanatory (symbol name and value), and
the Flags group consists of 4 boxes that can be checked (or unchecked) for the
selected symbol :
r Trace
r Protect Assign (protects special symbols from random assignments, for in-
ObjectARX applications)
Chapter 3: The Visual LISP Environment 233
The Symbol Service window has 4 buttons under its title bar (left to right):
r Show Definition calls a text editor window, and highlights in it the text of
r Help calls Visual LISP Help for a symbol that is a built-in function
The Watch Window item of the View menu displays the Watch window (Fig. 3.13).
This window displays the values of selected symbols while the program is being
executed. Symbols can be added to this window either with the Watch button of
the Symbol Service window, or with the Add Watch button in the Watch window.
The Apropos Window item of the View menu displays on screen the Apropos
options dialog box (Fig. 3.14).
The window lets you view the list of protected symbols (primarily, the built-in
function names). The list of names to display is formed basing on the sequence
of letters entered in the upper field (in Fig. 3.14, the only letter f is entered); the
letters must appear in all the symbol names on the list. The user can indicate with
checkbox whether the names must start with the sequence or just contain it, and
(see Section 2.8.8 ). Fig. 3.15 depicts the Apropos results window with symbols that have
the prefix F.
The Breakpoints Window item of the View menu opens the Breakpoints dialog box
The Output Window item opens the <Build Output> window for output of mes-
The LISP Console item of the View menu invokes the very important Visual LISP
Console window (Fig. 3.18). In Visual LISP, this window is analogous to the
to load and execute functions, and to retrieve values of variables. This is also where
The Browse Drawing Database item of the View menu includes the sub-
items: Browse All Entities, Browse Tables, Browse Blocks, Browse Selection, and
Inquire Extended Data; when selected, they call the respective windows
Chapter 3: The Visual LISP Environment 235
The Toolbars item of the View menu calls the Toolbars dialog box (Fig. 3.20),
The checkboxes in this window should be checked for any of the 5 toolbars
Changes can be made using the Hide All, Show All, and Apply buttons. The Close
button closes the Toolbars dialog box.
A project is a group of LISP programs combined into one application. That appli-
cation runs in the AutoCAD environment and is compiled into one file with the
FAS extension (there can be a FAS file created for each of the programs, although
These items are discussed in Section 3.4.2. The last lines in the menu show the
ronment.
These items along with their corresponding operations are covered in Section 3.4.1.
If the Visual LISP Console window is active in the Visual LISP environment, then
The AutoCAD Mode item turns the mode that uses the Visual LISP Console win-
This gives you the ability to enter AutoCAD commands in the console window.
If the mode is set to on, this menu item will have a checkmark to its left.
Syntax Coloring is only available if there is a file with an extension other than LSP
in the active window. The Color Style dialog box is called (Fig. 3.24 shows
this dialog box for a DCL file), which allows you to choose one of the predefined
text editor color styles. We shall discuss text editor color styles in more detail in
Section 3.3.1.
Configure Current opens the Window Attributes dialog box (Fig. 3.25), where you
can correct the color settings for text elements. The elements are listed in a drop-
Set Current Window to Prototype changes the active window settings to the pro-
totype ones; Set All Windows to Prototype performs the same operation on all
windows.
Fonts invokes the standard Font dialog box, offering a choice of fonts to be used in
r General Options
The first, General Options, calls the General Options dialog box, which controls
Visual LISP environment settings. The dialog box has 2 tabs: General (Fig. 3.27)
The second point of the Environment Options item , Visual LISP Format Options,
opens the Format options dialog box (Fig. 3.29) for Visual LISP text editor format
settings.
242 AutoCAD Developer's Guide to Visual LISP
Page Setup Options, the third point of the Environment Options item, opens the
Page Setup dialog box for printing Visual LISP texts (Fig. 3.30).
Chapter 3: The Visual LISP Environment 243
The Save Settings item of the Tools menu saves the Visual LISP desktop settings
(the windows last opened, window settings, etc.). To allow for automatic saving,
check the appropriate checkboxes from the DESKTOP group in the General
Options dialog box, General tab (Fig. 3.27).
If the active window of the Visual LISP environment is one that contains a pro-
gram text, the Tools menu will contain these extra items (Fig. 3.31):
The first 6 of these allow you to load for execution, to check, and to format either a pro-
gram text or a selected portion of one. More details are given in Sections 3.3.2 and 3.4.1.
The Interface Tools item includes:
These two load an edited DCL file or a selected portion of one for execution.
The Activate AutoCAD item minimizes the Visual LISP for AutoCAD window; the
main AutoCAD window becomes available temporarily (for instance, for drafting
r Search r Debug
As you can see, the names of the toolbars are similar to the names of the menus
r Print r Undo
The first 9 buttons are the same as the respective AutoCAD Standard toolbar but-
tons. The last, the Complete word button, corresponds to the Complete Word by
Apropos item of the Search menu; it allows you to complete a word you have be-
gun to type with appropriate matches from the standard symbols list.
r Toggle bookmark
246 AutoCAD Developer's Guide to Visual LISP
in the Search menu. The Find toolbar string button co-operates with a drop-down
list found in the Search toolbar; it searches for a symbol whose name has been
The first 6 buttons do the same as the first 6 Tools menu items when the user edits
The Comment block and Uncomment block buttons allow you to easily make or
Besides, one more button appears on the toolbar when you run a program in the
debug mode with breakpoints (this button is shown in Fig. 3.37, but in normal
mode it is not visible). The button indicates where the cursor is during the current
As the button names indicate, these are the same operations we encountered when
talking about the pull-down menus: AutoCAD Mode from the Tools menu;
choosing a window using the windows list from the Window menu; LISP Console,
Inspect, Trace Stack, Symbol Service, Apropos Window, and Watch Window from
following types in a separate window using the New File and Open File items of
The text editor included in the environment essentially facilitates writing files of
these types. From the very first input of text, it checks the syntax, indicating pa-
rentheses, operators, operands, comments, etc., in a different color font. The parse
type) using the Syntax Coloring option of the Window Attributes item from the
Tools menu (Fig. 3.24). By default, the editor is set to work with LISP program
texts.
The colors normally applied for LISP program parsing are the following:
Fig. 3.39 shows a text editor window with a portion of a LISP source code colored
You can change the colors to fit your preferences with the Configure Current op-
tion of the Window Attributes item in the Tools menu (see Fig. 3.25).
The Environment Options item of the Tools menu (see Section 3.1.6 ) can also help
text. To do so, drag the mouse over the text with its left button pressed, or use the
The Comment block and Uncomment block buttons of the Tools toolbar are ex-
The text of a whole file or a selected portion of it can be checked for syntax errors
using the Check Selection or Check Text in Editor items of the Tools menu, or the
The Parentheses Matching item of the Edit menu is good for checking to make
When saving an edited LSP file, Visual LISP reserves a backup copy under the
grams. In Section 3.4.2, the project tools that allows you to create applications
to contain dialog box DCL files or auxiliary text files, use a tool for complex ap-
plication design (VLX applications, see Section 3.4.3 ). Means of debugging are
the same role for the user as the AutoCAD command window. In the Console
250 AutoCAD Developer's Guide to Visual LISP
window, you can enter AutoLISP expressions and load and execute functions. Vis-
ual LISP and AutoCAD messages are displayed in this window too.
If you have an open LISP program file in the text editor current window, you can
load it (just as you would load a file with the AutoLISP load function) by selecting
the Load Text in Editor item of the Tools menu, or by pressing the Load active
edit window button of the Tools toolbar. Shown in Fig. 3.40 is a file load message
A program can be run in the step mode, with the user selecting and loading text
fragments one by one. To do so, use the Load Selection item from the Tools menu
The Visual LISP Console window user prompt is _$ . So, if, for example, you have
loaded a rcl.lsp file with a program of the same name in the previous step, you can
execute it by entering
(rcl)
in the console window.
Visual LISP will execute the program, and if it contains no errors, will display the
Global variable values can be read from the console directly. If you type a variable
name at the console prompt, Visual LISP will indicate its current value (Fig. 3.42
NOTE
To read a variable value in Visual LISP, unlike in AutoCAD, you dont have to precede
Fig. 3.40. A file load message in the Visual LISP Console window
Chapter 3: The Visual LISP Environment 251
Fig. 3.41. A file execution message in the Visual LISP Console window
Fig. 3.42. Reading a global variable value in the Visual LISP Console window
When debugging a program, its convenient to trace the values of selected vari-
ables. The Inspect, Symbol Service, and Watch Window items we talked about
when discussing the View menu show the corresponding windows (see Figs. 3.8,
3.12, and 3.13), where the current values of selected variables or expressions are
indicated. The contents of these windows can be copied to the Trace window for
subsequent summarizing.
In case of errors, use the Trace Stack and Error Trace items, which display win-
use of the Toggle breakpoint item of the Debug menu, or the button of the same
name from the Debug toolbar. Upon reaching a breakpoint, the program pauses
for user directions for further execution. To continue, use the Step Into, Step
Over, Step Out, Continue, Reset to Top Level, or Quit Current Level item of the
Combining breakpoints with an open Watch window (Fig. 3.13) is particularly ef-
fective. Executing your program portion by portion, you can keep an eye on the
monitored variables.
252 AutoCAD Developer's Guide to Visual LISP
The Animation item of the Debug menu allows you to trace program execution
along the source text; Visual LISP will highlight the program expressions being
3.4.2. Projects
A number of jointly executed LISP programs can be grouped into a project. Every
such project is an independent application; it can work in the form of source texts
or in the form of compiled files (with the FAS extension). Besides, all the files of
a project can be compiled into one FAS application file, which is preferable con-
Project operations are grouped in the Project menu. A new project can be created
using the New Project item, which opens the New project... window (Fig. 3.43).
The name for the project data file should be entered in this window. The file is
lation modes. The project file extension is PRJ. After the project name is entered,
This dialog box has 2 tabs: Project Files and Build Options. In the first one,
choose the LSP files folder for the Look in field; from the list of found files that
appears, transfer the names of the necessary files to the right side using the > but-
ton. If the project files are collected in more than one folder, you can change the
a file name produces a context menu, whose Log filenames and size item displays
in the lower part of the dialog box the full name and the size of each selected file
(Fig. 3.45).
When a complete list of the new project components is formed, it can be arranged
with the use of the Top, Up, Down, and Bottom buttons. Normally, such a rear-
are special initialization files, they must be put on the top in order to be loaded
first).
The Build Options tab of the Project properties dialog box (Fig. 3.46) defines the
modes for compiling and linking. In particular, the Merge files mode determines
whether every LSP file will result in a FAS file, or whether the whole project will
be compiled into one file (a FAS application). The folders for storing FAS files
When you have finished, click the Apply button. The OK button closes the dialog
box, and the window of the opened project appears on the screen. Fig. 3.47 con-
Fig. 3.44. The Project properties dialog box, Project Files tab
254 AutoCAD Developer's Guide to Visual LISP
Fig. 3.46. The Project properties dialog box, Build Options tab
Chapter 3: The Visual LISP Environment 255
There are 5 project operation buttons in the upper part of the window (left to
right):
The button names are analogous to the Project menu item names. Formerly cre-
ated projects can be modified, and loaded either already compiled or as source
code.
r LSP files
r Dialog box DCL files (for dialog box design, see Chapter 5)
r DVB files (applications created in the Visual Basic for Applications environ-
ment)
All included components are grouped into one application file with the VLX ex-
tension. The application properties are kept in the file of the same name with the
To create a new complex application, use the Make Application item of the File
menu. The item includes:
To create a new application, choose New Application Wizard, which opens the
The mode determining the application type, Simple or Expert, should be set in
this window. A simple application can consist of LISP program files only (resem-
bling a FAS application); an expert one can include DCL files, DVB files, etc.
Expert option and click the Next button, the Application Directory dialog box is
The Application Name field should be filled in here (the Target File panel will form
the new application name with the VLX extension), and in the Application Location
field you should put the full name of the folder where the application file (with the
extension VLX) and the application property file (extension PRV the base of the
file name is inherited from the application name) will reside. The folder can be also
chosen using the Browse button. After the parameters are set and the Next button is
By default, the Separate Namespace checkbox is not checked in this dialog box.
This means that the functions and global variables of your application will be avail-
able for other applications. If our application had nothing to do with the other
NOTE
Although a VLX application is created with a separate namespace, it is still possible to
export its functions to a document namespace with the use of the vl-doc-export func-
tion; its global variables can be exported with the vl-doc-set function. There is also
the possibility of function and variable import from the document into a VLX application
namespace (see Section 2.17.32).
If the Separate Namespace checkbox is checked, the user has access to the
Another dialog box, LISP Files to Include (Fig. 3.51), is for describing all files
with extensions LSP (source code), FAS (compiled files), and PRV (project files)
The Add and Remove buttons let you add and remove components. The order of
including LISP program elements in the application is controlled by the Top, Up,
Down, and Bottom buttons. There is a drop-down list of 3 lines: LISP source files,
Compiled LISP files, and Visual LISP project files, which assists in adding com-
The next dialog box is Resource Files to Include (Fig. 3.52); it will not appear
Elements from the drop-down list can be added to the application as supplementary
resources (the first 3 types could have been selected in the previous dialog box):
DCL files are dialog box files, the structure of which is discussed in Chapter 5. DVB
files are application files created in the Visual Basic for Applications language (the
design of these applications is a subject for a separate book). Text files are any text
files with the TXT extension where the user keeps subsidiary information that the
After defining all supplementary resources we proceed to the next window the
In this dialog box we have to choose either the Standard or the Optimize and Link
compilation mode. Optimization decreases the amount of object code and applica-
tion load time; however, improper use of protected symbols can cause the applica-
The Review Selections / Build Application dialog box (Fig. 3.54) is the last one the
If in this dialog box we leave the Build Application checkbox checked and click
the Finish button, Visual LISP will create an application file (extension VLX)
260 AutoCAD Developer's Guide to Visual LISP
this file later, choose Make Application from the same item of the File menu.
Existing Application Properties of the Make Application item lets you review and
the same item lets you recompile the application after some of its components
console with the use of the load AutoLISP function, or the Load File item from
LISP environment, use either the load function, or Load from the AutoLISP item
of the Tools AutoCAD menu, or the Load Application item of the same menu,
which will all open the Load/Unload Applications dialog box (Fig. 3.55).
when the drawing is opened, add the corresponding AutoLISP line in the acad.lsp
(load rcl.vlx)
Chapter 3: The Visual LISP Environment 261
Your rcl.vlx application will be loaded when loading the file you modified (acad.lsp
(drawings are AutoCAD documents), and the acad.lsp file is loaded on AutoCAD
startup. To provide access for AutoCAD to your acad.lsp and acaddoc.lsp files,
include their prefixes in the Support File Search Path (see Section 1.3 ), or place
the files in the AutoCAD working directory that is defined in the settings of the
If you have to have your application both auto-loaded and auto-run, add one more
line calling your applications main function to the acad.lsp or acaddoc.lsp file, for
example:
(rcl)
Another way of automatically loading is to use the autoload function (see
Section 2.17 ).
262 AutoCAD Developer's Guide to Visual LISP
The most convenient auto-load method is to enter the application names in a spe-
cial portfolio. In the right lower part (Fig. 3.55) of the Load/Unload Applications
dialog box youll find Startup Suite, which could be called an auto-load portfolio.
Clicking the Contents button, the Startup Suite dialog box opens (Fig. 3.56).
Using the Add button in this dialog box, you can add to the startup list the names
of application files that must auto-run at AutoCAD startup. The Remove button