zhangyuekai
2024-08-08 f0d05cbc4d6b65b08f846f9fe82c3ead0b9bbac2
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Events;
using Autodesk.Revit.DB.Plumbing;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Events;
using DevExpress.Utils.Extensions;
using HStation.RevitDev.Model.ModelEnum;
using HStation.RevitDev.RevitDataExport.Common;
using HStation.RevitDev.RevitDataExport.Entity;
using HStation.RevitDev.RevitDataExport.Service;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using static DevExpress.Data.Mask.Internal.RegExMaskMath.CloudOfInts;
 
namespace HStation.RevitDev.RevitDataExport.Utility
{
    public static class DocumentUtil
    {
        public static void RegistDocumentEvent(ExternalCommandData commandData)
        {
            commandData.Application.Application.DocumentChanged -= App_DocumentChanged;
            commandData.Application.Application.DocumentChanged += App_DocumentChanged;
 
            GlobalResource.CurrentUIDocument = commandData.Application.ActiveUIDocument;
            RegistViewEvent(commandData);
        }
 
        private static void App_DocumentChanged(object sender, DocumentChangedEventArgs e)
        {
            var doc = e.GetDocument();
            try
            {
                var addedIds = e.GetAddedElementIds();
                var removeIds = e.GetDeletedElementIds();
                var modifiedIds = e.GetModifiedElementIds();
 
                AddElements(doc,  addedIds);
                RemoveElements(removeIds);
                ModifyElements(modifiedIds);
 
                UpdateLinkerInfo(doc);
                GlobalResource.InstancePanel.UpdateForm();
            }
            catch (Exception ex)
            {
                TaskDialog.Show("错误", ex.Message);
            }
        }
 
        private static void UpdateLinkerInfo(Document doc)
        {
            if (!doc.IsValidObject) { return; }
 
            foreach (var pair in GlobalResource.RevitModels)
            {
                foreach (var model in pair.Value) 
                {
                    var id = model.Id;
                    var element = doc.GetElement(new ElementId(int.Parse(id)));
                    var linkedElems = element.GetConnectElements();
                    var linkedIds = linkedElems.Select(x => x.Id.IntegerValue.ToString());
                    model.LinkIds = string.Join(",", linkedIds);
                }
            } 
        }
 
        private static void ModifyElements(ICollection<ElementId> modifiedIds)
        {
            foreach (var id in modifiedIds)
            {
                if (GlobalResource.ElementIds.Contains(id.IntegerValue.ToString()))
                {
 
                }
            }
        }
 
        private static void RemoveElements(ICollection<ElementId> removeIds)
        {
            foreach (var id in removeIds)
            {
                var strId = id.IntegerValue.ToString();
                if (GlobalResource.ElementIds.Contains(strId))
                {
                    GlobalResource.ElementIds.Remove(id.IntegerValue.ToString());
                    foreach (var pair in GlobalResource.RevitModels)
                    {
                        var values = pair.Value;
                        var model = values.Where(x => x.Id == strId)?.ToList();
                        if (model == null || model.Count() == 0)
                        {
                            continue;
                        }
                        else
                        {
                            values.Remove(model[0]);
                        }
                    }
                }
            }
 
            return;
        }
 
        private static void AddElements(Document doc, ICollection<ElementId> addedIds)
        {
            bool instancePlacing = GlobalResource.InstancePlacing;
            bool pipePlacing = GlobalResource.PipePlacing;
 
            if (pipePlacing)
            {
                foreach (var id in addedIds)
                {
                    var elem = doc.GetElement(id);
                    if (elem is FamilyInstance fi)
                    {
                        AddAutoCreated(fi);
                    }
                    if (elem is Pipe)
                    {
                        GlobalResource.ElementIds.Add(id.IntegerValue.ToString());
                        AddManualCreated(elem);
                    }
                }
            }
            else if (instancePlacing)
            {
                foreach (var id in addedIds)
                {
                    var elem = doc.GetElement(id);
                    if (elem is FamilyInstance)
                    {
                        GlobalResource.ElementIds.Add(id.IntegerValue.ToString());
                        AddManualCreated(elem);
                    }
                }
            }
            else
            {
                bool isPumpSystemElement = false;
                foreach (var id in addedIds)
                {
                    if (IsConnectedWithPumpSystem(doc, id))
                    {
                        isPumpSystemElement = true;
                        break;
                    }
                }
 
                if (isPumpSystemElement)
                {
                    foreach (var id in addedIds)
                    {
                        AddToPumpSystem(doc, id);
                    }
                }
            }
            return;
        }
 
        private static bool IsConnectedWithPumpSystem(Document doc, ElementId id)
        {
            var elem = doc.GetElement(id);
            if (elem is Pipe pipe) //直接点击水泵的连接件创建管道
            {
                if (pipe.ConnectWithPumpSystem())
                {
                    //GlobalResource.ElementIds.Add(id.IntegerValue.ToString());
                    //TaskDialog.Show("提示", "增加管道:" + id.IntegerValue.ToString());
                    //AddManualCreated(elem);
                    return true;
                }
            }
            else if (elem is FamilyInstance fi)
            {
                if (fi.ConnectWithPumpSystem()) //直接点击管道连接点创建管道时,Revit会自动创建弯头,都需要加入到缓存
                {
                    //GlobalResource.ElementIds.Add(id.IntegerValue.ToString());
                    //TaskDialog.Show("提示", "增加族实例:" + id.IntegerValue.ToString());
                    //AddManualCreated(elem);
                    return true;
                }
            }
            return false;
        }
 
        private static void AddToPumpSystem(Document doc, ElementId id)
        {
            var elem = doc.GetElement(id);
            if (elem is Pipe)
            {
                GlobalResource.ElementIds.Add(id.IntegerValue.ToString());
                AddToMap(elem, RevitType.RFT_Pipe);
            }
            else if (elem is FamilyInstance fi)
            {
                GlobalResource.ElementIds.Add(id.IntegerValue.ToString());
                AddAutoCreated(fi);
            }
            return;
        }
 
        private static void AddAutoCreated(FamilyInstance fi)
        {
            var id = fi.Id;
            GlobalResource.ElementIds.Add(id.IntegerValue.ToString());
            if (fi.IsWanTou())
            {
                AddToMap(fi, RevitType.RFT_Elbow);
            }
            else if (fi.IsConverter())
            {
                AddToMap(fi, RevitType.RFT_Converter);
            }
            else if (fi.IsSanTong())
            {
                AddToMap(fi, RevitType.RFT_ThreeJoint);
            }
            else if (fi.IsSiTong())
            {
                AddToMap(fi, RevitType.RFT_FourJoint);
            }
            else
            {
                TaskDialog.Show("警告", $"未知类型被添加!id:{fi.Id.IntegerValue}");
                //报错
            }
        }
 
        private static void AddManualCreated(Element elem)
        {
            var id = elem.Id.IntegerValue.ToString();
 
            //更新已加入到缓存中的构件数据
            List<string> linkedIds = elem.GetConnectElements().Select(x => x.Id.IntegerValue.ToString()).ToList();
            //foreach (var linkId in linkedIds)
            //{
            //    var model = GlobalResource.GetElementModel(linkId);
            //    if (!model.LinkIds.Contains(id))
            //    {
            //        model.LinkIds += "," + id;
            //    }
            //}
 
            //把新增构件加入到缓存中
            if (!GlobalResource.RevitModels.ContainsKey(GlobalResource.RevitFamilyType))
            {
                GlobalResource.RevitModels.Add(GlobalResource.RevitFamilyType, new ObservableCollection<ElementModel>());
                GlobalResource.RevitModels[GlobalResource.RevitFamilyType].Add(new ElementModel
                {
                    Name = elem.Name,
                    Id = id,
                    LinkIds = /*string.Join(",", linkedIds)*/string.Empty
                });
 
            }
            else if(!GlobalResource.RevitModels[GlobalResource.RevitFamilyType].Any(x=>x.Id == id))
            {
                GlobalResource.RevitModels[GlobalResource.RevitFamilyType].Add(new ElementModel
                {
                    Name = elem.Name,
                    Id = id,
                    LinkIds = /* string.Join(",", linkedIds)*/string.Empty
                });
            }
        }
 
        private static void AddToMap(Element elem, RevitType revitType)
        {
            var id = elem.Id.IntegerValue.ToString();
 
            //更新已加入到缓存中的构件数据
            //List<string> linkedIds = elem.GetConnectElements().Select(x => x.Id.IntegerValue.ToString()).ToList();
            //foreach (var linkId in linkedIds)
            //{
            //    var model = GlobalResource.GetElementModel(linkId);
            //    if (!model.LinkIds.Contains(id))
            //    {
            //        model.LinkIds += "," + id;
            //    }
            //}
 
            if (!GlobalResource.RevitModels.ContainsKey(revitType))
            {
                GlobalResource.RevitModels.Add(revitType, new ObservableCollection<ElementModel>());
                GlobalResource.RevitModels[revitType].Add(new ElementModel
                {
                    Name = elem.Name,
                    Id = id,
                    LinkIds = /*string.Join(",", linkedIds)*/string.Empty
                });
            }
            else if (!GlobalResource.RevitModels[revitType].Any(x => x.Id == id))
            {
                GlobalResource.RevitModels[revitType].Add(new ElementModel
                {
                    Name = elem.Name,
                    Id = id,
                    LinkIds = /*string.Join(",", linkedIds)*/string.Empty
                });
            }
        }
 
        public static void RegistViewEvent(ExternalCommandData commandData)
        {
            commandData.Application.ViewActivated -= Application_ViewActivated;
            commandData.Application.ViewActivated += Application_ViewActivated;
        }
 
        private static void Application_ViewActivated(object sender, ViewActivatedEventArgs e)
        {
            GlobalResource.CurrentDocument = e.CurrentActiveView.Document;
        }
 
        public static List<Element> GetModelElements(this Document doc) 
        {
            if (!doc.IsValidObject)
            {
                return new List<Element>();
            }
            //FilteredElementCollector collector = null;
            //foreach (Category cate in doc.Settings.Categories)
            //{
            //    if (cate.CategoryType != CategoryType.Model)
            //    {
            //        continue;
            //    }
            //    var subCollector = new FilteredElementCollector(doc);
            //    subCollector.OfCategory((BuiltInCategory)(cate.Id.IntegerValue));
            //    subCollector.WhereElementIsNotElementType();
            //    if (collector == null)
            //    {
            //        collector = subCollector;
            //    }
            //    else
            //    {
            //        collector.UnionWith(subCollector);
            //    }
            //}
            //
            var collector1 = new FilteredElementCollector(doc).
                OfCategory(BuiltInCategory.OST_PipeCurves).
                WhereElementIsNotElementType();
 
            var collector2 = new FilteredElementCollector(doc).
                OfClass(typeof(FamilyInstance)).
                WhereElementIsNotElementType();
 
            var elements1 = collector1.ToElements()?.ToList();
            var elements2 = collector2.ToElements()?.ToList();
            elements1.AddRange(elements2);
 
            elements1 = elements1.Where(x => x.Category.CategoryType == CategoryType.Model).ToList();
            elements1 = elements1.Where(x => x.get_BoundingBox(null) != null).ToList();
 
            return elements1;
        }
 
        public static void AddUnhandledElementsToCache()
        {
            var elements = GlobalResource.CurrentDocument.GetModelElements();
            foreach (var element in elements) 
            {
                var id = element.Id.IntegerValue.ToString();
                if (GlobalResource.ElementIds.Contains(id))
                {
                    continue;
                }
                RevitMepCategoryService.MatchElement(element);
            }
            UpdateLinkerInfo(GlobalResource.CurrentDocument);
        }
    }
}