|
|

楼主 |
发表于 2007-3-13 08:33
|
显示全部楼层
undo.lsp
;;; UNDO.LSP made by piggy$ b9 B& k! p! O6 V% `
;;;! I! `6 S% z6 {0 I, w' J
;;; DESCRIPTION; D4 \% H" u/ m' D2 Q* b- l$ V
;;; These are general error routines which can be called by
9 q& L5 K: W4 M* i# a;;; other routine. See AutoCAD 14 ac_bonus.lsp for reference9 `. K2 I# f2 n+ Z0 B
;;;
0 q2 L4 ]0 b8 {, b;;; SUBROUTINE INCLUDED IN THIS FILE* X8 C: t8 ~, v
;;; UNDO_INIT
8 ]2 n7 [3 L: n! c6 V& m, k8 l- E: G;;; UNDO_RESTORE9 {. }4 t z' P2 V
;;; VAR_SAVE
1 D! s. V8 C8 e J, j/ q) P;;; VAR_SET 7 _9 x- F9 S7 |( C" u
;;; VAR_RESTORE* Q( q9 B& K. X
;;; ERR_INIT
9 T( `: U, N' ^7 P& W;;; ERR_MAIN 0 n0 @+ a0 H) M# F) `) P; @4 G
;;; ERR_RESTORE
" A, v5 y. U1 V' ];;;' v; h2 g7 I- u0 m
;;; DATE: 10/17/98; 03/31/998 I7 s9 l: q+ y( r
;;;
$ W, y: K" }7 |% K9 `! X;;; HISTORY:
" [9 M, K9 Y3 h% v" }. i;;; Add routine of mod_att
1 \ e3 ^$ M$ Y& ^9 n5 j;;;
) o+ d- s a. ], b0 j, T: k+ J- A+ K;;; USING METHOD S. P5 C4 T! o- B0 [. y6 T# \6 }
;;; ERR_INIT:
; u% a% T% o. S4 h! R;;; This routine initialzes the error handler. It should be called as:
: a1 Z+ E A% R9 o;;;
# a1 V; [; ^# J4 I" b;;; (if (and (not undo_init)& s, y1 [' N& d1 X G! L$ R) ^' Q
;;; (equal -1 (load "undo.lsp" -1))
$ u" c/ M+ D; p7 p+ V( P;;; );and) P; p4 |. h' a% \# M$ o5 T
;;; (progn (alert "Error:\n Cannot find UNDO.LSP.")(exit))
+ C: _. D5 B( s, B) x" W7 Q; L;;; ); if
* ?5 j0 @7 K5 I;;;
: g& E4 ? K9 H- a1 \8 l: c# y5 W;;; ARGUMENTS:
5 i& F# E" O- K! x6 M2 ]9 B;;; err_init Takes 3 arguments. " k6 a& W* V6 C0 w' U
;;; 1. - The first element of the argument:
$ O9 D" T; [( r3 @4 D;;; This is a list of system variables paired with
: Q& J3 `9 x4 S1 p/ a6 E;;; the values you want to set them to. i.e. '("CMDECHO" "ATTMODE")
6 i1 J" b" Q( I F4 `1 p;;; 2. - The second element is a flag
4 Y( ^4 F# \7 O( L9 A;;; If it is true, then in the event of an error 4 w, N7 ]* |; w4 J
;;; the custom *error* routine will utilize UNDO 4 `1 J* |6 ^2 T9 K5 s2 z. U/ S
;;; as a cleanup mechanism.9 Q3 C" H: p* q) H' F3 S, B
;;; 3. - The third element is a quoted function call.
4 ]1 \$ e; U9 i$ r; D0 Z1 |;;; You pass a quoted call to the function you
: z; U7 T! @6 n$ L5 Q;;; wish to execute at the end of nomal routine if an error occurs. * u, Z; H1 r- w: L) _$ o
;;; i.e. '(my_special_stuff arg1 arg2...)6 t$ j% T# q/ M& J. X% e
;;; Use this arg if you want to do some specialized clean up - D7 Q( A/ b5 w: P" o# ?7 v4 c, _
;;; things that are not already done by the standard bonus_error ( H) L# ~& V+ x, F
;;; function.
" g7 q0 ?8 T# [;;; i5 |' ~) b; [7 l
;;; ERR_MAIN: Body of error routine1 ` r1 T+ ~- ]4 N7 g0 ]/ e' K; I/ M
;;;
- h2 |% [2 ~7 ^- Z;;; ERR_RESTORE: This routine should be called at the end of command to/ F" W8 H M1 G U3 v
;;; restore the VARIABLES, UNDO & *error*.
+ `7 E/ @0 V4 Y) |0 [: y3 q;;;# ?1 z) r% c! V- B
;;; UNDO_INIT: Initialize the UNDO status
: L+ \, Y) U6 q) V! L$ ]$ q% x# k;;;3 L+ R* s, T* c# i" y! T
;;; UNDO_RESTORE: Restore the UNDO status1 u* G" C% e# l7 a3 J& t
;;;
+ D4 j* K' X$ T. [% \- N;;; VAR_SAVE: Save the variables. the argument is like '("CMDECHO" "ATTMODE")3 s& i5 J& K1 H0 A8 N& I% c
;;;6 z( ~& z9 ]7 C- z8 }1 v8 C. {4 m4 l1 F
;;; UNDO_set: Set the variables. the argument is like
; @/ a9 S: q* G& A- v;;; '(("CMDECHO" 0) ( "ATTMODE" 0))
$ s9 b; r$ ]( {$ P6 V% [2 v/ I;;;+ O# N$ H: m" O% g
;;; UNDO_RESTORE: Restore the variables* n2 S: u3 G' Z
;;;
4 M, P, z: v! |7 {8 c( d1 a$ S;----------------------------------------------------------------------------
! r/ Q( H5 w/ L; GLOBALS:
0 d2 u! e5 g: O. A/ o/ q) X7 K; old_undoctl -- old status of UNDO (voided by UNDO_RESTORE)
7 t$ F0 Y5 n/ H- D* _. U5 h7 t; m_lst -- list of variables (voided by VAR_RESTORE)
, s* k; F! ~, U9 g# V9 B; err_alive -- indicate error routine is active (voided by ERR_MAIN or
8 |8 W+ {% o% N P; ERR_OLD)
2 E# j, \4 N6 z% Q" t' k6 ?; err_old -- old handler of *error* (voided by ERR_MAIN or ERR_OLD)* O( F1 ^) B+ ]( P0 _
;----------------------------------------------------------------------------
; n0 k0 ?9 p+ C1 D, f" d8 I: P. u6 C' M7 B7 S; r
;----------------------------------------------------------------------------7 q/ {/ Z3 F' W- W3 K4 W8 X3 y
; Modify attributes according to entity name, attribute name, dxf_item9 q* Q. @" V4 W0 m
;----------------------------------------------------------------------------
0 K- p% T% L6 p" o: m, M(defun mod_att(ent id dxf_item)
5 W/ E2 N$ D' y2 @ a" D9 {: l (while (and (/= "ATTRIB" (car (entgetf '(0) ent))) (/= id (car (entgetf '(2) ent))))
8 J/ [+ G: A d- c! ^ u' U (setq ent (entnext ent))
# P/ |; T- Y+ b: k0 w3 N) l ); while
1 L; ^# M8 I4 N! k4 _ ((lambda (x)
% [" D3 ]$ r0 x* i3 r l (mapcar '(lambda (y)* X+ K( ]( c& c2 L* i. g; t
(setq x (subst y (assoc (car y) x) x))0 o. n6 Q9 I- t7 x' y, D. k; }0 e! D
); lambda! J: G. |5 u1 f5 C* U4 J1 b
dxf_item2 u: j8 n X$ @( a; M+ T* F4 D
); mapcar
# \9 v9 ?5 N' d2 }3 w2 {$ m5 X0 Z2 @ (entmod x)% L# v0 V! J* M( o% j. r3 m
(entupd ent)
5 x6 z! j7 M/ z/ v- } ); lambda A9 z: r( P/ J2 u
(entget ent)+ E$ R) N Q& n' m [# C$ P$ ?" P
)
( S8 W3 E% ~. P" K5 _' g2 [); defun mod_att) _4 m( L8 {* i5 p, O8 Z
* w1 A& j6 j! t4 U9 N' R;----------------------------------------------------------------------------
# y3 ?- H( o0 V( ^; Check layer status, return a association list which contains layer information
& ~) ]1 _/ _: [- A6 a;----------------------------------------------------------------------------, I# w8 V4 g* `- Y* m
(defun laychk(lay / l_sta)/ m; P/ o* ?1 C/ u7 `1 H" a; b" b
( (lambda (x)
7 o5 S" `4 N- B2 o) H9 K9 q( i6 z4 j (if (not l_s)
; R% L0 c# a9 `$ N4 x& c1 b (setq l_s (list (cons x (logand 5 (cdr (assoc 70 (tblsearch "LAYER" x)))))))
) h/ Z# A. ?+ ^5 h) R( Q; o ); if
( \ S9 S; l$ i" o! ^2 | ); lambda
! |( g& ~* @, Q: y# F, F (getvar "CLAYER")
5 m9 y9 x$ Z' c6 _( u a$ D+ i )4 {) `/ \: ^% \1 h# G
(if (not (tblsearch "LAYER" lay))
( H9 [9 q1 p$ ^ (progn ) r( S2 {% ~% U9 Z- h$ t
(initget "Yes No")
& ?4 A+ Z d( Y! v (if (= (setq l_sta (getkword "\n图层不存在,是否建立该图层 ?\(Y/N\)")) "Yes") p# y) @: Z. x* E: d
(progn
& u" S) D1 F& n8 m7 E5 A (command "_.layer" "n" lay "")8 |8 ]! M% R! U8 m8 B3 K4 ~1 i
(setq l_sta 0)
0 W2 V( D6 R) z- _1 e1 i2 U ); progn
+ T0 p1 Z; y) E {$ N ); if/ d4 x9 o8 N+ `9 C' }: o: F
); progn
. d! x, A- Y/ R3 f* H2 j( k (progn# ]3 d* T# u8 H6 N
(setq l_sta (logand 5 (cdr (assoc 70 (tblsearch "LAYER" lay)))))
- V+ ]: R: J3 A5 `0 V1 N (if (= 1 (logand 1 l_sta))
& a- h" `2 c. F4 E4 d" n: `$ v (progn
7 |% ]( T' V# T% Q (initget "Yes No")( H- M# r. A1 A) e* Q- k/ q# c
(if (= (getkword "\n该图层被冻结,是否解冻 ?\(Y/N\)") "Yes")' E2 z! z x S; i
(command "_.layer" "t" lay "")9 Y4 @: w- Z0 U+ q
(setq l_sta "No")
9 S- j5 T8 B; }0 u, U" Q2 Z" ~ ); if
) _4 ^# Z& n0 P+ ~- Q8 P ); progn8 ~2 d" k! z4 q! A
); if
4 y6 r4 A. w' Q (if (numberp l_sta)
3 X" j, Y4 H, A* f (if (= 4 (logand 4 l_sta)) (command "_.layer" "u" lay "")); if9 _4 c, ^! k2 U! ~
); if3 S, |; p2 t7 I
); progn& R2 N! E% U: y" T( c
); if
9 D q/ h; \ Z* p" N ( (lambda (x)# c, Q/ v ?7 {) A N: n1 {
(cond
& [' ?: b3 W y, t2 T ( (not x) p- l q0 P* N0 J
(setq l_s (cons (cons lay l_sta) l_s))
. i0 V3 o' f4 ^! O' G k )
Y2 v/ r7 W. \; ` l7 V4 Q7 T ( (= "No" (cdr x))& t& t; B$ a/ S* R- \" s3 M
(setq l_s (subst (cons lay l_sta) x l_s))
9 z% ]9 m; h) B' G* E } )
6 M3 \. v9 D) O# } ( T l_s)
1 M. M- Z$ r1 Y: P/ P ); cond
1 Z3 U9 [6 n T2 \! _# Y$ S ); lambda2 U. t* r; h* a* A
(assoc lay l_s)
0 f3 `* B' a. R) g5 o" p2 t7 B; d )
6 ^0 D4 \0 U- d% s* c5 o7 |6 x6 z3 E }
); defun chklay$ B7 J# B& b( t$ g
0 w# e7 t/ I0 E;----------------------------------------------------------------------------& u0 H% r+ R0 J
; Restore layer status according to association list l_s
( }: R+ G1 I$ {4 ~3 W- E;----------------------------------------------------------------------------
+ q! Z9 D" I O. T* p g(defun layres()
4 ^8 {& E" I) ?; o# Z (setvar "CLAYER" (car (last l_s))); L6 d7 [) l6 W4 u
(repeat (length l_s)4 K# M4 f* a3 t$ K
( (lambda(x)
% X5 H" b- m0 P% k3 ] (if (numberp (cdr x))
1 G, K1 Y4 {. |2 {; w, p (progn
8 o2 S* Y6 P* n/ S( x (if (= 4 (logand 4 (cdr x)))) W4 g& [: [" a
(command "_.layer" "lo" (car x) "")
# T3 Y* e. ^! z. o8 h ); if
2 `4 N* R& A, g/ z! Y: V (if (= 1 (logand 1 (cdr x)))
$ t9 u0 u. J3 q) p3 y7 B' I (command "_.layer" "f" (car x) "")3 c/ F- z8 q! Z0 F$ ]
); if' S( P- ~( v" P f
); progn
. o% z% f5 ^3 \+ S# I ); if . p, K* v1 U/ R
); lambda
# D9 ?9 M* _. p (car l_s)
6 o% C, V+ R: z& U% C& } )( K1 i* f4 g r5 @& d* `1 b; j+ q& R
(setq l_s (cdr l_s))
# p8 `2 ?# P" G: l1 d( y Q ); repeat
% b2 C) l+ {$ D0 X; A); layres W+ V, {# n$ m2 Z2 x
5 Z; @3 u9 M2 {, I
;----------------------------------------------------------------------------6 j! L. u/ k- R0 G; s8 |
; Get DXF codes$ q* q/ X$ _5 C
;----------------------------------------------------------------------------
( d# t; n \; ?! g# s5 k0 F k(defun entgetf (index ent)
9 [: h1 r- k. j% s1 z2 R3 B ((lambda (e)
5 q+ O$ r8 F3 }* z( |6 o (mapcar '(lambda (x)
$ @$ Q5 t. q, Q: o (cdr (assoc x e))3 e9 ]" d/ q) y, a0 X6 M J
); lambda$ h! {2 R$ ]& w% m) W: y2 k+ R
index) ; internal lambda function
% j6 ]4 k# @) R( S ); lambda
4 j+ A2 j4 k V* W( U* O (entget ent)
) r/ E1 P" @6 b2 }9 q7 \; y )
5 N1 x6 b9 p: J' C); defun entgetf
, Y( L. a4 R) @( C h
# Z2 ^8 D2 W! S+ l/ l& V- z;----------------------------------------------------------------------------
- ~9 {2 F& i2 a4 p4 h; Save UNDO status- @1 q) p; N+ z5 h9 G
;----------------------------------------------------------------------------& L/ W' d2 A2 Z0 b9 ?" E$ k
(defun undo_init (/ cmdecho undo_ctl)
. J- B5 j0 C$ O( V (setq cmdecho (getvar "CMDECHO") undo_ctl (getvar "UNDOCTL")) ; Save the value
, {/ v8 |/ q. H; C$ h2 T% \/ O (setvar "CMDECHO" 0)
# k' U% w1 K0 c2 [9 t1 q' W* f5 u5 X" m H+ r
(if (equal 0 undo_ctl) ; Make sure undo is fully enable; f7 q+ q& e0 u0 G' C) M! m0 Q2 [
(command "_.undo" "_all")
+ X* V% L# h( @% h- X (command "_.undo" "_control" "_all")9 q( C: t4 V. t1 P8 g0 R
)
0 X/ p' X. C# J# Y& Z6 q* _% B- |& X z3 y' M* B' [
(if (equal 4 (logand 4 (getvar "UNDOCTL"))) ; Ensure undo auto is off
0 H2 E' O9 `/ T% _. Q4 f3 g" e (command "_.undo" "_auto" "_off")# Z0 \: W+ C; `3 p9 ^
)
$ V" J/ {" E% B( [2 g
6 Q! f. O0 |7 o4 J% ?/ W (while (equal 8 (logand 8 (getvar "UNDOCTL"))) ; Place an end mark here
- o& Y/ ~! N9 G! ]1 o8 L (command "_.undo" "_end")
( G, G% }9 p* F1 Q' H )8 Q; t8 X D5 c! q) M' G$ ]; |
7 r: `8 T g: C$ G, F (while (not (equal 8 (logand 8 (getvar "UNDOCTL"))))
F$ w& J) Y* r2 Z% P5 \# c (command "_.undo" "_group")
" \6 Q- r( \7 U5 m! i) Z* A& [. Y )1 Y( w4 g- c0 b' ]3 M9 g8 C3 l
0 t0 w; y6 h' ? (setvar "CMDECHO" cmdecho)
l7 b: s7 ]3 \6 k7 D undo_ctl
: o9 C! d( q2 e* W# m$ u2 h) k3 A); defun undo_init
$ a9 ~3 X) V6 v0 [
3 u3 V& x, M! E+ o I0 Y;----------------------------------------------------------------------------% \! N( g! ^' a
; Restore UNDO status6 U1 `5 o# s2 Q q+ ^" \- R4 H
;----------------------------------------------------------------------------: \( [5 R7 ]) O1 ]
(defun undo_restore (/ cmdecho)1 i. T- ?9 M! k+ |% F0 T# k
(if old_undoctl
' X/ U: V4 ], e* {# H (progn
0 D/ ?* P+ p6 c8 @7 G' g; u (setq cmdecho (getvar "CMDECHO"))# p9 J* M- [0 z" m3 `4 v
(setvar "CMDECHO" 0)
& A5 g" M; P4 w- ~! n
& V: ~9 i: t% T, d- i (if (equal 0 (getvar "UNDOCTL")) (command "_.undo" "_all"))
: b0 J& V, e. ?; c" W5 W" F (while (equal 8 (logand 8 (getvar "UNDOCTL")))9 W0 A! I" z# e: X$ L0 j
(command "_.undo" "_end")
+ A6 _0 a, R% r: _' s. S ); while
" S h/ O) e3 k) u% e: T+ c- m5 Y- R8 N* x4 M
2 j! U1 l* _. A- Z, p4 S) s (if (not (equal old_undoctl (getvar "UNDOCTL")))! }" r+ O, Z4 [9 A1 D& F# C
(progn8 F8 B5 {' a6 G% C
(cond* I$ w F$ O. X- L6 ^
((equal 0 old_undoctl)8 J* B p/ }) N$ l: A E
(command "_.undo" "_control" "_none")% b7 Z6 ~" v q0 h5 a! \
)9 x, w0 ~9 w K1 |, B
((equal 2 (logand 2 old_undoctl))
5 l) d/ v; a% B, [ (command "_.undo" "_control" "_one")8 @& n5 ~7 M0 y& _1 j- @7 g
)+ F! h& {7 j. d9 d; o# P/ W4 `, E: W
)# z) J# R# ~ b$ l+ T
(if (equal 4 (logand 4 old_undoctl)): g0 a" }# v- V) K& A8 x1 k- r
(command "_.undo" "_auto" "_on")1 Q0 O8 p7 B1 H- [9 T6 S, _7 b5 K
(command "_.undo" "_auto" "_off"); x. Q0 ^0 f7 A. e" Z
)
* }4 J' p. @( h2 V6 T )' X. }. p- i* u. Z7 H- l6 q }
)6 b' R7 J' J/ G0 t) |
(setq old_undoctl nil)
$ A2 o/ v T' b% m (setvar "CMDECHO" cmdecho)
5 B+ R4 ~: q4 S )
8 J1 b) W' O* j4 W4 v/ j )
( P8 k7 W3 M& H2 ~5 i( f" \); defun undo_restore
: [3 w- g7 g% q& t8 j4 D; x s! i1 R2 ^
;----------------------------------------------------------------------------
3 k6 E r3 Z+ C$ \* V+ m+ V. v; Save variables/ w. o& D b: D) A0 d$ ]3 X) U( F+ Z" j
;----------------------------------------------------------------------------
2 U' w- S% Q8 ~, n) c! Z0 ^" \(defun var_save (a)
: @/ k$ D- ?$ F' v4 g1 l (setq m_lst '())- Z7 \4 ?% l9 `; x: s
(repeat (length a)
& H1 R" Q) q% }3 z$ r (setq m_lst (append m_lst (list (list (car a) (getvar (car a))))))/ e, K6 I8 |2 s9 O' ` B* S
(setq a (cdr a))
- n4 A1 |& J; o* C) {" _" j )' Y5 O% _$ b0 F/ H) |+ u' S: ~
); defun var_save
4 h3 ], L! s- B
- d& V+ a$ t+ s& l' @( @: H& x;----------------------------------------------------------------------------
, S& ^: T% ?; V0 i2 Y2 v, \8 o; Set variables9 y, h. t7 g& K5 g
;----------------------------------------------------------------------------
- O5 N1 p. \" y7 B1 t" N(defun var_set (m_lst)
* x3 U: q, Q; d9 G4 I( } (repeat (length m_lst)7 d' f% a3 p" } x9 I G+ Z ]+ d
(setvar (caar m_lst) (cadar m_lst))
7 v# v5 h0 N1 q9 j (setq m_lst (cdr m_lst))
3 y, B' w! s# c3 Y4 B' Z& U ); s, m4 a( O6 w9 f3 l P
); defun var_set5 ~ b: i! b- @' Y( ]
3 |7 E/ _, W! X: b
;----------------------------------------------------------------------------% v9 A5 a- Z1 f
; Restore variables# c3 T' r+ Y& C+ f
;----------------------------------------------------------------------------! |- h) _; c8 {+ A0 _+ L3 i
(defun var_restore ()
! b7 n% @+ s _ v; x6 v (repeat (length m_lst)
, y) l2 s' p+ }; O! h2 [ (setvar (caar m_lst) (cadar m_lst))% k3 s {8 u) K
(setq m_lst (cdr m_lst))
' v1 r! n" T. M) x )
* g C: E. o; C); defun var_restore
# y* ?! ?& s" q6 P6 X7 x' u
" g; V B$ a( D5 O$ `0 }: I, T;----------------------------------------------------------------------------- l) ^% d! S1 Q6 o: N% y
; Initialize routine ( g0 ?+ B: \! O4 n' a/ U2 @
;----------------------------------------------------------------------------+ \2 \9 m- [5 p
(defun err_init(e_lst u_enable add_fun)
8 l& o. D; J9 b) ] T2 B (if err_alive (err_restore)) ; To avoid nested call5 w& ~) l* Q& y( Z' {$ D
(setq err_alive T)$ ]2 w( \- L0 S# K3 d
(var_save e_lst) ; Save the modes
. C4 f6 B7 j2 ` L* @) K (if u_enable (setq old_undoctl (undo_init))) ; Initialize UNDO status7 \* {: |' Z( n. T/ n( y
(setq err_old *error* *error* err_main) ; Save the handle of *error* Y8 N) F+ I4 \2 G& P
(if add_fun ; Add the user cleaner
) @4 F* R# y+ {+ R1 e* R! B6 p3 i8 @ (setq *error* (append (reverse (cdr (reverse *error*)))
- ^1 |1 |2 U4 L9 ], Q; s% { (list add_fun (last *error*))9 y0 E( Y+ o+ a, c8 f
); append
/ x: g& D. y) C+ z/ w( A6 @. G )3 ]' n3 M* f2 G' @* _8 N
)
0 S- S I7 ?# s2 r); defun err_init |
|