프로젝트

일반

사용자정보

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

hytos / DTI_PID / SPPIDConverter / AutoModeling.cs @ 905f8163

이력 | 보기 | 이력해설 | 다운로드 (85.6 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
using DevExpress.XtraSplashScreen;
25
namespace Converter.SPPID
26
{
27
    public class AutoModeling
28
    {
29
        Placement _placement;
30
        LMADataSource dataSource;
31
        dynamic newDrawing;
32
        dynamic application;
33
        Ingr.RAD2D.Application radApp;
34
        SPPID_Document document;
35
        ETCSetting _ETCSetting;
36

    
37
        public string DocumentLabelText { get; set; }
38

    
39
        int CurrentCount;
40

    
41
        List <Tuple<string, Line, Line>> BranchLines = new List<Tuple<string, Line, Line>>();
42
        public AutoModeling(SPPID_Document document, dynamic application, Ingr.RAD2D.Application radApp)
43
        {
44
            this.document = document;
45
            this.application = application;
46
            this.radApp = radApp;
47
            this._ETCSetting = ETCSetting.GetInstance();
48
        }
49

    
50
        private int ClacProgressCount()
51
        {
52
            int EquipCount = 0;
53
            int SymbolCount = 0;
54
            int LineCount = 0;
55
            int NoteCount = 0;
56
            int TextCount = 0;
57
            int EndBreakCount = 0;
58
            int LineNumberCount = 0;
59

    
60
            EquipCount = document.Equipments.Count;
61
            SymbolCount = document.SYMBOLS.Count;
62
            SymbolCount = SymbolCount * 3;
63
            
64
            foreach (LineNumber lineNumber in document.LINENUMBERS)
65
                foreach (LineRun run in lineNumber.RUNS)
66
                    foreach (var item in run.RUNITEMS)
67
                        if (item.GetType() == typeof(Line))
68
                            LineCount++;
69
            foreach (TrimLine trimLine in document.TRIMLINES)
70
                foreach (LineRun run in trimLine.RUNS)
71
                    foreach (var item in run.RUNITEMS)
72
                        if (item.GetType() == typeof(Line))
73
                            LineCount++;
74

    
75
            LineCount = LineCount * 2;
76
            NoteCount = document.NOTES.Count;
77
            TextCount = document.TEXTINFOS.Count;
78
            EndBreakCount = document.EndBreaks.Count;
79
            LineNumberCount = document.LINENUMBERS.Count;
80
            LineNumberCount = LineNumberCount * 2;
81

    
82
            return EquipCount + SymbolCount + LineCount + NoteCount + TextCount + EndBreakCount;
83
        }
84

    
85
        /// <summary>
86
        /// 도면 단위당 실행되는 메서드
87
        /// </summary>
88
        public void Run()
89
        {
90
            try
91
            {
92
                _placement = new Placement();
93
                dataSource = _placement.PIDDataSource;
94

    
95
                CreateDocument();
96

    
97
                if (DocumentCoordinateCorrection())
98
                {
99
                    int AllCount = ClacProgressCount();
100

    
101
                    SplashScreenManager.ShowForm(typeof(SPPIDSplashScreen), true, true);
102
                    Thread.Sleep(1000);
103
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetParent, (IntPtr)radApp.HWnd);
104
                    Thread.Sleep(1000);
105
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetAllStep, AllCount);
106
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetDocumentName, DocumentLabelText);
107

    
108
                    // Equipment Modeling
109
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Equipments Modeling");
110
                    foreach (Equipment equipment in document.Equipments)
111
                        EquipmentModeling(equipment);
112

    
113
                    // LineRun Symbol Modeling
114
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Symbols Modeling");
115
                    foreach (LineNumber lineNumber in document.LINENUMBERS)
116
                        foreach (LineRun run in lineNumber.RUNS)
117
                            SymbolModelingByRun(run);
118
                    // TrimLineRun Symbol Modeling
119
                    foreach (TrimLine trimLine in document.TRIMLINES)
120
                        foreach (LineRun run in trimLine.RUNS)
121
                            SymbolModelingByRun(run);
122

    
123
                    // LineRun Line Modeling
124
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Lines Modeling");
125
                    foreach (LineNumber lineNumber in document.LINENUMBERS)
126
                        foreach (LineRun run in lineNumber.RUNS)
127
                            LineModelingByRun(run);
128
                    // TrimLineRun Line Modeling
129
                    foreach (TrimLine trimLine in document.TRIMLINES)
130
                        foreach (LineRun run in trimLine.RUNS)
131
                            LineModelingByRun(run);
132

    
133
                    // Branch Line Modeling
134
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Branch Lines Modeling");
135
                    foreach (var item in BranchLines)
136
                        BranchLineModeling(item);
137

    
138
                    // EndBreak Modeling
139
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "EndBreaks Modeling");
140
                    foreach (var item in document.EndBreaks)
141
                        EndBreakModeling(item);
142

    
143
                    // LineNumber Modeling
144
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "LineNumbers Modeling");
145
                    foreach (var item in document.LINENUMBERS)
146
                        LineNumberModeling(item);
147

    
148
                    // Note Modeling
149
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Notes Modeling");
150
                    foreach (var item in document.NOTES)
151
                        NoteModeling(item);
152

    
153
                    // Text Modeling
154
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Texts Modeling");
155
                    foreach (var item in document.TEXTINFOS)
156
                        TextModeling(item);
157

    
158
                    // LineRun Line Join
159
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Join LineRuns");
160
                    foreach (LineNumber lineNumber in document.LINENUMBERS)
161
                        foreach (LineRun run in lineNumber.RUNS)
162
                            JoinRunLine(run);
163
                    // TrimLineRun Line Join
164
                    foreach (TrimLine trimLine in document.TRIMLINES)
165
                        foreach (LineRun run in trimLine.RUNS)
166
                            JoinRunLine(run);
167

    
168
                    // Input LineNumber Attribute
169
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Set Lines Attribute");
170
                    foreach (var item in document.LINENUMBERS)
171
                        InputLineNumberAttribute(item);
172

    
173
                    // Input Symbol Attribute
174
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Set Symbols Attribute");
175
                    foreach (var item in document.SYMBOLS)
176
                        InputSymbolAttribute(item, item.ATTRIBUTES);
177

    
178
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Labels Modeling");
179
                    #region Label Modeling
180
                    foreach (var item in document.SYMBOLS)
181
                        LabelSymbolModeling(item);
182
                    #endregion
183

    
184

    
185
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetProgress, AllCount);
186
                }
187
            }
188
            catch (Exception ex)
189
            {
190
                System.Windows.Forms.MessageBox.Show(ex.Message + "\r\n" + ex.StackTrace);
191
            }
192
            finally
193
            {
194
                application.ActiveWindow.Fit();
195
                
196
                if (newDrawing != null)
197
                {
198
                    radApp.ActiveDocument.SaveOnClose = false;
199
                    radApp.ActiveDocument.Save();
200
                    ReleaseCOMObjects(newDrawing);
201
                }
202

    
203
                ReleaseCOMObjects(dataSource);
204
                ReleaseCOMObjects(_placement);
205

    
206
                SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.ClearParent, null);
207
                SplashScreenManager.CloseForm(false);
208
            }
209
        }
210

    
211
        /// <summary>
212
        /// 도면 생성 메서드
213
        /// </summary>
214
        private void CreateDocument()
215
        {
216
            newDrawing = application.Drawings.Add(document.Unit, document.Template, document.DrawingNumber, document.DrawingName);
217
            application.ActiveWindow.Fit();
218
            Thread.Sleep(1000);
219
            application.ActiveWindow.Zoom = 2000;
220
            Thread.Sleep(2000);
221
        }
222

    
223
        /// <summary>
224
        /// 도면 크기 구하는 메서드
225
        /// </summary>
226
        /// <returns></returns>
227
        private bool DocumentCoordinateCorrection()
228
        {
229
            double maxX = 0;
230
            double maxY = 0;
231
            foreach (object drawingObj in radApp.ActiveDocument.ActiveSheet.DrawingObjects)
232
            {
233
                Ingr.RAD2D.SmartFrame2d smartFrame2d = drawingObj as Ingr.RAD2D.SmartFrame2d;
234
                if (smartFrame2d != null)
235
                {
236
                    double x1 = 0;
237
                    double x2 = 0;
238
                    double y1 = 0;
239
                    double y2 = 0;
240
                    smartFrame2d.Range(out x1, out y1, out x2, out y2);
241
                    maxX = Math.Max(x2, maxX);
242
                    maxY = Math.Max(y2, maxY);
243
                }
244
            }
245
            if (maxX != 0 && maxY != 0)
246
            {
247
                document.SetSPPIDLocation(maxX, maxY);
248
                return true;
249
            }
250
            else
251
                return false;
252
        }
253

    
254
        /// <summary>
255
        /// 라인을 Run 단위로 모델링하는 진입 메서드
256
        /// </summary>
257
        /// <param name="run"></param>
258
        private void LineModelingByRun(LineRun run)
259
        {
260
            Line prevLine = null;
261
            List<Line> lines = new List<Line>();
262
            foreach (var item in run.RUNITEMS)
263
            {
264
                // Line일 경우
265
                if (item.GetType() == typeof(Line))
266
                {
267
                    Line line = item as Line;
268
                    if (prevLine == null)
269
                        lines.Add(line);
270
                    else if (prevLine != null)
271
                    {
272
                        if (prevLine.SPPID.MAPPINGNAME == line.SPPID.MAPPINGNAME)
273
                            lines.Add(line);
274
                        else
275
                        {
276
                            if (lines.Count > 0)
277
                            {
278
                                LineModeling(lines);
279
                                lines.Clear();
280
                            }
281
                            lines.Add(line);
282
                        }
283
                    }
284

    
285
                    prevLine = line;
286

    
287
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetProgress, ++CurrentCount);
288
                }
289
                // Symbol 일 경우
290
                else if (item.GetType() == typeof(Symbol))
291
                {
292
                    if (lines.Count > 0)
293
                    {
294
                        LineModeling(lines);
295
                        lines.Clear();
296
                    }
297
                }
298
            }
299

    
300
            if (lines.Count > 0)
301
                LineModeling(lines);
302
        }
303

    
304
        /// <summary>
305
        /// 심볼을 Run 단위로 모델링하는 진입 메서드
306
        /// </summary>
307
        /// <param name="run"></param>
308
        private void SymbolModelingByRun(LineRun run)
309
        {
310
            // 양끝 Symbol 검사 후 Line이 나올때까지만 Symbol Modeling
311
            if (run.RUNITEMS.Count > 0)
312
            {
313
                if (run.RUNITEMS[0].GetType() == typeof(Symbol))
314
                    SymbolModelingByRunStart(run.RUNITEMS[0] as Symbol, run);
315

    
316
                if (run.RUNITEMS[run.RUNITEMS.Count - 1].GetType() == typeof(Symbol))
317
                    SymbolModelingByRunEnd(run.RUNITEMS[run.RUNITEMS.Count - 1] as Symbol, run);
318
            }
319

    
320
            Symbol prevSymbol = null;
321
            Symbol targetSymbol = null;
322
            foreach (var item in run.RUNITEMS)
323
            {
324
                if (item.GetType() == typeof(Symbol))
325
                {
326
                    Symbol symbol = item as Symbol;
327
                    SymbolModeling(symbol, targetSymbol, prevSymbol);
328
                    prevSymbol = symbol;
329
                    targetSymbol = symbol;
330
                }
331
                else
332
                {
333
                    targetSymbol = null;
334
                }
335
            }
336
        }
337

    
338
        /// <summary>
339
        /// Run에 있는 심볼을 모델링하는데 기준이 Run의 시작점
340
        /// </summary>
341
        /// <param name="symbol"></param>
342
        /// <param name="run"></param>
343
        private void SymbolModelingByRunStart(Symbol symbol, LineRun run)
344
        {
345
            foreach (var connector in symbol.CONNECTORS)
346
            {
347
                object targetItem = SPPIDUtil.FindObjectByUID(document, connector.CONNECTEDITEM);
348
                if (targetItem != null &&
349
                    (targetItem.GetType() == typeof(Symbol) || targetItem.GetType() == typeof(Equipment)) &&
350
                    !IsSameLineRun(symbol, targetItem))
351
                {
352
                    SymbolModeling(symbol, targetItem as Symbol, null);
353
                    for (int i = 1; i < run.RUNITEMS.Count; i++)
354
                    {
355
                        object item = run.RUNITEMS[i];
356
                        if (item.GetType() == typeof(Symbol))
357
                            SymbolModeling(item as Symbol, run.RUNITEMS[i - 1] as Symbol, null);
358
                        else
359
                            break;
360
                    }
361
                    break;
362
                }
363
            }
364

    
365

    
366
        }
367

    
368
        /// <summary>
369
        /// Run에 있는 심볼을 모델링하는데 기준이 Run의 끝점
370
        /// </summary>
371
        /// <param name="symbol"></param>
372
        /// <param name="run"></param>
373
        private void SymbolModelingByRunEnd(Symbol symbol, LineRun run)
374
        {
375
            foreach (var connector in symbol.CONNECTORS)
376
            {
377
                object targetItem = SPPIDUtil.FindObjectByUID(document, connector.CONNECTEDITEM);
378
                if (targetItem != null &&
379
                    (targetItem.GetType() == typeof(Symbol) || targetItem.GetType() == typeof(Equipment)) &&
380
                    !IsSameLineRun(symbol, targetItem))
381
                {
382
                    SymbolModeling(symbol, targetItem as Symbol, null);
383
                    for (int i = run.RUNITEMS.Count - 2; i >= 0; i--)
384
                    {
385
                        object item = run.RUNITEMS[i];
386
                        if (item.GetType() == typeof(Symbol))
387
                            SymbolModeling(item as Symbol, run.RUNITEMS[i + 1] as Symbol, null);
388
                        else
389
                            break;
390
                    }
391
                    break;
392
                }
393
            }
394
        }
395

    
396
        /// <summary>
397
        /// 심볼을 실제로 Modeling 메서드
398
        /// </summary>
399
        /// <param name="symbol"></param>
400
        /// <param name="targetSymbol"></param>
401
        /// <param name="prevSymbol"></param>
402
        private void SymbolModeling(Symbol symbol, Symbol targetSymbol, Symbol prevSymbol)
403
        {
404
            // OWNERSYMBOL Attribute, 값을 가지고 있을 경우
405
            BaseModel.Attribute itemAttribute = symbol.ATTRIBUTES.Find(attr => attr.ATTRIBUTE == "OWNERSYMBOL");
406
            if (itemAttribute != null && string.IsNullOrEmpty(itemAttribute.VALUE) && itemAttribute.VALUE != "None")
407
                return;
408
            // 이미 모델링 됐을 경우
409
            else if (!string.IsNullOrEmpty(symbol.SPPID.RepresentationId))
410
                return;
411

    
412
            LMSymbol _LMSymbol = null;
413

    
414
            string mappingPath = symbol.SPPID.MAPPINGNAME;
415
            double x = symbol.SPPID.ORIGINAL_X;
416
            double y = symbol.SPPID.ORIGINAL_Y;
417
            int mirror = 0;
418
            double angle = symbol.ANGLE;
419
            
420
            // OPC 일경우 180도 일때 Mirror
421
            if (mappingPath.Contains("Piping OPC's") && angle == Math.PI)
422
                mirror = 1;
423

    
424
            if (targetSymbol != null && !string.IsNullOrEmpty(targetSymbol.SPPID.RepresentationId))
425
            {
426
                LMSymbol _TargetItem = dataSource.GetSymbol(targetSymbol.SPPID.RepresentationId);
427

    
428
                Ingr.RAD2D.Symbol2d symbol2d = radApp.ActiveDocument.ActiveSheet.DrawingObjects[_TargetItem.get_GraphicOID().ToString()];
429
                double x1 = 0;
430
                double x2 = 0;
431
                double y1 = 0;
432
                double y2 = 0;
433
                symbol2d.Range(out x1, out y1, out x2, out y2);
434

    
435
                if (y2 < y)
436
                    y = y2;
437
                else if (y1 > y)
438
                    y = y1;
439

    
440
                if (x2 < x)
441
                    x = x2;
442
                else if (x1 > x)
443
                    x = x1;
444

    
445
                _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle, TargetItem: _TargetItem);
446
                ReleaseCOMObjects(_TargetItem);
447
            }
448
            else if (prevSymbol != null)
449
            {
450
                LMSymbol _PrevSymbol = dataSource.GetSymbol(prevSymbol.SPPID.RepresentationId);
451
                SlopeType slopeType = SPPIDUtil.CalcSlope(x, y, prevSymbol.SPPID.ORIGINAL_X, prevSymbol.SPPID.ORIGINAL_Y);
452
                double prevX = _PrevSymbol.get_XCoordinate();
453
                double prevY = _PrevSymbol.get_YCoordinate();
454
                if (slopeType == SlopeType.HORIZONTAL)
455
                    y = prevY;
456
                else if (slopeType == SlopeType.VERTICAL)
457
                    x = prevX;
458
                _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle);
459
            }
460
            else
461
                _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle);
462

    
463

    
464
            if (_LMSymbol != null)
465
            {
466
                _LMSymbol.Commit();
467
                symbol.SPPID.RepresentationId = _LMSymbol.AsLMRepresentation().Id;
468

    
469
                foreach (var item in symbol.ChildSymbols)
470
                    CreateChildSymbol(item, _LMSymbol);
471
            }
472

    
473
            ReleaseCOMObjects(_LMSymbol);
474
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetProgress, ++CurrentCount);
475
        }
476

    
477
        private void LabelSymbolModeling(Symbol symbol)
478
        {
479
            BaseModel.Attribute itemAttribute = symbol.ATTRIBUTES.Find(x => x.ATTRIBUTE == "OWNERSYMBOL");
480
            if (itemAttribute == null || string.IsNullOrEmpty(itemAttribute.VALUE))
481
                return;
482

    
483
            Array points = new double[] { 0, symbol.SPPID.ORIGINAL_X, symbol.SPPID.ORIGINAL_Y };
484
            
485
            string symbolUID = itemAttribute.VALUE;
486
            object targetItem = SPPIDUtil.FindObjectByUID(document, symbolUID);
487
            if (targetItem != null)
488
            {
489
                // Object 아이템이 Symbol일 경우 Equipment일 경우 
490
                string sRep = null;
491
                if (targetItem.GetType() == typeof(Symbol))
492
                    sRep = ((Symbol)targetItem).SPPID.RepresentationId;
493
                else if (targetItem.GetType() == typeof(Equipment))
494
                    sRep = ((Equipment)targetItem).SPPID.RepresentationId;
495

    
496
                if (!string.IsNullOrEmpty(sRep))
497
                {
498
                    // LEADER Line 검사
499
                    bool leaderLine = false;
500
                    SymbolMapping symbolMapping = document.SymbolMappings.Find(x => x.UID == symbol.DBUID);
501
                    if (symbolMapping != null)
502
                        leaderLine = symbolMapping.LEADERLINE;
503

    
504
                    // Target Symbol Item 가져오고 Label Modeling
505
                    LMSymbol _TargetItem = dataSource.GetSymbol(sRep);
506
                    LMLabelPersist _LMLabelPresist = _placement.PIDPlaceLabel(symbol.SPPID.MAPPINGNAME, ref points, Rotation: 0, LabeledItem: _TargetItem.AsLMRepresentation(), IsLeaderVisible: leaderLine);
507

    
508
                    // Target Item에 Label의 Attribute Input
509
                    InputSymbolAttribute(targetItem, symbol.ATTRIBUTES);
510

    
511
                    //Leader 선 센터로
512
                    string OID = _LMLabelPresist.get_GraphicOID();
513
                    DependencyObject dependency = radApp.ActiveDocument.ActiveSheet.DrawingObjects[OID] as DependencyObject;
514
                    if (dependency != null)
515
                    {
516
                        bool result = false;
517
                        foreach (var attributes in dependency.AttributeSets)
518
                        {
519
                            foreach (var attribute in attributes)
520
                            {
521
                                string name = attribute.Name;
522
                                string value = attribute.GetValue().ToString();
523
                                if (name == "DrawingItemType" && value == "LabelPersist")
524
                                {
525
                                    foreach (DrawingObjectBase drawingObject in dependency.DrawingObjects)
526
                                    {
527
                                        if (drawingObject.Type == Ingr.RAD2D.ObjectType.igLineString2d)
528
                                        {
529
                                            Ingr.RAD2D.LineString2d lineString2D = drawingObject as Ingr.RAD2D.LineString2d;
530
                                            double prevX = _TargetItem.get_XCoordinate();
531
                                            double prevY = _TargetItem.get_YCoordinate();
532
                                            lineString2D.InsertVertex(lineString2D.VertexCount, prevX, prevY);
533
                                            lineString2D.RemoveVertex(lineString2D.VertexCount);
534
                                            result = true;
535
                                            break;
536
                                        }
537
                                    }
538
                                }
539

    
540
                                if (result)
541
                                    break;
542
                            }
543

    
544
                            if (result)
545
                                break;
546
                        }
547
                    }
548

    
549
                    _LMLabelPresist.Commit();
550
                    ReleaseCOMObjects(_TargetItem);
551
                    ReleaseCOMObjects(_LMLabelPresist);
552
                }
553
            }
554

    
555
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetProgress, ++CurrentCount);
556
        }
557

    
558
        /// <summary>
559
        /// Equipment를 실제로 Modeling 메서드
560
        /// </summary>
561
        /// <param name="equipment"></param>
562
        private void EquipmentModeling(Equipment equipment)
563
        {
564
            if (!string.IsNullOrEmpty(equipment.SPPID.RepresentationId))
565
                return;
566

    
567
            LMSymbol _LMSymbol = null;
568
            LMSymbol targetItem = null;
569
            string mappingPath = equipment.SPPID.MAPPINGNAME;
570
            double x = equipment.SPPID.ORIGINAL_X;
571
            double y = equipment.SPPID.ORIGINAL_Y;
572
            int mirror = 0;
573
            double angle = equipment.ANGLE;
574

    
575
            Connector connector = equipment.CONNECTORS.Find(conn => !string.IsNullOrEmpty(conn.CONNECTEDITEM) && conn.CONNECTEDITEM != "None");
576
            if (connector != null)
577
            {
578
                Equipment connEquipment = SPPIDUtil.FindObjectByUID(document, connector.CONNECTEDITEM) as Equipment;
579
                if (connEquipment != null)
580
                {
581
                    if (string.IsNullOrEmpty(connEquipment.SPPID.RepresentationId))
582
                        EquipmentModeling(connEquipment);
583

    
584
                    if (!string.IsNullOrEmpty(connEquipment.SPPID.RepresentationId))
585
                    {
586
                        targetItem = dataSource.GetSymbol(connEquipment.SPPID.RepresentationId);
587
                        if (targetItem != null)
588
                        {
589
                            _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle, TargetItem: targetItem);
590
                        }
591
                        else
592
                        {
593
                            _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle);
594
                        }
595
                    }
596
                    else
597
                    {
598
                        _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle);
599
                    }
600
                }
601
                else
602
                {
603
                    _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle);
604
                }
605
            }
606
            else
607
            {
608
                _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle);
609
            }
610

    
611
            if (_LMSymbol != null)
612
            {
613
                _LMSymbol.Commit();
614
                equipment.SPPID.RepresentationId = _LMSymbol.AsLMRepresentation().Id;
615
                ReleaseCOMObjects(_LMSymbol);
616
            }
617

    
618
            if (targetItem != null)
619
            {
620
                ReleaseCOMObjects(targetItem);
621
            }
622
            
623
            ReleaseCOMObjects(_LMSymbol);
624

    
625
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetProgress, ++CurrentCount);
626
        }
627

    
628
        /// <summary>
629
        /// 심볼을 실제로 Modeling할때 ChildSymbol이 있다면 Modeling하는 메서드
630
        /// </summary>
631
        /// <param name="childSymbol"></param>
632
        /// <param name="parentSymbol"></param>
633
        private void CreateChildSymbol(ChildSymbol childSymbol, LMSymbol parentSymbol)
634
        {
635
            Ingr.RAD2D.Symbol2d symbol2d = radApp.ActiveDocument.ActiveSheet.DrawingObjects[parentSymbol.get_GraphicOID().ToString()];
636
            double x1 = 0;
637
            double x2 = 0;
638
            double y1 = 0;
639
            double y2 = 0;
640
            symbol2d.Range(out x1, out y1, out x2, out y2);
641

    
642
            LMSymbol _LMSymbol = _placement.PIDPlaceSymbol(childSymbol.SPPID.MAPPINGNAME, (x1 + x2) / 2, (y1 + y2) / 2, TargetItem: parentSymbol);
643
            if (_LMSymbol != null)
644
            {
645
                childSymbol.SPPID.RepresentationId = _LMSymbol.AsLMRepresentation().Id;
646
                foreach (var item in childSymbol.ChildSymbols)
647
                    CreateChildSymbol(item, _LMSymbol);
648
            }
649
            
650

    
651
            ReleaseCOMObjects(_LMSymbol);
652
        }
653

    
654
        /// <summary>
655
        /// item이 TargetItem과 같은 LineRun에 있는지 검사
656
        /// </summary>
657
        /// <param name="item"></param>
658
        /// <param name="targetItem"></param>
659
        /// <returns></returns>
660
        private bool IsSameLineRun(object item, object targetItem)
661
        {
662
            foreach (var lineNumber in document.LINENUMBERS)
663
            {
664
                foreach (var run in lineNumber.RUNS)
665
                {
666
                    foreach (var runItem in run.RUNITEMS)
667
                    {
668
                        if (runItem == item)
669
                        {
670
                            foreach (var findItem in run.RUNITEMS)
671
                            {
672
                                if (findItem == targetItem)
673
                                {
674
                                    return true;
675
                                }
676
                            }
677

    
678
                            return false;
679

    
680
                        }
681
                    }
682
                }
683
            }
684

    
685
            return false;
686
        }
687

    
688
        /// <summary>
689
        /// Line을 실제로 모델링하는 메서드
690
        /// </summary>
691
        /// <param name="lines"></param>
692
        private void LineModeling(List<Line> lines)
693
        {
694
            _LMAItem _LMAItem = _placement.PIDCreateItem(lines[0].SPPID.MAPPINGNAME);
695
            PlaceRunInputs placeRunInputs = new PlaceRunInputs();
696
            LMSymbol _LMSymbol1 = null;
697
            LMSymbol _LMSymbol2 = null;
698
            Dictionary<LMConnector, List<double[]>> connectorVertices1 = new Dictionary<LMConnector, List<double[]>>();
699
            LMConnector targetConnector1 = null;
700
            Dictionary<LMConnector, List<double[]>> connectorVertices2 = new Dictionary<LMConnector, List<double[]>>();
701
            LMConnector targetConnector2 = null;
702

    
703
            Line startBranchLine = null;
704
            Line endBranchLine = null;
705

    
706
            for (int i = 0; i < lines.Count; i++)
707
            {
708
                Line line = lines[i];
709
                if (i == 0 || i + 1 != lines.Count)
710
                {
711
                    // 시작점에 연결된 Symbol 찾기
712
                    object connItem = SPPIDUtil.FindObjectByUID(document, line.CONNECTORS[0].CONNECTEDITEM);
713
                    if (connItem != null && connItem.GetType() == typeof(Symbol))
714
                    {
715
                        _LMSymbol1 = GetTargetSymbol(connItem as Symbol, line);
716
                        if (_LMSymbol1 != null)
717
                            placeRunInputs.AddSymbolTarget(_LMSymbol1, line.SPPID.START_X, line.SPPID.START_Y);
718
                        else
719
                            placeRunInputs.AddPoint(line.SPPID.START_X, line.SPPID.START_Y);
720
                    }
721
                    else if (connItem != null && connItem.GetType() == typeof(Line) && !lines.Contains(connItem))
722
                    {
723
                        connectorVertices1 = GetPipeRunVertices(((Line)connItem).SPPID.ModelItemId);
724
                        targetConnector1 = FindTargetLMConnector(connectorVertices1, line.SPPID.START_X, line.SPPID.START_Y, line.SPPID.END_X, line.SPPID.END_Y);
725

    
726
                        if (targetConnector1 != null)
727
                            placeRunInputs.AddConnectorTarget(targetConnector1, line.SPPID.START_X, line.SPPID.START_Y);
728
                        else
729
                        {
730
                            startBranchLine = connItem as Line;
731
                            placeRunInputs.AddPoint(line.SPPID.START_X, line.SPPID.START_Y);
732
                        }
733
                    }
734
                    else
735
                        placeRunInputs.AddPoint(line.SPPID.START_X, line.SPPID.START_Y);
736
                }
737

    
738
                if (i + 1 == lines.Count)
739
                {
740
                    // 끝점에 연결된 Symbol 찾기
741
                    object connItem = SPPIDUtil.FindObjectByUID(document, line.CONNECTORS[1].CONNECTEDITEM);
742

    
743
                    if (i != 0)
744
                        placeRunInputs.AddPoint(line.SPPID.START_X, line.SPPID.START_Y);
745

    
746
                    if (connItem != null && connItem.GetType() == typeof(Symbol))
747
                    {
748
                        _LMSymbol2 = GetTargetSymbol(connItem as Symbol, line);
749
                        if (_LMSymbol2 != null)
750
                            placeRunInputs.AddSymbolTarget(_LMSymbol2, line.SPPID.END_X, line.SPPID.END_Y);
751
                        else
752
                            placeRunInputs.AddPoint(line.SPPID.END_X, line.SPPID.END_Y);
753
                            
754
                    }
755
                    else if (connItem != null && connItem.GetType() == typeof(Line) && !lines.Contains(connItem))
756
                    {
757
                        connectorVertices2 = GetPipeRunVertices(((Line)connItem).SPPID.ModelItemId);
758
                        targetConnector2 = FindTargetLMConnector(connectorVertices2, line.SPPID.END_X, line.SPPID.END_Y, line.SPPID.START_X, line.SPPID.START_Y);
759

    
760
                        if (targetConnector2 != null)
761
                            placeRunInputs.AddConnectorTarget(targetConnector2, line.SPPID.END_X, line.SPPID.END_Y);
762
                        else
763
                        {
764
                            endBranchLine = connItem as Line;
765
                            placeRunInputs.AddPoint(line.SPPID.END_X, line.SPPID.END_Y);
766
                        }
767
                    }
768
                    else
769
                    {
770
                        placeRunInputs.AddPoint(line.SPPID.END_X, line.SPPID.END_Y);
771
                    }
772
                }
773
            }
774

    
775
            LMConnector _lMConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
776

    
777
            if (_lMConnector != null)
778
            {
779
                foreach (var line in lines)
780
                    line.SPPID.ModelItemId = _lMConnector.ModelItemID;
781
                _lMConnector.Commit();
782
                if (startBranchLine != null || endBranchLine != null)
783
                {
784
                    BranchLines.Add(new Tuple<string, Line, Line>(_lMConnector.ModelItemID, startBranchLine, endBranchLine));
785
                }
786
            }
787

    
788

    
789
            if (_LMSymbol1 != null)
790
                ReleaseCOMObjects(_LMSymbol1);
791
            if (_LMSymbol2 != null)
792
                ReleaseCOMObjects(_LMSymbol2);
793
            if (targetConnector1 != null)
794
                ReleaseCOMObjects(targetConnector1);
795
            if (targetConnector2 != null)
796
                ReleaseCOMObjects(targetConnector2);
797
            foreach (var item in connectorVertices1)
798
                ReleaseCOMObjects(item.Key);
799
            foreach (var item in connectorVertices2)
800
                ReleaseCOMObjects(item.Key);
801

    
802
            ReleaseCOMObjects(_lMConnector);
803
            ReleaseCOMObjects(placeRunInputs);
804
            ReleaseCOMObjects(_LMAItem);
805
        }
806

    
807
        /// <summary>
808
        /// Symbol이 모델링된 SPPPID Symbol Object를 반환 - 연결된 Symbol이 ChildSymbol일 수도 있기때문에 메서드 개발
809
        /// </summary>
810
        /// <param name="symbol"></param>
811
        /// <param name="line"></param>
812
        /// <returns></returns>
813
        private LMSymbol GetTargetSymbol(Symbol symbol, Line line)
814
        {
815
            LMSymbol _LMSymbol = null;
816
            foreach (var connector in symbol.CONNECTORS)
817
            {
818
                if (connector.CONNECTEDITEM == line.UID)
819
                {
820
                    if (connector.Index == 0)
821
                        _LMSymbol = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
822
                    else
823
                    {
824
                        ChildSymbol child = null;
825
                        foreach (var childSymbol in symbol.ChildSymbols)
826
                        {
827
                            if (childSymbol.Connectors.Contains(connector))
828
                                child = childSymbol;
829
                            else
830
                                child = GetChildSymbolByConnector(childSymbol, connector);
831

    
832
                            if (child != null)
833
                                break;
834
                        }
835

    
836
                        if (child != null)
837
                            _LMSymbol = dataSource.GetSymbol(child.SPPID.RepresentationId);
838
                    }
839

    
840
                    break;  
841
                }
842
            }
843

    
844
            return _LMSymbol;
845
        }
846

    
847
        /// <summary>
848
        /// Connector를 가지고 있는 ChildSymbol Object 반환
849
        /// </summary>
850
        /// <param name="item"></param>
851
        /// <param name="connector"></param>
852
        /// <returns></returns>
853
        private ChildSymbol GetChildSymbolByConnector(ChildSymbol item, Connector connector)
854
        {
855
            foreach (var childSymbol in item.ChildSymbols)
856
            {
857
                if (childSymbol.Connectors.Contains(connector))
858
                    return childSymbol;
859
                else
860
                    return GetChildSymbolByConnector(childSymbol, connector);
861
            }
862

    
863
            return null;
864
        }
865

    
866
        /// <summary>
867
        /// Branch 라인을 다시 모델링하는 진입 메서드
868
        /// </summary>
869
        /// <param name="branch"></param>
870
        private void BranchLineModeling(Tuple<string, Line, Line> branch)
871
        {
872
            List<Line> lines = SPPIDUtil.FindLinesByModelId(document, branch.Item1);
873
            Dictionary<LMConnector, List<double[]>> connectorVertices = GetPipeRunVertices(branch.Item1);
874

    
875
            LMConnector _StartConnector = null;
876
            LMConnector _EndConnector = null;
877
            double lengthStart = double.MaxValue;
878
            double lengthEnd = double.MaxValue;
879
            List<double[]> startPoints = new List<double[]>();
880
            List<double[]> endPoints = new List<double[]>();
881

    
882
            foreach (var item in connectorVertices)
883
            {
884
                foreach (var point in item.Value)
885
                {
886
                    // Start Point가 Branch
887
                    if (branch.Item2 != null)
888
                    {
889
                        Line targetLine = branch.Item2;
890
                        double distance = SPPIDUtil.CalcLineToPointDistance(targetLine.SPPID.START_X, targetLine.SPPID.START_Y, targetLine.SPPID.END_X, targetLine.SPPID.END_Y, point[0], point[1]);
891
                        if (lengthStart > distance)
892
                        {
893
                            _StartConnector = item.Key;
894
                            lengthStart = distance;
895
                            startPoints = item.Value;
896
                        }
897
                    }
898
                    // End Point가 Branch
899
                    if (branch.Item3 != null)
900
                    {
901
                        Line targetLine = branch.Item3;
902
                        double distance = SPPIDUtil.CalcLineToPointDistance(targetLine.SPPID.START_X, targetLine.SPPID.START_Y, targetLine.SPPID.END_X, targetLine.SPPID.END_Y, point[0], point[1]);
903
                        if (lengthEnd > distance)
904
                        {
905
                            _EndConnector = item.Key;
906
                            lengthEnd = distance;
907
                            endPoints = item.Value;
908
                        }
909
                    }
910
                }
911
            }
912
            #region Branch가 양쪽 전부일 때
913
            if (_StartConnector != null && _StartConnector == _EndConnector)
914
            {
915
                _placement.PIDRemovePlacement(_StartConnector.AsLMRepresentation());
916

    
917
                _LMAItem _LMAItem = _placement.PIDCreateItem(lines[0].SPPID.MAPPINGNAME);
918
                PlaceRunInputs placeRunInputs = new PlaceRunInputs();
919

    
920
                Dictionary<LMConnector, List<double[]>> startConnectorVertices = GetPipeRunVertices(branch.Item2.SPPID.ModelItemId);
921
                LMConnector _StartTargetConnector = FindTargetLMConnector(startConnectorVertices, startPoints[0][0], startPoints[0][1], startPoints[1][0], startPoints[1][1]);
922
                Dictionary<LMConnector, List<double[]>> endConnectorVertices = GetPipeRunVertices(branch.Item3.SPPID.ModelItemId);
923
                LMConnector _EndTargetConnector = FindTargetLMConnector(endConnectorVertices,
924
                   startPoints[startPoints.Count - 1][0],
925
                   startPoints[startPoints.Count - 1][1],
926
                   startPoints[startPoints.Count - 2][0],
927
                   startPoints[startPoints.Count - 2][1]);
928

    
929
                for (int i = 0; i < startPoints.Count; i++)
930
                {
931
                    double[] point = startPoints[i];
932
                    if (i == 0)
933
                        placeRunInputs.AddConnectorTarget(_StartTargetConnector, point[0], point[1]);
934
                    else if (i == startPoints.Count - 1)
935
                        placeRunInputs.AddConnectorTarget(_EndTargetConnector, point[0], point[1]);
936
                    else
937
                        placeRunInputs.AddPoint(point[0], point[1]);
938
                }
939

    
940
                LMConnector _LMConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
941
                if (_LMConnector != null)
942
                {
943
                    _LMConnector.Commit();
944
                    foreach (var item in lines)
945
                        item.SPPID.ModelItemId = _LMConnector.ModelItemID;
946
                }
947

    
948
                foreach (var item in startConnectorVertices)
949
                    ReleaseCOMObjects(item.Key);
950
                foreach (var item in endConnectorVertices)
951
                    ReleaseCOMObjects(item.Key);
952
                ReleaseCOMObjects(placeRunInputs);
953
                ReleaseCOMObjects(_LMAItem);
954
                ReleaseCOMObjects(_LMConnector);
955
            }
956
            #endregion
957
            #region 양쪽이 다른 Branch 
958
            else
959
            {
960
                // Branch 시작 Connector
961
                if (_StartConnector != null)
962
                    BranchLineModelingByConnector(branch, _StartConnector, startPoints, true);
963

    
964
                // Branch 끝 Connector
965
                if (_EndConnector != null)
966
                    BranchLineModelingByConnector(branch, _EndConnector, endPoints, false);
967
            }
968
            #endregion
969

    
970
            if (_StartConnector != null)
971
                ReleaseCOMObjects(_StartConnector);
972
            if (_EndConnector != null)
973
                ReleaseCOMObjects(_EndConnector);
974
            foreach (var item in connectorVertices)
975
                ReleaseCOMObjects(item.Key);
976
        }
977

    
978
        /// <summary>
979
        /// Branch 라인을 다시 실제로 모델링하는 메서드
980
        /// </summary>
981
        /// <param name="branch"></param>
982
        /// <param name="_Connector"></param>
983
        /// <param name="points"></param>
984
        /// <param name="IsStart"></param>
985
        private void BranchLineModelingByConnector(Tuple<string, Line, Line> branch, LMConnector _Connector, List<double[]> points, bool IsStart)
986
        {
987
            List<Line> lines = SPPIDUtil.FindLinesByModelId(document, branch.Item1);
988
            Dictionary<LMConnector, List<double[]>> connectorVertices = GetPipeRunVertices(branch.Item1);
989
            LMConnector _SameRunTargetConnector = null;
990
            LMSymbol _SameRunTargetSymbol = null;
991
            Dictionary<LMConnector, List<double[]>> branchConnectorVertices = null;
992
            LMConnector _BranchTargetConnector = null;
993
            PlaceRunInputs placeRunInputs = new PlaceRunInputs();
994

    
995
            // 같은 Line Run의 Connector 찾기
996
            foreach (var item in connectorVertices)
997
            {
998
                if (item.Key == _Connector)
999
                    continue;
1000

    
1001
                if (IsStart &&
1002
                    !DBNull.Value.Equals(item.Key.ConnectItem1SymbolID) &&
1003
                    !DBNull.Value.Equals(_Connector.ConnectItem2SymbolID)&& 
1004
                    item.Key.ConnectItem1SymbolID == _Connector.ConnectItem2SymbolID)
1005
                {
1006
                    _SameRunTargetConnector = item.Key;
1007
                    break;
1008
                }
1009
                else if (!IsStart &&
1010
                    !DBNull.Value.Equals(item.Key.ConnectItem2SymbolID) &&
1011
                    !DBNull.Value.Equals(_Connector.ConnectItem1SymbolID) && 
1012
                    item.Key.ConnectItem2SymbolID == _Connector.ConnectItem1SymbolID)
1013
                {
1014
                    _SameRunTargetConnector = item.Key;
1015
                    break;
1016
                }
1017
            }
1018

    
1019
            // Branch 반대편이 Symbol
1020
            if (_SameRunTargetConnector == null)
1021
            {
1022
                foreach (var line in lines)
1023
                {
1024
                    foreach (var connector in line.CONNECTORS)
1025
                    {
1026
                        Symbol symbol = SPPIDUtil.FindObjectByUID(document, connector.CONNECTEDITEM) as Symbol;
1027
                        if (symbol != null)
1028
                        {
1029
                            _SameRunTargetSymbol = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
1030
                            break;
1031
                        }
1032
                    }
1033
                }
1034
            }
1035

    
1036
            // 기존 Connector 제거
1037
            _placement.PIDRemovePlacement(_Connector.AsLMRepresentation());
1038
            
1039
            // 시작 Connector일 경우 첫 Point가 TargetConnector를 찾아야함
1040
            if (IsStart)
1041
            {
1042
                branchConnectorVertices = GetPipeRunVertices(branch.Item2.SPPID.ModelItemId);
1043
                _BranchTargetConnector = FindTargetLMConnector(branchConnectorVertices, points[0][0], points[0][1], points[1][0], points[1][1]);
1044
            }
1045
            // 끝 Conenctor일 경우 마지막 Point가 TargetConnector를 찾아야함
1046
            else
1047
            {
1048
                branchConnectorVertices = GetPipeRunVertices(branch.Item3.SPPID.ModelItemId);
1049
                _BranchTargetConnector = FindTargetLMConnector(branchConnectorVertices,
1050
                    points[points.Count - 1][0],
1051
                    points[points.Count - 1][1],
1052
                    points[points.Count - 2][0],
1053
                    points[points.Count - 2][1]);
1054
            }
1055

    
1056
            for (int i = 0; i < points.Count; i++)
1057
            {
1058
                double[] point = points[i];
1059
                if (i == 0)
1060
                {
1061
                    if (IsStart)
1062
                    {
1063
                        placeRunInputs.AddConnectorTarget(_BranchTargetConnector, point[0], point[1]);
1064
                    }
1065
                    else
1066
                    {
1067
                        if (_SameRunTargetConnector != null)
1068
                            placeRunInputs.AddConnectorTarget(_SameRunTargetConnector, point[0], point[1]);
1069
                        else if (_SameRunTargetSymbol != null)
1070
                            placeRunInputs.AddSymbolTarget(_SameRunTargetSymbol, point[0], point[1]);
1071
                        else
1072
                            placeRunInputs.AddPoint(point[0], point[1]);
1073
                    }
1074
                }
1075
                else if (i == points.Count - 1)
1076
                {
1077
                    if (IsStart)
1078
                    {
1079
                        if (_SameRunTargetConnector != null)
1080
                            placeRunInputs.AddConnectorTarget(_SameRunTargetConnector, point[0], point[1]);
1081
                        else if (_SameRunTargetSymbol != null)
1082
                            placeRunInputs.AddSymbolTarget(_SameRunTargetSymbol, point[0], point[1]);
1083
                        else
1084
                            placeRunInputs.AddPoint(point[0], point[1]);
1085
                    }
1086
                    else
1087
                    {
1088
                        if (_BranchTargetConnector != null)
1089
                        {
1090
                            placeRunInputs.AddConnectorTarget(_BranchTargetConnector, point[0], point[1]);
1091
                        }
1092
                    }
1093
                }
1094
                else
1095
                    placeRunInputs.AddPoint(point[0], point[1]);
1096
            }
1097
            _LMAItem _LMAItem = _placement.PIDCreateItem(lines[0].SPPID.MAPPINGNAME);
1098
            LMConnector _LMConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
1099

    
1100
            if (_LMConnector != null)
1101
            {
1102
                if (_SameRunTargetConnector != null)
1103
                {
1104
                    JoinPipeRun(_LMConnector.ModelItemID, _SameRunTargetConnector.ModelItemID);
1105
                }
1106
                else
1107
                {
1108
                    foreach (var item in lines)
1109
                        item.SPPID.ModelItemId = _LMConnector.ModelItemID;
1110
                }
1111

    
1112
                _LMConnector.Commit();
1113
                ReleaseCOMObjects(_LMConnector);
1114
            }
1115

    
1116
            ReleaseCOMObjects(placeRunInputs);
1117
            ReleaseCOMObjects(_LMAItem);
1118
            if (_BranchTargetConnector != null)
1119
                ReleaseCOMObjects(_BranchTargetConnector);
1120
            if (_SameRunTargetConnector != null)
1121
                ReleaseCOMObjects(_SameRunTargetConnector);
1122
            if (_SameRunTargetSymbol != null)
1123
                ReleaseCOMObjects(_SameRunTargetSymbol);
1124
            foreach (var item in connectorVertices)
1125
                ReleaseCOMObjects(item.Key);
1126
            foreach (var item in branchConnectorVertices)
1127
                ReleaseCOMObjects(item.Key);
1128
        }
1129

    
1130
        /// <summary>
1131
        /// EndBreak 모델링 메서드
1132
        /// </summary>
1133
        /// <param name="endBreak"></param>
1134
        private void EndBreakModeling(EndBreak endBreak)
1135
        {
1136
            object ownerObj = SPPIDUtil.FindObjectByUID(document, endBreak.OWNER);
1137
            LMConnector targetLMConnector = null;
1138
            if (ownerObj !=null && ownerObj.GetType() == typeof(Line))
1139
            {
1140
                Line ownerLine = ownerObj as Line;
1141
                LMLabelPersist _LmLabelPersist = null;
1142
                Dictionary<LMConnector, List<double[]>> connectorVertices = GetPipeRunVertices(ownerLine.SPPID.ModelItemId);
1143

    
1144
                targetLMConnector = FindTargetLMConnectorByPoint(connectorVertices, endBreak.SPPID.ORIGINAL_X, endBreak.SPPID.ORIGINAL_Y);
1145
                
1146
                if (targetLMConnector != null)
1147
                {
1148
                    Array array = new double[] { 0, endBreak.SPPID.ORIGINAL_X, endBreak.SPPID.ORIGINAL_Y };
1149
                    _LmLabelPersist = _placement.PIDPlaceLabel(endBreak.SPPID.MAPPINGNAME, ref array, Rotation: 0, LabeledItem: targetLMConnector.AsLMRepresentation(), IsLeaderVisible: true);
1150
                }
1151

    
1152
                if (_LmLabelPersist != null)
1153
                {
1154
                    _LmLabelPersist.Commit();
1155
                    ReleaseCOMObjects(_LmLabelPersist);
1156
                }
1157
                else
1158
                    RetryEndBreakModeling(endBreak, targetLMConnector);
1159

    
1160
                foreach (var item in connectorVertices)
1161
                    ReleaseCOMObjects(item.Key);
1162

    
1163
            }
1164
            else if (ownerObj != null && ownerObj.GetType() == typeof(Symbol))
1165
            {
1166
                Symbol ownerSymbol = ownerObj as Symbol;
1167
                LMSymbol _LMSymbol = dataSource.GetSymbol(ownerSymbol.SPPID.RepresentationId);
1168

    
1169
                targetLMConnector = null;
1170
                double distance = double.MaxValue;
1171

    
1172
                foreach (LMConnector connector in _LMSymbol.Avoid1Connectors)
1173
                {
1174
                    if (connector.get_ItemStatus() == "Active")
1175
                    {
1176
                        dynamic OID = connector.get_GraphicOID();
1177
                        DependencyObject drawingObject = radApp.ActiveDocument.ActiveSheet.DrawingObjects[OID];
1178
                        Ingr.RAD2D.LineStringGeometry2d lineStringGeometry = drawingObject.GetGeometry() as Ingr.RAD2D.LineStringGeometry2d;
1179
                        int verticesCount = lineStringGeometry.VertexCount;
1180
                        double[] vertices = null;
1181
                        lineStringGeometry.GetVertices(ref verticesCount, ref vertices);
1182
                        for (int i = 0; i < verticesCount; i++)
1183
                        {
1184
                            double x = 0;
1185
                            double y = 0;
1186
                            lineStringGeometry.GetVertex(i + 1, ref x, ref y);
1187

    
1188
                            double result = SPPIDUtil.CalcPointToPointdDistance(x, y, endBreak.SPPID.ORIGINAL_X, endBreak.SPPID.ORIGINAL_Y);
1189
                            if (result < distance)
1190
                            {
1191
                                targetLMConnector = connector;
1192
                                distance = result;
1193
                            }
1194
                        }
1195
                    }
1196
                }
1197

    
1198
                foreach (LMConnector connector in _LMSymbol.Avoid2Connectors)
1199
                {
1200
                    if (connector.get_ItemStatus() == "Active")
1201
                    {
1202
                        dynamic OID = connector.get_GraphicOID();
1203
                        DependencyObject drawingObject = radApp.ActiveDocument.ActiveSheet.DrawingObjects[OID];
1204
                        Ingr.RAD2D.LineStringGeometry2d lineStringGeometry = drawingObject.GetGeometry() as Ingr.RAD2D.LineStringGeometry2d;
1205
                        int verticesCount = lineStringGeometry.VertexCount;
1206
                        double[] vertices = null;
1207
                        lineStringGeometry.GetVertices(ref verticesCount, ref vertices);
1208
                        for (int i = 0; i < verticesCount; i++)
1209
                        {
1210
                            double x = 0;
1211
                            double y = 0;
1212
                            lineStringGeometry.GetVertex(i + 1, ref x, ref y);
1213

    
1214
                            double result = SPPIDUtil.CalcPointToPointdDistance(x, y, endBreak.SPPID.ORIGINAL_X, endBreak.SPPID.ORIGINAL_Y);
1215
                            if (result < distance)
1216
                            {
1217
                                targetLMConnector = connector;
1218
                                distance = result;
1219
                            }
1220
                        }
1221
                    }
1222
                }
1223

    
1224
                if (targetLMConnector != null)
1225
                {
1226
                    LMLabelPersist _LmLabelPersist = null;
1227
                    Array array = new double[] { 0, endBreak.SPPID.ORIGINAL_X, endBreak.SPPID.ORIGINAL_Y };
1228
                    _LmLabelPersist = _placement.PIDPlaceLabel(endBreak.SPPID.MAPPINGNAME, ref array, Rotation: 0, LabeledItem: targetLMConnector.AsLMRepresentation(), IsLeaderVisible: true);
1229
                    if (_LmLabelPersist != null)
1230
                    {
1231
                        _LmLabelPersist.Commit();
1232
                        ReleaseCOMObjects(_LmLabelPersist);
1233
                    }
1234
                    else
1235
                        RetryEndBreakModeling(endBreak, targetLMConnector);
1236
                }
1237
                
1238
                ReleaseCOMObjects(_LMSymbol);
1239
            }
1240

    
1241
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetProgress, ++CurrentCount);
1242
        }
1243

    
1244
        /// <summary>
1245
        /// EndBreak 모델링이 실패시 다시 시도하는 메서드
1246
        /// </summary>
1247
        /// <param name="endBreak"></param>
1248
        /// <param name="targetLMConnector"></param>
1249
        private void RetryEndBreakModeling(EndBreak endBreak, LMConnector targetLMConnector)
1250
        {
1251
            bool isZeroLength = Convert.ToBoolean(targetLMConnector.get_IsZeroLength());
1252
            Array array = null;
1253
            LMLabelPersist _LMLabelPersist = null;
1254
            LMConnector _LMConnector = null;
1255
            dynamic OID = targetLMConnector.get_GraphicOID();
1256
            DependencyObject drawingObject = radApp.ActiveDocument.ActiveSheet.DrawingObjects[OID];
1257
            Ingr.RAD2D.LineStringGeometry2d lineStringGeometry = drawingObject.GetGeometry() as Ingr.RAD2D.LineStringGeometry2d;
1258
            int verticesCount = lineStringGeometry.VertexCount;
1259
            PlaceRunInputs placeRunInputs = new PlaceRunInputs();
1260
            _LMAItem _LMAItem = _placement.PIDCreateItem(@"\Piping\Routing\Process Lines\Primary Piping.sym");
1261

    
1262
            if (isZeroLength)
1263
            {
1264
                double[] vertices = null;
1265
                lineStringGeometry.GetVertices(ref verticesCount, ref vertices);
1266
                double x = 0;
1267
                double y = 0;
1268
                lineStringGeometry.GetVertex(1, ref x, ref y);
1269

    
1270
                placeRunInputs.AddSymbolTarget(targetLMConnector.ConnectItem1SymbolObject, x, y);
1271
                placeRunInputs.AddSymbolTarget(targetLMConnector.ConnectItem2SymbolObject, x, y);
1272

    
1273
                _placement.PIDRemovePlacement(targetLMConnector.AsLMRepresentation());
1274
                _LMConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
1275

    
1276
                array = new double[] { 0, x, y };
1277
                _LMLabelPersist = _placement.PIDPlaceLabel(endBreak.SPPID.MAPPINGNAME, ref array, Rotation: 0, LabeledItem: _LMConnector.AsLMRepresentation(), IsLeaderVisible: true);
1278

    
1279
                AutoJoinPipeRun(_LMConnector.ModelItemID);
1280
            }
1281
            else
1282
            {
1283
                List<double[]> vertices = new List<double[]>();
1284
                for (int i = 1; i <= verticesCount; i++)
1285
                {
1286
                    double x = 0;
1287
                    double y = 0;
1288
                    lineStringGeometry.GetVertex(i, ref x, ref y);
1289
                    vertices.Add(new double[] { x, y });
1290
                }
1291

    
1292
                for (int i = 0; i < vertices.Count; i++)
1293
                {
1294
                    double[] points = vertices[i];
1295
                    if (i == 0)
1296
                    {
1297
                        if (targetLMConnector.ConnectItem1SymbolObject != null)
1298
                            placeRunInputs.AddSymbolTarget(targetLMConnector.ConnectItem1SymbolObject, points[0], points[1]);
1299
                        else
1300
                            placeRunInputs.AddPoint(points[0], points[1]);
1301
                    }
1302
                    else if (i == vertices.Count - 1)
1303
                    {
1304
                        if (targetLMConnector.ConnectItem2SymbolObject != null)
1305
                            placeRunInputs.AddSymbolTarget(targetLMConnector.ConnectItem2SymbolObject, points[0], points[1]);
1306
                        else
1307
                            placeRunInputs.AddPoint(points[0], points[1]);
1308
                    }
1309
                    else
1310
                        placeRunInputs.AddPoint(points[0], points[1]);
1311
                }
1312

    
1313
                List<Line> lines = SPPIDUtil.FindLinesByModelId(document, targetLMConnector.ModelItemID);
1314
                
1315
                _placement.PIDRemovePlacement(targetLMConnector.AsLMRepresentation());
1316
                _LMConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
1317

    
1318
                array = new double[] { 0, endBreak.SPPID.ORIGINAL_X, endBreak.SPPID.ORIGINAL_Y };
1319
                _LMLabelPersist = _placement.PIDPlaceLabel(endBreak.SPPID.MAPPINGNAME, ref array, Rotation: 0, LabeledItem: _LMConnector.AsLMRepresentation(), IsLeaderVisible: true);
1320

    
1321
                AutoJoinPipeRun(_LMConnector.ModelItemID);
1322
                foreach (var line in lines)
1323
                    line.SPPID.ModelItemId = _LMConnector.ModelItemID;
1324
            }
1325

    
1326

    
1327
            if (_LMLabelPersist != null)
1328
            {
1329
                _LMLabelPersist.Commit();
1330
                ReleaseCOMObjects(_LMLabelPersist);
1331
            }
1332
            else
1333
            {
1334
                
1335
            }
1336

    
1337
            ReleaseCOMObjects(_LMConnector);
1338
            ReleaseCOMObjects(placeRunInputs);
1339
            ReleaseCOMObjects(_LMAItem);
1340
        }
1341

    
1342
        /// <summary>
1343
        /// FromModelItem을 ToModelItem으로 PipeRunJoin하는 메서드
1344
        /// </summary>
1345
        /// <param name="fromModelItemId"></param>
1346
        /// <param name="toModelItemId"></param>
1347
        private void JoinPipeRun(string fromModelItemId, string toModelItemId)
1348
        {
1349
            LMModelItem modelItem1 = dataSource.GetModelItem(toModelItemId);
1350
            _LMAItem item1 = modelItem1.AsLMAItem();
1351
            LMModelItem modelItem2 = dataSource.GetModelItem(fromModelItemId);
1352
            _LMAItem item2 = modelItem2.AsLMAItem();
1353
            
1354
            // item2가 item1으로 조인
1355
            try
1356
            {
1357
                _placement.PIDJoinRuns(ref item1, ref item2);
1358
                item1.Commit();
1359
                item2.Commit();
1360

    
1361
                List<Line> lines = SPPIDUtil.FindLinesByModelId(document, fromModelItemId);
1362
                foreach (var line in lines)
1363
                    line.SPPID.ModelItemId = toModelItemId;
1364
            }
1365
            catch (Exception ex)
1366
            {
1367
                System.Windows.Forms.MessageBox.Show(ex.Message + "\r\n" + ex.StackTrace);
1368
            }
1369
            finally
1370
            {
1371
                ReleaseCOMObjects(modelItem1);
1372
                ReleaseCOMObjects(item1);
1373
                ReleaseCOMObjects(modelItem2);
1374
                ReleaseCOMObjects(item2);
1375
            }
1376
        }
1377

    
1378
        /// <summary>
1379
        /// PipeRun을 자동으로 Join하는 메서드
1380
        /// </summary>
1381
        /// <param name="modelItemId"></param>
1382
        private void AutoJoinPipeRun(string modelItemId)
1383
        {
1384
            LMModelItem modelItem = dataSource.GetModelItem(modelItemId);
1385
            _LMAItem item = modelItem.AsLMAItem();
1386
            try
1387
            {
1388
                string modelitemID = item.Id;
1389
                _placement.PIDAutoJoin(item, AutoJoinEndConstants.autoJoin_Both, ref item);
1390
                string afterModelItemID = item.Id;
1391
                
1392
                if (modelitemID != afterModelItemID)
1393
                {
1394
                    List<Line> lines = SPPIDUtil.FindLinesByModelId(document, modelitemID);
1395
                    foreach (var line in lines)
1396
                        line.SPPID.ModelItemId = afterModelItemID;
1397
                }
1398
                item.Commit();
1399
            }
1400
            catch (Exception ex)
1401
            {
1402
                System.Windows.Forms.MessageBox.Show(ex.Message + "\r\n" + ex.StackTrace);
1403
            }
1404
            finally
1405
            {
1406
                ReleaseCOMObjects(modelItem);
1407
                ReleaseCOMObjects(item);
1408
            }
1409
        }
1410

    
1411
        /// <summary>
1412
        /// LineRun에 있는 Line들을 Join하는 진입 메서드
1413
        /// </summary>
1414
        /// <param name="run"></param>
1415
        private void JoinRunLine(LineRun run)
1416
        {
1417
            string modelItemId = string.Empty;
1418
            foreach (var item in run.RUNITEMS)
1419
            {
1420
                if (item.GetType() == typeof(Line))
1421
                {
1422
                    Line line = item as Line;
1423
                    AutoJoinPipeRun(line.SPPID.ModelItemId);
1424
                    modelItemId = line.SPPID.ModelItemId;
1425

    
1426
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetProgress, ++CurrentCount);
1427
                }
1428
            }
1429
        }
1430

    
1431
        /// <summary>
1432
        /// PipeRun의 좌표를 가져오는 메서드
1433
        /// </summary>
1434
        /// <param name="modelId"></param>
1435
        /// <returns></returns>
1436
        private Dictionary<LMConnector, List<double[]>> GetPipeRunVertices(string modelId)
1437
        {
1438
            Dictionary<LMConnector, List<double[]>> connectorVertices = new Dictionary<LMConnector, List<double[]>>();
1439
            LMModelItem modelItem = dataSource.GetModelItem(modelId);
1440

    
1441
            if (modelItem != null)
1442
            {
1443
                foreach (LMRepresentation rep in modelItem.Representations)
1444
                {
1445
                    if (rep.Attributes["RepresentationType"].get_Value() == "Connector" && rep.Attributes["ItemStatus"].get_Value() == "Active")
1446
                    {
1447
                        LMConnector _LMConnector = dataSource.GetConnector(rep.Id);
1448
                        connectorVertices.Add(_LMConnector, new List<double[]>());
1449
                        dynamic OID = rep.get_GraphicOID();
1450
                        DependencyObject drawingObject = radApp.ActiveDocument.ActiveSheet.DrawingObjects[OID];
1451
                        Ingr.RAD2D.LineStringGeometry2d lineStringGeometry = drawingObject.GetGeometry() as Ingr.RAD2D.LineStringGeometry2d;
1452
                        int verticesCount = lineStringGeometry.VertexCount;
1453
                        double[] vertices = null;
1454
                        lineStringGeometry.GetVertices(ref verticesCount, ref vertices);
1455
                        for (int i = 0; i < verticesCount; i++)
1456
                        {
1457
                            double x = 0;
1458
                            double y = 0;
1459
                            lineStringGeometry.GetVertex(i + 1, ref x, ref y);
1460
                            connectorVertices[_LMConnector].Add(new double[] { Math.Round(x, 10), Math.Round(y, 10) });
1461
                        }
1462
                    }
1463
                }
1464

    
1465
                ReleaseCOMObjects(modelItem);
1466
            }
1467

    
1468
            return connectorVertices;
1469
        }
1470

    
1471
        /// <summary>
1472
        /// 좌표로 PipeRun의 Connector중에 어느 Connector에 가까운지/붙을지 가져오는 메서드 - 두점으로 라인의 교차점을 기준으로 구함
1473
        /// </summary>
1474
        /// <param name="connectorVertices"></param>
1475
        /// <param name="connX"></param>
1476
        /// <param name="connY"></param>
1477
        /// <param name="x2"></param>
1478
        /// <param name="y2"></param>
1479
        /// <returns></returns>
1480
        private LMConnector FindTargetLMConnector(Dictionary<LMConnector, List<double[]>> connectorVertices, double connX, double connY, double x2, double y2)
1481
        {
1482
            double length = double.MaxValue;
1483
            LMConnector targetConnector = null;
1484
            foreach (var item in connectorVertices)
1485
            {
1486
                List<double[]> points = item.Value;
1487
                for (int i = 0; i < points.Count - 1; i++)
1488
                {
1489
                    double[] point1 = points[i];
1490
                    double[] point2 = points[i + 1];
1491

    
1492
                    double maxLineX = Math.Max(point1[0], point2[0]);
1493
                    double minLineX = Math.Min(point1[0], point2[0]);
1494
                    double maxLineY = Math.Max(point1[1], point2[1]);
1495
                    double minLineY = Math.Min(point1[1], point2[1]);
1496

    
1497
                    SlopeType slope = SPPIDUtil.CalcSlope(minLineX, minLineY, maxLineX, maxLineY);
1498

    
1499
                    // 두직선의 교차점
1500
                    double[] crossingPoint = SPPIDUtil.CalcLineCrossingPoint(connX, connY, x2, y2, point1[0], point1[1], point2[0], point2[1]);
1501
                    if (crossingPoint != null)
1502
                    {
1503
                        double distance = SPPIDUtil.CalcPointToPointdDistance(connX, connY, crossingPoint[0], crossingPoint[1]);
1504
                        if (length >= distance)
1505
                        {
1506
                            if (slope == SlopeType.Slope &&
1507
                                minLineX <= crossingPoint[0] && maxLineX >= crossingPoint[0] &&
1508
                                minLineY <= crossingPoint[1] && maxLineY >= crossingPoint[1])
1509
                            {
1510
                                targetConnector = item.Key;
1511
                                length = distance;
1512
                            }
1513
                            else if (slope == SlopeType.HORIZONTAL &&
1514
                                minLineX <= crossingPoint[0] && maxLineX >= crossingPoint[0])
1515
                            {
1516
                                targetConnector = item.Key;
1517
                                length = distance;
1518
                            }
1519
                            else if (slope == SlopeType.VERTICAL &&
1520
                               minLineY <= crossingPoint[1] && maxLineY >= crossingPoint[1])
1521
                            {
1522
                                targetConnector = item.Key;
1523
                                length = distance;
1524
                            }
1525
                        }
1526
                    }
1527
                }
1528

    
1529

    
1530
            }
1531

    
1532
            if (targetConnector == null)
1533
            {
1534
                foreach (var item in connectorVertices)
1535
                {
1536
                    List<double[]> points = item.Value;
1537
                    foreach (var point in points)
1538
                    {
1539
                        double distance = SPPIDUtil.CalcPointToPointdDistance(connX, connY, point[0], point[1]);
1540
                        if (length >= distance)
1541
                        {
1542
                            targetConnector = item.Key;
1543
                            length = distance;
1544
                        }
1545
                    }
1546
                }
1547

    
1548
            }
1549

    
1550
            return targetConnector;
1551
        }
1552

    
1553
        /// <summary>
1554
        /// 좌표로 PipeRun의 Connector중에 어느 Connector에 가까운지/붙을지 가져오는 메서드 - 한점으로 제일 가까운 기준으로 구함(단순)
1555
        /// </summary>
1556
        /// <param name="connectorVertices"></param>
1557
        /// <param name="connX"></param>
1558
        /// <param name="connY"></param>
1559
        /// <returns></returns>
1560
        private LMConnector FindTargetLMConnectorByPoint(Dictionary<LMConnector, List<double[]>> connectorVertices, double connX, double connY)
1561
        {
1562
            double length = double.MaxValue;
1563
            LMConnector targetConnector = null;
1564
            foreach (var item in connectorVertices)
1565
            {
1566
                List<double[]> points = item.Value;
1567

    
1568
                foreach (double[] point in points)
1569
                {
1570
                    double distance = SPPIDUtil.CalcPointToPointdDistance(point[0], point[1], connX, connY);
1571
                    if (length >= distance)
1572
                    {
1573
                        targetConnector = item.Key;
1574
                        length = distance;
1575
                    }
1576
                }
1577
            }
1578

    
1579
            return targetConnector;
1580
        }
1581

    
1582
        /// <summary>
1583
        /// 좌표로 PipeRun의 Connector중에 어느 Connector에 가까운지/붙을지 가져오는 메서드 - 조건에 안맞아서 못찾을시 제일 가까운 점으로 가져오는 방식
1584
        /// </summary>
1585
        /// <param name="connectorVertices"></param>
1586
        /// <param name="connX"></param>
1587
        /// <param name="connY"></param>
1588
        /// <returns></returns>
1589
        private LMConnector FindTargetLMConnectorForLabel(Dictionary<LMConnector, List<double[]>> connectorVertices, double connX, double connY)
1590
        {
1591
            double length = double.MaxValue;
1592
            LMConnector targetConnector = null;
1593
            foreach (var item in connectorVertices)
1594
            {
1595
                List<double[]> points = item.Value;
1596
                for (int i = 0; i < points.Count - 1; i++)
1597
                {
1598
                    double[] point1 = points[i];
1599
                    double[] point2 = points[i + 1];
1600
                    double x1 = Math.Min(point1[0], point2[0]);
1601
                    double y1 = Math.Min(point1[1], point2[1]);
1602
                    double x2 = Math.Max(point1[0], point2[0]);
1603
                    double y2 = Math.Max(point1[1], point2[1]);
1604

    
1605
                    if ((x1 <= connX && x2 >= connX) ||
1606
                        (y1 <= connY && y2 >= connY))
1607
                    {
1608
                        double distance = SPPIDUtil.CalcPointToPointdDistance(point1[0], point1[1], connX, connY);
1609
                        if (length >= distance)
1610
                        {
1611
                            targetConnector = item.Key;
1612
                            length = distance;
1613
                        }
1614

    
1615
                        distance = SPPIDUtil.CalcPointToPointdDistance(point2[0], point2[1], connX, connY);
1616
                        if (length >= distance)
1617
                        {
1618
                            targetConnector = item.Key;
1619
                            length = distance;
1620
                        }
1621
                    }
1622
                }
1623
            }
1624

    
1625
            // 못찾았을때.
1626
            length = double.MaxValue;
1627
            if (targetConnector == null)
1628
            {
1629
                foreach (var item in connectorVertices)
1630
                {
1631
                    List<double[]> points = item.Value;
1632

    
1633
                    foreach (double[] point in points)
1634
                    {
1635
                        double distance = SPPIDUtil.CalcPointToPointdDistance(point[0], point[1], connX, connY);
1636
                        if (length >= distance)
1637
                        {
1638
                            targetConnector = item.Key;
1639
                            length = distance;
1640
                        }
1641
                    }
1642
                }
1643
            }
1644

    
1645
            return targetConnector;
1646
        }
1647

    
1648
        /// <summary>
1649
        /// Line Number Symbol을 실제로 Modeling하는 메서드
1650
        /// </summary>
1651
        /// <param name="lineNumber"></param>
1652
        private void LineNumberModeling(LineNumber lineNumber)
1653
        {
1654
            Line line = SPPIDUtil.FindObjectByUID(document, lineNumber.CONNLINE) as Line;
1655
            Dictionary<LMConnector, List<double[]>> connectorVertices = GetPipeRunVertices(line.SPPID.ModelItemId);
1656
            LMConnector connectedLMConnector = FindTargetLMConnectorForLabel(connectorVertices, lineNumber.SPPID.ORIGINAL_X, lineNumber.SPPID.ORIGINAL_Y);
1657
            if (connectedLMConnector != null)
1658
            {
1659
                double x = 0;
1660
                double y = 0;
1661
                CalcLabelLocation(ref x, ref y, lineNumber.SPPID.ORIGINAL_X, lineNumber.SPPID.ORIGINAL_Y, lineNumber.SPPIDLabelLocation, _ETCSetting.LineNumberLocation);
1662

    
1663
                Array points = new double[] { 0, x, y };
1664
                LMLabelPersist _LmLabelPresist = _placement.PIDPlaceLabel(lineNumber.SPPID.MAPPINGNAME, ref points, Rotation: lineNumber.ANGLE, LabeledItem: connectedLMConnector.AsLMRepresentation(), IsLeaderVisible: false);
1665

    
1666
                foreach (var item in connectorVertices)
1667
                    ReleaseCOMObjects(item.Key);
1668
                if (_LmLabelPresist != null)
1669
                {
1670
                    _LmLabelPresist.Commit();
1671
                    lineNumber.SPPID.RepresentationId = _LmLabelPresist.AsLMRepresentation().Id;
1672
                    ReleaseCOMObjects(_LmLabelPresist);
1673
                }
1674
                else
1675
                {
1676

    
1677
                }
1678
            }
1679

    
1680
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetProgress, ++CurrentCount);
1681
        }
1682

    
1683
        /// <summary>
1684
        /// Line Number 기준으로 모든 Item에 Line Number의 Attribute Input
1685
        /// </summary>
1686
        /// <param name="lineNumber"></param>
1687
        private void InputLineNumberAttribute(LineNumber lineNumber)
1688
        {
1689
            foreach (LineRun run in lineNumber.RUNS)
1690
            {
1691
                foreach (var item in run.RUNITEMS)
1692
                {
1693
                    if (item.GetType() == typeof(Symbol))
1694
                    {
1695
                        Symbol symbol = item as Symbol;
1696
                        LMSymbol _LMSymbol = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
1697
                        LMModelItem _LMModelItem = _LMSymbol.ModelItemObject;
1698

    
1699
                        if (_LMModelItem != null && _LMModelItem.get_ItemStatus() == "Active")
1700
                        {
1701
                            foreach (var attribute in lineNumber.ATTRIBUTES)
1702
                            {
1703
                                LineNumberMapping mapping = document.LineNumberMappings.Find(x => x.UID == attribute.UID);
1704
                                if (mapping != null)
1705
                                {
1706
                                    LMAAttribute _LMAAttribute = _LMModelItem.Attributes[mapping.SPPIDATTRIBUTENAME];
1707
                                    if (_LMAAttribute != null)
1708
                                    {
1709
                                        if (DBNull.Value.Equals(_LMAAttribute.get_Value()))
1710
                                            _LMAAttribute.set_Value(attribute.VALUE);
1711
                                        else if (_LMAAttribute.get_Value() != attribute.VALUE)
1712
                                            _LMAAttribute.set_Value(attribute.VALUE);
1713
                                    }
1714
                                }
1715
                            }
1716
                            _LMModelItem.Commit();
1717
                        }
1718
                        if (_LMModelItem != null)
1719
                            ReleaseCOMObjects(_LMModelItem);
1720
                        if (_LMSymbol != null)
1721
                            ReleaseCOMObjects(_LMSymbol);
1722
                    }
1723
                    else if (item.GetType() == typeof(Line))
1724
                    {
1725
                        Line line = item as Line;
1726
                        if (line != null)
1727
                        {
1728
                            LMModelItem _LMModelItem = dataSource.GetModelItem(line.SPPID.ModelItemId);
1729
                            if (_LMModelItem != null && _LMModelItem.get_ItemStatus() == "Active")
1730
                            {
1731
                                foreach (var attribute in lineNumber.ATTRIBUTES)
1732
                                {
1733
                                    LineNumberMapping mapping = document.LineNumberMappings.Find(x => x.UID == attribute.UID);
1734
                                    if (mapping != null)
1735
                                    {
1736
                                        LMAAttribute _LMAAttribute = _LMModelItem.Attributes[mapping.SPPIDATTRIBUTENAME];
1737
                                        if (_LMAAttribute != null)
1738
                                        {
1739
                                            if (DBNull.Value.Equals(_LMAAttribute.get_Value()))
1740
                                                _LMAAttribute.set_Value(attribute.VALUE);
1741
                                            else if (_LMAAttribute.get_Value() != attribute.VALUE)
1742
                                                _LMAAttribute.set_Value(attribute.VALUE);
1743
                                            
1744
                                        }
1745
                                    }
1746
                                }
1747
                                _LMModelItem.Commit();
1748
                            }
1749
                            if (_LMModelItem != null)
1750
                                ReleaseCOMObjects(_LMModelItem);
1751
                        }
1752
                    }
1753
                }
1754
            }
1755

    
1756
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetProgress, ++CurrentCount);
1757
        }
1758

    
1759
        /// <summary>
1760
        /// Symbol Attribute 입력 메서드
1761
        /// </summary>
1762
        /// <param name="item"></param>
1763
        private void InputSymbolAttribute(object targetItem, List<BaseModel.Attribute> targetAttributes)
1764
        {
1765
            try
1766
            {
1767
                // Object 아이템이 Symbol일 경우 Equipment일 경우 
1768
                string sRep = null;
1769
                if (targetItem.GetType() == typeof(Symbol))
1770
                    sRep = ((Symbol)targetItem).SPPID.RepresentationId;
1771
                else if (targetItem.GetType() == typeof(Equipment))
1772
                    sRep = ((Equipment)targetItem).SPPID.RepresentationId;
1773
                    
1774
                if (!string.IsNullOrEmpty(sRep))
1775
                {
1776
                    LMSymbol _LMSymbol = dataSource.GetSymbol(sRep);
1777
                    LMModelItem _LMModelItem = _LMSymbol.ModelItemObject;
1778
                    LMAAttributes _Attributes = _LMModelItem.Attributes;
1779

    
1780
                    foreach (var item in targetAttributes)
1781
                    {
1782
                        AttributeMapping mapping = document.AttributeMappings.Find(x => x.UID == item.UID);
1783
                        if (mapping != null && !string.IsNullOrEmpty(item.VALUE) && item.VALUE != "None")
1784
                        {
1785
                            LMAAttribute _Attribute = _Attributes[mapping.SPPIDATTRIBUTENAME];
1786
                            if (_Attribute != null)
1787
                                _Attribute.set_Value(item.VALUE);
1788
                        }
1789
                    }
1790
                    _LMModelItem.Commit();
1791
                    
1792
                    ReleaseCOMObjects(_Attributes);
1793
                    ReleaseCOMObjects(_LMModelItem);
1794
                    ReleaseCOMObjects(_LMSymbol);
1795
                }
1796
            }
1797
            catch (Exception ex)
1798
            {
1799
                System.Windows.Forms.MessageBox.Show(ex.Message + "\r\n" + ex.StackTrace);
1800
            }
1801

    
1802
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetProgress, ++CurrentCount);
1803
        }
1804

    
1805
        /// <summary>
1806
        /// Text Modeling - Association일 경우는 Text대신 해당 맵핑된 Symbol로 모델링
1807
        /// </summary>
1808
        /// <param name="text"></param>
1809
        private void TextModeling(Text text)
1810
        {
1811
            LMSymbol _LMSymbol = null;
1812
            try
1813
            {
1814
                //if (text.ASSOCIATION && !string.IsNullOrEmpty(text.OWNER) && text.OWNER != "None")
1815
                if (text.ASSOCIATION)
1816
                {
1817
                    object owner = SPPIDUtil.FindObjectByUID(document, text.OWNER);
1818
                    if (owner.GetType() == typeof(Symbol))
1819
                    {
1820
                        Symbol symbol = owner as Symbol;
1821
                        _LMSymbol = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
1822
                        if (_LMSymbol != null)
1823
                        {
1824
                            Association association = symbol.ASSOCIATIONS.Find(x => x.VALUE == text.UID);
1825
                            List<BaseModel.Attribute> attributes = symbol.ATTRIBUTES.FindAll(x => x.ATTRIBUTETYPE == association.TYPE);
1826
                            AttributeMapping mapping = null;
1827
                            foreach (var attribute in attributes)
1828
                            {
1829
                                if (string.IsNullOrEmpty(attribute.VALUE) || attribute.VALUE == "None")
1830
                                    continue;
1831

    
1832
                                 mapping = document.AttributeMappings.Find(x => x.UID == attribute.UID && !string.IsNullOrEmpty(x.SPPIDSYMBOLNAME));
1833
                                if (mapping != null)
1834
                                    break;  
1835
                            }
1836

    
1837
                            if (mapping != null)
1838
                            {
1839
                                double x = 0;
1840
                                double y = 0;
1841

    
1842
                                CalcLabelLocation(ref x, ref y, text.SPPID.ORIGINAL_X, text.SPPID.ORIGINAL_Y, text.SPPIDLabelLocation, mapping.Location);
1843
                                Array array = new double[] { 0, x, y };
1844

    
1845
                                LMLabelPersist _LMLabelPersist = _placement.PIDPlaceLabel(mapping.SPPIDSYMBOLNAME, ref array, Rotation: text.ANGLE, LabeledItem: _LMSymbol.AsLMRepresentation(), IsLeaderVisible: mapping.LeaderLine);
1846
                                if (_LMLabelPersist!=null)
1847
                                {
1848
                                    _LMLabelPersist.Commit();
1849
                                    ReleaseCOMObjects(_LMLabelPersist);
1850
                                }
1851
                            }
1852
                        }
1853
                    }
1854
                    else if (owner.GetType() == typeof(Line))
1855
                    {
1856

    
1857
                    }
1858
                }
1859
                else
1860
                {
1861
                    LMItemNote _LMItemNote = null;
1862
                    LMAAttribute _LMAAttribute = null;
1863

    
1864
                    double x = 0;
1865
                    double y = 0;
1866

    
1867
                    CalcLabelLocation(ref x, ref y, text.SPPID.ORIGINAL_X, text.SPPID.ORIGINAL_Y, text.SPPIDLabelLocation, _ETCSetting.TextLocation);
1868

    
1869
                    _LMSymbol = _placement.PIDPlaceSymbol(text.SPPID.MAPPINGNAME, x, y);
1870
                    _LMSymbol.Commit();
1871
                    _LMItemNote = _placement.PIDDataSource.GetItemNote(_LMSymbol.ModelItemID);
1872
                    _LMItemNote.Commit();
1873
                    _LMAAttribute = _LMItemNote.Attributes["Note.Body"];
1874
                    _LMAAttribute.set_Value(text.VALUE);
1875
                    _LMItemNote.Commit();
1876

    
1877
                    if (_LMAAttribute != null)
1878
                        ReleaseCOMObjects(_LMAAttribute);
1879
                    if (_LMItemNote != null)
1880
                        ReleaseCOMObjects(_LMItemNote);
1881
                }
1882
            }
1883
            catch (Exception ex)
1884
            {
1885

    
1886
            }
1887
            finally
1888
            {
1889
                if (_LMSymbol != null)
1890
                    ReleaseCOMObjects(_LMSymbol);
1891
            }
1892

    
1893
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetProgress, ++CurrentCount);
1894
        }
1895

    
1896
        /// <summary>
1897
        /// Note Modeling
1898
        /// </summary>
1899
        /// <param name="note"></param>
1900
        private void NoteModeling(Note note)
1901
        {
1902
            LMSymbol _LMSymbol = null;
1903
            LMItemNote _LMItemNote = null;
1904
            LMAAttribute _LMAAttribute = null;
1905

    
1906
            try
1907
            {
1908
                double x = 0;
1909
                double y = 0;
1910

    
1911
                CalcLabelLocation(ref x, ref y, note.SPPID.ORIGINAL_X, note.SPPID.ORIGINAL_Y, note.SPPIDLabelLocation, _ETCSetting.NoteLocation);
1912

    
1913
                _LMSymbol = _placement.PIDPlaceSymbol(note.SPPID.MAPPINGNAME, x, y);
1914
                _LMSymbol.Commit();
1915
                _LMItemNote = _placement.PIDDataSource.GetItemNote(_LMSymbol.ModelItemID);
1916
                _LMItemNote.Commit();
1917
                _LMAAttribute = _LMItemNote.Attributes["Note.Body"];
1918
                _LMAAttribute.set_Value(note.VALUE);
1919
                _LMItemNote.Commit();
1920
            }
1921
            catch (Exception ex)
1922
            {
1923

    
1924
            }
1925
            finally
1926
            {
1927
                if (_LMAAttribute != null)
1928
                    ReleaseCOMObjects(_LMAAttribute);
1929
                if (_LMItemNote != null)
1930
                    ReleaseCOMObjects(_LMItemNote);
1931
                if (_LMSymbol != null)
1932
                    ReleaseCOMObjects(_LMSymbol);
1933
            }
1934

    
1935
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetProgress, ++CurrentCount);
1936
        }
1937

    
1938
        /// <summary>
1939
        /// Label의 좌표를 구하는 메서드(ID2 기준의 좌표 -> SPPID 좌표)
1940
        /// </summary>
1941
        /// <param name="x"></param>
1942
        /// <param name="y"></param>
1943
        /// <param name="originX"></param>
1944
        /// <param name="originY"></param>
1945
        /// <param name="SPPIDLabelLocation"></param>
1946
        /// <param name="location"></param>
1947
        private void CalcLabelLocation(ref double x, ref double y, double originX, double originY, SPPIDLabelLocationInfo SPPIDLabelLocation, Location location)
1948
        {
1949
            if (location == Location.None)
1950
            {
1951
                x = originX;
1952
                y = originY;
1953
            }
1954
            else
1955
            {
1956
                if (location.HasFlag(Location.Center))
1957
                {
1958
                    x = (SPPIDLabelLocation.X1 + SPPIDLabelLocation.X2) / 2;
1959
                    y = (SPPIDLabelLocation.Y1 + SPPIDLabelLocation.Y2) / 2;
1960
                }
1961

    
1962
                if (location.HasFlag(Location.Left))
1963
                    x = SPPIDLabelLocation.X1;
1964
                else if (location.HasFlag(Location.Right))
1965
                    x = SPPIDLabelLocation.X2;
1966

    
1967
                if (location.HasFlag(Location.Down))
1968
                    y = SPPIDLabelLocation.Y1;
1969
                else if (location.HasFlag(Location.Up))
1970
                    y = SPPIDLabelLocation.Y2;
1971
            }
1972
        }
1973

    
1974
        /// <summary>
1975
        /// ComObject를 Release
1976
        /// </summary>
1977
        /// <param name="objVars"></param>
1978
        public void ReleaseCOMObjects(params object[] objVars)
1979
        {
1980
            int intNewRefCount = 0;
1981
            foreach (object obj in objVars)
1982
            {
1983
                if (!Information.IsNothing(obj) && System.Runtime.InteropServices.Marshal.IsComObject(obj))
1984
                    intNewRefCount = intNewRefCount + System.Runtime.InteropServices.Marshal.FinalReleaseComObject(obj);
1985
            }
1986
        }
1987
    }
1988
}
클립보드 이미지 추가 (최대 크기: 500 MB)