프로젝트

일반

사용자정보

통계
| 개정판:

hytos / DTI_PID / ID2PSN / PSN.cs @ 0846c6ee

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

1
using System;
2
using System.Collections.Generic;
3
using System.Data;
4
using System.IO;
5
using System.Linq;
6
using System.Text;
7
using System.Threading.Tasks;
8
using ID2PSN.Properties;
9
using System.Text.RegularExpressions;
10
using System.Windows.Forms;
11

    
12
namespace ID2PSN
13
{
14
    public enum PSNType
15
    {
16
        None,
17
        Branch,
18
        Equipment,
19
        Header,
20
        Symbol,
21
        OPC,
22
    }
23

    
24
    public enum ErrorType
25
    {
26
        Error = -1,
27
        OK,
28
        InValid //이값은 들어가는데가 없음..
29
    }
30

    
31
    
32

    
33

    
34
    public class PSN
35
    {
36
        private double[] DrawingSize = null;
37
        private double DrawingWidth = double.NaN;
38
        private double DrawingHeight = double.NaN;
39
        public int Revision;
40
        public string EquipTagNoAttributeName = string.Empty;
41
        public DataTable PathItems { get; set; }
42
        public DataTable SequenceData { get; set; }
43
        public DataTable PipeSystemNetwork { get; set; }
44
        public DataTable TopologySet { get; set; }
45
        public DataTable Equipment { get; set; }
46
        public DataTable Nozzle { get; set; }
47

    
48
        public string Rule1 = "";
49
        public string Rule2 = "";
50
        public string Rule3 = "";
51
        public string Rule4 = "";
52
        public string Rule5 = "";
53

    
54
        int tieInPointIndex = 1;
55

    
56
        List<Document> Documents;
57
        List<Group> groups = new List<Group>();
58
        List<PSNItem> PSNItems = new List<PSNItem>();
59
        List<Topology> Topologies = new List<Topology>();
60

    
61
        DataTable opcDT = null;
62
        DataTable topologyRuleDT = null;
63

    
64
        ID2Info id2Info = ID2Info.GetInstance();
65

    
66
        const string FluidPriorityType = "FLUIDCODE";
67
        const string PipingMaterialsPriorityType = "PIPINGMATERIALSCLASS";
68

    
69
        public PSN()
70
        {
71
            
72
        }
73

    
74
        public PSN(List<Document> documents, int Revision)
75
        {
76
            try
77
            {
78
                Documents = documents;
79
                foreach (Document document in Documents)
80
                    groups.AddRange(document.Groups);
81
                opcDT = GetOPCInfo();
82
                topologyRuleDT = GetTopologyRule();
83
                this.Revision = Revision;
84
                DrawingSize = DB.GetDrawingSize();
85
                if (DrawingSize == null)
86
                {
87
                    MessageBox.Show("There is no data whose Section is Area and Key is Drawing in the drawing table.", "ID2 " + id2Info.ProgramName, MessageBoxButtons.OK, MessageBoxIcon.Information);
88
                    return;
89
                }
90
                DrawingWidth = DrawingSize[2] - DrawingSize[0];
91
                DrawingHeight = DrawingSize[3] - DrawingSize[1];
92
            }
93
            catch (Exception ex)
94
            {
95
                Log.Write(ex.Message + "\r\n" + ex.StackTrace);
96
                MessageBox.Show(ex.Message, "ID2 " + id2Info.ProgramName, MessageBoxButtons.OK, MessageBoxIcon.Information);
97
            }
98
        }
99

    
100
        private string GetItemTag(Item item)
101
        {
102
            string result = string.Empty;
103
            if (item.ItemType == ItemType.Line)
104
                result = item.LineNumber != null ? item.LineNumber.Name : string.Empty;
105
            else if (item.ItemType == ItemType.Symbol && item.SubItemType == SubItemType.Nozzle)
106
                result = Nozzle.Select(string.Format("OID = '{0}'", item.UID)).First()["ITEMTAG"].ToString();
107

    
108
            return result;
109
        }
110
        private string GetItemName(Item item, string itemOID)
111
        {
112
            string result = string.Empty;
113
            if (item.ItemType == ItemType.Line && (item.Name == "Secondary" || item.Name == "Primary"))
114
            {
115
                if (itemOID.Contains("_"))
116
                {
117
                    string split = itemOID.Split(new char[] { '_' })[1];
118
                    if (split.StartsWith("B"))
119
                        result = "Branch";
120
                    else
121
                        result = "PipeRun";
122
                }
123
                else
124
                    result = "PipeRun";
125
            }
126
            else if (item.ItemType == ItemType.Symbol)
127
            {
128
                if (item.ID2DBCategory == "Instrumentation")
129
                    result = "Instrument";
130
                else if (item.ID2DBType == "Nozzles")
131
                    result = "Nozzle";
132
                else if (item.ID2DBType == "Fittings" ||
133
                        item.ID2DBType == "Piping OPC's" ||
134
                        item.ID2DBType == "Specialty Components" ||
135
                        item.ID2DBType == "Valves" ||
136
                        item.ID2DBType == "Reducers")
137
                    result = "PipingComp";
138
            }
139
            return result;
140
        }
141

    
142
        private string GetClass(Item item, string itemOID)
143
        {
144
            string result = string.Empty;
145
            if (item.ItemType == ItemType.Line && (item.Name == "Secondary" || item.Name == "Primary"))
146
            {
147
                if (itemOID.Contains("_"))
148
                {
149
                    string split = itemOID.Split(new char[] { '_' })[1];
150
                    if (split.StartsWith("B"))
151
                        result = "Branch";
152
                    else
153
                        result = "Piping";
154
                }
155
                else
156
                    result = "Piping";
157
            }
158
            else if (item.ItemType == ItemType.Symbol)
159
            {
160
                if (item.ID2DBCategory == "Instrumentation")
161
                    result = item.ID2DBType;
162
                else if (item.ID2DBType == "Nozzles")
163
                    result = string.Empty;
164
                else if (item.ID2DBType == "Fittings" ||
165
                       item.ID2DBType == "Piping OPC's" ||
166
                       item.ID2DBType == "Specialty Components" ||
167
                       item.ID2DBType == "Valves" ||
168
                       item.ID2DBType == "Reducers")
169
                    result = item.ID2DBType;
170
            }
171
            return result;
172
        }
173

    
174
        private string GetSubClass(Item item, string itemOID)
175
        {
176
            string result = string.Empty;
177
            if (item.ItemType == ItemType.Line && (item.Name == "Secondary" || item.Name == "Primary"))
178
            {
179
                if (itemOID.Contains("_"))
180
                {
181
                    string split = itemOID.Split(new char[] { '_' })[1];
182
                    if (split.StartsWith("B"))
183
                        result = "Tee";
184
                    else
185
                        result = "";
186
                }
187
                else
188
                    result = "";
189
            }
190
            else if (item.ItemType == ItemType.Symbol)
191
            {
192
                if (item.ID2DBCategory == "Instrumentation")
193
                    result = string.Empty;
194
                else if (item.ID2DBType == "Nozzles")
195
                    result = string.Empty;
196
                else if (item.ID2DBType == "Fittings" ||
197
                       item.ID2DBType == "Piping OPC's" ||
198
                       item.ID2DBType == "Specialty Components" ||
199
                       item.ID2DBType == "Valves" ||
200
                       item.ID2DBType == "Reducers")
201
                    result = "In-line component";
202
            }
203
            return result;
204
        }
205

    
206
        public void SetPSNData()
207
        {
208
            // Item들의 속성으로 Topology Data를 생성한다.
209
            // Topology Data는 Topology Rule Setting을 기준으로 생성한다. 
210
            SetTopologyData();
211
            // ID2의 OPC연결 Data 기반으로 Group(도면단위 PSN)을 연결한다.
212
            ConnectByOPC();
213
            // 실제 PSN 생성 로직
214
            // 연결된 Group을 하나의 PSN으로 만든다.
215
            SetPSNItem();
216
            // 생성된 PSN의 Type을 설정한다.
217
            SetPSNType();
218
            // ID2에는 Branch 정보가 없어서 Branch 정보를 생성한다.
219
            SetBranchInfo();
220
            // 생성된 Topology Data들을 정리하며 Main, Branch를 판단한다.
221
            SetTopology();
222
            // PSN이 Bypass인지 검사 
223
            SetPSNBypass();
224
            // Topology들에게 Index를 부여한다
225
            SetTopologyIndex();
226
            
227
            // Nozzle, Equipment의 정보를 저장
228
            SaveNozzleAndEquipment();
229
            // PSN의 정보를 저장
230
            SavePSNData();
231
            // Topology의 subtype을 update(bypass, Header, 등등) 
232
            UpdateSubType();
233
            // Update Error
234
            UpdateErrorForPSN();
235
        }
236

    
237
        private void SetTopologyData()
238
        {
239
            // 13번 excel
240
            foreach (Group group in groups)
241
            {
242
                LineNumber prevLineNumber = null;
243
                for (int i = 0; i < group.Items.Count; i++)
244
                {
245
                    Item item = group.Items[i];
246
                    LineNumber lineNumber = item.Document.LineNumbers.Find(x => x.UID == item.Owner);
247
                    if (lineNumber == null)
248
                    {
249
                        if (prevLineNumber != null)
250
                        {
251
                            if (!prevLineNumber.IsCopy)
252
                            {
253
                                prevLineNumber = prevLineNumber.Copy();
254
                                item.Document.LineNumbers.Add(prevLineNumber);
255
                            }
256

    
257
                            item.Owner = prevLineNumber.UID;
258
                        }
259
                    }
260
                    else
261
                        prevLineNumber = lineNumber;
262
                }
263

    
264
                prevLineNumber = null;
265
                for (int i = group.Items.Count - 1; i > -1; i--)
266
                {
267
                    Item item = group.Items[i];
268
                    LineNumber lineNumber = item.Document.LineNumbers.Find(x => x.UID == item.Owner);
269
                    if (lineNumber == null)
270
                    {
271
                        if (prevLineNumber != null)
272
                        {
273
                            if (!prevLineNumber.IsCopy)
274
                            {
275
                                prevLineNumber = prevLineNumber.Copy();
276
                                item.Document.LineNumbers.Add(prevLineNumber);
277
                            }
278

    
279
                            item.Owner = prevLineNumber.UID;
280
                        }
281
                    }
282
                    else
283
                        prevLineNumber = lineNumber;
284
                }
285

    
286
                if (prevLineNumber == null)
287
                {
288
                    List<LineNumber> lineNumbers = group.Document.LineNumbers.FindAll(x => !x.IsCopy);
289
                    Random random = new Random();
290
                    int index = random.Next(lineNumbers.Count - 1);
291
                    
292
                    // Copy
293
                    LineNumber cLineNumber = lineNumbers[index].Copy();
294
                    group.Document.LineNumbers.Add(cLineNumber);
295

    
296
                    foreach (Item item in group.Items)
297
                        item.Owner = cLineNumber.UID;
298
                }
299
            }
300

    
301

    
302
            foreach (Document document in Documents)
303
            {
304
                foreach (Item item in document.Items)
305
                {
306
                    item.TopologyData = string.Empty;
307
                    item.PSNPipeLineID = string.Empty;
308
                    LineNumber lineNumber = document.LineNumbers.Find(x => x.UID == item.Owner);
309
                    if (lineNumber != null)
310
                    {
311
                        item.LineNumber = lineNumber;
312

    
313
                        foreach (DataRow row in topologyRuleDT.Rows)
314
                        {
315
                            string uid = row["UID"].ToString();
316
                            if (uid == "-")
317
                                item.TopologyData += "-"; 
318
                            else
319
                            {
320
                                Attribute itemAttr = item.Attributes.Find(x => x.Name == uid);
321

    
322
                                Attribute attribute = lineNumber.Attributes.Find(x => x.Name == uid);
323
                                if (attribute != null)
324
                                    item.TopologyData += attribute.Value;
325
                            }
326
                        }
327

    
328
                        Attribute insulAttr = item.LineNumber.Attributes.Find(x => x.Name == "InsulationPurpose");
329
                        if (insulAttr != null && !string.IsNullOrEmpty(insulAttr.Value))
330
                            item.PSNPipeLineID = item.TopologyData + "-" + insulAttr.Value;
331
                        else
332
                            item.PSNPipeLineID = item.TopologyData;
333
                    }
334
                    else
335
                    {
336
                        item.TopologyData = "Empty LineNumber";
337
                        item.LineNumber = new LineNumber();
338
                    }
339
                }
340
            }
341

    
342
            int emptyIndex = 1;
343
            foreach (Group group in groups)
344
            {
345
                List<Item> groupItems = group.Items.FindAll(x => x.TopologyData == "Empty LineNumber");
346
                if (groupItems.Count > 0)
347
                {
348
                    foreach (var item in groupItems)
349
                        item.TopologyData += string.Format("-{0}", emptyIndex);
350
                    emptyIndex++;
351
                }
352
            }
353

    
354
        }
355
        private void ConnectByOPC()
356
        {
357
            foreach (Group group in groups.FindAll(x => x.Items.Last().SubItemType == SubItemType.OPC))
358
            {
359
                Item opcItem = group.Items.Last();
360
                DataRow[] fromRows = opcDT.Select(string.Format("FromOPCUID = '{0}'", opcItem.UID));
361
                if (fromRows.Length.Equals(1))
362
                {
363
                    DataRow opcRow = fromRows.First();
364
                    string toDrawing = opcRow["ToDrawing"].ToString();
365
                    string toOPCUID = opcRow["ToOPCUID"].ToString();
366

    
367
                    Document toDocument = Documents.Find(x => x.DrawingName == toDrawing);
368
                    if(toDocument != null)
369
                    { 
370
                        Group toGroup = toDocument.Groups.Find(x => x.Items.Find(y => y.UID == toOPCUID) != null);
371
                        DataRow[] toRows = opcDT.Select(string.Format("ToOPCUID = '{0}'", toGroup.Items.First().UID));
372
                        //1대1 매칭이 아닐때 걸림 (2개 이상일 때)
373
                        if (toRows.Length > 1)
374
                        {
375
                            throw new Exception("OPC error(multi connect)");
376
                        }
377
                        group.EndGroup = toGroup;
378
                        toGroup.StartGroup = group;
379
                    }
380
                }
381
            }
382
        }
383
        private void SetPSNItem()
384
        {
385
            Dictionary<Group, string> groupDic = new Dictionary<Group, string>(); 
386
            foreach (Group group in groups)
387
                groupDic.Add(group, Guid.NewGuid().ToString());
388

    
389
            foreach (Group group in groups)
390
            {
391
                string groupKey = groupDic[group];
392
                if (group.StartGroup != null)
393
                {
394
                    string otherKey = groupDic[group.StartGroup];
395
                    ChangeGroupID(otherKey, groupKey);
396
                }
397
            }
398

    
399
            // PSN 정리
400
            foreach (var item in groupDic)
401
            {
402
                Group group = item.Key;
403
                string uid = item.Value;
404
                PSNItem PSNItem = PSNItems.Find(x => x.UID == uid);
405
                if (PSNItem == null)
406
                {
407
                    PSNItem = new PSNItem(PSNItems.Count, Revision) { UID = uid };
408
                    PSNItems.Add(PSNItem);
409
                }
410
                PSNItem.Groups.Add(group);
411
                foreach (Item groupItem in group.Items)
412
                    groupItem.PSNItem = PSNItem;
413
            }
414

    
415
            // Sort PSN
416
            foreach (PSNItem PSNItem in PSNItems)
417
            {
418
                List<Group> _groups = new List<Group>();
419

    
420
                Stack<Group> stacks = new Stack<Group>();
421
                stacks.Push(PSNItem.Groups.First());
422
                while (stacks.Count > 0)
423
                {
424
                    Group stack = stacks.Pop();
425
                    if (_groups.Contains(stack))
426
                        continue;
427

    
428
                    if (_groups.Count == 0)
429
                        _groups.Add(stack);
430
                    else
431
                    {
432
                        if (stack.StartGroup != null && _groups.Contains(stack.StartGroup))
433
                        {
434
                            int index = _groups.IndexOf(stack.StartGroup);
435
                            _groups.Insert(index + 1, stack);
436
                        }
437
                        else if (stack.EndGroup != null && _groups.Contains(stack.EndGroup))
438
                        {
439
                            int index = _groups.IndexOf(stack.EndGroup);
440
                            _groups.Insert(index, stack);
441
                        }
442
                    }
443

    
444
                    if (stack.StartGroup != null)
445
                        stacks.Push(stack.StartGroup);
446
                    if (stack.EndGroup != null)
447
                        stacks.Push(stack.EndGroup);
448
                }
449

    
450
                PSNItem.Groups.Clear();
451
                PSNItem.Groups.AddRange(_groups);
452
            }
453

    
454
            void ChangeGroupID(string from, string to)
455
            {
456
                if (from.Equals(to))
457
                    return;
458

    
459
                List<Group> changeItems = new List<Group>();
460
                foreach (var _item in groupDic)
461
                    if (_item.Value.Equals(from))
462
                        changeItems.Add(_item.Key);
463
                foreach (var _item in changeItems)
464
                    groupDic[_item] = to;
465
            }
466
        }
467
        private void SetPSNType()
468
        {
469
            foreach (PSNItem PSNItem in PSNItems)
470
            {
471
                Group firstGroup = PSNItem.Groups.First();
472
                Group lastGroup = PSNItem.Groups.Last();
473

    
474
                Item firstItem = firstGroup.Items.First();
475
                Item lastItem = lastGroup.Items.Last();
476

    
477
                PSNItem.StartType = GetPSNType(firstItem, true);
478
                PSNItem.EndType = GetPSNType(lastItem, false);
479
            }
480

    
481
            PSNType GetPSNType(Item item, bool bFirst = true)
482
            {
483
                PSNType type = PSNType.None;
484

    
485
                if (item.ItemType == ItemType.Line)
486
                {
487
                    Group group = groups.Find(x => x.Items.Contains(item));
488
                    if (bFirst && item.Relations[0].Item != null && !group.Items.Contains(item.Relations[0].Item))
489
                    {
490
                        Item connItem = item.Relations[0].Item;
491
                        if (connItem.ItemType == ItemType.Line && !IsConnected(item, connItem))
492
                            type = PSNType.Branch;
493
                        else if (connItem.ItemType == ItemType.Symbol)
494
                            type = PSNType.Symbol;
495
                    }
496
                    else if (!bFirst && item.Relations[1].Item != null && !group.Items.Contains(item.Relations[1].Item))
497
                    {
498
                        Item connItem = item.Relations[1].Item;
499
                        if (connItem.ItemType == ItemType.Line && !IsConnected(item, connItem))
500
                            type = PSNType.Branch;
501
                        else if (connItem.ItemType == ItemType.Symbol)
502
                            type = PSNType.Symbol;
503
                    }
504
                }
505
                else if (item.ItemType == ItemType.Symbol)
506
                {
507
                    if (item.SubItemType == SubItemType.Nozzle)
508
                        type = PSNType.Equipment;
509
                    else if (item.SubItemType == SubItemType.Header)
510
                        type = PSNType.Header;
511
                    else if (item.SubItemType == SubItemType.OPC)
512
                        type = PSNType.OPC;
513
                }
514

    
515
                return type;
516
            }
517
        }
518
        private void SetBranchInfo()
519
        {
520
            foreach (Document document in Documents)
521
            {
522
                List<Item> lines = document.Items.FindAll(x => x.ItemType == ItemType.Line).ToList();
523
                foreach (Item line in lines)
524
                {
525
                    double[] point = line.Relations[0].Point;
526
                    List<Item> connLines = lines.FindAll(x => x.Relations.Find(y => y.UID == line.UID) != null && line.Relations.Find(y => y.UID == x.UID) == null);
527
                    connLines.Sort(SortBranchLine);
528
                    line.BranchItems.AddRange(connLines);
529

    
530
                    int SortBranchLine(Item a, Item b)
531
                    {
532
                        double[] pointA = a.Relations[0].UID == line.UID ? a.Relations[0].Point : a.Relations[1].Point;
533
                        double distanceA = CalcPointToPointdDistance(point[0], point[1], pointA[0], pointA[1]);
534

    
535
                        double[] pointB = b.Relations[0].UID == line.UID ? b.Relations[0].Point : b.Relations[1].Point;
536
                        double distanceB = CalcPointToPointdDistance(point[0], point[1], pointB[0], pointB[1]);
537

    
538
                        // 내림차순
539
                        return distanceA.CompareTo(distanceB);
540
                    }
541
                    double CalcPointToPointdDistance(double x1, double y1, double x2, double y2)
542
                    {
543
                        return Math.Pow(Math.Pow(x1 - x2, 2) + Math.Pow(y1 - y2, 2), 0.5);
544
                    }
545
                }
546
            }
547
        }
548
        private void SetTopology()
549
        {
550
            #region 기본 topology 정리
551
            foreach (PSNItem PSNItem in PSNItems)
552
            {
553
                Topology topology = null;
554
                foreach (Group group in PSNItem.Groups)
555
                {
556
                    foreach (Item item in group.Items)
557
                    {
558
                        if (string.IsNullOrEmpty(item.TopologyData))
559
                            topology = null;
560
                        else
561
                        {
562
                            if (topology == null)
563
                            {
564
                                topology = new Topology()
565
                                {
566
                                    ID = item.TopologyData
567
                                };
568
                                Topologies.Add(topology);
569

    
570
                                if (!PSNItem.Topologies.Contains(topology))
571
                                    PSNItem.Topologies.Add(topology);
572
                            }
573
                            else
574
                            {
575
                                if (topology.ID != item.TopologyData)
576
                                {
577
                                    topology = new Topology()
578
                                    {
579
                                        ID = item.TopologyData
580
                                    };
581
                                    Topologies.Add(topology);
582

    
583
                                    if (!PSNItem.Topologies.Contains(topology))
584
                                        PSNItem.Topologies.Add(topology);
585
                                }
586
                            }
587

    
588
                            item.Topology = topology;
589
                            topology.Items.Add(item);
590
                        }
591
                    }
592
                }
593
            }
594
            #endregion
595

    
596
            #region Type
597
            List<string> ids = Topologies.Select(x => x.ID).Distinct().ToList();
598
            foreach (string id in ids)
599
            {
600
                List<Topology> topologies = Topologies.FindAll(x => x.ID == id);
601

    
602
                // Main
603
                List<Topology> mainTopologies = FindMainTopology(topologies);
604
                foreach (Topology topology in mainTopologies)
605
                    topology.Type = "M";
606

    
607
                // Branch
608
                List<Topology> branchToplogies = topologies.FindAll(x => string.IsNullOrEmpty(x.Type));
609
                foreach (Topology topology in branchToplogies)
610
                    topology.Type = "B";
611
            }
612
            #endregion
613
        }
614
        private void SetTopologyIndex()
615
        {
616
            List<string> ids = Topologies.Select(x => x.ID).Distinct().ToList();
617
            foreach (string id in ids)
618
            {
619
                List<Topology> topologies = Topologies.FindAll(x => x.ID == id);
620

    
621
                // Main
622
                List<Topology> mainTopologies = topologies.FindAll(x => x.Type == "M");
623
                foreach (Topology topology in mainTopologies)
624
                    topology.Index = mainTopologies.IndexOf(topology).ToString();
625

    
626
                // Branch
627
                List<Topology> branchToplogies = topologies.FindAll(x => x.Type == "B");
628
                foreach (Topology topology in branchToplogies)
629
                    topology.Index = (branchToplogies.IndexOf(topology) + 1).ToString();
630
            }
631
        }
632
        private void SetPSNBypass()
633
        {
634
            foreach (PSNItem PSNItem in PSNItems)
635
                PSNItem.IsBypass = IsBypass(PSNItem);
636
        }
637
        private List<Topology> FindMainTopology(List<Topology> data)
638
        {
639
            DataTable nominalDiameterDT = DB.SelectNominalDiameter();
640
            DataTable PMCDT = DB.SelectPSNPIPINGMATLCLASS();
641
            DataTable fluidCodeDT = DB.SelectPSNFluidCode();
642

    
643
            List<Topology> main = new List<Topology>();
644
            main.AddRange(data);
645
            //
646
            main = GetNozzleTopology(data);
647
            if (main.Count == 1)
648
                return main;
649
            else
650
            {
651
                if (main.Count > 0)
652
                    main = GetPMCTopology(main);
653
                else
654
                    main = GetPMCTopology(data);
655

    
656
                if (main.Count == 1)
657
                    return main;
658
                else
659
                {
660
                    if (main.Count > 0)
661
                        main = GetDiaTopology(main);
662
                    else
663
                        main = GetDiaTopology(data);
664

    
665

    
666
                    if (main.Count == 1)
667
                        return main;
668
                    else
669
                    {
670
                        if (main.Count > 0)
671
                            main = GetItemTopology(main);
672
                        else
673
                            main = GetItemTopology(data);
674
                    }
675
                }
676
            }
677

    
678
            List<Topology> GetNozzleTopology(List<Topology> topologies)
679
            {
680
                return topologies.FindAll(x => x.Items.Find(y => y.SubItemType == SubItemType.Nozzle) != null);
681
            }
682

    
683
            List<Topology> GetPMCTopology(List<Topology> topologies)
684
            {
685
                List<Topology> result = new List<Topology>();
686
                foreach (DataRow row in PMCDT.Rows)
687
                {
688
                    string value = row["CODE"].ToString();
689
                    foreach (Topology topology in topologies)
690
                    {
691
                        foreach (Item item in topology.Items)
692
                        {
693
                            if (item.LineNumber == null)
694
                                continue;
695
                            Attribute attribute = item.LineNumber.Attributes.Find(x => x.Name == "PipingMaterialsClass");
696
                            if (attribute != null && value == attribute.Value)
697
                            {
698
                                result.Add(topology);
699
                                break;
700
                            }
701
                        }
702
                    }
703

    
704
                    if (result.Count > 0)
705
                        break;
706
                }
707

    
708
                return result;
709
            }
710

    
711
            List<Topology> GetDiaTopology(List<Topology> topologies)
712
            {
713
                List<Topology> result = new List<Topology>();
714
                foreach (DataRow row in nominalDiameterDT.Rows)
715
                {
716
                    string inchValue = row["InchStr"].ToString();
717
                    string metricValue = row["MetricStr"].ToString();
718
                    foreach (Topology topology in topologies)
719
                    {
720
                        foreach (Item item in topology.Items)
721
                        {
722
                            if (item.LineNumber == null)
723
                                continue;
724
                            Attribute attribute = item.LineNumber.Attributes.Find(x => x.Name == "NominalDiameter");
725
                            if (attribute != null && (inchValue == attribute.Value || metricValue == attribute.Value))
726
                            {
727
                                result.Add(topology);
728
                                break;
729
                            }
730
                        }
731
                    }
732

    
733
                    if (result.Count > 0)
734
                        break;
735
                }
736

    
737
                return result;
738
            }
739

    
740
            List<Topology> GetItemTopology(List<Topology> topologies)
741
            {
742
                return new List<Topology>() { topologies.OrderByDescending(x => x.Items.Count).ToList().First() };
743
            }
744

    
745
            return main;
746
        }
747

    
748
        private DataTable GetOPCInfo()
749
        {
750
            DataTable opc = DB.SelectOPCRelations();
751
            DataTable drawing = DB.SelectDrawings();
752

    
753
            DataTable dt = new DataTable();
754
            dt.Columns.Add("FromDrawing", typeof(string));
755
            dt.Columns.Add("FromDrawingUID", typeof(string));
756
            dt.Columns.Add("FromOPCUID", typeof(string));
757
            dt.Columns.Add("ToDrawing", typeof(string));
758
            dt.Columns.Add("ToDrawingUID", typeof(string));
759
            dt.Columns.Add("ToOPCUID", typeof(string));
760
            foreach (DataRow row in opc.Rows)
761
            {
762
                string fromDrawingUID = row["From_Drawings_UID"] == null ? string.Empty : row["From_Drawings_UID"].ToString();
763
                string fromOPCUID = row["From_OPC_UID"] == null ? string.Empty : row["From_OPC_UID"].ToString(); 
764
                string toDrawingUID = row["To_Drawings_UID"] == null ? string.Empty : row["To_Drawings_UID"].ToString(); 
765
                string toOPCUID = row["To_OPC_UID"] == null ? string.Empty : row["To_OPC_UID"].ToString();
766
                if (!string.IsNullOrEmpty(toOPCUID))
767
                {
768
                    DataRow[] fromRows = drawing.Select(string.Format("UID = '{0}'", fromDrawingUID));
769
                    DataRow[] toRows = drawing.Select(string.Format("UID = '{0}'", toDrawingUID));
770
                    if (fromRows.Length.Equals(1) && toRows.Length.Equals(1))
771
                    {
772
                        string fromDrawingName = Path.GetFileNameWithoutExtension(fromRows.First()["NAME"].ToString());
773
                        string toDrawingName = Path.GetFileNameWithoutExtension(toRows.First()["NAME"].ToString());
774

    
775
                        DataRow newRow = dt.NewRow();
776
                        newRow["FromDrawing"] = fromDrawingName;
777
                        newRow["FromDrawingUID"] = fromDrawingUID;
778
                        newRow["FromOPCUID"] = fromOPCUID;
779
                        newRow["ToDrawing"] = toDrawingName;
780
                        newRow["ToDrawingUID"] = toDrawingUID;
781
                        newRow["ToOPCUID"] = toOPCUID;
782

    
783
                        dt.Rows.Add(newRow);
784
                    }
785
                }
786
            }
787

    
788
            return dt;
789
        }
790
        private DataTable GetTopologyRule()
791
        {
792
            DataTable dt = DB.SelectTopologyRule();
793

    
794
            return dt;
795
        }
796
        private bool IsConnected(Item item1, Item item2)
797
        {
798
            if (item1.Relations.Find(x => x.UID.Equals(item2.UID)) != null &&
799
                item2.Relations.Find(x => x.UID.Equals(item1.UID)) != null)
800
                return true;
801
            else
802
                return false;
803
        }
804

    
805
        private void SaveNozzleAndEquipment()
806
        {
807
            List<Item> nozzles = new List<Item>();
808
            List<Equipment> equipments = new List<Equipment>();
809
            foreach (Document document in Documents)
810
            {
811
                nozzles.AddRange(document.Items.FindAll(x => x.SubItemType == SubItemType.Nozzle));
812
                equipments.AddRange(document.Equipments);
813
            }
814
                
815

    
816
            DataTable nozzleDT = new DataTable();
817
            nozzleDT.Columns.Add("OID", typeof(string));
818
            nozzleDT.Columns.Add("ITEMTAG", typeof(string));
819
            nozzleDT.Columns.Add("XCOORDS", typeof(string));
820
            nozzleDT.Columns.Add("YCOORDS", typeof(string));
821
            nozzleDT.Columns.Add("Equipment_OID", typeof(string));
822
            nozzleDT.Columns.Add("FLUID", typeof(string));
823
            nozzleDT.Columns.Add("NPD", typeof(string));
824
            nozzleDT.Columns.Add("ROTATION", typeof(string));
825
            nozzleDT.Columns.Add("FlowDirection", typeof(string));
826

    
827
            foreach (Item item in nozzles)
828
            {
829
                DataRow row = nozzleDT.NewRow();
830
                row["OID"] = item.UID;
831

    
832
                Relation relation = item.Relations.Find(x => equipments.Find(y => y.UID == x.UID) != null);
833
                if (relation != null)
834
                {
835
                    Equipment equipment = equipments.Find(x => x.UID == relation.UID);
836
                    equipment.Nozzles.Add(item);
837
                    row["ITEMTAG"] = string.Format("N-{0}", string.Format("{0:D3}", equipment.Nozzles.Count + 100));
838
                    row["Equipment_OID"] = equipment.UID;
839
                    item.Equipment = equipment;
840
                }
841
                row["XCOORDS"] = (item.POINT[0] / DrawingWidth).ToString();
842
                row["YCOORDS"] = (item.POINT[1] / DrawingHeight).ToString();
843
                Attribute fluidAttr = item.LineNumber.Attributes.Find(x => x.Name == "FluidCode");
844
                row["FLUID"] = fluidAttr != null ? fluidAttr.Value : string.Empty;
845
                Attribute npdAttr = item.LineNumber.Attributes.Find(x => x.Name == "NominalDiameter");
846
                row["NPD"] = npdAttr != null ? npdAttr.Value : string.Empty;
847

    
848
                double angle = Math.PI * 2 - Convert.ToDouble(item.ANGLE);
849
                if (angle >= Math.PI * 2)
850
                    angle = angle - Math.PI * 2;
851
                row["ROTATION"] = angle.ToString();
852

    
853
                if (item.Topology.Items.First().Equals(item))
854
                    row["FlowDirection"] = "Outlet";
855
                else if (item.Topology.Items.Last().Equals(item))
856
                    row["FlowDirection"] = "Inlet";
857
                else
858
                    row["FlowDirection"] = string.Empty;
859

    
860
                nozzleDT.Rows.Add(row);
861
            }
862

    
863
            DataTable equipDT = new DataTable();
864
            equipDT.Columns.Add("OID", typeof(string));
865
            equipDT.Columns.Add("ITEMTAG", typeof(string));
866
            equipDT.Columns.Add("XCOORDS", typeof(string));
867
            equipDT.Columns.Add("YCOORDS", typeof(string));
868

    
869
            foreach (Equipment equipment in equipments)
870
            {
871
                DataRow row = equipDT.NewRow();
872
                row["OID"] = equipment.UID;
873
                if (!string.IsNullOrEmpty(EquipTagNoAttributeName))
874
                {
875
                    Attribute attribute = equipment.Attributes.Find(x => x.Name == EquipTagNoAttributeName);
876
                    if (attribute != null)
877
                        equipment.ItemTag = attribute.Value;
878
                }
879
                else
880
                    equipment.ItemTag = equipment.Name;
881

    
882
                row["ITEMTAG"] = equipment.ItemTag;
883
                List<double> xList = equipment.POINT.Select(x => x[0]).ToList();
884
                row["XCOORDS"] = (xList.Sum() / (double)xList.Count) / DrawingWidth;
885

    
886
                List<double> yList = equipment.POINT.Select(x => x[1]).ToList();
887
                row["YCOORDS"] = (yList.Sum() / (double)yList.Count) / DrawingHeight;
888

    
889
                equipDT.Rows.Add(row);
890
            }
891

    
892
            Equipment = equipDT;
893
            Nozzle = nozzleDT;
894
        }
895
        private void SavePSNData()
896
        {
897
            try
898
            {
899
                DataTable pathItemsDT = new DataTable();
900
                pathItemsDT.Columns.Add("OID", typeof(string));
901
                pathItemsDT.Columns.Add("SequenceData_OID", typeof(string));
902
                pathItemsDT.Columns.Add("TopologySet_OID", typeof(string));
903
                pathItemsDT.Columns.Add("BranchTopologySet_OID", typeof(string));
904
                pathItemsDT.Columns.Add("PipeLine_OID", typeof(string));
905
                pathItemsDT.Columns.Add("ITEMNAME", typeof(string));
906
                pathItemsDT.Columns.Add("ITEMTAG", typeof(string));
907
                pathItemsDT.Columns.Add("Class", typeof(string));
908
                pathItemsDT.Columns.Add("SubClass", typeof(string));
909
                pathItemsDT.Columns.Add("TYPE", typeof(string));
910
                pathItemsDT.Columns.Add("PIDNAME", typeof(string));
911
                pathItemsDT.Columns.Add("NPD", typeof(string));
912
                pathItemsDT.Columns.Add("PipeSystemNetwork_OID", typeof(string));
913
                pathItemsDT.Columns.Add("ViewPipeSystemNetwork_OID", typeof(string));
914
                pathItemsDT.Columns.Add("PipeRun_OID", typeof(string));
915

    
916
                DataTable sequenceDataDT = new DataTable();
917
                sequenceDataDT.Columns.Add("OID", typeof(string));
918
                sequenceDataDT.Columns.Add("SERIALNUMBER", typeof(string));
919
                sequenceDataDT.Columns.Add("PathItem_OID", typeof(string));
920
                sequenceDataDT.Columns.Add("TopologySet_OID_Key", typeof(string));
921

    
922
                DataTable pipeSystemNetworkDT = new DataTable();
923
                pipeSystemNetworkDT.Columns.Add("OID", typeof(string));
924
                pipeSystemNetworkDT.Columns.Add("Type", typeof(string));
925
                pipeSystemNetworkDT.Columns.Add("OrderNumber", typeof(string));
926
                pipeSystemNetworkDT.Columns.Add("Pipeline_OID", typeof(string));
927
                pipeSystemNetworkDT.Columns.Add("FROM_DATA", typeof(string));
928
                pipeSystemNetworkDT.Columns.Add("TO_DATA", typeof(string));
929
                pipeSystemNetworkDT.Columns.Add("TopologySet_OID_Key", typeof(string));
930
                pipeSystemNetworkDT.Columns.Add("PSNRevisionNumber", typeof(string));
931
                pipeSystemNetworkDT.Columns.Add("PBS", typeof(string));
932
                pipeSystemNetworkDT.Columns.Add("PIDDrawings", typeof(string));
933
                pipeSystemNetworkDT.Columns.Add("Validity", typeof(string));
934
                pipeSystemNetworkDT.Columns.Add("Status", typeof(string));
935
                pipeSystemNetworkDT.Columns.Add("IncludingVirtualData", typeof(string));
936
                pipeSystemNetworkDT.Columns.Add("PSNAccuracy", typeof(string));
937

    
938
                DataTable topologySetDT = new DataTable();
939
                topologySetDT.Columns.Add("OID", typeof(string));
940
                topologySetDT.Columns.Add("Type", typeof(string));
941
                topologySetDT.Columns.Add("SubType", typeof(string));
942
                topologySetDT.Columns.Add("HeadItemTag", typeof(string));
943
                topologySetDT.Columns.Add("TailItemTag", typeof(string));
944
                topologySetDT.Columns.Add("HeadItemID", typeof(string));
945
                topologySetDT.Columns.Add("TailItemID", typeof(string));
946

    
947
                // Set Vent/Drain Info
948
                List<VentDrainInfo> VentDrainInfos = new List<VentDrainInfo>();
949
                DataTable dt = DB.SelectVentDrainSetting();
950
                foreach (DataRow row in dt.Rows)
951
                {
952
                    string groupID = row["GROUP_ID"].ToString();
953
                    string desc = row["DESCRIPTION"].ToString();
954
                    int index = Convert.ToInt32(row["INDEX"]);
955
                    string name = row["NAME"].ToString();
956

    
957
                    VentDrainInfo ventDrainInfo = VentDrainInfos.Find(x => x.UID.Equals(groupID));
958
                    if (ventDrainInfo == null)
959
                    {
960
                        ventDrainInfo = new VentDrainInfo(groupID);
961
                        ventDrainInfo.Description = desc;
962
                        VentDrainInfos.Add(ventDrainInfo);
963
                    }
964

    
965
                    ventDrainInfo.VentDrainItems.Add(new VentDrainItem()
966
                    {
967
                        Index = index,
968
                        Name = name
969
                    });
970
                }
971

    
972
                #region Keyword Info
973
                List<KeywordInfo> KeywordInfos = new List<KeywordInfo>();
974
                DataTable dtKeyword = DB.SelectKeywordsSetting();
975
                foreach (DataRow row in dtKeyword.Rows)
976
                {
977
                    string groupID = row["GROUP_ID"].ToString();
978
                    string desc = row["DESCRIPTION"].ToString();
979
                    int index = Convert.ToInt32(row["INDEX"]);
980
                    string name = row["NAME"].ToString();
981
                    string keyword = row["KEYWORD"].ToString();
982

    
983
                    KeywordInfo keywordInfo = KeywordInfos.Find(x => x.UID.Equals(groupID));
984
                    if (keywordInfo == null)
985
                    {
986
                        keywordInfo = new KeywordInfo(groupID);
987
                        keywordInfo.Description = desc;
988
                        KeywordInfos.Add(keywordInfo);
989
                    }
990

    
991
                    keywordInfo.KeywordItems.Add(new KeywordItem()
992
                    {
993
                        Index = index,
994
                        Name = name,
995
                        Keyword = keyword
996
                    });
997
                }               
998
                #endregion
999

    
1000
                // key = 미입력 branch
1001
                Dictionary<Item, Item> startBranchDic = new Dictionary<Item, Item>();
1002
                Dictionary<Item, Item> endBranchDic = new Dictionary<Item, Item>();
1003
                foreach (PSNItem PSNItem in PSNItems)
1004
                {
1005
                    int psnOrder = 0;
1006
                    int index = 0;
1007
                    bool bPSNStart = true;
1008
                    string sPSNData = string.Empty;
1009
                    bool bVentDrain = false;
1010

    
1011
                    //VentDrain 검사
1012
                    if (PSNItem.Groups.Count.Equals(1))
1013
                    {
1014
                        List<VentDrainInfo> endInfos = new List<VentDrainInfo>();
1015
                        Group group = PSNItem.Groups[0];
1016
                        for (int i = 0; i < group.Items.Count; i++)
1017
                        {
1018
                            Item item = group.Items[i];
1019
                            foreach (VentDrainInfo ventDrainInfo in VentDrainInfos)
1020
                            {
1021
                                if (endInfos.Contains(ventDrainInfo) || ventDrainInfo.VentDrainItems.Count != group.Items.Count)
1022
                                    continue;
1023
                                if (!ventDrainInfo.VentDrainItems[i].Name.Equals(item.Name))
1024
                                {
1025
                                    endInfos.Add(ventDrainInfo);
1026
                                    continue;
1027
                                }
1028

    
1029
                                if (i + 1 == group.Items.Count && group.Items.Count.Equals(ventDrainInfo.VentDrainItems.Count))
1030
                                    bVentDrain = true;
1031
                            }
1032
                        }
1033

    
1034
                        if (!bVentDrain)
1035
                        {
1036
                            endInfos = new List<VentDrainInfo>();
1037
                            for (int i = 0; i < group.Items.Count; i++)
1038
                            {
1039
                                Item item = group.Items[group.Items.Count - i - 1];
1040
                                foreach (VentDrainInfo ventDrainInfo in VentDrainInfos)
1041
                                {
1042
                                    if (endInfos.Contains(ventDrainInfo) || ventDrainInfo.VentDrainItems.Count != group.Items.Count)
1043
                                        continue;
1044
                                    if (!ventDrainInfo.VentDrainItems[i].Name.Equals(item.Name))
1045
                                    {
1046
                                        endInfos.Add(ventDrainInfo);
1047
                                        continue;
1048
                                    }
1049

    
1050
                                    if (i + 1 == group.Items.Count && group.Items.Count.Equals(ventDrainInfo.VentDrainItems.Count))
1051
                                        bVentDrain = true;
1052
                                }
1053
                            }
1054
                        }
1055
                    }
1056

    
1057
                    foreach (KeywordInfo keywordInfo in KeywordInfos)
1058
                        keywordInfo.KeywordItems = keywordInfo.KeywordItems.OrderBy(x => x.Index).ToList();
1059

    
1060
                    //PSN, PathItems, SequenceData 관련
1061
                    foreach (Group group in PSNItem.Groups)
1062
                    {
1063
                        foreach (Item item in group.Items)
1064
                        {
1065
                            if (item.BranchItems.Count == 0)
1066
                            {
1067
                                CreatePathItemsDataRow(item.UID, item.ID2DBType);
1068
                                CreateSequenceDataDataRow(item.UID);
1069
                                index++;
1070
                            }
1071
                            else
1072
                            {
1073
                                CreatePathItemsDataRow(item.UID + "_L1", item.ID2DBType);
1074
                                CreateSequenceDataDataRow(item.UID + "_L1");
1075
                                index++;
1076
                                for (int i = 0; i < item.BranchItems.Count; i++)
1077
                                {
1078
                                    CreatePathItemsDataRow(string.Format(item.UID + "_B{0}", i + 1), "Branch", item.BranchItems[i].Topology.FullName, item.BranchItems[i]);
1079
                                    CreateSequenceDataDataRow(string.Format(item.UID + "_B{0}", i + 1));
1080
                                    index++;
1081

    
1082
                                    CreatePathItemsDataRow(string.Format(item.UID + "_L{0}", i + 2), item.ID2DBType);
1083
                                    CreateSequenceDataDataRow(string.Format(item.UID + "_L{0}", i + 2));
1084
                                    index++;
1085

    
1086
                                    if (item.BranchItems[i].Relations[0].Item != null && item.BranchItems[i].Relations[0].Item == item)
1087
                                        startBranchDic.Add(item.BranchItems[i], item);
1088
                                    else if (item.BranchItems[i].Relations[1].Item != null && item.BranchItems[i].Relations[1].Item == item)
1089
                                        endBranchDic.Add(item.BranchItems[i], item);
1090
                                }
1091
                            }
1092

    
1093
                            if (bPSNStart)
1094
                            {
1095
                                CreatePipeSystemNetworkDataRow();
1096
                                sPSNData = item.TopologyData;
1097
                                psnOrder++;
1098
                                bPSNStart = false;
1099
                            }
1100
                            else
1101
                            {
1102
                                if (item.TopologyData != sPSNData)
1103
                                {
1104
                                    CreatePipeSystemNetworkDataRow();
1105
                                    sPSNData = item.TopologyData;
1106
                                    psnOrder++;
1107
                                }
1108
                            }
1109
                            void CreatePathItemsDataRow(string itemOID, string itemType, string branchTopologyName = "", Item branchItem = null)
1110
                            {
1111
                                DataRow newRow = pathItemsDT.NewRow();
1112
                                newRow["OID"] = itemOID;
1113

    
1114
                                newRow["SequenceData_OID"] = string.Format(item.Topology.FullName + "_{0}", index);
1115

    
1116
                                newRow["TopologySet_OID"] = item.Topology.FullName;
1117

    
1118
                                newRow["BranchTopologySet_OID"] = branchTopologyName;
1119
                                newRow["PipeLine_OID"] = item.PSNPipeLineID;
1120
                                newRow["ITEMNAME"] = GetItemName(item, itemOID);
1121
                                newRow["ITEMTAG"] = GetItemTag(item);
1122
                                newRow["Class"] = GetClass(item, itemOID);
1123
                                newRow["SubClass"] = GetSubClass(item, itemOID);
1124

    
1125
                                if (item.ItemType == ItemType.Symbol)
1126
                                    newRow["TYPE"] = item.ID2DBName;
1127
                                else if (item.ItemType == ItemType.Line)
1128
                                    newRow["TYPE"] = item.ID2DBType;
1129
                                newRow["PIDNAME"] = group.Document.DrawingName;
1130

    
1131
                                // NPD
1132
                                if (item.LineNumber != null)
1133
                                {
1134
                                    Attribute attribute = item.LineNumber.Attributes.Find(x => x.Name == "NominalDiameter");
1135
                                    if (attribute != null)
1136
                                        newRow["NPD"] = attribute.Value;
1137
                                }
1138
                                else
1139
                                    newRow["NPD"] = null;
1140

    
1141
                                newRow["PipeSystemNetwork_OID"] = PSNItem.PSN_OID();
1142
                                if (branchItem == null)
1143
                                    newRow["ViewPipeSystemNetwork_OID"] = PSNItem.PSN_OID();
1144
                                else
1145
                                    newRow["ViewPipeSystemNetwork_OID"] = branchItem.PSNItem.PSN_OID();
1146

    
1147
                                newRow["PipeRun_OID"] = item.LineNumber != null ? item.LineNumber.Name : string.Empty;
1148

    
1149
                                pathItemsDT.Rows.Add(newRow);
1150
                            }
1151
                            void CreateSequenceDataDataRow(string itemOID)
1152
                            {
1153
                                DataRow newRow = sequenceDataDT.NewRow();
1154
                                newRow["OID"] = string.Format(item.Topology.FullName + "_{0}", index);
1155
                                newRow["SERIALNUMBER"] = string.Format("{0}", index);
1156
                                newRow["PathItem_OID"] = itemOID;
1157
                                newRow["TopologySet_OID_Key"] = item.Topology.FullName;
1158

    
1159
                                sequenceDataDT.Rows.Add(newRow);
1160
                            }
1161
                            void CreatePipeSystemNetworkDataRow()
1162
                            {
1163
                                // VentDrain의 경우 제외 요청
1164
                                if (bVentDrain)
1165
                                    return;
1166

    
1167
                                List<double> lstAcc = new List<double>();
1168
                                DataRow newRow = pipeSystemNetworkDT.NewRow();
1169
                                newRow["OID"] = PSNItem.PSN_OID();
1170
                                newRow["Type"] = PSNItem.GetPSNType();
1171
                                newRow["OrderNumber"] = psnOrder;
1172
                                newRow["Pipeline_OID"] = item.PSNPipeLineID;
1173

    
1174

    
1175
                                string From_d = PSNItem.GetFromKeywordData();
1176
                                string To_d = PSNItem.GetToKeywordData();
1177

    
1178
                                if (string.IsNullOrEmpty(From_d))
1179
                                    From_d = PSNItem.GetFromData();
1180

    
1181
                                if (string.IsNullOrEmpty(To_d))
1182
                                    To_d = PSNItem.GetToData();
1183

    
1184
                                newRow["FROM_DATA"] = From_d;
1185
                                newRow["TO_DATA"] = To_d;
1186
                                newRow["TopologySet_OID_Key"] = item.Topology.FullName;
1187
                                newRow["PSNRevisionNumber"] = string.Format("V{0:D4}", Revision);
1188
                                newRow["PBS"] = PSNItem.GetPBSData();
1189
                                newRow["Validity"] = PSNItem.Validity;
1190
                                newRow["Status"] = !string.IsNullOrEmpty(PSNItem.Status) ? PSNItem.Status.Remove(0, 2) : string.Empty;
1191

    
1192

    
1193
                                if(!string.IsNullOrEmpty(From_d) || !string.IsNullOrEmpty(To_d))
1194
                                {
1195
                                    if (From_d.Contains(Rule1) || To_d.Contains(Rule1))
1196
                                        lstAcc.Add(0.75);
1197
                                    else if (From_d.Contains(Rule2) || To_d.Contains(Rule2))
1198
                                        lstAcc.Add(0.7);
1199
                                    else if (From_d.Contains(Rule3) || To_d.Contains(Rule3))
1200
                                        lstAcc.Add(0.5);
1201
                                    else if (From_d.Contains(Rule4) || To_d.Contains(Rule4))
1202
                                        lstAcc.Add(0.65);
1203
                                    else if (From_d.Contains(Rule5) || To_d.Contains(Rule5))
1204
                                        lstAcc.Add(0.6);
1205
                                }
1206

    
1207
                                
1208
                                PSNItem.PSNAccuracy = Convert.ToString(Math.Round(Convert.ToDouble(AccuracyCalculation(lstAcc)), 1));
1209

    
1210
                                if (PSNItem.PSNAccuracy == "100")
1211
                                    PSNItem.IncludingVirtualData = "No";
1212

    
1213
                                newRow["IncludingVirtualData"] = !string.IsNullOrEmpty(PSNItem.IncludingVirtualData) ? PSNItem.IncludingVirtualData : "Yes";
1214
                                newRow["PSNAccuracy"] = PSNItem.PSNAccuracy;
1215

    
1216
                                List<string> drawingNames = new List<string>();
1217
                                foreach (Group _group in PSNItem.Groups)
1218
                                {
1219
                                    if (!drawingNames.Contains(_group.Document.DrawingName))
1220
                                    {
1221
                                        if (drawingNames.Count == 0)
1222
                                            newRow["PIDDrawings"] = _group.Document.DrawingName;
1223
                                        else
1224
                                            newRow["PIDDrawings"] = newRow["PIDDrawings"] + ", " + _group.Document.DrawingName;
1225
                                        drawingNames.Add(_group.Document.DrawingName);
1226
                                    }
1227
                                }
1228

    
1229
                                pipeSystemNetworkDT.Rows.Add(newRow);
1230
                            }
1231
                        }
1232
                    }
1233

    
1234

    
1235
                    //TopologySet 관련
1236
                    foreach (Topology topology in PSNItem.Topologies)
1237
                    {
1238
                        DataRow newRow = topologySetDT.NewRow();
1239
                        newRow["OID"] = topology.FullName;
1240
                        newRow["Type"] = topology.FullName.Split(new char[] { '-' }).Last().StartsWith("M") ? "Main" : "Branch";
1241
                        if (bVentDrain)
1242
                            newRow["SubType"] = "Vent_Drain";
1243
                        else
1244
                            newRow["SubType"] = null;
1245
                        newRow["HeadItemTag"] = GetItemTag(topology.Items.Last());
1246
                        newRow["TailItemTag"] = GetItemTag(topology.Items.First());
1247
                        newRow["HeadItemID"] = topology.Items.Last().UID;
1248
                        newRow["TailItemID"] = topology.Items.First().UID;
1249
                        topologySetDT.Rows.Add(newRow);
1250
                    }
1251
                }
1252

    
1253
                foreach (var item in startBranchDic)
1254
                {
1255
                    string uid = item.Key.UID;
1256
                    string topologyName = item.Value.Topology.FullName;
1257
                    DataRow[] rows = pathItemsDT.Select(string.Format("OID LIKE '{0}%'", uid));
1258
                    if (rows.Length == 1)
1259
                        rows.First()["BranchTopologySet_OID"] = topologyName;
1260
                    else if (rows.Length > 1)
1261
                    {
1262
                        DataRow targetRow = null;
1263
                        int index = int.MaxValue;
1264
                        foreach (DataRow row in rows)
1265
                        {
1266
                            string split = row["OID"].ToString().Split(new char[] { '_' })[1];
1267
                            if (split.StartsWith("L"))
1268
                            {
1269
                                int num = Convert.ToInt32(split.Remove(0, 1));
1270
                                if (index > num)
1271
                                {
1272
                                    index = num;
1273
                                    targetRow = row;
1274
                                }
1275
                            }
1276
                        }
1277

    
1278
                        if (targetRow != null)
1279
                            targetRow["BranchTopologySet_OID"] = topologyName;
1280
                    }
1281
                }
1282
                foreach (var item in endBranchDic)
1283
                {
1284
                    string uid = item.Key.UID;
1285
                    string topologyName = item.Value.Topology.FullName;
1286
                    DataRow[] rows = pathItemsDT.Select(string.Format("OID LIKE '{0}%'", uid));
1287
                    if (rows.Length == 1)
1288
                        rows.First()["BranchTopologySet_OID"] = topologyName;
1289
                    else if (rows.Length > 1)
1290
                    {
1291
                        DataRow targetRow = null;
1292
                        int index = int.MinValue;
1293
                        foreach (DataRow row in rows)
1294
                        {
1295
                            string split = row["OID"].ToString().Split(new char[] { '_' })[1];
1296
                            if (split.StartsWith("L"))
1297
                            {
1298
                                int num = Convert.ToInt32(split.Remove(0, 1));
1299
                                if (index < num)
1300
                                {
1301
                                    index = num;
1302
                                    targetRow = row;
1303
                                }
1304
                            }
1305
                        }
1306

    
1307
                        if (targetRow != null)
1308
                            targetRow["BranchTopologySet_OID"] = topologyName;
1309
                    }
1310
                }
1311

    
1312
                PathItems = pathItemsDT;
1313
                SequenceData = sequenceDataDT;
1314
                PipeSystemNetwork = pipeSystemNetworkDT;
1315
                TopologySet = topologySetDT;
1316
            }
1317
            catch (Exception ex)
1318
            {
1319
                Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1320
                MessageBox.Show(ex.Message, "ID2 " + id2Info.ProgramName, MessageBoxButtons.OK, MessageBoxIcon.Information);
1321
            }
1322
        }
1323

    
1324

    
1325
        private double AccuracyCalculation(List<double> lstAcc)
1326
        {
1327
            double acc = 1;
1328
            foreach(double lacc in lstAcc)
1329
            {
1330
                acc *= lacc;
1331
            }
1332
            acc = acc * 100;
1333
            return acc;
1334
        }
1335

    
1336
        private void UpdateSubType()
1337
        {
1338
            foreach (PSNItem PSNItem in PSNItems)
1339
            {
1340
                if (PSNItem.IsBypass)
1341
                {
1342
                    foreach (Topology topology in PSNItem.Topologies)
1343
                    {
1344
                        DataRow[] rows = TopologySet.Select(string.Format("OID = '{0}'", topology.FullName));
1345
                        if (rows.Length.Equals(1))
1346
                            rows.First()["SubType"] = "Bypass";
1347
                    }
1348
                }
1349

    
1350
                if (PSNItem.StartType == PSNType.Header)
1351
                {
1352
                    Topology topology = PSNItem.Topologies.First();
1353
                    DataRow[] rows = TopologySet.Select(string.Format("OID = '{0}'", topology.FullName));
1354
                    if (rows.Length.Equals(1))
1355
                        rows.First()["SubType"] = "Header";
1356
                }
1357
                else if (PSNItem.EndType == PSNType.Header)
1358
                {
1359
                    Topology topology = PSNItem.Topologies.Last();
1360
                    DataRow[] rows = TopologySet.Select(string.Format("OID = '{0}'", topology.FullName));
1361
                    if (rows.Length.Equals(1))
1362
                        rows.First()["SubType"] = "Header";
1363
                }
1364
            }
1365

    
1366
            foreach (Topology topology in Topologies)
1367
            {
1368
                DataRow[] rows = TopologySet.Select(string.Format("OID = '{0}'", topology.FullName));
1369
                if (rows.Length.Equals(1) && rows.First()["SubType"] == null || string.IsNullOrEmpty(rows.First()["SubType"].ToString()))
1370
                {
1371
                    Item firstItem = topology.Items.First();
1372
                    Item lastItem = topology.Items.Last();
1373

    
1374
                    List<Relation> relations = new List<Relation>();
1375
                    relations.AddRange(firstItem.Relations.FindAll(x => x.Item != null && x.Item.Topology.ID != topology.ID));
1376
                    relations.AddRange(lastItem.Relations.FindAll(x => x.Item != null && x.Item.Topology.ID != topology.ID));
1377

    
1378
                    if (relations.Count > 0)
1379
                        rows.First()["SubType"] = "OtherSystem";
1380
                }
1381
            }
1382

    
1383
            foreach (DataRow row in TopologySet.Rows)
1384
                if (row["SubType"] == null || string.IsNullOrEmpty(row["SubType"].ToString()))
1385
                    row["SubType"] = "Normal";
1386
        }
1387
        private bool IsBypass(PSNItem PSNItem)
1388
        {
1389
            bool bResult = false;
1390

    
1391
            if (PSNItem.GetPSNType() == "B2B")
1392
            {
1393
                Group firstGroup = PSNItem.Groups.First();
1394
                Group lastGroup = PSNItem.Groups.Last();
1395
                Item firstItem = firstGroup.Items.First();
1396
                Item lastItem = lastGroup.Items.Last();
1397

    
1398
                Item connectedFirstItem = GetConnectedItemByPSN(firstItem);
1399
                Item connectedLastItem = GetConnectedItemByPSN(lastItem);
1400

    
1401
                if (connectedFirstItem.LineNumber != null && connectedLastItem.LineNumber != null &&
1402
                    !string.IsNullOrEmpty(connectedFirstItem.LineNumber.Name) && !string.IsNullOrEmpty(connectedLastItem.LineNumber.Name) &&
1403
                    connectedFirstItem.LineNumber.Name == connectedLastItem.LineNumber.Name)
1404
                    bResult = true;
1405
                else if (connectedFirstItem.PSNItem == connectedLastItem.PSNItem)
1406
                    bResult = true;
1407
            }
1408

    
1409
            Item GetConnectedItemByPSN(Item item)
1410
            {
1411
                Item result = null;
1412

    
1413
                Relation relation = item.Relations.Find(x => x.Item != null && x.Item.PSNItem != item.PSNItem);
1414
                if (relation != null)
1415
                    result = relation.Item;
1416

    
1417
                return result;
1418
            }
1419

    
1420
            return bResult;
1421
        }
1422
        private void UpdateErrorForPSN()
1423
        {
1424
            DataRow[] errorRows = PipeSystemNetwork.Select(string.Format(" Type = '{0}'", ErrorType.Error));
1425
            foreach (DataRow dataRow in errorRows)
1426
            {
1427
                PSNItem PSNItem = PSNItems.Find(x=>x.PSN_OID() == dataRow["OID"].ToString());
1428
                bool change = false;
1429
                if (!PSNItem.EnableType(PSNItem.StartType))
1430
                {
1431
                    change = true;
1432
                    DataRow[] pathItemRows = PathItems.Select(string.Format("PipeSystemNetwork_OID = '{0}'", dataRow["OID"]));
1433
                    int insertIndex = PathItems.Rows.IndexOf(pathItemRows.First());
1434

    
1435
                    Item item = PSNItem.Groups.First().Items.First();
1436
                    foreach (DataRow loopRow in PipeSystemNetwork.Select(string.Format("OID = '{0}'", PSNItem.PSN_OID())))
1437
                        loopRow["FROM_DATA"] = string.Format("TIEINPOINT_{0:D5}V", tieInPointIndex);
1438
                    tieInPointIndex++;
1439

    
1440

    
1441
                    if (item.ItemType == ItemType.Line)
1442
                    {
1443
                        PathItems.Rows.InsertAt(createTerminatorRow(pathItemRows.First()), insertIndex);
1444
                    }
1445
                    else
1446
                    {
1447
                        PathItems.Rows.InsertAt(createLineRow(PathItems.Select(string.Format("PipeLine_OID = '{0}' AND ItemName = 'PipeRun' AND PipeSystemNetwork_OID = '{1}'", item.PSNPipeLineID, dataRow["OID"])).First()), insertIndex);
1448
                        PathItems.Rows.InsertAt(createTerminatorRow(pathItemRows.First()), insertIndex);
1449
                    }
1450

    
1451
                    PSNItem.StartType = PSNType.Equipment;
1452
                }
1453

    
1454

    
1455
                if (!PSNItem.EnableType(PSNItem.EndType))
1456
                {
1457
                    change = true;
1458
                    DataRow[] pathItemRows = PathItems.Select(string.Format("PipeSystemNetwork_OID = '{0}'", dataRow["OID"]));
1459
                    int insertIndex = PathItems.Rows.IndexOf(pathItemRows.Last());
1460

    
1461
                    Item item = PSNItem.Groups.Last().Items.Last();
1462
                    foreach (DataRow loopRow in PipeSystemNetwork.Select(string.Format("OID = '{0}'", PSNItem.PSN_OID())))
1463
                        loopRow["TO_DATA"] = string.Format("TIEINPOINT_{0:D5}V", tieInPointIndex);
1464
                    tieInPointIndex++;
1465

    
1466

    
1467
                    if (item.ItemType == ItemType.Line)
1468
                    {
1469
                        PathItems.Rows.InsertAt(createTerminatorRow(pathItemRows.Last()), insertIndex + 1);
1470
                    }
1471
                    else
1472
                    {
1473
                        PathItems.Rows.InsertAt(createTerminatorRow(pathItemRows.Last()), insertIndex + 1);
1474
                        PathItems.Rows.InsertAt(createLineRow(PathItems.Select(string.Format("PipeLine_OID = '{0}' AND ItemName = 'PipeRun' AND PipeSystemNetwork_OID = '{1}'", item.PSNPipeLineID, dataRow["OID"])).Last()), insertIndex + 1);
1475
                    }
1476

    
1477
                    PSNItem.EndType = PSNType.Equipment;
1478
                }
1479

    
1480
                dataRow["Type"] = PSNItem.GetPSNType();
1481
                if (change)
1482
                {
1483
                    int rowIndex = 0;
1484
                    foreach (DataRow row in PathItems.Rows)
1485
                    {
1486
                        if (row["PipeSystemNetwork_OID"].ToString() != dataRow["OID"].ToString())
1487
                            continue;
1488
                        string sequenceData = row["SequenceData_OID"].ToString();
1489
                        string[] split = sequenceData.Split(new char[] { '_' });
1490

    
1491
                        StringBuilder sb = new StringBuilder();
1492
                        for (int i = 0; i < split.Length - 1; i++)
1493
                            sb.Append(split[i] + "_");
1494
                        sb.Append(rowIndex++);
1495
                        row["SequenceData_OID"] = sb.ToString(); 
1496
                    }
1497
                }
1498

    
1499
                DataRow createTerminatorRow(DataRow itemRow)
1500
                {
1501
                    DataRow newRow = PathItems.NewRow();
1502
                    newRow["OID"] = Guid.NewGuid().ToString();
1503
                    newRow["SequenceData_OID"] = itemRow["SequenceData_OID"];
1504
                    newRow["TopologySet_OID"] = itemRow["TopologySet_OID"];
1505
                    newRow["BranchTopologySet_OID"] = itemRow["BranchTopologySet_OID"];
1506
                    newRow["PipeLine_OID"] = itemRow["PipeLine_OID"];
1507
                    newRow["ITEMNAME"] = "End of line terminator";
1508
                    newRow["ITEMTAG"] = itemRow["ITEMTAG"];
1509
                    newRow["Class"] = "End of line terminator";
1510
                    newRow["SubClass"] = "End of line terminator";
1511
                    newRow["TYPE"] = "End of line terminator";
1512
                    newRow["PIDNAME"] = itemRow["PIDNAME"];
1513
                    newRow["NPD"] = itemRow["NPD"];
1514
                    newRow["PipeSystemNetwork_OID"] = itemRow["PipeSystemNetwork_OID"];
1515
                    newRow["ViewPipeSystemNetwork_OID"] = itemRow["ViewPipeSystemNetwork_OID"];
1516
                    newRow["PipeRun_OID"] = itemRow["PipeRun_OID"];
1517

    
1518
                    return newRow;
1519
                }
1520

    
1521
                DataRow createLineRow(DataRow itemRow)
1522
                {
1523
                    DataRow newRow = PathItems.NewRow();
1524
                    newRow["OID"] = Guid.NewGuid().ToString();
1525
                    newRow["SequenceData_OID"] = itemRow["SequenceData_OID"];
1526
                    newRow["TopologySet_OID"] = itemRow["TopologySet_OID"];
1527
                    newRow["BranchTopologySet_OID"] = itemRow["BranchTopologySet_OID"];
1528
                    newRow["PipeLine_OID"] = itemRow["PipeLine_OID"];
1529
                    newRow["ITEMNAME"] = itemRow["ITEMNAME"];
1530
                    newRow["ITEMTAG"] = itemRow["ITEMTAG"];
1531
                    newRow["Class"] = itemRow["Class"];
1532
                    newRow["SubClass"] = itemRow["SubClass"];
1533
                    newRow["TYPE"] = itemRow["TYPE"];
1534
                    newRow["PIDNAME"] = itemRow["PIDNAME"];
1535
                    newRow["NPD"] = itemRow["NPD"];
1536
                    newRow["PipeSystemNetwork_OID"] = itemRow["PipeSystemNetwork_OID"];
1537
                    newRow["ViewPipeSystemNetwork_OID"] = itemRow["ViewPipeSystemNetwork_OID"];
1538
                    newRow["PipeRun_OID"] = itemRow["PipeRun_OID"];
1539

    
1540
                    return newRow;
1541
                }
1542
            }            
1543
        }
1544
    }
1545

    
1546
    public class PSNItem
1547
    {
1548
        public PSNItem(int count, int Revision)
1549
        {
1550
            Groups = new List<Group>();
1551
            Topologies = new List<Topology>();
1552

    
1553
            Index = count + 1;
1554
            this.Revision = Revision;
1555
        }
1556
        private int Revision;
1557
        public string UID { get; set; }
1558
        public List<Group> Groups { get; set; }
1559
        public List<Topology> Topologies { get; set; }
1560
        public PSNType StartType { get; set; }
1561
        public PSNType EndType { get; set; }
1562
        public int Index { get; set; }
1563
        public string Validity { get; set; }
1564
        public string Status { get; set; }
1565
        public string IncludingVirtualData { get; set; }
1566
        public string PSNAccuracy { get; set; }
1567

    
1568
        public string PSN_OID()
1569
        {
1570
            return string.Format("V{0}-PSN-{1}", string.Format("{0:D4}", Revision), string.Format("{0:D5}", Index));
1571
        }
1572
        public string GetPSNType()
1573
        {
1574
            string result = string.Empty;
1575

    
1576
            if (EnableType(StartType) && EnableType(EndType))
1577
            {
1578
                if (StartType == PSNType.Equipment && EndType == PSNType.Equipment)
1579
                    result = "E2E";
1580
                else if (StartType == PSNType.Branch && EndType == PSNType.Branch)
1581
                    result = "B2B";
1582
                else if (StartType == PSNType.Header && EndType == PSNType.Header)
1583
                    result = "HD2";
1584

    
1585
                else if (StartType == PSNType.Equipment && EndType == PSNType.Branch)
1586
                    result = "E2B";
1587
                else if (StartType == PSNType.Branch && EndType == PSNType.Equipment)
1588
                    result = "B2E";
1589

    
1590
                else if (StartType == PSNType.Header && EndType == PSNType.Branch)
1591
                    result = "HDB";
1592
                else if (StartType == PSNType.Branch && EndType == PSNType.Header)
1593
                    result = "HDB";
1594

    
1595
                else if (StartType == PSNType.Header && EndType == PSNType.Equipment)
1596
                    result = "HDE";
1597
                else if (StartType == PSNType.Equipment && EndType == PSNType.Header)
1598
                    result = "HDE";
1599
                else
1600
                    result = "Error";
1601
            }
1602
            else
1603
                result = "Error";
1604

    
1605
            return result;
1606

    
1607
            
1608
        }
1609
        public bool EnableType(PSNType type)
1610
        {
1611
            bool result = false;
1612

    
1613
            if (type == PSNType.Branch ||
1614
                type == PSNType.Equipment ||
1615
                type == PSNType.Header)
1616
            {
1617
                result = true;
1618
            }
1619

    
1620
            return result;
1621
        }
1622
        public bool IsBypass { get; set; }
1623

    
1624
        public string GetFromData()
1625
        {
1626
            string result = string.Empty;
1627
            if (StartType == PSNType.Header)
1628
                result = "ENDOFHEADER";
1629
            else if (StartType == PSNType.Branch)
1630
            {
1631
                Item item = Groups.First().Items.First();
1632
                if (!string.IsNullOrEmpty(item.Relations.First().Item.LineNumber.Name))
1633
                    result = item.Relations.First().Item.LineNumber.Name;
1634
                else
1635
                {
1636
                    Status += ", Missing LineNumber";
1637
                    result = "Empty LineNumber";
1638
                }
1639
            }
1640
            else if (StartType == PSNType.Equipment)
1641
                result = Groups.First().Items.First().Equipment.ItemTag;
1642
            else
1643
            {
1644
                Validity = "Error";
1645
                Item item = Groups.First().Items.First();
1646
                if (item.ItemType == ItemType.Symbol)
1647
                {
1648
                    if (item.ID2DBType.Contains("OPC's"))
1649
                        Status += ", OPC Disconneted";
1650
                    else
1651
                        Status += ", Missing ItemTag or Description";
1652

    
1653
                    result = item.ID2DBName;
1654
                }
1655
                else if (item.ItemType == ItemType.Line)
1656
                {
1657
                    Status += ", Line Disconnected";
1658
                    result = !string.IsNullOrEmpty(item.LineNumber.Name) ? item.LineNumber.Name : "Empty LineNumber";
1659
                }
1660
                else
1661
                    result = "Unknown";
1662
            }
1663

    
1664
            return result;
1665
        }
1666

    
1667
        public string GetFromKeywordData()
1668
        {
1669
            string result = string.Empty;
1670
           
1671
            Item item = Groups.First().Items.First();
1672
            if(!string.IsNullOrEmpty(item.Keyword))
1673
            {
1674
                result = item.Keyword;
1675
            }
1676
            return result;
1677
        }
1678

    
1679
        public string GetToKeywordData()
1680
        {
1681
            string result = string.Empty;
1682

    
1683
            Item item = Groups.Last().Items.Last();
1684
            if (!string.IsNullOrEmpty(item.Keyword))
1685
            {
1686
                result = item.Keyword;
1687
            }
1688
            return result;
1689
        }
1690

    
1691
        public string GetToData()
1692
        {
1693
            string result = string.Empty;
1694
            if (EndType == PSNType.Header)
1695
                result = "ENDOFHEADER";
1696
            else if (EndType == PSNType.Branch)
1697
            {
1698
                Item item = Groups.Last().Items.Last();
1699
                if (!string.IsNullOrEmpty(item.Relations.Last().Item.LineNumber.Name))
1700
                    result = item.Relations.Last().Item.LineNumber.Name;
1701
                else
1702
                {
1703
                    Status += ", Missing LineNumber";
1704
                    result = "Empty LineNumber";
1705
                }
1706
            }
1707
            else if (EndType == PSNType.Equipment)
1708
                result = Groups.Last().Items.Last().Equipment.ItemTag;
1709
            else
1710
            {
1711
                Validity = "Error";
1712
                Item item = Groups.Last().Items.Last();
1713
                if (item.ItemType == ItemType.Symbol)
1714
                {
1715
                    if (item.ID2DBType.Contains("OPC's"))
1716
                        Status += ", OPC Disconneted";
1717
                    else
1718
                        Status += ", Missing ItemTag or Description";
1719
                    
1720
                    result = item.ID2DBName;
1721
                }
1722
                else if (item.ItemType == ItemType.Line)
1723
                {
1724
                    Status += ", Line Disconnected";
1725
                    result = !string.IsNullOrEmpty(item.LineNumber.Name) ? item.LineNumber.Name : "Empty LineNumber";
1726
                }
1727
                else
1728
                    result = "Unknown";
1729
            }
1730
            return result;
1731
        }
1732

    
1733
        public string GetPBSData()
1734
        {
1735
            string result = string.Empty;
1736
            List<string> PBSList = new List<string>();
1737
            if (Settings.Default.PBSSetting.Equals("Line Number"))
1738
            {
1739
                string attrValue = Settings.Default.PBSSettingValue;
1740

    
1741
                foreach (Group group in Groups)
1742
                {
1743
                    List<LineNumber> lineNumbers = group.Items.Select(x =>x.LineNumber).Distinct().ToList();
1744
                    foreach (LineNumber lineNumber in lineNumbers)
1745
                    {
1746
                        Attribute attribute = lineNumber.Attributes.Find(x => x.Name == attrValue && !string.IsNullOrEmpty(x.Value));
1747
                        if (attribute != null)
1748
                        {
1749
                            string value = attribute.Value;
1750
                            if (!PBSList.Contains(value))
1751
                                PBSList.Add(value);
1752
                        }
1753
                    }
1754
                }
1755
            }
1756
            else if (Settings.Default.PBSSetting.Equals("Item Attribute"))
1757
            {
1758
                string attrValue = Settings.Default.PBSSettingValue;
1759

    
1760
                foreach (Group group in Groups)
1761
                {
1762
                    List<Item> items = group.Items.FindAll(x => x.Attributes.Find(y => y.Name == attrValue && !string.IsNullOrEmpty(y.Value)) != null);
1763
                    foreach (Item item in items)
1764
                    {
1765
                        string value = item.Attributes.Find(x => x.Name == attrValue).Value;
1766
                        if (!PBSList.Contains(value))
1767
                            PBSList.Add(value);
1768
                    }
1769
                }
1770
            }
1771
            else if (Settings.Default.PBSSetting.Equals("Drawing No"))
1772
            {
1773
                string attrValue = Settings.Default.PBSSettingValue;
1774

    
1775
                foreach (Group group in Groups)
1776
                {
1777
                    List<Document> documents = group.Items.Select(x => x.Document).Distinct().ToList();
1778
                    foreach (Document document in documents)
1779
                    {
1780
                        string name = document.DrawingName;
1781

    
1782
                        int startIndex = Settings.Default.PBSSettingStartValue;
1783
                        int endIndex = Settings.Default.PBSSettingEndValue;
1784

    
1785
                        string subStr = name.Substring(startIndex - 1, endIndex - startIndex + 1);
1786
                        if (!PBSList.Contains(subStr))
1787
                            PBSList.Add(subStr);
1788
                    }
1789
                }
1790
            }
1791
            else if (Settings.Default.PBSSetting.Equals("Unit Area"))
1792
            {
1793
                foreach (Group group in Groups)
1794
                {
1795
                    List<Document> documents = group.Items.Select(x => x.Document).Distinct().ToList();
1796
                    foreach (Document document in documents)
1797
                    {
1798
                        List<TextInfo> textInfos = document.TextInfos.FindAll(x => x.Area == "Unit");
1799
                        foreach (TextInfo textInfo in textInfos)
1800
                        {
1801
                            if (!PBSList.Contains(textInfo.Value))
1802
                                PBSList.Add(textInfo.Value);
1803
                        }
1804
                    }
1805
                }
1806
            }
1807

    
1808
            foreach (var item in PBSList)
1809
            {
1810
                if (string.IsNullOrEmpty(result))
1811
                    result = item;
1812
                else
1813
                    result += ", " + item;
1814
            }
1815
            return result;
1816
        }
1817
    }
1818
}
클립보드 이미지 추가 (최대 크기: 500 MB)