Site Tools

Exporting Block Counts to Excel

Developer: RhinoScript
Summary: Demonstrates how to count blocks and then export the results to Microsoft Excel.


I have a question about blocks. Is it possible to count all blocks in a drawing and export these results to Microsoft Excel (or another program)?


With RhinoScript, the answer is yes.


The following example script demonstrates how to count all the block instances and then export the results to Microsoft Excel. If you want the results somewhere else, perhaps a text file, this is possible too.

To use:

  1. Unzip the script file (.RVB) and save it to some folder.
  2. Drag the script file and drop it on top of a running copy of Rhino.
  3. From the Rhino command line, run ExportBlockCount.

Here is the script code:

' ExportBlockCount.rvb -- May 2010
' If this code works, it was written by Dale Fugier.
' If not, I don't know who wrote it.
' Works with Rhino 4.0.
Option Explicit
Sub ExportBlockCount()
  Dim arrBlocks, strBlock, strName
  Dim objCounts, objKey
  Dim objExcel, objBook, objSheet, nCell
  ' Get all of the block instances in the document
  arrBlocks = Rhino.ObjectsByType(4096)
  If IsNull(arrBlocks) Then
    Call Rhino.Print("No blocks to export.")
    Exit Sub
  End If
  ' Create a dictionary object for counting blocks
  Set objCounts = CreateObject("Scripting.Dictionary")
  ' Count the blocks
  For Each strBlock In arrBlocks
    strName = Rhino.BlockInstanceName(strBlock)
    If objCounts.Exists(strName) Then
      objCounts(strName) = objCounts(strName) + 1
      Call objCounts.Add(strName, 1)
    End If
  ' Create a Excel object
  Set objExcel = CreateObject("Excel.Application")
  objExcel.Visible = True
  ' Initialize Excel
  Set objBook = objExcel.Workbooks.Add
  Set objSheet = objBook.Worksheets(1)
  ' Place titles on sheet
  nCell = 1
  objExcel.Cells(nCell, 1).Value = "Block Name"
  objExcel.Cells(nCell, 2).Value = "Count"
  nCell = nCell + 1
  ' Write the blocks and counts to the sheet
  Call Rhino.Print("Block counts:")
  For Each objKey In objCounts
    objExcel.Cells(nCell, 1).Value = CStr(objKey)
    objExcel.Cells(nCell, 2).Value = CStr(objCounts(objKey))
    ' Print to command line too
    Call Rhino.Print("  " & CStr(objKey) & " = " & CStr(objCounts(objKey)))
   nCell = nCell + 1
End Sub
' Drag & drop and alias creation stuff
Rhino.AddStartUpScript Rhino.LastLoadedScriptFile
Rhino.AddAlias "ExportBlockCount", "_-RunScript (ExportBlockCount)"

developer/scriptsamples/exportblockcount.txt · Last modified: 2020/08/14 (external edit)