c#_Create Value List GH Component for Custom GH Plugin

in this case you can use exactly the code provided, but add it in your element under the icon or whereever , just not inside, but outside of SolveInstance.

this line basically identifies, where the value list will be added at. the “1” inside the curvy brackets points on the second input parameter. it works, if you have something like this:

 protected override void RegisterInputParams(GH_Component.GH_InputParamManager pManager)
        {
            pManager.AddBrepParameter("Brep", "B", "the model to calculate", GH_ParamAccess.list);
            pManager.AddTextParameter("Material", "M", "select a value list to M to get a list of precious gems OR connect your own denisty value", GH_ParamAccess.item);
            pManager.AddBooleanParameter("Total", "T", "total weight if true, else single weight", GH_ParamAccess.item, true);
        }

your input parameters are numbered after their order they’ve been added. note that the second parameter is text and can accept strings that are given out from the value list.
the curvy brackets can be used like this as well {0,1,2}, which would give you three times the same list.

here you got a element code using the logic, you should be able to copy paste your code from here:


using Grasshopper.Kernel;
using Rhino.Geometry;
using System;
using System.Collections.Generic;
using System.Drawing;

namespace RhineGem
{
    public class GemWeight : GH_Component
    {
        /// <summary>
        /// Initializes a new instance of the MyComponent1 class.
        /// </summary>
        public GemWeight()
          : base("Gem Weight Calculator", "GmWeightCalc",
              "choose from a list of gems and calculates the total weight of gems",
              "RhineGem", "Analysis")
        {
        }

        /// <summary>
        /// Registers all the input parameters for this component.
        /// </summary>
        protected override void RegisterInputParams(GH_Component.GH_InputParamManager pManager)
        {
            pManager.AddBrepParameter("Brep", "B", "the model to calculate", GH_ParamAccess.list);
            pManager.AddTextParameter("Material", "M", "select a value list to M to get a list of precious gems OR connect your own denisty value", GH_ParamAccess.item);
            pManager.AddBooleanParameter("Total", "T", "total weight if true, else single weight", GH_ParamAccess.item, true);
        }

        /// <summary>
        /// Registers all the output parameters for this component.
        /// </summary>
        protected override void RegisterOutputParams(GH_Component.GH_OutputParamManager pManager)
        {
            pManager.AddTextParameter("Weight in carats", "W", "gem weight in carat in the selected gem type", GH_ParamAccess.item);
        }

        /// <summary>
        /// This is the method that actually does the work.
        /// </summary>
        /// <param name="DA">The DA object is used to retrieve from inputs and store in outputs.</param>
        protected override void SolveInstance(IGH_DataAccess DA)
        {
            List<Brep> Bs = new List<Brep>();
            if (!DA.GetDataList(0, Bs)) return;
            string M = "";
            if (!DA.GetData(1, ref M)) return;
            bool t = true;
            if (!DA.GetData(2, ref t)) return;

            double volcm = 0.0;
            if (t)
            {
                foreach (Brep B in Bs)
                {
                    volcm += B.GetVolume();
                }

                volcm /= 1000;
                double[] weight = { Math.Round(volcm * (Convert.ToDouble(M)), 4) };
                DA.SetDataList(0, weight);
            }
            else
            {
                List<double> dOut = new List<double>();
                for (int i = 0; i < Bs.Count; i++) dOut.Add(Math.Round(Bs[i].GetVolume() / 1000 * Convert.ToDouble(M), 4));
                DA.SetDataList(0, dOut);
            }
        }

        public override void AddedToDocument(GH_Document document)
        {

            base.AddedToDocument(document);


            //Add Value List
            int[] stringID = new int[] { 1 };

            for (int i = 0; i < stringID.Length; i++)
            {
                Grasshopper.Kernel.Parameters.Param_String in0str = Params.Input[stringID[i]] as Grasshopper.Kernel.Parameters.Param_String;
                if (in0str == null || in0str.SourceCount > 0 || in0str.PersistentDataCount > 0) return;
                Attributes.PerformLayout();
                int x = (int)in0str.Attributes.Pivot.X - 250;
                int y = (int)in0str.Attributes.Pivot.Y - 10;
                Grasshopper.Kernel.Special.GH_ValueList valList = new Grasshopper.Kernel.Special.GH_ValueList();
                valList.CreateAttributes();
                valList.Attributes.Pivot = new PointF(x, y);
                valList.Attributes.ExpireLayout();
                valList.ListItems.Clear();

                List<Grasshopper.Kernel.Special.GH_ValueListItem> materials = new List<Grasshopper.Kernel.Special.GH_ValueListItem>()
                 {
                    new Grasshopper.Kernel.Special.GH_ValueListItem("Actinolite","3.05"),
                    new Grasshopper.Kernel.Special.GH_ValueListItem("Uvarovite","3.465"),
                    new Grasshopper.Kernel.Special.GH_ValueListItem("Vanadium ","3.74"),
                    new Grasshopper.Kernel.Special.GH_ValueListItem("Williamsite","2.57"),
                    new Grasshopper.Kernel.Special.GH_ValueListItem("Zircon","4.33")

          };
                valList.ListItems.AddRange(materials);
                document.AddObject(valList, false);
                in0str.AddSource(valList);
            }
        }
        /// <summary>
        /// Provides an Icon for the component.
        /// </summary>
        protected override System.Drawing.Bitmap Icon
        {
            get
            {
                //You can add image files to your project resources and access them like this:
                // return Resources.IconForThisComponent;
                return Properties.Resources.gmbalance24;
            }
        }

        /// <summary>
        /// Gets the unique ID for this component. Do not change this ID after release.
        /// </summary>
        public override Guid ComponentGuid
        {
            get { return new Guid("!!!!!!!!!!!!!!!!!!!!!!!!myGUID!!!!!!!!!!!!!!"); }
        }
    }
}

add your own GUID!!

2 Likes