+ Reply to Thread
Results 1 to 9 of 9
  1. #1
    Junior Member
    Discipline
    Electrical
    Using
    AutoCAD 2018
    Join Date
    Jan 2018
    Posts
    11

    Default Select blocks by attribute TAG

    Registered forum members do not see this ad.

    Hello...I find a lot of solution for select blocks by an attribute values, but I did not find any solution for select all blocks in a file that have an attributes with the same tag.
    For example I want to select all the blocks that have the attribute tag "SIGLA"...I did not find a way by using normal autocad procedures or commands...
    Regards

  2. #2
    Super Member
    Computer Details
    ronjonp's Computer Details
    Operating System:
    Windows 10
    Using
    AutoCAD 2018
    Join Date
    Apr 2009
    Location
    Colorado
    Posts
    666

    Default

    The FIND command can create a selection set of objects with the same value.

  3. #3
    Super Member
    Discipline
    Multi-disciplinary
    Using
    AutoCAD 2015
    Join Date
    Nov 2013
    Posts
    1,280

    Default

    This should work. Sorry for the coding style (I was bored) :

    Code:
    (defun C:test ( / tmp )
      (if 
        (setq tmp
          ((lambda (every L / tmp) (if (apply '= (mapcar 'length (list L (setq tmp (every L))))) tmp))
            (lambda (L / tmp) (if (setq tmp (eval (car L))) (cons tmp (every (cdr L)))))
            '( (if (not vlax-get-acad-object) (alert "(vl-load-com)") t)
              (ssget "_X" '((0 . "INSERT")(66 . 1)))
              (car (nentsel "\nPick attribute: "))
              ''(87 97 116 99 104 97 32 108 111 111 107 105 110 103 32 102 111 114 63)
            )
          )
        )
        (apply
          '(lambda ( c a b d / enx v nSS i e o nm n atts nmL )
            (and a b
              (member '(0 . "ATTRIB") (setq enx (entget b)))
              (setq v (mapcar '(lambda (x) (cdr (assoc x enx))) '(2 1)))
              (setq nSS (ssadd))
              (repeat (setq i (sslength a))
                (or
                  (and 
                    (setq nm (vlax-get (setq o (vlax-ename->vla-object (setq e (ssname a (setq i (1- i)))))) 'EffectiveName))
                    (setq atts (vlax-invoke o 'GetAttributes))
                    nmL (setq n (cdr (assoc nm nmL)))
                    ( (lambda (x) (equal v (mapcar (function vlax-get) (list x x) '(TagString TextString)) 1e-3)) (nth n atts) )
                    (ssadd e nSS)
                  ); and
                  (and
                    (setq n -1)
                    (vl-some (function (lambda (x) (setq n (1+ n)) (if (equal v (mapcar (function (lambda (xx) (vlax-get x xx))) '(TagString TextString)) 1e-3) n))) atts)
                    (setq nmL (cons (cons nm n) nmL))
                    (ssadd e nSS)
                  ); and
                ); or
              ); repeat
            ); and
            (and nSS (/= 0 (sslength nSS)) (sssetfirst nil nSS))
          ); lambda
          tmp
        )
      )
      (princ)
    )
    (vl-load-com) (princ)
    Last edited by Grrr; Yesterday at 12:06 am.

  4. #4
    Quantum Mechanic Lee Mac's Avatar
    Computer Details
    Lee Mac's Computer Details
    Operating System:
    Windows 7 Ultimate (32-bit)
    Discipline
    Multi-disciplinary
    Lee Mac's Discipline Details
    Discipline
    Multi-disciplinary
    Details
    Custom Programming / Software Customisation
    Using
    AutoCAD 2013
    Join Date
    Aug 2008
    Location
    London, England
    Posts
    19,454

    Default

    I don't think the code need be quite so complex - here is a simple example:
    Code:
    (defun c:selbytag ( / a e i n s x )
        
        (setq n "SIGLA")
        
        (if (setq s (ssget "_X" '((0 . "INSERT") (66 . 1))))
            (repeat (setq i (sslength s))
                (setq i (1- i)
                      e (ssname  s i)
                      a (entnext e)
                      x (entget  a)
                )
                (while (and (= "ATTRIB" (cdr (assoc 0 x))) (/= n (strcase (cdr (assoc 2 x)))))
                    (setq a (entnext a)
                          x (entget  a)
                    )
                )
                (or (= "ATTRIB" (cdr (assoc 0 x))) (ssdel e s))
            )
        )
        (sssetfirst nil s) (princ)
    )
    Lee Mac ProgrammingTwitterExchange App StoreDropbox (500MB free)

    With Mathematics there is the possibility of perfect rigour, so why settle for less?

  5. #5
    Quantum Mechanic
    Using
    Civil 3D 2016
    Join Date
    Dec 2005
    Location
    GEELONG AUSTRALIA
    Posts
    10,136

    Default

    Re lee's code, for a block that you can see on screen use this, non hard coded answer.

    Code:
    (setq n (cdr (assoc 2 (entget (car (nentsel "pick attribute"))))))
    A man who never made mistakes never made anything

  6. #6
    Super Member
    Computer Details
    ronjonp's Computer Details
    Operating System:
    Windows 10
    Using
    AutoCAD 2018
    Join Date
    Apr 2009
    Location
    Colorado
    Posts
    666

    Default

    Here's a VL variant:
    Code:
    (defun c:foo (/ s tag)
      (setq tag "SIGLA")
      (if (setq s (ssget '((0 . "INSERT") (66 . 1))))
        (foreach e (vl-remove-if 'listp (mapcar 'cadr (ssnamex s)))
          (and (null (vl-some '(lambda (x) (eq (vla-get-tagstring x) (strcase tag)))
    			  (vlax-invoke (vlax-ename->vla-object e) 'getattributes)
    		 )
    	   )
    	   (ssdel e s)
          )
        )
      )
      (sssetfirst nil s)
      (princ)
    )
    (vl-load-com)
    Last edited by ronjonp; 13th Feb 2018 at 05:06 pm.

  7. #7
    Junior Member
    Discipline
    Electrical
    Using
    AutoCAD 2018
    Join Date
    Jan 2018
    Posts
    11

    Default

    thank you all! tomorrow I'll try them all...
    In the meantime, looking with google, I found other possible solutions...I read what "Information for new members" says about link so, for completeness, I'll pass you the link and it will be ok you'll read what i have found.
    I write to you as soon as possible!

  8. #8
    Junior Member
    Discipline
    Electrical
    Using
    AutoCAD 2018
    Join Date
    Jan 2018
    Posts
    11

    Default

    Hello to everyone! I tried the various lisp and these are my conclusions...I apologize if they are inaccurate but it was a full week, I did not have time and I report the "immediate result"...:
    1) the Grrr's lisp stopped after the pick of the attribute (I'll try it again)...but I like the idea of select by click an attribute!
    2) the ronjob's lisp is ok and I like that it works by a selection area
    3) selbytag is ok and it works also with frozen layer that it is very useful for me...at the moment I prefer this lisp.
    Maybe to exercise I'll try to write a lisp that includes all three...

    The other solution I have found in internet are these:
    https://forums.autodesk.com/t5/autoc...s/td-p/4791745 (very interesting)
    https://forums.autodesk.com/t5/visua...g/td-p/7377964

    the next step is it to extrapolate the values contained in the chosen attribute for count them!
    I tried the express command attext but it generates useless txt file...I will try to find some method that generate file .csv (I hope it exists).

    I love Lee Mac lisp like Count Attributes Value or Block Counter because they organize the data in tables or csv files, but I need to work only with the values of the tag attribute I choose for the selection.

    I thank you again and I greet you

  9. #9
    Super Member
    Discipline
    Multi-disciplinary
    Using
    AutoCAD 2015
    Join Date
    Nov 2013
    Posts
    1,280

    Default

    Registered forum members do not see this ad.

    Quote Originally Posted by itacad View Post
    1) the Grrr's lisp stopped after the pick of the attribute (I'll try it again)...but I like the idea of select by click an attribute!
    Doh! Fixed..

Similar Threads

  1. Replies: 8
    Last Post: 2nd Apr 2017, 08:05 pm
  2. [NEVERMIND] Select blocks by specific attribute value
    By Gotrekk in forum AutoLISP, Visual LISP & DCL
    Replies: 0
    Last Post: 28th Apr 2016, 05:03 pm
  3. How to select an attribute?
    By pkl in forum AutoLISP, Visual LISP & DCL
    Replies: 4
    Last Post: 22nd Nov 2015, 06:52 pm
  4. Cant select attribute
    By Ski_Me in forum AutoCAD 2D Drafting, Object Properties & Interface
    Replies: 1
    Last Post: 2nd Jul 2015, 07:22 pm
  5. Select Blocks by Name & Attribute Property
    By churchill in forum AutoCAD 2D Drafting, Object Properties & Interface
    Replies: 1
    Last Post: 22nd Nov 2013, 01:34 am

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts