TigAachen
TigAachen

Reputation: 13

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

Upvotes: 1

Views: 1383

Answers (1)

Mostafa Tarek Yassien
Mostafa Tarek Yassien

Reputation: 532

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;
 }

Upvotes: 0

Related Questions