프로젝트

일반

사용자정보

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

hytos / DTI_PID / ID2PSN / Document.cs @ 8487ecb2

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

1
using DevExpress.Utils.StructuredStorage.Internal.Reader;
2
using System;
3
using System.Collections.Generic;
4
using System.Data;
5
using System.IO;
6
using System.Linq;
7
using System.Text;
8
using System.Threading.Tasks;
9
using System.Xml.Linq;
10

    
11
namespace ID2PSN
12
{
13
    public class Document
14
    {
15
        public string DrawingName { get; set; }
16
        public string FileName { get; set; }
17
        public string FilePath { get; set; }
18
        public List<Item> Items { get; set; }
19
        public List<LineNumber> LineNumbers { get; set; }
20
        public List<Group> Groups { get; set; }
21
        public List<RunInfo> RunInfos { get; set; }
22
        public List<Item> SegmentBreaks { get; set; }
23
        public List<Equipment> Equipments { get; set; }
24

    
25
        private DataTable equipmentTable = DB.GetEquipmentType();
26

    
27
        public Document(string filePath)
28
        {
29
            FilePath = filePath;
30
            ReadFile();
31
            SetRelationItem();
32
            SetGroup();
33
        }
34
        private void ReadFile()
35
        {
36
            XElement xml = XElement.Load(FilePath);
37
            FileName = Path.GetFileNameWithoutExtension(FilePath);
38
            DrawingName = xml.Element("DWGNAME").Value;
39
            Items = new List<Item>();
40
            LineNumbers = new List<LineNumber>();
41
            RunInfos = new List<RunInfo>();
42
            Groups = new List<Group>();
43
            SegmentBreaks = new List<Item>();
44
            Equipments = new List<Equipment>();
45

    
46
            foreach (XElement element in xml.Element("SYMBOLS").Elements("SYMBOL"))
47
                Items.Add(GetSymbolItem(element));
48
            foreach (XElement element in xml.Element("LINEINFOS").Elements("LINE"))
49
                Items.Add(GetLineItem(element));
50
            foreach (XElement element in xml.Element("LINENOS").Elements("LINE_NO"))
51
                LineNumbers.Add(GetLineNumber(element));
52
            foreach (XElement element in xml.Element("VENDORS").Elements("VENDOR"))
53
                Equipments.Add(GetEquipment(element));
54

    
55

    
56
            List<Item> segmentBreaks = Items.FindAll(x => x.Type.ToUpper() == "Segment Breaks".ToUpper() || x.Type.ToUpper() == "End Break".ToUpper());
57
            SegmentBreaks.AddRange(segmentBreaks);
58
            foreach (var item in segmentBreaks)
59
                Items.Remove(item);
60

    
61
            foreach (DataRow row in equipmentTable.Rows)
62
            {
63
                string type = row["Type"].ToString();
64
                List<Item> equipments = Items.FindAll(x => x.Type == type);
65

    
66
                foreach (Item item in equipments)
67
                {
68
                    Equipments.Add(ConvertEquipment(item));
69
                    Items.Remove(item);
70
                }
71
            }
72

    
73
            SetRunInfo(xml);
74
        }
75
        private Item GetSymbolItem(XElement element)
76
        {
77
            Item item = new Item();
78
            item.UID = element.Element("UID").Value;
79
            item.Name = element.Element("NAME").Value;
80
            item.Type = element.Element("TYPE").Value;
81
            item.Owner = element.Element("OWNER").Value;
82
            item.Parent = element.Element("PARENT").Value;
83
            string[] sPoint = element.Element("ORIGINALPOINT").Value.Split(new char[] { ',' });
84
            item.POINT = new double[] { Convert.ToDouble(sPoint[0]), Convert.ToDouble(sPoint[1]) };
85
            item.ANGLE = element.Element("ANGLE").Value;
86
            item.Relations = GetRelations(element.Element("CONNECTORS"));
87
            item.Attributes = GetAttributes(element.Element("SYMBOLATTRIBUTES"));
88
            item.ItemType = ItemType.Symbol;
89

    
90
            if (item.Type.ToUpper().Equals("PIPING OPC'S"))
91
                item.SubItemType = SubItemType.OPC;
92
            else if (item.Type.ToUpper().Equals("NOZZLES"))
93
                item.SubItemType = SubItemType.Nozzle;
94

    
95
            return item;
96
        }
97
        private Item GetLineItem(XElement element)
98
        {
99
            Item item = new Item();
100
            item.UID = element.Element("UID").Value;
101
            item.Name = element.Element("TYPE").Value;
102
            item.Type = element.Element("TYPE").Value;
103
            item.Owner = element.Attribute("OWNER").Value;
104

    
105
            item.Relations = GetRelations(element.Element("CONNECTORS"));
106
            item.Attributes = GetAttributes(element.Element("SYMBOLATTRIBUTES"));
107
            item.ItemType = ItemType.Line;
108

    
109
            return item;
110
        }
111
        private LineNumber GetLineNumber(XElement element)
112
        {
113
            LineNumber lineNumber = new LineNumber();
114
            lineNumber.UID = element.Element("UID").Value;
115
            lineNumber.Name = element.Element("TEXT").Value;
116
            lineNumber.Attributes = GetAttributes(element);
117
            return lineNumber;
118
        }
119
        private Equipment GetEquipment(XElement element)
120
        {
121
            Equipment item = new Equipment();
122
            item.UID = element.Element("UID").Value;
123
            string[] sPoint = element.Element("POINT").Value.Split(new char[] { '/' });
124
            foreach (var sp in sPoint)
125
            {
126
                string[] xy = sp.Split(new char[] { ',' });
127
                item.POINT.Add(new double[] { Convert.ToDouble(xy[0]), Convert.ToDouble(xy[1]) });
128
            }
129

    
130
            return item;
131
        }
132

    
133
        private Equipment ConvertEquipment(Item item)
134
        {
135
            Equipment equipment = new Equipment();
136
            equipment.Name = item.Name;
137
            equipment.UID = item.UID;
138
            equipment.POINT.Add(item.POINT);
139
            return equipment;
140
        }
141
        private List<Relation> GetRelations(XElement element)
142
        {
143
            List<Relation> result = new List<Relation>();
144
            foreach (XElement item in element.Elements("CONNECTOR"))
145
            {
146
                string[] sPoint = item.Element("SCENECONNECTPOINT").Value.Split(new char[] { ',' });
147
                result.Add(new Relation()
148
                {
149
                    UID = item.Element("CONNECTEDITEM").Value,
150
                    Point = new double[] { Convert.ToDouble(sPoint[0]), Convert.ToDouble(sPoint[1]) }
151
                });
152
            }
153

    
154
            return result;
155
        }
156
        private List<Attribute> GetAttributes(XElement element)
157
        {
158
            List<Attribute> result = new List<Attribute>();
159
            foreach (XElement item in element.Elements("ATTRIBUTE"))
160
            {
161
                result.Add(new Attribute()
162
                {
163
                    UID = item.Attribute("UID").Value,
164
                    Name = item.Attribute("Attribute").Value,
165
                    DisplayName = item.Attribute("DisplayAttribute").Value,
166
                    Value = item.Value
167
                });
168
            }
169
            return result;
170
        }
171
        private void SetRelationItem()
172
        {
173
            foreach (Item item in Items)
174
                foreach (Relation relation in item.Relations)
175
                    relation.Item = Items.Find(x => x.UID.Equals(relation.UID));
176
        }
177
        private void SetRunInfo(XElement xml)
178
        {
179
            foreach (XElement element in xml.Element("LINENOS").Elements("LINE_NO"))
180
            {
181
                string UID = element.Element("UID").Value;
182
                foreach (XElement run in element.Elements("RUN"))
183
                {
184
                    RunInfo runInfo = new RunInfo() { UID = UID };
185
                    foreach (XElement line in run.Elements("LINE"))
186
                        runInfo.Items.Add(Items.Find(x => x.UID == line.Element("UID").Value));
187
                    foreach (XElement symbol in run.Elements("SYMBOL"))
188
                        runInfo.Items.Add(Items.Find(x => x.UID == symbol.Element("UID").Value));
189
                    RunInfos.Add(runInfo);
190
                }
191
            }
192

    
193
            foreach (XElement element in xml.Element("TRIMLINENOS").Elements("TRIM_LINE_NO"))
194
            {
195
                string UID = element.Element("UID").Value;
196
                foreach (XElement run in element.Elements("RUN"))
197
                {
198
                    RunInfo runInfo = new RunInfo() { UID = UID };
199
                    foreach (XElement line in run.Elements("LINE"))
200
                        runInfo.Items.Add(Items.Find(x => x.UID == line.Element("UID").Value));
201
                    foreach (XElement symbol in run.Elements("SYMBOL"))
202
                        runInfo.Items.Add(Items.Find(x => x.UID == symbol.Element("UID").Value));
203
                    RunInfos.Add(runInfo);
204
                }
205
            }
206
        }
207
        private void SetGroup()
208
        {
209
            List<Item> orderItems = Items.OrderByDescending(x => x.ItemType == ItemType.Line).ToList();
210

    
211
            Dictionary<Item, string> itemDic = new Dictionary<Item, string>();
212
            foreach (Item item in orderItems)
213
                itemDic.Add(item, Guid.NewGuid().ToString());
214

    
215
            foreach (Item item in orderItems)
216
            {
217
                string groupKey = itemDic[item];
218
                if (item.ItemType == ItemType.Line)
219
                {
220
                    GroupingForwardLine();
221
                    GroupingBackwardLine();
222
                }
223

    
224
                void GroupingForwardLine()
225
                {
226
                    Item connItem = item.Relations[1].Item;
227
                    if (connItem != null && IsConnected(connItem, item))
228
                    {
229
                        if (connItem.ItemType == ItemType.Line && item.Equals(connItem.Relations[0].Item))
230
                            ChangeGroupID(itemDic[connItem], groupKey);
231
                        else if (connItem.ItemType == ItemType.Symbol)
232
                        {
233
                            List<Item> allConnItems = GetConnectedLines(connItem);
234
                            allConnItems.Remove(item);
235
                            List<Item> connItems = GetConnectedForwardLines(connItem);
236
                            if (allConnItems.Count.Equals(1) && connItems.Count.Equals(1) && allConnItems[0].Equals(connItems[0]))
237
                            {
238
                                List<Item> connSymbols = GetConnectedSymbols(connItem);
239
                                foreach (Item loopItem in connSymbols)
240
                                    ChangeGroupID(itemDic[loopItem], groupKey);
241
                            }
242
                            else
243
                            {
244
                                List<Item> endItems = new List<Item>();
245
                                Stack<Item> stacks = new Stack<Item>();
246
                                stacks.Push(connItem);
247
                                while (stacks.Count > 0)
248
                                {
249
                                    Item stack = stacks.Pop();
250
                                    if (endItems.Contains(stack))
251
                                        continue;
252
                                    endItems.Add(stack);
253

    
254
                                    if (GetConnectedItemCount(stack) < 3)
255
                                    {
256
                                        ChangeGroupID(itemDic[stack], groupKey);
257
                                        List<Relation> relations = stack.Relations.FindAll(x => x.Item != null && x.Item.ItemType == ItemType.Symbol);
258
                                        foreach (Relation relation in relations)
259
                                            stacks.Push(relation.Item);
260
                                    }
261
                                    else if (IsSameRun(item, stack))
262
                                    {
263
                                        ChangeGroupID(itemDic[stack], groupKey);
264
                                        List<Relation> relations = stack.Relations.FindAll(x => x.Item != null && x.Item.ItemType == ItemType.Symbol && IsSameRun(item, x.Item));
265
                                        foreach (Relation relation in relations)
266
                                            stacks.Push(relation.Item);
267
                                    }
268
                                }
269
                            }
270
                        }
271
                    }
272
                }
273
                void GroupingBackwardLine()
274
                {
275
                    Item connItem = item.Relations[0].Item;
276
                    if (connItem != null && IsConnected(connItem, item))
277
                    {
278
                        if (connItem.ItemType == ItemType.Line && item.Equals(connItem.Relations[1].Item))
279
                            ChangeGroupID(itemDic[connItem], groupKey);
280
                        else if (connItem.ItemType == ItemType.Symbol)
281
                        {
282
                            List<Item> allConnItems = GetConnectedLines(connItem);
283
                            allConnItems.Remove(item);
284
                            List<Item> connItems = GetConnectedBackwardLines(connItem);
285
                            if (allConnItems.Count.Equals(1) && connItems.Count.Equals(1) && allConnItems[0].Equals(connItems[0]))
286
                            {
287
                                List<Item> connSymbols = GetConnectedSymbols(connItem);
288
                                foreach (Item loopItem in connSymbols)
289
                                    ChangeGroupID(itemDic[loopItem], groupKey);
290
                            }
291
                            else
292
                            {
293
                                List<Item> endItems = new List<Item>();
294
                                Stack<Item> stacks = new Stack<Item>();
295
                                stacks.Push(connItem);
296
                                while (stacks.Count > 0)
297
                                {
298
                                    Item stack = stacks.Pop();
299
                                    if (endItems.Contains(stack))
300
                                        continue;
301
                                    endItems.Add(stack);
302

    
303
                                    if (GetConnectedItemCount(stack) < 3)
304
                                    {
305
                                        ChangeGroupID(itemDic[stack], groupKey);
306
                                        List<Relation> relations = stack.Relations.FindAll(x => x.Item != null && x.Item.ItemType == ItemType.Symbol);
307
                                        foreach (Relation relation in relations)
308
                                            stacks.Push(relation.Item);
309
                                    }
310
                                    else if (IsSameRun(item, stack))
311
                                    {
312
                                        ChangeGroupID(itemDic[stack], groupKey);
313
                                        List<Relation> relations = stack.Relations.FindAll(x => x.Item != null && x.Item.ItemType == ItemType.Symbol && IsSameRun(item, x.Item));
314
                                        foreach (Relation relation in relations)
315
                                            stacks.Push(relation.Item);
316
                                    }
317
                                }
318
                            }
319
                        }
320
                    }
321
                }
322
            }
323

    
324
            List<string> endGroupIds = new List<string>();
325
            foreach (var item in itemDic)
326
            {
327
                if (endGroupIds.Contains(item.Value))
328
                    continue;
329
                endGroupIds.Add(item.Value);
330

    
331
                Group group = new Group(this);
332
                group.Items = itemDic.Where(x => x.Value.Equals(item.Value)).Select(x => x.Key).ToList();
333
                group.UID = item.Value;
334
                group.SortItems();
335

    
336
                Groups.Add(group);
337
            }
338

    
339
            #region HeaderSetting
340
            List<HeaderInfo> HeaderInfos = new List<HeaderInfo>();
341
            DataTable dt = DB.SelectHeaderSetting();
342
            foreach (DataRow row in dt.Rows)
343
            {
344
                string groupID = row["GROUP_ID"].ToString();
345
                string desc = row["DESCRIPTION"].ToString();
346
                int index = Convert.ToInt32(row["INDEX"]);
347
                string name = row["NAME"].ToString();
348

    
349
                HeaderInfo headerInfo = HeaderInfos.Find(x => x.UID.Equals(groupID));
350
                if (headerInfo == null)
351
                {
352
                    headerInfo = new HeaderInfo(groupID);
353
                    headerInfo.Description = desc;
354
                    HeaderInfos.Add(headerInfo);
355
                }
356

    
357
                headerInfo.HeaderItems.Add(new HeaderItem()
358
                {
359
                    Index = index,
360
                    Name = name
361
                });
362
            }
363
            foreach (HeaderInfo headerInfo in HeaderInfos)
364
                headerInfo.HeaderItems = headerInfo.HeaderItems.OrderBy(x => x.Index).ToList();
365

    
366
            foreach (Group group in Groups)
367
            {
368
                List<HeaderInfo> endInfos = new List<HeaderInfo>();
369
                bool bFind = false;
370
                for (int i = 0; i < group.Items.Count; i++)
371
                {
372
                    Item item = group.Items[i];
373
                    foreach (HeaderInfo header in HeaderInfos)
374
                    {
375
                        if (endInfos.Contains(header))
376
                            continue;
377

    
378
                        if (!header.HeaderItems[i].Name.Equals(item.Name))
379
                        {
380
                            endInfos.Add(header);
381
                            continue;
382
                        }
383

    
384
                        if (header.HeaderItems.Count.Equals(i + 1))
385
                        {
386
                            for (int j = 0; j < i + 1; j++)
387
                                group.Items[j].SubItemType = SubItemType.Header;
388
                            bFind = true;
389
                            break;
390
                        }
391
                    }
392

    
393
                    if (bFind || endInfos.Count.Equals(HeaderInfos.Count))
394
                        break;
395
                }
396

    
397
                endInfos = new List<HeaderInfo>();
398
                bFind = false;
399
                for (int i = 0; i < group.Items.Count; i++)
400
                {
401
                    Item item = group.Items[group.Items.Count - i - 1];
402
                    foreach (HeaderInfo header in HeaderInfos)
403
                    {
404
                        if (endInfos.Contains(header))
405
                            continue;
406

    
407
                        if (!header.HeaderItems[i].Name.Equals(item.Name))
408
                        {
409
                            endInfos.Add(header);
410
                            continue;
411
                        }
412

    
413
                        if (header.HeaderItems.Count.Equals(i + 1))
414
                        {
415
                            for (int j = 0; j < i + 1; j++)
416
                                group.Items[group.Items.Count - j - 1].SubItemType = SubItemType.Header;
417
                            bFind = true;
418
                            break;
419
                        }
420
                    }
421

    
422
                    if (bFind || endInfos.Count.Equals(HeaderInfos.Count))
423
                        break;
424
                }
425
            }
426
            #endregion
427

    
428
            int GetConnectedItemCount(Item item)
429
            {
430
                return item.Relations.FindAll(x => x.Item != null).Count;
431
            }
432

    
433
            List<Item> GetConnectedLines(Item item)
434
            {
435
                List<Item> result = new List<Item>();
436
                List<Item> end = new List<Item>();
437
                Stack<Item> stacks = new Stack<Item>();
438
                stacks.Push(item);
439
                while (stacks.Count > 0)
440
                {
441
                    Item stack = stacks.Pop();
442
                    if (end.Contains(stack) || stack.ItemType != ItemType.Symbol)
443
                        continue;
444
                    end.Add(stack);
445

    
446
                    List<Relation> relations = stack.Relations.FindAll(x => x.Item != null && x.Item.ItemType == ItemType.Symbol);
447
                    foreach (Relation relation in relations)
448
                        stacks.Push(relation.Item);
449

    
450
                    relations = stack.Relations.FindAll(x => x.Item != null && x.Item.ItemType == ItemType.Line);
451
                    foreach (Relation relation in relations)
452
                    {
453
                        result.Add(relation.Item);
454
                        end.Add(relation.Item);
455
                    }
456
                }
457

    
458
                return result;
459
            }
460

    
461
            List<Item> GetConnectedForwardLines(Item item)
462
            {
463
                List<Item> result = new List<Item>();
464
                List<Item> end = new List<Item>();
465
                Stack<Item> stacks = new Stack<Item>();
466
                stacks.Push(item);
467
                while (stacks.Count > 0)
468
                {
469
                    Item stack = stacks.Pop();
470
                    if (end.Contains(stack) || stack.ItemType != ItemType.Symbol)
471
                        continue;
472
                    end.Add(stack);
473

    
474
                    List<Relation> relations = stack.Relations.FindAll(x => x.Item != null && x.Item.ItemType == ItemType.Symbol);
475
                    foreach (Relation relation in relations)
476
                        stacks.Push(relation.Item);
477

    
478
                    relations = stack.Relations.FindAll(x => x.Item != null && x.Item.ItemType == ItemType.Line);
479
                    foreach (Relation relation in relations)
480
                    {
481
                        if (relation.Item.Relations.FindIndex(x => x.Item != null && x.Item.Equals(stack)).Equals(0))
482
                            result.Add(relation.Item);
483

    
484
                        end.Add(relation.Item);
485
                    }
486
                }
487

    
488
                return result;
489
            }
490

    
491
            List<Item> GetConnectedBackwardLines(Item item)
492
            {
493
                List<Item> result = new List<Item>();
494
                List<Item> end = new List<Item>();
495
                Stack<Item> stacks = new Stack<Item>();
496
                stacks.Push(item);
497
                while (stacks.Count > 0)
498
                {
499
                    Item stack = stacks.Pop();
500
                    if (end.Contains(stack) || stack.ItemType != ItemType.Symbol)
501
                        continue;
502
                    end.Add(stack);
503

    
504
                    List<Relation> relations = stack.Relations.FindAll(x => x.Item != null && x.Item.ItemType == ItemType.Symbol);
505
                    foreach (Relation relation in relations)
506
                        stacks.Push(relation.Item);
507

    
508
                    relations = stack.Relations.FindAll(x => x.Item != null && x.Item.ItemType == ItemType.Line);
509
                    foreach (Relation relation in relations)
510
                    {
511
                        if (relation.Item.Relations.FindIndex(x => x.Item != null && x.Item.Equals(stack)).Equals(1))
512
                            result.Add(relation.Item);
513

    
514
                        end.Add(relation.Item);
515
                    }
516
                }
517

    
518
                return result;
519
            }
520

    
521
            List<Item> GetConnectedSymbols(Item item)
522
            {
523
                List<Item> result = new List<Item>();
524
                List<Item> end = new List<Item>();
525
                Stack<Item> stacks = new Stack<Item>();
526
                stacks.Push(item);
527
                while (stacks.Count > 0)
528
                {
529
                    Item stack = stacks.Pop();
530
                    if (end.Contains(stack) || stack.ItemType != ItemType.Symbol)
531
                        continue;
532
                    end.Add(stack);
533
                    result.Add(stack);
534
                    List<Relation> relations = stack.Relations.FindAll(x => x.Item != null && x.Item.ItemType == ItemType.Symbol);
535
                    foreach (Relation relation in relations)
536
                        stacks.Push(relation.Item);
537
                }
538

    
539
                return result;
540
            }
541

    
542
            void ChangeGroupID(string from, string to)
543
            {
544
                if (from.Equals(to))
545
                    return;
546

    
547
                List<Item> changeItems = new List<Item>();
548
                foreach (var _item in itemDic)
549
                    if (_item.Value.Equals(from))
550
                        changeItems.Add(_item.Key);
551
                foreach (var _item in changeItems)
552
                    itemDic[_item] = to;
553
            }
554

    
555
            bool IsConnected(Item item1, Item item2)
556
            {
557
                if (item1.Relations.Find(x => x.UID.Equals(item2.UID)) != null &&
558
                    item2.Relations.Find(x => x.UID.Equals(item1.UID)) != null)
559
                    return true;
560
                else
561
                    return false;
562
            }
563

    
564
            bool IsSameRun(Item item1, Item item2)
565
            {
566
                return RunInfos.Find(x => x.Items.Contains(item1) && x.Items.Contains(item2)) != null ? true : false;
567
            }
568
        }
569
    }
570
}
클립보드 이미지 추가 (최대 크기: 500 MB)