Author Topic: COPY BLOCK ATTRIBUTE  (Read 6479 times)

0 Members and 1 Guest are viewing this topic.

Fabricio28

  • Swamp Rat
  • Posts: 670
COPY BLOCK ATTRIBUTE
« on: October 29, 2012, 07:49:19 AM »
Hey guys,
 I have two different blocks but the same tags and attribute. Is there any way to copy all attribute of the block to other block?

Best Regards.

Tharwat

  • Swamp Rat
  • Posts: 710
  • Hypersensitive
Re: COPY BLOCK ATTRIBUTE
« Reply #1 on: October 29, 2012, 08:02:48 AM »
Try this oldie of mine , and hope it helps you out with it . :)

Code - Auto/Visual Lisp: [Select]
  1. (defun c:Test (/ s ss e ent lst i)
  2. ;;; Tharwat 11. July. 2012 ;;;
  3.   (princ "\n Select first Attributed Block :")
  4.   (if (setq s (ssget "_+.:S" '((0 . "INSERT") (66 . 1))))
  5.     (progn (setq e (entnext (ssname s 0)))
  6.            (while (not (eq (cdr (assoc 0 (entget e))) "SEQEND"))
  7.              (if (eq (cdr (assoc 0 (entget e))) "ATTRIB")
  8.                (setq lst (cons (cdr (assoc 1 (entget e))) lst))
  9.              )
  10.              (setq e (entnext e))
  11.            )
  12.     )
  13.   )
  14.   (princ "\n Select Second Attributed Block :")
  15.   (if (and s (setq ss (ssget "_+.:S" '((0 . "INSERT") (66 . 1)))))
  16.     (progn (setq ent (entnext (ssname ss 0)))
  17.            (setq i   0
  18.                  lst (reverse lst)
  19.            )
  20.            (while (not (eq (cdr (assoc 0 (entget ent))) "SEQEND"))
  21.              (if (and (eq (cdr (assoc 0 (entget ent))) "ATTRIB") (nth i lst))
  22.                (entmod (subst (cons 1 (nth i lst)) (assoc 1 (entget ent)) (entget ent)))
  23.              )
  24.              (setq i (1+ i))
  25.              (setq ent (entnext ent))
  26.            )
  27.     )
  28.   )
  29.   (princ)
  30. )

Fabricio28

  • Swamp Rat
  • Posts: 670
Re: COPY BLOCK ATTRIBUTE
« Reply #2 on: October 29, 2012, 08:37:39 AM »
Hi, Tharwat.
 Thank you very much for the code. It's works perfect!!

I really appreciated your help.

Best Regards.

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: COPY BLOCK ATTRIBUTE
« Reply #3 on: October 29, 2012, 08:37:53 AM »
For what its worth, here is how I might write such a program:

Code - Auto/Visual Lisp: [Select]
  1. ;; Copy Attributes  -  Lee Mac
  2. ;; Prompts the user to select a 'source' attributed block and proceeds to copy
  3. ;; attribute data from the source block to all attributed blocks in the subsequent selection.
  4.  
  5. (defun c:attcopy ( / inc lst sel src val )
  6.     (if
  7.         (and
  8.             (setq src (LM:ssget "\nSelect Source Block: " '("_+.:E:S" ((0 . "INSERT") (66 . 1)))))
  9.             (setq sel (LM:ssget "\nSelect Blocks for Copied Attributes: " '(((0 . "INSERT") (66 . 1)))))
  10.         )
  11.         (progn
  12.             (setq lst
  13.                 (mapcar
  14.                     (function
  15.                         (lambda ( att )
  16.                             (cons (vla-get-tagstring att) (vla-get-textstring att))
  17.                         )
  18.                     )
  19.                     (vlax-invoke (vlax-ename->vla-object (ssname src 0)) 'getattributes)
  20.                 )
  21.             )
  22.             (repeat (setq inc (sslength sel))
  23.                 (foreach att (vlax-invoke (vlax-ename->vla-object (ssname sel (setq inc (1- inc)))) 'getattributes)
  24.                     (if (setq val (cdr (assoc (vla-get-tagstring att) lst)))
  25.                         (vl-catch-all-apply 'vla-put-textstring (list att val))
  26.                     )
  27.                 )
  28.             )
  29.         )
  30.     )
  31.     (princ)      
  32. )
  33.  
  34. ;; ssget  -  Lee Mac
  35. ;; A wrapper for the ssget function to permit the use of a custom selection prompt
  36. ;;
  37. ;; Arguments:
  38. ;; msg    - selection prompt
  39. ;; params - list of ssget arguments
  40.  
  41. (defun LM:ssget ( msg params / sel )
  42.     (princ msg)
  43.     (setvar 'nomutt 1)
  44.     (setq sel (vl-catch-all-apply 'ssget params))
  45.     (setvar 'nomutt 0)
  46.     (if (not (vl-catch-all-error-p sel)) sel)
  47. )
  48.  

By using an association list of attribute tags and their values, the program does not rely on the order in which attribute references are encountered in the blocks to which the attribute data is being copied.

Tharwat

  • Swamp Rat
  • Posts: 710
  • Hypersensitive
Re: COPY BLOCK ATTRIBUTE
« Reply #4 on: October 29, 2012, 08:39:40 AM »
Hi, Tharwat.
 Thank you very much for the code. It's works perfect!!

I really appreciated your help.

Best Regards.

I am happy to hear that . :)

Fabricio28

  • Swamp Rat
  • Posts: 670
Re: COPY BLOCK ATTRIBUTE
« Reply #5 on: October 29, 2012, 08:57:20 AM »
Quote
  By using an association list of attribute tags and their values, the program does not rely on the order in which attribute references are encountered in the blocks to which the attribute data is being copied.

  Thanks Lee,
  I really appreciated your help, too.

  Very nice your code.

Regards.

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: COPY BLOCK ATTRIBUTE
« Reply #6 on: October 29, 2012, 08:58:58 AM »
Thank you Fabricio  :-)

kruuger

  • Swamp Rat
  • Posts: 637
Re: COPY BLOCK ATTRIBUTE
« Reply #7 on: October 31, 2012, 11:11:54 AM »
Thank you Fabricio  :)
hello Lee
ssget wrapper is awesome :) is there any weak point ? work always ?

thanks
kruuger

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: COPY BLOCK ATTRIBUTE
« Reply #8 on: October 31, 2012, 11:44:48 AM »
ssget wrapper is awesome :)

Thank you Kruuger  :-)

work always ?

I hope so  :wink:

kruuger

  • Swamp Rat
  • Posts: 637
Re: COPY BLOCK ATTRIBUTE
« Reply #9 on: November 07, 2012, 06:36:43 AM »
ssget wrapper is awesome :)

Thank you Kruuger  :)

work always ?

I hope so  ;)
Hi Lee
How to pass _C to wrapper ?
i try:
Code: [Select]
(LM:ssget "ok" '("_C" p1 p2))but nil

thanks
kruuger

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: COPY BLOCK ATTRIBUTE
« Reply #10 on: November 07, 2012, 06:40:30 AM »
Code - Auto/Visual Lisp: [Select]
  1. (LM:ssget "ok" (list "_C" p1 p2))

Though note that "_C" is an automated selection mode and will not pause for user input  ;-)

kruuger

  • Swamp Rat
  • Posts: 637
Re: COPY BLOCK ATTRIBUTE
« Reply #11 on: November 07, 2012, 06:47:00 AM »
Code - Auto/Visual Lisp: [Select]
  1. (LM:ssget "ok" (list "_C" p1 p2))

Though note that "_C" is an automated selection mode and will not pause for user input  ;)
aaa...of course...forget...thanks