How can we access data from a Linked File. For example, for a certain requirement, how can we use the Revit API to access the Level information of all the Wall inside of a Linked file.
To meet this requirement, we can get a list of all the active documents loaded in the current Revit session using Application.Documents() collection. The linked Revit documents will also form a part of this collection. We can then use filters in Revit API to create a collection of Linked Revit documents. Comparing the name of the Linked File with that of the document object in the Documents() collection, we can get access to the required document object of the Linked file we are interested in working with.
Once we have the document object, all we need is to simply access each wall element using the FilteredElementCollector on this document object of the linked file. From each wall, we can access the Level information, as desired. The following code snippet illustrates the entire approach.
using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
namespace Revit.SDK.Samples.HelloRevit.CS
{
[Transaction(TransactionMode.Manual)]
public class Command : IExternalCommand
{
public Result Execute(ExternalCommandData commandData,
ref string message,
ElementSet elements)
{
UIApplication uiApp = commandData.Application;
Document doc = uiApp.ActiveUIDocument.Document;
FilteredElementCollector collector =
new FilteredElementCollector(doc);
IList<Element> elems = collector
.OfCategory(BuiltInCategory.OST_RvtLinks)
.OfClass(typeof(RevitLinkType))
.ToElements();
foreach (Element e in elems)
{
RevitLinkType linkType = e as RevitLinkType;
String s = String.Empty;
foreach (Document linkedDoc in uiApp.Application.Documents)
{
if (linkedDoc.Title.Equals(linkType.Name))
{
FilteredElementCollector collLinked =
new FilteredElementCollector(linkedDoc);
IList<Element> linkedWalls =
collLinked.OfClass(typeof(Wall)).ToElements();
foreach (Element eleWall in linkedWalls)
{
Wall wall = eleWall as Wall;
s = s + "\n" + eleWall.Level.Name;
}
TaskDialog.Show("Wall Level", linkedDoc.PathName + " : " + s);
}
}
}
return Result.Succeeded;
}
}