Question

I have two revit projects, MainProject.rvt and ProjectToLink.rvt. MainProject.rvt contains a link to ProjectToLink.rvt. Using the code below while in the MainProject.rvt document I can access the link itself:

FilteredElementCollector linkedModelCollector = new FilteredElementCollector(document);
linkedModelCollector.OfCategory(BuiltInCategory.OST_RvtLinks);

foreach (Element linkedModel in linkedModelCollector)
{
   //Do something
}

How can I access the elements inside of the linked model ProjectToLink.rvt from MainProject.rvt? Can I use the link element itself like in the code above or is there another mechanism?

PS - I need this to work for Revit 2011

Was it helpful?

Solution

In Revit 2012 I use following functions:

public IEnumerable<ExternalFileReference> GetLinkedFileReferences()
        {
            //ElementFilter categoryFilter = new ElementCategoryFilter(BuiltInCategory.OST_RvtLinks);
            //ElementFilter typeFilter = new ElementClassFilter(typeof(Instance));
            //ElementFilter logicalFilter = new LogicalAndFilter(categoryFilter, typeFilter);

            var collector = new FilteredElementCollector(_document);
            var linkedElements = collector
                .OfClass(typeof (RevitLinkType))
                //.OfCategory(BuiltInCategory.OST_RvtLinks)
                //.WherePasses(logicalFilter)
                .Select(x => x.GetExternalFileReference())
                .ToList();

            return linkedElements;                       
        }

and

public IEnumerable<Document> GetLinkedDocuments()
        {
            var linkedfiles = GetLinkedFileReferences();
            //List<String> linkedFileName = new List<string>(linkedfiles.Count);            

            var linkedFileNames = linkedfiles                
                .Select(x => ModelPathUtils.ConvertModelPathToUserVisiblePath(x.GetAbsolutePath()))                
                .ToList();

            //linkedFileName.AddRange
            //    (from linkedfile in linkedfiles 
            //     select linkedfile.GetTypeId() into typeId 
            //     where typeId != null 
            //     select document.get_Element(typeId).Name);

            return _document.Application.Documents
                .Cast<Document>()
                .Where(doc => linkedFileNames
                    .Any(fileName => doc.PathName.Equals(fileName)));
        }

the first one gets links to linked files (as in your code), and the second one - get dociments that represent linked files. So, when you have a Document you can get any element from it via FilteredElementCollector. But remember you cannot change elements in linked files.

Try to find similar functions in Revit 2011 API. And read this article

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top