Highlight elements from a linked document
It has long been a wish to select elements from a linked element. This wish seems to have been granted in the Revit 2023 Release API.
A new Selection function called SetReferences
has been added, allowing elements to be highlighted via reference. We don't often use references to highlight elements, but rather to set hosts, like placing hosted families or extracting element IDs from a ReferenceIntersector
or when selecting by picking.
So, if we provide the SetReferences
function with references from a linked document, will it work? In theory, yes, it should work. However, some extra work is required to capture such element references. Firstly, we need to understand that this function operates on the currently active document. This means that the references we provide must be in a format that the current document can recognize to highlight them in the current view.
Let's attempt to highlight a face from an element in a linked document in the following steps:
Click on a point over one of the faces in a linked document.
Then, pass this reference to
SetReferences
, and it will highlight the face from the linked document.Similarly, if you press Tab to cycle through line, face, and object, once you reach the object, select it to get the object reference.
var linkedFaceReference = UiDoc.Selection.PickObject(
Autodesk.Revit.UI.Selection.ObjectType.PointOnElement
);
UiDoc.Selection.SetReferences([linkedFaceReference]);
Now, this only happens when a user interacts with the UI. But what if I have an element ID from a linked document that I want to highlight? The real question then becomes, how can I extract a reference from an ElementId
that belongs to a linked document?
This is achievable, but not directly from the ElementId
; we need to work with the element itself. First, we need to get the element from the linked document, then create a reference for this element. However, we can't use this reference as it's only meaningful to the linked document, not the current one. Therefore, we must convert this reference to the current document using CreateLinkReference
and RevitLinkInstance
. It might sound confusing, but I've included the code below to demonstrate how it functions clearly.
var pickedReference = UiDoc.Selection.PickObject(
Autodesk.Revit.UI.Selection.ObjectType.PointOnElement
);
// get Revit link Instance and its document
var linkedRvtInstance = Doc.GetElement(pickedReference) as RevitLinkInstance;
var linkedDoc = linkedRvtInstance.GetLinkDocument();
//get the Linked element from the linked document
var linkedElement = linkedDoc.GetElement(pickedReference.LinkedElementId);
// now create a reference from this element [ this is a reference inside the linked document]
var reference = new Reference(linkedElement);
// convert the reference to be readable from the current document
reference = reference.CreateLinkReference(linkedRvtInstance);
// now the linked element is highlighted
UiDoc.Selection.SetReferences([reference]);
Subscribe to my newsletter
Read articles from Moustafa Khalil directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Moustafa Khalil
Moustafa Khalil
I'm Moustafa Khalil El-Sayed, an Egyptian native with a passion for architecture and technology. After completing high school in 1999, I embarked on my journey into architecture, earning my Bachelor's degree in Architecture in 2004. My academic pursuits continued, and I later acquired my Master's degree in Architecture from Wings University. However, my thirst for knowledge extended beyond architectural design, leading me to pursue a degree in Computer Science as well. Driven by a desire to innovate, I began self-teaching computer science, which later empowered me to achieve my Associate degree in Computer Science from the University of the People more easily. Armed with dual expertise in architecture and computer science, I've cultivated a unique skill set that blends creativity with technical acumen. My career has taken me across borders, from Egypt to Oman and the UAE, where I've honed my skills as an architect. As a graduate architect and a proficient .NET BIM Solution Developer, I specialize in translating unique visions into tangible realities. My role extends beyond conventional architectural practices; I leverage technology to streamline processes and enhance project outcomes. My journey into programming began with developing script-based Excel tools to augment project management efforts, notably during the Sharm El Sheikh development. Over time, I expanded my programming prowess to address practical challenges encountered in bridging Revit and AutoCAD within my professional sphere. Several of my applications, born from this evolution, have gained recognition, with some even being featured on the Autodesk website. I am committed to pushing boundaries and continually enriching the intersection of architecture and technology, ensuring that my clients benefit from cutting-edge solutions tailored to their needs.