Sweety Posted October 19, 2010 Share Posted October 19, 2010 Hello GUYs Suppose that I have the following routine and the user pressed Cancel before completing the Lisp . (setq os (getvar 'osmode)) (setvar 'osmode 0) ............. Here is the rest of codes ..... So the user Canceled the Lisp [color="#f4a460"][color="#f4a460"][color="red"][b](setvar 'osmode os) ; <<-- This System Variable won't be re-set as it was before.[/b][/color][/color][/color] So how could I re-set the System variable as it was before when the user canceled the routine ? Many Thanks Quote Link to comment Share on other sites More sharing options...
The Buzzard Posted October 19, 2010 Share Posted October 19, 2010 Hello GUYs Suppose that I have the following routine and the user pressed Cancel before completing the Lisp . (setq os (getvar 'osmode)) (setvar 'osmode 0) ............. Here is the rest of codes ..... So the user Canceled the Lisp [color=#f4a460][color=#f4a460][color=red][b](setvar 'osmode os) ; <<-- This System Variable won't be re-set as it was before.[/b][/color][/color][/color] So how could I re-set the System variable as it was before when the user canceled the routine ? Many Thanks You will need to set up an error handler or error trap. You will find many types used on this site. Below is an example I use in one of my programs Example to save (setq SUS_LIST (list "cmdecho" "orthomode" "osmode" "blipmode" "clayer" "snapang" "textsize" "textstyle" "angbase" "angdir") SUS (mapcar 'getvar SUS_LIST) TERR *error* *error* MD_SET) Example to restore (defun MD_RUS () (setq *error* TERR) (if SUS (mapcar 'setvar SUS_LIST SUS)) (princ "\nMechanical Duct - Imperial, Copyright © 2010") (princ "\nThe program will now restore the user settings and exit.") (princ)) Example If user presses escape or an error occurs. (defun MD_SET (ERRORMSG) (command nil nil nil) (if (not (member ERRORMSG '("console break" "Function cancelled"))) (princ (strcat "\nError:" ERRORMSG))) (if SUS (mapcar 'setvar SUS_LIST SUS)) (princ "\nAttention!....A user error has occurred.") (princ "\nThe program will now restore the user settings and exit.") (terpri) (setq *error* TERR) (princ)) Quote Link to comment Share on other sites More sharing options...
lpseifert Posted October 19, 2010 Share Posted October 19, 2010 Have a look here http://www.afralisp.net/autolisp/tutorials/error-trapping.php Quote Link to comment Share on other sites More sharing options...
The Buzzard Posted October 19, 2010 Share Posted October 19, 2010 Have a look herehttp://www.afralisp.net/autolisp/tutorials/error-trapping.php Thats where I got the one above. Good call Larry. Quote Link to comment Share on other sites More sharing options...
The Buzzard Posted October 19, 2010 Share Posted October 19, 2010 Here is the link for error trapping http://www.afralisp.net/archive/lispa/lisp6.htm Quote Link to comment Share on other sites More sharing options...
BlackBox Posted October 19, 2010 Share Posted October 19, 2010 I found Robert Bell's error handler to be a great starting point when first learning about them; picked it up from an AU course: (defun *error* (msg) (cond ((not msg)) ; Normal exit ((member msg '("Function cancelled" "quit / exit abort"))) ; <esc> or (quit) ((princ (strcat "\n <!> Error: " msg " <!> ")) ; Fatal error, display it (cond ((vl-bb-ref '*Debug*) (vl-bt))))) ; If in debug mode, dump backtrace (princ)) Quote Link to comment Share on other sites More sharing options...
alanjt Posted October 19, 2010 Share Posted October 19, 2010 what I use... (defun *error* (msg) (if (and msg (not (wcmatch (strcase msg) "*BREAK*,*CANCEL*,*QUIT*,"))) (princ (strcat "\nError: " msg)) ) ) Quote Link to comment Share on other sites More sharing options...
Sweety Posted October 19, 2010 Author Share Posted October 19, 2010 I found Robert Bell's error handler to be a great starting point when first learning about them; picked it up from an AU course: Thank you all guys, it's been wonderful replies to mine . So I found code that supported by mr Renderman the easiest to me to deal with , So will it be like this ... (defun *error* (msg) (cond ((not msg)) ; Normal exit ((member msg '("Function cancelled" "quit / exit abort"))) ; <esc> or (quit) ((princ (strcat "\n <!> Error: " msg " <!> ")) ; Fatal error, display it (cond ((vl-bb-ref '*Debug*) (vl-bt))))) ; If in debug mode, dump backtrace [b][color="red"](setvar 'osmode os) [/color][/b] (princ)) (setq os (getvar 'osmode)) (setvar 'osmode 0) .......... .............. code of the routine (setvar 'osmode os) Because if a user pressed cancel the routine would re-set the System variable that included within error defun. Is this right ? Great thanks again for all. Quote Link to comment Share on other sites More sharing options...
alanjt Posted October 19, 2010 Share Posted October 19, 2010 Don't forget to localize the *error* function!!! Quote Link to comment Share on other sites More sharing options...
Sweety Posted October 19, 2010 Author Share Posted October 19, 2010 Don't forget to localize the *error* function!!! Is this what you mean mr Alan.? (defun *error* (msg [b][color="red"]/ os[/color][/b]) (cond ((not msg)) ; Normal exit ((member msg '("Function cancelled" "quit / exit abort"))) ; <esc> or (quit) ((princ (strcat "\n <!> Error: " msg " <!> ")) ; Fatal error, display it (cond ((vl-bb-ref '*Debug*) (vl-bt))))) ; If in debug mode, dump backtrace (setvar 'osmode os) (princ)) (setq [b][color="red"]os[/color][/b] (getvar 'osmode)) (setvar 'osmode 0) .......... .............. code of the routine (setvar 'osmode os) Quote Link to comment Share on other sites More sharing options...
alanjt Posted October 19, 2010 Share Posted October 19, 2010 Is this what you mean mr Alan.? (defun *error* (msg [b][color="red"]/ so[/color][/b]) (cond ((not msg)) ; Normal exit ((member msg '("Function cancelled" "quit / exit abort"))) ; <esc> or (quit) ((princ (strcat "\n <!> Error: " msg " <!> ")) ; Fatal error, display it (cond ((vl-bb-ref '*Debug*) (vl-bt))))) ; If in debug mode, dump backtrace (setvar 'osmode os) (princ)) (setq os (getvar 'osmode)) (setvar 'osmode 0) .......... .............. code of the routine (setvar 'osmode os) No, localize the *error* function itself within the actual routine. eg. (defun c:Test (/ *error* p1 p2) (defun *error* (m))......(princ)) Quote Link to comment Share on other sites More sharing options...
Sweety Posted October 19, 2010 Author Share Posted October 19, 2010 OK now it's clear . But is it correct to (setvar 'osmode os) within the error defun to be restored if the user canceled the routine while it's running ? Quote Link to comment Share on other sites More sharing options...
alanjt Posted October 19, 2010 Share Posted October 19, 2010 OK now it's clear . But is it correct to (setvar 'osmode os) within the error defun to be restored if the user canceled the routine while it's running ? Yes, but I would check to see if the variable has been defined first. eg. (if os (setvar 'osmode os)) or (and os (setvar 'osmode os)) Quote Link to comment Share on other sites More sharing options...
Sweety Posted October 19, 2010 Author Share Posted October 19, 2010 Now what you think ? Hope you don't mine if I used yours of course . (defun *error* (msg [color="red"][b]os[/b][/color]) (if (and msg (not (wcmatch (strcase msg) "*BREAK*,*CANCEL*,*QUIT*,")) [color="red"][b](setvar 'osmode os) [/b][/color] ) (princ (strcat "\nError: " msg)) ) ) Thanks a lot. Quote Link to comment Share on other sites More sharing options...
alanjt Posted October 19, 2010 Share Posted October 19, 2010 (defun *error* (msg) (and os (setvar 'osmode os)) (if (and msg (not (wcmatch (strcase msg) "*BREAK*,*CANCEL*,*QUIT*,"))) (princ (strcat "\nError: " msg)) ) ) Quote Link to comment Share on other sites More sharing options...
Sweety Posted October 19, 2010 Author Share Posted October 19, 2010 (defun *error* (msg) (and os (setvar 'osmode os)) (if (and msg (not (wcmatch (strcase msg) "*BREAK*,*CANCEL*,*QUIT*,"))) (princ (strcat "\nError: " msg)) ) ) Thank you so much for your great help mr Alan. You have been so wonderful. My best regards. Quote Link to comment Share on other sites More sharing options...
alanjt Posted October 19, 2010 Share Posted October 19, 2010 Thank you so much for your great help mr Alan. You have been so wonderful. My best regards. More important than anything: do you understand the function and what it's doing? Quote Link to comment Share on other sites More sharing options...
Sweety Posted October 19, 2010 Author Share Posted October 19, 2010 Actually YES. By the use of your error function, this would prevent any intersection of unknown error messages and so . and if you mean the System variable which I added, the answer is yes of course, because this is what I have been looking for to re-set my Cad's Sys Vars as they were before . Would you like to add any, or correct me please. Thanks a lot Quote Link to comment Share on other sites More sharing options...
alanjt Posted October 20, 2010 Share Posted October 20, 2010 Actually YES. By the use of your error function, this would prevent any intersection of unknown error messages and so . and if you mean the System variable which I added, the answer is yes of course, because this is what I have been looking for to re-set my Cad's Sys Vars as they were before . Would you like to add any, or correct me please. Thanks a lot I ask that because everything I gave you a hint, you returned with something completely wrong. Really look at how I constructed the error handler. Quote Link to comment Share on other sites More sharing options...
Sweety Posted October 20, 2010 Author Share Posted October 20, 2010 I ask that because everything I gave you a hint, you returned with something completely wrong. Really look at how I constructed the error handler. I guess now it's correct . (defun *error* (msg) (if (and msg (not (wcmatch (strcase msg) "*BREAK*,*CANCEL*,*QUIT*,"))) (progn (princ (strcat "\nError: " msg)) (and os (setvar 'osmode os)) ) ) ) Tell me please . Many thanks 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.