Visual LISP, AutoLISP and General Customization
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Copy attribute from selected block to another

27 REPLIES 27
SOLVED
Reply
Message 1 of 28
spartan_vng
10173 Views, 27 Replies

Copy attribute from selected block to another

Hi all, I've been digging around the forum and can't find exactly what I'm looking for but I was hoping one of you folks might be able to point me in the right direction. I'm in need of a script that allows me to copy the value of an attribute from a selected block (source) and paste that value to an attribute of the same name in a different block (destination). The trick here I guess is that I need it to continue till I specify when to stop.
Thanks
27 REPLIES 27
Message 2 of 28
marko_ribar
in reply to: spartan_vng

Quick and dirty...

 

(defun c:copypasteattribattribbytag ( / att tag str blk a )

  (vl-load-com)

  (setq att (car (nentsel "\nPick attribute...")))
  (setq tag (vla-get-tagstring (vlax-ename->vla-object att)))
  (setq str (vla-get-textstring (vlax-ename->vla-object att)))
  (while (setq blk (car (entsel "\nPick block to paste attribute value to - ENTER TO FINISH...")))
    (setq a blk)
    (while (= (cdr (assoc 0 (entget (setq a (entnext a))))) "ATTRIB")
      (if (= (strcase tag) (strcase (vla-get-tagstring (vlax-ename->vla-object a))))
        (vla-put-textstring (vlax-ename->vla-object a) str)
      )
    )
  )
  (princ)
)

M.R.

Marko Ribar, d.i.a. (graduated engineer of architecture)
Message 3 of 28
spartan_vng
in reply to: spartan_vng

wow.... thank you so much. I'll give it a try when I get to work on Monday.
Thanks
Vince
Message 4 of 28
spartan_vng
in reply to: marko_ribar


  Hi Marko,

 

Thanks again for the quick reply. I gave the lisp a try and I works based on the rough description I gave you however I had a fellow coworker tweak the code for me a bit to make it work more for what I was looking for. Only issue I'm running into now is that I need to pick the actual source attribute vs picking the block that has that specific attribute within it. We can try and hack the code some more ( we are not lisp literate) but if you have any suggestions you may have would be greatly appreciated.

 

Regards,

Vince

 

 

 

Message 5 of 28
alanjt_
in reply to: spartan_vng

How about something like this...

 

(defun c:MAV (/ AT:GetSel atts ss i ass)
  ;; Match Attribute Values
  ;; Alan J. Thompson, 2017.01.31


  (defun AT:GetSel (meth msg fnc / ent)
    ;; meth - selection method (entsel, nentsel, nentselp)
    ;; msg - message to display (nil for default)
    ;; fnc - optional function to apply to selected object
    ;; Ex: (AT:GetSel entsel "\nSelect arc: " (lambda (x) (eq (cdr (assoc 0 (entget (car x)))) "ARC")))
    ;; Alan J. Thompson, 05.25.10
    (while
      (progn (setvar 'ERRNO 0)
             (setq ent (meth (cond (msg)
                                   ("\nSelect object: ")
                             )
                       )
             )
             (cond ((eq (getvar 'ERRNO) 7) (princ "\nMissed, try again."))
                   ((eq (type (car ent)) 'ENAME)
                    (if (and fnc (not (fnc ent)))
                      (princ "\nInvalid object!")
                    )
                   )
             )
      )
    )
    ent
  )


  (if (and (AT:GetSel entsel
                      "\nSelect source attributed block: "
                      (lambda (x / d)
                        (if (and (eq (cdr (assoc 0 (setq d (entget (car x))))) "INSERT")
                                 (eq (cdr (assoc 66 d)) 1)
                            )
                          (setq atts (mapcar (function (lambda (a) (cons (vla-get-tagstring a) (vla-get-textstring a))))
                                             (vlax-invoke (vlax-ename->vla-object (car x)) 'GetAttributes)
                                     )
                          )
                        )
                      )
           )
           (progn
             (princ "\nSelect destination attributed block(s): ")
             (setq ss (ssget "_:L" '((0 . "INSERT") (66 . 1))))
           )
      )
    (repeat (setq i (sslength ss))
      (foreach a (vlax-invoke (vlax-ename->vla-object (ssname ss (setq i (1- i)))) 'GetAttributes)
        (if (setq ass (cdr (assoc (vla-get-tagstring a) atts)))
          (vla-put-textstring a ass)
        )
      )
    )
  )

  (princ)
)
(vl-load-com)
(princ)
Message 6 of 28
scot-65
in reply to: spartan_vng

Selecting the source attribute.

 

Partial code block for your investigation:

 

;
 (while (setq a (entsel "Select an ATTRIBUTE to globally update: "))
  (if (= (cdr (assoc 0 (entget (car a)))) "INSERT")
   (progn
    (setq b (nentselp (cadr a)))
    (if (= (cdr (assoc 0 (entget (car b)))) "ATTRIB")
     (progn
      (setq bn (cdr (assoc 2 (entget (car a))))) ;block name
      (setq bt (cdr (assoc 2 (entget (car b))))) ;tag name
      (setq bv (cdr (assoc 1 (entget (car b))))) ;tag value
...
;

???

 

 

 

 


Scot-65
A gift of extraordinary Common Sense does not require an Acronym Suffix to be added to my given name.


Message 7 of 28
john.uhden
in reply to: marko_ribar

@marko_ribar wrote: "Quick and dirty...

 

(defun c:copypasteattribattribbytag ..."

Typing in "copypasteattribattribbytag" may be dirty but certainly not quick.  :]

John F. Uhden

Message 8 of 28
spartan_vng
in reply to: alanjt_

Alan,

 

dang.... Pretty spot on for what I need. Only thing was is that  I was hoping to have it loop through so I did not have to hit enter to execute the lisp again. No biggie still works for what I need.

 

Thanks so much

 

Vince

Message 9 of 28
alanjt_
in reply to: spartan_vng


@spartan_vng wrote:

Alan,

 

dang.... Pretty spot on for what I need. Only thing was is that  I was hoping to have it loop through so I did not have to hit enter to execute the lisp again. No biggie still works for what I need.

 

Thanks so much

 

Vince


 

You're welcome.

Swap out the first if statement for a while, and you'll be good.

 

 (if (and (AT:GetSel entsel

TO

 (while (and (AT:GetSel entsel

 

Message 10 of 28
tom_durfee
in reply to: alanjt_

Thanks @alanjt_

Absolute hero! You've saved me from having to manually re-enter up to 58 attributes per block! (from v1 of dynamic block to v2, with 1 added attribute)

Message 11 of 28
mrushcadtech
in reply to: alanjt_

Would there be a way to enable this routine to set the attribute's layer to "0" and the color of the layer to "GREEN" during the evaluation? 

 

What about providing an option to select similar once the first destination block was selected?

Message 12 of 28
MarsksMan
in reply to: alanjt_

and if I want a preselected block for source just like MA command?
Message 13 of 28
Jason.Rugg
in reply to: alanjt_

@alanjt_Is there a way to automatically repeat this on multiple sources and copy to multiple destinations? Example would be an old parts list where each line item is an attributed block and need to copy all of the line item attributes to a newer line item block. Would be nice not to have to manually run the command on each line.

Message 14 of 28
alanjt_
in reply to: Jason.Rugg

Definitely. You would just need a list of old attributed blocks and a corresponding list of new attributed blocks. 

Message 15 of 28
stephanosdoulos
in reply to: scot-65

Saved me a massive amount of time. Thank you very much. 

Message 16 of 28
hebertsanchez
in reply to: alanjt_

This right here is one of the best things that I have found in this forum... 100 thank yous for this good sir. 

Message 17 of 28
spartan_vng
in reply to: hebertsanchez

No. Prob...just thank the person who originally scripted it for me.
The members are great in helping each other out!
Message 18 of 28
mail
in reply to: spartan_vng

Any way to work this across layouts?

Message 19 of 28
jbernalPTA3R
in reply to: spartan_vng

THANK YOU THIS REALLY SAVED ME SOME TIME

Message 20 of 28
john.uhden
in reply to: jbernalPTA3R

Amazing. I hadn't even tested it.
Please hit the "Accept as solution" button to my previous note.
Remember that we want you to learn from these contributions so that one day
you will be providing the solutions to others.

John F. Uhden

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report

”Boost