algorithmic modeling for Rhino
Now, the RegisterXXXXParams methods:Public Class GH_ExampleComponent_VarOutputInherits GH_ComponentPublic Sub New()MyBase.New("Extract Paths", "ExPath", "Extract all the paths from a tree", "Sets", "Tree")End SubEnd Class
Protected Overrides Sub RegisterInputParams(ByVal pManager As GH_Component.GH_InputParamManager)pManager.Register_GenericParam("Tree", "T", "Data tree to examine", GH_ParamAccess.tree)End SubProtected 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
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) ThenAddRuntimeMessage(GH_RuntimeMessageLevel.Warning, "There are more outputs than paths in the tree.")ElseIf (paths.Count > Params.Output.Count) ThenAddRuntimeMessage(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) - 1DA.SetData(i, paths(i))NextEnd Sub
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 SubPrivate 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.PathsIf (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 outputsFor i As Int32 = Params.Output.Count + 1 To paths.CountDim param As New Grasshopper.Kernel.Parameters.Param_GenericObject()param.Name = "Path " & i.ToString()param.NickName = i.ToString()If (i.ToString.EndsWith("1")) Thenparam.Description = i.ToString() & "st path in tree"ElseIf (i.ToString.EndsWith("2")) Thenparam.Description = i.ToString() & "nd path in tree"ElseIf (i.ToString.EndsWith("3")) Thenparam.Description = i.ToString() & "rd path in tree"Elseparam.Description = i.ToString() & "th path in tree"End IfParams.RegisterOutputParam(param)NextElse'Remove excessive outputsDoIf (Params.Output.Count <= paths.Count) Then Exit DoDim param As IGH_Param = Params.Output(Params.Output.Count - 1)Params.UnregisterOutputParameter(param)LoopEnd IfParams.OnParametersChanged()ExpireSolution(True)End Sub
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 FunctionPublic 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
Tags:
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...
There's plenty of tutorials for C# already. The internet pipes are positively clogged with them.
--
David Rutten
david@mcneel.com
Welcome to
Grasshopper
Added by Parametric House 0 Comments 0 Likes
Added by Parametric House 0 Comments 0 Likes
Added by Parametric House 0 Comments 0 Likes
Added by Parametric House 0 Comments 0 Likes
© 2024 Created by Scott Davidson. Powered by