LayerTable max depth


(Paul Poinet) #1

Hi all,

What is the most elegant way to find the max depth within a LayerTable?
This is what I wrote so far:

Was wondering if I missed any RhinoCommon method that would be more efficient…

Best

Paul


(David Rutten) #2

At least you could build your max depth inside the loop.

...
int maxDepth = 0;
foreach (var layer in layers)
  maxDepth = Math.Max(maxDepth, LayerDepth(layer));
return maxDepth;
...

private int LayerDepth(Layer layer)
{
   string path = layer.FullPath;
   string reduced = path.Replace("::", string.Empty");
   return (path.Length - reduced.Length) / 2;
}

(Paul Poinet) #3

Nice one, thanks! Even though I am surprised that RhinoCommon does not contain a function to calculate a Layer’s depth.

Also, if I am not wrong, this line of code…

…should be like:
return (path.Length - reduced.Length) / 2 + 1


(David Rutten) #4

That depends on whether a top-level layer is considered to be at depth=0 or depth=1. Either is fine, there’s no official definition of depth.


(Paul Poinet) #5

yes, fair enough!