...
zhangyuekai
2024-08-15 4f89840b0c95ebd0ed79dff1ff1973d030a6c17d
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
using HStation.RevitDev.Model.AttributeClass;
using HStation.RevitDev.Model.ModelEnum;
using HStation.RevitDev.RevitDataExport.Common;
using HStation.RevitDev.RevitDataExport.Entity;
using HStation.RevitDev.RevitDataExport.Parser;
using System;
using System.Reflection;
 
namespace HStation.RevitDev.RevitDataExport.Utility
{
    public static class RevitTypeExtense
    {
        public static bool IsRequired(this RevitType type)
        {
            var config = GlobalResource.ConfigModel;
            var props = config.GetType().GetProperties();
 
            foreach (var prop in props) 
            {
                var attrs = prop.GetCustomAttributes(false);
                if (attrs.Length > 0)
                {
                    var attr = attrs[0] as RevitTypeAttribute;
                    if (attr.m_revitType == type)
                    {
                        bool? isRequired = prop.GetValue(config) as bool?;
                        if (isRequired == true)
                        {
                            return true;
                        }
                        return false;
                    }
                }
            }
 
            return false;
        }
 
        public static Type GetElementModelType(this RevitType revitType) 
        {
            var exportTypes = Assembly.GetExecutingAssembly().GetExportedTypes();
            foreach (var type in exportTypes)
            {
                if (!type.IsSubclassOf(typeof(ModelBase))) { continue; }
 
                var attr = type.GetCustomAttribute<RevitTypeAttribute>();
                if (attr == null) { continue; }
 
                var rvtType = attr.m_revitType;
                if (rvtType == revitType) 
                {
                    return type;
                }
            }
            return null;
        }
 
        public static BaseParser CreateModelParser(this RevitType revitType)
        {
            var types = Assembly.GetExecutingAssembly().GetTypes();
            foreach (var type in types) 
            {
                if (!type.IsSubclassOf(typeof(BaseParser))) { continue; }
                var attr = type.GetCustomAttribute<RevitTypeAttribute>();
 
                if (attr == null) { continue; }
                if (attr.m_revitType != revitType) { continue; }
 
                BaseParser parser = Activator.CreateInstance(type) as BaseParser;
                if (parser == null) { continue; }
 
                return parser;
            }
            return null;
        }
    }
}