Jump to content

How can I close a lisp with variables reset if the routine is escaped out of?


yrnomad

Recommended Posts

I'm trying to make this routine not fail to reset the osmode variable if the user cancels the function between the arrow and the text inputs. How would I do that?

 

;;;****************HR*************************
(defun c:hR (/ otmd osmd a b c)

       (setq otmd (getvar "orthomode"))
     (setq osmd (getvar "osmode"))
     (command "ortho" "off")
     (setvar "osmode" 695)
 (setq a (* 0.0333 (getvar "userr1")))
 (setq b (* 0.1000 (getvar "userr1")))
 (setq c (* 0.1 (getvar "userr1")))
 (command "pline" pause "arc"  "s" pause pause "L" "w" a 0.0 "L" b "")
       ;(setvar "osmode" 0)
       (setq textstart (getpoint "\nText start point: "))
   (command "textstyle" "romans")
       (command "dtext" textstart c "")
       (setvar "orthomode" otmd)
     (setvar "osmode" 695)
 );end homerun.

Link to comment
Share on other sites

One way is to use an error trap. Have a look at http://www.afralisp.net/lispa/lisp6.htm.

 

;;;****************HR*************************
(defun c:hR (/ otmd osmd a b c
        [color=Blue]temperr[/color])

       (setq otmd (getvar "orthomode"))
     (setq osmd (getvar "osmode"))

[color=Blue] (setq temperr *error*) ;store *error*
 (setq *error* trap1) ;re-assign *error*[/color]

     (command "ortho" "off")
     (setvar "osmode" 695)
 (setq a (* 0.0333 (getvar "userr1")))
 (setq b (* 0.1000 (getvar "userr1")))
 (setq c (* 0.1 (getvar "userr1")))
 (command "pline" pause "arc"  "s" pause pause "L" "w" a 0.0 "L" b "")
       ;(setvar "osmode" 0)
       (setq textstart (getpoint "\nText start point: "))
   (command "textstyle" "romans")
       (command "dtext" textstart c "")
       (setvar "orthomode" otmd)
     (setvar "osmode" 695)

[color=Blue]  (setq *error* temperr) ;restore *error*[/color]
 );end homerun.


[color=Blue]; ERROR TRAP
(defun trap1 (errmsg)                    ;define function
   (setq *error* temperr)                ;restore *error*
     (setvar "orthomode" otmd)            ;restore variable
   (setvar "osmode" osmd)                ;restore variable
   (prompt "\nAn unknown error has occured or Escape has been pressed.\nProgram cancelled.\n")    ;inform user
  (princ)
)[/color]

Not tested.

Link to comment
Share on other sites

Hi,

 

Another way, nesting the redefined *error* function and declaring it local

 

;;;****************HR*************************
(defun c:hR (/ [color=blue]*error*[/color] otmd osmd a b c)

 [color=blue];; redefine *error*
 (defun *error* (msg)
   (if (and msg (/= msg "Function cancelled"))
     (princ (strcat "\nError: " msg))
   )
   (setvar "orthomode" otmd)
   (setvar "osmode" osmd)
   (princ)
 )[/color]

 (setq otmd (getvar "orthomode"))
 (setq osmd (getvar "osmode"))
 (command "ortho" "off")
 (setvar "osmode" 695)
 (setq a (* 0.0333 (getvar "userr1")))
 (setq b (* 0.1000 (getvar "userr1")))
 (setq c (* 0.1 (getvar "userr1")))
 (command "pline" pause "arc" "s" pause pause "L" "w" a 0.0 "L" b "")
 ;; (setvar "osmode" 0)
 (setq textstart (getpoint "\nText start point: "))
 (command "textstyle" "romans")
 (command "dtext" textstart c "")
 [color=blue](*error* nil) ;_ restores variable calling *error*[/color]
) ;_ end homerun.

Link to comment
Share on other sites

give this a whirl:

 

;;;****************HR*************************
(defun c:hR (/ *error* otmd osmd a b c)

;;; error handler
 (defun *error* (#Message)
   (and otmd (setvar "orthomode" otmd))
   (and osmd (setvar "osmode" osmd))
   (and
     #Message
     (or (member
           #Message
           '("console break" "Function cancelled" "quit / exit abort")
         ) ;_ member
         (princ (strcat "\nError: " #Message))
     ) ;_ or
   ) ;_ and
 ) ;_ defun

     (setq otmd (getvar "orthomode"))
     (setq osmd (getvar "osmode"))
     (sevar "orthomode" 0)
     (setvar "osmode" 695)
 (setq a (* 0.0333 (getvar "userr1")))
 (setq b (* 0.1000 (getvar "userr1")))
 (setq c (* 0.1 (getvar "userr1")))
 (command "pline" pause "arc"  "s" pause pause "L" "w" a 0.0 "L" b "")
       ;(setvar "osmode" 0)
       (setq textstart (getpoint "\nText start point: "))
   (command "textstyle" "romans")
       (command "dtext" textstart c "")
     ;; reset variables
     (*error* nil)
 );end homerun.

*error* is a user redefinable subroutine. now, if you exit out of your routine, it will reset all variables (if defined). the MOST important thing you MUST REMEMBER is to localize the *error* handler. you want the original one after the routine is over.

 

i also switched out your use of command for setvar on orthomode.

Link to comment
Share on other sites

I sometimes use WCMATCH instead of member/vl-position:

 

(if (not (wcmatch (strcase msg) "*BREAK,*CANCEL*,*EXIT*"))
 (princ (strcat "\n** Error: " msg " **")))

 

 

:)

Link to comment
Share on other sites

I sometimes use WCMATCH instead of member/vl-position:

 

(if (not (wcmatch (strcase msg) "*BREAK,*CANCEL*,*EXIT*"))
 (princ (strcat "\n** Error: " msg " **")))

:)

 

another fine option. i wonder if one is one yields any better results.

Link to comment
Share on other sites

I just find that it is not as verbose... o:)

to each his own. i've seen the wcmatch way before, it's another nice method.

that's the great thing about programming, you can accomplish the same thing 50 different ways.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...