Revit二次开发之按照标高过滤元素

Revit二次开发之按照标高过滤元素

之前群里有朋友问怎么过滤特定标高的元素,当时有人回答先都过滤出来,然后遍历判断相应的标高参数来找出特定标高的元素。今天在看书的时候看到了一个可以过滤特定标高元素的方法,在此记录一下。
这个demo实现了亮显标高 1中所有元素的效果。


using System;
using System.Collections.Generic;
using Autodesk.Revit.UI;
using Autodesk.Revit.DB;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.UI.Selection;
using Autodesk.Revit.DB.Mechanical;
using Autodesk.Revit.DB.Plumbing;
using Autodesk.Revit.DB.Electrical;
using System.Threading;
using System.Linq;
using System.Runtime.InteropServices;

namespace 插件
{
     
    [Autodesk.Revit.Attributes.Transaction(TransactionMode.Manual)]
    [Autodesk.Revit.Attributes.Regeneration(RegenerationOption.Manual)]
    [Autodesk.Revit.Attributes.Journaling(JournalingMode.UsingCommandData)]
    public class Command : IExternalCommand
    {
     
        public readonly double unit = 304.8;

        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elementSet)
        {
     
            UIApplication uiapp = new UIApplication(commandData.Application.Application);
            UIDocument uidoc = commandData.Application.ActiveUIDocument;
            Document doc = uidoc.Document;
            Selection selection = uidoc.Selection;

            Level level = new FilteredElementCollector(doc).OfCategory(BuiltInCategory.OST_Levels).OfClass(typeof(Level)).WhereElementIsNotElementType().ToElements().Where(m => m.Name == "标高 1").ToList().FirstOrDefault() as Level;
            // 创建一个标高过滤器
            ElementLevelFilter levelFilter = new ElementLevelFilter(level.Id);
            FilteredElementCollector coll = new FilteredElementCollector(doc);
            ICollection<ElementId> ids = coll.WhereElementIsNotElementType().WherePasses(levelFilter).ToElementIds();
            selection.SetElementIds(ids);
            return Result.Succeeded;
        }
    }
}
  • 参考——API开发指南 宦国胜 主编

你可能感兴趣的:(Revit二次开发,Revit二次开发,Revit按照标高过滤)