|
|

楼主 |
发表于 2007-3-13 08:33
|
显示全部楼层
undo.lsp
;;; UNDO.LSP made by piggy
6 [* ~' X! k" p0 O. _- _;;;
]" e4 Z3 b& h' n) K: o& U;;; DESCRIPTION! O8 O4 n) [9 P% A. [; L/ s
;;; These are general error routines which can be called by 5 T6 k( v9 c% {. x+ B3 R. Z1 v- `
;;; other routine. See AutoCAD 14 ac_bonus.lsp for reference
" } y: ?5 j9 a0 ]; };;;
- C2 ~/ x# t$ G7 { d6 G0 D;;; SUBROUTINE INCLUDED IN THIS FILE0 s" w0 x9 y I6 ]$ `0 J" z
;;; UNDO_INIT
6 v# R" x- ?) E' A2 A; }3 |;;; UNDO_RESTORE
6 s9 x( z' c$ A* I. ^* ~# q;;; VAR_SAVE 3 v3 [& h4 V. ]* l% r
;;; VAR_SET
|: e+ t3 P! ]/ `;;; VAR_RESTORE# E) A5 ]- S, J. [$ s
;;; ERR_INIT
) C7 R$ \, r L: u. M2 K;;; ERR_MAIN
3 D+ K3 v( _2 j2 a1 J4 m7 G6 t+ ^" {;;; ERR_RESTORE
7 v8 L. _* [* |6 X) C7 w+ ~' r; ~;;;
# m% E/ c( d; x;;; DATE: 10/17/98; 03/31/99
" P& l7 Z2 t( b4 a6 t1 [;;;/ I, L6 L, a2 U$ w; `+ W
;;; HISTORY: 3 o! Q! [0 _4 I* s4 _
;;; Add routine of mod_att# d5 y* ?: q8 }6 u, U( a
;;;
7 f8 {7 ]$ N; F/ Z# s2 E;;; USING METHOD3 O* @- W" n% l0 V( i
;;; ERR_INIT:
; g+ \+ k% j0 N2 a# u! m8 g;;; This routine initialzes the error handler. It should be called as:
! N5 a+ G7 o7 Q3 v3 u;;;* \, V' d" u4 V7 Z8 h& {) P# y
;;; (if (and (not undo_init)
/ N: c- @" p7 c/ T$ l" k;;; (equal -1 (load "undo.lsp" -1)) ; R( ~) c8 X0 Y! f
;;; );and6 k/ r. D3 ^' k, C; T) l% o
;;; (progn (alert "Error:\n Cannot find UNDO.LSP.")(exit))
r& N0 i- g( v;;; ); if* S2 R3 ]: @5 m5 `! ^
;;;" Z) p2 `" T' Y8 ]$ i" q
;;; ARGUMENTS:0 f2 \# `1 \# k3 \
;;; err_init Takes 3 arguments.
2 y' V7 e$ \6 R$ d. m. T;;; 1. - The first element of the argument:' c) x0 E. P# F- \, B# v
;;; This is a list of system variables paired with
2 ?# z% m# o7 F) J' M- v, h;;; the values you want to set them to. i.e. '("CMDECHO" "ATTMODE")
- v3 S- W) r/ [# v0 w;;; 2. - The second element is a flag
* k8 W5 r. q7 J: ^1 w. m+ M! T2 Y;;; If it is true, then in the event of an error * M3 i$ }/ V9 u. D$ j7 `
;;; the custom *error* routine will utilize UNDO " y3 l o9 \. b" {/ s
;;; as a cleanup mechanism.% w6 t) u1 u8 F' }
;;; 3. - The third element is a quoted function call.
' y/ V1 D$ \- g7 a9 z3 _;;; You pass a quoted call to the function you) K& I9 L( H o1 Y9 \! U
;;; wish to execute at the end of nomal routine if an error occurs.
5 [% s. h/ D, f$ I7 s;;; i.e. '(my_special_stuff arg1 arg2...). ^3 G8 I E: }$ u9 j8 I) D0 d' V
;;; Use this arg if you want to do some specialized clean up ) I1 d/ R8 k( }% S9 i
;;; things that are not already done by the standard bonus_error $ ?: O" ?# ~$ P3 E7 H- S/ s! C5 t( ~# @
;;; function.4 R. k% F) H+ [. h! o
;;;* t3 B7 G: u3 Y) `6 i% i
;;; ERR_MAIN: Body of error routine8 L% T5 m" \0 h, y- n. Y# W
;;;5 @ l" l9 P: e2 u" K. m; X- S
;;; ERR_RESTORE: This routine should be called at the end of command to
3 u2 O) F2 q3 ]8 ];;; restore the VARIABLES, UNDO & *error*.! I+ y6 r" w7 n4 H- g
;;;6 W7 g; `- f) ~$ i% K
;;; UNDO_INIT: Initialize the UNDO status
6 |7 h6 x5 e, a2 ~6 Z1 P& J( ~;;;3 r! C! I6 ~, G/ p0 \
;;; UNDO_RESTORE: Restore the UNDO status9 o) L1 d( n0 A/ C) S: h; L# R9 S
;;;! \; `6 q* q5 W$ K! P, I
;;; VAR_SAVE: Save the variables. the argument is like '("CMDECHO" "ATTMODE") I0 Q* Z' Q: i0 J/ e8 Q" ]4 M' T
;;;% g4 C5 F/ K2 W
;;; UNDO_set: Set the variables. the argument is like
6 }3 {# B5 @0 m& i; V1 n2 s;;; '(("CMDECHO" 0) ( "ATTMODE" 0))
* Q+ A T8 w% c* e# `' ?2 N& N;;;9 d8 V3 v: p( ~
;;; UNDO_RESTORE: Restore the variables
+ ]% `5 l" B" N8 j;;;# ]& y' w5 T9 f! g9 J, _
;----------------------------------------------------------------------------8 N4 A; Y# v, Q- ] s9 ]' a
; GLOBALS:7 Z4 [/ {: m; _
; old_undoctl -- old status of UNDO (voided by UNDO_RESTORE)# o+ ]5 k, S9 b! P2 h8 M k
; m_lst -- list of variables (voided by VAR_RESTORE)
$ I e6 x1 {1 F& c! D! _3 J. u; err_alive -- indicate error routine is active (voided by ERR_MAIN or6 l/ M" q0 G# x. L
; ERR_OLD)6 e( S# u6 X" N9 N# R: f
; err_old -- old handler of *error* (voided by ERR_MAIN or ERR_OLD)
. b' w& u% V) j;----------------------------------------------------------------------------
! P2 A% ]2 Z" N) b" h( U
2 K8 t. ] g2 \- C, g;----------------------------------------------------------------------------
+ B# h8 [- V; D/ ~. u+ I1 N8 R; Modify attributes according to entity name, attribute name, dxf_item
# I) H; b" i# @* D;----------------------------------------------------------------------------
5 }" o; V& G i5 b# o; a(defun mod_att(ent id dxf_item)
P" z! _( t5 R+ w9 t+ q; v$ s) G0 g (while (and (/= "ATTRIB" (car (entgetf '(0) ent))) (/= id (car (entgetf '(2) ent))))2 q9 O! p# z, J/ ~8 i
(setq ent (entnext ent)); C! `3 P; J! }5 T
); while
; B$ o- W- S9 _! i I6 D T ((lambda (x)
& h; F6 ]5 p2 s+ s$ S, x5 S: B (mapcar '(lambda (y) I& P" k4 U, E' p0 x# m
(setq x (subst y (assoc (car y) x) x))( |! T5 W+ Q; D
); lambda. R0 i/ B1 V0 a/ h3 m7 C& R, e8 x* C
dxf_item
# a! j' V) ^* L# P p. H+ a% m2 P ); mapcar+ e: e5 c8 ^4 U8 {1 B* M* A1 m0 K
(entmod x)
3 B/ u X' D( p% s8 K3 Y (entupd ent)/ [4 ?. q' P7 P n K& Z& ?
); lambda
: i) _' `; u) B7 X$ d (entget ent), g& l8 B& r# ?, P6 ~
)
h8 z/ G) {% j' H2 S6 v* k( T, }); defun mod_att" i$ f' Q" R: f
3 O( m/ G% R: e! y9 g9 ?# F0 J$ P
;----------------------------------------------------------------------------! A6 }$ n4 [, `7 c1 g
; Check layer status, return a association list which contains layer information
4 l3 E+ f7 h% {- Y3 o* a6 G( ^;----------------------------------------------------------------------------
' u5 j& g7 Q1 k3 a: x2 M* a4 o(defun laychk(lay / l_sta)
6 F- Y& `% h$ d; Z ( (lambda (x)5 \3 b. Q$ T8 e
(if (not l_s)# S. `1 u: A5 ^% F7 w: j3 y
(setq l_s (list (cons x (logand 5 (cdr (assoc 70 (tblsearch "LAYER" x)))))))* f- k- N8 q9 X! H2 C( T
); if/ B/ j) g: ]. o4 g. ?
); lambda ; g+ M& e% c: N+ S* E
(getvar "CLAYER")
* Y1 \6 P* X, T) M2 w )
* ~$ U* w9 |. S4 D" X0 p* L (if (not (tblsearch "LAYER" lay))+ A8 C7 ?( ~9 ?' j
(progn / k7 y9 [3 `# b, y+ N
(initget "Yes No")
4 q* F( N8 g8 j8 [. b. V* E3 v (if (= (setq l_sta (getkword "\n图层不存在,是否建立该图层 ?\(Y/N\)")) "Yes")
1 ^) s- l8 M9 P; l/ F (progn
2 y5 [4 n6 ~; I1 I$ }% s (command "_.layer" "n" lay "")
3 B2 G) C; v' L6 z" U/ A (setq l_sta 0)$ r. L( I3 c) ~; o i# M7 |/ i
); progn6 I' B; [& ]) K/ \
); if
+ g" F! a9 q# }" F9 y* T& ?9 K ); progn
* V Z# n; G s: E (progn
* G% n6 x0 F- n$ u$ u (setq l_sta (logand 5 (cdr (assoc 70 (tblsearch "LAYER" lay)))))
. Y' V3 m8 O5 G; C (if (= 1 (logand 1 l_sta))
' d' Q& {( _! q9 {* B1 Y5 g (progn
2 i3 f t2 ?5 D4 G" C2 |8 c (initget "Yes No")
3 N# Y* ^* M7 T! U7 \; ^) S/ h (if (= (getkword "\n该图层被冻结,是否解冻 ?\(Y/N\)") "Yes")
' Z" l- G" v+ I6 `4 Y* T% Y* n& i (command "_.layer" "t" lay "")6 w3 @7 r- E0 {$ x% }
(setq l_sta "No") m! L6 V/ B" M, J. e; E+ \
); if. J: K5 h# m5 }1 `/ q
); progn
; k. G% n' x0 e ); if3 e1 C) `$ N0 o- m6 {4 f- H/ x
(if (numberp l_sta) & }7 o' t: r# Z
(if (= 4 (logand 4 l_sta)) (command "_.layer" "u" lay "")); if
% c' z3 A; K' m% ? ); if
/ [/ A" a+ C- k' y' p5 Y; P- V ); progn# e. y9 r. v5 U$ u$ H
); if% `. X$ G% o3 W! y5 N. P
( (lambda (x)
. |+ P" \& B! ] (cond
' c. J5 H6 E$ h ( (not x)
: R- T* x f- D9 t5 m (setq l_s (cons (cons lay l_sta) l_s))% z! k! W [; P, x# m
)
! n3 c! D/ i, Y: M, F% [ ( (= "No" (cdr x))
v! v( {1 u* v( q6 G' n9 d! I ? (setq l_s (subst (cons lay l_sta) x l_s))
" a! t0 X/ {5 ~ |" l3 s )
) W; ?+ w! @# M$ j( F$ R8 l: N; _ ( T l_s)
% t. H% D! y9 D: U' Y$ l/ n0 Z ); cond9 W; a% d2 `, W. n5 }% q8 |' a$ z
); lambda
: f3 \2 D5 I0 h5 l% U; X- u S (assoc lay l_s)
( v0 f- r0 g5 r" C S, p+ s* f )
* h2 V! V9 V8 y8 ^/ `/ X
6 x. ]$ ]+ N# i6 b) S); defun chklay1 f) r2 u* g& c) g( o, G; Q
' b b1 S9 Z4 X7 h" \; R+ u% D
;----------------------------------------------------------------------------
9 B! _( k; a5 w! T7 p. b( ]# V2 R; Restore layer status according to association list l_s
2 G) |4 R2 U0 \+ M' t3 m;----------------------------------------------------------------------------
4 i* ]9 p% s6 ^( @/ i6 K$ w' J7 d(defun layres()
8 o1 T Q; B: l+ F) `2 r (setvar "CLAYER" (car (last l_s)))
" _; [4 Y' a+ l+ Y (repeat (length l_s)1 p, j% Z8 o2 K1 f5 O* x/ p. t
( (lambda(x) 5 a3 ?/ l3 w2 s" a) F" ^7 d
(if (numberp (cdr x))6 \0 e9 I2 Z* N9 h0 ]# T
(progn
. k. H" Q# i8 q& P (if (= 4 (logand 4 (cdr x)))- z+ r7 X- A+ l1 z5 j
(command "_.layer" "lo" (car x) "")
$ e6 V8 J# k l: C1 t4 @3 R. H ); if
* G H, b& D. M( T (if (= 1 (logand 1 (cdr x)))
" L0 o2 I, I+ r (command "_.layer" "f" (car x) "")
! C5 J, d- x' D8 r/ ] ); if
3 y# d& s* n( L" S5 K! A0 Y- u ); progn0 M# e4 I- `" C G
); if
9 s3 G' \5 P- |3 \ ); lambda8 @* @/ w# D, v' d4 |% l
(car l_s)4 X6 t) y" t. k7 o( f: [
)
- @ l! Q0 S7 f! s% D0 q6 I0 C (setq l_s (cdr l_s))
. Q* v. s {: _( ~. t c* O1 t7 f ); repeat
9 Q: [% Z; Y! ^" [' }); layres
- O) x% f1 Z* u4 G$ S: X# J' A
$ _$ f Y. a4 s) K;----------------------------------------------------------------------------
# q, _; G+ Z+ r; Get DXF codes2 c1 J! p, j3 R
;----------------------------------------------------------------------------, ~2 J# p# z( F" Q: c
(defun entgetf (index ent)
. {2 ]+ x& }( v% J& r; L2 p ((lambda (e)
! X; z, g+ c& b0 r (mapcar '(lambda (x)- ^ z s; F" m' \" r
(cdr (assoc x e))7 z8 A/ i8 V) S0 x6 i4 T
); lambda6 s: z% e6 Y' y6 S/ V) x
index) ; internal lambda function
% [# n6 O/ k. s' U2 p ); lambda
/ n2 q& X2 o6 W7 S6 P0 M3 U" \ (entget ent)
- n8 I6 @" w, G( R) A )& n! t$ q/ u4 o5 Y9 ?
); defun entgetf
, X2 Y. l' ~. j- K1 ?+ ^' _
/ q4 g7 M7 O) t( U; h;----------------------------------------------------------------------------
% {: u i5 a1 R; Save UNDO status
7 k/ o6 e2 P: x+ ~+ B;---------------------------------------------------------------------------- j, \* H S/ n5 d
(defun undo_init (/ cmdecho undo_ctl)
8 b! z# ]/ X0 b0 P2 @ (setq cmdecho (getvar "CMDECHO") undo_ctl (getvar "UNDOCTL")) ; Save the value+ ^% S: I( H6 a$ c) v' A
(setvar "CMDECHO" 0)/ k0 X. S! j7 J" w9 s
3 X0 w5 x, y8 U4 N: I" i
(if (equal 0 undo_ctl) ; Make sure undo is fully enable
* y+ \1 s m& n4 t; M( o0 @ (command "_.undo" "_all")3 _; u2 A, E+ X
(command "_.undo" "_control" "_all")6 R# u8 N3 j( k
)
0 l4 N$ S% m# a0 j9 H) J& [6 S' T+ }/ `" u0 r
(if (equal 4 (logand 4 (getvar "UNDOCTL"))) ; Ensure undo auto is off; K9 g& `8 M/ ^" M) u- d$ R
(command "_.undo" "_auto" "_off")6 R c( s4 z: w( w( G
)
1 p7 [7 a5 P( a: m) ?; {6 X0 _8 m% `- j; Y" d6 _1 f2 w
(while (equal 8 (logand 8 (getvar "UNDOCTL"))) ; Place an end mark here# @( q6 i8 K( a$ ?9 D# U
(command "_.undo" "_end")
! A; @9 H+ j. C )
4 [! ~) H% o3 C0 P& o& I2 A5 r- }8 z( R, i* E% s+ j5 P0 l' I/ r
(while (not (equal 8 (logand 8 (getvar "UNDOCTL"))))
! Q1 C) Z2 D# b" h. f: d0 y- O$ G (command "_.undo" "_group")
+ S% V$ h" m% L )+ p1 \+ L' ^% U2 p) a% g* v- l7 u
$ n4 _# W! e8 S' X( u } (setvar "CMDECHO" cmdecho)
: O/ j, f4 v* s, } undo_ctl7 E; C1 i: f- J) s9 P2 ~
); defun undo_init
% E& y8 E9 E8 y7 C# ]8 ?( ^9 J# a' ^
;----------------------------------------------------------------------------
. f3 A' q9 L" f% _5 W; Restore UNDO status/ R: V8 H h9 \6 Y- C; }8 U
;----------------------------------------------------------------------------
( L- G8 P* z( e( z' Y(defun undo_restore (/ cmdecho); s( g/ W0 S7 i
(if old_undoctl, @7 d2 I! ?3 D, h
(progn
) _& }5 }8 A9 I7 L4 L5 j7 c" Z! D (setq cmdecho (getvar "CMDECHO"))
$ ^! g/ p& ^/ Y6 X( k, A: c (setvar "CMDECHO" 0)
/ K. l& ^1 v1 Z" r; Y
; t+ R9 X$ A! P/ R" g7 F f (if (equal 0 (getvar "UNDOCTL")) (command "_.undo" "_all"))
: ^2 M8 Z2 B9 C z (while (equal 8 (logand 8 (getvar "UNDOCTL"))); \0 b4 k% H `3 q
(command "_.undo" "_end")
2 h0 x! b: u1 n8 R% @* G ); while6 C z' O; i- e3 A ]3 j6 I
# V5 t- t* h- T- g3 B% U! t( j! X) g2 l, ?8 A' c
(if (not (equal old_undoctl (getvar "UNDOCTL")))
( @3 V. v$ F7 ^# w2 G$ ~; R, v (progn3 k& }6 W* s2 i! b
(cond
+ s) _5 [$ f% j! x0 _$ R' |4 @ ((equal 0 old_undoctl)4 G6 c( x8 z3 G: b/ v
(command "_.undo" "_control" "_none")% J' F! _2 P2 m G2 Y
); Z$ E5 g0 Y& w0 C$ t: e8 |
((equal 2 (logand 2 old_undoctl))# n4 s" E! Q K e6 c
(command "_.undo" "_control" "_one")
' I, r: N) E1 ?) h( @- Q )
* X2 o+ l5 r( P# K )
2 k: m" i( v+ Z7 m, D3 k (if (equal 4 (logand 4 old_undoctl))
% `) ~: _8 O' u9 m! Z$ d2 | (command "_.undo" "_auto" "_on")0 ^" O) ` x1 N8 L. B) K$ P. m3 s
(command "_.undo" "_auto" "_off")
' s7 j; v' V1 W, ` )* j6 A+ q# J2 @$ c
)
3 B6 e- z- ]% z) _9 D )
+ |* z2 S3 \. ~/ x9 W @ (setq old_undoctl nil)! N I; M7 C. c) M5 @" x y
(setvar "CMDECHO" cmdecho)
* b+ D5 |0 i4 h' D$ W3 |4 K7 b )1 }3 a: W* }! F- K# z$ _( _+ J7 q8 ?
)! m a" y: f4 v o0 e( }
); defun undo_restore, }+ A# ?% C6 G+ I" Z* H1 z4 q, J
9 e8 P; z+ ?( a% j
;----------------------------------------------------------------------------9 T6 l0 {6 [4 O- s0 h# M, j
; Save variables8 K) ^! ?! f- J: r8 r- U! I) J+ {7 M4 o
;----------------------------------------------------------------------------
9 ?" Z! b- y2 [+ ~' p, A9 O(defun var_save (a)9 w, f+ _( [' \; M% w8 l f
(setq m_lst '())1 C2 Q/ t0 R- E' I, Z- c
(repeat (length a)$ L& H( n8 z) j' b5 j% J
(setq m_lst (append m_lst (list (list (car a) (getvar (car a))))))
% P& X2 s8 l, ^/ t1 P (setq a (cdr a))' _9 c: M8 M, u# y# f! a
)8 g0 q3 `/ ?5 @4 U( P2 i
); defun var_save
) Q9 c5 I+ |$ x1 j4 C& d4 M( F
8 k) V# s. g& C5 L8 t;----------------------------------------------------------------------------3 }8 P0 r U8 X: I
; Set variables) i# d1 _( L) u: y" }& W( n
;----------------------------------------------------------------------------, C/ l+ T8 D$ Y$ ?& @
(defun var_set (m_lst)
" i* _/ O; H5 g, M1 O( H (repeat (length m_lst)
7 r7 u6 I( w, M& c% @ (setvar (caar m_lst) (cadar m_lst))4 y7 j2 v% b; T
(setq m_lst (cdr m_lst))
) ]( s- Z( `: K" o; C* Y$ d )
/ z! }2 ?! b# s); defun var_set0 [0 }" g/ P2 ]3 p( ^- T5 q- X" h
. `, |# d8 ?! O. }/ A) Q
;---------------------------------------------------------------------------- V) |; |7 x& Q. P" i; e
; Restore variables
# A2 R7 @! c+ R* Y7 P" ^;----------------------------------------------------------------------------4 c3 a) h2 J# x' a u
(defun var_restore ()! }# O7 ~& P! h! I9 e
(repeat (length m_lst)
' Z1 m* n: O5 O/ F! ^( K (setvar (caar m_lst) (cadar m_lst))) x: ?/ S% T- Y" N
(setq m_lst (cdr m_lst))/ I3 m9 M: U" X0 g5 W
)
; v s: n F$ Y1 W3 ?" x. o); defun var_restore% G# W& D+ Z, ^$ K: M) b( U& A
. [+ ~) {5 P a
;----------------------------------------------------------------------------
; N% L$ U$ [# A, ^6 ~) e2 \$ i; v; Initialize routine
4 j. R4 ^" Q# }4 S5 h' X! J( q;----------------------------------------------------------------------------: S$ x! @7 d) I0 F2 B0 S
(defun err_init(e_lst u_enable add_fun)
& R3 }- ^. E; N5 v: S' E4 C (if err_alive (err_restore)) ; To avoid nested call
* l, \" b! t" o$ O; @& t& d. Y (setq err_alive T) T' i2 ~% n0 Q& E& ? E1 T
(var_save e_lst) ; Save the modes
7 K. `- z4 G8 U5 f! F2 M! |3 h, f (if u_enable (setq old_undoctl (undo_init))) ; Initialize UNDO status6 A2 a9 r) C9 ~# @
(setq err_old *error* *error* err_main) ; Save the handle of *error*' X! U) f7 F/ t
(if add_fun ; Add the user cleaner
1 F3 X2 O& M9 Z! R3 K# o% O/ N5 x (setq *error* (append (reverse (cdr (reverse *error*)))# @! u0 k% G" ?4 ?
(list add_fun (last *error*)): V% F4 _ j% L
); append
: l, ^ M4 P/ C0 N7 Y5 M )
3 c/ } V( [$ f" u: |0 H) p: X )
4 J/ R* b" Q' C- U; V) f# p); defun err_init |
|