Jump to content

Recommended Posts

Posted

Hello.

 

I'm just wondering if it's possible to create a routine that can.. well, ok:

 

I want to select a selection set of blocks.

For each block in SS, find name in blocks collection.

if the block has the property "count" (meaning it's nested)

get all the names that are inside that block.

If one of THOSE blocks has the property "count"

get all the names that are inside THAT block...

 

etc etc etc...

 

Seems like it should be easy, but I can't get my mind around how to make it failproof. I also don't want to list more than one of the same named item. Basically I just want to know all the blocks that are inside each block.

 

I've written and re-written this thing several times, and am not sure the correct way to do this. It seems like a re-cursive type deal, but I can't figure it out. :(

 

Any help is appreciated. thanks.

Posted
Hello.

 

I'm just wondering if it's possible to create a routine that can.. well, ok:

 

I want to select a selection set of blocks.

For each block in SS, find name in blocks collection.

if the block has the property "count" (meaning it's nested)

get all the names that are inside that block.

If one of THOSE blocks has the property "count"

get all the names that are inside THAT block...

 

etc etc etc...

 

Seems like it should be easy, but I can't get my mind around how to make it failproof. I also don't want to list more than one of the same named item. Basically I just want to know all the blocks that are inside each block.

 

I've written and re-written this thing several times, and am not sure the correct way to do this. It seems like a re-cursive type deal, but I can't figure it out. :(

 

Any help is appreciated. thanks.

 

 

 

Please post the code so we can tell what you are doing wrong.

Posted
Basically I just want to know all the blocks that are inside each block.

 

Just want to mention, If you are trying to nest a block inside of a block and expect to get information from a nested block, Then you are wasting your time. Not sure if that is what you are doing. The problem you mention does not seem clear.

 

Can you elaborate on what exactly you are trying to do?

Or am I misinterpreting you?

 

Please explain in detail.

 

Thanks,

The Buzzard

Posted

I think this may work, but I reckon there are better routines out there:

 

(defun GetObj (blk / bObj lst)
 (if
   (and
     (setq lst (cons blk lst))
     (not
       (vl-catch-all-error-p
         (vl-catch-all-apply
           (function
             (lambda ( )
               (setq bObj
                 (vla-item
                   (vla-get-blocks
                     (vla-get-ActiveDocument
                       (vlax-get-acad-object))) blk))))))))
   (vlax-for Obj bObj
     (and (eq "AcDbBlockReference"
            (vla-get-ObjectName Obj))
          (setq lst (cons (vla-get-Name Obj) lst))
          (getObj (vla-get-Name Obj)))))
 lst)

(vl-load-com)
      

Posted
I think this may work, but I reckon there are better routines out there:

 

(defun GetObj (blk / bObj lst)
 (if
   (and
     (setq lst (cons blk lst))
     (not
       (vl-catch-all-error-p
         (vl-catch-all-apply
           (function
             (lambda ( )
               (setq bObj
                 (vla-item
                   (vla-get-blocks
                     (vla-get-ActiveDocument
                       (vlax-get-acad-object))) blk))))))))
   (vlax-for Obj bObj
     (and (eq "AcDbBlockReference"
            (vla-get-ObjectName Obj))
          (setq lst (cons (vla-get-Name Obj) lst))
          (getObj (vla-get-Name Obj)))))
 lst)

(vl-load-com)

 

Lee,

 

Just curious, What does that code do?:unsure:

Posted
Lee,

 

Just curious, What does that code do?:unsure:

 

 

Is this code to pull up dxf information of a dynamic block? :?

Posted
Lee,

 

Just curious, What does that code do?:unsure:

 

Hi Buzzard,

 

The code needs to be provided with a block name, and if it can find that particular block in the block collection, it will iterate through the Block Object Collection and search for any Block References that make up the block (i.e, nested blocks), if it finds one, then it will get its name and recursively operate on that new name, whilst constructing a list of all the names found. Not sure if it will work or not, just a "theoretical" post.

 

Just gotta watch that the stack limit isn't reached when using recursive loops. I tend to only use them for small-ish tasks, as the stack limit is not too high and can sometimes be reached quite easily.

 

Some great info on recursive programming here.

 

Lee

Posted
Hi Buzzard,

 

The code needs to be provided with a block name, and if it can find that particular block in the block collection, it will iterate through the Block Object Collection and search for any Block References that make up the block (i.e, nested blocks), if it finds one, then it will get its name and recursively operate on that new name, whilst constructing a list of all the names found. Not sure if it will work or not, just a "theoretical" post.

 

Just gotta watch that the stack limit isn't reached when using recursive loops. I tend to only use them for small-ish tasks, as the stack limit is not too high and can sometimes be reached quite easily.

 

Some great info on recursive programming here.

 

Lee

 

That site is way over my head. But that is interesting if it can be done. Not sure where the value is in nesting. Maybe I learn something new here.

Posted

I think I understand the request:

 

[b][color=BLACK]([/color][/b]defun c:nbc [b][color=FUCHSIA]([/color][/b]/ tdef fe fd bn nl[b][color=FUCHSIA])[/color][/b]
 [b][color=FUCHSIA]([/color][/b]while [b][color=NAVY]([/color][/b]setq tdef [b][color=MAROON]([/color][/b]tblnext [color=#2f4f4f]"BLOCK"[/color] [b][color=GREEN]([/color][/b]not tdef[b][color=GREEN])[/color][/b][b][color=MAROON])[/color][/b][b][color=NAVY])[/color][/b]
        [b][color=NAVY]([/color][/b]setq fe [b][color=MAROON]([/color][/b]cdr [b][color=GREEN]([/color][/b]assoc -2 tdef[b][color=GREEN])[/color][/b][b][color=MAROON])[/color][/b][b][color=NAVY])[/color][/b]
        [b][color=NAVY]([/color][/b]princ [b][color=MAROON]([/color][/b]strcat [color=#2f4f4f]"\n"[/color] [b][color=GREEN]([/color][/b]cdr [b][color=BLUE]([/color][/b]assoc 2 tdef[b][color=BLUE])[/color][/b][b][color=GREEN])[/color][/b][b][color=MAROON])[/color][/b][b][color=NAVY])[/color][/b]
        [b][color=NAVY]([/color][/b]while fe
           [b][color=MAROON]([/color][/b]setq fd [b][color=GREEN]([/color][/b]entget fe[b][color=GREEN])[/color][/b]
                 fe [b][color=GREEN]([/color][/b]entnext fe[b][color=GREEN])[/color][/b][b][color=MAROON])[/color][/b]
           [b][color=MAROON]([/color][/b]and [b][color=GREEN]([/color][/b]= [color=#2f4f4f]"INSERT"[/color] [b][color=BLUE]([/color][/b]cdr [b][color=RED]([/color][/b]assoc 0 fd[b][color=RED])[/color][/b][b][color=BLUE])[/color][/b][b][color=GREEN])[/color][/b]
                [b][color=GREEN]([/color][/b]setq bn [b][color=BLUE]([/color][/b]cdr [b][color=RED]([/color][/b]assoc 2 fd[b][color=RED])[/color][/b][b][color=BLUE])[/color][/b][b][color=GREEN])[/color][/b]
                [b][color=GREEN]([/color][/b]if [b][color=BLUE]([/color][/b]not [b][color=RED]([/color][/b]assoc bn nl[b][color=RED])[/color][/b][b][color=BLUE])[/color][/b]
                    [b][color=BLUE]([/color][/b]setq nl [b][color=RED]([/color][/b]cons [b][color=PURPLE]([/color][/b]cons bn 1[b][color=PURPLE])[/color][/b] nl[b][color=RED])[/color][/b][b][color=BLUE])[/color][/b]
                    [b][color=BLUE]([/color][/b]setq nl [b][color=RED]([/color][/b]subst [b][color=PURPLE]([/color][/b]cons bn [b][color=TEAL]([/color][/b]1+ [b][color=OLIVE]([/color][/b]cdr [b][color=GRAY]([/color][/b]assoc bn nl[b][color=GRAY])[/color][/b][b][color=OLIVE])[/color][/b][b][color=TEAL])[/color][/b][b][color=PURPLE])[/color][/b]
                                    [b][color=PURPLE]([/color][/b]assoc bn nl[b][color=PURPLE])[/color][/b] nl[b][color=RED])[/color][/b][b][color=BLUE])[/color][/b][b][color=GREEN])[/color][/b][b][color=MAROON])[/color][/b][b][color=NAVY])[/color][/b][b][color=FUCHSIA])[/color][/b]

  [b][color=FUCHSIA]([/color][/b]textpage[b][color=FUCHSIA])[/color][/b]
  [b][color=FUCHSIA]([/color][/b]prin1 nl[b][color=FUCHSIA])[/color][/b]
  [b][color=FUCHSIA]([/color][/b]foreach b nl
    [b][color=NAVY]([/color][/b]princ [b][color=MAROON]([/color][/b]strcat [color=#2f4f4f]"\nBlock "[/color] [b][color=GREEN]([/color][/b]car b[b][color=GREEN])[/color][/b]
                   [color=#2f4f4f]" Is Nested In "[/color] [b][color=GREEN]([/color][/b]rtos [b][color=BLUE]([/color][/b]cdr b[b][color=BLUE])[/color][/b] 2 0[b][color=GREEN])[/color][/b] [color=#2f4f4f]" Block[b][color=GREEN]([/color][/b]s[b][color=GREEN])[/color][/b]"[/color][b][color=MAROON])[/color][/b][b][color=NAVY])[/color][/b][b][color=FUCHSIA])[/color][/b]
  [b][color=FUCHSIA]([/color][/b]prin1[b][color=FUCHSIA])[/color][/b][b][color=BLACK])[/color][/b]

nl contains an associative list of dotted pair atoms

( block_name . number_of_nested_direct_instances_found )

 

This does not count the nested depth or total number of nested occurrences in the drawing. -David

Posted
Just want to mention, If you are trying to nest a block inside of a block and expect to get information from a nested block, Then you are wasting your time. Not sure if that is what you are doing. The problem you mention does not seem clear.

 

Can you elaborate on what exactly you are trying to do?

Or am I misinterpreting you?

 

Please explain in detail.

 

Thanks,

The Buzzard

 

You can get the information from a nested block by using the blocks collection, which contains all blocks in the drawing, and also has information on whether blocks are 'inside' that block.

 

Suppose I have a block for a part. Inside that part there are several more blocks. Some of the blocks inside those might have a block or two. So I want a routine that can, when I select that main part block, it get the name.. then finds the name in the block collection, and goes through the block collection for each name that resides somewhere inside that main block.

 

It can be done, it's just not pretty.. especially if you don't know how many blocks are nested, and you want to get all of them no matter what.

Posted
I think this may work, but I reckon there are better routines out there:

 

(defun GetObj (blk / bObj lst)
 (if
   (and
     (setq lst (cons blk lst))
     (not
       (vl-catch-all-error-p
         (vl-catch-all-apply
           (function
             (lambda ( )
               (setq bObj
                 (vla-item
                   (vla-get-blocks
                     (vla-get-ActiveDocument
                       (vlax-get-acad-object))) blk))))))))
   (vlax-for Obj bObj
     (and (eq "AcDbBlockReference"
            (vla-get-ObjectName Obj))
          (setq lst (cons (vla-get-Name Obj) lst))
          (getObj (vla-get-Name Obj)))))
 lst)

(vl-load-com)

 

 

Ok, I see there are two versions so I will try both of them. Thank you much!

Posted
You can get the information from a nested block by using the blocks collection, which contains all blocks in the drawing, and also has information on whether blocks are 'inside' that block.

 

Suppose I have a block for a part. Inside that part there are several more blocks. Some of the blocks inside those might have a block or two. So I want a routine that can, when I select that main part block, it get the name.. then finds the name in the block collection, and goes through the block collection for each name that resides somewhere inside that main block.

 

It can be done, it's just not pretty.. especially if you don't know how many blocks are nested, and you want to get all of them no matter what.

 

Ok I go with that, But what are the advantages to nesting blocks in the first place? I am not being critical, Just curious.

Posted
Please post the code so we can tell what you are doing wrong.

 

Ok, I think this is the working version I have for it, but it only goes 2 levels deep I think, so I have to either get the recursion to work, of have a function for each nested level.. ??

 

This code is not optimized or anything, I'm just trying to get it to work correctly first. :)

 

(defun test (/ object doc blocks)

(setq doc (vla-get-activedocument (vlax-get-acad-object))
      blocks (vla-get-blocks doc))

;get ss of blocks
;for each block, if count > 1 go through all and get names otherwise just get it's name. Append all to namelist
(setq ss (ssget (list (cons 0 "insert")(cons 410 (getvar "ctab"))))
      cntr 0)
(while (< cntr (sslength ss))
 (setq entName (ssname ss cntr)
       cntr (+ 1 cntr)
       object (vlax-ename->vla-object entname))
 (cond
  ((and
   (vlax-property-available-p object "name")
   (vlax-property-available-p object "count")
   (/= (wcmatch (strcase (vla-get-name object)) "*SPACE*") T))(foreach item object
                                                               (if
                                                                (vlax-property-available-p item "name")(setq nameList (append (list (vla-get-name item)) nameList)))))
  ((and
    (vlax-property-available-p object "name")
    (/= (wcmatch (strcase (vla-get-name object)) "*SPACE*") T))(setq nameList (append (list (vla-get-name object)) nameList)))
  )
 )
;for each name in the nameList, and
;find the name in the blocks collection.
;if the block with that name has the property "count" go to get multiple blocks
;otherwise don't do anything. (the name is already in the list)

(foreach name nameList ;for each name in the name list
(vlax-for block blocks ;for each block in the blocks collection..
(if
 (wcmatch (vla-get-name block) (strcat "*"name"*")) ;if the name is the same as current item in namelist...
 (if
  (vlax-property-available-p block "count") ;if the property "count" is available.. 
  (setq nameList (getNestedBlocks block nameList)) ;send block to getNestedBlocks routine to collect names
  )
 (if ;otherwise.. if..
  (and
   (vlax-property-available-p block "name") ;..the name property is valid..
   (wcmatch (vla-get-name block) (strcat "*"name"*")) ;..the name is the same as current item in nameList..
   (/= (wcmatch (strcase (vla-get-name object)) "*SPACE*") T)) ;..it's not one of the paper or model space blocks.
  (setq nameList (append (list (vla-get-name block)) nameList)) ;add it to the list..
  )
 )
 )
)
)

 (defun getNestedBlocks (block nameList)
  ;go through each item in sent block 
  (vlax-for item block
   (if
    (and
     (vlax-property-available-p item "name");if there is a name..
     (not (member (vla-get-name item) nameList)))
    (setq nameList (append (list (vla-get-name item)) nameList));add it to nameList
    (if (vlax-property-available-p item "count")
     (getNestedBlocks item nameList) 
     )
    )
   )

  )

Posted
Ok I go with that, But what are the advantages to nesting blocks in the first place? I am not being critical, Just curious.

 

I agree. I hate nested blocks. Unfortunatly other people like to use them. It actually is nice for BOM work, so that each item has it's own part number (this is for manufacturing furniture). And this code I'm working on is for making a BOM table.

 

thanks.

Posted
I agree. I hate nested blocks. Unfortunatly other people like to use them. It actually is nice for BOM work, so that each item has it's own part number (this is for manufacturing furniture). And this code I'm working on is for making a BOM table.

 

thanks.

 

Ok, I see now where that might be useful for assembly work.

A main assembly with sub-assemblies all in one tight package.

Very interesting.

Posted
Ok, I see now where that might be useful for assembly work.

A main assembly with sub-assemblies all in one tight package.

Very interesting.

 

Yep, thats about it. As much as nested blocks are a pain in the ***, their useful once in a while. :)

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...