七、revit中元素的获取方法(3)

3.过滤器

  • Category、Family、FamilySymbol、FamilyInstance这四个概念之间的关系

类别(Category)>族(Family)>族模型(FamilySymbol),这三者是子集关系,而你在视图中实际绘制的墙就叫做族实例(FamilyInstance)

  • 在revit中使用FilteredElementCollector这个类

  • 使用过滤器的方法:

    第一:明确目标对象的特征,编写过滤器

    第二:明确遍历方式,从过滤器获取元素(foreach或者linq)

    第三:获得结果对象

using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GetInformation
{
    [Transaction(TransactionMode.ReadOnly)] //事务
    public class FilterElement : IExternalCommand //实现IExternalCommand接口
    {
        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            //通过CommandData获取UIDocument
            var uidoc = commandData.Application.ActiveUIDocument;
            //通过UIDocument获取当前revit文档
            var doc = uidoc.Document;
            //第一步:创建过滤器,FilteredElementCollector函数可以有三个参数(文档,视图ID/元素ID的集合)意思就是,在创建过滤器的时候一定要给过滤器一个范围
            var collector = new FilteredElementCollector(doc);
            //对过滤器添加限制条件,方法一:通过类的类型来过滤
            //collector.OfClass(typeof(FamilyInstance)); //把指定类型过滤出来,用ofclass方法,它需要传入一个参数它的类型是type,
                                                       //可以通过typeof关键字,传入一个类,这样就可以把一个类的类型过滤出来。类的类型是每个类的属性
            //对过滤器添加限制条件,方法二:通过类的类别来过滤
            //collector.OfCategory(BuiltInCategory.OST_Walls);
            //但是通常要组合起来过滤,才能准确的找到你想要的东西,类似于在字典里面找字。这里表达的意思就是“过滤出门的族实例”,因为门类别的子项包含族实例还包括子项族模型
            collector.OfCategory(BuiltInCategory.OST_Doors).OfClass(typeof(FamilyInstance));
            //第二步:从过滤器中获取元素。常用方法是遍历foreach。FilteredElementCollector实际上是实现的IEnumerable接口,凡是实现这个接口的都可以用foreach进行遍历。
            var elementlist = new List<Element>();  //创建一个元素列表
            foreach (Element item in collector)  //通过foreach来循环collector里面的元素
            {
                elementlist.Add(item);  //将collector里面的元素添加到elementlist列表
            }
            TaskDialog.Show("ZZW", $"门的族实例有{elementlist.Count}个!");
            return Result.Succeeded;
        }
    }
}

using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GetInformation
{
    [Transaction(TransactionMode.ReadOnly)] //事务
    public class FilterElement : IExternalCommand //实现IExternalCommand接口
    {
        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            //通过CommandData获取UIDocument
            var uidoc = commandData.Application.ActiveUIDocument;
            //通过UIDocument获取当前revit文档
            var doc = uidoc.Document;
            //第一步:创建过滤器,FilteredElementCollector函数可以有三个参数(文档,视图ID/元素ID的集合)意思就是,在创建过滤器的时候一定要给过滤器一个范围
            var collector = new FilteredElementCollector(doc);
            //对过滤器添加限制条件,方法一:通过类的类型来过滤
            //collector.OfClass(typeof(FamilyInstance)); //把指定类型过滤出来,用ofclass方法,它需要传入一个参数它的类型是type,
                                                       //可以通过typeof关键字,传入一个类,这样就可以把一个类的类型过滤出来。类的类型是每个类的属性
            //对过滤器添加限制条件,方法二:通过类的类别来过滤
            //collector.OfCategory(BuiltInCategory.OST_Walls);
            //但是通常要组合起来过滤,才能准确的找到你想要的东西,类似于在字典里面找字。这里表达的意思就是“过滤出门的族实例”,因为门类别的子项包含族实例还包括子项族模型
            collector.OfCategory(BuiltInCategory.OST_Doors).OfClass(typeof(FamilyInstance));
            //第二步:从过滤器中获取元素。
            //通过linq语句查询(语言集成查询)
            var selectedDoors = from elem in collector where elem.Name == "Entrance door" select elem;

            TaskDialog.Show("ZZW", $"门的名字叫做Entrance door的族实例有{selectedDoors.Count()}个!");
            return Result.Succeeded;
        }
    }
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值