List of all block definitions

Discussion in 'AutoCAD' started by dave2, Apr 7, 2004.

  1. dave2

    dave2 Guest

    Using LISP, how can I make a list of all block definitons in the drawing?

    I know I can use the (command -insert "?" "*") to return a list of block definitions and then cut and paste the list, but what is the equivalent of this command using lisp to just return a nice neat list of block definitions only for using in a list? Thanks!!!
     
    dave2, Apr 7, 2004
    #1
  2. dave2

    JPSanders Guest

    For a selection set containing all blocks:

    (setq eset(ssget "X" (list (cons 0 "INSERT"))))

    For a list containing the names of all blocks:

    (setq bList(list))
    (setq tbl(tblnext "BLOCK" T))
    (setq bList(append bList(list (cdr(assoc 2 tbl)))))
    (while(setq tbl(tblnext "BLOCK"))
    (setq bList(append bList(list (cdr(assoc 2 tbl)))))
    )
     
    JPSanders, Apr 7, 2004
    #2
  3. dave2

    Rudy Tovar Guest

    Don't forget to filter out Xrefs.

    definitions and then cut and paste the list, but what is the equivalent of
    this command using lisp to just return a nice neat list of block definitions
    only for using in a list? Thanks!!!
     
    Rudy Tovar, Apr 7, 2004
    #3
  4. dave2

    Dan Allen Guest

    express tools BCOUNT?

    --
    ;;; For reply, change numbers to decimal


    definitions and then cut and paste the list, but what is the equivalent of
    this command using lisp to just return a nice neat list of block definitions
    only for using in a list? Thanks!!!
     
    Dan Allen, Apr 7, 2004
    #4
  5. dave2

    Don Butler Guest

    Cool! Way faster than the old function I have used.

    Tried it on a file I have with 6400 blocks.

    It's as fast as my ActiveX version.

    My old Lisp version used to hiccup with that many names.

    Dave:
    ;;;This should remove XREFS
    (vl-remove-if (function (lambda (x) (wcmatch x "*|*")))(table "BLOCK"))

    Don Butler


     
    Don Butler, Apr 8, 2004
    #5
  6. dave2

    Paul Turvill Guest

    Doesn't BCOUNT count *insertions*? The OP asked about block *definitions*...
    ___
     
    Paul Turvill, Apr 8, 2004
    #6
  7. dave2

    dave2 Guest

    Don,

    I'm having trouble getting this code

    (vl-remove-if (function (lambda (x) (wcmatch x "*|*")))(table "BLOCK"))

    to remove XREF's from the list. I need it to remove all unnamed blocks like (*X40 etc.) from the list as well.

    Any suggestions?
     
    dave2, Apr 8, 2004
    #7
  8. Glad I could help :)
     
    Jason Piercey, Apr 8, 2004
    #8
  9. Try something like this. Adjust the filter as required.

    ; function to return a list of block names
    ; excluding blocks in xrefs, hatch pattern
    ; blocks, anonymous blocks, and dimensions
    ; Based on Mr. Puckett's (table) function.

    (defun getNativeBlockNames (/ data name lst)
    (while (setq data (tblnext "block" (null data)))
    (setq name (cdr (assoc 2 data)))
    (if (wcmatch name "*|*,`*X*,`*U*,`*D*")
    nil
    (setq lst (cons name lst))
    )
    )
    (reverse lst)
    )
     
    Jason Piercey, Apr 8, 2004
    #9
  10. dave2

    dave2 Guest

    Jason,

    Very Cool!!! Thank you all...perfect! Much appreciated....
     
    dave2, Apr 8, 2004
    #10
  11. If what you are saying is that you only want primary
    blocks, that is, no xrefs, no xref dependent blocks,
    and no anonymous blocks then perhaps ...
    Code:
    
    (defun GetPrimaryBlockNames ( / data result )
    (while (setq data (tblnext "block" (null data)))
    ;; if it's not [xref, xref dependent, anonymous] ...
    (if (zerop (logand 21 (cdr (assoc 70 data))))
    (setq result
    (cons (cdr (assoc 2 data))
    result
    )
    )
    )
    )
    ;; assume for this case that
    ;; sorted would be preferred
    (if (< 1 (length result))
    (acad_strlsort result)
    result
    )
    )
    
    Note: 21 = 01 (anonymous) + _
    04 (xref) + _
    16 (xref dependent)
    
    
    PS: Thank you Jason :)

    Don,

    I'm having trouble getting this code

    (vl-remove-if (function (lambda (x) (wcmatch x "*|*")))(table "BLOCK"))

    to remove XREF's from the list. I need it to remove all unnamed blocks like (*X40 etc.) from the list as well.

    Any suggestions?
     
    michael puckett, Apr 8, 2004
    #11
  12. dave2

    Dan Allen Guest

    Given the OP mentioned copy/pasting a text screen, I thought perhaps the
    request was for a low-tech solution. Hey the advice didn't cost him
    anything...
     
    Dan Allen, Apr 8, 2004
    #12
  13. dave2

    Don Butler Guest

    Very nice!

    Don

    like (*X40 etc.) from the list as well.
     
    Don Butler, Apr 8, 2004
    #13
  14. dave2

    Don Butler Guest

    Looks like Jason and Michael took care of that.

    Don

    like (*X40 etc.) from the list as well.
     
    Don Butler, Apr 8, 2004
    #14
  15. dave2

    Don Butler Guest

    Michael:

    I hope you don't mind, but I have created a general purpose routine
    utilizing your code.

    ;|TABLE70 Bits

    BLOCK
    70 Block-type flags (bit-coded values, may be combined):
    0 = Indicates none of the following flags apply
    1 = This is an anonymous block generated by hatching, associative
    dimensioning, other internal operations, or an application
    2 = This block has non-constant attribute definitions (this bit is not set
    if the block has any attribute definitions that are constant, or has no
    attribute definitions at all)
    4 = This block is an external reference (xref)
    8 = This block is an xref overlay
    16 = This block is externally dependent
    32 = This is a resolved external reference, or dependent of an external
    reference (ignored on input)
    64 = This definition is a referenced external reference (ignored on input)

    LAYER
    70 Standard flags (bit-coded values):
    1 = Layer is frozen; otherwise layer is thawed
    2 = Layer is frozen by default in new viewports
    4 = Layer is locked
    16 = If set, table entry is externally dependent on an xref
    32 = If both this bit and bit 16 are set, the externally dependent xref has
    been successfully resolved
    64 = If set, the table entry was referenced by at least one entity in the
    drawing the last time the drawing was edited. (This flag is for the benefit
    of AutoCAD commands. It can be ignored by most programs that read DXF files
    and need not be set by programs that write DXF files)

    LTYPE
    70 Standard flag values (bit-coded values):
    16 = If set, table entry is externally dependent on an xref
    32 = If both this bit and bit 16 are set, the externally dependent xref has
    been successfully resolved
    64 = If set, the table entry was referenced by at least one entity in the
    drawing the last time the drawing was edited. (This flag is for the benefit
    of AutoCAD commands. It can be ignored by most programs that read DXF files
    and need not be set by programs that write DXF files)

    STYLE
    70 Standard flag values (bit-coded values):
    1 = If set, this entry describes a shape
    4 = Vertical text
    16 = If set, table entry is externally dependent on an xref
    32 = If both this bit and bit 16 are set, the externally dependent xref has
    been successfully resolved
    64 = If set, the table entry was referenced by at least one entity in the
    drawing the last time the drawing was edited. (This flag is for the benefit
    of AutoCAD commands. It can be ignored by most programs that read DXF files
    and need not be set by programs that write DXF files)

    UCS
    70 Standard flag values (bit-coded values):
    16 = If set, table entry is externally dependent on an xref
    32 = If both this bit and bit 16 are set, the externally dependent xref has
    been successfully resolved
    64 = If set, the table entry was referenced by at least one entity in the
    drawing the last time the drawing was edited. (This flag is for the benefit
    of AutoCAD commands. It can be ignored by most programs that read DXF files
    and need not be set by programs that write DXF files)

    VIEW
    70 Standard flag values (bit-coded values):
    1 = If set, this is a paper space view
    16 = If set, table entry is externally dependent on an xref
    32 = If both this bit and bit 16 are set, the externally dependent xref has
    been successfully resolved
    64 = If set, the table entry was referenced by at least one entity in the
    drawing the last time the drawing was edited. (This flag is for the benefit
    of AutoCAD commands. It can be ignored by most programs that read DXF files
    and need not be set by programs that write DXF files)

    VPORT
    70 Standard flag values (bit-coded values):
    16 = If set, table entry is externally dependent on an xref
    32 = If both this bit and bit 16 are set, the externally dependent xref has
    been successfully resolved
    64 = If set, the table entry was referenced by at least one entity in the
    drawing the last time the drawing was edited. (This flag is for the benefit
    of AutoCAD commands. It can be ignored by most programs that read DXF files
    and need not be set by programs that write DXF files)

    (table70 "LAYER" 5)
    (table70 "BLOCK" 21)...This is the same result as GetPrimaryBlockNames

    |;

    (defun table70 (table bit / data result)
    (while (setq data (tblnext table (null data)))
    (if (zerop (logand bit (cdr (assoc 70 data))))
    (setq result
    (cons (cdr (assoc 2 data))
    result
    )
    )
    )
    )
    (if (< 1 (length result))
    (acad_strlsort result)
    result
    )
    )

    I'd appreciate any comments you may have about potential problems using this
    approach.

    Thanks,

    Don




     
    Don Butler, Apr 8, 2004
    #15
  16. dave2

    Don Butler Guest

    Michael,

    It looks like I need to study your code first before attempting
    modifications. :-(

    It doesn't work.

    Can what I have attempted to do be done?

    Don

     
    Don Butler, Apr 8, 2004
    #16
  17. dave2

    Don Butler Guest

    OK, works now.

    I see now that you were returning an invert result.

    This now returns both invert and non-invert results.

    Well, back to work.

    Don


    ;|TABLE70 Bits

    BLOCK
    70 Block-type flags (bit-coded values, may be combined):
    0 = Indicates none of the following flags apply
    1 = This is an anonymous block generated by hatching, associative
    dimensioning, other internal operations, or an application
    2 = This block has non-constant attribute definitions (this bit is not set
    if the block has any attribute definitions that are constant, or has no
    attribute definitions at all)
    4 = This block is an external reference (xref)
    8 = This block is an xref overlay
    16 = This block is externally dependent
    32 = This is a resolved external reference, or dependent of an external
    reference (ignored on input)
    64 = This definition is a referenced external reference (ignored on input)

    LAYER
    70 Standard flags (bit-coded values):
    1 = Layer is frozen; otherwise layer is thawed
    2 = Layer is frozen by default in new viewports
    4 = Layer is locked
    16 = If set, table entry is externally dependent on an xref
    32 = If both this bit and bit 16 are set, the externally dependent xref has
    been successfully resolved
    64 = If set, the table entry was referenced by at least one entity in the
    drawing the last time the drawing was edited. (This flag is for the benefit
    of AutoCAD commands. It can be ignored by most programs that read DXF files
    and need not be set by programs that write DXF files)

    LTYPE
    70 Standard flag values (bit-coded values):
    16 = If set, table entry is externally dependent on an xref
    32 = If both this bit and bit 16 are set, the externally dependent xref has
    been successfully resolved
    64 = If set, the table entry was referenced by at least one entity in the
    drawing the last time the drawing was edited. (This flag is for the benefit
    of AutoCAD commands. It can be ignored by most programs that read DXF files
    and need not be set by programs that write DXF files)

    STYLE
    70 Standard flag values (bit-coded values):
    1 = If set, this entry describes a shape
    4 = Vertical text
    16 = If set, table entry is externally dependent on an xref
    32 = If both this bit and bit 16 are set, the externally dependent xref has
    been successfully resolved
    64 = If set, the table entry was referenced by at least one entity in the
    drawing the last time the drawing was edited. (This flag is for the benefit
    of AutoCAD commands. It can be ignored by most programs that read DXF files
    and need not be set by programs that write DXF files)

    UCS
    70 Standard flag values (bit-coded values):
    16 = If set, table entry is externally dependent on an xref
    32 = If both this bit and bit 16 are set, the externally dependent xref has
    been successfully resolved
    64 = If set, the table entry was referenced by at least one entity in the
    drawing the last time the drawing was edited. (This flag is for the benefit
    of AutoCAD commands. It can be ignored by most programs that read DXF files
    and need not be set by programs that write DXF files)

    VIEW
    70 Standard flag values (bit-coded values):
    1 = If set, this is a paper space view
    16 = If set, table entry is externally dependent on an xref
    32 = If both this bit and bit 16 are set, the externally dependent xref has
    been successfully resolved
    64 = If set, the table entry was referenced by at least one entity in the
    drawing the last time the drawing was edited. (This flag is for the benefit
    of AutoCAD commands. It can be ignored by most programs that read DXF files
    and need not be set by programs that write DXF files)

    VPORT
    70 Standard flag values (bit-coded values):
    16 = If set, table entry is externally dependent on an xref
    32 = If both this bit and bit 16 are set, the externally dependent xref has
    been successfully resolved
    64 = If set, the table entry was referenced by at least one entity in the
    drawing the last time the drawing was edited. (This flag is for the benefit
    of AutoCAD commands. It can be ignored by most programs that read DXF files
    and need not be set by programs that write DXF files)

    (table70 TABLE BIT INVERT)

    Non_frozen Layers
    (table70 "LAYER" 1 t)

    Frozen Layers
    (table70 "LAYER" 1 nil)

    Non-Anonymous Blocks
    (table70 "BLOCK" 1 t)

    Anonymous Blocks
    (table70 "BLOCK" 1 nil)

    Localized Blocks
    (table70 "BLOCK" 21 t)

    |;

    (defun table70 (table bit invert / data result arg)
    (if invert
    (setq arg '(zerop (logand bit (cdr (assoc 70 data)))))
    (setq arg '(= bit (logand bit (cdr (assoc 70 data)))))
    )
    (while (setq data (tblnext table (null data)))
    (if (eval arg)
    (setq result
    (cons (cdr (assoc 2 data))
    result
    )
    )
    )
    )
    (if (< 1 (length result))
    (acad_strlsort result)
    result
    )
    )
     
    Don Butler, Apr 8, 2004
    #17
  18. dave2

    BTO Guest

    thx for your code

    only to say, I saw anonymous block name like *Nxxxxx

    may be :

    (if (wcmatch name "*|*,`**")



    Bruno Toniutti
     
    BTO, Apr 9, 2004
    #18
  19. I don't mind, though I probably wouldn't bother myself: the law
    of diminishing returns starts to come into play when you try to
    make functions "too generic". Where that line is varies of course.

    If I were going to go add a function like this to my library I'd
    probably omit the sort code, as 99 times out of 100 you probably
    don't require a sorted result, thus paying an unnecessary performance
    hit the majority of the time. One option might be to add sorting
    as an option via a flag (parameter): (table name mask sorted)

    One potential problem is that all of a table entry's various statii
    are encoded to the 70 group, so a general 'LOGical AND' may not
    always work the way you envision it might, that is, sometimes you'll
    be interested in the entry if the 'AND'ed result is 0, other times
    when it is non zero or worse, combinations. Still DOable of course,
    it just requires more flexibility in your code (back to my first
    comment).

    Anyway, play with it -- if it helps or contributes to your coding
    great.

    Thanks again for asking.
     
    michael puckett, Apr 9, 2004
    #19
  20. dave2

    Jamie Duncan Guest

    Thanks for the nice simple code Michael - I like the use of null - solves
    that rewind thing nicely.

    And I wouldn't bother to add sorting either - if you need to sort the list
    it's very simple to insert the function call into a setq sort thing.


    --
    Jamie Duncan

    "How wrong it is for a woman to expect the man to build the world she wants,
    rather than to create it herself."
    - Anais Nin (1903-1977)
     
    Jamie Duncan, Apr 10, 2004
    #20
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.