Jump to content

What's if the user pressed CANCEL while running a lisp


Sweety

Recommended Posts

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

Link to comment
Share on other sites

  • Replies 21
  • Created
  • Last Reply

Top Posters In This Topic

  • alanjt

    8

  • Sweety

    8

  • The Buzzard

    3

  • lpseifert

    1

Top Posters In This Topic

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

what I use...

(defun *error* (msg)
 (if (and msg (not (wcmatch (strcase msg) "*BREAK*,*CANCEL*,*QUIT*,")))
   (princ (strcat "\nError: " msg))
 )
)

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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)

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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 ?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

(defun *error* (msg)
 (and os (setvar 'osmode os))
 (if (and msg (not (wcmatch (strcase msg) "*BREAK*,*CANCEL*,*QUIT*,")))
   (princ (strcat "\nError: " msg))
 )
)

Link to comment
Share on other sites

(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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.
Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...