Grasshopper

algorithmic modeling for Rhino

How to get the input parameter of your GH_Component in vb.net

In the class that builds the GH_component you register the parameter inputs in a Protected Overrides Sub. Now how can I use the input in an other sub?

Views: 885

Replies to This Discussion

RegisterInputParams and RegisterOutputParams are called from within the constructor of GH_Component. It is possible to remove and add parameters later as well (though never during a solution) by using the Params objects. So if you're inside a component and you want to iterate over your inputs:

For Each param As IGH_Param in Me.Params.Input

  'Do something with param

Next

--

David Rutten

david@mcneel.com

Poprad, Slovakia

Nicknames and even Names can change. It's always dangerous to rely on them. Accessing parameter through their index is the recommended way.

Also note that the volatile data may not have any branches. In that case Branch(0) will throw an exception. It's also possible for a branch to exist but not have any items. In that case Item(0) will throw an exception. It's also possible for a branch to contain null items. In that case calling ToString() on a null reference will throw an exception.

What exactly are you looking to accomplish with this code?

--

David Rutten

david@mcneel.com

Poprad, Slovakia

"name of the user"?

sorry, I must be confused today.

You're looking to populate a windows.forms.listview with input parameter names?

--

David Rutten

david@mcneel.com

Poprad, Slovakia

I see. Are you also absolutely certain that inputs only ever contain a single value and that value is not null?

IGH_Structure is a pretty minimal interface but GH_Structure(Of T) has some useful features for this sort of thing. Since you know the type of input parameter you also know what type of GH_Structure(Of T) you have. For example there are FirstItem and LastItem properties, both with the option to not include nulls. However this is all just icing on the cake, you can achieve all this with IGH_Structure as well.

To get the first value from all input values for a component, assuming all inputs are of type Param_String, something like this should work:

Dim strings As New List(Of String)

For Each param As IGH_Param in Me.Params.Input

  Dim stringParam As Param_String = TryCast(param, Param_String)

  If (stringParam Is Nothing) Then

    strings.Add("<parameter is not of type string>")

    Continue For

  End If

  Dim value As GH_String = stringParam.FirstItem(True)

  If (value Is Nothing) OrElse (value.Value Is Nothing) Then

    strings.Add("<null>")

  Else

    strings.Add(value.Value)

  End If

Next

--

David Rutten

david@mcneel.com

Poprad, Slovakia

RSS

About

Translate

Search

Videos

  • Add Videos
  • View All

© 2024   Created by Scott Davidson.   Powered by

Badges  |  Report an Issue  |  Terms of Service