Extract parameter as a dropdown list

Hi, I am pretty sure that I have seen this somewhere but I can not find it. I want “Extract parameter” as shown in GH to be a dropdown list. It saves me from doing a dropdown list that contains “Line, Arc and Smooth”. Could someone please point me to the right direction. I use Rhino 5 and GH 1.0

image

go to Params tab, under Input, you will find Value List…

Thanks.It is not what exactly what I am after. I am after assigning the Line, Arc and Smooth automatically to a dropdown list.

I also wished the extract would be smarter for different inputs…

But set your value list as such and it will do what you want…
image

This option is only available for a few components like referenceByLayer from elefront:


There are other plugins that implement similar method in their components like FabTools and VRay.

I’ve made a little component that automatically creates ValueList whenever it connects to an input with name-values:

namespace AutoValueList
{
    public class AutoValueListComponent : GH_Component
    {
        private bool _handled = false;
        private GH_Document _doc;
        public AutoValueListComponent()
          : base("AutoValueList", "AutoValueList",
              "Connect to an input with named value options to create a ValueList",
              "Params", "Input")
        {
        }
        protected override void RegisterInputParams(GH_Component.GH_InputParamManager pManager)
        {
        }
        protected override void RegisterOutputParams(GH_Component.GH_OutputParamManager pManager)
        {
            pManager.AddGenericParameter("ToNamedValue", "NV", "Connect to an input with named-values to create a ValueList",
                GH_ParamAccess.item);
        }
        protected override void SolveInstance(IGH_DataAccess DA)
        {
            _doc = OnPingDocument();
            if (_handled) return;
            _doc.SolutionStart += DocOnSolutionStart;
            _handled = true;
        }
        private void DocOnSolutionStart(object sender, GH_SolutionEventArgs e)
        {
            if(Params.Output[0].Recipients.Count > 0)
                AddRuntimeMessage(GH_RuntimeMessageLevel.Warning, "This input doesn't have named-values");
            var c = -1;
            for(var i = 0; i < Params.Output[0].Recipients.Count; i++)
            {
                if (!(Params.Output[0].Recipients[i] is Param_Integer)) continue;
                c = i;
                break;
            }
            if (c == -1) return;
            var recipient = Params.Output[0].Recipients[c];
            var paramInt = (Param_Integer) recipient;
            if (!paramInt.HasNamedValues) return;
            var namedValues = (IList)typeof(Param_Integer).GetField("m_namedValues", BindingFlags.NonPublic | BindingFlags.Instance)?.GetValue(paramInt);
            if (namedValues == null) return;
            var vl = new GH_ValueList();
            vl.ListItems.Clear();
            foreach (var nv in namedValues)
            {
                var name = (string)nv.GetType().GetField("Name", BindingFlags.Public | BindingFlags.Instance)?.GetValue(nv);
                var value = (int)nv.GetType().GetField("Value", BindingFlags.Public | BindingFlags.Instance)?.GetValue(nv);
                var vi = new GH_ValueListItem(name, value.ToString());
                vl.ListItems.Add(vi);
            }
            vl.NickName = recipient.NickName;

            var docIo = new GH_DocumentIO { Document = new GH_Document() };
            if (docIo.Document == null) return;
            docIo.Document.AddObject(vl, false, 1);
            vl.Attributes.Pivot = Attributes.Pivot;
            docIo.Document.SelectAll();
            docIo.Document.ExpireSolution();
            docIo.Document.MutateAllIds();
            var objs = docIo.Document.Objects;
            _doc.DeselectAll();
            _doc.UndoUtil.RecordAddObjectEvent("Create " + vl.NickName + " List", objs);
            _doc.MergeDocument(docIo.Document);
            recipient.AddSource(vl);
            _doc.DeselectAll();
            _doc.ScheduleSolution(1);
            _doc.SolutionStart -= DocOnSolutionStart;
            _doc.RemoveObject(this, true);
        }
        protected override System.Drawing.Bitmap Icon => Properties.Resources.List;
        public override Guid ComponentGuid => new Guid("875a0550-703d-43f3-819d-13f09315a05f");
    }
}

AutoValueList.gha (10 KB) (Grasshopper 0.9.76)
AutoValueList.zip (7.6 MB) (Source)

4 Likes

Hi Madiyar, This is brilliant. Would it be possible to upload your component to food4rhino.com as well so others can enjoy of this development. Thanks Manochehr