프로젝트

일반

사용자정보

통계
| 개정판:

hytos / DTI_PID / SPPIDConverter_AutoModeling / Modeling / AutoModeling.cs @ 11ff136b

이력 | 보기 | 이력해설 | 다운로드 (35.4 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

    
15

    
16
namespace SPPID.Modeling
17
{
18
    public class AutoModeling
19
    {
20
        private Document document;
21
        private ListBox logListBox;
22
        private ToolStripProgressBar progressBar;
23

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

    
29
        public void Run()
30
        {
31
            if (progressBar != null)
32
            {
33
                Log.SetProgressBarValue(0, progressBar);
34
                Log.SetProgressMaximumValue(document.SYMBOLS.Count + document.LINES.Count + document.LINENUMBERS.Count + document.TEXTS.Count + document.SPECBREAK.Count, progressBar);
35
            }
36

    
37
            Log.WriteLine(new ListBoxItem(string.Format("----- START [{0}] -----", document.DWGNAME), Color.Black), logListBox);
38
            Stopwatch sw = new Stopwatch();
39
            sw.Start();
40
            CloseOPCForm.Run();
41

    
42
            // Group Symbol 실패시 Group Remove
43
            foreach (Group group in document.GROUPS)
44
            {
45
                if (!GroupSymbolModeling(group))
46
                {
47
                    group.Enable = false;
48
                }
49
            }
50

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

    
60
                    if (IsPossibleDrawGroupLine(group))
61
                    {
62
                        loopAction = true;
63
                        GroupLineModeling(group);
64
                        document.GROUPS.Remove(group);
65
                        break;
66
                    }
67
                }
68
                if (!loopAction)
69
                    break;
70
            }
71
            // Line Group Modeling 실패한 로그
72
            foreach (Group group in document.GROUPS)
73
            {
74
                foreach (SPPID_ITEM item in group.Items)
75
                {
76
                    Line line = item as Line;
77
                    if (line != null && line.SPPID_ITEM_OBJECT == null)
78
                    {
79
                        Log.WriteLine(new ListBoxItem(string.Format("Fail Line UID : {0}", line.UID), Color.Red), logListBox);
80
                        Log.ProgressBarIncrement(progressBar);
81
                    }
82
                }
83
            }
84

    
85
            foreach (SpecBreak item in document.SPECBREAK)
86
            {
87
                item.Modeling(document);
88
                item.SetAttribute(document);
89
                foreach (var result in item.SPPID_ITEM_OBJECT_LIST)
90
                {
91
                    LMLabelPersist label = result.Value;
92
                    if (label != null)
93
                    {
94
                        Log.WriteLine(new ListBoxItem(string.Format("Create SpecBreak UID : {0}, Type : {1}", item.UID, result.Key), Color.Black), logListBox);
95
                    }
96
                    else
97
                    {
98
                        Log.WriteLine(new ListBoxItem(string.Format("Fail SpecBreak UID : {0}, Type : {1}", item.UID, result.Key), Color.Red), logListBox);
99
                    }
100
                }
101

    
102
                Log.ProgressBarIncrement(progressBar);
103
            }
104

    
105
            foreach (LineNumber lineNumber in document.LINENUMBERS)
106
            {
107
                lineNumber.SetAttribute(document);
108
            }
109

    
110
            // Text Modeling
111
            foreach (Text text in document.TEXTS)
112
            {
113
                text.Modeling(document);
114
                if (text.SPPID_ITEM_OBJECT != null || text.IsAssociation)
115
                    Log.WriteLine(new ListBoxItem(string.Format("Create Text UID : {0}", text.UID), Color.Black), logListBox);
116
                else
117
                {
118
                    Log.WriteLine(new ListBoxItem(string.Format("Fail Text UID : {0}", text.UID), Color.Red), logListBox);
119
                    Log.WriteLine(text.VALUE + "/" + text.SPPIDMAPPINGNAME);
120
                }
121

    
122
                Log.ProgressBarIncrement(progressBar);
123
            }
124

    
125
            if (progressBar != null)
126
            {
127
                Log.SetProgressBarValue(0, progressBar);
128
                Log.SetProgressMaximumValue(document.SYMBOLS.Count, progressBar);
129
            }
130
            // attribute
131
            foreach (Symbol item in document.SYMBOLS)
132
            {
133
                if (item.SPPID_ITEM_OBJECT != null)
134
                {
135
                    item.SetAttribute(document);
136
                }
137
                Log.ProgressBarIncrement(progressBar);
138
            }
139

    
140

    
141
            CloseOPCForm.Stop();
142
            //
143
            Log.SetProgressBarValue(progressBar.Maximum, progressBar);
144

    
145
            Log.WriteLine(new ListBoxItem(string.Format("----- END [{0}] -----", document.DWGNAME), Color.Black), logListBox);
146
            sw.Stop();
147
            TimeSpan ts = sw.Elapsed;
148
            string elapsedTime = String.Format("{0:00}:{1:00}.{2:00}",
149
            ts.Minutes, ts.Seconds,
150
            ts.Milliseconds / 10);
151
            Log.WriteLine(new ListBoxItem(string.Format("Time : {0}", ts), Color.Black), logListBox);
152
            Log.WriteLine(new ListBoxItem("", Color.Black), logListBox);
153
        }
154

    
155
        private bool IsPossibleDrawGroupLine(Group group)
156
        {
157
            foreach (SPPID_ITEM item in group.Items)
158
            {
159
                if (item.GetType() == typeof(Symbol))
160
                {
161
                    Symbol symbol = item as Symbol;
162
                    foreach (Connector connector in symbol.CONNECTORS)
163
                    {
164
                        SPPID_ITEM objItem = SPPIDUtill.FindObjectByUID(document, connector.CONNECTEDITEM) as SPPID_ITEM;
165
                        if (objItem != null && !group.Items.Contains(objItem) && objItem.SPPID_ITEM_OBJECT == null)
166
                            return false;
167
                    }
168
                }
169
                else if (item.GetType() == typeof(Line))
170
                {
171
                    Line line = item as Line;
172
                    foreach (Connector connector in line.CONNECTORS)
173
                    {
174
                        SPPID_ITEM objItem = SPPIDUtill.FindObjectByUID(document, connector.CONNECTEDITEM) as SPPID_ITEM;
175
                        if (objItem != null && !group.Items.Contains(objItem) && objItem.SPPID_ITEM_OBJECT == null)
176
                            return false;
177
                    }
178
                }
179
            }
180

    
181
            return true;
182
        }
183

    
184
        public void SetListBoxLog(ListBox logListBox)
185
        {
186
            this.logListBox = logListBox;
187
        }
188

    
189
        public void SetProgressBar(ToolStripProgressBar progressBar)
190
        {
191
            this.progressBar = progressBar;
192
        }
193

    
194
        #region Modeling
195

    
196
        private bool GroupSymbolModeling(Group group)
197
        {
198
            List<object> Items = group.Items;
199
            Symbol prevSymbol = null;
200
            int prevIndex = 0;
201
            bool result = true;
202

    
203
            for (int itemIndex = 0; itemIndex < Items.Count(); itemIndex++)
204
            {
205
                Line.SlopeType prevSlope = Line.SlopeType.NONE;
206
                Symbol symbol = Items[itemIndex] as Symbol;
207
                try
208
                {
209
                    if (symbol != null)
210
                    {
211
                        // Group중에 최근에 Modeling된 Symbol
212
                        if (prevSymbol != null)
213
                        {
214
                            LMSymbol prevLMSymbol = prevSymbol.SPPID_ITEM_OBJECT as LMSymbol;
215
                            for (int prevLineIndex = prevIndex + 1; prevLineIndex < itemIndex; prevLineIndex++)
216
                            {
217
                                Line prevLine = Items[prevLineIndex] as Line;
218
                                if (prevLine != null)
219
                                {
220
                                    if (prevSlope == Line.SlopeType.NONE)
221
                                    {
222
                                        prevSlope = prevLine.SLOPTYPE;
223
                                    }
224
                                    else if (prevSlope != prevLine.SLOPTYPE)
225
                                    {
226
                                        prevSlope = Line.SlopeType.NONE;
227
                                        break;
228
                                    }
229
                                }
230
                            }
231

    
232
                            if (prevSlope == Line.SlopeType.HORIZONTAL)
233
                                symbol.LOCATION_Y = prevLMSymbol.get_YCoordinate();
234
                            else if (prevSlope == Line.SlopeType.VERTICAL)
235
                                symbol.LOCATION_X = prevLMSymbol.get_XCoordinate();
236
                        }
237

    
238
                        symbol.Modeling(document);
239
                        if (symbol.SPPID_ITEM_OBJECT != null)
240
                        {
241
                            prevSymbol = symbol;
242
                            prevIndex = itemIndex;
243
                            Log.WriteLine(new ListBoxItem(string.Format("Create Symbol UID : {0}", symbol.UID), Color.Black), logListBox);
244
                            Log.ProgressBarIncrement(progressBar);
245
                        }
246
                        else
247
                        {
248
                            Log.WriteLine(new ListBoxItem(string.Format("Fail Symbol UID : {0}", symbol.UID), Color.Red), logListBox);
249
                            Log.ProgressBarIncrement(progressBar);
250
                            result = false;
251
                        }
252
                    }
253
                }
254
                catch (Exception ex)
255
                {
256
                    Log.WriteLine(new ListBoxItem(string.Format("Fail Symbol UID : {0}", symbol.UID), Color.Red), logListBox);
257
                    Log.ProgressBarIncrement(progressBar);
258
                    Log.WriteLine(ex);
259
                    result = false;
260
                    return result;
261
                }
262

    
263
                
264
            }
265

    
266
            return result;
267
        }
268

    
269
        private bool GroupLineModeling(Group group)
270
        {
271
            try
272
            {
273
                List<object> Items = group.Items;
274
                List<Line> lineList = new List<Line>();
275
                foreach (SPPID_ITEM item in Items)
276
                {
277
                    Line line = item as Line;
278
                    if (line != null)
279
                        lineList.Add(line);
280
                    else if (lineList.Count > 0)
281
                    {
282
                        GroupLineModelingByList(lineList, group);
283
                    }
284
                }
285

    
286
                if (lineList.Count > 0)
287
                    GroupLineModelingByList(lineList, group);
288
            }
289
            catch (Exception ex)
290
            {
291
                Log.WriteLine(ex);
292
                return false;
293
            }
294

    
295
            return true;
296
        }
297

    
298
        private void GroupLineModelingByList(List<Line> lines, Group group)
299
        {
300
            List<PointInfo> pointInfos = GetLinePointInfos(lines, group);
301
            LMConnector lMConnector = DrawLines(pointInfos, lines[0].SPPIDMAPPINGNAME, lines);
302

    
303
            if (lMConnector != null)
304
            {
305
                string modelId = lMConnector.ModelItemID;
306
                document.PipeRunPoint.Add(lMConnector.ModelItemID, pointInfos);
307
                foreach (Line line in lines)
308
                {
309
                    line.SPPID_ITEM_OBJECT = modelId;
310
                    IfContainLineNumber(line.UID);
311

    
312
                    Log.WriteLine(new ListBoxItem(string.Format("Create Line UID : {0}", line.UID), Color.Black), logListBox);
313
                    Log.ProgressBarIncrement(progressBar);
314
                }
315
            }
316
            else
317
            {
318
                foreach (Line line in lines)
319
                {
320
                    Log.WriteLine(new ListBoxItem(string.Format("Fail Line UID : {0}", line.UID), Color.Red), logListBox);
321
                    Log.ProgressBarIncrement(progressBar);
322
                }
323
            }
324

    
325
            lines.Clear();
326
        }
327

    
328
        private void IfContainLineNumber(string lineUID)
329
        {
330
            foreach (LineNumber lineNumber in document.LINENUMBERS)
331
            {
332
                if (lineNumber.CONNECTLINE == lineUID)
333
                {
334
                    lineNumber.Modeling(document);
335

    
336
                    if (lineNumber.SPPID_ITEM_OBJECT != null)
337
                        Log.WriteLine(new ListBoxItem(string.Format("Create Line Number UID : {0}", lineNumber.UID), Color.Black), logListBox);
338
                    else
339
                        Log.WriteLine(new ListBoxItem(string.Format("Fail Line Number UID : {0}", lineNumber.UID), Color.Red), logListBox);
340

    
341
                    Log.ProgressBarIncrement(progressBar);
342
                    break;
343
                }
344
            }
345
        }
346

    
347
        private LMConnector DrawLines(List<PointInfo> pointInfos, string SPPID_MAPPINGNAME, List<Line> lines)
348
        {
349
            LMConnector lMConnector = null;
350
            try
351
            {
352
                Placement _placement = new Placement();
353
                _LMAItem _LMAItem = _placement.PIDCreateItem(SPPID_MAPPINGNAME);
354
                PlaceRunInputs placeRunInputs = new PlaceRunInputs();
355

    
356
                PointInfo branchPoint1 = null;
357
                PointInfo branchPoint1_1 = null;
358
                int branchIndex1 = 0;
359
                List<string> branch_SP_ID_List1 = null;
360
                PointInfo branchPoint2 = null;
361
                PointInfo branchPoint2_1 = null;
362
                int branchIndex2 = 0;
363
                List<string> branch_SP_ID_List2 = null;
364

    
365

    
366
                for (int pointIndex = 0; pointIndex < pointInfos.Count; pointIndex++)
367
                {
368
                    PointInfo pointInfo = pointInfos[pointIndex];
369
                    if (pointIndex == 0 || pointIndex == pointInfos.Count - 1)
370
                    {
371
                        if (pointInfo.ConnectedItemObject == null)
372
                        {
373
                            placeRunInputs.AddPoint(pointInfo.X, pointInfo.Y);
374
                        }
375
                        else
376
                        {
377
                            if (typeof(Symbol) == pointInfo.ConnectedItemObject.GetType())
378
                            {
379
                                Symbol symbol = pointInfo.ConnectedItemObject as Symbol;
380
                                //check child
381
                                List<string> linesUID = new List<string>();
382
                                foreach (Line line in lines)
383
                                    linesUID.Add(line.UID);
384

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

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

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

    
476
                lMConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
477
                lMConnector.Commit();
478

    
479
                // Branch Point Setting
480
                if (branchPoint1 != null)
481
                {
482
                    Line branchedLine = branchPoint1.ConnectedItemObject as Line;
483
                    LMPipeRun lMPipeRun = _placement.PIDDataSource.GetPipeRun(branchedLine.SPPID_ITEM_OBJECT.ToString());
484
                    foreach (LMRepresentation rep in lMPipeRun.Representations)
485
                        if (rep.Attributes["RepresentationType"].get_Value() == "Branch" && rep.Attributes["ItemStatus"].get_Value() == "Active")
486
                        {
487
                            if (!branch_SP_ID_List1.Contains(rep.Id))
488
                            {
489
                                branchPoint1.SP_ID = rep.Id;
490
                                branchPoint1_1.SP_ID = rep.Id;
491
                                document.PipeRunPoint[branchedLine.SPPID_ITEM_OBJECT.ToString()].Insert(branchIndex1, branchPoint1);
492
                                break;
493
                            }
494
                        }
495

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

    
518
                    if (string.IsNullOrEmpty(branchPoint2.SP_ID))
519
                    {
520
                        document.PipeRunPoint[branchedLine.SPPID_ITEM_OBJECT.ToString()].RemoveAt(branchIndex2);
521
                        branchPoint2_1.Type = PointInfo.PointType.Run;
522
                    }
523
                }
524
                // End
525
            }
526
            catch (Exception ex)
527
            {
528
                Log.WriteLine(ex);
529
            }
530

    
531
            return lMConnector;
532
        }
533

    
534
        private List<PointInfo> GetLinePointInfos(List<Line> lineList, Group group)
535
        {
536
            // Line들의 points를 구함
537
            List<PointInfo> pointInfos = new List<PointInfo>();
538
            if (lineList.Count == 1)
539
            {
540
                pointInfos.Add(new PointInfo(lineList[0].START_X, lineList[0].START_Y) { ConnectedItem = lineList[0].CONNECTORS[0].CONNECTEDITEM });
541
                pointInfos.Add(new PointInfo(lineList[0].END_X, lineList[0].END_Y) { ConnectedItem = lineList[0].CONNECTORS[1].CONNECTEDITEM });
542
            }
543
            else
544
            {
545
                for (int i = 0; i < lineList.Count; i++)
546
                    if (lineList.Count > i + 1)
547
                        GetLinePoints(lineList[i], lineList[i + 1], pointInfos);
548
            }
549

    
550
            // 맨앞 뒤의 ConnectedItem 구함
551
            for (int i = 0; i < pointInfos.Count; i++)
552
            {
553
                PointInfo info = pointInfos[i];
554
                if (i == 0 || pointInfos.Count - 1 == i)
555
                {
556
                    info.ConnectedItemObject = SPPIDUtill.FindObjectByUID(document, info.ConnectedItem) as SPPID_ITEM;
557

    
558
                    if (info.ConnectedItemObject == null)
559
                    {
560
                        info.Type = PointInfo.PointType.Run;
561
                    }
562
                    else
563
                    {
564
                        if (info.ConnectedItemObject.GetType() == typeof(Symbol))
565
                        {
566
                            Symbol symbol = info.ConnectedItemObject as Symbol;
567
                            LMSymbol _lmSymbol = symbol.SPPID_ITEM_OBJECT as LMSymbol;
568
                            info.SP_ID = _lmSymbol.AsLMRepresentation().Id;
569
                        }
570
                        info.Type = PointInfo.PointType.Branch;
571
                    }
572
                }
573
                else
574
                {
575
                    info.ConnectedItem = string.Empty;
576
                    info.Type = PointInfo.PointType.Run;
577
                }
578
                    
579
            }
580

    
581
            // 라인 보정
582
            CalibratePointInfos(pointInfos, lineList);
583

    
584
            return pointInfos;
585
        }
586

    
587
        private void GetLinePoints(Line line, Line nextLine, List<PointInfo> pointInfos)
588
        {
589
            if (nextLine != null)
590
            {
591
                if (pointInfos.Count > 0)
592
                    pointInfos.RemoveRange(pointInfos.Count - 2, 2);
593

    
594
                if (line.CONNECTORS[0].CONNECTEDITEM == nextLine.UID)
595
                {
596
                    pointInfos.Add(new PointInfo(line.END_X, line.END_Y) { ConnectedItem = line.CONNECTORS[1].CONNECTEDITEM });
597
                    pointInfos.Add(new PointInfo(line.START_X, line.START_Y) { ConnectedItem = line.CONNECTORS[0].CONNECTEDITEM });
598

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

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

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

    
618
        private void CalibratePointInfos(List<PointInfo> pointInfos, List<Line> lines)
619
        {
620
            // 맨 앞이 Symbol일 경우
621
            if (pointInfos[0].ConnectedItemObject != null && typeof(Symbol) == pointInfos[0].ConnectedItemObject.GetType())
622
            {
623
                PointInfo startPoint = pointInfos[0];
624
                PointInfo nextPoint = pointInfos[1];
625

    
626
                Symbol symbol = startPoint.ConnectedItemObject as Symbol;
627
                LMSymbol _LMSymbol = symbol.SPPID_ITEM_OBJECT as LMSymbol;
628
                double symbolX = _LMSymbol.get_XCoordinate();
629
                double symbolY = _LMSymbol.get_YCoordinate();
630

    
631
                if (lines[0].SLOPTYPE == Line.SlopeType.HORIZONTAL)
632
                {
633
                    startPoint.Y = symbolY;
634
                    nextPoint.Y = symbolY;
635
                }
636
                else if (lines[0].SLOPTYPE == Line.SlopeType.VERTICAL)
637
                {
638
                    startPoint.X = symbolX;
639
                    nextPoint.X = symbolX;
640
                }
641
            }
642
            // 맨 뒤가 Symbol일 경우
643
            else if (pointInfos[pointInfos.Count - 1].ConnectedItemObject != null && typeof(Symbol) == pointInfos[pointInfos.Count - 1].ConnectedItemObject.GetType())
644
            {
645
                PointInfo startPoint = pointInfos[pointInfos.Count - 1];
646
                PointInfo nextPoint = pointInfos[pointInfos.Count - 2];
647

    
648
                Symbol symbol = startPoint.ConnectedItemObject as Symbol;
649
                LMSymbol _LMSymbol = symbol.SPPID_ITEM_OBJECT as LMSymbol;
650
                double symbolX = _LMSymbol.get_XCoordinate();
651
                double symbolY = _LMSymbol.get_YCoordinate();
652

    
653
                if (lines[lines.Count - 1].SLOPTYPE == Line.SlopeType.HORIZONTAL)
654
                {
655
                    startPoint.Y = symbolY;
656
                    nextPoint.Y = symbolY;
657
                }
658
                else if (lines[lines.Count - 1].SLOPTYPE == Line.SlopeType.VERTICAL)
659
                {
660
                    startPoint.X = symbolX;
661
                    nextPoint.X = symbolX;
662
                }
663
            }
664
        }
665

    
666
        private Tuple<LMConnector, int> GetTargetLMConnector(PointInfo pointInfo)
667
        {
668
            LMConnector lMConnector = null;
669
            int insertIndex = 0;
670
            try
671
            {
672
                Line line = pointInfo.ConnectedItemObject as Line;
673
                if (line.SPPID_ITEM_OBJECT == null)
674
                    return null;
675

    
676
                string modelId = line.SPPID_ITEM_OBJECT.ToString();
677
                List<PointInfo> pointInfos = document.PipeRunPoint[modelId];
678
                double distance = double.MaxValue;
679

    
680
                PointInfo point1 = new PointInfo(0, 0);
681
                PointInfo point2 = new PointInfo(0, 0);
682

    
683
                for (int i = 0; i < pointInfos.Count - 1; i++)
684
                {
685
                    PointInfo tempPoint1 = pointInfos[i];
686
                    PointInfo tempPoint2 = pointInfos[i + 1];
687

    
688
                    double tempDistance = double.MaxValue;
689

    
690
                    Line.SlopeType slopeType = SPPIDUtill.CalcSlop(tempPoint1, tempPoint2);
691
                    if (slopeType == Line.SlopeType.HORIZONTAL)
692
                    {
693
                        double min = Math.Min(tempPoint1.X, tempPoint2.X);
694
                        double max = Math.Max(tempPoint1.X, tempPoint2.X);
695

    
696
                        if (min <= pointInfo.X && max >= pointInfo.X)
697
                            tempDistance = SPPIDUtill.CalcLineToPointDistance(tempPoint1, tempPoint2, pointInfo);
698
                    }
699
                    else if (slopeType == Line.SlopeType.VERTICAL)
700
                    {
701
                        double min = Math.Min(tempPoint1.Y, tempPoint2.Y);
702
                        double max = Math.Max(tempPoint1.Y, tempPoint2.Y);
703

    
704
                        if (min <= pointInfo.Y && max >= pointInfo.Y)
705
                            tempDistance = SPPIDUtill.CalcLineToPointDistance(tempPoint1, tempPoint2, pointInfo);
706
                    }
707
                    else
708
                    {
709
                        tempDistance = SPPIDUtill.CalcLineToPointDistance(tempPoint1, tempPoint2, pointInfo);
710
                    }
711

    
712
                    if (tempDistance < distance)
713
                    {
714
                        insertIndex = i + 1;
715
                        distance = tempDistance;
716
                        point1 = tempPoint1;
717
                        point2 = tempPoint2;
718
                    }
719
                }
720

    
721
                if (point1.Type == PointInfo.PointType.Run)
722
                    lMConnector = GetLMConnectorByPoint(modelId, point1);
723
                else if (point2.Type == PointInfo.PointType.Run)
724
                    lMConnector = GetLMConnectorByPoint(modelId, point2);
725
                else
726
                    lMConnector = GetLMConnectorBySP_ID(modelId, point1.SP_ID, point2.SP_ID);
727
            }
728
            catch (Exception ex)
729
            {
730
                Log.WriteLine(ex);
731
                return null;
732
            }
733

    
734
            if (lMConnector == null)
735
                return null;
736
            else
737
                return new Tuple<LMConnector, int>(lMConnector, insertIndex);
738
        }
739

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

    
760
                                if (tempDistance < distance)
761
                                {
762
                                    distance = tempDistance;
763
                                    lMConnector = _tempLMConnector;
764
                                }
765
                            }
766

    
767
                        }
768
                    }
769
                }
770
            }
771
            catch (Exception ex)
772
            {
773
                Log.WriteLine(ex);
774
            }
775
            
776
            
777
            return lMConnector;
778
        }
779

    
780
        private LMConnector GetLMConnectorBySP_ID(string modelId, string SP_ID1, string SP_ID2)
781
        {
782
            Placement _placement = new Placement();
783

    
784
            LMPipeRun lMPipeRun = _placement.PIDDataSource.GetPipeRun(modelId);
785
            foreach (LMRepresentation rep in lMPipeRun.Representations)
786
            {
787
                if (rep.Attributes["RepresentationType"].get_Value() == "Connector" && rep.Attributes["ItemStatus"].get_Value() == "Active")
788
                {
789
                    LMConnector _tempLMConnector = _placement.PIDDataSource.GetConnector(rep.Id);
790

    
791
                    bool find1 = false;
792
                    bool find2 = false;
793

    
794
                    if (_tempLMConnector.ConnectItem1SymbolID != null && !DBNull.Value.Equals(_tempLMConnector.ConnectItem1SymbolID))
795
                    {
796
                        string connId = _tempLMConnector.ConnectItem1SymbolID;
797
                        if (connId == SP_ID1 || connId == SP_ID2)
798
                            find1 = true;
799
                    }
800

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

    
808

    
809
                    if (find1 && find2)
810
                    {
811
                        return _tempLMConnector;
812
                    }
813
                }
814
            }
815
            return null;
816
        }
817

    
818
        #endregion
819

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