프로젝트

일반

사용자정보

통계
| 개정판:

hytos / DTI_PID / SPPIDConverter / AutoModeling.cs @ 06b40010

이력 | 보기 | 이력해설 | 다운로드 (85.7 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
                ReleaseCOMObjects(_PrevSymbol);
459
                _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle);
460
            }
461
            else
462
                _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle);
463

    
464

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

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

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

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

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

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

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

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

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

    
541
                                if (result)
542
                                    break;
543
                            }
544

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

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

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

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

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

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

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

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

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

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

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

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

    
652
            ReleaseCOMObjects(_LMSymbol);
653
        }
654

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

    
679
                            return false;
680

    
681
                        }
682
                    }
683
                }
684
            }
685

    
686
            return false;
687
        }
688

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

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

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

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

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

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

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

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

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

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

    
789

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

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

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

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

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

    
841
                    break;  
842
                }
843
            }
844

    
845
            return _LMSymbol;
846
        }
847

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

    
864
            return null;
865
        }
866

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
1327

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

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

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

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

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

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

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

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

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

    
1466
                ReleaseCOMObjects(modelItem);
1467
            }
1468

    
1469
            return connectorVertices;
1470
        }
1471

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

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

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

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

    
1530

    
1531
            }
1532

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

    
1549
            }
1550

    
1551
            return targetConnector;
1552
        }
1553

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

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

    
1580
            return targetConnector;
1581
        }
1582

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

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

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

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

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

    
1646
            return targetConnector;
1647
        }
1648

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

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

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

    
1678
                }
1679
            }
1680

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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