Select All Blocks by typing Attributes tag

Select All Blocks by typing Attributes tag

sergiu_ciuhnenco
Advocate Advocate
4,347 Views
15 Replies
Message 1 of 16

Select All Blocks by typing Attributes tag

sergiu_ciuhnenco
Advocate
Advocate

Need a help from you guys, want to select all blocks by typing the tag of the attribute POZITIA, sound easy but I can't make it done
Thanks in advance  !!!

0 Likes
Accepted solutions (3)
4,348 Views
15 Replies
Replies (15)
Message 2 of 16

john.uhden
Mentor
Mentor
Accepted solution
100% untested...

(defun POZITIAS ( / ss I obj atts found) (vl-load-com) (and (setq ss (ssget "X" '((0 . "INSERT")(66.1)))) (setq found (ssadd)) (repeat (setq I (sslength ss)) (setq e (ssname ss (setq I (1- I)))) (setq obj (vlax-ename->vla-object e)) (setq atts (vla-getattributes obj)) (setq atts (vlax-variant-value atts)) (foreach att (vlax-safearray->list atts) (and (= (strcase (vlax-get att 'Tagstring)) "POZITIA") (ssadd e found) ) ) ) found )

You can probably use it in most any AutoCAD native command, e.g.

 

Command: Select

  Select objects: (pozitias)

John F. Uhden

Message 3 of 16

Ranjit_Singh
Advisor
Advisor
Accepted solution

Maybe like this. Minimal testing. There maybe additional considerations depending on the type of blocks; dynamic, nested etc.

(defun c:somefunc  (/ ent etdata nf ss1 ss2)
 (setq ss1 (ssget "_x" (list '(0 . "insert") '(66 . 1))))
 (and ss1
      (setq ss2 (ssadd))
      (mapcar '(lambda (x)
                (setq ent x
                      nf  t)
                (while (and (/= "SEQEND" (cdr (assoc 0 (setq etdata (entget ent))))) nf)
                 (and (= (cdr (assoc 0 etdata)) "ATTRIB")
                      (= "Pozitia" (cdr (assoc 2 etdata)))
                      (setq ss2 (ssadd x ss2)
                            nf  ()))
                 (setq ent (entnext ent))))
              (mapcar 'cadr (ssnamex ss1))))
 (sssetfirst nil ss2)
 (princ))

Block_from_attrib.gif

 

Message 4 of 16

sergiu_ciuhnenco
Advocate
Advocate

100% non working 🙂
I tried but unfortunately it doesn't work
Could you please rewiew it !!!
But for the last time let me explain what I want : run lisp just type the tag of attribute POZITIA , for example "50" and select all blocks with this attribute tag

0 Likes
Message 5 of 16

sergiu_ciuhnenco
Advocate
Advocate

Hi Ranjit.Singh !!!
your lisp is working but it does not give the solution for wich I was asking for,let me explain what I want : run lisp just type the tag of attribute POZITIA , for example "50" and select all blocks with this attribute tag

0 Likes
Message 6 of 16

sergiu_ciuhnenco
Advocate
Advocate

probably I was was asking wrong question , I think I have to say not attribute tag but attribute value

0 Likes
Message 7 of 16

DannyNL
Advisor
Advisor
Accepted solution

So you want to select based on attribute tag AND value? Right?

 

Well, this is my version. Basically almost the same as John's but in my own way of non-efficient and longer coding Smiley Wink

The attribute tag has been fixed on the value "POZITIA" but if you want to search for other tags as well, just remove the semicolon on the first line and put it before the second line so you will get an additional question tom provide the tag as well.

 

(defun c:SelBlk (/ SB_FindAtt SB_FindVal SB_Selection SB_FoundSel SB_Count SB_BlkEnt)
   ;(setq SB_FindAtt (getstring "\nAttribute Tag  : "))
   (setq SB_FindAtt "POZITIA")
   (setq SB_FindVal (getstring "\nAttribute Value: "))
   (if
      (setq SB_Selection (ssget "_X" '((0 . "INSERT")(66 . 1))))
      (progn
         (setq SB_FoundSel (ssadd))
         (setq SB_Count 0)
         (repeat (sslength SB_Selection)
            (setq SB_BlkEnt (ssname SB_Selection SB_Count))
            (if
               (FindAtt (vlax-ename->vla-object SB_BlkEnt) SB_FindAtt SB_FindVal)
               (ssadd SB_BlkEnt SB_FoundSel)
            )
            (setq SB_Count (1+ SB_Count))
         )
         (if
            (> (sslength SB_FoundSel) 0)
            (sssetfirst nil SB_FoundSel)
         )
      )
   )
   (princ)
)

(defun FindAtt (FA_BlkObject FA_AttTag FA_AttVal / FA_Attributes FA_Return)
   (if
      (and
         (= (type FA_BlkObject) 'VLA-OBJECT)
         (= (vla-get-ObjectName FA_BlkObject) "AcDbBlockReference")
         (= (vla-get-HasAttributes FA_BlkObject) :vlax-true)
         (= (type FA_AttTag) 'STR)
         (= (type FA_AttVal) 'STR)         
      )
      (progn         
         (setq FA_Attributes (vlax-safearray->list (vlax-variant-value (vla-GetAttributes FA_BlkObject))))
         (foreach FA_Item FA_Attributes
            (if
               (and
                  (= (strcase (vla-get-TagString  FA_Item)) (strcase FA_AttTag))
                  (= (strcase (vla-get-TextString FA_Item)) (strcase FA_AttVal))
               )
               (setq FA_Return T)
            )
         )
      )
   )
   FA_Return
)  

 

Message 8 of 16

sergiu_ciuhnenco
Advocate
Advocate

Thanks YOU !!!!!!
Exactly what I needed !!!
It will be very helpful !!!

0 Likes
Message 9 of 16

DannyNL
Advisor
Advisor

Glad we could help Smiley Happy

0 Likes
Message 10 of 16

JerVanK
Contributor
Contributor

I'm trying to run this Lisp in 2016 and only having partial luck.

these are my 2 question that seem to be causing issues

Does the tag valve search entry have to be complete? (yes)

and can the value have any spaces at the end? (no)

I'm trying to select block with a computer generated valve of "#05 " (with 2 spaces after the number).

 

Is these a way to find and select a block with these attribute tags?

0 Likes
Message 11 of 16

dlanorh
Advisor
Advisor

@JerVanK wrote:

I'm trying to run this Lisp in 2016 and only having partial luck.

these are my 2 question that seem to be causing issues

Does the tag valve search entry have to be complete? (yes)

and can the value have any spaces at the end? (no)

I'm trying to select block with a computer generated valve of "#05 " (with 2 spaces after the number).

 

Is these a way to find and select a block with these attribute tags?


Is "#05  " the block name, the attribute tag name or the tag value (textstring)?

 

If it is a tag name or tag value a simple adjustment to the code can be made to account for the 2 spaces if they are at the end of the string

I am not one of the robots you're looking for

0 Likes
Message 12 of 16

john.uhden
Mentor
Mentor
(wcmatch str "*# ")

John F. Uhden

0 Likes
Message 13 of 16

nanja_c86
Enthusiast
Enthusiast

I was searching on the forum for this kind of lisp, got this one this is helpfull.
But need some changes as below.

Need to Count the number of blocks and place a circle to its ins point and show alert message that this many circled placed.

 

Thanks in advance.

 

0 Likes
Message 14 of 16

Sea-Haven
Mentor
Mentor

To nanaj.c86 this would be better posted as a new request. Need to count the blocks just a grand total or each name counted ???

0 Likes
Message 15 of 16

nanja_c86
Enthusiast
Enthusiast

@Sea-Haven 

As per your suggestion i have posted as new, explained there what i  need. 

here is he link

Count and place circle to All Blocks by typing Attributes tag values - Autodesk Community

 

 

Thank you.

0 Likes
Message 16 of 16

Kyle.para
Advocate
Advocate

I have used the code provided by Danny and it works, but I need it tweaked a little bit , if anyone cares to lend a hand.

 

We have a block that has the same attribute tag and block name. Except one attribute value is LC01, LC02 etc. and the other one is SD01, SD02 etc. We only need to select all of the LC## ones.

 

I can get it to work by changing the code to select LC50 for example, but how can I modify it so that it finds all the blocks with a value for the attribute with a wildcard. Like LC** for example.

 

(setq SB_FindVal (getstring "\nAttribute Value: "))
(setq SB_FindVal "LC50")

0 Likes