프로젝트

일반

사용자정보

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

hytos / DTI_PID / ID2PSN / PSN.cs @ bfbc9f6c

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

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

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

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

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

    
48
        public string Rule1 = "Missing LineNumber_1"; //Line Disconnected에서 변경
49
        public string Rule2 = "Missing LineNumber_2"; //Missing LineNumber에서 변경
50
        public string Rule3 = "OPC Disconnected";
51
        public string Rule4 = "Missing ItemTag or Description";
52
        public string Rule5 = "Line Disconnected";
53

    
54
        int tieInPointIndex = 1;
55

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

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

    
64
        ID2Info id2Info = ID2Info.GetInstance();
65

    
66

    
67

    
68
        //const string FluidPriorityType = "FLUIDCODE";
69
        //const string PipingMaterialsPriorityType = "PIPINGMATERIALSCLASS";
70

    
71
        public PSN()
72
        {
73

    
74
        }
75

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

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

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

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

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

    
208
        public void SetPSNData(SplashScreenManager splashScreenManager1)
209
        {
210
            // Item들의 속성으로 Topology Data를 생성한다.
211
            // Topology Data는 Topology Rule Setting을 기준으로 생성한다.
212
            int i = 1;
213
            splashScreenManager1.SetWaitFormDescription(i++ + " / 19  SetTopologyData ( 3 % )");
214
            SetTopologyData();
215
            // ID2의 OPC연결 Data 기반으로 Group(도면단위 PSN)을 연결한다.
216
            splashScreenManager1.SetWaitFormDescription(i++ + " / 19  ConnectByOPC ( 5 % )");
217
            ConnectByOPC();
218
            // 실제 PSN 생성 로직
219
            // 연결된 Group을 하나의 PSN으로 만든다.
220
            splashScreenManager1.SetWaitFormDescription(i++ + " / 19  SetPSNItem ( 10 % )");
221
            SetPSNItem();
222
            // 생성된 PSN의 Type을 설정한다.
223
            splashScreenManager1.SetWaitFormDescription(i++ + " / 19  SetPSNType ( 15 % )");
224
            SetPSNType();
225
            // ID2에는 Branch 정보가 없어서 Branch 정보를 생성한다.
226
            splashScreenManager1.SetWaitFormDescription(i++ + " / 19  SetBranchInfo ( 20 % )");
227
            SetBranchInfo();
228
            // 생성된 Topology Data들을 정리하며 Main, Branch를 판단한다.
229
            splashScreenManager1.SetWaitFormDescription(i++ + " / 19  SetTopology ( 25 % )");
230
            SetTopology();
231
            // PSN이 Bypass인지 검사 
232
            splashScreenManager1.SetWaitFormDescription(i++ + " / 19  SetPSNBypass ( 30 % )");
233
            SetPSNBypass();
234
            // Topology들에게 Index를 부여한다
235
            splashScreenManager1.SetWaitFormDescription(i++ + " / 19  SetTopologyIndex ( 35 % )");
236
            SetTopologyIndex();
237
            // Nozzle, Equipment의 정보를 저장
238
            splashScreenManager1.SetWaitFormDescription(i++ + " / 19  SaveNozzleAndEquipment ( 40 % )");
239
            SaveNozzleAndEquipment();
240
            // PSN의 정보를 저장
241
            splashScreenManager1.SetWaitFormDescription(i++ + " / 19  SetPSNData ( 45 % )");
242
            SavePSNData();
243
            // Update Keyword
244
            splashScreenManager1.SetWaitFormDescription(i++ + " / 19  UpdateKeywordForPSN ( 55 % )");
245
            UpdateKeywordForPSN();
246
          
247
            // Topology의 subtype을 update(bypass, Header, 등등) 
248
            splashScreenManager1.SetWaitFormDescription(i++ + " / 19  UpdateSubType ( 60 % )");
249
            UpdateSubType();
250
            // Update Error
251
            splashScreenManager1.SetWaitFormDescription(i++ + " / 19  UpdateErrorForPSN ( 65 % )");
252
            PathItemSorting();
253
            UpdateErrorForPSN();
254
            // Insert Tee
255
            splashScreenManager1.SetWaitFormDescription(i++ + " / 19  InsertTeePSN ( 70 % )");
256
            InsertTeePSN();
257
         
258
            // ValveGrouping
259
            splashScreenManager1.SetWaitFormDescription(i++ + " / 19  UpdateValveGrouping ( 75 % )");
260
            UpdateValveGrouping();
261

    
262
            splashScreenManager1.SetWaitFormDescription(i++ + " / 19  PathItemSorting ( 80 % )");
263
            PathItemSorting();
264

    
265
            // AirFinCooler 
266
            splashScreenManager1.SetWaitFormDescription(i++ + " / 19  UpdateAirFinCooler ( 85 % )");
267
            UpdateAirFinCooler();
268

    
269
            // Vent/Drain PSN 데이터 제거
270
            splashScreenManager1.SetWaitFormDescription(i++ + " / 19  DeleteVentDrain ( 90 % )");
271
            DeleteVentDrain();
272

    
273
            // 확도 계산
274
            splashScreenManager1.SetWaitFormDescription(i++ + " / 19  UpdateAccuracy ( 95 % )");
275
            UpdateAccuracy();
276

    
277
            //UpdatePSNType();
278

    
279
        }
280

    
281
        private void UpdatePSNType()
282
        {
283
            try
284
            {
285
                foreach (PSNItem PSNItem in PSNItems)
286
                {    
287
                    foreach (Group group in PSNItem.Groups)
288
                    {
289
                        foreach (Item item in group.Items)
290
                        {
291
                            DataRow[] pipeSystems = PipeSystemNetwork.Select(string.Format("OID = '{0}'", PSNItem.PSN_OID()));
292
                           // PSNItem PSNItem = PSNItems.Find(x => x.PSN_OID() == dataRow["OID"].ToString());
293

    
294
                            foreach(DataRow dr in pipeSystems)
295
                            {
296
                                dr["Type"] = PSNItem.GetPSNType();
297
                            }                            
298
                        }
299
                    }                
300
                }
301
            }
302
            catch (Exception ex)
303
            {
304
                MessageBox.Show(ex.Message, "ID2 ", MessageBoxButtons.OK, MessageBoxIcon.Error);
305
            }
306
        }
307

    
308
        private void PathItemSorting()
309
        {
310
            try
311
            {
312
                DataTable dtPathItems = PathItems.Clone();
313
                DataTable dtPipeSystemNetwork = PipeSystemNetwork.DefaultView.ToTable(true, new string[] { "OID" });
314

    
315
                foreach (DataRow drpipe in dtPipeSystemNetwork.Rows)
316
                {
317
                    DataRow[] pathItemRows = PathItems.Select(string.Format("PipeSystemNetwork_OID = '{0}'", drpipe["OID"].ToString()));
318
                    DataTable dtSequenceItems = SequenceData.Clone();
319
                    foreach (DataRow drpath in pathItemRows)
320
                    {
321
                        DataRow sequenceRows = SequenceData.Select(string.Format("PathItem_OID = '{0}'", drpath.Field<string>("OID"))).First();
322

    
323
                        DataRow newRow = dtSequenceItems.NewRow();
324
                        foreach (DataColumn column in SequenceData.Columns)
325
                            if (dtSequenceItems.Columns[column.ColumnName] != null)
326
                                newRow[column.ColumnName] = sequenceRows[column.ColumnName];
327

    
328
                        dtSequenceItems.Rows.Add(newRow);
329
                    }
330

    
331
                    foreach (DataRow sqrow in dtSequenceItems.Select().OrderBy(x => Convert.ToInt32(x.Field<string>("SERIALNUMBER"))))
332
                    {
333
                        DataRow newRow = dtPathItems.NewRow();
334
                        DataRow row = PathItems.Select(string.Format("OID = '{0}'", sqrow["PathItem_OID"])).First();
335

    
336
                        foreach (DataColumn column in PathItems.Columns)
337
                            if (dtPathItems.Columns[column.ColumnName] != null)
338
                                newRow[column.ColumnName] = row[column.ColumnName];
339

    
340
                        dtPathItems.Rows.Add(newRow);
341
                    }
342
                }
343

    
344
                PathItems.Clear();
345
                PathItems = dtPathItems.Copy();
346
            }
347
            catch (Exception ex)
348
            {
349
                MessageBox.Show(ex.Message, "ID2 ", MessageBoxButtons.OK, MessageBoxIcon.Error);
350
            }
351
        }
352
        
353
        private void UpdateAirFinCooler()
354
        {
355
            try
356
            {
357
                
358
                int pumpTagNum = 0;
359
                #region EquipmentAirFinCooler Info
360
                EquipmentAirFinCoolerInfo EquipmentAirFinCooler = new EquipmentAirFinCoolerInfo();
361
                DataTable dtEquipmentAirFinCooler = DB.SelectAirFinCoolerSetting();
362
                foreach (DataRow row in dtEquipmentAirFinCooler.Rows)
363
                {
364
                    EquipmentAirFinCooler.EquipmentAirFinCoolerItem.Add(new EquipmentAirFinCoolerItem()
365
                    {
366
                        Type = row["Type"].ToString(),
367
                        TagIdentifier = row["TagIdentifier"].ToString(),
368
                        AttributeName = row["AttributeName"].ToString(),
369
                        Name = row["Name"].ToString()
370
                    });
371
                }
372
                #endregion
373

    
374
                
375
                DataRow[] pumpRows = PipeSystemNetwork.Select("PUMP = 'PUMP'");
376
                // 1, 2번
377
                foreach (DataRow dataRow in pumpRows) 
378
                {                  
379
                    DataRow[] pathItemRows = PathItems.Select(string.Format("PipeSystemNetwork_OID = '{0}'", dataRow["OID"].ToString()));
380

    
381
                    string EGTAG = "EGTAG";
382
                    string ItemTag = "ItemTag";
383
                    string EGFlowDirection = string.Empty;
384
                    string Prefix = string.Empty;
385
                    Dictionary<string, string> eqkeyValuePairs = new Dictionary<string, string>();
386

    
387
                    PSNItem PSNItem = PSNItems.Find(x => x.PSN_OID() == dataRow["OID"].ToString());
388
                    if (PSNItem.Groups.First().Items.First().Equipment != null)
389
                    {
390
                        if (EquipmentAirFinCooler.EquipmentAirFinCoolerItem.Where(x => x.Type == "Pump" && PSNItem.Groups.First().Items.First().Equipment.Name.Contains(x.Name)).Count() > 0)
391
                        {
392
                            EquipmentAirFinCoolerItem equipmentAirFinCoolerItem = EquipmentAirFinCooler.EquipmentAirFinCoolerItem.Where(x => x.Type == "Pump" && PSNItem.Groups.First().Items.First().Equipment.Name.Contains(x.Name)).First();
393
                            if (!string.IsNullOrEmpty(equipmentAirFinCoolerItem.AttributeName))
394
                                EGTAG = PSNItem.Groups.First().Items.First().Equipment.Attributes.Find(x => x.Name == equipmentAirFinCoolerItem.AttributeName) == null
395
                                   ? string.Empty : PSNItem.Groups.First().Items.First().Equipment.Attributes.Find(x => x.Name == equipmentAirFinCoolerItem.AttributeName).Value;
396

    
397
                            ItemTag = PSNItem.Groups.First().Items.First().Equipment.ItemTag;
398
                            Prefix = equipmentAirFinCoolerItem.TagIdentifier;
399
                            EGTAG = Prefix + "-" + EGTAG;
400
                            EGFlowDirection = "O";
401
                        }                        
402
                    } 
403
                    else if(PSNItem.Groups.Last().Items.Last().Equipment != null)
404
                    {
405
                        if (EquipmentAirFinCooler.EquipmentAirFinCoolerItem.Where(x => x.Type == "Pump" && PSNItem.Groups.Last().Items.Last().Equipment.Name.Contains(x.Name)).Count() > 0)
406
                        {
407
                            EquipmentAirFinCoolerItem equipmentAirFinCoolerItem = EquipmentAirFinCooler.EquipmentAirFinCoolerItem.Where(x => x.Type == "Pump" && PSNItem.Groups.Last().Items.Last().Equipment.Name.Contains(x.Name)).First();
408
                            if (!string.IsNullOrEmpty(equipmentAirFinCoolerItem.AttributeName))
409
                                EGTAG = EGTAG = PSNItem.Groups.Last().Items.Last().Equipment.Attributes.Find(x => x.Name == equipmentAirFinCoolerItem.AttributeName) == null
410
                                    ? string.Empty : PSNItem.Groups.Last().Items.Last().Equipment.Attributes.Find(x => x.Name == equipmentAirFinCoolerItem.AttributeName).Value;
411

    
412
                            ItemTag = PSNItem.Groups.Last().Items.Last().Equipment.ItemTag;
413
                            Prefix = equipmentAirFinCoolerItem.TagIdentifier;
414
                            EGTAG = Prefix + "-" + EGTAG;
415
                            EGFlowDirection = "I";
416
                        }
417
                    }
418

    
419
                    //Attribute가 세팅되어있지 않다면
420
                    if (EGTAG.Equals("EGTAG"))
421
                    {
422
                        if (!eqkeyValuePairs.ContainsKey(ItemTag))
423
                        {
424
                            pumpTagNum++;
425
                            EGTAG = Prefix + string.Format("-{0}", string.Format("{0:D5}", pumpTagNum));
426
                            eqkeyValuePairs.Add(ItemTag, EGTAG);
427
                        }
428
                        else
429
                        {
430
                            EGTAG = eqkeyValuePairs[ItemTag];
431
                        }
432
                    }
433

    
434
                    foreach (DataRow dr in pathItemRows)
435
                    {
436
                        dr["EqpGroupTag"] = EGTAG;
437
                        dr["EGFlowDirection"] = EGFlowDirection;
438
                    }
439
                }
440

    
441
                // 3, 4번
442
                foreach (DataRow dataRow in pumpRows) 
443
                {
444

    
445
                    DataRow[] pathItemRows = PathItems.Select(string.Format("PipeSystemNetwork_OID = '{0}'", dataRow["OID"].ToString()));
446
                    PSNItem PSNItem = PSNItems.Find(x => x.PSN_OID() == dataRow["OID"].ToString());
447
                    string EGFlowDirection = string.Empty;
448

    
449
                    if (PSNItem.Groups.First().Items.First().Equipment != null)
450
                    {
451
                        if (EquipmentAirFinCooler.EquipmentAirFinCoolerItem.Where(x => x.Type == "Pump" && PSNItem.Groups.First().Items.First().Equipment.Name.Contains(x.Name)).Count() > 0)
452
                        {
453
                            EGFlowDirection = "O";
454
                        }
455
                    }
456
                    else if (PSNItem.Groups.Last().Items.Last().Equipment != null)
457
                    {
458
                        if (EquipmentAirFinCooler.EquipmentAirFinCoolerItem.Where(x => x.Type == "Pump" && PSNItem.Groups.Last().Items.Last().Equipment.Name.Contains(x.Name)).Count() > 0)
459
                        {
460
                            EGFlowDirection = "I";
461
                        }
462
                    }
463

    
464

    
465
                    List<string> lstViewPipeSystemNetwork_OID = new List<string>();
466

    
467
                    foreach (DataRow dr in pathItemRows)
468
                    {
469
                        if(dr.Field<string>("ViewPipeSystemNetwork_OID") != dataRow["OID"].ToString())
470
                        {
471
                            string viewEGFlowDirection = string.Empty;
472
                            if (PipeSystemNetwork.Select(string.Format("OID = '{0}'", dr.Field<string>("ViewPipeSystemNetwork_OID"))).Count() > 0)
473
                            {
474
                                PSNItem viewPSNItem = PSNItems.Find(x => x.PSN_OID() == dr.Field<string>("ViewPipeSystemNetwork_OID"));                               
475

    
476
                                if (viewPSNItem.Groups.First().Items.First().Equipment != null)
477
                                {
478
                                    if (EquipmentAirFinCooler.EquipmentAirFinCoolerItem.Where(x => x.Type == "Pump" && viewPSNItem.Groups.First().Items.First().Equipment.Name.Contains(x.Name)).Count() > 0)
479
                                    {
480
                                        viewEGFlowDirection = "O";
481
                                    }
482
                                }
483
                                else if (viewPSNItem.Groups.Last().Items.Last().Equipment != null)
484
                                {
485
                                    if (EquipmentAirFinCooler.EquipmentAirFinCoolerItem.Where(x => x.Type == "Pump" && viewPSNItem.Groups.Last().Items.Last().Equipment.Name.Contains(x.Name)).Count() > 0)
486
                                    {
487
                                        viewEGFlowDirection = "I";
488
                                    }
489
                                }
490

    
491
                                if (EGFlowDirection.Equals(viewEGFlowDirection) && !lstViewPipeSystemNetwork_OID.Contains(dr.Field<string>("ViewPipeSystemNetwork_OID")))
492
                                    lstViewPipeSystemNetwork_OID.Add(dr.Field<string>("ViewPipeSystemNetwork_OID"));
493
                            }
494
                            else
495
                            {
496
                            }
497
                        }
498
                    }
499

    
500
                    string selectViewOID = string.Empty;
501

    
502
                    if (EGFlowDirection == "O") //From 이면 시작점에서 제일 먼 값
503
                    {
504
                        foreach (string viewOID in lstViewPipeSystemNetwork_OID)
505
                        {
506
                            if (PipeSystemNetwork.Select(string.Format("PUMP = 'PUMP' AND OID = '{0}'", viewOID)).Count() > 0)
507
                            {                                
508
                                selectViewOID = pathItemRows.Where(x => x.Field<string>("ViewPipeSystemNetwork_OID") == viewOID).Last().Field<string>("OID");
509
                            }
510
                        }
511
                    }
512
                    else if (EGFlowDirection == "I") //To 이면 시작점에서 제일 가까운 값
513
                    {
514
                        foreach (string viewOID in lstViewPipeSystemNetwork_OID)
515
                        {
516
                            if (PipeSystemNetwork.Select(string.Format("PUMP = 'PUMP' AND OID = '{0}'", viewOID)).Count() > 0)
517
                            {
518
                                selectViewOID = pathItemRows.Where(x => x.Field<string>("ViewPipeSystemNetwork_OID") == viewOID).Last().Field<string>("OID");
519
                                break;
520
                            }
521
                        }
522
                    }
523

    
524
                    if (!string.IsNullOrEmpty(selectViewOID)) //selectViewOID 가 있으면
525
                    {              
526
                        string EqpGroupTag = string.Empty;                       
527

    
528
                        if((EGFlowDirection == "O" && lstViewPipeSystemNetwork_OID.Contains(pathItemRows.Last().Field<string>("ViewPipeSystemNetwork_OID"))) ||
529
                            (EGFlowDirection == "I" && lstViewPipeSystemNetwork_OID.Contains(pathItemRows.First().Field<string>("ViewPipeSystemNetwork_OID"))))
530
                        { 
531
                            DataRow[] viewpathItemRows = PathItems.Select(string.Format("PipeSystemNetwork_OID = '{0}'", pathItemRows.First().Field<string>("ViewPipeSystemNetwork_OID")));
532

    
533
                            foreach (DataRow row in viewpathItemRows)
534
                            {
535
                                if (!string.IsNullOrEmpty(row.Field<string>("EqpGroupTag")))
536
                                {
537
                                    EqpGroupTag = row.Field<string>("EqpGroupTag");
538
                                    break;
539
                                }
540
                            }
541

    
542
                            foreach (DataRow dr in pathItemRows)
543
                            {
544
                                dr["EqpGroupTag"] = EqpGroupTag;
545
                            }
546

    
547
                        }
548
                        else
549
                        {
550
                            bool bCheck = false;
551
                            if (EGFlowDirection == "I") //To 일때
552
                            {
553
                                foreach (DataRow dr in pathItemRows)
554
                                {
555
                                    if (selectViewOID == dr["OID"].ToString())
556
                                    {
557
                                        dr["EGTConnectedPoint"] = "1";
558
                                        bCheck = true;
559
                                    }
560

    
561
                                    if (!bCheck)
562
                                    {
563
                                        dr["EqpGroupTag"] = string.Empty;
564
                                        dr["MainLineTag"] = string.Empty;
565
                                        dr["EGTConnectedPoint"] = "0";
566
                                        dr["EGFlowDirection"] = string.Empty;
567
                                    }
568
                                    else
569
                                    {
570
                                        dr["MainLineTag"] = "M";
571
                                    }
572
                                    
573
                                }
574

    
575
                            }
576
                            else if (EGFlowDirection == "O") //From 일 때
577
                            {
578
                                foreach (DataRow dr in pathItemRows)
579
                                {
580
                                    if (bCheck)
581
                                    {
582
                                        
583
                                        dr["EqpGroupTag"] = string.Empty;
584
                                        dr["MainLineTag"] = string.Empty;
585
                                        dr["EGTConnectedPoint"] = "0";
586
                                        dr["EGFlowDirection"] = string.Empty;
587
                                    }
588
                                    else
589
                                    {
590
                                        dr["MainLineTag"] = "M";
591
                                    }
592

    
593
                                    if (selectViewOID == dr["OID"].ToString())
594
                                    {
595
                                        dr["EGTConnectedPoint"] = "1";
596
                                        bCheck = true;
597
                                    }
598
                                }
599
                            }
600
                        }
601
                    }
602
                    else
603
                    {
604
                        foreach (DataRow dr in pathItemRows)
605
                        {
606
                            dr["EqpGroupTag"] = string.Empty;
607
                            dr["EGFlowDirection"] = string.Empty;
608
                        }
609
                    }
610
                }
611
                                
612
                // 5번
613
                foreach (DataRow dataRow in pumpRows)
614
                {
615
                    DataRow[] pathItemRows = PathItems.Select(string.Format("PipeSystemNetwork_OID = '{0}'", dataRow["OID"].ToString()));
616

    
617
                    bool bCheck = false;
618
                    string EqpGroupTag = string.Empty;
619

    
620
                    string EGFlowDirection = string.Empty;                   
621

    
622
                    PSNItem PSNItem = PSNItems.Find(x => x.PSN_OID() == dataRow.Field<string>("OID"));
623

    
624
                    if (PSNItem.Groups.First().Items.First().Equipment != null)
625
                    {
626
                        if (EquipmentAirFinCooler.EquipmentAirFinCoolerItem.Where(x => x.Type == "Pump" && PSNItem.Groups.First().Items.First().Equipment.Name.Contains(x.Name)).Count() > 0)
627
                        {
628
                            EGFlowDirection = "O";
629
                        }
630
                    }
631
                    else if (PSNItem.Groups.Last().Items.Last().Equipment != null)
632
                    {
633
                        if (EquipmentAirFinCooler.EquipmentAirFinCoolerItem.Where(x => x.Type == "Pump" && PSNItem.Groups.Last().Items.Last().Equipment.Name.Contains(x.Name)).Count() > 0)
634
                        {
635
                            EGFlowDirection = "I";
636
                        }
637
                    }
638

    
639
                    List<string> lstViewPipeSystemNetwork_OID = new List<string>();
640
                    List<string> lstEqTagRows = new List<string>();
641
                    if (EGFlowDirection.Equals("I"))
642
                    {
643
                        foreach (DataRow dr in pathItemRows)
644
                        {                            
645
                            if (!string.IsNullOrEmpty(dr.Field<string>("MainLineTag")) && dr.Field<string>("MainLineTag").Equals("M") && !string.IsNullOrEmpty(dr.Field<string>("EqpGroupTag")))
646
                            {
647
                                bCheck = true;
648
                                EqpGroupTag = dr.Field<string>("EqpGroupTag");
649
                                if(!lstEqTagRows.Contains(EqpGroupTag))
650
                                    lstEqTagRows.Add(EqpGroupTag);
651

    
652
                                if(dataRow["OID"].ToString() != dr.Field<string>("ViewPipeSystemNetwork_OID"))
653
                                {
654
                                    PSNItem viewPSNItem = PSNItems.Find(x => x.PSN_OID() == dr.Field<string>("ViewPipeSystemNetwork_OID"));
655
                                    if (viewPSNItem.Groups.Last().Items.Last().Equipment == null)
656
                                        continue;
657

    
658
                                    if (!lstEqTagRows.Contains(viewPSNItem.Groups.Last().Items.Last().Equipment.ItemTag))
659
                                        lstEqTagRows.Add(viewPSNItem.Groups.Last().Items.Last().Equipment.ItemTag);
660
                                }
661
                                
662
                            }
663

    
664
                        }
665
                        if(bCheck)
666
                        {
667
                            foreach (DataRow row in pumpRows)
668
                            {
669
                                if (row.Field<string>("OID").Equals(dataRow["OID"].ToString()))
670
                                    continue;
671

    
672
                                PSNItem viewPSNItem = PSNItems.Find(x => x.PSN_OID() == row["OID"].ToString());
673

    
674
                                if (viewPSNItem.Groups.First().Items.First().Equipment == null)
675
                                    continue;
676
                                
677
                                if (lstEqTagRows.Contains(viewPSNItem.Groups.First().Items.First().Equipment.ItemTag) && !lstViewPipeSystemNetwork_OID.Contains(row.Field<string>("OID")))
678
                                    lstViewPipeSystemNetwork_OID.Add(row.Field<string>("OID"));
679
                            }
680

    
681
                                
682

    
683
                            if (lstViewPipeSystemNetwork_OID.Count() > 0)
684
                            {
685
                                foreach (string viewPipesystem in lstViewPipeSystemNetwork_OID)
686
                                {
687
                                    DataRow[] viewpathItemRows = PathItems.Select(string.Format("PipeSystemNetwork_OID = '{0}'", viewPipesystem));
688
                                    foreach (DataRow viewdr in viewpathItemRows)
689
                                    {
690
                                        if (!string.IsNullOrEmpty(viewdr["EqpGroupTag"].ToString()))
691
                                            viewdr["EqpGroupTag"] = EqpGroupTag;
692
                                    }
693
                                }
694
                            }
695
                        }                        
696
                    }                   
697
                }
698
                
699
                int afcTagNum = 0;
700
                DataRow[] airFinCoolerRows = PipeSystemNetwork.Select("AFC Like 'P1%'");
701
                Dictionary<string, string> keyValuePairs = new Dictionary<string, string>();
702
                foreach (DataRow dataRow in airFinCoolerRows)
703
                {
704
                    string EGFlowDirection = string.Empty;
705
                    string EGTAG = "EGTAG";
706
                    string Prefix = string.Empty;
707
                    //item.Attributes.Find(x => x.Name == valveitem.AttributeName).Value;
708
                    PSNItem PSNItem = PSNItems.Find(x => x.PSN_OID() == dataRow["OID"].ToString());
709
                    if (PSNItem.Groups.First().Items.First().Equipment != null)
710
                    {
711
                        if (EquipmentAirFinCooler.EquipmentAirFinCoolerItem.Where(x => x.Type != "Pump" && PSNItem.Groups.First().Items.First().Equipment.Name.Contains(x.Name)).Count() > 0)
712
                        {
713
                            EquipmentAirFinCoolerItem equipmentAirFinCoolerItem = EquipmentAirFinCooler.EquipmentAirFinCoolerItem.Where(x => x.Type != "Pump" && PSNItem.Groups.First().Items.First().Equipment.Name.Contains(x.Name)).First();
714
                            if(!string.IsNullOrEmpty(equipmentAirFinCoolerItem.AttributeName))
715
                                EGTAG = PSNItem.Groups.First().Items.First().Equipment.Attributes.Find(x => x.Name == equipmentAirFinCoolerItem.AttributeName) == null 
716
                                    ? string.Empty : PSNItem.Groups.First().Items.First().Equipment.Attributes.Find(x => x.Name == equipmentAirFinCoolerItem.AttributeName).Value;
717

    
718
                            Prefix = equipmentAirFinCoolerItem.TagIdentifier;
719
                            EGTAG = Prefix + "-" + EGTAG;
720
                            EGFlowDirection = "O";
721
                        }
722
                    }
723
                    else if (PSNItem.Groups.Last().Items.Last().Equipment != null)
724
                    {
725
                        if (EquipmentAirFinCooler.EquipmentAirFinCoolerItem.Where(x => x.Type != "Pump" && PSNItem.Groups.Last().Items.Last().Equipment.Name.Contains(x.Name)).Count() > 0)
726
                        {
727
                            EquipmentAirFinCoolerItem equipmentAirFinCoolerItem = EquipmentAirFinCooler.EquipmentAirFinCoolerItem.Where(x => x.Type != "Pump" && PSNItem.Groups.Last().Items.Last().Equipment.Name.Contains(x.Name)).First();
728
                            if (!string.IsNullOrEmpty(equipmentAirFinCoolerItem.AttributeName))
729
                                EGTAG = PSNItem.Groups.Last().Items.Last().Equipment.Attributes.Find(x => x.Name == equipmentAirFinCoolerItem.AttributeName) == null 
730
                                    ? string.Empty : PSNItem.Groups.Last().Items.Last().Equipment.Attributes.Find(x => x.Name == equipmentAirFinCoolerItem.AttributeName).Value;
731

    
732
                            Prefix = equipmentAirFinCoolerItem.TagIdentifier;
733
                            EGTAG = Prefix + "-" + EGTAG;
734
                            EGFlowDirection = "I";
735
                        }
736
                    }
737

    
738
                    if(!string.IsNullOrEmpty(EGFlowDirection))
739
                    {
740
                        string[] afcTag = dataRow.Field<string>("AFC").Split(new string[] { "\\" }, StringSplitOptions.None);
741

    
742
                        //Attribute가 세팅되어있지 않다면
743
                        if (EGTAG.Equals("EGTAG"))
744
                        {
745
                            if (!keyValuePairs.ContainsKey(afcTag[1]))
746
                            {
747
                                afcTagNum++;
748
                                EGTAG = Prefix + string.Format("-{0}", string.Format("{0:D5}", afcTagNum));
749
                                keyValuePairs.Add(afcTag[1], EGTAG);
750
                            }
751
                            else
752
                            {
753
                                EGTAG = keyValuePairs[afcTag[1]];
754
                            }
755
                        }
756

    
757
                        DataRow[] pathItemRows = PathItems.Select(string.Format("PipeSystemNetwork_OID = '{0}'", dataRow["OID"].ToString()));
758
                        foreach (DataRow dr in pathItemRows)
759
                        {
760
                            dr["EqpGroupTag"] = EGTAG;
761
                            dr["EGFlowDirection"] = EGFlowDirection;
762
                        }
763
                    }
764
                    
765
                }
766

    
767
                List<string> changePSN = new List<string>();
768
                foreach (DataRow dataRow in airFinCoolerRows)
769
                {
770
                    if (changePSN.Contains(dataRow["OID"].ToString()))
771
                        continue;
772

    
773
                    DataRow[] pathItemRows = PathItems.Select(string.Format("PipeSystemNetwork_OID = '{0}'", dataRow["OID"].ToString()));
774
                    string AFCTag = string.Empty;
775
                    if (pathItemRows.Where(x => !string.IsNullOrEmpty(x.Field<string>("EqpGroupTag"))).Count() > 0)
776
                        AFCTag = pathItemRows.Where(x => !string.IsNullOrEmpty(x.Field<string>("EqpGroupTag"))).First().Field<string>("EqpGroupTag");
777

    
778
                    List<string> lstViewPipeSystemNetwork_OID = pathItemRows.Select(x => x.Field<string>("ViewPipeSystemNetwork_OID")).Distinct().ToList();
779

    
780
                    if (dataRow["Type"].ToString() == "E2E")
781
                    {
782
                        foreach (DataRow dr in pathItemRows)
783
                        {
784
                            dr["MainLineTag"] = "M";
785
                        }
786
                        dataRow["AFC"] = "P3";
787
                    }
788
                    else if (dataRow["Type"].ToString() == "E2B" || dataRow["Type"].ToString() == "B2E")
789
                    {
790
                        foreach (string lstoid in lstViewPipeSystemNetwork_OID)
791
                        { 
792
                            DataRow[] viewPathItemRows = PathItems.Select(string.Format("PipeSystemNetwork_OID = '{0}'", lstoid));
793
                            List<string> lstview = viewPathItemRows.Select(x => x.Field<string>("ViewPipeSystemNetwork_OID")).Distinct().ToList();
794
                            lstview.Remove(dataRow["OID"].ToString()); // P1인 자기 자신 제거
795
                            //제거 후 다른 P2를 확인
796
                            foreach (string lstvw in lstview)
797
                            {
798
                                DataRow[] pipesn = PipeSystemNetwork.Select(string.Format("OID = '{0}' AND AFC = 'P2'", lstvw));
799
                                if(pipesn.Count() > 0)
800
                                {
801
                                    List<string> lstpipe = PathItems.Select(string.Format("PipeSystemNetwork_OID = '{0}'", lstvw)).Select(x=>x.Field<string>("ViewPipeSystemNetwork_OID")).Distinct().ToList();
802
                                    lstpipe.Remove(lstvw); //현재 자기 라인 제거
803
                                    lstpipe.Remove(dataRow["OID"].ToString()); //기준 P1 제거
804
                                    List<string> lstnewpipe = new List<string>();
805
                                    foreach (string pipe in lstpipe)
806
                                    {
807
                                        if (airFinCoolerRows.Where(x => x.Field<string>("OID").Equals(pipe)).Count() > 0) //P1이면 리스트에 추가
808
                                            lstnewpipe.Add(pipe);
809
                                    }
810

    
811
                                    if (lstnewpipe.Count() == 0)
812
                                    {
813
                                        foreach (DataRow dr in pathItemRows)
814
                                        {
815
                                            dr["MainLineTag"] = "M";
816
                                        }
817
                                        dataRow["AFC"] = "P3";
818
                                    }
819
                                    else if (lstnewpipe.Count() > 0) //P2에 기준 P1 말고 다른 P1이 있다면 P2에 ML값 AGT값 지정 후 P2에 연결된 모든 P1들의 AGT값 치환
820
                                    {
821
                                        // P2에 ML값 AGT값 지정
822
                                        DataRow[] viewpathItemRows = PathItems.Select(string.Format("PipeSystemNetwork_OID = '{0}'", lstvw));
823
                                        DataRow[] pipesystemRows = PipeSystemNetwork.Select(string.Format("OID = '{0}'", lstvw));
824
                                        foreach (DataRow viewdr in pipesystemRows)
825
                                        {
826
                                            viewdr["AFC"] = "P4";
827
                                        }
828
                                        
829
                                        foreach (DataRow viewdr in viewpathItemRows)
830
                                        {
831
                                            viewdr["EqpGroupTag"] = AFCTag;
832
                                            viewdr["MainLineTag"] = "M";
833
                                        }
834
                                        // 연결된 모든 P1들의 AGT값 치환
835
                                        foreach (string pipe in lstnewpipe)
836
                                        {                                            
837
                                            viewpathItemRows = PathItems.Select(string.Format("PipeSystemNetwork_OID = '{0}'", pipe));
838
                                            foreach (DataRow viewdr in viewpathItemRows)
839
                                            {
840
                                                viewdr["EqpGroupTag"] = AFCTag;
841
                                            }
842
                                        }
843
                                    }
844
                                }
845
                            }    
846
                        }
847
                    }
848

    
849
                }
850
                //P3을 제외한 P1
851
                airFinCoolerRows = PipeSystemNetwork.Select("AFC Like 'P1%'");
852
                foreach (DataRow dataRow in airFinCoolerRows)
853
                {
854
                    DataRow[] pathItemRows = PathItems.Select(string.Format("PipeSystemNetwork_OID = '{0}'", dataRow["OID"].ToString()));
855
                    
856
                    if (pathItemRows.Count() > 0)
857
                    {
858
                        List<string> lstViewPipeSystemNetwork_OID = pathItemRows.Select(x => x.Field<string>("ViewPipeSystemNetwork_OID")).Distinct().ToList();
859
                        List<string> lstpsn = new List<string>();
860
                        List<string> lstAll = new List<string>();
861
                        string EqpGroupTag = string.Empty;
862
                        foreach (string viewOID in lstViewPipeSystemNetwork_OID)
863
                        {
864
                            if (dataRow["OID"].ToString() == viewOID)
865
                            {
866
                                lstAll.Add(viewOID);
867
                                continue;
868
                            }
869

    
870
                            //P3이면
871
                            DataRow viewPSN = null;
872
                            if (PipeSystemNetwork.Select(string.Format("OID = '{0}' AND AFC = 'P3'", viewOID)).Count() > 0)
873
                                viewPSN = PipeSystemNetwork.Select(string.Format("OID = '{0}' AND AFC = 'P3'", viewOID)).First();
874
                            
875
                            if (viewPSN != null)
876
                            {
877
                                //P3의 AGT를 가져와 P1에 입력
878
                                if(string.IsNullOrEmpty(EqpGroupTag))
879
                                    EqpGroupTag = PathItems.Select(string.Format("PipeSystemNetwork_OID = '{0}'", viewOID)).First().Field<string>("EqpGroupTag");
880

    
881
                                foreach (DataRow dr in pathItemRows)
882
                                {
883

    
884
                                    dr["EqpGroupTag"] = EqpGroupTag;
885

    
886
                                    //P1의 AGT와 같은 모든 AGT를 P3의 AGT로 변경하기위해 
887
                                    if (!string.IsNullOrEmpty(dr.Field<string>("ViewPipeSystemNetwork_OID")) && !lstpsn.Contains(dr.Field<string>("ViewPipeSystemNetwork_OID")))
888
                                    {
889
                                        //AND MainLineTag = ''
890
                                        if (PathItems.Select(string.Format("PipeSystemNetwork_OID = '{0}' ", dr.Field<string>("ViewPipeSystemNetwork_OID"))).Count() > 0)
891
                                        {
892
                                            lstpsn.Add(dr.Field<string>("ViewPipeSystemNetwork_OID"));
893
                                            lstAll.Add(dr.Field<string>("ViewPipeSystemNetwork_OID"));
894
                                        }
895
                                    }
896
                                }
897
                            }
898
                            
899
                        }
900

    
901
                        while (lstpsn.Count() != 0)
902
                        {                            
903
                            DataRow[] rule4pathItems = PathItems.Select(string.Format("PipeSystemNetwork_OID = '{0}'", lstpsn[0]));
904
                            foreach (DataRow dr in rule4pathItems)
905
                            {
906
                                if (!string.IsNullOrEmpty(dr.Field<string>("ViewPipeSystemNetwork_OID")) && !lstAll.Contains(dr.Field<string>("ViewPipeSystemNetwork_OID")))
907
                                {
908
                                    DataRow viewPSN = null;
909

    
910
                                    if (airFinCoolerRows.Where(x => x.Field<string>("OID") == dr.Field<string>("ViewPipeSystemNetwork_OID")).Count() > 0)
911
                                        viewPSN = airFinCoolerRows.Where(x => x.Field<string>("OID") == dr.Field<string>("ViewPipeSystemNetwork_OID")).First();
912

    
913
                                    //DataRow[] pathItemviewRows = PathItems.Select(string.Format("PipeSystemNetwork_OID = '{0}'", dr.Field<string>("ViewPipeSystemNetwork_OID")));
914
                                    //if (pathItemviewRows.Select(x => x.Field<string>("MainLineTag") == "M").Count() > 0)
915
                                    //{
916
                                    //}
917
                                    if (viewPSN != null)
918
                                    {
919
                                        lstpsn.Add(dr.Field<string>("ViewPipeSystemNetwork_OID"));
920
                                        lstAll.Add(dr.Field<string>("ViewPipeSystemNetwork_OID"));
921
                                    }
922
                                }
923

    
924
                                
925

    
926
                                dr["EqpGroupTag"] = EqpGroupTag;
927

    
928
                            }
929
                            lstpsn.Remove(lstpsn[0]);
930
                        }
931
                    }
932
                    
933
                }
934

    
935
                foreach(DataRow dr in PipeSystemNetwork.Rows)
936
                {
937
                    DataRow[] pathItemRows = PathItems.Select(string.Format("PipeSystemNetwork_OID = '{0}' AND MainLineTag = 'M'", dr["OID"].ToString()));
938
                    if(pathItemRows.Count() > 0)
939
                    {
940
                        if(dr["Type"].ToString() == "HD2")
941
                        {
942
                            List<string> lstViewPipeSystemNetwork_OID = pathItemRows.Select(x => x.Field<string>("ViewPipeSystemNetwork_OID")).Distinct().ToList();
943
                            foreach(string viewpsn in lstViewPipeSystemNetwork_OID)
944
                            {                              
945
                                if (PipeSystemNetwork.Select(string.Format("OID = '{0}' AND AFC = 'P2'", viewpsn)).Count() > 0)
946
                                {
947
                                    foreach(DataRow dataRow in pathItemRows.Where(x => x.Field<string>("ViewPipeSystemNetwork_OID") == viewpsn))
948
                                    {
949
                                        dataRow["EGTConnectedPoint"] = "1";
950
                                    }
951
                                }
952
                            }
953
                        }
954
                        else if(dr["Type"].ToString() == "E2B" || dr["Type"].ToString() == "B2E" || dr["Type"].ToString() == "E2E")
955
                        {
956
                            List<string> lstViewPipeSystemNetwork_OID = pathItemRows.Select(x => x.Field<string>("ViewPipeSystemNetwork_OID")).Distinct().ToList();
957
                            string lastP1 = string.Empty;
958
                            foreach (string viewpsn in lstViewPipeSystemNetwork_OID)
959
                            {
960
                                if (viewpsn == dr["OID"].ToString())
961
                                    continue;
962

    
963
                                if (PipeSystemNetwork.Select(string.Format("OID = '{0}' AND AFC Like 'P1%'", viewpsn)).Length == 0)
964
                                    continue;
965

    
966
                                DataRow rows = PipeSystemNetwork.Select(string.Format("OID = '{0}' AND AFC Like 'P1%'", viewpsn)).First();
967
                                if(rows != null)
968
                                {
969
                                    lastP1 = viewpsn;
970
                                }
971
                            }
972

    
973
                            if(!string.IsNullOrEmpty(lastP1))
974
                            {
975
                                bool bCheck = false;
976
                                foreach (DataRow dataRow in pathItemRows)
977
                                {
978
                                    if (bCheck)
979
                                        dataRow["EqpGroupTag"] = string.Empty;
980

    
981
                                    if (dataRow.Field<string>("ViewPipeSystemNetwork_OID").Equals(lastP1))
982
                                    {
983
                                        bCheck = true;
984
                                        dataRow["EGTConnectedPoint"] = 1;
985
                                    }                       
986
                                }
987
                            }
988
                        }
989
                    }
990
                }
991

    
992
                //psn data 정리
993
                foreach(DataRow pipesystem in PipeSystemNetwork.Rows)
994
                {
995
                    DataRow[] pathItemRows = PathItems.Select(string.Format("PipeSystemNetwork_OID = '{0}'", pipesystem["OID"].ToString()));
996
                    string EGTag = string.Empty;
997
                    string HasMLTags = "False";
998

    
999
                    foreach (DataRow dataRow in pathItemRows)
1000
                    {
1001
                        if (string.IsNullOrEmpty(EGTag) && !string.IsNullOrEmpty(dataRow.Field<string>("EqpGroupTag")))
1002
                            EGTag = dataRow.Field<string>("EqpGroupTag");
1003

    
1004
                        if (HasMLTags.Equals("False") && !string.IsNullOrEmpty(dataRow.Field<string>("MainLineTag")) && dataRow.Field<string>("MainLineTag").Equals("M"))
1005
                            HasMLTags = "True";
1006

    
1007
                        if (!string.IsNullOrEmpty(EGTag) && HasMLTags == "True")
1008
                            break;
1009
                    }
1010

    
1011
                    pipesystem["EGTag"] = EGTag;
1012
                    pipesystem["HasMLTags"] = HasMLTags;
1013
                }
1014
            }
1015
            catch (Exception ex)
1016
            {
1017
                MessageBox.Show(ex.Message, "ID2 ", MessageBoxButtons.OK, MessageBoxIcon.Error);
1018
            }
1019
            //}
1020
        }
1021

    
1022
        private void UpdateValveGrouping()
1023
        {
1024
            try
1025
            {
1026
                #region ValveGrouping Info
1027
                ValveGroupInfo ValveGrouping = new ValveGroupInfo();
1028
                DataTable dtValveGroupung = DB.SelectValveGroupItemsSetting();
1029
                foreach (DataRow row in dtValveGroupung.Rows)
1030
                {
1031
                    ValveGrouping.ValveGroupItems.Add(new ValveGroupItem()
1032
                    {
1033
                        OID = row["OID"].ToString(),
1034
                        GroupType = row["GroupType"].ToString(),
1035
                        TagIdentifier = row["TagIdentifier"].ToString(),
1036
                        AttributeName = row["AttributeName"].ToString(),
1037
                        SppidSymbolName = row["SppidSymbolName"].ToString()
1038
                    });
1039
                }
1040
                #endregion
1041

    
1042

    
1043
                int vgTagNum = 1;
1044
                DataRow[] tagpathItemRows = PathItems.Select(string.Format("GROUPTAG Like '%\\%'"));
1045
                foreach (DataRow drPathitem in tagpathItemRows)
1046
                {
1047
                    string[] valvetag = drPathitem["GROUPTAG"].ToString().Split(new string[] { "\\" }, StringSplitOptions.None);
1048
                    DataRow[] pathItemRows = PathItems.Select(string.Format("PipeSystemNetwork_OID = '{0}'", drPathitem["PipeSystemNetwork_OID"].ToString()));
1049
                    ValveGroupItem valveitem = ValveGrouping.ValveGroupItems.Where(x => x.SppidSymbolName == valvetag[0]).FirstOrDefault();
1050
                    if (valveitem == null || valveitem.GroupType == "Scope Break")
1051
                        continue;
1052
                    Dictionary<int, List<DataRow>> keyValuePairs = new Dictionary<int, List<DataRow>>();
1053
                    List<Item> valveGroupingItem = new List<Item>();
1054
                    int bCnt = 0;
1055

    
1056
                    
1057
                    List<DataRow> lstitem = new List<DataRow>();
1058
                    foreach (DataRow dr in pathItemRows)
1059
                    {
1060
                        if (!string.IsNullOrEmpty(dr["BranchTopologySet_OID"].ToString()))
1061
                        {
1062
                            DataRow[] rows = TopologySet.Select(string.Format("OID = '{0}'", dr["BranchTopologySet_OID"].ToString()));
1063
                            if (dr["GROUPTAG"].ToString() == "Scope Break")
1064
                            {
1065
                                dr["GROUPTAG"] = string.Empty;
1066
                                keyValuePairs.Add(bCnt, lstitem.ToList());
1067
                                bCnt++;
1068
                                lstitem.Clear();
1069
                            }
1070
                            else
1071
                            {
1072
                                if (rows.First()["SubType"].ToString() != "Bypass" && rows.First()["SubType"].ToString() != "Vent_Drain")
1073
                                {
1074
                                    if (lstitem.ToList().Where(x => !string.IsNullOrEmpty(x.Field<string>("GROUPTAG"))).Count() > 0)
1075
                                    {
1076
                                        lstitem.Add(dr);
1077
                                        keyValuePairs.Add(bCnt, lstitem.ToList());
1078
                                        lstitem.Clear();
1079
                                    }
1080
                                    else
1081
                                    {
1082
                                        keyValuePairs.Add(bCnt, lstitem.ToList());
1083
                                        lstitem.Clear();
1084
                                        lstitem.Add(dr);
1085
                                    }
1086
                                    bCnt++;
1087

    
1088
                                }
1089
                                else
1090
                                    lstitem.Add(dr);
1091
                            }
1092
                        }
1093
                        else
1094
                            lstitem.Add(dr);
1095
                    }
1096

    
1097
                    if (lstitem.Count > 0)
1098
                    {
1099
                        keyValuePairs.Add(bCnt, lstitem);
1100
                    }
1101

    
1102
                    if (keyValuePairs.Count() == 0)
1103
                        continue;
1104

    
1105
                    string VGTag = string.Empty;
1106
                    if (valveitem.AttributeName == "NoSelection" || valveitem.AttributeName == string.Empty)
1107
                    {
1108
                        VGTag = valveitem.TagIdentifier + string.Format("-{0}", string.Format("{0:D5}", vgTagNum));
1109
                        vgTagNum++;
1110
                    }
1111
                    else
1112
                    {
1113
                        //if(string.IsNullOrEmpty(valvetag[1]))
1114
                        //    VGTag = valveitem.TagIdentifier + string.Format("-{0}", string.Format("{0:D5}", vgTagNum));
1115
                        //else
1116
                        VGTag = valveitem.TagIdentifier + "-" + valvetag[1];
1117
                    }
1118

    
1119
                    foreach (KeyValuePair<int, List<DataRow>> keyValuePair in keyValuePairs)
1120
                    {
1121
                        if (keyValuePair.Value.Where(x => x.Field<string>("OID") == drPathitem.Field<string>("OID")).Count() > 0)
1122
                        {
1123
                            foreach (DataRow dr in keyValuePair.Value)
1124
                            {
1125
                                dr["GROUPTAG"] = VGTag;
1126

    
1127
                                if(!string.IsNullOrEmpty(dr["BranchTopologySet_OID"].ToString()))
1128
                                {
1129
                                    if(TopologySet.Select(string.Format("OID = '{0}'", dr["BranchTopologySet_OID"].ToString())).First().Field<string>("SubType") == "Bypass")
1130
                                    {
1131
                                        DataRow[] rows = keyValuePair.Value.Where(x => x.Field<string>("ViewPipeSystemNetwork_OID").Equals(dr["ViewPipeSystemNetwork_OID"].ToString())).ToArray();
1132
                                        foreach(DataRow path in rows)
1133
                                        {
1134
                                            DataRow topology = TopologySet.Select(string.Format("OID = '{0}'", path["BranchTopologySet_OID"].ToString())).First();
1135
                                            if (topology.Field<string>("SubType").Equals("Bypass"))
1136
                                            {
1137
                                                DataRow[] drPathItem = PathItems.Select(string.Format("TopologySet_OID = '{0}'", topology.Field<string>("OID")));
1138
                                                foreach(DataRow pathitem in drPathItem)
1139
                                                {
1140
                                                    pathitem["GROUPTAG"] = VGTag;
1141
                                                }
1142
                                            }
1143
                                        }
1144
                                    }
1145
                                }
1146
                            }
1147
                        }
1148
                    }
1149
                    if(valveitem.GroupType.Contains("PSV"))
1150
                    {
1151
                        DataRow[] psnRows = PipeSystemNetwork.Select(string.Format("OID = '{0}'", drPathitem["PipeSystemNetwork_OID"].ToString()));
1152
                        foreach (DataRow row in psnRows)
1153
                            row["Pocket"] = "Yes";
1154
                    }                    
1155
                }
1156
            }
1157
            catch (Exception ex)
1158
            {
1159
                MessageBox.Show(ex.Message, "ID2 ", MessageBoxButtons.OK, MessageBoxIcon.Error);
1160
            }
1161
        }
1162

    
1163
        private void SetTopologyData()
1164
        {
1165
            // 13번 excel
1166
            foreach (Group group in groups)
1167
            {
1168
                LineNumber prevLineNumber = null;
1169
                for (int i = 0; i < group.Items.Count; i++)
1170
                {
1171
                    Item item = group.Items[i];
1172
                    LineNumber lineNumber = item.Document.LineNumbers.Find(x => x.UID == item.Owner);
1173
                    if (lineNumber == null)
1174
                    {
1175
                        if (prevLineNumber != null)
1176
                        {
1177
                            if (!prevLineNumber.IsCopy)
1178
                            {
1179
                                prevLineNumber = prevLineNumber.Copy();
1180
                                item.Document.LineNumbers.Add(prevLineNumber);
1181
                                item.MissingLineNumber1 = true;
1182
                            }
1183
                            item.Owner = prevLineNumber.UID;
1184
                        }
1185
                    }
1186
                    else
1187
                        prevLineNumber = lineNumber;
1188
                }
1189

    
1190
                prevLineNumber = null;
1191
                for (int i = group.Items.Count - 1; i > -1; i--)
1192
                {
1193
                    Item item = group.Items[i];
1194
                    LineNumber lineNumber = item.Document.LineNumbers.Find(x => x.UID == item.Owner);
1195
                    if (lineNumber == null)
1196
                    {
1197
                        if (prevLineNumber != null)
1198
                        {
1199
                            if (!prevLineNumber.IsCopy)
1200
                            {
1201
                                prevLineNumber = prevLineNumber.Copy();
1202
                                item.Document.LineNumbers.Add(prevLineNumber);
1203
                                item.MissingLineNumber1 = true;
1204
                            }
1205

    
1206
                            item.Owner = prevLineNumber.UID;
1207
                        }
1208
                    }
1209
                    else
1210
                        prevLineNumber = lineNumber;
1211
                }
1212

    
1213
                if (prevLineNumber == null)
1214
                {
1215
                    List<LineNumber> lineNumbers = group.Document.LineNumbers.FindAll(x => !x.IsCopy);
1216
                    Random random = new Random();
1217
                    int index = random.Next(lineNumbers.Count - 1);
1218
                   
1219
                    // Copy
1220
                    LineNumber cLineNumber = lineNumbers[index].Copy();
1221
                    group.Document.LineNumbers.Add(cLineNumber);
1222

    
1223
                    foreach (Item item in group.Items)
1224
                    {
1225
                        item.Owner = cLineNumber.UID;
1226
                        item.MissingLineNumber2 = true;
1227
                    }
1228
                }
1229
            }
1230

    
1231
            foreach (Document document in Documents)
1232
            {
1233
                foreach (Item item in document.Items)
1234
                {
1235
                    item.TopologyData = string.Empty;
1236
                    item.PSNPipeLineID = string.Empty;
1237
                    List<string> pipeLineID = new List<string>();
1238
                    LineNumber lineNumber = document.LineNumbers.Find(x => x.UID == item.Owner);
1239
                    if (lineNumber != null)
1240
                    {
1241
                        item.LineNumber = lineNumber;
1242

    
1243
                        foreach (DataRow row in topologyRuleDT.Rows)
1244
                        {
1245
                            string uid = row["UID"].ToString();
1246
                            //if (uid == "-")
1247
                            //    pipeLineID.Add(item.TopologyData);//item.TopologyData += "-"; 
1248
                            if (uid != "-")
1249
                            {
1250
                                Attribute itemAttr = item.Attributes.Find(x => x.Name == uid);
1251

    
1252
                                Attribute attribute = lineNumber.Attributes.Find(x => x.DisplayName == uid);
1253
                                if (attribute != null && !string.IsNullOrEmpty(attribute.Value))
1254
                                    pipeLineID.Add(attribute.Value);//item.TopologyData += attribute.Value;
1255
                            }
1256
                        }
1257

    
1258
                        if (topologyRuleDT.Select(string.Format("UID = '{0}'", "InsulationPurpose")) == null)
1259
                        {
1260
                            Attribute insulAttr = item.LineNumber.Attributes.Find(x => x.Name == "InsulationPurpose");
1261
                            if (insulAttr != null && !string.IsNullOrEmpty(insulAttr.Value))
1262
                                pipeLineID.Add(insulAttr.Value); //item.PSNPipeLineID = item.TopologyData + "-" + insulAttr.Value;
1263
                                                                 //else
1264
                                                                 //    item.PSNPipeLineID = item.TopologyData;
1265
                        }
1266

    
1267
                        item.PSNPipeLineID = string.Join("-", pipeLineID);
1268
                        item.TopologyData = string.Join("-", pipeLineID);
1269
                       
1270
                    }
1271
                    else
1272
                    {
1273
                        item.TopologyData = "Empty LineNumber";
1274
                        item.LineNumber = new LineNumber();
1275
                    }
1276
                }
1277
            }
1278

    
1279
            int emptyIndex = 1;
1280
            foreach (Group group in groups)
1281
            {
1282
                List<Item> groupItems = group.Items.FindAll(x => x.TopologyData == "Empty LineNumber");
1283
                if (groupItems.Count > 0)
1284
                {
1285
                    foreach (var item in groupItems)
1286
                        item.TopologyData += string.Format("-{0}", emptyIndex);
1287
                    emptyIndex++;
1288
                }
1289
            }
1290

    
1291
        }
1292

    
1293
        private void ConnectByOPC()
1294
        {
1295
            try
1296
            {
1297
                foreach (Group group in groups.FindAll(x => x.Items.Last().SubItemType == SubItemType.OPC))
1298
                {
1299
                    Item opcItem = group.Items.Last();
1300
                    DataRow[] fromRows = opcDT.Select(string.Format("FromOPCUID = '{0}'", opcItem.UID));
1301
                    if (fromRows.Length.Equals(1))
1302
                    {
1303
                        DataRow opcRow = fromRows.First();
1304
                        string toDrawing = opcRow["ToDrawing"].ToString();
1305
                        string toOPCUID = opcRow["ToOPCUID"].ToString();
1306

    
1307
                        Document toDocument = Documents.Find(x => x.DrawingName == toDrawing);
1308
                        if (toDocument != null)
1309
                        {
1310
                            Group toGroup = toDocument.Groups.Find(x => x.Items.Find(y => y.UID == toOPCUID) != null);
1311
                            DataRow[] toRows = opcDT.Select(string.Format("ToOPCUID = '{0}'", toGroup.Items.First().UID));
1312
                            //1대1 매칭이 아닐때 걸림 (2개 이상일 때) 2021.11.07 
1313
                            if (toRows.Length > 1)
1314
                            {
1315
                                //throw new Exception("OPC error(multi connect)");
1316
                                MessageBox.Show("OPC error(multi connect)", "ID2 " + id2Info.ProgramName, MessageBoxButtons.OK, MessageBoxIcon.Information);
1317
                                return;
1318
                            }
1319
                            group.EndGroup = toGroup;
1320
                            toGroup.StartGroup = group;
1321
                        }
1322
                    }
1323
                }
1324
            }
1325
            catch (Exception ex)
1326
            {
1327
                Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1328
                //MessageBox.Show(ex.Message, "ID2 " + id2Info.ProgramName, MessageBoxButtons.OK, MessageBoxIcon.Information);
1329
            }
1330
        }
1331

    
1332
        private void SetPSNItem()
1333
        {
1334
            Dictionary<Group, string> groupDic = new Dictionary<Group, string>();
1335
            foreach (Group group in groups)
1336
                groupDic.Add(group, Guid.NewGuid().ToString());
1337

    
1338
            foreach (Group group in groups)
1339
            {
1340
                string groupKey = groupDic[group];
1341
                if (group.StartGroup != null)
1342
                {
1343
                    string otherKey = groupDic[group.StartGroup];
1344
                    ChangeGroupID(otherKey, groupKey);
1345
                }
1346
            }
1347

    
1348
            // PSN 정리
1349
            foreach (var item in groupDic)
1350
            {
1351
                Group group = item.Key;
1352
                string uid = item.Value;
1353
                PSNItem PSNItem = PSNItems.Find(x => x.UID == uid);
1354
                if (PSNItem == null)
1355
                {
1356
                    PSNItem = new PSNItem(PSNItems.Count, Revision) { UID = uid };
1357
                    PSNItems.Add(PSNItem);
1358
                }
1359
                PSNItem.Groups.Add(group);
1360
                foreach (Item groupItem in group.Items)
1361
                    groupItem.PSNItem = PSNItem;
1362
            }
1363

    
1364
            // Sort PSN
1365
            foreach (PSNItem PSNItem in PSNItems)
1366
            {
1367
                List<Group> _groups = new List<Group>();
1368

    
1369
                Stack<Group> stacks = new Stack<Group>();
1370
                stacks.Push(PSNItem.Groups.First());
1371
                while (stacks.Count > 0)
1372
                {
1373
                    Group stack = stacks.Pop();
1374
                    if (_groups.Contains(stack))
1375
                        continue;
1376

    
1377
                    if (_groups.Count == 0)
1378
                        _groups.Add(stack);
1379
                    else
1380
                    {
1381
                        if (stack.StartGroup != null && _groups.Contains(stack.StartGroup))
1382
                        {
1383
                            int index = _groups.IndexOf(stack.StartGroup);
1384
                            _groups.Insert(index + 1, stack);
1385
                        }
1386
                        else if (stack.EndGroup != null && _groups.Contains(stack.EndGroup))
1387
                        {
1388
                            int index = _groups.IndexOf(stack.EndGroup);
1389
                            _groups.Insert(index, stack);
1390
                        }
1391
                    }
1392

    
1393
                    if (stack.StartGroup != null)
1394
                        stacks.Push(stack.StartGroup);
1395
                    if (stack.EndGroup != null)
1396
                        stacks.Push(stack.EndGroup);
1397
                }
1398

    
1399
                PSNItem.Groups.Clear();
1400
                PSNItem.Groups.AddRange(_groups);
1401
            }
1402

    
1403
            void ChangeGroupID(string from, string to)
1404
            {
1405
                if (from.Equals(to))
1406
                    return;
1407

    
1408
                List<Group> changeItems = new List<Group>();
1409
                foreach (var _item in groupDic)
1410
                    if (_item.Value.Equals(from))
1411
                        changeItems.Add(_item.Key);
1412
                foreach (var _item in changeItems)
1413
                    groupDic[_item] = to;
1414
            }
1415
        }
1416

    
1417
        private void SetPSNType()
1418
        {
1419
            foreach (PSNItem PSNItem in PSNItems)
1420
            {
1421
                Group firstGroup = PSNItem.Groups.First();
1422
                Group lastGroup = PSNItem.Groups.Last();
1423

    
1424
                Item firstItem = firstGroup.Items.First();
1425
                Item lastItem = lastGroup.Items.Last();
1426

    
1427
                PSNItem.StartType = GetPSNType(firstItem, true);
1428
                PSNItem.EndType = GetPSNType(lastItem, false);
1429

    
1430
            }
1431

    
1432
            PSNType GetPSNType(Item item, bool bFirst = true)
1433
            {
1434
                PSNType type = PSNType.None;
1435

    
1436
                if (item.ItemType == ItemType.Line)
1437
                {
1438
                    Group group = groups.Find(x => x.Items.Contains(item));
1439
                    if (bFirst && item.Relations[0].Item != null && !group.Items.Contains(item.Relations[0].Item))
1440
                    {
1441
                        Item connItem = item.Relations[0].Item;
1442
                        if (connItem.ItemType == ItemType.Line && !IsConnected(item, connItem))
1443
                            type = PSNType.Branch;
1444
                        else if (connItem.ItemType == ItemType.Symbol)
1445
                            type = PSNType.Symbol;
1446
                    }
1447
                    else if (!bFirst && item.Relations[1].Item != null && !group.Items.Contains(item.Relations[1].Item))
1448
                    {
1449
                        Item connItem = item.Relations[1].Item;
1450
                        if (connItem.ItemType == ItemType.Line && !IsConnected(item, connItem))
1451
                            type = PSNType.Branch;
1452
                        else if (connItem.ItemType == ItemType.Symbol)
1453
                            type = PSNType.Symbol;
1454
                    }
1455
                }
1456
                else if (item.ItemType == ItemType.Symbol)
1457
                {
1458
                    if (item.SubItemType == SubItemType.Nozzle)
1459
                        type = PSNType.Equipment;
1460
                    else if (item.SubItemType == SubItemType.Header)
1461
                        type = PSNType.Header;
1462
                    else if (item.SubItemType == SubItemType.OPC)
1463
                        type = PSNType.OPC;
1464
                }
1465

    
1466
                return type;
1467
            }
1468
        }
1469

    
1470
        private void SetBranchInfo()
1471
        {
1472
            foreach (Document document in Documents)
1473
            {
1474
                List<Item> lines = document.Items.FindAll(x => x.ItemType == ItemType.Line).ToList();
1475
                foreach (Item line in lines)
1476
                {
1477
                    double[] point = line.Relations[0].Point;
1478
                    List<Item> connLines = lines.FindAll(x => x.Relations.Find(y => y.UID == line.UID) != null && line.Relations.Find(y => y.UID == x.UID) == null);
1479
                    connLines.Sort(SortBranchLine);
1480
                    line.BranchItems.AddRange(connLines);
1481

    
1482
                    int SortBranchLine(Item a, Item b)
1483
                    {
1484
                        double[] pointA = a.Relations[0].UID == line.UID ? a.Relations[0].Point : a.Relations[1].Point;
1485
                        double distanceA = CalcPointToPointdDistance(point[0], point[1], pointA[0], pointA[1]);
1486

    
1487
                        double[] pointB = b.Relations[0].UID == line.UID ? b.Relations[0].Point : b.Relations[1].Point;
1488
                        double distanceB = CalcPointToPointdDistance(point[0], point[1], pointB[0], pointB[1]);
1489

    
1490
                        // 내림차순
1491
                        return distanceA.CompareTo(distanceB);
1492
                    }
1493
                    double CalcPointToPointdDistance(double x1, double y1, double x2, double y2)
1494
                    {
1495
                        return Math.Pow(Math.Pow(x1 - x2, 2) + Math.Pow(y1 - y2, 2), 0.5);
1496
                    }
1497
                }
1498
            }
1499
        }
1500

    
1501
        private void SetTopology()
1502
        {
1503
            try
1504
            {
1505
                #region 기본 topology 정리
1506
                foreach (PSNItem PSNItem in PSNItems)
1507
                {
1508
                    Topology topology = null;
1509
                    foreach (Group group in PSNItem.Groups)
1510
                    {
1511
                        foreach (Item item in group.Items)
1512
                        {
1513
                            if (string.IsNullOrEmpty(item.TopologyData))
1514
                                topology = null;
1515
                            else
1516
                            {
1517
                                if (topology == null)
1518
                                {
1519
                                    topology = new Topology()
1520
                                    {
1521
                                        ID = item.TopologyData
1522
                                    };
1523
                                    Topologies.Add(topology);
1524

    
1525
                                    if (!PSNItem.Topologies.Contains(topology))
1526
                                        PSNItem.Topologies.Add(topology);
1527
                                }
1528
                                else
1529
                                {
1530
                                    if (topology.ID != item.TopologyData)
1531
                                    {
1532
                                        topology = new Topology()
1533
                                        {
1534
                                            ID = item.TopologyData
1535
                                        };
1536
                                        Topologies.Add(topology);
1537

    
1538
                                        if (!PSNItem.Topologies.Contains(topology))
1539
                                            PSNItem.Topologies.Add(topology);
1540
                                    }
1541
                                }
1542

    
1543
                                item.Topology = topology;
1544
                                topology.Items.Add(item);
1545
                            }
1546
                        }
1547
                    }
1548
                }
1549
                #endregion
1550

    
1551
                #region Type
1552
                List<string> ids = Topologies.Select(x => x.ID).Distinct().ToList();
1553
                foreach (string id in ids)
1554
                {
1555
                    try
1556
                    {
1557

    
1558

    
1559
                        List<Topology> topologies = Topologies.FindAll(x => x.ID == id);
1560

    
1561
                        // Main
1562
                        List<Topology> mainTopologies = FindMainTopology(topologies);
1563
                        foreach (Topology topology in mainTopologies)
1564
                            topology.Type = "M";
1565

    
1566
                        // Branch
1567
                        List<Topology> branchToplogies = topologies.FindAll(x => string.IsNullOrEmpty(x.Type));
1568
                        foreach (Topology topology in branchToplogies)
1569
                            topology.Type = "B";
1570
                    }
1571
                    catch (Exception ex)
1572
                    {
1573
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1574
                        MessageBox.Show(ex.Message, "ID2 " + id2Info.ProgramName, MessageBoxButtons.OK, MessageBoxIcon.Information);
1575
                    }
1576
                }
1577
                #endregion
1578
            }
1579
            catch (Exception ex)
1580
            {
1581
                Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1582
                MessageBox.Show(ex.Message, "ID2 " + id2Info.ProgramName, MessageBoxButtons.OK, MessageBoxIcon.Information);
1583
            }
1584

    
1585
        }
1586

    
1587
        private void SetTopologyIndex()
1588
        {
1589
            List<string> ids = Topologies.Select(x => x.ID).Distinct().ToList();
1590
            foreach (string id in ids)
1591
            {
1592
                List<Topology> topologies = Topologies.FindAll(x => x.ID == id);
1593

    
1594
                // Main
1595
                List<Topology> mainTopologies = topologies.FindAll(x => x.Type == "M");
1596
                foreach (Topology topology in mainTopologies)
1597
                    topology.Index = mainTopologies.IndexOf(topology).ToString();
1598

    
1599
                // Branch
1600
                List<Topology> branchToplogies = topologies.FindAll(x => x.Type == "B");
1601
                foreach (Topology topology in branchToplogies)
1602
                    topology.Index = (branchToplogies.IndexOf(topology) + 1).ToString();
1603
            }
1604
        }
1605

    
1606
        private void SetPSNBypass()
1607
        {
1608
            foreach (PSNItem PSNItem in PSNItems)
1609
                PSNItem.IsBypass = IsBypass(PSNItem);
1610
        }
1611

    
1612
        private List<Topology> FindMainTopology(List<Topology> data)
1613
        {
1614
            DataTable nominalDiameterDT = DB.SelectNominalDiameter();
1615
            DataTable PMCDT = DB.SelectPSNPIPINGMATLCLASS();
1616
            //2021.11.17 안쓰네 JY
1617
            //DataTable fluidCodeDT = DB.SelectPSNFluidCode(); 
1618

    
1619
            List<Topology> main = new List<Topology>();
1620
            main.AddRange(data);
1621
            //
1622
            main = GetNozzleTopology(data);
1623
            if (main.Count == 1)
1624
                return main;
1625
            else
1626
            {
1627
                if (main.Count > 0)
1628
                    main = GetPMCTopology(main);
1629
                else
1630
                    main = GetPMCTopology(data);
1631

    
1632
                if (main.Count == 1)
1633
                    return main;
1634
                else
1635
                {
1636
                    if (main.Count > 0)
1637
                        main = GetDiaTopology(main);
1638
                    else
1639
                        main = GetDiaTopology(data);
1640

    
1641

    
1642
                    if (main.Count == 1)
1643
                        return main;
1644
                    else
1645
                    {
1646
                        if (main.Count > 0)
1647
                            main = GetItemTopology(main);
1648
                        else
1649
                            main = GetItemTopology(data);
1650
                    }
1651
                }
1652
            }
1653

    
1654
            List<Topology> GetNozzleTopology(List<Topology> topologies)
1655
            {
1656
                return topologies.FindAll(x => x.Items.Find(y => y.SubItemType == SubItemType.Nozzle) != null);
1657
            }
1658

    
1659
            List<Topology> GetPMCTopology(List<Topology> topologies)
1660
            {
1661
                List<Topology> result = new List<Topology>();
1662
                foreach (DataRow row in PMCDT.Rows)
1663
                {
1664
                    string value = row["CODE"].ToString();
1665
                    foreach (Topology topology in topologies)
1666
                    {
1667
                        foreach (Item item in topology.Items)
1668
                        {
1669
                            if (item.LineNumber == null)
1670
                                continue;
1671
                            Attribute attribute = item.LineNumber.Attributes.Find(x => x.Name == "PipingMaterialsClass");
1672
                            if (attribute != null && value == attribute.Value)
1673
                            {
1674
                                result.Add(topology);
1675
                                break;
1676
                            }
1677
                        }
1678
                    }
1679

    
1680
                    if (result.Count > 0)
1681
                        break;
1682
                }
1683

    
1684
                return result;
1685
            }
1686

    
1687
            List<Topology> GetDiaTopology(List<Topology> topologies)
1688
            {
1689
                List<Topology> result = new List<Topology>();
1690
                foreach (DataRow row in nominalDiameterDT.Rows)
1691
                {
1692
                    string inchValue = row["InchStr"].ToString();
1693
                    string metricValue = row["MetricStr"].ToString();
1694
                    foreach (Topology topology in topologies)
1695
                    {
1696
                        foreach (Item item in topology.Items)
1697
                        {
1698
                            if (item.LineNumber == null)
1699
                                continue;
1700
                            Attribute attribute = item.LineNumber.Attributes.Find(x => x.Name == "NominalDiameter");
1701
                            if (attribute != null && (inchValue == attribute.Value || metricValue == attribute.Value))
1702
                            {
1703
                                result.Add(topology);
1704
                                break;
1705
                            }
1706
                        }
1707
                    }
1708

    
1709
                    if (result.Count > 0)
1710
                        break;
1711
                }
1712

    
1713
                return result;
1714
            }
1715

    
1716
            List<Topology> GetItemTopology(List<Topology> topologies)
1717
            {
1718
                return new List<Topology>() { topologies.OrderByDescending(x => x.Items.Count).ToList().First() };
1719
            }
1720

    
1721
            return main;
1722
        }
1723

    
1724
        private DataTable GetOPCInfo()
1725
        {
1726
            DataTable opc = DB.SelectOPCRelations();
1727
            DataTable drawing = DB.AllDrawings();
1728

    
1729
            DataTable dt = new DataTable();
1730
            dt.Columns.Add("FromDrawing", typeof(string));
1731
            dt.Columns.Add("FromDrawingUID", typeof(string));
1732
            dt.Columns.Add("FromOPCUID", typeof(string));
1733
            dt.Columns.Add("ToDrawing", typeof(string));
1734
            dt.Columns.Add("ToDrawingUID", typeof(string));
1735
            dt.Columns.Add("ToOPCUID", typeof(string));
1736
            foreach (DataRow row in opc.Rows)
1737
            {
1738
                string fromDrawingUID = row["From_Drawings_UID"] == null ? string.Empty : row["From_Drawings_UID"].ToString();
1739
                string fromOPCUID = row["From_OPC_UID"] == null ? string.Empty : row["From_OPC_UID"].ToString();
1740
                string toDrawingUID = row["To_Drawings_UID"] == null ? string.Empty : row["To_Drawings_UID"].ToString();
1741
                string toOPCUID = row["To_OPC_UID"] == null ? string.Empty : row["To_OPC_UID"].ToString();
1742
                if (!string.IsNullOrEmpty(toOPCUID))
1743
                {
1744
                    DataRow[] fromRows = drawing.Select(string.Format("UID = '{0}'", fromDrawingUID));
1745
                    DataRow[] toRows = drawing.Select(string.Format("UID = '{0}'", toDrawingUID));
1746
                    if (fromRows.Length.Equals(1) && toRows.Length.Equals(1))
1747
                    {
1748
                        string fromDrawingName = Path.GetFileNameWithoutExtension(fromRows.First()["NAME"].ToString());
1749
                        string toDrawingName = Path.GetFileNameWithoutExtension(toRows.First()["NAME"].ToString());
1750

    
1751
                        DataRow newRow = dt.NewRow();
1752
                        newRow["FromDrawing"] = fromDrawingName;
1753
                        newRow["FromDrawingUID"] = fromDrawingUID;
1754
                        newRow["FromOPCUID"] = fromOPCUID;
1755
                        newRow["ToDrawing"] = toDrawingName;
1756
                        newRow["ToDrawingUID"] = toDrawingUID;
1757
                        newRow["ToOPCUID"] = toOPCUID;
1758

    
1759
                        dt.Rows.Add(newRow);
1760
                    }
1761
                }
1762
            }
1763

    
1764
            return dt;
1765
        }
1766

    
1767
        private DataTable GetTopologyRule()
1768
        {
1769
            DataTable dt = DB.SelectTopologyRule();
1770

    
1771
            return dt;
1772
        }
1773

    
1774
        private bool IsConnected(Item item1, Item item2)
1775
        {
1776
            if (item1.Relations.Find(x => x.UID.Equals(item2.UID)) != null &&
1777
                item2.Relations.Find(x => x.UID.Equals(item1.UID)) != null)
1778
                return true;
1779
            else
1780
                return false;
1781
        }
1782

    
1783
        private void SaveNozzleAndEquipment()
1784
        {
1785
            List<Item> nozzles = new List<Item>();
1786
            List<Equipment> equipments = new List<Equipment>();
1787
            foreach (Document document in Documents)
1788
            {
1789
                nozzles.AddRange(document.Items.FindAll(x => x.SubItemType == SubItemType.Nozzle));
1790
                equipments.AddRange(document.Equipments);
1791
            }
1792

    
1793

    
1794
            DataTable nozzleDT = new DataTable();
1795
            nozzleDT.Columns.Add("OID", typeof(string));
1796
            nozzleDT.Columns.Add("ITEMTAG", typeof(string));
1797
            nozzleDT.Columns.Add("XCOORDS", typeof(string));
1798
            nozzleDT.Columns.Add("YCOORDS", typeof(string));
1799
            nozzleDT.Columns.Add("Equipment_OID", typeof(string));
1800
            nozzleDT.Columns.Add("FLUID", typeof(string));
1801
            nozzleDT.Columns.Add("NPD", typeof(string));
1802
            nozzleDT.Columns.Add("PMC", typeof(string));
1803
            nozzleDT.Columns.Add("ROTATION", typeof(string));
1804
            nozzleDT.Columns.Add("FlowDirection", typeof(string));
1805

    
1806
            foreach (Item item in nozzles)
1807
            {
1808
                DataRow row = nozzleDT.NewRow();
1809
                row["OID"] = item.UID;
1810

    
1811
                Relation relation = item.Relations.Find(x => equipments.Find(y => y.UID == x.UID) != null);
1812
                if (relation != null)
1813
                {
1814
                    Equipment equipment = equipments.Find(x => x.UID == relation.UID);
1815
                    equipment.Nozzles.Add(item);
1816
                    row["ITEMTAG"] = string.Format("N{0}", string.Format("{0:D3}", equipment.Nozzles.Count + 100));
1817
                    row["Equipment_OID"] = equipment.UID;
1818
                    item.Equipment = equipment;
1819
                }
1820
                row["XCOORDS"] = (item.POINT[0] / DrawingWidth).ToString();
1821
                row["YCOORDS"] = (item.POINT[1] / DrawingHeight).ToString();
1822
                Attribute fluidAttr = item.LineNumber.Attributes.Find(x => x.Name == "FluidCode");
1823
                row["FLUID"] = fluidAttr != null ? fluidAttr.Value : string.Empty;
1824
                Attribute npdAttr = item.LineNumber.Attributes.Find(x => x.Name == "NominalDiameter");
1825
                row["NPD"] = npdAttr != null ? npdAttr.Value : string.Empty;
1826
                Attribute pmcAttr = item.LineNumber.Attributes.Find(x => x.Name == "PipingMaterialsClass");
1827
                row["PMC"] = pmcAttr != null ? pmcAttr.Value : string.Empty;
1828

    
1829
                double angle = Math.PI * 2 - Convert.ToDouble(item.ANGLE);
1830
                if (angle >= Math.PI * 2)
1831
                    angle = angle - Math.PI * 2;
1832
                row["ROTATION"] = angle.ToString();
1833

    
1834
                if (item.Topology.Items.First().Equals(item))
1835
                    row["FlowDirection"] = "Outlet";
1836
                else if (item.Topology.Items.Last().Equals(item))
1837
                    row["FlowDirection"] = "Inlet";
1838
                else
1839
                    row["FlowDirection"] = string.Empty;
1840

    
1841
                nozzleDT.Rows.Add(row);
1842
            }
1843

    
1844
            DataTable equipDT = new DataTable();
1845
            equipDT.Columns.Add("OID", typeof(string));
1846
            equipDT.Columns.Add("ITEMTAG", typeof(string));
1847
            equipDT.Columns.Add("XCOORDS", typeof(string));
1848
            equipDT.Columns.Add("YCOORDS", typeof(string));
1849

    
1850
            foreach (Equipment equipment in equipments)
1851
            {
1852
                DataRow row = equipDT.NewRow();
1853
                row["OID"] = equipment.UID;
1854
                if (!string.IsNullOrEmpty(EquipTagNoAttributeName))
1855
                {
1856
                    Attribute attribute = equipment.Attributes.Find(x => x.Name == EquipTagNoAttributeName);
1857
                    if (attribute != null)
1858
                        equipment.ItemTag = attribute.Value;
1859
                }
1860
                else
1861
                    equipment.ItemTag = equipment.Name;
1862

    
1863
                row["ITEMTAG"] = equipment.ItemTag;
1864
                List<double> xList = equipment.POINT.Select(x => x[0]).ToList();
1865
                row["XCOORDS"] = (xList.Sum() / (double)xList.Count) / DrawingWidth;
1866

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

    
1870
                equipDT.Rows.Add(row);
1871
            }
1872

    
1873
            Equipment = equipDT;
1874
            Nozzle = nozzleDT;
1875
        }
1876

    
1877
        private void SavePSNData()
1878
        {
1879
            try
1880
            {
1881
                DataTable pathItemsDT = new DataTable();
1882
                pathItemsDT.Columns.Add("OID", typeof(string));
1883
                pathItemsDT.Columns.Add("SequenceData_OID", typeof(string));
1884
                pathItemsDT.Columns.Add("TopologySet_OID", typeof(string));
1885
                pathItemsDT.Columns.Add("BranchTopologySet_OID", typeof(string));
1886
                pathItemsDT.Columns.Add("PipeLine_OID", typeof(string));
1887
                pathItemsDT.Columns.Add("ITEMNAME", typeof(string));
1888
                pathItemsDT.Columns.Add("ITEMTAG", typeof(string));
1889
                pathItemsDT.Columns.Add("DESCRIPTION", typeof(string));
1890
                pathItemsDT.Columns.Add("Class", typeof(string));
1891
                pathItemsDT.Columns.Add("SubClass", typeof(string));
1892
                pathItemsDT.Columns.Add("TYPE", typeof(string));
1893
                pathItemsDT.Columns.Add("PIDNAME", typeof(string));
1894
                pathItemsDT.Columns.Add("Equipment_OID", typeof(string));
1895
                pathItemsDT.Columns.Add("NPD", typeof(string));
1896
                pathItemsDT.Columns.Add("GROUPTAG", typeof(string));
1897
                pathItemsDT.Columns.Add("PipeSystemNetwork_OID", typeof(string));
1898
                pathItemsDT.Columns.Add("ViewPipeSystemNetwork_OID", typeof(string));
1899
                pathItemsDT.Columns.Add("PipeRun_OID", typeof(string));
1900
                pathItemsDT.Columns.Add("EqpGroupTag", typeof(string));
1901
                pathItemsDT.Columns.Add("MainLineTag", typeof(string));
1902
                pathItemsDT.Columns.Add("EGTConnectedPoint", typeof(string));
1903
                pathItemsDT.Columns.Add("EGFlowDirection", typeof(string));
1904

    
1905
                DataTable sequenceDataDT = new DataTable();
1906
                sequenceDataDT.Columns.Add("OID", typeof(string));
1907
                sequenceDataDT.Columns.Add("SERIALNUMBER", typeof(string));
1908
                sequenceDataDT.Columns.Add("PathItem_OID", typeof(string));
1909
                sequenceDataDT.Columns.Add("TopologySet_OID_Key", typeof(string));
1910

    
1911
                DataTable pipeSystemNetworkDT = new DataTable();
1912
                pipeSystemNetworkDT.Columns.Add("OID", typeof(string));
1913
                pipeSystemNetworkDT.Columns.Add("Type", typeof(string));
1914
                pipeSystemNetworkDT.Columns.Add("OrderNumber", typeof(string));
1915
                pipeSystemNetworkDT.Columns.Add("Pipeline_OID", typeof(string));
1916
                pipeSystemNetworkDT.Columns.Add("FROM_DATA", typeof(string));
1917
                pipeSystemNetworkDT.Columns.Add("TO_DATA", typeof(string));
1918
                pipeSystemNetworkDT.Columns.Add("TopologySet_OID_Key", typeof(string));
1919
                pipeSystemNetworkDT.Columns.Add("PSNRevisionNumber", typeof(string));
1920
                pipeSystemNetworkDT.Columns.Add("PBS", typeof(string));
1921
                pipeSystemNetworkDT.Columns.Add("Drawings", typeof(string));
1922
                pipeSystemNetworkDT.Columns.Add("IsValid", typeof(string));
1923
                pipeSystemNetworkDT.Columns.Add("Status", typeof(string));
1924
                pipeSystemNetworkDT.Columns.Add("IncludingVirtualData", typeof(string));
1925
                pipeSystemNetworkDT.Columns.Add("PSNAccuracy", typeof(string));
1926
                pipeSystemNetworkDT.Columns.Add("Pocket", typeof(string));
1927
                pipeSystemNetworkDT.Columns.Add("EGTag", typeof(string));
1928
                pipeSystemNetworkDT.Columns.Add("HasMLTags", typeof(string));
1929
                pipeSystemNetworkDT.Columns.Add("AFC", typeof(string));
1930
                pipeSystemNetworkDT.Columns.Add("PUMP", typeof(string));
1931

    
1932
                DataTable topologySetDT = new DataTable();
1933
                topologySetDT.Columns.Add("OID", typeof(string));
1934
                topologySetDT.Columns.Add("Type", typeof(string));
1935
                topologySetDT.Columns.Add("SubType", typeof(string));
1936
                topologySetDT.Columns.Add("HeadItemTag", typeof(string));
1937
                topologySetDT.Columns.Add("TailItemTag", typeof(string));
1938
                topologySetDT.Columns.Add("HeadItemSPID", typeof(string));
1939
                topologySetDT.Columns.Add("TailItemSPID", typeof(string));
1940

    
1941
                DataTable pipelineDT = new DataTable();
1942
                pipelineDT.Columns.Add("OID", typeof(string));
1943
                pipelineDT.Columns.Add("PipeSystem_OID", typeof(string));
1944
                pipelineDT.Columns.Add("FLUID", typeof(string));
1945
                pipelineDT.Columns.Add("PMC", typeof(string));
1946
                pipelineDT.Columns.Add("SEQNUMBER", typeof(string));
1947
                pipelineDT.Columns.Add("INSULATION", typeof(string));
1948
                pipelineDT.Columns.Add("FROM_DATA", typeof(string));
1949
                pipelineDT.Columns.Add("TO_DATA", typeof(string));
1950
                pipelineDT.Columns.Add("Unit", typeof(string));
1951

    
1952
                DataTable pipesystemDT = new DataTable();
1953
                pipesystemDT.Columns.Add("OID", typeof(string));
1954
                pipesystemDT.Columns.Add("DESCRIPTION", typeof(string));
1955
                pipesystemDT.Columns.Add("FLUID", typeof(string));
1956
                pipesystemDT.Columns.Add("PMC", typeof(string));
1957
                pipesystemDT.Columns.Add("PipeLineQty", typeof(string));
1958
                pipesystemDT.Columns.Add("GroundLevel", typeof(string));
1959

    
1960
                // Set Vent/Drain Info
1961
                List<VentDrainInfo> VentDrainInfos = new List<VentDrainInfo>();
1962
                DataTable dt = DB.SelectVentDrainSetting();
1963
                foreach (DataRow row in dt.Rows)
1964
                {
1965
                    string groupID = row["GROUP_ID"].ToString();
1966
                    string desc = row["DESCRIPTION"].ToString();
1967
                    int index = Convert.ToInt32(row["INDEX"]);
1968
                    string name = row["NAME"].ToString();
1969

    
1970
                    VentDrainInfo ventDrainInfo = VentDrainInfos.Find(x => x.UID.Equals(groupID));
1971
                    if (ventDrainInfo == null)
1972
                    {
1973
                        ventDrainInfo = new VentDrainInfo(groupID);
1974
                        ventDrainInfo.Description = desc;
1975
                        VentDrainInfos.Add(ventDrainInfo);
1976
                    }
1977

    
1978
                    ventDrainInfo.VentDrainItems.Add(new VentDrainItem()
1979
                    {
1980
                        Index = index,
1981
                        Name = name
1982
                    });
1983
                }
1984
                foreach (VentDrainInfo ventDrainInfo in VentDrainInfos)
1985
                    ventDrainInfo.VentDrainItems = ventDrainInfo.VentDrainItems.OrderBy(x => x.Index).ToList();
1986

    
1987
                #region Keyword Info
1988
                KeywordInfo KeywordInfos = new KeywordInfo();
1989
                DataTable dtKeyword = DB.SelectKeywordsSetting();
1990
                foreach (DataRow row in dtKeyword.Rows)
1991
                {
1992
                    int index = Convert.ToInt32(row["INDEX"]);
1993
                    string name = row["NAME"].ToString();
1994
                    string keyword = row["KEYWORD"].ToString();
1995

    
1996
                    //KeywordInfo keywordInfo = new KeywordInfo();   
1997
                    KeywordInfos.KeywordItems.Add(new KeywordItem()
1998
                    {
1999
                        Index = index,
2000
                        Name = name,
2001
                        Keyword = keyword
2002
                    });
2003
                }
2004
                #endregion
2005

    
2006
                #region ValveGrouping Info
2007
                ValveGroupInfo ValveGrouping = new ValveGroupInfo();
2008
                DataTable dtValveGroupung = DB.SelectValveGroupItemsSetting();
2009
                foreach (DataRow row in dtValveGroupung.Rows)
2010
                {
2011
                    ValveGrouping.ValveGroupItems.Add(new ValveGroupItem()
2012
                    {
2013
                        OID = row["OID"].ToString(),
2014
                        GroupType = row["GroupType"].ToString(),
2015
                        TagIdentifier = row["TagIdentifier"].ToString(),
2016
                        AttributeName = row["AttributeName"].ToString(),
2017
                        SppidSymbolName = row["SppidSymbolName"].ToString()
2018
                    });
2019
                }
2020
                #endregion
2021

    
2022
                #region EquipmentNoPocket Info
2023
                EquipmentNoPocketInfo EquipmentNoPocket = new EquipmentNoPocketInfo();
2024
                DataTable dtEquipmentNoPocket = DB.SelectEquipmentNoPocketSetting();
2025
                foreach (DataRow row in dtEquipmentNoPocket.Rows)
2026
                {
2027
                    EquipmentNoPocket.EquipmentNoPocketItem.Add(new EquipmentNoPocketItem()
2028
                    {
2029
                        Index = Convert.ToInt32(row["INDEX"]),
2030
                        Type = row["TYPE"].ToString(),
2031
                        Name = row["NAME"].ToString()
2032
                    });
2033
                }
2034
                #endregion
2035

    
2036
                #region EquipmentAirFinCooler Info
2037
                EquipmentAirFinCoolerInfo EquipmentAirFinCooler = new EquipmentAirFinCoolerInfo();
2038
                DataTable dtEquipmentAirFinCooler = DB.SelectAirFinCoolerSetting();
2039
                foreach (DataRow row in dtEquipmentAirFinCooler.Rows)
2040
                {
2041
                    EquipmentAirFinCooler.EquipmentAirFinCoolerItem.Add(new EquipmentAirFinCoolerItem()
2042
                    {
2043
                        Type = row["Type"].ToString(),
2044
                        Name = row["Name"].ToString()
2045
                    });
2046
                }
2047
                #endregion
2048

    
2049
                // key = 미입력 branch
2050
                Dictionary<Item, Item> startBranchDic = new Dictionary<Item, Item>();
2051
                Dictionary<Item, Item> endBranchDic = new Dictionary<Item, Item>();
2052
                DataTable PSNFluidDT = DB.SelectPSNFluidCode();
2053
                DataTable PSNPMCDT = DB.SelectPSNPIPINGMATLCLASS();
2054
                foreach (PSNItem PSNItem in PSNItems)
2055
                {
2056
                    try
2057
                    {
2058
                        int psnOrder = 0;
2059
                        int index = 0;
2060
                        bool bPSNStart = true;
2061
                        string sPSNData = string.Empty;
2062
                        bool bVentDrain = false;
2063

    
2064
                        List<Group> Groups = PSNItem.Groups;
2065
                        Dictionary<string, List<Item>> keyValuePairs = new Dictionary<string, List<Item>>();
2066
                        List<Item> valveGroupingItem = new List<Item>();
2067

    
2068
                        //VentDrain 검사
2069
                        if (PSNItem.Groups.Count.Equals(1))
2070
                        {
2071
                            List<VentDrainInfo> endInfos = new List<VentDrainInfo>();
2072
                            for (int g = 0; g < Groups.Count; g++)
2073
                            {
2074
                                Group group = Groups[g];
2075
                                for (int i = 0; i < group.Items.Count; i++)
2076
                                {
2077
                                    Item item = group.Items[i];
2078
                                    foreach (VentDrainInfo ventDrainInfo in VentDrainInfos)
2079
                                    {
2080
                                        if (endInfos.Contains(ventDrainInfo))
2081
                                            continue;
2082

    
2083
                                        if (!ventDrainInfo.VentDrainItems[i].Name.Equals(item.Name))
2084
                                        {
2085
                                            endInfos.Add(ventDrainInfo);
2086
                                            continue;
2087
                                        }
2088

    
2089
                                        if (ventDrainInfo.VentDrainItems.Count.Equals(i + 1))
2090
                                        {
2091
                                            bVentDrain = true;
2092
                                            break;
2093
                                        }
2094
                                    }
2095

    
2096
                                    if (bVentDrain || endInfos.Count.Equals(VentDrainInfos.Count))
2097
                                        break;
2098
                                }
2099

    
2100
                                if (!bVentDrain)
2101
                                {
2102
                                    endInfos = new List<VentDrainInfo>();
2103
                                    for (int i = 0; i < group.Items.Count; i++)
2104
                                    {
2105
                                        Item item = group.Items[group.Items.Count - i - 1];
2106
                                        foreach (VentDrainInfo ventDrainInfo in VentDrainInfos)
2107
                                        {
2108
                                            if (endInfos.Contains(ventDrainInfo))
2109
                                                continue;
2110

    
2111
                                            if (!ventDrainInfo.VentDrainItems[i].Name.Equals(item.Name))
2112
                                            {
2113
                                                endInfos.Add(ventDrainInfo);
2114
                                                continue;
2115
                                            }
2116

    
2117
                                            if (ventDrainInfo.VentDrainItems.Count.Equals(i + 1))
2118
                                            {
2119
                                                bVentDrain = true;
2120
                                                break;
2121
                                            }
2122
                                        }
2123

    
2124
                                        if (bVentDrain || endInfos.Count.Equals(VentDrainInfos.Count))
2125
                                            break;
2126
                                    }
2127
                                }
2128
                            }
2129
                        }
2130

    
2131
                        try
2132
                        {
2133
                            foreach (Group group in PSNItem.Groups)
2134
                            {
2135
                                foreach (Item item in group.Items)
2136
                                {
2137
                                    string VgTag = string.Empty;
2138
                                    
2139
                                    if (ValveGrouping.ValveGroupItems.Where(x => x.SppidSymbolName == item.Name).Count() > 0)
2140
                                    {
2141
                                        ValveGroupItem valveitem = ValveGrouping.ValveGroupItems.Where(x => x.SppidSymbolName == item.Name).First();
2142
                                        string value = string.Empty;
2143

    
2144
                                        if (valveitem.GroupType == "Scope Break" || valveitem.AttributeName == "NoSelection" || valveitem.AttributeName == string.Empty)
2145
                                            value = "NoSelection";
2146
                                        else
2147
                                        {
2148
                                            if(item.Attributes.Find(x => x.Name == valveitem.AttributeName) == null)
2149
                                                value = "NoSelection";
2150
                                            else
2151
                                                value = item.Attributes.Find(x => x.Name == valveitem.AttributeName).Value;
2152
                                        }
2153

    
2154
                                        if (valveitem.GroupType == "Scope Break")
2155
                                            VgTag = "Scope Break";
2156
                                        else
2157
                                            VgTag = valveitem.SppidSymbolName + "\\" + value;
2158

    
2159
                                    }
2160

    
2161
                                    string PathitemUID = string.Empty;
2162
                                    if (item.BranchItems.Count == 0)
2163
                                    {
2164
                                        PathitemUID = item.UID;
2165
                                        CreatePathItemsDataRow(PathitemUID, item.ID2DBType, VgTag);
2166
                                        CreateSequenceDataDataRow(PathitemUID);
2167
                                        index++;
2168
                                    }
2169
                                    else
2170
                                    {
2171
                                        PathitemUID = item.UID + "_L1";
2172
                                        CreatePathItemsDataRow(PathitemUID, item.ID2DBType, VgTag);
2173
                                        CreateSequenceDataDataRow(PathitemUID);
2174
                                        index++;
2175
                                        for (int i = 0; i < item.BranchItems.Count; i++)
2176
                                        {
2177
                                            CreatePathItemsDataRow(string.Format(item.UID + "_B{0}", i + 1), "Branch", string.Empty, item.BranchItems[i].Topology.FullName, item.BranchItems[i]);
2178
                                            CreateSequenceDataDataRow(string.Format(item.UID + "_B{0}", i + 1));
2179
                                            index++;
2180

    
2181
                                            CreatePathItemsDataRow(string.Format(item.UID + "_L{0}", i + 2), item.ID2DBType);
2182
                                            CreateSequenceDataDataRow(string.Format(item.UID + "_L{0}", i + 2));
2183
                                            index++;
2184

    
2185
                                            if (item.BranchItems[i].Relations[0].Item != null && item.BranchItems[i].Relations[0].Item == item)
2186
                                                startBranchDic.Add(item.BranchItems[i], item);
2187
                                            else if (item.BranchItems[i].Relations[1].Item != null && item.BranchItems[i].Relations[1].Item == item)
2188
                                                endBranchDic.Add(item.BranchItems[i], item);
2189
                                        }
2190
                                    }
2191

    
2192
                                    if (bPSNStart)
2193
                                    {
2194
                                        CreatePipeSystemNetworkDataRow();
2195
                                        sPSNData = item.TopologyData;
2196
                                        psnOrder++;
2197
                                        bPSNStart = false;
2198
                                    }
2199
                                    else
2200
                                    {
2201
                                        if (item.TopologyData != sPSNData)
2202
                                        {
2203
                                            CreatePipeSystemNetworkDataRow();
2204
                                            sPSNData = item.TopologyData;
2205
                                            psnOrder++;
2206
                                        }
2207
                                    }
2208

    
2209
                                    void CreatePathItemsDataRow(string itemOID, string itemType, string GROUPTAG = "", string branchTopologyName = "", Item branchItem = null)
2210
                                    {
2211
                                        DataRow newRow = pathItemsDT.NewRow();
2212

    
2213
                                        if (itemType == "Nozzles")
2214
                                        {
2215
                                            newRow["Equipment_OID"] = item.Equipment != null ? item.Equipment.UID : "";
2216
                                        }
2217

    
2218
                                        newRow["OID"] = itemOID;
2219
                                        newRow["SequenceData_OID"] = string.Format(item.Topology.FullName + "_{0}", index);
2220
                                        newRow["TopologySet_OID"] = item.Topology.FullName;
2221
                                        newRow["BranchTopologySet_OID"] = branchTopologyName;
2222
                                        newRow["PipeLine_OID"] = item.PSNPipeLineID;
2223
                                        newRow["ITEMNAME"] = GetItemName(item, itemOID);
2224
                                        newRow["ITEMTAG"] = GetItemTag(item);
2225
                                        newRow["Class"] = GetClass(item, itemOID);
2226
                                        string subClass = GetSubClass(item, itemOID);
2227
                                        newRow["SubClass"] = subClass;
2228

    
2229
                                        if (item.ItemType == ItemType.Symbol)
2230
                                            newRow["TYPE"] = item.ID2DBName;
2231
                                        else if (item.ItemType == ItemType.Line)
2232
                                            newRow["TYPE"] = item.ID2DBType;
2233
                                        newRow["PIDNAME"] = group.Document.DrawingName;
2234

    
2235
                                        // NPD
2236
                                        if (item.LineNumber != null)
2237
                                        {
2238
                                            Attribute attribute = item.LineNumber.Attributes.Find(x => x.Name == "NominalDiameter");
2239
                                            if (attribute != null)
2240
                                                newRow["NPD"] = attribute.Value;
2241
                                        }
2242
                                        else
2243
                                            newRow["NPD"] = null;
2244

    
2245
                                        newRow["GROUPTAG"] = GROUPTAG;
2246
                                        newRow["PipeSystemNetwork_OID"] = PSNItem.PSN_OID();
2247
                                        if (branchItem == null)
2248
                                            newRow["ViewPipeSystemNetwork_OID"] = PSNItem.PSN_OID();
2249
                                        else
2250
                                            newRow["ViewPipeSystemNetwork_OID"] = branchItem.PSNItem.PSN_OID();
2251

    
2252
                                        newRow["PipeRun_OID"] = item.LineNumber != null ? item.LineNumber.Name : string.Empty;
2253
                                        newRow["EGTConnectedPoint"] = 0;
2254
                                        pathItemsDT.Rows.Add(newRow);
2255
                                    }
2256

    
2257

    
2258
                                    void CreateSequenceDataDataRow(string itemOID)
2259
                                    {
2260
                                        DataRow newRow = sequenceDataDT.NewRow();
2261
                                        newRow["OID"] = string.Format(item.Topology.FullName + "_{0}", index);
2262
                                        newRow["SERIALNUMBER"] = string.Format("{0}", index);
2263
                                        newRow["PathItem_OID"] = itemOID;
2264
                                        newRow["TopologySet_OID_Key"] = item.Topology.FullName;
2265

    
2266
                                        sequenceDataDT.Rows.Add(newRow);
2267
                                    }
2268

    
2269
                                    void CreatePipeSystemNetworkDataRow()
2270
                                    {
2271
                                        LineNumber lineNumber = item.Document.LineNumbers.Find(x => x.UID == item.Owner);
2272
                                        string FluidCode = string.Empty;
2273
                                        if (lineNumber != null)
2274
                                        {
2275
                                            List<Attribute> att = lineNumber.Attributes;
2276
                                            if (att != null)
2277
                                            {
2278
                                                List<string> oid = new List<string>();
2279
                                                FluidCode = att.Where(x => x.Name.ToUpper().Equals("FLUIDCODE")).FirstOrDefault() != null ? att.Where(x => x.Name.ToUpper().Equals("FLUIDCODE")).FirstOrDefault().Value : string.Empty;
2280
                                                string PMC = lineNumber.Attributes.Where(x => x.Name.ToUpper().Equals("PIPINGMATERIALSCLASS")).FirstOrDefault() != null ? lineNumber.Attributes.Where(x => x.Name.ToUpper().Equals("PIPINGMATERIALSCLASS")).FirstOrDefault().Value : string.Empty;
2281
                                                string SEQNUMBER = lineNumber.Attributes.Where(x => x.Name.ToUpper().Equals("TAG SEQ NO")).FirstOrDefault() != null ? lineNumber.Attributes.Where(x => x.Name.ToUpper().Equals("TAG SEQ NO")).FirstOrDefault().Value : string.Empty;
2282
                                                string INSULATION = lineNumber.Attributes.Where(x => x.Name.ToUpper().Equals("INSULATIONPURPOSE")).FirstOrDefault() != null ? lineNumber.Attributes.Where(x => x.Name.ToUpper().Equals("INSULATIONPURPOSE")).FirstOrDefault().Value : string.Empty;
2283
                                                //InsulationPurpose
2284
                                                if (!string.IsNullOrEmpty(FluidCode)) oid.Add(FluidCode);
2285
                                                if (!string.IsNullOrEmpty(PMC)) oid.Add(PMC);
2286

    
2287
                                                string PipeSystem_OID = string.Join("-", oid);
2288

    
2289
                                                if (!string.IsNullOrEmpty(SEQNUMBER)) oid.Add(SEQNUMBER);
2290
                                                if (!string.IsNullOrEmpty(INSULATION)) oid.Add(INSULATION);
2291

    
2292
                                                string OID = string.Join("-", oid);
2293
                                                string FluidCodeGL = string.Empty;
2294
                                                string PMCGL = string.Empty;
2295

    
2296

    
2297
                                                if (pipelineDT.Select(string.Format("OID = '{0}'", OID)).Count() == 0)
2298
                                                {
2299
                                                    DataRow newPipelineRow = pipelineDT.NewRow();
2300
                                                    newPipelineRow["OID"] = OID;
2301
                                                    newPipelineRow["PipeSystem_OID"] = PipeSystem_OID;
2302
                                                    newPipelineRow["FLUID"] = FluidCode;
2303
                                                    newPipelineRow["PMC"] = PMC;
2304
                                                    newPipelineRow["SEQNUMBER"] = SEQNUMBER;
2305
                                                    newPipelineRow["INSULATION"] = INSULATION;
2306
                                                    newPipelineRow["FROM_DATA"] = string.Empty;
2307
                                                    newPipelineRow["TO_DATA"] = string.Empty;
2308
                                                    newPipelineRow["Unit"] = PSNItem.GetPBSData();
2309
                                                    pipelineDT.Rows.Add(newPipelineRow);
2310
                                                }
2311

    
2312
                                                if (pipesystemDT.Select(string.Format("OID = '{0}'", PipeSystem_OID)).Count() == 0)
2313
                                                {
2314
                                                    DataRow newPipesystemRow = pipesystemDT.NewRow();
2315
                                                    newPipesystemRow["OID"] = PipeSystem_OID;
2316
                                                    newPipesystemRow["DESCRIPTION"] = string.Empty;
2317
                                                    newPipesystemRow["FLUID"] = FluidCode;
2318
                                                    newPipesystemRow["PMC"] = PMC;
2319
                                                    newPipesystemRow["PipeLineQty"] = string.Empty;
2320
                                                    string GroundLevel = string.Empty;
2321
                                                    if (!string.IsNullOrEmpty(FluidCode) && !string.IsNullOrEmpty(PMC))
2322
                                                    {
2323
                                                        FluidCodeGL = PSNFluidDT.Select(string.Format("Code = '{0}'", FluidCode)).FirstOrDefault().Field<string>("GroundLevel");
2324
                                                        PMCGL = PSNPMCDT.Select(string.Format("Code= '{0}'", PMC)).FirstOrDefault().Field<string>("GroundLevel");
2325
                                                        if (FluidCodeGL == "AG" && PMCGL == "AG")
2326
                                                            GroundLevel = "AG";
2327
                                                        else if (FluidCodeGL == "UG" && PMCGL == "UG")
2328
                                                            GroundLevel = "UG";
2329
                                                        else
2330
                                                            GroundLevel = "AG_UG";
2331
                                                    }
2332
                                                    newPipesystemRow["GroundLevel"] = GroundLevel;
2333

    
2334
                                                    pipesystemDT.Rows.Add(newPipesystemRow);
2335
                                                }
2336
                                            }
2337
                                        }
2338

    
2339
                                        DataRow newRow = pipeSystemNetworkDT.NewRow();
2340
                                        newRow["OID"] = PSNItem.PSN_OID();
2341

    
2342
                                        newRow["OrderNumber"] = psnOrder;
2343
                                        newRow["Pipeline_OID"] = item.PSNPipeLineID;
2344
                                        PSNItem.KeywordInfos = KeywordInfos;
2345
                                        PSNItem.Nozzle = Nozzle;
2346

    
2347
                                        string FromType = string.Empty;
2348
                                        Item From_item = new Item();
2349

    
2350
                                        string FROM_DATA = PSNItem.GetFromData(ref FromType, ref From_item);
2351
                                        string status = string.Empty;
2352
                                        if (psnOrder == 0)
2353
                                        {
2354
                                            status = PSNItem.Status;
2355
                                        }
2356

    
2357
                                        if (PSNItem.IsKeyword)
2358
                                        {
2359
                                            PSNItem.StartType = PSNType.Equipment;
2360
                                        }
2361
                                        string ToType = string.Empty;
2362
                                        Item To_item = new Item();
2363
                                        string TO_DATA = PSNItem.GetToData(ref ToType, ref To_item);
2364
                                        if (PSNItem.IsKeyword)
2365
                                        {
2366
                                            PSNItem.EndType = PSNType.Equipment;
2367
                                        }
2368

    
2369
                                        //if (Groups.Count == psnOrder + 1 && !string.IsNullOrEmpty(PSNItem.Status))
2370
                                        //{
2371
                                        if (!string.IsNullOrEmpty(PSNItem.Status))
2372
                                            status += PSNItem.Status;
2373
                                        //}
2374

    
2375
                                        newRow["FROM_DATA"] = FROM_DATA;
2376
                                        newRow["TO_DATA"] = TO_DATA;
2377
                                        //if(TO_DATA.Contains("Free Vent W-Screen") || TO_DATA.Contains("Empty LineNumber") || FROM_DATA.Contains("Empty LineNumber"))
2378
                                        //{
2379

    
2380
                                        //}
2381
                                        newRow["Type"] = PSNItem.GetPSNType();
2382

    
2383
                                        if (psnOrder > 0)
2384
                                        {
2385
                                            DataRow dr = pipeSystemNetworkDT.Select(string.Format("OID = '{0}' AND OrderNumber = {1}", PSNItem.PSN_OID(), psnOrder - 1)).FirstOrDefault();
2386
                                            if (!string.IsNullOrEmpty(PSNItem.Status))
2387
                                            {//status = !string.IsNullOrEmpty(status) ? status.Remove(0, 2) : string.Empty;
2388
                                                if (dr["Status"].ToString().Contains(PSNItem.Status))
2389
                                                    dr["Status"] = dr["Status"].ToString().Replace(PSNItem.Status, string.Empty);
2390
                                                else if (dr["Status"].ToString().Contains(PSNItem.Status.Remove(0, 2)))
2391
                                                    dr["Status"] = dr["Status"].ToString().Replace(PSNItem.Status.Remove(0, 2), string.Empty);
2392

    
2393
                                            }
2394

    
2395
                                            if (dr != null)
2396
                                            {
2397
                                                newRow["FROM_DATA"] = dr.Field<string>("FROM_DATA");
2398
                                                newRow["TO_DATA"] = dr.Field<string>("TO_DATA");
2399
                                                newRow["Type"] = dr.Field<string>("Type");
2400
                                            }
2401
                                        }
2402

    
2403
                                        status = !string.IsNullOrEmpty(status) ? status.Remove(0, 2) : string.Empty;
2404
                                        if (group.Items.Count == 1 && !string.IsNullOrEmpty(status))
2405
                                        {
2406
                                            MatchCollection matches = Regex.Matches(status, "Missing LineNumber_1");
2407
                                            int cnt = matches.Count;
2408
                                            if (cnt > 1)
2409
                                                status.Replace(", Missing LineNumber_1", string.Empty);
2410
                                        }
2411
                                        newRow["TopologySet_OID_Key"] = item.Topology.FullName;
2412
                                        newRow["PSNRevisionNumber"] = string.Format("V{0:D4}", Revision);
2413

    
2414

    
2415
                                        newRow["IsValid"] = PSNItem.IsValid;
2416
                                        newRow["Status"] = status;
2417
                                        newRow["PBS"] = PSNItem.GetPBSData();
2418

    
2419
                                        List<string> drawingNames = new List<string>();
2420
                                        foreach (Group _group in PSNItem.Groups)
2421
                                        {
2422
                                            if (!drawingNames.Contains(_group.Document.DrawingName))
2423
                                            {
2424
                                                if (drawingNames.Count == 0)
2425
                                                    newRow["Drawings"] = _group.Document.DrawingName;
2426
                                                else
2427
                                                    newRow["Drawings"] = newRow["Drawings"] + ", " + _group.Document.DrawingName;
2428
                                                drawingNames.Add(_group.Document.DrawingName);
2429
                                            }
2430
                                        }
2431

    
2432
                                        // VentDrain의 경우 제외 요청 (데이터를 아예 제거하였을 경우 후 가공에서 문제가 생김 마지막에 지우는것으로 변경)
2433
                                        if (bVentDrain)
2434
                                            newRow["IncludingVirtualData"] = "Vent_Drain";
2435
                                        else
2436
                                            newRow["IncludingVirtualData"] = "No";
2437
                                        //    return;
2438
                                        //newRow["IncludingVirtualData"] = "No";
2439
                                        newRow["PSNAccuracy"] = "100";
2440

    
2441
                                        string Pocket = "No";
2442
                                        string Condition = PSNFluidDT.Select(string.Format("Code = '{0}'", FluidCode)).FirstOrDefault().Field<string>("Condition");
2443
                                        if (Condition.Equals("Flare"))
2444
                                            Pocket = "Yes";
2445

    
2446
                                        if (item.ID2DBType == "Nozzles" && PSNItem.StartType == PSNType.Equipment && From_item.Equipment != null) 
2447
                                        {
2448
                                          //  string itemName = From_item.Name;
2449
                                            Equipment Equipment = From_item.Equipment;
2450
                                      
2451
                                            EquipmentNoPocketItem nopocket = EquipmentNoPocket.EquipmentNoPocketItem.Where(x => x.Name == Equipment.Name && x.Type != "Pump").FirstOrDefault();
2452
                                          
2453

    
2454
                                            if (nopocket != null)
2455
                                            {
2456
                                                DataRow bNozzle = null;
2457
                                                if (nopocket.Type.Equals("Vertical Vessel"))
2458
                                                {
2459
                                                    DataRow drNozzle = Nozzle.Select(string.Format("Equipment_OID = '{0}' AND OID = '{1}'", Equipment.UID, From_item.UID)).FirstOrDefault();
2460
                                                    if (drNozzle != null)
2461
                                                    {
2462
                                                        if (drNozzle.Field<string>("Rotation").Length >= 4 && drNozzle.Field<string>("Rotation").Substring(0, 4) == "4.71")
2463
                                                        {
2464
                                                            bNozzle = drNozzle;
2465
                                                        }
2466

    
2467
                                                        if (bNozzle == null)
2468
                                                        {
2469
                                                            DataRow[] nozzleRows = Nozzle.Select(string.Format("Equipment_OID = '{0}'", Equipment.UID));
2470

    
2471
                                                            if (drNozzle != null)
2472
                                                            {
2473
                                                                bNozzle = drNozzle;
2474
                                                                foreach (DataRow it in nozzleRows)
2475
                                                                {
2476
                                                                    if (Convert.ToDecimal(drNozzle.Field<string>("Ycoords")) > Convert.ToDecimal(it.Field<string>("Ycoords")))
2477
                                                                    {
2478
                                                                        bNozzle = null;
2479
                                                                        break;
2480
                                                                    }
2481
                                                                }
2482
                                                            }
2483
                                                        }
2484
                                                    }
2485

    
2486
                                                    if (bNozzle != null)
2487
                                                        Pocket = "Yes";
2488
                                                }
2489
                                                else
2490
                                                    Pocket = "Yes";
2491
                                            }
2492
                                        }
2493
                                        
2494
                                        if (item.ID2DBType == "Nozzles" && PSNItem.EndType == PSNType.Equipment && To_item.Equipment != null) //To는 전체
2495
                                        {
2496
                                           // string itemName = To_item.Name;
2497
                                            Equipment Equipment = To_item.Equipment;
2498
                                            EquipmentNoPocketItem nopocket = EquipmentNoPocket.EquipmentNoPocketItem.Where(x => x.Name == Equipment.Name).FirstOrDefault();
2499
                                            if (nopocket != null)
2500
                                            {
2501
                                                DataRow bNozzle = null;
2502
                                                if (nopocket.Type.Equals("Vertical Vessel"))
2503
                                                {
2504
                                                    DataRow drNozzle = Nozzle.Select(string.Format("Equipment_OID = '{0}' AND OID = '{1}'", Equipment.UID, To_item.UID)).FirstOrDefault();
2505
                                                    if(drNozzle != null)
2506
                                                    {
2507
                                                        if (drNozzle.Field<string>("Rotation").Length >= 4 && drNozzle.Field<string>("Rotation").Substring(0, 4) == "4.71")
2508
                                                        {
2509
                                                            bNozzle = drNozzle;
2510
                                                        }
2511

    
2512
                                                        if (bNozzle == null)
2513
                                                        {
2514
                                                            DataRow[] nozzleRows = Nozzle.Select(string.Format("Equipment_OID = '{0}'", Equipment.UID));
2515

    
2516
                                                            if (drNozzle != null)
2517
                                                            {
2518
                                                                bNozzle = drNozzle;
2519
                                                                foreach (DataRow it in nozzleRows)
2520
                                                                {
2521
                                                                    if (Convert.ToDecimal(drNozzle.Field<string>("Ycoords")) > Convert.ToDecimal(it.Field<string>("Ycoords")))
2522
                                                                    {
2523
                                                                        bNozzle = null;
2524
                                                                        break;
2525
                                                                    }
2526
                                                                }
2527
                                                            }
2528
                                                        }
2529
                                                    }                                                   
2530

    
2531
                                                    if (bNozzle != null)
2532
                                                        Pocket = "Yes";
2533
                                                }
2534
                                                else
2535
                                                    Pocket = "Yes";
2536
                                            }
2537
                                        }
2538
                                        
2539
                                        newRow["Pocket"] = Pocket;
2540
                                        string AFC = "P2";
2541
                                        if(PSNItem.StartType == PSNType.Equipment && From_item.Equipment != null)
2542
                                        {
2543
                                            if (EquipmentAirFinCooler.EquipmentAirFinCoolerItem.Where(x => x.Type != "Pump" && x.Name.Equals(From_item.Equipment.Name)).Count() > 0)
2544
                                                AFC = "P1\\" + From_item.Equipment.Name;
2545
                                            else if (EquipmentAirFinCooler.EquipmentAirFinCoolerItem.Where(x => x.Type == "Pump" && x.Name.Equals(From_item.Equipment.Name)).Count() > 0)
2546
                                                newRow["PUMP"] = "PUMP";
2547
                                        }
2548

    
2549
                                        if (PSNItem.EndType == PSNType.Equipment && To_item.Equipment != null)
2550
                                        {
2551
                                            if (EquipmentAirFinCooler.EquipmentAirFinCoolerItem.Where(x => x.Type != "Pump" && x.Name.Equals(To_item.Equipment.Name)).Count() > 0)
2552
                                                AFC = "P1\\" + To_item.Equipment.Name;
2553
                                            else if (EquipmentAirFinCooler.EquipmentAirFinCoolerItem.Where(x => x.Type == "Pump" && x.Name.Equals(To_item.Equipment.Name)).Count() > 0)
2554
                                                newRow["PUMP"] = "PUMP";
2555
                                        }
2556

    
2557
                                        newRow["AFC"] = AFC;
2558

    
2559
                                        newRow["EGTag"] = string.Empty;
2560
                                        newRow["HasMLTags"] = "False";
2561
                                        pipeSystemNetworkDT.Rows.Add(newRow);
2562
                                    }
2563
                                }
2564

    
2565
                            }
2566
                        
2567

    
2568
                        }
2569
                        catch (Exception ex)
2570
                        {
2571
                            Log.Write(ex.Message + "\r\n" + ex.StackTrace);
2572
                            MessageBox.Show(ex.Message, "ID2 " + id2Info.ProgramName, MessageBoxButtons.OK, MessageBoxIcon.Information);
2573
                        }
2574

    
2575
                        //TopologySet 관련
2576
                        foreach (Topology topology in PSNItem.Topologies)
2577
                        {
2578
                            DataRow newRow = topologySetDT.NewRow();
2579
                            newRow["OID"] = topology.FullName;
2580
                            newRow["Type"] = topology.FullName.Split(new char[] { '-' }).Last().StartsWith("M") ? "Main" : "Branch";
2581
                            if (bVentDrain)
2582
                                newRow["SubType"] = "Vent_Drain";
2583
                            else
2584
                                newRow["SubType"] = null;
2585
                            newRow["HeadItemTag"] = GetItemTag(topology.Items.Last());
2586
                            newRow["TailItemTag"] = GetItemTag(topology.Items.First());
2587
                            newRow["HeadItemSPID"] = topology.Items.Last().UID;
2588
                            newRow["TailItemSPID"] = topology.Items.First().UID;
2589
                            topologySetDT.Rows.Add(newRow);
2590
                        }
2591

    
2592
                    }
2593
                    catch (Exception ee)
2594
                    {
2595

    
2596
                    }
2597
                }
2598

    
2599

    
2600
                foreach (var item in startBranchDic)
2601
                {
2602
                    string uid = item.Key.UID;
2603
                    string topologyName = item.Value.Topology.FullName;
2604
                    DataRow[] rows = pathItemsDT.Select(string.Format("OID LIKE '{0}%'", uid));
2605

    
2606
                    if (rows.Length == 1)
2607
                    {
2608
                        rows.First()["BranchTopologySet_OID"] = topologyName;
2609
                        rows.First()["ViewPipeSystemNetwork_OID"] = item.Value.PSNItem.PSN_OID();
2610
                    }
2611
                    else if (rows.Length > 1)
2612
                    {
2613
                        DataRow targetRow = null;
2614
                        int index = int.MaxValue;
2615
                        foreach (DataRow row in rows)
2616
                        {
2617
                            string split = row["OID"].ToString().Split(new char[] { '_' })[1];
2618
                            if (split.StartsWith("L"))
2619
                            {
2620
                                int num = Convert.ToInt32(split.Remove(0, 1));
2621
                                if (index > num)
2622
                                {
2623
                                    index = num;
2624
                                    targetRow = row;
2625
                                }
2626
                            }
2627
                        }
2628

    
2629
                        if (targetRow != null)
2630
                        {
2631
                            targetRow["BranchTopologySet_OID"] = topologyName;
2632
                            targetRow["ViewPipeSystemNetwork_OID"] = item.Value.PSNItem.PSN_OID();
2633
                        }
2634
                    }
2635
                }
2636

    
2637
                foreach (var item in endBranchDic)
2638
                {
2639
                    string uid = item.Key.UID;
2640
                    string topologyName = item.Value.Topology.FullName;
2641
                    DataRow[] rows = pathItemsDT.Select(string.Format("OID LIKE '{0}%'", uid));
2642
                    if (rows.Length == 1)
2643
                    {
2644
                        rows.First()["BranchTopologySet_OID"] = topologyName;
2645
                        rows.First()["ViewPipeSystemNetwork_OID"] = item.Value.PSNItem.PSN_OID();
2646
                    }
2647
                    else if (rows.Length > 1)
2648
                    {
2649
                        DataRow targetRow = null;
2650
                        int index = int.MinValue;
2651
                        foreach (DataRow row in rows)
2652
                        {
2653
                            string split = row["OID"].ToString().Split(new char[] { '_' })[1];
2654
                            if (split.StartsWith("L"))
2655
                            {
2656
                                int num = Convert.ToInt32(split.Remove(0, 1));
2657
                                if (index < num)
2658
                                {
2659
                                    index = num;
2660
                                    targetRow = row;
2661
                                }
2662
                            }
2663
                        }
2664

    
2665
                        if (targetRow != null)
2666
                        {
2667
                            targetRow["BranchTopologySet_OID"] = topologyName;
2668
                            targetRow["ViewPipeSystemNetwork_OID"] = item.Value.PSNItem.PSN_OID();
2669
                        }
2670
                    }
2671
                }
2672

    
2673
                PathItems = pathItemsDT;
2674
                SequenceData = sequenceDataDT;
2675
                PipeSystemNetwork = pipeSystemNetworkDT;
2676
                TopologySet = topologySetDT;
2677
                PipeLine = pipelineDT;
2678
                PipeSystem = pipesystemDT;
2679
            }
2680
            catch (Exception ex)
2681
            {
2682
                Log.Write(ex.Message + "\r\n" + ex.StackTrace);
2683
                MessageBox.Show(ex.Message, "ID2 " + id2Info.ProgramName, MessageBoxButtons.OK, MessageBoxIcon.Information);
2684
            }
2685
        }
2686

    
2687
        private double AccuracyCalculation(List<double> lstAcc, double acc)
2688
        {
2689
            foreach (double lacc in lstAcc)
2690
            {
2691
                acc *= lacc;
2692
            }
2693
            return acc;
2694
        }
2695

    
2696
        private void UpdateSubType()
2697
        {
2698
            try
2699
            {
2700
                foreach (PSNItem PSNItem in PSNItems)
2701
                {
2702
                    if (PSNItem.IsBypass)
2703
                    {
2704
                        foreach (Topology topology in PSNItem.Topologies)
2705
                        {
2706
                            DataRow[] rows = TopologySet.Select(string.Format("OID = '{0}'", topology.FullName));
2707
                            if (rows.Length.Equals(1))
2708
                                rows.First()["SubType"] = "Bypass";
2709
                        }
2710
                    }
2711

    
2712
                    if (PSNItem.StartType == PSNType.Header)
2713
                    {
2714
                        Topology topology = PSNItem.Topologies.First();
2715
                        DataRow[] rows = TopologySet.Select(string.Format("OID = '{0}'", topology.FullName));
2716
                        if (rows.Length.Equals(1))
2717
                            rows.First()["SubType"] = "Header";
2718
                    }
2719
                    else if (PSNItem.EndType == PSNType.Header)
2720
                    {
2721
                        Topology topology = PSNItem.Topologies.Last();
2722
                        DataRow[] rows = TopologySet.Select(string.Format("OID = '{0}'", topology.FullName));
2723
                        if (rows.Length.Equals(1))
2724
                            rows.First()["SubType"] = "Header";
2725
                    }
2726
                }
2727

    
2728

    
2729
                foreach (Topology topology in Topologies)
2730
                {
2731
                    try
2732
                    {
2733
                        DataRow[] rows = TopologySet.Select(string.Format("OID = '{0}'", topology.FullName));
2734

    
2735
                        if (rows.Count() > 0)
2736
                        {
2737
                            if (rows.Length.Equals(1) && rows.First()["SubType"] == null || string.IsNullOrEmpty(rows.First()["SubType"].ToString()))
2738
                            {
2739
                                if (topology.Items == null)
2740
                                    continue;
2741

    
2742
                                Item firstItem = topology.Items.First();
2743
                                Item lastItem = topology.Items.Last();
2744

    
2745
                                List<Relation> relations = new List<Relation>();
2746

    
2747
                                if (firstItem.Relations.FindAll(x => x.Item == null).Count() != 0)
2748
                                    relations.AddRange(firstItem.Relations.FindAll(x => x.Item != null && x.Item.Topology.ID != topology.ID));
2749

    
2750
                                if (lastItem.Relations.FindAll(x => x.Item == null).Count() != 0)
2751
                                    relations.AddRange(lastItem.Relations.FindAll(x => x.Item != null && x.Item.Topology.ID != topology.ID));
2752

    
2753
                                if (relations.Count > 0)
2754
                                    rows.First()["SubType"] = "OtherSystem";
2755
                            }
2756
                        }
2757
                    }
2758
                    catch (Exception ex)
2759
                    {
2760

    
2761
                        MessageBox.Show(ex.Message, "ID2 " + id2Info.ProgramName, MessageBoxButtons.OK, MessageBoxIcon.Information);
2762
                    }
2763
                }
2764

    
2765
                foreach (DataRow row in TopologySet.Rows)
2766
                    if (row["SubType"] == null || string.IsNullOrEmpty(row["SubType"].ToString()))
2767
                        row["SubType"] = "Normal";
2768
            }
2769
            catch (Exception ex)
2770
            {
2771
                Log.Write(ex.Message + "\r\n" + ex.StackTrace);
2772
                MessageBox.Show(ex.Message, "ID2 " + id2Info.ProgramName, MessageBoxButtons.OK, MessageBoxIcon.Information);
2773
            }
2774
        }
2775

    
2776
        private bool IsBypass(PSNItem PSNItem)
2777
        {
2778
            bool bResult = false;
2779

    
2780
            if (PSNItem.GetPSNType() == "B2B")
2781
            {
2782
                Group firstGroup = PSNItem.Groups.First();
2783
                Group lastGroup = PSNItem.Groups.Last();
2784
                Item firstItem = firstGroup.Items.First();
2785
                Item lastItem = lastGroup.Items.Last();
2786

    
2787
                Item connectedFirstItem = GetConnectedItemByPSN(firstItem);
2788
                Item connectedLastItem = GetConnectedItemByPSN(lastItem);
2789

    
2790
                if (connectedFirstItem.LineNumber != null && connectedLastItem.LineNumber != null &&
2791
                    !string.IsNullOrEmpty(connectedFirstItem.LineNumber.Name) && !string.IsNullOrEmpty(connectedLastItem.LineNumber.Name) &&
2792
                    connectedFirstItem.LineNumber.Name == connectedLastItem.LineNumber.Name)
2793
                    bResult = true;
2794
                else if (connectedFirstItem.PSNItem == connectedLastItem.PSNItem)
2795
                    bResult = true;
2796
            }
2797

    
2798
            Item GetConnectedItemByPSN(Item item)
2799
            {
2800
                Item result = null;
2801

    
2802
                Relation relation = item.Relations.Find(x => x.Item != null && x.Item.PSNItem != item.PSNItem);
2803
                if (relation != null)
2804
                    result = relation.Item;
2805

    
2806

    
2807
                return result;
2808
            }
2809

    
2810
            return bResult;
2811
        }
2812

    
2813
        private void DeleteVentDrain()
2814
        {
2815
            DataRow[] ventdrainRows = PipeSystemNetwork.Select(" IncludingVirtualData = 'Vent_Drain'");
2816

    
2817
            foreach (DataRow dataRow in ventdrainRows)
2818
            {
2819
                dataRow.Delete();
2820
            }
2821
        }
2822

    
2823
        private void UpdateAccuracy()
2824
        {
2825
            //DataRow[] statusRows = PipeSystemNetwork.Select(" Type = 'Error' OR IsValid = 'Error'"); 
2826
            DataRow[] statusRows = PipeSystemNetwork.Select(" Type = 'Error' OR IsValid = 'Error'");
2827
            List<double> lstAcc = null;
2828
            string Status = string.Empty;
2829

    
2830
            foreach (DataRow dataRow in statusRows)
2831
            {
2832
                lstAcc = new List<double>();
2833
                Status = dataRow.Field<string>("Status");
2834
                if (!string.IsNullOrEmpty(Status))
2835
                {
2836
                    string[] arrStatus = Status.Split(',');
2837
                    foreach (string arrstr in arrStatus)
2838
                    {
2839
                        if (arrstr.Contains(Rule1)) //Missing LineNumber_1
2840
                            lstAcc.Add(0.75);
2841

    
2842
                        if (arrstr.Contains(Rule2)) //Missing LineNumber_2
2843
                            lstAcc.Add(0.7);
2844

    
2845
                        if (arrstr.Contains(Rule3)) // OPC Disconnected
2846
                            lstAcc.Add(0.5);
2847

    
2848
                        if (arrstr.Contains(Rule4)) //Missing ItemTag or Description
2849
                            lstAcc.Add(0.65);
2850

    
2851
                        if (arrstr.Contains(Rule5)) //Line Disconnected
2852
                            lstAcc.Add(0.6);
2853
                    }
2854
                }
2855

    
2856
                string PSNAccuracy = dataRow["PSNAccuracy"].ToString();
2857
                if (PSNAccuracy == "100")
2858
                    PSNAccuracy = "1";
2859

    
2860
                PSNAccuracy = Convert.ToString(Convert.ToDecimal(AccuracyCalculation(lstAcc, Convert.ToDouble(PSNAccuracy))));
2861
                //if (PSNAccuracy != "100")
2862
                //{
2863
                //    //dataRow["IncludingVirtualData"] = "No";
2864
                dataRow["PSNAccuracy"] = PSNAccuracy;
2865
                //}
2866
            }
2867
            DataTable dt = PipeSystemNetwork.DefaultView.ToTable(true, new string[] { "OID" });
2868
            foreach (DataRow dr in dt.Rows)
2869
            {
2870
                string oid = dr.Field<string>("OID");
2871
                DataRow[] select = PipeSystemNetwork.Select(string.Format("OID = '{0}'", oid));
2872
                double totalDdr = 0;
2873
                foreach (DataRow ddr in select)
2874
                {
2875
                    double acc = Convert.ToDouble(ddr.Field<string>("PSNAccuracy"));
2876
                    if (acc == 100) acc = 1;
2877
                    if (totalDdr == 0) totalDdr = acc;
2878
                    else totalDdr *= acc;
2879
                }
2880

    
2881
                totalDdr *= 100;
2882

    
2883
                if (totalDdr != 100)
2884
                {
2885
                    foreach (DataRow ddr in select)
2886
                    {
2887
                        ddr["IncludingVirtualData"] = "Yes";
2888
                        ddr["PSNAccuracy"] = String.Format("{0:0.00}", Math.Round(totalDdr, 2));
2889
                    }
2890
                }
2891
                else
2892
                {
2893
                    foreach (DataRow ddr in select)
2894
                    {
2895
                        ddr["IncludingVirtualData"] = "No";
2896
                        ddr["PSNAccuracy"] = String.Format("{0:0.00}", Math.Round(totalDdr, 2));
2897
                    }
2898
                }
2899
            }
2900

    
2901
        }
2902

    
2903
        private void UpdateKeywordForPSN()
2904
        {
2905
            #region Keyword Info
2906
            KeywordInfo KeywordInfos = new KeywordInfo();
2907
            DataTable dtKeyword = DB.SelectKeywordsSetting();
2908
            foreach (DataRow row in dtKeyword.Rows)
2909
            {
2910
                int index = Convert.ToInt32(row["INDEX"]);
2911
                string name = row["NAME"].ToString();
2912
                string keyword = row["KEYWORD"].ToString();
2913

    
2914
                //KeywordInfo keywordInfo = new KeywordInfo();   
2915
                KeywordInfos.KeywordItems.Add(new KeywordItem()
2916
                {
2917
                    Index = index,
2918
                    Name = name,
2919
                    Keyword = keyword
2920
                });
2921
            }
2922
            #endregion
2923

    
2924
            DataRow[] endofHeaderRows = PipeSystemNetwork.Select(string.Format(" From_Data = '{0}' OR To_Data = '{0}'", "ENDOFHEADER"));
2925
            foreach (DataRow dataRow in endofHeaderRows)
2926
            {
2927
                PSNItem PSNItem = PSNItems.Find(x => x.PSN_OID() == dataRow["OID"].ToString());
2928

    
2929
                if (dataRow.Field<string>("From_Data") == "ENDOFHEADER")
2930
                {
2931
                    DataRow[] pathItemRows = PathItems.Select(string.Format("PipeSystemNetwork_OID = '{0}'", dataRow["OID"]));
2932
                    DataRow dr = pathItemRows.First();
2933
                    dr["CLASS"] = PSNItem.Groups.First().Items.First().ID2DBName;
2934
                    dr["TYPE"] = "End";
2935
                    dr["ITEMTAG"] = "ENDOFHEADER";
2936
                    dr["DESCRIPTION"] = "ENDOFHEADER";
2937
                }
2938

    
2939
                if (dataRow.Field<string>("To_Data") == "ENDOFHEADER")
2940
                {
2941
                    DataRow[] pathItemRows = PathItems.Select(string.Format("PipeSystemNetwork_OID = '{0}'", dataRow["OID"]));
2942
                    DataRow dr = pathItemRows.Last();
2943
                    dr["CLASS"] = PSNItem.Groups.Last().Items.Last().ID2DBName;
2944
                    dr["TYPE"] = "End";
2945
                    dr["ITEMTAG"] = "ENDOFHEADER";
2946
                    dr["DESCRIPTION"] = "ENDOFHEADER";
2947
                }
2948
            }
2949

    
2950
            foreach (KeywordItem keyitem in KeywordInfos.KeywordItems)
2951
            {
2952
                DataRow[] keywordRows = PipeSystemNetwork.Select(string.Format(" From_Data = '{0}' OR To_Data = '{0}'", keyitem.Keyword));
2953
                foreach (DataRow dataRow in keywordRows)
2954
                {
2955
                    PSNItem PSNItem = PSNItems.Find(x => x.PSN_OID() == dataRow["OID"].ToString());
2956

    
2957
                    if (dataRow.Field<string>("From_Data") == keyitem.Keyword)
2958
                    {
2959
                        DataRow[] pathItemRows = PathItems.Select(string.Format("PipeSystemNetwork_OID = '{0}'", dataRow["OID"]));
2960

    
2961
                        //
2962
                        //if(.)
2963
                        if (PSNItem.Groups.First().Items.First().Name.Equals(keyitem.Name))
2964
                        {
2965
                            DataRow dr = pathItemRows.First();
2966
                            //dr["CLASS"] = ""; //Type
2967
                            dr["TYPE"] = "End";
2968
                            dr["ITEMTAG"] = keyitem.Keyword;
2969
                            dr["DESCRIPTION"] = keyitem.Keyword;
2970
                        }
2971

    
2972
                    }
2973

    
2974
                    if (dataRow.Field<string>("To_Data") == keyitem.Keyword)
2975
                    {
2976
                        DataRow[] pathItemRows = PathItems.Select(string.Format("PipeSystemNetwork_OID = '{0}'", dataRow["OID"]));
2977

    
2978
                        if (PSNItem.Groups.Last().Items.Last().Name.Equals(keyitem.Name))
2979
                        {
2980
                            DataRow dr = pathItemRows.Last();
2981
                            //dr["CLASS"] = ""; //Type
2982
                            dr["TYPE"] = "End";
2983
                            dr["ITEMTAG"] = keyitem.Keyword;
2984
                            dr["DESCRIPTION"] = keyitem.Keyword;
2985
                            //dr.Field<string>("Type")
2986
                        }
2987

    
2988
                    }
2989
                }
2990
            }
2991
        }
2992

    
2993
        private void UpdateErrorForPSN()
2994
        {
2995
            DataRow[] errorRows = PipeSystemNetwork.Select(string.Format(" Type = '{0}'", ErrorType.Error));
2996
            foreach (DataRow dataRow in errorRows)
2997
            {
2998
                try
2999
                {
3000
                    PSNItem PSNItem = PSNItems.Find(x => x.PSN_OID() == dataRow["OID"].ToString());
3001
                   
3002
                    bool change = false;
3003
                    bool bCheck = false;
3004
                    if (!PSNItem.EnableType(PSNItem.StartType))
3005
                    {
3006
                        change = true;
3007
                        DataRow[] pathItemRows = PathItems.Select(string.Format("PipeSystemNetwork_OID = '{0}'", dataRow["OID"]));
3008
                        int insertIndex = PathItems.Rows.IndexOf(pathItemRows.First());
3009

    
3010
                        Item item = PSNItem.Groups.First().Items.First();
3011
                        try
3012
                        {
3013
                            string FROM_DATA = string.Format("TIEINPOINT_{0:D5}V", tieInPointIndex);
3014

    
3015
                            foreach (DataRow loopRow in PipeSystemNetwork.Select(string.Format("OID = '{0}'", PSNItem.PSN_OID())))
3016
                            {
3017
                                loopRow["FROM_DATA"] = FROM_DATA;
3018
                                if (item.ItemType == ItemType.Line)
3019
                                {
3020
                                    if (loopRow.Field<string>("OrderNumber") == "0")
3021
                                    {
3022
                                        string status = loopRow.Field<string>("Status");
3023
                                        //string isvali
3024
                                        if (string.IsNullOrEmpty(status))
3025
                                            status = "Line Disconnected";
3026
                                        else if (!status.Contains("Line Disconnected"))
3027
                                            status += ", Line Disconnected";
3028
                                        loopRow["Status"] = status;
3029
                                        if (!string.IsNullOrEmpty(status))
3030
                                            loopRow["IsValid"] = "Error";
3031
                                    }
3032
                                }
3033
                            }
3034

    
3035
                            tieInPointIndex++;
3036

    
3037
                            if (item.ItemType == ItemType.Line)
3038
                            {
3039
                                PathItems.Rows.InsertAt(createTerminatorRow(pathItemRows.First(), FROM_DATA), insertIndex);                                
3040
                            }
3041
                            else
3042
                            {
3043
                                PathItems.Rows.InsertAt(createLineRow(PathItems.Select(string.Format("PipeLine_OID = '{0}' AND ItemName = 'PipeRun' AND PipeSystemNetwork_OID = '{1}'", item.PSNPipeLineID, dataRow["OID"])).First()), insertIndex);
3044
                                PathItems.Rows.InsertAt(createTerminatorRow(pathItemRows.First(), FROM_DATA), insertIndex);
3045

    
3046
                                bCheck = true;
3047
                            }
3048

    
3049
                            PSNItem.StartType = PSNType.Equipment;
3050
                        }
3051
                        catch (Exception ex)
3052
                        {
3053
                            MessageBox.Show("Please check the item.\r\nDrawingName : " + item.Document.DrawingName + "\r\nUID : " + item.UID, "ID2 " + id2Info.ProgramName, MessageBoxButtons.OK, MessageBoxIcon.Warning);
3054
                            return;
3055
                        }
3056
                    }
3057

    
3058
                    if (!PSNItem.EnableType(PSNItem.EndType))
3059
                    {                        
3060
                        DataRow[] pathItemRows = PathItems.Select(string.Format("PipeSystemNetwork_OID = '{0}'", dataRow["OID"]));
3061
                        //int insertIndex = PathItems.Rows.IndexOf(pathItemRows.First()) + pathItemRows.Count() - 1;
3062
                        DataRow dr = pathItemRows.Last();
3063
                        if (change)
3064
                        {
3065
                            if(bCheck)
3066
                                dr = pathItemRows[pathItemRows.Count() - 3];
3067
                            else
3068
                            {
3069
                                dr = pathItemRows[pathItemRows.Count() - 2];
3070
                            }
3071
                        }
3072

    
3073
                        change = true;
3074
                        int insertIndex = PathItems.Rows.IndexOf(dr) + 1;
3075

    
3076
                        Item item = PSNItem.Groups.Last().Items.Last();
3077
                        try
3078
                        {
3079
                            string TO_DATA = string.Format("TIEINPOINT_{0:D5}V", tieInPointIndex);
3080

    
3081
                           
3082
                            foreach (DataRow loopRow in PipeSystemNetwork.Select(string.Format("OID = '{0}'", PSNItem.PSN_OID())))
3083
                            {
3084
                                loopRow["TO_DATA"] = TO_DATA;
3085
                                if (item.ItemType == ItemType.Line)
3086
                                {
3087
                                    if (loopRow.Field<string>("OrderNumber") == Convert.ToString(PipeSystemNetwork.Select(string.Format("OID = '{0}'", PSNItem.PSN_OID())).Count() - 1))
3088
                                    {
3089
                                        string status = loopRow.Field<string>("Status");
3090
                                        if (string.IsNullOrEmpty(status))
3091
                                            status = "Line Disconnected";
3092
                                        else if (!status.Contains("Line Disconnected"))
3093
                                            status += ", Line Disconnected";
3094
                                        loopRow["Status"] = status;
3095
                                        if (!string.IsNullOrEmpty(status))
3096
                                            loopRow["IsValid"] = "Error";
3097
                                    }
3098
                                }
3099
                            }                            
3100

    
3101
                            tieInPointIndex++;
3102

    
3103
                            if (item.ItemType == ItemType.Line)
3104
                            {
3105
                                PathItems.Rows.InsertAt(createTerminatorRow(pathItemRows.Last(), TO_DATA), insertIndex);
3106
                            }
3107
                            else
3108
                            {
3109
                                PathItems.Rows.InsertAt(createTerminatorRow(pathItemRows.Last(), TO_DATA), insertIndex);
3110
                                if(PathItems.Select(string.Format("PipeLine_OID = '{0}' AND ItemName = 'PipeRun' AND PipeSystemNetwork_OID = '{1}'", item.PSNPipeLineID, dataRow["OID"])).Count() > 0)
3111
                                    PathItems.Rows.InsertAt(createLineRow(PathItems.Select(string.Format("PipeLine_OID = '{0}' AND ItemName = 'PipeRun' AND PipeSystemNetwork_OID = '{1}'", item.PSNPipeLineID, dataRow["OID"])).Last()), insertIndex);
3112
                            }
3113

    
3114
                            PSNItem.EndType = PSNType.Equipment;
3115
                        }
3116
                        catch (Exception ex)
3117
                        {
3118
                            MessageBox.Show("Please check the item.\r\nDrawingName : " + item.Document.DrawingName + "\r\nUID : " + item.UID, "ID2 " + id2Info.ProgramName, MessageBoxButtons.OK, MessageBoxIcon.Warning);
3119
                            return;
3120
                        }
3121
                    }
3122

    
3123
                    dataRow["Type"] = PSNItem.GetPSNType();
3124
                    if (change)
3125
                    {
3126
                        int rowIndex = 0;
3127
                        for (int i = 0; i < PathItems.Rows.Count; i++)
3128
                        {
3129
                            DataRow row = PathItems.Rows[i];
3130
                            if (row["PipeSystemNetwork_OID"].ToString() != dataRow["OID"].ToString())
3131
                                continue;
3132
                            string sequenceData = row["SequenceData_OID"].ToString();
3133
                            string[] split = sequenceData.Split(new char[] { '_' });
3134

    
3135
                            StringBuilder sb = new StringBuilder();
3136
                            for (int j = 0; j < split.Length - 1; j++)
3137
                                sb.Append(split[j] + "_");
3138
                            sb.Append(rowIndex++);
3139
                            row["SequenceData_OID"] = sb.ToString();
3140

    
3141
                            DataRow seqItemRows = SequenceData.Select(string.Format("PathItem_OID = '{0}'", row["OID"])).FirstOrDefault();
3142
                            int insertSeqIndex = SequenceData.Rows.IndexOf(seqItemRows);
3143

    
3144
                            string[] splitseq = sb.ToString().Split(new char[] { '_' });
3145

    
3146
                            if (seqItemRows == null)
3147
                            {
3148
                                DataRow newRow = SequenceData.NewRow();
3149
                                newRow["OID"] = sb.ToString();
3150
                                newRow["SERIALNUMBER"] = splitseq[splitseq.Length - 1];
3151
                                newRow["PathItem_OID"] = row["OID"];
3152
                                newRow["TopologySet_OID_Key"] = row["TopologySet_OID"];
3153
                                SequenceData.Rows.InsertAt(newRow, Convert.ToInt32(splitseq[splitseq.Length - 1]));
3154
                            }
3155
                            else
3156
                            {
3157
                                seqItemRows["OID"] = sb.ToString();
3158
                                seqItemRows["SERIALNUMBER"] = splitseq[splitseq.Length - 1];
3159
                                seqItemRows["PathItem_OID"] = row["OID"];
3160
                                seqItemRows["TopologySet_OID_Key"] = row["TopologySet_OID"];
3161
                            }
3162
                        }
3163
                    }
3164

    
3165
                    DataRow createTerminatorRow(DataRow itemRow, string DATA)
3166
                    {
3167
                        DataRow newRow = PathItems.NewRow();
3168
                        newRow["OID"] = Guid.NewGuid().ToString();
3169
                        newRow["SequenceData_OID"] = itemRow["SequenceData_OID"];
3170
                        newRow["TopologySet_OID"] = itemRow["TopologySet_OID"];
3171
                        newRow["BranchTopologySet_OID"] = itemRow["BranchTopologySet_OID"];
3172
                        newRow["PipeLine_OID"] = itemRow["PipeLine_OID"];
3173
                        newRow["ITEMNAME"] = "PipingComp"; //newRow["ITEMNAME"] = "End of line terminator";
3174
                        newRow["ITEMTAG"] = DATA; //itemRow["ITEMTAG"];
3175
                        newRow["DESCRIPTION"] = DATA;
3176
                        newRow["Class"] = "End of line terminator";
3177
                        newRow["SubClass"] = "End of line terminator";
3178
                        newRow["TYPE"] = "End";
3179
                        newRow["PIDNAME"] = itemRow["PIDNAME"];
3180
                        newRow["NPD"] = itemRow["NPD"];
3181
                        newRow["PipeSystemNetwork_OID"] = itemRow["PipeSystemNetwork_OID"];
3182
                        newRow["ViewPipeSystemNetwork_OID"] = itemRow["ViewPipeSystemNetwork_OID"];
3183
                        newRow["PipeRun_OID"] = itemRow["PipeRun_OID"];
3184
                        newRow["EGTConnectedPoint"] = "0";
3185
                        return newRow;
3186
                    }
3187

    
3188
                    DataRow createLineRow(DataRow itemRow)
3189
                    {
3190
                        DataRow newRow = PathItems.NewRow();
3191
                        newRow["OID"] = Guid.NewGuid().ToString();
3192
                        newRow["SequenceData_OID"] = itemRow["SequenceData_OID"];
3193
                        newRow["TopologySet_OID"] = itemRow["TopologySet_OID"];
3194
                        newRow["BranchTopologySet_OID"] = itemRow["BranchTopologySet_OID"];
3195
                        newRow["PipeLine_OID"] = itemRow["PipeLine_OID"];
3196
                        newRow["ITEMNAME"] = itemRow["ITEMNAME"];
3197
                        newRow["ITEMTAG"] = itemRow["ITEMTAG"];
3198
                        newRow["Class"] = itemRow["Class"];
3199
                        newRow["SubClass"] = itemRow["SubClass"];
3200
                        newRow["TYPE"] = itemRow["TYPE"];
3201
                        newRow["PIDNAME"] = itemRow["PIDNAME"];
3202
                        newRow["NPD"] = itemRow["NPD"];
3203
                        newRow["PipeSystemNetwork_OID"] = itemRow["PipeSystemNetwork_OID"];
3204
                        newRow["ViewPipeSystemNetwork_OID"] = itemRow["ViewPipeSystemNetwork_OID"];
3205
                        newRow["PipeRun_OID"] = itemRow["PipeRun_OID"];
3206
                        newRow["EGTConnectedPoint"] = "0";
3207
                        return newRow;
3208
                    }
3209
                }
3210
                catch (Exception ex)
3211
                {
3212

    
3213
                }
3214
            }
3215
        }
3216

    
3217
        private void InsertTeePSN()
3218
        {
3219
            DataTable dt = PipeSystemNetwork.DefaultView.ToTable(true, new string[] { "OID", "Type" });
3220
            DataRow[] branchRows = dt.Select("Type Like '%B%'");
3221
            bool change = false;
3222
            foreach (DataRow dataRow in branchRows)
3223
            {
3224
                try
3225
                {
3226
                    PSNItem PSNItem = PSNItems.Find(x => x.PSN_OID() == dataRow["OID"].ToString());
3227
                    change = false;
3228

    
3229
                    if (PSNItem.StartType == PSNType.Branch)
3230
                    {
3231
                        DataRow[] pathItemRows = PathItems.Select(string.Format("PipeSystemNetwork_OID = '{0}'", PSNItem.PSN_OID()));
3232
                        int insertIndex = PathItems.Rows.IndexOf(pathItemRows.First());
3233
                        Item Teeitem = PSNItem.Groups.First().Items.First();
3234
                        try
3235
                        {
3236
                            if (Teeitem.ItemType == ItemType.Line)
3237
                            {
3238
                                if (pathItemRows.First().Field<string>("SubClass") != "Tee")
3239
                                {
3240
                                    PathItems.Rows.InsertAt(createTeeRow(pathItemRows.First()), insertIndex);
3241
                                    pathItemRows.First().SetField("BranchTopologySet_OID", string.Empty);
3242
                                    pathItemRows.First().SetField("ViewPipeSystemNetwork_OID", dataRow["OID"].ToString());
3243
                                    change = true;
3244
                                }
3245

    
3246
                            }
3247
                        }
3248
                        catch (Exception ex)
3249
                        {
3250
                            MessageBox.Show("Please check the item.\r\nDrawingName : " + Teeitem.Document.DrawingName + "\r\nUID : " + Teeitem.UID, "ID2 " + id2Info.ProgramName, MessageBoxButtons.OK, MessageBoxIcon.Warning);
3251
                            return;
3252
                        }
3253
                    }
3254

    
3255
                    if (PSNItem.EndType == PSNType.Branch)
3256
                    {
3257
                        //change = true;
3258
                        DataRow[] pathItemRows = PathItems.Select(string.Format("PipeSystemNetwork_OID = '{0}'", PSNItem.PSN_OID()));
3259

    
3260
                        Item Teeitem = PSNItem.Groups.Last().Items.Last();
3261

    
3262
                        DataRow dr = pathItemRows.Last();
3263
                        if (change)
3264
                            dr = pathItemRows[pathItemRows.Count() - 2];
3265

    
3266
                        int insertIndex = PathItems.Rows.IndexOf(dr) + 1;
3267

    
3268
                        try
3269
                        {
3270
                            if (Teeitem.ItemType == ItemType.Line)
3271
                            {
3272
                                if (dr.Field<string>("SubClass") != "Tee")
3273
                                {
3274
                                    PathItems.Rows.InsertAt(createTeeRow(dr), insertIndex);
3275
                                    change = true;
3276
                                    dr.SetField("BranchTopologySet_OID", string.Empty);
3277
                                    dr.SetField("ViewPipeSystemNetwork_OID", dataRow["OID"].ToString());
3278
                                }
3279

    
3280
                            }
3281
                        }
3282
                        catch (Exception ex)
3283
                        {
3284
                            MessageBox.Show("Please check the item.\r\nDrawingName : " + Teeitem.Document.DrawingName + "\r\nUID : " + Teeitem.UID, "ID2 " + id2Info.ProgramName, MessageBoxButtons.OK, MessageBoxIcon.Warning);
3285
                            return;
3286
                        }
3287
                    }
3288

    
3289
                    if (change)
3290
                    {
3291
                        //DataRow[] pathItemRows = pathItemsDT.Select(string.Format("PipeSystemNetwork_OID = '{0}'", PSNItem.PSN_OID()));
3292
                        int rowIndex = 0;
3293
                        for (int i = 0; i < PathItems.Rows.Count; i++)
3294
                        {
3295
                            DataRow row = PathItems.Rows[i];
3296
                            if (row["PipeSystemNetwork_OID"].ToString() != PSNItem.PSN_OID())
3297
                                continue;
3298
                            string sequenceData = row["SequenceData_OID"].ToString();
3299
                            string[] split = sequenceData.Split(new char[] { '_' });
3300

    
3301
                            StringBuilder sb = new StringBuilder();
3302
                            for (int j = 0; j < split.Length - 1; j++)
3303
                                sb.Append(split[j] + "_");
3304
                            sb.Append(rowIndex++);
3305
                            row["SequenceData_OID"] = sb.ToString();
3306

    
3307
                            DataRow seqItemRows = SequenceData.Select(string.Format("PathItem_OID = '{0}'", row["OID"])).FirstOrDefault();
3308
                            int insertSeqIndex = SequenceData.Rows.IndexOf(seqItemRows);
3309

    
3310
                            string[] splitseq = sb.ToString().Split(new char[] { '_' });
3311

    
3312
                            if (seqItemRows == null)
3313
                            {
3314
                                DataRow newRow = SequenceData.NewRow();
3315
                                newRow["OID"] = sb.ToString();
3316
                                newRow["SERIALNUMBER"] = splitseq[splitseq.Length - 1];
3317
                                newRow["PathItem_OID"] = row["OID"];
3318
                                newRow["TopologySet_OID_Key"] = row["TopologySet_OID"];
3319
                                SequenceData.Rows.InsertAt(newRow, Convert.ToInt32(splitseq[splitseq.Length - 1]));
3320
                            }
3321
                            else
3322
                            {
3323
                                seqItemRows["OID"] = sb.ToString();
3324
                                seqItemRows["SERIALNUMBER"] = splitseq[splitseq.Length - 1];
3325
                                seqItemRows["PathItem_OID"] = row["OID"];
3326
                                seqItemRows["TopologySet_OID_Key"] = row["TopologySet_OID"];
3327
                            }
3328
                        }
3329
                    }
3330

    
3331
                    DataRow createTeeRow(DataRow itemRow)
3332
                    {
3333
                        DataRow newRow = PathItems.NewRow();
3334
                        newRow["OID"] = Guid.NewGuid().ToString();
3335
                        newRow["SequenceData_OID"] = itemRow["SequenceData_OID"];
3336
                        newRow["TopologySet_OID"] = itemRow["TopologySet_OID"];
3337
                        newRow["BranchTopologySet_OID"] = itemRow["BranchTopologySet_OID"];
3338
                        newRow["PipeLine_OID"] = itemRow["PipeLine_OID"];
3339
                        newRow["ITEMNAME"] = "Branch"; //newRow["ITEMNAME"] = "End of line terminator";
3340
                        newRow["ITEMTAG"] = itemRow["ITEMTAG"];
3341
                        newRow["DESCRIPTION"] = "";
3342
                        newRow["Class"] = "Branch";
3343
                        newRow["SubClass"] = "Tee";
3344
                        newRow["TYPE"] = itemRow["TYPE"];
3345
                        newRow["PIDNAME"] = itemRow["PIDNAME"];
3346
                        newRow["NPD"] = itemRow["NPD"];
3347
                        newRow["PipeSystemNetwork_OID"] = itemRow["PipeSystemNetwork_OID"];
3348
                        newRow["ViewPipeSystemNetwork_OID"] = itemRow["ViewPipeSystemNetwork_OID"];
3349
                        newRow["PipeRun_OID"] = itemRow["PipeRun_OID"];
3350
                      
3351
                        newRow["EGTConnectedPoint"] = 0;
3352
                        return newRow;
3353
                    }
3354
                }
3355
                catch (Exception ex)
3356
                {
3357

    
3358
                }
3359
            }
3360
        }
3361
    }
3362

    
3363
    public class PSNItem
3364
    {
3365
        public PSNItem(int count, int Revision)
3366
        {
3367
            Groups = new List<Group>();
3368
            Topologies = new List<Topology>();
3369

    
3370
            Index = count + 1;
3371
            this.Revision = Revision;
3372
        }
3373

    
3374
        private int Revision;
3375
        public string UID { get; set; }
3376
        public List<Group> Groups { get; set; }
3377
        public List<Topology> Topologies { get; set; }
3378
        public PSNType StartType { get; set; }
3379
        public PSNType EndType { get; set; }
3380
        public int Index { get; set; }
3381
        public string IsValid { get; set; }
3382
        public bool IsKeyword { get; set; }
3383
        public string Status { get; set; }
3384
        public string IncludingVirtualData { get; set; }
3385
        public string PSNAccuracy { get; set; }
3386
        public KeywordInfo KeywordInfos = new KeywordInfo();
3387
        public DataTable Nozzle = new DataTable();
3388

    
3389
        public string PSN_OID()
3390
        {
3391
            return string.Format("V{0}-PSN-{1}", string.Format("{0:D4}", Revision), string.Format("{0:D5}", Index));
3392
        }
3393

    
3394
        public string GetPSNType()
3395
        {
3396
            string result = string.Empty;
3397

    
3398
            if (EnableType(StartType) && EnableType(EndType))
3399
            {
3400
                if (StartType == PSNType.Equipment && EndType == PSNType.Equipment)
3401
                    result = "E2E";
3402
                else if (StartType == PSNType.Branch && EndType == PSNType.Branch)
3403
                    result = "B2B";
3404
                else if (StartType == PSNType.Header && EndType == PSNType.Header)
3405
                    result = "HD2";
3406

    
3407
                else if (StartType == PSNType.Equipment && EndType == PSNType.Branch)
3408
                    result = "E2B";
3409
                else if (StartType == PSNType.Branch && EndType == PSNType.Equipment)
3410
                    result = "B2E";
3411

    
3412
                else if (StartType == PSNType.Header && EndType == PSNType.Branch)
3413
                    result = "HDB";
3414
                else if (StartType == PSNType.Branch && EndType == PSNType.Header)
3415
                    result = "HDB";
3416

    
3417
                else if (StartType == PSNType.Header && EndType == PSNType.Equipment)
3418
                    result = "HDE";
3419
                else if (StartType == PSNType.Equipment && EndType == PSNType.Header)
3420
                    result = "HDE";
3421
                else
3422
                    result = "Error";
3423
            }
3424
            else
3425
                result = "Error";
3426

    
3427
            return result;
3428

    
3429

    
3430
        }
3431

    
3432
        public bool EnableType(PSNType type)
3433
        {
3434
            bool result = false;
3435

    
3436
            if (type == PSNType.Branch ||
3437
                type == PSNType.Equipment ||
3438
                type == PSNType.Header)
3439
            {
3440
                result = true;
3441
            }
3442

    
3443
            return result;
3444
        }
3445

    
3446
        public bool IsBypass { get; set; }
3447

    
3448
        public string GetFromData(ref string Type, ref Item item)
3449
        {
3450
            Status = string.Empty;
3451
            string result = string.Empty;
3452
            if (IsKeyword)
3453
                IsKeyword = false;
3454
            try
3455
            {
3456
                item = Groups.First().Items.First();
3457

    
3458
                if (StartType == PSNType.Header)
3459
                    result = "ENDOFHEADER";
3460
                else if (StartType == PSNType.Branch)
3461
                {
3462
                    //if (!item.MissingLineNumber && item.Relations.First().Item.LineNumber != null && !string.IsNullOrEmpty(item.Relations.First().Item.LineNumber.Name))
3463
                    if (item.Relations.First().Item.LineNumber != null && !string.IsNullOrEmpty(item.Relations.First().Item.LineNumber.Name))
3464
                        result = item.Relations.First().Item.LineNumber.Name;
3465
                    else
3466
                    {
3467
                        IsValid = "Error";
3468
                        result = "Empty LineNumber";
3469
                    }
3470

    
3471
                    if (item.MissingLineNumber2)
3472
                        Status += ", Missing LineNumber_2";
3473

    
3474

    
3475
                    //if (item.MissingLineNumber1)
3476
                    //{
3477
                    //    Status += ", Missing LineNumber_1";
3478
                    //    result = item.MissingLineNumber1 && item.LineNumber != null && !string.IsNullOrEmpty(item.LineNumber.Name) ? item.LineNumber.Name : "Empty LineNumber";
3479

    
3480
                    //}
3481
                }
3482
                else if (StartType == PSNType.Equipment)
3483
                {
3484
                    if (Groups.First().Items.First().Equipment != null)
3485
                        result = Groups.First().Items.First().Equipment.ItemTag;
3486
                    DataRow drNozzle = Nozzle.Select(string.Format("OID = '{0}'", Groups.First().Items.First().UID)).FirstOrDefault();
3487

    
3488
                    if (drNozzle != null)
3489
                        result += " [" + drNozzle.Field<string>("ITEMTAG") + "]";
3490
                }
3491
                else
3492
                {
3493
                    IsValid = "Error";
3494
                    item = Groups.First().Items.First();
3495
                    if (item.ItemType == ItemType.Symbol)
3496
                    {
3497

    
3498
                        string keyword = string.Empty;
3499
                        keyword = GetFromKeywordData(ref Type, item);
3500

    
3501
                        if (string.IsNullOrEmpty(keyword))
3502
                        {
3503
                            if (item.ID2DBType.Contains("OPC's"))
3504
                                Status += ", OPC Disconnected";
3505
                            else
3506
                                Status += ", Missing ItemTag or Description";
3507

    
3508
                            result = item.ID2DBName;
3509
                        }
3510
                        else
3511
                        {
3512
                            result = keyword;
3513
                            IsKeyword = true;
3514
                            IsValid = string.Empty;
3515
                        }
3516

    
3517
                    }
3518
                    else if (item.ItemType == ItemType.Line)
3519
                    {
3520

    
3521
                        if (item.MissingLineNumber1)
3522
                        {
3523
                            IsValid = "Error";
3524
                            Status += ", Missing LineNumber_1";
3525
                            result = item.MissingLineNumber1 && item.LineNumber != null && !string.IsNullOrEmpty(item.LineNumber.Name) ? item.LineNumber.Name : "Empty LineNumber";
3526
                        }
3527
                        else
3528
                            result = !string.IsNullOrEmpty(item.LineNumber.Name) ? item.LineNumber.Name : "Empty LineNumber";
3529
                    }
3530
                    else
3531
                        result = "Unknown";
3532

    
3533
                }
3534
            }
3535
            catch (Exception ex)
3536
            {
3537

    
3538
            }
3539

    
3540
            return result;
3541
        }
3542

    
3543
        public string GetFromKeywordData(ref string Type, Item item)
3544
        {
3545
            string result = string.Empty;
3546

    
3547
            foreach (KeywordItem keyitem in KeywordInfos.KeywordItems)
3548
            {
3549
                if (keyitem.Name.Equals(item.Name))
3550
                {
3551
                    result = keyitem.Keyword;
3552
                    Type = item.ID2DBType;
3553
                    break;
3554
                }
3555
            }
3556

    
3557
            return result;
3558
        }
3559

    
3560
        public string GetToKeywordData(ref string Type, Item item)
3561
        {
3562
            string result = string.Empty;
3563

    
3564
            foreach (KeywordItem keyitem in KeywordInfos.KeywordItems)
3565
            {
3566
                if (keyitem.Name.Equals(item.Name))
3567
                {
3568
                    result = keyitem.Keyword;
3569
                    Type = item.ID2DBType;
3570
                    break;
3571
                }
3572
            }
3573
            return result;
3574
        }
3575

    
3576
        public string GetToData(ref string ToType, ref Item item)
3577
        {
3578
            string result = string.Empty;
3579
            Status = string.Empty;
3580

    
3581
            if (IsKeyword)
3582
                IsKeyword = false;
3583

    
3584
            item = Groups.Last().Items.Last();
3585

    
3586
            if (EndType == PSNType.Header)
3587
                result = "ENDOFHEADER";
3588
            else if (EndType == PSNType.Branch)
3589
            {
3590
                //if (!item.MissingLineNumber && item.Relations.Last().Item.LineNumber != null && !string.IsNullOrEmpty(item.Relations.Last().Item.LineNumber.Name))
3591
                if (item.Relations.Last().Item.LineNumber != null && !string.IsNullOrEmpty(item.Relations.Last().Item.LineNumber.Name))
3592
                    result = item.Relations.Last().Item.LineNumber.Name;
3593
                else
3594
                {
3595
                    IsValid = "Error";                    
3596
                    result = "Empty LineNumber";
3597
                }
3598

    
3599
                if(item.MissingLineNumber2)
3600
                    Status += ", Missing LineNumber_2";
3601
                //if (item.MissingLineNumber1)
3602
                //{
3603
                //    Status += ", Missing LineNumber_1";
3604
                //    result = item.MissingLineNumber1 && item.LineNumber != null && !string.IsNullOrEmpty(item.LineNumber.Name) ? item.LineNumber.Name : "Empty LineNumber";
3605

    
3606
                //}
3607
            }
3608
            else if (EndType == PSNType.Equipment)
3609
            {
3610
                if (Groups.Last().Items.Last().Equipment != null)
3611
                    result = Groups.Last().Items.Last().Equipment.ItemTag;
3612

    
3613
                DataRow drNozzle = Nozzle.Select(string.Format("OID = '{0}'", Groups.Last().Items.Last().UID)).FirstOrDefault();
3614

    
3615
                if (drNozzle != null)
3616
                    result += " [" + drNozzle.Field<string>("ITEMTAG") + "]";
3617
            }
3618
            else
3619
            {
3620
                IsValid = "Error";
3621
                item = Groups.Last().Items.Last();
3622
                if (item.ItemType == ItemType.Symbol)
3623
                {
3624
                    string keyword = string.Empty;
3625
                    keyword = GetToKeywordData(ref ToType, item);
3626

    
3627
                    if (string.IsNullOrEmpty(keyword))
3628
                    {
3629
                        if (item.ID2DBType.Contains("OPC's"))
3630
                            Status += ", OPC Disconnected";
3631
                        else
3632
                            Status += ", Missing ItemTag or Description";
3633

    
3634
                        result = item.ID2DBName;
3635
                    }
3636
                    else
3637
                    {
3638
                        result = keyword;
3639
                        IsValid = string.Empty;
3640
                        IsKeyword = true;
3641
                    }
3642

    
3643
                }
3644
                else if (item.ItemType == ItemType.Line)
3645
                {
3646
                    if (item.MissingLineNumber1)
3647
                    {
3648
                        IsValid = "Error";
3649
                        Status += ", Missing LineNumber_1";
3650
                        result = item.MissingLineNumber1 && item.LineNumber != null && !string.IsNullOrEmpty(item.LineNumber.Name) ? item.LineNumber.Name : "Empty LineNumber";
3651
                    }
3652
                    else
3653
                        result = !string.IsNullOrEmpty(item.LineNumber.Name) ? item.LineNumber.Name : "Empty LineNumber";
3654
                }
3655
                else
3656
                    result = "Unknown";
3657

    
3658

    
3659
            }
3660

    
3661

    
3662

    
3663
            return result;
3664
        }
3665

    
3666
        public string GetPBSData()
3667
        {
3668
            string result = string.Empty;
3669
            List<string> PBSList = new List<string>();
3670
            if (Settings.Default.PBSSetting.Equals("Line Number"))
3671
            {
3672
                string attrValue = Settings.Default.PBSSettingValue;
3673

    
3674
                foreach (Group group in Groups)
3675
                {
3676
                    List<LineNumber> lineNumbers = group.Items.Select(x => x.LineNumber).Distinct().ToList();
3677
                    foreach (LineNumber lineNumber in lineNumbers)
3678
                    {
3679
                        Attribute attribute = lineNumber.Attributes.Find(x => x.Name == attrValue && !string.IsNullOrEmpty(x.Value));
3680
                        if (attribute != null)
3681
                        {
3682
                            string value = attribute.Value;
3683
                            if (!PBSList.Contains(value))
3684
                                PBSList.Add(value);
3685
                        }
3686
                    }
3687
                }
3688
            }
3689
            else if (Settings.Default.PBSSetting.Equals("Item Attribute"))
3690
            {
3691
                string attrValue = Settings.Default.PBSSettingValue;
3692

    
3693
                foreach (Group group in Groups)
3694
                {
3695
                    List<Item> items = group.Items.FindAll(x => x.Attributes.Find(y => y.Name == attrValue && !string.IsNullOrEmpty(y.Value)) != null);
3696
                    foreach (Item item in items)
3697
                    {
3698
                        string value = item.Attributes.Find(x => x.Name == attrValue).Value;
3699
                        if (!PBSList.Contains(value))
3700
                            PBSList.Add(value);
3701
                    }
3702
                }
3703
            }
3704
            else if (Settings.Default.PBSSetting.Equals("Drawing No"))
3705
            {
3706
                string attrValue = Settings.Default.PBSSettingValue;
3707

    
3708
                foreach (Group group in Groups)
3709
                {
3710
                    List<Document> documents = group.Items.Select(x => x.Document).Distinct().ToList();
3711
                    foreach (Document document in documents)
3712
                    {
3713
                        string name = document.DrawingName;
3714

    
3715
                        int startIndex = Settings.Default.PBSSettingStartValue;
3716
                        int endIndex = Settings.Default.PBSSettingEndValue;
3717

    
3718
                        string subStr = name.Substring(startIndex - 1, endIndex - startIndex + 1);
3719
                        if (!PBSList.Contains(subStr))
3720
                            PBSList.Add(subStr);
3721
                    }
3722
                }
3723
            }
3724
            else if (Settings.Default.PBSSetting.Equals("Unit Area"))
3725
            {
3726
                foreach (Group group in Groups)
3727
                {
3728
                    List<Document> documents = group.Items.Select(x => x.Document).Distinct().ToList();
3729
                    foreach (Document document in documents)
3730
                    {
3731
                        List<TextInfo> textInfos = document.TextInfos.FindAll(x => x.Area == "Unit");
3732
                        foreach (TextInfo textInfo in textInfos)
3733
                        {
3734
                            if (!PBSList.Contains(textInfo.Value))
3735
                                PBSList.Add(textInfo.Value);
3736
                        }
3737
                    }
3738
                }
3739
            }
3740

    
3741
            foreach (var item in PBSList)
3742
            {
3743
                if (string.IsNullOrEmpty(result))
3744
                    result = item;
3745
                else
3746
                    result += ", " + item;
3747
            }
3748
            return result;
3749
        }
3750
    }
3751
}
클립보드 이미지 추가 (최대 크기: 500 MB)