|
![](static/image/common/ico_lz.png)
楼主 |
发表于 2007-3-13 08:33
|
显示全部楼层
undo.lsp
;;; UNDO.LSP made by piggy
' B s. G3 v: G5 N% w9 D;;;; d6 a$ _1 D, _" A. P
;;; DESCRIPTION
& C/ G1 p8 }' V% i2 X k4 u) R( F. ^;;; These are general error routines which can be called by . V2 Q( t- Q' a. ~, s! ]
;;; other routine. See AutoCAD 14 ac_bonus.lsp for reference
; H' ~; U# b; ]) P4 W: p" v5 E;;;
* P1 v1 }4 y" j% M; [# m;;; SUBROUTINE INCLUDED IN THIS FILE; @ V0 N( }1 e w
;;; UNDO_INIT
" w! g$ f* |# ^ H; V" V' ^3 N0 g5 n;;; UNDO_RESTORE) q+ n( D1 l1 V( b6 @, l( X1 S
;;; VAR_SAVE * Y$ O+ S) v9 O6 f; D4 \
;;; VAR_SET
, A! h* e3 b& R9 k4 ];;; VAR_RESTORE
% S* ^& v- R4 o1 l5 E;;; ERR_INIT # L4 k% f t( T* i K4 D9 ^* G
;;; ERR_MAIN / T6 Z' e/ G" Q
;;; ERR_RESTORE
9 \& T. X; `" F; z;;;5 I! W( N& ?2 F h8 C
;;; DATE: 10/17/98; 03/31/99
+ X0 q3 j* a" r! V;;;7 `6 k- s- p5 q6 m1 O
;;; HISTORY:
. G* g" V0 \& o. J7 x* T+ p;;; Add routine of mod_att
0 w& q$ a- v% P" _/ N;;;
0 ?# |, w3 E6 i# |, J;;; USING METHOD& X$ A E! B( L
;;; ERR_INIT:
+ N2 Q3 Z( ? a;;; This routine initialzes the error handler. It should be called as:' _- n, Q0 |" K- L2 r/ U
;;;
. F: u( F2 U# L& L. E/ L6 @4 {;;; (if (and (not undo_init)
4 T0 I( f5 n' m;;; (equal -1 (load "undo.lsp" -1)) 6 ]8 K/ u7 w7 T' K. F
;;; );and
2 I4 j% \8 Y; J* F;;; (progn (alert "Error:\n Cannot find UNDO.LSP.")(exit))
1 g* P2 l, M F f) x;;; ); if/ A/ _9 _; P3 {: g" n0 o0 i* d( ~
;;;1 C7 R4 y. K7 S/ c# |; {2 b
;;; ARGUMENTS:! E2 F1 M; e! c) t* P
;;; err_init Takes 3 arguments.
, s5 r" \/ p6 ^4 o;;; 1. - The first element of the argument:/ ]7 p. Z) ]0 v/ p& }+ Z1 _, Q
;;; This is a list of system variables paired with. W! z: U& F/ v/ W6 f' T2 t
;;; the values you want to set them to. i.e. '("CMDECHO" "ATTMODE")2 I1 G7 r# }& t% H: D: N: N
;;; 2. - The second element is a flag# `, f) R4 K1 P/ l9 t9 p$ n
;;; If it is true, then in the event of an error 4 x T: B; q7 b
;;; the custom *error* routine will utilize UNDO + Y' B$ V E, H
;;; as a cleanup mechanism.
1 E, V; s# B* P( O7 r& b;;; 3. - The third element is a quoted function call.9 B- Y, h! G6 L& V* i
;;; You pass a quoted call to the function you
7 ]. H" f- M7 T+ b6 ?;;; wish to execute at the end of nomal routine if an error occurs.
& `8 K; [( a: P7 L2 f' p;;; i.e. '(my_special_stuff arg1 arg2...)' Q% ]3 k3 x- v2 Q4 y
;;; Use this arg if you want to do some specialized clean up
' y* g& ~; }5 u* F3 x3 I t: v;;; things that are not already done by the standard bonus_error 9 ]( Z! ?! X, I y1 s/ N
;;; function.
, V, M m) N$ p' e;;;
% D/ X- W4 d7 |) W3 a# D1 G% F+ `$ a;;; ERR_MAIN: Body of error routine$ S# w' e j" `' M/ f5 o+ L2 r
;;;
0 P" u; g k1 ~;;; ERR_RESTORE: This routine should be called at the end of command to8 a, o: R" Q% B! q5 k
;;; restore the VARIABLES, UNDO & *error*.6 Q, p- g' c; y+ u" R. y
;;;, ^, ]9 I% O9 t5 N
;;; UNDO_INIT: Initialize the UNDO status6 I- s$ t1 }3 l# ~5 Q' A
;;;
+ Z8 a) v/ q6 `( W4 f6 k;;; UNDO_RESTORE: Restore the UNDO status
c% T' [$ s0 ]5 w- J0 w% o2 g;;;4 i. G) ?5 Z) m( h( J4 l) Y+ |$ P0 q7 i
;;; VAR_SAVE: Save the variables. the argument is like '("CMDECHO" "ATTMODE")7 z g* Z* w& `+ T$ k) K8 _
;;;
/ ^% c4 E/ U6 M1 y3 I; g;;; UNDO_set: Set the variables. the argument is like
- A, `0 I+ K& F# V3 w;;; '(("CMDECHO" 0) ( "ATTMODE" 0)). b6 ?( b" g7 f* n
;;;+ a8 S" o3 {8 n2 v0 l" v" x
;;; UNDO_RESTORE: Restore the variables, h/ V4 k- A4 z( W( J, V
;;;
* ^( Q+ i. t, L$ d! M; _;----------------------------------------------------------------------------- c! @2 S- ^2 |; Z& I& g$ |
; GLOBALS:
% T4 {1 q# j# F; old_undoctl -- old status of UNDO (voided by UNDO_RESTORE)
$ n* }( p9 n4 D7 y2 D; m_lst -- list of variables (voided by VAR_RESTORE)3 K6 c( K2 X, y1 G q7 s$ I) D
; err_alive -- indicate error routine is active (voided by ERR_MAIN or( B# U6 f z0 r/ b3 a6 G
; ERR_OLD)
4 n. E) R- G$ ~! a; err_old -- old handler of *error* (voided by ERR_MAIN or ERR_OLD)3 ^% c2 } Z* d( T5 A
;----------------------------------------------------------------------------% |& ?5 c" ~% E6 \' y% F5 ^5 Y& e
) c1 h7 t( r" A" S7 M;----------------------------------------------------------------------------
" v2 }# }( f, `- v* o1 F2 p9 F8 ^; Modify attributes according to entity name, attribute name, dxf_item8 W: G8 u$ Q& J8 K
;----------------------------------------------------------------------------) |5 `0 M* v0 g7 Y/ a
(defun mod_att(ent id dxf_item) - s. p2 y3 a! \+ e' s
(while (and (/= "ATTRIB" (car (entgetf '(0) ent))) (/= id (car (entgetf '(2) ent))))
# d! m% t' i9 O (setq ent (entnext ent))3 W; Z& \: {9 n U
); while ) u- d7 S8 e3 N( U9 C0 v) ~
((lambda (x)9 i6 b( r: H& d6 P' V$ \1 O1 {
(mapcar '(lambda (y)
; p* l& x# {# C3 I. c* R (setq x (subst y (assoc (car y) x) x))
R* o( L& Y; y# [( P ); lambda M2 |# t8 `4 }$ l3 N' P; V2 ?2 _
dxf_item. v' A3 M+ m9 v0 h+ W' h
); mapcar9 @' f/ ^) P- C- @
(entmod x); W( Z# u& b% L; n; _& U
(entupd ent)
& q$ s1 n* N8 S" e0 o2 o ); lambda& H0 F. x) `; f. y
(entget ent)+ y5 P$ w" B( h7 b1 a5 ?7 f: ]8 `) i
)
) C% }% K+ H. w- |( f, h3 P); defun mod_att
) y$ d# \4 B$ F0 J, a1 v: `& S* A# V% `
;----------------------------------------------------------------------------$ {1 B; F# e- o
; Check layer status, return a association list which contains layer information
6 i: I! B% v, O7 G$ `7 m) W;----------------------------------------------------------------------------2 T& i, G& k: ?6 a
(defun laychk(lay / l_sta)9 o. k$ N8 H% f0 S+ Y7 I3 H
( (lambda (x): k S0 c6 u2 M/ [1 V7 n
(if (not l_s)1 C/ o) D: e; |; L
(setq l_s (list (cons x (logand 5 (cdr (assoc 70 (tblsearch "LAYER" x)))))))
" F7 y7 T- ]2 M4 I6 j t7 f+ e( U ); if
5 s/ X. o/ R6 b z, [ ); lambda 5 w* F) [# k* c9 W& O$ _* n
(getvar "CLAYER")
2 I0 p3 C4 b6 N* [ ), L1 r7 L9 h# I Y: a; f
(if (not (tblsearch "LAYER" lay))
7 S5 h9 D: Y7 j) U+ D* N (progn 0 C3 `8 v2 m3 c2 h
(initget "Yes No")
7 a4 V. Z, Z6 |8 Z l (if (= (setq l_sta (getkword "\n图层不存在,是否建立该图层 ?\(Y/N\)")) "Yes")
6 ?: b# i! T; N2 b5 \ (progn, v n$ _! N! }/ _! b
(command "_.layer" "n" lay ""). C0 u. \& K' }% U
(setq l_sta 0): X/ s* l- m/ E" c6 c6 W7 S
); progn
( d+ m. y; g! |5 w$ ^ ); if
" F, m! G8 ]+ g/ B: l( h; u ); progn
9 u; ?# |/ t- T (progn
$ v3 s G$ `1 D* Z, k8 V% g (setq l_sta (logand 5 (cdr (assoc 70 (tblsearch "LAYER" lay)))))
' `9 J$ W# x6 { (if (= 1 (logand 1 l_sta))
" U& q+ L/ |# U& M- s, d9 e (progn
6 k6 |, w% J$ [$ ~, u( i4 D (initget "Yes No") }- j& \9 p( m# M5 `7 R& J- D9 @
(if (= (getkword "\n该图层被冻结,是否解冻 ?\(Y/N\)") "Yes")
' m& i: L1 ?9 g" v! i* H/ n' i2 A. X (command "_.layer" "t" lay "")" o" @, O! b7 y. K
(setq l_sta "No") N- E+ _6 c8 f" ?
); if
4 x5 I& F( `2 y/ R* x% ]. g) D ); progn
; ^$ b. v' [$ y- U; W% q( i, X Y ); if: o8 [0 I# ?% W$ o% V! q% P9 {9 x+ N% P
(if (numberp l_sta) 9 A4 ~0 A; t5 h+ h1 }9 K$ a
(if (= 4 (logand 4 l_sta)) (command "_.layer" "u" lay "")); if
% {' Z H( Y* L) \, e# ~) h ); if ~# X; s1 w/ T7 V; [
); progn
- a5 @( T2 I/ w4 a3 ` ); if B1 n- ], V/ I6 x( o* o$ F
( (lambda (x)
: P# i# O, P2 l# F' C- G. f! y (cond
4 S% u; u% Z) ~( Z7 O' c( ]" ^ ( (not x)
# Z) R6 T/ T1 X' | (setq l_s (cons (cons lay l_sta) l_s))
6 m( C+ V' m$ S% r, H/ u7 U5 Z3 P )
c1 ?9 \" Z# C0 z ( (= "No" (cdr x))
: S) T( G6 P7 K& [ (setq l_s (subst (cons lay l_sta) x l_s))# K+ Y: Q" a/ y g8 c" N
)
5 e5 l& S! m# Y9 @- \+ L3 r" q- o ( T l_s)
- u4 \2 W4 f: ?% N" _ ); cond) `, B4 x w1 i( B
); lambda, F$ T4 c8 S* R3 V' c
(assoc lay l_s)
! B- @3 y" O- i% L8 T )
# R& W, w% L3 P4 s. s+ |) c3 y
2 _! ^( G* O$ G1 O$ J/ \$ B% g; p); defun chklay
$ J% h. }' u: X; |4 ^8 R) w, |4 Z5 S0 h; j
: R8 A8 P# X+ A; [" }; n( F. D;----------------------------------------------------------------------------
( m8 Z' F0 X3 r2 M) H7 c; Restore layer status according to association list l_s
( V0 ^1 Y9 U* S" P2 i% h, m;----------------------------------------------------------------------------! ~, g! t- ?7 f3 _- b
(defun layres()( w- [& E4 o& O# a% V# L+ k2 B
(setvar "CLAYER" (car (last l_s)))! ~5 ^3 C& q. `# s' C1 x
(repeat (length l_s)) f; f+ B. k, ?/ E
( (lambda(x)
$ ]; W. v, A2 B( t1 \% r (if (numberp (cdr x))$ U( w/ }* {1 j- B6 w. i
(progn - q- [7 V. n- ?$ A0 [
(if (= 4 (logand 4 (cdr x)))
) k* W J9 Z) m# x. r# L4 _/ x (command "_.layer" "lo" (car x) "")
9 P. M5 ~+ n& n8 I: n7 h0 C- U ); if
$ E( p4 a5 ^- D& A( z6 z (if (= 1 (logand 1 (cdr x)))+ [& K/ M# G5 A& ?! c
(command "_.layer" "f" (car x) "")
$ G1 @2 E4 h( E- L; r6 g ); if
/ k9 ~" M( Y0 o+ f- m) l ); progn
5 a' I+ L) y* Q5 X ); if
; L$ o- X3 G4 F# J% q3 F ); lambda
9 [" i! c0 l8 [- a& L (car l_s)6 H( ]) p" Z" @
)! P" D* Y/ S# m; l+ t
(setq l_s (cdr l_s))
( @$ R5 x; @3 I9 ` ); repeat
; y& `5 Q$ o9 k% J); layres
# l0 u4 Y7 r" l# ^- \4 _; ?" b1 V8 j3 k) c
;----------------------------------------------------------------------------
5 _1 S/ w" r3 \4 h; Get DXF codes
, \2 p" p1 V; T;----------------------------------------------------------------------------
) Z! i$ ^4 }! z3 o: M7 e(defun entgetf (index ent)$ u! \& |) R( l. X
((lambda (e)
" D6 F O, L& g0 q% C* p, p (mapcar '(lambda (x)+ v# G2 |) t# T4 Y7 o) P( m: |
(cdr (assoc x e))8 s5 x+ |& j, H9 n* z
); lambda
) E1 Z5 f# w3 z: B6 {$ | index) ; internal lambda function7 y% q; E, c2 R1 D0 n. g8 b
); lambda
/ \7 A2 h* v: K4 z9 P W (entget ent)
; F4 V, T5 K3 A, u, z )0 U5 a( V8 b( w: R1 N! G! H# o* g
); defun entgetf: l d/ l6 Q- K8 V5 T
- m" j% f' p! S" [2 D
;----------------------------------------------------------------------------! j& H3 E- O7 A! U5 c
; Save UNDO status) B* G$ P. Q) M0 R% d5 D
;----------------------------------------------------------------------------( y! P' T1 b! X4 n
(defun undo_init (/ cmdecho undo_ctl) , \1 C1 _6 C9 M
(setq cmdecho (getvar "CMDECHO") undo_ctl (getvar "UNDOCTL")) ; Save the value) @# T ` p" q
(setvar "CMDECHO" 0)1 s5 h* [& Q) u2 w1 Q# `* Z+ _
( c) c4 U8 [$ e7 |) {: c (if (equal 0 undo_ctl) ; Make sure undo is fully enable9 J9 b6 D5 y f% d
(command "_.undo" "_all")! N6 @! M( p" M" d* h
(command "_.undo" "_control" "_all") W( f& Q- t# I3 s( }" t
)- \+ P$ R/ S. K) S; ?
; S U9 H+ q3 A, J
(if (equal 4 (logand 4 (getvar "UNDOCTL"))) ; Ensure undo auto is off
# ]! G- Y$ a9 I3 G) F! u5 a# ~* y (command "_.undo" "_auto" "_off")) Q9 u( f+ K$ O) e/ u
)$ n! v1 N! p( t8 Y2 k7 d
, u' |8 ~! ~9 j
(while (equal 8 (logand 8 (getvar "UNDOCTL"))) ; Place an end mark here
8 ]: g* F1 ?9 A2 T (command "_.undo" "_end")
# X- a& [# J4 F/ y/ @5 ` )
8 W- n/ k5 B2 O
1 F7 @1 ]7 ]/ d) {4 O# H (while (not (equal 8 (logand 8 (getvar "UNDOCTL"))))
+ B' g/ ^+ n& Z (command "_.undo" "_group")
1 M$ v$ }& q) }2 C )
3 P: R# E& B6 W) J, |9 e- \8 n( }! Y: C% a
(setvar "CMDECHO" cmdecho)
6 B+ V; a# `) a/ l$ ?* o8 C/ J" r undo_ctl
5 z. d; E8 l. z0 P' x, S2 P0 c1 f); defun undo_init( h: M2 O9 z$ w3 S
' H$ L! o4 e! ^0 P
;----------------------------------------------------------------------------8 L5 H/ k, b% G% b, t# b2 }9 N
; Restore UNDO status; u9 Y8 e& E/ X, m* ]" J5 t$ O( _
;----------------------------------------------------------------------------
! y {8 b* {5 ](defun undo_restore (/ cmdecho)0 i% e- ?# [$ x, C0 J
(if old_undoctl
$ a/ O, T" B: k* |9 j# f8 |$ i (progn
. e' Y" j, q( g6 r6 [ (setq cmdecho (getvar "CMDECHO"))( g0 d/ r8 `1 H5 Y/ W. k, v3 ~
(setvar "CMDECHO" 0)! N% ]2 Q* Z4 {7 L/ R* I4 x
4 c# Y! t6 {3 [+ A (if (equal 0 (getvar "UNDOCTL")) (command "_.undo" "_all"))
& L, \" ~0 o+ |' n5 l7 r& m8 z) F# m (while (equal 8 (logand 8 (getvar "UNDOCTL")))
* n- k# f7 v) \* ^ (command "_.undo" "_end")4 q! h7 z2 x1 g0 T
); while3 I+ f0 Z( m% X7 d7 I4 l
( M0 v o" }& {, @+ E4 _ ]
+ R- _/ p% @" O) i n7 Q7 d
(if (not (equal old_undoctl (getvar "UNDOCTL"))); I) v$ k/ z, F# e
(progn
4 M7 R& F9 y& E0 [0 b4 ~! X+ R (cond$ l$ f5 ^: H( E" m& J U0 t
((equal 0 old_undoctl)0 |, U- k1 r( y* i* d ]/ u) B( c
(command "_.undo" "_control" "_none")% l m) G, l; {8 T$ b9 e1 v6 ~
)
& d1 R2 P9 _+ ]4 Z. E, L0 c( e2 U" n# N ((equal 2 (logand 2 old_undoctl))
" O7 x( U# [6 ~1 L# ]- a7 J" }7 ? (command "_.undo" "_control" "_one")
% l; b& Q" F* U+ F )
& |7 @) b) H5 a$ L p& q )
. K& v9 U1 q: |% | (if (equal 4 (logand 4 old_undoctl))
* w7 u! u; ]" B( ~ (command "_.undo" "_auto" "_on"); a, |3 l2 p: T! G7 M. _" \' H$ }" N% E0 r
(command "_.undo" "_auto" "_off"). X6 q1 |& _+ G# q
)
- M- ^4 N- y/ T5 r )
. _3 ~1 ^ z1 T. f )
2 a2 C' d$ x7 J' F0 \" l! V3 K (setq old_undoctl nil)
/ J0 ]& l. ~( K (setvar "CMDECHO" cmdecho)
* D' [6 B- W; K9 _# g% U. O )6 `& @# c+ l8 K
)
' h! r) k% Z E- ?4 g' c/ D); defun undo_restore t: @5 V2 W) \ T3 @6 W# g
4 |/ i7 e: M! w& M0 Z1 F;----------------------------------------------------------------------------4 X! O7 p) d. r; [( l
; Save variables
0 a% F- }8 p& E. ~;----------------------------------------------------------------------------
u" g- q( q" e p4 P5 W! C8 ^(defun var_save (a)5 p' m. G) b w* S
(setq m_lst '())
0 q2 [3 k( S4 `1 }8 L5 ?, b (repeat (length a)
2 ]% M9 `, O0 Z( c; d! } (setq m_lst (append m_lst (list (list (car a) (getvar (car a))))))
# @$ b) V! _6 t: O+ ? (setq a (cdr a))
% H' w" | W7 S1 `. U: @2 b )
7 j+ I7 Z9 G" a2 R, N); defun var_save
8 ^1 V; o/ j; N
5 d8 C; x+ o" T( Y& j3 O! U! r;----------------------------------------------------------------------------
3 A4 A% C5 {3 ~' w' h; Set variables# |& N" ^6 ~; _5 P+ _8 g
;----------------------------------------------------------------------------
" V6 e% j. X8 e+ `2 U% n$ G(defun var_set (m_lst)+ A# R7 J/ t6 h" U: l" d1 e
(repeat (length m_lst)! U A" r5 H' {' L8 G" A
(setvar (caar m_lst) (cadar m_lst))
' {9 }( N$ m# |7 g, Z (setq m_lst (cdr m_lst))
" E0 S' L6 ]* C/ H$ A8 f )
6 @- S4 S; c5 C \); defun var_set) g' m5 L3 M) c
9 \ Z% y- u) t% d, E0 y7 m;----------------------------------------------------------------------------
) R' C. g4 ^7 l5 }2 @8 S9 y; Restore variables+ v; t( q" E* k
;----------------------------------------------------------------------------9 `" y/ X6 H5 }" ^# [ _
(defun var_restore ()% f7 g8 U5 Q Q L* q; i( r
(repeat (length m_lst)- _5 R# i& G% I! c# [
(setvar (caar m_lst) (cadar m_lst))4 K5 w% }5 n1 ~( L G, n, r& F
(setq m_lst (cdr m_lst))/ n0 r7 T7 P+ O" O; g
)
# D- z9 e/ `: _" c4 A0 H# [); defun var_restore5 F: @, ?0 n. I+ _6 c
) u4 l( O* b% U6 Z y
;----------------------------------------------------------------------------
8 s' j5 z/ O4 q0 x/ W; Initialize routine ) k/ q. n# \3 A% v9 j
;----------------------------------------------------------------------------
Q+ E3 z+ T. d5 e. @" v(defun err_init(e_lst u_enable add_fun)
; ^# _7 k( }0 B; C* V (if err_alive (err_restore)) ; To avoid nested call5 T% m( ^: u" o6 e) k
(setq err_alive T)
# d& n8 X- f2 S# H' t (var_save e_lst) ; Save the modes* z, |& z1 ]; o4 T
(if u_enable (setq old_undoctl (undo_init))) ; Initialize UNDO status
" e" K/ q5 q0 c7 G; k7 Y/ F! s (setq err_old *error* *error* err_main) ; Save the handle of *error*
. M! q, d# k/ M$ M$ b (if add_fun ; Add the user cleaner- |9 Q$ w. _" l
(setq *error* (append (reverse (cdr (reverse *error*)))
. ?* L- o. g# R (list add_fun (last *error*))2 u% k, o% a' R7 g- O" a1 s
); append
- Z3 S! G3 n+ L )
% ?: O$ S& f1 v& Q( x( @+ N2 W )0 H4 S/ _0 E- T$ s" N; U
); defun err_init |
|