Can you explain what you just wrote here... i have no clue
Not without having you do some tests. It's way easier to illustrate if you run the functions below and read the accompanying comments.
First start a fresh drawing. Create a new layer called "612" and set it to color red. Set layer "0" currently active and leave its color as the default 7 (white or black). Now load this test routine and run it. Draw one or two lines with it.
(defun C:TEST ()
(setvar "CLAYER" "612")
(command "LINE")
(setvar "CLAYER" "0")
)
This little routine sets the layer "612" active, issues a LINE command and resets the layer to "0". The intension is naturally to put all new lines on layer "612" .. but it doesn't. Your lines will be put on layer "0".
Whenever AutoLISP issues an AutoCAD command with the COMMAND function, it does not stop up and wait for AutoCAD to do its stuff. This means that before you even get to click at the start point, AutoLISP will have speeded ahead and already executed any subsequent code. Thus, AutoLISP sets the layer to "0" before AutoCAD gets a chance to ask you to draw a line.
So, how do you politely ask AutoLISP to stop and wait while the user finishes a command? You already know one way: to use the symbol PAUSE.
But PAUSE will only hold AutoLISP back while specifying a single parameter in a command. Try this slightly modified version of the routine above:
(defun C:TEST ()
(setvar "CLAYER" "612")
(command "LINE" pause pause)
(setvar "CLAYER" "0")
)
Cool. Now we at least get to draw a single line on the layer that we wanted. But if we stay in the command and want to draw more lines, AutoLISP will jump the speeder after the last PAUSE symbol and set the layer to "0".
We could put more PAUSE's in there but who are we to tell the user how many lines s/he is allowed to draw? So we'll have to find a way to stop AutoLISP entirely while there is a command active in AutoCAD.
This is what the system variable CMDACTIVE is for. It's simply a flag that tells if a command is active. If there are no commands currently running, this system variable will be 0 and all we have to do is check for that instance. By running a loop while a command is active we can detect when AutoCAD has finished doing its stuff. While a command is active, we simply issue the PAUSE symbol that allows the user to communicate with the command:
(defun C:TEST ()
(setvar "CLAYER" "612")
(command "LINE")
(while (> (getvar "CMDACTIVE") 0)
(command pause)
)
(setvar "CLAYER" "0")
)
Voíla, no more speeding tickets for AutoLISP. It sits neatly in its loop and supplies PAUSE's to the command until the user has finished.
Then it sets the layer to "0".