Results 1 to 10 of 10

Thread: Stop a routine midstream

  1. #1
    100 Club
    Join Date
    2009-04
    Location
    Houston, TX
    Posts
    169

    Default Stop a routine midstream

    How do you stop a routine midstream...

    E.g.

    (if this
    do this and stop the routine
    else, do this and continue with the rest of the code..
    )if

    Of course there's exit and/or quit, but the option where it stops the routine midstream I am displaying stuff at the cmd line so if I exit I get the error quit / exit abort being displayed and I don't want to see that.

  2. #2
    Certified AUGI Addict rkmcswain's Avatar
    Join Date
    2004-09
    Location
    Houston
    Posts
    7,519

    Default Re: Stop a routine midstream

    One way:

    Code:
    
    
    (if (test)
      (do this and then exit)
      (progn
         (do this and then continue)
         (more functions)
         (some more functions)
         (some more functions)
         (even more functions)
         (even more functions)
         (even more functions)
    )
    (princ)
    
    

  3. #3
    Active Member
    Join Date
    2002-12
    Posts
    77

    Default Re: Stop a routine midstream

    Quote Originally Posted by M. Kubitza View Post
    How do you stop a routine midstream...

    E.g.

    (if this
    do this and stop the routine
    else, do this and continue with the rest of the code..
    )if

    Of course there's exit and/or quit, but the option where it stops the routine midstream I am displaying stuff at the cmd line so if I exit I get the error quit / exit abort being displayed and I don't want to see that.
    Look at
    (exit)

    This will stop a routine where ever necessary

  4. #4
    100 Club
    Join Date
    2009-04
    Location
    Houston, TX
    Posts
    169

    Default Re: Stop a routine midstream

    Well, exit is what I was using, the only issue is using exit displays "; error: quit / exit abort" at the cmd line, and I am displaying some stuff at the cmd line already, so the return from (exit) is in the way and I need it gone, if possible.. or stop the routine by some other means but I don't know what that would be if anything.

  5. #5
    I could stop if I wanted to msretenovic's Avatar
    Join Date
    2002-02
    Location
    Galloway, Oh
    Posts
    304

    Smile Re: Stop a routine midstream

    If you use RK's example, you won't need to use EXIT. However, if you do use EXIT, you can use an error handler to catch it and then display nothing. I'll try to explain RK's example a bit more here (comments in red):
    Code:
    ;;check to see which path to take
    (if (test)
      ;;do what you need to here.
      ;;when it is finished, it will
      ;;exit the IF statement
      (do this and then exit)
      ;;use PROGN to encapsulate more
      ;;functions into the IF statement
      (progn
         (do this and then continue)
         (more functions)
         (some more functions)
         (some more functions)
         (even more functions)
         (even more functions)
         (even more functions)
    )
    ;;when you get past the IF statement
    ;;you can safely exit the program
    ;;using a PRINC for a 'silent' exit
     (princ)
    However, if you need to use the EXIT function, you can use an error handler like so (comments in red):
    Code:
    ;;make sure to define *Error* as local
    (defun c:myProg (/ *Error*)
      ;;redefine the global error handler within your routine
      (defun *Error* (msg)
        ;;check for contents of msg
        (cond
          ;;if msg is nil, do nothing
          ((not msg))
          ;;if msg is "quit / exit abort" (the content you want to trap from
          ;;the EXIT function, do nothing
          ;;NOTE: you may get this if the user cancels the routine as well
          ((equal (strcase msg t) "quit / exit abort"))
          ;;if msg is within this list, user may have canceled the function
          ((member (strcase msg t) '("console break" "function canceled"))
            ;;inform the user the routine was canceled
            (princ "*Cancel*\n")
          )
          ;;something else happened, print the error
          ((princ (strcat "\n; error: " msg "\n")))
        )
        ;;'silent' exit
        (princ)
      );;end of *Error*
      ;;-------
      ;;start of main routine
      ;;-------
      (if (test)
        (progn
          (your code here)
          (exit)
        )
        (progn
          (your alternate code here)
          (more of your code here)
          (more of your code here)
          (more of your code here)
        )
      )
      ;;use the error handler for any cleanup,
      ;;including 'silent' exit
      (*Error* nil)
    )
    I will note this, it would be preferable to use RK's suggestion along with the error handler. Using the EXIT function would be used more for an error condition. From the help:
    If exit is called, it returns the error message quit/exit abort and returns to the AutoCAD Command prompt.
    Emphasis added
    If you want to use it in that regard, you can use the error handler method and do something like this code snippet (comments in red):
    Code:
    ;;check for 'custom' error condition
    (if (test-for-error)
      (progn
        ;;if your custom error occurred, call the LOCAL *Error* handler,
        ;;your custom message will be displayed
        (*Error* "Something happened that wasn't supposed to.")
        ;;then call EXIT, nothing should be displayed if the *Error*
        ;;function is defined as above ^
        ;;alternatively, you can have the "quit / exit abort" within the list and
        ;;allow the *Error* handler to issue a "Canceled" to the cmd prompt
        (exit)
      )
    )
    Anyway, there's three different ideas for you to choose from I hope that at least one of them covers what you need.
    Michael K. Sretenović
    Most folks are about as happy as they make up their minds to be.
    -Abraham Lincoln (1809-1865)
    -16th president of The United States of America (1861 - 1865)

  6. #6
    100 Club
    Join Date
    2009-04
    Location
    Houston, TX
    Posts
    169

    Default Re: Stop a routine midstream

    Thanks everyone for the response. RK's code did work yes, I just misread it, and I didn't really want to put the whole rest of the routine inside an if statement, don't ask me why I'm just weird and overly picky like that .

  7. #7
    Certified AUGI Addict rkmcswain's Avatar
    Join Date
    2004-09
    Location
    Houston
    Posts
    7,519

    Default Re: Stop a routine midstream

    Quote Originally Posted by M. Kubitza View Post
    and I didn't really want to put the whole rest of the routine inside an if statement, don't ask me why I'm just weird and overly picky like that .
    Yes, in a large program, it can get hairy trying to keep track of several (if) statements and all the of the parenthesis... In a case like that, you might be better off building subroutines and then calling these from the main function. Then you can put the (if) in the main function and you may only have a few lines of code inside rather than hundreds of lines..

  8. #8
    100 Club
    Join Date
    2009-04
    Location
    Houston, TX
    Posts
    169

    Default Re: Stop a routine midstream

    Quote Originally Posted by rkmcswain View Post
    Yes, in a large program, it can get hairy trying to keep track of several (if) statements and all the of the parenthesis... In a case like that, you might be better off building subroutines and then calling these from the main function. Then you can put the (if) in the main function and you may only have a few lines of code inside rather than hundreds of lines..
    Yes, I've done a fairly large routine (still in progress actually), and in that one it's beyond easy to get lost in the birds nest of (if) (cond) (while) etc. I just started toying with subroutines with the one where I encountered this clean exit issue.

  9. #9
    AUGI Addict alanjt's Avatar
    Join Date
    2008-02
    Posts
    1,073

    Default Re: Stop a routine midstream

    and can help out a lot with that.
    Civil 3D 2011|2012 ~ Windohz 7
    Dropbox | finding the light…

  10. #10
    100 Club
    Join Date
    2009-04
    Location
    Houston, TX
    Posts
    169

    Default Re: Stop a routine midstream

    I use and, or, & not a lot which definitely helps. The most nested junk I've had so far has been 12 "levels", or whatever the technical term is. It didn't take me long to see what someone meant when they said LISP = Lost In Stupid Parenthesis

Similar Threads

  1. Combine three lisp routine into one routine.
    By BrianTFC in forum AutoLISP
    Replies: 1
    Last Post: 2012-02-08, 11:14 AM
  2. Midstream Workset Reassignment
    By atowne in forum Revit Architecture - General
    Replies: 2
    Last Post: 2010-03-01, 01:03 PM
  3. How to Stop the Routine
    By BeKirra in forum AutoLISP
    Replies: 3
    Last Post: 2009-03-04, 08:44 PM
  4. Replies: 9
    Last Post: 2007-06-13, 01:22 PM
  5. Replies: 2
    Last Post: 2007-04-20, 09:51 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •