Block Rename to Attribute Tag Value

Block Rename to Attribute Tag Value

NAZEEB.HAMZA
Participant Participant
2,055 Views
14 Replies
Message 1 of 15

Block Rename to Attribute Tag Value

NAZEEB.HAMZA
Participant
Participant

Hi All,

 

I am trying to rename my blocks with the Attribute tag value.

In the included example drawing I have 2 different Blocks (Style-M) & (Style-L) with multiple copies of each.

And each copies have its unique Attributes and I want to rename each copies of the block using the Attribute values.

For e.g. Style-M blocks will need be renamed using the attributes to 2000, 2001, 2002 ...

 

I have been searching the forum and the closest I could find is the below.

https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/block-rename-to-attribute-text-value...

The LISP as it is doesn't work for my requirement, but I am hopeful that the experts here can provide a solution.

 

Thanks!

Nazeeb H

0 Likes
Accepted solutions (1)
2,056 Views
14 Replies
Replies (14)
Message 2 of 15

pbejse
Mentor
Mentor
Accepted solution

@NAZEEB.HAMZA wrote:

For e.g. Style-M blocks will need be renamed using the attributes to 2000, 2001, 2002 ...


Untested... copied from link

I see its a different , let me get my hands on a computer, hang on.

 

Here you go.

 

(Defun c:renb ( /. aDoc blocks ss i effname e)
  (setq aDoc (vla-get-ActiveDocument (vlax-get-acad-object))
	blocks (vla-get-blocks adoc))
  	(if (setq ss (ssget "_:L" '((0 . "INSERT"))))
	  	(repeat (setq i (sslength ss))
		  	(setq e (vlax-ename->vla-object (ssname ss (setq i (1- i))))
				effname (vla-get-EffectiveName e))
		  (cond
		    (	(not (setq atval (vl-some
				      '(lambda (at)
					 (if (= (vla-get-tagstring at) "PK1") (vla-get-textstring at)))
			  	(vlax-invoke e 'GetAttributes)))		
						)
		     )
		    ( (tblsearch "BLOCK" atval) (vlax-put e 'Name atval)
		     				)
		    ( (=  effname atval)	)	
		    ( T
		       (vlax-invoke e 'ConvertToAnonymousBlock) 
		       (vla-put-name (vla-item blocks (Vlax-get e 'Name)) atval) )
		    			)
		  )	
	  )
  (princ)
  )

HTH

Message 3 of 15

kajanthangavel
Advocate
Advocate

It working..... 😀

Message 4 of 15

NAZEEB.HAMZA
Participant
Participant

Awesome!!! It is working, Thanks a lot Mr. Patrick.

I also really appreciate the quick response.

 

0 Likes
Message 5 of 15

pbejse
Mentor
Mentor

@NAZEEB.HAMZA wrote:

Awesome!!! It is working, Thanks a lot.

Also I really appreciate the quick solution!


Good to hear that. You are welcome  🙂

- One is glad to be of service -- 

 

Nice to know it works for you too @kajanthangavel 

 

Message 6 of 15

Anonymous
Not applicable

Hi!

Thanks for the code.

 

Is it possible to change the code so it makes a new block but only whit the content of the specified layer? I have a block that contains two layers and I need content of only one layer.

 

Thank you!

 

Denis

0 Likes
Message 7 of 15

pbejse
Mentor
Mentor

@Anonymous wrote:

Is it possible to change the code so it makes a new block but only whit the content of the specified layer? I have a block that contains two layers and I need content of only one layer.


That's an interesting twist to the original intent of the code. I supposed it can be done. How would the user specify the layer ? and what will be the eventual name of the block? will it still be from an attribute value?

 

Message 8 of 15

Anonymous
Not applicable

thanks for answer

 

it could be specified in the code that the content of the layer named LAYER1 should create the new named block. 

I didn't say at the first post...content of the second layer should move outside the block, not to be deleted.

Yes, the name will still be from an attribute value.

 

tnx

d

0 Likes
Message 9 of 15

pbejse
Mentor
Mentor

-move outside the block, -

On every instance of the block?

 

A sample drawing would explain much better than how the way you were describing the intent.

 

0 Likes
Message 10 of 15

Anonymous
Not applicable

Sorry for not being clear...here is a sample drawing

content of layer2 could be in the old block if that's easier to do...

0 Likes
Message 11 of 15

pbejse
Mentor
Mentor

But what of the value of the attributes? Are you wanting to retain the value and not leave it as a broken link?

 

pbejse_0-1630070362433.png

 

 

 

0 Likes
Message 12 of 15

Anonymous
Not applicable

If it's any problem to retain value it can be a broken link.

0 Likes
Message 13 of 15

pbejse
Mentor
Mentor

@Anonymous wrote:

Sorry for not being clear...here is a sample drawing

content of layer2 could be in the old block if that's easier to do...


I had to delete the "K_NP_125_65_140" block that was there on the sample drawing , as the insertion point is way off.

Here's how the code works

 

The list shown on the code is key.

(setq BnameLayer '(("POLICA" "POLICA" "LAYER1")))
  • The first item of the list is the Block name
  • Second item is the TAG name as the source for the new name
  • Third item is the layer name to be retained on the renamed block, the rest will be left "outside"

Command: REBeL

Select objects: Specify opposite corner: 12 found

Select objects:
Block reference renamed to: K_NP_125_65_140
Block reference renamed to: K_NP_125_35_140
Block reference renamed to: K_NP_100_45_140
Block reference renamed to: K_NP_90_35_140

 

HTH

 

Refer to attached files [ Rename Explode By Layer.dwg | Rename Explode By Layer.lsp ]

 

0 Likes
Message 14 of 15

Anonymous
Not applicable

Thank you very much, it is working perfectly!

Regards!

Denis

 

0 Likes
Message 15 of 15

pbejse
Mentor
Mentor

@Anonymous wrote:

Thank you very much, it is working perfectly!

Regards!

Denis


Glad i could help 🙂

0 Likes