Grasshopper

algorithmic modeling for Rhino

Hi everyone,

What i try to achieve is quite straightforward.
I want to compute the number of objects within a selected layer in Rhino.
The only way i found to do so is the following:

len(Rhino.RhinoDoc.ActiveDoc.Objects.FindByLayer("layerName"))

It is kind of stupid and not very elegant since it computes actually the objects themselves (which I don't need)

If my rhino file is very big, and for example, i want to find out what is the max number of objects contained in one of the layers of my layer table, I would have to do the following:

layers = Rhino.RhinoDoc.ActiveDoc.Layers
layerSizes = []
for l in layers:
    layerSizes.append(len(Rhino.RhinoDoc.ActiveDoc.Objects.FindByLayer(l)))
print max(layerSizes)

Which is very expensive...

Did I miss something in the Layer class? 
.ActiveCount only allows you to get the number of layers within the Rhino file.

Any help would be appreciated,

Thanks

Paul

Views: 1704

Replies to This Discussion

It'll probably be slightly faster to call ObjectCount, since you don't have to get the actual objects. Still pretty slow with a lot of objects though:


Also note the many ObjectEnumeratorSettings, they can be a bit confusing, certainly were for me :)

Attachments:

Thank you! Always here when it comes to python :)
It saves already quite some time indeed.
However if you open the smallest rhino file they have here at D2P, it will still take a few seconds to compute... I think I will post something on discourse then.

Hehe, I'd like to think it was more of a RhinoCommon thing, but yes ;)

Whoo, that sounds like a pretty terrifying file! Perhaps it makes sense to try out a compiled C# version first that calls the same method and see how that plays out. That said, I'm thinking that this might be inherently costly, since one will have to enumerate the objects at some point. It'll be interesting to hear what the Rhino devs say.

Thanks for your input Dimitrie!
I am not very familiar with VB and the Enumerable class but I think I got your approach, which i sort of replicated here (or maybe that's not even close from what you wrote).
Anyway it works quite well and it's much faster than the precedent versions!

@Anders, I realized that the result of your script differed a bit from the first version I posted because it dismissed the invisible/hidden objects, which I had to specify here with ObjectEnumeratorSettings.

Attachments:
Indeed, hence the "..note the many ObjectEnumeratorSettings, they can be a bit confusing.." bit ;)

@Dimitrie: oops I meant C#. Just tried it. works like a charm :)

Sorry for the late reply polpo, i don't get notifications on this one! But, yeah, it seems you got it. 

  • The Enumerable.Repeat just populates an empty array with 0s (and an array length of the number of layers in the doc). 
  • Then I'm iterating through all the objects in the active doc, and i'm incrementing the value in the array at their layerindex. Well, you got it. I guess. 
  • My explanation sounds like shit, but essentially i was avoiding the find by layer operation, usually find()ing stuff in arrays is expensive

try this, dunno if it's faster:

```cs

int[] layerCounts = Enumerable.Repeat(0, Rhino.RhinoDoc.ActiveDoc.Layers.Count - 1).ToArray();

foreach(var obj in Rhino.RhinoDoc.ActiveDoc.Objects)

layerCounts[obj.Attributes.LayerIndex]++;

A = layerCounts;

```

RSS

About

Translate

Search

Videos

  • Add Videos
  • View All

© 2024   Created by Scott Davidson.   Powered by

Badges  |  Report an Issue  |  Terms of Service