Maintain selection after command is complete

Maintain selection after command is complete

mdolanVQYAA
Enthusiast Enthusiast
386 Views
3 Replies
Message 1 of 4

Maintain selection after command is complete

mdolanVQYAA
Enthusiast
Enthusiast

I have commands that ask the user to select objects.  Nothing fancy just like below.  After the command does what it needs to do, the objects are unselected.  What is causing the unselecting of the objects?  Is there a way to maintain the selection?

Ideally, the objects would stay selected because there are subsequent commands that will be run that will require the same objects to be selected again.

 

        // Request for objects to be selected in the drawing area
        PromptSelectionResult acSSPrompt = doc.Editor.GetSelection();

        // If the prompt status is OK, objects were selected
        if (acSSPrompt.Status == PromptStatus.OK)
        {
            SelectionSet acSSet = acSSPrompt.Value;

            // Step through the objects in the selection set
            foreach (SelectedObject acSSObj in acSSet)
            {
                // Do something here.
            }
        }
        tr.Commit();

 

0 Likes
Accepted solutions (1)
387 Views
3 Replies
Replies (3)
Message 2 of 4

ActivistInvestor
Mentor
Mentor
Accepted solution

You add CommandFlags.Redraw to your CommandMethod attribute, and then call the Editor's SetImpliedSelection() method to specify the objects to be left selected after the command ends.

[CommandMethod("MYCOMMAND", CommandFlags.UsePickSet | CommandFlags.Redraw)]
public static void MyCommand()
{
   var doc = Application.DocumentManager.MdiActiveDocument;
   
   PromptSelectionResult result = doc.Editor.GetSelection();

   if(result.Status == PromptStatus.OK)
   {
      SelectionSet acSSet = result.Value;

      // Step through the objects in the selection set
      foreach(SelectedObject acSSObj in acSSet)
      {
         // Do something here.
      }
      
      doc.Editor.SetImpliedSelection(acSSet.GetObjectIds());
   }
}

 

Another tip:

 

Don't iterate over a selection set's SelectedObjects if you don't need them. In most cases, you only need the ObjectIds of the selected entities, which you can get by calling SelectionSet.GetObjectIds(), and you can iterate over those instead. The SelectedObject class that's enumerated by the SelectionSet class has some overhead, because it exposes not only the objectId of the selected object, but also selection metadata (e.g., how the object was selected, and selection parameters, such as the pick point, window/crossing polygon vertices, and so on). If you don't need that information, then you can avoid the overhead of marshaling it to manged code by just iterating over the ObjectIds instead.

 

So, instead of:

foreach(SelectedObject acSSObj in acSSet)
{
   // Do something here.
}

 

It is less-costly to do this:

foreach(ObjectId id in acSSet.GetObjectIds())
{
   // Do something here.
}

 

Message 3 of 4

Ed__Jobe
Mentor
Mentor

That’s just the way AutoCAD works. When you run a command, the selection is unhighlighted. However you still have the selection set. Just run your code on the entities in the selection set. 

Ed


Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
How to post your code.

EESignature

0 Likes
Message 4 of 4

mdolanVQYAA
Enthusiast
Enthusiast

ActivistInvestor, CommandFlags.UsePickSet was exactly what I was looking for.  Worked like a charm.

0 Likes