TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: ribarm on May 04, 2020, 10:43:57 AM

Title: LISP sometimes not possible to kill with ESC...
Post by: ribarm on May 04, 2020, 10:43:57 AM
I have this question from title...
Very often in order to perform fast execution I don't open VLIDE and therefore, sometimes with ESC CAD can't be interuppted to break routine - my screen goes white - pale and I must terminate CAD... I want to set routine in the way it never comes to this... For example I have very extensive calculations to perform that will take very long time... So patial calculations can be stored in some global variable cumulative while routine works, but then I want to break it with ESC... If screen goes white, I have 2 options either to wait to finish and other one to close CAD... Both of these options are bad for me... I want ESC - BREAK, QUIT, EXIT routine and CAD remains active with my global variable with data stored all the way until ESC...
Is this possible with some kind of special error handler or something I don't know yet...
Thanks for reading, M.R.
Title: Re: LISP sometimes not possible to kill with ESC...
Post by: MP on May 04, 2020, 11:03:22 AM
Visual BASIC sports the DoEvents method which gives other processes an opportunity to execute. LISP doesn't. However, Lee tried using (command ".delay" 0) and found it kind of fakes same - at minimum it prevents or minimizes the ghosting; seemingly stalled appearance. I wrapped it in a defun and place it in any process intensive loops I execute, typically on a "perform every nth loop" basis. For what it's worth:

Code: [Select]
(defun _do-events ( / cmdecho )
    (setq cmdecho (getvar 'cmdecho))
    (setvar 'cmdecho 0)
    (gc) ;; hello jtoverka
    (repeat 2 (command ".delay" 0) (princ ""))
    (setvar 'cmdecho cmdecho)
    (princ)
)
Title: Re: LISP sometimes not possible to kill with ESC...
Post by: ribarm on May 04, 2020, 11:50:04 AM
Thanks MP...
That's exactly what I was looking for...
Title: Re: LISP sometimes not possible to kill with ESC...
Post by: MP on May 04, 2020, 11:53:09 AM
Thanks Lee / MP...
That's exactly what I was looking for...

YW!
Title: Re: LISP sometimes not possible to kill with ESC...
Post by: roy_043 on May 04, 2020, 02:43:36 PM
'Original' topic: https://www.theswamp.org/index.php?topic=50848.0

What is the reason for repeat?
Title: Re: LISP sometimes not possible to kill with ESC...
Post by: ribarm on May 04, 2020, 03:09:17 PM
'Original' topic: https://www.theswamp.org/index.php?topic=50848.0

What is the reason for repeat?

Thanks for the link Roy... This is the reason why I needed that info : https://www.cadtutor.net/forum/topic/70417-make-pat-files/?do=findComment&comment=565434
Title: Re: LISP sometimes not possible to kill with ESC...
Post by: MP on May 04, 2020, 03:36:28 PM
What is the reason for repeat?

It’s a Jacqueline Susann thing.
Title: Re: LISP sometimes not possible to kill with ESC...
Post by: ribarm on May 05, 2020, 12:48:26 AM
To my newest testings, simple error handler is enough to force BREAK with ESC - i.e. "Function canceled" error...

I had to revise my code posted at cadtutor as this suggestion by MP dramatically slows loop calculations...

I only hope that I am right with error handler... Haven't still tested long enough, but it seems that ESC works as desired...
Title: Re: LISP sometimes not possible to kill with ESC...
Post by: MP on May 05, 2020, 01:07:11 AM
... this suggestion by MP dramatically slows loop calculations ...

... typically on a "perform every nth loop" basis ...

e.g. (if (zerop (rem (setq i (1+ i)) 5000)) (_do-events))
Title: Re: LISP sometimes not possible to kill with ESC...
Post by: ribarm on May 05, 2020, 01:19:54 AM
You are right MP... I've retested and white screen again appeared... But then again, I have to do testings for BricsCAD... For now it looks that white screen won't appear if there is error handler... Still I have to do intensive checking...
Title: Re: LISP sometimes not possible to kill with ESC...
Post by: ribarm on May 05, 2020, 01:24:37 AM
You are right MP... I've retested and white screen again appeared... But then again, I have to do testings for BricsCAD... For now it looks that white screen won't appear if there is error handler... Still I have to do intensive checking...

Sh** it appeared and in BricsCAD... I think I'll go now your way, just don't know if 5000 is correct number...
Title: Re: LISP sometimes not possible to kill with ESC...
Post by: MP on May 05, 2020, 01:59:29 AM
... don't know if 5000 is correct number ...

There is no “one size fits all” number as it’s context and hardware dependent. Non exhaustive testing will reveal a practical value to use.
Title: Re: LISP sometimes not possible to kill with ESC...
Post by: ribarm on May 05, 2020, 02:02:15 AM
... don't know if 5000 is correct number ...

There is no “one size fits all” number as it’s context and hardware dependent. Non exhaustive testing will reveal a practical value to use.

I've finally revised my code at cadtutor and I hardcoded 5000, so what the hell (we don't know users PC specs...)...
Title: Re: LISP sometimes not possible to kill with ESC...
Post by: MP on May 05, 2020, 02:19:02 AM
I've finally revised my code at cadtutor and I hardcoded 5000, so what the hell (we don't know users PC specs...)...

Find what works well on your machine (e.g. 5000) and then halve it (e.g. 2500), assuming the worst pc it will run on sports half the performance of yours ...
Title: Re: LISP sometimes not possible to kill with ESC...
Post by: ribarm on May 06, 2020, 10:05:18 AM
When I run my LISP from BricsCAD, I get this error :

Code: [Select]
...Unknown Error in Lisp or CAD system or 'Stack Overflow'

What can I do?
In AutoCAD it works well, but I wanted faster computing and to compare results, i.e. *.pat files...
Title: Re: LISP sometimes not possible to kill with ESC...
Post by: ribarm on May 06, 2020, 10:35:48 AM
Quote
Code: [Select]
...Unknown Error in Lisp or CAD system or 'Stack Overflow'


Here is my testing project in ZIP...
BricsCAD can hatch *.pat file, but AutoCAD can't... But AutoCAD calculated *.pat and BricsCAD failed with above mentioned error...
Do happen to know why there are errors in BricsCAD presentation of *.pat file... According to LISP there should be no errors and lacks... If you see something in my code that you think is bad, please enlighten me...
I thought that BricsCAD will calculate differently, but it failed... And BTW, I tried to interrupt calculation and when I appended *.txt file to *.pat I did get differently then *.pat calculation in one go... So still somethings wrong with my codes, but I can't quite find out what...

[EDIT : Superman *.pat file was attached to this post at cadtutor... I had to scale down DWG 10x smaller size and do fixing lines to SNAP points of 0.5,0.5, before applying lastly posted code from cadtutor link... ACAD did the job corrctly and BCAD didn't do like ACAD... So altough slower ACAD is still better IMHO...
Here is the link for *.pat file : https://www.cadtutor.net/forum/topic/70417-make-pat-files/?do=findComment&comment=574183
]
Title: Re: LISP sometimes not possible to kill with ESC...
Post by: ribarm on May 13, 2020, 08:03:15 AM
Just to inform... This topic is still not solved... I run my routine again and still white screen appears after ESC... So this thing with (_do-events) doesn't really solves it... We need more methods and options to consider... Yes the problem is (while/repeat/foreach/recursion) looping, but how to solve this problem more reliably? Note sometimes this thingy (_do-events) is not applicable (foreach/recursion)... So are there any new tricks?

[EDIT : I am not saying that it isn't possible to implement (_do-events) and for (foreach/recursion), but the thing is that it slows things and still not 100% reliable...]
Title: Re: LISP sometimes not possible to kill with ESC...
Post by: VovKa on May 13, 2020, 09:58:08 AM
Marko you want to do-events only with lisp? no arx?
Title: Re: LISP sometimes not possible to kill with ESC...
Post by: ribarm on May 13, 2020, 10:46:24 AM
Marko you want to do-events only with lisp? no arx?

I want to make this routine working as it's supposed to, so that when I hit ESC, it quits 100% reliably without going to white screen (Windows application - not responding - i.e. AutoCAD)...
https://www.cadtutor.net/forum/topic/70417-make-pat-files/?do=findComment&comment=565434
Title: Re: LISP sometimes not possible to kill with ESC...
Post by: MP on May 13, 2020, 10:56:54 AM
;;  My very quick perusal of your code suggests you're using (_do-events) very
;;  heavy handedly, i.e. invoking it every pass thru iteration loops. Do this instead:

;;  before loop

(setq
    i      0
    cycles 5000 ;; determine optimal value empirically
)


;; in processing loop

(if (zerop (rem (setq i (1+ i)) cycles)) (_do-events))

;;  cheers
Title: Re: LISP sometimes not possible to kill with ESC...
Post by: ribarm on May 13, 2020, 11:25:08 AM
I am doing what you suggested... And I've put 2000 instead of 5000, so I am betting on better response on ESC... I hope my code is then finished as I wanted. Now if white screen pops up again, I hope it's not by me and badly coding...  :mrgreen:
Title: Re: LISP sometimes not possible to kill with ESC...
Post by: MP on May 13, 2020, 11:41:26 AM
I am doing what you suggested...

Not entirely. You defined the do-events function using vl-cmdf instead of command. Use the latter.
Title: Re: LISP sometimes not possible to kill with ESC...
Post by: MP on May 13, 2020, 12:04:02 PM
Really long processes might benefit from this if AutoCAD loses focus:

Code: [Select]
(defun _doc ( )
    (vl-load-com)
    (setq *doc* (vla-get-activedocument (vlax-get-acad-object)))
    (defun _doc ( ) *doc*)
    *doc*
)

(defun _do-events ( / cmdecho )
    (setq cmdecho (getvar 'cmdecho))
    (setvar 'cmdecho 0)
    (gc)
    (vla-activate (_doc)) ;; <<--- new
    (repeat 2 (command ".delay" 0) (princ ""))
    (setvar 'cmdecho cmdecho)
    (princ)
)

Concept code; untested.
Title: Re: LISP sometimes not possible to kill with ESC...
Post by: ribarm on May 13, 2020, 12:43:08 PM
It's just me...
I prefer using (vl-cmdf) instead of both (command) and (command-s) predominately from this reasons...
In later versions of AutoCAD (command-s) was suggested to be used in error handlers if you want to use command calls, but in older versions (command-s) isn't implemented... So I experimented and I came to conclusion that (vl-cmdf) also behaves correctly in error handlers and BTW. it exist in all ACAD versions since also (command) function was introduced in ALISP... Further investigations showed that timings of execution was fastest with (vl-cmdf) - just very very small difference than (command-s)... (command) was slowest according to my testings... So if you think more over it, no matter it returns T and not nil, (vl-cmdf) can very well substitute both (command) and (command-s)... One more fact in favor of (vl-cmdf) : (command-s) must have supplied all tokens in single expression wihout breaking syntax... (vl-cmdf) here bahaves like (command), you simply can use it like this :

Code: [Select]
(vl-cmdf "_.PLINE")
(foreach p pl
  (vl-cmdf "_non" p)
)
(vl-cmdf "")

Or this snippet :

Code: [Select]
(vl-cmdf "_.PLINE")
(while (< 0 (getvar 'cmdactive))
  (vl-cmdf "\\")
)

(command-s) can't imitate this behaviour...
You'd have to do someting like this :

Code: [Select]
(apply 'command-s (cons "_.PLINE" (append (apply 'append (mapcar '(lambda ( x ) (list "_non" x)) pl)) (list ""))))

But that syntax is crazy IMHO...
Title: Re: LISP sometimes not possible to kill with ESC...
Post by: MP on May 13, 2020, 12:57:48 PM
While I use vl-cmdf / command-s where applicable in my coding none of the "benefits" are applicable for this context imo, especially speed. Cheers.
Title: Re: LISP sometimes not possible to kill with ESC...
Post by: VovKa on May 13, 2020, 04:26:00 PM
I want to make this routine working as it's supposed to
this is clear

what i meant was that you can try onidleacad function from ProgressBars arx library by Alexander Rivilis
original post https://www.theswamp.org/index.php?topic=8570.msg115457#msg115457
latest version http://www.maestrogroup.com.ua/support/ProgressBars.zip
Title: Re: LISP sometimes not possible to kill with ESC...
Post by: MP on May 13, 2020, 04:48:06 PM
what i meant was that you can try onidleacad function from ProgressBars arx library by Alexander Rivilis
original post https://www.theswamp.org/index.php?topic=8570.msg115457#msg115457
latest version http://www.maestrogroup.com.ua/support/ProgressBars.zip

Nice, Alexander Rivilis' work is top notch.

If you're going the third party route there's also DOS_LIB's dos_getprogress function:

(http://i.ibb.co/y6nJR3D/dos-getprogress.png)
Title: Re: LISP sometimes not possible to kill with ESC...
Post by: ribarm on January 24, 2021, 05:06:46 AM
I want to ask someone, can this issue be sent to AutoDesk for consideration for future... Simply I want that ACAD handles ESC within itself by terminating routine workflow and not to make ACAD not responding and making Windows in charge from which you can only terminate ACAD session... I want session active, terminated routine and nothing more... So can this (while, repeat, foreach... recursion) looping be implemented differently not to disturb its performance, but to allow termination...
I am reviving this topic as it's so evidently important and incredibly relevant for working with LISP and ACAD generally... When I say ESC for termination, I also mean every routine *.dll, *.lsp, *.arx, *.vlx, ...
Title: Re: LISP sometimes not possible to kill with ESC...
Post by: BIGAL on January 24, 2021, 07:09:41 PM
Agree with you sometimes accidently send a lisp into a infinite loop no exit and majority of times a kill via task manager is only way to stop, recognising Ctrl+break would be helpful.