Table of Contents
How To: Pre-Pick and Post-Pick Objects
C++, .NET
Summary: Demonstrates how to both pre-pick and post-pick objects using a single CRhinoGetObject object.
The normal operation for commands that manipulate geometric objects is to allow the user to either pre-pick or post-pick the objects. Occasionaly, though, it might be necessary for commands to want to allow for both pre-picked and post-picked objects. That is, after it has been determined that objects were pre-picked, allow the user to continue to pos-pick more objects.
The following code sample demonstrates this capability.
C++
CRhinoCommand::result CCommandTest::RunCommand( const CRhinoCommandContext& context ) { bool bObjectsWerePreSelected = false; bool bObjectsWerePostSelected = false; ON_SimpleArray<const CRhinoObject*> obj_list; // Select some objects. Allow for pre-selected objects CRhinoGetObject go; go.SetCommandPrompt( L"Select objects" ); go.EnablePreSelect( TRUE ); go.GetObjects( 0, 0 ); if( go.CommandResult() != CRhinoCommand::success ) return go.CommandResult(); // Add the selected objects to our list int i; for( i = 0; i < go.ObjectCount(); i++ ) { const CRhinoObjRef& objref = go.Object(i); const CRhinoObject* obj = objref.Object(); if( obj ) obj_list.Append( obj ); } // Determine of the bjects were pre-selected // or post-selected. if( go.ObjectsWerePreSelected() ) bObjectsWerePreSelected = true; else bObjectsWerePostSelected = true; // If objects were pre-selected, then select // more objects. But, ignore pre-selected ones. if( bObjectsWerePreSelected ) { go.EnablePreSelect( FALSE ); go.EnableDeselectAllBeforePostSelect( FALSE ); go.GetObjects( 0, 0 ); if( go.CommandResult() != CRhinoCommand::success ) return go.CommandResult(); for( i = 0; i < go.ObjectCount(); i++ ) { const CRhinoObjRef& objref = go.Object(i); const CRhinoObject* obj = objref.Object(); if( obj ) obj_list.Append( obj ); } bObjectsWerePostSelected = true; } if( obj_list.Count() == 0 ) return CRhinoCommand::nothing; // // TODO: do something with the object list here // // The normal behavior of commands is that when they finish, // objects that were pre-selected remain selected and objects // that were post-selected will not be selected. Because we // potentially could have both, we'll try to do something // consistent. if( bObjectsWerePreSelected && bObjectsWerePreSelected ) { for( i = 0; i < obj_list.Count(); i++ ) { const CRhinoObject* obj = obj_list[i]; if( obj && obj->IsSelected() ) obj->Select( false ); } context.m_doc.Redraw(); } return CRhinoCommand::success; }
VB.NET
Public Overrides Function RunCommand(ByVal context As RMA.Rhino.IRhinoCommandContext)_ As RMA.Rhino.IRhinoCommand.result Dim bObjectsWerePreSelected As Boolean = False Dim bObjectsWerePostSelected As Boolean = False Dim obj_list As New System.Collections.Generic.List(Of IRhinoObject) ' Select some objects. Allow for pre-selected objects Dim go As New MRhinoGetObject() go.SetCommandPrompt("Select objects") go.EnablePreSelect(True) go.GetObjects(0, 0) If (go.CommandResult() <> IRhinoCommand.result.success) Then Return go.CommandResult() ' Add the selected objects to our list For i As Integer = 0 To go.ObjectCount() - 1 Dim objref As IRhinoObjRef = go.Object(i) Dim obj As IRhinoObject = objref.Object() If (obj IsNot Nothing) Then obj_list.Add(obj) Next i ' Determine of the bjects were pre-selected or post-selected. If (go.ObjectsWerePreSelected()) Then bObjectsWerePreSelected = True Else bObjectsWerePostSelected = True End If ' If objects were pre-selected, then select ' more objects. But, ignore pre-selected ones. If (bObjectsWerePreSelected) Then go.EnablePreSelect(False) go.EnableDeselectAllBeforePostSelect(False) go.GetObjects(0, 0) If (go.CommandResult() <> IRhinoCommand.result.success) Then Return go.CommandResult() For i As Integer = 0 To go.ObjectCount() - 1 Dim objref As IRhinoObjRef = go.Object(i) Dim obj As IRhinoObject = objref.Object() If (obj IsNot Nothing) Then obj_list.Add(obj) Next i bObjectsWerePostSelected = True End If If (obj_list.Count = 0) Then Return IRhinoCommand.result.nothing ' TODO: do something with the object list here ' The normal behavior of commands is that when they finish, ' objects that were pre-selected remain selected and objects ' that were post-selected will not be selected. Because we ' potentially could have both, we'll try to do something ' consistent. If (bObjectsWerePreSelected And bObjectsWerePreSelected) Then For Each obj As IRhinoObject In obj_list If (obj.IsSelected() > 0) Then obj.Select(False) Next context.m_doc.Redraw() End If Return IRhinoCommand.result.success End Function
C#
public override IRhinoCommand.result RunCommand(IRhinoCommandContext context) { bool bObjectsWerePreSelected = false; bool bObjectsWerePostSelected = false; System.Collections.Generic.List<IRhinoObject> obj_list = new System.Collections.Generic.List<IRhinoObject>(); // Select some objects. Allow for pre-selected objects MRhinoGetObject go = new MRhinoGetObject(); go.SetCommandPrompt("Select objects"); go.EnablePreSelect(true); go.GetObjects(0, 0); if(go.CommandResult() != IRhinoCommand.result.success) return go.CommandResult(); // Add the selected objects to our list for( int i=0; i<go.ObjectCount(); i++ ) { IRhinoObjRef objref = go.Object(i); IRhinoObject obj = objref.Object(); if(obj != null) obj_list.Add(obj); } // Determine of the bjects were pre-selected or post-selected. if(go.ObjectsWerePreSelected()) bObjectsWerePreSelected = true; else bObjectsWerePostSelected = true; // If objects were pre-selected, then select // more objects. But, ignore pre-selected ones. if(bObjectsWerePreSelected) { go.EnablePreSelect(false); go.EnableDeselectAllBeforePostSelect(false); go.GetObjects(0, 0); if(go.CommandResult() != IRhinoCommand.result.success) return go.CommandResult(); for( int i=0; i<go.ObjectCount(); i++ ) { IRhinoObjRef objref = go.Object(i); IRhinoObject obj = objref.Object(); if(obj != null) obj_list.Add(obj); } bObjectsWerePostSelected = true; } if(obj_list.Count == 0) return IRhinoCommand.result.nothing; // TODO: do something with the object list here // The normal behavior of commands is that when they finish, // objects that were pre-selected remain selected and objects // that were post-selected will not be selected. Because we // potentially could have both, we'll try to do something // consistent. if(bObjectsWerePreSelected) { foreach( IRhinoObject obj in obj_list ) { if( obj.IsSelected() > 0 ) obj.Select(); } context.m_doc.Redraw(); } return IRhinoCommand.result.success; }