|
|

楼主 |
发表于 2007-3-13 08:33
|
显示全部楼层
undo.lsp
;;; UNDO.LSP made by piggy3 Q( e4 y& y2 B3 D6 ~; |' X8 @) p
;;;. z& c- V; l8 @% c
;;; DESCRIPTION9 F2 ^3 x2 o& g& k! |: n3 V1 _
;;; These are general error routines which can be called by
$ {0 C& J& Z2 q& u;;; other routine. See AutoCAD 14 ac_bonus.lsp for reference2 g2 g6 F# F' G. x, h
;;;& {- q: P( f4 x. h
;;; SUBROUTINE INCLUDED IN THIS FILE! x3 [* C% E: K) `# w* t! f
;;; UNDO_INIT
' i. G- r/ E6 U# O8 E9 ~;;; UNDO_RESTORE
* V$ j" R: K5 \1 l# z! z5 m;;; VAR_SAVE
( W& D6 a3 f$ J0 h ]8 Q: L6 H) C) o;;; VAR_SET
( V* l0 n- u% s- Y u;;; VAR_RESTORE( A0 d3 C3 M& I l
;;; ERR_INIT 5 C& S0 k7 Y2 N1 Q
;;; ERR_MAIN
/ F) ~: K8 A0 _7 C# H$ |, E;;; ERR_RESTORE
5 ?. o3 D; p: V) F {# h0 W n;;;1 i/ ?6 s, B+ }* }$ A
;;; DATE: 10/17/98; 03/31/99- _3 c+ m* J9 v/ v, O( m
;;;
$ s, W1 o4 }" ?2 C2 \;;; HISTORY: 3 c5 @0 C$ A% o- n8 s+ O
;;; Add routine of mod_att
3 Z( z( E2 o8 v* _6 _0 u;;;
. c, `7 K* L) h# w" f;;; USING METHOD x" ~) _0 o5 D
;;; ERR_INIT:5 l- X$ ]+ _1 \& e9 E- f
;;; This routine initialzes the error handler. It should be called as:
' Q+ ^' ^/ V9 l& w: _;;;
4 q9 g- w f4 [5 X, M/ Z* |4 N' F;;; (if (and (not undo_init)
: F+ j; e$ x4 c# X! l+ \- I8 J( };;; (equal -1 (load "undo.lsp" -1))
/ `7 O! |7 w$ ?8 Q: B;;; );and* E& M _% u1 F9 W1 m
;;; (progn (alert "Error:\n Cannot find UNDO.LSP.")(exit))
* l% |: M# M$ ~- B;;; ); if
# p& Q" e& B' ^9 P/ n: H! m4 N! };;;3 b* r1 d" q8 D; F4 E
;;; ARGUMENTS:4 {$ A9 c* w4 ?. d @) K
;;; err_init Takes 3 arguments. + }2 q; A# U+ [+ `1 |, G
;;; 1. - The first element of the argument:
8 @; Q9 J% v; K. q0 {;;; This is a list of system variables paired with s. N4 s2 b3 R" [, ^2 ]% o7 R# z
;;; the values you want to set them to. i.e. '("CMDECHO" "ATTMODE"), O0 Y# i0 k! _% w, ^. p$ H7 X
;;; 2. - The second element is a flag
0 W6 l7 O/ f- \0 Y& ];;; If it is true, then in the event of an error + e9 M7 c4 t, W* O5 s
;;; the custom *error* routine will utilize UNDO
% O; M) F( R: z; u7 N;;; as a cleanup mechanism.; n; e( `) I* M$ @" H; z) b
;;; 3. - The third element is a quoted function call.( Z) K$ K) O# v
;;; You pass a quoted call to the function you0 ~- L& A( O2 j, {0 v- [
;;; wish to execute at the end of nomal routine if an error occurs.
2 U8 P0 {2 m! o8 ~;;; i.e. '(my_special_stuff arg1 arg2...)
4 a. z1 {) o# T4 y;;; Use this arg if you want to do some specialized clean up
% o# y, \ Q4 D J$ o% v;;; things that are not already done by the standard bonus_error 1 n( O" z- @! i( L8 C
;;; function.
4 Z+ F1 Y. _' W;;;
" v W" }4 n- g# V;;; ERR_MAIN: Body of error routine/ f1 v4 ]+ U' v8 Q
;;;
5 v: B; d. ^7 K$ U" m& \: S; Q;;; ERR_RESTORE: This routine should be called at the end of command to2 Y: N( q+ f) L# p; C, u
;;; restore the VARIABLES, UNDO & *error*.% V" C) P5 ]* q
;;;4 x7 K, |+ ~$ x; R
;;; UNDO_INIT: Initialize the UNDO status
5 T' U! ~) J8 B7 w* j3 i! U( \ ^;;;
, A' O# X+ G8 k, c;;; UNDO_RESTORE: Restore the UNDO status
) n1 Y( u, y1 n$ K;;;3 y! l6 u% X' U" O! H" U$ ~
;;; VAR_SAVE: Save the variables. the argument is like '("CMDECHO" "ATTMODE")
: ^) g; @6 E7 }/ u% P3 r K- ];;;
" G9 t4 }& ?7 A1 b. K;;; UNDO_set: Set the variables. the argument is like& O3 E( p, a& o
;;; '(("CMDECHO" 0) ( "ATTMODE" 0))
) }( P7 X* Y' O: a1 B; _% t;;;# f: W7 H0 z5 a) @' \; L1 |
;;; UNDO_RESTORE: Restore the variables
" J- W- c1 {0 [7 `) C4 G;;;
/ T A9 j' b: k4 B; D1 a d;----------------------------------------------------------------------------& Z, M7 S% [8 }" C
; GLOBALS:, g3 ?/ h; m9 C" X% N y' S' V7 i
; old_undoctl -- old status of UNDO (voided by UNDO_RESTORE)
9 o* Q. G- L* G2 f! I9 l. T. m6 g; m_lst -- list of variables (voided by VAR_RESTORE)) k+ v( {1 y& Y9 o/ E
; err_alive -- indicate error routine is active (voided by ERR_MAIN or5 W5 \: s J, w2 i
; ERR_OLD)/ C( b& O6 }1 @9 F
; err_old -- old handler of *error* (voided by ERR_MAIN or ERR_OLD), t0 E7 z' M( [
;----------------------------------------------------------------------------
# }" k% p2 \. [' F, _' U- J
) z; p3 Y9 x7 l* R& K" o, k;----------------------------------------------------------------------------
$ x' ^8 E, [. {& A ]; Modify attributes according to entity name, attribute name, dxf_item
, P3 J& v3 T$ E+ z& N6 {;----------------------------------------------------------------------------, r" `, r! P- E. c+ h G
(defun mod_att(ent id dxf_item) % e; G3 e& C$ e8 {# k
(while (and (/= "ATTRIB" (car (entgetf '(0) ent))) (/= id (car (entgetf '(2) ent))))
6 k: W5 q$ n, h# F$ R (setq ent (entnext ent)): k8 l' e) y$ B- k) }
); while
' K' h3 ~. U( z' X p/ _ ((lambda (x)
, H* m0 e) q5 m* O3 b. s (mapcar '(lambda (y)! [8 R; }4 i9 a& ?+ j, V
(setq x (subst y (assoc (car y) x) x))
4 x: e c* u: f ); lambda! |: q* T4 L8 J7 U- K% Z; x: w
dxf_item
% L' A, V0 O k1 w! s# l ); mapcar
2 A/ [" c* A8 X: H3 X: w j" V (entmod x)
8 p$ S) h2 a8 X0 w# r1 } (entupd ent)5 ~, E& j7 q1 n& f2 Q+ d
); lambda
8 N' A* g. A% {1 L A1 y& @ (entget ent)/ j3 v& [1 H2 t
)
$ e M8 B8 }6 t9 |, C); defun mod_att
) Z# _. y" w; Q- I: F3 B, ]% R" _. B$ @1 O% V
;----------------------------------------------------------------------------
5 e) u4 F/ T: h7 m; Check layer status, return a association list which contains layer information& v- L6 a0 G( c) v; L* |
;----------------------------------------------------------------------------2 Z9 s/ V6 Q& ? E( J3 Q: p
(defun laychk(lay / l_sta)8 E2 c* z2 e. F+ ?& k3 ~( G
( (lambda (x)
$ W2 o( J$ R4 q, x (if (not l_s)
- C) C, C7 [4 N) U( a t (setq l_s (list (cons x (logand 5 (cdr (assoc 70 (tblsearch "LAYER" x))))))). u7 r0 |/ Y: k: `+ z! d- V
); if" K3 T8 W1 S4 [" ?& \
); lambda % u- H z6 s7 t4 U6 i
(getvar "CLAYER")
) x/ H7 T% }8 J- u j- {5 x- P )
9 Q8 Z% e# ]3 r! w- e. ^ (if (not (tblsearch "LAYER" lay))
6 p% Z, X* L C! o& F2 M (progn
o7 K& e+ p: a* A (initget "Yes No")
3 e3 s' v$ _5 N' F/ G/ |; ]& l# A9 w (if (= (setq l_sta (getkword "\n图层不存在,是否建立该图层 ?\(Y/N\)")) "Yes")
1 I" a$ I( p0 M6 { (progn
# @$ X/ _, U+ \- a; [$ ?' e (command "_.layer" "n" lay "")
& y$ H# u1 [) M' O: [ (setq l_sta 0)
/ i4 d( O) g1 J$ S: q! A& {* Z ); progn5 f) I0 {" ? o' e7 `; k
); if. A3 O) i0 D9 i0 b1 }$ h' M6 h5 s* y
); progn
* O6 n( R+ u5 i+ H3 N8 ? (progn( ~( X4 i9 T% F; B
(setq l_sta (logand 5 (cdr (assoc 70 (tblsearch "LAYER" lay)))))
4 Q5 j9 n% W3 n! T: k6 ^ (if (= 1 (logand 1 l_sta))# u4 F% ?, F0 F& q9 _' W* x
(progn
2 J% ^5 i3 C; m% m (initget "Yes No"); Q! Q3 z. G% F' c- H8 V4 Z
(if (= (getkword "\n该图层被冻结,是否解冻 ?\(Y/N\)") "Yes")8 ?6 w5 z2 @$ Q# e! R& z0 o. T+ e/ G
(command "_.layer" "t" lay "")
/ E/ }$ l; C7 B (setq l_sta "No")7 S+ x/ V7 Y- ?0 h4 K
); if
v$ L: B' Y. V: O1 u* g% U) p ); progn+ z' e B; H6 a5 i1 B
); if" H9 |; n9 J, d# U$ l- ?
(if (numberp l_sta) ; d% A$ e. i8 h! w1 K3 m3 ^ p
(if (= 4 (logand 4 l_sta)) (command "_.layer" "u" lay "")); if
4 _- g$ Z* K9 \. N5 C ); if
/ P& U% U" k$ c k3 s3 y4 e ); progn
9 k5 \3 m/ x+ _6 m' K/ P6 o; \; l ); if0 [! T9 ~' I' @ X7 ]# v/ d
( (lambda (x)
0 x6 g d4 j6 R+ o# i: X6 \ (cond + F9 P7 ]4 ~0 x0 g8 y2 o
( (not x)
% J) L. P9 B! p$ M/ ?; y, _5 X (setq l_s (cons (cons lay l_sta) l_s))
8 `, \. E2 o# k- X Z7 \6 D% k' H )
2 v3 Y" v- g; X1 L* \: q ( (= "No" (cdr x))! _7 v6 }( A0 u1 k2 v& G
(setq l_s (subst (cons lay l_sta) x l_s))
8 T9 x( N, }# Z+ J& ]. l )
. O. Q. O- n0 E: [ ( T l_s)9 k4 M! L* x s9 F- p* [. \* m+ J- ~
); cond# }4 ?$ P. r2 u- \# C
); lambda
7 h7 n8 C) X, D2 u3 d+ e9 Q# c (assoc lay l_s)
# R' z: W: h3 `. T; R )
7 {( v6 J- R7 ?9 G! L/ l$ \* C) D" @! l, Z8 J
); defun chklay9 L4 }( ~& A a# a& R* c6 ^8 Z ~
- |" M& M0 }, t) F' a- O
;----------------------------------------------------------------------------
7 Q9 J# {" ?4 u1 ~* O5 C; Restore layer status according to association list l_s
6 L$ L& f8 T; N;----------------------------------------------------------------------------
2 z. t8 a( h1 G6 C9 w7 s# \(defun layres()
. J x" ~, _& N0 f- g9 P" x (setvar "CLAYER" (car (last l_s)))/ W s* z& N8 v$ X" @
(repeat (length l_s)+ A n7 {6 V7 q- j4 _
( (lambda(x)
9 n/ y* @# A. c" ~4 w (if (numberp (cdr x)); O5 L( `1 |$ Q5 ~
(progn : M% M0 p, d( d2 V2 _* P) R! Y9 c, P; e
(if (= 4 (logand 4 (cdr x)))
% B. f; B' F Y" q3 w. x; N9 g (command "_.layer" "lo" (car x) "")# l1 m4 n9 h3 I! x0 z" t1 x) m2 G
); if9 r2 M2 m! s) X) P" u Z& f0 z
(if (= 1 (logand 1 (cdr x)))
& H3 Z/ C0 Z) [% q. r (command "_.layer" "f" (car x) "")8 A% F; R' J$ h
); if/ K% j5 {) D. n2 s5 v9 I
); progn
" V6 n# o4 n" O4 K) l, z( K) b: @ ); if : n* Z5 [+ G! y, `+ Y
); lambda0 X9 \$ b- T! y3 J8 y
(car l_s)4 z8 @: C! `: N
)
s, R* m% e) |4 [8 k$ s& F1 O (setq l_s (cdr l_s))
# }5 u+ z6 \& e- F ); repeat
3 u3 i, m, Z- y8 d); layres* X2 H, A/ a' K
% D: ]9 @) U0 z- l
;----------------------------------------------------------------------------
8 r5 l% W& [( @5 p; Get DXF codes6 r( ]. t! ?/ d% {! M/ D3 z
;----------------------------------------------------------------------------
% r1 f1 L+ u, ~3 q(defun entgetf (index ent)* v/ T+ ^ n" s6 I! }6 T1 ]* `
((lambda (e)
8 D* o+ |/ c+ l/ r: Z (mapcar '(lambda (x)2 Z' A; ~% W/ _: P+ m
(cdr (assoc x e)) @. E$ Q1 x9 ?4 [
); lambda
& H: R+ C0 g) z8 i index) ; internal lambda function4 d- J G% T. W# E
); lambda7 K- q/ @. l+ l$ v- k3 f U, a
(entget ent)
+ b: G* \$ X0 J* f )4 T* O* h, ^. L* U+ |% N& _
); defun entgetf
5 ]' b) g& U, r1 H" i+ @& P) i+ o3 Y3 ^0 h) A4 a. W, h9 w
;----------------------------------------------------------------------------
* J3 n- D( D! d z/ D$ y/ U0 p* w/ q; Save UNDO status
% @; }& a! ?4 @" |/ Y/ i% V6 Q;----------------------------------------------------------------------------
0 r# {" x+ g9 O. T(defun undo_init (/ cmdecho undo_ctl)
- S$ S2 ^, b' u% t4 F0 ? (setq cmdecho (getvar "CMDECHO") undo_ctl (getvar "UNDOCTL")) ; Save the value
0 `+ X3 [* Q) w/ w$ l1 |7 J" G% g (setvar "CMDECHO" 0)
+ Y7 f* Z. `+ j4 Q# ~, c' O% u5 h9 l4 O4 I
(if (equal 0 undo_ctl) ; Make sure undo is fully enable: ]4 R' T b. u L
(command "_.undo" "_all")& {0 q8 _) v ?" v+ @! ~( r0 n w
(command "_.undo" "_control" "_all")* M6 i. d$ ^! w3 z/ V' ?* K8 ~0 A
)+ Z8 V& X7 V: S/ v1 w) R
& s: |# v! l3 R- o( n5 W9 o; X (if (equal 4 (logand 4 (getvar "UNDOCTL"))) ; Ensure undo auto is off& f* @$ S3 P4 W$ i6 X) x1 |9 ^
(command "_.undo" "_auto" "_off")7 ]( F) X2 T3 {, S
)
x! f B( d. ~, o$ G. a1 \
: h1 F" J. w4 G+ C+ M, T3 D (while (equal 8 (logand 8 (getvar "UNDOCTL"))) ; Place an end mark here
! ]8 z# ]7 ~) B/ S (command "_.undo" "_end"), V2 P7 E* @* S8 c: u- r& b
) N5 x3 J5 S: p; v- F& b+ w
2 {7 {4 }. l7 K6 z+ F (while (not (equal 8 (logand 8 (getvar "UNDOCTL"))))
- D9 k, i4 G; [- |& v (command "_.undo" "_group")( B) m5 X/ c6 `
)6 A/ j! R9 P# z4 L) I
8 c, m; ?* I3 w S2 I) ] (setvar "CMDECHO" cmdecho)' \& ?; i1 O; ~8 ~
undo_ctl
* h: d% a" {; I' E t$ e); defun undo_init, g; d8 c8 q- j+ f/ D
5 O# O5 w F$ F* e; ^4 Y" E;----------------------------------------------------------------------------6 f) m7 Y+ F5 `( ~
; Restore UNDO status
. p0 z9 k/ o8 }2 R. p4 g;----------------------------------------------------------------------------
# I* p R) C9 m: w. B4 B9 P* O! U(defun undo_restore (/ cmdecho)4 f2 I: v5 y7 t
(if old_undoctl
8 U" S9 y5 |2 c' K (progn9 Y7 F/ I* p- F2 i" w* z
(setq cmdecho (getvar "CMDECHO"))
. g% B/ k( x5 ]& F, b4 j7 V (setvar "CMDECHO" 0)
% n8 a3 b7 k1 Y9 l5 Q# z
5 `% l6 C7 z9 _% H& P8 B3 i4 e (if (equal 0 (getvar "UNDOCTL")) (command "_.undo" "_all")) ; \0 _( W0 n C+ x9 h) C* _. E
(while (equal 8 (logand 8 (getvar "UNDOCTL")))
. h x8 t5 u/ L9 R0 _9 G (command "_.undo" "_end")
! Y5 r0 g' Y; E& r! i1 a ); while
1 ]$ r; h3 S; M3 [# u, [/ t9 _4 v' }8 F& V7 Y
; @( K8 N1 T/ `: Z
(if (not (equal old_undoctl (getvar "UNDOCTL")))7 Z6 R) B- K0 c7 j$ ^
(progn6 a" S: G8 `' ^$ U% ]
(cond% e! H8 ^0 P, p) B2 O: @3 k
((equal 0 old_undoctl)/ i$ h( `7 O& ?; h* P+ |9 R
(command "_.undo" "_control" "_none")
+ q+ f" N1 ?( [- R )4 f ~; j2 C$ N4 P3 F& q3 l$ s8 i
((equal 2 (logand 2 old_undoctl))
( t; H# R' T9 V, C0 S (command "_.undo" "_control" "_one")
( k: i* _3 e7 i5 x% k6 O )4 A6 M. k) m5 k3 D9 k* B* v; P. d: M
)8 y: ]. s" V3 M B* b! F
(if (equal 4 (logand 4 old_undoctl))
% N: U8 u3 H4 J (command "_.undo" "_auto" "_on")
$ Q! M% k* |7 Q1 `% Y2 q (command "_.undo" "_auto" "_off")% C6 H) R7 L: e. X$ c* [
)* s: k5 f5 N# B9 B* ^: v
)
3 L: V( |/ o; o5 H. M ) T) W' s, i) ?* ~
(setq old_undoctl nil)2 r* c x. l' [/ z& K
(setvar "CMDECHO" cmdecho)
" ` n; i1 t1 o7 l& t9 E* o+ Q/ h' a )# Z( e. e, Z( h
)
3 L: s7 N& `/ c, ?); defun undo_restore
9 X# ?8 q8 P& p; d# c" ? M7 h @* R+ w, E1 {1 C
;----------------------------------------------------------------------------
& O4 u. @' m0 w+ x4 S; Save variables0 U* t; l! A! F w, m
;----------------------------------------------------------------------------
% }% ?; z6 f! q4 y(defun var_save (a)# R* p* g! i: a, t0 v9 {; n0 V! B. S
(setq m_lst '())
- x' C& {- _- z7 ~" |0 l+ Z' R4 n (repeat (length a)
% |/ D0 U* ?, ^* ?9 V (setq m_lst (append m_lst (list (list (car a) (getvar (car a))))))* U3 p* j3 W' l- _# c
(setq a (cdr a))' h7 Y5 M* w& [5 M$ h z' \( H- O2 a( ?
)$ w, s6 Y# ^8 h7 E" B; ?
); defun var_save/ g5 K% R9 c/ t. `' B: A
# f0 E: A, O& z
;----------------------------------------------------------------------------
* [. s/ K( g2 ^; Set variables D7 L6 i: D0 Q
;----------------------------------------------------------------------------
" d; i, g6 M2 C- {2 }( W(defun var_set (m_lst)( f6 e$ K+ }, y# j5 p; S
(repeat (length m_lst)
" O+ q* J5 F3 k2 `. P (setvar (caar m_lst) (cadar m_lst))
" U1 h% Y; y+ U3 O- t% A7 Z* P8 _& w (setq m_lst (cdr m_lst))# j' v& k+ C+ G b
)" }& ^, R( G' x% T
); defun var_set
+ h) E K7 l9 g7 J8 s& m; N7 L1 {1 e
;----------------------------------------------------------------------------) r& k- Z. K' @9 y
; Restore variables5 g3 R( |& x5 l2 l$ a
;----------------------------------------------------------------------------
4 @9 I: {! S( k1 n1 c(defun var_restore ()! ^5 H2 H8 D) _* d5 g
(repeat (length m_lst)
k0 n$ r4 S' r9 B+ I (setvar (caar m_lst) (cadar m_lst)): v: T) f" v7 u" }7 p9 {3 [. T
(setq m_lst (cdr m_lst))( u5 a. |' ]- Z: I9 A
) H6 ?* m4 W0 I0 g' T& c% k8 I2 _
); defun var_restore. B6 A' E6 |* l8 g: E
6 v0 R3 o2 L- c8 A2 ]: X: M6 Z; l1 A
;----------------------------------------------------------------------------; @$ O# g9 K2 M: @" j
; Initialize routine H: T. I* Z/ K$ {: [' w7 G# d! U
;----------------------------------------------------------------------------% ^% U! j' X) B: O, U/ q
(defun err_init(e_lst u_enable add_fun)
: P8 \5 j9 Y0 I (if err_alive (err_restore)) ; To avoid nested call* H* r/ h3 i- J# c. x
(setq err_alive T) F7 Q: B9 U. ]0 w
(var_save e_lst) ; Save the modes$ a! g# A+ I2 I0 @( D8 K' ~
(if u_enable (setq old_undoctl (undo_init))) ; Initialize UNDO status' y$ h" q. W0 S3 K
(setq err_old *error* *error* err_main) ; Save the handle of *error*8 J' C8 w S0 l! X
(if add_fun ; Add the user cleaner
+ H x1 V+ T, |1 f% B (setq *error* (append (reverse (cdr (reverse *error*)))
: R8 l- c0 e& z; h( [ o (list add_fun (last *error*))5 w6 ~" F1 }7 a- d k
); append2 ~& H1 h" \: Q
)
5 I6 C/ l0 ~, B) s0 x )
/ `% M$ V9 m4 V+ c2 k0 H; Z); defun err_init |
|