Advertisement
Guest User

Untitled

a guest
Nov 22nd, 2011
339
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.64 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Grasshopper.Kernel;
  6.  
  7. namespace Avw.Elephant
  8. {
  9.     public class TupleComponent : GH_Component
  10.     {
  11.         public TupleComponent() : base("Tuple", "Tuple", "Create A tuple from a string", "Extra", "Elephant")
  12.         {
  13.         }
  14.  
  15.  
  16.         protected override void RegisterInputParams(GH_Component.GH_InputParamManager pManager)
  17.         {
  18.             pManager.Register_StringParam("Input", "I", "Input key/value pair", GH_ParamAccess.tree);
  19.         }
  20.  
  21.         protected override void RegisterOutputParams(GH_Component.GH_OutputParamManager pManager)
  22.         {
  23.             pManager.Register_GenericParam("Tuple", "T", "A key/value tuple");
  24.         }
  25.  
  26.         protected override void SolveInstance(IGH_DataAccess DA)
  27.         {
  28.             Dictionary<string, string> Output = new Dictionary<string, string>();
  29.  
  30.             // iterate through all the sources by the Params.input value (and thus bypass the DA object, is this ugly?)
  31.             foreach (IGH_Param Param in this.Params.Input[0].Sources)
  32.             {
  33.                 String nickname = Param.NickName;
  34.  
  35.                 if (nickname.Length == 0)
  36.                 {
  37.                     this.AddRuntimeMessage(GH_RuntimeMessageLevel.Warning, "Nickname of the connected component is empty");
  38.                 }
  39.  
  40.                 Grasshopper.Kernel.Data.IGH_StructureEnumerator DataList = Param.VolatileData.AllData(false);
  41.  
  42.                 // Convert the list to strings
  43.                 List<String> Data = new List<String>();
  44.                 foreach (Grasshopper.Kernel.Types.GH_String DataItem in DataList)
  45.                 {
  46.                     Data.Add(DataItem.Value);
  47.                 }
  48.  
  49.                 // Add the data to the list. If result is a tree or something similar: this means the results will be flattened.
  50.                 if (Data.Count == 1)
  51.                 {
  52.                     // If the component has a single output: name it singular
  53.                     Output.Add(nickname, Data[0]);
  54.                 } else if (Data.Count > 1)
  55.                 {
  56.                     // .. otherwise: add an index value to it.
  57.                     int j = 0;
  58.                     foreach (String item in Data)
  59.                     {
  60.                         Output.Add(nickname + "-" + j.ToString(), item);
  61.                         j++;
  62.                     }
  63.                 }
  64.             }
  65.             DA.SetDataList(0,Output);
  66.         }
  67.  
  68.         public override Guid ComponentGuid
  69.         {
  70.             get { return new Guid("{2C479128-F6CA-435d-8CED-C2B3B99A03CC}"); }
  71.         }
  72.     }
  73. }
  74.  
  75.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement