|

楼主 |
发表于 2007-3-13 08:33
|
显示全部楼层
undo.lsp
;;; UNDO.LSP made by piggy
- k0 `7 V% n0 k;;;7 I& n6 e) u' f
;;; DESCRIPTION9 n- p4 q0 C- N* w+ i1 G
;;; These are general error routines which can be called by * g+ a' C+ ^# N6 O/ x* X! m
;;; other routine. See AutoCAD 14 ac_bonus.lsp for reference
( H% F0 B* w6 q& m2 ?;;;
, A, y' o2 s" q. B;;; SUBROUTINE INCLUDED IN THIS FILE5 H( C% \3 \( O P) E6 }& x
;;; UNDO_INIT
. N. m% |" l% W6 O/ C7 F;;; UNDO_RESTORE7 `, k5 a( J% F* J
;;; VAR_SAVE
2 I: z- I, ^( b+ P3 W' I;;; VAR_SET
, L5 N- Y1 d# I* w3 ?0 f;;; VAR_RESTORE
$ U- o" D% l+ `$ _1 K7 [8 ^; t;;; ERR_INIT
/ v* y4 A e4 @8 q) D6 M;;; ERR_MAIN
' _0 q: g) l; e6 I$ I5 |; t% P;;; ERR_RESTORE* |/ Z; S9 `5 B8 x
;;;$ n1 }! k* A6 L" Q$ Z
;;; DATE: 10/17/98; 03/31/99
6 N m+ U! Y' ^8 {" d;;;7 I% A: U* @# O0 ^) U6 y6 b
;;; HISTORY:
B# Z# |1 Y: a6 M;;; Add routine of mod_att& z6 i6 }2 S7 m: t+ v+ A
;;;
( V* x W! ~' R9 ^; W% r8 X;;; USING METHOD
' ^6 n( u, c& }/ N* `;;; ERR_INIT:
* A& n- V5 R9 H8 Q;;; This routine initialzes the error handler. It should be called as:0 M; D% Y5 x* y8 E% b
;;;
6 o) w! a& |# N# l; C( S;;; (if (and (not undo_init)" v9 s$ u* T- i+ J0 f9 |( K
;;; (equal -1 (load "undo.lsp" -1)) . v% L0 Q+ x- V0 u' K( e5 @5 L
;;; );and
8 _2 c, M; a4 E( P) r# Q! `;;; (progn (alert "Error:\n Cannot find UNDO.LSP.")(exit))
, D* }; @7 `/ h8 t- @# q;;; ); if) z! E( M, p% W3 z- N6 |
;;;1 @0 o. C2 q6 V! j; O5 Z% R- j
;;; ARGUMENTS:+ I4 f! G: T4 s( Q* a; ]) e
;;; err_init Takes 3 arguments. 7 q+ l6 {0 ^" F
;;; 1. - The first element of the argument:
3 a, W; j: @/ l* j, m;;; This is a list of system variables paired with: p$ D" d6 j4 O) ?! \; N2 y
;;; the values you want to set them to. i.e. '("CMDECHO" "ATTMODE")/ E' f& O8 C$ @2 \8 K+ |; v9 a
;;; 2. - The second element is a flag
# s4 O' B7 W; w+ p6 r u;;; If it is true, then in the event of an error
/ q. l2 D. @: x6 P5 E;;; the custom *error* routine will utilize UNDO $ L3 k! k8 d& m% j" P; i4 o w
;;; as a cleanup mechanism.9 h+ D v8 f/ H: f- p- F( u9 w
;;; 3. - The third element is a quoted function call.: F0 D/ r! R2 h5 q, Q$ ~) w
;;; You pass a quoted call to the function you7 M! e1 e+ f4 H2 w: s b% v
;;; wish to execute at the end of nomal routine if an error occurs.
' E$ l- e( l4 n;;; i.e. '(my_special_stuff arg1 arg2...)6 h3 L2 c0 F& X( f0 u
;;; Use this arg if you want to do some specialized clean up
1 o: I2 A" q) r7 d;;; things that are not already done by the standard bonus_error ) w8 @1 |6 B: ]3 E6 A+ X
;;; function./ c1 x9 `) g. T y
;;;5 W4 U9 i( e& Y% a' U5 e0 }5 p
;;; ERR_MAIN: Body of error routine
5 F$ P! e. Q. e9 s4 M# {# L;;;
0 D4 K/ o7 r) Z8 @& h;;; ERR_RESTORE: This routine should be called at the end of command to
, e' Y& z+ Q9 e/ n+ G4 O;;; restore the VARIABLES, UNDO & *error*.
- |9 {& k# B7 g% |2 L;;;
4 S5 l5 k- G9 G- q% _! A; a;;; UNDO_INIT: Initialize the UNDO status
4 ?: v' u# N) o1 k4 {9 j;;;' `8 J8 u/ I1 o9 `
;;; UNDO_RESTORE: Restore the UNDO status
. I. g( }4 S; b, G, }" }# e* V;;;: G; ?( Y; ?9 [, [4 F4 ]
;;; VAR_SAVE: Save the variables. the argument is like '("CMDECHO" "ATTMODE")
, S1 n$ J' d* v$ [6 k6 l) Q% B0 D;;;
L# s& n1 j8 c9 H;;; UNDO_set: Set the variables. the argument is like3 F- `$ F( o" o. `- o
;;; '(("CMDECHO" 0) ( "ATTMODE" 0))7 |9 s3 o5 n7 Q' ]3 Y( W1 w
;;;
) I0 M+ b. D7 K;;; UNDO_RESTORE: Restore the variables
: E+ {5 c* H1 x6 @3 k;;;2 z$ D3 q6 T7 V# p
;----------------------------------------------------------------------------% b D$ ^! U: R6 A
; GLOBALS:
4 S2 Y5 f: t8 |8 I$ Q8 l; old_undoctl -- old status of UNDO (voided by UNDO_RESTORE)
) X+ O& K1 s& o, y+ y6 \ L; m_lst -- list of variables (voided by VAR_RESTORE)( o; p) m1 x8 _! ~6 k9 o
; err_alive -- indicate error routine is active (voided by ERR_MAIN or4 j( ^0 l9 e! B/ w( C5 B
; ERR_OLD): Q, e7 H" m2 q* \# I% Y* E" {+ X& Q
; err_old -- old handler of *error* (voided by ERR_MAIN or ERR_OLD)/ W3 D$ s" b4 M1 e% b; S8 z
;----------------------------------------------------------------------------# k0 o) S+ O% o! Z! f6 u% j
( x' i( L, g4 e, O1 U8 A2 m;----------------------------------------------------------------------------" e `9 C) x+ U- T; i2 R3 p
; Modify attributes according to entity name, attribute name, dxf_item
2 U/ ]8 B/ E9 I! |;----------------------------------------------------------------------------# M. \2 t# E# _/ A. A' V
(defun mod_att(ent id dxf_item)
. [3 {$ T. S- \) a5 j$ Z (while (and (/= "ATTRIB" (car (entgetf '(0) ent))) (/= id (car (entgetf '(2) ent))))6 C$ N9 n* Y: |
(setq ent (entnext ent))
R9 A2 y( P7 e# G- s6 r T# G ); while ) L2 A$ c/ V1 Q! Q$ J4 l% H
((lambda (x)
( {; |' a3 \0 j" N (mapcar '(lambda (y)
6 }( @& g) W4 p# W. ` {3 ~ (setq x (subst y (assoc (car y) x) x))9 h' V& ?) Y* S4 { h; v
); lambda
) S4 y! O" F/ |- S dxf_item
8 B9 H; Y1 }5 ~5 w4 g1 X ); mapcar
. W: o5 q. O) U+ C3 g5 H (entmod x)! ]& ?0 j, y" S
(entupd ent)
6 ]6 Z* L2 O3 ? I) c+ _) | ); lambda
G6 i9 |" A1 ~6 E$ Q1 p (entget ent)( ~* o p# D/ T' Z% v
)
2 y& ^ b4 A- h( r L! O% u e8 @); defun mod_att
3 m" @+ _, n) S0 `# s
0 ?- ~6 e5 }) u9 U5 \! P;----------------------------------------------------------------------------
+ @6 Y* A, F! s; Check layer status, return a association list which contains layer information0 u( x. F: K, w; Q$ g
;----------------------------------------------------------------------------
. F+ q6 d6 B/ N(defun laychk(lay / l_sta)4 `( A1 l$ ]8 L7 P. F$ m& y
( (lambda (x)
7 h' _% e+ u3 D6 _- P- T, ^ (if (not l_s)
) @* @! b' T' G6 p& p2 \7 } (setq l_s (list (cons x (logand 5 (cdr (assoc 70 (tblsearch "LAYER" x)))))))% _5 _5 j5 `% B. a: ~+ R, Y
); if
0 H+ B7 t! X# D5 |: D& p# h ); lambda ; V- k7 A# z" l
(getvar "CLAYER")( _# i# R' F; L \0 F
)
# t$ Q( V4 B. i (if (not (tblsearch "LAYER" lay))* t9 \, V; z# q- b6 D* b
(progn a1 u7 S& s: Q, m
(initget "Yes No")
$ z2 z" O) b! g" M/ j5 G (if (= (setq l_sta (getkword "\n图层不存在,是否建立该图层 ?\(Y/N\)")) "Yes")
9 j3 ?1 U/ m8 S (progn. z! G1 ?' I& L, i2 a. P, ]" @7 G. i. b) d
(command "_.layer" "n" lay "")
9 e7 S( @; F$ t* Y4 n$ f7 d8 _, a4 g) { (setq l_sta 0)3 [* k$ {! Z2 o. ~& `/ @/ V) Z
); progn7 h3 W+ @8 I! Y7 B
); if
- I9 O: P- O* C" p( d7 C ); progn2 h h5 N2 C) v$ t4 w0 Q2 A
(progn
' b0 G8 g+ Z C( X (setq l_sta (logand 5 (cdr (assoc 70 (tblsearch "LAYER" lay)))))
: E5 b; @. h9 R s' x (if (= 1 (logand 1 l_sta))
z( s) Z% b3 U- q( q& S (progn- \- p7 }) D; s3 ?) `0 d* x
(initget "Yes No")
, D0 i' O: `* i8 q+ v (if (= (getkword "\n该图层被冻结,是否解冻 ?\(Y/N\)") "Yes")
9 d6 h$ @! [. _ (command "_.layer" "t" lay ""); w2 @* u! v& v/ l5 [2 s
(setq l_sta "No")# i! M2 l- g/ D, _( h5 u2 D
); if
; i* E1 g8 @( T- c ); progn0 j& E7 W9 I" z' @0 H$ S
); if
0 p( W3 E5 M J6 L# J9 f4 E (if (numberp l_sta)
+ w# l! N8 m2 O$ f0 h (if (= 4 (logand 4 l_sta)) (command "_.layer" "u" lay "")); if
& R- K3 ^: {% c7 f ); if5 Q$ {5 Q. H Y) f
); progn
. ]/ N8 s0 G& l2 ]$ W5 m# g* R ); if% M9 B, C) b: M! P) A
( (lambda (x)
! h' p( m2 d4 q6 x (cond
% z1 Y7 n& F; ]0 y& P2 P ( (not x)5 J, I' V( i( S8 v- r% a
(setq l_s (cons (cons lay l_sta) l_s))# a+ T, K& Z. }: o9 E+ s; p
)
; [6 Q; E" n* _ ( (= "No" (cdr x)). Q$ v# }- k1 V9 ^
(setq l_s (subst (cons lay l_sta) x l_s)), p2 \( Z) r- }6 |& `/ y0 s
)) O2 G2 N; d% _" m
( T l_s)
) a5 l0 B9 E! ?! l1 Y ); cond
6 n0 O W- p) i; F& j" F2 u: @( w ); lambda
# I5 N4 W6 L" G (assoc lay l_s)
! T6 f% N3 Q: H! ` )
" s! r" X! B; p: E0 }7 Z* b- d( N. b) m. e( Y/ c% E, H d1 U
); defun chklay
0 \* z" O$ o# O6 O( o e {: R. D7 D7 U& E4 ~8 i' [7 m
;----------------------------------------------------------------------------$ G/ v2 [5 R V" S. i$ x" R
; Restore layer status according to association list l_s
- F0 @4 o" `4 i" \6 B& J4 ]9 e;----------------------------------------------------------------------------& H: n* U% S% L4 h$ N
(defun layres() F4 A" h/ g. j5 ~" P" I6 s! V* f
(setvar "CLAYER" (car (last l_s)))
; x: j1 K' c9 K/ G. \( ` (repeat (length l_s)
+ t; b7 T1 K7 U) l ( (lambda(x)
. e. p/ {5 v. x) K7 G% ]9 i (if (numberp (cdr x))
# p, z* _9 l; M% V6 X" m (progn
6 }& @$ [" R5 x4 D- U! }0 W. M (if (= 4 (logand 4 (cdr x))), k8 A; D3 z, R1 A
(command "_.layer" "lo" (car x) "")
8 [2 \ |; n# W* L" }, a) J ); if
" Z( w9 T" J: d$ M' ^ (if (= 1 (logand 1 (cdr x)))
, {* d: ^/ n5 A. e (command "_.layer" "f" (car x) "")
, g" D( ]; f7 [5 s0 ^ ); if
" x- e+ |; v9 s: ^ ); progn
" G5 @/ r7 w" j+ @. v! |" \/ m' \ ); if ( a; F& v2 Q# O; e9 e3 H9 _
); lambda: s, e* Y: i9 j1 O4 c( U# ]
(car l_s)
$ l9 z8 I: g! e6 k, [; d$ X& W0 J )
. B" o! W, r3 a7 M (setq l_s (cdr l_s))
7 X2 b" Z6 k# w+ O. k3 j% j ); repeat
p! J% n* i$ g); layres6 O: m% N1 I. q' m5 u4 M
9 V: f O; m2 ^) z; U- O& v: ~8 I0 Y8 u;----------------------------------------------------------------------------
) ?- P, e6 }: G# ^6 T; Get DXF codes( y, t U4 X" e1 a4 H
;----------------------------------------------------------------------------
7 ]1 v* Q! ]" F2 Z9 e(defun entgetf (index ent)6 s4 k. ~4 j! i& a5 t
((lambda (e)
" f8 r! v' d5 J9 [) F (mapcar '(lambda (x)" x1 W) k$ z' X) g$ k* ~
(cdr (assoc x e))9 L P+ O7 y0 e
); lambda
: Z- i6 a5 k9 l" G4 ?: u% A index) ; internal lambda function$ `. T# U$ T8 R7 i6 O( v3 J0 A4 c% n: R
); lambda- ^3 U( t1 V! ^2 ?
(entget ent) , M. _ Z$ y, V
)
. ^% x3 J& V ~- Z2 p); defun entgetf) G- x, e& M* i
* [+ N% S; ]' j1 O: v4 F
;----------------------------------------------------------------------------
' Z5 J" d1 D1 g6 i; Save UNDO status
& O* u) k6 ]7 _$ V: M6 w;----------------------------------------------------------------------------; |6 Y6 @' m8 X; g/ v
(defun undo_init (/ cmdecho undo_ctl)
, k+ @% r; d+ S (setq cmdecho (getvar "CMDECHO") undo_ctl (getvar "UNDOCTL")) ; Save the value
" R) Q7 U7 i1 [' P* f- k2 ^ (setvar "CMDECHO" 0)! C2 I1 U+ a( U) c$ }3 I
7 r# ]0 c% h1 C3 t: y7 | (if (equal 0 undo_ctl) ; Make sure undo is fully enable
P2 H: q8 J ^% s. g (command "_.undo" "_all")1 V- t0 ?4 G D/ z
(command "_.undo" "_control" "_all")
3 q' Y: K: h' U K# S ]" ^ )
3 _; C7 Q% P* n2 [: O
9 e; y2 D& i/ i( {5 Q: F (if (equal 4 (logand 4 (getvar "UNDOCTL"))) ; Ensure undo auto is off
6 I% k. m( j9 R (command "_.undo" "_auto" "_off")" {" J0 |( p) m) R
)
+ t6 z0 H% y0 @- M; S9 f$ X, d8 U
p5 \1 ], Q ]5 d- R. e- H' ^, G (while (equal 8 (logand 8 (getvar "UNDOCTL"))) ; Place an end mark here3 B- R. S" L, h! [1 e$ W
(command "_.undo" "_end")
$ U" v v5 L+ d) h) W- i )
* Z# l# |- ~# I7 [& C; {4 f3 F
6 l' g! u; \; d C c8 @* P5 e (while (not (equal 8 (logand 8 (getvar "UNDOCTL")))). N. l, n# _. C/ _
(command "_.undo" "_group"): U+ A. X/ t G
)$ @$ t4 n& e; a
3 o5 d; V0 E1 M% v' n
(setvar "CMDECHO" cmdecho)
2 U( O% w- s: Y* n# Z undo_ctl; H& L: Z. r* {$ U }
); defun undo_init' ~3 n' y; I* m% V" y" O# n; B
" J n# p7 K, W/ Y/ j: K4 x2 a7 i# x;----------------------------------------------------------------------------
, A8 A, o$ W3 b; h9 L: r; Restore UNDO status
$ K1 m' ~% ?0 e5 L$ b1 Q;----------------------------------------------------------------------------
0 W( y7 K( b. _5 d" Q(defun undo_restore (/ cmdecho)
7 u5 d2 G" i9 _ (if old_undoctl8 u/ V2 _: f: z- n; p; T
(progn
* V' A2 L" t9 ]+ Y% P8 p7 i3 H (setq cmdecho (getvar "CMDECHO"))
7 m) z2 W' o% u6 D$ M (setvar "CMDECHO" 0)0 a6 | X! g" t, d* t$ ]
# I2 q9 z2 C, h& t+ C' e
(if (equal 0 (getvar "UNDOCTL")) (command "_.undo" "_all")) ) p6 T' E1 U: m8 J- T# w
(while (equal 8 (logand 8 (getvar "UNDOCTL")))
5 @5 X: Z( J; K (command "_.undo" "_end")' }. _% G5 k$ Y. Q: b" H- m
); while
; S) ^! Z C: l2 q( D5 q, F; \0 Z. \9 }6 K0 h
$ m0 d0 a2 N6 k. a (if (not (equal old_undoctl (getvar "UNDOCTL")))
' }, c! ?9 @8 L# {* v' {0 Z; P% @3 q (progn! P* s4 H) k* h( x* R
(cond* m6 E9 }2 Y; E) U
((equal 0 old_undoctl)' t, L c2 [$ p& U$ A9 c
(command "_.undo" "_control" "_none")5 I2 N7 y& m& v7 f" G- Q
)6 Q$ I" o* r' Q# I- ?1 ] A
((equal 2 (logand 2 old_undoctl))$ N7 o( g' |& E: H P) e' Z7 C
(command "_.undo" "_control" "_one") I v/ H, N" l# `; t$ T0 \* e A
)# h5 j* a3 @" D* I+ e3 v
)
# ]% P' K( ]# }% n8 n6 ] (if (equal 4 (logand 4 old_undoctl))
: a( l6 a2 {: c (command "_.undo" "_auto" "_on")* E4 N- |( O% x6 o: X
(command "_.undo" "_auto" "_off")
5 c9 ~3 m! v: x% M- {( I) @! V' a )
5 I1 z) b" d3 h* a) m# a ): G# I D# B1 p* a1 z) k4 Y
)
# n( B) {5 m. K8 c (setq old_undoctl nil)
* w: _9 q/ B3 m- V% Z9 S1 O (setvar "CMDECHO" cmdecho)
7 N) D* Y: x0 L' ?8 n )4 U: u" E+ X0 S+ ?4 S# }6 L
)# z" v0 P: d" W1 V3 S/ g* m Z# D
); defun undo_restore) H, @( W8 L# E, J; w
) @& Q) K: ?) z( }/ Z/ B;----------------------------------------------------------------------------
: c% O. T1 y: Y5 I) K# |5 B; Save variables% a6 m" p/ r" o8 H6 e: k* o
;----------------------------------------------------------------------------
2 a( X( `9 s2 e& k$ z8 Z: Z% O(defun var_save (a)
2 e0 a O/ f# n% } (setq m_lst '())
, m: K4 i- z! a/ Y+ n ~" k; l (repeat (length a)0 \, B5 S0 ?2 K4 {+ u) B- f% {
(setq m_lst (append m_lst (list (list (car a) (getvar (car a))))))
5 V; X( Q. _5 ^0 I5 G (setq a (cdr a))
- ^# @1 C9 `0 q& u )
& n$ P6 G4 E- D7 e); defun var_save
s! a5 E3 b! \2 t
2 t0 M1 D( s6 s& [7 a: O;----------------------------------------------------------------------------6 }. u/ r2 m, d" `
; Set variables
6 a" g8 m* p# [9 ?;----------------------------------------------------------------------------
, N7 n7 I( j7 N# P(defun var_set (m_lst)* q( s8 b! s1 q- @( C
(repeat (length m_lst)
2 [9 g1 N: u- ~, O8 C- ~) O. v (setvar (caar m_lst) (cadar m_lst))( m4 S( Z8 h4 |
(setq m_lst (cdr m_lst))
# q; q& w+ e- j9 b. U )! _+ \' t9 X, q* c/ |
); defun var_set
* s- e6 W! g! S; Q7 V1 j# P4 O/ i( C7 c/ r2 C* }0 g5 q0 U* M4 v
;----------------------------------------------------------------------------
1 ]0 _$ g6 O, q7 w6 f; Restore variables
8 T/ h5 e* c A, r; x;----------------------------------------------------------------------------- C9 ~3 G; c* a q
(defun var_restore ()
9 J% C+ g: }2 j }. b (repeat (length m_lst); _% Y9 A2 `! S( H6 Z
(setvar (caar m_lst) (cadar m_lst))' ]0 r2 m( }% Y7 ^" m4 u U: W
(setq m_lst (cdr m_lst))3 q: `, J* e# l$ H0 [
)
% g+ ?. x) r8 F0 P); defun var_restore7 H h5 M) S1 q' [0 u( }* ?3 `" l) ?
6 P; L& D( I! D: d;----------------------------------------------------------------------------" |. k% `% K+ z: U# M: E' y& p' W
; Initialize routine ) Y, d1 U- U$ Y X: E/ n O X$ F
;----------------------------------------------------------------------------: W! j4 E; v8 r6 I% \) b
(defun err_init(e_lst u_enable add_fun). b# `: s7 J1 @$ \7 G' N5 i& E" N
(if err_alive (err_restore)) ; To avoid nested call
6 z0 R; B9 s8 M) e2 [ (setq err_alive T)
) C+ i# `' l, e+ } (var_save e_lst) ; Save the modes
7 @2 p, @' @$ S l (if u_enable (setq old_undoctl (undo_init))) ; Initialize UNDO status" X8 s' n1 `. M
(setq err_old *error* *error* err_main) ; Save the handle of *error*
6 ?% J# q2 ^7 k# m# A, x! D (if add_fun ; Add the user cleaner
$ ?5 }0 C/ ^; B+ m) j3 q+ v. E6 O; o7 j (setq *error* (append (reverse (cdr (reverse *error*))). M. i+ O- o0 Z. Z9 n& E4 } \
(list add_fun (last *error*))
4 Q) ~; Z" r* b b6 Z ); append
3 | {0 T# T* n' O' N1 M# O. l" G )
/ m! c, d/ j3 H, p# H ). |5 w6 m8 F8 z- v* r( A3 x
); defun err_init |
|