Author Topic: Crash and Burn....  (Read 5897 times)

0 Members and 1 Guest are viewing this topic.

Water Bear

  • Guest
Crash and Burn....
« on: February 11, 2004, 09:29:52 AM »
I've written a few routines that repeat in a loop until a null is entered...problem is that I always forget to respond the last time. I'll usually select another menu item and I crash!

 Ideas anyone?
Code: [Select]
(defun rd (/ pt1 pt2 pt3 dia)
  ;(init)
  (setvar "cmdecho" 0)
  (setvar "blipmode" 1)
  (setvar "menuecho" 1)
  (setvar "highlight" 1)
  (setvar "osmode" 32)
  (command "layer" "m" "AR-Lights" "c" "5" "" "lw" ".25" "" "")


  (setq dia (/ (getdist "\nwhat is the diameter of the light?: ") 2)
)
  (setq pt1 (getpoint "\nSelect first point for midpoint location: "))

  (while pt1
   
    (setq pt2 (getpoint pt1 "\nSelect opposite corner: ")
 pt3 (mapcar (function (lambda (pt1 pt3) (/ (+ pt1 pt3) 2.0)))
     pt1
     pt2
     )
 ) ;setq
    (command "circle" pt3 dia "circle" pt3 (- dia 1))
    (redraw)

    (setq pt1 (getpoint "\nSelect first point for midpoint location: "))

    (command "undo" "m")
    ) ;while
  ;(reset)
  (princ)
  ) ;defun
(princ)

daron

  • Guest
Crash and Burn....
« Reply #1 on: February 11, 2004, 09:31:15 AM »
Code anyone? No idea, unless you post the code.

Craig

  • Guest
Crash and Burn....
« Reply #2 on: February 11, 2004, 09:37:08 AM »
Can't you just use error trapping to fix this? Of course posting the code would help. It seems what you could do is when the expected variable isn't presented and or a null isn't entered, then make the program exit without crashing.

Water Bear

  • Guest
Crash and Burn....
« Reply #3 on: February 11, 2004, 09:43:07 AM »
added code...

daron

  • Guest
Crash and Burn....
« Reply #4 on: February 11, 2004, 09:55:18 AM »
See if this'll help.
Code: [Select]
(defun rd (/ pt1 pt2 pt3 dia)
  (setvar "cmdecho" 0)
  (setvar "blipmode" 1)
  (setvar "menuecho" 1)
  (setvar "highlight" 1)
  (setvar "osmode" 32)
  (command "layer" "m" "AR-Lights" "c" "5" "" "lw" ".25" "" "")

(initget);check the help files to make this better.
  (setq   dia (/ (getdist "\nwhat is the diameter of the light?: ") 2)
   )
(initget)
  (setq pt1 (getpoint "\nSelect first point for midpoint location: "))

  (while pt1
   (initget)
    (setq pt2 (getpoint pt1 "\nSelect opposite corner: ")
     pt3 (mapcar (function (lambda (pt1 pt3) (/ (+ pt1 pt3) 2.0)))
            pt1
            pt2
            )
     )            ;setq
    (command "circle" pt3 dia "circle" pt3 (- dia 1))
    (redraw)
(initget)
    (setq pt1 (getpoint "\nSelect first point for midpoint location: "))

    (command "undo" "m")
    )               ;while
  ;(reset)
  (princ)
  )               ;defun
(princ)


Although, I haven't tried the code, but I'm not sure why you're resetting the pt1 variable in the while loop.

Mark

  • Custom Title
  • Seagull
  • Posts: 28762
Crash and Burn....
« Reply #5 on: February 11, 2004, 10:01:18 AM »
another way to do it.
Code: [Select]

(defun rd (/ pt1 pt2 pt3 dia) ;(init)
    (setvar "cmdecho" 0)
    (setvar "blipmode" 1)
    (setvar "menuecho" 1)
    (setvar "highlight" 1)
    (setvar "osmode" 32)
    (command "layer" "m" "AR-Lights" "c" "5" "" "lw" ".25" "" "")
    (setq dia (/ (getdist "\nwhat is the diameter of the light?: ") 2))
    (while
      (setq pt1 (getpoint "\nSelect first point for midpoint location: "))

      (setq pt2 (getpoint pt1 "\nSelect opposite corner: ")
   pt3 (mapcar (function (lambda (pt1 pt3) (/ (+ pt1 pt3) 2.0)))
pt1
pt2
)
   ) ;setq
      (command "_.circle" pt3 dia "")
      (command "_.circle" pt3 (- dia 1) "")
      (redraw)
      (command "undo" "m")
      ) ;while
 ;(reset)
    (princ)
    ) ;defun
(princ)
TheSwamp.org  (serving the CAD community since 2003)

Water Bear

  • Guest
Crash and Burn....
« Reply #6 on: February 11, 2004, 10:39:11 AM »
Mark,
I rewrote it as you recommend, it certainly looks much cleaner. But, just a note, I was getting an error after each circle command..so I removed the """" from both and it now works fine...
Code: [Select]
(defun rd (/ pt1 pt2 pt3 dia)
;(init)
  (setvar "cmdecho" 0)
  (setvar "blipmode" 1)
  (setvar "menuecho" 0)
  (setvar "highlight" 1)
  (setvar "osmode" 32)
  (setvar "orthomode" 0)
  (command "layer" "m" "AR-Lights" "c" "5" "" "lw" ".25" "" "")
  (initget 1)
  (setq dia (/ (getdist "\nWhat is the diameter of the light?: ") 2))
  (while
    (setq pt1 (getpoint "\nSelect first point for midpoint location: "))

     (setq pt2 (getpoint pt1 "\nSelect opposite corner: ")
  pt3 (mapcar (function (lambda (pt1 pt3) (/ (+ pt1 pt3) 2.0)))
      pt1
      pt2
      )
  ) ;setq
     (command "_.circle" pt3 dia)
     (command "_.circle" pt3 (- dia 1))
     (redraw)
     (command "undo" "m")
     ) ;while
;(reset)
  (princ)
  ) ;defun
(princ)

Daron,
I added the initget at the beginning but I get an error if I add it in the while.

daron

  • Guest
Crash and Burn....
« Reply #7 on: February 11, 2004, 11:11:34 AM »
Hmm. Okay.

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Crash and Burn....
« Reply #8 on: February 11, 2004, 11:29:17 AM »
The (initget 1) can be added to the while loop as Daron said to prevent
exit by pressing Enter. If that was your desire.

Code: [Select]
(defun rd (/ pt1 pt2 pt3 dia)
               ;(init)
  (setvar "cmdecho" 0)
  (setvar "blipmode" 1)
  (setvar "menuecho" 0)
  (setvar "highlight" 1)
  (setvar "osmode" 32)
  (setvar "orthomode" 0)
  (command "layer" "m" "AR-Lights" "c" "5" "" "lw" ".25" "" "")
  (initget 1)
  (setq dia (/ (getdist "\nWhat is the diameter of the light?: ") 2))
  (initget 1); <- CAB, prevent exit on point 1
  (while  (setq pt1 (getpoint "\nSelect first point for midpoint location: "))
     (initget 1); <- CAB, prevent exit on point 2
     (setq pt2 (getpoint pt1 "\nSelect opposite corner: ")
           pt3 (mapcar (function (lambda (pt1 pt3) (/ (+ pt1 pt3) 2.0))) pt1 pt2)
     );setq
     (command "_.circle" pt3 dia)
     (command "_.circle" pt3 (- dia 1))
     (redraw)
     (command "undo" "m")
     (initget 1); <- CAB, prevent exit on point 1
  );while
  ;(reset)
  (princ)
  );defun
(princ)
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

Water Bear

  • Guest
Crash and Burn....
« Reply #9 on: February 11, 2004, 12:08:34 PM »
CAB,

That works well, my mistake (I had the initget immediately after the while)

I actually DO want to exit with a null (or if another menu option is selected)..but when I do this the error routine runs, is that an acceptable way to exit?

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Crash and Burn....
« Reply #10 on: February 11, 2004, 12:50:05 PM »
That will have to be handled through your error routine.
add this to your prg load and close VLIDE when you test it.

Your routine will be canceled and the picked button will be activated.

You realy need to reset your system vars in the error routine.

I'm a little weak in the error handling department so maybe someone can
guide you better.

CAB

Code: [Select]
;; error function & Routine Exit
(defun *error* (msg)
  (if
    (not
      (member
msg
'("console break" "Function cancelled" "quit / exit abort" "")
      )
    )
     (princ (strcat "\nError: " msg))
  ) ; end if
  (command)
  (command) ; cancels any command in progress
  ;;*********************
  ;;  reset all variables here
  ;;*********************
  (princ)
) ;end error function
(princ)
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

SMadsen

  • Guest
Crash and Burn....
« Reply #11 on: February 11, 2004, 01:30:20 PM »
Aggreeing with CAB. It's not only an acceptable way to exit, it's also necessary in this case. You set alot of system variables that need to be reset.

However, a little error handling in strategic places can do wonders. Try to break the version of your routine below. Hit cancel or select menu items all you want while it's running.
This may look like overkill - and it probably is - but it's all about thinking in errors.

Code: [Select]
(defun rd (/ pt1 pt2 pt3 dia cmd blip mnu hilite osm lay)
  (defun setvars (lst)
    (mapcar 'setvar '("CMDECHO"      "BLIPMODE"     "MENUECHO"
                      "HIGHLIGHT"    "OSMODE"       "CLAYER")
            lst
    )
  )
  (mapcar 'set '(cmd blip mnu hilite osm lay)
          (mapcar 'getvar '("CMDECHO"      "BLIPMODE"     "MENUECHO"
                            "HIGHLIGHT"    "OSMODE"       "CLAYER")
          )
  )
  (setvars (list 0 1 1 1 32))
  (command "layer" "m" "AR-Lights" "c" "5" "" "lw" ".25" "" "")
  (cond
    ((and (not (vl-catch-all-error-p
                 (setq dia (vl-catch-all-apply 'getdist
                             '("\nWhat is the diameter of the light?: "))
                 )
               )
          )
          (not (vl-catch-all-error-p
              (setq pt1 (vl-catch-all-apply 'getpoint
                      '("\nSelect first point for midpoint location: "))
              )
            )
          )
     )
     (setq dia (/ dia 2.0))
     (while (vl-consp pt1)
       (cond
         ((and (not (vl-catch-all-error-p
                      (setq pt2 (vl-catch-all-apply 'getpoint
                                  (list pt1 "\nSelect opposite corner: "))
                      )
                    )
               )
               (setq pt3 (mapcar (function (lambda (p1 p2) (/ (+ p1 p2) 2.0)))
                              pt1
                              pt2
                      )
               )
               (vl-cmdf "circle" pt3 dia "circle" pt3 (- dia 1))
               (not (vl-catch-all-error-p
                      (setq pt1 (vl-catch-all-apply 'getpoint
                               '("\nSelect first point for midpoint location: "))
                      )
                    )
               )
               (vl-cmdf "undo" "m")
          )
          (redraw)
         )
         (T
          (setq pt1 nil)
          (setvars (list cmd blip mnu hilite osm lay))
         )
       ) ;_ cond
     ) ;_ while
    )
    ((setvars (list cmd blip mnu hilite osm lay))
    )
  ) ;_ cond
  (princ)
) ;_ defun

Water Bear

  • Guest
Crash and Burn....
« Reply #12 on: February 11, 2004, 01:54:26 PM »
Well this is what I used as a trap (had them remmed out in post)
Code: [Select]
;;;*****SIMPLE PROMPT TO INCICATE THAT DEFUN INIT IS LOADED
(defun errorload ()
  (prompt "\n...Error Trapping is Loaded...Ready...")
  (princ)
  ) ;end defun
(princ)

;;;Saves existing system variables
(defun init ()
  (errorload)
  (setq temperr *error*) ;store *error*
  (setq *error* mytrap) ;re-assign *error*
  (setq oldecho (getvar "cmdecho")) ;store variables
  (setq oldlayer (getvar "clayer"))
  (setq oldsnap (getvar "osmode"))
  (setq oldpick (getvar "pickbox"))
  (setq oldsnag (getvar "snapang"))
  (setq oldsnab (getvar "snapbase"))
  (setq oldunit (getvar "lunits"))
  (setq oldprec (getvar "luprec"))
  (setq oldstyle (getvar "textstyle"))
  (setq oldplinewid (getvar "plinewid"))
  (setq oldmirr (getvar "mirrtext"))
  (setq oldpdmode (getvar "pdmode"))
  (setq oldortho (getvar "orthomode"))
  (setq oldblip (getvar "blipmode"))
  (princ)
  ) ;end defun

;;;Define my error message and resets variables in the event of error
(defun mytrap (errmsg)
  (command nil nil nil)
  (if (not (member errmsg '("console break" "Function Cancelled"))
  )
    (princ (strcat "\nError: " errmsg)) ;print message
    )
  (command "undo" "b" "") ;<===============need to set a mark @ fitting, so that entire session isn't undone!
  (setq *error* temperr)
  (setvar "cmdecho" oldecho)
  (setvar "clayer" oldlayer)
  (setvar "osmode" oldsnap)
  (setvar "pickbox" oldpick)
  (setvar "snapang" oldsnag)
  (setvar "snapbase" oldsnab)
  (setvar "lunits" oldunit)
  (setvar "luprec" oldprec)
  (setvar "textstyle" oldstyle)
  (setvar "blipmode" oldblip)
  (setvar "plinewid" oldplinewid)
  (setvar "mirrtext" oldmirr)
  (setvar "pdmode" oldpdmode)
  (setvar "orthomode" oldortho)
  (princ
    "\nError has occurred Resetting Environment...everything UNDONE by my trap"
    )
;(redraw)
  (princ)
  ) ;end defun
(princ)
;;;If all goes smoothly to completion..set all changed variables back to original state

(defun reset () ;define function
  (setq *error* temperr) ;restore *error*
  (setvar "cmdecho" oldecho)
  (setvar "clayer" oldlayer)
  (setvar "osmode" oldsnap)
  (setvar "pickbox" oldpick)
  (setvar "snapang" oldsnag)
  (setvar "snapbase" oldsnab)
  (setvar "blipmode" oldblip)
  (setvar "orthomode" oldortho)
  (setvar "lunits" oldunit)
  (setvar "luprec" oldprec)
  (setvar "textstyle" oldstyle)
  (setvar "plinewid" oldplinewid)
  (setvar "mirrtext" oldmirr)
  (setvar "pdmode" oldpdmode)

  (prompt
    "\nProgram completed, Resetting System Variables... "
    ) ;inform user
  (princ)
  ) ;end defun
(princ)
;;;*********************************End of Error Trapping*********************************


Is it deficient?