So you want to select all blocks within a drawing
Code:
(setq ss (ssget '((0 . "INSERT"))))
Now you have your selection set of blocks, you need to step through them to find the attributes for each one. But you only want to change a certain one, so we need to know the tag name. For now we will use 'TAGNAME'.
First we need to step through the selection set. We will use the counter way.
Code:
(setq cnt 0)
(while (setq Ent (ssname ss cnt))
This is saying while 'cnt' (a number) equals and entity ('Ent') within the selection set ('ss') we want something to happen. When then number is larger than the amount in the selection set it will return nil, so the while loop will end.
So 'Ent' is out block entity, so now we can step though the blocks sub-entities (attributes).
Code:
(while
(and
(setq Ent (entnext Ent))
(setq EntData (entget Ent))
(= (cdr (assoc 0 EntData)) "ATTRIB")
)
Here we have to use entnext to get the sub-entity. Once we get one, entnext will go to the next one sub entity. Then we get the dxf code that makes up the entity (see help if you need to understand what this is). Then we want to make sure that the entity is an attribute, that is what 'ATTRIB' stands for.
Now that we are looking at all the attributes, we want to only change the one we want 'TAGNAME' (dxf code 2 is the tag), so we will use an if statement to test it, and if it is the name we want we will change the text of it (dxf code 1).
Code:
(if (= (cdr (assoc 2 EntData)) "TAGNAME")
(entmod (subst (cons 1 "Updated Text!") (assoc 1 EntData) EntData))
)
Hope that makes sense.
So the whole code will look like
Code:
(setq ss (ssget '((0 . "INSERT"))))
(setq cnt 0)
(while (setq Ent (ssname ss cnt))
(while
(and
(setq Ent (entnext Ent))
(setq EntData (entget Ent))
(= (cdr (assoc 0 EntData)) "ATTRIB")
)
(if (= (cdr (assoc 2 EntData)) "TAGNAME")
(entmod (subst (cons 1 "Updated Text!") (assoc 1 EntData) EntData))
)
)
(setq cnt (1+ cnt)) ; used to get the next item in the selection set
)