프로젝트

일반

사용자정보

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

hytos / DTI_PID / ID2PSN / PSN.cs @ a5616391

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

1 6b9e7a56 gaqhf
using System;
2
using System.Collections.Generic;
3
using System.Data;
4
using System.IO;
5
using System.Linq;
6
using System.Text;
7
using System.Threading.Tasks;
8 7881ec8f gaqhf
using ID2PSN.Properties;
9
using System.Text.RegularExpressions;
10 5dfc785c LJIYEON
using System.Windows.Forms;
11 6b9e7a56 gaqhf
12
namespace ID2PSN
13
{
14
    public enum PSNType
15
    {
16
        None,
17
        Branch,
18
        Equipment,
19
        Header,
20
        Symbol,
21
        OPC,
22
    }
23
24 45529c16 LJIYEON
    public enum ErrorType
25
    {
26
        Error = -1,
27
        OK,
28
        InValid //이값은 들어가는데가 없음..
29
    }
30
31 6b9e7a56 gaqhf
    public class PSN
32
    {
33 8f24b438 gaqhf
        private double[] DrawingSize = null;
34
        private double DrawingWidth = double.NaN;
35
        private double DrawingHeight = double.NaN;
36
        public int Revision;
37 c6503eaa gaqhf
        public string EquipTagNoAttributeName = string.Empty;
38 6b9e7a56 gaqhf
        public DataTable PathItems { get; set; }
39
        public DataTable SequenceData { get; set; }
40
        public DataTable PipeSystemNetwork { get; set; }
41 36a45f13 gaqhf
        public DataTable TopologySet { get; set; }
42 6b9e7a56 gaqhf
        public DataTable Equipment { get; set; }
43
        public DataTable Nozzle { get; set; }
44 a36541fb LJIYEON
        public DataTable PipeLine { get; set; }
45 6b9e7a56 gaqhf
46 5e4c2ad1 LJIYEON
        public string Rule1 = "Line Disconnected";
47
        public string Rule2 = "Missing LineNumber"; //토폴로지데이터가 = Empty LineNumber 인것 ??..
48
        public string Rule3 = "OPC Disconneted";
49
        public string Rule4 = "Missing ItemTag or Description";
50 2ada3be8 LJIYEON
        public string Rule5 = "";
51
52 710a49f1 gaqhf
        int tieInPointIndex = 1;
53
54 6b9e7a56 gaqhf
        List<Document> Documents;
55
        List<Group> groups = new List<Group>();
56
        List<PSNItem> PSNItems = new List<PSNItem>();
57
        List<Topology> Topologies = new List<Topology>();
58
59
        DataTable opcDT = null;
60
        DataTable topologyRuleDT = null;
61
62 5dfc785c LJIYEON
        ID2Info id2Info = ID2Info.GetInstance();
63
64 eb44d82c LJIYEON
        
65
66 a36541fb LJIYEON
        //const string FluidPriorityType = "FLUIDCODE";
67
        //const string PipingMaterialsPriorityType = "PIPINGMATERIALSCLASS";
68 6b9e7a56 gaqhf
69 5c248ee3 gaqhf
        public PSN()
70
        {
71
            
72
        }
73
74 8f24b438 gaqhf
        public PSN(List<Document> documents, int Revision)
75 6b9e7a56 gaqhf
        {
76 5dfc785c LJIYEON
            try
77
            {
78
                Documents = documents;
79
                foreach (Document document in Documents)
80
                    groups.AddRange(document.Groups);
81
                opcDT = GetOPCInfo();
82
                topologyRuleDT = GetTopologyRule();
83
                this.Revision = Revision;
84
                DrawingSize = DB.GetDrawingSize();
85
                if (DrawingSize == null)
86
                {
87
                    MessageBox.Show("There is no data whose Section is Area and Key is Drawing in the drawing table.", "ID2 " + id2Info.ProgramName, MessageBoxButtons.OK, MessageBoxIcon.Information);
88
                    return;
89
                }
90
                DrawingWidth = DrawingSize[2] - DrawingSize[0];
91
                DrawingHeight = DrawingSize[3] - DrawingSize[1];
92
            }
93
            catch (Exception ex)
94
            {
95
                Log.Write(ex.Message + "\r\n" + ex.StackTrace);
96
                MessageBox.Show(ex.Message, "ID2 " + id2Info.ProgramName, MessageBoxButtons.OK, MessageBoxIcon.Information);
97
            }
98 6b9e7a56 gaqhf
        }
99
100 36a45f13 gaqhf
        private string GetItemTag(Item item)
101
        {
102
            string result = string.Empty;
103
            if (item.ItemType == ItemType.Line)
104
                result = item.LineNumber != null ? item.LineNumber.Name : string.Empty;
105
            else if (item.ItemType == ItemType.Symbol && item.SubItemType == SubItemType.Nozzle)
106
                result = Nozzle.Select(string.Format("OID = '{0}'", item.UID)).First()["ITEMTAG"].ToString();
107
108
            return result;
109
        }
110 7881ec8f gaqhf
        private string GetItemName(Item item, string itemOID)
111
        {
112
            string result = string.Empty;
113
            if (item.ItemType == ItemType.Line && (item.Name == "Secondary" || item.Name == "Primary"))
114
            {
115
                if (itemOID.Contains("_"))
116
                {
117
                    string split = itemOID.Split(new char[] { '_' })[1];
118
                    if (split.StartsWith("B"))
119
                        result = "Branch";
120
                    else
121
                        result = "PipeRun";
122
                }
123
                else
124
                    result = "PipeRun";
125
            }
126
            else if (item.ItemType == ItemType.Symbol)
127
            {
128
                if (item.ID2DBCategory == "Instrumentation")
129
                    result = "Instrument";
130
                else if (item.ID2DBType == "Nozzles")
131
                    result = "Nozzle";
132
                else if (item.ID2DBType == "Fittings" ||
133
                        item.ID2DBType == "Piping OPC's" ||
134
                        item.ID2DBType == "Specialty Components" ||
135
                        item.ID2DBType == "Valves" ||
136
                        item.ID2DBType == "Reducers")
137
                    result = "PipingComp";
138
            }
139
            return result;
140
        }
141
142
        private string GetClass(Item item, string itemOID)
143
        {
144
            string result = string.Empty;
145
            if (item.ItemType == ItemType.Line && (item.Name == "Secondary" || item.Name == "Primary"))
146
            {
147
                if (itemOID.Contains("_"))
148
                {
149
                    string split = itemOID.Split(new char[] { '_' })[1];
150
                    if (split.StartsWith("B"))
151
                        result = "Branch";
152
                    else
153
                        result = "Piping";
154
                }
155
                else
156
                    result = "Piping";
157
            }
158
            else if (item.ItemType == ItemType.Symbol)
159
            {
160
                if (item.ID2DBCategory == "Instrumentation")
161
                    result = item.ID2DBType;
162
                else if (item.ID2DBType == "Nozzles")
163
                    result = string.Empty;
164
                else if (item.ID2DBType == "Fittings" ||
165
                       item.ID2DBType == "Piping OPC's" ||
166
                       item.ID2DBType == "Specialty Components" ||
167
                       item.ID2DBType == "Valves" ||
168
                       item.ID2DBType == "Reducers")
169
                    result = item.ID2DBType;
170
            }
171
            return result;
172
        }
173
174
        private string GetSubClass(Item item, string itemOID)
175
        {
176
            string result = string.Empty;
177
            if (item.ItemType == ItemType.Line && (item.Name == "Secondary" || item.Name == "Primary"))
178
            {
179
                if (itemOID.Contains("_"))
180
                {
181
                    string split = itemOID.Split(new char[] { '_' })[1];
182
                    if (split.StartsWith("B"))
183
                        result = "Tee";
184
                    else
185
                        result = "";
186
                }
187
                else
188
                    result = "";
189
            }
190
            else if (item.ItemType == ItemType.Symbol)
191
            {
192
                if (item.ID2DBCategory == "Instrumentation")
193
                    result = string.Empty;
194
                else if (item.ID2DBType == "Nozzles")
195
                    result = string.Empty;
196
                else if (item.ID2DBType == "Fittings" ||
197
                       item.ID2DBType == "Piping OPC's" ||
198
                       item.ID2DBType == "Specialty Components" ||
199
                       item.ID2DBType == "Valves" ||
200
                       item.ID2DBType == "Reducers")
201
                    result = "In-line component";
202
            }
203
            return result;
204
        }
205 36a45f13 gaqhf
206 6b9e7a56 gaqhf
        public void SetPSNData()
207
        {
208 7881ec8f gaqhf
            // Item들의 속성으로 Topology Data를 생성한다.
209
            // Topology Data는 Topology Rule Setting을 기준으로 생성한다. 
210 327a7a9c LJIYEON
            SetTopologyData();
211 7881ec8f gaqhf
            // ID2의 OPC연결 Data 기반으로 Group(도면단위 PSN)을 연결한다.
212 6b9e7a56 gaqhf
            ConnectByOPC();
213 7881ec8f gaqhf
            // 실제 PSN 생성 로직
214
            // 연결된 Group을 하나의 PSN으로 만든다.
215 6b9e7a56 gaqhf
            SetPSNItem();
216 7881ec8f gaqhf
            // 생성된 PSN의 Type을 설정한다.
217 6b9e7a56 gaqhf
            SetPSNType();
218 7881ec8f gaqhf
            // ID2에는 Branch 정보가 없어서 Branch 정보를 생성한다.
219 6b9e7a56 gaqhf
            SetBranchInfo();
220 7881ec8f gaqhf
            // 생성된 Topology Data들을 정리하며 Main, Branch를 판단한다.
221 6b9e7a56 gaqhf
            SetTopology();
222 7881ec8f gaqhf
            // PSN이 Bypass인지 검사 
223
            SetPSNBypass();
224
            // Topology들에게 Index를 부여한다
225 5e4c2ad1 LJIYEON
            SetTopologyIndex();            
226 7881ec8f gaqhf
            // Nozzle, Equipment의 정보를 저장
227 6b9e7a56 gaqhf
            SaveNozzleAndEquipment();
228 7881ec8f gaqhf
            // PSN의 정보를 저장
229 6b9e7a56 gaqhf
            SavePSNData();
230 7881ec8f gaqhf
            // Topology의 subtype을 update(bypass, Header, 등등) 
231
            UpdateSubType();
232
            // Update Error
233 710a49f1 gaqhf
            UpdateErrorForPSN();
234 a5616391 LJIYEON
            // Update Keyword
235
            //UpdateKeywordForPSN();
236
         
237 5e4c2ad1 LJIYEON
            // 확도 계산
238 a36541fb LJIYEON
            UpdateAccuracy();          
239 6b9e7a56 gaqhf
        }
240 7881ec8f gaqhf
241 6b9e7a56 gaqhf
        private void SetTopologyData()
242
        {
243 710a49f1 gaqhf
            // 13번 excel
244
            foreach (Group group in groups)
245
            {
246
                LineNumber prevLineNumber = null;
247
                for (int i = 0; i < group.Items.Count; i++)
248
                {
249
                    Item item = group.Items[i];
250
                    LineNumber lineNumber = item.Document.LineNumbers.Find(x => x.UID == item.Owner);
251
                    if (lineNumber == null)
252
                    {
253
                        if (prevLineNumber != null)
254
                        {
255
                            if (!prevLineNumber.IsCopy)
256
                            {
257
                                prevLineNumber = prevLineNumber.Copy();
258
                                item.Document.LineNumbers.Add(prevLineNumber);
259
                            }
260
261
                            item.Owner = prevLineNumber.UID;
262
                        }
263
                    }
264
                    else
265
                        prevLineNumber = lineNumber;
266
                }
267
268
                prevLineNumber = null;
269
                for (int i = group.Items.Count - 1; i > -1; i--)
270
                {
271
                    Item item = group.Items[i];
272
                    LineNumber lineNumber = item.Document.LineNumbers.Find(x => x.UID == item.Owner);
273
                    if (lineNumber == null)
274
                    {
275
                        if (prevLineNumber != null)
276
                        {
277
                            if (!prevLineNumber.IsCopy)
278
                            {
279
                                prevLineNumber = prevLineNumber.Copy();
280
                                item.Document.LineNumbers.Add(prevLineNumber);
281
                            }
282
283
                            item.Owner = prevLineNumber.UID;
284
                        }
285
                    }
286
                    else
287
                        prevLineNumber = lineNumber;
288
                }
289
290
                if (prevLineNumber == null)
291
                {
292
                    List<LineNumber> lineNumbers = group.Document.LineNumbers.FindAll(x => !x.IsCopy);
293
                    Random random = new Random();
294
                    int index = random.Next(lineNumbers.Count - 1);
295
                    
296
                    // Copy
297
                    LineNumber cLineNumber = lineNumbers[index].Copy();
298
                    group.Document.LineNumbers.Add(cLineNumber);
299
300
                    foreach (Item item in group.Items)
301
                        item.Owner = cLineNumber.UID;
302
                }
303
            }
304
305 6b9e7a56 gaqhf
            foreach (Document document in Documents)
306
            {
307
                foreach (Item item in document.Items)
308
                {
309
                    item.TopologyData = string.Empty;
310 8f24b438 gaqhf
                    item.PSNPipeLineID = string.Empty;
311 33cee849 LJIYEON
                    List<string> pipeLineID = new List<string>();
312 6b9e7a56 gaqhf
                    LineNumber lineNumber = document.LineNumbers.Find(x => x.UID == item.Owner);
313
                    if (lineNumber != null)
314
                    {
315
                        item.LineNumber = lineNumber;
316
317
                        foreach (DataRow row in topologyRuleDT.Rows)
318
                        {
319
                            string uid = row["UID"].ToString();
320 33cee849 LJIYEON
                            //if (uid == "-")
321
                            //    pipeLineID.Add(item.TopologyData);//item.TopologyData += "-"; 
322
                            if (uid != "-")
323 6b9e7a56 gaqhf
                            {
324 f25b787a gaqhf
                                Attribute itemAttr = item.Attributes.Find(x => x.Name == uid);
325
326
                                Attribute attribute = lineNumber.Attributes.Find(x => x.Name == uid);
327 33cee849 LJIYEON
                                if (attribute != null && !string.IsNullOrEmpty(attribute.Value))
328
                                    pipeLineID.Add(attribute.Value);//item.TopologyData += attribute.Value;
329 6b9e7a56 gaqhf
                            }
330
                        }
331 8f24b438 gaqhf
332 3210f690 LJIYEON
                        if(topologyRuleDT.Select(string.Format("UID = '{0}'", "InsulationPurpose")) == null)
333
                        {
334
                            Attribute insulAttr = item.LineNumber.Attributes.Find(x => x.Name == "InsulationPurpose");
335
                            if (insulAttr != null && !string.IsNullOrEmpty(insulAttr.Value))
336
                                pipeLineID.Add(insulAttr.Value); //item.PSNPipeLineID = item.TopologyData + "-" + insulAttr.Value;
337
                                                                 //else
338
                                                                 //    item.PSNPipeLineID = item.TopologyData;
339
                        }
340
341 33cee849 LJIYEON
                        item.PSNPipeLineID = string.Join("-", pipeLineID);
342
                        item.TopologyData = string.Join("-", pipeLineID);
343 6b9e7a56 gaqhf
                    }
344 0f07fa34 gaqhf
                    else
345
                    {
346
                        item.TopologyData = "Empty LineNumber";
347
                        item.LineNumber = new LineNumber();
348
                    }
349 6b9e7a56 gaqhf
                }
350
            }
351
352 0f07fa34 gaqhf
            int emptyIndex = 1;
353
            foreach (Group group in groups)
354
            {
355
                List<Item> groupItems = group.Items.FindAll(x => x.TopologyData == "Empty LineNumber");
356
                if (groupItems.Count > 0)
357
                {
358
                    foreach (var item in groupItems)
359
                        item.TopologyData += string.Format("-{0}", emptyIndex);
360
                    emptyIndex++;
361
                }
362
            }
363 6b9e7a56 gaqhf
364
        }
365 5e4c2ad1 LJIYEON
366 6b9e7a56 gaqhf
        private void ConnectByOPC()
367
        {
368 21edb7bc LJIYEON
            try
369 6b9e7a56 gaqhf
            {
370 21edb7bc LJIYEON
                foreach (Group group in groups.FindAll(x => x.Items.Last().SubItemType == SubItemType.OPC))
371 6b9e7a56 gaqhf
                {
372 21edb7bc LJIYEON
                    Item opcItem = group.Items.Last();
373
                    DataRow[] fromRows = opcDT.Select(string.Format("FromOPCUID = '{0}'", opcItem.UID));
374
                    if (fromRows.Length.Equals(1))
375
                    {
376
                        DataRow opcRow = fromRows.First();
377
                        string toDrawing = opcRow["ToDrawing"].ToString();
378
                        string toOPCUID = opcRow["ToOPCUID"].ToString();
379
380
                        Document toDocument = Documents.Find(x => x.DrawingName == toDrawing);
381
                        if (toDocument != null)
382 710a49f1 gaqhf
                        {
383 21edb7bc LJIYEON
                            Group toGroup = toDocument.Groups.Find(x => x.Items.Find(y => y.UID == toOPCUID) != null);
384
                            DataRow[] toRows = opcDT.Select(string.Format("ToOPCUID = '{0}'", toGroup.Items.First().UID));
385
                            //1대1 매칭이 아닐때 걸림 (2개 이상일 때) 2021.11.07 
386
                            if (toRows.Length > 1)
387
                            {
388
                                //throw new Exception("OPC error(multi connect)");
389
                                MessageBox.Show("OPC error(multi connect)", "ID2 " + id2Info.ProgramName, MessageBoxButtons.OK, MessageBoxIcon.Information);
390
                                return;
391
                            }
392
                            group.EndGroup = toGroup;
393
                            toGroup.StartGroup = group;
394 710a49f1 gaqhf
                        }
395 0fe04b33 LJIYEON
                    }
396 6b9e7a56 gaqhf
                }
397
            }
398 21edb7bc LJIYEON
            catch (Exception ex)
399
            {
400
                Log.Write(ex.Message + "\r\n" + ex.StackTrace);
401
                //MessageBox.Show(ex.Message, "ID2 " + id2Info.ProgramName, MessageBoxButtons.OK, MessageBoxIcon.Information);
402
            }
403 6b9e7a56 gaqhf
        }
404 5e4c2ad1 LJIYEON
405 6b9e7a56 gaqhf
        private void SetPSNItem()
406
        {
407
            Dictionary<Group, string> groupDic = new Dictionary<Group, string>(); 
408
            foreach (Group group in groups)
409
                groupDic.Add(group, Guid.NewGuid().ToString());
410
411
            foreach (Group group in groups)
412
            {
413
                string groupKey = groupDic[group];
414
                if (group.StartGroup != null)
415
                {
416
                    string otherKey = groupDic[group.StartGroup];
417
                    ChangeGroupID(otherKey, groupKey);
418
                }
419
            }
420
421
            // PSN 정리
422
            foreach (var item in groupDic)
423
            {
424
                Group group = item.Key;
425
                string uid = item.Value;
426
                PSNItem PSNItem = PSNItems.Find(x => x.UID == uid);
427
                if (PSNItem == null)
428
                {
429 8f24b438 gaqhf
                    PSNItem = new PSNItem(PSNItems.Count, Revision) { UID = uid };
430 6b9e7a56 gaqhf
                    PSNItems.Add(PSNItem);
431
                }
432
                PSNItem.Groups.Add(group);
433 36a45f13 gaqhf
                foreach (Item groupItem in group.Items)
434
                    groupItem.PSNItem = PSNItem;
435 6b9e7a56 gaqhf
            }
436
437
            // Sort PSN
438
            foreach (PSNItem PSNItem in PSNItems)
439
            {
440
                List<Group> _groups = new List<Group>();
441
442
                Stack<Group> stacks = new Stack<Group>();
443
                stacks.Push(PSNItem.Groups.First());
444
                while (stacks.Count > 0)
445
                {
446
                    Group stack = stacks.Pop();
447
                    if (_groups.Contains(stack))
448
                        continue;
449
450
                    if (_groups.Count == 0)
451
                        _groups.Add(stack);
452
                    else
453
                    {
454
                        if (stack.StartGroup != null && _groups.Contains(stack.StartGroup))
455
                        {
456
                            int index = _groups.IndexOf(stack.StartGroup);
457
                            _groups.Insert(index + 1, stack);
458
                        }
459
                        else if (stack.EndGroup != null && _groups.Contains(stack.EndGroup))
460
                        {
461
                            int index = _groups.IndexOf(stack.EndGroup);
462
                            _groups.Insert(index, stack);
463
                        }
464
                    }
465
466
                    if (stack.StartGroup != null)
467
                        stacks.Push(stack.StartGroup);
468
                    if (stack.EndGroup != null)
469
                        stacks.Push(stack.EndGroup);
470
                }
471
472
                PSNItem.Groups.Clear();
473
                PSNItem.Groups.AddRange(_groups);
474
            }
475
476
            void ChangeGroupID(string from, string to)
477
            {
478
                if (from.Equals(to))
479
                    return;
480
481
                List<Group> changeItems = new List<Group>();
482
                foreach (var _item in groupDic)
483
                    if (_item.Value.Equals(from))
484
                        changeItems.Add(_item.Key);
485
                foreach (var _item in changeItems)
486
                    groupDic[_item] = to;
487
            }
488
        }
489 5e4c2ad1 LJIYEON
490 6b9e7a56 gaqhf
        private void SetPSNType()
491
        {
492
            foreach (PSNItem PSNItem in PSNItems)
493
            {
494
                Group firstGroup = PSNItem.Groups.First();
495
                Group lastGroup = PSNItem.Groups.Last();
496
497
                Item firstItem = firstGroup.Items.First();
498
                Item lastItem = lastGroup.Items.Last();
499
500
                PSNItem.StartType = GetPSNType(firstItem, true);
501
                PSNItem.EndType = GetPSNType(lastItem, false);
502
            }
503
504
            PSNType GetPSNType(Item item, bool bFirst = true)
505
            {
506
                PSNType type = PSNType.None;
507
508
                if (item.ItemType == ItemType.Line)
509
                {
510
                    Group group = groups.Find(x => x.Items.Contains(item));
511
                    if (bFirst && item.Relations[0].Item != null && !group.Items.Contains(item.Relations[0].Item))
512
                    {
513
                        Item connItem = item.Relations[0].Item;
514
                        if (connItem.ItemType == ItemType.Line && !IsConnected(item, connItem))
515
                            type = PSNType.Branch;
516
                        else if (connItem.ItemType == ItemType.Symbol)
517
                            type = PSNType.Symbol;
518
                    }
519
                    else if (!bFirst && item.Relations[1].Item != null && !group.Items.Contains(item.Relations[1].Item))
520
                    {
521
                        Item connItem = item.Relations[1].Item;
522
                        if (connItem.ItemType == ItemType.Line && !IsConnected(item, connItem))
523
                            type = PSNType.Branch;
524
                        else if (connItem.ItemType == ItemType.Symbol)
525
                            type = PSNType.Symbol;
526
                    }
527
                }
528
                else if (item.ItemType == ItemType.Symbol)
529
                {
530
                    if (item.SubItemType == SubItemType.Nozzle)
531
                        type = PSNType.Equipment;
532
                    else if (item.SubItemType == SubItemType.Header)
533
                        type = PSNType.Header;
534
                    else if (item.SubItemType == SubItemType.OPC)
535
                        type = PSNType.OPC;
536
                }
537
538
                return type;
539
            }
540
        }
541 5e4c2ad1 LJIYEON
542 6b9e7a56 gaqhf
        private void SetBranchInfo()
543
        {
544
            foreach (Document document in Documents)
545
            {
546
                List<Item> lines = document.Items.FindAll(x => x.ItemType == ItemType.Line).ToList();
547
                foreach (Item line in lines)
548
                {
549
                    double[] point = line.Relations[0].Point;
550
                    List<Item> connLines = lines.FindAll(x => x.Relations.Find(y => y.UID == line.UID) != null && line.Relations.Find(y => y.UID == x.UID) == null);
551
                    connLines.Sort(SortBranchLine);
552
                    line.BranchItems.AddRange(connLines);
553
554
                    int SortBranchLine(Item a, Item b)
555
                    {
556
                        double[] pointA = a.Relations[0].UID == line.UID ? a.Relations[0].Point : a.Relations[1].Point;
557
                        double distanceA = CalcPointToPointdDistance(point[0], point[1], pointA[0], pointA[1]);
558
559
                        double[] pointB = b.Relations[0].UID == line.UID ? b.Relations[0].Point : b.Relations[1].Point;
560
                        double distanceB = CalcPointToPointdDistance(point[0], point[1], pointB[0], pointB[1]);
561
562
                        // 내림차순
563
                        return distanceA.CompareTo(distanceB);
564
                    }
565
                    double CalcPointToPointdDistance(double x1, double y1, double x2, double y2)
566
                    {
567
                        return Math.Pow(Math.Pow(x1 - x2, 2) + Math.Pow(y1 - y2, 2), 0.5);
568
                    }
569
                }
570
            }
571
        }
572 5e4c2ad1 LJIYEON
573 6b9e7a56 gaqhf
        private void SetTopology()
574
        {
575 27d06aa8 LJIYEON
            try
576 6b9e7a56 gaqhf
            {
577 27d06aa8 LJIYEON
                #region 기본 topology 정리
578
                foreach (PSNItem PSNItem in PSNItems)
579 6b9e7a56 gaqhf
                {
580 27d06aa8 LJIYEON
                    Topology topology = null;
581
                    foreach (Group group in PSNItem.Groups)
582 6b9e7a56 gaqhf
                    {
583 27d06aa8 LJIYEON
                        foreach (Item item in group.Items)
584 6b9e7a56 gaqhf
                        {
585 27d06aa8 LJIYEON
                            if (string.IsNullOrEmpty(item.TopologyData))
586
                                topology = null;
587 6b9e7a56 gaqhf
                            else
588
                            {
589 27d06aa8 LJIYEON
                                if (topology == null)
590 6b9e7a56 gaqhf
                                {
591
                                    topology = new Topology()
592
                                    {
593
                                        ID = item.TopologyData
594
                                    };
595
                                    Topologies.Add(topology);
596
597
                                    if (!PSNItem.Topologies.Contains(topology))
598
                                        PSNItem.Topologies.Add(topology);
599
                                }
600 27d06aa8 LJIYEON
                                else
601
                                {
602
                                    if (topology.ID != item.TopologyData)
603
                                    {
604
                                        topology = new Topology()
605
                                        {
606
                                            ID = item.TopologyData
607
                                        };
608
                                        Topologies.Add(topology);
609
610
                                        if (!PSNItem.Topologies.Contains(topology))
611
                                            PSNItem.Topologies.Add(topology);
612
                                    }
613
                                }
614 6b9e7a56 gaqhf
615 27d06aa8 LJIYEON
                                item.Topology = topology;
616
                                topology.Items.Add(item);
617
                            }
618 6b9e7a56 gaqhf
                        }
619
                    }
620
                }
621 27d06aa8 LJIYEON
                #endregion
622 6b9e7a56 gaqhf
623 27d06aa8 LJIYEON
                #region Type
624
                List<string> ids = Topologies.Select(x => x.ID).Distinct().ToList();
625
                foreach (string id in ids)
626
                {
627
                    try
628
                    {
629 678760c6 gaqhf
630 6b9e7a56 gaqhf
631 27d06aa8 LJIYEON
                        List<Topology> topologies = Topologies.FindAll(x => x.ID == id);
632
633
                        // Main
634
                        List<Topology> mainTopologies = FindMainTopology(topologies);
635
                        foreach (Topology topology in mainTopologies)
636
                            topology.Type = "M";
637
638
                        // Branch
639
                        List<Topology> branchToplogies = topologies.FindAll(x => string.IsNullOrEmpty(x.Type));
640
                        foreach (Topology topology in branchToplogies)
641
                            topology.Type = "B";
642
                    }
643
                    catch (Exception ex)
644
                    {
645
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
646
                        MessageBox.Show(ex.Message, "ID2 " + id2Info.ProgramName, MessageBoxButtons.OK, MessageBoxIcon.Information);
647
                    }
648
                }
649
                #endregion
650
            }
651
            catch (Exception ex)
652
            {
653
                Log.Write(ex.Message + "\r\n" + ex.StackTrace);
654
                MessageBox.Show(ex.Message, "ID2 " + id2Info.ProgramName, MessageBoxButtons.OK, MessageBoxIcon.Information);
655 6b9e7a56 gaqhf
            }
656 27d06aa8 LJIYEON
657 6b9e7a56 gaqhf
        }
658 5e4c2ad1 LJIYEON
659 7881ec8f gaqhf
        private void SetTopologyIndex()
660
        {
661
            List<string> ids = Topologies.Select(x => x.ID).Distinct().ToList();
662
            foreach (string id in ids)
663
            {
664
                List<Topology> topologies = Topologies.FindAll(x => x.ID == id);
665
666
                // Main
667
                List<Topology> mainTopologies = topologies.FindAll(x => x.Type == "M");
668
                foreach (Topology topology in mainTopologies)
669
                    topology.Index = mainTopologies.IndexOf(topology).ToString();
670
671
                // Branch
672
                List<Topology> branchToplogies = topologies.FindAll(x => x.Type == "B");
673
                foreach (Topology topology in branchToplogies)
674
                    topology.Index = (branchToplogies.IndexOf(topology) + 1).ToString();
675
            }
676
        }
677 5e4c2ad1 LJIYEON
678 7881ec8f gaqhf
        private void SetPSNBypass()
679
        {
680
            foreach (PSNItem PSNItem in PSNItems)
681
                PSNItem.IsBypass = IsBypass(PSNItem);
682
        }
683 5e4c2ad1 LJIYEON
684 6b9e7a56 gaqhf
        private List<Topology> FindMainTopology(List<Topology> data)
685
        {
686 678760c6 gaqhf
            DataTable nominalDiameterDT = DB.SelectNominalDiameter();
687
            DataTable PMCDT = DB.SelectPSNPIPINGMATLCLASS();
688 a36541fb LJIYEON
            //2021.11.17 안쓰네 JY
689
            //DataTable fluidCodeDT = DB.SelectPSNFluidCode(); 
690 678760c6 gaqhf
691
            List<Topology> main = new List<Topology>();
692
            main.AddRange(data);
693 6b9e7a56 gaqhf
            //
694 678760c6 gaqhf
            main = GetNozzleTopology(data);
695
            if (main.Count == 1)
696
                return main;
697
            else
698
            {
699
                if (main.Count > 0)
700
                    main = GetPMCTopology(main);
701
                else
702
                    main = GetPMCTopology(data);
703
704
                if (main.Count == 1)
705
                    return main;
706
                else
707
                {
708
                    if (main.Count > 0)
709
                        main = GetDiaTopology(main);
710
                    else
711
                        main = GetDiaTopology(data);
712
713
714
                    if (main.Count == 1)
715
                        return main;
716
                    else
717
                    {
718
                        if (main.Count > 0)
719
                            main = GetItemTopology(main);
720
                        else
721
                            main = GetItemTopology(data);
722
                    }
723
                }
724
            }
725
726
            List<Topology> GetNozzleTopology(List<Topology> topologies)
727
            {
728
                return topologies.FindAll(x => x.Items.Find(y => y.SubItemType == SubItemType.Nozzle) != null);
729
            }
730 6b9e7a56 gaqhf
731 678760c6 gaqhf
            List<Topology> GetPMCTopology(List<Topology> topologies)
732
            {
733
                List<Topology> result = new List<Topology>();
734
                foreach (DataRow row in PMCDT.Rows)
735
                {
736
                    string value = row["CODE"].ToString();
737
                    foreach (Topology topology in topologies)
738
                    {
739
                        foreach (Item item in topology.Items)
740
                        {
741
                            if (item.LineNumber == null)
742
                                continue;
743 0f07fa34 gaqhf
                            Attribute attribute = item.LineNumber.Attributes.Find(x => x.Name == "PipingMaterialsClass");
744
                            if (attribute != null && value == attribute.Value)
745 678760c6 gaqhf
                            {
746
                                result.Add(topology);
747
                                break;
748
                            }
749
                        }
750
                    }
751
752
                    if (result.Count > 0)
753
                        break;
754
                }
755
756
                return result;
757
            }
758
759
            List<Topology> GetDiaTopology(List<Topology> topologies)
760
            {
761
                List<Topology> result = new List<Topology>();
762
                foreach (DataRow row in nominalDiameterDT.Rows)
763
                {
764
                    string inchValue = row["InchStr"].ToString();
765
                    string metricValue = row["MetricStr"].ToString();
766
                    foreach (Topology topology in topologies)
767
                    {
768
                        foreach (Item item in topology.Items)
769
                        {
770
                            if (item.LineNumber == null)
771
                                continue;
772 0f07fa34 gaqhf
                            Attribute attribute = item.LineNumber.Attributes.Find(x => x.Name == "NominalDiameter");
773
                            if (attribute != null && (inchValue == attribute.Value || metricValue == attribute.Value))
774 678760c6 gaqhf
                            {
775
                                result.Add(topology);
776
                                break;
777
                            }
778
                        }
779
                    }
780
781
                    if (result.Count > 0)
782
                        break;
783
                }
784
785
                return result;
786
            }
787
788
            List<Topology> GetItemTopology(List<Topology> topologies)
789
            {
790
                return new List<Topology>() { topologies.OrderByDescending(x => x.Items.Count).ToList().First() };
791
            }
792 6b9e7a56 gaqhf
793 678760c6 gaqhf
            return main;
794 6b9e7a56 gaqhf
        }
795
796
        private DataTable GetOPCInfo()
797
        {
798
            DataTable opc = DB.SelectOPCRelations();
799
            DataTable drawing = DB.SelectDrawings();
800
801
            DataTable dt = new DataTable();
802
            dt.Columns.Add("FromDrawing", typeof(string));
803
            dt.Columns.Add("FromDrawingUID", typeof(string));
804
            dt.Columns.Add("FromOPCUID", typeof(string));
805
            dt.Columns.Add("ToDrawing", typeof(string));
806
            dt.Columns.Add("ToDrawingUID", typeof(string));
807
            dt.Columns.Add("ToOPCUID", typeof(string));
808
            foreach (DataRow row in opc.Rows)
809
            {
810
                string fromDrawingUID = row["From_Drawings_UID"] == null ? string.Empty : row["From_Drawings_UID"].ToString();
811
                string fromOPCUID = row["From_OPC_UID"] == null ? string.Empty : row["From_OPC_UID"].ToString(); 
812
                string toDrawingUID = row["To_Drawings_UID"] == null ? string.Empty : row["To_Drawings_UID"].ToString(); 
813
                string toOPCUID = row["To_OPC_UID"] == null ? string.Empty : row["To_OPC_UID"].ToString();
814
                if (!string.IsNullOrEmpty(toOPCUID))
815
                {
816
                    DataRow[] fromRows = drawing.Select(string.Format("UID = '{0}'", fromDrawingUID));
817
                    DataRow[] toRows = drawing.Select(string.Format("UID = '{0}'", toDrawingUID));
818
                    if (fromRows.Length.Equals(1) && toRows.Length.Equals(1))
819
                    {
820
                        string fromDrawingName = Path.GetFileNameWithoutExtension(fromRows.First()["NAME"].ToString());
821
                        string toDrawingName = Path.GetFileNameWithoutExtension(toRows.First()["NAME"].ToString());
822
823
                        DataRow newRow = dt.NewRow();
824
                        newRow["FromDrawing"] = fromDrawingName;
825
                        newRow["FromDrawingUID"] = fromDrawingUID;
826
                        newRow["FromOPCUID"] = fromOPCUID;
827
                        newRow["ToDrawing"] = toDrawingName;
828
                        newRow["ToDrawingUID"] = toDrawingUID;
829
                        newRow["ToOPCUID"] = toOPCUID;
830
831
                        dt.Rows.Add(newRow);
832
                    }
833
                }
834
            }
835
836
            return dt;
837
        }
838 5e4c2ad1 LJIYEON
839 6b9e7a56 gaqhf
        private DataTable GetTopologyRule()
840
        {
841
            DataTable dt = DB.SelectTopologyRule();
842
843
            return dt;
844
        }
845 5e4c2ad1 LJIYEON
846 6b9e7a56 gaqhf
        private bool IsConnected(Item item1, Item item2)
847
        {
848
            if (item1.Relations.Find(x => x.UID.Equals(item2.UID)) != null &&
849
                item2.Relations.Find(x => x.UID.Equals(item1.UID)) != null)
850
                return true;
851
            else
852
                return false;
853
        }
854
855
        private void SaveNozzleAndEquipment()
856
        {
857
            List<Item> nozzles = new List<Item>();
858
            List<Equipment> equipments = new List<Equipment>();
859
            foreach (Document document in Documents)
860
            {
861
                nozzles.AddRange(document.Items.FindAll(x => x.SubItemType == SubItemType.Nozzle));
862
                equipments.AddRange(document.Equipments);
863
            }
864
                
865
866
            DataTable nozzleDT = new DataTable();
867
            nozzleDT.Columns.Add("OID", typeof(string));
868
            nozzleDT.Columns.Add("ITEMTAG", typeof(string));
869
            nozzleDT.Columns.Add("XCOORDS", typeof(string));
870
            nozzleDT.Columns.Add("YCOORDS", typeof(string));
871
            nozzleDT.Columns.Add("Equipment_OID", typeof(string));
872
            nozzleDT.Columns.Add("FLUID", typeof(string));
873
            nozzleDT.Columns.Add("NPD", typeof(string));
874 33cee849 LJIYEON
            nozzleDT.Columns.Add("PMC", typeof(string));
875 6b9e7a56 gaqhf
            nozzleDT.Columns.Add("ROTATION", typeof(string));
876
            nozzleDT.Columns.Add("FlowDirection", typeof(string));
877
878
            foreach (Item item in nozzles)
879
            {
880
                DataRow row = nozzleDT.NewRow();
881
                row["OID"] = item.UID;
882
883
                Relation relation = item.Relations.Find(x => equipments.Find(y => y.UID == x.UID) != null);
884
                if (relation != null)
885
                {
886
                    Equipment equipment = equipments.Find(x => x.UID == relation.UID);
887
                    equipment.Nozzles.Add(item);
888 a36541fb LJIYEON
                    row["ITEMTAG"] = string.Format("N{0}", string.Format("{0:D3}", equipment.Nozzles.Count + 100));
889 3210f690 LJIYEON
                    row["Equipment_OID"] = equipment.UID; 
890 4c76a67a gaqhf
                    item.Equipment = equipment;
891 6b9e7a56 gaqhf
                }
892 8f24b438 gaqhf
                row["XCOORDS"] = (item.POINT[0] / DrawingWidth).ToString();
893
                row["YCOORDS"] = (item.POINT[1] / DrawingHeight).ToString();
894 6b9e7a56 gaqhf
                Attribute fluidAttr = item.LineNumber.Attributes.Find(x => x.Name == "FluidCode");
895
                row["FLUID"] = fluidAttr != null ? fluidAttr.Value : string.Empty;
896
                Attribute npdAttr = item.LineNumber.Attributes.Find(x => x.Name == "NominalDiameter");
897
                row["NPD"] = npdAttr != null ? npdAttr.Value : string.Empty;
898 33cee849 LJIYEON
                Attribute pmcAttr = item.LineNumber.Attributes.Find(x => x.Name == "PipingMaterialsClass");
899
                row["PMC"] = pmcAttr != null ? pmcAttr.Value : string.Empty;
900 f25b787a gaqhf
901
                double angle = Math.PI * 2 - Convert.ToDouble(item.ANGLE);
902
                if (angle >= Math.PI * 2)
903
                    angle = angle - Math.PI * 2;
904
                row["ROTATION"] = angle.ToString();
905 6b9e7a56 gaqhf
906
                if (item.Topology.Items.First().Equals(item))
907
                    row["FlowDirection"] = "Outlet";
908
                else if (item.Topology.Items.Last().Equals(item))
909
                    row["FlowDirection"] = "Inlet";
910
                else
911
                    row["FlowDirection"] = string.Empty;
912
913
                nozzleDT.Rows.Add(row);
914
            }
915
916
            DataTable equipDT = new DataTable();
917
            equipDT.Columns.Add("OID", typeof(string));
918
            equipDT.Columns.Add("ITEMTAG", typeof(string));
919
            equipDT.Columns.Add("XCOORDS", typeof(string));
920
            equipDT.Columns.Add("YCOORDS", typeof(string));
921
922
            foreach (Equipment equipment in equipments)
923
            {
924
                DataRow row = equipDT.NewRow();
925
                row["OID"] = equipment.UID;
926 c6503eaa gaqhf
                if (!string.IsNullOrEmpty(EquipTagNoAttributeName))
927
                {
928
                    Attribute attribute = equipment.Attributes.Find(x => x.Name == EquipTagNoAttributeName);
929
                    if (attribute != null)
930
                        equipment.ItemTag = attribute.Value;
931
                }
932
                else
933
                    equipment.ItemTag = equipment.Name;
934 6b9e7a56 gaqhf
935 c6503eaa gaqhf
                row["ITEMTAG"] = equipment.ItemTag;
936 6b9e7a56 gaqhf
                List<double> xList = equipment.POINT.Select(x => x[0]).ToList();
937 8f24b438 gaqhf
                row["XCOORDS"] = (xList.Sum() / (double)xList.Count) / DrawingWidth;
938 6b9e7a56 gaqhf
939
                List<double> yList = equipment.POINT.Select(x => x[1]).ToList();
940 8f24b438 gaqhf
                row["YCOORDS"] = (yList.Sum() / (double)yList.Count) / DrawingHeight;
941 6b9e7a56 gaqhf
942
                equipDT.Rows.Add(row);
943
            }
944
945
            Equipment = equipDT;
946
            Nozzle = nozzleDT;
947
        }
948 5e4c2ad1 LJIYEON
949 6b9e7a56 gaqhf
        private void SavePSNData()
950
        {
951 2c46461b LJIYEON
            try
952 36a45f13 gaqhf
            {
953 2c46461b LJIYEON
                DataTable pathItemsDT = new DataTable();
954
                pathItemsDT.Columns.Add("OID", typeof(string));
955
                pathItemsDT.Columns.Add("SequenceData_OID", typeof(string));
956
                pathItemsDT.Columns.Add("TopologySet_OID", typeof(string));
957
                pathItemsDT.Columns.Add("BranchTopologySet_OID", typeof(string));
958
                pathItemsDT.Columns.Add("PipeLine_OID", typeof(string));
959
                pathItemsDT.Columns.Add("ITEMNAME", typeof(string));
960
                pathItemsDT.Columns.Add("ITEMTAG", typeof(string));
961 a36541fb LJIYEON
                pathItemsDT.Columns.Add("DESCRIPTION", typeof(string));                
962 2c46461b LJIYEON
                pathItemsDT.Columns.Add("Class", typeof(string));
963
                pathItemsDT.Columns.Add("SubClass", typeof(string));
964
                pathItemsDT.Columns.Add("TYPE", typeof(string));
965
                pathItemsDT.Columns.Add("PIDNAME", typeof(string));
966 a36541fb LJIYEON
                pathItemsDT.Columns.Add("Equipment_OID", typeof(string));
967 2c46461b LJIYEON
                pathItemsDT.Columns.Add("NPD", typeof(string));
968
                pathItemsDT.Columns.Add("PipeSystemNetwork_OID", typeof(string));
969
                pathItemsDT.Columns.Add("ViewPipeSystemNetwork_OID", typeof(string));
970
                pathItemsDT.Columns.Add("PipeRun_OID", typeof(string));
971
972
                DataTable sequenceDataDT = new DataTable();
973
                sequenceDataDT.Columns.Add("OID", typeof(string));
974
                sequenceDataDT.Columns.Add("SERIALNUMBER", typeof(string));
975
                sequenceDataDT.Columns.Add("PathItem_OID", typeof(string));
976
                sequenceDataDT.Columns.Add("TopologySet_OID_Key", typeof(string));
977
978
                DataTable pipeSystemNetworkDT = new DataTable();
979
                pipeSystemNetworkDT.Columns.Add("OID", typeof(string));
980
                pipeSystemNetworkDT.Columns.Add("Type", typeof(string));
981
                pipeSystemNetworkDT.Columns.Add("OrderNumber", typeof(string));
982
                pipeSystemNetworkDT.Columns.Add("Pipeline_OID", typeof(string));
983
                pipeSystemNetworkDT.Columns.Add("FROM_DATA", typeof(string));
984
                pipeSystemNetworkDT.Columns.Add("TO_DATA", typeof(string));
985
                pipeSystemNetworkDT.Columns.Add("TopologySet_OID_Key", typeof(string));
986
                pipeSystemNetworkDT.Columns.Add("PSNRevisionNumber", typeof(string));
987
                pipeSystemNetworkDT.Columns.Add("PBS", typeof(string));
988 72775f2e LJIYEON
                pipeSystemNetworkDT.Columns.Add("Drawings", typeof(string));
989
                pipeSystemNetworkDT.Columns.Add("IsValid", typeof(string));
990 2c46461b LJIYEON
                pipeSystemNetworkDT.Columns.Add("Status", typeof(string));
991 ddc1c369 LJIYEON
                pipeSystemNetworkDT.Columns.Add("IncludingVirtualData", typeof(string));
992
                pipeSystemNetworkDT.Columns.Add("PSNAccuracy", typeof(string));
993 2c46461b LJIYEON
994
                DataTable topologySetDT = new DataTable();
995
                topologySetDT.Columns.Add("OID", typeof(string));
996
                topologySetDT.Columns.Add("Type", typeof(string));
997
                topologySetDT.Columns.Add("SubType", typeof(string));
998
                topologySetDT.Columns.Add("HeadItemTag", typeof(string));
999
                topologySetDT.Columns.Add("TailItemTag", typeof(string));
1000 72775f2e LJIYEON
                topologySetDT.Columns.Add("HeadItemSPID", typeof(string));
1001
                topologySetDT.Columns.Add("TailItemSPID", typeof(string));
1002 2c46461b LJIYEON
1003 f9f2787b LJIYEON
                DataTable pipelineDT = new DataTable();
1004
                pipelineDT.Columns.Add("OID", typeof(string));
1005
                pipelineDT.Columns.Add("PipeSystem_OID", typeof(string));
1006
                pipelineDT.Columns.Add("FLUID", typeof(string));
1007
                pipelineDT.Columns.Add("PMC", typeof(string));
1008
                pipelineDT.Columns.Add("SEQNUMBER", typeof(string));
1009
                pipelineDT.Columns.Add("INSULATION", typeof(string));
1010
                pipelineDT.Columns.Add("FROM_DATA", typeof(string));
1011
                pipelineDT.Columns.Add("TO_DATA", typeof(string));
1012
                pipelineDT.Columns.Add("Unit", typeof(string));
1013
1014 879ce10b LJIYEON
                // Set Vent/Drain Info
1015 2c46461b LJIYEON
                List<VentDrainInfo> VentDrainInfos = new List<VentDrainInfo>();
1016
                DataTable dt = DB.SelectVentDrainSetting();
1017
                foreach (DataRow row in dt.Rows)
1018 36a45f13 gaqhf
                {
1019 2c46461b LJIYEON
                    string groupID = row["GROUP_ID"].ToString();
1020
                    string desc = row["DESCRIPTION"].ToString();
1021
                    int index = Convert.ToInt32(row["INDEX"]);
1022
                    string name = row["NAME"].ToString();
1023 36a45f13 gaqhf
1024 2c46461b LJIYEON
                    VentDrainInfo ventDrainInfo = VentDrainInfos.Find(x => x.UID.Equals(groupID));
1025
                    if (ventDrainInfo == null)
1026 36a45f13 gaqhf
                    {
1027 2c46461b LJIYEON
                        ventDrainInfo = new VentDrainInfo(groupID);
1028
                        ventDrainInfo.Description = desc;
1029
                        VentDrainInfos.Add(ventDrainInfo);
1030 36a45f13 gaqhf
                    }
1031
1032 2c46461b LJIYEON
                    ventDrainInfo.VentDrainItems.Add(new VentDrainItem()
1033
                    {
1034
                        Index = index,
1035
                        Name = name
1036
                    });
1037
                }
1038
1039 879ce10b LJIYEON
                #region Keyword Info
1040 eb44d82c LJIYEON
                KeywordInfo KeywordInfos = new KeywordInfo();
1041 879ce10b LJIYEON
                DataTable dtKeyword = DB.SelectKeywordsSetting();
1042
                foreach (DataRow row in dtKeyword.Rows)
1043
                {
1044
                    int index = Convert.ToInt32(row["INDEX"]);
1045
                    string name = row["NAME"].ToString();
1046
                    string keyword = row["KEYWORD"].ToString();
1047
1048 eb44d82c LJIYEON
                    //KeywordInfo keywordInfo = new KeywordInfo();   
1049
                    KeywordInfos.KeywordItems.Add(new KeywordItem()
1050 879ce10b LJIYEON
                    {
1051
                        Index = index,
1052
                        Name = name,
1053
                        Keyword = keyword
1054
                    });
1055
                }               
1056
                #endregion
1057
1058 2c46461b LJIYEON
                // key = 미입력 branch
1059
                Dictionary<Item, Item> startBranchDic = new Dictionary<Item, Item>();
1060
                Dictionary<Item, Item> endBranchDic = new Dictionary<Item, Item>();
1061 f9f2787b LJIYEON
1062 2c46461b LJIYEON
                foreach (PSNItem PSNItem in PSNItems)
1063
                {
1064 3210f690 LJIYEON
                    try
1065 36a45f13 gaqhf
                    {
1066 3210f690 LJIYEON
                        int psnOrder = 0;
1067
                        int index = 0;
1068
                        bool bPSNStart = true;
1069
                        string sPSNData = string.Empty;
1070
                        bool bVentDrain = false;
1071
1072
                        //VentDrain 검사
1073
                        if (PSNItem.Groups.Count.Equals(1))
1074 36a45f13 gaqhf
                        {
1075 3210f690 LJIYEON
                            List<VentDrainInfo> endInfos = new List<VentDrainInfo>();
1076
                            Group group = PSNItem.Groups[0];
1077 a5616391 LJIYEON
                            List<Item> items = group.Items.FindAll(x => x.Name != "PSN_Nozzle");
1078
                            for (int i = 0; i < items.Count; i++)
1079 2c46461b LJIYEON
                            {
1080 a5616391 LJIYEON
                                Item item = items[i];                               
1081
1082 2c46461b LJIYEON
                                foreach (VentDrainInfo ventDrainInfo in VentDrainInfos)
1083
                                {
1084 a5616391 LJIYEON
                                    if (endInfos.Contains(ventDrainInfo) || ventDrainInfo.VentDrainItems.Count != items.Count)
1085 2c46461b LJIYEON
                                        continue;
1086
                                    if (!ventDrainInfo.VentDrainItems[i].Name.Equals(item.Name))
1087
                                    {
1088
                                        endInfos.Add(ventDrainInfo);
1089
                                        continue;
1090
                                    }
1091
1092 a5616391 LJIYEON
                                    if (i + 1 == items.Count && items.Count.Equals(ventDrainInfo.VentDrainItems.Count))
1093 3210f690 LJIYEON
                                    {
1094 2c46461b LJIYEON
                                        bVentDrain = true;
1095 3210f690 LJIYEON
                                    }
1096 2c46461b LJIYEON
                                }
1097
                            }
1098
1099 3210f690 LJIYEON
                            if (!bVentDrain)
1100 2c46461b LJIYEON
                            {
1101 3210f690 LJIYEON
                                endInfos = new List<VentDrainInfo>();
1102 a5616391 LJIYEON
                                for (int i = 0; i < items.Count; i++)
1103 2c46461b LJIYEON
                                {
1104 a5616391 LJIYEON
                                    Item item = items[items.Count - i - 1];
1105
                                   
1106 3210f690 LJIYEON
                                    foreach (VentDrainInfo ventDrainInfo in VentDrainInfos)
1107 27d06aa8 LJIYEON
                                    {
1108 a5616391 LJIYEON
                                        if (endInfos.Contains(ventDrainInfo) || ventDrainInfo.VentDrainItems.Count != items.Count)
1109 3210f690 LJIYEON
                                            continue;
1110
                                        if (!ventDrainInfo.VentDrainItems[i].Name.Equals(item.Name))
1111
                                        {
1112
                                            endInfos.Add(ventDrainInfo);
1113
                                            continue;
1114
                                        }
1115 6b9e7a56 gaqhf
1116 a5616391 LJIYEON
                                        if (i + 1 == items.Count && items.Count.Equals(ventDrainInfo.VentDrainItems.Count))
1117 3210f690 LJIYEON
                                        {
1118
                                            bVentDrain = true;
1119
                                        }
1120 27d06aa8 LJIYEON
                                    }
1121
                                }
1122 3210f690 LJIYEON
                            }
1123
                        }
1124 a5616391 LJIYEON
                                                                       
1125
                        try
1126
                        {                            
1127
                            //PSN, PathItems, SequenceData 관련
1128
                            foreach (Group group in PSNItem.Groups)
1129
                            {                                
1130
                                foreach (Item item in group.Items)
1131 27d06aa8 LJIYEON
                                {
1132 a5616391 LJIYEON
                                    string PathitemUID = string.Empty;
1133
                                    if (item.BranchItems.Count == 0)
1134 a36541fb LJIYEON
                                    {
1135 a5616391 LJIYEON
                                        PathitemUID = item.UID;
1136
                                        CreatePathItemsDataRow(PathitemUID, item.ID2DBType);
1137
                                        CreateSequenceDataDataRow(PathitemUID);
1138
                                        index++;
1139
                                    }
1140
                                    else
1141
                                    {
1142
                                        PathitemUID = item.UID + "_L1";
1143
                                        CreatePathItemsDataRow(PathitemUID, item.ID2DBType);
1144
                                        CreateSequenceDataDataRow(PathitemUID);
1145
                                        index++;
1146
                                        for (int i = 0; i < item.BranchItems.Count; i++)
1147 3210f690 LJIYEON
                                        {
1148 a5616391 LJIYEON
                                            CreatePathItemsDataRow(string.Format(item.UID + "_B{0}", i + 1), "Branch", item.BranchItems[i].Topology.FullName, item.BranchItems[i]);
1149
                                            CreateSequenceDataDataRow(string.Format(item.UID + "_B{0}", i + 1));
1150 3210f690 LJIYEON
                                            index++;
1151
                                                
1152 a5616391 LJIYEON
                                            CreatePathItemsDataRow(string.Format(item.UID + "_L{0}", i + 2), item.ID2DBType);
1153
                                            CreateSequenceDataDataRow(string.Format(item.UID + "_L{0}", i + 2));
1154
                                            index++;
1155 a36541fb LJIYEON
1156 a5616391 LJIYEON
                                            if (item.BranchItems[i].Relations[0].Item != null && item.BranchItems[i].Relations[0].Item == item)
1157
                                                startBranchDic.Add(item.BranchItems[i], item);
1158
                                            else if (item.BranchItems[i].Relations[1].Item != null && item.BranchItems[i].Relations[1].Item == item)
1159
                                                endBranchDic.Add(item.BranchItems[i], item);
1160
                                        }
1161
                                    }
1162
                                    if (bPSNStart)
1163
                                    {
1164
                                        CreatePipeSystemNetworkDataRow();
1165
                                        sPSNData = item.TopologyData;
1166
                                        psnOrder++;
1167
                                        bPSNStart = false;
1168
                                    }
1169
                                    else
1170
                                    {
1171
                                        if (item.TopologyData != sPSNData)
1172 3210f690 LJIYEON
                                        {
1173
                                            CreatePipeSystemNetworkDataRow();
1174
                                            sPSNData = item.TopologyData;
1175
                                            psnOrder++;
1176
                                        }
1177 a5616391 LJIYEON
                                    }
1178
                                    void CreatePathItemsDataRow(string itemOID, string itemType, string branchTopologyName = "", Item branchItem = null)
1179
                                    {
1180
                                        DataRow newRow = pathItemsDT.NewRow();
1181
1182
                                        if (itemType == "Nozzles")
1183 3210f690 LJIYEON
                                        {
1184 a5616391 LJIYEON
                                            newRow["Equipment_OID"] = item.Equipment.UID;
1185 3210f690 LJIYEON
                                        }
1186 27d06aa8 LJIYEON
1187 a5616391 LJIYEON
                                        newRow["OID"] = itemOID;
1188
                                        newRow["SequenceData_OID"] = string.Format(item.Topology.FullName + "_{0}", index);
1189
                                        newRow["TopologySet_OID"] = item.Topology.FullName;
1190
                                        newRow["BranchTopologySet_OID"] = branchTopologyName;
1191
                                        newRow["PipeLine_OID"] = item.PSNPipeLineID;
1192
                                        newRow["ITEMNAME"] = GetItemName(item, itemOID);
1193
                                        newRow["ITEMTAG"] = GetItemTag(item);
1194
                                        newRow["Class"] = GetClass(item, itemOID);
1195
                                        string subClass = GetSubClass(item, itemOID);                                           
1196
                                        newRow["SubClass"] = subClass;
1197
1198
                                        if (item.ItemType == ItemType.Symbol)
1199
                                            newRow["TYPE"] = item.ID2DBName;
1200
                                        else if (item.ItemType == ItemType.Line)
1201
                                            newRow["TYPE"] = item.ID2DBType;
1202
                                        newRow["PIDNAME"] = group.Document.DrawingName;
1203
1204
                                        // NPD
1205
                                        if (item.LineNumber != null)
1206
                                        {
1207
                                            Attribute attribute = item.LineNumber.Attributes.Find(x => x.Name == "NominalDiameter");
1208
                                            if (attribute != null)
1209
                                                newRow["NPD"] = attribute.Value;
1210
                                        }
1211
                                        else
1212
                                            newRow["NPD"] = null;
1213 7881ec8f gaqhf
1214 6b9e7a56 gaqhf
1215 a5616391 LJIYEON
                                        newRow["PipeSystemNetwork_OID"] = PSNItem.PSN_OID();
1216
                                        if (branchItem == null)
1217
                                            newRow["ViewPipeSystemNetwork_OID"] = PSNItem.PSN_OID();
1218
                                        else
1219
                                            newRow["ViewPipeSystemNetwork_OID"] = branchItem.PSNItem.PSN_OID();
1220 27d06aa8 LJIYEON
1221 a5616391 LJIYEON
                                        newRow["PipeRun_OID"] = item.LineNumber != null ? item.LineNumber.Name : string.Empty;
1222 6b9e7a56 gaqhf
1223 a5616391 LJIYEON
                                        pathItemsDT.Rows.Add(newRow);
1224
                                    }
1225
                                    void UpdatePathItemsDataRow(string itemOID, string Type, string Text)
1226
                                    {
1227
                                        if (pathItemsDT.Select(string.Format("OID = '{0}'", itemOID)).FirstOrDefault() != null)
1228 3210f690 LJIYEON
                                        {
1229 a5616391 LJIYEON
                                            //pathItemsDT.Select(string.Format("OID = '{0}'", itemOID)).FirstOrDefault()["TYPE"] = Type;
1230
                                            pathItemsDT.Select(string.Format("OID = '{0}'", itemOID)).FirstOrDefault()["ITEMTAG"] = Text;
1231
                                            pathItemsDT.Select(string.Format("OID = '{0}'", itemOID)).FirstOrDefault()["DESCRIPTION"] = Text;
1232
                                            pathItemsDT.Select(string.Format("OID = '{0}'", itemOID)).FirstOrDefault()["ITEMNAME"] = "PipingComp";
1233 3210f690 LJIYEON
                                        }
1234 a5616391 LJIYEON
                                    }
1235
                                    void UpdatePathItemsKeywordDataRow(string itemOID, string Text, string Type)
1236
                                    {
1237
                                        if (pathItemsDT.Select(string.Format("OID = '{0}'", itemOID)).FirstOrDefault() != null)
1238 839708c6 LJIYEON
                                        {
1239 a5616391 LJIYEON
                                            pathItemsDT.Select(string.Format("OID = '{0}'", itemOID)).FirstOrDefault()["CLASS"] = Type;
1240
                                            pathItemsDT.Select(string.Format("OID = '{0}'", itemOID)).FirstOrDefault()["TYPE"] = "End";
1241
                                            pathItemsDT.Select(string.Format("OID = '{0}'", itemOID)).FirstOrDefault()["ITEMTAG"] = Text;
1242
                                            pathItemsDT.Select(string.Format("OID = '{0}'", itemOID)).FirstOrDefault()["DESCRIPTION"] = Text;
1243 3210f690 LJIYEON
                                        }
1244 a5616391 LJIYEON
                                    }
1245
                                    void CreateSequenceDataDataRow(string itemOID)
1246
                                    {
1247
                                        DataRow newRow = sequenceDataDT.NewRow();
1248
                                        newRow["OID"] = string.Format(item.Topology.FullName + "_{0}", index);
1249
                                        newRow["SERIALNUMBER"] = string.Format("{0}", index);
1250
                                        newRow["PathItem_OID"] = itemOID;
1251
                                        newRow["TopologySet_OID_Key"] = item.Topology.FullName;
1252 839708c6 LJIYEON
1253 a5616391 LJIYEON
                                        sequenceDataDT.Rows.Add(newRow);
1254
                                    }
1255
                                    void CreatePipeSystemNetworkDataRow()
1256
                                    {                                       
1257
                                        LineNumber lineNumber = item.Document.LineNumbers.Find(x => x.UID == item.Owner);
1258
                                        if (lineNumber != null)
1259 3210f690 LJIYEON
                                        {
1260 a5616391 LJIYEON
                                            List<Attribute> att = lineNumber.Attributes;
1261
                                            if (att != null)
1262 3210f690 LJIYEON
                                            {
1263 a5616391 LJIYEON
                                                List<string> oid = new List<string>();
1264
                                                string FluidCode = att.Where(x => x.Name.ToUpper().Equals("FLUIDCODE")).FirstOrDefault() != null ? att.Where(x => x.Name.ToUpper().Equals("FLUIDCODE")).FirstOrDefault().Value : string.Empty;
1265
                                                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;
1266
                                                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;
1267
                                                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;
1268
                                                //InsulationPurpose
1269
                                                if (!string.IsNullOrEmpty(FluidCode)) oid.Add(FluidCode);
1270
                                                if (!string.IsNullOrEmpty(PMC)) oid.Add(PMC);
1271
1272
                                                string PipeSystem_OID = string.Join("-", oid);
1273
1274
                                                if (!string.IsNullOrEmpty(SEQNUMBER)) oid.Add(SEQNUMBER);
1275
                                                if (!string.IsNullOrEmpty(INSULATION)) oid.Add(INSULATION);
1276
1277
                                                string OID = string.Join("-", oid);
1278 3210f690 LJIYEON
1279 a5616391 LJIYEON
                                                if (pipelineDT.Select(string.Format("OID = '{0}'", OID)).Count() == 0)
1280
                                                {
1281
                                                    DataRow newPipelineRow = pipelineDT.NewRow();
1282
                                                    newPipelineRow["OID"] = OID;
1283
                                                    newPipelineRow["PipeSystem_OID"] = PipeSystem_OID;
1284
                                                    newPipelineRow["FLUID"] = FluidCode;
1285
                                                    newPipelineRow["PMC"] = PMC;
1286
                                                    newPipelineRow["SEQNUMBER"] = SEQNUMBER;
1287
                                                    newPipelineRow["INSULATION"] = INSULATION;
1288
                                                    newPipelineRow["FROM_DATA"] = string.Empty;
1289
                                                    newPipelineRow["TO_DATA"] = string.Empty;
1290
                                                    newPipelineRow["Unit"] = PSNItem.GetPBSData();
1291
                                                    pipelineDT.Rows.Add(newPipelineRow);
1292 3210f690 LJIYEON
                                                }
1293
                                            }
1294 a5616391 LJIYEON
                                        }
1295 3210f690 LJIYEON
1296 a5616391 LJIYEON
                                        DataRow newRow = pipeSystemNetworkDT.NewRow();
1297
                                        newRow["OID"] = PSNItem.PSN_OID();
1298
                                            
1299
                                        newRow["OrderNumber"] = psnOrder;
1300
                                        newRow["Pipeline_OID"] = item.PSNPipeLineID;
1301
                                        PSNItem.KeywordInfos = KeywordInfos;
1302
                                        PSNItem.Nozzle = Nozzle;
1303
                                        
1304
                                        string FromType = string.Empty;
1305
                                        Item From_item = new Item();
1306
                                        string FROM_DATA = PSNItem.GetFromData(ref FromType, ref From_item);
1307
                                        newRow["FROM_DATA"] = FROM_DATA;
1308
1309
                                        if (PSNItem.IsKeyword)
1310
                                        {                                           
1311
                                            PSNItem.StartType = PSNType.Equipment;
1312
                                            UpdatePathItemsKeywordDataRow(From_item.UID, FROM_DATA, FromType);
1313
                                        }
1314
                                        else if (FROM_DATA == "ENDOFHEADER")
1315
                                        {
1316
                                            UpdatePathItemsDataRow(From_item.UID, From_item.ID2DBName, FROM_DATA);
1317
                                        }
1318 3210f690 LJIYEON
1319 a5616391 LJIYEON
                                        string ToType = string.Empty;
1320
                                        Item To_item = new Item();
1321 3210f690 LJIYEON
1322 a5616391 LJIYEON
                                        string TO_DATA = PSNItem.GetToData(ref ToType, ref To_item);
1323
                                        newRow["TO_DATA"] = TO_DATA;
1324 839708c6 LJIYEON
1325 a5616391 LJIYEON
                                        if (PSNItem.IsKeyword)
1326
                                        {
1327
                                            PSNItem.EndType = PSNType.Equipment;                                            
1328
                                            UpdatePathItemsKeywordDataRow(To_item.UID, TO_DATA, ToType);
1329
                                        }
1330
                                        else if (TO_DATA == "ENDOFHEADER")
1331
                                        {
1332
                                            UpdatePathItemsDataRow(To_item.UID, To_item.ID2DBName, TO_DATA);
1333
                                        }
1334 33cee849 LJIYEON
1335 a5616391 LJIYEON
                                        // VentDrain의 경우 제외 요청
1336
                                        //if (bVentDrain)
1337
                                        //    return;
1338 f9f2787b LJIYEON
1339 a5616391 LJIYEON
                                        newRow["Type"] = PSNItem.GetPSNType();
1340 eb44d82c LJIYEON
1341 a5616391 LJIYEON
                                        newRow["TopologySet_OID_Key"] = item.Topology.FullName;
1342
                                        newRow["PSNRevisionNumber"] = string.Format("V{0:D4}", Revision);
1343 5e4c2ad1 LJIYEON
1344 a5616391 LJIYEON
                                        newRow["IsValid"] = PSNItem.IsValid;
1345
                                        newRow["Status"] = !string.IsNullOrEmpty(PSNItem.Status) ? PSNItem.Status.Remove(0, 2) : string.Empty;
1346
                                        newRow["PBS"] = PSNItem.GetPBSData();
1347 879ce10b LJIYEON
1348 a5616391 LJIYEON
                                        List<string> drawingNames = new List<string>();
1349
                                        foreach (Group _group in PSNItem.Groups)
1350
                                        {
1351
                                            if (!drawingNames.Contains(_group.Document.DrawingName))
1352 3210f690 LJIYEON
                                            {
1353 a5616391 LJIYEON
                                                if (drawingNames.Count == 0)
1354
                                                    newRow["Drawings"] = _group.Document.DrawingName;
1355
                                                else
1356
                                                    newRow["Drawings"] = newRow["Drawings"] + ", " + _group.Document.DrawingName;
1357
                                                drawingNames.Add(_group.Document.DrawingName);
1358 3210f690 LJIYEON
                                            }
1359 27d06aa8 LJIYEON
                                        }
1360 a5616391 LJIYEON
                                        newRow["IncludingVirtualData"] = "No";
1361
                                        newRow["PSNAccuracy"] = "100";
1362
                                        pipeSystemNetworkDT.Rows.Add(newRow);
1363 2c46461b LJIYEON
                                    }
1364 27d06aa8 LJIYEON
                                }
1365 94a117ca gaqhf
                            }
1366 a5616391 LJIYEON
                        }
1367
                        catch (Exception ex)
1368
                        {
1369
                            Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1370
                            MessageBox.Show(ex.Message, "ID2 " + id2Info.ProgramName, MessageBoxButtons.OK, MessageBoxIcon.Information);
1371
                        }
1372 3210f690 LJIYEON
1373 a5616391 LJIYEON
                        //TopologySet 관련
1374
                        foreach (Topology topology in PSNItem.Topologies)
1375
                        {
1376
                            DataRow newRow = topologySetDT.NewRow();
1377
                            newRow["OID"] = topology.FullName;
1378
                            newRow["Type"] = topology.FullName.Split(new char[] { '-' }).Last().StartsWith("M") ? "Main" : "Branch";
1379
                            if (bVentDrain)
1380
                                newRow["SubType"] = "Vent_Drain";
1381
                            else
1382
                                newRow["SubType"] = null;
1383
                            newRow["HeadItemTag"] = GetItemTag(topology.Items.Last());
1384
                            newRow["TailItemTag"] = GetItemTag(topology.Items.First());
1385
                            newRow["HeadItemSPID"] = topology.Items.Last().UID;
1386
                            newRow["TailItemSPID"] = topology.Items.First().UID;
1387
                            topologySetDT.Rows.Add(newRow);
1388 94a117ca gaqhf
                        }
1389 a5616391 LJIYEON
                        
1390 6b9e7a56 gaqhf
                    }
1391 3210f690 LJIYEON
                    catch(Exception ee)
1392 27d06aa8 LJIYEON
                    {
1393 879ce10b LJIYEON
1394 2c46461b LJIYEON
                    }
1395 6b9e7a56 gaqhf
                }
1396
1397 3210f690 LJIYEON
1398 2c46461b LJIYEON
                foreach (var item in startBranchDic)
1399 5c248ee3 gaqhf
                {
1400 2c46461b LJIYEON
                    string uid = item.Key.UID;
1401
                    string topologyName = item.Value.Topology.FullName;
1402
                    DataRow[] rows = pathItemsDT.Select(string.Format("OID LIKE '{0}%'", uid));
1403 3210f690 LJIYEON
                   
1404 2c46461b LJIYEON
                    if (rows.Length == 1)
1405 9c151350 gaqhf
                    {
1406 2c46461b LJIYEON
                        rows.First()["BranchTopologySet_OID"] = topologyName;
1407 9c151350 gaqhf
                        rows.First()["ViewPipeSystemNetwork_OID"] = item.Value.PSNItem.PSN_OID();
1408
                    }
1409 2c46461b LJIYEON
                    else if (rows.Length > 1)
1410 5c248ee3 gaqhf
                    {
1411 2c46461b LJIYEON
                        DataRow targetRow = null;
1412
                        int index = int.MaxValue;
1413
                        foreach (DataRow row in rows)
1414 5c248ee3 gaqhf
                        {
1415 2c46461b LJIYEON
                            string split = row["OID"].ToString().Split(new char[] { '_' })[1];
1416
                            if (split.StartsWith("L"))
1417 5c248ee3 gaqhf
                            {
1418 2c46461b LJIYEON
                                int num = Convert.ToInt32(split.Remove(0, 1));
1419
                                if (index > num)
1420
                                {
1421
                                    index = num;
1422
                                    targetRow = row;
1423
                                }
1424 5c248ee3 gaqhf
                            }
1425
                        }
1426
1427 2c46461b LJIYEON
                        if (targetRow != null)
1428 9c151350 gaqhf
                        {
1429 2c46461b LJIYEON
                            targetRow["BranchTopologySet_OID"] = topologyName;
1430 9c151350 gaqhf
                            targetRow["ViewPipeSystemNetwork_OID"] = item.Value.PSNItem.PSN_OID();
1431
                        }
1432 2c46461b LJIYEON
                    }
1433 5c248ee3 gaqhf
                }
1434 f9f2787b LJIYEON
1435 2c46461b LJIYEON
                foreach (var item in endBranchDic)
1436 5c248ee3 gaqhf
                {
1437 2c46461b LJIYEON
                    string uid = item.Key.UID;
1438
                    string topologyName = item.Value.Topology.FullName;
1439
                    DataRow[] rows = pathItemsDT.Select(string.Format("OID LIKE '{0}%'", uid));
1440
                    if (rows.Length == 1)
1441 9c151350 gaqhf
                    {
1442 2c46461b LJIYEON
                        rows.First()["BranchTopologySet_OID"] = topologyName;
1443 9c151350 gaqhf
                        rows.First()["ViewPipeSystemNetwork_OID"] = item.Value.PSNItem.PSN_OID();
1444
                    }
1445 2c46461b LJIYEON
                    else if (rows.Length > 1)
1446 5c248ee3 gaqhf
                    {
1447 2c46461b LJIYEON
                        DataRow targetRow = null;
1448
                        int index = int.MinValue;
1449
                        foreach (DataRow row in rows)
1450 5c248ee3 gaqhf
                        {
1451 2c46461b LJIYEON
                            string split = row["OID"].ToString().Split(new char[] { '_' })[1];
1452
                            if (split.StartsWith("L"))
1453 5c248ee3 gaqhf
                            {
1454 2c46461b LJIYEON
                                int num = Convert.ToInt32(split.Remove(0, 1));
1455
                                if (index < num)
1456
                                {
1457
                                    index = num;
1458
                                    targetRow = row;
1459
                                }
1460 5c248ee3 gaqhf
                            }
1461
                        }
1462
1463 2c46461b LJIYEON
                        if (targetRow != null)
1464 9c151350 gaqhf
                        {
1465 2c46461b LJIYEON
                            targetRow["BranchTopologySet_OID"] = topologyName;
1466 9c151350 gaqhf
                            targetRow["ViewPipeSystemNetwork_OID"] = item.Value.PSNItem.PSN_OID();
1467
                        }
1468 2c46461b LJIYEON
                    }
1469 5c248ee3 gaqhf
                }
1470
1471 2c46461b LJIYEON
                PathItems = pathItemsDT;
1472
                SequenceData = sequenceDataDT;
1473
                PipeSystemNetwork = pipeSystemNetworkDT;
1474
                TopologySet = topologySetDT;
1475 f9f2787b LJIYEON
                PipeLine = pipelineDT;
1476 2c46461b LJIYEON
            }
1477
            catch (Exception ex)
1478
            {
1479
                Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1480
                MessageBox.Show(ex.Message, "ID2 " + id2Info.ProgramName, MessageBoxButtons.OK, MessageBoxIcon.Information);
1481
            }
1482 36a45f13 gaqhf
        }
1483 2ada3be8 LJIYEON
1484 5e4c2ad1 LJIYEON
        private double AccuracyCalculation(List<double> lstAcc, double acc)
1485 2ada3be8 LJIYEON
        {
1486
            foreach(double lacc in lstAcc)
1487
            {
1488
                acc *= lacc;
1489
            }
1490 bfe278bb LJIYEON
            acc = acc;
1491 2ada3be8 LJIYEON
            return acc;
1492
        }
1493
1494 7881ec8f gaqhf
        private void UpdateSubType()
1495 36a45f13 gaqhf
        {
1496 27d06aa8 LJIYEON
            try
1497 36a45f13 gaqhf
            {
1498 27d06aa8 LJIYEON
1499
1500
                foreach (PSNItem PSNItem in PSNItems)
1501 36a45f13 gaqhf
                {
1502 27d06aa8 LJIYEON
                    if (PSNItem.IsBypass)
1503 36a45f13 gaqhf
                    {
1504 27d06aa8 LJIYEON
                        foreach (Topology topology in PSNItem.Topologies)
1505
                        {
1506
                            DataRow[] rows = TopologySet.Select(string.Format("OID = '{0}'", topology.FullName));
1507
                            if (rows.Length.Equals(1))
1508
                                rows.First()["SubType"] = "Bypass";
1509
                        }
1510
                    }
1511
1512
                    if (PSNItem.StartType == PSNType.Header)
1513
                    {
1514
                        Topology topology = PSNItem.Topologies.First();
1515
                        DataRow[] rows = TopologySet.Select(string.Format("OID = '{0}'", topology.FullName));
1516
                        if (rows.Length.Equals(1))
1517
                            rows.First()["SubType"] = "Header";
1518
                    }
1519
                    else if (PSNItem.EndType == PSNType.Header)
1520
                    {
1521
                        Topology topology = PSNItem.Topologies.Last();
1522 7881ec8f gaqhf
                        DataRow[] rows = TopologySet.Select(string.Format("OID = '{0}'", topology.FullName));
1523
                        if (rows.Length.Equals(1))
1524 27d06aa8 LJIYEON
                            rows.First()["SubType"] = "Header";
1525 36a45f13 gaqhf
                    }
1526
                }
1527 7881ec8f gaqhf
1528 36a45f13 gaqhf
1529 27d06aa8 LJIYEON
                foreach (Topology topology in Topologies)
1530 7881ec8f gaqhf
                {
1531 27d06aa8 LJIYEON
                    try
1532
                    {
1533
                        DataRow[] rows = TopologySet.Select(string.Format("OID = '{0}'", topology.FullName));
1534 3210f690 LJIYEON
                        
1535
                        if(rows.Count() > 0)
1536 27d06aa8 LJIYEON
                        {
1537 3210f690 LJIYEON
                            if (rows.Length.Equals(1) && rows.First()["SubType"] == null || string.IsNullOrEmpty(rows.First()["SubType"].ToString()))
1538
                            {
1539
                                if (topology.Items == null)
1540
                                    continue;
1541 27d06aa8 LJIYEON
1542 3210f690 LJIYEON
                                Item firstItem = topology.Items.First();
1543
                                Item lastItem = topology.Items.Last();
1544 27d06aa8 LJIYEON
1545 3210f690 LJIYEON
                                List<Relation> relations = new List<Relation>();
1546 7881ec8f gaqhf
1547 3210f690 LJIYEON
                                if (firstItem.Relations.FindAll(x => x.Item == null).Count() != 0)
1548
                                    relations.AddRange(firstItem.Relations.FindAll(x => x.Item != null && x.Item.Topology.ID != topology.ID));
1549 7881ec8f gaqhf
1550 3210f690 LJIYEON
                                if (lastItem.Relations.FindAll(x => x.Item == null).Count() != 0)
1551
                                    relations.AddRange(lastItem.Relations.FindAll(x => x.Item != null && x.Item.Topology.ID != topology.ID));
1552
1553
                                if (relations.Count > 0)
1554
                                    rows.First()["SubType"] = "OtherSystem";
1555
                            }
1556 27d06aa8 LJIYEON
                        }
1557
                    }
1558
                    catch (Exception ex)
1559
                    {
1560
                       
1561
                        MessageBox.Show(ex.Message, "ID2 " + id2Info.ProgramName, MessageBoxButtons.OK, MessageBoxIcon.Information);
1562
                    }
1563 7881ec8f gaqhf
                }
1564
1565 27d06aa8 LJIYEON
                    foreach (DataRow row in TopologySet.Rows)
1566
                    if (row["SubType"] == null || string.IsNullOrEmpty(row["SubType"].ToString()))
1567
                        row["SubType"] = "Normal";
1568
            }
1569
            catch (Exception ex)
1570
            {
1571
                Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1572
                MessageBox.Show(ex.Message, "ID2 " + id2Info.ProgramName, MessageBoxButtons.OK, MessageBoxIcon.Information);
1573
            }
1574 7881ec8f gaqhf
        }
1575 5e4c2ad1 LJIYEON
1576 7881ec8f gaqhf
        private bool IsBypass(PSNItem PSNItem)
1577
        {
1578
            bool bResult = false;
1579
1580
            if (PSNItem.GetPSNType() == "B2B")
1581
            {
1582
                Group firstGroup = PSNItem.Groups.First();
1583
                Group lastGroup = PSNItem.Groups.Last();
1584
                Item firstItem = firstGroup.Items.First();
1585
                Item lastItem = lastGroup.Items.Last();
1586
1587
                Item connectedFirstItem = GetConnectedItemByPSN(firstItem);
1588
                Item connectedLastItem = GetConnectedItemByPSN(lastItem);
1589 36a45f13 gaqhf
1590 7881ec8f gaqhf
                if (connectedFirstItem.LineNumber != null && connectedLastItem.LineNumber != null &&
1591
                    !string.IsNullOrEmpty(connectedFirstItem.LineNumber.Name) && !string.IsNullOrEmpty(connectedLastItem.LineNumber.Name) &&
1592
                    connectedFirstItem.LineNumber.Name == connectedLastItem.LineNumber.Name)
1593
                    bResult = true;
1594
                else if (connectedFirstItem.PSNItem == connectedLastItem.PSNItem)
1595
                    bResult = true;
1596
            }
1597 36a45f13 gaqhf
1598
            Item GetConnectedItemByPSN(Item item)
1599
            {
1600
                Item result = null;
1601
1602
                Relation relation = item.Relations.Find(x => x.Item != null && x.Item.PSNItem != item.PSNItem);
1603
                if (relation != null)
1604
                    result = relation.Item;
1605
1606 3210f690 LJIYEON
1607 36a45f13 gaqhf
                return result;
1608
            }
1609 7881ec8f gaqhf
1610
            return bResult;
1611
        }
1612 5e4c2ad1 LJIYEON
1613 a36541fb LJIYEON
     
1614 5e4c2ad1 LJIYEON
        private void UpdateAccuracy()
1615
        {
1616 72775f2e LJIYEON
            DataRow[] statusRows = PipeSystemNetwork.Select(" Type = 'Error' OR IsValid = 'Error'"); 
1617 5e4c2ad1 LJIYEON
            List<double> lstAcc = null;
1618
            string Status = string.Empty;
1619 f9f2787b LJIYEON
1620 5e4c2ad1 LJIYEON
            foreach (DataRow dataRow in statusRows)
1621
            {
1622
                lstAcc = new List<double>();
1623
                Status = dataRow.Field<string>("Status");
1624
1625
                if (!string.IsNullOrEmpty(Status))
1626
                {
1627
                    if (Status.Contains(Rule1)) //Line Disconnected
1628
                        lstAcc.Add(0.75);
1629 f9f2787b LJIYEON
1630
                    if (Status.Contains(Rule2)) //Missing LineNumber
1631 5e4c2ad1 LJIYEON
                        lstAcc.Add(0.7);            
1632 f9f2787b LJIYEON
1633
                    if (Status.Contains(Rule3)) //OPC Disconneted
1634
                        lstAcc.Add(0.5);            
1635
1636
                    if (Status.Contains(Rule4)) //Missing ItemTag or Description
1637 5e4c2ad1 LJIYEON
                        lstAcc.Add(0.65);           
1638
                    //else if (Status.Contains(Rule5))
1639
                    //    lstAcc.Add(0.6);
1640
                }
1641
1642
                string PSNAccuracy = dataRow["PSNAccuracy"].ToString();
1643
                if (PSNAccuracy == "100")
1644
                    PSNAccuracy = "1";
1645
1646
                PSNAccuracy = Convert.ToString(Math.Round(Convert.ToDouble(AccuracyCalculation(lstAcc, Convert.ToDouble(PSNAccuracy))), 2));
1647
                if (PSNAccuracy != "100")
1648
                {
1649 f9f2787b LJIYEON
                    dataRow["IncludingVirtualData"] = "No";
1650 5e4c2ad1 LJIYEON
                    dataRow["PSNAccuracy"] = PSNAccuracy;
1651
                }
1652
            }
1653 bfe278bb LJIYEON
            DataTable dt = PipeSystemNetwork.DefaultView.ToTable(true, new string[] { "OID" });
1654
            foreach (DataRow dr in dt.Rows)
1655
            {
1656
                string oid = dr.Field<string>("OID");
1657
                DataRow[] select = PipeSystemNetwork.Select(string.Format("OID = '{0}'", oid));
1658
                double totalDdr = 0;
1659
                foreach (DataRow ddr in select)
1660
                {
1661
                    double acc = Convert.ToDouble(ddr.Field<string>("PSNAccuracy"));
1662
                    if (acc == 100) acc = 1;
1663
                    if (totalDdr == 0) totalDdr = acc;
1664
                    else totalDdr *= acc;
1665
                }
1666
1667
                totalDdr *= 100;
1668
1669 f9f2787b LJIYEON
                if(totalDdr != 100)
1670 bfe278bb LJIYEON
                {
1671 f9f2787b LJIYEON
                    foreach (DataRow ddr in select)
1672
                    {
1673
                        ddr["IncludingVirtualData"] = "Yes";
1674 3210f690 LJIYEON
                        ddr["PSNAccuracy"] = Math.Round(totalDdr, 2);
1675 f9f2787b LJIYEON
                    }
1676 bfe278bb LJIYEON
                }
1677
            }
1678
1679 5e4c2ad1 LJIYEON
        }
1680
1681 a5616391 LJIYEON
        private void UpdateKeywordForPSN()
1682
        {
1683
            #region Keyword Info
1684
            KeywordInfo KeywordInfos = new KeywordInfo();
1685
            DataTable dtKeyword = DB.SelectKeywordsSetting();
1686
            foreach (DataRow row in dtKeyword.Rows)
1687
            {
1688
                int index = Convert.ToInt32(row["INDEX"]);
1689
                string name = row["NAME"].ToString();
1690
                string keyword = row["KEYWORD"].ToString();
1691
1692
                //KeywordInfo keywordInfo = new KeywordInfo();   
1693
                KeywordInfos.KeywordItems.Add(new KeywordItem()
1694
                {
1695
                    Index = index,
1696
                    Name = name,
1697
                    Keyword = keyword
1698
                });
1699
            }
1700
            #endregion
1701
1702
            foreach (KeywordItem keyitem in KeywordInfos.KeywordItems)
1703
            {
1704
                DataRow[] keywordRows = PipeSystemNetwork.Select(string.Format(" From_Data = '{0}' OR To_Data = '{0}'", keyitem.Name));
1705
                foreach (DataRow dataRow in keywordRows)
1706
                {                    
1707
                    PSNItem PSNItem = PSNItems.Find(x => x.PSN_OID() == dataRow["OID"].ToString());
1708
                    bool change = false;            
1709
1710
                    if(dataRow.Field<string>("From_Data") == keyitem.Name)
1711
                    {                        
1712
                        change = true;
1713
                        DataRow[] pathItemRows = PathItems.Select(string.Format("PipeSystemNetwork_OID = '{0}'", dataRow["OID"]));
1714
                                               
1715
                        if(pathItemRows.First().Field<string>("Type").Equals(keyitem.Name))
1716
                        {
1717
                            DataRow dr = pathItemRows.First();
1718
                            //dr["CLASS"] = ""; //Type
1719
                            dr["TYPE"] = "End";
1720
                            dr["ITEMTAG"] = keyitem.Keyword;
1721
                            dr["DESCRIPTION"] = keyitem.Keyword;
1722
                        }
1723
                           
1724
                    }
1725
1726
                    if (dataRow.Field<string>("To_Data") == keyitem.Name)
1727
                    {
1728
                        change = true;
1729
                        DataRow[] pathItemRows = PathItems.Select(string.Format("PipeSystemNetwork_OID = '{0}'", dataRow["OID"]));
1730
1731
                        if (pathItemRows.Last().Field<string>("Type").Equals(keyitem.Name))
1732
                        {
1733
                            DataRow dr = pathItemRows.Last();
1734
                            //dr["CLASS"] = ""; //Type
1735
                            dr["TYPE"] = "End";
1736
                            dr["ITEMTAG"] = keyitem.Keyword;
1737
                            dr["DESCRIPTION"] = keyitem.Keyword;
1738
                            //dr.Field<string>("Type")
1739
                        }
1740
1741
                    }                   
1742
                }
1743
            }          
1744
        }
1745
1746 7881ec8f gaqhf
        private void UpdateErrorForPSN()
1747
        {
1748 45529c16 LJIYEON
            DataRow[] errorRows = PipeSystemNetwork.Select(string.Format(" Type = '{0}'", ErrorType.Error));
1749 5e4c2ad1 LJIYEON
            bool bCheckRule5 = false;
1750 7881ec8f gaqhf
            foreach (DataRow dataRow in errorRows)
1751
            {
1752 eb44d82c LJIYEON
                try
1753 710a49f1 gaqhf
                {
1754 a36541fb LJIYEON
                   
1755 eb44d82c LJIYEON
                    PSNItem PSNItem = PSNItems.Find(x => x.PSN_OID() == dataRow["OID"].ToString());
1756
                    bool change = false;
1757
                    bCheckRule5 = false;
1758 710a49f1 gaqhf
1759 eb44d82c LJIYEON
                    if (!PSNItem.EnableType(PSNItem.StartType))
1760 710a49f1 gaqhf
                    {
1761 eb44d82c LJIYEON
                        change = true;
1762 a36541fb LJIYEON
                        DataRow[] pathItemRows = PathItems.Select(string.Format("PipeSystemNetwork_OID = '{0}'", dataRow["OID"]));                       
1763 eb44d82c LJIYEON
                        int insertIndex = PathItems.Rows.IndexOf(pathItemRows.First());
1764 5e4c2ad1 LJIYEON
1765 a36541fb LJIYEON
                        DataRow[] seqItemRows = SequenceData.Select(string.Format("PathItem_OID = '{0}'", pathItemRows.First()["OID"]));
1766
                        int insertSeqIndex = SequenceData.Rows.IndexOf(seqItemRows.First());
1767
1768 eb44d82c LJIYEON
                        Item item = PSNItem.Groups.First().Items.First();
1769
                        try
1770 a36541fb LJIYEON
                        {
1771
                            string FROM_DATA = string.Format("TIEINPOINT_{0:D5}V", tieInPointIndex);
1772 eb44d82c LJIYEON
                            foreach (DataRow loopRow in PipeSystemNetwork.Select(string.Format("OID = '{0}'", PSNItem.PSN_OID())))
1773 a36541fb LJIYEON
                                loopRow["FROM_DATA"] = FROM_DATA;
1774 eb44d82c LJIYEON
                            tieInPointIndex++;
1775 5e4c2ad1 LJIYEON
1776 7881ec8f gaqhf
1777 eb44d82c LJIYEON
                            if (item.ItemType == ItemType.Line)
1778
                            {
1779
                                //createTerminatorRow - Rule 5번
1780 a36541fb LJIYEON
                                PathItems.Rows.InsertAt(createTerminatorRow(pathItemRows.First(), FROM_DATA), insertIndex);
1781 3210f690 LJIYEON
                                //SequenceData.Rows.InsertAt(createSequenceTerminatorRow(pathItemRows.First(), insertSeqIndex), insertSeqIndex);
1782 710a49f1 gaqhf
1783 eb44d82c LJIYEON
                                bCheckRule5 = true;
1784
                            }
1785
                            else
1786
                            {
1787
                                PathItems.Rows.InsertAt(createLineRow(PathItems.Select(string.Format("PipeLine_OID = '{0}' AND ItemName = 'PipeRun' AND PipeSystemNetwork_OID = '{1}'", item.PSNPipeLineID, dataRow["OID"])).First()), insertIndex);
1788 3210f690 LJIYEON
                                //SequenceData.Rows.InsertAt(createSequenceTerminatorRow(PathItems.Select(string.Format("PipeLine_OID = '{0}' AND ItemName = 'PipeRun' AND PipeSystemNetwork_OID = '{1}'", item.PSNPipeLineID, dataRow["OID"])).First(), insertSeqIndex), insertSeqIndex);
1789 eb44d82c LJIYEON
                                //createTerminatorRow - Rule 5번
1790 a36541fb LJIYEON
                                PathItems.Rows.InsertAt(createTerminatorRow(pathItemRows.First(), FROM_DATA), insertIndex);
1791 3210f690 LJIYEON
                                //SequenceData.Rows.InsertAt(createSequenceTerminatorRow(pathItemRows.First(), insertSeqIndex), insertSeqIndex);
1792 710a49f1 gaqhf
1793 eb44d82c LJIYEON
                                bCheckRule5 = true;
1794
                            }
1795 710a49f1 gaqhf
1796 eb44d82c LJIYEON
                            PSNItem.StartType = PSNType.Equipment;
1797
                        }
1798
                        catch (Exception ex)
1799
                        {
1800
                            MessageBox.Show("Please check the item.\r\nDrawingName : " + item.Document.DrawingName + "\r\nUID : " + item.UID, "ID2 " + id2Info.ProgramName, MessageBoxButtons.OK, MessageBoxIcon.Warning);
1801
                            return;
1802
                        }
1803
                    }
1804 710a49f1 gaqhf
1805
1806 eb44d82c LJIYEON
                    if (!PSNItem.EnableType(PSNItem.EndType))
1807 710a49f1 gaqhf
                    {
1808 eb44d82c LJIYEON
                        change = true;
1809
                        DataRow[] pathItemRows = PathItems.Select(string.Format("PipeSystemNetwork_OID = '{0}'", dataRow["OID"]));
1810
                        int insertIndex = PathItems.Rows.IndexOf(pathItemRows.Last());
1811 a36541fb LJIYEON
1812 3210f690 LJIYEON
                       
1813 a36541fb LJIYEON
1814 eb44d82c LJIYEON
                        Item item = PSNItem.Groups.Last().Items.Last();
1815
                        try
1816 a36541fb LJIYEON
                        {
1817
                            string TO_DATA = string.Format("TIEINPOINT_{0:D5}V", tieInPointIndex);
1818
1819 eb44d82c LJIYEON
                            foreach (DataRow loopRow in PipeSystemNetwork.Select(string.Format("OID = '{0}'", PSNItem.PSN_OID())))
1820 a36541fb LJIYEON
                                loopRow["TO_DATA"] = TO_DATA;
1821 eb44d82c LJIYEON
                            tieInPointIndex++;
1822
1823
1824
                            if (item.ItemType == ItemType.Line)
1825
                            {
1826
                                //createTerminatorRow - Rule 5번
1827 a36541fb LJIYEON
                                PathItems.Rows.InsertAt(createTerminatorRow(pathItemRows.Last(), TO_DATA), insertIndex + 1);
1828 3210f690 LJIYEON
                                //SequenceData.Rows.InsertAt(createSequenceTerminatorRow(pathItemRows.Last(), insertSeqIndex + 1), insertSeqIndex + 1);
1829 eb44d82c LJIYEON
1830
                                bCheckRule5 = true;
1831
                            }
1832
                            else
1833
                            {
1834
                                //createTerminatorRow - Rule 5번
1835 a36541fb LJIYEON
                                PathItems.Rows.InsertAt(createTerminatorRow(pathItemRows.Last(), TO_DATA), insertIndex + 1);
1836 3210f690 LJIYEON
                                //SequenceData.Rows.InsertAt(createSequenceTerminatorRow(pathItemRows.Last(), insertSeqIndex + 1), insertSeqIndex + 1);
1837 eb44d82c LJIYEON
                                PathItems.Rows.InsertAt(createLineRow(PathItems.Select(string.Format("PipeLine_OID = '{0}' AND ItemName = 'PipeRun' AND PipeSystemNetwork_OID = '{1}'", item.PSNPipeLineID, dataRow["OID"])).Last()), insertIndex + 1);
1838 3210f690 LJIYEON
                                //SequenceData.Rows.InsertAt(createSequenceTerminatorRow(PathItems.Select(string.Format("PipeLine_OID = '{0}' AND ItemName = 'PipeRun' AND PipeSystemNetwork_OID = '{1}'", item.PSNPipeLineID, dataRow["OID"])).Last(), insertSeqIndex + 1), insertSeqIndex + 1);
1839 eb44d82c LJIYEON
                                bCheckRule5 = true;
1840
                            }
1841 5e4c2ad1 LJIYEON
1842 eb44d82c LJIYEON
                            PSNItem.EndType = PSNType.Equipment;
1843
                        }
1844
                        catch(Exception ex)
1845
                        {
1846
                            MessageBox.Show("Please check the item.\r\nDrawingName : " + item.Document.DrawingName + "\r\nUID : " + item.UID, "ID2 " + id2Info.ProgramName, MessageBoxButtons.OK, MessageBoxIcon.Warning);
1847
                            return;
1848
                        }
1849 710a49f1 gaqhf
                    }
1850 eb44d82c LJIYEON
1851
                    dataRow["Type"] = PSNItem.GetPSNType();
1852
                    if (change)
1853 710a49f1 gaqhf
                    {
1854 eb44d82c LJIYEON
                        int rowIndex = 0;
1855
                        for (int i = 0; i < PathItems.Rows.Count; i++)
1856
                        {
1857
                            DataRow row = PathItems.Rows[i];
1858
                            if (row["PipeSystemNetwork_OID"].ToString() != dataRow["OID"].ToString())
1859
                                continue;
1860
                            string sequenceData = row["SequenceData_OID"].ToString();
1861
                            string[] split = sequenceData.Split(new char[] { '_' });
1862
1863
                            StringBuilder sb = new StringBuilder();
1864
                            for (int j = 0; j < split.Length - 1; j++)
1865
                                sb.Append(split[j] + "_");
1866
                            sb.Append(rowIndex++);
1867
                            row["SequenceData_OID"] = sb.ToString();
1868 a36541fb LJIYEON
1869 3210f690 LJIYEON
                            DataRow seqItemRows = SequenceData.Select(string.Format("PathItem_OID = '{0}'", row["OID"])).FirstOrDefault();
1870
                            int insertSeqIndex = SequenceData.Rows.IndexOf(seqItemRows);
1871
1872
                            string[] splitseq = sb.ToString().Split(new char[] { '_' });
1873
1874
                            if (seqItemRows == null)
1875
                            {
1876
                                DataRow newRow = SequenceData.NewRow();    
1877
                                newRow["OID"] = sb.ToString();
1878
                                newRow["SERIALNUMBER"] = splitseq[splitseq.Length - 1];
1879
                                newRow["PathItem_OID"] = row["OID"];
1880
                                newRow["TopologySet_OID_Key"] = row["TopologySet_OID"];
1881
                                SequenceData.Rows.InsertAt(newRow, Convert.ToInt32(splitseq[splitseq.Length - 1]));
1882
                            }
1883
                            else
1884
                            {
1885
                                seqItemRows["OID"] = sb.ToString();
1886
                                seqItemRows["SERIALNUMBER"] = splitseq[splitseq.Length - 1];
1887
                                seqItemRows["PathItem_OID"] = row["OID"];
1888
                                seqItemRows["TopologySet_OID_Key"] = row["TopologySet_OID"];
1889
1890
                            }
1891
                           
1892 a36541fb LJIYEON
1893
                        }
1894 3210f690 LJIYEON
1895
                        //rowIndex = 0;
1896
                        //for (int i = 0; i < SequenceData.Rows.Count; i++)
1897
                        //{
1898
                        //    DataRow row = SequenceData.Rows[i];
1899
                        //    if (row["PathItem_OID"].ToString() != dataRow["OID"].ToString())
1900
                        //        continue;
1901
                        //    string sequenceData = row["SequenceData_OID"].ToString();
1902
                        //    string[] split = sequenceData.Split(new char[] { '_' });
1903
1904
                        //    row["SERIALNUMBER"] = rowIndex++;
1905
                        //}
1906 eb44d82c LJIYEON
                    }
1907 5e4c2ad1 LJIYEON
1908 eb44d82c LJIYEON
                    if (bCheckRule5)
1909
                    {                       
1910
                        string PSNAccuracy = "0.6";
1911
                        dataRow["IncludingVirtualData"] = "Yes";
1912
                        dataRow["PSNAccuracy"] = PSNAccuracy;
1913
                       
1914 710a49f1 gaqhf
                    }
1915 7881ec8f gaqhf
1916 a36541fb LJIYEON
                    DataRow createTerminatorRow(DataRow itemRow, string DATA)
1917 eb44d82c LJIYEON
                    {
1918
                        DataRow newRow = PathItems.NewRow();
1919
                        newRow["OID"] = Guid.NewGuid().ToString();
1920
                        newRow["SequenceData_OID"] = itemRow["SequenceData_OID"];
1921
                        newRow["TopologySet_OID"] = itemRow["TopologySet_OID"];
1922
                        newRow["BranchTopologySet_OID"] = itemRow["BranchTopologySet_OID"];
1923 a36541fb LJIYEON
                        newRow["PipeLine_OID"] = itemRow["PipeLine_OID"];                        
1924
                        newRow["ITEMNAME"] = "PipingComp"; //newRow["ITEMNAME"] = "End of line terminator";
1925
                        newRow["ITEMTAG"] = DATA; //itemRow["ITEMTAG"];
1926
                        newRow["DESCRIPTION"] = DATA; 
1927 eb44d82c LJIYEON
                        newRow["Class"] = "End of line terminator";
1928
                        newRow["SubClass"] = "End of line terminator";
1929 a36541fb LJIYEON
                        newRow["TYPE"] = "End";
1930 eb44d82c LJIYEON
                        newRow["PIDNAME"] = itemRow["PIDNAME"];
1931
                        newRow["NPD"] = itemRow["NPD"];
1932
                        newRow["PipeSystemNetwork_OID"] = itemRow["PipeSystemNetwork_OID"];
1933
                        newRow["ViewPipeSystemNetwork_OID"] = itemRow["ViewPipeSystemNetwork_OID"];
1934
                        newRow["PipeRun_OID"] = itemRow["PipeRun_OID"];
1935
1936 a36541fb LJIYEON
                        
1937
1938
                        return newRow;
1939
                    }
1940
1941 3210f690 LJIYEON
                    //DataRow createSequenceTerminatorRow(DataRow itemRow, int insertIndex)
1942
                    //{
1943
                    //    DataRow newRow = SequenceData.NewRow();
1944
                    //    newRow["OID"] = itemRow["SequenceData_OID"]; 
1945
                    //    newRow["SERIALNUMBER"] = string.Format("{0}", insertIndex);
1946
                    //    newRow["PathItem_OID"] = itemRow["OID"];
1947
                    //    newRow["TopologySet_OID_Key"] = itemRow["TopologySet_OID"];
1948 a36541fb LJIYEON
1949 3210f690 LJIYEON
                    //    return newRow;
1950
                    //}
1951 7881ec8f gaqhf
1952 eb44d82c LJIYEON
                    DataRow createLineRow(DataRow itemRow)
1953 710a49f1 gaqhf
                    {
1954 eb44d82c LJIYEON
                        DataRow newRow = PathItems.NewRow();
1955
                        newRow["OID"] = Guid.NewGuid().ToString();
1956
                        newRow["SequenceData_OID"] = itemRow["SequenceData_OID"];
1957
                        newRow["TopologySet_OID"] = itemRow["TopologySet_OID"];
1958
                        newRow["BranchTopologySet_OID"] = itemRow["BranchTopologySet_OID"];
1959
                        newRow["PipeLine_OID"] = itemRow["PipeLine_OID"];
1960
                        newRow["ITEMNAME"] = itemRow["ITEMNAME"];
1961
                        newRow["ITEMTAG"] = itemRow["ITEMTAG"];
1962
                        newRow["Class"] = itemRow["Class"];
1963
                        newRow["SubClass"] = itemRow["SubClass"];
1964
                        newRow["TYPE"] = itemRow["TYPE"];
1965
                        newRow["PIDNAME"] = itemRow["PIDNAME"];
1966
                        newRow["NPD"] = itemRow["NPD"];
1967
                        newRow["PipeSystemNetwork_OID"] = itemRow["PipeSystemNetwork_OID"];
1968
                        newRow["ViewPipeSystemNetwork_OID"] = itemRow["ViewPipeSystemNetwork_OID"];
1969
                        newRow["PipeRun_OID"] = itemRow["PipeRun_OID"];
1970
1971
                        return newRow;
1972 710a49f1 gaqhf
                    }
1973
                }
1974 eb44d82c LJIYEON
                catch(Exception ex)
1975 5e4c2ad1 LJIYEON
                {
1976 710a49f1 gaqhf
1977
                }
1978 eb44d82c LJIYEON
            }
1979 6b9e7a56 gaqhf
        }
1980
    }
1981
1982
    public class PSNItem
1983
    {
1984 8f24b438 gaqhf
        public PSNItem(int count, int Revision)
1985 6b9e7a56 gaqhf
        {
1986
            Groups = new List<Group>();
1987
            Topologies = new List<Topology>();
1988 94a117ca gaqhf
1989
            Index = count + 1;
1990 8f24b438 gaqhf
            this.Revision = Revision;
1991 6b9e7a56 gaqhf
        }
1992 eb44d82c LJIYEON
1993 8f24b438 gaqhf
        private int Revision;
1994 6b9e7a56 gaqhf
        public string UID { get; set; }
1995
        public List<Group> Groups { get; set; }
1996
        public List<Topology> Topologies { get; set; }
1997
        public PSNType StartType { get; set; }
1998
        public PSNType EndType { get; set; }
1999 94a117ca gaqhf
        public int Index { get; set; }
2000 72775f2e LJIYEON
        public string IsValid { get; set; }
2001 a36541fb LJIYEON
        public bool IsKeyword { get; set; }
2002 36a45f13 gaqhf
        public string Status { get; set; }
2003 ddc1c369 LJIYEON
        public string IncludingVirtualData { get; set; }
2004
        public string PSNAccuracy { get; set; }
2005 eb44d82c LJIYEON
        public KeywordInfo KeywordInfos = new KeywordInfo();
2006 a36541fb LJIYEON
        public DataTable Nozzle = new DataTable();
2007 ddc1c369 LJIYEON
2008 94a117ca gaqhf
        public string PSN_OID()
2009
        {
2010 36a45f13 gaqhf
            return string.Format("V{0}-PSN-{1}", string.Format("{0:D4}", Revision), string.Format("{0:D5}", Index));
2011 94a117ca gaqhf
        }
2012
        public string GetPSNType()
2013
        {
2014
            string result = string.Empty;
2015
2016
            if (EnableType(StartType) && EnableType(EndType))
2017
            {
2018
                if (StartType == PSNType.Equipment && EndType == PSNType.Equipment)
2019
                    result = "E2E";
2020
                else if (StartType == PSNType.Branch && EndType == PSNType.Branch)
2021
                    result = "B2B";
2022
                else if (StartType == PSNType.Header && EndType == PSNType.Header)
2023
                    result = "HD2";
2024
2025
                else if (StartType == PSNType.Equipment && EndType == PSNType.Branch)
2026
                    result = "E2B";
2027
                else if (StartType == PSNType.Branch && EndType == PSNType.Equipment)
2028
                    result = "B2E";
2029
2030
                else if (StartType == PSNType.Header && EndType == PSNType.Branch)
2031
                    result = "HDB";
2032
                else if (StartType == PSNType.Branch && EndType == PSNType.Header)
2033
                    result = "HDB";
2034
2035
                else if (StartType == PSNType.Header && EndType == PSNType.Equipment)
2036
                    result = "HDE";
2037
                else if (StartType == PSNType.Equipment && EndType == PSNType.Header)
2038
                    result = "HDE";
2039
                else
2040
                    result = "Error";
2041
            }
2042
            else
2043
                result = "Error";
2044
2045
            return result;
2046
2047
            
2048
        }
2049 710a49f1 gaqhf
        public bool EnableType(PSNType type)
2050 94a117ca gaqhf
        {
2051
            bool result = false;
2052
2053
            if (type == PSNType.Branch ||
2054
                type == PSNType.Equipment ||
2055
                type == PSNType.Header)
2056
            {
2057
                result = true;
2058
            }
2059
2060
            return result;
2061
        }
2062 7881ec8f gaqhf
        public bool IsBypass { get; set; }
2063 94a117ca gaqhf
2064 a5616391 LJIYEON
        public string GetFromData(ref string Type, ref Item item)
2065 94a117ca gaqhf
        {
2066
            string result = string.Empty;
2067 a36541fb LJIYEON
            if (IsKeyword)
2068
                IsKeyword = false;
2069 27d06aa8 LJIYEON
            try
2070 36a45f13 gaqhf
            {
2071 27d06aa8 LJIYEON
                if (StartType == PSNType.Header)
2072
                    result = "ENDOFHEADER";
2073
                else if (StartType == PSNType.Branch)
2074
                {
2075 a5616391 LJIYEON
                    item = Groups.First().Items.First();
2076 27d06aa8 LJIYEON
                    if (item.Relations.First().Item.LineNumber != null && !string.IsNullOrEmpty(item.Relations.First().Item.LineNumber.Name))
2077
                        result = item.Relations.First().Item.LineNumber.Name;
2078
                    else
2079
                    {
2080
                        Status += ", Missing LineNumber";
2081
                        result = "Empty LineNumber";
2082
                    }
2083
                }
2084
                else if (StartType == PSNType.Equipment)
2085 a36541fb LJIYEON
                {
2086 3210f690 LJIYEON
                    if (Groups.First().Items.First().Equipment != null)
2087
                        result = Groups.First().Items.First().Equipment.ItemTag;
2088 a36541fb LJIYEON
                    DataRow drNozzle = Nozzle.Select(string.Format("OID = '{0}'", Groups.First().Items.First().UID)).FirstOrDefault();
2089
2090
                    if (drNozzle != null)
2091
                        result += " ["+ drNozzle.Field<string>("ITEMTAG") + "]";
2092
                }
2093
                
2094 36a45f13 gaqhf
                else
2095
                {
2096 72775f2e LJIYEON
                    IsValid = "Error";
2097 a5616391 LJIYEON
                    item = Groups.First().Items.First();
2098 27d06aa8 LJIYEON
                    if (item.ItemType == ItemType.Symbol)
2099
                    {
2100 a5616391 LJIYEON
                        
2101 8ab98ea3 LJIYEON
                        string keyword = string.Empty;
2102 a5616391 LJIYEON
                        keyword = GetFromKeywordData(ref Type, item);
2103 8ab98ea3 LJIYEON
2104
                        if (string.IsNullOrEmpty(keyword))
2105
                        {
2106
                            if (item.ID2DBType.Contains("OPC's"))
2107
                                Status += ", OPC Disconneted";
2108
                            else
2109
                                Status += ", Missing ItemTag or Description";
2110
2111
                            result = item.ID2DBName;
2112
                        }
2113
                        else
2114 eb44d82c LJIYEON
                        {
2115 8ab98ea3 LJIYEON
                            result = keyword;
2116 a36541fb LJIYEON
                            IsKeyword = true;
2117
                            IsValid = string.Empty;
2118 eb44d82c LJIYEON
                        }
2119 8ab98ea3 LJIYEON
                        
2120 27d06aa8 LJIYEON
                    }
2121
                    else if (item.ItemType == ItemType.Line)
2122
                    {
2123
                        Status += ", Line Disconnected";
2124
                        result = item.LineNumber != null && !string.IsNullOrEmpty(item.LineNumber.Name) ? item.LineNumber.Name : "Empty LineNumber";
2125
                    }
2126
                    else
2127
                        result = "Unknown";
2128 36a45f13 gaqhf
                }
2129
            }
2130 27d06aa8 LJIYEON
            catch(Exception ex)
2131 36a45f13 gaqhf
            {
2132
2133
            }
2134 94a117ca gaqhf
2135
            return result;
2136
        }
2137
2138 a5616391 LJIYEON
        public string GetFromKeywordData(ref string Type, Item item)
2139 879ce10b LJIYEON
        {
2140
            string result = string.Empty;
2141
           
2142 eb44d82c LJIYEON
            foreach(KeywordItem keyitem in KeywordInfos.KeywordItems)
2143 879ce10b LJIYEON
            {
2144 eb44d82c LJIYEON
                if(keyitem.Name.Equals(item.Name))
2145 a5616391 LJIYEON
                {
2146 eb44d82c LJIYEON
                    result = keyitem.Keyword;
2147 a5616391 LJIYEON
                    Type = item.ID2DBType;
2148
                    //break;
2149
                }
2150 879ce10b LJIYEON
            }
2151 eb44d82c LJIYEON
          
2152 879ce10b LJIYEON
            return result;
2153
        }
2154
2155 a5616391 LJIYEON
        public string GetToKeywordData(ref string Type, Item item)
2156 879ce10b LJIYEON
        {
2157
            string result = string.Empty;
2158
2159 eb44d82c LJIYEON
            foreach (KeywordItem keyitem in KeywordInfos.KeywordItems)
2160 879ce10b LJIYEON
            {
2161 eb44d82c LJIYEON
                if (keyitem.Name.Equals(item.Name))
2162 a5616391 LJIYEON
                {
2163 eb44d82c LJIYEON
                    result = keyitem.Keyword;
2164 a5616391 LJIYEON
                    Type = item.ID2DBType;
2165
                   // break;
2166
                }
2167 879ce10b LJIYEON
            }
2168
            return result;
2169
        }
2170
2171 a5616391 LJIYEON
        public string GetToData(ref string ToType, ref Item item)
2172 94a117ca gaqhf
        {
2173
            string result = string.Empty;
2174 3210f690 LJIYEON
          
2175 eb44d82c LJIYEON
2176 3210f690 LJIYEON
                if (IsKeyword)
2177
                    IsKeyword = false;
2178
2179
                if (EndType == PSNType.Header)
2180
                    result = "ENDOFHEADER";
2181
                else if (EndType == PSNType.Branch)
2182 36a45f13 gaqhf
                {
2183 a5616391 LJIYEON
                    item = Groups.Last().Items.Last();
2184 3210f690 LJIYEON
                    if (item.Relations.Last().Item.LineNumber != null && !string.IsNullOrEmpty(item.Relations.Last().Item.LineNumber.Name))
2185
                        result = item.Relations.Last().Item.LineNumber.Name;
2186
                    else
2187
                    {
2188
                        Status += ", Missing LineNumber";
2189
                        result = "Empty LineNumber";
2190
                    }
2191 36a45f13 gaqhf
                }
2192 3210f690 LJIYEON
                else if (EndType == PSNType.Equipment)
2193 36a45f13 gaqhf
                {
2194 3210f690 LJIYEON
                    if(Groups.Last().Items.Last().Equipment != null)
2195
                        result = Groups.Last().Items.Last().Equipment.ItemTag;
2196 8ab98ea3 LJIYEON
2197 3210f690 LJIYEON
                    DataRow drNozzle = Nozzle.Select(string.Format("OID = '{0}'", Groups.First().Items.First().UID)).FirstOrDefault();
2198
2199
                    if (drNozzle != null)
2200
                        result += " [" + drNozzle.Field<string>("ITEMTAG") + "]";
2201
                }
2202
                else
2203
                {
2204
                    IsValid = "Error";
2205 a5616391 LJIYEON
                    item = Groups.Last().Items.Last();
2206 3210f690 LJIYEON
                    if (item.ItemType == ItemType.Symbol)
2207 8ab98ea3 LJIYEON
                    {
2208 3210f690 LJIYEON
                        string keyword = string.Empty;
2209 a5616391 LJIYEON
                        keyword = GetToKeywordData(ref ToType, item);
2210 3210f690 LJIYEON
2211
                        if (string.IsNullOrEmpty(keyword))
2212
                        {
2213
                            if (item.ID2DBType.Contains("OPC's"))
2214
                                Status += ", OPC Disconneted";
2215
                            else
2216
                                Status += ", Missing ItemTag or Description";
2217
2218
                            result = item.ID2DBName;
2219
                        }
2220 8ab98ea3 LJIYEON
                        else
2221 3210f690 LJIYEON
                        {
2222
                            result = keyword;
2223
                            IsValid = string.Empty;
2224
                            IsKeyword = true;
2225
                        }
2226 8ab98ea3 LJIYEON
2227
                    }
2228 3210f690 LJIYEON
                    else if (item.ItemType == ItemType.Line)
2229 eb44d82c LJIYEON
                    {
2230 3210f690 LJIYEON
                        Status += ", Line Disconnected";
2231
                        result = item.LineNumber != null && !string.IsNullOrEmpty(item.LineNumber.Name) ? item.LineNumber.Name : "Empty LineNumber";
2232 eb44d82c LJIYEON
                    }
2233 3210f690 LJIYEON
                    else
2234
                        result = "Unknown";
2235 36a45f13 gaqhf
                }
2236 3210f690 LJIYEON
        
2237
         
2238 94a117ca gaqhf
            return result;
2239
        }
2240 7881ec8f gaqhf
2241
        public string GetPBSData()
2242
        {
2243
            string result = string.Empty;
2244
            List<string> PBSList = new List<string>();
2245
            if (Settings.Default.PBSSetting.Equals("Line Number"))
2246
            {
2247
                string attrValue = Settings.Default.PBSSettingValue;
2248
2249
                foreach (Group group in Groups)
2250
                {
2251
                    List<LineNumber> lineNumbers = group.Items.Select(x =>x.LineNumber).Distinct().ToList();
2252
                    foreach (LineNumber lineNumber in lineNumbers)
2253
                    {
2254
                        Attribute attribute = lineNumber.Attributes.Find(x => x.Name == attrValue && !string.IsNullOrEmpty(x.Value));
2255
                        if (attribute != null)
2256
                        {
2257
                            string value = attribute.Value;
2258
                            if (!PBSList.Contains(value))
2259
                                PBSList.Add(value);
2260
                        }
2261
                    }
2262
                }
2263
            }
2264
            else if (Settings.Default.PBSSetting.Equals("Item Attribute"))
2265
            {
2266
                string attrValue = Settings.Default.PBSSettingValue;
2267
2268
                foreach (Group group in Groups)
2269
                {
2270
                    List<Item> items = group.Items.FindAll(x => x.Attributes.Find(y => y.Name == attrValue && !string.IsNullOrEmpty(y.Value)) != null);
2271
                    foreach (Item item in items)
2272
                    {
2273
                        string value = item.Attributes.Find(x => x.Name == attrValue).Value;
2274
                        if (!PBSList.Contains(value))
2275
                            PBSList.Add(value);
2276
                    }
2277
                }
2278
            }
2279
            else if (Settings.Default.PBSSetting.Equals("Drawing No"))
2280
            {
2281
                string attrValue = Settings.Default.PBSSettingValue;
2282
2283
                foreach (Group group in Groups)
2284
                {
2285
                    List<Document> documents = group.Items.Select(x => x.Document).Distinct().ToList();
2286
                    foreach (Document document in documents)
2287
                    {
2288
                        string name = document.DrawingName;
2289
2290
                        int startIndex = Settings.Default.PBSSettingStartValue;
2291
                        int endIndex = Settings.Default.PBSSettingEndValue;
2292
2293
                        string subStr = name.Substring(startIndex - 1, endIndex - startIndex + 1);
2294
                        if (!PBSList.Contains(subStr))
2295
                            PBSList.Add(subStr);
2296
                    }
2297
                }
2298
            }
2299
            else if (Settings.Default.PBSSetting.Equals("Unit Area"))
2300
            {
2301
                foreach (Group group in Groups)
2302
                {
2303
                    List<Document> documents = group.Items.Select(x => x.Document).Distinct().ToList();
2304
                    foreach (Document document in documents)
2305
                    {
2306
                        List<TextInfo> textInfos = document.TextInfos.FindAll(x => x.Area == "Unit");
2307
                        foreach (TextInfo textInfo in textInfos)
2308
                        {
2309
                            if (!PBSList.Contains(textInfo.Value))
2310
                                PBSList.Add(textInfo.Value);
2311
                        }
2312
                    }
2313
                }
2314
            }
2315
2316
            foreach (var item in PBSList)
2317
            {
2318
                if (string.IsNullOrEmpty(result))
2319
                    result = item;
2320
                else
2321
                    result += ", " + item;
2322
            }
2323
            return result;
2324
        }
2325 6b9e7a56 gaqhf
    }
2326
}
클립보드 이미지 추가 (최대 크기: 500 MB)