프로젝트

일반

사용자정보

통계
| 개정판:

hytos / DTI_PID / ID2PSN / Document.cs @ bd86dd48

이력 | 보기 | 이력해설 | 다운로드 (38 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
        public List<TextInfo> TextInfos { get; set; }
25

    
26
        private DataTable symbolNameTable = null;
27
        private DataTable equipmentTable = DB.GetEquipmentType();
28

    
29
        public Document(string filePath, DataTable symbolNameTable)
30
        {
31
            this.symbolNameTable = symbolNameTable;
32
            FilePath = filePath;
33
            // XML Data 정리
34
            ReadFile();
35
            // 해당 소스는 Line-Equipment가 붙어있을 시 가상의 Nozzle Data를 넣어줌 (삼엔 요청)
36
            InsertNozzle();
37
            // ID2 Item Relation 정보 기준으로 Item Object 연결
38
            SetRelationItem();
39
            // Instrument Line이 있는 아이템/group 제거
40
            RemoveItems();
41
            // 속성 상관 없이 drawing 단위의 PSN 생성
42
            SetGroup();
43

    
44
            foreach (var item in Items)
45
                item.Document = this;
46
        }
47
        private void ReadFile()
48
        {
49
            XElement xml = XElement.Load(FilePath);
50
            FileName = Path.GetFileNameWithoutExtension(FilePath);
51
            DrawingName = xml.Element("DWGNAME").Value;
52
            Items = new List<Item>();
53
            LineNumbers = new List<LineNumber>();
54
            RunInfos = new List<RunInfo>();
55
            Groups = new List<Group>();
56
            SegmentBreaks = new List<Item>();
57
            Equipments = new List<Equipment>();
58
            TextInfos = new List<TextInfo>();
59

    
60
            foreach (XElement element in xml.Element("SYMBOLS").Elements("SYMBOL"))
61
                Items.Add(GetSymbolItem(element));
62
            foreach (XElement element in xml.Element("LINEINFOS").Elements("LINE"))
63
                Items.Add(GetLineItem(element));
64
            foreach (XElement element in xml.Element("LINENOS").Elements("LINE_NO"))
65
                LineNumbers.Add(GetLineNumber(element));
66
            foreach (XElement element in xml.Element("VENDORS").Elements("VENDOR"))
67
                Equipments.Add(GetEquipment(element));
68
            foreach (XElement element in xml.Element("TEXTINFOS").Elements("ATTRIBUTE"))
69
                TextInfos.Add(GetTextInfo(element));
70

    
71
            List<Item> segmentBreaks = Items.FindAll(x => x.ID2DBType.ToUpper() == "Segment Breaks".ToUpper() || x.ID2DBType.ToUpper() == "End Break".ToUpper());
72
            SegmentBreaks.AddRange(segmentBreaks);
73
            foreach (var item in segmentBreaks)
74
                Items.Remove(item);
75

    
76
            foreach (DataRow row in equipmentTable.Rows)
77
            {
78
                string type = row["Type"].ToString();
79
                List<Item> equipments = Items.FindAll(x => x.ID2DBType == type);
80

    
81
                foreach (Item item in equipments)
82
                {
83
                    Equipments.Add(ConvertEquipment(item));
84
                    Items.Remove(item);
85
                }
86
            }
87

    
88
            SetRunInfo(xml);
89
        }
90
        private Item GetSymbolItem(XElement element)
91
        {
92
            Item item = new Item();
93
            item.UID = element.Element("UID").Value;
94
            item.Name = element.Element("NAME").Value;
95
            item.ID2DBType = element.Element("TYPE").Value;
96
            item.Owner = element.Element("OWNER").Value;
97
            item.ID2DBName = element.Element("PARENT").Value;
98
            string[] sPoint = element.Element("ORIGINALPOINT").Value.Split(new char[] { ',' });
99
            item.POINT = new double[] { Convert.ToDouble(sPoint[0]), Convert.ToDouble(sPoint[1]) };
100
            item.ANGLE = element.Element("ANGLE").Value;
101
            item.Relations = GetRelations(element.Element("CONNECTORS"));
102
            item.Attributes = GetAttributes(element.Element("SYMBOLATTRIBUTES"));
103
            item.ItemType = ItemType.Symbol;
104
            item.ID2DBCategory = GetID2DBCategory(item.ID2DBType, item.ID2DBName);
105

    
106
            if (item.ID2DBType.ToUpper().Equals("PIPING OPC'S"))
107
                item.SubItemType = SubItemType.OPC;
108
            else if (item.ID2DBType.ToUpper().Equals("NOZZLES"))
109
                item.SubItemType = SubItemType.Nozzle;
110

    
111
            return item;
112
        }
113
        private Item GetLineItem(XElement element)
114
        {
115
            Item item = new Item();
116
            item.UID = element.Element("UID").Value;
117
            item.Name = element.Element("TYPE").Value;
118
            item.ID2DBType = element.Element("TYPE").Value;
119
            item.Owner = element.Attribute("OWNER").Value;
120

    
121
            item.Relations = GetRelations(element.Element("CONNECTORS"));
122
            item.Attributes = GetAttributes(element.Element("SYMBOLATTRIBUTES"));
123
            item.ItemType = ItemType.Line;
124

    
125
            return item;
126
        }
127
        private LineNumber GetLineNumber(XElement element)
128
        {
129
            LineNumber lineNumber = new LineNumber();
130
            lineNumber.UID = element.Element("UID").Value;           
131
            lineNumber.Name = element.Element("TEXT").Value;
132
            lineNumber.Attributes = GetAttributes(element);
133
            return lineNumber;
134
        }
135
        private Equipment GetEquipment(XElement element)
136
        {
137
            Equipment item = new Equipment();
138
            item.UID = element.Element("UID").Value;
139
            item.Attributes = GetAttributes(element.Element("SYMBOLATTRIBUTES"));
140
            string[] sPoint = element.Element("POINT").Value.Split(new char[] { '/' });
141
            foreach (var sp in sPoint)
142
            {
143
                string[] xy = sp.Split(new char[] { ',' });
144
                item.POINT.Add(new double[] { Convert.ToDouble(xy[0]), Convert.ToDouble(xy[1]) });
145
            }
146

    
147
            return item;
148
        }
149
        private TextInfo GetTextInfo(XElement element)
150
        {
151
            TextInfo textInfo = new TextInfo();
152
            textInfo.Value = element.Element("VALUE").Value;
153
            textInfo.Area = element.Element("AREA").Value;
154

    
155
            return textInfo;
156
        }
157
        private Equipment ConvertEquipment(Item item)
158
        {
159
            Equipment equipment = new Equipment();
160
            equipment.Name = item.Name;
161
            equipment.UID = item.UID;
162
            equipment.POINT.Add(item.POINT);
163
            equipment.Attributes.AddRange(item.Attributes);
164
            return equipment;
165
        }
166
        private List<Relation> GetRelations(XElement element)
167
        {
168
            List<Relation> result = new List<Relation>();
169
            foreach (XElement item in element.Elements("CONNECTOR"))
170
            {
171
                string[] sPoint = item.Element("SCENECONNECTPOINT").Value.Split(new char[] { ',' });
172
                result.Add(new Relation()
173
                {
174
                    UID = item.Element("CONNECTEDITEM").Value,
175
                    Point = new double[] { Convert.ToDouble(sPoint[0]), Convert.ToDouble(sPoint[1]) }
176
                });
177
            }
178

    
179
            return result;
180
        }
181
        private List<Attribute> GetAttributes(XElement element)
182
        {
183
            List<Attribute> result = new List<Attribute>();
184
            foreach (XElement item in element.Elements("ATTRIBUTE"))
185
            {
186
               
187
                result.Add(new Attribute()
188
                {
189
                    UID = item.Attribute("UID").Value,
190
                    Name = item.Attribute("Attribute").Value,
191
                    DisplayName = item.Attribute("DisplayAttribute").Value,
192
                    Value = item.Value
193
                });
194
            }
195
            return result;
196
        }
197
        private string GetID2DBCategory(string type, string name)
198
        {
199
            string result = string.Empty;
200
            DataRow[] rows = symbolNameTable.Select(string.Format("Type = '{0}' AND Name = '{1}'", type.Replace("'", "''"), name.Replace("'", "''")));
201
            if (rows.Length.Equals(1))
202
                result = rows.First()["Category"].ToString();
203

    
204
            return result;
205
        }
206
        private void SetRelationItem()
207
        {
208
            foreach (Item item in Items)
209
                foreach (Relation relation in item.Relations)
210
                    relation.Item = Items.Find(x => x.UID.Equals(relation.UID));
211
        }
212
        private void SetRunInfo(XElement xml)
213
        {
214
            foreach (XElement element in xml.Element("LINENOS").Elements("LINE_NO"))
215
            {
216
                string UID = element.Element("UID").Value;
217
                foreach (XElement run in element.Elements("RUN"))
218
                {
219
                    RunInfo runInfo = new RunInfo() { UID = UID };
220
                    foreach (XElement line in run.Elements("LINE"))
221
                        runInfo.Items.Add(Items.Find(x => x.UID == line.Element("UID").Value));
222
                    foreach (XElement symbol in run.Elements("SYMBOL"))
223
                        runInfo.Items.Add(Items.Find(x => x.UID == symbol.Element("UID").Value));
224
                    RunInfos.Add(runInfo);
225
                }
226
            }
227

    
228
            foreach (XElement element in xml.Element("TRIMLINENOS").Elements("TRIM_LINE_NO"))
229
            {
230
                string UID = element.Element("UID").Value;
231
                foreach (XElement run in element.Elements("RUN"))
232
                {
233
                    RunInfo runInfo = new RunInfo() { UID = UID };
234
                    foreach (XElement line in run.Elements("LINE"))
235
                        runInfo.Items.Add(Items.Find(x => x.UID == line.Element("UID").Value));
236
                    foreach (XElement symbol in run.Elements("SYMBOL"))
237
                        runInfo.Items.Add(Items.Find(x => x.UID == symbol.Element("UID").Value));
238
                    RunInfos.Add(runInfo);
239
                }
240
            }
241
        }
242
        private void SetGroup()
243
        {
244
            List<Item> orderItems = Items.OrderByDescending(x => x.ItemType == ItemType.Line).ToList();
245

    
246
            Dictionary<Item, string> itemDic = new Dictionary<Item, string>();
247
            foreach (Item item in orderItems)
248
                itemDic.Add(item, Guid.NewGuid().ToString());
249

    
250
            foreach (Item item in orderItems)
251
            {
252
                string groupKey = itemDic[item];
253
                if (item.ItemType == ItemType.Line)
254
                {
255
                    //if(item.Relations.Count() > 2)
256
                    //{
257
                    //    int i = 2;
258
                    //    GroupingForwardLine(2);
259
                    //    if(item.Relations.Count() >= 4)
260
                    //        GroupingBackwardLine(3);
261
                    //}
262
                    GroupingForwardLine();
263
                    GroupingBackwardLine();
264
                }
265

    
266

    
267
                void GroupingForwardLine(int i = 1)
268
                {
269
                    Item connItem = item.Relations[1].Item;
270
                    if (connItem != null && IsConnected(connItem, item))
271
                    {
272
                        if (connItem.ItemType == ItemType.Line && item.Equals(connItem.Relations[0].Item))
273
                            ChangeGroupID(itemDic[connItem], groupKey);
274
                        else if (connItem.ItemType == ItemType.Symbol)
275
                        {
276
                            List<Item> allConnItems = GetConnectedLines(connItem);
277
                            allConnItems.Remove(item);
278
                            List<Item> connItems = GetConnectedForwardLines(connItem);
279
                            if (allConnItems.Count.Equals(1) && connItems.Count.Equals(1) && allConnItems[0].Equals(connItems[0]))
280
                            {
281
                                List<Item> connSymbols = GetConnectedSymbols(connItem);
282
                                foreach (Item loopItem in connSymbols)
283
                                    ChangeGroupID(itemDic[loopItem], groupKey);
284
                            }
285
                            else
286
                            {
287
                                List<Item> endItems = new List<Item>();
288
                                Stack<Item> stacks = new Stack<Item>();
289
                                stacks.Push(connItem);
290
                                while (stacks.Count > 0)
291
                                {
292
                                    Item stack = stacks.Pop();
293
                                    if (endItems.Contains(stack))
294
                                        continue;
295
                                    endItems.Add(stack);
296

    
297
                                    if (GetConnectedItemCount(stack) < 3)
298
                                    {
299
                                        ChangeGroupID(itemDic[stack], groupKey);
300
                                        List<Relation> relations = stack.Relations.FindAll(x => x.Item != null && x.Item.ItemType == ItemType.Symbol);
301
                                        foreach (Relation relation in relations)
302
                                            stacks.Push(relation.Item);
303
                                    }
304
                                    else if (IsSameRun(item, stack))
305
                                    {
306
                                        ChangeGroupID(itemDic[stack], groupKey);
307
                                        List<Relation> relations = stack.Relations.FindAll(x => x.Item != null && x.Item.ItemType == ItemType.Symbol && IsSameRun(item, x.Item));
308
                                        foreach (Relation relation in relations)
309
                                            stacks.Push(relation.Item);
310
                                    }
311
                                }
312
                            }
313
                        }
314
                    }
315
                }
316
                void GroupingBackwardLine(int i = 0)
317
                {
318
                    Item connItem = item.Relations[0].Item;
319
                    if (connItem != null && IsConnected(connItem, item))
320
                    {
321
                        if (connItem.ItemType == ItemType.Line && item.Equals(connItem.Relations[1].Item))
322
                            ChangeGroupID(itemDic[connItem], groupKey);
323
                        else if (connItem.ItemType == ItemType.Symbol)
324
                        {
325
                            List<Item> allConnItems = GetConnectedLines(connItem);
326
                            allConnItems.Remove(item);
327
                            List<Item> connItems = GetConnectedBackwardLines(connItem);
328
                            if (allConnItems.Count.Equals(1) && connItems.Count.Equals(1) && allConnItems[0].Equals(connItems[0]))
329
                            {
330
                                List<Item> connSymbols = GetConnectedSymbols(connItem);
331
                                foreach (Item loopItem in connSymbols)
332
                                    ChangeGroupID(itemDic[loopItem], groupKey);
333
                            }
334
                            else
335
                            {
336
                                List<Item> endItems = new List<Item>();
337
                                Stack<Item> stacks = new Stack<Item>();
338
                                stacks.Push(connItem);
339
                                while (stacks.Count > 0)
340
                                {
341
                                    Item stack = stacks.Pop();
342
                                    if (endItems.Contains(stack))
343
                                        continue;
344
                                    endItems.Add(stack);
345

    
346
                                    if (GetConnectedItemCount(stack) < 3)
347
                                    {
348
                                        ChangeGroupID(itemDic[stack], groupKey);
349
                                        List<Relation> relations = stack.Relations.FindAll(x => x.Item != null && x.Item.ItemType == ItemType.Symbol);
350
                                        foreach (Relation relation in relations)
351
                                            stacks.Push(relation.Item);
352
                                    }
353
                                    else if (IsSameRun(item, stack))
354
                                    {
355
                                        ChangeGroupID(itemDic[stack], groupKey);
356
                                        List<Relation> relations = stack.Relations.FindAll(x => x.Item != null && x.Item.ItemType == ItemType.Symbol && IsSameRun(item, x.Item));
357
                                        foreach (Relation relation in relations)
358
                                            stacks.Push(relation.Item);
359
                                    }
360
                                }
361
                            }
362
                        }
363
                    }
364
                }
365
            }
366

    
367
            List<string> endGroupIds = new List<string>();
368
            foreach (var item in itemDic)
369
            {
370
                if (endGroupIds.Contains(item.Value))
371
                    continue;
372
                endGroupIds.Add(item.Value);
373

    
374
                List<Item> groupItems = itemDic.Where(x => x.Value.Equals(item.Value)).Select(x => x.Key).ToList();
375
                if (groupItems.Find(x => x.ItemType == ItemType.Line) == null)
376
                {
377
                    foreach (var groupItem in groupItems)
378
                        Items.Remove(groupItem);
379
                }
380
                else
381
                {
382
                    Group group = new Group(this);
383
                    group.Items = groupItems;
384
                    group.UID = item.Value;
385
                    group.SortItems();
386
                    foreach (Item groupItem in groupItems)
387
                        groupItem.Group = group;
388
                    Groups.Add(group);
389
                }
390
            }
391

    
392
            #region HeaderSetting
393
            List<HeaderInfo> HeaderInfos = new List<HeaderInfo>();
394
            DataTable dt = DB.SelectHeaderSetting();
395
            foreach (DataRow row in dt.Rows)
396
            {
397
                string groupID = row["GROUP_ID"].ToString();
398
                string desc = row["DESCRIPTION"].ToString();
399
                int index = Convert.ToInt32(row["INDEX"]);
400
                string name = row["NAME"].ToString();
401

    
402
                HeaderInfo headerInfo = HeaderInfos.Find(x => x.UID.Equals(groupID));
403
                if (headerInfo == null)
404
                {
405
                    headerInfo = new HeaderInfo(groupID);
406
                    headerInfo.Description = desc;
407
                    HeaderInfos.Add(headerInfo);
408
                }
409

    
410

    
411
                headerInfo.HeaderItems.Add(new HeaderItem()
412
                {
413
                    Index = index,
414
                    Name = name
415
                });
416
            }
417

    
418

    
419
            foreach (HeaderInfo headerInfo in HeaderInfos)
420
                headerInfo.HeaderItems = headerInfo.HeaderItems.OrderByDescending(x => x.Index).ToList();
421

    
422
            foreach (Group group in Groups)
423
            {
424
                foreach (HeaderInfo header in HeaderInfos)
425
                {
426
                    if (group.Items.Count < header.HeaderItems.Count)
427
                    {
428
                        continue;
429
                    }
430

    
431
                    bool found = true;
432
                    for (int i = 0; i < header.HeaderItems.Count; i++)
433
                    {
434
                        if (!header.HeaderItems[header.HeaderItems.Count - 1 - i].Name.Equals(group.Items[i].Name))
435
                        {
436
                            found = false;
437
                            break;
438
                        }
439
                    }
440
                    if (found)
441
                    {
442
                        found = false;
443
                        List<Item> branches = new List<Item>();
444
                        //for (int i = 0; i < header.HeaderItems.Count; i++)
445
                        //{
446
                        //    if (group.Items[i].ItemType == ItemType.Line)
447
                        //    {
448
                                foreach(var _line in group.Items.Where(x=>x.ItemType == ItemType.Line))
449
                                {
450
                                    branches = Groups.Where(w => w != group).SelectMany(s => s.Items).Where(w => w.ItemType == ItemType.Line).Where(w => w.Relations[0].Item == _line || w.Relations[1].Item == _line).ToList();
451
                                    if (branches.Count > 0)
452
                                    {
453
                                        found = true;
454
                                        break;
455
                                    }
456

    
457
                                    if (found)
458
                                        break;
459
                                }                                
460
                        //    }
461
                        //}
462

    
463
                        if (found)
464
                        {
465
                            for (int i = 0; i < header.HeaderItems.Count; i++)
466
                            {
467
                                group.Items[i].SubItemType = SubItemType.Header;
468
                            }
469
                        }
470
                    }
471

    
472
                    found = true;
473
                    for (int i = 0; i < header.HeaderItems.Count; i++)
474
                    {
475
                        if (!header.HeaderItems[i].Name.Equals(group.Items[group.Items.Count - header.HeaderItems.Count + i].Name))
476
                        {
477
                            found = false;
478
                            break;
479
                        }
480
                    }
481
                    if (found)
482
                    {
483
                        found = false;
484
                        List<Item> branches = new List<Item>();
485
                        //for (int i = 0; i < header.HeaderItems.Count; i++)
486
                        //{
487
                        //    if (group.Items[group.Items.Count - 1 - i].ItemType == ItemType.Line)
488
                        //    {
489

    
490
                                foreach (var _line in group.Items.Where(x => x.ItemType == ItemType.Line))
491
                                {
492
                                    branches = Groups.Where(w => w != group).SelectMany(s => s.Items).Where(w => w.ItemType == ItemType.Line).Where(w => w.Relations[0].Item == _line || w.Relations[1].Item == _line).ToList();
493
                                    if (branches.Count > 0)
494
                                    {
495
                                        found = true;
496
                                        break;
497
                                    }
498

    
499
                                    if (found)
500
                                        break;
501
                                }                                
502
                        //    }
503
                        //}
504

    
505
                        if (found)
506
                        {
507
                            for (int i = 0; i < header.HeaderItems.Count; i++)
508
                            {
509
                                group.Items[group.Items.Count - 1 - i].SubItemType = SubItemType.Header;
510
                            }
511
                        }
512
                    }
513
                }
514
            }
515

    
516
            #endregion
517

    
518
            //#region HeaderSetting
519
            //List<HeaderInfo> HeaderInfos = new List<HeaderInfo>();
520
            //DataTable dt = DB.SelectHeaderSetting();
521
            //foreach (DataRow row in dt.Rows)
522
            //{
523
            //    string groupID = row["GROUP_ID"].ToString();
524
            //    string desc = row["DESCRIPTION"].ToString();
525
            //    int index = Convert.ToInt32(row["INDEX"]);
526
            //    string name = row["NAME"].ToString();
527

    
528
            //    HeaderInfo headerInfo = HeaderInfos.Find(x => x.UID.Equals(groupID));
529
            //    if (headerInfo == null)
530
            //    {
531
            //        headerInfo = new HeaderInfo(groupID);
532
            //        headerInfo.Description = desc;
533
            //        HeaderInfos.Add(headerInfo);
534
            //    }
535

    
536
            //    headerInfo.HeaderItems.Add(new HeaderItem()
537
            //    {
538
            //        Index = index,
539
            //        Name = name
540
            //    });
541
            //}
542
            //foreach (HeaderInfo headerInfo in HeaderInfos)
543
            //    headerInfo.HeaderItems = headerInfo.HeaderItems.OrderBy(x => x.Index).ToList();
544

    
545
            //foreach (Group group in Groups)
546
            //{
547
            //    List<HeaderInfo> endInfos = new List<HeaderInfo>();
548
            //    bool bFind = false;
549
            //    for (int i = 0; i < group.Items.Count; i++)
550
            //    {
551
            //        Item item = group.Items[i];
552
            //        foreach (HeaderInfo header in HeaderInfos)
553
            //        {
554
            //            if (endInfos.Contains(header))
555
            //                continue;
556

    
557
            //            if (!header.HeaderItems[i].Name.Equals(item.Name))
558
            //            {
559
            //                endInfos.Add(header);
560
            //                continue;
561
            //            }
562

    
563
            //            if (header.HeaderItems.Count.Equals(i + 1))
564
            //            {
565
            //                for (int j = 0; j < i + 1; j++)
566
            //                    group.Items[j].SubItemType = SubItemType.Header;
567
            //                bFind = true;
568
            //                break;
569
            //            }
570
            //        }
571

    
572
            //        if (bFind || endInfos.Count.Equals(HeaderInfos.Count))
573
            //            break;
574
            //    }
575

    
576
            //    endInfos = new List<HeaderInfo>();
577
            //    bFind = false;
578
            //    for (int i = 0; i < group.Items.Count; i++)
579
            //    {
580
            //        Item item = group.Items[group.Items.Count - i - 1];
581
            //        foreach (HeaderInfo header in HeaderInfos)
582
            //        {
583
            //            if (endInfos.Contains(header))
584
            //                continue;
585

    
586
            //            if (!header.HeaderItems[i].Name.Equals(item.Name))
587
            //            {
588
            //                endInfos.Add(header);
589
            //                continue;
590
            //            }
591

    
592
            //            if (header.HeaderItems.Count.Equals(i + 1))
593
            //            {
594
            //                for (int j = 0; j < i + 1; j++)
595
            //                    group.Items[group.Items.Count - j - 1].SubItemType = SubItemType.Header;
596
            //                bFind = true;
597
            //                break;
598
            //            }
599
            //        }
600

    
601
            //        if (bFind || endInfos.Count.Equals(HeaderInfos.Count))
602
            //            break;
603
            //    }
604
            //}
605
            //#endregion
606

    
607
            //#region KeywordSetting
608
            ////List<KeywordInfo> KeywordInfos = new List<KeywordInfo>();
609
            //DataTable dtKeyword = DB.SelectKeywordsSetting();
610
            //KeywordInfo KeywordInfos = new KeywordInfo();
611

    
612
            //foreach (DataRow row in dtKeyword.Rows)
613
            //{
614
            //    int index = Convert.ToInt32(row["INDEX"]);
615
            //    string name = row["NAME"].ToString();
616
            //    string keyword = row["KEYWORD"].ToString();
617

    
618
            //    KeywordInfos.KeywordItems.Add(new KeywordItem()
619
            //    {
620
            //        Index = index,
621
            //        Name = name,
622
            //        Keyword = keyword
623
            //    });
624
            //}
625

    
626
            //KeywordInfos.KeywordItems = KeywordInfos.KeywordItems.OrderBy(x => x.Index).ToList();
627

    
628
            //foreach (Group group in Groups)
629
            //{
630
            //    //List<KeywordItem> endInfos = new List<KeywordItem>();
631
            //    bool bFind = false;
632
            //    for (int i = 0; i < group.Items.Count; i++)
633
            //    {
634
            //        Item item = group.Items[i];
635
            //        foreach (KeywordItem keyword in KeywordInfos.KeywordItems)
636
            //        {
637
            //            if (endInfos.Contains(keyword))
638
            //                continue;
639

    
640
            //            if (!keyword.Name.Equals(item.Name))
641
            //            {
642
            //                endInfos.Add(keyword);
643
            //                continue;
644
            //            }
645

    
646
            //            if (KeywordInfos.KeywordItems.Count.Equals(i + 1))
647
            //            {
648
            //                for (int j = 0; j < i + 1; j++)
649
            //                    group.Items[j].Keyword = keyword.Keyword;
650
            //                bFind = true;
651
            //                break;
652
            //            }
653
            //        }
654

    
655
            //        if (bFind || endInfos.Count.Equals(KeywordInfos.KeywordItems.Count))
656
            //            break;
657
            //    }
658

    
659
            //    endInfos = new List<KeywordItem>();
660
            //    bFind = false;
661
            //    for (int i = 0; i < group.Items.Count; i++)
662
            //    {
663
            //        Item item = group.Items[group.Items.Count - i - 1];
664
            //        foreach (KeywordItem keyword in KeywordInfos.KeywordItems)
665
            //        {
666
            //            if (endInfos.Contains(keyword))
667
            //                continue;
668

    
669
            //            if (!keyword.Name.Equals(item.Name))
670
            //            {
671
            //                endInfos.Add(keyword);
672
            //                continue;
673
            //            }
674

    
675
            //            if (KeywordInfos.KeywordItems.Count.Equals(i + 1))
676
            //            {
677
            //                for (int j = 0; j < i + 1; j++)
678
            //                    group.Items[group.Items.Count - j - 1].Keyword = keyword.Keyword;
679
            //                bFind = true;
680
            //                break;
681
            //            }
682
            //        }
683

    
684
            //        if (bFind || endInfos.Count.Equals(KeywordInfos.KeywordItems.Count))
685
            //            break;
686
            //    }
687
            //}
688
            //#endregion
689

    
690
            int GetConnectedItemCount(Item item)
691
            {
692
                return item.Relations.FindAll(x => x.Item != null).Count;
693
            }
694

    
695
            List<Item> GetConnectedLines(Item item)
696
            {
697
                List<Item> result = new List<Item>();
698
                List<Item> end = new List<Item>();
699
                Stack<Item> stacks = new Stack<Item>();
700
                stacks.Push(item);
701
                while (stacks.Count > 0)
702
                {
703
                    Item stack = stacks.Pop();
704
                    if (end.Contains(stack) || stack.ItemType != ItemType.Symbol)
705
                        continue;
706
                    end.Add(stack);
707

    
708
                    List<Relation> relations = stack.Relations.FindAll(x => x.Item != null && x.Item.ItemType == ItemType.Symbol);
709
                    foreach (Relation relation in relations)
710
                        stacks.Push(relation.Item);
711

    
712
                    relations = stack.Relations.FindAll(x => x.Item != null && x.Item.ItemType == ItemType.Line);
713
                    foreach (Relation relation in relations)
714
                    {
715
                        result.Add(relation.Item);
716
                        end.Add(relation.Item);
717
                    }
718
                }
719

    
720
                return result;
721
            }
722

    
723
            List<Item> GetConnectedForwardLines(Item item)
724
            {
725
                List<Item> result = new List<Item>();
726
                List<Item> end = new List<Item>();
727
                Stack<Item> stacks = new Stack<Item>();
728
                stacks.Push(item);
729
                while (stacks.Count > 0)
730
                {
731
                    Item stack = stacks.Pop();
732
                    if (end.Contains(stack) || stack.ItemType != ItemType.Symbol)
733
                        continue;
734
                    end.Add(stack);
735

    
736
                    List<Relation> relations = stack.Relations.FindAll(x => x.Item != null && x.Item.ItemType == ItemType.Symbol);
737
                    foreach (Relation relation in relations)
738
                        stacks.Push(relation.Item);
739

    
740
                    relations = stack.Relations.FindAll(x => x.Item != null && x.Item.ItemType == ItemType.Line);
741
                    foreach (Relation relation in relations)
742
                    {
743
                        if (relation.Item.Relations.FindIndex(x => x.Item != null && x.Item.Equals(stack)).Equals(0))
744
                            result.Add(relation.Item);
745

    
746
                        end.Add(relation.Item);
747
                    }
748
                }
749

    
750
                return result;
751
            }
752

    
753
            List<Item> GetConnectedBackwardLines(Item item)
754
            {
755
                List<Item> result = new List<Item>();
756
                List<Item> end = new List<Item>();
757
                Stack<Item> stacks = new Stack<Item>();
758
                stacks.Push(item);
759
                while (stacks.Count > 0)
760
                {
761
                    Item stack = stacks.Pop();
762
                    if (end.Contains(stack) || stack.ItemType != ItemType.Symbol)
763
                        continue;
764
                    end.Add(stack);
765

    
766
                    List<Relation> relations = stack.Relations.FindAll(x => x.Item != null && x.Item.ItemType == ItemType.Symbol);
767
                    foreach (Relation relation in relations)
768
                        stacks.Push(relation.Item);
769

    
770
                    relations = stack.Relations.FindAll(x => x.Item != null && x.Item.ItemType == ItemType.Line);
771
                    foreach (Relation relation in relations)
772
                    {
773
                        if (relation.Item.Relations.FindIndex(x => x.Item != null && x.Item.Equals(stack)).Equals(1))
774
                            result.Add(relation.Item);
775

    
776
                        end.Add(relation.Item);
777
                    }
778
                }
779

    
780
                return result;
781
            }
782

    
783
            List<Item> GetConnectedSymbols(Item item)
784
            {
785
                List<Item> result = new List<Item>();
786
                List<Item> end = new List<Item>();
787
                Stack<Item> stacks = new Stack<Item>();
788
                stacks.Push(item);
789
                while (stacks.Count > 0)
790
                {
791
                    Item stack = stacks.Pop();
792
                    if (end.Contains(stack) || stack.ItemType != ItemType.Symbol)
793
                        continue;
794
                    end.Add(stack);
795
                    result.Add(stack);
796
                    List<Relation> relations = stack.Relations.FindAll(x => x.Item != null && x.Item.ItemType == ItemType.Symbol);
797
                    foreach (Relation relation in relations)
798
                        stacks.Push(relation.Item);
799
                }
800

    
801
                return result;
802
            }
803

    
804
            void ChangeGroupID(string from, string to)
805
            {
806
                if (from.Equals(to))
807
                    return;
808

    
809
                List<Item> changeItems = new List<Item>();
810
                foreach (var _item in itemDic)
811
                    if (_item.Value.Equals(from))
812
                        changeItems.Add(_item.Key);
813
                foreach (var _item in changeItems)
814
                    itemDic[_item] = to;
815
            }
816

    
817
            bool IsConnected(Item item1, Item item2)
818
            {
819
                if (item1.Relations.Find(x => x.UID.Equals(item2.UID)) != null &&
820
                    item2.Relations.Find(x => x.UID.Equals(item1.UID)) != null)
821
                    return true;
822
                else
823
                    return false;
824
            }
825

    
826
            bool IsSameRun(Item item1, Item item2)
827
            {
828
                return RunInfos.Find(x => x.Items.Contains(item1) && x.Items.Contains(item2)) != null ? true : false;
829
            }
830
        }
831
        private void RemoveItems()
832
        {
833
            List<Item> removeItems = new List<Item>();
834
            foreach (var item in Items)
835
            {
836
                if (item.ItemType == ItemType.Line && (item.Name != "Secondary" && item.Name != "Primary"))
837
                {
838
                    List<Relation> relations = item.Relations.FindAll(x => x.Item != null);
839
                    foreach (Relation relation in relations)
840
                    {
841
                        Relation otherRelation = Items.Find(x => x.UID == relation.UID).Relations.Find(x => x.UID == item.UID);
842
                        if (otherRelation != null)
843
                        {
844
                            otherRelation.Item = null;
845
                            otherRelation.UID = string.Empty;
846
                        }
847
                    }
848

    
849
                    removeItems.Add(item);
850
                }
851
            }
852

    
853
            foreach (var item in removeItems)
854
            {
855
                Items.Remove(item);
856
            }
857
        }
858
        
859
        private void InsertNozzle()
860
        {
861
            List<Item> newNozzles = new List<Item>();
862
            foreach (var item in Items)
863
            {
864
                if (item.SubItemType == SubItemType.Nozzle)
865
                    continue;
866
                
867
                foreach (Relation relation in item.Relations)
868
                {
869
                    Equipment equipment = Equipments.Find(x => x.UID == relation.UID);
870
                    if (equipment != null)
871
                    {
872
                        Item newNozzle = new Item();
873
                        newNozzle.UID = Guid.NewGuid().ToString();
874
                        newNozzle.Name = "PSN_Nozzle";
875
                        newNozzle.ID2DBType = "Nozzles";
876
                        newNozzle.Owner = item.Owner;
877
                        newNozzle.ID2DBName = "PSN_Nozzle";
878
                        newNozzle.POINT = relation.Point;
879
                        newNozzle.Relations = new List<Relation>();
880

    
881
                        newNozzle.Relations.Add(new Relation()
882
                        {
883
                            Point = relation.Point,
884
                            UID = equipment.UID
885
                        });
886
                        newNozzle.Relations.Add(new Relation()
887
                        {
888
                            Point = relation.Point,
889
                            UID = item.UID
890
                        });
891

    
892
                        newNozzle.ItemType = ItemType.Symbol;
893
                        newNozzle.SubItemType = SubItemType.Nozzle;
894

    
895
                        newNozzles.Add(newNozzle);
896

    
897
                        relation.UID = newNozzle.UID;
898
                    }
899
                }
900
            }
901

    
902
            Items.AddRange(newNozzles);
903
        }
904
    }
905
}
클립보드 이미지 추가 (최대 크기: 500 MB)