Grasshopper

algorithmic modeling for Rhino

Hi all, 

I want to make a custom component which has a Data Tree input and gives a specific item in a list as an output.

It gives me the cast error: Specified cast is not valid.

Can anyone tel me what is the problem?

Thanks a lot!!!!!!!!!!!!!!

Here is my script:

-Protected Overrides Sub RegisterInputParams(ByVal pManager As GH_Component.GH_InputParamManager)
pManager.Register_LineParam("Line", "LT", "my lines",
Grasshopper.Kernel.GH_ParamAccess.tree)

-Protected Overrides Sub RegisterOutputParams(ByVal pManager As GH_Component.GH_OutputParamManager)
pManager.Register_lineParam("point", "P", "my points")

-Protected Overrides Sub SolveInstance(ByVal DA As IGH_DataAccess)


Dim Edge As Rhino.Geometry.Line = Rhino.Geometry.Line.Unset

Dim edgetree As New Data.GH_Structure(Of GH_Line)

If (Not DA.GetDataTree(0, edgetree)) Then Return

Edge = edgetree.Branch(0).Item(0)

DA.GetData(0, Edge)

 

Views: 1774

Replies to This Discussion

Edge = edgetree.Branch(0).Item(0)

DA.GetData(0, Edge)

Edge is of type Line, but the data inside the tree if of type GH_Line. This is your cast error.

Also, assigning data is doe with the DA.SetData() method, not GetData.

--

David Rutten

david@mcneel.com

Poprad, Slovakia

 

The data inside the tree is Line, and I want get the start and endpoint of those lines. 

A question here is that when I define a data as type GH_Line, there is no property access as ".From" or ".To" for that. So I cannot have the start or end point of the line. Is there any other way to get those properties OR Is there any other way to define the input datatree but  "As New Grasshopper.Kernel.Data.GH_Structure(Of GH_Line)"??

  

Protected Overrides Sub SolveInstance(ByVal DA As IGH_DataAccess)
Dim Edge As Grasshopper.Kernel.Types.GH_Line = Nothing
Dim edgelist As New List(Of GH_Line)

Dim edgetree As New Grasshopper.Kernel.Data.GH_Structure(Of GH_Line)

If (Not DA.GetDataTree(0, edgetree)) Then Return

Edge.From ?!!! (Doesn't work)

 

 

Any help is appreciated!

The data inside the tree cannot be Line, because the type Line in RhinoCommon does not implement the IGH_Goo interface, which is a prerequisite of GH_Structure.

This is why there is a GH_Line class, which exposes the Line type while implementing the IGH_Goo interface. This is true for practically all data types in Grasshopper. Rhino.Geometry.Brep is wrapped up in GH_Brep. System.Double is wrapped up in GH_Number. 

If you want to get at the actual data inside the wrapper classes, there's usually a Value property that provides getter/setter access to the real data stored inside the wrapper.

Dim edgeData As Data.GH_Structure(Of GH_Line) = Nothing

If (Not DA.GetDataTree(0, edgeData)) Then Return
Dim edge As Line = edgeData.Branches(0)(0).Value

Note that when you retrieve IGH_Goo type data from input parameters you get the original data. This data may be shared amongst many parameters and therefore you should not change this data. Unfortunately there is no constness in .NET so I cannot enforce this limitation without copying data and I want to copy data as little as possible due to overhead.

Incidentally, if you want to make sure that there are no exceptions thrown in the code, you should probably add more tests:

Dim edgeData As GH_Structure(Of GH_Line) = Nothing

If (Not DA.GetDataTree(0, edgeData)) Then Return

If (edgeData Is Nothing) Then Return

If (edgeData.PathCount = 0) Then Return

Dim edgeList As List(Of GH_Line) = edgeData.Branch(0)

If (edgeList Is Nothing) Then Return

If (edgeList.Count = 0) Then Return

Dim edgeWrap As GH_Line = edgeList(0)

If (edgeWrap Is Nothing) Then Return

Dim edge As Line = edgeWrap.Value

As you can see, working with GH_Structures will involve a lot of bookkeeping and validation.

--

David Rutten

david@mcneel.com

Poprad, Slovakia

Great explanation! Thanks

Hi David, 

I have a similar problem with a GH_Structure(of GH_Number). I've written a code that perfectly functions in Grasshopper, but when I build it as a GHA, it gives an error because it cannot do a multiplication between a GH_Number and a double. I guess there is no way to get a datatree (for building a GHA in visual studio) as a datatree(of T) and we should instead get a GH_Structure(of T).If so, how should I convert the data of GH_types into normal types?

Ps. I exactly tried to get the value of some of my GH_Structre (Ntw_DF.Branch(i)(j)) and I couldn't find it. 

Best,

Pirouz

Sorry to butt in, but perhaps you could try the GH_Structure<GH_Number>.get_DataItem(GH_Path path, int index) method to get your doubles and then reconstruct your tree/ do multiplication/ whatever.

I have used the simpler get_DataItem(int index) on lists before and it seems to work fine in terms of getting a double:

 

GH_Structure<GH_Number> myData = (GH_Structure<GH_Number>)Params.Input[0].VolatileData;

double myJimmy = myData.get_DataItem(0).Value;

 

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