Jump to content
itacad

Copy attribute value between two blocks (different tags)

Recommended Posts

itacad

Hello, I found a lot of lisp for copy attributes from a bloch to another but usually the operation is applied to two equal blocks.

I have 2 sets of different blocks, structured in the same way:

- From the first series of blocks I need the attribute with the tag SIGLA (whatever the name of the block)

- From the second set of blocks I need the attribute with the tag NAME (whatever the name of the block)

The next description holds in both directions.

I would like to select any block with attribute with SIGLA tag, acquire its value and copy it in any block with NAME tag attribute even if not compiled.

I'd like to just click on the blocks.

Have you ever seen / developed something like this?

Thanks in advance

Share this post


Link to post
Share on other sites
Emmanuel Delay

This script does what you require, if your attributes are visible.  For example set every NAME attribute to "." ,  just so you can select it as target. (which might not be practical to you, not sure)

 

 

 

But I'll see if I can make it fit your specific purpose.

Share this post


Link to post
Share on other sites
Emmanuel Delay

This should work

Command TTCB , but please feel free to rename this

 

(vl-load-com)

;; To copy/paste from fixed attributes, source tag:  "SIGLA",  destination tag:  "NAME"
;; TTC Block
(defun c:ttcb ( / sText)
  (defun TCC_copyAttribute (source / blk )
    (if (setq blk (entsel "\nSelect source block: "))
	  (setq sText (LM:vl-getattributevalue (vlax-ename->vla-object (car blk)) source))
	)
  )
  (defun TCC_pasteAttribute (dest / blk)
    (if (setq blk (entsel "\nSelect destination block: "))
	  (LM:vl-setattributevalues (vlax-ename->vla-object (car blk)) (list (cons dest sText))  )
	)
  ) 
  
  (while (TCC_copyAttribute  "SIGLA")
    (TCC_pasteAttribute  "NAME")
  )
  
) 

;; Get Attribute Value  -  Lee Mac
;; Returns the value held by the specified tag within the supplied block, if present.
;; blk - [vla] VLA Block Reference Object
;; tag - [str] Attribute TagString
;; Returns: [str] Attribute value, else nil if tag is not found.
(defun LM:vl-getattributevalue ( blk tag )
    (setq tag (strcase tag))
    (vl-some '(lambda ( att ) (if (= tag (strcase (vla-get-tagstring att))) (vla-get-textstring att))) (vlax-invoke blk 'getattributes))
)

;; Set Attribute Values  -  Lee Mac
;; Sets attributes with tags found in the association list to their associated values.
;; blk - [vla] VLA Block Reference Object
;; lst - [lst] Association list of ((<tag> . <value>) ... )
;; Returns: nil
(defun LM:vl-setattributevalues ( blk lst / itm )
    (foreach att (vlax-invoke blk 'getattributes)
        (if (setq itm (assoc (vla-get-tagstring att) lst))
            (vla-put-textstring att (cdr itm))
        )
    )
)

 

Edited by Emmanuel Delay

Share this post


Link to post
Share on other sites
BIGAL

Here is a start allows picking an attribute directly returning tag name and block name

 


(setq ans  (entget (car (nentsel "pick block 1"))))
(setq tag1 (cdr (assoc 2 ans)))
(setq obj (vlax-ename->vla-object (cdr(assoc 330 ans))))
(setq blk1 (vla-get-EffectiveName obj))
(setq ans  (entget (car (nentsel "pick block 2"))))
(setq tag2 (assoc ans))
(setq obj (vlax-ename->vla-object (cdr(assoc 330 ans))))
(setq blk2 (vla-get-EffectiveName obj))

Share this post


Link to post
Share on other sites
itacad

Hi Emmanuel Delay, unfortunately, the lisp ttc is not suitable in this case because precisely, the destination attributes could be invisible or not compiled (the result is that I have nothing to click on if not the block)

 

For Bigal: in your code there aren't the specified attribute's tag (SIGLA and NAME) why?

Share this post


Link to post
Share on other sites
BIGAL

That's the idea of the code, using nentsel returns the attribute tag name, I need to change to "Pick block1 Attribute" "Pick block 2 Attribute"

 

There

 

add

Code updated in prior post mistake fixed also
(setq ans  (entget (car (nentsel "pick block 1 attribute"))))
(setq tag1 (cdr (assoc 2 ans)))
(setq obj (vlax-ename->vla-object (cdr(assoc 330 ans))))
(setq blk1 (vla-get-EffectiveName obj))
(setq ans  (entget (car (nentsel "pick block 2 attribute"))))
(setq tag2 (cdr (assoc 2 ans)))
(setq obj (vlax-ename->vla-object (cdr(assoc 330 ans))))
(setq blk2 (vla-get-EffectiveName obj))
(alert (strcat "You picked block1 \ntagname = " tag1 "\nName =" blk1 "\nYou picked block 2  \ntagname = " tag2 "\nName =" blk2 ))

 

Edited by BIGAL

Share this post


Link to post
Share on other sites
Emmanuel Delay
10 hours ago, itacad said:

Hi Emmanuel Delay, unfortunately, the lisp ttc is not suitable in this case because precisely, the destination attributes could be invisible or not compiled (the result is that I have nothing to click on if not the block) 

  

For Bigal: in your code there aren't the specified attribute's tag (SIGLA and NAME) why? 

 

Well yes, exactly why I wrote that second reply. 

With TTCB you click on the block, any part of the block

 

Edited by Emmanuel Delay

Share this post


Link to post
Share on other sites
BIGAL

Your missing the point no hard coding of blocknames or tagnames, the next time you have to do this and use a different block name you have to edit the actual lisp file. The coding provides a universal answer rather 1 time only.

Share this post


Link to post
Share on other sites
maratovich

itacad

To be precise and there is no error - attach a sample of your file with blocks.

Share this post


Link to post
Share on other sites
itacad

I found the solution in your gigantic discussion archive

is it possible to make it work both ways?

Thank you very much

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×