|
|

楼主 |
发表于 2007-3-13 08:33
|
显示全部楼层
undo.lsp
;;; UNDO.LSP made by piggy X1 `7 ^. l' {' }4 h2 t. U1 s
;;;8 o" W& a0 ]1 f9 `
;;; DESCRIPTION( Y& L7 A; T: C9 H( O5 k3 `1 ~
;;; These are general error routines which can be called by 4 q d% k7 q P% o
;;; other routine. See AutoCAD 14 ac_bonus.lsp for reference
8 l2 B. a: P0 D: q% n;;;/ c7 b/ M# u/ m5 V6 y' B4 I' ]% e
;;; SUBROUTINE INCLUDED IN THIS FILE- z9 a' k/ ?8 B. S, ?8 @9 x
;;; UNDO_INIT- k. D8 ], S. ?8 ^% N
;;; UNDO_RESTORE, d' E2 U0 J0 M6 A
;;; VAR_SAVE
( N; B/ x7 i+ r: {;;; VAR_SET 6 k8 T2 K# e; g. E$ ^% k) C/ \
;;; VAR_RESTORE h/ i) R. `/ [% i/ @
;;; ERR_INIT 7 V5 S+ l L. q, y9 d7 Z4 v
;;; ERR_MAIN
\: q# l2 M S;;; ERR_RESTORE
% X7 e N0 c3 A;;;
& m, ]0 O3 l+ S4 i! b6 `' Y;;; DATE: 10/17/98; 03/31/99
: G! |; `: w4 Z5 G' v;;;1 W% W9 H* J1 o
;;; HISTORY:
8 N5 N K1 I1 u2 S0 Z3 R9 M Q# S;;; Add routine of mod_att
1 I2 K5 z+ @& h8 Q( P9 U;;;+ p/ N7 P# H i- a9 T9 k* U6 f9 J2 o
;;; USING METHOD
; C, g; @: C% _# h# ];;; ERR_INIT:
+ V2 ~% ?8 k% |2 q: k3 };;; This routine initialzes the error handler. It should be called as:
' U, M$ P- t6 Y0 m: W: |5 L- e;;;
Q& K7 [( p' S+ q;;; (if (and (not undo_init)
2 Q* H. X3 s& e9 S;;; (equal -1 (load "undo.lsp" -1)) % {2 L: n( i/ S j: z3 G8 {) ]
;;; );and6 J! w8 h# c8 W# p- f. V7 G
;;; (progn (alert "Error:\n Cannot find UNDO.LSP.")(exit))
( K2 K4 v6 D/ F0 i( l" J& x2 c T;;; ); if
: v- a# o) d# Y9 o! h* x/ S;;;
2 K( F4 A& E4 U+ z/ v# ?& F;;; ARGUMENTS:6 p& A$ x3 U+ h" X" U
;;; err_init Takes 3 arguments.
6 s0 @/ T2 _, l;;; 1. - The first element of the argument:
5 G6 Q7 |) p" [" k( J;;; This is a list of system variables paired with
- j1 o8 N. _* X, g, S! {1 Z; O;;; the values you want to set them to. i.e. '("CMDECHO" "ATTMODE")+ U; G- J5 x/ b* l
;;; 2. - The second element is a flag) O" u& N. X. e6 V% q ?" Q
;;; If it is true, then in the event of an error
- _4 Q- d% ^- g;;; the custom *error* routine will utilize UNDO
1 w/ L" q/ |/ {. V; e;;; as a cleanup mechanism.
6 m4 X' Q5 h! w2 ^3 U7 c;;; 3. - The third element is a quoted function call.
4 M/ \/ E0 W, M;;; You pass a quoted call to the function you
- n8 N% t/ q. ]$ l;;; wish to execute at the end of nomal routine if an error occurs. : b9 I+ o; F4 V, o9 @* [4 I
;;; i.e. '(my_special_stuff arg1 arg2...)
n% x/ y# [" Y0 A" O/ a;;; Use this arg if you want to do some specialized clean up $ K/ z, B. F) _
;;; things that are not already done by the standard bonus_error
- x) ?/ ~* ?! o3 P' k;;; function.1 _7 c7 c1 P/ i/ p
;;;2 U w) S' F5 ]# K
;;; ERR_MAIN: Body of error routine6 O6 ]# H E+ {4 i7 c' a+ t
;;;
6 A+ _6 p; J1 Y! N0 B3 N;;; ERR_RESTORE: This routine should be called at the end of command to
$ {7 s) D' @8 e- P6 W& O;;; restore the VARIABLES, UNDO & *error*.& F# }5 A+ I+ U: A
;;;1 r" Y, x0 I" M5 c* h; m0 ?7 b5 [% G
;;; UNDO_INIT: Initialize the UNDO status, L; m& E# S7 I! t
;;;
: F0 C, W# |( ]$ _6 u6 M;;; UNDO_RESTORE: Restore the UNDO status
) j' D$ E/ m* {+ C1 h;;;! O5 e, \! H h- ~7 ?; ], e
;;; VAR_SAVE: Save the variables. the argument is like '("CMDECHO" "ATTMODE")
# _" Y4 V6 [- N. ~( E+ X" [;;;- {1 m' ^! ]2 k0 V4 e
;;; UNDO_set: Set the variables. the argument is like
! V, g7 M; E! q) Q; {' p8 ?;;; '(("CMDECHO" 0) ( "ATTMODE" 0))' z# }# m [+ i7 `
;;;6 H+ U3 F4 G: w$ h
;;; UNDO_RESTORE: Restore the variables/ {# n$ e& Z: v9 `' W9 n) J
;;;
! v+ t3 |" B3 [6 y9 V;----------------------------------------------------------------------------% V1 R" H b/ V% @& ^. U/ J b6 ^
; GLOBALS:) ]7 `' @9 |3 n3 @% f
; old_undoctl -- old status of UNDO (voided by UNDO_RESTORE)1 _0 x$ q, Q7 G: {7 ?
; m_lst -- list of variables (voided by VAR_RESTORE)
' s. F/ w! N" h# }% m% @5 r4 n1 p6 _; err_alive -- indicate error routine is active (voided by ERR_MAIN or5 O; f: l& k# ? h) F! W
; ERR_OLD)
# m3 b) R9 r0 \* y4 _2 _& m5 n; err_old -- old handler of *error* (voided by ERR_MAIN or ERR_OLD)
& Z# M3 C+ J y2 ?& n. y& O;----------------------------------------------------------------------------
, q. u8 {5 C$ Q$ C- }; E. F( s3 Y& N3 c4 z% i
;----------------------------------------------------------------------------
) D' X6 F' v( X& e; Modify attributes according to entity name, attribute name, dxf_item
; C( d+ p+ C# c; r+ U;----------------------------------------------------------------------------
& `+ q4 B1 Z& { M- E(defun mod_att(ent id dxf_item) ; f' y) B1 I* x4 M5 P% t
(while (and (/= "ATTRIB" (car (entgetf '(0) ent))) (/= id (car (entgetf '(2) ent))))
+ j$ E) J( G# z: u0 ` (setq ent (entnext ent))
2 F: X: B# @) g0 m; `% m* l9 @3 P ); while
4 ]/ K$ v* C! r4 y& s0 ` ((lambda (x): Q( P; _- S# r
(mapcar '(lambda (y)9 _+ g+ l( V4 M7 Q0 u4 ^
(setq x (subst y (assoc (car y) x) x))6 A e+ u6 b& \+ ~: F" m* Q7 |
); lambda
) x J' C' q( Y* @) B8 Q dxf_item
3 a# L, ?. u% [: y5 }1 ?0 S ); mapcar
! _: q/ H& J0 K' n& H( E( ~8 d (entmod x)
) C& t: f( z: w, I6 S (entupd ent)
4 X" N6 n4 S# G# i$ I: U ); lambda
% A/ ]& |- v. V. n: M$ _( ]/ o (entget ent)
; i' `% M6 t$ Q- J ) M- {; k6 P3 s# a+ v' M7 X; Q4 L' s
); defun mod_att+ z7 x, w8 N( w5 H& ~' W& z
1 b$ @! R# a5 U( o- ?2 L% ]( o, T;----------------------------------------------------------------------------
2 e/ P% h$ t6 O: O1 P; Check layer status, return a association list which contains layer information
# f) `" x( U2 g& F;----------------------------------------------------------------------------
- }6 B! K) a2 \% L- ^(defun laychk(lay / l_sta)
- H7 `6 C: B( K2 L! N ( (lambda (x)1 e1 P4 ]" k# F6 S4 g/ y
(if (not l_s)( j# u5 j3 d$ C/ Y3 B e
(setq l_s (list (cons x (logand 5 (cdr (assoc 70 (tblsearch "LAYER" x)))))))6 _. I; q; f2 i6 u1 }; V; Y6 `0 {6 ? w
); if8 S* F& z& L4 S
); lambda
! Z0 D1 E8 s' K (getvar "CLAYER")6 }0 V- P6 \+ x: k; `' y2 {
), z% a2 v, T) s2 g6 T
(if (not (tblsearch "LAYER" lay))7 ~# z' w m: m7 P: M
(progn
6 Y- y1 U1 \' z# J2 V1 a (initget "Yes No")
0 R v0 m& N; x$ n3 o- R5 Q (if (= (setq l_sta (getkword "\n图层不存在,是否建立该图层 ?\(Y/N\)")) "Yes")
% o4 H% W& V. u9 H7 g (progn0 I4 p/ i$ n5 f) s. y
(command "_.layer" "n" lay "")5 \; f" V" o f: ~, z. d- D2 M
(setq l_sta 0)
! I: O/ ?6 j$ I! r0 }* {7 `5 D7 f ); progn
: ~! A1 J0 \: M+ E+ Y2 k ); if+ Y& Q3 B8 K2 t, g- k4 \
); progn
% d% O) d* f: n (progn
) E3 A$ j4 b3 G# u2 ~3 U. B (setq l_sta (logand 5 (cdr (assoc 70 (tblsearch "LAYER" lay))))), U$ `( ]; E; P3 u* I% a, a; m
(if (= 1 (logand 1 l_sta))8 w3 S) r, T. ?7 q1 }; p
(progn% E$ q7 I4 x/ W7 T! T+ Z7 P
(initget "Yes No")# ^3 q) T! G0 ^+ ~2 \8 g. F
(if (= (getkword "\n该图层被冻结,是否解冻 ?\(Y/N\)") "Yes")
1 J" y! G) N+ E0 _" x+ [0 ?/ K (command "_.layer" "t" lay ""). h4 @0 A$ ?0 ?2 I! a: C9 y
(setq l_sta "No")) i p q; o- D9 R) b" u5 b
); if2 y I5 D$ }( V
); progn2 K' w* K2 K, f5 ~
); if
5 M2 @! d! J( a (if (numberp l_sta) + [' C, A( z& T; Y5 {
(if (= 4 (logand 4 l_sta)) (command "_.layer" "u" lay "")); if, N- z$ a F/ L: p7 x" v
); if
8 _9 E/ h. ^9 h ); progn+ B2 O4 o9 i$ L" }
); if5 V7 L J# ^/ l+ s: |/ E- f* J
( (lambda (x)+ R/ {$ r& p8 p. }* w* m! \
(cond
( r I: p: D% g; `- o ( (not x)
' {3 w6 M* B: g* u" G* G (setq l_s (cons (cons lay l_sta) l_s))4 x% W0 [7 F7 o2 i I0 F0 F1 |
)
5 \$ Y; v8 V; M ( (= "No" (cdr x))' m; K8 T0 Y; ]
(setq l_s (subst (cons lay l_sta) x l_s))
5 x0 a" n* ]+ P7 v9 [1 h2 H6 q% g )4 A/ Y! X# B! S: Y. |7 b" G
( T l_s)
; S% v) b9 a! ~( W- s ); cond
r, T/ t0 D( O5 x ); lambda9 L0 m5 _0 J" M2 m
(assoc lay l_s)# u5 p, H/ K0 t" u6 _
)
& k! j' n& p! f- n( `. M, V' N9 I6 A l2 g- ?1 ~4 w
); defun chklay
9 P4 y9 B5 q' d* s. U8 |+ w/ s9 @# i
;----------------------------------------------------------------------------
4 p$ O. u% [ [; Restore layer status according to association list l_s
) U/ K, t0 d% a" z% S: k' I4 h$ B1 x;----------------------------------------------------------------------------/ S7 R# E( F' m
(defun layres()$ C( G) j" C; ]
(setvar "CLAYER" (car (last l_s)))- J4 `" I* j* a3 h" t4 }6 c
(repeat (length l_s)2 I5 R- T3 B5 I
( (lambda(x)
2 |2 {3 H* h: L: h! Z8 J) G: U) r (if (numberp (cdr x))
1 a; n- [) h: Z7 | k! ~0 k (progn
* e7 L3 l6 l, ]0 f2 p, c# U9 }1 e (if (= 4 (logand 4 (cdr x))): W/ S" Y; c h$ B) q/ S8 H2 B
(command "_.layer" "lo" (car x) "")
* @* b1 {. i/ c8 @# x# A ); if! L) K5 q8 f: I. i- z7 x
(if (= 1 (logand 1 (cdr x)))
9 c2 {9 F/ r" _ y2 C4 k (command "_.layer" "f" (car x) "")% L/ c2 P. ~) ]' g! |* A
); if1 g b) t( H2 x) `
); progn5 Z. T( J4 T- R. G( x$ R: W
); if ) O( X/ T8 o) A- u a
); lambda
6 v0 u$ u9 L5 \+ N; F1 u: m (car l_s)( K- C. Y& P) k: ^$ ^
), X2 G6 h/ g6 X W
(setq l_s (cdr l_s))
* B; ]2 Z/ v' A% h& H }' g! c ); repeat5 @) {( x% b- [5 C
); layres( h$ D. Z! V1 B! U. G( M& }% {
" t0 K" o, K- N7 s) y;----------------------------------------------------------------------------$ w- _; d8 V* n
; Get DXF codes
5 x+ y; m! j; \+ @, R/ V! f7 p;----------------------------------------------------------------------------1 @8 @) m1 j+ E# V
(defun entgetf (index ent)
1 f( i( ^, T( n8 c2 J! ^& G ((lambda (e)+ u7 c9 D1 m6 a* \ ~
(mapcar '(lambda (x)( \8 ^) c6 n: I9 |% f: p
(cdr (assoc x e))
5 ?' u2 D, [5 t( X6 z ); lambda, X3 F# ^6 e) M
index) ; internal lambda function
) _# I9 g E7 }3 t- s ); lambda @0 b+ ^2 S. C9 X7 P
(entget ent) . g [2 q+ l7 J& T. V" L1 w! r
)& w1 t; i# {; R" w5 R# {
); defun entgetf
4 M. s( I# R0 a9 I! a' V8 R: I4 J0 v% U$ k% g' J
;----------------------------------------------------------------------------
4 F( D% f. N# i3 X& i; Save UNDO status. ^7 g+ W8 l2 H7 p
;----------------------------------------------------------------------------
4 c; U. P2 }! @# q; q- A% z/ S' t- I(defun undo_init (/ cmdecho undo_ctl)
6 `; V& A! Y# V( ]( s (setq cmdecho (getvar "CMDECHO") undo_ctl (getvar "UNDOCTL")) ; Save the value
k/ Q: b8 I" E% ], G9 U (setvar "CMDECHO" 0)
8 R2 G2 H+ E" R% A5 t
* X! e. p5 Q4 J# c; ]6 ^: z (if (equal 0 undo_ctl) ; Make sure undo is fully enable7 M. c7 v& C& Q9 K8 b
(command "_.undo" "_all")
# m; I7 }+ z) W (command "_.undo" "_control" "_all")
6 i' q# t; a3 ~4 m2 P )1 q( A2 S6 h% q1 G. s- t
+ l7 V2 e0 s+ j4 X& Q3 [6 n O$ ] (if (equal 4 (logand 4 (getvar "UNDOCTL"))) ; Ensure undo auto is off* x8 b9 {2 C+ e) N: P; h1 o7 z
(command "_.undo" "_auto" "_off") L5 G# j. v Z: G1 L
)
1 I- o( T/ T% V) M9 U4 u& Y
# c% R: x1 G8 H2 Y7 D' c (while (equal 8 (logand 8 (getvar "UNDOCTL"))) ; Place an end mark here
7 B( n" `. h; U6 n2 [( u (command "_.undo" "_end")& R! V5 \9 m4 c) [
). P1 a% ?' |, j
/ [* _9 u; N" x! G9 D. D3 N# @
(while (not (equal 8 (logand 8 (getvar "UNDOCTL"))))
8 E/ c; d, H Q3 V' ]& @6 X (command "_.undo" "_group")- m" K) q. w% d& z2 k* _
)1 `1 `# y0 ]! Y
6 R9 C; p3 k) P (setvar "CMDECHO" cmdecho)) ~2 J4 Q+ V/ D# a6 @1 J9 K
undo_ctl9 J. H4 Z' F' D3 T$ p: N9 F8 p
); defun undo_init+ \8 m, f/ T- t3 r# V$ u9 d
( e1 F1 C: g8 a2 J' Z* V
;----------------------------------------------------------------------------! `5 @+ h/ k( v. M3 c6 s/ h
; Restore UNDO status! @ r9 P0 h; H; H# Z
;----------------------------------------------------------------------------6 H1 V: Q) y& B
(defun undo_restore (/ cmdecho)+ A5 [ F' ~( q; T
(if old_undoctl
3 u/ {7 ~5 @, d1 t (progn
# N6 a, P" Q |. z. L2 M u (setq cmdecho (getvar "CMDECHO"))
$ D1 c. d0 i; ]+ D) k& ^3 T (setvar "CMDECHO" 0)) H H: S4 I1 M* ?" t+ [. D* s
0 d: y$ O6 l7 c: ~$ T1 W
(if (equal 0 (getvar "UNDOCTL")) (command "_.undo" "_all"))
+ Q* U2 k9 ~7 f, a, q+ i (while (equal 8 (logand 8 (getvar "UNDOCTL")))4 t" Y: ]" l' T! A E
(command "_.undo" "_end")& |! C" N1 Z1 M+ x7 w
); while* |+ L- |, z& F, ~. ?
$ b$ |9 j4 p* M# z$ m% X
6 Q6 B; p/ ^2 d8 u/ f6 ~% y ]! E
(if (not (equal old_undoctl (getvar "UNDOCTL")))
5 k: s+ {) h. f# |+ V7 a (progn1 x5 K* ^6 |( M% w3 g- J
(cond* ^6 o* g0 @% I8 a( K/ L0 b
((equal 0 old_undoctl)) s: }' h: g' _$ h
(command "_.undo" "_control" "_none")3 a& [6 k. F1 I* H1 q
)
' j+ V" H$ e; U( e% W ((equal 2 (logand 2 old_undoctl))
" `! o/ q% c& v, ? (command "_.undo" "_control" "_one")
9 E/ e" {6 j% n% B8 u )
2 N6 i' ~2 E* @" ^3 Y )$ W2 F8 N) g+ v
(if (equal 4 (logand 4 old_undoctl))
' ?$ C& g1 u+ C, ~' I% S (command "_.undo" "_auto" "_on")
+ ]5 G' L3 c$ h (command "_.undo" "_auto" "_off")5 w" T: x% @- w( {8 q) z, \
). B! I! B! f: H; Z( O, Q
)
( |3 L" l. P) C6 x7 u( r ): c6 s/ H1 ~3 \6 o: c) g
(setq old_undoctl nil)
. _9 P7 |/ S) \9 M9 ^7 Z (setvar "CMDECHO" cmdecho)
8 u' D2 |; u/ t+ s! r/ ]: R7 q )
# u, r% e+ x" P9 d6 s; M: s' G7 Z* ? )8 k2 {3 h) y& B( b2 d
); defun undo_restore6 _9 A( P6 S" ~" d4 B+ E1 W* Y
$ p' v" \3 d+ E) J( [1 R;----------------------------------------------------------------------------
9 t" P: P/ s# X; Save variables6 z; |) t0 T2 u% y& a- U/ f
;----------------------------------------------------------------------------5 l/ g' E% e) E1 i) W m* `9 J
(defun var_save (a)5 S; a3 b4 w7 r, s
(setq m_lst '())
7 \; N P/ J) `5 f; w2 Y (repeat (length a)) K1 W% }/ p0 ^, V3 y* T
(setq m_lst (append m_lst (list (list (car a) (getvar (car a))))))
8 K$ w1 y( W6 }" g8 q! t- U# \ (setq a (cdr a)); n0 y& s" b0 W+ d5 w0 i4 z& _# U4 T
), u8 w" d' n' [% o; l9 f, c
); defun var_save
; g9 V6 p) v- j$ D3 P B% D5 D. m# @* k5 v$ B6 [. f: a; \4 w
;----------------------------------------------------------------------------
3 c% K' L% U( ~3 w% q% t; Set variables: o. C1 @+ W f, G
;----------------------------------------------------------------------------
% f' [0 z) L6 q, W6 g; K(defun var_set (m_lst)
% x4 _# r( c9 K( g& `# @ (repeat (length m_lst)+ z1 [; F9 D& p' O
(setvar (caar m_lst) (cadar m_lst))
+ }, y! {! j, e/ \ c" _# u (setq m_lst (cdr m_lst))8 ^1 X- J" L- ]
). @" L1 Y2 P0 y7 Z6 G5 B3 Y" z
); defun var_set: R3 ~( d, P! r) B
/ L) N7 g# _! o8 K1 D5 T P;----------------------------------------------------------------------------8 P8 L6 i' u) N6 }3 X v3 R6 m
; Restore variables2 V5 C) Q0 e( N& J* [6 b
;----------------------------------------------------------------------------3 J( z' B' @' V+ H Q% S& g
(defun var_restore (): g; v" h( f' y
(repeat (length m_lst)6 q/ Q4 w! K1 c9 z; S' e8 k" B5 M
(setvar (caar m_lst) (cadar m_lst))
6 e" o( P! i; _( E, \) t2 H (setq m_lst (cdr m_lst))
+ y" J7 F8 N9 K" C5 y )
8 d5 D1 m; U/ b, x8 D$ M- B); defun var_restore4 b$ o0 j' B2 u( e# d7 D$ j- A$ e
; X# ^' a5 J% \3 s& B8 P8 P0 Z8 t;----------------------------------------------------------------------------
6 W4 U, m. f4 S/ h" q; Initialize routine 0 p' S& x+ [- ^: k) E
;----------------------------------------------------------------------------7 z' n5 p! \/ f! `! X
(defun err_init(e_lst u_enable add_fun)8 Q4 m6 {- z$ e8 g: n
(if err_alive (err_restore)) ; To avoid nested call
$ M g, T9 Q4 c- A; K) G (setq err_alive T) r( V2 K) T8 _7 G
(var_save e_lst) ; Save the modes2 M. n- R9 e; k. `6 `
(if u_enable (setq old_undoctl (undo_init))) ; Initialize UNDO status" I8 [) d) C1 ^" X
(setq err_old *error* *error* err_main) ; Save the handle of *error*
% z( ^6 C; f& t6 \. `) I, } (if add_fun ; Add the user cleaner1 J7 D; L/ E S& F( p
(setq *error* (append (reverse (cdr (reverse *error*)))
/ r! {: O! S+ j+ k' h (list add_fun (last *error*))8 ] F4 p+ Y8 `+ C# D. Y
); append; Z1 { k0 @5 E! j, L/ h3 k
)( S6 r/ I' @3 Z1 _
)
2 P/ e" ]+ P) J- _); defun err_init |
|