home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 13 / 13.iso / p / p037 / cr12_5.ddi / R11SUPP.EXE / ATTREDEF.LSP < prev    next >
Encoding:
Lisp/Scheme  |  1990-10-08  |  11.2 KB  |  333 lines

  1. ;;;--------------------------------------------------------------------------;
  2. ;;; ATTREDEF.LSP
  3. ;;;   Copyright (C) 1990 by Autodesk, Inc.
  4. ;;;  
  5. ;;;   Permission to use, copy, modify, and distribute this software and its
  6. ;;;   documentation for any purpose and without fee is hereby granted.  
  7. ;;;
  8. ;;;   THIS SOFTWARE IS PROVIDED "AS IS" WITHOUT EXPRESS OR IMPLIED WARRANTY. 
  9. ;;;   ALL IMPLIED WARRANTIES OF FITNESS FOR ANY PARTICULAR PURPOSE AND OF 
  10. ;;;   MERCHANTABILITY ARE HEREBY DISCLAIMED.
  11. ;;;
  12. ;;;   Written by Karry Layden - May 1988
  13. ;;;   Updated to handle blocks with no attributes.    28 June 1990    JSY
  14. ;;;
  15. ;;; --------------------------------------------------------------------------;
  16. ;;; DESCRIPTION
  17. ;;;
  18. ;;;   This program allows you to redefine a Block and update the
  19. ;;;   Attributes associated with any previous insertions of that Block.
  20. ;;;   All new Attributes are added to the old Blocks and given their
  21. ;;;   default values. All old Attributes with equal tag values to the new
  22. ;;;   Attributes are redefined but retain their old value. And all old
  23. ;;;   Attributes not included in the new Block are deleted.
  24. ;;;
  25. ;;;   Note that if handles are enabled, new handles will be assigned to
  26. ;;;   each redefined block.
  27. ;;;
  28. ;;; --------------------------------------------------------------------------;
  29.  
  30. ;;;
  31. ;;; Oldatts sets "old_al" (OLD_Attribute_List) to the list of old Attributes
  32. ;;; for each Block.  The list does not include constant Attributes.
  33. ;;;
  34. (defun oldatts (/ e_name e_list cont)
  35.   (setq oa_ctr 0 
  36.         cont   T
  37.         e_name b1
  38.   )
  39.   (while cont
  40.     (if (setq e_name (entnext e_name))
  41.       (progn
  42.         (setq e_list (entget e_name))
  43.         (if (and (= (cdr (assoc 0 e_list)) "ATTRIB")
  44.                  ;; NOT a constant attribute -- (cdr (assoc 70 e_list)) != 2)
  45.                  (/= (logand (cdr (assoc 70 e_list)) 2) 2))
  46.           (progn
  47.             (if old_al
  48.               (setq old_al (cons e_list old_al))
  49.               (setq old_al (list e_list))
  50.             )
  51.             (setq oa_ctr (1+ oa_ctr))           ; count the number of old atts
  52.           )
  53.           ;; else, exit
  54.           (setq cont nil)
  55.         )
  56.       )
  57.       (setq cont nil)
  58.     )
  59.   )
  60. )
  61. ;;;
  62. ;;; Newatts sets "new_al" to the list of new Attributes in the new Block.
  63. ;;; The list does not include constant Attributes.
  64. ;;;
  65. (defun newatts (ssetn ssl / i e_name e_list)
  66.   (setq i 0 na_ctr 0)
  67.   (while (< i ssl)
  68.     (if (setq e_name (ssname ssetn i))
  69.       (progn
  70.         (setq e_list (entget e_name))
  71.         (if (and (= (cdr (assoc 0 e_list)) "ATTDEF")
  72.                  ;; NOT a constant attribute -- (cdr (assoc 70 e_list)) != 2)
  73.                  (/= (logand (cdr (assoc 70 e_list)) 2) 2))
  74.           (progn
  75.             (if new_al
  76.               (setq new_al (cons e_list new_al))
  77.               (setq new_al (list e_list))
  78.             )
  79.             (setq na_ctr (1+ na_ctr))     ; count the number of new atts
  80.           )
  81.         )
  82.       )
  83.     )
  84.     (setq i (1+ i))
  85.   )
  86.   na_ctr
  87. )
  88. ;;;
  89. ;;; Compare the list of "old" to the list of "new" Attributes and make
  90. ;;; the two lists "same" and "preset". "Same" contains the old values of
  91. ;;; all the Attributes in "old" with equal tag values to some Attribute
  92. ;;; in "new" and the default values of all the other Attributes. "Preset"
  93. ;;; contains the preset Attributes in old with equal tag values to some
  94. ;;; Attribute in new.
  95. ;;;
  96. (defun compare (/ i j)
  97.   (setq i 0
  98.         j 0
  99.         pa_ctr 0
  100.         same nil
  101.         va_ctr 0
  102.         preset nil)
  103.   ;; "i" is a counter that increments until the number of new attributes
  104.   ;; is reached.
  105.   (while (< i na_ctr)
  106.     (cond 
  107.       ;; If there are old attributes AND the tag strings of the old and new 
  108.       ;; attributes are the same...
  109.       ((and old_al
  110.             (= (cdr (assoc 2 (nth j old_al))) (cdr (assoc 2 (nth i new_al)))))
  111.         ;; IS a preset attribute -- (cdr (assoc 70 e_list)) == 8)
  112.         (if (= (logand (cdr (assoc 70 (nth i new_al))) 8) 8)
  113.           ;; If the attribute is a preset attribute then add it to the list
  114.           ;; of preset attributes and increment the counter "pa_ctr".
  115.           ;; IS a preset attribute -- (cdr (assoc 70 e_list)) == 8)
  116.           (progn
  117.             (if preset
  118.               (setq preset (cons (nth j old_al) preset))
  119.               (setq preset (list (nth j old_al)))
  120.             )
  121.             (setq pa_ctr (1+ pa_ctr))     ; count preset atts
  122.           )
  123.           ;; Else, add it to the list of same attributes "same".
  124.           (if same
  125.             (setq same (cons (cdr (assoc 1 (nth j old_al))) same))
  126.             (setq same (list (cdr (assoc 1 (nth j old_al)))))
  127.           )
  128.         )
  129.         ;; If the attribute must be verified, increment counter "va_ctr".
  130.         ;; NOT a preset attribute -- (cdr (assoc 70 e_list)) != 8)
  131.         (if (and (/= (logand (cdr (assoc 70 (nth i new_al))) 8) 8)
  132.                  ;; IS a verified attribute -- (cdr (assoc 70 e_list)) == 4)
  133.                  (= (logand (cdr (assoc 70 (nth i new_al))) 4) 4))
  134.           (setq va_ctr (+ 1 va_ctr))
  135.         )
  136.         (setq i (1+ i))
  137.         (setq j 0)
  138.       )
  139.       ;; If the number of old attributes equals the old attribute counter "j"
  140.       ((= j oa_ctr)
  141.         ;; If this attribute is not a preset attribute, but is not in the 
  142.         ;; old list, then add it to the list "same".
  143.         ;; NOT a preset attribute -- (cdr (assoc 70 e_list)) != 8)
  144.         (if (/= (logand (cdr (assoc 70 (nth i new_al))) 8) 8)
  145.           (if same
  146.             (setq same (cons (cdr (assoc 1 (nth i new_al))) same))
  147.             (setq same (list (cdr (assoc 1 (nth i new_al)))))
  148.           )
  149.         )
  150.         ;; NOT a preset attribute -- (cdr (assoc 70 e_list)) != 8)
  151.         (if (and (/= (logand (cdr (assoc 70 (nth i new_al))) 8) 8)
  152.                  ;; IS a verified attribute -- (cdr (assoc 70 e_list)) == 4)
  153.                  (= (logand (cdr (assoc 70 (nth i new_al))) 4) 4))
  154.           (setq va_ctr (+ 1 va_ctr))
  155.         )
  156.         (setq i (1+ i))
  157.         (setq j 0)
  158.       )
  159.       ;; Increment the old attribute counter "j"...
  160.       (t
  161.         (setq j (1+ j))
  162.       )
  163.     )
  164.   )
  165. )
  166. ;;;
  167. ;;; Find the entity for each of the "preset" Attributes in the newly
  168. ;;; inserted Block.
  169. ;;;
  170. (defun findpt ()
  171.   (setq test T)
  172.   (setq en (entnext e1))
  173.   (setq e_list (entget en))
  174.   (while test
  175.     (if (and (= (cdr (assoc 0 e_list)) "ATTRIB") (= (cdr (assoc 2 e_list)) tag))
  176.       (setq test nil)
  177.       (progn
  178.         (setq ex en)
  179.         (setq en (entnext ex))
  180.         (if e_list
  181.           (setq e_list (entget en))
  182.         )
  183.       )
  184.     )
  185.   )
  186. )
  187. ;;;
  188. ;;; Insert a new Block on top of each old Block and set its new Attributes
  189. ;;; to their values in the list "same". Then replace each of the "preset"
  190. ;;; Attributes with its old value.
  191. ;;;
  192. (defun redef (/ xsf ysf zsf ls i e1 v)
  193.   (command "ucs" "e" b1)         ; define the block's UCS
  194.   (setq xsf (cdr (assoc 41 (entget b1)))) ; find x scale factor
  195.   (setq ysf (cdr (assoc 42 (entget b1)))) ; find y scale factor
  196.   (setq zsf (cdr (assoc 43 (entget b1)))) ; find z scale factor
  197.   (setq ls (length same))
  198.   (setq i 0)
  199.   (command "insert" bn "0.0,0.0,0.0" "XYZ" xsf ysf zsf "0.0")
  200.   (while (< i ls)                     ; set attributes to their values
  201.     (command (nth i same))
  202.     (setq i (1+ i))
  203.   )
  204.   (while (< 0 va_ctr)
  205.     (command "")                      ; at prompts, verify attributes
  206.     (setq va_ctr (1- va_ctr))
  207.   )
  208.   (setq i 0)
  209.   (setq e1 (entlast))
  210.   (while (< 0 pa_ctr)                    ; edit each of the "preset" attributes
  211.     (setq tag (cdr (assoc 2 (nth i preset))))
  212.     (setq v (cdr (assoc 1 (nth i preset))))
  213.     (findpt)                          ; find the entity to modify
  214.     (setq e_list (subst (cons 1 v) (assoc 1 e_list) e_list))
  215.     (entmod e_list)                        ; modify the entity's value
  216.     (setq i (1+ i))
  217.     (setq pa_ctr (1- pa_ctr))
  218.   )
  219.   (command "ucs" "p")                 ; restore the previous UCS
  220. )
  221. ;;;
  222. ;;; System variable save
  223. ;;;
  224. (defun modes (a)
  225.   (setq mlst '())
  226.   (repeat (length a)
  227.     (setq mlst (append mlst (list (list (car a) (getvar (car a))))))
  228.     (setq a (cdr a)))
  229. )
  230. ;;;
  231. ;;; System variable restore
  232. ;;;
  233. (defun moder ()
  234.   (repeat (length mlst)
  235.     (setvar (caar mlst) (cadar mlst))
  236.     (setq mlst (cdr mlst))
  237.   )
  238. )
  239. ;;;
  240. ;;; Internal error handler
  241. ;;;
  242. (defun attrerr (s)                    ; If an error (such as CTRL-C) occurs
  243.                                       ; while this command is active...
  244.   (if (/= s "Function cancelled")
  245.     (princ (strcat "\nError: " s))
  246.   )
  247.   (moder)                             ; restore saved modes
  248.   (setq *error* olderr)               ; restore old *error* handler
  249.   (princ)
  250. )
  251. ;;;
  252. ;;; Main program
  253. ;;;
  254. (defun C:ATTREDEF (/ k n olderr bn sseto ssetn pt ssl new_al
  255.                      old_al same preset b1 oa_ctr va_ctr na_ctr
  256.                   ) 
  257.   (setq k 0
  258.       n 0
  259.       test T
  260.       olderr *error*
  261.       *error* attrerr
  262.   )
  263.  
  264.   (modes '("CMDECHO" "ATTDIA" "ATTREQ" "GRIDMODE" "UCSFOLLOW"))
  265.   (setvar "cmdecho" 0)                ; turn cmdecho off
  266.   (setvar "attdia" 0)                 ; turn attdia off
  267.   (setvar "attreq" 1)                 ; turn attreq on
  268.   (setvar "gridmode" 0)               ; turn gridmode off
  269.   (setvar "ucsfollow" 0)              ; turn ucsfollow off
  270.  
  271.   (while 
  272.     (progn
  273.       (setq bn (strcase (getstring 
  274.         "\nName of Block you wish to redefine: ")))
  275.       (if (tblsearch "block" bn)
  276.         (progn
  277.           (setq sseto (ssget "x" (list (cons 2 bn))))
  278.           (setq test nil)
  279.         )
  280.         (progn
  281.           (princ "\nBlock ")
  282.           (princ bn)
  283.           (princ " is not defined. Please try again.\n")
  284.         )
  285.        )
  286.     )
  287.   )
  288.   (if sseto
  289.     (progn
  290.       (while 
  291.         (progn
  292.           (princ "\nSelect entities for new Block... ")
  293.           (if (null (setq ssetn (ssget)))
  294.             (princ "\nNo new Block selected. Please try again.")
  295.             (setq test nil)
  296.           )
  297.         )
  298.       )
  299.       ;; find the list of new attributes
  300.       (setq na_ctr (newatts ssetn (sslength ssetn)) )
  301.       (if (> na_ctr 0)
  302.         (progn
  303.           (initget 1)
  304.           (setq pt (getpoint "\nInsertion base point of new Block: "))
  305.           (setq ssl (sslength sseto))
  306.           ;; redefine the block
  307.           (command "block" bn "Y" pt ssetn "") 
  308.           (while (< k ssl)
  309.             (setq b1 (ssname sseto k))    ; For each old block...
  310.             (setq old_al nil)
  311.             (oldatts)                     ; find the list of old attributes,
  312.             (compare)                     ; compare the old list with the new,
  313.             (redef)                       ; and redefine its attributes.
  314.             (entdel b1)                   ; delete the old block.
  315.             (setq k (1+ k))
  316.           )
  317.           (command "regenall")
  318.         )
  319.         (princ "\nNew block has no attributes. ")
  320.       )
  321.     )
  322.     (princ (strcat "\nNo insertions of block " bn " found to redefine. "))
  323.   )
  324.   (moder)                             ; restore saved modes
  325.   (setq *error* olderr)               ; restore old *error* handler
  326.   (princ)
  327. )
  328.  
  329. (defun c:at () (c:attredef))
  330. (princ 
  331.   "\n\tC:ATtredef loaded.  Start command with AT or ATTREDEF.")
  332. (princ)
  333.