Grasshopper

algorithmic modeling for Rhino

Dealing with arrays/lists with the C# component in Grasshopper 3D

Hi there.

New user of Grasshopper 3D here, and I am in need of some C# syntax help for coding in Grasshopper 3D.

I have a script, for example, that's pasted below:

public static int arraySum(int[] myArray){

    int someValue = 0;

    for(int i = 0; i < myArray.Length; i++){

       someValue += myArray[i];

    }

    return someValue;

}

The above static method sums all values of an array.

From my understanding of the scripting components of C# in Grasshopper, you cannot create static methods, as everything is a non-returning void method. You assign a variable (the output) as a psuedo-return, is that correct?

Knowing that - how do I implement my above script, for example, to a C# component?

Instead of having a "return", I simply assigned a variable, for example, A as the sum. But I ran into some issues there, for example, with some C# methods like .Length not working.

Views: 4506

Replies to This Discussion

Hi Jacky,

the method that gets executed by the script component indeed does not return any values, however you can create all the methods and types you want inside the <custom additional code> tags.

You can paste the method above into this area, then write:

Note that if you rewrite your static method to operate on IEnumerable<int> instead of int[] you can use a foreach loop and you don't have to convert the list to an array.

If you want to type this logic directly into the RunScript() method, then you'd need to write the following:

private void RunScript(List<int> N, ref object A)

{

  int sumTotal = 0;

  foreach (int value in N)

    sumTotal += value;

  A = sumTotal;

}

--

David Rutten

david@mcneel.com

Tirol, Austria

Sorry to retrieve an old thread.
 I'm actually facing the same issue, but i'm implementing more than one method in the additional code.

Unfortunately an issue is raised:

Error (CS1106): Extension method must be defined in a non-generic static class (line 31)

Where line 31 is:

public class Script_Instance : GH_ScriptInstance

I'm quite new to C#, coming from python, so methods handling is causing me lots of trouble. Could you explain me where is the error here?

for(int i = 0; i < SrcPts.Count; i++) {
Plane B_Plane = new Plane(SrcPts[i], Vector3d.ZAxis);
if (SrcPts.Count == XY_Angles.Count){
B_Plane.Rotate(XY_Angles[i], B_Plane.ZAxis);
}
else {}
}

B = SD_Data.Chunk(314); //Splitting multiple merged .SD1 filetype
}

<Custom additional code>

public static IEnumerable<IEnumerable<T>> Chunk<T > (this IEnumerable<T> source, int chunksize)
{
while (source.Any())
{
yield return source.Take(chunksize);
source = source.Skip(chunksize);
}
}

public static Plane EmissivePlane( Rhino.Geometry.Point3d pts, double angles) {
Plane BasePlane = new Plane(pts, angles);
}

Thanks for the reply Tom, and for the valuable advices.

I already got out Extension method and turned to standard.
It seems quite odd to me that I need to create a class of methods, and call them in the RunScript, since where already are in the GH Component class.

Just a question.
Will DataTree allow me access items through multiple nested loops?


Edit:
I do not think it will let me create extension methods inside nested classes.
It is really another world compared to Python.

And just one more question.
Are there performance differences in using DataTree against c# native types?

Thanks again in advance!

An Array (System.Array) is fixed in size once it is allocated. You can't add items to it or remove items from it. Also, all the elements must be the same type. As a result, it is type safe, and is also the most efficient of the three, both in terms of memory and performance.

Use c# array when you are dealing with data that is:

*fixed in size, or unlikely to grow much
*suitably large (more than 10, 50, 100 elements, depending on the algorithm)
*you will be doing lots of indexing into it, i.e. you know you will often want the third element, or the fifth, or whatever.

A List<> leverages generics; it is essentially a type-safe version of ArrayList. This means there is no boxing or unboxing (which improves performance) and if you attempt to add an item of the wrong type it'll generate a compile-time error.

Use a c# list for:

*variable length data lists
*that are mostly used as a stack or a queue or need to be iterated in its entirety
*when you do not want to write an expression to derive the ultimate array size for the declaration and you do not want to wastefully pick a large number

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