Grasshopper

algorithmic modeling for Rhino

Hi,

I have a logic brain teaser that I've become stuck over. I need to split a series of lines that are stored in a single dimensional array. The resulting split lines will be stored in a multi-dimensional array. I've noticed that the first loop works perfectly, cylcing through the empty split array (called gtSplit) and filling it with null.  The second loop is faulty tho. It tries to fill the array with the split parts.

Logically every original curve (i) gets divided into two parts, and one division operation produces two pieces (j). The loop is designed to produce two j for every i. 

Any thoughts?

Code: note that gtCurve is the incoming curve array of an arbitrary size and that gtLength is the measured length of a curve (double), not the number of elements in the curve array.

// Counter for gtCurve

int[] count = gtCurve.Length;

// Array to hold split curves, note that the original curve is split into two parts 

Curve[,] gtSplit = new Curve[count,2] 

// Loop designed to fill gtSplit with null

for (int i = 0; i < count; i++) // cylces through the original curves (think array rows)

{

for(int j = 0; i < 2; j++) // cycles through the array columns to fill them with null

  {

  gtSplit[i,j] = null;

  }

}

// Loop designed to fill gtSplit with split parts

for (int i = 0; i < count; i++) // cylces through the original curves

{

for(int j = 0; i < 2; j++) // cycles through columns to fill with split parts

  {

  gtSplit[i,j] = gtCurve[i].Split(gtLength[i]/2);   //error code

  }

}

The error provided on trying to execute the code is:

"Cannot implicitly convert type Rhino.Geometry.Curve[] to Rhino.Geometry.Curve"

The error stems from the line of code commented as "error code"

I'm a little puzzled by the error because my variables are each arrays (I have never defined variables as single elements). So I guess I'm wondering if this is this coming from something deeper in the method or my logic?

Views: 1273

Replies to This Discussion

Hi there,

the method Split you are using returns an array. The error comes from the fact that you are trying to assign a single cell in the 2D array with the result of the split operation.

2D arrays (like all MD/MultiDimensional arrays in .Net) must have always the same number of rows and columns. If you know in advance you always have two columns, then you might just set the first one and the second one manually. However, there is no language keyword to set a whole row or a whole column.

On the other hand, you could create a single array of type Curve[][] (not Curve[,]), where you could then nest an array inside each cell. Logically similar but does not requite copying.

Otherwise, you might approach this in even different ways, depending what you want to do later.

I hope this helps,

Giulio
--
Giulio Piacentino
for Robert McNeel & Associates
giulio@mcneel.com

Giulio,

This approach worked out very well. Thank you!

RSS

About

Translate

Search

Photos

  • Add Photos
  • View All

Videos

  • Add Videos
  • View All

© 2024   Created by Scott Davidson.   Powered by

Badges  |  Report an Issue  |  Terms of Service