duheng
2025-03-05 0f831db8df9c2e4adc7feca636967a0fb1cd5e29
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Plumbing;
using Autodesk.Revit.UI;
using DevExpress.Internal.WinApi.Windows.UI.Notifications;
using HStation.RevitDev.RevitDataExport.Common;
using HStation.RevitDev.RevitDataExport.Entity;
using HStation.RevitDev.RevitDataExport.Forms;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Windows.Forms;
 
namespace HStation.RevitDev.RevitDataExport.Utility
{
    public class SystemCheckUtils
    {
        public static Dictionary<CheckResultType, List<ElementId>> SystemCheck(ExternalCommandData data)
        {
            UIDocument uidoc = data.Application.ActiveUIDocument;
            Document doc = uidoc.Document;
            
            Dictionary<CheckResultType, List<ElementId>> retDict = new Dictionary<CheckResultType, List<ElementId>>();
            var ids = new List<ElementId>();
            
            foreach (var id in GlobalResource.RevitModels.GetIds())
            {
                var elemId = new ElementId(int.Parse(id));
                var element = doc.GetElement(elemId);
                if (element == null) { continue; }
 
                bool checkResult = CheckElement(element, out CheckResultType errorType);
 
                if (!checkResult)
                {
                    ids.Add(new ElementId(int.Parse(id)));
                    if (!retDict.ContainsKey(errorType))
                    {
                        retDict.Add(errorType, new List<ElementId>());
                    }
                    retDict[errorType].Add(elemId);
                }
            }
            //uidoc.Selection.SetElementIds(ids);
 
            return retDict;
        }
 
        internal static void ShowCheckResult(UIApplication m_uiapp, List<CheckResultViewModel> checkResult)
        {
            var revitHandle = Process.GetCurrentProcess().MainWindowHandle;
            Form_SystemCheckResult result = new Form_SystemCheckResult(m_uiapp, checkResult);
            result.Show(new WindowHandle(revitHandle));
        }
 
        private static bool CheckElement(Element element, out CheckResultType errorType)
        {
            errorType = CheckResultType.CRT_Unknown;
 
            //管道两端都必须有节点构件连接
            if (element is Pipe pipe)
            {
                var connectors = pipe.ConnectorManager.Connectors;
                foreach (Autodesk.Revit.DB.Connector connector in connectors)
                {
                    var connectElems = connector.GetConnectorElements();
                    if (connectElems.Count < 1)
                    {
                        errorType = CheckResultType.CRT_PipeConnectorOpen;
                        return false;
                    }
 
                    var fi = connectElems[0] as FamilyInstance;
                    if (fi == null) 
                    {
                        errorType = CheckResultType.CRT_Unknown;
                        return false; 
                    }
 
                    foreach (var elem in connectElems)
                    {
                        var id = elem.Id.IntegerValue.ToString();
                        if (!GlobalResource.RevitModels.Contains(id))
                        {
                            errorType = CheckResultType.CRT_PipeConnectorOpen;
                            return false;
                        }
                    }
                }
                return true;
            }
 
            //节点不能连接节点
            else if (element is FamilyInstance fi)
            {
                var mepModel = fi.MEPModel;
                if (mepModel == null) { return true; }
 
                ConnectorSet connectors = mepModel.ConnectorManager?.Connectors;
                if (connectors == null) { return true; }
 
                foreach(Autodesk.Revit.DB.Connector connector in connectors)
                {
                    var connectElems = connector.GetConnectorElements();
                    foreach (var connectElem in connectElems)
                    {
                        if (connectElem is FamilyInstance)
                        {
                            errorType = CheckResultType.CRT_NodeConnectNode;
                            return false;
                        }
                    }
                }
                return true;
            }
            else
            {
                errorType = CheckResultType.CRT_Unknown;
                return false;
            }
        }
    }
}