Sort Curves by Length using GH_Structure

Hi everyone. I am trying to sort each Brach of a tree (in this case the tree is a GH_Structure) I have an example to do this for a DataTree structure Sort a BrepCurve by length this implemetation works great when you work on C# Scrip component and when you use DataTree.

In My case I am trying to perform the same with GH_Structure.

    //Duplicate curvesTree
    GH_Structure<IGH_Goo> sortedCurvesTree = new GH_Structure<IGH_Goo>();
    sortedCurvesTree = curvesTree.Duplicate();

    for (int i = 0; i < sortedCurvesTree.PathCount; i++)
      GH_Path pathTree = new GH_Path(0, i);
      sortedCurvesTree[pathTree].Sort((a,b) =>
        return b.GetLength().CompareTo(a.GetLength());

I run in a couple of issues:

  1. Error (CS1660): Cannot convert lambda expression to type ‘System.Collections.Generic.IComparer<Grasshopper.Kernel.Types.IGH_Goo>’ because it is not a delegate type (line 79)
  1. Error (CS1061): ‘Grasshopper.Kernel.Types.IGH_Goo’ does not contain a definition for ‘GetLength’ and no extension method ‘GetLength’ accepting a first argument of type ‘Grasshopper.Kernel.Types.IGH_Goo’ could be found (are you missing a using directive or an assembly reference?) (line 81)

I believe this is because in the GH_Structure implementation the type elements inside each branch are:


and each element inside the list is:


On a previous post Convert GH_Curve to Curve I found that you can access to the curve stored inside the ghcurve. In my case I am struggling to access to the Value parameter. So far in my case I did not have any luck accessing to it.

I tried trying to access to element using curvesTree.get_DataItem(pathTree, 0) but Value is not there as well.

My question is it possible to replicate the sorting technique used in DataTree example for the GH_Structure implementation or should I look for other techniques and approaches?

Every recommendation will be greatly appreciated.

I attached both examples in the GH definition below
DataTree vs (429.0 KB)

private void RunScript(List<Brep> breps, ref object A)
  var crvTree = new GH_Structure<GH_Curve>();
  for (var i = 0; i < breps.Count; i++)
    for (var j = 0; j < breps[i].Curves3D.Count; j++)
      crvTree.Append(new GH_Curve(breps[i].Curves3D[j]), new GH_Path(0, i));
  var sorted = crvTree.Duplicate();
  for (var i = 0; i < sorted.PathCount; i++)
    sorted[sorted.Paths[i]].Sort((a, b) => b.Value.GetLength().CompareTo(a.Value.GetLength()));
  A = sorted;
} (424.2 KB)

1 Like

@Mahdiyar thanks. It is a nice implementation and works really well!