프로젝트

일반

사용자정보

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

hytos / DTI_PID / ID2PSN / PSN.cs @ c6503eaa

이력 | 보기 | 이력해설 | 다운로드 (42.5 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

    
9
namespace ID2PSN
10
{
11
    public enum PSNType
12
    {
13
        None,
14
        Branch,
15
        Equipment,
16
        Header,
17
        Symbol,
18
        OPC,
19
    }
20

    
21
    public class PSN
22
    {
23
        private double[] DrawingSize = null;
24
        private double DrawingWidth = double.NaN;
25
        private double DrawingHeight = double.NaN;
26
        public int Revision;
27
        public string EquipTagNoAttributeName = string.Empty;
28
        public DataTable PathItems { get; set; }
29
        public DataTable SequenceData { get; set; }
30
        public DataTable PipeSystemNetwork { get; set; }
31
        public DataTable Equipment { get; set; }
32
        public DataTable Nozzle { get; set; }
33

    
34
        List<Document> Documents;
35
        List<Group> groups = new List<Group>();
36
        List<PSNItem> PSNItems = new List<PSNItem>();
37
        List<Topology> Topologies = new List<Topology>();
38

    
39
        DataTable opcDT = null;
40
        DataTable topologyRuleDT = null;
41

    
42
        const string FluidPriorityType = "FLUIDCODE";
43
        const string PipingMaterialsPriorityType = "PIPINGMATERIALSCLASS";
44

    
45
        public PSN()
46
        {
47
            
48
        }
49

    
50
        public PSN(List<Document> documents, int Revision)
51
        {
52
            Documents = documents;
53
            foreach (Document document in Documents)
54
                groups.AddRange(document.Groups);
55
            opcDT = GetOPCInfo();
56
            topologyRuleDT = GetTopologyRule();
57
            this.Revision = Revision;
58
            DrawingSize = DB.GetDrawingSize();
59
            DrawingWidth = DrawingSize[2] - DrawingSize[0];
60
            DrawingHeight = DrawingSize[3] - DrawingSize[1];
61
        }
62

    
63
        public void SetPSNData()
64
        {
65
            SetTopologyData();
66
            ConnectByOPC();
67
            SetPSNItem();
68
            SetPSNType();
69
            SetBranchInfo();
70
            SetTopology();
71

    
72
            SaveNozzleAndEquipment();
73
            SavePSNData();
74
        }
75
        private void SetTopologyData()
76
        {
77
            foreach (Document document in Documents)
78
            {
79
                foreach (Item item in document.Items)
80
                {
81
                    item.TopologyData = string.Empty;
82
                    item.PSNPipeLineID = string.Empty;
83
                    LineNumber lineNumber = document.LineNumbers.Find(x => x.UID == item.Owner);
84
                    if (lineNumber != null)
85
                    {
86
                        item.LineNumber = lineNumber;
87

    
88
                        foreach (DataRow row in topologyRuleDT.Rows)
89
                        {
90
                            string uid = row["UID"].ToString();
91
                            if (uid == "-")
92
                                item.TopologyData += "-"; 
93
                            else
94
                            {
95
                                Attribute itemAttr = item.Attributes.Find(x => x.Name == uid);
96

    
97
                                Attribute attribute = lineNumber.Attributes.Find(x => x.Name == uid);
98
                                if (attribute != null)
99
                                    item.TopologyData += attribute.Value;
100
                            }
101
                        }
102

    
103
                        Attribute insulAttr = item.LineNumber.Attributes.Find(x => x.Name == "InsulationPurpose");
104
                        if (insulAttr != null && !string.IsNullOrEmpty(insulAttr.Value))
105
                            item.PSNPipeLineID = item.TopologyData + "-" + insulAttr.Value;
106
                        else
107
                            item.PSNPipeLineID = item.TopologyData;
108
                    }
109
                    else
110
                    {
111
                        item.TopologyData = "Empty LineNumber";
112
                        item.LineNumber = new LineNumber();
113
                    }
114
                }
115
            }
116

    
117
            int emptyIndex = 1;
118
            foreach (Group group in groups)
119
            {
120
                List<Item> groupItems = group.Items.FindAll(x => x.TopologyData == "Empty LineNumber");
121
                if (groupItems.Count > 0)
122
                {
123
                    foreach (var item in groupItems)
124
                        item.TopologyData += string.Format("-{0}", emptyIndex);
125
                    emptyIndex++;
126
                }
127
            }
128

    
129
        }
130
        private void ConnectByOPC()
131
        {
132
            foreach (Group group in groups.FindAll(x => x.Items.Last().SubItemType == SubItemType.OPC))
133
            {
134
                Item opcItem = group.Items.Last();
135
                DataRow[] fromRows = opcDT.Select(string.Format("FromOPCUID = '{0}'", opcItem.UID));
136
                if (fromRows.Length.Equals(1))
137
                {
138
                    DataRow opcRow = fromRows.First();
139
                    string toDrawing = opcRow["ToDrawing"].ToString();
140
                    string toOPCUID = opcRow["ToOPCUID"].ToString();
141

    
142
                    Document toDocument = Documents.Find(x => x.DrawingName == toDrawing);
143
                    Group toGroup = toDocument.Groups.Find(x => x.Items.Find(y => y.UID == toOPCUID) != null);
144
                    group.EndGroup = toGroup;
145
                    toGroup.StartGroup = group;
146
                }
147
            }
148
        }
149
        private void SetPSNItem()
150
        {
151
            Dictionary<Group, string> groupDic = new Dictionary<Group, string>(); 
152
            foreach (Group group in groups)
153
                groupDic.Add(group, Guid.NewGuid().ToString());
154

    
155
            foreach (Group group in groups)
156
            {
157
                string groupKey = groupDic[group];
158
                if (group.StartGroup != null)
159
                {
160
                    string otherKey = groupDic[group.StartGroup];
161
                    ChangeGroupID(otherKey, groupKey);
162
                }
163
            }
164

    
165
            // PSN 정리
166
            foreach (var item in groupDic)
167
            {
168
                Group group = item.Key;
169
                string uid = item.Value;
170
                PSNItem PSNItem = PSNItems.Find(x => x.UID == uid);
171
                if (PSNItem == null)
172
                {
173
                    PSNItem = new PSNItem(PSNItems.Count, Revision) { UID = uid };
174
                    PSNItems.Add(PSNItem);
175
                }
176
                PSNItem.Groups.Add(group);
177
            }
178

    
179
            // Sort PSN
180
            foreach (PSNItem PSNItem in PSNItems)
181
            {
182
                List<Group> _groups = new List<Group>();
183

    
184
                Stack<Group> stacks = new Stack<Group>();
185
                stacks.Push(PSNItem.Groups.First());
186
                while (stacks.Count > 0)
187
                {
188
                    Group stack = stacks.Pop();
189
                    if (_groups.Contains(stack))
190
                        continue;
191

    
192
                    if (_groups.Count == 0)
193
                        _groups.Add(stack);
194
                    else
195
                    {
196
                        if (stack.StartGroup != null && _groups.Contains(stack.StartGroup))
197
                        {
198
                            int index = _groups.IndexOf(stack.StartGroup);
199
                            _groups.Insert(index + 1, stack);
200
                        }
201
                        else if (stack.EndGroup != null && _groups.Contains(stack.EndGroup))
202
                        {
203
                            int index = _groups.IndexOf(stack.EndGroup);
204
                            _groups.Insert(index, stack);
205
                        }
206
                    }
207

    
208
                    if (stack.StartGroup != null)
209
                        stacks.Push(stack.StartGroup);
210
                    if (stack.EndGroup != null)
211
                        stacks.Push(stack.EndGroup);
212
                }
213

    
214
                PSNItem.Groups.Clear();
215
                PSNItem.Groups.AddRange(_groups);
216
            }
217

    
218

    
219
            void ChangeGroupID(string from, string to)
220
            {
221
                if (from.Equals(to))
222
                    return;
223

    
224
                List<Group> changeItems = new List<Group>();
225
                foreach (var _item in groupDic)
226
                    if (_item.Value.Equals(from))
227
                        changeItems.Add(_item.Key);
228
                foreach (var _item in changeItems)
229
                    groupDic[_item] = to;
230
            }
231
        }
232
        private void SetPSNType()
233
        {
234
            foreach (PSNItem PSNItem in PSNItems)
235
            {
236
                Group firstGroup = PSNItem.Groups.First();
237
                Group lastGroup = PSNItem.Groups.Last();
238

    
239
                Item firstItem = firstGroup.Items.First();
240
                Item lastItem = lastGroup.Items.Last();
241

    
242
                PSNItem.StartType = GetPSNType(firstItem, true);
243
                PSNItem.EndType = GetPSNType(lastItem, false);
244
            }
245

    
246
            PSNType GetPSNType(Item item, bool bFirst = true)
247
            {
248
                PSNType type = PSNType.None;
249

    
250
                if (item.ItemType == ItemType.Line)
251
                {
252
                    Group group = groups.Find(x => x.Items.Contains(item));
253
                    if (bFirst && item.Relations[0].Item != null && !group.Items.Contains(item.Relations[0].Item))
254
                    {
255
                        Item connItem = item.Relations[0].Item;
256
                        if (connItem.ItemType == ItemType.Line && !IsConnected(item, connItem))
257
                            type = PSNType.Branch;
258
                        else if (connItem.ItemType == ItemType.Symbol)
259
                            type = PSNType.Symbol;
260
                    }
261
                    else if (!bFirst && item.Relations[1].Item != null && !group.Items.Contains(item.Relations[1].Item))
262
                    {
263
                        Item connItem = item.Relations[1].Item;
264
                        if (connItem.ItemType == ItemType.Line && !IsConnected(item, connItem))
265
                            type = PSNType.Branch;
266
                        else if (connItem.ItemType == ItemType.Symbol)
267
                            type = PSNType.Symbol;
268
                    }
269
                }
270
                else if (item.ItemType == ItemType.Symbol)
271
                {
272
                    if (item.SubItemType == SubItemType.Nozzle)
273
                        type = PSNType.Equipment;
274
                    else if (item.SubItemType == SubItemType.Header)
275
                        type = PSNType.Header;
276
                    else if (item.SubItemType == SubItemType.OPC)
277
                        type = PSNType.OPC;
278
                }
279

    
280
                return type;
281
            }
282
        }
283
        private void SetBranchInfo()
284
        {
285
            foreach (Document document in Documents)
286
            {
287
                List<Item> lines = document.Items.FindAll(x => x.ItemType == ItemType.Line).ToList();
288
                foreach (Item line in lines)
289
                {
290
                    double[] point = line.Relations[0].Point;
291
                    List<Item> connLines = lines.FindAll(x => x.Relations.Find(y => y.UID == line.UID) != null && line.Relations.Find(y => y.UID == x.UID) == null);
292
                    connLines.Sort(SortBranchLine);
293
                    line.BranchItems.AddRange(connLines);
294

    
295
                    int SortBranchLine(Item a, Item b)
296
                    {
297
                        double[] pointA = a.Relations[0].UID == line.UID ? a.Relations[0].Point : a.Relations[1].Point;
298
                        double distanceA = CalcPointToPointdDistance(point[0], point[1], pointA[0], pointA[1]);
299

    
300
                        double[] pointB = b.Relations[0].UID == line.UID ? b.Relations[0].Point : b.Relations[1].Point;
301
                        double distanceB = CalcPointToPointdDistance(point[0], point[1], pointB[0], pointB[1]);
302

    
303
                        // 내림차순
304
                        return distanceA.CompareTo(distanceB);
305
                    }
306
                    double CalcPointToPointdDistance(double x1, double y1, double x2, double y2)
307
                    {
308
                        return Math.Pow(Math.Pow(x1 - x2, 2) + Math.Pow(y1 - y2, 2), 0.5);
309
                    }
310
                }
311
            }
312
        }
313
        private void SetTopology()
314
        {
315
            #region 기본 topology 정리
316
            foreach (PSNItem PSNItem in PSNItems)
317
            {
318
                Topology topology = null;
319
                foreach (Group group in PSNItem.Groups)
320
                {
321
                    foreach (Item item in group.Items)
322
                    {
323
                        if (string.IsNullOrEmpty(item.TopologyData))
324
                            topology = null;
325
                        else
326
                        {
327
                            if (topology == null)
328
                            {
329
                                topology = new Topology()
330
                                {
331
                                    ID = item.TopologyData
332
                                };
333
                                Topologies.Add(topology);
334

    
335
                                if (!PSNItem.Topologies.Contains(topology))
336
                                    PSNItem.Topologies.Add(topology);
337
                            }
338
                            else
339
                            {
340
                                if (topology.ID != item.TopologyData)
341
                                {
342
                                    topology = new Topology()
343
                                    {
344
                                        ID = item.TopologyData
345
                                    };
346
                                    Topologies.Add(topology);
347

    
348
                                    if (!PSNItem.Topologies.Contains(topology))
349
                                        PSNItem.Topologies.Add(topology);
350
                                }
351
                            }
352

    
353
                            item.Topology = topology;
354
                            topology.Items.Add(item);
355
                        }
356
                    }
357
                }
358
            }
359
            #endregion
360

    
361
            #region Type
362

    
363
            //foreach (PSNItem PSNItem in PSNItems)
364
            //{
365
            //    List<Topology> psnTopology = PSNItem.Topologies;
366

    
367
            //    List<string> ids = psnTopology.Select(x => x.ID).Distinct().ToList();
368
            //    foreach (string id in ids)
369
            //    {
370
            //        List<Topology> topologies = psnTopology.FindAll(x => x.ID == id);
371

    
372
            //        // Main
373
            //        List<Topology> mainTopologies = FindMainTopology(topologies);
374
            //        foreach (Topology topology in mainTopologies)
375
            //        {
376
            //            topology.Type = "M";
377
            //            topology.Index = mainTopologies.IndexOf(topology).ToString();
378
            //        }
379

    
380
            //        // Branch
381
            //        List<Topology> branchToplogies = topologies.FindAll(x => string.IsNullOrEmpty(x.Type));
382
            //        foreach (Topology topology in branchToplogies)
383
            //        {
384
            //            topology.Type = "B";
385
            //            topology.Index = (branchToplogies.IndexOf(topology) + 1).ToString();
386
            //        }
387
            //    }
388
            //}
389

    
390
            List<string> ids = Topologies.Select(x => x.ID).Distinct().ToList();
391
            foreach (string id in ids)
392
            {
393
                List<Topology> topologies = Topologies.FindAll(x => x.ID == id);
394

    
395
                // Main
396
                List<Topology> mainTopologies = FindMainTopology(topologies);
397
                foreach (Topology topology in mainTopologies)
398
                {
399
                    topology.Type = "M";
400
                    topology.Index = mainTopologies.IndexOf(topology).ToString();
401
                }
402

    
403
                // Branch
404
                List<Topology> branchToplogies = topologies.FindAll(x => string.IsNullOrEmpty(x.Type));
405
                foreach (Topology topology in branchToplogies)
406
                {
407
                    topology.Type = "B";
408
                    topology.Index = (branchToplogies.IndexOf(topology) + 1).ToString();
409
                }
410
            }
411
            #endregion
412
        }
413
        private List<Topology> FindMainTopology(List<Topology> data)
414
        {
415
            DataTable nominalDiameterDT = DB.SelectNominalDiameter();
416
            DataTable PMCDT = DB.SelectPSNPIPINGMATLCLASS();
417
            DataTable fluidCodeDT = DB.SelectPSNFluidCode();
418

    
419
            List<Topology> main = new List<Topology>();
420
            main.AddRange(data);
421
            //
422
            main = GetNozzleTopology(data);
423
            if (main.Count == 1)
424
                return main;
425
            else
426
            {
427
                if (main.Count > 0)
428
                    main = GetPMCTopology(main);
429
                else
430
                    main = GetPMCTopology(data);
431

    
432
                if (main.Count == 1)
433
                    return main;
434
                else
435
                {
436
                    if (main.Count > 0)
437
                        main = GetDiaTopology(main);
438
                    else
439
                        main = GetDiaTopology(data);
440

    
441

    
442
                    if (main.Count == 1)
443
                        return main;
444
                    else
445
                    {
446
                        if (main.Count > 0)
447
                            main = GetItemTopology(main);
448
                        else
449
                            main = GetItemTopology(data);
450
                    }
451
                }
452
            }
453

    
454
            List<Topology> GetNozzleTopology(List<Topology> topologies)
455
            {
456
                return topologies.FindAll(x => x.Items.Find(y => y.SubItemType == SubItemType.Nozzle) != null);
457
            }
458

    
459
            List<Topology> GetPMCTopology(List<Topology> topologies)
460
            {
461
                List<Topology> result = new List<Topology>();
462
                foreach (DataRow row in PMCDT.Rows)
463
                {
464
                    string value = row["CODE"].ToString();
465
                    foreach (Topology topology in topologies)
466
                    {
467
                        foreach (Item item in topology.Items)
468
                        {
469
                            if (item.LineNumber == null)
470
                                continue;
471
                            Attribute attribute = item.LineNumber.Attributes.Find(x => x.Name == "PipingMaterialsClass");
472
                            if (attribute != null && value == attribute.Value)
473
                            {
474
                                result.Add(topology);
475
                                break;
476
                            }
477
                        }
478
                    }
479

    
480
                    if (result.Count > 0)
481
                        break;
482
                }
483

    
484
                return result;
485
            }
486

    
487
            List<Topology> GetDiaTopology(List<Topology> topologies)
488
            {
489
                List<Topology> result = new List<Topology>();
490
                foreach (DataRow row in nominalDiameterDT.Rows)
491
                {
492
                    string inchValue = row["InchStr"].ToString();
493
                    string metricValue = row["MetricStr"].ToString();
494
                    foreach (Topology topology in topologies)
495
                    {
496
                        foreach (Item item in topology.Items)
497
                        {
498
                            if (item.LineNumber == null)
499
                                continue;
500
                            Attribute attribute = item.LineNumber.Attributes.Find(x => x.Name == "NominalDiameter");
501
                            if (attribute != null && (inchValue == attribute.Value || metricValue == attribute.Value))
502
                            {
503
                                result.Add(topology);
504
                                break;
505
                            }
506
                        }
507
                    }
508

    
509
                    if (result.Count > 0)
510
                        break;
511
                }
512

    
513
                return result;
514
            }
515

    
516
            List<Topology> GetItemTopology(List<Topology> topologies)
517
            {
518
                return new List<Topology>() { topologies.OrderByDescending(x => x.Items.Count).ToList().First() };
519
            }
520

    
521
            return main;
522
        }
523

    
524
        private DataTable GetOPCInfo()
525
        {
526
            DataTable opc = DB.SelectOPCRelations();
527
            DataTable drawing = DB.SelectDrawings();
528

    
529
            DataTable dt = new DataTable();
530
            dt.Columns.Add("FromDrawing", typeof(string));
531
            dt.Columns.Add("FromDrawingUID", typeof(string));
532
            dt.Columns.Add("FromOPCUID", typeof(string));
533
            dt.Columns.Add("ToDrawing", typeof(string));
534
            dt.Columns.Add("ToDrawingUID", typeof(string));
535
            dt.Columns.Add("ToOPCUID", typeof(string));
536
            foreach (DataRow row in opc.Rows)
537
            {
538
                string fromDrawingUID = row["From_Drawings_UID"] == null ? string.Empty : row["From_Drawings_UID"].ToString();
539
                string fromOPCUID = row["From_OPC_UID"] == null ? string.Empty : row["From_OPC_UID"].ToString(); 
540
                string toDrawingUID = row["To_Drawings_UID"] == null ? string.Empty : row["To_Drawings_UID"].ToString(); 
541
                string toOPCUID = row["To_OPC_UID"] == null ? string.Empty : row["To_OPC_UID"].ToString();
542
                if (!string.IsNullOrEmpty(toOPCUID))
543
                {
544
                    DataRow[] fromRows = drawing.Select(string.Format("UID = '{0}'", fromDrawingUID));
545
                    DataRow[] toRows = drawing.Select(string.Format("UID = '{0}'", toDrawingUID));
546
                    if (fromRows.Length.Equals(1) && toRows.Length.Equals(1))
547
                    {
548
                        string fromDrawingName = Path.GetFileNameWithoutExtension(fromRows.First()["NAME"].ToString());
549
                        string toDrawingName = Path.GetFileNameWithoutExtension(toRows.First()["NAME"].ToString());
550

    
551
                        DataRow newRow = dt.NewRow();
552
                        newRow["FromDrawing"] = fromDrawingName;
553
                        newRow["FromDrawingUID"] = fromDrawingUID;
554
                        newRow["FromOPCUID"] = fromOPCUID;
555
                        newRow["ToDrawing"] = toDrawingName;
556
                        newRow["ToDrawingUID"] = toDrawingUID;
557
                        newRow["ToOPCUID"] = toOPCUID;
558

    
559
                        dt.Rows.Add(newRow);
560
                    }
561
                }
562
            }
563

    
564
            return dt;
565
        }
566
        private DataTable GetTopologyRule()
567
        {
568
            DataTable dt = DB.SelectTopologyRule();
569

    
570
            return dt;
571
        }
572
        private bool IsConnected(Item item1, Item item2)
573
        {
574
            if (item1.Relations.Find(x => x.UID.Equals(item2.UID)) != null &&
575
                item2.Relations.Find(x => x.UID.Equals(item1.UID)) != null)
576
                return true;
577
            else
578
                return false;
579
        }
580

    
581
        private void SaveNozzleAndEquipment()
582
        {
583
            List<Item> nozzles = new List<Item>();
584
            List<Equipment> equipments = new List<Equipment>();
585
            foreach (Document document in Documents)
586
            {
587
                nozzles.AddRange(document.Items.FindAll(x => x.SubItemType == SubItemType.Nozzle));
588
                equipments.AddRange(document.Equipments);
589
            }
590
                
591

    
592
            DataTable nozzleDT = new DataTable();
593
            nozzleDT.Columns.Add("OID", typeof(string));
594
            nozzleDT.Columns.Add("ITEMTAG", typeof(string));
595
            nozzleDT.Columns.Add("XCOORDS", typeof(string));
596
            nozzleDT.Columns.Add("YCOORDS", typeof(string));
597
            nozzleDT.Columns.Add("Equipment_OID", typeof(string));
598
            nozzleDT.Columns.Add("FLUID", typeof(string));
599
            nozzleDT.Columns.Add("NPD", typeof(string));
600
            nozzleDT.Columns.Add("ROTATION", typeof(string));
601
            nozzleDT.Columns.Add("FlowDirection", typeof(string));
602

    
603
            foreach (Item item in nozzles)
604
            {
605
                DataRow row = nozzleDT.NewRow();
606
                row["OID"] = item.UID;
607

    
608
                Relation relation = item.Relations.Find(x => equipments.Find(y => y.UID == x.UID) != null);
609
                if (relation != null)
610
                {
611
                    Equipment equipment = equipments.Find(x => x.UID == relation.UID);
612
                    equipment.Nozzles.Add(item);
613
                    row["ITEMTAG"] = string.Format("N-{0}", string.Format("{0:D3}", equipment.Nozzles.Count + 100));
614
                    row["Equipment_OID"] = equipment.UID;
615
                    item.Equipment = equipment;
616
                }
617
                row["XCOORDS"] = (item.POINT[0] / DrawingWidth).ToString();
618
                row["YCOORDS"] = (item.POINT[1] / DrawingHeight).ToString();
619
                Attribute fluidAttr = item.LineNumber.Attributes.Find(x => x.Name == "FluidCode");
620
                row["FLUID"] = fluidAttr != null ? fluidAttr.Value : string.Empty;
621
                Attribute npdAttr = item.LineNumber.Attributes.Find(x => x.Name == "NominalDiameter");
622
                row["NPD"] = npdAttr != null ? npdAttr.Value : string.Empty;
623

    
624
                double angle = Math.PI * 2 - Convert.ToDouble(item.ANGLE);
625
                if (angle >= Math.PI * 2)
626
                    angle = angle - Math.PI * 2;
627
                row["ROTATION"] = angle.ToString();
628

    
629
                if (item.Topology.Items.First().Equals(item))
630
                    row["FlowDirection"] = "Outlet";
631
                else if (item.Topology.Items.Last().Equals(item))
632
                    row["FlowDirection"] = "Inlet";
633
                else
634
                    row["FlowDirection"] = string.Empty;
635

    
636
                nozzleDT.Rows.Add(row);
637
            }
638

    
639
            DataTable equipDT = new DataTable();
640
            equipDT.Columns.Add("OID", typeof(string));
641
            equipDT.Columns.Add("ITEMTAG", typeof(string));
642
            equipDT.Columns.Add("XCOORDS", typeof(string));
643
            equipDT.Columns.Add("YCOORDS", typeof(string));
644

    
645
            foreach (Equipment equipment in equipments)
646
            {
647
                DataRow row = equipDT.NewRow();
648
                row["OID"] = equipment.UID;
649
                if (!string.IsNullOrEmpty(EquipTagNoAttributeName))
650
                {
651
                    Attribute attribute = equipment.Attributes.Find(x => x.Name == EquipTagNoAttributeName);
652
                    if (attribute != null)
653
                        equipment.ItemTag = attribute.Value;
654
                }
655
                else
656
                    equipment.ItemTag = equipment.Name;
657

    
658
                row["ITEMTAG"] = equipment.ItemTag;
659
                List<double> xList = equipment.POINT.Select(x => x[0]).ToList();
660
                row["XCOORDS"] = (xList.Sum() / (double)xList.Count) / DrawingWidth;
661

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

    
665
                equipDT.Rows.Add(row);
666
            }
667

    
668
            Equipment = equipDT;
669
            Nozzle = nozzleDT;
670
        }
671
        private void SavePSNData()
672
        {
673
            DataTable pathItemsDT = new DataTable();
674
            pathItemsDT.Columns.Add("OID", typeof(string));
675
            pathItemsDT.Columns.Add("SequenceData_OID", typeof(string));
676
            pathItemsDT.Columns.Add("TopologySet_OID", typeof(string));
677
            pathItemsDT.Columns.Add("BranchTopologySet_OID", typeof(string));
678
            pathItemsDT.Columns.Add("PipeLine_OID", typeof(string));
679
            pathItemsDT.Columns.Add("ITEMNAME", typeof(string));
680
            pathItemsDT.Columns.Add("ITEMTAG", typeof(string));
681
            pathItemsDT.Columns.Add("TYPE", typeof(string));
682
            pathItemsDT.Columns.Add("PIDNAME", typeof(string));
683
            pathItemsDT.Columns.Add("NPD", typeof(string));
684
            pathItemsDT.Columns.Add("PipeSystemNetwork_OID", typeof(string));
685
            pathItemsDT.Columns.Add("PipeRun_OID", typeof(string));
686

    
687
            DataTable sequenceDataDT = new DataTable();
688
            sequenceDataDT.Columns.Add("OID", typeof(string));
689
            sequenceDataDT.Columns.Add("SERIALNUMBER", typeof(string));
690
            sequenceDataDT.Columns.Add("PathItem_OID", typeof(string));
691
            sequenceDataDT.Columns.Add("TopologySet_OID_Key", typeof(string));
692

    
693
            DataTable pipeSystemNetworkDT = new DataTable();
694
            pipeSystemNetworkDT.Columns.Add("OID", typeof(string));
695
            pipeSystemNetworkDT.Columns.Add("Type", typeof(string));
696
            pipeSystemNetworkDT.Columns.Add("OrderNumber", typeof(string));
697
            pipeSystemNetworkDT.Columns.Add("Pipeline_OID", typeof(string));
698
            pipeSystemNetworkDT.Columns.Add("FROM_DATA", typeof(string));
699
            pipeSystemNetworkDT.Columns.Add("TO_DATA", typeof(string));
700
            pipeSystemNetworkDT.Columns.Add("TopologySet_OID_Key", typeof(string));
701
            pipeSystemNetworkDT.Columns.Add("PSNRevisionNumber", typeof(string));
702
            pipeSystemNetworkDT.Columns.Add("PathOID", typeof(string));
703
            pipeSystemNetworkDT.Columns.Add("PBS", typeof(string));
704
            pipeSystemNetworkDT.Columns.Add("PIDDrawings", typeof(string));
705

    
706
            // key = 미입력 branch
707
            Dictionary<Item, Item> startBranchDic = new Dictionary<Item, Item>();
708
            Dictionary<Item, Item> endBranchDic = new Dictionary<Item, Item>();
709
            foreach (PSNItem PSNItem in PSNItems)
710
            {
711
                int psnOrder = 0;
712
                int index = 0;
713
                bool bPSNStart = true;
714
                string sPSNData = string.Empty;
715
                foreach (Group group in PSNItem.Groups)
716
                {
717
                    foreach (Item item in group.Items)
718
                    {
719
                        if (item.BranchItems.Count == 0)
720
                        {
721
                            CreatePathItemsDataRow(item.UID, item.Type);
722
                            CreateSequenceDataDataRow(item.UID);
723
                            index++;
724
                        }
725
                        else
726
                        {
727
                            CreatePathItemsDataRow(item.UID + "_L1", item.Type);
728
                            CreateSequenceDataDataRow(item.UID + "_L1");
729
                            index++;
730
                            for (int i = 0; i < item.BranchItems.Count; i++)
731
                            {
732
                                CreatePathItemsDataRow(string.Format(item.UID + "_B{0}", i + 1), "Branch", item.BranchItems[i].Topology.FullName);
733
                                CreateSequenceDataDataRow(string.Format(item.UID + "_B{0}", i + 1));
734
                                index++;
735

    
736
                                CreatePathItemsDataRow(string.Format(item.UID + "_L{0}", i + 2), item.Type);
737
                                CreateSequenceDataDataRow(string.Format(item.UID + "_L{0}", i + 2));
738
                                index++;
739

    
740
                                if (item.BranchItems[i].Relations[0].Item != null && item.BranchItems[i].Relations[0].Item == item)
741
                                    startBranchDic.Add(item.BranchItems[i], item);
742
                                else if (item.BranchItems[i].Relations[1].Item != null && item.BranchItems[i].Relations[1].Item == item)
743
                                    endBranchDic.Add(item.BranchItems[i], item);
744
                            }
745
                        }
746

    
747
                        if (bPSNStart)
748
                        {
749
                            CreatePipeSystemNetworkDataRow();
750
                            sPSNData = item.TopologyData;
751
                            psnOrder++;
752
                            bPSNStart = false;
753
                        }
754
                        else
755
                        {
756
                            if (item.TopologyData != sPSNData)
757
                            {
758
                                CreatePipeSystemNetworkDataRow();
759
                                sPSNData = item.TopologyData;
760
                                psnOrder++;
761
                            }
762
                        }
763
                        void CreatePathItemsDataRow(string itemOID, string itemType, string branchTopologyName = "")
764
                        {
765
                            DataRow newRow = pathItemsDT.NewRow();
766
                            newRow["OID"] = itemOID;
767

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

    
770
                            newRow["TopologySet_OID"] = item.Topology.FullName;
771

    
772
                            newRow["BranchTopologySet_OID"] = branchTopologyName;
773
                            newRow["PipeLine_OID"] = item.PSNPipeLineID;
774
                            newRow["ITEMNAME"] = null;
775
                            newRow["ITEMTAG"] = GetItemTag();
776
                            newRow["TYPE"] = item.Name;
777
                            newRow["PIDNAME"] = group.Document.DrawingName;
778

    
779
                            // NPD
780
                            if (item.LineNumber != null)
781
                            {
782
                                Attribute attribute = item.LineNumber.Attributes.Find(x => x.Name == "NominalDiameter");
783
                                if (attribute != null)
784
                                    newRow["NPD"] = attribute.Value;
785
                            }
786
                            else
787
                                newRow["NPD"] = null;
788

    
789
                            newRow["PipeSystemNetwork_OID"] = PSNItem.PSN_OID();
790
                            newRow["PipeRun_OID"] = item.LineNumber != null ? item.LineNumber.Name : string.Empty;
791

    
792
                            pathItemsDT.Rows.Add(newRow);
793
                        }
794
                        void CreateSequenceDataDataRow(string itemOID)
795
                        {
796
                            DataRow newRow = sequenceDataDT.NewRow();
797
                            newRow["OID"] = string.Format(item.Topology.FullName + "_{0}", index);
798
                            newRow["SERIALNUMBER"] = string.Format("{0}", index);
799
                            newRow["PathItem_OID"] = itemOID;
800
                            newRow["TopologySet_OID_Key"] = item.Topology.FullName;
801

    
802
                            sequenceDataDT.Rows.Add(newRow);
803
                        }
804
                        void CreatePipeSystemNetworkDataRow()
805
                        {
806
                            DataRow newRow = pipeSystemNetworkDT.NewRow();
807
                            newRow["OID"] = PSNItem.PSN_OID();
808
                            newRow["Type"] = PSNItem.GetPSNType();
809
                            newRow["OrderNumber"] = psnOrder;
810
                            newRow["Pipeline_OID"] = item.PSNPipeLineID;
811
                            newRow["FROM_DATA"] = PSNItem.GetFromData();
812
                            newRow["TO_DATA"] = PSNItem.GetToData();
813
                            newRow["TopologySet_OID_Key"] = item.Topology.FullName;
814
                            newRow["PSNRevisionNumber"] = string.Format("{0:D3}", Revision);
815
                            newRow["PathOID"] = null;
816
                            newRow["PBS"] = null;
817

    
818

    
819
                            List<string> drawingNames = new List<string>();
820
                            foreach (Group _group in PSNItem.Groups)
821
                            {
822
                                if (newRow["PBS"] == null)
823
                                {
824
                                    List<Item> _items = _group.Items.FindAll(x => x.Attributes.Find(y => y.Name.ToUpper() == "UNITNUMBER" && !string.IsNullOrEmpty(y.Value)) != null);
825
                                    foreach (Item _item in _items)
826
                                    {
827
                                        string _value = _item.Attributes.Find(x => x.Name.ToUpper() == "UNITNUMBER").Value;
828
                                        newRow["PBS"] = _value;
829
                                        break;
830
                                    }
831
                                }
832

    
833
                                if (!drawingNames.Contains(_group.Document.DrawingName))
834
                                {
835
                                    if (drawingNames.Count == 0)
836
                                        newRow["PIDDrawings"] = _group.Document.DrawingName;
837
                                    else
838
                                        newRow["PIDDrawings"] = newRow["PIDDrawings"] + ", " + _group.Document.DrawingName;
839
                                    drawingNames.Add(_group.Document.DrawingName);
840
                                }
841
                            }
842
                            pipeSystemNetworkDT.Rows.Add(newRow);
843
                        }
844

    
845
                        string GetItemTag()
846
                        {
847
                            string result = string.Empty;
848
                            if (item.ItemType == ItemType.Line)
849
                                result = item.LineNumber != null ? item.LineNumber.Name : string.Empty;
850
                            else if (item.ItemType == ItemType.Symbol && item.SubItemType == SubItemType.Nozzle)
851
                                result = Nozzle.Select(string.Format("OID = '{0}'", item.UID)).First()["ITEMTAG"].ToString();
852

    
853

    
854
                            return result;
855
                        }
856
                    }
857
                }
858
            }
859

    
860
            foreach (var item in startBranchDic)
861
            {
862
                string uid = item.Key.UID;
863
                string topologyName = item.Value.Topology.FullName;
864
                DataRow[] rows = pathItemsDT.Select(string.Format("OID LIKE '{0}%'", uid));
865
                if (rows.Length == 1)
866
                    rows.First()["BranchTopologySet_OID"] = topologyName;
867
                else if (rows.Length > 1)
868
                {
869
                    DataRow targetRow = null;
870
                    int index = int.MaxValue;
871
                    foreach (DataRow row in rows)
872
                    {
873
                        string split = row["OID"].ToString().Split(new char[] { '_' })[1];
874
                        if (split.StartsWith("L"))
875
                        {
876
                            int num = Convert.ToInt32(split.Remove(0, 1));
877
                            if (index > num)
878
                            {
879
                                index = num;
880
                                targetRow = row;
881
                            }
882
                        }
883
                    }
884

    
885
                    if (targetRow != null)
886
                        targetRow["BranchTopologySet_OID"] = topologyName;
887
                }
888
            }
889
            foreach (var item in endBranchDic)
890
            {
891
                string uid = item.Key.UID;
892
                string topologyName = item.Value.Topology.FullName;
893
                DataRow[] rows = pathItemsDT.Select(string.Format("OID LIKE '{0}%'", uid));
894
                if (rows.Length == 1)
895
                    rows.First()["BranchTopologySet_OID"] = topologyName;
896
                else if (rows.Length > 1)
897
                {
898
                    DataRow targetRow = null;
899
                    int index = int.MinValue;
900
                    foreach (DataRow row in rows)
901
                    {
902
                        string split = row["OID"].ToString().Split(new char[] { '_' })[1];
903
                        if (split.StartsWith("L"))
904
                        {
905
                            int num = Convert.ToInt32(split.Remove(0, 1));
906
                            if (index < num)
907
                            {
908
                                index = num;
909
                                targetRow = row;
910
                            }
911
                        }
912
                    }
913

    
914
                    if (targetRow != null)
915
                        targetRow["BranchTopologySet_OID"] = topologyName;
916
                }
917
            }
918

    
919
            PathItems = pathItemsDT;
920
            SequenceData = sequenceDataDT;
921
            PipeSystemNetwork = pipeSystemNetworkDT;
922
        }
923
    }
924

    
925
    public class PSNItem
926
    {
927
        public PSNItem(int count, int Revision)
928
        {
929
            Groups = new List<Group>();
930
            Topologies = new List<Topology>();
931

    
932
            Index = count + 1;
933
            this.Revision = Revision;
934
        }
935
        private int Revision;
936
        public string UID { get; set; }
937
        public List<Group> Groups { get; set; }
938
        public List<Topology> Topologies { get; set; }
939
        public PSNType StartType { get; set; }
940
        public PSNType EndType { get; set; }
941
        public int Index { get; set; }
942
        public string PSN_OID()
943
        {
944
            return string.Format("R{0}-PSN-{1}", string.Format("{0:D3}", Revision), string.Format("{0:D5}", Index));
945
        }
946
        public string GetPSNType()
947
        {
948
            string result = string.Empty;
949

    
950
            if (EnableType(StartType) && EnableType(EndType))
951
            {
952
                if (StartType == PSNType.Equipment && EndType == PSNType.Equipment)
953
                    result = "E2E";
954
                else if (StartType == PSNType.Branch && EndType == PSNType.Branch)
955
                    result = "B2B";
956
                else if (StartType == PSNType.Header && EndType == PSNType.Header)
957
                    result = "HD2";
958

    
959
                else if (StartType == PSNType.Equipment && EndType == PSNType.Branch)
960
                    result = "E2B";
961
                else if (StartType == PSNType.Branch && EndType == PSNType.Equipment)
962
                    result = "B2E";
963

    
964
                else if (StartType == PSNType.Header && EndType == PSNType.Branch)
965
                    result = "HDB";
966
                else if (StartType == PSNType.Branch && EndType == PSNType.Header)
967
                    result = "HDB";
968

    
969
                else if (StartType == PSNType.Header && EndType == PSNType.Equipment)
970
                    result = "HDE";
971
                else if (StartType == PSNType.Equipment && EndType == PSNType.Header)
972
                    result = "HDE";
973
                else
974
                    result = "Error";
975
            }
976
            else
977
                result = "Error";
978

    
979
            return result;
980

    
981
            
982
        }
983
        private bool EnableType(PSNType type)
984
        {
985
            bool result = false;
986

    
987
            if (type == PSNType.Branch ||
988
                type == PSNType.Equipment ||
989
                type == PSNType.Header)
990
            {
991
                result = true;
992
            }
993

    
994
            return result;
995
        }
996

    
997
        public string GetFromData()
998
        {
999
            string result = string.Empty;
1000
            if (StartType == PSNType.Header)
1001
                result = "ENDOFHEADER";
1002
            else if (StartType == PSNType.Branch)
1003
                result = Groups.First().Items.First().LineNumber.Name;
1004
            else if (StartType == PSNType.Equipment)
1005
                result = Groups.First().Items.First().Equipment.ItemTag;
1006
            else
1007
                result = "Unknown";
1008

    
1009
            return result;
1010
        }
1011

    
1012
        public string GetToData()
1013
        {
1014
            string result = string.Empty;
1015
            if (EndType == PSNType.Header)
1016
                result = "ENDOFHEADER";
1017
            else if (EndType == PSNType.Branch)
1018
                result = Groups.Last().Items.Last().LineNumber.Name;
1019
            else if (EndType == PSNType.Equipment)
1020
                result = Groups.Last().Items.Last().Equipment.ItemTag;
1021
            else 
1022
                result = "Unknown";
1023
            return result;
1024
        }
1025
    }
1026
}
클립보드 이미지 추가 (최대 크기: 500 MB)