Grasshopper

algorithmic modeling for Rhino

 

Hello there,

I’m trying to pass an array of object arrays which contain different data types between two grasshopper components. Not sure if I should be doing this at all, but I thought I'd try!

 

COMPONENT 1: - the user inputs a line (curve) and a length (double). It stores the start point of the curve and the length in an object[]

Input parameters:

pManager.Register_CurveParam( "Line", "Line", "Line to get start point of"); pManager.Register_DoubleParam("Length", "Line Length", "Length of Line to pass on");

 

Output parameters:

pManager.Register_GenericParam("Object", "Object", "mixed objects"); //insert output patameters

  

SolveInstance:

object[] objectArray = new object[2];

Rhino.Geometry.Curve line = null;

DA.GetData(0, ref line);

 

double length = 0;

DA.GetData(1, ref length);

 

objectArray [0] = line.PointAt(0.0);

objectArray [1] = length;

 

DA.SetData(0, objectArray);

 

So far so good (?). When I try to receive it in the next component down the line, I try the following:

 

COMPONENT2: Tries to read the object[] imported in the first component, and export the object[] as well as the length property in object[1]

Input parameters:

pManager.Register_GenericParam("objects", "objects in", "objects to process", GH_ParamAccess.list);

 

Output parameters:

pManager.Register_GenericParam("objects", "objects", "objects ");

pManager.Register_DoubleParam("object property", "object property", "object length");

 

SolveInstance:

List<object[]> objects = new List<object[]>();

DA.GetDataList(0, objects);

 

DA.SetData(0, objects);

DA.SetData(1, objects[0][1]);

 

In the output, I get the error “Invalid cast: ObjectWrapper >> Object[]”. I guess this has to do with GH_Goo wrapping the "object[]" into an "object" when exporting from the first component?

I also tried taking the "object[]" arrays in as "objects", which removes the error, but then I cannot apply the indexing I require to access the length property anymore…

 

I’m not sure if that’s at all the way to go about passing this kind of information between components. I looked at creating my own parameters following David's instructions in the SDK (is this the way this should be done?), but got very stuck when I tried to include multiple types (or arrays).

Any ideas or pointers on how I could pass multiple-type info on without needing multiple wires or converting everything to the same type (might not be possible) first?

 

Thanks for any help, and Merry Christmas,

Daniel

 

Views: 3010

Replies to This Discussion

Hi Daniel,

Hope this helps.

Regards,

Sridevi

Attachments:

hmm, thanks Sridevi, but I'm not sure this solves my issue. Does it work with you?

I can't even use .ElementAt(i) as you suggested to retrieve the "objects" item. "objects[i]" doesn't work either. I'll keep on playing around with it, perhaps I'll find a way!

Thanks again! Dan

That's odd. The code works for me. What if you try using the foreach loop? Something along the lines of ->

int index = 0;

foreach(object output in objects)

{

      DA.SetData(index, output);

      index++;

}   

may try List instead of arrays.. much more easy to work with.....

and make you own class.... you have to provide the class in both components

public class line_and_length_para

{

public int;

public Line;

}

this you simply have to cast in the second component...

line_and_length_para my_para = (line_and_length_para) object;

and you have perfect access

thanks for your help, both of two!

I tried lists and classes, foreach's and the behaviour you describe was what I was hoping for, but it seems I'm getting issues with grasshopper wrapping the objects or list of objects in a 

Grasshopper.Kernel.Types.GH_ObjectWrapper

type when I pass it in and out of components. From this type I cannot cast to my custom object. 

If I understand correctly, David Rutten talks about this in the SDK examples section when describing custom data types. I think I might have to write my own CastTo-function when specifying my class, which seems complicated if I have more than one .Value (of different types).

I'm currently trying to write a class which holds a list of objects as .Value, but as you might have guessed I'm not exactly the world expert in writing code ;-)

Michael, have you ever tried your way of passing a class from one component to another? Don't you get the "Invalid cast: Grasshopper.Kernel.Types.GH_ObjectWrapper >> custom type" error?

I'll keep on trying and keep you posted, I know there's a way somehow!

Dan

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