Find and replace for attribute values in multiple blocks
NOTICE: NO HELP OR ROUTINE NEEDED!!!
I have text values in two different attributes of a bunch of blocks and want to edit the text in each block to remove a text string from each attribute. The value of the attributes is not the same, but there are two common text strings that I ignorantly put in the values. I want to take the text out without taking days to fix it. Any suggestions of a quick way to do this? Are there any routines out there to do something like this?
There are two attribute tags in one block and one in another block that need to change. I want to be able to remove the offending text string in all the blocks in one fell swoop, if possible!
Thanks so much for your help :-)
HA! -attedit is the command that will work for this!!!!
i need no help with this, there is no lisp routine or programming needed, except maybe to save a macro :-)
Re: Find and replace for attribute values in multiple blocks
Using lisp its pretty easy to get a selection of blocks and just walk through their values does not matter how many attributes it just needs one thing that it checks if a block has attributes.
if you look at this (foreach att (vlax-invoke (vlax-ename->vla-object (ssname SS x )) 'getattributes) you can then get the textstring of att, if it matches what your looking for then change the value.
Re: Find and replace for attribute values in multiple blocks
Quote:
Originally Posted by
BIG-AL
Using lisp its pretty easy to get a selection of blocks and just walk through their values does not matter how many attributes it just needs one thing that it checks if a block has attributes.
if you look at this (foreach att (vlax-invoke (vlax-ename->vla-object (ssname SS x )) 'getattributes) you can then get the textstring of att, if it matches what your looking for then change the value.
thanks so much for the response, but I don't need to use lisp to do what I need to do. I can use -attedit to do a find and replace in all the values of all the blocks on a layout tab.
Again, thanks for the response!
I'd like to delete this post but don't know how to do that.
Re: Find and replace for attribute values in multiple blocks
Quote:
Originally Posted by
Lemons
I'd like to delete this post but don't know how to do that.
It's always good to keep posts like this, someone out there may be looking for a similar reason and search and find this post.
Posting your resolution is encouraged (as you did) and will help others!
Glad you got it figured out.
Re: Find and replace for attribute values in multiple blocks
Hey Lemons, -attedit has it's limitations, it can't grab an attribute that doesn't currently have a value assigned.
Here's a method I sometimes use, it first parses the block list looking for an attribute tag, adding the block name to a list.
This might be overkill in some cases, it was developed for a situation where we might have several block names that include a unique tag identifier and the block list was still being developed. It was also linked up with information that was extracted from a database and meant to potentially transfer information to several hundred blocks with several attributes.
So the first portion can be eliminated and anyone can opt to use the portion starting at (setq block_list (ssget "x" '((0 . "insert") (2 . "block_name")))), removing the ";" preceding it, of course.
Code:
(vl-load-com)
(setq blockname_variable "ATT_VAL")
(setq found_list nil)
(setq blk_table (tblnext "BLOCK" 1))
(while (/= blk_table nil) ;while1
(setq items nil)
(vlax-for item
(vla-item (vla-get-blocks (vla-get-activedocument (vlax-get-acad-object))) (cdr (assoc 2 blk_table)))
(setq items (cons (vlax-vla-object->ename item) items))
)
(setq index 0)
(repeat (length items)
(setq ename (nth index items))
(setq blkrefobj (vlax-ename->vla-object ename))
(if (and (= (cdr (assoc 0 (entget ename))) "ATTDEF") (= (vla-get-TagString blkrefobj) blockname_variable))
(setq found_list (append found_list (list (cdr (assoc 2 blk_table)))))
)
(setq index (+ index 1))
)
(setq blk_table (tblnext "BLOCK"))
)
(setq foundlist_string nil)
(setq foundlist_index 0)
(if found_list
(progn
(repeat (length found_list)
(if foundlist_string
(setq foundlist_string (strcat foundlist_string "," (nth foundlist_index found_list)))
(setq foundlist_string (nth foundlist_index found_list))
)
(setq foundlist_index (+ foundlist_index 1))
)
(setq found_list (list (cons 0 "insert") (cons 2 foundlist_string)))
)
)
(setq block_list (ssget "x" found_list))
;if you know the block name you can use, you don't need to search through the block table.
;this is good in the oft chance you have a wide variety of block names that use the attribute tag
;(setq block_list (ssget "x" '((0 . "insert") (2 . "block_name")))) ; multiple -> (2 . "block1,block2,blokc3")
(if block_list
(repeat (setq block_index (sslength block_list))
(setq obj_name (vlax-ename->vla-object (ssname block_list (setq block_index (- block_index 1)))))
(setq process nil)
(foreach att (append (vlax-invoke obj_name 'GetAttributes) (vlax-invoke obj_name 'GetConstantAttributes))
(cond ((= (vla-get-TagString att) "ATT_VAL") (vla-put-TextString att "NEW TEXT VALUE"))
;search for primary attribute within block and update additional attributes such as SHT_NAME ... SHT_NUM, ISSUE_DATE, etc
((= (vla-get-TagString att) "ADD1_VAL") (vla-put-TextString att "ADDITIONAL ATTRIBUTE ASSOCIATED WITH ATT_VAL"))
((= (vla-get-TagString att) "ADD2_VAL") (vla-put-TextString att "ADDITIONAL ATTRIBUTE ASSOCIATED WITH ATT_VAL"))
((= (vla-get-TagString att) "ADD3_VAL") (vla-put-TextString att "ADDITIONAL ATTRIBUTE ASSOCIATED WITH ATT_VAL"))
)
)
)
)
I apologize for the drawn out code names, I usually try to be a little more descriptive in my code because simple variable names don't usually mean much to me, especially 6 months down the line after I used "e" or "h" for several different programs. LOL!