Select multi element by Revit API same use Ctrl in Revit

Select multi element by Revit API same use Ctrl in Revit

peerawit.ru
Enthusiast Enthusiast
1,063 Views
6 Replies
Message 1 of 7

Select multi element by Revit API same use Ctrl in Revit

peerawit.ru
Enthusiast
Enthusiast

Hi everyone

I want to know Revit API can select multi element same select in Revit(use Ctrl and click one by one).

If I use PickObjects() It can't sort element order as I needed , I want to sort element by selection order. 

 

If Revit API can to this element in list from select sort by selection order or element id 

Thank for your recommend.

 

0 Likes
Accepted solutions (1)
1,064 Views
6 Replies
Replies (6)
Message 2 of 7

jeremy_tammik
Alumni
Alumni
Accepted solution

Hmm. PickObjects returns a List, so the result could potentially be sorted.

  

If you prefer a different approach with total control, you could call PickObject in a loop and select the elements one by one, keeping track of the selection order yourself.

   

Jeremy Tammik Developer Advocacy and Support + The Building Coder + Autodesk Developer Network + ADN Open
Message 3 of 7

peerawit.ru
Enthusiast
Enthusiast

Thank you very mush 

I have a question if i use while loop for select object .

Can i highlight selected and how?😅

 

 

 

 

0 Likes
Message 4 of 7

jeremy_tammik
Alumni
Alumni

You can highlight an element on the Revit graphics screen by adding it to the UIDocument Selection property using the Selection.SetElementIds method:

  

  

For example usage, look at the DirectionCalculation SDK sample.

  

Jeremy Tammik Developer Advocacy and Support + The Building Coder + Autodesk Developer Network + ADN Open
Message 5 of 7

rhanzlick
Advocate
Advocate

It was my understanding that even if you use PickObjects(), that the UIDocument.Selection property to which @jeremy_tammik referred, already stores the items in the order in which they were selected (although not sure how it sorts selection via window select). Otherwise, I have had good luck using a while loop and appending to a list of ElementIds.

Message 6 of 7

peerawit.ru
Enthusiast
Enthusiast

When i use Selection.SetElementIds it work when end loop .

My target is show to user see what element are selected when while loop is running

 

bool flag = true;
IList<ElementId> idList = new List<ElementId>();
while (flag)
{
  try
  {
    Reference element = Uidoc.Selection.PickObject(ObjectType.Element);
    ElementId elementId = element.ElementId;
    if (!idList.Contains(elementId))
    {
      idList.Add(elementId);
      Uidoc.Selection.SetElementIds(idList);
    }
    else
    {
      idList.Remove(elementId);
      Uidoc.Selection.SetElementIds(idList);
    }
  }
  catch
  {
    flag = false;
    break;
  }
}

 Where did i miss it?

 

Thank you 

0 Likes
Message 7 of 7

rhanzlick
Advocate
Advocate

Not sure of your specific use case, but it looks good to me.

 

Are you trying to show these Ids in a modeless dialog? If so, just make sure your data binding has no bugs.

Are you just trying to highlight them in the UI? If so, Revit's out-of-the-box Selection process works pretty well, and the "Inquiry" Panel on the "Manage" tab has an "Ids of Selection" button.

 

Additionally, you could try using UIDocument.RefreshActiveView() or UIDocument.UpdateAllOpenViews() methods to force the UI update within your loop. We've had good luck with this when making selection changes within Modal dialogs.

 

Finally, in Revit 2023 and later, you can avoid the whole loop and subscribe to the Selection Changed event, retrieving the elements that way.

 

Again, more detail about your goal would be useful, but hopefully, these suggestions help. Let us know how it works out.

 

Let us know how this turns out.