|
|

楼主 |
发表于 2007-3-13 08:33
|
显示全部楼层
undo.lsp
;;; UNDO.LSP made by piggy
' I5 f6 @) H; ^! _;;;
: [, A5 A0 C; ~- |" G8 |;;; DESCRIPTION
1 u1 e: c4 v L. Z8 l' f" Q& A;;; These are general error routines which can be called by
$ W+ v6 U/ m7 D! j- q( U: B;;; other routine. See AutoCAD 14 ac_bonus.lsp for reference1 t& e, I; C& A9 F
;;;2 j* l. N$ z. M9 o6 a
;;; SUBROUTINE INCLUDED IN THIS FILE
: j$ R" O6 B P r;;; UNDO_INIT
) n3 ]- G8 Y$ M;;; UNDO_RESTORE
) S$ G% w5 V$ ]# l. O/ j6 J4 G# j;;; VAR_SAVE ! m$ J2 M4 j* y4 ^1 ^4 o
;;; VAR_SET
3 l6 D3 Z6 Q+ @2 O+ N;;; VAR_RESTORE+ ?1 Q' [: n/ Y8 _, s
;;; ERR_INIT " U( c+ Y; @3 V U* b
;;; ERR_MAIN
, `. \& t) b4 ^4 g;;; ERR_RESTORE
: B7 ]# ~8 b5 P* @4 {; [0 N;;;
1 W1 G7 i |2 o/ m;;; DATE: 10/17/98; 03/31/99
( Q5 m5 {9 u Z2 c; b! M;;;5 o& d$ Q/ @( n; l+ C
;;; HISTORY:
3 X% g# V, L7 |* t& _;;; Add routine of mod_att
2 [, u+ m# c6 v2 a;;;
/ ^) Y" B: A9 `/ s# @6 L;;; USING METHOD
, J1 G" g! [% y% a4 ?0 ];;; ERR_INIT:
9 G" u0 D1 C1 Y4 C3 Q;;; This routine initialzes the error handler. It should be called as:0 F1 C' L( g/ L* S1 a; t
;;;
. `, J( `; e. w( E* [2 a0 Y: \;;; (if (and (not undo_init)# u) [5 R" Y$ Z3 n! v
;;; (equal -1 (load "undo.lsp" -1))
& W) R' n2 n6 W. ];;; );and
, H/ _. }: M b8 X( I0 W: k;;; (progn (alert "Error:\n Cannot find UNDO.LSP.")(exit))/ A) ]0 i7 Y- j8 ^6 }
;;; ); if
' f& [! P! z+ P- ^; t( i' T& j% {2 U;;;
6 r* |% v7 U+ O5 K1 b0 H3 O;;; ARGUMENTS:
! n7 p& i+ L/ f; ^1 x9 T6 I/ O;;; err_init Takes 3 arguments. / Y6 s" m' ~; m) ~/ `9 S: ~
;;; 1. - The first element of the argument:
- }9 ~4 q& L; R& [;;; This is a list of system variables paired with9 _, B# H& W' T
;;; the values you want to set them to. i.e. '("CMDECHO" "ATTMODE")
6 X# O; o/ D) _6 I; K;;; 2. - The second element is a flag0 C- l G7 S8 |5 l5 l( N
;;; If it is true, then in the event of an error ' p# v5 }7 B2 d" h9 P; K" t; d. @
;;; the custom *error* routine will utilize UNDO ! w1 H9 v7 r9 l& a
;;; as a cleanup mechanism.
* ]' F$ b ~% A8 H* t" U! F5 Y;;; 3. - The third element is a quoted function call.) v2 G6 z; [2 ]( V# T
;;; You pass a quoted call to the function you
( F. t3 u* ~' Q, r: D;;; wish to execute at the end of nomal routine if an error occurs.
% r4 r2 m' E* t1 x8 O* l% x;;; i.e. '(my_special_stuff arg1 arg2...)3 C" m! y2 p. ^9 G. G7 L) |/ S
;;; Use this arg if you want to do some specialized clean up
5 B! w, Y; c: ~% Z3 _4 s6 F8 X( f;;; things that are not already done by the standard bonus_error ' }" E& ]. [4 k/ c
;;; function.9 X! R. m; }" K6 c
;;;( F# T# \& v5 }
;;; ERR_MAIN: Body of error routine, i2 |! q3 H5 \$ h7 h; b% g
;;;7 e C0 O+ k6 S* Y; L+ C
;;; ERR_RESTORE: This routine should be called at the end of command to
, [% ~, P6 }: c* I' Q;;; restore the VARIABLES, UNDO & *error*.
3 V+ U B: ~1 ?3 Z;;;$ E n3 S( o7 W- J
;;; UNDO_INIT: Initialize the UNDO status S# E+ I* g+ X: U
;;;* v, C- G! b$ l: {6 [3 [- A
;;; UNDO_RESTORE: Restore the UNDO status
2 G! l8 X$ n. H8 [" [! }' o8 o;;;5 b% U* n0 a1 s7 E
;;; VAR_SAVE: Save the variables. the argument is like '("CMDECHO" "ATTMODE")
8 v d$ x. ^# v;;;5 S' V3 {. e: h& S( a. y
;;; UNDO_set: Set the variables. the argument is like( m. e. F1 E# H" o. C) w0 n
;;; '(("CMDECHO" 0) ( "ATTMODE" 0))
1 v) }( B, s3 d9 v7 V;;;& q' [, }" v0 }2 x
;;; UNDO_RESTORE: Restore the variables3 x, E; G, r$ F! [7 x9 }8 k
;;;' ~5 h* w- Z0 v. T$ I7 g
;----------------------------------------------------------------------------
) ?5 s3 ]; K! r. S7 W; GLOBALS:9 q0 c, I8 D5 w0 ^1 z
; old_undoctl -- old status of UNDO (voided by UNDO_RESTORE)
3 ^( W4 I5 w" a9 R9 U3 C# w; m_lst -- list of variables (voided by VAR_RESTORE). q0 ?$ n0 G5 G/ A
; err_alive -- indicate error routine is active (voided by ERR_MAIN or
. ^" |5 |7 f2 m& H9 N0 K9 [% m; c; ERR_OLD)
5 L2 T; E" i, \8 K9 Z' j9 s; err_old -- old handler of *error* (voided by ERR_MAIN or ERR_OLD)8 y: ]. t) ^! H
;----------------------------------------------------------------------------5 E9 }; k+ X+ B5 _
4 u6 P6 {5 h$ J+ N! f9 t7 M" I: E;----------------------------------------------------------------------------
7 L0 R$ y6 Y" B% a( ?& ]& q9 ?* h; Modify attributes according to entity name, attribute name, dxf_item5 x) E9 P- Z/ E$ @
;----------------------------------------------------------------------------1 x* {9 y. [. V4 g# {! `
(defun mod_att(ent id dxf_item) - R. W% y3 G# k% V+ H
(while (and (/= "ATTRIB" (car (entgetf '(0) ent))) (/= id (car (entgetf '(2) ent))))
" a% r6 t8 z9 o2 i: N (setq ent (entnext ent))
) n+ H4 X; ]! B/ w4 i$ y" M ); while
2 e# _' n: ~+ c0 j9 C ((lambda (x)
1 W' F# N% d6 {$ t- d; P/ z& L; ~. j (mapcar '(lambda (y)+ |' O; j3 i( }
(setq x (subst y (assoc (car y) x) x))
$ @0 E& ^6 m! f# l, W' N$ k( L' N4 B ); lambda% [1 X9 c, n2 x) ~0 T% q
dxf_item
" c( [$ C0 t6 m7 x( S# N0 m ); mapcar( s0 ?9 o3 }1 h
(entmod x)$ J h' ]/ Q0 f/ m( ~4 V
(entupd ent) |: F! o W7 p3 ~5 X& z
); lambda
) N% @% W8 U3 [, S6 ]* v) ^2 `. U (entget ent)
% t6 {5 a& U, P6 @- o! |1 }! q )
5 h7 M4 _% F5 J, q, t3 j3 F); defun mod_att, P) d* I. w% u/ p4 S$ R* h
$ R' G' b( F9 U7 v; W# J A
;----------------------------------------------------------------------------# o7 [6 J0 u, J8 L# k4 Z' m' D
; Check layer status, return a association list which contains layer information
* U" j: E* J9 t! ]. W; O0 o% i" E;----------------------------------------------------------------------------
0 o+ {0 H6 W+ U& F9 S$ Q6 {( t(defun laychk(lay / l_sta), z% ^3 p5 U- G/ r" a
( (lambda (x)
% X2 N) t. i5 \2 ~5 h! i2 r: H (if (not l_s)( L! \) ]1 a1 o, m. @- k0 o) D
(setq l_s (list (cons x (logand 5 (cdr (assoc 70 (tblsearch "LAYER" x)))))))
* t: J3 c+ m$ v2 ~2 P$ S+ t! o2 J ); if" R8 z% j5 Y5 k2 D! K# R+ K
); lambda ( K. m; A2 q- `
(getvar "CLAYER")1 L% J& w6 ? K8 i+ U9 A0 w
)
- V' m: L7 `# t1 t S: g3 G (if (not (tblsearch "LAYER" lay))
: i0 Y# X: l [4 P: R (progn % h% Y' }, P. O9 Z9 P
(initget "Yes No")
- O: E+ f! Q! B6 ? (if (= (setq l_sta (getkword "\n图层不存在,是否建立该图层 ?\(Y/N\)")) "Yes")
( e+ E6 u0 x$ H; C' z, s* u (progn9 |5 v& d% m9 S6 f* m. o1 B
(command "_.layer" "n" lay "")
1 K% q: W% }/ } m (setq l_sta 0)
# C& g: T5 V$ {! D+ b ); progn
$ e/ ?. X- T& D ); if
1 a( U& ~6 \% m9 S0 A ); progn) C, s. |4 V) G, d1 C
(progn
) s" i- @7 l" m4 f( C2 T( w8 ~8 p8 c (setq l_sta (logand 5 (cdr (assoc 70 (tblsearch "LAYER" lay)))))
5 T: {( h( I, O- [: W i# I F (if (= 1 (logand 1 l_sta))
|1 t* X* ]! J- r (progn+ y# G) ~4 S) u- M, `$ N
(initget "Yes No")
, G( Z& c: b" r- l1 m1 j" T (if (= (getkword "\n该图层被冻结,是否解冻 ?\(Y/N\)") "Yes") K- A) g/ d( S2 y8 [# M
(command "_.layer" "t" lay "")1 {" F4 r1 C* o: |- n" F' }
(setq l_sta "No")( h9 ^& U5 ]7 K4 }! _
); if
. z& o8 \( C3 w9 V# M ); progn
* W4 Z$ _% }6 k4 e4 b: G \ ); if
# W2 E$ D+ d# G' e: M; T6 H/ m (if (numberp l_sta)
( j; ~2 i* r. T; q/ M# a (if (= 4 (logand 4 l_sta)) (command "_.layer" "u" lay "")); if3 O) @1 s; p0 p, i# c* r0 ^; H
); if1 P1 F' N3 G$ s' T5 ], g/ S
); progn7 q- t! Z, Q; ^$ D- z" h# ^+ R1 T
); if# [: L$ ~: q9 [
( (lambda (x)
e: z% R+ g& G0 D7 B$ F (cond
; I8 j* M6 a M/ ^0 {5 I# Y ( (not x)
7 s2 @/ h2 s1 Y6 L+ [* {: x (setq l_s (cons (cons lay l_sta) l_s))0 ]! ~- F3 n+ i& s
)+ [: X; X5 H" W5 t4 Q
( (= "No" (cdr x))" K& K( N7 t9 A4 ? K
(setq l_s (subst (cons lay l_sta) x l_s))
0 V3 G% c+ z: Y+ i9 ]9 y )! X9 c* [$ A- a H
( T l_s)! r% r T6 P' |2 B+ a0 s
); cond+ p6 n, q5 p. A% P- v& D
); lambda" T' L# e( g! f3 a
(assoc lay l_s)6 Y3 s; y! a9 G& V
)
1 ? g& M: Q+ u' V, {. {
N% b/ N- Q7 [); defun chklay
4 J1 Q/ g5 O, l6 ^# K2 \3 y2 ]5 ?+ O
;----------------------------------------------------------------------------9 v2 l) n% N. D# k
; Restore layer status according to association list l_s* j$ a2 M7 u+ _! E* I
;----------------------------------------------------------------------------
* T" u D* C1 W8 d$ \(defun layres()+ j+ g1 I8 r/ e' |
(setvar "CLAYER" (car (last l_s)))* f% D: A- \' M& c* g G
(repeat (length l_s), u% W8 @4 m% E* f5 I1 c
( (lambda(x)
) b5 O9 S% s% j! @% v! f (if (numberp (cdr x))
+ V1 Y% { z' o5 g (progn
' i4 @& X% g8 t6 | (if (= 4 (logand 4 (cdr x)))& P' d! ^4 V, `8 m
(command "_.layer" "lo" (car x) "")& G! s& m4 m9 ]8 N- I2 v
); if
# I+ a4 E6 W7 O: a8 k (if (= 1 (logand 1 (cdr x)))
2 V+ `* ?; y3 }: ^# G6 Y (command "_.layer" "f" (car x) "")( F$ O9 `) J, `8 \5 G6 u* G; L
); if3 f3 n q* l( Y+ g3 n
); progn: R- j# _2 W g
); if % M% i: F$ b: G. w: m5 \5 b( G
); lambda! V/ z+ M. c4 W7 x* b' s, B0 C
(car l_s)8 x7 ^0 W& e: q Q8 u9 M! |
)
( T' A4 O# Z1 u4 P (setq l_s (cdr l_s))
! V! `3 J0 v" r9 } ); repeat$ ~2 U2 O% [& e' X' ?
); layres
4 Y' C7 H7 _# p4 Z& v. W+ u% T/ ^6 S, c+ C4 E w
;----------------------------------------------------------------------------
4 b6 y y# x) }- J; Get DXF codes Q1 Z3 L% ^0 [ P
;----------------------------------------------------------------------------1 n; t5 Y2 ?5 t. F
(defun entgetf (index ent)* ~. `- d& a# S' ?6 F
((lambda (e)0 J1 Z& w& J) W9 {7 Z5 v% p) D
(mapcar '(lambda (x)# c9 @( S ]8 D4 W1 M* O
(cdr (assoc x e))
4 q2 h5 f. y1 `9 ?2 @7 A ); lambda
C2 x: u4 M1 `/ X) D3 k index) ; internal lambda function
3 L/ D$ z6 Q6 Z4 w7 }; | ); lambda
+ y, B1 _. h3 L4 M5 v (entget ent) & p n* Z1 @/ A3 t+ C4 P& X
)
) s# K+ G+ r i); defun entgetf
' F2 d* |( |5 |+ s5 Q
8 c9 T, R% G1 @* a+ J;----------------------------------------------------------------------------' Y1 h" w! h" D: g
; Save UNDO status
b1 f4 p# q/ [ q( V q;---------------------------------------------------------------------------- [$ b5 g, b3 O4 v* J, g9 A
(defun undo_init (/ cmdecho undo_ctl)
$ a% W, Z; |3 X, B) E, h& l T (setq cmdecho (getvar "CMDECHO") undo_ctl (getvar "UNDOCTL")) ; Save the value: p+ E" u" P$ y$ Q0 I
(setvar "CMDECHO" 0)
8 p- `, r+ w3 d. ]- u) }; w3 R0 l8 Y2 V3 Y' e
(if (equal 0 undo_ctl) ; Make sure undo is fully enable L' o, m7 u$ p
(command "_.undo" "_all")7 U3 z+ P1 r! B- U" i& M; F
(command "_.undo" "_control" "_all")
# o$ d$ u7 E" D6 k6 u: K) i )6 U! J5 ^- U% O" t+ d% D; f
/ x; V9 o) S: J' N* N
(if (equal 4 (logand 4 (getvar "UNDOCTL"))) ; Ensure undo auto is off' D' }) r2 }# x* Q
(command "_.undo" "_auto" "_off")
- Y$ z4 I- O1 \ W9 K% Y3 W1 Q )
4 r4 O9 v- U% t4 }* L- v" @7 h: ?; z+ p) |% ]/ Q
(while (equal 8 (logand 8 (getvar "UNDOCTL"))) ; Place an end mark here; A' E( n1 a& x7 A) f" K! u. k3 M
(command "_.undo" "_end")
# j: f# i/ G# F: J )6 K# {5 j' @/ s9 x5 Y( L# B8 }# n
# q; G$ T: f& a& V. |5 I! \, b7 i (while (not (equal 8 (logand 8 (getvar "UNDOCTL"))))
- Q! o% B R. o7 m- [6 `$ G (command "_.undo" "_group")- b9 |9 Q: w' m; I
)0 V3 B. Q9 Q8 J* a
. R+ l/ {% H! ]' V& E) n
(setvar "CMDECHO" cmdecho)$ h: G( G" O! H
undo_ctl
& L2 `( |3 v- O); defun undo_init
. z& n) ^' r" d3 m2 ^2 E6 a' {- e, W" ?) K. x- i
;----------------------------------------------------------------------------
1 \" n1 i9 `6 c+ F$ }; Restore UNDO status- G/ `' S; m- k8 p ^8 |9 k
;----------------------------------------------------------------------------
q5 m5 Z4 c& S* A/ ~- y) \(defun undo_restore (/ cmdecho)9 Z: ?9 m8 u- Y
(if old_undoctl
& s/ U: m6 Q" v (progn
( Q" @6 E8 w$ \ (setq cmdecho (getvar "CMDECHO"))6 J* t" [/ L: c/ c; L' {5 @1 {
(setvar "CMDECHO" 0)# k& T5 Q7 q" R) I' R" Z( h
% B3 y; z9 H/ d- ]; Z2 |" l+ P
(if (equal 0 (getvar "UNDOCTL")) (command "_.undo" "_all")) ) I. T# N1 a! H) X" E* g, h
(while (equal 8 (logand 8 (getvar "UNDOCTL")))
8 s5 K. M) y5 T (command "_.undo" "_end")- C- T; J. p* q% M* v( G
); while5 p& t3 x2 a7 r5 l/ @! ^
. ?% j! v/ X4 {4 s) a# r2 ?) F
' l. a1 o$ I1 u0 ^1 E7 S+ m
(if (not (equal old_undoctl (getvar "UNDOCTL")))
! @# O1 a" z1 z9 K/ K: w- ?8 q (progn$ } \- A1 T; p. X, j9 }1 n* Q
(cond1 i4 ~2 B$ L$ B! j1 }/ o
((equal 0 old_undoctl)
1 ^ s6 u& m' U. h4 A (command "_.undo" "_control" "_none")& u# ?2 K7 z/ E: @; n' v
)- v* B. K# y' Q" \
((equal 2 (logand 2 old_undoctl))) j" h8 Z4 Q6 h3 G5 O% r
(command "_.undo" "_control" "_one")( _7 p: c# b0 A+ b3 L
), E( C' o1 S* _9 U! q& a
)/ B" d X" |' g6 g* e* U0 A, ?: @
(if (equal 4 (logand 4 old_undoctl))
S% T7 f- N" j1 _ (command "_.undo" "_auto" "_on")
; K, m5 W; c% T (command "_.undo" "_auto" "_off")
/ T) [, h* |1 V1 Y. ? )8 M5 N0 l! n6 F
); R$ G2 D( t ?- `* o* U0 ^# U, S8 h
) l% d, K9 ]% o4 g2 T8 B+ M8 u5 a- O
(setq old_undoctl nil)
$ ^0 A. Z/ g, w4 e! r! N1 s (setvar "CMDECHO" cmdecho)* i6 A, l( d' n5 }1 @9 X7 x
)
$ a# h, i3 \8 U- l2 P7 w7 ]! V: k )
% Q3 K0 N" p* ^. E- `); defun undo_restore4 P" ~) D$ `9 c% W
2 r& ?2 u* W# M8 n2 V
;----------------------------------------------------------------------------6 q( |+ @* B( n2 o; b" X1 p- Q
; Save variables
7 H1 B g5 @" A# v* w" t- p8 V;---------------------------------------------------------------------------- S9 Y/ e# ], h, c
(defun var_save (a)- ~9 O! f* I% f8 w! Z/ Q9 h0 T
(setq m_lst '())& h7 I3 N" A3 D& L6 B3 C' l8 r
(repeat (length a)- ^0 v3 P: @& I" u& k
(setq m_lst (append m_lst (list (list (car a) (getvar (car a))))))
) V# q: ~3 Y& l+ N (setq a (cdr a))
7 x$ k; z7 Y/ R1 Z7 R9 n4 y* Z" b )
4 F5 w" X3 s' U/ L5 _# |); defun var_save0 w O# J& [) u+ x% F: P6 c
1 o* u/ ]# P6 L1 y+ F8 u) k;----------------------------------------------------------------------------
& O' |. R( p0 o( ]' {! n4 Q7 ~/ h4 x; Set variables! O0 @' b5 c6 ^8 A* W( C
;----------------------------------------------------------------------------
0 ?" a8 O! U3 f! \(defun var_set (m_lst)# p. U0 @1 c& N8 a
(repeat (length m_lst)+ A/ n) r2 l+ @: b
(setvar (caar m_lst) (cadar m_lst))
$ G" r+ S/ X5 g9 ~ (setq m_lst (cdr m_lst))
2 c# F" s2 ?) z9 V ); q, \# _: R; L- l0 D
); defun var_set
4 y+ F, V8 h, ]0 k& S' S! \- M7 X: t9 Y6 Z
;----------------------------------------------------------------------------
- e2 t2 Z+ P6 J" K4 t* ^! s: ~2 y$ U& ?; Restore variables
; C6 O3 |4 A4 p;----------------------------------------------------------------------------
& P# u# l5 C9 s- z0 m/ l" ^/ o(defun var_restore ()
3 b8 T8 d# S! W% j (repeat (length m_lst)! I; v1 j4 M5 Y0 U: }7 r' k
(setvar (caar m_lst) (cadar m_lst))
7 C+ w0 {: Z0 i (setq m_lst (cdr m_lst))+ Y) }1 a: Q) d8 r b
). C2 G; M8 k) q4 x9 M: I
); defun var_restore
" c/ T# h9 T5 c3 W6 g- c6 o+ H y2 n* a% s. i/ C+ y& O
;----------------------------------------------------------------------------
+ l( |6 O q& r r; i6 s( M; Initialize routine
0 f; w- f7 D3 M! o1 x7 J;----------------------------------------------------------------------------* n: E* A; B& _9 s* f
(defun err_init(e_lst u_enable add_fun)) X2 k/ z& A% X7 e* t
(if err_alive (err_restore)) ; To avoid nested call5 n9 s- _9 G+ Q' c
(setq err_alive T) i/ B) _/ d, I8 ?
(var_save e_lst) ; Save the modes
/ [6 E. {2 l& r9 O7 x5 y (if u_enable (setq old_undoctl (undo_init))) ; Initialize UNDO status7 r6 z% K2 ^- {1 {4 a6 R, k- L
(setq err_old *error* *error* err_main) ; Save the handle of *error*
6 `& A3 A! F% p$ P1 | (if add_fun ; Add the user cleaner
& p, H" d9 B' q! `, d. @- R (setq *error* (append (reverse (cdr (reverse *error*)))# t, x8 T# N% {; ]" u% Q; t& Q
(list add_fun (last *error*))
0 n: V9 }* ^ l ); append% \- q. B+ q; y2 V8 Q$ W& R! @- y
)
. \; L- @ y. b( f )
5 x$ h( j! m H9 L/ i. v0 K); defun err_init |
|