Announcements

Starting in December, we will archive content from the community that is 10 years and older. This FAQ provides more information.

Loop blocks, search and match attribute value and change another value.

andreas7ZYXQ
Advocate
Advocate

Loop blocks, search and match attribute value and change another value.

andreas7ZYXQ
Advocate
Advocate

Im, trying to solve an easy problem for someone with more knowledge than me but for me a big one. 
I have multiple blocks/dynamic blocks inside a drawing and i would like to see if it cointain a attribute with specific name and value and then if it exists update another one.

I would be more than happy if someone could finish and help me with this code. and i would be even more grateful if comments in the code.

 

So.
Loop all blocks
Has attribute SEKTION with value sek_num
Then:
Change attribute SEK_SIDA to sek_page

This i what i got so far and finding some code from lee mac.

 

(defun C:adress_se_blad (/ ss i ename)

(setq sek_num (getstring "Search for section number:"))
(setq sek_page (getstring "In wich page?"))

(while
 (not(eq "SEQEND"(cdr(assoc 0(entget(setq bEnt(entnext bEnt)))))))
 
 (if (eq "SEKTION" (cdr (assoc 2 (entget bEnt))));Has attribute name
   (entupd
     (cdr
       (assoc -1
         (entmod(subst(cons 1 "Lee Mac") (assoc 1 (entget bEnt)) (entget bEnt)
           )
         )
       )
     )
   )
 )
)
);end

 

 

0 Likes
Reply
Accepted solutions (1)
157 Views
2 Replies
Replies (2)

Sea-Haven
Mentor
Mentor

For me use selection set, using "INSERT" filter, with block has atrributes, You can use VL get-attributes, then a simple loop to check is Tagname = SEK_SIDA and so on.

 

Post a sample dwg with a before and after after. Say at least 4 blocks.

0 Likes

andreas7ZYXQ
Advocate
Advocate
Accepted solution

Lee Mac solved my problem, found a script and manage to change it so it works for me.

(defun c:adress_se_blad ( / des ent enx flg fnd idx new sel tag )
   (setq tag "SEK_SIDA"
   )
   (if (setq sel (ssget "_X" (list '(0 . "INSERT") '(66 . 1))))
       (progn
           (setq fnd (strcase (getstring t "\nSpecify attribute value to find: "))
                 new (cons  1 (getstring t (strcat "\nSpecify new value for \"" tag "\" attribute: ")))
           )
           (repeat (setq idx (sslength sel))
               (setq ent (entnext (ssname sel (setq idx (1- idx))))
                     enx (entget ent)
                     des nil
                     flg nil
               )
               (while (= "ATTRIB" (cdr (assoc 0 enx)))
                   (cond
                       (   (= tag (strcase (cdr (assoc 2 enx))))
                           (setq des enx)
                       )
                       (   (or flg (setq flg (wcmatch (strcase (cdr (assoc 1 enx))) fnd))))
                   )
                   (setq ent (entnext ent)
                         enx (entget  ent)
                   )
               )
               (if (and des flg)
                   (if (entmod (subst new (assoc 1 des) des))
                       (entupd (cdr (assoc -1 des)))
                   )
               )
           )
       )
       (princ (strcat "\nNo blocks found in the active drawing."))
   )
   (princ)
)
0 Likes