Return Revit Model linked to a project

Return Revit Model linked to a project

pmcmm1
Enthusiast Enthusiast
664 Views
3 Replies
Message 1 of 4

Return Revit Model linked to a project

pmcmm1
Enthusiast
Enthusiast

Dear colleagues,

 

I'd be surprised if this question hasn't been answered yet in this forum. Yet, I wasn't able to find the exact answer to what I'm looking for. So, in first place, excuse the redundancy if that's the case.

 

I want to obtain the linked revit models that are linked to an open document. I've used the following line

DocumentSet docs = commandData.Application.ActiveUIDocument.Document.Application.Documents;

to retrieve a DocumentSet containing all the documents open in revit. Then I iterated through the documents and used the Property doc.IsLinked to check if that document is linked.

 

However, if I have two models open in revit and both have linked models, my code will return the linked models of both files. How can I make sure that only linked files from my document get returned?

0 Likes
Accepted solutions (1)
665 Views
3 Replies
Replies (3)
Message 2 of 4

jeremytammik
Autodesk
Autodesk
Accepted solution

Install RevitLookup. Look at the database contents. In the document containing links, you will see RevitLinkInstance entries representing the linked files. You can retrieve all those entries using a filtered element collector. 

 



Jeremy Tammik
Developer Technical Services
Autodesk Developer Network, ADN Open
The Building Coder

Message 3 of 4

pmcmm1
Enthusiast
Enthusiast

Thank you once again Jeremy. For reference, this is the little function I wrote to retrieve all linked documents

 

public static List<Document> GetLinkedDocuments(Document doc)
        {
            var fc = new FilteredElementCollector(doc)
                .OfCategory(BuiltInCategory.OST_RvtLinks)
                .WhereElementIsNotElementType();

            var list_docs = new List<Document>();
            foreach (var elem in fc)
            {
                RevitLinkInstance link = (RevitLinkInstance)elem;
                Document link_doc = link.GetLinkDocument();
                list_docs.Add(link_doc);
            }

            return list_docs;

 

 

Message 4 of 4

jeremytammik
Autodesk
Autodesk

Cool. 

 

Thank you for your appreciation.

 

Here is a slightly more succinct and efficient version that does not construct a new List object:

 

    IEnumerable<Document> GetLinkedDocuments(
      Document doc )
    {
      return new FilteredElementCollector( doc )
        .OfCategory( BuiltInCategory.OST_RvtLinks )
        .WhereElementIsNotElementType()
        .Cast<RevitLinkInstance>()
        .Select<RevitLinkInstance, Document>( 
          link => link.GetLinkDocument() );
    }

  

I also added it to The Building Coder samples for future reference.

  

Can you confirm that it works as well, please?

  

Thank you!

 

Cheers,

 

Jeremy

 



Jeremy Tammik
Developer Technical Services
Autodesk Developer Network, ADN Open
The Building Coder