Sort a BrepCurveList by length

Hi, I am doing some baby steps in the C# development world in Grasshopper using Rhino API.

Goal:
Sort by length a BrepCurveList that I obtain after using Brep.Curves3D Property. The problem is that BrepCurveList does not have a method to sort. I have been trying to cast a BrepCurveList to a CurveList so I can use the method sort. So far all my tries have failed.

In the first component (See attached Rhino and GH files), I tried creating a simple list:

    edges = _Brep.Curves3D;
    List<Curve> Test1 = new List<Curve>();
    Test1 = _Brep.Curves3D;

    // Get the dimension of a curve
    int listLength = _Brep.Curves3D.Count; // This line finds the length of the list to be analyze on the for loop
    loopLength = listLength;
    Rhino.Collections.RhinoList <double> curveLength = new  Rhino.Collections.RhinoList<double>();

    for (int i = 0; i < listLength; i++)
    {
      double lengthOfCurve = _Brep.Curves3D[i].GetLength();
      curveLength.Add(lengthOfCurve);
    }

    length_ = curveLength;

In this case I get:

  1. Error (CS0029): Cannot implicitly convert type ‘Rhino.Geometry.Collections.BrepCurveList’ to ‘System.Collections.Generic.List<Rhino.Geometry.Curve>’ (line 60)

In the second component, I tried as well using RhinoList :

    // Get the edges of the solid
    edges = _Brep.Curves3D;
    Rhino.Collections.RhinoList<Curve> Test1 = new Rhino.Collections.RhinoList<Curve>();
    Test1 = _Brep.Curves3D;

    // Get the dimension of a curve
    int listLength = _Brep.Curves3D.Count; // This line finds the length of the list to be analyze on the for loop
    loopLength = listLength;
    Rhino.Collections.RhinoList <double> curveLength = new  Rhino.Collections.RhinoList<double>();

    for (int i = 0; i < listLength; i++)
    {
      double lengthOfCurve = _Brep.Curves3D[i].GetLength();
      curveLength.Add(lengthOfCurve);
    }

    length_ = curveLength;



    // Sort by length

In this second case I get:

  1. Error (CS0029): Cannot implicitly convert type ‘Rhino.Geometry.Collections.BrepCurveList’ to ‘Rhino.Collections.RhinoList<Rhino.Geometry.Curve>’ (line 60)

I am starting my journey in C# developing with Rhino so if anybody can point me in the right direction I will really appreciate it.

Regards
SortUsingCSharpRhino.3dm (21.8 KB)
SortUsingCSharpRhino.gh (4.6 KB)

Hi @rolandoavillena ,
Instead of directly casting, you could initialize the List of curves by providing the Brep.Curves3D as argument, and then you can sort by length
List<Curve> Test1 = new List<Curve>(_Brep.Curves3D);

Or you could use LINQ to sort curves by length
List<Curve> sortedCurves= _Brep.Curves3D.OrderByDescending(b => b.GetLength()).ToList();

2 Likes

Thanks @Darryl_Menezes