|
|

楼主 |
发表于 2007-3-13 08:33
|
显示全部楼层
undo.lsp
;;; UNDO.LSP made by piggy, G7 j" S n& O9 L& |" m% Q `
;;;/ w" x0 U7 I, Z3 A
;;; DESCRIPTION0 }* z% F/ W5 `. }
;;; These are general error routines which can be called by
3 z6 N" ?( m3 ?& b3 E;;; other routine. See AutoCAD 14 ac_bonus.lsp for reference9 \3 q: k8 ~7 C. A
;;;) Q% P/ H. ]4 a2 H
;;; SUBROUTINE INCLUDED IN THIS FILE
4 {) f, L" q# b0 i;;; UNDO_INIT
# l# t. b& m2 W( _3 f;;; UNDO_RESTORE
A; R! t* x2 P1 t7 }0 _2 o# };;; VAR_SAVE
' R+ G6 w- [" {# {;;; VAR_SET
5 d* v, p; r0 J! q8 M;;; VAR_RESTORE
, J/ X9 {% @3 i$ p) D;;; ERR_INIT ! g0 K9 [% F& g- k
;;; ERR_MAIN
5 o" n6 M) |5 q1 k9 V. E$ V;;; ERR_RESTORE
% O9 J% i+ V: f5 [. d;;;* V9 u$ |7 p: G5 f+ ]6 D$ s# d# A+ K" ?- X
;;; DATE: 10/17/98; 03/31/999 k* Y. Y1 j7 E
;;;
4 K5 M, e% _0 J6 ~;;; HISTORY:
. f6 |- y4 w- |* R6 X;;; Add routine of mod_att
' w9 S6 {$ C! F; V;;;5 I3 E$ D$ [% q; q+ V3 @1 H
;;; USING METHOD" {7 s0 B1 ?- X* _6 z+ v, l6 X
;;; ERR_INIT:* ]! T& p' }: p; c4 V
;;; This routine initialzes the error handler. It should be called as:9 _7 ?8 h6 Y$ t- @/ q
;;;( _0 H# N9 J9 n' ]% W# U/ X) |
;;; (if (and (not undo_init)# E. e' L: k9 {& z9 V6 r$ V( o
;;; (equal -1 (load "undo.lsp" -1))
3 D0 r& R+ K5 Q9 @+ R4 A* ]$ _' j) a) q;;; );and
$ j2 F3 @" R9 X, a; J$ [4 U;;; (progn (alert "Error:\n Cannot find UNDO.LSP.")(exit))7 V% a( j0 ^4 _3 S! k' a. `
;;; ); if
2 D7 ]! X, A- D( Z3 ~* F;;;
* c& w. c+ `# ]- w+ X' y* n8 g;;; ARGUMENTS:' O8 O1 ?, H1 f* b: p2 p: T
;;; err_init Takes 3 arguments. 4 y) Z; R# i4 Q U
;;; 1. - The first element of the argument:# q2 |' g2 {% d9 b' e
;;; This is a list of system variables paired with
$ i; g7 M) u& \;;; the values you want to set them to. i.e. '("CMDECHO" "ATTMODE"). x6 e- o) p1 o
;;; 2. - The second element is a flag8 a# d! W4 t5 O" z" }. k
;;; If it is true, then in the event of an error
- K6 p9 Q& p U' S( F$ o6 O;;; the custom *error* routine will utilize UNDO
5 Y. Y* s/ ?& Y0 S# `3 A: _;;; as a cleanup mechanism.
1 ?( t5 e9 |, Q S& C' R;;; 3. - The third element is a quoted function call.
$ F/ H! M* h" C1 J( m! T8 A! |9 E;;; You pass a quoted call to the function you6 j3 E" F( p8 v& p, |% c+ _, C+ T1 M
;;; wish to execute at the end of nomal routine if an error occurs.
2 y# R/ Z/ H3 z/ J' u;;; i.e. '(my_special_stuff arg1 arg2...)8 J* ~% W; y0 t$ _6 I# |7 X+ @
;;; Use this arg if you want to do some specialized clean up
7 ^8 x. ~1 L# {! H% z;;; things that are not already done by the standard bonus_error ! F% Z- N, h; k6 C1 t& I
;;; function.
1 w; [6 N/ z3 B& `( q;;;
' ?, d4 {, t; b;;; ERR_MAIN: Body of error routine3 \2 W3 G2 t X; B
;;;8 _$ _, v! P$ I* q" @1 |6 I" k5 j& P
;;; ERR_RESTORE: This routine should be called at the end of command to
3 ^% w" u1 W3 ?8 ~3 j9 c;;; restore the VARIABLES, UNDO & *error*.
5 S& v4 ]. x# e; u2 G% K$ V;;;
6 s2 I2 P7 s' X;;; UNDO_INIT: Initialize the UNDO status
7 G8 e: T. X4 Q) y0 @7 [! d;;;1 a; @2 {* S5 S& s
;;; UNDO_RESTORE: Restore the UNDO status8 O& f' l2 _ \ g j- A- z1 a# r
;;;1 G. Y* ]" f# k4 \
;;; VAR_SAVE: Save the variables. the argument is like '("CMDECHO" "ATTMODE")# w O7 _7 |$ i$ j& N: O
;;;
# p4 f" }: O( \. y3 e- V- b;;; UNDO_set: Set the variables. the argument is like
# I L( C4 g4 F;;; '(("CMDECHO" 0) ( "ATTMODE" 0))
, o6 H2 ?3 V' G: X5 _;;;- m3 S- s1 ]5 b0 }6 R4 D
;;; UNDO_RESTORE: Restore the variables. c+ f1 T8 g# p2 P
;;;
& U5 [+ T" v8 x) b1 y9 L9 e8 P9 E;----------------------------------------------------------------------------
, e5 i0 o3 K1 H; GLOBALS:# V+ r7 L5 d7 g# ~5 N R- H3 J* G
; old_undoctl -- old status of UNDO (voided by UNDO_RESTORE)
4 m" ~/ c8 a8 d' t& m w$ r. ~' s4 y/ F; m_lst -- list of variables (voided by VAR_RESTORE)
: o* O% [+ Z4 I9 \- F- T; err_alive -- indicate error routine is active (voided by ERR_MAIN or7 k1 D& L; G( F7 F( `* ^8 W
; ERR_OLD) h. k- T, p* w& D% H
; err_old -- old handler of *error* (voided by ERR_MAIN or ERR_OLD)( y3 r: L* t7 s8 |( o* e
;----------------------------------------------------------------------------
7 W0 q h4 @1 [, b# b
6 R4 R3 k1 P" b. ? {: r;----------------------------------------------------------------------------
$ c N2 e) k, ]; G# Q" K H; Modify attributes according to entity name, attribute name, dxf_item1 W: s& D7 D+ P$ I3 T% j- o+ Q
;----------------------------------------------------------------------------$ [. E2 o' W3 O- C& ~- a5 c
(defun mod_att(ent id dxf_item) $ [/ e$ D- Z- R" d; y0 h
(while (and (/= "ATTRIB" (car (entgetf '(0) ent))) (/= id (car (entgetf '(2) ent))))
. l( S5 I; J2 w- b U& G+ w) ? (setq ent (entnext ent)): M) Z/ Q9 f( T. A* c5 }
); while
# \7 U( d: U: e' V ((lambda (x)
6 O; ^4 T( d: i. a6 I! Z8 Y (mapcar '(lambda (y)8 f2 N( k7 C9 K# h/ M/ J7 ^. u
(setq x (subst y (assoc (car y) x) x))
/ f! X4 ~$ Y% d ); lambda
* p8 V0 {7 V# @& o5 Z' ^( o dxf_item
" i9 ~9 [) B! d9 c; L2 ] ); mapcar' n) P: U4 ` N) C( O
(entmod x)
- L) Q1 r% W- C7 n (entupd ent)
5 {7 w; M: [# Z* V ); lambda
" N4 r9 y. a+ I2 O; J (entget ent)
1 t' G, B( } R( B )
7 d$ t( A! T% E& { i); defun mod_att: ]7 u, U) P5 }7 A4 O Z
- |$ b. y& f4 T; M& L5 C% o
;----------------------------------------------------------------------------. f* ^9 [ ?" W
; Check layer status, return a association list which contains layer information
2 t' M0 t5 ]) B5 f) _; O;----------------------------------------------------------------------------
+ i8 C) V& \, S: E(defun laychk(lay / l_sta)
! j3 T- n" K! b7 C2 e ( (lambda (x)/ a- O7 v5 O! ?. c4 X$ M
(if (not l_s)- b# B3 M" V0 ?; R. U- \, H- Y* \
(setq l_s (list (cons x (logand 5 (cdr (assoc 70 (tblsearch "LAYER" x)))))))
# H8 J% N, C6 i8 Q+ c7 e2 l& { ); if
, L' Q1 A4 M+ I ); lambda
# P) w# {/ C9 d6 c& L, F (getvar "CLAYER")
5 i0 W) ]# M' z7 D& ] )
! `7 \0 M, r" }7 |8 w2 ] (if (not (tblsearch "LAYER" lay))
- q8 j. G& f9 n2 Z p (progn
/ C- x' E N i. I (initget "Yes No")
7 D: t$ Q0 F" Z, \+ d (if (= (setq l_sta (getkword "\n图层不存在,是否建立该图层 ?\(Y/N\)")) "Yes")" r, v- l; h8 }8 i( ?$ C
(progn
/ w6 _ n1 L L% L C% k+ J5 N (command "_.layer" "n" lay "")7 h. O `5 M, t+ m$ M
(setq l_sta 0)
. {/ ^8 Y" B" q# a5 Y- Z( y, \ ); progn
( \9 s) S4 x% q! E% m ); if4 J0 Q" x1 U, }% c; L
); progn2 ~" n7 I$ U. J. n7 n, p' A; g
(progn
! ^" p9 N* S9 [- q# g7 ^$ w( w (setq l_sta (logand 5 (cdr (assoc 70 (tblsearch "LAYER" lay)))))
, _, W, b& g9 \( _, \; H( f (if (= 1 (logand 1 l_sta))
( N& S+ k- B7 W& [ (progn
, R- ^+ E; A2 X' c0 h (initget "Yes No")! P6 k: @% P/ d% Q& }
(if (= (getkword "\n该图层被冻结,是否解冻 ?\(Y/N\)") "Yes")
' E1 A( G- q. e) P, |: y (command "_.layer" "t" lay "")
" D: H, ~- t( J2 K, f (setq l_sta "No")( J$ |. h' W' ~5 E" C/ e
); if
% O; R }( f1 z ); progn
3 n& b# a/ Y# E ); if
7 q3 M4 r' R% F! J (if (numberp l_sta) 0 j1 ~! Z4 b9 W" o& Y2 x4 Q; q
(if (= 4 (logand 4 l_sta)) (command "_.layer" "u" lay "")); if$ x7 }4 [! m% c
); if
9 t9 C4 e q) |- [+ y3 h ); progn
: C m L0 z/ @! B# C8 m3 g ); if6 C4 M( l, [* h K% r
( (lambda (x)
7 |# I E% j: |9 T2 F (cond 1 r% X4 o5 k$ J8 x0 b
( (not x)
: n. }8 P" f5 o2 {, l (setq l_s (cons (cons lay l_sta) l_s))
# w, D* _" D- L )
+ m( ^5 E8 U2 H3 d7 s ( (= "No" (cdr x))
5 o, f2 L/ a% ^0 b7 U (setq l_s (subst (cons lay l_sta) x l_s))3 h" _8 }* w) U, ^# I
)3 p( ~8 Y( C1 D2 q1 V
( T l_s)+ Q& j( y; ~# u# W0 ~
); cond% o/ W( Y" U2 O/ J+ ~! B- Z5 M
); lambda
6 }' S1 r1 e% p5 Z3 b- Z1 d* f (assoc lay l_s)/ A7 ]+ O# u9 }3 `" y1 J, S
)( k" z) N& \5 L9 a5 u
: t; M" G$ m) _5 @# B. j; t' r); defun chklay
$ O I4 P# ]# Y9 W s% [ V! l! ~% f9 Q7 v# b B( B$ \
;----------------------------------------------------------------------------6 h6 J3 e, u& J, q; V0 S* a
; Restore layer status according to association list l_s
- j& ]* s+ c" p' c" C! q;----------------------------------------------------------------------------1 m, k i+ @, z! ^( A1 w4 a2 O- s. n
(defun layres()
0 L: N e. H8 d. |4 I8 @ (setvar "CLAYER" (car (last l_s)))
* o0 a5 y n/ O4 h (repeat (length l_s)
! A" Q$ l- n2 H ( (lambda(x) - y0 M5 B6 `# K6 \
(if (numberp (cdr x))
E& L- B6 r4 _ t (progn 2 `1 Y s! d7 g/ ]1 |/ a6 D( p7 M0 \
(if (= 4 (logand 4 (cdr x)))
" L6 n, e; I# _% O (command "_.layer" "lo" (car x) "")
`9 y' d' ^( X( e8 C8 A! |1 N ); if/ L% H1 M5 |: E) k B, t" d1 e
(if (= 1 (logand 1 (cdr x)))
, U, v( B) E. X/ w* p5 ~( a5 J (command "_.layer" "f" (car x) "")
$ P& e! A0 }9 j" ]' d ); if
& R6 }* V% N/ T9 _& P ); progn! D* H9 L6 `5 |8 K' q
); if
5 w( |0 a7 u/ }2 h6 }: [ ); lambda& L1 ~1 c; A% l
(car l_s)/ s- A& V# q; z# A% t: `. W( J5 J
)7 R1 |6 Y: z6 f) \( H# a8 L9 \
(setq l_s (cdr l_s))+ `) F0 C, `% Q+ M5 I; g% R
); repeat
i7 j+ E4 k7 m); layres
% S* @0 }( C* P
' u m9 V% O4 |6 O! \: O;----------------------------------------------------------------------------
% l# W9 T3 w$ ?. l/ J; _; Get DXF codes: g% D2 h' M6 u- Q& x* X
;----------------------------------------------------------------------------
. E( P4 Z z9 x: r7 z, c/ [6 B(defun entgetf (index ent)
j+ g; x" `0 J3 u ((lambda (e)
h$ ]1 s+ t- @7 i/ I* k4 A( H (mapcar '(lambda (x)
9 O" S' {' f; Z' f/ `6 L& G6 c (cdr (assoc x e))* g: H `( l0 y/ m+ P5 x
); lambda" Q# J% Q- d( W s, N. g
index) ; internal lambda function
0 T* v: Q. P! q( _4 @ ); lambda5 B$ F7 X q, }
(entget ent)
; c N, q# w g/ G% k; [1 _ ), Q% X" s G8 z( \% E0 M$ K
); defun entgetf
! p: q$ L9 g/ Q Y
. W8 u9 y/ _. ]3 o" h;----------------------------------------------------------------------------
6 u6 d8 ~& C; S" P& ]; Save UNDO status
! T" l' p* |! I3 ?;----------------------------------------------------------------------------
; j' t: {4 }3 S) v(defun undo_init (/ cmdecho undo_ctl)
4 Z6 I+ q) ]% m$ B3 w (setq cmdecho (getvar "CMDECHO") undo_ctl (getvar "UNDOCTL")) ; Save the value
7 I; K# C2 u& K5 g (setvar "CMDECHO" 0)
7 G: k' u! k0 N" C8 _) E
' J) W' _/ h3 T& x. \, G6 u- S9 x3 D (if (equal 0 undo_ctl) ; Make sure undo is fully enable
/ @1 ]2 o+ w3 E+ {: c (command "_.undo" "_all"); v. m& T! N6 [; N/ d+ I+ N
(command "_.undo" "_control" "_all") ~) x( r$ X+ [; D/ w4 v
)
' B2 U7 u' g( ~' P/ _2 ~2 G& ~- g* X3 o2 a
(if (equal 4 (logand 4 (getvar "UNDOCTL"))) ; Ensure undo auto is off
. w! B( @& c7 e5 ?# W' Z (command "_.undo" "_auto" "_off")
( K1 H% R. Y) s( y+ t1 c4 w )' L: e$ k5 o* N- a+ ]+ m
0 _) L3 _0 b q& U1 a
(while (equal 8 (logand 8 (getvar "UNDOCTL"))) ; Place an end mark here
* {2 ?& U0 y* V1 B/ @& y* E6 i8 w (command "_.undo" "_end")
/ U; A! z4 b& A3 z' B )
, y) A3 y, Q+ N+ S J0 I2 ~
6 a( h0 F% d, i! R% x8 p (while (not (equal 8 (logand 8 (getvar "UNDOCTL"))))) k" z1 e8 t$ r, {7 n$ A9 O
(command "_.undo" "_group")# `) k% h& ~" ^3 B
)
$ d1 ~) K0 q0 {- V
' J! V2 n& C! v" A3 x (setvar "CMDECHO" cmdecho)3 _' u T+ E1 }/ z# U7 E8 w
undo_ctl# o9 U* I( i. a/ H) O& X! X8 {
); defun undo_init
- G( Z: N- a/ ]' k7 q3 D$ G; I% ]* G; L% t% V; r
;----------------------------------------------------------------------------1 K! w! N2 ]/ `8 F) m
; Restore UNDO status6 J! U2 O2 a0 u( p" p
;----------------------------------------------------------------------------3 Q. ]! r. ?+ h
(defun undo_restore (/ cmdecho)& D& {( M7 Q) o, w- f
(if old_undoctl
: h$ K& A9 }/ |& k& V (progn
$ f' F6 {% ]' ~# r. W8 _ (setq cmdecho (getvar "CMDECHO"))
) j2 }- j3 h0 P3 A; H (setvar "CMDECHO" 0)
* Q8 l! \* |, H4 o% ]- M) |8 G, I' n/ E
(if (equal 0 (getvar "UNDOCTL")) (command "_.undo" "_all")) . |* w6 I9 ^* i
(while (equal 8 (logand 8 (getvar "UNDOCTL")))
: q' w8 C8 b3 g/ ~( ?9 G (command "_.undo" "_end")
0 ?8 h$ g8 J5 d0 t2 H. k ); while
v# e( H5 ?' _- Q. e. d+ R5 y% e8 W1 u4 n# I
4 c- W2 d' q: I5 }: Q
(if (not (equal old_undoctl (getvar "UNDOCTL")))1 W% ~- b0 y- J; v2 p l3 |4 C
(progn
, r/ Y$ L% i4 a# N4 a) G; C9 W& Q, b* G (cond
3 m( d1 h# X e( D; l# x" P1 d ((equal 0 old_undoctl)
8 B8 @8 \ T8 g (command "_.undo" "_control" "_none")) s. ]5 p; O! ]; N- d! a; n6 j
)5 E0 ~0 }/ z# C& A3 X1 N
((equal 2 (logand 2 old_undoctl))
% A3 x8 Z. Z, y6 o, X; t (command "_.undo" "_control" "_one")
& M& H! k/ T2 j )
, g. w& d7 U0 _: N' J7 _8 l- O )5 z! H! `' k. ^: J
(if (equal 4 (logand 4 old_undoctl))9 A9 _0 R5 M+ t! t# {" N6 {6 n
(command "_.undo" "_auto" "_on")
4 I: Y& z$ M* Z+ Q) { (command "_.undo" "_auto" "_off")1 T7 A% }4 ?# H! |
)& V; f3 D# A* s5 w o( b$ H
)
. x! X3 W" w% T$ ], e% I2 s# Z )0 L; }3 F! v# B* X+ E! K7 b
(setq old_undoctl nil)0 n1 j& q' h- X) P: u7 L' B
(setvar "CMDECHO" cmdecho); m8 X ?% e" C, v# R8 u
)7 t/ {+ m7 [& g. ]; n/ g+ }
). j% ?0 a' L# Q3 D+ \
); defun undo_restore9 k, V( i$ B- A- r5 U, N
% G/ E) O" c! l5 h1 l, u, f! K
;----------------------------------------------------------------------------+ X; C3 b4 B, N5 B, N, M' r
; Save variables0 d% g8 J( s" c
;----------------------------------------------------------------------------
: w5 q# S& F( G1 K" E9 {' B(defun var_save (a)
7 g) W# G& @0 U0 x1 L (setq m_lst '())
+ L% {2 b) h l7 g (repeat (length a)
5 j0 C/ W0 F6 K1 e' L (setq m_lst (append m_lst (list (list (car a) (getvar (car a))))))
2 ?& p1 U% _$ W. g2 N6 B, v; ^3 |$ x! b (setq a (cdr a))
6 B8 H1 j" i( J )
: i" g1 S2 ~' V: |/ g. n) L- J- R4 E); defun var_save/ ~6 w, P6 k6 l0 I! v$ W, v
+ u2 t' j8 b" n
;----------------------------------------------------------------------------: A& H0 y4 N/ c2 w
; Set variables
( u& C5 N* a% W" j: S. ^;----------------------------------------------------------------------------, m( w( I4 V+ r; g* g
(defun var_set (m_lst)
g h4 I$ \5 |8 k (repeat (length m_lst)
S& B" f; D' p2 r& |( z6 k% Q* ] (setvar (caar m_lst) (cadar m_lst))
: I, r+ B% L3 J$ l+ T (setq m_lst (cdr m_lst))8 S; w+ q- q+ z% d( y" N$ H6 l
)
* V8 o4 x3 j/ x); defun var_set2 O; Y1 h( R$ r6 R$ X4 s
0 \9 k- G1 d$ M7 T- v6 P8 @. h9 w
;----------------------------------------------------------------------------3 I5 J b3 u! ^0 ^; }$ i
; Restore variables
* V' P n" o7 q' z5 ?* L u* b5 O;----------------------------------------------------------------------------9 P {% U$ r. A5 o& n/ x' V
(defun var_restore ()& d8 l2 e& p$ M$ q
(repeat (length m_lst)1 D1 Q( z9 v7 J" j0 v
(setvar (caar m_lst) (cadar m_lst))5 y% h0 t; M+ G$ U3 `
(setq m_lst (cdr m_lst))
) H; |" V8 ], f* E. W, p )& N3 b5 M; z: A6 G; X
); defun var_restore1 }, w) [' G3 s6 t1 @% F% C' F
: x: q, {' y) o1 N( H6 k
;----------------------------------------------------------------------------
9 D- f" j' o) A$ h- B; Initialize routine 2 ^! G3 a) @% B
;----------------------------------------------------------------------------3 o7 ^$ m& v/ \. u0 B* P! I
(defun err_init(e_lst u_enable add_fun)( s' }5 H+ G( }! M! N7 B) a6 T: y
(if err_alive (err_restore)) ; To avoid nested call
! y' G2 L# X0 j) ~( X7 R& R (setq err_alive T); K& J% _* U7 l, `- Z
(var_save e_lst) ; Save the modes
4 S0 `' D% R' H$ E (if u_enable (setq old_undoctl (undo_init))) ; Initialize UNDO status
* f& J# _; I7 @( B: M+ f/ [' G5 `) E (setq err_old *error* *error* err_main) ; Save the handle of *error*# f. C( ^4 x& q% Q8 U& m+ B7 o& V9 t
(if add_fun ; Add the user cleaner
: ]1 g: K7 o% S$ B& h% k9 w& } (setq *error* (append (reverse (cdr (reverse *error*)))
1 _ V/ e7 q+ l (list add_fun (last *error*))
: m: U# V% B' ^4 F9 @4 Q ); append
3 H1 V3 I0 k, C( k. U5 X3 W )
+ Y. d) j5 h% O, N6 j ); q( n5 e& M; P3 o0 c3 s1 E
); defun err_init |
|