Turn Osanp to original settings if ESC key is used

Turn Osanp to original settings if ESC key is used

Anonymous
Not applicable
1,508 Views
14 Replies
Message 1 of 15

Turn Osanp to original settings if ESC key is used

Anonymous
Not applicable

I'm trying to figure out what to add to a lisp command so that Osnap settings revert back to user settings if the ESC key is used.

 

Here is the code I am currently trying to modify:

 

(Defun LRSD4 (/ sf e1 pt1 pt2)
(setq sf (getvar "dimscale"))
(command "ortho" "on")
(setq smode (getvar "osmode"))
(command "osnap" "none")
(prompt "Enter left location: ")
(command "insert" "lsd2" "scale" sf pause "0.0")
(setq e1 (entget (entlast)))
(setq pt1 (cdr (assoc 10 e1)))
(setq pt2 (getpoint "\nEnter right location: " pt1 ))
(command "insert" "rsd2" "scale" sf pt2 "0.0")
(command "ortho" "off")
(command "insert" "3RDL" "scale" sf pt2 PAUSE)
(command "setvar" "osmode" smode)
)
(Defun c:lrsd2 ()
(setvar "cmdecho" 0)
(lrsd4)
(setvar "cmdecho" 1)
(princ)
)

 

Any help would be greatly appreciated.

 

0 Likes
1,509 Views
14 Replies
Replies (14)
Message 2 of 15

Ranjit_Singh
Advisor
Advisor

I would imagine you just add an error function. Make it set all the settings back to original values. See AutoCAD documentation here

Message 3 of 15

Kent1Cooper
Consultant
Consultant

Search this Forum for Posts containing

 

(defun *error*

 

and you will find countless examples of how to do that.

Kent Cooper, AIA
Message 4 of 15

BlackBox_
Advisor
Advisor

@Kent1Cooper, and @Ranjit_Singh are exactly right.

 

As example, here's a quick adaptation to the code above:

 

(defun c:LRSD2 (/ *error* cmdecho dimscale orthomode osmode e1 pt1 pt2)

  (defun *error* (msg)

    ;; restore variables
    (if cmdecho (setvar 'cmdecho))
    (if orthomode (setvar 'orthomode))
    (if osmode (setvar 'osmode))    
    (cond ((not msg))                                                   ; Normal exit
          ((member msg '("Function cancelled" "quit / exit abort")))    ; <esc> or (quit)
          ((princ (strcat "\n** Error: " msg " ** ")))                  ; Fatal error, display it
    )
    (princ)
  )

  ;; store variables
  (setq cmdecho (getvar 'cmdecho))
  (setq dimscale (getvar 'dimscale))
  ;;(command "ortho" "on")
  (setq orthomode (getvar 'orthomode))
  (setq osmode (getvar 'osmode))

  ;; set variables
  (setvar 'cmdecho 0)
  (setvar 'orthmode 1)  
  (setvar 'osmode 0)

  ;; main code
  (prompt "\nEnter left location: ")
  (command "insert" "lsd2" "scale" dimscale PAUSE "0.0")
  (setq e (entget (entlast)))
  (setq pt1 (cdr (assoc 10 e)))
  (setq pt2 (getpoint "\nEnter right location: " pt1))
  (command "insert" "rsd2" "scale" dimscale pt2 "0.0")
  (setvar 'orthomode 0)
  (command "insert" "3RDL" "scale" dimscale pt2 PAUSE)
  
  (*error*)
)

 

 

Cheers


"How we think determines what we do, and what we do determines what we get."

Sincpac C3D ~ Autodesk Exchange Apps

0 Likes
Message 5 of 15

Kent1Cooper
Consultant
Consultant

@BlackBox_ wrote:

@Kent1Cooper 

....
(defun *error* (msg) ....
) .... [ c o d e ] .... (*error*)
....

That raises a question in my mind....

 

I have seen others call the *error* function at the end of a routine, in order to reset System Variables and sometimes do an Undo End marker, rather than spell those things out at the end of the code itself.  I don't "like" to do it that way, because it somehow "hurts my soul" to cause the *error* handler to fire when there has not, in fact, been an error, but I that's just my personal preference.

 

However, in that approach I've typically seen it called at the end this way:

 

(*error* nil)

 

Since, as you have it, *error* is defined to take an argument, if you call it with only (*error*), without supplying that nil as argument, don't you get a too-few-arguments error from that?  It certainly happens to me if I define a function to take an argument, and call it without supplying any.

Kent Cooper, AIA
Message 6 of 15

BlackBox_
Advisor
Advisor

Bah - you're spot on, Kent.

 

That's what I get for Ctrl+F & replace in lieu of re-typing code. Unfortunately, I cannot go back and correct my earlier post, so apologies to OP for any confusion... I attempted to keep it simple by using as much of the OP code as I could quickly, rather than my instinct to re-write using acet-ss-drag-[move/rotate] functions, and single vla-EndUndoMark, etc. Haha

 

I appreciate the correction.

 

 

Cheers


"How we think determines what we do, and what we do determines what we get."

Sincpac C3D ~ Autodesk Exchange Apps

0 Likes
Message 7 of 15

Kent1Cooper
Consultant
Consultant

@Kent1Cooper wrote:

Search this Forum ... and you will find countless examples of how to do that.


Just one example is here, which I cite because it describes what I think is an interesting way to handle System Variable saving changing and resetting, and typically with less code if there are more than about two of them to reset.

Kent Cooper, AIA
0 Likes
Message 8 of 15

scot-65
Advisor
Advisor
Well, I was waiting on someone else to help
you along with the error code. Since you are
trying to work with OSMODE, here is what you
can inject into your program. I have provided
comments to help you out...

;=====================================================
; Note: Where OSMODE requires turning off, 16384 added to value, which
; supresses OSMODE and the user can turn back on via status line toggle,
; if needed.
;
;inside *error*:
(if (> (getvar "OSMODE") 16384)
(setvar "OSMODE" (- (getvar "OSMODE") 16384)) );if
;set:
(setvar "OSMODE" (+ (getvar "OSMODE") 16384)) ;toggles setting
;reset:
(if (> (getvar "OSMODE") 16384)
(setvar "OSMODE" (- (getvar "OSMODE") 16384)) );if

Declaring "smode" is no longer needed.

And Welcome to these Forums.

???

Scot-65
A gift of extraordinary Common Sense does not require an Acronym Suffix to be added to my given name.

0 Likes
Message 9 of 15

Kent1Cooper
Consultant
Consultant

@scot-65 wrote:
...
; Note: Where OSMODE requires turning off, 16384 added to value, which
; supresses OSMODE ....
;
;inside *error*:
(if (> (getvar "OSMODE") 16384)
(setvar "OSMODE" (- (getvar "OSMODE") 16384)) );if
;set:
(setvar "OSMODE" (+ (getvar "OSMODE") 16384)) ;toggles setting
....

See a discussion of the potential problem with that approach here.

Kent Cooper, AIA
0 Likes
Message 10 of 15

john.uhden
Mentor
Mentor

That's interesting.

*error* is defuned as taking one argument, yet you closed the main function calling *error* with no arguments.

I presume by your credentials that you know it's okay to do.  Has that always been legal?  Or is calling *error* without an argument calling *error* with an argument?

John F. Uhden

0 Likes
Message 11 of 15

BlackBox_
Advisor
Advisor

Unfortunately, no - the reason you're still reading that in my earlier post is because @Anonymous chose to preclude users from editing their posts (after a given amount of time). Any other CAD forum, one can go back and correct mistakes such as this.

 

Nonetheless, this is an error on my part, spending too much time in .NET, and wanted to get back into my first coding language (LISP). I apologize for any confusion my being unable to edit the post has caused. 

 

Please call (*error* nil) with an argument; see Kent's apt post/correction. 

 

 

Cheers

 

 


"How we think determines what we do, and what we do determines what we get."

Sincpac C3D ~ Autodesk Exchange Apps

0 Likes
Message 12 of 15

john.uhden
Mentor
Mentor

Well, at least you're a Civil kinda person.

I haven't made too many mistakes, but the best was back in my youth when the state required us to add superelevation for all curves <= 1000' C/L radius.

The C/L profile was at only 0.35% so unbeknownst to me I created a low point on the inside gutter.  Luckily it rained hard before the stab. base was down and my shortsightedness turned into "Lake Uhden."  A change order for about 1500 feet of pipe and several structures compensated.  Just one of those mistakes you make only once in a life time.

John F. Uhden

0 Likes
Message 13 of 15

BlackBox_
Advisor
Advisor

That's kind of you to say, @john.uhden... and I'd be remiss to pass it off as having always been the case. 

 

I've made many mistakes along the way, and I'll make many more... my hope is that I will have done more good in that time, than not. 

 

 

Cheers


"How we think determines what we do, and what we do determines what we get."

Sincpac C3D ~ Autodesk Exchange Apps

0 Likes
Message 14 of 15

john.uhden
Mentor
Mentor
I'd say you are succeeding in my goal, that is "to make the world around
you a little better."

John F. Uhden

0 Likes
Message 15 of 15

john.uhden
Mentor
Mentor
I'd say you are succeeding in my goal, that is "to make the world around
you a little better."

John F. Uhden

0 Likes