Author Topic: exiting while loop with escape. error omitted?  (Read 9784 times)

0 Members and 1 Guest are viewing this topic.

Red Nova

  • Newt
  • Posts: 69
exiting while loop with escape. error omitted?
« on: January 10, 2018, 06:24:34 PM »
Hi.
I bit nooby but I recently noticed that if I force break a while loop with escape the continuation will not go through *error* function.
1. Can you tell me why? ։)
2. What can be a solution?

Here is an example.
I am creating a while loop with a simple calculation that will take a few seconds to run.
Set current color - GREEN
run test command
If we let the command finish we will have same GREEN current color when done.
But if we press escape before while is finished we get RED in current color.

Code: [Select]
(defun c:test ( / i var val *error*)
  (defun *error* ( msg )
    (mapcar 'setvar var val)
    (if (and msg (not (wcmatch (strcase msg t) "*break,*cancel*,*exit*")))
                 (princ (strcat "\nError: " msg)))
    (vla-endundomark adoc)
    )

  (vl-load-com)
  (vla-startundomark (setq adoc (vla-get-activedocument (vlax-get-acad-object))))
 
  (setq var '(clayer cecolor)
        val  (mapcar 'getvar var))

  (setvar "cecolor" "1")
  (setq i 1)
  (while
    (< i 5000000)
    (setq i (1+ i))
    )
  (setvar "cecolor" "2")
  (*error* nil)
  )

Thanks for the feedback.


Red Nova

  • Newt
  • Posts: 69
Re: exiting while loop with escape. error omitted?
« Reply #2 on: January 11, 2018, 03:12:12 AM »
Yes, it's English version. What's the difference?

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1453
  • Marco
Re: exiting while loop with escape. error omitted?
« Reply #3 on: January 11, 2018, 03:35:10 AM »
Maybe it is not the solution... but in other languages:

"Function cancelled" "console break" "quit / exit abort"   in italian is
"Funzione annullata" "la funzione è stata annullata" "interruzione da tastiera" "esci / continua"

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1453
  • Marco
Re: exiting while loop with escape. error omitted?
« Reply #4 on: January 11, 2018, 03:49:59 AM »
Code: [Select]
(defun c:test ( / i var val *error*)
  (defun *error* ( msg )
    (print (getvar "cecolor")) (princ " < color on start *error*")
    (mapcar 'setvar var val)
    (print (getvar "cecolor")) (princ " < color after mapcar *error*")
    (if (and msg (not (wcmatch (strcase msg t) "*break,*cancel*,*exit*")))
                 (princ (strcat "\nError: " msg)))
    (vla-endundomark adoc)
    (print (getvar "cecolor")) (princ " < color after endundomar *error*")
  )
  (vl-load-com)
  (vla-startundomark (setq adoc (vla-get-activedocument (vlax-get-acad-object))))
  (print (getvar "cecolor")) (princ " < color on start test")
  (setq var '(clayer cecolor)
        val  (mapcar 'getvar var))
  (setvar "cecolor" "1")
  (print (getvar "cecolor")) (princ " < color after cecolor 1")
  (setq i 1)
  (while
    (< i 5000000)
    (setq i (1+ i))
  )
  (setvar "cecolor" "2")
  (print (getvar "cecolor")) (princ " < color after cecolor 2")
  (*error* nil)
  (print (getvar "cecolor")) (princ " < color on end test")
)
Code: [Select]
Comando: TEST
"3"  < color on start test
"1"  < color after cecolor 1
"1"  < color on start *error*
"3"  < color after mapcar *error*
Error: Funzione annullata                   <<< ESC
"3"  < color after endundomar *error*

Comando: test
"3"  < color on start test
"1"  < color after cecolor 1
"2"  < color after cecolor 2
"2"  < color on start *error*
"3"  < color after mapcar *error*
"3"  < color after endundomar *error*
"3"  < color on end test" < color on end test"

Red Nova

  • Newt
  • Posts: 69
Re: exiting while loop with escape. error omitted?
« Reply #5 on: January 11, 2018, 10:19:36 AM »
So when you break while with escape it actually goes through *error* for you?
I got this:

Code: [Select]
Command: TEST
"3"  < color on start test
"1"  < color after cecolor 1
"1"  < color on start *error*Function cancelled      <<< ESC
Command:
Command: *Cancel*

Command: TEST
"1"  < color on start test
"1"  < color after cecolor 1
"2"  < color after cecolor 2
"2"  < color on start *error*
"1"  < color after mapcar *error*
"1"  < color after endundomar *error*
"1"  < color on end test" < color on end test"
Command:

How can this be?  :-o

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1453
  • Marco
Re: exiting while loop with escape. error omitted?
« Reply #6 on: January 11, 2018, 10:48:10 AM »
So when you break while with escape it actually goes through *error* for you?
Yes.

Try   (while  (< i 50000000)  (setq i (1+ i)))

Red Nova

  • Newt
  • Posts: 69
Re: exiting while loop with escape. error omitted?
« Reply #7 on: January 11, 2018, 01:06:09 PM »
AutoCAD is broken.  :crazy2:
I use 2017. Which version is yours?

So when you break while with escape it actually goes through *error* for you?
Yes.

Try   (while  (< i 50000000)  (setq i (1+ i)))


Same result.
Code: [Select]
Command: TEST
"3"  < color on start test
"1"  < color after cecolor 1
"1"  < color on start *error*Function cancelled
Command:
Command: *Cancel*

Red Nova

  • Newt
  • Posts: 69
Re: exiting while loop with escape. error omitted?
« Reply #8 on: January 11, 2018, 01:41:10 PM »
Just tried on 2018 English. Still not working.

PKENEWELL

  • Bull Frog
  • Posts: 318
Re: exiting while loop with escape. error omitted?
« Reply #9 on: January 11, 2018, 03:27:36 PM »
I think I know what your problem is:

Your "var" list is not quoting the system variable names or sending them as strings which is required for (getvar). Try the following instead.

Code: [Select]
(setq var '("clayer" "cecolor"))   
"When you are asked if you can do a job, tell 'em, 'Certainly I can!' Then get busy and find out how to do it." - Theodore Roosevelt

Red Nova

  • Newt
  • Posts: 69
Re: exiting while loop with escape. error omitted?
« Reply #10 on: January 11, 2018, 03:52:35 PM »
I think I know what your problem is:

Your "var" list is not quoting the system variable names or sending them as strings which is required for (getvar). Try the following instead.

Code: [Select]
(setq var '("clayer" "cecolor"))   

That doesn't help. As you can see from previous posts if I let the code complete (and my code actually finishes with *error*), then *error* will work just fine. But if I escape during while loop then it won't simply go to *error*. Try Code from Marc'Antonio Alessi, I wonder what will you get. Try first letting the code do it's work. Then try to break while with escape.

JohnK

  • Administrator
  • Seagull
  • Posts: 10637
Re: exiting while loop with escape. error omitted?
« Reply #11 on: January 11, 2018, 04:02:09 PM »
If I'm not mistaken, I believe you haven't set the error routine to run properly.

-i.e. See highlighted line.
Code - Auto/Visual Lisp: [Select]
  1. (defun c:MyLisp ( / AweSh0t )
  2.  
  3.   (defun AweSh0t (s / )
  4.     (setq *error* olderr
  5.           olderr  nil)
  6.     (princ) )
  7.   (setq olderr *error* *error* AweSh0t)
  8.  
  9.   (command "_line")
  10.  
  11.   (while (eq (getvar "cmdactive") 1)
  12.          (command PAUSE))
  13. )
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

Red Nova

  • Newt
  • Posts: 69
Re: exiting while loop with escape. error omitted?
« Reply #12 on: January 11, 2018, 05:01:20 PM »
If I'm not mistaken, I believe you haven't set the error routine to run properly.

-i.e. See highlighted line.
Code - Auto/Visual Lisp: [Select]
  1. (defun c:MyLisp ( / AweSh0t )
  2.  
  3.   (defun AweSh0t (s / )
  4.     (setq *error* olderr
  5.           olderr  nil)
  6.     (princ) )
  7.   (setq olderr *error* *error* AweSh0t)
  8.  
  9.   (command "_line")
  10.  
  11.   (while (eq (getvar "cmdactive") 1)
  12.          (command PAUSE))
  13. )

John, aren't those simply different approaches to error handling? I am localizing *error* function, you are not.
However, if *error* is not written the right way, why would it work well if you reach it in the end of the code and not if you break "while" loop with escape button?

owenwengerd

  • Bull Frog
  • Posts: 451
Re: exiting while loop with escape. error omitted?
« Reply #13 on: January 11, 2018, 07:28:36 PM »
This:

Command: TEST
"3"  < color on start test
"1"  < color after cecolor 1
"1"  < color on start *error*Function cancelled      <<< ESC
Command:
Command: *Cancel*

... indicates that Escape was pressed twice: once to cancel the command, then again to cancel the error handler.

VovKa

  • Water Moccasin
  • Posts: 1631
  • Ukraine
Re: exiting while loop with escape. error omitted?
« Reply #14 on: January 11, 2018, 07:36:37 PM »
Red Nova, mapcar must be removed from *error*
this should work
Code: [Select]
(defun c:test (/ i val *error*)
  (defun *error* (msg)
    (print (getvar "cecolor"))
    (princ " < color on start *error*")
    (foreach v val (setvar (car v) (cdr v)))
    (print (getvar "cecolor"))
    (princ " < color after mapcar *error*")
    (if (and msg (not (wcmatch (strcase msg t) "*break,*cancel*,*exit*")))
      (princ (strcat "\nError: " msg))
    )
    (vla-endundomark adoc)
    (print (getvar "cecolor"))
    (princ " < color after endundomar *error*")
  )
  (vl-load-com)
  (vla-startundomark (setq adoc (vla-get-activedocument (vlax-get-acad-object))))
  (print (getvar "cecolor"))
  (princ " < color on start test")
  (setq val (mapcar (function (lambda (v) (cons v (getvar v)))) '("clayer" "cecolor")))
  (setvar "cecolor" "1")
  (print (getvar "cecolor"))
  (princ " < color after cecolor 1")
  (setq i 1)
  (while (< i 5000000) (setq i (1+ i)))
  (setvar "cecolor" "2")
  (print (getvar "cecolor"))
  (princ " < color after cecolor 2")
  (*error* nil)
  (print (getvar "cecolor"))
  (princ " < color on end test")
)
Code: [Select]
Command: test

"3"  < color on start test
"1"  < color after cecolor 1
"1"  < color on start *error*
"3"  < color after mapcar *error*
"3"  < color after endundomar *error*
Command:
Command: *Cancel*