프로젝트

일반

사용자정보

통계
| 개정판:

hytos / DTI_PID / SPPIDConverter / AutoModeling.cs @ 3e5292ae

이력 | 보기 | 이력해설 | 다운로드 (153 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
        List<Line> NewBranchLines = new List<Line>();
41
        List<Tuple<string, Line, Line>> BranchLines = new List<Tuple<string, Line, Line>>();
42
        List<string> ZeroLengthModelItemID = new List<string>();
43
        List<Symbol> prioritySymbols;
44

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

    
53
        private void SetSystemEditingCommand(bool value)
54
        {
55
            foreach (var item in radApp.Commands)
56
            {
57
                if (item.Argument == "SystemEditingCmd.SystemEditing")
58
                {
59
                    if (item.Checked != value)
60
                    {
61
                        radApp.RunMacro("systemeditingcmd.dll");
62
                        break;
63
                    }
64

    
65
                }
66
            }
67
        }
68

    
69
        /// <summary>
70
        /// 도면 단위당 실행되는 메서드
71
        /// </summary>
72
        public void Run()
73
        {
74
            string drawingNumber = document.DrawingNumber;
75
            string drawingName = document.DrawingName;
76
            try
77
            {
78
                _placement = new Placement();
79
                dataSource = _placement.PIDDataSource;
80

    
81
                CreateDocument(ref drawingNumber, ref drawingName);
82

    
83
                if (DocumentCoordinateCorrection())
84
                {
85
                    Log.Write("Start Modeling");
86
                    SplashScreenManager.ShowForm(typeof(SPPIDSplashScreen), true, true);
87
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetParent, (IntPtr)radApp.HWnd);
88
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetDocumentName, DocumentLabelText);
89
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Priority Symbol Modeling");
90
                    prioritySymbols = GetPrioritySymbol();
91
                    foreach (var item in prioritySymbols)
92
                    {
93
                        try
94
                        {
95
                            SymbolModelingBySymbol(item);
96
                        }
97
                        catch (Exception ex)
98
                        {
99
                            Log.Write("Error in SymbolModelingByPriority");
100
                            Log.Write("UID : " + item.UID);
101
                            Log.Write(ex.Message);
102
                            Log.Write(ex.StackTrace);
103
                        }
104
                    }
105

    
106
                    // Equipment Modeling
107
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Equipments Modeling");
108
                    foreach (Equipment equipment in document.Equipments)
109
                    {
110
                        try
111
                        {
112
                            EquipmentModeling(equipment);
113
                        }
114
                        catch (Exception ex)
115
                        {
116
                            Log.Write("Error in EquipmentModeling");
117
                            Log.Write("UID : " + equipment.UID);
118
                            Log.Write(ex.Message);
119
                            Log.Write(ex.StackTrace);
120
                        }
121
                    }
122

    
123
                    SetPriorityLine();
124
                    // LineRun Line Modeling
125
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Lines Modeling");
126
                    foreach (var item in document.LINES)
127
                    {
128
                        NewLineModeling(item);
129
                    }
130

    
131
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Branch Lines Modeling");
132
                    while (NewBranchLines.Count > 0)
133
                    {
134
                        SortBranchLines();
135
                        NewLineModeling(NewBranchLines[0], true);
136
                    }
137

    
138
                    #region Clear Nominal Diameter
139
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Clear Attribute");
140
                    SetSystemEditingCommand(false);
141
                    foreach (var item in radApp.ActiveDocument.ActiveSheet.Symbols)
142
                    {
143
                        string modelItemID = string.Empty;
144
                        foreach (var attributes in item.AttributeSets)
145
                        {
146
                            if (!string.IsNullOrEmpty(modelItemID))
147
                                break;
148

    
149
                            foreach (var attribute in attributes)
150
                            {
151
                                if (attribute.Name == "ModelID")
152
                                {
153
                                    modelItemID = attribute.GetValue().ToString();
154
                                    break;
155
                                }
156
                            }
157
                        }
158

    
159
                        if (!string.IsNullOrEmpty(modelItemID))
160
                        {
161
                            LMModelItem modelItem = dataSource.GetModelItem(modelItemID);
162
                            if (modelItem != null)
163
                            {
164
                                LMAAttribute attribute = modelItem.Attributes["NominalDiameter"];
165
                                if (attribute != null)
166
                                    attribute.set_Value(DBNull.Value);
167
                                attribute = modelItem.Attributes["PipingPoint1.NominalDiameter"];
168
                                if (attribute != null)
169
                                    attribute.set_Value(DBNull.Value);
170
                                attribute = modelItem.Attributes["PipingPoint2.NominalDiameter"];
171
                                if (attribute != null)
172
                                    attribute.set_Value(DBNull.Value);
173

    
174
                                modelItem.Commit();
175
                                ReleaseCOMObjects(modelItem);
176
                            }
177
                        }
178
                    }
179
                    foreach (var item in radApp.ActiveDocument.ActiveSheet.AllDependencyObjects)
180
                    {
181
                        string modelItemID = string.Empty;
182
                        foreach (var attributes in item.AttributeSets)
183
                        {
184
                            if (!string.IsNullOrEmpty(modelItemID))
185
                                break;
186

    
187
                            foreach (var attribute in attributes)
188
                            {
189
                                if (attribute.Name == "ModelID")
190
                                {
191
                                    modelItemID = attribute.GetValue().ToString();
192
                                    break;
193
                                }
194
                            }
195
                        }
196

    
197
                        if (!string.IsNullOrEmpty(modelItemID))
198
                        {
199
                            LMModelItem modelItem = dataSource.GetModelItem(modelItemID);
200
                            if (modelItem != null)
201
                            {
202
                                LMAAttribute attribute = modelItem.Attributes["NominalDiameter"];
203
                                if (attribute != null)
204
                                    attribute.set_Value(DBNull.Value);
205

    
206
                                modelItem.Commit();
207
                                ReleaseCOMObjects(modelItem);
208
                            }
209
                        }
210
                    }
211
                    SetSystemEditingCommand(true);
212
                    #endregion
213

    
214
                    //// EndBreak Modeling
215
                    //SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "EndBreaks Modeling");
216
                    //foreach (var item in document.EndBreaks)
217
                    //    try
218
                    //    {
219
                    //        EndBreakModeling(item);
220
                    //    }
221
                    //    catch (Exception ex)
222
                    //    {
223
                    //        Log.Write("Error in EndBreakModeling");
224
                    //        Log.Write("UID : " + item.UID);
225
                    //        Log.Write(ex.Message);
226
                    //        Log.Write(ex.StackTrace);
227
                    //    }
228

    
229

    
230
                    //// SpecBreak Modeling
231
                    //SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "SpecBreaks Modeling");
232
                    //foreach (var item in document.SpecBreaks)
233
                    //    try
234
                    //    {
235
                    //        SpecBreakModeling(item);
236
                    //    }
237
                    //    catch (Exception ex)
238
                    //    {
239
                    //        Log.Write("Error in SpecBreakModeling");
240
                    //        Log.Write("UID : " + item.UID);
241
                    //        Log.Write(ex.Message);
242
                    //        Log.Write(ex.StackTrace);
243
                    //    }
244

    
245

    
246
                    //// LineNumber Modeling
247
                    //SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "LineNumbers Modeling");
248
                    //foreach (var item in document.LINENUMBERS)
249
                    //    try
250
                    //    {
251
                    //        LineNumberModeling(item);
252
                    //    }
253
                    //    catch (Exception ex)
254
                    //    {
255
                    //        Log.Write("Error in LineNumberModeling");
256
                    //        Log.Write("UID : " + item.UID);
257
                    //        Log.Write(ex.Message);
258
                    //        Log.Write(ex.StackTrace);
259
                    //    }
260

    
261

    
262
                    //// FlowMark Modeling
263
                    //SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Flow Mark Modeling");
264
                    //foreach (var item in document.LINES)
265
                    //    try
266
                    //    {
267
                    //        FlowMarkModeling(item);
268
                    //    }
269
                    //    catch (Exception ex)
270
                    //    {
271
                    //        Log.Write("Error in FlowMarkModeling");
272
                    //        Log.Write("UID : " + item.UID);
273
                    //        Log.Write(ex.Message);
274
                    //        Log.Write(ex.StackTrace);
275
                    //    }
276

    
277

    
278
                    //// Note Symbol Modeling
279
                    //SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Note Symbol Modeling");
280
                    //foreach (var item in document.SYMBOLS)
281
                    //    try
282
                    //    {
283
                    //        NoteSymbolModeling(item);
284
                    //    }
285
                    //    catch (Exception ex)
286
                    //    {
287
                    //        Log.Write("Error in NoteSymbolModeling");
288
                    //        Log.Write("UID : " + item.UID);
289
                    //        Log.Write(ex.Message);
290
                    //        Log.Write(ex.StackTrace);
291
                    //    }
292

    
293

    
294
                    //// Note Modeling
295
                    //SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Notes Modeling");
296
                    //foreach (var item in document.NOTES)
297
                    //    try
298
                    //    {
299
                    //        NoteModeling(item);
300
                    //    }
301
                    //    catch (Exception ex)
302
                    //    {
303
                    //        Log.Write("Error in NoteModeling");
304
                    //        Log.Write("UID : " + item.UID);
305
                    //        Log.Write(ex.Message);
306
                    //        Log.Write(ex.StackTrace);
307
                    //    }
308

    
309
                    //// Text Modeling
310
                    //SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Texts Modeling");
311
                    //foreach (var item in document.TEXTINFOS)
312
                    //    try
313
                    //    {
314
                    //        TextModeling(item);
315
                    //    }
316
                    //    catch (Exception ex)
317
                    //    {
318
                    //        Log.Write("Error in TextModeling");
319
                    //        Log.Write("UID : " + item.UID);
320
                    //        Log.Write(ex.Message);
321
                    //        Log.Write(ex.StackTrace);
322
                    //    }
323

    
324
                    //// Input LineNumber Attribute
325
                    //SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Set LineNumbers Attribute");
326
                    //foreach (var item in document.LINENUMBERS)
327
                    //    try
328
                    //    {
329
                    //        InputLineNumberAttribute(item);
330
                    //    }
331
                    //    catch (Exception ex)
332
                    //    {
333
                    //        Log.Write("Error in InputLineNumberAttribute");
334
                    //        Log.Write("UID : " + item.UID);
335
                    //        Log.Write(ex.Message);
336
                    //        Log.Write(ex.StackTrace);
337
                    //    }
338

    
339
                    //// Input Symbol Attribute
340
                    //SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Set Symbols Attribute");
341
                    //foreach (var item in document.SYMBOLS)
342
                    //    try
343
                    //    {
344
                    //        InputSymbolAttribute(item, item.ATTRIBUTES);
345
                    //    }
346
                    //    catch (Exception ex)
347
                    //    {
348
                    //        Log.Write("Error in InputSymbolAttribute");
349
                    //        Log.Write("UID : " + item.UID);
350
                    //        Log.Write(ex.Message);
351
                    //        Log.Write(ex.StackTrace);
352
                    //    }
353

    
354
                    //// Input SpecBreak Attribute
355
                    //SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Set Symbols Attribute");
356
                    //foreach (var item in document.SpecBreaks)
357
                    //    try
358
                    //    {
359
                    //        InputSpecBreakAttribute(item);
360
                    //    }
361
                    //    catch (Exception ex)
362
                    //    {
363
                    //        Log.Write("Error in InputSpecBreakAttribute");
364
                    //        Log.Write("UID : " + item.UID);
365
                    //        Log.Write(ex.Message);
366
                    //        Log.Write(ex.StackTrace);
367
                    //    }
368

    
369
                    //// Label Symbol Modeling
370
                    //SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Labels Modeling");
371
                    //foreach (var item in document.SYMBOLS)
372
                    //    try
373
                    //    {
374
                    //        LabelSymbolModeling(item);
375
                    //    }
376
                    //    catch (Exception ex)
377
                    //    {
378
                    //        Log.Write("Error in LabelSymbolModeling");
379
                    //        Log.Write("UID : " + item.UID);
380
                    //        Log.Write(ex.Message);
381
                    //        Log.Write(ex.StackTrace);
382
                    //    }
383

    
384
                    //// LineRun Line Join
385
                    //SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Join LineRuns");
386
                    //foreach (LineNumber lineNumber in document.LINENUMBERS)
387
                    //    try
388
                    //    {
389
                    //        foreach (LineRun run in lineNumber.RUNS)
390
                    //            JoinRunLine(run);
391
                    //    }
392
                    //    catch (Exception ex)
393
                    //    {
394
                    //        Log.Write("Error in JoinRunLine");
395
                    //        Log.Write("UID : " + lineNumber.UID);
396
                    //        Log.Write(ex.Message);
397
                    //        Log.Write(ex.StackTrace);
398
                    //    }
399

    
400
                    //// TrimLineRun Line Join
401
                    //foreach (TrimLine trimLine in document.TRIMLINES)
402
                    //    try
403
                    //    {
404
                    //        foreach (LineRun run in trimLine.RUNS)
405
                    //            JoinRunLine(run);
406
                    //    }
407
                    //    catch (Exception ex)
408
                    //    {
409
                    //        Log.Write("Error in JoinRunLine");
410
                    //        Log.Write("UID : " + trimLine.UID);
411
                    //        Log.Write(ex.Message);
412
                    //        Log.Write(ex.StackTrace);
413
                    //    }
414

    
415
                    //SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetProgress, AllCount);
416
                }
417
            }
418
            catch (Exception ex)
419
            {
420
                SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.ClearParent, null);
421
                SplashScreenManager.CloseForm(false);
422
                System.Windows.Forms.MessageBox.Show(ex.Message + "\r\n" + ex.StackTrace);
423
            }
424
            finally
425
            {
426
                Log.Write("End Modeling");
427
                application.ActiveWindow.Fit();
428

    
429
                if (radApp.ActiveDocument != null)
430
                {
431
                    //radApp.ActiveDocument.Save();
432
                    //radApp.ActiveDocument.SaveOnClose = false;
433
                    //radApp.ActiveDocument.Close(false);
434

    
435
                    ReleaseCOMObjects(newDrawing);
436
                }
437

    
438
                ReleaseCOMObjects(dataSource);
439
                ReleaseCOMObjects(_placement);
440

    
441
                Project_DB.InsertDrawingInfo(document.PATH, drawingNumber, drawingName, document);
442
                if (SplashScreenManager.Default.IsSplashFormVisible)
443
                {
444
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.ClearParent, null);
445
                    SplashScreenManager.CloseForm(false);
446
                    Log.Write("\r\n");
447
                }
448
            }
449
        }
450

    
451
        /// <summary>
452
        /// 도면 생성 메서드
453
        /// </summary>
454
        private void CreateDocument(ref string drawingNumber, ref string drawingName)
455
        {
456
            Log.Write("------------------ Start create document ------------------");
457
            GetDrawingNameAndNumber(ref drawingName, ref drawingNumber);
458
            Log.Write("Drawing name : " + drawingName);
459
            Log.Write("Drawing number : " + drawingNumber);
460
            newDrawing = application.Drawings.Add(document.Unit, document.Template, drawingNumber, drawingName);
461
            document.SPPID_DrawingNumber = drawingNumber;
462
            document.SPPID_DrawingName = drawingName;
463
            application.ActiveWindow.Fit();
464
            Thread.Sleep(1000);
465
            application.ActiveWindow.Zoom = 2000;
466
            Thread.Sleep(2000);
467
        }
468

    
469
        /// <summary>
470
        /// DrawingName, DrawingNumber를 확인하여 중복이 있으면 _1을 붙이고 +1씩 한다.
471
        /// </summary>
472
        /// <param name="drawingName"></param>
473
        /// <param name="drawingNumber"></param>
474
        private void GetDrawingNameAndNumber(ref string drawingName, ref string drawingNumber)
475
        {
476
            LMDrawings drawings = new LMDrawings();
477
            drawings.Collect(dataSource);
478

    
479
            List<string> drawingNameList = new List<string>();
480
            List<string> drawingNumberList = new List<string>();
481

    
482
            foreach (LMDrawing item in drawings)
483
            {
484
                drawingNameList.Add(item.Attributes["Name"].get_Value().ToString());
485
                drawingNumberList.Add(item.Attributes["DrawingNumber"].get_Value().ToString());
486
            }
487

    
488
            int nameLength = drawingName.Length;
489
            while (drawingNameList.Contains(drawingName))
490
            {
491
                if (nameLength == drawingName.Length)
492
                    drawingName += "-1";
493
                else
494
                {
495
                    int index = Convert.ToInt32(drawingName.Remove(0, nameLength + 1));
496
                    drawingName = drawingName.Substring(0, nameLength + 1);
497
                    drawingName += ++index;
498
                }
499
            }
500

    
501
            int numberLength = drawingNumber.Length;
502
            while (drawingNameList.Contains(drawingNumber))
503
            {
504
                if (numberLength == drawingNumber.Length)
505
                    drawingNumber += "-1";
506
                else
507
                {
508
                    int index = Convert.ToInt32(drawingNumber.Remove(0, numberLength + 1));
509
                    drawingNumber = drawingNumber.Substring(0, numberLength + 1);
510
                    drawingNumber += ++index;
511
                }
512
            }
513

    
514
            ReleaseCOMObjects(drawings);
515
        }
516

    
517
        /// <summary>
518
        /// 도면 크기 구하는 메서드
519
        /// </summary>
520
        /// <returns></returns>
521
        private bool DocumentCoordinateCorrection()
522
        {
523
            if (Settings.Default.DrawingX != 0 && Settings.Default.DrawingY != 0)
524
            {
525
                Log.Write("Setting Drawing X, Drawing Y");
526
                document.SetSPPIDLocation(Settings.Default.DrawingX, Settings.Default.DrawingY);
527
                Log.Write("Start coordinate correction");
528
                document.CoordinateCorrection();
529
                return true;
530
            }
531
            else
532
            {
533
                Log.Write("Need Drawing X, Y");
534
                return false;
535
            }
536
        }
537

    
538
        /// <summary>
539
        /// 심볼을 실제로 Modeling 메서드
540
        /// </summary>
541
        /// <param name="symbol"></param>
542
        /// <param name="targetSymbol"></param>
543
        /// <param name="prevSymbol"></param>
544
        private void SymbolModeling(Symbol symbol, Symbol targetSymbol)
545
        {
546
            // OWNERSYMBOL Attribute, 값을 가지고 있을 경우
547
            BaseModel.Attribute itemAttribute = symbol.ATTRIBUTES.Find(attr => attr.ATTRIBUTE == "OWNERSYMBOL");
548
            if (itemAttribute != null && (string.IsNullOrEmpty(itemAttribute.VALUE) || itemAttribute.VALUE != "None"))
549
                return;
550
            // 이미 모델링 됐을 경우
551
            else if (!string.IsNullOrEmpty(symbol.SPPID.RepresentationId))
552
                return;
553

    
554
            LMSymbol _LMSymbol = null;
555

    
556
            string mappingPath = symbol.SPPID.MAPPINGNAME;
557
            double x = symbol.SPPID.ORIGINAL_X;
558
            double y = symbol.SPPID.ORIGINAL_Y;
559
            int mirror = 0;
560
            double angle = symbol.ANGLE;
561

    
562
            // OPC 일경우 180도 일때 Mirror
563
            if (mappingPath.Contains("Piping OPC's") && angle == Math.PI)
564
                mirror = 1;
565

    
566
            // Mirror 계산
567
            if (symbol.FLIP == 1)
568
            {
569
                mirror = 1;
570
                angle += Math.PI;
571
            }
572

    
573
            if (targetSymbol != null && !string.IsNullOrEmpty(targetSymbol.SPPID.RepresentationId))
574
            {
575
                LMSymbol _TargetItem = dataSource.GetSymbol(targetSymbol.SPPID.RepresentationId);
576
                Connector connector = SPPIDUtil.FindSymbolConnectorByUID(document, symbol.UID, targetSymbol);
577
                if (connector != null)
578
                    GetTargetSymbolConnectorPoint(connector, targetSymbol, ref x, ref y);
579

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

    
582
                if (_LMSymbol != null && _TargetItem != null)
583
                {
584
                    symbol.SPPID.RepresentationId = _LMSymbol.AsLMRepresentation().Id;
585
                    LMConnector reModelingConnector = FindBreakLineTarget(symbol, targetSymbol);
586

    
587
                    if (reModelingConnector != null)
588
                        ReModelingLMConnector(reModelingConnector);
589
                }
590

    
591
                ReleaseCOMObjects(_TargetItem);
592
            }
593
            else
594
                _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle);
595

    
596
            if (_LMSymbol != null)
597
            {
598
                _LMSymbol.Commit();
599
                symbol.SPPID.RepresentationId = _LMSymbol.AsLMRepresentation().Id;
600
                symbol.SPPID.ModelItemID = _LMSymbol.ModelItemID;
601
                symbol.SPPID.GraphicOID = _LMSymbol.get_GraphicOID();
602

    
603
                foreach (var item in symbol.ChildSymbols)
604
                    CreateChildSymbol(item, _LMSymbol);
605

    
606
                symbol.SPPID.SPPID_X = _LMSymbol.get_XCoordinate();
607
                symbol.SPPID.SPPID_Y = _LMSymbol.get_YCoordinate();
608

    
609
                double[] range = null;
610
                GetSPPIDSymbolRange(symbol, ref range);
611
                symbol.SPPID.SPPID_Min_X = range[0];
612
                symbol.SPPID.SPPID_Min_Y = range[1];
613
                symbol.SPPID.SPPID_Max_X = range[2];
614
                symbol.SPPID.SPPID_Max_Y = range[3];
615

    
616
                foreach (var item in symbol.SPPID.CorrectionX_GroupSymbols)
617
                    item.SPPID.ORIGINAL_X = symbol.SPPID.SPPID_X;
618
                foreach (var item in symbol.SPPID.CorrectionY_GroupSymbols)
619
                    item.SPPID.ORIGINAL_Y = symbol.SPPID.SPPID_Y;
620

    
621
                ReleaseCOMObjects(_LMSymbol);
622
            }
623
        }
624

    
625
        private void RemoveSymbol(Symbol symbol)
626
        {
627
            if (!string.IsNullOrEmpty(symbol.SPPID.RepresentationId))
628
            {
629
                LMSymbol _LMSymbol = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
630
                if (_LMSymbol != null)
631
                {
632
                    _placement.PIDRemovePlacement(_LMSymbol.AsLMRepresentation());
633
                    ReleaseCOMObjects(_LMSymbol);
634
                }
635
            }
636

    
637
            symbol.SPPID.RepresentationId = string.Empty;
638
            symbol.SPPID.ModelItemID = string.Empty;
639
            symbol.SPPID.SPPID_X = double.NaN;
640
            symbol.SPPID.SPPID_Y = double.NaN;
641
            symbol.SPPID.SPPID_Min_X = double.NaN;
642
            symbol.SPPID.SPPID_Min_Y = double.NaN;
643
            symbol.SPPID.SPPID_Max_X = double.NaN;
644
            symbol.SPPID.SPPID_Max_Y = double.NaN;
645
        }
646

    
647
        private void RemoveSymbol(List<Symbol> symbols)
648
        {
649
            foreach (var symbol in symbols)
650
            {
651
                if (!string.IsNullOrEmpty(symbol.SPPID.RepresentationId))
652
                {
653
                    LMSymbol _LMSymbol = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
654
                    if (_LMSymbol != null)
655
                    {
656
                        _placement.PIDRemovePlacement(_LMSymbol.AsLMRepresentation());
657
                        ReleaseCOMObjects(_LMSymbol);
658
                    }
659
                }
660

    
661
                symbol.SPPID.RepresentationId = string.Empty;
662
                symbol.SPPID.ModelItemID = string.Empty;
663
                symbol.SPPID.SPPID_X = double.NaN;
664
                symbol.SPPID.SPPID_Y = double.NaN;
665
                symbol.SPPID.SPPID_Min_X = double.NaN;
666
                symbol.SPPID.SPPID_Min_Y = double.NaN;
667
                symbol.SPPID.SPPID_Max_X = double.NaN;
668
                symbol.SPPID.SPPID_Max_Y = double.NaN;
669
            }
670
        }
671

    
672
        /// <summary>
673
        /// ID2의 Symbol Width와 Height를 비교해서 상대적인 SPPID Connector좌표를 가져온다.
674
        /// </summary>
675
        /// <param name="targetConnector"></param>
676
        /// <param name="targetSymbol"></param>
677
        /// <param name="x"></param>
678
        /// <param name="y"></param>
679
        private void GetTargetSymbolConnectorPoint(Connector targetConnector, Symbol targetSymbol, ref double x, ref double y)
680
        {
681
            LMSymbol _TargetItem = dataSource.GetSymbol(targetSymbol.SPPID.RepresentationId);
682

    
683
            double[] range = null;
684
            List<double[]> points = new List<double[]>();
685
            GetSPPIDSymbolRangeAndConnectionPoints(targetSymbol, ref range, points);
686
            double x1 = range[0];
687
            double y1 = range[1];
688
            double x2 = range[2];
689
            double y2 = range[3];
690

    
691
            // Origin 기준 Connector의 위치차이
692
            double sceneX = 0;
693
            double sceneY = 0;
694
            SPPIDUtil.ConvertPointBystring(targetConnector.SCENECONNECTPOINT, ref sceneX, ref sceneY);
695
            double originX = 0;
696
            double originY = 0;
697
            SPPIDUtil.ConvertPointBystring(targetSymbol.ORIGINALPOINT, ref originX, ref originY);
698
            double gapX = originX - sceneX;
699
            double gapY = originY - sceneY;
700

    
701
            // SPPID Symbol과 ID2 심볼의 크기 차이
702
            DataRow row = document.SymbolTable.Select(string.Format("UID = '{0}'",targetSymbol.DBUID))[0];
703
            double sizeWidth = Convert.ToDouble(row["Width"]);
704
            double sizeHeight = Convert.ToDouble(row["Height"]);
705
            //SPPIDUtil.ConvertPointBystring(targetSymbol.SIZE, ref sizeWidth, ref sizeHeight);
706
            double percentX = (x2 - x1) / sizeWidth;
707
            double percentY = (y2 - y1) / sizeHeight;
708

    
709
            double SPPIDgapX = gapX * percentX;
710
            double SPPIDgapY = gapY * percentY;
711

    
712
            double[] SPPIDOriginPoint = new double[] { _TargetItem.get_XCoordinate() - SPPIDgapX, _TargetItem.get_YCoordinate() + SPPIDgapY };
713
            double distance = double.MaxValue;
714
            double[] resultPoint;
715
            foreach (var point in points)
716
            {
717
                double result = SPPIDUtil.CalcPointToPointdDistance(point[0], point[1], SPPIDOriginPoint[0], SPPIDOriginPoint[1]);
718
                if (distance > result)
719
                {
720
                    distance = result;
721
                    resultPoint = point;
722
                    x = point[0];
723
                    y = point[1];
724
                }
725
            }
726

    
727
            ReleaseCOMObjects(_TargetItem);
728
        }
729

    
730
        private void GetTargetLineConnectorPoint(Connector targetConnector, Line targetLine, ref double x, ref double y)
731
        {
732
            int index = targetLine.CONNECTORS.IndexOf(targetConnector);
733
            if (index == 0)
734
            {
735
                x = targetLine.SPPID.START_X;
736
                y = targetLine.SPPID.START_Y;
737
            }
738
            else
739
            {
740
                x = targetLine.SPPID.END_X;
741
                y = targetLine.SPPID.END_Y;
742
            }
743
        }
744

    
745
        /// <summary>
746
        /// SPPID Symbol의 Range를 구한다.
747
        /// </summary>
748
        /// <param name="symbol"></param>
749
        /// <param name="range"></param>
750
        private void GetSPPIDSymbolRangeAndConnectionPoints(Symbol symbol, ref double[] range, List<double[]> points)
751
        {
752
            LMSymbol _TargetItem = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
753
            Ingr.RAD2D.Symbol2d symbol2d = radApp.ActiveDocument.ActiveSheet.DrawingObjects[_TargetItem.get_GraphicOID().ToString()];
754
            double x1 = 0;
755
            double y1 = 0;
756
            double x2 = 0;
757
            double y2 = 0;
758
            symbol2d.Range(out x1, out y1, out x2, out y2);
759
            range = new double[] { x1, y1, x2, y2 };
760

    
761
            for (int i = 1; i < int.MaxValue; i++)
762
            {
763
                double connX = 0;
764
                double connY = 0;
765
                if (_placement.PIDConnectPointLocation(_TargetItem, i, ref connX, ref connY))
766
                    points.Add(new double[] { connX, connY });
767
                else
768
                    break;
769
            }
770

    
771
            foreach (var childSymbol in symbol.ChildSymbols)
772
                GetSPPIDChildSymbolRange(childSymbol, ref range, points);
773

    
774
            ReleaseCOMObjects(_TargetItem);
775
        }
776

    
777
        private void GetSPPIDSymbolRange(Symbol symbol, ref double[] range)
778
        {
779
            LMSymbol _TargetItem = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
780
            Ingr.RAD2D.Symbol2d symbol2d = radApp.ActiveDocument.ActiveSheet.DrawingObjects[_TargetItem.get_GraphicOID().ToString()];
781
            double x1 = 0;
782
            double y1 = 0;
783
            double x2 = 0;
784
            double y2 = 0;
785
            symbol2d.Range(out x1, out y1, out x2, out y2);
786
            range = new double[] { x1, y1, x2, y2 };
787

    
788
            foreach (var childSymbol in symbol.ChildSymbols)
789
                GetSPPIDChildSymbolRange(childSymbol, ref range);
790

    
791
            ReleaseCOMObjects(_TargetItem);
792
        }
793

    
794
        private void GetSPPIDSymbolRange(List<Symbol> symbols, ref double[] range)
795
        {
796
            double[] tempRange = new double[] { double.MaxValue, double.MaxValue, double.MinValue, double.MinValue };
797
            foreach (var symbol in symbols)
798
            {
799
                LMSymbol _TargetItem = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
800
                Ingr.RAD2D.Symbol2d symbol2d = radApp.ActiveDocument.ActiveSheet.DrawingObjects[_TargetItem.get_GraphicOID().ToString()];
801
                double x1 = 0;
802
                double y1 = 0;
803
                double x2 = 0;
804
                double y2 = 0;
805
                symbol2d.Range(out x1, out y1, out x2, out y2);
806

    
807
                tempRange[0] = Math.Min(tempRange[0], x1);
808
                tempRange[1] = Math.Min(tempRange[1], y1);
809
                tempRange[2] = Math.Max(tempRange[2], x2);
810
                tempRange[3] = Math.Max(tempRange[3], y2);
811

    
812
                foreach (var childSymbol in symbol.ChildSymbols)
813
                    GetSPPIDChildSymbolRange(childSymbol, ref tempRange);
814

    
815
                ReleaseCOMObjects(_TargetItem);
816
            }
817

    
818
            range = tempRange;
819
        }
820

    
821
        /// <summary>
822
        /// Child Modeling 된 Symbol의 Range를 구한다.
823
        /// </summary>
824
        /// <param name="childSymbol"></param>
825
        /// <param name="range"></param>
826
        private void GetSPPIDChildSymbolRange(ChildSymbol childSymbol, ref double[] range, List<double[]> points)
827
        {
828
            LMSymbol _ChildSymbol = dataSource.GetSymbol(childSymbol.SPPID.RepresentationId);
829
            Ingr.RAD2D.Symbol2d symbol2d = radApp.ActiveDocument.ActiveSheet.DrawingObjects[_ChildSymbol.get_GraphicOID().ToString()];
830
            double x1 = 0;
831
            double y1 = 0;
832
            double x2 = 0;
833
            double y2 = 0;
834
            symbol2d.Range(out x1, out y1, out x2, out y2);
835
            range[0] = Math.Min(range[0], x1);
836
            range[1] = Math.Min(range[1], y1);
837
            range[2] = Math.Max(range[2], x2);
838
            range[3] = Math.Max(range[3], y2);
839

    
840
            for (int i = 1; i < int.MaxValue; i++)
841
            {
842
                double connX = 0;
843
                double connY = 0;
844
                if (_placement.PIDConnectPointLocation(_ChildSymbol, i, ref connX, ref connY))
845
                    points.Add(new double[] { connX, connY });
846
                else
847
                    break;
848
            }
849

    
850
            foreach (var loopChildSymbol in childSymbol.ChildSymbols)
851
                GetSPPIDChildSymbolRange(loopChildSymbol, ref range, points);
852

    
853
            ReleaseCOMObjects(_ChildSymbol);
854
        }
855

    
856
        private void GetSPPIDChildSymbolRange(ChildSymbol childSymbol, ref double[] range)
857
        {
858
            LMSymbol _ChildSymbol = dataSource.GetSymbol(childSymbol.SPPID.RepresentationId);
859
            Ingr.RAD2D.Symbol2d symbol2d = radApp.ActiveDocument.ActiveSheet.DrawingObjects[_ChildSymbol.get_GraphicOID().ToString()];
860
            double x1 = 0;
861
            double y1 = 0;
862
            double x2 = 0;
863
            double y2 = 0;
864
            symbol2d.Range(out x1, out y1, out x2, out y2);
865
            range[0] = Math.Min(range[0], x1);
866
            range[1] = Math.Min(range[1], y1);
867
            range[2] = Math.Max(range[2], x2);
868
            range[3] = Math.Max(range[3], y2);
869

    
870
            foreach (var loopChildSymbol in childSymbol.ChildSymbols)
871
                GetSPPIDChildSymbolRange(loopChildSymbol, ref range);
872

    
873
            ReleaseCOMObjects(_ChildSymbol);
874
        }
875

    
876
        /// <summary>
877
        /// Label Symbol Modeling
878
        /// </summary>
879
        /// <param name="symbol"></param>
880
        private void LabelSymbolModeling(Symbol symbol)
881
        {
882
            if (string.IsNullOrEmpty(symbol.SPPID.RepresentationId))
883
            {
884
                BaseModel.Attribute itemAttribute = symbol.ATTRIBUTES.Find(x => x.ATTRIBUTE == "OWNERSYMBOL");
885
                if (itemAttribute == null || string.IsNullOrEmpty(itemAttribute.VALUE) || itemAttribute.VALUE == "None")
886
                    return;
887
                Array points = new double[] { 0, symbol.SPPID.ORIGINAL_X, symbol.SPPID.ORIGINAL_Y };
888

    
889
                string symbolUID = itemAttribute.VALUE;
890
                object targetItem = SPPIDUtil.FindObjectByUID(document, symbolUID);
891
                if (targetItem != null &&
892
                    (targetItem.GetType() == typeof(Symbol) ||
893
                    targetItem.GetType() == typeof(Equipment)))
894
                {
895
                    // Object 아이템이 Symbol일 경우 Equipment일 경우 
896
                    string sRep = null;
897
                    if (targetItem.GetType() == typeof(Symbol))
898
                        sRep = ((Symbol)targetItem).SPPID.RepresentationId;
899
                    else if (targetItem.GetType() == typeof(Equipment))
900
                        sRep = ((Equipment)targetItem).SPPID.RepresentationId;
901
                    if (!string.IsNullOrEmpty(sRep))
902
                    {
903
                        // LEADER Line 검사
904
                        bool leaderLine = false;
905
                        SymbolMapping symbolMapping = document.SymbolMappings.Find(x => x.UID == symbol.DBUID);
906
                        if (symbolMapping != null)
907
                            leaderLine = symbolMapping.LEADERLINE;
908

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

    
913
                        //Leader 선 센터로
914
                        if (_LMLabelPresist != null)
915
                        {
916
                            // Target Item에 Label의 Attribute Input
917
                            InputSymbolAttribute(targetItem, symbol.ATTRIBUTES);
918

    
919
                            string OID = _LMLabelPresist.get_GraphicOID();
920
                            DependencyObject dependency = radApp.ActiveDocument.ActiveSheet.DrawingObjects[OID] as DependencyObject;
921
                            if (dependency != null)
922
                            {
923
                                bool result = false;
924
                                foreach (var attributes in dependency.AttributeSets)
925
                                {
926
                                    foreach (var attribute in attributes)
927
                                    {
928
                                        string name = attribute.Name;
929
                                        string value = attribute.GetValue().ToString();
930
                                        if (name == "DrawingItemType" && value == "LabelPersist")
931
                                        {
932
                                            foreach (DrawingObjectBase drawingObject in dependency.DrawingObjects)
933
                                            {
934
                                                if (drawingObject.Type == Ingr.RAD2D.ObjectType.igLineString2d)
935
                                                {
936
                                                    Ingr.RAD2D.LineString2d lineString2D = drawingObject as Ingr.RAD2D.LineString2d;
937
                                                    double prevX = _TargetItem.get_XCoordinate();
938
                                                    double prevY = _TargetItem.get_YCoordinate();
939
                                                    lineString2D.InsertVertex(lineString2D.VertexCount, prevX, prevY);
940
                                                    lineString2D.RemoveVertex(lineString2D.VertexCount);
941
                                                    result = true;
942
                                                    break;
943
                                                }
944
                                            }
945
                                        }
946

    
947
                                        if (result)
948
                                            break;
949
                                    }
950

    
951
                                    if (result)
952
                                        break;
953
                                }
954
                            }
955

    
956
                            _LMLabelPresist.Commit();
957
                            ReleaseCOMObjects(_LMLabelPresist);
958
                        }
959

    
960
                        ReleaseCOMObjects(_TargetItem);
961
                    }
962
                }
963
                else if (targetItem != null && targetItem.GetType() == typeof(Line))
964
                {
965
                    Line targetLine = targetItem as Line;
966
                    Dictionary<LMConnector, List<double[]>> connectorVertices = GetPipeRunVertices(targetLine.SPPID.ModelItemId);
967
                    LMConnector connectedLMConnector = FindTargetLMConnectorForLabel(connectorVertices, symbol.SPPID.ORIGINAL_X, symbol.SPPID.ORIGINAL_Y);
968
                    if (connectedLMConnector != null)
969
                    {
970
                        // LEADER Line 검사
971
                        bool leaderLine = false;
972
                        SymbolMapping symbolMapping = document.SymbolMappings.Find(x => x.UID == symbol.DBUID);
973
                        if (symbolMapping != null)
974
                            leaderLine = symbolMapping.LEADERLINE;
975

    
976
                        LMLabelPersist _LMLabelPresist = _placement.PIDPlaceLabel(symbol.SPPID.MAPPINGNAME, ref points, Rotation: 0, LabeledItem: connectedLMConnector.AsLMRepresentation(), IsLeaderVisible: leaderLine);
977
                        if (_LMLabelPresist != null)
978
                        {
979
                            _LMLabelPresist.Commit();
980
                            ReleaseCOMObjects(_LMLabelPresist);
981
                        }
982
                        ReleaseCOMObjects(connectedLMConnector);
983
                    }
984

    
985
                    foreach (var item in connectorVertices)
986
                        if (item.Key != null)
987
                            ReleaseCOMObjects(item.Key);
988
                }
989
            }
990
        }
991

    
992
        /// <summary>
993
        /// Equipment를 실제로 Modeling 메서드
994
        /// </summary>
995
        /// <param name="equipment"></param>
996
        private void EquipmentModeling(Equipment equipment)
997
        {
998
            if (!string.IsNullOrEmpty(equipment.SPPID.RepresentationId))
999
                return;
1000

    
1001
            LMSymbol _LMSymbol = null;
1002
            LMSymbol targetItem = null;
1003
            string mappingPath = equipment.SPPID.MAPPINGNAME;
1004
            double x = equipment.SPPID.ORIGINAL_X;
1005
            double y = equipment.SPPID.ORIGINAL_Y;
1006
            int mirror = 0;
1007
            double angle = equipment.ANGLE;
1008

    
1009
            SPPIDUtil.ConvertGridPoint(ref x, ref y);
1010

    
1011
            Connector connector = equipment.CONNECTORS.Find(conn => !string.IsNullOrEmpty(conn.CONNECTEDITEM) && conn.CONNECTEDITEM != "None");
1012
            if (connector != null)
1013
            {
1014
                Equipment connEquipment = SPPIDUtil.FindObjectByUID(document, connector.CONNECTEDITEM) as Equipment;
1015
                if (connEquipment != null)
1016
                {
1017
                    if (string.IsNullOrEmpty(connEquipment.SPPID.RepresentationId))
1018
                        EquipmentModeling(connEquipment);
1019

    
1020
                    if (!string.IsNullOrEmpty(connEquipment.SPPID.RepresentationId))
1021
                    {
1022
                        targetItem = dataSource.GetSymbol(connEquipment.SPPID.RepresentationId);
1023
                        if (targetItem != null)
1024
                        {
1025
                            _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle, TargetItem: targetItem);
1026
                        }
1027
                        else
1028
                        {
1029
                            _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle);
1030
                        }
1031
                    }
1032
                    else
1033
                    {
1034
                        _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle);
1035
                    }
1036
                }
1037
                else
1038
                {
1039
                    _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle);
1040
                }
1041
            }
1042
            else
1043
            {
1044
                _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle);
1045
            }
1046

    
1047
            if (_LMSymbol != null)
1048
            {
1049
                _LMSymbol.Commit();
1050
                equipment.SPPID.RepresentationId = _LMSymbol.AsLMRepresentation().Id;
1051
                equipment.SPPID.GraphicOID = _LMSymbol.get_GraphicOID();
1052
                ReleaseCOMObjects(_LMSymbol);
1053
            }
1054

    
1055
            if (targetItem != null)
1056
            {
1057
                ReleaseCOMObjects(targetItem);
1058
            }
1059

    
1060
            ReleaseCOMObjects(_LMSymbol);
1061
        }
1062

    
1063
        /// <summary>
1064
        /// 첫 진입점
1065
        /// </summary>
1066
        /// <param name="symbol"></param>
1067
        private void SymbolModelingBySymbol(Symbol symbol)
1068
        {
1069
            SymbolModeling(symbol, null);
1070
            List<object> endObjects = new List<object>();
1071
            endObjects.Add(symbol);
1072

    
1073
            foreach (var connector in symbol.CONNECTORS)
1074
            {
1075
                object connItem = SPPIDUtil.FindObjectByUID(document, connector.CONNECTEDITEM);
1076
                if (connItem != null && connItem.GetType() != typeof(Equipment))
1077
                {
1078
                    endObjects.Add(connItem);
1079
                    if (connItem.GetType() == typeof(Symbol))
1080
                    {
1081
                        Symbol connSymbol = connItem as Symbol;
1082
                        if (string.IsNullOrEmpty(connSymbol.SPPID.RepresentationId))
1083
                        {
1084
                            SymbolModeling(connSymbol, symbol);
1085
                        }
1086
                        SymbolModelingByNeerSymbolLoop(connSymbol, endObjects);
1087
                    }
1088
                    else if (connItem.GetType() == typeof(Line))
1089
                    {
1090
                        Line connLine = connItem as Line;
1091
                        SymbolModelingByNeerLineLoop(connLine, endObjects, symbol);
1092
                    }
1093
                }
1094
            }
1095
        }
1096

    
1097
        private void SymbolModelingByNeerSymbolLoop(Symbol symbol, List<object> endObjects)
1098
        {
1099
            foreach (var connector in symbol.CONNECTORS)
1100
            {
1101
                object connItem = SPPIDUtil.FindObjectByUID(document, connector.CONNECTEDITEM);
1102
                if (connItem != null && connItem.GetType() != typeof(Equipment))
1103
                {
1104
                    if (!endObjects.Contains(connItem))
1105
                    {
1106
                        endObjects.Add(connItem);
1107
                        if (connItem.GetType() == typeof(Symbol))
1108
                        {
1109
                            Symbol connSymbol = connItem as Symbol;
1110
                            if (string.IsNullOrEmpty(connSymbol.SPPID.RepresentationId))
1111
                            {
1112
                                SymbolModeling(connSymbol, symbol);
1113
                            }
1114
                            SymbolModelingByNeerSymbolLoop(connSymbol, endObjects);
1115
                        }
1116
                        else if (connItem.GetType() == typeof(Line))
1117
                        {
1118
                            Line connLine = connItem as Line;
1119
                            SymbolModelingByNeerLineLoop(connLine, endObjects, symbol);
1120
                        }
1121
                    }
1122
                }
1123
            }
1124
        }
1125

    
1126
        private void SymbolModelingByNeerLineLoop(Line line, List<object> endObjects, Symbol prevSymbol)
1127
        {
1128
            foreach (var connector in line.CONNECTORS)
1129
            {
1130
                object connItem = SPPIDUtil.FindObjectByUID(document, connector.CONNECTEDITEM);
1131
                if (connItem != null && connItem.GetType() != typeof(Equipment))
1132
                {
1133
                    if (!endObjects.Contains(connItem))
1134
                    {
1135
                        endObjects.Add(connItem);
1136
                        if (connItem.GetType() == typeof(Symbol))
1137
                        {
1138
                            Symbol connSymbol = connItem as Symbol;
1139
                            if (string.IsNullOrEmpty(connSymbol.SPPID.RepresentationId))
1140
                            {
1141
                                List<Symbol> group = new List<Symbol>();
1142
                                SPPIDUtil.FindConnectedSymbolGroup(document, connSymbol, group);
1143
                                Symbol priority = prioritySymbols.Find(x => group.Contains(x));
1144
                                List<Symbol> endModelingGroup = new List<Symbol>();
1145
                                if (priority != null)
1146
                                {
1147
                                    SymbolGroupModeling(priority, group);
1148

    
1149
                                    // Range 겹치는지 확인해야함
1150
                                    double[] prevRange = null;
1151
                                    GetSPPIDSymbolRange(prevSymbol, ref prevRange);
1152
                                    double[] groupRange = null;
1153
                                    GetSPPIDSymbolRange(group, ref groupRange);
1154

    
1155
                                    double distanceX = 0;
1156
                                    double distanceY = 0;
1157
                                    bool overlapX = false;
1158
                                    bool overlapY = false;
1159
                                    SlopeType slopeType = SPPIDUtil.CalcSlope(prevSymbol.SPPID.ORIGINAL_X, prevSymbol.SPPID.ORIGINAL_Y, connSymbol.SPPID.ORIGINAL_X, connSymbol.SPPID.ORIGINAL_Y);
1160
                                    SPPIDUtil.CalcOverlap(prevRange, groupRange, ref distanceX, ref distanceY, ref overlapX, ref overlapY);
1161
                                    if ((slopeType == SlopeType.HORIZONTAL && overlapX) ||
1162
                                        (slopeType == SlopeType.VERTICAL && overlapY))
1163
                                    {
1164
                                        RemoveSymbol(group);
1165
                                        foreach (var _temp in group)
1166
                                            SPPIDUtil.CalcNewCoordinateForSymbol(_temp, prevSymbol, distanceX, distanceY);
1167

    
1168
                                        SymbolGroupModeling(priority, group);
1169
                                    }
1170
                                }
1171
                                else
1172
                                {
1173
                                    SymbolModeling(connSymbol, null);
1174
                                    // Range 겹치는지 확인해야함
1175
                                    double[] prevRange = null;
1176
                                    GetSPPIDSymbolRange(prevSymbol, ref prevRange);
1177
                                    double[] connRange = null;
1178
                                    GetSPPIDSymbolRange(connSymbol, ref connRange);
1179

    
1180
                                    double distanceX = 0;
1181
                                    double distanceY = 0;
1182
                                    bool overlapX = false;
1183
                                    bool overlapY = false;
1184
                                    SlopeType slopeType = SPPIDUtil.CalcSlope(prevSymbol.SPPID.ORIGINAL_X, prevSymbol.SPPID.ORIGINAL_Y, connSymbol.SPPID.ORIGINAL_X, connSymbol.SPPID.ORIGINAL_Y);
1185
                                    SPPIDUtil.CalcOverlap(prevRange, connRange, ref distanceX, ref distanceY, ref overlapX, ref overlapY);
1186
                                    if ((slopeType == SlopeType.HORIZONTAL && overlapX) ||
1187
                                        (slopeType == SlopeType.VERTICAL && overlapY))
1188
                                    {
1189
                                        RemoveSymbol(connSymbol);
1190
                                        SPPIDUtil.CalcNewCoordinateForSymbol(connSymbol, prevSymbol, distanceX, distanceY);
1191

    
1192
                                        SymbolModeling(connSymbol, null);
1193
                                    }
1194
                                }
1195
                            }
1196
                            SymbolModelingByNeerSymbolLoop(connSymbol, endObjects);
1197
                        }
1198
                        else if (connItem.GetType() == typeof(Line))
1199
                        {
1200
                            Line connLine = connItem as Line;
1201
                            if (!SPPIDUtil.IsBranchLine(connLine, line))
1202
                                SymbolModelingByNeerLineLoop(connLine, endObjects, prevSymbol);
1203
                        }
1204
                    }
1205
                }
1206
            }
1207
        }
1208

    
1209
        private void SymbolGroupModeling(Symbol firstSymbol, List<Symbol> group)
1210
        {
1211
            List<Symbol> endModelingGroup = new List<Symbol>();
1212
            SymbolModeling(firstSymbol, null);
1213
            endModelingGroup.Add(firstSymbol);
1214
            while (endModelingGroup.Count != group.Count)
1215
            {
1216
                foreach (var _symbol in group)
1217
                {
1218
                    if (!endModelingGroup.Contains(_symbol))
1219
                    {
1220
                        foreach (var _connector in _symbol.CONNECTORS)
1221
                        {
1222
                            Symbol _connSymbol = SPPIDUtil.FindObjectByUID(document, _connector.CONNECTEDITEM) as Symbol;
1223
                            if (_connSymbol != null && endModelingGroup.Contains(_connSymbol))
1224
                            {
1225
                                SymbolModeling(_symbol, _connSymbol);
1226
                                endModelingGroup.Add(_symbol);
1227
                                break;
1228
                            }
1229
                        }
1230
                    }
1231
                }
1232
            }
1233
        }
1234

    
1235
        /// <summary>
1236
        /// 심볼을 실제로 Modeling할때 ChildSymbol이 있다면 Modeling하는 메서드
1237
        /// </summary>
1238
        /// <param name="childSymbol"></param>
1239
        /// <param name="parentSymbol"></param>
1240
        private void CreateChildSymbol(ChildSymbol childSymbol, LMSymbol parentSymbol)
1241
        {
1242
            Ingr.RAD2D.Symbol2d symbol2d = radApp.ActiveDocument.ActiveSheet.DrawingObjects[parentSymbol.get_GraphicOID().ToString()];
1243
            double x1 = 0;
1244
            double x2 = 0;
1245
            double y1 = 0;
1246
            double y2 = 0;
1247
            symbol2d.Range(out x1, out y1, out x2, out y2);
1248

    
1249
            LMSymbol _LMSymbol = _placement.PIDPlaceSymbol(childSymbol.SPPID.MAPPINGNAME, (x1 + x2) / 2, (y1 + y2) / 2, TargetItem: parentSymbol);
1250
            if (_LMSymbol != null)
1251
            {
1252
                childSymbol.SPPID.RepresentationId = _LMSymbol.AsLMRepresentation().Id;
1253
                foreach (var item in childSymbol.ChildSymbols)
1254
                    CreateChildSymbol(item, _LMSymbol);
1255
            }
1256

    
1257

    
1258
            ReleaseCOMObjects(_LMSymbol);
1259
        }
1260

    
1261
        private void NewLineModeling(Line line, bool isBranchModeling = false)
1262
        {
1263
            if (!string.IsNullOrEmpty(line.SPPID.ModelItemId) || (NewBranchLines.Contains(line) && !isBranchModeling))
1264
                return;
1265

    
1266
            List<Line> group = new List<Line>();
1267
            GetConnectedLineGroup(line, group);
1268
            LineCoordinateCorrection(group);
1269

    
1270
            foreach (var groupLine in group)
1271
            {
1272
                try
1273
                {
1274
                    if (!isBranchModeling && SPPIDUtil.IsBranchLine(groupLine))
1275
                    {
1276
                        NewBranchLines.Add(groupLine);
1277
                        continue;
1278
                    }
1279

    
1280
                    _LMAItem _LMAItem = _placement.PIDCreateItem(groupLine.SPPID.MAPPINGNAME);
1281
                    PlaceRunInputs placeRunInputs = new PlaceRunInputs();
1282
                    foreach (var connector in groupLine.CONNECTORS)
1283
                    {
1284
                        double x = 0;
1285
                        double y = 0;
1286
                        GetTargetLineConnectorPoint(connector, groupLine, ref x, ref y);
1287
                        if (connector.ConnectedObject == null)
1288
                        {
1289
                            placeRunInputs.AddPoint(x, y);
1290
                        }
1291
                        else if (connector.ConnectedObject.GetType() == typeof(Symbol))
1292
                        {
1293
                            Symbol targetSymbol = connector.ConnectedObject as Symbol;
1294
                            LMSymbol _LMSymbol = GetTargetSymbol(targetSymbol, groupLine);
1295
                            placeRunInputs.AddSymbolTarget(_LMSymbol, x, y);
1296
                        }
1297
                        else if (connector.ConnectedObject.GetType() == typeof(Line))
1298
                        {
1299
                            Line targetLine = connector.ConnectedObject as Line;
1300
                            if (!string.IsNullOrEmpty(targetLine.SPPID.ModelItemId))
1301
                            {
1302
                                LMConnector targetConnector = FindTargetLMConnectorForBranch(line, targetLine, ref x, ref y);
1303
                                placeRunInputs.AddConnectorTarget(targetConnector, x, y);
1304
                                ChangeLineSPPIDCoordinateByConnector(groupLine, targetLine, x, y, false);
1305
                            }
1306
                            else
1307
                            {
1308
                                if (groupLine.CONNECTORS.IndexOf(connector) == 0)
1309
                                {
1310
                                    if (groupLine.SlopeType == SlopeType.HORIZONTAL)
1311
                                        placeRunInputs.AddPoint(x, -0.1);
1312
                                    else if (groupLine.SlopeType == SlopeType.VERTICAL)
1313
                                        placeRunInputs.AddPoint(-0.1, y);
1314
                                    else
1315
                                        placeRunInputs.AddPoint(x, -0.1);
1316
                                }
1317

    
1318
                                placeRunInputs.AddPoint(x, y);
1319

    
1320
                                if (groupLine.CONNECTORS.IndexOf(connector) == 1)
1321
                                {
1322
                                    if (groupLine.SlopeType == SlopeType.HORIZONTAL)
1323
                                        placeRunInputs.AddPoint(x, -0.1);
1324
                                    else if (groupLine.SlopeType == SlopeType.VERTICAL)
1325
                                        placeRunInputs.AddPoint(-0.1, y);
1326
                                    else
1327
                                        placeRunInputs.AddPoint(x, -0.1);
1328
                                }
1329
                            }
1330
                        }
1331
                    }
1332

    
1333
                    LMConnector _lMConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
1334
                    if (_lMConnector != null)
1335
                    {
1336
                        groupLine.SPPID.ModelItemId = _lMConnector.ModelItemID;
1337
                        ReleaseCOMObjects(_lMConnector);
1338
                    }
1339

    
1340
                    List<object> removeLines = groupLine.CONNECTORS.FindAll(x =>
1341
                    x.ConnectedObject != null &&
1342
                    x.ConnectedObject.GetType() == typeof(Line) &&
1343
                    !string.IsNullOrEmpty(((Line)x.ConnectedObject).SPPID.ModelItemId))
1344
                    .Select(x => x.ConnectedObject)
1345
                    .ToList();
1346

    
1347
                    foreach (var item in removeLines)
1348
                        RemoveLineForModeling(item as Line);
1349

    
1350
                    if (_LMAItem != null)
1351
                        ReleaseCOMObjects(_LMAItem);
1352
                    if (placeRunInputs != null)
1353
                        ReleaseCOMObjects(placeRunInputs);
1354

    
1355
                    if (isBranchModeling && NewBranchLines.Contains(groupLine))
1356
                        NewBranchLines.Remove(groupLine);
1357
                }
1358
                catch (Exception ex)
1359
                {
1360
                    throw new Exception(groupLine.UID);
1361
                }
1362
            }
1363
        }
1364

    
1365
        private void RemoveLineForModeling(Line line)
1366
        {
1367
            LMModelItem modelItem = dataSource.GetModelItem(line.SPPID.ModelItemId);
1368
            if (modelItem != null)
1369
            {
1370
                foreach (LMRepresentation rep in modelItem.Representations)
1371
                {
1372
                    if (rep.Attributes["RepresentationType"].get_Value() == "Connector" && rep.Attributes["ItemStatus"].get_Value() == "Active")
1373
                    {
1374
                        LMConnector _LMConnector = dataSource.GetConnector(rep.Id);
1375
                        dynamic OID = rep.get_GraphicOID();
1376
                        DependencyObject drawingObject = radApp.ActiveDocument.ActiveSheet.DrawingObjects[OID];
1377
                        Ingr.RAD2D.LineStringGeometry2d lineStringGeometry = drawingObject.GetGeometry() as Ingr.RAD2D.LineStringGeometry2d;
1378
                        int verticesCount = lineStringGeometry.VertexCount;
1379
                        double[] vertices = null;
1380
                        lineStringGeometry.GetVertices(ref verticesCount, ref vertices);
1381
                        for (int i = 0; i < verticesCount; i++)
1382
                        {
1383
                            double x = 0;
1384
                            double y = 0;
1385
                            lineStringGeometry.GetVertex(i + 1, ref x, ref y);
1386
                            if (verticesCount == 2 && x < 0 || y < 0)
1387
                                _placement.PIDRemovePlacement(rep);
1388
                        }
1389
                        ReleaseCOMObjects(_LMConnector);
1390
                    }
1391
                }
1392

    
1393
                ReleaseCOMObjects(modelItem);
1394
            }
1395
        }
1396

    
1397
        private void GetConnectedLineGroup(Line line, List<Line> group)
1398
        {
1399
            if (!group.Contains(line))
1400
                group.Add(line);
1401
            foreach (var connector in line.CONNECTORS)
1402
            {
1403
                if (connector.ConnectedObject != null &&
1404
                    connector.ConnectedObject.GetType() == typeof(Line) &&
1405
                    !group.Contains(connector.ConnectedObject) &&
1406
                    string.IsNullOrEmpty(((Line)connector.ConnectedObject).SPPID.ModelItemId))
1407
                {
1408
                    Line connLine = connector.ConnectedObject as Line;
1409
                    if (!SPPIDUtil.IsBranchLine(connLine, line))
1410
                        GetConnectedLineGroup(connLine, group);
1411
                }
1412
            }
1413
        }
1414

    
1415
        private void LineCoordinateCorrection(List<Line> group)
1416
        {
1417
            // 순서대로 전 Item 기준 정렬
1418
            LineCoordinateCorrectionByStart(group);
1419

    
1420
            // 역으로 심볼이 있을 경우 좌표 보정
1421
            LineCoordinateCorrectionForLastLine(group);
1422
        }
1423

    
1424
        private void LineCoordinateCorrectionByStart(List<Line> group)
1425
        {
1426
            for (int i = 0; i < group.Count; i++)
1427
            {
1428
                Line line = group[i];
1429
                if (i == 0)
1430
                {
1431
                    Connector symbolConnector = line.CONNECTORS.Find(x => x.ConnectedObject != null && x.ConnectedObject.GetType() == typeof(Symbol));
1432
                    if (symbolConnector != null)
1433
                        LineCoordinateCorrectionByConnItem(line, symbolConnector.ConnectedObject);
1434
                }
1435
                else if (i != 0)
1436
                {
1437
                    LineCoordinateCorrectionByConnItem(line, group[i - 1]);
1438
                }
1439
            }
1440
        }
1441

    
1442
        private void LineCoordinateCorrectionForLastLine(List<Line> group)
1443
        {
1444
            Line checkLine = group[group.Count - 1];
1445
            Connector lastSymbolConnector = checkLine.CONNECTORS.Find(x => x.ConnectedObject != null && x.ConnectedObject.GetType() == typeof(Symbol));
1446
            if (lastSymbolConnector != null)
1447
            {
1448
                LineCoordinateCorrectionByConnItem(checkLine, lastSymbolConnector.ConnectedObject);
1449
                for (int i = group.Count - 2; i >= 0; i--)
1450
                {
1451
                    Line line = group[i + 1];
1452
                    Line prevLine = group[i];
1453

    
1454
                    // 같으면 보정
1455
                    if (line.SlopeType == prevLine.SlopeType)
1456
                        LineCoordinateCorrectionByConnItem(prevLine, line);
1457
                    else
1458
                    {
1459
                        if (line.SlopeType == SlopeType.HORIZONTAL)
1460
                        {
1461
                            double prevX = 0;
1462
                            double prevY = 0;
1463
                            GetTargetLineConnectorPoint(prevLine.CONNECTORS.Find(z => z.ConnectedObject == line), prevLine, ref prevX, ref prevY);
1464
                            ChangeLineSPPIDCoordinateByConnectorOnlyX(line, prevLine, prevX);
1465

    
1466
                            double x = 0;
1467
                            double y = 0;
1468
                            GetTargetLineConnectorPoint(line.CONNECTORS.Find(z => z.ConnectedObject == prevLine), line, ref x, ref y);
1469
                            ChangeLineSPPIDCoordinateByConnectorOnlyY(prevLine, line, y);
1470
                        }
1471
                        else if (line.SlopeType == SlopeType.VERTICAL)
1472
                        {
1473
                            double prevX = 0;
1474
                            double prevY = 0;
1475
                            GetTargetLineConnectorPoint(prevLine.CONNECTORS.Find(z => z.ConnectedObject == line), prevLine, ref prevX, ref prevY);
1476
                            ChangeLineSPPIDCoordinateByConnectorOnlyY(line, prevLine, prevY);
1477

    
1478
                            double x = 0;
1479
                            double y = 0;
1480
                            GetTargetLineConnectorPoint(line.CONNECTORS.Find(z => z.ConnectedObject == prevLine), line, ref x, ref y);
1481
                            ChangeLineSPPIDCoordinateByConnectorOnlyX(prevLine, line, x);
1482
                        }
1483
                        break;
1484
                    }
1485
                }
1486
            }
1487
        }
1488

    
1489
        private void LineCoordinateCorrectionByConnItem(Line line, object connItem)
1490
        {
1491
            double x = 0;
1492
            double y = 0;
1493
            if (connItem.GetType() == typeof(Symbol))
1494
            {
1495
                Symbol targetSymbol = connItem as Symbol;
1496
                Connector targetConnector = targetSymbol.CONNECTORS.Find(z => z.ConnectedObject == line);
1497
                if (targetConnector != null)
1498
                    GetTargetSymbolConnectorPoint(targetConnector, targetSymbol, ref x, ref y);
1499
                else
1500
                    throw new Exception("Target symbol UID : " + targetSymbol.UID + "\r\nLine UID : " + line.UID);
1501
            }
1502
            else if (connItem.GetType() == typeof(Line))
1503
            {
1504
                Line targetLine = connItem as Line;
1505
                GetTargetLineConnectorPoint(targetLine.CONNECTORS.Find(z => z.ConnectedObject == line), targetLine, ref x, ref y);
1506
            }
1507

    
1508
            ChangeLineSPPIDCoordinateByConnector(line, connItem, x, y);
1509
        }
1510

    
1511
        private void ChangeLineSPPIDCoordinateByConnector(Line line, object connItem, double x, double y, bool changeOtherCoordinate = true)
1512
        {
1513
            Connector connector = line.CONNECTORS.Find(z => z.ConnectedObject == connItem);
1514
            int index = line.CONNECTORS.IndexOf(connector);
1515
            if (index == 0)
1516
            {
1517
                line.SPPID.START_X = x;
1518
                line.SPPID.START_Y = y;
1519
                if (line.SlopeType == SlopeType.HORIZONTAL && changeOtherCoordinate)
1520
                    line.SPPID.END_Y = y;
1521
                else if (line.SlopeType == SlopeType.VERTICAL && changeOtherCoordinate)
1522
                    line.SPPID.END_X = x;
1523
            }
1524
            else
1525
            {
1526
                line.SPPID.END_X = x;
1527
                line.SPPID.END_Y = y;
1528
                if (line.SlopeType == SlopeType.HORIZONTAL && changeOtherCoordinate)
1529
                    line.SPPID.START_Y = y;
1530
                else if (line.SlopeType == SlopeType.VERTICAL && changeOtherCoordinate)
1531
                    line.SPPID.START_X = x;
1532
            }
1533
        }
1534

    
1535
        private void ChangeLineSPPIDCoordinateByConnectorOnlyX(Line line, object connItem, double x)
1536
        {
1537
            Connector connector = line.CONNECTORS.Find(z => z.ConnectedObject == connItem);
1538
            int index = line.CONNECTORS.IndexOf(connector);
1539
            if (index == 0)
1540
            {
1541
                line.SPPID.START_X = x;
1542
                if (line.SlopeType == SlopeType.VERTICAL)
1543
                    line.SPPID.END_X = x;
1544
            }
1545
            else
1546
            {
1547
                line.SPPID.END_X = x;
1548
                if (line.SlopeType == SlopeType.VERTICAL)
1549
                    line.SPPID.START_X = x;
1550
            }
1551
        }
1552

    
1553
        private void ChangeLineSPPIDCoordinateByConnectorOnlyY(Line line, object connItem, double y)
1554
        {
1555
            Connector connector = line.CONNECTORS.Find(z => z.ConnectedObject == connItem);
1556
            int index = line.CONNECTORS.IndexOf(connector);
1557
            if (index == 0)
1558
            {
1559
                line.SPPID.START_Y = y;
1560
                if (line.SlopeType == SlopeType.HORIZONTAL)
1561
                    line.SPPID.END_Y = y;
1562
            }
1563
            else
1564
            {
1565
                line.SPPID.END_Y = y;
1566
                if (line.SlopeType == SlopeType.HORIZONTAL)
1567
                    line.SPPID.START_Y = y;
1568
            }
1569
        }
1570

    
1571
        private void NeedReModeling(Line line, LMSymbol symbol, ref bool result)
1572
        {
1573
            if (symbol != null)
1574
            {
1575
                string repID = symbol.AsLMRepresentation().Id;
1576
                string symbolUID = SPPIDUtil.FindSymbolByRepresentationID(document, repID).UID;
1577
                string lineUID = line.UID;
1578

    
1579
                SpecBreak startSpecBreak = document.SpecBreaks.Find(x =>
1580
                (x.DownStreamUID == symbolUID || x.UpStreamUID == symbolUID) &&
1581
                (x.DownStreamUID == lineUID || x.UpStreamUID == lineUID));
1582

    
1583
                EndBreak startEndBreak = document.EndBreaks.Find(x =>
1584
                (x.OWNER == symbolUID || x.PROPERTIES.Find(y => y.ATTRIBUTE == "Connected Item").VALUE == symbolUID) &&
1585
                (x.OWNER == lineUID || x.PROPERTIES.Find(y => y.ATTRIBUTE == "Connected Item").VALUE == lineUID));
1586

    
1587
                if (startSpecBreak != null || startEndBreak != null)
1588
                    result = true;
1589
            }
1590
        }
1591

    
1592
        /// <summary>
1593
        /// Symbol에 붙을 경우 Line을 Remodeling 한다.
1594
        /// </summary>
1595
        /// <param name="lines"></param>
1596
        /// <param name="prevLMConnector"></param>
1597
        /// <param name="startSymbol"></param>
1598
        /// <param name="endSymbol"></param>
1599
        private void ReModelingLine(List<Line> lines, LMConnector prevLMConnector, LMSymbol startSymbol, LMSymbol endSymbol, bool bStart, bool bEnd)
1600
        {
1601
            string symbolPath = string.Empty;
1602
            #region get symbol path
1603
            LMModelItem modelItem = dataSource.GetModelItem(prevLMConnector.ModelItemID);
1604
            foreach (LMRepresentation rep in modelItem.Representations)
1605
            {
1606
                if (!DBNull.Value.Equals(rep.get_FileName()) && !string.IsNullOrEmpty(rep.get_FileName()))
1607
                {
1608
                    symbolPath = rep.get_FileName();
1609
                    break;
1610
                }
1611
            }
1612
            #endregion
1613
            _LMAItem _LMAItem = _placement.PIDCreateItem(symbolPath);
1614
            LMConnector newConnector = null;
1615
            dynamic OID = prevLMConnector.get_GraphicOID();
1616
            DependencyObject drawingObject = radApp.ActiveDocument.ActiveSheet.DrawingObjects[OID];
1617
            Ingr.RAD2D.LineStringGeometry2d lineStringGeometry = drawingObject.GetGeometry() as Ingr.RAD2D.LineStringGeometry2d;
1618
            int verticesCount = lineStringGeometry.VertexCount;
1619
            PlaceRunInputs placeRunInputs = new PlaceRunInputs();
1620

    
1621
            List<double[]> vertices = new List<double[]>();
1622
            for (int i = 1; i <= verticesCount; i++)
1623
            {
1624
                double x = 0;
1625
                double y = 0;
1626
                lineStringGeometry.GetVertex(i, ref x, ref y);
1627
                vertices.Add(new double[] { x, y });
1628
            }
1629

    
1630
            for (int i = 0; i < vertices.Count; i++)
1631
            {
1632
                double[] points = vertices[i];
1633
                // 시작 심볼이 있고 첫번째 좌표일 때
1634
                if (startSymbol != null && i == 0)
1635
                {
1636
                    if (bStart)
1637
                    {
1638
                        SlopeType slopeType = SPPIDUtil.CalcSlope(points[0], points[1], vertices[i + 1][0], vertices[i + 1][1]);
1639
                        if (slopeType == SlopeType.HORIZONTAL)
1640
                            placeRunInputs.AddPoint(points[0], -0.1);
1641
                        else if (slopeType == SlopeType.VERTICAL)
1642
                            placeRunInputs.AddPoint(-0.1, points[1]);
1643
                        else
1644
                            placeRunInputs.AddPoint(points[0], -0.1);
1645

    
1646
                        placeRunInputs.AddPoint(points[0], points[1]);
1647
                    }
1648
                    else
1649
                    {
1650
                        placeRunInputs.AddSymbolTarget(startSymbol, points[0], points[1]);
1651
                    }
1652
                }
1653
                // 마지막 심볼이 있고 마지막 좌표일 때
1654
                else if (endSymbol != null && i == vertices.Count - 1)
1655
                {
1656
                    if (bEnd)
1657
                    {
1658
                        placeRunInputs.AddPoint(points[0], points[1]);
1659

    
1660
                        SlopeType slopeType = SPPIDUtil.CalcSlope(points[0], points[1], vertices[i - 1][0], vertices[i - 1][1]);
1661
                        if (slopeType == SlopeType.HORIZONTAL)
1662
                            placeRunInputs.AddPoint(points[0], -0.1);
1663
                        else if (slopeType == SlopeType.VERTICAL)
1664
                            placeRunInputs.AddPoint(-0.1, points[1]);
1665
                        else
1666
                            placeRunInputs.AddPoint(points[0], -0.1);
1667
                    }
1668
                    else
1669
                    {
1670
                        placeRunInputs.AddSymbolTarget(endSymbol, points[0], points[1]);
1671
                    }
1672
                }
1673
                // 첫번째이며 시작 심볼이 아니고 Connecotr일 경우
1674
                else if (i == 0 && prevLMConnector.ConnectItem1SymbolObject != null)
1675
                    placeRunInputs.AddSymbolTarget(prevLMConnector.ConnectItem1SymbolObject, points[0], points[1]);
1676
                // 마지막이며 마지막 심볼이 아니고 Connecotr일 경우
1677
                else if (i == vertices.Count - 1 && prevLMConnector.ConnectItem2SymbolObject != null)
1678
                    placeRunInputs.AddSymbolTarget(prevLMConnector.ConnectItem2SymbolObject, points[0], points[1]);
1679
                else
1680
                    placeRunInputs.AddPoint(points[0], points[1]);
1681
            }
1682

    
1683
            _placement.PIDRemovePlacement(prevLMConnector.AsLMRepresentation());
1684
            newConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
1685

    
1686
            ReleaseCOMObjects(placeRunInputs);
1687
            ReleaseCOMObjects(_LMAItem);
1688
            ReleaseCOMObjects(modelItem);
1689

    
1690
            if (newConnector != null)
1691
            {
1692
                if (startSymbol != null && bStart)
1693
                {
1694
                    _LMAItem = _placement.PIDCreateItem(symbolPath);
1695
                    placeRunInputs = new PlaceRunInputs();
1696
                    placeRunInputs.AddSymbolTarget(startSymbol, vertices[0][0], vertices[0][1]);
1697
                    placeRunInputs.AddConnectorTarget(newConnector, vertices[0][0], vertices[0][1]);
1698
                    LMConnector _LMConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
1699
                    if (_LMConnector != null)
1700
                    {
1701
                        RemoveConnectorForReModelingLine(newConnector);
1702
                        ZeroLengthModelItemID.Add(_LMConnector.ModelItemID);
1703
                        ReleaseCOMObjects(_LMConnector);
1704
                    }
1705
                    ReleaseCOMObjects(placeRunInputs);
1706
                    ReleaseCOMObjects(_LMAItem);
1707
                }
1708

    
1709
                if (endSymbol != null && bEnd)
1710
                {
1711
                    if (startSymbol != null)
1712
                    {
1713
                        Dictionary<LMConnector, List<double[]>> dicVertices = GetPipeRunVertices(newConnector.ModelItemID);
1714
                        newConnector = dicVertices.First().Key;
1715
                    }
1716

    
1717
                    _LMAItem = _placement.PIDCreateItem(symbolPath);
1718
                    placeRunInputs = new PlaceRunInputs();
1719
                    placeRunInputs.AddSymbolTarget(endSymbol, vertices[vertices.Count - 1][0], vertices[vertices.Count - 1][1]);
1720
                    placeRunInputs.AddConnectorTarget(newConnector, vertices[vertices.Count - 1][0], vertices[vertices.Count - 1][1]);
1721
                    LMConnector _LMConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
1722
                    if (_LMConnector != null)
1723
                    {
1724
                        RemoveConnectorForReModelingLine(newConnector);
1725
                        ZeroLengthModelItemID.Add(_LMConnector.ModelItemID);
1726
                        ReleaseCOMObjects(_LMConnector);
1727
                    }
1728
                    ReleaseCOMObjects(placeRunInputs);
1729
                    ReleaseCOMObjects(_LMAItem);
1730
                }
1731

    
1732
                foreach (var line in lines)
1733
                    line.SPPID.ModelItemId = newConnector.ModelItemID;
1734
                ReleaseCOMObjects(newConnector);
1735
            }
1736

    
1737
            ReleaseCOMObjects(modelItem);
1738
        }
1739

    
1740
        /// <summary>
1741
        /// Remodeling 과정에서 생긴 불필요한 Connector 제거
1742
        /// </summary>
1743
        /// <param name="connector"></param>
1744
        private void RemoveConnectorForReModelingLine(LMConnector connector)
1745
        {
1746
            Dictionary<LMConnector, List<double[]>> dicVertices = GetPipeRunVertices(connector.ModelItemID);
1747
            foreach (var item in dicVertices)
1748
            {
1749
                bool result = false;
1750
                foreach (var point in item.Value)
1751
                {
1752
                    if (point[0] < 0 || point[1] < 0)
1753
                    {
1754
                        result = true;
1755
                        _placement.PIDRemovePlacement(item.Key.AsLMRepresentation());
1756
                        break;
1757
                    }
1758
                }
1759

    
1760
                if (result)
1761
                    break;
1762
            }
1763
            foreach (var item in dicVertices)
1764
                ReleaseCOMObjects(item.Key);
1765
        }
1766

    
1767
        /// <summary>
1768
        /// Symbol이 모델링된 SPPPID Symbol Object를 반환 - 연결된 Symbol이 ChildSymbol일 수도 있기때문에 메서드 개발
1769
        /// </summary>
1770
        /// <param name="symbol"></param>
1771
        /// <param name="line"></param>
1772
        /// <returns></returns>
1773
        private LMSymbol GetTargetSymbol(Symbol symbol, Line line)
1774
        {
1775
            LMSymbol _LMSymbol = null;
1776
            foreach (var connector in symbol.CONNECTORS)
1777
            {
1778
                if (connector.CONNECTEDITEM == line.UID)
1779
                {
1780
                    if (connector.Index == 0)
1781
                        _LMSymbol = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
1782
                    else
1783
                    {
1784
                        ChildSymbol child = null;
1785
                        foreach (var childSymbol in symbol.ChildSymbols)
1786
                        {
1787
                            if (childSymbol.Connectors.Contains(connector))
1788
                                child = childSymbol;
1789
                            else
1790
                                child = GetChildSymbolByConnector(childSymbol, connector);
1791

    
1792
                            if (child != null)
1793
                                break;
1794
                        }
1795

    
1796
                        if (child != null)
1797
                            _LMSymbol = dataSource.GetSymbol(child.SPPID.RepresentationId);
1798
                    }
1799

    
1800
                    break;
1801
                }
1802
            }
1803

    
1804
            return _LMSymbol;
1805
        }
1806

    
1807
        /// <summary>
1808
        /// Connector를 가지고 있는 ChildSymbol Object 반환
1809
        /// </summary>
1810
        /// <param name="item"></param>
1811
        /// <param name="connector"></param>
1812
        /// <returns></returns>
1813
        private ChildSymbol GetChildSymbolByConnector(ChildSymbol item, Connector connector)
1814
        {
1815
            foreach (var childSymbol in item.ChildSymbols)
1816
            {
1817
                if (childSymbol.Connectors.Contains(connector))
1818
                    return childSymbol;
1819
                else
1820
                    return GetChildSymbolByConnector(childSymbol, connector);
1821
            }
1822

    
1823
            return null;
1824
        }
1825

    
1826
        /// <summary>
1827
        /// EndBreak 모델링 메서드
1828
        /// </summary>
1829
        /// <param name="endBreak"></param>
1830
        private void EndBreakModeling(EndBreak endBreak)
1831
        {
1832
            object ownerObj = SPPIDUtil.FindObjectByUID(document, endBreak.OWNER);
1833
            object connectedItem = SPPIDUtil.FindObjectByUID(document, endBreak.PROPERTIES.Find(x => x.ATTRIBUTE == "Connected Item").VALUE);
1834
            LMConnector targetLMConnector = FindBreakLineTarget(ownerObj, connectedItem);
1835

    
1836
            if (targetLMConnector != null)
1837
            {
1838
                Array array = new double[] { 0, endBreak.SPPID.ORIGINAL_X, endBreak.SPPID.ORIGINAL_Y };
1839
                LMLabelPersist _LmLabelPersist = _placement.PIDPlaceLabel(endBreak.SPPID.MAPPINGNAME, ref array, Rotation: 0, LabeledItem: targetLMConnector.AsLMRepresentation(), IsLeaderVisible: true);
1840
            }
1841
        }
1842

    
1843
        private LMConnector ReModelingLMConnector(LMConnector connector)
1844
        {
1845
            string symbolPath = string.Empty;
1846
            #region get symbol path
1847
            LMModelItem modelItem = dataSource.GetModelItem(connector.ModelItemID);
1848
            foreach (LMRepresentation rep in modelItem.Representations)
1849
            {
1850
                if (!DBNull.Value.Equals(rep.get_FileName()) && !string.IsNullOrEmpty(rep.get_FileName()))
1851
                {
1852
                    symbolPath = rep.get_FileName();
1853
                    break;
1854
                }
1855
            }
1856
            #endregion
1857

    
1858
            LMConnector newConnector = null;
1859
            dynamic OID = connector.get_GraphicOID();
1860
            DependencyObject drawingObject = radApp.ActiveDocument.ActiveSheet.DrawingObjects[OID];
1861
            Ingr.RAD2D.LineStringGeometry2d lineStringGeometry = drawingObject.GetGeometry() as Ingr.RAD2D.LineStringGeometry2d;
1862
            int verticesCount = lineStringGeometry.VertexCount;
1863
            PlaceRunInputs placeRunInputs = new PlaceRunInputs();
1864
            _LMAItem _LMAItem = _placement.PIDCreateItem(symbolPath);
1865

    
1866
            if (Convert.ToBoolean(connector.get_IsZeroLength()))
1867
            {
1868
                double[] vertices = null;
1869
                lineStringGeometry.GetVertices(ref verticesCount, ref vertices);
1870
                double x = 0;
1871
                double y = 0;
1872
                lineStringGeometry.GetVertex(1, ref x, ref y);
1873

    
1874
                placeRunInputs.AddSymbolTarget(connector.ConnectItem1SymbolObject, x, y);
1875
                placeRunInputs.AddSymbolTarget(connector.ConnectItem2SymbolObject, x, y);
1876

    
1877
                _placement.PIDRemovePlacement(connector.AsLMRepresentation());
1878
                newConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
1879
            }
1880
            else
1881
            {
1882
                List<double[]> vertices = new List<double[]>();
1883
                for (int i = 1; i <= verticesCount; i++)
1884
                {
1885
                    double x = 0;
1886
                    double y = 0;
1887
                    lineStringGeometry.GetVertex(i, ref x, ref y);
1888
                    vertices.Add(new double[] { x, y });
1889
                }
1890

    
1891
                for (int i = 0; i < vertices.Count; i++)
1892
                {
1893
                    double[] points = vertices[i];
1894
                    if (i == 0)
1895
                    {
1896
                        if (connector.ConnectItem1SymbolObject != null)
1897
                            placeRunInputs.AddSymbolTarget(connector.ConnectItem1SymbolObject, points[0], points[1]);
1898
                        else
1899
                            placeRunInputs.AddPoint(points[0], points[1]);
1900
                    }
1901
                    else if (i == vertices.Count - 1)
1902
                    {
1903
                        if (connector.ConnectItem2SymbolObject != null)
1904
                            placeRunInputs.AddSymbolTarget(connector.ConnectItem2SymbolObject, points[0], points[1]);
1905
                        else
1906
                            placeRunInputs.AddPoint(points[0], points[1]);
1907
                    }
1908
                    else
1909
                        placeRunInputs.AddPoint(points[0], points[1]);
1910
                }
1911

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

    
1914
                _placement.PIDRemovePlacement(connector.AsLMRepresentation());
1915
                newConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
1916

    
1917
                foreach (var line in lines)
1918
                    line.SPPID.ModelItemId = newConnector.ModelItemID;
1919
            }
1920

    
1921

    
1922
            return newConnector;
1923
        }
1924

    
1925
        /// <summary>
1926
        /// SpecBreak Modeling 메서드
1927
        /// </summary>
1928
        /// <param name="specBreak"></param>
1929
        private void SpecBreakModeling(SpecBreak specBreak)
1930
        {
1931
            object upStreamObj = SPPIDUtil.FindObjectByUID(document, specBreak.UpStreamUID);
1932
            object downStreamObj = SPPIDUtil.FindObjectByUID(document, specBreak.DownStreamUID);
1933

    
1934
            if (upStreamObj != null &&
1935
                downStreamObj != null)
1936
            {
1937
                LMConnector targetLMConnector = FindBreakLineTarget(upStreamObj, downStreamObj);
1938

    
1939
                if (targetLMConnector != null)
1940
                {
1941
                    foreach (var attribute in specBreak.ATTRIBUTES)
1942
                    {
1943
                        AttributeMapping mapping = document.AttributeMappings.Find(x => x.UID == attribute.UID);
1944
                        if (mapping != null && !string.IsNullOrEmpty(mapping.SPPIDSYMBOLNAME) && mapping.SPPIDSYMBOLNAME != "None")
1945
                        {
1946
                            string MappingPath = mapping.SPPIDSYMBOLNAME;
1947
                            Array array = new double[] { 0, specBreak.SPPID.ORIGINAL_X, specBreak.SPPID.ORIGINAL_Y };
1948
                            LMLabelPersist _LmLabelPersist = _placement.PIDPlaceLabel(MappingPath, ref array, Rotation: specBreak.ANGLE, LabeledItem: targetLMConnector.AsLMRepresentation(), IsLeaderVisible: mapping.LeaderLine);
1949

    
1950
                            if (_LmLabelPersist != null)
1951
                            {
1952
                                ReleaseCOMObjects(_LmLabelPersist);
1953
                            }
1954
                        }
1955
                    }
1956
                    ReleaseCOMObjects(targetLMConnector);
1957
                }
1958
            }
1959
        }
1960

    
1961
        private LMConnector FindBreakLineTarget(object targetObj, object connectedObj)
1962
        {
1963
            LMConnector targetConnector = null;
1964
            Symbol targetSymbol = targetObj as Symbol;
1965
            Symbol connectedSymbol = connectedObj as Symbol;
1966
            Line targetLine = targetObj as Line;
1967
            Line connectedLine = connectedObj as Line;
1968
            if (targetSymbol != null && connectedSymbol != null)
1969
            {
1970
                LMSymbol targetLMSymbol = dataSource.GetSymbol(targetSymbol.SPPID.RepresentationId);
1971
                LMSymbol connectedLMSymbol = dataSource.GetSymbol(connectedSymbol.SPPID.RepresentationId);
1972

    
1973
                foreach (LMConnector connector in targetLMSymbol.Avoid1Connectors)
1974
                {
1975
                    if (connector.get_ItemStatus() != "Active")
1976
                        continue;
1977

    
1978
                    if (connector.ConnectItem1SymbolObject.Id == connectedLMSymbol.Id)
1979
                    {
1980
                        targetConnector = connector;
1981
                        break;
1982
                    }
1983
                    else if (connector.ConnectItem2SymbolObject.Id == connectedLMSymbol.Id)
1984
                    {
1985
                        targetConnector = connector;
1986
                        break;
1987
                    }
1988
                }
1989

    
1990
                foreach (LMConnector connector in targetLMSymbol.Avoid2Connectors)
1991
                {
1992
                    if (connector.get_ItemStatus() != "Active")
1993
                        continue;
1994

    
1995
                    if (connector.ConnectItem1SymbolObject.Id == connectedLMSymbol.Id)
1996
                    {
1997
                        targetConnector = connector;
1998
                        break;
1999
                    }
2000
                    else if (connector.ConnectItem2SymbolObject.Id == connectedLMSymbol.Id)
2001
                    {
2002
                        targetConnector = connector;
2003
                        break;
2004
                    }
2005
                }
2006

    
2007
                ReleaseCOMObjects(targetLMSymbol);
2008
                ReleaseCOMObjects(connectedLMSymbol);
2009
            }
2010
            else if (targetLine != null && connectedLine != null)
2011
            {
2012
                LMModelItem targetModelItem = dataSource.GetModelItem(targetLine.SPPID.ModelItemId);
2013
                LMModelItem connectedModelItem = dataSource.GetModelItem(connectedLine.SPPID.ModelItemId);
2014

    
2015
                if (targetModelItem != null && connectedModelItem != null)
2016
                {
2017
                    foreach (LMRepresentation rep in targetModelItem.Representations)
2018
                    {
2019
                        if (targetConnector != null)
2020
                            break;
2021

    
2022
                        if (rep.Attributes["RepresentationType"].get_Value() == "Connector" && rep.Attributes["ItemStatus"].get_Value() == "Active")
2023
                        {
2024
                            LMConnector _LMConnector = dataSource.GetConnector(rep.Id);
2025

    
2026
                            if (IsConnected(_LMConnector, connectedModelItem))
2027
                                targetConnector = _LMConnector;
2028
                            else
2029
                                ReleaseCOMObjects(_LMConnector);
2030
                        }
2031
                    }
2032

    
2033
                    ReleaseCOMObjects(targetModelItem);
2034
                }
2035
            }
2036
            else
2037
            {
2038
                LMSymbol connectedLMSymbol = null;
2039
                if (connectedSymbol != null)
2040
                    connectedLMSymbol = dataSource.GetSymbol(connectedSymbol.SPPID.RepresentationId);
2041
                else if (targetSymbol != null)
2042
                    connectedLMSymbol = dataSource.GetSymbol(targetSymbol.SPPID.RepresentationId);
2043
                else
2044
                {
2045

    
2046
                }
2047
                LMModelItem targetModelItem = null;
2048
                if (targetLine != null)
2049
                    targetModelItem = dataSource.GetModelItem(targetLine.SPPID.ModelItemId);
2050
                else if (connectedLine != null)
2051
                    targetModelItem = dataSource.GetModelItem(connectedLine.SPPID.ModelItemId);
2052
                else
2053
                {
2054

    
2055
                }
2056
                if (connectedLMSymbol != null && targetModelItem != null)
2057
                {
2058
                    foreach (LMConnector connector in connectedLMSymbol.Avoid1Connectors)
2059
                    {
2060
                        if (connector.get_ItemStatus() != "Active")
2061
                            continue;
2062

    
2063
                        if (IsConnected(connector, targetModelItem))
2064
                        {
2065
                            targetConnector = connector;
2066
                            break;
2067
                        }
2068
                    }
2069

    
2070
                    if (targetConnector == null)
2071
                    {
2072
                        foreach (LMConnector connector in connectedLMSymbol.Avoid2Connectors)
2073
                        {
2074
                            if (connector.get_ItemStatus() != "Active")
2075
                                continue;
2076

    
2077
                            if (IsConnected(connector, targetModelItem))
2078
                            {
2079
                                targetConnector = connector;
2080
                                break;
2081
                            }
2082
                        }
2083
                    }
2084
                }
2085

    
2086
            }
2087

    
2088
            return targetConnector;
2089
        }
2090

    
2091
        private bool IsConnected(LMConnector connector, LMModelItem modelItem)
2092
        {
2093
            bool result = false;
2094

    
2095
            foreach (LMRepresentation rep in modelItem.Representations)
2096
            {
2097
                if (result)
2098
                    break;
2099

    
2100
                if (rep.Attributes["RepresentationType"].get_Value() == "Connector" && rep.Attributes["ItemStatus"].get_Value() == "Active")
2101
                {
2102
                    LMConnector _LMConnector = dataSource.GetConnector(rep.Id);
2103

    
2104
                    if (_LMConnector.ConnectItem1SymbolObject != null &&
2105
                        connector.ConnectItem1SymbolObject != null &&
2106
                        _LMConnector.ConnectItem1SymbolObject.Id == connector.ConnectItem1SymbolObject.Id)
2107
                    {
2108
                        result = true;
2109
                        ReleaseCOMObjects(_LMConnector);
2110
                        break;
2111
                    }
2112
                    else if (_LMConnector.ConnectItem1SymbolObject != null &&
2113
                        connector.ConnectItem2SymbolObject != null &&
2114
                        _LMConnector.ConnectItem1SymbolObject.Id == connector.ConnectItem2SymbolObject.Id)
2115
                    {
2116
                        result = true;
2117
                        ReleaseCOMObjects(_LMConnector);
2118
                        break;
2119
                    }
2120
                    else if (_LMConnector.ConnectItem2SymbolObject != null &&
2121
                        connector.ConnectItem1SymbolObject != null &&
2122
                        _LMConnector.ConnectItem2SymbolObject.Id == connector.ConnectItem1SymbolObject.Id)
2123
                    {
2124
                        result = true;
2125
                        ReleaseCOMObjects(_LMConnector);
2126
                        break;
2127
                    }
2128
                    else if (_LMConnector.ConnectItem2SymbolObject != null &&
2129
                        connector.ConnectItem2SymbolObject != null &&
2130
                        _LMConnector.ConnectItem2SymbolObject.Id == connector.ConnectItem2SymbolObject.Id)
2131
                    {
2132
                        result = true;
2133
                        ReleaseCOMObjects(_LMConnector);
2134
                        break;
2135
                    }
2136

    
2137
                    ReleaseCOMObjects(_LMConnector);
2138
                }
2139
            }
2140

    
2141

    
2142
            return result;
2143
        }
2144

    
2145
        /// <summary>
2146
        /// FromModelItem을 ToModelItem으로 PipeRunJoin하는 메서드
2147
        /// </summary>
2148
        /// <param name="fromModelItemId"></param>
2149
        /// <param name="toModelItemId"></param>
2150
        private void JoinPipeRun(string fromModelItemId, string toModelItemId)
2151
        {
2152
            LMModelItem modelItem1 = dataSource.GetModelItem(toModelItemId);
2153
            _LMAItem item1 = modelItem1.AsLMAItem();
2154
            LMModelItem modelItem2 = dataSource.GetModelItem(fromModelItemId);
2155
            _LMAItem item2 = modelItem2.AsLMAItem();
2156

    
2157
            // item2가 item1으로 조인
2158
            _placement.PIDJoinRuns(ref item1, ref item2);
2159
            item1.Commit();
2160
            item2.Commit();
2161

    
2162
            List<Line> lines = SPPIDUtil.FindLinesByModelId(document, fromModelItemId);
2163
            foreach (var line in lines)
2164
                line.SPPID.ModelItemId = toModelItemId;
2165

    
2166
            ReleaseCOMObjects(modelItem1);
2167
            ReleaseCOMObjects(item1);
2168
            ReleaseCOMObjects(modelItem2);
2169
            ReleaseCOMObjects(item2);
2170
        }
2171

    
2172
        /// <summary>
2173
        /// PipeRun을 자동으로 Join하는 메서드
2174
        /// </summary>
2175
        /// <param name="modelItemId"></param>
2176
        private void AutoJoinPipeRun(string modelItemId)
2177
        {
2178
            LMModelItem modelItem = dataSource.GetModelItem(modelItemId);
2179
            _LMAItem item = modelItem.AsLMAItem();
2180
            if (modelItem.get_ItemStatus() == "Active")
2181
            {
2182
                string modelitemID = item.Id;
2183
                _placement.PIDAutoJoin(item, AutoJoinEndConstants.autoJoin_Both, ref item);
2184
                string afterModelItemID = item.Id;
2185

    
2186
                if (modelitemID != afterModelItemID)
2187
                {
2188
                    List<Line> lines = SPPIDUtil.FindLinesByModelId(document, modelitemID);
2189
                    foreach (var line in lines)
2190
                        line.SPPID.ModelItemId = afterModelItemID;
2191
                }
2192
                item.Commit();
2193
            }
2194

    
2195
            ReleaseCOMObjects(modelItem);
2196
            ReleaseCOMObjects(item);
2197
        }
2198

    
2199
        /// <summary>
2200
        /// LineRun에 있는 Line들을 Join하는 진입 메서드
2201
        /// </summary>
2202
        /// <param name="run"></param>
2203
        private void JoinRunLine(LineRun run)
2204
        {
2205
            string modelItemId = string.Empty;
2206
            foreach (var item in run.RUNITEMS)
2207
            {
2208
                if (item.GetType() == typeof(Line))
2209
                {
2210
                    Line line = item as Line;
2211
                    AutoJoinPipeRun(line.SPPID.ModelItemId);
2212
                    modelItemId = line.SPPID.ModelItemId;
2213
                }
2214
            }
2215
        }
2216

    
2217
        /// <summary>
2218
        /// PipeRun의 좌표를 가져오는 메서드
2219
        /// </summary>
2220
        /// <param name="modelId"></param>
2221
        /// <returns></returns>
2222
        private Dictionary<LMConnector, List<double[]>> GetPipeRunVertices(string modelId)
2223
        {
2224
            Dictionary<LMConnector, List<double[]>> connectorVertices = new Dictionary<LMConnector, List<double[]>>();
2225
            LMModelItem modelItem = dataSource.GetModelItem(modelId);
2226

    
2227
            if (modelItem != null)
2228
            {
2229
                foreach (LMRepresentation rep in modelItem.Representations)
2230
                {
2231
                    if (rep.Attributes["RepresentationType"].get_Value() == "Connector" && rep.Attributes["ItemStatus"].get_Value() == "Active")
2232
                    {
2233
                        LMConnector _LMConnector = dataSource.GetConnector(rep.Id);
2234
                        connectorVertices.Add(_LMConnector, new List<double[]>());
2235
                        dynamic OID = rep.get_GraphicOID();
2236
                        DependencyObject drawingObject = radApp.ActiveDocument.ActiveSheet.DrawingObjects[OID];
2237
                        Ingr.RAD2D.LineStringGeometry2d lineStringGeometry = drawingObject.GetGeometry() as Ingr.RAD2D.LineStringGeometry2d;
2238
                        int verticesCount = lineStringGeometry.VertexCount;
2239
                        double[] vertices = null;
2240
                        lineStringGeometry.GetVertices(ref verticesCount, ref vertices);
2241
                        for (int i = 0; i < verticesCount; i++)
2242
                        {
2243
                            double x = 0;
2244
                            double y = 0;
2245
                            lineStringGeometry.GetVertex(i + 1, ref x, ref y);
2246
                            connectorVertices[_LMConnector].Add(new double[] { Math.Round(x, 10), Math.Round(y, 10) });
2247
                        }
2248
                    }
2249
                }
2250

    
2251
                ReleaseCOMObjects(modelItem);
2252
            }
2253

    
2254
            return connectorVertices;
2255
        }
2256

    
2257
        /// <summary>
2258
        /// 좌표로 PipeRun의 Connector중에 어느 Connector에 가까운지/붙을지 가져오는 메서드 - 조건에 안맞아서 못찾을시 제일 가까운 점으로 가져오는 방식
2259
        /// </summary>
2260
        /// <param name="connectorVertices"></param>
2261
        /// <param name="connX"></param>
2262
        /// <param name="connY"></param>
2263
        /// <returns></returns>
2264
        private LMConnector FindTargetLMConnectorForLabel(Dictionary<LMConnector, List<double[]>> connectorVertices, double connX, double connY)
2265
        {
2266
            double length = double.MaxValue;
2267
            LMConnector targetConnector = null;
2268
            foreach (var item in connectorVertices)
2269
            {
2270
                List<double[]> points = item.Value;
2271
                for (int i = 0; i < points.Count - 1; i++)
2272
                {
2273
                    double[] point1 = points[i];
2274
                    double[] point2 = points[i + 1];
2275
                    double x1 = Math.Min(point1[0], point2[0]);
2276
                    double y1 = Math.Min(point1[1], point2[1]);
2277
                    double x2 = Math.Max(point1[0], point2[0]);
2278
                    double y2 = Math.Max(point1[1], point2[1]);
2279

    
2280
                    if ((x1 <= connX && x2 >= connX) ||
2281
                        (y1 <= connY && y2 >= connY))
2282
                    {
2283
                        double distance = SPPIDUtil.CalcPointToPointdDistance(point1[0], point1[1], connX, connY);
2284
                        if (length >= distance)
2285
                        {
2286
                            targetConnector = item.Key;
2287
                            length = distance;
2288
                        }
2289

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

    
2300
            // 못찾았을때.
2301
            length = double.MaxValue;
2302
            if (targetConnector == null)
2303
            {
2304
                foreach (var item in connectorVertices)
2305
                {
2306
                    List<double[]> points = item.Value;
2307

    
2308
                    foreach (double[] point in points)
2309
                    {
2310
                        double distance = SPPIDUtil.CalcPointToPointdDistance(point[0], point[1], connX, connY);
2311
                        if (length >= distance)
2312
                        {
2313
                            targetConnector = item.Key;
2314
                            length = distance;
2315
                        }
2316
                    }
2317
                }
2318
            }
2319

    
2320
            return targetConnector;
2321
        }
2322

    
2323
        private LMConnector FindTargetLMConnectorForBranch(Line line, Line targetLine, ref double x, ref double y)
2324
        {
2325
            Dictionary<LMConnector, List<double[]>> vertices = GetPipeRunVertices(targetLine.SPPID.ModelItemId);
2326

    
2327
            double length = double.MaxValue;
2328
            LMConnector targetConnector = null;
2329
            double[] resultPoint = null;
2330
            List<double[]> targetVertices = null;
2331

    
2332
            // Vertices 포인트에 제일 가까운곳
2333
            foreach (var item in vertices)
2334
            {
2335
                List<double[]> points = item.Value;
2336
                for (int i = 0; i < points.Count; i++)
2337
                {
2338
                    double[] point = points[i];
2339
                    double tempX = point[0];
2340
                    double tempY = point[1];
2341

    
2342
                    double distance = SPPIDUtil.CalcPointToPointdDistance(tempX, tempY, x, y);
2343
                    if (length >= distance)
2344
                    {
2345
                        targetConnector = item.Key;
2346
                        length = distance;
2347
                        resultPoint = point;
2348
                        targetVertices = item.Value;
2349
                    }
2350
                }
2351
            }
2352

    
2353
            // Vertices Cross에 제일 가까운곳
2354
            foreach (var item in vertices)
2355
            {
2356
                List<double[]> points = item.Value;
2357
                for (int i = 0; i < points.Count - 1; i++)
2358
                {
2359
                    double[] point1 = points[i];
2360
                    double[] point2 = points[i + 1];
2361

    
2362
                    double maxLineX = Math.Max(point1[0], point2[0]);
2363
                    double minLineX = Math.Min(point1[0], point2[0]);
2364
                    double maxLineY = Math.Max(point1[1], point2[1]);
2365
                    double minLineY = Math.Min(point1[1], point2[1]);
2366

    
2367
                    SlopeType slope = SPPIDUtil.CalcSlope(minLineX, minLineY, maxLineX, maxLineY);
2368

    
2369
                    double[] crossingPoint = SPPIDUtil.CalcLineCrossingPoint(line.SPPID.START_X, line.SPPID.START_Y, line.SPPID.END_X, line.SPPID.END_Y, point1[0], point1[1], point2[0], point2[1]);
2370
                    if (crossingPoint != null)
2371
                    {
2372
                        double distance = SPPIDUtil.CalcPointToPointdDistance(crossingPoint[0], crossingPoint[1], x, y);
2373
                        if (length >= distance)
2374
                        {
2375
                            if (slope == SlopeType.Slope &&
2376
                                minLineX <= crossingPoint[0] && maxLineX >= crossingPoint[0] &&
2377
                                minLineY <= crossingPoint[1] && maxLineY >= crossingPoint[1])
2378
                            {
2379
                                targetConnector = item.Key;
2380
                                length = distance;
2381
                                resultPoint = crossingPoint;
2382
                                targetVertices = item.Value;
2383
                            }
2384
                            else if (slope == SlopeType.HORIZONTAL &&
2385
                                minLineX <= crossingPoint[0] && maxLineX >= crossingPoint[0])
2386
                            {
2387
                                targetConnector = item.Key;
2388
                                length = distance;
2389
                                resultPoint = crossingPoint;
2390
                                targetVertices = item.Value;
2391
                            }
2392
                            else if (slope == SlopeType.VERTICAL &&
2393
                               minLineY <= crossingPoint[1] && maxLineY >= crossingPoint[1])
2394
                            {
2395
                                targetConnector = item.Key;
2396
                                length = distance;
2397
                                resultPoint = crossingPoint;
2398
                                targetVertices = item.Value;
2399
                            }
2400
                        }
2401
                    }
2402
                }
2403
            }
2404

    
2405
            foreach (var item in vertices)
2406
                if (item.Key != null && item.Key != targetConnector)
2407
                    ReleaseCOMObjects(item.Key);
2408

    
2409
            if (SPPIDUtil.IsBranchLine(line, targetLine))
2410
            {
2411
                double tempResultX = resultPoint[0];
2412
                double tempResultY = resultPoint[1];
2413
                SPPIDUtil.ConvertGridPoint(ref tempResultX, ref tempResultY);
2414

    
2415
                GridSetting gridSetting = GridSetting.GetInstance();
2416

    
2417
                for (int i = 0; i < targetVertices.Count; i++)
2418
                {
2419
                    double[] point = targetVertices[i];
2420
                    double tempX = targetVertices[i][0];
2421
                    double tempY = targetVertices[i][1];
2422
                    SPPIDUtil.ConvertGridPoint(ref tempX, ref tempY);
2423
                    if (tempX == tempResultX && tempY == tempResultY)
2424
                    {
2425
                        if (i == 0)
2426
                        {
2427
                            LMSymbol connSymbol = targetConnector.ConnectItem1SymbolObject;
2428
                            if (connSymbol == null ||
2429
                                (connSymbol != null && connSymbol.get_ItemStatus() == "Active" && connSymbol.get_RepresentationType() != "Branch"))
2430
                            {
2431
                                bool bCalcX = false;
2432
                                bool bCalcY = false;
2433
                                if (targetLine.SlopeType == SlopeType.HORIZONTAL)
2434
                                    bCalcX = true;
2435
                                else if (targetLine.SlopeType == SlopeType.VERTICAL)
2436
                                    bCalcY = true;
2437
                                else
2438
                                {
2439
                                    bCalcX = true;
2440
                                    bCalcY = true;
2441
                                }
2442

    
2443
                                if (bCalcX)
2444
                                {
2445
                                    double nextX = targetVertices[i + 1][0];
2446
                                    double newX = 0;
2447
                                    if (nextX > tempX)
2448
                                    {
2449
                                        newX = tempX + gridSetting.Length;
2450
                                        if (newX > nextX)
2451
                                            newX = (point[0] + nextX) / 2;
2452
                                    }
2453
                                    else
2454
                                    {
2455
                                        newX = tempX - gridSetting.Length;
2456
                                        if (newX < nextX)
2457
                                            newX = (point[0] + nextX) / 2;
2458
                                    }
2459
                                    resultPoint = new double[] { newX, resultPoint[1] };
2460
                                }
2461

    
2462
                                if (bCalcY)
2463
                                {
2464
                                    double nextY = targetVertices[i + 1][1];
2465
                                    double newY = 0;
2466
                                    if (nextY > tempY)
2467
                                    {
2468
                                        newY = tempY + gridSetting.Length;
2469
                                        if (newY > nextY)
2470
                                            newY = (point[1] + nextY) / 2;
2471
                                    }
2472
                                    else
2473
                                    {
2474
                                        newY = tempY - gridSetting.Length;
2475
                                        if (newY < nextY)
2476
                                            newY = (point[1] + nextY) / 2;
2477
                                    }
2478
                                    resultPoint = new double[] { resultPoint[0], newY };
2479
                                }
2480
                            }
2481
                        }
2482
                        else if (i == targetVertices.Count - 1)
2483
                        {
2484
                            LMSymbol connSymbol = targetConnector.ConnectItem2SymbolObject;
2485
                            if (connSymbol == null ||
2486
                                (connSymbol != null && connSymbol.get_ItemStatus() == "Active" && connSymbol.get_RepresentationType() != "Branch"))
2487
                            {
2488
                                bool bCalcX = false;
2489
                                bool bCalcY = false;
2490
                                if (targetLine.SlopeType == SlopeType.HORIZONTAL)
2491
                                    bCalcX = true;
2492
                                else if (targetLine.SlopeType == SlopeType.VERTICAL)
2493
                                    bCalcY = true;
2494
                                else
2495
                                {
2496
                                    bCalcX = true;
2497
                                    bCalcY = true;
2498
                                }
2499

    
2500
                                if (bCalcX)
2501
                                {
2502
                                    double nextX = targetVertices[i - 1][0];
2503
                                    double newX = 0;
2504
                                    if (nextX > tempX)
2505
                                    {
2506
                                        newX = tempX + gridSetting.Length;
2507
                                        if (newX > nextX)
2508
                                            newX = (point[0] + nextX) / 2;
2509
                                    }
2510
                                    else
2511
                                    {
2512
                                        newX = tempX - gridSetting.Length;
2513
                                        if (newX < nextX)
2514
                                            newX = (point[0] + nextX) / 2;
2515
                                    }
2516
                                    resultPoint = new double[] { newX, resultPoint[1] };
2517
                                }
2518

    
2519
                                if (bCalcY)
2520
                                {
2521
                                    double nextY = targetVertices[i - 1][1];
2522
                                    double newY = 0;
2523
                                    if (nextY > tempY)
2524
                                    {
2525
                                        newY = tempY + gridSetting.Length;
2526
                                        if (newY > nextY)
2527
                                            newY = (point[1] + nextY) / 2;
2528
                                    }
2529
                                    else
2530
                                    {
2531
                                        newY = tempY - gridSetting.Length;
2532
                                        if (newY < nextY)
2533
                                            newY = (point[1] + nextY) / 2;
2534
                                    }
2535
                                    resultPoint = new double[] { resultPoint[0], newY };
2536
                                }
2537
                            }
2538
                        }
2539
                        break;
2540
                    }
2541
                }
2542
            }
2543

    
2544
            x = resultPoint[0];
2545
            y = resultPoint[1];
2546

    
2547
            return targetConnector;
2548
        }
2549

    
2550
        /// <summary>
2551
        /// Line Number Symbol을 실제로 Modeling하는 메서드
2552
        /// </summary>
2553
        /// <param name="lineNumber"></param>
2554
        private void LineNumberModeling(LineNumber lineNumber)
2555
        {
2556
            object obj = SPPIDUtil.FindObjectByUID(document, lineNumber.CONNLINE);
2557
            if (obj != null)
2558
            {
2559
                Line line = obj as Line;
2560
                Dictionary<LMConnector, List<double[]>> connectorVertices = GetPipeRunVertices(line.SPPID.ModelItemId);
2561
                LMConnector connectedLMConnector = FindTargetLMConnectorForLabel(connectorVertices, lineNumber.SPPID.ORIGINAL_X, lineNumber.SPPID.ORIGINAL_Y);
2562
                if (connectedLMConnector != null)
2563
                {
2564
                    double x = 0;
2565
                    double y = 0;
2566
                    CalcLabelLocation(ref x, ref y, lineNumber.SPPID.ORIGINAL_X, lineNumber.SPPID.ORIGINAL_Y, lineNumber.SPPIDLabelLocation, _ETCSetting.LineNumberLocation);
2567

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

    
2571
                    foreach (var item in connectorVertices)
2572
                        ReleaseCOMObjects(item.Key);
2573
                    if (_LmLabelPresist != null)
2574
                    {
2575
                        _LmLabelPresist.Commit();
2576
                        lineNumber.SPPID.RepresentationId = _LmLabelPresist.AsLMRepresentation().Id;
2577
                        ReleaseCOMObjects(_LmLabelPresist);
2578
                    }
2579
                    else
2580
                    {
2581

    
2582
                    }
2583
                }
2584
            }
2585
        }
2586

    
2587
        /// <summary>
2588
        /// Flow Mark Modeling
2589
        /// </summary>
2590
        /// <param name="line"></param>
2591
        private void FlowMarkModeling(Line line)
2592
        {
2593
            if (line.FLOWMARK && !string.IsNullOrEmpty(line.SPPID.ModelItemId) && !string.IsNullOrEmpty(_ETCSetting.FlowMarkSymbolPath))
2594
            {
2595
                SlopeType targetSlopeType = SPPIDUtil.CalcSlope(line.SPPID.START_X, line.SPPID.START_Y, line.SPPID.END_X, line.SPPID.END_Y);
2596
                string mappingPath = _ETCSetting.FlowMarkSymbolPath;
2597
                double percent = line.FLOWMARK_PERCENT;
2598
                double tempX = 0;
2599
                double tempY = 0;
2600

    
2601
                double gapX;
2602
                double gapY;
2603
                // ID2 기준의 Gap을 구함
2604
                if (percent == 0)
2605
                {
2606
                    gapX = 0;
2607
                    gapY = 0;
2608
                }
2609
                else
2610
                {
2611
                    gapX = Math.Abs(line.SPPID.START_X - line.SPPID.END_X) / 100 * percent;
2612
                    gapY = Math.Abs(line.SPPID.START_Y - line.SPPID.END_Y) / 100 * percent;
2613
                }
2614

    
2615
                if (line.SPPID.START_X < line.SPPID.END_X)
2616
                    tempX = line.SPPID.START_X + gapX;
2617
                else
2618
                    tempX = line.SPPID.START_X - gapX;
2619

    
2620
                if (line.SPPID.START_Y < line.SPPID.END_Y)
2621
                    tempY = line.SPPID.START_Y + gapY;
2622
                else
2623
                    tempY = line.SPPID.START_Y - gapY;
2624

    
2625

    
2626

    
2627
                Dictionary<LMConnector, List<double[]>> connectorVertices = GetPipeRunVertices(line.SPPID.ModelItemId);
2628
                LMConnector _TargetItem = null;
2629
                double distance = double.MaxValue;
2630
                double[] startPoint = null;
2631
                double[] endPoint = null;
2632
                // ID2의 기준 Gap으로 제일 가까운 Line 찾음(Slope도 같은조건)
2633
                foreach (var item in connectorVertices)
2634
                {
2635
                    for (int i = 0; i < item.Value.Count - 1; i++)
2636
                    {
2637
                        List<double[]> points = item.Value;
2638
                        double[] point1 = points[i];
2639
                        double[] point2 = points[i + 1];
2640

    
2641
                        SlopeType slopeType = SPPIDUtil.CalcSlope(point1[0], point1[1], point2[0], point2[1]);
2642
                        if (slopeType == targetSlopeType)
2643
                        {
2644
                            double result = SPPIDUtil.CalcPointToPointdDistance(point1[0], point1[1], tempX, tempY);
2645
                            if (result < distance)
2646
                            {
2647
                                distance = result;
2648
                                _TargetItem = item.Key;
2649

    
2650
                                startPoint = point1;
2651
                                endPoint = point2;
2652
                            }
2653

    
2654
                            result = SPPIDUtil.CalcPointToPointdDistance(point2[0], point2[1], tempX, tempY);
2655
                            if (result < distance)
2656
                            {
2657
                                distance = result;
2658
                                _TargetItem = item.Key;
2659

    
2660
                                startPoint = point1;
2661
                                endPoint = point2;
2662
                            }
2663
                        }
2664
                    }
2665
                }
2666

    
2667
                if (_TargetItem != null)
2668
                {
2669
                    double x = 0;
2670
                    double y = 0;
2671
                    double angle = 0;
2672
                    // SPPID 기준의 Gap으로 실 좌표를 구함
2673
                    if (percent == 0)
2674
                    {
2675
                        gapX = 0;
2676
                        gapY = 0;
2677
                    }
2678
                    else
2679
                    {
2680
                        gapX = Math.Abs(startPoint[0] - endPoint[0]) / 100 * percent;
2681
                        gapY = Math.Abs(startPoint[1] - endPoint[1]) / 100 * percent;
2682
                    }
2683

    
2684
                    if (startPoint[0] < endPoint[0])
2685
                        x = startPoint[0] + gapX;
2686
                    else
2687
                        x = startPoint[0] - gapX;
2688

    
2689
                    if (startPoint[1] < endPoint[1])
2690
                        y = startPoint[1] + gapY;
2691
                    else
2692
                        y = startPoint[1] - gapY;
2693

    
2694
                    if (targetSlopeType == SlopeType.HORIZONTAL)
2695
                    {
2696
                        if (startPoint[0] < endPoint[0])
2697
                            angle = 0;
2698
                        else
2699
                            angle = Math.PI;
2700
                    }
2701
                    // 90 270
2702
                    else if (targetSlopeType == SlopeType.VERTICAL)
2703
                    {
2704
                        if (startPoint[1] < endPoint[1])
2705
                            angle = 90 * Math.PI / 180;
2706
                        else
2707
                            angle = 270 * Math.PI / 180;
2708
                    }
2709

    
2710
                    LMSymbol _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: 0, Rotation: angle, TargetItem: _TargetItem);
2711

    
2712
                    if (_LMSymbol != null)
2713
                    {
2714
                        ReleaseCOMObjects(_LMSymbol);
2715
                    }
2716

    
2717
                }
2718

    
2719
                foreach (var item in connectorVertices)
2720
                    ReleaseCOMObjects(item.Key);
2721
            }
2722
        }
2723

    
2724
        /// <summary>
2725
        /// Line Number 기준으로 모든 Item에 Line Number의 Attribute Input
2726
        /// </summary>
2727
        /// <param name="lineNumber"></param>
2728
        private void InputLineNumberAttribute(LineNumber lineNumber)
2729
        {
2730
            foreach (LineRun run in lineNumber.RUNS)
2731
            {
2732
                foreach (var item in run.RUNITEMS)
2733
                {
2734
                    if (item.GetType() == typeof(Symbol))
2735
                    {
2736
                        Symbol symbol = item as Symbol;
2737
                        LMSymbol _LMSymbol = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
2738
                        if (_LMSymbol != null)
2739
                        {
2740
                            LMModelItem _LMModelItem = _LMSymbol.ModelItemObject;
2741

    
2742
                            if (_LMModelItem != null && _LMModelItem.get_ItemStatus() == "Active")
2743
                            {
2744
                                foreach (var attribute in lineNumber.ATTRIBUTES)
2745
                                {
2746
                                    LineNumberMapping mapping = document.LineNumberMappings.Find(x => x.UID == attribute.UID);
2747
                                    if (mapping != null && !string.IsNullOrEmpty(attribute.VALUE) && attribute.VALUE != "None")
2748
                                    {
2749
                                        LMAAttribute _LMAAttribute = _LMModelItem.Attributes[mapping.SPPIDATTRIBUTENAME];
2750
                                        if (_LMAAttribute != null)
2751
                                        {
2752
                                            if (DBNull.Value.Equals(_LMAAttribute.get_Value()))
2753
                                                _LMAAttribute.set_Value(attribute.VALUE);
2754
                                            else if (_LMAAttribute.get_Value() != attribute.VALUE)
2755
                                                _LMAAttribute.set_Value(attribute.VALUE);
2756
                                        }
2757
                                    }
2758
                                }
2759
                                _LMModelItem.Commit();
2760
                            }
2761
                            if (_LMModelItem != null)
2762
                                ReleaseCOMObjects(_LMModelItem);
2763
                        }
2764
                        if (_LMSymbol != null)
2765
                            ReleaseCOMObjects(_LMSymbol);
2766
                    }
2767
                    else if (item.GetType() == typeof(Line))
2768
                    {
2769
                        Line line = item as Line;
2770
                        if (line != null)
2771
                        {
2772
                            LMModelItem _LMModelItem = dataSource.GetModelItem(line.SPPID.ModelItemId);
2773
                            if (_LMModelItem != null && _LMModelItem.get_ItemStatus() == "Active")
2774
                            {
2775
                                foreach (var attribute in lineNumber.ATTRIBUTES)
2776
                                {
2777
                                    LineNumberMapping mapping = document.LineNumberMappings.Find(x => x.UID == attribute.UID);
2778
                                    if (mapping != null && !string.IsNullOrEmpty(attribute.VALUE) && attribute.VALUE != "None")
2779
                                    {
2780
                                        LMAAttribute _LMAAttribute = _LMModelItem.Attributes[mapping.SPPIDATTRIBUTENAME];
2781
                                        if (_LMAAttribute != null)
2782
                                        {
2783
                                            if (DBNull.Value.Equals(_LMAAttribute.get_Value()))
2784
                                                _LMAAttribute.set_Value(attribute.VALUE);
2785
                                            else if (_LMAAttribute.get_Value() != attribute.VALUE)
2786
                                                _LMAAttribute.set_Value(attribute.VALUE);
2787

    
2788
                                        }
2789
                                    }
2790
                                }
2791
                                _LMModelItem.Commit();
2792
                            }
2793
                            if (_LMModelItem != null)
2794
                                ReleaseCOMObjects(_LMModelItem);
2795
                        }
2796
                    }
2797
                }
2798
            }
2799
        }
2800

    
2801
        /// <summary>
2802
        /// Symbol Attribute 입력 메서드
2803
        /// </summary>
2804
        /// <param name="item"></param>
2805
        private void InputSymbolAttribute(object targetItem, List<BaseModel.Attribute> targetAttributes)
2806
        {
2807

    
2808
            // Object 아이템이 Symbol일 경우 Equipment일 경우 
2809
            string sRep = null;
2810
            if (targetItem.GetType() == typeof(Symbol))
2811
                sRep = ((Symbol)targetItem).SPPID.RepresentationId;
2812
            else if (targetItem.GetType() == typeof(Equipment))
2813
                sRep = ((Equipment)targetItem).SPPID.RepresentationId;
2814

    
2815
            if (!string.IsNullOrEmpty(sRep))
2816
            {
2817
                LMSymbol _LMSymbol = dataSource.GetSymbol(sRep);
2818
                LMModelItem _LMModelItem = _LMSymbol.ModelItemObject;
2819
                LMAAttributes _Attributes = _LMModelItem.Attributes;
2820

    
2821
                foreach (var item in targetAttributes)
2822
                {
2823
                    AttributeMapping mapping = document.AttributeMappings.Find(x => x.UID == item.UID);
2824
                    if (mapping != null && !string.IsNullOrEmpty(item.VALUE) && item.VALUE != "None")
2825
                    {
2826
                        LMAAttribute _Attribute = _Attributes[mapping.SPPIDATTRIBUTENAME];
2827
                        if (_Attribute != null)
2828
                            _Attribute.set_Value(item.VALUE);
2829
                    }
2830
                }
2831
                _LMModelItem.Commit();
2832

    
2833
                ReleaseCOMObjects(_Attributes);
2834
                ReleaseCOMObjects(_LMModelItem);
2835
                ReleaseCOMObjects(_LMSymbol);
2836
            }
2837
        }
2838

    
2839
        /// <summary>
2840
        /// Input SpecBreak Attribute
2841
        /// </summary>
2842
        /// <param name="specBreak"></param>
2843
        private void InputSpecBreakAttribute(SpecBreak specBreak)
2844
        {
2845
            object upStreamObj = SPPIDUtil.FindObjectByUID(document, specBreak.UpStreamUID);
2846
            object downStreamObj = SPPIDUtil.FindObjectByUID(document, specBreak.DownStreamUID);
2847

    
2848
            if (upStreamObj != null &&
2849
                downStreamObj != null)
2850
            {
2851
                LMConnector targetLMConnector = FindBreakLineTarget(upStreamObj, downStreamObj);
2852

    
2853
                if (targetLMConnector != null)
2854
                {
2855
                    foreach (LMLabelPersist _LMLabelPersist in targetLMConnector.LabelPersists)
2856
                    {
2857
                        string symbolPath = _LMLabelPersist.get_FileName();
2858
                        AttributeMapping mapping = document.AttributeMappings.Find(x => x.SPPIDSYMBOLNAME == symbolPath);
2859
                        if (mapping != null)
2860
                        {
2861
                            BaseModel.Attribute attribute = specBreak.ATTRIBUTES.Find(y => y.UID == mapping.UID);
2862
                            if (attribute != null && !string.IsNullOrEmpty(attribute.VALUE) && attribute.VALUE != "None")
2863
                            {
2864
                                string[] values = attribute.VALUE.Split(new char[] { ',' });
2865
                                if (values.Length == 2)
2866
                                {
2867
                                    string upStreamValue = values[0];
2868
                                    string downStreamValue = values[1];
2869

    
2870
                                    InputAttributeForSpecBreak(upStreamObj, downStreamObj, upStreamValue, downStreamValue, mapping.SPPIDATTRIBUTENAME);
2871
                                }
2872
                            }
2873
                        }
2874
                    }
2875

    
2876
                    ReleaseCOMObjects(targetLMConnector);
2877
                }
2878
            }
2879

    
2880

    
2881
            #region 내부에서만 쓰는 메서드
2882
            void InputAttributeForSpecBreak(object _upStreamObj, object _downStreamObj, string upStreamValue, string downStreamValue, string sppidAttributeName)
2883
            {
2884
                Symbol upStreamSymbol = _upStreamObj as Symbol;
2885
                Line upStreamLine = _upStreamObj as Line;
2886
                Symbol downStreamSymbol = _downStreamObj as Symbol;
2887
                Line downStreamLine = _downStreamObj as Line;
2888
                // 둘다 Line일 경우
2889
                if (upStreamLine != null && downStreamLine != null)
2890
                {
2891
                    InputLineAttributeForSpecBreakLine(upStreamLine, sppidAttributeName, upStreamValue);
2892
                    InputLineAttributeForSpecBreakLine(downStreamLine, sppidAttributeName, downStreamValue);
2893
                }
2894
                // 둘다 Symbol일 경우
2895
                else if (upStreamSymbol != null && downStreamSymbol != null)
2896
                {
2897
                    LMConnector zeroLenthConnector = FindBreakLineTarget(upStreamSymbol, downStreamSymbol);
2898
                    LMSymbol upStreamLMSymbol = dataSource.GetSymbol(upStreamSymbol.SPPID.RepresentationId);
2899
                    LMSymbol downStreamLMSymbol = dataSource.GetSymbol(downStreamSymbol.SPPID.RepresentationId);
2900

    
2901
                    foreach (LMConnector connector in upStreamLMSymbol.Avoid1Connectors)
2902
                    {
2903
                        if (connector.get_ItemStatus() != "Active")
2904
                            continue;
2905

    
2906
                        if (connector.Id != zeroLenthConnector.Id)
2907
                            InputLineAttributeForSpecBreakLMConnector(connector, sppidAttributeName, upStreamValue);
2908
                    }
2909

    
2910
                    foreach (LMConnector connector in upStreamLMSymbol.Avoid2Connectors)
2911
                    {
2912
                        if (connector.get_ItemStatus() != "Active")
2913
                            continue;
2914

    
2915
                        if (connector.Id != zeroLenthConnector.Id)
2916
                            InputLineAttributeForSpecBreakLMConnector(connector, sppidAttributeName, upStreamValue);
2917
                    }
2918

    
2919
                    foreach (LMConnector connector in downStreamLMSymbol.Avoid1Connectors)
2920
                    {
2921
                        if (connector.get_ItemStatus() != "Active")
2922
                            continue;
2923

    
2924
                        if (connector.Id != zeroLenthConnector.Id)
2925
                            InputLineAttributeForSpecBreakLMConnector(connector, sppidAttributeName, downStreamValue);
2926
                    }
2927

    
2928
                    foreach (LMConnector connector in downStreamLMSymbol.Avoid2Connectors)
2929
                    {
2930
                        if (connector.get_ItemStatus() != "Active")
2931
                            continue;
2932

    
2933
                        if (connector.Id != zeroLenthConnector.Id)
2934
                            InputLineAttributeForSpecBreakLMConnector(connector, sppidAttributeName, downStreamValue);
2935
                    }
2936

    
2937
                    ReleaseCOMObjects(zeroLenthConnector);
2938
                    ReleaseCOMObjects(upStreamLMSymbol);
2939
                    ReleaseCOMObjects(downStreamLMSymbol);
2940
                }
2941
                else if (upStreamSymbol != null && downStreamLine != null)
2942
                {
2943
                    LMConnector zeroLenthConnector = FindBreakLineTarget(upStreamSymbol, downStreamLine);
2944
                    InputLineAttributeForSpecBreakLine(downStreamLine, sppidAttributeName, downStreamValue);
2945
                    LMSymbol upStreamLMSymbol = dataSource.GetSymbol(upStreamSymbol.SPPID.RepresentationId);
2946

    
2947
                    foreach (LMConnector connector in upStreamLMSymbol.Avoid1Connectors)
2948
                    {
2949
                        if (connector.get_ItemStatus() != "Active")
2950
                            continue;
2951

    
2952
                        if (connector.Id != zeroLenthConnector.Id)
2953
                            InputLineAttributeForSpecBreakLMConnector(connector, sppidAttributeName, upStreamValue);
2954
                    }
2955

    
2956
                    foreach (LMConnector connector in upStreamLMSymbol.Avoid2Connectors)
2957
                    {
2958
                        if (connector.get_ItemStatus() != "Active")
2959
                            continue;
2960

    
2961
                        if (connector.Id != zeroLenthConnector.Id)
2962
                            InputLineAttributeForSpecBreakLMConnector(connector, sppidAttributeName, upStreamValue);
2963
                    }
2964

    
2965
                    ReleaseCOMObjects(zeroLenthConnector);
2966
                    ReleaseCOMObjects(upStreamLMSymbol);
2967
                }
2968
                else if (upStreamLine != null && downStreamSymbol != null)
2969
                {
2970
                    LMConnector zeroLenthConnector = FindBreakLineTarget(upStreamLine, downStreamSymbol);
2971
                    InputLineAttributeForSpecBreakLine(upStreamLine, sppidAttributeName, upStreamValue);
2972
                    LMSymbol downStreamLMSymbol = dataSource.GetSymbol(downStreamSymbol.SPPID.RepresentationId);
2973

    
2974
                    foreach (LMConnector connector in downStreamLMSymbol.Avoid1Connectors)
2975
                    {
2976
                        if (connector.get_ItemStatus() != "Active")
2977
                            continue;
2978

    
2979
                        if (connector.Id != zeroLenthConnector.Id)
2980
                            InputLineAttributeForSpecBreakLMConnector(connector, sppidAttributeName, downStreamValue);
2981
                    }
2982

    
2983
                    foreach (LMConnector connector in downStreamLMSymbol.Avoid2Connectors)
2984
                    {
2985
                        if (connector.get_ItemStatus() != "Active")
2986
                            continue;
2987

    
2988
                        if (connector.Id != zeroLenthConnector.Id)
2989
                            InputLineAttributeForSpecBreakLMConnector(connector, sppidAttributeName, downStreamValue);
2990
                    }
2991

    
2992
                    ReleaseCOMObjects(zeroLenthConnector);
2993
                    ReleaseCOMObjects(downStreamLMSymbol);
2994
                }
2995
            }
2996

    
2997
            void InputLineAttributeForSpecBreakLine(Line line, string attrName, string value)
2998
            {
2999
                LMModelItem _LMModelItem = dataSource.GetModelItem(line.SPPID.ModelItemId);
3000
                if (_LMModelItem != null && _LMModelItem.get_ItemStatus() == "Active")
3001
                {
3002
                    LMAAttribute _LMAAttribute = _LMModelItem.Attributes[attrName];
3003
                    if (_LMAAttribute != null)
3004
                    {
3005
                        if (DBNull.Value.Equals(_LMAAttribute.get_Value()))
3006
                            _LMAAttribute.set_Value(value);
3007
                        else if (_LMAAttribute.get_Value() != value)
3008
                            _LMAAttribute.set_Value(value);
3009
                    }
3010

    
3011
                    _LMModelItem.Commit();
3012
                }
3013
                if (_LMModelItem != null)
3014
                    ReleaseCOMObjects(_LMModelItem);
3015
            }
3016

    
3017
            void InputLineAttributeForSpecBreakLMConnector(LMConnector connector, string attrName, string value)
3018
            {
3019
                LMModelItem _LMModelItem = dataSource.GetModelItem(connector.ModelItemID);
3020
                if (_LMModelItem != null && _LMModelItem.get_ItemStatus() == "Active")
3021
                {
3022
                    LMAAttribute _LMAAttribute = _LMModelItem.Attributes[attrName];
3023
                    if (_LMAAttribute != null)
3024
                    {
3025
                        if (DBNull.Value.Equals(_LMAAttribute.get_Value()))
3026
                            _LMAAttribute.set_Value(value);
3027
                        else if (_LMAAttribute.get_Value() != value)
3028
                            _LMAAttribute.set_Value(value);
3029
                    }
3030

    
3031
                    _LMModelItem.Commit();
3032
                }
3033
                if (_LMModelItem != null)
3034
                    ReleaseCOMObjects(_LMModelItem);
3035
            }
3036
            #endregion
3037
        }
3038

    
3039
        /// <summary>
3040
        /// Text Modeling - Association일 경우는 Text대신 해당 맵핑된 Symbol로 모델링
3041
        /// </summary>
3042
        /// <param name="text"></param>
3043
        private void TextModeling(Text text)
3044
        {
3045
            LMSymbol _LMSymbol = null;
3046
            LMConnector connectedLMConnector = null;
3047
            //if (text.ASSOCIATION && !string.IsNullOrEmpty(text.OWNER) && text.OWNER != "None")
3048
            if (text.ASSOCIATION)
3049
            {
3050
                object owner = SPPIDUtil.FindObjectByUID(document, text.OWNER);
3051
                if (owner.GetType() == typeof(Symbol))
3052
                {
3053
                    Symbol symbol = owner as Symbol;
3054
                    _LMSymbol = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
3055
                    if (_LMSymbol != null)
3056
                    {
3057
                        BaseModel.Attribute attribute = symbol.ATTRIBUTES.Find(x => x.ASSOCITEM == text.UID);
3058
                        if (attribute != null && !string.IsNullOrEmpty(attribute.VALUE) && attribute.VALUE != "None")
3059
                        {
3060
                            AttributeMapping mapping = document.AttributeMappings.Find(x => x.UID == attribute.UID && !string.IsNullOrEmpty(x.SPPIDSYMBOLNAME));
3061

    
3062
                            if (mapping != null)
3063
                            {
3064
                                double x = 0;
3065
                                double y = 0;
3066

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

    
3070
                                LMLabelPersist _LMLabelPersist = _placement.PIDPlaceLabel(mapping.SPPIDSYMBOLNAME, ref array, Rotation: text.ANGLE, LabeledItem: _LMSymbol.AsLMRepresentation(), IsLeaderVisible: mapping.LeaderLine);
3071
                                if (_LMLabelPersist != null)
3072
                                {
3073
                                    _LMLabelPersist.Commit();
3074
                                    ReleaseCOMObjects(_LMLabelPersist);
3075
                                }
3076
                            }
3077
                        }
3078
                    }
3079
                }
3080
                else if (owner.GetType() == typeof(Line))
3081
                {
3082
                    Line line = owner as Line;
3083
                    Dictionary<LMConnector, List<double[]>> connectorVertices = GetPipeRunVertices(line.SPPID.ModelItemId);
3084
                    connectedLMConnector = FindTargetLMConnectorForLabel(connectorVertices, text.SPPID.ORIGINAL_X, text.SPPID.ORIGINAL_Y);
3085

    
3086
                    if (connectedLMConnector != null)
3087
                    {
3088
                        BaseModel.Attribute attribute = line.ATTRIBUTES.Find(x => x.ASSOCITEM == text.UID);
3089
                        if (attribute != null && !string.IsNullOrEmpty(attribute.VALUE) && attribute.VALUE != "None")
3090
                        {
3091
                            AttributeMapping mapping = document.AttributeMappings.Find(x => x.UID == attribute.UID && !string.IsNullOrEmpty(x.SPPIDSYMBOLNAME));
3092

    
3093
                            if (mapping != null)
3094
                            {
3095
                                double x = 0;
3096
                                double y = 0;
3097

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

    
3101
                                LMLabelPersist _LMLabelPersist = _placement.PIDPlaceLabel(mapping.SPPIDSYMBOLNAME, ref array, Rotation: text.ANGLE, LabeledItem: connectedLMConnector.AsLMRepresentation(), IsLeaderVisible: mapping.LeaderLine);
3102
                                if (_LMLabelPersist != null)
3103
                                {
3104
                                    _LMLabelPersist.Commit();
3105
                                    ReleaseCOMObjects(_LMLabelPersist);
3106
                                }
3107
                            }
3108
                        }
3109
                    }
3110
                }
3111
            }
3112
            else
3113
            {
3114
                LMItemNote _LMItemNote = null;
3115
                LMAAttribute _LMAAttribute = null;
3116

    
3117
                double x = 0;
3118
                double y = 0;
3119
                double angle = text.ANGLE;
3120
                CalcLabelLocation(ref x, ref y, text.SPPID.ORIGINAL_X, text.SPPID.ORIGINAL_Y, text.SPPIDLabelLocation, _ETCSetting.TextLocation);
3121

    
3122
                _LMSymbol = _placement.PIDPlaceSymbol(text.SPPID.MAPPINGNAME, x, y, Rotation: angle);
3123
                _LMSymbol.Commit();
3124
                _LMItemNote = _placement.PIDDataSource.GetItemNote(_LMSymbol.ModelItemID);
3125
                _LMItemNote.Commit();
3126
                _LMAAttribute = _LMItemNote.Attributes["Note.Body"];
3127
                _LMAAttribute.set_Value(text.VALUE);
3128
                _LMItemNote.Commit();
3129

    
3130
                if (_LMAAttribute != null)
3131
                    ReleaseCOMObjects(_LMAAttribute);
3132
                if (_LMItemNote != null)
3133
                    ReleaseCOMObjects(_LMItemNote);
3134
            }
3135
            if (_LMSymbol != null)
3136
                ReleaseCOMObjects(_LMSymbol);
3137
        }
3138

    
3139
        /// <summary>
3140
        /// Note Modeling
3141
        /// </summary>
3142
        /// <param name="note"></param>
3143
        private void NoteModeling(Note note)
3144
        {
3145
            LMSymbol _LMSymbol = null;
3146
            LMItemNote _LMItemNote = null;
3147
            LMAAttribute _LMAAttribute = null;
3148

    
3149
            if (string.IsNullOrEmpty(note.OWNER) || note.OWNER == "None")
3150
            {
3151
                double x = 0;
3152
                double y = 0;
3153

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

    
3156
                _LMSymbol = _placement.PIDPlaceSymbol(note.SPPID.MAPPINGNAME, x, y);
3157
                _LMSymbol.Commit();
3158
                _LMItemNote = _placement.PIDDataSource.GetItemNote(_LMSymbol.ModelItemID);
3159
                _LMItemNote.Commit();
3160
                _LMAAttribute = _LMItemNote.Attributes["Note.Body"];
3161
                _LMAAttribute.set_Value(note.VALUE);
3162
                _LMItemNote.Commit();
3163
            }
3164

    
3165
            if (_LMAAttribute != null)
3166
                ReleaseCOMObjects(_LMAAttribute);
3167
            if (_LMItemNote != null)
3168
                ReleaseCOMObjects(_LMItemNote);
3169
            if (_LMSymbol != null)
3170
                ReleaseCOMObjects(_LMSymbol);
3171
        }
3172

    
3173
        /// <summary>
3174
        /// Note Symbol Modeling
3175
        /// </summary>
3176
        /// <param name="symbol"></param>
3177
        private void NoteSymbolModeling(Symbol symbol)
3178
        {
3179
            if (symbol.TYPE == "Notes")
3180
            {
3181
                string mappingPath = symbol.SPPID.MAPPINGNAME;
3182
                double x = symbol.SPPID.ORIGINAL_X;
3183
                double y = symbol.SPPID.ORIGINAL_Y;
3184
                int mirror = 0;
3185
                double angle = symbol.ANGLE;
3186

    
3187
                LMSymbol _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle);
3188
                symbol.SPPID.RepresentationId = _LMSymbol.AsLMRepresentation().Id;
3189
                symbol.SPPID.ModelItemID = _LMSymbol.ModelItemID;
3190

    
3191
                ReleaseCOMObjects(_LMSymbol);
3192

    
3193
                InputSymbolAttribute(symbol, symbol.ATTRIBUTES);
3194
            }
3195
        }
3196

    
3197
        /// <summary>
3198
        /// Label의 좌표를 구하는 메서드(ID2 기준의 좌표 -> SPPID 좌표)
3199
        /// </summary>
3200
        /// <param name="x"></param>
3201
        /// <param name="y"></param>
3202
        /// <param name="originX"></param>
3203
        /// <param name="originY"></param>
3204
        /// <param name="SPPIDLabelLocation"></param>
3205
        /// <param name="location"></param>
3206
        private void CalcLabelLocation(ref double x, ref double y, double originX, double originY, SPPIDLabelLocationInfo SPPIDLabelLocation, Location location)
3207
        {
3208
            if (location == Location.None)
3209
            {
3210
                x = originX;
3211
                y = originY;
3212
            }
3213
            else
3214
            {
3215
                if (location.HasFlag(Location.Center))
3216
                {
3217
                    x = (SPPIDLabelLocation.X1 + SPPIDLabelLocation.X2) / 2;
3218
                    y = (SPPIDLabelLocation.Y1 + SPPIDLabelLocation.Y2) / 2;
3219
                }
3220

    
3221
                if (location.HasFlag(Location.Left))
3222
                    x = SPPIDLabelLocation.X1;
3223
                else if (location.HasFlag(Location.Right))
3224
                    x = SPPIDLabelLocation.X2;
3225

    
3226
                if (location.HasFlag(Location.Down))
3227
                    y = SPPIDLabelLocation.Y1;
3228
                else if (location.HasFlag(Location.Up))
3229
                    y = SPPIDLabelLocation.Y2;
3230
            }
3231
        }
3232

    
3233
        /// <summary>
3234
        /// Symbol의 우선순위 Modeling 목록을 가져온다.
3235
        /// 1. Angle Valve
3236
        /// 2. 3개로 이루어진 Symbol Group
3237
        /// </summary>
3238
        /// <returns></returns>
3239
        private List<Symbol> GetPrioritySymbol()
3240
        {
3241
            DataTable symbolTable = document.SymbolTable;
3242
            // List에 순서대로 쌓는다.
3243
            List<Symbol> symbols = new List<Symbol>();
3244

    
3245
            // Angle Valve 부터
3246
            foreach (var symbol in document.SYMBOLS.FindAll(x => x.CONNECTORS.FindAll(y => y.Index == 0).Count == 2))
3247
            {
3248
                if (!symbols.Contains(symbol))
3249
                {
3250
                    double originX = 0;
3251
                    double originY = 0;
3252

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

    
3257
                    SlopeType slopeType1 = SlopeType.None;
3258
                    SlopeType slopeType2 = SlopeType.None;
3259
                    foreach (Connector connector in symbol.CONNECTORS.FindAll(x => x.Index == 0))
3260
                    {
3261
                        double connectorX = 0;
3262
                        double connectorY = 0;
3263
                        SPPIDUtil.ConvertPointBystring(connector.CONNECTPOINT, ref connectorX, ref connectorY);
3264
                        if (slopeType1 == SlopeType.None)
3265
                            slopeType1 = SPPIDUtil.CalcSlope(originX, originY, connectorX, connectorY);
3266
                        else
3267
                            slopeType2 = SPPIDUtil.CalcSlope(originX, originY, connectorX, connectorY);
3268
                    }
3269

    
3270
                    if ((slopeType1 == SlopeType.VERTICAL && slopeType2 == SlopeType.HORIZONTAL) ||
3271
                        (slopeType2 == SlopeType.VERTICAL && slopeType1 == SlopeType.HORIZONTAL))
3272
                        symbols.Add(symbol);
3273
                }
3274
            }
3275

    
3276
            List<Symbol> tempSymbols = new List<Symbol>();
3277
            // Conn 갯수 기준
3278
            foreach (var item in document.SYMBOLS)
3279
            {
3280
                if (!symbols.Contains(item))
3281
                    tempSymbols.Add(item);
3282
            }
3283
            tempSymbols.Sort(SortSymbolPriority);
3284
            symbols.AddRange(tempSymbols);
3285

    
3286
            return symbols;
3287
        }
3288

    
3289
        private void SetPriorityLine()
3290
        {
3291
            document.LINES.Sort(SortLinePriority);
3292

    
3293
            int SortLinePriority(Line a, Line b)
3294
            {
3295
                // Branch 없는것부터
3296
                int branchRetval = CompareBranchLine(a, b);
3297
                if (branchRetval != 0)
3298
                {
3299
                    return branchRetval;
3300
                }
3301
                else
3302
                {
3303
                    // Symbol 연결 갯수
3304
                    int connSymbolRetval = CompareConnSymbol(a, b);
3305
                    if (connSymbolRetval != 0)
3306
                    {
3307
                        return connSymbolRetval;
3308
                    }
3309
                    else
3310
                    {
3311
                        // 아이템 연결 갯수(심볼, Line이면서 Not Branch)
3312
                        int connItemRetval = CompareConnItem(a, b);
3313
                        if (connItemRetval != 0)
3314
                        {
3315
                            return connItemRetval;
3316
                        }
3317
                        else
3318
                        {
3319
                            // ConnectedItem이 없는것
3320
                            int noneConnRetval = CompareNoneConn(a, b);
3321
                            if (noneConnRetval != 0)
3322
                            {
3323
                                return noneConnRetval;
3324
                            }
3325
                            else
3326
                            {
3327

    
3328
                            }
3329
                        }
3330
                    }
3331
                }
3332

    
3333
                return 0;
3334
            }
3335

    
3336
            int CompareConnSymbol(Line a, Line b)
3337
            {
3338
                List<Connector> connectorsA = a.CONNECTORS
3339
                    .Where(conn => conn.ConnectedObject != null && conn.ConnectedObject.GetType() == typeof(Symbol))
3340
                    .ToList();
3341

    
3342
                List<Connector> connectorsB = b.CONNECTORS
3343
                    .Where(conn => conn.ConnectedObject != null && conn.ConnectedObject.GetType() == typeof(Symbol))
3344
                    .ToList();
3345

    
3346
                // 오름차순
3347
                return connectorsB.Count.CompareTo(connectorsA.Count);
3348
            }
3349

    
3350
            int CompareConnItem(Line a, Line b)
3351
            {
3352
                List<Connector> connectorsA = a.CONNECTORS
3353
                    .Where(conn => conn.ConnectedObject != null && 
3354
                    (conn.ConnectedObject.GetType() == typeof(Symbol) || 
3355
                    (conn.ConnectedObject.GetType() == typeof(Line) && !SPPIDUtil.IsBranchLine((Line)conn.ConnectedObject, a))))
3356
                    .ToList();
3357

    
3358
                List<Connector> connectorsB = b.CONNECTORS
3359
                    .Where(conn => conn.ConnectedObject != null &&
3360
                    (conn.ConnectedObject.GetType() == typeof(Symbol) ||
3361
                    (conn.ConnectedObject.GetType() == typeof(Line) && !SPPIDUtil.IsBranchLine((Line)conn.ConnectedObject, b))))
3362
                    .ToList();
3363

    
3364
                // 오름차순
3365
                return connectorsB.Count.CompareTo(connectorsA.Count);
3366
            }
3367

    
3368
            int CompareBranchLine(Line a, Line b)
3369
            {
3370
                List<Connector> connectorsA = a.CONNECTORS
3371
                    .Where(conn => conn.ConnectedObject != null && conn.ConnectedObject.GetType() == typeof(Line) && SPPIDUtil.IsBranchLine(a, conn.ConnectedObject as Line))
3372
                    .ToList();
3373
                List<Connector> connectorsB = b.CONNECTORS
3374
                    .Where(conn => conn.ConnectedObject != null && conn.ConnectedObject.GetType() == typeof(Line) && SPPIDUtil.IsBranchLine(b, conn.ConnectedObject as Line))
3375
                    .ToList();
3376

    
3377
                // 내림차순
3378
                return connectorsA.Count.CompareTo(connectorsB.Count);
3379
            }
3380

    
3381
            int CompareNoneConn(Line a, Line b)
3382
            {
3383
                List<Connector> connectorsA = a.CONNECTORS
3384
                    .Where(conn => conn.ConnectedObject == null)
3385
                    .ToList();
3386

    
3387
                List<Connector> connectorsB = b.CONNECTORS
3388
                    .Where(conn => conn.ConnectedObject == null)
3389
                    .ToList();
3390

    
3391
                // 오름차순
3392
                return connectorsB.Count.CompareTo(connectorsA.Count);
3393
            }
3394
        }
3395

    
3396
        private void SortBranchLines()
3397
        {
3398
            NewBranchLines.Sort(SortBranchLine);
3399
            int SortBranchLine(Line a, Line b)
3400
            {
3401
                int countA = a.CONNECTORS.FindAll(x => x.ConnectedObject != null &&
3402
                 x.ConnectedObject.GetType() == typeof(Line) &&
3403
                 SPPIDUtil.IsBranchLine(x.ConnectedObject as Line, a) &&
3404
                 string.IsNullOrEmpty(((Line)x.ConnectedObject).SPPID.ModelItemId)).Count;
3405

    
3406
                int countB = b.CONNECTORS.FindAll(x => x.ConnectedObject != null &&
3407
                 x.ConnectedObject.GetType() == typeof(Line) &&
3408
                 SPPIDUtil.IsBranchLine(x.ConnectedObject as Line, b) &&
3409
                 string.IsNullOrEmpty(((Line)x.ConnectedObject).SPPID.ModelItemId)).Count;
3410

    
3411
                // 내림차순
3412
                return countA.CompareTo(countB);
3413
            }
3414
        }
3415

    
3416
        private static int SortSymbolPriority(Symbol a, Symbol b)
3417
        {
3418
            int countA = a.CONNECTORS.FindAll(x => !string.IsNullOrEmpty(x.CONNECTEDITEM) && x.CONNECTEDITEM != "None").Count;
3419
            int countB = b.CONNECTORS.FindAll(x => !string.IsNullOrEmpty(x.CONNECTEDITEM) && x.CONNECTEDITEM != "None").Count;
3420
            int retval = countB.CompareTo(countA);
3421
            if (retval != 0)
3422
                return retval;
3423
            else
3424
                return a.SPPID.ORIGINAL_X.CompareTo(b.SPPID.ORIGINAL_X);
3425
        }
3426

    
3427
        /// <summary>
3428
        /// Graphic OID로 해당 Symbol의 크기를 구하여 Zoom
3429
        /// </summary>
3430
        /// <param name="graphicOID"></param>
3431
        /// <param name="milliseconds"></param>
3432
        private void ZoomObjectByGraphicOID(string graphicOID, int milliseconds = 150)
3433
        {
3434
            if (radApp.ActiveDocument.ActiveSheet.DrawingObjects[graphicOID] != null)
3435
            {
3436
                double minX = 0;
3437
                double minY = 0;
3438
                double maxX = 0;
3439
                double maxY = 0;
3440
                radApp.ActiveDocument.ActiveSheet.DrawingObjects[graphicOID].Range(out minX, out minY, out maxX, out maxY);
3441
                radApp.ActiveWindow.ZoomArea2(minX - 0.007, minY - 0.007, maxX + 0.007, maxY + 0.007, null);
3442

    
3443
                Thread.Sleep(milliseconds);
3444
            }
3445
        }
3446

    
3447
        /// <summary>
3448
        /// ComObject를 Release
3449
        /// </summary>
3450
        /// <param name="objVars"></param>
3451
        public void ReleaseCOMObjects(params object[] objVars)
3452
        {
3453
            int intNewRefCount = 0;
3454
            foreach (object obj in objVars)
3455
            {
3456
                if (!Information.IsNothing(obj) && System.Runtime.InteropServices.Marshal.IsComObject(obj))
3457
                    intNewRefCount = intNewRefCount + System.Runtime.InteropServices.Marshal.FinalReleaseComObject(obj);
3458
            }
3459
        }
3460
    }
3461
}
클립보드 이미지 추가 (최대 크기: 500 MB)