Revit二次开发——加快过滤速度,以及对ElementIntersect

来源:建筑界编辑:黄子俊发布时间:2020-03-22 19:39:26

[摘要] 在Revit的API中有快过滤器和慢过滤器,其中慢过滤器和快过滤器合用会加快过滤器的速度。首先来看一个例子,以下是一个用了ElementInterse

在Revit的API中有快过滤器和慢过滤器,其中慢过滤器和快过滤器合用会加快过滤器的速度。

首先来看一个例子,以下是一个用了ElementIntersectsSolidFilter 慢过滤器的例子,如果元素多达几千个,下面最后一行代码起码耗时三四十秒:

 FilteredElementCollector collector = new FilteredElementCollector(revitDoc); ElementIntersectsSolidFilter solidFilter = new ElementIntersectsSolidFilter(solid); collector.WherePasses(solidFilter); IList<Element> listElement = new List<Element>(); listElement = collector.ToList();

怎幺加快速度,而又不影响结果呢?那就和快过滤器合起来用,下面的代码多用了BoundingBoxIntersectsFilter,BoundingBoxIsInsideFilter这两个快过滤器。结果跟上面一致,但是速度快上十倍不止。至于原理,去看看官方教材就知道怎幺回事了。

 FilteredElementCollector collector = new FilteredElementCollector(revitDoc); BoundingBoxIntersectsFilter boxFilter = new BoundingBoxIntersectsFilter(new Outline(solid.GetBoundingBox().Min, solid.GetBoundingBox().Max)); BoundingBoxIsInsideFilter boxInsideFilter = new BoundingBoxIsInsideFilter(new Outline(solid.GetBoundingBox().Min, solid.GetBoundingBox().Max)); LogicalOrFilter logicOrFilter = new LogicalOrFilter(boxFilter, boxInsideFilter); ElementIntersectsSolidFilter solidFilter = new ElementIntersectsSolidFilter(solid); collector.WherePasses(logicOrFilter); collector.WherePasses(solidFilter); IList<Element> listElement = new List<Element>(); listElement = collector.ToList();

上面的代码在Debug版本里没有丝毫事情,但是在Release版本里却出现错误,尤其是碰到跑闲置程序的后台功能在跑,会直接导致致命错误,Revit会直接崩溃。这就是ElementIntersectsSolidFilter的大Bug,17版和18版的Revit的API都有这样的缺陷。

那幺怎幺解决这个Bug呢,这个大Bug花了我十几天的时间,也是坑得飞起。但是解决起来却很简单。比如我们要用上面过滤出来的结果,我们不用ElementIntersectsSolidFilter过滤,只是用它来判断元素是不是在这个过滤器里面。最后花费的时间并没有明显增长,而且解决了这个api的Bug问题。

FilteredElementCollector collector = new FilteredElementCollector(revitDoc);BoundingBoxIntersectsFilter boxFilter = new BoundingBoxIntersectsFilter(new Outline(solid.GetBoundingBox().Min, solid.GetBoundingBox().Max));BoundingBoxIsInsideFilter boxInsideFilter = new BoundingBoxIsInsideFilter(new Outline(solid.GetBoundingBox().Min, solid.GetBoundingBox().Max));LogicalOrFilter logicOrFilter = new LogicalOrFilter(boxFilter, boxInsideFilter); collector.WherePasses(logicOrFilter); IList<Element> listElement = new List<Element>();listElement = collector.ToList(); foreach(Element elem in listElement) { FilteredElementCollector elementCollector = new FilteredElementCollector(revitDoc, new List<ElementId>() { elem.Id }); ElementIntersectsSolidFilter solidFilter = new ElementIntersectsSolidFilter(solid); elementCollector.WherePasses(solidFilter); if(elementCollector!=null&&elementCollector.Count()>0) { DoSomeThing(elem); } elementCollector.Dispose(); }

 

过滤,加快,速度

延展阅读

相关文章