Script to count block and all blocks used by parent blocks

Hi, using Rhino Microsoft V5. French version

I’m trying to count Block Instances and all blocks inside the differents blocks from a file.

I found a script https://wiki.mcneel.com/developer/scriptsamples/countblocks :

Sub CountAllInstancesOfAllBlocks
   arrNames = Rhino.BlockNames(True)
   If IsArray(arrNames) Then
     For Each strName In arrNames
       Rhino.Print strName & " = " & CStr(Rhino.BlockInstanceCount(strName))
     Next
   End If
 End Sub

I add a _ in front of all command to indiquate that it’s an english command :

_Sub _CountAllInstancesOfAllBlocks
  _arrNames = _Rhino.BlockNames(True)
  _If _IsArray(arrNames) _Then
    _For _Each _strName _In _arrNames
      _Rhino.Print _strName & " = " & _CStr(Rhino.BlockInstanceCount(strName))
    _Next
  _End _If
_End _Sub

But it doesn’t work, message unknow command or commande inconnue in french.

I’m a bit new in script and I really need to make it work.

Thank you for your help.

Hi @francois1, the underscore in front of a command only works for Rhino commands not for script code. Eg. you can make your french Rhino accept the english command name _Join or “_Explode”.

_
c.

Yes your script should work in the first version without underscore.

Thank you, it work !

next step, I would like to export blocks like this script :

'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' 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
    Else
      Call objCounts.Add(strName, 1)
    End If
  Next
 
  ' 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
  Next
 
End Sub
 
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Drag & drop and alias creation stuff
Rhino.AddStartUpScript Rhino.LastLoadedScriptFile
Rhino.AddAlias "ExportBlockCount", "_-RunScript (ExportBlockCount)" 

https://wiki.mcneel.com/developer/scriptsamples/exportblockcount

Unfortunatly, we don’t use Excell. And I would like to export as a .CSV or in openoffice sheet.

How can I modify this script ?

1 Like