Hi @adrian_ortega32GAWA ,
1. To filter elements from a linked document, you need to pass the linkedDoc object (of type Document) in the FilteredElementCollector.
2. You can use uidoc.Selection.SetReferences() to highlight elements present in the linked document. I believe this functionality was added in Revit 2023.
UIApplication uiapp = commandData.Application;
UIDocument uidoc = uiapp.ActiveUIDocument;
Document doc = uidoc.Document;
// Get the first linked Revit document
RevitLinkInstance linkInstance = new FilteredElementCollector(doc).OfClass(typeof(RevitLinkInstance))
.WhereElementIsNotElementType().FirstElement() as RevitLinkInstance;
Document linkedDoc = linkInstance?.GetLinkDocument();
// Get all wall elements from the linked document
var walls = new FilteredElementCollector(linkedDoc)
.OfCategory(BuiltInCategory.OST_Walls).WhereElementIsNotElementType();
// Create linked references for selection
var references = walls
.Cast<Element>()
.Select(e => new Reference(e).CreateLinkReference(linkInstance)).ToList();
// Highlight the linked wall elements in the UI
uidoc.Selection.SetReferences(references);
3. You can use an intersection filter to determine whether elements in the main document and the linked document intersect. This below sample code works by selecting an element in the linked document
and detecting all elements in the host document it intersects with. (Please note, this sample code is very simple and can be further enhanced and customized by implementing your own logic; This is just for your reference;)
UIApplication uiapp = commandData.Application;
UIDocument uidoc = uiapp.ActiveUIDocument;
Document doc = uidoc.Document;
// Get the first Revit link instance in the current document
var linkInst = new FilteredElementCollector(doc)
.OfClass(typeof(RevitLinkInstance))
.WhereElementIsNotElementType()
.FirstOrDefault() as RevitLinkInstance;
Document linkedDoc = linkInst.GetLinkDocument();
// Let user select one element from the linked model
Element linkedElement = linkedDoc.GetElement(uidoc.Selection.PickObject(ObjectType.LinkedElement, "Linked").LinkedElementId);
// Extract solid geometry from the linked element
Options geomOptions = new Options { ComputeReferences = true, View = doc.ActiveView, IncludeNonVisibleObjects = true };
Solid linkedSolid = (linkedElement.get_Geometry(geomOptions)?.
OfType<Solid>().FirstOrDefault());
// Transform linked solid into host document coordinates
Solid transformedSolid = SolidUtils.CreateTransformed(linkedSolid, linkInst.GetTransform());
// Find elements in the main model that intersect with the selected linked element from the linked model.
var intersectingElements = new FilteredElementCollector(doc)
.WherePasses(new ElementIntersectsSolidFilter(transformedSolid))
.WhereElementIsNotElementType();
Naveen Kumar T
Developer Technical Services
Autodesk Developer Network