Selection set based on attribute value.

Discussion in 'AutoCAD' started by Casey Roberts, Mar 1, 2005.

  1. How would I go about making a selection set of all blocks that have an
    attribute with a particular string value?

    TIA... Casey
     
    Casey Roberts, Mar 1, 2005
    #1
    1. Advertisements

  2. Casey Roberts

    BillZ Guest

    What are you going to use it for?

    Bill
     
    BillZ, Mar 1, 2005
    #2
    1. Advertisements

  3. You don't. You can limit the selection set to
    inserts that have attributes then you will have
    to step though the selection set testing each
    item for the desired value(s).

    To limit the selection set, include (66 . 1) in
    the filter to ssget.
     
    Jason Piercey, Mar 1, 2005
    #3
  4. Casey Roberts

    ECCAD Guest

    Casey,
    Try this one:
    ;;
    ;; Local DXF Function
    (defun dxf (code elist)
    (cdr (assoc code elist))
    ); end function dxf

    (defun add_to_ss ( entity )
    (if (not ss)
    ; make blank selection set
    (progn
    (setq ss (ssadd (entlast)))
    (setq ss (ssdel (entlast) ss))
    ); progn
    ); if
    ; else, add entity to ss
    (setq ss (ssadd entity ss))
    ); end function

    (defun sset ( strng / ss1 C ent entx en val elist )
    ; first, get all blocks
    (setq ss1 (ssget "X" (list (cons 0 "INSERT"))))
    ; step thru ss1, get each entity, and get attribute values
    (if ss1
    (progn
    (setq C 0)
    (repeat (sslength ss1)
    (setq ent (ssname ss1 C)); get each entity
    (setq entx ent); case of a match
    (setq elist (entget ent)); each entity 'list' item.
    (if (/= (cdr (assoc 66 elist)) nil); Attrib's following
    (progn
    (setq en (entget (entnext (dxf -1 elist))))
    (setq ent en)
    (if (= "ATTRIB" (cdr (assoc 0 en))); case of attributes
    (progn
    (setq val (cdr (assoc 1 en)))
    (if (= val strng)
    (add_to_ss entx)
    ); if
    ); progn
    ); if
    (while (/= "SEQEND" (cdr (assoc 0 en)))
    (setq en (entget (entnext (dxf -1 ent))))
    (if (= "ATTRIB" (cdr (assoc 0 en))); case of attributes
    (progn
    (setq val (cdr (assoc 1 en)))
    (if (= val strng)
    (add_to_ss entx)
    ); if
    (setq ent en); swap next
    ); progn
    ); if
    ); end while
    ); end progn
    ); end if
    (setq C (+ C 1)); increment counter C to get next entity.
    ); end repeat
    ); end progn
    ); end if
    ); end function
    ;;
    ;; Get search string - can contain spaces
    ;;
    (setq str (getstring 1 "\nString Value to Search for : "))
    (sset str)
    (if (> (sslength ss) 0)
    (progn
    (setq num (itoa (sslength ss)))
    (prompt (strcat "\nSelection Set SS contains " num " Blocks with attribute value of " str " "))
    ); progn
    ); if
    (princ)


    Bob
     
    ECCAD, Mar 1, 2005
    #4
  5. Casey Roberts

    BillZ Guest

    When I am changing/working with attribute values, I use something like this:

    <snip>
    Code:
    ;03/01/05
    ;To find Block reference & Attribute Values.
    ;Usage: (AttTagEdit "BlockReference name" "Attribute tag name")
    ;
    (defun AttTxt (BlkNam TagStr / *mspace*)
    (vl-load-com)
    (setq *mspace* (vla-get-modelspace (vla-get-activedocument (vlax-get-acad-object)))
    )
    ;;;
    (vlax-for item *mspace*
    (if (and (= (vla-get-objectname item)"AcDbBlockReference")   ;if a block ref.
    (= (strcase (vla-get-name item)) BlkNam)            ;if you want specific block names.
    )
    (mapcar '(lambda (a)
    (if (= (vla-get-TagString a) TagStr)      ;could be TextString instead.
    
    ;;;;do whatever here, i.e. (vla-put-TextString
    )
    )
    (vlax-safearray->list
    (variant-value
    (vla-GetAttributes item)
    )
    )
    )
    )
    )
    ;;;
    (princ)
    )
    Don't have to mess with the SS that way.


    Bill
     
    BillZ, Mar 1, 2005
    #5
  6. I want a slection set of all blocks that have attributes with a value that
    contains a certain word. I'm cleaning up arch drawings and just need to
    delete a selection of blocks from the drawing. The only unique part of the
    particular blocks I want to delete, is an attribute value.

    There are some blocks within the drawing that I want to retain (and
    therefore do not want in my selection set) that have the same
    rotation,scale,name,layer etc etc etc except for the attribute value.

    Casey
     
    Casey Roberts, Mar 3, 2005
    #6
  7. Casey Roberts

    BillZ Guest

    This should do it.

    Remeber this looks for the actual attribute value NOT the tag value.
    The values entered are case sensitive, i.e. "testblock" is not the same as "TestBlock" so be exact.

    Code:
    ;03/03/05;To Delete Block Reference w\Attribute Values.
    ;Usage: (DelBlk-w-Att "Blockname" "AttributeValue")
    ;Case specific on blockname and att value.
    ;
    ;
    (defun DelBlk-w-Att (BlkNam TextStr / *mspace* ItemLst)
    (vl-load-com)
    (setq *mspace* (vla-get-modelspace (vla-get-activedocument (vlax-get-acad-object)))
    )
    (vlax-for item *mspace*
    (if (and (= (vla-get-objectname item)"AcDbBlockReference")   ;if a block ref.
    (= (vla-get-name item) BlkNam)
    )
    (mapcar '(lambda (a)
    (if (= (vla-get-TextString a) TextStr)
    (setq ItemLst (cons item ItemLst))
    )
    )
    (vlax-safearray->list
    (variant-value
    (vla-GetAttributes item)
    )
    )
    )
    )
    )
    (if ItemLst
    (foreach n ItemLst
    (vla-delete n)
    )
    )
    (princ)
    )
    Bill
     
    BillZ, Mar 3, 2005
    #7
  8. Casey Roberts

    BillZ Guest

    I should add:

    This subr is set for model space only.

    Let me know if you need something different.


    Bill
     
    BillZ, Mar 3, 2005
    #8
  9. Cool... That worked... But I've got one last question.... could it be
    modified so the text string could accept wildcards?

    Casey
     
    Casey Roberts, Mar 3, 2005
    #9
  10. Casey Roberts

    MP Guest

    change
    (if (= (vla-get-TextString a) TextStr)
    to
    (if (wcmatch (vla-get-TextString a) TextStr)


     
    MP, Mar 3, 2005
    #10
  11. Casey Roberts

    Jeff Mishler Guest

    Bill, just curious why you iterate the entire MS collection. Which, as you
    point out, only works for MS. Whereas if you used a selection set that
    selected All inserts that have attributes it would cut down on processing
    time tremendously, and would have the benefit of getting even those in PS

    (defun delBlk-w-attribute (bname txtstr / )
    (if (setq ss (ssget "x" (list '(0 . "INSERT")(cons 2 bname) '(66 . 1))))
    (progn
    (setq idx -1)
    (while (< (setq idx (1+ idx)) (sslength ss))
    ;;also shows a method of accessing the attributes
    ;; without variants and safearrays
    (setq obj (vlax-ename->vla-object (ssname ss idx)))
    (setq atts (vlax-invoke obj 'getattributes))
    (foreach att atts
    (if (wcmatch (vla-get-textstring att) txtstr);added wildcard
    functionality
    (progn
    (vla-delete obj)
    (setq atts nil)
    )
    )
    )
    )
    )
    )
    )
     
    Jeff Mishler, Mar 3, 2005
    #11
  12. sweeeeet.....

    Thanks


     
    Casey Roberts, Mar 3, 2005
    #12
  13. Casey Roberts

    MP Guest

    welcome,
    be sure to note Jeff's last post as well.
    Mark
     
    MP, Mar 4, 2005
    #13
  14. Casey Roberts

    BillZ Guest

    Hi Jeff,

    Good point.

    I snipped most of this from some other work I've been doing. Must've got carried away. ;-)

    FWIW: I figured most blocks like this would reside in model space anyway.

    Thanks for the tip.

    Bill
     
    BillZ, Mar 4, 2005
    #14
  15. For my purposes, the blocks do only reside in model space, so it works for
    what I need now. But who knows what the future will bring.

    Thanks again everyone for all your help.

    Casey
     
    Casey Roberts, Mar 4, 2005
    #15
  16. Casey Roberts

    BillZ Guest

    Glad it worked for you.

    (with a little assistance from my "helpers")

    ;^)

    Bill
     
    BillZ, Mar 4, 2005
    #16
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.