Revit二次开发入门——获取模型中门窗数量

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

namespace HelloWorld
{

    [Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)]
    [Regeneration(RegenerationOption.Manual)]
    public class GetAllWindows : IExternalCommand
    {

        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {

            UIApplication uiApp = commandData.Application;
            Autodesk.Revit.ApplicationServices.Application app = uiApp.Application;
            Document doc = uiApp.ActiveUIDocument.Document;
           //获得所有的窗户
            FilteredElementCollector collector = new FilteredElementCollector(doc);
            collector.OfClass(typeof(FamilyInstance)).OfCategory(BuiltInCategory.OST_Windows);
            IList<Element> lists = collector.ToElements();
            string strMsg = string.Format("There are {0}windows in current model", lists.Count);
            MessageBox.Show(strMsg);

            //过滤器来获取所有的门
            FilteredElementCollector collector2 = new FilteredElementCollector(doc);
            ElementClassFilter classFilter = new ElementClassFilter(typeof(FamilyInstance));
            ElementCategoryFilter catFilter = new ElementCategoryFilter(BuiltInCategory.OST_Doors);
            LogicalAndFilter logicalFilter = new LogicalAndFilter(classFilter, catFilter);
            collector2.WherePasses(logicalFilter);
            IList < Element > list2= collector2.ToElements();
            strMsg = string.Format("There Area{0}doors in current mode", list2.Count);
            MessageBox.Show(strMsg);



            return Result.Succeeded;
        }

    }
}

Revit中加载外部命令,即可提示模型中门窗数量。

FilteredElementCollector用法
直接添加过滤条件
-OfCategory
-ofCategoryId
-OfClass

识别Revit中不同对象
-使用类名来判断
-通过类名无法判断时,用Category来判断其类别

以下代码实现返回 某一层中的窗户数:

 UIApplication uiApp = commandData.Application;
            Autodesk.Revit.ApplicationServices.Application app = uiApp.Application;
            Document doc = uiApp.ActiveUIDocument.Document;

            FilteredElementCollector collector = new FilteredElementCollector(doc);
            collector.OfClass(typeof(FamilyInstance)).OfCategory(BuiltInCategory.OST_Windows);
            IList<Element> lists = collector.ToElements();

             var windowsInLevel1 = from element in collector
                                   where element.Level.Name == "Level 1"
                                   select element;
             string strMsg = string.Format("There are {0}windows in Level 1", windowsInLevel1.Count());
             MessageBox.Show(strMsg);
            return Result.Succeeded;

你可能感兴趣的:(Revit二次开发)