Visual LISP, AutoLISP and General Customization
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Report number of 'nested blocks'

7 REPLIES 7
SOLVED
Reply
Message 1 of 8
john.laidler
961 Views, 7 Replies

Report number of 'nested blocks'

Anyone know how to count the number of 'nested blocks', then report back the number?

John Laidler
AutoCAD, Inventor and Vault



Please use "Accept as Solution" & give "Kudos" if this response helped you.
7 REPLIES 7
Message 2 of 8
Kent1Cooper
in reply to: john.laidler

More detail, please....  How many in Block definitions, or how many total in all Insertions of Blocks that contain nested other Blocks?

 

Say I have Block A that contains, nested within its definition, Block B.  And I have Block C that contains, nested within it, Blocks D and E.  And I have Block A Inserted twice in the drawing, and Block C Inserted four times.

 

Would you want the report to say there are three nested Blocks?  They would be B, D and E -- B is nested in the definition of A, and D and E are nested in the definition of C.

 

Or would you want it to say there are ten of them?  They would be the two B's nested in the Insertions of A, and the four each of D and E nested in the Insertions of C.

 

Also:  Should the report say how many different Blocks are nested [somewhere, anywhere, any number of times], or how many separate nestings of them there are?  What if A contains two nested B Blocks?  Or if both A and C each contain B?  In either case, should both B's be considered separate nestings in the count, or should the fact that B is nested anywhere count as only one nested Block, no matter how many other Blocks it's nested in, or how many times it's nested in some individual other Block?

 

 

Kent Cooper, AIA
Message 3 of 8
john.laidler
in reply to: Kent1Cooper

Basically, I just need a 'count' of how many blocks have 'nested blocks'.

 

John Laidler
AutoCAD, Inventor and Vault



Please use "Accept as Solution" & give "Kudos" if this response helped you.
Message 4 of 8
ВeekeeCZ
in reply to: john.laidler

Just quick tip on Lee Mac's routine HERE if you didn't try yet.

Message 5 of 8
john.laidler
in reply to: ВeekeeCZ

Thanks, I did already have that.  What my user needs is just a total 'count' of the nested blocks.

He doesn't need the names listed.  This is where i'm having the trouble.

 

John Laidler
AutoCAD, Inventor and Vault



Please use "Accept as Solution" & give "Kudos" if this response helped you.
Message 6 of 8
Kent1Cooper
in reply to: john.laidler


@jclaidler wrote:

Basically, I just need a 'count' of how many blocks have 'nested blocks'.

---[and]---

....  What my user needs is just a total 'count' of the nested blocks. ....


Those descriptions seem like different things to me.  For the first one [while it seems curious to me that such a number would be of any use without also determining which Blocks they are, because presumably you want to do something with the information], you could take this thing I did a few years ago:

 

;;  BlockNestList.lsp [command name: BNL]
;;  Returns a list of lists of all Block and Xref names and all other Blocks/
;;    Xrefs/Minserts nested within each.  [Will also list Windows Metafiles
;;    as if nested Blocks, because they are also "Insert" objects, but filters out
;;    old-style Hatch pattern and Dimension "blocks."]
;;  Each sub-list consists of the parent Block name followed by any Block(s)
;;    nested in its definition.  Does not contain names of Blocks nested more
;;    than one level down -- see the sub-list for each nested Block's name.
;;  Does not include Blocks containing no nested Blocks in overall list [see
;;    note below to have them included].
;;  Kent Cooper, May 2011

(defun C:BNL (/ blk blknamelist blklist blkitem)
  (setq blknestlist nil)
    ; clear list if used more than once; not localized in case User
    ; wants to make use of overall list after routine is finished
  (while ; list of all names in Block table
    (setq blk (cdadr (tblnext "block" (not blk))))
    (if (not (wcmatch blk "`*D*,`*X*"))
        ; not Dimension or old-style Hatch pattern "block" -- use
        ; string "`*D*,`*X*,`*U*" to also ignore anonymous Blocks
      (setq blknamelist (cons blk blknamelist)); add name to list
    ); end if
  ); end while
  (foreach blk blknamelist
    (setq
      blklist nil blklist (list blk); start sub-list over for subject Block name
      blkitem (tblobjname "block" blk); parent Block entity name
    ); end setq
    (while (setq blkitem (entnext blkitem)); looks at Block's contents; nil at end
      (if
        (and
          (= (cdr (assoc 0 (entget blkitem))) "INSERT")
          (not (wcmatch (cdr (assoc 2 (entget blkitem))) "`*D*,`*X*"))
            ; not Dimension or old-style Hatch pattern "block" -- use
            ; string "`*D*,`*X*,`*U*" to also ignore anonymous Blocks
          (not (member (cdr (assoc 2 (entget blkitem))) blklist)); not already in sub-list
        ); end and
        (setq blklist (append blklist (list (cdr (assoc 2 (entget blkitem))))))
          ;; add any Insert object's name to subject Blocks' sub-list
      ); end if
    ); end while -- contents of current subject block
    (if (> (length blklist) 1)
      ; ignore sub-lists of only one item [Blocks w/o nested Insert objects].  To
      ; include them, remove or comment out   (if...   line above and   ); end if
      ; line below.  Such Block names will appear alone in one-item sub-lists.
      (setq blknestlist (cons blklist blknestlist)); add sub-list to overall list
    ); end if
  ); end foreach
  blknestlist
); end defun
(prompt "\nType BNL to make a Block Nesting List.")

and just replace this line just before the end:

 

  blknestlist

 

with this instead:

 

  (length blknestlist)

 

[and change the prompt at the bottom].  That will give you just a raw integer -- the number of Block definitions which contain any Block(s) nested within them.  [And if that's really all you need, of course it could be simplified considerably.  But without simplification, it does provide the availability of 'blknestlist' so if you need to, you can check out which Blocks have nested Blocks in them, and what those nested Blocks are.]

 

For the second description, "a total 'count' of the nested blocks" raises some of the same questions as in my first reply, and maybe some other similar ones.

Kent Cooper, AIA
Message 7 of 8
john.laidler
in reply to: Kent1Cooper

That works great!!  I like the idea of listing the block name, but your LISP lists both the block and nested blocks.

Can this be changed to just list the block name only, that contains the nested blocks?

John Laidler
AutoCAD, Inventor and Vault



Please use "Accept as Solution" & give "Kudos" if this response helped you.
Message 8 of 8
Kent1Cooper
in reply to: john.laidler


@jclaidler wrote:

That works great!!  I like the idea of listing the block name, but your LISP lists both the block and nested blocks.

Can this be changed to just list the block name only, that contains the nested blocks?


Replace this line:

 

  blknestlist

 

with this:

 

  (mapcar 'car blknestlist)

Kent Cooper, AIA

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report

”Boost