I don't write many macros/lisp files so when I do it's a re-learning experience so I have a quick question.
I wanted to make a simple lisp that would put dimensions on a spacific layer and then switch back to the layer the user was on. What I wrote works perfect except if the user escapes out of to command. So I was hoping to write a function to switch it back to the current layer if the user does hit the escape key. Can this be done or am I going to have to write something more elaberate to get user imput data then execute the command in case they do hit the ESC key?
(DEFUN C:tt (/ CL) (SETQ CL (GETVAR "CLAYER")) (COMMAND "CLAYER" "DIMENSIONS") (COMMAND "_DIMLINEAR" "\\" "\\" "\\") (COMMAND "CLAYER" CL) (PRINC) )
Solved! Go to Solution.
Solved by Kent1Cooper. Go to Solution.
Solved by hmsilva. Go to Solution.
Something like this perhaps.
(defun c:TT (/ *error* CL) (defun *error* (msg) (if CL (setvar "CLAYER" CL) ) (cond ((not msg)) ((member msg '("Function cancelled" "quit / exit abort"))) ((princ (strcat "\n** Error: " msg " ** "))) ) (princ) ) (setq CL (getvar 'CLAYER)) (command "_layer" "T" "DIMENSIONS" "M" "DIMENSIONS" "") (command "_DIMLINEAR") (while (> (getvar 'CMDACTIVE) 0) (command pause) ) (setvar "CLAYER" CL) (*error* nil) (princ) )
HTH
Henrique
The checking of the CMDACTIVE System Variable is the key to this, so these couple of little comments are sort of beside the point, but....
The CL variable is set before there is any opportunity for an error of any kind, or a User cancellation, to occur, so the (if CL ... in the error handler seems unnecessary, because CL will always be there, and the check will always return T[rue]. And if you make it run the error handler at the end, there's no need to reset the Layer just before that, because the error handler is going to do it. I think you could shorten it a little to:
(defun c:TT (/ *error* CL)
(defun *error* (msg)
(setvar "CLAYER" CL)
(cond ((not msg))
((member msg '("Function cancelled" "quit / exit abort")))
((princ (strcat "\n** Error: " msg " ** ")))
)
(princ)
)
(setq CL (getvar 'CLAYER))
(command "_layer" "T" "DIMENSIONS" "M" "DIMENSIONS" "")
(command "_DIMLINEAR")
(while (> (getvar 'CMDACTIVE) 0)
(command pause)
)
(*error* nil)
(princ)
)
Or, since if there's no error message, the only thing the error handler does is to reset the Layer, you could put
(setvar 'clayer CL)
in place of
(*error* nil)
at the end -- either would do, but both are not needed. And if you do make that change, you can omit the (not msg) condition from the error handler, since if it is ever triggered, there will always be a message. [It always "feels wrong" to me, in a way, to do the re-setting of things via *error* with no message, when in fact there has been no error, but that's a matter of taste, and it does sometimes reduce the overall amount of code.]
That worked PERFECT, I didn't even think to try it that way, and thanks for explaining I tip my hat ot you sir
@Kent1Cooper wrote:
The checking of the CMDACTIVE System Variable is the key to this, so these couple of little comments are sort of beside the point, but....
The CL variable is set before there is any opportunity for an error of any kind, or a User cancellation, to occur, so the (if CL ... in the error handler seems unnecessary, because CL will always be there, and the check will always return T[rue]. And if you make it run the error handler at the end, there's no need to reset the Layer just before that, because the error handler is going to do it. I think you could shorten it a little to:
(defun c:TT (/ *error* CL)
(defun *error* (msg)
(setvar "CLAYER" CL)
(cond ((not msg))
((member msg '("Function cancelled" "quit / exit abort")))
((princ (strcat "\n** Error: " msg " ** ")))
)
(princ)
)
(setq CL (getvar 'CLAYER))
(command "_layer" "T" "DIMENSIONS" "M" "DIMENSIONS" "")
(command "_DIMLINEAR")
(while (> (getvar 'CMDACTIVE) 0)
(command pause)
)
(*error* nil)
(princ)
)
Or, since if there's no error message, the only thing the error handler does is to reset the Layer, you could put
(setvar 'clayer CL)
in place of
(*error* nil)
at the end -- either would do, but both are not needed. And if you do make that change, you can omit the (not msg) condition from the error handler, since if it is ever triggered, there will always be a message. [It always "feels wrong" to me, in a way, to do the re-setting of things via *error* with no message, when in fact there has been no error, but that's a matter of taste, and it does sometimes reduce the overall amount of code.]
Kent,
as usual, I do agree with your comments...
I did used a generic error handler, to demonstrate to the OP how would it be possible to restore a System Variable on error...
And yes, could be written with less code lines.
Thank you!
Henrique
You're welcome, but I was only "fine-tuning" hmsilva's suggestion, which [though I haven't loaded it up and tried it] should also work fine. Therefore [oh, the awesome power that Expert Elite members wield! -- we get to do this even on threads we didn't initiate] I've also marked hmsilva's reply as a solution.
I didn't mean to disregard HMsilva's contabution, I didn't really studdy the code because although it looked pretty I pretty much had the same problem where if I cancled out of the command I was stuck back on the dimension layer. I really appreciate both your help
Sorry to bug you guys again but I just need one last change. The dimensions worked so great that I figured I needed it done for QLEADER, the problem is though I lock up autocad, I am sure it has something to do with QLEADER switching to MTEXT and the pause string. I am just not sure how to fix it...
(defun c:QL (/ *error* CL) (defun *error* (msg) (setvar "CLAYER" CL) (cond ((not msg)) ((member msg '("Function cancelled" "quit / exit abort"))) ((princ (strcat "\n** Error: " msg " ** "))) ) (princ) ) (setq CL (getvar 'CLAYER)) (command "_layer" "T" "DIMENSIONS" "M" "DIMENSIONS" "") (COMMAND "_QLEADER") (while (> (getvar 'CMDACTIVE) 0) (command pause) ) (*error* nil) (princ) )
Try
(defun c:QL (/ *error* CL) (defun *error* (msg) (setvar "CLAYER" CL) (princ) ) (setq CL (getvar 'CLAYER)) (command "_layer" "T" "DIMENSIONS" "M" "DIMENSIONS" "") (command "_qleader") (*error* nil) (princ) )
HTH
Henrique