yrnomad Posted August 28, 2009 Share Posted August 28, 2009 I'm trying to make this routine not fail to reset the osmode variable if the user cancels the function between the arrow and the text inputs. How would I do that? ;;;****************HR************************* (defun c:hR (/ otmd osmd a b c) (setq otmd (getvar "orthomode")) (setq osmd (getvar "osmode")) (command "ortho" "off") (setvar "osmode" 695) (setq a (* 0.0333 (getvar "userr1"))) (setq b (* 0.1000 (getvar "userr1"))) (setq c (* 0.1 (getvar "userr1"))) (command "pline" pause "arc" "s" pause pause "L" "w" a 0.0 "L" b "") ;(setvar "osmode" 0) (setq textstart (getpoint "\nText start point: ")) (command "textstyle" "romans") (command "dtext" textstart c "") (setvar "orthomode" otmd) (setvar "osmode" 695) );end homerun. Quote Link to comment Share on other sites More sharing options...
JohnM Posted August 28, 2009 Share Posted August 28, 2009 there have been many post on this subject here. just do a search and you will find more than you need Quote Link to comment Share on other sites More sharing options...
SteveK Posted August 28, 2009 Share Posted August 28, 2009 One way is to use an error trap. Have a look at http://www.afralisp.net/lispa/lisp6.htm. ;;;****************HR************************* (defun c:hR (/ otmd osmd a b c [color=Blue]temperr[/color]) (setq otmd (getvar "orthomode")) (setq osmd (getvar "osmode")) [color=Blue] (setq temperr *error*) ;store *error* (setq *error* trap1) ;re-assign *error*[/color] (command "ortho" "off") (setvar "osmode" 695) (setq a (* 0.0333 (getvar "userr1"))) (setq b (* 0.1000 (getvar "userr1"))) (setq c (* 0.1 (getvar "userr1"))) (command "pline" pause "arc" "s" pause pause "L" "w" a 0.0 "L" b "") ;(setvar "osmode" 0) (setq textstart (getpoint "\nText start point: ")) (command "textstyle" "romans") (command "dtext" textstart c "") (setvar "orthomode" otmd) (setvar "osmode" 695) [color=Blue] (setq *error* temperr) ;restore *error*[/color] );end homerun. [color=Blue]; ERROR TRAP (defun trap1 (errmsg) ;define function (setq *error* temperr) ;restore *error* (setvar "orthomode" otmd) ;restore variable (setvar "osmode" osmd) ;restore variable (prompt "\nAn unknown error has occured or Escape has been pressed.\nProgram cancelled.\n") ;inform user (princ) )[/color] Not tested. Quote Link to comment Share on other sites More sharing options...
gile Posted August 28, 2009 Share Posted August 28, 2009 Hi, Another way, nesting the redefined *error* function and declaring it local ;;;****************HR************************* (defun c:hR (/ [color=blue]*error*[/color] otmd osmd a b c) [color=blue];; redefine *error* (defun *error* (msg) (if (and msg (/= msg "Function cancelled")) (princ (strcat "\nError: " msg)) ) (setvar "orthomode" otmd) (setvar "osmode" osmd) (princ) )[/color] (setq otmd (getvar "orthomode")) (setq osmd (getvar "osmode")) (command "ortho" "off") (setvar "osmode" 695) (setq a (* 0.0333 (getvar "userr1"))) (setq b (* 0.1000 (getvar "userr1"))) (setq c (* 0.1 (getvar "userr1"))) (command "pline" pause "arc" "s" pause pause "L" "w" a 0.0 "L" b "") ;; (setvar "osmode" 0) (setq textstart (getpoint "\nText start point: ")) (command "textstyle" "romans") (command "dtext" textstart c "") [color=blue](*error* nil) ;_ restores variable calling *error*[/color] ) ;_ end homerun. Quote Link to comment Share on other sites More sharing options...
alanjt Posted August 28, 2009 Share Posted August 28, 2009 give this a whirl: ;;;****************HR************************* (defun c:hR (/ *error* otmd osmd a b c) ;;; error handler (defun *error* (#Message) (and otmd (setvar "orthomode" otmd)) (and osmd (setvar "osmode" osmd)) (and #Message (or (member #Message '("console break" "Function cancelled" "quit / exit abort") ) ;_ member (princ (strcat "\nError: " #Message)) ) ;_ or ) ;_ and ) ;_ defun (setq otmd (getvar "orthomode")) (setq osmd (getvar "osmode")) (sevar "orthomode" 0) (setvar "osmode" 695) (setq a (* 0.0333 (getvar "userr1"))) (setq b (* 0.1000 (getvar "userr1"))) (setq c (* 0.1 (getvar "userr1"))) (command "pline" pause "arc" "s" pause pause "L" "w" a 0.0 "L" b "") ;(setvar "osmode" 0) (setq textstart (getpoint "\nText start point: ")) (command "textstyle" "romans") (command "dtext" textstart c "") ;; reset variables (*error* nil) );end homerun. *error* is a user redefinable subroutine. now, if you exit out of your routine, it will reset all variables (if defined). the MOST important thing you MUST REMEMBER is to localize the *error* handler. you want the original one after the routine is over. i also switched out your use of command for setvar on orthomode. Quote Link to comment Share on other sites More sharing options...
alanjt Posted August 28, 2009 Share Posted August 28, 2009 oops, gile beat me to it. Quote Link to comment Share on other sites More sharing options...
SteveK Posted August 28, 2009 Share Posted August 28, 2009 Yep they're better ways, I'd use them. Quote Link to comment Share on other sites More sharing options...
Lee Mac Posted August 30, 2009 Share Posted August 30, 2009 I sometimes use WCMATCH instead of member/vl-position: (if (not (wcmatch (strcase msg) "*BREAK,*CANCEL*,*EXIT*")) (princ (strcat "\n** Error: " msg " **"))) Quote Link to comment Share on other sites More sharing options...
alanjt Posted August 31, 2009 Share Posted August 31, 2009 I sometimes use WCMATCH instead of member/vl-position: (if (not (wcmatch (strcase msg) "*BREAK,*CANCEL*,*EXIT*")) (princ (strcat "\n** Error: " msg " **"))) another fine option. i wonder if one is one yields any better results. Quote Link to comment Share on other sites More sharing options...
Lee Mac Posted August 31, 2009 Share Posted August 31, 2009 I just find that it is not as verbose... Quote Link to comment Share on other sites More sharing options...
alanjt Posted September 1, 2009 Share Posted September 1, 2009 I just find that it is not as verbose... to each his own. i've seen the wcmatch way before, it's another nice method. that's the great thing about programming, you can accomplish the same thing 50 different ways. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.