Grasshopper

algorithmic modeling for Rhino

Populating the AddNamedValue list of an input parameter depending on the user's input of the previous input parameter


Hay guys,
Been struggeling with this one for a while and hope someone could help out.
I have a C# component with two input parameters, both as set Param_Integer.
For the first, I have a list of predefined options for the user, say 1,2 and 3. For the second, I want the choice list to be dependent on the input of the first.
Here is an example of my code..How can I get the code to run ie access SolveInstance to get the value of the first input param, before running the full solution? I set the second parameter as option to force a dummy run, but still couldn't get it to work.


public class myclass{

//public declarations:

public int firstparamvalue = 0;
public static List<string> secondparamList = new List<string>();
public Grasshopper.Kernel.Parameters.Param_Integer secondParam;

//in Register Input params:

pManager.AddIntegerParameter("first", "first", "first", GH_ParamAccess.item);

Grasshopper.Kernel.Parameters.Param_Integer firstParam = pManager[0] as Grasshopper.Kernel.Parameters.Param_Integer;
firstParam.AddNamedValue("one", 1);
firstParam.AddNamedValue("two", 2);
firstParam.AddNamedValue("three", 3);

pManager.AddIntegerParameter("second", "second", "second", GH_ParamAccess.item);
secondParam = pManager[1] as Grasshopper.Kernel.Parameters.Param_Integer;
pManager[1].Optional = true;

for (int i = 0; i < secondparamList.Count; i++)
{
secondparam.AddNamedValue(secondparamList[i], i + 1);
}

//in Solve instance:

secondparamList.clear();


if (!DA.GetData(0, ref firstparamvalue))
{
return;
}

//loop through some XML, get all the elements with tag = "firstparamvalue", and use them to populate the list "secondparamlist"


int secondparamvalue = 0;
if (!DA.GetData(1, ref secondparamvalue))
{
return;
}

}

Views: 1411

Replies to This Discussion

Hi Ahmed,

I don't get it. As soon as someone sets the first parameter a new solution runs anyway. Why not override BeforeSolveInstance(), look at the GH_Structure<GH_Integer> inside the first input parameter and then set the named values on the second?

protected override void BeforeSolveInstance()
{
Param_Integer param0 = Params.Input[0] as Param_Integer;
Param_Integer param1 = Params.Input[1] as Param_Integer;
param1.ClearNamedValues();

GH_Structure<GH_Integer> data = param0.VolatileData as GH_Structure<GH_Integer>;
if (data.IsEmpty) return;
foreach (GH_Integer value in data.AllData(true))
{
switch (value.Value)
{
case 1:
param1.AddNamedValue("First option for 1", 11);
param1.AddNamedValue("Second option for 1", 12);
param1.AddNamedValue("Third option for 1", 13);
break;

case 2:
param1.AddNamedValue("First option for 2", 21);
param1.AddNamedValue("Second option for 2", 22);
param1.AddNamedValue("Third option for 2", 23);
break;

case 3:
param1.AddNamedValue("First option for 3", 31);
param1.AddNamedValue("Second option for 3", 32);
param1.AddNamedValue("Third option for 3", 33);
break;
}
return;
}
}

--

David Rutten

david@mcneel.com

Hay David,

many thanks for your reply.

This seems to be working well, overriding the BeforeSolveInstance() did the job. However, I am also adding AddNamedValue to input[0] i.e. it too has a list. This doesn't seem to work, even though I assigned 1,2,3 to the strings... It only works if I explicitly connect an integer to it. Any thoughts?

Is that parameter optional?

--

David Rutten

david@mcneel.com

Yes it is.

Input[0] is required, while Input[1] is optional.

I tried making both required but no luck. Is it because VolatileData is only accessing the param_integer's explicit input and not its "named value list"?

The named values are just UI sugar. The only places where they are used are in the menu for the parameter (instead of 'Set one integer' there's now menu items for all the named values) and the tooltip of the parameter (instead of the integer it shows the associated name).

Other than that the parameter logic remains unchanged.

Typically you assign named values during RegisterInputParams(). You should then be able to open the menu and select a value. If you want a default value then you'll also need to assign that inside RegisterInputParams. The AddIntegerParameter() method has overloads that allow you to set a default value.

I don't think I understand your exact problem though.

--

David Rutten

david@mcneel.com

Finally got it. Problem was having nested named value lists one depending on the other and took some time to figure out. Code works with switch case as you suggested.

Many thanks!

RSS

About

Translate

Search

Videos

  • Add Videos
  • View All

© 2024   Created by Scott Davidson.   Powered by

Badges  |  Report an Issue  |  Terms of Service