Finding Used Types in an RVT Link

Writing about the Copy/Monitor UI, Steve mentions that “It would be cool if Revit identified the elements and types that are actually in use in the linked file”

The Revit API can’t modify the┬áCopy/Monitor UI, but the API can be used to identify┬áthe elements and types that are actually in use in the linked file.

levelTypes

public void levelTypesInLink()
{
    Document doc = this.ActiveUIDocument.Document;
    foreach (RevitLinkType linkType in new FilteredElementCollector(doc).OfClass(typeof(RevitLinkType)).Cast<RevitLinkType>())
    {
        string usedLevelTypes = "";
        string notUsedLevelTypes = "";
        Document linkDoc = getLinkDoc(doc, linkType);
        foreach (LevelType levelType in new FilteredElementCollector(linkDoc).OfClass(typeof(LevelType)).Cast<LevelType>())
        {
            if (new FilteredElementCollector(linkDoc).OfClass(typeof(Level)).Cast<Level>().Where(level => level.LevelType.Id == levelType.Id).Count() > 0)
                usedLevelTypes += levelType.Name + ", ";
            else
                notUsedLevelTypes += levelType.Name + ", ";
        }
        TaskDialog.Show("Level Types in " + linkDoc.Title,
                        "Used\n------\n" + usedLevelTypes + 
                        "\n\nNot Used\n---------\n" + notUsedLevelTypes);
    }
}

private Document getLinkDoc(Document doc, RevitLinkType linkType)
{
    foreach (Document linkDoc in doc.Application.Documents)
    {
      if (linkDoc.Title.Equals(linkType.Name))
      {
          return linkDoc; 
      }
    }
    return null;
}