|
|

楼主 |
发表于 2007-3-13 08:33
|
显示全部楼层
undo.lsp
;;; UNDO.LSP made by piggy
$ F" E7 p" K5 j6 j;;;- E# b' u7 J6 E# O" c; y
;;; DESCRIPTION
. `3 ]8 @2 G$ u6 Y: W;;; These are general error routines which can be called by
, X) g' H5 ^3 \2 D \;;; other routine. See AutoCAD 14 ac_bonus.lsp for reference
% R: e- G, M, d' ?;;;* Q! \+ A4 ^' l% B6 T5 z& g
;;; SUBROUTINE INCLUDED IN THIS FILE* v+ N: z) s# M4 f7 G; Y
;;; UNDO_INIT3 S& j3 I/ y7 S5 a" b; }" V& ~
;;; UNDO_RESTORE
- K# y4 s* L* v3 u) c! a# b3 T' U;;; VAR_SAVE ) {, l+ p& B! h4 Y5 }/ [8 N7 W
;;; VAR_SET 6 H$ f$ k! k W; c' l! q) R, s
;;; VAR_RESTORE6 N1 k1 p8 g9 S8 Q9 g Z
;;; ERR_INIT 2 K# _$ u, Q$ e( @- M- ?- l
;;; ERR_MAIN
; S3 Y' A X/ _3 ]9 Z;;; ERR_RESTORE( f. s) n+ m q
;;;, \6 X% K u+ W4 h
;;; DATE: 10/17/98; 03/31/99
( U* ]4 x. {* |, w3 ~;;;
1 g: k v% R. _ m" u; c;;; HISTORY:
6 o2 G8 j5 Z W/ p" p;;; Add routine of mod_att
3 @. G7 X6 F A5 W, X$ Z! Z;;;
+ s$ ?5 m3 |7 e, K* p5 r7 H7 A;;; USING METHOD
0 p5 l- |0 l: e+ ]# G* _5 d;;; ERR_INIT:" w. k& \3 f& \2 k' Q8 T. q
;;; This routine initialzes the error handler. It should be called as:
1 D; b! k( Z* ]' `* E;;;
$ r( A5 i- r1 [6 u" r;;; (if (and (not undo_init)' \8 G$ T7 e8 }! q& T( u7 a
;;; (equal -1 (load "undo.lsp" -1))
( j# k" @# W! m3 S;;; );and
# }7 s: Y) Z" a! Y& W8 f J;;; (progn (alert "Error:\n Cannot find UNDO.LSP.")(exit))
6 b5 Q( U5 c: D' h \& l* X;;; ); if
9 t5 \9 S7 A- N" V" Q;;;
: S& n+ i0 X% O;;; ARGUMENTS:2 d( T; j4 N7 s# I$ E/ O$ z
;;; err_init Takes 3 arguments. : C7 r3 Y- E& @) y: }' E$ s5 D5 G3 A
;;; 1. - The first element of the argument:
3 h4 {% N0 ~; D. h- _# I; E) Y5 [;;; This is a list of system variables paired with; q9 v* i8 ~9 Y
;;; the values you want to set them to. i.e. '("CMDECHO" "ATTMODE")
5 a% j9 W7 [* f$ @) {# b# d;;; 2. - The second element is a flag! g; e4 c" S4 S" S+ v$ Q! ?
;;; If it is true, then in the event of an error % n! d7 j' y% X4 C1 G
;;; the custom *error* routine will utilize UNDO . |, l7 c0 C6 b/ Q9 X m: x/ T
;;; as a cleanup mechanism.: N2 A+ H8 Q) ?3 H/ U
;;; 3. - The third element is a quoted function call.
h" }; {7 p$ R- n, O) `4 n( b) B% A;;; You pass a quoted call to the function you
; x; A i- [ _) r& a3 E, @;;; wish to execute at the end of nomal routine if an error occurs.
5 |0 p3 W) m5 L& Q/ |/ y;;; i.e. '(my_special_stuff arg1 arg2...)% r/ A1 l' k& F, C) v- `
;;; Use this arg if you want to do some specialized clean up 7 P& E b8 X) j# @# O
;;; things that are not already done by the standard bonus_error
& o e# z3 O. y! A+ Y;;; function.
0 A+ {9 D# T/ c;;;
* a% `: s+ y. o- w/ R$ e;;; ERR_MAIN: Body of error routine& L: _% F0 ]' U9 Q5 l- D& S
;;;8 {8 H7 N* ^7 I$ `" j; S4 t) }" a
;;; ERR_RESTORE: This routine should be called at the end of command to
5 O; M* A* T! n# ?* _5 ?;;; restore the VARIABLES, UNDO & *error*.
4 ]# I- N% l6 z: ~ `5 Z;;;
W5 d7 b6 r+ C+ T) g3 };;; UNDO_INIT: Initialize the UNDO status3 s! {; J# a# I0 A! w: s+ a3 [7 O
;;;. s1 e! J& U& r4 E& l/ {) \( k
;;; UNDO_RESTORE: Restore the UNDO status' ]$ M; _ R, f4 M7 W, G% a
;;;$ k6 ^% `9 C. l) a7 Q
;;; VAR_SAVE: Save the variables. the argument is like '("CMDECHO" "ATTMODE")
l0 t6 a% F. ?;;;7 A9 p' K& r2 M% [) P9 ~4 q$ O8 G' Q
;;; UNDO_set: Set the variables. the argument is like
6 x0 `8 P; K3 ~* Q4 T8 E' d;;; '(("CMDECHO" 0) ( "ATTMODE" 0))3 t1 x- a: P7 ^4 e" a5 ^
;;;/ Q+ t8 @' v9 H$ ]% T
;;; UNDO_RESTORE: Restore the variables
2 Z' i! z/ u( I8 h6 {9 |+ b, L: K% s;;;, O% s. V. E- m7 {8 Y: k$ C! `
;----------------------------------------------------------------------------! B0 y( Q/ p. X% q
; GLOBALS:/ a. \% h2 d2 Y
; old_undoctl -- old status of UNDO (voided by UNDO_RESTORE)1 e; j0 _( q# Y* p, Z6 ]
; m_lst -- list of variables (voided by VAR_RESTORE)% ]3 H8 h J/ S. X$ z. G: v
; err_alive -- indicate error routine is active (voided by ERR_MAIN or) r A0 K& k; ?) g4 g3 {
; ERR_OLD)
6 z) L2 s! E% @% K; err_old -- old handler of *error* (voided by ERR_MAIN or ERR_OLD)7 k5 J9 c( ^ t9 l# ?/ Y" S& L
;----------------------------------------------------------------------------
: |" ? e' u4 o6 b* @8 l/ w8 @- r4 l
# d+ ?: f8 i4 X;----------------------------------------------------------------------------0 J7 ]2 Y' H/ d, R
; Modify attributes according to entity name, attribute name, dxf_item; v9 H3 l7 P$ k1 J' a
;----------------------------------------------------------------------------
8 }7 v9 z z2 U: t1 J+ k2 Z(defun mod_att(ent id dxf_item)
; y8 O( i* Z5 `0 B9 p (while (and (/= "ATTRIB" (car (entgetf '(0) ent))) (/= id (car (entgetf '(2) ent))))( m* {7 v* j3 i8 z+ G
(setq ent (entnext ent))
) X! ?+ L% c: C$ D& {& P. x ); while ) k& a7 O. m7 Z% L
((lambda (x)
3 l$ [8 p! m+ j (mapcar '(lambda (y)
# P9 X; X3 r2 \! f+ {2 @/ M8 v (setq x (subst y (assoc (car y) x) x))5 [* m; s3 w3 X5 J. R
); lambda! l2 Y" ^7 U/ ^- X) g
dxf_item/ m% l* {6 t& M; s4 \
); mapcar
, a: J4 J/ \) D# A t, w (entmod x) k( t0 R+ t+ x |. r e
(entupd ent)
$ @1 e% @2 a A3 u ); lambda
* v* j$ l& A n# r% R) | (entget ent)' f7 _; [5 R8 ]' N
)# m6 L& }; L% t9 A
); defun mod_att
: g `* [. c2 G# T: w q8 Z+ @' |. c+ r
;----------------------------------------------------------------------------
- z; {) Y9 f) H* t9 e( i* u; Check layer status, return a association list which contains layer information6 v+ e4 r" z5 p% V
;----------------------------------------------------------------------------
: |) C5 a2 H) E(defun laychk(lay / l_sta)! G! P- m( v O3 g. i) y. t
( (lambda (x)# L4 e. O" d& X! ?6 a- ?' N
(if (not l_s)* `: [: `1 |7 Z4 b' r' M
(setq l_s (list (cons x (logand 5 (cdr (assoc 70 (tblsearch "LAYER" x)))))))) y6 ^, d: H$ b% v% L& [
); if* J7 [9 m" Q# n. c1 r6 X1 F
); lambda & m3 q1 h1 s0 r Z% Q, k& r2 e3 A, i4 r
(getvar "CLAYER")
% V/ E/ ?% [3 E! l )
, J) e) l& B5 l9 ~# T! }. V: j (if (not (tblsearch "LAYER" lay))
0 l! Z* ?0 w# ?4 R) ~" ^7 s (progn
5 W1 `6 v' }- t; L (initget "Yes No")
, I1 ?8 ?! A& z# v (if (= (setq l_sta (getkword "\n图层不存在,是否建立该图层 ?\(Y/N\)")) "Yes")
2 X9 K S; B' I) D/ C (progn
' b0 p. e; f% M4 N (command "_.layer" "n" lay ""), }, S3 l: u( k# z
(setq l_sta 0): C- a9 L% v* j9 O+ p, l2 @
); progn
) o1 P# v( c& q% W3 G0 j- } ); if5 o/ r) ?% d* s% h# d( \. d
); progn
! ~4 V6 [% O i# }, o- | (progn' P# y' z- G+ }# @# ]
(setq l_sta (logand 5 (cdr (assoc 70 (tblsearch "LAYER" lay)))))
3 q& w* [/ s; [' [# |* [2 c0 O (if (= 1 (logand 1 l_sta))
# r/ G5 M' a5 V+ B a (progn* ?+ l( U7 [5 F \
(initget "Yes No")
5 Z/ ?6 M( b- ~& J7 N (if (= (getkword "\n该图层被冻结,是否解冻 ?\(Y/N\)") "Yes")+ [1 W( ?7 A8 Q- E8 \# N: H
(command "_.layer" "t" lay ""). g }& m) u& C3 g5 p9 u
(setq l_sta "No")
5 {. X- }+ j+ Y$ h: H) E ); if4 ]! D6 C1 o; v1 a8 U2 _4 b& @
); progn# m# G! P6 T9 p4 h
); if, }' e9 e: v* H, `
(if (numberp l_sta)
7 ^9 s# b* I8 L (if (= 4 (logand 4 l_sta)) (command "_.layer" "u" lay "")); if
* w4 C1 f" }$ i$ I ); if
5 D5 Z$ I6 j8 k ); progn
$ U( g* A$ }, J ); if* p! k5 a% v8 n# s' |
( (lambda (x)
! L9 @( o' i6 K# L o (cond & ~8 E' X1 B6 Q6 K3 `
( (not x)
; _7 y2 ?' B/ |7 Z: ?# K2 k% J (setq l_s (cons (cons lay l_sta) l_s))8 ^9 F5 C- t" R$ w
)+ y1 I9 l7 F. y' Z! D, G' R
( (= "No" (cdr x))- ^) m f' a. ~4 Q
(setq l_s (subst (cons lay l_sta) x l_s))1 j" h* h Z+ u& x; U
)7 D8 g' B5 T& S1 W) z
( T l_s)3 ?( R! g5 q: G
); cond
. g. v+ c* A( g8 `- S ); lambda0 U* d& [4 x0 j: J5 x
(assoc lay l_s) ~6 ?9 z( i; r1 z ]( Q6 v* B
)+ S" L2 F5 N+ v! I' N1 C( n: ~
; H% r( A' W5 O: y
); defun chklay
M$ x% ^# x1 f8 a9 t
5 f a. ^! t* j7 R5 z8 |- c9 A;----------------------------------------------------------------------------! a2 j! W1 J. L: I" [! U& U
; Restore layer status according to association list l_s6 I5 @# ?" u! z0 b% W
;----------------------------------------------------------------------------
! S% f$ ^3 Q! v(defun layres()0 ?( u) @9 q9 N4 A3 L3 H% p: @. s
(setvar "CLAYER" (car (last l_s))), v& I- d5 w: l
(repeat (length l_s)" z- @8 @8 z1 U
( (lambda(x) 3 e' ^; K, B* V% F# Z. t& v4 ]
(if (numberp (cdr x))
9 z& p' D( {2 V; c, F" y* i, G (progn 2 `, D5 O% F& B
(if (= 4 (logand 4 (cdr x)))
+ [5 R- I) L/ h% N- B: M8 [ (command "_.layer" "lo" (car x) "")" \' r4 G% R" F+ \, u4 {
); if
; W4 A$ ]) E' i0 l (if (= 1 (logand 1 (cdr x))): r4 C4 X$ v' H" c" m% W
(command "_.layer" "f" (car x) "")
8 \7 u/ S5 Q' [7 \ ); if8 W' O A( W+ i6 ]' j
); progn- M2 u6 d* S4 A5 d2 D' A
); if ) w9 X& E2 q' D f: N
); lambda
4 {3 Q# j3 g7 \$ O# Z6 X (car l_s)
: s" Z* @4 i# x! S+ G. A ) }) P9 }( f0 U
(setq l_s (cdr l_s))
3 z+ {$ r7 w! I ); repeat. F- r* q ? t* m9 ~
); layres9 q$ F1 V' b4 b5 X: M: h
6 ~) R2 |# z- J, W& X
;----------------------------------------------------------------------------8 _& l9 _* ~* |
; Get DXF codes
& x, Z5 W6 B! ]( M; D- R;----------------------------------------------------------------------------* T1 y' l& q! F3 R2 J
(defun entgetf (index ent)
5 _# g9 n8 f, q* v ((lambda (e)& `% ]* k! J. i# c
(mapcar '(lambda (x)
6 W6 Z9 E4 [3 }4 T (cdr (assoc x e))
$ N6 }2 v3 r3 u: D$ [! ? ); lambda& H: u0 @4 x1 E6 u. n
index) ; internal lambda function
- ~; s F8 o; O5 o6 `5 |6 ]* F ); lambda7 K5 q) F0 h3 p& [- V$ }8 a
(entget ent) 5 p" G8 l5 u2 }$ O4 y
)4 W" @, V' S3 T
); defun entgetf! K2 ?: P: }& S- l
5 A) g7 B3 ~" n* z. C0 \) `8 L
;----------------------------------------------------------------------------( h0 S$ R- K" [4 J7 v9 v4 A
; Save UNDO status
$ Y, ?+ d- G. v: P" ~4 _;----------------------------------------------------------------------------
, t5 w, V4 j1 {' O(defun undo_init (/ cmdecho undo_ctl)
0 \* f; k0 }( l9 m (setq cmdecho (getvar "CMDECHO") undo_ctl (getvar "UNDOCTL")) ; Save the value* L3 d; C! P% A0 [0 {0 C( \
(setvar "CMDECHO" 0)
3 e) t' R, Q; e& H" L/ O" {: P) ]) a' \
(if (equal 0 undo_ctl) ; Make sure undo is fully enable
+ d4 d& t0 a0 D4 b+ P1 ] (command "_.undo" "_all")& N- b6 d0 j/ ` m" R0 h
(command "_.undo" "_control" "_all"): i- S2 [! m9 w9 d2 m6 Y1 N9 z
) u6 S* ~* K9 Z8 r0 a! \5 f
" v: n# k+ j" w0 A+ T- t( s" X (if (equal 4 (logand 4 (getvar "UNDOCTL"))) ; Ensure undo auto is off# C) y' Z# S4 O p3 ]# K! A
(command "_.undo" "_auto" "_off"), C, e3 I3 m$ y* ^9 ^5 ~
)" F% j" i# Z3 W% ?+ b, R
) A( ]5 [8 r0 P, J7 h$ v& I M" L (while (equal 8 (logand 8 (getvar "UNDOCTL"))) ; Place an end mark here
+ X0 l1 Y! D4 h/ ]* l* K" q (command "_.undo" "_end") s3 O3 s" H @3 y/ `% ~
)
% r' w R0 k0 o* H1 w8 U8 u* E: B _" h. I- o: \% A
(while (not (equal 8 (logand 8 (getvar "UNDOCTL"))))
2 | w" c. l# S6 C0 m3 Z (command "_.undo" "_group")9 n7 n$ D/ x, Q! O" n7 O1 O
)& d" F: `* d. ^# ~! q/ ^% k
4 L' j- L" V/ p3 O- [7 ~
(setvar "CMDECHO" cmdecho)+ a7 a, w+ U ?7 Y
undo_ctl
) u4 R: A0 }8 I" g& n/ f- ]1 y); defun undo_init
1 U5 W: ^; ~+ F
* Y+ _* ?- A+ M( Y0 i! S2 C;----------------------------------------------------------------------------; B3 K; p% U' F; h" f$ O
; Restore UNDO status F; u7 a3 K' C Y) o3 B; G
;----------------------------------------------------------------------------7 {* ~! a# U( X2 ]8 v; Q& @
(defun undo_restore (/ cmdecho)
# @# T3 Z6 p, |$ B+ ^. s (if old_undoctl
, b5 D% ?+ J8 J; A (progn
. _" x) L; C4 Z9 m (setq cmdecho (getvar "CMDECHO"))
2 @; t! I$ U" {) f r) M6 _ (setvar "CMDECHO" 0)
' q' c; |; r" g$ D Q! k& m9 a# e3 s2 G/ o5 f" O4 M# w7 G
(if (equal 0 (getvar "UNDOCTL")) (command "_.undo" "_all")) # n6 H$ B/ h' Y
(while (equal 8 (logand 8 (getvar "UNDOCTL")))
0 e& K* X8 O9 }) X1 z (command "_.undo" "_end")# T2 a; N! w$ P# ~% }0 T) e
); while
9 b5 m5 W) {+ n/ ^* c7 _2 A% X0 Y r) L
' r2 v7 c& H" H r: U
(if (not (equal old_undoctl (getvar "UNDOCTL")))4 N2 f& o+ N* m- F! ~' k
(progn9 l/ V- `5 u0 @& w% o1 _
(cond7 e. m% t0 z% U5 Z6 r: q
((equal 0 old_undoctl)
o& ]; X# P" S) \ (command "_.undo" "_control" "_none"), k6 X7 v: P1 G# b: e2 E8 ]
)# b/ w2 i* |/ }+ C: l- M( s; Y" m+ t
((equal 2 (logand 2 old_undoctl))4 @) E6 j; f2 i' B4 F! \' t9 }, Z
(command "_.undo" "_control" "_one"): {' y: U' b W7 A' ]; h3 D
)
* O9 b! \: i" }6 _5 U )
, T: U" b4 X2 c, ~0 a (if (equal 4 (logand 4 old_undoctl))
0 s* M1 D1 Q7 l2 }' A (command "_.undo" "_auto" "_on"), o7 ^, w4 E+ M
(command "_.undo" "_auto" "_off")6 X) ^1 m' G% \; s ]3 J9 k3 Z
) O1 N% c7 ^& {% y
)
+ `$ X3 H, P: s3 Q, e+ q8 _ )
% [# O2 h7 U8 f0 w4 v (setq old_undoctl nil)% t1 O" o1 x+ _/ K
(setvar "CMDECHO" cmdecho)
% D8 R( q8 A: G! N, n )9 V3 y4 T# p7 g& ~7 c$ l
)# ]& H& ?: J& E1 D) ]
); defun undo_restore
4 \# \) L( t1 N% T. }
* {% ?5 I( \% T/ S* X# e;----------------------------------------------------------------------------' P3 Q* }" A7 b- |0 o- k% l
; Save variables9 w% v" g- d {5 l/ x n
;----------------------------------------------------------------------------
- c: n# t3 k8 Y9 w- j7 f(defun var_save (a)
0 g. S% T4 C9 n9 H1 k (setq m_lst '())6 G1 n) ]1 D u: n
(repeat (length a)
( Y+ L& K$ ]( \ (setq m_lst (append m_lst (list (list (car a) (getvar (car a))))))# |3 C2 _1 e* u4 `: q
(setq a (cdr a))4 n$ O0 e+ r: T' ?* j) P4 j
)
" y" C% i7 U. d1 b. i); defun var_save
3 M5 F$ @" g3 g6 ?& O4 i
- j9 Q6 b$ }" V- i+ i( _2 u;----------------------------------------------------------------------------' g4 f0 y, t, d# ^# ]. G% o* O
; Set variables- s$ [; d* Y s6 Z$ ~- ?) O/ b9 ^
;----------------------------------------------------------------------------
: j- D, q+ D' `1 L( ](defun var_set (m_lst)
* x4 U* g$ E) I) h: q+ j1 q (repeat (length m_lst)
+ E/ ^2 ]; F; ? (setvar (caar m_lst) (cadar m_lst))6 G8 z0 I) ?5 J+ S9 Q( x
(setq m_lst (cdr m_lst))
. ~$ [4 Y( v1 L )
. I- K. u5 Y9 z4 \, z2 ]# s( z); defun var_set
7 t: {( F/ n2 j m
0 p2 N6 @( O8 T) a3 i. r) g @( n;----------------------------------------------------------------------------" C6 }7 E0 H3 r) D3 S3 c
; Restore variables, I+ ]8 u- w2 } w" [( Z
;----------------------------------------------------------------------------4 C0 j4 \/ I$ N$ s3 S# x
(defun var_restore ()
( e) ]9 s4 j1 b* V$ c (repeat (length m_lst)# a6 ?2 \% l! i* d
(setvar (caar m_lst) (cadar m_lst))" z6 N4 S! s% \ N
(setq m_lst (cdr m_lst))
# E) D* q- @ g& f* J1 Y2 I$ }1 q ), ^3 e, o" ?: Y* _9 R+ @& N0 K. v
); defun var_restore
! b C1 s) I j8 t" Y' H( l5 u8 d1 a1 l J- N
;----------------------------------------------------------------------------+ T( L \' T/ e+ {9 S
; Initialize routine 8 [% ?8 h3 y! N- Q
;----------------------------------------------------------------------------
+ J$ _. s2 ~3 F8 D* w* \(defun err_init(e_lst u_enable add_fun)
) D2 R8 i% ~1 r U. k/ C3 J0 P (if err_alive (err_restore)) ; To avoid nested call
! e$ m9 }! V; Z( D9 w (setq err_alive T)
( F4 n4 x p/ | F! g6 H/ ] (var_save e_lst) ; Save the modes0 b0 Z; C7 M( [- U' l: c, }0 d
(if u_enable (setq old_undoctl (undo_init))) ; Initialize UNDO status1 G% v" } v- y- {/ M0 O
(setq err_old *error* *error* err_main) ; Save the handle of *error*
% z8 O$ b o6 X, X! i+ R, l (if add_fun ; Add the user cleaner- r$ {, ]+ X4 n) O n! d
(setq *error* (append (reverse (cdr (reverse *error*)))
" r; c" |4 ^& `! `7 { (list add_fun (last *error*))
5 e; ~, ?* |5 i0 f$ `5 O. }, E ); append
3 Z- E5 G0 ?8 f" V. @! w. S )
& {5 \, g4 U0 y# ? )
2 W0 {8 _, w3 {5 }# Y p( x7 ^+ W4 A); defun err_init |
|