Free Tutorials on AutoCAD: AutoCAD Customization using Autolisp

Page content

Here I will discuss about some of the advanced topics of autolisp programming.

Loops: In order to perform repetitive tasks loop statements like while and repeat are being used.

While: syntax of while is as below:

(while condition operation to be performed)


(setq p 1)

(while (p<5)

(alert “value of p is still less”)

(setq p (+ p 1))

It will pop up a massage four times stating the massage “value of p is still less”

repeat: this function repeats the statements just below it by the desired number of times.

Syntax: (repeat integer)

Example: (repeat 20

(princ r))

It will print the alphabet r 20 times in command window.

If: The syntax is as follows:


Then (do this)

Else (do this)


Example: (defun c:testif ()

(setq a (getreal “\nEnter a Number : “)

b (getreal “\nEnter Second Number : “)


(if (= a b)

(prompt “\nBoth Numbers are equal”)

(prompt “\nBoth numbers are not equal”)





Another method of loading auto lisp file to AutoCAD

Write all the autolisp functions to a separate text file and save it by the name of acad.lsp. Now go to AutoCAD and follow the path:

Tools>options>files>support file search path >click the add button

And give the location of the file acad.lsp. Now next time onward whenever you will start AutoCAD the acad.lsp will be automatically loaded to AutoCAD and you can use the functions written in that.

Locating files: There are two ways one can locate a file using autolisp:

Findfile: This option will search for the file which is in the current autocad search path, if full path of the file is not specified.

Syntax: (findfile “filename”)

Example: (findfile “ACAD2000.LSP”)

Will return “C:\\Program Files\\ACAD2000\\support\\ACAD2000.LSP”

Getfiled: This option will create a popup window foe selecting the required file.

Syntax: (getfiled “pop up window title” “default file location path” “default file extension” Flag)

If default file location path is specified then the pop up window will open with this path. This field can be NULL.

If you specify the default file extension then you will be able to select only that type of file, if it is NULL then any types of file could be selected.

One can further customize the pop up window by setting the flag value to 1,4,3 and 8 or any combinations of these numbers.

Opening and writing files: For opening external files through autocad we have to use the open function.

Syntax of open: (open “filename” “mode”)

We have three modes for open function here:

For reading: use r

For writing: use w

For appending: use w

Let’s see an example:

(setq f(open “aaa.txt” “w”)

Autolisp should return something like #<file “aaa1.txt">.

(write-line “I love u” f)

Autolisp will write the above line to that file “aaa.txt”.

Now you should close the file as below:

(close f)

Important note: Autocad always try to open and write file from the autocad installation directory (e.g. “C:\Program Files\ACAD2000”). So if you want some file from other location you should first find and assign that file from that location or else autocad will create a file in its instillation directory. We will discuss an example regarding this in my next part of the article.)

This post is part of the series: Autocad customization

This is the series of articles that describe AutoCAD cutomization using the tools like Autolisp, Visual lisp ,VB,C++ used for customization of Autocad.

  1. AutoCAD Customization using Autolisp - Part 1
  2. AutoCAD Customization using autolisp (part 2)
  3. AutoCAD Customization using autolisp (part 3)