프로젝트

일반

사용자정보

통계
| 개정판:

hytos / DTI_PID / SPPIDConverter / AutoModeling.cs @ 1805d3b7

이력 | 보기 | 이력해설 | 다운로드 (162 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

    
90
                    // Equipment Modeling
91
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Equipments Modeling");
92
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetAllProgress, document.Equipments.Count);
93
                    foreach (Equipment item in document.Equipments)
94
                    {
95
                        try
96
                        {
97
                            EquipmentModeling(item);
98
                            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetProgress, document.Equipments.FindAll(x => !string.IsNullOrEmpty(x.SPPID.RepresentationId)).Count);
99
                        }
100
                        catch (Exception ex)
101
                        {
102
                            Log.Write("Error in EquipmentModeling");
103
                            Log.Write("UID : " + item.UID);
104
                            Log.Write(ex.Message);
105
                            Log.Write(ex.StackTrace);
106
                        }
107
                    }
108

    
109
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Symbol Modeling");
110
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetAllProgress, document.SYMBOLS.Count);
111
                    prioritySymbols = GetPrioritySymbol();
112
                    foreach (var item in prioritySymbols)
113
                    {
114
                        try
115
                        {
116
                            SymbolModelingBySymbol(item);
117
                        }
118
                        catch (Exception ex)
119
                        {
120
                            Log.Write("Error in SymbolModelingByPriority");
121
                            Log.Write("UID : " + item.UID);
122
                            Log.Write(ex.Message);
123
                            Log.Write(ex.StackTrace);
124
                        }
125
                    }
126

    
127
                    // LineRun Line Modeling
128
                    SetPriorityLine();
129
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Lines Modeling");
130
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetAllProgress, document.LINES.Count);
131
                    foreach (var item in document.LINES)
132
                    {
133
                        try
134
                        {
135
                            NewLineModeling(item);
136
                            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
137
                        }
138
                        catch (Exception ex)
139
                        {
140
                            Log.Write("Error in NewLineModeling");
141
                            Log.Write("UID : " + item.UID);
142
                            Log.Write(ex.Message);
143
                            Log.Write(ex.StackTrace);
144
                        }
145
                    }
146

    
147
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Branch Lines Modeling");
148
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetAllProgress, NewBranchLines.Count);
149
                    int branchCount = NewBranchLines.Count;
150
                    while (NewBranchLines.Count > 0)
151
                    {
152
                        Line item = NewBranchLines[0];
153
                        try
154
                        {
155
                            SortBranchLines();
156
                            NewLineModeling(item, true);
157
                            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
158
                        }
159
                        catch (Exception ex)
160
                        {
161
                            Log.Write("Error in NewLineModeling");
162
                            Log.Write("UID : " + item.UID);
163
                            Log.Write(ex.Message);
164
                            Log.Write(ex.StackTrace);
165
                        }
166
                    }
167

    
168
                    #region Clear Nominal Diameter
169
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetAllProgress, document.SYMBOLS.Count + document.LINES.Count);
170
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Clear Attribute");
171
                    SetSystemEditingCommand(false);
172
                    List<string> endClearModelItemID = new List<string>();
173
                    for (int i = 0; i < document.LINES.Count; i++)
174
                    {
175
                        Line item = document.LINES[i];
176
                        string modelItemID = item.SPPID.ModelItemId;
177
                        if (!string.IsNullOrEmpty(modelItemID))
178
                        {
179
                            LMModelItem modelItem = dataSource.GetModelItem(modelItemID);
180
                            if (modelItem != null)
181
                            {
182
                                LMAAttribute attribute = modelItem.Attributes["NominalDiameter"];
183
                                if (attribute != null)
184
                                    attribute.set_Value(DBNull.Value);
185

    
186
                                modelItem.Commit();
187
                                ReleaseCOMObjects(modelItem);
188
                            }
189
                        }
190
                        if (!endClearModelItemID.Contains(modelItemID))
191
                            endClearModelItemID.Add(modelItemID);
192
                        SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
193
                    }
194

    
195
                    for (int i = 0; i < document.SYMBOLS.Count; i++)
196
                    {
197
                        Symbol item = document.SYMBOLS[i];
198
                        string repID = item.SPPID.RepresentationId;
199
                        string modelItemID = item.SPPID.ModelItemID;
200
                        if (!string.IsNullOrEmpty(modelItemID))
201
                        {
202
                            LMModelItem modelItem = dataSource.GetModelItem(modelItemID);
203
                            if (modelItem != null)
204
                            {
205
                                LMAAttribute attribute = modelItem.Attributes["NominalDiameter"];
206
                                if (attribute != null)
207
                                    attribute.set_Value(DBNull.Value);
208
                                attribute = modelItem.Attributes["PipingPoint1.NominalDiameter"];
209
                                if (attribute != null)
210
                                    attribute.set_Value(DBNull.Value);
211
                                attribute = modelItem.Attributes["PipingPoint2.NominalDiameter"];
212
                                if (attribute != null)
213
                                    attribute.set_Value(DBNull.Value);
214
                                modelItem.Commit();
215

    
216
                                modelItem.Commit();
217
                                ReleaseCOMObjects(modelItem);
218
                            }
219
                        }
220
                        if (!string.IsNullOrEmpty(repID))
221
                        {
222
                            LMSymbol symbol = dataSource.GetSymbol(repID);
223
                            if (symbol != null)
224
                            {
225
                                foreach (LMConnector connector in symbol.Connect1Connectors)
226
                                {
227
                                    if (connector.get_ItemStatus() == "Active" && !endClearModelItemID.Contains(connector.ModelItemID))
228
                                    {
229
                                        endClearModelItemID.Add(connector.ModelItemID);
230
                                        LMModelItem modelItem = connector.ModelItemObject;
231
                                        if (modelItem != null)
232
                                        {
233
                                            LMAAttribute attribute = modelItem.Attributes["NominalDiameter"];
234
                                            if (attribute != null)
235
                                                attribute.set_Value(DBNull.Value);
236

    
237
                                            modelItem.Commit();
238
                                            ReleaseCOMObjects(modelItem);
239
                                        }
240
                                    }
241
                                }
242
                                foreach (LMConnector connector in symbol.Connect2Connectors)
243
                                {
244
                                    if (connector.get_ItemStatus() == "Active" && !endClearModelItemID.Contains(connector.ModelItemID))
245
                                    {
246
                                        endClearModelItemID.Add(connector.ModelItemID);
247
                                        LMModelItem modelItem = connector.ModelItemObject;
248
                                        if (modelItem != null)
249
                                        {
250
                                            LMAAttribute attribute = modelItem.Attributes["NominalDiameter"];
251
                                            if (attribute != null)
252
                                                attribute.set_Value(DBNull.Value);
253

    
254
                                            modelItem.Commit();
255
                                            ReleaseCOMObjects(modelItem);
256
                                        }
257
                                    }
258
                                }
259
                                ReleaseCOMObjects(symbol);
260
                            }
261
                        }
262
                        SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
263
                    }
264
                    SetSystemEditingCommand(true);
265
                    #endregion
266

    
267
                    // EndBreak Modeling
268
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetAllProgress, document.EndBreaks.Count);
269
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "EndBreaks Modeling");
270
                    foreach (var item in document.EndBreaks)
271
                        try
272
                        {
273
                            EndBreakModeling(item);
274
                            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
275
                            if (string.IsNullOrEmpty(item.SPPID.ModelItemID))
276
                                Log.Write("Fail modeling endbreak UID : " + item.UID);
277
                        }
278
                        catch (Exception ex)
279
                        {
280
                            Log.Write("Error in EndBreakModeling");
281
                            Log.Write("UID : " + item.UID);
282
                            Log.Write(ex.Message);
283
                            Log.Write(ex.StackTrace);
284
                        }
285

    
286

    
287
                    // SpecBreak Modeling
288
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetAllProgress, document.SpecBreaks.Count);
289
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "SpecBreaks Modeling");
290
                    foreach (var item in document.SpecBreaks)
291
                        try
292
                        {
293
                            SpecBreakModeling(item);
294
                            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
295
                            if (string.IsNullOrEmpty(item.SPPID.ModelItemID))
296
                                Log.Write("Fail modeling specbreak UID : " + item.UID);
297
                        }
298
                        catch (Exception ex)
299
                        {
300
                            Log.Write("Error in SpecBreakModeling");
301
                            Log.Write("UID : " + item.UID);
302
                            Log.Write(ex.Message);
303
                            Log.Write(ex.StackTrace);
304
                        }
305

    
306

    
307
                    //// LineNumber Modeling
308
                    //SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "LineNumbers Modeling");
309
                    //foreach (var item in document.LINENUMBERS)
310
                    //    try
311
                    //    {
312
                    //        LineNumberModeling(item);
313
                    //    }
314
                    //    catch (Exception ex)
315
                    //    {
316
                    //        Log.Write("Error in LineNumberModeling");
317
                    //        Log.Write("UID : " + item.UID);
318
                    //        Log.Write(ex.Message);
319
                    //        Log.Write(ex.StackTrace);
320
                    //    }
321

    
322

    
323
                    //// FlowMark Modeling
324
                    //SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Flow Mark Modeling");
325
                    //foreach (var item in document.LINES)
326
                    //    try
327
                    //    {
328
                    //        FlowMarkModeling(item);
329
                    //    }
330
                    //    catch (Exception ex)
331
                    //    {
332
                    //        Log.Write("Error in FlowMarkModeling");
333
                    //        Log.Write("UID : " + item.UID);
334
                    //        Log.Write(ex.Message);
335
                    //        Log.Write(ex.StackTrace);
336
                    //    }
337

    
338

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

    
354

    
355
                    //// Note Modeling
356
                    //SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Notes Modeling");
357
                    //foreach (var item in document.NOTES)
358
                    //    try
359
                    //    {
360
                    //        NoteModeling(item);
361
                    //    }
362
                    //    catch (Exception ex)
363
                    //    {
364
                    //        Log.Write("Error in NoteModeling");
365
                    //        Log.Write("UID : " + item.UID);
366
                    //        Log.Write(ex.Message);
367
                    //        Log.Write(ex.StackTrace);
368
                    //    }
369

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

    
385
                    //// Input LineNumber Attribute
386
                    //SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Set LineNumbers Attribute");
387
                    //foreach (var item in document.LINENUMBERS)
388
                    //    try
389
                    //    {
390
                    //        InputLineNumberAttribute(item);
391
                    //    }
392
                    //    catch (Exception ex)
393
                    //    {
394
                    //        Log.Write("Error in InputLineNumberAttribute");
395
                    //        Log.Write("UID : " + item.UID);
396
                    //        Log.Write(ex.Message);
397
                    //        Log.Write(ex.StackTrace);
398
                    //    }
399

    
400
                    //// Input Symbol Attribute
401
                    //SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Set Symbols Attribute");
402
                    //foreach (var item in document.SYMBOLS)
403
                    //    try
404
                    //    {
405
                    //        InputSymbolAttribute(item, item.ATTRIBUTES);
406
                    //    }
407
                    //    catch (Exception ex)
408
                    //    {
409
                    //        Log.Write("Error in InputSymbolAttribute");
410
                    //        Log.Write("UID : " + item.UID);
411
                    //        Log.Write(ex.Message);
412
                    //        Log.Write(ex.StackTrace);
413
                    //    }
414

    
415
                    //// Input SpecBreak Attribute
416
                    //SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Set Symbols Attribute");
417
                    //foreach (var item in document.SpecBreaks)
418
                    //    try
419
                    //    {
420
                    //        InputSpecBreakAttribute(item);
421
                    //    }
422
                    //    catch (Exception ex)
423
                    //    {
424
                    //        Log.Write("Error in InputSpecBreakAttribute");
425
                    //        Log.Write("UID : " + item.UID);
426
                    //        Log.Write(ex.Message);
427
                    //        Log.Write(ex.StackTrace);
428
                    //    }
429

    
430
                    //// Label Symbol Modeling
431
                    //SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Labels Modeling");
432
                    //foreach (var item in document.SYMBOLS)
433
                    //    try
434
                    //    {
435
                    //        LabelSymbolModeling(item);
436
                    //    }
437
                    //    catch (Exception ex)
438
                    //    {
439
                    //        Log.Write("Error in LabelSymbolModeling");
440
                    //        Log.Write("UID : " + item.UID);
441
                    //        Log.Write(ex.Message);
442
                    //        Log.Write(ex.StackTrace);
443
                    //    }
444

    
445
                    //// LineRun Line Join
446
                    //SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Join LineRuns");
447
                    //foreach (LineNumber lineNumber in document.LINENUMBERS)
448
                    //    try
449
                    //    {
450
                    //        foreach (LineRun run in lineNumber.RUNS)
451
                    //            JoinRunLine(run);
452
                    //    }
453
                    //    catch (Exception ex)
454
                    //    {
455
                    //        Log.Write("Error in JoinRunLine");
456
                    //        Log.Write("UID : " + lineNumber.UID);
457
                    //        Log.Write(ex.Message);
458
                    //        Log.Write(ex.StackTrace);
459
                    //    }
460

    
461
                    //// TrimLineRun Line Join
462
                    //foreach (TrimLine trimLine in document.TRIMLINES)
463
                    //    try
464
                    //    {
465
                    //        foreach (LineRun run in trimLine.RUNS)
466
                    //            JoinRunLine(run);
467
                    //    }
468
                    //    catch (Exception ex)
469
                    //    {
470
                    //        Log.Write("Error in JoinRunLine");
471
                    //        Log.Write("UID : " + trimLine.UID);
472
                    //        Log.Write(ex.Message);
473
                    //        Log.Write(ex.StackTrace);
474
                    //    }
475
                }
476
            }
477
            catch (Exception ex)
478
            {
479
                SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.ClearParent, null);
480
                SplashScreenManager.CloseForm(false);
481
                System.Windows.Forms.MessageBox.Show(ex.Message + "\r\n" + ex.StackTrace);
482
            }
483
            finally
484
            {
485
                Log.Write("End Modeling");
486
                application.ActiveWindow.Fit();
487

    
488
                if (radApp.ActiveDocument != null)
489
                {
490
                    radApp.ActiveDocument.Save();
491
                    //radApp.ActiveDocument.SaveOnClose = false;
492
                    //radApp.ActiveDocument.Close(false);
493

    
494
                    ReleaseCOMObjects(newDrawing);
495
                }
496

    
497
                ReleaseCOMObjects(dataSource);
498
                ReleaseCOMObjects(_placement);
499

    
500
                Project_DB.InsertDrawingInfo(document.PATH, drawingNumber, drawingName, document);
501
                if (SplashScreenManager.Default.IsSplashFormVisible)
502
                {
503
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.ClearParent, null);
504
                    SplashScreenManager.CloseForm(false);
505
                    Log.Write("\r\n");
506
                }
507
            }
508
        }
509

    
510
        /// <summary>
511
        /// 도면 생성 메서드
512
        /// </summary>
513
        private void CreateDocument(ref string drawingNumber, ref string drawingName)
514
        {
515
            Log.Write("------------------ Start create document ------------------");
516
            GetDrawingNameAndNumber(ref drawingName, ref drawingNumber);
517
            Log.Write("Drawing name : " + drawingName);
518
            Log.Write("Drawing number : " + drawingNumber);
519
            newDrawing = application.Drawings.Add(document.Unit, document.Template, drawingNumber, drawingName);
520
            document.SPPID_DrawingNumber = drawingNumber;
521
            document.SPPID_DrawingName = drawingName;
522
            application.ActiveWindow.Fit();
523
            Thread.Sleep(1000);
524
            application.ActiveWindow.Zoom = 2000;
525
            Thread.Sleep(2000);
526
        }
527

    
528
        /// <summary>
529
        /// DrawingName, DrawingNumber를 확인하여 중복이 있으면 _1을 붙이고 +1씩 한다.
530
        /// </summary>
531
        /// <param name="drawingName"></param>
532
        /// <param name="drawingNumber"></param>
533
        private void GetDrawingNameAndNumber(ref string drawingName, ref string drawingNumber)
534
        {
535
            LMDrawings drawings = new LMDrawings();
536
            drawings.Collect(dataSource);
537

    
538
            List<string> drawingNameList = new List<string>();
539
            List<string> drawingNumberList = new List<string>();
540

    
541
            foreach (LMDrawing item in drawings)
542
            {
543
                drawingNameList.Add(item.Attributes["Name"].get_Value().ToString());
544
                drawingNumberList.Add(item.Attributes["DrawingNumber"].get_Value().ToString());
545
            }
546

    
547
            int nameLength = drawingName.Length;
548
            while (drawingNameList.Contains(drawingName))
549
            {
550
                if (nameLength == drawingName.Length)
551
                    drawingName += "-1";
552
                else
553
                {
554
                    int index = Convert.ToInt32(drawingName.Remove(0, nameLength + 1));
555
                    drawingName = drawingName.Substring(0, nameLength + 1);
556
                    drawingName += ++index;
557
                }
558
            }
559

    
560
            int numberLength = drawingNumber.Length;
561
            while (drawingNameList.Contains(drawingNumber))
562
            {
563
                if (numberLength == drawingNumber.Length)
564
                    drawingNumber += "-1";
565
                else
566
                {
567
                    int index = Convert.ToInt32(drawingNumber.Remove(0, numberLength + 1));
568
                    drawingNumber = drawingNumber.Substring(0, numberLength + 1);
569
                    drawingNumber += ++index;
570
                }
571
            }
572

    
573
            ReleaseCOMObjects(drawings);
574
        }
575

    
576
        /// <summary>
577
        /// 도면 크기 구하는 메서드
578
        /// </summary>
579
        /// <returns></returns>
580
        private bool DocumentCoordinateCorrection()
581
        {
582
            if (Settings.Default.DrawingX != 0 && Settings.Default.DrawingY != 0)
583
            {
584
                Log.Write("Setting Drawing X, Drawing Y");
585
                document.SetSPPIDLocation(Settings.Default.DrawingX, Settings.Default.DrawingY);
586
                Log.Write("Start coordinate correction");
587
                document.CoordinateCorrection();
588
                return true;
589
            }
590
            else
591
            {
592
                Log.Write("Need Drawing X, Y");
593
                return false;
594
            }
595
        }
596

    
597
        /// <summary>
598
        /// 심볼을 실제로 Modeling 메서드
599
        /// </summary>
600
        /// <param name="symbol"></param>
601
        /// <param name="targetSymbol"></param>
602
        /// <param name="prevSymbol"></param>
603
        private void SymbolModeling(Symbol symbol, Symbol targetSymbol)
604
        {
605
            // OWNERSYMBOL Attribute, 값을 가지고 있을 경우
606
            BaseModel.Attribute itemAttribute = symbol.ATTRIBUTES.Find(attr => attr.ATTRIBUTE == "OWNERSYMBOL");
607
            if (itemAttribute != null && (string.IsNullOrEmpty(itemAttribute.VALUE) || itemAttribute.VALUE != "None"))
608
                return;
609
            // 이미 모델링 됐을 경우
610
            else if (!string.IsNullOrEmpty(symbol.SPPID.RepresentationId))
611
                return;
612

    
613
            LMSymbol _LMSymbol = null;
614

    
615
            string mappingPath = symbol.SPPID.MAPPINGNAME;
616
            double x = symbol.SPPID.ORIGINAL_X;
617
            double y = symbol.SPPID.ORIGINAL_Y;
618
            int mirror = 0;
619
            double angle = symbol.ANGLE;
620

    
621
            // OPC 일경우 180도 일때 Mirror
622
            if (mappingPath.Contains("Piping OPC's") && angle == Math.PI)
623
                mirror = 1;
624

    
625
            // Mirror 계산
626
            if (symbol.FLIP == 1)
627
            {
628
                mirror = 1;
629
                angle += Math.PI;
630
            }
631

    
632
            if (targetSymbol != null && !string.IsNullOrEmpty(targetSymbol.SPPID.RepresentationId))
633
            {
634
                LMSymbol _TargetItem = dataSource.GetSymbol(targetSymbol.SPPID.RepresentationId);
635
                Connector connector = SPPIDUtil.FindSymbolConnectorByUID(document, symbol.UID, targetSymbol);
636
                if (connector != null)
637
                    GetTargetSymbolConnectorPoint(connector, targetSymbol, ref x, ref y);
638

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

    
641
                if (_LMSymbol != null && _TargetItem != null)
642
                {
643
                    symbol.SPPID.RepresentationId = _LMSymbol.AsLMRepresentation().Id;
644
                    LMConnector reModelingConnector = FindBreakLineTarget(symbol, targetSymbol);
645

    
646
                    if (reModelingConnector != null)
647
                        ReModelingLMConnector(reModelingConnector);
648
                }
649

    
650
                ReleaseCOMObjects(_TargetItem);
651
            }
652
            else
653
                _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle);
654

    
655
            if (_LMSymbol != null)
656
            {
657
                _LMSymbol.Commit();
658
                symbol.SPPID.RepresentationId = _LMSymbol.AsLMRepresentation().Id;
659
                symbol.SPPID.ModelItemID = _LMSymbol.ModelItemID;
660
                symbol.SPPID.GraphicOID = _LMSymbol.get_GraphicOID();
661

    
662
                foreach (var item in symbol.ChildSymbols)
663
                    CreateChildSymbol(item, _LMSymbol);
664

    
665
                symbol.SPPID.SPPID_X = _LMSymbol.get_XCoordinate();
666
                symbol.SPPID.SPPID_Y = _LMSymbol.get_YCoordinate();
667

    
668
                double[] range = null;
669
                GetSPPIDSymbolRange(symbol, ref range);
670
                symbol.SPPID.SPPID_Min_X = range[0];
671
                symbol.SPPID.SPPID_Min_Y = range[1];
672
                symbol.SPPID.SPPID_Max_X = range[2];
673
                symbol.SPPID.SPPID_Max_Y = range[3];
674

    
675
                foreach (var item in symbol.SPPID.CorrectionX_GroupSymbols)
676
                    item.SPPID.ORIGINAL_X = symbol.SPPID.SPPID_X;
677
                foreach (var item in symbol.SPPID.CorrectionY_GroupSymbols)
678
                    item.SPPID.ORIGINAL_Y = symbol.SPPID.SPPID_Y;
679

    
680
                ReleaseCOMObjects(_LMSymbol);
681
            }
682
        }
683

    
684
        private void RemoveSymbol(Symbol symbol)
685
        {
686
            if (!string.IsNullOrEmpty(symbol.SPPID.RepresentationId))
687
            {
688
                LMSymbol _LMSymbol = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
689
                if (_LMSymbol != null)
690
                {
691
                    _placement.PIDRemovePlacement(_LMSymbol.AsLMRepresentation());
692
                    ReleaseCOMObjects(_LMSymbol);
693
                }
694
            }
695

    
696
            symbol.SPPID.RepresentationId = string.Empty;
697
            symbol.SPPID.ModelItemID = string.Empty;
698
            symbol.SPPID.SPPID_X = double.NaN;
699
            symbol.SPPID.SPPID_Y = double.NaN;
700
            symbol.SPPID.SPPID_Min_X = double.NaN;
701
            symbol.SPPID.SPPID_Min_Y = double.NaN;
702
            symbol.SPPID.SPPID_Max_X = double.NaN;
703
            symbol.SPPID.SPPID_Max_Y = double.NaN;
704
        }
705

    
706
        private void RemoveSymbol(List<Symbol> symbols)
707
        {
708
            foreach (var symbol in symbols)
709
            {
710
                if (!string.IsNullOrEmpty(symbol.SPPID.RepresentationId))
711
                {
712
                    LMSymbol _LMSymbol = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
713
                    if (_LMSymbol != null)
714
                    {
715
                        _placement.PIDRemovePlacement(_LMSymbol.AsLMRepresentation());
716
                        ReleaseCOMObjects(_LMSymbol);
717
                    }
718
                }
719

    
720
                symbol.SPPID.RepresentationId = string.Empty;
721
                symbol.SPPID.ModelItemID = string.Empty;
722
                symbol.SPPID.SPPID_X = double.NaN;
723
                symbol.SPPID.SPPID_Y = double.NaN;
724
                symbol.SPPID.SPPID_Min_X = double.NaN;
725
                symbol.SPPID.SPPID_Min_Y = double.NaN;
726
                symbol.SPPID.SPPID_Max_X = double.NaN;
727
                symbol.SPPID.SPPID_Max_Y = double.NaN;
728
            }
729
        }
730

    
731
        /// <summary>
732
        /// ID2의 Symbol Width와 Height를 비교해서 상대적인 SPPID Connector좌표를 가져온다.
733
        /// </summary>
734
        /// <param name="targetConnector"></param>
735
        /// <param name="targetSymbol"></param>
736
        /// <param name="x"></param>
737
        /// <param name="y"></param>
738
        private void GetTargetSymbolConnectorPoint(Connector targetConnector, Symbol targetSymbol, ref double x, ref double y)
739
        {
740
            LMSymbol _TargetItem = dataSource.GetSymbol(targetSymbol.SPPID.RepresentationId);
741

    
742
            double[] range = null;
743
            List<double[]> points = new List<double[]>();
744
            GetSPPIDSymbolRangeAndConnectionPoints(targetSymbol, ref range, points);
745
            double x1 = range[0];
746
            double y1 = range[1];
747
            double x2 = range[2];
748
            double y2 = range[3];
749

    
750
            // Origin 기준 Connector의 위치차이
751
            double sceneX = 0;
752
            double sceneY = 0;
753
            SPPIDUtil.ConvertPointBystring(targetConnector.SCENECONNECTPOINT, ref sceneX, ref sceneY);
754
            double originX = 0;
755
            double originY = 0;
756
            SPPIDUtil.ConvertPointBystring(targetSymbol.ORIGINALPOINT, ref originX, ref originY);
757
            double gapX = originX - sceneX;
758
            double gapY = originY - sceneY;
759

    
760
            // SPPID Symbol과 ID2 심볼의 크기 차이
761
            double sizeWidth = 0;
762
            double sizeHeight = 0;
763
            SPPIDUtil.ConvertPointBystring(targetSymbol.SIZE, ref sizeWidth, ref sizeHeight);
764
            if (sizeWidth == 0 || sizeHeight == 0)
765
                throw new Exception("Check symbol size! \r\nUID : " + targetSymbol.UID);
766

    
767
            double percentX = (x2 - x1) / sizeWidth;
768
            double percentY = (y2 - y1) / sizeHeight;
769

    
770
            double SPPIDgapX = gapX * percentX;
771
            double SPPIDgapY = gapY * percentY;
772

    
773
            double[] SPPIDOriginPoint = new double[] { _TargetItem.get_XCoordinate() - SPPIDgapX, _TargetItem.get_YCoordinate() + SPPIDgapY };
774
            double distance = double.MaxValue;
775
            double[] resultPoint;
776
            foreach (var point in points)
777
            {
778
                double result = SPPIDUtil.CalcPointToPointdDistance(point[0], point[1], SPPIDOriginPoint[0], SPPIDOriginPoint[1]);
779
                if (distance > result)
780
                {
781
                    distance = result;
782
                    resultPoint = point;
783
                    x = point[0];
784
                    y = point[1];
785
                }
786
            }
787

    
788
            ReleaseCOMObjects(_TargetItem);
789
        }
790

    
791
        private void GetTargetLineConnectorPoint(Connector targetConnector, Line targetLine, ref double x, ref double y)
792
        {
793
            int index = targetLine.CONNECTORS.IndexOf(targetConnector);
794
            if (index == 0)
795
            {
796
                x = targetLine.SPPID.START_X;
797
                y = targetLine.SPPID.START_Y;
798
            }
799
            else
800
            {
801
                x = targetLine.SPPID.END_X;
802
                y = targetLine.SPPID.END_Y;
803
            }
804
        }
805

    
806
        /// <summary>
807
        /// SPPID Symbol의 Range를 구한다.
808
        /// </summary>
809
        /// <param name="symbol"></param>
810
        /// <param name="range"></param>
811
        private void GetSPPIDSymbolRangeAndConnectionPoints(Symbol symbol, ref double[] range, List<double[]> points)
812
        {
813
            LMSymbol _TargetItem = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
814
            Ingr.RAD2D.Symbol2d symbol2d = radApp.ActiveDocument.ActiveSheet.DrawingObjects[_TargetItem.get_GraphicOID().ToString()];
815
            double x1 = 0;
816
            double y1 = 0;
817
            double x2 = 0;
818
            double y2 = 0;
819
            symbol2d.Range(out x1, out y1, out x2, out y2);
820
            range = new double[] { x1, y1, x2, y2 };
821

    
822
            for (int i = 1; i < int.MaxValue; i++)
823
            {
824
                double connX = 0;
825
                double connY = 0;
826
                if (_placement.PIDConnectPointLocation(_TargetItem, i, ref connX, ref connY))
827
                    points.Add(new double[] { connX, connY });
828
                else
829
                    break;
830
            }
831

    
832
            foreach (var childSymbol in symbol.ChildSymbols)
833
                GetSPPIDChildSymbolRange(childSymbol, ref range, points);
834

    
835
            ReleaseCOMObjects(_TargetItem);
836
        }
837

    
838
        private void GetSPPIDSymbolRange(Symbol symbol, ref double[] range)
839
        {
840
            LMSymbol _TargetItem = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
841
            Ingr.RAD2D.Symbol2d symbol2d = radApp.ActiveDocument.ActiveSheet.DrawingObjects[_TargetItem.get_GraphicOID().ToString()];
842
            double x1 = 0;
843
            double y1 = 0;
844
            double x2 = 0;
845
            double y2 = 0;
846
            symbol2d.Range(out x1, out y1, out x2, out y2);
847
            range = new double[] { x1, y1, x2, y2 };
848

    
849
            foreach (var childSymbol in symbol.ChildSymbols)
850
                GetSPPIDChildSymbolRange(childSymbol, ref range);
851

    
852
            ReleaseCOMObjects(_TargetItem);
853
        }
854

    
855
        private void GetSPPIDSymbolRange(List<Symbol> symbols, ref double[] range)
856
        {
857
            double[] tempRange = new double[] { double.MaxValue, double.MaxValue, double.MinValue, double.MinValue };
858
            foreach (var symbol in symbols)
859
            {
860
                LMSymbol _TargetItem = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
861
                Ingr.RAD2D.Symbol2d symbol2d = radApp.ActiveDocument.ActiveSheet.DrawingObjects[_TargetItem.get_GraphicOID().ToString()];
862
                double x1 = 0;
863
                double y1 = 0;
864
                double x2 = 0;
865
                double y2 = 0;
866
                symbol2d.Range(out x1, out y1, out x2, out y2);
867

    
868
                tempRange[0] = Math.Min(tempRange[0], x1);
869
                tempRange[1] = Math.Min(tempRange[1], y1);
870
                tempRange[2] = Math.Max(tempRange[2], x2);
871
                tempRange[3] = Math.Max(tempRange[3], y2);
872

    
873
                foreach (var childSymbol in symbol.ChildSymbols)
874
                    GetSPPIDChildSymbolRange(childSymbol, ref tempRange);
875

    
876
                ReleaseCOMObjects(_TargetItem);
877
            }
878

    
879
            range = tempRange;
880
        }
881

    
882
        /// <summary>
883
        /// Child Modeling 된 Symbol의 Range를 구한다.
884
        /// </summary>
885
        /// <param name="childSymbol"></param>
886
        /// <param name="range"></param>
887
        private void GetSPPIDChildSymbolRange(ChildSymbol childSymbol, ref double[] range, List<double[]> points)
888
        {
889
            LMSymbol _ChildSymbol = dataSource.GetSymbol(childSymbol.SPPID.RepresentationId);
890
            Ingr.RAD2D.Symbol2d symbol2d = radApp.ActiveDocument.ActiveSheet.DrawingObjects[_ChildSymbol.get_GraphicOID().ToString()];
891
            double x1 = 0;
892
            double y1 = 0;
893
            double x2 = 0;
894
            double y2 = 0;
895
            symbol2d.Range(out x1, out y1, out x2, out y2);
896
            range[0] = Math.Min(range[0], x1);
897
            range[1] = Math.Min(range[1], y1);
898
            range[2] = Math.Max(range[2], x2);
899
            range[3] = Math.Max(range[3], y2);
900

    
901
            for (int i = 1; i < int.MaxValue; i++)
902
            {
903
                double connX = 0;
904
                double connY = 0;
905
                if (_placement.PIDConnectPointLocation(_ChildSymbol, i, ref connX, ref connY))
906
                    points.Add(new double[] { connX, connY });
907
                else
908
                    break;
909
            }
910

    
911
            foreach (var loopChildSymbol in childSymbol.ChildSymbols)
912
                GetSPPIDChildSymbolRange(loopChildSymbol, ref range, points);
913

    
914
            ReleaseCOMObjects(_ChildSymbol);
915
        }
916

    
917
        private void GetSPPIDChildSymbolRange(ChildSymbol childSymbol, ref double[] range)
918
        {
919
            LMSymbol _ChildSymbol = dataSource.GetSymbol(childSymbol.SPPID.RepresentationId);
920
            Ingr.RAD2D.Symbol2d symbol2d = radApp.ActiveDocument.ActiveSheet.DrawingObjects[_ChildSymbol.get_GraphicOID().ToString()];
921
            double x1 = 0;
922
            double y1 = 0;
923
            double x2 = 0;
924
            double y2 = 0;
925
            symbol2d.Range(out x1, out y1, out x2, out y2);
926
            range[0] = Math.Min(range[0], x1);
927
            range[1] = Math.Min(range[1], y1);
928
            range[2] = Math.Max(range[2], x2);
929
            range[3] = Math.Max(range[3], y2);
930

    
931
            foreach (var loopChildSymbol in childSymbol.ChildSymbols)
932
                GetSPPIDChildSymbolRange(loopChildSymbol, ref range);
933

    
934
            ReleaseCOMObjects(_ChildSymbol);
935
        }
936

    
937
        /// <summary>
938
        /// Label Symbol Modeling
939
        /// </summary>
940
        /// <param name="symbol"></param>
941
        private void LabelSymbolModeling(Symbol symbol)
942
        {
943
            if (string.IsNullOrEmpty(symbol.SPPID.RepresentationId))
944
            {
945
                BaseModel.Attribute itemAttribute = symbol.ATTRIBUTES.Find(x => x.ATTRIBUTE == "OWNERSYMBOL");
946
                if (itemAttribute == null || string.IsNullOrEmpty(itemAttribute.VALUE) || itemAttribute.VALUE == "None")
947
                    return;
948
                Array points = new double[] { 0, symbol.SPPID.ORIGINAL_X, symbol.SPPID.ORIGINAL_Y };
949

    
950
                string symbolUID = itemAttribute.VALUE;
951
                object targetItem = SPPIDUtil.FindObjectByUID(document, symbolUID);
952
                if (targetItem != null &&
953
                    (targetItem.GetType() == typeof(Symbol) ||
954
                    targetItem.GetType() == typeof(Equipment)))
955
                {
956
                    // Object 아이템이 Symbol일 경우 Equipment일 경우 
957
                    string sRep = null;
958
                    if (targetItem.GetType() == typeof(Symbol))
959
                        sRep = ((Symbol)targetItem).SPPID.RepresentationId;
960
                    else if (targetItem.GetType() == typeof(Equipment))
961
                        sRep = ((Equipment)targetItem).SPPID.RepresentationId;
962
                    if (!string.IsNullOrEmpty(sRep))
963
                    {
964
                        // LEADER Line 검사
965
                        bool leaderLine = false;
966
                        SymbolMapping symbolMapping = document.SymbolMappings.Find(x => x.UID == symbol.DBUID);
967
                        if (symbolMapping != null)
968
                            leaderLine = symbolMapping.LEADERLINE;
969

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

    
974
                        //Leader 선 센터로
975
                        if (_LMLabelPresist != null)
976
                        {
977
                            // Target Item에 Label의 Attribute Input
978
                            InputSymbolAttribute(targetItem, symbol.ATTRIBUTES);
979

    
980
                            string OID = _LMLabelPresist.get_GraphicOID();
981
                            DependencyObject dependency = radApp.ActiveDocument.ActiveSheet.DrawingObjects[OID] as DependencyObject;
982
                            if (dependency != null)
983
                            {
984
                                bool result = false;
985
                                foreach (var attributes in dependency.AttributeSets)
986
                                {
987
                                    foreach (var attribute in attributes)
988
                                    {
989
                                        string name = attribute.Name;
990
                                        string value = attribute.GetValue().ToString();
991
                                        if (name == "DrawingItemType" && value == "LabelPersist")
992
                                        {
993
                                            foreach (DrawingObjectBase drawingObject in dependency.DrawingObjects)
994
                                            {
995
                                                if (drawingObject.Type == Ingr.RAD2D.ObjectType.igLineString2d)
996
                                                {
997
                                                    Ingr.RAD2D.LineString2d lineString2D = drawingObject as Ingr.RAD2D.LineString2d;
998
                                                    double prevX = _TargetItem.get_XCoordinate();
999
                                                    double prevY = _TargetItem.get_YCoordinate();
1000
                                                    lineString2D.InsertVertex(lineString2D.VertexCount, prevX, prevY);
1001
                                                    lineString2D.RemoveVertex(lineString2D.VertexCount);
1002
                                                    result = true;
1003
                                                    break;
1004
                                                }
1005
                                            }
1006
                                        }
1007

    
1008
                                        if (result)
1009
                                            break;
1010
                                    }
1011

    
1012
                                    if (result)
1013
                                        break;
1014
                                }
1015
                            }
1016

    
1017
                            _LMLabelPresist.Commit();
1018
                            ReleaseCOMObjects(_LMLabelPresist);
1019
                        }
1020

    
1021
                        ReleaseCOMObjects(_TargetItem);
1022
                    }
1023
                }
1024
                else if (targetItem != null && targetItem.GetType() == typeof(Line))
1025
                {
1026
                    Line targetLine = targetItem as Line;
1027
                    Dictionary<LMConnector, List<double[]>> connectorVertices = GetPipeRunVertices(targetLine.SPPID.ModelItemId);
1028
                    LMConnector connectedLMConnector = FindTargetLMConnectorForLabel(connectorVertices, symbol.SPPID.ORIGINAL_X, symbol.SPPID.ORIGINAL_Y);
1029
                    if (connectedLMConnector != null)
1030
                    {
1031
                        // LEADER Line 검사
1032
                        bool leaderLine = false;
1033
                        SymbolMapping symbolMapping = document.SymbolMappings.Find(x => x.UID == symbol.DBUID);
1034
                        if (symbolMapping != null)
1035
                            leaderLine = symbolMapping.LEADERLINE;
1036

    
1037
                        LMLabelPersist _LMLabelPresist = _placement.PIDPlaceLabel(symbol.SPPID.MAPPINGNAME, ref points, Rotation: 0, LabeledItem: connectedLMConnector.AsLMRepresentation(), IsLeaderVisible: leaderLine);
1038
                        if (_LMLabelPresist != null)
1039
                        {
1040
                            _LMLabelPresist.Commit();
1041
                            ReleaseCOMObjects(_LMLabelPresist);
1042
                        }
1043
                        ReleaseCOMObjects(connectedLMConnector);
1044
                    }
1045

    
1046
                    foreach (var item in connectorVertices)
1047
                        if (item.Key != null)
1048
                            ReleaseCOMObjects(item.Key);
1049
                }
1050
            }
1051
        }
1052

    
1053
        /// <summary>
1054
        /// Equipment를 실제로 Modeling 메서드
1055
        /// </summary>
1056
        /// <param name="equipment"></param>
1057
        private void EquipmentModeling(Equipment equipment)
1058
        {
1059
            if (!string.IsNullOrEmpty(equipment.SPPID.RepresentationId))
1060
                return;
1061

    
1062
            LMSymbol _LMSymbol = null;
1063
            LMSymbol targetItem = null;
1064
            string mappingPath = equipment.SPPID.MAPPINGNAME;
1065
            double x = equipment.SPPID.ORIGINAL_X;
1066
            double y = equipment.SPPID.ORIGINAL_Y;
1067
            int mirror = 0;
1068
            double angle = equipment.ANGLE;
1069

    
1070
            SPPIDUtil.ConvertGridPoint(ref x, ref y);
1071

    
1072
            Connector connector = equipment.CONNECTORS.Find(conn => !string.IsNullOrEmpty(conn.CONNECTEDITEM) && conn.CONNECTEDITEM != "None");
1073
            if (connector != null)
1074
            {
1075
                Equipment connEquipment = SPPIDUtil.FindObjectByUID(document, connector.CONNECTEDITEM) as Equipment;
1076
                if (connEquipment != null)
1077
                {
1078
                    if (string.IsNullOrEmpty(connEquipment.SPPID.RepresentationId))
1079
                        EquipmentModeling(connEquipment);
1080

    
1081
                    if (!string.IsNullOrEmpty(connEquipment.SPPID.RepresentationId))
1082
                    {
1083
                        targetItem = dataSource.GetSymbol(connEquipment.SPPID.RepresentationId);
1084
                        if (targetItem != null)
1085
                        {
1086
                            _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle, TargetItem: targetItem);
1087
                        }
1088
                        else
1089
                        {
1090
                            _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle);
1091
                        }
1092
                    }
1093
                    else
1094
                    {
1095
                        _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle);
1096
                    }
1097
                }
1098
                else
1099
                {
1100
                    _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle);
1101
                }
1102
            }
1103
            else
1104
            {
1105
                _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle);
1106
            }
1107

    
1108
            if (_LMSymbol != null)
1109
            {
1110
                _LMSymbol.Commit();
1111
                equipment.SPPID.RepresentationId = _LMSymbol.AsLMRepresentation().Id;
1112
                equipment.SPPID.GraphicOID = _LMSymbol.get_GraphicOID();
1113
                ReleaseCOMObjects(_LMSymbol);
1114
            }
1115

    
1116
            if (targetItem != null)
1117
            {
1118
                ReleaseCOMObjects(targetItem);
1119
            }
1120

    
1121
            ReleaseCOMObjects(_LMSymbol);
1122
        }
1123

    
1124
        /// <summary>
1125
        /// 첫 진입점
1126
        /// </summary>
1127
        /// <param name="symbol"></param>
1128
        private void SymbolModelingBySymbol(Symbol symbol)
1129
        {
1130
            SymbolModeling(symbol, null);
1131
            List<object> endObjects = new List<object>();
1132
            endObjects.Add(symbol);
1133

    
1134
            foreach (var connector in symbol.CONNECTORS)
1135
            {
1136
                object connItem = SPPIDUtil.FindObjectByUID(document, connector.CONNECTEDITEM);
1137
                if (connItem != null && connItem.GetType() != typeof(Equipment))
1138
                {
1139
                    endObjects.Add(connItem);
1140
                    if (connItem.GetType() == typeof(Symbol))
1141
                    {
1142
                        Symbol connSymbol = connItem as Symbol;
1143
                        if (string.IsNullOrEmpty(connSymbol.SPPID.RepresentationId))
1144
                        {
1145
                            SymbolModeling(connSymbol, symbol);
1146
                        }
1147
                        SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetProgress, document.SYMBOLS.FindAll(x => !string.IsNullOrEmpty(x.SPPID.RepresentationId)).Count);
1148
                        SymbolModelingByNeerSymbolLoop(connSymbol, endObjects);
1149
                    }
1150
                    else if (connItem.GetType() == typeof(Line))
1151
                    {
1152
                        Line connLine = connItem as Line;
1153
                        SymbolModelingByNeerLineLoop(connLine, endObjects, symbol);
1154
                    }
1155
                }
1156
            }
1157
        }
1158

    
1159
        private void SymbolModelingByNeerSymbolLoop(Symbol symbol, List<object> endObjects)
1160
        {
1161
            foreach (var connector in symbol.CONNECTORS)
1162
            {
1163
                object connItem = SPPIDUtil.FindObjectByUID(document, connector.CONNECTEDITEM);
1164
                if (connItem != null && connItem.GetType() != typeof(Equipment))
1165
                {
1166
                    if (!endObjects.Contains(connItem))
1167
                    {
1168
                        endObjects.Add(connItem);
1169
                        if (connItem.GetType() == typeof(Symbol))
1170
                        {
1171
                            Symbol connSymbol = connItem as Symbol;
1172
                            if (string.IsNullOrEmpty(connSymbol.SPPID.RepresentationId))
1173
                            {
1174
                                SymbolModeling(connSymbol, symbol);
1175
                            }
1176
                            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetProgress, document.SYMBOLS.FindAll(x => !string.IsNullOrEmpty(x.SPPID.RepresentationId)).Count);
1177
                            SymbolModelingByNeerSymbolLoop(connSymbol, endObjects);
1178
                        }
1179
                        else if (connItem.GetType() == typeof(Line))
1180
                        {
1181
                            Line connLine = connItem as Line;
1182
                            SymbolModelingByNeerLineLoop(connLine, endObjects, symbol);
1183
                        }
1184
                    }
1185
                }
1186
            }
1187
        }
1188

    
1189
        private void SymbolModelingByNeerLineLoop(Line line, List<object> endObjects, Symbol prevSymbol)
1190
        {
1191
            foreach (var connector in line.CONNECTORS)
1192
            {
1193
                object connItem = SPPIDUtil.FindObjectByUID(document, connector.CONNECTEDITEM);
1194
                if (connItem != null && connItem.GetType() != typeof(Equipment))
1195
                {
1196
                    if (!endObjects.Contains(connItem))
1197
                    {
1198
                        endObjects.Add(connItem);
1199
                        if (connItem.GetType() == typeof(Symbol))
1200
                        {
1201
                            Symbol connSymbol = connItem as Symbol;
1202
                            if (string.IsNullOrEmpty(connSymbol.SPPID.RepresentationId))
1203
                            {
1204
                                List<Symbol> group = new List<Symbol>();
1205
                                SPPIDUtil.FindConnectedSymbolGroup(document, connSymbol, group);
1206
                                Symbol priority = prioritySymbols.Find(x => group.Contains(x));
1207
                                List<Symbol> endModelingGroup = new List<Symbol>();
1208
                                if (priority != null)
1209
                                {
1210
                                    SymbolGroupModeling(priority, group);
1211

    
1212
                                    // Range 겹치는지 확인해야함
1213
                                    double[] prevRange = null;
1214
                                    GetSPPIDSymbolRange(prevSymbol, ref prevRange);
1215
                                    double[] groupRange = null;
1216
                                    GetSPPIDSymbolRange(group, ref groupRange);
1217

    
1218
                                    double distanceX = 0;
1219
                                    double distanceY = 0;
1220
                                    bool overlapX = false;
1221
                                    bool overlapY = false;
1222
                                    SlopeType slopeType = SPPIDUtil.CalcSlope(prevSymbol.SPPID.ORIGINAL_X, prevSymbol.SPPID.ORIGINAL_Y, connSymbol.SPPID.ORIGINAL_X, connSymbol.SPPID.ORIGINAL_Y);
1223
                                    SPPIDUtil.CalcOverlap(prevRange, groupRange, ref distanceX, ref distanceY, ref overlapX, ref overlapY);
1224
                                    if ((slopeType == SlopeType.HORIZONTAL && overlapX) ||
1225
                                        (slopeType == SlopeType.VERTICAL && overlapY))
1226
                                    {
1227
                                        RemoveSymbol(group);
1228
                                        foreach (var _temp in group)
1229
                                            SPPIDUtil.CalcNewCoordinateForSymbol(_temp, prevSymbol, distanceX, distanceY);
1230

    
1231
                                        SymbolGroupModeling(priority, group);
1232
                                    }
1233
                                }
1234
                                else
1235
                                {
1236
                                    SymbolModeling(connSymbol, null);
1237
                                    // Range 겹치는지 확인해야함
1238
                                    double[] prevRange = null;
1239
                                    GetSPPIDSymbolRange(prevSymbol, ref prevRange);
1240
                                    double[] connRange = null;
1241
                                    GetSPPIDSymbolRange(connSymbol, ref connRange);
1242

    
1243
                                    double distanceX = 0;
1244
                                    double distanceY = 0;
1245
                                    bool overlapX = false;
1246
                                    bool overlapY = false;
1247
                                    SlopeType slopeType = SPPIDUtil.CalcSlope(prevSymbol.SPPID.ORIGINAL_X, prevSymbol.SPPID.ORIGINAL_Y, connSymbol.SPPID.ORIGINAL_X, connSymbol.SPPID.ORIGINAL_Y);
1248
                                    SPPIDUtil.CalcOverlap(prevRange, connRange, ref distanceX, ref distanceY, ref overlapX, ref overlapY);
1249
                                    if ((slopeType == SlopeType.HORIZONTAL && overlapX) ||
1250
                                        (slopeType == SlopeType.VERTICAL && overlapY))
1251
                                    {
1252
                                        RemoveSymbol(connSymbol);
1253
                                        SPPIDUtil.CalcNewCoordinateForSymbol(connSymbol, prevSymbol, distanceX, distanceY);
1254

    
1255
                                        SymbolModeling(connSymbol, null);
1256
                                    }
1257
                                }
1258
                            }
1259
                            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetProgress, document.SYMBOLS.FindAll(x => !string.IsNullOrEmpty(x.SPPID.RepresentationId)).Count);
1260
                            SymbolModelingByNeerSymbolLoop(connSymbol, endObjects);
1261
                        }
1262
                        else if (connItem.GetType() == typeof(Line))
1263
                        {
1264
                            Line connLine = connItem as Line;
1265
                            if (!SPPIDUtil.IsBranchLine(connLine, line))
1266
                                SymbolModelingByNeerLineLoop(connLine, endObjects, prevSymbol);
1267
                        }
1268
                    }
1269
                }
1270
            }
1271
        }
1272

    
1273
        private void SymbolGroupModeling(Symbol firstSymbol, List<Symbol> group)
1274
        {
1275
            List<Symbol> endModelingGroup = new List<Symbol>();
1276
            SymbolModeling(firstSymbol, null);
1277
            endModelingGroup.Add(firstSymbol);
1278
            while (endModelingGroup.Count != group.Count)
1279
            {
1280
                foreach (var _symbol in group)
1281
                {
1282
                    if (!endModelingGroup.Contains(_symbol))
1283
                    {
1284
                        foreach (var _connector in _symbol.CONNECTORS)
1285
                        {
1286
                            Symbol _connSymbol = SPPIDUtil.FindObjectByUID(document, _connector.CONNECTEDITEM) as Symbol;
1287
                            if (_connSymbol != null && endModelingGroup.Contains(_connSymbol))
1288
                            {
1289
                                SymbolModeling(_symbol, _connSymbol);
1290
                                endModelingGroup.Add(_symbol);
1291
                                break;
1292
                            }
1293
                        }
1294
                    }
1295
                }
1296
            }
1297
        }
1298

    
1299
        /// <summary>
1300
        /// 심볼을 실제로 Modeling할때 ChildSymbol이 있다면 Modeling하는 메서드
1301
        /// </summary>
1302
        /// <param name="childSymbol"></param>
1303
        /// <param name="parentSymbol"></param>
1304
        private void CreateChildSymbol(ChildSymbol childSymbol, LMSymbol parentSymbol)
1305
        {
1306
            Ingr.RAD2D.Symbol2d symbol2d = radApp.ActiveDocument.ActiveSheet.DrawingObjects[parentSymbol.get_GraphicOID().ToString()];
1307
            double x1 = 0;
1308
            double x2 = 0;
1309
            double y1 = 0;
1310
            double y2 = 0;
1311
            symbol2d.Range(out x1, out y1, out x2, out y2);
1312

    
1313
            LMSymbol _LMSymbol = _placement.PIDPlaceSymbol(childSymbol.SPPID.MAPPINGNAME, (x1 + x2) / 2, (y1 + y2) / 2, TargetItem: parentSymbol);
1314
            if (_LMSymbol != null)
1315
            {
1316
                childSymbol.SPPID.RepresentationId = _LMSymbol.AsLMRepresentation().Id;
1317
                foreach (var item in childSymbol.ChildSymbols)
1318
                    CreateChildSymbol(item, _LMSymbol);
1319
            }
1320

    
1321

    
1322
            ReleaseCOMObjects(_LMSymbol);
1323
        }
1324

    
1325
        private void NewLineModeling(Line line, bool isBranchModeling = false, Tuple<LMConnector, bool, bool> reModelingInfo = null)
1326
        {
1327
            if (!string.IsNullOrEmpty(line.SPPID.ModelItemId) || (NewBranchLines.Contains(line) && !isBranchModeling))
1328
                return;
1329

    
1330
            List<Line> group = new List<Line>();
1331
            GetConnectedLineGroup(line, group);
1332
            LineCoordinateCorrection(group);
1333

    
1334
            foreach (var groupLine in group)
1335
            {
1336
                if (!isBranchModeling && SPPIDUtil.IsBranchLine(groupLine))
1337
                {
1338
                    NewBranchLines.Add(groupLine);
1339
                    continue;
1340
                }
1341

    
1342
                _LMAItem _LMAItem = _placement.PIDCreateItem(groupLine.SPPID.MAPPINGNAME);
1343
                LMSymbol _LMSymbolStart = null;
1344
                LMSymbol _LMSymbolEnd = null;
1345
                PlaceRunInputs placeRunInputs = new PlaceRunInputs();
1346
                foreach (var connector in groupLine.CONNECTORS)
1347
                {
1348
                    double x = 0;
1349
                    double y = 0;
1350
                    GetTargetLineConnectorPoint(connector, groupLine, ref x, ref y);
1351
                    if (connector.ConnectedObject == null)
1352
                    {
1353
                        placeRunInputs.AddPoint(x, y);
1354
                    }
1355
                    else if (connector.ConnectedObject.GetType() == typeof(Symbol))
1356
                    {
1357
                        Symbol targetSymbol = connector.ConnectedObject as Symbol;
1358
                        if (groupLine.CONNECTORS.IndexOf(connector) == 0)
1359
                        {
1360
                            _LMSymbolStart = GetTargetSymbol(targetSymbol, groupLine);
1361
                            placeRunInputs.AddSymbolTarget(_LMSymbolStart, x, y);
1362
                        }
1363
                        else
1364
                        {
1365
                            _LMSymbolEnd = GetTargetSymbol(targetSymbol, groupLine);
1366
                            placeRunInputs.AddSymbolTarget(_LMSymbolEnd, x, y);
1367
                        }
1368
                    }
1369
                    else if (connector.ConnectedObject.GetType() == typeof(Line))
1370
                    {
1371
                        Line targetLine = connector.ConnectedObject as Line;
1372
                        if (!string.IsNullOrEmpty(targetLine.SPPID.ModelItemId))
1373
                        {
1374
                            LMConnector targetConnector = FindTargetLMConnectorForBranch(line, targetLine, ref x, ref y);
1375
                            placeRunInputs.AddConnectorTarget(targetConnector, x, y);
1376
                            ChangeLineSPPIDCoordinateByConnector(groupLine, targetLine, x, y, false);
1377
                        }
1378
                        else
1379
                        {
1380
                            if (groupLine.CONNECTORS.IndexOf(connector) == 0)
1381
                            {
1382
                                if (groupLine.SlopeType == SlopeType.HORIZONTAL)
1383
                                    placeRunInputs.AddPoint(x, -0.1);
1384
                                else if (groupLine.SlopeType == SlopeType.VERTICAL)
1385
                                    placeRunInputs.AddPoint(-0.1, y);
1386
                                else
1387
                                    placeRunInputs.AddPoint(x, -0.1);
1388
                            }
1389

    
1390
                            placeRunInputs.AddPoint(x, y);
1391

    
1392
                            if (groupLine.CONNECTORS.IndexOf(connector) == 1)
1393
                            {
1394
                                if (groupLine.SlopeType == SlopeType.HORIZONTAL)
1395
                                    placeRunInputs.AddPoint(x, -0.1);
1396
                                else if (groupLine.SlopeType == SlopeType.VERTICAL)
1397
                                    placeRunInputs.AddPoint(-0.1, y);
1398
                                else
1399
                                    placeRunInputs.AddPoint(x, -0.1);
1400
                            }
1401
                        }
1402
                    }
1403
                }
1404

    
1405
                LMConnector _lMConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
1406
                if (_lMConnector != null)
1407
                {
1408
                    groupLine.SPPID.ModelItemId = _lMConnector.ModelItemID;
1409
                    bool bRemodelingStart = false;
1410
                    if (_LMSymbolStart != null)
1411
                        NeedReModeling(groupLine, _LMSymbolStart, ref bRemodelingStart);
1412
                    bool bRemodelingEnd = false;
1413
                    if (_LMSymbolEnd != null)
1414
                        NeedReModeling(groupLine, _LMSymbolEnd, ref bRemodelingEnd);
1415

    
1416
                    if (bRemodelingStart || bRemodelingEnd)
1417
                        ReModelingLine(groupLine, _lMConnector, _LMSymbolStart, _LMSymbolEnd, bRemodelingStart, bRemodelingEnd);
1418
                    
1419
                    ReleaseCOMObjects(_lMConnector);
1420
                }
1421

    
1422
                List<object> removeLines = groupLine.CONNECTORS.FindAll(x =>
1423
                x.ConnectedObject != null &&
1424
                x.ConnectedObject.GetType() == typeof(Line) &&
1425
                !string.IsNullOrEmpty(((Line)x.ConnectedObject).SPPID.ModelItemId))
1426
                .Select(x => x.ConnectedObject)
1427
                .ToList();
1428

    
1429
                foreach (var item in removeLines)
1430
                    RemoveLineForModeling(item as Line);
1431

    
1432
                if (_LMAItem != null)
1433
                    ReleaseCOMObjects(_LMAItem);
1434
                if (placeRunInputs != null)
1435
                    ReleaseCOMObjects(placeRunInputs);
1436
                if (_LMSymbolStart != null)
1437
                    ReleaseCOMObjects(_LMSymbolStart);
1438
                if (_LMSymbolEnd != null)
1439
                    ReleaseCOMObjects(_LMSymbolEnd);
1440

    
1441
                if (isBranchModeling && NewBranchLines.Contains(groupLine))
1442
                    NewBranchLines.Remove(groupLine);
1443
            }
1444
        }
1445

    
1446
        private void RemoveLineForModeling(Line line)
1447
        {
1448
            LMModelItem modelItem = dataSource.GetModelItem(line.SPPID.ModelItemId);
1449
            if (modelItem != null)
1450
            {
1451
                foreach (LMRepresentation rep in modelItem.Representations)
1452
                {
1453
                    if (rep.Attributes["RepresentationType"].get_Value() == "Connector" && rep.Attributes["ItemStatus"].get_Value() == "Active")
1454
                    {
1455
                        LMConnector _LMConnector = dataSource.GetConnector(rep.Id);
1456
                        dynamic OID = rep.get_GraphicOID();
1457
                        DependencyObject drawingObject = radApp.ActiveDocument.ActiveSheet.DrawingObjects[OID];
1458
                        Ingr.RAD2D.LineStringGeometry2d lineStringGeometry = drawingObject.GetGeometry() as Ingr.RAD2D.LineStringGeometry2d;
1459
                        int verticesCount = lineStringGeometry.VertexCount;
1460
                        double[] vertices = null;
1461
                        lineStringGeometry.GetVertices(ref verticesCount, ref vertices);
1462
                        for (int i = 0; i < verticesCount; i++)
1463
                        {
1464
                            double x = 0;
1465
                            double y = 0;
1466
                            lineStringGeometry.GetVertex(i + 1, ref x, ref y);
1467
                            if (verticesCount == 2 && x < 0 || y < 0)
1468
                                _placement.PIDRemovePlacement(rep);
1469
                        }
1470
                        ReleaseCOMObjects(_LMConnector);
1471
                    }
1472
                }
1473

    
1474
                ReleaseCOMObjects(modelItem);
1475
            }
1476
        }
1477

    
1478
        private void GetConnectedLineGroup(Line line, List<Line> group)
1479
        {
1480
            if (!group.Contains(line))
1481
                group.Add(line);
1482
            foreach (var connector in line.CONNECTORS)
1483
            {
1484
                if (connector.ConnectedObject != null &&
1485
                    connector.ConnectedObject.GetType() == typeof(Line) &&
1486
                    !group.Contains(connector.ConnectedObject) &&
1487
                    string.IsNullOrEmpty(((Line)connector.ConnectedObject).SPPID.ModelItemId))
1488
                {
1489
                    Line connLine = connector.ConnectedObject as Line;
1490
                    if (!SPPIDUtil.IsBranchLine(connLine, line))
1491
                        GetConnectedLineGroup(connLine, group);
1492
                }
1493
            }
1494
        }
1495

    
1496
        private void LineCoordinateCorrection(List<Line> group)
1497
        {
1498
            // 순서대로 전 Item 기준 정렬
1499
            LineCoordinateCorrectionByStart(group);
1500

    
1501
            // 역으로 심볼이 있을 경우 좌표 보정
1502
            LineCoordinateCorrectionForLastLine(group);
1503
        }
1504

    
1505
        private void LineCoordinateCorrectionByStart(List<Line> group)
1506
        {
1507
            for (int i = 0; i < group.Count; i++)
1508
            {
1509
                Line line = group[i];
1510
                if (i == 0)
1511
                {
1512
                    Connector symbolConnector = line.CONNECTORS.Find(x => x.ConnectedObject != null && x.ConnectedObject.GetType() == typeof(Symbol));
1513
                    if (symbolConnector != null)
1514
                        LineCoordinateCorrectionByConnItem(line, symbolConnector.ConnectedObject);
1515
                }
1516
                else if (i != 0)
1517
                {
1518
                    LineCoordinateCorrectionByConnItem(line, group[i - 1]);
1519
                }
1520
            }
1521
        }
1522

    
1523
        private void LineCoordinateCorrectionForLastLine(List<Line> group)
1524
        {
1525
            Line checkLine = group[group.Count - 1];
1526
            Connector lastSymbolConnector = checkLine.CONNECTORS.Find(x => x.ConnectedObject != null && x.ConnectedObject.GetType() == typeof(Symbol));
1527
            if (lastSymbolConnector != null)
1528
            {
1529
                LineCoordinateCorrectionByConnItem(checkLine, lastSymbolConnector.ConnectedObject);
1530
                for (int i = group.Count - 2; i >= 0; i--)
1531
                {
1532
                    Line line = group[i + 1];
1533
                    Line prevLine = group[i];
1534

    
1535
                    // 같으면 보정
1536
                    if (line.SlopeType == prevLine.SlopeType)
1537
                        LineCoordinateCorrectionByConnItem(prevLine, line);
1538
                    else
1539
                    {
1540
                        if (line.SlopeType == SlopeType.HORIZONTAL)
1541
                        {
1542
                            double prevX = 0;
1543
                            double prevY = 0;
1544
                            GetTargetLineConnectorPoint(prevLine.CONNECTORS.Find(z => z.ConnectedObject == line), prevLine, ref prevX, ref prevY);
1545
                            ChangeLineSPPIDCoordinateByConnectorOnlyX(line, prevLine, prevX);
1546

    
1547
                            double x = 0;
1548
                            double y = 0;
1549
                            GetTargetLineConnectorPoint(line.CONNECTORS.Find(z => z.ConnectedObject == prevLine), line, ref x, ref y);
1550
                            ChangeLineSPPIDCoordinateByConnectorOnlyY(prevLine, line, y);
1551
                        }
1552
                        else if (line.SlopeType == SlopeType.VERTICAL)
1553
                        {
1554
                            double prevX = 0;
1555
                            double prevY = 0;
1556
                            GetTargetLineConnectorPoint(prevLine.CONNECTORS.Find(z => z.ConnectedObject == line), prevLine, ref prevX, ref prevY);
1557
                            ChangeLineSPPIDCoordinateByConnectorOnlyY(line, prevLine, prevY);
1558

    
1559
                            double x = 0;
1560
                            double y = 0;
1561
                            GetTargetLineConnectorPoint(line.CONNECTORS.Find(z => z.ConnectedObject == prevLine), line, ref x, ref y);
1562
                            ChangeLineSPPIDCoordinateByConnectorOnlyX(prevLine, line, x);
1563
                        }
1564
                        break;
1565
                    }
1566
                }
1567
            }
1568
        }
1569

    
1570
        private void LineCoordinateCorrectionByConnItem(Line line, object connItem)
1571
        {
1572
            double x = 0;
1573
            double y = 0;
1574
            if (connItem.GetType() == typeof(Symbol))
1575
            {
1576
                Symbol targetSymbol = connItem as Symbol;
1577
                Connector targetConnector = targetSymbol.CONNECTORS.Find(z => z.ConnectedObject == line);
1578
                if (targetConnector != null)
1579
                    GetTargetSymbolConnectorPoint(targetConnector, targetSymbol, ref x, ref y);
1580
                else
1581
                    throw new Exception("Target symbol UID : " + targetSymbol.UID + "\r\nLine UID : " + line.UID);
1582
            }
1583
            else if (connItem.GetType() == typeof(Line))
1584
            {
1585
                Line targetLine = connItem as Line;
1586
                GetTargetLineConnectorPoint(targetLine.CONNECTORS.Find(z => z.ConnectedObject == line), targetLine, ref x, ref y);
1587
            }
1588

    
1589
            ChangeLineSPPIDCoordinateByConnector(line, connItem, x, y);
1590
        }
1591

    
1592
        private void ChangeLineSPPIDCoordinateByConnector(Line line, object connItem, double x, double y, bool changeOtherCoordinate = true)
1593
        {
1594
            Connector connector = line.CONNECTORS.Find(z => z.ConnectedObject == connItem);
1595
            int index = line.CONNECTORS.IndexOf(connector);
1596
            if (index == 0)
1597
            {
1598
                line.SPPID.START_X = x;
1599
                line.SPPID.START_Y = y;
1600
                if (line.SlopeType == SlopeType.HORIZONTAL && changeOtherCoordinate)
1601
                    line.SPPID.END_Y = y;
1602
                else if (line.SlopeType == SlopeType.VERTICAL && changeOtherCoordinate)
1603
                    line.SPPID.END_X = x;
1604
            }
1605
            else
1606
            {
1607
                line.SPPID.END_X = x;
1608
                line.SPPID.END_Y = y;
1609
                if (line.SlopeType == SlopeType.HORIZONTAL && changeOtherCoordinate)
1610
                    line.SPPID.START_Y = y;
1611
                else if (line.SlopeType == SlopeType.VERTICAL && changeOtherCoordinate)
1612
                    line.SPPID.START_X = x;
1613
            }
1614
        }
1615

    
1616
        private void ChangeLineSPPIDCoordinateByConnectorOnlyX(Line line, object connItem, double x)
1617
        {
1618
            Connector connector = line.CONNECTORS.Find(z => z.ConnectedObject == connItem);
1619
            int index = line.CONNECTORS.IndexOf(connector);
1620
            if (index == 0)
1621
            {
1622
                line.SPPID.START_X = x;
1623
                if (line.SlopeType == SlopeType.VERTICAL)
1624
                    line.SPPID.END_X = x;
1625
            }
1626
            else
1627
            {
1628
                line.SPPID.END_X = x;
1629
                if (line.SlopeType == SlopeType.VERTICAL)
1630
                    line.SPPID.START_X = x;
1631
            }
1632
        }
1633

    
1634
        private void ChangeLineSPPIDCoordinateByConnectorOnlyY(Line line, object connItem, double y)
1635
        {
1636
            Connector connector = line.CONNECTORS.Find(z => z.ConnectedObject == connItem);
1637
            int index = line.CONNECTORS.IndexOf(connector);
1638
            if (index == 0)
1639
            {
1640
                line.SPPID.START_Y = y;
1641
                if (line.SlopeType == SlopeType.HORIZONTAL)
1642
                    line.SPPID.END_Y = y;
1643
            }
1644
            else
1645
            {
1646
                line.SPPID.END_Y = y;
1647
                if (line.SlopeType == SlopeType.HORIZONTAL)
1648
                    line.SPPID.START_Y = y;
1649
            }
1650
        }
1651

    
1652
        private void NeedReModeling(Line line, LMSymbol symbol, ref bool result)
1653
        {
1654
            if (symbol != null)
1655
            {
1656
                string repID = symbol.AsLMRepresentation().Id;
1657
                string symbolUID = SPPIDUtil.FindSymbolByRepresentationID(document, repID).UID;
1658
                string lineUID = line.UID;
1659

    
1660
                SpecBreak startSpecBreak = document.SpecBreaks.Find(x =>
1661
                (x.DownStreamUID == symbolUID || x.UpStreamUID == symbolUID) &&
1662
                (x.DownStreamUID == lineUID || x.UpStreamUID == lineUID));
1663

    
1664
                EndBreak startEndBreak = document.EndBreaks.Find(x =>
1665
                (x.OWNER == symbolUID || x.PROPERTIES.Find(y => y.ATTRIBUTE == "Connected Item").VALUE == symbolUID) &&
1666
                (x.OWNER == lineUID || x.PROPERTIES.Find(y => y.ATTRIBUTE == "Connected Item").VALUE == lineUID));
1667

    
1668
                if (startSpecBreak != null || startEndBreak != null)
1669
                    result = true;
1670
            }
1671
        }
1672

    
1673
        /// <summary>
1674
        /// Symbol에 붙을 경우 Line을 Remodeling 한다.
1675
        /// </summary>
1676
        /// <param name="lines"></param>
1677
        /// <param name="prevLMConnector"></param>
1678
        /// <param name="startSymbol"></param>
1679
        /// <param name="endSymbol"></param>
1680
        private void ReModelingLine(Line line, LMConnector prevLMConnector, LMSymbol startSymbol, LMSymbol endSymbol, bool bStart, bool bEnd)
1681
        {
1682
            string symbolPath = string.Empty;
1683
            #region get symbol path
1684
            LMModelItem modelItem = dataSource.GetModelItem(prevLMConnector.ModelItemID);
1685
            foreach (LMRepresentation rep in modelItem.Representations)
1686
            {
1687
                if (!DBNull.Value.Equals(rep.get_FileName()) && !string.IsNullOrEmpty(rep.get_FileName()))
1688
                {
1689
                    symbolPath = rep.get_FileName();
1690
                    break;
1691
                }
1692
            }
1693
            #endregion
1694
            _LMAItem _LMAItem = _placement.PIDCreateItem(symbolPath);
1695
            LMConnector newConnector = null;
1696
            dynamic OID = prevLMConnector.get_GraphicOID();
1697
            DependencyObject drawingObject = radApp.ActiveDocument.ActiveSheet.DrawingObjects[OID];
1698
            Ingr.RAD2D.LineStringGeometry2d lineStringGeometry = drawingObject.GetGeometry() as Ingr.RAD2D.LineStringGeometry2d;
1699
            int verticesCount = lineStringGeometry.VertexCount;
1700
            PlaceRunInputs placeRunInputs = new PlaceRunInputs();
1701

    
1702
            List<double[]> vertices = new List<double[]>();
1703
            for (int i = 1; i <= verticesCount; i++)
1704
            {
1705
                double x = 0;
1706
                double y = 0;
1707
                lineStringGeometry.GetVertex(i, ref x, ref y);
1708
                vertices.Add(new double[] { x, y });
1709
            }
1710

    
1711
            for (int i = 0; i < vertices.Count; i++)
1712
            {
1713
                double[] points = vertices[i];
1714
                // 시작 심볼이 있고 첫번째 좌표일 때
1715
                if (startSymbol != null && i == 0)
1716
                {
1717
                    if (bStart)
1718
                    {
1719
                        SlopeType slopeType = SPPIDUtil.CalcSlope(points[0], points[1], vertices[i + 1][0], vertices[i + 1][1]);
1720
                        if (slopeType == SlopeType.HORIZONTAL)
1721
                            placeRunInputs.AddPoint(points[0], -0.1);
1722
                        else if (slopeType == SlopeType.VERTICAL)
1723
                            placeRunInputs.AddPoint(-0.1, points[1]);
1724
                        else
1725
                            placeRunInputs.AddPoint(points[0], -0.1);
1726

    
1727
                        placeRunInputs.AddPoint(points[0], points[1]);
1728
                    }
1729
                    else
1730
                    {
1731
                        placeRunInputs.AddSymbolTarget(startSymbol, points[0], points[1]);
1732
                    }
1733
                }
1734
                // 마지막 심볼이 있고 마지막 좌표일 때
1735
                else if (endSymbol != null && i == vertices.Count - 1)
1736
                {
1737
                    if (bEnd)
1738
                    {
1739
                        placeRunInputs.AddPoint(points[0], points[1]);
1740

    
1741
                        SlopeType slopeType = SPPIDUtil.CalcSlope(points[0], points[1], vertices[i - 1][0], vertices[i - 1][1]);
1742
                        if (slopeType == SlopeType.HORIZONTAL)
1743
                            placeRunInputs.AddPoint(points[0], -0.1);
1744
                        else if (slopeType == SlopeType.VERTICAL)
1745
                            placeRunInputs.AddPoint(-0.1, points[1]);
1746
                        else
1747
                            placeRunInputs.AddPoint(points[0], -0.1);
1748
                    }
1749
                    else
1750
                    {
1751
                        placeRunInputs.AddSymbolTarget(endSymbol, points[0], points[1]);
1752
                    }
1753
                }
1754
                // 첫번째이며 시작 심볼이 아니고 Connecotr일 경우
1755
                else if (i == 0 && prevLMConnector.ConnectItem1SymbolObject != null)
1756
                    placeRunInputs.AddSymbolTarget(prevLMConnector.ConnectItem1SymbolObject, points[0], points[1]);
1757
                // 마지막이며 마지막 심볼이 아니고 Connecotr일 경우
1758
                else if (i == vertices.Count - 1 && prevLMConnector.ConnectItem2SymbolObject != null)
1759
                    placeRunInputs.AddSymbolTarget(prevLMConnector.ConnectItem2SymbolObject, points[0], points[1]);
1760
                else
1761
                    placeRunInputs.AddPoint(points[0], points[1]);
1762
            }
1763

    
1764
            _placement.PIDRemovePlacement(prevLMConnector.AsLMRepresentation());
1765
            newConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
1766

    
1767
            ReleaseCOMObjects(placeRunInputs);
1768
            ReleaseCOMObjects(_LMAItem);
1769
            ReleaseCOMObjects(modelItem);
1770

    
1771
            if (newConnector != null)
1772
            {
1773
                if (startSymbol != null && bStart)
1774
                {
1775
                    _LMAItem = _placement.PIDCreateItem(symbolPath);
1776
                    placeRunInputs = new PlaceRunInputs();
1777
                    placeRunInputs.AddSymbolTarget(startSymbol, vertices[0][0], vertices[0][1]);
1778
                    placeRunInputs.AddConnectorTarget(newConnector, vertices[0][0], vertices[0][1]);
1779
                    LMConnector _LMConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
1780
                    if (_LMConnector != null)
1781
                    {
1782
                        RemoveConnectorForReModelingLine(newConnector);
1783
                        ZeroLengthModelItemID.Add(_LMConnector.ModelItemID);
1784
                        ReleaseCOMObjects(_LMConnector);
1785
                    }
1786
                    ReleaseCOMObjects(placeRunInputs);
1787
                    ReleaseCOMObjects(_LMAItem);
1788
                }
1789

    
1790
                if (endSymbol != null && bEnd)
1791
                {
1792
                    if (startSymbol != null)
1793
                    {
1794
                        Dictionary<LMConnector, List<double[]>> dicVertices = GetPipeRunVertices(newConnector.ModelItemID);
1795
                        newConnector = dicVertices.First().Key;
1796
                    }
1797

    
1798
                    _LMAItem = _placement.PIDCreateItem(symbolPath);
1799
                    placeRunInputs = new PlaceRunInputs();
1800
                    placeRunInputs.AddSymbolTarget(endSymbol, vertices[vertices.Count - 1][0], vertices[vertices.Count - 1][1]);
1801
                    placeRunInputs.AddConnectorTarget(newConnector, vertices[vertices.Count - 1][0], vertices[vertices.Count - 1][1]);
1802
                    LMConnector _LMConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
1803
                    if (_LMConnector != null)
1804
                    {
1805
                        RemoveConnectorForReModelingLine(newConnector);
1806
                        ZeroLengthModelItemID.Add(_LMConnector.ModelItemID);
1807
                        ReleaseCOMObjects(_LMConnector);
1808
                    }
1809
                    ReleaseCOMObjects(placeRunInputs);
1810
                    ReleaseCOMObjects(_LMAItem);
1811
                }
1812

    
1813
                line.SPPID.ModelItemId = newConnector.ModelItemID;
1814
                ReleaseCOMObjects(newConnector);
1815
            }
1816

    
1817
            ReleaseCOMObjects(modelItem);
1818
        }
1819

    
1820
        /// <summary>
1821
        /// Remodeling 과정에서 생긴 불필요한 Connector 제거
1822
        /// </summary>
1823
        /// <param name="connector"></param>
1824
        private void RemoveConnectorForReModelingLine(LMConnector connector)
1825
        {
1826
            Dictionary<LMConnector, List<double[]>> dicVertices = GetPipeRunVertices(connector.ModelItemID);
1827
            foreach (var item in dicVertices)
1828
            {
1829
                if (item.Value.Count == 2)
1830
                {
1831
                    bool result = false;
1832
                    foreach (var point in item.Value)
1833
                    {
1834
                        if (point[0] < 0 || point[1] < 0)
1835
                        {
1836
                            result = true;
1837
                            _placement.PIDRemovePlacement(item.Key.AsLMRepresentation());
1838
                            break;
1839
                        }
1840
                    }
1841

    
1842
                    if (result)
1843
                        break;
1844
                }
1845
            }
1846
            foreach (var item in dicVertices)
1847
                ReleaseCOMObjects(item.Key);
1848
        }
1849

    
1850
        /// <summary>
1851
        /// Symbol이 모델링된 SPPPID Symbol Object를 반환 - 연결된 Symbol이 ChildSymbol일 수도 있기때문에 메서드 개발
1852
        /// </summary>
1853
        /// <param name="symbol"></param>
1854
        /// <param name="line"></param>
1855
        /// <returns></returns>
1856
        private LMSymbol GetTargetSymbol(Symbol symbol, Line line)
1857
        {
1858
            LMSymbol _LMSymbol = null;
1859
            foreach (var connector in symbol.CONNECTORS)
1860
            {
1861
                if (connector.CONNECTEDITEM == line.UID)
1862
                {
1863
                    if (connector.Index == 0)
1864
                        _LMSymbol = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
1865
                    else
1866
                    {
1867
                        ChildSymbol child = null;
1868
                        foreach (var childSymbol in symbol.ChildSymbols)
1869
                        {
1870
                            if (childSymbol.Connectors.Contains(connector))
1871
                                child = childSymbol;
1872
                            else
1873
                                child = GetChildSymbolByConnector(childSymbol, connector);
1874

    
1875
                            if (child != null)
1876
                                break;
1877
                        }
1878

    
1879
                        if (child != null)
1880
                            _LMSymbol = dataSource.GetSymbol(child.SPPID.RepresentationId);
1881
                    }
1882

    
1883
                    break;
1884
                }
1885
            }
1886

    
1887
            return _LMSymbol;
1888
        }
1889

    
1890
        /// <summary>
1891
        /// Connector를 가지고 있는 ChildSymbol Object 반환
1892
        /// </summary>
1893
        /// <param name="item"></param>
1894
        /// <param name="connector"></param>
1895
        /// <returns></returns>
1896
        private ChildSymbol GetChildSymbolByConnector(ChildSymbol item, Connector connector)
1897
        {
1898
            foreach (var childSymbol in item.ChildSymbols)
1899
            {
1900
                if (childSymbol.Connectors.Contains(connector))
1901
                    return childSymbol;
1902
                else
1903
                    return GetChildSymbolByConnector(childSymbol, connector);
1904
            }
1905

    
1906
            return null;
1907
        }
1908

    
1909
        /// <summary>
1910
        /// EndBreak 모델링 메서드
1911
        /// </summary>
1912
        /// <param name="endBreak"></param>
1913
        private void EndBreakModeling(EndBreak endBreak)
1914
        {
1915
            object ownerObj = SPPIDUtil.FindObjectByUID(document, endBreak.OWNER);
1916
            object connectedItem = SPPIDUtil.FindObjectByUID(document, endBreak.PROPERTIES.Find(x => x.ATTRIBUTE == "Connected Item").VALUE);
1917
            LMConnector targetLMConnector = FindBreakLineTarget(ownerObj, connectedItem);
1918

    
1919
            if (targetLMConnector != null)
1920
            {
1921
                Array array = new double[] { 0, endBreak.SPPID.ORIGINAL_X, endBreak.SPPID.ORIGINAL_Y };
1922
                LMLabelPersist _LmLabelPersist = _placement.PIDPlaceLabel(endBreak.SPPID.MAPPINGNAME, ref array, Rotation: 0, LabeledItem: targetLMConnector.AsLMRepresentation(), IsLeaderVisible: true);
1923
                if (_LmLabelPersist != null)
1924
                {
1925
                    endBreak.SPPID.RepresentationId = _LmLabelPersist.AsLMRepresentation().Id;
1926
                    endBreak.SPPID.ModelItemID = _LmLabelPersist.ModelItemID;
1927
                    endBreak.SPPID.GraphicOID = _LmLabelPersist.get_GraphicOID();
1928
                    ReleaseCOMObjects(_LmLabelPersist);
1929
                }
1930
                ReleaseCOMObjects(targetLMConnector);
1931
            }
1932
        }
1933

    
1934
        private LMConnector ReModelingLMConnector(LMConnector connector)
1935
        {
1936
            string symbolPath = string.Empty;
1937
            #region get symbol path
1938
            LMModelItem modelItem = dataSource.GetModelItem(connector.ModelItemID);
1939
            foreach (LMRepresentation rep in modelItem.Representations)
1940
            {
1941
                if (!DBNull.Value.Equals(rep.get_FileName()) && !string.IsNullOrEmpty(rep.get_FileName()))
1942
                {
1943
                    symbolPath = rep.get_FileName();
1944
                    break;
1945
                }
1946
            }
1947
            #endregion
1948

    
1949
            LMConnector newConnector = null;
1950
            dynamic OID = connector.get_GraphicOID();
1951
            DependencyObject drawingObject = radApp.ActiveDocument.ActiveSheet.DrawingObjects[OID];
1952
            Ingr.RAD2D.LineStringGeometry2d lineStringGeometry = drawingObject.GetGeometry() as Ingr.RAD2D.LineStringGeometry2d;
1953
            int verticesCount = lineStringGeometry.VertexCount;
1954
            PlaceRunInputs placeRunInputs = new PlaceRunInputs();
1955
            _LMAItem _LMAItem = _placement.PIDCreateItem(symbolPath);
1956

    
1957
            if (Convert.ToBoolean(connector.get_IsZeroLength()))
1958
            {
1959
                double[] vertices = null;
1960
                lineStringGeometry.GetVertices(ref verticesCount, ref vertices);
1961
                double x = 0;
1962
                double y = 0;
1963
                lineStringGeometry.GetVertex(1, ref x, ref y);
1964

    
1965
                placeRunInputs.AddSymbolTarget(connector.ConnectItem1SymbolObject, x, y);
1966
                placeRunInputs.AddSymbolTarget(connector.ConnectItem2SymbolObject, x, y);
1967

    
1968
                _placement.PIDRemovePlacement(connector.AsLMRepresentation());
1969
                newConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
1970
            }
1971
            else
1972
            {
1973
                List<double[]> vertices = new List<double[]>();
1974
                for (int i = 1; i <= verticesCount; i++)
1975
                {
1976
                    double x = 0;
1977
                    double y = 0;
1978
                    lineStringGeometry.GetVertex(i, ref x, ref y);
1979
                    vertices.Add(new double[] { x, y });
1980
                }
1981

    
1982
                for (int i = 0; i < vertices.Count; i++)
1983
                {
1984
                    double[] points = vertices[i];
1985
                    if (i == 0)
1986
                    {
1987
                        if (connector.ConnectItem1SymbolObject != null)
1988
                            placeRunInputs.AddSymbolTarget(connector.ConnectItem1SymbolObject, points[0], points[1]);
1989
                        else
1990
                            placeRunInputs.AddPoint(points[0], points[1]);
1991
                    }
1992
                    else if (i == vertices.Count - 1)
1993
                    {
1994
                        if (connector.ConnectItem2SymbolObject != null)
1995
                            placeRunInputs.AddSymbolTarget(connector.ConnectItem2SymbolObject, points[0], points[1]);
1996
                        else
1997
                            placeRunInputs.AddPoint(points[0], points[1]);
1998
                    }
1999
                    else
2000
                        placeRunInputs.AddPoint(points[0], points[1]);
2001
                }
2002

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

    
2005
                _placement.PIDRemovePlacement(connector.AsLMRepresentation());
2006
                newConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
2007

    
2008
                foreach (var line in lines)
2009
                    line.SPPID.ModelItemId = newConnector.ModelItemID;
2010
            }
2011

    
2012

    
2013
            return newConnector;
2014
        }
2015

    
2016
        /// <summary>
2017
        /// SpecBreak Modeling 메서드
2018
        /// </summary>
2019
        /// <param name="specBreak"></param>
2020
        private void SpecBreakModeling(SpecBreak specBreak)
2021
        {
2022
            object upStreamObj = SPPIDUtil.FindObjectByUID(document, specBreak.UpStreamUID);
2023
            object downStreamObj = SPPIDUtil.FindObjectByUID(document, specBreak.DownStreamUID);
2024

    
2025
            if (upStreamObj != null &&
2026
                downStreamObj != null)
2027
            {
2028
                LMConnector targetLMConnector = FindBreakLineTarget(upStreamObj, downStreamObj);
2029

    
2030
                if (targetLMConnector != null)
2031
                {
2032
                    foreach (var attribute in specBreak.ATTRIBUTES)
2033
                    {
2034
                        AttributeMapping mapping = document.AttributeMappings.Find(x => x.UID == attribute.UID);
2035
                        if (mapping != null && !string.IsNullOrEmpty(mapping.SPPIDSYMBOLNAME) && mapping.SPPIDSYMBOLNAME != "None")
2036
                        {
2037
                            string MappingPath = mapping.SPPIDSYMBOLNAME;
2038
                            Array array = new double[] { 0, specBreak.SPPID.ORIGINAL_X, specBreak.SPPID.ORIGINAL_Y };
2039
                            LMLabelPersist _LmLabelPersist = _placement.PIDPlaceLabel(MappingPath, ref array, Rotation: specBreak.ANGLE, LabeledItem: targetLMConnector.AsLMRepresentation(), IsLeaderVisible: mapping.LeaderLine);
2040

    
2041
                            if (_LmLabelPersist != null)
2042
                            {
2043
                                specBreak.SPPID.RepresentationId = _LmLabelPersist.AsLMRepresentation().Id;
2044
                                specBreak.SPPID.ModelItemID = _LmLabelPersist.ModelItemID;
2045
                                specBreak.SPPID.GraphicOID = _LmLabelPersist.get_GraphicOID();
2046
                                ReleaseCOMObjects(_LmLabelPersist);
2047
                            }
2048
                        }
2049
                    }
2050
                    ReleaseCOMObjects(targetLMConnector);
2051
                }
2052
            }
2053
        }
2054

    
2055
        private LMConnector FindBreakLineTarget(object targetObj, object connectedObj)
2056
        {
2057
            LMConnector targetConnector = null;
2058
            Symbol targetSymbol = targetObj as Symbol;
2059
            Symbol connectedSymbol = connectedObj as Symbol;
2060
            Line targetLine = targetObj as Line;
2061
            Line connectedLine = connectedObj as Line;
2062
            if (targetSymbol != null && connectedSymbol != null)
2063
            {
2064
                LMSymbol targetLMSymbol = dataSource.GetSymbol(targetSymbol.SPPID.RepresentationId);
2065
                LMSymbol connectedLMSymbol = dataSource.GetSymbol(connectedSymbol.SPPID.RepresentationId);
2066

    
2067
                foreach (LMConnector connector in targetLMSymbol.Avoid1Connectors)
2068
                {
2069
                    if (connector.get_ItemStatus() != "Active")
2070
                        continue;
2071

    
2072
                    if (connector.ConnectItem1SymbolObject.Id == connectedLMSymbol.Id)
2073
                    {
2074
                        targetConnector = connector;
2075
                        break;
2076
                    }
2077
                    else if (connector.ConnectItem2SymbolObject.Id == connectedLMSymbol.Id)
2078
                    {
2079
                        targetConnector = connector;
2080
                        break;
2081
                    }
2082
                }
2083

    
2084
                foreach (LMConnector connector in targetLMSymbol.Avoid2Connectors)
2085
                {
2086
                    if (connector.get_ItemStatus() != "Active")
2087
                        continue;
2088

    
2089
                    if (connector.ConnectItem1SymbolObject.Id == connectedLMSymbol.Id)
2090
                    {
2091
                        targetConnector = connector;
2092
                        break;
2093
                    }
2094
                    else if (connector.ConnectItem2SymbolObject.Id == connectedLMSymbol.Id)
2095
                    {
2096
                        targetConnector = connector;
2097
                        break;
2098
                    }
2099
                }
2100

    
2101
                ReleaseCOMObjects(targetLMSymbol);
2102
                ReleaseCOMObjects(connectedLMSymbol);
2103
            }
2104
            else if (targetLine != null && connectedLine != null)
2105
            {
2106
                LMModelItem targetModelItem = dataSource.GetModelItem(targetLine.SPPID.ModelItemId);
2107
                LMModelItem connectedModelItem = dataSource.GetModelItem(connectedLine.SPPID.ModelItemId);
2108

    
2109
                if (targetModelItem != null && connectedModelItem != null)
2110
                {
2111
                    foreach (LMRepresentation rep in targetModelItem.Representations)
2112
                    {
2113
                        if (targetConnector != null)
2114
                            break;
2115

    
2116
                        if (rep.Attributes["RepresentationType"].get_Value() == "Connector" && rep.Attributes["ItemStatus"].get_Value() == "Active")
2117
                        {
2118
                            LMConnector _LMConnector = dataSource.GetConnector(rep.Id);
2119

    
2120
                            if (IsConnected(_LMConnector, connectedModelItem))
2121
                                targetConnector = _LMConnector;
2122
                            else
2123
                                ReleaseCOMObjects(_LMConnector);
2124
                        }
2125
                    }
2126

    
2127
                    ReleaseCOMObjects(targetModelItem);
2128
                }
2129
            }
2130
            else
2131
            {
2132
                LMSymbol connectedLMSymbol = null;
2133
                if (connectedSymbol != null)
2134
                    connectedLMSymbol = dataSource.GetSymbol(connectedSymbol.SPPID.RepresentationId);
2135
                else if (targetSymbol != null)
2136
                    connectedLMSymbol = dataSource.GetSymbol(targetSymbol.SPPID.RepresentationId);
2137
                else
2138
                {
2139

    
2140
                }
2141
                LMModelItem targetModelItem = null;
2142
                if (targetLine != null)
2143
                    targetModelItem = dataSource.GetModelItem(targetLine.SPPID.ModelItemId);
2144
                else if (connectedLine != null)
2145
                    targetModelItem = dataSource.GetModelItem(connectedLine.SPPID.ModelItemId);
2146
                else
2147
                {
2148

    
2149
                }
2150
                if (connectedLMSymbol != null && targetModelItem != null)
2151
                {
2152
                    foreach (LMConnector connector in connectedLMSymbol.Avoid1Connectors)
2153
                    {
2154
                        if (connector.get_ItemStatus() != "Active")
2155
                            continue;
2156

    
2157
                        if (IsConnected(connector, targetModelItem))
2158
                        {
2159
                            targetConnector = connector;
2160
                            break;
2161
                        }
2162
                    }
2163

    
2164
                    if (targetConnector == null)
2165
                    {
2166
                        foreach (LMConnector connector in connectedLMSymbol.Avoid2Connectors)
2167
                        {
2168
                            if (connector.get_ItemStatus() != "Active")
2169
                                continue;
2170

    
2171
                            if (IsConnected(connector, targetModelItem))
2172
                            {
2173
                                targetConnector = connector;
2174
                                break;
2175
                            }
2176
                        }
2177
                    }
2178
                }
2179

    
2180
            }
2181

    
2182
            return targetConnector;
2183
        }
2184

    
2185
        private bool IsConnected(LMConnector connector, LMModelItem modelItem)
2186
        {
2187
            bool result = false;
2188

    
2189
            foreach (LMRepresentation rep in modelItem.Representations)
2190
            {
2191
                if (result)
2192
                    break;
2193

    
2194
                if (rep.Attributes["RepresentationType"].get_Value() == "Connector" && rep.Attributes["ItemStatus"].get_Value() == "Active")
2195
                {
2196
                    LMConnector _LMConnector = dataSource.GetConnector(rep.Id);
2197

    
2198
                    if (_LMConnector.ConnectItem1SymbolObject != null &&
2199
                        connector.ConnectItem1SymbolObject != null &&
2200
                        _LMConnector.ConnectItem1SymbolObject.Id == connector.ConnectItem1SymbolObject.Id)
2201
                    {
2202
                        result = true;
2203
                        ReleaseCOMObjects(_LMConnector);
2204
                        break;
2205
                    }
2206
                    else if (_LMConnector.ConnectItem1SymbolObject != null &&
2207
                        connector.ConnectItem2SymbolObject != null &&
2208
                        _LMConnector.ConnectItem1SymbolObject.Id == connector.ConnectItem2SymbolObject.Id)
2209
                    {
2210
                        result = true;
2211
                        ReleaseCOMObjects(_LMConnector);
2212
                        break;
2213
                    }
2214
                    else if (_LMConnector.ConnectItem2SymbolObject != null &&
2215
                        connector.ConnectItem1SymbolObject != null &&
2216
                        _LMConnector.ConnectItem2SymbolObject.Id == connector.ConnectItem1SymbolObject.Id)
2217
                    {
2218
                        result = true;
2219
                        ReleaseCOMObjects(_LMConnector);
2220
                        break;
2221
                    }
2222
                    else if (_LMConnector.ConnectItem2SymbolObject != null &&
2223
                        connector.ConnectItem2SymbolObject != null &&
2224
                        _LMConnector.ConnectItem2SymbolObject.Id == connector.ConnectItem2SymbolObject.Id)
2225
                    {
2226
                        result = true;
2227
                        ReleaseCOMObjects(_LMConnector);
2228
                        break;
2229
                    }
2230

    
2231
                    ReleaseCOMObjects(_LMConnector);
2232
                }
2233
            }
2234

    
2235

    
2236
            return result;
2237
        }
2238

    
2239
        /// <summary>
2240
        /// FromModelItem을 ToModelItem으로 PipeRunJoin하는 메서드
2241
        /// </summary>
2242
        /// <param name="fromModelItemId"></param>
2243
        /// <param name="toModelItemId"></param>
2244
        private void JoinPipeRun(string fromModelItemId, string toModelItemId)
2245
        {
2246
            LMModelItem modelItem1 = dataSource.GetModelItem(toModelItemId);
2247
            _LMAItem item1 = modelItem1.AsLMAItem();
2248
            LMModelItem modelItem2 = dataSource.GetModelItem(fromModelItemId);
2249
            _LMAItem item2 = modelItem2.AsLMAItem();
2250

    
2251
            // item2가 item1으로 조인
2252
            _placement.PIDJoinRuns(ref item1, ref item2);
2253
            item1.Commit();
2254
            item2.Commit();
2255

    
2256
            List<Line> lines = SPPIDUtil.FindLinesByModelId(document, fromModelItemId);
2257
            foreach (var line in lines)
2258
                line.SPPID.ModelItemId = toModelItemId;
2259

    
2260
            ReleaseCOMObjects(modelItem1);
2261
            ReleaseCOMObjects(item1);
2262
            ReleaseCOMObjects(modelItem2);
2263
            ReleaseCOMObjects(item2);
2264
        }
2265

    
2266
        /// <summary>
2267
        /// PipeRun을 자동으로 Join하는 메서드
2268
        /// </summary>
2269
        /// <param name="modelItemId"></param>
2270
        private void AutoJoinPipeRun(string modelItemId)
2271
        {
2272
            LMModelItem modelItem = dataSource.GetModelItem(modelItemId);
2273
            _LMAItem item = modelItem.AsLMAItem();
2274
            if (modelItem.get_ItemStatus() == "Active")
2275
            {
2276
                string modelitemID = item.Id;
2277
                _placement.PIDAutoJoin(item, AutoJoinEndConstants.autoJoin_Both, ref item);
2278
                string afterModelItemID = item.Id;
2279

    
2280
                if (modelitemID != afterModelItemID)
2281
                {
2282
                    List<Line> lines = SPPIDUtil.FindLinesByModelId(document, modelitemID);
2283
                    foreach (var line in lines)
2284
                        line.SPPID.ModelItemId = afterModelItemID;
2285
                }
2286
                item.Commit();
2287
            }
2288

    
2289
            ReleaseCOMObjects(modelItem);
2290
            ReleaseCOMObjects(item);
2291
        }
2292

    
2293
        /// <summary>
2294
        /// LineRun에 있는 Line들을 Join하는 진입 메서드
2295
        /// </summary>
2296
        /// <param name="run"></param>
2297
        private void JoinRunLine(LineRun run)
2298
        {
2299
            string modelItemId = string.Empty;
2300
            foreach (var item in run.RUNITEMS)
2301
            {
2302
                if (item.GetType() == typeof(Line))
2303
                {
2304
                    Line line = item as Line;
2305
                    AutoJoinPipeRun(line.SPPID.ModelItemId);
2306
                    modelItemId = line.SPPID.ModelItemId;
2307
                }
2308
            }
2309
        }
2310

    
2311
        /// <summary>
2312
        /// PipeRun의 좌표를 가져오는 메서드
2313
        /// </summary>
2314
        /// <param name="modelId"></param>
2315
        /// <returns></returns>
2316
        private Dictionary<LMConnector, List<double[]>> GetPipeRunVertices(string modelId)
2317
        {
2318
            Dictionary<LMConnector, List<double[]>> connectorVertices = new Dictionary<LMConnector, List<double[]>>();
2319
            LMModelItem modelItem = dataSource.GetModelItem(modelId);
2320

    
2321
            if (modelItem != null)
2322
            {
2323
                foreach (LMRepresentation rep in modelItem.Representations)
2324
                {
2325
                    if (rep.Attributes["RepresentationType"].get_Value() == "Connector" && rep.Attributes["ItemStatus"].get_Value() == "Active")
2326
                    {
2327
                        LMConnector _LMConnector = dataSource.GetConnector(rep.Id);
2328
                        connectorVertices.Add(_LMConnector, new List<double[]>());
2329
                        dynamic OID = rep.get_GraphicOID();
2330
                        DependencyObject drawingObject = radApp.ActiveDocument.ActiveSheet.DrawingObjects[OID];
2331
                        Ingr.RAD2D.LineStringGeometry2d lineStringGeometry = drawingObject.GetGeometry() as Ingr.RAD2D.LineStringGeometry2d;
2332
                        int verticesCount = lineStringGeometry.VertexCount;
2333
                        double[] vertices = null;
2334
                        lineStringGeometry.GetVertices(ref verticesCount, ref vertices);
2335
                        for (int i = 0; i < verticesCount; i++)
2336
                        {
2337
                            double x = 0;
2338
                            double y = 0;
2339
                            lineStringGeometry.GetVertex(i + 1, ref x, ref y);
2340
                            connectorVertices[_LMConnector].Add(new double[] { Math.Round(x, 10), Math.Round(y, 10) });
2341
                        }
2342
                    }
2343
                }
2344

    
2345
                ReleaseCOMObjects(modelItem);
2346
            }
2347

    
2348
            return connectorVertices;
2349
        }
2350

    
2351
        /// <summary>
2352
        /// 좌표로 PipeRun의 Connector중에 어느 Connector에 가까운지/붙을지 가져오는 메서드 - 조건에 안맞아서 못찾을시 제일 가까운 점으로 가져오는 방식
2353
        /// </summary>
2354
        /// <param name="connectorVertices"></param>
2355
        /// <param name="connX"></param>
2356
        /// <param name="connY"></param>
2357
        /// <returns></returns>
2358
        private LMConnector FindTargetLMConnectorForLabel(Dictionary<LMConnector, List<double[]>> connectorVertices, double connX, double connY)
2359
        {
2360
            double length = double.MaxValue;
2361
            LMConnector targetConnector = null;
2362
            foreach (var item in connectorVertices)
2363
            {
2364
                List<double[]> points = item.Value;
2365
                for (int i = 0; i < points.Count - 1; i++)
2366
                {
2367
                    double[] point1 = points[i];
2368
                    double[] point2 = points[i + 1];
2369
                    double x1 = Math.Min(point1[0], point2[0]);
2370
                    double y1 = Math.Min(point1[1], point2[1]);
2371
                    double x2 = Math.Max(point1[0], point2[0]);
2372
                    double y2 = Math.Max(point1[1], point2[1]);
2373

    
2374
                    if ((x1 <= connX && x2 >= connX) ||
2375
                        (y1 <= connY && y2 >= connY))
2376
                    {
2377
                        double distance = SPPIDUtil.CalcPointToPointdDistance(point1[0], point1[1], connX, connY);
2378
                        if (length >= distance)
2379
                        {
2380
                            targetConnector = item.Key;
2381
                            length = distance;
2382
                        }
2383

    
2384
                        distance = SPPIDUtil.CalcPointToPointdDistance(point2[0], point2[1], connX, connY);
2385
                        if (length >= distance)
2386
                        {
2387
                            targetConnector = item.Key;
2388
                            length = distance;
2389
                        }
2390
                    }
2391
                }
2392
            }
2393

    
2394
            // 못찾았을때.
2395
            length = double.MaxValue;
2396
            if (targetConnector == null)
2397
            {
2398
                foreach (var item in connectorVertices)
2399
                {
2400
                    List<double[]> points = item.Value;
2401

    
2402
                    foreach (double[] point in points)
2403
                    {
2404
                        double distance = SPPIDUtil.CalcPointToPointdDistance(point[0], point[1], connX, connY);
2405
                        if (length >= distance)
2406
                        {
2407
                            targetConnector = item.Key;
2408
                            length = distance;
2409
                        }
2410
                    }
2411
                }
2412
            }
2413

    
2414
            return targetConnector;
2415
        }
2416

    
2417
        private LMConnector FindTargetLMConnectorForBranch(Line line, Line targetLine, ref double x, ref double y)
2418
        {
2419
            Dictionary<LMConnector, List<double[]>> vertices = GetPipeRunVertices(targetLine.SPPID.ModelItemId);
2420

    
2421
            double length = double.MaxValue;
2422
            LMConnector targetConnector = null;
2423
            double[] resultPoint = null;
2424
            List<double[]> targetVertices = null;
2425

    
2426
            // Vertices 포인트에 제일 가까운곳
2427
            foreach (var item in vertices)
2428
            {
2429
                List<double[]> points = item.Value;
2430
                for (int i = 0; i < points.Count; i++)
2431
                {
2432
                    double[] point = points[i];
2433
                    double tempX = point[0];
2434
                    double tempY = point[1];
2435

    
2436
                    double distance = SPPIDUtil.CalcPointToPointdDistance(tempX, tempY, x, y);
2437
                    if (length >= distance)
2438
                    {
2439
                        targetConnector = item.Key;
2440
                        length = distance;
2441
                        resultPoint = point;
2442
                        targetVertices = item.Value;
2443
                    }
2444
                }
2445
            }
2446

    
2447
            // Vertices Cross에 제일 가까운곳
2448
            foreach (var item in vertices)
2449
            {
2450
                List<double[]> points = item.Value;
2451
                for (int i = 0; i < points.Count - 1; i++)
2452
                {
2453
                    double[] point1 = points[i];
2454
                    double[] point2 = points[i + 1];
2455

    
2456
                    double maxLineX = Math.Max(point1[0], point2[0]);
2457
                    double minLineX = Math.Min(point1[0], point2[0]);
2458
                    double maxLineY = Math.Max(point1[1], point2[1]);
2459
                    double minLineY = Math.Min(point1[1], point2[1]);
2460

    
2461
                    SlopeType slope = SPPIDUtil.CalcSlope(minLineX, minLineY, maxLineX, maxLineY);
2462

    
2463
                    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]);
2464
                    if (crossingPoint != null)
2465
                    {
2466
                        double distance = SPPIDUtil.CalcPointToPointdDistance(crossingPoint[0], crossingPoint[1], x, y);
2467
                        if (length >= distance)
2468
                        {
2469
                            if (slope == SlopeType.Slope &&
2470
                                minLineX <= crossingPoint[0] && maxLineX >= crossingPoint[0] &&
2471
                                minLineY <= crossingPoint[1] && maxLineY >= crossingPoint[1])
2472
                            {
2473
                                targetConnector = item.Key;
2474
                                length = distance;
2475
                                resultPoint = crossingPoint;
2476
                                targetVertices = item.Value;
2477
                            }
2478
                            else if (slope == SlopeType.HORIZONTAL &&
2479
                                minLineX <= crossingPoint[0] && maxLineX >= crossingPoint[0])
2480
                            {
2481
                                targetConnector = item.Key;
2482
                                length = distance;
2483
                                resultPoint = crossingPoint;
2484
                                targetVertices = item.Value;
2485
                            }
2486
                            else if (slope == SlopeType.VERTICAL &&
2487
                               minLineY <= crossingPoint[1] && maxLineY >= crossingPoint[1])
2488
                            {
2489
                                targetConnector = item.Key;
2490
                                length = distance;
2491
                                resultPoint = crossingPoint;
2492
                                targetVertices = item.Value;
2493
                            }
2494
                        }
2495
                    }
2496
                }
2497
            }
2498

    
2499
            foreach (var item in vertices)
2500
                if (item.Key != null && item.Key != targetConnector)
2501
                    ReleaseCOMObjects(item.Key);
2502

    
2503
            if (SPPIDUtil.IsBranchLine(line, targetLine))
2504
            {
2505
                double tempResultX = resultPoint[0];
2506
                double tempResultY = resultPoint[1];
2507
                SPPIDUtil.ConvertGridPoint(ref tempResultX, ref tempResultY);
2508

    
2509
                GridSetting gridSetting = GridSetting.GetInstance();
2510

    
2511
                for (int i = 0; i < targetVertices.Count; i++)
2512
                {
2513
                    double[] point = targetVertices[i];
2514
                    double tempX = targetVertices[i][0];
2515
                    double tempY = targetVertices[i][1];
2516
                    SPPIDUtil.ConvertGridPoint(ref tempX, ref tempY);
2517
                    if (tempX == tempResultX && tempY == tempResultY)
2518
                    {
2519
                        if (i == 0)
2520
                        {
2521
                            LMSymbol connSymbol = targetConnector.ConnectItem1SymbolObject;
2522
                            if (connSymbol == null ||
2523
                                (connSymbol != null && connSymbol.get_ItemStatus() == "Active"))
2524
                            {
2525
                                bool containZeroLength = false;
2526
                                foreach (LMConnector connector in connSymbol.Connect1Connectors)
2527
                                {
2528
                                    if (connector.get_ItemStatus() == "Active" && connector.get_IsZeroLength() == true)
2529
                                        containZeroLength = true;
2530
                                }
2531
                                foreach (LMConnector connector in connSymbol.Connect2Connectors)
2532
                                {
2533
                                    if (connector.get_ItemStatus() == "Active" && connector.get_IsZeroLength() == true)
2534
                                        containZeroLength = true;
2535
                                }
2536

    
2537
                                if (connSymbol.get_RepresentationType() != "Branch" ||
2538
                                    containZeroLength)
2539
                                {
2540
                                    bool bCalcX = false;
2541
                                    bool bCalcY = false;
2542
                                    if (targetLine.SlopeType == SlopeType.HORIZONTAL)
2543
                                        bCalcX = true;
2544
                                    else if (targetLine.SlopeType == SlopeType.VERTICAL)
2545
                                        bCalcY = true;
2546
                                    else
2547
                                    {
2548
                                        bCalcX = true;
2549
                                        bCalcY = true;
2550
                                    }
2551

    
2552
                                    if (bCalcX)
2553
                                    {
2554
                                        double nextX = targetVertices[i + 1][0];
2555
                                        double newX = 0;
2556
                                        if (nextX > tempX)
2557
                                        {
2558
                                            newX = tempX + gridSetting.Length;
2559
                                            if (newX > nextX)
2560
                                                newX = (point[0] + nextX) / 2;
2561
                                        }
2562
                                        else
2563
                                        {
2564
                                            newX = tempX - gridSetting.Length;
2565
                                            if (newX < nextX)
2566
                                                newX = (point[0] + nextX) / 2;
2567
                                        }
2568
                                        resultPoint = new double[] { newX, resultPoint[1] };
2569
                                    }
2570

    
2571
                                    if (bCalcY)
2572
                                    {
2573
                                        double nextY = targetVertices[i + 1][1];
2574
                                        double newY = 0;
2575
                                        if (nextY > tempY)
2576
                                        {
2577
                                            newY = tempY + gridSetting.Length;
2578
                                            if (newY > nextY)
2579
                                                newY = (point[1] + nextY) / 2;
2580
                                        }
2581
                                        else
2582
                                        {
2583
                                            newY = tempY - gridSetting.Length;
2584
                                            if (newY < nextY)
2585
                                                newY = (point[1] + nextY) / 2;
2586
                                        }
2587
                                        resultPoint = new double[] { resultPoint[0], newY };
2588
                                    }
2589
                                }
2590
                            }
2591
                        }
2592
                        else if (i == targetVertices.Count - 1)
2593
                        {
2594
                            LMSymbol connSymbol = targetConnector.ConnectItem2SymbolObject;
2595
                            if (connSymbol == null ||
2596
                                (connSymbol != null && connSymbol.get_ItemStatus() == "Active"))
2597
                            {
2598
                                bool containZeroLength = false;
2599
                                foreach (LMConnector connector in connSymbol.Connect1Connectors)
2600
                                {
2601
                                    if (connector.get_ItemStatus() == "Active" && connector.get_IsZeroLength() == true)
2602
                                        containZeroLength = true;
2603
                                }
2604
                                foreach (LMConnector connector in connSymbol.Connect2Connectors)
2605
                                {
2606
                                    if (connector.get_ItemStatus() == "Active" && connector.get_IsZeroLength() == true)
2607
                                        containZeroLength = true;
2608
                                }
2609

    
2610
                                if (connSymbol.get_RepresentationType() != "Branch" ||
2611
                                    containZeroLength)
2612
                                {
2613
                                    bool bCalcX = false;
2614
                                    bool bCalcY = false;
2615
                                    if (targetLine.SlopeType == SlopeType.HORIZONTAL)
2616
                                        bCalcX = true;
2617
                                    else if (targetLine.SlopeType == SlopeType.VERTICAL)
2618
                                        bCalcY = true;
2619
                                    else
2620
                                    {
2621
                                        bCalcX = true;
2622
                                        bCalcY = true;
2623
                                    }
2624

    
2625
                                    if (bCalcX)
2626
                                    {
2627
                                        double nextX = targetVertices[i - 1][0];
2628
                                        double newX = 0;
2629
                                        if (nextX > tempX)
2630
                                        {
2631
                                            newX = tempX + gridSetting.Length;
2632
                                            if (newX > nextX)
2633
                                                newX = (point[0] + nextX) / 2;
2634
                                        }
2635
                                        else
2636
                                        {
2637
                                            newX = tempX - gridSetting.Length;
2638
                                            if (newX < nextX)
2639
                                                newX = (point[0] + nextX) / 2;
2640
                                        }
2641
                                        resultPoint = new double[] { newX, resultPoint[1] };
2642
                                    }
2643

    
2644
                                    if (bCalcY)
2645
                                    {
2646
                                        double nextY = targetVertices[i - 1][1];
2647
                                        double newY = 0;
2648
                                        if (nextY > tempY)
2649
                                        {
2650
                                            newY = tempY + gridSetting.Length;
2651
                                            if (newY > nextY)
2652
                                                newY = (point[1] + nextY) / 2;
2653
                                        }
2654
                                        else
2655
                                        {
2656
                                            newY = tempY - gridSetting.Length;
2657
                                            if (newY < nextY)
2658
                                                newY = (point[1] + nextY) / 2;
2659
                                        }
2660
                                        resultPoint = new double[] { resultPoint[0], newY };
2661
                                    }
2662
                                }
2663
                            }
2664
                        }
2665
                        break;
2666
                    }
2667
                }
2668
            }
2669

    
2670
            x = resultPoint[0];
2671
            y = resultPoint[1];
2672

    
2673
            return targetConnector;
2674
        }
2675

    
2676
        /// <summary>
2677
        /// Line Number Symbol을 실제로 Modeling하는 메서드
2678
        /// </summary>
2679
        /// <param name="lineNumber"></param>
2680
        private void LineNumberModeling(LineNumber lineNumber)
2681
        {
2682
            object obj = SPPIDUtil.FindObjectByUID(document, lineNumber.CONNLINE);
2683
            if (obj != null)
2684
            {
2685
                Line line = obj as Line;
2686
                Dictionary<LMConnector, List<double[]>> connectorVertices = GetPipeRunVertices(line.SPPID.ModelItemId);
2687
                LMConnector connectedLMConnector = FindTargetLMConnectorForLabel(connectorVertices, lineNumber.SPPID.ORIGINAL_X, lineNumber.SPPID.ORIGINAL_Y);
2688
                if (connectedLMConnector != null)
2689
                {
2690
                    double x = 0;
2691
                    double y = 0;
2692
                    CalcLabelLocation(ref x, ref y, lineNumber.SPPID.ORIGINAL_X, lineNumber.SPPID.ORIGINAL_Y, lineNumber.SPPIDLabelLocation, _ETCSetting.LineNumberLocation);
2693

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

    
2697
                    foreach (var item in connectorVertices)
2698
                        ReleaseCOMObjects(item.Key);
2699
                    if (_LmLabelPresist != null)
2700
                    {
2701
                        _LmLabelPresist.Commit();
2702
                        lineNumber.SPPID.RepresentationId = _LmLabelPresist.AsLMRepresentation().Id;
2703
                        ReleaseCOMObjects(_LmLabelPresist);
2704
                    }
2705
                    else
2706
                    {
2707

    
2708
                    }
2709
                }
2710
            }
2711
        }
2712

    
2713
        /// <summary>
2714
        /// Flow Mark Modeling
2715
        /// </summary>
2716
        /// <param name="line"></param>
2717
        private void FlowMarkModeling(Line line)
2718
        {
2719
            if (line.FLOWMARK && !string.IsNullOrEmpty(line.SPPID.ModelItemId) && !string.IsNullOrEmpty(_ETCSetting.FlowMarkSymbolPath))
2720
            {
2721
                SlopeType targetSlopeType = SPPIDUtil.CalcSlope(line.SPPID.START_X, line.SPPID.START_Y, line.SPPID.END_X, line.SPPID.END_Y);
2722
                string mappingPath = _ETCSetting.FlowMarkSymbolPath;
2723
                double percent = line.FLOWMARK_PERCENT;
2724
                double tempX = 0;
2725
                double tempY = 0;
2726

    
2727
                double gapX;
2728
                double gapY;
2729
                // ID2 기준의 Gap을 구함
2730
                if (percent == 0)
2731
                {
2732
                    gapX = 0;
2733
                    gapY = 0;
2734
                }
2735
                else
2736
                {
2737
                    gapX = Math.Abs(line.SPPID.START_X - line.SPPID.END_X) / 100 * percent;
2738
                    gapY = Math.Abs(line.SPPID.START_Y - line.SPPID.END_Y) / 100 * percent;
2739
                }
2740

    
2741
                if (line.SPPID.START_X < line.SPPID.END_X)
2742
                    tempX = line.SPPID.START_X + gapX;
2743
                else
2744
                    tempX = line.SPPID.START_X - gapX;
2745

    
2746
                if (line.SPPID.START_Y < line.SPPID.END_Y)
2747
                    tempY = line.SPPID.START_Y + gapY;
2748
                else
2749
                    tempY = line.SPPID.START_Y - gapY;
2750

    
2751

    
2752

    
2753
                Dictionary<LMConnector, List<double[]>> connectorVertices = GetPipeRunVertices(line.SPPID.ModelItemId);
2754
                LMConnector _TargetItem = null;
2755
                double distance = double.MaxValue;
2756
                double[] startPoint = null;
2757
                double[] endPoint = null;
2758
                // ID2의 기준 Gap으로 제일 가까운 Line 찾음(Slope도 같은조건)
2759
                foreach (var item in connectorVertices)
2760
                {
2761
                    for (int i = 0; i < item.Value.Count - 1; i++)
2762
                    {
2763
                        List<double[]> points = item.Value;
2764
                        double[] point1 = points[i];
2765
                        double[] point2 = points[i + 1];
2766

    
2767
                        SlopeType slopeType = SPPIDUtil.CalcSlope(point1[0], point1[1], point2[0], point2[1]);
2768
                        if (slopeType == targetSlopeType)
2769
                        {
2770
                            double result = SPPIDUtil.CalcPointToPointdDistance(point1[0], point1[1], tempX, tempY);
2771
                            if (result < distance)
2772
                            {
2773
                                distance = result;
2774
                                _TargetItem = item.Key;
2775

    
2776
                                startPoint = point1;
2777
                                endPoint = point2;
2778
                            }
2779

    
2780
                            result = SPPIDUtil.CalcPointToPointdDistance(point2[0], point2[1], tempX, tempY);
2781
                            if (result < distance)
2782
                            {
2783
                                distance = result;
2784
                                _TargetItem = item.Key;
2785

    
2786
                                startPoint = point1;
2787
                                endPoint = point2;
2788
                            }
2789
                        }
2790
                    }
2791
                }
2792

    
2793
                if (_TargetItem != null)
2794
                {
2795
                    double x = 0;
2796
                    double y = 0;
2797
                    double angle = 0;
2798
                    // SPPID 기준의 Gap으로 실 좌표를 구함
2799
                    if (percent == 0)
2800
                    {
2801
                        gapX = 0;
2802
                        gapY = 0;
2803
                    }
2804
                    else
2805
                    {
2806
                        gapX = Math.Abs(startPoint[0] - endPoint[0]) / 100 * percent;
2807
                        gapY = Math.Abs(startPoint[1] - endPoint[1]) / 100 * percent;
2808
                    }
2809

    
2810
                    if (startPoint[0] < endPoint[0])
2811
                        x = startPoint[0] + gapX;
2812
                    else
2813
                        x = startPoint[0] - gapX;
2814

    
2815
                    if (startPoint[1] < endPoint[1])
2816
                        y = startPoint[1] + gapY;
2817
                    else
2818
                        y = startPoint[1] - gapY;
2819

    
2820
                    if (targetSlopeType == SlopeType.HORIZONTAL)
2821
                    {
2822
                        if (startPoint[0] < endPoint[0])
2823
                            angle = 0;
2824
                        else
2825
                            angle = Math.PI;
2826
                    }
2827
                    // 90 270
2828
                    else if (targetSlopeType == SlopeType.VERTICAL)
2829
                    {
2830
                        if (startPoint[1] < endPoint[1])
2831
                            angle = 90 * Math.PI / 180;
2832
                        else
2833
                            angle = 270 * Math.PI / 180;
2834
                    }
2835

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

    
2838
                    if (_LMSymbol != null)
2839
                    {
2840
                        ReleaseCOMObjects(_LMSymbol);
2841
                    }
2842

    
2843
                }
2844

    
2845
                foreach (var item in connectorVertices)
2846
                    ReleaseCOMObjects(item.Key);
2847
            }
2848
        }
2849

    
2850
        /// <summary>
2851
        /// Line Number 기준으로 모든 Item에 Line Number의 Attribute Input
2852
        /// </summary>
2853
        /// <param name="lineNumber"></param>
2854
        private void InputLineNumberAttribute(LineNumber lineNumber)
2855
        {
2856
            foreach (LineRun run in lineNumber.RUNS)
2857
            {
2858
                foreach (var item in run.RUNITEMS)
2859
                {
2860
                    if (item.GetType() == typeof(Symbol))
2861
                    {
2862
                        Symbol symbol = item as Symbol;
2863
                        LMSymbol _LMSymbol = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
2864
                        if (_LMSymbol != null)
2865
                        {
2866
                            LMModelItem _LMModelItem = _LMSymbol.ModelItemObject;
2867

    
2868
                            if (_LMModelItem != null && _LMModelItem.get_ItemStatus() == "Active")
2869
                            {
2870
                                foreach (var attribute in lineNumber.ATTRIBUTES)
2871
                                {
2872
                                    LineNumberMapping mapping = document.LineNumberMappings.Find(x => x.UID == attribute.UID);
2873
                                    if (mapping != null && !string.IsNullOrEmpty(attribute.VALUE) && attribute.VALUE != "None")
2874
                                    {
2875
                                        LMAAttribute _LMAAttribute = _LMModelItem.Attributes[mapping.SPPIDATTRIBUTENAME];
2876
                                        if (_LMAAttribute != null)
2877
                                        {
2878
                                            if (DBNull.Value.Equals(_LMAAttribute.get_Value()))
2879
                                                _LMAAttribute.set_Value(attribute.VALUE);
2880
                                            else if (_LMAAttribute.get_Value() != attribute.VALUE)
2881
                                                _LMAAttribute.set_Value(attribute.VALUE);
2882
                                        }
2883
                                    }
2884
                                }
2885
                                _LMModelItem.Commit();
2886
                            }
2887
                            if (_LMModelItem != null)
2888
                                ReleaseCOMObjects(_LMModelItem);
2889
                        }
2890
                        if (_LMSymbol != null)
2891
                            ReleaseCOMObjects(_LMSymbol);
2892
                    }
2893
                    else if (item.GetType() == typeof(Line))
2894
                    {
2895
                        Line line = item as Line;
2896
                        if (line != null)
2897
                        {
2898
                            LMModelItem _LMModelItem = dataSource.GetModelItem(line.SPPID.ModelItemId);
2899
                            if (_LMModelItem != null && _LMModelItem.get_ItemStatus() == "Active")
2900
                            {
2901
                                foreach (var attribute in lineNumber.ATTRIBUTES)
2902
                                {
2903
                                    LineNumberMapping mapping = document.LineNumberMappings.Find(x => x.UID == attribute.UID);
2904
                                    if (mapping != null && !string.IsNullOrEmpty(attribute.VALUE) && attribute.VALUE != "None")
2905
                                    {
2906
                                        LMAAttribute _LMAAttribute = _LMModelItem.Attributes[mapping.SPPIDATTRIBUTENAME];
2907
                                        if (_LMAAttribute != null)
2908
                                        {
2909
                                            if (DBNull.Value.Equals(_LMAAttribute.get_Value()))
2910
                                                _LMAAttribute.set_Value(attribute.VALUE);
2911
                                            else if (_LMAAttribute.get_Value() != attribute.VALUE)
2912
                                                _LMAAttribute.set_Value(attribute.VALUE);
2913

    
2914
                                        }
2915
                                    }
2916
                                }
2917
                                _LMModelItem.Commit();
2918
                            }
2919
                            if (_LMModelItem != null)
2920
                                ReleaseCOMObjects(_LMModelItem);
2921
                        }
2922
                    }
2923
                }
2924
            }
2925
        }
2926

    
2927
        /// <summary>
2928
        /// Symbol Attribute 입력 메서드
2929
        /// </summary>
2930
        /// <param name="item"></param>
2931
        private void InputSymbolAttribute(object targetItem, List<BaseModel.Attribute> targetAttributes)
2932
        {
2933

    
2934
            // Object 아이템이 Symbol일 경우 Equipment일 경우 
2935
            string sRep = null;
2936
            if (targetItem.GetType() == typeof(Symbol))
2937
                sRep = ((Symbol)targetItem).SPPID.RepresentationId;
2938
            else if (targetItem.GetType() == typeof(Equipment))
2939
                sRep = ((Equipment)targetItem).SPPID.RepresentationId;
2940

    
2941
            if (!string.IsNullOrEmpty(sRep))
2942
            {
2943
                LMSymbol _LMSymbol = dataSource.GetSymbol(sRep);
2944
                LMModelItem _LMModelItem = _LMSymbol.ModelItemObject;
2945
                LMAAttributes _Attributes = _LMModelItem.Attributes;
2946

    
2947
                foreach (var item in targetAttributes)
2948
                {
2949
                    AttributeMapping mapping = document.AttributeMappings.Find(x => x.UID == item.UID);
2950
                    if (mapping != null && !string.IsNullOrEmpty(item.VALUE) && item.VALUE != "None")
2951
                    {
2952
                        LMAAttribute _Attribute = _Attributes[mapping.SPPIDATTRIBUTENAME];
2953
                        if (_Attribute != null)
2954
                            _Attribute.set_Value(item.VALUE);
2955
                    }
2956
                }
2957
                _LMModelItem.Commit();
2958

    
2959
                ReleaseCOMObjects(_Attributes);
2960
                ReleaseCOMObjects(_LMModelItem);
2961
                ReleaseCOMObjects(_LMSymbol);
2962
            }
2963
        }
2964

    
2965
        /// <summary>
2966
        /// Input SpecBreak Attribute
2967
        /// </summary>
2968
        /// <param name="specBreak"></param>
2969
        private void InputSpecBreakAttribute(SpecBreak specBreak)
2970
        {
2971
            object upStreamObj = SPPIDUtil.FindObjectByUID(document, specBreak.UpStreamUID);
2972
            object downStreamObj = SPPIDUtil.FindObjectByUID(document, specBreak.DownStreamUID);
2973

    
2974
            if (upStreamObj != null &&
2975
                downStreamObj != null)
2976
            {
2977
                LMConnector targetLMConnector = FindBreakLineTarget(upStreamObj, downStreamObj);
2978

    
2979
                if (targetLMConnector != null)
2980
                {
2981
                    foreach (LMLabelPersist _LMLabelPersist in targetLMConnector.LabelPersists)
2982
                    {
2983
                        string symbolPath = _LMLabelPersist.get_FileName();
2984
                        AttributeMapping mapping = document.AttributeMappings.Find(x => x.SPPIDSYMBOLNAME == symbolPath);
2985
                        if (mapping != null)
2986
                        {
2987
                            BaseModel.Attribute attribute = specBreak.ATTRIBUTES.Find(y => y.UID == mapping.UID);
2988
                            if (attribute != null && !string.IsNullOrEmpty(attribute.VALUE) && attribute.VALUE != "None")
2989
                            {
2990
                                string[] values = attribute.VALUE.Split(new char[] { ',' });
2991
                                if (values.Length == 2)
2992
                                {
2993
                                    string upStreamValue = values[0];
2994
                                    string downStreamValue = values[1];
2995

    
2996
                                    InputAttributeForSpecBreak(upStreamObj, downStreamObj, upStreamValue, downStreamValue, mapping.SPPIDATTRIBUTENAME);
2997
                                }
2998
                            }
2999
                        }
3000
                    }
3001

    
3002
                    ReleaseCOMObjects(targetLMConnector);
3003
                }
3004
            }
3005

    
3006

    
3007
            #region 내부에서만 쓰는 메서드
3008
            void InputAttributeForSpecBreak(object _upStreamObj, object _downStreamObj, string upStreamValue, string downStreamValue, string sppidAttributeName)
3009
            {
3010
                Symbol upStreamSymbol = _upStreamObj as Symbol;
3011
                Line upStreamLine = _upStreamObj as Line;
3012
                Symbol downStreamSymbol = _downStreamObj as Symbol;
3013
                Line downStreamLine = _downStreamObj as Line;
3014
                // 둘다 Line일 경우
3015
                if (upStreamLine != null && downStreamLine != null)
3016
                {
3017
                    InputLineAttributeForSpecBreakLine(upStreamLine, sppidAttributeName, upStreamValue);
3018
                    InputLineAttributeForSpecBreakLine(downStreamLine, sppidAttributeName, downStreamValue);
3019
                }
3020
                // 둘다 Symbol일 경우
3021
                else if (upStreamSymbol != null && downStreamSymbol != null)
3022
                {
3023
                    LMConnector zeroLenthConnector = FindBreakLineTarget(upStreamSymbol, downStreamSymbol);
3024
                    LMSymbol upStreamLMSymbol = dataSource.GetSymbol(upStreamSymbol.SPPID.RepresentationId);
3025
                    LMSymbol downStreamLMSymbol = dataSource.GetSymbol(downStreamSymbol.SPPID.RepresentationId);
3026

    
3027
                    foreach (LMConnector connector in upStreamLMSymbol.Avoid1Connectors)
3028
                    {
3029
                        if (connector.get_ItemStatus() != "Active")
3030
                            continue;
3031

    
3032
                        if (connector.Id != zeroLenthConnector.Id)
3033
                            InputLineAttributeForSpecBreakLMConnector(connector, sppidAttributeName, upStreamValue);
3034
                    }
3035

    
3036
                    foreach (LMConnector connector in upStreamLMSymbol.Avoid2Connectors)
3037
                    {
3038
                        if (connector.get_ItemStatus() != "Active")
3039
                            continue;
3040

    
3041
                        if (connector.Id != zeroLenthConnector.Id)
3042
                            InputLineAttributeForSpecBreakLMConnector(connector, sppidAttributeName, upStreamValue);
3043
                    }
3044

    
3045
                    foreach (LMConnector connector in downStreamLMSymbol.Avoid1Connectors)
3046
                    {
3047
                        if (connector.get_ItemStatus() != "Active")
3048
                            continue;
3049

    
3050
                        if (connector.Id != zeroLenthConnector.Id)
3051
                            InputLineAttributeForSpecBreakLMConnector(connector, sppidAttributeName, downStreamValue);
3052
                    }
3053

    
3054
                    foreach (LMConnector connector in downStreamLMSymbol.Avoid2Connectors)
3055
                    {
3056
                        if (connector.get_ItemStatus() != "Active")
3057
                            continue;
3058

    
3059
                        if (connector.Id != zeroLenthConnector.Id)
3060
                            InputLineAttributeForSpecBreakLMConnector(connector, sppidAttributeName, downStreamValue);
3061
                    }
3062

    
3063
                    ReleaseCOMObjects(zeroLenthConnector);
3064
                    ReleaseCOMObjects(upStreamLMSymbol);
3065
                    ReleaseCOMObjects(downStreamLMSymbol);
3066
                }
3067
                else if (upStreamSymbol != null && downStreamLine != null)
3068
                {
3069
                    LMConnector zeroLenthConnector = FindBreakLineTarget(upStreamSymbol, downStreamLine);
3070
                    InputLineAttributeForSpecBreakLine(downStreamLine, sppidAttributeName, downStreamValue);
3071
                    LMSymbol upStreamLMSymbol = dataSource.GetSymbol(upStreamSymbol.SPPID.RepresentationId);
3072

    
3073
                    foreach (LMConnector connector in upStreamLMSymbol.Avoid1Connectors)
3074
                    {
3075
                        if (connector.get_ItemStatus() != "Active")
3076
                            continue;
3077

    
3078
                        if (connector.Id != zeroLenthConnector.Id)
3079
                            InputLineAttributeForSpecBreakLMConnector(connector, sppidAttributeName, upStreamValue);
3080
                    }
3081

    
3082
                    foreach (LMConnector connector in upStreamLMSymbol.Avoid2Connectors)
3083
                    {
3084
                        if (connector.get_ItemStatus() != "Active")
3085
                            continue;
3086

    
3087
                        if (connector.Id != zeroLenthConnector.Id)
3088
                            InputLineAttributeForSpecBreakLMConnector(connector, sppidAttributeName, upStreamValue);
3089
                    }
3090

    
3091
                    ReleaseCOMObjects(zeroLenthConnector);
3092
                    ReleaseCOMObjects(upStreamLMSymbol);
3093
                }
3094
                else if (upStreamLine != null && downStreamSymbol != null)
3095
                {
3096
                    LMConnector zeroLenthConnector = FindBreakLineTarget(upStreamLine, downStreamSymbol);
3097
                    InputLineAttributeForSpecBreakLine(upStreamLine, sppidAttributeName, upStreamValue);
3098
                    LMSymbol downStreamLMSymbol = dataSource.GetSymbol(downStreamSymbol.SPPID.RepresentationId);
3099

    
3100
                    foreach (LMConnector connector in downStreamLMSymbol.Avoid1Connectors)
3101
                    {
3102
                        if (connector.get_ItemStatus() != "Active")
3103
                            continue;
3104

    
3105
                        if (connector.Id != zeroLenthConnector.Id)
3106
                            InputLineAttributeForSpecBreakLMConnector(connector, sppidAttributeName, downStreamValue);
3107
                    }
3108

    
3109
                    foreach (LMConnector connector in downStreamLMSymbol.Avoid2Connectors)
3110
                    {
3111
                        if (connector.get_ItemStatus() != "Active")
3112
                            continue;
3113

    
3114
                        if (connector.Id != zeroLenthConnector.Id)
3115
                            InputLineAttributeForSpecBreakLMConnector(connector, sppidAttributeName, downStreamValue);
3116
                    }
3117

    
3118
                    ReleaseCOMObjects(zeroLenthConnector);
3119
                    ReleaseCOMObjects(downStreamLMSymbol);
3120
                }
3121
            }
3122

    
3123
            void InputLineAttributeForSpecBreakLine(Line line, string attrName, string value)
3124
            {
3125
                LMModelItem _LMModelItem = dataSource.GetModelItem(line.SPPID.ModelItemId);
3126
                if (_LMModelItem != null && _LMModelItem.get_ItemStatus() == "Active")
3127
                {
3128
                    LMAAttribute _LMAAttribute = _LMModelItem.Attributes[attrName];
3129
                    if (_LMAAttribute != null)
3130
                    {
3131
                        if (DBNull.Value.Equals(_LMAAttribute.get_Value()))
3132
                            _LMAAttribute.set_Value(value);
3133
                        else if (_LMAAttribute.get_Value() != value)
3134
                            _LMAAttribute.set_Value(value);
3135
                    }
3136

    
3137
                    _LMModelItem.Commit();
3138
                }
3139
                if (_LMModelItem != null)
3140
                    ReleaseCOMObjects(_LMModelItem);
3141
            }
3142

    
3143
            void InputLineAttributeForSpecBreakLMConnector(LMConnector connector, string attrName, string value)
3144
            {
3145
                LMModelItem _LMModelItem = dataSource.GetModelItem(connector.ModelItemID);
3146
                if (_LMModelItem != null && _LMModelItem.get_ItemStatus() == "Active")
3147
                {
3148
                    LMAAttribute _LMAAttribute = _LMModelItem.Attributes[attrName];
3149
                    if (_LMAAttribute != null)
3150
                    {
3151
                        if (DBNull.Value.Equals(_LMAAttribute.get_Value()))
3152
                            _LMAAttribute.set_Value(value);
3153
                        else if (_LMAAttribute.get_Value() != value)
3154
                            _LMAAttribute.set_Value(value);
3155
                    }
3156

    
3157
                    _LMModelItem.Commit();
3158
                }
3159
                if (_LMModelItem != null)
3160
                    ReleaseCOMObjects(_LMModelItem);
3161
            }
3162
            #endregion
3163
        }
3164

    
3165
        /// <summary>
3166
        /// Text Modeling - Association일 경우는 Text대신 해당 맵핑된 Symbol로 모델링
3167
        /// </summary>
3168
        /// <param name="text"></param>
3169
        private void TextModeling(Text text)
3170
        {
3171
            LMSymbol _LMSymbol = null;
3172
            LMConnector connectedLMConnector = null;
3173
            //if (text.ASSOCIATION && !string.IsNullOrEmpty(text.OWNER) && text.OWNER != "None")
3174
            if (text.ASSOCIATION)
3175
            {
3176
                object owner = SPPIDUtil.FindObjectByUID(document, text.OWNER);
3177
                if (owner.GetType() == typeof(Symbol))
3178
                {
3179
                    Symbol symbol = owner as Symbol;
3180
                    _LMSymbol = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
3181
                    if (_LMSymbol != null)
3182
                    {
3183
                        BaseModel.Attribute attribute = symbol.ATTRIBUTES.Find(x => x.ASSOCITEM == text.UID);
3184
                        if (attribute != null && !string.IsNullOrEmpty(attribute.VALUE) && attribute.VALUE != "None")
3185
                        {
3186
                            AttributeMapping mapping = document.AttributeMappings.Find(x => x.UID == attribute.UID && !string.IsNullOrEmpty(x.SPPIDSYMBOLNAME));
3187

    
3188
                            if (mapping != null)
3189
                            {
3190
                                double x = 0;
3191
                                double y = 0;
3192

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

    
3196
                                LMLabelPersist _LMLabelPersist = _placement.PIDPlaceLabel(mapping.SPPIDSYMBOLNAME, ref array, Rotation: text.ANGLE, LabeledItem: _LMSymbol.AsLMRepresentation(), IsLeaderVisible: mapping.LeaderLine);
3197
                                if (_LMLabelPersist != null)
3198
                                {
3199
                                    _LMLabelPersist.Commit();
3200
                                    ReleaseCOMObjects(_LMLabelPersist);
3201
                                }
3202
                            }
3203
                        }
3204
                    }
3205
                }
3206
                else if (owner.GetType() == typeof(Line))
3207
                {
3208
                    Line line = owner as Line;
3209
                    Dictionary<LMConnector, List<double[]>> connectorVertices = GetPipeRunVertices(line.SPPID.ModelItemId);
3210
                    connectedLMConnector = FindTargetLMConnectorForLabel(connectorVertices, text.SPPID.ORIGINAL_X, text.SPPID.ORIGINAL_Y);
3211

    
3212
                    if (connectedLMConnector != null)
3213
                    {
3214
                        BaseModel.Attribute attribute = line.ATTRIBUTES.Find(x => x.ASSOCITEM == text.UID);
3215
                        if (attribute != null && !string.IsNullOrEmpty(attribute.VALUE) && attribute.VALUE != "None")
3216
                        {
3217
                            AttributeMapping mapping = document.AttributeMappings.Find(x => x.UID == attribute.UID && !string.IsNullOrEmpty(x.SPPIDSYMBOLNAME));
3218

    
3219
                            if (mapping != null)
3220
                            {
3221
                                double x = 0;
3222
                                double y = 0;
3223

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

    
3227
                                LMLabelPersist _LMLabelPersist = _placement.PIDPlaceLabel(mapping.SPPIDSYMBOLNAME, ref array, Rotation: text.ANGLE, LabeledItem: connectedLMConnector.AsLMRepresentation(), IsLeaderVisible: mapping.LeaderLine);
3228
                                if (_LMLabelPersist != null)
3229
                                {
3230
                                    _LMLabelPersist.Commit();
3231
                                    ReleaseCOMObjects(_LMLabelPersist);
3232
                                }
3233
                            }
3234
                        }
3235
                    }
3236
                }
3237
            }
3238
            else
3239
            {
3240
                LMItemNote _LMItemNote = null;
3241
                LMAAttribute _LMAAttribute = null;
3242

    
3243
                double x = 0;
3244
                double y = 0;
3245
                double angle = text.ANGLE;
3246
                CalcLabelLocation(ref x, ref y, text.SPPID.ORIGINAL_X, text.SPPID.ORIGINAL_Y, text.SPPIDLabelLocation, _ETCSetting.TextLocation);
3247

    
3248
                _LMSymbol = _placement.PIDPlaceSymbol(text.SPPID.MAPPINGNAME, x, y, Rotation: angle);
3249
                _LMSymbol.Commit();
3250
                _LMItemNote = _placement.PIDDataSource.GetItemNote(_LMSymbol.ModelItemID);
3251
                _LMItemNote.Commit();
3252
                _LMAAttribute = _LMItemNote.Attributes["Note.Body"];
3253
                _LMAAttribute.set_Value(text.VALUE);
3254
                _LMItemNote.Commit();
3255

    
3256
                if (_LMAAttribute != null)
3257
                    ReleaseCOMObjects(_LMAAttribute);
3258
                if (_LMItemNote != null)
3259
                    ReleaseCOMObjects(_LMItemNote);
3260
            }
3261
            if (_LMSymbol != null)
3262
                ReleaseCOMObjects(_LMSymbol);
3263
        }
3264

    
3265
        /// <summary>
3266
        /// Note Modeling
3267
        /// </summary>
3268
        /// <param name="note"></param>
3269
        private void NoteModeling(Note note)
3270
        {
3271
            LMSymbol _LMSymbol = null;
3272
            LMItemNote _LMItemNote = null;
3273
            LMAAttribute _LMAAttribute = null;
3274

    
3275
            if (string.IsNullOrEmpty(note.OWNER) || note.OWNER == "None")
3276
            {
3277
                double x = 0;
3278
                double y = 0;
3279

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

    
3282
                _LMSymbol = _placement.PIDPlaceSymbol(note.SPPID.MAPPINGNAME, x, y);
3283
                _LMSymbol.Commit();
3284
                _LMItemNote = _placement.PIDDataSource.GetItemNote(_LMSymbol.ModelItemID);
3285
                _LMItemNote.Commit();
3286
                _LMAAttribute = _LMItemNote.Attributes["Note.Body"];
3287
                _LMAAttribute.set_Value(note.VALUE);
3288
                _LMItemNote.Commit();
3289
            }
3290

    
3291
            if (_LMAAttribute != null)
3292
                ReleaseCOMObjects(_LMAAttribute);
3293
            if (_LMItemNote != null)
3294
                ReleaseCOMObjects(_LMItemNote);
3295
            if (_LMSymbol != null)
3296
                ReleaseCOMObjects(_LMSymbol);
3297
        }
3298

    
3299
        /// <summary>
3300
        /// Note Symbol Modeling
3301
        /// </summary>
3302
        /// <param name="symbol"></param>
3303
        private void NoteSymbolModeling(Symbol symbol)
3304
        {
3305
            if (symbol.TYPE == "Notes")
3306
            {
3307
                string mappingPath = symbol.SPPID.MAPPINGNAME;
3308
                double x = symbol.SPPID.ORIGINAL_X;
3309
                double y = symbol.SPPID.ORIGINAL_Y;
3310
                int mirror = 0;
3311
                double angle = symbol.ANGLE;
3312

    
3313
                LMSymbol _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle);
3314
                symbol.SPPID.RepresentationId = _LMSymbol.AsLMRepresentation().Id;
3315
                symbol.SPPID.ModelItemID = _LMSymbol.ModelItemID;
3316

    
3317
                ReleaseCOMObjects(_LMSymbol);
3318

    
3319
                InputSymbolAttribute(symbol, symbol.ATTRIBUTES);
3320
            }
3321
        }
3322

    
3323
        /// <summary>
3324
        /// Label의 좌표를 구하는 메서드(ID2 기준의 좌표 -> SPPID 좌표)
3325
        /// </summary>
3326
        /// <param name="x"></param>
3327
        /// <param name="y"></param>
3328
        /// <param name="originX"></param>
3329
        /// <param name="originY"></param>
3330
        /// <param name="SPPIDLabelLocation"></param>
3331
        /// <param name="location"></param>
3332
        private void CalcLabelLocation(ref double x, ref double y, double originX, double originY, SPPIDLabelLocationInfo SPPIDLabelLocation, Location location)
3333
        {
3334
            if (location == Location.None)
3335
            {
3336
                x = originX;
3337
                y = originY;
3338
            }
3339
            else
3340
            {
3341
                if (location.HasFlag(Location.Center))
3342
                {
3343
                    x = (SPPIDLabelLocation.X1 + SPPIDLabelLocation.X2) / 2;
3344
                    y = (SPPIDLabelLocation.Y1 + SPPIDLabelLocation.Y2) / 2;
3345
                }
3346

    
3347
                if (location.HasFlag(Location.Left))
3348
                    x = SPPIDLabelLocation.X1;
3349
                else if (location.HasFlag(Location.Right))
3350
                    x = SPPIDLabelLocation.X2;
3351

    
3352
                if (location.HasFlag(Location.Down))
3353
                    y = SPPIDLabelLocation.Y1;
3354
                else if (location.HasFlag(Location.Up))
3355
                    y = SPPIDLabelLocation.Y2;
3356
            }
3357
        }
3358

    
3359
        /// <summary>
3360
        /// Symbol의 우선순위 Modeling 목록을 가져온다.
3361
        /// 1. Angle Valve
3362
        /// 2. 3개로 이루어진 Symbol Group
3363
        /// </summary>
3364
        /// <returns></returns>
3365
        private List<Symbol> GetPrioritySymbol()
3366
        {
3367
            DataTable symbolTable = document.SymbolTable;
3368
            // List에 순서대로 쌓는다.
3369
            List<Symbol> symbols = new List<Symbol>();
3370

    
3371
            // Angle Valve 부터
3372
            foreach (var symbol in document.SYMBOLS.FindAll(x => x.CONNECTORS.FindAll(y => y.Index == 0).Count == 2))
3373
            {
3374
                if (!symbols.Contains(symbol))
3375
                {
3376
                    double originX = 0;
3377
                    double originY = 0;
3378

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

    
3383
                    SlopeType slopeType1 = SlopeType.None;
3384
                    SlopeType slopeType2 = SlopeType.None;
3385
                    foreach (Connector connector in symbol.CONNECTORS.FindAll(x => x.Index == 0))
3386
                    {
3387
                        double connectorX = 0;
3388
                        double connectorY = 0;
3389
                        SPPIDUtil.ConvertPointBystring(connector.CONNECTPOINT, ref connectorX, ref connectorY);
3390
                        if (slopeType1 == SlopeType.None)
3391
                            slopeType1 = SPPIDUtil.CalcSlope(originX, originY, connectorX, connectorY);
3392
                        else
3393
                            slopeType2 = SPPIDUtil.CalcSlope(originX, originY, connectorX, connectorY);
3394
                    }
3395

    
3396
                    if ((slopeType1 == SlopeType.VERTICAL && slopeType2 == SlopeType.HORIZONTAL) ||
3397
                        (slopeType2 == SlopeType.VERTICAL && slopeType1 == SlopeType.HORIZONTAL))
3398
                        symbols.Add(symbol);
3399
                }
3400
            }
3401

    
3402
            List<Symbol> tempSymbols = new List<Symbol>();
3403
            // Conn 갯수 기준
3404
            foreach (var item in document.SYMBOLS)
3405
            {
3406
                if (!symbols.Contains(item))
3407
                    tempSymbols.Add(item);
3408
            }
3409
            tempSymbols.Sort(SortSymbolPriority);
3410
            symbols.AddRange(tempSymbols);
3411

    
3412
            return symbols;
3413
        }
3414

    
3415
        private void SetPriorityLine()
3416
        {
3417
            document.LINES.Sort(SortLinePriority);
3418

    
3419
            int SortLinePriority(Line a, Line b)
3420
            {
3421
                // Branch 없는것부터
3422
                int branchRetval = CompareBranchLine(a, b);
3423
                if (branchRetval != 0)
3424
                {
3425
                    return branchRetval;
3426
                }
3427
                else
3428
                {
3429
                    // Symbol 연결 갯수
3430
                    int connSymbolRetval = CompareConnSymbol(a, b);
3431
                    if (connSymbolRetval != 0)
3432
                    {
3433
                        return connSymbolRetval;
3434
                    }
3435
                    else
3436
                    {
3437
                        // 아이템 연결 갯수(심볼, Line이면서 Not Branch)
3438
                        int connItemRetval = CompareConnItem(a, b);
3439
                        if (connItemRetval != 0)
3440
                        {
3441
                            return connItemRetval;
3442
                        }
3443
                        else
3444
                        {
3445
                            // ConnectedItem이 없는것
3446
                            int noneConnRetval = CompareNoneConn(a, b);
3447
                            if (noneConnRetval != 0)
3448
                            {
3449
                                return noneConnRetval;
3450
                            }
3451
                            else
3452
                            {
3453

    
3454
                            }
3455
                        }
3456
                    }
3457
                }
3458

    
3459
                return 0;
3460
            }
3461

    
3462
            int CompareConnSymbol(Line a, Line b)
3463
            {
3464
                List<Connector> connectorsA = a.CONNECTORS
3465
                    .Where(conn => conn.ConnectedObject != null && conn.ConnectedObject.GetType() == typeof(Symbol))
3466
                    .ToList();
3467

    
3468
                List<Connector> connectorsB = b.CONNECTORS
3469
                    .Where(conn => conn.ConnectedObject != null && conn.ConnectedObject.GetType() == typeof(Symbol))
3470
                    .ToList();
3471

    
3472
                // 오름차순
3473
                return connectorsB.Count.CompareTo(connectorsA.Count);
3474
            }
3475

    
3476
            int CompareConnItem(Line a, Line b)
3477
            {
3478
                List<Connector> connectorsA = a.CONNECTORS
3479
                    .Where(conn => conn.ConnectedObject != null && 
3480
                    (conn.ConnectedObject.GetType() == typeof(Symbol) || 
3481
                    (conn.ConnectedObject.GetType() == typeof(Line) && !SPPIDUtil.IsBranchLine((Line)conn.ConnectedObject, a))))
3482
                    .ToList();
3483

    
3484
                List<Connector> connectorsB = b.CONNECTORS
3485
                    .Where(conn => conn.ConnectedObject != null &&
3486
                    (conn.ConnectedObject.GetType() == typeof(Symbol) ||
3487
                    (conn.ConnectedObject.GetType() == typeof(Line) && !SPPIDUtil.IsBranchLine((Line)conn.ConnectedObject, b))))
3488
                    .ToList();
3489

    
3490
                // 오름차순
3491
                return connectorsB.Count.CompareTo(connectorsA.Count);
3492
            }
3493

    
3494
            int CompareBranchLine(Line a, Line b)
3495
            {
3496
                List<Connector> connectorsA = a.CONNECTORS
3497
                    .Where(conn => conn.ConnectedObject != null && conn.ConnectedObject.GetType() == typeof(Line) && SPPIDUtil.IsBranchLine(a, conn.ConnectedObject as Line))
3498
                    .ToList();
3499
                List<Connector> connectorsB = b.CONNECTORS
3500
                    .Where(conn => conn.ConnectedObject != null && conn.ConnectedObject.GetType() == typeof(Line) && SPPIDUtil.IsBranchLine(b, conn.ConnectedObject as Line))
3501
                    .ToList();
3502

    
3503
                // 내림차순
3504
                return connectorsA.Count.CompareTo(connectorsB.Count);
3505
            }
3506

    
3507
            int CompareNoneConn(Line a, Line b)
3508
            {
3509
                List<Connector> connectorsA = a.CONNECTORS
3510
                    .Where(conn => conn.ConnectedObject == null)
3511
                    .ToList();
3512

    
3513
                List<Connector> connectorsB = b.CONNECTORS
3514
                    .Where(conn => conn.ConnectedObject == null)
3515
                    .ToList();
3516

    
3517
                // 오름차순
3518
                return connectorsB.Count.CompareTo(connectorsA.Count);
3519
            }
3520
        }
3521

    
3522
        private void SortBranchLines()
3523
        {
3524
            NewBranchLines.Sort(SortBranchLine);
3525
            int SortBranchLine(Line a, Line b)
3526
            {
3527
                int countA = a.CONNECTORS.FindAll(x => x.ConnectedObject != null &&
3528
                 x.ConnectedObject.GetType() == typeof(Line) &&
3529
                 SPPIDUtil.IsBranchLine(x.ConnectedObject as Line, a) &&
3530
                 string.IsNullOrEmpty(((Line)x.ConnectedObject).SPPID.ModelItemId)).Count;
3531

    
3532
                int countB = b.CONNECTORS.FindAll(x => x.ConnectedObject != null &&
3533
                 x.ConnectedObject.GetType() == typeof(Line) &&
3534
                 SPPIDUtil.IsBranchLine(x.ConnectedObject as Line, b) &&
3535
                 string.IsNullOrEmpty(((Line)x.ConnectedObject).SPPID.ModelItemId)).Count;
3536

    
3537
                // 내림차순
3538
                return countA.CompareTo(countB);
3539
            }
3540
        }
3541

    
3542
        private static int SortSymbolPriority(Symbol a, Symbol b)
3543
        {
3544
            int countA = a.CONNECTORS.FindAll(x => !string.IsNullOrEmpty(x.CONNECTEDITEM) && x.CONNECTEDITEM != "None").Count;
3545
            int countB = b.CONNECTORS.FindAll(x => !string.IsNullOrEmpty(x.CONNECTEDITEM) && x.CONNECTEDITEM != "None").Count;
3546
            int retval = countB.CompareTo(countA);
3547
            if (retval != 0)
3548
                return retval;
3549
            else
3550
                return a.SPPID.ORIGINAL_X.CompareTo(b.SPPID.ORIGINAL_X);
3551
        }
3552

    
3553
        /// <summary>
3554
        /// Graphic OID로 해당 Symbol의 크기를 구하여 Zoom
3555
        /// </summary>
3556
        /// <param name="graphicOID"></param>
3557
        /// <param name="milliseconds"></param>
3558
        private void ZoomObjectByGraphicOID(string graphicOID, int milliseconds = 150)
3559
        {
3560
            if (radApp.ActiveDocument.ActiveSheet.DrawingObjects[graphicOID] != null)
3561
            {
3562
                double minX = 0;
3563
                double minY = 0;
3564
                double maxX = 0;
3565
                double maxY = 0;
3566
                radApp.ActiveDocument.ActiveSheet.DrawingObjects[graphicOID].Range(out minX, out minY, out maxX, out maxY);
3567
                radApp.ActiveWindow.ZoomArea2(minX - 0.007, minY - 0.007, maxX + 0.007, maxY + 0.007, null);
3568

    
3569
                Thread.Sleep(milliseconds);
3570
            }
3571
        }
3572

    
3573
        /// <summary>
3574
        /// ComObject를 Release
3575
        /// </summary>
3576
        /// <param name="objVars"></param>
3577
        public void ReleaseCOMObjects(params object[] objVars)
3578
        {
3579
            int intNewRefCount = 0;
3580
            foreach (object obj in objVars)
3581
            {
3582
                if (!Information.IsNothing(obj) && System.Runtime.InteropServices.Marshal.IsComObject(obj))
3583
                    intNewRefCount = intNewRefCount + System.Runtime.InteropServices.Marshal.FinalReleaseComObject(obj);
3584
            }
3585
        }
3586
    }
3587
}
클립보드 이미지 추가 (최대 크기: 500 MB)