프로젝트

일반

사용자정보

통계
| 개정판:

hytos / DTI_PID / SPPIDConverter_AutoModeling / Modeling / AutoModeling.cs @ d5a7b262

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

1
using System;
2
using System.Windows.Forms;
3
using System.Collections.Generic;
4
using System.Drawing;
5
using System.Linq;
6
using System.Text;
7
using System.Threading.Tasks;
8
using System.Xml.Linq;
9
using Llama;
10
using Plaice;
11
using SPPID.Model;
12
using SPPID.Utill;
13
using System.Diagnostics;
14
using SPPIDConverter_AutoModeling;
15

    
16

    
17
namespace SPPID.Modeling
18
{
19
    public class AutoModeling
20
    {
21
        private Document document;
22
        
23

    
24
        public AutoModeling(Document doc)
25
        {
26
            document = doc;
27
        }
28

    
29
        public void Run()
30
        {
31
            CloseOPCForm.Run();
32

    
33
            ProgressForm.SetProgressAllValue(0);
34

    
35
            // Group Symbol 실패시 Group Remove
36
            ProgressForm.UpdateStatus("Symbol Modeling");
37
            ProgressForm.SetProgressStatusValue(0);
38
            ProgressForm.SetProgressStatusMax(document.SYMBOLS.Count);
39
            foreach (Group group in document.GROUPS)
40
            {
41
                if (!GroupSymbolModeling(group))
42
                {
43
                    group.Enable = false;
44
                }
45
            }
46
            ProgressForm.SetProgressStatusMax();
47
            ProgressForm.IncreaseProgressAll();
48
            System.Threading.Thread.Sleep(100);
49

    
50
            // Line Group은 Group 없을 때까지 Loop
51
            ProgressForm.UpdateStatus("Line Modeling");
52
            ProgressForm.SetProgressStatusValue(0);
53
            ProgressForm.SetProgressStatusMax(document.LINES.Count);
54
            while (document.GROUPS.Count > 0)
55
            {
56
                bool loopAction = false;
57
                foreach (Group group in document.GROUPS)
58
                {
59
                    if (!group.Enable)
60
                        continue;
61

    
62
                    if (IsPossibleDrawGroupLine(group))
63
                    {
64
                        loopAction = true;
65
                        GroupLineModeling(group);
66
                        document.GROUPS.Remove(group);
67
                        break;
68
                    }
69
                }
70
                if (!loopAction)
71
                    break;
72
            }
73
            // Line Group Modeling 실패한 로그
74
            foreach (Group group in document.GROUPS)
75
            {
76
                foreach (SPPID_ITEM item in group.Items)
77
                {
78
                    Line line = item as Line;
79
                    if (line != null && line.SPPID_ITEM_OBJECT == null)
80
                    {
81
                        ProgressForm.IncreaseProgressStatus();
82
                    }
83
                }
84
            }
85
            ProgressForm.SetProgressStatusMax();
86
            ProgressForm.IncreaseProgressAll();
87
            System.Threading.Thread.Sleep(100);
88

    
89
            // SprecBreak Modeling
90
            ProgressForm.UpdateStatus("Spec Break Modeling");
91
            ProgressForm.SetProgressStatusValue(0);
92
            ProgressForm.SetProgressStatusMax(document.SPECBREAK.Count);
93
            foreach (SpecBreak item in document.SPECBREAK)
94
            {
95
                item.Modeling(document);
96
                item.SetAttribute(document);
97
                foreach (var result in item.SPPID_ITEM_OBJECT_LIST)
98
                {
99
                    LMLabelPersist label = result.Value;
100
                    if (label != null)
101
                    {
102
                        
103
                    }
104
                    else
105
                    {
106
                        
107
                    }
108
                }
109

    
110
                ProgressForm.IncreaseProgressStatus();
111
            }
112
            ProgressForm.SetProgressStatusMax();
113
            ProgressForm.IncreaseProgressAll();
114
            System.Threading.Thread.Sleep(100);
115

    
116

    
117
            // Text Modeling
118
            ProgressForm.UpdateStatus("Text Modeling");
119
            ProgressForm.SetProgressStatusValue(0);
120
            ProgressForm.SetProgressStatusMax(document.TEXTS.Count);
121
            foreach (Text text in document.TEXTS)
122
            {
123
                text.Modeling(document);
124
                if (text.SPPID_ITEM_OBJECT != null || text.IsAssociation)
125
                {
126

    
127
                }
128
                else
129
                {
130
                    
131
                }
132
                ProgressForm.IncreaseProgressStatus();
133
            }
134
            ProgressForm.SetProgressStatusMax();
135
            ProgressForm.IncreaseProgressAll();
136
            System.Threading.Thread.Sleep(100);
137

    
138

    
139
            // attribute
140
            ProgressForm.UpdateStatus("Input Symbol Attribute");
141
            ProgressForm.SetProgressStatusValue(0);
142
            ProgressForm.SetProgressStatusMax(document.SYMBOLS.Count);
143
            foreach (Symbol item in document.SYMBOLS)
144
            {
145
                if (item.SPPID_ITEM_OBJECT != null)
146
                {
147
                    item.SetAttribute(document);
148
                }
149

    
150
                ProgressForm.IncreaseProgressStatus();
151
            }
152
            ProgressForm.SetProgressStatusMax();
153
            ProgressForm.IncreaseProgressAll();
154
            System.Threading.Thread.Sleep(100);
155

    
156
            // Line Number Attribute Modeling
157
            ProgressForm.UpdateStatus("Input Line Number Attribute");
158
            ProgressForm.SetProgressStatusValue(0);
159
            ProgressForm.SetProgressStatusMax(document.LINENUMBERS.Count);
160
            foreach (LineNumber lineNumber in document.LINENUMBERS)
161
            {
162
                lineNumber.SetAttribute(document);
163

    
164
                ProgressForm.IncreaseProgressStatus();
165
            }
166
            ProgressForm.SetProgressStatusMax();
167
            ProgressForm.IncreaseProgressAll();
168
            System.Threading.Thread.Sleep(100);
169
            CloseOPCForm.Stop();
170
            
171
            //
172
            //Log.WriteLine(new ListBoxItem(string.Format("Time : {0}", ts), Color.Black), logListBox);
173
        }
174

    
175
        private bool IsPossibleDrawGroupLine(Group group)
176
        {
177
            foreach (SPPID_ITEM item in group.Items)
178
            {
179
                if (item.GetType() == typeof(Symbol))
180
                {
181
                    Symbol symbol = item as Symbol;
182
                    foreach (Connector connector in symbol.CONNECTORS)
183
                    {
184
                        SPPID_ITEM objItem = SPPIDUtill.FindObjectByUID(document, connector.CONNECTEDITEM) as SPPID_ITEM;
185
                        if (objItem != null && !group.Items.Contains(objItem) && objItem.SPPID_ITEM_OBJECT == null)
186
                            return false;
187
                    }
188
                }
189
                else if (item.GetType() == typeof(Line))
190
                {
191
                    Line line = item as Line;
192
                    foreach (Connector connector in line.CONNECTORS)
193
                    {
194
                        SPPID_ITEM objItem = SPPIDUtill.FindObjectByUID(document, connector.CONNECTEDITEM) as SPPID_ITEM;
195
                        if (objItem != null && !group.Items.Contains(objItem) && objItem.SPPID_ITEM_OBJECT == null)
196
                            return false;
197
                    }
198
                }
199
            }
200

    
201
            return true;
202
        }
203

    
204
        #region Modeling
205

    
206
        private bool GroupSymbolModeling(Group group)
207
        {
208
            List<object> Items = group.Items;
209
            Symbol prevSymbol = null;
210
            int prevIndex = 0;
211
            bool result = true;
212

    
213
            for (int itemIndex = 0; itemIndex < Items.Count(); itemIndex++)
214
            {
215
                Line.SlopeType prevSlope = Line.SlopeType.NONE;
216
                Symbol symbol = Items[itemIndex] as Symbol;
217
                try
218
                {
219
                    if (symbol != null)
220
                    {
221
                        ProgressForm.IncreaseProgressStatus();
222
                        // Group중에 최근에 Modeling된 Symbol
223
                        if (prevSymbol != null)
224
                        {
225
                            LMSymbol prevLMSymbol = prevSymbol.SPPID_ITEM_OBJECT as LMSymbol;
226
                            for (int prevLineIndex = prevIndex + 1; prevLineIndex < itemIndex; prevLineIndex++)
227
                            {
228
                                Line prevLine = Items[prevLineIndex] as Line;
229
                                if (prevLine != null)
230
                                {
231
                                    if (prevSlope == Line.SlopeType.NONE)
232
                                    {
233
                                        prevSlope = prevLine.SLOPTYPE;
234
                                    }
235
                                    else if (prevSlope != prevLine.SLOPTYPE)
236
                                    {
237
                                        prevSlope = Line.SlopeType.NONE;
238
                                        break;
239
                                    }
240
                                }
241
                            }
242

    
243
                            if (prevSlope == Line.SlopeType.HORIZONTAL)
244
                                symbol.LOCATION_Y = prevLMSymbol.get_YCoordinate();
245
                            else if (prevSlope == Line.SlopeType.VERTICAL)
246
                                symbol.LOCATION_X = prevLMSymbol.get_XCoordinate();
247
                        }
248

    
249
                        symbol.Modeling(document);
250
                        if (symbol.SPPID_ITEM_OBJECT != null)
251
                        {
252
                            prevSymbol = symbol;
253
                            prevIndex = itemIndex;
254
                        }
255
                        else
256
                        {
257
                            result = false;
258
                        }
259
                    }
260
                }
261
                catch (Exception ex)
262
                {
263
                    result = false;
264
                    return result;
265
                }
266
                finally
267
                {
268
                    
269
                }
270
            }
271

    
272
            return result;
273
        }
274

    
275
        private bool GroupLineModeling(Group group)
276
        {
277
            try
278
            {
279
                List<object> Items = group.Items;
280
                List<Line> lineList = new List<Line>();
281
                foreach (SPPID_ITEM item in Items)
282
                {
283
                    Line line = item as Line;
284
                    if (line != null)
285
                        lineList.Add(line);
286
                    else if (lineList.Count > 0)
287
                    {
288
                        GroupLineModelingByList(lineList, group);
289
                    }
290
                }
291

    
292
                if (lineList.Count > 0)
293
                    GroupLineModelingByList(lineList, group);
294
            }
295
            catch (Exception ex)
296
            {
297
                Log.WriteLine(ex);
298
                return false;
299
            }
300

    
301
            return true;
302
        }
303

    
304
        private void GroupLineModelingByList(List<Line> lines, Group group)
305
        {
306
            List<PointInfo> pointInfos = GetLinePointInfos(lines, group);
307
            LMConnector lMConnector = DrawLines(pointInfos, lines[0].SPPIDMAPPINGNAME, lines);
308

    
309
            if (lMConnector != null)
310
            {
311
                string modelId = lMConnector.ModelItemID;
312
                document.PipeRunPoint.Add(lMConnector.ModelItemID, pointInfos);
313
                foreach (Line line in lines)
314
                {
315
                    line.SPPID_ITEM_OBJECT = modelId;
316
                    IfContainLineNumber(line.UID);
317
                }
318
            }
319
            else
320
            {
321
                foreach (Line line in lines)
322
                {
323
                    //Log.WriteLine(new ListBoxItem(string.Format("Fail Line UID : {0}", line.UID), Color.Red), logListBox);
324
                    //Log.ProgressBarIncrement(progressBar);
325
                }
326
            }
327

    
328
            for (int i = 0; i < lines.Count; i++)
329
                ProgressForm.IncreaseProgressStatus();
330

    
331
            lines.Clear();
332
        }
333

    
334
        private void IfContainLineNumber(string lineUID)
335
        {
336
            foreach (LineNumber lineNumber in document.LINENUMBERS)
337
            {
338
                if (lineNumber.CONNECTLINE == lineUID)
339
                {
340
                    lineNumber.Modeling(document);
341

    
342
                    if (lineNumber.SPPID_ITEM_OBJECT != null)
343
                    {
344

    
345
                    }
346
                    else
347
                    {
348

    
349
                    }
350
                    break;
351
                }
352
            }
353
        }
354

    
355
        private LMConnector DrawLines(List<PointInfo> pointInfos, string SPPID_MAPPINGNAME, List<Line> lines)
356
        {
357
            LMConnector lMConnector = null;
358
            try
359
            {
360
                Placement _placement = new Placement();
361
                _LMAItem _LMAItem = _placement.PIDCreateItem(SPPID_MAPPINGNAME);
362
                PlaceRunInputs placeRunInputs = new PlaceRunInputs();
363

    
364
                PointInfo branchPoint1 = null;
365
                PointInfo branchPoint1_1 = null;
366
                int branchIndex1 = 0;
367
                List<string> branch_SP_ID_List1 = null;
368
                PointInfo branchPoint2 = null;
369
                PointInfo branchPoint2_1 = null;
370
                int branchIndex2 = 0;
371
                List<string> branch_SP_ID_List2 = null;
372

    
373

    
374
                for (int pointIndex = 0; pointIndex < pointInfos.Count; pointIndex++)
375
                {
376
                    PointInfo pointInfo = pointInfos[pointIndex];
377
                    if (pointIndex == 0 || pointIndex == pointInfos.Count - 1)
378
                    {
379
                        if (pointInfo.ConnectedItemObject == null)
380
                        {
381
                            placeRunInputs.AddPoint(pointInfo.X, pointInfo.Y);
382
                        }
383
                        else
384
                        {
385
                            if (typeof(Symbol) == pointInfo.ConnectedItemObject.GetType())
386
                            {
387
                                Symbol symbol = pointInfo.ConnectedItemObject as Symbol;
388
                                //check child
389
                                List<string> linesUID = new List<string>();
390
                                foreach (Line line in lines)
391
                                    linesUID.Add(line.UID);
392

    
393
                                int symbolIndex = 0;
394
                                foreach (Connector connector in symbol.CONNECTORS)
395
                                {
396
                                    if (linesUID.Contains(connector.CONNECTEDITEM))
397
                                    {
398
                                        symbolIndex = connector.INDEX;
399
                                        break;
400
                                    }
401
                                }
402
                                LMSymbol tartgetLMSymbol = null;
403
                                if (symbolIndex == 0)
404
                                    tartgetLMSymbol = symbol.SPPID_ITEM_OBJECT as LMSymbol;
405
                                else
406
                                {
407
                                    foreach (SymbolChild child in symbol.CHILD_LIST)
408
                                    {
409
                                        if (child.Index == symbolIndex)
410
                                        {
411
                                            tartgetLMSymbol = child.SPPID_ITEM_OBJECT as LMSymbol;
412
                                            break;
413
                                        }
414
                                    }
415
                                }
416
                                
417
                                placeRunInputs.AddSymbolTarget(tartgetLMSymbol, pointInfo.X, pointInfo.Y);
418
                                pointInfo.Type = PointInfo.PointType.Branch;
419
                            }
420
                            else if (typeof(Line) == pointInfo.ConnectedItemObject.GetType())
421
                            {
422
                                Line line = pointInfo.ConnectedItemObject as Line;
423
                                Tuple<LMConnector, int> targetLMConnector = GetTargetLMConnector(pointInfo);
424
                                if (targetLMConnector != null) 
425
                                {
426
                                    if (targetLMConnector.Item1 != null)
427
                                    {
428
                                        if (branchPoint1 == null)
429
                                        {
430
                                            placeRunInputs.AddConnectorTarget(targetLMConnector.Item1, pointInfo.X, pointInfo.Y);
431
                                            pointInfo.Type = PointInfo.PointType.Branch;
432
                                            branchPoint1 = new PointInfo(pointInfo.X, pointInfo.Y);
433
                                            branchPoint1.Type = PointInfo.PointType.Branch;
434
                                            branchPoint1.ConnectedItem = pointInfo.ConnectedItem;
435
                                            branchPoint1.ConnectedItemObject = pointInfo.ConnectedItemObject;
436
                                            branchIndex1 = targetLMConnector.Item2;
437
                                            branchPoint1_1 = pointInfo;
438

    
439
                                            branch_SP_ID_List1 = new List<string>();
440
                                            LMPipeRun lMPipeRun = _placement.PIDDataSource.GetPipeRun(line.SPPID_ITEM_OBJECT.ToString());
441
                                            foreach (LMRepresentation rep in lMPipeRun.Representations)
442
                                                if (rep.Attributes["RepresentationType"].get_Value() == "Branch" && rep.Attributes["ItemStatus"].get_Value() == "Active")
443
                                                    branch_SP_ID_List1.Add(rep.Id);
444
                                        }
445
                                        else
446
                                        {
447
                                            placeRunInputs.AddConnectorTarget(targetLMConnector.Item1, pointInfo.X, pointInfo.Y);
448
                                            pointInfo.Type = PointInfo.PointType.Branch;
449
                                            branchPoint2 = new PointInfo(pointInfo.X, pointInfo.Y);
450
                                            branchPoint2.Type = PointInfo.PointType.Branch;
451
                                            branchPoint2.ConnectedItem = pointInfo.ConnectedItem;
452
                                            branchPoint2.ConnectedItemObject = pointInfo.ConnectedItemObject;
453
                                            branchIndex2 = targetLMConnector.Item2;
454
                                            branchPoint2_1 = pointInfo;
455

    
456
                                            branch_SP_ID_List2 = new List<string>();
457
                                            LMPipeRun lMPipeRun = _placement.PIDDataSource.GetPipeRun(line.SPPID_ITEM_OBJECT.ToString());
458
                                            foreach (LMRepresentation rep in lMPipeRun.Representations)
459
                                                if (rep.Attributes["RepresentationType"].get_Value() == "Branch" && rep.Attributes["ItemStatus"].get_Value() == "Active")
460
                                                    branch_SP_ID_List2.Add(rep.Id);
461
                                        }
462
                                    }
463
                                    else
464
                                    {
465
                                        placeRunInputs.AddPoint(pointInfo.X, pointInfo.Y);
466
                                        pointInfo.Type = PointInfo.PointType.Run;
467
                                    }
468
                                }
469
                                else
470
                                {
471
                                    placeRunInputs.AddPoint(pointInfo.X, pointInfo.Y);
472
                                    pointInfo.Type = PointInfo.PointType.Run;
473
                                }
474
                            }
475
                        }
476
                    }
477
                    else
478
                    {
479
                        placeRunInputs.AddPoint(pointInfo.X, pointInfo.Y);
480
                        pointInfo.Type = PointInfo.PointType.Run;
481
                    }
482
                }
483

    
484
                lMConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
485
                lMConnector.Commit();
486

    
487
                // Branch Point Setting
488
                if (branchPoint1 != null)
489
                {
490
                    Line branchedLine = branchPoint1.ConnectedItemObject as Line;
491
                    LMPipeRun lMPipeRun = _placement.PIDDataSource.GetPipeRun(branchedLine.SPPID_ITEM_OBJECT.ToString());
492
                    foreach (LMRepresentation rep in lMPipeRun.Representations)
493
                        if (rep.Attributes["RepresentationType"].get_Value() == "Branch" && rep.Attributes["ItemStatus"].get_Value() == "Active")
494
                        {
495
                            if (!branch_SP_ID_List1.Contains(rep.Id))
496
                            {
497
                                branchPoint1.SP_ID = rep.Id;
498
                                branchPoint1_1.SP_ID = rep.Id;
499
                                document.PipeRunPoint[branchedLine.SPPID_ITEM_OBJECT.ToString()].Insert(branchIndex1, branchPoint1);
500
                                break;
501
                            }
502
                        }
503

    
504
                    if (string.IsNullOrEmpty(branchPoint1.SP_ID))
505
                    {
506
                        document.PipeRunPoint[branchedLine.SPPID_ITEM_OBJECT.ToString()].RemoveAt(branchIndex1);
507
                        branchPoint1_1.Type = PointInfo.PointType.Run;
508
                    }
509
                }
510
                if (branchPoint2 != null)
511
                {
512
                    Line branchedLine = branchPoint2.ConnectedItemObject as Line;
513
                    LMPipeRun lMPipeRun = _placement.PIDDataSource.GetPipeRun(branchedLine.SPPID_ITEM_OBJECT.ToString());
514
                    foreach (LMRepresentation rep in lMPipeRun.Representations)
515
                        if (rep.Attributes["RepresentationType"].get_Value() == "Branch" && rep.Attributes["ItemStatus"].get_Value() == "Active")
516
                        {
517
                            if (!branch_SP_ID_List2.Contains(rep.Id))
518
                            {
519
                                branchPoint2.SP_ID = rep.Id;
520
                                branchPoint2_1.SP_ID = rep.Id;
521
                                document.PipeRunPoint[branchedLine.SPPID_ITEM_OBJECT.ToString()].Insert(branchIndex2, branchPoint2);
522
                                break;
523
                            }
524
                        }
525

    
526
                    if (string.IsNullOrEmpty(branchPoint2.SP_ID))
527
                    {
528
                        document.PipeRunPoint[branchedLine.SPPID_ITEM_OBJECT.ToString()].RemoveAt(branchIndex2);
529
                        branchPoint2_1.Type = PointInfo.PointType.Run;
530
                    }
531
                }
532
                // End
533
            }
534
            catch (Exception ex)
535
            {
536
                Log.WriteLine(ex);
537
            }
538

    
539
            return lMConnector;
540
        }
541

    
542
        private List<PointInfo> GetLinePointInfos(List<Line> lineList, Group group)
543
        {
544
            // Line들의 points를 구함
545
            List<PointInfo> pointInfos = new List<PointInfo>();
546
            if (lineList.Count == 1)
547
            {
548
                pointInfos.Add(new PointInfo(lineList[0].START_X, lineList[0].START_Y) { ConnectedItem = lineList[0].CONNECTORS[0].CONNECTEDITEM });
549
                pointInfos.Add(new PointInfo(lineList[0].END_X, lineList[0].END_Y) { ConnectedItem = lineList[0].CONNECTORS[1].CONNECTEDITEM });
550
            }
551
            else
552
            {
553
                for (int i = 0; i < lineList.Count; i++)
554
                    if (lineList.Count > i + 1)
555
                        GetLinePoints(lineList[i], lineList[i + 1], pointInfos);
556
            }
557

    
558
            // 맨앞 뒤의 ConnectedItem 구함
559
            for (int i = 0; i < pointInfos.Count; i++)
560
            {
561
                PointInfo info = pointInfos[i];
562
                if (i == 0 || pointInfos.Count - 1 == i)
563
                {
564
                    info.ConnectedItemObject = SPPIDUtill.FindObjectByUID(document, info.ConnectedItem) as SPPID_ITEM;
565

    
566
                    if (info.ConnectedItemObject == null)
567
                    {
568
                        info.Type = PointInfo.PointType.Run;
569
                    }
570
                    else
571
                    {
572
                        if (info.ConnectedItemObject.GetType() == typeof(Symbol))
573
                        {
574
                            Symbol symbol = info.ConnectedItemObject as Symbol;
575
                            LMSymbol _lmSymbol = symbol.SPPID_ITEM_OBJECT as LMSymbol;
576
                            info.SP_ID = _lmSymbol.AsLMRepresentation().Id;
577
                        }
578
                        info.Type = PointInfo.PointType.Branch;
579
                    }
580
                }
581
                else
582
                {
583
                    info.ConnectedItem = string.Empty;
584
                    info.Type = PointInfo.PointType.Run;
585
                }
586
                    
587
            }
588

    
589
            // 라인 보정
590
            CalibratePointInfos(pointInfos, lineList);
591

    
592
            return pointInfos;
593
        }
594

    
595
        private void GetLinePoints(Line line, Line nextLine, List<PointInfo> pointInfos)
596
        {
597
            if (nextLine != null)
598
            {
599
                if (pointInfos.Count > 0)
600
                    pointInfos.RemoveRange(pointInfos.Count - 2, 2);
601

    
602
                if (line.CONNECTORS[0].CONNECTEDITEM == nextLine.UID)
603
                {
604
                    pointInfos.Add(new PointInfo(line.END_X, line.END_Y) { ConnectedItem = line.CONNECTORS[1].CONNECTEDITEM });
605
                    pointInfos.Add(new PointInfo(line.START_X, line.START_Y) { ConnectedItem = line.CONNECTORS[0].CONNECTEDITEM });
606

    
607
                    if (nextLine.CONNECTORS[0].CONNECTEDITEM == line.UID)
608
                        pointInfos.Add(new PointInfo(nextLine.END_X, nextLine.END_Y) { ConnectedItem = nextLine.CONNECTORS[1].CONNECTEDITEM });
609
                    else if (nextLine.CONNECTORS[1].CONNECTEDITEM == line.UID)
610
                        pointInfos.Add(new PointInfo(nextLine.START_X, nextLine.START_Y) { ConnectedItem = nextLine.CONNECTORS[0].CONNECTEDITEM });
611

    
612
                }
613
                else if (line.CONNECTORS[1].CONNECTEDITEM == nextLine.UID)
614
                {
615
                    pointInfos.Add(new PointInfo(line.START_X, line.START_Y) { ConnectedItem = line.CONNECTORS[0].CONNECTEDITEM });
616
                    pointInfos.Add(new PointInfo(line.END_X, line.END_Y) { ConnectedItem = line.CONNECTORS[1].CONNECTEDITEM });
617

    
618
                    if (nextLine.CONNECTORS[0].CONNECTEDITEM == line.UID)
619
                        pointInfos.Add(new PointInfo(nextLine.END_X, nextLine.END_Y) { ConnectedItem = nextLine.CONNECTORS[1].CONNECTEDITEM });
620
                    else if (nextLine.CONNECTORS[1].CONNECTEDITEM == line.UID)
621
                        pointInfos.Add(new PointInfo(nextLine.START_X, nextLine.START_Y) { ConnectedItem = nextLine.CONNECTORS[0].CONNECTEDITEM });
622
                }
623
            }
624
        }
625

    
626
        private void CalibratePointInfos(List<PointInfo> pointInfos, List<Line> lines)
627
        {
628
            // 맨 앞이 Symbol일 경우
629
            if (pointInfos[0].ConnectedItemObject != null && typeof(Symbol) == pointInfos[0].ConnectedItemObject.GetType())
630
            {
631
                PointInfo startPoint = pointInfos[0];
632
                PointInfo nextPoint = pointInfos[1];
633

    
634
                Symbol symbol = startPoint.ConnectedItemObject as Symbol;
635
                LMSymbol _LMSymbol = symbol.SPPID_ITEM_OBJECT as LMSymbol;
636
                double symbolX = _LMSymbol.get_XCoordinate();
637
                double symbolY = _LMSymbol.get_YCoordinate();
638

    
639
                if (lines[0].SLOPTYPE == Line.SlopeType.HORIZONTAL)
640
                {
641
                    startPoint.Y = symbolY;
642
                    nextPoint.Y = symbolY;
643
                }
644
                else if (lines[0].SLOPTYPE == Line.SlopeType.VERTICAL)
645
                {
646
                    startPoint.X = symbolX;
647
                    nextPoint.X = symbolX;
648
                }
649
            }
650
            // 맨 뒤가 Symbol일 경우
651
            else if (pointInfos[pointInfos.Count - 1].ConnectedItemObject != null && typeof(Symbol) == pointInfos[pointInfos.Count - 1].ConnectedItemObject.GetType())
652
            {
653
                PointInfo startPoint = pointInfos[pointInfos.Count - 1];
654
                PointInfo nextPoint = pointInfos[pointInfos.Count - 2];
655

    
656
                Symbol symbol = startPoint.ConnectedItemObject as Symbol;
657
                LMSymbol _LMSymbol = symbol.SPPID_ITEM_OBJECT as LMSymbol;
658
                double symbolX = _LMSymbol.get_XCoordinate();
659
                double symbolY = _LMSymbol.get_YCoordinate();
660

    
661
                if (lines[lines.Count - 1].SLOPTYPE == Line.SlopeType.HORIZONTAL)
662
                {
663
                    startPoint.Y = symbolY;
664
                    nextPoint.Y = symbolY;
665
                }
666
                else if (lines[lines.Count - 1].SLOPTYPE == Line.SlopeType.VERTICAL)
667
                {
668
                    startPoint.X = symbolX;
669
                    nextPoint.X = symbolX;
670
                }
671
            }
672
        }
673

    
674
        private Tuple<LMConnector, int> GetTargetLMConnector(PointInfo pointInfo)
675
        {
676
            LMConnector lMConnector = null;
677
            int insertIndex = 0;
678
            try
679
            {
680
                Line line = pointInfo.ConnectedItemObject as Line;
681
                if (line.SPPID_ITEM_OBJECT == null)
682
                    return null;
683

    
684
                string modelId = line.SPPID_ITEM_OBJECT.ToString();
685
                List<PointInfo> pointInfos = document.PipeRunPoint[modelId];
686
                double distance = double.MaxValue;
687

    
688
                PointInfo point1 = new PointInfo(0, 0);
689
                PointInfo point2 = new PointInfo(0, 0);
690

    
691
                for (int i = 0; i < pointInfos.Count - 1; i++)
692
                {
693
                    PointInfo tempPoint1 = pointInfos[i];
694
                    PointInfo tempPoint2 = pointInfos[i + 1];
695

    
696
                    double tempDistance = double.MaxValue;
697

    
698
                    Line.SlopeType slopeType = SPPIDUtill.CalcSlop(tempPoint1, tempPoint2);
699
                    if (slopeType == Line.SlopeType.HORIZONTAL)
700
                    {
701
                        double min = Math.Min(tempPoint1.X, tempPoint2.X);
702
                        double max = Math.Max(tempPoint1.X, tempPoint2.X);
703

    
704
                        if (min <= pointInfo.X && max >= pointInfo.X)
705
                            tempDistance = SPPIDUtill.CalcLineToPointDistance(tempPoint1, tempPoint2, pointInfo);
706
                    }
707
                    else if (slopeType == Line.SlopeType.VERTICAL)
708
                    {
709
                        double min = Math.Min(tempPoint1.Y, tempPoint2.Y);
710
                        double max = Math.Max(tempPoint1.Y, tempPoint2.Y);
711

    
712
                        if (min <= pointInfo.Y && max >= pointInfo.Y)
713
                            tempDistance = SPPIDUtill.CalcLineToPointDistance(tempPoint1, tempPoint2, pointInfo);
714
                    }
715
                    else
716
                    {
717
                        tempDistance = SPPIDUtill.CalcLineToPointDistance(tempPoint1, tempPoint2, pointInfo);
718
                    }
719

    
720
                    if (tempDistance < distance)
721
                    {
722
                        insertIndex = i + 1;
723
                        distance = tempDistance;
724
                        point1 = tempPoint1;
725
                        point2 = tempPoint2;
726
                    }
727
                }
728

    
729
                if (point1.Type == PointInfo.PointType.Run)
730
                    lMConnector = GetLMConnectorByPoint(modelId, point1);
731
                else if (point2.Type == PointInfo.PointType.Run)
732
                    lMConnector = GetLMConnectorByPoint(modelId, point2);
733
                else
734
                    lMConnector = GetLMConnectorBySP_ID(modelId, point1.SP_ID, point2.SP_ID);
735
            }
736
            catch (Exception ex)
737
            {
738
                Log.WriteLine(ex);
739
                return null;
740
            }
741

    
742
            if (lMConnector == null)
743
                return null;
744
            else
745
                return new Tuple<LMConnector, int>(lMConnector, insertIndex);
746
        }
747

    
748
        private LMConnector GetLMConnectorByPoint(string modelId, PointInfo pointInfo)
749
        {
750
            LMConnector lMConnector = null;
751
            
752
            try
753
            {
754
                Placement _placement = new Placement();
755
                double distance = double.MaxValue;
756
                LMPipeRun lMPipeRun = _placement.PIDDataSource.GetPipeRun(modelId);
757
                if (lMPipeRun != null)
758
                {
759
                    foreach (LMRepresentation rep in lMPipeRun.Representations)
760
                    {
761
                        if (rep.Attributes["RepresentationType"].get_Value() == "Connector" && rep.Attributes["ItemStatus"].get_Value() == "Active")
762
                        {
763
                            LMConnector _tempLMConnector = _placement.PIDDataSource.GetConnector(rep.Id);
764
                            foreach (LMConnectorVertex vertex in _tempLMConnector.ConnectorVertices)
765
                            {
766
                                double tempDistance = SPPIDUtill.CalcPointToPointdDistance(pointInfo, new PointInfo(vertex.get_XCoordinate(), vertex.get_YCoordinate()));
767

    
768
                                if (tempDistance < distance)
769
                                {
770
                                    distance = tempDistance;
771
                                    lMConnector = _tempLMConnector;
772
                                }
773
                            }
774

    
775
                        }
776
                    }
777
                }
778
            }
779
            catch (Exception ex)
780
            {
781
                Log.WriteLine(ex);
782
            }
783
            
784
            
785
            return lMConnector;
786
        }
787

    
788
        private LMConnector GetLMConnectorBySP_ID(string modelId, string SP_ID1, string SP_ID2)
789
        {
790
            Placement _placement = new Placement();
791

    
792
            LMPipeRun lMPipeRun = _placement.PIDDataSource.GetPipeRun(modelId);
793
            foreach (LMRepresentation rep in lMPipeRun.Representations)
794
            {
795
                if (rep.Attributes["RepresentationType"].get_Value() == "Connector" && rep.Attributes["ItemStatus"].get_Value() == "Active")
796
                {
797
                    LMConnector _tempLMConnector = _placement.PIDDataSource.GetConnector(rep.Id);
798

    
799
                    bool find1 = false;
800
                    bool find2 = false;
801

    
802
                    if (_tempLMConnector.ConnectItem1SymbolID != null && !DBNull.Value.Equals(_tempLMConnector.ConnectItem1SymbolID))
803
                    {
804
                        string connId = _tempLMConnector.ConnectItem1SymbolID;
805
                        if (connId == SP_ID1 || connId == SP_ID2)
806
                            find1 = true;
807
                    }
808

    
809
                    if (_tempLMConnector.ConnectItem2SymbolID != null && !DBNull.Value.Equals(_tempLMConnector.ConnectItem2SymbolID))
810
                    {
811
                        string connId = _tempLMConnector.ConnectItem2SymbolID;
812
                        if (connId == SP_ID1 || connId == SP_ID2)
813
                            find2 = true;
814
                    }
815

    
816

    
817
                    if (find1 && find2)
818
                    {
819
                        return _tempLMConnector;
820
                    }
821
                }
822
            }
823
            return null;
824
        }
825

    
826
        #endregion
827

    
828
    }
829
}
클립보드 이미지 추가 (최대 크기: 500 MB)