|
![](static/image/common/ico_lz.png)
楼主 |
发表于 2007-3-13 08:33
|
显示全部楼层
undo.lsp
;;; UNDO.LSP made by piggy
0 m; u5 z+ A" m" };;;
9 C; n, h' H) j;;; DESCRIPTION
+ Q9 @$ v8 X) R# K" [;;; These are general error routines which can be called by
& q0 d e6 G( z! e' H$ Y/ d( |;;; other routine. See AutoCAD 14 ac_bonus.lsp for reference% x5 G6 u) M8 v( r0 J
;;;
- m, W, }7 S, t;;; SUBROUTINE INCLUDED IN THIS FILE5 ^2 X' ] n+ c3 d: P
;;; UNDO_INIT+ t* u4 c1 J6 G( R8 L4 i7 ?0 R
;;; UNDO_RESTORE
! R& E: ^# g0 i4 A9 E;;; VAR_SAVE / g0 o$ J( Z! C( D5 d" I
;;; VAR_SET 4 k5 B, B6 H; }& B& w
;;; VAR_RESTORE1 [5 t: Z0 t% @
;;; ERR_INIT
; X+ ?0 U5 W. x' t$ R/ _;;; ERR_MAIN
i& J" c) r' W/ b3 R( q;;; ERR_RESTORE
. u4 i( V \( P; v2 u: E- |# {;;;; E* ~% [4 h+ @4 a5 _ @) G
;;; DATE: 10/17/98; 03/31/99; |8 H; l8 W0 Z+ a
;;;7 Q- u, S# M+ d' d5 k
;;; HISTORY:
4 l; ^4 n* M q$ M; V& V9 T;;; Add routine of mod_att- q5 y$ X- t' y5 E# r
;;;
7 q: a2 z, H/ l6 ]& s;;; USING METHOD* B- a/ ^$ k. |4 c! a2 Z1 B
;;; ERR_INIT:. E/ a' H; X1 i
;;; This routine initialzes the error handler. It should be called as:
/ A e- T! f: a4 f3 @4 ];;;/ V+ N7 t1 O/ Z' ^: N+ J4 V
;;; (if (and (not undo_init)
* n- `7 E1 J" L& I' n1 b;;; (equal -1 (load "undo.lsp" -1))
( s! L6 Z( U; p;;; );and: F0 j' W6 S' I5 }
;;; (progn (alert "Error:\n Cannot find UNDO.LSP.")(exit))
r: h `6 i1 H4 T;;; ); if, E/ k/ c7 ^( y# _; O
;;;
2 I! w; V, {- O/ w& ~( z;;; ARGUMENTS:
: I/ z- A4 d% W. ?9 J- n) S% C;;; err_init Takes 3 arguments.
. I7 _; G! W+ u1 I, q;;; 1. - The first element of the argument:5 d: H2 p4 B+ c
;;; This is a list of system variables paired with
9 e9 ?( H) g. _3 ]( S( B% i% H0 v;;; the values you want to set them to. i.e. '("CMDECHO" "ATTMODE")
. q2 T3 K; N% O;;; 2. - The second element is a flag
E! r2 J- s- l7 {) d7 w( c;;; If it is true, then in the event of an error ; r! w L0 J3 R- u, Q+ m6 l
;;; the custom *error* routine will utilize UNDO
+ A$ v8 e3 z1 Z5 I/ n" h;;; as a cleanup mechanism.- r# l7 @# Y# l- L0 l: r0 P3 E! |
;;; 3. - The third element is a quoted function call.
, u* r. }* j5 s9 b3 \;;; You pass a quoted call to the function you1 x! S0 t% w/ b4 ^7 ]9 D' T/ k
;;; wish to execute at the end of nomal routine if an error occurs.
; z+ x4 ], k+ r q) a+ A2 U;;; i.e. '(my_special_stuff arg1 arg2...)
/ V W1 S+ k! |0 U;;; Use this arg if you want to do some specialized clean up
3 A+ B6 Y9 d, H1 b( v/ p;;; things that are not already done by the standard bonus_error
. ]0 T# N/ }* p) v; O& A;;; function.
& u) R0 k7 Z7 e. ?;;;
2 R# u* ]4 D- I; U4 u;;; ERR_MAIN: Body of error routine
7 R9 v! u W, ?' L. ~ t;;;2 j5 v! ]! z6 e* Q2 |/ U4 u+ }
;;; ERR_RESTORE: This routine should be called at the end of command to0 ]8 U7 \ t. p6 |+ E
;;; restore the VARIABLES, UNDO & *error*." |6 ~" u4 k$ [3 d6 N
;;;8 c2 ?( c X) `( x4 E) L+ O, g' u8 l
;;; UNDO_INIT: Initialize the UNDO status, ~) V& X7 @! b1 w0 w; `
;;;0 x) I/ K6 s" _7 i8 c4 i3 s1 U
;;; UNDO_RESTORE: Restore the UNDO status9 c9 {" C9 G" d% ?" J9 u9 b
;;;4 P, D D# k0 v
;;; VAR_SAVE: Save the variables. the argument is like '("CMDECHO" "ATTMODE")% B& D0 |4 _( I# @9 E! t
;;;
( F g p+ N; T8 g' F3 k& F& K;;; UNDO_set: Set the variables. the argument is like2 L. J" ^, [$ h7 a
;;; '(("CMDECHO" 0) ( "ATTMODE" 0))
. |8 _" J. G% c;;;
5 O c$ P0 C) l6 T;;; UNDO_RESTORE: Restore the variables
/ z2 ~4 ?# N! U;;;
5 A j3 B8 M: g* b: ~) M4 C;----------------------------------------------------------------------------/ S3 Y3 N6 R0 [" I- C
; GLOBALS:' z8 g7 g; p9 E" u- A- B0 @$ C
; old_undoctl -- old status of UNDO (voided by UNDO_RESTORE)
' e3 p D' r* r$ t, A \& _* `" F' O; m_lst -- list of variables (voided by VAR_RESTORE)
2 B4 p$ C+ r- S1 S$ N' z; err_alive -- indicate error routine is active (voided by ERR_MAIN or
8 P! C9 h; [. t; ERR_OLD)
- [0 N" ]4 b$ r% q/ a; err_old -- old handler of *error* (voided by ERR_MAIN or ERR_OLD)3 `5 G% H4 B2 i+ S& v. K+ T
;----------------------------------------------------------------------------' k# O: U, Y$ j8 S2 [9 i3 H" y
% l# p$ A1 A6 I& s" y;----------------------------------------------------------------------------
! Z* r# X4 k* T- |; Modify attributes according to entity name, attribute name, dxf_item
+ ?) O" O4 U, T& q$ F; k6 p;----------------------------------------------------------------------------
: S7 J& `7 `: \ F(defun mod_att(ent id dxf_item)
: k0 Q; o t: d8 R! f+ d" e (while (and (/= "ATTRIB" (car (entgetf '(0) ent))) (/= id (car (entgetf '(2) ent))))& W& P; `0 x2 N* O5 W( a
(setq ent (entnext ent))
3 H* R' m7 y# l1 u+ n ); while 7 M8 T) e; O8 d0 }" N
((lambda (x)
. {: ]' ?- y0 w- J4 x1 ?- v (mapcar '(lambda (y)
" w$ a! P' J% @6 V! T; u (setq x (subst y (assoc (car y) x) x))
( E9 ?/ K" x9 I, X/ n8 f ); lambda N/ Q1 A( J, t1 V7 i' p9 `# q% L
dxf_item, D: V% h: t0 N# \# D: t
); mapcar
4 r7 }& e" @& L0 z (entmod x)
1 ?& f9 b! ^7 `& P* k5 @4 V (entupd ent)
7 S( T- K6 F7 u( l% a ); lambda
, N8 Q& r! }! g3 y" t( w1 d (entget ent)8 r" q2 x# E' ~
)
6 d# W5 m3 @, m- g. t5 L% @); defun mod_att
" H9 b3 {, l4 R9 V3 R# T8 N6 D! @
;----------------------------------------------------------------------------# [; O1 V; ~5 J# D8 x
; Check layer status, return a association list which contains layer information
7 j t, `& H3 ~/ \: `: r;----------------------------------------------------------------------------
1 ~. O+ c) U; y* i0 L. t(defun laychk(lay / l_sta)4 Y1 r/ s; W+ Q3 L: m$ i( f4 q
( (lambda (x)
# T5 D# {6 K5 [% e+ y' C/ [; m (if (not l_s)) U8 u8 U. M& z1 Z% m9 l5 w% N9 M
(setq l_s (list (cons x (logand 5 (cdr (assoc 70 (tblsearch "LAYER" x)))))))
" T% K( w1 E0 t. j1 [! g; T3 i ); if8 b! Q5 W2 ]8 X: h
); lambda : t9 j: p+ p! a2 [- _
(getvar "CLAYER")' t7 A7 b; o2 ]; f7 ?
)0 u7 A9 K' K/ P& H
(if (not (tblsearch "LAYER" lay))! G9 b5 O- ~% _$ [5 H; D K+ `
(progn - x& E/ n9 S: H4 D& [
(initget "Yes No")4 d1 l- S2 H. [( Z! D. O' C
(if (= (setq l_sta (getkword "\n图层不存在,是否建立该图层 ?\(Y/N\)")) "Yes")! V! p( Y- g% G( o8 P( i2 E4 T
(progn
9 G3 n' [, y8 k2 F0 T (command "_.layer" "n" lay "")7 M1 |1 {9 B$ a5 n" S" n
(setq l_sta 0)
6 q0 M {! r1 {9 }5 A ); progn" v4 G& ]- i' V: S& I6 b
); if- n1 U# Y( n, `
); progn9 e, P' s" R: C+ u, p
(progn
7 a7 K" e* @) D4 o; O" P (setq l_sta (logand 5 (cdr (assoc 70 (tblsearch "LAYER" lay))))), g2 q& a1 y. i2 {; }# A5 L
(if (= 1 (logand 1 l_sta))
, q) U1 ^, `2 N; b/ I (progn
+ R7 H7 H3 f% h! ~" l& v4 I (initget "Yes No")" A, m* d& m% S0 }# t
(if (= (getkword "\n该图层被冻结,是否解冻 ?\(Y/N\)") "Yes"); O: M S t7 B8 N9 {( f+ }4 S
(command "_.layer" "t" lay "")3 \1 y/ R5 N; Q; s
(setq l_sta "No")
/ L) G: Q" x6 {3 f1 ? ); if
" j \: h. V& @/ V ); progn# d/ Z! `9 b# E. P \
); if0 |* N; r- G# ~# k! i. M0 P8 I' @
(if (numberp l_sta)
2 ^5 A. q+ X N: p+ z0 g8 U, M (if (= 4 (logand 4 l_sta)) (command "_.layer" "u" lay "")); if
: t, ^' Q0 ] e! v! [9 f, o# f ); if
5 s1 f( [( C4 A1 L. t ); progn8 |$ J0 a2 y8 Y1 I! z* l* f
); if
9 L$ B3 i* |% k' |$ C3 j$ o- I ( (lambda (x)' {$ T4 W% I/ u" i8 \6 W1 C$ [
(cond & P( L8 w& B- @- t9 ^& O
( (not x)+ ~, b- e W% }' N# A; r8 ^
(setq l_s (cons (cons lay l_sta) l_s))) n. `1 Q* Q% ?4 z2 r9 f& N8 w
)
. N' Z% h& B% \7 r" b ( (= "No" (cdr x))
4 }# l& v, W- v) H4 [" S (setq l_s (subst (cons lay l_sta) x l_s))/ g L6 e4 v% i) `# B& s
)5 D! z f4 W0 z/ z
( T l_s)" {: i {0 ^( z2 O* \. u C
); cond7 ^# W0 N3 I1 q f
); lambda0 h2 {( D* S) h& U' E4 j; J3 H
(assoc lay l_s)
" t+ c: K$ Q7 o! r% M )
4 Y, U4 c9 b# Z) H# |# m. z: s4 \1 x$ D' N
); defun chklay
8 D9 `1 ^% }3 _. p }6 P5 w' f- b
;----------------------------------------------------------------------------
. b+ N+ I/ H& |& h5 |; Restore layer status according to association list l_s
( ~! t2 U5 E. X+ f;----------------------------------------------------------------------------4 N o9 ?% q% U" O' X [% y
(defun layres()
( b# N9 {7 `- E! i: z0 S (setvar "CLAYER" (car (last l_s)))5 \' Z7 `: o0 m/ R
(repeat (length l_s)* o+ C3 L0 i; U! w4 z
( (lambda(x)
1 x& q2 K; l- k- x U (if (numberp (cdr x)): o# P9 Q! g7 r- M; O& p- L! _4 L0 N
(progn
- _2 d# J$ ?& T4 Z4 e J (if (= 4 (logand 4 (cdr x)))
8 Y4 a. H- ]+ k6 q A4 H P0 h (command "_.layer" "lo" (car x) "")# o \* t- D$ j) T
); if4 w3 ^3 J4 p& R0 n
(if (= 1 (logand 1 (cdr x)))
+ @6 k5 R0 e9 a4 d (command "_.layer" "f" (car x) "")# q0 m3 u; C* |5 S: w1 N' f
); if
. @- Y/ a! A6 r& A3 g1 o5 J ); progn7 i( G/ C: e( `/ S3 ^
); if
, I4 @( t/ P) s5 Q2 [& S+ D ); lambda1 C& ]# M/ V$ c& ?2 h, \0 V$ y) F
(car l_s)0 T7 @) ?+ Q$ ^6 y l. N, M1 x2 Q
)
- S5 J) V, M( ~, u (setq l_s (cdr l_s))/ Q+ W8 O4 R" N" L7 o
); repeat/ `& B: a) I7 \
); layres. e5 I: k! o, D% k6 _# Z
2 i* F. r+ W C. S
;----------------------------------------------------------------------------4 {- w4 n8 X0 R' I8 d
; Get DXF codes
9 h5 O8 C+ n" x8 C: w;----------------------------------------------------------------------------
2 H) r1 `! @: k; [(defun entgetf (index ent)
v2 G* H! m, N+ H+ z# }2 V | ((lambda (e)$ c, N5 y2 x/ s+ a& G3 y
(mapcar '(lambda (x)
; F+ f: z% }+ W% X0 g. v3 k (cdr (assoc x e))
9 i9 q; p. ~: [9 C2 F6 Y& S) f' {2 a ); lambda
! f: H' I7 Z" S/ |# E" I index) ; internal lambda function
- W' j- q6 I5 e: x ); lambda" ?9 C( {6 {; G0 N7 |' F8 `
(entget ent) ( G6 ]2 @$ m% z% v# T
)+ E/ G8 M3 c& J8 T4 J: l' q
); defun entgetf
3 q- F4 ?- s. V' C$ E7 V9 }, O% n- Q* a( Z/ T$ T( E. X
;----------------------------------------------------------------------------
' c# O# y0 a! K' {1 {; Save UNDO status+ Y# x5 r( [! C5 ]& r9 H
;----------------------------------------------------------------------------9 A ] x8 {) {: v; ?$ g$ Z
(defun undo_init (/ cmdecho undo_ctl) ! F, V3 Q0 C+ x3 O6 s& ^6 S7 |% y
(setq cmdecho (getvar "CMDECHO") undo_ctl (getvar "UNDOCTL")) ; Save the value7 X# z7 Y& M( | Q5 J
(setvar "CMDECHO" 0)
; k) p1 \5 d# K. ~1 Z. f
- e! u! Y& `* O9 E (if (equal 0 undo_ctl) ; Make sure undo is fully enable
% R- W5 r% \7 x3 R' a L3 c (command "_.undo" "_all")5 G/ A& M5 y( |6 S% c
(command "_.undo" "_control" "_all")' l H3 f, ^# o6 c
)3 R3 B5 E* u% q# p; O2 }
! U8 Z" H/ W( b7 k' |, L (if (equal 4 (logand 4 (getvar "UNDOCTL"))) ; Ensure undo auto is off
' F3 j/ J' W5 v8 O (command "_.undo" "_auto" "_off")
' X* A0 I( l% \5 e& H# V; b* i ). j+ k' `7 T+ @# Z2 o& R1 f, K
: E0 z6 u; {% Z: u
(while (equal 8 (logand 8 (getvar "UNDOCTL"))) ; Place an end mark here6 H- z* O# ^( l: h6 m
(command "_.undo" "_end")
$ P+ G4 K, c+ X )
, J7 c( ^5 D L! E0 S( l8 n/ D# D# t( x. D. \# ~
(while (not (equal 8 (logand 8 (getvar "UNDOCTL"))))
- y1 Y: o; w7 c" z; j* m9 _% H. l (command "_.undo" "_group")
: d7 L) [. }$ Y9 r )
* q9 a7 s6 C$ W& ~# o" k
6 f4 a" `7 q. `4 R' Q! ~ (setvar "CMDECHO" cmdecho)
% w" \/ {. Z8 B/ C& _ undo_ctl/ j% ^0 N% X7 F# ^; }
); defun undo_init
2 Y v2 n b- @" f2 v4 I( s5 o& k0 c* O5 n0 c$ p. [
;----------------------------------------------------------------------------$ i+ H3 q: Q; u% E
; Restore UNDO status- u* ?* ] c; g- \2 n$ O- M1 t
;----------------------------------------------------------------------------* `4 |4 B$ Q3 o* Z' ?
(defun undo_restore (/ cmdecho)
8 v; P$ ?2 O% |& V2 y( E (if old_undoctl# U6 G: x& M1 v! \ q
(progn# w5 z: D9 `8 Z) i
(setq cmdecho (getvar "CMDECHO"))
7 H" F) B' Y4 I( s% p (setvar "CMDECHO" 0)
" Z+ q% z( u4 n* Z
2 M1 Q- u; u& H" m" { (if (equal 0 (getvar "UNDOCTL")) (command "_.undo" "_all")) 3 H5 d) n( W/ H& D& D8 g/ J
(while (equal 8 (logand 8 (getvar "UNDOCTL")))% |7 j& j+ C( S
(command "_.undo" "_end"): z& s. ~$ @5 `
); while
3 x4 ?$ R1 T' q2 Y K
2 M$ N- s. m: x6 v/ d$ q3 y& U, y; Q5 {5 l
(if (not (equal old_undoctl (getvar "UNDOCTL")))' l2 E. c3 u, P7 d" ~% o
(progn1 f- d4 Z' p! H
(cond
7 N/ V# t8 S5 M8 r `" G( O# o$ |1 S- l ((equal 0 old_undoctl)% A J/ V. f1 ~8 L
(command "_.undo" "_control" "_none"). i }+ j$ B8 p2 N( U. u
)
# z) Z0 x( n h9 D( q ((equal 2 (logand 2 old_undoctl))) q- z' f7 W& X# W5 i8 j% C
(command "_.undo" "_control" "_one"); }# Z& t# `7 e, x( \% R6 Q
)
5 ?* ?2 s. @: k* L )2 m0 m1 }) b$ ~/ R
(if (equal 4 (logand 4 old_undoctl))/ l4 R* s' |$ K. d
(command "_.undo" "_auto" "_on")2 d! N4 ~3 a, S) k; K# Q
(command "_.undo" "_auto" "_off")2 h! _6 [$ Y# ]. Q V
)0 N1 V& I- B" u9 L
)
1 w8 j: h# @- p! F* m& c7 H& X+ } )) g" X% {2 d8 R: e5 w- u5 e$ _
(setq old_undoctl nil)# P" o5 w0 z5 w! W, |) s' t' R
(setvar "CMDECHO" cmdecho)7 g- W" h3 K. x( P& _" _
)
& ^2 |& y* m2 R F )
2 |% D9 t ]8 Z9 L- `8 C: ^); defun undo_restore
, H7 @- I6 T. D! B, u
6 x# _8 Z( J( w;----------------------------------------------------------------------------( }% B3 N# C6 S. r
; Save variables
$ r1 d5 {6 u( R9 F& k;----------------------------------------------------------------------------
8 ]8 Z. ?7 |& s- P$ g( K, B(defun var_save (a)8 r+ c4 R" H( t) m# @
(setq m_lst '())
& O! I3 F. X$ C) ? }( s (repeat (length a)
4 @$ b/ y3 L) g5 y, v) e (setq m_lst (append m_lst (list (list (car a) (getvar (car a))))))
4 x$ ]5 \0 n8 G6 u, }% M* B) { (setq a (cdr a))) h8 j. b) d! A8 Z; b/ \. j
)( Q$ t5 y4 D7 b7 J7 H: g
); defun var_save
0 g( V4 S: W: p- e4 I2 Z2 V# ?, ?5 N5 N: ~
;----------------------------------------------------------------------------
8 ]5 Y& E# ]% z# X* f# }; Set variables# I# `" k- l; n' I' R- W
;----------------------------------------------------------------------------
6 I. y5 F# m3 x$ B" `: q% O(defun var_set (m_lst)/ z$ T, H* i4 O+ p( u& t: T! \
(repeat (length m_lst)- n/ M% ^2 f! Z7 w
(setvar (caar m_lst) (cadar m_lst))& Y" s3 E! P0 J- D6 J9 G$ M& I
(setq m_lst (cdr m_lst))
; F: r( e M6 y+ f1 X( I1 y1 ] ); @* p* w9 |5 _, }0 a5 {0 ?
); defun var_set/ p1 s; U5 R i" l6 |
$ x8 [/ B: u+ s: J' z0 b
;----------------------------------------------------------------------------* ?# v% l: ]# v( F2 b
; Restore variables
! D3 h' ~' t5 D5 M5 ?4 |9 C;----------------------------------------------------------------------------! m8 Y9 }0 o5 z
(defun var_restore ()/ p' D3 G. N; L' c8 w
(repeat (length m_lst)
" L5 l) l; G4 _' d% @ (setvar (caar m_lst) (cadar m_lst))/ c0 K4 O' d+ \9 E2 J6 Q& c+ n' U- t+ P5 h
(setq m_lst (cdr m_lst))
# L& M% k0 T/ J) k' o! _ )7 ]5 X" F5 u* V4 s
); defun var_restore
/ F) R, C. f1 E$ M. Z0 V
; e$ a+ w+ y4 t4 p+ H;----------------------------------------------------------------------------0 O) b1 a$ g0 W9 N
; Initialize routine
, f' k: ~9 M: i" D) F5 E4 x8 t, T;----------------------------------------------------------------------------8 Q7 x8 P" H3 |3 Q( C) {& y Y( w$ L
(defun err_init(e_lst u_enable add_fun)
$ x: y" z! }0 V (if err_alive (err_restore)) ; To avoid nested call
/ g8 a1 t) y' X6 \5 j! ^' b. ^- Q (setq err_alive T)% m% p2 F4 \/ v& T
(var_save e_lst) ; Save the modes) J; w: a/ D9 k+ M, X
(if u_enable (setq old_undoctl (undo_init))) ; Initialize UNDO status
/ N. k _7 H4 V: A' k8 z* T (setq err_old *error* *error* err_main) ; Save the handle of *error*
! W3 ~$ n: \0 x: k0 ^- O (if add_fun ; Add the user cleaner
0 Y" v4 b9 `5 R ~9 N$ i (setq *error* (append (reverse (cdr (reverse *error*)))
+ y5 l9 N* ?$ Q( [& O- Z (list add_fun (last *error*))+ B# h# Y" H+ t I) U+ X
); append2 e Z! d9 \1 O1 d/ L* I! f
)% R, E) U2 Q4 ~$ Q6 @1 W
)
; Y6 ]/ _9 R. q- X" J); defun err_init |
|