Object Selection with Options

GetObject

RhinoCommon’s GetObject class has a few properties and methods that you need to use, including:

Also, after clicking a command line option, turn off pre-selection, using GetObject.EnablePreSelect. Otherwise, GetObject.GetMultiple will return with a GetResult.Object return code.

For example:

using System;
using Rhino;
using Rhino.Commands;
using Rhino.DocObjects;
using Rhino.Input;
using Rhino.Input.Custom;
...
protected override Result RunCommand(RhinoDoc doc, RunMode mode)
{
const ObjectType geometryFilter = ObjectType.Surface | ObjectType.PolysrfFilter | ObjectType.Mesh;
int integer1 = 300;
int integer2 = 300;
OptionInteger optionInteger1 = new OptionInteger(integer1, 200, 900);
OptionInteger optionInteger2 = new OptionInteger(integer2, 200, 900);
GetObject go = new GetObject();
go.SetCommandPrompt("Select surfaces, polysurfaces, or meshes");
go.GeometryFilter = geometryFilter;
go.AddOptionInteger("Option1", ref optionInteger1);
go.AddOptionInteger("Option2", ref optionInteger2);
go.GroupSelect = true;
go.SubObjectSelect = false;
go.EnableClearObjectsOnEntry(false);
go.EnableUnselectObjectsOnExit(false);
go.DeselectAllBeforePostSelect = false;
bool bHavePreselectedObjects = false;
for (;;)
{
GetResult res = go.GetMultiple(1, 0);
if (res == GetResult.Option)
{
go.EnablePreSelect(false, true);
continue;
}
else if (res != GetResult.Object)
return Result.Cancel;
if (go.ObjectsWerePreselected)
{
bHavePreselectedObjects = true;
go.EnablePreSelect(false, true);
continue;
}
break;
}
if (bHavePreselectedObjects)
{
// Normally, pre-selected objects will remain selected, when a
// command finishes, and post-selected objects will be unselected.
// This this way of picking, it is possible to have a combination
// of pre-selected and post-selected. So, to make sure everything
// "looks the same", lets unselect everything before finishing
// the command.
for (int i = 0; i < go.ObjectCount; i++)
{
RhinoObject rhinoObject = go.Object(i).Object();
if (null != rhinoObject)
rhinoObject.Select(false);
}
doc.Views.Redraw();
}
int objectCount = go.ObjectCount;
integer1 = optionInteger1.CurrentValue;
integer2 = optionInteger2.CurrentValue;
RhinoApp.WriteLine(string.Format("Select object count = {0}", objectCount));
RhinoApp.WriteLine(string.Format("Value of integer1 = {0}", integer1));
RhinoApp.WriteLine(string.Format("Value of integer2 = {0}", integer2));
return Result.Success;
}