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

0 Members and 1 Guest are viewing this topic.

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1451
  • Marco
Re: exiting while loop with escape. error omitted?
« Reply #15 on: January 12, 2018, 03:32:06 AM »
AutoCAD is broken.  :crazy2:
I use 2017. Which version is yours?
Sorry for late (time zone). I have tested on BricsCAD V16 V18 - AutoCAD 2013 2018 same result.
Try this:
Code: [Select]
(defun MyError (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*")
    (setq *error* olderr)
)
(defun c:test ( / i ) ; olderr var val > are globals
  (setq olderr *error*  *error* Myerror)
  (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")
  (Myerror nil)
 ; (setq *error* olderr) ; not needed
  (print (getvar "cecolor")) (princ " < color on end test")
)

Red Nova

  • Newt
  • Posts: 69
Re: exiting while loop with escape. error omitted?
« Reply #16 on: January 12, 2018, 11:09:16 AM »
owenwengerd - I definitely press escape only once.

VovKa - Actually that helped in the beginning. I don't know why, but using foreach instead of mapcar helped for the initial code.
Escaping during test1 I got this:
Quote
Command: TEST1
"3"  < color on start test
"1"  < color after cecolor 1
"1"  < color on start *error*
"3"  < color after variavle reset using foreach *error*
"3"  < color after endundomar *error*
Command:
Command: *Cancel*

Then I tried to add also UCS reset to *error* and it broke again. Note that error worked untill the moment it reached UCS reset part.
Escaping during test1 I got this:
Quote
Command: TEST2
"3"  < color on start test
"1"  < color after cecolor 1ucs
Current ucs name:  *WORLD*
Specify origin of UCS or [Face/NAmed/OBject/Previous/View/World/X/Y/Z/ZAxis] <World>: z
Specify rotation angle about Z axis <90d0'0">: 45
Command:
"UCS rotated"
"1"  < color on start *error*
"3"  < color after variavle reset using foreach *error*Function cancelled
Command: *Cancel*

Here are 2 codes that I used:
Code: [Select]
(defun c:test2 (/ 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 variavle reset using foreach *error*")
    (command-s "_.ucs" "_w")
    (print "UCS reset *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")
  (command "ucs" "z" "45")
  (print "UCS rotated")
  (setq i 1)
  (while (< i 5000000) (setq i (1+ i)))
  (setvar "cecolor" "2")
  (print (getvar "cecolor"))
  (princ " < color after cecolor 2 (while completed)")
  (*error* nil)
  (print (getvar "cecolor"))
  (princ " < color on end test")
)

(defun c:test1 (/ 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 variavle reset using foreach *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 (while completed)")
  (*error* nil)
  (print (getvar "cecolor"))
  (princ " < color on end test")
)

Marc'Antonio Alessi
Your version still did not work for me.

Actually can I ask everyone who is reading this topic to test my initial code from post 1 and post what is the outcome for you? I would appreciate if at least some of you do that. I do not understand how this code can work for Marc'Antonio Alessi and not work for me. Your tests would at least be informative to understand if something is wrong on my computers.

Red Nova

  • Newt
  • Posts: 69
Re: exiting while loop with escape. error omitted?
« Reply #17 on: January 12, 2018, 01:32:26 PM »
Looks like I could get it right now. :idea:
Replaced command method of UCS return in *error* to activeX and that helped.
I still do not understand why using macpar and command-s within *error* function is causing these issues. But at least now it works.
Thank you guys for the support.
Much appreciated :)

The final code
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 variavle reset using foreach *error*")
    (kb:UCS:NameWorld t)
    (print "UCS reset *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")
  (command "ucs" "z" "45")
  (print "UCS rotated")
  (setq i 1)
  (while (< i 5000000) (setq i (1+ i)))
  (setvar "cecolor" "2")
  (print (getvar "cecolor"))
  (princ " < color after cecolor 2 (while completed)")
  (*error* nil)
  (print (getvar "cecolor"))
  (princ " < color on end test")
)

(defun kb:UCS:NameWorld (MakeActive / localUCS)
  (or g:activedoc (setq g:activedoc (vla-get-activedocument (vlax-get-acad-object))))
  (or g:ucss
      (setq g:ucss
             (vla-get-usercoordinatesystems (vla-get-activedocument (vlax-get-acad-object))
             )
      )
  )
  (setq localUCS (vla-add g:ucss
                          (vlax-3d-point '(0.0 0.0 0.0)) ;origin
                          (vlax-3d-point '(1.0 0.0 0.0)) ;x-axis
                          (vlax-3d-point '(0.0 1.0 0.0)) ;y-axis
                          "_WorldUCS"
                 )
  )
  (if MakeActive
    (vla-put-activeucs g:activedoc localUCS)
  )
  localUCS
)

kdub_nz

  • Mesozoic keyThumper
  • SuperMod
  • Water Moccasin
  • Posts: 2132
  • class keyThumper<T>:ILazy<T>
Re: exiting while loop with escape. error omitted?
« Reply #18 on: January 12, 2018, 05:21:59 PM »
For anyone following up on this :

The kb:UCS:NameWorld comes from https://www.theswamp.org/index.php?topic=8314.0

The variables prefixed with g: are intended to be global scope in the document domain. These variable values are usually set when the document is opened and are intended to hold the 'pointers' to the object tables and collections, not actual dynamic data.

Regarding the *error* function, I believe the function should be declared local to it's containing function ; constructed just as Red Nova has done. I recall reading 'somewhere' in the AutoCAD Visual Lisp documentation that this was the 'correct' way to declare the function, reducing the likelyhood of the global error handler becoming corrupted.
I haven't evaluated the error function content in this case.

Regards,

« Last Edit: January 12, 2018, 05:28:46 PM by kdub »
Called Kerry in my other life
Retired; but they dragged me back in !

I live at UTC + 13.00

---
some people complain about loading the dishwasher.
Sometimes the question is more important than the answer.

steve.carson

  • Newt
  • Posts: 108
Re: exiting while loop with escape. error omitted?
« Reply #19 on: January 12, 2018, 06:47:14 PM »
Red Nova, your original code worked fine for me. Civil3D 2016


Steve

roy_043

  • Water Moccasin
  • Posts: 1895
  • BricsCAD 18
Re: exiting while loop with escape. error omitted?
« Reply #20 on: January 14, 2018, 04:37:53 AM »
@ Red Nova:
Maybe you should look at the *push-error-using-command* function.


Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1451
  • Marco
Re: exiting while loop with escape. error omitted?
« Reply #21 on: January 14, 2018, 12:08:49 PM »

@ Red Nova:
Maybe you should look at the *push-error-using-command* function.
...
Actually can I ask everyone who is reading this topic to test my initial code from post 1 and post what is the outcome for you? I would appreciate if at least some of you do that. I do not understand how this code can work for Marc'Antonio Alessi and not work for me. Your tests would at least be informative to understand if something is wrong on my computers.
What do you get with   "
code from post 1 "?

Lee Mac

  • Seagull
  • Posts: 12912
  • London, England
Re: exiting while loop with escape. error omitted?
« Reply #22 on: January 14, 2018, 02:16:34 PM »
Red Nova, mapcar must be removed from *error*

Why would the use of mapcar result in this behaviour?

VovKa

  • Water Moccasin
  • Posts: 1628
  • Ukraine
Re: exiting while loop with escape. error omitted?
« Reply #23 on: January 14, 2018, 04:50:55 PM »
Red Nova, mapcar must be removed from *error*

Why would the use of mapcar result in this behaviour?
it looks like a bug and it is not exactly (and only) mapcar's
i think interpreter's error trapping function is faulty
i thought it was broken only in the old releases (which i use) but now i see it's still present even in v2018

Lee Mac

  • Seagull
  • Posts: 12912
  • London, England
Re: exiting while loop with escape. error omitted?
« Reply #24 on: January 14, 2018, 05:45:15 PM »
Red Nova, mapcar must be removed from *error*

Why would the use of mapcar result in this behaviour?
it looks like a bug and it is not exactly (and only) mapcar's
i think interpreter's error trapping function is faulty
i thought it was broken only in the old releases (which i use) but now i see it's still present even in v2018

I'm surprised I've not encountered it previously - thanks VovKa.

VovKa

  • Water Moccasin
  • Posts: 1628
  • Ukraine
Re: exiting while loop with escape. error omitted?
« Reply #25 on: January 14, 2018, 06:32:11 PM »
I'm surprised I've not encountered it previously - thanks VovKa.
this indicates that *error* function rarely triggers inside your code, and it is good news ;)

PKENEWELL

  • Bull Frog
  • Posts: 317
Re: exiting while loop with escape. error omitted?
« Reply #26 on: January 16, 2018, 01:11:11 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.

I tested you code with my change and did not detect a problem. The "cecolor" value was reset properly every time and the *error* function was entered. I commented out the (if) and just printed the error message to be sure it was entering the *error* function.

Code: [Select]
(defun c:test ( / i var val *error*)

  (defun *error* ( msg )
    (mapcar 'setvar var val)
    (if msg (princ (strcat "\nError: " msg)))
;;     (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)
)
"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 #27 on: January 18, 2018, 09:31:23 AM »
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.

I tested you code with my change and did not detect a problem. The "cecolor" value was reset properly every time and the *error* function was entered. I commented out the (if) and just printed the error message to be sure it was entering the *error* function.

Code: [Select]
(defun c:test ( / i var val *error*)

  (defun *error* ( msg )
    (mapcar 'setvar var val)
    (if msg (princ (strcat "\nError: " msg)))
;;     (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)
)

As it was mentioned by VovKa, probably there is something wrong with my error trapping function, since both mapcar and command-s were not stable. I am not sure why this happens, but I see it on all computers on our network both on 2017 and 2018 AutoCAD. Since everybody use my settings, I think I might have done something to cause that, but who knows what exactly...
And no, your code still didn't work for my case.
« Last Edit: January 18, 2018, 01:12:16 PM by Red Nova »

Red Nova

  • Newt
  • Posts: 69
Re: exiting while loop with escape. error omitted?
« Reply #28 on: January 18, 2018, 09:32:43 AM »
Red Nova, your original code worked fine for me. Civil3D 2016


Steve

Thanks for the check. Looks like until now it's just me on this topic to face this issue.

Red Nova

  • Newt
  • Posts: 69
Re: exiting while loop with escape. error omitted?
« Reply #29 on: January 18, 2018, 09:59:44 AM »
@ Red Nova:
Maybe you should look at the *push-error-using-command* function.

Need to look into that. Never tried. Thanks.