Hi, I just had a similar problem and ended up combining code from different places. Please see the Macro below that goes though the whole process of selecting an object of a specific type, getting the linked document and getting the element in the linked document as well.
Hope this helps someone!
using System;
using Autodesk.Revit.UI;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI.Selection;
using System.Collections.Generic;
using System.Linq;
namespace PickLinkedElement
{
[Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)]
[Autodesk.Revit.DB.Macros.AddInId("5EC5C348-E15F-478E-9E88-6876CBE9D21F")]
public partial class ThisDocument
{
private void Module_Startup(object sender, EventArgs e)
{
}
private void Module_Shutdown(object sender, EventArgs e)
{
}
#region Revit Macros generated code
private void InternalStartup()
{
this.Startup += new System.EventHandler(Module_Startup);
this.Shutdown += new System.EventHandler(Module_Shutdown);
}
#endregion
public void SelectWindow()
{
var uiDoc = this.Application.ActiveUIDocument;
var doc = this.Document;
Document linkedDoc = null;
Reference pickedObject = uiDoc.Selection.PickObject(ObjectType.LinkedElement, new GroupPickFilterWindow(), "Please pick a window in the linked model");
//Add code to get linked document here.
if (pickedObject != null)
{
var linkedEleemnt = doc.GetElement(pickedObject.ElementId) as RevitLinkInstance;
linkedDoc = linkedEleemnt.GetLinkDocument();
var linkElement = linkedDoc.GetElement( pickedObject.LinkedElementId);
TaskDialog.Show("element", linkElement.Name + " - " + linkElement.GetType().ToString() + " - " + linkedDoc.PathName);
}
}
}
// This filter allows selection of only a certain category in a link instance.
public class GroupPickFilterWindow : ISelectionFilter
{
private RevitLinkInstance m_currentInstance = null;
public bool AllowElement(Element e)
{
// Accept any link instance, and save the handle for use in AllowReference()
m_currentInstance = e as RevitLinkInstance;
return (m_currentInstance != null);
}
public bool AllowReference(Reference refer, XYZ point)
{
if (m_currentInstance == null)
return false;
// Get the handle to the element in the link
Document linkedDoc = m_currentInstance.GetLinkDocument();
Element e = linkedDoc.GetElement(refer.LinkedElementId);
// Accept the selection if the element exists and is of the correct type
if (e.Category != null && e.Category.Id.IntegerValue.Equals((int)BuiltInCategory.OST_Windows))
{
return true;
}
else
{
return false;
}
}
}
}