Select elements by ElementID

Select elements by ElementID

sakwet
Enthusiast Enthusiast
7,370 Views
4 Replies
Message 1 of 5

Select elements by ElementID

sakwet
Enthusiast
Enthusiast

I have a list of ElementIDs in the clipboard, how do I select the relevant elements in Revit by using the API? A function similar to 'Select by ID' under inquiry in Manage tab.

I tired using Document.GetElement() method, but could not succeed.

 

0 Likes
7,371 Views
4 Replies
Replies (4)
Message 2 of 5

naveen.kumar.t
Autodesk Support
Autodesk Support

Hi @sakwet ,

There are 2 ways to achieve what you want.

try using the below codes

//USING POSTABLE COMMAND
                RevitCommandId ID = RevitCommandId.LookupPostableCommandId(PostableCommand.SelectById);
                commandData.Application.PostCommand(ID);

//HIGHLIGHTELEMENTS FilteredElementCollector collector = new FilteredElementCollector(doc).OfClass(typeof(Wall)).WhereElementIsNotElementType(); IList<ElementId> list_elementid = collector.ToElementIds() as IList<ElementId>; uidoc.Selection.SetElementIds(list_elementid);

Naveen Kumar T
Developer Technical Services
Autodesk Developer Network

Message 3 of 5

john_dalessandro
Contributor
Contributor

You can use this function to get the text from the clipboard, then assuming your element ids are comma separated, do something like:

UIDocument.Selection.SetElementIds(
    Clipboard.GetText().Split(',').Select(s => new ElementId(Int32.Parse(s)).ToArray());

We split the text into an array of strings, each of which we parse into an int and make into an ElementId. We make the IEnumerable into a collection by calling .ToArray() and pass that into SetElementIds. You don't even need the elements themselves.

0 Likes
Message 4 of 5

sakwet
Enthusiast
Enthusiast

@john_dalessandro I got an error
Error CS1061 'ElementId' does not contain a definition for 'ToArray' and no accessible extension method 'ToArray' accepting a first argument of type 'ElementId' could be found 

0 Likes
Message 5 of 5

BIM.Frankliang
Collaborator
Collaborator

Dear Friend,

 

        John lost a ")" , the codes should be :

var uidoc = commandData.Application.ActiveUIDocument;
uidoc.Selection.SetElementIds(Clipboard.GetText().Split(',').Select(s => new ElementId(int.Parse(s))).ToArray());

Best Regards

Frank Liang

TJAD BIM Software Dev Lead

 

0 Likes