프로젝트

일반

사용자정보

통계
| 개정판:

hytos / DTI_PID / SPPIDConverter / AutoModeling.cs @ 47ad9a46

이력 | 보기 | 이력해설 | 다운로드 (103 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
        List <Tuple<string, Line, Line>> BranchLines = new List<Tuple<string, Line, Line>>();
41

    
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
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetAllStep, AllCount);
103
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetDocumentName, DocumentLabelText);
104
                    //Thread.Sleep(1000);
105
                    //SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetParent, (IntPtr)radApp.HWnd);
106
                    //Thread.Sleep(1000);
107

    
108
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Group Symbol Modeling");
109
                    List<List<Symbol>> symbolGroups = SPPIDUtil.GetThreeConnectedSymbolGroup(document);
110
                    foreach (List<Symbol> symbolGroup in symbolGroups)
111
                        SymbolModelingByThreeSymbolGroup(symbolGroup);
112

    
113
                    // Equipment Modeling
114
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Equipments Modeling");
115
                    foreach (Equipment equipment in document.Equipments)
116
                        EquipmentModeling(equipment);
117

    
118
                    // LineRun Symbol Modeling
119
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Symbols Modeling");
120
                    foreach (LineNumber lineNumber in document.LINENUMBERS)
121
                        foreach (LineRun run in lineNumber.RUNS)
122
                            SymbolModelingByRun(run);
123
                    // TrimLineRun Symbol Modeling
124
                    foreach (TrimLine trimLine in document.TRIMLINES)
125
                        foreach (LineRun run in trimLine.RUNS)
126
                            SymbolModelingByRun(run);
127

    
128
                    // LineRun Line Modeling
129
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Lines Modeling");
130
                    foreach (LineNumber lineNumber in document.LINENUMBERS)
131
                        foreach (LineRun run in lineNumber.RUNS)
132
                            LineModelingByRun(run);
133
                    // TrimLineRun Line Modeling
134
                    foreach (TrimLine trimLine in document.TRIMLINES)
135
                        foreach (LineRun run in trimLine.RUNS)
136
                            LineModelingByRun(run);
137

    
138
                    // Branch Line Modeling
139
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Branch Lines Modeling");
140
                    foreach (var item in BranchLines)
141
                        BranchLineModeling(item);
142

    
143
                    // EndBreak Modeling
144
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "EndBreaks Modeling");
145
                    foreach (var item in document.EndBreaks)
146
                        EndBreakModeling(item);
147

    
148
                    // LineNumber Modeling
149
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "LineNumbers Modeling");
150
                    foreach (var item in document.LINENUMBERS)
151
                        LineNumberModeling(item);
152

    
153
                    // LineNumber Modeling
154
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Flow Mark Modeling");
155
                    foreach (var item in document.LINES)
156
                        FlowMarkModeling(item);
157

    
158
                    // Note Modeling
159
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Notes Modeling");
160
                    foreach (var item in document.NOTES)
161
                        NoteModeling(item);
162

    
163
                    // Text Modeling
164
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Texts Modeling");
165
                    foreach (var item in document.TEXTINFOS)
166
                        TextModeling(item);
167

    
168
                    // LineRun Line Join
169
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Join LineRuns");
170
                    foreach (LineNumber lineNumber in document.LINENUMBERS)
171
                        foreach (LineRun run in lineNumber.RUNS)
172
                            JoinRunLine(run);
173
                    // TrimLineRun Line Join
174
                    foreach (TrimLine trimLine in document.TRIMLINES)
175
                        foreach (LineRun run in trimLine.RUNS)
176
                            JoinRunLine(run);
177

    
178
                    // Input LineNumber Attribute
179
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Set Lines Attribute");
180
                    foreach (var item in document.LINENUMBERS)
181
                        InputLineNumberAttribute(item);
182

    
183
                    // Input Symbol Attribute
184
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Set Symbols Attribute");
185
                    foreach (var item in document.SYMBOLS)
186
                        InputSymbolAttribute(item, item.ATTRIBUTES);
187

    
188
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Labels Modeling");
189
                    foreach (var item in document.SYMBOLS)
190
                        LabelSymbolModeling(item);
191

    
192
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetProgress, AllCount);
193
                }
194
            }
195
            catch (Exception ex)
196
            {
197
                System.Windows.Forms.MessageBox.Show(ex.Message + "\r\n" + ex.StackTrace);
198
            }
199
            finally
200
            {
201
                application.ActiveWindow.Fit();
202

    
203
                if (newDrawing != null)
204
                {
205
                    radApp.ActiveDocument.SaveOnClose = false;
206
                    radApp.ActiveDocument.Save();
207
                    ReleaseCOMObjects(newDrawing);
208
                }
209

    
210
                ReleaseCOMObjects(dataSource);
211
                ReleaseCOMObjects(_placement);
212

    
213
                //SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.ClearParent, null);
214
                SplashScreenManager.CloseForm(false);
215
            }
216
        }
217

    
218
        /// <summary>
219
        /// 도면 생성 메서드
220
        /// </summary>
221
        private void CreateDocument()
222
        {
223
            newDrawing = application.Drawings.Add(document.Unit, document.Template, document.DrawingNumber, document.DrawingName);
224
            application.ActiveWindow.Fit();
225
            Thread.Sleep(1000);
226
            application.ActiveWindow.Zoom = 2000;
227
            Thread.Sleep(2000);
228
        }
229

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

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

    
292
                    prevLine = line;
293

    
294
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetProgress, ++CurrentCount);
295
                }
296
                // Symbol 일 경우
297
                else if (item.GetType() == typeof(Symbol))
298
                {
299
                    if (lines.Count > 0)
300
                    {
301
                        LineModeling(lines);
302
                        lines.Clear();
303
                    }
304
                }
305
            }
306

    
307
            if (lines.Count > 0)
308
                LineModeling(lines);
309
        }
310

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

    
323
                if (run.RUNITEMS[run.RUNITEMS.Count - 1].GetType() == typeof(Symbol))
324
                    SymbolModelingByRunEnd(run.RUNITEMS[run.RUNITEMS.Count - 1] as Symbol, run);
325
            }
326

    
327
            Symbol targetSymbol = null;
328
            foreach (var item in run.RUNITEMS)
329
            {
330
                if (item.GetType() == typeof(Symbol))
331
                {
332
                    Symbol symbol = item as Symbol;
333
                    SymbolModeling(symbol, targetSymbol);
334
                    targetSymbol = symbol;
335
                }
336
                else
337
                {
338
                    targetSymbol = null;
339
                }
340
            }
341
        }
342

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

    
370

    
371
        }
372

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

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

    
417
            LMSymbol _LMSymbol = null;
418

    
419
            string mappingPath = symbol.SPPID.MAPPINGNAME;
420
            double x = symbol.SPPID.ORIGINAL_X;
421
            double y = symbol.SPPID.ORIGINAL_Y;
422
            int mirror = 0;
423
            double angle = symbol.ANGLE;
424

    
425
            SPPIDUtil.ConvertGridPoint(ref x, ref y);
426

    
427
            // OPC 일경우 180도 일때 Mirror
428
            if (mappingPath.Contains("Piping OPC's") && angle == Math.PI)
429
                mirror = 1;
430

    
431
            if (targetSymbol != null && !string.IsNullOrEmpty(targetSymbol.SPPID.RepresentationId))
432
            {
433
                LMSymbol _TargetItem = dataSource.GetSymbol(targetSymbol.SPPID.RepresentationId);
434
                Connector connector = SPPIDUtil.FindSymbolConnectorByUID(document, symbol.UID, targetSymbol);
435
                if (connector != null)
436
                    GetTargetSymbolConnectorPoint(connector, targetSymbol, ref x, ref y);
437

    
438
                _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle, TargetItem: _TargetItem);
439
                ReleaseCOMObjects(_TargetItem);
440
            }
441
            else
442
                _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle);
443

    
444

    
445
            if (_LMSymbol != null)
446
            {
447
                _LMSymbol.Commit();
448
                symbol.SPPID.RepresentationId = _LMSymbol.AsLMRepresentation().Id;
449
                symbol.SPPID.GraphicOID = _LMSymbol.get_GraphicOID();
450

    
451
                foreach (var item in symbol.ChildSymbols)
452
                    CreateChildSymbol(item, _LMSymbol);
453
            }
454

    
455
            ReleaseCOMObjects(_LMSymbol);
456
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetProgress, ++CurrentCount);
457
        }
458

    
459
        /// <summary>
460
        /// ID2의 Symbol Width와 Height를 비교해서 상대적인 SPPID Connector좌표를 가져온다.
461
        /// </summary>
462
        /// <param name="targetConnector"></param>
463
        /// <param name="targetSymbol"></param>
464
        /// <param name="x"></param>
465
        /// <param name="y"></param>
466
        private void GetTargetSymbolConnectorPoint(Connector targetConnector, Symbol targetSymbol, ref double x, ref double y)
467
        {
468
            LMSymbol _TargetItem = dataSource.GetSymbol(targetSymbol.SPPID.RepresentationId);
469

    
470
            double[] range = null;
471
            List<double[]> points = new List<double[]>();
472
            GetSPPIDSymbolRangeAndConnectionPoints(targetSymbol, ref range, points);
473
            double x1 = range[0];
474
            double y1 = range[1];
475
            double x2 = range[2];
476
            double y2 = range[3];
477

    
478
            // Origin 기준 Connector의 위치차이
479
            double sceneX = 0;
480
            double sceneY = 0;
481
            SPPIDUtil.ConvertPointBystring(targetConnector.SCENECONNECTPOINT, ref sceneX, ref sceneY);
482
            double originX = 0;
483
            double originY = 0;
484
            SPPIDUtil.ConvertPointBystring(targetSymbol.ORIGINALPOINT, ref originX, ref originY);
485
            double gapX = originX - sceneX;
486
            double gapY = originY - sceneY;
487

    
488
            // SPPID Symbol과 ID2 심볼의 크기 차이
489
            double sizeWidth = 0;
490
            double sizeHeight = 0;
491
            SPPIDUtil.ConvertPointBystring(targetSymbol.SIZE, ref sizeWidth, ref sizeHeight);
492
            double percentX = (x2 - x1) / sizeWidth;
493
            double percentY = (y2 - y1) / sizeHeight;
494

    
495
            double SPPIDgapX = gapX * percentX;
496
            double SPPIDgapY = gapY * percentY;
497

    
498
            double[] SPPIDOriginPoint = new double[] { _TargetItem.get_XCoordinate() - SPPIDgapX, _TargetItem.get_YCoordinate() + SPPIDgapY };
499
            double distance = double.MaxValue;
500
            double[] resultPoint;
501
            foreach (var point in points)
502
            {
503
                double result = SPPIDUtil.CalcPointToPointdDistance(point[0], point[1], SPPIDOriginPoint[0], SPPIDOriginPoint[1]);
504
                if (distance > result)
505
                {
506
                    distance = result;
507
                    resultPoint = point;
508
                    x = point[0];
509
                    y = point[1];
510
                }
511
            }
512

    
513
            ReleaseCOMObjects(_TargetItem);
514
        }
515

    
516
        /// <summary>
517
        /// SPPID Symbol의 Range를 구한다.
518
        /// </summary>
519
        /// <param name="symbol"></param>
520
        /// <param name="range"></param>
521
        private void GetSPPIDSymbolRangeAndConnectionPoints(Symbol symbol, ref double[] range, List<double[]> points)
522
        {
523
            LMSymbol _TargetItem = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
524
            Ingr.RAD2D.Symbol2d symbol2d = radApp.ActiveDocument.ActiveSheet.DrawingObjects[_TargetItem.get_GraphicOID().ToString()];
525
            double x1 = 0;
526
            double y1 = 0;
527
            double x2 = 0;
528
            double y2 = 0;
529
            symbol2d.Range(out x1, out y1, out x2, out y2);
530
            range = new double[] { x1, y1, x2, y2 };
531

    
532
            for (int i = 1; i < int.MaxValue; i++)
533
            {
534
                double connX = 0;
535
                double connY = 0;
536
                if (_placement.PIDConnectPointLocation(_TargetItem, i, ref connX, ref connY))
537
                    points.Add(new double[] { connX, connY });
538
                else
539
                    break;
540
            }
541

    
542
            foreach (var childSymbol in symbol.ChildSymbols)
543
                GetSPPIDChildSymbolRange(childSymbol, ref range, points);
544

    
545
            ReleaseCOMObjects(_TargetItem);
546
        }
547

    
548
        /// <summary>
549
        /// Child Modeling 된 Symbol의 Range를 구한다.
550
        /// </summary>
551
        /// <param name="childSymbol"></param>
552
        /// <param name="range"></param>
553
        private void GetSPPIDChildSymbolRange(ChildSymbol childSymbol, ref double[] range, List<double[]> points)
554
        {
555
            LMSymbol _ChildSymbol = dataSource.GetSymbol(childSymbol.SPPID.RepresentationId);
556
            Ingr.RAD2D.Symbol2d symbol2d = radApp.ActiveDocument.ActiveSheet.DrawingObjects[_ChildSymbol.get_GraphicOID().ToString()];
557
            double x1 = 0;
558
            double y1 = 0;
559
            double x2 = 0;
560
            double y2 = 0;
561
            symbol2d.Range(out x1, out y1, out x2, out y2);
562
            range[0] = Math.Min(range[0], x1);
563
            range[1] = Math.Min(range[1], y1);
564
            range[2] = Math.Max(range[2], x2);
565
            range[3] = Math.Max(range[3], y2);
566

    
567
            for (int i = 1; i < int.MaxValue; i++)
568
            {
569
                double connX = 0;
570
                double connY = 0;
571
                if (_placement.PIDConnectPointLocation(_ChildSymbol, i, ref connX, ref connY))
572
                    points.Add(new double[] { connX, connY });
573
                else
574
                    break;
575
            }
576

    
577
            foreach (var loopChildSymbol in childSymbol.ChildSymbols)
578
                GetSPPIDChildSymbolRange(loopChildSymbol, ref range, points);
579

    
580
            ReleaseCOMObjects(_ChildSymbol);
581
        }
582

    
583
        /// <summary>
584
        /// Label Symbol Modeling
585
        /// </summary>
586
        /// <param name="symbol"></param>
587
        private void LabelSymbolModeling(Symbol symbol)
588
        {
589
            BaseModel.Attribute itemAttribute = symbol.ATTRIBUTES.Find(x => x.ATTRIBUTE == "OWNERSYMBOL");
590
            if (itemAttribute == null || string.IsNullOrEmpty(itemAttribute.VALUE))
591
                return;
592

    
593
            Array points = new double[] { 0, symbol.SPPID.ORIGINAL_X, symbol.SPPID.ORIGINAL_Y };
594
            
595
            string symbolUID = itemAttribute.VALUE;
596
            object targetItem = SPPIDUtil.FindObjectByUID(document, symbolUID);
597
            if (targetItem != null)
598
            {
599
                // Object 아이템이 Symbol일 경우 Equipment일 경우 
600
                string sRep = null;
601
                if (targetItem.GetType() == typeof(Symbol))
602
                    sRep = ((Symbol)targetItem).SPPID.RepresentationId;
603
                else if (targetItem.GetType() == typeof(Equipment))
604
                    sRep = ((Equipment)targetItem).SPPID.RepresentationId;
605

    
606
                if (!string.IsNullOrEmpty(sRep))
607
                {
608
                    // LEADER Line 검사
609
                    bool leaderLine = false;
610
                    SymbolMapping symbolMapping = document.SymbolMappings.Find(x => x.UID == symbol.DBUID);
611
                    if (symbolMapping != null)
612
                        leaderLine = symbolMapping.LEADERLINE;
613

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

    
618
                    //Leader 선 센터로
619
                    if (_LMLabelPresist != null)
620
                    {
621
                        // Target Item에 Label의 Attribute Input
622
                        InputSymbolAttribute(targetItem, symbol.ATTRIBUTES);
623

    
624
                        string OID = _LMLabelPresist.get_GraphicOID();
625
                        DependencyObject dependency = radApp.ActiveDocument.ActiveSheet.DrawingObjects[OID] as DependencyObject;
626
                        if (dependency != null)
627
                        {
628
                            bool result = false;
629
                            foreach (var attributes in dependency.AttributeSets)
630
                            {
631
                                foreach (var attribute in attributes)
632
                                {
633
                                    string name = attribute.Name;
634
                                    string value = attribute.GetValue().ToString();
635
                                    if (name == "DrawingItemType" && value == "LabelPersist")
636
                                    {
637
                                        foreach (DrawingObjectBase drawingObject in dependency.DrawingObjects)
638
                                        {
639
                                            if (drawingObject.Type == Ingr.RAD2D.ObjectType.igLineString2d)
640
                                            {
641
                                                Ingr.RAD2D.LineString2d lineString2D = drawingObject as Ingr.RAD2D.LineString2d;
642
                                                double prevX = _TargetItem.get_XCoordinate();
643
                                                double prevY = _TargetItem.get_YCoordinate();
644
                                                lineString2D.InsertVertex(lineString2D.VertexCount, prevX, prevY);
645
                                                lineString2D.RemoveVertex(lineString2D.VertexCount);
646
                                                result = true;
647
                                                break;
648
                                            }
649
                                        }
650
                                    }
651

    
652
                                    if (result)
653
                                        break;
654
                                }
655

    
656
                                if (result)
657
                                    break;
658
                            }
659
                        }
660

    
661
                        _LMLabelPresist.Commit();
662
                    }
663
                    
664
                    ReleaseCOMObjects(_TargetItem);
665
                    ReleaseCOMObjects(_LMLabelPresist);
666
                }
667
            }
668

    
669
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetProgress, ++CurrentCount);
670
        }
671

    
672
        /// <summary>
673
        /// Equipment를 실제로 Modeling 메서드
674
        /// </summary>
675
        /// <param name="equipment"></param>
676
        private void EquipmentModeling(Equipment equipment)
677
        {
678
            if (!string.IsNullOrEmpty(equipment.SPPID.RepresentationId))
679
                return;
680

    
681
            LMSymbol _LMSymbol = null;
682
            LMSymbol targetItem = null;
683
            string mappingPath = equipment.SPPID.MAPPINGNAME;
684
            double x = equipment.SPPID.ORIGINAL_X;
685
            double y = equipment.SPPID.ORIGINAL_Y;
686
            int mirror = 0;
687
            double angle = equipment.ANGLE;
688

    
689
            SPPIDUtil.ConvertGridPoint(ref x, ref y);
690

    
691
            Connector connector = equipment.CONNECTORS.Find(conn => !string.IsNullOrEmpty(conn.CONNECTEDITEM) && conn.CONNECTEDITEM != "None");
692
            if (connector != null)
693
            {
694
                Equipment connEquipment = SPPIDUtil.FindObjectByUID(document, connector.CONNECTEDITEM) as Equipment;
695
                if (connEquipment != null)
696
                {
697
                    if (string.IsNullOrEmpty(connEquipment.SPPID.RepresentationId))
698
                        EquipmentModeling(connEquipment);
699

    
700
                    if (!string.IsNullOrEmpty(connEquipment.SPPID.RepresentationId))
701
                    {
702
                        targetItem = dataSource.GetSymbol(connEquipment.SPPID.RepresentationId);
703
                        if (targetItem != null)
704
                        {
705
                            _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle, TargetItem: targetItem);
706
                        }
707
                        else
708
                        {
709
                            _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle);
710
                        }
711
                    }
712
                    else
713
                    {
714
                        _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle);
715
                    }
716
                }
717
                else
718
                {
719
                    _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle);
720
                }
721
            }
722
            else
723
            {
724
                _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle);
725
            }
726

    
727
            if (_LMSymbol != null)
728
            {
729
                _LMSymbol.Commit();
730
                equipment.SPPID.RepresentationId = _LMSymbol.AsLMRepresentation().Id;
731
                equipment.SPPID.GraphicOID = _LMSymbol.get_GraphicOID();
732
                ReleaseCOMObjects(_LMSymbol);
733
            }
734

    
735
            if (targetItem != null)
736
            {
737
                ReleaseCOMObjects(targetItem);
738
            }
739
            
740
            ReleaseCOMObjects(_LMSymbol);
741

    
742
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetProgress, ++CurrentCount);
743
        }
744

    
745
        /// <summary>
746
        /// 3개의 Symbol이 붙어있을경우 CenterSymbol을 Grid기준으로 맞추기 위해서 모델링
747
        /// </summary>
748
        /// <param name="group"></param>
749
        private void SymbolModelingByThreeSymbolGroup(List<Symbol> group)
750
        {
751
            // Group의 가운데 찾기
752
            Symbol symbol1 = null;
753
            Symbol symbol2 = null;
754
            Symbol centerSymbol = null;
755
            foreach (var symbol in group)
756
            {
757
                int count = 0;
758
                foreach (var connector in symbol.CONNECTORS)
759
                {
760
                    object item = SPPIDUtil.FindObjectByUID(document, connector.CONNECTEDITEM);
761
                    if (item != null && item.GetType() == typeof(Symbol))
762
                        count++;
763
                }
764

    
765
                // Center Symbol
766
                if (count == 2)
767
                {
768
                    SymbolModeling(symbol, null);
769
                    centerSymbol = symbol;
770
                }
771
                else if (symbol1 == null)
772
                    symbol1 = symbol;
773
                else
774
                    symbol2 = symbol;
775
            }
776

    
777
            SymbolModeling(symbol1, centerSymbol);
778
            SymbolModeling(symbol2, centerSymbol);
779
        }
780

    
781
        /// <summary>
782
        /// 심볼을 실제로 Modeling할때 ChildSymbol이 있다면 Modeling하는 메서드
783
        /// </summary>
784
        /// <param name="childSymbol"></param>
785
        /// <param name="parentSymbol"></param>
786
        private void CreateChildSymbol(ChildSymbol childSymbol, LMSymbol parentSymbol)
787
        {
788
            Ingr.RAD2D.Symbol2d symbol2d = radApp.ActiveDocument.ActiveSheet.DrawingObjects[parentSymbol.get_GraphicOID().ToString()];
789
            double x1 = 0;
790
            double x2 = 0;
791
            double y1 = 0;
792
            double y2 = 0;
793
            symbol2d.Range(out x1, out y1, out x2, out y2);
794

    
795
            LMSymbol _LMSymbol = _placement.PIDPlaceSymbol(childSymbol.SPPID.MAPPINGNAME, (x1 + x2) / 2, (y1 + y2) / 2, TargetItem: parentSymbol);
796
            if (_LMSymbol != null)
797
            {
798
                childSymbol.SPPID.RepresentationId = _LMSymbol.AsLMRepresentation().Id;
799
                foreach (var item in childSymbol.ChildSymbols)
800
                    CreateChildSymbol(item, _LMSymbol);
801
            }
802
            
803

    
804
            ReleaseCOMObjects(_LMSymbol);
805
        }
806

    
807
        /// <summary>
808
        /// item이 TargetItem과 같은 LineRun에 있는지 검사
809
        /// </summary>
810
        /// <param name="item"></param>
811
        /// <param name="targetItem"></param>
812
        /// <returns></returns>
813
        private bool IsSameLineRun(object item, object targetItem)
814
        {
815
            foreach (var lineNumber in document.LINENUMBERS)
816
            {
817
                foreach (var run in lineNumber.RUNS)
818
                {
819
                    foreach (var runItem in run.RUNITEMS)
820
                    {
821
                        if (runItem == item)
822
                        {
823
                            foreach (var findItem in run.RUNITEMS)
824
                            {
825
                                if (findItem == targetItem)
826
                                {
827
                                    return true;
828
                                }
829
                            }
830

    
831
                            return false;
832

    
833
                        }
834
                    }
835
                }
836
            }
837

    
838
            return false;
839
        }
840

    
841
        /// <summary>
842
        /// Line을 실제로 모델링하는 메서드
843
        /// </summary>
844
        /// <param name="lines"></param>
845
        private void LineModeling(List<Line> lines)
846
        {
847
            _LMAItem _LMAItem = _placement.PIDCreateItem(lines[0].SPPID.MAPPINGNAME);
848
            PlaceRunInputs placeRunInputs = new PlaceRunInputs();
849
            LMSymbol _LMSymbol1 = null;
850
            LMSymbol _LMSymbol2 = null;
851
            Dictionary<LMConnector, List<double[]>> connectorVertices1 = new Dictionary<LMConnector, List<double[]>>();
852
            LMConnector targetConnector1 = null;
853
            Dictionary<LMConnector, List<double[]>> connectorVertices2 = new Dictionary<LMConnector, List<double[]>>();
854
            LMConnector targetConnector2 = null;
855

    
856
            Line startBranchLine = null;
857
            Line endBranchLine = null;
858

    
859
            // Type, Line, TargetObjet, x, y
860
            List<Tuple<string, Line, object, double, double>> linePointInfo = new List<Tuple<string, Line, object, double, double>>();
861
            // Point 정리
862
            for (int i = 0; i < lines.Count; i++)
863
            {
864
                Line line = lines[i];
865
                if (i == 0 || i + 1 != lines.Count)
866
                {
867
                    // 시작점에 연결된 Symbol 찾기
868
                    object connItem = SPPIDUtil.FindObjectByUID(document, line.CONNECTORS[0].CONNECTEDITEM);
869
                    if (connItem != null && connItem.GetType() == typeof(Symbol))
870
                    {
871
                        Symbol symbol1 = connItem as Symbol;
872
                        _LMSymbol1 = GetTargetSymbol(symbol1, line);
873
                        if (_LMSymbol1 != null)
874
                        {
875
                            double x = line.SPPID.START_X;
876
                            double y = line.SPPID.START_Y;
877
                            Connector connector = SPPIDUtil.FindSymbolConnectorByUID(document, line.UID, symbol1);
878
                            if (connector != null)
879
                            {
880
                                GetTargetSymbolConnectorPoint(connector, symbol1, ref x, ref y);
881
                                line.SPPID.START_X = x;
882
                                line.SPPID.START_Y = y;
883
                            }
884

    
885
                            linePointInfo.Add(new Tuple<string, Line, object, double, double>("SYMBOL", line, _LMSymbol1, x, y));
886
                        }
887
                        else
888
                            linePointInfo.Add(new Tuple<string, Line, object, double, double>(null, line, null, line.SPPID.START_X, line.SPPID.START_Y));
889
                    }
890
                    else if (connItem != null && connItem.GetType() == typeof(Line) && !lines.Contains(connItem))
891
                    {
892
                        connectorVertices1 = GetPipeRunVertices(((Line)connItem).SPPID.ModelItemId);
893
                        targetConnector1 = FindTargetLMConnector(connectorVertices1, line.SPPID.START_X, line.SPPID.START_Y, line.SPPID.END_X, line.SPPID.END_Y);
894

    
895
                        if (targetConnector1 != null)
896
                            linePointInfo.Add(new Tuple<string, Line, object, double, double>("LINE", line, targetConnector1, line.SPPID.START_X, line.SPPID.START_Y));
897
                        else
898
                        {
899
                            startBranchLine = connItem as Line;
900
                            linePointInfo.Add(new Tuple<string, Line, object, double, double>(null, line, null, line.SPPID.START_X, line.SPPID.START_Y));
901
                        }
902
                    }
903
                    else
904
                        linePointInfo.Add(new Tuple<string, Line, object, double, double>(null, line, null, line.SPPID.START_X, line.SPPID.START_Y));
905
                        
906
                }
907
                if (i + 1 == lines.Count)
908
                {
909
                    // 끝점에 연결된 Symbol 찾기
910
                    object connItem = SPPIDUtil.FindObjectByUID(document, line.CONNECTORS[1].CONNECTEDITEM);
911

    
912
                    if (i != 0)
913
                        linePointInfo.Add(new Tuple<string, Line, object, double, double>(null, line, null, line.SPPID.START_X, line.SPPID.START_Y));
914

    
915
                    if (connItem != null && connItem.GetType() == typeof(Symbol))
916
                    {
917
                        Symbol symbol2 = connItem as Symbol;
918
                        _LMSymbol2 = GetTargetSymbol(connItem as Symbol, line);
919
                        if (_LMSymbol2 != null)
920
                        {
921
                            double x = line.SPPID.END_X;
922
                            double y = line.SPPID.END_Y;
923
                            Connector connector = SPPIDUtil.FindSymbolConnectorByUID(document, line.UID, symbol2);
924
                            if (connector != null)
925
                            {
926
                                GetTargetSymbolConnectorPoint(connector, symbol2, ref x, ref y);
927
                                line.SPPID.END_X = x;
928
                                line.SPPID.END_Y = y;
929
                            }
930

    
931
                            linePointInfo.Add(new Tuple<string, Line, object, double, double>("SYMBOL", line, _LMSymbol2, x, y));
932
                        }
933
                        else
934
                            linePointInfo.Add(new Tuple<string, Line, object, double, double>(null, line, null, line.SPPID.END_X, line.SPPID.END_Y));
935
                    }
936
                    else if (connItem != null && connItem.GetType() == typeof(Line) && !lines.Contains(connItem))
937
                    {
938
                        connectorVertices2 = GetPipeRunVertices(((Line)connItem).SPPID.ModelItemId);
939
                        targetConnector2 = FindTargetLMConnector(connectorVertices2, line.SPPID.END_X, line.SPPID.END_Y, line.SPPID.START_X, line.SPPID.START_Y);
940

    
941
                        if (targetConnector2 != null)
942
                            linePointInfo.Add(new Tuple<string, Line, object, double, double>("LINE", line, targetConnector2, line.SPPID.END_X, line.SPPID.END_Y));
943
                        else
944
                        {
945
                            endBranchLine = connItem as Line;
946
                            linePointInfo.Add(new Tuple<string, Line, object, double, double>(null, line, null, line.SPPID.END_X, line.SPPID.END_Y));
947
                        }
948
                    }
949
                    else
950
                        linePointInfo.Add(new Tuple<string, Line, object, double, double>(null, line, null, line.SPPID.END_X, line.SPPID.END_Y));
951
                }
952
            }
953

    
954
            double prevX = double.NaN;
955
            double prevY = double.NaN;
956
            SlopeType prevSlopeType = SlopeType.None;
957
            for (int i = 0; i < linePointInfo.Count; i++)
958
            {
959
                Tuple<string, Line, object, double, double> item = linePointInfo[i];
960
                Line line = item.Item2;
961
                double x = item.Item4;
962
                double y = item.Item5;
963
                SlopeType slopeType = SPPIDUtil.CalcSlope(line.SPPID.START_X, line.SPPID.START_Y, line.SPPID.END_X, line.SPPID.END_Y);
964
                // Symbol일 경우 바로 Input Point
965
                if (item.Item1 == "SYMBOL")
966
                {
967
                    placeRunInputs.AddSymbolTarget(item.Item3 as LMSymbol, x, y);
968
                    prevX = x;
969
                    prevY = y;
970
                    prevSlopeType = slopeType;
971
                    continue;
972
                }
973

    
974
                SPPIDUtil.ConvertGridPoint(ref x, ref y);
975
                // i == 0은 그대로 사용
976
                if (i != 0)
977
                {
978
                    Tuple<string, Line, object, double, double> prevItem = linePointInfo[i - 1];
979
                    // y 좌표가 같아야함
980
                    if (prevSlopeType == SlopeType.HORIZONTAL)
981
                        y = prevY;
982
                    else if (prevSlopeType == SlopeType.VERTICAL)
983
                        x = prevX;
984

    
985
                    if (i + 1 == linePointInfo.Count - 1 && linePointInfo[i + 1].Item1 == "SYMBOL")
986
                    {
987
                        Line nextLine = linePointInfo[i + 1].Item2;
988
                        SlopeType nextSlopeType = SPPIDUtil.CalcSlope(nextLine.SPPID.START_X, nextLine.SPPID.START_Y, nextLine.SPPID.END_X, nextLine.SPPID.END_Y);
989
                        if (prevSlopeType == SlopeType.HORIZONTAL)
990
                            y = linePointInfo[i + 1].Item5;
991
                        else if (prevSlopeType == SlopeType.VERTICAL)
992
                            x = linePointInfo[i + 1].Item4;
993
                    }
994
                }
995

    
996
                if (item.Item1 == "LINE")
997
                    placeRunInputs.AddConnectorTarget(item.Item3 as LMConnector, x, y);
998
                else
999
                    placeRunInputs.AddPoint(x, y);
1000

    
1001
                prevX = x;
1002
                prevY = y;
1003
                prevSlopeType = slopeType;
1004
            }
1005

    
1006
            LMConnector _lMConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
1007

    
1008
            if (_lMConnector != null)
1009
            {
1010
                foreach (var line in lines)
1011
                    line.SPPID.ModelItemId = _lMConnector.ModelItemID;
1012
                _lMConnector.Commit();
1013
                if (startBranchLine != null || endBranchLine != null)
1014
                {
1015
                    BranchLines.Add(new Tuple<string, Line, Line>(_lMConnector.ModelItemID, startBranchLine, endBranchLine));
1016
                }
1017
            }
1018

    
1019

    
1020
            if (_LMSymbol1 != null)
1021
                ReleaseCOMObjects(_LMSymbol1);
1022
            if (_LMSymbol2 != null)
1023
                ReleaseCOMObjects(_LMSymbol2);
1024
            if (targetConnector1 != null)
1025
                ReleaseCOMObjects(targetConnector1);
1026
            if (targetConnector2 != null)
1027
                ReleaseCOMObjects(targetConnector2);
1028
            foreach (var item in connectorVertices1)
1029
                ReleaseCOMObjects(item.Key);
1030
            foreach (var item in connectorVertices2)
1031
                ReleaseCOMObjects(item.Key);
1032

    
1033
            ReleaseCOMObjects(_lMConnector);
1034
            ReleaseCOMObjects(placeRunInputs);
1035
            ReleaseCOMObjects(_LMAItem);
1036
        }
1037

    
1038
        /// <summary>
1039
        /// Symbol이 모델링된 SPPPID Symbol Object를 반환 - 연결된 Symbol이 ChildSymbol일 수도 있기때문에 메서드 개발
1040
        /// </summary>
1041
        /// <param name="symbol"></param>
1042
        /// <param name="line"></param>
1043
        /// <returns></returns>
1044
        private LMSymbol GetTargetSymbol(Symbol symbol, Line line)
1045
        {
1046
            LMSymbol _LMSymbol = null;
1047
            foreach (var connector in symbol.CONNECTORS)
1048
            {
1049
                if (connector.CONNECTEDITEM == line.UID)
1050
                {
1051
                    if (connector.Index == 0)
1052
                        _LMSymbol = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
1053
                    else
1054
                    {
1055
                        ChildSymbol child = null;
1056
                        foreach (var childSymbol in symbol.ChildSymbols)
1057
                        {
1058
                            if (childSymbol.Connectors.Contains(connector))
1059
                                child = childSymbol;
1060
                            else
1061
                                child = GetChildSymbolByConnector(childSymbol, connector);
1062

    
1063
                            if (child != null)
1064
                                break;
1065
                        }
1066

    
1067
                        if (child != null)
1068
                            _LMSymbol = dataSource.GetSymbol(child.SPPID.RepresentationId);
1069
                    }
1070

    
1071
                    break;  
1072
                }
1073
            }
1074

    
1075
            return _LMSymbol;
1076
        }
1077

    
1078
        /// <summary>
1079
        /// Connector를 가지고 있는 ChildSymbol Object 반환
1080
        /// </summary>
1081
        /// <param name="item"></param>
1082
        /// <param name="connector"></param>
1083
        /// <returns></returns>
1084
        private ChildSymbol GetChildSymbolByConnector(ChildSymbol item, Connector connector)
1085
        {
1086
            foreach (var childSymbol in item.ChildSymbols)
1087
            {
1088
                if (childSymbol.Connectors.Contains(connector))
1089
                    return childSymbol;
1090
                else
1091
                    return GetChildSymbolByConnector(childSymbol, connector);
1092
            }
1093

    
1094
            return null;
1095
        }
1096

    
1097
        /// <summary>
1098
        /// Branch 라인을 다시 모델링하는 진입 메서드
1099
        /// </summary>
1100
        /// <param name="branch"></param>
1101
        private void BranchLineModeling(Tuple<string, Line, Line> branch)
1102
        {
1103
            List<Line> lines = SPPIDUtil.FindLinesByModelId(document, branch.Item1);
1104
            Dictionary<LMConnector, List<double[]>> connectorVertices = GetPipeRunVertices(branch.Item1);
1105

    
1106
            LMConnector _StartConnector = null;
1107
            LMConnector _EndConnector = null;
1108
            double lengthStart = double.MaxValue;
1109
            double lengthEnd = double.MaxValue;
1110
            List<double[]> startPoints = new List<double[]>();
1111
            List<double[]> endPoints = new List<double[]>();
1112

    
1113
            foreach (var item in connectorVertices)
1114
            {
1115
                foreach (var point in item.Value)
1116
                {
1117
                    // Start Point가 Branch
1118
                    if (branch.Item2 != null)
1119
                    {
1120
                        Line targetLine = branch.Item2;
1121
                        double distance = SPPIDUtil.CalcLineToPointDistance(targetLine.SPPID.START_X, targetLine.SPPID.START_Y, targetLine.SPPID.END_X, targetLine.SPPID.END_Y, point[0], point[1]);
1122
                        if (lengthStart > distance)
1123
                        {
1124
                            _StartConnector = item.Key;
1125
                            lengthStart = distance;
1126
                            startPoints = item.Value;
1127
                        }
1128
                    }
1129
                    // End Point가 Branch
1130
                    if (branch.Item3 != null)
1131
                    {
1132
                        Line targetLine = branch.Item3;
1133
                        double distance = SPPIDUtil.CalcLineToPointDistance(targetLine.SPPID.START_X, targetLine.SPPID.START_Y, targetLine.SPPID.END_X, targetLine.SPPID.END_Y, point[0], point[1]);
1134
                        if (lengthEnd > distance)
1135
                        {
1136
                            _EndConnector = item.Key;
1137
                            lengthEnd = distance;
1138
                            endPoints = item.Value;
1139
                        }
1140
                    }
1141
                }
1142
            }
1143
            #region Branch가 양쪽 전부일 때
1144
            if (_StartConnector != null && _StartConnector == _EndConnector)
1145
            {
1146
                _placement.PIDRemovePlacement(_StartConnector.AsLMRepresentation());
1147

    
1148
                _LMAItem _LMAItem = _placement.PIDCreateItem(lines[0].SPPID.MAPPINGNAME);
1149
                PlaceRunInputs placeRunInputs = new PlaceRunInputs();
1150

    
1151
                Dictionary<LMConnector, List<double[]>> startConnectorVertices = GetPipeRunVertices(branch.Item2.SPPID.ModelItemId);
1152
                LMConnector _StartTargetConnector = FindTargetLMConnector(startConnectorVertices, startPoints[0][0], startPoints[0][1], startPoints[1][0], startPoints[1][1]);
1153
                Dictionary<LMConnector, List<double[]>> endConnectorVertices = GetPipeRunVertices(branch.Item3.SPPID.ModelItemId);
1154
                LMConnector _EndTargetConnector = FindTargetLMConnector(endConnectorVertices,
1155
                   startPoints[startPoints.Count - 1][0],
1156
                   startPoints[startPoints.Count - 1][1],
1157
                   startPoints[startPoints.Count - 2][0],
1158
                   startPoints[startPoints.Count - 2][1]);
1159

    
1160
                for (int i = 0; i < startPoints.Count; i++)
1161
                {
1162
                    double[] point = startPoints[i];
1163
                    if (i == 0)
1164
                        placeRunInputs.AddConnectorTarget(_StartTargetConnector, point[0], point[1]);
1165
                    else if (i == startPoints.Count - 1)
1166
                        placeRunInputs.AddConnectorTarget(_EndTargetConnector, point[0], point[1]);
1167
                    else
1168
                        placeRunInputs.AddPoint(point[0], point[1]);
1169
                }
1170

    
1171
                LMConnector _LMConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
1172
                if (_LMConnector != null)
1173
                {
1174
                    _LMConnector.Commit();
1175
                    foreach (var item in lines)
1176
                        item.SPPID.ModelItemId = _LMConnector.ModelItemID;
1177
                }
1178

    
1179
                foreach (var item in startConnectorVertices)
1180
                    ReleaseCOMObjects(item.Key);
1181
                foreach (var item in endConnectorVertices)
1182
                    ReleaseCOMObjects(item.Key);
1183
                ReleaseCOMObjects(placeRunInputs);
1184
                ReleaseCOMObjects(_LMAItem);
1185
                ReleaseCOMObjects(_LMConnector);
1186
            }
1187
            #endregion
1188
            #region 양쪽이 다른 Branch 
1189
            else
1190
            {
1191
                // Branch 시작 Connector
1192
                if (_StartConnector != null)
1193
                    BranchLineModelingByConnector(branch, _StartConnector, startPoints, true);
1194

    
1195
                // Branch 끝 Connector
1196
                if (_EndConnector != null)
1197
                    BranchLineModelingByConnector(branch, _EndConnector, endPoints, false);
1198
            }
1199
            #endregion
1200

    
1201
            if (_StartConnector != null)
1202
                ReleaseCOMObjects(_StartConnector);
1203
            if (_EndConnector != null)
1204
                ReleaseCOMObjects(_EndConnector);
1205
            foreach (var item in connectorVertices)
1206
                ReleaseCOMObjects(item.Key);
1207
        }
1208

    
1209
        /// <summary>
1210
        /// Branch 라인을 다시 실제로 모델링하는 메서드
1211
        /// </summary>
1212
        /// <param name="branch"></param>
1213
        /// <param name="_Connector"></param>
1214
        /// <param name="points"></param>
1215
        /// <param name="IsStart"></param>
1216
        private void BranchLineModelingByConnector(Tuple<string, Line, Line> branch, LMConnector _Connector, List<double[]> points, bool IsStart)
1217
        {
1218
            List<Line> lines = SPPIDUtil.FindLinesByModelId(document, branch.Item1);
1219
            Dictionary<LMConnector, List<double[]>> connectorVertices = GetPipeRunVertices(branch.Item1);
1220
            LMConnector _SameRunTargetConnector = null;
1221
            LMSymbol _SameRunTargetSymbol = null;
1222
            Dictionary<LMConnector, List<double[]>> branchConnectorVertices = null;
1223
            LMConnector _BranchTargetConnector = null;
1224
            PlaceRunInputs placeRunInputs = new PlaceRunInputs();
1225

    
1226
            // 같은 Line Run의 Connector 찾기
1227
            foreach (var item in connectorVertices)
1228
            {
1229
                if (item.Key == _Connector)
1230
                    continue;
1231

    
1232
                if (IsStart &&
1233
                    !DBNull.Value.Equals(item.Key.ConnectItem1SymbolID) &&
1234
                    !DBNull.Value.Equals(_Connector.ConnectItem2SymbolID)&& 
1235
                    item.Key.ConnectItem1SymbolID == _Connector.ConnectItem2SymbolID)
1236
                {
1237
                    _SameRunTargetConnector = item.Key;
1238
                    break;
1239
                }
1240
                else if (!IsStart &&
1241
                    !DBNull.Value.Equals(item.Key.ConnectItem2SymbolID) &&
1242
                    !DBNull.Value.Equals(_Connector.ConnectItem1SymbolID) && 
1243
                    item.Key.ConnectItem2SymbolID == _Connector.ConnectItem1SymbolID)
1244
                {
1245
                    _SameRunTargetConnector = item.Key;
1246
                    break;
1247
                }
1248
            }
1249

    
1250
            // Branch 반대편이 Symbol
1251
            if (_SameRunTargetConnector == null)
1252
            {
1253
                foreach (var line in lines)
1254
                {
1255
                    foreach (var connector in line.CONNECTORS)
1256
                    {
1257
                        Symbol symbol = SPPIDUtil.FindObjectByUID(document, connector.CONNECTEDITEM) as Symbol;
1258
                        if (symbol != null)
1259
                        {
1260
                            _SameRunTargetSymbol = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
1261
                            break;
1262
                        }
1263
                    }
1264
                }
1265
            }
1266

    
1267
            // 기존 Connector 제거
1268
            _placement.PIDRemovePlacement(_Connector.AsLMRepresentation());
1269
            
1270
            // 시작 Connector일 경우 첫 Point가 TargetConnector를 찾아야함
1271
            if (IsStart)
1272
            {
1273
                branchConnectorVertices = GetPipeRunVertices(branch.Item2.SPPID.ModelItemId);
1274
                _BranchTargetConnector = FindTargetLMConnector(branchConnectorVertices, points[0][0], points[0][1], points[1][0], points[1][1]);
1275
            }
1276
            // 끝 Conenctor일 경우 마지막 Point가 TargetConnector를 찾아야함
1277
            else
1278
            {
1279
                branchConnectorVertices = GetPipeRunVertices(branch.Item3.SPPID.ModelItemId);
1280
                _BranchTargetConnector = FindTargetLMConnector(branchConnectorVertices,
1281
                    points[points.Count - 1][0],
1282
                    points[points.Count - 1][1],
1283
                    points[points.Count - 2][0],
1284
                    points[points.Count - 2][1]);
1285
            }
1286

    
1287
            for (int i = 0; i < points.Count; i++)
1288
            {
1289
                double[] point = points[i];
1290
                if (i == 0)
1291
                {
1292
                    if (IsStart)
1293
                    {
1294
                        if (_BranchTargetConnector != null)
1295
                        {
1296
                            placeRunInputs.AddConnectorTarget(_BranchTargetConnector, point[0], point[1]);
1297
                        }
1298
                        else
1299
                        {
1300
                            placeRunInputs.AddPoint(point[0], point[1]);
1301
                        }
1302
                        
1303
                    }
1304
                    else
1305
                    {
1306
                        if (_SameRunTargetConnector != null)
1307
                            placeRunInputs.AddConnectorTarget(_SameRunTargetConnector, point[0], point[1]);
1308
                        else if (_SameRunTargetSymbol != null)
1309
                            placeRunInputs.AddSymbolTarget(_SameRunTargetSymbol, point[0], point[1]);
1310
                        else
1311
                            placeRunInputs.AddPoint(point[0], point[1]);
1312
                    }
1313
                }
1314
                else if (i == points.Count - 1)
1315
                {
1316
                    if (IsStart)
1317
                    {
1318
                        if (_SameRunTargetConnector != null)
1319
                            placeRunInputs.AddConnectorTarget(_SameRunTargetConnector, point[0], point[1]);
1320
                        else if (_SameRunTargetSymbol != null)
1321
                            placeRunInputs.AddSymbolTarget(_SameRunTargetSymbol, point[0], point[1]);
1322
                        else
1323
                            placeRunInputs.AddPoint(point[0], point[1]);
1324
                    }
1325
                    else
1326
                    {
1327
                        if (_BranchTargetConnector != null)
1328
                        {
1329
                            placeRunInputs.AddConnectorTarget(_BranchTargetConnector, point[0], point[1]);
1330
                        }
1331
                        else
1332
                        {
1333
                            placeRunInputs.AddPoint(point[0], point[1]);
1334
                        }
1335
                    }
1336
                }
1337
                else
1338
                    placeRunInputs.AddPoint(point[0], point[1]);
1339
            }
1340
            _LMAItem _LMAItem = _placement.PIDCreateItem(lines[0].SPPID.MAPPINGNAME);
1341
            LMConnector _LMConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
1342

    
1343
            if (_LMConnector != null)
1344
            {
1345
                if (_SameRunTargetConnector != null)
1346
                {
1347
                    JoinPipeRun(_LMConnector.ModelItemID, _SameRunTargetConnector.ModelItemID);
1348
                }
1349
                else
1350
                {
1351
                    foreach (var item in lines)
1352
                        item.SPPID.ModelItemId = _LMConnector.ModelItemID;
1353
                }
1354

    
1355
                _LMConnector.Commit();
1356
                ReleaseCOMObjects(_LMConnector);
1357
            }
1358

    
1359
            ReleaseCOMObjects(placeRunInputs);
1360
            ReleaseCOMObjects(_LMAItem);
1361
            if (_BranchTargetConnector != null)
1362
                ReleaseCOMObjects(_BranchTargetConnector);
1363
            if (_SameRunTargetConnector != null)
1364
                ReleaseCOMObjects(_SameRunTargetConnector);
1365
            if (_SameRunTargetSymbol != null)
1366
                ReleaseCOMObjects(_SameRunTargetSymbol);
1367
            foreach (var item in connectorVertices)
1368
                ReleaseCOMObjects(item.Key);
1369
            foreach (var item in branchConnectorVertices)
1370
                ReleaseCOMObjects(item.Key);
1371
        }
1372

    
1373
        /// <summary>
1374
        /// EndBreak 모델링 메서드
1375
        /// </summary>
1376
        /// <param name="endBreak"></param>
1377
        private void EndBreakModeling(EndBreak endBreak)
1378
        {
1379
            object ownerObj = SPPIDUtil.FindObjectByUID(document, endBreak.OWNER);
1380
            LMConnector targetLMConnector = null;
1381
            if (ownerObj !=null && ownerObj.GetType() == typeof(Line))
1382
            {
1383
                Line ownerLine = ownerObj as Line;
1384
                LMLabelPersist _LmLabelPersist = null;
1385
                Dictionary<LMConnector, List<double[]>> connectorVertices = GetPipeRunVertices(ownerLine.SPPID.ModelItemId);
1386

    
1387
                targetLMConnector = FindTargetLMConnectorByPoint(connectorVertices, endBreak.SPPID.ORIGINAL_X, endBreak.SPPID.ORIGINAL_Y);
1388
                
1389
                if (targetLMConnector != null)
1390
                {
1391
                    Array array = new double[] { 0, endBreak.SPPID.ORIGINAL_X, endBreak.SPPID.ORIGINAL_Y };
1392
                    _LmLabelPersist = _placement.PIDPlaceLabel(endBreak.SPPID.MAPPINGNAME, ref array, Rotation: 0, LabeledItem: targetLMConnector.AsLMRepresentation(), IsLeaderVisible: true);
1393
                }
1394

    
1395
                if (_LmLabelPersist != null)
1396
                {
1397
                    _LmLabelPersist.Commit();
1398
                    ReleaseCOMObjects(_LmLabelPersist);
1399
                }
1400
                else
1401
                    RetryEndBreakModeling(endBreak, targetLMConnector);
1402

    
1403
                foreach (var item in connectorVertices)
1404
                    ReleaseCOMObjects(item.Key);
1405

    
1406
            }
1407
            else if (ownerObj != null && ownerObj.GetType() == typeof(Symbol))
1408
            {
1409
                Symbol ownerSymbol = ownerObj as Symbol;
1410
                LMSymbol _LMSymbol = dataSource.GetSymbol(ownerSymbol.SPPID.RepresentationId);
1411

    
1412
                targetLMConnector = null;
1413
                double distance = double.MaxValue;
1414

    
1415
                foreach (LMConnector connector in _LMSymbol.Avoid1Connectors)
1416
                {
1417
                    if (connector.get_ItemStatus() == "Active")
1418
                    {
1419
                        dynamic OID = connector.get_GraphicOID();
1420
                        DependencyObject drawingObject = radApp.ActiveDocument.ActiveSheet.DrawingObjects[OID];
1421
                        Ingr.RAD2D.LineStringGeometry2d lineStringGeometry = drawingObject.GetGeometry() as Ingr.RAD2D.LineStringGeometry2d;
1422
                        int verticesCount = lineStringGeometry.VertexCount;
1423
                        double[] vertices = null;
1424
                        lineStringGeometry.GetVertices(ref verticesCount, ref vertices);
1425
                        for (int i = 0; i < verticesCount; i++)
1426
                        {
1427
                            double x = 0;
1428
                            double y = 0;
1429
                            lineStringGeometry.GetVertex(i + 1, ref x, ref y);
1430

    
1431
                            double result = SPPIDUtil.CalcPointToPointdDistance(x, y, endBreak.SPPID.ORIGINAL_X, endBreak.SPPID.ORIGINAL_Y);
1432
                            if (result < distance)
1433
                            {
1434
                                targetLMConnector = connector;
1435
                                distance = result;
1436
                            }
1437
                        }
1438
                    }
1439
                }
1440

    
1441
                foreach (LMConnector connector in _LMSymbol.Avoid2Connectors)
1442
                {
1443
                    if (connector.get_ItemStatus() == "Active")
1444
                    {
1445
                        dynamic OID = connector.get_GraphicOID();
1446
                        DependencyObject drawingObject = radApp.ActiveDocument.ActiveSheet.DrawingObjects[OID];
1447
                        Ingr.RAD2D.LineStringGeometry2d lineStringGeometry = drawingObject.GetGeometry() as Ingr.RAD2D.LineStringGeometry2d;
1448
                        int verticesCount = lineStringGeometry.VertexCount;
1449
                        double[] vertices = null;
1450
                        lineStringGeometry.GetVertices(ref verticesCount, ref vertices);
1451
                        for (int i = 0; i < verticesCount; i++)
1452
                        {
1453
                            double x = 0;
1454
                            double y = 0;
1455
                            lineStringGeometry.GetVertex(i + 1, ref x, ref y);
1456

    
1457
                            double result = SPPIDUtil.CalcPointToPointdDistance(x, y, endBreak.SPPID.ORIGINAL_X, endBreak.SPPID.ORIGINAL_Y);
1458
                            if (result < distance)
1459
                            {
1460
                                targetLMConnector = connector;
1461
                                distance = result;
1462
                            }
1463
                        }
1464
                    }
1465
                }
1466

    
1467
                if (targetLMConnector != null)
1468
                {
1469
                    LMLabelPersist _LmLabelPersist = null;
1470
                    Array array = new double[] { 0, endBreak.SPPID.ORIGINAL_X, endBreak.SPPID.ORIGINAL_Y };
1471
                    _LmLabelPersist = _placement.PIDPlaceLabel(endBreak.SPPID.MAPPINGNAME, ref array, Rotation: 0, LabeledItem: targetLMConnector.AsLMRepresentation(), IsLeaderVisible: true);
1472
                    if (_LmLabelPersist != null)
1473
                    {
1474
                        _LmLabelPersist.Commit();
1475
                        ReleaseCOMObjects(_LmLabelPersist);
1476
                    }
1477
                    else
1478
                        RetryEndBreakModeling(endBreak, targetLMConnector);
1479
                }
1480
                
1481
                ReleaseCOMObjects(_LMSymbol);
1482
            }
1483

    
1484
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetProgress, ++CurrentCount);
1485
        }
1486

    
1487
        /// <summary>
1488
        /// EndBreak 모델링이 실패시 다시 시도하는 메서드
1489
        /// </summary>
1490
        /// <param name="endBreak"></param>
1491
        /// <param name="targetLMConnector"></param>
1492
        private void RetryEndBreakModeling(EndBreak endBreak, LMConnector targetLMConnector)
1493
        {
1494
            bool isZeroLength = Convert.ToBoolean(targetLMConnector.get_IsZeroLength());
1495
            Array array = null;
1496
            LMLabelPersist _LMLabelPersist = null;
1497
            LMConnector _LMConnector = null;
1498
            dynamic OID = targetLMConnector.get_GraphicOID();
1499
            DependencyObject drawingObject = radApp.ActiveDocument.ActiveSheet.DrawingObjects[OID];
1500
            Ingr.RAD2D.LineStringGeometry2d lineStringGeometry = drawingObject.GetGeometry() as Ingr.RAD2D.LineStringGeometry2d;
1501
            int verticesCount = lineStringGeometry.VertexCount;
1502
            PlaceRunInputs placeRunInputs = new PlaceRunInputs();
1503
            _LMAItem _LMAItem = _placement.PIDCreateItem(@"\Piping\Routing\Process Lines\Primary Piping.sym");
1504

    
1505
            if (isZeroLength)
1506
            {
1507
                double[] vertices = null;
1508
                lineStringGeometry.GetVertices(ref verticesCount, ref vertices);
1509
                double x = 0;
1510
                double y = 0;
1511
                lineStringGeometry.GetVertex(1, ref x, ref y);
1512

    
1513
                placeRunInputs.AddSymbolTarget(targetLMConnector.ConnectItem1SymbolObject, x, y);
1514
                placeRunInputs.AddSymbolTarget(targetLMConnector.ConnectItem2SymbolObject, x, y);
1515

    
1516
                _placement.PIDRemovePlacement(targetLMConnector.AsLMRepresentation());
1517
                _LMConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
1518

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

    
1522
                AutoJoinPipeRun(_LMConnector.ModelItemID);
1523
            }
1524
            else
1525
            {
1526
                List<double[]> vertices = new List<double[]>();
1527
                for (int i = 1; i <= verticesCount; i++)
1528
                {
1529
                    double x = 0;
1530
                    double y = 0;
1531
                    lineStringGeometry.GetVertex(i, ref x, ref y);
1532
                    vertices.Add(new double[] { x, y });
1533
                }
1534

    
1535
                for (int i = 0; i < vertices.Count; i++)
1536
                {
1537
                    double[] points = vertices[i];
1538
                    if (i == 0)
1539
                    {
1540
                        if (targetLMConnector.ConnectItem1SymbolObject != null)
1541
                            placeRunInputs.AddSymbolTarget(targetLMConnector.ConnectItem1SymbolObject, points[0], points[1]);
1542
                        else
1543
                            placeRunInputs.AddPoint(points[0], points[1]);
1544
                    }
1545
                    else if (i == vertices.Count - 1)
1546
                    {
1547
                        if (targetLMConnector.ConnectItem2SymbolObject != null)
1548
                            placeRunInputs.AddSymbolTarget(targetLMConnector.ConnectItem2SymbolObject, points[0], points[1]);
1549
                        else
1550
                            placeRunInputs.AddPoint(points[0], points[1]);
1551
                    }
1552
                    else
1553
                        placeRunInputs.AddPoint(points[0], points[1]);
1554
                }
1555

    
1556
                List<Line> lines = SPPIDUtil.FindLinesByModelId(document, targetLMConnector.ModelItemID);
1557
                
1558
                _placement.PIDRemovePlacement(targetLMConnector.AsLMRepresentation());
1559
                _LMConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
1560

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

    
1564
                AutoJoinPipeRun(_LMConnector.ModelItemID);
1565
                foreach (var line in lines)
1566
                    line.SPPID.ModelItemId = _LMConnector.ModelItemID;
1567
            }
1568

    
1569

    
1570
            if (_LMLabelPersist != null)
1571
            {
1572
                _LMLabelPersist.Commit();
1573
                ReleaseCOMObjects(_LMLabelPersist);
1574
            }
1575
            else
1576
            {
1577
                
1578
            }
1579

    
1580
            ReleaseCOMObjects(_LMConnector);
1581
            ReleaseCOMObjects(placeRunInputs);
1582
            ReleaseCOMObjects(_LMAItem);
1583
        }
1584

    
1585
        /// <summary>
1586
        /// FromModelItem을 ToModelItem으로 PipeRunJoin하는 메서드
1587
        /// </summary>
1588
        /// <param name="fromModelItemId"></param>
1589
        /// <param name="toModelItemId"></param>
1590
        private void JoinPipeRun(string fromModelItemId, string toModelItemId)
1591
        {
1592
            LMModelItem modelItem1 = dataSource.GetModelItem(toModelItemId);
1593
            _LMAItem item1 = modelItem1.AsLMAItem();
1594
            LMModelItem modelItem2 = dataSource.GetModelItem(fromModelItemId);
1595
            _LMAItem item2 = modelItem2.AsLMAItem();
1596
            
1597
            // item2가 item1으로 조인
1598
            try
1599
            {
1600
                _placement.PIDJoinRuns(ref item1, ref item2);
1601
                item1.Commit();
1602
                item2.Commit();
1603

    
1604
                List<Line> lines = SPPIDUtil.FindLinesByModelId(document, fromModelItemId);
1605
                foreach (var line in lines)
1606
                    line.SPPID.ModelItemId = toModelItemId;
1607
            }
1608
            catch (Exception ex)
1609
            {
1610
                System.Windows.Forms.MessageBox.Show(ex.Message + "\r\n" + ex.StackTrace);
1611
            }
1612
            finally
1613
            {
1614
                ReleaseCOMObjects(modelItem1);
1615
                ReleaseCOMObjects(item1);
1616
                ReleaseCOMObjects(modelItem2);
1617
                ReleaseCOMObjects(item2);
1618
            }
1619
        }
1620

    
1621
        /// <summary>
1622
        /// PipeRun을 자동으로 Join하는 메서드
1623
        /// </summary>
1624
        /// <param name="modelItemId"></param>
1625
        private void AutoJoinPipeRun(string modelItemId)
1626
        {
1627
            LMModelItem modelItem = dataSource.GetModelItem(modelItemId);
1628
            _LMAItem item = modelItem.AsLMAItem();
1629
            try
1630
            {
1631
                string modelitemID = item.Id;
1632
                _placement.PIDAutoJoin(item, AutoJoinEndConstants.autoJoin_Both, ref item);
1633
                string afterModelItemID = item.Id;
1634
                
1635
                if (modelitemID != afterModelItemID)
1636
                {
1637
                    List<Line> lines = SPPIDUtil.FindLinesByModelId(document, modelitemID);
1638
                    foreach (var line in lines)
1639
                        line.SPPID.ModelItemId = afterModelItemID;
1640
                }
1641
                item.Commit();
1642
            }
1643
            catch (Exception ex)
1644
            {
1645
                System.Windows.Forms.MessageBox.Show(ex.Message + "\r\n" + ex.StackTrace);
1646
            }
1647
            finally
1648
            {
1649
                ReleaseCOMObjects(modelItem);
1650
                ReleaseCOMObjects(item);
1651
            }
1652
        }
1653

    
1654
        /// <summary>
1655
        /// LineRun에 있는 Line들을 Join하는 진입 메서드
1656
        /// </summary>
1657
        /// <param name="run"></param>
1658
        private void JoinRunLine(LineRun run)
1659
        {
1660
            string modelItemId = string.Empty;
1661
            foreach (var item in run.RUNITEMS)
1662
            {
1663
                if (item.GetType() == typeof(Line))
1664
                {
1665
                    Line line = item as Line;
1666
                    AutoJoinPipeRun(line.SPPID.ModelItemId);
1667
                    modelItemId = line.SPPID.ModelItemId;
1668

    
1669
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetProgress, ++CurrentCount);
1670
                }
1671
            }
1672
        }
1673

    
1674
        /// <summary>
1675
        /// PipeRun의 좌표를 가져오는 메서드
1676
        /// </summary>
1677
        /// <param name="modelId"></param>
1678
        /// <returns></returns>
1679
        private Dictionary<LMConnector, List<double[]>> GetPipeRunVertices(string modelId)
1680
        {
1681
            Dictionary<LMConnector, List<double[]>> connectorVertices = new Dictionary<LMConnector, List<double[]>>();
1682
            LMModelItem modelItem = dataSource.GetModelItem(modelId);
1683

    
1684
            if (modelItem != null)
1685
            {
1686
                foreach (LMRepresentation rep in modelItem.Representations)
1687
                {
1688
                    if (rep.Attributes["RepresentationType"].get_Value() == "Connector" && rep.Attributes["ItemStatus"].get_Value() == "Active")
1689
                    {
1690
                        LMConnector _LMConnector = dataSource.GetConnector(rep.Id);
1691
                        connectorVertices.Add(_LMConnector, new List<double[]>());
1692
                        dynamic OID = rep.get_GraphicOID();
1693
                        DependencyObject drawingObject = radApp.ActiveDocument.ActiveSheet.DrawingObjects[OID];
1694
                        Ingr.RAD2D.LineStringGeometry2d lineStringGeometry = drawingObject.GetGeometry() as Ingr.RAD2D.LineStringGeometry2d;
1695
                        int verticesCount = lineStringGeometry.VertexCount;
1696
                        double[] vertices = null;
1697
                        lineStringGeometry.GetVertices(ref verticesCount, ref vertices);
1698
                        for (int i = 0; i < verticesCount; i++)
1699
                        {
1700
                            double x = 0;
1701
                            double y = 0;
1702
                            lineStringGeometry.GetVertex(i + 1, ref x, ref y);
1703
                            connectorVertices[_LMConnector].Add(new double[] { Math.Round(x, 10), Math.Round(y, 10) });
1704
                        }
1705
                    }
1706
                }
1707

    
1708
                ReleaseCOMObjects(modelItem);
1709
            }
1710

    
1711
            return connectorVertices;
1712
        }
1713

    
1714
        /// <summary>
1715
        /// 좌표로 PipeRun의 Connector중에 어느 Connector에 가까운지/붙을지 가져오는 메서드 - 두점으로 라인의 교차점을 기준으로 구함
1716
        /// </summary>
1717
        /// <param name="connectorVertices"></param>
1718
        /// <param name="connX"></param>
1719
        /// <param name="connY"></param>
1720
        /// <param name="x2"></param>
1721
        /// <param name="y2"></param>
1722
        /// <returns></returns>
1723
        private LMConnector FindTargetLMConnector(Dictionary<LMConnector, List<double[]>> connectorVertices, double connX, double connY, double x2, double y2)
1724
        {
1725
            double length = double.MaxValue;
1726
            LMConnector targetConnector = null;
1727
            foreach (var item in connectorVertices)
1728
            {
1729
                List<double[]> points = item.Value;
1730
                for (int i = 0; i < points.Count - 1; i++)
1731
                {
1732
                    double[] point1 = points[i];
1733
                    double[] point2 = points[i + 1];
1734

    
1735
                    double maxLineX = Math.Max(point1[0], point2[0]);
1736
                    double minLineX = Math.Min(point1[0], point2[0]);
1737
                    double maxLineY = Math.Max(point1[1], point2[1]);
1738
                    double minLineY = Math.Min(point1[1], point2[1]);
1739

    
1740
                    SlopeType slope = SPPIDUtil.CalcSlope(minLineX, minLineY, maxLineX, maxLineY);
1741

    
1742
                    // 두직선의 교차점
1743
                    double[] crossingPoint = SPPIDUtil.CalcLineCrossingPoint(connX, connY, x2, y2, point1[0], point1[1], point2[0], point2[1]);
1744
                    if (crossingPoint != null)
1745
                    {
1746
                        double distance = SPPIDUtil.CalcPointToPointdDistance(connX, connY, crossingPoint[0], crossingPoint[1]);
1747
                        if (length >= distance)
1748
                        {
1749
                            if (slope == SlopeType.Slope &&
1750
                                minLineX <= crossingPoint[0] && maxLineX >= crossingPoint[0] &&
1751
                                minLineY <= crossingPoint[1] && maxLineY >= crossingPoint[1])
1752
                            {
1753
                                targetConnector = item.Key;
1754
                                length = distance;
1755
                            }
1756
                            else if (slope == SlopeType.HORIZONTAL &&
1757
                                minLineX <= crossingPoint[0] && maxLineX >= crossingPoint[0])
1758
                            {
1759
                                targetConnector = item.Key;
1760
                                length = distance;
1761
                            }
1762
                            else if (slope == SlopeType.VERTICAL &&
1763
                               minLineY <= crossingPoint[1] && maxLineY >= crossingPoint[1])
1764
                            {
1765
                                targetConnector = item.Key;
1766
                                length = distance;
1767
                            }
1768
                        }
1769
                    }
1770
                }
1771

    
1772

    
1773
            }
1774

    
1775
            if (targetConnector == null)
1776
            {
1777
                foreach (var item in connectorVertices)
1778
                {
1779
                    List<double[]> points = item.Value;
1780
                    foreach (var point in points)
1781
                    {
1782
                        double distance = SPPIDUtil.CalcPointToPointdDistance(connX, connY, point[0], point[1]);
1783
                        if (length >= distance)
1784
                        {
1785
                            targetConnector = item.Key;
1786
                            length = distance;
1787
                        }
1788
                    }
1789
                }
1790

    
1791
            }
1792

    
1793
            return targetConnector;
1794
        }
1795

    
1796
        /// <summary>
1797
        /// 좌표로 PipeRun의 Connector중에 어느 Connector에 가까운지/붙을지 가져오는 메서드 - 한점으로 제일 가까운 기준으로 구함(단순)
1798
        /// </summary>
1799
        /// <param name="connectorVertices"></param>
1800
        /// <param name="connX"></param>
1801
        /// <param name="connY"></param>
1802
        /// <returns></returns>
1803
        private LMConnector FindTargetLMConnectorByPoint(Dictionary<LMConnector, List<double[]>> connectorVertices, double connX, double connY)
1804
        {
1805
            double length = double.MaxValue;
1806
            LMConnector targetConnector = null;
1807
            foreach (var item in connectorVertices)
1808
            {
1809
                List<double[]> points = item.Value;
1810

    
1811
                foreach (double[] point in points)
1812
                {
1813
                    double distance = SPPIDUtil.CalcPointToPointdDistance(point[0], point[1], connX, connY);
1814
                    if (length >= distance)
1815
                    {
1816
                        targetConnector = item.Key;
1817
                        length = distance;
1818
                    }
1819
                }
1820
            }
1821

    
1822
            return targetConnector;
1823
        }
1824

    
1825
        /// <summary>
1826
        /// 좌표로 PipeRun의 Connector중에 어느 Connector에 가까운지/붙을지 가져오는 메서드 - 조건에 안맞아서 못찾을시 제일 가까운 점으로 가져오는 방식
1827
        /// </summary>
1828
        /// <param name="connectorVertices"></param>
1829
        /// <param name="connX"></param>
1830
        /// <param name="connY"></param>
1831
        /// <returns></returns>
1832
        private LMConnector FindTargetLMConnectorForLabel(Dictionary<LMConnector, List<double[]>> connectorVertices, double connX, double connY)
1833
        {
1834
            double length = double.MaxValue;
1835
            LMConnector targetConnector = null;
1836
            foreach (var item in connectorVertices)
1837
            {
1838
                List<double[]> points = item.Value;
1839
                for (int i = 0; i < points.Count - 1; i++)
1840
                {
1841
                    double[] point1 = points[i];
1842
                    double[] point2 = points[i + 1];
1843
                    double x1 = Math.Min(point1[0], point2[0]);
1844
                    double y1 = Math.Min(point1[1], point2[1]);
1845
                    double x2 = Math.Max(point1[0], point2[0]);
1846
                    double y2 = Math.Max(point1[1], point2[1]);
1847

    
1848
                    if ((x1 <= connX && x2 >= connX) ||
1849
                        (y1 <= connY && y2 >= connY))
1850
                    {
1851
                        double distance = SPPIDUtil.CalcPointToPointdDistance(point1[0], point1[1], connX, connY);
1852
                        if (length >= distance)
1853
                        {
1854
                            targetConnector = item.Key;
1855
                            length = distance;
1856
                        }
1857

    
1858
                        distance = SPPIDUtil.CalcPointToPointdDistance(point2[0], point2[1], connX, connY);
1859
                        if (length >= distance)
1860
                        {
1861
                            targetConnector = item.Key;
1862
                            length = distance;
1863
                        }
1864
                    }
1865
                }
1866
            }
1867

    
1868
            // 못찾았을때.
1869
            length = double.MaxValue;
1870
            if (targetConnector == null)
1871
            {
1872
                foreach (var item in connectorVertices)
1873
                {
1874
                    List<double[]> points = item.Value;
1875

    
1876
                    foreach (double[] point in points)
1877
                    {
1878
                        double distance = SPPIDUtil.CalcPointToPointdDistance(point[0], point[1], connX, connY);
1879
                        if (length >= distance)
1880
                        {
1881
                            targetConnector = item.Key;
1882
                            length = distance;
1883
                        }
1884
                    }
1885
                }
1886
            }
1887

    
1888
            return targetConnector;
1889
        }
1890

    
1891
        /// <summary>
1892
        /// Line Number Symbol을 실제로 Modeling하는 메서드
1893
        /// </summary>
1894
        /// <param name="lineNumber"></param>
1895
        private void LineNumberModeling(LineNumber lineNumber)
1896
        {
1897
            Line line = SPPIDUtil.FindObjectByUID(document, lineNumber.CONNLINE) as Line;
1898
            Dictionary<LMConnector, List<double[]>> connectorVertices = GetPipeRunVertices(line.SPPID.ModelItemId);
1899
            LMConnector connectedLMConnector = FindTargetLMConnectorForLabel(connectorVertices, lineNumber.SPPID.ORIGINAL_X, lineNumber.SPPID.ORIGINAL_Y);
1900
            if (connectedLMConnector != null)
1901
            {
1902
                double x = 0;
1903
                double y = 0;
1904
                CalcLabelLocation(ref x, ref y, lineNumber.SPPID.ORIGINAL_X, lineNumber.SPPID.ORIGINAL_Y, lineNumber.SPPIDLabelLocation, _ETCSetting.LineNumberLocation);
1905

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

    
1909
                foreach (var item in connectorVertices)
1910
                    ReleaseCOMObjects(item.Key);
1911
                if (_LmLabelPresist != null)
1912
                {
1913
                    _LmLabelPresist.Commit();
1914
                    lineNumber.SPPID.RepresentationId = _LmLabelPresist.AsLMRepresentation().Id;
1915
                    ReleaseCOMObjects(_LmLabelPresist);
1916
                }
1917
                else
1918
                {
1919

    
1920
                }
1921
            }
1922

    
1923
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetProgress, ++CurrentCount);
1924
        }
1925

    
1926
        /// <summary>
1927
        /// Flow Mark Modeling
1928
        /// </summary>
1929
        /// <param name="line"></param>
1930
        private void FlowMarkModeling(Line line)
1931
        {
1932
            if (line.FLOWMARK && !string.IsNullOrEmpty(line.SPPID.ModelItemId))
1933
            {
1934
                SlopeType targetSlopeType = SPPIDUtil.CalcSlope(line.SPPID.START_X, line.SPPID.START_Y, line.SPPID.END_X, line.SPPID.END_Y);
1935
                string mappingPath = _ETCSetting.FlowMarkSymbolPath;
1936
                double percent = line.FLOWMARK_PERCENT;
1937
                double tempX = 0;
1938
                double tempY = 0;
1939

    
1940
                double gapX;
1941
                double gapY;
1942
                // ID2 기준의 Gap을 구함
1943
                if (percent == 0)
1944
                {
1945
                    gapX = 0;
1946
                    gapY = 0;
1947
                }
1948
                else
1949
                {
1950
                    gapX = Math.Abs(line.SPPID.START_X - line.SPPID.END_X) / 100 * percent;
1951
                    gapY = Math.Abs(line.SPPID.START_Y - line.SPPID.END_Y) / 100 * percent;
1952
                }
1953

    
1954
                if (line.SPPID.START_X < line.SPPID.END_X)
1955
                    tempX = line.SPPID.START_X + gapX;
1956
                else
1957
                    tempX = line.SPPID.START_X - gapX;
1958

    
1959
                if (line.SPPID.START_Y < line.SPPID.END_Y)
1960
                    tempY = line.SPPID.START_Y + gapY;
1961
                else
1962
                    tempY = line.SPPID.START_Y - gapY;
1963

    
1964
                
1965
               
1966
                Dictionary<LMConnector, List<double[]>> connectorVertices = GetPipeRunVertices(line.SPPID.ModelItemId);
1967
                LMConnector _TargetItem = null;
1968
                double distance = double.MaxValue;
1969
                double[] startPoint = null;
1970
                double[] endPoint = null;
1971
                // ID2의 기준 Gap으로 제일 가까운 Line 찾음(Slope도 같은조건)
1972
                foreach (var item in connectorVertices)
1973
                {
1974
                    for (int i = 0; i < item.Value.Count - 1; i++)
1975
                    {
1976
                        List<double[]> points = item.Value;
1977
                        double[] point1 = points[i];
1978
                        double[] point2 = points[i + 1];
1979

    
1980
                        SlopeType slopeType = SPPIDUtil.CalcSlope(point1[0], point1[1], point2[0], point2[1]);
1981
                        if (slopeType == targetSlopeType)
1982
                        {
1983
                            double result = SPPIDUtil.CalcPointToPointdDistance(point1[0], point1[1], tempX, tempY);
1984
                            if (result < distance)
1985
                            {
1986
                                distance = result;
1987
                                _TargetItem = item.Key;
1988

    
1989
                                startPoint = point1;
1990
                                endPoint = point2;
1991
                            }
1992

    
1993
                            result = SPPIDUtil.CalcPointToPointdDistance(point2[0], point2[1], tempX, tempY);
1994
                            if (result < distance)
1995
                            {
1996
                                distance = result;
1997
                                _TargetItem = item.Key;
1998

    
1999
                                startPoint = point1;
2000
                                endPoint = point2;
2001
                            }
2002
                        }
2003
                    }
2004
                }
2005

    
2006
                if (_TargetItem != null)
2007
                {
2008
                    double x = 0;
2009
                    double y = 0;
2010
                    double angle = 0;
2011
                    // SPPID 기준의 Gap으로 실 좌표를 구함
2012
                    if (percent == 0)
2013
                    {
2014
                        gapX = 0;
2015
                        gapY = 0;
2016
                    }
2017
                    else
2018
                    {
2019
                        gapX = Math.Abs(startPoint[0] - endPoint[0]) / 100 * percent;
2020
                        gapY = Math.Abs(startPoint[1] - endPoint[1]) / 100 * percent;
2021
                    }
2022

    
2023
                    if (startPoint[0] < endPoint[0])
2024
                        x = startPoint[0] + gapX;
2025
                    else
2026
                        x = startPoint[0] - gapX;
2027

    
2028
                    if (startPoint[1] < endPoint[1])
2029
                        y = startPoint[1] + gapY;
2030
                    else
2031
                        y = startPoint[1] - gapY;
2032
                    
2033
                    if (targetSlopeType == SlopeType.HORIZONTAL)
2034
                    {
2035
                        if (startPoint[0] < endPoint[0])
2036
                            angle = 0;
2037
                        else
2038
                            angle = Math.PI;
2039
                    }
2040
                    // 90 270
2041
                    else if (targetSlopeType == SlopeType.VERTICAL)
2042
                    {
2043
                        if (startPoint[1] < endPoint[1])
2044
                            angle = 90 * Math.PI / 180;
2045
                        else
2046
                            angle = 270 * Math.PI / 180;
2047
                    }
2048

    
2049
                    LMSymbol _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: 0, Rotation: angle, TargetItem: _TargetItem);
2050
                    
2051
                    if (_LMSymbol != null)
2052
                    {
2053
                        ReleaseCOMObjects(_LMSymbol);
2054
                    }
2055
                        
2056
                }
2057

    
2058
                foreach (var item in connectorVertices)
2059
                    ReleaseCOMObjects(item.Key);
2060
            }
2061
        }
2062

    
2063
        /// <summary>
2064
        /// Line Number 기준으로 모든 Item에 Line Number의 Attribute Input
2065
        /// </summary>
2066
        /// <param name="lineNumber"></param>
2067
        private void InputLineNumberAttribute(LineNumber lineNumber)
2068
        {
2069
            foreach (LineRun run in lineNumber.RUNS)
2070
            {
2071
                foreach (var item in run.RUNITEMS)
2072
                {
2073
                    if (item.GetType() == typeof(Symbol))
2074
                    {
2075
                        Symbol symbol = item as Symbol;
2076
                        LMSymbol _LMSymbol = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
2077
                        LMModelItem _LMModelItem = _LMSymbol.ModelItemObject;
2078

    
2079
                        if (_LMModelItem != null && _LMModelItem.get_ItemStatus() == "Active")
2080
                        {
2081
                            foreach (var attribute in lineNumber.ATTRIBUTES)
2082
                            {
2083
                                LineNumberMapping mapping = document.LineNumberMappings.Find(x => x.UID == attribute.UID);
2084
                                if (mapping != null && !string.IsNullOrEmpty(attribute.VALUE) && attribute.VALUE != "None")
2085
                                {
2086
                                    LMAAttribute _LMAAttribute = _LMModelItem.Attributes[mapping.SPPIDATTRIBUTENAME];
2087
                                    if (_LMAAttribute != null)
2088
                                    {
2089
                                        if (DBNull.Value.Equals(_LMAAttribute.get_Value()))
2090
                                            _LMAAttribute.set_Value(attribute.VALUE);
2091
                                        else if (_LMAAttribute.get_Value() != attribute.VALUE)
2092
                                            _LMAAttribute.set_Value(attribute.VALUE);
2093
                                    }
2094
                                }
2095
                            }
2096
                            _LMModelItem.Commit();
2097
                        }
2098
                        if (_LMModelItem != null)
2099
                            ReleaseCOMObjects(_LMModelItem);
2100
                        if (_LMSymbol != null)
2101
                            ReleaseCOMObjects(_LMSymbol);
2102
                    }
2103
                    else if (item.GetType() == typeof(Line))
2104
                    {
2105
                        Line line = item as Line;
2106
                        if (line != null)
2107
                        {
2108
                            LMModelItem _LMModelItem = dataSource.GetModelItem(line.SPPID.ModelItemId);
2109
                            if (_LMModelItem != null && _LMModelItem.get_ItemStatus() == "Active")
2110
                            {
2111
                                foreach (var attribute in lineNumber.ATTRIBUTES)
2112
                                {
2113
                                    LineNumberMapping mapping = document.LineNumberMappings.Find(x => x.UID == attribute.UID);
2114
                                    if (mapping != null && !string.IsNullOrEmpty(attribute.VALUE) && attribute.VALUE != "None")
2115
                                    {
2116
                                        LMAAttribute _LMAAttribute = _LMModelItem.Attributes[mapping.SPPIDATTRIBUTENAME];
2117
                                        if (_LMAAttribute != null)
2118
                                        {
2119
                                            if (DBNull.Value.Equals(_LMAAttribute.get_Value()))
2120
                                                _LMAAttribute.set_Value(attribute.VALUE);
2121
                                            else if (_LMAAttribute.get_Value() != attribute.VALUE)
2122
                                                _LMAAttribute.set_Value(attribute.VALUE);
2123
                                            
2124
                                        }
2125
                                    }
2126
                                }
2127
                                _LMModelItem.Commit();
2128
                            }
2129
                            if (_LMModelItem != null)
2130
                                ReleaseCOMObjects(_LMModelItem);
2131
                        }
2132
                    }
2133
                }
2134
            }
2135

    
2136
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetProgress, ++CurrentCount);
2137
        }
2138

    
2139
        /// <summary>
2140
        /// Symbol Attribute 입력 메서드
2141
        /// </summary>
2142
        /// <param name="item"></param>
2143
        private void InputSymbolAttribute(object targetItem, List<BaseModel.Attribute> targetAttributes)
2144
        {
2145
            try
2146
            {
2147
                // Object 아이템이 Symbol일 경우 Equipment일 경우 
2148
                string sRep = null;
2149
                if (targetItem.GetType() == typeof(Symbol))
2150
                    sRep = ((Symbol)targetItem).SPPID.RepresentationId;
2151
                else if (targetItem.GetType() == typeof(Equipment))
2152
                    sRep = ((Equipment)targetItem).SPPID.RepresentationId;
2153
                    
2154
                if (!string.IsNullOrEmpty(sRep))
2155
                {
2156
                    LMSymbol _LMSymbol = dataSource.GetSymbol(sRep);
2157
                    LMModelItem _LMModelItem = _LMSymbol.ModelItemObject;
2158
                    LMAAttributes _Attributes = _LMModelItem.Attributes;
2159

    
2160
                    foreach (var item in targetAttributes)
2161
                    {
2162
                        AttributeMapping mapping = document.AttributeMappings.Find(x => x.UID == item.UID);
2163
                        if (mapping != null && !string.IsNullOrEmpty(item.VALUE) && item.VALUE != "None")
2164
                        {
2165
                            LMAAttribute _Attribute = _Attributes[mapping.SPPIDATTRIBUTENAME];
2166
                            if (_Attribute != null)
2167
                                _Attribute.set_Value(item.VALUE);
2168
                        }
2169
                    }
2170
                    _LMModelItem.Commit();
2171
                    
2172
                    ReleaseCOMObjects(_Attributes);
2173
                    ReleaseCOMObjects(_LMModelItem);
2174
                    ReleaseCOMObjects(_LMSymbol);
2175
                }
2176
            }
2177
            catch (Exception ex)
2178
            {
2179
                System.Windows.Forms.MessageBox.Show(ex.Message + "\r\n" + ex.StackTrace);
2180
            }
2181

    
2182
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetProgress, ++CurrentCount);
2183
        }
2184

    
2185
        /// <summary>
2186
        /// Text Modeling - Association일 경우는 Text대신 해당 맵핑된 Symbol로 모델링
2187
        /// </summary>
2188
        /// <param name="text"></param>
2189
        private void TextModeling(Text text)
2190
        {
2191
            LMSymbol _LMSymbol = null;
2192
            try
2193
            {
2194
                //if (text.ASSOCIATION && !string.IsNullOrEmpty(text.OWNER) && text.OWNER != "None")
2195
                if (text.ASSOCIATION)
2196
                {
2197
                    object owner = SPPIDUtil.FindObjectByUID(document, text.OWNER);
2198
                    if (owner.GetType() == typeof(Symbol))
2199
                    {
2200
                        Symbol symbol = owner as Symbol;
2201
                        _LMSymbol = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
2202
                        if (_LMSymbol != null)
2203
                        {
2204
                            Association association = symbol.ASSOCIATIONS.Find(x => x.VALUE == text.UID);
2205
                            List<BaseModel.Attribute> attributes = symbol.ATTRIBUTES.FindAll(x => x.ATTRIBUTETYPE == association.TYPE);
2206
                            AttributeMapping mapping = null;
2207
                            foreach (var attribute in attributes)
2208
                            {
2209
                                if (string.IsNullOrEmpty(attribute.VALUE) || attribute.VALUE == "None")
2210
                                    continue;
2211

    
2212
                                 mapping = document.AttributeMappings.Find(x => x.UID == attribute.UID && !string.IsNullOrEmpty(x.SPPIDSYMBOLNAME));
2213
                                if (mapping != null)
2214
                                    break;  
2215
                            }
2216

    
2217
                            if (mapping != null)
2218
                            {
2219
                                double x = 0;
2220
                                double y = 0;
2221

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

    
2225
                                LMLabelPersist _LMLabelPersist = _placement.PIDPlaceLabel(mapping.SPPIDSYMBOLNAME, ref array, Rotation: text.ANGLE, LabeledItem: _LMSymbol.AsLMRepresentation(), IsLeaderVisible: mapping.LeaderLine);
2226
                                if (_LMLabelPersist!=null)
2227
                                {
2228
                                    _LMLabelPersist.Commit();
2229
                                    ReleaseCOMObjects(_LMLabelPersist);
2230
                                }
2231
                            }
2232
                        }
2233
                    }
2234
                    else if (owner.GetType() == typeof(Line))
2235
                    {
2236

    
2237
                    }
2238
                }
2239
                else
2240
                {
2241
                    LMItemNote _LMItemNote = null;
2242
                    LMAAttribute _LMAAttribute = null;
2243

    
2244
                    double x = 0;
2245
                    double y = 0;
2246

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

    
2249
                    _LMSymbol = _placement.PIDPlaceSymbol(text.SPPID.MAPPINGNAME, x, y);
2250
                    _LMSymbol.Commit();
2251
                    _LMItemNote = _placement.PIDDataSource.GetItemNote(_LMSymbol.ModelItemID);
2252
                    _LMItemNote.Commit();
2253
                    _LMAAttribute = _LMItemNote.Attributes["Note.Body"];
2254
                    _LMAAttribute.set_Value(text.VALUE);
2255
                    _LMItemNote.Commit();
2256

    
2257
                    if (_LMAAttribute != null)
2258
                        ReleaseCOMObjects(_LMAAttribute);
2259
                    if (_LMItemNote != null)
2260
                        ReleaseCOMObjects(_LMItemNote);
2261
                }
2262
            }
2263
            catch (Exception ex)
2264
            {
2265

    
2266
            }
2267
            finally
2268
            {
2269
                if (_LMSymbol != null)
2270
                    ReleaseCOMObjects(_LMSymbol);
2271
            }
2272

    
2273
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetProgress, ++CurrentCount);
2274
        }
2275

    
2276
        /// <summary>
2277
        /// Note Modeling
2278
        /// </summary>
2279
        /// <param name="note"></param>
2280
        private void NoteModeling(Note note)
2281
        {
2282
            LMSymbol _LMSymbol = null;
2283
            LMItemNote _LMItemNote = null;
2284
            LMAAttribute _LMAAttribute = null;
2285

    
2286
            try
2287
            {
2288
                double x = 0;
2289
                double y = 0;
2290

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

    
2293
                _LMSymbol = _placement.PIDPlaceSymbol(note.SPPID.MAPPINGNAME, x, y);
2294
                _LMSymbol.Commit();
2295
                _LMItemNote = _placement.PIDDataSource.GetItemNote(_LMSymbol.ModelItemID);
2296
                _LMItemNote.Commit();
2297
                _LMAAttribute = _LMItemNote.Attributes["Note.Body"];
2298
                _LMAAttribute.set_Value(note.VALUE);
2299
                _LMItemNote.Commit();
2300
            }
2301
            catch (Exception ex)
2302
            {
2303

    
2304
            }
2305
            finally
2306
            {
2307
                if (_LMAAttribute != null)
2308
                    ReleaseCOMObjects(_LMAAttribute);
2309
                if (_LMItemNote != null)
2310
                    ReleaseCOMObjects(_LMItemNote);
2311
                if (_LMSymbol != null)
2312
                    ReleaseCOMObjects(_LMSymbol);
2313
            }
2314

    
2315
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetProgress, ++CurrentCount);
2316
        }
2317

    
2318
        /// <summary>
2319
        /// Label의 좌표를 구하는 메서드(ID2 기준의 좌표 -> SPPID 좌표)
2320
        /// </summary>
2321
        /// <param name="x"></param>
2322
        /// <param name="y"></param>
2323
        /// <param name="originX"></param>
2324
        /// <param name="originY"></param>
2325
        /// <param name="SPPIDLabelLocation"></param>
2326
        /// <param name="location"></param>
2327
        private void CalcLabelLocation(ref double x, ref double y, double originX, double originY, SPPIDLabelLocationInfo SPPIDLabelLocation, Location location)
2328
        {
2329
            if (location == Location.None)
2330
            {
2331
                x = originX;
2332
                y = originY;
2333
            }
2334
            else
2335
            {
2336
                if (location.HasFlag(Location.Center))
2337
                {
2338
                    x = (SPPIDLabelLocation.X1 + SPPIDLabelLocation.X2) / 2;
2339
                    y = (SPPIDLabelLocation.Y1 + SPPIDLabelLocation.Y2) / 2;
2340
                }
2341

    
2342
                if (location.HasFlag(Location.Left))
2343
                    x = SPPIDLabelLocation.X1;
2344
                else if (location.HasFlag(Location.Right))
2345
                    x = SPPIDLabelLocation.X2;
2346

    
2347
                if (location.HasFlag(Location.Down))
2348
                    y = SPPIDLabelLocation.Y1;
2349
                else if (location.HasFlag(Location.Up))
2350
                    y = SPPIDLabelLocation.Y2;
2351
            }
2352
        }
2353

    
2354
        /// <summary>
2355
        /// Graphic OID로 해당 Symbol의 크기를 구하여 Zoom
2356
        /// </summary>
2357
        /// <param name="graphicOID"></param>
2358
        /// <param name="milliseconds"></param>
2359
        private void ZoomObjectByGraphicOID(string graphicOID, int milliseconds = 150)
2360
        {
2361
            if (radApp.ActiveDocument.ActiveSheet.DrawingObjects[graphicOID] != null)
2362
            {
2363
                double minX = 0;
2364
                double minY = 0;
2365
                double maxX = 0;
2366
                double maxY = 0;
2367
                radApp.ActiveDocument.ActiveSheet.DrawingObjects[graphicOID].Range(out minX, out minY, out maxX, out maxY);
2368
                radApp.ActiveWindow.ZoomArea2(minX - 0.007, minY - 0.007, maxX + 0.007, maxY + 0.007, null);
2369

    
2370
                Thread.Sleep(milliseconds);
2371
            }
2372
        }
2373

    
2374
        /// <summary>
2375
        /// ComObject를 Release
2376
        /// </summary>
2377
        /// <param name="objVars"></param>
2378
        public void ReleaseCOMObjects(params object[] objVars)
2379
        {
2380
            int intNewRefCount = 0;
2381
            foreach (object obj in objVars)
2382
            {
2383
                if (!Information.IsNothing(obj) && System.Runtime.InteropServices.Marshal.IsComObject(obj))
2384
                    intNewRefCount = intNewRefCount + System.Runtime.InteropServices.Marshal.FinalReleaseComObject(obj);
2385
            }
2386
        }
2387
    }
2388
}
클립보드 이미지 추가 (최대 크기: 500 MB)