A More Versatile Loop : the while Function with autolisp in AutoCAD

The (repeat) function is limited to situations where the programmer (or the user) can determine beforehand the number of loops desired. The more common looping situation is to continue to execute a series of expressions until a certain changing condition is met. This is exactly the use of the (while) function.

Explanation: the (while) function

(while expression1

where expressionx is a conditional statement.

The (while) function first evaluates the expression1. As long as it is non- nil, the remaining expressions are evaluated and it then re-evaluates the expression1. (The assumption is that the expressions within the loop are somehow changing the expression1.) The cycle continues until the expression1 is nil, and the (while) function returns the most recent value of the last expressionn.


(setq RADIUS 1.0)
(setq PT (getpoint “\nEnter a center point: “))
(while (< RADIUS 2.25)
       (command “circle” PT RADIUS)
       (setq RADIUS (+ RADIUS 0.125))

This expression draws concentric circles at a center point picked by the user. It starts with a radius of 1 and adds 0.125 to the radius of each successive circle until the value of the radius reaches 2.25.

  • Any non-nil character may serve as a conditional expression.

(setq PT (getpoint “\nPick a point:”))

This expression could be used as the conditional expression in a (while) loop: as long as the user picks a point, the loop will repeat. If the user hits the spacebar or <enter> key, that returns nil and the loop is exited. Thus the user’s input becomes the condition of the (while) statement.

See also  Using Additional Point List Functions with autolisp in AutoCAD


(while (setq PT (getpoint “\nPick a point:”))
(setq PTLIST (cons PT PTLIST))
(foreach CPT PTLIST
(command “circle” CPT 1.0)

This expression will continue to add points to the list of points as long as the user picks or enters coordinates, then use the list of points to draw the circles.


Modify the file TAG.LSP to include incremental numbers inside the circle as long as the user selects a point for a tag. Estimated time for completion: 10 minutes.

Steps to the solution

1. Get starting number from user.

2. Request placement point for leader endpoint and center of tag.

3. Draw leader and tag.

4. Increase the next tag number by 1.

5. Loop back to Step 3 as long as user selects endpoint of leader.


(defun c:TAG ()
(setq CE-SAV (getvar “cmdecho”))
(setvar “cmdecho” 0)
(setq OS-SAVE (getvar “osnapcoord”))
(setvar “osnapcoord” 1)
(initget (+ 1 2 4))
(setq TAGNUMBER (getint “\nTag starting number: “))
(while (setq PT1 (getpoint “\nPick the leader endpoint or Press Enter tofinish: ” ))
(setq PT2 (getpoint PT1 “\nPick the tag center point: “))
(setq RADIUS (/ (getvar “dimscale”) 4.0))
(setq ANGLE1 (angle PT2 PT1))
(command “line” PT1 LEADERENDPT “”
“circle” PT2 RADIUS
                        “text” “m” PT2(* (getvar “dimscale”) 0.25)0(itoa TAGNUMBER)
        ) ;_ end of command
      (setq TAGNUMBER (1+ TAGNUMBER)) )
;_ end of while
(setvar “cmdecho” CE-SAV)
(setvar “osnapcoord” OS-SAVE
) ;_ end of defun

See also  Additional Filter Options for (ssget) with autolisp in AutoCAD

Note: The (itoa) function converts atoms of an integer data type to a string data type. (Data conversion will be covered later in the course.)

  • A complete solution to this exercise is on your class disk as TAG-B.LSP.
Back to top button