프로젝트

일반

사용자정보

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

hytos / DTI_PID / ID2PSN / Document.cs @ eaa41534

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

1 6b9e7a56 gaqhf
using DevExpress.Utils.StructuredStorage.Internal.Reader;
2
using System;
3 0dae5645 gaqhf
using System.Collections.Generic;
4 6b9e7a56 gaqhf
using System.Data;
5 0dae5645 gaqhf
using System.IO;
6
using System.Linq;
7
using System.Text;
8
using System.Threading.Tasks;
9
using System.Xml.Linq;
10
11
namespace ID2PSN
12
{
13 6b9e7a56 gaqhf
    public class Document
14 0dae5645 gaqhf
    {
15
        public string DrawingName { get; set; }
16
        public string FileName { get; set; }
17
        public string FilePath { get; set; }
18
        public List<Item> Items { get; set; }
19
        public List<LineNumber> LineNumbers { get; set; }
20 6b9e7a56 gaqhf
        public List<Group> Groups { get; set; }
21 0dae5645 gaqhf
        public List<RunInfo> RunInfos { get; set; }
22 6b9e7a56 gaqhf
        public List<Item> SegmentBreaks { get; set; }
23
        public List<Equipment> Equipments { get; set; }
24 7881ec8f gaqhf
        public List<TextInfo> TextInfos { get; set; }
25 0dae5645 gaqhf
26 7881ec8f gaqhf
        private DataTable symbolNameTable = null;
27 4c76a67a gaqhf
        private DataTable equipmentTable = DB.GetEquipmentType();
28
29 7881ec8f gaqhf
        public Document(string filePath, DataTable symbolNameTable)
30 0dae5645 gaqhf
        {
31 7881ec8f gaqhf
            this.symbolNameTable = symbolNameTable;
32 0dae5645 gaqhf
            FilePath = filePath;
33 7881ec8f gaqhf
            // XML Data 정리
34 0dae5645 gaqhf
            ReadFile();
35 7881ec8f gaqhf
            // 해당 소스는 Line-Equipment가 붙어있을 시 가상의 Nozzle Data를 넣어줌 (삼엔 요청)
36 0f07fa34 gaqhf
            InsertNozzle();
37 7881ec8f gaqhf
            // ID2 Item Relation 정보 기준으로 Item Object 연결
38 0dae5645 gaqhf
            SetRelationItem();
39 7881ec8f gaqhf
            // 속성 상관 없이 drawing 단위의 PSN 생성
40 6b9e7a56 gaqhf
            SetGroup();
41 7881ec8f gaqhf
            // Instrument Line이 있는 아이템/group 제거
42 0f07fa34 gaqhf
            RemoveItems();
43 7881ec8f gaqhf
44
            foreach (var item in Items)
45
                item.Document = this;
46 0dae5645 gaqhf
        }
47
        private void ReadFile()
48
        {
49
            XElement xml = XElement.Load(FilePath);
50
            FileName = Path.GetFileNameWithoutExtension(FilePath);
51
            DrawingName = xml.Element("DWGNAME").Value;
52
            Items = new List<Item>();
53
            LineNumbers = new List<LineNumber>();
54
            RunInfos = new List<RunInfo>();
55 6b9e7a56 gaqhf
            Groups = new List<Group>();
56
            SegmentBreaks = new List<Item>();
57
            Equipments = new List<Equipment>();
58 7881ec8f gaqhf
            TextInfos = new List<TextInfo>();
59 0dae5645 gaqhf
60
            foreach (XElement element in xml.Element("SYMBOLS").Elements("SYMBOL"))
61
                Items.Add(GetSymbolItem(element));
62
            foreach (XElement element in xml.Element("LINEINFOS").Elements("LINE"))
63
                Items.Add(GetLineItem(element));
64
            foreach (XElement element in xml.Element("LINENOS").Elements("LINE_NO"))
65
                LineNumbers.Add(GetLineNumber(element));
66 6b9e7a56 gaqhf
            foreach (XElement element in xml.Element("VENDORS").Elements("VENDOR"))
67
                Equipments.Add(GetEquipment(element));
68 7881ec8f gaqhf
            foreach (XElement element in xml.Element("TEXTINFOS").Elements("ATTRIBUTE"))
69
                TextInfos.Add(GetTextInfo(element));
70 6b9e7a56 gaqhf
71
72 7881ec8f gaqhf
            List<Item> segmentBreaks = Items.FindAll(x => x.ID2DBType.ToUpper() == "Segment Breaks".ToUpper() || x.ID2DBType.ToUpper() == "End Break".ToUpper());
73 6b9e7a56 gaqhf
            SegmentBreaks.AddRange(segmentBreaks);
74
            foreach (var item in segmentBreaks)
75
                Items.Remove(item);
76 0dae5645 gaqhf
77 4c76a67a gaqhf
            foreach (DataRow row in equipmentTable.Rows)
78
            {
79
                string type = row["Type"].ToString();
80 7881ec8f gaqhf
                List<Item> equipments = Items.FindAll(x => x.ID2DBType == type);
81 4c76a67a gaqhf
82
                foreach (Item item in equipments)
83
                {
84
                    Equipments.Add(ConvertEquipment(item));
85
                    Items.Remove(item);
86
                }
87
            }
88
89 0dae5645 gaqhf
            SetRunInfo(xml);
90
        }
91
        private Item GetSymbolItem(XElement element)
92
        {
93
            Item item = new Item();
94
            item.UID = element.Element("UID").Value;
95
            item.Name = element.Element("NAME").Value;
96 7881ec8f gaqhf
            item.ID2DBType = element.Element("TYPE").Value;
97 0dae5645 gaqhf
            item.Owner = element.Element("OWNER").Value;
98 7881ec8f gaqhf
            item.ID2DBName = element.Element("PARENT").Value;
99 6b9e7a56 gaqhf
            string[] sPoint = element.Element("ORIGINALPOINT").Value.Split(new char[] { ',' });
100
            item.POINT = new double[] { Convert.ToDouble(sPoint[0]), Convert.ToDouble(sPoint[1]) };
101
            item.ANGLE = element.Element("ANGLE").Value;
102 0dae5645 gaqhf
            item.Relations = GetRelations(element.Element("CONNECTORS"));
103
            item.Attributes = GetAttributes(element.Element("SYMBOLATTRIBUTES"));
104
            item.ItemType = ItemType.Symbol;
105 7881ec8f gaqhf
            item.ID2DBCategory = GetID2DBCategory(item.ID2DBType, item.ID2DBName);
106 0dae5645 gaqhf
107 7881ec8f gaqhf
            if (item.ID2DBType.ToUpper().Equals("PIPING OPC'S"))
108 6b9e7a56 gaqhf
                item.SubItemType = SubItemType.OPC;
109 7881ec8f gaqhf
            else if (item.ID2DBType.ToUpper().Equals("NOZZLES"))
110 6b9e7a56 gaqhf
                item.SubItemType = SubItemType.Nozzle;
111
112 0dae5645 gaqhf
            return item;
113
        }
114
        private Item GetLineItem(XElement element)
115
        {
116
            Item item = new Item();
117
            item.UID = element.Element("UID").Value;
118
            item.Name = element.Element("TYPE").Value;
119 7881ec8f gaqhf
            item.ID2DBType = element.Element("TYPE").Value;
120 0dae5645 gaqhf
            item.Owner = element.Attribute("OWNER").Value;
121
122
            item.Relations = GetRelations(element.Element("CONNECTORS"));
123
            item.Attributes = GetAttributes(element.Element("SYMBOLATTRIBUTES"));
124
            item.ItemType = ItemType.Line;
125
126
            return item;
127
        }
128
        private LineNumber GetLineNumber(XElement element)
129
        {
130
            LineNumber lineNumber = new LineNumber();
131
            lineNumber.UID = element.Element("UID").Value;
132
            lineNumber.Name = element.Element("TEXT").Value;
133
            lineNumber.Attributes = GetAttributes(element);
134
            return lineNumber;
135
        }
136 6b9e7a56 gaqhf
        private Equipment GetEquipment(XElement element)
137
        {
138
            Equipment item = new Equipment();
139
            item.UID = element.Element("UID").Value;
140 c6503eaa gaqhf
            item.Attributes = GetAttributes(element.Element("SYMBOLATTRIBUTES"));
141 6b9e7a56 gaqhf
            string[] sPoint = element.Element("POINT").Value.Split(new char[] { '/' });
142
            foreach (var sp in sPoint)
143
            {
144
                string[] xy = sp.Split(new char[] { ',' });
145
                item.POINT.Add(new double[] { Convert.ToDouble(xy[0]), Convert.ToDouble(xy[1]) });
146
            }
147
148
            return item;
149
        }
150 7881ec8f gaqhf
        private TextInfo GetTextInfo(XElement element)
151
        {
152
            TextInfo textInfo = new TextInfo();
153
            textInfo.Value = element.Element("VALUE").Value;
154
            textInfo.Area = element.Element("AREA").Value;
155 4c76a67a gaqhf
156 7881ec8f gaqhf
            return textInfo;
157
        }
158 4c76a67a gaqhf
        private Equipment ConvertEquipment(Item item)
159
        {
160
            Equipment equipment = new Equipment();
161
            equipment.Name = item.Name;
162
            equipment.UID = item.UID;
163
            equipment.POINT.Add(item.POINT);
164 c6503eaa gaqhf
            equipment.Attributes.AddRange(item.Attributes);
165 4c76a67a gaqhf
            return equipment;
166
        }
167 0dae5645 gaqhf
        private List<Relation> GetRelations(XElement element)
168
        {
169
            List<Relation> result = new List<Relation>();
170
            foreach (XElement item in element.Elements("CONNECTOR"))
171
            {
172
                string[] sPoint = item.Element("SCENECONNECTPOINT").Value.Split(new char[] { ',' });
173
                result.Add(new Relation()
174
                {
175
                    UID = item.Element("CONNECTEDITEM").Value,
176 6b9e7a56 gaqhf
                    Point = new double[] { Convert.ToDouble(sPoint[0]), Convert.ToDouble(sPoint[1]) }
177 0dae5645 gaqhf
                });
178
            }
179 6b9e7a56 gaqhf
180 0dae5645 gaqhf
            return result;
181
        }
182
        private List<Attribute> GetAttributes(XElement element)
183
        {
184
            List<Attribute> result = new List<Attribute>();
185
            foreach (XElement item in element.Elements("ATTRIBUTE"))
186
            {
187
                result.Add(new Attribute()
188
                {
189
                    UID = item.Attribute("UID").Value,
190
                    Name = item.Attribute("Attribute").Value,
191
                    DisplayName = item.Attribute("DisplayAttribute").Value,
192
                    Value = item.Value
193
                });
194
            }
195
            return result;
196
        }
197 7881ec8f gaqhf
        private string GetID2DBCategory(string type, string name)
198
        {
199
            string result = string.Empty;
200
            DataRow[] rows = symbolNameTable.Select(string.Format("Type = '{0}' AND Name = '{1}'", type.Replace("'", "''"), name.Replace("'", "''")));
201
            if (rows.Length.Equals(1))
202
                result = rows.First()["Category"].ToString();
203
204
            return result;
205
        }
206 0dae5645 gaqhf
        private void SetRelationItem()
207
        {
208
            foreach (Item item in Items)
209
                foreach (Relation relation in item.Relations)
210
                    relation.Item = Items.Find(x => x.UID.Equals(relation.UID));
211
        }
212 1be87cbd gaqhf
        private void SetRunInfo(XElement xml)
213
        {
214 6b9e7a56 gaqhf
            foreach (XElement element in xml.Element("LINENOS").Elements("LINE_NO"))
215
            {
216
                string UID = element.Element("UID").Value;
217
                foreach (XElement run in element.Elements("RUN"))
218
                {
219
                    RunInfo runInfo = new RunInfo() { UID = UID };
220
                    foreach (XElement line in run.Elements("LINE"))
221
                        runInfo.Items.Add(Items.Find(x => x.UID == line.Element("UID").Value));
222
                    foreach (XElement symbol in run.Elements("SYMBOL"))
223
                        runInfo.Items.Add(Items.Find(x => x.UID == symbol.Element("UID").Value));
224
                    RunInfos.Add(runInfo);
225
                }
226
            }
227
228 1be87cbd gaqhf
            foreach (XElement element in xml.Element("TRIMLINENOS").Elements("TRIM_LINE_NO"))
229
            {
230
                string UID = element.Element("UID").Value;
231
                foreach (XElement run in element.Elements("RUN"))
232
                {
233
                    RunInfo runInfo = new RunInfo() { UID = UID };
234
                    foreach (XElement line in run.Elements("LINE"))
235
                        runInfo.Items.Add(Items.Find(x => x.UID == line.Element("UID").Value));
236
                    foreach (XElement symbol in run.Elements("SYMBOL"))
237
                        runInfo.Items.Add(Items.Find(x => x.UID == symbol.Element("UID").Value));
238
                    RunInfos.Add(runInfo);
239
                }
240
            }
241
        }
242 6b9e7a56 gaqhf
        private void SetGroup()
243 0dae5645 gaqhf
        {
244
            List<Item> orderItems = Items.OrderByDescending(x => x.ItemType == ItemType.Line).ToList();
245
246 6b9e7a56 gaqhf
            Dictionary<Item, string> itemDic = new Dictionary<Item, string>();
247 0dae5645 gaqhf
            foreach (Item item in orderItems)
248 6b9e7a56 gaqhf
                itemDic.Add(item, Guid.NewGuid().ToString());
249 0dae5645 gaqhf
250
            foreach (Item item in orderItems)
251
            {
252 6b9e7a56 gaqhf
                string groupKey = itemDic[item];
253 0dae5645 gaqhf
                if (item.ItemType == ItemType.Line)
254
                {
255
                    GroupingForwardLine();
256
                    GroupingBackwardLine();
257
                }
258 1be87cbd gaqhf
259 0dae5645 gaqhf
                void GroupingForwardLine()
260
                {
261
                    Item connItem = item.Relations[1].Item;
262
                    if (connItem != null && IsConnected(connItem, item))
263
                    {
264
                        if (connItem.ItemType == ItemType.Line && item.Equals(connItem.Relations[0].Item))
265 6b9e7a56 gaqhf
                            ChangeGroupID(itemDic[connItem], groupKey);
266 0dae5645 gaqhf
                        else if (connItem.ItemType == ItemType.Symbol)
267
                        {
268
                            List<Item> allConnItems = GetConnectedLines(connItem);
269
                            allConnItems.Remove(item);
270
                            List<Item> connItems = GetConnectedForwardLines(connItem);
271
                            if (allConnItems.Count.Equals(1) && connItems.Count.Equals(1) && allConnItems[0].Equals(connItems[0]))
272
                            {
273
                                List<Item> connSymbols = GetConnectedSymbols(connItem);
274
                                foreach (Item loopItem in connSymbols)
275 6b9e7a56 gaqhf
                                    ChangeGroupID(itemDic[loopItem], groupKey);
276 0dae5645 gaqhf
                            }
277
                            else
278
                            {
279
                                List<Item> endItems = new List<Item>();
280
                                Stack<Item> stacks = new Stack<Item>();
281
                                stacks.Push(connItem);
282
                                while (stacks.Count > 0)
283
                                {
284
                                    Item stack = stacks.Pop();
285
                                    if (endItems.Contains(stack))
286
                                        continue;
287
                                    endItems.Add(stack);
288
289
                                    if (GetConnectedItemCount(stack) < 3)
290
                                    {
291 6b9e7a56 gaqhf
                                        ChangeGroupID(itemDic[stack], groupKey);
292 0dae5645 gaqhf
                                        List<Relation> relations = stack.Relations.FindAll(x => x.Item != null && x.Item.ItemType == ItemType.Symbol);
293
                                        foreach (Relation relation in relations)
294
                                            stacks.Push(relation.Item);
295
                                    }
296 1be87cbd gaqhf
                                    else if (IsSameRun(item, stack))
297 0dae5645 gaqhf
                                    {
298 6b9e7a56 gaqhf
                                        ChangeGroupID(itemDic[stack], groupKey);
299 0dae5645 gaqhf
                                        List<Relation> relations = stack.Relations.FindAll(x => x.Item != null && x.Item.ItemType == ItemType.Symbol && IsSameRun(item, x.Item));
300
                                        foreach (Relation relation in relations)
301
                                            stacks.Push(relation.Item);
302
                                    }
303
                                }
304
                            }
305
                        }
306
                    }
307
                }
308
                void GroupingBackwardLine()
309
                {
310
                    Item connItem = item.Relations[0].Item;
311
                    if (connItem != null && IsConnected(connItem, item))
312
                    {
313
                        if (connItem.ItemType == ItemType.Line && item.Equals(connItem.Relations[1].Item))
314 6b9e7a56 gaqhf
                            ChangeGroupID(itemDic[connItem], groupKey);
315 0dae5645 gaqhf
                        else if (connItem.ItemType == ItemType.Symbol)
316
                        {
317
                            List<Item> allConnItems = GetConnectedLines(connItem);
318
                            allConnItems.Remove(item);
319
                            List<Item> connItems = GetConnectedBackwardLines(connItem);
320
                            if (allConnItems.Count.Equals(1) && connItems.Count.Equals(1) && allConnItems[0].Equals(connItems[0]))
321
                            {
322
                                List<Item> connSymbols = GetConnectedSymbols(connItem);
323
                                foreach (Item loopItem in connSymbols)
324 6b9e7a56 gaqhf
                                    ChangeGroupID(itemDic[loopItem], groupKey);
325 0dae5645 gaqhf
                            }
326
                            else
327
                            {
328
                                List<Item> endItems = new List<Item>();
329
                                Stack<Item> stacks = new Stack<Item>();
330
                                stacks.Push(connItem);
331
                                while (stacks.Count > 0)
332
                                {
333
                                    Item stack = stacks.Pop();
334
                                    if (endItems.Contains(stack))
335
                                        continue;
336
                                    endItems.Add(stack);
337
338
                                    if (GetConnectedItemCount(stack) < 3)
339
                                    {
340 6b9e7a56 gaqhf
                                        ChangeGroupID(itemDic[stack], groupKey);
341 0dae5645 gaqhf
                                        List<Relation> relations = stack.Relations.FindAll(x => x.Item != null && x.Item.ItemType == ItemType.Symbol);
342
                                        foreach (Relation relation in relations)
343
                                            stacks.Push(relation.Item);
344
                                    }
345
                                    else if (IsSameRun(item, stack))
346
                                    {
347 6b9e7a56 gaqhf
                                        ChangeGroupID(itemDic[stack], groupKey);
348 0dae5645 gaqhf
                                        List<Relation> relations = stack.Relations.FindAll(x => x.Item != null && x.Item.ItemType == ItemType.Symbol && IsSameRun(item, x.Item));
349
                                        foreach (Relation relation in relations)
350
                                            stacks.Push(relation.Item);
351
                                    }
352
                                }
353
                            }
354
                        }
355
                    }
356
                }
357
            }
358
359 6b9e7a56 gaqhf
            List<string> endGroupIds = new List<string>();
360
            foreach (var item in itemDic)
361
            {
362
                if (endGroupIds.Contains(item.Value))
363
                    continue;
364
                endGroupIds.Add(item.Value);
365
366 0f07fa34 gaqhf
                List<Item> groupItems = itemDic.Where(x => x.Value.Equals(item.Value)).Select(x => x.Key).ToList();
367
                if (groupItems.Find(x => x.ItemType == ItemType.Line) == null)
368
                {
369
                    foreach (var groupItem in groupItems)
370
                        Items.Remove(groupItem);
371
                }
372
                else
373
                {
374
                    Group group = new Group(this);
375
                    group.Items = groupItems;
376
                    group.UID = item.Value;
377
                    group.SortItems();
378 36a45f13 gaqhf
                    foreach (Item groupItem in groupItems)
379
                        groupItem.Group = group;
380 0f07fa34 gaqhf
                    Groups.Add(group);
381
                }
382 6b9e7a56 gaqhf
            }
383
384
            #region HeaderSetting
385
            List<HeaderInfo> HeaderInfos = new List<HeaderInfo>();
386
            DataTable dt = DB.SelectHeaderSetting();
387
            foreach (DataRow row in dt.Rows)
388
            {
389
                string groupID = row["GROUP_ID"].ToString();
390
                string desc = row["DESCRIPTION"].ToString();
391
                int index = Convert.ToInt32(row["INDEX"]);
392
                string name = row["NAME"].ToString();
393
394
                HeaderInfo headerInfo = HeaderInfos.Find(x => x.UID.Equals(groupID));
395
                if (headerInfo == null)
396
                {
397
                    headerInfo = new HeaderInfo(groupID);
398
                    headerInfo.Description = desc;
399
                    HeaderInfos.Add(headerInfo);
400
                }
401
402
                headerInfo.HeaderItems.Add(new HeaderItem()
403
                {
404
                    Index = index,
405
                    Name = name
406
                });
407
            }
408
            foreach (HeaderInfo headerInfo in HeaderInfos)
409
                headerInfo.HeaderItems = headerInfo.HeaderItems.OrderBy(x => x.Index).ToList();
410
411
            foreach (Group group in Groups)
412
            {
413
                List<HeaderInfo> endInfos = new List<HeaderInfo>();
414
                bool bFind = false;
415
                for (int i = 0; i < group.Items.Count; i++)
416
                {
417
                    Item item = group.Items[i];
418
                    foreach (HeaderInfo header in HeaderInfos)
419
                    {
420
                        if (endInfos.Contains(header))
421
                            continue;
422
423
                        if (!header.HeaderItems[i].Name.Equals(item.Name))
424
                        {
425
                            endInfos.Add(header);
426
                            continue;
427
                        }
428
429
                        if (header.HeaderItems.Count.Equals(i + 1))
430
                        {
431
                            for (int j = 0; j < i + 1; j++)
432
                                group.Items[j].SubItemType = SubItemType.Header;
433
                            bFind = true;
434
                            break;
435
                        }
436
                    }
437
438
                    if (bFind || endInfos.Count.Equals(HeaderInfos.Count))
439
                        break;
440
                }
441
442
                endInfos = new List<HeaderInfo>();
443
                bFind = false;
444
                for (int i = 0; i < group.Items.Count; i++)
445
                {
446
                    Item item = group.Items[group.Items.Count - i - 1];
447
                    foreach (HeaderInfo header in HeaderInfos)
448
                    {
449
                        if (endInfos.Contains(header))
450
                            continue;
451
452
                        if (!header.HeaderItems[i].Name.Equals(item.Name))
453
                        {
454
                            endInfos.Add(header);
455
                            continue;
456
                        }
457
458
                        if (header.HeaderItems.Count.Equals(i + 1))
459
                        {
460
                            for (int j = 0; j < i + 1; j++)
461
                                group.Items[group.Items.Count - j - 1].SubItemType = SubItemType.Header;
462
                            bFind = true;
463
                            break;
464
                        }
465
                    }
466
467
                    if (bFind || endInfos.Count.Equals(HeaderInfos.Count))
468
                        break;
469
                }
470
            }
471 0dae5645 gaqhf
            #endregion
472
473
            int GetConnectedItemCount(Item item)
474
            {
475
                return item.Relations.FindAll(x => x.Item != null).Count;
476
            }
477
478
            List<Item> GetConnectedLines(Item item)
479
            {
480
                List<Item> result = new List<Item>();
481
                List<Item> end = new List<Item>();
482
                Stack<Item> stacks = new Stack<Item>();
483
                stacks.Push(item);
484
                while (stacks.Count > 0)
485
                {
486
                    Item stack = stacks.Pop();
487
                    if (end.Contains(stack) || stack.ItemType != ItemType.Symbol)
488
                        continue;
489
                    end.Add(stack);
490
491
                    List<Relation> relations = stack.Relations.FindAll(x => x.Item != null && x.Item.ItemType == ItemType.Symbol);
492
                    foreach (Relation relation in relations)
493
                        stacks.Push(relation.Item);
494
495
                    relations = stack.Relations.FindAll(x => x.Item != null && x.Item.ItemType == ItemType.Line);
496
                    foreach (Relation relation in relations)
497
                    {
498
                        result.Add(relation.Item);
499
                        end.Add(relation.Item);
500
                    }
501
                }
502
503
                return result;
504
            }
505
506
            List<Item> GetConnectedForwardLines(Item item)
507
            {
508
                List<Item> result = new List<Item>();
509
                List<Item> end = new List<Item>();
510
                Stack<Item> stacks = new Stack<Item>();
511
                stacks.Push(item);
512
                while (stacks.Count > 0)
513
                {
514
                    Item stack = stacks.Pop();
515
                    if (end.Contains(stack) || stack.ItemType != ItemType.Symbol)
516
                        continue;
517
                    end.Add(stack);
518
519
                    List<Relation> relations = stack.Relations.FindAll(x => x.Item != null && x.Item.ItemType == ItemType.Symbol);
520
                    foreach (Relation relation in relations)
521
                        stacks.Push(relation.Item);
522
523
                    relations = stack.Relations.FindAll(x => x.Item != null && x.Item.ItemType == ItemType.Line);
524
                    foreach (Relation relation in relations)
525
                    {
526
                        if (relation.Item.Relations.FindIndex(x => x.Item != null && x.Item.Equals(stack)).Equals(0))
527
                            result.Add(relation.Item);
528
529
                        end.Add(relation.Item);
530
                    }
531
                }
532
533
                return result;
534
            }
535
536
            List<Item> GetConnectedBackwardLines(Item item)
537
            {
538
                List<Item> result = new List<Item>();
539
                List<Item> end = new List<Item>();
540
                Stack<Item> stacks = new Stack<Item>();
541
                stacks.Push(item);
542
                while (stacks.Count > 0)
543
                {
544
                    Item stack = stacks.Pop();
545
                    if (end.Contains(stack) || stack.ItemType != ItemType.Symbol)
546
                        continue;
547
                    end.Add(stack);
548
549
                    List<Relation> relations = stack.Relations.FindAll(x => x.Item != null && x.Item.ItemType == ItemType.Symbol);
550
                    foreach (Relation relation in relations)
551
                        stacks.Push(relation.Item);
552
553
                    relations = stack.Relations.FindAll(x => x.Item != null && x.Item.ItemType == ItemType.Line);
554
                    foreach (Relation relation in relations)
555
                    {
556
                        if (relation.Item.Relations.FindIndex(x => x.Item != null && x.Item.Equals(stack)).Equals(1))
557
                            result.Add(relation.Item);
558
559
                        end.Add(relation.Item);
560
                    }
561
                }
562
563
                return result;
564
            }
565
566
            List<Item> GetConnectedSymbols(Item item)
567
            {
568
                List<Item> result = new List<Item>();
569
                List<Item> end = new List<Item>();
570
                Stack<Item> stacks = new Stack<Item>();
571
                stacks.Push(item);
572
                while (stacks.Count > 0)
573
                {
574
                    Item stack = stacks.Pop();
575
                    if (end.Contains(stack) || stack.ItemType != ItemType.Symbol)
576
                        continue;
577
                    end.Add(stack);
578
                    result.Add(stack);
579
                    List<Relation> relations = stack.Relations.FindAll(x => x.Item != null && x.Item.ItemType == ItemType.Symbol);
580
                    foreach (Relation relation in relations)
581
                        stacks.Push(relation.Item);
582
                }
583
584
                return result;
585
            }
586
587
            void ChangeGroupID(string from, string to)
588
            {
589
                if (from.Equals(to))
590
                    return;
591 1be87cbd gaqhf
592 0dae5645 gaqhf
                List<Item> changeItems = new List<Item>();
593 6b9e7a56 gaqhf
                foreach (var _item in itemDic)
594 0dae5645 gaqhf
                    if (_item.Value.Equals(from))
595
                        changeItems.Add(_item.Key);
596
                foreach (var _item in changeItems)
597 6b9e7a56 gaqhf
                    itemDic[_item] = to;
598 0dae5645 gaqhf
            }
599
600
            bool IsConnected(Item item1, Item item2)
601
            {
602
                if (item1.Relations.Find(x => x.UID.Equals(item2.UID)) != null &&
603
                    item2.Relations.Find(x => x.UID.Equals(item1.UID)) != null)
604
                    return true;
605
                else
606
                    return false;
607
            }
608
609
            bool IsSameRun(Item item1, Item item2)
610
            {
611
                return RunInfos.Find(x => x.Items.Contains(item1) && x.Items.Contains(item2)) != null ? true : false;
612
            }
613
        }
614 0f07fa34 gaqhf
        private void RemoveItems()
615
        {
616
            List<Group> removeGroups = new List<Group>();
617
            foreach (Group group in Groups)
618
            {
619
                Item instLine = group.Items.Find(x => x.ItemType == ItemType.Line && (x.Name != "Secondary" && x.Name != "Primary"));
620
                if (instLine != null)
621
                    removeGroups.Add(group);
622
            }
623
624
            foreach (Group group in removeGroups)
625
            {
626
                foreach (var item in group.Items)
627
                    Items.Remove(item);
628
                Groups.Remove(group);
629
            }
630
        }
631
        private void InsertNozzle()
632
        {
633
            List<Item> newNozzles = new List<Item>();
634
            foreach (var item in Items)
635
            {
636
                if (item.SubItemType == SubItemType.Nozzle)
637
                    continue;
638
                
639
                foreach (Relation relation in item.Relations)
640
                {
641
                    Equipment equipment = Equipments.Find(x => x.UID == relation.UID);
642
                    if (equipment != null)
643
                    {
644
                        Item newNozzle = new Item();
645
                        newNozzle.UID = Guid.NewGuid().ToString();
646
                        newNozzle.Name = "PSN_Nozzle";
647 7881ec8f gaqhf
                        newNozzle.ID2DBType = "Nozzles";
648 0f07fa34 gaqhf
                        newNozzle.Owner = item.Owner;
649 7881ec8f gaqhf
                        newNozzle.ID2DBName = "PSN_Nozzle";
650 0f07fa34 gaqhf
                        newNozzle.POINT = relation.Point;
651
                        newNozzle.Relations = new List<Relation>();
652
653
                        newNozzle.Relations.Add(new Relation()
654
                        {
655
                            Point = relation.Point,
656
                            UID = equipment.UID
657
                        });
658
                        newNozzle.Relations.Add(new Relation()
659
                        {
660
                            Point = relation.Point,
661
                            UID = item.UID
662
                        });
663
664
                        newNozzle.ItemType = ItemType.Symbol;
665
                        newNozzle.SubItemType = SubItemType.Nozzle;
666
667
                        newNozzles.Add(newNozzle);
668
669
                        relation.UID = newNozzle.UID;
670
                    }
671
                }
672
            }
673
674
            Items.AddRange(newNozzles);
675
        }
676 0dae5645 gaqhf
    }
677
}
클립보드 이미지 추가 (최대 크기: 500 MB)