Yarik,
Be aware that your code iterates through the SelectionSet twice (the first time doesn't do anything), furthermore, it iterates through each block definition for every insert in the drawing (when this only needs to happen once), and will iterate through the block collection for every insert (you could use vla-item to get the block definition).
The following code is what my previous post was hinting at, also incorporating Alan's suggestion to exclude XRefs:
([color=BLUE]defun[/color] c:test ( [color=BLUE]/[/color] blockcollection blockdefinition blockname i processedblocks selectionset ) ([color=BLUE]vl-load-com[/color])
([color=BLUE]setq[/color] BlockCollection
([color=BLUE]vla-get-blocks[/color]
([color=BLUE]vla-get-activedocument[/color] ([color=BLUE]vlax-get-acad-object[/color]))
)
)
([color=BLUE]if[/color] ([color=BLUE]setq[/color] SelectionSet ([color=BLUE]ssget[/color] '((0 . [color=MAROON]"INSERT"[/color]))))
([color=BLUE]repeat[/color] ([color=BLUE]setq[/color] i ([color=BLUE]sslength[/color] SelectionSet))
([color=BLUE]setq[/color] BlockName ([color=BLUE]cdr[/color] ([color=BLUE]assoc[/color] 2 ([color=BLUE]entget[/color] ([color=BLUE]ssname[/color] SelectionSet ([color=BLUE]setq[/color] i ([color=BLUE]1-[/color] i)))))))
([color=BLUE]if[/color] ([color=BLUE]not[/color] ([color=BLUE]member[/color] BlockName ProcessedBlocks))
([color=BLUE]progn[/color]
([color=BLUE]setq[/color] BlockDefinition ([color=BLUE]vla-item[/color] BlockCollection BlockName))
([color=BLUE]if[/color] ([color=BLUE]eq[/color] [color=BLUE]:vlax-false[/color] ([color=BLUE]vla-get-isxref[/color] BlockDefinition))
([color=BLUE]vlax-for[/color] object BlockDefinition
([color=BLUE]vla-put-color[/color] object [color=BLUE]acbylayer[/color])
)
)
([color=BLUE]setq[/color] ProcessedBlocks ([color=BLUE]cons[/color] BlockName ProcessedBlocks))
)
)
)
)
([color=BLUE]princ[/color])
)
That's really great Lee .
The way you named the variables is very useful for me to understand the process of the routine .
Great thanks .