Thanks for the code Dale, but I am sorry, I still get incorrect layers sort index.
Below is the code I now use:
Rhino.RhinoDoc doc = Rhino.RhinoDoc.ActiveDoc;
doc.Views.RedrawEnabled = false;
doc.Views.RedrawEnabled = true;
doc.Views.Redraw();
List<KeyValuePair<int, int>> sortList = new List<KeyValuePair<int, int>>(doc.Layers.Count);
foreach (Rhino.DocObjects.Layer layer in doc.Layers)
{
if (!layer.IsDeleted)
sortList.Add(new KeyValuePair<int, int>(layer.LayerIndex, layer.SortIndex));
}
sortList.Sort(
delegate(KeyValuePair<int, int> firstPair,
KeyValuePair<int, int> nextPair)
{
return firstPair.Value.CompareTo(nextPair.Value);
}
);
RhinoApp.WriteLine("---------------- Layer order --------------------------------------------");
foreach (KeyValuePair<int, int> pair in sortList)
if (IsToolpathLayer(doc.Layers[pair.Key]))
RhinoApp.WriteLine("Toolpath Name = {0}, LayerIndex = {1}, SortIndex = {2}", doc.Layers[pair.Key].Name, pair.Key, pair.Value);
else
RhinoApp.WriteLine("Name = {0}, LayerIndex = {1}, SortIndex = {2}", doc.Layers[pair.Key].Name, pair.Key, pair.Value);
The ui layers looks like this when open the model:
…and the layer order i get.
---------------- Layer order --------------------------------------------
Name = 01 Block, LayerIndex = 0, SortIndex = 0
Name = 02 Surfaces, LayerIndex = 1, SortIndex = 1
Name = madCAM, LayerIndex = 2, SortIndex = 2
Name = Clipping Planes, LayerIndex = 3, SortIndex = 3
Name = Box-region, LayerIndex = 4, SortIndex = 4
Name = 36_T14_Ø50.8r1.6_finish_bullnose_alum, LayerIndex = 5, SortIndex = 5
Name = 40_T8_Ø10_finish_ballnose_alum, LayerIndex = 6, SortIndex = 6
Toolpath Name = Z_Levels_40_T8_Ø10_finish_ballnose_alum Group:3, LayerIndex = 7, SortIndex = 7
Toolpath Name = Roughing_36_T14_Ø50.8r1.6_finish_bullnose_alum Group:0, LayerIndex = 8, SortIndex = 8
Toolpath Name = Roughing_36_T14_Ø50.8r1.6_finish_bullnose_alum Group:1, LayerIndex = 9, SortIndex = 9
Toolpath Name = Planar_36_T14_Ø50.8r1.6_finish_bullnose_alum Group:0, LayerIndex = 10, SortIndex = 10
Layer number 7 should be the last toolpath, but it goes as the first toolpath. If I manually move layer number 5 down in the ui list and then up again, it seems like the layer list becomes correct and the sortindexes syncs with the ui as it should.
Below is what I get after manually moving a layer in the list.
---------------- Layer order --------------------------------------------
Name = 01 Block, LayerIndex = 0, SortIndex = 0
Name = 02 Surfaces, LayerIndex = 1, SortIndex = 1
Name = madCAM, LayerIndex = 2, SortIndex = 2
Name = Clipping Planes, LayerIndex = 3, SortIndex = 3
Name = Box-region, LayerIndex = 4, SortIndex = 4
Name = 36_T14_Ø50.8r1.6_finish_bullnose_alum, LayerIndex = 5, SortIndex = 5
Toolpath Name = Roughing_36_T14_Ø50.8r1.6_finish_bullnose_alum Group:0, LayerIndex = 8, SortIndex = 6
Toolpath Name = Roughing_36_T14_Ø50.8r1.6_finish_bullnose_alum Group:1, LayerIndex = 9, SortIndex = 7
Toolpath Name = Planar_36_T14_Ø50.8r1.6_finish_bullnose_alum Group:0, LayerIndex = 10, SortIndex = 8
Name = 40_T8_Ø10_finish_ballnose_alum, LayerIndex = 6, SortIndex = 9
Toolpath Name = Z_Levels_40_T8_Ø10_finish_ballnose_alum Group:3, LayerIndex = 7, SortIndex = 10
Thanks,
Joakim