看了下网上所有关于Revit链接获取方式全是通过selection,如果每次执行都要框选太恶心了,而且数据量大的话框选很容易崩溃,高亮渲染会很卡。
获取链接文档的话,如下列代码,但是如果涉及到导出的话文档是无法获取链接矩阵,这个方法走不通。
UIDocument activeUIDoc = commandData.Application.ActiveUIDocument;
Document activeDoc = activeUIDoc.Document;
foreach(Document doc in activeDoc.Application.Documents)
{
if(doc.IsLinked)
{
//该doc为链接文档
}
}
要获取链接的矩阵,就要获取到链接的实例,RevitLinkInstance。
FilteredElementCollector collector = new FilteredElementCollector(activeDoc);
ElementClassFilter LinkFilter = new ElementClassFilter(typeof(RevitLinkInstance));
var linkIterator = collector.WherePasses(LinkFilter).GetElementIterator();
while (linkIterator.MoveNext())
{
RevitLinkInstance LinkInstance = linkIterator.Current as RevitLinkInstance;
Document LinkDoc = LinkInstance.GetLinkDocument();
//获取链接矩阵
Transform trans = Transform.Identity.Multiply((LinkInstance as RevitLinkInstance).GetTransform());
//通过链接的文档获取过滤器
FilteredElementCollector LinkCor = new FilteredElementCollector(LinkDoc);
//获取链接的三维视图
FilteredElementIterator viewIterator = LinkCor.OfClass(typeof(Autodesk.Revit.DB.View)).GetElementIterator();
View3D view3D = viewIterator.Current as View3D;
while (viewIterator.MoveNext())
{
View3D view = viewIterator.Current as View3D;
if (view != null && view.IsValidObject && !view.IsTemplate)
{
if (view.ViewName == "{三维}")
{
view3D = view;
break;
}
}
}
}
就此我们可以获取链接的矩阵,Document,视图,就可以对链接进行操作了。注意,链接的Document.ActiveView为空
然后就可以通过过滤器获取Element了
FilteredElementCollector collector = new FilteredElementCollector(LinkDoc);
ElementClassFilter instanceFilter = new ElementClassFilter(typeof(FamilyInstance));
ElementClassFilter HostFilter = new ElementClassFilter(typeof(HostObject));
//过滤族实例和宿主元素
LogicalOrFilter filter = new LogicalOrFilter(new List<ElementFilter>() { instanceFilter, HostFilter });
FilteredElementCollector instance = collector.OfCategoryId(categoryId).WherePasses(filter).WhereElementIsNotElementType();
var elems = instance.WhereElementIsNotElementType().GetElementIterator();
while(elems)
{
var element = elems.Current;
//balabala
}
忙了几天终于把链接走通了,程序猿最开心的莫过于此吧。