|

楼主 |
发表于 2007-3-13 08:33
|
显示全部楼层
undo.lsp
;;; UNDO.LSP made by piggy
2 m% B9 B6 K' `4 X6 f2 `/ C: F;;;
# N- a1 V/ F1 {8 O) b' k# V;;; DESCRIPTION
5 X# w% u$ L* ~# \+ _- D; w* H;;; These are general error routines which can be called by
1 N; B( V' s l; R G;;; other routine. See AutoCAD 14 ac_bonus.lsp for reference
( Z V6 P, J4 \: N;;;
8 Z y3 m3 j# j+ y+ P2 k! z" ?;;; SUBROUTINE INCLUDED IN THIS FILE
( } M# m' t9 ~; g5 T8 x;;; UNDO_INIT1 {. H( D- K E- q2 S4 u; Y+ T
;;; UNDO_RESTORE
+ i" n1 y- I0 V+ E;;; VAR_SAVE 4 w# ]% k+ ~7 }& h2 @9 @
;;; VAR_SET
) e Q5 ~! y H8 u;;; VAR_RESTORE+ ^+ f2 ~. G1 V& a( Z
;;; ERR_INIT
% e' n1 F. V+ y2 J' f) x! s;;; ERR_MAIN
! J1 |7 x- P- X8 Y9 X3 l' K" j;;; ERR_RESTORE2 h5 {1 [. U; T+ I6 ?& E$ F/ w
;;;# K. @8 e- r4 j! W7 L* t
;;; DATE: 10/17/98; 03/31/99. y( \4 x6 n; ]. F1 W4 q# ^; O
;;;3 x4 R* E2 I3 x5 b. M6 B: k. A
;;; HISTORY:
! h8 k3 p N% {/ @;;; Add routine of mod_att$ h( h9 b% u6 A+ [" R: z
;;;
+ a. H1 s% R, Z) D, ~( i! ?;;; USING METHOD
' G) N( @9 J0 S2 }. Y- g;;; ERR_INIT:
& Y8 y& V6 ^, ]* r+ p;;; This routine initialzes the error handler. It should be called as:6 g$ Z Q( o( s C
;;;2 u4 N! N4 ~5 V6 q
;;; (if (and (not undo_init)5 p! h0 M# N6 f: P1 }3 G; v
;;; (equal -1 (load "undo.lsp" -1))
& S% Y( J5 b: P5 U;;; );and& u N5 G, C0 q$ f( l
;;; (progn (alert "Error:\n Cannot find UNDO.LSP.")(exit))
# J; J2 T4 t. ]6 S/ L. I;;; ); if8 r/ |& l% X, @
;;;
; q) V& D" b' w/ e2 a;;; ARGUMENTS:
3 z3 R' t! f3 D i5 j;;; err_init Takes 3 arguments.
$ c& e' V6 g% H5 z5 O9 J;;; 1. - The first element of the argument:
9 J/ v- g& n& f8 j. L0 x;;; This is a list of system variables paired with* H6 c B7 h' j( ~1 E4 o3 f) P
;;; the values you want to set them to. i.e. '("CMDECHO" "ATTMODE")
" o" q; B6 |4 h, i% _;;; 2. - The second element is a flag
# w" {/ c0 q. j2 C4 c! e$ {8 U;;; If it is true, then in the event of an error # Q- @' a. ]& ?4 J
;;; the custom *error* routine will utilize UNDO 6 z7 ]1 D7 o" E+ t% b& L9 O
;;; as a cleanup mechanism.
9 a+ x: ?0 l' g8 s;;; 3. - The third element is a quoted function call.
+ V0 {6 }- C7 {# U, T2 z;;; You pass a quoted call to the function you, ]* E3 S; J. l9 k w, f( m9 E
;;; wish to execute at the end of nomal routine if an error occurs. . o" d' T4 R) M) Z- s% v# x
;;; i.e. '(my_special_stuff arg1 arg2...)
% A6 N1 V: {+ O0 I7 Q; n/ c;;; Use this arg if you want to do some specialized clean up
( D! ?! A8 z2 Z+ o/ L* {+ r;;; things that are not already done by the standard bonus_error
6 t# T5 t9 n, W1 ?$ {6 E! f* {;;; function.
% w4 s5 G, @% u5 b8 w;;;7 [4 @7 n0 v, b5 r
;;; ERR_MAIN: Body of error routine
: C3 \, ?+ c5 D6 y9 V;;;( D# b/ z" w" ^% n- Y: H/ u
;;; ERR_RESTORE: This routine should be called at the end of command to
]& O. E, ^) z, @;;; restore the VARIABLES, UNDO & *error*.9 i: Y) n3 r7 K. z* i& V
;;;
0 q- @0 k6 @7 i3 z5 s+ {;;; UNDO_INIT: Initialize the UNDO status
x1 z# b6 r$ ~ C/ O;;;
% B* @" r& N9 w4 H6 T;;; UNDO_RESTORE: Restore the UNDO status
9 _* G' O/ E) o) j8 @9 z" Z$ B8 r;;;
* G. U! G2 y( I. Q3 {;;; VAR_SAVE: Save the variables. the argument is like '("CMDECHO" "ATTMODE")
" w; m$ Z7 n! b' U" w2 ^; L;;;
# L0 _8 |& d+ y% r) G) ?! a; U7 s;;; UNDO_set: Set the variables. the argument is like
/ ?5 C1 ~& G0 f0 k2 s;;; '(("CMDECHO" 0) ( "ATTMODE" 0))6 L1 s/ @- [2 _* ]* Z
;;;
: g0 v3 M8 ~- v5 D) i! E;;; UNDO_RESTORE: Restore the variables3 ~5 B7 Z3 Z' [& G4 i1 s
;;;
D, v% u! J2 V. h. y;----------------------------------------------------------------------------
5 P) S* l7 c/ z$ V: U; GLOBALS:; N) d( `: U: j( d
; old_undoctl -- old status of UNDO (voided by UNDO_RESTORE)# B& R7 T; P/ T- o
; m_lst -- list of variables (voided by VAR_RESTORE)
) G( ]( Z2 g. }3 G& R9 e; err_alive -- indicate error routine is active (voided by ERR_MAIN or
8 V/ n/ R; C3 V: w! ]8 z; ERR_OLD) F' X. y6 I1 E' d
; err_old -- old handler of *error* (voided by ERR_MAIN or ERR_OLD) J# [( M5 b# T/ s* y; O
;----------------------------------------------------------------------------
/ C1 p, G) B/ x5 |2 c+ L% I
1 f& q3 ^/ i4 q, g' Z* w A;----------------------------------------------------------------------------
( H: e/ W" m) L1 Z/ u6 w4 U# X9 F3 }; Modify attributes according to entity name, attribute name, dxf_item
* P' ~( h5 r, b* C1 k5 N3 n# ~;----------------------------------------------------------------------------2 s8 Y. r0 K% z( s8 _! L# @: [
(defun mod_att(ent id dxf_item)
, Z: P/ N9 U' |9 r% l" O8 K (while (and (/= "ATTRIB" (car (entgetf '(0) ent))) (/= id (car (entgetf '(2) ent))))
$ W& W6 q0 h8 e; N" E% M (setq ent (entnext ent))
1 a- g& Q# @/ c ); while
- a& W0 I/ d# t3 b. F ((lambda (x)
$ G$ M% A9 H8 [1 M+ u (mapcar '(lambda (y)/ _ H. c" n9 |' a
(setq x (subst y (assoc (car y) x) x))7 M. @7 v- q) D" B- }. w- D
); lambda2 }! J1 T- ^- u& X6 O' @( ]
dxf_item
0 K% X. m% E& T7 l3 { ); mapcar5 b" Z: i+ m ]
(entmod x)- P+ f: T; ?0 m8 U: g
(entupd ent)% r# x! g, z! z" V9 j
); lambda! V( ^/ Q* R1 s6 j& `
(entget ent)$ N! e E/ S( _+ R9 { D
)6 I3 O7 m+ X/ B
); defun mod_att
V9 l4 E4 W+ l! q/ {# m# ]4 S {) k3 D# \; V5 \6 a/ B9 ^
;----------------------------------------------------------------------------5 d& y+ O) P$ o9 U; Q3 E2 B
; Check layer status, return a association list which contains layer information8 _$ k- w) i: c z# g) \5 x
;----------------------------------------------------------------------------# V4 y6 n6 O+ x
(defun laychk(lay / l_sta): d! S5 H( `! C9 {2 m( U! F% W
( (lambda (x)
% _ Z) E1 d" N7 V6 U3 V( |! T (if (not l_s)
& n5 @# S2 v7 u/ j (setq l_s (list (cons x (logand 5 (cdr (assoc 70 (tblsearch "LAYER" x)))))))3 z# t& `4 R: s
); if
% k, O* ]& P9 O$ f1 o ); lambda , Y( E* `: Z: z9 N$ L* I8 s4 h
(getvar "CLAYER")
0 E* U6 V* ^ l* a )
# {$ Z, t8 {' w0 \- E' h (if (not (tblsearch "LAYER" lay))
3 m `% p' S4 Z* R% B3 _- A (progn
3 ~% M$ \" a4 w9 ^- ?+ i (initget "Yes No")5 v, }7 T3 F" N( p
(if (= (setq l_sta (getkword "\n图层不存在,是否建立该图层 ?\(Y/N\)")) "Yes")
2 h- X0 z' C7 A" I3 k% ?! w (progn
4 B/ Q% l7 y0 A7 o) n7 V1 P$ i- c (command "_.layer" "n" lay "")
% z, F2 _( h/ o, d s, z3 v (setq l_sta 0)
( k. m1 c2 V d) H! U7 Z: l ); progn p! v$ C1 y! A) d6 D
); if. M& {- b) t! i1 t i% U
); progn i* w$ Q% U" ^+ G+ k7 U
(progn$ w0 _4 F) n Z6 d
(setq l_sta (logand 5 (cdr (assoc 70 (tblsearch "LAYER" lay)))))
9 }/ h! f }& V# i7 s# a (if (= 1 (logand 1 l_sta))
3 D1 |; L: {& y+ L6 T (progn* \2 ]9 @( J1 a0 ~9 j7 j' H) u
(initget "Yes No")
( I2 A5 i- t, I6 z4 b; ~& ] (if (= (getkword "\n该图层被冻结,是否解冻 ?\(Y/N\)") "Yes")
: Q) t. a6 N" K3 Y3 v (command "_.layer" "t" lay "")4 T' e7 D* ~2 O
(setq l_sta "No")
9 j/ _* y# t+ v- D' n0 x ); if9 X$ n D$ X- ^
); progn, g+ a n9 M- B2 Y
); if
. j- @: }# W& ?& l (if (numberp l_sta) ; l" L2 O- }, b- c: _7 n
(if (= 4 (logand 4 l_sta)) (command "_.layer" "u" lay "")); if$ d' k6 ^/ R7 T4 T! E4 v: H& I
); if9 e8 ^) E( A7 H8 U! e- W& C
); progn* {# e2 F; Q. L- H
); if" A W7 l. r- w! V
( (lambda (x)9 N G" t G+ }1 O
(cond
+ e3 u* {! k/ Z0 w, W; P ( (not x)
/ T1 f! i; F' P( i1 g6 {7 ~' e (setq l_s (cons (cons lay l_sta) l_s))
2 U$ A" }' g5 [3 W1 j, z ); y6 ^9 b' H) L4 t( F7 b4 z
( (= "No" (cdr x)): n, }* ?7 p% \, ?- h; Y
(setq l_s (subst (cons lay l_sta) x l_s))* o, D* o2 P/ w; |4 Q; v/ H$ ^8 N
)
/ E- x2 U/ M; Y5 q( b ( T l_s)" E4 M% I$ v/ G$ p
); cond) y! ^8 D N6 o: d* [
); lambda x0 u- L; k9 S: W! O9 p6 _% V
(assoc lay l_s)( }# v; F5 v' H Y# e
)
/ h0 {2 {: j; Y, W( L1 C, c5 c6 _! }% g. Z3 l) [
); defun chklay
3 x/ o/ H, r! I0 V6 D6 Z' I5 ~+ v {8 a$ j3 `! z$ P
;----------------------------------------------------------------------------
# a: i- A. x# @; L9 m$ g) Y" P; Restore layer status according to association list l_s
( X. m7 B l9 q* I;----------------------------------------------------------------------------8 F+ f: f+ G; f( a8 m$ z7 c! X
(defun layres()
" U( K% g U* |' I5 T8 E6 C (setvar "CLAYER" (car (last l_s))) H3 n! q! p! z9 k* ?
(repeat (length l_s)2 m' p) k* N( h
( (lambda(x) K$ a" n8 ]( p& I2 O7 ~3 J/ v
(if (numberp (cdr x))& N. W( i# m( U& z
(progn 7 Z5 P9 Y& v6 Z
(if (= 4 (logand 4 (cdr x)))
- Y1 j7 _" g* d8 ]: ^/ K5 j6 n (command "_.layer" "lo" (car x) "")! n1 b( S4 h9 F" h7 g
); if
2 T2 N3 h% A7 Q; L5 J! Y' P) F1 E5 F B (if (= 1 (logand 1 (cdr x)))
" ~/ I& K# C, a( i; X0 j (command "_.layer" "f" (car x) "")2 V. K, T- l' V4 I& y& ~! K$ y
); if
) n+ `& ]8 {- ]9 k8 o8 L" z ); progn
1 q/ i5 L4 H* }+ M! P, a+ z" | ); if 8 d6 K) ~5 ~# d" v
); lambda" ?0 h/ P$ ~+ {% s6 m
(car l_s)
( r! J' P% H' w% x3 X; w4 U: D( P' Y$ J )/ w- C/ S, P+ b& d1 B
(setq l_s (cdr l_s))+ }" U8 O) y6 u* I0 C; F* y9 p( h5 c
); repeat1 K5 e N9 W1 p9 T) P2 F
); layres
. _% K$ w; G7 B, C( \* G Y: e2 Z7 A+ F
;----------------------------------------------------------------------------! M/ O) m7 W3 {6 d& G2 p2 x4 M
; Get DXF codes
; G K4 Y1 \$ j;----------------------------------------------------------------------------* g6 u6 B& H3 w3 |1 N
(defun entgetf (index ent) g7 P. }3 U; Y2 l. W2 ]3 Y( `
((lambda (e)/ F5 R& h7 C9 T8 h
(mapcar '(lambda (x)7 }. m' `9 u% K" Z
(cdr (assoc x e))
1 E# W4 b4 @. Q+ b* z; Z8 I n0 d ); lambda
! [; }: l# v: C/ d3 f index) ; internal lambda function
* T3 `1 N/ F: A0 _$ j* | ); lambda
0 s0 H( T, f; E (entget ent) . i- n% S3 X0 r3 {( B
)
( X9 U* a( b; @. Z; c); defun entgetf% {# m. s/ V* Q0 b+ a
) N0 A5 [- H7 m;----------------------------------------------------------------------------2 ~9 b( K/ L7 |
; Save UNDO status1 I% p' e) G. I2 a1 e. \9 s
;----------------------------------------------------------------------------
% T8 @3 ?; D1 u. l(defun undo_init (/ cmdecho undo_ctl)
' g9 d! i0 E: v7 D8 a5 a# A (setq cmdecho (getvar "CMDECHO") undo_ctl (getvar "UNDOCTL")) ; Save the value! S$ I- F3 y6 S, m
(setvar "CMDECHO" 0). c; ~& p% Y$ Q* Y* y! b) D
7 x; B6 K. K( z2 Y& ` (if (equal 0 undo_ctl) ; Make sure undo is fully enable- o" S6 g& v2 K1 n1 n9 t
(command "_.undo" "_all")- t' a% S/ _6 U0 `; M k8 ^
(command "_.undo" "_control" "_all")
]) o6 K* o9 a$ w# y! R) j. d )
; w+ ?! A5 m {- d( y) N6 {! o$ s& U$ U& f& {7 r! [+ D
(if (equal 4 (logand 4 (getvar "UNDOCTL"))) ; Ensure undo auto is off9 N% X: N$ a. _( C/ x% p$ j
(command "_.undo" "_auto" "_off")
$ r2 {; D- d! F# ? )
6 @* w* J+ X/ Z8 Y4 S1 q: U+ g
7 l. P( F( C8 X- \) R (while (equal 8 (logand 8 (getvar "UNDOCTL"))) ; Place an end mark here
- _, E9 z* A* g. L" l (command "_.undo" "_end")
. b# y T) y$ X4 a! B )# l+ L3 n- Z% c9 _$ \3 r# z, T r
$ W7 o" a0 W7 c% S: A
(while (not (equal 8 (logand 8 (getvar "UNDOCTL"))))
7 Z/ C! H- S+ f, @& Z6 h (command "_.undo" "_group")
. u6 W; k5 {7 A7 u )
; o. z( P9 z4 O0 \: x. y5 W* c- o: U. o0 a- I
(setvar "CMDECHO" cmdecho)
" r1 W. O& D* L. T# Q: F undo_ctl+ p4 ?% X6 g1 b% m- V" `% z
); defun undo_init
$ o7 v2 g3 r& {( j( q6 s: d1 a4 O8 l* ~8 r. O+ i
;----------------------------------------------------------------------------& V) ?3 M$ z& E' S( {
; Restore UNDO status
" ~ k9 b. u! y! y;----------------------------------------------------------------------------, t* A4 s9 P; v% d2 |8 W
(defun undo_restore (/ cmdecho)
; E- q2 U+ C+ d! q (if old_undoctl
( C; q( x# ]8 M( a (progn
+ B% c8 u1 G3 L% R- w. g) D (setq cmdecho (getvar "CMDECHO"))) V8 A0 a$ z: a2 d J+ Y& r
(setvar "CMDECHO" 0)
; I/ ^6 n+ |" Z8 x5 w' m7 W* T
! ?& P$ p* j4 _& Z (if (equal 0 (getvar "UNDOCTL")) (command "_.undo" "_all"))
3 g J: n2 k$ M (while (equal 8 (logand 8 (getvar "UNDOCTL")))
# o. o) o% a: d4 \/ S1 |! _ (command "_.undo" "_end")
, U# l8 z j5 n$ s; c" Z ); while$ o- b8 E7 R5 v& F
# L% O' H' j- O4 P4 x/ Q- J
0 z% ?& B, s% ?; u/ u5 _ (if (not (equal old_undoctl (getvar "UNDOCTL")))
. \ o" V( U" N6 T. r (progn1 a& n6 l5 a* m1 P
(cond. z' C4 Y0 o! n5 x% U
((equal 0 old_undoctl)) l* u- L% h( q+ @
(command "_.undo" "_control" "_none")7 o+ t* G& x$ S9 U0 W
)5 D& j. R/ x4 q' A$ X$ s6 H/ W! I
((equal 2 (logand 2 old_undoctl))- l0 D. T7 ?" x( z
(command "_.undo" "_control" "_one")
. Q$ W* k2 d% i1 b5 _& d )
1 b3 N+ U/ t+ Z: V) l5 z- v )$ p" `) Y8 b) F. u: c
(if (equal 4 (logand 4 old_undoctl))- @$ q5 n- M( ^! o: [& @; Z
(command "_.undo" "_auto" "_on")6 S1 v- p, c4 E% `1 h
(command "_.undo" "_auto" "_off")1 P) {% J& F/ i0 h# G( J
)0 F' ]! T) T! J. v6 `3 b9 p0 m
)
6 W) g" C# `1 X3 Y )
0 O0 N# n$ @7 U) z- |: K (setq old_undoctl nil)+ P! ]" i( H% Q/ L4 d& G
(setvar "CMDECHO" cmdecho)9 N4 O ~8 T( G/ P4 b8 P
)
6 _4 Q( H r" F! w$ p% T8 v$ i )7 y. h* F. N$ T4 e# r" A
); defun undo_restore
3 [7 M, X: p: L S* s- r1 B, ~6 h- H% a) w- W1 L; K) z
;----------------------------------------------------------------------------. K$ U7 N% v3 z1 L4 \
; Save variables* N) Q! ]$ k9 P! b2 }. l
;----------------------------------------------------------------------------! z& F/ e4 m# q3 M; K2 p9 \ F
(defun var_save (a): q. @9 s5 {. ?/ b8 k( q
(setq m_lst '())6 h% _$ g% M- D# x) p
(repeat (length a)
8 s# D- M8 i; e1 ~3 J! k0 m+ l7 m (setq m_lst (append m_lst (list (list (car a) (getvar (car a)))))); G# h$ s$ I( C J! C* t8 N! r
(setq a (cdr a))5 ^! s0 X$ Z8 M: V
)
/ @1 j) b! s2 l. L9 u); defun var_save
+ w# R! `5 g% l1 E+ B& }2 `$ _6 Q9 T* C. J9 W4 J" G0 G
;----------------------------------------------------------------------------
8 D# S( ^8 L4 a! Z% b; Set variables9 g8 f$ Q. H, i" F3 [5 B, P+ P# t
;----------------------------------------------------------------------------$ L# e" `# z# L+ T" u
(defun var_set (m_lst)
0 t5 a8 V( {, { {, G (repeat (length m_lst)
/ t% s/ c0 |, _$ G( G- w2 K (setvar (caar m_lst) (cadar m_lst))$ V( ^5 B$ o5 z; v6 @- E8 E
(setq m_lst (cdr m_lst))4 D, Y6 K) N* `2 @& w! W
)
% y% S! b. w: `% r2 N! z); defun var_set
1 A' L! H/ A' o% j) }! S
, y# a: _4 m2 Z- R2 U; I; [0 F;----------------------------------------------------------------------------
; U5 F) O, B1 J7 b* S/ G; Restore variables1 o2 g: M0 A9 x" E0 z
;----------------------------------------------------------------------------6 u' {7 i( ~0 J$ `) S
(defun var_restore ()
) B8 L8 L$ v8 a* r$ A (repeat (length m_lst)
- X8 c. [% z* G8 W4 `' Y- O# d! L$ L (setvar (caar m_lst) (cadar m_lst))
% t' B+ E$ x+ e' s5 f) Q (setq m_lst (cdr m_lst))
$ M* a; ^% b V/ r )
( V# [- u, a4 E); defun var_restore
8 a3 }" t7 k7 s" ?1 V, g6 M9 h/ W& F. b1 G7 Z6 W2 I
;----------------------------------------------------------------------------
6 z( W! V# {5 f% v/ S# w; Initialize routine : {- h+ n6 o3 ~5 \
;----------------------------------------------------------------------------
; q) L H8 p7 v& t" y2 |$ O! B! |(defun err_init(e_lst u_enable add_fun) x k( U' Q6 E8 J" y
(if err_alive (err_restore)) ; To avoid nested call
% U: _/ b$ S g1 g/ p+ z( V (setq err_alive T)
, p8 G- y M; l7 h (var_save e_lst) ; Save the modes
" E2 i% h0 s1 }0 S) J- m (if u_enable (setq old_undoctl (undo_init))) ; Initialize UNDO status1 b: N& }: Y8 r" Z+ R. M3 V
(setq err_old *error* *error* err_main) ; Save the handle of *error*
' V* `6 ~$ R, x: m# v (if add_fun ; Add the user cleaner1 k4 G* h. T) r+ n; ` ?; M8 [
(setq *error* (append (reverse (cdr (reverse *error*)))$ K3 `* [: d1 @) c, g4 M% S
(list add_fun (last *error*))
' W; I. ]1 _) w% j, s- W ); append
- I5 q' h C2 a3 \ )
, ]; U$ [4 Q% @) i# r )
9 u( q2 D+ e" c+ z* V6 s; U* J6 l9 l); defun err_init |
|