프로젝트

일반

사용자정보

통계
| 개정판:

hytos / DTI_PID / ID2PSN / PSN.cs @ eaa41534

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

1 6b9e7a56 gaqhf
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 7881ec8f gaqhf
using ID2PSN.Properties;
9
using System.Text.RegularExpressions;
10 6b9e7a56 gaqhf
11
namespace ID2PSN
12
{
13
    public enum PSNType
14
    {
15
        None,
16
        Branch,
17
        Equipment,
18
        Header,
19
        Symbol,
20
        OPC,
21
    }
22
23
    public class PSN
24
    {
25 8f24b438 gaqhf
        private double[] DrawingSize = null;
26
        private double DrawingWidth = double.NaN;
27
        private double DrawingHeight = double.NaN;
28
        public int Revision;
29 c6503eaa gaqhf
        public string EquipTagNoAttributeName = string.Empty;
30 6b9e7a56 gaqhf
        public DataTable PathItems { get; set; }
31
        public DataTable SequenceData { get; set; }
32
        public DataTable PipeSystemNetwork { get; set; }
33 36a45f13 gaqhf
        public DataTable TopologySet { get; set; }
34 6b9e7a56 gaqhf
        public DataTable Equipment { get; set; }
35
        public DataTable Nozzle { get; set; }
36
37
        List<Document> Documents;
38
        List<Group> groups = new List<Group>();
39
        List<PSNItem> PSNItems = new List<PSNItem>();
40
        List<Topology> Topologies = new List<Topology>();
41
42
        DataTable opcDT = null;
43
        DataTable topologyRuleDT = null;
44
45
        const string FluidPriorityType = "FLUIDCODE";
46
        const string PipingMaterialsPriorityType = "PIPINGMATERIALSCLASS";
47
48 5c248ee3 gaqhf
        public PSN()
49
        {
50
            
51
        }
52
53 8f24b438 gaqhf
        public PSN(List<Document> documents, int Revision)
54 6b9e7a56 gaqhf
        {
55
            Documents = documents;
56
            foreach (Document document in Documents)
57
                groups.AddRange(document.Groups);
58
            opcDT = GetOPCInfo();
59
            topologyRuleDT = GetTopologyRule();
60 8f24b438 gaqhf
            this.Revision = Revision;
61
            DrawingSize = DB.GetDrawingSize();
62
            DrawingWidth = DrawingSize[2] - DrawingSize[0];
63
            DrawingHeight = DrawingSize[3] - DrawingSize[1];
64 6b9e7a56 gaqhf
        }
65
66 36a45f13 gaqhf
        private string GetItemTag(Item item)
67
        {
68
            string result = string.Empty;
69
            if (item.ItemType == ItemType.Line)
70
                result = item.LineNumber != null ? item.LineNumber.Name : string.Empty;
71
            else if (item.ItemType == ItemType.Symbol && item.SubItemType == SubItemType.Nozzle)
72
                result = Nozzle.Select(string.Format("OID = '{0}'", item.UID)).First()["ITEMTAG"].ToString();
73
74
            return result;
75
        }
76 7881ec8f gaqhf
        private string GetItemName(Item item, string itemOID)
77
        {
78
            string result = string.Empty;
79
            if (item.ItemType == ItemType.Line && (item.Name == "Secondary" || item.Name == "Primary"))
80
            {
81
                if (itemOID.Contains("_"))
82
                {
83
                    string split = itemOID.Split(new char[] { '_' })[1];
84
                    if (split.StartsWith("B"))
85
                        result = "Branch";
86
                    else
87
                        result = "PipeRun";
88
                }
89
                else
90
                    result = "PipeRun";
91
            }
92
            else if (item.ItemType == ItemType.Symbol)
93
            {
94
                if (item.ID2DBCategory == "Instrumentation")
95
                    result = "Instrument";
96
                else if (item.ID2DBType == "Nozzles")
97
                    result = "Nozzle";
98
                else if (item.ID2DBType == "Fittings" ||
99
                        item.ID2DBType == "Piping OPC's" ||
100
                        item.ID2DBType == "Specialty Components" ||
101
                        item.ID2DBType == "Valves" ||
102
                        item.ID2DBType == "Reducers")
103
                    result = "PipingComp";
104
            }
105
            return result;
106
        }
107
108
        private string GetClass(Item item, string itemOID)
109
        {
110
            string result = string.Empty;
111
            if (item.ItemType == ItemType.Line && (item.Name == "Secondary" || item.Name == "Primary"))
112
            {
113
                if (itemOID.Contains("_"))
114
                {
115
                    string split = itemOID.Split(new char[] { '_' })[1];
116
                    if (split.StartsWith("B"))
117
                        result = "Branch";
118
                    else
119
                        result = "Piping";
120
                }
121
                else
122
                    result = "Piping";
123
            }
124
            else if (item.ItemType == ItemType.Symbol)
125
            {
126
                if (item.ID2DBCategory == "Instrumentation")
127
                    result = item.ID2DBType;
128
                else if (item.ID2DBType == "Nozzles")
129
                    result = string.Empty;
130
                else if (item.ID2DBType == "Fittings" ||
131
                       item.ID2DBType == "Piping OPC's" ||
132
                       item.ID2DBType == "Specialty Components" ||
133
                       item.ID2DBType == "Valves" ||
134
                       item.ID2DBType == "Reducers")
135
                    result = item.ID2DBType;
136
            }
137
            return result;
138
        }
139
140
        private string GetSubClass(Item item, string itemOID)
141
        {
142
            string result = string.Empty;
143
            if (item.ItemType == ItemType.Line && (item.Name == "Secondary" || item.Name == "Primary"))
144
            {
145
                if (itemOID.Contains("_"))
146
                {
147
                    string split = itemOID.Split(new char[] { '_' })[1];
148
                    if (split.StartsWith("B"))
149
                        result = "Tee";
150
                    else
151
                        result = "";
152
                }
153
                else
154
                    result = "";
155
            }
156
            else if (item.ItemType == ItemType.Symbol)
157
            {
158
                if (item.ID2DBCategory == "Instrumentation")
159
                    result = string.Empty;
160
                else if (item.ID2DBType == "Nozzles")
161
                    result = string.Empty;
162
                else if (item.ID2DBType == "Fittings" ||
163
                       item.ID2DBType == "Piping OPC's" ||
164
                       item.ID2DBType == "Specialty Components" ||
165
                       item.ID2DBType == "Valves" ||
166
                       item.ID2DBType == "Reducers")
167
                    result = "In-line component";
168
            }
169
            return result;
170
        }
171 36a45f13 gaqhf
172 6b9e7a56 gaqhf
        public void SetPSNData()
173
        {
174 7881ec8f gaqhf
            // Item들의 속성으로 Topology Data를 생성한다.
175
            // Topology Data는 Topology Rule Setting을 기준으로 생성한다. 
176 6b9e7a56 gaqhf
            SetTopologyData();
177 7881ec8f gaqhf
            // ID2의 OPC연결 Data 기반으로 Group(도면단위 PSN)을 연결한다.
178 6b9e7a56 gaqhf
            ConnectByOPC();
179 7881ec8f gaqhf
            // 실제 PSN 생성 로직
180
            // 연결된 Group을 하나의 PSN으로 만든다.
181 6b9e7a56 gaqhf
            SetPSNItem();
182 7881ec8f gaqhf
            // 생성된 PSN의 Type을 설정한다.
183 6b9e7a56 gaqhf
            SetPSNType();
184 7881ec8f gaqhf
            // ID2에는 Branch 정보가 없어서 Branch 정보를 생성한다.
185 6b9e7a56 gaqhf
            SetBranchInfo();
186 7881ec8f gaqhf
            // 생성된 Topology Data들을 정리하며 Main, Branch를 판단한다.
187 6b9e7a56 gaqhf
            SetTopology();
188 7881ec8f gaqhf
            // PSN이 Bypass인지 검사 
189
            SetPSNBypass();
190
            // Topology들에게 Index를 부여한다
191
            SetTopologyIndex();
192
            
193
            // Nozzle, Equipment의 정보를 저장
194 6b9e7a56 gaqhf
            SaveNozzleAndEquipment();
195 7881ec8f gaqhf
            // PSN의 정보를 저장
196 6b9e7a56 gaqhf
            SavePSNData();
197 7881ec8f gaqhf
            // Topology의 subtype을 update(bypass, Header, 등등) 
198
            UpdateSubType();
199
            // Update Error
200 f89175b9 gaqhf
            //UpdateErrorForPSN();
201 6b9e7a56 gaqhf
        }
202 7881ec8f gaqhf
203 6b9e7a56 gaqhf
        private void SetTopologyData()
204
        {
205
            foreach (Document document in Documents)
206
            {
207
                foreach (Item item in document.Items)
208
                {
209
                    item.TopologyData = string.Empty;
210 8f24b438 gaqhf
                    item.PSNPipeLineID = string.Empty;
211 6b9e7a56 gaqhf
                    LineNumber lineNumber = document.LineNumbers.Find(x => x.UID == item.Owner);
212
                    if (lineNumber != null)
213
                    {
214
                        item.LineNumber = lineNumber;
215
216
                        foreach (DataRow row in topologyRuleDT.Rows)
217
                        {
218
                            string uid = row["UID"].ToString();
219
                            if (uid == "-")
220
                                item.TopologyData += "-"; 
221
                            else
222
                            {
223 f25b787a gaqhf
                                Attribute itemAttr = item.Attributes.Find(x => x.Name == uid);
224
225
                                Attribute attribute = lineNumber.Attributes.Find(x => x.Name == uid);
226 6b9e7a56 gaqhf
                                if (attribute != null)
227
                                    item.TopologyData += attribute.Value;
228
                            }
229
                        }
230 8f24b438 gaqhf
231
                        Attribute insulAttr = item.LineNumber.Attributes.Find(x => x.Name == "InsulationPurpose");
232 0f07fa34 gaqhf
                        if (insulAttr != null && !string.IsNullOrEmpty(insulAttr.Value))
233 8f24b438 gaqhf
                            item.PSNPipeLineID = item.TopologyData + "-" + insulAttr.Value;
234
                        else
235
                            item.PSNPipeLineID = item.TopologyData;
236 6b9e7a56 gaqhf
                    }
237 0f07fa34 gaqhf
                    else
238
                    {
239
                        item.TopologyData = "Empty LineNumber";
240
                        item.LineNumber = new LineNumber();
241
                    }
242 6b9e7a56 gaqhf
                }
243
            }
244
245 0f07fa34 gaqhf
            int emptyIndex = 1;
246
            foreach (Group group in groups)
247
            {
248
                List<Item> groupItems = group.Items.FindAll(x => x.TopologyData == "Empty LineNumber");
249
                if (groupItems.Count > 0)
250
                {
251
                    foreach (var item in groupItems)
252
                        item.TopologyData += string.Format("-{0}", emptyIndex);
253
                    emptyIndex++;
254
                }
255
            }
256 6b9e7a56 gaqhf
257
        }
258
        private void ConnectByOPC()
259
        {
260
            foreach (Group group in groups.FindAll(x => x.Items.Last().SubItemType == SubItemType.OPC))
261
            {
262
                Item opcItem = group.Items.Last();
263
                DataRow[] fromRows = opcDT.Select(string.Format("FromOPCUID = '{0}'", opcItem.UID));
264
                if (fromRows.Length.Equals(1))
265
                {
266
                    DataRow opcRow = fromRows.First();
267
                    string toDrawing = opcRow["ToDrawing"].ToString();
268
                    string toOPCUID = opcRow["ToOPCUID"].ToString();
269
270
                    Document toDocument = Documents.Find(x => x.DrawingName == toDrawing);
271
                    Group toGroup = toDocument.Groups.Find(x => x.Items.Find(y => y.UID == toOPCUID) != null);
272
                    group.EndGroup = toGroup;
273
                    toGroup.StartGroup = group;
274
                }
275
            }
276
        }
277
        private void SetPSNItem()
278
        {
279
            Dictionary<Group, string> groupDic = new Dictionary<Group, string>(); 
280
            foreach (Group group in groups)
281
                groupDic.Add(group, Guid.NewGuid().ToString());
282
283
            foreach (Group group in groups)
284
            {
285
                string groupKey = groupDic[group];
286
                if (group.StartGroup != null)
287
                {
288
                    string otherKey = groupDic[group.StartGroup];
289
                    ChangeGroupID(otherKey, groupKey);
290
                }
291
            }
292
293
            // PSN 정리
294
            foreach (var item in groupDic)
295
            {
296
                Group group = item.Key;
297
                string uid = item.Value;
298
                PSNItem PSNItem = PSNItems.Find(x => x.UID == uid);
299
                if (PSNItem == null)
300
                {
301 8f24b438 gaqhf
                    PSNItem = new PSNItem(PSNItems.Count, Revision) { UID = uid };
302 6b9e7a56 gaqhf
                    PSNItems.Add(PSNItem);
303
                }
304
                PSNItem.Groups.Add(group);
305 36a45f13 gaqhf
                foreach (Item groupItem in group.Items)
306
                    groupItem.PSNItem = PSNItem;
307 6b9e7a56 gaqhf
            }
308
309
            // Sort PSN
310
            foreach (PSNItem PSNItem in PSNItems)
311
            {
312
                List<Group> _groups = new List<Group>();
313
314
                Stack<Group> stacks = new Stack<Group>();
315
                stacks.Push(PSNItem.Groups.First());
316
                while (stacks.Count > 0)
317
                {
318
                    Group stack = stacks.Pop();
319
                    if (_groups.Contains(stack))
320
                        continue;
321
322
                    if (_groups.Count == 0)
323
                        _groups.Add(stack);
324
                    else
325
                    {
326
                        if (stack.StartGroup != null && _groups.Contains(stack.StartGroup))
327
                        {
328
                            int index = _groups.IndexOf(stack.StartGroup);
329
                            _groups.Insert(index + 1, stack);
330
                        }
331
                        else if (stack.EndGroup != null && _groups.Contains(stack.EndGroup))
332
                        {
333
                            int index = _groups.IndexOf(stack.EndGroup);
334
                            _groups.Insert(index, stack);
335
                        }
336
                    }
337
338
                    if (stack.StartGroup != null)
339
                        stacks.Push(stack.StartGroup);
340
                    if (stack.EndGroup != null)
341
                        stacks.Push(stack.EndGroup);
342
                }
343
344
                PSNItem.Groups.Clear();
345
                PSNItem.Groups.AddRange(_groups);
346
            }
347
348
            void ChangeGroupID(string from, string to)
349
            {
350
                if (from.Equals(to))
351
                    return;
352
353
                List<Group> changeItems = new List<Group>();
354
                foreach (var _item in groupDic)
355
                    if (_item.Value.Equals(from))
356
                        changeItems.Add(_item.Key);
357
                foreach (var _item in changeItems)
358
                    groupDic[_item] = to;
359
            }
360
        }
361
        private void SetPSNType()
362
        {
363
            foreach (PSNItem PSNItem in PSNItems)
364
            {
365
                Group firstGroup = PSNItem.Groups.First();
366
                Group lastGroup = PSNItem.Groups.Last();
367
368
                Item firstItem = firstGroup.Items.First();
369
                Item lastItem = lastGroup.Items.Last();
370
371
                PSNItem.StartType = GetPSNType(firstItem, true);
372
                PSNItem.EndType = GetPSNType(lastItem, false);
373
            }
374
375
            PSNType GetPSNType(Item item, bool bFirst = true)
376
            {
377
                PSNType type = PSNType.None;
378
379
                if (item.ItemType == ItemType.Line)
380
                {
381
                    Group group = groups.Find(x => x.Items.Contains(item));
382
                    if (bFirst && item.Relations[0].Item != null && !group.Items.Contains(item.Relations[0].Item))
383
                    {
384
                        Item connItem = item.Relations[0].Item;
385
                        if (connItem.ItemType == ItemType.Line && !IsConnected(item, connItem))
386
                            type = PSNType.Branch;
387
                        else if (connItem.ItemType == ItemType.Symbol)
388
                            type = PSNType.Symbol;
389
                    }
390
                    else if (!bFirst && item.Relations[1].Item != null && !group.Items.Contains(item.Relations[1].Item))
391
                    {
392
                        Item connItem = item.Relations[1].Item;
393
                        if (connItem.ItemType == ItemType.Line && !IsConnected(item, connItem))
394
                            type = PSNType.Branch;
395
                        else if (connItem.ItemType == ItemType.Symbol)
396
                            type = PSNType.Symbol;
397
                    }
398
                }
399
                else if (item.ItemType == ItemType.Symbol)
400
                {
401
                    if (item.SubItemType == SubItemType.Nozzle)
402
                        type = PSNType.Equipment;
403
                    else if (item.SubItemType == SubItemType.Header)
404
                        type = PSNType.Header;
405
                    else if (item.SubItemType == SubItemType.OPC)
406
                        type = PSNType.OPC;
407
                }
408
409
                return type;
410
            }
411
        }
412
        private void SetBranchInfo()
413
        {
414
            foreach (Document document in Documents)
415
            {
416
                List<Item> lines = document.Items.FindAll(x => x.ItemType == ItemType.Line).ToList();
417
                foreach (Item line in lines)
418
                {
419
                    double[] point = line.Relations[0].Point;
420
                    List<Item> connLines = lines.FindAll(x => x.Relations.Find(y => y.UID == line.UID) != null && line.Relations.Find(y => y.UID == x.UID) == null);
421
                    connLines.Sort(SortBranchLine);
422
                    line.BranchItems.AddRange(connLines);
423
424
                    int SortBranchLine(Item a, Item b)
425
                    {
426
                        double[] pointA = a.Relations[0].UID == line.UID ? a.Relations[0].Point : a.Relations[1].Point;
427
                        double distanceA = CalcPointToPointdDistance(point[0], point[1], pointA[0], pointA[1]);
428
429
                        double[] pointB = b.Relations[0].UID == line.UID ? b.Relations[0].Point : b.Relations[1].Point;
430
                        double distanceB = CalcPointToPointdDistance(point[0], point[1], pointB[0], pointB[1]);
431
432
                        // 내림차순
433
                        return distanceA.CompareTo(distanceB);
434
                    }
435
                    double CalcPointToPointdDistance(double x1, double y1, double x2, double y2)
436
                    {
437
                        return Math.Pow(Math.Pow(x1 - x2, 2) + Math.Pow(y1 - y2, 2), 0.5);
438
                    }
439
                }
440
            }
441
        }
442
        private void SetTopology()
443
        {
444
            #region 기본 topology 정리
445
            foreach (PSNItem PSNItem in PSNItems)
446
            {
447
                Topology topology = null;
448
                foreach (Group group in PSNItem.Groups)
449
                {
450
                    foreach (Item item in group.Items)
451
                    {
452
                        if (string.IsNullOrEmpty(item.TopologyData))
453
                            topology = null;
454
                        else
455
                        {
456
                            if (topology == null)
457
                            {
458
                                topology = new Topology()
459
                                {
460
                                    ID = item.TopologyData
461
                                };
462
                                Topologies.Add(topology);
463
464
                                if (!PSNItem.Topologies.Contains(topology))
465
                                    PSNItem.Topologies.Add(topology);
466
                            }
467
                            else
468
                            {
469
                                if (topology.ID != item.TopologyData)
470
                                {
471
                                    topology = new Topology()
472
                                    {
473
                                        ID = item.TopologyData
474
                                    };
475
                                    Topologies.Add(topology);
476
477
                                    if (!PSNItem.Topologies.Contains(topology))
478
                                        PSNItem.Topologies.Add(topology);
479
                                }
480
                            }
481
482
                            item.Topology = topology;
483
                            topology.Items.Add(item);
484
                        }
485
                    }
486
                }
487
            }
488
            #endregion
489
490
            #region Type
491
            List<string> ids = Topologies.Select(x => x.ID).Distinct().ToList();
492
            foreach (string id in ids)
493
            {
494
                List<Topology> topologies = Topologies.FindAll(x => x.ID == id);
495 678760c6 gaqhf
496 6b9e7a56 gaqhf
                // Main
497 678760c6 gaqhf
                List<Topology> mainTopologies = FindMainTopology(topologies);
498
                foreach (Topology topology in mainTopologies)
499
                    topology.Type = "M";
500 6b9e7a56 gaqhf
501
                // Branch
502
                List<Topology> branchToplogies = topologies.FindAll(x => string.IsNullOrEmpty(x.Type));
503
                foreach (Topology topology in branchToplogies)
504
                    topology.Type = "B";
505
            }
506
            #endregion
507
        }
508 7881ec8f gaqhf
        private void SetTopologyIndex()
509
        {
510
            List<string> ids = Topologies.Select(x => x.ID).Distinct().ToList();
511
            foreach (string id in ids)
512
            {
513
                List<Topology> topologies = Topologies.FindAll(x => x.ID == id);
514
515
                // Main
516
                List<Topology> mainTopologies = topologies.FindAll(x => x.Type == "M");
517
                foreach (Topology topology in mainTopologies)
518
                    topology.Index = mainTopologies.IndexOf(topology).ToString();
519
520
                // Branch
521
                List<Topology> branchToplogies = topologies.FindAll(x => x.Type == "B");
522
                foreach (Topology topology in branchToplogies)
523
                    topology.Index = (branchToplogies.IndexOf(topology) + 1).ToString();
524
            }
525
        }
526
        private void SetPSNBypass()
527
        {
528
            foreach (PSNItem PSNItem in PSNItems)
529
                PSNItem.IsBypass = IsBypass(PSNItem);
530
        }
531 6b9e7a56 gaqhf
        private List<Topology> FindMainTopology(List<Topology> data)
532
        {
533 678760c6 gaqhf
            DataTable nominalDiameterDT = DB.SelectNominalDiameter();
534
            DataTable PMCDT = DB.SelectPSNPIPINGMATLCLASS();
535
            DataTable fluidCodeDT = DB.SelectPSNFluidCode();
536
537
            List<Topology> main = new List<Topology>();
538
            main.AddRange(data);
539 6b9e7a56 gaqhf
            //
540 678760c6 gaqhf
            main = GetNozzleTopology(data);
541
            if (main.Count == 1)
542
                return main;
543
            else
544
            {
545
                if (main.Count > 0)
546
                    main = GetPMCTopology(main);
547
                else
548
                    main = GetPMCTopology(data);
549
550
                if (main.Count == 1)
551
                    return main;
552
                else
553
                {
554
                    if (main.Count > 0)
555
                        main = GetDiaTopology(main);
556
                    else
557
                        main = GetDiaTopology(data);
558
559
560
                    if (main.Count == 1)
561
                        return main;
562
                    else
563
                    {
564
                        if (main.Count > 0)
565
                            main = GetItemTopology(main);
566
                        else
567
                            main = GetItemTopology(data);
568
                    }
569
                }
570
            }
571
572
            List<Topology> GetNozzleTopology(List<Topology> topologies)
573
            {
574
                return topologies.FindAll(x => x.Items.Find(y => y.SubItemType == SubItemType.Nozzle) != null);
575
            }
576 6b9e7a56 gaqhf
577 678760c6 gaqhf
            List<Topology> GetPMCTopology(List<Topology> topologies)
578
            {
579
                List<Topology> result = new List<Topology>();
580
                foreach (DataRow row in PMCDT.Rows)
581
                {
582
                    string value = row["CODE"].ToString();
583
                    foreach (Topology topology in topologies)
584
                    {
585
                        foreach (Item item in topology.Items)
586
                        {
587
                            if (item.LineNumber == null)
588
                                continue;
589 0f07fa34 gaqhf
                            Attribute attribute = item.LineNumber.Attributes.Find(x => x.Name == "PipingMaterialsClass");
590
                            if (attribute != null && value == attribute.Value)
591 678760c6 gaqhf
                            {
592
                                result.Add(topology);
593
                                break;
594
                            }
595
                        }
596
                    }
597
598
                    if (result.Count > 0)
599
                        break;
600
                }
601
602
                return result;
603
            }
604
605
            List<Topology> GetDiaTopology(List<Topology> topologies)
606
            {
607
                List<Topology> result = new List<Topology>();
608
                foreach (DataRow row in nominalDiameterDT.Rows)
609
                {
610
                    string inchValue = row["InchStr"].ToString();
611
                    string metricValue = row["MetricStr"].ToString();
612
                    foreach (Topology topology in topologies)
613
                    {
614
                        foreach (Item item in topology.Items)
615
                        {
616
                            if (item.LineNumber == null)
617
                                continue;
618 0f07fa34 gaqhf
                            Attribute attribute = item.LineNumber.Attributes.Find(x => x.Name == "NominalDiameter");
619
                            if (attribute != null && (inchValue == attribute.Value || metricValue == attribute.Value))
620 678760c6 gaqhf
                            {
621
                                result.Add(topology);
622
                                break;
623
                            }
624
                        }
625
                    }
626
627
                    if (result.Count > 0)
628
                        break;
629
                }
630
631
                return result;
632
            }
633
634
            List<Topology> GetItemTopology(List<Topology> topologies)
635
            {
636
                return new List<Topology>() { topologies.OrderByDescending(x => x.Items.Count).ToList().First() };
637
            }
638 6b9e7a56 gaqhf
639 678760c6 gaqhf
            return main;
640 6b9e7a56 gaqhf
        }
641
642
        private DataTable GetOPCInfo()
643
        {
644
            DataTable opc = DB.SelectOPCRelations();
645
            DataTable drawing = DB.SelectDrawings();
646
647
            DataTable dt = new DataTable();
648
            dt.Columns.Add("FromDrawing", typeof(string));
649
            dt.Columns.Add("FromDrawingUID", typeof(string));
650
            dt.Columns.Add("FromOPCUID", typeof(string));
651
            dt.Columns.Add("ToDrawing", typeof(string));
652
            dt.Columns.Add("ToDrawingUID", typeof(string));
653
            dt.Columns.Add("ToOPCUID", typeof(string));
654
            foreach (DataRow row in opc.Rows)
655
            {
656
                string fromDrawingUID = row["From_Drawings_UID"] == null ? string.Empty : row["From_Drawings_UID"].ToString();
657
                string fromOPCUID = row["From_OPC_UID"] == null ? string.Empty : row["From_OPC_UID"].ToString(); 
658
                string toDrawingUID = row["To_Drawings_UID"] == null ? string.Empty : row["To_Drawings_UID"].ToString(); 
659
                string toOPCUID = row["To_OPC_UID"] == null ? string.Empty : row["To_OPC_UID"].ToString();
660
                if (!string.IsNullOrEmpty(toOPCUID))
661
                {
662
                    DataRow[] fromRows = drawing.Select(string.Format("UID = '{0}'", fromDrawingUID));
663
                    DataRow[] toRows = drawing.Select(string.Format("UID = '{0}'", toDrawingUID));
664
                    if (fromRows.Length.Equals(1) && toRows.Length.Equals(1))
665
                    {
666
                        string fromDrawingName = Path.GetFileNameWithoutExtension(fromRows.First()["NAME"].ToString());
667
                        string toDrawingName = Path.GetFileNameWithoutExtension(toRows.First()["NAME"].ToString());
668
669
                        DataRow newRow = dt.NewRow();
670
                        newRow["FromDrawing"] = fromDrawingName;
671
                        newRow["FromDrawingUID"] = fromDrawingUID;
672
                        newRow["FromOPCUID"] = fromOPCUID;
673
                        newRow["ToDrawing"] = toDrawingName;
674
                        newRow["ToDrawingUID"] = toDrawingUID;
675
                        newRow["ToOPCUID"] = toOPCUID;
676
677
                        dt.Rows.Add(newRow);
678
                    }
679
                }
680
            }
681
682
            return dt;
683
        }
684
        private DataTable GetTopologyRule()
685
        {
686
            DataTable dt = DB.SelectTopologyRule();
687
688
            return dt;
689
        }
690
        private bool IsConnected(Item item1, Item item2)
691
        {
692
            if (item1.Relations.Find(x => x.UID.Equals(item2.UID)) != null &&
693
                item2.Relations.Find(x => x.UID.Equals(item1.UID)) != null)
694
                return true;
695
            else
696
                return false;
697
        }
698
699
        private void SaveNozzleAndEquipment()
700
        {
701
            List<Item> nozzles = new List<Item>();
702
            List<Equipment> equipments = new List<Equipment>();
703
            foreach (Document document in Documents)
704
            {
705
                nozzles.AddRange(document.Items.FindAll(x => x.SubItemType == SubItemType.Nozzle));
706
                equipments.AddRange(document.Equipments);
707
            }
708
                
709
710
            DataTable nozzleDT = new DataTable();
711
            nozzleDT.Columns.Add("OID", typeof(string));
712
            nozzleDT.Columns.Add("ITEMTAG", typeof(string));
713
            nozzleDT.Columns.Add("XCOORDS", typeof(string));
714
            nozzleDT.Columns.Add("YCOORDS", typeof(string));
715
            nozzleDT.Columns.Add("Equipment_OID", typeof(string));
716
            nozzleDT.Columns.Add("FLUID", typeof(string));
717
            nozzleDT.Columns.Add("NPD", typeof(string));
718
            nozzleDT.Columns.Add("ROTATION", typeof(string));
719
            nozzleDT.Columns.Add("FlowDirection", typeof(string));
720
721
            foreach (Item item in nozzles)
722
            {
723
                DataRow row = nozzleDT.NewRow();
724
                row["OID"] = item.UID;
725
726
                Relation relation = item.Relations.Find(x => equipments.Find(y => y.UID == x.UID) != null);
727
                if (relation != null)
728
                {
729
                    Equipment equipment = equipments.Find(x => x.UID == relation.UID);
730
                    equipment.Nozzles.Add(item);
731 8f24b438 gaqhf
                    row["ITEMTAG"] = string.Format("N-{0}", string.Format("{0:D3}", equipment.Nozzles.Count + 100));
732 6b9e7a56 gaqhf
                    row["Equipment_OID"] = equipment.UID;
733 4c76a67a gaqhf
                    item.Equipment = equipment;
734 6b9e7a56 gaqhf
                }
735 8f24b438 gaqhf
                row["XCOORDS"] = (item.POINT[0] / DrawingWidth).ToString();
736
                row["YCOORDS"] = (item.POINT[1] / DrawingHeight).ToString();
737 6b9e7a56 gaqhf
                Attribute fluidAttr = item.LineNumber.Attributes.Find(x => x.Name == "FluidCode");
738
                row["FLUID"] = fluidAttr != null ? fluidAttr.Value : string.Empty;
739
                Attribute npdAttr = item.LineNumber.Attributes.Find(x => x.Name == "NominalDiameter");
740
                row["NPD"] = npdAttr != null ? npdAttr.Value : string.Empty;
741 f25b787a gaqhf
742
                double angle = Math.PI * 2 - Convert.ToDouble(item.ANGLE);
743
                if (angle >= Math.PI * 2)
744
                    angle = angle - Math.PI * 2;
745
                row["ROTATION"] = angle.ToString();
746 6b9e7a56 gaqhf
747
                if (item.Topology.Items.First().Equals(item))
748
                    row["FlowDirection"] = "Outlet";
749
                else if (item.Topology.Items.Last().Equals(item))
750
                    row["FlowDirection"] = "Inlet";
751
                else
752
                    row["FlowDirection"] = string.Empty;
753
754
                nozzleDT.Rows.Add(row);
755
            }
756
757
            DataTable equipDT = new DataTable();
758
            equipDT.Columns.Add("OID", typeof(string));
759
            equipDT.Columns.Add("ITEMTAG", typeof(string));
760
            equipDT.Columns.Add("XCOORDS", typeof(string));
761
            equipDT.Columns.Add("YCOORDS", typeof(string));
762
763
            foreach (Equipment equipment in equipments)
764
            {
765
                DataRow row = equipDT.NewRow();
766
                row["OID"] = equipment.UID;
767 c6503eaa gaqhf
                if (!string.IsNullOrEmpty(EquipTagNoAttributeName))
768
                {
769
                    Attribute attribute = equipment.Attributes.Find(x => x.Name == EquipTagNoAttributeName);
770
                    if (attribute != null)
771
                        equipment.ItemTag = attribute.Value;
772
                }
773
                else
774
                    equipment.ItemTag = equipment.Name;
775 6b9e7a56 gaqhf
776 c6503eaa gaqhf
                row["ITEMTAG"] = equipment.ItemTag;
777 6b9e7a56 gaqhf
                List<double> xList = equipment.POINT.Select(x => x[0]).ToList();
778 8f24b438 gaqhf
                row["XCOORDS"] = (xList.Sum() / (double)xList.Count) / DrawingWidth;
779 6b9e7a56 gaqhf
780
                List<double> yList = equipment.POINT.Select(x => x[1]).ToList();
781 8f24b438 gaqhf
                row["YCOORDS"] = (yList.Sum() / (double)yList.Count) / DrawingHeight;
782 6b9e7a56 gaqhf
783
                equipDT.Rows.Add(row);
784
            }
785
786
            Equipment = equipDT;
787
            Nozzle = nozzleDT;
788
        }
789
        private void SavePSNData()
790
        {
791
            DataTable pathItemsDT = new DataTable();
792
            pathItemsDT.Columns.Add("OID", typeof(string));
793
            pathItemsDT.Columns.Add("SequenceData_OID", typeof(string));
794
            pathItemsDT.Columns.Add("TopologySet_OID", typeof(string));
795
            pathItemsDT.Columns.Add("BranchTopologySet_OID", typeof(string));
796
            pathItemsDT.Columns.Add("PipeLine_OID", typeof(string));
797
            pathItemsDT.Columns.Add("ITEMNAME", typeof(string));
798
            pathItemsDT.Columns.Add("ITEMTAG", typeof(string));
799 7881ec8f gaqhf
            pathItemsDT.Columns.Add("Class", typeof(string));
800
            pathItemsDT.Columns.Add("SubClass", typeof(string));
801 6b9e7a56 gaqhf
            pathItemsDT.Columns.Add("TYPE", typeof(string));
802
            pathItemsDT.Columns.Add("PIDNAME", typeof(string));
803
            pathItemsDT.Columns.Add("NPD", typeof(string));
804
            pathItemsDT.Columns.Add("PipeSystemNetwork_OID", typeof(string));
805 7881ec8f gaqhf
            pathItemsDT.Columns.Add("ViewPipeSystemNetwork_OID", typeof(string));
806 6b9e7a56 gaqhf
            pathItemsDT.Columns.Add("PipeRun_OID", typeof(string));
807
808
            DataTable sequenceDataDT = new DataTable();
809
            sequenceDataDT.Columns.Add("OID", typeof(string));
810
            sequenceDataDT.Columns.Add("SERIALNUMBER", typeof(string));
811
            sequenceDataDT.Columns.Add("PathItem_OID", typeof(string));
812
            sequenceDataDT.Columns.Add("TopologySet_OID_Key", typeof(string));
813
814
            DataTable pipeSystemNetworkDT = new DataTable();
815
            pipeSystemNetworkDT.Columns.Add("OID", typeof(string));
816
            pipeSystemNetworkDT.Columns.Add("Type", typeof(string));
817
            pipeSystemNetworkDT.Columns.Add("OrderNumber", typeof(string));
818
            pipeSystemNetworkDT.Columns.Add("Pipeline_OID", typeof(string));
819
            pipeSystemNetworkDT.Columns.Add("FROM_DATA", typeof(string));
820
            pipeSystemNetworkDT.Columns.Add("TO_DATA", typeof(string));
821
            pipeSystemNetworkDT.Columns.Add("TopologySet_OID_Key", typeof(string));
822
            pipeSystemNetworkDT.Columns.Add("PSNRevisionNumber", typeof(string));
823
            pipeSystemNetworkDT.Columns.Add("PBS", typeof(string));
824
            pipeSystemNetworkDT.Columns.Add("PIDDrawings", typeof(string));
825 36a45f13 gaqhf
            pipeSystemNetworkDT.Columns.Add("Validity", typeof(string));
826
            pipeSystemNetworkDT.Columns.Add("Status", typeof(string));
827
828
            DataTable topologySetDT = new DataTable();
829
            topologySetDT.Columns.Add("OID", typeof(string));
830
            topologySetDT.Columns.Add("Type", typeof(string));
831
            topologySetDT.Columns.Add("SubType", typeof(string));
832
            topologySetDT.Columns.Add("HeadItemTag", typeof(string));
833
            topologySetDT.Columns.Add("TailItemTag", typeof(string));
834
            topologySetDT.Columns.Add("HeadItemID", typeof(string));
835
            topologySetDT.Columns.Add("TailItemID", typeof(string));
836
837
            // Set Header Info
838
            List<VentDrainInfo> VentDrainInfos = new List<VentDrainInfo>();
839
            DataTable dt = DB.SelectVentDrainSetting();
840
            foreach (DataRow row in dt.Rows)
841
            {
842
                string groupID = row["GROUP_ID"].ToString();
843
                string desc = row["DESCRIPTION"].ToString();
844
                int index = Convert.ToInt32(row["INDEX"]);
845
                string name = row["NAME"].ToString();
846
847
                VentDrainInfo ventDrainInfo = VentDrainInfos.Find(x => x.UID.Equals(groupID));
848
                if (ventDrainInfo == null)
849
                {
850
                    ventDrainInfo = new VentDrainInfo(groupID);
851
                    ventDrainInfo.Description = desc;
852
                    VentDrainInfos.Add(ventDrainInfo);
853
                }
854
855
                ventDrainInfo.VentDrainItems.Add(new VentDrainItem()
856
                {
857
                    Index = index,
858
                    Name = name
859
                });
860
            }
861 6b9e7a56 gaqhf
862 5c248ee3 gaqhf
            // key = 미입력 branch
863
            Dictionary<Item, Item> startBranchDic = new Dictionary<Item, Item>();
864
            Dictionary<Item, Item> endBranchDic = new Dictionary<Item, Item>();
865 6b9e7a56 gaqhf
            foreach (PSNItem PSNItem in PSNItems)
866
            {
867 94a117ca gaqhf
                int psnOrder = 0;
868 6b9e7a56 gaqhf
                int index = 0;
869 94a117ca gaqhf
                bool bPSNStart = true;
870
                string sPSNData = string.Empty;
871 36a45f13 gaqhf
                bool bVentDrain = false;
872
873
                //VentDrain 검사
874
                if (PSNItem.Groups.Count.Equals(1))
875
                {
876
                    List<VentDrainInfo> endInfos = new List<VentDrainInfo>();
877
                    Group group = PSNItem.Groups[0];
878
                    for (int i = 0; i < group.Items.Count; i++)
879
                    {
880
                        Item item = group.Items[i];
881
                        foreach (VentDrainInfo ventDrainInfo in VentDrainInfos)
882
                        {
883 7881ec8f gaqhf
                            if (endInfos.Contains(ventDrainInfo) || ventDrainInfo.VentDrainItems.Count != group.Items.Count)
884 36a45f13 gaqhf
                                continue;
885
                            if (!ventDrainInfo.VentDrainItems[i].Name.Equals(item.Name))
886
                            {
887
                                endInfos.Add(ventDrainInfo);
888
                                continue;
889
                            }
890
891
                            if (i + 1 == group.Items.Count && group.Items.Count.Equals(ventDrainInfo.VentDrainItems.Count))
892
                                bVentDrain = true;
893
                        }
894
                    }
895
896
                    if (!bVentDrain)
897
                    {
898
                        endInfos = new List<VentDrainInfo>();
899
                        for (int i = 0; i < group.Items.Count; i++)
900
                        {
901
                            Item item = group.Items[group.Items.Count - i - 1];
902
                            foreach (VentDrainInfo ventDrainInfo in VentDrainInfos)
903
                            {
904 7881ec8f gaqhf
                                if (endInfos.Contains(ventDrainInfo) || ventDrainInfo.VentDrainItems.Count != group.Items.Count)
905 36a45f13 gaqhf
                                    continue;
906
                                if (!ventDrainInfo.VentDrainItems[i].Name.Equals(item.Name))
907
                                {
908
                                    endInfos.Add(ventDrainInfo);
909
                                    continue;
910
                                }
911
912
                                if (i + 1 == group.Items.Count && group.Items.Count.Equals(ventDrainInfo.VentDrainItems.Count))
913
                                    bVentDrain = true;
914
                            }
915
                        }
916
                    }
917
                }
918
919
                //PSN, PathItems, SequenceData 관련
920 6b9e7a56 gaqhf
                foreach (Group group in PSNItem.Groups)
921
                {
922
                    foreach (Item item in group.Items)
923
                    {
924
                        if (item.BranchItems.Count == 0)
925
                        {
926 7881ec8f gaqhf
                            CreatePathItemsDataRow(item.UID, item.ID2DBType);
927 6b9e7a56 gaqhf
                            CreateSequenceDataDataRow(item.UID);
928
                            index++;
929
                        }
930
                        else
931
                        {
932 7881ec8f gaqhf
                            CreatePathItemsDataRow(item.UID + "_L1", item.ID2DBType);
933 5c248ee3 gaqhf
                            CreateSequenceDataDataRow(item.UID + "_L1");
934 6b9e7a56 gaqhf
                            index++;
935
                            for (int i = 0; i < item.BranchItems.Count; i++)
936
                            {
937 7881ec8f gaqhf
                                CreatePathItemsDataRow(string.Format(item.UID + "_B{0}", i + 1), "Branch", item.BranchItems[i].Topology.FullName, item.BranchItems[i]);
938 5c248ee3 gaqhf
                                CreateSequenceDataDataRow(string.Format(item.UID + "_B{0}", i + 1));
939 6b9e7a56 gaqhf
                                index++;
940
941 7881ec8f gaqhf
                                CreatePathItemsDataRow(string.Format(item.UID + "_L{0}", i + 2), item.ID2DBType);
942 5c248ee3 gaqhf
                                CreateSequenceDataDataRow(string.Format(item.UID + "_L{0}", i + 2));
943 6b9e7a56 gaqhf
                                index++;
944 5c248ee3 gaqhf
945
                                if (item.BranchItems[i].Relations[0].Item != null && item.BranchItems[i].Relations[0].Item == item)
946
                                    startBranchDic.Add(item.BranchItems[i], item);
947
                                else if (item.BranchItems[i].Relations[1].Item != null && item.BranchItems[i].Relations[1].Item == item)
948
                                    endBranchDic.Add(item.BranchItems[i], item);
949 6b9e7a56 gaqhf
                            }
950
                        }
951
952 94a117ca gaqhf
                        if (bPSNStart)
953
                        {
954
                            CreatePipeSystemNetworkDataRow();
955
                            sPSNData = item.TopologyData;
956
                            psnOrder++;
957
                            bPSNStart = false;
958
                        }
959
                        else
960
                        {
961
                            if (item.TopologyData != sPSNData)
962
                            {
963
                                CreatePipeSystemNetworkDataRow();
964
                                sPSNData = item.TopologyData;
965
                                psnOrder++;
966
                            }
967
                        }
968 7881ec8f gaqhf
                        void CreatePathItemsDataRow(string itemOID, string itemType, string branchTopologyName = "", Item branchItem = null)
969 6b9e7a56 gaqhf
                        {
970
                            DataRow newRow = pathItemsDT.NewRow();
971
                            newRow["OID"] = itemOID;
972
973 8f24b438 gaqhf
                            newRow["SequenceData_OID"] = string.Format(item.Topology.FullName + "_{0}", index);
974 6b9e7a56 gaqhf
975
                            newRow["TopologySet_OID"] = item.Topology.FullName;
976
977
                            newRow["BranchTopologySet_OID"] = branchTopologyName;
978 8f24b438 gaqhf
                            newRow["PipeLine_OID"] = item.PSNPipeLineID;
979 7881ec8f gaqhf
                            newRow["ITEMNAME"] = GetItemName(item, itemOID);
980 36a45f13 gaqhf
                            newRow["ITEMTAG"] = GetItemTag(item);
981 7881ec8f gaqhf
                            newRow["Class"] = GetClass(item, itemOID);
982
                            newRow["SubClass"] = GetSubClass(item, itemOID);
983
984 36a45f13 gaqhf
                            if (item.ItemType == ItemType.Symbol)
985 7881ec8f gaqhf
                                newRow["TYPE"] = item.ID2DBName;
986 36a45f13 gaqhf
                            else if (item.ItemType == ItemType.Line)
987 7881ec8f gaqhf
                                newRow["TYPE"] = item.ID2DBType;
988 6b9e7a56 gaqhf
                            newRow["PIDNAME"] = group.Document.DrawingName;
989
990
                            // NPD
991
                            if (item.LineNumber != null)
992
                            {
993
                                Attribute attribute = item.LineNumber.Attributes.Find(x => x.Name == "NominalDiameter");
994
                                if (attribute != null)
995
                                    newRow["NPD"] = attribute.Value;
996
                            }
997
                            else
998
                                newRow["NPD"] = null;
999
1000 94a117ca gaqhf
                            newRow["PipeSystemNetwork_OID"] = PSNItem.PSN_OID();
1001 7881ec8f gaqhf
                            if (branchItem == null)
1002
                                newRow["ViewPipeSystemNetwork_OID"] = PSNItem.PSN_OID();
1003
                            else
1004
                                newRow["ViewPipeSystemNetwork_OID"] = branchItem.PSNItem.PSN_OID();
1005
                            
1006 6b9e7a56 gaqhf
                            newRow["PipeRun_OID"] = item.LineNumber != null ? item.LineNumber.Name : string.Empty;
1007
1008
                            pathItemsDT.Rows.Add(newRow);
1009
                        }
1010
                        void CreateSequenceDataDataRow(string itemOID)
1011
                        {
1012
                            DataRow newRow = sequenceDataDT.NewRow();
1013 94a117ca gaqhf
                            newRow["OID"] = string.Format(item.Topology.FullName + "_{0}", index);
1014 6b9e7a56 gaqhf
                            newRow["SERIALNUMBER"] = string.Format("{0}", index);
1015
                            newRow["PathItem_OID"] = itemOID;
1016
                            newRow["TopologySet_OID_Key"] = item.Topology.FullName;
1017
1018
                            sequenceDataDT.Rows.Add(newRow);
1019
                        }
1020 94a117ca gaqhf
                        void CreatePipeSystemNetworkDataRow()
1021
                        {
1022 36a45f13 gaqhf
                            // VentDrain의 경우 제외 요청
1023
                            if (bVentDrain)
1024
                                return;
1025
1026 94a117ca gaqhf
                            DataRow newRow = pipeSystemNetworkDT.NewRow();
1027
                            newRow["OID"] = PSNItem.PSN_OID();
1028
                            newRow["Type"] = PSNItem.GetPSNType();
1029
                            newRow["OrderNumber"] = psnOrder;
1030 8f24b438 gaqhf
                            newRow["Pipeline_OID"] = item.PSNPipeLineID;
1031 94a117ca gaqhf
                            newRow["FROM_DATA"] = PSNItem.GetFromData();
1032
                            newRow["TO_DATA"] = PSNItem.GetToData();
1033
                            newRow["TopologySet_OID_Key"] = item.Topology.FullName;
1034 36a45f13 gaqhf
                            newRow["PSNRevisionNumber"] = string.Format("V{0:D4}", Revision);
1035 7881ec8f gaqhf
                            newRow["PBS"] = PSNItem.GetPBSData();
1036 36a45f13 gaqhf
                            newRow["Validity"] = PSNItem.Validity;
1037
                            newRow["Status"] = !string.IsNullOrEmpty(PSNItem.Status) ? PSNItem.Status.Remove(0, 2) : string.Empty;
1038 6b9e7a56 gaqhf
1039 7881ec8f gaqhf
1040 94a117ca gaqhf
                            List<string> drawingNames = new List<string>();
1041
                            foreach (Group _group in PSNItem.Groups)
1042
                            {
1043 f25b787a gaqhf
                                if (!drawingNames.Contains(_group.Document.DrawingName))
1044
                                {
1045 94a117ca gaqhf
                                    if (drawingNames.Count == 0)
1046
                                        newRow["PIDDrawings"] = _group.Document.DrawingName;
1047
                                    else
1048
                                        newRow["PIDDrawings"] = newRow["PIDDrawings"] + ", " + _group.Document.DrawingName;
1049
                                    drawingNames.Add(_group.Document.DrawingName);
1050
                                }
1051
                            }
1052
                            pipeSystemNetworkDT.Rows.Add(newRow);
1053
                        }
1054 6b9e7a56 gaqhf
                    }
1055
                }
1056 36a45f13 gaqhf
                //TopologySet 관련
1057
                foreach (Topology topology in PSNItem.Topologies)
1058
                {
1059
                    DataRow newRow = topologySetDT.NewRow();
1060
                    newRow["OID"] = topology.FullName;
1061
                    newRow["Type"] = topology.FullName.Split(new char[] { '-' }).Last().StartsWith("M") ? "Main" : "Branch";
1062
                    if (bVentDrain)
1063 7881ec8f gaqhf
                        newRow["SubType"] = "Vent_Drain";
1064 36a45f13 gaqhf
                    else
1065
                        newRow["SubType"] = null;
1066
                    newRow["HeadItemTag"] = GetItemTag(topology.Items.Last());
1067
                    newRow["TailItemTag"] = GetItemTag(topology.Items.First());
1068
                    newRow["HeadItemID"] = topology.Items.Last().UID;
1069
                    newRow["TailItemID"] = topology.Items.First().UID;
1070
                    topologySetDT.Rows.Add(newRow);
1071
                }
1072 6b9e7a56 gaqhf
            }
1073
1074 5c248ee3 gaqhf
            foreach (var item in startBranchDic)
1075
            {
1076
                string uid = item.Key.UID;
1077
                string topologyName = item.Value.Topology.FullName;
1078
                DataRow[] rows = pathItemsDT.Select(string.Format("OID LIKE '{0}%'", uid));
1079
                if (rows.Length == 1)
1080
                    rows.First()["BranchTopologySet_OID"] = topologyName;
1081
                else if (rows.Length > 1)
1082
                {
1083
                    DataRow targetRow = null;
1084
                    int index = int.MaxValue;
1085
                    foreach (DataRow row in rows)
1086
                    {
1087
                        string split = row["OID"].ToString().Split(new char[] { '_' })[1];
1088
                        if (split.StartsWith("L"))
1089
                        {
1090
                            int num = Convert.ToInt32(split.Remove(0, 1));
1091
                            if (index > num)
1092
                            {
1093
                                index = num;
1094
                                targetRow = row;
1095
                            }
1096
                        }
1097
                    }
1098
1099
                    if (targetRow != null)
1100
                        targetRow["BranchTopologySet_OID"] = topologyName;
1101
                }
1102
            }
1103
            foreach (var item in endBranchDic)
1104
            {
1105
                string uid = item.Key.UID;
1106
                string topologyName = item.Value.Topology.FullName;
1107
                DataRow[] rows = pathItemsDT.Select(string.Format("OID LIKE '{0}%'", uid));
1108
                if (rows.Length == 1)
1109
                    rows.First()["BranchTopologySet_OID"] = topologyName;
1110
                else if (rows.Length > 1)
1111
                {
1112
                    DataRow targetRow = null;
1113
                    int index = int.MinValue;
1114
                    foreach (DataRow row in rows)
1115
                    {
1116
                        string split = row["OID"].ToString().Split(new char[] { '_' })[1];
1117
                        if (split.StartsWith("L"))
1118
                        {
1119
                            int num = Convert.ToInt32(split.Remove(0, 1));
1120
                            if (index < num)
1121
                            {
1122
                                index = num;
1123
                                targetRow = row;
1124
                            }
1125
                        }
1126
                    }
1127
1128
                    if (targetRow != null)
1129
                        targetRow["BranchTopologySet_OID"] = topologyName;
1130
                }
1131
            }
1132
1133 6b9e7a56 gaqhf
            PathItems = pathItemsDT;
1134
            SequenceData = sequenceDataDT;
1135
            PipeSystemNetwork = pipeSystemNetworkDT;
1136 36a45f13 gaqhf
            TopologySet = topologySetDT;
1137
        }
1138 7881ec8f gaqhf
        private void UpdateSubType()
1139 36a45f13 gaqhf
        {
1140
            foreach (PSNItem PSNItem in PSNItems)
1141
            {
1142 7881ec8f gaqhf
                if (PSNItem.IsBypass)
1143 36a45f13 gaqhf
                {
1144 7881ec8f gaqhf
                    foreach (Topology topology in PSNItem.Topologies)
1145 36a45f13 gaqhf
                    {
1146 7881ec8f gaqhf
                        DataRow[] rows = TopologySet.Select(string.Format("OID = '{0}'", topology.FullName));
1147
                        if (rows.Length.Equals(1))
1148
                            rows.First()["SubType"] = "Bypass";
1149 36a45f13 gaqhf
                    }
1150
                }
1151 7881ec8f gaqhf
1152
                if (PSNItem.StartType == PSNType.Header)
1153
                {
1154
                    Topology topology = PSNItem.Topologies.First();
1155
                    DataRow[] rows = TopologySet.Select(string.Format("OID = '{0}'", topology.FullName));
1156
                    if (rows.Length.Equals(1))
1157
                        rows.First()["SubType"] = "Header";
1158
                }
1159
                else if (PSNItem.EndType == PSNType.Header)
1160
                {
1161
                    Topology topology = PSNItem.Topologies.Last();
1162
                    DataRow[] rows = TopologySet.Select(string.Format("OID = '{0}'", topology.FullName));
1163
                    if (rows.Length.Equals(1))
1164
                        rows.First()["SubType"] = "Header";
1165
                }
1166 36a45f13 gaqhf
            }
1167
1168 7881ec8f gaqhf
            foreach (Topology topology in Topologies)
1169
            {
1170
                DataRow[] rows = TopologySet.Select(string.Format("OID = '{0}'", topology.FullName));
1171
                if (rows.Length.Equals(1) && rows.First()["SubType"] == null || string.IsNullOrEmpty(rows.First()["SubType"].ToString()))
1172
                {
1173
                    Item firstItem = topology.Items.First();
1174
                    Item lastItem = topology.Items.Last();
1175
1176
                    List<Relation> relations = new List<Relation>();
1177
                    relations.AddRange(firstItem.Relations.FindAll(x => x.Item != null && x.Item.Topology.ID != topology.ID));
1178
                    relations.AddRange(lastItem.Relations.FindAll(x => x.Item != null && x.Item.Topology.ID != topology.ID));
1179
1180
                    if (relations.Count > 0)
1181
                        rows.First()["SubType"] = "OtherSystem";
1182
                }
1183
            }
1184
1185
            foreach (DataRow row in TopologySet.Rows)
1186
                if (row["SubType"] == null || string.IsNullOrEmpty(row["SubType"].ToString()))
1187
                    row["SubType"] = "Normal";
1188
        }
1189
        private bool IsBypass(PSNItem PSNItem)
1190
        {
1191
            bool bResult = false;
1192
1193
            if (PSNItem.GetPSNType() == "B2B")
1194
            {
1195
                Group firstGroup = PSNItem.Groups.First();
1196
                Group lastGroup = PSNItem.Groups.Last();
1197
                Item firstItem = firstGroup.Items.First();
1198
                Item lastItem = lastGroup.Items.Last();
1199
1200
                Item connectedFirstItem = GetConnectedItemByPSN(firstItem);
1201
                Item connectedLastItem = GetConnectedItemByPSN(lastItem);
1202 36a45f13 gaqhf
1203 7881ec8f gaqhf
                if (connectedFirstItem.LineNumber != null && connectedLastItem.LineNumber != null &&
1204
                    !string.IsNullOrEmpty(connectedFirstItem.LineNumber.Name) && !string.IsNullOrEmpty(connectedLastItem.LineNumber.Name) &&
1205
                    connectedFirstItem.LineNumber.Name == connectedLastItem.LineNumber.Name)
1206
                    bResult = true;
1207
                else if (connectedFirstItem.PSNItem == connectedLastItem.PSNItem)
1208
                    bResult = true;
1209
            }
1210 36a45f13 gaqhf
1211
            Item GetConnectedItemByPSN(Item item)
1212
            {
1213
                Item result = null;
1214
1215
                Relation relation = item.Relations.Find(x => x.Item != null && x.Item.PSNItem != item.PSNItem);
1216
                if (relation != null)
1217
                    result = relation.Item;
1218
1219
                return result;
1220
            }
1221 7881ec8f gaqhf
1222
            return bResult;
1223
        }
1224
        private void UpdateErrorForPSN()
1225
        {
1226
            DataRow[] errorRows = PipeSystemNetwork.Select(" Type = 'Error'");
1227
            foreach (DataRow dataRow in errorRows)
1228
            {
1229
                PSNItem PSNItem = PSNItems.Find(x=>x.PSN_OID() == dataRow["OID"].ToString());
1230
1231
                if (PSNItem.StartType == PSNType.OPC)
1232
                    PSNItem.StartType = PSNType.Equipment;
1233
1234
                if (PSNItem.EndType == PSNType.OPC)
1235
                    PSNItem.EndType = PSNType.Equipment;
1236
1237
                dataRow["Type"] = PSNItem.GetPSNType();
1238
            }
1239
1240
            
1241 6b9e7a56 gaqhf
        }
1242
    }
1243
1244
    public class PSNItem
1245
    {
1246 8f24b438 gaqhf
        public PSNItem(int count, int Revision)
1247 6b9e7a56 gaqhf
        {
1248
            Groups = new List<Group>();
1249
            Topologies = new List<Topology>();
1250 94a117ca gaqhf
1251
            Index = count + 1;
1252 8f24b438 gaqhf
            this.Revision = Revision;
1253 6b9e7a56 gaqhf
        }
1254 8f24b438 gaqhf
        private int Revision;
1255 6b9e7a56 gaqhf
        public string UID { get; set; }
1256
        public List<Group> Groups { get; set; }
1257
        public List<Topology> Topologies { get; set; }
1258
        public PSNType StartType { get; set; }
1259
        public PSNType EndType { get; set; }
1260 94a117ca gaqhf
        public int Index { get; set; }
1261 36a45f13 gaqhf
        public string Validity { get; set; }
1262
        public string Status { get; set; }
1263 94a117ca gaqhf
        public string PSN_OID()
1264
        {
1265 36a45f13 gaqhf
            return string.Format("V{0}-PSN-{1}", string.Format("{0:D4}", Revision), string.Format("{0:D5}", Index));
1266 94a117ca gaqhf
        }
1267
        public string GetPSNType()
1268
        {
1269
            string result = string.Empty;
1270
1271
            if (EnableType(StartType) && EnableType(EndType))
1272
            {
1273
                if (StartType == PSNType.Equipment && EndType == PSNType.Equipment)
1274
                    result = "E2E";
1275
                else if (StartType == PSNType.Branch && EndType == PSNType.Branch)
1276
                    result = "B2B";
1277
                else if (StartType == PSNType.Header && EndType == PSNType.Header)
1278
                    result = "HD2";
1279
1280
                else if (StartType == PSNType.Equipment && EndType == PSNType.Branch)
1281
                    result = "E2B";
1282
                else if (StartType == PSNType.Branch && EndType == PSNType.Equipment)
1283
                    result = "B2E";
1284
1285
                else if (StartType == PSNType.Header && EndType == PSNType.Branch)
1286
                    result = "HDB";
1287
                else if (StartType == PSNType.Branch && EndType == PSNType.Header)
1288
                    result = "HDB";
1289
1290
                else if (StartType == PSNType.Header && EndType == PSNType.Equipment)
1291
                    result = "HDE";
1292
                else if (StartType == PSNType.Equipment && EndType == PSNType.Header)
1293
                    result = "HDE";
1294
                else
1295
                    result = "Error";
1296
            }
1297
            else
1298
                result = "Error";
1299
1300
            return result;
1301
1302
            
1303
        }
1304
        private bool EnableType(PSNType type)
1305
        {
1306
            bool result = false;
1307
1308
            if (type == PSNType.Branch ||
1309
                type == PSNType.Equipment ||
1310
                type == PSNType.Header)
1311
            {
1312
                result = true;
1313
            }
1314
1315
            return result;
1316
        }
1317 7881ec8f gaqhf
        public bool IsBypass { get; set; }
1318 94a117ca gaqhf
1319
        public string GetFromData()
1320
        {
1321
            string result = string.Empty;
1322
            if (StartType == PSNType.Header)
1323
                result = "ENDOFHEADER";
1324
            else if (StartType == PSNType.Branch)
1325 36a45f13 gaqhf
            {
1326
                Item item = Groups.First().Items.First();
1327
                if (!string.IsNullOrEmpty(item.Relations.First().Item.LineNumber.Name))
1328
                    result = item.Relations.First().Item.LineNumber.Name;
1329
                else
1330
                {
1331
                    Status += ", Missing LineNumber";
1332
                    result = "Empty LineNumber";
1333
                }
1334
            }
1335 94a117ca gaqhf
            else if (StartType == PSNType.Equipment)
1336 c6503eaa gaqhf
                result = Groups.First().Items.First().Equipment.ItemTag;
1337 94a117ca gaqhf
            else
1338 36a45f13 gaqhf
            {
1339
                Validity = "Error";
1340
                Item item = Groups.First().Items.First();
1341
                if (item.ItemType == ItemType.Symbol)
1342
                {
1343 7881ec8f gaqhf
                    if (item.ID2DBType.Contains("OPC's"))
1344 36a45f13 gaqhf
                        Status += ", OPC Disconneted";
1345
                    else
1346
                        Status += ", Missing ItemTag or Description";
1347
1348 7881ec8f gaqhf
                    result = item.ID2DBName;
1349 36a45f13 gaqhf
                }
1350
                else if (item.ItemType == ItemType.Line)
1351
                {
1352
                    Status += ", Line Disconnected";
1353
                    result = !string.IsNullOrEmpty(item.LineNumber.Name) ? item.LineNumber.Name : "Empty LineNumber";
1354
                }
1355
                else
1356
                    result = "Unknown";
1357
            }
1358 94a117ca gaqhf
1359
            return result;
1360
        }
1361
1362
        public string GetToData()
1363
        {
1364
            string result = string.Empty;
1365
            if (EndType == PSNType.Header)
1366
                result = "ENDOFHEADER";
1367
            else if (EndType == PSNType.Branch)
1368 36a45f13 gaqhf
            {
1369
                Item item = Groups.Last().Items.Last();
1370
                if (!string.IsNullOrEmpty(item.Relations.Last().Item.LineNumber.Name))
1371
                    result = item.Relations.Last().Item.LineNumber.Name;
1372
                else
1373
                {
1374
                    Status += ", Missing LineNumber";
1375
                    result = "Empty LineNumber";
1376
                }
1377
            }
1378 94a117ca gaqhf
            else if (EndType == PSNType.Equipment)
1379 c6503eaa gaqhf
                result = Groups.Last().Items.Last().Equipment.ItemTag;
1380 36a45f13 gaqhf
            else
1381
            {
1382
                Validity = "Error";
1383
                Item item = Groups.Last().Items.Last();
1384
                if (item.ItemType == ItemType.Symbol)
1385
                {
1386 7881ec8f gaqhf
                    if (item.ID2DBType.Contains("OPC's"))
1387 36a45f13 gaqhf
                        Status += ", OPC Disconneted";
1388
                    else
1389
                        Status += ", Missing ItemTag or Description";
1390
                    
1391 7881ec8f gaqhf
                    result = item.ID2DBName;
1392 36a45f13 gaqhf
                }
1393
                else if (item.ItemType == ItemType.Line)
1394
                {
1395
                    Status += ", Line Disconnected";
1396
                    result = !string.IsNullOrEmpty(item.LineNumber.Name) ? item.LineNumber.Name : "Empty LineNumber";
1397
                }
1398
                else
1399
                    result = "Unknown";
1400
            }
1401 94a117ca gaqhf
            return result;
1402
        }
1403 7881ec8f gaqhf
1404
        public string GetPBSData()
1405
        {
1406
            string result = string.Empty;
1407
            List<string> PBSList = new List<string>();
1408
            if (Settings.Default.PBSSetting.Equals("Line Number"))
1409
            {
1410
                string attrValue = Settings.Default.PBSSettingValue;
1411
1412
                foreach (Group group in Groups)
1413
                {
1414
                    List<LineNumber> lineNumbers = group.Items.Select(x =>x.LineNumber).Distinct().ToList();
1415
                    foreach (LineNumber lineNumber in lineNumbers)
1416
                    {
1417
                        Attribute attribute = lineNumber.Attributes.Find(x => x.Name == attrValue && !string.IsNullOrEmpty(x.Value));
1418
                        if (attribute != null)
1419
                        {
1420
                            string value = attribute.Value;
1421
                            if (!PBSList.Contains(value))
1422
                                PBSList.Add(value);
1423
                        }
1424
                    }
1425
                }
1426
            }
1427
            else if (Settings.Default.PBSSetting.Equals("Item Attribute"))
1428
            {
1429
                string attrValue = Settings.Default.PBSSettingValue;
1430
1431
                foreach (Group group in Groups)
1432
                {
1433
                    List<Item> items = group.Items.FindAll(x => x.Attributes.Find(y => y.Name == attrValue && !string.IsNullOrEmpty(y.Value)) != null);
1434
                    foreach (Item item in items)
1435
                    {
1436
                        string value = item.Attributes.Find(x => x.Name == attrValue).Value;
1437
                        if (!PBSList.Contains(value))
1438
                            PBSList.Add(value);
1439
                    }
1440
                }
1441
            }
1442
            else if (Settings.Default.PBSSetting.Equals("Drawing No"))
1443
            {
1444
                string attrValue = Settings.Default.PBSSettingValue;
1445
1446
                foreach (Group group in Groups)
1447
                {
1448
                    List<Document> documents = group.Items.Select(x => x.Document).Distinct().ToList();
1449
                    foreach (Document document in documents)
1450
                    {
1451
                        string name = document.DrawingName;
1452
1453
                        int startIndex = Settings.Default.PBSSettingStartValue;
1454
                        int endIndex = Settings.Default.PBSSettingEndValue;
1455
1456
                        string subStr = name.Substring(startIndex - 1, endIndex - startIndex + 1);
1457
                        if (!PBSList.Contains(subStr))
1458
                            PBSList.Add(subStr);
1459
                    }
1460
                }
1461
            }
1462
            else if (Settings.Default.PBSSetting.Equals("Unit Area"))
1463
            {
1464
                foreach (Group group in Groups)
1465
                {
1466
                    List<Document> documents = group.Items.Select(x => x.Document).Distinct().ToList();
1467
                    foreach (Document document in documents)
1468
                    {
1469
                        List<TextInfo> textInfos = document.TextInfos.FindAll(x => x.Area == "Unit");
1470
                        foreach (TextInfo textInfo in textInfos)
1471
                        {
1472
                            if (!PBSList.Contains(textInfo.Value))
1473
                                PBSList.Add(textInfo.Value);
1474
                        }
1475
                    }
1476
                }
1477
            }
1478
1479
            foreach (var item in PBSList)
1480
            {
1481
                if (string.IsNullOrEmpty(result))
1482
                    result = item;
1483
                else
1484
                    result += ", " + item;
1485
            }
1486
            return result;
1487
        }
1488 6b9e7a56 gaqhf
    }
1489
}
클립보드 이미지 추가 (최대 크기: 500 MB)