Author Topic: Abort and return to command prompt  (Read 6073 times)

0 Members and 1 Guest are viewing this topic.

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4075
Re: Abort and return to command prompt
« Reply #15 on: August 09, 2012, 01:07:02 PM »
the above could be combined using the LISP version of AND, once I learn how
Everyone has a photographic memory, Some just don't have film.
They say money can't buy happiness, but it can buy Bacon and that's a close second.
Sometimes the question is more important than the answer. (Thanks Kerry for reminding me)

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: Abort and return to command prompt
« Reply #16 on: August 09, 2012, 01:12:45 PM »
Somewhat contrived use of (exit):

Code: [Select]
(defun _Document_Has_XRefs ( document / result )

    (vl-catch-all-apply
        (function
            (lambda ( )
                (vlax-for block (vla-get-blocks document)
                    (if (eq :vlax-true (vla-get-isxref block))
                        (progn
                            (setq result T)
                            (exit)
                        )
                    )
                )
            )       
        )       
    )

    result

)

Cheers.
I think this would always return nil. Seeing as the first block in the blocks collection would be the model space - which isn't an xref. And thus the exit is called on the first block. Though you do mention it's a contrived usage  ;)

Edit:
*allowing time for post edit before posting correction*
Sorry ... yes I missed the progn when reading this.  :-[ Stupid of me. Sorry MP! It's novel I must say!
« Last Edit: August 09, 2012, 01:31:01 PM by irneb »
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4075
Re: Abort and return to command prompt
« Reply #17 on: August 09, 2012, 01:14:39 PM »
do you have a non-vlisp example?  I haven't learned all the Vlisp commands
Everyone has a photographic memory, Some just don't have film.
They say money can't buy happiness, but it can buy Bacon and that's a close second.
Sometimes the question is more important than the answer. (Thanks Kerry for reminding me)

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: Abort and return to command prompt
« Reply #18 on: August 09, 2012, 01:20:59 PM »
I think this would always return nil. Seeing as the first block in the blocks collection would be the model space - which isn't an xref. And thus the exit is called on the first block.

*allowing time for post edit before posting correction*

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4075
Re: Abort and return to command prompt
« Reply #19 on: August 09, 2012, 01:31:51 PM »
Everyone has a photographic memory, Some just don't have film.
They say money can't buy happiness, but it can buy Bacon and that's a close second.
Sometimes the question is more important than the answer. (Thanks Kerry for reminding me)

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: Abort and return to command prompt
« Reply #20 on: August 09, 2012, 01:48:50 PM »
do you have a non-vlisp example?  I haven't learned all the Vlisp commands
Here's  simplistic idea to show something which happens quite often:
Code - Auto/Visual Lisp: [Select]
  1. (defun c:DrawOneLine  (/ pt1 pt2 osmode cmdecho *error*)
  2.   (defun *error*  (msg)
  3.     (and osmode (setvar "OSMODE" osmode))
  4.     (and cmdecho (setvar "CMDECHO" cmdecho))
  5.     (cond ((not msg))
  6.           ((wcmatch (strcase msg) "*EXIT*,*ABORT*,*QUIT*"))
  7.           (t (princ msg)))
  8.     (princ))
  9.   (if (and (setq pt1 (getpoint "Pick line's start point: "))
  10.            (setq pt2 (getpoint pt1 "Pick line's end point: ")))
  11.     (progn (setq osmode  (getvar "OSMODE")
  12.                  cmdecho (getvar "CMDECHO"))
  13.            (setvar "OSMODE" 0)
  14.            (setvar "CMDECHO" 0)
  15.            (command "_.LINE" pt1 pt2 "")))
  16.   (*error* nil))
There are better way to write the above. But it serves much like RenderMan's situation - i.e. use the *error* routine to clean up even if there was no error.

Say the above is started transparently during another command. The line command would fail with a "Function canceled" error message. The *error* defun would still ensure that osmode & cmdecho are reset back to what they were.

Also if the user presses ESC (or you add an exit, perhaps due to a point not inside a defined area) that message won't be displayed on the command-line. If you want it displayed then omit that wcmatch line, or modify for other messages.
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.

Eddie D.

  • Newt
  • Posts: 29
Re: Abort and return to command prompt
« Reply #21 on: August 09, 2012, 01:58:15 PM »
Not related to your topic, CMDRDUH, but it's good to see someone else on The Swamp that does substation design!

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Abort and return to command prompt
« Reply #22 on: August 09, 2012, 04:20:17 PM »
the above could be combined using the LISP version of AND, once I learn how

Perhaps something like this David

Code - Auto/Visual Lisp: [Select]
  1.  
  2.         (= (strcase bshgt) "LOW")
  3.         (< *LOWBUS* 98)
  4.     )
  5.     (progn
  6.         (alert "Too Low!")
  7.         (exit)
  8.     )
  9. )
  10.  
  11.  

Just yell if you need help

Stay well,
Kerry
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

David Hall

  • Automatic Duh Generator
  • King Gator
  • Posts: 4075
Re: Abort and return to command prompt
« Reply #23 on: August 09, 2012, 04:25:05 PM »
Thats the ticket Kerry! thanks.  I knew it would be easy.  I am still thinking in .Net and cant remember the old LISP ways.
Everyone has a photographic memory, Some just don't have film.
They say money can't buy happiness, but it can buy Bacon and that's a close second.
Sometimes the question is more important than the answer. (Thanks Kerry for reminding me)

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: Abort and return to command prompt
« Reply #24 on: August 09, 2012, 05:22:17 PM »
Here is how I might code it:

Code - Auto/Visual Lisp: [Select]
  1.     (   (and (= (strcase bshgt) "LOW") (< *LOWBUS* 98))
  2.         (alert "Too Low!")
  3.     )
  4.     (   t
  5.         <Rest of Program>
  6.     )
  7. )

This avoids the need to use exit to force an error since the program will cease evaluation of the cond expression when a test expression returns a non-nil value. Of course, in this particular case where there is only a single condition to be tested, an if expression could also be used in place of the cond expression, however, usually there is more than one error condition to be checked, and so cond becomes more suitable (and readable) than multiple nested if statements.

In fact, I use this construct in almost all of my programs which utilise DCL, as, in my opinion, it is cleaner than using exit, doesn't necessarily require an *error* handler, and furthermore retains the flow of the code (i.e. the program is being evaluated line by line rather than aborting).

My 2p  :-)

owenwengerd

  • Bull Frog
  • Posts: 451
Re: Abort and return to command prompt
« Reply #25 on: August 09, 2012, 06:21:52 PM »
Here is how I might code it:

Code - Auto/Visual Lisp: [Select]
  1.     (   (and (= (strcase bshgt) "LOW") (< *LOWBUS* 98))
  2.         (alert "Too Low!")
  3.     )
  4.     (   t
  5.         <Rest of Program>
  6.     )
  7. )

I think this makes code more readable:

Code - Text: [Select]
  1. (if (and (= (strcase bshgt) "LOW") (< *LOWBUS* 98))
  2.   (progn (alert "Too Low!") (exit)))
  3.  
  4. (if (failed-p (test1))
  5.   (exit))
  6.  
  7. (if (failed-p (test2))
  8.   (exit))
  9.  

I want the error condition to cause an immediate failure without getting propagated and without being hidden intside a fancy structure (or indented 20 spaces), and I want it to be obvious when scanning the code 30 years later what the control flow is here.

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: Abort and return to command prompt
« Reply #26 on: August 09, 2012, 06:33:28 PM »
I shan't argue with your evident programming expertise Owen and I respect your opinion, but I can't say that this structure:

Code - Auto/Visual Lisp: [Select]
  1. (if (failed-p (test1))
  2.     (progn
  3.         (princ "\nTest1 failed.")
  4.         (exit)
  5.     )
  6. )
  7.  
  8. (if (failed-p (test2))
  9.     (progn
  10.         (princ "\nTest2 failed.")
  11.         (exit)
  12.     )
  13. )
  14.  
  15. (if (failed-p (test2))
  16.     (progn
  17.         (princ "\nTest1 failed.")
  18.         (exit)
  19.     )
  20. )

is any more readable than:

Code - Auto/Visual Lisp: [Select]
  1.     (   (failed-p (test1))
  2.         (princ "\nTest1 failed.")
  3.     )
  4.     (   (failed-p (test2))
  5.         (princ "\nTest2 failed.")
  6.     )
  7.     (   (failed-p (test3))
  8.         (princ "\nTest3 failed.")
  9.     )
  10. )

I don't think the level of indentation plays any part since both would have to be indented to the level at which the conditions are to be tested in any case.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Abort and return to command prompt
« Reply #27 on: August 09, 2012, 10:21:29 PM »
< .. >
I want the error condition to cause an immediate failure without getting propagated and without being hidden intside a fancy structure (or indented 20 spaces), and I want it to be obvious when scanning the code 30 years later what the control flow is here.

I concur !


I shan't argue with your evident programming expertise Owen and I respect your opinion, but I can't say that this structure:

Code - Auto/Visual Lisp: [Select]
  1. (if (failed-p (test1))
  2.     (progn
  3.         (princ "\nTest1 failed.")
  4.         (exit)
  5.     )
  6. )
  7.  
  8. (if (failed-p (test2))
  9.     (progn
  10.         (princ "\nTest2 failed.")
  11.         (exit)
  12.     )
  13. )
  14.  
  15. (if (failed-p (test2))
  16.     (progn
  17.         (princ "\nTest1 failed.")
  18.         (exit)
  19.     )
  20. )

is any more readable than:

Code - Auto/Visual Lisp: [Select]
  1.     (   (failed-p (test1))
  2.         (princ "\nTest1 failed.")
  3.     )
  4.     (   (failed-p (test2))
  5.         (princ "\nTest2 failed.")
  6.     )
  7.     (   (failed-p (test3))
  8.         (princ "\nTest3 failed.")
  9.     )
  10. )

I don't think the level of indentation plays any part since both would have to be indented to the level at which the conditions are to be tested in any case.

Lee, You're assuming the assertion tests can all be collected together.
I've never had a situation where that can be achieved that I recall.
... Particularly when dealing with code that is any considerable length.

Regards
Kerry
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Abort and return to command prompt
« Reply #28 on: August 09, 2012, 10:27:16 PM »

Just for an option.
This is really old .. and it is called innumerable times each day.

Code - Auto/Visual Lisp: [Select]
  1.  
  2. ;;;------------------------------------------------------------------
  3. ;;;------------------------------------------------------------------
  4. ;;;
  5. ;;; Spit the Dummy if 'testStatement evaluates to nil
  6. ;;; argument must be quoted
  7. ;;;   (setq var 0)
  8. ;;;   (KDUB:assert 'var "Symbol 'VAR' has no value assigned.")
  9. ;;;   (KDUB:assert '(not(zerop var)) "Symbol 'VAR' evaluates to 0 ... this is illegal ... go to jail")
  10. ;;;   (KDUB:assert '(> var 0) nil)
  11. ;;;   (KDUB:assert '(< var 1) nil)
  12. (defun kdub:assert (teststatement message)
  13.   (if (not (eval teststatement))
  14.     (progn (kdub:display-message
  15.              " **** Assertion Failure"
  16.              (if message
  17.                message
  18.                (list (vl-prin1-to-string teststatement) " ==>> nil/null")
  19.              )
  20.            )
  21.            ;; decide what to do here later ?
  22.            (if kglobal:debug_on
  23.              (princ "  ")
  24.              (exit)
  25.            )
  26.     )
  27.   )
  28.   (princ)
  29. )
  30. ;;;------------------------------------------------------------------
  31. ;;;------------------------------------------------------------------
  32.  
  33.  
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Abort and return to command prompt
« Reply #29 on: August 09, 2012, 10:45:47 PM »

This goes with that :)

Code - Auto/Visual Lisp: [Select]
  1.  
  2. ;;;------------------------------------------------------------------
  3. ;;;
  4. ;;; (KDUB:display_formatted_message prefix mess )
  5. ;;; kwb 20020715
  6. ;|
  7. prefix : String or nil
  8. mess   : atom or list comprising STRing, INT, REAL,
  9.          SYMbol, ENAME, VLA-OBJECT, FILEid etc
  10. |;
  11. ;;;
  12. ;;; Print a formatted message to the command line
  13. ;;;
  14.  
  15. (defun kdub:display-message (prefix mess)
  16.           (list "\n"
  17.                 (if (= (type prefix) 'str)
  18.                   prefix
  19.                   "; **** ERROR"
  20.                 )
  21.                 ": "
  22.           )
  23.   )
  24.   (if (vl-consp mess)
  25.     (mapcar 'princ mess)
  26.     (princ mess)
  27.   )
  28.   (princ)
  29. )
  30.  
  31. ;;;------------------------------------------------------------------
  32. ;;;------------------------------------------------------------------
  33.  

.. thanks John boy :)
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.