프로젝트

일반

사용자정보

통계
| 개정판:

hytos / DTI_PID / ID2PSN / Document.cs @ 6b9e7a56

이력 | 보기 | 이력해설 | 다운로드 (23.4 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
        public Document(string filePath)
26
        {
27
            FilePath = filePath;
28
            ReadFile();
29
            SetRelationItem();
30
            SetGroup();
31
        }
32
        private void ReadFile()
33
        {
34
            XElement xml = XElement.Load(FilePath);
35
            FileName = Path.GetFileNameWithoutExtension(FilePath);
36
            DrawingName = xml.Element("DWGNAME").Value;
37
            Items = new List<Item>();
38
            LineNumbers = new List<LineNumber>();
39
            RunInfos = new List<RunInfo>();
40
            Groups = new List<Group>();
41
            SegmentBreaks = new List<Item>();
42
            Equipments = new List<Equipment>();
43

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

    
53

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

    
59
            SetRunInfo(xml);
60
        }
61
        private Item GetSymbolItem(XElement element)
62
        {
63
            Item item = new Item();
64
            item.UID = element.Element("UID").Value;
65
            item.Name = element.Element("NAME").Value;
66
            item.Type = element.Element("TYPE").Value;
67
            item.Owner = element.Element("OWNER").Value;
68
            item.Parent = element.Element("PARENT").Value;
69
            string[] sPoint = element.Element("ORIGINALPOINT").Value.Split(new char[] { ',' });
70
            item.POINT = new double[] { Convert.ToDouble(sPoint[0]), Convert.ToDouble(sPoint[1]) };
71
            item.ANGLE = element.Element("ANGLE").Value;
72
            item.Relations = GetRelations(element.Element("CONNECTORS"));
73
            item.Attributes = GetAttributes(element.Element("SYMBOLATTRIBUTES"));
74
            item.ItemType = ItemType.Symbol;
75

    
76
            if (item.Type.ToUpper().Equals("PIPING OPC'S"))
77
                item.SubItemType = SubItemType.OPC;
78
            else if (item.Type.ToUpper().Equals("NOZZLES"))
79
                item.SubItemType = SubItemType.Nozzle;
80

    
81
            return item;
82
        }
83
        private Item GetLineItem(XElement element)
84
        {
85
            Item item = new Item();
86
            item.UID = element.Element("UID").Value;
87
            item.Name = element.Element("TYPE").Value;
88
            item.Type = element.Element("TYPE").Value;
89
            item.Owner = element.Attribute("OWNER").Value;
90

    
91
            item.Relations = GetRelations(element.Element("CONNECTORS"));
92
            item.Attributes = GetAttributes(element.Element("SYMBOLATTRIBUTES"));
93
            item.ItemType = ItemType.Line;
94

    
95
            return item;
96
        }
97
        private LineNumber GetLineNumber(XElement element)
98
        {
99
            LineNumber lineNumber = new LineNumber();
100
            lineNumber.UID = element.Element("UID").Value;
101
            lineNumber.Name = element.Element("TEXT").Value;
102
            lineNumber.Attributes = GetAttributes(element);
103
            return lineNumber;
104
        }
105
        private Equipment GetEquipment(XElement element)
106
        {
107
            Equipment item = new Equipment();
108
            item.UID = element.Element("UID").Value;
109
            string[] sPoint = element.Element("POINT").Value.Split(new char[] { '/' });
110
            foreach (var sp in sPoint)
111
            {
112
                string[] xy = sp.Split(new char[] { ',' });
113
                item.POINT.Add(new double[] { Convert.ToDouble(xy[0]), Convert.ToDouble(xy[1]) });
114
            }
115

    
116
            return item;
117
        }
118
        private List<Relation> GetRelations(XElement element)
119
        {
120
            List<Relation> result = new List<Relation>();
121
            foreach (XElement item in element.Elements("CONNECTOR"))
122
            {
123
                string[] sPoint = item.Element("SCENECONNECTPOINT").Value.Split(new char[] { ',' });
124
                result.Add(new Relation()
125
                {
126
                    UID = item.Element("CONNECTEDITEM").Value,
127
                    Point = new double[] { Convert.ToDouble(sPoint[0]), Convert.ToDouble(sPoint[1]) }
128
                });
129
            }
130

    
131
            return result;
132
        }
133
        private List<Attribute> GetAttributes(XElement element)
134
        {
135
            List<Attribute> result = new List<Attribute>();
136
            foreach (XElement item in element.Elements("ATTRIBUTE"))
137
            {
138
                result.Add(new Attribute()
139
                {
140
                    UID = item.Attribute("UID").Value,
141
                    Name = item.Attribute("Attribute").Value,
142
                    DisplayName = item.Attribute("DisplayAttribute").Value,
143
                    Value = item.Value
144
                });
145
            }
146
            return result;
147
        }
148
        private void SetRelationItem()
149
        {
150
            foreach (Item item in Items)
151
                foreach (Relation relation in item.Relations)
152
                    relation.Item = Items.Find(x => x.UID.Equals(relation.UID));
153
        }
154
        private void SetRunInfo(XElement xml)
155
        {
156
            foreach (XElement element in xml.Element("LINENOS").Elements("LINE_NO"))
157
            {
158
                string UID = element.Element("UID").Value;
159
                foreach (XElement run in element.Elements("RUN"))
160
                {
161
                    RunInfo runInfo = new RunInfo() { UID = UID };
162
                    foreach (XElement line in run.Elements("LINE"))
163
                        runInfo.Items.Add(Items.Find(x => x.UID == line.Element("UID").Value));
164
                    foreach (XElement symbol in run.Elements("SYMBOL"))
165
                        runInfo.Items.Add(Items.Find(x => x.UID == symbol.Element("UID").Value));
166
                    RunInfos.Add(runInfo);
167
                }
168
            }
169

    
170
            foreach (XElement element in xml.Element("TRIMLINENOS").Elements("TRIM_LINE_NO"))
171
            {
172
                string UID = element.Element("UID").Value;
173
                foreach (XElement run in element.Elements("RUN"))
174
                {
175
                    RunInfo runInfo = new RunInfo() { UID = UID };
176
                    foreach (XElement line in run.Elements("LINE"))
177
                        runInfo.Items.Add(Items.Find(x => x.UID == line.Element("UID").Value));
178
                    foreach (XElement symbol in run.Elements("SYMBOL"))
179
                        runInfo.Items.Add(Items.Find(x => x.UID == symbol.Element("UID").Value));
180
                    RunInfos.Add(runInfo);
181
                }
182
            }
183
        }
184
        private void SetGroup()
185
        {
186
            List<Item> orderItems = Items.OrderByDescending(x => x.ItemType == ItemType.Line).ToList();
187

    
188
            Dictionary<Item, string> itemDic = new Dictionary<Item, string>();
189
            foreach (Item item in orderItems)
190
                itemDic.Add(item, Guid.NewGuid().ToString());
191

    
192
            foreach (Item item in orderItems)
193
            {
194
                string groupKey = itemDic[item];
195
                if (item.ItemType == ItemType.Line)
196
                {
197
                    GroupingForwardLine();
198
                    GroupingBackwardLine();
199
                }
200

    
201
                void GroupingForwardLine()
202
                {
203
                    Item connItem = item.Relations[1].Item;
204
                    if (connItem != null && IsConnected(connItem, item))
205
                    {
206
                        if (connItem.ItemType == ItemType.Line && item.Equals(connItem.Relations[0].Item))
207
                            ChangeGroupID(itemDic[connItem], groupKey);
208
                        else if (connItem.ItemType == ItemType.Symbol)
209
                        {
210
                            List<Item> allConnItems = GetConnectedLines(connItem);
211
                            allConnItems.Remove(item);
212
                            List<Item> connItems = GetConnectedForwardLines(connItem);
213
                            if (allConnItems.Count.Equals(1) && connItems.Count.Equals(1) && allConnItems[0].Equals(connItems[0]))
214
                            {
215
                                List<Item> connSymbols = GetConnectedSymbols(connItem);
216
                                foreach (Item loopItem in connSymbols)
217
                                    ChangeGroupID(itemDic[loopItem], groupKey);
218
                            }
219
                            else
220
                            {
221
                                List<Item> endItems = new List<Item>();
222
                                Stack<Item> stacks = new Stack<Item>();
223
                                stacks.Push(connItem);
224
                                while (stacks.Count > 0)
225
                                {
226
                                    Item stack = stacks.Pop();
227
                                    if (endItems.Contains(stack))
228
                                        continue;
229
                                    endItems.Add(stack);
230

    
231
                                    if (GetConnectedItemCount(stack) < 3)
232
                                    {
233
                                        ChangeGroupID(itemDic[stack], groupKey);
234
                                        List<Relation> relations = stack.Relations.FindAll(x => x.Item != null && x.Item.ItemType == ItemType.Symbol);
235
                                        foreach (Relation relation in relations)
236
                                            stacks.Push(relation.Item);
237
                                    }
238
                                    else if (IsSameRun(item, stack))
239
                                    {
240
                                        ChangeGroupID(itemDic[stack], groupKey);
241
                                        List<Relation> relations = stack.Relations.FindAll(x => x.Item != null && x.Item.ItemType == ItemType.Symbol && IsSameRun(item, x.Item));
242
                                        foreach (Relation relation in relations)
243
                                            stacks.Push(relation.Item);
244
                                    }
245
                                }
246
                            }
247
                        }
248
                    }
249
                }
250
                void GroupingBackwardLine()
251
                {
252
                    Item connItem = item.Relations[0].Item;
253
                    if (connItem != null && IsConnected(connItem, item))
254
                    {
255
                        if (connItem.ItemType == ItemType.Line && item.Equals(connItem.Relations[1].Item))
256
                            ChangeGroupID(itemDic[connItem], groupKey);
257
                        else if (connItem.ItemType == ItemType.Symbol)
258
                        {
259
                            List<Item> allConnItems = GetConnectedLines(connItem);
260
                            allConnItems.Remove(item);
261
                            List<Item> connItems = GetConnectedBackwardLines(connItem);
262
                            if (allConnItems.Count.Equals(1) && connItems.Count.Equals(1) && allConnItems[0].Equals(connItems[0]))
263
                            {
264
                                List<Item> connSymbols = GetConnectedSymbols(connItem);
265
                                foreach (Item loopItem in connSymbols)
266
                                    ChangeGroupID(itemDic[loopItem], groupKey);
267
                            }
268
                            else
269
                            {
270
                                List<Item> endItems = new List<Item>();
271
                                Stack<Item> stacks = new Stack<Item>();
272
                                stacks.Push(connItem);
273
                                while (stacks.Count > 0)
274
                                {
275
                                    Item stack = stacks.Pop();
276
                                    if (endItems.Contains(stack))
277
                                        continue;
278
                                    endItems.Add(stack);
279

    
280
                                    if (GetConnectedItemCount(stack) < 3)
281
                                    {
282
                                        ChangeGroupID(itemDic[stack], groupKey);
283
                                        List<Relation> relations = stack.Relations.FindAll(x => x.Item != null && x.Item.ItemType == ItemType.Symbol);
284
                                        foreach (Relation relation in relations)
285
                                            stacks.Push(relation.Item);
286
                                    }
287
                                    else if (IsSameRun(item, stack))
288
                                    {
289
                                        ChangeGroupID(itemDic[stack], groupKey);
290
                                        List<Relation> relations = stack.Relations.FindAll(x => x.Item != null && x.Item.ItemType == ItemType.Symbol && IsSameRun(item, x.Item));
291
                                        foreach (Relation relation in relations)
292
                                            stacks.Push(relation.Item);
293
                                    }
294
                                }
295
                            }
296
                        }
297
                    }
298
                }
299
            }
300

    
301
            List<string> endGroupIds = new List<string>();
302
            foreach (var item in itemDic)
303
            {
304
                if (endGroupIds.Contains(item.Value))
305
                    continue;
306
                endGroupIds.Add(item.Value);
307

    
308
                Group group = new Group(this);
309
                group.Items = itemDic.Where(x => x.Value.Equals(item.Value)).Select(x => x.Key).ToList();
310
                group.UID = item.Value;
311
                group.SortItems();
312

    
313
                Groups.Add(group);
314
            }
315

    
316
            #region HeaderSetting
317
            List<HeaderInfo> HeaderInfos = new List<HeaderInfo>();
318
            DataTable dt = DB.SelectHeaderSetting();
319
            foreach (DataRow row in dt.Rows)
320
            {
321
                string groupID = row["GROUP_ID"].ToString();
322
                string desc = row["DESCRIPTION"].ToString();
323
                int index = Convert.ToInt32(row["INDEX"]);
324
                string name = row["NAME"].ToString();
325

    
326
                HeaderInfo headerInfo = HeaderInfos.Find(x => x.UID.Equals(groupID));
327
                if (headerInfo == null)
328
                {
329
                    headerInfo = new HeaderInfo(groupID);
330
                    headerInfo.Description = desc;
331
                    HeaderInfos.Add(headerInfo);
332
                }
333

    
334
                headerInfo.HeaderItems.Add(new HeaderItem()
335
                {
336
                    Index = index,
337
                    Name = name
338
                });
339
            }
340
            foreach (HeaderInfo headerInfo in HeaderInfos)
341
                headerInfo.HeaderItems = headerInfo.HeaderItems.OrderBy(x => x.Index).ToList();
342

    
343
            foreach (Group group in Groups)
344
            {
345
                List<HeaderInfo> endInfos = new List<HeaderInfo>();
346
                bool bFind = false;
347
                for (int i = 0; i < group.Items.Count; i++)
348
                {
349
                    Item item = group.Items[i];
350
                    foreach (HeaderInfo header in HeaderInfos)
351
                    {
352
                        if (endInfos.Contains(header))
353
                            continue;
354

    
355
                        if (!header.HeaderItems[i].Name.Equals(item.Name))
356
                        {
357
                            endInfos.Add(header);
358
                            continue;
359
                        }
360

    
361
                        if (header.HeaderItems.Count.Equals(i + 1))
362
                        {
363
                            for (int j = 0; j < i + 1; j++)
364
                                group.Items[j].SubItemType = SubItemType.Header;
365
                            bFind = true;
366
                            break;
367
                        }
368
                    }
369

    
370
                    if (bFind || endInfos.Count.Equals(HeaderInfos.Count))
371
                        break;
372
                }
373

    
374
                endInfos = new List<HeaderInfo>();
375
                bFind = false;
376
                for (int i = 0; i < group.Items.Count; i++)
377
                {
378
                    Item item = group.Items[group.Items.Count - i - 1];
379
                    foreach (HeaderInfo header in HeaderInfos)
380
                    {
381
                        if (endInfos.Contains(header))
382
                            continue;
383

    
384
                        if (!header.HeaderItems[i].Name.Equals(item.Name))
385
                        {
386
                            endInfos.Add(header);
387
                            continue;
388
                        }
389

    
390
                        if (header.HeaderItems.Count.Equals(i + 1))
391
                        {
392
                            for (int j = 0; j < i + 1; j++)
393
                                group.Items[group.Items.Count - j - 1].SubItemType = SubItemType.Header;
394
                            bFind = true;
395
                            break;
396
                        }
397
                    }
398

    
399
                    if (bFind || endInfos.Count.Equals(HeaderInfos.Count))
400
                        break;
401
                }
402
            }
403
            #endregion
404

    
405
            int GetConnectedItemCount(Item item)
406
            {
407
                return item.Relations.FindAll(x => x.Item != null).Count;
408
            }
409

    
410
            List<Item> GetConnectedLines(Item item)
411
            {
412
                List<Item> result = new List<Item>();
413
                List<Item> end = new List<Item>();
414
                Stack<Item> stacks = new Stack<Item>();
415
                stacks.Push(item);
416
                while (stacks.Count > 0)
417
                {
418
                    Item stack = stacks.Pop();
419
                    if (end.Contains(stack) || stack.ItemType != ItemType.Symbol)
420
                        continue;
421
                    end.Add(stack);
422

    
423
                    List<Relation> relations = stack.Relations.FindAll(x => x.Item != null && x.Item.ItemType == ItemType.Symbol);
424
                    foreach (Relation relation in relations)
425
                        stacks.Push(relation.Item);
426

    
427
                    relations = stack.Relations.FindAll(x => x.Item != null && x.Item.ItemType == ItemType.Line);
428
                    foreach (Relation relation in relations)
429
                    {
430
                        result.Add(relation.Item);
431
                        end.Add(relation.Item);
432
                    }
433
                }
434

    
435
                return result;
436
            }
437

    
438
            List<Item> GetConnectedForwardLines(Item item)
439
            {
440
                List<Item> result = new List<Item>();
441
                List<Item> end = new List<Item>();
442
                Stack<Item> stacks = new Stack<Item>();
443
                stacks.Push(item);
444
                while (stacks.Count > 0)
445
                {
446
                    Item stack = stacks.Pop();
447
                    if (end.Contains(stack) || stack.ItemType != ItemType.Symbol)
448
                        continue;
449
                    end.Add(stack);
450

    
451
                    List<Relation> relations = stack.Relations.FindAll(x => x.Item != null && x.Item.ItemType == ItemType.Symbol);
452
                    foreach (Relation relation in relations)
453
                        stacks.Push(relation.Item);
454

    
455
                    relations = stack.Relations.FindAll(x => x.Item != null && x.Item.ItemType == ItemType.Line);
456
                    foreach (Relation relation in relations)
457
                    {
458
                        if (relation.Item.Relations.FindIndex(x => x.Item != null && x.Item.Equals(stack)).Equals(0))
459
                            result.Add(relation.Item);
460

    
461
                        end.Add(relation.Item);
462
                    }
463
                }
464

    
465
                return result;
466
            }
467

    
468
            List<Item> GetConnectedBackwardLines(Item item)
469
            {
470
                List<Item> result = new List<Item>();
471
                List<Item> end = new List<Item>();
472
                Stack<Item> stacks = new Stack<Item>();
473
                stacks.Push(item);
474
                while (stacks.Count > 0)
475
                {
476
                    Item stack = stacks.Pop();
477
                    if (end.Contains(stack) || stack.ItemType != ItemType.Symbol)
478
                        continue;
479
                    end.Add(stack);
480

    
481
                    List<Relation> relations = stack.Relations.FindAll(x => x.Item != null && x.Item.ItemType == ItemType.Symbol);
482
                    foreach (Relation relation in relations)
483
                        stacks.Push(relation.Item);
484

    
485
                    relations = stack.Relations.FindAll(x => x.Item != null && x.Item.ItemType == ItemType.Line);
486
                    foreach (Relation relation in relations)
487
                    {
488
                        if (relation.Item.Relations.FindIndex(x => x.Item != null && x.Item.Equals(stack)).Equals(1))
489
                            result.Add(relation.Item);
490

    
491
                        end.Add(relation.Item);
492
                    }
493
                }
494

    
495
                return result;
496
            }
497

    
498
            List<Item> GetConnectedSymbols(Item item)
499
            {
500
                List<Item> result = new List<Item>();
501
                List<Item> end = new List<Item>();
502
                Stack<Item> stacks = new Stack<Item>();
503
                stacks.Push(item);
504
                while (stacks.Count > 0)
505
                {
506
                    Item stack = stacks.Pop();
507
                    if (end.Contains(stack) || stack.ItemType != ItemType.Symbol)
508
                        continue;
509
                    end.Add(stack);
510
                    result.Add(stack);
511
                    List<Relation> relations = stack.Relations.FindAll(x => x.Item != null && x.Item.ItemType == ItemType.Symbol);
512
                    foreach (Relation relation in relations)
513
                        stacks.Push(relation.Item);
514
                }
515

    
516
                return result;
517
            }
518

    
519
            void ChangeGroupID(string from, string to)
520
            {
521
                if (from.Equals(to))
522
                    return;
523

    
524
                List<Item> changeItems = new List<Item>();
525
                foreach (var _item in itemDic)
526
                    if (_item.Value.Equals(from))
527
                        changeItems.Add(_item.Key);
528
                foreach (var _item in changeItems)
529
                    itemDic[_item] = to;
530
            }
531

    
532
            bool IsConnected(Item item1, Item item2)
533
            {
534
                if (item1.Relations.Find(x => x.UID.Equals(item2.UID)) != null &&
535
                    item2.Relations.Find(x => x.UID.Equals(item1.UID)) != null)
536
                    return true;
537
                else
538
                    return false;
539
            }
540

    
541
            bool IsSameRun(Item item1, Item item2)
542
            {
543
                return RunInfos.Find(x => x.Items.Contains(item1) && x.Items.Contains(item2)) != null ? true : false;
544
            }
545
        }
546
    }
547
}
클립보드 이미지 추가 (최대 크기: 500 MB)