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

nested block finding

2 REPLIES 2
Reply
Message 1 of 3
sudarsann
1855 Views, 2 Replies

nested block finding

Hi,

 

I have blocks in a drawing, some of them are nested blocks and remaining are normal blocks.

How can i find the nested blocks using lisp.

 

2 REPLIES 2
Message 2 of 3
p_mcknight
in reply to: sudarsann

To do what with?  You can go through all blocks in the blocks collection and make a list of them (I have a similar program that makes a list of all blocks and their children or all blocks and the parents, or all blocks, and exports the list to excel).  Or you can ask the operator to selct the nested block and get the information on the object via nentsel and then do something with the actual objects.  What you do to get the objects will depend greatly on what you want to do with the objects (blocks) once they are gotten.

Message 3 of 3
Kent1Cooper
in reply to: sudarsann


@sudarsann wrote:

Hi,

 

I have blocks in a drawing, some of them are nested blocks and remaining are normal blocks.

How can i find the nested blocks using lisp.

 


I agree that it depends on what you want to do with the information, but to report on all Block definitions with nested Blocks within them, I did this 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.")

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