Search code examples
c#revit-api

Get selected elements in linked revit document using revit api


I am trying to get selection to work from a linked document without a result:

Document linkedDoc = testLinkedElement.GetLinkDocument();
UIDocument linkedUIDoc = new UIDocument(linkedDoc);
// Get the selection object from the UIDocument
Selection selection = linkedUIDoc.Selection;

// Get the selected elements
ICollection<ElementId> selectedElementIds = selection.GetElementIds();

I get an error when I try to instantiate the UIDocument from the linked document object If I use the active UIDocument I have no access to the elements in the linked documents.

Any suggestions ?

I am trying to get selection to work from a linked document without a result I am assuming that the selectedElementIds will be filled with selected elements


Solution

  • Here is a sample of Getting the element id from a linked element, you can try to modify this according to your needs and get the data you want from the Element.

    public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
     {
         try
         {
             Reference selectedObj;
             UIDocument uidoc = commandData.Application.ActiveUIDocument;
             Document doc = uidoc.Document;
             Selection sel = uidoc.Selection;
             selectedObj = sel.PickObject(ObjectType.LinkedElement, "Select Linked Element");
             RevitLinkInstance linkInstance = doc.GetElement(selectedObj) as RevitLinkInstance;
             Document linkedDoc = linkInstance.GetLinkDocument();
             ElementId xx = linkedDoc.GetElement(selectedObj.LinkedElementId).Id;
         }
         catch (Exception)
         {
            return Result.Failed;
         }
         return Result.Succeeded;
     }