Grasshopper

algorithmic modeling for Rhino

Hi,

I am building a GHA upon some VB scripted components, which are already working quite fine in GH. A few of these components, take in an input, which is a datatree(of object). I realized that I better take a datatree as a GH_Structure(of T) in VS. However, I get this error when I build my class. 

Error 1 Type argument 'Object' does not inherit from or implement the constraint type 'Grasshopper.Kernel.Types.IGH_Goo'. d:\pnourian\my documents\visual studio 2012\Projects\SpaceSyntaxDesign_GH\SpaceSyntaxDesign_GH\Class1.vb 379 40 SpaceSyntaxDesign_GH

BTW, this datatree(of Object) contains data of types String, Double and Color; in case someone has a better suggestion...

I have been searching for this goo in GH SDK and on wiki pages but I haven't gotten the point yet. I wonder why I should define the type object as something else; and what that other type is!?

I appreciate any help in advance,

Pirouz

Views: 1080

Replies to This Discussion

Hi, 

I found out that I need to get a Datatree(of Object) as a GH_Structure(of IGH_Goo), now my problem is how to convert data of type IGH_Goo to doubles, strings and colors!

DataTree is only suppose to be used in VB/C#/Python script components. As you found out, when making a GHA project you need to get a GH_Structure(Of T).

GH_Structure(Of T) has a constraint on T which means it has to implement IGH_Goo. If your input is a Number Parameter then you can get a GH_Structure(Of GH_Number). If your input is a string parameter, then you can get a GH_Structure(Of GH_String).

GH_Number and GH_Integer and GH_Brep etc. all have a Value property that gives access to the value inside the goo class. You can also use the GH_Convert class to convert those grasshopper wrapper types into useful types.

--

David Rutten

david@mcneel.com

Seattle, WA

Hi David, 

Thanks for replying from Seattle! The thing is, I have made a datatree of attributes, containing strings, doubles and colors, in order to make it easier for the user to provide them altogether. So I have defined it as a datatree(of object), so as to allow for containing different things. 

The following works in VS, but fails in GH when I drag in the built GHA.

Protected Overrides Sub SolveInstance(DA As IGH_DataAccess)
Dim Attributes As New GH_Structure(Of IGH_Goo)
Dim Tags As New List(Of String)
Dim Areas As New List(Of Double)
Dim Colors As New List(Of Color)
If DA.GetDataTree(Of IGH_Goo)(0, Attributes) Then
Tags = Attributes.Branch(0)
Areas = Attributes.Branch(1)
Colors = Attributes.Branch(2)
DA.SetDataList(0, Tags)
DA.SetDataList(1, Areas)
DA.SetDataList(2, Colors)
End If
End Sub

Do I need to iterate through every branch and convert them to strings, doubles and colors separately or there is a better way?

Thanks for your support,

Pirouz

That shouldn't work in Visual Studio. If it does, it means you're not working in Option Explicit mode in VB and you won't catch a lot of problems until you try and run your code.

Your GH_Structure(Of IGH_Goo) contains branches that are List(Of IGH_Goo). In order to get all this data into a List(Of String), you'll have to actually iterate over them*:

Dim areas As New List(Of String)

For Each gooArea As IGH_Goo in Attributes.Branch(1)

  If (gooArea Is Nothing) Then

    areas.Add(Double.NaN)

  Else

    Dim area As Double = Double.NaN

    GH_Convert.ToDouble(gooArea, area, GH_Conversion.Both)

    areas.Add(area)

  End If

Next

IGH_Goo can always be a null reference, so you have to check for that. Furthermore, data could be stored as GH_Integer or GH_String or something and only look like a double, so you're better off using GH_Convert.

If you're absolutely 100% certain that it can only ever be GH_Number, then you can write this:

...

Else

  areas.Add(DirectCast(gooTag, GH_Number).Value)

End If

--

David Rutten

david@mcneel.com

Seattle, WA

* There's probably ways using LINQ as well, but let's not go there today.

Thank you very much for the explanation. However, I have 

Option Strict Off
Option Explicit On

on the top of everything and I don't get any errors in VS. Am I missing something else?

(BTW, I was trying to use a Linq method .cast (of ResultT) to do this but it didn't work either.) If it is just because of datatrees (GH_Structures), may be I should consider using a list(of list(of Object)) instead; is that a good idea (except for making it unreadable within GH)? I actually don't get the essence of the problem that for instance it is not possible to directly cast a GH_String to String or a GH_Number to Double? If I understood you correctly, I can convert their value properties but not themselves. Right? Could you please enlighten me a bit on the necessity of all such conversions?

RSS

About

Translate

Search

© 2024   Created by Scott Davidson.   Powered by

Badges  |  Report an Issue  |  Terms of Service