Author Topic: when error, I want Restore the "textstyle"  (Read 6183 times)

0 Members and 1 Guest are viewing this topic.

liuhaixin88

  • Guest
when error, I want Restore the "textstyle"
« on: May 16, 2014, 07:59:01 AM »
This code is restore the layer
Code: [Select]
(defun c:xxx(/ *error* cl  )
Code: [Select]
(defun *error* (msg)
    (redraw)
    (if cl
      (setvar 'clayer cl)
    )
    (if (wcmatch (strcase msg) "*BREAK,*CANCEL*,*EXIT*")
      (princ msg)
      (princ (strcat "\n** Error: " msg " **"))
    )
  )

I want restore the layer and textstyle together, how to modify?
« Last Edit: May 16, 2014, 08:45:32 AM by liuhaixin88 »

ronjonp

  • Needs a day job
  • Posts: 7527
Re: when error, I want Restore the "textstyle"
« Reply #1 on: May 16, 2014, 08:37:34 AM »
Try something like this:
Code: [Select]
(setq vars (mapcar '(lambda (x) (cons x (getvar x))) '("clayer" "textstyle")))
(defun *error* (msg)
  ;; Reset variables
  (mapcar '(lambda (x) (setvar (car x) (cdr x))) vars)
  (if (not (wcmatch (strcase msg) "*BREAK,*CANCEL*,*EXIT*"))
    (princ (strcat "\nError: " msg))
  )
  (princ)
)

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

liuhaixin88

  • Guest
Re: when error, I want Restore the "textstyle"
« Reply #2 on: May 16, 2014, 08:47:26 AM »
Modify it like this ? ok?
Code: [Select]
(defun c:xxx(/ *error* cl ts )
Code: [Select]
(defun *error* (msg)
    (redraw)
    (if cl
      (setvar 'clayer cl)
    )
    (if ts
      (setvar 'TEXTSTYLE ts)
    )
    (if (wcmatch (strcase msg) "*BREAK,*CANCEL*,*EXIT*")
      (princ msg)
      (princ (strcat "\n** Error: " msg " **"))
    )
  )

liuhaixin88

  • Guest
Re: when error, I want Restore the "textstyle"
« Reply #3 on: May 16, 2014, 09:55:19 AM »
Try something like this:
Code: [Select]
(setq vars (mapcar '(lambda (x) (cons x (getvar x))) '("clayer" "textstyle")))
(defun *error* (msg)
  ;; Reset variables
  (mapcar '(lambda (x) (setvar (car x) (cdr x))) vars)
  (if (not (wcmatch (strcase msg) "*BREAK,*CANCEL*,*EXIT*"))
    (princ (strcat "\nError: " msg))
  )
  (princ)
)


Hi, ronjonp ,
Thank you
At the end of  routine(normal finish) ,how to restore ?

ronjonp

  • Needs a day job
  • Posts: 7527
Re: when error, I want Restore the "textstyle"
« Reply #4 on: May 16, 2014, 10:25:19 AM »
(mapcar '(lambda (x) (setvar (car x) (cdr x))) vars)

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

liuhaixin88

  • Guest
Re: when error, I want Restore the "textstyle"
« Reply #5 on: May 16, 2014, 10:34:32 AM »
(mapcar '(lambda (x) (setvar (car x) (cdr x))) vars)

OK! is good ! thank you!

liuhaixin88

  • Guest
Re: when error, I want Restore the "textstyle"
« Reply #6 on: May 16, 2014, 12:42:10 PM »
Try something like this:
Code: [Select]
(setq vars (mapcar '(lambda (x) (cons x (getvar x))) '("clayer" "textstyle")))
(defun *error* (msg)
  ;; Reset variables
  (mapcar '(lambda (x) (setvar (car x) (cdr x))) vars)
  (if (not (wcmatch (strcase msg) "*BREAK,*CANCEL*,*EXIT*"))
    (princ (strcat "\nError: " msg))
  )
  (princ)
)


Hi ,ronjonp,
Your routine is good , can add in "osmode" ?

Lee's rountine
Code: [Select]
(defun c:test ( / *error* osm )
 
    (defun *error* ( msg )
        (if osm (setvar 'osmode osm))
        (if (not (member msg '("Function cancelled" "quit / exit abort")))
            (princ (strcat "\nError: " msg))
        )
        (princ)
    )

    (setq osm (getvar 'osmode))
    (setvar 'osmode 0)

    (rtos (getreal "\nPress Esc to exit, press Enter to force an error ..."))

    (setvar 'osmode osm)
    (princ)
)

ronjonp

  • Needs a day job
  • Posts: 7527
Re: when error, I want Restore the "textstyle"
« Reply #7 on: May 16, 2014, 12:44:47 PM »
You can add whatever other variables you want to this portion of the code:


'("clayer" "textstyle")


That's the beauty of it :)

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

liuhaixin88

  • Guest
Re: when error, I want Restore the "textstyle"
« Reply #8 on: May 16, 2014, 12:58:13 PM »
You can add whatever other variables you want to this portion of the code:


'("clayer" "textstyle")


That's the beauty of it :)

This ?
Code: [Select]
(setq vars (mapcar '(lambda (x) (cons x (getvar x))) '("clayer" "textstyle" "osmode")))
(defun *error* (msg)
  ;; Reset variables
  (mapcar '(lambda (x) (setvar (car x) (cdr x))) vars)
  (if (not (wcmatch (strcase msg) "*BREAK,*CANCEL*,*EXIT*"))
    (princ (strcat "\nError: " msg))
  )
  (princ)
)

ronjonp

  • Needs a day job
  • Posts: 7527
Re: when error, I want Restore the "textstyle"
« Reply #9 on: May 16, 2014, 01:16:16 PM »
Yes

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

gile

  • Gator
  • Posts: 2507
  • Marseille, France
Re: when error, I want Restore the "textstyle"
« Reply #10 on: May 16, 2014, 01:42:15 PM »
Hi,

To set and restore system variables, you can also the ai_sysvar function.
This function is available in AutoCAD since a while.
It's defined in acaddoc20XX.lsp (ai_utils.lsp or acad.mnl in older versions), so it's automatically loaded in each document.

Quote
;;; --- ai_sysvar ------------------------------------------
;;; Change system variable settings and save current settings
;;; (Note: used by *merr* to restore system settings on error.)
;;;
;;; The <vars> argument is used to...
;;;   restore previous settings (ai_sysvar NIL)
;;;   set a single sys'var (ai_sysvar  '("cmdecho" . 0))
;;;   set multiple sys'vars (ai_sysvar '(("cmdecho" . 0)("gridmode" . 0)))
;;;
;;; defun-q is needed by Visual Lisp for functions which redefine themselves.
;;; it is aliased to defun for seamless use with AutoLISP.

Anywhere in the code, you can call ai_sysvar to save the current value of one or more sysvar and set it a new value (or not).

Example:
Code - Auto/Visual Lisp: [Select]
  1. ;; save and set "osmode" to 0
  2. (ai_sysvar '("osmode" . 0))
  3. ...
  4. ;; save and set "orthomode" to 1 and only save "autosnap"
  5. (ai_sysvar '(("orthomode" . 1) ("autosnap" . nil)))
  6. ...
  7. ;; restor all sysvars
  8. (ai_sysvar nil)

<EDIT> I forgot to say, the implementation of ai_sysvar is quite interesting with the use of defun-q which allows to store the list of var names ans values within the function.
« Last Edit: May 16, 2014, 02:19:18 PM by gile »
Speaking English as a French Frog

liuhaixin88

  • Guest
Re: when error, I want Restore the "textstyle"
« Reply #11 on: May 17, 2014, 12:45:51 PM »
Hi,

To set and restore system variables, you can also the ai_sysvar function.
This function is available in AutoCAD since a while.
It's defined in acaddoc20XX.lsp (ai_utils.lsp or acad.mnl in older versions), so it's automatically loaded in each document.

Quote
;;; --- ai_sysvar ------------------------------------------
;;; Change system variable settings and save current settings
;;; (Note: used by *merr* to restore system settings on error.)
;;;
;;; The <vars> argument is used to...
;;;   restore previous settings (ai_sysvar NIL)
;;;   set a single sys'var (ai_sysvar  '("cmdecho" . 0))
;;;   set multiple sys'vars (ai_sysvar '(("cmdecho" . 0)("gridmode" . 0)))
;;;
;;; defun-q is needed by Visual Lisp for functions which redefine themselves.
;;; it is aliased to defun for seamless use with AutoLISP.

Anywhere in the code, you can call ai_sysvar to save the current value of one or more sysvar and set it a new value (or not).

Example:
Code - Auto/Visual Lisp: [Select]
  1. ;; save and set "osmode" to 0
  2. (ai_sysvar '("osmode" . 0))
  3. ...
  4. ;; save and set "orthomode" to 1 and only save "autosnap"
  5. (ai_sysvar '(("orthomode" . 1) ("autosnap" . nil)))
  6. ...
  7. ;; restor all sysvars
  8. (ai_sysvar nil)

<EDIT> I forgot to say, the implementation of ai_sysvar is quite interesting with the use of defun-q which allows to store the list of var names ans values within the function.

Thanks  gile, I can't understand. Can you give me a routine ? like ronjonp.

liuhaixin88

  • Guest
Re: when error, I want Restore the "textstyle"
« Reply #12 on: June 06, 2014, 04:51:21 AM »
Yes

Hi, ronjonp , How are you!

If I want add "(command ".undo" "e")"  and "DIMSTYLE" to your function, how to do?


Code: [Select]
(setq vars (mapcar '(lambda (x) (cons x (getvar x))) '("clayer" "textstyle" "osmode")))
(defun *error* (msg)
  ;; Reset variables
  (mapcar '(lambda (x) (setvar (car x) (cdr x))) vars)
  (if (not (wcmatch (strcase msg) "*BREAK,*CANCEL*,*EXIT*"))
    (princ (strcat "\nError: " msg))
  )
  (princ)
)

Code: [Select]
;;start;(command "_.undo" "be")
(defun _StartUndo (*DOC*)
  (_EndUndo *DOC*)
  (vla-StartUndoMark *DOC*)
)
;;end;(if (= 8 (logand (getvar "undoctl") 8)) (command "_.undo" "_e"))
(defun _EndUndo (*DOC*)
  (if (= 8 (logand 8 (getvar 'UNDOCTL)))
    (vla-EndUndoMark *DOC*)
  )
)
« Last Edit: June 06, 2014, 07:59:01 AM by liuhaixin88 »

reltro

  • Guest
Re: when error, I want Restore the "textstyle"
« Reply #13 on: June 06, 2014, 05:13:11 AM »
Hey People...

This is what I use, includes also a mechanism to undo all stuff done...

*error*-functions can also be nested

Code - Auto/Visual Lisp: [Select]
  1. ;Note:
  2. ;    *error* should not be localized!!!
  3.  
  4.  
  5. (defun ini*error* (name sysvars / *doc*)
  6.     (eval
  7.         (list 'defun '*error* '(msg / )
  8.             (list
  9.                 (lambda (name prevErrorHandler SysVars *doc* currentcmdecho / )
  10.                     (mapcar
  11.                         'setvar
  12.                         (mapcar 'car SysVars)
  13.                         (mapcar 'cdr SysVars)
  14.                     )
  15.                     (vla-endundomark *doc*)
  16.                     (setq *error* prevErrorHandler)
  17.                     (if msg
  18.                         (progn
  19.                             (setvar 'cmdecho 0)
  20.                             (command-s "_.undo" 1 "")
  21.                             (setvar 'cmdecho currentcmdecho)
  22.                            
  23.                             (if (not (wcmatch msg "*EXIT*,*BREAK*,*CANCEL*"))
  24.                                 (progn
  25.                                     (princ "\n*** error ***\n")
  26.                                     (princ (strcat "\t" name ": " (vl-princ-to-string msg) "\n"))
  27.                                    
  28.                                     (*error* msg)
  29.                                     (princ)
  30.                                 )
  31.                             )
  32.                         )
  33.                     )
  34.                 )
  35.                 (vl-princ-to-string name)
  36.                 *error*
  37.                 (list 'quote
  38.                     (mapcar
  39.                         '(lambda (a / ) (cons a (getvar a)))
  40.                         sysvars
  41.                     )
  42.                 )
  43.                 *doc*
  44.                 (getvar 'cmdecho)
  45.             )
  46.         )
  47.     )    
  48. )
  49.    
  50. (ini*error* ;initialize error-handler
  51.     "String to display if an error occurs to identify wich function fails: "
  52.     '("CLAYER" "OSMODE" "CMDECHO" "DIMZIN") ;LIST of Sysvars to save/restore; String or Sym
  53. )
  54.  
  55. ;other stuff to do....
  56.  
  57. (*error* nil) ;restore Sys-Vars, and a previous-error-handler

reltro

roy_043

  • Water Moccasin
  • Posts: 1895
  • BricsCAD 18
Re: when error, I want Restore the "textstyle"
« Reply #14 on: June 06, 2014, 10:47:08 AM »
@ Reltro:
Interesting code. I have a question about this portion:
Code: [Select]
(if (not (wcmatch msg "*EXIT*,*BREAK*,*CANCEL*"))
  (progn
    (princ "\n*** error ***\n")
    (princ (strcat "\t" name ": " (vl-princ-to-string msg) "\n"))

    (*error* msg)
    (princ)
  )
)

If an EXIT/BREAK/CANCEL error occurs the restored *error* function is not called meaning only a single level is undone.

If a different error occurs the restored *error* function is called meaning all levels are undone. And there will also be a superfluous call to the 'default' *error* function.

I may be missing something but this does not seem logical.

reltro

  • Guest
Re: when error, I want Restore the "textstyle"
« Reply #15 on: June 06, 2014, 11:16:27 AM »
@ Reltro:
Interesting code. I have a question about this portion:
Code: [Select]
(if (not (wcmatch msg "*EXIT*,*BREAK*,*CANCEL*"))
  (progn
    (princ "\n*** error ***\n")
    (princ (strcat "\t" name ": " (vl-princ-to-string msg) "\n"))

    (*error* msg)
    (princ)
  )
)

If an EXIT/BREAK/CANCEL error occurs the restored *error* function is not called meaning only a single level is undone.

If a different error occurs the restored *error* function is called meaning all levels are undone. And there will also be a superfluous call to the 'default' *error* function.

I may be missing something but this does not seem logical.

Oh shit...
roy_043 u r totally right... this is a mistake...

Found an other issue...
should be (wcmatch (strcase msg) ...)

corrected version
Code - Auto/Visual Lisp: [Select]
  1. ;Note:
  2. ;    *error* should not be localized!!!
  3.  
  4.  
  5. (defun ini*error* (name sysvars / *doc*)
  6.     (eval
  7.         (list 'defun '*error* '(msg / )
  8.             (list
  9.                 (lambda (name prevErrorHandler SysVars *doc* currentcmdecho / )
  10.                     (mapcar
  11.                         'setvar
  12.                         (mapcar 'car SysVars)
  13.                         (mapcar 'cdr SysVars)
  14.                     )
  15.                     (vla-endundomark *doc*)
  16.                     (setq *error* prevErrorHandler)
  17.                     (if msg
  18.                         (progn
  19.                             (setvar 'cmdecho 0)
  20.                             (command-s "_.undo" 1 "")
  21.                             (setvar 'cmdecho currentcmdecho)
  22.  
  23.                             (if (not (wcmatch (strcase msg) "*EXIT*,*BREAK*,*CANCEL*"))
  24.                                 (progn
  25.                                     (princ "\n*** error ***\n")
  26.                                     (princ (strcat "\t" name ": " (vl-princ-to-string msg) "\n"))
  27.  
  28.                                     (princ)
  29.                                 )
  30.                             )
  31.                                                        
  32.                             (*error* msg)
  33.                         )
  34.                     )
  35.                 )
  36.                 (vl-princ-to-string name)
  37.                 *error*
  38.                 (list 'quote
  39.                     (mapcar
  40.                         '(lambda (a / ) (cons a (getvar a)))
  41.                         sysvars
  42.                     )
  43.                 )
  44.                 *doc*
  45.                 (getvar 'cmdecho)
  46.             )
  47.         )
  48.     )    
  49. )
  50.  
  51. (ini*error* ;initialize error-handler
  52.     "String to display if an error occurs to identify wich function fails: "
  53.     '("CLAYER" "OSMODE" "CMDECHO" "DIMZIN") ;LIST of Sysvars to save/restore; String or Sym
  54. )
  55.  
  56. ;other stuff to do....
  57.  
  58. (*error* nil) ;restore Sys-Vars, and a previous-error-handler
  59.  

reltro

reltro

  • Guest
Re: when error, I want Restore the "textstyle"
« Reply #16 on: June 06, 2014, 12:04:12 PM »
DOES NOT WORK

Code - Auto/Visual Lisp: [Select]
  1. (defun initializeError (name /  *doc*)
  2.    
  3.     (eval
  4.         (list 'defun-q '*error* '(msg / )
  5.             (list 'setq 'setvar setvar)
  6.             nil
  7.             (list
  8.                 (lambda (name *doc* prevErr / )
  9.                     (vla-endundomark *doc*)
  10.                    
  11.                     (setq *error* prevErr)
  12.                    
  13.                     (if msg
  14.                         (progn
  15.                             (setvar 'cmdecho 0)
  16.                             (command-s "_.undo" 1 "")
  17.                             (setvar 'cmdecho currentcmdecho)
  18.  
  19.                             (if (not (wcmatch (strcase msg) "*EXIT*,*BREAK*,*CANCEL*"))
  20.                                 (progn
  21.                                     (princ "\n*** error ***\n")
  22.                                     (princ (strcat "\t" name ": " msg "\n"))
  23.  
  24.                                     (princ)
  25.                                 )
  26.                             )
  27.                            
  28.                             (*error* msg)
  29.                         )
  30.                     )
  31.                 )
  32.                 (vl-princ-to-string name)
  33.                 *doc*
  34.                 (list 'quote *error*)
  35.             )
  36.         )
  37.     )
  38.    
  39.     (eval
  40.         (list 'defun 'setvar '(n v / )
  41.             (list
  42.                 (lambda (S store / )
  43.                     (eval
  44.                         (vl-list*
  45.                             'defun-q '*error* '(msg / )
  46.                             (cadr *error*)
  47.                             (list 'setvar (vl-princ-to-string n) store)
  48.                             (cddr *error*)
  49.                         )
  50.                     )
  51.                     (S n v)
  52.                 )
  53.                 setvar
  54.                 (list getvar 'n)
  55.             )
  56.         )
  57.     )
  58. )
  59.  

This one would be nice too, I think, but overwriting 'setvar does not work...  :-(

reltro
« Last Edit: June 06, 2014, 01:20:50 PM by reltro »

liuhaixin88

  • Guest
Re: when error, I want Restore the "textstyle"
« Reply #17 on: June 10, 2014, 02:35:47 AM »
Thanks ,reltro

reltro

  • Guest
Re: when error, I want Restore the "textstyle"
« Reply #18 on: June 10, 2014, 01:01:38 PM »
I'm not sure if this is good stuff or not...
Im overwriting setvar to catch all changes and restore changed sysvars if an error occurs or the errorHandler is called with nil...

Code - Auto/Visual Lisp: [Select]
  1. (defun INIerror (name /  *doc*)
  2.  
  3.     (eval
  4.         (list 'defun-q '*error* '(msg / )
  5.             (list
  6.                 (lambda (s / )
  7.                     (pragma '((unprotect-assign setvar)))
  8.                     (setq setvar s)
  9.                     (pragma '((protect-assign setvar)))
  10.                 )
  11.                 setvar
  12.             )
  13.             (list
  14.                 (lambda (store / )
  15.                     (mapcar
  16.                         '(lambda (a / )
  17.                             (setvar (car a) (cdr a))
  18.                         )
  19.                         store
  20.                     )
  21.                 )
  22.                 nil
  23.             )
  24.             (list
  25.                 (lambda (name *doc* prevErr  / currentcmdecho)
  26.                     (vla-endundomark *doc*)
  27.                     (setq *error* prevErr)
  28.  
  29.                     (if msg
  30.                         (progn
  31.                             (setq currentcmdecho (getvar 'cmdecho))
  32.                             (setvar 'cmdecho 0)
  33.                             (command-s "_.undo" 1 "")
  34.                             (setvar 'cmdecho currentcmdecho)
  35.  
  36.                             (if (not (wcmatch (strcase msg) "*EXIT*,*BREAK*,*CANCEL*"))
  37.                                 (progn
  38.                                     (princ "\n*** error ***\n")
  39.                                     (princ (strcat "\t" name ": " msg "\n"))
  40.  
  41.                                     (princ)
  42.                                 )
  43.                             )
  44.  
  45.                             (*error* msg)
  46.                         )
  47.                     )
  48.                 )
  49.                 (vl-princ-to-string name)
  50.                 *doc*
  51.                 (list 'quote *error*)
  52.             )
  53.         )
  54.     )
  55.  
  56.     (pragma '((unprotect-assign setvar)))
  57.    
  58.     (eval
  59.         (list 'defun 'setvar '(n v / )
  60.             (list
  61.                 (lambda (S store / )
  62.                     (setq n (strcase (vl-princ-to-string n)))
  63.                     (eval
  64.                         (vl-list*
  65.                             'defun-q '*error* '(msg / )
  66.                             (cadr *error*)
  67.                             (    (lambda (values / )
  68.                                     (if (assoc n (eval (cadr values)))
  69.                                         values
  70.                                         (list
  71.                                             (car values)
  72.                                             (list 'quote (cons (cons n store) (eval (cadr values))))
  73.                                         )
  74.                                     )
  75.                                 )
  76.                                 (caddr *error*)
  77.                             )
  78.                             (cddr *error*)
  79.                         )
  80.                     )
  81.                     (S n v)
  82.                 )
  83.                 setvar
  84.                 (list getvar 'n)
  85.             )
  86.         )
  87.     )
  88.    
  89.     (pragma '((protect-assign setvar)))
  90. )
  91.  

USAGE:
Code: [Select]
(INIerror "This is a function ...")

(setvar "osmode" 0)
(setvar "cmdecho 0)

.
.
.
.

(*error* nil)

reltro

liuhaixin88

  • Guest
Re: when error, I want Restore the "textstyle"
« Reply #19 on: June 16, 2014, 09:49:15 PM »
I'm not sure if this is good stuff or not...
Im overwriting setvar to catch all changes and restore changed sysvars if an error occurs or the errorHandler is called with nil...

Code - Auto/Visual Lisp: [Select]
  1. (defun INIerror (name /  *doc*)
  2.  
  3.     (eval
  4.         (list 'defun-q '*error* '(msg / )
  5.             (list
  6.                 (lambda (s / )
  7.                     (pragma '((unprotect-assign setvar)))
  8.                     (setq setvar s)
  9.                     (pragma '((protect-assign setvar)))
  10.                 )
  11.                 setvar
  12.             )
  13.             (list
  14.                 (lambda (store / )
  15.                     (mapcar
  16.                         '(lambda (a / )
  17.                             (setvar (car a) (cdr a))
  18.                         )
  19.                         store
  20.                     )
  21.                 )
  22.                 nil
  23.             )
  24.             (list
  25.                 (lambda (name *doc* prevErr  / currentcmdecho)
  26.                     (vla-endundomark *doc*)
  27.                     (setq *error* prevErr)
  28.  
  29.                     (if msg
  30.                         (progn
  31.                             (setq currentcmdecho (getvar 'cmdecho))
  32.                             (setvar 'cmdecho 0)
  33.                             (command-s "_.undo" 1 "")
  34.                             (setvar 'cmdecho currentcmdecho)
  35.  
  36.                             (if (not (wcmatch (strcase msg) "*EXIT*,*BREAK*,*CANCEL*"))
  37.                                 (progn
  38.                                     (princ "\n*** error ***\n")
  39.                                     (princ (strcat "\t" name ": " msg "\n"))
  40.  
  41.                                     (princ)
  42.                                 )
  43.                             )
  44.  
  45.                             (*error* msg)
  46.                         )
  47.                     )
  48.                 )
  49.                 (vl-princ-to-string name)
  50.                 *doc*
  51.                 (list 'quote *error*)
  52.             )
  53.         )
  54.     )
  55.  
  56.     (pragma '((unprotect-assign setvar)))
  57.    
  58.     (eval
  59.         (list 'defun 'setvar '(n v / )
  60.             (list
  61.                 (lambda (S store / )
  62.                     (setq n (strcase (vl-princ-to-string n)))
  63.                     (eval
  64.                         (vl-list*
  65.                             'defun-q '*error* '(msg / )
  66.                             (cadr *error*)
  67.                             (    (lambda (values / )
  68.                                     (if (assoc n (eval (cadr values)))
  69.                                         values
  70.                                         (list
  71.                                             (car values)
  72.                                             (list 'quote (cons (cons n store) (eval (cadr values))))
  73.                                         )
  74.                                     )
  75.                                 )
  76.                                 (caddr *error*)
  77.                             )
  78.                             (cddr *error*)
  79.                         )
  80.                     )
  81.                     (S n v)
  82.                 )
  83.                 setvar
  84.                 (list getvar 'n)
  85.             )
  86.         )
  87.     )
  88.    
  89.     (pragma '((protect-assign setvar)))
  90. )
  91.  

USAGE:
Code: [Select]
(INIerror "This is a function ...")

(setvar "osmode" 0)
(setvar "cmdecho 0)

.
.
.
.

(*error* nil)

reltro

Thanks  reltro, you are great !
your routine  is complex. I can't understand completely.

When errors happen, this routine can do what ?


« Last Edit: June 16, 2014, 09:52:32 PM by liuhaixin88 »

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: when error, I want Restore the "textstyle"
« Reply #20 on: June 16, 2014, 11:24:21 PM »
My experience has shown that the *error* function is best defined as local to the main application function.

eg
Quote
(defun c:test ( / *error* ... )
 
    (defun *error* ( msg )
        ;; stuff here
    )
    ;; stuff here

    ; force a clean-up
    (*error* nil )
    (princ)
)

If you define *error as global it will stay in memory and be re-used for any following errors. ... something that may not be required if you are making specific restorations or changes to the environment.

A general guideline is "Leave the environment as you found it". I've seen cases where a user defined global *error* routine has caused real debugging problems in other routines.

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.