Grasshopper

algorithmic modeling for Rhino

Hi,

I have got own class called GH_Model.

Model has a property Elements what is a List of GH_Elements.

I need to cast from GH_Model output to GH_Elements input (between two components).

So I need to cast List<GH_Elements> from GH_Model (no only one object).

 

Is it possible cast to list or array of object? How can I do it?

 

This is my try for better understanding what I want: 


public override bool CastTo(ref Q target)
{

//TRY CAST TO List.
if (typeof(Q).IsAssignableFrom(typeof(List<GH_Elements>)))
{
object ptr = this.Value.Elements; //this is List<GH_Elements>
target = (Q)ptr;
return true;
}

//TRY CAST TO GH_Structure.
if (typeof(Q).IsAssignableFrom(typeof(GH_Structure<GH_Elements>)))
{
object ptr = this.Value.Elements.ToArray();
target = (Q)ptr;
return true;
}

//THIS WORKS (only single object)
if (typeof(Q).IsAssignableFrom(typeof(GH_Element)))
{
object ptr = this.Value.Elements[0];
target = (Q)ptr;
return true;
}

return false;
}

Thanks

Views: 631

Replies to This Discussion

Resurrecting an old post... but did you get anywhere with this? Did you get it working?

Hello,

Same here. I'm running in the same kind of problem. Can anybody can give a little clue on that ?

I have a custom object (Grid) which internal representation is a jagged array (data = double[N][]).

Each sub array (data[d], d=0 … N-1) correspond to a « dimension » of the grid object. Additionally, each dimension can have a Label (labels = string[N])

I'm setting up a GH_Grid data type so it can be wired through GH caneva.
I would like to CastTo this custom object into a DataTree<double> or GH_Structure<GH_Number> when the user plug it into a number parameter. I also would like to CatFrom a  DataTree<double> or GH_Structure<GH_Number> input to create a new Grid object with empty labels.

Will the help and forum post are quite helpful form « item » parameter, this is quite confusing to me how it works for list or tree input/outputs.

Thanks.

The casting methods only allow you to convert from one instance to one other instance. It's not possible to use this approach to convert some type of IGH_Goo into a list or a tree of IGH_Goo. You'll need to create a custom component for such a conversion.

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