go.GetSelectedEnumValue<>() fails


#1

Hi,

I’m trying to create a small command, to calculate weight from Volume

I created an input to selectively let the user choose either a “Standard Material” or let them set a density Value

Selection options work fine - however I have trouble selecting the chosen Value using
go.GetSelectedEnumValue() just before the case switch.

I also had a problem in the loop before, but was able to catch it by filtering the OptionIndex
The command just breaks.

Attached please find the command sample as C# Code

public enum Material

        {
            Steel,
            Aluminum,
            StainlessSteel,
            Other
        };

        protected override Result RunCommand(RhinoDoc doc, RunMode mode)
        {
            // TODO: complete command.
            ObjRef[] objs = null;

            Rhino.Input.Custom.GetObject go = new Rhino.Input.Custom.GetObject();
            go.SetCommandPrompt("Get Solids To Calculate");

            go.SubObjectSelect = false;
            go.AcceptNothing(true);
            go.GeometryFilter = Rhino.DocObjects.ObjectType.Brep & Rhino.DocObjects.ObjectType.Extrusion;
            go.GroupSelect = true;
                      
            
            OptionDouble densityOption = new OptionDouble(7700, true, 0);
            go.AddOptionEnumList("Material", Material.Steel);
            

            while (true)
            {
                // perform the get operation. This will prompt the user to input a point, but also
                // allow for command line options defined above
                Rhino.Input.GetResult get_rc = go.GetMultiple(0, 0);
                if (go.CommandResult() != Rhino.Commands.Result.Success)
                {
                    return go.CommandResult();
                }

                if (get_rc == Rhino.Input.GetResult.Object)
                {
                    objs = go.Objects();
                }
                else if (get_rc == Rhino.Input.GetResult.Option)
                {
                    int optionIndex = go.OptionIndex();
                    if (optionIndex == 1)
                    {
                        Material currentMaterial = go.GetSelectedEnumValue<Material>();
                        if (currentMaterial == Material.Other)
                        {
                            go.ClearCommandOptions();
                            go.AddOptionEnumList("Material", Material.Other);
                            go.AddOptionDouble("MaterialDensity", ref densityOption, "Material Density in kg/cm³");
                        }
                        else
                        {
                            go.ClearCommandOptions();
                            go.AddOptionEnumList("Material", currentMaterial);
                        }
                    }                    
                    continue;
                }
                break;
            }

            double density;            

            Material selectedMaterial  = go.GetSelectedEnumValue<Material>();
            switch (selectedMaterial)
            {
                case Material.Aluminum:
                    density = 444;
                    break;

                case Material.Steel:
                    density = 444;
                    break;

                case Material.StainlessSteel:
                    density = 444;
                    break;

                case Material.Other:
                    density = densityOption.CurrentValue;
                    break;
            }     

            return Result.Success;
        }

SelectionOptions.zip (1.3 KB)


(Menno Deij - van Rijswijk) #2

Code below works. Instead of getting the selected material after exiting the while(true) loop, immediately store the selection in the currentMaterial variable.
Then use that after the loop is exited. I’m not exactly sure why the error occurs, but this approach works :smile:

Material currentMaterial = Material.Steel;
go.AddOptionEnumList("Material", currentMaterial);

            
while (true)
{
    // perform the get operation. This will prompt the user to input a point, but also
    // allow for command line options defined above
    Rhino.Input.GetResult get_rc = go.GetMultiple(0, 0);
    if (go.CommandResult() != Rhino.Commands.Result.Success)
    {
        return go.CommandResult();
    }

    if (get_rc == Rhino.Input.GetResult.Object)
    {
        objs = go.Objects();
    }
    else if (get_rc == Rhino.Input.GetResult.Option)
    {
        int optionIndex = go.OptionIndex();
        if (optionIndex == 1)
        {
            currentMaterial = go.GetSelectedEnumValue<Material>();
            if (currentMaterial == Material.Other)
            {
                go.ClearCommandOptions();
                go.AddOptionEnumList("Material", Material.Other);
                go.AddOptionDouble("MaterialDensity", ref densityOption, "Material Density in kg/cm³");
            }
            else
            {
                go.ClearCommandOptions();
                go.AddOptionEnumList("Material", currentMaterial);
            }
        }
        continue;
    }
    break;
}