프로젝트

일반

사용자정보

통계
| 개정판:

hytos / DTI_PID / ID2PSN / PSN.cs @ ecdd26cc

이력 | 보기 | 이력해설 | 다운로드 (209 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 string TieInPointAttributeName = string.Empty;
40
        public DataTable PathItems { get; set; }
41
        public DataTable SequenceData { get; set; }
42
        public DataTable PipeSystemNetwork { get; set; }
43
        public DataTable TopologySet { get; set; }
44
        public DataTable Equipment { get; set; }
45
        public DataTable Nozzle { get; set; }
46
        public DataTable PipeLine { get; set; }
47
        public DataTable PipeSystem { get; set; }
48

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

    
55
        int tieInPointIndex = 1;
56
        int tieInPointSymbolIndex = 1;
57
        List<Document> Documents;
58
        List<Group> groups = new List<Group>();
59
        List<PSNItem> PSNItems = new List<PSNItem>();
60
        List<Topology> Topologies = new List<Topology>();
61

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

    
65
        ID2Info id2Info = ID2Info.GetInstance();
66

    
67

    
68

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

    
72
        public PSN()
73
        {
74

    
75
        }
76

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

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

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

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

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

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

    
252
            // Vent/Drain PSN 데이터 제거
253
            splashScreenManager1.SetWaitFormDescription(i++ + " / 19  DeleteVentDrain ( 65 % )");
254
            DeleteVentDrain();
255

    
256
            // Update Error
257
            splashScreenManager1.SetWaitFormDescription(i++ + " / 19  UpdateErrorForPSN ( 70 % )");
258
            PathItemSorting();
259
            UpdateErrorForPSN();
260
            // Insert Tee
261
            splashScreenManager1.SetWaitFormDescription(i++ + " / 19  InsertTeePSN ( 75 % )");
262
            PathItemSorting();
263
            InsertTeePSN();
264

    
265
            // ValveGrouping            
266
            splashScreenManager1.SetWaitFormDescription(i++ + " / 19  UpdateValveGrouping ( 80 % )");
267
            PathItemSorting(); 
268
            UpdateValveGrouping();
269

    
270
            splashScreenManager1.SetWaitFormDescription(i++ + " / 19  PathItemSorting ( 85 % )");
271
            PathItemSorting();
272

    
273
            // AirFinCooler 
274
            splashScreenManager1.SetWaitFormDescription(i++ + " / 19  UpdateAirFinCooler ( 90 % )");
275
            UpdateAirFinCooler();
276

    
277
            UpdateNoPocket();
278

    
279
            // 확도 계산
280
            splashScreenManager1.SetWaitFormDescription(i++ + " / 19  UpdateAccuracy ( 95 % )");
281
            UpdateAccuracy();
282

    
283
            //UpdatePSNType();
284

    
285
        }
286

    
287
        private void UpdateNoPocket()
288
        {
289
            try
290
            {
291
                DataRow[] nopocketRows = PipeSystemNetwork.Select("Pocket = 'Yes'");
292
                foreach (DataRow row in nopocketRows)
293
                {
294
                    DataRow[] pathItemRows = PathItems.Select(string.Format("PipeSystemNetwork_OID = '{0}'", row["OID"].ToString()));
295
                    foreach (DataRow dr in pathItemRows)
296
                    {
297
                        if (!string.IsNullOrEmpty(dr["BranchTopologySet_OID"].ToString()))
298
                        {
299
                            DataRow[] rows = TopologySet.Select(string.Format("OID = '{0}'", dr["BranchTopologySet_OID"].ToString()));
300

    
301
                            if (rows.First()["SubType"].ToString() == "Bypass")
302
                            {
303
                                DataRow[] bypassRows = PipeSystemNetwork.Select(string.Format("OID = '{0}'", dr.Field<string>("ViewPipeSystemNetwork_OID")));
304

    
305
                                foreach (DataRow drby in bypassRows)
306
                                {
307
                                    drby["Pocket"] = "Yes";
308
                                }
309
                            }
310
                        }
311
                    }                    
312
                }            
313
            }
314
            catch (Exception ex)
315
            {
316
                MessageBox.Show(ex.Message, "ID2 ", MessageBoxButtons.OK, MessageBoxIcon.Error);
317
            }
318
        }
319

    
320
        private void UpdatePSNType()
321
        {
322
            try
323
            {
324
                foreach (PSNItem PSNItem in PSNItems)
325
                {    
326
                    foreach (Group group in PSNItem.Groups)
327
                    {
328
                        foreach (Item item in group.Items)
329
                        {
330
                            DataRow[] pipeSystems = PipeSystemNetwork.Select(string.Format("OID = '{0}'", PSNItem.PSN_OID()));
331
                           // PSNItem PSNItem = PSNItems.Find(x => x.PSN_OID() == dataRow["OID"].ToString());
332

    
333
                            foreach(DataRow dr in pipeSystems)
334
                            {
335
                                dr["Type"] = PSNItem.GetPSNType();
336
                            }                            
337
                        }
338
                    }                
339
                }
340
            }
341
            catch (Exception ex)
342
            {
343
                MessageBox.Show(ex.Message, "ID2 ", MessageBoxButtons.OK, MessageBoxIcon.Error);
344
            }
345
        }
346

    
347
        private void PathItemSorting()
348
        {
349
            try
350
            {
351
                DataTable dtPathItems = PathItems.Clone();
352
                DataTable dtPipeSystemNetwork = PipeSystemNetwork.DefaultView.ToTable(true, new string[] { "OID" });
353

    
354
                foreach (DataRow drpipe in dtPipeSystemNetwork.Rows)
355
                {
356
                    DataRow[] pathItemRows = PathItems.Select(string.Format("PipeSystemNetwork_OID = '{0}'", drpipe["OID"].ToString()));
357
                    DataTable dtSequenceItems = SequenceData.Clone();
358
                    foreach (DataRow drpath in pathItemRows)
359
                    {
360
                        DataRow sequenceRows = SequenceData.Select(string.Format("PathItem_OID = '{0}'", drpath.Field<string>("OID"))).First();
361

    
362
                        DataRow newRow = dtSequenceItems.NewRow();
363
                        foreach (DataColumn column in SequenceData.Columns)
364
                            if (dtSequenceItems.Columns[column.ColumnName] != null)
365
                                newRow[column.ColumnName] = sequenceRows[column.ColumnName];
366

    
367
                        dtSequenceItems.Rows.Add(newRow);
368
                    }
369

    
370
                    foreach (DataRow sqrow in dtSequenceItems.Select().OrderBy(x => Convert.ToInt32(x.Field<string>("SERIALNUMBER"))))
371
                    {
372
                        DataRow newRow = dtPathItems.NewRow();
373
                        DataRow row = PathItems.Select(string.Format("OID = '{0}'", sqrow["PathItem_OID"])).First();
374

    
375
                        foreach (DataColumn column in PathItems.Columns)
376
                            if (dtPathItems.Columns[column.ColumnName] != null)
377
                                newRow[column.ColumnName] = row[column.ColumnName];
378

    
379
                        dtPathItems.Rows.Add(newRow);
380
                    }
381
                }
382

    
383
                PathItems.Clear();
384
                PathItems = dtPathItems.Copy();
385
            }
386
            catch (Exception ex)
387
            {
388
                MessageBox.Show(ex.Message, "ID2 ", MessageBoxButtons.OK, MessageBoxIcon.Error);
389
            }
390
        }
391
        
392
        private void UpdateAirFinCooler()
393
        {
394
            try
395
            {
396
                
397
                int pumpTagNum = 0;
398
                #region EquipmentAirFinCooler Info
399
                EquipmentAirFinCoolerInfo EquipmentAirFinCooler = new EquipmentAirFinCoolerInfo();
400
                DataTable dtEquipmentAirFinCooler = DB.SelectAirFinCoolerSetting();
401
                foreach (DataRow row in dtEquipmentAirFinCooler.Rows)
402
                {
403
                    EquipmentAirFinCooler.EquipmentAirFinCoolerItem.Add(new EquipmentAirFinCoolerItem()
404
                    {
405
                        Type = row["Type"].ToString(),
406
                        TagIdentifier = row["TagIdentifier"].ToString(),
407
                        AttributeName = row["AttributeName"].ToString(),
408
                        Name = row["Name"].ToString()
409
                    });
410
                }
411
                #endregion
412

    
413
                
414
                DataRow[] pumpRows = PipeSystemNetwork.Select("PUMP = 'PUMP'");
415
                Dictionary<string, string> eqkeyValuePairs = new Dictionary<string, string>();
416
                // 1, 2번
417
                foreach (DataRow dataRow in pumpRows) 
418
                {                  
419
                    DataRow[] pathItemRows = PathItems.Select(string.Format("PipeSystemNetwork_OID = '{0}'", dataRow["OID"].ToString()));
420

    
421
                    string EGTAG = "EGTAG";
422
                   // string ItemTag = "ItemTag";
423
                    string EGFlowDirection = string.Empty;
424
                    string Prefix = string.Empty;
425
                    string SymbolName = string.Empty;
426

    
427
                    PSNItem PSNItem = PSNItems.Find(x => x.PSN_OID() == dataRow["OID"].ToString());
428
                    if (PSNItem.Groups.First().Items.First().Equipment != null)
429
                    {
430
                        if (EquipmentAirFinCooler.EquipmentAirFinCoolerItem.Where(x => x.Type == "Pump" && PSNItem.Groups.First().Items.First().Equipment.Name.Contains(x.Name)).Count() > 0)
431
                        {
432
                            EquipmentAirFinCoolerItem equipmentAirFinCoolerItem = EquipmentAirFinCooler.EquipmentAirFinCoolerItem.Where(x => x.Type == "Pump" && PSNItem.Groups.First().Items.First().Equipment.Name.Contains(x.Name)).First();
433
                            if (!string.IsNullOrEmpty(equipmentAirFinCoolerItem.AttributeName))
434
                                EGTAG = PSNItem.Groups.First().Items.First().Equipment.Attributes.Find(x => x.Name == equipmentAirFinCoolerItem.AttributeName) == null
435
                                   ? string.Empty : PSNItem.Groups.First().Items.First().Equipment.Attributes.Find(x => x.Name == equipmentAirFinCoolerItem.AttributeName).Value;
436

    
437
                            SymbolName = PSNItem.Groups.First().Items.First().Equipment.Name;
438
                            Prefix = equipmentAirFinCoolerItem.TagIdentifier;                            
439
                            EGFlowDirection = "O";
440
                        }                        
441
                    } 
442
                    
443
                    if(string.IsNullOrEmpty(EGFlowDirection) && PSNItem.Groups.Last().Items.Last().Equipment != null)
444
                    {
445
                        if (EquipmentAirFinCooler.EquipmentAirFinCoolerItem.Where(x => x.Type == "Pump" && PSNItem.Groups.Last().Items.Last().Equipment.Name.Contains(x.Name)).Count() > 0)
446
                        {
447
                            EquipmentAirFinCoolerItem equipmentAirFinCoolerItem = EquipmentAirFinCooler.EquipmentAirFinCoolerItem.Where(x => x.Type == "Pump" && PSNItem.Groups.Last().Items.Last().Equipment.Name.Contains(x.Name)).First();
448
                            if (!string.IsNullOrEmpty(equipmentAirFinCoolerItem.AttributeName))
449
                                EGTAG = PSNItem.Groups.Last().Items.Last().Equipment.Attributes.Find(x => x.Name == equipmentAirFinCoolerItem.AttributeName) == null
450
                                    ? string.Empty : PSNItem.Groups.Last().Items.Last().Equipment.Attributes.Find(x => x.Name == equipmentAirFinCoolerItem.AttributeName).Value;
451

    
452
                            SymbolName = PSNItem.Groups.Last().Items.Last().Equipment.Name;
453
                            Prefix = equipmentAirFinCoolerItem.TagIdentifier;                            
454
                            EGFlowDirection = "I";
455
                        }
456
                    }
457

    
458
                    //Attribute가 세팅되어있지 않다면
459
                    if (EGTAG.Equals("EGTAG"))
460
                    {
461
                        if (!eqkeyValuePairs.ContainsKey(SymbolName))
462
                        {
463
                            if (!string.IsNullOrEmpty(Prefix))
464
                            {
465
                                pumpTagNum++;
466
                                EGTAG = Prefix + string.Format("-{0}", string.Format("{0:D5}", pumpTagNum));
467
                                eqkeyValuePairs.Add(SymbolName, EGTAG);
468
                            }
469
                            else
470
                            {
471
                                eqkeyValuePairs.Add(SymbolName, SymbolName);
472
                            }
473
                        }
474
                        else
475
                        {
476
                            EGTAG = eqkeyValuePairs[SymbolName];
477
                        }
478
                    }
479
                    else
480
                    {
481
                        if (!string.IsNullOrEmpty(Prefix))
482
                            EGTAG = Prefix + "-" + EGTAG;
483
                    }
484

    
485

    
486
                    foreach (DataRow dr in pathItemRows)
487
                    {
488
                        dr["EqpGroupTag"] = EGTAG;
489
                        dr["EGFlowDirection"] = EGFlowDirection;
490
                    }
491
                }
492

    
493
                // 3, 4번
494
                foreach (DataRow dataRow in pumpRows) 
495
                {
496

    
497
                    DataRow[] pathItemRows = PathItems.Select(string.Format("PipeSystemNetwork_OID = '{0}'", dataRow["OID"].ToString()));
498
                    PSNItem PSNItem = PSNItems.Find(x => x.PSN_OID() == dataRow["OID"].ToString());
499
                    string EGFlowDirection = string.Empty;
500

    
501
                    if (PSNItem.Groups.First().Items.First().Equipment != null)
502
                    {
503
                        if (EquipmentAirFinCooler.EquipmentAirFinCoolerItem.Where(x => x.Type == "Pump" && PSNItem.Groups.First().Items.First().Equipment.Name.Contains(x.Name)).Count() > 0)
504
                        {
505
                            EGFlowDirection = "O";
506
                        }
507
                    }
508
                    
509
                    if (string.IsNullOrEmpty(EGFlowDirection) && PSNItem.Groups.Last().Items.Last().Equipment != null)
510
                    {
511
                        if (EquipmentAirFinCooler.EquipmentAirFinCoolerItem.Where(x => x.Type == "Pump" && PSNItem.Groups.Last().Items.Last().Equipment.Name.Contains(x.Name)).Count() > 0)
512
                        {
513
                            EGFlowDirection = "I";
514
                        }
515
                    }
516

    
517

    
518
                    List<string> lstViewPipeSystemNetwork_OID = new List<string>();
519

    
520
                    foreach (DataRow dr in pathItemRows)
521
                    {
522
                        if(dr.Field<string>("ViewPipeSystemNetwork_OID") != dataRow["OID"].ToString())
523
                        {
524
                            string viewEGFlowDirection = string.Empty;
525
                            if (PipeSystemNetwork.Select(string.Format("OID = '{0}'", dr.Field<string>("ViewPipeSystemNetwork_OID"))).Count() > 0)
526
                            {
527
                                PSNItem viewPSNItem = PSNItems.Find(x => x.PSN_OID() == dr.Field<string>("ViewPipeSystemNetwork_OID"));                               
528

    
529
                                if (viewPSNItem.Groups.First().Items.First().Equipment != null)
530
                                {
531
                                    if (EquipmentAirFinCooler.EquipmentAirFinCoolerItem.Where(x => x.Type == "Pump" && viewPSNItem.Groups.First().Items.First().Equipment.Name.Contains(x.Name)).Count() > 0)
532
                                    {
533
                                        viewEGFlowDirection = "O";
534
                                    }
535
                                }
536
                                
537
                                
538
                                if (string.IsNullOrEmpty(viewEGFlowDirection) && viewPSNItem.Groups.Last().Items.Last().Equipment != null)
539
                                {
540
                                    if (EquipmentAirFinCooler.EquipmentAirFinCoolerItem.Where(x => x.Type == "Pump" && viewPSNItem.Groups.Last().Items.Last().Equipment.Name.Contains(x.Name)).Count() > 0)
541
                                    {
542
                                        viewEGFlowDirection = "I";
543
                                    }
544
                                }
545

    
546
                                if (EGFlowDirection.Equals(viewEGFlowDirection) && !lstViewPipeSystemNetwork_OID.Contains(dr.Field<string>("ViewPipeSystemNetwork_OID")))
547
                                    lstViewPipeSystemNetwork_OID.Add(dr.Field<string>("ViewPipeSystemNetwork_OID"));
548
                            }
549
                            else
550
                            {
551
                            }
552
                        }
553
                    }
554

    
555
                    string selectViewOID = string.Empty;
556

    
557
                    if (EGFlowDirection == "O") //From 이면 시작점에서 제일 먼 값
558
                    {
559
                        foreach (string viewOID in lstViewPipeSystemNetwork_OID)
560
                        {
561
                            if (PipeSystemNetwork.Select(string.Format("PUMP = 'PUMP' AND OID = '{0}'", viewOID)).Count() > 0)
562
                            {                                
563
                                selectViewOID = pathItemRows.Where(x => x.Field<string>("ViewPipeSystemNetwork_OID") == viewOID).Last().Field<string>("OID");
564
                            }
565
                        }
566
                    }
567
                    else if (EGFlowDirection == "I") //To 이면 시작점에서 제일 가까운 값
568
                    {
569
                        foreach (string viewOID in lstViewPipeSystemNetwork_OID)
570
                        {
571
                            if (PipeSystemNetwork.Select(string.Format("PUMP = 'PUMP' AND OID = '{0}'", viewOID)).Count() > 0)
572
                            {
573
                                selectViewOID = pathItemRows.Where(x => x.Field<string>("ViewPipeSystemNetwork_OID") == viewOID).Last().Field<string>("OID");
574
                                break;
575
                            }
576
                        }
577
                    }
578

    
579
                    if (!string.IsNullOrEmpty(selectViewOID)) //selectViewOID 가 있으면
580
                    {              
581
                        string EqpGroupTag = string.Empty;                       
582

    
583
                        if((EGFlowDirection == "O" && lstViewPipeSystemNetwork_OID.Contains(pathItemRows.Last().Field<string>("ViewPipeSystemNetwork_OID"))) ||
584
                            (EGFlowDirection == "I" && lstViewPipeSystemNetwork_OID.Contains(pathItemRows.First().Field<string>("ViewPipeSystemNetwork_OID"))))
585
                        { 
586
                            DataRow[] viewpathItemRows = PathItems.Select(string.Format("PipeSystemNetwork_OID = '{0}'", pathItemRows.First().Field<string>("ViewPipeSystemNetwork_OID")));
587

    
588
                            foreach (DataRow row in viewpathItemRows)
589
                            {
590
                                if (!string.IsNullOrEmpty(row.Field<string>("EqpGroupTag")))
591
                                {
592
                                    EqpGroupTag = row.Field<string>("EqpGroupTag");
593
                                    break;
594
                                }
595
                            }
596

    
597
                            foreach (DataRow dr in pathItemRows)
598
                            {
599
                                dr["EqpGroupTag"] = EqpGroupTag;
600
                            }
601

    
602
                        }
603
                        else
604
                        {
605
                            bool bCheck = false;
606
                            if (EGFlowDirection == "I") //To 일때
607
                            {
608
                                foreach (DataRow dr in pathItemRows)
609
                                {
610
                                    if (selectViewOID == dr["OID"].ToString())
611
                                    {
612
                                        dr["EGTConnectedPoint"] = "1";
613
                                        bCheck = true;
614
                                    }
615

    
616
                                    if (!bCheck)
617
                                    {
618
                                        dr["EqpGroupTag"] = string.Empty;
619
                                        dr["MainLineTag"] = string.Empty;
620
                                        dr["EGTConnectedPoint"] = "0";
621
                                        dr["EGFlowDirection"] = string.Empty;
622
                                    }
623
                                    else
624
                                    {
625
                                        dr["MainLineTag"] = "M";
626
                                    }
627
                                    
628
                                }
629

    
630
                            }
631
                            else if (EGFlowDirection == "O") //From 일 때
632
                            {
633
                                foreach (DataRow dr in pathItemRows)
634
                                {
635
                                    if (bCheck)
636
                                    {
637
                                        
638
                                        dr["EqpGroupTag"] = string.Empty;
639
                                        dr["MainLineTag"] = string.Empty;
640
                                        dr["EGTConnectedPoint"] = "0";
641
                                        dr["EGFlowDirection"] = string.Empty;
642
                                    }
643
                                    else
644
                                    {
645
                                        dr["MainLineTag"] = "M";
646
                                    }
647

    
648
                                    if (selectViewOID == dr["OID"].ToString())
649
                                    {
650
                                        dr["EGTConnectedPoint"] = "1";
651
                                        bCheck = true;
652
                                    }
653
                                }
654
                            }
655
                        }
656
                    }
657
                    else
658
                    {
659
                        //foreach (DataRow dr in pathItemRows)
660
                        //{
661
                        //    dr["EqpGroupTag"] = string.Empty;
662
                        //    dr["EGFlowDirection"] = string.Empty;
663
                        //    dr["EGTConnectedPoint"] = "0";
664
                        //    dr["MainLineTag"] = string.Empty;
665
                        //}
666
                    }
667
                }
668
                                
669
                // 5번
670
                foreach (DataRow dataRow in pumpRows)
671
                {
672
                    DataRow[] pathItemRows = PathItems.Select(string.Format("PipeSystemNetwork_OID = '{0}'", dataRow["OID"].ToString()));
673

    
674
                    bool bCheck = false;
675
                    string EqpGroupTag = string.Empty;
676

    
677
                    string EGFlowDirection = string.Empty;                   
678

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

    
681
                    if (PSNItem.Groups.First().Items.First().Equipment != null)
682
                    {
683
                        if (EquipmentAirFinCooler.EquipmentAirFinCoolerItem.Where(x => x.Type == "Pump" && PSNItem.Groups.First().Items.First().Equipment.Name.Contains(x.Name)).Count() > 0)
684
                        {
685
                            EGFlowDirection = "O";
686
                        }
687
                    }
688
                    
689
                    if (string.IsNullOrEmpty(EGFlowDirection) && PSNItem.Groups.Last().Items.Last().Equipment != null)
690
                    {
691
                        if (EquipmentAirFinCooler.EquipmentAirFinCoolerItem.Where(x => x.Type == "Pump" && PSNItem.Groups.Last().Items.Last().Equipment.Name.Contains(x.Name)).Count() > 0)
692
                        {
693
                            EGFlowDirection = "I";
694
                        }
695
                    }
696

    
697
                    List<string> lstViewPipeSystemNetwork_OID = new List<string>();
698
                    List<string> lstEqTagRows = new List<string>();
699
                    if (EGFlowDirection.Equals("I"))
700
                    {
701
                        foreach (DataRow dr in pathItemRows)
702
                        {                            
703
                            if (!string.IsNullOrEmpty(dr.Field<string>("MainLineTag")) && dr.Field<string>("MainLineTag").Equals("M") && !string.IsNullOrEmpty(dr.Field<string>("EqpGroupTag")))
704
                            {
705
                                bCheck = true;
706
                                EqpGroupTag = dr.Field<string>("EqpGroupTag");
707
                                if(!lstEqTagRows.Contains(EqpGroupTag))
708
                                    lstEqTagRows.Add(EqpGroupTag);
709

    
710
                                if(dataRow["OID"].ToString() != dr.Field<string>("ViewPipeSystemNetwork_OID"))
711
                                {
712
                                    PSNItem viewPSNItem = PSNItems.Find(x => x.PSN_OID() == dr.Field<string>("ViewPipeSystemNetwork_OID"));
713
                                    if (viewPSNItem.Groups.Last().Items.Last().Equipment == null)
714
                                        continue;
715

    
716
                                    if (!lstEqTagRows.Contains(viewPSNItem.Groups.Last().Items.Last().Equipment.ItemTag))
717
                                        lstEqTagRows.Add(viewPSNItem.Groups.Last().Items.Last().Equipment.ItemTag);
718
                                }
719
                                
720
                            }
721

    
722
                        }
723
                        if(bCheck)
724
                        {
725
                            foreach (DataRow row in pumpRows)
726
                            {
727
                                if (row.Field<string>("OID").Equals(dataRow["OID"].ToString()))
728
                                    continue;
729

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

    
732
                                if (viewPSNItem.Groups.First().Items.First().Equipment == null)
733
                                    continue;
734
                                
735
                                if (lstEqTagRows.Contains(viewPSNItem.Groups.First().Items.First().Equipment.ItemTag) && !lstViewPipeSystemNetwork_OID.Contains(row.Field<string>("OID")))
736
                                    lstViewPipeSystemNetwork_OID.Add(row.Field<string>("OID"));
737
                            }
738

    
739
                                
740

    
741
                            if (lstViewPipeSystemNetwork_OID.Count() > 0)
742
                            {
743
                                foreach (string viewPipesystem in lstViewPipeSystemNetwork_OID)
744
                                {
745
                                    DataRow[] viewpathItemRows = PathItems.Select(string.Format("PipeSystemNetwork_OID = '{0}'", viewPipesystem));
746
                                    foreach (DataRow viewdr in viewpathItemRows)
747
                                    {
748
                                        if (!string.IsNullOrEmpty(viewdr["EqpGroupTag"].ToString()))
749
                                            viewdr["EqpGroupTag"] = EqpGroupTag;
750
                                    }
751
                                }
752
                            }
753
                        }                        
754
                    }                   
755
                }
756
                
757
                int afcTagNum = 0;
758
                DataRow[] airFinCoolerRows = PipeSystemNetwork.Select("AFC Like 'P1%'");
759
                Dictionary<string, string> keyValuePairs = new Dictionary<string, string>();
760
                foreach (DataRow dataRow in airFinCoolerRows)
761
                {
762
                    string EGFlowDirection = string.Empty;
763
                    string EGTAG = "EGTAG";
764
                    string Prefix = string.Empty;
765
                    string SymbolName = string.Empty;
766

    
767
                    //item.Attributes.Find(x => x.Name == valveitem.AttributeName).Value;
768
                    PSNItem PSNItem = PSNItems.Find(x => x.PSN_OID() == dataRow["OID"].ToString());
769
                    if (PSNItem.Groups.First().Items.First().Equipment != null)
770
                    {
771
                        if (EquipmentAirFinCooler.EquipmentAirFinCoolerItem.Where(x => x.Type != "Pump" && PSNItem.Groups.First().Items.First().Equipment.Name.Contains(x.Name)).Count() > 0)
772
                        {
773
                            EquipmentAirFinCoolerItem equipmentAirFinCoolerItem = EquipmentAirFinCooler.EquipmentAirFinCoolerItem.Where(x => x.Type != "Pump" && PSNItem.Groups.First().Items.First().Equipment.Name.Contains(x.Name)).First();
774
                            if(!string.IsNullOrEmpty(equipmentAirFinCoolerItem.AttributeName))
775
                                EGTAG = PSNItem.Groups.First().Items.First().Equipment.Attributes.Find(x => x.Name == equipmentAirFinCoolerItem.AttributeName) == null 
776
                                    ? string.Empty : PSNItem.Groups.First().Items.First().Equipment.Attributes.Find(x => x.Name == equipmentAirFinCoolerItem.AttributeName).Value;
777

    
778
                            Prefix = equipmentAirFinCoolerItem.TagIdentifier;
779
                            EGFlowDirection = "O";
780
                        }
781
                    }
782

    
783
                    if (string.IsNullOrEmpty(EGFlowDirection) && PSNItem.Groups.Last().Items.Last().Equipment != null)
784
                    {
785
                        if (EquipmentAirFinCooler.EquipmentAirFinCoolerItem.Where(x => x.Type != "Pump" && PSNItem.Groups.Last().Items.Last().Equipment.Name.Contains(x.Name)).Count() > 0)
786
                        {
787
                            EquipmentAirFinCoolerItem equipmentAirFinCoolerItem = EquipmentAirFinCooler.EquipmentAirFinCoolerItem.Where(x => x.Type != "Pump" && PSNItem.Groups.Last().Items.Last().Equipment.Name.Contains(x.Name)).First();
788
                            if (!string.IsNullOrEmpty(equipmentAirFinCoolerItem.AttributeName))
789
                                EGTAG = PSNItem.Groups.Last().Items.Last().Equipment.Attributes.Find(x => x.Name == equipmentAirFinCoolerItem.AttributeName) == null 
790
                                    ? string.Empty : PSNItem.Groups.Last().Items.Last().Equipment.Attributes.Find(x => x.Name == equipmentAirFinCoolerItem.AttributeName).Value;
791

    
792
                            Prefix = equipmentAirFinCoolerItem.TagIdentifier;
793
                            EGFlowDirection = "I";
794
                        }
795
                    }
796

    
797
                    if(!string.IsNullOrEmpty(EGFlowDirection))
798
                    {
799
                        string[] afcTag = dataRow.Field<string>("AFC").Split(new string[] { "\\" }, StringSplitOptions.None);
800

    
801
                        //Attribute가 세팅되어있지 않다면
802
                        if (EGTAG.Equals("EGTAG"))
803
                        {                          
804
                            if (!keyValuePairs.ContainsKey(afcTag[1]))
805
                            {
806
                                if (!string.IsNullOrEmpty(Prefix))
807
                                {
808
                                    afcTagNum++;
809
                                    EGTAG = Prefix + string.Format("-{0}", string.Format("{0:D5}", afcTagNum));
810
                                    keyValuePairs.Add(afcTag[1], EGTAG);
811
                                }
812
                                else
813
                                {
814
                                    keyValuePairs.Add(afcTag[1], afcTag[1]);
815

    
816
                                }
817
                            }
818
                            else
819
                            {
820
                                EGTAG = keyValuePairs[afcTag[1]];
821
                            }
822
                            
823
                        }
824
                        else
825
                        {
826
                            if (!string.IsNullOrEmpty(Prefix))
827
                                EGTAG = Prefix + "-" + EGTAG;                          
828
                        }
829

    
830
                        DataRow[] pathItemRows = PathItems.Select(string.Format("PipeSystemNetwork_OID = '{0}'", dataRow["OID"].ToString()));
831
                        foreach (DataRow dr in pathItemRows)
832
                        {
833
                            dr["EqpGroupTag"] = EGTAG;
834
                            dr["EGFlowDirection"] = EGFlowDirection;
835
                        }
836
                    }
837
                    
838
                }
839

    
840
                List<string> changePSN = new List<string>();
841
                foreach (DataRow dataRow in airFinCoolerRows)
842
                {
843
                    if (changePSN.Contains(dataRow["OID"].ToString()))
844
                        continue;
845

    
846
                    DataRow[] pathItemRows = PathItems.Select(string.Format("PipeSystemNetwork_OID = '{0}'", dataRow["OID"].ToString()));
847
                    string AFCTag = string.Empty;
848
                    if (pathItemRows.Where(x => !string.IsNullOrEmpty(x.Field<string>("EqpGroupTag"))).Count() > 0)
849
                        AFCTag = pathItemRows.Where(x => !string.IsNullOrEmpty(x.Field<string>("EqpGroupTag"))).First().Field<string>("EqpGroupTag");
850

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

    
853
                    if (dataRow["Type"].ToString() == "E2E")
854
                    {
855
                        foreach (DataRow dr in pathItemRows)
856
                        {
857
                            dr["MainLineTag"] = "M";
858
                        }
859
                        dataRow["AFC"] = "P3";
860
                    }
861
                    else if (dataRow["Type"].ToString() == "E2B" || dataRow["Type"].ToString() == "B2E")
862
                    {
863
                        foreach (string lstoid in lstViewPipeSystemNetwork_OID)
864
                        {
865
                            DataRow[] p2psn = PipeSystemNetwork.Select(string.Format("OID = '{0}' AND AFC = 'P2'", lstoid));
866

    
867
                            if (p2psn.Count() > 0)
868
                            {
869
                                DataRow[] viewPathItemRows = PathItems.Select(string.Format("PipeSystemNetwork_OID = '{0}'", lstoid));
870
                                List<string> lstview = viewPathItemRows.Select(x => x.Field<string>("ViewPipeSystemNetwork_OID")).Distinct().ToList();
871
                                lstview.Remove(dataRow["OID"].ToString()); // P1인 자신 제거
872
                                lstview.Remove(lstoid); // P2 자신 제거
873

    
874
                                List<string> lstnewpipe = new List<string>();
875
                                foreach (string lstvw in lstview) //자신 외 P1이 있는지 확인
876
                                {
877
                                    if (airFinCoolerRows.Where(x => x.Field<string>("OID").Equals(lstvw)).Count() > 0) //P1이면 리스트에 추가
878
                                        lstnewpipe.Add(lstvw);
879
                                }
880

    
881
                                if (lstnewpipe.Count() == 0)
882
                                {
883
                                    foreach (DataRow dr in pathItemRows)
884
                                    {
885
                                        dr["MainLineTag"] = "M";
886
                                    }
887
                                    dataRow["AFC"] = "P3";
888
                                }
889
                                else
890
                                {
891
                                    // P2에 ML값 AGT값 지정
892
                                    DataRow[] viewpathItemRows = PathItems.Select(string.Format("PipeSystemNetwork_OID = '{0}'", lstoid));
893
                                    DataRow[] pipesystemRows = PipeSystemNetwork.Select(string.Format("OID = '{0}'", lstoid));
894
                                    foreach (DataRow viewdr in pipesystemRows)
895
                                    {
896
                                        viewdr["AFC"] = "P4";
897
                                    }
898

    
899
                                    foreach (DataRow viewdr in viewpathItemRows)
900
                                    {
901
                                        viewdr["EqpGroupTag"] = AFCTag;
902
                                        viewdr["MainLineTag"] = "M";
903
                                    }
904
                                    // 연결된 모든 P1들의 AGT값 치환
905
                                    foreach (string pipe in lstnewpipe)
906
                                    {
907
                                        viewpathItemRows = PathItems.Select(string.Format("PipeSystemNetwork_OID = '{0}'", pipe));
908
                                        foreach (DataRow viewdr in viewpathItemRows)
909
                                        {
910
                                            viewdr["EqpGroupTag"] = AFCTag;
911
                                        }
912
                                    }
913
                                }
914
                            }                               
915
                        }   
916
                    }
917
                }
918
                
919
                //P3을 제외한 P1
920
                airFinCoolerRows = PipeSystemNetwork.Select("AFC Like 'P1%'");
921
                foreach (DataRow dataRow in airFinCoolerRows)
922
                {
923
                    DataRow[] pathItemRows = PathItems.Select(string.Format("PipeSystemNetwork_OID = '{0}'", dataRow["OID"].ToString()));
924
                    
925
                    if (pathItemRows.Count() > 0)
926
                    {
927
                        List<string> lstViewPipeSystemNetwork_OID = pathItemRows.Select(x => x.Field<string>("ViewPipeSystemNetwork_OID")).Distinct().ToList();
928
                        List<string> lstpsn = new List<string>();
929
                        List<string> lstAll = new List<string>();
930
                        string EqpGroupTag = string.Empty;
931
                        foreach (string viewOID in lstViewPipeSystemNetwork_OID)
932
                        {
933
                            if (dataRow["OID"].ToString() == viewOID)
934
                            {
935
                                lstAll.Add(viewOID);
936
                                continue;
937
                            }
938

    
939
                            //P3이면
940
                            DataRow viewPSN = null;
941
                            if (PipeSystemNetwork.Select(string.Format("OID = '{0}' AND AFC = 'P3'", viewOID)).Count() > 0)
942
                                viewPSN = PipeSystemNetwork.Select(string.Format("OID = '{0}' AND AFC = 'P3'", viewOID)).First();
943
                            
944
                            if (viewPSN != null)
945
                            {
946
                                //P3의 AGT를 가져와 P1에 입력
947
                                if(string.IsNullOrEmpty(EqpGroupTag))
948
                                    EqpGroupTag = PathItems.Select(string.Format("PipeSystemNetwork_OID = '{0}'", viewOID)).First().Field<string>("EqpGroupTag");
949

    
950
                                foreach (DataRow dr in pathItemRows)
951
                                {
952
                                    dr["EqpGroupTag"] = EqpGroupTag;
953

    
954
                                    //P1의 AGT와 같은 모든 AGT를 P3의 AGT로 변경하기위해 
955
                                    if (!string.IsNullOrEmpty(dr.Field<string>("ViewPipeSystemNetwork_OID")) && !lstpsn.Contains(dr.Field<string>("ViewPipeSystemNetwork_OID")))
956
                                    {
957
                                        //AND MainLineTag = ''
958
                                        if (PathItems.Select(string.Format("PipeSystemNetwork_OID = '{0}' ", dr.Field<string>("ViewPipeSystemNetwork_OID"))).Count() > 0)
959
                                        {
960
                                            lstpsn.Add(dr.Field<string>("ViewPipeSystemNetwork_OID"));
961
                                            lstAll.Add(dr.Field<string>("ViewPipeSystemNetwork_OID"));
962
                                        }
963
                                    }
964
                                }
965
                            }                            
966
                        }
967

    
968
                        while (lstpsn.Count() != 0)
969
                        {                            
970
                            DataRow[] rule4pathItems = PathItems.Select(string.Format("PipeSystemNetwork_OID = '{0}'", lstpsn[0]));
971
                            foreach (DataRow dr in rule4pathItems)
972
                            {
973
                                if (!string.IsNullOrEmpty(dr.Field<string>("ViewPipeSystemNetwork_OID")) && !lstAll.Contains(dr.Field<string>("ViewPipeSystemNetwork_OID")))
974
                                {
975
                                    DataRow viewPSN = null;
976

    
977
                                    if (airFinCoolerRows.Where(x => x.Field<string>("OID") == dr.Field<string>("ViewPipeSystemNetwork_OID")).Count() > 0)
978
                                        viewPSN = airFinCoolerRows.Where(x => x.Field<string>("OID") == dr.Field<string>("ViewPipeSystemNetwork_OID")).First();
979
                                                                        
980
                                    if (viewPSN != null)
981
                                    {
982
                                        lstpsn.Add(dr.Field<string>("ViewPipeSystemNetwork_OID"));
983
                                        lstAll.Add(dr.Field<string>("ViewPipeSystemNetwork_OID"));
984
                                    }
985
                                }                                
986

    
987
                                dr["EqpGroupTag"] = EqpGroupTag;
988

    
989
                            }
990
                            lstpsn.Remove(lstpsn[0]);
991
                        }
992
                    }
993
                    
994
                }
995

    
996
                foreach(DataRow dr in PipeSystemNetwork.Rows)
997
                {
998
                    DataRow[] pathItemRows = PathItems.Select(string.Format("PipeSystemNetwork_OID = '{0}' AND MainLineTag = 'M'", dr["OID"].ToString()));
999
                    if(pathItemRows.Count() > 0)
1000
                    {
1001
                        if(dr["Type"].ToString() == "HD2")
1002
                        {
1003
                            List<string> lstViewPipeSystemNetwork_OID = pathItemRows.Select(x => x.Field<string>("ViewPipeSystemNetwork_OID")).Distinct().ToList();
1004
                            foreach(string viewpsn in lstViewPipeSystemNetwork_OID)
1005
                            {                              
1006
                                if (PipeSystemNetwork.Select(string.Format("OID = '{0}' AND AFC = 'P2'", viewpsn)).Count() > 0)
1007
                                {
1008
                                    foreach(DataRow dataRow in pathItemRows.Where(x => x.Field<string>("ViewPipeSystemNetwork_OID") == viewpsn))
1009
                                    {
1010
                                        dataRow["EGTConnectedPoint"] = "1";
1011
                                    }
1012
                                }
1013
                            }
1014
                        }
1015
                        else if(dr["Type"].ToString() == "E2B" || dr["Type"].ToString() == "B2E" || dr["Type"].ToString() == "E2E")
1016
                        {
1017
                            PSNItem PSNItem = PSNItems.Find(x => x.PSN_OID() == dr["OID"].ToString());
1018
                            string EGFlowDirection = string.Empty;
1019
                            if (PSNItem.Groups.First().Items.First().Equipment != null)
1020
                            {
1021
                                if (EquipmentAirFinCooler.EquipmentAirFinCoolerItem.Where(x => x.Type != "Pump" && PSNItem.Groups.First().Items.First().Equipment.Name.Contains(x.Name)).Count() > 0)
1022
                                {
1023
                                    EquipmentAirFinCoolerItem equipmentAirFinCoolerItem = EquipmentAirFinCooler.EquipmentAirFinCoolerItem.Where(x => x.Type != "Pump" && PSNItem.Groups.First().Items.First().Equipment.Name.Contains(x.Name)).First();
1024
                                    if (equipmentAirFinCoolerItem != null)
1025
                                        EGFlowDirection = "O";
1026
                                }
1027
                            }
1028
                            
1029
                            if (string.IsNullOrEmpty(EGFlowDirection) && PSNItem.Groups.Last().Items.Last().Equipment != null)
1030
                            {
1031
                                if (EquipmentAirFinCooler.EquipmentAirFinCoolerItem.Where(x => x.Type != "Pump" && PSNItem.Groups.Last().Items.Last().Equipment.Name.Contains(x.Name)).Count() > 0)
1032
                                {
1033
                                    EquipmentAirFinCoolerItem equipmentAirFinCoolerItem = EquipmentAirFinCooler.EquipmentAirFinCoolerItem.Where(x => x.Type != "Pump" && PSNItem.Groups.Last().Items.Last().Equipment.Name.Contains(x.Name)).First();
1034
                                    if(equipmentAirFinCoolerItem != null)
1035
                                        EGFlowDirection = "I";
1036
                                }
1037
                            }
1038

    
1039
                            if (!string.IsNullOrEmpty(EGFlowDirection))
1040
                            {
1041
                                List<string> lstViewPipeSystemNetwork_OID = pathItemRows.Select(x => x.Field<string>("ViewPipeSystemNetwork_OID")).Distinct().ToList();
1042
                                string lastP1 = string.Empty;
1043

    
1044
                                foreach (string viewpsn in lstViewPipeSystemNetwork_OID)
1045
                                {
1046
                                    if (viewpsn == dr["OID"].ToString())
1047
                                        continue;
1048

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

    
1052
                                    DataRow rows = PipeSystemNetwork.Select(string.Format("OID = '{0}' AND AFC Like 'P1%'", viewpsn)).First();
1053

    
1054
                                    if (rows != null)
1055
                                    {
1056
                                        lastP1 = viewpsn;
1057
                                        if (EGFlowDirection.Equals("I")) // To         
1058
                                            break;
1059
                                    }
1060
                                }
1061

    
1062
                                if (!string.IsNullOrEmpty(lastP1))
1063
                                {
1064
                                    bool bCheck = false;
1065
                                    if (EGFlowDirection.Equals("O")) // From
1066
                                    {
1067
                                        foreach (DataRow dataRow in pathItemRows)
1068
                                        {
1069
                                            if (bCheck)
1070
                                            {
1071
                                                dataRow["EqpGroupTag"] = string.Empty;
1072
                                                dataRow["MainLineTag"] = string.Empty;
1073
                                                dataRow["EGTConnectedPoint"] = 0;
1074
                                                dataRow["EGFlowDirection"] = string.Empty;
1075
                                            }
1076

    
1077
                                            if (dataRow.Field<string>("ViewPipeSystemNetwork_OID").Equals(lastP1))
1078
                                            {
1079
                                                bCheck = true;
1080
                                                dataRow["EGTConnectedPoint"] = 1;
1081
                                            }
1082
                                        }
1083
                                    }
1084
                                    else
1085
                                    {
1086
                                        foreach (DataRow dataRow in pathItemRows)
1087
                                        {
1088
                                            if (dataRow.Field<string>("ViewPipeSystemNetwork_OID").Equals(lastP1))
1089
                                            {
1090
                                                dataRow["EGTConnectedPoint"] = 1;
1091
                                                break;
1092
                                            }
1093
                                            
1094
                                            dataRow["EqpGroupTag"] = string.Empty;
1095
                                            dataRow["MainLineTag"] = string.Empty;
1096
                                            dataRow["EGTConnectedPoint"] = 0;
1097
                                            dataRow["EGFlowDirection"] = string.Empty;
1098

    
1099
                                        }
1100
                                    }
1101
                                }
1102
                            }                          
1103
                        }
1104
                    }
1105
                }
1106

    
1107
                //psn data 정리
1108
                foreach(DataRow pipesystem in PipeSystemNetwork.Rows)
1109
                {
1110
                    DataRow[] pathItemRows = PathItems.Select(string.Format("PipeSystemNetwork_OID = '{0}'", pipesystem["OID"].ToString()));
1111
                    string EGTag = string.Empty;
1112
                    string HasMLTags = "False";
1113

    
1114
                    foreach (DataRow dataRow in pathItemRows)
1115
                    {
1116
                        if (string.IsNullOrEmpty(EGTag) && !string.IsNullOrEmpty(dataRow.Field<string>("EqpGroupTag")))
1117
                            EGTag = dataRow.Field<string>("EqpGroupTag");
1118

    
1119
                        if (HasMLTags.Equals("False") && !string.IsNullOrEmpty(dataRow.Field<string>("MainLineTag")) && dataRow.Field<string>("MainLineTag").Equals("M"))
1120
                            HasMLTags = "True";
1121

    
1122
                        if (!string.IsNullOrEmpty(EGTag) && HasMLTags == "True")
1123
                            break;
1124
                    }
1125

    
1126
                    pipesystem["EGTag"] = EGTag;
1127
                    pipesystem["HasMLTags"] = HasMLTags;
1128
                }
1129
            }
1130
            catch (Exception ex)
1131
            {
1132
                MessageBox.Show(ex.Message, "ID2 ", MessageBoxButtons.OK, MessageBoxIcon.Error);
1133
            }
1134
            //}
1135
        }
1136

    
1137
        private void UpdateValveGrouping()
1138
        {
1139
            try
1140
            {
1141
                #region ValveGrouping Info
1142
                ValveGroupInfo ValveGrouping = new ValveGroupInfo();
1143
                DataTable dtValveGroupung = DB.SelectValveGroupItemsSetting();
1144
                foreach (DataRow row in dtValveGroupung.Rows)
1145
                {
1146
                    ValveGrouping.ValveGroupItems.Add(new ValveGroupItem()
1147
                    {
1148
                        OID = row["OID"].ToString(),
1149
                        GroupType = row["GroupType"].ToString(),
1150
                        TagIdentifier = row["TagIdentifier"].ToString(),
1151
                        AttributeName = row["AttributeName"].ToString(),
1152
                        SppidSymbolName = row["SppidSymbolName"].ToString()
1153
                    });
1154
                }
1155
                #endregion
1156

    
1157

    
1158
                int vgTagNum = 1;
1159
                DataRow[] tagpathItemRows = PathItems.Select(string.Format("GROUPTAG Like '%\\%'"));
1160
                List<string> pathOid = new List<string>();
1161
                foreach (DataRow drPathitem in tagpathItemRows)
1162
                {
1163
                    if (pathOid.Contains(drPathitem.Field<string>("OID")))
1164
                        continue;
1165

    
1166
                    string[] valvetag = drPathitem["GROUPTAG"].ToString().Split(new string[] { "\\" }, StringSplitOptions.None);
1167
                    DataRow[] pathItemRows = PathItems.Select(string.Format("PipeSystemNetwork_OID = '{0}'", drPathitem["PipeSystemNetwork_OID"].ToString()));
1168
                    ValveGroupItem valveitem = ValveGrouping.ValveGroupItems.Where(x => x.SppidSymbolName == valvetag[0]).FirstOrDefault();
1169
                    if (valveitem == null || valveitem.GroupType == "Scope Break")
1170
                        continue;
1171
                    Dictionary<int, List<DataRow>> keyValuePairs = new Dictionary<int, List<DataRow>>();
1172
                    List<Item> valveGroupingItem = new List<Item>();
1173
                    int bCnt = 0;
1174

    
1175
                    
1176
                    List<DataRow> lstitem = new List<DataRow>();
1177
                    foreach (DataRow dr in pathItemRows)
1178
                    {
1179
                        if (dr["GROUPTAG"].ToString() == "Scope Break")
1180
                        {
1181
                            dr["GROUPTAG"] = string.Empty;
1182
                            keyValuePairs.Add(bCnt, lstitem.ToList());
1183
                            bCnt++;
1184
                            lstitem.Clear();
1185
                            continue;
1186
                        }
1187
                       
1188

    
1189
                        if (!string.IsNullOrEmpty(dr["BranchTopologySet_OID"].ToString()))
1190
                        {
1191
                            DataRow[] rows = TopologySet.Select(string.Format("OID = '{0}'", dr["BranchTopologySet_OID"].ToString()));                          
1192
                           
1193
                            if (rows.First()["SubType"].ToString() != "Bypass" && rows.First()["SubType"].ToString() != "Vent_Drain")
1194
                            {
1195
                                if (lstitem.ToList().Where(x => !string.IsNullOrEmpty(x.Field<string>("GROUPTAG"))).Count() > 0)
1196
                                {
1197
                                    lstitem.Add(dr);
1198
                                    keyValuePairs.Add(bCnt, lstitem.ToList());
1199
                                    lstitem.Clear();
1200
                                }
1201
                                else
1202
                                {
1203
                                    keyValuePairs.Add(bCnt, lstitem.ToList());
1204
                                    lstitem.Clear();
1205
                                    lstitem.Add(dr);
1206
                                }
1207
                                bCnt++;
1208

    
1209
                            }
1210
                            else
1211
                                lstitem.Add(dr);
1212
                            
1213
                        }
1214
                        else
1215
                            lstitem.Add(dr);
1216
                    }
1217

    
1218
                    if (lstitem.Count > 0)
1219
                    {
1220
                        keyValuePairs.Add(bCnt, lstitem);
1221
                    }
1222

    
1223
                    if (keyValuePairs.Count() == 0)
1224
                        continue;
1225

    
1226
                    string VGTag = string.Empty;
1227
                    if (valveitem.AttributeName == "NoSelection" || valveitem.AttributeName == string.Empty)
1228
                    {
1229
                        VGTag = valveitem.TagIdentifier + string.Format("-{0}", string.Format("{0:D5}", vgTagNum));
1230
                        vgTagNum++;
1231
                    }
1232
                    else
1233
                    {                        
1234
                        VGTag = valveitem.TagIdentifier + "-" + valvetag[1];
1235
                    }
1236

    
1237
                    foreach (KeyValuePair<int, List<DataRow>> keyValuePair in keyValuePairs)
1238
                    {                       
1239
                        if (keyValuePair.Value.Where(x => x.Field<string>("OID") == drPathitem.Field<string>("OID")).Count() > 0)
1240
                        {
1241
                            if (!pathOid.Contains(drPathitem.Field<string>("OID")))
1242
                                pathOid.Add(drPathitem.Field<string>("OID"));
1243

    
1244
                            foreach (DataRow dr in keyValuePair.Value)
1245
                            {
1246
                                dr["GROUPTAG"] = VGTag;
1247

    
1248
                                if(!string.IsNullOrEmpty(dr["BranchTopologySet_OID"].ToString()))
1249
                                {
1250
                                    if(TopologySet.Select(string.Format("OID = '{0}'", dr["BranchTopologySet_OID"].ToString())).First().Field<string>("SubType") == "Bypass")
1251
                                    {
1252
                                        DataRow[] rows = keyValuePair.Value.Where(x => x.Field<string>("ViewPipeSystemNetwork_OID").Equals(dr["ViewPipeSystemNetwork_OID"].ToString())).ToArray();
1253
                                        foreach(DataRow path in rows)
1254
                                        {
1255
                                            DataRow topology = TopologySet.Select(string.Format("OID = '{0}'", path["BranchTopologySet_OID"].ToString())).First();
1256
                                            if (topology.Field<string>("SubType").Equals("Bypass"))
1257
                                            {
1258
                                                DataRow[] drPathItem = PathItems.Select(string.Format("TopologySet_OID = '{0}'", topology.Field<string>("OID")));
1259
                                                //if (dr["GROUPTAG"].ToString() == "Scope Break")
1260
                                                if(drPathItem.Where(x => x.Field<string>("GROUPTAG") == "Scope Break").Count() == 0)
1261
                                                {
1262
                                                    foreach (DataRow pathitem in drPathItem)
1263
                                                    {
1264
                                                        pathitem["GROUPTAG"] = VGTag;
1265
                                                    }
1266
                                                }
1267
                                            }
1268
                                        }
1269
                                    }
1270
                                }
1271
                            }
1272
                        }
1273
                        else
1274
                        {                            
1275
                            foreach (DataRow drs in tagpathItemRows)
1276
                            {
1277
                                if (drs.Field<string>("OID").Equals(drPathitem.Field<string>("OID")))
1278
                                    continue;
1279
                              
1280
                                if(keyValuePair.Value.Where(x => x.Field<string>("OID") == drs.Field<string>("OID")).Count() > 0)
1281
                                {
1282
                                    if (!pathOid.Contains(drs.Field<string>("OID")))
1283
                                    {
1284
                                        pathOid.Add(drs.Field<string>("OID"));
1285

    
1286
                                        valvetag = drs["GROUPTAG"].ToString().Split(new string[] { "\\" }, StringSplitOptions.None);
1287
                                        ValveGroupItem valveitems = ValveGrouping.ValveGroupItems.Where(x => x.SppidSymbolName == valvetag[0]).FirstOrDefault();
1288
                                        if (valveitems == null || valveitems.GroupType == "Scope Break")
1289
                                            continue;
1290

    
1291
                                        VGTag = string.Empty;
1292
                                        if (valveitems.AttributeName == "NoSelection" || valveitems.AttributeName == string.Empty)
1293
                                        {
1294
                                            VGTag = valveitems.TagIdentifier + string.Format("-{0}", string.Format("{0:D5}", vgTagNum));
1295
                                            vgTagNum++;
1296
                                        }
1297
                                        else
1298
                                        {
1299
                                            VGTag = valveitems.TagIdentifier + "-" + valvetag[1];
1300
                                        }
1301

    
1302
                                        foreach (DataRow dr in keyValuePair.Value)
1303
                                        {
1304
                                            dr["GROUPTAG"] = VGTag;
1305

    
1306
                                            if (!string.IsNullOrEmpty(dr["BranchTopologySet_OID"].ToString()))
1307
                                            {
1308
                                                if (TopologySet.Select(string.Format("OID = '{0}'", dr["BranchTopologySet_OID"].ToString())).First().Field<string>("SubType") == "Bypass")
1309
                                                {
1310
                                                    DataRow[] rows = keyValuePair.Value.Where(x => x.Field<string>("ViewPipeSystemNetwork_OID").Equals(dr["ViewPipeSystemNetwork_OID"].ToString())).ToArray();
1311
                                                    foreach (DataRow path in rows)
1312
                                                    {
1313
                                                        DataRow topology = TopologySet.Select(string.Format("OID = '{0}'", path["BranchTopologySet_OID"].ToString())).First();
1314
                                                        if (topology.Field<string>("SubType").Equals("Bypass"))
1315
                                                        {
1316
                                                            DataRow[] drPathItem = PathItems.Select(string.Format("TopologySet_OID = '{0}'", topology.Field<string>("OID")));
1317
                                                            if (drPathItem.Where(x => x.Field<string>("GROUPTAG") == "Scope Break").Count() == 0)
1318
                                                            {
1319
                                                                foreach (DataRow pathitem in drPathItem)
1320
                                                                {
1321
                                                                    pathitem["GROUPTAG"] = VGTag;
1322
                                                                }
1323
                                                            }
1324
                                                        }
1325
                                                    }
1326
                                                }
1327
                                            }
1328
                                        }
1329

    
1330
                                        if (valveitems.GroupType.Contains("PSV"))
1331
                                        {
1332
                                            DataRow[] psnRows = PipeSystemNetwork.Select(string.Format("OID = '{0}'", drPathitem["PipeSystemNetwork_OID"].ToString()));
1333
                                            foreach (DataRow row in psnRows)
1334
                                                row["Pocket"] = "Yes";
1335
                                        }
1336
                                    }     
1337
                                }
1338
                            }                            
1339
                        }
1340
                    }
1341

    
1342
                    if(valveitem.GroupType.Contains("PSV"))
1343
                    {
1344
                        DataRow[] psnRows = PipeSystemNetwork.Select(string.Format("OID = '{0}'", drPathitem["PipeSystemNetwork_OID"].ToString()));
1345
                        foreach (DataRow row in psnRows)
1346
                            row["Pocket"] = "Yes";
1347
                    }                    
1348
                }
1349

    
1350
                tagpathItemRows = PathItems.Select(string.Format("GROUPTAG = 'Scope Break'"));
1351
                foreach (DataRow drPathitem in tagpathItemRows)
1352
                {
1353
                    drPathitem["GROUPTAG"] = string.Empty;
1354
                }
1355
            }
1356
            catch (Exception ex)
1357
            {
1358
                MessageBox.Show(ex.Message, "ID2 ", MessageBoxButtons.OK, MessageBoxIcon.Error);
1359
            }
1360
        }
1361

    
1362
        private void SetTopologyData()
1363
        {
1364
            // 13번 excel
1365
            foreach (Group group in groups)
1366
            {
1367
                LineNumber prevLineNumber = null;
1368
                for (int i = 0; i < group.Items.Count; i++)
1369
                {
1370
                    Item item = group.Items[i];
1371
                    LineNumber lineNumber = item.Document.LineNumbers.Find(x => x.UID == item.Owner);
1372
                    if (lineNumber == null)
1373
                    {
1374
                        if (prevLineNumber != null)
1375
                        {
1376
                            if (!prevLineNumber.IsCopy)
1377
                            {
1378
                                prevLineNumber = prevLineNumber.Copy();
1379
                                prevLineNumber.MissingLineNumber1 = true;
1380
                                item.Document.LineNumbers.Add(prevLineNumber);
1381
                                //item.LineNumber.MissingLineNumber1 = true;
1382
                            }
1383
                            item.Owner = prevLineNumber.UID;
1384
                        }
1385
                    }
1386
                    else
1387
                        prevLineNumber = lineNumber;
1388
                }
1389

    
1390
                prevLineNumber = null;
1391
                for (int i = group.Items.Count - 1; i > -1; i--)
1392
                {
1393
                    Item item = group.Items[i];
1394
                    LineNumber lineNumber = item.Document.LineNumbers.Find(x => x.UID == item.Owner);
1395
                    if (lineNumber == null)
1396
                    {
1397
                        if (prevLineNumber != null)
1398
                        {
1399
                            if (!prevLineNumber.IsCopy)
1400
                            {
1401
                                prevLineNumber = prevLineNumber.Copy();
1402
                                prevLineNumber.MissingLineNumber1 = true;
1403
                                item.Document.LineNumbers.Add(prevLineNumber);
1404
                                //item.LineNumber.MissingLineNumber1 = true;                                
1405
                            }
1406

    
1407
                            item.Owner = prevLineNumber.UID;
1408
                        }
1409
                    }
1410
                    else
1411
                        prevLineNumber = lineNumber;
1412
                }
1413

    
1414
                if (prevLineNumber == null)
1415
                {
1416
                    List<LineNumber> lineNumbers = group.Document.LineNumbers.FindAll(x => !x.IsCopy);
1417
                    Random random = new Random();
1418

    
1419
                    LineNumber cLineNumber = null;
1420

    
1421
                    if (lineNumbers.Count == 0)
1422
                    {
1423
                        var _groups = groups.Where(w => w != group).ToList();
1424
                        while (cLineNumber == null)
1425
                        {
1426
                            int _index = random.Next(_groups.Count - 1);
1427
                            List<LineNumber> _lineNumbers = groups[_index].Document.LineNumbers.FindAll(x => !x.IsCopy);
1428
                            if (_lineNumbers.Count != 0)
1429
                            {
1430
                                _index = random.Next(_lineNumbers.Count - 1);
1431

    
1432
                                // Copy
1433
                                cLineNumber = _lineNumbers[_index].Copy();
1434
                            }
1435
                        }
1436
                    }
1437
                    else
1438
                    {
1439
                        int index = random.Next(lineNumbers.Count - 1);
1440

    
1441
                        // Copy
1442
                        cLineNumber = lineNumbers[index].Copy();
1443
                    }
1444

    
1445
                    cLineNumber.MissingLineNumber2 = true;
1446
                    group.Document.LineNumbers.Add(cLineNumber);
1447

    
1448
                    foreach (Item item in group.Items)
1449
                    {
1450
                        item.Owner = cLineNumber.UID;                        
1451
                    }
1452
                }
1453

    
1454
                //if (prevLineNumber == null)
1455
                //{
1456
                //    List<LineNumber> lineNumbers = group.Document.LineNumbers.FindAll(x => !x.IsCopy);
1457
                //    Random random = new Random();
1458
                //    int index = random.Next(lineNumbers.Count - 1);
1459

    
1460
                //    // Copy
1461
                //    LineNumber cLineNumber = lineNumbers[index].Copy();
1462
                //    group.Document.LineNumbers.Add(cLineNumber);
1463

    
1464
                //    foreach (Item item in group.Items)
1465
                //    {
1466
                //        item.Owner = cLineNumber.UID;
1467
                //        item.Document.MissingLineNumber2 = true;
1468
                //    }
1469
                //}
1470
            }
1471

    
1472
            foreach (Document document in Documents)
1473
            {
1474
                foreach (Item item in document.Items)
1475
                {
1476
                    item.TopologyData = string.Empty;
1477
                    item.PSNPipeLineID = string.Empty;
1478
                    List<string> pipeLineID = new List<string>();
1479
                    LineNumber lineNumber = document.LineNumbers.Find(x => x.UID == item.Owner);
1480
                    
1481
                    if (lineNumber != null)
1482
                    {
1483
                        item.LineNumber = lineNumber;
1484

    
1485
                        foreach (DataRow row in topologyRuleDT.Rows)
1486
                        {
1487
                            string uid = row["UID"].ToString();
1488
                            //if (uid == "-")
1489
                            //    pipeLineID.Add(item.TopologyData);//item.TopologyData += "-"; 
1490
                            if (uid != "-")
1491
                            {
1492
                                Attribute itemAttr = item.Attributes.Find(x => x.Name == uid);
1493

    
1494
                                Attribute attribute = lineNumber.Attributes.Find(x => x.DisplayName == uid);
1495
                                if (attribute != null && !string.IsNullOrEmpty(attribute.Value))
1496
                                {
1497
                                    pipeLineID.Add(attribute.Value);//item.TopologyData += attribute.Value;
1498
                                                                    //if (uid.Equals("Tag Seq No") && attribute.Value.ToString().Substring(attribute.Value.ToString().Length - 1, 1).Equals("V"))
1499
                                                                    //{
1500
                                    if (item.LineNumber.MissingLineNumber1 && item.LineNumber.MissingLineNumber2)
1501
                                    {
1502

    
1503
                                    }
1504

    
1505
                                }
1506
                                else
1507
                                {
1508

    
1509
                                }
1510

    
1511
                            }
1512
                        }
1513
                        
1514
                        if (topologyRuleDT.Select(string.Format("UID = '{0}'", "InsulationPurpose")) == null)
1515
                        {
1516
                            Attribute insulAttr = item.LineNumber.Attributes.Find(x => x.Name == "InsulationPurpose");
1517
                            if (insulAttr != null && !string.IsNullOrEmpty(insulAttr.Value))
1518
                                pipeLineID.Add(insulAttr.Value); //item.PSNPipeLineID = item.TopologyData + "-" + insulAttr.Value;
1519
                                                                 //else
1520
                                                                 //    item.PSNPipeLineID = item.TopologyData;
1521
                        }
1522

    
1523
                        item.PSNPipeLineID = string.Join("-", pipeLineID);
1524
                        item.TopologyData = string.Join("-", pipeLineID);
1525
                        
1526
                    }
1527
                    else
1528
                    {
1529
                        item.TopologyData = "Empty LineNumber";
1530
                        item.LineNumber = new LineNumber();
1531
                    }
1532
                }
1533
            }
1534

    
1535
            int emptyIndex = 1;
1536
            foreach (Group group in groups)
1537
            {
1538
                List<Item> groupItems = group.Items.FindAll(x => x.TopologyData == "Empty LineNumber");
1539
                if (groupItems.Count > 0)
1540
                {
1541
                    foreach (var item in groupItems)
1542
                        item.TopologyData += string.Format("-{0}", emptyIndex);
1543
                    emptyIndex++;
1544
                }
1545
            }
1546

    
1547
        }
1548

    
1549
        private void ConnectByOPC()
1550
        {
1551
            try
1552
            {
1553
                foreach (Group group in groups.FindAll(x => x.Items.Last().SubItemType == SubItemType.OPC))
1554
                {
1555
                    Item opcItem = group.Items.Last();
1556
                    if(opcDT.Select(string.Format("FromOPCUID = '{0}'", opcItem.UID)) != null)
1557
                    {
1558
                        DataRow[] fromRows = opcDT.Select(string.Format("FromOPCUID = '{0}'", opcItem.UID));
1559
                        if (fromRows.Length.Equals(1))
1560
                        {
1561
                            DataRow opcRow = fromRows.First();
1562
                            string toDrawing = opcRow["ToDrawing"].ToString();
1563
                            string toOPCUID = opcRow["ToOPCUID"].ToString();
1564

    
1565
                            Document toDocument = Documents.Find(x => x.DrawingName == toDrawing);
1566
                            if (toDocument != null)
1567
                            {
1568
                                Group toGroup = toDocument.Groups.Find(x => x.Items.Find(y => y.UID == toOPCUID) != null);
1569

    
1570
                                if (toGroup != null && opcDT.Select(string.Format("ToOPCUID = '{0}'", toGroup.Items.First().UID)) != null)
1571
                                {
1572
                                    DataRow[] toRows = opcDT.Select(string.Format("ToOPCUID = '{0}'", toGroup.Items.First().UID));
1573
                                    //1대1 매칭이 아닐때 걸림 (2개 이상일 때) 2021.11.07 
1574
                                    if (toRows.Length > 1)
1575
                                    {
1576
                                        //throw new Exception("OPC error(multi connect)");
1577
                                        MessageBox.Show("OPC error(multi connect)", "ID2 " + id2Info.ProgramName, MessageBoxButtons.OK, MessageBoxIcon.Information);
1578
                                        return;
1579
                                    }
1580
                                    group.EndGroup = toGroup;
1581
                                    toGroup.StartGroup = group;
1582
                                }
1583

    
1584
                            }
1585
                        }
1586
                    }                    
1587
                }
1588
            }
1589
            catch (Exception ex)
1590
            {
1591
                Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1592
                //MessageBox.Show(ex.Message, "ID2 " + id2Info.ProgramName, MessageBoxButtons.OK, MessageBoxIcon.Information);
1593
            }
1594
        }
1595

    
1596
        private void SetPSNItem()
1597
        {
1598
            Dictionary<Group, string> groupDic = new Dictionary<Group, string>();
1599
            foreach (Group group in groups)
1600
                groupDic.Add(group, Guid.NewGuid().ToString());
1601

    
1602
            foreach (Group group in groups)
1603
            {
1604
                string groupKey = groupDic[group];
1605
                if (group.StartGroup != null)
1606
                {
1607
                    string otherKey = groupDic[group.StartGroup];
1608
                    ChangeGroupID(otherKey, groupKey);
1609
                }
1610
            }
1611

    
1612
            // PSN 정리
1613
            foreach (var item in groupDic)
1614
            {
1615
                Group group = item.Key;
1616
                string uid = item.Value;
1617
                PSNItem PSNItem = PSNItems.Find(x => x.UID == uid);
1618
                if (PSNItem == null)
1619
                {
1620
                    PSNItem = new PSNItem(PSNItems.Count, Revision) { UID = uid };
1621
                    PSNItems.Add(PSNItem);
1622
                }
1623
                PSNItem.Groups.Add(group);
1624
                foreach (Item groupItem in group.Items)
1625
                    groupItem.PSNItem = PSNItem;
1626
            }
1627

    
1628
            // Sort PSN
1629
            foreach (PSNItem PSNItem in PSNItems)
1630
            {
1631
                List<Group> _groups = new List<Group>();
1632

    
1633
                Stack<Group> stacks = new Stack<Group>();
1634
                stacks.Push(PSNItem.Groups.First());
1635
                while (stacks.Count > 0)
1636
                {
1637
                    Group stack = stacks.Pop();
1638
                    if (_groups.Contains(stack))
1639
                        continue;
1640

    
1641
                    if (_groups.Count == 0)
1642
                        _groups.Add(stack);
1643
                    else
1644
                    {
1645
                        if (stack.StartGroup != null && _groups.Contains(stack.StartGroup))
1646
                        {
1647
                            int index = _groups.IndexOf(stack.StartGroup);
1648
                            _groups.Insert(index + 1, stack);
1649
                        }
1650
                        else if (stack.EndGroup != null && _groups.Contains(stack.EndGroup))
1651
                        {
1652
                            int index = _groups.IndexOf(stack.EndGroup);
1653
                            _groups.Insert(index, stack);
1654
                        }
1655
                    }
1656

    
1657
                    if (stack.StartGroup != null)
1658
                        stacks.Push(stack.StartGroup);
1659
                    if (stack.EndGroup != null)
1660
                        stacks.Push(stack.EndGroup);
1661
                }
1662

    
1663
                PSNItem.Groups.Clear();
1664
                PSNItem.Groups.AddRange(_groups);
1665
            }
1666

    
1667
            void ChangeGroupID(string from, string to)
1668
            {
1669
                if (from.Equals(to))
1670
                    return;
1671

    
1672
                List<Group> changeItems = new List<Group>();
1673
                foreach (var _item in groupDic)
1674
                    if (_item.Value.Equals(from))
1675
                        changeItems.Add(_item.Key);
1676
                foreach (var _item in changeItems)
1677
                    groupDic[_item] = to;
1678
            }
1679
        }
1680

    
1681
        private void SetPSNType()
1682
        {
1683
            foreach (PSNItem PSNItem in PSNItems)
1684
            {
1685
                Group firstGroup = PSNItem.Groups.First();
1686
                Group lastGroup = PSNItem.Groups.Last();
1687

    
1688
                Item firstItem = firstGroup.Items.First();
1689
                Item lastItem = lastGroup.Items.Last();
1690

    
1691
                PSNItem.StartType = GetPSNType(firstItem, true);
1692
                PSNItem.EndType = GetPSNType(lastItem, false);
1693

    
1694
            }
1695

    
1696
            PSNType GetPSNType(Item item, bool bFirst = true)
1697
            {
1698
                PSNType type = PSNType.None;
1699

    
1700
                if (item.ItemType == ItemType.Line)
1701
                {
1702
                    Group group = groups.Find(x => x.Items.Contains(item));
1703
                    if (bFirst && item.Relations[0].Item != null && !group.Items.Contains(item.Relations[0].Item))
1704
                    {
1705
                        Item connItem = item.Relations[0].Item;
1706
                        if (connItem.ItemType == ItemType.Line && !IsConnected(item, connItem))
1707
                            type = PSNType.Branch;
1708
                        else if (connItem.ItemType == ItemType.Symbol)
1709
                            type = PSNType.Symbol;
1710
                    }
1711
                    else if (!bFirst && item.Relations[1].Item != null && !group.Items.Contains(item.Relations[1].Item))
1712
                    {
1713
                        Item connItem = item.Relations[1].Item;
1714
                        if (connItem.ItemType == ItemType.Line && !IsConnected(item, connItem))
1715
                            type = PSNType.Branch;
1716
                        else if (connItem.ItemType == ItemType.Symbol)
1717
                            type = PSNType.Symbol;
1718
                    }
1719
                }
1720
                else if (item.ItemType == ItemType.Symbol)
1721
                {
1722
                    if (item.SubItemType == SubItemType.Nozzle)
1723
                        type = PSNType.Equipment;
1724
                    else if (item.SubItemType == SubItemType.Header)
1725
                        type = PSNType.Header;
1726
                    else if (item.SubItemType == SubItemType.OPC)
1727
                        type = PSNType.OPC;
1728
                }
1729
                else
1730
                {
1731

    
1732
                }
1733

    
1734
                return type;
1735
            }
1736
        }
1737

    
1738
        private void SetBranchInfo()
1739
        {
1740
            foreach (Document document in Documents)
1741
            {
1742
                List<Item> lines = document.Items.FindAll(x => x.ItemType == ItemType.Line).ToList();
1743
                foreach (Item line in lines)
1744
                {
1745
                    double[] point = line.Relations[0].Point;
1746
                    List<Item> connLines = lines.FindAll(x => x.Relations.Find(y => y.UID == line.UID) != null && line.Relations.Find(y => y.UID == x.UID) == null);
1747
                    connLines.Sort(SortBranchLine);
1748
                    line.BranchItems.AddRange(connLines);
1749

    
1750
                    int SortBranchLine(Item a, Item b)
1751
                    {
1752
                        double[] pointA = a.Relations[0].UID == line.UID ? a.Relations[0].Point : a.Relations[1].Point;
1753
                        double distanceA = CalcPointToPointdDistance(point[0], point[1], pointA[0], pointA[1]);
1754

    
1755
                        double[] pointB = b.Relations[0].UID == line.UID ? b.Relations[0].Point : b.Relations[1].Point;
1756
                        double distanceB = CalcPointToPointdDistance(point[0], point[1], pointB[0], pointB[1]);
1757

    
1758
                        // 내림차순
1759
                        return distanceA.CompareTo(distanceB);
1760
                    }
1761
                    double CalcPointToPointdDistance(double x1, double y1, double x2, double y2)
1762
                    {
1763
                        return Math.Pow(Math.Pow(x1 - x2, 2) + Math.Pow(y1 - y2, 2), 0.5);
1764
                    }
1765
                }
1766
            }
1767
        }
1768

    
1769
        private void SetTopology()
1770
        {
1771
            try
1772
            {
1773
                #region 기본 topology 정리
1774
                foreach (PSNItem PSNItem in PSNItems)
1775
                {
1776
                    Topology topology = null;
1777
                    foreach (Group group in PSNItem.Groups)
1778
                    {
1779
                        foreach (Item item in group.Items)
1780
                        {
1781
                            if (string.IsNullOrEmpty(item.TopologyData))
1782
                                topology = null;
1783
                            else
1784
                            {
1785
                                if (topology == null)
1786
                                {
1787
                                    topology = new Topology()
1788
                                    {
1789
                                        ID = item.TopologyData
1790
                                    };
1791
                                    Topologies.Add(topology);
1792

    
1793
                                    if (!PSNItem.Topologies.Contains(topology))
1794
                                        PSNItem.Topologies.Add(topology);
1795
                                }
1796
                                else
1797
                                {
1798
                                    if (topology.ID != item.TopologyData)
1799
                                    {
1800
                                        topology = new Topology()
1801
                                        {
1802
                                            ID = item.TopologyData
1803
                                        };
1804
                                        Topologies.Add(topology);
1805

    
1806
                                        if (!PSNItem.Topologies.Contains(topology))
1807
                                            PSNItem.Topologies.Add(topology);
1808
                                    }
1809
                                }
1810
                                if (topology == null)
1811
                                {
1812

    
1813
                                }
1814
                                item.Topology = topology;
1815
                                topology.Items.Add(item);
1816
                            }
1817
                        }
1818
                    }
1819
                    
1820
                }
1821
                #endregion
1822

    
1823
                #region Type
1824
                List<string> ids = Topologies.Select(x => x.ID).Distinct().ToList();
1825
                foreach (string id in ids)
1826
                {
1827
                    try
1828
                    {
1829

    
1830

    
1831
                        List<Topology> topologies = Topologies.FindAll(x => x.ID == id);
1832

    
1833
                        // Main
1834
                        List<Topology> mainTopologies = FindMainTopology(topologies);
1835
                        foreach (Topology topology in mainTopologies)
1836
                            topology.Type = "M";
1837

    
1838
                        // Branch
1839
                        List<Topology> branchToplogies = topologies.FindAll(x => string.IsNullOrEmpty(x.Type));
1840
                        foreach (Topology topology in branchToplogies)
1841
                            topology.Type = "B";
1842
                    }
1843
                    catch (Exception ex)
1844
                    {
1845
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1846
                        MessageBox.Show(ex.Message, "ID2 " + id2Info.ProgramName, MessageBoxButtons.OK, MessageBoxIcon.Information);
1847
                    }
1848
                }
1849
                #endregion
1850
            }
1851
            catch (Exception ex)
1852
            {
1853
                Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1854
                MessageBox.Show(ex.Message, "ID2 " + id2Info.ProgramName, MessageBoxButtons.OK, MessageBoxIcon.Information);
1855
            }
1856

    
1857
        }
1858

    
1859
        private void SetTopologyIndex()
1860
        {
1861
            List<string> ids = Topologies.Select(x => x.ID).Distinct().ToList();
1862
            foreach (string id in ids)
1863
            {
1864
                List<Topology> topologies = Topologies.FindAll(x => x.ID == id);
1865

    
1866
                // Main
1867
                List<Topology> mainTopologies = topologies.FindAll(x => x.Type == "M");
1868
                foreach (Topology topology in mainTopologies)
1869
                    topology.Index = mainTopologies.IndexOf(topology).ToString();
1870

    
1871
                // Branch
1872
                List<Topology> branchToplogies = topologies.FindAll(x => x.Type == "B");
1873
                foreach (Topology topology in branchToplogies)
1874
                    topology.Index = (branchToplogies.IndexOf(topology) + 1).ToString();
1875
            }
1876
        }
1877

    
1878
        private void SetPSNBypass()
1879
        {
1880
            foreach (PSNItem PSNItem in PSNItems)
1881
                PSNItem.IsBypass = IsBypass(PSNItem);
1882
        }
1883

    
1884
        private List<Topology> FindMainTopology(List<Topology> data)
1885
        {
1886
            DataTable nominalDiameterDT = DB.SelectNominalDiameter();
1887
            DataTable PMCDT = DB.SelectPSNPIPINGMATLCLASS();
1888
            //2021.11.17 안쓰네 JY
1889
            //DataTable fluidCodeDT = DB.SelectPSNFluidCode(); 
1890

    
1891
            List<Topology> main = new List<Topology>();
1892
            main.AddRange(data);
1893
            //
1894
            main = GetNozzleTopology(data);
1895
            if (main.Count == 1)
1896
                return main;
1897
            else
1898
            {
1899
                if (main.Count > 0)
1900
                    main = GetPMCTopology(main);
1901
                else
1902
                    main = GetPMCTopology(data);
1903

    
1904
                if (main.Count == 1)
1905
                    return main;
1906
                else
1907
                {
1908
                    if (main.Count > 0)
1909
                        main = GetDiaTopology(main);
1910
                    else
1911
                        main = GetDiaTopology(data);
1912

    
1913

    
1914
                    if (main.Count == 1)
1915
                        return main;
1916
                    else
1917
                    {
1918
                        if (main.Count > 0)
1919
                            main = GetItemTopology(main);
1920
                        else
1921
                            main = GetItemTopology(data);
1922
                    }
1923
                }
1924
            }
1925

    
1926
            List<Topology> GetNozzleTopology(List<Topology> topologies)
1927
            {
1928
                return topologies.FindAll(x => x.Items.Find(y => y.SubItemType == SubItemType.Nozzle) != null);
1929
            }
1930

    
1931
            List<Topology> GetPMCTopology(List<Topology> topologies)
1932
            {
1933
                List<Topology> result = new List<Topology>();
1934
                foreach (DataRow row in PMCDT.Rows)
1935
                {
1936
                    string value = row["CODE"].ToString();
1937
                    foreach (Topology topology in topologies)
1938
                    {
1939
                        foreach (Item item in topology.Items)
1940
                        {
1941
                            if (item.LineNumber == null)
1942
                                continue;
1943
                            Attribute attribute = item.LineNumber.Attributes.Find(x => x.Name == "PipingMaterialsClass");
1944
                            if (attribute != null && value == attribute.Value)
1945
                            {
1946
                                result.Add(topology);
1947
                                break;
1948
                            }
1949
                        }
1950
                    }
1951

    
1952
                    if (result.Count > 0)
1953
                        break;
1954
                }
1955

    
1956
                return result;
1957
            }
1958

    
1959
            List<Topology> GetDiaTopology(List<Topology> topologies)
1960
            {
1961
                List<Topology> result = new List<Topology>();
1962
                foreach (DataRow row in nominalDiameterDT.Rows)
1963
                {
1964
                    string inchValue = row["InchStr"].ToString();
1965
                    string metricValue = row["MetricStr"].ToString();
1966
                    foreach (Topology topology in topologies)
1967
                    {
1968
                        foreach (Item item in topology.Items)
1969
                        {
1970
                            if (item.LineNumber == null)
1971
                                continue;
1972
                            Attribute attribute = item.LineNumber.Attributes.Find(x => x.Name == "NominalDiameter");
1973
                            if (attribute != null && (inchValue == attribute.Value || metricValue == attribute.Value))
1974
                            {
1975
                                result.Add(topology);
1976
                                break;
1977
                            }
1978
                        }
1979
                    }
1980

    
1981
                    if (result.Count > 0)
1982
                        break;
1983
                }
1984

    
1985
                return result;
1986
            }
1987

    
1988
            List<Topology> GetItemTopology(List<Topology> topologies)
1989
            {
1990
                return new List<Topology>() { topologies.OrderByDescending(x => x.Items.Count).ToList().First() };
1991
            }
1992

    
1993
            return main;
1994
        }
1995

    
1996
        private DataTable GetOPCInfo()
1997
        {
1998
            DataTable opc = DB.SelectOPCRelations();
1999
            DataTable drawing = DB.AllDrawings();
2000

    
2001
            DataTable dt = new DataTable();
2002
            dt.Columns.Add("FromDrawing", typeof(string));
2003
            dt.Columns.Add("FromDrawingUID", typeof(string));
2004
            dt.Columns.Add("FromOPCUID", typeof(string));
2005
            dt.Columns.Add("ToDrawing", typeof(string));
2006
            dt.Columns.Add("ToDrawingUID", typeof(string));
2007
            dt.Columns.Add("ToOPCUID", typeof(string));
2008
            foreach (DataRow row in opc.Rows)
2009
            {
2010
                string fromDrawingUID = row["From_Drawings_UID"] == null ? string.Empty : row["From_Drawings_UID"].ToString();
2011
                string fromOPCUID = row["From_OPC_UID"] == null ? string.Empty : row["From_OPC_UID"].ToString();
2012
                string toDrawingUID = row["To_Drawings_UID"] == null ? string.Empty : row["To_Drawings_UID"].ToString();
2013
                string toOPCUID = row["To_OPC_UID"] == null ? string.Empty : row["To_OPC_UID"].ToString();
2014
                if (!string.IsNullOrEmpty(toOPCUID))
2015
                {
2016
                    DataRow[] fromRows = drawing.Select(string.Format("UID = '{0}'", fromDrawingUID));
2017
                    DataRow[] toRows = drawing.Select(string.Format("UID = '{0}'", toDrawingUID));
2018
                    if (fromRows.Length.Equals(1) && toRows.Length.Equals(1))
2019
                    {
2020
                        string fromDrawingName = Path.GetFileNameWithoutExtension(fromRows.First()["NAME"].ToString());
2021
                        string toDrawingName = Path.GetFileNameWithoutExtension(toRows.First()["NAME"].ToString());
2022

    
2023
                        DataRow newRow = dt.NewRow();
2024
                        newRow["FromDrawing"] = fromDrawingName;
2025
                        newRow["FromDrawingUID"] = fromDrawingUID;
2026
                        newRow["FromOPCUID"] = fromOPCUID;
2027
                        newRow["ToDrawing"] = toDrawingName;
2028
                        newRow["ToDrawingUID"] = toDrawingUID;
2029
                        newRow["ToOPCUID"] = toOPCUID;
2030

    
2031
                        dt.Rows.Add(newRow);
2032
                    }
2033
                }
2034
            }
2035

    
2036
            return dt;
2037
        }
2038

    
2039
        private DataTable GetTopologyRule()
2040
        {
2041
            DataTable dt = DB.SelectTopologyRule();
2042

    
2043
            return dt;
2044
        }
2045

    
2046
        private bool IsConnected(Item item1, Item item2)
2047
        {
2048
            if (item1.Relations.Find(x => x.UID.Equals(item2.UID)) != null &&
2049
                item2.Relations.Find(x => x.UID.Equals(item1.UID)) != null)
2050
                return true;
2051
            else
2052
                return false;
2053
        }
2054

    
2055
        private void SaveNozzleAndEquipment()
2056
        {
2057
            Dictionary<string, int> globalNozzleCount = new Dictionary<string, int>();
2058

    
2059
            List<Item> nozzles = new List<Item>();
2060
            List<Equipment> equipments = new List<Equipment>();
2061
            foreach (Document document in Documents)
2062
            {
2063
                nozzles.AddRange(document.Items.FindAll(x => x.SubItemType == SubItemType.Nozzle));
2064
                equipments.AddRange(document.Equipments);
2065
            }
2066

    
2067
            DataTable equipDT = new DataTable();
2068
            equipDT.Columns.Add("OID", typeof(string));
2069
            equipDT.Columns.Add("ITEMTAG", typeof(string));
2070
            equipDT.Columns.Add("XCOORDS", typeof(string));
2071
            equipDT.Columns.Add("YCOORDS", typeof(string));
2072

    
2073
            foreach (Equipment equipment in equipments)
2074
            {
2075
                DataRow row = equipDT.NewRow();
2076
                row["OID"] = equipment.UID;
2077
                if (!string.IsNullOrEmpty(EquipTagNoAttributeName))
2078
                {
2079
                    Attribute attribute = equipment.Attributes.Find(x => x.Name == EquipTagNoAttributeName);
2080
                    if (attribute != null && attribute.Value != "None")
2081
                        equipment.ItemTag = attribute.Value;
2082
                    else
2083
                    {
2084
                        equipment.ItemTag = equipment.Name;
2085
                    }
2086
                }
2087
                else
2088
                    equipment.ItemTag = equipment.Name;
2089

    
2090
                if (!string.IsNullOrEmpty(equipment.ItemTag))
2091
                {
2092
                    if (!globalNozzleCount.ContainsKey(equipment.ItemTag))
2093
                    {
2094
                        globalNozzleCount[equipment.ItemTag] = 0;
2095
                    }
2096
                }
2097

    
2098
                row["ITEMTAG"] = equipment.ItemTag;
2099
                List<double> xList = equipment.POINT.Select(x => x[0]).ToList();
2100
                row["XCOORDS"] = (xList.Sum() / (double)xList.Count) / DrawingWidth;
2101

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

    
2105
                equipDT.Rows.Add(row);
2106
            }
2107

    
2108
            DataTable nozzleDT = new DataTable();
2109
            nozzleDT.Columns.Add("OID", typeof(string));
2110
            nozzleDT.Columns.Add("ITEMTAG", typeof(string));
2111
            nozzleDT.Columns.Add("XCOORDS", typeof(string));
2112
            nozzleDT.Columns.Add("YCOORDS", typeof(string));
2113
            nozzleDT.Columns.Add("Equipment_OID", typeof(string));
2114
            nozzleDT.Columns.Add("FLUID", typeof(string));
2115
            nozzleDT.Columns.Add("NPD", typeof(string));
2116
            nozzleDT.Columns.Add("PMC", typeof(string));
2117
            nozzleDT.Columns.Add("ROTATION", typeof(string));
2118
            nozzleDT.Columns.Add("FlowDirection", typeof(string));
2119

    
2120
            foreach (Item item in nozzles)
2121
            {
2122
                DataRow row = nozzleDT.NewRow();
2123
                row["OID"] = item.UID;
2124

    
2125
                Relation relation = item.Relations.Find(x => equipments.Find(y => y.UID == x.UID) != null);
2126
                if (relation != null)
2127
                {
2128
                    Equipment equipment = equipments.Find(x => x.UID == relation.UID);
2129
                    equipment.Nozzles.Add(item);
2130
                    //row["ITEMTAG"] = string.Format("N{0}", string.Format("{0:D3}", equipment.Nozzles.Count + 100));
2131

    
2132
                    int nozzleCount = equipment.Nozzles.Count;
2133
                    if (globalNozzleCount.ContainsKey(equipment.ItemTag))
2134
                    {
2135
                        nozzleCount = globalNozzleCount[equipment.ItemTag];
2136
                    }
2137
                    row["ITEMTAG"] = string.Format("N{0}", string.Format("{0:D3}", nozzleCount + 100));
2138
                    if (globalNozzleCount.ContainsKey(equipment.ItemTag))
2139
                    {
2140
                        globalNozzleCount[equipment.ItemTag] += 1;
2141
                    }
2142

    
2143
                    row["Equipment_OID"] = equipment.UID;
2144
                    item.Equipment = equipment;
2145
                }
2146
                row["XCOORDS"] = (item.POINT[0] / DrawingWidth).ToString();
2147
                row["YCOORDS"] = (item.POINT[1] / DrawingHeight).ToString();
2148
                Attribute fluidAttr = item.LineNumber.Attributes.Find(x => x.Name == "FluidCode");
2149
                row["FLUID"] = fluidAttr != null ? fluidAttr.Value : string.Empty;
2150
                Attribute npdAttr = item.LineNumber.Attributes.Find(x => x.Name == "NominalDiameter");
2151
                row["NPD"] = npdAttr != null ? npdAttr.Value : string.Empty;
2152
                Attribute pmcAttr = item.LineNumber.Attributes.Find(x => x.Name == "PipingMaterialsClass");
2153
                row["PMC"] = pmcAttr != null ? pmcAttr.Value : string.Empty;
2154

    
2155
                double angle = Math.PI * 2 - Convert.ToDouble(item.ANGLE);
2156
                if (angle >= Math.PI * 2)
2157
                    angle = angle - Math.PI * 2;
2158
                row["ROTATION"] = angle.ToString();
2159

    
2160
                if (item.Topology.Items.First().Equals(item))
2161
                    row["FlowDirection"] = "Outlet";
2162
                else if (item.Topology.Items.Last().Equals(item))
2163
                    row["FlowDirection"] = "Inlet";
2164
                else
2165
                    row["FlowDirection"] = string.Empty;
2166

    
2167
                nozzleDT.Rows.Add(row);
2168
            }
2169

    
2170
            //DataTable equipDT = new DataTable();
2171
            //equipDT.Columns.Add("OID", typeof(string));
2172
            //equipDT.Columns.Add("ITEMTAG", typeof(string));
2173
            //equipDT.Columns.Add("XCOORDS", typeof(string));
2174
            //equipDT.Columns.Add("YCOORDS", typeof(string));
2175

    
2176
            //foreach (Equipment equipment in equipments)
2177
            //{
2178
            //    DataRow row = equipDT.NewRow();
2179
            //    row["OID"] = equipment.UID;
2180
            //    if (!string.IsNullOrEmpty(EquipTagNoAttributeName))
2181
            //    {
2182
            //        Attribute attribute = equipment.Attributes.Find(x => x.Name == EquipTagNoAttributeName);
2183
            //        if (attribute != null)
2184
            //            equipment.ItemTag = attribute.Value;
2185
            //    }
2186
            //    else
2187
            //        equipment.ItemTag = equipment.Name;
2188

    
2189
            //    row["ITEMTAG"] = equipment.ItemTag;
2190
            //    List<double> xList = equipment.POINT.Select(x => x[0]).ToList();
2191
            //    row["XCOORDS"] = (xList.Sum() / (double)xList.Count) / DrawingWidth;
2192

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

    
2196
            //    equipDT.Rows.Add(row);
2197
            //}
2198

    
2199
            Equipment = equipDT;
2200
            Nozzle = nozzleDT;
2201
        }
2202

    
2203
        private void SavePSNData()
2204
        {
2205
            try
2206
            {
2207
                DataTable pathItemsDT = new DataTable();
2208
                pathItemsDT.Columns.Add("OID", typeof(string));
2209
                pathItemsDT.Columns.Add("SequenceData_OID", typeof(string));
2210
                pathItemsDT.Columns.Add("TopologySet_OID", typeof(string));
2211
                pathItemsDT.Columns.Add("BranchTopologySet_OID", typeof(string));
2212
                pathItemsDT.Columns.Add("PipeLine_OID", typeof(string));
2213
                pathItemsDT.Columns.Add("ITEMNAME", typeof(string));
2214
                pathItemsDT.Columns.Add("ITEMTAG", typeof(string));
2215
                pathItemsDT.Columns.Add("DESCRIPTION", typeof(string));
2216
                pathItemsDT.Columns.Add("Class", typeof(string));
2217
                pathItemsDT.Columns.Add("SubClass", typeof(string));
2218
                pathItemsDT.Columns.Add("TYPE", typeof(string));
2219
                pathItemsDT.Columns.Add("PIDNAME", typeof(string));
2220
                pathItemsDT.Columns.Add("Equipment_OID", typeof(string));
2221
                pathItemsDT.Columns.Add("NPD", typeof(string));
2222
                pathItemsDT.Columns.Add("GROUPTAG", typeof(string));
2223
                pathItemsDT.Columns.Add("PipeSystemNetwork_OID", typeof(string));
2224
                pathItemsDT.Columns.Add("ViewPipeSystemNetwork_OID", typeof(string));
2225
                pathItemsDT.Columns.Add("PipeRun_OID", typeof(string));
2226
                pathItemsDT.Columns.Add("EqpGroupTag", typeof(string));
2227
                pathItemsDT.Columns.Add("MainLineTag", typeof(string));
2228
                pathItemsDT.Columns.Add("EGTConnectedPoint", typeof(string));
2229
                pathItemsDT.Columns.Add("EGFlowDirection", typeof(string));
2230

    
2231
                DataTable sequenceDataDT = new DataTable();
2232
                sequenceDataDT.Columns.Add("OID", typeof(string));
2233
                sequenceDataDT.Columns.Add("SERIALNUMBER", typeof(string));
2234
                sequenceDataDT.Columns.Add("PathItem_OID", typeof(string));
2235
                sequenceDataDT.Columns.Add("TopologySet_OID_Key", typeof(string));
2236

    
2237
                DataTable pipeSystemNetworkDT = new DataTable();
2238
                pipeSystemNetworkDT.Columns.Add("OID", typeof(string));
2239
                pipeSystemNetworkDT.Columns.Add("Type", typeof(string));
2240
                pipeSystemNetworkDT.Columns.Add("OrderNumber", typeof(string));
2241
                pipeSystemNetworkDT.Columns.Add("Pipeline_OID", typeof(string));
2242
                pipeSystemNetworkDT.Columns.Add("FROM_DATA", typeof(string));
2243
                pipeSystemNetworkDT.Columns.Add("TO_DATA", typeof(string));
2244
                pipeSystemNetworkDT.Columns.Add("TopologySet_OID_Key", typeof(string));
2245
                pipeSystemNetworkDT.Columns.Add("PSNRevisionNumber", typeof(string));
2246
                pipeSystemNetworkDT.Columns.Add("PBS", typeof(string));
2247
                pipeSystemNetworkDT.Columns.Add("Drawings", typeof(string));
2248
                pipeSystemNetworkDT.Columns.Add("IsValid", typeof(string));
2249
                pipeSystemNetworkDT.Columns.Add("Status", typeof(string));
2250
                pipeSystemNetworkDT.Columns.Add("IncludingVirtualData", typeof(string));
2251
                pipeSystemNetworkDT.Columns.Add("PSNAccuracy", typeof(string));
2252
                pipeSystemNetworkDT.Columns.Add("Pocket", typeof(string));
2253
                pipeSystemNetworkDT.Columns.Add("EGTag", typeof(string));
2254
                pipeSystemNetworkDT.Columns.Add("HasMLTags", typeof(string));
2255
                pipeSystemNetworkDT.Columns.Add("GroundLevel", typeof(string));
2256
                pipeSystemNetworkDT.Columns.Add("AFC", typeof(string));
2257
                pipeSystemNetworkDT.Columns.Add("PUMP", typeof(string));
2258

    
2259
                DataTable topologySetDT = new DataTable();
2260
                topologySetDT.Columns.Add("OID", typeof(string));
2261
                topologySetDT.Columns.Add("Type", typeof(string));
2262
                topologySetDT.Columns.Add("SubType", typeof(string));
2263
                topologySetDT.Columns.Add("HeadItemTag", typeof(string));
2264
                topologySetDT.Columns.Add("TailItemTag", typeof(string));
2265
                topologySetDT.Columns.Add("HeadItemSPID", typeof(string));
2266
                topologySetDT.Columns.Add("TailItemSPID", typeof(string));
2267

    
2268
                DataTable pipelineDT = new DataTable();
2269
                pipelineDT.Columns.Add("OID", typeof(string));
2270
                pipelineDT.Columns.Add("PipeSystem_OID", typeof(string));
2271
                pipelineDT.Columns.Add("FLUID", typeof(string));
2272
                pipelineDT.Columns.Add("PMC", typeof(string));
2273
                pipelineDT.Columns.Add("SEQNUMBER", typeof(string));
2274
                pipelineDT.Columns.Add("INSULATION", typeof(string));
2275
                pipelineDT.Columns.Add("FROM_DATA", typeof(string));
2276
                pipelineDT.Columns.Add("TO_DATA", typeof(string));
2277
                pipelineDT.Columns.Add("Unit", typeof(string));
2278

    
2279
                DataTable pipesystemDT = new DataTable();
2280
                pipesystemDT.Columns.Add("OID", typeof(string));
2281
                pipesystemDT.Columns.Add("DESCRIPTION", typeof(string));
2282
                pipesystemDT.Columns.Add("FLUID", typeof(string));
2283
                pipesystemDT.Columns.Add("PMC", typeof(string));
2284
                pipesystemDT.Columns.Add("PipeLineQty", typeof(string));
2285
                pipesystemDT.Columns.Add("GroundLevel", typeof(string));
2286

    
2287
                // Set Vent/Drain Info
2288
                List<VentDrainInfo> VentDrainInfos = new List<VentDrainInfo>();
2289
                DataTable dt = DB.SelectVentDrainSetting();
2290
                foreach (DataRow row in dt.Rows)
2291
                {
2292
                    string groupID = row["GROUP_ID"].ToString();
2293
                    string desc = row["DESCRIPTION"].ToString();
2294
                    int index = Convert.ToInt32(row["INDEX"]);
2295
                    string name = row["NAME"].ToString();
2296

    
2297
                    VentDrainInfo ventDrainInfo = VentDrainInfos.Find(x => x.UID.Equals(groupID));
2298
                    if (ventDrainInfo == null)
2299
                    {
2300
                        ventDrainInfo = new VentDrainInfo(groupID);
2301
                        ventDrainInfo.Description = desc;
2302
                        VentDrainInfos.Add(ventDrainInfo);
2303
                    }
2304

    
2305
                    ventDrainInfo.VentDrainItems.Add(new VentDrainItem()
2306
                    {
2307
                        Index = index,
2308
                        Name = name
2309
                    });
2310
                }
2311
                foreach (VentDrainInfo ventDrainInfo in VentDrainInfos)
2312
                    ventDrainInfo.VentDrainItems = ventDrainInfo.VentDrainItems.OrderBy(x => x.Index).ToList();
2313

    
2314
                #region Keyword Info
2315
                KeywordInfo KeywordInfos = new KeywordInfo();
2316
                DataTable dtKeyword = DB.SelectKeywordsSetting();
2317
                foreach (DataRow row in dtKeyword.Rows)
2318
                {
2319
                    int index = Convert.ToInt32(row["INDEX"]);
2320
                    string name = row["NAME"].ToString();
2321
                    string keyword = row["KEYWORD"].ToString();
2322

    
2323
                    //KeywordInfo keywordInfo = new KeywordInfo();   
2324
                    KeywordInfos.KeywordItems.Add(new KeywordItem()
2325
                    {
2326
                        Index = index,
2327
                        Name = name,
2328
                        Keyword = keyword
2329
                    });
2330
                }
2331
                #endregion
2332

    
2333
                #region ValveGrouping Info
2334
                ValveGroupInfo ValveGrouping = new ValveGroupInfo();
2335
                DataTable dtValveGroupung = DB.SelectValveGroupItemsSetting();
2336
                foreach (DataRow row in dtValveGroupung.Rows)
2337
                {
2338
                    ValveGrouping.ValveGroupItems.Add(new ValveGroupItem()
2339
                    {
2340
                        OID = row["OID"].ToString(),
2341
                        GroupType = row["GroupType"].ToString(),
2342
                        TagIdentifier = row["TagIdentifier"].ToString(),
2343
                        AttributeName = row["AttributeName"].ToString(),
2344
                        SppidSymbolName = row["SppidSymbolName"].ToString()
2345
                    });
2346
                }
2347
                #endregion
2348

    
2349
                #region EquipmentNoPocket Info
2350
                EquipmentNoPocketInfo EquipmentNoPocket = new EquipmentNoPocketInfo();
2351
                DataTable dtEquipmentNoPocket = DB.SelectEquipmentNoPocketSetting();
2352
                foreach (DataRow row in dtEquipmentNoPocket.Rows)
2353
                {
2354
                    EquipmentNoPocket.EquipmentNoPocketItem.Add(new EquipmentNoPocketItem()
2355
                    {
2356
                        Index = Convert.ToInt32(row["INDEX"]),
2357
                        Type = row["TYPE"].ToString(),
2358
                        Name = row["NAME"].ToString()
2359
                    });
2360
                }
2361
                #endregion
2362

    
2363
                #region EquipmentAirFinCooler Info
2364
                EquipmentAirFinCoolerInfo EquipmentAirFinCooler = new EquipmentAirFinCoolerInfo();
2365
                DataTable dtEquipmentAirFinCooler = DB.SelectAirFinCoolerSetting();
2366
                foreach (DataRow row in dtEquipmentAirFinCooler.Rows)
2367
                {
2368
                    EquipmentAirFinCooler.EquipmentAirFinCoolerItem.Add(new EquipmentAirFinCoolerItem()
2369
                    {
2370
                        Type = row["Type"].ToString(),
2371
                        Name = row["Name"].ToString()
2372
                    });
2373
                }
2374
                #endregion
2375

    
2376
                // key = 미입력 branch
2377
                Dictionary<Item, Item> startBranchDic = new Dictionary<Item, Item>();
2378
                Dictionary<Item, Item> endBranchDic = new Dictionary<Item, Item>();
2379
                DataTable PSNFluidDT = DB.SelectPSNFluidCode();
2380
                DataTable PSNPMCDT = DB.SelectPSNPIPINGMATLCLASS();
2381
                foreach (PSNItem PSNItem in PSNItems)
2382
                {
2383
                    try
2384
                    {
2385
                        int psnOrder = 0;
2386
                        int index = 0;
2387
                        bool bPSNStart = true;
2388
                        string sPSNData = string.Empty;
2389
                        bool bVentDrain = false;
2390

    
2391
                        List<Group> Groups = PSNItem.Groups;
2392
                        Dictionary<string, List<Item>> keyValuePairs = new Dictionary<string, List<Item>>();
2393
                        List<Item> valveGroupingItem = new List<Item>();
2394

    
2395
                        //VentDrain 검사
2396
                        if (PSNItem.Groups.Count.Equals(1))
2397
                        {
2398
                            List<VentDrainInfo> endInfos = new List<VentDrainInfo>();
2399
                            for (int g = 0; g < Groups.Count; g++)
2400
                            {
2401
                                Group group = Groups[g];
2402

    
2403
                                foreach (VentDrainInfo ventDrainInfo in VentDrainInfos)
2404
                                {
2405
                                    if(!bVentDrain)
2406
                                    {
2407
                                        for (int i = 0; i < group.Items.Count; i++)
2408
                                        {
2409
                                            Item item = group.Items[i];
2410
                                            if (ventDrainInfo.VentDrainItems[endInfos.Count()].Name.Equals(item.Name))
2411
                                                endInfos.Add(ventDrainInfo);
2412
                                            else
2413
                                            {
2414
                                                endInfos.Clear();
2415
                                                if (ventDrainInfo.VentDrainItems[endInfos.Count()].Name.Equals(item.Name))
2416
                                                    endInfos.Add(ventDrainInfo);
2417
                                            }
2418

    
2419
                                            if (ventDrainInfo.VentDrainItems.Count().Equals(endInfos.Count()))
2420
                                            {
2421
                                                bVentDrain = true;
2422
                                                break;
2423
                                            }
2424
                                        }
2425
                                    }                                   
2426
                                }                               
2427

    
2428
                                if (!bVentDrain)
2429
                                {
2430
                                    endInfos = new List<VentDrainInfo>();
2431

    
2432
                                    foreach (VentDrainInfo ventDrainInfo in VentDrainInfos)
2433
                                    {
2434
                                        if (!bVentDrain)
2435
                                        {
2436
                                            for (int i = 0; i < group.Items.Count; i++)
2437
                                            {
2438
                                                Item item = group.Items[group.Items.Count - i - 1];
2439

    
2440
                                                if (ventDrainInfo.VentDrainItems[endInfos.Count()].Name.Equals(item.Name))
2441
                                                    endInfos.Add(ventDrainInfo);
2442
                                                else
2443
                                                {
2444
                                                    endInfos.Clear();
2445
                                                    if (ventDrainInfo.VentDrainItems[endInfos.Count()].Name.Equals(item.Name))
2446
                                                        endInfos.Add(ventDrainInfo);
2447
                                                }
2448

    
2449
                                                if (ventDrainInfo.VentDrainItems.Count().Equals(endInfos.Count()))
2450
                                                {
2451
                                                    bVentDrain = true;
2452
                                                    break;
2453
                                                }
2454
                                            }
2455
                                        }
2456
                                    }
2457
                                }
2458
                            }
2459
                        }
2460

    
2461
                        try
2462
                        {
2463
                            foreach (Group group in PSNItem.Groups)
2464
                            {
2465
                                foreach (Item item in group.Items)
2466
                                {
2467
                                    string VgTag = string.Empty;
2468
                                    
2469
                                    if (ValveGrouping.ValveGroupItems.Where(x => x.SppidSymbolName == item.Name).Count() > 0)
2470
                                    {
2471
                                        ValveGroupItem valveitem = ValveGrouping.ValveGroupItems.Where(x => x.SppidSymbolName == item.Name).First();
2472
                                        string value = string.Empty;
2473

    
2474
                                        if (valveitem.GroupType == "Scope Break" || valveitem.AttributeName == "NoSelection" || valveitem.AttributeName == string.Empty)
2475
                                            value = "NoSelection";
2476
                                        else
2477
                                        {
2478
                                            if(item.Attributes.Find(x => x.Name == valveitem.AttributeName) == null)
2479
                                                value = "NoSelection";
2480
                                            else
2481
                                                value = item.Attributes.Find(x => x.Name == valveitem.AttributeName).Value;
2482
                                        }
2483

    
2484
                                        if (valveitem.GroupType == "Scope Break")
2485
                                            VgTag = "Scope Break";
2486
                                        else
2487
                                            VgTag = valveitem.SppidSymbolName + "\\" + value;
2488

    
2489
                                    }
2490

    
2491
                                    string PathitemUID = string.Empty;
2492
                                    if (item.BranchItems.Count == 0)
2493
                                    {
2494
                                        PathitemUID = item.UID;
2495
                                        CreatePathItemsDataRow(PathitemUID, item.ID2DBType, VgTag);
2496
                                        CreateSequenceDataDataRow(PathitemUID);
2497
                                        index++;
2498
                                    }
2499
                                    else
2500
                                    {
2501
                                        PathitemUID = item.UID + "_L1";
2502
                                        CreatePathItemsDataRow(PathitemUID, item.ID2DBType, VgTag);
2503
                                        CreateSequenceDataDataRow(PathitemUID);
2504
                                        index++;
2505
                                        for (int i = 0; i < item.BranchItems.Count; i++)
2506
                                        {
2507
                                            CreatePathItemsDataRow(string.Format(item.UID + "_B{0}", i + 1), "Branch", string.Empty, item.BranchItems[i].Topology.FullName, item.BranchItems[i]);
2508
                                            CreateSequenceDataDataRow(string.Format(item.UID + "_B{0}", i + 1));
2509
                                            index++;
2510

    
2511
                                            CreatePathItemsDataRow(string.Format(item.UID + "_L{0}", i + 2), item.ID2DBType);
2512
                                            CreateSequenceDataDataRow(string.Format(item.UID + "_L{0}", i + 2));
2513
                                            index++;
2514

    
2515
                                            if (item.BranchItems[i].Relations[0].Item != null && item.BranchItems[i].Relations[0].Item == item)
2516
                                                startBranchDic.Add(item.BranchItems[i], item);
2517
                                            else if (item.BranchItems[i].Relations[1].Item != null && item.BranchItems[i].Relations[1].Item == item)
2518
                                                endBranchDic.Add(item.BranchItems[i], item);
2519
                                        }
2520
                                    }
2521

    
2522
                                    if (bPSNStart)
2523
                                    {
2524
                                        CreatePipeSystemNetworkDataRow();
2525
                                        sPSNData = item.TopologyData;
2526
                                        psnOrder++;
2527
                                        bPSNStart = false;
2528
                                    }
2529
                                    else
2530
                                    {
2531
                                        if (item.TopologyData != sPSNData)
2532
                                        {
2533
                                            CreatePipeSystemNetworkDataRow();
2534
                                            sPSNData = item.TopologyData;
2535
                                            psnOrder++;
2536
                                        }
2537
                                    }
2538

    
2539
                                    void CreatePathItemsDataRow(string itemOID, string itemType, string GROUPTAG = "", string branchTopologyName = "", Item branchItem = null)
2540
                                    {
2541
                                        DataRow newRow = pathItemsDT.NewRow();
2542

    
2543
                                        if (itemType == "Nozzles")
2544
                                        {
2545
                                            newRow["Equipment_OID"] = item.Equipment != null ? item.Equipment.UID : "";
2546
                                        }
2547

    
2548
                                        //string topologyFullName = 
2549

    
2550
                                        newRow["OID"] = itemOID;
2551
                                        newRow["SequenceData_OID"] = string.Format(item.Topology.FullName + "_{0}", index);
2552
                                        newRow["TopologySet_OID"] = item.Topology.FullName;
2553
                                        newRow["BranchTopologySet_OID"] = branchTopologyName;
2554
                                        newRow["PipeLine_OID"] = item.PSNPipeLineID;
2555

    
2556
                                        if (item.Name.ToUpper().Equals("TIEINPOINT"))
2557
                                        {                                            
2558
                                            newRow["ITEMNAME"] = "PipingComp";
2559

    
2560
                                            string value = string.Empty;
2561
                                            if (string.IsNullOrEmpty(TieInPointAttributeName))
2562
                                                value = string.Format("TIEINPOINT_{0:D3}", tieInPointSymbolIndex);
2563
                                            else
2564
                                                value = item.Attributes.Find(x => x.Name == TieInPointAttributeName).Value;
2565

    
2566

    
2567
                                            newRow["ITEMTAG"] = value;
2568
                                            newRow["DESCRIPTION"] = value;
2569
                                            newRow["Class"] = "In-Line Fitting";
2570
                                            newRow["SubClass"] = "";
2571
                                            newRow["TYPE"] = "End";
2572
                                            tieInPointSymbolIndex++;
2573
                                        }
2574
                                        else
2575
                                        {
2576
                                            newRow["ITEMNAME"] = GetItemName(item, itemOID);
2577
                                            newRow["ITEMTAG"] = GetItemTag(item);
2578
                                            newRow["Class"] = GetClass(item, itemOID);
2579
                                            string subClass = GetSubClass(item, itemOID);
2580
                                            newRow["SubClass"] = subClass;
2581

    
2582
                                            if (item.ItemType == ItemType.Symbol)
2583
                                                newRow["TYPE"] = item.ID2DBName;
2584
                                            else if (item.ItemType == ItemType.Line)
2585
                                                newRow["TYPE"] = item.ID2DBType;
2586
                                        }
2587
                                       
2588
                                        
2589
                                       
2590
                                        newRow["PIDNAME"] = group.Document.DrawingName;
2591

    
2592
                                        // NPD
2593
                                        if (item.LineNumber != null)
2594
                                        {
2595
                                            Attribute attribute = item.LineNumber.Attributes.Find(x => x.Name == "NominalDiameter");
2596
                                            if (attribute != null)
2597
                                                newRow["NPD"] = attribute.Value;
2598
                                        }
2599
                                        else
2600
                                            newRow["NPD"] = null;
2601

    
2602
                                        newRow["GROUPTAG"] = GROUPTAG;
2603
                                        newRow["PipeSystemNetwork_OID"] = PSNItem.PSN_OID();
2604
                                        if (branchItem == null)
2605
                                            newRow["ViewPipeSystemNetwork_OID"] = PSNItem.PSN_OID();
2606
                                        else
2607
                                            newRow["ViewPipeSystemNetwork_OID"] = branchItem.PSNItem.PSN_OID();
2608

    
2609
                                        newRow["PipeRun_OID"] = item.LineNumber != null ? item.LineNumber.Name : string.Empty;
2610
                                        newRow["EGTConnectedPoint"] = 0;
2611
                                        pathItemsDT.Rows.Add(newRow);
2612
                                    }
2613

    
2614

    
2615
                                    void CreateSequenceDataDataRow(string itemOID)
2616
                                    {
2617
                                        DataRow newRow = sequenceDataDT.NewRow();
2618
                                        newRow["OID"] = string.Format(item.Topology.FullName + "_{0}", index);
2619
                                        newRow["SERIALNUMBER"] = string.Format("{0}", index);
2620
                                        newRow["PathItem_OID"] = itemOID;
2621
                                        newRow["TopologySet_OID_Key"] = item.Topology.FullName;
2622

    
2623
                                        sequenceDataDT.Rows.Add(newRow);
2624
                                    }
2625

    
2626
                                    void CreatePipeSystemNetworkDataRow()
2627
                                    {
2628
                                        LineNumber lineNumber = item.Document.LineNumbers.Find(x => x.UID == item.Owner);
2629
                                        string FluidCode = string.Empty;
2630
                                        string GroundLevel = string.Empty;
2631
                                        if (lineNumber != null)
2632
                                        {
2633
                                            List<Attribute> att = lineNumber.Attributes;
2634
                                            if (att != null)
2635
                                            {
2636
                                                List<string> oid = new List<string>();
2637
                                                FluidCode = att.Where(x => x.Name.ToUpper().Equals("FLUIDCODE")).FirstOrDefault() != null ? att.Where(x => x.Name.ToUpper().Equals("FLUIDCODE")).FirstOrDefault().Value : string.Empty;
2638
                                                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;
2639
                                                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;
2640
                                                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;
2641
                                                //InsulationPurpose
2642
                                                if (!string.IsNullOrEmpty(FluidCode)) oid.Add(FluidCode);
2643
                                                if (!string.IsNullOrEmpty(PMC)) oid.Add(PMC);
2644

    
2645
                                                string PipeSystem_OID = string.Join("-", oid);
2646

    
2647
                                                if (!string.IsNullOrEmpty(SEQNUMBER)) oid.Add(SEQNUMBER);
2648
                                                if (!string.IsNullOrEmpty(INSULATION)) oid.Add(INSULATION);
2649

    
2650
                                                string OID = string.Join("-", oid);
2651
                                                string FluidCodeGL = string.Empty;
2652
                                                string PMCGL = string.Empty;
2653

    
2654
                                                if (pipelineDT.Select(string.Format("OID = '{0}'", OID)).Count() == 0)
2655
                                                {
2656
                                                    DataRow newPipelineRow = pipelineDT.NewRow();
2657
                                                    newPipelineRow["OID"] = OID;
2658
                                                    newPipelineRow["PipeSystem_OID"] = PipeSystem_OID;
2659
                                                    newPipelineRow["FLUID"] = FluidCode;
2660
                                                    newPipelineRow["PMC"] = PMC;
2661
                                                    newPipelineRow["SEQNUMBER"] = SEQNUMBER;
2662
                                                    newPipelineRow["INSULATION"] = INSULATION;
2663
                                                    newPipelineRow["FROM_DATA"] = string.Empty;
2664
                                                    newPipelineRow["TO_DATA"] = string.Empty;
2665
                                                    newPipelineRow["Unit"] = PSNItem.GetPBSData();
2666
                                                    pipelineDT.Rows.Add(newPipelineRow);
2667
                                                }
2668

    
2669
                                                if (pipesystemDT.Select(string.Format("OID = '{0}'", PipeSystem_OID)).Count() == 0)
2670
                                                {
2671
                                                    DataRow newPipesystemRow = pipesystemDT.NewRow();
2672
                                                    newPipesystemRow["OID"] = PipeSystem_OID;
2673
                                                    newPipesystemRow["DESCRIPTION"] = string.Empty;
2674
                                                    newPipesystemRow["FLUID"] = FluidCode;
2675
                                                    newPipesystemRow["PMC"] = PMC;
2676
                                                    newPipesystemRow["PipeLineQty"] = string.Empty;
2677
                                                    
2678
                                                    if (!string.IsNullOrEmpty(FluidCode) && !string.IsNullOrEmpty(PMC))
2679
                                                    {
2680
                                                        FluidCodeGL = PSNFluidDT.Select(string.Format("Code = '{0}'", FluidCode)).FirstOrDefault().Field<string>("GroundLevel");
2681
                                                        PMCGL = PSNPMCDT.Select(string.Format("Code= '{0}'", PMC)).FirstOrDefault().Field<string>("GroundLevel");
2682
                                                        if (FluidCodeGL == "AG" && PMCGL == "AG")
2683
                                                            GroundLevel = "AG";
2684
                                                        else if (FluidCodeGL == "UG" && PMCGL == "UG")
2685
                                                            GroundLevel = "UG";
2686
                                                        else
2687
                                                            GroundLevel = "AG_UG";
2688
                                                    }
2689
                                                    newPipesystemRow["GroundLevel"] = GroundLevel;
2690
                                                    pipesystemDT.Rows.Add(newPipesystemRow);
2691
                                                }
2692
                                                else
2693
                                                {
2694
                                                    GroundLevel = pipesystemDT.Select(string.Format("OID = '{0}'", PipeSystem_OID)).First().Field<string>("GroundLevel");
2695
                                                }
2696
                                            }
2697
                                        }
2698

    
2699
                                        DataRow newRow = pipeSystemNetworkDT.NewRow();
2700
                                        newRow["OID"] = PSNItem.PSN_OID();
2701

    
2702
                                        newRow["OrderNumber"] = psnOrder;
2703
                                        newRow["Pipeline_OID"] = item.PSNPipeLineID;
2704
                                        PSNItem.KeywordInfos = KeywordInfos;
2705
                                        PSNItem.Nozzle = Nozzle;
2706

    
2707
                                        string FromType = string.Empty;
2708
                                        Item From_item = new Item();
2709

    
2710
                                        string oStatus = string.Empty;
2711
                                        string FROM_DATA = PSNItem.GetFromData(ref FromType, ref From_item, item, ref oStatus);
2712
                                        string status = string.Empty;
2713
                                        if (psnOrder == 0)
2714
                                            status = PSNItem.Status;
2715

    
2716
                                        if (PSNItem.IsKeyword)
2717
                                        {
2718
                                            PSNItem.StartType = PSNType.Equipment;
2719
                                        }
2720
                                        string ToType = string.Empty;
2721
                                        Item To_item = new Item();
2722
                                        string TO_DATA = PSNItem.GetToData(ref ToType, ref To_item, item);
2723
                                        if (PSNItem.IsKeyword)
2724
                                        {
2725
                                            PSNItem.EndType = PSNType.Equipment;
2726
                                        }
2727
                                                   
2728
                                        newRow["FROM_DATA"] = FROM_DATA;
2729
                                        newRow["TO_DATA"] = TO_DATA;
2730
                                       
2731
                                        newRow["Type"] = PSNItem.GetPSNType();
2732

    
2733
                                        if (psnOrder == 0) //order 0일땐 그냥 붙여줌
2734
                                        {
2735
                                           status += oStatus + PSNItem.Status;                                                                                      
2736
                                        }
2737
                                        else if (psnOrder > 0) //0보다 크면 
2738
                                        {
2739
                                            DataRow dr = pipeSystemNetworkDT.Select(string.Format("OID = '{0}' AND OrderNumber = {1}", PSNItem.PSN_OID(), psnOrder - 1)).FirstOrDefault();
2740
                                            
2741
                                            if (psnOrder >= 1) 
2742
                                            {
2743
                                                if (!string.IsNullOrEmpty(PSNItem.Status))
2744
                                                {
2745
                                                    if (dr["Status"].ToString().Contains(PSNItem.Status))
2746
                                                    {
2747
                                                        dr["Status"] = dr["Status"].ToString().Replace(PSNItem.Status, string.Empty);
2748
                                                    }
2749
                                                    else
2750
                                                    {
2751
                                                        dr["Status"] = dr["Status"].ToString().Replace(PSNItem.Status.Remove(0, 2), string.Empty);
2752
                                                    }    
2753
                                                }
2754
                                            }                                                                                    
2755
                                            
2756
                                            status = oStatus + PSNItem.Status;
2757
                                            if (dr != null)
2758
                                            {
2759
                                                newRow["FROM_DATA"] = dr.Field<string>("FROM_DATA");
2760
                                                newRow["TO_DATA"] = dr.Field<string>("TO_DATA");
2761
                                                newRow["Type"] = dr.Field<string>("Type");
2762
                                            }
2763
                                        }                                        
2764
                                    
2765
                                        status = !string.IsNullOrEmpty(status) ? status.Remove(0, 2) : string.Empty;
2766
                                        string[] st = status.Split(new string[] { ", " }, StringSplitOptions.None);
2767
                                        if (st.Count() > 1)
2768
                                        {
2769
                                            status = string.Join(", ", st.Distinct());                                                                                     
2770
                                        }
2771
                                        newRow["Status"] = status;
2772
                                        newRow["IsValid"] = PSNItem.IsValid;
2773

    
2774
                                        newRow["TopologySet_OID_Key"] = item.Topology.FullName;
2775
                                        newRow["PSNRevisionNumber"] = string.Format("V{0:D4}", Revision);                                                                           
2776
                                        newRow["PBS"] = PSNItem.GetPBSData();
2777

    
2778
                                        List<string> drawingNames = new List<string>();
2779
                                        foreach (Group _group in PSNItem.Groups)
2780
                                        {
2781
                                            if (!drawingNames.Contains(_group.Document.DrawingName))
2782
                                            {
2783
                                                if (drawingNames.Count == 0)
2784
                                                    newRow["Drawings"] = _group.Document.DrawingName;
2785
                                                else
2786
                                                    newRow["Drawings"] = newRow["Drawings"] + ", " + _group.Document.DrawingName;
2787
                                                drawingNames.Add(_group.Document.DrawingName);
2788
                                            }
2789
                                        }
2790

    
2791
                                        // VentDrain의 경우 제외 요청 (데이터를 아예 제거하였을 경우 후 가공에서 문제가 생김 마지막에 지우는것으로 변경)
2792
                                        if (bVentDrain)
2793
                                            newRow["IncludingVirtualData"] = "Vent_Drain";
2794
                                        else
2795
                                            newRow["IncludingVirtualData"] = "No";
2796
                                       
2797
                                        newRow["PSNAccuracy"] = "100";
2798

    
2799
                                        string Pocket = "No";
2800
                                        string Condition = PSNFluidDT.Select(string.Format("Code = '{0}'", FluidCode)).FirstOrDefault().Field<string>("Condition");
2801
                                        if (Condition.Equals("Flare"))
2802
                                            Pocket = "Yes";
2803

    
2804
                                        if (item.ID2DBType == "Nozzles" && PSNItem.StartType == PSNType.Equipment && From_item.Equipment != null) 
2805
                                        {
2806
                                            Equipment Equipment = From_item.Equipment;                                      
2807
                                            EquipmentNoPocketItem nopocket = EquipmentNoPocket.EquipmentNoPocketItem.Where(x => x.Name == Equipment.Name && x.Type != "Pump").FirstOrDefault();
2808
                                          
2809
                                            if (nopocket != null)
2810
                                            {
2811
                                                DataRow bNozzle = null;
2812
                                                if (nopocket.Type.Equals("Vertical Vessel"))
2813
                                                {
2814
                                                    DataRow drNozzle = Nozzle.Select(string.Format("Equipment_OID = '{0}' AND OID = '{1}'", Equipment.UID, From_item.UID)).FirstOrDefault();
2815
                                                    if (drNozzle != null)
2816
                                                    {
2817
                                                        if (drNozzle.Field<string>("Rotation").Length >= 4 && drNozzle.Field<string>("Rotation").Substring(0, 4) == "4.71")
2818
                                                        {
2819
                                                            bNozzle = drNozzle;
2820
                                                        }
2821

    
2822
                                                        if (bNozzle == null)
2823
                                                        {
2824
                                                            DataRow[] nozzleRows = Nozzle.Select(string.Format("Equipment_OID = '{0}'", Equipment.UID));
2825

    
2826
                                                            if (drNozzle != null)
2827
                                                            {
2828
                                                                bNozzle = drNozzle;
2829
                                                                foreach (DataRow it in nozzleRows)
2830
                                                                {
2831
                                                                    if (Convert.ToDecimal(drNozzle.Field<string>("Ycoords")) > Convert.ToDecimal(it.Field<string>("Ycoords")))
2832
                                                                    {
2833
                                                                        bNozzle = null;
2834
                                                                        break;
2835
                                                                    }
2836
                                                                }
2837
                                                            }
2838
                                                        }
2839
                                                    }
2840

    
2841
                                                    if (bNozzle != null)
2842
                                                        Pocket = "Yes";
2843
                                                }
2844
                                                else
2845
                                                    Pocket = "Yes";
2846
                                            }
2847
                                        }
2848
                                        
2849
                                        if (item.ID2DBType == "Nozzles" && PSNItem.EndType == PSNType.Equipment && To_item.Equipment != null) //To는 전체
2850
                                        {
2851
                                            Equipment Equipment = To_item.Equipment;
2852
                                            EquipmentNoPocketItem nopocket = EquipmentNoPocket.EquipmentNoPocketItem.Where(x => x.Name == Equipment.Name).FirstOrDefault();
2853
                                            if (nopocket != null)
2854
                                            {
2855
                                                DataRow bNozzle = null;
2856
                                                if (nopocket.Type.Equals("Vertical Vessel"))
2857
                                                {
2858
                                                    DataRow drNozzle = Nozzle.Select(string.Format("Equipment_OID = '{0}' AND OID = '{1}'", Equipment.UID, To_item.UID)).FirstOrDefault();
2859
                                                    if(drNozzle != null)
2860
                                                    {
2861
                                                        if (drNozzle.Field<string>("Rotation").Length >= 4 && drNozzle.Field<string>("Rotation").Substring(0, 4) == "4.71")
2862
                                                        {
2863
                                                            bNozzle = drNozzle;
2864
                                                        }
2865

    
2866
                                                        if (bNozzle == null)
2867
                                                        {
2868
                                                            DataRow[] nozzleRows = Nozzle.Select(string.Format("Equipment_OID = '{0}'", Equipment.UID));
2869

    
2870
                                                            if (drNozzle != null)
2871
                                                            {
2872
                                                                bNozzle = drNozzle;
2873
                                                                foreach (DataRow it in nozzleRows)
2874
                                                                {
2875
                                                                    if (Convert.ToDecimal(drNozzle.Field<string>("Ycoords")) > Convert.ToDecimal(it.Field<string>("Ycoords")))
2876
                                                                    {
2877
                                                                        bNozzle = null;
2878
                                                                        break;
2879
                                                                    }
2880
                                                                }
2881
                                                            }
2882
                                                        }
2883
                                                    }                                                   
2884

    
2885
                                                    if (bNozzle != null)
2886
                                                        Pocket = "Yes";
2887
                                                }
2888
                                                else
2889
                                                    Pocket = "Yes";
2890
                                            }
2891
                                        }
2892
                                        
2893
                                        newRow["Pocket"] = Pocket;
2894
                                        string AFC = "P2";
2895
                                        if (PSNItem.StartType == PSNType.Equipment && From_item.Equipment != null)
2896
                                        {
2897
                                            if (EquipmentAirFinCooler.EquipmentAirFinCoolerItem.Where(x => x.Type != "Pump" && x.Name.Equals(From_item.Equipment.Name)).Count() > 0)
2898
                                                AFC = "P1\\" + From_item.Equipment.Name;
2899
                                            else if (EquipmentAirFinCooler.EquipmentAirFinCoolerItem.Where(x => x.Type == "Pump" && x.Name.Equals(From_item.Equipment.Name)).Count() > 0)
2900
                                                newRow["PUMP"] = "PUMP";
2901
                                        }
2902

    
2903
                                        if (PSNItem.EndType == PSNType.Equipment && To_item.Equipment != null)
2904
                                        {
2905
                                            if (EquipmentAirFinCooler.EquipmentAirFinCoolerItem.Where(x => x.Type != "Pump" && x.Name.Equals(To_item.Equipment.Name)).Count() > 0)
2906
                                                AFC = "P1\\" + To_item.Equipment.Name;
2907
                                            else if (EquipmentAirFinCooler.EquipmentAirFinCoolerItem.Where(x => x.Type == "Pump" && x.Name.Equals(To_item.Equipment.Name)).Count() > 0)
2908
                                                newRow["PUMP"] = "PUMP";
2909
                                        }
2910

    
2911
                                        newRow["AFC"] = AFC;
2912

    
2913
                                        newRow["EGTag"] = string.Empty;
2914
                                        newRow["HasMLTags"] = "False";
2915
                                        newRow["GroundLevel"] = GroundLevel;
2916
                                        pipeSystemNetworkDT.Rows.Add(newRow);
2917
                                    }
2918
                                }
2919
                            }                      
2920
                        }
2921
                        catch (Exception ex)
2922
                        {
2923
                            Log.Write(ex.Message + "\r\n" + ex.StackTrace);
2924
                            MessageBox.Show(ex.Message, "ID2 " + id2Info.ProgramName, MessageBoxButtons.OK, MessageBoxIcon.Information);
2925
                        }
2926

    
2927
                        //TopologySet 관련
2928
                        foreach (Topology topology in PSNItem.Topologies)
2929
                        {
2930
                            DataRow newRow = topologySetDT.NewRow();
2931
                            newRow["OID"] = topology.FullName;
2932
                            newRow["Type"] = topology.FullName.Split(new char[] { '-' }).Last().StartsWith("M") ? "Main" : "Branch";
2933
                            if (bVentDrain)
2934
                                newRow["SubType"] = "Vent_Drain";
2935
                            else
2936
                                newRow["SubType"] = null;
2937
                            newRow["HeadItemTag"] = GetItemTag(topology.Items.Last());
2938
                            newRow["TailItemTag"] = GetItemTag(topology.Items.First());
2939
                            newRow["HeadItemSPID"] = topology.Items.Last().UID;
2940
                            newRow["TailItemSPID"] = topology.Items.First().UID;
2941
                            topologySetDT.Rows.Add(newRow);
2942
                        }
2943

    
2944
                    }
2945
                    catch (Exception ee)
2946
                    {
2947

    
2948
                    }
2949
                }
2950

    
2951

    
2952
                foreach (var item in startBranchDic)
2953
                {
2954
                    string uid = item.Key.UID;
2955
                    string topologyName = item.Value.Topology.FullName;
2956
                    DataRow[] rows = pathItemsDT.Select(string.Format("OID LIKE '{0}%'", uid));
2957

    
2958
                    if (rows.Length == 1)
2959
                    {
2960
                        rows.First()["BranchTopologySet_OID"] = topologyName;
2961
                        rows.First()["ViewPipeSystemNetwork_OID"] = item.Value.PSNItem.PSN_OID();
2962
                    }
2963
                    else if (rows.Length > 1)
2964
                    {
2965
                        DataRow targetRow = null;
2966
                        int index = int.MaxValue;
2967
                        foreach (DataRow row in rows)
2968
                        {
2969
                            string split = row["OID"].ToString().Split(new char[] { '_' })[1];
2970
                            if (split.StartsWith("L"))
2971
                            {
2972
                                int num = Convert.ToInt32(split.Remove(0, 1));
2973
                                if (index > num)
2974
                                {
2975
                                    index = num;
2976
                                    targetRow = row;
2977
                                }
2978
                            }
2979
                        }
2980

    
2981
                        if (targetRow != null)
2982
                        {
2983
                            targetRow["BranchTopologySet_OID"] = topologyName;
2984
                            targetRow["ViewPipeSystemNetwork_OID"] = item.Value.PSNItem.PSN_OID();
2985
                        }
2986
                    }
2987
                }
2988

    
2989
                foreach (var item in endBranchDic)
2990
                {
2991
                    string uid = item.Key.UID;
2992
                    string topologyName = item.Value.Topology.FullName;
2993
                    DataRow[] rows = pathItemsDT.Select(string.Format("OID LIKE '{0}%'", uid));
2994
                    if (rows.Length == 1)
2995
                    {
2996
                        rows.First()["BranchTopologySet_OID"] = topologyName;
2997
                        rows.First()["ViewPipeSystemNetwork_OID"] = item.Value.PSNItem.PSN_OID();
2998
                    }
2999
                    else if (rows.Length > 1)
3000
                    {
3001
                        DataRow targetRow = null;
3002
                        int index = int.MinValue;
3003
                        foreach (DataRow row in rows)
3004
                        {
3005
                            string split = row["OID"].ToString().Split(new char[] { '_' })[1];
3006
                            if (split.StartsWith("L"))
3007
                            {
3008
                                int num = Convert.ToInt32(split.Remove(0, 1));
3009
                                if (index < num)
3010
                                {
3011
                                    index = num;
3012
                                    targetRow = row;
3013
                                }
3014
                            }
3015
                        }
3016

    
3017
                        if (targetRow != null)
3018
                        {
3019
                            targetRow["BranchTopologySet_OID"] = topologyName;
3020
                            targetRow["ViewPipeSystemNetwork_OID"] = item.Value.PSNItem.PSN_OID();
3021
                        }
3022
                    }
3023
                }
3024

    
3025
                PathItems = pathItemsDT;
3026
                SequenceData = sequenceDataDT;
3027
                PipeSystemNetwork = pipeSystemNetworkDT;
3028
                TopologySet = topologySetDT;
3029
                PipeLine = pipelineDT;
3030
                PipeSystem = pipesystemDT;
3031
            }
3032
            catch (Exception ex)
3033
            {
3034
                Log.Write(ex.Message + "\r\n" + ex.StackTrace);
3035
                MessageBox.Show(ex.Message, "ID2 " + id2Info.ProgramName, MessageBoxButtons.OK, MessageBoxIcon.Information);
3036
            }
3037
        }
3038

    
3039
        private double AccuracyCalculation(List<double> lstAcc, double acc)
3040
        {
3041
            foreach (double lacc in lstAcc)
3042
            {
3043
                acc *= lacc;
3044
            }
3045
            return acc;
3046
        }
3047

    
3048
        private void UpdateSubType()
3049
        {
3050
            try
3051
            {
3052
                foreach (PSNItem PSNItem in PSNItems)
3053
                {
3054
                    if (PSNItem.IsBypass)
3055
                    {
3056
                        foreach (Topology topology in PSNItem.Topologies)
3057
                        {
3058
                            DataRow[] rows = TopologySet.Select(string.Format("OID = '{0}'", topology.FullName));
3059
                            if (rows.Length.Equals(1))
3060
                                rows.First()["SubType"] = "Bypass";
3061
                        }
3062
                    }
3063

    
3064
                    if (PSNItem.StartType == PSNType.Header)
3065
                    {
3066
                        Topology topology = PSNItem.Topologies.First();
3067
                        DataRow[] rows = TopologySet.Select(string.Format("OID = '{0}'", topology.FullName));
3068
                        if (rows.Length.Equals(1))
3069
                            rows.First()["SubType"] = "Header";
3070
                    }
3071
                    else if (PSNItem.EndType == PSNType.Header)
3072
                    {
3073
                        Topology topology = PSNItem.Topologies.Last();
3074
                        DataRow[] rows = TopologySet.Select(string.Format("OID = '{0}'", topology.FullName));
3075
                        if (rows.Length.Equals(1))
3076
                            rows.First()["SubType"] = "Header";
3077
                    }
3078
                }
3079

    
3080

    
3081
                foreach (Topology topology in Topologies)
3082
                {
3083
                    try
3084
                    {
3085
                        DataRow[] rows = TopologySet.Select(string.Format("OID = '{0}'", topology.FullName));
3086

    
3087
                        if (rows.Count() > 0)
3088
                        {
3089
                            if (rows.Length.Equals(1) && rows.First()["SubType"] == null || string.IsNullOrEmpty(rows.First()["SubType"].ToString()))
3090
                            {
3091
                                if (topology.Items == null)
3092
                                    continue;
3093

    
3094
                                Item firstItem = topology.Items.First();
3095
                                Item lastItem = topology.Items.Last();
3096

    
3097
                                List<Relation> relations = new List<Relation>();
3098

    
3099
                                if (firstItem.Relations.FindAll(x => x.Item == null).Count() != 0 && firstItem.Relations.FindAll(x => x.Item != null && x.Item.Topology != null && x.Item.Topology.ID != topology.ID) != null )
3100
                                    relations.AddRange(firstItem.Relations.FindAll(x => x.Item != null && x.Item.Topology != null && x.Item.Topology.ID != topology.ID));
3101

    
3102
                                if (lastItem.Relations.FindAll(x => x.Item == null).Count() != 0 && lastItem.Relations.FindAll(x => x.Item != null && x.Item.Topology != null && x.Item.Topology.ID != topology.ID) != null)
3103
                                    relations.AddRange(lastItem.Relations.FindAll(x => x.Item != null && x.Item.Topology != null && x.Item.Topology.ID != topology.ID));
3104

    
3105
                                if (relations.Count > 0)
3106
                                    rows.First()["SubType"] = "OtherSystem";
3107
                            }
3108
                        }
3109
                    }
3110
                    catch (Exception ex)
3111
                    {
3112

    
3113
                        MessageBox.Show(ex.Message, "ID2 " + id2Info.ProgramName, MessageBoxButtons.OK, MessageBoxIcon.Information);
3114
                    }
3115
                }
3116

    
3117
                foreach (DataRow row in TopologySet.Rows)
3118
                    if (row["SubType"] == null || string.IsNullOrEmpty(row["SubType"].ToString()))
3119
                        row["SubType"] = "Normal";
3120
            }
3121
            catch (Exception ex)
3122
            {
3123
                Log.Write(ex.Message + "\r\n" + ex.StackTrace);
3124
                MessageBox.Show(ex.Message, "ID2 " + id2Info.ProgramName, MessageBoxButtons.OK, MessageBoxIcon.Information);
3125
            }
3126
        }
3127

    
3128
        private bool IsBypass(PSNItem PSNItem)
3129
        {
3130
            bool bResult = false;
3131

    
3132
            if (PSNItem.GetPSNType() == "B2B")
3133
            {
3134
                Group firstGroup = PSNItem.Groups.First();
3135
                Group lastGroup = PSNItem.Groups.Last();
3136
                Item firstItem = firstGroup.Items.First();
3137
                Item lastItem = lastGroup.Items.Last();
3138

    
3139
                Item connectedFirstItem = GetConnectedItemByPSN(firstItem);
3140
                Item connectedLastItem = GetConnectedItemByPSN(lastItem);
3141

    
3142
                if (connectedFirstItem.LineNumber != null && connectedLastItem.LineNumber != null &&
3143
                    !string.IsNullOrEmpty(connectedFirstItem.LineNumber.Name) && !string.IsNullOrEmpty(connectedLastItem.LineNumber.Name) &&
3144
                    connectedFirstItem.LineNumber.Name == connectedLastItem.LineNumber.Name)
3145
                    bResult = true;
3146
                else if (connectedFirstItem.PSNItem == connectedLastItem.PSNItem)
3147
                    bResult = true;
3148
            }
3149

    
3150
            Item GetConnectedItemByPSN(Item item)
3151
            {
3152
                Item result = null;
3153

    
3154
                Relation relation = item.Relations.Find(x => x.Item != null && x.Item.PSNItem != item.PSNItem);
3155
                if (relation != null)
3156
                    result = relation.Item;
3157

    
3158

    
3159
                return result;
3160
            }
3161

    
3162
            return bResult;
3163
        }
3164

    
3165
        private void DeleteVentDrain()
3166
        {
3167
            DataRow[] ventdrainRows = PipeSystemNetwork.Select(" IncludingVirtualData = 'Vent_Drain'");
3168

    
3169
            foreach (DataRow dataRow in ventdrainRows)
3170
            {
3171
                dataRow.Delete();
3172
            }
3173
        }
3174

    
3175
        private void UpdateAccuracy()
3176
        {
3177
            //DataRow[] statusRows = PipeSystemNetwork.Select(" Type = 'Error' OR IsValid = 'Error'"); 
3178
            DataRow[] statusRows = PipeSystemNetwork.Select(" Type = 'Error' OR IsValid = 'Error'");
3179
            List<double> lstAcc = null;
3180
            string Status = string.Empty;
3181

    
3182
            foreach (DataRow dataRow in statusRows)
3183
            {
3184
                lstAcc = new List<double>();
3185
                Status = dataRow.Field<string>("Status");
3186
                if (!string.IsNullOrEmpty(Status))
3187
                {
3188
                    string[] arrStatus = Status.Split(',');
3189
                    foreach (string arrstr in arrStatus)
3190
                    {
3191
                        if (arrstr.Contains(Rule1)) //Missing LineNumber_1
3192
                            lstAcc.Add(0.75);
3193

    
3194
                        if (arrstr.Contains(Rule2)) //Missing LineNumber_2
3195
                            lstAcc.Add(0.7);
3196

    
3197
                        if (arrstr.Contains(Rule3)) // OPC Disconnected
3198
                            lstAcc.Add(0.5);
3199

    
3200
                        if (arrstr.Contains(Rule4)) //Missing ItemTag or Description
3201
                            lstAcc.Add(0.65);
3202

    
3203
                        if (arrstr.Contains(Rule5)) //Line Disconnected
3204
                            lstAcc.Add(0.6);
3205
                    }
3206
                }
3207

    
3208
                string PSNAccuracy = dataRow["PSNAccuracy"].ToString();
3209
                if (PSNAccuracy == "100")
3210
                    PSNAccuracy = "1";
3211

    
3212
                PSNAccuracy = Convert.ToString(Convert.ToDecimal(AccuracyCalculation(lstAcc, Convert.ToDouble(PSNAccuracy))));
3213
                //if (PSNAccuracy != "100")
3214
                //{
3215
                //    //dataRow["IncludingVirtualData"] = "No";
3216
                dataRow["PSNAccuracy"] = PSNAccuracy;
3217
                //}
3218
            }
3219
            DataTable dt = PipeSystemNetwork.DefaultView.ToTable(true, new string[] { "OID" });
3220
            foreach (DataRow dr in dt.Rows)
3221
            {
3222
                string oid = dr.Field<string>("OID");
3223
                DataRow[] select = PipeSystemNetwork.Select(string.Format("OID = '{0}'", oid));
3224
                double totalDdr = 0;
3225
                foreach (DataRow ddr in select)
3226
                {
3227
                    double acc = Convert.ToDouble(ddr.Field<string>("PSNAccuracy"));
3228
                    if (acc == 100) acc = 1;
3229
                    if (totalDdr == 0) totalDdr = acc;
3230
                    else totalDdr *= acc;
3231
                }
3232

    
3233
                totalDdr *= 100;
3234

    
3235
                if (totalDdr != 100)
3236
                {
3237
                    foreach (DataRow ddr in select)
3238
                    {
3239
                        ddr["IncludingVirtualData"] = "Yes";
3240
                        ddr["PSNAccuracy"] = String.Format("{0:0.00}", Math.Round(totalDdr, 2));
3241
                        ddr["IsValid"] = "Error";
3242
                    }
3243
                }
3244
                else
3245
                {
3246
                    foreach (DataRow ddr in select)
3247
                    {
3248
                        ddr["IncludingVirtualData"] = "No";
3249
                        ddr["PSNAccuracy"] = String.Format("{0:0.00}", Math.Round(totalDdr, 2));
3250
                    }
3251
                }
3252
            }
3253

    
3254
        }
3255

    
3256
        private void UpdateKeywordForPSN()
3257
        {
3258
            #region Keyword Info
3259
            KeywordInfo KeywordInfos = new KeywordInfo();
3260
            DataTable dtKeyword = DB.SelectKeywordsSetting();
3261
            foreach (DataRow row in dtKeyword.Rows)
3262
            {
3263
                int index = Convert.ToInt32(row["INDEX"]);
3264
                string name = row["NAME"].ToString();
3265
                string keyword = row["KEYWORD"].ToString();
3266

    
3267
                //KeywordInfo keywordInfo = new KeywordInfo();   
3268
                KeywordInfos.KeywordItems.Add(new KeywordItem()
3269
                {
3270
                    Index = index,
3271
                    Name = name,
3272
                    Keyword = keyword
3273
                });
3274
            }
3275
            #endregion
3276

    
3277
            DataRow[] endofHeaderRows = PipeSystemNetwork.Select(string.Format(" From_Data = '{0}' OR To_Data = '{0}'", "ENDOFHEADER"));
3278
            foreach (DataRow dataRow in endofHeaderRows)
3279
            {
3280
                PSNItem PSNItem = PSNItems.Find(x => x.PSN_OID() == dataRow["OID"].ToString());
3281

    
3282
                if (dataRow.Field<string>("From_Data") == "ENDOFHEADER")
3283
                {
3284
                    DataRow[] pathItemRows = PathItems.Select(string.Format("PipeSystemNetwork_OID = '{0}'", dataRow["OID"]));
3285
                    DataRow dr = pathItemRows.First();
3286
                    dr["CLASS"] = PSNItem.Groups.First().Items.First().ID2DBName;
3287
                    dr["TYPE"] = "End";
3288
                    dr["ITEMTAG"] = "ENDOFHEADER";
3289
                    dr["DESCRIPTION"] = "ENDOFHEADER";
3290
                }
3291

    
3292
                if (dataRow.Field<string>("To_Data") == "ENDOFHEADER")
3293
                {
3294
                    DataRow[] pathItemRows = PathItems.Select(string.Format("PipeSystemNetwork_OID = '{0}'", dataRow["OID"]));
3295
                    DataRow dr = pathItemRows.Last();
3296
                    dr["CLASS"] = PSNItem.Groups.Last().Items.Last().ID2DBName;
3297
                    dr["TYPE"] = "End";
3298
                    dr["ITEMTAG"] = "ENDOFHEADER";
3299
                    dr["DESCRIPTION"] = "ENDOFHEADER";
3300
                }
3301
            }
3302

    
3303
            foreach (KeywordItem keyitem in KeywordInfos.KeywordItems)
3304
            {
3305
                DataRow[] keywordRows = PipeSystemNetwork.Select(string.Format(" From_Data = '{0}' OR To_Data = '{0}'", keyitem.Keyword));
3306
                foreach (DataRow dataRow in keywordRows)
3307
                {
3308
                    PSNItem PSNItem = PSNItems.Find(x => x.PSN_OID() == dataRow["OID"].ToString());
3309

    
3310
                    if (dataRow.Field<string>("From_Data") == keyitem.Keyword)
3311
                    {
3312
                        DataRow[] pathItemRows = PathItems.Select(string.Format("PipeSystemNetwork_OID = '{0}'", dataRow["OID"]));
3313

    
3314
                        //
3315
                        //if(.)
3316
                        if (PSNItem.Groups.First().Items.First().Name.Equals(keyitem.Name))
3317
                        {
3318
                            DataRow dr = pathItemRows.First();
3319
                            //dr["CLASS"] = ""; //Type
3320
                            dr["TYPE"] = "End";
3321
                            dr["ITEMTAG"] = keyitem.Keyword;
3322
                            dr["DESCRIPTION"] = keyitem.Keyword;
3323
                        }
3324

    
3325
                    }
3326

    
3327
                    if (dataRow.Field<string>("To_Data") == keyitem.Keyword)
3328
                    {
3329
                        DataRow[] pathItemRows = PathItems.Select(string.Format("PipeSystemNetwork_OID = '{0}'", dataRow["OID"]));
3330

    
3331
                        if (PSNItem.Groups.Last().Items.Last().Name.Equals(keyitem.Name))
3332
                        {
3333
                            DataRow dr = pathItemRows.Last();
3334
                            //dr["CLASS"] = ""; //Type
3335
                            dr["TYPE"] = "End";
3336
                            dr["ITEMTAG"] = keyitem.Keyword;
3337
                            dr["DESCRIPTION"] = keyitem.Keyword;
3338
                            //dr.Field<string>("Type")
3339
                        }
3340

    
3341
                    }
3342
                }
3343
            }
3344
        }
3345

    
3346
        private void UpdateErrorForPSN()
3347
        {
3348
            DataRow[] errorRows = PipeSystemNetwork.Select(string.Format(" Type = '{0}'", ErrorType.Error));
3349
            foreach (DataRow dataRow in errorRows)
3350
            {
3351
                try
3352
                {
3353
                    PSNItem PSNItem = PSNItems.Find(x => x.PSN_OID() == dataRow["OID"].ToString());
3354
                   
3355
                    bool change = false;
3356
                    bool bCheck = false;
3357
                    if (!PSNItem.EnableType(PSNItem.StartType))
3358
                    {
3359
                        
3360
                        DataRow[] pathItemRows = PathItems.Select(string.Format("PipeSystemNetwork_OID = '{0}'", dataRow["OID"]));
3361
                        int insertIndex = PathItems.Rows.IndexOf(pathItemRows.First());
3362

    
3363
                        Item item = PSNItem.Groups.First().Items.First();
3364
                        try
3365
                        {
3366
                            if (!item.Name.ToUpper().Equals("TIEINPOINT"))
3367
                            {
3368
                                change = true;
3369

    
3370
                                string FROM_DATA = string.Format("TIEINPOINT_{0:D5}V", tieInPointIndex);
3371

    
3372
                                foreach (DataRow loopRow in PipeSystemNetwork.Select(string.Format("OID = '{0}'", PSNItem.PSN_OID())))
3373
                                {
3374
                                    loopRow["FROM_DATA"] = FROM_DATA;
3375
                                    if (item.ItemType == ItemType.Line)
3376
                                    {
3377
                                        if (loopRow.Field<string>("OrderNumber") == "0")
3378
                                        {
3379
                                            string status = loopRow.Field<string>("Status");
3380
                                            //string isvali
3381
                                            if (string.IsNullOrEmpty(status))
3382
                                                status = "Line Disconnected";
3383
                                            else if (!status.Contains("Line Disconnected"))
3384
                                                status += ", Line Disconnected";
3385
                                            loopRow["Status"] = status;
3386
                                            if (!string.IsNullOrEmpty(status))
3387
                                                loopRow["IsValid"] = "Error";
3388
                                        }
3389
                                    }
3390
                                }
3391

    
3392
                                tieInPointIndex++;
3393

    
3394
                                if (item.ItemType == ItemType.Line)
3395
                                {
3396
                                    PathItems.Rows.InsertAt(createTerminatorRow(pathItemRows.First(), FROM_DATA), insertIndex);
3397
                                }
3398
                                else
3399
                                {
3400
                                    PathItems.Rows.InsertAt(createLineRow(PathItems.Select(string.Format("PipeLine_OID = '{0}' AND PipeSystemNetwork_OID = '{1}'", item.PSNPipeLineID, dataRow["OID"])).First()), insertIndex);
3401
                                    PathItems.Rows.InsertAt(createTerminatorRow(pathItemRows.First(), FROM_DATA), insertIndex);
3402

    
3403
                                    bCheck = true;
3404
                                }
3405
                            }
3406
                            else
3407
                            {
3408
                                foreach (DataRow loopRow in PipeSystemNetwork.Select(string.Format("OID = '{0}'", PSNItem.PSN_OID())))
3409
                                {
3410
                                    loopRow["FROM_DATA"] = pathItemRows.First().Field<string>("ITEMTAG");
3411
                                 
3412
                                    if (loopRow.Field<string>("OrderNumber") == "0")
3413
                                    {
3414
                                        if(loopRow["Status"].ToString().Contains(", Missing ItemTag or Description"))
3415
                                            loopRow["Status"] = loopRow["Status"].ToString().Replace(", Missing ItemTag or Description", "");
3416
                                        else if (loopRow["Status"].ToString().Contains("Missing ItemTag or Description"))
3417
                                            loopRow["Status"] = loopRow["Status"].ToString().Replace("Missing ItemTag or Description", "");
3418

    
3419
                                        if (!string.IsNullOrEmpty(loopRow["Status"].ToString()) && loopRow["Status"].ToString().Substring(0, 2).Equals(", "))
3420
                                            loopRow["Status"] = loopRow["Status"].ToString().Remove(0, 2);
3421
                                    }                                    
3422
                                }
3423

    
3424
                                bool bStatus = false;
3425
                                foreach (DataRow loopRow in PipeSystemNetwork.Select(string.Format("OID = '{0}'", PSNItem.PSN_OID())))
3426
                                {
3427
                                    if (!string.IsNullOrEmpty(loopRow["Status"].ToString()))
3428
                                        bStatus = true;
3429
                                }
3430

    
3431
                                if (!bStatus)
3432
                                {
3433
                                    foreach (DataRow loopRow in PipeSystemNetwork.Select(string.Format("OID = '{0}'", PSNItem.PSN_OID())))
3434
                                    {
3435
                                        loopRow["IsValid"] = string.Empty;
3436
                                    }
3437
                                }
3438
                            }
3439

    
3440

    
3441

    
3442
                            PSNItem.StartType = PSNType.Equipment;
3443
                        }
3444
                        catch (Exception ex)
3445
                        {
3446
                            MessageBox.Show("Please check the item.\r\nDrawingName : " + item.Document.DrawingName + "\r\nUID : " + item.UID, "ID2 " + id2Info.ProgramName, MessageBoxButtons.OK, MessageBoxIcon.Warning);
3447
                            return;
3448
                        }                     
3449
                    }
3450

    
3451
                    if (!PSNItem.EnableType(PSNItem.EndType))
3452
                    {
3453
                        DataRow[] pathItemRows = PathItems.Select(string.Format("PipeSystemNetwork_OID = '{0}'", dataRow["OID"]));
3454
                        //int insertIndex = PathItems.Rows.IndexOf(pathItemRows.First()) + pathItemRows.Count() - 1;
3455
                        DataRow dr = pathItemRows.Last();
3456
                        if (change)
3457
                        {
3458
                            if (bCheck)
3459
                                dr = pathItemRows[pathItemRows.Count() - 3];
3460
                            else
3461
                            {
3462
                                dr = pathItemRows[pathItemRows.Count() - 2];
3463
                            }
3464
                        }
3465

    
3466
                        change = true;
3467
                        int insertIndex = PathItems.Rows.IndexOf(dr) + 1;
3468

    
3469
                        Item item = PSNItem.Groups.Last().Items.Last();
3470
                            
3471
                        try
3472
                        {
3473
                            if (!item.Name.ToUpper().Equals("TIEINPOINT"))
3474
                            {
3475
                                string TO_DATA = string.Format("TIEINPOINT_{0:D5}V", tieInPointIndex);
3476

    
3477
                                foreach (DataRow loopRow in PipeSystemNetwork.Select(string.Format("OID = '{0}'", PSNItem.PSN_OID())))
3478
                                {
3479
                                    loopRow["TO_DATA"] = TO_DATA;
3480
                                    if (item.ItemType == ItemType.Line)
3481
                                    {
3482
                                        if (loopRow.Field<string>("OrderNumber") == Convert.ToString(PipeSystemNetwork.Select(string.Format("OID = '{0}'", PSNItem.PSN_OID())).Count() - 1))
3483
                                        {
3484
                                            string status = loopRow.Field<string>("Status");
3485
                                            if (string.IsNullOrEmpty(status))
3486
                                                status = "Line Disconnected";
3487
                                            else if (!status.Contains("Line Disconnected"))
3488
                                                status += ", Line Disconnected";
3489
                                            loopRow["Status"] = status;
3490
                                            if (!string.IsNullOrEmpty(status))
3491
                                                loopRow["IsValid"] = "Error";
3492
                                        }
3493
                                    }
3494
                                }
3495

    
3496
                                tieInPointIndex++;
3497

    
3498
                                if (item.ItemType == ItemType.Line)
3499
                                {
3500
                                    PathItems.Rows.InsertAt(createTerminatorRow(pathItemRows.Last(), TO_DATA), insertIndex);
3501
                                }
3502
                                else
3503
                                {
3504
                                    PathItems.Rows.InsertAt(createTerminatorRow(pathItemRows.Last(), TO_DATA), insertIndex);
3505
                                    PathItems.Rows.InsertAt(createLineRow(PathItems.Select(string.Format("PipeLine_OID = '{0}' AND PipeSystemNetwork_OID = '{1}'", item.PSNPipeLineID, dataRow["OID"])).Last()), insertIndex);
3506
                                }
3507
                            }
3508
                            else
3509
                            {
3510
                                foreach (DataRow loopRow in PipeSystemNetwork.Select(string.Format("OID = '{0}'", PSNItem.PSN_OID())))
3511
                                {
3512
                                    loopRow["TO_DATA"] = dr.Field<string>("ITEMTAG");
3513

    
3514
                                    if (loopRow.Field<string>("OrderNumber") == Convert.ToString(PipeSystemNetwork.Select(string.Format("OID = '{0}'", PSNItem.PSN_OID())).Count() - 1))
3515
                                    {
3516
                                        if (loopRow["Status"].ToString().Contains(", Missing ItemTag or Description"))
3517
                                            loopRow["Status"] = loopRow["Status"].ToString().Replace(", Missing ItemTag or Description", "");
3518
                                        else if (loopRow["Status"].ToString().Contains("Missing ItemTag or Description"))
3519
                                            loopRow["Status"] = loopRow["Status"].ToString().Replace("Missing ItemTag or Description", "");
3520

    
3521
                                        if (!string.IsNullOrEmpty(loopRow["Status"].ToString()) && loopRow["Status"].ToString().Substring(0, 2).Equals(", "))
3522
                                            loopRow["Status"] = loopRow["Status"].ToString().Remove(0, 2);
3523
                                    }
3524
                                }
3525

    
3526
                                bool bStatus = false;
3527
                                foreach (DataRow loopRow in PipeSystemNetwork.Select(string.Format("OID = '{0}'", PSNItem.PSN_OID())))
3528
                                {
3529
                                    if (!string.IsNullOrEmpty(loopRow["Status"].ToString()))
3530
                                        bStatus = true;
3531
                                }
3532

    
3533
                                if (!bStatus)
3534
                                {
3535
                                    foreach (DataRow loopRow in PipeSystemNetwork.Select(string.Format("OID = '{0}'", PSNItem.PSN_OID())))
3536
                                    {
3537
                                        loopRow["IsValid"] = string.Empty;
3538
                                    }
3539
                                }
3540
                            }
3541
                            PSNItem.EndType = PSNType.Equipment;
3542
                        }
3543
                        catch (Exception ex)
3544
                        {
3545
                            MessageBox.Show("Please check the item.\r\nDrawingName : " + item.Document.DrawingName + "\r\nUID : " + item.UID, "ID2 " + id2Info.ProgramName, MessageBoxButtons.OK, MessageBoxIcon.Warning);
3546
                            return;
3547
                        }
3548
                           
3549
                    }
3550

    
3551
                    dataRow["Type"] = PSNItem.GetPSNType();
3552
                    if (change)
3553
                    {
3554
                        int rowIndex = 0;
3555
                        for (int i = 0; i < PathItems.Rows.Count; i++)
3556
                        {
3557
                            DataRow row = PathItems.Rows[i];
3558
                            if (row["PipeSystemNetwork_OID"].ToString() != dataRow["OID"].ToString())
3559
                                continue;
3560
                            string sequenceData = row["SequenceData_OID"].ToString();
3561
                            string[] split = sequenceData.Split(new char[] { '_' });
3562

    
3563
                            StringBuilder sb = new StringBuilder();
3564
                            for (int j = 0; j < split.Length - 1; j++)
3565
                                sb.Append(split[j] + "_");
3566
                            sb.Append(rowIndex++);
3567
                            row["SequenceData_OID"] = sb.ToString();
3568

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

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

    
3574
                            if (seqItemRows == null)
3575
                            {
3576
                                DataRow newRow = SequenceData.NewRow();
3577
                                newRow["OID"] = sb.ToString();
3578
                                newRow["SERIALNUMBER"] = splitseq[splitseq.Length - 1];
3579
                                newRow["PathItem_OID"] = row["OID"];
3580
                                newRow["TopologySet_OID_Key"] = row["TopologySet_OID"];
3581
                                SequenceData.Rows.InsertAt(newRow, Convert.ToInt32(splitseq[splitseq.Length - 1]));
3582
                            }
3583
                            else
3584
                            {
3585
                                seqItemRows["OID"] = sb.ToString();
3586
                                seqItemRows["SERIALNUMBER"] = splitseq[splitseq.Length - 1];
3587
                                seqItemRows["PathItem_OID"] = row["OID"];
3588
                                seqItemRows["TopologySet_OID_Key"] = row["TopologySet_OID"];
3589
                            }
3590
                        }
3591
                    }
3592

    
3593
                    DataRow createTerminatorRow(DataRow itemRow, string DATA)
3594
                    {
3595
                        DataRow newRow = PathItems.NewRow();
3596
                        newRow["OID"] = Guid.NewGuid().ToString();
3597
                        newRow["SequenceData_OID"] = itemRow["SequenceData_OID"];
3598
                        newRow["TopologySet_OID"] = itemRow["TopologySet_OID"];
3599
                        newRow["BranchTopologySet_OID"] = itemRow["BranchTopologySet_OID"];
3600
                        newRow["PipeLine_OID"] = itemRow["PipeLine_OID"];
3601
                        newRow["ITEMNAME"] = "PipingComp"; //newRow["ITEMNAME"] = "End of line terminator";
3602
                        newRow["ITEMTAG"] = DATA; //itemRow["ITEMTAG"];
3603
                        newRow["DESCRIPTION"] = DATA;
3604
                        newRow["Class"] = "End of line terminator";
3605
                        newRow["SubClass"] = "End of line terminator";
3606
                        newRow["TYPE"] = "End";
3607
                        newRow["PIDNAME"] = itemRow["PIDNAME"];
3608
                        newRow["NPD"] = itemRow["NPD"];
3609
                        newRow["PipeSystemNetwork_OID"] = itemRow["PipeSystemNetwork_OID"];
3610
                        newRow["ViewPipeSystemNetwork_OID"] = itemRow["ViewPipeSystemNetwork_OID"];
3611
                        newRow["PipeRun_OID"] = itemRow["PipeRun_OID"];
3612
                        newRow["EGTConnectedPoint"] = "0";
3613
                        return newRow;
3614
                    }
3615

    
3616
                    DataRow createLineRow(DataRow itemRow)
3617
                    {
3618
                        DataRow newRow = PathItems.NewRow();
3619
                        newRow["OID"] = Guid.NewGuid().ToString();
3620
                        newRow["SequenceData_OID"] = itemRow["SequenceData_OID"];
3621
                        newRow["TopologySet_OID"] = itemRow["TopologySet_OID"];
3622
                        newRow["BranchTopologySet_OID"] = "";
3623
                        newRow["PipeLine_OID"] = itemRow["PipeLine_OID"];
3624
                        newRow["ITEMNAME"] = "PipeRun";
3625
                        newRow["ITEMTAG"] = itemRow["PipeRun_OID"];
3626
                        newRow["Class"] = "Piping";
3627
                        newRow["SubClass"] = "";
3628
                        newRow["TYPE"] = "Secondary";
3629
                        newRow["PIDNAME"] = itemRow["PIDNAME"];
3630
                        newRow["NPD"] = itemRow["NPD"];
3631
                        newRow["PipeSystemNetwork_OID"] = itemRow["PipeSystemNetwork_OID"];
3632
                        newRow["ViewPipeSystemNetwork_OID"] = itemRow["ViewPipeSystemNetwork_OID"];
3633
                        newRow["PipeRun_OID"] = itemRow["PipeRun_OID"];
3634
                        newRow["EGTConnectedPoint"] = "0";
3635
                        return newRow;
3636
                    }
3637
                }
3638
                catch (Exception ex)
3639
                {
3640

    
3641
                }
3642
            }
3643
        }
3644

    
3645
        private void InsertTeePSN()
3646
        {
3647
            DataTable dt = PipeSystemNetwork.DefaultView.ToTable(true, new string[] { "OID", "Type" });
3648
            DataRow[] branchRows = dt.Select("Type Like '%B%'");
3649
            bool change = false;
3650
            foreach (DataRow dataRow in branchRows)
3651
            {
3652
                try
3653
                {
3654
                    PSNItem PSNItem = PSNItems.Find(x => x.PSN_OID() == dataRow["OID"].ToString());
3655
                    change = false;
3656

    
3657
                    if (PSNItem.StartType == PSNType.Branch)
3658
                    {
3659
                        DataRow[] pathItemRows = PathItems.Select(string.Format("PipeSystemNetwork_OID = '{0}'", PSNItem.PSN_OID()));
3660
                        int insertIndex = PathItems.Rows.IndexOf(pathItemRows.First());
3661
                        Item Teeitem = PSNItem.Groups.First().Items.First();
3662
                        try
3663
                        {
3664
                            if (Teeitem.ItemType == ItemType.Line)
3665
                            {
3666
                                if (pathItemRows.First().Field<string>("SubClass") != "Tee")
3667
                                {
3668
                                    PathItems.Rows.InsertAt(createTeeRow(pathItemRows.First()), insertIndex);
3669
                                    pathItemRows.First().SetField("BranchTopologySet_OID", string.Empty);
3670
                                    pathItemRows.First().SetField("ViewPipeSystemNetwork_OID", dataRow["OID"].ToString());
3671
                                    change = true;
3672
                                }
3673

    
3674
                            }
3675
                        }
3676
                        catch (Exception ex)
3677
                        {
3678
                            MessageBox.Show("Please check the item.\r\nDrawingName : " + Teeitem.Document.DrawingName + "\r\nUID : " + Teeitem.UID, "ID2 " + id2Info.ProgramName, MessageBoxButtons.OK, MessageBoxIcon.Warning);
3679
                            return;
3680
                        }
3681
                    }
3682

    
3683
                    if (PSNItem.EndType == PSNType.Branch)
3684
                    {
3685
                        //change = true;
3686
                        DataRow[] pathItemRows = PathItems.Select(string.Format("PipeSystemNetwork_OID = '{0}'", PSNItem.PSN_OID()));
3687

    
3688
                        Item Teeitem = PSNItem.Groups.Last().Items.Last();
3689

    
3690
                        DataRow dr = pathItemRows.Last();
3691
                        if (change)
3692
                            dr = pathItemRows[pathItemRows.Count() - 2];
3693

    
3694
                        int insertIndex = PathItems.Rows.IndexOf(dr) + 1;
3695

    
3696
                        try
3697
                        {
3698
                            if (Teeitem.ItemType == ItemType.Line)
3699
                            {
3700
                                if (dr.Field<string>("SubClass") != "Tee")
3701
                                {
3702
                                    PathItems.Rows.InsertAt(createTeeRow(dr), insertIndex);
3703
                                    change = true;
3704
                                    dr.SetField("BranchTopologySet_OID", string.Empty);
3705
                                    dr.SetField("ViewPipeSystemNetwork_OID", dataRow["OID"].ToString());
3706
                                }
3707

    
3708
                            }
3709
                        }
3710
                        catch (Exception ex)
3711
                        {
3712
                            MessageBox.Show("Please check the item.\r\nDrawingName : " + Teeitem.Document.DrawingName + "\r\nUID : " + Teeitem.UID, "ID2 " + id2Info.ProgramName, MessageBoxButtons.OK, MessageBoxIcon.Warning);
3713
                            return;
3714
                        }
3715
                    }
3716

    
3717
                    if (change)
3718
                    {
3719
                        //DataRow[] pathItemRows = pathItemsDT.Select(string.Format("PipeSystemNetwork_OID = '{0}'", PSNItem.PSN_OID()));
3720
                        int rowIndex = 0;
3721
                        for (int i = 0; i < PathItems.Rows.Count; i++)
3722
                        {
3723
                            DataRow row = PathItems.Rows[i];
3724
                            if (row["PipeSystemNetwork_OID"].ToString() != PSNItem.PSN_OID())
3725
                                continue;
3726
                            string sequenceData = row["SequenceData_OID"].ToString();
3727
                            string[] split = sequenceData.Split(new char[] { '_' });
3728

    
3729
                            StringBuilder sb = new StringBuilder();
3730
                            for (int j = 0; j < split.Length - 1; j++)
3731
                                sb.Append(split[j] + "_");
3732
                            sb.Append(rowIndex++);
3733
                            row["SequenceData_OID"] = sb.ToString();
3734

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

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

    
3740
                            if (seqItemRows == null)
3741
                            {
3742
                                DataRow newRow = SequenceData.NewRow();
3743
                                newRow["OID"] = sb.ToString();
3744
                                newRow["SERIALNUMBER"] = splitseq[splitseq.Length - 1];
3745
                                newRow["PathItem_OID"] = row["OID"];
3746
                                newRow["TopologySet_OID_Key"] = row["TopologySet_OID"];
3747
                                SequenceData.Rows.InsertAt(newRow, Convert.ToInt32(splitseq[splitseq.Length - 1]));
3748
                            }
3749
                            else
3750
                            {
3751
                                seqItemRows["OID"] = sb.ToString();
3752
                                seqItemRows["SERIALNUMBER"] = splitseq[splitseq.Length - 1];
3753
                                seqItemRows["PathItem_OID"] = row["OID"];
3754
                                seqItemRows["TopologySet_OID_Key"] = row["TopologySet_OID"];
3755
                            }
3756
                        }
3757
                    }
3758

    
3759
                    DataRow createTeeRow(DataRow itemRow)
3760
                    {
3761
                        DataRow newRow = PathItems.NewRow();
3762
                        newRow["OID"] = Guid.NewGuid().ToString();
3763
                        newRow["SequenceData_OID"] = itemRow["SequenceData_OID"];
3764
                        newRow["TopologySet_OID"] = itemRow["TopologySet_OID"];
3765
                        newRow["BranchTopologySet_OID"] = itemRow["BranchTopologySet_OID"];
3766
                        newRow["PipeLine_OID"] = itemRow["PipeLine_OID"];
3767
                        newRow["ITEMNAME"] = "Branch"; //newRow["ITEMNAME"] = "End of line terminator";
3768
                        newRow["ITEMTAG"] = itemRow["ITEMTAG"];
3769
                        newRow["DESCRIPTION"] = "";
3770
                        newRow["Class"] = "Branch";
3771
                        newRow["SubClass"] = "Tee";
3772
                        newRow["TYPE"] = itemRow["TYPE"];
3773
                        newRow["PIDNAME"] = itemRow["PIDNAME"];
3774
                        newRow["NPD"] = itemRow["NPD"];
3775
                        newRow["PipeSystemNetwork_OID"] = itemRow["PipeSystemNetwork_OID"];
3776
                        newRow["ViewPipeSystemNetwork_OID"] = itemRow["ViewPipeSystemNetwork_OID"];
3777
                        newRow["PipeRun_OID"] = itemRow["PipeRun_OID"];
3778
                      
3779
                        newRow["EGTConnectedPoint"] = 0;
3780
                        return newRow;
3781
                    }
3782
                }
3783
                catch (Exception ex)
3784
                {
3785

    
3786
                }
3787
            }
3788
        }
3789
    }
3790

    
3791
    public class PSNItem
3792
    {
3793
        public PSNItem(int count, int Revision)
3794
        {
3795
            Groups = new List<Group>();
3796
            Topologies = new List<Topology>();
3797

    
3798
            Index = count + 1;
3799
            this.Revision = Revision;
3800
        }
3801

    
3802
        private int Revision;
3803
        public string UID { get; set; }
3804
        public List<Group> Groups { get; set; }
3805
        public List<Topology> Topologies { get; set; }
3806
        public PSNType StartType { get; set; }
3807
        public PSNType EndType { get; set; }
3808
        public int Index { get; set; }
3809
        public string IsValid { get; set; }
3810
        public bool IsKeyword { get; set; }
3811
        public string Status { get; set; }
3812
        public string IncludingVirtualData { get; set; }
3813
        public string PSNAccuracy { get; set; }
3814
        public KeywordInfo KeywordInfos = new KeywordInfo();
3815
        public DataTable Nozzle = new DataTable();
3816

    
3817
        public string PSN_OID()
3818
        {
3819
            return string.Format("V{0}-PSN-{1}", string.Format("{0:D4}", Revision), string.Format("{0:D5}", Index));
3820
        }
3821

    
3822
        public string GetPSNType()
3823
        {
3824
            string result = string.Empty;
3825

    
3826
            if (EnableType(StartType) && EnableType(EndType))
3827
            {
3828
                if (StartType == PSNType.Equipment && EndType == PSNType.Equipment)
3829
                    result = "E2E";
3830
                else if (StartType == PSNType.Branch && EndType == PSNType.Branch)
3831
                    result = "B2B";
3832
                else if (StartType == PSNType.Header && EndType == PSNType.Header)
3833
                    result = "HD2";
3834

    
3835
                else if (StartType == PSNType.Equipment && EndType == PSNType.Branch)
3836
                    result = "E2B";
3837
                else if (StartType == PSNType.Branch && EndType == PSNType.Equipment)
3838
                    result = "B2E";
3839

    
3840
                else if (StartType == PSNType.Header && EndType == PSNType.Branch)
3841
                    result = "HDB";
3842
                else if (StartType == PSNType.Branch && EndType == PSNType.Header)
3843
                    result = "HDB";
3844

    
3845
                else if (StartType == PSNType.Header && EndType == PSNType.Equipment)
3846
                    result = "HDE";
3847
                else if (StartType == PSNType.Equipment && EndType == PSNType.Header)
3848
                    result = "HDE";
3849
                else
3850
                    result = "Error";
3851
            }
3852
            else
3853
                result = "Error";
3854

    
3855
            return result;
3856

    
3857

    
3858
        }
3859

    
3860
        public bool EnableType(PSNType type)
3861
        {
3862
            bool result = false;
3863

    
3864
            if (type == PSNType.Branch ||
3865
                type == PSNType.Equipment ||
3866
                type == PSNType.Header)
3867
            {
3868
                result = true;
3869
            }
3870

    
3871
            return result;
3872
        }
3873

    
3874
        public bool IsBypass { get; set; }
3875

    
3876
        public string GetFromData(ref string Type, ref Item item, Item oItem, ref string OStatus)
3877
        {
3878
            Status = string.Empty;
3879
            string result = string.Empty;
3880
            if (IsKeyword)
3881
                IsKeyword = false;
3882
            try
3883
            {
3884
                item = Groups.First().Items.First();
3885

    
3886
                if (StartType == PSNType.Header)
3887
                    result = "ENDOFHEADER";
3888
                else if (StartType == PSNType.Branch)
3889
                {
3890
                    if (item.Relations.First().Item.LineNumber != null && !string.IsNullOrEmpty(item.Relations.First().Item.LineNumber.Name))
3891
                        result = item.Relations.First().Item.LineNumber.Name;
3892
                    else
3893
                    {
3894
                        IsValid = "Error";
3895
                        result = "Empty LineNumber";
3896
                    }
3897

    
3898
                        
3899

    
3900
                    //if (item.Relations.First().Item.LineNumber != null && !string.IsNullOrEmpty(item.Relations.First().Item.LineNumber.Name))
3901
                    //{
3902
                    //    result = item.Relations.First().Item.LineNumber.Name;
3903
                    //    if (item.Relations.First().Item.LineNumber.MissingLineNumber2)
3904
                    //    {
3905
                    //        Status += ", Missing LineNumber_2";
3906
                    //        IsValid = "Error";
3907
                    //    }
3908
                    //    if (item.Relations.First().Item.LineNumber.MissingLineNumber1)
3909
                    //    {
3910
                    //        Status += ", Missing LineNumber_1";
3911
                    //        IsValid = "Error";
3912
                    //    }
3913
                    //}
3914
                    //else
3915
                    //{
3916
                    //    IsValid = "Error";
3917
                    //    result = "Empty LineNumber";
3918
                    //}
3919
                }
3920
                else if (StartType == PSNType.Equipment)
3921
                {
3922
                    if (Groups.First().Items.First().Equipment != null)
3923
                        result = Groups.First().Items.First().Equipment.ItemTag;
3924
                    DataRow drNozzle = Nozzle.Select(string.Format("OID = '{0}'", Groups.First().Items.First().UID)).FirstOrDefault();
3925

    
3926
                    if (drNozzle != null)
3927
                        result += " [" + drNozzle.Field<string>("ITEMTAG") + "]";
3928
                }
3929
                else
3930
                {
3931
                    
3932
                    item = Groups.First().Items.First();
3933
                    if (item.ItemType == ItemType.Symbol)
3934
                    {
3935
                        string keyword = string.Empty;
3936
                        keyword = GetFromKeywordData(ref Type, item);
3937

    
3938
                        if (string.IsNullOrEmpty(keyword))
3939
                        {
3940
                            if (item.ID2DBType.Contains("OPC's"))
3941
                                Status += ", OPC Disconnected";
3942
                            else
3943
                                Status += ", Missing ItemTag or Description";
3944

    
3945
                            result = item.ID2DBName;
3946
                            IsValid = "Error";
3947
                        }
3948
                        else
3949
                        {
3950
                            result = keyword;
3951
                            IsKeyword = true;
3952
                        }
3953

    
3954
                    }
3955
                    else if (item.ItemType == ItemType.Line)
3956
                    {
3957
                        if (item.LineNumber != null && !string.IsNullOrEmpty(item.LineNumber.Name))
3958
                        {
3959
                            result = item.LineNumber.Name;
3960
                        }
3961
                        else
3962
                        {
3963
                            IsValid = "Error";
3964
                            result = "Empty LineNumber";
3965
                        }
3966

    
3967
                   
3968
                       
3969
                    }
3970
                    else
3971
                        result = "Unknown";
3972
                }
3973

    
3974
                if (oItem.LineNumber != null && !string.IsNullOrEmpty(oItem.LineNumber.Name))
3975
                {
3976
                    if (oItem.LineNumber.MissingLineNumber2)
3977
                    {
3978
                        OStatus += ", Missing LineNumber_2";
3979
                        IsValid = "Error";
3980
                    }
3981
                    if (oItem.LineNumber.MissingLineNumber1)
3982
                    {
3983
                        OStatus += ", Missing LineNumber_1";
3984
                        IsValid = "Error";
3985
                    }
3986
                }
3987
            }
3988
            catch (Exception ex)
3989
            {
3990

    
3991
            }
3992

    
3993
            return result;
3994
        }
3995

    
3996
        public string GetFromKeywordData(ref string Type, Item item)
3997
        {
3998
            string result = string.Empty;
3999

    
4000
            foreach (KeywordItem keyitem in KeywordInfos.KeywordItems)
4001
            {
4002
                if (keyitem.Name.Equals(item.Name))
4003
                {
4004
                    result = keyitem.Keyword;
4005
                    Type = item.ID2DBType;
4006
                    break;
4007
                }
4008
            }
4009

    
4010
            return result;
4011
        }
4012

    
4013
        public string GetToKeywordData(ref string Type, Item item)
4014
        {
4015
            string result = string.Empty;
4016

    
4017
            foreach (KeywordItem keyitem in KeywordInfos.KeywordItems)
4018
            {
4019
                if (keyitem.Name.Equals(item.Name))
4020
                {
4021
                    result = keyitem.Keyword;
4022
                    Type = item.ID2DBType;
4023
                    break;
4024
                }
4025
            }
4026
            return result;
4027
        }
4028

    
4029
        public string GetToData(ref string ToType, ref Item item, Item oItem)
4030
        {
4031
            string result = string.Empty;
4032
            Status = string.Empty;
4033

    
4034
            if (IsKeyword)
4035
                IsKeyword = false;
4036

    
4037
            item = Groups.Last().Items.Last();
4038

    
4039
            if (EndType == PSNType.Header)
4040
                result = "ENDOFHEADER";
4041
            else if (EndType == PSNType.Branch)
4042
            {
4043
                if (item.Relations.Last().Item.LineNumber != null && !string.IsNullOrEmpty(item.Relations.Last().Item.LineNumber.Name))
4044
                    result = item.Relations.Last().Item.LineNumber.Name;
4045
                else
4046
                {
4047
                    IsValid = "Error";
4048
                    result = "Empty LineNumber";
4049
                }
4050

    
4051
            
4052
              
4053
                //if (item.Relations.Last().Item.LineNumber != null && !string.IsNullOrEmpty(item.Relations.Last().Item.LineNumber.Name))
4054
                //{
4055
                //    result = item.Relations.Last().Item.LineNumber.Name;
4056
                //    if (item.Relations.Last().Item.LineNumber.MissingLineNumber2)
4057
                //    {
4058
                //        Status += ", Missing LineNumber_2";
4059
                //        IsValid = "Error";
4060
                //    }
4061
                //    if (item.Relations.Last().Item.LineNumber.MissingLineNumber1)
4062
                //    {
4063
                //        Status += ", Missing LineNumber_1";
4064
                //        IsValid = "Error";
4065
                //    }
4066
                //}
4067
                //else
4068
                //{
4069
                //    IsValid = "Error";
4070
                //    result = "Empty LineNumber";
4071
                //}                
4072
            }
4073
            else if (EndType == PSNType.Equipment)
4074
            {
4075
                if (Groups.Last().Items.Last().Equipment != null)
4076
                    result = Groups.Last().Items.Last().Equipment.ItemTag;
4077

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

    
4080
                if (drNozzle != null)
4081
                    result += " [" + drNozzle.Field<string>("ITEMTAG") + "]";
4082
            }
4083
            else
4084
            {
4085
                
4086
                item = Groups.Last().Items.Last();
4087
                if (item.ItemType == ItemType.Symbol)
4088
                {
4089
                    string keyword = string.Empty;
4090
                    keyword = GetToKeywordData(ref ToType, item);
4091

    
4092
                    if (string.IsNullOrEmpty(keyword))
4093
                    {
4094
                        if (item.ID2DBType.Contains("OPC's"))
4095
                            Status += ", OPC Disconnected";
4096
                        else
4097
                            Status += ", Missing ItemTag or Description";
4098

    
4099
                        result = item.ID2DBName;
4100
                        IsValid = "Error";
4101
                    }
4102
                    else
4103
                    {
4104
                        result = keyword;
4105
                        IsKeyword = true;
4106
                    }
4107

    
4108
                }
4109
                else if (item.ItemType == ItemType.Line)
4110
                {
4111
                    if (item.LineNumber != null && !string.IsNullOrEmpty(item.LineNumber.Name))
4112
                    {
4113
                        result = item.LineNumber.Name;
4114
                    }
4115
                    else
4116
                    {
4117
                        IsValid = "Error";
4118
                        result = "Empty LineNumber";
4119
                    }
4120

    
4121
                   
4122
                    
4123
                }
4124
                else
4125
                    result = "Unknown";
4126
            }
4127

    
4128
            return result;
4129
        }
4130

    
4131
        public string GetPBSData()
4132
        {
4133
            string result = string.Empty;
4134
            List<string> PBSList = new List<string>();
4135
            if (Settings.Default.PBSSetting.Equals("Line Number"))
4136
            {
4137
                string attrValue = Settings.Default.PBSSettingValue;
4138

    
4139
                foreach (Group group in Groups)
4140
                {
4141
                    List<LineNumber> lineNumbers = group.Items.Select(x => x.LineNumber).Distinct().ToList();
4142
                    foreach (LineNumber lineNumber in lineNumbers)
4143
                    {
4144
                        Attribute attribute = lineNumber.Attributes.Find(x => x.Name == attrValue && !string.IsNullOrEmpty(x.Value));
4145
                        if (attribute != null)
4146
                        {
4147
                            string value = attribute.Value;
4148
                            if (!PBSList.Contains(value))
4149
                                PBSList.Add(value);
4150
                        }
4151
                    }
4152
                }
4153
            }
4154
            else if (Settings.Default.PBSSetting.Equals("Item Attribute"))
4155
            {
4156
                string attrValue = Settings.Default.PBSSettingValue;
4157

    
4158
                foreach (Group group in Groups)
4159
                {
4160
                    List<Item> items = group.Items.FindAll(x => x.Attributes.Find(y => y.Name == attrValue && !string.IsNullOrEmpty(y.Value)) != null);
4161
                    foreach (Item item in items)
4162
                    {
4163
                        string value = item.Attributes.Find(x => x.Name == attrValue).Value;
4164
                        if (!PBSList.Contains(value))
4165
                            PBSList.Add(value);
4166
                    }
4167
                }
4168
            }
4169
            else if (Settings.Default.PBSSetting.Equals("Drawing No"))
4170
            {
4171
                string attrValue = Settings.Default.PBSSettingValue;
4172

    
4173
                foreach (Group group in Groups)
4174
                {
4175
                    List<Document> documents = group.Items.Select(x => x.Document).Distinct().ToList();
4176
                    foreach (Document document in documents)
4177
                    {
4178
                        string name = document.DrawingName;
4179

    
4180
                        int startIndex = Settings.Default.PBSSettingStartValue;
4181
                        int endIndex = Settings.Default.PBSSettingEndValue;
4182

    
4183
                        string subStr = name.Substring(startIndex - 1, endIndex - startIndex + 1);
4184
                        if (!PBSList.Contains(subStr))
4185
                            PBSList.Add(subStr);
4186
                    }
4187
                }
4188
            }
4189
            else if (Settings.Default.PBSSetting.Equals("Unit Area"))
4190
            {
4191
                foreach (Group group in Groups)
4192
                {
4193
                    List<Document> documents = group.Items.Select(x => x.Document).Distinct().ToList();
4194
                    foreach (Document document in documents)
4195
                    {
4196
                        List<TextInfo> textInfos = document.TextInfos.FindAll(x => x.Area == "Unit");
4197
                        foreach (TextInfo textInfo in textInfos)
4198
                        {
4199
                            if (!PBSList.Contains(textInfo.Value))
4200
                                PBSList.Add(textInfo.Value);
4201
                        }
4202
                    }
4203
                }
4204
            }
4205

    
4206
            foreach (var item in PBSList)
4207
            {
4208
                if (string.IsNullOrEmpty(result))
4209
                    result = item;
4210
                else
4211
                    result += ", " + item;
4212
            }
4213
            return result;
4214
        }
4215
    }
4216
}
클립보드 이미지 추가 (최대 크기: 500 MB)