Access/Retrieve Document of picked linked element in revit link instance.

Access/Retrieve Document of picked linked element in revit link instance.

mr.engineer.aec
Advocate Advocate
2,069 Views
5 Replies
Message 1 of 6

Access/Retrieve Document of picked linked element in revit link instance.

mr.engineer.aec
Advocate
Advocate

 Hi All,

I'm in host model and i selected beam in linked file.

I want to retrieve linked document of beam (to get location line). But wrong !

How can i solved this problem ?

Thank in advanced !

 

Reference pickObject = uidoc.Selection.PickObject(ObjectType.LinkedElement, "Pick element in linked file.");

//doc = document in host file.
beam = doc.GetElement(pickObject) as FamilyInstance; 

//Location
Line beamLine = ((beam.Location as LocationCurve).Curve as Line);

 

 

0 Likes
2,070 Views
5 Replies
Replies (5)
Message 2 of 6

joshua.lumley
Advocate
Advocate

I think this will work: 

 

LinkElementId myLinkElementId = pickObject.LinkedElementId as LinkElementId;
RevitLinkInstance linkedDoc = doc.GetElement(myLinkElementId.LinkInstanceId) as RevitLinkInstance;
0 Likes
Message 3 of 6

mr.engineer.aec
Advocate
Advocate

 Thank for your relpy. I'll trying

0 Likes
Message 4 of 6

AmitMetz
Explorer
Explorer

pickedObject.ElementId return the RevitLinkInstance Id (What we need)

pickedObject.LinkedElementId return the Element Id

 

//Get UIDocument and Document
UIDocument uidoc = ActiveUIDocument;
Document doc = uidoc.Document;

//Define Link Document
Document linkDoc = null;

//Select element in link
Reference pickObject = uidoc.Selection.PickObject(ObjectType.LinkedElement,"Pick element in link");
if (pickObject != null)
{
	RevitLinkInstance link = doc.GetElement(pickObject.ElementId) as RevitLinkInstance;
	linkDoc = link.GetLinkDocument();
}

 

 

0 Likes
Message 5 of 6

jvideiraGZHPC
Participant
Participant

why those suggestions don't work if the Reference is a PlanarFace and not an object picked by the user?
How can I make it work

0 Likes
Message 6 of 6

davidarh
Explorer
Explorer

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