Visual LISP, AutoLISP and General Customization
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Message switching and escape key

12 REPLIES 12
SOLVED
Reply
Message 1 of 13
zenkai
934 Views, 12 Replies

Message switching and escape key

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)
  )

 

 

 

12 REPLIES 12
Message 2 of 13
hmsilva
in reply to: zenkai

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

EESignature

Message 3 of 13
Kent1Cooper
in reply to: hmsilva

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 Cooper, AIA
Message 4 of 13
zenkai
in reply to: Kent1Cooper

That worked PERFECT, I didn't even think to try it that way, and thanks for explaining I tip my hat ot you sir

Message 5 of 13
hmsilva
in reply to: Kent1Cooper


@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

EESignature

Message 6 of 13
Kent1Cooper
in reply to: zenkai

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.

Kent Cooper, AIA
Message 7 of 13
zenkai
in reply to: Kent1Cooper

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

Message 8 of 13
zenkai
in reply to: zenkai

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)
)

 

Message 9 of 13
hmsilva
in reply to: zenkai

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

EESignature

Message 10 of 13
Gary_J_Orr
in reply to: zenkai

qleader always has been a finiky beast...
you don't by any chance use mleaders do you? they are a bit more stable in their implementation (and would work with your while: cmdactive: pause)
Gary J. Orr
(Your Friendly Neighborhood) CADD/BIM/VDC Applications Manager
http://www.linkedin.com/in/garyorr

aka (current and past user names):
Gary_J_Orr (GOMO Stuff 2008-Present); OrrG (Forum Studio 2005-2008); Gary J. Orr (LHB Inc 2002-2005); Orr, Gary J. (Gossen Livingston 1997-2002)
Message 11 of 13
Gary_J_Orr
in reply to: Gary_J_Orr

I think that I need to recant this suggestion...
the text entry in mleader is VERY dependant on acad settings... as a result, using the while: cmdactive: pause can cause unexpected results.

-G
Gary J. Orr
(Your Friendly Neighborhood) CADD/BIM/VDC Applications Manager
http://www.linkedin.com/in/garyorr

aka (current and past user names):
Gary_J_Orr (GOMO Stuff 2008-Present); OrrG (Forum Studio 2005-2008); Gary J. Orr (LHB Inc 2002-2005); Orr, Gary J. (Gossen Livingston 1997-2002)
Message 12 of 13
zenkai
in reply to: Gary_J_Orr

Actually Gary the way I have my templates set up, the mleader suggestion is working great
Message 13 of 13
Gary_J_Orr
in reply to: zenkai

when I ran it in one environment it worked well for me as well...
Then I tried it again in another environment (different OS/different version of Acad/ Different Acad settings) and it only allowed me to enter a single word, as soon as I hit the space bar it ended the command...
Therefore I can't stand behind my original statement and would not trust it now...
but that's just me *ReallyBigGrin*

-G
Gary J. Orr
(Your Friendly Neighborhood) CADD/BIM/VDC Applications Manager
http://www.linkedin.com/in/garyorr

aka (current and past user names):
Gary_J_Orr (GOMO Stuff 2008-Present); OrrG (Forum Studio 2005-2008); Gary J. Orr (LHB Inc 2002-2005); Orr, Gary J. (Gossen Livingston 1997-2002)

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report

”Boost