|
|

楼主 |
发表于 2007-3-13 08:33
|
显示全部楼层
undo.lsp
;;; UNDO.LSP made by piggy0 M" H5 G) n! A+ s( ]
;;;3 h6 k( j! F8 K# @0 q
;;; DESCRIPTION
# X% V- g) W5 V;;; These are general error routines which can be called by $ X% R$ Q" x( p* y
;;; other routine. See AutoCAD 14 ac_bonus.lsp for reference
- u; O) O' U' y5 ^# N( n) l;;;
# r1 m! J7 T% K6 E& D2 `;;; SUBROUTINE INCLUDED IN THIS FILE! o. A9 h1 r4 H+ ^3 V) d% D
;;; UNDO_INIT
5 V$ L/ o1 z* w- q: ~2 |;;; UNDO_RESTORE4 h! y; c8 P' |5 b, J6 u4 k' q$ \
;;; VAR_SAVE
- q. X) W; J0 B8 [3 h7 J;;; VAR_SET
3 I% ]% v& b6 R9 Y: q i( i;;; VAR_RESTORE9 w8 T5 h6 V5 P9 n" [
;;; ERR_INIT
1 ^, ~) ?+ d4 I2 x9 v;;; ERR_MAIN 6 X4 R4 z; H3 q' L
;;; ERR_RESTORE! G# t' ~$ w7 {, M! @3 a
;;;
, Y1 C6 D0 _: o+ Z! m7 w D;;; DATE: 10/17/98; 03/31/99. d1 P1 P6 ~" h" @* z* M& R! L
;;;( C# r* K* Q/ E! @" t, ^5 z
;;; HISTORY: ; D0 H/ r$ y" A3 Z7 F8 S
;;; Add routine of mod_att* ^' [: ~9 }% @8 ^% @
;;;
1 X( r4 y1 h$ F1 R5 o& [: \;;; USING METHOD8 A5 y+ }8 p4 y' K( w4 x
;;; ERR_INIT:
: e+ H! W( X$ u, Y;;; This routine initialzes the error handler. It should be called as:
! n( u4 B" Z5 e' u; K4 p;;;
2 Y' u7 f' {7 ]/ m9 A v;;; (if (and (not undo_init)8 n, S g$ x- [4 ~( I2 _; I' T
;;; (equal -1 (load "undo.lsp" -1)) - g- n0 E) T2 F9 i' A B: c
;;; );and1 e; {4 W" n- E* C
;;; (progn (alert "Error:\n Cannot find UNDO.LSP.")(exit))& }7 i: R4 l% \! ]7 r! V& l$ s
;;; ); if2 J/ I; s: w( I3 Z' U; n
;;;
$ c. C0 ^5 m8 {) C# f7 [% x/ H;;; ARGUMENTS:
: c8 v d7 }; T0 Z- H1 j+ E1 d+ i;;; err_init Takes 3 arguments.
8 B& F/ y8 l2 E5 `1 i3 c$ e5 i. U;;; 1. - The first element of the argument:
1 W. I, X6 N0 ]$ y. X; w;;; This is a list of system variables paired with
/ e# @. p2 {( o8 [;;; the values you want to set them to. i.e. '("CMDECHO" "ATTMODE")
, A. W) J* q- E4 `;;; 2. - The second element is a flag7 H: G5 y! k+ Y
;;; If it is true, then in the event of an error # R$ }# q: B0 J9 l4 J9 I* z( Q% M4 T
;;; the custom *error* routine will utilize UNDO 3 A1 c4 w- t# w( d! S
;;; as a cleanup mechanism.
/ O% V% _ p/ V;;; 3. - The third element is a quoted function call.+ R7 V; P6 {5 b# F7 H
;;; You pass a quoted call to the function you
* |! b7 y2 z5 |+ o;;; wish to execute at the end of nomal routine if an error occurs.
, P' G4 e4 j# l* ]0 c/ L! E4 p8 u6 Z;;; i.e. '(my_special_stuff arg1 arg2...)8 w6 j8 k* v+ r! ]1 P6 O
;;; Use this arg if you want to do some specialized clean up ( f. Y2 |6 y' U
;;; things that are not already done by the standard bonus_error
2 |0 f6 X, \1 O V/ I4 j;;; function.
. c) o9 [5 c7 `. _, v& m- ]8 w;;;) b" [) x% x( z' F! H
;;; ERR_MAIN: Body of error routine! G+ w$ M, `; U
;;;
" `+ Z, S( h2 p0 g3 g7 B7 U) x5 f;;; ERR_RESTORE: This routine should be called at the end of command to: _: @& B7 _" Q2 X
;;; restore the VARIABLES, UNDO & *error*.
! X" B( |& _( U+ J* a- J;;;
I7 l m. u# S9 A& l& v- K; W7 k;;; UNDO_INIT: Initialize the UNDO status8 i6 L E3 Z3 T/ X. ]9 i7 C
;;;% s2 |$ O# T% b& M6 n3 H
;;; UNDO_RESTORE: Restore the UNDO status
- K2 H& y4 K7 f2 j$ R- };;;
7 \% y" d$ j+ C& \3 ~;;; VAR_SAVE: Save the variables. the argument is like '("CMDECHO" "ATTMODE")( t$ G( c2 e) Y, v% f* V
;;;- X2 N' C; J# W
;;; UNDO_set: Set the variables. the argument is like6 C x" u) @# S) m2 O' |$ _
;;; '(("CMDECHO" 0) ( "ATTMODE" 0))% I, t% _4 P/ ~: r$ f
;;;
9 O4 N& ~) T# G;;; UNDO_RESTORE: Restore the variables' E/ |6 X3 d# \5 B- F% v
;;;- e) u; i5 K9 s, E; H0 j. S
;----------------------------------------------------------------------------: J' B: r* ]2 |5 w2 g3 Q& a7 v
; GLOBALS:$ F, Q- V1 m, ] Q5 G
; old_undoctl -- old status of UNDO (voided by UNDO_RESTORE)6 Y' ] o( s) h) p$ v: ]. {
; m_lst -- list of variables (voided by VAR_RESTORE)
% i+ v9 g3 h3 Q0 P0 [; err_alive -- indicate error routine is active (voided by ERR_MAIN or) C( O% `8 T; d5 }1 r7 K+ n
; ERR_OLD)
6 l7 T. Z3 u4 d' C; err_old -- old handler of *error* (voided by ERR_MAIN or ERR_OLD)
* A& i$ r4 V. X;----------------------------------------------------------------------------* l% q& j7 }$ P1 P Z
- ]1 @' i% f1 k& q2 H;----------------------------------------------------------------------------. [) }0 d/ `0 x- R
; Modify attributes according to entity name, attribute name, dxf_item O$ f/ X! n+ y9 X. {2 W
;----------------------------------------------------------------------------1 K& Q4 U+ y/ a: B* C
(defun mod_att(ent id dxf_item)
( C0 w1 b0 t* K5 k# e (while (and (/= "ATTRIB" (car (entgetf '(0) ent))) (/= id (car (entgetf '(2) ent))))
0 b- {) [/ M% }+ G+ P (setq ent (entnext ent))& D2 m! ~* X! C9 E6 u
); while 1 y' Z! V; p, T! k0 i3 j
((lambda (x)2 w" x9 d4 R8 d' E$ \( t; X! B% E
(mapcar '(lambda (y)
1 o! X% M0 ]0 V" U6 W1 ~ (setq x (subst y (assoc (car y) x) x))
, f) j1 e0 Q% P9 q n e ); lambda) @+ e& m" Z, ?1 d/ m
dxf_item8 s) n. l: @- m; s2 ?
); mapcar
, [6 P, l' h5 Z* }5 U# P% y7 ~ (entmod x)
- K |1 e# d# L3 i9 h (entupd ent)# p9 S* p4 V( [4 k8 ?
); lambda
. W2 I+ h$ [# u7 o: h (entget ent)1 r& x3 g8 c+ ^) \: r6 f
)
! W3 x/ x) t' W; e$ v); defun mod_att
; L7 c+ n! E4 k3 |! ^, f J. k1 h" U% n/ f+ [
;----------------------------------------------------------------------------6 f! S ?0 i+ V# {% C, n
; Check layer status, return a association list which contains layer information2 d! q+ N7 k7 _! s
;----------------------------------------------------------------------------8 b: E$ f. E* ~6 d& b" N6 I
(defun laychk(lay / l_sta)
$ R! Z/ _0 F; ^/ K. Z4 X2 I ( (lambda (x); M- Y3 ~/ l3 y* N
(if (not l_s)# D6 o# c" c' o! K% p2 S+ i
(setq l_s (list (cons x (logand 5 (cdr (assoc 70 (tblsearch "LAYER" x)))))))
2 Q* T. t2 K. O* t! e& F" t ); if! P4 p) g$ `0 I
); lambda % w5 G6 F1 Y, ?, F: d
(getvar "CLAYER"). f6 z5 u% @( o) J) p; z
)
- W6 ]8 b& e3 E0 g( ?. c" r4 E (if (not (tblsearch "LAYER" lay))
1 b% z) j7 F9 E) _5 w+ a8 b (progn + A. I6 k6 D2 W( e2 u0 o! s
(initget "Yes No")
- m4 G& k8 f) }* j0 E# B0 y' x (if (= (setq l_sta (getkword "\n图层不存在,是否建立该图层 ?\(Y/N\)")) "Yes")
" T0 G; ^! m# u% D (progn0 Y0 j$ ^! x* H. F3 k" r! T
(command "_.layer" "n" lay "")
@$ z0 Q( e6 f$ X2 E' H O (setq l_sta 0), l; ]4 x1 ^6 Y9 @' V7 F
); progn* _) v- U+ M3 z; R/ R Q: s, P
); if% b4 x# l% r' a
); progn9 f! a5 M3 y) k% W6 P, F7 F
(progn9 C2 G1 M" i/ S! D
(setq l_sta (logand 5 (cdr (assoc 70 (tblsearch "LAYER" lay)))))3 T; V* h- N% ~
(if (= 1 (logand 1 l_sta))
. s, ]% f- q: g- E& \1 [ (progn3 z8 f: F- \; m1 D6 \4 y* E& [
(initget "Yes No")$ O6 Z/ J( g1 q' x' W' O1 }" n0 e
(if (= (getkword "\n该图层被冻结,是否解冻 ?\(Y/N\)") "Yes"); e9 n W# E/ G- e- p9 v
(command "_.layer" "t" lay "")2 L: s! T7 ]5 k# v* Y
(setq l_sta "No")- G! W6 Z: X2 K' R/ ^
); if# i4 @& P h) c. A; A
); progn
1 A1 I5 H7 n: P ); if/ {- l- t$ l- |# s9 D
(if (numberp l_sta) , X1 j9 @$ h+ r5 I5 K9 g; L8 V6 S. c
(if (= 4 (logand 4 l_sta)) (command "_.layer" "u" lay "")); if
2 g" t4 X- \ N; N- D ); if, Z3 j7 K5 }- ]! E
); progn
: r9 A) {1 L, l, u; {2 d7 [7 x5 v ); if
& y# C3 `# Q3 q& A ( (lambda (x)
8 x+ Z/ k* s6 ` (cond
$ @, o# ], [7 K! K9 W ( (not x)
0 @- t' M) K ~ @: F1 n (setq l_s (cons (cons lay l_sta) l_s))! b$ V5 r4 \5 S0 Y
)+ e/ z/ q5 P0 f6 U. o) P% |" m
( (= "No" (cdr x))
- K- ? }2 i5 [" A (setq l_s (subst (cons lay l_sta) x l_s))
/ A5 J) h- d$ {, O. s1 Y/ u. h% ?7 v )
7 T' n' j S' g7 K ( T l_s)* J8 { p* ^2 V
); cond
' o3 P8 ~5 R6 t7 Y9 C ); lambda
7 A; {. k" }1 N$ }8 T, D (assoc lay l_s)- R8 d9 g7 v# h% t
)* [5 z+ a) [( ?9 ?5 ^% P: H9 r* g
9 i8 I/ U9 [% t5 t- g3 t! Q7 s
); defun chklay+ D$ Q: E1 n8 C9 z
7 p" _: y1 I, o+ M;----------------------------------------------------------------------------5 b$ n# ?' \- q5 U1 `
; Restore layer status according to association list l_s# q6 t# e; c1 c* j
;----------------------------------------------------------------------------
6 t4 W' i" H) o6 p6 E9 z9 l(defun layres()8 W4 [- [- `( c! q$ V% s
(setvar "CLAYER" (car (last l_s)))
0 ~! G) j- x. n7 u+ ^+ Q (repeat (length l_s): }( ?. L' K- k1 M
( (lambda(x)
! `+ R1 J2 G# s5 @( \. E7 }- ^ (if (numberp (cdr x))
g' q7 i$ X5 ^4 W; @ (progn % ~$ J. H$ D4 H% y9 D3 M" l: ]4 q
(if (= 4 (logand 4 (cdr x)))
0 q3 U. }% r6 `+ l. \4 U; V! d" U$ Z (command "_.layer" "lo" (car x) "")/ b+ M$ y8 Q7 x, m7 p( a
); if, d3 p5 s3 v9 b* Z
(if (= 1 (logand 1 (cdr x)))# z/ s2 h) a' R# [2 w( U9 @
(command "_.layer" "f" (car x) "")
9 k# i4 S" a% G ); if
" p( O7 m; `+ F ~! Y ); progn% n, M$ W9 b$ \* d+ M) t' S1 T" @
); if
5 G5 q; f2 v& ?7 ? J* I+ G* }# Y7 q ); lambda
! K+ _ |" R' b0 b/ ^1 k, k (car l_s)9 X4 T; r: F k
)* H8 D ]# O8 [7 R7 C+ m# S9 l
(setq l_s (cdr l_s))# i( X/ _+ M2 c6 T- [/ {
); repeat
. r; m4 M/ T! |5 p); layres
, g2 {: Q4 `' E, A- V2 }5 Y( t4 c3 S& T
;----------------------------------------------------------------------------
7 Q& D2 P, j( H) C; Get DXF codes+ Q3 b; D4 h" l
;----------------------------------------------------------------------------
( y) Z5 A: r" q! k8 f' `6 }(defun entgetf (index ent)0 W( S' l$ ]- x% S9 r# ^& P
((lambda (e)
3 ^5 x* t$ [2 {3 ~( |4 S8 I3 O (mapcar '(lambda (x)
" c6 m. B; d2 O7 k! u (cdr (assoc x e))3 K2 a4 N( |0 P$ H
); lambda( p8 ]) ]4 @; L# s; J
index) ; internal lambda function8 f4 m. S0 N6 q }2 g; p) q8 j
); lambda
) N0 B0 I' y7 O! F# o (entget ent) 9 O: z' ?6 f" h
)# E6 d$ n: U9 \2 s- B5 x) L
); defun entgetf& V; W( _* }6 K, N4 e! e# D
+ Z. b7 ] I6 n1 E. f
;----------------------------------------------------------------------------- O/ f9 [) R7 u! V$ n
; Save UNDO status
2 |; `/ F" I: i$ p. {! R;----------------------------------------------------------------------------5 Y) H3 z! Z; `3 z
(defun undo_init (/ cmdecho undo_ctl)
* M. ]( e: K# v+ k! X. J9 A1 B (setq cmdecho (getvar "CMDECHO") undo_ctl (getvar "UNDOCTL")) ; Save the value$ l% f- r, I. } d$ f* Q9 `
(setvar "CMDECHO" 0)3 p" Y# _# P6 A
4 }& d5 X2 y" [( q, ]
(if (equal 0 undo_ctl) ; Make sure undo is fully enable
: A4 ]: p' G3 O H" V5 E1 \ (command "_.undo" "_all")7 B" X+ v3 `% O5 ~* B1 R
(command "_.undo" "_control" "_all")
1 J! N/ x& {" s6 G. w6 }7 E )( ?. z" C# ^5 d% C7 H7 k- @
( G" F6 z/ o, Q/ R# v (if (equal 4 (logand 4 (getvar "UNDOCTL"))) ; Ensure undo auto is off
) F1 r( q8 }% M# f h( M C (command "_.undo" "_auto" "_off")! _+ w) E2 e8 B9 n: g( d& u
)
( y0 X( C1 ], Y! D
. p6 I/ S# O5 P% P6 W ^( G/ C7 ^ (while (equal 8 (logand 8 (getvar "UNDOCTL"))) ; Place an end mark here
' T$ c! @4 ?3 q (command "_.undo" "_end")' w+ ]4 a) d5 _* ]1 c
) P1 a, n7 B* }1 b1 I2 \; N8 Z+ d
6 `# ^2 _+ h" c- q. a q/ s2 h (while (not (equal 8 (logand 8 (getvar "UNDOCTL"))))
. W0 t5 E2 C! S5 Y; J6 H! L# n( ? (command "_.undo" "_group")
; I# |! k8 N' l# P )
9 A ?# J* h+ q" i. j+ x# K$ A y. ~
, X/ o. r5 |) h* q) r' T (setvar "CMDECHO" cmdecho)3 U, i+ P1 `3 t- ?# b
undo_ctl
1 W4 r( |2 J% l2 d* c0 F); defun undo_init2 J( ?1 W2 H; ] h) N9 K n" b! j
" |1 [% D. b2 c5 h( R;----------------------------------------------------------------------------
0 W: F2 K8 [; M$ B3 h7 s: v4 k; Restore UNDO status
4 G/ f" _$ p1 d K$ j! V;----------------------------------------------------------------------------
, }% z9 E6 h5 S/ N3 a( J; E(defun undo_restore (/ cmdecho)
. _) s0 _# ]' Z% G3 R& o (if old_undoctl
4 ~8 e. ^4 L6 _7 b (progn
8 u* W$ Y2 N7 |) b* y' R (setq cmdecho (getvar "CMDECHO"))
0 V' U8 U0 [9 {7 A" i* k' ^ (setvar "CMDECHO" 0)# H7 m3 _* I/ n9 O$ [/ |8 S* j
# T" X& J% e( O$ j; ?9 f. w2 T (if (equal 0 (getvar "UNDOCTL")) (command "_.undo" "_all"))
" r* D; a0 |& i6 z (while (equal 8 (logand 8 (getvar "UNDOCTL")))
/ b; z5 E; i ? W (command "_.undo" "_end")9 p1 t! s G. O8 _! Q' W+ X& S: E
); while4 Q F/ V( q" \7 L$ N
9 A8 b! `: S4 L, B5 R1 ~" C: W
+ @( s+ X% k1 P3 ~5 i4 E
(if (not (equal old_undoctl (getvar "UNDOCTL")))
+ q9 e& v7 ? a! `: S9 Y, q* h (progn
, D3 B5 }) v' l- C7 U8 n& ?6 { (cond4 n, Y: m) h- A7 C
((equal 0 old_undoctl)
- _4 @, N4 ^! [+ y3 {; _# } (command "_.undo" "_control" "_none")
( P+ ~: I9 w1 ?7 M; {2 [0 l. p+ L ). e; M# n- V5 t/ p+ {
((equal 2 (logand 2 old_undoctl))
0 F5 |8 Q9 v) T0 T N9 n (command "_.undo" "_control" "_one")
" n. l7 ^; D, o+ H) S r3 u ). r' n0 R) f$ |% |4 Y d7 F- Z
)
0 l! g. _+ t6 L2 Y# \ C (if (equal 4 (logand 4 old_undoctl))
! H- U- c/ L. `; ]8 ~ (command "_.undo" "_auto" "_on")
1 F* n9 J# [0 O3 b( P (command "_.undo" "_auto" "_off")4 u( S( L/ V8 L9 d5 e e# K
)5 y4 ?# @& A8 Y+ T, ?7 n5 h
)6 `( ^. E& M6 G' W4 y" |7 R
)
! ^6 A! Z9 n# g* S% g: W (setq old_undoctl nil)5 p5 l v0 Y5 A2 U5 L* ~
(setvar "CMDECHO" cmdecho)8 K) r/ l0 t1 x. E6 o* Q
)
" w8 k" f5 R* [$ `9 `) v ) _4 r- j( k- D) L `# j
); defun undo_restore
2 S8 w7 {: I* j+ o& j: c4 }) z, \+ r( y/ X/ w6 }! a2 ?
;----------------------------------------------------------------------------+ t7 l9 S+ k% o/ D/ n1 G$ H
; Save variables1 j/ v2 P+ u1 J I: Y# [) r2 z
;----------------------------------------------------------------------------0 L( u4 J4 f6 C
(defun var_save (a)
% M& L8 ], i7 z5 o; e (setq m_lst '())
- D9 ?: L. l& V0 A9 ?5 X: E (repeat (length a)! _) f5 Y2 X6 z7 l
(setq m_lst (append m_lst (list (list (car a) (getvar (car a))))))) j( ^& Z: f$ f" e7 x$ g
(setq a (cdr a))
0 ~$ B5 x, ^3 H& [2 L+ e )& G1 E. f7 |7 L1 w/ i* b
); defun var_save
2 @% p; E' M2 t8 E7 g0 f( v, r l# ~' F3 M+ g) ]! V& A L
;----------------------------------------------------------------------------, ^$ K4 S: G; r% B% O, U W, \
; Set variables
" Z& ^( i) X9 j;----------------------------------------------------------------------------8 _* f* s" p1 F6 v! m: a
(defun var_set (m_lst). }+ B0 I9 a9 `1 }4 d
(repeat (length m_lst)' Z8 P4 t9 `" i/ J) m' J$ {0 D
(setvar (caar m_lst) (cadar m_lst))
2 A) p: B7 Y7 v (setq m_lst (cdr m_lst))
" ]# M% }5 B. X0 q: G0 g2 {9 b, d )$ ~5 j R( W; `
); defun var_set
, n1 c, t" C! Q/ X- [
* G& `2 S! W W8 y;----------------------------------------------------------------------------4 `: N9 P8 S7 J3 O- ?
; Restore variables' k, I9 L. m% B5 m1 f: H: x2 x
;----------------------------------------------------------------------------
1 I" f0 k z1 W% x$ d6 k* T(defun var_restore ()
! Q/ c2 C7 P5 o+ D. L4 o (repeat (length m_lst), Q% {; X: w7 ]1 g
(setvar (caar m_lst) (cadar m_lst))
* V2 L1 [ q+ [ (setq m_lst (cdr m_lst))
" U# d' A4 i1 M4 G- h% C )5 {' {7 u/ R& W1 D" q* J5 f
); defun var_restore* V) A# h6 m" L. k7 o
& a' | |3 v' B' t. S( X
;----------------------------------------------------------------------------
5 X9 n2 A5 Y. @0 c# O0 q: v; Initialize routine 1 @: H3 y& ~+ t+ W
;----------------------------------------------------------------------------
. B: X: p/ X6 a(defun err_init(e_lst u_enable add_fun)
% M2 M5 x- x1 g! H) w* Z* U (if err_alive (err_restore)) ; To avoid nested call+ J5 I$ D8 |/ ^( y9 O
(setq err_alive T)
" r2 v- H/ }6 G" @, }3 j (var_save e_lst) ; Save the modes
4 u% m: b- A5 p5 V4 [8 x (if u_enable (setq old_undoctl (undo_init))) ; Initialize UNDO status
$ n; z8 X9 O. p% Z( T (setq err_old *error* *error* err_main) ; Save the handle of *error*7 d. o% ^+ R3 O$ f/ D% F
(if add_fun ; Add the user cleaner( [6 E' Z& c) T+ B/ C
(setq *error* (append (reverse (cdr (reverse *error*)))+ K# Q5 F0 l6 P4 q$ F2 Q
(list add_fun (last *error*))) M' \- c5 @: e3 d# s: i
); append0 h$ `# b8 n& }
)
- A) E8 g# V) g5 O5 X; m ): a) o, b C% W N# p1 X
); defun err_init |
|