|

楼主 |
发表于 2007-3-13 08:33
|
显示全部楼层
undo.lsp
;;; UNDO.LSP made by piggy. U' W' a1 ?8 h' Y( _) B- C
;;;8 m+ T+ ?6 e" X% q
;;; DESCRIPTION( ^3 h9 A4 V% ~! t; T- Y
;;; These are general error routines which can be called by . b8 d; T: M+ ?
;;; other routine. See AutoCAD 14 ac_bonus.lsp for reference. t# M1 p1 [; C3 ?0 Q
;;;
- M: S; w" C2 D; @( h0 M1 D" v;;; SUBROUTINE INCLUDED IN THIS FILE! C/ T- ?7 n/ `
;;; UNDO_INIT. L Z6 j% \9 X( q K- G! ]6 J" \
;;; UNDO_RESTORE
1 `# K$ M' e3 B9 a;;; VAR_SAVE . O2 l; y8 c) R# I e2 C
;;; VAR_SET % }2 K) f4 l% F" H0 A: ~
;;; VAR_RESTORE7 n& w- K k/ @; J
;;; ERR_INIT
, b( a% ~- P" E3 H;;; ERR_MAIN
3 H0 C4 X2 m7 y$ O;;; ERR_RESTORE6 n" a/ P7 I' ]3 f, r+ V+ G3 m) V" V
;;;
9 w V: f: W! z;;; DATE: 10/17/98; 03/31/991 m6 ^9 t; ^4 r7 ~3 t8 o
;;;
: S2 H9 C- d: U, B; r;;; HISTORY:
* @7 _9 ]; P' B# F3 |% ]# w2 o;;; Add routine of mod_att
* j) M8 G& F! {2 M;;;
. t2 S1 v, L& ~- l;;; USING METHOD9 k" F/ l% P: P1 L/ ~5 y6 m7 P
;;; ERR_INIT:) V* N5 p" E8 `
;;; This routine initialzes the error handler. It should be called as:
( h9 r4 ^) w$ ]. O3 r L;;;* R& X8 \/ \: }5 O1 B( j& o+ ]9 `. c- f
;;; (if (and (not undo_init)
% |3 n6 `: q0 F; R( N) l& a6 L;;; (equal -1 (load "undo.lsp" -1))
7 Q# f3 I$ ?3 u) p7 G;;; );and2 C- G* q5 Z( i! p- h1 C4 ]
;;; (progn (alert "Error:\n Cannot find UNDO.LSP.")(exit))
6 Q0 N- A1 f1 i& D8 Z1 C& }- b" v4 l;;; ); if
) }1 m: M) S5 s& d4 n# U* X;;;
. z. ^! U% A* b; C- [;;; ARGUMENTS:! S2 ?6 y6 F$ c& |4 u! [
;;; err_init Takes 3 arguments.
. r0 w( W- ]/ r, U2 I;;; 1. - The first element of the argument:
; \, P Z' p7 g2 X8 Q;;; This is a list of system variables paired with
/ M7 C6 D0 j* C" l# z& u* r3 |;;; the values you want to set them to. i.e. '("CMDECHO" "ATTMODE")
! k5 M: y, _1 b2 p% P$ X, Z+ ^;;; 2. - The second element is a flag1 P2 q3 n- ^0 c. w
;;; If it is true, then in the event of an error
. a# E* P; b& F0 l! `4 R;;; the custom *error* routine will utilize UNDO 6 {7 ^, C/ u- \. Y$ O& d
;;; as a cleanup mechanism.
! M7 [' [% K6 m$ W W;;; 3. - The third element is a quoted function call.
- Y: |! y$ r2 J, W4 O# U, L9 u;;; You pass a quoted call to the function you' k' }+ E7 @+ V! o; T6 e
;;; wish to execute at the end of nomal routine if an error occurs. * |9 H4 Q/ H7 H: J. `
;;; i.e. '(my_special_stuff arg1 arg2...)
! y/ ~: w! o2 z4 f;;; Use this arg if you want to do some specialized clean up 0 [( x+ n! a8 `
;;; things that are not already done by the standard bonus_error
) ~. T, a: D+ J5 P: |( a* | u;;; function.
2 C1 `/ _' ^0 k$ Z u' L;;;& T% p! K7 p" n, J' D
;;; ERR_MAIN: Body of error routine
; e7 Y3 { b |8 O+ r" ~4 ^" \;;;
! M! V3 X8 ~4 w/ S5 h;;; ERR_RESTORE: This routine should be called at the end of command to
9 ?$ f$ y- r2 {* _' H# d;;; restore the VARIABLES, UNDO & *error*.
6 d2 L/ ?0 y" ?3 b) p* o/ b;;;
: ~/ g& B* [( b/ O" [8 m;;; UNDO_INIT: Initialize the UNDO status0 x' u$ R/ ?+ I6 n5 e
;;;' v% ]4 c8 y- [" Z3 e0 K1 `
;;; UNDO_RESTORE: Restore the UNDO status
, N9 m0 G7 e9 c- h$ [5 f# W5 ^9 @- r: Q;;;
8 |" z! Z- y- \$ d;;; VAR_SAVE: Save the variables. the argument is like '("CMDECHO" "ATTMODE")
4 G& c$ N3 A" b& U0 P, I;;;
s& W- Z7 U( W: x! o, n;;; UNDO_set: Set the variables. the argument is like/ c7 v( u8 C8 a7 R+ z
;;; '(("CMDECHO" 0) ( "ATTMODE" 0))
- m! \; z+ _, e( R# e! h+ D;;;7 h H& u4 V; O! I1 Z0 ?' u/ c
;;; UNDO_RESTORE: Restore the variables7 W+ Q7 ^$ d7 R( Q1 t1 W% @0 i O
;;;4 }2 y( I3 A6 L( N# r. F, ? ~% Q2 x
;----------------------------------------------------------------------------7 k/ ?9 h: J/ c$ f2 U
; GLOBALS: R1 R; b0 z- g6 J8 D
; old_undoctl -- old status of UNDO (voided by UNDO_RESTORE)& t- M5 G( X- ?$ Y
; m_lst -- list of variables (voided by VAR_RESTORE)
" L+ q/ J7 @$ L" `; err_alive -- indicate error routine is active (voided by ERR_MAIN or: ]4 o0 n w0 t: }, v4 ]$ l7 ^
; ERR_OLD)7 {$ m4 a. p% R
; err_old -- old handler of *error* (voided by ERR_MAIN or ERR_OLD)& o) [8 r1 T1 i) G+ S0 N+ R. m
;----------------------------------------------------------------------------4 Y4 Z/ D$ L7 y6 t
2 Z5 E8 \ o9 I1 F; K$ p;----------------------------------------------------------------------------
. M( G+ e( o: d4 M6 e$ I; Modify attributes according to entity name, attribute name, dxf_item
3 n7 a; i& y4 o1 T;----------------------------------------------------------------------------- P- r( C, Z- d0 t2 M, W6 c
(defun mod_att(ent id dxf_item)
n, x4 M7 Q0 V$ s0 }7 W, T% r: P (while (and (/= "ATTRIB" (car (entgetf '(0) ent))) (/= id (car (entgetf '(2) ent)))) m* H& y/ L8 ?$ D8 N9 Q
(setq ent (entnext ent))5 X* {9 b! y8 M3 ?: f
); while ; S; h; q# R2 C
((lambda (x)1 {* L/ Q$ ` b6 X- {' O6 ]
(mapcar '(lambda (y)5 I8 j5 F: W. ]* y7 y5 Y5 O8 P1 a
(setq x (subst y (assoc (car y) x) x)), d% `2 ~: M p& f% y; H
); lambda
+ Y. e9 Y8 K& y. ] dxf_item
& K' ~; z+ k' k( z" g7 R ); mapcar( r. O8 B% l2 m* p
(entmod x)
e3 l8 o, v8 Y( M (entupd ent)2 U* a- H* i6 }0 ~* h Y& K6 l/ c
); lambda9 X$ m0 n' c# S5 |, A
(entget ent)0 W/ J0 ?- Q' U( Z
)) N" I1 Z" j* I) s9 A9 f" h
); defun mod_att/ p( v- D1 V4 a1 L s
$ J/ y3 a- @7 a;----------------------------------------------------------------------------; j" d v4 V- s7 j. w
; Check layer status, return a association list which contains layer information
5 o% o M" K$ h+ J; \0 F;----------------------------------------------------------------------------% O3 Q5 c3 ~, y$ g5 X' S7 Y
(defun laychk(lay / l_sta)9 `' K" k6 W1 n4 P: k) k
( (lambda (x)
5 a8 R4 F" M$ f w ~ (if (not l_s)3 G* O* U) O; v: f
(setq l_s (list (cons x (logand 5 (cdr (assoc 70 (tblsearch "LAYER" x)))))))7 K! e: z, a3 g4 ^ A9 W' e; y
); if7 t( s% n# ]! C2 s- f" N" x
); lambda
5 z, q k$ z/ T (getvar "CLAYER")
7 i1 M( E7 O: y! x )( s X/ ]( z& K8 q0 D' M8 j M
(if (not (tblsearch "LAYER" lay))$ L( L9 p$ H) p6 n0 |7 i
(progn ( o* N- G' [( T+ m3 ^6 _
(initget "Yes No")) H" ?' C$ p, W& o
(if (= (setq l_sta (getkword "\n图层不存在,是否建立该图层 ?\(Y/N\)")) "Yes")1 r: i# N4 v0 T* R- f$ H' W9 u& d1 n! h6 ^
(progn1 m0 M7 e. x3 D$ Z2 H
(command "_.layer" "n" lay ""); l8 e7 T4 U! T4 J& Z+ w7 n3 t
(setq l_sta 0)
7 u+ R" J5 B4 [ ); progn
9 v- U3 O+ ]: \0 _ ); if
2 J1 ?9 E9 _7 }& E1 \5 } ); progn' W6 k$ G& C9 [; {0 I3 Z
(progn
% b* K! A+ M5 L8 l. r9 U v0 ? (setq l_sta (logand 5 (cdr (assoc 70 (tblsearch "LAYER" lay)))))
. U! K( x1 A/ I$ [ (if (= 1 (logand 1 l_sta))
# b6 m' Z* i3 W4 z8 T% E. _ (progn8 O8 W. S" F; [/ {7 N, R. d/ u* ]
(initget "Yes No")
$ E G& l: W( X5 `+ z+ h4 l (if (= (getkword "\n该图层被冻结,是否解冻 ?\(Y/N\)") "Yes")/ c3 K" W/ F% o! v- G; \+ o4 y. T
(command "_.layer" "t" lay "")- _" w* q4 k4 B
(setq l_sta "No")
/ \) h. z9 @/ n7 o, a5 R ); if% g( Z( X1 v9 z4 V9 V0 ?
); progn+ ~9 `1 N( j) g9 ~- U7 C
); if! X2 [; w! d! w A8 C* l# g. q
(if (numberp l_sta)
( X9 c7 H- F4 g) P (if (= 4 (logand 4 l_sta)) (command "_.layer" "u" lay "")); if
) `) N$ `* S" t% C- {: Y6 ? ); if% ^+ r# g8 U8 \( E
); progn% R. ]8 b' U) ~+ r F% h3 P
); if
9 R3 p- e! m- Z' g: | ( (lambda (x)8 T; {5 Q2 ^( G* _
(cond
2 s, u9 ?2 P- |. }9 r ( (not x)
6 ]1 C8 b1 X* k" F3 [) q( y (setq l_s (cons (cons lay l_sta) l_s))
z) n% f8 {, M+ i9 E9 M. {/ ? )) d( U& o7 I% X' F Q' R
( (= "No" (cdr x))
' H3 m, }. H9 Z$ Q7 b (setq l_s (subst (cons lay l_sta) x l_s))9 B' L, C, O" F! c; C' n9 z0 ^
)) {; r. S l# o- Q7 a9 {, L" q
( T l_s)8 T+ ?# {" \ e& A4 n1 p
); cond% Y% D# c" B% @% h% v6 I: z
); lambda9 s" I2 r& K4 i6 G- C
(assoc lay l_s)
( j$ L! Q1 M% v3 n1 a' M7 S- G )2 O% [. L# l" ~
8 S7 @; |) O, _
); defun chklay2 F3 W8 q( m$ c0 i. K# |# [( U9 M
) B# }2 W* ?2 n2 |) Z
;----------------------------------------------------------------------------7 d; h$ L* Y( y. `/ N
; Restore layer status according to association list l_s
/ F- I" J, ?, @$ l: N* W+ L- h;----------------------------------------------------------------------------
0 d& n2 H& r5 d! f(defun layres()* ^ l3 ^* t( t7 ]+ Q; m4 O" @
(setvar "CLAYER" (car (last l_s)))
5 v, l8 _- U) f5 k) z (repeat (length l_s)
& q- B$ B7 |4 ~ ( (lambda(x)
$ p) z! U9 Z/ I& a; O5 @8 U (if (numberp (cdr x))
$ f; _3 s' q# {, o8 a (progn
, {7 U# M. |/ h/ \ (if (= 4 (logand 4 (cdr x)))* e. q) s9 L. D% J+ R
(command "_.layer" "lo" (car x) "")6 f% X$ h% O6 A% D# ^. W2 ?
); if; h' x# e0 H: ^; X1 N
(if (= 1 (logand 1 (cdr x)))* W8 r% T. W! Y1 n: J- D9 R
(command "_.layer" "f" (car x) "")
7 L% n4 S+ P" _6 R ]7 A. D# K- a ); if0 ]6 q; y: c" H# F x* K) V
); progn& d" S$ ]" w8 J0 `8 J
); if
7 O+ \5 W2 H: c" { ); lambda
; c3 w3 L6 ^, H- {1 j) [. W0 i (car l_s)* ]- ^0 Z3 J% w1 l- i; j3 z
)& o+ A0 J) o: ]/ T5 j2 H
(setq l_s (cdr l_s)): s" M8 g9 r1 C, l) K$ ~1 c8 K
); repeat
" F6 }) `* n% z); layres4 ?7 o ~0 r3 O# y
* d, U2 e, ]6 ^;----------------------------------------------------------------------------
4 d4 U! r2 C+ q# r& a+ A" _% T; Get DXF codes8 t& l- q' @" F
;----------------------------------------------------------------------------+ n% t3 u" c. e7 L
(defun entgetf (index ent), f) F8 P) r7 D8 j. m4 I. P
((lambda (e)
1 g: k7 B" X2 F7 S (mapcar '(lambda (x)
$ E6 M) F2 A Q1 h J9 D (cdr (assoc x e))% f- p+ u/ k" g7 C8 W
); lambda5 |2 b* ^$ t" U/ d6 A5 t
index) ; internal lambda function
2 B: s- O* m* |) B6 I$ y- P ); lambda2 w2 B% t: D0 ]2 a0 H( ?' Y
(entget ent)
; ]3 A# w9 M1 _1 r ). e) s" ]& B; c2 n
); defun entgetf
% Z, L# x! L; x# L; U/ M1 R: S9 R! S% Z; D* Z% U1 g0 [
;----------------------------------------------------------------------------; N1 [: i% z; K
; Save UNDO status6 J' \: U1 s' O* E
;----------------------------------------------------------------------------) q U2 ?9 p5 D' R
(defun undo_init (/ cmdecho undo_ctl)
! T6 H* ^1 Q1 X, z6 y0 i% Q M (setq cmdecho (getvar "CMDECHO") undo_ctl (getvar "UNDOCTL")) ; Save the value
; b% u, O7 H& g* X( o (setvar "CMDECHO" 0)
4 o7 p* b* t, K# k+ O$ d& x: L. Y+ ]' M2 P! u8 N5 ]5 C, b$ w
(if (equal 0 undo_ctl) ; Make sure undo is fully enable$ N# M. |- l) W% i3 i( v
(command "_.undo" "_all")
/ `6 }. I3 F8 i$ m! K& j4 f (command "_.undo" "_control" "_all")1 ^4 j$ s! q* w9 x8 A: J: y5 t- V8 _
)
- N" ]) p2 | ^1 n: W4 u# y: k! H! N
(if (equal 4 (logand 4 (getvar "UNDOCTL"))) ; Ensure undo auto is off( H! T1 r9 i. X1 e1 n9 ?8 M
(command "_.undo" "_auto" "_off")
9 ` Z5 D3 _: E8 B )' I) @8 X4 i0 T$ C* d! q
" T! {8 |) [9 k& x% x3 O# F
(while (equal 8 (logand 8 (getvar "UNDOCTL"))) ; Place an end mark here
" f: f, h4 T7 L, o6 h ^0 h (command "_.undo" "_end")6 k9 ], H" L# e* h7 j. f
)$ i+ X0 W0 ^6 O! q: n/ t
2 N+ ~& ^" T9 g M
(while (not (equal 8 (logand 8 (getvar "UNDOCTL"))))
' C* y }% h( n& Y8 d& b$ o (command "_.undo" "_group")% p9 Q$ K+ T; m7 J
)
/ v6 x5 D$ Q* n9 j$ r$ v
; d# N9 N# C- T1 T# M$ J1 y5 K2 @ (setvar "CMDECHO" cmdecho)8 m9 h0 X2 K! J; t, H) ^! Y4 E
undo_ctl: f, H' U, @6 v! b3 N6 u" C6 y
); defun undo_init
/ u& v! S- n9 t A' K" H. n# o5 a0 Q9 M4 o
;----------------------------------------------------------------------------* O O4 A( i+ h8 v* n8 n
; Restore UNDO status H I: J0 }- r1 X
;----------------------------------------------------------------------------* v L9 [5 L& O. w: m& ^/ C2 B
(defun undo_restore (/ cmdecho)
: A# v$ f& Q% T1 l (if old_undoctl; f$ @, Z4 b4 _+ P1 ?* M6 `
(progn
& ^/ U* @2 I; d, |+ k, x4 e (setq cmdecho (getvar "CMDECHO"))
3 u- }" g e# h- } (setvar "CMDECHO" 0)# a7 b, X1 h, c* \; L
1 q! b1 m8 s+ K- k5 {( S d (if (equal 0 (getvar "UNDOCTL")) (command "_.undo" "_all")) " n2 V0 F& V G+ L" z! |
(while (equal 8 (logand 8 (getvar "UNDOCTL"))) b/ s4 [1 A$ Z/ L' {5 S
(command "_.undo" "_end")
- j! L( ~7 V7 e: |7 q; J, E/ M ); while
% T9 c: R/ B8 U+ V4 s6 ^+ m6 z7 H: W( c2 D, y8 Z7 M k4 n
# v8 ~# C9 p. T9 D: n+ ]9 E" T (if (not (equal old_undoctl (getvar "UNDOCTL")))1 C; F% j* g% Y" q: \2 [
(progn8 h9 q' }' C$ _. Z3 f/ ~4 I
(cond
2 O9 R8 x, U' D9 d7 F ((equal 0 old_undoctl)4 ]1 C9 ~1 ~. o; Z
(command "_.undo" "_control" "_none")* C% G9 \5 G- ]: y; A! A( D( `
)
% \, W7 |( K: N& [' y: t( R7 f" c ((equal 2 (logand 2 old_undoctl))
) ~. a8 e4 j. W" @ (command "_.undo" "_control" "_one")
% ^# w! f4 A- @+ |. ?3 N; J )
; o8 E* ` m0 |' d5 B5 y8 l )& Y' C" k$ C) W5 s' b* N
(if (equal 4 (logand 4 old_undoctl))
! ^) @0 q, n" v (command "_.undo" "_auto" "_on")
! k3 j) U) |# \7 F" z (command "_.undo" "_auto" "_off")
% {% X+ y! B Y! } )) w+ f* k8 w0 d3 n# R/ W) _2 m
)
) }8 Z: d. e$ q; \! ^* e7 A )1 h# h+ q# k; ?8 t5 ^
(setq old_undoctl nil)
, ^4 D4 f9 ]' [! q (setvar "CMDECHO" cmdecho)
0 {7 p4 h8 G$ k9 K. N- r1 _+ V3 _ )
* ]; g" ^9 ? _1 [ )
: i$ {- p; `5 `9 O) X. ^' f* v); defun undo_restore S* U3 M3 d- c* H8 q1 x' `
# c o) e/ J8 ]+ j9 \8 I+ \;----------------------------------------------------------------------------2 I% ~0 k( M4 C, S. U$ B5 g
; Save variables
, o% L! m/ k+ V1 ?. P. e o; r3 U2 H;----------------------------------------------------------------------------
; Y8 F- [/ N2 A(defun var_save (a): |( e3 B5 Y6 V" e% p* }
(setq m_lst '())
% d7 x7 B X& E' l2 U/ E (repeat (length a). x* I- H7 B0 u- f
(setq m_lst (append m_lst (list (list (car a) (getvar (car a))))))
' T9 ^9 R, }; Q5 x1 t- I (setq a (cdr a))" q9 z- B c* A+ E) s* H" u
)
. J, C7 E% u U$ d- K k% `& K); defun var_save
- N! m; c( Y0 V C% Y$ p# o! d$ M' H' B( G' X4 W9 |
;----------------------------------------------------------------------------( i3 u; d- h4 j+ g* m
; Set variables H9 i- Q s8 W" K" R
;----------------------------------------------------------------------------* h+ G" l# q7 K; c/ S& ^$ o5 f% B. j
(defun var_set (m_lst)) L: @2 h- ^! F
(repeat (length m_lst)
: b( s9 A% p' R (setvar (caar m_lst) (cadar m_lst))/ @3 [ z* @' K6 O3 a
(setq m_lst (cdr m_lst))
5 F1 c8 _7 l/ v; V& y1 C ) x& g a" S9 @! r) A
); defun var_set
8 J- _$ d1 j5 x. f
@: G: Z+ Z& p& T1 c: h/ Y;----------------------------------------------------------------------------
0 [9 h- H ^& c' B. l; Restore variables, _* P9 m; o+ y! e- Q# m, e
;----------------------------------------------------------------------------$ I, G3 b& n) v) m
(defun var_restore ()2 o& L6 I8 Y& R8 T
(repeat (length m_lst)( m4 u z5 S* M
(setvar (caar m_lst) (cadar m_lst))
( E. J9 o. z i# {, T1 h (setq m_lst (cdr m_lst))
# r% r6 K8 ~1 I! q' A; _ )
1 S/ V8 ^" n" L/ c! y1 d); defun var_restore/ @# S/ [0 R; A5 [& r/ q
6 N# {( b4 D+ b/ q
;----------------------------------------------------------------------------* ?) c$ \! o/ V, C0 @! a
; Initialize routine
) f8 y; B( |8 ]" H( }+ v4 e3 _;----------------------------------------------------------------------------
) t5 Z& g! c/ G- w! j(defun err_init(e_lst u_enable add_fun)
. w7 r7 t* p* S+ i7 n/ B. x- @ (if err_alive (err_restore)) ; To avoid nested call2 R( }3 G; U3 }8 |5 J) a" |
(setq err_alive T)
@, t1 k) d# ?7 O) I (var_save e_lst) ; Save the modes' l+ W4 R( F# W1 M. G
(if u_enable (setq old_undoctl (undo_init))) ; Initialize UNDO status' n a/ Y$ |( B/ r# s
(setq err_old *error* *error* err_main) ; Save the handle of *error*" c( x! _* N$ G+ b: K; P
(if add_fun ; Add the user cleaner4 W& p* a; i+ a- f( R/ D0 ]9 W
(setq *error* (append (reverse (cdr (reverse *error*)))
: {' ~: J1 _2 i( W- |8 v4 p: T (list add_fun (last *error*))& \/ W: B0 r, Y! A7 r) F
); append
0 Y3 Q1 Y: q) _: w0 m: a )6 R9 V2 y+ T( t) g) h
)
) T8 w: K9 t4 y% S/ z5 |) [); defun err_init |
|