I have a command that takes a value “metals” from an Option List. I would like the index to persist. So if I set the index one time, it would remember the choice.
Here is a sample of the code
var go = new GetOption();
var go = new GetOption();
int metalIndex = 2;
int opList = go.AddOptionList("Metals", Densities.metals, metalIndex);
go.AcceptNothing(true);
go.SetCommandPrompt("Please select a material.");
GetResult res = go.Get();
if (res == GetResult.Option)
{
var option = go.Option();
metalIndex = go.Option().CurrentListOptionIndex;
}
//caluclate density
Rhino.RhinoApp.WriteLine(" selection = {0}", Densities.metals[metalIndex]);
double density = Densities.metalDensitites[Densities.metals[metalIndex]];
To make the _selectedIndex persist, you need to store it as a class-level variable so it retains its value between command executions. Here’s a clear and simplified example:
public class RetrieveMaterialDensity : Command
{
private int _selectedIndex;
private string[] _materials = new[] { "Aluminum", "Brass", "Gold" };
private double[] _densities = new[] { 2.7, 8.4, 19.32 };
public override string EnglishName => "GetMaterialDensity";
protected override Result RunCommand(Rhino.RhinoDoc doc, RunMode mode)
{
using (var go = new GetOption())
{
go.SetCommandPrompt("Select a material to get its density:");
var materialOptionIndex = go.AddOptionList("Materials", _materials, _selectedIndex);
go.AcceptNothing(true);
var result = go.Get();
if (result == GetResult.Option && go.OptionIndex() == materialOptionIndex)
{
_selectedIndex = go.Option().CurrentListOptionIndex;
Rhino.RhinoApp.WriteLine("The density of {0} is {1} g/cm³.", _materials[_selectedIndex], _densities[_selectedIndex]);
}
else if (result != GetResult.Nothing)
Rhino.RhinoApp.WriteLine("No valid material was selected.");
}
return Result.Success;
}
}