PDA

View Full Version : Repeating an autolisp program


pkreusch
2009-07-30, 01:38 PM
I recently wrote a small lisp program (with the help of some great forum readers!) that I want to repeat until the user escapes (self repeating). How do I accomplish this? Here's the short program:
_____________________________________________________________
;Creates a polyline around an area, automatically closes the
;pline, and automatically prompts the user to place
;a text with the square footage inside the area with the
;area rounded to the nearest square foot. Load lisp file
;and type "at" at the command prompt. Follow prompts to first
;draw the pline by selecting points and then place the cursor
;where you want the middle point of your text.

(defun c:AT(/ area)
(setvar "cmdecho" 1)
(command "._PLINE")
(while (> (logand (getvar "CMDACTIVE") 1) 0)
(command pause)
)
(command "pedit" "l" "c" "")
(command "area" "o" "l")
(setq area(rtos(/(getvar "area")144)2 0))
(command "text" "m" pause (getvar "textsize") "0" (strcat area " s.f."))
)

Opie
2009-07-30, 03:09 PM
You could wrap most of the code with a while loop. For the test to see if the while loop will execute, you might request the user to select the first point of the polyline and place that into a variable. If the value of the variable is nil, then do not run the routine.

rkmcswain
2009-07-30, 07:53 PM
Here is one way...


(defun c:AT (/ area)
(setvar "cmdecho" 1)
(while (setq p (getpoint "\nPick first point "))
(command "._PLINE" p)
(while (> (logand (getvar "CMDACTIVE") 1) 0)
(command pause)
)
(command "._pedit" "_L" "_C" "")
(command "._area" "_O" "_L")
(setq area (rtos (/ (getvar "area") 144) 2 0))
(command "._text"
"m"
pause
(getvar "textsize")
"0"
(strcat area " s.f.")
)
)
(princ)
)


FYI: If the current textstyle has a fixed text height, this will fail. You may want to test for this condition or use (entmake) to create the text entities.

pkreusch
2009-07-31, 03:17 AM
Thank you for your input, and it certainly works....I just don't really understand why. The "while" function starts with the whole program nested into it and I get that. But what is the "while" command waiting for? Doesn't it need another argument like "while (some number or function) is active, do this until (some number or function) is no longer active"? Is this just the way the "while" function works? It doesn't need another argument? Can I program this into just about anything that I want to loop until escape?

I'm guessng it sees everything inside the "while" function parenthesis as a test expression which is undoubtetly not nil so it keeps on going??

What would happen if somewhere inside a while finction I set a variable to nil? would that cause the while loop to stop mid-stream?

Thanks,
Patrick

rkmcswain
2009-07-31, 02:39 PM
The (while) function requires two arguments. The first is a test expression, whose result is either nil or something else. If its nil, then (while) ends, otherwise it continues processing the second argument, which is the code to be evaluated.

In the function above, the first argument is (setq p (getpoint "\nPick first point ")) - so if you pick a point here, it tells the (while) function to keep going, since it's not nil. But if you right click for example, the (getpoint) function returns nil and the (while) loop ends. The (setq p) is just there so we can pass the result of the (getpoint) to the (command "._pline") line.

pkreusch
2009-07-31, 08:11 PM
Thanks. That's pretty clear.