Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using Grasshopper.Kernel;
- namespace Avw.Elephant
- {
- public class TupleComponent : GH_Component
- {
- public TupleComponent() : base("Tuple", "Tuple", "Create A tuple from a string", "Extra", "Elephant")
- {
- }
- protected override void RegisterInputParams(GH_Component.GH_InputParamManager pManager)
- {
- pManager.Register_StringParam("Input", "I", "Input key/value pair", GH_ParamAccess.tree);
- }
- protected override void RegisterOutputParams(GH_Component.GH_OutputParamManager pManager)
- {
- pManager.Register_GenericParam("Tuple", "T", "A key/value tuple");
- }
- protected override void SolveInstance(IGH_DataAccess DA)
- {
- Dictionary<string, string> Output = new Dictionary<string, string>();
- // iterate through all the sources by the Params.input value (and thus bypass the DA object, is this ugly?)
- foreach (IGH_Param Param in this.Params.Input[0].Sources)
- {
- String nickname = Param.NickName;
- if (nickname.Length == 0)
- {
- this.AddRuntimeMessage(GH_RuntimeMessageLevel.Warning, "Nickname of the connected component is empty");
- }
- Grasshopper.Kernel.Data.IGH_StructureEnumerator DataList = Param.VolatileData.AllData(false);
- // Convert the list to strings
- List<String> Data = new List<String>();
- foreach (Grasshopper.Kernel.Types.GH_String DataItem in DataList)
- {
- Data.Add(DataItem.Value);
- }
- // Add the data to the list. If result is a tree or something similar: this means the results will be flattened.
- if (Data.Count == 1)
- {
- // If the component has a single output: name it singular
- Output.Add(nickname, Data[0]);
- } else if (Data.Count > 1)
- {
- // .. otherwise: add an index value to it.
- int j = 0;
- foreach (String item in Data)
- {
- Output.Add(nickname + "-" + j.ToString(), item);
- j++;
- }
- }
- }
- DA.SetDataList(0,Output);
- }
- public override Guid ComponentGuid
- {
- get { return new Guid("{2C479128-F6CA-435d-8CED-C2B3B99A03CC}"); }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement