Grasshopper c# editing GH_Path in loop

Hi all,

What is the best method to edit the index of GH_Path within a loop? I’m new to rhino commons, so any help would be appreciated.

I have currently written the code like this, but I am sure there must be a better way just to have one sortingPath and increment the path index within the loop by “j”.

Thanks guys!

// sortPoints
    
    GH_Path sortingPath_0 = new GH_Path(0);
    GH_Path sortingPath_1 = new GH_Path(1);
    GH_Path sortingPath_2 = new GH_Path(2);
    GH_Path sortingPath_3 = new GH_Path(3);
    GH_Path sortingPath_4 = new GH_Path(4);
    
    for( int i = 0; i < subCurvePoints.BranchCount; i++){
      for( int j = 1; j < subCurvePoints.Branches[i].Count; j++){
        
        if( j == 0){
          subCurvePointsFlipped.Add(subCurvePoints.Branches[i][j], sortingPath_0);
        }
        
        if( j == 1){
          subCurvePointsFlipped.Add(subCurvePoints.Branches[i][j], sortingPath_1);
        }
        
        if( j == 2){
          subCurvePointsFlipped.Add(subCurvePoints.Branches[i][j], sortingPath_2);
        }
        
        if( j == 3){
          subCurvePointsFlipped.Add(subCurvePoints.Branches[i][j], sortingPath_3);
        }
        
        if( j == 4){
          subCurvePointsFlipped.Add(subCurvePoints.Branches[i][j], sortingPath_4);
        }
      }
    }
public DataTree <Point3d> subCurvePoints;
 public DataTree <Point3d> subCurvePointsFlipped;

 static int max = 5;

 public void Test(){

   int[] validIdx = Enumerable.Range(0, max).ToArray();        // set some max idx

   for( int i = 0; i < subCurvePoints.BranchCount; i++){
     for( int j = 0; j < subCurvePoints.Branch(i).Count; j++){  // why was 1?
       if(validIdx.Contains(j)) subCurvePointsFlipped.Add(subCurvePoints.Branch(i)[j], new GH_Path(j));
       else break;
     }
   }
 }

Hey, thanks for your reply.

I suppose my question was mostly about understanding how to modify an already declared GH_Path to a new index rather than using new GH_Path.

For example:

GH_Path somePath = new GH_Path(0);

Now if I want to change the path to an existing path 2 for example. I can’t seem to write without using “new”.

somePath = GH_Path(2); // doesn’t work

Is this possible to reassign this variable?

I hope that makes sense!

.
GH_Path is a Reference Type > thus use new:

GH_Path path = new GH_Path(0);
    for(int i = 1; i < 120;i++){
      path =  new GH_Path(i);
    }

Other than that I insist (in the name of simplicity) to use some validIdx dimensions collection (or some other Type if you have multi dims in mind) instead of asking the same question again and again (Contains is not Ducati fast … but who cares?). Obviously you can use an Interval as well.

Try some LOL stuff as well:

@PeterFotiadis - Thank you for your patience and response, albeit I have yet to wrap my head around how you have gone about things.

Nonetheless, I have been able to progress my script like this for the interim based on your comment, “GH_Path is a Reference Type > thus use new”.

    GH_Path sortingPath;

    for( int i = 0; i < subCurvePoints.BranchCount; i++){
      for( int j = 1; j < subCurvePoints.Branches[i].Count; j++){
        sortingPath = new GH_Path(j);
        subCurvePointsFlipped.Add(subCurvePoints.Branches[i][j], sortingPath);
      }
    }

Cheers!

Note: As shown above SortingPath has no restrictions (VS your initial code).

Other than that why use that public var? (i.e. use a new GH_Path directly as the destination Path) - meaning: learn to think with the most minimal way possible (just imagine writing 1000++ lines of code (with lot’s of Methods): as is the norm in real-life).

PS: Describe EXACTLY what you are after and we’ll see what is the minimal way to cut the mustard (assuming that you know what LINQ is [and Classes]).

Who knows? (and who cares?)

BTW: Maybe you are after - more or less - that kind of thing?
Curve_DivideByCount_V1.gh (121.3 KB)