|
|

楼主 |
发表于 2007-3-13 08:33
|
显示全部楼层
undo.lsp
;;; UNDO.LSP made by piggy5 z) R5 l! s8 z: r5 T3 @* Z+ r
;;;
! O! p9 I3 J# k3 r;;; DESCRIPTION
- U) P# A, H7 s: M;;; These are general error routines which can be called by 7 J% u6 X& ^. Q1 y
;;; other routine. See AutoCAD 14 ac_bonus.lsp for reference% S) i0 K" o: `; [
;;;$ ?: N8 t6 d2 l; w# u
;;; SUBROUTINE INCLUDED IN THIS FILE6 K. t, l' ]3 r b7 B% k
;;; UNDO_INIT: r$ j$ C" R% {4 ^0 B8 m
;;; UNDO_RESTORE
5 m! ]- n0 m4 }$ b$ E+ @;;; VAR_SAVE
$ ?' q( m8 e% g0 M. t;;; VAR_SET % ^, P0 u+ `8 ? z& L6 G& ?
;;; VAR_RESTORE' Q3 I" N7 r" Y l& i
;;; ERR_INIT
9 C) y$ \. A5 K;;; ERR_MAIN
, J* u5 D5 a& f. q8 x;;; ERR_RESTORE# z1 w4 m* I- |# H0 ]. `
;;;
4 l* `, s+ b |: L7 h1 p5 N7 H;;; DATE: 10/17/98; 03/31/99
# @0 a6 S4 a( t2 s- t% z2 d;;;3 ?# k+ j+ r/ L) [+ X" W) C
;;; HISTORY:
+ ], u/ F7 H& T! i) O2 O;;; Add routine of mod_att3 }, @$ U5 q% }3 Z+ `, `
;;;
& M' m2 t L7 j# W6 @;;; USING METHOD9 e K0 F; j8 v7 g# U
;;; ERR_INIT:# N! _5 w! e' c' r
;;; This routine initialzes the error handler. It should be called as:
9 W9 k1 C8 o l, A7 w. h% U8 @;;;
: K. O* S' I( q;;; (if (and (not undo_init)0 w7 ^9 [1 o, e) W' W! T: `9 k
;;; (equal -1 (load "undo.lsp" -1))
3 Q, c8 J- r5 h9 o& C' v;;; );and$ m2 K& y# B8 ?8 r) W
;;; (progn (alert "Error:\n Cannot find UNDO.LSP.")(exit))7 M. `0 O9 Q" |4 d3 R
;;; ); if
! U# u9 ?) A. c% W+ W;;;
) D6 N' t4 G* N6 M- |' Z; H* k;;; ARGUMENTS:
' r5 |7 q1 Z6 S1 {2 ]* F, o;;; err_init Takes 3 arguments.
3 a- n3 o$ u+ z/ _0 z;;; 1. - The first element of the argument:
3 v8 i2 j f, Q, [5 F- W;;; This is a list of system variables paired with
1 U7 K1 y( U- E8 S;;; the values you want to set them to. i.e. '("CMDECHO" "ATTMODE")
; }2 g! f8 F: g;;; 2. - The second element is a flag0 ], j6 Y" [6 k0 n1 Q
;;; If it is true, then in the event of an error / G5 M/ i+ ]* r) ~, _
;;; the custom *error* routine will utilize UNDO " [: X9 B8 X( ]3 D; H7 T9 w
;;; as a cleanup mechanism.6 s! E3 t# w: R2 b1 s
;;; 3. - The third element is a quoted function call.* O1 F2 f" M: }" Q; p' z6 t/ B
;;; You pass a quoted call to the function you& ?9 g& B& |+ Q5 ?
;;; wish to execute at the end of nomal routine if an error occurs. : w" m" w2 e' c9 y
;;; i.e. '(my_special_stuff arg1 arg2...)
5 ^' R, \7 T: }4 S;;; Use this arg if you want to do some specialized clean up
+ Q( |' ]% Q& g: y3 L7 c;;; things that are not already done by the standard bonus_error 8 ^$ }" g2 t3 @. D
;;; function.* U5 M: F, m6 Q
;;;
7 F; Z+ d- {3 t;;; ERR_MAIN: Body of error routine8 _& r2 h1 x4 t
;;;. ^. B$ _3 q0 y) ?/ P9 b, v% O
;;; ERR_RESTORE: This routine should be called at the end of command to) ^- N9 x4 n2 n4 Z/ {3 z
;;; restore the VARIABLES, UNDO & *error*.
3 T/ C& t; U% J* G1 W- R;;;1 n6 w- z! m, H h
;;; UNDO_INIT: Initialize the UNDO status
5 X0 b9 T0 d! j$ r; T4 X;;;
% `% v8 r: M8 s& S9 ]- N;;; UNDO_RESTORE: Restore the UNDO status% q5 u4 ?1 s- e) H* W% ^+ w( A
;;;& I4 ^. n3 v, Y
;;; VAR_SAVE: Save the variables. the argument is like '("CMDECHO" "ATTMODE")# T! E4 W4 D [; S0 X! P
;;;4 q: `. W9 X1 `' \, M
;;; UNDO_set: Set the variables. the argument is like; U! r9 f5 f: M
;;; '(("CMDECHO" 0) ( "ATTMODE" 0))- p8 O" z3 U& o6 W' {
;;;$ D% P3 O, c# G" Z+ V; X; i
;;; UNDO_RESTORE: Restore the variables5 ?3 C" l' e8 u" S- }# W
;;;0 D7 W. I" ?7 j3 o5 a/ e4 N3 c! ~
;----------------------------------------------------------------------------
( V# ?, a6 Z. l2 ~) I2 G& ~7 i; GLOBALS:# Z* B \( Q: [7 R" n5 b3 W
; old_undoctl -- old status of UNDO (voided by UNDO_RESTORE)# L+ z4 @" c4 U( B4 ~0 x4 \+ D& Z
; m_lst -- list of variables (voided by VAR_RESTORE)
; y. M9 j E2 O/ y- t; err_alive -- indicate error routine is active (voided by ERR_MAIN or' ]- H# ]* ]/ q
; ERR_OLD)
( m# z. I: n6 b8 T3 D; err_old -- old handler of *error* (voided by ERR_MAIN or ERR_OLD)
# J0 v8 X k6 M0 R% e* }: ~;----------------------------------------------------------------------------# N* I' ^8 F/ D4 r7 `9 f( @
8 P/ l# W2 z, r) f;----------------------------------------------------------------------------
' Z2 G) P4 S' z P( N; T1 t& e/ ~! l; Modify attributes according to entity name, attribute name, dxf_item
: L- }! r& n9 j;----------------------------------------------------------------------------0 r& z8 R5 ~" x7 i- \& _) S9 J
(defun mod_att(ent id dxf_item) + x3 F9 ~* H+ d- K" Z, y/ i8 Z
(while (and (/= "ATTRIB" (car (entgetf '(0) ent))) (/= id (car (entgetf '(2) ent))))) _2 [( Y3 q) \
(setq ent (entnext ent))5 Q1 q1 O% m5 w+ p5 D
); while
+ M, n1 {' `. I" T9 I6 C" V* g ((lambda (x)
5 a+ ?1 q" h; D$ L0 ?$ y' v (mapcar '(lambda (y)
- v" o* h1 j+ ]/ Y. \9 s2 i# y (setq x (subst y (assoc (car y) x) x))$ O( j( b/ H' B) x7 k5 \
); lambda
, Y5 N3 Q6 S" M- i7 J$ v, q dxf_item) }3 o, ^: X& s4 r4 t; P/ _! p& Q
); mapcar2 x# H$ v) N- D# e2 W/ v/ J
(entmod x) P2 ~/ m! c) ~' m/ I* N$ |! [
(entupd ent)) d9 N3 |5 A6 ~
); lambda& q+ e( |+ @! u: Q8 W# G+ Q! S
(entget ent)' ^* Q* G2 q8 ]# I, Y
)4 ^ b' k; Q* k; U9 Q0 C m6 G
); defun mod_att
2 f- q$ ~* q2 w. t- E' |
( ^7 i* t1 I8 |9 X/ o0 e" @;----------------------------------------------------------------------------
3 p/ k! y6 [- F# f: d; Check layer status, return a association list which contains layer information. n) t0 ^" \- N% j7 i/ r: d* V
;----------------------------------------------------------------------------- L. `' q5 t. G- Z- l0 ^
(defun laychk(lay / l_sta)
1 K( X/ d* ]; |7 F D3 Z5 c5 k ( (lambda (x)
5 c' C" c% }$ ~% [: U6 i (if (not l_s)
3 D+ G, Z; S1 u0 L. g" o (setq l_s (list (cons x (logand 5 (cdr (assoc 70 (tblsearch "LAYER" x)))))))
, j7 T- g! R1 t' B ); if3 e2 d) b! E3 P1 F
); lambda
6 n3 V' ^- H/ d5 @ (getvar "CLAYER"): K+ y4 E$ y+ W/ H, [5 H
)% C! a$ u' N$ J" W5 @
(if (not (tblsearch "LAYER" lay))( W+ y6 \ A! g n0 P8 D2 W
(progn , Y+ }" v* T" @$ Z ?
(initget "Yes No")
# e5 d3 a6 H1 z, E (if (= (setq l_sta (getkword "\n图层不存在,是否建立该图层 ?\(Y/N\)")) "Yes")
5 |6 Q$ g( {. V+ H$ {. C (progn
3 _% E4 T3 v5 [, S (command "_.layer" "n" lay "")( p- |# A8 y# ^/ J' i8 B! [
(setq l_sta 0)
( O( v% C, l" `# A/ k ); progn
2 K1 ^* j$ _3 ~ ); if- n2 D+ I5 }" w* f
); progn
2 D- Y4 f) p9 t, _; i4 ]' Z/ ~ (progn) w' s6 z" H/ r' k }
(setq l_sta (logand 5 (cdr (assoc 70 (tblsearch "LAYER" lay)))))
% {( p' p& N* h$ e (if (= 1 (logand 1 l_sta))' J7 S7 `- T @+ j) \* r
(progn: k/ I/ h0 P) E. ~
(initget "Yes No")
" x. `7 h P- @! ~: F (if (= (getkword "\n该图层被冻结,是否解冻 ?\(Y/N\)") "Yes")( K) K9 E+ r$ B) T% z4 a6 a
(command "_.layer" "t" lay "")
' a/ j6 v' @7 Y$ J: n$ P (setq l_sta "No")
/ n8 M: I( Y0 R2 d9 a- A1 N9 `1 [ ); if
. A* q! K* ^! f ); progn' o- {0 }' ^9 m/ b7 c7 R, U* Z
); if6 `! n* i7 H6 ^4 J# s- F
(if (numberp l_sta) 4 |- G# o1 w c+ |) C
(if (= 4 (logand 4 l_sta)) (command "_.layer" "u" lay "")); if1 x& `- A# S* h) v
); if$ e, y! }5 I9 X; U
); progn
' s3 z0 ` D' n; I' | ); if
7 k! H. t3 B6 r H! J" k* ?) m ( (lambda (x)
; \+ F$ _ U1 R3 `2 `6 j, [9 y- C' N (cond 6 S" w+ A) X# F8 |) |- l
( (not x)
8 s2 X8 Z# w6 C$ \2 h (setq l_s (cons (cons lay l_sta) l_s))
3 G/ H. j) f2 \/ G0 Z8 x9 s ), `# l. o# m; q7 s/ t$ T& C v
( (= "No" (cdr x)), G A8 G' u" h) c& O
(setq l_s (subst (cons lay l_sta) x l_s))
1 |# v9 u. O. j( N# d8 r ). i; w, W( P6 j
( T l_s)2 o9 T! E+ i+ S) i# m0 ?& p9 s$ u0 F
); cond w; C+ a5 M2 @8 h I1 t6 V
); lambda
. B% u* L+ C( W# m (assoc lay l_s)
; d3 `+ P, y1 S7 f# ` )% y/ O5 L! V$ Y3 i: s3 J, y
" A8 n7 X8 R7 K- f4 b); defun chklay
5 R' s! T6 T* X1 j
0 ~4 J) Y; c6 Y8 D. ~;----------------------------------------------------------------------------! p' S* g$ |, M/ K
; Restore layer status according to association list l_s
9 b1 k w7 e5 N. p;----------------------------------------------------------------------------
) C) v- \0 A, |- K; S(defun layres()
4 u/ \* o5 P' q8 a- B+ z. ^ (setvar "CLAYER" (car (last l_s)))
5 T, P' ~0 \9 `) d( j( o u( i3 @9 p (repeat (length l_s)
( ^8 z7 ~. v# o$ n9 J ( (lambda(x)
9 X& x0 M% k; _0 Y! J" [8 _ (if (numberp (cdr x))
! j, }1 x/ B- O1 z% C (progn
3 a' U }) m# T (if (= 4 (logand 4 (cdr x)))% \ o% J7 s; s
(command "_.layer" "lo" (car x) "")
- H" I5 i0 z' h6 ? F \' ~ ); if
_3 g& A$ @7 B, t (if (= 1 (logand 1 (cdr x)))6 }+ ~% n+ H) @6 E
(command "_.layer" "f" (car x) "")9 K; a! q3 \# Q- T( [$ c M" A4 [
); if
) M% G) F4 c, k6 H1 ]4 b' b. j ); progn/ G9 U$ K: T$ r! O' ~' T9 ?( d
); if
$ f3 l/ `+ E q( w3 V ); lambda
" @: C# t7 Q- Q/ m" `% ^( U (car l_s)$ a# C2 c, s$ W! E- j' y* c
), m" S- W' H0 h, z% C% u/ d/ q; L6 e0 p
(setq l_s (cdr l_s))- Q+ I3 b! ?+ A( }
); repeat; e# h7 _9 j$ P5 d/ E
); layres
) _ a) g+ S8 ^) B: R/ @' D& X6 c# y7 K9 f5 I2 v6 B) h0 ~
;----------------------------------------------------------------------------7 W: { F; J! `/ k% e
; Get DXF codes
6 A" Y9 ?- ~! B1 a;----------------------------------------------------------------------------
' Y" O, w7 L X1 P(defun entgetf (index ent)! c& D) k7 d( H8 @
((lambda (e)3 W' {7 c2 f5 C/ w, d: F
(mapcar '(lambda (x)
2 A/ c! |$ |0 D: k6 @7 x+ p (cdr (assoc x e))4 C9 R2 r) \) [& i$ O. q, F' s2 v
); lambda3 Z, U _: J5 U5 P. v/ V
index) ; internal lambda function" l: [# Q( _7 O! {% X T, F) d
); lambda
9 k7 g5 _* ^ N" Z& P0 Y (entget ent)
* ~ M; f+ \* u7 w )2 E8 P% ]* x: g
); defun entgetf
. }! g9 @. K+ {9 b; X* X5 D' v" Y% m: V3 M, N; h7 x6 x4 n; N
;----------------------------------------------------------------------------5 |% h) a! ]$ x7 T
; Save UNDO status) F1 L. g# A5 |, K) H: P; v# a$ n
;----------------------------------------------------------------------------+ h& o: j; ~# g0 o
(defun undo_init (/ cmdecho undo_ctl)
C" q2 z0 `4 B3 U (setq cmdecho (getvar "CMDECHO") undo_ctl (getvar "UNDOCTL")) ; Save the value
* U$ ?, W6 L m$ r5 [ (setvar "CMDECHO" 0)& q( H7 w# i- K1 t
4 m4 E; U! E Y. _0 W$ W (if (equal 0 undo_ctl) ; Make sure undo is fully enable/ \( g) R- ]! M7 u0 `
(command "_.undo" "_all")
d1 I. Z5 Y* ? (command "_.undo" "_control" "_all")
: E- q9 o* |6 x: K )/ E% n, `2 y, w
$ B1 c3 {" @" f" A8 H3 @. J4 L (if (equal 4 (logand 4 (getvar "UNDOCTL"))) ; Ensure undo auto is off' y7 e4 [5 `; f4 ?# q) r. @
(command "_.undo" "_auto" "_off"), Z* U; x: j6 t; [
)
. A, m8 Q1 ]* W) C4 ~0 I6 s0 S
, g! @9 H) i X. Y* N# t7 F (while (equal 8 (logand 8 (getvar "UNDOCTL"))) ; Place an end mark here
0 b0 I% M5 T$ u: R" b$ \7 g (command "_.undo" "_end")
( d2 c$ D4 t% D1 a5 K3 I: T0 Q )9 u5 D6 j2 f9 k @0 \& m; v
, ?; Z8 I) r4 ~
(while (not (equal 8 (logand 8 (getvar "UNDOCTL"))))
7 e8 l+ H- l$ D% M0 _ (command "_.undo" "_group")
( L5 V; ~0 R% L3 Y( M )
" R+ p8 O& j6 J: v/ g7 U
9 i. E# [ Y7 D; C (setvar "CMDECHO" cmdecho)
0 Y4 c. k" q& b! M' x undo_ctl
" F4 @! s) S" m- E); defun undo_init
% m6 }( a1 _* j
6 S: q( i K; n& L+ m6 P9 e7 g;----------------------------------------------------------------------------
6 P4 j y! D1 g' h; Restore UNDO status
$ {0 x: m0 V! @ ~- \$ v;----------------------------------------------------------------------------
/ h. e; W& b! q6 b/ e/ z7 i" f(defun undo_restore (/ cmdecho)3 l& v( Y4 A8 o7 T$ P" X; Z
(if old_undoctl
( _. o9 P/ Y! R- K (progn# k' O; @4 t, j& P2 T
(setq cmdecho (getvar "CMDECHO"))& Q: q' }$ E& I8 b& a( H
(setvar "CMDECHO" 0)! o, P& p6 m8 k! W
: j3 a' j% L# g0 v g8 b$ y (if (equal 0 (getvar "UNDOCTL")) (command "_.undo" "_all"))
4 t$ G9 l& z3 x+ R- p (while (equal 8 (logand 8 (getvar "UNDOCTL")))' k& q8 v$ ~5 P9 A
(command "_.undo" "_end")0 y" o$ r, E5 m" ?- k
); while
6 T4 ^& \$ X B/ Z5 Z! K5 B
( w5 [! W! a$ w3 f" u% V
( \% M5 M6 E. Q; K* {% ] (if (not (equal old_undoctl (getvar "UNDOCTL")))
$ W9 b3 M, ~ \1 Q; t2 b0 ~4 J (progn
, W2 R5 @. y8 j5 n (cond
8 E A8 F0 M) V( V7 p ((equal 0 old_undoctl)6 ?& k; Q2 P( @5 ~9 R# m4 T8 B
(command "_.undo" "_control" "_none")
, ^5 Z8 y: Y, T* p$ M ); I5 L |4 b1 B
((equal 2 (logand 2 old_undoctl))
5 c1 p; x) m! z7 j2 X (command "_.undo" "_control" "_one")
|% w9 a2 F! {. x: y9 `/ u )3 A$ q2 k6 r( j8 a2 w
)
5 S5 d/ E9 K/ `! a (if (equal 4 (logand 4 old_undoctl))6 t9 l0 o2 N, U0 d- z6 B2 M7 i
(command "_.undo" "_auto" "_on")
( t8 B' g% w& b (command "_.undo" "_auto" "_off")9 {% W- S0 ~1 h# x9 R# s
)
6 N3 i |) G T$ ?6 }- } )+ n. n# q8 w" ?2 J4 E
)
& k2 j0 B& }1 ]/ x; y& Y5 A (setq old_undoctl nil)) t' \2 f3 O9 V6 `8 y2 {
(setvar "CMDECHO" cmdecho)( y0 r1 A+ ^6 Y7 p c, X
)3 c, {5 L; u" {0 b- @; `5 k! w3 n6 ~1 B
)
0 ?8 D3 V9 j2 Y) Q, \. {); defun undo_restore
& A5 W* Z S/ R0 ]- j. y; t$ J3 n4 h5 b0 f* W1 R
;----------------------------------------------------------------------------
8 _1 u+ W4 ], n. e0 @4 o; Save variables
8 U$ t) F- {7 j+ f7 i. k;----------------------------------------------------------------------------
- f$ {. H0 X p' n(defun var_save (a)0 }* D/ p) i- G! ~
(setq m_lst '())* P6 Q- s, R5 G, f! F: ]4 Y
(repeat (length a)
' E" F( s/ z: i& A2 c (setq m_lst (append m_lst (list (list (car a) (getvar (car a))))))9 }9 X4 m; }# {% e9 O* _) l z
(setq a (cdr a))
+ r7 |. t: ]$ N% @5 U$ j, ]7 f )* Y+ ^- K/ M* j
); defun var_save
. C1 [+ d# s+ c# [$ N2 X0 k& `: |- l7 q0 ]
;----------------------------------------------------------------------------
6 x# B% G" D- |' q& A; Set variables$ X+ w8 i7 {" q- u1 S5 W; D
;----------------------------------------------------------------------------+ ?; e: a* z0 m. n
(defun var_set (m_lst)3 \7 x0 e X) q* g; u
(repeat (length m_lst)
" A6 j1 Y+ ]; n8 J, \" c2 g* d (setvar (caar m_lst) (cadar m_lst))
8 J4 V! i0 R( q0 }* t7 {6 t/ F9 c, X* T (setq m_lst (cdr m_lst)). d7 O$ V% e3 S/ m* \4 g- E
)
9 ?. U9 | t, r. q; x+ H$ `) w); defun var_set- |; Z; W; m' E4 v
3 K/ q Q5 B4 e( u# e. I( \* A
;----------------------------------------------------------------------------" ]. C% f- F/ q0 \. I0 S b
; Restore variables
g1 x4 z7 }- m) j8 P/ `;----------------------------------------------------------------------------$ H0 l4 O* {0 r$ O' F& h& _% E
(defun var_restore ()8 N2 k0 U9 d1 ~0 [
(repeat (length m_lst)& j6 V! {) P: y$ E
(setvar (caar m_lst) (cadar m_lst))- ]6 u+ H% g/ s, d8 O
(setq m_lst (cdr m_lst)) R- r6 x0 s" m% x" E6 t0 d! |
)% z' H; ]0 v. b& {6 V4 E
); defun var_restore2 W# }$ {# q5 W6 n% C: s
9 g$ u& ~: H8 t4 w
;----------------------------------------------------------------------------$ D6 e: ~7 \0 W* a" C
; Initialize routine
' d$ _# B; {7 ~; C7 M9 m4 @( y;----------------------------------------------------------------------------8 U! o9 i3 p9 A5 m8 m
(defun err_init(e_lst u_enable add_fun)7 b4 j. `& b- \6 b
(if err_alive (err_restore)) ; To avoid nested call
6 j3 n+ u2 [% `% { (setq err_alive T)
7 X$ k( R+ V/ \7 M8 \ (var_save e_lst) ; Save the modes1 [! w8 S G# {
(if u_enable (setq old_undoctl (undo_init))) ; Initialize UNDO status
8 {2 i; J+ y( o! ~* ~3 f (setq err_old *error* *error* err_main) ; Save the handle of *error*
7 y# M+ q2 I9 ^- r, u2 p: q (if add_fun ; Add the user cleaner
0 e3 N2 p; E8 I, D (setq *error* (append (reverse (cdr (reverse *error*)))* w/ D4 _! J0 W$ u$ q
(list add_fun (last *error*))
; Y" A6 ?9 v( ^3 T6 G* k ); append7 j" j8 [/ V: |* F. F# C9 B
)! q h" E. Z$ @0 ?1 p0 F& N: Y
)
1 f2 u" Z, g6 Y* j' {# C0 W); defun err_init |
|