프로젝트

일반

사용자정보

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

hytos / DTI_PID / SPPIDConverter / AutoModeling.cs @ 10872260

이력 | 보기 | 이력해설 | 다운로드 (45.5 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
                foreach (var item in document.LINENUMBERS)
100
                {
101
                    LineNumberModeling(item);
102
                }
103

    
104

    
105

    
106
                foreach (LineNumber lineNumber in document.LINENUMBERS)
107
                {
108

    
109
                    foreach (LineRun run in lineNumber.RUNS)
110
                    {
111
                        JoinRunLine(run);
112
                    }
113
                }
114

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

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

    
160
                    prevLine = line;
161
                }
162
                // Symbol 일 경우
163
                else if (item.GetType() == typeof(Symbol))
164
                {
165
                    if (lines.Count > 0)
166
                    {
167
                        LineModeling(lines);
168
                        lines.Clear();
169
                    }
170
                }
171
            }
172

    
173
            if (lines.Count > 0)
174
                LineModeling(lines);
175
        }
176

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

    
185
                if (run.RUNITEMS[run.RUNITEMS.Count - 1].GetType() == typeof(Symbol))
186
                    SymbolModelingByRunEnd(run.RUNITEMS[run.RUNITEMS.Count - 1] as Symbol, run);
187
            }
188

    
189
            Symbol prevSymbol = null;
190
            Symbol targetSymbol = null;
191
            foreach (var item in run.RUNITEMS)
192
            {
193
                if (item.GetType() == typeof(Symbol))
194
                {
195
                    Symbol symbol = item as Symbol;
196
                    SymbolModeling(symbol, targetSymbol, prevSymbol);
197
                    prevSymbol = symbol;
198
                    targetSymbol = symbol;
199
                }
200
                else
201
                {
202
                    targetSymbol = null;
203
                }
204
            }
205
        }
206

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

    
229

    
230
        }
231

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

    
255
        private void TestSource()
256
        {
257

    
258
        }
259

    
260
        private void SymbolModeling(Symbol symbol, Symbol prevSymbol, object prevItem, bool IsFirst)
261
        {
262
            if (!string.IsNullOrEmpty(symbol.SPPID.RepresentationId))
263
                return;
264

    
265
            LMSymbol _LMSymbol = null;
266

    
267
            string mappingPath = symbol.SPPID.MAPPINGNAME;
268
            double x = symbol.SPPID.ORIGINAL_X;
269
            double y = symbol.SPPID.ORIGINAL_Y;
270
            int mirror = 0;
271
            double angle = symbol.ANGLE;
272
            LMSymbol _TargetItem = null;
273

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

    
305
            if (_TargetItem == null)
306
                _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle);
307
            else
308
                _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle, TargetItem: _TargetItem);
309

    
310
            if (_LMSymbol != null)
311
            {
312
                _LMSymbol.Commit();
313
                symbol.SPPID.RepresentationId = _LMSymbol.AsLMRepresentation().Id;
314
            }
315

    
316
            if (_TargetItem != null)
317
                ReleaseCOMObjects(_TargetItem);
318
            
319
            ReleaseCOMObjects(_LMSymbol);
320
        }
321

    
322
        private void SymbolModeling(Symbol symbol, Symbol targetSymbol, Symbol prevSymbol)
323
        {
324
            if (!string.IsNullOrEmpty(symbol.SPPID.RepresentationId))
325
                return;
326

    
327
            LMSymbol _LMSymbol = null;
328

    
329
            string mappingPath = symbol.SPPID.MAPPINGNAME;
330
            double x = symbol.SPPID.ORIGINAL_X;
331
            double y = symbol.SPPID.ORIGINAL_Y;
332
            int mirror = 0;
333
            double angle = symbol.ANGLE;
334

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

    
358

    
359
            if (_LMSymbol != null)
360
            {
361
                _LMSymbol.Commit();
362
                symbol.SPPID.RepresentationId = _LMSymbol.AsLMRepresentation().Id;
363
            }
364

    
365
            ReleaseCOMObjects(_LMSymbol);
366
        }
367

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

    
386
                            return false;
387

    
388
                        }
389
                    }
390
                }
391
            }
392

    
393
            return false;
394
        }
395

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

    
407
            Line startBranchLine = null;
408
            Line endBranchLine = null;
409

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

    
430
                        if (targetConnector1 != null)
431
                            placeRunInputs.AddConnectorTarget(targetConnector1, line.SPPID.START_X, line.SPPID.START_Y);
432
                        else
433
                        {
434
                            startBranchLine = connItem as Line;
435
                            placeRunInputs.AddPoint(line.SPPID.START_X, line.SPPID.START_Y);
436
                        }
437
                    }
438
                    else
439
                        placeRunInputs.AddPoint(line.SPPID.START_X, line.SPPID.START_Y);
440
                }
441

    
442
                if (i + 1 == lines.Count)
443
                {
444
                    // 끝점에 연결된 Symbol 찾기
445
                    object connItem = SPPIDUtil.FindObjectByUID(document, line.CONNECTORS[1].CONNECTEDITEM);
446

    
447
                    if (i != 0)
448
                        placeRunInputs.AddPoint(line.SPPID.START_X, line.SPPID.START_Y);
449

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

    
464
                        if (targetConnector2 != null)
465
                            placeRunInputs.AddConnectorTarget(targetConnector2, line.SPPID.END_X, line.SPPID.END_Y);
466
                        else
467
                        {
468
                            endBranchLine = connItem as Line;
469
                            placeRunInputs.AddPoint(line.SPPID.END_X, line.SPPID.END_Y);
470
                        }
471
                    }
472
                    else
473
                    {
474
                        placeRunInputs.AddPoint(line.SPPID.END_X, line.SPPID.END_Y);
475
                    }
476
                }
477
            }
478

    
479
            LMConnector _lMConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
480

    
481
            if (_lMConnector != null)
482
            {
483
                foreach (var line in lines)
484
                    line.SPPID.ModelItemId = _lMConnector.ModelItemID;
485
                _lMConnector.Commit();
486
                if (startBranchLine != null || endBranchLine != null)
487
                {
488
                    BranchLines.Add(new Tuple<string, Line, Line>(_lMConnector.ModelItemID, startBranchLine, endBranchLine));
489
                }
490
            }
491

    
492

    
493
            if (_LMSymbol1 != null)
494
                ReleaseCOMObjects(_LMSymbol1);
495
            if (_LMSymbol2 != null)
496
                ReleaseCOMObjects(_LMSymbol2);
497
            if (targetConnector1 != null)
498
                ReleaseCOMObjects(targetConnector1);
499
            if (targetConnector2 != null)
500
                ReleaseCOMObjects(targetConnector2);
501
            foreach (var item in connectorVertices1)
502
                ReleaseCOMObjects(item.Key);
503
            foreach (var item in connectorVertices2)
504
                ReleaseCOMObjects(item.Key);
505

    
506
            ReleaseCOMObjects(_lMConnector);
507
            ReleaseCOMObjects(placeRunInputs);
508
            ReleaseCOMObjects(_LMAItem);
509
        }
510

    
511
        private void BranchLineModeling(Tuple<string, Line, Line> branch)
512
        {
513
            List<Line> lines = SPPIDUtil.FindLinesByModelId(document, branch.Item1);
514
            Dictionary<LMConnector, List<double[]>> connectorVertices = GetPipeRunVertices(branch.Item1);
515

    
516
            LMConnector _StartConnector = null;
517
            LMConnector _EndConnector = null;
518
            double lengthStart = double.MaxValue;
519
            double lengthEnd = double.MaxValue;
520
            List<double[]> startPoints = new List<double[]>();
521
            List<double[]> endPoints = new List<double[]>();
522

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

    
558
                _LMAItem _LMAItem = _placement.PIDCreateItem(lines[0].SPPID.MAPPINGNAME);
559
                PlaceRunInputs placeRunInputs = new PlaceRunInputs();
560

    
561
                Dictionary<LMConnector, List<double[]>> startConnectorVertices = GetPipeRunVertices(branch.Item2.SPPID.ModelItemId);
562
                LMConnector _StartTargetConnector = FindTargetLMConnector(startConnectorVertices, startPoints[0][0], startPoints[0][1], startPoints[1][0], startPoints[1][1]);
563
                Dictionary<LMConnector, List<double[]>> endConnectorVertices = GetPipeRunVertices(branch.Item3.SPPID.ModelItemId);
564
                LMConnector _EndTargetConnector = FindTargetLMConnector(endConnectorVertices,
565
                   startPoints[startPoints.Count - 1][0],
566
                   startPoints[startPoints.Count - 1][1],
567
                   startPoints[startPoints.Count - 2][0],
568
                   startPoints[startPoints.Count - 2][1]);
569

    
570
                for (int i = 0; i < startPoints.Count; i++)
571
                {
572
                    double[] point = startPoints[i];
573
                    if (i == 0)
574
                        placeRunInputs.AddConnectorTarget(_StartTargetConnector, point[0], point[1]);
575
                    else if (i == startPoints.Count - 1)
576
                        placeRunInputs.AddConnectorTarget(_EndTargetConnector, point[0], point[1]);
577
                    else
578
                        placeRunInputs.AddPoint(point[0], point[1]);
579
                }
580

    
581
                LMConnector _LMConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
582
                if (_LMConnector != null)
583
                {
584
                    _LMConnector.Commit();
585
                    foreach (var item in lines)
586
                        item.SPPID.ModelItemId = _LMConnector.ModelItemID;
587
                }
588

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

    
604
                // Branch 시작 Connector
605
                if (_StartConnector != null)
606
                {
607
                    LMConnector _EndTargetConnector = null;
608
                    LMSymbol _EndTargetSymbol = null;
609
                    // Branch 반대편이 Line
610
                    foreach (var item in connectorVertices)
611
                    {
612
                        if (item.Key == _StartConnector)
613
                            continue;
614

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

    
638
                    _placement.PIDRemovePlacement(_StartConnector.AsLMRepresentation());
639

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

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

    
661
                    LMConnector _LMConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
662
                    if (_LMConnector != null )
663
                    {
664
                        if (_EndTargetConnector != null)
665
                        {
666
                            JoinPipeRun(_LMConnector.ModelItemID, _EndTargetConnector.ModelItemID);
667
                        }
668
                        else
669
                        {
670
                            foreach (var item in lines)
671
                                item.SPPID.ModelItemId = _LMConnector.ModelItemID;
672
                        }
673

    
674
                        _LMConnector.Commit();
675
                        ReleaseCOMObjects(_LMConnector);
676
                    }
677

    
678
                    if (_EndTargetConnector != null)
679
                        ReleaseCOMObjects(_EndTargetConnector);
680
                    if (_EndTargetSymbol != null)
681
                        ReleaseCOMObjects(_EndTargetSymbol);
682
                    foreach (var item in startConnectorVertices)
683
                        ReleaseCOMObjects(item.Key);
684

    
685
                }
686
                // Branch 끝 Connector
687
                if (_EndConnector != null)
688
                {
689
                    LMConnector _StartTargetConnector = null;
690
                    LMSymbol _StartTargetSymbol = null;
691
                    // Branch 반대편이 Line
692
                    foreach (var item in connectorVertices)
693
                    {
694
                        if (item.Key == _EndConnector)
695
                            continue;
696

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

    
720
                    _placement.PIDRemovePlacement(_EndConnector.AsLMRepresentation());
721

    
722
                    Dictionary<LMConnector, List<double[]>> startConnectorVertices = GetPipeRunVertices(branch.Item3.SPPID.ModelItemId);
723
                    LMConnector _EndTargetConnector = FindTargetLMConnector(startConnectorVertices, 
724
                        endPoints[endPoints.Count - 1][0], 
725
                        endPoints[endPoints.Count - 1][1], 
726
                        endPoints[endPoints.Count - 2][0], 
727
                        endPoints[endPoints.Count - 2][1]);
728

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

    
749
                    LMConnector _LMConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
750
                    if (_LMConnector != null)
751
                    {
752
                        if (_StartTargetConnector != null)
753
                        {
754
                            JoinPipeRun(_LMConnector.ModelItemID, _StartTargetConnector.ModelItemID);
755
                        }
756
                        else
757
                        {
758
                            foreach (var item in lines)
759
                                item.SPPID.ModelItemId = _LMConnector.ModelItemID;
760
                        }
761

    
762
                        _LMConnector.Commit();
763
                        ReleaseCOMObjects(_LMConnector);
764
                    }
765

    
766
                    if (_StartTargetConnector != null)
767
                        ReleaseCOMObjects(_StartTargetConnector);
768
                    if (_StartTargetSymbol != null)
769
                        ReleaseCOMObjects(_StartTargetSymbol);
770
                    foreach (var item in startConnectorVertices)
771
                        ReleaseCOMObjects(item.Key);
772
                }
773

    
774
                ReleaseCOMObjects(_LMAItem);
775
                ReleaseCOMObjects(placeRunInputs);
776
            }
777
            #endregion
778

    
779
            if (_StartConnector != null)
780
                ReleaseCOMObjects(_StartConnector);
781
            if (_EndConnector != null)
782
                ReleaseCOMObjects(_EndConnector);
783
            foreach (var item in connectorVertices)
784
                ReleaseCOMObjects(item.Key);
785

    
786

    
787

    
788
            //if (!string.IsNullOrEmpty(line.SPPID.ModelItemId) && !string.IsNullOrEmpty(targetLine.SPPID.ModelItemId))
789
            //{
790

    
791

    
792

    
793
            //}
794
        }
795

    
796
        private void EndBreakModeling(EndBreak endBreak)
797
        {
798
            Line ownerLine = SPPIDUtil.FindObjectByUID(document, endBreak.OWNER) as Line;
799
            Line connLine = null;   
800
            Property property = endBreak.PROPERTIES.Find(x => x.ATTRIBUTE == "Connected Item");
801
            if (property != null)
802
                connLine = SPPIDUtil.FindObjectByUID(document, property.VALUE) as Line;
803

    
804
            if (ownerLine != null && connLine != null)
805
            {
806
                LMLabelPersist _LmLabelPersist = null;
807

    
808
                string lineModelId = ownerLine.SPPID.ModelItemId;
809
                LMPipeRun _LMPipeRun = _placement.PIDDataSource.GetPipeRun(lineModelId);
810
                LMPipeRun _ConnLMPipeRun = _placement.PIDDataSource.GetPipeRun(connLine.SPPID.ModelItemId);
811
                LMConnector connectedLMConnector = null;
812

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

    
843
                    ReleaseCOMObjects(_LMPipeRun);
844
                    ReleaseCOMObjects(_ConnLMPipeRun);
845
                    if (_LmLabelPersist != null)
846
                        ReleaseCOMObjects(_LmLabelPersist);
847
                }
848
            }
849
        }
850

    
851
        private bool ExistConnItem(LMPipeRun _LMPipeRun, string ConnItem)
852
        {
853
            foreach (LMRepresentation rep in _LMPipeRun.Representations)
854
            {
855
                if (rep.Attributes["RepresentationType"].get_Value() == "Connector" && rep.Attributes["ItemStatus"].get_Value() == "Active")
856
                {
857
                    LMConnector _LMConnector = dataSource.GetConnector(rep.Id);
858
                    if (_LMConnector.ConnectItem1SymbolID != null && !DBNull.Value.Equals(_LMConnector.ConnectItem1SymbolID) && _LMConnector.ConnectItem1SymbolID == ConnItem)
859
                    {
860
                        ReleaseCOMObjects(_LMConnector);
861
                        return true;
862
                    }
863
                    else if (_LMConnector.ConnectItem2SymbolID != null && !DBNull.Value.Equals(_LMConnector.ConnectItem2SymbolID) && _LMConnector.ConnectItem2SymbolID == ConnItem)
864
                    {
865
                        ReleaseCOMObjects(_LMConnector);
866
                        return true;
867
                    }
868
                    else
869
                    {
870
                        ReleaseCOMObjects(_LMConnector);
871
                    }
872
                }
873
            }
874

    
875
            return false;
876
        }
877

    
878
        private void JoinPipeRun(string fromModelItemId, string toModelItemId)
879
        {
880
            LMPipeRun run1 = dataSource.GetPipeRun(toModelItemId);
881
            LMPipeRun run2 = dataSource.GetPipeRun(fromModelItemId);
882
            // item2가 item1으로 조인
883
            try
884
            {
885
                _LMAItem item1 = run1.AsLMAItem();
886
                _LMAItem item2 = run2.AsLMAItem();
887

    
888
                _placement.PIDJoinRuns(ref item1, ref item2);
889
                List<Line> lines = SPPIDUtil.FindLinesByModelId(document, fromModelItemId);
890
                foreach (var line in lines)
891
                    line.SPPID.ModelItemId = toModelItemId;
892

    
893
            }
894
            catch (Exception ex)
895
            {
896
                System.Windows.Forms.MessageBox.Show(ex.Message + "\r\n" + ex.StackTrace);
897
            }
898
            finally
899
            {
900
                ReleaseCOMObjects(run1);
901
                ReleaseCOMObjects(run2);
902
            }
903
        }
904

    
905
        private void AutoJoinPipeRun(string modelItemId)
906
        {
907
            LMPipeRun run = dataSource.GetPipeRun(modelItemId);
908
            _LMAItem item = run.AsLMAItem();
909
            _placement.PIDAutoJoin(item, AutoJoinEndConstants.autoJoin_Both, ref item);
910
        }
911

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

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

    
965
                ReleaseCOMObjects(_LMPipeRun);
966
            }
967

    
968
            return connectorVertices;
969
        }
970

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

    
983
                    double maxLineX = Math.Max(point1[0], point2[0]);
984
                    double minLineX = Math.Min(point1[0], point2[0]);
985
                    double maxLineY = Math.Max(point1[1], point2[1]);
986
                    double minLineY = Math.Min(point1[1], point2[1]);
987

    
988
                    SlopeType slope = SPPIDUtil.CalcSlope(minLineX, minLineY, maxLineX, maxLineY);
989

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

    
1021
            return targetConnector;
1022
        }
1023

    
1024
        private void LineNumberModeling(LineNumber lineNumber)
1025
        {
1026
            Line line = null;
1027
            foreach (var lineRun in lineNumber.RUNS)
1028
            {
1029
                foreach (var item in lineRun.RUNITEMS)
1030
                {
1031
                    line = item as Line;
1032
                    if (line != null && !string.IsNullOrEmpty(line.SPPID.ModelItemId))
1033
                        break;
1034
                    else
1035
                        line = null;
1036
                }
1037
                if (line != null)
1038
                    break;
1039
            }
1040

    
1041
            if (line != null && !string.IsNullOrEmpty(line.SPPID.ModelItemId))
1042
            {
1043
                Array points = new double[] { 0, lineNumber.SPPID.ORIGINAL_X, lineNumber.SPPID.ORIGINAL_Y };
1044
                LMPipeRun _pipeRun = dataSource.GetPipeRun(line.SPPID.ModelItemId);
1045
                if (_pipeRun != null)
1046
                {
1047
                    foreach (LMRepresentation rep in _pipeRun.Representations)
1048
                    {
1049
                        if (rep.get_RepresentationType() == "Connector" && rep.get_ItemStatus() == "Active")
1050
                        {
1051
                            LMConnector _LmConnector = dataSource.GetConnector(rep.Id);
1052
                            LMLabelPersist _LmLabelPresist = _placement.PIDPlaceLabel(lineNumber.SPPID.MAPPINGNAME, ref points, Rotation: lineNumber.ANGLE, LabeledItem: _LmConnector.AsLMRepresentation(), IsLeaderVisible: false);
1053
                            _LmLabelPresist.Commit();
1054

    
1055
                            if (_LmConnector != null)
1056
                                ReleaseCOMObjects(_LmConnector);
1057
                            if (_LmLabelPresist != null)
1058
                            {
1059
                                lineNumber.SPPID.RepresentationId = _LmLabelPresist.AsLMRepresentation().Id;
1060
                                ReleaseCOMObjects(_LmConnector);
1061
                            }
1062

    
1063
                            break;
1064
                        }
1065
                    }
1066

    
1067
                    ReleaseCOMObjects(_pipeRun);
1068
                }
1069
            }
1070
        }
1071

    
1072
        private void TextModeling(Text text)
1073
        {
1074

    
1075
        }
1076

    
1077
        private void NoteModeling(Note note)
1078
        {
1079

    
1080
        }
1081

    
1082
        public void ReleaseCOMObjects(params object[] objVars)
1083
        {
1084
            int intNewRefCount = 0;
1085
            foreach (object obj in objVars)
1086
            {
1087
                if (!Information.IsNothing(obj) && System.Runtime.InteropServices.Marshal.IsComObject(obj))
1088
                    intNewRefCount = intNewRefCount + System.Runtime.InteropServices.Marshal.FinalReleaseComObject(obj);
1089
            }
1090
        }
1091
    }
1092
}
클립보드 이미지 추가 (최대 크기: 500 MB)