Insert Every Block Instance on Document?

Hi people,

I am currently needing to insert every block definition within a document.

I found this: Which replaces every point with the same block. I tried to change it to instead of inserting the same block, go cycling along each block definition, but no luck so far.

' Replaces points with blocks
Sub ReplacePointsWithBlocks

  ' Select points to replace with a block
  Dim arrObjects
  arrObjects = Rhino.GetObjects("Select points to replace with a block", 1, True, True)
  If Not IsArray(arrObjects) Then Exit Sub

  ' Get the names of all block definitions in the document    
  Dim arrBlocks
  arrBlocks = Rhino.BlockNames(True)
  If Not IsArray(arrBlocks) Then
    Rhino.Print "No block definitions found in the document."
    Exit Sub
  End If

  ' Select a block name from a list
  Dim strBlock
  strBlock = Rhino.ListBox(arrBlocks, "Select block", "Replace Points")
  If IsNull(strBlock) Then Exit Sub

  ' Turn off redrawing (faster)
  Rhino.EnableRedraw True      

  ' Process each selected point object
  Dim strObject, arrPoint
  For Each strObject In arrObjects
    ' Get the point object's coordinates
    arrPoint = Rhino.PointCoordinates(strObject)
    ' Insert the block at that location
    Rhino.InsertBlock strBlock, arrPoint
  Next

  ' Delete all of the point objects
  Rhino.DeleteObjects arrObjects   

  ' Turn redrawing back on     
  Rhino.EnableRedraw True      

End Sub

I’m not sure whether this is what you want or not:

import rhinoscriptsyntax as rs
points = rs.GetObjects("Select points", rs.filter.point)
names = rs.BlockNames(True)
for i in range(len(points)):
    name = names[i % len(names)]
    rs.InsertBlock(name, points[i], (1,1,1), 0, (0,0,1))

ShynnSup.py (239 Bytes)

Yup, pretty much! That is indeed way simpler than what I thought. Thank you!

Mahdiyar, I am afraid I might have to go one step further. I am trying to move each block definition to the same layer, for this I need to insert each block, double click to edit it, select it, and move it to said layer. Pretty tedious.

Do you happen to know a way to edit multiple blocks at once, or at least, change the original layer the block was created for all of them at the same time?

Thank you in advance.

Is that what you want:
Caution: Save your file before trying this

import rhinoscriptsyntax as rs
names = rs.BlockNames(True)
for name in names:
    block = rs.InsertBlock(name, [0,0,0], (1,1,1), 0, (0,0,1))
    objectsInBlock = rs.ExplodeBlockInstance(block, True)
    for object in objectsInBlock:
        rs.ObjectLayer(object, "Default")
    rs.DeleteBlock(name)
    rs.AddBlock(objectsInBlock, [0,0,0], name, True)

ShynnSup.py (360 Bytes)

I think this is it! Thank you!