Author Topic: Error Handling  (Read 1433 times)

0 Members and 1 Guest are viewing this topic.

Lupo76

  • Bull Frog
  • Posts: 343
Error Handling
« on: February 18, 2015, 01:11:40 PM »
Hello everyone,

for error handling I use the following code:

Code: [Select]
(defun c:TEST (/ var msgg var_ORTHOMODE  snapcorr var_INSUNITSDEFSOURCE var_INSUNITSDEFTARGET var_INSUNITS var_OSNAPZ OSNAPZ)
  ;*************************************
  (setq var (getvar "CMDECHO"))
  (setvar "CMDECHO" 0)
  (setq old-error *error*)
  (defun *error* (msg)
     (setq msgg (strcat "Error: " msg))
     (princ msgg)
     (setq *error* old-error)
     (command "_.undo" "_end" "_u")
     (exit)
  )
  (command "_.undo" "_be") 
 
  (setq snapcorr (getvar "OSMODE"))
  (setvar "OSMODE" 0)
 
  (setq var_INSUNITSDEFSOURCE (getvar "INSUNITSDEFSOURCE"))
  (setq var_INSUNITSDEFTARGET (getvar "INSUNITSDEFTARGET"))
  (setq var_INSUNITS (getvar "INSUNITS"))
  (setq var_OSNAPZ (getvar "OSNAPZ"))

  (setvar "INSUNITSDEFSOURCE" 0)
  (setvar "INSUNITSDEFTARGET" 0)
  (setvar "INSUNITS" 0)
  (setvar "OSNAPZ" 1)
  ;*************************************


   ....custom code....
   ....custom code....
   ....custom code....
   ....custom code....


  ;*************************************
  (setvar "INSUNITSDEFSOURCE" var_INSUNITSDEFSOURCE)
  (setvar "INSUNITSDEFTARGET" var_INSUNITSDEFTARGET)
  (setvar "INSUNITS" var_INSUNITS)
  (setvar "OSNAPZ" var_OSNAPZ)
 
  (setvar "OSMODE" snapcorr)
 
  (setq *error* old-error)
  (command "_.undo" "_end")
  (setvar "cmdecho" var)
  (princ)
  ;*************************************
)


Unfortunately, if the user presses ESC when running object snaps are not recovered.
What am I doing wrong?

Lupo76

  • Bull Frog
  • Posts: 343
Re: Error Handling
« Reply #1 on: February 18, 2015, 01:23:46 PM »
I solved it by yourself!

in custom code, I used to call a function that I had one line of code too :embarrassed:
I'm glad to have found this bug because this function was used many times in my code :lol:



ur_naz

  • Newt
  • Posts: 68
  • Made in Ukraine
Re: Error Handling
« Reply #2 on: February 18, 2015, 01:55:40 PM »
you can use something like below to improve your commands
Code - Auto/Visual Lisp: [Select]
  1. (defun _start_undo ()
  2.  
  3.   (_end_undo)
  4. )
  5.  
  6. (defun _end_undo ()
  7.  
  8.   (if (logand 8 (getvar "UNDOCTL"))
  9.   )
  10. )
  11.  
  12. (defun _save_var (lst)
  13.  
  14.   (mapcar '(lambda (x) (cons x (getvar x))) lst)
  15.  
  16. )
  17.  
  18. (defun _restore_var (lst)
  19.  
  20.   (foreach item lst
  21.     (setvar (car item) (cdr item))
  22.   )
  23.  
  24. )
  25.  
  26. (defun _err (msg)
  27.   (if
  28.     (member
  29.       (strcase msg T)
  30.      '("function cancelled"
  31.         "console break"
  32.         "quit / exit abort"
  33.       )
  34.     )
  35.     (princ "\nExiting!")
  36.     (princ (strcat "\nERROR: " (itoa (getvar 'errno)) " - " msg))
  37.   )
  38.  
  39.   (end_cmd)
  40.  
  41. )
  42.  
  43. (defun start_cmd ( / svlist)
  44.  
  45.  
  46.   (_start_undo)
  47.  
  48.   (setq svlist '( attdia cmddia blipmode clayer cmdecho osmode
  49.                   cmddia shortcutmenu insunits peditaccept ))
  50.  
  51.  
  52.  
  53.   (setq *sysvarstack* (_save_var svlist)
  54.         *olderr* *error*
  55.         *error*  _err
  56.   )
  57. )
  58.  
  59. (defun end_cmd ()
  60.   (_restore_var *sysvarstack*)
  61.   (setq *error* *olderr* *olderr* nil)
  62.   (_end_undo)
  63.   (redraw)
  64.   (princ "\nReady!")
  65.   (princ)
  66. )
  67.  
  68. (defun _set_ui_mode (osm)
  69.   (setvar 'cmdecho 1)
  70.   (setvar 'osmode osm)
  71.  
  72. )
  73.  
  74. (defun _set_rotate_mode (osm)
  75.  (setvar 'cmdecho 0)
  76.  (setvar 'osmode osm)
  77. )
  78.  
  79. (defun _set_ins_mode (osm)
  80.    '(cmdecho     attdia  cmddia  blipmode    shortcutmenu    insunits)
  81.    '(0           0       0       0           0               4)
  82.   )
  83.   (setvar 'osmode osm)
  84. )
  85.  
  86. (defun _set_dw_mode ()
  87.   (mapcar 'setvar '(cmdecho osmode blipmode) '(0 0 0))
  88. )