Grasshopper

algorithmic modeling for Rhino

Creating a Component with a variable number of output parameters

This topic is a follow-up from a discussion started by Benjamin Golder. In it I will show the necessary steps for creating a custom component that has a variable number of output parameters, based on the data structure of the input.

I'll create a component that aims to write all the GH_Paths inside the input data structure into separate output parameters. I'll add a menu item to the component that allows users to synch the number of outputs with the current data.

Note that there are some bugs I found related to Undo here, but I'll attempt to fix those asap. The mechanisms employed in this example are correct.

Let's start with the Component class definition and the constructor:

Public Class GH_ExampleComponent_VarOutput
  Inherits GH_Component

  Public Sub New()
    MyBase.New("Extract Paths", "ExPath", "Extract all the paths from a tree", "Sets", "Tree")
  End Sub

End Class

Now, the RegisterXXXXParams methods:

Protected Overrides Sub RegisterInputParams(ByVal pManager As GH_Component.GH_InputParamManager)
  pManager.Register_GenericParam("Tree", "T", "Data tree to examine", GH_ParamAccess.tree)
End Sub
Protected Overrides Sub RegisterOutputParams(ByVal pManager As GH_Component.GH_OutputParamManager)
  'We'll add one output parameter, just to not have a jagged output.
  pManager.Register_PathParam("Path 1", "1", "1st path in tree")
End Sub

SolveInstance() is somewhat special, but not very complicated:

Protected Overrides Sub SolveInstance(ByVal DA As IGH_DataAccess)
  'We have only one input parameter and it is set to Tree, 
  'so SolveInstance will only be called once for every solution.

  'We don't actually need the data inside the input, we're only interested in the paths.
  'So we don't actually need to call DA.GetDataTree, we can just go in and extract the 
  'paths directly:
  Dim paths As IList(Of GH_Path) = Params.Input(0).VolatileData.Paths

  'Abort if there is no tree.
  If (paths.Count = 0) Then Return

  'Post a warning if the number of output parameters does not 
  'equal the number of paths in the tree.
  If (paths.Count < Params.Output.Count) Then
AddRuntimeMessage(GH_RuntimeMessageLevel.Warning, "There are more outputs than paths in the tree.")
  ElseIf (paths.Count > Params.Output.Count) Then
AddRuntimeMessage(GH_RuntimeMessageLevel.Warning, "There are fewer outputs than paths in the tree.")
  End If

  'Iterate over all paths and assign to output parameters.
  For i As Int32 = 0 To Math.Min(Params.Output.Count, paths.Count) - 1
DA.SetData(i, paths(i))
  Next
End Sub

Adding a menu item to the component menu is relatively straightforward, however handling the menu command requires a fair bit of logic:

Protected Overrides Sub Menu_AppendCustomComponentItems(ByVal iMenu As System.Windows.Forms.ToolStripDropDown)
  'Add a single item to the component menu.
  Menu_AppendGenericMenuItem(iMenu, "Synch outputs", AddressOf Menu_SynchOutputClicked)
End Sub
Private Sub Menu_SynchOutputClicked(ByVal sender As Object, ByVal e As EventArgs)
  'Here we have to synch the number of output parameters with the number 
  'of paths in the volatile data tree in the input parameter.
  'This requires a few steps:
  '1. Determine whether something needs to happen at all.
  '2. Record an undo event.
  '3. Remove excess outputs or add missing outputs.

  Dim paths As IList(Of GH_Path) = Params.Input(0).VolatileData.Paths
  If (paths.Count = Params.Output.Count) Then Return 'yay, nothing needs to be done.

  'Something needs to be done, record an undo state.
  RecordUndoEvent("Synch output")

  'We either have too few or too many outputs, determine which is the case.
  If (paths.Count > Params.Output.Count) Then
'Add the missing outputs
For i As Int32 = Params.Output.Count + 1 To paths.Count
 Dim param As New Grasshopper.Kernel.Parameters.Param_GenericObject()
 param.Name = "Path " & i.ToString()
 param.NickName = i.ToString()

 If (i.ToString.EndsWith("1")) Then
param.Description = i.ToString() & "st path in tree"
 ElseIf (i.ToString.EndsWith("2")) Then
param.Description = i.ToString() & "nd path in tree"
 ElseIf (i.ToString.EndsWith("3")) Then
param.Description = i.ToString() & "rd path in tree"
 Else
param.Description = i.ToString() & "th path in tree"
 End If

 Params.RegisterOutputParam(param)
Next

  Else
'Remove excessive outputs
Do
 If (Params.Output.Count <= paths.Count) Then Exit Do
 Dim param As IGH_Param = Params.Output(Params.Output.Count - 1)
 Params.UnregisterOutputParameter(param)
Loop
  End If

  Params.OnParametersChanged()
  ExpireSolution(True)
End Sub

Finally, we must make sure that the component properly (de)serializes. This means we have to override the Write and Read methods and add additional information to the GHX archive:

Public Overrides Function Write(ByVal writer As GH_IO.Serialization.GH_IWriter) As Boolean
  'We must make sure that the number of output parameters is correctly stored.
  'We'll use a special function on the GH_ComponentParamServer to accompish this
  'without too much sweat.
  Params.WriteParameterTypeData(writer)

  Return MyBase.Write(writer)
End Function
Public Overrides Function Read(ByVal reader As GH_IO.Serialization.GH_IReader) As Boolean
  'Very important, we must make sure all parameters exist before we 
  'start with the main deserialization.
  Params.Clear()
  Params.ReadParameterTypeData(reader)

  Return MyBase.Read(reader)
End Function

I attached a VB file that contains the code outlined above.

--
David Rutten
david@mcneel.com
Seattle, WA

Views: 4372

Attachments:

Replies to This Discussion

What are you looking for? An example showing parameter or an example showing how to execute .NET user code?

--

David Rutten

david@mcneel.com

Tirol, Austria

There are a lot of great pointers in here, including some code that David has written for managing the process of conditionally adding and removing parameters...

http://www.grasshopper3d.com/forum/topics/gha-developers-implementi...

Thanks for this

There's plenty of tutorials for C# already. The internet pipes are positively clogged with them.

--

David Rutten

david@mcneel.com

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