프로젝트

일반

사용자정보

통계
| 개정판:

hytos / DTI_PID / SPPIDConverter / AutoModeling.cs @ 2fdb56bf

이력 | 보기 | 이력해설 | 다운로드 (93.5 KB)

1
using System;
2
using System.Collections.Generic;
3
using System.Linq;
4
using System.Text;
5
using System.Threading.Tasks;
6
using Llama;
7
using Plaice;
8
using Ingr.RAD2D.Interop.RAD2D;
9
using Ingr.RAD2D.Internal;
10
using Ingr.RAD2D.Helper;
11
using Converter.BaseModel;
12
using Converter.SPPID.Model;
13
using Converter.SPPID.Properties;
14
using Converter.SPPID.Util;
15
using Converter.SPPID.DB;
16
using Ingr.RAD2D.MacroControls.CmdCtrl;
17
using Ingr.RAD2D;
18
using System.Windows;
19
using System.Threading;
20
using System.Drawing;
21
using Microsoft.VisualBasic;
22
using Newtonsoft.Json;
23

    
24
using DevExpress.XtraSplashScreen;
25
namespace Converter.SPPID
26
{
27
    public class AutoModeling
28
    {
29
        Placement _placement;
30
        LMADataSource dataSource;
31
        dynamic newDrawing;
32
        dynamic application;
33
        Ingr.RAD2D.Application radApp;
34
        SPPID_Document document;
35
        ETCSetting _ETCSetting;
36

    
37
        public string DocumentLabelText { get; set; }
38

    
39
        int CurrentCount;
40

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

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

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

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

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

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

    
95
                CreateDocument();
96

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

    
101
                    SplashScreenManager.ShowForm(typeof(SPPIDSplashScreen), true, true);
102
                    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
                    List<List<Symbol>> symbolGroups = SPPIDUtil.GetThreeConnectedSymbolGroup(document);
109
                    foreach (List<Symbol> symbolGroup in symbolGroups)
110
                        SymbolModelingByThreeSymbolGroup(symbolGroup);
111

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

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

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

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

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

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

    
152
                    // Note Modeling
153
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Notes Modeling");
154
                    foreach (var item in document.NOTES)
155
                        NoteModeling(item);
156

    
157
                    // Text Modeling
158
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Texts Modeling");
159
                    foreach (var item in document.TEXTINFOS)
160
                        TextModeling(item);
161

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

    
172
                    // Input LineNumber Attribute
173
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Set Lines Attribute");
174
                    foreach (var item in document.LINENUMBERS)
175
                        InputLineNumberAttribute(item);
176

    
177
                    // Input Symbol Attribute
178
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Set Symbols Attribute");
179
                    foreach (var item in document.SYMBOLS)
180
                        InputSymbolAttribute(item, item.ATTRIBUTES);
181

    
182
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Labels Modeling");
183
                    foreach (var item in document.SYMBOLS)
184
                        LabelSymbolModeling(item);
185

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

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

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

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

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

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

    
286
                    prevLine = line;
287

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

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

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

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

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

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

    
366

    
367
        }
368

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

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

    
413
            LMSymbol _LMSymbol = null;
414

    
415
            string mappingPath = symbol.SPPID.MAPPINGNAME;
416
            double x = symbol.SPPID.ORIGINAL_X;
417
            double y = symbol.SPPID.ORIGINAL_Y;
418
            int mirror = 0;
419
            double angle = symbol.ANGLE;
420

    
421
            SPPIDUtil.ConvertGridPoint(ref x, ref y);
422

    
423
            // OPC 일경우 180도 일때 Mirror
424
            if (mappingPath.Contains("Piping OPC's") && angle == Math.PI)
425
                mirror = 1;
426

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

    
434
                _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle, TargetItem: _TargetItem);
435
                ReleaseCOMObjects(_TargetItem);
436
            }
437
            else if (prevSymbol != null)
438
            {
439
                LMSymbol _PrevSymbol = dataSource.GetSymbol(prevSymbol.SPPID.RepresentationId);
440
                SlopeType slopeType = SPPIDUtil.CalcSlope(x, y, prevSymbol.SPPID.ORIGINAL_X, prevSymbol.SPPID.ORIGINAL_Y);
441
                double prevX = _PrevSymbol.get_XCoordinate();
442
                double prevY = _PrevSymbol.get_YCoordinate();
443
                //if (slopeType == SlopeType.HORIZONTAL)
444
                //    y = prevY;
445
                //else if (slopeType == SlopeType.VERTICAL)
446
                //    x = prevX;
447
                ReleaseCOMObjects(_PrevSymbol);
448
                _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle);
449
            }
450
            else
451
                _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle);
452

    
453

    
454
            if (_LMSymbol != null)
455
            {
456
                _LMSymbol.Commit();
457
                symbol.SPPID.RepresentationId = _LMSymbol.AsLMRepresentation().Id;
458
                symbol.SPPID.GraphicOID = _LMSymbol.get_GraphicOID();
459

    
460
                foreach (var item in symbol.ChildSymbols)
461
                    CreateChildSymbol(item, _LMSymbol);
462
            }
463

    
464
            ReleaseCOMObjects(_LMSymbol);
465
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetProgress, ++CurrentCount);
466
        }
467

    
468
        /// <summary>
469
        /// ID2의 Symbol Width와 Height를 비교해서 상대적인 SPPID Connector좌표를 가져온다.
470
        /// </summary>
471
        /// <param name="targetConnector"></param>
472
        /// <param name="targetSymbol"></param>
473
        /// <param name="x"></param>
474
        /// <param name="y"></param>
475
        private void GetTargetSymbolConnectorPoint(Connector targetConnector, Symbol targetSymbol, ref double x, ref double y)
476
        {
477
            LMSymbol _TargetItem = dataSource.GetSymbol(targetSymbol.SPPID.RepresentationId);
478

    
479
            double[] range = null;
480
            List<double[]> points = new List<double[]>();
481
            GetSPPIDSymbolRangeAndConnectionPoints(targetSymbol, ref range, points);
482
            double x1 = range[0];
483
            double y1 = range[1];
484
            double x2 = range[2];
485
            double y2 = range[3];
486

    
487
            // Origin 기준 Connector의 위치차이
488
            double sceneX = 0;
489
            double sceneY = 0;
490
            SPPIDUtil.ConvertPointBystring(targetConnector.SCENECONNECTPOINT, ref sceneX, ref sceneY);
491
            double originX = 0;
492
            double originY = 0;
493
            SPPIDUtil.ConvertPointBystring(targetSymbol.ORIGINALPOINT, ref originX, ref originY);
494
            double gapX = originX - sceneX;
495
            double gapY = originY - sceneY;
496

    
497
            // SPPID Symbol과 ID2 심볼의 크기 차이
498
            double sizeWidth = 0;
499
            double sizeHeight = 0;
500
            SPPIDUtil.ConvertPointBystring(targetSymbol.SIZE, ref sizeWidth, ref sizeHeight);
501
            double percentX = (x2 - x1) / sizeWidth;
502
            double percentY = (y2 - y1) / sizeHeight;
503

    
504
            double SPPIDgapX = gapX * percentX;
505
            double SPPIDgapY = gapY * percentY;
506

    
507
            double[] SPPIDOriginPoint = new double[] { _TargetItem.get_XCoordinate() - SPPIDgapX, _TargetItem.get_YCoordinate() + SPPIDgapY };
508
            double distance = double.MaxValue;
509
            double[] resultPoint;
510
            foreach (var point in points)
511
            {
512
                double result = SPPIDUtil.CalcPointToPointdDistance(point[0], point[1], SPPIDOriginPoint[0], SPPIDOriginPoint[1]);
513
                if (distance > result)
514
                {
515
                    distance = result;
516
                    resultPoint = point;
517
                    x = point[0];
518
                    y = point[1];
519
                }
520
            }
521

    
522
            ReleaseCOMObjects(_TargetItem);
523
        }
524

    
525
        /// <summary>
526
        /// SPPID Symbol의 Range를 구한다.
527
        /// </summary>
528
        /// <param name="symbol"></param>
529
        /// <param name="range"></param>
530
        private void GetSPPIDSymbolRangeAndConnectionPoints(Symbol symbol, ref double[] range, List<double[]> points)
531
        {
532
            LMSymbol _TargetItem = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
533
            Ingr.RAD2D.Symbol2d symbol2d = radApp.ActiveDocument.ActiveSheet.DrawingObjects[_TargetItem.get_GraphicOID().ToString()];
534
            double x1 = 0;
535
            double y1 = 0;
536
            double x2 = 0;
537
            double y2 = 0;
538
            symbol2d.Range(out x1, out y1, out x2, out y2);
539
            range = new double[] { x1, y1, x2, y2 };
540

    
541
            for (int i = 1; i < int.MaxValue; i++)
542
            {
543
                double connX = 0;
544
                double connY = 0;
545
                if (_placement.PIDConnectPointLocation(_TargetItem, i, ref connX, ref connY))
546
                    points.Add(new double[] { connX, connY });
547
                else
548
                    break;
549
            }
550

    
551
            foreach (var childSymbol in symbol.ChildSymbols)
552
                GetSPPIDChildSymbolRange(childSymbol, ref range, points);
553

    
554
            ReleaseCOMObjects(_TargetItem);
555
        }
556

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

    
576
            for (int i = 1; i < int.MaxValue; i++)
577
            {
578
                double connX = 0;
579
                double connY = 0;
580
                if (_placement.PIDConnectPointLocation(_ChildSymbol, i, ref connX, ref connY))
581
                    points.Add(new double[] { connX, connY });
582
                else
583
                    break;
584
            }
585

    
586
            foreach (var loopChildSymbol in childSymbol.ChildSymbols)
587
                GetSPPIDChildSymbolRange(loopChildSymbol, ref range, points);
588

    
589
            ReleaseCOMObjects(_ChildSymbol);
590
        }
591

    
592
        /// <summary>
593
        /// Label Symbol Modeling
594
        /// </summary>
595
        /// <param name="symbol"></param>
596
        private void LabelSymbolModeling(Symbol symbol)
597
        {
598
            BaseModel.Attribute itemAttribute = symbol.ATTRIBUTES.Find(x => x.ATTRIBUTE == "OWNERSYMBOL");
599
            if (itemAttribute == null || string.IsNullOrEmpty(itemAttribute.VALUE))
600
                return;
601

    
602
            Array points = new double[] { 0, symbol.SPPID.ORIGINAL_X, symbol.SPPID.ORIGINAL_Y };
603
            
604
            string symbolUID = itemAttribute.VALUE;
605
            object targetItem = SPPIDUtil.FindObjectByUID(document, symbolUID);
606
            if (targetItem != null)
607
            {
608
                // Object 아이템이 Symbol일 경우 Equipment일 경우 
609
                string sRep = null;
610
                if (targetItem.GetType() == typeof(Symbol))
611
                    sRep = ((Symbol)targetItem).SPPID.RepresentationId;
612
                else if (targetItem.GetType() == typeof(Equipment))
613
                    sRep = ((Equipment)targetItem).SPPID.RepresentationId;
614

    
615
                if (!string.IsNullOrEmpty(sRep))
616
                {
617
                    // LEADER Line 검사
618
                    bool leaderLine = false;
619
                    SymbolMapping symbolMapping = document.SymbolMappings.Find(x => x.UID == symbol.DBUID);
620
                    if (symbolMapping != null)
621
                        leaderLine = symbolMapping.LEADERLINE;
622

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

    
627
                    // Target Item에 Label의 Attribute Input
628
                    InputSymbolAttribute(targetItem, symbol.ATTRIBUTES);
629

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

    
659
                                if (result)
660
                                    break;
661
                            }
662

    
663
                            if (result)
664
                                break;
665
                        }
666
                    }
667

    
668
                    _LMLabelPresist.Commit();
669
                    ReleaseCOMObjects(_TargetItem);
670
                    ReleaseCOMObjects(_LMLabelPresist);
671
                }
672
            }
673

    
674
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetProgress, ++CurrentCount);
675
        }
676

    
677
        /// <summary>
678
        /// Equipment를 실제로 Modeling 메서드
679
        /// </summary>
680
        /// <param name="equipment"></param>
681
        private void EquipmentModeling(Equipment equipment)
682
        {
683
            if (!string.IsNullOrEmpty(equipment.SPPID.RepresentationId))
684
                return;
685

    
686
            LMSymbol _LMSymbol = null;
687
            LMSymbol targetItem = null;
688
            string mappingPath = equipment.SPPID.MAPPINGNAME;
689
            double x = equipment.SPPID.ORIGINAL_X;
690
            double y = equipment.SPPID.ORIGINAL_Y;
691
            int mirror = 0;
692
            double angle = equipment.ANGLE;
693

    
694
            SPPIDUtil.ConvertGridPoint(ref x, ref y);
695

    
696
            Connector connector = equipment.CONNECTORS.Find(conn => !string.IsNullOrEmpty(conn.CONNECTEDITEM) && conn.CONNECTEDITEM != "None");
697
            if (connector != null)
698
            {
699
                Equipment connEquipment = SPPIDUtil.FindObjectByUID(document, connector.CONNECTEDITEM) as Equipment;
700
                if (connEquipment != null)
701
                {
702
                    if (string.IsNullOrEmpty(connEquipment.SPPID.RepresentationId))
703
                        EquipmentModeling(connEquipment);
704

    
705
                    if (!string.IsNullOrEmpty(connEquipment.SPPID.RepresentationId))
706
                    {
707
                        targetItem = dataSource.GetSymbol(connEquipment.SPPID.RepresentationId);
708
                        if (targetItem != null)
709
                        {
710
                            _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle, TargetItem: targetItem);
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
            else
728
            {
729
                _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle);
730
            }
731

    
732
            if (_LMSymbol != null)
733
            {
734
                _LMSymbol.Commit();
735
                equipment.SPPID.RepresentationId = _LMSymbol.AsLMRepresentation().Id;
736
                equipment.SPPID.GraphicOID = _LMSymbol.get_GraphicOID();
737
                ReleaseCOMObjects(_LMSymbol);
738
            }
739

    
740
            if (targetItem != null)
741
            {
742
                ReleaseCOMObjects(targetItem);
743
            }
744
            
745
            ReleaseCOMObjects(_LMSymbol);
746

    
747
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetProgress, ++CurrentCount);
748
        }
749

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

    
770
                // Center Symbol
771
                if (count == 2)
772
                {
773
                    SymbolModeling(symbol, null, null);
774
                    centerSymbol = symbol;
775
                }
776
                else if (symbol1 == null)
777
                    symbol1 = symbol;
778
                else
779
                    symbol2 = symbol;
780
            }
781

    
782
            SymbolModeling(symbol1, centerSymbol, null);
783
            SymbolModeling(symbol2, centerSymbol, null);
784
        }
785

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

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

    
809
            ReleaseCOMObjects(_LMSymbol);
810
        }
811

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

    
836
                            return false;
837

    
838
                        }
839
                    }
840
                }
841
            }
842

    
843
            return false;
844
        }
845

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

    
861
            Line startBranchLine = null;
862
            Line endBranchLine = null;
863

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

    
887
                            placeRunInputs.AddSymbolTarget(_LMSymbol1, x, y);
888
                        }
889
                        else
890
                            placeRunInputs.AddPoint(line.SPPID.START_X, line.SPPID.START_Y);
891
                    }
892
                    else if (connItem != null && connItem.GetType() == typeof(Line) && !lines.Contains(connItem))
893
                    {
894
                        connectorVertices1 = GetPipeRunVertices(((Line)connItem).SPPID.ModelItemId);
895
                        targetConnector1 = FindTargetLMConnector(connectorVertices1, line.SPPID.START_X, line.SPPID.START_Y, line.SPPID.END_X, line.SPPID.END_Y);
896

    
897
                        if (targetConnector1 != null)
898
                            placeRunInputs.AddConnectorTarget(targetConnector1, line.SPPID.START_X, line.SPPID.START_Y);
899
                        else
900
                        {
901
                            startBranchLine = connItem as Line;
902
                            placeRunInputs.AddPoint(line.SPPID.START_X, line.SPPID.START_Y);
903
                        }
904
                    }
905
                    else
906
                        placeRunInputs.AddPoint(line.SPPID.START_X, line.SPPID.START_Y);
907
                }
908
                if (i + 1 == lines.Count)
909
                {
910
                    // 끝점에 연결된 Symbol 찾기
911
                    object connItem = SPPIDUtil.FindObjectByUID(document, line.CONNECTORS[1].CONNECTEDITEM);
912

    
913
                    if (i != 0)
914
                        placeRunInputs.AddPoint(line.SPPID.START_X, line.SPPID.START_Y);
915

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

    
933
                            placeRunInputs.AddSymbolTarget(_LMSymbol2, x, y);
934
                        }
935
                        else
936
                            placeRunInputs.AddPoint(line.SPPID.END_X, line.SPPID.END_Y);
937
                            
938
                    }
939
                    else if (connItem != null && connItem.GetType() == typeof(Line) && !lines.Contains(connItem))
940
                    {
941
                        connectorVertices2 = GetPipeRunVertices(((Line)connItem).SPPID.ModelItemId);
942
                        targetConnector2 = FindTargetLMConnector(connectorVertices2, line.SPPID.END_X, line.SPPID.END_Y, line.SPPID.START_X, line.SPPID.START_Y);
943

    
944
                        if (targetConnector2 != null)
945
                            placeRunInputs.AddConnectorTarget(targetConnector2, line.SPPID.END_X, line.SPPID.END_Y);
946
                        else
947
                        {
948
                            endBranchLine = connItem as Line;
949
                            placeRunInputs.AddPoint(line.SPPID.END_X, line.SPPID.END_Y);
950
                        }
951
                    }
952
                    else
953
                    {
954
                        placeRunInputs.AddPoint(line.SPPID.END_X, line.SPPID.END_Y);
955
                    }
956
                }
957
            }
958

    
959
            LMConnector _lMConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
960

    
961
            if (_lMConnector != null)
962
            {
963
                foreach (var line in lines)
964
                    line.SPPID.ModelItemId = _lMConnector.ModelItemID;
965
                _lMConnector.Commit();
966
                if (startBranchLine != null || endBranchLine != null)
967
                {
968
                    BranchLines.Add(new Tuple<string, Line, Line>(_lMConnector.ModelItemID, startBranchLine, endBranchLine));
969
                }
970
            }
971

    
972

    
973
            if (_LMSymbol1 != null)
974
                ReleaseCOMObjects(_LMSymbol1);
975
            if (_LMSymbol2 != null)
976
                ReleaseCOMObjects(_LMSymbol2);
977
            if (targetConnector1 != null)
978
                ReleaseCOMObjects(targetConnector1);
979
            if (targetConnector2 != null)
980
                ReleaseCOMObjects(targetConnector2);
981
            foreach (var item in connectorVertices1)
982
                ReleaseCOMObjects(item.Key);
983
            foreach (var item in connectorVertices2)
984
                ReleaseCOMObjects(item.Key);
985

    
986
            ReleaseCOMObjects(_lMConnector);
987
            ReleaseCOMObjects(placeRunInputs);
988
            ReleaseCOMObjects(_LMAItem);
989
        }
990

    
991
        /// <summary>
992
        /// Symbol이 모델링된 SPPPID Symbol Object를 반환 - 연결된 Symbol이 ChildSymbol일 수도 있기때문에 메서드 개발
993
        /// </summary>
994
        /// <param name="symbol"></param>
995
        /// <param name="line"></param>
996
        /// <returns></returns>
997
        private LMSymbol GetTargetSymbol(Symbol symbol, Line line)
998
        {
999
            LMSymbol _LMSymbol = null;
1000
            foreach (var connector in symbol.CONNECTORS)
1001
            {
1002
                if (connector.CONNECTEDITEM == line.UID)
1003
                {
1004
                    if (connector.Index == 0)
1005
                        _LMSymbol = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
1006
                    else
1007
                    {
1008
                        ChildSymbol child = null;
1009
                        foreach (var childSymbol in symbol.ChildSymbols)
1010
                        {
1011
                            if (childSymbol.Connectors.Contains(connector))
1012
                                child = childSymbol;
1013
                            else
1014
                                child = GetChildSymbolByConnector(childSymbol, connector);
1015

    
1016
                            if (child != null)
1017
                                break;
1018
                        }
1019

    
1020
                        if (child != null)
1021
                            _LMSymbol = dataSource.GetSymbol(child.SPPID.RepresentationId);
1022
                    }
1023

    
1024
                    break;  
1025
                }
1026
            }
1027

    
1028
            return _LMSymbol;
1029
        }
1030

    
1031
        /// <summary>
1032
        /// Connector를 가지고 있는 ChildSymbol Object 반환
1033
        /// </summary>
1034
        /// <param name="item"></param>
1035
        /// <param name="connector"></param>
1036
        /// <returns></returns>
1037
        private ChildSymbol GetChildSymbolByConnector(ChildSymbol item, Connector connector)
1038
        {
1039
            foreach (var childSymbol in item.ChildSymbols)
1040
            {
1041
                if (childSymbol.Connectors.Contains(connector))
1042
                    return childSymbol;
1043
                else
1044
                    return GetChildSymbolByConnector(childSymbol, connector);
1045
            }
1046

    
1047
            return null;
1048
        }
1049

    
1050
        /// <summary>
1051
        /// Branch 라인을 다시 모델링하는 진입 메서드
1052
        /// </summary>
1053
        /// <param name="branch"></param>
1054
        private void BranchLineModeling(Tuple<string, Line, Line> branch)
1055
        {
1056
            List<Line> lines = SPPIDUtil.FindLinesByModelId(document, branch.Item1);
1057
            Dictionary<LMConnector, List<double[]>> connectorVertices = GetPipeRunVertices(branch.Item1);
1058

    
1059
            LMConnector _StartConnector = null;
1060
            LMConnector _EndConnector = null;
1061
            double lengthStart = double.MaxValue;
1062
            double lengthEnd = double.MaxValue;
1063
            List<double[]> startPoints = new List<double[]>();
1064
            List<double[]> endPoints = new List<double[]>();
1065

    
1066
            foreach (var item in connectorVertices)
1067
            {
1068
                foreach (var point in item.Value)
1069
                {
1070
                    // Start Point가 Branch
1071
                    if (branch.Item2 != null)
1072
                    {
1073
                        Line targetLine = branch.Item2;
1074
                        double distance = SPPIDUtil.CalcLineToPointDistance(targetLine.SPPID.START_X, targetLine.SPPID.START_Y, targetLine.SPPID.END_X, targetLine.SPPID.END_Y, point[0], point[1]);
1075
                        if (lengthStart > distance)
1076
                        {
1077
                            _StartConnector = item.Key;
1078
                            lengthStart = distance;
1079
                            startPoints = item.Value;
1080
                        }
1081
                    }
1082
                    // End Point가 Branch
1083
                    if (branch.Item3 != null)
1084
                    {
1085
                        Line targetLine = branch.Item3;
1086
                        double distance = SPPIDUtil.CalcLineToPointDistance(targetLine.SPPID.START_X, targetLine.SPPID.START_Y, targetLine.SPPID.END_X, targetLine.SPPID.END_Y, point[0], point[1]);
1087
                        if (lengthEnd > distance)
1088
                        {
1089
                            _EndConnector = item.Key;
1090
                            lengthEnd = distance;
1091
                            endPoints = item.Value;
1092
                        }
1093
                    }
1094
                }
1095
            }
1096
            #region Branch가 양쪽 전부일 때
1097
            if (_StartConnector != null && _StartConnector == _EndConnector)
1098
            {
1099
                _placement.PIDRemovePlacement(_StartConnector.AsLMRepresentation());
1100

    
1101
                _LMAItem _LMAItem = _placement.PIDCreateItem(lines[0].SPPID.MAPPINGNAME);
1102
                PlaceRunInputs placeRunInputs = new PlaceRunInputs();
1103

    
1104
                Dictionary<LMConnector, List<double[]>> startConnectorVertices = GetPipeRunVertices(branch.Item2.SPPID.ModelItemId);
1105
                LMConnector _StartTargetConnector = FindTargetLMConnector(startConnectorVertices, startPoints[0][0], startPoints[0][1], startPoints[1][0], startPoints[1][1]);
1106
                Dictionary<LMConnector, List<double[]>> endConnectorVertices = GetPipeRunVertices(branch.Item3.SPPID.ModelItemId);
1107
                LMConnector _EndTargetConnector = FindTargetLMConnector(endConnectorVertices,
1108
                   startPoints[startPoints.Count - 1][0],
1109
                   startPoints[startPoints.Count - 1][1],
1110
                   startPoints[startPoints.Count - 2][0],
1111
                   startPoints[startPoints.Count - 2][1]);
1112

    
1113
                for (int i = 0; i < startPoints.Count; i++)
1114
                {
1115
                    double[] point = startPoints[i];
1116
                    if (i == 0)
1117
                        placeRunInputs.AddConnectorTarget(_StartTargetConnector, point[0], point[1]);
1118
                    else if (i == startPoints.Count - 1)
1119
                        placeRunInputs.AddConnectorTarget(_EndTargetConnector, point[0], point[1]);
1120
                    else
1121
                        placeRunInputs.AddPoint(point[0], point[1]);
1122
                }
1123

    
1124
                LMConnector _LMConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
1125
                if (_LMConnector != null)
1126
                {
1127
                    _LMConnector.Commit();
1128
                    foreach (var item in lines)
1129
                        item.SPPID.ModelItemId = _LMConnector.ModelItemID;
1130
                }
1131

    
1132
                foreach (var item in startConnectorVertices)
1133
                    ReleaseCOMObjects(item.Key);
1134
                foreach (var item in endConnectorVertices)
1135
                    ReleaseCOMObjects(item.Key);
1136
                ReleaseCOMObjects(placeRunInputs);
1137
                ReleaseCOMObjects(_LMAItem);
1138
                ReleaseCOMObjects(_LMConnector);
1139
            }
1140
            #endregion
1141
            #region 양쪽이 다른 Branch 
1142
            else
1143
            {
1144
                // Branch 시작 Connector
1145
                if (_StartConnector != null)
1146
                    BranchLineModelingByConnector(branch, _StartConnector, startPoints, true);
1147

    
1148
                // Branch 끝 Connector
1149
                if (_EndConnector != null)
1150
                    BranchLineModelingByConnector(branch, _EndConnector, endPoints, false);
1151
            }
1152
            #endregion
1153

    
1154
            if (_StartConnector != null)
1155
                ReleaseCOMObjects(_StartConnector);
1156
            if (_EndConnector != null)
1157
                ReleaseCOMObjects(_EndConnector);
1158
            foreach (var item in connectorVertices)
1159
                ReleaseCOMObjects(item.Key);
1160
        }
1161

    
1162
        /// <summary>
1163
        /// Branch 라인을 다시 실제로 모델링하는 메서드
1164
        /// </summary>
1165
        /// <param name="branch"></param>
1166
        /// <param name="_Connector"></param>
1167
        /// <param name="points"></param>
1168
        /// <param name="IsStart"></param>
1169
        private void BranchLineModelingByConnector(Tuple<string, Line, Line> branch, LMConnector _Connector, List<double[]> points, bool IsStart)
1170
        {
1171
            List<Line> lines = SPPIDUtil.FindLinesByModelId(document, branch.Item1);
1172
            Dictionary<LMConnector, List<double[]>> connectorVertices = GetPipeRunVertices(branch.Item1);
1173
            LMConnector _SameRunTargetConnector = null;
1174
            LMSymbol _SameRunTargetSymbol = null;
1175
            Dictionary<LMConnector, List<double[]>> branchConnectorVertices = null;
1176
            LMConnector _BranchTargetConnector = null;
1177
            PlaceRunInputs placeRunInputs = new PlaceRunInputs();
1178

    
1179
            // 같은 Line Run의 Connector 찾기
1180
            foreach (var item in connectorVertices)
1181
            {
1182
                if (item.Key == _Connector)
1183
                    continue;
1184

    
1185
                if (IsStart &&
1186
                    !DBNull.Value.Equals(item.Key.ConnectItem1SymbolID) &&
1187
                    !DBNull.Value.Equals(_Connector.ConnectItem2SymbolID)&& 
1188
                    item.Key.ConnectItem1SymbolID == _Connector.ConnectItem2SymbolID)
1189
                {
1190
                    _SameRunTargetConnector = item.Key;
1191
                    break;
1192
                }
1193
                else if (!IsStart &&
1194
                    !DBNull.Value.Equals(item.Key.ConnectItem2SymbolID) &&
1195
                    !DBNull.Value.Equals(_Connector.ConnectItem1SymbolID) && 
1196
                    item.Key.ConnectItem2SymbolID == _Connector.ConnectItem1SymbolID)
1197
                {
1198
                    _SameRunTargetConnector = item.Key;
1199
                    break;
1200
                }
1201
            }
1202

    
1203
            // Branch 반대편이 Symbol
1204
            if (_SameRunTargetConnector == null)
1205
            {
1206
                foreach (var line in lines)
1207
                {
1208
                    foreach (var connector in line.CONNECTORS)
1209
                    {
1210
                        Symbol symbol = SPPIDUtil.FindObjectByUID(document, connector.CONNECTEDITEM) as Symbol;
1211
                        if (symbol != null)
1212
                        {
1213
                            _SameRunTargetSymbol = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
1214
                            break;
1215
                        }
1216
                    }
1217
                }
1218
            }
1219

    
1220
            // 기존 Connector 제거
1221
            _placement.PIDRemovePlacement(_Connector.AsLMRepresentation());
1222
            
1223
            // 시작 Connector일 경우 첫 Point가 TargetConnector를 찾아야함
1224
            if (IsStart)
1225
            {
1226
                branchConnectorVertices = GetPipeRunVertices(branch.Item2.SPPID.ModelItemId);
1227
                _BranchTargetConnector = FindTargetLMConnector(branchConnectorVertices, points[0][0], points[0][1], points[1][0], points[1][1]);
1228
            }
1229
            // 끝 Conenctor일 경우 마지막 Point가 TargetConnector를 찾아야함
1230
            else
1231
            {
1232
                branchConnectorVertices = GetPipeRunVertices(branch.Item3.SPPID.ModelItemId);
1233
                _BranchTargetConnector = FindTargetLMConnector(branchConnectorVertices,
1234
                    points[points.Count - 1][0],
1235
                    points[points.Count - 1][1],
1236
                    points[points.Count - 2][0],
1237
                    points[points.Count - 2][1]);
1238
            }
1239

    
1240
            for (int i = 0; i < points.Count; i++)
1241
            {
1242
                double[] point = points[i];
1243
                if (i == 0)
1244
                {
1245
                    if (IsStart)
1246
                    {
1247
                        placeRunInputs.AddConnectorTarget(_BranchTargetConnector, point[0], point[1]);
1248
                    }
1249
                    else
1250
                    {
1251
                        if (_SameRunTargetConnector != null)
1252
                            placeRunInputs.AddConnectorTarget(_SameRunTargetConnector, point[0], point[1]);
1253
                        else if (_SameRunTargetSymbol != null)
1254
                            placeRunInputs.AddSymbolTarget(_SameRunTargetSymbol, point[0], point[1]);
1255
                        else
1256
                            placeRunInputs.AddPoint(point[0], point[1]);
1257
                    }
1258
                }
1259
                else if (i == points.Count - 1)
1260
                {
1261
                    if (IsStart)
1262
                    {
1263
                        if (_SameRunTargetConnector != null)
1264
                            placeRunInputs.AddConnectorTarget(_SameRunTargetConnector, point[0], point[1]);
1265
                        else if (_SameRunTargetSymbol != null)
1266
                            placeRunInputs.AddSymbolTarget(_SameRunTargetSymbol, point[0], point[1]);
1267
                        else
1268
                            placeRunInputs.AddPoint(point[0], point[1]);
1269
                    }
1270
                    else
1271
                    {
1272
                        if (_BranchTargetConnector != null)
1273
                        {
1274
                            placeRunInputs.AddConnectorTarget(_BranchTargetConnector, point[0], point[1]);
1275
                        }
1276
                    }
1277
                }
1278
                else
1279
                    placeRunInputs.AddPoint(point[0], point[1]);
1280
            }
1281
            _LMAItem _LMAItem = _placement.PIDCreateItem(lines[0].SPPID.MAPPINGNAME);
1282
            LMConnector _LMConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
1283

    
1284
            if (_LMConnector != null)
1285
            {
1286
                if (_SameRunTargetConnector != null)
1287
                {
1288
                    JoinPipeRun(_LMConnector.ModelItemID, _SameRunTargetConnector.ModelItemID);
1289
                }
1290
                else
1291
                {
1292
                    foreach (var item in lines)
1293
                        item.SPPID.ModelItemId = _LMConnector.ModelItemID;
1294
                }
1295

    
1296
                _LMConnector.Commit();
1297
                ReleaseCOMObjects(_LMConnector);
1298
            }
1299

    
1300
            ReleaseCOMObjects(placeRunInputs);
1301
            ReleaseCOMObjects(_LMAItem);
1302
            if (_BranchTargetConnector != null)
1303
                ReleaseCOMObjects(_BranchTargetConnector);
1304
            if (_SameRunTargetConnector != null)
1305
                ReleaseCOMObjects(_SameRunTargetConnector);
1306
            if (_SameRunTargetSymbol != null)
1307
                ReleaseCOMObjects(_SameRunTargetSymbol);
1308
            foreach (var item in connectorVertices)
1309
                ReleaseCOMObjects(item.Key);
1310
            foreach (var item in branchConnectorVertices)
1311
                ReleaseCOMObjects(item.Key);
1312
        }
1313

    
1314
        /// <summary>
1315
        /// EndBreak 모델링 메서드
1316
        /// </summary>
1317
        /// <param name="endBreak"></param>
1318
        private void EndBreakModeling(EndBreak endBreak)
1319
        {
1320
            object ownerObj = SPPIDUtil.FindObjectByUID(document, endBreak.OWNER);
1321
            LMConnector targetLMConnector = null;
1322
            if (ownerObj !=null && ownerObj.GetType() == typeof(Line))
1323
            {
1324
                Line ownerLine = ownerObj as Line;
1325
                LMLabelPersist _LmLabelPersist = null;
1326
                Dictionary<LMConnector, List<double[]>> connectorVertices = GetPipeRunVertices(ownerLine.SPPID.ModelItemId);
1327

    
1328
                targetLMConnector = FindTargetLMConnectorByPoint(connectorVertices, endBreak.SPPID.ORIGINAL_X, endBreak.SPPID.ORIGINAL_Y);
1329
                
1330
                if (targetLMConnector != null)
1331
                {
1332
                    Array array = new double[] { 0, endBreak.SPPID.ORIGINAL_X, endBreak.SPPID.ORIGINAL_Y };
1333
                    _LmLabelPersist = _placement.PIDPlaceLabel(endBreak.SPPID.MAPPINGNAME, ref array, Rotation: 0, LabeledItem: targetLMConnector.AsLMRepresentation(), IsLeaderVisible: true);
1334
                }
1335

    
1336
                if (_LmLabelPersist != null)
1337
                {
1338
                    _LmLabelPersist.Commit();
1339
                    ReleaseCOMObjects(_LmLabelPersist);
1340
                }
1341
                else
1342
                    RetryEndBreakModeling(endBreak, targetLMConnector);
1343

    
1344
                foreach (var item in connectorVertices)
1345
                    ReleaseCOMObjects(item.Key);
1346

    
1347
            }
1348
            else if (ownerObj != null && ownerObj.GetType() == typeof(Symbol))
1349
            {
1350
                Symbol ownerSymbol = ownerObj as Symbol;
1351
                LMSymbol _LMSymbol = dataSource.GetSymbol(ownerSymbol.SPPID.RepresentationId);
1352

    
1353
                targetLMConnector = null;
1354
                double distance = double.MaxValue;
1355

    
1356
                foreach (LMConnector connector in _LMSymbol.Avoid1Connectors)
1357
                {
1358
                    if (connector.get_ItemStatus() == "Active")
1359
                    {
1360
                        dynamic OID = connector.get_GraphicOID();
1361
                        DependencyObject drawingObject = radApp.ActiveDocument.ActiveSheet.DrawingObjects[OID];
1362
                        Ingr.RAD2D.LineStringGeometry2d lineStringGeometry = drawingObject.GetGeometry() as Ingr.RAD2D.LineStringGeometry2d;
1363
                        int verticesCount = lineStringGeometry.VertexCount;
1364
                        double[] vertices = null;
1365
                        lineStringGeometry.GetVertices(ref verticesCount, ref vertices);
1366
                        for (int i = 0; i < verticesCount; i++)
1367
                        {
1368
                            double x = 0;
1369
                            double y = 0;
1370
                            lineStringGeometry.GetVertex(i + 1, ref x, ref y);
1371

    
1372
                            double result = SPPIDUtil.CalcPointToPointdDistance(x, y, endBreak.SPPID.ORIGINAL_X, endBreak.SPPID.ORIGINAL_Y);
1373
                            if (result < distance)
1374
                            {
1375
                                targetLMConnector = connector;
1376
                                distance = result;
1377
                            }
1378
                        }
1379
                    }
1380
                }
1381

    
1382
                foreach (LMConnector connector in _LMSymbol.Avoid2Connectors)
1383
                {
1384
                    if (connector.get_ItemStatus() == "Active")
1385
                    {
1386
                        dynamic OID = connector.get_GraphicOID();
1387
                        DependencyObject drawingObject = radApp.ActiveDocument.ActiveSheet.DrawingObjects[OID];
1388
                        Ingr.RAD2D.LineStringGeometry2d lineStringGeometry = drawingObject.GetGeometry() as Ingr.RAD2D.LineStringGeometry2d;
1389
                        int verticesCount = lineStringGeometry.VertexCount;
1390
                        double[] vertices = null;
1391
                        lineStringGeometry.GetVertices(ref verticesCount, ref vertices);
1392
                        for (int i = 0; i < verticesCount; i++)
1393
                        {
1394
                            double x = 0;
1395
                            double y = 0;
1396
                            lineStringGeometry.GetVertex(i + 1, ref x, ref y);
1397

    
1398
                            double result = SPPIDUtil.CalcPointToPointdDistance(x, y, endBreak.SPPID.ORIGINAL_X, endBreak.SPPID.ORIGINAL_Y);
1399
                            if (result < distance)
1400
                            {
1401
                                targetLMConnector = connector;
1402
                                distance = result;
1403
                            }
1404
                        }
1405
                    }
1406
                }
1407

    
1408
                if (targetLMConnector != null)
1409
                {
1410
                    LMLabelPersist _LmLabelPersist = null;
1411
                    Array array = new double[] { 0, endBreak.SPPID.ORIGINAL_X, endBreak.SPPID.ORIGINAL_Y };
1412
                    _LmLabelPersist = _placement.PIDPlaceLabel(endBreak.SPPID.MAPPINGNAME, ref array, Rotation: 0, LabeledItem: targetLMConnector.AsLMRepresentation(), IsLeaderVisible: true);
1413
                    if (_LmLabelPersist != null)
1414
                    {
1415
                        _LmLabelPersist.Commit();
1416
                        ReleaseCOMObjects(_LmLabelPersist);
1417
                    }
1418
                    else
1419
                        RetryEndBreakModeling(endBreak, targetLMConnector);
1420
                }
1421
                
1422
                ReleaseCOMObjects(_LMSymbol);
1423
            }
1424

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

    
1428
        /// <summary>
1429
        /// EndBreak 모델링이 실패시 다시 시도하는 메서드
1430
        /// </summary>
1431
        /// <param name="endBreak"></param>
1432
        /// <param name="targetLMConnector"></param>
1433
        private void RetryEndBreakModeling(EndBreak endBreak, LMConnector targetLMConnector)
1434
        {
1435
            bool isZeroLength = Convert.ToBoolean(targetLMConnector.get_IsZeroLength());
1436
            Array array = null;
1437
            LMLabelPersist _LMLabelPersist = null;
1438
            LMConnector _LMConnector = null;
1439
            dynamic OID = targetLMConnector.get_GraphicOID();
1440
            DependencyObject drawingObject = radApp.ActiveDocument.ActiveSheet.DrawingObjects[OID];
1441
            Ingr.RAD2D.LineStringGeometry2d lineStringGeometry = drawingObject.GetGeometry() as Ingr.RAD2D.LineStringGeometry2d;
1442
            int verticesCount = lineStringGeometry.VertexCount;
1443
            PlaceRunInputs placeRunInputs = new PlaceRunInputs();
1444
            _LMAItem _LMAItem = _placement.PIDCreateItem(@"\Piping\Routing\Process Lines\Primary Piping.sym");
1445

    
1446
            if (isZeroLength)
1447
            {
1448
                double[] vertices = null;
1449
                lineStringGeometry.GetVertices(ref verticesCount, ref vertices);
1450
                double x = 0;
1451
                double y = 0;
1452
                lineStringGeometry.GetVertex(1, ref x, ref y);
1453

    
1454
                placeRunInputs.AddSymbolTarget(targetLMConnector.ConnectItem1SymbolObject, x, y);
1455
                placeRunInputs.AddSymbolTarget(targetLMConnector.ConnectItem2SymbolObject, x, y);
1456

    
1457
                _placement.PIDRemovePlacement(targetLMConnector.AsLMRepresentation());
1458
                _LMConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
1459

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

    
1463
                AutoJoinPipeRun(_LMConnector.ModelItemID);
1464
            }
1465
            else
1466
            {
1467
                List<double[]> vertices = new List<double[]>();
1468
                for (int i = 1; i <= verticesCount; i++)
1469
                {
1470
                    double x = 0;
1471
                    double y = 0;
1472
                    lineStringGeometry.GetVertex(i, ref x, ref y);
1473
                    vertices.Add(new double[] { x, y });
1474
                }
1475

    
1476
                for (int i = 0; i < vertices.Count; i++)
1477
                {
1478
                    double[] points = vertices[i];
1479
                    if (i == 0)
1480
                    {
1481
                        if (targetLMConnector.ConnectItem1SymbolObject != null)
1482
                            placeRunInputs.AddSymbolTarget(targetLMConnector.ConnectItem1SymbolObject, points[0], points[1]);
1483
                        else
1484
                            placeRunInputs.AddPoint(points[0], points[1]);
1485
                    }
1486
                    else if (i == vertices.Count - 1)
1487
                    {
1488
                        if (targetLMConnector.ConnectItem2SymbolObject != null)
1489
                            placeRunInputs.AddSymbolTarget(targetLMConnector.ConnectItem2SymbolObject, points[0], points[1]);
1490
                        else
1491
                            placeRunInputs.AddPoint(points[0], points[1]);
1492
                    }
1493
                    else
1494
                        placeRunInputs.AddPoint(points[0], points[1]);
1495
                }
1496

    
1497
                List<Line> lines = SPPIDUtil.FindLinesByModelId(document, targetLMConnector.ModelItemID);
1498
                
1499
                _placement.PIDRemovePlacement(targetLMConnector.AsLMRepresentation());
1500
                _LMConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
1501

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

    
1505
                AutoJoinPipeRun(_LMConnector.ModelItemID);
1506
                foreach (var line in lines)
1507
                    line.SPPID.ModelItemId = _LMConnector.ModelItemID;
1508
            }
1509

    
1510

    
1511
            if (_LMLabelPersist != null)
1512
            {
1513
                _LMLabelPersist.Commit();
1514
                ReleaseCOMObjects(_LMLabelPersist);
1515
            }
1516
            else
1517
            {
1518
                
1519
            }
1520

    
1521
            ReleaseCOMObjects(_LMConnector);
1522
            ReleaseCOMObjects(placeRunInputs);
1523
            ReleaseCOMObjects(_LMAItem);
1524
        }
1525

    
1526
        /// <summary>
1527
        /// FromModelItem을 ToModelItem으로 PipeRunJoin하는 메서드
1528
        /// </summary>
1529
        /// <param name="fromModelItemId"></param>
1530
        /// <param name="toModelItemId"></param>
1531
        private void JoinPipeRun(string fromModelItemId, string toModelItemId)
1532
        {
1533
            LMModelItem modelItem1 = dataSource.GetModelItem(toModelItemId);
1534
            _LMAItem item1 = modelItem1.AsLMAItem();
1535
            LMModelItem modelItem2 = dataSource.GetModelItem(fromModelItemId);
1536
            _LMAItem item2 = modelItem2.AsLMAItem();
1537
            
1538
            // item2가 item1으로 조인
1539
            try
1540
            {
1541
                _placement.PIDJoinRuns(ref item1, ref item2);
1542
                item1.Commit();
1543
                item2.Commit();
1544

    
1545
                List<Line> lines = SPPIDUtil.FindLinesByModelId(document, fromModelItemId);
1546
                foreach (var line in lines)
1547
                    line.SPPID.ModelItemId = toModelItemId;
1548
            }
1549
            catch (Exception ex)
1550
            {
1551
                System.Windows.Forms.MessageBox.Show(ex.Message + "\r\n" + ex.StackTrace);
1552
            }
1553
            finally
1554
            {
1555
                ReleaseCOMObjects(modelItem1);
1556
                ReleaseCOMObjects(item1);
1557
                ReleaseCOMObjects(modelItem2);
1558
                ReleaseCOMObjects(item2);
1559
            }
1560
        }
1561

    
1562
        /// <summary>
1563
        /// PipeRun을 자동으로 Join하는 메서드
1564
        /// </summary>
1565
        /// <param name="modelItemId"></param>
1566
        private void AutoJoinPipeRun(string modelItemId)
1567
        {
1568
            LMModelItem modelItem = dataSource.GetModelItem(modelItemId);
1569
            _LMAItem item = modelItem.AsLMAItem();
1570
            try
1571
            {
1572
                string modelitemID = item.Id;
1573
                _placement.PIDAutoJoin(item, AutoJoinEndConstants.autoJoin_Both, ref item);
1574
                string afterModelItemID = item.Id;
1575
                
1576
                if (modelitemID != afterModelItemID)
1577
                {
1578
                    List<Line> lines = SPPIDUtil.FindLinesByModelId(document, modelitemID);
1579
                    foreach (var line in lines)
1580
                        line.SPPID.ModelItemId = afterModelItemID;
1581
                }
1582
                item.Commit();
1583
            }
1584
            catch (Exception ex)
1585
            {
1586
                System.Windows.Forms.MessageBox.Show(ex.Message + "\r\n" + ex.StackTrace);
1587
            }
1588
            finally
1589
            {
1590
                ReleaseCOMObjects(modelItem);
1591
                ReleaseCOMObjects(item);
1592
            }
1593
        }
1594

    
1595
        /// <summary>
1596
        /// LineRun에 있는 Line들을 Join하는 진입 메서드
1597
        /// </summary>
1598
        /// <param name="run"></param>
1599
        private void JoinRunLine(LineRun run)
1600
        {
1601
            string modelItemId = string.Empty;
1602
            foreach (var item in run.RUNITEMS)
1603
            {
1604
                if (item.GetType() == typeof(Line))
1605
                {
1606
                    Line line = item as Line;
1607
                    AutoJoinPipeRun(line.SPPID.ModelItemId);
1608
                    modelItemId = line.SPPID.ModelItemId;
1609

    
1610
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetProgress, ++CurrentCount);
1611
                }
1612
            }
1613
        }
1614

    
1615
        /// <summary>
1616
        /// PipeRun의 좌표를 가져오는 메서드
1617
        /// </summary>
1618
        /// <param name="modelId"></param>
1619
        /// <returns></returns>
1620
        private Dictionary<LMConnector, List<double[]>> GetPipeRunVertices(string modelId)
1621
        {
1622
            Dictionary<LMConnector, List<double[]>> connectorVertices = new Dictionary<LMConnector, List<double[]>>();
1623
            LMModelItem modelItem = dataSource.GetModelItem(modelId);
1624

    
1625
            if (modelItem != null)
1626
            {
1627
                foreach (LMRepresentation rep in modelItem.Representations)
1628
                {
1629
                    if (rep.Attributes["RepresentationType"].get_Value() == "Connector" && rep.Attributes["ItemStatus"].get_Value() == "Active")
1630
                    {
1631
                        LMConnector _LMConnector = dataSource.GetConnector(rep.Id);
1632
                        connectorVertices.Add(_LMConnector, new List<double[]>());
1633
                        dynamic OID = rep.get_GraphicOID();
1634
                        DependencyObject drawingObject = radApp.ActiveDocument.ActiveSheet.DrawingObjects[OID];
1635
                        Ingr.RAD2D.LineStringGeometry2d lineStringGeometry = drawingObject.GetGeometry() as Ingr.RAD2D.LineStringGeometry2d;
1636
                        int verticesCount = lineStringGeometry.VertexCount;
1637
                        double[] vertices = null;
1638
                        lineStringGeometry.GetVertices(ref verticesCount, ref vertices);
1639
                        for (int i = 0; i < verticesCount; i++)
1640
                        {
1641
                            double x = 0;
1642
                            double y = 0;
1643
                            lineStringGeometry.GetVertex(i + 1, ref x, ref y);
1644
                            connectorVertices[_LMConnector].Add(new double[] { Math.Round(x, 10), Math.Round(y, 10) });
1645
                        }
1646
                    }
1647
                }
1648

    
1649
                ReleaseCOMObjects(modelItem);
1650
            }
1651

    
1652
            return connectorVertices;
1653
        }
1654

    
1655
        /// <summary>
1656
        /// 좌표로 PipeRun의 Connector중에 어느 Connector에 가까운지/붙을지 가져오는 메서드 - 두점으로 라인의 교차점을 기준으로 구함
1657
        /// </summary>
1658
        /// <param name="connectorVertices"></param>
1659
        /// <param name="connX"></param>
1660
        /// <param name="connY"></param>
1661
        /// <param name="x2"></param>
1662
        /// <param name="y2"></param>
1663
        /// <returns></returns>
1664
        private LMConnector FindTargetLMConnector(Dictionary<LMConnector, List<double[]>> connectorVertices, double connX, double connY, double x2, double y2)
1665
        {
1666
            double length = double.MaxValue;
1667
            LMConnector targetConnector = null;
1668
            foreach (var item in connectorVertices)
1669
            {
1670
                List<double[]> points = item.Value;
1671
                for (int i = 0; i < points.Count - 1; i++)
1672
                {
1673
                    double[] point1 = points[i];
1674
                    double[] point2 = points[i + 1];
1675

    
1676
                    double maxLineX = Math.Max(point1[0], point2[0]);
1677
                    double minLineX = Math.Min(point1[0], point2[0]);
1678
                    double maxLineY = Math.Max(point1[1], point2[1]);
1679
                    double minLineY = Math.Min(point1[1], point2[1]);
1680

    
1681
                    SlopeType slope = SPPIDUtil.CalcSlope(minLineX, minLineY, maxLineX, maxLineY);
1682

    
1683
                    // 두직선의 교차점
1684
                    double[] crossingPoint = SPPIDUtil.CalcLineCrossingPoint(connX, connY, x2, y2, point1[0], point1[1], point2[0], point2[1]);
1685
                    if (crossingPoint != null)
1686
                    {
1687
                        double distance = SPPIDUtil.CalcPointToPointdDistance(connX, connY, crossingPoint[0], crossingPoint[1]);
1688
                        if (length >= distance)
1689
                        {
1690
                            if (slope == SlopeType.Slope &&
1691
                                minLineX <= crossingPoint[0] && maxLineX >= crossingPoint[0] &&
1692
                                minLineY <= crossingPoint[1] && maxLineY >= crossingPoint[1])
1693
                            {
1694
                                targetConnector = item.Key;
1695
                                length = distance;
1696
                            }
1697
                            else if (slope == SlopeType.HORIZONTAL &&
1698
                                minLineX <= crossingPoint[0] && maxLineX >= crossingPoint[0])
1699
                            {
1700
                                targetConnector = item.Key;
1701
                                length = distance;
1702
                            }
1703
                            else if (slope == SlopeType.VERTICAL &&
1704
                               minLineY <= crossingPoint[1] && maxLineY >= crossingPoint[1])
1705
                            {
1706
                                targetConnector = item.Key;
1707
                                length = distance;
1708
                            }
1709
                        }
1710
                    }
1711
                }
1712

    
1713

    
1714
            }
1715

    
1716
            if (targetConnector == null)
1717
            {
1718
                foreach (var item in connectorVertices)
1719
                {
1720
                    List<double[]> points = item.Value;
1721
                    foreach (var point in points)
1722
                    {
1723
                        double distance = SPPIDUtil.CalcPointToPointdDistance(connX, connY, point[0], point[1]);
1724
                        if (length >= distance)
1725
                        {
1726
                            targetConnector = item.Key;
1727
                            length = distance;
1728
                        }
1729
                    }
1730
                }
1731

    
1732
            }
1733

    
1734
            return targetConnector;
1735
        }
1736

    
1737
        /// <summary>
1738
        /// 좌표로 PipeRun의 Connector중에 어느 Connector에 가까운지/붙을지 가져오는 메서드 - 한점으로 제일 가까운 기준으로 구함(단순)
1739
        /// </summary>
1740
        /// <param name="connectorVertices"></param>
1741
        /// <param name="connX"></param>
1742
        /// <param name="connY"></param>
1743
        /// <returns></returns>
1744
        private LMConnector FindTargetLMConnectorByPoint(Dictionary<LMConnector, List<double[]>> connectorVertices, double connX, double connY)
1745
        {
1746
            double length = double.MaxValue;
1747
            LMConnector targetConnector = null;
1748
            foreach (var item in connectorVertices)
1749
            {
1750
                List<double[]> points = item.Value;
1751

    
1752
                foreach (double[] point in points)
1753
                {
1754
                    double distance = SPPIDUtil.CalcPointToPointdDistance(point[0], point[1], connX, connY);
1755
                    if (length >= distance)
1756
                    {
1757
                        targetConnector = item.Key;
1758
                        length = distance;
1759
                    }
1760
                }
1761
            }
1762

    
1763
            return targetConnector;
1764
        }
1765

    
1766
        /// <summary>
1767
        /// 좌표로 PipeRun의 Connector중에 어느 Connector에 가까운지/붙을지 가져오는 메서드 - 조건에 안맞아서 못찾을시 제일 가까운 점으로 가져오는 방식
1768
        /// </summary>
1769
        /// <param name="connectorVertices"></param>
1770
        /// <param name="connX"></param>
1771
        /// <param name="connY"></param>
1772
        /// <returns></returns>
1773
        private LMConnector FindTargetLMConnectorForLabel(Dictionary<LMConnector, List<double[]>> connectorVertices, double connX, double connY)
1774
        {
1775
            double length = double.MaxValue;
1776
            LMConnector targetConnector = null;
1777
            foreach (var item in connectorVertices)
1778
            {
1779
                List<double[]> points = item.Value;
1780
                for (int i = 0; i < points.Count - 1; i++)
1781
                {
1782
                    double[] point1 = points[i];
1783
                    double[] point2 = points[i + 1];
1784
                    double x1 = Math.Min(point1[0], point2[0]);
1785
                    double y1 = Math.Min(point1[1], point2[1]);
1786
                    double x2 = Math.Max(point1[0], point2[0]);
1787
                    double y2 = Math.Max(point1[1], point2[1]);
1788

    
1789
                    if ((x1 <= connX && x2 >= connX) ||
1790
                        (y1 <= connY && y2 >= connY))
1791
                    {
1792
                        double distance = SPPIDUtil.CalcPointToPointdDistance(point1[0], point1[1], connX, connY);
1793
                        if (length >= distance)
1794
                        {
1795
                            targetConnector = item.Key;
1796
                            length = distance;
1797
                        }
1798

    
1799
                        distance = SPPIDUtil.CalcPointToPointdDistance(point2[0], point2[1], connX, connY);
1800
                        if (length >= distance)
1801
                        {
1802
                            targetConnector = item.Key;
1803
                            length = distance;
1804
                        }
1805
                    }
1806
                }
1807
            }
1808

    
1809
            // 못찾았을때.
1810
            length = double.MaxValue;
1811
            if (targetConnector == null)
1812
            {
1813
                foreach (var item in connectorVertices)
1814
                {
1815
                    List<double[]> points = item.Value;
1816

    
1817
                    foreach (double[] point in points)
1818
                    {
1819
                        double distance = SPPIDUtil.CalcPointToPointdDistance(point[0], point[1], connX, connY);
1820
                        if (length >= distance)
1821
                        {
1822
                            targetConnector = item.Key;
1823
                            length = distance;
1824
                        }
1825
                    }
1826
                }
1827
            }
1828

    
1829
            return targetConnector;
1830
        }
1831

    
1832
        /// <summary>
1833
        /// Line Number Symbol을 실제로 Modeling하는 메서드
1834
        /// </summary>
1835
        /// <param name="lineNumber"></param>
1836
        private void LineNumberModeling(LineNumber lineNumber)
1837
        {
1838
            Line line = SPPIDUtil.FindObjectByUID(document, lineNumber.CONNLINE) as Line;
1839
            Dictionary<LMConnector, List<double[]>> connectorVertices = GetPipeRunVertices(line.SPPID.ModelItemId);
1840
            LMConnector connectedLMConnector = FindTargetLMConnectorForLabel(connectorVertices, lineNumber.SPPID.ORIGINAL_X, lineNumber.SPPID.ORIGINAL_Y);
1841
            if (connectedLMConnector != null)
1842
            {
1843
                double x = 0;
1844
                double y = 0;
1845
                CalcLabelLocation(ref x, ref y, lineNumber.SPPID.ORIGINAL_X, lineNumber.SPPID.ORIGINAL_Y, lineNumber.SPPIDLabelLocation, _ETCSetting.LineNumberLocation);
1846

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

    
1850
                foreach (var item in connectorVertices)
1851
                    ReleaseCOMObjects(item.Key);
1852
                if (_LmLabelPresist != null)
1853
                {
1854
                    _LmLabelPresist.Commit();
1855
                    lineNumber.SPPID.RepresentationId = _LmLabelPresist.AsLMRepresentation().Id;
1856
                    ReleaseCOMObjects(_LmLabelPresist);
1857
                }
1858
                else
1859
                {
1860

    
1861
                }
1862
            }
1863

    
1864
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetProgress, ++CurrentCount);
1865
        }
1866

    
1867
        /// <summary>
1868
        /// Line Number 기준으로 모든 Item에 Line Number의 Attribute Input
1869
        /// </summary>
1870
        /// <param name="lineNumber"></param>
1871
        private void InputLineNumberAttribute(LineNumber lineNumber)
1872
        {
1873
            foreach (LineRun run in lineNumber.RUNS)
1874
            {
1875
                foreach (var item in run.RUNITEMS)
1876
                {
1877
                    if (item.GetType() == typeof(Symbol))
1878
                    {
1879
                        Symbol symbol = item as Symbol;
1880
                        LMSymbol _LMSymbol = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
1881
                        LMModelItem _LMModelItem = _LMSymbol.ModelItemObject;
1882

    
1883
                        if (_LMModelItem != null && _LMModelItem.get_ItemStatus() == "Active")
1884
                        {
1885
                            foreach (var attribute in lineNumber.ATTRIBUTES)
1886
                            {
1887
                                LineNumberMapping mapping = document.LineNumberMappings.Find(x => x.UID == attribute.UID);
1888
                                if (mapping != null)
1889
                                {
1890
                                    LMAAttribute _LMAAttribute = _LMModelItem.Attributes[mapping.SPPIDATTRIBUTENAME];
1891
                                    if (_LMAAttribute != null)
1892
                                    {
1893
                                        if (DBNull.Value.Equals(_LMAAttribute.get_Value()))
1894
                                            _LMAAttribute.set_Value(attribute.VALUE);
1895
                                        else if (_LMAAttribute.get_Value() != attribute.VALUE)
1896
                                            _LMAAttribute.set_Value(attribute.VALUE);
1897
                                    }
1898
                                }
1899
                            }
1900
                            _LMModelItem.Commit();
1901
                        }
1902
                        if (_LMModelItem != null)
1903
                            ReleaseCOMObjects(_LMModelItem);
1904
                        if (_LMSymbol != null)
1905
                            ReleaseCOMObjects(_LMSymbol);
1906
                    }
1907
                    else if (item.GetType() == typeof(Line))
1908
                    {
1909
                        Line line = item as Line;
1910
                        if (line != null)
1911
                        {
1912
                            LMModelItem _LMModelItem = dataSource.GetModelItem(line.SPPID.ModelItemId);
1913
                            if (_LMModelItem != null && _LMModelItem.get_ItemStatus() == "Active")
1914
                            {
1915
                                foreach (var attribute in lineNumber.ATTRIBUTES)
1916
                                {
1917
                                    LineNumberMapping mapping = document.LineNumberMappings.Find(x => x.UID == attribute.UID);
1918
                                    if (mapping != null)
1919
                                    {
1920
                                        LMAAttribute _LMAAttribute = _LMModelItem.Attributes[mapping.SPPIDATTRIBUTENAME];
1921
                                        if (_LMAAttribute != null)
1922
                                        {
1923
                                            if (DBNull.Value.Equals(_LMAAttribute.get_Value()))
1924
                                                _LMAAttribute.set_Value(attribute.VALUE);
1925
                                            else if (_LMAAttribute.get_Value() != attribute.VALUE)
1926
                                                _LMAAttribute.set_Value(attribute.VALUE);
1927
                                            
1928
                                        }
1929
                                    }
1930
                                }
1931
                                _LMModelItem.Commit();
1932
                            }
1933
                            if (_LMModelItem != null)
1934
                                ReleaseCOMObjects(_LMModelItem);
1935
                        }
1936
                    }
1937
                }
1938
            }
1939

    
1940
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetProgress, ++CurrentCount);
1941
        }
1942

    
1943
        /// <summary>
1944
        /// Symbol Attribute 입력 메서드
1945
        /// </summary>
1946
        /// <param name="item"></param>
1947
        private void InputSymbolAttribute(object targetItem, List<BaseModel.Attribute> targetAttributes)
1948
        {
1949
            try
1950
            {
1951
                // Object 아이템이 Symbol일 경우 Equipment일 경우 
1952
                string sRep = null;
1953
                if (targetItem.GetType() == typeof(Symbol))
1954
                    sRep = ((Symbol)targetItem).SPPID.RepresentationId;
1955
                else if (targetItem.GetType() == typeof(Equipment))
1956
                    sRep = ((Equipment)targetItem).SPPID.RepresentationId;
1957
                    
1958
                if (!string.IsNullOrEmpty(sRep))
1959
                {
1960
                    LMSymbol _LMSymbol = dataSource.GetSymbol(sRep);
1961
                    LMModelItem _LMModelItem = _LMSymbol.ModelItemObject;
1962
                    LMAAttributes _Attributes = _LMModelItem.Attributes;
1963

    
1964
                    foreach (var item in targetAttributes)
1965
                    {
1966
                        AttributeMapping mapping = document.AttributeMappings.Find(x => x.UID == item.UID);
1967
                        if (mapping != null && !string.IsNullOrEmpty(item.VALUE) && item.VALUE != "None")
1968
                        {
1969
                            LMAAttribute _Attribute = _Attributes[mapping.SPPIDATTRIBUTENAME];
1970
                            if (_Attribute != null)
1971
                                _Attribute.set_Value(item.VALUE);
1972
                        }
1973
                    }
1974
                    _LMModelItem.Commit();
1975
                    
1976
                    ReleaseCOMObjects(_Attributes);
1977
                    ReleaseCOMObjects(_LMModelItem);
1978
                    ReleaseCOMObjects(_LMSymbol);
1979
                }
1980
            }
1981
            catch (Exception ex)
1982
            {
1983
                System.Windows.Forms.MessageBox.Show(ex.Message + "\r\n" + ex.StackTrace);
1984
            }
1985

    
1986
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetProgress, ++CurrentCount);
1987
        }
1988

    
1989
        /// <summary>
1990
        /// Text Modeling - Association일 경우는 Text대신 해당 맵핑된 Symbol로 모델링
1991
        /// </summary>
1992
        /// <param name="text"></param>
1993
        private void TextModeling(Text text)
1994
        {
1995
            LMSymbol _LMSymbol = null;
1996
            try
1997
            {
1998
                //if (text.ASSOCIATION && !string.IsNullOrEmpty(text.OWNER) && text.OWNER != "None")
1999
                if (text.ASSOCIATION)
2000
                {
2001
                    object owner = SPPIDUtil.FindObjectByUID(document, text.OWNER);
2002
                    if (owner.GetType() == typeof(Symbol))
2003
                    {
2004
                        Symbol symbol = owner as Symbol;
2005
                        _LMSymbol = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
2006
                        if (_LMSymbol != null)
2007
                        {
2008
                            Association association = symbol.ASSOCIATIONS.Find(x => x.VALUE == text.UID);
2009
                            List<BaseModel.Attribute> attributes = symbol.ATTRIBUTES.FindAll(x => x.ATTRIBUTETYPE == association.TYPE);
2010
                            AttributeMapping mapping = null;
2011
                            foreach (var attribute in attributes)
2012
                            {
2013
                                if (string.IsNullOrEmpty(attribute.VALUE) || attribute.VALUE == "None")
2014
                                    continue;
2015

    
2016
                                 mapping = document.AttributeMappings.Find(x => x.UID == attribute.UID && !string.IsNullOrEmpty(x.SPPIDSYMBOLNAME));
2017
                                if (mapping != null)
2018
                                    break;  
2019
                            }
2020

    
2021
                            if (mapping != null)
2022
                            {
2023
                                double x = 0;
2024
                                double y = 0;
2025

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

    
2029
                                LMLabelPersist _LMLabelPersist = _placement.PIDPlaceLabel(mapping.SPPIDSYMBOLNAME, ref array, Rotation: text.ANGLE, LabeledItem: _LMSymbol.AsLMRepresentation(), IsLeaderVisible: mapping.LeaderLine);
2030
                                if (_LMLabelPersist!=null)
2031
                                {
2032
                                    _LMLabelPersist.Commit();
2033
                                    ReleaseCOMObjects(_LMLabelPersist);
2034
                                }
2035
                            }
2036
                        }
2037
                    }
2038
                    else if (owner.GetType() == typeof(Line))
2039
                    {
2040

    
2041
                    }
2042
                }
2043
                else
2044
                {
2045
                    LMItemNote _LMItemNote = null;
2046
                    LMAAttribute _LMAAttribute = null;
2047

    
2048
                    double x = 0;
2049
                    double y = 0;
2050

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

    
2053
                    _LMSymbol = _placement.PIDPlaceSymbol(text.SPPID.MAPPINGNAME, x, y);
2054
                    _LMSymbol.Commit();
2055
                    _LMItemNote = _placement.PIDDataSource.GetItemNote(_LMSymbol.ModelItemID);
2056
                    _LMItemNote.Commit();
2057
                    _LMAAttribute = _LMItemNote.Attributes["Note.Body"];
2058
                    _LMAAttribute.set_Value(text.VALUE);
2059
                    _LMItemNote.Commit();
2060

    
2061
                    if (_LMAAttribute != null)
2062
                        ReleaseCOMObjects(_LMAAttribute);
2063
                    if (_LMItemNote != null)
2064
                        ReleaseCOMObjects(_LMItemNote);
2065
                }
2066
            }
2067
            catch (Exception ex)
2068
            {
2069

    
2070
            }
2071
            finally
2072
            {
2073
                if (_LMSymbol != null)
2074
                    ReleaseCOMObjects(_LMSymbol);
2075
            }
2076

    
2077
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetProgress, ++CurrentCount);
2078
        }
2079

    
2080
        /// <summary>
2081
        /// Note Modeling
2082
        /// </summary>
2083
        /// <param name="note"></param>
2084
        private void NoteModeling(Note note)
2085
        {
2086
            LMSymbol _LMSymbol = null;
2087
            LMItemNote _LMItemNote = null;
2088
            LMAAttribute _LMAAttribute = null;
2089

    
2090
            try
2091
            {
2092
                double x = 0;
2093
                double y = 0;
2094

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

    
2097
                _LMSymbol = _placement.PIDPlaceSymbol(note.SPPID.MAPPINGNAME, x, y);
2098
                _LMSymbol.Commit();
2099
                _LMItemNote = _placement.PIDDataSource.GetItemNote(_LMSymbol.ModelItemID);
2100
                _LMItemNote.Commit();
2101
                _LMAAttribute = _LMItemNote.Attributes["Note.Body"];
2102
                _LMAAttribute.set_Value(note.VALUE);
2103
                _LMItemNote.Commit();
2104
            }
2105
            catch (Exception ex)
2106
            {
2107

    
2108
            }
2109
            finally
2110
            {
2111
                if (_LMAAttribute != null)
2112
                    ReleaseCOMObjects(_LMAAttribute);
2113
                if (_LMItemNote != null)
2114
                    ReleaseCOMObjects(_LMItemNote);
2115
                if (_LMSymbol != null)
2116
                    ReleaseCOMObjects(_LMSymbol);
2117
            }
2118

    
2119
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetProgress, ++CurrentCount);
2120
        }
2121

    
2122
        /// <summary>
2123
        /// Label의 좌표를 구하는 메서드(ID2 기준의 좌표 -> SPPID 좌표)
2124
        /// </summary>
2125
        /// <param name="x"></param>
2126
        /// <param name="y"></param>
2127
        /// <param name="originX"></param>
2128
        /// <param name="originY"></param>
2129
        /// <param name="SPPIDLabelLocation"></param>
2130
        /// <param name="location"></param>
2131
        private void CalcLabelLocation(ref double x, ref double y, double originX, double originY, SPPIDLabelLocationInfo SPPIDLabelLocation, Location location)
2132
        {
2133
            if (location == Location.None)
2134
            {
2135
                x = originX;
2136
                y = originY;
2137
            }
2138
            else
2139
            {
2140
                if (location.HasFlag(Location.Center))
2141
                {
2142
                    x = (SPPIDLabelLocation.X1 + SPPIDLabelLocation.X2) / 2;
2143
                    y = (SPPIDLabelLocation.Y1 + SPPIDLabelLocation.Y2) / 2;
2144
                }
2145

    
2146
                if (location.HasFlag(Location.Left))
2147
                    x = SPPIDLabelLocation.X1;
2148
                else if (location.HasFlag(Location.Right))
2149
                    x = SPPIDLabelLocation.X2;
2150

    
2151
                if (location.HasFlag(Location.Down))
2152
                    y = SPPIDLabelLocation.Y1;
2153
                else if (location.HasFlag(Location.Up))
2154
                    y = SPPIDLabelLocation.Y2;
2155
            }
2156
        }
2157

    
2158
        /// <summary>
2159
        /// ComObject를 Release
2160
        /// </summary>
2161
        /// <param name="objVars"></param>
2162
        public void ReleaseCOMObjects(params object[] objVars)
2163
        {
2164
            int intNewRefCount = 0;
2165
            foreach (object obj in objVars)
2166
            {
2167
                if (!Information.IsNothing(obj) && System.Runtime.InteropServices.Marshal.IsComObject(obj))
2168
                    intNewRefCount = intNewRefCount + System.Runtime.InteropServices.Marshal.FinalReleaseComObject(obj);
2169
            }
2170
        }
2171
    }
2172
}
클립보드 이미지 추가 (최대 크기: 500 MB)