프로젝트

일반

사용자정보

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

hytos / DTI_PID / SPPIDConverter / AutoModeling.cs @ de97eaaa

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

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

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

    
38
        public string DocumentLabelText { get; set; }
39

    
40
        int CurrentCount;
41
        List <Tuple<string, Line, Line>> BranchLines = new List<Tuple<string, Line, Line>>();
42

    
43
        public AutoModeling(SPPID_Document document, dynamic application, Ingr.RAD2D.Application radApp)
44
        {
45
            this.document = document;
46
            this.application = application;
47
            this.radApp = radApp;
48
            this._ETCSetting = ETCSetting.GetInstance();
49
        }
50

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

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

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

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

    
86
        private void SetSystemEditingCommand(bool value)
87
        {
88
            foreach (var item in radApp.Commands)
89
            {
90
                if (item.Argument == "SystemEditingCmd.SystemEditing")
91
                {
92
                    if (item.Checked != value)
93
                    {
94
                        radApp.RunMacro("systemeditingcmd.dll");
95
                        break;
96
                    }
97

    
98
                }
99
            }
100
        }
101

    
102
        /// <summary>
103
        /// 도면 단위당 실행되는 메서드
104
        /// </summary>
105
        public void Run()
106
        {
107
            try
108
            {
109
                _placement = new Placement();
110
                dataSource = _placement.PIDDataSource;
111

    
112
                CreateDocument();
113

    
114
                if (DocumentCoordinateCorrection())
115
                {
116
                    int AllCount = ClacProgressCount();
117

    
118
                    SplashScreenManager.ShowForm(typeof(SPPIDSplashScreen), true, true);
119
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetAllStep, AllCount);
120
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetDocumentName, DocumentLabelText);
121

    
122
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Priority Symbol Modeling");
123
                    List<Symbol> prioritySymbols = GetPrioritySymbol();
124
                    foreach (var item in prioritySymbols)
125
                        SymbolModelingByPriority(item);
126

    
127
                    // Equipment Modeling
128
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Equipments Modeling");
129
                    foreach (Equipment equipment in document.Equipments)
130
                        EquipmentModeling(equipment);
131

    
132
                    // LineRun Symbol Modeling
133
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Symbols Modeling");
134
                    foreach (LineNumber lineNumber in document.LINENUMBERS)
135
                        foreach (LineRun run in lineNumber.RUNS)
136
                            SymbolModelingByRun(run);
137
                    // TrimLineRun Symbol Modeling
138
                    foreach (TrimLine trimLine in document.TRIMLINES)
139
                        foreach (LineRun run in trimLine.RUNS)
140
                            SymbolModelingByRun(run);
141

    
142
                    // LineRun Line Modeling
143
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Lines Modeling");
144
                    foreach (LineNumber lineNumber in document.LINENUMBERS)
145
                        foreach (LineRun run in lineNumber.RUNS)
146
                            LineModelingByRun(run);
147
                    // TrimLineRun Line Modeling
148
                    foreach (TrimLine trimLine in document.TRIMLINES)
149
                        foreach (LineRun run in trimLine.RUNS)
150
                            LineModelingByRun(run);
151

    
152
                    // Branch Line Modeling
153
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Branch Lines Modeling");
154
                    foreach (var item in BranchLines)
155
                        BranchLineModeling(item);
156

    
157
                    // EndBreak Modeling
158
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "EndBreaks Modeling");
159
                    foreach (var item in document.EndBreaks)
160
                        EndBreakModeling(item);
161

    
162
                    // SpecBreak Modeling
163
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "SpecBreaks Modeling");
164
                    foreach (var item in document.SpecBreaks)
165
                        SpecBreakModeling(item);
166

    
167
                    // LineNumber Modeling
168
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "LineNumbers Modeling");
169
                    foreach (var item in document.LINENUMBERS)
170
                        LineNumberModeling(item);
171

    
172
                    // LineNumber Modeling
173
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Flow Mark Modeling");
174
                    foreach (var item in document.LINES)
175
                        FlowMarkModeling(item);
176

    
177
                    // Note Modeling
178
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Notes Modeling");
179
                    foreach (var item in document.NOTES)
180
                        NoteModeling(item);
181

    
182
                    // Text Modeling
183
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Texts Modeling");
184
                    foreach (var item in document.TEXTINFOS)
185
                        TextModeling(item);
186

    
187
                    // LineRun Line Join
188
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Join LineRuns");
189
                    foreach (LineNumber lineNumber in document.LINENUMBERS)
190
                        foreach (LineRun run in lineNumber.RUNS)
191
                            JoinRunLine(run);
192
                    // TrimLineRun Line Join
193
                    foreach (TrimLine trimLine in document.TRIMLINES)
194
                        foreach (LineRun run in trimLine.RUNS)
195
                            JoinRunLine(run);
196

    
197
                    // Input LineNumber Attribute
198
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Set Lines Attribute");
199
                    foreach (var item in document.LINENUMBERS)
200
                        InputLineNumberAttribute(item);
201

    
202
                    // Input Symbol Attribute
203
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Set Symbols Attribute");
204
                    foreach (var item in document.SYMBOLS)
205
                        InputSymbolAttribute(item, item.ATTRIBUTES);
206

    
207
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Labels Modeling");
208
                    foreach (var item in document.SYMBOLS)
209
                        LabelSymbolModeling(item);
210

    
211
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetProgress, AllCount);
212
                }
213
            }
214
            catch (Exception ex)
215
            {
216
                System.Windows.Forms.MessageBox.Show(ex.Message + "\r\n" + ex.StackTrace);
217
            }
218
            finally
219
            {
220
                application.ActiveWindow.Fit();
221

    
222
                if (radApp.ActiveDocument != null)
223
                {
224
                    //radApp.ActiveDocument.Save();
225
                    //radApp.ActiveDocument.SaveOnClose = false;
226
                    //radApp.ActiveDocument.Close(false);
227

    
228
                    ReleaseCOMObjects(newDrawing);
229
                }
230

    
231
                ReleaseCOMObjects(dataSource);
232
                ReleaseCOMObjects(_placement);
233

    
234
                //SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.ClearParent, null);
235
                SplashScreenManager.CloseForm(false);
236
            }
237
        }
238

    
239
        /// <summary>
240
        /// 도면 생성 메서드
241
        /// </summary>
242
        private void CreateDocument()
243
        {
244
            string drawingName = document.DrawingName;
245
            string drawingNumber = document.DrawingNumber;
246

    
247
            GetDrawingNameAndNumber(ref drawingName, ref drawingNumber);
248

    
249
            newDrawing = application.Drawings.Add(document.Unit, document.Template, drawingNumber, drawingName);
250
            application.ActiveWindow.Fit();
251
            Thread.Sleep(1000);
252
            application.ActiveWindow.Zoom = 2000;
253
            Thread.Sleep(2000);
254

    
255
            
256
        }
257

    
258
        /// <summary>
259
        /// DrawingName, DrawingNumber를 확인하여 중복이 있으면 _1을 붙이고 +1씩 한다.
260
        /// </summary>
261
        /// <param name="drawingName"></param>
262
        /// <param name="drawingNumber"></param>
263
        private void GetDrawingNameAndNumber(ref string drawingName, ref string drawingNumber)
264
        {
265
            LMDrawings drawings = new LMDrawings();
266
            drawings.Collect(dataSource);
267

    
268
            List<string> drawingNameList = new List<string>();
269
            List<string> drawingNumberList = new List<string>();
270

    
271
            foreach (LMDrawing item in drawings)
272
            {
273
                drawingNameList.Add(item.Attributes["Name"].get_Value().ToString());
274
                drawingNumberList.Add(item.Attributes["DrawingNumber"].get_Value().ToString());
275
            }
276

    
277
            int nameLength = drawingName.Length;
278
            while (drawingNameList.Contains(drawingName))
279
            {
280
                if (nameLength == drawingName.Length)
281
                    drawingName += "-1";
282
                else
283
                {
284
                    int index = Convert.ToInt32(drawingName.Remove(0, nameLength + 1));
285
                    drawingName = drawingName.Substring(0, nameLength + 1);
286
                    drawingName += ++index;
287
                }
288
            }
289

    
290
            int numberLength = drawingNumber.Length;
291
            while (drawingNameList.Contains(drawingNumber))
292
            {
293
                if (numberLength == drawingNumber.Length)
294
                    drawingNumber += "-1";
295
                else
296
                {
297
                    int index = Convert.ToInt32(drawingNumber.Remove(0, numberLength + 1));
298
                    drawingNumber = drawingNumber.Substring(0, numberLength + 1);
299
                    drawingNumber += ++index;
300
                }
301
            }
302

    
303
            ReleaseCOMObjects(drawings);
304
        }
305

    
306
        /// <summary>
307
        /// 도면 크기 구하는 메서드
308
        /// </summary>
309
        /// <returns></returns>
310
        private bool DocumentCoordinateCorrection()
311
        {
312
            if (Settings.Default.DrawingX != 0 && Settings.Default.DrawingY != 0)
313
            {
314
                document.SetSPPIDLocation(Settings.Default.DrawingX, Settings.Default.DrawingY);
315
                document.CoordinateCorrection();
316
                return true;
317
            }
318
            else
319
                return false;
320
        }
321

    
322
        /// <summary>
323
        /// 라인을 Run 단위로 모델링하는 진입 메서드
324
        /// </summary>
325
        /// <param name="run"></param>
326
        private void LineModelingByRun(LineRun run)
327
        {
328
            Line prevLine = null;
329
            List<Line> lines = new List<Line>();
330
            foreach (var item in run.RUNITEMS)
331
            {
332
                // Line일 경우
333
                if (item.GetType() == typeof(Line))
334
                {
335
                    Line line = item as Line;
336
                    if (prevLine == null)
337
                        lines.Add(line);
338
                    else if (prevLine != null)
339
                    {
340
                        if (prevLine.SPPID.MAPPINGNAME == line.SPPID.MAPPINGNAME)
341
                            lines.Add(line);
342
                        else
343
                        {
344
                            if (lines.Count > 0)
345
                            {
346
                                LineModeling(lines);
347
                                lines.Clear();
348
                            }
349
                            lines.Add(line);
350
                        }
351
                    }
352

    
353
                    prevLine = line;
354

    
355
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetProgress, ++CurrentCount);
356
                }
357
                // Symbol 일 경우
358
                else if (item.GetType() == typeof(Symbol))
359
                {
360
                    if (lines.Count > 0)
361
                    {
362
                        LineModeling(lines);
363
                        lines.Clear();
364
                    }
365
                }
366
            }
367

    
368
            if (lines.Count > 0)
369
                LineModeling(lines);
370
        }
371

    
372
        /// <summary>
373
        /// 심볼을 Run 단위로 모델링하는 진입 메서드
374
        /// </summary>
375
        /// <param name="run"></param>
376
        private void SymbolModelingByRun(LineRun run)
377
        {
378
            // 양끝 Symbol 검사 후 Line이 나올때까지만 Symbol Modeling
379
            if (run.RUNITEMS.Count > 0)
380
            {
381
                if (run.RUNITEMS[0].GetType() == typeof(Symbol))
382
                    SymbolModelingByRunStart(run.RUNITEMS[0] as Symbol, run);
383

    
384
                if (run.RUNITEMS[run.RUNITEMS.Count - 1].GetType() == typeof(Symbol))
385
                    SymbolModelingByRunEnd(run.RUNITEMS[run.RUNITEMS.Count - 1] as Symbol, run);
386
            }
387

    
388
            Symbol targetSymbol = null;
389
            foreach (var item in run.RUNITEMS)
390
            {
391
                if (item.GetType() == typeof(Symbol))
392
                {
393
                    Symbol symbol = item as Symbol;
394
                    SymbolModeling(symbol, targetSymbol);
395
                    targetSymbol = symbol;
396
                }
397
                else
398
                {
399
                    targetSymbol = null;
400
                }
401
            }
402
        }
403

    
404
        /// <summary>
405
        /// Run에 있는 심볼을 모델링하는데 기준이 Run의 시작점
406
        /// </summary>
407
        /// <param name="symbol"></param>
408
        /// <param name="run"></param>
409
        private void SymbolModelingByRunStart(Symbol symbol, LineRun run)
410
        {
411
            foreach (var connector in symbol.CONNECTORS)
412
            {
413
                object targetItem = SPPIDUtil.FindObjectByUID(document, connector.CONNECTEDITEM);
414
                if (targetItem != null &&
415
                    (targetItem.GetType() == typeof(Symbol) || targetItem.GetType() == typeof(Equipment)) &&
416
                    !IsSameLineRun(symbol, targetItem))
417
                {
418
                    SymbolModeling(symbol, targetItem as Symbol);
419
                    for (int i = 1; i < run.RUNITEMS.Count; i++)
420
                    {
421
                        object item = run.RUNITEMS[i];
422
                        if (item.GetType() == typeof(Symbol))
423
                            SymbolModeling(item as Symbol, run.RUNITEMS[i - 1] as Symbol);
424
                        else
425
                            break;
426
                    }
427
                    break;
428
                }
429
            }
430

    
431

    
432
        }
433

    
434
        /// <summary>
435
        /// Run에 있는 심볼을 모델링하는데 기준이 Run의 끝점
436
        /// </summary>
437
        /// <param name="symbol"></param>
438
        /// <param name="run"></param>
439
        private void SymbolModelingByRunEnd(Symbol symbol, LineRun run)
440
        {
441
            foreach (var connector in symbol.CONNECTORS)
442
            {
443
                object targetItem = SPPIDUtil.FindObjectByUID(document, connector.CONNECTEDITEM);
444
                if (targetItem != null &&
445
                    (targetItem.GetType() == typeof(Symbol) || targetItem.GetType() == typeof(Equipment)) &&
446
                    !IsSameLineRun(symbol, targetItem))
447
                {
448
                    SymbolModeling(symbol, targetItem as Symbol);
449
                    for (int i = run.RUNITEMS.Count - 2; i >= 0; i--)
450
                    {
451
                        object item = run.RUNITEMS[i];
452
                        if (item.GetType() == typeof(Symbol))
453
                            SymbolModeling(item as Symbol, run.RUNITEMS[i + 1] as Symbol);
454
                        else
455
                            break;
456
                    }
457
                    break;
458
                }
459
            }
460
        }
461

    
462
        /// <summary>
463
        /// 심볼을 실제로 Modeling 메서드
464
        /// </summary>
465
        /// <param name="symbol"></param>
466
        /// <param name="targetSymbol"></param>
467
        /// <param name="prevSymbol"></param>
468
        private void SymbolModeling(Symbol symbol, Symbol targetSymbol)
469
        {
470
#if DEBUG
471
            try
472
            {
473
#endif
474
                // OWNERSYMBOL Attribute, 값을 가지고 있을 경우
475
                BaseModel.Attribute itemAttribute = symbol.ATTRIBUTES.Find(attr => attr.ATTRIBUTE == "OWNERSYMBOL");
476
                if (itemAttribute != null && string.IsNullOrEmpty(itemAttribute.VALUE) && itemAttribute.VALUE != "None")
477
                    return;
478
                // 이미 모델링 됐을 경우
479
                else if (!string.IsNullOrEmpty(symbol.SPPID.RepresentationId))
480
                    return;
481

    
482
                LMSymbol _LMSymbol = null;
483

    
484
                string mappingPath = symbol.SPPID.MAPPINGNAME;
485
                double x = symbol.SPPID.ORIGINAL_X;
486
                double y = symbol.SPPID.ORIGINAL_Y;
487
                int mirror = 0;
488
                double angle = symbol.ANGLE;
489

    
490
                SPPIDUtil.ConvertGridPoint(ref x, ref y);
491

    
492
                // OPC 일경우 180도 일때 Mirror
493
                if (mappingPath.Contains("Piping OPC's") && angle == Math.PI)
494
                    mirror = 1;
495

    
496
                // Mirror 계산
497
                if (symbol.FLIP == 1)
498
                {
499
                    mirror = 1;
500
                    angle += Math.PI;
501
                }
502

    
503
                if (targetSymbol != null && !string.IsNullOrEmpty(targetSymbol.SPPID.RepresentationId))
504
                {
505
                    LMSymbol _TargetItem = dataSource.GetSymbol(targetSymbol.SPPID.RepresentationId);
506
                    Connector connector = SPPIDUtil.FindSymbolConnectorByUID(document, symbol.UID, targetSymbol);
507
                    if (connector != null)
508
                        GetTargetSymbolConnectorPoint(connector, targetSymbol, ref x, ref y);
509

    
510
                    _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle, TargetItem: _TargetItem);
511

    
512
                    if (_LMSymbol != null && _TargetItem != null)
513
                    {
514
                        symbol.SPPID.RepresentationId = _LMSymbol.AsLMRepresentation().Id;
515
                        LMConnector reModelingConnector = FindBreakLineTarget(symbol, targetSymbol);
516

    
517
                        if (reModelingConnector != null)
518
                            ReModelingLMConnector(reModelingConnector);
519
                    }
520

    
521
                    ReleaseCOMObjects(_TargetItem);
522
                }
523
                else
524
                    _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle);
525

    
526

    
527
                if (_LMSymbol != null)
528
                {
529
                    _LMSymbol.Commit();
530
                    symbol.SPPID.RepresentationId = _LMSymbol.AsLMRepresentation().Id;
531
                    symbol.SPPID.GraphicOID = _LMSymbol.get_GraphicOID();
532

    
533
                    foreach (var item in symbol.ChildSymbols)
534
                        CreateChildSymbol(item, _LMSymbol);
535
                }
536

    
537
                ReleaseCOMObjects(_LMSymbol);
538
                SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetProgress, ++CurrentCount);
539
#if DEBUG
540

    
541
            }
542
            catch (Exception ex)
543
            {
544
                System.Windows.Forms.MessageBox.Show(ex.StackTrace);
545
            }
546
#endif
547
        }
548

    
549
        /// <summary>
550
        /// ID2의 Symbol Width와 Height를 비교해서 상대적인 SPPID Connector좌표를 가져온다.
551
        /// </summary>
552
        /// <param name="targetConnector"></param>
553
        /// <param name="targetSymbol"></param>
554
        /// <param name="x"></param>
555
        /// <param name="y"></param>
556
        private void GetTargetSymbolConnectorPoint(Connector targetConnector, Symbol targetSymbol, ref double x, ref double y)
557
        {
558
            LMSymbol _TargetItem = dataSource.GetSymbol(targetSymbol.SPPID.RepresentationId);
559

    
560
            double[] range = null;
561
            List<double[]> points = new List<double[]>();
562
            GetSPPIDSymbolRangeAndConnectionPoints(targetSymbol, ref range, points);
563
            double x1 = range[0];
564
            double y1 = range[1];
565
            double x2 = range[2];
566
            double y2 = range[3];
567

    
568
            // Origin 기준 Connector의 위치차이
569
            double sceneX = 0;
570
            double sceneY = 0;
571
            SPPIDUtil.ConvertPointBystring(targetConnector.SCENECONNECTPOINT, ref sceneX, ref sceneY);
572
            double originX = 0;
573
            double originY = 0;
574
            SPPIDUtil.ConvertPointBystring(targetSymbol.ORIGINALPOINT, ref originX, ref originY);
575
            double gapX = originX - sceneX;
576
            double gapY = originY - sceneY;
577

    
578
            // SPPID Symbol과 ID2 심볼의 크기 차이
579
            double sizeWidth = 0;
580
            double sizeHeight = 0;
581
            SPPIDUtil.ConvertPointBystring(targetSymbol.SIZE, ref sizeWidth, ref sizeHeight);
582
            double percentX = (x2 - x1) / sizeWidth;
583
            double percentY = (y2 - y1) / sizeHeight;
584

    
585
            double SPPIDgapX = gapX * percentX;
586
            double SPPIDgapY = gapY * percentY;
587

    
588
            double[] SPPIDOriginPoint = new double[] { _TargetItem.get_XCoordinate() - SPPIDgapX, _TargetItem.get_YCoordinate() + SPPIDgapY };
589
            double distance = double.MaxValue;
590
            double[] resultPoint;
591
            foreach (var point in points)
592
            {
593
                double result = SPPIDUtil.CalcPointToPointdDistance(point[0], point[1], SPPIDOriginPoint[0], SPPIDOriginPoint[1]);
594
                if (distance > result)
595
                {
596
                    distance = result;
597
                    resultPoint = point;
598
                    x = point[0];
599
                    y = point[1];
600
                }
601
            }
602

    
603
            ReleaseCOMObjects(_TargetItem);
604
        }
605

    
606
        /// <summary>
607
        /// SPPID Symbol의 Range를 구한다.
608
        /// </summary>
609
        /// <param name="symbol"></param>
610
        /// <param name="range"></param>
611
        private void GetSPPIDSymbolRangeAndConnectionPoints(Symbol symbol, ref double[] range, List<double[]> points)
612
        {
613
            LMSymbol _TargetItem = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
614
            Ingr.RAD2D.Symbol2d symbol2d = radApp.ActiveDocument.ActiveSheet.DrawingObjects[_TargetItem.get_GraphicOID().ToString()];
615
            double x1 = 0;
616
            double y1 = 0;
617
            double x2 = 0;
618
            double y2 = 0;
619
            symbol2d.Range(out x1, out y1, out x2, out y2);
620
            range = new double[] { x1, y1, x2, y2 };
621

    
622
            for (int i = 1; i < int.MaxValue; i++)
623
            {
624
                double connX = 0;
625
                double connY = 0;
626
                if (_placement.PIDConnectPointLocation(_TargetItem, i, ref connX, ref connY))
627
                    points.Add(new double[] { connX, connY });
628
                else
629
                    break;
630
            }
631

    
632
            foreach (var childSymbol in symbol.ChildSymbols)
633
                GetSPPIDChildSymbolRange(childSymbol, ref range, points);
634

    
635
            ReleaseCOMObjects(_TargetItem);
636
        }
637

    
638
        /// <summary>
639
        /// Child Modeling 된 Symbol의 Range를 구한다.
640
        /// </summary>
641
        /// <param name="childSymbol"></param>
642
        /// <param name="range"></param>
643
        private void GetSPPIDChildSymbolRange(ChildSymbol childSymbol, ref double[] range, List<double[]> points)
644
        {
645
            LMSymbol _ChildSymbol = dataSource.GetSymbol(childSymbol.SPPID.RepresentationId);
646
            Ingr.RAD2D.Symbol2d symbol2d = radApp.ActiveDocument.ActiveSheet.DrawingObjects[_ChildSymbol.get_GraphicOID().ToString()];
647
            double x1 = 0;
648
            double y1 = 0;
649
            double x2 = 0;
650
            double y2 = 0;
651
            symbol2d.Range(out x1, out y1, out x2, out y2);
652
            range[0] = Math.Min(range[0], x1);
653
            range[1] = Math.Min(range[1], y1);
654
            range[2] = Math.Max(range[2], x2);
655
            range[3] = Math.Max(range[3], y2);
656

    
657
            for (int i = 1; i < int.MaxValue; i++)
658
            {
659
                double connX = 0;
660
                double connY = 0;
661
                if (_placement.PIDConnectPointLocation(_ChildSymbol, i, ref connX, ref connY))
662
                    points.Add(new double[] { connX, connY });
663
                else
664
                    break;
665
            }
666

    
667
            foreach (var loopChildSymbol in childSymbol.ChildSymbols)
668
                GetSPPIDChildSymbolRange(loopChildSymbol, ref range, points);
669

    
670
            ReleaseCOMObjects(_ChildSymbol);
671
        }
672

    
673
        /// <summary>
674
        /// Label Symbol Modeling
675
        /// </summary>
676
        /// <param name="symbol"></param>
677
        private void LabelSymbolModeling(Symbol symbol)
678
        {
679
            BaseModel.Attribute itemAttribute = symbol.ATTRIBUTES.Find(x => x.ATTRIBUTE == "OWNERSYMBOL");
680
            if (itemAttribute == null || string.IsNullOrEmpty(itemAttribute.VALUE))
681
                return;
682

    
683
            Array points = new double[] { 0, symbol.SPPID.ORIGINAL_X, symbol.SPPID.ORIGINAL_Y };
684
            
685
            string symbolUID = itemAttribute.VALUE;
686
            object targetItem = SPPIDUtil.FindObjectByUID(document, symbolUID);
687
            if (targetItem != null)
688
            {
689
                // Object 아이템이 Symbol일 경우 Equipment일 경우 
690
                string sRep = null;
691
                if (targetItem.GetType() == typeof(Symbol))
692
                    sRep = ((Symbol)targetItem).SPPID.RepresentationId;
693
                else if (targetItem.GetType() == typeof(Equipment))
694
                    sRep = ((Equipment)targetItem).SPPID.RepresentationId;
695

    
696
                if (!string.IsNullOrEmpty(sRep))
697
                {
698
                    // LEADER Line 검사
699
                    bool leaderLine = false;
700
                    SymbolMapping symbolMapping = document.SymbolMappings.Find(x => x.UID == symbol.DBUID);
701
                    if (symbolMapping != null)
702
                        leaderLine = symbolMapping.LEADERLINE;
703

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

    
708
                    //Leader 선 센터로
709
                    if (_LMLabelPresist != null)
710
                    {
711
                        // Target Item에 Label의 Attribute Input
712
                        InputSymbolAttribute(targetItem, symbol.ATTRIBUTES);
713

    
714
                        string OID = _LMLabelPresist.get_GraphicOID();
715
                        DependencyObject dependency = radApp.ActiveDocument.ActiveSheet.DrawingObjects[OID] as DependencyObject;
716
                        if (dependency != null)
717
                        {
718
                            bool result = false;
719
                            foreach (var attributes in dependency.AttributeSets)
720
                            {
721
                                foreach (var attribute in attributes)
722
                                {
723
                                    string name = attribute.Name;
724
                                    string value = attribute.GetValue().ToString();
725
                                    if (name == "DrawingItemType" && value == "LabelPersist")
726
                                    {
727
                                        foreach (DrawingObjectBase drawingObject in dependency.DrawingObjects)
728
                                        {
729
                                            if (drawingObject.Type == Ingr.RAD2D.ObjectType.igLineString2d)
730
                                            {
731
                                                Ingr.RAD2D.LineString2d lineString2D = drawingObject as Ingr.RAD2D.LineString2d;
732
                                                double prevX = _TargetItem.get_XCoordinate();
733
                                                double prevY = _TargetItem.get_YCoordinate();
734
                                                lineString2D.InsertVertex(lineString2D.VertexCount, prevX, prevY);
735
                                                lineString2D.RemoveVertex(lineString2D.VertexCount);
736
                                                result = true;
737
                                                break;
738
                                            }
739
                                        }
740
                                    }
741

    
742
                                    if (result)
743
                                        break;
744
                                }
745

    
746
                                if (result)
747
                                    break;
748
                            }
749
                        }
750

    
751
                        _LMLabelPresist.Commit();
752
                    }
753
                    
754
                    ReleaseCOMObjects(_TargetItem);
755
                    ReleaseCOMObjects(_LMLabelPresist);
756
                }
757
            }
758

    
759
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetProgress, ++CurrentCount);
760
        }
761

    
762
        /// <summary>
763
        /// Equipment를 실제로 Modeling 메서드
764
        /// </summary>
765
        /// <param name="equipment"></param>
766
        private void EquipmentModeling(Equipment equipment)
767
        {
768
            if (!string.IsNullOrEmpty(equipment.SPPID.RepresentationId))
769
                return;
770

    
771
            LMSymbol _LMSymbol = null;
772
            LMSymbol targetItem = null;
773
            string mappingPath = equipment.SPPID.MAPPINGNAME;
774
            double x = equipment.SPPID.ORIGINAL_X;
775
            double y = equipment.SPPID.ORIGINAL_Y;
776
            int mirror = 0;
777
            double angle = equipment.ANGLE;
778

    
779
            SPPIDUtil.ConvertGridPoint(ref x, ref y);
780

    
781
            Connector connector = equipment.CONNECTORS.Find(conn => !string.IsNullOrEmpty(conn.CONNECTEDITEM) && conn.CONNECTEDITEM != "None");
782
            if (connector != null)
783
            {
784
                Equipment connEquipment = SPPIDUtil.FindObjectByUID(document, connector.CONNECTEDITEM) as Equipment;
785
                if (connEquipment != null)
786
                {
787
                    if (string.IsNullOrEmpty(connEquipment.SPPID.RepresentationId))
788
                        EquipmentModeling(connEquipment);
789

    
790
                    if (!string.IsNullOrEmpty(connEquipment.SPPID.RepresentationId))
791
                    {
792
                        targetItem = dataSource.GetSymbol(connEquipment.SPPID.RepresentationId);
793
                        if (targetItem != null)
794
                        {
795
                            _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle, TargetItem: targetItem);
796
                        }
797
                        else
798
                        {
799
                            _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle);
800
                        }
801
                    }
802
                    else
803
                    {
804
                        _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle);
805
                    }
806
                }
807
                else
808
                {
809
                    _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle);
810
                }
811
            }
812
            else
813
            {
814
                _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle);
815
            }
816

    
817
            if (_LMSymbol != null)
818
            {
819
                _LMSymbol.Commit();
820
                equipment.SPPID.RepresentationId = _LMSymbol.AsLMRepresentation().Id;
821
                equipment.SPPID.GraphicOID = _LMSymbol.get_GraphicOID();
822
                ReleaseCOMObjects(_LMSymbol);
823
            }
824

    
825
            if (targetItem != null)
826
            {
827
                ReleaseCOMObjects(targetItem);
828
            }
829
            
830
            ReleaseCOMObjects(_LMSymbol);
831

    
832
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetProgress, ++CurrentCount);
833
        }
834

    
835
        private void SymbolModelingByPriority(Symbol symbol)
836
        {
837
            // Angle, Center, 우선순위 모델링
838
            SymbolModeling(symbol, null);
839
            List<Symbol> group = new List<Symbol>() { symbol };
840
            SPPIDUtil.FindConnectedSymbolGroup(document, symbol, group);
841

    
842
            List<Symbol> endModeling = new List<Symbol>() { symbol };
843
            while (endModeling.Count != group.Count)
844
            {
845
                foreach (var item in group)
846
                {
847
                    if (!endModeling.Contains(item))
848
                    {
849
                        bool result = false;
850
                        foreach (var connector in item.CONNECTORS)
851
                        {
852
                            Symbol connSymbol = group.Find(x => x.UID == connector.CONNECTEDITEM);
853
                            if (connSymbol == item)
854
                                throw new Exception(connSymbol.UID);
855

    
856
                            if (connSymbol != null && endModeling.Contains(connSymbol))
857
                            {
858
                                SymbolModeling(item, connSymbol);
859
                                endModeling.Add(item);
860
                                result = true;
861
                                break;
862
                            }
863
                        }
864

    
865
                        if (result)
866
                            break;
867
                    }
868
                }
869
            }
870
        }
871

    
872
        /// <summary>
873
        /// 심볼을 실제로 Modeling할때 ChildSymbol이 있다면 Modeling하는 메서드
874
        /// </summary>
875
        /// <param name="childSymbol"></param>
876
        /// <param name="parentSymbol"></param>
877
        private void CreateChildSymbol(ChildSymbol childSymbol, LMSymbol parentSymbol)
878
        {
879
            Ingr.RAD2D.Symbol2d symbol2d = radApp.ActiveDocument.ActiveSheet.DrawingObjects[parentSymbol.get_GraphicOID().ToString()];
880
            double x1 = 0;
881
            double x2 = 0;
882
            double y1 = 0;
883
            double y2 = 0;
884
            symbol2d.Range(out x1, out y1, out x2, out y2);
885

    
886
            LMSymbol _LMSymbol = _placement.PIDPlaceSymbol(childSymbol.SPPID.MAPPINGNAME, (x1 + x2) / 2, (y1 + y2) / 2, TargetItem: parentSymbol);
887
            if (_LMSymbol != null)
888
            {
889
                childSymbol.SPPID.RepresentationId = _LMSymbol.AsLMRepresentation().Id;
890
                foreach (var item in childSymbol.ChildSymbols)
891
                    CreateChildSymbol(item, _LMSymbol);
892
            }
893
            
894

    
895
            ReleaseCOMObjects(_LMSymbol);
896
        }
897

    
898
        /// <summary>
899
        /// item이 TargetItem과 같은 LineRun에 있는지 검사
900
        /// </summary>
901
        /// <param name="item"></param>
902
        /// <param name="targetItem"></param>
903
        /// <returns></returns>
904
        private bool IsSameLineRun(object item, object targetItem)
905
        {
906
            foreach (var lineNumber in document.LINENUMBERS)
907
            {
908
                foreach (var run in lineNumber.RUNS)
909
                {
910
                    foreach (var runItem in run.RUNITEMS)
911
                    {
912
                        if (runItem == item)
913
                        {
914
                            foreach (var findItem in run.RUNITEMS)
915
                            {
916
                                if (findItem == targetItem)
917
                                {
918
                                    return true;
919
                                }
920
                            }
921

    
922
                            return false;
923

    
924
                        }
925
                    }
926
                }
927
            }
928

    
929
            return false;
930
        }
931

    
932
        /// <summary>
933
        /// Line을 실제로 모델링하는 메서드
934
        /// </summary>
935
        /// <param name="lines"></param>
936
        private void LineModeling(List<Line> lines)
937
        {
938
            _LMAItem _LMAItem = _placement.PIDCreateItem(lines[0].SPPID.MAPPINGNAME);
939
            PlaceRunInputs placeRunInputs = new PlaceRunInputs();
940
            LMSymbol _LMSymbol1 = null;
941
            LMSymbol _LMSymbol2 = null;
942
            Dictionary<LMConnector, List<double[]>> connectorVertices1 = new Dictionary<LMConnector, List<double[]>>();
943
            LMConnector targetConnector1 = null;
944
            Dictionary<LMConnector, List<double[]>> connectorVertices2 = new Dictionary<LMConnector, List<double[]>>();
945
            LMConnector targetConnector2 = null;
946
            
947
            Line startBranchLine = null;
948
            Line endBranchLine = null;
949

    
950
            // Type, Line, TargetObjet, x, y
951
            List<Tuple<string, Line, object, double, double>> linePointInfo = new List<Tuple<string, Line, object, double, double>>();
952
            // Point 정리
953
            for (int i = 0; i < lines.Count; i++)
954
            {
955
                Line line = lines[i];
956
                if (i == 0 || i + 1 != lines.Count)
957
                {
958
                    // 시작점에 연결된 Symbol 찾기
959
                    object connItem = SPPIDUtil.FindObjectByUID(document, line.CONNECTORS[0].CONNECTEDITEM);
960
                    if (connItem != null && connItem.GetType() == typeof(Symbol))
961
                    {
962
                        Symbol symbol1 = connItem as Symbol;
963
                        _LMSymbol1 = GetTargetSymbol(symbol1, line);
964
                        if (_LMSymbol1 != null)
965
                        {
966
                            double x = line.SPPID.START_X;
967
                            double y = line.SPPID.START_Y;
968
                            Connector connector = SPPIDUtil.FindSymbolConnectorByUID(document, line.UID, symbol1);
969
                            if (connector != null)
970
                            {
971
                                GetTargetSymbolConnectorPoint(connector, symbol1, ref x, ref y);
972
                                line.SPPID.START_X = x;
973
                                line.SPPID.START_Y = y;
974
                            }
975

    
976
                            linePointInfo.Add(new Tuple<string, Line, object, double, double>("SYMBOL", line, _LMSymbol1, x, y));
977
                        }
978
                        else
979
                            linePointInfo.Add(new Tuple<string, Line, object, double, double>(null, line, null, line.SPPID.START_X, line.SPPID.START_Y));
980
                    }
981
                    else if (connItem != null && connItem.GetType() == typeof(Line) && !lines.Contains(connItem))
982
                    {
983
                        connectorVertices1 = GetPipeRunVertices(((Line)connItem).SPPID.ModelItemId);
984
                        targetConnector1 = FindTargetLMConnector(connectorVertices1, line.SPPID.START_X, line.SPPID.START_Y, line.SPPID.END_X, line.SPPID.END_Y);
985

    
986
                        if (targetConnector1 != null)
987
                            linePointInfo.Add(new Tuple<string, Line, object, double, double>("LINE", line, targetConnector1, line.SPPID.START_X, line.SPPID.START_Y));
988
                        else
989
                        {
990
                            startBranchLine = connItem as Line;
991
                            linePointInfo.Add(new Tuple<string, Line, object, double, double>(null, line, null, line.SPPID.START_X, line.SPPID.START_Y));
992
                        }
993
                    }
994
                    else
995
                        linePointInfo.Add(new Tuple<string, Line, object, double, double>(null, line, null, line.SPPID.START_X, line.SPPID.START_Y));
996
                        
997
                }
998
                if (i + 1 == lines.Count)
999
                {
1000
                    // 끝점에 연결된 Symbol 찾기
1001
                    object connItem = SPPIDUtil.FindObjectByUID(document, line.CONNECTORS[1].CONNECTEDITEM);
1002

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

    
1006
                    if (connItem != null && connItem.GetType() == typeof(Symbol))
1007
                    {
1008
                        Symbol symbol2 = connItem as Symbol;
1009
                        _LMSymbol2 = GetTargetSymbol(connItem as Symbol, line);
1010
                        if (_LMSymbol2 != null)
1011
                        {
1012
                            double x = line.SPPID.END_X;
1013
                            double y = line.SPPID.END_Y;
1014
                            Connector connector = SPPIDUtil.FindSymbolConnectorByUID(document, line.UID, symbol2);
1015
                            if (connector != null)
1016
                            {
1017
                                GetTargetSymbolConnectorPoint(connector, symbol2, ref x, ref y);
1018
                                line.SPPID.END_X = x;
1019
                                line.SPPID.END_Y = y;
1020
                            }
1021

    
1022
                            linePointInfo.Add(new Tuple<string, Line, object, double, double>("SYMBOL", line, _LMSymbol2, x, y));
1023
                        }
1024
                        else
1025
                            linePointInfo.Add(new Tuple<string, Line, object, double, double>(null, line, null, line.SPPID.END_X, line.SPPID.END_Y));
1026
                    }
1027
                    else if (connItem != null && connItem.GetType() == typeof(Line) && !lines.Contains(connItem))
1028
                    {
1029
                        connectorVertices2 = GetPipeRunVertices(((Line)connItem).SPPID.ModelItemId);
1030
                        targetConnector2 = FindTargetLMConnector(connectorVertices2, line.SPPID.END_X, line.SPPID.END_Y, line.SPPID.START_X, line.SPPID.START_Y);
1031

    
1032
                        if (targetConnector2 != null)
1033
                            linePointInfo.Add(new Tuple<string, Line, object, double, double>("LINE", line, targetConnector2, line.SPPID.END_X, line.SPPID.END_Y));
1034
                        else
1035
                        {
1036
                            endBranchLine = connItem as Line;
1037
                            linePointInfo.Add(new Tuple<string, Line, object, double, double>(null, line, null, line.SPPID.END_X, line.SPPID.END_Y));
1038
                        }
1039
                    }
1040
                    else
1041
                        linePointInfo.Add(new Tuple<string, Line, object, double, double>(null, line, null, line.SPPID.END_X, line.SPPID.END_Y));
1042
                }
1043
            }
1044

    
1045
            double prevX = double.NaN;
1046
            double prevY = double.NaN;
1047
            SlopeType prevSlopeType = SlopeType.None;
1048
            for (int i = 0; i < linePointInfo.Count; i++)
1049
            {
1050
                Tuple<string, Line, object, double, double> item = linePointInfo[i];
1051
                Line line = item.Item2;
1052
                double x = item.Item4;
1053
                double y = item.Item5;
1054
                SlopeType slopeType = SPPIDUtil.CalcSlope(line.SPPID.START_X, line.SPPID.START_Y, line.SPPID.END_X, line.SPPID.END_Y);
1055
                // Symbol일 경우 바로 Input Point
1056
                if (item.Item1 == "SYMBOL")
1057
                    placeRunInputs.AddSymbolTarget(item.Item3 as LMSymbol, x, y);
1058
                else
1059
                {
1060
                    SPPIDUtil.ConvertGridPoint(ref x, ref y);
1061
                    // i == 0은 그대로 사용
1062
                    if (i != 0)
1063
                    {
1064
                        Tuple<string, Line, object, double, double> prevItem = linePointInfo[i - 1];
1065
                        // y 좌표가 같아야함 및 Symbol 좌표가 정확하지 않으므로 한번더 보정
1066
                        if (prevSlopeType == SlopeType.HORIZONTAL)
1067
                        {
1068
                            y = prevY;
1069
                            SPPIDUtil.ConvertGridPointOnlyOnePoint(ref y);
1070
                        }
1071
                        else if (prevSlopeType == SlopeType.VERTICAL)
1072
                        {
1073
                            x = prevX;
1074
                            SPPIDUtil.ConvertGridPointOnlyOnePoint(ref x);
1075
                        }
1076

    
1077
                        // 마지막이 Symbol일 경우는 Symbol의 좌표를 따라감
1078
                        if (i + 1 == linePointInfo.Count - 1 && linePointInfo[i + 1].Item1 == "SYMBOL")
1079
                        {
1080
                            Line nextLine = linePointInfo[i + 1].Item2;
1081
                            SlopeType nextSlopeType = SPPIDUtil.CalcSlope(nextLine.SPPID.START_X, nextLine.SPPID.START_Y, nextLine.SPPID.END_X, nextLine.SPPID.END_Y);
1082
                            if (slopeType == SlopeType.HORIZONTAL)
1083
                                y = linePointInfo[i + 1].Item5;
1084
                            else if (slopeType == SlopeType.VERTICAL)
1085
                                x = linePointInfo[i + 1].Item4;
1086
                        }
1087
                    }
1088

    
1089
                    if (item.Item1 == "LINE")
1090
                        placeRunInputs.AddConnectorTarget(item.Item3 as LMConnector, x, y);
1091
                    else
1092
                        placeRunInputs.AddPoint(x, y);
1093
                }
1094

    
1095
                prevX = x;
1096
                prevY = y;
1097
                prevSlopeType = slopeType;
1098
            }
1099

    
1100
            LMConnector _lMConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
1101

    
1102
            if (_lMConnector != null)
1103
            {
1104
                if (_LMSymbol1 != null || _LMSymbol2 != null)
1105
                    ReModelingLine(lines, _lMConnector, _LMSymbol1, _LMSymbol2);
1106
                else
1107
                {
1108
                    foreach (var line in lines)
1109
                        line.SPPID.ModelItemId = _lMConnector.ModelItemID;
1110
                    _lMConnector.Commit();
1111
                }
1112
                if (startBranchLine != null || endBranchLine != null)
1113
                    BranchLines.Add(new Tuple<string, Line, Line>(lines[0].SPPID.ModelItemId, startBranchLine, endBranchLine));
1114
                
1115
            }
1116

    
1117
            if (_LMSymbol1 != null)
1118
                ReleaseCOMObjects(_LMSymbol1);
1119
            if (_LMSymbol2 != null)
1120
                ReleaseCOMObjects(_LMSymbol2);
1121
            if (targetConnector1 != null)
1122
                ReleaseCOMObjects(targetConnector1);
1123
            if (targetConnector2 != null)
1124
                ReleaseCOMObjects(targetConnector2);
1125
            foreach (var item in connectorVertices1)
1126
                ReleaseCOMObjects(item.Key);
1127
            foreach (var item in connectorVertices2)
1128
                ReleaseCOMObjects(item.Key);
1129

    
1130
            ReleaseCOMObjects(_lMConnector);
1131
            ReleaseCOMObjects(placeRunInputs);
1132
            ReleaseCOMObjects(_LMAItem);
1133
        }
1134

    
1135
        /// <summary>
1136
        /// Symbol에 붙을 경우 Line을 Remodeling 한다.
1137
        /// </summary>
1138
        /// <param name="lines"></param>
1139
        /// <param name="prevLMConnector"></param>
1140
        /// <param name="startSymbol"></param>
1141
        /// <param name="endSymbol"></param>
1142
        private void ReModelingLine(List<Line> lines, LMConnector prevLMConnector, LMSymbol startSymbol, LMSymbol endSymbol)
1143
        {
1144
            
1145
            string symbolPath = string.Empty;
1146
            #region get symbol path
1147
            LMModelItem modelItem = dataSource.GetModelItem(prevLMConnector.ModelItemID);
1148
            foreach (LMRepresentation rep in modelItem.Representations)
1149
            {
1150
                if (!DBNull.Value.Equals(rep.get_FileName()) && !string.IsNullOrEmpty(rep.get_FileName()))
1151
                {
1152
                    symbolPath = rep.get_FileName();
1153
                    break;
1154
                }
1155
            }
1156
            #endregion
1157
            _LMAItem _LMAItem = _placement.PIDCreateItem(symbolPath);
1158
            LMConnector newConnector = null;
1159
            dynamic OID = prevLMConnector.get_GraphicOID();
1160
            DependencyObject drawingObject = radApp.ActiveDocument.ActiveSheet.DrawingObjects[OID];
1161
            Ingr.RAD2D.LineStringGeometry2d lineStringGeometry = drawingObject.GetGeometry() as Ingr.RAD2D.LineStringGeometry2d;
1162
            int verticesCount = lineStringGeometry.VertexCount;
1163
            PlaceRunInputs placeRunInputs = new PlaceRunInputs();
1164
            
1165
            List<double[]> vertices = new List<double[]>();
1166
            for (int i = 1; i <= verticesCount; i++)
1167
            {
1168
                double x = 0;
1169
                double y = 0;
1170
                lineStringGeometry.GetVertex(i, ref x, ref y);
1171
                vertices.Add(new double[] { x, y });
1172
            }
1173

    
1174
            for (int i = 0; i < vertices.Count; i++)
1175
            {
1176
                double[] points = vertices[i];
1177
                // 시작 심볼이 있고 첫번째 좌표일 때
1178
                if (startSymbol != null && i == 0)
1179
                {
1180
                    SlopeType slopeType = SPPIDUtil.CalcSlope(points[0], points[1], vertices[i + 1][0], vertices[i + 1][1]);
1181
                    if (slopeType == SlopeType.HORIZONTAL)
1182
                        placeRunInputs.AddPoint(points[0], -0.1);
1183
                    else if (slopeType == SlopeType.VERTICAL)
1184
                        placeRunInputs.AddPoint(-0.1, points[1]);
1185
                    else
1186
                        placeRunInputs.AddPoint(points[0], -0.1);
1187

    
1188
                    placeRunInputs.AddPoint(points[0], points[1]);
1189
                }
1190
                // 마지막 심볼이 있고 마지막 좌표일 때
1191
                else if (endSymbol != null && i == vertices.Count - 1)
1192
                {
1193
                    placeRunInputs.AddPoint(points[0], points[1]);
1194

    
1195
                    SlopeType slopeType = SPPIDUtil.CalcSlope(points[0], points[1], vertices[i - 1][0], vertices[i - 1][1]);
1196
                    if (slopeType == SlopeType.HORIZONTAL)
1197
                        placeRunInputs.AddPoint(points[0], -0.1);
1198
                    else if (slopeType == SlopeType.VERTICAL)
1199
                        placeRunInputs.AddPoint(-0.1, points[1]);
1200
                    else
1201
                        placeRunInputs.AddPoint(points[0], -0.1);
1202
                }
1203
                // 첫번째이며 시작 심볼이 아니고 Connecotr일 경우
1204
                else if (i == 0 && prevLMConnector.ConnectItem1SymbolObject != null)
1205
                    placeRunInputs.AddSymbolTarget(prevLMConnector.ConnectItem1SymbolObject, points[0], points[1]);
1206
                // 마지막이며 마지막 심볼이 아니고 Connecotr일 경우
1207
                else if (i == vertices.Count - 1 && prevLMConnector.ConnectItem2SymbolObject != null)
1208
                    placeRunInputs.AddSymbolTarget(prevLMConnector.ConnectItem2SymbolObject, points[0], points[1]);
1209
                else
1210
                    placeRunInputs.AddPoint(points[0], points[1]);
1211
            }
1212

    
1213
            _placement.PIDRemovePlacement(prevLMConnector.AsLMRepresentation());
1214
            newConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
1215

    
1216
            ReleaseCOMObjects(placeRunInputs);
1217
            ReleaseCOMObjects(_LMAItem);
1218
            ReleaseCOMObjects(modelItem);
1219

    
1220
            if (newConnector != null)
1221
            {
1222
                if (startSymbol != null)
1223
                {
1224
                    _LMAItem = _placement.PIDCreateItem(symbolPath);
1225
                    placeRunInputs = new PlaceRunInputs();
1226
                    placeRunInputs.AddSymbolTarget(startSymbol, vertices[0][0], vertices[0][1]);
1227
                    placeRunInputs.AddConnectorTarget(newConnector, vertices[0][0], vertices[0][1]);
1228
                    LMConnector _LMConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
1229
                    if (_LMConnector != null)
1230
                    {
1231
                        RemoveConnectorForReModelingLine(newConnector);
1232
                        ReleaseCOMObjects(_LMConnector);
1233
                    }
1234
                    ReleaseCOMObjects(placeRunInputs);
1235
                    ReleaseCOMObjects(_LMAItem);
1236
                }
1237

    
1238
                if (endSymbol != null)
1239
                {
1240
                    if (startSymbol != null)
1241
                    {
1242
                        Dictionary<LMConnector, List<double[]>> dicVertices = GetPipeRunVertices(newConnector.ModelItemID);
1243
                        newConnector = dicVertices.First().Key;
1244
                    }
1245

    
1246
                    _LMAItem = _placement.PIDCreateItem(symbolPath);
1247
                    placeRunInputs = new PlaceRunInputs();
1248
                    placeRunInputs.AddSymbolTarget(endSymbol, vertices[vertices.Count - 1][0], vertices[vertices.Count - 1][1]);
1249
                    placeRunInputs.AddConnectorTarget(newConnector, vertices[vertices.Count - 1][0], vertices[vertices.Count - 1][1]);
1250
                    LMConnector _LMConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
1251
                    if (_LMConnector != null)
1252
                    {
1253
                        RemoveConnectorForReModelingLine(newConnector);
1254
                        ReleaseCOMObjects(_LMConnector);
1255
                    }
1256
                    ReleaseCOMObjects(placeRunInputs);
1257
                    ReleaseCOMObjects(_LMAItem);
1258
                }
1259

    
1260
                foreach (var line in lines)
1261
                    line.SPPID.ModelItemId = newConnector.ModelItemID;
1262
                ReleaseCOMObjects(newConnector);
1263
            }
1264

    
1265
            ReleaseCOMObjects(modelItem);
1266
        }
1267

    
1268
        /// <summary>
1269
        /// Remodeling 과정에서 생긴 불필요한 Connector 제거
1270
        /// </summary>
1271
        /// <param name="connector"></param>
1272
        private void RemoveConnectorForReModelingLine(LMConnector connector)
1273
        {
1274
            Dictionary<LMConnector, List<double[]>> dicVertices = GetPipeRunVertices(connector.ModelItemID);
1275
            foreach (var item in dicVertices)
1276
            {
1277
                bool result = false;
1278
                foreach (var point in item.Value)
1279
                {
1280
                    if (point[0] < 0 || point[1] < 0)
1281
                    {
1282
                        result = true;
1283
                        _placement.PIDRemovePlacement(item.Key.AsLMRepresentation());
1284
                        break;
1285
                    }
1286
                }
1287

    
1288
                if (result)
1289
                    break;
1290
            }
1291
            foreach (var item in dicVertices)
1292
                ReleaseCOMObjects(item.Key);
1293
        }
1294

    
1295
        /// <summary>
1296
        /// Symbol이 모델링된 SPPPID Symbol Object를 반환 - 연결된 Symbol이 ChildSymbol일 수도 있기때문에 메서드 개발
1297
        /// </summary>
1298
        /// <param name="symbol"></param>
1299
        /// <param name="line"></param>
1300
        /// <returns></returns>
1301
        private LMSymbol GetTargetSymbol(Symbol symbol, Line line)
1302
        {
1303
            LMSymbol _LMSymbol = null;
1304
            foreach (var connector in symbol.CONNECTORS)
1305
            {
1306
                if (connector.CONNECTEDITEM == line.UID)
1307
                {
1308
                    if (connector.Index == 0)
1309
                        _LMSymbol = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
1310
                    else
1311
                    {
1312
                        ChildSymbol child = null;
1313
                        foreach (var childSymbol in symbol.ChildSymbols)
1314
                        {
1315
                            if (childSymbol.Connectors.Contains(connector))
1316
                                child = childSymbol;
1317
                            else
1318
                                child = GetChildSymbolByConnector(childSymbol, connector);
1319

    
1320
                            if (child != null)
1321
                                break;
1322
                        }
1323

    
1324
                        if (child != null)
1325
                            _LMSymbol = dataSource.GetSymbol(child.SPPID.RepresentationId);
1326
                    }
1327

    
1328
                    break;  
1329
                }
1330
            }
1331

    
1332
            return _LMSymbol;
1333
        }
1334

    
1335
        /// <summary>
1336
        /// Connector를 가지고 있는 ChildSymbol Object 반환
1337
        /// </summary>
1338
        /// <param name="item"></param>
1339
        /// <param name="connector"></param>
1340
        /// <returns></returns>
1341
        private ChildSymbol GetChildSymbolByConnector(ChildSymbol item, Connector connector)
1342
        {
1343
            foreach (var childSymbol in item.ChildSymbols)
1344
            {
1345
                if (childSymbol.Connectors.Contains(connector))
1346
                    return childSymbol;
1347
                else
1348
                    return GetChildSymbolByConnector(childSymbol, connector);
1349
            }
1350

    
1351
            return null;
1352
        }
1353

    
1354
        /// <summary>
1355
        /// Branch 라인을 다시 모델링하는 진입 메서드
1356
        /// </summary>
1357
        /// <param name="branch"></param>
1358
        private void BranchLineModeling(Tuple<string, Line, Line> branch)
1359
        {
1360
            List<Line> lines = SPPIDUtil.FindLinesByModelId(document, branch.Item1);
1361

    
1362
            Dictionary<LMConnector, List<double[]>> connectorVertices = GetPipeRunVertices(branch.Item1);
1363

    
1364
            LMConnector _StartConnector = null;
1365
            LMConnector _EndConnector = null;
1366
            double lengthStart = double.MaxValue;
1367
            double lengthEnd = double.MaxValue;
1368
            List<double[]> startPoints = new List<double[]>();
1369
            List<double[]> endPoints = new List<double[]>();
1370

    
1371
            foreach (var item in connectorVertices)
1372
            {
1373
                foreach (var point in item.Value)
1374
                {
1375
                    // Start Point가 Branch
1376
                    if (branch.Item2 != null)
1377
                    {
1378
                        Line targetLine = branch.Item2;
1379
                        double distance = SPPIDUtil.CalcLineToPointDistance(targetLine.SPPID.START_X, targetLine.SPPID.START_Y, targetLine.SPPID.END_X, targetLine.SPPID.END_Y, point[0], point[1]);
1380
                        if (lengthStart > distance)
1381
                        {
1382
                            _StartConnector = item.Key;
1383
                            lengthStart = distance;
1384
                            startPoints = item.Value;
1385
                        }
1386
                    }
1387
                    // End Point가 Branch
1388
                    if (branch.Item3 != null)
1389
                    {
1390
                        Line targetLine = branch.Item3;
1391
                        double distance = SPPIDUtil.CalcLineToPointDistance(targetLine.SPPID.START_X, targetLine.SPPID.START_Y, targetLine.SPPID.END_X, targetLine.SPPID.END_Y, point[0], point[1]);
1392
                        if (lengthEnd > distance)
1393
                        {
1394
                            _EndConnector = item.Key;
1395
                            lengthEnd = distance;
1396
                            endPoints = item.Value;
1397
                        }
1398
                    }
1399
                }
1400
            }
1401
            #region Branch가 양쪽 전부일 때
1402
            if (_StartConnector != null && _StartConnector == _EndConnector)
1403
            {
1404
                _placement.PIDRemovePlacement(_StartConnector.AsLMRepresentation());
1405

    
1406
                _LMAItem _LMAItem = _placement.PIDCreateItem(lines[0].SPPID.MAPPINGNAME);
1407
                PlaceRunInputs placeRunInputs = new PlaceRunInputs();
1408

    
1409
                Dictionary<LMConnector, List<double[]>> startConnectorVertices = GetPipeRunVertices(branch.Item2.SPPID.ModelItemId);
1410
                LMConnector _StartTargetConnector = FindTargetLMConnector(startConnectorVertices, startPoints[0][0], startPoints[0][1], startPoints[1][0], startPoints[1][1]);
1411
                Dictionary<LMConnector, List<double[]>> endConnectorVertices = GetPipeRunVertices(branch.Item3.SPPID.ModelItemId);
1412
                LMConnector _EndTargetConnector = FindTargetLMConnector(endConnectorVertices,
1413
                   startPoints[startPoints.Count - 1][0],
1414
                   startPoints[startPoints.Count - 1][1],
1415
                   startPoints[startPoints.Count - 2][0],
1416
                   startPoints[startPoints.Count - 2][1]);
1417

    
1418
                for (int i = 0; i < startPoints.Count; i++)
1419
                {
1420
                    double[] point = startPoints[i];
1421
                    if (i == 0)
1422
                        placeRunInputs.AddConnectorTarget(_StartTargetConnector, point[0], point[1]);
1423
                    else if (i == startPoints.Count - 1)
1424
                        placeRunInputs.AddConnectorTarget(_EndTargetConnector, point[0], point[1]);
1425
                    else
1426
                        placeRunInputs.AddPoint(point[0], point[1]);
1427
                }
1428

    
1429
                LMConnector _LMConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
1430
                if (_LMConnector != null)
1431
                {
1432
                    _LMConnector.Commit();
1433
                    foreach (var item in lines)
1434
                        item.SPPID.ModelItemId = _LMConnector.ModelItemID;
1435
                }
1436

    
1437
                foreach (var item in startConnectorVertices)
1438
                    ReleaseCOMObjects(item.Key);
1439
                foreach (var item in endConnectorVertices)
1440
                    ReleaseCOMObjects(item.Key);
1441
                ReleaseCOMObjects(placeRunInputs);
1442
                ReleaseCOMObjects(_LMAItem);
1443
                ReleaseCOMObjects(_LMConnector);
1444
            }
1445
            #endregion
1446
            #region 양쪽이 다른 Branch 
1447
            else
1448
            {
1449
                // Branch 시작 Connector
1450
                if (_StartConnector != null)
1451
                    BranchLineModelingByConnector(branch, _StartConnector, startPoints, true);
1452

    
1453
                // Branch 끝 Connector
1454
                if (_EndConnector != null)
1455
                    BranchLineModelingByConnector(branch, _EndConnector, endPoints, false);
1456
            }
1457
            #endregion
1458

    
1459
            if (_StartConnector != null)
1460
                ReleaseCOMObjects(_StartConnector);
1461
            if (_EndConnector != null)
1462
                ReleaseCOMObjects(_EndConnector);
1463
            foreach (var item in connectorVertices)
1464
                ReleaseCOMObjects(item.Key);
1465
        }
1466

    
1467
        /// <summary>
1468
        /// Branch 라인을 다시 실제로 모델링하는 메서드
1469
        /// </summary>
1470
        /// <param name="branch"></param>
1471
        /// <param name="_Connector"></param>
1472
        /// <param name="points"></param>
1473
        /// <param name="IsStart"></param>
1474
        private void BranchLineModelingByConnector(Tuple<string, Line, Line> branch, LMConnector _Connector, List<double[]> points, bool IsStart)
1475
        {
1476
            List<Line> lines = SPPIDUtil.FindLinesByModelId(document, branch.Item1);
1477
            Dictionary<LMConnector, List<double[]>> connectorVertices = GetPipeRunVertices(branch.Item1);
1478
            LMConnector _SameRunTargetConnector = null;
1479
            LMSymbol _SameRunTargetSymbol = null;
1480
            Dictionary<LMConnector, List<double[]>> branchConnectorVertices = null;
1481
            LMConnector _BranchTargetConnector = null;
1482
            PlaceRunInputs placeRunInputs = new PlaceRunInputs();
1483

    
1484
            // 같은 Line Run의 Connector 찾기
1485
            foreach (var item in connectorVertices)
1486
            {
1487
                if (item.Key == _Connector)
1488
                    continue;
1489

    
1490
                if (IsStart &&
1491
                    !DBNull.Value.Equals(item.Key.ConnectItem1SymbolID) &&
1492
                    !DBNull.Value.Equals(_Connector.ConnectItem2SymbolID)&& 
1493
                    item.Key.ConnectItem1SymbolID == _Connector.ConnectItem2SymbolID)
1494
                {
1495
                    _SameRunTargetConnector = item.Key;
1496
                    break;
1497
                }
1498
                else if (!IsStart &&
1499
                    !DBNull.Value.Equals(item.Key.ConnectItem2SymbolID) &&
1500
                    !DBNull.Value.Equals(_Connector.ConnectItem1SymbolID) && 
1501
                    item.Key.ConnectItem2SymbolID == _Connector.ConnectItem1SymbolID)
1502
                {
1503
                    _SameRunTargetConnector = item.Key;
1504
                    break;
1505
                }
1506
            }
1507

    
1508
            // Branch 반대편이 Symbol
1509
            if (_SameRunTargetConnector == null)
1510
            {
1511
                foreach (var line in lines)
1512
                {
1513
                    foreach (var connector in line.CONNECTORS)
1514
                    {
1515
                        Symbol symbol = SPPIDUtil.FindObjectByUID(document, connector.CONNECTEDITEM) as Symbol;
1516
                        if (symbol != null)
1517
                        {
1518
                            _SameRunTargetSymbol = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
1519
                            break;
1520
                        }
1521
                    }
1522
                }
1523
            }
1524

    
1525
            // 기존 Connector 제거
1526
            _placement.PIDRemovePlacement(_Connector.AsLMRepresentation());
1527
            
1528
            // 시작 Connector일 경우 첫 Point가 TargetConnector를 찾아야함
1529
            if (IsStart)
1530
            {
1531
                branchConnectorVertices = GetPipeRunVertices(branch.Item2.SPPID.ModelItemId);
1532
                _BranchTargetConnector = FindTargetLMConnector(branchConnectorVertices, points[0][0], points[0][1], points[1][0], points[1][1]);
1533
            }
1534
            // 끝 Conenctor일 경우 마지막 Point가 TargetConnector를 찾아야함
1535
            else
1536
            {
1537
                branchConnectorVertices = GetPipeRunVertices(branch.Item3.SPPID.ModelItemId);
1538
                _BranchTargetConnector = FindTargetLMConnector(branchConnectorVertices,
1539
                    points[points.Count - 1][0],
1540
                    points[points.Count - 1][1],
1541
                    points[points.Count - 2][0],
1542
                    points[points.Count - 2][1]);
1543
            }
1544

    
1545
            for (int i = 0; i < points.Count; i++)
1546
            {
1547
                double[] point = points[i];
1548
                if (i == 0)
1549
                {
1550
                    if (IsStart)
1551
                    {
1552
                        if (_BranchTargetConnector != null)
1553
                        {
1554
                            placeRunInputs.AddConnectorTarget(_BranchTargetConnector, point[0], point[1]);
1555
                        }
1556
                        else
1557
                        {
1558
                            placeRunInputs.AddPoint(point[0], point[1]);
1559
                        }
1560
                        
1561
                    }
1562
                    else
1563
                    {
1564
                        if (_SameRunTargetConnector != null)
1565
                            placeRunInputs.AddConnectorTarget(_SameRunTargetConnector, point[0], point[1]);
1566
                        else if (_SameRunTargetSymbol != null)
1567
                            placeRunInputs.AddSymbolTarget(_SameRunTargetSymbol, point[0], point[1]);
1568
                        else
1569
                            placeRunInputs.AddPoint(point[0], point[1]);
1570
                    }
1571
                }
1572
                else if (i == points.Count - 1)
1573
                {
1574
                    if (IsStart)
1575
                    {
1576
                        if (_SameRunTargetConnector != null)
1577
                            placeRunInputs.AddConnectorTarget(_SameRunTargetConnector, point[0], point[1]);
1578
                        else if (_SameRunTargetSymbol != null)
1579
                            placeRunInputs.AddSymbolTarget(_SameRunTargetSymbol, point[0], point[1]);
1580
                        else
1581
                            placeRunInputs.AddPoint(point[0], point[1]);
1582
                    }
1583
                    else
1584
                    {
1585
                        if (_BranchTargetConnector != null)
1586
                        {
1587
                            placeRunInputs.AddConnectorTarget(_BranchTargetConnector, point[0], point[1]);
1588
                        }
1589
                        else
1590
                        {
1591
                            placeRunInputs.AddPoint(point[0], point[1]);
1592
                        }
1593
                    }
1594
                }
1595
                else
1596
                    placeRunInputs.AddPoint(point[0], point[1]);
1597
            }
1598
            _LMAItem _LMAItem = _placement.PIDCreateItem(lines[0].SPPID.MAPPINGNAME);
1599
            LMConnector _LMConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
1600

    
1601
            if (_LMConnector != null)
1602
            {
1603
                if (_SameRunTargetConnector != null)
1604
                {
1605
                    JoinPipeRun(_LMConnector.ModelItemID, _SameRunTargetConnector.ModelItemID);
1606
                }
1607
                else
1608
                {
1609
                    foreach (var item in lines)
1610
                        item.SPPID.ModelItemId = _LMConnector.ModelItemID;
1611
                }
1612

    
1613
                _LMConnector.Commit();
1614
                ReleaseCOMObjects(_LMConnector);
1615
            }
1616

    
1617
            ReleaseCOMObjects(placeRunInputs);
1618
            ReleaseCOMObjects(_LMAItem);
1619
            if (_BranchTargetConnector != null)
1620
                ReleaseCOMObjects(_BranchTargetConnector);
1621
            if (_SameRunTargetConnector != null)
1622
                ReleaseCOMObjects(_SameRunTargetConnector);
1623
            if (_SameRunTargetSymbol != null)
1624
                ReleaseCOMObjects(_SameRunTargetSymbol);
1625
            foreach (var item in connectorVertices)
1626
                ReleaseCOMObjects(item.Key);
1627
            foreach (var item in branchConnectorVertices)
1628
                ReleaseCOMObjects(item.Key);
1629
        }
1630

    
1631
        /// <summary>
1632
        /// EndBreak 모델링 메서드
1633
        /// </summary>
1634
        /// <param name="endBreak"></param>
1635
        private void EndBreakModeling(EndBreak endBreak)
1636
        {
1637
            object ownerObj = SPPIDUtil.FindObjectByUID(document, endBreak.OWNER);
1638
            object connectedItem = SPPIDUtil.FindObjectByUID(document, endBreak.PROPERTIES.Find(x => x.ATTRIBUTE == "Connected Item").VALUE);
1639
            LMConnector targetLMConnector = FindBreakLineTarget(ownerObj, connectedItem);
1640

    
1641
            if (targetLMConnector != null)
1642
            {
1643
                Array array = new double[] { 0, endBreak.SPPID.ORIGINAL_X, endBreak.SPPID.ORIGINAL_Y };
1644
                LMLabelPersist _LmLabelPersist = _placement.PIDPlaceLabel(endBreak.SPPID.MAPPINGNAME, ref array, Rotation: 0, LabeledItem: targetLMConnector.AsLMRepresentation(), IsLeaderVisible: true);
1645
            }
1646
            
1647
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetProgress, ++CurrentCount);
1648
        }
1649

    
1650
        /// <summary>
1651
        /// EndBreak 모델링이 실패시 다시 시도하는 메서드
1652
        /// </summary>
1653
        /// <param name="endBreak"></param>
1654
        /// <param name="targetLMConnector"></param>
1655
        private void RetryEndBreakModeling(EndBreak endBreak, LMConnector targetLMConnector)
1656
        {
1657
            string symbolPath = string.Empty;
1658
            #region get symbol path
1659
            LMModelItem modelItem = dataSource.GetModelItem(targetLMConnector.ModelItemID);
1660
            foreach (LMRepresentation rep in modelItem.Representations)
1661
            {
1662
                if (!DBNull.Value.Equals(rep.get_FileName()) && !string.IsNullOrEmpty(rep.get_FileName()))
1663
                {
1664
                    symbolPath = rep.get_FileName();
1665
                    break;
1666
                }
1667
            }
1668
            #endregion
1669
            bool isZeroLength = Convert.ToBoolean(targetLMConnector.get_IsZeroLength());
1670
            Array array = null;
1671
            LMLabelPersist _LMLabelPersist = null;
1672
            LMConnector _LMConnector = null;
1673
            dynamic OID = targetLMConnector.get_GraphicOID();
1674
            DependencyObject drawingObject = radApp.ActiveDocument.ActiveSheet.DrawingObjects[OID];
1675
            Ingr.RAD2D.LineStringGeometry2d lineStringGeometry = drawingObject.GetGeometry() as Ingr.RAD2D.LineStringGeometry2d;
1676
            int verticesCount = lineStringGeometry.VertexCount;
1677
            PlaceRunInputs placeRunInputs = new PlaceRunInputs();
1678
            _LMAItem _LMAItem = _placement.PIDCreateItem(symbolPath);
1679

    
1680
            if (isZeroLength)
1681
            {
1682
                double[] vertices = null;
1683
                lineStringGeometry.GetVertices(ref verticesCount, ref vertices);
1684
                double x = 0;
1685
                double y = 0;
1686
                lineStringGeometry.GetVertex(1, ref x, ref y);
1687

    
1688
                placeRunInputs.AddSymbolTarget(targetLMConnector.ConnectItem1SymbolObject, x, y);
1689
                placeRunInputs.AddSymbolTarget(targetLMConnector.ConnectItem2SymbolObject, x, y);
1690

    
1691
                _placement.PIDRemovePlacement(targetLMConnector.AsLMRepresentation());
1692
                _LMConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
1693

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

    
1697
                AutoJoinPipeRun(_LMConnector.ModelItemID);
1698
            }
1699
            else
1700
            {
1701
                List<double[]> vertices = new List<double[]>();
1702
                for (int i = 1; i <= verticesCount; i++)
1703
                {
1704
                    double x = 0;
1705
                    double y = 0;
1706
                    lineStringGeometry.GetVertex(i, ref x, ref y);
1707
                    vertices.Add(new double[] { x, y });
1708
                }
1709

    
1710
                for (int i = 0; i < vertices.Count; i++)
1711
                {
1712
                    double[] points = vertices[i];
1713
                    if (i == 0)
1714
                    {
1715
                        if (targetLMConnector.ConnectItem1SymbolObject != null)
1716
                            placeRunInputs.AddSymbolTarget(targetLMConnector.ConnectItem1SymbolObject, points[0], points[1]);
1717
                        else
1718
                            placeRunInputs.AddPoint(points[0], points[1]);
1719
                    }
1720
                    else if (i == vertices.Count - 1)
1721
                    {
1722
                        if (targetLMConnector.ConnectItem2SymbolObject != null)
1723
                            placeRunInputs.AddSymbolTarget(targetLMConnector.ConnectItem2SymbolObject, points[0], points[1]);
1724
                        else
1725
                            placeRunInputs.AddPoint(points[0], points[1]);
1726
                    }
1727
                    else
1728
                        placeRunInputs.AddPoint(points[0], points[1]);
1729
                }
1730

    
1731
                List<Line> lines = SPPIDUtil.FindLinesByModelId(document, targetLMConnector.ModelItemID);
1732
                
1733
                _placement.PIDRemovePlacement(targetLMConnector.AsLMRepresentation());
1734
                _LMConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
1735

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

    
1739
                AutoJoinPipeRun(_LMConnector.ModelItemID);
1740
                foreach (var line in lines)
1741
                    line.SPPID.ModelItemId = _LMConnector.ModelItemID;
1742
            }
1743

    
1744

    
1745
            if (_LMLabelPersist != null)
1746
            {
1747
                _LMLabelPersist.Commit();
1748
                ReleaseCOMObjects(_LMLabelPersist);
1749
            }
1750
            else
1751
            {
1752
                
1753
            }
1754

    
1755
            ReleaseCOMObjects(_LMConnector);
1756
            ReleaseCOMObjects(placeRunInputs);
1757
            ReleaseCOMObjects(_LMAItem);
1758
        }
1759

    
1760
        private LMConnector ReModelingLMConnector(LMConnector connector)
1761
        {
1762
            string symbolPath = string.Empty;
1763
            #region get symbol path
1764
            LMModelItem modelItem = dataSource.GetModelItem(connector.ModelItemID);
1765
            foreach (LMRepresentation rep in modelItem.Representations)
1766
            {
1767
                if (!DBNull.Value.Equals(rep.get_FileName()) && !string.IsNullOrEmpty(rep.get_FileName()))
1768
                {
1769
                    symbolPath = rep.get_FileName();
1770
                    break;
1771
                }
1772
            }
1773
            #endregion
1774

    
1775
            LMConnector newConnector = null;
1776
            dynamic OID = connector.get_GraphicOID();
1777
            DependencyObject drawingObject = radApp.ActiveDocument.ActiveSheet.DrawingObjects[OID];
1778
            Ingr.RAD2D.LineStringGeometry2d lineStringGeometry = drawingObject.GetGeometry() as Ingr.RAD2D.LineStringGeometry2d;
1779
            int verticesCount = lineStringGeometry.VertexCount;
1780
            PlaceRunInputs placeRunInputs = new PlaceRunInputs();
1781
            _LMAItem _LMAItem = _placement.PIDCreateItem(symbolPath);
1782

    
1783
            if (Convert.ToBoolean(connector.get_IsZeroLength()))
1784
            {
1785
                double[] vertices = null;
1786
                lineStringGeometry.GetVertices(ref verticesCount, ref vertices);
1787
                double x = 0;
1788
                double y = 0;
1789
                lineStringGeometry.GetVertex(1, ref x, ref y);
1790

    
1791
                placeRunInputs.AddSymbolTarget(connector.ConnectItem1SymbolObject, x, y);
1792
                placeRunInputs.AddSymbolTarget(connector.ConnectItem2SymbolObject, x, y);
1793

    
1794
                _placement.PIDRemovePlacement(connector.AsLMRepresentation());
1795
                newConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
1796
            }
1797
            else
1798
            {
1799
                List<double[]> vertices = new List<double[]>();
1800
                for (int i = 1; i <= verticesCount; i++)
1801
                {
1802
                    double x = 0;
1803
                    double y = 0;
1804
                    lineStringGeometry.GetVertex(i, ref x, ref y);
1805
                    vertices.Add(new double[] { x, y });
1806
                }
1807

    
1808
                for (int i = 0; i < vertices.Count; i++)
1809
                {
1810
                    double[] points = vertices[i];
1811
                    if (i == 0)
1812
                    {
1813
                        if (connector.ConnectItem1SymbolObject != null)
1814
                            placeRunInputs.AddSymbolTarget(connector.ConnectItem1SymbolObject, points[0], points[1]);
1815
                        else
1816
                            placeRunInputs.AddPoint(points[0], points[1]);
1817
                    }
1818
                    else if (i == vertices.Count - 1)
1819
                    {
1820
                        if (connector.ConnectItem2SymbolObject != null)
1821
                            placeRunInputs.AddSymbolTarget(connector.ConnectItem2SymbolObject, points[0], points[1]);
1822
                        else
1823
                            placeRunInputs.AddPoint(points[0], points[1]);
1824
                    }
1825
                    else
1826
                        placeRunInputs.AddPoint(points[0], points[1]);
1827
                }
1828

    
1829
                List<Line> lines = SPPIDUtil.FindLinesByModelId(document, connector.ModelItemID);
1830

    
1831
                _placement.PIDRemovePlacement(connector.AsLMRepresentation());
1832
                newConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
1833

    
1834
                foreach (var line in lines)
1835
                    line.SPPID.ModelItemId = newConnector.ModelItemID;
1836
            }
1837

    
1838

    
1839
            return newConnector;
1840
        }
1841

    
1842
        /// <summary>
1843
        /// SpecBreak Modeling 메서드
1844
        /// </summary>
1845
        /// <param name="specBreak"></param>
1846
        private void SpecBreakModeling(SpecBreak specBreak)
1847
        {
1848
            object upStreamObj = SPPIDUtil.FindObjectByUID(document, specBreak.UpStreamUID);
1849
            object downStreamObj = SPPIDUtil.FindObjectByUID(document, specBreak.DownStreamUID);
1850

    
1851
            if (upStreamObj != null &&
1852
                downStreamObj != null)
1853
            {
1854
                LMConnector targetLMConnector = FindBreakLineTarget(upStreamObj, downStreamObj);
1855

    
1856
                if (targetLMConnector != null)
1857
                {
1858
                    string MappingPath = specBreak.SPPID.MAPPINGNAME;
1859
                    Array array = new double[] { 0, specBreak.SPPID.ORIGINAL_X, specBreak.SPPID.ORIGINAL_Y };
1860
                    LMLabelPersist _LmLabelPersist = _placement.PIDPlaceLabel(MappingPath, ref array, Rotation: specBreak.ANGLE, LabeledItem: targetLMConnector.AsLMRepresentation(), IsLeaderVisible: true);
1861

    
1862
                    if (_LmLabelPersist != null)
1863
                    {
1864
                        specBreak.SPPID.RepresentationId = _LmLabelPersist.AsLMRepresentation().Id;
1865
                        ReleaseCOMObjects(_LmLabelPersist);
1866
                    }
1867

    
1868
                    ReleaseCOMObjects(targetLMConnector);
1869
                }
1870
            }
1871
        }
1872

    
1873
        private LMConnector FindBreakLineTarget(object targetObj, object connectedObj)
1874
        {
1875
            LMConnector targetConnector = null;
1876
            Symbol targetSymbol = targetObj as Symbol;
1877
            Symbol connectedSymbol = connectedObj as Symbol;
1878
            Line targetLine = targetObj as Line;
1879
            Line connectedLine = connectedObj as Line;
1880
            if (targetSymbol != null && connectedSymbol != null)
1881
            {
1882
                LMSymbol targetLMSymbol = dataSource.GetSymbol(targetSymbol.SPPID.RepresentationId);
1883
                LMSymbol connectedLMSymbol = dataSource.GetSymbol(connectedSymbol.SPPID.RepresentationId);
1884

    
1885
                foreach (LMConnector connector in targetLMSymbol.Avoid1Connectors)
1886
                {
1887
                    if (connector.get_ItemStatus() != "Active")
1888
                        continue;
1889

    
1890
                    if (connector.ConnectItem1SymbolObject.Id == connectedLMSymbol.Id)
1891
                    {
1892
                        targetConnector = connector;
1893
                        break;
1894
                    }
1895
                    else if (connector.ConnectItem2SymbolObject.Id == connectedLMSymbol.Id)
1896
                    {
1897
                        targetConnector = connector;
1898
                        break;
1899
                    }
1900
                }
1901

    
1902
                foreach (LMConnector connector in targetLMSymbol.Avoid2Connectors)
1903
                {
1904
                    if (connector.get_ItemStatus() != "Active")
1905
                        continue;
1906

    
1907
                    if (connector.ConnectItem1SymbolObject.Id == connectedLMSymbol.Id)
1908
                    {
1909
                        targetConnector = connector;
1910
                        break;
1911
                    }
1912
                    else if (connector.ConnectItem2SymbolObject.Id == connectedLMSymbol.Id)
1913
                    {
1914
                        targetConnector = connector;
1915
                        break;
1916
                    }
1917
                }
1918

    
1919
                ReleaseCOMObjects(targetLMSymbol);
1920
                ReleaseCOMObjects(connectedLMSymbol);
1921
            }
1922
            else if (targetLine != null && connectedLine != null)
1923
            {
1924
                LMModelItem targetModelItem = dataSource.GetModelItem(targetLine.SPPID.ModelItemId);
1925
                LMModelItem connectedModelItem = dataSource.GetModelItem(connectedLine.SPPID.ModelItemId);
1926

    
1927
                if (targetModelItem != null && connectedModelItem != null)
1928
                {
1929
                    foreach (LMRepresentation rep in targetModelItem.Representations)
1930
                    {
1931
                        if (targetConnector != null)
1932
                            break;
1933

    
1934
                        if (rep.Attributes["RepresentationType"].get_Value() == "Connector" && rep.Attributes["ItemStatus"].get_Value() == "Active")
1935
                        {
1936
                            LMConnector _LMConnector = dataSource.GetConnector(rep.Id);
1937

    
1938
                            if (IsConnected(_LMConnector, connectedModelItem))
1939
                                targetConnector = _LMConnector;
1940
                            else
1941
                                ReleaseCOMObjects(_LMConnector);
1942
                        }
1943
                    }
1944

    
1945
                    ReleaseCOMObjects(targetModelItem);
1946
                }
1947
            }
1948
            else
1949
            {
1950
                LMSymbol connectedLMSymbol = connectedSymbol != null ? 
1951
                    dataSource.GetSymbol(connectedSymbol.SPPID.RepresentationId) : dataSource.GetSymbol(targetSymbol.SPPID.RepresentationId);
1952
                LMModelItem targetModelItem = targetLine != null ?
1953
                    dataSource.GetModelItem(targetLine.SPPID.ModelItemId) : dataSource.GetModelItem(connectedLine.SPPID.ModelItemId);
1954
                if (connectedLMSymbol != null && targetModelItem != null)
1955
                {
1956
                    foreach (LMConnector connector in connectedLMSymbol.Avoid1Connectors)
1957
                    {
1958
                        if (IsConnected(connector, targetModelItem))
1959
                        {
1960
                            targetConnector = connector;
1961
                            break;
1962
                        }
1963
                    }
1964

    
1965
                    if (targetConnector == null)
1966
                    {
1967
                        foreach (LMConnector connector in connectedLMSymbol.Avoid2Connectors)
1968
                        {
1969
                            if (IsConnected(connector, targetModelItem))
1970
                            {
1971
                                targetConnector = connector;
1972
                                break;
1973
                            }
1974
                        }
1975
                    }
1976
                }
1977

    
1978
            }
1979

    
1980
            return targetConnector;
1981
        }
1982

    
1983
        private bool IsConnected(LMConnector connector, LMModelItem modelItem)
1984
        {
1985
            bool result = false;
1986

    
1987
            foreach (LMRepresentation rep in modelItem.Representations)
1988
            {
1989
                if (result)
1990
                    break;
1991

    
1992
                if (rep.Attributes["RepresentationType"].get_Value() == "Connector" && rep.Attributes["ItemStatus"].get_Value() == "Active")
1993
                {
1994
                    LMConnector _LMConnector = dataSource.GetConnector(rep.Id);
1995

    
1996
                    if (_LMConnector.ConnectItem1SymbolObject != null &&
1997
                        connector.ConnectItem1SymbolObject != null &&
1998
                        _LMConnector.ConnectItem1SymbolObject.Id == connector.ConnectItem1SymbolObject.Id)
1999
                    {
2000
                        result = true;
2001
                        ReleaseCOMObjects(_LMConnector);
2002
                        break;
2003
                    }
2004
                    else if (_LMConnector.ConnectItem1SymbolObject != null &&
2005
                        connector.ConnectItem2SymbolObject != null &&
2006
                        _LMConnector.ConnectItem1SymbolObject.Id == connector.ConnectItem2SymbolObject.Id)
2007
                    {
2008
                        result = true;
2009
                        ReleaseCOMObjects(_LMConnector);
2010
                        break;
2011
                    }
2012
                    else if (_LMConnector.ConnectItem2SymbolObject != null &&
2013
                        connector.ConnectItem1SymbolObject != null &&
2014
                        _LMConnector.ConnectItem2SymbolObject.Id == connector.ConnectItem1SymbolObject.Id)
2015
                    {
2016
                        result = true;
2017
                        ReleaseCOMObjects(_LMConnector);
2018
                        break;
2019
                    }
2020
                    else if (_LMConnector.ConnectItem2SymbolObject != null &&
2021
                        connector.ConnectItem2SymbolObject != null &&
2022
                        _LMConnector.ConnectItem2SymbolObject.Id == connector.ConnectItem2SymbolObject.Id)
2023
                    {
2024
                        result = true;
2025
                        ReleaseCOMObjects(_LMConnector);
2026
                        break;
2027
                    }
2028

    
2029
                    ReleaseCOMObjects(_LMConnector);
2030
                }
2031
            }
2032

    
2033

    
2034
            return result;
2035
        }
2036

    
2037
        /// <summary>
2038
        /// FromModelItem을 ToModelItem으로 PipeRunJoin하는 메서드
2039
        /// </summary>
2040
        /// <param name="fromModelItemId"></param>
2041
        /// <param name="toModelItemId"></param>
2042
        private void JoinPipeRun(string fromModelItemId, string toModelItemId)
2043
        {
2044
            LMModelItem modelItem1 = dataSource.GetModelItem(toModelItemId);
2045
            _LMAItem item1 = modelItem1.AsLMAItem();
2046
            LMModelItem modelItem2 = dataSource.GetModelItem(fromModelItemId);
2047
            _LMAItem item2 = modelItem2.AsLMAItem();
2048
            
2049
            // item2가 item1으로 조인
2050
            try
2051
            {
2052
                _placement.PIDJoinRuns(ref item1, ref item2);
2053
                item1.Commit();
2054
                item2.Commit();
2055

    
2056
                List<Line> lines = SPPIDUtil.FindLinesByModelId(document, fromModelItemId);
2057
                foreach (var line in lines)
2058
                    line.SPPID.ModelItemId = toModelItemId;
2059
            }
2060
            catch (Exception ex)
2061
            {
2062
                System.Windows.Forms.MessageBox.Show(ex.Message + "\r\n" + ex.StackTrace);
2063
            }
2064
            finally
2065
            {
2066
                ReleaseCOMObjects(modelItem1);
2067
                ReleaseCOMObjects(item1);
2068
                ReleaseCOMObjects(modelItem2);
2069
                ReleaseCOMObjects(item2);
2070
            }
2071
        }
2072

    
2073
        /// <summary>
2074
        /// PipeRun을 자동으로 Join하는 메서드
2075
        /// </summary>
2076
        /// <param name="modelItemId"></param>
2077
        private void AutoJoinPipeRun(string modelItemId)
2078
        {
2079
            LMModelItem modelItem = dataSource.GetModelItem(modelItemId);
2080
            _LMAItem item = modelItem.AsLMAItem();
2081
            try
2082
            {
2083
                string modelitemID = item.Id;
2084
                _placement.PIDAutoJoin(item, AutoJoinEndConstants.autoJoin_Both, ref item);
2085
                string afterModelItemID = item.Id;
2086
                
2087
                if (modelitemID != afterModelItemID)
2088
                {
2089
                    List<Line> lines = SPPIDUtil.FindLinesByModelId(document, modelitemID);
2090
                    foreach (var line in lines)
2091
                        line.SPPID.ModelItemId = afterModelItemID;
2092
                }
2093
                item.Commit();
2094
            }
2095
            catch (Exception ex)
2096
            {
2097
                System.Windows.Forms.MessageBox.Show(ex.Message + "\r\n" + ex.StackTrace);
2098
            }
2099
            finally
2100
            {
2101
                ReleaseCOMObjects(modelItem);
2102
                ReleaseCOMObjects(item);
2103
            }
2104
        }
2105

    
2106
        /// <summary>
2107
        /// LineRun에 있는 Line들을 Join하는 진입 메서드
2108
        /// </summary>
2109
        /// <param name="run"></param>
2110
        private void JoinRunLine(LineRun run)
2111
        {
2112
            string modelItemId = string.Empty;
2113
            foreach (var item in run.RUNITEMS)
2114
            {
2115
                if (item.GetType() == typeof(Line))
2116
                {
2117
                    Line line = item as Line;
2118
                    AutoJoinPipeRun(line.SPPID.ModelItemId);
2119
                    modelItemId = line.SPPID.ModelItemId;
2120

    
2121
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetProgress, ++CurrentCount);
2122
                }
2123
            }
2124
        }
2125

    
2126
        /// <summary>
2127
        /// PipeRun의 좌표를 가져오는 메서드
2128
        /// </summary>
2129
        /// <param name="modelId"></param>
2130
        /// <returns></returns>
2131
        private Dictionary<LMConnector, List<double[]>> GetPipeRunVertices(string modelId)
2132
        {
2133
            Dictionary<LMConnector, List<double[]>> connectorVertices = new Dictionary<LMConnector, List<double[]>>();
2134
            LMModelItem modelItem = dataSource.GetModelItem(modelId);
2135

    
2136
            if (modelItem != null)
2137
            {
2138
                foreach (LMRepresentation rep in modelItem.Representations)
2139
                {
2140
                    if (rep.Attributes["RepresentationType"].get_Value() == "Connector" && rep.Attributes["ItemStatus"].get_Value() == "Active")
2141
                    {
2142
                        LMConnector _LMConnector = dataSource.GetConnector(rep.Id);
2143
                        connectorVertices.Add(_LMConnector, new List<double[]>());
2144
                        dynamic OID = rep.get_GraphicOID();
2145
                        DependencyObject drawingObject = radApp.ActiveDocument.ActiveSheet.DrawingObjects[OID];
2146
                        Ingr.RAD2D.LineStringGeometry2d lineStringGeometry = drawingObject.GetGeometry() as Ingr.RAD2D.LineStringGeometry2d;
2147
                        int verticesCount = lineStringGeometry.VertexCount;
2148
                        double[] vertices = null;
2149
                        lineStringGeometry.GetVertices(ref verticesCount, ref vertices);
2150
                        for (int i = 0; i < verticesCount; i++)
2151
                        {
2152
                            double x = 0;
2153
                            double y = 0;
2154
                            lineStringGeometry.GetVertex(i + 1, ref x, ref y);
2155
                            connectorVertices[_LMConnector].Add(new double[] { Math.Round(x, 10), Math.Round(y, 10) });
2156
                        }
2157
                    }
2158
                }
2159

    
2160
                ReleaseCOMObjects(modelItem);
2161
            }
2162

    
2163
            return connectorVertices;
2164
        }
2165

    
2166
        /// <summary>
2167
        /// LMConnector들을 가져온다.
2168
        /// </summary>
2169
        /// <param name="modelId"></param>
2170
        /// <returns></returns>
2171
        private List<LMConnector> GetConnectors(string modelId)
2172
        {
2173
            List<LMConnector> connectors = new List<LMConnector>();
2174
            LMModelItem modelItem = dataSource.GetModelItem(modelId);
2175

    
2176
            if (modelItem != null)
2177
            {
2178
                foreach (LMRepresentation rep in modelItem.Representations)
2179
                {
2180
                    if (rep.Attributes["RepresentationType"].get_Value() == "Connector" && rep.Attributes["ItemStatus"].get_Value() == "Active")
2181
                    {
2182
                        LMConnector _LMConnector = dataSource.GetConnector(rep.Id);
2183
                        connectors.Add(_LMConnector);
2184
                    }
2185
                }
2186

    
2187
                ReleaseCOMObjects(modelItem);
2188
            }
2189

    
2190
            return connectors;
2191
        }
2192

    
2193
        /// <summary>
2194
        /// 좌표로 PipeRun의 Connector중에 어느 Connector에 가까운지/붙을지 가져오는 메서드 - 두점으로 라인의 교차점을 기준으로 구함
2195
        /// </summary>
2196
        /// <param name="connectorVertices"></param>
2197
        /// <param name="connX"></param>
2198
        /// <param name="connY"></param>
2199
        /// <param name="x2"></param>
2200
        /// <param name="y2"></param>
2201
        /// <returns></returns>
2202
        private LMConnector FindTargetLMConnector(Dictionary<LMConnector, List<double[]>> connectorVertices, double connX, double connY, double x2, double y2)
2203
        {
2204
            double length = double.MaxValue;
2205
            LMConnector targetConnector = null;
2206
            foreach (var item in connectorVertices)
2207
            {
2208
                List<double[]> points = item.Value;
2209
                for (int i = 0; i < points.Count - 1; i++)
2210
                {
2211
                    double[] point1 = points[i];
2212
                    double[] point2 = points[i + 1];
2213

    
2214
                    double maxLineX = Math.Max(point1[0], point2[0]);
2215
                    double minLineX = Math.Min(point1[0], point2[0]);
2216
                    double maxLineY = Math.Max(point1[1], point2[1]);
2217
                    double minLineY = Math.Min(point1[1], point2[1]);
2218

    
2219
                    SlopeType slope = SPPIDUtil.CalcSlope(minLineX, minLineY, maxLineX, maxLineY);
2220

    
2221
                    // 두직선의 교차점
2222
                    double[] crossingPoint = SPPIDUtil.CalcLineCrossingPoint(connX, connY, x2, y2, point1[0], point1[1], point2[0], point2[1]);
2223
                    if (crossingPoint != null)
2224
                    {
2225
                        double distance = SPPIDUtil.CalcPointToPointdDistance(connX, connY, crossingPoint[0], crossingPoint[1]);
2226
                        if (length >= distance)
2227
                        {
2228
                            if (slope == SlopeType.Slope &&
2229
                                minLineX <= crossingPoint[0] && maxLineX >= crossingPoint[0] &&
2230
                                minLineY <= crossingPoint[1] && maxLineY >= crossingPoint[1])
2231
                            {
2232
                                targetConnector = item.Key;
2233
                                length = distance;
2234
                            }
2235
                            else if (slope == SlopeType.HORIZONTAL &&
2236
                                minLineX <= crossingPoint[0] && maxLineX >= crossingPoint[0])
2237
                            {
2238
                                targetConnector = item.Key;
2239
                                length = distance;
2240
                            }
2241
                            else if (slope == SlopeType.VERTICAL &&
2242
                               minLineY <= crossingPoint[1] && maxLineY >= crossingPoint[1])
2243
                            {
2244
                                targetConnector = item.Key;
2245
                                length = distance;
2246
                            }
2247
                        }
2248
                    }
2249
                }
2250

    
2251

    
2252
            }
2253

    
2254
            if (targetConnector == null)
2255
            {
2256
                foreach (var item in connectorVertices)
2257
                {
2258
                    List<double[]> points = item.Value;
2259
                    foreach (var point in points)
2260
                    {
2261
                        double distance = SPPIDUtil.CalcPointToPointdDistance(connX, connY, point[0], point[1]);
2262
                        if (length >= distance)
2263
                        {
2264
                            targetConnector = item.Key;
2265
                            length = distance;
2266
                        }
2267
                    }
2268
                }
2269

    
2270
            }
2271

    
2272
            return targetConnector;
2273
        }
2274

    
2275
        /// <summary>
2276
        /// 좌표로 PipeRun의 Connector중에 어느 Connector에 가까운지/붙을지 가져오는 메서드 - 한점으로 제일 가까운 기준으로 구함(단순)
2277
        /// </summary>
2278
        /// <param name="connectorVertices"></param>
2279
        /// <param name="connX"></param>
2280
        /// <param name="connY"></param>
2281
        /// <returns></returns>
2282
        private LMConnector FindTargetLMConnectorByPoint(Dictionary<LMConnector, List<double[]>> connectorVertices, double connX, double connY)
2283
        {
2284
            double length = double.MaxValue;
2285
            LMConnector targetConnector = null;
2286
            foreach (var item in connectorVertices)
2287
            {
2288
                List<double[]> points = item.Value;
2289

    
2290
                foreach (double[] point in points)
2291
                {
2292
                    double distance = SPPIDUtil.CalcPointToPointdDistance(point[0], point[1], connX, connY);
2293
                    if (length >= distance)
2294
                    {
2295
                        targetConnector = item.Key;
2296
                        length = distance;
2297
                    }
2298
                }
2299
            }
2300

    
2301
            return targetConnector;
2302
        }
2303

    
2304
        /// <summary>
2305
        /// 좌표로 PipeRun의 Connector중에 어느 Connector에 가까운지/붙을지 가져오는 메서드 - 조건에 안맞아서 못찾을시 제일 가까운 점으로 가져오는 방식
2306
        /// </summary>
2307
        /// <param name="connectorVertices"></param>
2308
        /// <param name="connX"></param>
2309
        /// <param name="connY"></param>
2310
        /// <returns></returns>
2311
        private LMConnector FindTargetLMConnectorForLabel(Dictionary<LMConnector, List<double[]>> connectorVertices, double connX, double connY)
2312
        {
2313
            double length = double.MaxValue;
2314
            LMConnector targetConnector = null;
2315
            foreach (var item in connectorVertices)
2316
            {
2317
                List<double[]> points = item.Value;
2318
                for (int i = 0; i < points.Count - 1; i++)
2319
                {
2320
                    double[] point1 = points[i];
2321
                    double[] point2 = points[i + 1];
2322
                    double x1 = Math.Min(point1[0], point2[0]);
2323
                    double y1 = Math.Min(point1[1], point2[1]);
2324
                    double x2 = Math.Max(point1[0], point2[0]);
2325
                    double y2 = Math.Max(point1[1], point2[1]);
2326

    
2327
                    if ((x1 <= connX && x2 >= connX) ||
2328
                        (y1 <= connY && y2 >= connY))
2329
                    {
2330
                        double distance = SPPIDUtil.CalcPointToPointdDistance(point1[0], point1[1], connX, connY);
2331
                        if (length >= distance)
2332
                        {
2333
                            targetConnector = item.Key;
2334
                            length = distance;
2335
                        }
2336

    
2337
                        distance = SPPIDUtil.CalcPointToPointdDistance(point2[0], point2[1], connX, connY);
2338
                        if (length >= distance)
2339
                        {
2340
                            targetConnector = item.Key;
2341
                            length = distance;
2342
                        }
2343
                    }
2344
                }
2345
            }
2346

    
2347
            // 못찾았을때.
2348
            length = double.MaxValue;
2349
            if (targetConnector == null)
2350
            {
2351
                foreach (var item in connectorVertices)
2352
                {
2353
                    List<double[]> points = item.Value;
2354

    
2355
                    foreach (double[] point in points)
2356
                    {
2357
                        double distance = SPPIDUtil.CalcPointToPointdDistance(point[0], point[1], connX, connY);
2358
                        if (length >= distance)
2359
                        {
2360
                            targetConnector = item.Key;
2361
                            length = distance;
2362
                        }
2363
                    }
2364
                }
2365
            }
2366

    
2367
            return targetConnector;
2368
        }
2369

    
2370
        /// <summary>
2371
        /// Line Number Symbol을 실제로 Modeling하는 메서드
2372
        /// </summary>
2373
        /// <param name="lineNumber"></param>
2374
        private void LineNumberModeling(LineNumber lineNumber)
2375
        {
2376
            Line line = SPPIDUtil.FindObjectByUID(document, lineNumber.CONNLINE) as Line;
2377
            Dictionary<LMConnector, List<double[]>> connectorVertices = GetPipeRunVertices(line.SPPID.ModelItemId);
2378
            LMConnector connectedLMConnector = FindTargetLMConnectorForLabel(connectorVertices, lineNumber.SPPID.ORIGINAL_X, lineNumber.SPPID.ORIGINAL_Y);
2379
            if (connectedLMConnector != null)
2380
            {
2381
                double x = 0;
2382
                double y = 0;
2383
                CalcLabelLocation(ref x, ref y, lineNumber.SPPID.ORIGINAL_X, lineNumber.SPPID.ORIGINAL_Y, lineNumber.SPPIDLabelLocation, _ETCSetting.LineNumberLocation);
2384

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

    
2388
                foreach (var item in connectorVertices)
2389
                    ReleaseCOMObjects(item.Key);
2390
                if (_LmLabelPresist != null)
2391
                {
2392
                    _LmLabelPresist.Commit();
2393
                    lineNumber.SPPID.RepresentationId = _LmLabelPresist.AsLMRepresentation().Id;
2394
                    ReleaseCOMObjects(_LmLabelPresist);
2395
                }
2396
                else
2397
                {
2398

    
2399
                }
2400
            }
2401

    
2402
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetProgress, ++CurrentCount);
2403
        }
2404

    
2405
        /// <summary>
2406
        /// Flow Mark Modeling
2407
        /// </summary>
2408
        /// <param name="line"></param>
2409
        private void FlowMarkModeling(Line line)
2410
        {
2411
            if (line.FLOWMARK && !string.IsNullOrEmpty(line.SPPID.ModelItemId) && !string.IsNullOrEmpty(_ETCSetting.FlowMarkSymbolPath))
2412
            {
2413
                SlopeType targetSlopeType = SPPIDUtil.CalcSlope(line.SPPID.START_X, line.SPPID.START_Y, line.SPPID.END_X, line.SPPID.END_Y);
2414
                string mappingPath = _ETCSetting.FlowMarkSymbolPath;
2415
                double percent = line.FLOWMARK_PERCENT;
2416
                double tempX = 0;
2417
                double tempY = 0;
2418

    
2419
                double gapX;
2420
                double gapY;
2421
                // ID2 기준의 Gap을 구함
2422
                if (percent == 0)
2423
                {
2424
                    gapX = 0;
2425
                    gapY = 0;
2426
                }
2427
                else
2428
                {
2429
                    gapX = Math.Abs(line.SPPID.START_X - line.SPPID.END_X) / 100 * percent;
2430
                    gapY = Math.Abs(line.SPPID.START_Y - line.SPPID.END_Y) / 100 * percent;
2431
                }
2432

    
2433
                if (line.SPPID.START_X < line.SPPID.END_X)
2434
                    tempX = line.SPPID.START_X + gapX;
2435
                else
2436
                    tempX = line.SPPID.START_X - gapX;
2437

    
2438
                if (line.SPPID.START_Y < line.SPPID.END_Y)
2439
                    tempY = line.SPPID.START_Y + gapY;
2440
                else
2441
                    tempY = line.SPPID.START_Y - gapY;
2442

    
2443
                
2444
               
2445
                Dictionary<LMConnector, List<double[]>> connectorVertices = GetPipeRunVertices(line.SPPID.ModelItemId);
2446
                LMConnector _TargetItem = null;
2447
                double distance = double.MaxValue;
2448
                double[] startPoint = null;
2449
                double[] endPoint = null;
2450
                // ID2의 기준 Gap으로 제일 가까운 Line 찾음(Slope도 같은조건)
2451
                foreach (var item in connectorVertices)
2452
                {
2453
                    for (int i = 0; i < item.Value.Count - 1; i++)
2454
                    {
2455
                        List<double[]> points = item.Value;
2456
                        double[] point1 = points[i];
2457
                        double[] point2 = points[i + 1];
2458

    
2459
                        SlopeType slopeType = SPPIDUtil.CalcSlope(point1[0], point1[1], point2[0], point2[1]);
2460
                        if (slopeType == targetSlopeType)
2461
                        {
2462
                            double result = SPPIDUtil.CalcPointToPointdDistance(point1[0], point1[1], tempX, tempY);
2463
                            if (result < distance)
2464
                            {
2465
                                distance = result;
2466
                                _TargetItem = item.Key;
2467

    
2468
                                startPoint = point1;
2469
                                endPoint = point2;
2470
                            }
2471

    
2472
                            result = SPPIDUtil.CalcPointToPointdDistance(point2[0], point2[1], tempX, tempY);
2473
                            if (result < distance)
2474
                            {
2475
                                distance = result;
2476
                                _TargetItem = item.Key;
2477

    
2478
                                startPoint = point1;
2479
                                endPoint = point2;
2480
                            }
2481
                        }
2482
                    }
2483
                }
2484

    
2485
                if (_TargetItem != null)
2486
                {
2487
                    double x = 0;
2488
                    double y = 0;
2489
                    double angle = 0;
2490
                    // SPPID 기준의 Gap으로 실 좌표를 구함
2491
                    if (percent == 0)
2492
                    {
2493
                        gapX = 0;
2494
                        gapY = 0;
2495
                    }
2496
                    else
2497
                    {
2498
                        gapX = Math.Abs(startPoint[0] - endPoint[0]) / 100 * percent;
2499
                        gapY = Math.Abs(startPoint[1] - endPoint[1]) / 100 * percent;
2500
                    }
2501

    
2502
                    if (startPoint[0] < endPoint[0])
2503
                        x = startPoint[0] + gapX;
2504
                    else
2505
                        x = startPoint[0] - gapX;
2506

    
2507
                    if (startPoint[1] < endPoint[1])
2508
                        y = startPoint[1] + gapY;
2509
                    else
2510
                        y = startPoint[1] - gapY;
2511
                    
2512
                    if (targetSlopeType == SlopeType.HORIZONTAL)
2513
                    {
2514
                        if (startPoint[0] < endPoint[0])
2515
                            angle = 0;
2516
                        else
2517
                            angle = Math.PI;
2518
                    }
2519
                    // 90 270
2520
                    else if (targetSlopeType == SlopeType.VERTICAL)
2521
                    {
2522
                        if (startPoint[1] < endPoint[1])
2523
                            angle = 90 * Math.PI / 180;
2524
                        else
2525
                            angle = 270 * Math.PI / 180;
2526
                    }
2527

    
2528
                    LMSymbol _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: 0, Rotation: angle, TargetItem: _TargetItem);
2529
                    
2530
                    if (_LMSymbol != null)
2531
                    {
2532
                        ReleaseCOMObjects(_LMSymbol);
2533
                    }
2534
                        
2535
                }
2536

    
2537
                foreach (var item in connectorVertices)
2538
                    ReleaseCOMObjects(item.Key);
2539
            }
2540
        }
2541

    
2542
        /// <summary>
2543
        /// Line Number 기준으로 모든 Item에 Line Number의 Attribute Input
2544
        /// </summary>
2545
        /// <param name="lineNumber"></param>
2546
        private void InputLineNumberAttribute(LineNumber lineNumber)
2547
        {
2548
            foreach (LineRun run in lineNumber.RUNS)
2549
            {
2550
                foreach (var item in run.RUNITEMS)
2551
                {
2552
                    if (item.GetType() == typeof(Symbol))
2553
                    {
2554
                        Symbol symbol = item as Symbol;
2555
                        LMSymbol _LMSymbol = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
2556
                        LMModelItem _LMModelItem = _LMSymbol.ModelItemObject;
2557

    
2558
                        if (_LMModelItem != null && _LMModelItem.get_ItemStatus() == "Active")
2559
                        {
2560
                            foreach (var attribute in lineNumber.ATTRIBUTES)
2561
                            {
2562
                                LineNumberMapping mapping = document.LineNumberMappings.Find(x => x.UID == attribute.UID);
2563
                                if (mapping != null && !string.IsNullOrEmpty(attribute.VALUE) && attribute.VALUE != "None")
2564
                                {
2565
                                    LMAAttribute _LMAAttribute = _LMModelItem.Attributes[mapping.SPPIDATTRIBUTENAME];
2566
                                    if (_LMAAttribute != null)
2567
                                    {
2568
                                        if (DBNull.Value.Equals(_LMAAttribute.get_Value()))
2569
                                            _LMAAttribute.set_Value(attribute.VALUE);
2570
                                        else if (_LMAAttribute.get_Value() != attribute.VALUE)
2571
                                            _LMAAttribute.set_Value(attribute.VALUE);
2572
                                    }
2573
                                }
2574
                            }
2575
                            _LMModelItem.Commit();
2576
                        }
2577
                        if (_LMModelItem != null)
2578
                            ReleaseCOMObjects(_LMModelItem);
2579
                        if (_LMSymbol != null)
2580
                            ReleaseCOMObjects(_LMSymbol);
2581
                    }
2582
                    else if (item.GetType() == typeof(Line))
2583
                    {
2584
                        Line line = item as Line;
2585
                        if (line != null)
2586
                        {
2587
                            LMModelItem _LMModelItem = dataSource.GetModelItem(line.SPPID.ModelItemId);
2588
                            if (_LMModelItem != null && _LMModelItem.get_ItemStatus() == "Active")
2589
                            {
2590
                                foreach (var attribute in lineNumber.ATTRIBUTES)
2591
                                {
2592
                                    LineNumberMapping mapping = document.LineNumberMappings.Find(x => x.UID == attribute.UID);
2593
                                    if (mapping != null && !string.IsNullOrEmpty(attribute.VALUE) && attribute.VALUE != "None")
2594
                                    {
2595
                                        LMAAttribute _LMAAttribute = _LMModelItem.Attributes[mapping.SPPIDATTRIBUTENAME];
2596
                                        if (_LMAAttribute != null)
2597
                                        {
2598
                                            if (DBNull.Value.Equals(_LMAAttribute.get_Value()))
2599
                                                _LMAAttribute.set_Value(attribute.VALUE);
2600
                                            else if (_LMAAttribute.get_Value() != attribute.VALUE)
2601
                                                _LMAAttribute.set_Value(attribute.VALUE);
2602
                                            
2603
                                        }
2604
                                    }
2605
                                }
2606
                                _LMModelItem.Commit();
2607
                            }
2608
                            if (_LMModelItem != null)
2609
                                ReleaseCOMObjects(_LMModelItem);
2610
                        }
2611
                    }
2612
                }
2613
            }
2614

    
2615
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetProgress, ++CurrentCount);
2616
        }
2617

    
2618
        /// <summary>
2619
        /// Symbol Attribute 입력 메서드
2620
        /// </summary>
2621
        /// <param name="item"></param>
2622
        private void InputSymbolAttribute(object targetItem, List<BaseModel.Attribute> targetAttributes)
2623
        {
2624
            try
2625
            {
2626
                // Object 아이템이 Symbol일 경우 Equipment일 경우 
2627
                string sRep = null;
2628
                if (targetItem.GetType() == typeof(Symbol))
2629
                    sRep = ((Symbol)targetItem).SPPID.RepresentationId;
2630
                else if (targetItem.GetType() == typeof(Equipment))
2631
                    sRep = ((Equipment)targetItem).SPPID.RepresentationId;
2632
                    
2633
                if (!string.IsNullOrEmpty(sRep))
2634
                {
2635
                    LMSymbol _LMSymbol = dataSource.GetSymbol(sRep);
2636
                    LMModelItem _LMModelItem = _LMSymbol.ModelItemObject;
2637
                    LMAAttributes _Attributes = _LMModelItem.Attributes;
2638

    
2639
                    foreach (var item in targetAttributes)
2640
                    {
2641
                        AttributeMapping mapping = document.AttributeMappings.Find(x => x.UID == item.UID);
2642
                        if (mapping != null && !string.IsNullOrEmpty(item.VALUE) && item.VALUE != "None")
2643
                        {
2644
                            LMAAttribute _Attribute = _Attributes[mapping.SPPIDATTRIBUTENAME];
2645
                            if (_Attribute != null)
2646
                                _Attribute.set_Value(item.VALUE);
2647
                        }
2648
                    }
2649
                    _LMModelItem.Commit();
2650
                    
2651
                    ReleaseCOMObjects(_Attributes);
2652
                    ReleaseCOMObjects(_LMModelItem);
2653
                    ReleaseCOMObjects(_LMSymbol);
2654
                }
2655
            }
2656
            catch (Exception ex)
2657
            {
2658
                System.Windows.Forms.MessageBox.Show(ex.Message + "\r\n" + ex.StackTrace);
2659
            }
2660

    
2661
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetProgress, ++CurrentCount);
2662
        }
2663

    
2664
        /// <summary>
2665
        /// Text Modeling - Association일 경우는 Text대신 해당 맵핑된 Symbol로 모델링
2666
        /// </summary>
2667
        /// <param name="text"></param>
2668
        private void TextModeling(Text text)
2669
        {
2670
            LMSymbol _LMSymbol = null;
2671
            try
2672
            {
2673
                //if (text.ASSOCIATION && !string.IsNullOrEmpty(text.OWNER) && text.OWNER != "None")
2674
                if (text.ASSOCIATION)
2675
                {
2676
                    object owner = SPPIDUtil.FindObjectByUID(document, text.OWNER);
2677
                    if (owner.GetType() == typeof(Symbol))
2678
                    {
2679
                        Symbol symbol = owner as Symbol;
2680
                        _LMSymbol = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
2681
                        if (_LMSymbol != null)
2682
                        {
2683
                            Association association = symbol.ASSOCIATIONS.Find(x => x.VALUE == text.UID);
2684
                            List<BaseModel.Attribute> attributes = symbol.ATTRIBUTES.FindAll(x => x.ATTRIBUTETYPE == association.TYPE);
2685
                            AttributeMapping mapping = null;
2686
                            foreach (var attribute in attributes)
2687
                            {
2688
                                if (string.IsNullOrEmpty(attribute.VALUE) || attribute.VALUE == "None")
2689
                                    continue;
2690

    
2691
                                 mapping = document.AttributeMappings.Find(x => x.UID == attribute.UID && !string.IsNullOrEmpty(x.SPPIDSYMBOLNAME));
2692
                                if (mapping != null)
2693
                                    break;  
2694
                            }
2695

    
2696
                            if (mapping != null)
2697
                            {
2698
                                double x = 0;
2699
                                double y = 0;
2700

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

    
2704
                                LMLabelPersist _LMLabelPersist = _placement.PIDPlaceLabel(mapping.SPPIDSYMBOLNAME, ref array, Rotation: text.ANGLE, LabeledItem: _LMSymbol.AsLMRepresentation(), IsLeaderVisible: mapping.LeaderLine);
2705
                                if (_LMLabelPersist!=null)
2706
                                {
2707
                                    _LMLabelPersist.Commit();
2708
                                    ReleaseCOMObjects(_LMLabelPersist);
2709
                                }
2710
                            }
2711
                        }
2712
                    }
2713
                    else if (owner.GetType() == typeof(Line))
2714
                    {
2715

    
2716
                    }
2717
                }
2718
                else
2719
                {
2720
                    LMItemNote _LMItemNote = null;
2721
                    LMAAttribute _LMAAttribute = null;
2722

    
2723
                    double x = 0;
2724
                    double y = 0;
2725

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

    
2728
                    _LMSymbol = _placement.PIDPlaceSymbol(text.SPPID.MAPPINGNAME, x, y);
2729
                    _LMSymbol.Commit();
2730
                    _LMItemNote = _placement.PIDDataSource.GetItemNote(_LMSymbol.ModelItemID);
2731
                    _LMItemNote.Commit();
2732
                    _LMAAttribute = _LMItemNote.Attributes["Note.Body"];
2733
                    _LMAAttribute.set_Value(text.VALUE);
2734
                    _LMItemNote.Commit();
2735

    
2736
                    if (_LMAAttribute != null)
2737
                        ReleaseCOMObjects(_LMAAttribute);
2738
                    if (_LMItemNote != null)
2739
                        ReleaseCOMObjects(_LMItemNote);
2740
                }
2741
            }
2742
            catch (Exception ex)
2743
            {
2744

    
2745
            }
2746
            finally
2747
            {
2748
                if (_LMSymbol != null)
2749
                    ReleaseCOMObjects(_LMSymbol);
2750
            }
2751

    
2752
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetProgress, ++CurrentCount);
2753
        }
2754

    
2755
        /// <summary>
2756
        /// Note Modeling
2757
        /// </summary>
2758
        /// <param name="note"></param>
2759
        private void NoteModeling(Note note)
2760
        {
2761
            LMSymbol _LMSymbol = null;
2762
            LMItemNote _LMItemNote = null;
2763
            LMAAttribute _LMAAttribute = null;
2764

    
2765
            try
2766
            {
2767
                double x = 0;
2768
                double y = 0;
2769

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

    
2772
                _LMSymbol = _placement.PIDPlaceSymbol(note.SPPID.MAPPINGNAME, x, y);
2773
                _LMSymbol.Commit();
2774
                _LMItemNote = _placement.PIDDataSource.GetItemNote(_LMSymbol.ModelItemID);
2775
                _LMItemNote.Commit();
2776
                _LMAAttribute = _LMItemNote.Attributes["Note.Body"];
2777
                _LMAAttribute.set_Value(note.VALUE);
2778
                _LMItemNote.Commit();
2779
            }
2780
            catch (Exception ex)
2781
            {
2782

    
2783
            }
2784
            finally
2785
            {
2786
                if (_LMAAttribute != null)
2787
                    ReleaseCOMObjects(_LMAAttribute);
2788
                if (_LMItemNote != null)
2789
                    ReleaseCOMObjects(_LMItemNote);
2790
                if (_LMSymbol != null)
2791
                    ReleaseCOMObjects(_LMSymbol);
2792
            }
2793

    
2794
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetProgress, ++CurrentCount);
2795
        }
2796

    
2797
        /// <summary>
2798
        /// Label의 좌표를 구하는 메서드(ID2 기준의 좌표 -> SPPID 좌표)
2799
        /// </summary>
2800
        /// <param name="x"></param>
2801
        /// <param name="y"></param>
2802
        /// <param name="originX"></param>
2803
        /// <param name="originY"></param>
2804
        /// <param name="SPPIDLabelLocation"></param>
2805
        /// <param name="location"></param>
2806
        private void CalcLabelLocation(ref double x, ref double y, double originX, double originY, SPPIDLabelLocationInfo SPPIDLabelLocation, Location location)
2807
        {
2808
            if (location == Location.None)
2809
            {
2810
                x = originX;
2811
                y = originY;
2812
            }
2813
            else
2814
            {
2815
                if (location.HasFlag(Location.Center))
2816
                {
2817
                    x = (SPPIDLabelLocation.X1 + SPPIDLabelLocation.X2) / 2;
2818
                    y = (SPPIDLabelLocation.Y1 + SPPIDLabelLocation.Y2) / 2;
2819
                }
2820

    
2821
                if (location.HasFlag(Location.Left))
2822
                    x = SPPIDLabelLocation.X1;
2823
                else if (location.HasFlag(Location.Right))
2824
                    x = SPPIDLabelLocation.X2;
2825

    
2826
                if (location.HasFlag(Location.Down))
2827
                    y = SPPIDLabelLocation.Y1;
2828
                else if (location.HasFlag(Location.Up))
2829
                    y = SPPIDLabelLocation.Y2;
2830
            }
2831
        }
2832

    
2833
        /// <summary>
2834
        /// Symbol의 우선순위 Modeling 목록을 가져온다.
2835
        /// 1. Angle Valve
2836
        /// 2. 3개로 이루어진 Symbol Group
2837
        /// </summary>
2838
        /// <returns></returns>
2839
        private List<Symbol> GetPrioritySymbol()
2840
        {
2841
            DataTable symbolTable = document.SymbolTable;
2842

    
2843
            // List에 순서대로 쌓는다.
2844
            List<Symbol> symbols = new List<Symbol>();
2845
            // Angle Valve 부터
2846
            foreach (var symbol in document.SYMBOLS.FindAll(x => x.CONNECTORS.Count == 2))
2847
            {
2848
                if (!symbols.Contains(symbol))
2849
                {
2850
                    double originX = 0;
2851
                    double originY = 0;
2852

    
2853
                    // ID2 Table에서 Original Point 가져옴.
2854
                    string OriginalPoint = symbolTable.Select(string.Format("UID = {0}",symbol.DBUID))[0]["OriginalPoint"].ToString();
2855
                    SPPIDUtil.ConvertPointBystring(OriginalPoint, ref originX, ref originY);
2856

    
2857
                    SlopeType slopeType1 = SlopeType.None;
2858
                    SlopeType slopeType2 = SlopeType.None;
2859
                    foreach (Connector connector in symbol.CONNECTORS)
2860
                    {
2861
                        double connectorX = 0;
2862
                        double connectorY = 0;
2863
                        SPPIDUtil.ConvertPointBystring(connector.CONNECTPOINT, ref connectorX, ref connectorY);
2864
                        if (slopeType1 == SlopeType.None)
2865
                            slopeType1 = SPPIDUtil.CalcSlope(originX, originY, connectorX, connectorY);
2866
                        else
2867
                            slopeType2 = SPPIDUtil.CalcSlope(originX, originY, connectorX, connectorY);
2868
                    }
2869

    
2870
                    if ((slopeType1 == SlopeType.VERTICAL && slopeType2 == SlopeType.HORIZONTAL) ||
2871
                        (slopeType2 == SlopeType.VERTICAL && slopeType1 == SlopeType.HORIZONTAL))
2872
                        symbols.Add(symbol);
2873
                }
2874
            }
2875

    
2876
            // 3개의 Symbol이 뭉쳐 있을 때
2877
            foreach (var item in document.SYMBOLS)
2878
            {
2879
                List<Symbol> group = new List<Symbol>();
2880
                SPPIDUtil.FindConnectedSymbolGroup(document, item, group);
2881
                if (group.Count == 3)
2882
                {
2883
                    Symbol symbol = SPPIDUtil.FindCenterAtThreeSymbols(document, group);
2884
                    if (!symbols.Contains(symbol))
2885
                        symbols.Add(symbol);
2886
                }
2887
            }
2888

    
2889
            // Connection Point가 3개 이상
2890
            foreach (var symbol in document.SYMBOLS)
2891
                if (symbol.CONNECTORS.Count > 2 && !symbols.Contains(symbol))
2892
                    symbols.Add(symbol);
2893

    
2894
            return symbols;
2895
        }
2896

    
2897
        /// <summary>
2898
        /// Graphic OID로 해당 Symbol의 크기를 구하여 Zoom
2899
        /// </summary>
2900
        /// <param name="graphicOID"></param>
2901
        /// <param name="milliseconds"></param>
2902
        private void ZoomObjectByGraphicOID(string graphicOID, int milliseconds = 150)
2903
        {
2904
            if (radApp.ActiveDocument.ActiveSheet.DrawingObjects[graphicOID] != null)
2905
            {
2906
                double minX = 0;
2907
                double minY = 0;
2908
                double maxX = 0;
2909
                double maxY = 0;
2910
                radApp.ActiveDocument.ActiveSheet.DrawingObjects[graphicOID].Range(out minX, out minY, out maxX, out maxY);
2911
                radApp.ActiveWindow.ZoomArea2(minX - 0.007, minY - 0.007, maxX + 0.007, maxY + 0.007, null);
2912

    
2913
                Thread.Sleep(milliseconds);
2914
            }
2915
        }
2916

    
2917
        /// <summary>
2918
        /// ComObject를 Release
2919
        /// </summary>
2920
        /// <param name="objVars"></param>
2921
        public void ReleaseCOMObjects(params object[] objVars)
2922
        {
2923
            int intNewRefCount = 0;
2924
            foreach (object obj in objVars)
2925
            {
2926
                if (!Information.IsNothing(obj) && System.Runtime.InteropServices.Marshal.IsComObject(obj))
2927
                    intNewRefCount = intNewRefCount + System.Runtime.InteropServices.Marshal.FinalReleaseComObject(obj);
2928
            }
2929
        }
2930
    }
2931
}
클립보드 이미지 추가 (최대 크기: 500 MB)