Saving commands

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]];

I would like to persist metalIndex.

Here’s how I do this in python using the sticky dictionary:

import scriptcontext as sc

unique_string = str(ghenv.Component.Attributes.InstanceGuid)
LOCAL_KEY_1 = unique_string + "_KEY_1"
LOCAL_KEY_2 = unique_string + "_KEY_2"
GLOBAL_KEY_1 = "GLOBAL_KEY_1"
GLOBAL_KEY_2 = "GLOBAL_KEY_2"

def get_sticky(key, default_value=None):
    return sc.sticky[key] if sc.sticky.has_key(key) else default_value

def set_sticky(key, value):
    sc.sticky[key] = value

maybe write it to the document’s user strings

Would you show me more examples of how to access and write to the StringTable? Do I need to instantiate it or does it exist somewhere?

I am writing a C# plugin not a Python script so I don’t think this will help.

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;
    }
}
1 Like

Oh of course! That is such an elegant solution. By moving the variable out of the RunCommand method it persists!