qin
2024-09-28 e358beb08f5be49703009b64f058ecfbcfeefbd9
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
using Autodesk.Revit.DB;
using DevExpress.XtraPrinting.Export.Pdf;
using HStation.RevitDev.RevitDataExport.Utility;
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
 
namespace HStation.RevitDev.RevitDataExport.Service
{
    public class SearchService
    {
        public static List<Element> SearchElements(Document doc, Type type, string nameFilter, BuiltInCategory cate = BuiltInCategory.INVALID)
        {
            if (doc == null) { return null; }
 
            FilteredElementCollector collector = new FilteredElementCollector(doc);
            if(cate != BuiltInCategory.INVALID)
            {
                collector = collector.OfCategory(cate);
            }
 
            if(type != null)
            {
                collector = collector.OfClass(type);
            }
 
            List<Element> result = new List<Element>(); 
            if (!string.IsNullOrEmpty(nameFilter))
            {
                nameFilter = RegexUtils.FormatPatternString(nameFilter);
                IList<Element> elems = collector.ToElements();
                foreach (Element elem in elems)
                {
                    if (elem != null)
                    {
                        string name = elem.Name;
                        Regex regex = new Regex(nameFilter);
                        Match isMatch = regex.Match(name);
                        if (isMatch != null && isMatch.Success)
                        {
                            result.Add(elem);
                        }
                    }
                }
            }
            
            return result;
        }
    }
}