프로젝트

일반

사용자정보

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

hytos / DTI_PID / ID2PSN / PSN.cs @ bfb338d6

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