프로젝트

일반

사용자정보

통계
| 개정판:

hytos / DTI_PID / SPPIDConverter / AutoModeling.cs @ 3165c259

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

1
using System;
2
using System.Collections.Generic;
3
using System.Linq;
4
using System.Text;
5
using System.Threading.Tasks;
6
using Llama;
7
using Plaice;
8
using Ingr.RAD2D.Interop.RAD2D;
9
using Ingr.RAD2D.Internal;
10
using Ingr.RAD2D.Helper;
11
using Converter.BaseModel;
12
using Converter.SPPID.Model;
13
using Converter.SPPID.Properties;
14
using Converter.SPPID.Util;
15
using Converter.SPPID.DB;
16
using Ingr.RAD2D.MacroControls.CmdCtrl;
17
using Ingr.RAD2D;
18
using System.Windows;
19
using System.Threading;
20
using System.Drawing;
21
using Microsoft.VisualBasic;
22
using Newtonsoft.Json;
23

    
24
namespace Converter.SPPID
25
{
26
    public class AutoModeling
27
    {
28
        Placement _placement;
29
        LMADataSource dataSource;
30
        Ingr.RAD2D.Application radApp;
31
        SPPID_Document document;
32

    
33
        List<Tuple<string, Line, Line>> BranchLines = new List<Tuple<string, Line, Line>>();
34
        public AutoModeling(SPPID_Document document, Ingr.RAD2D.Application radApp)
35
        {
36
            this.document = document;
37
            this.radApp = radApp;
38
        }
39

    
40
        public void Run()
41
        {
42
            _placement = new Placement();
43
            dataSource = _placement.PIDDataSource;
44
            //dynamic application = Interaction.GetObject("", "PIDAutomation.Application");
45
            //dynamic newDrawing = application.Drawings.Add(document.Unit, document.Template, document.DrawingNumber, document.DrawingName);
46
            //application.ActiveWindow.Fit();
47
            //Thread.Sleep(100);
48
            //application.ActiveWindow.Zoom = 60;
49
            //Thread.Sleep(100);
50
            try
51
            {
52
                foreach (Equipment equipment in document.Equipments)
53
                {
54
                    SymbolModeling(equipment as Symbol, null, null);
55
                }
56

    
57
                foreach (LineNumber lineNumber in document.LINENUMBERS)
58
                {
59
                    foreach (LineRun run in lineNumber.RUNS)
60
                    {
61
                        SymbolModelingByRun(run);
62
                    }
63
                }
64

    
65
                foreach (TrimLine trimLine in document.TRIMLINES)
66
                {
67
                    foreach (LineRun run in trimLine.RUNS)
68
                    {
69
                        SymbolModelingByRun(run);
70
                    }
71
                }
72

    
73
                foreach (LineNumber lineNumber in document.LINENUMBERS)
74
                {
75
                    foreach (LineRun run in lineNumber.RUNS)
76
                    {
77
                        LineModelingByRun(run);
78
                    }
79
                }
80

    
81
                foreach (TrimLine trimLine in document.TRIMLINES)
82
                {
83
                    foreach (LineRun run in trimLine.RUNS)
84
                    {
85
                        LineModelingByRun(run);
86
                    }
87
                }
88

    
89
                foreach (var item in BranchLines)
90
                {
91
                    BranchLineModeling(item);
92
                }
93

    
94
                foreach (var item in document.EndBreaks)
95
                {
96
                    EndBreakModeling(item);
97
                }
98

    
99

    
100

    
101
                foreach (LineNumber lineNumber in document.LINENUMBERS)
102
                {
103

    
104
                    foreach (LineRun run in lineNumber.RUNS)
105
                    {
106
                        JoinRunLine(run);
107
                    }
108
                }
109

    
110
                foreach (TrimLine trimLine in document.TRIMLINES)
111
                {
112
                    foreach (LineRun run in trimLine.RUNS)
113
                    {
114
                        JoinRunLine(run);
115
                    }
116
                }
117
            }
118
            catch (Exception ex)
119
            {
120
                System.Windows.Forms.MessageBox.Show(ex.Message + "\r\n" + ex.StackTrace);
121
            }
122
            finally
123
            {
124
                ReleaseCOMObjects(dataSource);
125
                ReleaseCOMObjects(_placement);
126
            }
127
           
128
            System.Windows.Forms.MessageBox.Show("end");
129
        }
130

    
131
        private void LineModelingByRun(LineRun run)
132
        {
133
            Line prevLine = null;
134
            List<Line> lines = new List<Line>();
135
            foreach (var item in run.RUNITEMS)
136
            {
137
                // Line일 경우
138
                if (item.GetType() == typeof(Line))
139
                {
140
                    Line line = item as Line;
141
                    if (prevLine == null)
142
                        lines.Add(line);
143
                    else if (prevLine != null)
144
                    {
145
                        if (prevLine.SPPID.MAPPINGNAME == line.SPPID.MAPPINGNAME)
146
                            lines.Add(line);
147
                        else
148
                        {
149
                            LineModeling(lines);
150
                            lines.Clear();
151
                            lines.Add(line);
152
                        }
153
                    }
154

    
155
                    prevLine = line;
156
                }
157
                // Symbol 일 경우
158
                else if (item.GetType() == typeof(Symbol))
159
                {
160
                    if (lines.Count > 0)
161
                    {
162
                        LineModeling(lines);
163
                        lines.Clear();
164
                    }
165
                }
166
            }
167

    
168
            if (lines.Count > 0)
169
                LineModeling(lines);
170
        }
171

    
172
        private void SymbolModelingByRun(LineRun run)
173
        {
174
            // 양끝 Symbol 검사 후 Line이 나올때까지만 Symbol Modeling
175
            if (run.RUNITEMS.Count > 0)
176
            {
177
                if (run.RUNITEMS[0].GetType() == typeof(Symbol))
178
                    SymbolModelingByRunStart(run.RUNITEMS[0] as Symbol, run);
179

    
180
                if (run.RUNITEMS[run.RUNITEMS.Count - 1].GetType() == typeof(Symbol))
181
                    SymbolModelingByRunEnd(run.RUNITEMS[run.RUNITEMS.Count - 1] as Symbol, run);
182
            }
183

    
184
            Symbol prevSymbol = null;
185
            Symbol targetSymbol = null;
186
            foreach (var item in run.RUNITEMS)
187
            {
188
                if (item.GetType() == typeof(Symbol))
189
                {
190
                    Symbol symbol = item as Symbol;
191
                    SymbolModeling(symbol, targetSymbol, prevSymbol);
192
                    prevSymbol = symbol;
193
                    targetSymbol = symbol;
194
                }
195
                else
196
                {
197
                    targetSymbol = null;
198
                }
199
            }
200
        }
201

    
202
        private void SymbolModelingByRunStart(Symbol symbol, LineRun run)
203
        {
204
            foreach (var connector in symbol.CONNECTORS)
205
            {
206
                object targetItem = SPPIDUtil.FindObjectByUID(document, connector.CONNECTEDITEM);
207
                if (targetItem != null &&
208
                    (targetItem.GetType() == typeof(Symbol) || targetItem.GetType() == typeof(Equipment)) &&
209
                    !IsSameLineNumber(symbol, targetItem))
210
                {
211
                    SymbolModeling(symbol, targetItem as Symbol, null);
212
                    for (int i = 1; i < run.RUNITEMS.Count; i++)
213
                    {
214
                        object item = run.RUNITEMS[i];
215
                        if (item.GetType() == typeof(Symbol))
216
                            SymbolModeling(item as Symbol, run.RUNITEMS[i - 1] as Symbol, null);
217
                        else
218
                            break;
219
                    }
220
                    break;
221
                }
222
            }
223

    
224

    
225
        }
226

    
227
        private void SymbolModelingByRunEnd(Symbol symbol, LineRun run)
228
        {
229
            foreach (var connector in symbol.CONNECTORS)
230
            {
231
                object targetItem = SPPIDUtil.FindObjectByUID(document, connector.CONNECTEDITEM);
232
                if (targetItem != null &&
233
                    (targetItem.GetType() == typeof(Symbol) || targetItem.GetType() == typeof(Equipment)) &&
234
                    !IsSameLineNumber(symbol, targetItem))
235
                {
236
                    SymbolModeling(symbol, targetItem as Symbol, null);
237
                    for (int i = run.RUNITEMS.Count - 2; i >= 0; i--)
238
                    {
239
                        object item = run.RUNITEMS[i];
240
                        if (item.GetType() == typeof(Symbol))
241
                            SymbolModeling(item as Symbol, run.RUNITEMS[i + 1] as Symbol, null);
242
                        else
243
                            break;
244
                    }
245
                    break;
246
                }
247
            }
248
        }
249

    
250
        private void TestSource()
251
        {
252

    
253
        }
254

    
255
        private void SymbolModeling(Symbol symbol, Symbol prevSymbol, object prevItem, bool IsFirst)
256
        {
257
            if (!string.IsNullOrEmpty(symbol.SPPID.RepresentationId))
258
                return;
259

    
260
            LMSymbol _LMSymbol = null;
261

    
262
            string mappingPath = symbol.SPPID.MAPPINGNAME;
263
            double x = symbol.SPPID.ORIGINAL_X;
264
            double y = symbol.SPPID.ORIGINAL_Y;
265
            int mirror = 0;
266
            double angle = symbol.ANGLE;
267
            LMSymbol _TargetItem = null;
268

    
269
            if (IsFirst)
270
            {
271
                foreach (var connector in symbol.CONNECTORS)
272
                {
273
                    object item = SPPIDUtil.FindObjectByUID(document, connector.CONNECTEDITEM);
274
                    if (item != null && 
275
                        (item.GetType() == typeof(Symbol) || item.GetType() == typeof(Equipment)) && 
276
                        !IsSameLineNumber(symbol, item))
277
                    {
278
                        _TargetItem = dataSource.GetSymbol(((Symbol)item).SPPID.RepresentationId);
279
                        break;
280
                    }
281
                }
282
            }
283
            else if (prevItem != null && prevItem.GetType() == typeof(Symbol))
284
                _TargetItem = dataSource.GetSymbol(((Symbol)prevItem).SPPID.RepresentationId);
285
            
286
            if (prevSymbol != null)
287
            {
288
                SlopeType slopeType = SPPIDUtil.CalcSlope(x, y, prevSymbol.SPPID.ORIGINAL_X, prevSymbol.SPPID.ORIGINAL_Y);
289
                LMSymbol prevLMSymbol = dataSource.GetSymbol(prevSymbol.SPPID.RepresentationId);
290
                double prevX = prevLMSymbol.get_XCoordinate();
291
                double prevY = prevLMSymbol.get_YCoordinate();
292
                if (slopeType == SlopeType.HORIZONTAL)
293
                    y = prevY;
294
                else if (slopeType == SlopeType.VERTICAL)
295
                    x = prevX;
296
                
297
                ReleaseCOMObjects(prevLMSymbol);
298
            }
299

    
300
            if (_TargetItem == null)
301
                _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle);
302
            else
303
                _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle, TargetItem: _TargetItem);
304

    
305
            if (_LMSymbol != null)
306
            {
307
                _LMSymbol.Commit();
308
                symbol.SPPID.RepresentationId = _LMSymbol.AsLMRepresentation().Id;
309
            }
310

    
311
            if (_TargetItem != null)
312
                ReleaseCOMObjects(_TargetItem);
313
            
314
            ReleaseCOMObjects(_LMSymbol);
315
        }
316

    
317
        private void SymbolModeling(Symbol symbol, Symbol targetSymbol, Symbol prevSymbol)
318
        {
319
            if (!string.IsNullOrEmpty(symbol.SPPID.RepresentationId))
320
                return;
321

    
322
            LMSymbol _LMSymbol = null;
323

    
324
            string mappingPath = symbol.SPPID.MAPPINGNAME;
325
            double x = symbol.SPPID.ORIGINAL_X;
326
            double y = symbol.SPPID.ORIGINAL_Y;
327
            int mirror = 0;
328
            double angle = symbol.ANGLE;
329

    
330
            if (targetSymbol != null && !string.IsNullOrEmpty(targetSymbol.SPPID.RepresentationId))
331
            {
332
                LMSymbol _TargetItem = dataSource.GetSymbol(targetSymbol.SPPID.RepresentationId);
333
                _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle, TargetItem: _TargetItem);
334
                ReleaseCOMObjects(_TargetItem);
335
            }
336
            else if (prevSymbol != null)
337
            {
338
                LMSymbol _PrevSymbol = dataSource.GetSymbol(prevSymbol.SPPID.RepresentationId);
339
                SlopeType slopeType = SPPIDUtil.CalcSlope(x, y, prevSymbol.SPPID.ORIGINAL_X, prevSymbol.SPPID.ORIGINAL_Y);
340
                double prevX = _PrevSymbol.get_XCoordinate();
341
                double prevY = _PrevSymbol.get_YCoordinate();
342
                if (slopeType == SlopeType.HORIZONTAL)
343
                    y = prevY;
344
                else if (slopeType == SlopeType.VERTICAL)
345
                    x = prevX;
346
                _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle);
347
            }
348
            else
349
            {
350
                _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle);
351
            }
352

    
353

    
354
            if (_LMSymbol != null)
355
            {
356
                _LMSymbol.Commit();
357
                symbol.SPPID.RepresentationId = _LMSymbol.AsLMRepresentation().Id;
358
            }
359

    
360
            ReleaseCOMObjects(_LMSymbol);
361
        }
362

    
363
        private bool IsSameLineNumber(object item, object targetItem)
364
        {
365
            foreach (var lineNumber in document.LINENUMBERS)
366
            {
367
                foreach (var run in lineNumber.RUNS)
368
                {
369
                    foreach (var runItem in run.RUNITEMS)
370
                    {
371
                        if (runItem == item)
372
                        {
373
                            foreach (var findItem in run.RUNITEMS)
374
                            {
375
                                if (findItem == targetItem)
376
                                {
377
                                    return true;
378
                                }
379
                            }
380

    
381
                            return false;
382

    
383
                        }
384
                    }
385
                }
386
            }
387

    
388
            return false;
389
        }
390

    
391
        private void LineModeling(List<Line> lines)
392
        {
393
            _LMAItem _LMAItem = _placement.PIDCreateItem(lines[0].SPPID.MAPPINGNAME);
394
            PlaceRunInputs placeRunInputs = new PlaceRunInputs();
395
            LMSymbol _LMSymbol1 = null;
396
            LMSymbol _LMSymbol2 = null;
397
            Dictionary<LMConnector, List<double[]>> connectorVertices1 = new Dictionary<LMConnector, List<double[]>>();
398
            LMConnector targetConnector1 = null;
399
            Dictionary<LMConnector, List<double[]>> connectorVertices2 = new Dictionary<LMConnector, List<double[]>>();
400
            LMConnector targetConnector2 = null;
401

    
402
            Line startBranchLine = null;
403
            Line endBranchLine = null;
404

    
405
            for (int i = 0; i < lines.Count; i++)
406
            {
407
                Line line = lines[i];
408
                if (i == 0 || i + 1 != lines.Count)
409
                {
410
                    // 시작점에 연결된 Symbol 찾기
411
                    object connItem = SPPIDUtil.FindObjectByUID(document, line.CONNECTORS[0].CONNECTEDITEM);
412
                    if (connItem != null && connItem.GetType() == typeof(Symbol))
413
                    {
414
                        _LMSymbol1 = dataSource.GetSymbol(((Symbol)connItem).SPPID.RepresentationId);
415
                        if (_LMSymbol1 != null)
416
                            placeRunInputs.AddSymbolTarget(_LMSymbol1, line.SPPID.START_X, line.SPPID.START_Y);
417
                        else
418
                            placeRunInputs.AddPoint(line.SPPID.START_X, line.SPPID.START_Y);
419
                    }
420
                    else if (connItem != null && connItem.GetType() == typeof(Line) && !lines.Contains(connItem))
421
                    {
422
                        connectorVertices1 = GetPipeRunVertices(((Line)connItem).SPPID.ModelItemId);
423
                        targetConnector1 = FindTargetLMConnector(connectorVertices1, line.SPPID.START_X, line.SPPID.START_Y, line.SPPID.END_X, line.SPPID.END_Y);
424

    
425
                        if (targetConnector1 != null)
426
                            placeRunInputs.AddConnectorTarget(targetConnector1, line.SPPID.START_X, line.SPPID.START_Y);
427
                        else
428
                        {
429
                            startBranchLine = connItem as Line;
430
                            placeRunInputs.AddPoint(line.SPPID.START_X, line.SPPID.START_Y);
431
                        }
432
                    }
433
                    else
434
                        placeRunInputs.AddPoint(line.SPPID.START_X, line.SPPID.START_Y);
435
                }
436

    
437
                if (i + 1 == lines.Count)
438
                {
439
                    // 끝점에 연결된 Symbol 찾기
440
                    object connItem = SPPIDUtil.FindObjectByUID(document, line.CONNECTORS[1].CONNECTEDITEM);
441

    
442
                    if (i != 0)
443
                        placeRunInputs.AddPoint(line.SPPID.START_X, line.SPPID.START_Y);
444

    
445
                    if (connItem != null && connItem.GetType() == typeof(Symbol))
446
                    {
447
                        _LMSymbol2 = dataSource.GetSymbol(((Symbol)connItem).SPPID.RepresentationId);
448
                        if (_LMSymbol2 != null)
449
                            placeRunInputs.AddSymbolTarget(_LMSymbol2, line.SPPID.END_X, line.SPPID.END_Y);
450
                        else
451
                            placeRunInputs.AddPoint(line.SPPID.END_X, line.SPPID.END_Y);
452
                            
453
                    }
454
                    else if (connItem != null && connItem.GetType() == typeof(Line) && !lines.Contains(connItem))
455
                    {
456
                        connectorVertices2 = GetPipeRunVertices(((Line)connItem).SPPID.ModelItemId);
457
                        targetConnector2 = FindTargetLMConnector(connectorVertices2, line.SPPID.END_X, line.SPPID.END_Y, line.SPPID.START_X, line.SPPID.START_Y);
458

    
459
                        if (targetConnector2 != null)
460
                            placeRunInputs.AddConnectorTarget(targetConnector2, line.SPPID.END_X, line.SPPID.END_Y);
461
                        else
462
                        {
463
                            endBranchLine = connItem as Line;
464
                            placeRunInputs.AddPoint(line.SPPID.END_X, line.SPPID.END_Y);
465
                        }
466
                    }
467
                    else
468
                    {
469
                        placeRunInputs.AddPoint(line.SPPID.END_X, line.SPPID.END_Y);
470
                    }
471
                }
472
            }
473

    
474
            LMConnector _lMConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
475

    
476
            if (_lMConnector != null)
477
            {
478
                foreach (var line in lines)
479
                    line.SPPID.ModelItemId = _lMConnector.ModelItemID;
480
                _lMConnector.Commit();
481
                if (startBranchLine != null || endBranchLine != null)
482
                {
483
                    BranchLines.Add(new Tuple<string, Line, Line>(_lMConnector.ModelItemID, startBranchLine, endBranchLine));
484
                }
485
            }
486

    
487

    
488
            if (_LMSymbol1 != null)
489
                ReleaseCOMObjects(_LMSymbol1);
490
            if (_LMSymbol2 != null)
491
                ReleaseCOMObjects(_LMSymbol2);
492
            if (targetConnector1 != null)
493
                ReleaseCOMObjects(targetConnector1);
494
            if (targetConnector2 != null)
495
                ReleaseCOMObjects(targetConnector2);
496
            foreach (var item in connectorVertices1)
497
                ReleaseCOMObjects(item.Key);
498
            foreach (var item in connectorVertices2)
499
                ReleaseCOMObjects(item.Key);
500

    
501
            ReleaseCOMObjects(_lMConnector);
502
            ReleaseCOMObjects(placeRunInputs);
503
            ReleaseCOMObjects(_LMAItem);
504
        }
505

    
506
        private void BranchLineModeling(Tuple<string, Line, Line> branch)
507
        {
508
            List<Line> lines = SPPIDUtil.FindLinesByModelId(document, branch.Item1);
509
            Dictionary<LMConnector, List<double[]>> connectorVertices = GetPipeRunVertices(branch.Item1);
510

    
511
            LMConnector _StartConnector = null;
512
            LMConnector _EndConnector = null;
513
            double lengthStart = double.MaxValue;
514
            double lengthEnd = double.MaxValue;
515
            List<double[]> startPoints = new List<double[]>();
516
            List<double[]> endPoints = new List<double[]>();
517

    
518
            foreach (var item in connectorVertices)
519
            {
520
                foreach (var point in item.Value)
521
                {
522
                    // Start Point가 Branch
523
                    if (branch.Item2 != null)
524
                    {
525
                        Line targetLine = branch.Item2;
526
                        double distance = SPPIDUtil.CalcLineToPointDistance(targetLine.SPPID.START_X, targetLine.SPPID.START_Y, targetLine.SPPID.END_X, targetLine.SPPID.END_Y, point[0], point[1]);
527
                        if (lengthStart > distance)
528
                        {
529
                            _StartConnector = item.Key;
530
                            lengthStart = distance;
531
                            startPoints = item.Value;
532
                        }
533
                    }
534
                    // End Point가 Branch
535
                    if (branch.Item3 != null)
536
                    {
537
                        Line targetLine = branch.Item3;
538
                        double distance = SPPIDUtil.CalcLineToPointDistance(targetLine.SPPID.START_X, targetLine.SPPID.START_Y, targetLine.SPPID.END_X, targetLine.SPPID.END_Y, point[0], point[1]);
539
                        if (lengthEnd > distance)
540
                        {
541
                            _EndConnector = item.Key;
542
                            lengthEnd = distance;
543
                            endPoints = item.Value;
544
                        }
545
                    }
546
                }
547
            }
548
            #region Branch가 양쪽 전부일 때
549
            if (_StartConnector != null && _StartConnector == _EndConnector)
550
            {
551
                _placement.PIDRemovePlacement(_StartConnector.AsLMRepresentation());
552

    
553
                _LMAItem _LMAItem = _placement.PIDCreateItem(lines[0].SPPID.MAPPINGNAME);
554
                PlaceRunInputs placeRunInputs = new PlaceRunInputs();
555

    
556
                Dictionary<LMConnector, List<double[]>> startConnectorVertices = GetPipeRunVertices(branch.Item2.SPPID.ModelItemId);
557
                LMConnector _StartTargetConnector = FindTargetLMConnector(startConnectorVertices, startPoints[0][0], startPoints[0][1], startPoints[1][0], startPoints[1][1]);
558
                Dictionary<LMConnector, List<double[]>> endConnectorVertices = GetPipeRunVertices(branch.Item3.SPPID.ModelItemId);
559
                LMConnector _EndTargetConnector = FindTargetLMConnector(endConnectorVertices,
560
                   startPoints[startPoints.Count - 1][0],
561
                   startPoints[startPoints.Count - 1][1],
562
                   startPoints[startPoints.Count - 2][0],
563
                   startPoints[startPoints.Count - 2][1]);
564

    
565
                for (int i = 0; i < startPoints.Count; i++)
566
                {
567
                    double[] point = startPoints[i];
568
                    if (i == 0)
569
                        placeRunInputs.AddConnectorTarget(_StartTargetConnector, point[0], point[1]);
570
                    else if (i == startPoints.Count - 1)
571
                        placeRunInputs.AddConnectorTarget(_EndTargetConnector, point[0], point[1]);
572
                    else
573
                        placeRunInputs.AddPoint(point[0], point[1]);
574
                }
575

    
576
                LMConnector _LMConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
577
                if (_LMConnector != null)
578
                {
579
                    _LMConnector.Commit();
580
                    foreach (var item in lines)
581
                        item.SPPID.ModelItemId = _LMConnector.ModelItemID;
582
                }
583

    
584
                foreach (var item in startConnectorVertices)
585
                    ReleaseCOMObjects(item.Key);
586
                foreach (var item in endConnectorVertices)
587
                    ReleaseCOMObjects(item.Key);
588
                ReleaseCOMObjects(placeRunInputs);
589
                ReleaseCOMObjects(_LMAItem);
590
                ReleaseCOMObjects(_LMConnector);
591
            }
592
            #endregion
593
            #region 양쪽이 다른 Branch 
594
            else
595
            {
596
                _LMAItem _LMAItem = _placement.PIDCreateItem(lines[0].SPPID.MAPPINGNAME);
597
                PlaceRunInputs placeRunInputs = new PlaceRunInputs();
598

    
599
                // Branch 시작 Connector
600
                if (_StartConnector != null)
601
                {
602
                    LMConnector _EndTargetConnector = null;
603
                    LMSymbol _EndTargetSymbol = null;
604
                    // Branch 반대편이 Line
605
                    foreach (var item in connectorVertices)
606
                    {
607
                        if (item.Key == _StartConnector)
608
                            continue;
609

    
610
                        if (item.Key.ConnectItem1SymbolID == _StartConnector.ConnectItem2SymbolID)
611
                        {
612
                            _EndTargetConnector = item.Key;
613
                            break;
614
                        }
615
                    }
616
                    // Branch 반대편이 Symbol
617
                    if (_EndTargetConnector == null)
618
                    {
619
                        foreach (var line in lines)
620
                        {
621
                            foreach (var connector in line.CONNECTORS)
622
                            {
623
                                Symbol symbol = SPPIDUtil.FindObjectByUID(document, connector.CONNECTEDITEM) as Symbol;
624
                                if (symbol != null)
625
                                {
626
                                    _EndTargetSymbol = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
627
                                    break;
628
                                }
629
                            }
630
                        }
631
                    }
632

    
633
                    _placement.PIDRemovePlacement(_StartConnector.AsLMRepresentation());
634

    
635
                    Dictionary<LMConnector, List<double[]>> startConnectorVertices = GetPipeRunVertices(branch.Item2.SPPID.ModelItemId);
636
                    LMConnector _StartTargetConnector = FindTargetLMConnector(startConnectorVertices, startPoints[0][0], startPoints[0][1], startPoints[1][0], startPoints[1][1]);
637

    
638
                    for (int i = 0; i < startPoints.Count; i++)
639
                    {
640
                        double[] point = startPoints[i];
641
                        if (i == 0)
642
                            placeRunInputs.AddConnectorTarget(_StartTargetConnector, point[0], point[1]);
643
                        else if (i == startPoints.Count - 1)
644
                        {
645
                            if (_EndTargetConnector != null)
646
                                placeRunInputs.AddConnectorTarget(_EndTargetConnector, point[0], point[1]);
647
                            else if (_EndTargetSymbol != null)
648
                                placeRunInputs.AddSymbolTarget(_EndTargetSymbol, point[0], point[1]);
649
                            else
650
                                placeRunInputs.AddPoint(point[0], point[1]);
651
                        }
652
                        else
653
                            placeRunInputs.AddPoint(point[0], point[1]);
654
                    }
655

    
656
                    LMConnector _LMConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
657
                    if (_LMConnector != null )
658
                    {
659
                        if (_EndTargetConnector != null)
660
                        {
661
                            JoinPipeRun(_LMConnector.ModelItemID, _EndTargetConnector.ModelItemID);
662
                        }
663
                        else
664
                        {
665
                            foreach (var item in lines)
666
                                item.SPPID.ModelItemId = _LMConnector.ModelItemID;
667
                        }
668

    
669
                        _LMConnector.Commit();
670
                        ReleaseCOMObjects(_LMConnector);
671
                    }
672

    
673
                    if (_EndTargetConnector != null)
674
                        ReleaseCOMObjects(_EndTargetConnector);
675
                    if (_EndTargetSymbol != null)
676
                        ReleaseCOMObjects(_EndTargetSymbol);
677
                    foreach (var item in startConnectorVertices)
678
                        ReleaseCOMObjects(item.Key);
679

    
680
                }
681
                // Branch 끝 Connector
682
                if (_EndConnector != null)
683
                {
684
                    LMConnector _StartTargetConnector = null;
685
                    LMSymbol _StartTargetSymbol = null;
686
                    // Branch 반대편이 Line
687
                    foreach (var item in connectorVertices)
688
                    {
689
                        if (item.Key == _EndConnector)
690
                            continue;
691

    
692
                        if (item.Key.ConnectItem2SymbolID == _EndConnector.ConnectItem1SymbolID)
693
                        {
694
                            _StartTargetConnector = item.Key;
695
                            break;
696
                        }
697
                    }
698
                    // Branch 반대편이 Symbol
699
                    if (_StartTargetConnector == null)
700
                    {
701
                        foreach (var line in lines)
702
                        {
703
                            foreach (var connector in line.CONNECTORS)
704
                            {
705
                                Symbol symbol = SPPIDUtil.FindObjectByUID(document, connector.CONNECTEDITEM) as Symbol;
706
                                if (symbol != null)
707
                                {
708
                                    _StartTargetSymbol = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
709
                                    break;
710
                                }
711
                            }
712
                        }
713
                    }
714

    
715
                    _placement.PIDRemovePlacement(_EndConnector.AsLMRepresentation());
716

    
717
                    Dictionary<LMConnector, List<double[]>> startConnectorVertices = GetPipeRunVertices(branch.Item3.SPPID.ModelItemId);
718
                    LMConnector _EndTargetConnector = FindTargetLMConnector(startConnectorVertices, 
719
                        endPoints[endPoints.Count - 1][0], 
720
                        endPoints[endPoints.Count - 1][1], 
721
                        endPoints[endPoints.Count - 2][0], 
722
                        endPoints[endPoints.Count - 2][1]);
723

    
724
                    for (int i = 0; i < endPoints.Count; i++)
725
                    {
726
                        double[] point = endPoints[i];
727
                        if (i == 0)
728
                        {
729
                            if (_StartTargetConnector != null)
730
                                placeRunInputs.AddConnectorTarget(_StartTargetConnector, point[0], point[1]);
731
                            else if (_StartTargetSymbol != null)
732
                                placeRunInputs.AddSymbolTarget(_StartTargetSymbol, point[0], point[1]);
733
                            else
734
                                placeRunInputs.AddPoint(point[0], point[1]);
735
                        }
736
                        else if (i == endPoints.Count - 1)
737
                        {
738
                            placeRunInputs.AddConnectorTarget(_EndTargetConnector, point[0], point[1]);
739
                        }
740
                        else
741
                            placeRunInputs.AddPoint(point[0], point[1]);
742
                    }
743

    
744
                    LMConnector _LMConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
745
                    if (_LMConnector != null)
746
                    {
747
                        if (_StartTargetConnector != null)
748
                        {
749
                            JoinPipeRun(_LMConnector.ModelItemID, _StartTargetConnector.ModelItemID);
750
                        }
751
                        else
752
                        {
753
                            foreach (var item in lines)
754
                                item.SPPID.ModelItemId = _LMConnector.ModelItemID;
755
                        }
756

    
757
                        _LMConnector.Commit();
758
                        ReleaseCOMObjects(_LMConnector);
759
                    }
760

    
761
                    if (_StartTargetConnector != null)
762
                        ReleaseCOMObjects(_StartTargetConnector);
763
                    if (_StartTargetSymbol != null)
764
                        ReleaseCOMObjects(_StartTargetSymbol);
765
                    foreach (var item in startConnectorVertices)
766
                        ReleaseCOMObjects(item.Key);
767
                }
768

    
769
                ReleaseCOMObjects(_LMAItem);
770
                ReleaseCOMObjects(placeRunInputs);
771
            }
772
            #endregion
773

    
774
            if (_StartConnector != null)
775
                ReleaseCOMObjects(_StartConnector);
776
            if (_EndConnector != null)
777
                ReleaseCOMObjects(_EndConnector);
778
            foreach (var item in connectorVertices)
779
                ReleaseCOMObjects(item.Key);
780

    
781

    
782

    
783
            //if (!string.IsNullOrEmpty(line.SPPID.ModelItemId) && !string.IsNullOrEmpty(targetLine.SPPID.ModelItemId))
784
            //{
785

    
786

    
787

    
788
            //}
789
        }
790

    
791
        private void EndBreakModeling(EndBreak endBreak)
792
        {
793
            Line ownerLine = SPPIDUtil.FindObjectByUID(document, endBreak.OWNER) as Line;
794
            Line connLine = null;   
795
            Property property = endBreak.PROPERTIES.Find(x => x.ATTRIBUTE == "Connected Item");
796
            if (property != null)
797
                connLine = SPPIDUtil.FindObjectByUID(document, property.VALUE) as Line;
798

    
799
            if (ownerLine != null && connLine != null)
800
            {
801
                LMLabelPersist _LmLabelPersist = null;
802

    
803
                string lineModelId = ownerLine.SPPID.ModelItemId;
804
                LMPipeRun _LMPipeRun = _placement.PIDDataSource.GetPipeRun(lineModelId);
805
                LMPipeRun _ConnLMPipeRun = _placement.PIDDataSource.GetPipeRun(connLine.SPPID.ModelItemId);
806
                LMConnector connectedLMConnector = null;
807

    
808
                if (_LMPipeRun != null && _ConnLMPipeRun != null)
809
                {
810
                    foreach (LMRepresentation rep in _LMPipeRun.Representations)
811
                    {
812
                        if (rep.Attributes["RepresentationType"].get_Value() == "Connector" && rep.Attributes["ItemStatus"].get_Value() == "Active")
813
                        {
814
                            LMConnector _LMConnector = _placement.PIDDataSource.GetConnector(rep.Id);
815
                            if (_LMConnector.ConnectItem1SymbolID != null && !DBNull.Value.Equals(_LMConnector.ConnectItem1SymbolID) && ExistConnItem(_ConnLMPipeRun, _LMConnector.ConnectItem1SymbolID))
816
                            {
817
                                connectedLMConnector = _LMConnector;
818
                                break;
819
                            }
820
                            else if (_LMConnector.ConnectItem2SymbolID != null && !DBNull.Value.Equals(_LMConnector.ConnectItem2SymbolID) && ExistConnItem(_ConnLMPipeRun, _LMConnector.ConnectItem2SymbolID))
821
                            {
822
                                connectedLMConnector = _LMConnector;
823
                                break;
824
                            }
825
                            else
826
                            {
827
                                ReleaseCOMObjects(_LMConnector);
828
                            }
829
                        }
830
                    }
831
                    if (connectedLMConnector != null)
832
                    {
833
                        Array array = new double[] { 0, endBreak.SPPID.ORIGINAL_X, endBreak.SPPID.ORIGINAL_Y };
834
                        _LmLabelPersist = _placement.PIDPlaceLabel(endBreak.SPPID.MAPPINGNAME, ref array, Rotation: 0, LabeledItem: connectedLMConnector.AsLMRepresentation(), IsLeaderVisible: true);
835
                        ReleaseCOMObjects(connectedLMConnector);
836
                    }
837

    
838
                    ReleaseCOMObjects(_LMPipeRun);
839
                    ReleaseCOMObjects(_ConnLMPipeRun);
840
                    if (_LmLabelPersist != null)
841
                        ReleaseCOMObjects(_LmLabelPersist);
842
                }
843
            }
844
        }
845
        private bool ExistConnItem(LMPipeRun _LMPipeRun, string ConnItem)
846
        {
847
            foreach (LMRepresentation rep in _LMPipeRun.Representations)
848
            {
849
                if (rep.Attributes["RepresentationType"].get_Value() == "Connector" && rep.Attributes["ItemStatus"].get_Value() == "Active")
850
                {
851
                    LMConnector _LMConnector = dataSource.GetConnector(rep.Id);
852
                    if (_LMConnector.ConnectItem1SymbolID != null && !DBNull.Value.Equals(_LMConnector.ConnectItem1SymbolID) && _LMConnector.ConnectItem1SymbolID == ConnItem)
853
                    {
854
                        ReleaseCOMObjects(_LMConnector);
855
                        return true;
856
                    }
857
                    else if (_LMConnector.ConnectItem2SymbolID != null && !DBNull.Value.Equals(_LMConnector.ConnectItem2SymbolID) && _LMConnector.ConnectItem2SymbolID == ConnItem)
858
                    {
859
                        ReleaseCOMObjects(_LMConnector);
860
                        return true;
861
                    }
862
                    else
863
                    {
864
                        ReleaseCOMObjects(_LMConnector);
865
                    }
866
                }
867
            }
868

    
869
            return false;
870
        }
871

    
872
        private void JoinPipeRun(string fromModelItemId, string toModelItemId)
873
        {
874
            LMPipeRun run1 = dataSource.GetPipeRun(toModelItemId);
875
            LMPipeRun run2 = dataSource.GetPipeRun(fromModelItemId);
876
            // item2가 item1으로 조인
877
            try
878
            {
879
                _LMAItem item1 = run1.AsLMAItem();
880
                _LMAItem item2 = run2.AsLMAItem();
881

    
882
                _placement.PIDJoinRuns(ref item1, ref item2);
883
                List<Line> lines = SPPIDUtil.FindLinesByModelId(document, fromModelItemId);
884
                foreach (var line in lines)
885
                    line.SPPID.ModelItemId = toModelItemId;
886

    
887
            }
888
            catch (Exception ex)
889
            {
890
                System.Windows.Forms.MessageBox.Show(ex.Message + "\r\n" + ex.StackTrace);
891
            }
892
            finally
893
            {
894
                ReleaseCOMObjects(run1);
895
                ReleaseCOMObjects(run2);
896
            }
897
        }
898

    
899
        private void AutoJoinPipeRun(string modelItemId)
900
        {
901
            LMPipeRun run = dataSource.GetPipeRun(modelItemId);
902
            _LMAItem item = run.AsLMAItem();
903
            _placement.PIDAutoJoin(item, AutoJoinEndConstants.autoJoin_Both, ref item);
904
        }
905

    
906
        private void JoinRunLine(LineRun run)
907
        {
908
            string modelItemId = string.Empty;
909
            foreach (var item in run.RUNITEMS)
910
            {
911
                if (item.GetType() == typeof(Line))
912
                {
913
                    Line line = item as Line;
914
                    if (modelItemId != line.SPPID.ModelItemId)
915
                    {
916
                        AutoJoinPipeRun(line.SPPID.ModelItemId);
917
                        modelItemId = line.SPPID.ModelItemId;
918
                    }
919
                    //if (string.IsNullOrEmpty(modelItemId))
920
                    //{
921
                    //    modelItemId = line.SPPID.ModelItemId;
922
                    //}
923
                    //else if(modelItemId != line.SPPID.ModelItemId)
924
                    //{
925
                    //    JoinPipeRun(line.SPPID.ModelItemId, modelItemId);
926
                    //}
927
                }
928
            }
929
        }
930

    
931
        private Dictionary<LMConnector, List<double[]>> GetPipeRunVertices(string modelId)
932
        {
933
            Dictionary<LMConnector, List<double[]>> connectorVertices = new Dictionary<LMConnector, List<double[]>>();
934
            LMPipeRun _LMPipeRun = dataSource.GetPipeRun(modelId);
935
            if (_LMPipeRun != null)
936
            {
937
                foreach (LMRepresentation rep in _LMPipeRun.Representations)
938
                {
939
                    if (rep.Attributes["RepresentationType"].get_Value() == "Connector" && rep.Attributes["ItemStatus"].get_Value() == "Active")
940
                    {
941
                        LMConnector _LMConnector = dataSource.GetConnector(rep.Id);
942
                        connectorVertices.Add(_LMConnector, new List<double[]>());
943
                        dynamic OID = rep.get_GraphicOID();
944
                        DependencyObject drawingObject = radApp.ActiveDocument.ActiveSheet.DrawingObjects[OID];
945
                        Ingr.RAD2D.LineStringGeometry2d lineStringGeometry = drawingObject.GetGeometry() as Ingr.RAD2D.LineStringGeometry2d;
946
                        int verticesCount = lineStringGeometry.VertexCount;
947
                        double[] vertices = null;
948
                        lineStringGeometry.GetVertices(ref verticesCount, ref vertices);
949
                        for (int i = 0; i < verticesCount; i++)
950
                        {
951
                            double x = 0;
952
                            double y = 0;
953
                            lineStringGeometry.GetVertex(i + 1, ref x, ref y);
954
                            connectorVertices[_LMConnector].Add(new double[] { Math.Round(x, 10), Math.Round(y, 10) });
955
                        }
956
                    }
957
                }
958

    
959
                ReleaseCOMObjects(_LMPipeRun);
960
            }
961

    
962
            return connectorVertices;
963
        }
964

    
965
        private LMConnector FindTargetLMConnector(Dictionary<LMConnector, List<double[]>> connectorVertices, double connX, double connY, double x2, double y2)
966
        {
967
            double length = double.MaxValue;
968
            LMConnector targetConnector = null;
969
            foreach (var item in connectorVertices)
970
            {
971
                List<double[]> points = item.Value;
972
                for (int i = 0; i < points.Count - 1; i++)
973
                {
974
                    double[] point1 = points[i];
975
                    double[] point2 = points[i + 1];
976

    
977
                    double maxLineX = Math.Max(point1[0], point2[0]);
978
                    double minLineX = Math.Min(point1[0], point2[0]);
979
                    double maxLineY = Math.Max(point1[1], point2[1]);
980
                    double minLineY = Math.Min(point1[1], point2[1]);
981

    
982
                    SlopeType slope = SPPIDUtil.CalcSlope(minLineX, minLineY, maxLineX, maxLineY);
983

    
984
                    // 두직선의 교차점
985
                    double[] crossingPoint = SPPIDUtil.CalcLineCrossingPoint(connX, connY, x2, y2, point1[0], point1[1], point2[0], point2[1]);
986
                    if (crossingPoint != null)
987
                    {
988
                        double distance = SPPIDUtil.CalcPointToPointdDistance(connX, connY, crossingPoint[0], crossingPoint[1]);
989
                        if (length >= distance)
990
                        {
991
                            if (slope == SlopeType.Slope &&
992
                                minLineX <= crossingPoint[0] && maxLineX >= crossingPoint[0] &&
993
                                minLineY <= crossingPoint[1] && maxLineY >= crossingPoint[1])
994
                            {
995
                                targetConnector = item.Key;
996
                                length = distance;
997
                            }
998
                            else if (slope == SlopeType.HORIZONTAL &&
999
                                minLineX <= crossingPoint[0] && maxLineX >= crossingPoint[0])
1000
                            {
1001
                                targetConnector = item.Key;
1002
                                length = distance;
1003
                            }
1004
                            else if (slope == SlopeType.VERTICAL &&
1005
                               minLineY <= crossingPoint[1] && maxLineY >= crossingPoint[1])
1006
                            {
1007
                                targetConnector = item.Key;
1008
                                length = distance;
1009
                            }
1010
                        }
1011
                    }
1012
                }
1013
            }
1014

    
1015
            return targetConnector;
1016
        }
1017

    
1018
        private void LineNumberModeling(LineNumber lineNumber)
1019
        {
1020

    
1021
        }
1022

    
1023
        private void TextModeling(Text text)
1024
        {
1025

    
1026
        }
1027

    
1028
        private void NoteModeling(Note note)
1029
        {
1030

    
1031
        }
1032

    
1033

    
1034
        public void ReleaseCOMObjects(params object[] objVars)
1035
        {
1036
            int intNewRefCount = 0;
1037
            foreach (object obj in objVars)
1038
            {
1039
                if (!Information.IsNothing(obj) && System.Runtime.InteropServices.Marshal.IsComObject(obj))
1040
                    intNewRefCount = intNewRefCount + System.Runtime.InteropServices.Marshal.FinalReleaseComObject(obj);
1041
            }
1042
        }
1043
    }
1044
}
클립보드 이미지 추가 (최대 크기: 500 MB)