프로젝트

일반

사용자정보

통계
| 개정판:

hytos / DTI_PID / SPPIDConverter / AutoModeling.cs @ 5173ba5d

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

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

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

    
38
        public string DocumentLabelText { get; set; }
39

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

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

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

    
65
                }
66
            }
67
        }
68

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

    
81
                CreateDocument(ref drawingNumber, ref drawingName);
82

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

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

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

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

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

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

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

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

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

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

    
254
                                            modelItem.Commit();
255
                                            ReleaseCOMObjects(modelItem);
256
                                        }
257
                                    }
258
                                }
259
                                ReleaseCOMObjects(symbol);
260
                            }
261
                        }
262
                        SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
263
                    }
264
                    
265
                    #region RadApp 방식
266
                    //SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetAllProgress, radApp.ActiveDocument.ActiveSheet.Symbols.Count + radApp.ActiveDocument.ActiveSheet.AllDependencyObjects.Count);
267
                    //SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Clear Attribute");
268
                    //foreach (var item in radApp.ActiveDocument.ActiveSheet.Symbols)
269
                    //{
270
                    //    string modelItemID = string.Empty;
271
                    //    foreach (var attributes in item.AttributeSets)
272
                    //    {
273
                    //        if (!string.IsNullOrEmpty(modelItemID))
274
                    //            break;
275

    
276
                    //        foreach (var attribute in attributes)
277
                    //        {
278
                    //            if (attribute.Name == "ModelID")
279
                    //            {
280
                    //                modelItemID = attribute.GetValue().ToString();
281
                    //                break;
282
                    //            }
283
                    //        }
284
                    //    }
285

    
286
                    //    if (!string.IsNullOrEmpty(modelItemID))
287
                    //    {
288
                    //        LMModelItem modelItem = dataSource.GetModelItem(modelItemID);
289
                    //        if (modelItem != null)
290
                    //        {
291
                    //            LMAAttribute attribute = modelItem.Attributes["NominalDiameter"];
292
                    //            if (attribute != null)
293
                    //                attribute.set_Value(DBNull.Value);
294
                    //            attribute = modelItem.Attributes["PipingPoint1.NominalDiameter"];
295
                    //            if (attribute != null)
296
                    //                attribute.set_Value(DBNull.Value);
297
                    //            attribute = modelItem.Attributes["PipingPoint2.NominalDiameter"];
298
                    //            if (attribute != null)
299
                    //                attribute.set_Value(DBNull.Value);
300

    
301
                    //            modelItem.Commit();
302
                    //            ReleaseCOMObjects(modelItem);
303
                    //        }
304
                    //    }
305

    
306
                    //}
307
                    //foreach (var item in radApp.ActiveDocument.ActiveSheet.AllDependencyObjects)
308
                    //{
309
                    //    string modelItemID = string.Empty;
310
                    //    foreach (var attributes in item.AttributeSets)
311
                    //    {
312
                    //        if (!string.IsNullOrEmpty(modelItemID))
313
                    //            break;
314

    
315
                    //        foreach (var attribute in attributes)
316
                    //        {
317
                    //            if (attribute.Name == "ModelID")
318
                    //            {
319
                    //                modelItemID = attribute.GetValue().ToString();
320
                    //                break;
321
                    //            }
322
                    //        }
323
                    //    }
324

    
325
                    //    if (!string.IsNullOrEmpty(modelItemID))
326
                    //    {
327
                    //        LMModelItem modelItem = dataSource.GetModelItem(modelItemID);
328
                    //        if (modelItem != null)
329
                    //        {
330
                    //            LMAAttribute attribute = modelItem.Attributes["NominalDiameter"];
331
                    //            if (attribute != null)
332
                    //                attribute.set_Value(DBNull.Value);
333

    
334
                    //            modelItem.Commit();
335
                    //            ReleaseCOMObjects(modelItem);
336
                    //        }
337
                    //    }
338
                    //}
339
                    #endregion 
340
                    SetSystemEditingCommand(true);
341
                    #endregion
342

    
343
                    // EndBreak Modeling
344
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetAllProgress, document.EndBreaks.Count);
345
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "EndBreaks Modeling");
346
                    foreach (var item in document.EndBreaks)
347
                        try
348
                        {
349
                            EndBreakModeling(item);
350
                            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
351
                            if (string.IsNullOrEmpty(item.SPPID.ModelItemID))
352
                                Log.Write("Fail modeling endbreak UID : " + item.UID);
353
                        }
354
                        catch (Exception ex)
355
                        {
356
                            Log.Write("Error in EndBreakModeling");
357
                            Log.Write("UID : " + item.UID);
358
                            Log.Write(ex.Message);
359
                            Log.Write(ex.StackTrace);
360
                        }
361

    
362

    
363
                    // SpecBreak Modeling
364
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetAllProgress, document.SpecBreaks.Count);
365
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "SpecBreaks Modeling");
366
                    foreach (var item in document.SpecBreaks)
367
                        try
368
                        {
369
                            SpecBreakModeling(item);
370
                            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
371
                            if (string.IsNullOrEmpty(item.SPPID.ModelItemID))
372
                                Log.Write("Fail modeling specbreak UID : " + item.UID);
373
                        }
374
                        catch (Exception ex)
375
                        {
376
                            Log.Write("Error in SpecBreakModeling");
377
                            Log.Write("UID : " + item.UID);
378
                            Log.Write(ex.Message);
379
                            Log.Write(ex.StackTrace);
380
                        }
381

    
382

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

    
398

    
399
                    //// FlowMark Modeling
400
                    //SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Flow Mark Modeling");
401
                    //foreach (var item in document.LINES)
402
                    //    try
403
                    //    {
404
                    //        FlowMarkModeling(item);
405
                    //    }
406
                    //    catch (Exception ex)
407
                    //    {
408
                    //        Log.Write("Error in FlowMarkModeling");
409
                    //        Log.Write("UID : " + item.UID);
410
                    //        Log.Write(ex.Message);
411
                    //        Log.Write(ex.StackTrace);
412
                    //    }
413

    
414

    
415
                    //// Note Symbol Modeling
416
                    //SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Note Symbol Modeling");
417
                    //foreach (var item in document.SYMBOLS)
418
                    //    try
419
                    //    {
420
                    //        NoteSymbolModeling(item);
421
                    //    }
422
                    //    catch (Exception ex)
423
                    //    {
424
                    //        Log.Write("Error in NoteSymbolModeling");
425
                    //        Log.Write("UID : " + item.UID);
426
                    //        Log.Write(ex.Message);
427
                    //        Log.Write(ex.StackTrace);
428
                    //    }
429

    
430

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

    
446
                    //// Text Modeling
447
                    //SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Texts Modeling");
448
                    //foreach (var item in document.TEXTINFOS)
449
                    //    try
450
                    //    {
451
                    //        TextModeling(item);
452
                    //    }
453
                    //    catch (Exception ex)
454
                    //    {
455
                    //        Log.Write("Error in TextModeling");
456
                    //        Log.Write("UID : " + item.UID);
457
                    //        Log.Write(ex.Message);
458
                    //        Log.Write(ex.StackTrace);
459
                    //    }
460

    
461
                    //// Input LineNumber Attribute
462
                    //SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Set LineNumbers Attribute");
463
                    //foreach (var item in document.LINENUMBERS)
464
                    //    try
465
                    //    {
466
                    //        InputLineNumberAttribute(item);
467
                    //    }
468
                    //    catch (Exception ex)
469
                    //    {
470
                    //        Log.Write("Error in InputLineNumberAttribute");
471
                    //        Log.Write("UID : " + item.UID);
472
                    //        Log.Write(ex.Message);
473
                    //        Log.Write(ex.StackTrace);
474
                    //    }
475

    
476
                    //// Input Symbol Attribute
477
                    //SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Set Symbols Attribute");
478
                    //foreach (var item in document.SYMBOLS)
479
                    //    try
480
                    //    {
481
                    //        InputSymbolAttribute(item, item.ATTRIBUTES);
482
                    //    }
483
                    //    catch (Exception ex)
484
                    //    {
485
                    //        Log.Write("Error in InputSymbolAttribute");
486
                    //        Log.Write("UID : " + item.UID);
487
                    //        Log.Write(ex.Message);
488
                    //        Log.Write(ex.StackTrace);
489
                    //    }
490

    
491
                    //// Input SpecBreak Attribute
492
                    //SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Set Symbols Attribute");
493
                    //foreach (var item in document.SpecBreaks)
494
                    //    try
495
                    //    {
496
                    //        InputSpecBreakAttribute(item);
497
                    //    }
498
                    //    catch (Exception ex)
499
                    //    {
500
                    //        Log.Write("Error in InputSpecBreakAttribute");
501
                    //        Log.Write("UID : " + item.UID);
502
                    //        Log.Write(ex.Message);
503
                    //        Log.Write(ex.StackTrace);
504
                    //    }
505

    
506
                    //// Label Symbol Modeling
507
                    //SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Labels Modeling");
508
                    //foreach (var item in document.SYMBOLS)
509
                    //    try
510
                    //    {
511
                    //        LabelSymbolModeling(item);
512
                    //    }
513
                    //    catch (Exception ex)
514
                    //    {
515
                    //        Log.Write("Error in LabelSymbolModeling");
516
                    //        Log.Write("UID : " + item.UID);
517
                    //        Log.Write(ex.Message);
518
                    //        Log.Write(ex.StackTrace);
519
                    //    }
520

    
521
                    //// LineRun Line Join
522
                    //SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Join LineRuns");
523
                    //foreach (LineNumber lineNumber in document.LINENUMBERS)
524
                    //    try
525
                    //    {
526
                    //        foreach (LineRun run in lineNumber.RUNS)
527
                    //            JoinRunLine(run);
528
                    //    }
529
                    //    catch (Exception ex)
530
                    //    {
531
                    //        Log.Write("Error in JoinRunLine");
532
                    //        Log.Write("UID : " + lineNumber.UID);
533
                    //        Log.Write(ex.Message);
534
                    //        Log.Write(ex.StackTrace);
535
                    //    }
536

    
537
                    //// TrimLineRun Line Join
538
                    //foreach (TrimLine trimLine in document.TRIMLINES)
539
                    //    try
540
                    //    {
541
                    //        foreach (LineRun run in trimLine.RUNS)
542
                    //            JoinRunLine(run);
543
                    //    }
544
                    //    catch (Exception ex)
545
                    //    {
546
                    //        Log.Write("Error in JoinRunLine");
547
                    //        Log.Write("UID : " + trimLine.UID);
548
                    //        Log.Write(ex.Message);
549
                    //        Log.Write(ex.StackTrace);
550
                    //    }
551
                }
552
            }
553
            catch (Exception ex)
554
            {
555
                SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.ClearParent, null);
556
                SplashScreenManager.CloseForm(false);
557
                System.Windows.Forms.MessageBox.Show(ex.Message + "\r\n" + ex.StackTrace);
558
            }
559
            finally
560
            {
561
                Log.Write("End Modeling");
562
                application.ActiveWindow.Fit();
563

    
564
                if (radApp.ActiveDocument != null)
565
                {
566
                    //radApp.ActiveDocument.Save();
567
                    //radApp.ActiveDocument.SaveOnClose = false;
568
                    //radApp.ActiveDocument.Close(false);
569

    
570
                    ReleaseCOMObjects(newDrawing);
571
                }
572

    
573
                ReleaseCOMObjects(dataSource);
574
                ReleaseCOMObjects(_placement);
575

    
576
                Project_DB.InsertDrawingInfo(document.PATH, drawingNumber, drawingName, document);
577
                if (SplashScreenManager.Default.IsSplashFormVisible)
578
                {
579
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.ClearParent, null);
580
                    SplashScreenManager.CloseForm(false);
581
                    Log.Write("\r\n");
582
                }
583
            }
584
        }
585

    
586
        /// <summary>
587
        /// 도면 생성 메서드
588
        /// </summary>
589
        private void CreateDocument(ref string drawingNumber, ref string drawingName)
590
        {
591
            Log.Write("------------------ Start create document ------------------");
592
            GetDrawingNameAndNumber(ref drawingName, ref drawingNumber);
593
            Log.Write("Drawing name : " + drawingName);
594
            Log.Write("Drawing number : " + drawingNumber);
595
            newDrawing = application.Drawings.Add(document.Unit, document.Template, drawingNumber, drawingName);
596
            document.SPPID_DrawingNumber = drawingNumber;
597
            document.SPPID_DrawingName = drawingName;
598
            application.ActiveWindow.Fit();
599
            Thread.Sleep(1000);
600
            application.ActiveWindow.Zoom = 2000;
601
            Thread.Sleep(2000);
602
        }
603

    
604
        /// <summary>
605
        /// DrawingName, DrawingNumber를 확인하여 중복이 있으면 _1을 붙이고 +1씩 한다.
606
        /// </summary>
607
        /// <param name="drawingName"></param>
608
        /// <param name="drawingNumber"></param>
609
        private void GetDrawingNameAndNumber(ref string drawingName, ref string drawingNumber)
610
        {
611
            LMDrawings drawings = new LMDrawings();
612
            drawings.Collect(dataSource);
613

    
614
            List<string> drawingNameList = new List<string>();
615
            List<string> drawingNumberList = new List<string>();
616

    
617
            foreach (LMDrawing item in drawings)
618
            {
619
                drawingNameList.Add(item.Attributes["Name"].get_Value().ToString());
620
                drawingNumberList.Add(item.Attributes["DrawingNumber"].get_Value().ToString());
621
            }
622

    
623
            int nameLength = drawingName.Length;
624
            while (drawingNameList.Contains(drawingName))
625
            {
626
                if (nameLength == drawingName.Length)
627
                    drawingName += "-1";
628
                else
629
                {
630
                    int index = Convert.ToInt32(drawingName.Remove(0, nameLength + 1));
631
                    drawingName = drawingName.Substring(0, nameLength + 1);
632
                    drawingName += ++index;
633
                }
634
            }
635

    
636
            int numberLength = drawingNumber.Length;
637
            while (drawingNameList.Contains(drawingNumber))
638
            {
639
                if (numberLength == drawingNumber.Length)
640
                    drawingNumber += "-1";
641
                else
642
                {
643
                    int index = Convert.ToInt32(drawingNumber.Remove(0, numberLength + 1));
644
                    drawingNumber = drawingNumber.Substring(0, numberLength + 1);
645
                    drawingNumber += ++index;
646
                }
647
            }
648

    
649
            ReleaseCOMObjects(drawings);
650
        }
651

    
652
        /// <summary>
653
        /// 도면 크기 구하는 메서드
654
        /// </summary>
655
        /// <returns></returns>
656
        private bool DocumentCoordinateCorrection()
657
        {
658
            if (Settings.Default.DrawingX != 0 && Settings.Default.DrawingY != 0)
659
            {
660
                Log.Write("Setting Drawing X, Drawing Y");
661
                document.SetSPPIDLocation(Settings.Default.DrawingX, Settings.Default.DrawingY);
662
                Log.Write("Start coordinate correction");
663
                document.CoordinateCorrection();
664
                return true;
665
            }
666
            else
667
            {
668
                Log.Write("Need Drawing X, Y");
669
                return false;
670
            }
671
        }
672

    
673
        /// <summary>
674
        /// 심볼을 실제로 Modeling 메서드
675
        /// </summary>
676
        /// <param name="symbol"></param>
677
        /// <param name="targetSymbol"></param>
678
        /// <param name="prevSymbol"></param>
679
        private void SymbolModeling(Symbol symbol, Symbol targetSymbol)
680
        {
681
            // OWNERSYMBOL Attribute, 값을 가지고 있을 경우
682
            BaseModel.Attribute itemAttribute = symbol.ATTRIBUTES.Find(attr => attr.ATTRIBUTE == "OWNERSYMBOL");
683
            if (itemAttribute != null && (string.IsNullOrEmpty(itemAttribute.VALUE) || itemAttribute.VALUE != "None"))
684
                return;
685
            // 이미 모델링 됐을 경우
686
            else if (!string.IsNullOrEmpty(symbol.SPPID.RepresentationId))
687
                return;
688

    
689
            LMSymbol _LMSymbol = null;
690

    
691
            string mappingPath = symbol.SPPID.MAPPINGNAME;
692
            double x = symbol.SPPID.ORIGINAL_X;
693
            double y = symbol.SPPID.ORIGINAL_Y;
694
            int mirror = 0;
695
            double angle = symbol.ANGLE;
696

    
697
            // OPC 일경우 180도 일때 Mirror
698
            if (mappingPath.Contains("Piping OPC's") && angle == Math.PI)
699
                mirror = 1;
700

    
701
            // Mirror 계산
702
            if (symbol.FLIP == 1)
703
            {
704
                mirror = 1;
705
                angle += Math.PI;
706
            }
707

    
708
            if (targetSymbol != null && !string.IsNullOrEmpty(targetSymbol.SPPID.RepresentationId))
709
            {
710
                LMSymbol _TargetItem = dataSource.GetSymbol(targetSymbol.SPPID.RepresentationId);
711
                Connector connector = SPPIDUtil.FindSymbolConnectorByUID(document, symbol.UID, targetSymbol);
712
                if (connector != null)
713
                    GetTargetSymbolConnectorPoint(connector, targetSymbol, ref x, ref y);
714

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

    
717
                if (_LMSymbol != null && _TargetItem != null)
718
                {
719
                    symbol.SPPID.RepresentationId = _LMSymbol.AsLMRepresentation().Id;
720
                    LMConnector reModelingConnector = FindBreakLineTarget(symbol, targetSymbol);
721

    
722
                    if (reModelingConnector != null)
723
                        ReModelingLMConnector(reModelingConnector);
724
                }
725

    
726
                ReleaseCOMObjects(_TargetItem);
727
            }
728
            else
729
                _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle);
730

    
731
            if (_LMSymbol != null)
732
            {
733
                _LMSymbol.Commit();
734
                symbol.SPPID.RepresentationId = _LMSymbol.AsLMRepresentation().Id;
735
                symbol.SPPID.ModelItemID = _LMSymbol.ModelItemID;
736
                symbol.SPPID.GraphicOID = _LMSymbol.get_GraphicOID();
737

    
738
                foreach (var item in symbol.ChildSymbols)
739
                    CreateChildSymbol(item, _LMSymbol);
740

    
741
                symbol.SPPID.SPPID_X = _LMSymbol.get_XCoordinate();
742
                symbol.SPPID.SPPID_Y = _LMSymbol.get_YCoordinate();
743

    
744
                double[] range = null;
745
                GetSPPIDSymbolRange(symbol, ref range);
746
                symbol.SPPID.SPPID_Min_X = range[0];
747
                symbol.SPPID.SPPID_Min_Y = range[1];
748
                symbol.SPPID.SPPID_Max_X = range[2];
749
                symbol.SPPID.SPPID_Max_Y = range[3];
750

    
751
                foreach (var item in symbol.SPPID.CorrectionX_GroupSymbols)
752
                    item.SPPID.ORIGINAL_X = symbol.SPPID.SPPID_X;
753
                foreach (var item in symbol.SPPID.CorrectionY_GroupSymbols)
754
                    item.SPPID.ORIGINAL_Y = symbol.SPPID.SPPID_Y;
755

    
756
                ReleaseCOMObjects(_LMSymbol);
757
            }
758
        }
759

    
760
        private void RemoveSymbol(Symbol symbol)
761
        {
762
            if (!string.IsNullOrEmpty(symbol.SPPID.RepresentationId))
763
            {
764
                LMSymbol _LMSymbol = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
765
                if (_LMSymbol != null)
766
                {
767
                    _placement.PIDRemovePlacement(_LMSymbol.AsLMRepresentation());
768
                    ReleaseCOMObjects(_LMSymbol);
769
                }
770
            }
771

    
772
            symbol.SPPID.RepresentationId = string.Empty;
773
            symbol.SPPID.ModelItemID = string.Empty;
774
            symbol.SPPID.SPPID_X = double.NaN;
775
            symbol.SPPID.SPPID_Y = double.NaN;
776
            symbol.SPPID.SPPID_Min_X = double.NaN;
777
            symbol.SPPID.SPPID_Min_Y = double.NaN;
778
            symbol.SPPID.SPPID_Max_X = double.NaN;
779
            symbol.SPPID.SPPID_Max_Y = double.NaN;
780
        }
781

    
782
        private void RemoveSymbol(List<Symbol> symbols)
783
        {
784
            foreach (var symbol in symbols)
785
            {
786
                if (!string.IsNullOrEmpty(symbol.SPPID.RepresentationId))
787
                {
788
                    LMSymbol _LMSymbol = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
789
                    if (_LMSymbol != null)
790
                    {
791
                        _placement.PIDRemovePlacement(_LMSymbol.AsLMRepresentation());
792
                        ReleaseCOMObjects(_LMSymbol);
793
                    }
794
                }
795

    
796
                symbol.SPPID.RepresentationId = string.Empty;
797
                symbol.SPPID.ModelItemID = string.Empty;
798
                symbol.SPPID.SPPID_X = double.NaN;
799
                symbol.SPPID.SPPID_Y = double.NaN;
800
                symbol.SPPID.SPPID_Min_X = double.NaN;
801
                symbol.SPPID.SPPID_Min_Y = double.NaN;
802
                symbol.SPPID.SPPID_Max_X = double.NaN;
803
                symbol.SPPID.SPPID_Max_Y = double.NaN;
804
            }
805
        }
806

    
807
        /// <summary>
808
        /// ID2의 Symbol Width와 Height를 비교해서 상대적인 SPPID Connector좌표를 가져온다.
809
        /// </summary>
810
        /// <param name="targetConnector"></param>
811
        /// <param name="targetSymbol"></param>
812
        /// <param name="x"></param>
813
        /// <param name="y"></param>
814
        private void GetTargetSymbolConnectorPoint(Connector targetConnector, Symbol targetSymbol, ref double x, ref double y)
815
        {
816
            LMSymbol _TargetItem = dataSource.GetSymbol(targetSymbol.SPPID.RepresentationId);
817

    
818
            double[] range = null;
819
            List<double[]> points = new List<double[]>();
820
            GetSPPIDSymbolRangeAndConnectionPoints(targetSymbol, ref range, points);
821
            double x1 = range[0];
822
            double y1 = range[1];
823
            double x2 = range[2];
824
            double y2 = range[3];
825

    
826
            // Origin 기준 Connector의 위치차이
827
            double sceneX = 0;
828
            double sceneY = 0;
829
            SPPIDUtil.ConvertPointBystring(targetConnector.SCENECONNECTPOINT, ref sceneX, ref sceneY);
830
            double originX = 0;
831
            double originY = 0;
832
            SPPIDUtil.ConvertPointBystring(targetSymbol.ORIGINALPOINT, ref originX, ref originY);
833
            double gapX = originX - sceneX;
834
            double gapY = originY - sceneY;
835

    
836
            // SPPID Symbol과 ID2 심볼의 크기 차이
837
            double sizeWidth = 0;
838
            double sizeHeight = 0;
839
            SPPIDUtil.ConvertPointBystring(targetSymbol.SIZE, ref sizeWidth, ref sizeHeight);
840
            if (sizeWidth == 0 || sizeHeight == 0)
841
                throw new Exception("Check symbol size! \r\nUID : " + targetSymbol.UID);
842

    
843
            double percentX = (x2 - x1) / sizeWidth;
844
            double percentY = (y2 - y1) / sizeHeight;
845

    
846
            double SPPIDgapX = gapX * percentX;
847
            double SPPIDgapY = gapY * percentY;
848

    
849
            double[] SPPIDOriginPoint = new double[] { _TargetItem.get_XCoordinate() - SPPIDgapX, _TargetItem.get_YCoordinate() + SPPIDgapY };
850
            double distance = double.MaxValue;
851
            double[] resultPoint;
852
            foreach (var point in points)
853
            {
854
                double result = SPPIDUtil.CalcPointToPointdDistance(point[0], point[1], SPPIDOriginPoint[0], SPPIDOriginPoint[1]);
855
                if (distance > result)
856
                {
857
                    distance = result;
858
                    resultPoint = point;
859
                    x = point[0];
860
                    y = point[1];
861
                }
862
            }
863

    
864
            ReleaseCOMObjects(_TargetItem);
865
        }
866

    
867
        private void GetTargetLineConnectorPoint(Connector targetConnector, Line targetLine, ref double x, ref double y)
868
        {
869
            int index = targetLine.CONNECTORS.IndexOf(targetConnector);
870
            if (index == 0)
871
            {
872
                x = targetLine.SPPID.START_X;
873
                y = targetLine.SPPID.START_Y;
874
            }
875
            else
876
            {
877
                x = targetLine.SPPID.END_X;
878
                y = targetLine.SPPID.END_Y;
879
            }
880
        }
881

    
882
        /// <summary>
883
        /// SPPID Symbol의 Range를 구한다.
884
        /// </summary>
885
        /// <param name="symbol"></param>
886
        /// <param name="range"></param>
887
        private void GetSPPIDSymbolRangeAndConnectionPoints(Symbol symbol, ref double[] range, List<double[]> points)
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
            range = new double[] { x1, y1, x2, y2 };
897

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

    
908
            foreach (var childSymbol in symbol.ChildSymbols)
909
                GetSPPIDChildSymbolRange(childSymbol, ref range, points);
910

    
911
            ReleaseCOMObjects(_TargetItem);
912
        }
913

    
914
        private void GetSPPIDSymbolRange(Symbol symbol, ref double[] range)
915
        {
916
            LMSymbol _TargetItem = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
917
            Ingr.RAD2D.Symbol2d symbol2d = radApp.ActiveDocument.ActiveSheet.DrawingObjects[_TargetItem.get_GraphicOID().ToString()];
918
            double x1 = 0;
919
            double y1 = 0;
920
            double x2 = 0;
921
            double y2 = 0;
922
            symbol2d.Range(out x1, out y1, out x2, out y2);
923
            range = new double[] { x1, y1, x2, y2 };
924

    
925
            foreach (var childSymbol in symbol.ChildSymbols)
926
                GetSPPIDChildSymbolRange(childSymbol, ref range);
927

    
928
            ReleaseCOMObjects(_TargetItem);
929
        }
930

    
931
        private void GetSPPIDSymbolRange(List<Symbol> symbols, ref double[] range)
932
        {
933
            double[] tempRange = new double[] { double.MaxValue, double.MaxValue, double.MinValue, double.MinValue };
934
            foreach (var symbol in symbols)
935
            {
936
                LMSymbol _TargetItem = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
937
                Ingr.RAD2D.Symbol2d symbol2d = radApp.ActiveDocument.ActiveSheet.DrawingObjects[_TargetItem.get_GraphicOID().ToString()];
938
                double x1 = 0;
939
                double y1 = 0;
940
                double x2 = 0;
941
                double y2 = 0;
942
                symbol2d.Range(out x1, out y1, out x2, out y2);
943

    
944
                tempRange[0] = Math.Min(tempRange[0], x1);
945
                tempRange[1] = Math.Min(tempRange[1], y1);
946
                tempRange[2] = Math.Max(tempRange[2], x2);
947
                tempRange[3] = Math.Max(tempRange[3], y2);
948

    
949
                foreach (var childSymbol in symbol.ChildSymbols)
950
                    GetSPPIDChildSymbolRange(childSymbol, ref tempRange);
951

    
952
                ReleaseCOMObjects(_TargetItem);
953
            }
954

    
955
            range = tempRange;
956
        }
957

    
958
        /// <summary>
959
        /// Child Modeling 된 Symbol의 Range를 구한다.
960
        /// </summary>
961
        /// <param name="childSymbol"></param>
962
        /// <param name="range"></param>
963
        private void GetSPPIDChildSymbolRange(ChildSymbol childSymbol, ref double[] range, List<double[]> points)
964
        {
965
            LMSymbol _ChildSymbol = dataSource.GetSymbol(childSymbol.SPPID.RepresentationId);
966
            Ingr.RAD2D.Symbol2d symbol2d = radApp.ActiveDocument.ActiveSheet.DrawingObjects[_ChildSymbol.get_GraphicOID().ToString()];
967
            double x1 = 0;
968
            double y1 = 0;
969
            double x2 = 0;
970
            double y2 = 0;
971
            symbol2d.Range(out x1, out y1, out x2, out y2);
972
            range[0] = Math.Min(range[0], x1);
973
            range[1] = Math.Min(range[1], y1);
974
            range[2] = Math.Max(range[2], x2);
975
            range[3] = Math.Max(range[3], y2);
976

    
977
            for (int i = 1; i < int.MaxValue; i++)
978
            {
979
                double connX = 0;
980
                double connY = 0;
981
                if (_placement.PIDConnectPointLocation(_ChildSymbol, i, ref connX, ref connY))
982
                    points.Add(new double[] { connX, connY });
983
                else
984
                    break;
985
            }
986

    
987
            foreach (var loopChildSymbol in childSymbol.ChildSymbols)
988
                GetSPPIDChildSymbolRange(loopChildSymbol, ref range, points);
989

    
990
            ReleaseCOMObjects(_ChildSymbol);
991
        }
992

    
993
        private void GetSPPIDChildSymbolRange(ChildSymbol childSymbol, ref double[] range)
994
        {
995
            LMSymbol _ChildSymbol = dataSource.GetSymbol(childSymbol.SPPID.RepresentationId);
996
            Ingr.RAD2D.Symbol2d symbol2d = radApp.ActiveDocument.ActiveSheet.DrawingObjects[_ChildSymbol.get_GraphicOID().ToString()];
997
            double x1 = 0;
998
            double y1 = 0;
999
            double x2 = 0;
1000
            double y2 = 0;
1001
            symbol2d.Range(out x1, out y1, out x2, out y2);
1002
            range[0] = Math.Min(range[0], x1);
1003
            range[1] = Math.Min(range[1], y1);
1004
            range[2] = Math.Max(range[2], x2);
1005
            range[3] = Math.Max(range[3], y2);
1006

    
1007
            foreach (var loopChildSymbol in childSymbol.ChildSymbols)
1008
                GetSPPIDChildSymbolRange(loopChildSymbol, ref range);
1009

    
1010
            ReleaseCOMObjects(_ChildSymbol);
1011
        }
1012

    
1013
        /// <summary>
1014
        /// Label Symbol Modeling
1015
        /// </summary>
1016
        /// <param name="symbol"></param>
1017
        private void LabelSymbolModeling(Symbol symbol)
1018
        {
1019
            if (string.IsNullOrEmpty(symbol.SPPID.RepresentationId))
1020
            {
1021
                BaseModel.Attribute itemAttribute = symbol.ATTRIBUTES.Find(x => x.ATTRIBUTE == "OWNERSYMBOL");
1022
                if (itemAttribute == null || string.IsNullOrEmpty(itemAttribute.VALUE) || itemAttribute.VALUE == "None")
1023
                    return;
1024
                Array points = new double[] { 0, symbol.SPPID.ORIGINAL_X, symbol.SPPID.ORIGINAL_Y };
1025

    
1026
                string symbolUID = itemAttribute.VALUE;
1027
                object targetItem = SPPIDUtil.FindObjectByUID(document, symbolUID);
1028
                if (targetItem != null &&
1029
                    (targetItem.GetType() == typeof(Symbol) ||
1030
                    targetItem.GetType() == typeof(Equipment)))
1031
                {
1032
                    // Object 아이템이 Symbol일 경우 Equipment일 경우 
1033
                    string sRep = null;
1034
                    if (targetItem.GetType() == typeof(Symbol))
1035
                        sRep = ((Symbol)targetItem).SPPID.RepresentationId;
1036
                    else if (targetItem.GetType() == typeof(Equipment))
1037
                        sRep = ((Equipment)targetItem).SPPID.RepresentationId;
1038
                    if (!string.IsNullOrEmpty(sRep))
1039
                    {
1040
                        // LEADER Line 검사
1041
                        bool leaderLine = false;
1042
                        SymbolMapping symbolMapping = document.SymbolMappings.Find(x => x.UID == symbol.DBUID);
1043
                        if (symbolMapping != null)
1044
                            leaderLine = symbolMapping.LEADERLINE;
1045

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

    
1050
                        //Leader 선 센터로
1051
                        if (_LMLabelPresist != null)
1052
                        {
1053
                            // Target Item에 Label의 Attribute Input
1054
                            InputSymbolAttribute(targetItem, symbol.ATTRIBUTES);
1055

    
1056
                            string OID = _LMLabelPresist.get_GraphicOID();
1057
                            DependencyObject dependency = radApp.ActiveDocument.ActiveSheet.DrawingObjects[OID] as DependencyObject;
1058
                            if (dependency != null)
1059
                            {
1060
                                bool result = false;
1061
                                foreach (var attributes in dependency.AttributeSets)
1062
                                {
1063
                                    foreach (var attribute in attributes)
1064
                                    {
1065
                                        string name = attribute.Name;
1066
                                        string value = attribute.GetValue().ToString();
1067
                                        if (name == "DrawingItemType" && value == "LabelPersist")
1068
                                        {
1069
                                            foreach (DrawingObjectBase drawingObject in dependency.DrawingObjects)
1070
                                            {
1071
                                                if (drawingObject.Type == Ingr.RAD2D.ObjectType.igLineString2d)
1072
                                                {
1073
                                                    Ingr.RAD2D.LineString2d lineString2D = drawingObject as Ingr.RAD2D.LineString2d;
1074
                                                    double prevX = _TargetItem.get_XCoordinate();
1075
                                                    double prevY = _TargetItem.get_YCoordinate();
1076
                                                    lineString2D.InsertVertex(lineString2D.VertexCount, prevX, prevY);
1077
                                                    lineString2D.RemoveVertex(lineString2D.VertexCount);
1078
                                                    result = true;
1079
                                                    break;
1080
                                                }
1081
                                            }
1082
                                        }
1083

    
1084
                                        if (result)
1085
                                            break;
1086
                                    }
1087

    
1088
                                    if (result)
1089
                                        break;
1090
                                }
1091
                            }
1092

    
1093
                            _LMLabelPresist.Commit();
1094
                            ReleaseCOMObjects(_LMLabelPresist);
1095
                        }
1096

    
1097
                        ReleaseCOMObjects(_TargetItem);
1098
                    }
1099
                }
1100
                else if (targetItem != null && targetItem.GetType() == typeof(Line))
1101
                {
1102
                    Line targetLine = targetItem as Line;
1103
                    Dictionary<LMConnector, List<double[]>> connectorVertices = GetPipeRunVertices(targetLine.SPPID.ModelItemId);
1104
                    LMConnector connectedLMConnector = FindTargetLMConnectorForLabel(connectorVertices, symbol.SPPID.ORIGINAL_X, symbol.SPPID.ORIGINAL_Y);
1105
                    if (connectedLMConnector != null)
1106
                    {
1107
                        // LEADER Line 검사
1108
                        bool leaderLine = false;
1109
                        SymbolMapping symbolMapping = document.SymbolMappings.Find(x => x.UID == symbol.DBUID);
1110
                        if (symbolMapping != null)
1111
                            leaderLine = symbolMapping.LEADERLINE;
1112

    
1113
                        LMLabelPersist _LMLabelPresist = _placement.PIDPlaceLabel(symbol.SPPID.MAPPINGNAME, ref points, Rotation: 0, LabeledItem: connectedLMConnector.AsLMRepresentation(), IsLeaderVisible: leaderLine);
1114
                        if (_LMLabelPresist != null)
1115
                        {
1116
                            _LMLabelPresist.Commit();
1117
                            ReleaseCOMObjects(_LMLabelPresist);
1118
                        }
1119
                        ReleaseCOMObjects(connectedLMConnector);
1120
                    }
1121

    
1122
                    foreach (var item in connectorVertices)
1123
                        if (item.Key != null)
1124
                            ReleaseCOMObjects(item.Key);
1125
                }
1126
            }
1127
        }
1128

    
1129
        /// <summary>
1130
        /// Equipment를 실제로 Modeling 메서드
1131
        /// </summary>
1132
        /// <param name="equipment"></param>
1133
        private void EquipmentModeling(Equipment equipment)
1134
        {
1135
            if (!string.IsNullOrEmpty(equipment.SPPID.RepresentationId))
1136
                return;
1137

    
1138
            LMSymbol _LMSymbol = null;
1139
            LMSymbol targetItem = null;
1140
            string mappingPath = equipment.SPPID.MAPPINGNAME;
1141
            double x = equipment.SPPID.ORIGINAL_X;
1142
            double y = equipment.SPPID.ORIGINAL_Y;
1143
            int mirror = 0;
1144
            double angle = equipment.ANGLE;
1145

    
1146
            SPPIDUtil.ConvertGridPoint(ref x, ref y);
1147

    
1148
            Connector connector = equipment.CONNECTORS.Find(conn => !string.IsNullOrEmpty(conn.CONNECTEDITEM) && conn.CONNECTEDITEM != "None");
1149
            if (connector != null)
1150
            {
1151
                Equipment connEquipment = SPPIDUtil.FindObjectByUID(document, connector.CONNECTEDITEM) as Equipment;
1152
                if (connEquipment != null)
1153
                {
1154
                    if (string.IsNullOrEmpty(connEquipment.SPPID.RepresentationId))
1155
                        EquipmentModeling(connEquipment);
1156

    
1157
                    if (!string.IsNullOrEmpty(connEquipment.SPPID.RepresentationId))
1158
                    {
1159
                        targetItem = dataSource.GetSymbol(connEquipment.SPPID.RepresentationId);
1160
                        if (targetItem != null)
1161
                        {
1162
                            _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle, TargetItem: targetItem);
1163
                        }
1164
                        else
1165
                        {
1166
                            _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle);
1167
                        }
1168
                    }
1169
                    else
1170
                    {
1171
                        _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle);
1172
                    }
1173
                }
1174
                else
1175
                {
1176
                    _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle);
1177
                }
1178
            }
1179
            else
1180
            {
1181
                _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle);
1182
            }
1183

    
1184
            if (_LMSymbol != null)
1185
            {
1186
                _LMSymbol.Commit();
1187
                equipment.SPPID.RepresentationId = _LMSymbol.AsLMRepresentation().Id;
1188
                equipment.SPPID.GraphicOID = _LMSymbol.get_GraphicOID();
1189
                ReleaseCOMObjects(_LMSymbol);
1190
            }
1191

    
1192
            if (targetItem != null)
1193
            {
1194
                ReleaseCOMObjects(targetItem);
1195
            }
1196

    
1197
            ReleaseCOMObjects(_LMSymbol);
1198
        }
1199

    
1200
        /// <summary>
1201
        /// 첫 진입점
1202
        /// </summary>
1203
        /// <param name="symbol"></param>
1204
        private void SymbolModelingBySymbol(Symbol symbol)
1205
        {
1206
            SymbolModeling(symbol, null);
1207
            List<object> endObjects = new List<object>();
1208
            endObjects.Add(symbol);
1209

    
1210
            foreach (var connector in symbol.CONNECTORS)
1211
            {
1212
                object connItem = SPPIDUtil.FindObjectByUID(document, connector.CONNECTEDITEM);
1213
                if (connItem != null && connItem.GetType() != typeof(Equipment))
1214
                {
1215
                    endObjects.Add(connItem);
1216
                    if (connItem.GetType() == typeof(Symbol))
1217
                    {
1218
                        Symbol connSymbol = connItem as Symbol;
1219
                        if (string.IsNullOrEmpty(connSymbol.SPPID.RepresentationId))
1220
                        {
1221
                            SymbolModeling(connSymbol, symbol);
1222
                        }
1223
                        SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetProgress, document.SYMBOLS.FindAll(x => !string.IsNullOrEmpty(x.SPPID.RepresentationId)).Count);
1224
                        SymbolModelingByNeerSymbolLoop(connSymbol, endObjects);
1225
                    }
1226
                    else if (connItem.GetType() == typeof(Line))
1227
                    {
1228
                        Line connLine = connItem as Line;
1229
                        SymbolModelingByNeerLineLoop(connLine, endObjects, symbol);
1230
                    }
1231
                }
1232
            }
1233
        }
1234

    
1235
        private void SymbolModelingByNeerSymbolLoop(Symbol symbol, List<object> endObjects)
1236
        {
1237
            foreach (var connector in symbol.CONNECTORS)
1238
            {
1239
                object connItem = SPPIDUtil.FindObjectByUID(document, connector.CONNECTEDITEM);
1240
                if (connItem != null && connItem.GetType() != typeof(Equipment))
1241
                {
1242
                    if (!endObjects.Contains(connItem))
1243
                    {
1244
                        endObjects.Add(connItem);
1245
                        if (connItem.GetType() == typeof(Symbol))
1246
                        {
1247
                            Symbol connSymbol = connItem as Symbol;
1248
                            if (string.IsNullOrEmpty(connSymbol.SPPID.RepresentationId))
1249
                            {
1250
                                SymbolModeling(connSymbol, symbol);
1251
                            }
1252
                            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetProgress, document.SYMBOLS.FindAll(x => !string.IsNullOrEmpty(x.SPPID.RepresentationId)).Count);
1253
                            SymbolModelingByNeerSymbolLoop(connSymbol, endObjects);
1254
                        }
1255
                        else if (connItem.GetType() == typeof(Line))
1256
                        {
1257
                            Line connLine = connItem as Line;
1258
                            SymbolModelingByNeerLineLoop(connLine, endObjects, symbol);
1259
                        }
1260
                    }
1261
                }
1262
            }
1263
        }
1264

    
1265
        private void SymbolModelingByNeerLineLoop(Line line, List<object> endObjects, Symbol prevSymbol)
1266
        {
1267
            foreach (var connector in line.CONNECTORS)
1268
            {
1269
                object connItem = SPPIDUtil.FindObjectByUID(document, connector.CONNECTEDITEM);
1270
                if (connItem != null && connItem.GetType() != typeof(Equipment))
1271
                {
1272
                    if (!endObjects.Contains(connItem))
1273
                    {
1274
                        endObjects.Add(connItem);
1275
                        if (connItem.GetType() == typeof(Symbol))
1276
                        {
1277
                            Symbol connSymbol = connItem as Symbol;
1278
                            if (string.IsNullOrEmpty(connSymbol.SPPID.RepresentationId))
1279
                            {
1280
                                List<Symbol> group = new List<Symbol>();
1281
                                SPPIDUtil.FindConnectedSymbolGroup(document, connSymbol, group);
1282
                                Symbol priority = prioritySymbols.Find(x => group.Contains(x));
1283
                                List<Symbol> endModelingGroup = new List<Symbol>();
1284
                                if (priority != null)
1285
                                {
1286
                                    SymbolGroupModeling(priority, group);
1287

    
1288
                                    // Range 겹치는지 확인해야함
1289
                                    double[] prevRange = null;
1290
                                    GetSPPIDSymbolRange(prevSymbol, ref prevRange);
1291
                                    double[] groupRange = null;
1292
                                    GetSPPIDSymbolRange(group, ref groupRange);
1293

    
1294
                                    double distanceX = 0;
1295
                                    double distanceY = 0;
1296
                                    bool overlapX = false;
1297
                                    bool overlapY = false;
1298
                                    SlopeType slopeType = SPPIDUtil.CalcSlope(prevSymbol.SPPID.ORIGINAL_X, prevSymbol.SPPID.ORIGINAL_Y, connSymbol.SPPID.ORIGINAL_X, connSymbol.SPPID.ORIGINAL_Y);
1299
                                    SPPIDUtil.CalcOverlap(prevRange, groupRange, ref distanceX, ref distanceY, ref overlapX, ref overlapY);
1300
                                    if ((slopeType == SlopeType.HORIZONTAL && overlapX) ||
1301
                                        (slopeType == SlopeType.VERTICAL && overlapY))
1302
                                    {
1303
                                        RemoveSymbol(group);
1304
                                        foreach (var _temp in group)
1305
                                            SPPIDUtil.CalcNewCoordinateForSymbol(_temp, prevSymbol, distanceX, distanceY);
1306

    
1307
                                        SymbolGroupModeling(priority, group);
1308
                                    }
1309
                                }
1310
                                else
1311
                                {
1312
                                    SymbolModeling(connSymbol, null);
1313
                                    // Range 겹치는지 확인해야함
1314
                                    double[] prevRange = null;
1315
                                    GetSPPIDSymbolRange(prevSymbol, ref prevRange);
1316
                                    double[] connRange = null;
1317
                                    GetSPPIDSymbolRange(connSymbol, ref connRange);
1318

    
1319
                                    double distanceX = 0;
1320
                                    double distanceY = 0;
1321
                                    bool overlapX = false;
1322
                                    bool overlapY = false;
1323
                                    SlopeType slopeType = SPPIDUtil.CalcSlope(prevSymbol.SPPID.ORIGINAL_X, prevSymbol.SPPID.ORIGINAL_Y, connSymbol.SPPID.ORIGINAL_X, connSymbol.SPPID.ORIGINAL_Y);
1324
                                    SPPIDUtil.CalcOverlap(prevRange, connRange, ref distanceX, ref distanceY, ref overlapX, ref overlapY);
1325
                                    if ((slopeType == SlopeType.HORIZONTAL && overlapX) ||
1326
                                        (slopeType == SlopeType.VERTICAL && overlapY))
1327
                                    {
1328
                                        RemoveSymbol(connSymbol);
1329
                                        SPPIDUtil.CalcNewCoordinateForSymbol(connSymbol, prevSymbol, distanceX, distanceY);
1330

    
1331
                                        SymbolModeling(connSymbol, null);
1332
                                    }
1333
                                }
1334
                            }
1335
                            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetProgress, document.SYMBOLS.FindAll(x => !string.IsNullOrEmpty(x.SPPID.RepresentationId)).Count);
1336
                            SymbolModelingByNeerSymbolLoop(connSymbol, endObjects);
1337
                        }
1338
                        else if (connItem.GetType() == typeof(Line))
1339
                        {
1340
                            Line connLine = connItem as Line;
1341
                            if (!SPPIDUtil.IsBranchLine(connLine, line))
1342
                                SymbolModelingByNeerLineLoop(connLine, endObjects, prevSymbol);
1343
                        }
1344
                    }
1345
                }
1346
            }
1347
        }
1348

    
1349
        private void SymbolGroupModeling(Symbol firstSymbol, List<Symbol> group)
1350
        {
1351
            List<Symbol> endModelingGroup = new List<Symbol>();
1352
            SymbolModeling(firstSymbol, null);
1353
            endModelingGroup.Add(firstSymbol);
1354
            while (endModelingGroup.Count != group.Count)
1355
            {
1356
                foreach (var _symbol in group)
1357
                {
1358
                    if (!endModelingGroup.Contains(_symbol))
1359
                    {
1360
                        foreach (var _connector in _symbol.CONNECTORS)
1361
                        {
1362
                            Symbol _connSymbol = SPPIDUtil.FindObjectByUID(document, _connector.CONNECTEDITEM) as Symbol;
1363
                            if (_connSymbol != null && endModelingGroup.Contains(_connSymbol))
1364
                            {
1365
                                SymbolModeling(_symbol, _connSymbol);
1366
                                endModelingGroup.Add(_symbol);
1367
                                break;
1368
                            }
1369
                        }
1370
                    }
1371
                }
1372
            }
1373
        }
1374

    
1375
        /// <summary>
1376
        /// 심볼을 실제로 Modeling할때 ChildSymbol이 있다면 Modeling하는 메서드
1377
        /// </summary>
1378
        /// <param name="childSymbol"></param>
1379
        /// <param name="parentSymbol"></param>
1380
        private void CreateChildSymbol(ChildSymbol childSymbol, LMSymbol parentSymbol)
1381
        {
1382
            Ingr.RAD2D.Symbol2d symbol2d = radApp.ActiveDocument.ActiveSheet.DrawingObjects[parentSymbol.get_GraphicOID().ToString()];
1383
            double x1 = 0;
1384
            double x2 = 0;
1385
            double y1 = 0;
1386
            double y2 = 0;
1387
            symbol2d.Range(out x1, out y1, out x2, out y2);
1388

    
1389
            LMSymbol _LMSymbol = _placement.PIDPlaceSymbol(childSymbol.SPPID.MAPPINGNAME, (x1 + x2) / 2, (y1 + y2) / 2, TargetItem: parentSymbol);
1390
            if (_LMSymbol != null)
1391
            {
1392
                childSymbol.SPPID.RepresentationId = _LMSymbol.AsLMRepresentation().Id;
1393
                foreach (var item in childSymbol.ChildSymbols)
1394
                    CreateChildSymbol(item, _LMSymbol);
1395
            }
1396

    
1397

    
1398
            ReleaseCOMObjects(_LMSymbol);
1399
        }
1400

    
1401
        private void NewLineModeling(Line line, bool isBranchModeling = false, Tuple<LMConnector, bool, bool> reModelingInfo = null)
1402
        {
1403
            if (!string.IsNullOrEmpty(line.SPPID.ModelItemId) || (NewBranchLines.Contains(line) && !isBranchModeling))
1404
                return;
1405

    
1406
            List<Line> group = new List<Line>();
1407
            GetConnectedLineGroup(line, group);
1408
            LineCoordinateCorrection(group);
1409

    
1410
            foreach (var groupLine in group)
1411
            {
1412
                if (!isBranchModeling && SPPIDUtil.IsBranchLine(groupLine))
1413
                {
1414
                    NewBranchLines.Add(groupLine);
1415
                    continue;
1416
                }
1417

    
1418
                _LMAItem _LMAItem = _placement.PIDCreateItem(groupLine.SPPID.MAPPINGNAME);
1419
                LMSymbol _LMSymbolStart = null;
1420
                LMSymbol _LMSymbolEnd = null;
1421
                PlaceRunInputs placeRunInputs = new PlaceRunInputs();
1422
                foreach (var connector in groupLine.CONNECTORS)
1423
                {
1424
                    double x = 0;
1425
                    double y = 0;
1426
                    GetTargetLineConnectorPoint(connector, groupLine, ref x, ref y);
1427
                    if (connector.ConnectedObject == null)
1428
                    {
1429
                        placeRunInputs.AddPoint(x, y);
1430
                    }
1431
                    else if (connector.ConnectedObject.GetType() == typeof(Symbol))
1432
                    {
1433
                        Symbol targetSymbol = connector.ConnectedObject as Symbol;
1434
                        if (groupLine.CONNECTORS.IndexOf(connector) == 0)
1435
                        {
1436
                            _LMSymbolStart = GetTargetSymbol(targetSymbol, groupLine);
1437
                            placeRunInputs.AddSymbolTarget(_LMSymbolStart, x, y);
1438
                        }
1439
                        else
1440
                        {
1441
                            _LMSymbolEnd = GetTargetSymbol(targetSymbol, groupLine);
1442
                            placeRunInputs.AddSymbolTarget(_LMSymbolEnd, x, y);
1443
                        }
1444
                    }
1445
                    else if (connector.ConnectedObject.GetType() == typeof(Line))
1446
                    {
1447
                        Line targetLine = connector.ConnectedObject as Line;
1448
                        if (!string.IsNullOrEmpty(targetLine.SPPID.ModelItemId))
1449
                        {
1450
                            LMConnector targetConnector = FindTargetLMConnectorForBranch(line, targetLine, ref x, ref y);
1451
                            placeRunInputs.AddConnectorTarget(targetConnector, x, y);
1452
                            ChangeLineSPPIDCoordinateByConnector(groupLine, targetLine, x, y, false);
1453
                        }
1454
                        else
1455
                        {
1456
                            if (groupLine.CONNECTORS.IndexOf(connector) == 0)
1457
                            {
1458
                                if (groupLine.SlopeType == SlopeType.HORIZONTAL)
1459
                                    placeRunInputs.AddPoint(x, -0.1);
1460
                                else if (groupLine.SlopeType == SlopeType.VERTICAL)
1461
                                    placeRunInputs.AddPoint(-0.1, y);
1462
                                else
1463
                                    placeRunInputs.AddPoint(x, -0.1);
1464
                            }
1465

    
1466
                            placeRunInputs.AddPoint(x, y);
1467

    
1468
                            if (groupLine.CONNECTORS.IndexOf(connector) == 1)
1469
                            {
1470
                                if (groupLine.SlopeType == SlopeType.HORIZONTAL)
1471
                                    placeRunInputs.AddPoint(x, -0.1);
1472
                                else if (groupLine.SlopeType == SlopeType.VERTICAL)
1473
                                    placeRunInputs.AddPoint(-0.1, y);
1474
                                else
1475
                                    placeRunInputs.AddPoint(x, -0.1);
1476
                            }
1477
                        }
1478
                    }
1479
                }
1480

    
1481
                LMConnector _lMConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
1482
                if (_lMConnector != null)
1483
                {
1484
                    groupLine.SPPID.ModelItemId = _lMConnector.ModelItemID;
1485
                    bool bRemodelingStart = false;
1486
                    if (_LMSymbolStart != null)
1487
                        NeedReModeling(groupLine, _LMSymbolStart, ref bRemodelingStart);
1488
                    bool bRemodelingEnd = false;
1489
                    if (_LMSymbolEnd != null)
1490
                        NeedReModeling(groupLine, _LMSymbolEnd, ref bRemodelingEnd);
1491

    
1492
                    if (bRemodelingStart || bRemodelingEnd)
1493
                        ReModelingLine(groupLine, _lMConnector, _LMSymbolStart, _LMSymbolEnd, bRemodelingStart, bRemodelingEnd);
1494
                    
1495
                    ReleaseCOMObjects(_lMConnector);
1496
                }
1497

    
1498
                List<object> removeLines = groupLine.CONNECTORS.FindAll(x =>
1499
                x.ConnectedObject != null &&
1500
                x.ConnectedObject.GetType() == typeof(Line) &&
1501
                !string.IsNullOrEmpty(((Line)x.ConnectedObject).SPPID.ModelItemId))
1502
                .Select(x => x.ConnectedObject)
1503
                .ToList();
1504

    
1505
                foreach (var item in removeLines)
1506
                    RemoveLineForModeling(item as Line);
1507

    
1508
                if (_LMAItem != null)
1509
                    ReleaseCOMObjects(_LMAItem);
1510
                if (placeRunInputs != null)
1511
                    ReleaseCOMObjects(placeRunInputs);
1512
                if (_LMSymbolStart != null)
1513
                    ReleaseCOMObjects(_LMSymbolStart);
1514
                if (_LMSymbolEnd != null)
1515
                    ReleaseCOMObjects(_LMSymbolEnd);
1516

    
1517
                if (isBranchModeling && NewBranchLines.Contains(groupLine))
1518
                    NewBranchLines.Remove(groupLine);
1519
            }
1520
        }
1521

    
1522
        private void RemoveLineForModeling(Line line)
1523
        {
1524
            LMModelItem modelItem = dataSource.GetModelItem(line.SPPID.ModelItemId);
1525
            if (modelItem != null)
1526
            {
1527
                foreach (LMRepresentation rep in modelItem.Representations)
1528
                {
1529
                    if (rep.Attributes["RepresentationType"].get_Value() == "Connector" && rep.Attributes["ItemStatus"].get_Value() == "Active")
1530
                    {
1531
                        LMConnector _LMConnector = dataSource.GetConnector(rep.Id);
1532
                        dynamic OID = rep.get_GraphicOID();
1533
                        DependencyObject drawingObject = radApp.ActiveDocument.ActiveSheet.DrawingObjects[OID];
1534
                        Ingr.RAD2D.LineStringGeometry2d lineStringGeometry = drawingObject.GetGeometry() as Ingr.RAD2D.LineStringGeometry2d;
1535
                        int verticesCount = lineStringGeometry.VertexCount;
1536
                        double[] vertices = null;
1537
                        lineStringGeometry.GetVertices(ref verticesCount, ref vertices);
1538
                        for (int i = 0; i < verticesCount; i++)
1539
                        {
1540
                            double x = 0;
1541
                            double y = 0;
1542
                            lineStringGeometry.GetVertex(i + 1, ref x, ref y);
1543
                            if (verticesCount == 2 && x < 0 || y < 0)
1544
                                _placement.PIDRemovePlacement(rep);
1545
                        }
1546
                        ReleaseCOMObjects(_LMConnector);
1547
                    }
1548
                }
1549

    
1550
                ReleaseCOMObjects(modelItem);
1551
            }
1552
        }
1553

    
1554
        private void GetConnectedLineGroup(Line line, List<Line> group)
1555
        {
1556
            if (!group.Contains(line))
1557
                group.Add(line);
1558
            foreach (var connector in line.CONNECTORS)
1559
            {
1560
                if (connector.ConnectedObject != null &&
1561
                    connector.ConnectedObject.GetType() == typeof(Line) &&
1562
                    !group.Contains(connector.ConnectedObject) &&
1563
                    string.IsNullOrEmpty(((Line)connector.ConnectedObject).SPPID.ModelItemId))
1564
                {
1565
                    Line connLine = connector.ConnectedObject as Line;
1566
                    if (!SPPIDUtil.IsBranchLine(connLine, line))
1567
                        GetConnectedLineGroup(connLine, group);
1568
                }
1569
            }
1570
        }
1571

    
1572
        private void LineCoordinateCorrection(List<Line> group)
1573
        {
1574
            // 순서대로 전 Item 기준 정렬
1575
            LineCoordinateCorrectionByStart(group);
1576

    
1577
            // 역으로 심볼이 있을 경우 좌표 보정
1578
            LineCoordinateCorrectionForLastLine(group);
1579
        }
1580

    
1581
        private void LineCoordinateCorrectionByStart(List<Line> group)
1582
        {
1583
            for (int i = 0; i < group.Count; i++)
1584
            {
1585
                Line line = group[i];
1586
                if (i == 0)
1587
                {
1588
                    Connector symbolConnector = line.CONNECTORS.Find(x => x.ConnectedObject != null && x.ConnectedObject.GetType() == typeof(Symbol));
1589
                    if (symbolConnector != null)
1590
                        LineCoordinateCorrectionByConnItem(line, symbolConnector.ConnectedObject);
1591
                }
1592
                else if (i != 0)
1593
                {
1594
                    LineCoordinateCorrectionByConnItem(line, group[i - 1]);
1595
                }
1596
            }
1597
        }
1598

    
1599
        private void LineCoordinateCorrectionForLastLine(List<Line> group)
1600
        {
1601
            Line checkLine = group[group.Count - 1];
1602
            Connector lastSymbolConnector = checkLine.CONNECTORS.Find(x => x.ConnectedObject != null && x.ConnectedObject.GetType() == typeof(Symbol));
1603
            if (lastSymbolConnector != null)
1604
            {
1605
                LineCoordinateCorrectionByConnItem(checkLine, lastSymbolConnector.ConnectedObject);
1606
                for (int i = group.Count - 2; i >= 0; i--)
1607
                {
1608
                    Line line = group[i + 1];
1609
                    Line prevLine = group[i];
1610

    
1611
                    // 같으면 보정
1612
                    if (line.SlopeType == prevLine.SlopeType)
1613
                        LineCoordinateCorrectionByConnItem(prevLine, line);
1614
                    else
1615
                    {
1616
                        if (line.SlopeType == SlopeType.HORIZONTAL)
1617
                        {
1618
                            double prevX = 0;
1619
                            double prevY = 0;
1620
                            GetTargetLineConnectorPoint(prevLine.CONNECTORS.Find(z => z.ConnectedObject == line), prevLine, ref prevX, ref prevY);
1621
                            ChangeLineSPPIDCoordinateByConnectorOnlyX(line, prevLine, prevX);
1622

    
1623
                            double x = 0;
1624
                            double y = 0;
1625
                            GetTargetLineConnectorPoint(line.CONNECTORS.Find(z => z.ConnectedObject == prevLine), line, ref x, ref y);
1626
                            ChangeLineSPPIDCoordinateByConnectorOnlyY(prevLine, line, y);
1627
                        }
1628
                        else if (line.SlopeType == SlopeType.VERTICAL)
1629
                        {
1630
                            double prevX = 0;
1631
                            double prevY = 0;
1632
                            GetTargetLineConnectorPoint(prevLine.CONNECTORS.Find(z => z.ConnectedObject == line), prevLine, ref prevX, ref prevY);
1633
                            ChangeLineSPPIDCoordinateByConnectorOnlyY(line, prevLine, prevY);
1634

    
1635
                            double x = 0;
1636
                            double y = 0;
1637
                            GetTargetLineConnectorPoint(line.CONNECTORS.Find(z => z.ConnectedObject == prevLine), line, ref x, ref y);
1638
                            ChangeLineSPPIDCoordinateByConnectorOnlyX(prevLine, line, x);
1639
                        }
1640
                        break;
1641
                    }
1642
                }
1643
            }
1644
        }
1645

    
1646
        private void LineCoordinateCorrectionByConnItem(Line line, object connItem)
1647
        {
1648
            double x = 0;
1649
            double y = 0;
1650
            if (connItem.GetType() == typeof(Symbol))
1651
            {
1652
                Symbol targetSymbol = connItem as Symbol;
1653
                Connector targetConnector = targetSymbol.CONNECTORS.Find(z => z.ConnectedObject == line);
1654
                if (targetConnector != null)
1655
                    GetTargetSymbolConnectorPoint(targetConnector, targetSymbol, ref x, ref y);
1656
                else
1657
                    throw new Exception("Target symbol UID : " + targetSymbol.UID + "\r\nLine UID : " + line.UID);
1658
            }
1659
            else if (connItem.GetType() == typeof(Line))
1660
            {
1661
                Line targetLine = connItem as Line;
1662
                GetTargetLineConnectorPoint(targetLine.CONNECTORS.Find(z => z.ConnectedObject == line), targetLine, ref x, ref y);
1663
            }
1664

    
1665
            ChangeLineSPPIDCoordinateByConnector(line, connItem, x, y);
1666
        }
1667

    
1668
        private void ChangeLineSPPIDCoordinateByConnector(Line line, object connItem, double x, double y, bool changeOtherCoordinate = true)
1669
        {
1670
            Connector connector = line.CONNECTORS.Find(z => z.ConnectedObject == connItem);
1671
            int index = line.CONNECTORS.IndexOf(connector);
1672
            if (index == 0)
1673
            {
1674
                line.SPPID.START_X = x;
1675
                line.SPPID.START_Y = y;
1676
                if (line.SlopeType == SlopeType.HORIZONTAL && changeOtherCoordinate)
1677
                    line.SPPID.END_Y = y;
1678
                else if (line.SlopeType == SlopeType.VERTICAL && changeOtherCoordinate)
1679
                    line.SPPID.END_X = x;
1680
            }
1681
            else
1682
            {
1683
                line.SPPID.END_X = x;
1684
                line.SPPID.END_Y = y;
1685
                if (line.SlopeType == SlopeType.HORIZONTAL && changeOtherCoordinate)
1686
                    line.SPPID.START_Y = y;
1687
                else if (line.SlopeType == SlopeType.VERTICAL && changeOtherCoordinate)
1688
                    line.SPPID.START_X = x;
1689
            }
1690
        }
1691

    
1692
        private void ChangeLineSPPIDCoordinateByConnectorOnlyX(Line line, object connItem, double x)
1693
        {
1694
            Connector connector = line.CONNECTORS.Find(z => z.ConnectedObject == connItem);
1695
            int index = line.CONNECTORS.IndexOf(connector);
1696
            if (index == 0)
1697
            {
1698
                line.SPPID.START_X = x;
1699
                if (line.SlopeType == SlopeType.VERTICAL)
1700
                    line.SPPID.END_X = x;
1701
            }
1702
            else
1703
            {
1704
                line.SPPID.END_X = x;
1705
                if (line.SlopeType == SlopeType.VERTICAL)
1706
                    line.SPPID.START_X = x;
1707
            }
1708
        }
1709

    
1710
        private void ChangeLineSPPIDCoordinateByConnectorOnlyY(Line line, object connItem, double y)
1711
        {
1712
            Connector connector = line.CONNECTORS.Find(z => z.ConnectedObject == connItem);
1713
            int index = line.CONNECTORS.IndexOf(connector);
1714
            if (index == 0)
1715
            {
1716
                line.SPPID.START_Y = y;
1717
                if (line.SlopeType == SlopeType.HORIZONTAL)
1718
                    line.SPPID.END_Y = y;
1719
            }
1720
            else
1721
            {
1722
                line.SPPID.END_Y = y;
1723
                if (line.SlopeType == SlopeType.HORIZONTAL)
1724
                    line.SPPID.START_Y = y;
1725
            }
1726
        }
1727

    
1728
        private void NeedReModeling(Line line, LMSymbol symbol, ref bool result)
1729
        {
1730
            if (symbol != null)
1731
            {
1732
                string repID = symbol.AsLMRepresentation().Id;
1733
                string symbolUID = SPPIDUtil.FindSymbolByRepresentationID(document, repID).UID;
1734
                string lineUID = line.UID;
1735

    
1736
                SpecBreak startSpecBreak = document.SpecBreaks.Find(x =>
1737
                (x.DownStreamUID == symbolUID || x.UpStreamUID == symbolUID) &&
1738
                (x.DownStreamUID == lineUID || x.UpStreamUID == lineUID));
1739

    
1740
                EndBreak startEndBreak = document.EndBreaks.Find(x =>
1741
                (x.OWNER == symbolUID || x.PROPERTIES.Find(y => y.ATTRIBUTE == "Connected Item").VALUE == symbolUID) &&
1742
                (x.OWNER == lineUID || x.PROPERTIES.Find(y => y.ATTRIBUTE == "Connected Item").VALUE == lineUID));
1743

    
1744
                if (startSpecBreak != null || startEndBreak != null)
1745
                    result = true;
1746
            }
1747
        }
1748

    
1749
        /// <summary>
1750
        /// Symbol에 붙을 경우 Line을 Remodeling 한다.
1751
        /// </summary>
1752
        /// <param name="lines"></param>
1753
        /// <param name="prevLMConnector"></param>
1754
        /// <param name="startSymbol"></param>
1755
        /// <param name="endSymbol"></param>
1756
        private void ReModelingLine(Line line, LMConnector prevLMConnector, LMSymbol startSymbol, LMSymbol endSymbol, bool bStart, bool bEnd)
1757
        {
1758
            string symbolPath = string.Empty;
1759
            #region get symbol path
1760
            LMModelItem modelItem = dataSource.GetModelItem(prevLMConnector.ModelItemID);
1761
            foreach (LMRepresentation rep in modelItem.Representations)
1762
            {
1763
                if (!DBNull.Value.Equals(rep.get_FileName()) && !string.IsNullOrEmpty(rep.get_FileName()))
1764
                {
1765
                    symbolPath = rep.get_FileName();
1766
                    break;
1767
                }
1768
            }
1769
            #endregion
1770
            _LMAItem _LMAItem = _placement.PIDCreateItem(symbolPath);
1771
            LMConnector newConnector = null;
1772
            dynamic OID = prevLMConnector.get_GraphicOID();
1773
            DependencyObject drawingObject = radApp.ActiveDocument.ActiveSheet.DrawingObjects[OID];
1774
            Ingr.RAD2D.LineStringGeometry2d lineStringGeometry = drawingObject.GetGeometry() as Ingr.RAD2D.LineStringGeometry2d;
1775
            int verticesCount = lineStringGeometry.VertexCount;
1776
            PlaceRunInputs placeRunInputs = new PlaceRunInputs();
1777

    
1778
            List<double[]> vertices = new List<double[]>();
1779
            for (int i = 1; i <= verticesCount; i++)
1780
            {
1781
                double x = 0;
1782
                double y = 0;
1783
                lineStringGeometry.GetVertex(i, ref x, ref y);
1784
                vertices.Add(new double[] { x, y });
1785
            }
1786

    
1787
            for (int i = 0; i < vertices.Count; i++)
1788
            {
1789
                double[] points = vertices[i];
1790
                // 시작 심볼이 있고 첫번째 좌표일 때
1791
                if (startSymbol != null && i == 0)
1792
                {
1793
                    if (bStart)
1794
                    {
1795
                        SlopeType slopeType = SPPIDUtil.CalcSlope(points[0], points[1], vertices[i + 1][0], vertices[i + 1][1]);
1796
                        if (slopeType == SlopeType.HORIZONTAL)
1797
                            placeRunInputs.AddPoint(points[0], -0.1);
1798
                        else if (slopeType == SlopeType.VERTICAL)
1799
                            placeRunInputs.AddPoint(-0.1, points[1]);
1800
                        else
1801
                            placeRunInputs.AddPoint(points[0], -0.1);
1802

    
1803
                        placeRunInputs.AddPoint(points[0], points[1]);
1804
                    }
1805
                    else
1806
                    {
1807
                        placeRunInputs.AddSymbolTarget(startSymbol, points[0], points[1]);
1808
                    }
1809
                }
1810
                // 마지막 심볼이 있고 마지막 좌표일 때
1811
                else if (endSymbol != null && i == vertices.Count - 1)
1812
                {
1813
                    if (bEnd)
1814
                    {
1815
                        placeRunInputs.AddPoint(points[0], points[1]);
1816

    
1817
                        SlopeType slopeType = SPPIDUtil.CalcSlope(points[0], points[1], vertices[i - 1][0], vertices[i - 1][1]);
1818
                        if (slopeType == SlopeType.HORIZONTAL)
1819
                            placeRunInputs.AddPoint(points[0], -0.1);
1820
                        else if (slopeType == SlopeType.VERTICAL)
1821
                            placeRunInputs.AddPoint(-0.1, points[1]);
1822
                        else
1823
                            placeRunInputs.AddPoint(points[0], -0.1);
1824
                    }
1825
                    else
1826
                    {
1827
                        placeRunInputs.AddSymbolTarget(endSymbol, points[0], points[1]);
1828
                    }
1829
                }
1830
                // 첫번째이며 시작 심볼이 아니고 Connecotr일 경우
1831
                else if (i == 0 && prevLMConnector.ConnectItem1SymbolObject != null)
1832
                    placeRunInputs.AddSymbolTarget(prevLMConnector.ConnectItem1SymbolObject, points[0], points[1]);
1833
                // 마지막이며 마지막 심볼이 아니고 Connecotr일 경우
1834
                else if (i == vertices.Count - 1 && prevLMConnector.ConnectItem2SymbolObject != null)
1835
                    placeRunInputs.AddSymbolTarget(prevLMConnector.ConnectItem2SymbolObject, points[0], points[1]);
1836
                else
1837
                    placeRunInputs.AddPoint(points[0], points[1]);
1838
            }
1839

    
1840
            _placement.PIDRemovePlacement(prevLMConnector.AsLMRepresentation());
1841
            newConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
1842

    
1843
            ReleaseCOMObjects(placeRunInputs);
1844
            ReleaseCOMObjects(_LMAItem);
1845
            ReleaseCOMObjects(modelItem);
1846

    
1847
            if (newConnector != null)
1848
            {
1849
                if (startSymbol != null && bStart)
1850
                {
1851
                    _LMAItem = _placement.PIDCreateItem(symbolPath);
1852
                    placeRunInputs = new PlaceRunInputs();
1853
                    placeRunInputs.AddSymbolTarget(startSymbol, vertices[0][0], vertices[0][1]);
1854
                    placeRunInputs.AddConnectorTarget(newConnector, vertices[0][0], vertices[0][1]);
1855
                    LMConnector _LMConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
1856
                    if (_LMConnector != null)
1857
                    {
1858
                        RemoveConnectorForReModelingLine(newConnector);
1859
                        ZeroLengthModelItemID.Add(_LMConnector.ModelItemID);
1860
                        ReleaseCOMObjects(_LMConnector);
1861
                    }
1862
                    ReleaseCOMObjects(placeRunInputs);
1863
                    ReleaseCOMObjects(_LMAItem);
1864
                }
1865

    
1866
                if (endSymbol != null && bEnd)
1867
                {
1868
                    if (startSymbol != null)
1869
                    {
1870
                        Dictionary<LMConnector, List<double[]>> dicVertices = GetPipeRunVertices(newConnector.ModelItemID);
1871
                        newConnector = dicVertices.First().Key;
1872
                    }
1873

    
1874
                    _LMAItem = _placement.PIDCreateItem(symbolPath);
1875
                    placeRunInputs = new PlaceRunInputs();
1876
                    placeRunInputs.AddSymbolTarget(endSymbol, vertices[vertices.Count - 1][0], vertices[vertices.Count - 1][1]);
1877
                    placeRunInputs.AddConnectorTarget(newConnector, vertices[vertices.Count - 1][0], vertices[vertices.Count - 1][1]);
1878
                    LMConnector _LMConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
1879
                    if (_LMConnector != null)
1880
                    {
1881
                        RemoveConnectorForReModelingLine(newConnector);
1882
                        ZeroLengthModelItemID.Add(_LMConnector.ModelItemID);
1883
                        ReleaseCOMObjects(_LMConnector);
1884
                    }
1885
                    ReleaseCOMObjects(placeRunInputs);
1886
                    ReleaseCOMObjects(_LMAItem);
1887
                }
1888

    
1889
                line.SPPID.ModelItemId = newConnector.ModelItemID;
1890
                ReleaseCOMObjects(newConnector);
1891
            }
1892

    
1893
            ReleaseCOMObjects(modelItem);
1894
        }
1895

    
1896
        /// <summary>
1897
        /// Remodeling 과정에서 생긴 불필요한 Connector 제거
1898
        /// </summary>
1899
        /// <param name="connector"></param>
1900
        private void RemoveConnectorForReModelingLine(LMConnector connector)
1901
        {
1902
            Dictionary<LMConnector, List<double[]>> dicVertices = GetPipeRunVertices(connector.ModelItemID);
1903
            foreach (var item in dicVertices)
1904
            {
1905
                bool result = false;
1906
                foreach (var point in item.Value)
1907
                {
1908
                    if (point[0] < 0 || point[1] < 0)
1909
                    {
1910
                        result = true;
1911
                        _placement.PIDRemovePlacement(item.Key.AsLMRepresentation());
1912
                        break;
1913
                    }
1914
                }
1915

    
1916
                if (result)
1917
                    break;
1918
            }
1919
            foreach (var item in dicVertices)
1920
                ReleaseCOMObjects(item.Key);
1921
        }
1922

    
1923
        /// <summary>
1924
        /// Symbol이 모델링된 SPPPID Symbol Object를 반환 - 연결된 Symbol이 ChildSymbol일 수도 있기때문에 메서드 개발
1925
        /// </summary>
1926
        /// <param name="symbol"></param>
1927
        /// <param name="line"></param>
1928
        /// <returns></returns>
1929
        private LMSymbol GetTargetSymbol(Symbol symbol, Line line)
1930
        {
1931
            LMSymbol _LMSymbol = null;
1932
            foreach (var connector in symbol.CONNECTORS)
1933
            {
1934
                if (connector.CONNECTEDITEM == line.UID)
1935
                {
1936
                    if (connector.Index == 0)
1937
                        _LMSymbol = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
1938
                    else
1939
                    {
1940
                        ChildSymbol child = null;
1941
                        foreach (var childSymbol in symbol.ChildSymbols)
1942
                        {
1943
                            if (childSymbol.Connectors.Contains(connector))
1944
                                child = childSymbol;
1945
                            else
1946
                                child = GetChildSymbolByConnector(childSymbol, connector);
1947

    
1948
                            if (child != null)
1949
                                break;
1950
                        }
1951

    
1952
                        if (child != null)
1953
                            _LMSymbol = dataSource.GetSymbol(child.SPPID.RepresentationId);
1954
                    }
1955

    
1956
                    break;
1957
                }
1958
            }
1959

    
1960
            return _LMSymbol;
1961
        }
1962

    
1963
        /// <summary>
1964
        /// Connector를 가지고 있는 ChildSymbol Object 반환
1965
        /// </summary>
1966
        /// <param name="item"></param>
1967
        /// <param name="connector"></param>
1968
        /// <returns></returns>
1969
        private ChildSymbol GetChildSymbolByConnector(ChildSymbol item, Connector connector)
1970
        {
1971
            foreach (var childSymbol in item.ChildSymbols)
1972
            {
1973
                if (childSymbol.Connectors.Contains(connector))
1974
                    return childSymbol;
1975
                else
1976
                    return GetChildSymbolByConnector(childSymbol, connector);
1977
            }
1978

    
1979
            return null;
1980
        }
1981

    
1982
        /// <summary>
1983
        /// EndBreak 모델링 메서드
1984
        /// </summary>
1985
        /// <param name="endBreak"></param>
1986
        private void EndBreakModeling(EndBreak endBreak)
1987
        {
1988
            object ownerObj = SPPIDUtil.FindObjectByUID(document, endBreak.OWNER);
1989
            object connectedItem = SPPIDUtil.FindObjectByUID(document, endBreak.PROPERTIES.Find(x => x.ATTRIBUTE == "Connected Item").VALUE);
1990
            LMConnector targetLMConnector = FindBreakLineTarget(ownerObj, connectedItem);
1991

    
1992
            if (targetLMConnector != null)
1993
            {
1994
                Array array = new double[] { 0, endBreak.SPPID.ORIGINAL_X, endBreak.SPPID.ORIGINAL_Y };
1995
                LMLabelPersist _LmLabelPersist = _placement.PIDPlaceLabel(endBreak.SPPID.MAPPINGNAME, ref array, Rotation: 0, LabeledItem: targetLMConnector.AsLMRepresentation(), IsLeaderVisible: true);
1996
                if (_LmLabelPersist != null)
1997
                {
1998
                    endBreak.SPPID.RepresentationId = _LmLabelPersist.AsLMRepresentation().Id;
1999
                    endBreak.SPPID.ModelItemID = _LmLabelPersist.ModelItemID;
2000
                    endBreak.SPPID.GraphicOID = _LmLabelPersist.get_GraphicOID();
2001
                    ReleaseCOMObjects(_LmLabelPersist);
2002
                }
2003
                ReleaseCOMObjects(targetLMConnector);
2004
            }
2005
        }
2006

    
2007
        private LMConnector ReModelingLMConnector(LMConnector connector)
2008
        {
2009
            string symbolPath = string.Empty;
2010
            #region get symbol path
2011
            LMModelItem modelItem = dataSource.GetModelItem(connector.ModelItemID);
2012
            foreach (LMRepresentation rep in modelItem.Representations)
2013
            {
2014
                if (!DBNull.Value.Equals(rep.get_FileName()) && !string.IsNullOrEmpty(rep.get_FileName()))
2015
                {
2016
                    symbolPath = rep.get_FileName();
2017
                    break;
2018
                }
2019
            }
2020
            #endregion
2021

    
2022
            LMConnector newConnector = null;
2023
            dynamic OID = connector.get_GraphicOID();
2024
            DependencyObject drawingObject = radApp.ActiveDocument.ActiveSheet.DrawingObjects[OID];
2025
            Ingr.RAD2D.LineStringGeometry2d lineStringGeometry = drawingObject.GetGeometry() as Ingr.RAD2D.LineStringGeometry2d;
2026
            int verticesCount = lineStringGeometry.VertexCount;
2027
            PlaceRunInputs placeRunInputs = new PlaceRunInputs();
2028
            _LMAItem _LMAItem = _placement.PIDCreateItem(symbolPath);
2029

    
2030
            if (Convert.ToBoolean(connector.get_IsZeroLength()))
2031
            {
2032
                double[] vertices = null;
2033
                lineStringGeometry.GetVertices(ref verticesCount, ref vertices);
2034
                double x = 0;
2035
                double y = 0;
2036
                lineStringGeometry.GetVertex(1, ref x, ref y);
2037

    
2038
                placeRunInputs.AddSymbolTarget(connector.ConnectItem1SymbolObject, x, y);
2039
                placeRunInputs.AddSymbolTarget(connector.ConnectItem2SymbolObject, x, y);
2040

    
2041
                _placement.PIDRemovePlacement(connector.AsLMRepresentation());
2042
                newConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
2043
            }
2044
            else
2045
            {
2046
                List<double[]> vertices = new List<double[]>();
2047
                for (int i = 1; i <= verticesCount; i++)
2048
                {
2049
                    double x = 0;
2050
                    double y = 0;
2051
                    lineStringGeometry.GetVertex(i, ref x, ref y);
2052
                    vertices.Add(new double[] { x, y });
2053
                }
2054

    
2055
                for (int i = 0; i < vertices.Count; i++)
2056
                {
2057
                    double[] points = vertices[i];
2058
                    if (i == 0)
2059
                    {
2060
                        if (connector.ConnectItem1SymbolObject != null)
2061
                            placeRunInputs.AddSymbolTarget(connector.ConnectItem1SymbolObject, points[0], points[1]);
2062
                        else
2063
                            placeRunInputs.AddPoint(points[0], points[1]);
2064
                    }
2065
                    else if (i == vertices.Count - 1)
2066
                    {
2067
                        if (connector.ConnectItem2SymbolObject != null)
2068
                            placeRunInputs.AddSymbolTarget(connector.ConnectItem2SymbolObject, points[0], points[1]);
2069
                        else
2070
                            placeRunInputs.AddPoint(points[0], points[1]);
2071
                    }
2072
                    else
2073
                        placeRunInputs.AddPoint(points[0], points[1]);
2074
                }
2075

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

    
2078
                _placement.PIDRemovePlacement(connector.AsLMRepresentation());
2079
                newConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
2080

    
2081
                foreach (var line in lines)
2082
                    line.SPPID.ModelItemId = newConnector.ModelItemID;
2083
            }
2084

    
2085

    
2086
            return newConnector;
2087
        }
2088

    
2089
        /// <summary>
2090
        /// SpecBreak Modeling 메서드
2091
        /// </summary>
2092
        /// <param name="specBreak"></param>
2093
        private void SpecBreakModeling(SpecBreak specBreak)
2094
        {
2095
            object upStreamObj = SPPIDUtil.FindObjectByUID(document, specBreak.UpStreamUID);
2096
            object downStreamObj = SPPIDUtil.FindObjectByUID(document, specBreak.DownStreamUID);
2097

    
2098
            if (upStreamObj != null &&
2099
                downStreamObj != null)
2100
            {
2101
                LMConnector targetLMConnector = FindBreakLineTarget(upStreamObj, downStreamObj);
2102

    
2103
                if (targetLMConnector != null)
2104
                {
2105
                    foreach (var attribute in specBreak.ATTRIBUTES)
2106
                    {
2107
                        AttributeMapping mapping = document.AttributeMappings.Find(x => x.UID == attribute.UID);
2108
                        if (mapping != null && !string.IsNullOrEmpty(mapping.SPPIDSYMBOLNAME) && mapping.SPPIDSYMBOLNAME != "None")
2109
                        {
2110
                            string MappingPath = mapping.SPPIDSYMBOLNAME;
2111
                            Array array = new double[] { 0, specBreak.SPPID.ORIGINAL_X, specBreak.SPPID.ORIGINAL_Y };
2112
                            LMLabelPersist _LmLabelPersist = _placement.PIDPlaceLabel(MappingPath, ref array, Rotation: specBreak.ANGLE, LabeledItem: targetLMConnector.AsLMRepresentation(), IsLeaderVisible: mapping.LeaderLine);
2113

    
2114
                            if (_LmLabelPersist != null)
2115
                            {
2116
                                specBreak.SPPID.RepresentationId = _LmLabelPersist.AsLMRepresentation().Id;
2117
                                specBreak.SPPID.ModelItemID = _LmLabelPersist.ModelItemID;
2118
                                specBreak.SPPID.GraphicOID = _LmLabelPersist.get_GraphicOID();
2119
                                ReleaseCOMObjects(_LmLabelPersist);
2120
                            }
2121
                        }
2122
                    }
2123
                    ReleaseCOMObjects(targetLMConnector);
2124
                }
2125
            }
2126
        }
2127

    
2128
        private LMConnector FindBreakLineTarget(object targetObj, object connectedObj)
2129
        {
2130
            LMConnector targetConnector = null;
2131
            Symbol targetSymbol = targetObj as Symbol;
2132
            Symbol connectedSymbol = connectedObj as Symbol;
2133
            Line targetLine = targetObj as Line;
2134
            Line connectedLine = connectedObj as Line;
2135
            if (targetSymbol != null && connectedSymbol != null)
2136
            {
2137
                LMSymbol targetLMSymbol = dataSource.GetSymbol(targetSymbol.SPPID.RepresentationId);
2138
                LMSymbol connectedLMSymbol = dataSource.GetSymbol(connectedSymbol.SPPID.RepresentationId);
2139

    
2140
                foreach (LMConnector connector in targetLMSymbol.Avoid1Connectors)
2141
                {
2142
                    if (connector.get_ItemStatus() != "Active")
2143
                        continue;
2144

    
2145
                    if (connector.ConnectItem1SymbolObject.Id == connectedLMSymbol.Id)
2146
                    {
2147
                        targetConnector = connector;
2148
                        break;
2149
                    }
2150
                    else if (connector.ConnectItem2SymbolObject.Id == connectedLMSymbol.Id)
2151
                    {
2152
                        targetConnector = connector;
2153
                        break;
2154
                    }
2155
                }
2156

    
2157
                foreach (LMConnector connector in targetLMSymbol.Avoid2Connectors)
2158
                {
2159
                    if (connector.get_ItemStatus() != "Active")
2160
                        continue;
2161

    
2162
                    if (connector.ConnectItem1SymbolObject.Id == connectedLMSymbol.Id)
2163
                    {
2164
                        targetConnector = connector;
2165
                        break;
2166
                    }
2167
                    else if (connector.ConnectItem2SymbolObject.Id == connectedLMSymbol.Id)
2168
                    {
2169
                        targetConnector = connector;
2170
                        break;
2171
                    }
2172
                }
2173

    
2174
                ReleaseCOMObjects(targetLMSymbol);
2175
                ReleaseCOMObjects(connectedLMSymbol);
2176
            }
2177
            else if (targetLine != null && connectedLine != null)
2178
            {
2179
                LMModelItem targetModelItem = dataSource.GetModelItem(targetLine.SPPID.ModelItemId);
2180
                LMModelItem connectedModelItem = dataSource.GetModelItem(connectedLine.SPPID.ModelItemId);
2181

    
2182
                if (targetModelItem != null && connectedModelItem != null)
2183
                {
2184
                    foreach (LMRepresentation rep in targetModelItem.Representations)
2185
                    {
2186
                        if (targetConnector != null)
2187
                            break;
2188

    
2189
                        if (rep.Attributes["RepresentationType"].get_Value() == "Connector" && rep.Attributes["ItemStatus"].get_Value() == "Active")
2190
                        {
2191
                            LMConnector _LMConnector = dataSource.GetConnector(rep.Id);
2192

    
2193
                            if (IsConnected(_LMConnector, connectedModelItem))
2194
                                targetConnector = _LMConnector;
2195
                            else
2196
                                ReleaseCOMObjects(_LMConnector);
2197
                        }
2198
                    }
2199

    
2200
                    ReleaseCOMObjects(targetModelItem);
2201
                }
2202
            }
2203
            else
2204
            {
2205
                LMSymbol connectedLMSymbol = null;
2206
                if (connectedSymbol != null)
2207
                    connectedLMSymbol = dataSource.GetSymbol(connectedSymbol.SPPID.RepresentationId);
2208
                else if (targetSymbol != null)
2209
                    connectedLMSymbol = dataSource.GetSymbol(targetSymbol.SPPID.RepresentationId);
2210
                else
2211
                {
2212

    
2213
                }
2214
                LMModelItem targetModelItem = null;
2215
                if (targetLine != null)
2216
                    targetModelItem = dataSource.GetModelItem(targetLine.SPPID.ModelItemId);
2217
                else if (connectedLine != null)
2218
                    targetModelItem = dataSource.GetModelItem(connectedLine.SPPID.ModelItemId);
2219
                else
2220
                {
2221

    
2222
                }
2223
                if (connectedLMSymbol != null && targetModelItem != null)
2224
                {
2225
                    foreach (LMConnector connector in connectedLMSymbol.Avoid1Connectors)
2226
                    {
2227
                        if (connector.get_ItemStatus() != "Active")
2228
                            continue;
2229

    
2230
                        if (IsConnected(connector, targetModelItem))
2231
                        {
2232
                            targetConnector = connector;
2233
                            break;
2234
                        }
2235
                    }
2236

    
2237
                    if (targetConnector == null)
2238
                    {
2239
                        foreach (LMConnector connector in connectedLMSymbol.Avoid2Connectors)
2240
                        {
2241
                            if (connector.get_ItemStatus() != "Active")
2242
                                continue;
2243

    
2244
                            if (IsConnected(connector, targetModelItem))
2245
                            {
2246
                                targetConnector = connector;
2247
                                break;
2248
                            }
2249
                        }
2250
                    }
2251
                }
2252

    
2253
            }
2254

    
2255
            return targetConnector;
2256
        }
2257

    
2258
        private bool IsConnected(LMConnector connector, LMModelItem modelItem)
2259
        {
2260
            bool result = false;
2261

    
2262
            foreach (LMRepresentation rep in modelItem.Representations)
2263
            {
2264
                if (result)
2265
                    break;
2266

    
2267
                if (rep.Attributes["RepresentationType"].get_Value() == "Connector" && rep.Attributes["ItemStatus"].get_Value() == "Active")
2268
                {
2269
                    LMConnector _LMConnector = dataSource.GetConnector(rep.Id);
2270

    
2271
                    if (_LMConnector.ConnectItem1SymbolObject != null &&
2272
                        connector.ConnectItem1SymbolObject != null &&
2273
                        _LMConnector.ConnectItem1SymbolObject.Id == connector.ConnectItem1SymbolObject.Id)
2274
                    {
2275
                        result = true;
2276
                        ReleaseCOMObjects(_LMConnector);
2277
                        break;
2278
                    }
2279
                    else if (_LMConnector.ConnectItem1SymbolObject != null &&
2280
                        connector.ConnectItem2SymbolObject != null &&
2281
                        _LMConnector.ConnectItem1SymbolObject.Id == connector.ConnectItem2SymbolObject.Id)
2282
                    {
2283
                        result = true;
2284
                        ReleaseCOMObjects(_LMConnector);
2285
                        break;
2286
                    }
2287
                    else if (_LMConnector.ConnectItem2SymbolObject != null &&
2288
                        connector.ConnectItem1SymbolObject != null &&
2289
                        _LMConnector.ConnectItem2SymbolObject.Id == connector.ConnectItem1SymbolObject.Id)
2290
                    {
2291
                        result = true;
2292
                        ReleaseCOMObjects(_LMConnector);
2293
                        break;
2294
                    }
2295
                    else if (_LMConnector.ConnectItem2SymbolObject != null &&
2296
                        connector.ConnectItem2SymbolObject != null &&
2297
                        _LMConnector.ConnectItem2SymbolObject.Id == connector.ConnectItem2SymbolObject.Id)
2298
                    {
2299
                        result = true;
2300
                        ReleaseCOMObjects(_LMConnector);
2301
                        break;
2302
                    }
2303

    
2304
                    ReleaseCOMObjects(_LMConnector);
2305
                }
2306
            }
2307

    
2308

    
2309
            return result;
2310
        }
2311

    
2312
        /// <summary>
2313
        /// FromModelItem을 ToModelItem으로 PipeRunJoin하는 메서드
2314
        /// </summary>
2315
        /// <param name="fromModelItemId"></param>
2316
        /// <param name="toModelItemId"></param>
2317
        private void JoinPipeRun(string fromModelItemId, string toModelItemId)
2318
        {
2319
            LMModelItem modelItem1 = dataSource.GetModelItem(toModelItemId);
2320
            _LMAItem item1 = modelItem1.AsLMAItem();
2321
            LMModelItem modelItem2 = dataSource.GetModelItem(fromModelItemId);
2322
            _LMAItem item2 = modelItem2.AsLMAItem();
2323

    
2324
            // item2가 item1으로 조인
2325
            _placement.PIDJoinRuns(ref item1, ref item2);
2326
            item1.Commit();
2327
            item2.Commit();
2328

    
2329
            List<Line> lines = SPPIDUtil.FindLinesByModelId(document, fromModelItemId);
2330
            foreach (var line in lines)
2331
                line.SPPID.ModelItemId = toModelItemId;
2332

    
2333
            ReleaseCOMObjects(modelItem1);
2334
            ReleaseCOMObjects(item1);
2335
            ReleaseCOMObjects(modelItem2);
2336
            ReleaseCOMObjects(item2);
2337
        }
2338

    
2339
        /// <summary>
2340
        /// PipeRun을 자동으로 Join하는 메서드
2341
        /// </summary>
2342
        /// <param name="modelItemId"></param>
2343
        private void AutoJoinPipeRun(string modelItemId)
2344
        {
2345
            LMModelItem modelItem = dataSource.GetModelItem(modelItemId);
2346
            _LMAItem item = modelItem.AsLMAItem();
2347
            if (modelItem.get_ItemStatus() == "Active")
2348
            {
2349
                string modelitemID = item.Id;
2350
                _placement.PIDAutoJoin(item, AutoJoinEndConstants.autoJoin_Both, ref item);
2351
                string afterModelItemID = item.Id;
2352

    
2353
                if (modelitemID != afterModelItemID)
2354
                {
2355
                    List<Line> lines = SPPIDUtil.FindLinesByModelId(document, modelitemID);
2356
                    foreach (var line in lines)
2357
                        line.SPPID.ModelItemId = afterModelItemID;
2358
                }
2359
                item.Commit();
2360
            }
2361

    
2362
            ReleaseCOMObjects(modelItem);
2363
            ReleaseCOMObjects(item);
2364
        }
2365

    
2366
        /// <summary>
2367
        /// LineRun에 있는 Line들을 Join하는 진입 메서드
2368
        /// </summary>
2369
        /// <param name="run"></param>
2370
        private void JoinRunLine(LineRun run)
2371
        {
2372
            string modelItemId = string.Empty;
2373
            foreach (var item in run.RUNITEMS)
2374
            {
2375
                if (item.GetType() == typeof(Line))
2376
                {
2377
                    Line line = item as Line;
2378
                    AutoJoinPipeRun(line.SPPID.ModelItemId);
2379
                    modelItemId = line.SPPID.ModelItemId;
2380
                }
2381
            }
2382
        }
2383

    
2384
        /// <summary>
2385
        /// PipeRun의 좌표를 가져오는 메서드
2386
        /// </summary>
2387
        /// <param name="modelId"></param>
2388
        /// <returns></returns>
2389
        private Dictionary<LMConnector, List<double[]>> GetPipeRunVertices(string modelId)
2390
        {
2391
            Dictionary<LMConnector, List<double[]>> connectorVertices = new Dictionary<LMConnector, List<double[]>>();
2392
            LMModelItem modelItem = dataSource.GetModelItem(modelId);
2393

    
2394
            if (modelItem != null)
2395
            {
2396
                foreach (LMRepresentation rep in modelItem.Representations)
2397
                {
2398
                    if (rep.Attributes["RepresentationType"].get_Value() == "Connector" && rep.Attributes["ItemStatus"].get_Value() == "Active")
2399
                    {
2400
                        LMConnector _LMConnector = dataSource.GetConnector(rep.Id);
2401
                        connectorVertices.Add(_LMConnector, new List<double[]>());
2402
                        dynamic OID = rep.get_GraphicOID();
2403
                        DependencyObject drawingObject = radApp.ActiveDocument.ActiveSheet.DrawingObjects[OID];
2404
                        Ingr.RAD2D.LineStringGeometry2d lineStringGeometry = drawingObject.GetGeometry() as Ingr.RAD2D.LineStringGeometry2d;
2405
                        int verticesCount = lineStringGeometry.VertexCount;
2406
                        double[] vertices = null;
2407
                        lineStringGeometry.GetVertices(ref verticesCount, ref vertices);
2408
                        for (int i = 0; i < verticesCount; i++)
2409
                        {
2410
                            double x = 0;
2411
                            double y = 0;
2412
                            lineStringGeometry.GetVertex(i + 1, ref x, ref y);
2413
                            connectorVertices[_LMConnector].Add(new double[] { Math.Round(x, 10), Math.Round(y, 10) });
2414
                        }
2415
                    }
2416
                }
2417

    
2418
                ReleaseCOMObjects(modelItem);
2419
            }
2420

    
2421
            return connectorVertices;
2422
        }
2423

    
2424
        /// <summary>
2425
        /// 좌표로 PipeRun의 Connector중에 어느 Connector에 가까운지/붙을지 가져오는 메서드 - 조건에 안맞아서 못찾을시 제일 가까운 점으로 가져오는 방식
2426
        /// </summary>
2427
        /// <param name="connectorVertices"></param>
2428
        /// <param name="connX"></param>
2429
        /// <param name="connY"></param>
2430
        /// <returns></returns>
2431
        private LMConnector FindTargetLMConnectorForLabel(Dictionary<LMConnector, List<double[]>> connectorVertices, double connX, double connY)
2432
        {
2433
            double length = double.MaxValue;
2434
            LMConnector targetConnector = null;
2435
            foreach (var item in connectorVertices)
2436
            {
2437
                List<double[]> points = item.Value;
2438
                for (int i = 0; i < points.Count - 1; i++)
2439
                {
2440
                    double[] point1 = points[i];
2441
                    double[] point2 = points[i + 1];
2442
                    double x1 = Math.Min(point1[0], point2[0]);
2443
                    double y1 = Math.Min(point1[1], point2[1]);
2444
                    double x2 = Math.Max(point1[0], point2[0]);
2445
                    double y2 = Math.Max(point1[1], point2[1]);
2446

    
2447
                    if ((x1 <= connX && x2 >= connX) ||
2448
                        (y1 <= connY && y2 >= connY))
2449
                    {
2450
                        double distance = SPPIDUtil.CalcPointToPointdDistance(point1[0], point1[1], connX, connY);
2451
                        if (length >= distance)
2452
                        {
2453
                            targetConnector = item.Key;
2454
                            length = distance;
2455
                        }
2456

    
2457
                        distance = SPPIDUtil.CalcPointToPointdDistance(point2[0], point2[1], connX, connY);
2458
                        if (length >= distance)
2459
                        {
2460
                            targetConnector = item.Key;
2461
                            length = distance;
2462
                        }
2463
                    }
2464
                }
2465
            }
2466

    
2467
            // 못찾았을때.
2468
            length = double.MaxValue;
2469
            if (targetConnector == null)
2470
            {
2471
                foreach (var item in connectorVertices)
2472
                {
2473
                    List<double[]> points = item.Value;
2474

    
2475
                    foreach (double[] point in points)
2476
                    {
2477
                        double distance = SPPIDUtil.CalcPointToPointdDistance(point[0], point[1], connX, connY);
2478
                        if (length >= distance)
2479
                        {
2480
                            targetConnector = item.Key;
2481
                            length = distance;
2482
                        }
2483
                    }
2484
                }
2485
            }
2486

    
2487
            return targetConnector;
2488
        }
2489

    
2490
        private LMConnector FindTargetLMConnectorForBranch(Line line, Line targetLine, ref double x, ref double y)
2491
        {
2492
            Dictionary<LMConnector, List<double[]>> vertices = GetPipeRunVertices(targetLine.SPPID.ModelItemId);
2493

    
2494
            double length = double.MaxValue;
2495
            LMConnector targetConnector = null;
2496
            double[] resultPoint = null;
2497
            List<double[]> targetVertices = null;
2498

    
2499
            // Vertices 포인트에 제일 가까운곳
2500
            foreach (var item in vertices)
2501
            {
2502
                List<double[]> points = item.Value;
2503
                for (int i = 0; i < points.Count; i++)
2504
                {
2505
                    double[] point = points[i];
2506
                    double tempX = point[0];
2507
                    double tempY = point[1];
2508

    
2509
                    double distance = SPPIDUtil.CalcPointToPointdDistance(tempX, tempY, x, y);
2510
                    if (length >= distance)
2511
                    {
2512
                        targetConnector = item.Key;
2513
                        length = distance;
2514
                        resultPoint = point;
2515
                        targetVertices = item.Value;
2516
                    }
2517
                }
2518
            }
2519

    
2520
            // Vertices Cross에 제일 가까운곳
2521
            foreach (var item in vertices)
2522
            {
2523
                List<double[]> points = item.Value;
2524
                for (int i = 0; i < points.Count - 1; i++)
2525
                {
2526
                    double[] point1 = points[i];
2527
                    double[] point2 = points[i + 1];
2528

    
2529
                    double maxLineX = Math.Max(point1[0], point2[0]);
2530
                    double minLineX = Math.Min(point1[0], point2[0]);
2531
                    double maxLineY = Math.Max(point1[1], point2[1]);
2532
                    double minLineY = Math.Min(point1[1], point2[1]);
2533

    
2534
                    SlopeType slope = SPPIDUtil.CalcSlope(minLineX, minLineY, maxLineX, maxLineY);
2535

    
2536
                    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]);
2537
                    if (crossingPoint != null)
2538
                    {
2539
                        double distance = SPPIDUtil.CalcPointToPointdDistance(crossingPoint[0], crossingPoint[1], x, y);
2540
                        if (length >= distance)
2541
                        {
2542
                            if (slope == SlopeType.Slope &&
2543
                                minLineX <= crossingPoint[0] && maxLineX >= crossingPoint[0] &&
2544
                                minLineY <= crossingPoint[1] && maxLineY >= crossingPoint[1])
2545
                            {
2546
                                targetConnector = item.Key;
2547
                                length = distance;
2548
                                resultPoint = crossingPoint;
2549
                                targetVertices = item.Value;
2550
                            }
2551
                            else if (slope == SlopeType.HORIZONTAL &&
2552
                                minLineX <= crossingPoint[0] && maxLineX >= crossingPoint[0])
2553
                            {
2554
                                targetConnector = item.Key;
2555
                                length = distance;
2556
                                resultPoint = crossingPoint;
2557
                                targetVertices = item.Value;
2558
                            }
2559
                            else if (slope == SlopeType.VERTICAL &&
2560
                               minLineY <= crossingPoint[1] && maxLineY >= crossingPoint[1])
2561
                            {
2562
                                targetConnector = item.Key;
2563
                                length = distance;
2564
                                resultPoint = crossingPoint;
2565
                                targetVertices = item.Value;
2566
                            }
2567
                        }
2568
                    }
2569
                }
2570
            }
2571

    
2572
            foreach (var item in vertices)
2573
                if (item.Key != null && item.Key != targetConnector)
2574
                    ReleaseCOMObjects(item.Key);
2575

    
2576
            if (SPPIDUtil.IsBranchLine(line, targetLine))
2577
            {
2578
                double tempResultX = resultPoint[0];
2579
                double tempResultY = resultPoint[1];
2580
                SPPIDUtil.ConvertGridPoint(ref tempResultX, ref tempResultY);
2581

    
2582
                GridSetting gridSetting = GridSetting.GetInstance();
2583

    
2584
                for (int i = 0; i < targetVertices.Count; i++)
2585
                {
2586
                    double[] point = targetVertices[i];
2587
                    double tempX = targetVertices[i][0];
2588
                    double tempY = targetVertices[i][1];
2589
                    SPPIDUtil.ConvertGridPoint(ref tempX, ref tempY);
2590
                    if (tempX == tempResultX && tempY == tempResultY)
2591
                    {
2592
                        if (i == 0)
2593
                        {
2594
                            LMSymbol connSymbol = targetConnector.ConnectItem1SymbolObject;
2595
                            if (connSymbol == null ||
2596
                                (connSymbol != null && connSymbol.get_ItemStatus() == "Active" && connSymbol.get_RepresentationType() != "Branch"))
2597
                            {
2598
                                bool bCalcX = false;
2599
                                bool bCalcY = false;
2600
                                if (targetLine.SlopeType == SlopeType.HORIZONTAL)
2601
                                    bCalcX = true;
2602
                                else if (targetLine.SlopeType == SlopeType.VERTICAL)
2603
                                    bCalcY = true;
2604
                                else
2605
                                {
2606
                                    bCalcX = true;
2607
                                    bCalcY = true;
2608
                                }
2609

    
2610
                                if (bCalcX)
2611
                                {
2612
                                    double nextX = targetVertices[i + 1][0];
2613
                                    double newX = 0;
2614
                                    if (nextX > tempX)
2615
                                    {
2616
                                        newX = tempX + gridSetting.Length;
2617
                                        if (newX > nextX)
2618
                                            newX = (point[0] + nextX) / 2;
2619
                                    }
2620
                                    else
2621
                                    {
2622
                                        newX = tempX - gridSetting.Length;
2623
                                        if (newX < nextX)
2624
                                            newX = (point[0] + nextX) / 2;
2625
                                    }
2626
                                    resultPoint = new double[] { newX, resultPoint[1] };
2627
                                }
2628

    
2629
                                if (bCalcY)
2630
                                {
2631
                                    double nextY = targetVertices[i + 1][1];
2632
                                    double newY = 0;
2633
                                    if (nextY > tempY)
2634
                                    {
2635
                                        newY = tempY + gridSetting.Length;
2636
                                        if (newY > nextY)
2637
                                            newY = (point[1] + nextY) / 2;
2638
                                    }
2639
                                    else
2640
                                    {
2641
                                        newY = tempY - gridSetting.Length;
2642
                                        if (newY < nextY)
2643
                                            newY = (point[1] + nextY) / 2;
2644
                                    }
2645
                                    resultPoint = new double[] { resultPoint[0], newY };
2646
                                }
2647
                            }
2648
                        }
2649
                        else if (i == targetVertices.Count - 1)
2650
                        {
2651
                            LMSymbol connSymbol = targetConnector.ConnectItem2SymbolObject;
2652
                            if (connSymbol == null ||
2653
                                (connSymbol != null && connSymbol.get_ItemStatus() == "Active" && connSymbol.get_RepresentationType() != "Branch"))
2654
                            {
2655
                                bool bCalcX = false;
2656
                                bool bCalcY = false;
2657
                                if (targetLine.SlopeType == SlopeType.HORIZONTAL)
2658
                                    bCalcX = true;
2659
                                else if (targetLine.SlopeType == SlopeType.VERTICAL)
2660
                                    bCalcY = true;
2661
                                else
2662
                                {
2663
                                    bCalcX = true;
2664
                                    bCalcY = true;
2665
                                }
2666

    
2667
                                if (bCalcX)
2668
                                {
2669
                                    double nextX = targetVertices[i - 1][0];
2670
                                    double newX = 0;
2671
                                    if (nextX > tempX)
2672
                                    {
2673
                                        newX = tempX + gridSetting.Length;
2674
                                        if (newX > nextX)
2675
                                            newX = (point[0] + nextX) / 2;
2676
                                    }
2677
                                    else
2678
                                    {
2679
                                        newX = tempX - gridSetting.Length;
2680
                                        if (newX < nextX)
2681
                                            newX = (point[0] + nextX) / 2;
2682
                                    }
2683
                                    resultPoint = new double[] { newX, resultPoint[1] };
2684
                                }
2685

    
2686
                                if (bCalcY)
2687
                                {
2688
                                    double nextY = targetVertices[i - 1][1];
2689
                                    double newY = 0;
2690
                                    if (nextY > tempY)
2691
                                    {
2692
                                        newY = tempY + gridSetting.Length;
2693
                                        if (newY > nextY)
2694
                                            newY = (point[1] + nextY) / 2;
2695
                                    }
2696
                                    else
2697
                                    {
2698
                                        newY = tempY - gridSetting.Length;
2699
                                        if (newY < nextY)
2700
                                            newY = (point[1] + nextY) / 2;
2701
                                    }
2702
                                    resultPoint = new double[] { resultPoint[0], newY };
2703
                                }
2704
                            }
2705
                        }
2706
                        break;
2707
                    }
2708
                }
2709
            }
2710

    
2711
            x = resultPoint[0];
2712
            y = resultPoint[1];
2713

    
2714
            return targetConnector;
2715
        }
2716

    
2717
        /// <summary>
2718
        /// Line Number Symbol을 실제로 Modeling하는 메서드
2719
        /// </summary>
2720
        /// <param name="lineNumber"></param>
2721
        private void LineNumberModeling(LineNumber lineNumber)
2722
        {
2723
            object obj = SPPIDUtil.FindObjectByUID(document, lineNumber.CONNLINE);
2724
            if (obj != null)
2725
            {
2726
                Line line = obj as Line;
2727
                Dictionary<LMConnector, List<double[]>> connectorVertices = GetPipeRunVertices(line.SPPID.ModelItemId);
2728
                LMConnector connectedLMConnector = FindTargetLMConnectorForLabel(connectorVertices, lineNumber.SPPID.ORIGINAL_X, lineNumber.SPPID.ORIGINAL_Y);
2729
                if (connectedLMConnector != null)
2730
                {
2731
                    double x = 0;
2732
                    double y = 0;
2733
                    CalcLabelLocation(ref x, ref y, lineNumber.SPPID.ORIGINAL_X, lineNumber.SPPID.ORIGINAL_Y, lineNumber.SPPIDLabelLocation, _ETCSetting.LineNumberLocation);
2734

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

    
2738
                    foreach (var item in connectorVertices)
2739
                        ReleaseCOMObjects(item.Key);
2740
                    if (_LmLabelPresist != null)
2741
                    {
2742
                        _LmLabelPresist.Commit();
2743
                        lineNumber.SPPID.RepresentationId = _LmLabelPresist.AsLMRepresentation().Id;
2744
                        ReleaseCOMObjects(_LmLabelPresist);
2745
                    }
2746
                    else
2747
                    {
2748

    
2749
                    }
2750
                }
2751
            }
2752
        }
2753

    
2754
        /// <summary>
2755
        /// Flow Mark Modeling
2756
        /// </summary>
2757
        /// <param name="line"></param>
2758
        private void FlowMarkModeling(Line line)
2759
        {
2760
            if (line.FLOWMARK && !string.IsNullOrEmpty(line.SPPID.ModelItemId) && !string.IsNullOrEmpty(_ETCSetting.FlowMarkSymbolPath))
2761
            {
2762
                SlopeType targetSlopeType = SPPIDUtil.CalcSlope(line.SPPID.START_X, line.SPPID.START_Y, line.SPPID.END_X, line.SPPID.END_Y);
2763
                string mappingPath = _ETCSetting.FlowMarkSymbolPath;
2764
                double percent = line.FLOWMARK_PERCENT;
2765
                double tempX = 0;
2766
                double tempY = 0;
2767

    
2768
                double gapX;
2769
                double gapY;
2770
                // ID2 기준의 Gap을 구함
2771
                if (percent == 0)
2772
                {
2773
                    gapX = 0;
2774
                    gapY = 0;
2775
                }
2776
                else
2777
                {
2778
                    gapX = Math.Abs(line.SPPID.START_X - line.SPPID.END_X) / 100 * percent;
2779
                    gapY = Math.Abs(line.SPPID.START_Y - line.SPPID.END_Y) / 100 * percent;
2780
                }
2781

    
2782
                if (line.SPPID.START_X < line.SPPID.END_X)
2783
                    tempX = line.SPPID.START_X + gapX;
2784
                else
2785
                    tempX = line.SPPID.START_X - gapX;
2786

    
2787
                if (line.SPPID.START_Y < line.SPPID.END_Y)
2788
                    tempY = line.SPPID.START_Y + gapY;
2789
                else
2790
                    tempY = line.SPPID.START_Y - gapY;
2791

    
2792

    
2793

    
2794
                Dictionary<LMConnector, List<double[]>> connectorVertices = GetPipeRunVertices(line.SPPID.ModelItemId);
2795
                LMConnector _TargetItem = null;
2796
                double distance = double.MaxValue;
2797
                double[] startPoint = null;
2798
                double[] endPoint = null;
2799
                // ID2의 기준 Gap으로 제일 가까운 Line 찾음(Slope도 같은조건)
2800
                foreach (var item in connectorVertices)
2801
                {
2802
                    for (int i = 0; i < item.Value.Count - 1; i++)
2803
                    {
2804
                        List<double[]> points = item.Value;
2805
                        double[] point1 = points[i];
2806
                        double[] point2 = points[i + 1];
2807

    
2808
                        SlopeType slopeType = SPPIDUtil.CalcSlope(point1[0], point1[1], point2[0], point2[1]);
2809
                        if (slopeType == targetSlopeType)
2810
                        {
2811
                            double result = SPPIDUtil.CalcPointToPointdDistance(point1[0], point1[1], tempX, tempY);
2812
                            if (result < distance)
2813
                            {
2814
                                distance = result;
2815
                                _TargetItem = item.Key;
2816

    
2817
                                startPoint = point1;
2818
                                endPoint = point2;
2819
                            }
2820

    
2821
                            result = SPPIDUtil.CalcPointToPointdDistance(point2[0], point2[1], tempX, tempY);
2822
                            if (result < distance)
2823
                            {
2824
                                distance = result;
2825
                                _TargetItem = item.Key;
2826

    
2827
                                startPoint = point1;
2828
                                endPoint = point2;
2829
                            }
2830
                        }
2831
                    }
2832
                }
2833

    
2834
                if (_TargetItem != null)
2835
                {
2836
                    double x = 0;
2837
                    double y = 0;
2838
                    double angle = 0;
2839
                    // SPPID 기준의 Gap으로 실 좌표를 구함
2840
                    if (percent == 0)
2841
                    {
2842
                        gapX = 0;
2843
                        gapY = 0;
2844
                    }
2845
                    else
2846
                    {
2847
                        gapX = Math.Abs(startPoint[0] - endPoint[0]) / 100 * percent;
2848
                        gapY = Math.Abs(startPoint[1] - endPoint[1]) / 100 * percent;
2849
                    }
2850

    
2851
                    if (startPoint[0] < endPoint[0])
2852
                        x = startPoint[0] + gapX;
2853
                    else
2854
                        x = startPoint[0] - gapX;
2855

    
2856
                    if (startPoint[1] < endPoint[1])
2857
                        y = startPoint[1] + gapY;
2858
                    else
2859
                        y = startPoint[1] - gapY;
2860

    
2861
                    if (targetSlopeType == SlopeType.HORIZONTAL)
2862
                    {
2863
                        if (startPoint[0] < endPoint[0])
2864
                            angle = 0;
2865
                        else
2866
                            angle = Math.PI;
2867
                    }
2868
                    // 90 270
2869
                    else if (targetSlopeType == SlopeType.VERTICAL)
2870
                    {
2871
                        if (startPoint[1] < endPoint[1])
2872
                            angle = 90 * Math.PI / 180;
2873
                        else
2874
                            angle = 270 * Math.PI / 180;
2875
                    }
2876

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

    
2879
                    if (_LMSymbol != null)
2880
                    {
2881
                        ReleaseCOMObjects(_LMSymbol);
2882
                    }
2883

    
2884
                }
2885

    
2886
                foreach (var item in connectorVertices)
2887
                    ReleaseCOMObjects(item.Key);
2888
            }
2889
        }
2890

    
2891
        /// <summary>
2892
        /// Line Number 기준으로 모든 Item에 Line Number의 Attribute Input
2893
        /// </summary>
2894
        /// <param name="lineNumber"></param>
2895
        private void InputLineNumberAttribute(LineNumber lineNumber)
2896
        {
2897
            foreach (LineRun run in lineNumber.RUNS)
2898
            {
2899
                foreach (var item in run.RUNITEMS)
2900
                {
2901
                    if (item.GetType() == typeof(Symbol))
2902
                    {
2903
                        Symbol symbol = item as Symbol;
2904
                        LMSymbol _LMSymbol = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
2905
                        if (_LMSymbol != null)
2906
                        {
2907
                            LMModelItem _LMModelItem = _LMSymbol.ModelItemObject;
2908

    
2909
                            if (_LMModelItem != null && _LMModelItem.get_ItemStatus() == "Active")
2910
                            {
2911
                                foreach (var attribute in lineNumber.ATTRIBUTES)
2912
                                {
2913
                                    LineNumberMapping mapping = document.LineNumberMappings.Find(x => x.UID == attribute.UID);
2914
                                    if (mapping != null && !string.IsNullOrEmpty(attribute.VALUE) && attribute.VALUE != "None")
2915
                                    {
2916
                                        LMAAttribute _LMAAttribute = _LMModelItem.Attributes[mapping.SPPIDATTRIBUTENAME];
2917
                                        if (_LMAAttribute != null)
2918
                                        {
2919
                                            if (DBNull.Value.Equals(_LMAAttribute.get_Value()))
2920
                                                _LMAAttribute.set_Value(attribute.VALUE);
2921
                                            else if (_LMAAttribute.get_Value() != attribute.VALUE)
2922
                                                _LMAAttribute.set_Value(attribute.VALUE);
2923
                                        }
2924
                                    }
2925
                                }
2926
                                _LMModelItem.Commit();
2927
                            }
2928
                            if (_LMModelItem != null)
2929
                                ReleaseCOMObjects(_LMModelItem);
2930
                        }
2931
                        if (_LMSymbol != null)
2932
                            ReleaseCOMObjects(_LMSymbol);
2933
                    }
2934
                    else if (item.GetType() == typeof(Line))
2935
                    {
2936
                        Line line = item as Line;
2937
                        if (line != null)
2938
                        {
2939
                            LMModelItem _LMModelItem = dataSource.GetModelItem(line.SPPID.ModelItemId);
2940
                            if (_LMModelItem != null && _LMModelItem.get_ItemStatus() == "Active")
2941
                            {
2942
                                foreach (var attribute in lineNumber.ATTRIBUTES)
2943
                                {
2944
                                    LineNumberMapping mapping = document.LineNumberMappings.Find(x => x.UID == attribute.UID);
2945
                                    if (mapping != null && !string.IsNullOrEmpty(attribute.VALUE) && attribute.VALUE != "None")
2946
                                    {
2947
                                        LMAAttribute _LMAAttribute = _LMModelItem.Attributes[mapping.SPPIDATTRIBUTENAME];
2948
                                        if (_LMAAttribute != null)
2949
                                        {
2950
                                            if (DBNull.Value.Equals(_LMAAttribute.get_Value()))
2951
                                                _LMAAttribute.set_Value(attribute.VALUE);
2952
                                            else if (_LMAAttribute.get_Value() != attribute.VALUE)
2953
                                                _LMAAttribute.set_Value(attribute.VALUE);
2954

    
2955
                                        }
2956
                                    }
2957
                                }
2958
                                _LMModelItem.Commit();
2959
                            }
2960
                            if (_LMModelItem != null)
2961
                                ReleaseCOMObjects(_LMModelItem);
2962
                        }
2963
                    }
2964
                }
2965
            }
2966
        }
2967

    
2968
        /// <summary>
2969
        /// Symbol Attribute 입력 메서드
2970
        /// </summary>
2971
        /// <param name="item"></param>
2972
        private void InputSymbolAttribute(object targetItem, List<BaseModel.Attribute> targetAttributes)
2973
        {
2974

    
2975
            // Object 아이템이 Symbol일 경우 Equipment일 경우 
2976
            string sRep = null;
2977
            if (targetItem.GetType() == typeof(Symbol))
2978
                sRep = ((Symbol)targetItem).SPPID.RepresentationId;
2979
            else if (targetItem.GetType() == typeof(Equipment))
2980
                sRep = ((Equipment)targetItem).SPPID.RepresentationId;
2981

    
2982
            if (!string.IsNullOrEmpty(sRep))
2983
            {
2984
                LMSymbol _LMSymbol = dataSource.GetSymbol(sRep);
2985
                LMModelItem _LMModelItem = _LMSymbol.ModelItemObject;
2986
                LMAAttributes _Attributes = _LMModelItem.Attributes;
2987

    
2988
                foreach (var item in targetAttributes)
2989
                {
2990
                    AttributeMapping mapping = document.AttributeMappings.Find(x => x.UID == item.UID);
2991
                    if (mapping != null && !string.IsNullOrEmpty(item.VALUE) && item.VALUE != "None")
2992
                    {
2993
                        LMAAttribute _Attribute = _Attributes[mapping.SPPIDATTRIBUTENAME];
2994
                        if (_Attribute != null)
2995
                            _Attribute.set_Value(item.VALUE);
2996
                    }
2997
                }
2998
                _LMModelItem.Commit();
2999

    
3000
                ReleaseCOMObjects(_Attributes);
3001
                ReleaseCOMObjects(_LMModelItem);
3002
                ReleaseCOMObjects(_LMSymbol);
3003
            }
3004
        }
3005

    
3006
        /// <summary>
3007
        /// Input SpecBreak Attribute
3008
        /// </summary>
3009
        /// <param name="specBreak"></param>
3010
        private void InputSpecBreakAttribute(SpecBreak specBreak)
3011
        {
3012
            object upStreamObj = SPPIDUtil.FindObjectByUID(document, specBreak.UpStreamUID);
3013
            object downStreamObj = SPPIDUtil.FindObjectByUID(document, specBreak.DownStreamUID);
3014

    
3015
            if (upStreamObj != null &&
3016
                downStreamObj != null)
3017
            {
3018
                LMConnector targetLMConnector = FindBreakLineTarget(upStreamObj, downStreamObj);
3019

    
3020
                if (targetLMConnector != null)
3021
                {
3022
                    foreach (LMLabelPersist _LMLabelPersist in targetLMConnector.LabelPersists)
3023
                    {
3024
                        string symbolPath = _LMLabelPersist.get_FileName();
3025
                        AttributeMapping mapping = document.AttributeMappings.Find(x => x.SPPIDSYMBOLNAME == symbolPath);
3026
                        if (mapping != null)
3027
                        {
3028
                            BaseModel.Attribute attribute = specBreak.ATTRIBUTES.Find(y => y.UID == mapping.UID);
3029
                            if (attribute != null && !string.IsNullOrEmpty(attribute.VALUE) && attribute.VALUE != "None")
3030
                            {
3031
                                string[] values = attribute.VALUE.Split(new char[] { ',' });
3032
                                if (values.Length == 2)
3033
                                {
3034
                                    string upStreamValue = values[0];
3035
                                    string downStreamValue = values[1];
3036

    
3037
                                    InputAttributeForSpecBreak(upStreamObj, downStreamObj, upStreamValue, downStreamValue, mapping.SPPIDATTRIBUTENAME);
3038
                                }
3039
                            }
3040
                        }
3041
                    }
3042

    
3043
                    ReleaseCOMObjects(targetLMConnector);
3044
                }
3045
            }
3046

    
3047

    
3048
            #region 내부에서만 쓰는 메서드
3049
            void InputAttributeForSpecBreak(object _upStreamObj, object _downStreamObj, string upStreamValue, string downStreamValue, string sppidAttributeName)
3050
            {
3051
                Symbol upStreamSymbol = _upStreamObj as Symbol;
3052
                Line upStreamLine = _upStreamObj as Line;
3053
                Symbol downStreamSymbol = _downStreamObj as Symbol;
3054
                Line downStreamLine = _downStreamObj as Line;
3055
                // 둘다 Line일 경우
3056
                if (upStreamLine != null && downStreamLine != null)
3057
                {
3058
                    InputLineAttributeForSpecBreakLine(upStreamLine, sppidAttributeName, upStreamValue);
3059
                    InputLineAttributeForSpecBreakLine(downStreamLine, sppidAttributeName, downStreamValue);
3060
                }
3061
                // 둘다 Symbol일 경우
3062
                else if (upStreamSymbol != null && downStreamSymbol != null)
3063
                {
3064
                    LMConnector zeroLenthConnector = FindBreakLineTarget(upStreamSymbol, downStreamSymbol);
3065
                    LMSymbol upStreamLMSymbol = dataSource.GetSymbol(upStreamSymbol.SPPID.RepresentationId);
3066
                    LMSymbol downStreamLMSymbol = dataSource.GetSymbol(downStreamSymbol.SPPID.RepresentationId);
3067

    
3068
                    foreach (LMConnector connector in upStreamLMSymbol.Avoid1Connectors)
3069
                    {
3070
                        if (connector.get_ItemStatus() != "Active")
3071
                            continue;
3072

    
3073
                        if (connector.Id != zeroLenthConnector.Id)
3074
                            InputLineAttributeForSpecBreakLMConnector(connector, sppidAttributeName, upStreamValue);
3075
                    }
3076

    
3077
                    foreach (LMConnector connector in upStreamLMSymbol.Avoid2Connectors)
3078
                    {
3079
                        if (connector.get_ItemStatus() != "Active")
3080
                            continue;
3081

    
3082
                        if (connector.Id != zeroLenthConnector.Id)
3083
                            InputLineAttributeForSpecBreakLMConnector(connector, sppidAttributeName, upStreamValue);
3084
                    }
3085

    
3086
                    foreach (LMConnector connector in downStreamLMSymbol.Avoid1Connectors)
3087
                    {
3088
                        if (connector.get_ItemStatus() != "Active")
3089
                            continue;
3090

    
3091
                        if (connector.Id != zeroLenthConnector.Id)
3092
                            InputLineAttributeForSpecBreakLMConnector(connector, sppidAttributeName, downStreamValue);
3093
                    }
3094

    
3095
                    foreach (LMConnector connector in downStreamLMSymbol.Avoid2Connectors)
3096
                    {
3097
                        if (connector.get_ItemStatus() != "Active")
3098
                            continue;
3099

    
3100
                        if (connector.Id != zeroLenthConnector.Id)
3101
                            InputLineAttributeForSpecBreakLMConnector(connector, sppidAttributeName, downStreamValue);
3102
                    }
3103

    
3104
                    ReleaseCOMObjects(zeroLenthConnector);
3105
                    ReleaseCOMObjects(upStreamLMSymbol);
3106
                    ReleaseCOMObjects(downStreamLMSymbol);
3107
                }
3108
                else if (upStreamSymbol != null && downStreamLine != null)
3109
                {
3110
                    LMConnector zeroLenthConnector = FindBreakLineTarget(upStreamSymbol, downStreamLine);
3111
                    InputLineAttributeForSpecBreakLine(downStreamLine, sppidAttributeName, downStreamValue);
3112
                    LMSymbol upStreamLMSymbol = dataSource.GetSymbol(upStreamSymbol.SPPID.RepresentationId);
3113

    
3114
                    foreach (LMConnector connector in upStreamLMSymbol.Avoid1Connectors)
3115
                    {
3116
                        if (connector.get_ItemStatus() != "Active")
3117
                            continue;
3118

    
3119
                        if (connector.Id != zeroLenthConnector.Id)
3120
                            InputLineAttributeForSpecBreakLMConnector(connector, sppidAttributeName, upStreamValue);
3121
                    }
3122

    
3123
                    foreach (LMConnector connector in upStreamLMSymbol.Avoid2Connectors)
3124
                    {
3125
                        if (connector.get_ItemStatus() != "Active")
3126
                            continue;
3127

    
3128
                        if (connector.Id != zeroLenthConnector.Id)
3129
                            InputLineAttributeForSpecBreakLMConnector(connector, sppidAttributeName, upStreamValue);
3130
                    }
3131

    
3132
                    ReleaseCOMObjects(zeroLenthConnector);
3133
                    ReleaseCOMObjects(upStreamLMSymbol);
3134
                }
3135
                else if (upStreamLine != null && downStreamSymbol != null)
3136
                {
3137
                    LMConnector zeroLenthConnector = FindBreakLineTarget(upStreamLine, downStreamSymbol);
3138
                    InputLineAttributeForSpecBreakLine(upStreamLine, sppidAttributeName, upStreamValue);
3139
                    LMSymbol downStreamLMSymbol = dataSource.GetSymbol(downStreamSymbol.SPPID.RepresentationId);
3140

    
3141
                    foreach (LMConnector connector in downStreamLMSymbol.Avoid1Connectors)
3142
                    {
3143
                        if (connector.get_ItemStatus() != "Active")
3144
                            continue;
3145

    
3146
                        if (connector.Id != zeroLenthConnector.Id)
3147
                            InputLineAttributeForSpecBreakLMConnector(connector, sppidAttributeName, downStreamValue);
3148
                    }
3149

    
3150
                    foreach (LMConnector connector in downStreamLMSymbol.Avoid2Connectors)
3151
                    {
3152
                        if (connector.get_ItemStatus() != "Active")
3153
                            continue;
3154

    
3155
                        if (connector.Id != zeroLenthConnector.Id)
3156
                            InputLineAttributeForSpecBreakLMConnector(connector, sppidAttributeName, downStreamValue);
3157
                    }
3158

    
3159
                    ReleaseCOMObjects(zeroLenthConnector);
3160
                    ReleaseCOMObjects(downStreamLMSymbol);
3161
                }
3162
            }
3163

    
3164
            void InputLineAttributeForSpecBreakLine(Line line, string attrName, string value)
3165
            {
3166
                LMModelItem _LMModelItem = dataSource.GetModelItem(line.SPPID.ModelItemId);
3167
                if (_LMModelItem != null && _LMModelItem.get_ItemStatus() == "Active")
3168
                {
3169
                    LMAAttribute _LMAAttribute = _LMModelItem.Attributes[attrName];
3170
                    if (_LMAAttribute != null)
3171
                    {
3172
                        if (DBNull.Value.Equals(_LMAAttribute.get_Value()))
3173
                            _LMAAttribute.set_Value(value);
3174
                        else if (_LMAAttribute.get_Value() != value)
3175
                            _LMAAttribute.set_Value(value);
3176
                    }
3177

    
3178
                    _LMModelItem.Commit();
3179
                }
3180
                if (_LMModelItem != null)
3181
                    ReleaseCOMObjects(_LMModelItem);
3182
            }
3183

    
3184
            void InputLineAttributeForSpecBreakLMConnector(LMConnector connector, string attrName, string value)
3185
            {
3186
                LMModelItem _LMModelItem = dataSource.GetModelItem(connector.ModelItemID);
3187
                if (_LMModelItem != null && _LMModelItem.get_ItemStatus() == "Active")
3188
                {
3189
                    LMAAttribute _LMAAttribute = _LMModelItem.Attributes[attrName];
3190
                    if (_LMAAttribute != null)
3191
                    {
3192
                        if (DBNull.Value.Equals(_LMAAttribute.get_Value()))
3193
                            _LMAAttribute.set_Value(value);
3194
                        else if (_LMAAttribute.get_Value() != value)
3195
                            _LMAAttribute.set_Value(value);
3196
                    }
3197

    
3198
                    _LMModelItem.Commit();
3199
                }
3200
                if (_LMModelItem != null)
3201
                    ReleaseCOMObjects(_LMModelItem);
3202
            }
3203
            #endregion
3204
        }
3205

    
3206
        /// <summary>
3207
        /// Text Modeling - Association일 경우는 Text대신 해당 맵핑된 Symbol로 모델링
3208
        /// </summary>
3209
        /// <param name="text"></param>
3210
        private void TextModeling(Text text)
3211
        {
3212
            LMSymbol _LMSymbol = null;
3213
            LMConnector connectedLMConnector = null;
3214
            //if (text.ASSOCIATION && !string.IsNullOrEmpty(text.OWNER) && text.OWNER != "None")
3215
            if (text.ASSOCIATION)
3216
            {
3217
                object owner = SPPIDUtil.FindObjectByUID(document, text.OWNER);
3218
                if (owner.GetType() == typeof(Symbol))
3219
                {
3220
                    Symbol symbol = owner as Symbol;
3221
                    _LMSymbol = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
3222
                    if (_LMSymbol != null)
3223
                    {
3224
                        BaseModel.Attribute attribute = symbol.ATTRIBUTES.Find(x => x.ASSOCITEM == text.UID);
3225
                        if (attribute != null && !string.IsNullOrEmpty(attribute.VALUE) && attribute.VALUE != "None")
3226
                        {
3227
                            AttributeMapping mapping = document.AttributeMappings.Find(x => x.UID == attribute.UID && !string.IsNullOrEmpty(x.SPPIDSYMBOLNAME));
3228

    
3229
                            if (mapping != null)
3230
                            {
3231
                                double x = 0;
3232
                                double y = 0;
3233

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

    
3237
                                LMLabelPersist _LMLabelPersist = _placement.PIDPlaceLabel(mapping.SPPIDSYMBOLNAME, ref array, Rotation: text.ANGLE, LabeledItem: _LMSymbol.AsLMRepresentation(), IsLeaderVisible: mapping.LeaderLine);
3238
                                if (_LMLabelPersist != null)
3239
                                {
3240
                                    _LMLabelPersist.Commit();
3241
                                    ReleaseCOMObjects(_LMLabelPersist);
3242
                                }
3243
                            }
3244
                        }
3245
                    }
3246
                }
3247
                else if (owner.GetType() == typeof(Line))
3248
                {
3249
                    Line line = owner as Line;
3250
                    Dictionary<LMConnector, List<double[]>> connectorVertices = GetPipeRunVertices(line.SPPID.ModelItemId);
3251
                    connectedLMConnector = FindTargetLMConnectorForLabel(connectorVertices, text.SPPID.ORIGINAL_X, text.SPPID.ORIGINAL_Y);
3252

    
3253
                    if (connectedLMConnector != null)
3254
                    {
3255
                        BaseModel.Attribute attribute = line.ATTRIBUTES.Find(x => x.ASSOCITEM == text.UID);
3256
                        if (attribute != null && !string.IsNullOrEmpty(attribute.VALUE) && attribute.VALUE != "None")
3257
                        {
3258
                            AttributeMapping mapping = document.AttributeMappings.Find(x => x.UID == attribute.UID && !string.IsNullOrEmpty(x.SPPIDSYMBOLNAME));
3259

    
3260
                            if (mapping != null)
3261
                            {
3262
                                double x = 0;
3263
                                double y = 0;
3264

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

    
3268
                                LMLabelPersist _LMLabelPersist = _placement.PIDPlaceLabel(mapping.SPPIDSYMBOLNAME, ref array, Rotation: text.ANGLE, LabeledItem: connectedLMConnector.AsLMRepresentation(), IsLeaderVisible: mapping.LeaderLine);
3269
                                if (_LMLabelPersist != null)
3270
                                {
3271
                                    _LMLabelPersist.Commit();
3272
                                    ReleaseCOMObjects(_LMLabelPersist);
3273
                                }
3274
                            }
3275
                        }
3276
                    }
3277
                }
3278
            }
3279
            else
3280
            {
3281
                LMItemNote _LMItemNote = null;
3282
                LMAAttribute _LMAAttribute = null;
3283

    
3284
                double x = 0;
3285
                double y = 0;
3286
                double angle = text.ANGLE;
3287
                CalcLabelLocation(ref x, ref y, text.SPPID.ORIGINAL_X, text.SPPID.ORIGINAL_Y, text.SPPIDLabelLocation, _ETCSetting.TextLocation);
3288

    
3289
                _LMSymbol = _placement.PIDPlaceSymbol(text.SPPID.MAPPINGNAME, x, y, Rotation: angle);
3290
                _LMSymbol.Commit();
3291
                _LMItemNote = _placement.PIDDataSource.GetItemNote(_LMSymbol.ModelItemID);
3292
                _LMItemNote.Commit();
3293
                _LMAAttribute = _LMItemNote.Attributes["Note.Body"];
3294
                _LMAAttribute.set_Value(text.VALUE);
3295
                _LMItemNote.Commit();
3296

    
3297
                if (_LMAAttribute != null)
3298
                    ReleaseCOMObjects(_LMAAttribute);
3299
                if (_LMItemNote != null)
3300
                    ReleaseCOMObjects(_LMItemNote);
3301
            }
3302
            if (_LMSymbol != null)
3303
                ReleaseCOMObjects(_LMSymbol);
3304
        }
3305

    
3306
        /// <summary>
3307
        /// Note Modeling
3308
        /// </summary>
3309
        /// <param name="note"></param>
3310
        private void NoteModeling(Note note)
3311
        {
3312
            LMSymbol _LMSymbol = null;
3313
            LMItemNote _LMItemNote = null;
3314
            LMAAttribute _LMAAttribute = null;
3315

    
3316
            if (string.IsNullOrEmpty(note.OWNER) || note.OWNER == "None")
3317
            {
3318
                double x = 0;
3319
                double y = 0;
3320

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

    
3323
                _LMSymbol = _placement.PIDPlaceSymbol(note.SPPID.MAPPINGNAME, x, y);
3324
                _LMSymbol.Commit();
3325
                _LMItemNote = _placement.PIDDataSource.GetItemNote(_LMSymbol.ModelItemID);
3326
                _LMItemNote.Commit();
3327
                _LMAAttribute = _LMItemNote.Attributes["Note.Body"];
3328
                _LMAAttribute.set_Value(note.VALUE);
3329
                _LMItemNote.Commit();
3330
            }
3331

    
3332
            if (_LMAAttribute != null)
3333
                ReleaseCOMObjects(_LMAAttribute);
3334
            if (_LMItemNote != null)
3335
                ReleaseCOMObjects(_LMItemNote);
3336
            if (_LMSymbol != null)
3337
                ReleaseCOMObjects(_LMSymbol);
3338
        }
3339

    
3340
        /// <summary>
3341
        /// Note Symbol Modeling
3342
        /// </summary>
3343
        /// <param name="symbol"></param>
3344
        private void NoteSymbolModeling(Symbol symbol)
3345
        {
3346
            if (symbol.TYPE == "Notes")
3347
            {
3348
                string mappingPath = symbol.SPPID.MAPPINGNAME;
3349
                double x = symbol.SPPID.ORIGINAL_X;
3350
                double y = symbol.SPPID.ORIGINAL_Y;
3351
                int mirror = 0;
3352
                double angle = symbol.ANGLE;
3353

    
3354
                LMSymbol _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle);
3355
                symbol.SPPID.RepresentationId = _LMSymbol.AsLMRepresentation().Id;
3356
                symbol.SPPID.ModelItemID = _LMSymbol.ModelItemID;
3357

    
3358
                ReleaseCOMObjects(_LMSymbol);
3359

    
3360
                InputSymbolAttribute(symbol, symbol.ATTRIBUTES);
3361
            }
3362
        }
3363

    
3364
        /// <summary>
3365
        /// Label의 좌표를 구하는 메서드(ID2 기준의 좌표 -> SPPID 좌표)
3366
        /// </summary>
3367
        /// <param name="x"></param>
3368
        /// <param name="y"></param>
3369
        /// <param name="originX"></param>
3370
        /// <param name="originY"></param>
3371
        /// <param name="SPPIDLabelLocation"></param>
3372
        /// <param name="location"></param>
3373
        private void CalcLabelLocation(ref double x, ref double y, double originX, double originY, SPPIDLabelLocationInfo SPPIDLabelLocation, Location location)
3374
        {
3375
            if (location == Location.None)
3376
            {
3377
                x = originX;
3378
                y = originY;
3379
            }
3380
            else
3381
            {
3382
                if (location.HasFlag(Location.Center))
3383
                {
3384
                    x = (SPPIDLabelLocation.X1 + SPPIDLabelLocation.X2) / 2;
3385
                    y = (SPPIDLabelLocation.Y1 + SPPIDLabelLocation.Y2) / 2;
3386
                }
3387

    
3388
                if (location.HasFlag(Location.Left))
3389
                    x = SPPIDLabelLocation.X1;
3390
                else if (location.HasFlag(Location.Right))
3391
                    x = SPPIDLabelLocation.X2;
3392

    
3393
                if (location.HasFlag(Location.Down))
3394
                    y = SPPIDLabelLocation.Y1;
3395
                else if (location.HasFlag(Location.Up))
3396
                    y = SPPIDLabelLocation.Y2;
3397
            }
3398
        }
3399

    
3400
        /// <summary>
3401
        /// Symbol의 우선순위 Modeling 목록을 가져온다.
3402
        /// 1. Angle Valve
3403
        /// 2. 3개로 이루어진 Symbol Group
3404
        /// </summary>
3405
        /// <returns></returns>
3406
        private List<Symbol> GetPrioritySymbol()
3407
        {
3408
            DataTable symbolTable = document.SymbolTable;
3409
            // List에 순서대로 쌓는다.
3410
            List<Symbol> symbols = new List<Symbol>();
3411

    
3412
            // Angle Valve 부터
3413
            foreach (var symbol in document.SYMBOLS.FindAll(x => x.CONNECTORS.FindAll(y => y.Index == 0).Count == 2))
3414
            {
3415
                if (!symbols.Contains(symbol))
3416
                {
3417
                    double originX = 0;
3418
                    double originY = 0;
3419

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

    
3424
                    SlopeType slopeType1 = SlopeType.None;
3425
                    SlopeType slopeType2 = SlopeType.None;
3426
                    foreach (Connector connector in symbol.CONNECTORS.FindAll(x => x.Index == 0))
3427
                    {
3428
                        double connectorX = 0;
3429
                        double connectorY = 0;
3430
                        SPPIDUtil.ConvertPointBystring(connector.CONNECTPOINT, ref connectorX, ref connectorY);
3431
                        if (slopeType1 == SlopeType.None)
3432
                            slopeType1 = SPPIDUtil.CalcSlope(originX, originY, connectorX, connectorY);
3433
                        else
3434
                            slopeType2 = SPPIDUtil.CalcSlope(originX, originY, connectorX, connectorY);
3435
                    }
3436

    
3437
                    if ((slopeType1 == SlopeType.VERTICAL && slopeType2 == SlopeType.HORIZONTAL) ||
3438
                        (slopeType2 == SlopeType.VERTICAL && slopeType1 == SlopeType.HORIZONTAL))
3439
                        symbols.Add(symbol);
3440
                }
3441
            }
3442

    
3443
            List<Symbol> tempSymbols = new List<Symbol>();
3444
            // Conn 갯수 기준
3445
            foreach (var item in document.SYMBOLS)
3446
            {
3447
                if (!symbols.Contains(item))
3448
                    tempSymbols.Add(item);
3449
            }
3450
            tempSymbols.Sort(SortSymbolPriority);
3451
            symbols.AddRange(tempSymbols);
3452

    
3453
            return symbols;
3454
        }
3455

    
3456
        private void SetPriorityLine()
3457
        {
3458
            document.LINES.Sort(SortLinePriority);
3459

    
3460
            int SortLinePriority(Line a, Line b)
3461
            {
3462
                // Branch 없는것부터
3463
                int branchRetval = CompareBranchLine(a, b);
3464
                if (branchRetval != 0)
3465
                {
3466
                    return branchRetval;
3467
                }
3468
                else
3469
                {
3470
                    // Symbol 연결 갯수
3471
                    int connSymbolRetval = CompareConnSymbol(a, b);
3472
                    if (connSymbolRetval != 0)
3473
                    {
3474
                        return connSymbolRetval;
3475
                    }
3476
                    else
3477
                    {
3478
                        // 아이템 연결 갯수(심볼, Line이면서 Not Branch)
3479
                        int connItemRetval = CompareConnItem(a, b);
3480
                        if (connItemRetval != 0)
3481
                        {
3482
                            return connItemRetval;
3483
                        }
3484
                        else
3485
                        {
3486
                            // ConnectedItem이 없는것
3487
                            int noneConnRetval = CompareNoneConn(a, b);
3488
                            if (noneConnRetval != 0)
3489
                            {
3490
                                return noneConnRetval;
3491
                            }
3492
                            else
3493
                            {
3494

    
3495
                            }
3496
                        }
3497
                    }
3498
                }
3499

    
3500
                return 0;
3501
            }
3502

    
3503
            int CompareConnSymbol(Line a, Line b)
3504
            {
3505
                List<Connector> connectorsA = a.CONNECTORS
3506
                    .Where(conn => conn.ConnectedObject != null && conn.ConnectedObject.GetType() == typeof(Symbol))
3507
                    .ToList();
3508

    
3509
                List<Connector> connectorsB = b.CONNECTORS
3510
                    .Where(conn => conn.ConnectedObject != null && conn.ConnectedObject.GetType() == typeof(Symbol))
3511
                    .ToList();
3512

    
3513
                // 오름차순
3514
                return connectorsB.Count.CompareTo(connectorsA.Count);
3515
            }
3516

    
3517
            int CompareConnItem(Line a, Line b)
3518
            {
3519
                List<Connector> connectorsA = a.CONNECTORS
3520
                    .Where(conn => conn.ConnectedObject != null && 
3521
                    (conn.ConnectedObject.GetType() == typeof(Symbol) || 
3522
                    (conn.ConnectedObject.GetType() == typeof(Line) && !SPPIDUtil.IsBranchLine((Line)conn.ConnectedObject, a))))
3523
                    .ToList();
3524

    
3525
                List<Connector> connectorsB = b.CONNECTORS
3526
                    .Where(conn => conn.ConnectedObject != null &&
3527
                    (conn.ConnectedObject.GetType() == typeof(Symbol) ||
3528
                    (conn.ConnectedObject.GetType() == typeof(Line) && !SPPIDUtil.IsBranchLine((Line)conn.ConnectedObject, b))))
3529
                    .ToList();
3530

    
3531
                // 오름차순
3532
                return connectorsB.Count.CompareTo(connectorsA.Count);
3533
            }
3534

    
3535
            int CompareBranchLine(Line a, Line b)
3536
            {
3537
                List<Connector> connectorsA = a.CONNECTORS
3538
                    .Where(conn => conn.ConnectedObject != null && conn.ConnectedObject.GetType() == typeof(Line) && SPPIDUtil.IsBranchLine(a, conn.ConnectedObject as Line))
3539
                    .ToList();
3540
                List<Connector> connectorsB = b.CONNECTORS
3541
                    .Where(conn => conn.ConnectedObject != null && conn.ConnectedObject.GetType() == typeof(Line) && SPPIDUtil.IsBranchLine(b, conn.ConnectedObject as Line))
3542
                    .ToList();
3543

    
3544
                // 내림차순
3545
                return connectorsA.Count.CompareTo(connectorsB.Count);
3546
            }
3547

    
3548
            int CompareNoneConn(Line a, Line b)
3549
            {
3550
                List<Connector> connectorsA = a.CONNECTORS
3551
                    .Where(conn => conn.ConnectedObject == null)
3552
                    .ToList();
3553

    
3554
                List<Connector> connectorsB = b.CONNECTORS
3555
                    .Where(conn => conn.ConnectedObject == null)
3556
                    .ToList();
3557

    
3558
                // 오름차순
3559
                return connectorsB.Count.CompareTo(connectorsA.Count);
3560
            }
3561
        }
3562

    
3563
        private void SortBranchLines()
3564
        {
3565
            NewBranchLines.Sort(SortBranchLine);
3566
            int SortBranchLine(Line a, Line b)
3567
            {
3568
                int countA = a.CONNECTORS.FindAll(x => x.ConnectedObject != null &&
3569
                 x.ConnectedObject.GetType() == typeof(Line) &&
3570
                 SPPIDUtil.IsBranchLine(x.ConnectedObject as Line, a) &&
3571
                 string.IsNullOrEmpty(((Line)x.ConnectedObject).SPPID.ModelItemId)).Count;
3572

    
3573
                int countB = b.CONNECTORS.FindAll(x => x.ConnectedObject != null &&
3574
                 x.ConnectedObject.GetType() == typeof(Line) &&
3575
                 SPPIDUtil.IsBranchLine(x.ConnectedObject as Line, b) &&
3576
                 string.IsNullOrEmpty(((Line)x.ConnectedObject).SPPID.ModelItemId)).Count;
3577

    
3578
                // 내림차순
3579
                return countA.CompareTo(countB);
3580
            }
3581
        }
3582

    
3583
        private static int SortSymbolPriority(Symbol a, Symbol b)
3584
        {
3585
            int countA = a.CONNECTORS.FindAll(x => !string.IsNullOrEmpty(x.CONNECTEDITEM) && x.CONNECTEDITEM != "None").Count;
3586
            int countB = b.CONNECTORS.FindAll(x => !string.IsNullOrEmpty(x.CONNECTEDITEM) && x.CONNECTEDITEM != "None").Count;
3587
            int retval = countB.CompareTo(countA);
3588
            if (retval != 0)
3589
                return retval;
3590
            else
3591
                return a.SPPID.ORIGINAL_X.CompareTo(b.SPPID.ORIGINAL_X);
3592
        }
3593

    
3594
        /// <summary>
3595
        /// Graphic OID로 해당 Symbol의 크기를 구하여 Zoom
3596
        /// </summary>
3597
        /// <param name="graphicOID"></param>
3598
        /// <param name="milliseconds"></param>
3599
        private void ZoomObjectByGraphicOID(string graphicOID, int milliseconds = 150)
3600
        {
3601
            if (radApp.ActiveDocument.ActiveSheet.DrawingObjects[graphicOID] != null)
3602
            {
3603
                double minX = 0;
3604
                double minY = 0;
3605
                double maxX = 0;
3606
                double maxY = 0;
3607
                radApp.ActiveDocument.ActiveSheet.DrawingObjects[graphicOID].Range(out minX, out minY, out maxX, out maxY);
3608
                radApp.ActiveWindow.ZoomArea2(minX - 0.007, minY - 0.007, maxX + 0.007, maxY + 0.007, null);
3609

    
3610
                Thread.Sleep(milliseconds);
3611
            }
3612
        }
3613

    
3614
        /// <summary>
3615
        /// ComObject를 Release
3616
        /// </summary>
3617
        /// <param name="objVars"></param>
3618
        public void ReleaseCOMObjects(params object[] objVars)
3619
        {
3620
            int intNewRefCount = 0;
3621
            foreach (object obj in objVars)
3622
            {
3623
                if (!Information.IsNothing(obj) && System.Runtime.InteropServices.Marshal.IsComObject(obj))
3624
                    intNewRefCount = intNewRefCount + System.Runtime.InteropServices.Marshal.FinalReleaseComObject(obj);
3625
            }
3626
        }
3627
    }
3628
}
클립보드 이미지 추가 (최대 크기: 500 MB)