|

楼主 |
发表于 2007-3-13 08:33
|
显示全部楼层
undo.lsp
;;; UNDO.LSP made by piggy% O$ f, X- r6 e' i# K: h6 v0 ]
;;;
& d0 D2 j% a8 ~& K: S Z;;; DESCRIPTION
O& D- f; H* f7 y0 ^9 [/ c0 ~: H;;; These are general error routines which can be called by
0 t$ P- Y5 L% f" k4 ^;;; other routine. See AutoCAD 14 ac_bonus.lsp for reference7 \- s5 A* G3 Q# l. a
;;;
: F- x# |! {9 A! t% q7 `* s- m9 O( r;;; SUBROUTINE INCLUDED IN THIS FILE/ H C1 O$ u: n# Z$ I$ c
;;; UNDO_INIT
' C1 N& a2 c% F7 W; T;;; UNDO_RESTORE
6 W: M9 O, b: e. e M; Q% x;;; VAR_SAVE
4 y' V% d2 k6 h( ?$ m. e;;; VAR_SET
2 |' |# R7 a4 j! ~# a- s;;; VAR_RESTORE
, y0 G5 p$ H+ G- H4 w+ s: ?4 f" S _;;; ERR_INIT
7 q5 D/ w; I, \1 [# i;;; ERR_MAIN
8 v5 h3 }' u) j5 c;;; ERR_RESTORE! H) g/ K, a2 q' a4 e
;;;3 [4 ]- p# m C) y/ }2 i
;;; DATE: 10/17/98; 03/31/994 ~2 s% ~2 l$ @- r+ d
;;;
8 h" u) e! @ }0 |;;; HISTORY: 5 j A1 O. i) }# T2 s
;;; Add routine of mod_att
Q4 Z5 e4 K" L* N, ~;;;
4 G) H! x0 {& N8 ~! j! s2 O;;; USING METHOD
/ p6 R9 E. F3 c0 A/ A;;; ERR_INIT:/ \/ M: _2 M+ T9 w7 T5 j# W8 Z
;;; This routine initialzes the error handler. It should be called as:2 ?& j- q% b. W& `2 l1 b# g
;;;4 I7 ] G/ c7 G
;;; (if (and (not undo_init)
8 t# L, i: m$ A+ S: n( f( u) X;;; (equal -1 (load "undo.lsp" -1)) # U; p( y4 J! U7 C1 R: e6 k2 K) H: |
;;; );and
/ {/ q1 v" A! f& ^/ `;;; (progn (alert "Error:\n Cannot find UNDO.LSP.")(exit)) \8 i; f9 B) U& R0 A" Z( \6 l5 ]
;;; ); if4 k I* A* J) N- @* O
;;;; Y( e3 _2 t1 C! ^- J
;;; ARGUMENTS:$ t* A4 g% `5 G3 H2 x9 A
;;; err_init Takes 3 arguments.
$ Q+ ?- y' Q8 \; D2 |;;; 1. - The first element of the argument:
* ^" T, j8 A2 D9 D! v0 [;;; This is a list of system variables paired with9 q5 z; }2 @& @8 \, ]% z1 k& h
;;; the values you want to set them to. i.e. '("CMDECHO" "ATTMODE")
0 c0 o% b3 X. h1 c6 [, o;;; 2. - The second element is a flag
4 ?" r9 C) y) J& k8 t;;; If it is true, then in the event of an error 2 P' U3 \8 j' R. O8 a
;;; the custom *error* routine will utilize UNDO
) z- I. n9 M% |+ A;;; as a cleanup mechanism.
( K! }. y* F& V4 P' h8 ]$ T;;; 3. - The third element is a quoted function call. G! e% x% l6 i5 c$ Q9 g% [
;;; You pass a quoted call to the function you6 t3 U+ W( t& l5 S
;;; wish to execute at the end of nomal routine if an error occurs.
1 Q; W7 k$ \! S;;; i.e. '(my_special_stuff arg1 arg2...)8 j, w, s, V( h9 p4 r
;;; Use this arg if you want to do some specialized clean up
+ V" H$ G U$ o: E# t( t% c2 s1 g;;; things that are not already done by the standard bonus_error * d! J' S$ U" @
;;; function.) v! k7 h( N4 p2 A4 N
;;;
9 Z3 s; c" f9 u0 M/ F) g;;; ERR_MAIN: Body of error routine! G5 V/ n$ P! t' ?2 r6 a
;;;4 Z* M9 a! O% ?9 D' b T
;;; ERR_RESTORE: This routine should be called at the end of command to7 A9 n# w8 Y) [) s
;;; restore the VARIABLES, UNDO & *error*., X7 m w$ _0 @0 T }9 b4 L
;;;
( P( u6 U5 R8 h;;; UNDO_INIT: Initialize the UNDO status# J6 m7 }6 `: P8 ~! n" f8 G8 _
;;;1 `/ L% O# ?; p+ l/ @1 I
;;; UNDO_RESTORE: Restore the UNDO status9 m+ Q9 l7 c( R" Z q
;;;
' D5 `( l( J4 z. i( |2 Q;;; VAR_SAVE: Save the variables. the argument is like '("CMDECHO" "ATTMODE")
6 s4 r# ^' N# _$ g0 n1 `;;;
# ^0 j5 Y$ w1 x* S+ e6 J1 ~! O B0 \;;; UNDO_set: Set the variables. the argument is like
( G! v3 X# [: C. {;;; '(("CMDECHO" 0) ( "ATTMODE" 0))
+ j) i4 p% ~0 h- {;;;
- b; r* P, t+ x# c+ M6 L; w3 i3 Z! q;;; UNDO_RESTORE: Restore the variables
0 G" J- R0 S: r: w% s0 b; z; L1 X2 L;;;
! i' B# d: r1 T( g;----------------------------------------------------------------------------
( t5 x6 ~# W0 V* j' h; GLOBALS:
; o7 P/ c- l- O; old_undoctl -- old status of UNDO (voided by UNDO_RESTORE)$ Z, [8 @# \- I
; m_lst -- list of variables (voided by VAR_RESTORE)/ D8 F- X7 ?, j* T. @
; err_alive -- indicate error routine is active (voided by ERR_MAIN or
( p3 Z0 W5 _5 f+ H# S: x; ERR_OLD)( W, X: T/ n3 E; k
; err_old -- old handler of *error* (voided by ERR_MAIN or ERR_OLD)4 ~! W& W0 Y6 p! n4 b
;----------------------------------------------------------------------------
, K6 M3 m1 m3 i m# |
6 k6 Q6 `1 w- p" P;----------------------------------------------------------------------------
v( K+ q7 U& }4 k2 y; Modify attributes according to entity name, attribute name, dxf_item
# b( C1 m* C' B# Y7 o2 \+ o0 f! K v;----------------------------------------------------------------------------
; @' ^" M! S) A2 j(defun mod_att(ent id dxf_item) # {$ R5 C3 w# N' G9 H
(while (and (/= "ATTRIB" (car (entgetf '(0) ent))) (/= id (car (entgetf '(2) ent))))
7 e; Q5 G! i' ]: ]3 S) f* q5 i (setq ent (entnext ent))) b+ R1 j7 m0 Y) z& k
); while
# X/ s, E0 F4 L7 y! B/ B$ [6 O ((lambda (x); Y0 i y* Y) e$ x7 Z( Q
(mapcar '(lambda (y)6 v o2 G g1 J6 K. O3 u3 ?: n
(setq x (subst y (assoc (car y) x) x))
# h, c7 d; y# |* Q& M, f ); lambda' P, [ j9 x$ j; T! @
dxf_item1 [8 A- _1 _. V# {4 ]1 U; f" j8 u
); mapcar( A3 B! a- K" P
(entmod x)2 R" ]/ d$ p- e) e6 M( J4 k' f( c$ u+ }
(entupd ent)5 e' B, o1 m1 {2 X
); lambda6 u6 ]" w4 @4 z( ~
(entget ent)
* X$ S* u# k8 Z3 I9 ], n7 ?8 Z! i )
* G# a. V6 t# |) E2 V7 ~7 l; s( i, y1 B); defun mod_att
0 |6 g& }9 O6 D/ P
: }' [1 Z- U1 ? J7 x1 V;----------------------------------------------------------------------------; t/ O! u* o8 u E3 p
; Check layer status, return a association list which contains layer information
) b, X* z7 x! I# i3 Z! T;----------------------------------------------------------------------------" i) O7 K( Z2 z, F
(defun laychk(lay / l_sta)9 d8 ^1 D- R% R
( (lambda (x)& [. Q N( m' F" ~. M' T
(if (not l_s)
. @* g. y: i; v1 z. L$ }# _7 ]3 ^ (setq l_s (list (cons x (logand 5 (cdr (assoc 70 (tblsearch "LAYER" x)))))))$ d/ L4 X- U. e, q2 d2 ~# t5 Z
); if. d9 @$ P" \" T6 E) T3 e
); lambda
; [' s% A4 S4 B0 j7 Z (getvar "CLAYER"): k2 K6 x. ?; G$ A5 _
)
" Z# I) |/ v# z9 @ (if (not (tblsearch "LAYER" lay))) P: Y+ j$ u9 E$ z; O. ^/ j
(progn
" p7 u: X/ R ^3 `% r: W8 u: A (initget "Yes No")3 D6 u* c8 f* F/ k d- N" y: r
(if (= (setq l_sta (getkword "\n图层不存在,是否建立该图层 ?\(Y/N\)")) "Yes")
7 U6 ?% w3 |5 v+ ]; H/ h (progn! m; u/ `" p. _1 G4 `2 S6 c0 }
(command "_.layer" "n" lay "")8 F( ^8 O$ Y) T
(setq l_sta 0)
' |1 E4 J3 G; Q! a4 a ); progn7 ]. `% w( V0 f
); if
$ ^) |( N# ]8 @5 }( c ); progn" `) d, A8 Y7 K, }+ d. ^
(progn
/ f4 ]$ j6 t: d# G (setq l_sta (logand 5 (cdr (assoc 70 (tblsearch "LAYER" lay)))))- H" u3 e. m/ T0 y+ o- Z# v
(if (= 1 (logand 1 l_sta))
9 B* z3 C, @# k3 f (progn
1 \" A& e: r4 f L& M (initget "Yes No")8 |4 D# h; p3 E
(if (= (getkword "\n该图层被冻结,是否解冻 ?\(Y/N\)") "Yes")3 f% @$ W2 a+ h8 F" q* p4 d$ ^$ f
(command "_.layer" "t" lay "")
% Y( Z9 I* @; f& I- s* Z (setq l_sta "No")
- R$ V/ L7 V% Z7 N: L0 ?1 ` ); if
& }2 h# w- r9 R, p& |$ J* r ); progn
1 E6 V$ ]3 a; E7 ~8 I ); if
! y- C- F" \$ e (if (numberp l_sta) 1 T+ f( a+ T" ^# G! h
(if (= 4 (logand 4 l_sta)) (command "_.layer" "u" lay "")); if
7 }! u& |( F4 v1 W ); if" i4 ^1 A: k! T, t8 F
); progn3 v0 ?+ V$ |6 B8 k- {8 k
); if$ h+ d# ?, U& I+ H m
( (lambda (x)% |: Q0 s$ N, d6 W/ c/ D" O
(cond
: n: P2 R, ^+ Z2 f ( (not x)
8 O; k2 x. N5 X) c' j (setq l_s (cons (cons lay l_sta) l_s))
/ S9 }7 G3 Z1 w& W5 N; U )
! e* t9 @4 S# y# w* O0 X3 N. O ( (= "No" (cdr x))* \3 b' h8 n; E
(setq l_s (subst (cons lay l_sta) x l_s))
' O8 T# h# B: `, ]2 k c( I )0 {8 D% m1 F; M/ B7 V" B
( T l_s)) i. c+ q1 Q0 K% R
); cond' t1 u. ] ~6 h- @7 G
); lambda
/ j. W. z& {! d (assoc lay l_s)" x& F; s E ~$ {: y e
)/ W, q* G$ B; u7 i2 X! r
2 y ]/ ~2 B$ R; i! b9 c7 S
); defun chklay% L6 V8 d4 g1 Y0 x2 p' \; a
- y. o) O. P \* a6 P' }7 Q;----------------------------------------------------------------------------
, c; s* a$ H$ _6 F3 F& X8 y; R; Restore layer status according to association list l_s! c9 \7 j4 W0 Q3 u' O4 w6 s3 s
;----------------------------------------------------------------------------1 N' E- c1 s$ X7 R$ m1 Y
(defun layres()2 S; |! f4 J4 r2 q3 p8 H* F
(setvar "CLAYER" (car (last l_s)))' P# z9 [& T# Z
(repeat (length l_s)8 q, A7 K; m" A% v5 { w$ m3 C6 U
( (lambda(x)
5 |0 |' z" [6 L, [8 c (if (numberp (cdr x))) b7 ~; R" G; U n" |
(progn ~# W: B, C4 n* k' W& M
(if (= 4 (logand 4 (cdr x)))8 u* B$ f3 x9 d9 ~1 c
(command "_.layer" "lo" (car x) "")( h; }6 {+ x6 [6 U% v3 k8 {& Y+ i
); if
% ]' E }& l( s8 v- [* u; s (if (= 1 (logand 1 (cdr x)))' j m% n, b0 J* m0 X" X
(command "_.layer" "f" (car x) "")8 E# ^: S: }3 H
); if
3 ?9 d8 i' F) @& O ); progn$ ?) a7 j" x' z0 v+ I/ w/ x( b [
); if {. X4 d" ^) O/ x: z1 E* o6 ~
); lambda
* _! b$ }& e- |* b# D2 h (car l_s)0 d# u0 w6 W8 [8 n6 i2 s
)
! S1 s4 ?1 Z- f) ~ (setq l_s (cdr l_s))
6 F( ]' Z* a6 z" O, u4 p L( x$ P ); repeat
; P5 \: X# E. t: Q+ M2 H1 e- P# a); layres
+ `7 U5 ]+ b7 L9 H# [1 I6 W' f9 E( _1 K% @
;----------------------------------------------------------------------------( r& m# O+ e* J8 d: E! g
; Get DXF codes
+ R6 e0 ?" l. C& ^2 T0 @;----------------------------------------------------------------------------( E3 @+ H! n" k& o8 z
(defun entgetf (index ent). q9 k+ f$ b G2 P$ f
((lambda (e)
' y; v4 t1 }4 B9 ~' K (mapcar '(lambda (x)
3 L# X: Z2 ~" U1 j+ x; e (cdr (assoc x e)), K: i" V! Q/ s* i
); lambda( U; X% O7 z' J' w( m( V6 I) n3 G/ |
index) ; internal lambda function
/ Y* ~$ p% U7 V# r8 s# E ); lambda1 m) z7 u% v. _ Q
(entget ent)
1 v% J' j) v6 c. c0 S+ F )8 S0 ]1 X. T: t7 e$ W
); defun entgetf
; |7 f/ V+ E4 W* V! ~+ L: Z1 A( I' k. w* F4 j3 V
;----------------------------------------------------------------------------
/ N1 q- ]4 p$ F1 c8 \; Save UNDO status8 ]' W ~6 D/ j8 \" u0 Q
;----------------------------------------------------------------------------
6 K" k! K: Y y6 K% y1 q(defun undo_init (/ cmdecho undo_ctl) 6 g; X9 t( Y2 z# F' ?
(setq cmdecho (getvar "CMDECHO") undo_ctl (getvar "UNDOCTL")) ; Save the value
* W/ P& o3 W+ E2 D: B6 ?( } (setvar "CMDECHO" 0)3 v2 r6 \2 O3 A5 O6 b2 D
9 F, x# w) T' p0 _
(if (equal 0 undo_ctl) ; Make sure undo is fully enable
& K7 K) W5 ^: a* s* W+ J (command "_.undo" "_all")8 y3 l- G2 }1 [9 ]2 n, Z" }
(command "_.undo" "_control" "_all")" X" I% J; w s1 Y
), B5 ^0 O! j% }$ |
& u6 q- B& y7 A9 }/ a. h7 a% W6 S* I (if (equal 4 (logand 4 (getvar "UNDOCTL"))) ; Ensure undo auto is off% C" w% W: L( ~
(command "_.undo" "_auto" "_off")9 R/ z0 f8 O$ G- ^0 _2 M
)+ T# [$ t9 x: `+ E
7 r% I# ?: z/ @3 w& S) b (while (equal 8 (logand 8 (getvar "UNDOCTL"))) ; Place an end mark here
# w3 I9 s8 s ]9 i+ d- v8 |5 Q (command "_.undo" "_end")
6 j* J7 q4 c/ |, |/ i1 h# M4 C4 Z- B ), @( W+ Q8 b: h0 `
i/ H: t) O) z9 B9 m (while (not (equal 8 (logand 8 (getvar "UNDOCTL"))))6 w9 F) h( g3 a
(command "_.undo" "_group")
$ ?) d Q8 B7 u* _ )! |0 }( d. B% V+ c
2 j" l4 [, f5 y7 g (setvar "CMDECHO" cmdecho)
]- K# B8 ?5 q$ N0 R) I undo_ctl' t* f& f$ H9 i& w: b
); defun undo_init# z* o1 x" k" M0 V( v- n+ v
, @! I* c6 I# k
;----------------------------------------------------------------------------
" W1 W# x" i8 _' O; Restore UNDO status; l$ t' f" P7 a$ [6 X: E
;----------------------------------------------------------------------------
2 P: t1 x/ _" R: z; ~9 u(defun undo_restore (/ cmdecho)
0 T/ S3 k( n( H5 \ (if old_undoctl
% v4 C3 V9 z1 A4 l0 X7 r, N, S' t (progn% }( {8 p r a4 S8 [% @$ X
(setq cmdecho (getvar "CMDECHO"))) Y2 r+ G t; F+ ?
(setvar "CMDECHO" 0)
8 L* `) ? k, k
9 O+ s) s1 ~1 L! y2 j7 W8 ^ (if (equal 0 (getvar "UNDOCTL")) (command "_.undo" "_all")) " h3 R: O$ b- T9 W" X
(while (equal 8 (logand 8 (getvar "UNDOCTL")))
# D# C2 T/ Z" c0 ?4 k# O (command "_.undo" "_end")3 T- ~5 H/ n7 L- G4 N
); while+ V! l3 X* J# @, G1 D
6 f# z; ~5 F$ |
* z* a: h% [: n; _) a) s( x (if (not (equal old_undoctl (getvar "UNDOCTL")))2 w2 B% H4 e# ~( m- W& F: \
(progn( l3 r' A! b4 f, `
(cond; |' l. Z/ d4 _, z: ^
((equal 0 old_undoctl)
- u* o, x* X9 r (command "_.undo" "_control" "_none")
4 P+ y7 N5 X: S+ z& T' Q0 Q! ^ )4 q( c/ w% d K$ ^) r) z' _0 E
((equal 2 (logand 2 old_undoctl))- u9 Q( U4 \* ]
(command "_.undo" "_control" "_one")
, z8 x- [& q; L/ ~; @ )" D! c* |9 D7 u4 t- `- ~( z# X8 }6 T1 i& N
)1 R3 N0 G$ Q% l4 |4 [
(if (equal 4 (logand 4 old_undoctl))
; ^) l) z0 m' T u% Y! a (command "_.undo" "_auto" "_on")
0 w( v k& l0 b2 {# r (command "_.undo" "_auto" "_off")" e8 ?$ r; r6 A+ G' d+ p! w, k; B
); i+ Q- F1 W9 [- I5 f* W7 H
)& P( G2 V0 ? h( E- T
)
$ [# A0 J( C- \/ K3 q3 n! \ (setq old_undoctl nil)
! C7 t! t2 ^- ~# I1 O4 t2 R. a+ T (setvar "CMDECHO" cmdecho)
' r, M1 i) ^8 v }4 I7 R* ]1 k )
1 P( W+ m0 K8 g )
0 A/ B; w! @# P; ~); defun undo_restore
0 I/ ?& \ u2 U4 {' P+ H0 O* e/ y, k- A
;----------------------------------------------------------------------------
) |& X% m( k! ~4 s# [( B9 e; Save variables/ V- L1 @5 ^" W( x
;----------------------------------------------------------------------------
( \& o6 Q) U0 E' C( M7 }% u' c4 y1 A(defun var_save (a): j" I7 w( a/ ?7 _# G9 Y
(setq m_lst '())
/ b7 N( b& D0 C- n# z! b (repeat (length a)
. L+ s9 `, K$ D2 Q1 [ (setq m_lst (append m_lst (list (list (car a) (getvar (car a))))))6 a$ [3 Z* ?- C- h
(setq a (cdr a))
3 y& M3 S8 b4 i; e9 r' Z! l6 `7 } )
+ Z- e) D$ x9 H7 I); defun var_save# o, Q' f% M9 F; p* m
" O5 z* n, M& j, Q5 o! h" m;----------------------------------------------------------------------------, k$ S0 v& i, s# M! w3 G. j7 T( m
; Set variables8 c1 ? b5 i7 L7 g. W) \' B
;----------------------------------------------------------------------------
- \5 R3 W R0 K8 J5 _& M' Y(defun var_set (m_lst)
* ^, W1 i% K9 x (repeat (length m_lst)
7 C' J( }9 w- Y: r. T9 M (setvar (caar m_lst) (cadar m_lst)), @- J" A+ _: ^5 e% o& v8 P: I8 b v/ y$ |
(setq m_lst (cdr m_lst))" D& ^( i W$ x& |1 H: ]! g
)
, ?6 N0 |+ L' C! m5 m); defun var_set
) v; l) ^8 Z' P" N) {! c, _& H* `2 }% m: s9 O
;----------------------------------------------------------------------------+ [7 G7 D9 X1 ^3 d$ W$ J+ Q
; Restore variables8 L& z' l' p5 v: M
;----------------------------------------------------------------------------3 ^0 b8 q I B9 y4 _0 m5 R( h
(defun var_restore ()$ q$ \5 k+ e/ T* D7 U0 U y' s
(repeat (length m_lst)
$ `8 T4 L6 J. v) @: Z (setvar (caar m_lst) (cadar m_lst))
, v9 w* s3 N& w. T (setq m_lst (cdr m_lst))! {+ y4 G2 n5 P w5 B
)
3 P5 p; k) w5 C, A) A/ `( z4 U); defun var_restore
! U5 t2 H: Y; |4 R+ Q# F7 m: |4 z$ \: E# z! _ Q2 [
;----------------------------------------------------------------------------
' B2 }% f# V: n5 b; Initialize routine
. F- w5 R* `! }. ]# o: ^$ `;----------------------------------------------------------------------------2 f4 I7 `% i& S! [# X8 n2 Y$ l
(defun err_init(e_lst u_enable add_fun); G+ ? m! U6 F/ U' z
(if err_alive (err_restore)) ; To avoid nested call- W& |4 v1 O/ ]" b0 ^
(setq err_alive T)
/ M+ J4 p, @, U5 S (var_save e_lst) ; Save the modes; O( s, g8 c: u! J7 m3 N2 F
(if u_enable (setq old_undoctl (undo_init))) ; Initialize UNDO status
% E6 J4 Q# a& Q$ y. j (setq err_old *error* *error* err_main) ; Save the handle of *error*
" K# `/ Y4 v0 S7 B7 j9 s (if add_fun ; Add the user cleaner
+ N8 G& {; f, N/ T (setq *error* (append (reverse (cdr (reverse *error*)))$ U) }) s$ X# E4 d0 ~
(list add_fun (last *error*))
6 s9 X; [9 N. Q& I% b/ | ); append
; h5 Z. K% C7 H7 ~ )
, T) @% n7 Q7 ]4 g9 H )
$ T3 j; h6 a$ P. ^: |& c! D); defun err_init |
|