프로젝트

일반

사용자정보

통계
| 개정판:

hytos / DTI_PID / SPPIDConverter / AutoModeling.cs @ 6b298450

이력 | 보기 | 이력해설 | 다운로드 (43.9 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
                // Equipment Modeling
53
                foreach (Equipment equipment in document.Equipments)
54
                    SymbolModeling(equipment as Symbol, null, null);
55
                // LineRun Symbol Modeling
56
                foreach (LineNumber lineNumber in document.LINENUMBERS)
57
                    foreach (LineRun run in lineNumber.RUNS)
58
                        SymbolModelingByRun(run);
59
                // TrimLineRun Symbol Modeling
60
                foreach (TrimLine trimLine in document.TRIMLINES)
61
                    foreach (LineRun run in trimLine.RUNS)
62
                        SymbolModelingByRun(run);
63
                // LineRun Line Modeling
64
                foreach (LineNumber lineNumber in document.LINENUMBERS)
65
                    foreach (LineRun run in lineNumber.RUNS)
66
                        LineModelingByRun(run);
67
                // TrimLineRun Line Modeling
68
                foreach (TrimLine trimLine in document.TRIMLINES)
69
                    foreach (LineRun run in trimLine.RUNS)
70
                        LineModelingByRun(run);
71
                // Branch Line Modeling
72
                foreach (var item in BranchLines)
73
                    BranchLineModeling(item);
74
                // EndBreak Modeling
75
                foreach (var item in document.EndBreaks)
76
                    EndBreakModeling(item);
77
                // LineNumber Modeling
78
                foreach (var item in document.LINENUMBERS)
79
                    LineNumberModeling(item);
80
                // Input Symbol Attribute
81
                foreach (var item in document.SYMBOLS)
82
                    InputSymbolAttribute(item);
83
                // Input Line Attribute
84

    
85
                // Input LineNumber Attribute
86

    
87
                // Note Modeling
88
                foreach (var item in document.NOTES)
89
                    NoteModeling(item);
90
                // Text Modeling
91
                foreach (var item in document.TEXTINFOS)
92
                    TextModeling(item);
93
                // LineRun Line Join
94
                foreach (LineNumber lineNumber in document.LINENUMBERS)
95
                {
96

    
97
                    foreach (LineRun run in lineNumber.RUNS)
98
                    {
99
                        JoinRunLine(run);
100
                    }
101
                }
102
                // TrimLineRun Line Join
103
                foreach (TrimLine trimLine in document.TRIMLINES)
104
                {
105
                    foreach (LineRun run in trimLine.RUNS)
106
                    {
107
                        JoinRunLine(run);
108
                    }
109
                }
110
            }
111
            catch (Exception ex)
112
            {
113
                System.Windows.Forms.MessageBox.Show(ex.Message + "\r\n" + ex.StackTrace);
114
            }
115
            finally
116
            {
117
                ReleaseCOMObjects(dataSource);
118
                ReleaseCOMObjects(_placement);
119
            }
120
           
121
            System.Windows.Forms.MessageBox.Show("end");
122
        }
123

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

    
148
                    prevLine = line;
149
                }
150
                // Symbol 일 경우
151
                else if (item.GetType() == typeof(Symbol))
152
                {
153
                    if (lines.Count > 0)
154
                    {
155
                        LineModeling(lines);
156
                        lines.Clear();
157
                    }
158
                }
159
            }
160

    
161
            if (lines.Count > 0)
162
                LineModeling(lines);
163
        }
164

    
165
        private void SymbolModelingByRun(LineRun run)
166
        {
167
            // 양끝 Symbol 검사 후 Line이 나올때까지만 Symbol Modeling
168
            if (run.RUNITEMS.Count > 0)
169
            {
170
                if (run.RUNITEMS[0].GetType() == typeof(Symbol))
171
                    SymbolModelingByRunStart(run.RUNITEMS[0] as Symbol, run);
172

    
173
                if (run.RUNITEMS[run.RUNITEMS.Count - 1].GetType() == typeof(Symbol))
174
                    SymbolModelingByRunEnd(run.RUNITEMS[run.RUNITEMS.Count - 1] as Symbol, run);
175
            }
176

    
177
            Symbol prevSymbol = null;
178
            Symbol targetSymbol = null;
179
            foreach (var item in run.RUNITEMS)
180
            {
181
                if (item.GetType() == typeof(Symbol))
182
                {
183
                    Symbol symbol = item as Symbol;
184
                    SymbolModeling(symbol, targetSymbol, prevSymbol);
185
                    prevSymbol = symbol;
186
                    targetSymbol = symbol;
187
                }
188
                else
189
                {
190
                    targetSymbol = null;
191
                }
192
            }
193
        }
194

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

    
217

    
218
        }
219

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

    
243
        private void SymbolModeling(Symbol symbol, Symbol targetSymbol, Symbol prevSymbol)
244
        {
245
            if (!string.IsNullOrEmpty(symbol.SPPID.RepresentationId))
246
                return;
247

    
248
            LMSymbol _LMSymbol = null;
249

    
250
            string mappingPath = symbol.SPPID.MAPPINGNAME;
251
            double x = symbol.SPPID.ORIGINAL_X;
252
            double y = symbol.SPPID.ORIGINAL_Y;
253
            int mirror = 0;
254
            double angle = symbol.ANGLE;
255

    
256
            if (targetSymbol != null && !string.IsNullOrEmpty(targetSymbol.SPPID.RepresentationId))
257
            {
258
                LMSymbol _TargetItem = dataSource.GetSymbol(targetSymbol.SPPID.RepresentationId);
259
                _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle, TargetItem: _TargetItem);
260
                ReleaseCOMObjects(_TargetItem);
261
            }
262
            else if (prevSymbol != null)
263
            {
264
                LMSymbol _PrevSymbol = dataSource.GetSymbol(prevSymbol.SPPID.RepresentationId);
265
                SlopeType slopeType = SPPIDUtil.CalcSlope(x, y, prevSymbol.SPPID.ORIGINAL_X, prevSymbol.SPPID.ORIGINAL_Y);
266
                double prevX = _PrevSymbol.get_XCoordinate();
267
                double prevY = _PrevSymbol.get_YCoordinate();
268
                if (slopeType == SlopeType.HORIZONTAL)
269
                    y = prevY;
270
                else if (slopeType == SlopeType.VERTICAL)
271
                    x = prevX;
272
                _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle);
273
            }
274
            else
275
            {
276
                _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle);
277
            }
278

    
279

    
280
            if (_LMSymbol != null)
281
            {
282
                _LMSymbol.Commit();
283
                symbol.SPPID.RepresentationId = _LMSymbol.AsLMRepresentation().Id;
284
            }
285

    
286
            ReleaseCOMObjects(_LMSymbol);
287
        }
288

    
289
        private bool IsSameLineNumber(object item, object targetItem)
290
        {
291
            foreach (var lineNumber in document.LINENUMBERS)
292
            {
293
                foreach (var run in lineNumber.RUNS)
294
                {
295
                    foreach (var runItem in run.RUNITEMS)
296
                    {
297
                        if (runItem == item)
298
                        {
299
                            foreach (var findItem in run.RUNITEMS)
300
                            {
301
                                if (findItem == targetItem)
302
                                {
303
                                    return true;
304
                                }
305
                            }
306

    
307
                            return false;
308

    
309
                        }
310
                    }
311
                }
312
            }
313

    
314
            return false;
315
        }
316

    
317
        private void LineModeling(List<Line> lines)
318
        {
319
            _LMAItem _LMAItem = _placement.PIDCreateItem(lines[0].SPPID.MAPPINGNAME);
320
            PlaceRunInputs placeRunInputs = new PlaceRunInputs();
321
            LMSymbol _LMSymbol1 = null;
322
            LMSymbol _LMSymbol2 = null;
323
            Dictionary<LMConnector, List<double[]>> connectorVertices1 = new Dictionary<LMConnector, List<double[]>>();
324
            LMConnector targetConnector1 = null;
325
            Dictionary<LMConnector, List<double[]>> connectorVertices2 = new Dictionary<LMConnector, List<double[]>>();
326
            LMConnector targetConnector2 = null;
327

    
328
            Line startBranchLine = null;
329
            Line endBranchLine = null;
330

    
331
            for (int i = 0; i < lines.Count; i++)
332
            {
333
                Line line = lines[i];
334
                if (i == 0 || i + 1 != lines.Count)
335
                {
336
                    // 시작점에 연결된 Symbol 찾기
337
                    object connItem = SPPIDUtil.FindObjectByUID(document, line.CONNECTORS[0].CONNECTEDITEM);
338
                    if (connItem != null && connItem.GetType() == typeof(Symbol))
339
                    {
340
                        _LMSymbol1 = dataSource.GetSymbol(((Symbol)connItem).SPPID.RepresentationId);
341
                        if (_LMSymbol1 != null)
342
                            placeRunInputs.AddSymbolTarget(_LMSymbol1, line.SPPID.START_X, line.SPPID.START_Y);
343
                        else
344
                            placeRunInputs.AddPoint(line.SPPID.START_X, line.SPPID.START_Y);
345
                    }
346
                    else if (connItem != null && connItem.GetType() == typeof(Line) && !lines.Contains(connItem))
347
                    {
348
                        connectorVertices1 = GetPipeRunVertices(((Line)connItem).SPPID.ModelItemId);
349
                        targetConnector1 = FindTargetLMConnector(connectorVertices1, line.SPPID.START_X, line.SPPID.START_Y, line.SPPID.END_X, line.SPPID.END_Y);
350

    
351
                        if (targetConnector1 != null)
352
                            placeRunInputs.AddConnectorTarget(targetConnector1, line.SPPID.START_X, line.SPPID.START_Y);
353
                        else
354
                        {
355
                            startBranchLine = connItem as Line;
356
                            placeRunInputs.AddPoint(line.SPPID.START_X, line.SPPID.START_Y);
357
                        }
358
                    }
359
                    else
360
                        placeRunInputs.AddPoint(line.SPPID.START_X, line.SPPID.START_Y);
361
                }
362

    
363
                if (i + 1 == lines.Count)
364
                {
365
                    // 끝점에 연결된 Symbol 찾기
366
                    object connItem = SPPIDUtil.FindObjectByUID(document, line.CONNECTORS[1].CONNECTEDITEM);
367

    
368
                    if (i != 0)
369
                        placeRunInputs.AddPoint(line.SPPID.START_X, line.SPPID.START_Y);
370

    
371
                    if (connItem != null && connItem.GetType() == typeof(Symbol))
372
                    {
373
                        _LMSymbol2 = dataSource.GetSymbol(((Symbol)connItem).SPPID.RepresentationId);
374
                        if (_LMSymbol2 != null)
375
                            placeRunInputs.AddSymbolTarget(_LMSymbol2, line.SPPID.END_X, line.SPPID.END_Y);
376
                        else
377
                            placeRunInputs.AddPoint(line.SPPID.END_X, line.SPPID.END_Y);
378
                            
379
                    }
380
                    else if (connItem != null && connItem.GetType() == typeof(Line) && !lines.Contains(connItem))
381
                    {
382
                        connectorVertices2 = GetPipeRunVertices(((Line)connItem).SPPID.ModelItemId);
383
                        targetConnector2 = FindTargetLMConnector(connectorVertices2, line.SPPID.END_X, line.SPPID.END_Y, line.SPPID.START_X, line.SPPID.START_Y);
384

    
385
                        if (targetConnector2 != null)
386
                            placeRunInputs.AddConnectorTarget(targetConnector2, line.SPPID.END_X, line.SPPID.END_Y);
387
                        else
388
                        {
389
                            endBranchLine = connItem as Line;
390
                            placeRunInputs.AddPoint(line.SPPID.END_X, line.SPPID.END_Y);
391
                        }
392
                    }
393
                    else
394
                    {
395
                        placeRunInputs.AddPoint(line.SPPID.END_X, line.SPPID.END_Y);
396
                    }
397
                }
398
            }
399

    
400
            LMConnector _lMConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
401

    
402
            if (_lMConnector != null)
403
            {
404
                foreach (var line in lines)
405
                    line.SPPID.ModelItemId = _lMConnector.ModelItemID;
406
                _lMConnector.Commit();
407
                if (startBranchLine != null || endBranchLine != null)
408
                {
409
                    BranchLines.Add(new Tuple<string, Line, Line>(_lMConnector.ModelItemID, startBranchLine, endBranchLine));
410
                }
411
            }
412

    
413

    
414
            if (_LMSymbol1 != null)
415
                ReleaseCOMObjects(_LMSymbol1);
416
            if (_LMSymbol2 != null)
417
                ReleaseCOMObjects(_LMSymbol2);
418
            if (targetConnector1 != null)
419
                ReleaseCOMObjects(targetConnector1);
420
            if (targetConnector2 != null)
421
                ReleaseCOMObjects(targetConnector2);
422
            foreach (var item in connectorVertices1)
423
                ReleaseCOMObjects(item.Key);
424
            foreach (var item in connectorVertices2)
425
                ReleaseCOMObjects(item.Key);
426

    
427
            ReleaseCOMObjects(_lMConnector);
428
            ReleaseCOMObjects(placeRunInputs);
429
            ReleaseCOMObjects(_LMAItem);
430
        }
431

    
432
        private void BranchLineModeling(Tuple<string, Line, Line> branch)
433
        {
434
            List<Line> lines = SPPIDUtil.FindLinesByModelId(document, branch.Item1);
435
            Dictionary<LMConnector, List<double[]>> connectorVertices = GetPipeRunVertices(branch.Item1);
436

    
437
            LMConnector _StartConnector = null;
438
            LMConnector _EndConnector = null;
439
            double lengthStart = double.MaxValue;
440
            double lengthEnd = double.MaxValue;
441
            List<double[]> startPoints = new List<double[]>();
442
            List<double[]> endPoints = new List<double[]>();
443

    
444
            foreach (var item in connectorVertices)
445
            {
446
                foreach (var point in item.Value)
447
                {
448
                    // Start Point가 Branch
449
                    if (branch.Item2 != null)
450
                    {
451
                        Line targetLine = branch.Item2;
452
                        double distance = SPPIDUtil.CalcLineToPointDistance(targetLine.SPPID.START_X, targetLine.SPPID.START_Y, targetLine.SPPID.END_X, targetLine.SPPID.END_Y, point[0], point[1]);
453
                        if (lengthStart > distance)
454
                        {
455
                            _StartConnector = item.Key;
456
                            lengthStart = distance;
457
                            startPoints = item.Value;
458
                        }
459
                    }
460
                    // End Point가 Branch
461
                    if (branch.Item3 != null)
462
                    {
463
                        Line targetLine = branch.Item3;
464
                        double distance = SPPIDUtil.CalcLineToPointDistance(targetLine.SPPID.START_X, targetLine.SPPID.START_Y, targetLine.SPPID.END_X, targetLine.SPPID.END_Y, point[0], point[1]);
465
                        if (lengthEnd > distance)
466
                        {
467
                            _EndConnector = item.Key;
468
                            lengthEnd = distance;
469
                            endPoints = item.Value;
470
                        }
471
                    }
472
                }
473
            }
474
            #region Branch가 양쪽 전부일 때
475
            if (_StartConnector != null && _StartConnector == _EndConnector)
476
            {
477
                _placement.PIDRemovePlacement(_StartConnector.AsLMRepresentation());
478

    
479
                _LMAItem _LMAItem = _placement.PIDCreateItem(lines[0].SPPID.MAPPINGNAME);
480
                PlaceRunInputs placeRunInputs = new PlaceRunInputs();
481

    
482
                Dictionary<LMConnector, List<double[]>> startConnectorVertices = GetPipeRunVertices(branch.Item2.SPPID.ModelItemId);
483
                LMConnector _StartTargetConnector = FindTargetLMConnector(startConnectorVertices, startPoints[0][0], startPoints[0][1], startPoints[1][0], startPoints[1][1]);
484
                Dictionary<LMConnector, List<double[]>> endConnectorVertices = GetPipeRunVertices(branch.Item3.SPPID.ModelItemId);
485
                LMConnector _EndTargetConnector = FindTargetLMConnector(endConnectorVertices,
486
                   startPoints[startPoints.Count - 1][0],
487
                   startPoints[startPoints.Count - 1][1],
488
                   startPoints[startPoints.Count - 2][0],
489
                   startPoints[startPoints.Count - 2][1]);
490

    
491
                for (int i = 0; i < startPoints.Count; i++)
492
                {
493
                    double[] point = startPoints[i];
494
                    if (i == 0)
495
                        placeRunInputs.AddConnectorTarget(_StartTargetConnector, point[0], point[1]);
496
                    else if (i == startPoints.Count - 1)
497
                        placeRunInputs.AddConnectorTarget(_EndTargetConnector, point[0], point[1]);
498
                    else
499
                        placeRunInputs.AddPoint(point[0], point[1]);
500
                }
501

    
502
                LMConnector _LMConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
503
                if (_LMConnector != null)
504
                {
505
                    _LMConnector.Commit();
506
                    foreach (var item in lines)
507
                        item.SPPID.ModelItemId = _LMConnector.ModelItemID;
508
                }
509

    
510
                foreach (var item in startConnectorVertices)
511
                    ReleaseCOMObjects(item.Key);
512
                foreach (var item in endConnectorVertices)
513
                    ReleaseCOMObjects(item.Key);
514
                ReleaseCOMObjects(placeRunInputs);
515
                ReleaseCOMObjects(_LMAItem);
516
                ReleaseCOMObjects(_LMConnector);
517
            }
518
            #endregion
519
            #region 양쪽이 다른 Branch 
520
            else
521
            {
522
                // Branch 시작 Connector
523
                if (_StartConnector != null)
524
                    BranchLineModelingByConnector(branch, _StartConnector, startPoints, true);
525

    
526
                // Branch 끝 Connector
527
                if (_EndConnector != null)
528
                    BranchLineModelingByConnector(branch, _EndConnector, endPoints, false);
529
            }
530
            #endregion
531

    
532
            if (_StartConnector != null)
533
                ReleaseCOMObjects(_StartConnector);
534
            if (_EndConnector != null)
535
                ReleaseCOMObjects(_EndConnector);
536
            foreach (var item in connectorVertices)
537
                ReleaseCOMObjects(item.Key);
538
        }
539

    
540
        private void BranchLineModelingByConnector(Tuple<string, Line, Line> branch, LMConnector _Connector, List<double[]> points, bool IsStart)
541
        {
542
            List<Line> lines = SPPIDUtil.FindLinesByModelId(document, branch.Item1);
543
            Dictionary<LMConnector, List<double[]>> connectorVertices = GetPipeRunVertices(branch.Item1);
544
            LMConnector _SameRunTargetConnector = null;
545
            LMSymbol _SameRunTargetSymbol = null;
546
            Dictionary<LMConnector, List<double[]>> branchConnectorVertices = null;
547
            LMConnector _BranchTargetConnector = null;
548
            PlaceRunInputs placeRunInputs = new PlaceRunInputs();
549

    
550
            // 같은 Line Run의 Connector 찾기
551
            foreach (var item in connectorVertices)
552
            {
553
                if (item.Key == _Connector)
554
                    continue;
555

    
556
                if (IsStart &&
557
                    !DBNull.Value.Equals(item.Key.ConnectItem1SymbolID) &&
558
                    !DBNull.Value.Equals(_Connector.ConnectItem2SymbolID)&& 
559
                    item.Key.ConnectItem1SymbolID == _Connector.ConnectItem2SymbolID)
560
                {
561
                    _SameRunTargetConnector = item.Key;
562
                    break;
563
                }
564
                else if (!IsStart &&
565
                    !DBNull.Value.Equals(item.Key.ConnectItem2SymbolID) &&
566
                    !DBNull.Value.Equals(_Connector.ConnectItem1SymbolID) && 
567
                    item.Key.ConnectItem2SymbolID == _Connector.ConnectItem1SymbolID)
568
                {
569
                    _SameRunTargetConnector = item.Key;
570
                    break;
571
                }
572
            }
573

    
574
            // Branch 반대편이 Symbol
575
            if (_SameRunTargetConnector == null)
576
            {
577
                foreach (var line in lines)
578
                {
579
                    foreach (var connector in line.CONNECTORS)
580
                    {
581
                        Symbol symbol = SPPIDUtil.FindObjectByUID(document, connector.CONNECTEDITEM) as Symbol;
582
                        if (symbol != null)
583
                        {
584
                            _SameRunTargetSymbol = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
585
                            break;
586
                        }
587
                    }
588
                }
589
            }
590

    
591
            // 기존 Connector 제거
592
            _placement.PIDRemovePlacement(_Connector.AsLMRepresentation());
593
            
594
            // 시작 Connector일 경우 첫 Point가 TargetConnector를 찾아야함
595
            if (IsStart)
596
            {
597
                branchConnectorVertices = GetPipeRunVertices(branch.Item2.SPPID.ModelItemId);
598
                _BranchTargetConnector = FindTargetLMConnector(branchConnectorVertices, points[0][0], points[0][1], points[1][0], points[1][1]);
599
            }
600
            // 끝 Conenctor일 경우 마지막 Point가 TargetConnector를 찾아야함
601
            else
602
            {
603
                branchConnectorVertices = GetPipeRunVertices(branch.Item3.SPPID.ModelItemId);
604
                _BranchTargetConnector = FindTargetLMConnector(branchConnectorVertices,
605
                    points[points.Count - 1][0],
606
                    points[points.Count - 1][1],
607
                    points[points.Count - 2][0],
608
                    points[points.Count - 2][1]);
609
            }
610

    
611
            for (int i = 0; i < points.Count; i++)
612
            {
613
                double[] point = points[i];
614
                if (i == 0)
615
                {
616
                    if (IsStart)
617
                    {
618
                        placeRunInputs.AddConnectorTarget(_BranchTargetConnector, point[0], point[1]);
619
                    }
620
                    else
621
                    {
622
                        if (_SameRunTargetConnector != null)
623
                            placeRunInputs.AddConnectorTarget(_SameRunTargetConnector, point[0], point[1]);
624
                        else if (_SameRunTargetConnector != null)
625
                            placeRunInputs.AddSymbolTarget(_SameRunTargetSymbol, point[0], point[1]);
626
                        else
627
                            placeRunInputs.AddPoint(point[0], point[1]);
628
                    }
629
                }
630
                else if (i == points.Count - 1)
631
                {
632
                    if (IsStart)
633
                    {
634
                        if (_SameRunTargetConnector != null)
635
                            placeRunInputs.AddConnectorTarget(_SameRunTargetConnector, point[0], point[1]);
636
                        else if (_SameRunTargetSymbol != null)
637
                            placeRunInputs.AddSymbolTarget(_SameRunTargetSymbol, point[0], point[1]);
638
                        else
639
                            placeRunInputs.AddPoint(point[0], point[1]);
640
                    }
641
                    else
642
                    {
643
                        placeRunInputs.AddConnectorTarget(_BranchTargetConnector, point[0], point[1]);
644
                    }
645
                }
646
                else
647
                    placeRunInputs.AddPoint(point[0], point[1]);
648
            }
649
            _LMAItem _LMAItem = _placement.PIDCreateItem(lines[0].SPPID.MAPPINGNAME);
650
            LMConnector _LMConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
651

    
652
            if (_LMConnector != null)
653
            {
654
                if (_SameRunTargetConnector != null)
655
                {
656
                    JoinPipeRun(_LMConnector.ModelItemID, _SameRunTargetConnector.ModelItemID);
657
                }
658
                else
659
                {
660
                    foreach (var item in lines)
661
                        item.SPPID.ModelItemId = _LMConnector.ModelItemID;
662
                }
663

    
664
                _LMConnector.Commit();
665
                ReleaseCOMObjects(_LMConnector);
666
            }
667

    
668
            ReleaseCOMObjects(placeRunInputs);
669
            ReleaseCOMObjects(_LMAItem);
670
            if (_BranchTargetConnector != null)
671
                ReleaseCOMObjects(_BranchTargetConnector);
672
            if (_SameRunTargetConnector != null)
673
                ReleaseCOMObjects(_SameRunTargetConnector);
674
            if (_SameRunTargetSymbol != null)
675
                ReleaseCOMObjects(_SameRunTargetSymbol);
676
            foreach (var item in connectorVertices)
677
                ReleaseCOMObjects(item.Key);
678
            foreach (var item in branchConnectorVertices)
679
                ReleaseCOMObjects(item.Key);
680
        }
681

    
682
        private void EndBreakModeling(EndBreak endBreak)
683
        {
684
            Line ownerLine = SPPIDUtil.FindObjectByUID(document, endBreak.OWNER) as Line;
685
            Line connLine = null;   
686
            Property property = endBreak.PROPERTIES.Find(x => x.ATTRIBUTE == "Connected Item");
687
            if (property != null)
688
                connLine = SPPIDUtil.FindObjectByUID(document, property.VALUE) as Line;
689

    
690
            if (ownerLine != null && connLine != null)
691
            {
692
                LMLabelPersist _LmLabelPersist = null;
693

    
694
                string lineModelId = ownerLine.SPPID.ModelItemId;
695
                LMPipeRun _LMPipeRun = _placement.PIDDataSource.GetPipeRun(lineModelId);
696
                LMPipeRun _ConnLMPipeRun = _placement.PIDDataSource.GetPipeRun(connLine.SPPID.ModelItemId);
697
                LMConnector connectedLMConnector = null;
698

    
699
                if (_LMPipeRun != null && _ConnLMPipeRun != null)
700
                {
701
                    foreach (LMRepresentation rep in _LMPipeRun.Representations)
702
                    {
703
                        if (rep.Attributes["RepresentationType"].get_Value() == "Connector" && rep.Attributes["ItemStatus"].get_Value() == "Active")
704
                        {
705
                            LMConnector _LMConnector = _placement.PIDDataSource.GetConnector(rep.Id);
706
                            if (_LMConnector.ConnectItem1SymbolID != null && !DBNull.Value.Equals(_LMConnector.ConnectItem1SymbolID) && ExistConnItem(_ConnLMPipeRun, _LMConnector.ConnectItem1SymbolID))
707
                            {
708
                                connectedLMConnector = _LMConnector;
709
                                break;
710
                            }
711
                            else if (_LMConnector.ConnectItem2SymbolID != null && !DBNull.Value.Equals(_LMConnector.ConnectItem2SymbolID) && ExistConnItem(_ConnLMPipeRun, _LMConnector.ConnectItem2SymbolID))
712
                            {
713
                                connectedLMConnector = _LMConnector;
714
                                break;
715
                            }
716
                            else
717
                            {
718
                                ReleaseCOMObjects(_LMConnector);
719
                            }
720
                        }
721
                    }
722
                    if (connectedLMConnector != null)
723
                    {
724
                        Array array = new double[] { 0, endBreak.SPPID.ORIGINAL_X, endBreak.SPPID.ORIGINAL_Y };
725
                        _LmLabelPersist = _placement.PIDPlaceLabel(endBreak.SPPID.MAPPINGNAME, ref array, Rotation: 0, LabeledItem: connectedLMConnector.AsLMRepresentation(), IsLeaderVisible: true);
726
                        ReleaseCOMObjects(connectedLMConnector);
727
                    }
728

    
729
                    ReleaseCOMObjects(_LMPipeRun);
730
                    ReleaseCOMObjects(_ConnLMPipeRun);
731
                    if (_LmLabelPersist != null)
732
                        ReleaseCOMObjects(_LmLabelPersist);
733
                }
734
            }
735
        }
736

    
737
        private bool ExistConnItem(LMPipeRun _LMPipeRun, string ConnItem)
738
        {
739
            foreach (LMRepresentation rep in _LMPipeRun.Representations)
740
            {
741
                if (rep.Attributes["RepresentationType"].get_Value() == "Connector" && rep.Attributes["ItemStatus"].get_Value() == "Active")
742
                {
743
                    LMConnector _LMConnector = dataSource.GetConnector(rep.Id);
744
                    if (_LMConnector.ConnectItem1SymbolID != null && !DBNull.Value.Equals(_LMConnector.ConnectItem1SymbolID) && _LMConnector.ConnectItem1SymbolID == ConnItem)
745
                    {
746
                        ReleaseCOMObjects(_LMConnector);
747
                        return true;
748
                    }
749
                    else if (_LMConnector.ConnectItem2SymbolID != null && !DBNull.Value.Equals(_LMConnector.ConnectItem2SymbolID) && _LMConnector.ConnectItem2SymbolID == ConnItem)
750
                    {
751
                        ReleaseCOMObjects(_LMConnector);
752
                        return true;
753
                    }
754
                    else
755
                    {
756
                        ReleaseCOMObjects(_LMConnector);
757
                    }
758
                }
759
            }
760

    
761
            return false;
762
        }
763

    
764
        private void JoinPipeRun(string fromModelItemId, string toModelItemId)
765
        {
766
            LMPipeRun run1 = dataSource.GetPipeRun(toModelItemId);
767
            LMPipeRun run2 = dataSource.GetPipeRun(fromModelItemId);
768
            // item2가 item1으로 조인
769
            try
770
            {
771
                _LMAItem item1 = run1.AsLMAItem();
772
                _LMAItem item2 = run2.AsLMAItem();
773

    
774
                _placement.PIDJoinRuns(ref item1, ref item2);
775
                List<Line> lines = SPPIDUtil.FindLinesByModelId(document, fromModelItemId);
776
                foreach (var line in lines)
777
                    line.SPPID.ModelItemId = toModelItemId;
778

    
779
            }
780
            catch (Exception ex)
781
            {
782
                System.Windows.Forms.MessageBox.Show(ex.Message + "\r\n" + ex.StackTrace);
783
            }
784
            finally
785
            {
786
                ReleaseCOMObjects(run1);
787
                ReleaseCOMObjects(run2);
788
            }
789
        }
790

    
791
        private void AutoJoinPipeRun(string modelItemId)
792
        {
793
            LMPipeRun run = dataSource.GetPipeRun(modelItemId);
794
            _LMAItem item = run.AsLMAItem();
795
            _placement.PIDAutoJoin(item, AutoJoinEndConstants.autoJoin_Both, ref item);
796
        }
797

    
798
        private void JoinRunLine(LineRun run)
799
        {
800
            string modelItemId = string.Empty;
801
            foreach (var item in run.RUNITEMS)
802
            {
803
                if (item.GetType() == typeof(Line))
804
                {
805
                    Line line = item as Line;
806
                    if (modelItemId != line.SPPID.ModelItemId)
807
                    {
808
                        AutoJoinPipeRun(line.SPPID.ModelItemId);
809
                        modelItemId = line.SPPID.ModelItemId;
810
                    }
811
                    //if (string.IsNullOrEmpty(modelItemId))
812
                    //{
813
                    //    modelItemId = line.SPPID.ModelItemId;
814
                    //}
815
                    //else if(modelItemId != line.SPPID.ModelItemId)
816
                    //{
817
                    //    JoinPipeRun(line.SPPID.ModelItemId, modelItemId);
818
                    //}
819
                }
820
            }
821
        }
822

    
823
        private Dictionary<LMConnector, List<double[]>> GetPipeRunVertices(string modelId)
824
        {
825
            Dictionary<LMConnector, List<double[]>> connectorVertices = new Dictionary<LMConnector, List<double[]>>();
826
            LMPipeRun _LMPipeRun = dataSource.GetPipeRun(modelId);
827
            if (_LMPipeRun != null)
828
            {
829
                foreach (LMRepresentation rep in _LMPipeRun.Representations)
830
                {
831
                    if (rep.Attributes["RepresentationType"].get_Value() == "Connector" && rep.Attributes["ItemStatus"].get_Value() == "Active")
832
                    {
833
                        LMConnector _LMConnector = dataSource.GetConnector(rep.Id);
834
                        connectorVertices.Add(_LMConnector, new List<double[]>());
835
                        dynamic OID = rep.get_GraphicOID();
836
                        DependencyObject drawingObject = radApp.ActiveDocument.ActiveSheet.DrawingObjects[OID];
837
                        Ingr.RAD2D.LineStringGeometry2d lineStringGeometry = drawingObject.GetGeometry() as Ingr.RAD2D.LineStringGeometry2d;
838
                        int verticesCount = lineStringGeometry.VertexCount;
839
                        double[] vertices = null;
840
                        lineStringGeometry.GetVertices(ref verticesCount, ref vertices);
841
                        for (int i = 0; i < verticesCount; i++)
842
                        {
843
                            double x = 0;
844
                            double y = 0;
845
                            lineStringGeometry.GetVertex(i + 1, ref x, ref y);
846
                            connectorVertices[_LMConnector].Add(new double[] { Math.Round(x, 10), Math.Round(y, 10) });
847
                        }
848
                    }
849
                }
850

    
851
                ReleaseCOMObjects(_LMPipeRun);
852
            }
853

    
854
            return connectorVertices;
855
        }
856

    
857
        private LMConnector FindTargetLMConnector(Dictionary<LMConnector, List<double[]>> connectorVertices, double connX, double connY, double x2, double y2)
858
        {
859
            double length = double.MaxValue;
860
            LMConnector targetConnector = null;
861
            foreach (var item in connectorVertices)
862
            {
863
                List<double[]> points = item.Value;
864
                for (int i = 0; i < points.Count - 1; i++)
865
                {
866
                    double[] point1 = points[i];
867
                    double[] point2 = points[i + 1];
868

    
869
                    double maxLineX = Math.Max(point1[0], point2[0]);
870
                    double minLineX = Math.Min(point1[0], point2[0]);
871
                    double maxLineY = Math.Max(point1[1], point2[1]);
872
                    double minLineY = Math.Min(point1[1], point2[1]);
873

    
874
                    SlopeType slope = SPPIDUtil.CalcSlope(minLineX, minLineY, maxLineX, maxLineY);
875

    
876
                    // 두직선의 교차점
877
                    double[] crossingPoint = SPPIDUtil.CalcLineCrossingPoint(connX, connY, x2, y2, point1[0], point1[1], point2[0], point2[1]);
878
                    if (crossingPoint != null)
879
                    {
880
                        double distance = SPPIDUtil.CalcPointToPointdDistance(connX, connY, crossingPoint[0], crossingPoint[1]);
881
                        if (length >= distance)
882
                        {
883
                            if (slope == SlopeType.Slope &&
884
                                minLineX <= crossingPoint[0] && maxLineX >= crossingPoint[0] &&
885
                                minLineY <= crossingPoint[1] && maxLineY >= crossingPoint[1])
886
                            {
887
                                targetConnector = item.Key;
888
                                length = distance;
889
                            }
890
                            else if (slope == SlopeType.HORIZONTAL &&
891
                                minLineX <= crossingPoint[0] && maxLineX >= crossingPoint[0])
892
                            {
893
                                targetConnector = item.Key;
894
                                length = distance;
895
                            }
896
                            else if (slope == SlopeType.VERTICAL &&
897
                               minLineY <= crossingPoint[1] && maxLineY >= crossingPoint[1])
898
                            {
899
                                targetConnector = item.Key;
900
                                length = distance;
901
                            }
902
                        }
903
                    }
904
                }
905
            }
906

    
907
            return targetConnector;
908
        }
909

    
910
        private void LineNumberModeling(LineNumber lineNumber)
911
        {
912
            Line line = null;
913
            foreach (var lineRun in lineNumber.RUNS)
914
            {
915
                foreach (var item in lineRun.RUNITEMS)
916
                {
917
                    line = item as Line;
918
                    if (line != null && !string.IsNullOrEmpty(line.SPPID.ModelItemId))
919
                        break;
920
                    else
921
                        line = null;
922
                }
923
                if (line != null)
924
                    break;
925
            }
926

    
927
            if (line != null && !string.IsNullOrEmpty(line.SPPID.ModelItemId))
928
            {
929
                Array points = new double[] { 0, lineNumber.SPPID.ORIGINAL_X, lineNumber.SPPID.ORIGINAL_Y };
930
                LMPipeRun _pipeRun = dataSource.GetPipeRun(line.SPPID.ModelItemId);
931
                if (_pipeRun != null)
932
                {
933
                    foreach (LMRepresentation rep in _pipeRun.Representations)
934
                    {
935
                        if (rep.get_RepresentationType() == "Connector" && rep.get_ItemStatus() == "Active")
936
                        {
937
                            LMConnector _LmConnector = dataSource.GetConnector(rep.Id);
938
                            LMLabelPersist _LmLabelPresist = _placement.PIDPlaceLabel(lineNumber.SPPID.MAPPINGNAME, ref points, Rotation: lineNumber.ANGLE, LabeledItem: _LmConnector.AsLMRepresentation(), IsLeaderVisible: false);
939
                            _LmLabelPresist.Commit();
940

    
941
                            if (_LmConnector != null)
942
                                ReleaseCOMObjects(_LmConnector);
943
                            if (_LmLabelPresist != null)
944
                            {
945
                                lineNumber.SPPID.RepresentationId = _LmLabelPresist.AsLMRepresentation().Id;
946
                                ReleaseCOMObjects(_LmConnector);
947
                            }
948

    
949
                            break;
950
                        }
951
                    }
952

    
953
                    ReleaseCOMObjects(_pipeRun);
954
                }
955
            }
956
        }
957

    
958
        private void InputSymbolAttribute(Symbol symbol)
959
        {
960
            if (!string.IsNullOrEmpty(symbol.SPPID.RepresentationId))
961
            {
962
                foreach (var item in symbol.PROPERTIES)
963
                {
964
                    
965

    
966
                }
967

    
968
                foreach (var item in symbol.ATTRIBUTES)
969
                {
970

    
971

    
972
                }
973
            }
974
        }
975

    
976
        private void TextModeling(Text text)
977
        {
978
            LMSymbol _LMSymbol = null;
979
            LMItemNote _LMItemNote = null;
980
            LMAAttribute _LMAAttribute = null;
981

    
982
            try
983
            {
984
                _LMSymbol = _placement.PIDPlaceSymbol(text.SPPID.MAPPINGNAME, text.SPPID.ORIGINAL_X, text.SPPID.ORIGINAL_Y);
985
                _LMSymbol.Commit();
986
                _LMItemNote = _placement.PIDDataSource.GetItemNote(_LMSymbol.ModelItemID);
987
                _LMItemNote.Commit();
988
                _LMAAttribute = _LMItemNote.Attributes["Note.Body"];
989
                _LMAAttribute.set_Value(text.VALUE);
990
                _LMItemNote.Commit();
991
            }
992
            catch (Exception ex)
993
            {
994

    
995
            }
996
            finally
997
            {
998
                if (_LMAAttribute != null)
999
                    ReleaseCOMObjects(_LMAAttribute);
1000
                if (_LMItemNote != null)
1001
                    ReleaseCOMObjects(_LMItemNote);
1002
                if (_LMSymbol != null)
1003
                    ReleaseCOMObjects(_LMSymbol);
1004
            }
1005
        }
1006

    
1007
        private void NoteModeling(Note note)
1008
        {
1009
            LMSymbol _LMSymbol = null;
1010
            LMItemNote _LMItemNote = null;
1011
            LMAAttribute _LMAAttribute = null;
1012

    
1013
            try
1014
            {
1015
                _LMSymbol = _placement.PIDPlaceSymbol(note.SPPID.MAPPINGNAME, note.SPPID.ORIGINAL_X, note.SPPID.ORIGINAL_Y);
1016
                _LMSymbol.Commit();
1017
                _LMItemNote = _placement.PIDDataSource.GetItemNote(_LMSymbol.ModelItemID);
1018
                _LMItemNote.Commit();
1019
                _LMAAttribute = _LMItemNote.Attributes["Note.Body"];
1020
                _LMAAttribute.set_Value(note.VALUE);
1021
                _LMItemNote.Commit();
1022
            }
1023
            catch (Exception ex)
1024
            {
1025

    
1026
            }
1027
            finally
1028
            {
1029
                if (_LMAAttribute != null)
1030
                    ReleaseCOMObjects(_LMAAttribute);
1031
                if (_LMItemNote != null)
1032
                    ReleaseCOMObjects(_LMItemNote);
1033
                if (_LMSymbol != null)
1034
                    ReleaseCOMObjects(_LMSymbol);
1035
            }
1036
            
1037
        }
1038

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