How can I retrieve the count of InstanceObject for each InstanceDefinition with C#?

Struggling RhinoCommon newbie here.
How can I retrieve the count of InstanceObject for each InstanceDefinition with C# ?

I’m trying to use the TextFields.BlockInstanceCount Method .
I’ve got no errors, but I’ve got no juice either…

Sorry for the wrong pointer I gave earlier to these Textfields, I don’t believe Rhino.Runtime is going to be what you need.

It had a nice sound to it though… “BlockInstanceCount”…
:slight_smile:

1 Like

blocks are highly confusing. For example if you look at my organize blocks script:

you see that to get the name of a block instance, I need to retrieve the block instances in the document, then get its name through block.InstanceDefinition.Name

in python it’s easy to retrieve the blockinstance count through rhinoscriptsyntax.BlockIinstanceCount(name)

by using debugging, and ‘step into’ you can look into the code and see that this code does the following:

def BlockInstanceCount(block_name,where_to_look=0):    
    idef = scriptcontext.doc.InstanceDefinitions.Find(block_name)
        if not idef: raise ValueError("%s does not exist in InstanceDefinitionsTable"%block_name)
        refs = idef.GetReferences(where_to_look)
        return len(refs)
2 Likes

Hmmm… this way of organizing blocks is quite interesting !
But regarding the instance count, let’s see if I follow you here : you are using “rhinoscriptsyntax”, so basically, you are short-circuiting RhinoCommon, right ?

I never used debugging (you are doing that in Visual Studio I suppose).
What scares me with the “GetReferences” part is that it’s going to store those references in memory.

I’m trying to avoid this at all cost because it’s like exploding all my blocks, thus negating the whole interest of having blocks.

I’m looking into the InstanceDefinition.InUse Method because it allows me to filter out Definitions that I don’t need to take into account as they are not referenced.
It’s a first step.

Well… @TomTom would say I’m just pointing fingers and bitching because I can’t figure stuff out by myslelf, but blocks… man…the horror

I don’t think GetReferences is going to add any weight to your script, it just looks for the references of a block definition that are used in the document.

For example this simple python script:

import Rhino
import scriptcontext as sc

block= sc.doc.InstanceDefinitions.Find("block")

amount = len( Rhino.DocObjects.InstanceDefinition.GetReferences(block,0))
print amount

will give me the count of blocks named ‘block’

I’m not too familiar with c# and can’t seem to find what the equivalent is of ‘scriptcontext’ but to me it looks if you can solve that, you basically have what you need to get the count of blocks in use.

no rhinoscriptsyntax is what rhinoscript did before we had python: it’s a wrap around RhinoCommon : without needing to know RhinoCommon you can still use it.

I’m using the python script editor. Debugging in this case is just a matter of adding a break point (in this case to the line where I put rhinoscriptsyntax.BlockInstanceCount()
then at the command line you can choose ‘step into’ which will load blocks.py and then you can see how the command uses RhinoCommon. It’s in fact a good way to learn RhinoCommon this way first (using python with rhinoscriptsyntax and ‘step into’)

1 Like

Yes, “Wrap around” ; that’s what I was trying to say.

Well, I’m put off by the absence of documentation.
But when I see how compact the Python scripts are compared to the C# ones… I wonder if I’d better not do like you.

Well, thanks to your help, I think I figured out the proper syntax in C# :

I’m kind of suspicious though because I find that 273ms is quite a long execution time for such a small number of blocks…

it’s probably just the initialization time of the script, I bet it won’t get much more than that when the amount of blocks increase

Well, with 748 block definitions, I’m already hitting almost 4 seconds…
Now when you think of it, nowhere in Rhino can you have this info all at once.
One needs to use the Block manager, select the block name and the the “Count” button, like your average caveman.

hmm, that’s indeed a lot… I’m going to see if I get the same behavior out of grasshopper.
btw: I was wondering where the namespace RhinoDocument comes from. How did you find that?
That seems the equivalent of scriptcontext.doc in python

By contrast, retrieving all the GUIs of the objects contained inthe Block definitions only takes 19ms !

try removing the ‘if (def==null) return’ part

looks like that’s causing the delay, at least it does here

Well, it’s slightly better…

weird:

Not as strange as this :

RhinoDocument

Trying to answer to :

so how did you find it?

I didn’t find it ; someone made this component for me :flushed:

It might have been Gan Keyu…

I now see where it comes from, click the ‘Members’ section in the script editor. So it is not a namespace at all

Did I accidentally help you figure out something useful ?
:grinning:

In my 4 hours “C# for beginners” video, they never mentionned “Members”…

well, as mentioned, I’m not very familiar with C#, I haven’t used the C# scripting component much before apart from some simple modifications to posted scripts.

the only experience with c# I have is when I wanted to make my own cross platform plugin to scale an object to a certain volume. It was a simple project but good one to learn using Visual Studio a bit.