using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using System.Collections.Generic;
using System.Linq;
namespace RevitLinkElementExtractor
{
[Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.ReadOnly)]
public class LinkElementCollector : IExternalCommand
{
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
var doc = commandData.Application.ActiveUIDocument.Document;
foreach (RevitLinkInstance link in new FilteredElementCollector(doc)
.OfClass(typeof(RevitLinkInstance)))
{
TaskDialog.Show("链接模型", link.Name);
var linkDoc = link.GetLinkDocument();
var elementsInLink = new FilteredElementCollector(linkDoc)
.WhereElementIsNotElementType();
ProcessElements(elementsInLink.ToElements(), link.Name);
}
return Result.Succeeded;
}
private void ProcessElements(IEnumerable<Element> elements, string linkName)
{
var count = new Dictionary<string, int>();
foreach (var e in elements)
{
var typeName = e.GetType().Name;
if (count.ContainsKey(typeName)) count[typeName]++;
else count.Add(typeName, 1);
}
TaskDialog.Show("图元统计", string.Join("\n",
count.Select(p => $"{p.Key}: {p.Value}")));
}
}
}