How to Flatten a Rhino.Collections.CurveList to use DoDirectionsMatch

I have been struggling with a way to flatten a Rhino.Collections.CurveList. The end goal is to take the Curve list called LastValue and check if the curves are align or not.

The problem is that when I use DoDirectionsMatch(curve1, curve2) my code seems to be comparing every curve against itself instead of compare with all curves in the structure.

As a result it is always showing true. In this case two lines are in the same direction but one is pointing in the other direction:

My code is here. I was thinking that easy way would be flatten LastValue, the problem is that this is a Rhino collection curve list and there is no Flatten method associated.

  private void RunScript(Brep _Brep, ref object AuxiliarResults, ref object edges, ref object length_, ref object sortedCurves, ref object LastValue, ref object CurveDirectionBool)
  {
    // Get the edges of the solid
    Rhino.Geometry.Collections.BrepCurveList curvesFromBrep = _Brep.Curves3D;
    Rhino.Collections.CurveList curvesFromBrepCurves1 = new Rhino.Collections.CurveList();

    for (int i = 0; i < _Brep.Curves3D.Count; i++)
    {
      var curve = _Brep.Curves3D[i];
      curvesFromBrepCurves1.Add(curve);
    }

    // Get the dimension of a curve
    int listLength = curvesFromBrep.Count;
    Rhino.Collections.RhinoList <double> curveLength = new  Rhino.Collections.RhinoList<double>();

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

    // Sort by length
    double[] curveLengthArray;
    curveLengthArray = curveLength.ToArray();
    curvesFromBrepCurves1.Sort(curveLengthArray);

    // Get the last element of a sorted list
    Rhino.Collections.CurveList lastCurve = new Rhino.Collections.CurveList();
    lastCurve.Add(curvesFromBrepCurves1.Last);

    // Check if directions match on the new list of elements created only by the last element of sorted curves
    Rhino.Collections.RhinoList <bool> cueveDirectionList = new  Rhino.Collections.RhinoList<bool>();

    for (int i = 0; i < lastCurve.Count; i++)
    {
      bool curveDirection = Rhino.Geometry.Curve.DoDirectionsMatch(lastCurve[0], lastCurve[i]);
      cueveDirectionList.Add(curveDirection);
    }

    // Outputs
    edges = curvesFromBrep;
    length_ = curveLength;
    sortedCurves = curvesFromBrepCurves1;
    LastValue = lastCurve;
    CurveDirectionBool = cueveDirectionList;
    //AuxiliarResults =



  }

Is there a better way to approach this task? Any thoughts?

Brep001.3dm (386.3 KB)
Flatten Rhino.Collections.CurveList.gh (19.0 KB)

Welcome to the forum. I can see what’s wrong, let my try to explain it in an understandable way.

Your input named _Brep is set to Item Access and you are inputting 3 Breps. This means that the code runs one iteration for each Brep.

  1. In the first iteration, you compare the lines in the first Brep to each other.
  2. Then in the second iteration, you compare the lines in the second Brep to each other.
  3. Then finally in the third iteration, you compare the lines in the third brep to each other.

This is why your outputs are DataTrees with 3 branches - there is one branch for each code iteration.

To get your code to work like you want, you’ll have to change your _Brep input to List Access and re-write your code to iterate the input list of Breps. You can create a separate list for the curves from each Brep or put them in one list as you desire (you’ll have to re-write your code to deal with the sorting and comparing).

I hope I’ve explained this in a way that can be understood. Don’t be afraid to ask questions - there are lots of very helpful people on this forum.

-Kevin

1 Like

@kev.r thanks for the clear explanation.

For the first part of my algorithm I wanted to have a one iteration for each Brep.

See my GH Prototype example:

Right before the flip step is where I flatten the tree to be able to make the flip. My question here is: Is there a way to flatten Rhino.Collections.CurveList so I can obtain a similar behavior shown on the picture above? (I attached the file as well). Or is there any other way before I commit to re-write my code?

My end goal is to develop a GH component. My thought process was:

  1. Develop a prototype on GH with GH components first
  2. Develop a C# Component using the C# Script Component
  3. Develop custom Grasshopper components and plugins on Visual Studio.

I figured this 3 step process would be less painful than jump all at once to develop the custom Grasshopper components and plugins on Visual Studio.

Brep001.3dm (386.7 KB)
GH Prototipe to convert in a C# component.gh (32.7 KB)

No, you can’t flatten a Rhino.Collections.CurveList. It is a single dimensional list not a DataTree.

I’ve re-written your code to use List Access on the input parameter. I think it works in the way you intended. I’ve sorted the curves from longest to shortest, so the longest curves are at index [0] in the lists.

I have also internalised your breps in the grasshopper file, so no need to include a Rhino file.

CurveList_re.gh (162.5 KB)

-Kevin

1 Like

@kev.r thanks for your response . Also thanks for taking the time to re-write the code. It works as intended. Right now I am studying the code you provided to fully understand it.

Made another version of this using DataTrees to store the curves instead of Lists.

Works basically the same either way, but it seems like DataTrees are the more “grasshopper” way of doing things. DataTrees are also a topic I want to explore further.

CurveList_re_210814a.gh (162.0 KB)

-Kevin

1 Like

@kev.r this is really useful. Both examples you provided solved the issue plus they are really great simple examples for beginners like me on how to work with list and trees.

I agree the DataTrees look a bit more like the “grasshopper” way.