프로젝트

일반

사용자정보

통계
| 개정판:

hytos / DTI_PID / SPPIDConverter / AutoModeling.cs @ 9bcb092b

이력 | 보기 | 이력해설 | 다운로드 (161 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
using DevExpress.XtraSplashScreen;
25
namespace Converter.SPPID
26
{
27
    public class AutoModeling
28
    {
29
        Placement _placement;
30
        LMADataSource dataSource;
31
        dynamic newDrawing;
32
        dynamic application;
33
        Ingr.RAD2D.Application radApp;
34
        SPPID_Document document;
35
        ETCSetting _ETCSetting;
36

    
37
        public string DocumentLabelText { get; set; }
38

    
39
        List<Line> NewBranchLines = new List<Line>();
40
        List<Tuple<string, Line, Line>> BranchLines = new List<Tuple<string, Line, Line>>();
41
        List<string> ZeroLengthModelItemID = new List<string>();
42
        List<Symbol> prioritySymbols;
43

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

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

    
64
                }
65
            }
66
        }
67

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

    
80
                CreateDocument(ref drawingNumber, ref drawingName);
81

    
82
                if (DocumentCoordinateCorrection())
83
                {
84
                    Log.Write("Start Modeling");
85
                    SplashScreenManager.ShowForm(typeof(SPPIDSplashScreen), true, true);
86
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetParent, (IntPtr)radApp.HWnd);
87
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetDocumentName, DocumentLabelText);
88

    
89
                    // Equipment Modeling
90
                    RunEquipmentModeling();
91

    
92
                    // Symbol Modeling
93
                    RunSymbolModeling();
94

    
95
                    // LineRun Line Modeling
96
                    RunLineModeling();
97

    
98
                    // Branch Line Modeling
99
                    RunBranchLineModeling();
100

    
101
                    // Clear Attribute
102
                    RunClearAttribute();
103

    
104
                    // EndBreak Modeling
105
                    RunEndBreakModeling();
106

    
107
                    // SpecBreak Modeling
108
                    RunSpecBreakModeling();
109

    
110
                    //// LineNumber Modeling
111
                    //RunLineNumberModeling();
112

    
113
                    //// FlowMark Modeling
114
                    //SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Flow Mark Modeling");
115
                    //foreach (var item in document.LINES)
116
                    //    try
117
                    //    {
118
                    //        FlowMarkModeling(item);
119
                    //    }
120
                    //    catch (Exception ex)
121
                    //    {
122
                    //        Log.Write("Error in FlowMarkModeling");
123
                    //        Log.Write("UID : " + item.UID);
124
                    //        Log.Write(ex.Message);
125
                    //        Log.Write(ex.StackTrace);
126
                    //    }
127

    
128

    
129
                    //// Note Symbol Modeling
130
                    //SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Note Symbol Modeling");
131
                    //foreach (var item in document.SYMBOLS)
132
                    //    try
133
                    //    {
134
                    //        NoteSymbolModeling(item);
135
                    //    }
136
                    //    catch (Exception ex)
137
                    //    {
138
                    //        Log.Write("Error in NoteSymbolModeling");
139
                    //        Log.Write("UID : " + item.UID);
140
                    //        Log.Write(ex.Message);
141
                    //        Log.Write(ex.StackTrace);
142
                    //    }
143

    
144

    
145
                    //// Note Modeling
146
                    //SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Notes Modeling");
147
                    //foreach (var item in document.NOTES)
148
                    //    try
149
                    //    {
150
                    //        NoteModeling(item);
151
                    //    }
152
                    //    catch (Exception ex)
153
                    //    {
154
                    //        Log.Write("Error in NoteModeling");
155
                    //        Log.Write("UID : " + item.UID);
156
                    //        Log.Write(ex.Message);
157
                    //        Log.Write(ex.StackTrace);
158
                    //    }
159

    
160
                    //// Text Modeling
161
                    //SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Texts Modeling");
162
                    //foreach (var item in document.TEXTINFOS)
163
                    //    try
164
                    //    {
165
                    //        TextModeling(item);
166
                    //    }
167
                    //    catch (Exception ex)
168
                    //    {
169
                    //        Log.Write("Error in TextModeling");
170
                    //        Log.Write("UID : " + item.UID);
171
                    //        Log.Write(ex.Message);
172
                    //        Log.Write(ex.StackTrace);
173
                    //    }
174

    
175
                    //// Input LineNumber Attribute
176
                    //SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Set LineNumbers Attribute");
177
                    //foreach (var item in document.LINENUMBERS)
178
                    //    try
179
                    //    {
180
                    //        InputLineNumberAttribute(item);
181
                    //    }
182
                    //    catch (Exception ex)
183
                    //    {
184
                    //        Log.Write("Error in InputLineNumberAttribute");
185
                    //        Log.Write("UID : " + item.UID);
186
                    //        Log.Write(ex.Message);
187
                    //        Log.Write(ex.StackTrace);
188
                    //    }
189

    
190
                    //// Input Symbol Attribute
191
                    //SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Set Symbols Attribute");
192
                    //foreach (var item in document.SYMBOLS)
193
                    //    try
194
                    //    {
195
                    //        InputSymbolAttribute(item, item.ATTRIBUTES);
196
                    //    }
197
                    //    catch (Exception ex)
198
                    //    {
199
                    //        Log.Write("Error in InputSymbolAttribute");
200
                    //        Log.Write("UID : " + item.UID);
201
                    //        Log.Write(ex.Message);
202
                    //        Log.Write(ex.StackTrace);
203
                    //    }
204

    
205
                    //// Input SpecBreak Attribute
206
                    //SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Set Symbols Attribute");
207
                    //foreach (var item in document.SpecBreaks)
208
                    //    try
209
                    //    {
210
                    //        InputSpecBreakAttribute(item);
211
                    //    }
212
                    //    catch (Exception ex)
213
                    //    {
214
                    //        Log.Write("Error in InputSpecBreakAttribute");
215
                    //        Log.Write("UID : " + item.UID);
216
                    //        Log.Write(ex.Message);
217
                    //        Log.Write(ex.StackTrace);
218
                    //    }
219

    
220
                    //// Label Symbol Modeling
221
                    //SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Labels Modeling");
222
                    //foreach (var item in document.SYMBOLS)
223
                    //    try
224
                    //    {
225
                    //        LabelSymbolModeling(item);
226
                    //    }
227
                    //    catch (Exception ex)
228
                    //    {
229
                    //        Log.Write("Error in LabelSymbolModeling");
230
                    //        Log.Write("UID : " + item.UID);
231
                    //        Log.Write(ex.Message);
232
                    //        Log.Write(ex.StackTrace);
233
                    //    }
234

    
235
                    //// LineRun Line Join
236
                    //SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Join LineRuns");
237
                    //foreach (LineNumber lineNumber in document.LINENUMBERS)
238
                    //    try
239
                    //    {
240
                    //        foreach (LineRun run in lineNumber.RUNS)
241
                    //            JoinRunLine(run);
242
                    //    }
243
                    //    catch (Exception ex)
244
                    //    {
245
                    //        Log.Write("Error in JoinRunLine");
246
                    //        Log.Write("UID : " + lineNumber.UID);
247
                    //        Log.Write(ex.Message);
248
                    //        Log.Write(ex.StackTrace);
249
                    //    }
250

    
251
                    //// TrimLineRun Line Join
252
                    //foreach (TrimLine trimLine in document.TRIMLINES)
253
                    //    try
254
                    //    {
255
                    //        foreach (LineRun run in trimLine.RUNS)
256
                    //            JoinRunLine(run);
257
                    //    }
258
                    //    catch (Exception ex)
259
                    //    {
260
                    //        Log.Write("Error in JoinRunLine");
261
                    //        Log.Write("UID : " + trimLine.UID);
262
                    //        Log.Write(ex.Message);
263
                    //        Log.Write(ex.StackTrace);
264
                    //    }
265
                }
266
            }
267
            catch (Exception ex)
268
            {
269
                SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.ClearParent, null);
270
                SplashScreenManager.CloseForm(false);
271
                System.Windows.Forms.MessageBox.Show(ex.Message + "\r\n" + ex.StackTrace);
272
            }
273
            finally
274
            {
275
                Log.Write("End Modeling");
276
                application.ActiveWindow.Fit();
277

    
278
                if (radApp.ActiveDocument != null)
279
                {
280
                    radApp.ActiveDocument.Save();
281
                    //radApp.ActiveDocument.SaveOnClose = false;
282
                    //radApp.ActiveDocument.Close(false);
283

    
284
                    ReleaseCOMObjects(newDrawing);
285
                }
286

    
287
                ReleaseCOMObjects(dataSource);
288
                ReleaseCOMObjects(_placement);
289

    
290
                Project_DB.InsertDrawingInfo(document.PATH, drawingNumber, drawingName, document);
291
                if (SplashScreenManager.Default.IsSplashFormVisible)
292
                {
293
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.ClearParent, null);
294
                    SplashScreenManager.CloseForm(false);
295
                    Log.Write("\r\n");
296
                }
297
            }
298
        }
299
        private void RunEquipmentModeling()
300
        {
301
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Equipments Modeling");
302
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetAllProgress, document.Equipments.Count);
303
            foreach (Equipment item in document.Equipments)
304
            {
305
                try
306
                {
307
                    EquipmentModeling(item);
308
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetProgress, document.Equipments.FindAll(x => !string.IsNullOrEmpty(x.SPPID.RepresentationId)).Count);
309
                }
310
                catch (Exception ex)
311
                {
312
                    Log.Write("Error in EquipmentModeling");
313
                    Log.Write("UID : " + item.UID);
314
                    Log.Write(ex.Message);
315
                    Log.Write(ex.StackTrace);
316
                }
317
            }
318
        }
319
        private void RunSymbolModeling()
320
        {
321
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Symbol Modeling");
322
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetAllProgress, document.SYMBOLS.Count);
323
            prioritySymbols = GetPrioritySymbol();
324
            foreach (var item in prioritySymbols)
325
            {
326
                try
327
                {
328
                    SymbolModelingBySymbol(item);
329
                }
330
                catch (Exception ex)
331
                {
332
                    Log.Write("Error in SymbolModelingByPriority");
333
                    Log.Write("UID : " + item.UID);
334
                    Log.Write(ex.Message);
335
                    Log.Write(ex.StackTrace);
336
                }
337
            }
338
        }
339
        private void RunLineModeling()
340
        {
341
            SetPriorityLine();
342
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Lines Modeling");
343
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetAllProgress, document.LINES.Count);
344
            foreach (var item in document.LINES)
345
            {
346
                try
347
                {
348
                    NewLineModeling(item);
349
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
350
                }
351
                catch (Exception ex)
352
                {
353
                    Log.Write("Error in NewLineModeling");
354
                    Log.Write("UID : " + item.UID);
355
                    Log.Write(ex.Message);
356
                    Log.Write(ex.StackTrace);
357
                }
358
            }
359
        }
360
        private void RunBranchLineModeling()
361
        {
362
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Branch Lines Modeling");
363
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetAllProgress, NewBranchLines.Count);
364
            int branchCount = NewBranchLines.Count;
365
            while (NewBranchLines.Count > 0)
366
            {
367
                Line item = NewBranchLines[0];
368
                try
369
                {
370
                    SortBranchLines();
371
                    NewLineModeling(item, true);
372
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
373
                }
374
                catch (Exception ex)
375
                {
376
                    Log.Write("Error in NewLineModeling");
377
                    Log.Write("UID : " + item.UID);
378
                    Log.Write(ex.Message);
379
                    Log.Write(ex.StackTrace);
380
                }
381
            }
382
        }
383
        private void RunClearAttribute()
384
        {
385
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetAllProgress, document.SYMBOLS.Count + document.LINES.Count);
386
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Clear Attribute");
387
            SetSystemEditingCommand(false);
388
            List<string> endClearModelItemID = new List<string>();
389
            for (int i = 0; i < document.LINES.Count; i++)
390
            {
391
                Line item = document.LINES[i];
392
                string modelItemID = item.SPPID.ModelItemId;
393
                if (!string.IsNullOrEmpty(modelItemID))
394
                {
395
                    LMModelItem modelItem = dataSource.GetModelItem(modelItemID);
396
                    if (modelItem != null)
397
                    {
398
                        LMAAttribute attribute = modelItem.Attributes["NominalDiameter"];
399
                        if (attribute != null)
400
                            attribute.set_Value(DBNull.Value);
401

    
402
                        modelItem.Commit();
403
                        ReleaseCOMObjects(modelItem);
404
                    }
405
                }
406
                if (!endClearModelItemID.Contains(modelItemID))
407
                    endClearModelItemID.Add(modelItemID);
408
                SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
409
            }
410
            for (int i = 0; i < document.SYMBOLS.Count; i++)
411
            {
412
                Symbol item = document.SYMBOLS[i];
413
                string repID = item.SPPID.RepresentationId;
414
                string modelItemID = item.SPPID.ModelItemID;
415
                if (!string.IsNullOrEmpty(modelItemID))
416
                {
417
                    LMModelItem modelItem = dataSource.GetModelItem(modelItemID);
418
                    if (modelItem != null)
419
                    {
420
                        LMAAttribute attribute = modelItem.Attributes["NominalDiameter"];
421
                        if (attribute != null)
422
                            attribute.set_Value(DBNull.Value);
423
                        attribute = modelItem.Attributes["PipingPoint1.NominalDiameter"];
424
                        if (attribute != null)
425
                            attribute.set_Value(DBNull.Value);
426
                        attribute = modelItem.Attributes["PipingPoint2.NominalDiameter"];
427
                        if (attribute != null)
428
                            attribute.set_Value(DBNull.Value);
429
                        modelItem.Commit();
430

    
431
                        modelItem.Commit();
432
                        ReleaseCOMObjects(modelItem);
433
                    }
434
                }
435
                if (!string.IsNullOrEmpty(repID))
436
                {
437
                    LMSymbol symbol = dataSource.GetSymbol(repID);
438
                    if (symbol != null)
439
                    {
440
                        foreach (LMConnector connector in symbol.Connect1Connectors)
441
                        {
442
                            if (connector.get_ItemStatus() == "Active" && !endClearModelItemID.Contains(connector.ModelItemID))
443
                            {
444
                                endClearModelItemID.Add(connector.ModelItemID);
445
                                LMModelItem modelItem = connector.ModelItemObject;
446
                                if (modelItem != null)
447
                                {
448
                                    LMAAttribute attribute = modelItem.Attributes["NominalDiameter"];
449
                                    if (attribute != null)
450
                                        attribute.set_Value(DBNull.Value);
451

    
452
                                    modelItem.Commit();
453
                                    ReleaseCOMObjects(modelItem);
454
                                }
455
                            }
456
                        }
457
                        foreach (LMConnector connector in symbol.Connect2Connectors)
458
                        {
459
                            if (connector.get_ItemStatus() == "Active" && !endClearModelItemID.Contains(connector.ModelItemID))
460
                            {
461
                                endClearModelItemID.Add(connector.ModelItemID);
462
                                LMModelItem modelItem = connector.ModelItemObject;
463
                                if (modelItem != null)
464
                                {
465
                                    LMAAttribute attribute = modelItem.Attributes["NominalDiameter"];
466
                                    if (attribute != null)
467
                                        attribute.set_Value(DBNull.Value);
468

    
469
                                    modelItem.Commit();
470
                                    ReleaseCOMObjects(modelItem);
471
                                }
472
                            }
473
                        }
474
                        ReleaseCOMObjects(symbol);
475
                    }
476
                }
477
                SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
478
            }
479
            SetSystemEditingCommand(true);
480
        }
481
        private void RunEndBreakModeling()
482
        {
483
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetAllProgress, document.EndBreaks.Count);
484
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "EndBreaks Modeling");
485
            foreach (var item in document.EndBreaks)
486
                try
487
                {
488
                    EndBreakModeling(item);
489
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
490
                    if (string.IsNullOrEmpty(item.SPPID.ModelItemID))
491
                        Log.Write("Fail modeling endbreak UID : " + item.UID);
492
                }
493
                catch (Exception ex)
494
                {
495
                    Log.Write("Error in EndBreakModeling");
496
                    Log.Write("UID : " + item.UID);
497
                    Log.Write(ex.Message);
498
                    Log.Write(ex.StackTrace);
499
                }
500
        }
501
        private void RunSpecBreakModeling()
502
        {
503
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetAllProgress, document.SpecBreaks.Count);
504
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "SpecBreaks Modeling");
505
            foreach (var item in document.SpecBreaks)
506
                try
507
                {
508
                    SpecBreakModeling(item);
509
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
510
                    if (string.IsNullOrEmpty(item.SPPID.ModelItemID))
511
                        Log.Write("Fail modeling specbreak UID : " + item.UID);
512
                }
513
                catch (Exception ex)
514
                {
515
                    Log.Write("Error in SpecBreakModeling");
516
                    Log.Write("UID : " + item.UID);
517
                    Log.Write(ex.Message);
518
                    Log.Write(ex.StackTrace);
519
                }
520
        }
521

    
522
        private void RunLineNumberModeling()
523
        {
524
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "LineNumbers Modeling");
525
            foreach (var item in document.LINENUMBERS)
526
                try
527
                {
528
                    LineNumberModeling(item);
529
                }
530
                catch (Exception ex)
531
                {
532
                    Log.Write("Error in LineNumberModeling");
533
                    Log.Write("UID : " + item.UID);
534
                    Log.Write(ex.Message);
535
                    Log.Write(ex.StackTrace);
536
                }
537
        }
538

    
539
        /// <summary>
540
        /// 도면 생성 메서드
541
        /// </summary>
542
        private void CreateDocument(ref string drawingNumber, ref string drawingName)
543
        {
544
            Log.Write("------------------ Start create document ------------------");
545
            GetDrawingNameAndNumber(ref drawingName, ref drawingNumber);
546
            Log.Write("Drawing name : " + drawingName);
547
            Log.Write("Drawing number : " + drawingNumber);
548
            newDrawing = application.Drawings.Add(document.Unit, document.Template, drawingNumber, drawingName);
549
            document.SPPID_DrawingNumber = drawingNumber;
550
            document.SPPID_DrawingName = drawingName;
551
            application.ActiveWindow.Fit();
552
            Thread.Sleep(1000);
553
            application.ActiveWindow.Zoom = 2000;
554
            Thread.Sleep(2000);
555
        }
556

    
557
        /// <summary>
558
        /// DrawingName, DrawingNumber를 확인하여 중복이 있으면 _1을 붙이고 +1씩 한다.
559
        /// </summary>
560
        /// <param name="drawingName"></param>
561
        /// <param name="drawingNumber"></param>
562
        private void GetDrawingNameAndNumber(ref string drawingName, ref string drawingNumber)
563
        {
564
            LMDrawings drawings = new LMDrawings();
565
            drawings.Collect(dataSource);
566

    
567
            List<string> drawingNameList = new List<string>();
568
            List<string> drawingNumberList = new List<string>();
569

    
570
            foreach (LMDrawing item in drawings)
571
            {
572
                drawingNameList.Add(item.Attributes["Name"].get_Value().ToString());
573
                drawingNumberList.Add(item.Attributes["DrawingNumber"].get_Value().ToString());
574
            }
575

    
576
            int nameLength = drawingName.Length;
577
            while (drawingNameList.Contains(drawingName))
578
            {
579
                if (nameLength == drawingName.Length)
580
                    drawingName += "-1";
581
                else
582
                {
583
                    int index = Convert.ToInt32(drawingName.Remove(0, nameLength + 1));
584
                    drawingName = drawingName.Substring(0, nameLength + 1);
585
                    drawingName += ++index;
586
                }
587
            }
588

    
589
            int numberLength = drawingNumber.Length;
590
            while (drawingNameList.Contains(drawingNumber))
591
            {
592
                if (numberLength == drawingNumber.Length)
593
                    drawingNumber += "-1";
594
                else
595
                {
596
                    int index = Convert.ToInt32(drawingNumber.Remove(0, numberLength + 1));
597
                    drawingNumber = drawingNumber.Substring(0, numberLength + 1);
598
                    drawingNumber += ++index;
599
                }
600
            }
601

    
602
            ReleaseCOMObjects(drawings);
603
        }
604

    
605
        /// <summary>
606
        /// 도면 크기 구하는 메서드
607
        /// </summary>
608
        /// <returns></returns>
609
        private bool DocumentCoordinateCorrection()
610
        {
611
            if (Settings.Default.DrawingX != 0 && Settings.Default.DrawingY != 0)
612
            {
613
                Log.Write("Setting Drawing X, Drawing Y");
614
                document.SetSPPIDLocation(Settings.Default.DrawingX, Settings.Default.DrawingY);
615
                Log.Write("Start coordinate correction");
616
                document.CoordinateCorrection();
617
                return true;
618
            }
619
            else
620
            {
621
                Log.Write("Need Drawing X, Y");
622
                return false;
623
            }
624
        }
625

    
626
        /// <summary>
627
        /// 심볼을 실제로 Modeling 메서드
628
        /// </summary>
629
        /// <param name="symbol"></param>
630
        /// <param name="targetSymbol"></param>
631
        /// <param name="prevSymbol"></param>
632
        private void SymbolModeling(Symbol symbol, Symbol targetSymbol)
633
        {
634
            // OWNERSYMBOL Attribute, 값을 가지고 있을 경우
635
            BaseModel.Attribute itemAttribute = symbol.ATTRIBUTES.Find(attr => attr.ATTRIBUTE == "OWNERSYMBOL");
636
            if (itemAttribute != null && (string.IsNullOrEmpty(itemAttribute.VALUE) || itemAttribute.VALUE != "None"))
637
                return;
638
            // 이미 모델링 됐을 경우
639
            else if (!string.IsNullOrEmpty(symbol.SPPID.RepresentationId))
640
                return;
641

    
642
            LMSymbol _LMSymbol = null;
643

    
644
            string mappingPath = symbol.SPPID.MAPPINGNAME;
645
            double x = symbol.SPPID.ORIGINAL_X;
646
            double y = symbol.SPPID.ORIGINAL_Y;
647
            int mirror = 0;
648
            double angle = symbol.ANGLE;
649

    
650
            // OPC 일경우 180도 일때 Mirror
651
            if (mappingPath.Contains("Piping OPC's") && angle == Math.PI)
652
                mirror = 1;
653

    
654
            // Mirror 계산
655
            if (symbol.FLIP == 1)
656
            {
657
                mirror = 1;
658
                angle += Math.PI;
659
            }
660

    
661
            if (targetSymbol != null && !string.IsNullOrEmpty(targetSymbol.SPPID.RepresentationId))
662
            {
663
                LMSymbol _TargetItem = dataSource.GetSymbol(targetSymbol.SPPID.RepresentationId);
664
                Connector connector = SPPIDUtil.FindSymbolConnectorByUID(document, symbol.UID, targetSymbol);
665
                if (connector != null)
666
                    GetTargetSymbolConnectorPoint(connector, targetSymbol, ref x, ref y);
667

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

    
670
                if (_LMSymbol != null && _TargetItem != null)
671
                {
672
                    symbol.SPPID.RepresentationId = _LMSymbol.AsLMRepresentation().Id;
673
                    LMConnector reModelingConnector = FindBreakLineTarget(symbol, targetSymbol);
674

    
675
                    if (reModelingConnector != null)
676
                        ReModelingLMConnector(reModelingConnector);
677
                }
678

    
679
                ReleaseCOMObjects(_TargetItem);
680
            }
681
            else
682
                _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle);
683

    
684
            if (_LMSymbol != null)
685
            {
686
                _LMSymbol.Commit();
687
                symbol.SPPID.RepresentationId = _LMSymbol.AsLMRepresentation().Id;
688
                symbol.SPPID.ModelItemID = _LMSymbol.ModelItemID;
689
                symbol.SPPID.GraphicOID = _LMSymbol.get_GraphicOID();
690

    
691
                foreach (var item in symbol.ChildSymbols)
692
                    CreateChildSymbol(item, _LMSymbol);
693

    
694
                symbol.SPPID.SPPID_X = _LMSymbol.get_XCoordinate();
695
                symbol.SPPID.SPPID_Y = _LMSymbol.get_YCoordinate();
696

    
697
                double[] range = null;
698
                GetSPPIDSymbolRange(symbol, ref range);
699
                symbol.SPPID.SPPID_Min_X = range[0];
700
                symbol.SPPID.SPPID_Min_Y = range[1];
701
                symbol.SPPID.SPPID_Max_X = range[2];
702
                symbol.SPPID.SPPID_Max_Y = range[3];
703

    
704
                foreach (var item in symbol.SPPID.CorrectionX_GroupSymbols)
705
                    item.SPPID.ORIGINAL_X = symbol.SPPID.SPPID_X;
706
                foreach (var item in symbol.SPPID.CorrectionY_GroupSymbols)
707
                    item.SPPID.ORIGINAL_Y = symbol.SPPID.SPPID_Y;
708

    
709
                ReleaseCOMObjects(_LMSymbol);
710
            }
711
        }
712

    
713
        private void RemoveSymbol(Symbol symbol)
714
        {
715
            if (!string.IsNullOrEmpty(symbol.SPPID.RepresentationId))
716
            {
717
                LMSymbol _LMSymbol = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
718
                if (_LMSymbol != null)
719
                {
720
                    _placement.PIDRemovePlacement(_LMSymbol.AsLMRepresentation());
721
                    ReleaseCOMObjects(_LMSymbol);
722
                }
723
            }
724

    
725
            symbol.SPPID.RepresentationId = string.Empty;
726
            symbol.SPPID.ModelItemID = string.Empty;
727
            symbol.SPPID.SPPID_X = double.NaN;
728
            symbol.SPPID.SPPID_Y = double.NaN;
729
            symbol.SPPID.SPPID_Min_X = double.NaN;
730
            symbol.SPPID.SPPID_Min_Y = double.NaN;
731
            symbol.SPPID.SPPID_Max_X = double.NaN;
732
            symbol.SPPID.SPPID_Max_Y = double.NaN;
733
        }
734

    
735
        private void RemoveSymbol(List<Symbol> symbols)
736
        {
737
            foreach (var symbol in symbols)
738
            {
739
                if (!string.IsNullOrEmpty(symbol.SPPID.RepresentationId))
740
                {
741
                    LMSymbol _LMSymbol = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
742
                    if (_LMSymbol != null)
743
                    {
744
                        _placement.PIDRemovePlacement(_LMSymbol.AsLMRepresentation());
745
                        ReleaseCOMObjects(_LMSymbol);
746
                    }
747
                }
748

    
749
                symbol.SPPID.RepresentationId = string.Empty;
750
                symbol.SPPID.ModelItemID = string.Empty;
751
                symbol.SPPID.SPPID_X = double.NaN;
752
                symbol.SPPID.SPPID_Y = double.NaN;
753
                symbol.SPPID.SPPID_Min_X = double.NaN;
754
                symbol.SPPID.SPPID_Min_Y = double.NaN;
755
                symbol.SPPID.SPPID_Max_X = double.NaN;
756
                symbol.SPPID.SPPID_Max_Y = double.NaN;
757
            }
758
        }
759

    
760
        /// <summary>
761
        /// ID2의 Symbol Width와 Height를 비교해서 상대적인 SPPID Connector좌표를 가져온다.
762
        /// </summary>
763
        /// <param name="targetConnector"></param>
764
        /// <param name="targetSymbol"></param>
765
        /// <param name="x"></param>
766
        /// <param name="y"></param>
767
        private void GetTargetSymbolConnectorPoint(Connector targetConnector, Symbol targetSymbol, ref double x, ref double y)
768
        {
769
            LMSymbol _TargetItem = dataSource.GetSymbol(targetSymbol.SPPID.RepresentationId);
770

    
771
            double[] range = null;
772
            List<double[]> points = new List<double[]>();
773
            GetSPPIDSymbolRangeAndConnectionPoints(targetSymbol, ref range, points);
774
            double x1 = range[0];
775
            double y1 = range[1];
776
            double x2 = range[2];
777
            double y2 = range[3];
778

    
779
            // Origin 기준 Connector의 위치차이
780
            double sceneX = 0;
781
            double sceneY = 0;
782
            SPPIDUtil.ConvertPointBystring(targetConnector.SCENECONNECTPOINT, ref sceneX, ref sceneY);
783
            double originX = 0;
784
            double originY = 0;
785
            SPPIDUtil.ConvertPointBystring(targetSymbol.ORIGINALPOINT, ref originX, ref originY);
786
            double gapX = originX - sceneX;
787
            double gapY = originY - sceneY;
788

    
789
            // SPPID Symbol과 ID2 심볼의 크기 차이
790
            double sizeWidth = 0;
791
            double sizeHeight = 0;
792
            SPPIDUtil.ConvertPointBystring(targetSymbol.SIZE, ref sizeWidth, ref sizeHeight);
793
            if (sizeWidth == 0 || sizeHeight == 0)
794
                throw new Exception("Check symbol size! \r\nUID : " + targetSymbol.UID);
795

    
796
            double percentX = (x2 - x1) / sizeWidth;
797
            double percentY = (y2 - y1) / sizeHeight;
798

    
799
            double SPPIDgapX = gapX * percentX;
800
            double SPPIDgapY = gapY * percentY;
801

    
802
            double[] SPPIDOriginPoint = new double[] { _TargetItem.get_XCoordinate() - SPPIDgapX, _TargetItem.get_YCoordinate() + SPPIDgapY };
803
            double distance = double.MaxValue;
804
            double[] resultPoint;
805
            foreach (var point in points)
806
            {
807
                double result = SPPIDUtil.CalcPointToPointdDistance(point[0], point[1], SPPIDOriginPoint[0], SPPIDOriginPoint[1]);
808
                if (distance > result)
809
                {
810
                    distance = result;
811
                    resultPoint = point;
812
                    x = point[0];
813
                    y = point[1];
814
                }
815
            }
816

    
817
            ReleaseCOMObjects(_TargetItem);
818
        }
819

    
820
        private void GetTargetLineConnectorPoint(Connector targetConnector, Line targetLine, ref double x, ref double y)
821
        {
822
            int index = targetLine.CONNECTORS.IndexOf(targetConnector);
823
            if (index == 0)
824
            {
825
                x = targetLine.SPPID.START_X;
826
                y = targetLine.SPPID.START_Y;
827
            }
828
            else
829
            {
830
                x = targetLine.SPPID.END_X;
831
                y = targetLine.SPPID.END_Y;
832
            }
833
        }
834

    
835
        /// <summary>
836
        /// SPPID Symbol의 Range를 구한다.
837
        /// </summary>
838
        /// <param name="symbol"></param>
839
        /// <param name="range"></param>
840
        private void GetSPPIDSymbolRangeAndConnectionPoints(Symbol symbol, ref double[] range, List<double[]> points)
841
        {
842
            LMSymbol _TargetItem = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
843
            Ingr.RAD2D.Symbol2d symbol2d = radApp.ActiveDocument.ActiveSheet.DrawingObjects[_TargetItem.get_GraphicOID().ToString()];
844
            double x1 = 0;
845
            double y1 = 0;
846
            double x2 = 0;
847
            double y2 = 0;
848
            symbol2d.Range(out x1, out y1, out x2, out y2);
849
            range = new double[] { x1, y1, x2, y2 };
850

    
851
            for (int i = 1; i < int.MaxValue; i++)
852
            {
853
                double connX = 0;
854
                double connY = 0;
855
                if (_placement.PIDConnectPointLocation(_TargetItem, i, ref connX, ref connY))
856
                    points.Add(new double[] { connX, connY });
857
                else
858
                    break;
859
            }
860

    
861
            foreach (var childSymbol in symbol.ChildSymbols)
862
                GetSPPIDChildSymbolRange(childSymbol, ref range, points);
863

    
864
            ReleaseCOMObjects(_TargetItem);
865
        }
866

    
867
        private void GetSPPIDSymbolRange(Symbol symbol, ref double[] range)
868
        {
869
            LMSymbol _TargetItem = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
870
            Ingr.RAD2D.Symbol2d symbol2d = radApp.ActiveDocument.ActiveSheet.DrawingObjects[_TargetItem.get_GraphicOID().ToString()];
871
            double x1 = 0;
872
            double y1 = 0;
873
            double x2 = 0;
874
            double y2 = 0;
875
            symbol2d.Range(out x1, out y1, out x2, out y2);
876
            range = new double[] { x1, y1, x2, y2 };
877

    
878
            foreach (var childSymbol in symbol.ChildSymbols)
879
                GetSPPIDChildSymbolRange(childSymbol, ref range);
880

    
881
            ReleaseCOMObjects(_TargetItem);
882
        }
883

    
884
        private void GetSPPIDSymbolRange(List<Symbol> symbols, ref double[] range)
885
        {
886
            double[] tempRange = new double[] { double.MaxValue, double.MaxValue, double.MinValue, double.MinValue };
887
            foreach (var symbol in symbols)
888
            {
889
                LMSymbol _TargetItem = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
890
                Ingr.RAD2D.Symbol2d symbol2d = radApp.ActiveDocument.ActiveSheet.DrawingObjects[_TargetItem.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

    
897
                tempRange[0] = Math.Min(tempRange[0], x1);
898
                tempRange[1] = Math.Min(tempRange[1], y1);
899
                tempRange[2] = Math.Max(tempRange[2], x2);
900
                tempRange[3] = Math.Max(tempRange[3], y2);
901

    
902
                foreach (var childSymbol in symbol.ChildSymbols)
903
                    GetSPPIDChildSymbolRange(childSymbol, ref tempRange);
904

    
905
                ReleaseCOMObjects(_TargetItem);
906
            }
907

    
908
            range = tempRange;
909
        }
910

    
911
        /// <summary>
912
        /// Child Modeling 된 Symbol의 Range를 구한다.
913
        /// </summary>
914
        /// <param name="childSymbol"></param>
915
        /// <param name="range"></param>
916
        private void GetSPPIDChildSymbolRange(ChildSymbol childSymbol, ref double[] range, List<double[]> points)
917
        {
918
            LMSymbol _ChildSymbol = dataSource.GetSymbol(childSymbol.SPPID.RepresentationId);
919
            Ingr.RAD2D.Symbol2d symbol2d = radApp.ActiveDocument.ActiveSheet.DrawingObjects[_ChildSymbol.get_GraphicOID().ToString()];
920
            double x1 = 0;
921
            double y1 = 0;
922
            double x2 = 0;
923
            double y2 = 0;
924
            symbol2d.Range(out x1, out y1, out x2, out y2);
925
            range[0] = Math.Min(range[0], x1);
926
            range[1] = Math.Min(range[1], y1);
927
            range[2] = Math.Max(range[2], x2);
928
            range[3] = Math.Max(range[3], y2);
929

    
930
            for (int i = 1; i < int.MaxValue; i++)
931
            {
932
                double connX = 0;
933
                double connY = 0;
934
                if (_placement.PIDConnectPointLocation(_ChildSymbol, i, ref connX, ref connY))
935
                    points.Add(new double[] { connX, connY });
936
                else
937
                    break;
938
            }
939

    
940
            foreach (var loopChildSymbol in childSymbol.ChildSymbols)
941
                GetSPPIDChildSymbolRange(loopChildSymbol, ref range, points);
942

    
943
            ReleaseCOMObjects(_ChildSymbol);
944
        }
945

    
946
        private void GetSPPIDChildSymbolRange(ChildSymbol childSymbol, ref double[] range)
947
        {
948
            LMSymbol _ChildSymbol = dataSource.GetSymbol(childSymbol.SPPID.RepresentationId);
949
            Ingr.RAD2D.Symbol2d symbol2d = radApp.ActiveDocument.ActiveSheet.DrawingObjects[_ChildSymbol.get_GraphicOID().ToString()];
950
            double x1 = 0;
951
            double y1 = 0;
952
            double x2 = 0;
953
            double y2 = 0;
954
            symbol2d.Range(out x1, out y1, out x2, out y2);
955
            range[0] = Math.Min(range[0], x1);
956
            range[1] = Math.Min(range[1], y1);
957
            range[2] = Math.Max(range[2], x2);
958
            range[3] = Math.Max(range[3], y2);
959

    
960
            foreach (var loopChildSymbol in childSymbol.ChildSymbols)
961
                GetSPPIDChildSymbolRange(loopChildSymbol, ref range);
962

    
963
            ReleaseCOMObjects(_ChildSymbol);
964
        }
965

    
966
        /// <summary>
967
        /// Label Symbol Modeling
968
        /// </summary>
969
        /// <param name="symbol"></param>
970
        private void LabelSymbolModeling(Symbol symbol)
971
        {
972
            if (string.IsNullOrEmpty(symbol.SPPID.RepresentationId))
973
            {
974
                BaseModel.Attribute itemAttribute = symbol.ATTRIBUTES.Find(x => x.ATTRIBUTE == "OWNERSYMBOL");
975
                if (itemAttribute == null || string.IsNullOrEmpty(itemAttribute.VALUE) || itemAttribute.VALUE == "None")
976
                    return;
977
                Array points = new double[] { 0, symbol.SPPID.ORIGINAL_X, symbol.SPPID.ORIGINAL_Y };
978

    
979
                string symbolUID = itemAttribute.VALUE;
980
                object targetItem = SPPIDUtil.FindObjectByUID(document, symbolUID);
981
                if (targetItem != null &&
982
                    (targetItem.GetType() == typeof(Symbol) ||
983
                    targetItem.GetType() == typeof(Equipment)))
984
                {
985
                    // Object 아이템이 Symbol일 경우 Equipment일 경우 
986
                    string sRep = null;
987
                    if (targetItem.GetType() == typeof(Symbol))
988
                        sRep = ((Symbol)targetItem).SPPID.RepresentationId;
989
                    else if (targetItem.GetType() == typeof(Equipment))
990
                        sRep = ((Equipment)targetItem).SPPID.RepresentationId;
991
                    if (!string.IsNullOrEmpty(sRep))
992
                    {
993
                        // LEADER Line 검사
994
                        bool leaderLine = false;
995
                        SymbolMapping symbolMapping = document.SymbolMappings.Find(x => x.UID == symbol.DBUID);
996
                        if (symbolMapping != null)
997
                            leaderLine = symbolMapping.LEADERLINE;
998

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

    
1003
                        //Leader 선 센터로
1004
                        if (_LMLabelPresist != null)
1005
                        {
1006
                            // Target Item에 Label의 Attribute Input
1007
                            InputSymbolAttribute(targetItem, symbol.ATTRIBUTES);
1008

    
1009
                            string OID = _LMLabelPresist.get_GraphicOID();
1010
                            DependencyObject dependency = radApp.ActiveDocument.ActiveSheet.DrawingObjects[OID] as DependencyObject;
1011
                            if (dependency != null)
1012
                            {
1013
                                bool result = false;
1014
                                foreach (var attributes in dependency.AttributeSets)
1015
                                {
1016
                                    foreach (var attribute in attributes)
1017
                                    {
1018
                                        string name = attribute.Name;
1019
                                        string value = attribute.GetValue().ToString();
1020
                                        if (name == "DrawingItemType" && value == "LabelPersist")
1021
                                        {
1022
                                            foreach (DrawingObjectBase drawingObject in dependency.DrawingObjects)
1023
                                            {
1024
                                                if (drawingObject.Type == Ingr.RAD2D.ObjectType.igLineString2d)
1025
                                                {
1026
                                                    Ingr.RAD2D.LineString2d lineString2D = drawingObject as Ingr.RAD2D.LineString2d;
1027
                                                    double prevX = _TargetItem.get_XCoordinate();
1028
                                                    double prevY = _TargetItem.get_YCoordinate();
1029
                                                    lineString2D.InsertVertex(lineString2D.VertexCount, prevX, prevY);
1030
                                                    lineString2D.RemoveVertex(lineString2D.VertexCount);
1031
                                                    result = true;
1032
                                                    break;
1033
                                                }
1034
                                            }
1035
                                        }
1036

    
1037
                                        if (result)
1038
                                            break;
1039
                                    }
1040

    
1041
                                    if (result)
1042
                                        break;
1043
                                }
1044
                            }
1045

    
1046
                            _LMLabelPresist.Commit();
1047
                            ReleaseCOMObjects(_LMLabelPresist);
1048
                        }
1049

    
1050
                        ReleaseCOMObjects(_TargetItem);
1051
                    }
1052
                }
1053
                else if (targetItem != null && targetItem.GetType() == typeof(Line))
1054
                {
1055
                    Line targetLine = targetItem as Line;
1056
                    Dictionary<LMConnector, List<double[]>> connectorVertices = GetPipeRunVertices(targetLine.SPPID.ModelItemId);
1057
                    LMConnector connectedLMConnector = FindTargetLMConnectorForLabel(connectorVertices, symbol.SPPID.ORIGINAL_X, symbol.SPPID.ORIGINAL_Y);
1058
                    if (connectedLMConnector != null)
1059
                    {
1060
                        // LEADER Line 검사
1061
                        bool leaderLine = false;
1062
                        SymbolMapping symbolMapping = document.SymbolMappings.Find(x => x.UID == symbol.DBUID);
1063
                        if (symbolMapping != null)
1064
                            leaderLine = symbolMapping.LEADERLINE;
1065

    
1066
                        LMLabelPersist _LMLabelPresist = _placement.PIDPlaceLabel(symbol.SPPID.MAPPINGNAME, ref points, Rotation: 0, LabeledItem: connectedLMConnector.AsLMRepresentation(), IsLeaderVisible: leaderLine);
1067
                        if (_LMLabelPresist != null)
1068
                        {
1069
                            _LMLabelPresist.Commit();
1070
                            ReleaseCOMObjects(_LMLabelPresist);
1071
                        }
1072
                        ReleaseCOMObjects(connectedLMConnector);
1073
                    }
1074

    
1075
                    foreach (var item in connectorVertices)
1076
                        if (item.Key != null)
1077
                            ReleaseCOMObjects(item.Key);
1078
                }
1079
            }
1080
        }
1081

    
1082
        /// <summary>
1083
        /// Equipment를 실제로 Modeling 메서드
1084
        /// </summary>
1085
        /// <param name="equipment"></param>
1086
        private void EquipmentModeling(Equipment equipment)
1087
        {
1088
            if (!string.IsNullOrEmpty(equipment.SPPID.RepresentationId))
1089
                return;
1090

    
1091
            LMSymbol _LMSymbol = null;
1092
            LMSymbol targetItem = null;
1093
            string mappingPath = equipment.SPPID.MAPPINGNAME;
1094
            double x = equipment.SPPID.ORIGINAL_X;
1095
            double y = equipment.SPPID.ORIGINAL_Y;
1096
            int mirror = 0;
1097
            double angle = equipment.ANGLE;
1098

    
1099
            SPPIDUtil.ConvertGridPoint(ref x, ref y);
1100

    
1101
            Connector connector = equipment.CONNECTORS.Find(conn => !string.IsNullOrEmpty(conn.CONNECTEDITEM) && conn.CONNECTEDITEM != "None");
1102
            if (connector != null)
1103
            {
1104
                Equipment connEquipment = SPPIDUtil.FindObjectByUID(document, connector.CONNECTEDITEM) as Equipment;
1105
                if (connEquipment != null)
1106
                {
1107
                    if (string.IsNullOrEmpty(connEquipment.SPPID.RepresentationId))
1108
                        EquipmentModeling(connEquipment);
1109

    
1110
                    if (!string.IsNullOrEmpty(connEquipment.SPPID.RepresentationId))
1111
                    {
1112
                        targetItem = dataSource.GetSymbol(connEquipment.SPPID.RepresentationId);
1113
                        if (targetItem != null)
1114
                        {
1115
                            _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle, TargetItem: targetItem);
1116
                        }
1117
                        else
1118
                        {
1119
                            _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle);
1120
                        }
1121
                    }
1122
                    else
1123
                    {
1124
                        _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle);
1125
                    }
1126
                }
1127
                else
1128
                {
1129
                    _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle);
1130
                }
1131
            }
1132
            else
1133
            {
1134
                _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle);
1135
            }
1136

    
1137
            if (_LMSymbol != null)
1138
            {
1139
                _LMSymbol.Commit();
1140
                equipment.SPPID.RepresentationId = _LMSymbol.AsLMRepresentation().Id;
1141
                equipment.SPPID.GraphicOID = _LMSymbol.get_GraphicOID();
1142
                ReleaseCOMObjects(_LMSymbol);
1143
            }
1144

    
1145
            if (targetItem != null)
1146
            {
1147
                ReleaseCOMObjects(targetItem);
1148
            }
1149

    
1150
            ReleaseCOMObjects(_LMSymbol);
1151
        }
1152

    
1153
        /// <summary>
1154
        /// 첫 진입점
1155
        /// </summary>
1156
        /// <param name="symbol"></param>
1157
        private void SymbolModelingBySymbol(Symbol symbol)
1158
        {
1159
            SymbolModeling(symbol, null);
1160
            List<object> endObjects = new List<object>();
1161
            endObjects.Add(symbol);
1162

    
1163
            foreach (var connector in symbol.CONNECTORS)
1164
            {
1165
                object connItem = SPPIDUtil.FindObjectByUID(document, connector.CONNECTEDITEM);
1166
                if (connItem != null && connItem.GetType() != typeof(Equipment))
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
        private void SymbolModelingByNeerSymbolLoop(Symbol symbol, List<object> endObjects)
1189
        {
1190
            foreach (var connector in symbol.CONNECTORS)
1191
            {
1192
                object connItem = SPPIDUtil.FindObjectByUID(document, connector.CONNECTEDITEM);
1193
                if (connItem != null && connItem.GetType() != typeof(Equipment))
1194
                {
1195
                    if (!endObjects.Contains(connItem))
1196
                    {
1197
                        endObjects.Add(connItem);
1198
                        if (connItem.GetType() == typeof(Symbol))
1199
                        {
1200
                            Symbol connSymbol = connItem as Symbol;
1201
                            if (string.IsNullOrEmpty(connSymbol.SPPID.RepresentationId))
1202
                            {
1203
                                SymbolModeling(connSymbol, symbol);
1204
                            }
1205
                            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetProgress, document.SYMBOLS.FindAll(x => !string.IsNullOrEmpty(x.SPPID.RepresentationId)).Count);
1206
                            SymbolModelingByNeerSymbolLoop(connSymbol, endObjects);
1207
                        }
1208
                        else if (connItem.GetType() == typeof(Line))
1209
                        {
1210
                            Line connLine = connItem as Line;
1211
                            SymbolModelingByNeerLineLoop(connLine, endObjects, symbol);
1212
                        }
1213
                    }
1214
                }
1215
            }
1216
        }
1217

    
1218
        private void SymbolModelingByNeerLineLoop(Line line, List<object> endObjects, Symbol prevSymbol)
1219
        {
1220
            foreach (var connector in line.CONNECTORS)
1221
            {
1222
                object connItem = SPPIDUtil.FindObjectByUID(document, connector.CONNECTEDITEM);
1223
                if (connItem != null && connItem.GetType() != typeof(Equipment))
1224
                {
1225
                    if (!endObjects.Contains(connItem))
1226
                    {
1227
                        endObjects.Add(connItem);
1228
                        if (connItem.GetType() == typeof(Symbol))
1229
                        {
1230
                            Symbol connSymbol = connItem as Symbol;
1231
                            if (string.IsNullOrEmpty(connSymbol.SPPID.RepresentationId))
1232
                            {
1233
                                List<Symbol> group = new List<Symbol>();
1234
                                SPPIDUtil.FindConnectedSymbolGroup(document, connSymbol, group);
1235
                                Symbol priority = prioritySymbols.Find(x => group.Contains(x));
1236
                                List<Symbol> endModelingGroup = new List<Symbol>();
1237
                                if (priority != null)
1238
                                {
1239
                                    SymbolGroupModeling(priority, group);
1240

    
1241
                                    // Range 겹치는지 확인해야함
1242
                                    double[] prevRange = null;
1243
                                    GetSPPIDSymbolRange(prevSymbol, ref prevRange);
1244
                                    double[] groupRange = null;
1245
                                    GetSPPIDSymbolRange(group, ref groupRange);
1246

    
1247
                                    double distanceX = 0;
1248
                                    double distanceY = 0;
1249
                                    bool overlapX = false;
1250
                                    bool overlapY = false;
1251
                                    SlopeType slopeType = SPPIDUtil.CalcSlope(prevSymbol.SPPID.ORIGINAL_X, prevSymbol.SPPID.ORIGINAL_Y, connSymbol.SPPID.ORIGINAL_X, connSymbol.SPPID.ORIGINAL_Y);
1252
                                    SPPIDUtil.CalcOverlap(prevRange, groupRange, ref distanceX, ref distanceY, ref overlapX, ref overlapY);
1253
                                    if ((slopeType == SlopeType.HORIZONTAL && overlapX) ||
1254
                                        (slopeType == SlopeType.VERTICAL && overlapY))
1255
                                    {
1256
                                        RemoveSymbol(group);
1257
                                        foreach (var _temp in group)
1258
                                            SPPIDUtil.CalcNewCoordinateForSymbol(_temp, prevSymbol, distanceX, distanceY);
1259

    
1260
                                        SymbolGroupModeling(priority, group);
1261
                                    }
1262
                                }
1263
                                else
1264
                                {
1265
                                    SymbolModeling(connSymbol, null);
1266
                                    // Range 겹치는지 확인해야함
1267
                                    double[] prevRange = null;
1268
                                    GetSPPIDSymbolRange(prevSymbol, ref prevRange);
1269
                                    double[] connRange = null;
1270
                                    GetSPPIDSymbolRange(connSymbol, ref connRange);
1271

    
1272
                                    double distanceX = 0;
1273
                                    double distanceY = 0;
1274
                                    bool overlapX = false;
1275
                                    bool overlapY = false;
1276
                                    SlopeType slopeType = SPPIDUtil.CalcSlope(prevSymbol.SPPID.ORIGINAL_X, prevSymbol.SPPID.ORIGINAL_Y, connSymbol.SPPID.ORIGINAL_X, connSymbol.SPPID.ORIGINAL_Y);
1277
                                    SPPIDUtil.CalcOverlap(prevRange, connRange, ref distanceX, ref distanceY, ref overlapX, ref overlapY);
1278
                                    if ((slopeType == SlopeType.HORIZONTAL && overlapX) ||
1279
                                        (slopeType == SlopeType.VERTICAL && overlapY))
1280
                                    {
1281
                                        RemoveSymbol(connSymbol);
1282
                                        SPPIDUtil.CalcNewCoordinateForSymbol(connSymbol, prevSymbol, distanceX, distanceY);
1283

    
1284
                                        SymbolModeling(connSymbol, null);
1285
                                    }
1286
                                }
1287
                            }
1288
                            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetProgress, document.SYMBOLS.FindAll(x => !string.IsNullOrEmpty(x.SPPID.RepresentationId)).Count);
1289
                            SymbolModelingByNeerSymbolLoop(connSymbol, endObjects);
1290
                        }
1291
                        else if (connItem.GetType() == typeof(Line))
1292
                        {
1293
                            Line connLine = connItem as Line;
1294
                            if (!SPPIDUtil.IsBranchLine(connLine, line))
1295
                                SymbolModelingByNeerLineLoop(connLine, endObjects, prevSymbol);
1296
                        }
1297
                    }
1298
                }
1299
            }
1300
        }
1301

    
1302
        private void SymbolGroupModeling(Symbol firstSymbol, List<Symbol> group)
1303
        {
1304
            List<Symbol> endModelingGroup = new List<Symbol>();
1305
            SymbolModeling(firstSymbol, null);
1306
            endModelingGroup.Add(firstSymbol);
1307
            while (endModelingGroup.Count != group.Count)
1308
            {
1309
                foreach (var _symbol in group)
1310
                {
1311
                    if (!endModelingGroup.Contains(_symbol))
1312
                    {
1313
                        foreach (var _connector in _symbol.CONNECTORS)
1314
                        {
1315
                            Symbol _connSymbol = SPPIDUtil.FindObjectByUID(document, _connector.CONNECTEDITEM) as Symbol;
1316
                            if (_connSymbol != null && endModelingGroup.Contains(_connSymbol))
1317
                            {
1318
                                SymbolModeling(_symbol, _connSymbol);
1319
                                endModelingGroup.Add(_symbol);
1320
                                break;
1321
                            }
1322
                        }
1323
                    }
1324
                }
1325
            }
1326
        }
1327

    
1328
        /// <summary>
1329
        /// 심볼을 실제로 Modeling할때 ChildSymbol이 있다면 Modeling하는 메서드
1330
        /// </summary>
1331
        /// <param name="childSymbol"></param>
1332
        /// <param name="parentSymbol"></param>
1333
        private void CreateChildSymbol(ChildSymbol childSymbol, LMSymbol parentSymbol)
1334
        {
1335
            Ingr.RAD2D.Symbol2d symbol2d = radApp.ActiveDocument.ActiveSheet.DrawingObjects[parentSymbol.get_GraphicOID().ToString()];
1336
            double x1 = 0;
1337
            double x2 = 0;
1338
            double y1 = 0;
1339
            double y2 = 0;
1340
            symbol2d.Range(out x1, out y1, out x2, out y2);
1341

    
1342
            LMSymbol _LMSymbol = _placement.PIDPlaceSymbol(childSymbol.SPPID.MAPPINGNAME, (x1 + x2) / 2, (y1 + y2) / 2, TargetItem: parentSymbol);
1343
            if (_LMSymbol != null)
1344
            {
1345
                childSymbol.SPPID.RepresentationId = _LMSymbol.AsLMRepresentation().Id;
1346
                foreach (var item in childSymbol.ChildSymbols)
1347
                    CreateChildSymbol(item, _LMSymbol);
1348
            }
1349

    
1350

    
1351
            ReleaseCOMObjects(_LMSymbol);
1352
        }
1353

    
1354
        private void NewLineModeling(Line line, bool isBranchModeling = false, Tuple<LMConnector, bool, bool> reModelingInfo = null)
1355
        {
1356
            if (!string.IsNullOrEmpty(line.SPPID.ModelItemId) || (NewBranchLines.Contains(line) && !isBranchModeling))
1357
                return;
1358

    
1359
            List<Line> group = new List<Line>();
1360
            GetConnectedLineGroup(line, group);
1361
            LineCoordinateCorrection(group);
1362

    
1363
            foreach (var groupLine in group)
1364
            {
1365
                if (!isBranchModeling && SPPIDUtil.IsBranchLine(groupLine))
1366
                {
1367
                    NewBranchLines.Add(groupLine);
1368
                    continue;
1369
                }
1370

    
1371
                _LMAItem _LMAItem = _placement.PIDCreateItem(groupLine.SPPID.MAPPINGNAME);
1372
                LMSymbol _LMSymbolStart = null;
1373
                LMSymbol _LMSymbolEnd = null;
1374
                PlaceRunInputs placeRunInputs = new PlaceRunInputs();
1375
                foreach (var connector in groupLine.CONNECTORS)
1376
                {
1377
                    double x = 0;
1378
                    double y = 0;
1379
                    GetTargetLineConnectorPoint(connector, groupLine, ref x, ref y);
1380
                    if (connector.ConnectedObject == null)
1381
                    {
1382
                        placeRunInputs.AddPoint(x, y);
1383
                    }
1384
                    else if (connector.ConnectedObject.GetType() == typeof(Symbol))
1385
                    {
1386
                        Symbol targetSymbol = connector.ConnectedObject as Symbol;
1387
                        if (groupLine.CONNECTORS.IndexOf(connector) == 0)
1388
                        {
1389
                            _LMSymbolStart = GetTargetSymbol(targetSymbol, groupLine);
1390
                            placeRunInputs.AddSymbolTarget(_LMSymbolStart, x, y);
1391
                        }
1392
                        else
1393
                        {
1394
                            _LMSymbolEnd = GetTargetSymbol(targetSymbol, groupLine);
1395
                            placeRunInputs.AddSymbolTarget(_LMSymbolEnd, x, y);
1396
                        }
1397
                    }
1398
                    else if (connector.ConnectedObject.GetType() == typeof(Line))
1399
                    {
1400
                        Line targetLine = connector.ConnectedObject as Line;
1401
                        if (!string.IsNullOrEmpty(targetLine.SPPID.ModelItemId))
1402
                        {
1403
                            LMConnector targetConnector = FindTargetLMConnectorForBranch(line, targetLine, ref x, ref y);
1404
                            placeRunInputs.AddConnectorTarget(targetConnector, x, y);
1405
                            ChangeLineSPPIDCoordinateByConnector(groupLine, targetLine, x, y, false);
1406
                        }
1407
                        else
1408
                        {
1409
                            if (groupLine.CONNECTORS.IndexOf(connector) == 0)
1410
                            {
1411
                                if (groupLine.SlopeType == SlopeType.HORIZONTAL)
1412
                                    placeRunInputs.AddPoint(x, -0.1);
1413
                                else if (groupLine.SlopeType == SlopeType.VERTICAL)
1414
                                    placeRunInputs.AddPoint(-0.1, y);
1415
                                else
1416
                                    placeRunInputs.AddPoint(x, -0.1);
1417
                            }
1418

    
1419
                            placeRunInputs.AddPoint(x, y);
1420

    
1421
                            if (groupLine.CONNECTORS.IndexOf(connector) == 1)
1422
                            {
1423
                                if (groupLine.SlopeType == SlopeType.HORIZONTAL)
1424
                                    placeRunInputs.AddPoint(x, -0.1);
1425
                                else if (groupLine.SlopeType == SlopeType.VERTICAL)
1426
                                    placeRunInputs.AddPoint(-0.1, y);
1427
                                else
1428
                                    placeRunInputs.AddPoint(x, -0.1);
1429
                            }
1430
                        }
1431
                    }
1432
                }
1433

    
1434
                LMConnector _lMConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
1435
                if (_lMConnector != null)
1436
                {
1437
                    groupLine.SPPID.ModelItemId = _lMConnector.ModelItemID;
1438
                    bool bRemodelingStart = false;
1439
                    if (_LMSymbolStart != null)
1440
                        NeedReModeling(groupLine, _LMSymbolStart, ref bRemodelingStart);
1441
                    bool bRemodelingEnd = false;
1442
                    if (_LMSymbolEnd != null)
1443
                        NeedReModeling(groupLine, _LMSymbolEnd, ref bRemodelingEnd);
1444

    
1445
                    if (bRemodelingStart || bRemodelingEnd)
1446
                        ReModelingLine(groupLine, _lMConnector, _LMSymbolStart, _LMSymbolEnd, bRemodelingStart, bRemodelingEnd);
1447
                    
1448
                    ReleaseCOMObjects(_lMConnector);
1449
                }
1450

    
1451
                List<object> removeLines = groupLine.CONNECTORS.FindAll(x =>
1452
                x.ConnectedObject != null &&
1453
                x.ConnectedObject.GetType() == typeof(Line) &&
1454
                !string.IsNullOrEmpty(((Line)x.ConnectedObject).SPPID.ModelItemId))
1455
                .Select(x => x.ConnectedObject)
1456
                .ToList();
1457

    
1458
                foreach (var item in removeLines)
1459
                    RemoveLineForModeling(item as Line);
1460

    
1461
                if (_LMAItem != null)
1462
                    ReleaseCOMObjects(_LMAItem);
1463
                if (placeRunInputs != null)
1464
                    ReleaseCOMObjects(placeRunInputs);
1465
                if (_LMSymbolStart != null)
1466
                    ReleaseCOMObjects(_LMSymbolStart);
1467
                if (_LMSymbolEnd != null)
1468
                    ReleaseCOMObjects(_LMSymbolEnd);
1469

    
1470
                if (isBranchModeling && NewBranchLines.Contains(groupLine))
1471
                    NewBranchLines.Remove(groupLine);
1472
            }
1473
        }
1474

    
1475
        private void RemoveLineForModeling(Line line)
1476
        {
1477
            LMModelItem modelItem = dataSource.GetModelItem(line.SPPID.ModelItemId);
1478
            if (modelItem != null)
1479
            {
1480
                foreach (LMRepresentation rep in modelItem.Representations)
1481
                {
1482
                    if (rep.Attributes["RepresentationType"].get_Value() == "Connector" && rep.Attributes["ItemStatus"].get_Value() == "Active")
1483
                    {
1484
                        LMConnector _LMConnector = dataSource.GetConnector(rep.Id);
1485
                        dynamic OID = rep.get_GraphicOID();
1486
                        DependencyObject drawingObject = radApp.ActiveDocument.ActiveSheet.DrawingObjects[OID];
1487
                        Ingr.RAD2D.LineStringGeometry2d lineStringGeometry = drawingObject.GetGeometry() as Ingr.RAD2D.LineStringGeometry2d;
1488
                        int verticesCount = lineStringGeometry.VertexCount;
1489
                        double[] vertices = null;
1490
                        lineStringGeometry.GetVertices(ref verticesCount, ref vertices);
1491
                        for (int i = 0; i < verticesCount; i++)
1492
                        {
1493
                            double x = 0;
1494
                            double y = 0;
1495
                            lineStringGeometry.GetVertex(i + 1, ref x, ref y);
1496
                            if (verticesCount == 2 && x < 0 || y < 0)
1497
                                _placement.PIDRemovePlacement(rep);
1498
                        }
1499
                        ReleaseCOMObjects(_LMConnector);
1500
                    }
1501
                }
1502

    
1503
                ReleaseCOMObjects(modelItem);
1504
            }
1505
        }
1506

    
1507
        private void GetConnectedLineGroup(Line line, List<Line> group)
1508
        {
1509
            if (!group.Contains(line))
1510
                group.Add(line);
1511
            foreach (var connector in line.CONNECTORS)
1512
            {
1513
                if (connector.ConnectedObject != null &&
1514
                    connector.ConnectedObject.GetType() == typeof(Line) &&
1515
                    !group.Contains(connector.ConnectedObject) &&
1516
                    string.IsNullOrEmpty(((Line)connector.ConnectedObject).SPPID.ModelItemId))
1517
                {
1518
                    Line connLine = connector.ConnectedObject as Line;
1519
                    if (!SPPIDUtil.IsBranchLine(connLine, line))
1520
                        GetConnectedLineGroup(connLine, group);
1521
                }
1522
            }
1523
        }
1524

    
1525
        private void LineCoordinateCorrection(List<Line> group)
1526
        {
1527
            // 순서대로 전 Item 기준 정렬
1528
            LineCoordinateCorrectionByStart(group);
1529

    
1530
            // 역으로 심볼이 있을 경우 좌표 보정
1531
            LineCoordinateCorrectionForLastLine(group);
1532
        }
1533

    
1534
        private void LineCoordinateCorrectionByStart(List<Line> group)
1535
        {
1536
            for (int i = 0; i < group.Count; i++)
1537
            {
1538
                Line line = group[i];
1539
                if (i == 0)
1540
                {
1541
                    Connector symbolConnector = line.CONNECTORS.Find(x => x.ConnectedObject != null && x.ConnectedObject.GetType() == typeof(Symbol));
1542
                    if (symbolConnector != null)
1543
                        LineCoordinateCorrectionByConnItem(line, symbolConnector.ConnectedObject);
1544
                }
1545
                else if (i != 0)
1546
                {
1547
                    LineCoordinateCorrectionByConnItem(line, group[i - 1]);
1548
                }
1549
            }
1550
        }
1551

    
1552
        private void LineCoordinateCorrectionForLastLine(List<Line> group)
1553
        {
1554
            Line checkLine = group[group.Count - 1];
1555
            Connector lastSymbolConnector = checkLine.CONNECTORS.Find(x => x.ConnectedObject != null && x.ConnectedObject.GetType() == typeof(Symbol));
1556
            if (lastSymbolConnector != null)
1557
            {
1558
                LineCoordinateCorrectionByConnItem(checkLine, lastSymbolConnector.ConnectedObject);
1559
                for (int i = group.Count - 2; i >= 0; i--)
1560
                {
1561
                    Line line = group[i + 1];
1562
                    Line prevLine = group[i];
1563

    
1564
                    // 같으면 보정
1565
                    if (line.SlopeType == prevLine.SlopeType)
1566
                        LineCoordinateCorrectionByConnItem(prevLine, line);
1567
                    else
1568
                    {
1569
                        if (line.SlopeType == SlopeType.HORIZONTAL)
1570
                        {
1571
                            double prevX = 0;
1572
                            double prevY = 0;
1573
                            GetTargetLineConnectorPoint(prevLine.CONNECTORS.Find(z => z.ConnectedObject == line), prevLine, ref prevX, ref prevY);
1574
                            ChangeLineSPPIDCoordinateByConnectorOnlyX(line, prevLine, prevX);
1575

    
1576
                            double x = 0;
1577
                            double y = 0;
1578
                            GetTargetLineConnectorPoint(line.CONNECTORS.Find(z => z.ConnectedObject == prevLine), line, ref x, ref y);
1579
                            ChangeLineSPPIDCoordinateByConnectorOnlyY(prevLine, line, y);
1580
                        }
1581
                        else if (line.SlopeType == SlopeType.VERTICAL)
1582
                        {
1583
                            double prevX = 0;
1584
                            double prevY = 0;
1585
                            GetTargetLineConnectorPoint(prevLine.CONNECTORS.Find(z => z.ConnectedObject == line), prevLine, ref prevX, ref prevY);
1586
                            ChangeLineSPPIDCoordinateByConnectorOnlyY(line, prevLine, prevY);
1587

    
1588
                            double x = 0;
1589
                            double y = 0;
1590
                            GetTargetLineConnectorPoint(line.CONNECTORS.Find(z => z.ConnectedObject == prevLine), line, ref x, ref y);
1591
                            ChangeLineSPPIDCoordinateByConnectorOnlyX(prevLine, line, x);
1592
                        }
1593
                        break;
1594
                    }
1595
                }
1596
            }
1597
        }
1598

    
1599
        private void LineCoordinateCorrectionByConnItem(Line line, object connItem)
1600
        {
1601
            double x = 0;
1602
            double y = 0;
1603
            if (connItem.GetType() == typeof(Symbol))
1604
            {
1605
                Symbol targetSymbol = connItem as Symbol;
1606
                Connector targetConnector = targetSymbol.CONNECTORS.Find(z => z.ConnectedObject == line);
1607
                if (targetConnector != null)
1608
                    GetTargetSymbolConnectorPoint(targetConnector, targetSymbol, ref x, ref y);
1609
                else
1610
                    throw new Exception("Target symbol UID : " + targetSymbol.UID + "\r\nLine UID : " + line.UID);
1611
            }
1612
            else if (connItem.GetType() == typeof(Line))
1613
            {
1614
                Line targetLine = connItem as Line;
1615
                GetTargetLineConnectorPoint(targetLine.CONNECTORS.Find(z => z.ConnectedObject == line), targetLine, ref x, ref y);
1616
            }
1617

    
1618
            ChangeLineSPPIDCoordinateByConnector(line, connItem, x, y);
1619
        }
1620

    
1621
        private void ChangeLineSPPIDCoordinateByConnector(Line line, object connItem, double x, double y, bool changeOtherCoordinate = true)
1622
        {
1623
            Connector connector = line.CONNECTORS.Find(z => z.ConnectedObject == connItem);
1624
            int index = line.CONNECTORS.IndexOf(connector);
1625
            if (index == 0)
1626
            {
1627
                line.SPPID.START_X = x;
1628
                line.SPPID.START_Y = y;
1629
                if (line.SlopeType == SlopeType.HORIZONTAL && changeOtherCoordinate)
1630
                    line.SPPID.END_Y = y;
1631
                else if (line.SlopeType == SlopeType.VERTICAL && changeOtherCoordinate)
1632
                    line.SPPID.END_X = x;
1633
            }
1634
            else
1635
            {
1636
                line.SPPID.END_X = x;
1637
                line.SPPID.END_Y = y;
1638
                if (line.SlopeType == SlopeType.HORIZONTAL && changeOtherCoordinate)
1639
                    line.SPPID.START_Y = y;
1640
                else if (line.SlopeType == SlopeType.VERTICAL && changeOtherCoordinate)
1641
                    line.SPPID.START_X = x;
1642
            }
1643
        }
1644

    
1645
        private void ChangeLineSPPIDCoordinateByConnectorOnlyX(Line line, object connItem, double x)
1646
        {
1647
            Connector connector = line.CONNECTORS.Find(z => z.ConnectedObject == connItem);
1648
            int index = line.CONNECTORS.IndexOf(connector);
1649
            if (index == 0)
1650
            {
1651
                line.SPPID.START_X = x;
1652
                if (line.SlopeType == SlopeType.VERTICAL)
1653
                    line.SPPID.END_X = x;
1654
            }
1655
            else
1656
            {
1657
                line.SPPID.END_X = x;
1658
                if (line.SlopeType == SlopeType.VERTICAL)
1659
                    line.SPPID.START_X = x;
1660
            }
1661
        }
1662

    
1663
        private void ChangeLineSPPIDCoordinateByConnectorOnlyY(Line line, object connItem, double y)
1664
        {
1665
            Connector connector = line.CONNECTORS.Find(z => z.ConnectedObject == connItem);
1666
            int index = line.CONNECTORS.IndexOf(connector);
1667
            if (index == 0)
1668
            {
1669
                line.SPPID.START_Y = y;
1670
                if (line.SlopeType == SlopeType.HORIZONTAL)
1671
                    line.SPPID.END_Y = y;
1672
            }
1673
            else
1674
            {
1675
                line.SPPID.END_Y = y;
1676
                if (line.SlopeType == SlopeType.HORIZONTAL)
1677
                    line.SPPID.START_Y = y;
1678
            }
1679
        }
1680

    
1681
        private void NeedReModeling(Line line, LMSymbol symbol, ref bool result)
1682
        {
1683
            if (symbol != null)
1684
            {
1685
                string repID = symbol.AsLMRepresentation().Id;
1686
                string symbolUID = SPPIDUtil.FindSymbolByRepresentationID(document, repID).UID;
1687
                string lineUID = line.UID;
1688

    
1689
                SpecBreak startSpecBreak = document.SpecBreaks.Find(x =>
1690
                (x.DownStreamUID == symbolUID || x.UpStreamUID == symbolUID) &&
1691
                (x.DownStreamUID == lineUID || x.UpStreamUID == lineUID));
1692

    
1693
                EndBreak startEndBreak = document.EndBreaks.Find(x =>
1694
                (x.OWNER == symbolUID || x.PROPERTIES.Find(y => y.ATTRIBUTE == "Connected Item").VALUE == symbolUID) &&
1695
                (x.OWNER == lineUID || x.PROPERTIES.Find(y => y.ATTRIBUTE == "Connected Item").VALUE == lineUID));
1696

    
1697
                if (startSpecBreak != null || startEndBreak != null)
1698
                    result = true;
1699
            }
1700
        }
1701

    
1702
        /// <summary>
1703
        /// Symbol에 붙을 경우 Line을 Remodeling 한다.
1704
        /// </summary>
1705
        /// <param name="lines"></param>
1706
        /// <param name="prevLMConnector"></param>
1707
        /// <param name="startSymbol"></param>
1708
        /// <param name="endSymbol"></param>
1709
        private void ReModelingLine(Line line, LMConnector prevLMConnector, LMSymbol startSymbol, LMSymbol endSymbol, bool bStart, bool bEnd)
1710
        {
1711
            string symbolPath = string.Empty;
1712
            #region get symbol path
1713
            LMModelItem modelItem = dataSource.GetModelItem(prevLMConnector.ModelItemID);
1714
            foreach (LMRepresentation rep in modelItem.Representations)
1715
            {
1716
                if (!DBNull.Value.Equals(rep.get_FileName()) && !string.IsNullOrEmpty(rep.get_FileName()))
1717
                {
1718
                    symbolPath = rep.get_FileName();
1719
                    break;
1720
                }
1721
            }
1722
            #endregion
1723
            _LMAItem _LMAItem = _placement.PIDCreateItem(symbolPath);
1724
            LMConnector newConnector = null;
1725
            dynamic OID = prevLMConnector.get_GraphicOID();
1726
            DependencyObject drawingObject = radApp.ActiveDocument.ActiveSheet.DrawingObjects[OID];
1727
            Ingr.RAD2D.LineStringGeometry2d lineStringGeometry = drawingObject.GetGeometry() as Ingr.RAD2D.LineStringGeometry2d;
1728
            int verticesCount = lineStringGeometry.VertexCount;
1729
            PlaceRunInputs placeRunInputs = new PlaceRunInputs();
1730

    
1731
            List<double[]> vertices = new List<double[]>();
1732
            for (int i = 1; i <= verticesCount; i++)
1733
            {
1734
                double x = 0;
1735
                double y = 0;
1736
                lineStringGeometry.GetVertex(i, ref x, ref y);
1737
                vertices.Add(new double[] { x, y });
1738
            }
1739

    
1740
            for (int i = 0; i < vertices.Count; i++)
1741
            {
1742
                double[] points = vertices[i];
1743
                // 시작 심볼이 있고 첫번째 좌표일 때
1744
                if (startSymbol != null && i == 0)
1745
                {
1746
                    if (bStart)
1747
                    {
1748
                        SlopeType slopeType = SPPIDUtil.CalcSlope(points[0], points[1], vertices[i + 1][0], vertices[i + 1][1]);
1749
                        if (slopeType == SlopeType.HORIZONTAL)
1750
                            placeRunInputs.AddPoint(points[0], -0.1);
1751
                        else if (slopeType == SlopeType.VERTICAL)
1752
                            placeRunInputs.AddPoint(-0.1, points[1]);
1753
                        else
1754
                            placeRunInputs.AddPoint(points[0], -0.1);
1755

    
1756
                        placeRunInputs.AddPoint(points[0], points[1]);
1757
                    }
1758
                    else
1759
                    {
1760
                        placeRunInputs.AddSymbolTarget(startSymbol, points[0], points[1]);
1761
                    }
1762
                }
1763
                // 마지막 심볼이 있고 마지막 좌표일 때
1764
                else if (endSymbol != null && i == vertices.Count - 1)
1765
                {
1766
                    if (bEnd)
1767
                    {
1768
                        placeRunInputs.AddPoint(points[0], points[1]);
1769

    
1770
                        SlopeType slopeType = SPPIDUtil.CalcSlope(points[0], points[1], vertices[i - 1][0], vertices[i - 1][1]);
1771
                        if (slopeType == SlopeType.HORIZONTAL)
1772
                            placeRunInputs.AddPoint(points[0], -0.1);
1773
                        else if (slopeType == SlopeType.VERTICAL)
1774
                            placeRunInputs.AddPoint(-0.1, points[1]);
1775
                        else
1776
                            placeRunInputs.AddPoint(points[0], -0.1);
1777
                    }
1778
                    else
1779
                    {
1780
                        placeRunInputs.AddSymbolTarget(endSymbol, points[0], points[1]);
1781
                    }
1782
                }
1783
                // 첫번째이며 시작 심볼이 아니고 Connecotr일 경우
1784
                else if (i == 0 && prevLMConnector.ConnectItem1SymbolObject != null)
1785
                    placeRunInputs.AddSymbolTarget(prevLMConnector.ConnectItem1SymbolObject, points[0], points[1]);
1786
                // 마지막이며 마지막 심볼이 아니고 Connecotr일 경우
1787
                else if (i == vertices.Count - 1 && prevLMConnector.ConnectItem2SymbolObject != null)
1788
                    placeRunInputs.AddSymbolTarget(prevLMConnector.ConnectItem2SymbolObject, points[0], points[1]);
1789
                else
1790
                    placeRunInputs.AddPoint(points[0], points[1]);
1791
            }
1792

    
1793
            _placement.PIDRemovePlacement(prevLMConnector.AsLMRepresentation());
1794
            newConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
1795

    
1796
            ReleaseCOMObjects(placeRunInputs);
1797
            ReleaseCOMObjects(_LMAItem);
1798
            ReleaseCOMObjects(modelItem);
1799

    
1800
            if (newConnector != null)
1801
            {
1802
                if (startSymbol != null && bStart)
1803
                {
1804
                    _LMAItem = _placement.PIDCreateItem(symbolPath);
1805
                    placeRunInputs = new PlaceRunInputs();
1806
                    placeRunInputs.AddSymbolTarget(startSymbol, vertices[0][0], vertices[0][1]);
1807
                    placeRunInputs.AddConnectorTarget(newConnector, vertices[0][0], vertices[0][1]);
1808
                    LMConnector _LMConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
1809
                    if (_LMConnector != null)
1810
                    {
1811
                        RemoveConnectorForReModelingLine(newConnector);
1812
                        ZeroLengthModelItemID.Add(_LMConnector.ModelItemID);
1813
                        ReleaseCOMObjects(_LMConnector);
1814
                    }
1815
                    ReleaseCOMObjects(placeRunInputs);
1816
                    ReleaseCOMObjects(_LMAItem);
1817
                }
1818

    
1819
                if (endSymbol != null && bEnd)
1820
                {
1821
                    if (startSymbol != null)
1822
                    {
1823
                        Dictionary<LMConnector, List<double[]>> dicVertices = GetPipeRunVertices(newConnector.ModelItemID);
1824
                        newConnector = dicVertices.First().Key;
1825
                    }
1826

    
1827
                    _LMAItem = _placement.PIDCreateItem(symbolPath);
1828
                    placeRunInputs = new PlaceRunInputs();
1829
                    placeRunInputs.AddSymbolTarget(endSymbol, vertices[vertices.Count - 1][0], vertices[vertices.Count - 1][1]);
1830
                    placeRunInputs.AddConnectorTarget(newConnector, vertices[vertices.Count - 1][0], vertices[vertices.Count - 1][1]);
1831
                    LMConnector _LMConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
1832
                    if (_LMConnector != null)
1833
                    {
1834
                        RemoveConnectorForReModelingLine(newConnector);
1835
                        ZeroLengthModelItemID.Add(_LMConnector.ModelItemID);
1836
                        ReleaseCOMObjects(_LMConnector);
1837
                    }
1838
                    ReleaseCOMObjects(placeRunInputs);
1839
                    ReleaseCOMObjects(_LMAItem);
1840
                }
1841

    
1842
                line.SPPID.ModelItemId = newConnector.ModelItemID;
1843
                ReleaseCOMObjects(newConnector);
1844
            }
1845

    
1846
            ReleaseCOMObjects(modelItem);
1847
        }
1848

    
1849
        /// <summary>
1850
        /// Remodeling 과정에서 생긴 불필요한 Connector 제거
1851
        /// </summary>
1852
        /// <param name="connector"></param>
1853
        private void RemoveConnectorForReModelingLine(LMConnector connector)
1854
        {
1855
            Dictionary<LMConnector, List<double[]>> dicVertices = GetPipeRunVertices(connector.ModelItemID);
1856
            foreach (var item in dicVertices)
1857
            {
1858
                if (item.Value.Count == 2)
1859
                {
1860
                    bool result = false;
1861
                    foreach (var point in item.Value)
1862
                    {
1863
                        if (point[0] < 0 || point[1] < 0)
1864
                        {
1865
                            result = true;
1866
                            _placement.PIDRemovePlacement(item.Key.AsLMRepresentation());
1867
                            break;
1868
                        }
1869
                    }
1870

    
1871
                    if (result)
1872
                        break;
1873
                }
1874
            }
1875
            foreach (var item in dicVertices)
1876
                ReleaseCOMObjects(item.Key);
1877
        }
1878

    
1879
        /// <summary>
1880
        /// Symbol이 모델링된 SPPPID Symbol Object를 반환 - 연결된 Symbol이 ChildSymbol일 수도 있기때문에 메서드 개발
1881
        /// </summary>
1882
        /// <param name="symbol"></param>
1883
        /// <param name="line"></param>
1884
        /// <returns></returns>
1885
        private LMSymbol GetTargetSymbol(Symbol symbol, Line line)
1886
        {
1887
            LMSymbol _LMSymbol = null;
1888
            foreach (var connector in symbol.CONNECTORS)
1889
            {
1890
                if (connector.CONNECTEDITEM == line.UID)
1891
                {
1892
                    if (connector.Index == 0)
1893
                        _LMSymbol = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
1894
                    else
1895
                    {
1896
                        ChildSymbol child = null;
1897
                        foreach (var childSymbol in symbol.ChildSymbols)
1898
                        {
1899
                            if (childSymbol.Connectors.Contains(connector))
1900
                                child = childSymbol;
1901
                            else
1902
                                child = GetChildSymbolByConnector(childSymbol, connector);
1903

    
1904
                            if (child != null)
1905
                                break;
1906
                        }
1907

    
1908
                        if (child != null)
1909
                            _LMSymbol = dataSource.GetSymbol(child.SPPID.RepresentationId);
1910
                    }
1911

    
1912
                    break;
1913
                }
1914
            }
1915

    
1916
            return _LMSymbol;
1917
        }
1918

    
1919
        /// <summary>
1920
        /// Connector를 가지고 있는 ChildSymbol Object 반환
1921
        /// </summary>
1922
        /// <param name="item"></param>
1923
        /// <param name="connector"></param>
1924
        /// <returns></returns>
1925
        private ChildSymbol GetChildSymbolByConnector(ChildSymbol item, Connector connector)
1926
        {
1927
            foreach (var childSymbol in item.ChildSymbols)
1928
            {
1929
                if (childSymbol.Connectors.Contains(connector))
1930
                    return childSymbol;
1931
                else
1932
                    return GetChildSymbolByConnector(childSymbol, connector);
1933
            }
1934

    
1935
            return null;
1936
        }
1937

    
1938
        /// <summary>
1939
        /// EndBreak 모델링 메서드
1940
        /// </summary>
1941
        /// <param name="endBreak"></param>
1942
        private void EndBreakModeling(EndBreak endBreak)
1943
        {
1944
            object ownerObj = SPPIDUtil.FindObjectByUID(document, endBreak.OWNER);
1945
            object connectedItem = SPPIDUtil.FindObjectByUID(document, endBreak.PROPERTIES.Find(x => x.ATTRIBUTE == "Connected Item").VALUE);
1946
            LMConnector targetLMConnector = FindBreakLineTarget(ownerObj, connectedItem);
1947

    
1948
            if (targetLMConnector != null)
1949
            {
1950
                Array array = new double[] { 0, endBreak.SPPID.ORIGINAL_X, endBreak.SPPID.ORIGINAL_Y };
1951
                LMLabelPersist _LmLabelPersist = _placement.PIDPlaceLabel(endBreak.SPPID.MAPPINGNAME, ref array, Rotation: 0, LabeledItem: targetLMConnector.AsLMRepresentation(), IsLeaderVisible: true);
1952
                if (_LmLabelPersist != null)
1953
                {
1954
                    endBreak.SPPID.RepresentationId = _LmLabelPersist.AsLMRepresentation().Id;
1955
                    endBreak.SPPID.ModelItemID = _LmLabelPersist.ModelItemID;
1956
                    endBreak.SPPID.GraphicOID = _LmLabelPersist.get_GraphicOID();
1957
                    ReleaseCOMObjects(_LmLabelPersist);
1958
                }
1959
                ReleaseCOMObjects(targetLMConnector);
1960
            }
1961
        }
1962

    
1963
        private LMConnector ReModelingLMConnector(LMConnector connector)
1964
        {
1965
            string symbolPath = string.Empty;
1966
            #region get symbol path
1967
            LMModelItem modelItem = dataSource.GetModelItem(connector.ModelItemID);
1968
            foreach (LMRepresentation rep in modelItem.Representations)
1969
            {
1970
                if (!DBNull.Value.Equals(rep.get_FileName()) && !string.IsNullOrEmpty(rep.get_FileName()))
1971
                {
1972
                    symbolPath = rep.get_FileName();
1973
                    break;
1974
                }
1975
            }
1976
            #endregion
1977

    
1978
            LMConnector newConnector = null;
1979
            dynamic OID = connector.get_GraphicOID();
1980
            DependencyObject drawingObject = radApp.ActiveDocument.ActiveSheet.DrawingObjects[OID];
1981
            Ingr.RAD2D.LineStringGeometry2d lineStringGeometry = drawingObject.GetGeometry() as Ingr.RAD2D.LineStringGeometry2d;
1982
            int verticesCount = lineStringGeometry.VertexCount;
1983
            PlaceRunInputs placeRunInputs = new PlaceRunInputs();
1984
            _LMAItem _LMAItem = _placement.PIDCreateItem(symbolPath);
1985

    
1986
            if (Convert.ToBoolean(connector.get_IsZeroLength()))
1987
            {
1988
                double[] vertices = null;
1989
                lineStringGeometry.GetVertices(ref verticesCount, ref vertices);
1990
                double x = 0;
1991
                double y = 0;
1992
                lineStringGeometry.GetVertex(1, ref x, ref y);
1993

    
1994
                placeRunInputs.AddSymbolTarget(connector.ConnectItem1SymbolObject, x, y);
1995
                placeRunInputs.AddSymbolTarget(connector.ConnectItem2SymbolObject, x, y);
1996

    
1997
                _placement.PIDRemovePlacement(connector.AsLMRepresentation());
1998
                newConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
1999
            }
2000
            else
2001
            {
2002
                List<double[]> vertices = new List<double[]>();
2003
                for (int i = 1; i <= verticesCount; i++)
2004
                {
2005
                    double x = 0;
2006
                    double y = 0;
2007
                    lineStringGeometry.GetVertex(i, ref x, ref y);
2008
                    vertices.Add(new double[] { x, y });
2009
                }
2010

    
2011
                for (int i = 0; i < vertices.Count; i++)
2012
                {
2013
                    double[] points = vertices[i];
2014
                    if (i == 0)
2015
                    {
2016
                        if (connector.ConnectItem1SymbolObject != null)
2017
                            placeRunInputs.AddSymbolTarget(connector.ConnectItem1SymbolObject, points[0], points[1]);
2018
                        else
2019
                            placeRunInputs.AddPoint(points[0], points[1]);
2020
                    }
2021
                    else if (i == vertices.Count - 1)
2022
                    {
2023
                        if (connector.ConnectItem2SymbolObject != null)
2024
                            placeRunInputs.AddSymbolTarget(connector.ConnectItem2SymbolObject, points[0], points[1]);
2025
                        else
2026
                            placeRunInputs.AddPoint(points[0], points[1]);
2027
                    }
2028
                    else
2029
                        placeRunInputs.AddPoint(points[0], points[1]);
2030
                }
2031

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

    
2034
                _placement.PIDRemovePlacement(connector.AsLMRepresentation());
2035
                newConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
2036

    
2037
                foreach (var line in lines)
2038
                    line.SPPID.ModelItemId = newConnector.ModelItemID;
2039
            }
2040

    
2041

    
2042
            return newConnector;
2043
        }
2044

    
2045
        /// <summary>
2046
        /// SpecBreak Modeling 메서드
2047
        /// </summary>
2048
        /// <param name="specBreak"></param>
2049
        private void SpecBreakModeling(SpecBreak specBreak)
2050
        {
2051
            object upStreamObj = SPPIDUtil.FindObjectByUID(document, specBreak.UpStreamUID);
2052
            object downStreamObj = SPPIDUtil.FindObjectByUID(document, specBreak.DownStreamUID);
2053

    
2054
            if (upStreamObj != null &&
2055
                downStreamObj != null)
2056
            {
2057
                LMConnector targetLMConnector = FindBreakLineTarget(upStreamObj, downStreamObj);
2058

    
2059
                if (targetLMConnector != null)
2060
                {
2061
                    foreach (var attribute in specBreak.ATTRIBUTES)
2062
                    {
2063
                        AttributeMapping mapping = document.AttributeMappings.Find(x => x.UID == attribute.UID);
2064
                        if (mapping != null && !string.IsNullOrEmpty(mapping.SPPIDSYMBOLNAME) && mapping.SPPIDSYMBOLNAME != "None")
2065
                        {
2066
                            string MappingPath = mapping.SPPIDSYMBOLNAME;
2067
                            Array array = new double[] { 0, specBreak.SPPID.ORIGINAL_X, specBreak.SPPID.ORIGINAL_Y };
2068
                            LMLabelPersist _LmLabelPersist = _placement.PIDPlaceLabel(MappingPath, ref array, Rotation: specBreak.ANGLE, LabeledItem: targetLMConnector.AsLMRepresentation(), IsLeaderVisible: mapping.LeaderLine);
2069

    
2070
                            if (_LmLabelPersist != null)
2071
                            {
2072
                                specBreak.SPPID.RepresentationId = _LmLabelPersist.AsLMRepresentation().Id;
2073
                                specBreak.SPPID.ModelItemID = _LmLabelPersist.ModelItemID;
2074
                                specBreak.SPPID.GraphicOID = _LmLabelPersist.get_GraphicOID();
2075
                                ReleaseCOMObjects(_LmLabelPersist);
2076
                            }
2077
                        }
2078
                    }
2079
                    ReleaseCOMObjects(targetLMConnector);
2080
                }
2081
            }
2082
        }
2083

    
2084
        private LMConnector FindBreakLineTarget(object targetObj, object connectedObj)
2085
        {
2086
            LMConnector targetConnector = null;
2087
            Symbol targetSymbol = targetObj as Symbol;
2088
            Symbol connectedSymbol = connectedObj as Symbol;
2089
            Line targetLine = targetObj as Line;
2090
            Line connectedLine = connectedObj as Line;
2091
            if (targetSymbol != null && connectedSymbol != null)
2092
            {
2093
                LMSymbol targetLMSymbol = dataSource.GetSymbol(targetSymbol.SPPID.RepresentationId);
2094
                LMSymbol connectedLMSymbol = dataSource.GetSymbol(connectedSymbol.SPPID.RepresentationId);
2095

    
2096
                foreach (LMConnector connector in targetLMSymbol.Avoid1Connectors)
2097
                {
2098
                    if (connector.get_ItemStatus() != "Active")
2099
                        continue;
2100

    
2101
                    if (connector.ConnectItem1SymbolObject.Id == connectedLMSymbol.Id)
2102
                    {
2103
                        targetConnector = connector;
2104
                        break;
2105
                    }
2106
                    else if (connector.ConnectItem2SymbolObject.Id == connectedLMSymbol.Id)
2107
                    {
2108
                        targetConnector = connector;
2109
                        break;
2110
                    }
2111
                }
2112

    
2113
                foreach (LMConnector connector in targetLMSymbol.Avoid2Connectors)
2114
                {
2115
                    if (connector.get_ItemStatus() != "Active")
2116
                        continue;
2117

    
2118
                    if (connector.ConnectItem1SymbolObject.Id == connectedLMSymbol.Id)
2119
                    {
2120
                        targetConnector = connector;
2121
                        break;
2122
                    }
2123
                    else if (connector.ConnectItem2SymbolObject.Id == connectedLMSymbol.Id)
2124
                    {
2125
                        targetConnector = connector;
2126
                        break;
2127
                    }
2128
                }
2129

    
2130
                ReleaseCOMObjects(targetLMSymbol);
2131
                ReleaseCOMObjects(connectedLMSymbol);
2132
            }
2133
            else if (targetLine != null && connectedLine != null)
2134
            {
2135
                LMModelItem targetModelItem = dataSource.GetModelItem(targetLine.SPPID.ModelItemId);
2136
                LMModelItem connectedModelItem = dataSource.GetModelItem(connectedLine.SPPID.ModelItemId);
2137

    
2138
                if (targetModelItem != null && connectedModelItem != null)
2139
                {
2140
                    foreach (LMRepresentation rep in targetModelItem.Representations)
2141
                    {
2142
                        if (targetConnector != null)
2143
                            break;
2144

    
2145
                        if (rep.Attributes["RepresentationType"].get_Value() == "Connector" && rep.Attributes["ItemStatus"].get_Value() == "Active")
2146
                        {
2147
                            LMConnector _LMConnector = dataSource.GetConnector(rep.Id);
2148

    
2149
                            if (IsConnected(_LMConnector, connectedModelItem))
2150
                                targetConnector = _LMConnector;
2151
                            else
2152
                                ReleaseCOMObjects(_LMConnector);
2153
                        }
2154
                    }
2155

    
2156
                    ReleaseCOMObjects(targetModelItem);
2157
                }
2158
            }
2159
            else
2160
            {
2161
                LMSymbol connectedLMSymbol = null;
2162
                if (connectedSymbol != null)
2163
                    connectedLMSymbol = dataSource.GetSymbol(connectedSymbol.SPPID.RepresentationId);
2164
                else if (targetSymbol != null)
2165
                    connectedLMSymbol = dataSource.GetSymbol(targetSymbol.SPPID.RepresentationId);
2166
                else
2167
                {
2168

    
2169
                }
2170
                LMModelItem targetModelItem = null;
2171
                if (targetLine != null)
2172
                    targetModelItem = dataSource.GetModelItem(targetLine.SPPID.ModelItemId);
2173
                else if (connectedLine != null)
2174
                    targetModelItem = dataSource.GetModelItem(connectedLine.SPPID.ModelItemId);
2175
                else
2176
                {
2177

    
2178
                }
2179
                if (connectedLMSymbol != null && targetModelItem != null)
2180
                {
2181
                    foreach (LMConnector connector in connectedLMSymbol.Avoid1Connectors)
2182
                    {
2183
                        if (connector.get_ItemStatus() != "Active")
2184
                            continue;
2185

    
2186
                        if (IsConnected(connector, targetModelItem))
2187
                        {
2188
                            targetConnector = connector;
2189
                            break;
2190
                        }
2191
                    }
2192

    
2193
                    if (targetConnector == null)
2194
                    {
2195
                        foreach (LMConnector connector in connectedLMSymbol.Avoid2Connectors)
2196
                        {
2197
                            if (connector.get_ItemStatus() != "Active")
2198
                                continue;
2199

    
2200
                            if (IsConnected(connector, targetModelItem))
2201
                            {
2202
                                targetConnector = connector;
2203
                                break;
2204
                            }
2205
                        }
2206
                    }
2207
                }
2208

    
2209
            }
2210

    
2211
            return targetConnector;
2212
        }
2213

    
2214
        private bool IsConnected(LMConnector connector, LMModelItem modelItem)
2215
        {
2216
            bool result = false;
2217

    
2218
            foreach (LMRepresentation rep in modelItem.Representations)
2219
            {
2220
                if (result)
2221
                    break;
2222

    
2223
                if (rep.Attributes["RepresentationType"].get_Value() == "Connector" && rep.Attributes["ItemStatus"].get_Value() == "Active")
2224
                {
2225
                    LMConnector _LMConnector = dataSource.GetConnector(rep.Id);
2226

    
2227
                    if (_LMConnector.ConnectItem1SymbolObject != null &&
2228
                        connector.ConnectItem1SymbolObject != null &&
2229
                        _LMConnector.ConnectItem1SymbolObject.Id == connector.ConnectItem1SymbolObject.Id)
2230
                    {
2231
                        result = true;
2232
                        ReleaseCOMObjects(_LMConnector);
2233
                        break;
2234
                    }
2235
                    else if (_LMConnector.ConnectItem1SymbolObject != null &&
2236
                        connector.ConnectItem2SymbolObject != null &&
2237
                        _LMConnector.ConnectItem1SymbolObject.Id == connector.ConnectItem2SymbolObject.Id)
2238
                    {
2239
                        result = true;
2240
                        ReleaseCOMObjects(_LMConnector);
2241
                        break;
2242
                    }
2243
                    else if (_LMConnector.ConnectItem2SymbolObject != null &&
2244
                        connector.ConnectItem1SymbolObject != null &&
2245
                        _LMConnector.ConnectItem2SymbolObject.Id == connector.ConnectItem1SymbolObject.Id)
2246
                    {
2247
                        result = true;
2248
                        ReleaseCOMObjects(_LMConnector);
2249
                        break;
2250
                    }
2251
                    else if (_LMConnector.ConnectItem2SymbolObject != null &&
2252
                        connector.ConnectItem2SymbolObject != null &&
2253
                        _LMConnector.ConnectItem2SymbolObject.Id == connector.ConnectItem2SymbolObject.Id)
2254
                    {
2255
                        result = true;
2256
                        ReleaseCOMObjects(_LMConnector);
2257
                        break;
2258
                    }
2259

    
2260
                    ReleaseCOMObjects(_LMConnector);
2261
                }
2262
            }
2263

    
2264

    
2265
            return result;
2266
        }
2267

    
2268
        /// <summary>
2269
        /// FromModelItem을 ToModelItem으로 PipeRunJoin하는 메서드
2270
        /// </summary>
2271
        /// <param name="fromModelItemId"></param>
2272
        /// <param name="toModelItemId"></param>
2273
        private void JoinPipeRun(string fromModelItemId, string toModelItemId)
2274
        {
2275
            LMModelItem modelItem1 = dataSource.GetModelItem(toModelItemId);
2276
            _LMAItem item1 = modelItem1.AsLMAItem();
2277
            LMModelItem modelItem2 = dataSource.GetModelItem(fromModelItemId);
2278
            _LMAItem item2 = modelItem2.AsLMAItem();
2279

    
2280
            // item2가 item1으로 조인
2281
            _placement.PIDJoinRuns(ref item1, ref item2);
2282
            item1.Commit();
2283
            item2.Commit();
2284

    
2285
            List<Line> lines = SPPIDUtil.FindLinesByModelId(document, fromModelItemId);
2286
            foreach (var line in lines)
2287
                line.SPPID.ModelItemId = toModelItemId;
2288

    
2289
            ReleaseCOMObjects(modelItem1);
2290
            ReleaseCOMObjects(item1);
2291
            ReleaseCOMObjects(modelItem2);
2292
            ReleaseCOMObjects(item2);
2293
        }
2294

    
2295
        /// <summary>
2296
        /// PipeRun을 자동으로 Join하는 메서드
2297
        /// </summary>
2298
        /// <param name="modelItemId"></param>
2299
        private void AutoJoinPipeRun(string modelItemId)
2300
        {
2301
            LMModelItem modelItem = dataSource.GetModelItem(modelItemId);
2302
            _LMAItem item = modelItem.AsLMAItem();
2303
            if (modelItem.get_ItemStatus() == "Active")
2304
            {
2305
                string modelitemID = item.Id;
2306
                _placement.PIDAutoJoin(item, AutoJoinEndConstants.autoJoin_Both, ref item);
2307
                string afterModelItemID = item.Id;
2308

    
2309
                if (modelitemID != afterModelItemID)
2310
                {
2311
                    List<Line> lines = SPPIDUtil.FindLinesByModelId(document, modelitemID);
2312
                    foreach (var line in lines)
2313
                        line.SPPID.ModelItemId = afterModelItemID;
2314
                }
2315
                item.Commit();
2316
            }
2317

    
2318
            ReleaseCOMObjects(modelItem);
2319
            ReleaseCOMObjects(item);
2320
        }
2321

    
2322
        /// <summary>
2323
        /// LineRun에 있는 Line들을 Join하는 진입 메서드
2324
        /// </summary>
2325
        /// <param name="run"></param>
2326
        private void JoinRunLine(LineRun run)
2327
        {
2328
            string modelItemId = string.Empty;
2329
            foreach (var item in run.RUNITEMS)
2330
            {
2331
                if (item.GetType() == typeof(Line))
2332
                {
2333
                    Line line = item as Line;
2334
                    AutoJoinPipeRun(line.SPPID.ModelItemId);
2335
                    modelItemId = line.SPPID.ModelItemId;
2336
                }
2337
            }
2338
        }
2339

    
2340
        /// <summary>
2341
        /// PipeRun의 좌표를 가져오는 메서드
2342
        /// </summary>
2343
        /// <param name="modelId"></param>
2344
        /// <returns></returns>
2345
        private Dictionary<LMConnector, List<double[]>> GetPipeRunVertices(string modelId)
2346
        {
2347
            Dictionary<LMConnector, List<double[]>> connectorVertices = new Dictionary<LMConnector, List<double[]>>();
2348
            LMModelItem modelItem = dataSource.GetModelItem(modelId);
2349

    
2350
            if (modelItem != null)
2351
            {
2352
                foreach (LMRepresentation rep in modelItem.Representations)
2353
                {
2354
                    if (rep.Attributes["RepresentationType"].get_Value() == "Connector" && rep.Attributes["ItemStatus"].get_Value() == "Active")
2355
                    {
2356
                        LMConnector _LMConnector = dataSource.GetConnector(rep.Id);
2357
                        connectorVertices.Add(_LMConnector, new List<double[]>());
2358
                        dynamic OID = rep.get_GraphicOID();
2359
                        DependencyObject drawingObject = radApp.ActiveDocument.ActiveSheet.DrawingObjects[OID];
2360
                        Ingr.RAD2D.LineStringGeometry2d lineStringGeometry = drawingObject.GetGeometry() as Ingr.RAD2D.LineStringGeometry2d;
2361
                        int verticesCount = lineStringGeometry.VertexCount;
2362
                        double[] vertices = null;
2363
                        lineStringGeometry.GetVertices(ref verticesCount, ref vertices);
2364
                        for (int i = 0; i < verticesCount; i++)
2365
                        {
2366
                            double x = 0;
2367
                            double y = 0;
2368
                            lineStringGeometry.GetVertex(i + 1, ref x, ref y);
2369
                            connectorVertices[_LMConnector].Add(new double[] { Math.Round(x, 10), Math.Round(y, 10) });
2370
                        }
2371
                    }
2372
                }
2373

    
2374
                ReleaseCOMObjects(modelItem);
2375
            }
2376

    
2377
            return connectorVertices;
2378
        }
2379

    
2380
        /// <summary>
2381
        /// 좌표로 PipeRun의 Connector중에 어느 Connector에 가까운지/붙을지 가져오는 메서드 - 조건에 안맞아서 못찾을시 제일 가까운 점으로 가져오는 방식
2382
        /// </summary>
2383
        /// <param name="connectorVertices"></param>
2384
        /// <param name="connX"></param>
2385
        /// <param name="connY"></param>
2386
        /// <returns></returns>
2387
        private LMConnector FindTargetLMConnectorForLabel(Dictionary<LMConnector, List<double[]>> connectorVertices, double connX, double connY)
2388
        {
2389
            double length = double.MaxValue;
2390
            LMConnector targetConnector = null;
2391
            foreach (var item in connectorVertices)
2392
            {
2393
                List<double[]> points = item.Value;
2394
                for (int i = 0; i < points.Count - 1; i++)
2395
                {
2396
                    double[] point1 = points[i];
2397
                    double[] point2 = points[i + 1];
2398
                    double x1 = Math.Min(point1[0], point2[0]);
2399
                    double y1 = Math.Min(point1[1], point2[1]);
2400
                    double x2 = Math.Max(point1[0], point2[0]);
2401
                    double y2 = Math.Max(point1[1], point2[1]);
2402

    
2403
                    if ((x1 <= connX && x2 >= connX) ||
2404
                        (y1 <= connY && y2 >= connY))
2405
                    {
2406
                        double distance = SPPIDUtil.CalcPointToPointdDistance(point1[0], point1[1], connX, connY);
2407
                        if (length >= distance)
2408
                        {
2409
                            targetConnector = item.Key;
2410
                            length = distance;
2411
                        }
2412

    
2413
                        distance = SPPIDUtil.CalcPointToPointdDistance(point2[0], point2[1], connX, connY);
2414
                        if (length >= distance)
2415
                        {
2416
                            targetConnector = item.Key;
2417
                            length = distance;
2418
                        }
2419
                    }
2420
                }
2421
            }
2422

    
2423
            // 못찾았을때.
2424
            length = double.MaxValue;
2425
            if (targetConnector == null)
2426
            {
2427
                foreach (var item in connectorVertices)
2428
                {
2429
                    List<double[]> points = item.Value;
2430

    
2431
                    foreach (double[] point in points)
2432
                    {
2433
                        double distance = SPPIDUtil.CalcPointToPointdDistance(point[0], point[1], connX, connY);
2434
                        if (length >= distance)
2435
                        {
2436
                            targetConnector = item.Key;
2437
                            length = distance;
2438
                        }
2439
                    }
2440
                }
2441
            }
2442

    
2443
            return targetConnector;
2444
        }
2445

    
2446
        private LMConnector FindTargetLMConnectorForBranch(Line line, Line targetLine, ref double x, ref double y)
2447
        {
2448
            Dictionary<LMConnector, List<double[]>> vertices = GetPipeRunVertices(targetLine.SPPID.ModelItemId);
2449

    
2450
            double length = double.MaxValue;
2451
            LMConnector targetConnector = null;
2452
            double[] resultPoint = null;
2453
            List<double[]> targetVertices = null;
2454

    
2455
            // Vertices 포인트에 제일 가까운곳
2456
            foreach (var item in vertices)
2457
            {
2458
                List<double[]> points = item.Value;
2459
                for (int i = 0; i < points.Count; i++)
2460
                {
2461
                    double[] point = points[i];
2462
                    double tempX = point[0];
2463
                    double tempY = point[1];
2464

    
2465
                    double distance = SPPIDUtil.CalcPointToPointdDistance(tempX, tempY, x, y);
2466
                    if (length >= distance)
2467
                    {
2468
                        targetConnector = item.Key;
2469
                        length = distance;
2470
                        resultPoint = point;
2471
                        targetVertices = item.Value;
2472
                    }
2473
                }
2474
            }
2475

    
2476
            // Vertices Cross에 제일 가까운곳
2477
            foreach (var item in vertices)
2478
            {
2479
                List<double[]> points = item.Value;
2480
                for (int i = 0; i < points.Count - 1; i++)
2481
                {
2482
                    double[] point1 = points[i];
2483
                    double[] point2 = points[i + 1];
2484

    
2485
                    double maxLineX = Math.Max(point1[0], point2[0]);
2486
                    double minLineX = Math.Min(point1[0], point2[0]);
2487
                    double maxLineY = Math.Max(point1[1], point2[1]);
2488
                    double minLineY = Math.Min(point1[1], point2[1]);
2489

    
2490
                    SlopeType slope = SPPIDUtil.CalcSlope(minLineX, minLineY, maxLineX, maxLineY);
2491

    
2492
                    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]);
2493
                    if (crossingPoint != null)
2494
                    {
2495
                        double distance = SPPIDUtil.CalcPointToPointdDistance(crossingPoint[0], crossingPoint[1], x, y);
2496
                        if (length >= distance)
2497
                        {
2498
                            if (slope == SlopeType.Slope &&
2499
                                minLineX <= crossingPoint[0] && maxLineX >= crossingPoint[0] &&
2500
                                minLineY <= crossingPoint[1] && maxLineY >= crossingPoint[1])
2501
                            {
2502
                                targetConnector = item.Key;
2503
                                length = distance;
2504
                                resultPoint = crossingPoint;
2505
                                targetVertices = item.Value;
2506
                            }
2507
                            else if (slope == SlopeType.HORIZONTAL &&
2508
                                minLineX <= crossingPoint[0] && maxLineX >= crossingPoint[0])
2509
                            {
2510
                                targetConnector = item.Key;
2511
                                length = distance;
2512
                                resultPoint = crossingPoint;
2513
                                targetVertices = item.Value;
2514
                            }
2515
                            else if (slope == SlopeType.VERTICAL &&
2516
                               minLineY <= crossingPoint[1] && maxLineY >= crossingPoint[1])
2517
                            {
2518
                                targetConnector = item.Key;
2519
                                length = distance;
2520
                                resultPoint = crossingPoint;
2521
                                targetVertices = item.Value;
2522
                            }
2523
                        }
2524
                    }
2525
                }
2526
            }
2527

    
2528
            foreach (var item in vertices)
2529
                if (item.Key != null && item.Key != targetConnector)
2530
                    ReleaseCOMObjects(item.Key);
2531

    
2532
            if (SPPIDUtil.IsBranchLine(line, targetLine))
2533
            {
2534
                double tempResultX = resultPoint[0];
2535
                double tempResultY = resultPoint[1];
2536
                SPPIDUtil.ConvertGridPoint(ref tempResultX, ref tempResultY);
2537

    
2538
                GridSetting gridSetting = GridSetting.GetInstance();
2539

    
2540
                for (int i = 0; i < targetVertices.Count; i++)
2541
                {
2542
                    double[] point = targetVertices[i];
2543
                    double tempX = targetVertices[i][0];
2544
                    double tempY = targetVertices[i][1];
2545
                    SPPIDUtil.ConvertGridPoint(ref tempX, ref tempY);
2546
                    if (tempX == tempResultX && tempY == tempResultY)
2547
                    {
2548
                        if (i == 0)
2549
                        {
2550
                            LMSymbol connSymbol = targetConnector.ConnectItem1SymbolObject;
2551
                            bool containZeroLength = false;
2552
                            if (connSymbol != null)
2553
                            {
2554
                                foreach (LMConnector connector in connSymbol.Connect1Connectors)
2555
                                {
2556
                                    if (connector.get_ItemStatus() == "Active" && Convert.ToBoolean(connector.get_IsZeroLength()) == true)
2557
                                        containZeroLength = true;
2558
                                }
2559
                                foreach (LMConnector connector in connSymbol.Connect2Connectors)
2560
                                {
2561
                                    if (connector.get_ItemStatus() == "Active" && Convert.ToBoolean(connector.get_IsZeroLength()) == true)
2562
                                        containZeroLength = true;
2563
                                }
2564
                            }
2565

    
2566
                            if (connSymbol == null ||
2567
                                (connSymbol != null && connSymbol.get_ItemStatus() == "Active" && connSymbol.get_RepresentationType() != "Branch") ||
2568
                                containZeroLength)
2569
                            {
2570
                                bool bCalcX = false;
2571
                                bool bCalcY = false;
2572
                                if (targetLine.SlopeType == SlopeType.HORIZONTAL)
2573
                                    bCalcX = true;
2574
                                else if (targetLine.SlopeType == SlopeType.VERTICAL)
2575
                                    bCalcY = true;
2576
                                else
2577
                                {
2578
                                    bCalcX = true;
2579
                                    bCalcY = true;
2580
                                }
2581

    
2582
                                if (bCalcX)
2583
                                {
2584
                                    double nextX = targetVertices[i + 1][0];
2585
                                    double newX = 0;
2586
                                    if (nextX > tempX)
2587
                                    {
2588
                                        newX = tempX + gridSetting.Length;
2589
                                        if (newX > nextX)
2590
                                            newX = (point[0] + nextX) / 2;
2591
                                    }
2592
                                    else
2593
                                    {
2594
                                        newX = tempX - gridSetting.Length;
2595
                                        if (newX < nextX)
2596
                                            newX = (point[0] + nextX) / 2;
2597
                                    }
2598
                                    resultPoint = new double[] { newX, resultPoint[1] };
2599
                                }
2600

    
2601
                                if (bCalcY)
2602
                                {
2603
                                    double nextY = targetVertices[i + 1][1];
2604
                                    double newY = 0;
2605
                                    if (nextY > tempY)
2606
                                    {
2607
                                        newY = tempY + gridSetting.Length;
2608
                                        if (newY > nextY)
2609
                                            newY = (point[1] + nextY) / 2;
2610
                                    }
2611
                                    else
2612
                                    {
2613
                                        newY = tempY - gridSetting.Length;
2614
                                        if (newY < nextY)
2615
                                            newY = (point[1] + nextY) / 2;
2616
                                    }
2617
                                    resultPoint = new double[] { resultPoint[0], newY };
2618
                                }
2619
                            }
2620
                        }
2621
                        else if (i == targetVertices.Count - 1)
2622
                        {
2623
                            LMSymbol connSymbol = targetConnector.ConnectItem2SymbolObject;
2624
                            bool containZeroLength = false;
2625
                            if (connSymbol != null)
2626
                            {
2627
                                foreach (LMConnector connector in connSymbol.Connect1Connectors)
2628
                                {
2629
                                    if (connector.get_ItemStatus() == "Active" && Convert.ToBoolean(connector.get_IsZeroLength()) == true)
2630
                                        containZeroLength = true;
2631
                                }
2632
                                foreach (LMConnector connector in connSymbol.Connect2Connectors)
2633
                                {
2634
                                    if (connector.get_ItemStatus() == "Active" && Convert.ToBoolean(connector.get_IsZeroLength()) == true)
2635
                                        containZeroLength = true;
2636
                                }
2637
                            }
2638

    
2639
                            if (connSymbol == null ||
2640
                                 (connSymbol != null && connSymbol.get_ItemStatus() == "Active" && connSymbol.get_RepresentationType() != "Branch") ||
2641
                                containZeroLength)
2642
                            {
2643
                                bool bCalcX = false;
2644
                                bool bCalcY = false;
2645
                                if (targetLine.SlopeType == SlopeType.HORIZONTAL)
2646
                                    bCalcX = true;
2647
                                else if (targetLine.SlopeType == SlopeType.VERTICAL)
2648
                                    bCalcY = true;
2649
                                else
2650
                                {
2651
                                    bCalcX = true;
2652
                                    bCalcY = true;
2653
                                }
2654

    
2655
                                if (bCalcX)
2656
                                {
2657
                                    double nextX = targetVertices[i - 1][0];
2658
                                    double newX = 0;
2659
                                    if (nextX > tempX)
2660
                                    {
2661
                                        newX = tempX + gridSetting.Length;
2662
                                        if (newX > nextX)
2663
                                            newX = (point[0] + nextX) / 2;
2664
                                    }
2665
                                    else
2666
                                    {
2667
                                        newX = tempX - gridSetting.Length;
2668
                                        if (newX < nextX)
2669
                                            newX = (point[0] + nextX) / 2;
2670
                                    }
2671
                                    resultPoint = new double[] { newX, resultPoint[1] };
2672
                                }
2673

    
2674
                                if (bCalcY)
2675
                                {
2676
                                    double nextY = targetVertices[i - 1][1];
2677
                                    double newY = 0;
2678
                                    if (nextY > tempY)
2679
                                    {
2680
                                        newY = tempY + gridSetting.Length;
2681
                                        if (newY > nextY)
2682
                                            newY = (point[1] + nextY) / 2;
2683
                                    }
2684
                                    else
2685
                                    {
2686
                                        newY = tempY - gridSetting.Length;
2687
                                        if (newY < nextY)
2688
                                            newY = (point[1] + nextY) / 2;
2689
                                    }
2690
                                    resultPoint = new double[] { resultPoint[0], newY };
2691
                                }
2692
                            }
2693
                        }
2694
                        break;
2695
                    }
2696
                }
2697
            }
2698

    
2699
            x = resultPoint[0];
2700
            y = resultPoint[1];
2701

    
2702
            return targetConnector;
2703
        }
2704

    
2705
        /// <summary>
2706
        /// Line Number Symbol을 실제로 Modeling하는 메서드
2707
        /// </summary>
2708
        /// <param name="lineNumber"></param>
2709
        private void LineNumberModeling(LineNumber lineNumber)
2710
        {
2711
            object obj = SPPIDUtil.FindObjectByUID(document, lineNumber.CONNLINE);
2712
            if (obj != null)
2713
            {
2714
                Line line = obj as Line;
2715
                Dictionary<LMConnector, List<double[]>> connectorVertices = GetPipeRunVertices(line.SPPID.ModelItemId);
2716
                LMConnector connectedLMConnector = FindTargetLMConnectorForLabel(connectorVertices, lineNumber.SPPID.ORIGINAL_X, lineNumber.SPPID.ORIGINAL_Y);
2717
                if (connectedLMConnector != null)
2718
                {
2719
                    double x = 0;
2720
                    double y = 0;
2721
                    CalcLabelLocation(ref x, ref y, lineNumber.SPPID.ORIGINAL_X, lineNumber.SPPID.ORIGINAL_Y, lineNumber.SPPIDLabelLocation, _ETCSetting.LineNumberLocation);
2722

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

    
2726
                    foreach (var item in connectorVertices)
2727
                        ReleaseCOMObjects(item.Key);
2728
                    if (_LmLabelPresist != null)
2729
                    {
2730
                        _LmLabelPresist.Commit();
2731
                        lineNumber.SPPID.RepresentationId = _LmLabelPresist.AsLMRepresentation().Id;
2732
                        ReleaseCOMObjects(_LmLabelPresist);
2733
                    }
2734
                    else
2735
                    {
2736

    
2737
                    }
2738
                }
2739
            }
2740
        }
2741

    
2742
        /// <summary>
2743
        /// Flow Mark Modeling
2744
        /// </summary>
2745
        /// <param name="line"></param>
2746
        private void FlowMarkModeling(Line line)
2747
        {
2748
            if (line.FLOWMARK && !string.IsNullOrEmpty(line.SPPID.ModelItemId) && !string.IsNullOrEmpty(_ETCSetting.FlowMarkSymbolPath))
2749
            {
2750
                SlopeType targetSlopeType = SPPIDUtil.CalcSlope(line.SPPID.START_X, line.SPPID.START_Y, line.SPPID.END_X, line.SPPID.END_Y);
2751
                string mappingPath = _ETCSetting.FlowMarkSymbolPath;
2752
                double percent = line.FLOWMARK_PERCENT;
2753
                double tempX = 0;
2754
                double tempY = 0;
2755

    
2756
                double gapX;
2757
                double gapY;
2758
                // ID2 기준의 Gap을 구함
2759
                if (percent == 0)
2760
                {
2761
                    gapX = 0;
2762
                    gapY = 0;
2763
                }
2764
                else
2765
                {
2766
                    gapX = Math.Abs(line.SPPID.START_X - line.SPPID.END_X) / 100 * percent;
2767
                    gapY = Math.Abs(line.SPPID.START_Y - line.SPPID.END_Y) / 100 * percent;
2768
                }
2769

    
2770
                if (line.SPPID.START_X < line.SPPID.END_X)
2771
                    tempX = line.SPPID.START_X + gapX;
2772
                else
2773
                    tempX = line.SPPID.START_X - gapX;
2774

    
2775
                if (line.SPPID.START_Y < line.SPPID.END_Y)
2776
                    tempY = line.SPPID.START_Y + gapY;
2777
                else
2778
                    tempY = line.SPPID.START_Y - gapY;
2779

    
2780

    
2781

    
2782
                Dictionary<LMConnector, List<double[]>> connectorVertices = GetPipeRunVertices(line.SPPID.ModelItemId);
2783
                LMConnector _TargetItem = null;
2784
                double distance = double.MaxValue;
2785
                double[] startPoint = null;
2786
                double[] endPoint = null;
2787
                // ID2의 기준 Gap으로 제일 가까운 Line 찾음(Slope도 같은조건)
2788
                foreach (var item in connectorVertices)
2789
                {
2790
                    for (int i = 0; i < item.Value.Count - 1; i++)
2791
                    {
2792
                        List<double[]> points = item.Value;
2793
                        double[] point1 = points[i];
2794
                        double[] point2 = points[i + 1];
2795

    
2796
                        SlopeType slopeType = SPPIDUtil.CalcSlope(point1[0], point1[1], point2[0], point2[1]);
2797
                        if (slopeType == targetSlopeType)
2798
                        {
2799
                            double result = SPPIDUtil.CalcPointToPointdDistance(point1[0], point1[1], tempX, tempY);
2800
                            if (result < distance)
2801
                            {
2802
                                distance = result;
2803
                                _TargetItem = item.Key;
2804

    
2805
                                startPoint = point1;
2806
                                endPoint = point2;
2807
                            }
2808

    
2809
                            result = SPPIDUtil.CalcPointToPointdDistance(point2[0], point2[1], tempX, tempY);
2810
                            if (result < distance)
2811
                            {
2812
                                distance = result;
2813
                                _TargetItem = item.Key;
2814

    
2815
                                startPoint = point1;
2816
                                endPoint = point2;
2817
                            }
2818
                        }
2819
                    }
2820
                }
2821

    
2822
                if (_TargetItem != null)
2823
                {
2824
                    double x = 0;
2825
                    double y = 0;
2826
                    double angle = 0;
2827
                    // SPPID 기준의 Gap으로 실 좌표를 구함
2828
                    if (percent == 0)
2829
                    {
2830
                        gapX = 0;
2831
                        gapY = 0;
2832
                    }
2833
                    else
2834
                    {
2835
                        gapX = Math.Abs(startPoint[0] - endPoint[0]) / 100 * percent;
2836
                        gapY = Math.Abs(startPoint[1] - endPoint[1]) / 100 * percent;
2837
                    }
2838

    
2839
                    if (startPoint[0] < endPoint[0])
2840
                        x = startPoint[0] + gapX;
2841
                    else
2842
                        x = startPoint[0] - gapX;
2843

    
2844
                    if (startPoint[1] < endPoint[1])
2845
                        y = startPoint[1] + gapY;
2846
                    else
2847
                        y = startPoint[1] - gapY;
2848

    
2849
                    if (targetSlopeType == SlopeType.HORIZONTAL)
2850
                    {
2851
                        if (startPoint[0] < endPoint[0])
2852
                            angle = 0;
2853
                        else
2854
                            angle = Math.PI;
2855
                    }
2856
                    // 90 270
2857
                    else if (targetSlopeType == SlopeType.VERTICAL)
2858
                    {
2859
                        if (startPoint[1] < endPoint[1])
2860
                            angle = 90 * Math.PI / 180;
2861
                        else
2862
                            angle = 270 * Math.PI / 180;
2863
                    }
2864

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

    
2867
                    if (_LMSymbol != null)
2868
                    {
2869
                        ReleaseCOMObjects(_LMSymbol);
2870
                    }
2871

    
2872
                }
2873

    
2874
                foreach (var item in connectorVertices)
2875
                    ReleaseCOMObjects(item.Key);
2876
            }
2877
        }
2878

    
2879
        /// <summary>
2880
        /// Line Number 기준으로 모든 Item에 Line Number의 Attribute Input
2881
        /// </summary>
2882
        /// <param name="lineNumber"></param>
2883
        private void InputLineNumberAttribute(LineNumber lineNumber)
2884
        {
2885
            foreach (LineRun run in lineNumber.RUNS)
2886
            {
2887
                foreach (var item in run.RUNITEMS)
2888
                {
2889
                    if (item.GetType() == typeof(Symbol))
2890
                    {
2891
                        Symbol symbol = item as Symbol;
2892
                        LMSymbol _LMSymbol = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
2893
                        if (_LMSymbol != null)
2894
                        {
2895
                            LMModelItem _LMModelItem = _LMSymbol.ModelItemObject;
2896

    
2897
                            if (_LMModelItem != null && _LMModelItem.get_ItemStatus() == "Active")
2898
                            {
2899
                                foreach (var attribute in lineNumber.ATTRIBUTES)
2900
                                {
2901
                                    LineNumberMapping mapping = document.LineNumberMappings.Find(x => x.UID == attribute.UID);
2902
                                    if (mapping != null && !string.IsNullOrEmpty(attribute.VALUE) && attribute.VALUE != "None")
2903
                                    {
2904
                                        LMAAttribute _LMAAttribute = _LMModelItem.Attributes[mapping.SPPIDATTRIBUTENAME];
2905
                                        if (_LMAAttribute != null)
2906
                                        {
2907
                                            if (DBNull.Value.Equals(_LMAAttribute.get_Value()))
2908
                                                _LMAAttribute.set_Value(attribute.VALUE);
2909
                                            else if (_LMAAttribute.get_Value() != attribute.VALUE)
2910
                                                _LMAAttribute.set_Value(attribute.VALUE);
2911
                                        }
2912
                                    }
2913
                                }
2914
                                _LMModelItem.Commit();
2915
                            }
2916
                            if (_LMModelItem != null)
2917
                                ReleaseCOMObjects(_LMModelItem);
2918
                        }
2919
                        if (_LMSymbol != null)
2920
                            ReleaseCOMObjects(_LMSymbol);
2921
                    }
2922
                    else if (item.GetType() == typeof(Line))
2923
                    {
2924
                        Line line = item as Line;
2925
                        if (line != null)
2926
                        {
2927
                            LMModelItem _LMModelItem = dataSource.GetModelItem(line.SPPID.ModelItemId);
2928
                            if (_LMModelItem != null && _LMModelItem.get_ItemStatus() == "Active")
2929
                            {
2930
                                foreach (var attribute in lineNumber.ATTRIBUTES)
2931
                                {
2932
                                    LineNumberMapping mapping = document.LineNumberMappings.Find(x => x.UID == attribute.UID);
2933
                                    if (mapping != null && !string.IsNullOrEmpty(attribute.VALUE) && attribute.VALUE != "None")
2934
                                    {
2935
                                        LMAAttribute _LMAAttribute = _LMModelItem.Attributes[mapping.SPPIDATTRIBUTENAME];
2936
                                        if (_LMAAttribute != null)
2937
                                        {
2938
                                            if (DBNull.Value.Equals(_LMAAttribute.get_Value()))
2939
                                                _LMAAttribute.set_Value(attribute.VALUE);
2940
                                            else if (_LMAAttribute.get_Value() != attribute.VALUE)
2941
                                                _LMAAttribute.set_Value(attribute.VALUE);
2942

    
2943
                                        }
2944
                                    }
2945
                                }
2946
                                _LMModelItem.Commit();
2947
                            }
2948
                            if (_LMModelItem != null)
2949
                                ReleaseCOMObjects(_LMModelItem);
2950
                        }
2951
                    }
2952
                }
2953
            }
2954
        }
2955

    
2956
        /// <summary>
2957
        /// Symbol Attribute 입력 메서드
2958
        /// </summary>
2959
        /// <param name="item"></param>
2960
        private void InputSymbolAttribute(object targetItem, List<BaseModel.Attribute> targetAttributes)
2961
        {
2962

    
2963
            // Object 아이템이 Symbol일 경우 Equipment일 경우 
2964
            string sRep = null;
2965
            if (targetItem.GetType() == typeof(Symbol))
2966
                sRep = ((Symbol)targetItem).SPPID.RepresentationId;
2967
            else if (targetItem.GetType() == typeof(Equipment))
2968
                sRep = ((Equipment)targetItem).SPPID.RepresentationId;
2969

    
2970
            if (!string.IsNullOrEmpty(sRep))
2971
            {
2972
                LMSymbol _LMSymbol = dataSource.GetSymbol(sRep);
2973
                LMModelItem _LMModelItem = _LMSymbol.ModelItemObject;
2974
                LMAAttributes _Attributes = _LMModelItem.Attributes;
2975

    
2976
                foreach (var item in targetAttributes)
2977
                {
2978
                    AttributeMapping mapping = document.AttributeMappings.Find(x => x.UID == item.UID);
2979
                    if (mapping != null && !string.IsNullOrEmpty(item.VALUE) && item.VALUE != "None")
2980
                    {
2981
                        LMAAttribute _Attribute = _Attributes[mapping.SPPIDATTRIBUTENAME];
2982
                        if (_Attribute != null)
2983
                            _Attribute.set_Value(item.VALUE);
2984
                    }
2985
                }
2986
                _LMModelItem.Commit();
2987

    
2988
                ReleaseCOMObjects(_Attributes);
2989
                ReleaseCOMObjects(_LMModelItem);
2990
                ReleaseCOMObjects(_LMSymbol);
2991
            }
2992
        }
2993

    
2994
        /// <summary>
2995
        /// Input SpecBreak Attribute
2996
        /// </summary>
2997
        /// <param name="specBreak"></param>
2998
        private void InputSpecBreakAttribute(SpecBreak specBreak)
2999
        {
3000
            object upStreamObj = SPPIDUtil.FindObjectByUID(document, specBreak.UpStreamUID);
3001
            object downStreamObj = SPPIDUtil.FindObjectByUID(document, specBreak.DownStreamUID);
3002

    
3003
            if (upStreamObj != null &&
3004
                downStreamObj != null)
3005
            {
3006
                LMConnector targetLMConnector = FindBreakLineTarget(upStreamObj, downStreamObj);
3007

    
3008
                if (targetLMConnector != null)
3009
                {
3010
                    foreach (LMLabelPersist _LMLabelPersist in targetLMConnector.LabelPersists)
3011
                    {
3012
                        string symbolPath = _LMLabelPersist.get_FileName();
3013
                        AttributeMapping mapping = document.AttributeMappings.Find(x => x.SPPIDSYMBOLNAME == symbolPath);
3014
                        if (mapping != null)
3015
                        {
3016
                            BaseModel.Attribute attribute = specBreak.ATTRIBUTES.Find(y => y.UID == mapping.UID);
3017
                            if (attribute != null && !string.IsNullOrEmpty(attribute.VALUE) && attribute.VALUE != "None")
3018
                            {
3019
                                string[] values = attribute.VALUE.Split(new char[] { ',' });
3020
                                if (values.Length == 2)
3021
                                {
3022
                                    string upStreamValue = values[0];
3023
                                    string downStreamValue = values[1];
3024

    
3025
                                    InputAttributeForSpecBreak(upStreamObj, downStreamObj, upStreamValue, downStreamValue, mapping.SPPIDATTRIBUTENAME);
3026
                                }
3027
                            }
3028
                        }
3029
                    }
3030

    
3031
                    ReleaseCOMObjects(targetLMConnector);
3032
                }
3033
            }
3034

    
3035

    
3036
            #region 내부에서만 쓰는 메서드
3037
            void InputAttributeForSpecBreak(object _upStreamObj, object _downStreamObj, string upStreamValue, string downStreamValue, string sppidAttributeName)
3038
            {
3039
                Symbol upStreamSymbol = _upStreamObj as Symbol;
3040
                Line upStreamLine = _upStreamObj as Line;
3041
                Symbol downStreamSymbol = _downStreamObj as Symbol;
3042
                Line downStreamLine = _downStreamObj as Line;
3043
                // 둘다 Line일 경우
3044
                if (upStreamLine != null && downStreamLine != null)
3045
                {
3046
                    InputLineAttributeForSpecBreakLine(upStreamLine, sppidAttributeName, upStreamValue);
3047
                    InputLineAttributeForSpecBreakLine(downStreamLine, sppidAttributeName, downStreamValue);
3048
                }
3049
                // 둘다 Symbol일 경우
3050
                else if (upStreamSymbol != null && downStreamSymbol != null)
3051
                {
3052
                    LMConnector zeroLenthConnector = FindBreakLineTarget(upStreamSymbol, downStreamSymbol);
3053
                    LMSymbol upStreamLMSymbol = dataSource.GetSymbol(upStreamSymbol.SPPID.RepresentationId);
3054
                    LMSymbol downStreamLMSymbol = dataSource.GetSymbol(downStreamSymbol.SPPID.RepresentationId);
3055

    
3056
                    foreach (LMConnector connector in upStreamLMSymbol.Avoid1Connectors)
3057
                    {
3058
                        if (connector.get_ItemStatus() != "Active")
3059
                            continue;
3060

    
3061
                        if (connector.Id != zeroLenthConnector.Id)
3062
                            InputLineAttributeForSpecBreakLMConnector(connector, sppidAttributeName, upStreamValue);
3063
                    }
3064

    
3065
                    foreach (LMConnector connector in upStreamLMSymbol.Avoid2Connectors)
3066
                    {
3067
                        if (connector.get_ItemStatus() != "Active")
3068
                            continue;
3069

    
3070
                        if (connector.Id != zeroLenthConnector.Id)
3071
                            InputLineAttributeForSpecBreakLMConnector(connector, sppidAttributeName, upStreamValue);
3072
                    }
3073

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

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

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

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

    
3092
                    ReleaseCOMObjects(zeroLenthConnector);
3093
                    ReleaseCOMObjects(upStreamLMSymbol);
3094
                    ReleaseCOMObjects(downStreamLMSymbol);
3095
                }
3096
                else if (upStreamSymbol != null && downStreamLine != null)
3097
                {
3098
                    LMConnector zeroLenthConnector = FindBreakLineTarget(upStreamSymbol, downStreamLine);
3099
                    InputLineAttributeForSpecBreakLine(downStreamLine, sppidAttributeName, downStreamValue);
3100
                    LMSymbol upStreamLMSymbol = dataSource.GetSymbol(upStreamSymbol.SPPID.RepresentationId);
3101

    
3102
                    foreach (LMConnector connector in upStreamLMSymbol.Avoid1Connectors)
3103
                    {
3104
                        if (connector.get_ItemStatus() != "Active")
3105
                            continue;
3106

    
3107
                        if (connector.Id != zeroLenthConnector.Id)
3108
                            InputLineAttributeForSpecBreakLMConnector(connector, sppidAttributeName, upStreamValue);
3109
                    }
3110

    
3111
                    foreach (LMConnector connector in upStreamLMSymbol.Avoid2Connectors)
3112
                    {
3113
                        if (connector.get_ItemStatus() != "Active")
3114
                            continue;
3115

    
3116
                        if (connector.Id != zeroLenthConnector.Id)
3117
                            InputLineAttributeForSpecBreakLMConnector(connector, sppidAttributeName, upStreamValue);
3118
                    }
3119

    
3120
                    ReleaseCOMObjects(zeroLenthConnector);
3121
                    ReleaseCOMObjects(upStreamLMSymbol);
3122
                }
3123
                else if (upStreamLine != null && downStreamSymbol != null)
3124
                {
3125
                    LMConnector zeroLenthConnector = FindBreakLineTarget(upStreamLine, downStreamSymbol);
3126
                    InputLineAttributeForSpecBreakLine(upStreamLine, sppidAttributeName, upStreamValue);
3127
                    LMSymbol downStreamLMSymbol = dataSource.GetSymbol(downStreamSymbol.SPPID.RepresentationId);
3128

    
3129
                    foreach (LMConnector connector in downStreamLMSymbol.Avoid1Connectors)
3130
                    {
3131
                        if (connector.get_ItemStatus() != "Active")
3132
                            continue;
3133

    
3134
                        if (connector.Id != zeroLenthConnector.Id)
3135
                            InputLineAttributeForSpecBreakLMConnector(connector, sppidAttributeName, downStreamValue);
3136
                    }
3137

    
3138
                    foreach (LMConnector connector in downStreamLMSymbol.Avoid2Connectors)
3139
                    {
3140
                        if (connector.get_ItemStatus() != "Active")
3141
                            continue;
3142

    
3143
                        if (connector.Id != zeroLenthConnector.Id)
3144
                            InputLineAttributeForSpecBreakLMConnector(connector, sppidAttributeName, downStreamValue);
3145
                    }
3146

    
3147
                    ReleaseCOMObjects(zeroLenthConnector);
3148
                    ReleaseCOMObjects(downStreamLMSymbol);
3149
                }
3150
            }
3151

    
3152
            void InputLineAttributeForSpecBreakLine(Line line, string attrName, string value)
3153
            {
3154
                LMModelItem _LMModelItem = dataSource.GetModelItem(line.SPPID.ModelItemId);
3155
                if (_LMModelItem != null && _LMModelItem.get_ItemStatus() == "Active")
3156
                {
3157
                    LMAAttribute _LMAAttribute = _LMModelItem.Attributes[attrName];
3158
                    if (_LMAAttribute != null)
3159
                    {
3160
                        if (DBNull.Value.Equals(_LMAAttribute.get_Value()))
3161
                            _LMAAttribute.set_Value(value);
3162
                        else if (_LMAAttribute.get_Value() != value)
3163
                            _LMAAttribute.set_Value(value);
3164
                    }
3165

    
3166
                    _LMModelItem.Commit();
3167
                }
3168
                if (_LMModelItem != null)
3169
                    ReleaseCOMObjects(_LMModelItem);
3170
            }
3171

    
3172
            void InputLineAttributeForSpecBreakLMConnector(LMConnector connector, string attrName, string value)
3173
            {
3174
                LMModelItem _LMModelItem = dataSource.GetModelItem(connector.ModelItemID);
3175
                if (_LMModelItem != null && _LMModelItem.get_ItemStatus() == "Active")
3176
                {
3177
                    LMAAttribute _LMAAttribute = _LMModelItem.Attributes[attrName];
3178
                    if (_LMAAttribute != null)
3179
                    {
3180
                        if (DBNull.Value.Equals(_LMAAttribute.get_Value()))
3181
                            _LMAAttribute.set_Value(value);
3182
                        else if (_LMAAttribute.get_Value() != value)
3183
                            _LMAAttribute.set_Value(value);
3184
                    }
3185

    
3186
                    _LMModelItem.Commit();
3187
                }
3188
                if (_LMModelItem != null)
3189
                    ReleaseCOMObjects(_LMModelItem);
3190
            }
3191
            #endregion
3192
        }
3193

    
3194
        /// <summary>
3195
        /// Text Modeling - Association일 경우는 Text대신 해당 맵핑된 Symbol로 모델링
3196
        /// </summary>
3197
        /// <param name="text"></param>
3198
        private void TextModeling(Text text)
3199
        {
3200
            LMSymbol _LMSymbol = null;
3201
            LMConnector connectedLMConnector = null;
3202
            //if (text.ASSOCIATION && !string.IsNullOrEmpty(text.OWNER) && text.OWNER != "None")
3203
            if (text.ASSOCIATION)
3204
            {
3205
                object owner = SPPIDUtil.FindObjectByUID(document, text.OWNER);
3206
                if (owner.GetType() == typeof(Symbol))
3207
                {
3208
                    Symbol symbol = owner as Symbol;
3209
                    _LMSymbol = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
3210
                    if (_LMSymbol != null)
3211
                    {
3212
                        BaseModel.Attribute attribute = symbol.ATTRIBUTES.Find(x => x.ASSOCITEM == text.UID);
3213
                        if (attribute != null && !string.IsNullOrEmpty(attribute.VALUE) && attribute.VALUE != "None")
3214
                        {
3215
                            AttributeMapping mapping = document.AttributeMappings.Find(x => x.UID == attribute.UID && !string.IsNullOrEmpty(x.SPPIDSYMBOLNAME));
3216

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

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

    
3225
                                LMLabelPersist _LMLabelPersist = _placement.PIDPlaceLabel(mapping.SPPIDSYMBOLNAME, ref array, Rotation: text.ANGLE, LabeledItem: _LMSymbol.AsLMRepresentation(), IsLeaderVisible: mapping.LeaderLine);
3226
                                if (_LMLabelPersist != null)
3227
                                {
3228
                                    _LMLabelPersist.Commit();
3229
                                    ReleaseCOMObjects(_LMLabelPersist);
3230
                                }
3231
                            }
3232
                        }
3233
                    }
3234
                }
3235
                else if (owner.GetType() == typeof(Line))
3236
                {
3237
                    Line line = owner as Line;
3238
                    Dictionary<LMConnector, List<double[]>> connectorVertices = GetPipeRunVertices(line.SPPID.ModelItemId);
3239
                    connectedLMConnector = FindTargetLMConnectorForLabel(connectorVertices, text.SPPID.ORIGINAL_X, text.SPPID.ORIGINAL_Y);
3240

    
3241
                    if (connectedLMConnector != null)
3242
                    {
3243
                        BaseModel.Attribute attribute = line.ATTRIBUTES.Find(x => x.ASSOCITEM == text.UID);
3244
                        if (attribute != null && !string.IsNullOrEmpty(attribute.VALUE) && attribute.VALUE != "None")
3245
                        {
3246
                            AttributeMapping mapping = document.AttributeMappings.Find(x => x.UID == attribute.UID && !string.IsNullOrEmpty(x.SPPIDSYMBOLNAME));
3247

    
3248
                            if (mapping != null)
3249
                            {
3250
                                double x = 0;
3251
                                double y = 0;
3252

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

    
3256
                                LMLabelPersist _LMLabelPersist = _placement.PIDPlaceLabel(mapping.SPPIDSYMBOLNAME, ref array, Rotation: text.ANGLE, LabeledItem: connectedLMConnector.AsLMRepresentation(), IsLeaderVisible: mapping.LeaderLine);
3257
                                if (_LMLabelPersist != null)
3258
                                {
3259
                                    _LMLabelPersist.Commit();
3260
                                    ReleaseCOMObjects(_LMLabelPersist);
3261
                                }
3262
                            }
3263
                        }
3264
                    }
3265
                }
3266
            }
3267
            else
3268
            {
3269
                LMItemNote _LMItemNote = null;
3270
                LMAAttribute _LMAAttribute = null;
3271

    
3272
                double x = 0;
3273
                double y = 0;
3274
                double angle = text.ANGLE;
3275
                CalcLabelLocation(ref x, ref y, text.SPPID.ORIGINAL_X, text.SPPID.ORIGINAL_Y, text.SPPIDLabelLocation, _ETCSetting.TextLocation);
3276

    
3277
                _LMSymbol = _placement.PIDPlaceSymbol(text.SPPID.MAPPINGNAME, x, y, Rotation: angle);
3278
                _LMSymbol.Commit();
3279
                _LMItemNote = _placement.PIDDataSource.GetItemNote(_LMSymbol.ModelItemID);
3280
                _LMItemNote.Commit();
3281
                _LMAAttribute = _LMItemNote.Attributes["Note.Body"];
3282
                _LMAAttribute.set_Value(text.VALUE);
3283
                _LMItemNote.Commit();
3284

    
3285
                if (_LMAAttribute != null)
3286
                    ReleaseCOMObjects(_LMAAttribute);
3287
                if (_LMItemNote != null)
3288
                    ReleaseCOMObjects(_LMItemNote);
3289
            }
3290
            if (_LMSymbol != null)
3291
                ReleaseCOMObjects(_LMSymbol);
3292
        }
3293

    
3294
        /// <summary>
3295
        /// Note Modeling
3296
        /// </summary>
3297
        /// <param name="note"></param>
3298
        private void NoteModeling(Note note)
3299
        {
3300
            LMSymbol _LMSymbol = null;
3301
            LMItemNote _LMItemNote = null;
3302
            LMAAttribute _LMAAttribute = null;
3303

    
3304
            if (string.IsNullOrEmpty(note.OWNER) || note.OWNER == "None")
3305
            {
3306
                double x = 0;
3307
                double y = 0;
3308

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

    
3311
                _LMSymbol = _placement.PIDPlaceSymbol(note.SPPID.MAPPINGNAME, x, y);
3312
                _LMSymbol.Commit();
3313
                _LMItemNote = _placement.PIDDataSource.GetItemNote(_LMSymbol.ModelItemID);
3314
                _LMItemNote.Commit();
3315
                _LMAAttribute = _LMItemNote.Attributes["Note.Body"];
3316
                _LMAAttribute.set_Value(note.VALUE);
3317
                _LMItemNote.Commit();
3318
            }
3319

    
3320
            if (_LMAAttribute != null)
3321
                ReleaseCOMObjects(_LMAAttribute);
3322
            if (_LMItemNote != null)
3323
                ReleaseCOMObjects(_LMItemNote);
3324
            if (_LMSymbol != null)
3325
                ReleaseCOMObjects(_LMSymbol);
3326
        }
3327

    
3328
        /// <summary>
3329
        /// Note Symbol Modeling
3330
        /// </summary>
3331
        /// <param name="symbol"></param>
3332
        private void NoteSymbolModeling(Symbol symbol)
3333
        {
3334
            if (symbol.TYPE == "Notes")
3335
            {
3336
                string mappingPath = symbol.SPPID.MAPPINGNAME;
3337
                double x = symbol.SPPID.ORIGINAL_X;
3338
                double y = symbol.SPPID.ORIGINAL_Y;
3339
                int mirror = 0;
3340
                double angle = symbol.ANGLE;
3341

    
3342
                LMSymbol _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle);
3343
                symbol.SPPID.RepresentationId = _LMSymbol.AsLMRepresentation().Id;
3344
                symbol.SPPID.ModelItemID = _LMSymbol.ModelItemID;
3345

    
3346
                ReleaseCOMObjects(_LMSymbol);
3347

    
3348
                InputSymbolAttribute(symbol, symbol.ATTRIBUTES);
3349
            }
3350
        }
3351

    
3352
        /// <summary>
3353
        /// Label의 좌표를 구하는 메서드(ID2 기준의 좌표 -> SPPID 좌표)
3354
        /// </summary>
3355
        /// <param name="x"></param>
3356
        /// <param name="y"></param>
3357
        /// <param name="originX"></param>
3358
        /// <param name="originY"></param>
3359
        /// <param name="SPPIDLabelLocation"></param>
3360
        /// <param name="location"></param>
3361
        private void CalcLabelLocation(ref double x, ref double y, double originX, double originY, SPPIDLabelLocationInfo SPPIDLabelLocation, Location location)
3362
        {
3363
            if (location == Location.None)
3364
            {
3365
                x = originX;
3366
                y = originY;
3367
            }
3368
            else
3369
            {
3370
                if (location.HasFlag(Location.Center))
3371
                {
3372
                    x = (SPPIDLabelLocation.X1 + SPPIDLabelLocation.X2) / 2;
3373
                    y = (SPPIDLabelLocation.Y1 + SPPIDLabelLocation.Y2) / 2;
3374
                }
3375

    
3376
                if (location.HasFlag(Location.Left))
3377
                    x = SPPIDLabelLocation.X1;
3378
                else if (location.HasFlag(Location.Right))
3379
                    x = SPPIDLabelLocation.X2;
3380

    
3381
                if (location.HasFlag(Location.Down))
3382
                    y = SPPIDLabelLocation.Y1;
3383
                else if (location.HasFlag(Location.Up))
3384
                    y = SPPIDLabelLocation.Y2;
3385
            }
3386
        }
3387

    
3388
        /// <summary>
3389
        /// Symbol의 우선순위 Modeling 목록을 가져온다.
3390
        /// 1. Angle Valve
3391
        /// 2. 3개로 이루어진 Symbol Group
3392
        /// </summary>
3393
        /// <returns></returns>
3394
        private List<Symbol> GetPrioritySymbol()
3395
        {
3396
            DataTable symbolTable = document.SymbolTable;
3397
            // List에 순서대로 쌓는다.
3398
            List<Symbol> symbols = new List<Symbol>();
3399

    
3400
            // Angle Valve 부터
3401
            foreach (var symbol in document.SYMBOLS.FindAll(x => x.CONNECTORS.FindAll(y => y.Index == 0).Count == 2))
3402
            {
3403
                if (!symbols.Contains(symbol))
3404
                {
3405
                    double originX = 0;
3406
                    double originY = 0;
3407

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

    
3412
                    SlopeType slopeType1 = SlopeType.None;
3413
                    SlopeType slopeType2 = SlopeType.None;
3414
                    foreach (Connector connector in symbol.CONNECTORS.FindAll(x => x.Index == 0))
3415
                    {
3416
                        double connectorX = 0;
3417
                        double connectorY = 0;
3418
                        SPPIDUtil.ConvertPointBystring(connector.CONNECTPOINT, ref connectorX, ref connectorY);
3419
                        if (slopeType1 == SlopeType.None)
3420
                            slopeType1 = SPPIDUtil.CalcSlope(originX, originY, connectorX, connectorY);
3421
                        else
3422
                            slopeType2 = SPPIDUtil.CalcSlope(originX, originY, connectorX, connectorY);
3423
                    }
3424

    
3425
                    if ((slopeType1 == SlopeType.VERTICAL && slopeType2 == SlopeType.HORIZONTAL) ||
3426
                        (slopeType2 == SlopeType.VERTICAL && slopeType1 == SlopeType.HORIZONTAL))
3427
                        symbols.Add(symbol);
3428
                }
3429
            }
3430

    
3431
            List<Symbol> tempSymbols = new List<Symbol>();
3432
            // Conn 갯수 기준
3433
            foreach (var item in document.SYMBOLS)
3434
            {
3435
                if (!symbols.Contains(item))
3436
                    tempSymbols.Add(item);
3437
            }
3438
            tempSymbols.Sort(SortSymbolPriority);
3439
            symbols.AddRange(tempSymbols);
3440

    
3441
            return symbols;
3442
        }
3443

    
3444
        private void SetPriorityLine()
3445
        {
3446
            document.LINES.Sort(SortLinePriority);
3447

    
3448
            int SortLinePriority(Line a, Line b)
3449
            {
3450
                // Branch 없는것부터
3451
                int branchRetval = CompareBranchLine(a, b);
3452
                if (branchRetval != 0)
3453
                {
3454
                    return branchRetval;
3455
                }
3456
                else
3457
                {
3458
                    // Symbol 연결 갯수
3459
                    int connSymbolRetval = CompareConnSymbol(a, b);
3460
                    if (connSymbolRetval != 0)
3461
                    {
3462
                        return connSymbolRetval;
3463
                    }
3464
                    else
3465
                    {
3466
                        // 아이템 연결 갯수(심볼, Line이면서 Not Branch)
3467
                        int connItemRetval = CompareConnItem(a, b);
3468
                        if (connItemRetval != 0)
3469
                        {
3470
                            return connItemRetval;
3471
                        }
3472
                        else
3473
                        {
3474
                            // ConnectedItem이 없는것
3475
                            int noneConnRetval = CompareNoneConn(a, b);
3476
                            if (noneConnRetval != 0)
3477
                            {
3478
                                return noneConnRetval;
3479
                            }
3480
                            else
3481
                            {
3482

    
3483
                            }
3484
                        }
3485
                    }
3486
                }
3487

    
3488
                return 0;
3489
            }
3490

    
3491
            int CompareConnSymbol(Line a, Line b)
3492
            {
3493
                List<Connector> connectorsA = a.CONNECTORS
3494
                    .Where(conn => conn.ConnectedObject != null && conn.ConnectedObject.GetType() == typeof(Symbol))
3495
                    .ToList();
3496

    
3497
                List<Connector> connectorsB = b.CONNECTORS
3498
                    .Where(conn => conn.ConnectedObject != null && conn.ConnectedObject.GetType() == typeof(Symbol))
3499
                    .ToList();
3500

    
3501
                // 오름차순
3502
                return connectorsB.Count.CompareTo(connectorsA.Count);
3503
            }
3504

    
3505
            int CompareConnItem(Line a, Line b)
3506
            {
3507
                List<Connector> connectorsA = a.CONNECTORS
3508
                    .Where(conn => conn.ConnectedObject != null && 
3509
                    (conn.ConnectedObject.GetType() == typeof(Symbol) || 
3510
                    (conn.ConnectedObject.GetType() == typeof(Line) && !SPPIDUtil.IsBranchLine((Line)conn.ConnectedObject, a))))
3511
                    .ToList();
3512

    
3513
                List<Connector> connectorsB = b.CONNECTORS
3514
                    .Where(conn => conn.ConnectedObject != null &&
3515
                    (conn.ConnectedObject.GetType() == typeof(Symbol) ||
3516
                    (conn.ConnectedObject.GetType() == typeof(Line) && !SPPIDUtil.IsBranchLine((Line)conn.ConnectedObject, b))))
3517
                    .ToList();
3518

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

    
3523
            int CompareBranchLine(Line a, Line b)
3524
            {
3525
                List<Connector> connectorsA = a.CONNECTORS
3526
                    .Where(conn => conn.ConnectedObject != null && conn.ConnectedObject.GetType() == typeof(Line) && SPPIDUtil.IsBranchLine(a, conn.ConnectedObject as Line))
3527
                    .ToList();
3528
                List<Connector> connectorsB = b.CONNECTORS
3529
                    .Where(conn => conn.ConnectedObject != null && conn.ConnectedObject.GetType() == typeof(Line) && SPPIDUtil.IsBranchLine(b, conn.ConnectedObject as Line))
3530
                    .ToList();
3531

    
3532
                // 내림차순
3533
                return connectorsA.Count.CompareTo(connectorsB.Count);
3534
            }
3535

    
3536
            int CompareNoneConn(Line a, Line b)
3537
            {
3538
                List<Connector> connectorsA = a.CONNECTORS
3539
                    .Where(conn => conn.ConnectedObject == null)
3540
                    .ToList();
3541

    
3542
                List<Connector> connectorsB = b.CONNECTORS
3543
                    .Where(conn => conn.ConnectedObject == null)
3544
                    .ToList();
3545

    
3546
                // 오름차순
3547
                return connectorsB.Count.CompareTo(connectorsA.Count);
3548
            }
3549
        }
3550

    
3551
        private void SortBranchLines()
3552
        {
3553
            NewBranchLines.Sort(SortBranchLine);
3554
            int SortBranchLine(Line a, Line b)
3555
            {
3556
                int countA = a.CONNECTORS.FindAll(x => x.ConnectedObject != null &&
3557
                 x.ConnectedObject.GetType() == typeof(Line) &&
3558
                 SPPIDUtil.IsBranchLine(x.ConnectedObject as Line, a) &&
3559
                 string.IsNullOrEmpty(((Line)x.ConnectedObject).SPPID.ModelItemId)).Count;
3560

    
3561
                int countB = b.CONNECTORS.FindAll(x => x.ConnectedObject != null &&
3562
                 x.ConnectedObject.GetType() == typeof(Line) &&
3563
                 SPPIDUtil.IsBranchLine(x.ConnectedObject as Line, b) &&
3564
                 string.IsNullOrEmpty(((Line)x.ConnectedObject).SPPID.ModelItemId)).Count;
3565

    
3566
                // 내림차순
3567
                return countA.CompareTo(countB);
3568
            }
3569
        }
3570

    
3571
        private static int SortSymbolPriority(Symbol a, Symbol b)
3572
        {
3573
            int countA = a.CONNECTORS.FindAll(x => !string.IsNullOrEmpty(x.CONNECTEDITEM) && x.CONNECTEDITEM != "None").Count;
3574
            int countB = b.CONNECTORS.FindAll(x => !string.IsNullOrEmpty(x.CONNECTEDITEM) && x.CONNECTEDITEM != "None").Count;
3575
            int retval = countB.CompareTo(countA);
3576
            if (retval != 0)
3577
                return retval;
3578
            else
3579
                return a.SPPID.ORIGINAL_X.CompareTo(b.SPPID.ORIGINAL_X);
3580
        }
3581

    
3582
        /// <summary>
3583
        /// Graphic OID로 해당 Symbol의 크기를 구하여 Zoom
3584
        /// </summary>
3585
        /// <param name="graphicOID"></param>
3586
        /// <param name="milliseconds"></param>
3587
        private void ZoomObjectByGraphicOID(string graphicOID, int milliseconds = 150)
3588
        {
3589
            if (radApp.ActiveDocument.ActiveSheet.DrawingObjects[graphicOID] != null)
3590
            {
3591
                double minX = 0;
3592
                double minY = 0;
3593
                double maxX = 0;
3594
                double maxY = 0;
3595
                radApp.ActiveDocument.ActiveSheet.DrawingObjects[graphicOID].Range(out minX, out minY, out maxX, out maxY);
3596
                radApp.ActiveWindow.ZoomArea2(minX - 0.007, minY - 0.007, maxX + 0.007, maxY + 0.007, null);
3597

    
3598
                Thread.Sleep(milliseconds);
3599
            }
3600
        }
3601

    
3602
        /// <summary>
3603
        /// ComObject를 Release
3604
        /// </summary>
3605
        /// <param name="objVars"></param>
3606
        public void ReleaseCOMObjects(params object[] objVars)
3607
        {
3608
            int intNewRefCount = 0;
3609
            foreach (object obj in objVars)
3610
            {
3611
                if (!Information.IsNothing(obj) && System.Runtime.InteropServices.Marshal.IsComObject(obj))
3612
                    intNewRefCount = intNewRefCount + System.Runtime.InteropServices.Marshal.FinalReleaseComObject(obj);
3613
            }
3614
        }
3615
    }
3616
}
클립보드 이미지 추가 (최대 크기: 500 MB)