프로젝트

일반

사용자정보

통계
| 브랜치(Branch): | 개정판:

hytos / DTI_PID / ID2PSN / Topology.cs @ 0dae5645

이력 | 보기 | 이력해설 | 다운로드 (20.8 KB)

1
using System;
2
using System.Collections.Generic;
3
using System.IO;
4
using System.Linq;
5
using System.Text;
6
using System.Threading.Tasks;
7
using System.Xml.Linq;
8

    
9
namespace ID2PSN
10
{
11
    public class Topology
12
    {
13
        public string DrawingName { get; set; }
14
        public string FileName { get; set; }
15
        public string FilePath { get; set; }
16
        public List<Item> Items { get; set; }
17
        public List<LineNumber> LineNumbers { get; set; }
18
        public List<RunInfo> RunInfos { get; set; }
19

    
20
        public Topology(string filePath)
21
        {
22
            FilePath = filePath;
23
            ReadFile();
24
            SetRelationItem();
25
            SetTopology();
26
        }
27
        private void ReadFile()
28
        {
29
            XElement xml = XElement.Load(FilePath);
30
            FileName = Path.GetFileNameWithoutExtension(FilePath);
31
            DrawingName = xml.Element("DWGNAME").Value;
32
            Items = new List<Item>();
33
            LineNumbers = new List<LineNumber>();
34
            RunInfos = new List<RunInfo>();
35

    
36
            foreach (XElement element in xml.Element("SYMBOLS").Elements("SYMBOL"))
37
                Items.Add(GetSymbolItem(element));
38
            foreach (XElement element in xml.Element("LINEINFOS").Elements("LINE"))
39
                Items.Add(GetLineItem(element));
40
            foreach (XElement element in xml.Element("LINENOS").Elements("LINE_NO"))
41
                LineNumbers.Add(GetLineNumber(element));
42

    
43
            SetRunInfo(xml);
44
        }
45
        private Item GetSymbolItem(XElement element)
46
        {
47
            Item item = new Item();
48
            item.UID = element.Element("UID").Value;
49
            item.Name = element.Element("NAME").Value;
50
            item.Type = element.Element("TYPE").Value;
51
            item.Owner = element.Element("OWNER").Value;
52

    
53
            item.Relations = GetRelations(element.Element("CONNECTORS"));
54
            item.Attributes = GetAttributes(element.Element("SYMBOLATTRIBUTES"));
55
            item.ItemType = ItemType.Symbol;
56

    
57
            return item;
58
        }
59
        private Item GetLineItem(XElement element)
60
        {
61
            Item item = new Item();
62
            item.UID = element.Element("UID").Value;
63
            item.Name = element.Element("TYPE").Value;
64
            item.Type = element.Element("TYPE").Value;
65
            item.Owner = element.Attribute("OWNER").Value;
66

    
67
            item.Relations = GetRelations(element.Element("CONNECTORS"));
68
            item.Attributes = GetAttributes(element.Element("SYMBOLATTRIBUTES"));
69
            item.ItemType = ItemType.Line;
70

    
71
            return item;
72
        }
73
        private LineNumber GetLineNumber(XElement element)
74
        {
75
            LineNumber lineNumber = new LineNumber();
76
            lineNumber.UID = element.Element("UID").Value;
77
            lineNumber.Name = element.Element("TEXT").Value;
78
            lineNumber.Attributes = GetAttributes(element);
79
            return lineNumber;
80
        }
81
        private List<Relation> GetRelations(XElement element)
82
        {
83
            List<Relation> result = new List<Relation>();
84
            foreach (XElement item in element.Elements("CONNECTOR"))
85
            {
86
                string[] sPoint = item.Element("SCENECONNECTPOINT").Value.Split(new char[] { ',' });
87
                result.Add(new Relation()
88
                {
89
                    UID = item.Element("CONNECTEDITEM").Value,
90
                    Point = new double[] { Convert.ToDouble(sPoint[0]) , Convert.ToDouble(sPoint[1]) }
91
                });
92
            }
93
            
94
            return result;
95
        }
96
        private List<Attribute> GetAttributes(XElement element)
97
        {
98
            List<Attribute> result = new List<Attribute>();
99
            foreach (XElement item in element.Elements("ATTRIBUTE"))
100
            {
101
                result.Add(new Attribute()
102
                {
103
                    UID = item.Attribute("UID").Value,
104
                    Name = item.Attribute("Attribute").Value,
105
                    DisplayName = item.Attribute("DisplayAttribute").Value,
106
                    Value = item.Value
107
                });
108
            }
109
            return result;
110
        }
111
        private void SetRelationItem()
112
        {
113
            foreach (Item item in Items)
114
                foreach (Relation relation in item.Relations)
115
                    relation.Item = Items.Find(x => x.UID.Equals(relation.UID));
116
        }
117
        private void SetTopology()
118
        {
119
            List<Item> orderItems = Items.OrderByDescending(x => x.ItemType == ItemType.Line).ToList();
120

    
121
            Dictionary<Item, string> groupDic = new Dictionary<Item, string>();
122
            foreach (Item item in orderItems)
123
                groupDic.Add(item, Guid.NewGuid().ToString());
124

    
125
            foreach (Item item in orderItems)
126
            {
127
                string groupKey = groupDic[item];
128
                if (item.ItemType == ItemType.Line)
129
                {
130
                    GroupingForwardLine();
131
                    GroupingBackwardLine();
132
                }
133
                
134
                void GroupingForwardLine()
135
                {
136
                    Item connItem = item.Relations[1].Item;
137
                    if (connItem != null && IsConnected(connItem, item))
138
                    {
139
                        if (connItem.ItemType == ItemType.Line && item.Equals(connItem.Relations[0].Item))
140
                            ChangeGroupID(groupDic[connItem], groupKey);
141
                        else if (connItem.ItemType == ItemType.Symbol)
142
                        {
143
                            List<Item> allConnItems = GetConnectedLines(connItem);
144
                            allConnItems.Remove(item);
145
                            List<Item> connItems = GetConnectedForwardLines(connItem);
146
                            if (allConnItems.Count.Equals(1) && connItems.Count.Equals(1) && allConnItems[0].Equals(connItems[0]))
147
                            {
148
                                List<Item> connSymbols = GetConnectedSymbols(connItem);
149
                                foreach (Item loopItem in connSymbols)
150
                                    ChangeGroupID(groupDic[loopItem], groupKey);
151
                            }
152
                            else
153
                            {
154
                                List<Item> endItems = new List<Item>();
155
                                Stack<Item> stacks = new Stack<Item>();
156
                                stacks.Push(connItem);
157
                                while (stacks.Count > 0)
158
                                {
159
                                    Item stack = stacks.Pop();
160
                                    if (endItems.Contains(stack))
161
                                        continue;
162
                                    endItems.Add(stack);
163

    
164
                                    if (GetConnectedItemCount(stack) < 3)
165
                                    {
166
                                        ChangeGroupID(groupDic[stack], groupKey);
167
                                        List<Relation> relations = stack.Relations.FindAll(x => x.Item != null && x.Item.ItemType == ItemType.Symbol);
168
                                        foreach (Relation relation in relations)
169
                                            stacks.Push(relation.Item);
170
                                    }
171
                                    else if(IsSameRun(item, stack))
172
                                    {
173
                                        ChangeGroupID(groupDic[stack], groupKey);
174
                                        List<Relation> relations = stack.Relations.FindAll(x => x.Item != null && x.Item.ItemType == ItemType.Symbol && IsSameRun(item, x.Item));
175
                                        foreach (Relation relation in relations)
176
                                            stacks.Push(relation.Item);
177
                                    }
178
                                }
179
                            }
180
                        }
181
                    }
182
                }
183
                void GroupingBackwardLine()
184
                {
185
                    Item connItem = item.Relations[0].Item;
186
                    if (connItem != null && IsConnected(connItem, item))
187
                    {
188
                        if (connItem.ItemType == ItemType.Line && item.Equals(connItem.Relations[1].Item))
189
                            ChangeGroupID(groupDic[connItem], groupKey);
190
                        else if (connItem.ItemType == ItemType.Symbol)
191
                        {
192
                            List<Item> allConnItems = GetConnectedLines(connItem);
193
                            allConnItems.Remove(item);
194
                            List<Item> connItems = GetConnectedBackwardLines(connItem);
195
                            if (allConnItems.Count.Equals(1) && connItems.Count.Equals(1) && allConnItems[0].Equals(connItems[0]))
196
                            {
197
                                List<Item> connSymbols = GetConnectedSymbols(connItem);
198
                                foreach (Item loopItem in connSymbols)
199
                                    ChangeGroupID(groupDic[loopItem], groupKey);
200
                            }
201
                            else
202
                            {
203
                                List<Item> endItems = new List<Item>();
204
                                Stack<Item> stacks = new Stack<Item>();
205
                                stacks.Push(connItem);
206
                                while (stacks.Count > 0)
207
                                {
208
                                    Item stack = stacks.Pop();
209
                                    if (endItems.Contains(stack))
210
                                        continue;
211
                                    endItems.Add(stack);
212

    
213
                                    if (GetConnectedItemCount(stack) < 3)
214
                                    {
215
                                        ChangeGroupID(groupDic[stack], groupKey);
216
                                        List<Relation> relations = stack.Relations.FindAll(x => x.Item != null && x.Item.ItemType == ItemType.Symbol);
217
                                        foreach (Relation relation in relations)
218
                                            stacks.Push(relation.Item);
219
                                    }
220
                                    else if (IsSameRun(item, stack))
221
                                    {
222
                                        ChangeGroupID(groupDic[stack], groupKey);
223
                                        List<Relation> relations = stack.Relations.FindAll(x => x.Item != null && x.Item.ItemType == ItemType.Symbol && IsSameRun(item, x.Item));
224
                                        foreach (Relation relation in relations)
225
                                            stacks.Push(relation.Item);
226
                                    }
227
                                }
228
                            }
229
                        }
230
                    }
231
                }
232
                #region before
233
                //if (item.ItemType == ItemType.Line)
234
                //{
235
                //    Item startItem = item.Relations[0].Item;
236
                //    Item endItem = item.Relations[1].Item;
237
                //    if (startItem != null && startItem.ItemType == ItemType.Line &&
238
                //        startItem.Relations[1] != null && startItem.Relations[1].Item.Equals(item) &&
239
                //        !groupDic[startItem].Equals(groupKey))
240
                //        ChangeGroupID(groupDic[startItem], groupKey);
241
                //    if (endItem != null && endItem.ItemType == ItemType.Line &&
242
                //        endItem.Relations[0] != null && endItem.Relations[0].Item.Equals(item) &&
243
                //        !groupDic[endItem].Equals(groupKey))
244
                //        ChangeGroupID(groupDic[endItem], groupKey);
245
                //}
246
                //else if (item.ItemType == ItemType.Symbol)
247
                //{
248
                //    int connCount = GetConnectedItemCount(item);
249
                //    if (connCount.Equals(1))
250
                //    {
251
                //        Item connItem = item.Relations.Find(x => x.Item != null).Item;
252
                //        if (!groupDic[connItem].Equals(groupKey))
253
                //            ChangeGroupID(groupDic[connItem], groupKey);
254
                //    }
255
                //    else if (connCount < 3)
256
                //    {
257
                //        List<Item> connItems = item.Relations.FindAll(x => x.Item != null).Select(x => x.Item).ToList();
258
                //        foreach (Item connItem in connItems)
259
                //        {
260
                //            if (GetConnectedItemCount)
261
                //            {
262

    
263
                //            }
264
                //        }
265
                //    }
266
                //    else
267
                //    {
268

    
269
                //    }
270
                //}
271
                //else
272
                //    throw new Exception("logic error!");
273
                #endregion
274
            }
275

    
276
            #region before
277
            //List<Item> endItems = new List<Item>();
278
            //foreach (Item item in orderItems)
279
            //{
280
            //    Stack<Item> stacks = new Stack<Item>();
281
            //    stacks.Push(item);
282
            //    while (stacks.Count > 0)
283
            //    {
284
            //        Item stack = stacks.Pop();
285
            //        if (endItems.Contains(stack))
286
            //            continue;
287
            //        endItems.Add(stack);
288

    
289
            //        if (stack.ItemType == ItemType.Line)
290
            //        {
291
            //            Item startItem = stack.Relations[0].Item;
292
            //            Item endItem = stack.Relations[1].Item;
293
            //            if (startItem != null && startItem.ItemType == ItemType.Line &&
294
            //                startItem.Relations[1] != null && startItem.Relations[1].Item.Equals(stack))
295
            //            {
296

    
297
            //            }
298
            //            if (endItem != null && endItem.ItemType == ItemType.Line &&
299
            //                endItem.Relations[0] != null && endItem.Relations[0].Item.Equals(stack))
300
            //            {
301

    
302
            //            }
303
            //        }
304
            //        else if (stack.ItemType == ItemType.Symbol)
305
            //        {
306
            //            int connCount = GetConnectedItemCount(stack);
307
            //            if (connCount < 3)
308
            //            {
309

    
310
            //            }
311
            //        }
312
            //        else
313
            //            throw new Exception("logic error!");
314
            //    }
315
            //}
316
            #endregion
317

    
318
            int GetConnectedItemCount(Item item)
319
            {
320
                return item.Relations.FindAll(x => x.Item != null).Count;
321
            }
322

    
323
            List<Item> GetConnectedLines(Item item)
324
            {
325
                List<Item> result = new List<Item>();
326
                List<Item> end = new List<Item>();
327
                Stack<Item> stacks = new Stack<Item>();
328
                stacks.Push(item);
329
                while (stacks.Count > 0)
330
                {
331
                    Item stack = stacks.Pop();
332
                    if (end.Contains(stack) || stack.ItemType != ItemType.Symbol)
333
                        continue;
334
                    end.Add(stack);
335

    
336
                    List<Relation> relations = stack.Relations.FindAll(x => x.Item != null && x.Item.ItemType == ItemType.Symbol);
337
                    foreach (Relation relation in relations)
338
                        stacks.Push(relation.Item);
339

    
340
                    relations = stack.Relations.FindAll(x => x.Item != null && x.Item.ItemType == ItemType.Line);
341
                    foreach (Relation relation in relations)
342
                    {
343
                        result.Add(relation.Item);
344
                        end.Add(relation.Item);
345
                    }
346
                }
347

    
348
                return result;
349
            }
350

    
351
            List<Item> GetConnectedForwardLines(Item item)
352
            {
353
                List<Item> result = new List<Item>();
354
                List<Item> end = new List<Item>();
355
                Stack<Item> stacks = new Stack<Item>();
356
                stacks.Push(item);
357
                while (stacks.Count > 0)
358
                {
359
                    Item stack = stacks.Pop();
360
                    if (end.Contains(stack) || stack.ItemType != ItemType.Symbol)
361
                        continue;
362
                    end.Add(stack);
363

    
364
                    List<Relation> relations = stack.Relations.FindAll(x => x.Item != null && x.Item.ItemType == ItemType.Symbol);
365
                    foreach (Relation relation in relations)
366
                        stacks.Push(relation.Item);
367

    
368
                    relations = stack.Relations.FindAll(x => x.Item != null && x.Item.ItemType == ItemType.Line);
369
                    foreach (Relation relation in relations)
370
                    {
371
                        if (relation.Item.Relations.FindIndex(x => x.Item != null && x.Item.Equals(stack)).Equals(0))
372
                            result.Add(relation.Item);
373

    
374
                        end.Add(relation.Item);
375
                    }
376
                }
377

    
378
                return result;
379
            }
380

    
381
            List<Item> GetConnectedBackwardLines(Item item)
382
            {
383
                List<Item> result = new List<Item>();
384
                List<Item> end = new List<Item>();
385
                Stack<Item> stacks = new Stack<Item>();
386
                stacks.Push(item);
387
                while (stacks.Count > 0)
388
                {
389
                    Item stack = stacks.Pop();
390
                    if (end.Contains(stack) || stack.ItemType != ItemType.Symbol)
391
                        continue;
392
                    end.Add(stack);
393

    
394
                    List<Relation> relations = stack.Relations.FindAll(x => x.Item != null && x.Item.ItemType == ItemType.Symbol);
395
                    foreach (Relation relation in relations)
396
                        stacks.Push(relation.Item);
397

    
398
                    relations = stack.Relations.FindAll(x => x.Item != null && x.Item.ItemType == ItemType.Line);
399
                    foreach (Relation relation in relations)
400
                    {
401
                        if (relation.Item.Relations.FindIndex(x => x.Item != null && x.Item.Equals(stack)).Equals(1))
402
                            result.Add(relation.Item);
403

    
404
                        end.Add(relation.Item);
405
                    }
406
                }
407

    
408
                return result;
409
            }
410

    
411
            List<Item> GetConnectedSymbols(Item item)
412
            {
413
                List<Item> result = new List<Item>();
414
                List<Item> end = new List<Item>();
415
                Stack<Item> stacks = new Stack<Item>();
416
                stacks.Push(item);
417
                while (stacks.Count > 0)
418
                {
419
                    Item stack = stacks.Pop();
420
                    if (end.Contains(stack) || stack.ItemType != ItemType.Symbol)
421
                        continue;
422
                    end.Add(stack);
423
                    result.Add(stack);
424
                    List<Relation> relations = stack.Relations.FindAll(x => x.Item != null && x.Item.ItemType == ItemType.Symbol);
425
                    foreach (Relation relation in relations)
426
                        stacks.Push(relation.Item);
427
                }
428

    
429
                return result;
430
            }
431

    
432
            void ChangeGroupID(string from, string to)
433
            {
434
                if (from.Equals(to))
435
                    return;
436
                
437
                List<Item> changeItems = new List<Item>();
438
                foreach (var _item in groupDic)
439
                    if (_item.Value.Equals(from))
440
                        changeItems.Add(_item.Key);
441
                foreach (var _item in changeItems)
442
                    groupDic[_item] = to;
443
            }
444

    
445
            bool IsConnected(Item item1, Item item2)
446
            {
447
                if (item1.Relations.Find(x => x.UID.Equals(item2.UID)) != null &&
448
                    item2.Relations.Find(x => x.UID.Equals(item1.UID)) != null)
449
                    return true;
450
                else
451
                    return false;
452
            }
453

    
454
            bool IsSameRun(Item item1, Item item2)
455
            {
456
                return RunInfos.Find(x => x.Items.Contains(item1) && x.Items.Contains(item2)) != null ? true : false;
457
            }
458
        }
459
        private void SetRunInfo(XElement xml)
460
        {
461
            foreach (XElement element in xml.Element("TRIMLINENOS").Elements("TRIM_LINE_NO"))
462
            {
463
                string UID = element.Element("UID").Value;
464
                int index = 0;
465
                foreach (XElement run in element.Elements("RUN"))
466
                {
467
                    RunInfo runInfo = new RunInfo() { UID = UID };
468
                    runInfo.Index = index;
469
                    foreach (XElement line in run.Elements("LINE"))
470
                        runInfo.Items.Add(Items.Find(x => x.UID == line.Element("UID").Value));
471
                    foreach (XElement symbol in run.Elements("SYMBOL"))
472
                        runInfo.Items.Add(Items.Find(x => x.UID == symbol.Element("UID").Value));
473
                    RunInfos.Add(runInfo);
474
                    index++;
475
                }
476
            }
477
        }
478
    }
479
}
클립보드 이미지 추가 (최대 크기: 500 MB)