프로젝트

일반

사용자정보

통계
| 개정판:

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

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

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

    
14

    
15
namespace SPPID.Modeling
16
{
17
    public class AutoModeling
18
    {
19
        private Document document;
20

    
21
        public AutoModeling(Document doc)
22
        {
23
            document = doc;
24
        }
25

    
26
        public void Run()
27
        {
28
            Stopwatch sw = new Stopwatch();
29
            sw.Start();
30
            CloseOPCForm.Run();
31

    
32
            // Group Symbol 실패시 Group Remove
33
            foreach (Group group in document.GROUPS)
34
            {
35
                if (!GroupSymbolModeling(group))
36
                {
37
                    group.Enable = false;
38
                }
39
            }
40

    
41
            // Line Group은 Group 없을 때까지 Loop
42
            while (document.GROUPS.Count > 0)
43
            {
44
                bool loopAction = false;
45
                foreach (Group group in document.GROUPS)
46
                {
47
                    if (!group.Enable)
48
                        continue;
49

    
50
                    if (IsPossibleDrawGroupLine(group))
51
                    {
52
                        loopAction = true;
53
                        GroupLineModeling(group);
54
                        document.GROUPS.Remove(group);
55
                        break;
56
                    }
57
                }
58
                if (!loopAction)
59
                    break;
60
            }
61
            // Line Group Modeling 실패한 로그
62
            foreach (Group group in document.GROUPS)
63
            {
64
                foreach (SPPID_ITEM item in group.Items)
65
                {
66
                    Line line = item as Line;
67
                    if (line != null && line.SPPID_ITEM_OBJECT == null)
68
                    {
69
                     
70
                    }
71
                }
72
            }
73

    
74
            foreach (SpecBreak item in document.SPECBREAK)
75
            {
76
                item.Modeling(document);
77
                item.SetAttribute(document);
78
                foreach (var result in item.SPPID_ITEM_OBJECT_LIST)
79
                {
80
                    LMLabelPersist label = result.Value;
81
                    if (label != null)
82
                    {
83
                        
84
                    }
85
                    else
86
                    {
87
                        
88
                    }
89
                }
90
            }
91

    
92
            foreach (LineNumber lineNumber in document.LINENUMBERS)
93
            {
94
                lineNumber.SetAttribute(document);
95
            }
96

    
97
            // Text Modeling
98
            foreach (Text text in document.TEXTS)
99
            {
100
                text.Modeling(document);
101
                if (text.SPPID_ITEM_OBJECT != null || text.IsAssociation)
102
                {
103

    
104
                }
105
                else
106
                {
107
                    
108
                }
109
            }
110

    
111
            // attribute
112
            foreach (Symbol item in document.SYMBOLS)
113
            {
114
                if (item.SPPID_ITEM_OBJECT != null)
115
                {
116
                    item.SetAttribute(document);
117
                }
118
            }
119

    
120

    
121
            CloseOPCForm.Stop();
122
            //
123
            sw.Stop();
124
            TimeSpan ts = sw.Elapsed;
125
            string elapsedTime = String.Format("{0:00}:{1:00}.{2:00}",
126
            ts.Minutes, ts.Seconds,
127
            ts.Milliseconds / 10);
128
        }
129

    
130
        private bool IsPossibleDrawGroupLine(Group group)
131
        {
132
            foreach (SPPID_ITEM item in group.Items)
133
            {
134
                if (item.GetType() == typeof(Symbol))
135
                {
136
                    Symbol symbol = item as Symbol;
137
                    foreach (Connector connector in symbol.CONNECTORS)
138
                    {
139
                        SPPID_ITEM objItem = SPPIDUtill.FindObjectByUID(document, connector.CONNECTEDITEM) as SPPID_ITEM;
140
                        if (objItem != null && !group.Items.Contains(objItem) && objItem.SPPID_ITEM_OBJECT == null)
141
                            return false;
142
                    }
143
                }
144
                else if (item.GetType() == typeof(Line))
145
                {
146
                    Line line = item as Line;
147
                    foreach (Connector connector in line.CONNECTORS)
148
                    {
149
                        SPPID_ITEM objItem = SPPIDUtill.FindObjectByUID(document, connector.CONNECTEDITEM) as SPPID_ITEM;
150
                        if (objItem != null && !group.Items.Contains(objItem) && objItem.SPPID_ITEM_OBJECT == null)
151
                            return false;
152
                    }
153
                }
154
            }
155

    
156
            return true;
157
        }
158

    
159
        #region Modeling
160

    
161
        private bool GroupSymbolModeling(Group group)
162
        {
163
            List<object> Items = group.Items;
164
            Symbol prevSymbol = null;
165
            int prevIndex = 0;
166
            bool result = true;
167

    
168
            for (int itemIndex = 0; itemIndex < Items.Count(); itemIndex++)
169
            {
170
                Line.SlopeType prevSlope = Line.SlopeType.NONE;
171
                Symbol symbol = Items[itemIndex] as Symbol;
172
                try
173
                {
174
                    if (symbol != null)
175
                    {
176
                        // Group중에 최근에 Modeling된 Symbol
177
                        if (prevSymbol != null)
178
                        {
179
                            LMSymbol prevLMSymbol = prevSymbol.SPPID_ITEM_OBJECT as LMSymbol;
180
                            for (int prevLineIndex = prevIndex + 1; prevLineIndex < itemIndex; prevLineIndex++)
181
                            {
182
                                Line prevLine = Items[prevLineIndex] as Line;
183
                                if (prevLine != null)
184
                                {
185
                                    if (prevSlope == Line.SlopeType.NONE)
186
                                    {
187
                                        prevSlope = prevLine.SLOPTYPE;
188
                                    }
189
                                    else if (prevSlope != prevLine.SLOPTYPE)
190
                                    {
191
                                        prevSlope = Line.SlopeType.NONE;
192
                                        break;
193
                                    }
194
                                }
195
                            }
196

    
197
                            if (prevSlope == Line.SlopeType.HORIZONTAL)
198
                                symbol.LOCATION_Y = prevLMSymbol.get_YCoordinate();
199
                            else if (prevSlope == Line.SlopeType.VERTICAL)
200
                                symbol.LOCATION_X = prevLMSymbol.get_XCoordinate();
201
                        }
202

    
203
                        symbol.Modeling(document);
204
                        if (symbol.SPPID_ITEM_OBJECT != null)
205
                        {
206
                            prevSymbol = symbol;
207
                            prevIndex = itemIndex;
208
                        }
209
                        else
210
                        {
211
                            result = false;
212
                        }
213
                    }
214
                }
215
                catch (Exception ex)
216
                {
217
                    result = false;
218
                    return result;
219
                }
220

    
221
                
222
            }
223

    
224
            return result;
225
        }
226

    
227
        private bool GroupLineModeling(Group group)
228
        {
229
            try
230
            {
231
                List<object> Items = group.Items;
232
                List<Line> lineList = new List<Line>();
233
                foreach (SPPID_ITEM item in Items)
234
                {
235
                    Line line = item as Line;
236
                    if (line != null)
237
                        lineList.Add(line);
238
                    else if (lineList.Count > 0)
239
                    {
240
                        GroupLineModelingByList(lineList, group);
241
                    }
242
                }
243

    
244
                if (lineList.Count > 0)
245
                    GroupLineModelingByList(lineList, group);
246
            }
247
            catch (Exception ex)
248
            {
249
                Log.WriteLine(ex);
250
                return false;
251
            }
252

    
253
            return true;
254
        }
255

    
256
        private void GroupLineModelingByList(List<Line> lines, Group group)
257
        {
258
            List<PointInfo> pointInfos = GetLinePointInfos(lines, group);
259
            LMConnector lMConnector = DrawLines(pointInfos, lines[0].SPPIDMAPPINGNAME, lines);
260

    
261
            if (lMConnector != null)
262
            {
263
                string modelId = lMConnector.ModelItemID;
264
                document.PipeRunPoint.Add(lMConnector.ModelItemID, pointInfos);
265
                foreach (Line line in lines)
266
                {
267
                    line.SPPID_ITEM_OBJECT = modelId;
268
                    IfContainLineNumber(line.UID);
269
                }
270
            }
271
            else
272
            {
273
                foreach (Line line in lines)
274
                {
275
                 
276
                }
277
            }
278

    
279
            lines.Clear();
280
        }
281

    
282
        private void IfContainLineNumber(string lineUID)
283
        {
284
            foreach (LineNumber lineNumber in document.LINENUMBERS)
285
            {
286
                if (lineNumber.CONNECTLINE == lineUID)
287
                {
288
                    lineNumber.Modeling(document);
289

    
290
                    if (lineNumber.SPPID_ITEM_OBJECT != null)
291
                    {
292

    
293
                    }
294
                    else
295
                    {
296

    
297
                    }
298
                    break;
299
                }
300
            }
301
        }
302

    
303
        private LMConnector DrawLines(List<PointInfo> pointInfos, string SPPID_MAPPINGNAME, List<Line> lines)
304
        {
305
            LMConnector lMConnector = null;
306
            try
307
            {
308
                Placement _placement = new Placement();
309
                _LMAItem _LMAItem = _placement.PIDCreateItem(SPPID_MAPPINGNAME);
310
                PlaceRunInputs placeRunInputs = new PlaceRunInputs();
311

    
312
                PointInfo branchPoint1 = null;
313
                PointInfo branchPoint1_1 = null;
314
                int branchIndex1 = 0;
315
                List<string> branch_SP_ID_List1 = null;
316
                PointInfo branchPoint2 = null;
317
                PointInfo branchPoint2_1 = null;
318
                int branchIndex2 = 0;
319
                List<string> branch_SP_ID_List2 = null;
320

    
321

    
322
                for (int pointIndex = 0; pointIndex < pointInfos.Count; pointIndex++)
323
                {
324
                    PointInfo pointInfo = pointInfos[pointIndex];
325
                    if (pointIndex == 0 || pointIndex == pointInfos.Count - 1)
326
                    {
327
                        if (pointInfo.ConnectedItemObject == null)
328
                        {
329
                            placeRunInputs.AddPoint(pointInfo.X, pointInfo.Y);
330
                        }
331
                        else
332
                        {
333
                            if (typeof(Symbol) == pointInfo.ConnectedItemObject.GetType())
334
                            {
335
                                Symbol symbol = pointInfo.ConnectedItemObject as Symbol;
336
                                //check child
337
                                List<string> linesUID = new List<string>();
338
                                foreach (Line line in lines)
339
                                    linesUID.Add(line.UID);
340

    
341
                                int symbolIndex = 0;
342
                                foreach (Connector connector in symbol.CONNECTORS)
343
                                {
344
                                    if (linesUID.Contains(connector.CONNECTEDITEM))
345
                                    {
346
                                        symbolIndex = connector.INDEX;
347
                                        break;
348
                                    }
349
                                }
350
                                LMSymbol tartgetLMSymbol = null;
351
                                if (symbolIndex == 0)
352
                                    tartgetLMSymbol = symbol.SPPID_ITEM_OBJECT as LMSymbol;
353
                                else
354
                                {
355
                                    foreach (SymbolChild child in symbol.CHILD_LIST)
356
                                    {
357
                                        if (child.Index == symbolIndex)
358
                                        {
359
                                            tartgetLMSymbol = child.SPPID_ITEM_OBJECT as LMSymbol;
360
                                            break;
361
                                        }
362
                                    }
363
                                }
364
                                
365
                                placeRunInputs.AddSymbolTarget(tartgetLMSymbol, pointInfo.X, pointInfo.Y);
366
                                pointInfo.Type = PointInfo.PointType.Branch;
367
                            }
368
                            else if (typeof(Line) == pointInfo.ConnectedItemObject.GetType())
369
                            {
370
                                Line line = pointInfo.ConnectedItemObject as Line;
371
                                Tuple<LMConnector, int> targetLMConnector = GetTargetLMConnector(pointInfo);
372
                                if (targetLMConnector != null) 
373
                                {
374
                                    if (targetLMConnector.Item1 != null)
375
                                    {
376
                                        if (branchPoint1 == null)
377
                                        {
378
                                            placeRunInputs.AddConnectorTarget(targetLMConnector.Item1, pointInfo.X, pointInfo.Y);
379
                                            pointInfo.Type = PointInfo.PointType.Branch;
380
                                            branchPoint1 = new PointInfo(pointInfo.X, pointInfo.Y);
381
                                            branchPoint1.Type = PointInfo.PointType.Branch;
382
                                            branchPoint1.ConnectedItem = pointInfo.ConnectedItem;
383
                                            branchPoint1.ConnectedItemObject = pointInfo.ConnectedItemObject;
384
                                            branchIndex1 = targetLMConnector.Item2;
385
                                            branchPoint1_1 = pointInfo;
386

    
387
                                            branch_SP_ID_List1 = new List<string>();
388
                                            LMPipeRun lMPipeRun = _placement.PIDDataSource.GetPipeRun(line.SPPID_ITEM_OBJECT.ToString());
389
                                            foreach (LMRepresentation rep in lMPipeRun.Representations)
390
                                                if (rep.Attributes["RepresentationType"].get_Value() == "Branch" && rep.Attributes["ItemStatus"].get_Value() == "Active")
391
                                                    branch_SP_ID_List1.Add(rep.Id);
392
                                        }
393
                                        else
394
                                        {
395
                                            placeRunInputs.AddConnectorTarget(targetLMConnector.Item1, pointInfo.X, pointInfo.Y);
396
                                            pointInfo.Type = PointInfo.PointType.Branch;
397
                                            branchPoint2 = new PointInfo(pointInfo.X, pointInfo.Y);
398
                                            branchPoint2.Type = PointInfo.PointType.Branch;
399
                                            branchPoint2.ConnectedItem = pointInfo.ConnectedItem;
400
                                            branchPoint2.ConnectedItemObject = pointInfo.ConnectedItemObject;
401
                                            branchIndex2 = targetLMConnector.Item2;
402
                                            branchPoint2_1 = pointInfo;
403

    
404
                                            branch_SP_ID_List2 = new List<string>();
405
                                            LMPipeRun lMPipeRun = _placement.PIDDataSource.GetPipeRun(line.SPPID_ITEM_OBJECT.ToString());
406
                                            foreach (LMRepresentation rep in lMPipeRun.Representations)
407
                                                if (rep.Attributes["RepresentationType"].get_Value() == "Branch" && rep.Attributes["ItemStatus"].get_Value() == "Active")
408
                                                    branch_SP_ID_List2.Add(rep.Id);
409
                                        }
410
                                    }
411
                                    else
412
                                    {
413
                                        placeRunInputs.AddPoint(pointInfo.X, pointInfo.Y);
414
                                        pointInfo.Type = PointInfo.PointType.Run;
415
                                    }
416
                                }
417
                                else
418
                                {
419
                                    placeRunInputs.AddPoint(pointInfo.X, pointInfo.Y);
420
                                    pointInfo.Type = PointInfo.PointType.Run;
421
                                }
422
                            }
423
                        }
424
                    }
425
                    else
426
                    {
427
                        placeRunInputs.AddPoint(pointInfo.X, pointInfo.Y);
428
                        pointInfo.Type = PointInfo.PointType.Run;
429
                    }
430
                }
431

    
432
                lMConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
433
                lMConnector.Commit();
434

    
435
                // Branch Point Setting
436
                if (branchPoint1 != null)
437
                {
438
                    Line branchedLine = branchPoint1.ConnectedItemObject as Line;
439
                    LMPipeRun lMPipeRun = _placement.PIDDataSource.GetPipeRun(branchedLine.SPPID_ITEM_OBJECT.ToString());
440
                    foreach (LMRepresentation rep in lMPipeRun.Representations)
441
                        if (rep.Attributes["RepresentationType"].get_Value() == "Branch" && rep.Attributes["ItemStatus"].get_Value() == "Active")
442
                        {
443
                            if (!branch_SP_ID_List1.Contains(rep.Id))
444
                            {
445
                                branchPoint1.SP_ID = rep.Id;
446
                                branchPoint1_1.SP_ID = rep.Id;
447
                                document.PipeRunPoint[branchedLine.SPPID_ITEM_OBJECT.ToString()].Insert(branchIndex1, branchPoint1);
448
                                break;
449
                            }
450
                        }
451

    
452
                    if (string.IsNullOrEmpty(branchPoint1.SP_ID))
453
                    {
454
                        document.PipeRunPoint[branchedLine.SPPID_ITEM_OBJECT.ToString()].RemoveAt(branchIndex1);
455
                        branchPoint1_1.Type = PointInfo.PointType.Run;
456
                    }
457
                }
458
                if (branchPoint2 != null)
459
                {
460
                    Line branchedLine = branchPoint2.ConnectedItemObject as Line;
461
                    LMPipeRun lMPipeRun = _placement.PIDDataSource.GetPipeRun(branchedLine.SPPID_ITEM_OBJECT.ToString());
462
                    foreach (LMRepresentation rep in lMPipeRun.Representations)
463
                        if (rep.Attributes["RepresentationType"].get_Value() == "Branch" && rep.Attributes["ItemStatus"].get_Value() == "Active")
464
                        {
465
                            if (!branch_SP_ID_List2.Contains(rep.Id))
466
                            {
467
                                branchPoint2.SP_ID = rep.Id;
468
                                branchPoint2_1.SP_ID = rep.Id;
469
                                document.PipeRunPoint[branchedLine.SPPID_ITEM_OBJECT.ToString()].Insert(branchIndex2, branchPoint2);
470
                                break;
471
                            }
472
                        }
473

    
474
                    if (string.IsNullOrEmpty(branchPoint2.SP_ID))
475
                    {
476
                        document.PipeRunPoint[branchedLine.SPPID_ITEM_OBJECT.ToString()].RemoveAt(branchIndex2);
477
                        branchPoint2_1.Type = PointInfo.PointType.Run;
478
                    }
479
                }
480
                // End
481
            }
482
            catch (Exception ex)
483
            {
484
                Log.WriteLine(ex);
485
            }
486

    
487
            return lMConnector;
488
        }
489

    
490
        private List<PointInfo> GetLinePointInfos(List<Line> lineList, Group group)
491
        {
492
            // Line들의 points를 구함
493
            List<PointInfo> pointInfos = new List<PointInfo>();
494
            if (lineList.Count == 1)
495
            {
496
                pointInfos.Add(new PointInfo(lineList[0].START_X, lineList[0].START_Y) { ConnectedItem = lineList[0].CONNECTORS[0].CONNECTEDITEM });
497
                pointInfos.Add(new PointInfo(lineList[0].END_X, lineList[0].END_Y) { ConnectedItem = lineList[0].CONNECTORS[1].CONNECTEDITEM });
498
            }
499
            else
500
            {
501
                for (int i = 0; i < lineList.Count; i++)
502
                    if (lineList.Count > i + 1)
503
                        GetLinePoints(lineList[i], lineList[i + 1], pointInfos);
504
            }
505

    
506
            // 맨앞 뒤의 ConnectedItem 구함
507
            for (int i = 0; i < pointInfos.Count; i++)
508
            {
509
                PointInfo info = pointInfos[i];
510
                if (i == 0 || pointInfos.Count - 1 == i)
511
                {
512
                    info.ConnectedItemObject = SPPIDUtill.FindObjectByUID(document, info.ConnectedItem) as SPPID_ITEM;
513

    
514
                    if (info.ConnectedItemObject == null)
515
                    {
516
                        info.Type = PointInfo.PointType.Run;
517
                    }
518
                    else
519
                    {
520
                        if (info.ConnectedItemObject.GetType() == typeof(Symbol))
521
                        {
522
                            Symbol symbol = info.ConnectedItemObject as Symbol;
523
                            LMSymbol _lmSymbol = symbol.SPPID_ITEM_OBJECT as LMSymbol;
524
                            info.SP_ID = _lmSymbol.AsLMRepresentation().Id;
525
                        }
526
                        info.Type = PointInfo.PointType.Branch;
527
                    }
528
                }
529
                else
530
                {
531
                    info.ConnectedItem = string.Empty;
532
                    info.Type = PointInfo.PointType.Run;
533
                }
534
                    
535
            }
536

    
537
            // 라인 보정
538
            CalibratePointInfos(pointInfos, lineList);
539

    
540
            return pointInfos;
541
        }
542

    
543
        private void GetLinePoints(Line line, Line nextLine, List<PointInfo> pointInfos)
544
        {
545
            if (nextLine != null)
546
            {
547
                if (pointInfos.Count > 0)
548
                    pointInfos.RemoveRange(pointInfos.Count - 2, 2);
549

    
550
                if (line.CONNECTORS[0].CONNECTEDITEM == nextLine.UID)
551
                {
552
                    pointInfos.Add(new PointInfo(line.END_X, line.END_Y) { ConnectedItem = line.CONNECTORS[1].CONNECTEDITEM });
553
                    pointInfos.Add(new PointInfo(line.START_X, line.START_Y) { ConnectedItem = line.CONNECTORS[0].CONNECTEDITEM });
554

    
555
                    if (nextLine.CONNECTORS[0].CONNECTEDITEM == line.UID)
556
                        pointInfos.Add(new PointInfo(nextLine.END_X, nextLine.END_Y) { ConnectedItem = nextLine.CONNECTORS[1].CONNECTEDITEM });
557
                    else if (nextLine.CONNECTORS[1].CONNECTEDITEM == line.UID)
558
                        pointInfos.Add(new PointInfo(nextLine.START_X, nextLine.START_Y) { ConnectedItem = nextLine.CONNECTORS[0].CONNECTEDITEM });
559

    
560
                }
561
                else if (line.CONNECTORS[1].CONNECTEDITEM == nextLine.UID)
562
                {
563
                    pointInfos.Add(new PointInfo(line.START_X, line.START_Y) { ConnectedItem = line.CONNECTORS[0].CONNECTEDITEM });
564
                    pointInfos.Add(new PointInfo(line.END_X, line.END_Y) { ConnectedItem = line.CONNECTORS[1].CONNECTEDITEM });
565

    
566
                    if (nextLine.CONNECTORS[0].CONNECTEDITEM == line.UID)
567
                        pointInfos.Add(new PointInfo(nextLine.END_X, nextLine.END_Y) { ConnectedItem = nextLine.CONNECTORS[1].CONNECTEDITEM });
568
                    else if (nextLine.CONNECTORS[1].CONNECTEDITEM == line.UID)
569
                        pointInfos.Add(new PointInfo(nextLine.START_X, nextLine.START_Y) { ConnectedItem = nextLine.CONNECTORS[0].CONNECTEDITEM });
570
                }
571
            }
572
        }
573

    
574
        private void CalibratePointInfos(List<PointInfo> pointInfos, List<Line> lines)
575
        {
576
            // 맨 앞이 Symbol일 경우
577
            if (pointInfos[0].ConnectedItemObject != null && typeof(Symbol) == pointInfos[0].ConnectedItemObject.GetType())
578
            {
579
                PointInfo startPoint = pointInfos[0];
580
                PointInfo nextPoint = pointInfos[1];
581

    
582
                Symbol symbol = startPoint.ConnectedItemObject as Symbol;
583
                LMSymbol _LMSymbol = symbol.SPPID_ITEM_OBJECT as LMSymbol;
584
                double symbolX = _LMSymbol.get_XCoordinate();
585
                double symbolY = _LMSymbol.get_YCoordinate();
586

    
587
                if (lines[0].SLOPTYPE == Line.SlopeType.HORIZONTAL)
588
                {
589
                    startPoint.Y = symbolY;
590
                    nextPoint.Y = symbolY;
591
                }
592
                else if (lines[0].SLOPTYPE == Line.SlopeType.VERTICAL)
593
                {
594
                    startPoint.X = symbolX;
595
                    nextPoint.X = symbolX;
596
                }
597
            }
598
            // 맨 뒤가 Symbol일 경우
599
            else if (pointInfos[pointInfos.Count - 1].ConnectedItemObject != null && typeof(Symbol) == pointInfos[pointInfos.Count - 1].ConnectedItemObject.GetType())
600
            {
601
                PointInfo startPoint = pointInfos[pointInfos.Count - 1];
602
                PointInfo nextPoint = pointInfos[pointInfos.Count - 2];
603

    
604
                Symbol symbol = startPoint.ConnectedItemObject as Symbol;
605
                LMSymbol _LMSymbol = symbol.SPPID_ITEM_OBJECT as LMSymbol;
606
                double symbolX = _LMSymbol.get_XCoordinate();
607
                double symbolY = _LMSymbol.get_YCoordinate();
608

    
609
                if (lines[lines.Count - 1].SLOPTYPE == Line.SlopeType.HORIZONTAL)
610
                {
611
                    startPoint.Y = symbolY;
612
                    nextPoint.Y = symbolY;
613
                }
614
                else if (lines[lines.Count - 1].SLOPTYPE == Line.SlopeType.VERTICAL)
615
                {
616
                    startPoint.X = symbolX;
617
                    nextPoint.X = symbolX;
618
                }
619
            }
620
        }
621

    
622
        private Tuple<LMConnector, int> GetTargetLMConnector(PointInfo pointInfo)
623
        {
624
            LMConnector lMConnector = null;
625
            int insertIndex = 0;
626
            try
627
            {
628
                Line line = pointInfo.ConnectedItemObject as Line;
629
                if (line.SPPID_ITEM_OBJECT == null)
630
                    return null;
631

    
632
                string modelId = line.SPPID_ITEM_OBJECT.ToString();
633
                List<PointInfo> pointInfos = document.PipeRunPoint[modelId];
634
                double distance = double.MaxValue;
635

    
636
                PointInfo point1 = new PointInfo(0, 0);
637
                PointInfo point2 = new PointInfo(0, 0);
638

    
639
                for (int i = 0; i < pointInfos.Count - 1; i++)
640
                {
641
                    PointInfo tempPoint1 = pointInfos[i];
642
                    PointInfo tempPoint2 = pointInfos[i + 1];
643

    
644
                    double tempDistance = double.MaxValue;
645

    
646
                    Line.SlopeType slopeType = SPPIDUtill.CalcSlop(tempPoint1, tempPoint2);
647
                    if (slopeType == Line.SlopeType.HORIZONTAL)
648
                    {
649
                        double min = Math.Min(tempPoint1.X, tempPoint2.X);
650
                        double max = Math.Max(tempPoint1.X, tempPoint2.X);
651

    
652
                        if (min <= pointInfo.X && max >= pointInfo.X)
653
                            tempDistance = SPPIDUtill.CalcLineToPointDistance(tempPoint1, tempPoint2, pointInfo);
654
                    }
655
                    else if (slopeType == Line.SlopeType.VERTICAL)
656
                    {
657
                        double min = Math.Min(tempPoint1.Y, tempPoint2.Y);
658
                        double max = Math.Max(tempPoint1.Y, tempPoint2.Y);
659

    
660
                        if (min <= pointInfo.Y && max >= pointInfo.Y)
661
                            tempDistance = SPPIDUtill.CalcLineToPointDistance(tempPoint1, tempPoint2, pointInfo);
662
                    }
663
                    else
664
                    {
665
                        tempDistance = SPPIDUtill.CalcLineToPointDistance(tempPoint1, tempPoint2, pointInfo);
666
                    }
667

    
668
                    if (tempDistance < distance)
669
                    {
670
                        insertIndex = i + 1;
671
                        distance = tempDistance;
672
                        point1 = tempPoint1;
673
                        point2 = tempPoint2;
674
                    }
675
                }
676

    
677
                if (point1.Type == PointInfo.PointType.Run)
678
                    lMConnector = GetLMConnectorByPoint(modelId, point1);
679
                else if (point2.Type == PointInfo.PointType.Run)
680
                    lMConnector = GetLMConnectorByPoint(modelId, point2);
681
                else
682
                    lMConnector = GetLMConnectorBySP_ID(modelId, point1.SP_ID, point2.SP_ID);
683
            }
684
            catch (Exception ex)
685
            {
686
                Log.WriteLine(ex);
687
                return null;
688
            }
689

    
690
            if (lMConnector == null)
691
                return null;
692
            else
693
                return new Tuple<LMConnector, int>(lMConnector, insertIndex);
694
        }
695

    
696
        private LMConnector GetLMConnectorByPoint(string modelId, PointInfo pointInfo)
697
        {
698
            LMConnector lMConnector = null;
699
            
700
            try
701
            {
702
                Placement _placement = new Placement();
703
                double distance = double.MaxValue;
704
                LMPipeRun lMPipeRun = _placement.PIDDataSource.GetPipeRun(modelId);
705
                if (lMPipeRun != null)
706
                {
707
                    foreach (LMRepresentation rep in lMPipeRun.Representations)
708
                    {
709
                        if (rep.Attributes["RepresentationType"].get_Value() == "Connector" && rep.Attributes["ItemStatus"].get_Value() == "Active")
710
                        {
711
                            LMConnector _tempLMConnector = _placement.PIDDataSource.GetConnector(rep.Id);
712
                            foreach (LMConnectorVertex vertex in _tempLMConnector.ConnectorVertices)
713
                            {
714
                                double tempDistance = SPPIDUtill.CalcPointToPointdDistance(pointInfo, new PointInfo(vertex.get_XCoordinate(), vertex.get_YCoordinate()));
715

    
716
                                if (tempDistance < distance)
717
                                {
718
                                    distance = tempDistance;
719
                                    lMConnector = _tempLMConnector;
720
                                }
721
                            }
722

    
723
                        }
724
                    }
725
                }
726
            }
727
            catch (Exception ex)
728
            {
729
                Log.WriteLine(ex);
730
            }
731
            
732
            
733
            return lMConnector;
734
        }
735

    
736
        private LMConnector GetLMConnectorBySP_ID(string modelId, string SP_ID1, string SP_ID2)
737
        {
738
            Placement _placement = new Placement();
739

    
740
            LMPipeRun lMPipeRun = _placement.PIDDataSource.GetPipeRun(modelId);
741
            foreach (LMRepresentation rep in lMPipeRun.Representations)
742
            {
743
                if (rep.Attributes["RepresentationType"].get_Value() == "Connector" && rep.Attributes["ItemStatus"].get_Value() == "Active")
744
                {
745
                    LMConnector _tempLMConnector = _placement.PIDDataSource.GetConnector(rep.Id);
746

    
747
                    bool find1 = false;
748
                    bool find2 = false;
749

    
750
                    if (_tempLMConnector.ConnectItem1SymbolID != null && !DBNull.Value.Equals(_tempLMConnector.ConnectItem1SymbolID))
751
                    {
752
                        string connId = _tempLMConnector.ConnectItem1SymbolID;
753
                        if (connId == SP_ID1 || connId == SP_ID2)
754
                            find1 = true;
755
                    }
756

    
757
                    if (_tempLMConnector.ConnectItem2SymbolID != null && !DBNull.Value.Equals(_tempLMConnector.ConnectItem2SymbolID))
758
                    {
759
                        string connId = _tempLMConnector.ConnectItem2SymbolID;
760
                        if (connId == SP_ID1 || connId == SP_ID2)
761
                            find2 = true;
762
                    }
763

    
764

    
765
                    if (find1 && find2)
766
                    {
767
                        return _tempLMConnector;
768
                    }
769
                }
770
            }
771
            return null;
772
        }
773

    
774
        #endregion
775

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