C# 确定是否为族状态在视图中可见

C# 确定是否为族状态在视图中可见,c#,view,element,revit-api,revit,C#,View,Element,Revit Api,Revit,我有一个FamilyInstance pFam和一个Autodesk.Revit.DB.View pView。我想知道pFam是否在pView中可见。我试过使用 if (pFam.IsHidden(pView) continue; 不幸的是,这只告诉我元素是否应该隐藏,事实并非如此。但是,元素在每个视图中都不可见,在这种情况下,我希望(或者更确切地说,不希望)发生一些事情。对于FamilyInstances,没有Visible或IsVisible属性。。。有人知道如何处理这些情况吗 谢

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

if (pFam.IsHidden(pView)
    continue;
不幸的是,这只告诉我元素是否应该隐藏,事实并非如此。但是,元素在每个
视图中都不可见,在这种情况下,我希望(或者更确切地说,不希望)发生一些事情。对于
FamilyInstance
s,没有
Visible
IsVisible
属性。。。有人知道如何处理这些情况吗


谢谢

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

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

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]注释。

谢谢您,科林!我将其添加到,在的讨论中进行了描述。我还总结了此讨论,并指出了有关建筑编码器的以前相关问题:。又来了!