【问题标题】:Determine is a FamilyInstance is visible in a View确定 FamilyInstance 在 View 中是否可见
【发布时间】:2017-10-16 04:40:21
【问题描述】:

我有一个FamilyInstance pFam 和一个Autodesk.Revit.DB.View pView。我想知道pFam 在pView 中是否可见。我试过使用

if (pFam.IsHidden(pView)
    continue;

不幸的是,这只告诉我该元素是否应该被隐藏,事实并非如此。但是,该元素在每个View 中都不可见,在这种情况下,我希望(或者更确切地说不希望)发生一些事情。 FamilyInstances 没有 Visible 或 IsVisible 属性...有谁知道处理这些情况的方法吗?

谢谢!

【问题讨论】:

    标签: c# view element revit-api revit


    【解决方案1】:

    我发现了解某个元素在视图中是否可见的最可靠方法是使用特定于该视图的 FilteredElementCollector。控制元素可见性的方法有很多种,因此尝试任何其他方法来确定这一点都是不切实际的。

    下面是我用来实现这一点的实用函数。请注意,这适用于任何元素,而不仅仅是家庭实例。

    public static bool IsElementVisibleInView([NotNull] this View view, [NotNull] Element el)
        {
            if (view == null)
            {
                throw new ArgumentNullException(nameof(view));
            }
    
            if (el == null)
            {
                throw new ArgumentNullException(nameof(el));
            }
    
            // Obtain the element's document
            Document doc = el.Document;
    
            ElementId elId = el.Id;
    
            // Create a FilterRule that searches for an element matching the given Id
            FilterRule idRule = ParameterFilterRuleFactory.CreateEqualsRule(new ElementId(BuiltInParameter.ID_PARAM), elId);
            var idFilter = new ElementParameterFilter(idRule);
    
            // Use an ElementCategoryFilter to speed up the search, as ElementParameterFilter is a slow filter
            Category cat = el.Category;
            var catFilter = new ElementCategoryFilter(cat.Id);
    
            // Use the constructor of FilteredElementCollector that accepts a view id as a parameter to only search that view
            // Also use the WhereElementIsNotElementType filter to eliminate element types
            FilteredElementCollector collector =
                new FilteredElementCollector(doc, view.Id).WhereElementIsNotElementType().WherePasses(catFilter).WherePasses(idFilter);
    
            // If the collector contains any items, then we know that the element is visible in the given view
            return collector.Any();
        }
    

    类别过滤器用于在使用较慢的参数过滤器查找所需元素之前消除任何不属于所需类别的元素。巧妙地使用过滤器可能会进一步加快速度,但我发现它在实践中对我来说已经足够快了。

    如果您没有 ReSharper,请删除您看到的 [NotNull] 注释。

    【讨论】:

    猜你喜欢
    • 2016-02-07
    • 2013-05-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-25
    • 2011-08-20
    • 1970-01-01
    相关资源
    最近更新 更多