프로젝트

일반

사용자정보

통계
| 개정판:

hytos / DTI_PID / SPPIDConverter / AutoModeling.cs @ ebe81079

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

1
using System;
2
using System.Collections.Generic;
3
using System.Linq;
4
using System.Text;
5
using System.Threading.Tasks;
6
using System.Data;
7
using Llama;
8
using Plaice;
9
using Ingr.RAD2D.Interop.RAD2D;
10
using Ingr.RAD2D.Internal;
11
using Ingr.RAD2D.Helper;
12
using Converter.BaseModel;
13
using Converter.SPPID.Model;
14
using Converter.SPPID.Properties;
15
using Converter.SPPID.Util;
16
using Converter.SPPID.DB;
17
using Ingr.RAD2D.MacroControls.CmdCtrl;
18
using Ingr.RAD2D;
19
using System.Windows;
20
using System.Threading;
21
using System.Drawing;
22
using Microsoft.VisualBasic;
23
using Newtonsoft.Json;
24
using DevExpress.XtraSplashScreen;
25
namespace Converter.SPPID
26
{
27
    public class AutoModeling
28
    {
29
        Placement _placement;
30
        LMADataSource dataSource;
31
        dynamic newDrawing;
32
        dynamic application;
33
        Ingr.RAD2D.Application radApp;
34
        SPPID_Document document;
35
        ETCSetting _ETCSetting;
36

    
37
        public string DocumentLabelText { get; set; }
38

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

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

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

    
64
                }
65
            }
66
        }
67

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

    
80
                CreateDocument(ref drawingNumber, ref drawingName);
81

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

    
89
                    // Equipment Modeling
90
                    RunEquipmentModeling();
91

    
92
                    // Symbol Modeling
93
                    RunSymbolModeling();
94

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

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

    
101
                    // Clear Attribute
102
                    RunClearAttribute();
103

    
104
                    // EndBreak Modeling
105
                    RunEndBreakModeling();
106

    
107
                    // SpecBreak Modeling
108
                    RunSpecBreakModeling();
109

    
110
                    
111

    
112

    
113
                    
114
                    
115

    
116

    
117
                    //// LineNumber Modeling
118
                    //SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "LineNumbers Modeling");
119
                    //foreach (var item in document.LINENUMBERS)
120
                    //    try
121
                    //    {
122
                    //        LineNumberModeling(item);
123
                    //    }
124
                    //    catch (Exception ex)
125
                    //    {
126
                    //        Log.Write("Error in LineNumberModeling");
127
                    //        Log.Write("UID : " + item.UID);
128
                    //        Log.Write(ex.Message);
129
                    //        Log.Write(ex.StackTrace);
130
                    //    }
131

    
132

    
133
                    //// FlowMark Modeling
134
                    //SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Flow Mark Modeling");
135
                    //foreach (var item in document.LINES)
136
                    //    try
137
                    //    {
138
                    //        FlowMarkModeling(item);
139
                    //    }
140
                    //    catch (Exception ex)
141
                    //    {
142
                    //        Log.Write("Error in FlowMarkModeling");
143
                    //        Log.Write("UID : " + item.UID);
144
                    //        Log.Write(ex.Message);
145
                    //        Log.Write(ex.StackTrace);
146
                    //    }
147

    
148

    
149
                    //// Note Symbol Modeling
150
                    //SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Note Symbol Modeling");
151
                    //foreach (var item in document.SYMBOLS)
152
                    //    try
153
                    //    {
154
                    //        NoteSymbolModeling(item);
155
                    //    }
156
                    //    catch (Exception ex)
157
                    //    {
158
                    //        Log.Write("Error in NoteSymbolModeling");
159
                    //        Log.Write("UID : " + item.UID);
160
                    //        Log.Write(ex.Message);
161
                    //        Log.Write(ex.StackTrace);
162
                    //    }
163

    
164

    
165
                    //// Note Modeling
166
                    //SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Notes Modeling");
167
                    //foreach (var item in document.NOTES)
168
                    //    try
169
                    //    {
170
                    //        NoteModeling(item);
171
                    //    }
172
                    //    catch (Exception ex)
173
                    //    {
174
                    //        Log.Write("Error in NoteModeling");
175
                    //        Log.Write("UID : " + item.UID);
176
                    //        Log.Write(ex.Message);
177
                    //        Log.Write(ex.StackTrace);
178
                    //    }
179

    
180
                    //// Text Modeling
181
                    //SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Texts Modeling");
182
                    //foreach (var item in document.TEXTINFOS)
183
                    //    try
184
                    //    {
185
                    //        TextModeling(item);
186
                    //    }
187
                    //    catch (Exception ex)
188
                    //    {
189
                    //        Log.Write("Error in TextModeling");
190
                    //        Log.Write("UID : " + item.UID);
191
                    //        Log.Write(ex.Message);
192
                    //        Log.Write(ex.StackTrace);
193
                    //    }
194

    
195
                    //// Input LineNumber Attribute
196
                    //SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Set LineNumbers Attribute");
197
                    //foreach (var item in document.LINENUMBERS)
198
                    //    try
199
                    //    {
200
                    //        InputLineNumberAttribute(item);
201
                    //    }
202
                    //    catch (Exception ex)
203
                    //    {
204
                    //        Log.Write("Error in InputLineNumberAttribute");
205
                    //        Log.Write("UID : " + item.UID);
206
                    //        Log.Write(ex.Message);
207
                    //        Log.Write(ex.StackTrace);
208
                    //    }
209

    
210
                    //// Input Symbol Attribute
211
                    //SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Set Symbols Attribute");
212
                    //foreach (var item in document.SYMBOLS)
213
                    //    try
214
                    //    {
215
                    //        InputSymbolAttribute(item, item.ATTRIBUTES);
216
                    //    }
217
                    //    catch (Exception ex)
218
                    //    {
219
                    //        Log.Write("Error in InputSymbolAttribute");
220
                    //        Log.Write("UID : " + item.UID);
221
                    //        Log.Write(ex.Message);
222
                    //        Log.Write(ex.StackTrace);
223
                    //    }
224

    
225
                    //// Input SpecBreak Attribute
226
                    //SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Set Symbols Attribute");
227
                    //foreach (var item in document.SpecBreaks)
228
                    //    try
229
                    //    {
230
                    //        InputSpecBreakAttribute(item);
231
                    //    }
232
                    //    catch (Exception ex)
233
                    //    {
234
                    //        Log.Write("Error in InputSpecBreakAttribute");
235
                    //        Log.Write("UID : " + item.UID);
236
                    //        Log.Write(ex.Message);
237
                    //        Log.Write(ex.StackTrace);
238
                    //    }
239

    
240
                    //// Label Symbol Modeling
241
                    //SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Labels Modeling");
242
                    //foreach (var item in document.SYMBOLS)
243
                    //    try
244
                    //    {
245
                    //        LabelSymbolModeling(item);
246
                    //    }
247
                    //    catch (Exception ex)
248
                    //    {
249
                    //        Log.Write("Error in LabelSymbolModeling");
250
                    //        Log.Write("UID : " + item.UID);
251
                    //        Log.Write(ex.Message);
252
                    //        Log.Write(ex.StackTrace);
253
                    //    }
254

    
255
                    //// LineRun Line Join
256
                    //SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Join LineRuns");
257
                    //foreach (LineNumber lineNumber in document.LINENUMBERS)
258
                    //    try
259
                    //    {
260
                    //        foreach (LineRun run in lineNumber.RUNS)
261
                    //            JoinRunLine(run);
262
                    //    }
263
                    //    catch (Exception ex)
264
                    //    {
265
                    //        Log.Write("Error in JoinRunLine");
266
                    //        Log.Write("UID : " + lineNumber.UID);
267
                    //        Log.Write(ex.Message);
268
                    //        Log.Write(ex.StackTrace);
269
                    //    }
270

    
271
                    //// TrimLineRun Line Join
272
                    //foreach (TrimLine trimLine in document.TRIMLINES)
273
                    //    try
274
                    //    {
275
                    //        foreach (LineRun run in trimLine.RUNS)
276
                    //            JoinRunLine(run);
277
                    //    }
278
                    //    catch (Exception ex)
279
                    //    {
280
                    //        Log.Write("Error in JoinRunLine");
281
                    //        Log.Write("UID : " + trimLine.UID);
282
                    //        Log.Write(ex.Message);
283
                    //        Log.Write(ex.StackTrace);
284
                    //    }
285
                }
286
            }
287
            catch (Exception ex)
288
            {
289
                SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.ClearParent, null);
290
                SplashScreenManager.CloseForm(false);
291
                System.Windows.Forms.MessageBox.Show(ex.Message + "\r\n" + ex.StackTrace);
292
            }
293
            finally
294
            {
295
                Log.Write("End Modeling");
296
                application.ActiveWindow.Fit();
297

    
298
                if (radApp.ActiveDocument != null)
299
                {
300
                    radApp.ActiveDocument.Save();
301
                    //radApp.ActiveDocument.SaveOnClose = false;
302
                    //radApp.ActiveDocument.Close(false);
303

    
304
                    ReleaseCOMObjects(newDrawing);
305
                }
306

    
307
                ReleaseCOMObjects(dataSource);
308
                ReleaseCOMObjects(_placement);
309

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

    
422
                        modelItem.Commit();
423
                        ReleaseCOMObjects(modelItem);
424
                    }
425
                }
426
                if (!endClearModelItemID.Contains(modelItemID))
427
                    endClearModelItemID.Add(modelItemID);
428
                SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
429
            }
430
            for (int i = 0; i < document.SYMBOLS.Count; i++)
431
            {
432
                Symbol item = document.SYMBOLS[i];
433
                string repID = item.SPPID.RepresentationId;
434
                string modelItemID = item.SPPID.ModelItemID;
435
                if (!string.IsNullOrEmpty(modelItemID))
436
                {
437
                    LMModelItem modelItem = dataSource.GetModelItem(modelItemID);
438
                    if (modelItem != null)
439
                    {
440
                        LMAAttribute attribute = modelItem.Attributes["NominalDiameter"];
441
                        if (attribute != null)
442
                            attribute.set_Value(DBNull.Value);
443
                        attribute = modelItem.Attributes["PipingPoint1.NominalDiameter"];
444
                        if (attribute != null)
445
                            attribute.set_Value(DBNull.Value);
446
                        attribute = modelItem.Attributes["PipingPoint2.NominalDiameter"];
447
                        if (attribute != null)
448
                            attribute.set_Value(DBNull.Value);
449
                        modelItem.Commit();
450

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

    
472
                                    modelItem.Commit();
473
                                    ReleaseCOMObjects(modelItem);
474
                                }
475
                            }
476
                        }
477
                        foreach (LMConnector connector in symbol.Connect2Connectors)
478
                        {
479
                            if (connector.get_ItemStatus() == "Active" && !endClearModelItemID.Contains(connector.ModelItemID))
480
                            {
481
                                endClearModelItemID.Add(connector.ModelItemID);
482
                                LMModelItem modelItem = connector.ModelItemObject;
483
                                if (modelItem != null)
484
                                {
485
                                    LMAAttribute attribute = modelItem.Attributes["NominalDiameter"];
486
                                    if (attribute != null)
487
                                        attribute.set_Value(DBNull.Value);
488

    
489
                                    modelItem.Commit();
490
                                    ReleaseCOMObjects(modelItem);
491
                                }
492
                            }
493
                        }
494
                        ReleaseCOMObjects(symbol);
495
                    }
496
                }
497
                SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
498
            }
499
            SetSystemEditingCommand(true);
500
        }
501
        private void RunEndBreakModeling()
502
        {
503
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetAllProgress, document.EndBreaks.Count);
504
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "EndBreaks Modeling");
505
            foreach (var item in document.EndBreaks)
506
                try
507
                {
508
                    EndBreakModeling(item);
509
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
510
                    if (string.IsNullOrEmpty(item.SPPID.ModelItemID))
511
                        Log.Write("Fail modeling endbreak UID : " + item.UID);
512
                }
513
                catch (Exception ex)
514
                {
515
                    Log.Write("Error in EndBreakModeling");
516
                    Log.Write("UID : " + item.UID);
517
                    Log.Write(ex.Message);
518
                    Log.Write(ex.StackTrace);
519
                }
520
        }
521
        private void RunSpecBreakModeling()
522
        {
523
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetAllProgress, document.SpecBreaks.Count);
524
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "SpecBreaks Modeling");
525
            foreach (var item in document.SpecBreaks)
526
                try
527
                {
528
                    SpecBreakModeling(item);
529
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
530
                    if (string.IsNullOrEmpty(item.SPPID.ModelItemID))
531
                        Log.Write("Fail modeling specbreak UID : " + item.UID);
532
                }
533
                catch (Exception ex)
534
                {
535
                    Log.Write("Error in SpecBreakModeling");
536
                    Log.Write("UID : " + item.UID);
537
                    Log.Write(ex.Message);
538
                    Log.Write(ex.StackTrace);
539
                }
540
        }
541

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

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

    
570
            List<string> drawingNameList = new List<string>();
571
            List<string> drawingNumberList = new List<string>();
572

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

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

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

    
605
            ReleaseCOMObjects(drawings);
606
        }
607

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

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

    
645
            LMSymbol _LMSymbol = null;
646

    
647
            string mappingPath = symbol.SPPID.MAPPINGNAME;
648
            double x = symbol.SPPID.ORIGINAL_X;
649
            double y = symbol.SPPID.ORIGINAL_Y;
650
            int mirror = 0;
651
            double angle = symbol.ANGLE;
652

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

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

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

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

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

    
678
                    if (reModelingConnector != null)
679
                        ReModelingLMConnector(reModelingConnector);
680
                }
681

    
682
                ReleaseCOMObjects(_TargetItem);
683
            }
684
            else
685
                _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle);
686

    
687
            if (_LMSymbol != null)
688
            {
689
                _LMSymbol.Commit();
690
                symbol.SPPID.RepresentationId = _LMSymbol.AsLMRepresentation().Id;
691
                symbol.SPPID.ModelItemID = _LMSymbol.ModelItemID;
692
                symbol.SPPID.GraphicOID = _LMSymbol.get_GraphicOID();
693

    
694
                foreach (var item in symbol.ChildSymbols)
695
                    CreateChildSymbol(item, _LMSymbol);
696

    
697
                symbol.SPPID.SPPID_X = _LMSymbol.get_XCoordinate();
698
                symbol.SPPID.SPPID_Y = _LMSymbol.get_YCoordinate();
699

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

    
707
                foreach (var item in symbol.SPPID.CorrectionX_GroupSymbols)
708
                    item.SPPID.ORIGINAL_X = symbol.SPPID.SPPID_X;
709
                foreach (var item in symbol.SPPID.CorrectionY_GroupSymbols)
710
                    item.SPPID.ORIGINAL_Y = symbol.SPPID.SPPID_Y;
711

    
712
                ReleaseCOMObjects(_LMSymbol);
713
            }
714
        }
715

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

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

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

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

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

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

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

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

    
799
            double percentX = (x2 - x1) / sizeWidth;
800
            double percentY = (y2 - y1) / sizeHeight;
801

    
802
            double SPPIDgapX = gapX * percentX;
803
            double SPPIDgapY = gapY * percentY;
804

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

    
820
            ReleaseCOMObjects(_TargetItem);
821
        }
822

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

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

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

    
864
            foreach (var childSymbol in symbol.ChildSymbols)
865
                GetSPPIDChildSymbolRange(childSymbol, ref range, points);
866

    
867
            ReleaseCOMObjects(_TargetItem);
868
        }
869

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

    
881
            foreach (var childSymbol in symbol.ChildSymbols)
882
                GetSPPIDChildSymbolRange(childSymbol, ref range);
883

    
884
            ReleaseCOMObjects(_TargetItem);
885
        }
886

    
887
        private void GetSPPIDSymbolRange(List<Symbol> symbols, ref double[] range)
888
        {
889
            double[] tempRange = new double[] { double.MaxValue, double.MaxValue, double.MinValue, double.MinValue };
890
            foreach (var symbol in symbols)
891
            {
892
                LMSymbol _TargetItem = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
893
                Ingr.RAD2D.Symbol2d symbol2d = radApp.ActiveDocument.ActiveSheet.DrawingObjects[_TargetItem.get_GraphicOID().ToString()];
894
                double x1 = 0;
895
                double y1 = 0;
896
                double x2 = 0;
897
                double y2 = 0;
898
                symbol2d.Range(out x1, out y1, out x2, out y2);
899

    
900
                tempRange[0] = Math.Min(tempRange[0], x1);
901
                tempRange[1] = Math.Min(tempRange[1], y1);
902
                tempRange[2] = Math.Max(tempRange[2], x2);
903
                tempRange[3] = Math.Max(tempRange[3], y2);
904

    
905
                foreach (var childSymbol in symbol.ChildSymbols)
906
                    GetSPPIDChildSymbolRange(childSymbol, ref tempRange);
907

    
908
                ReleaseCOMObjects(_TargetItem);
909
            }
910

    
911
            range = tempRange;
912
        }
913

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

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

    
943
            foreach (var loopChildSymbol in childSymbol.ChildSymbols)
944
                GetSPPIDChildSymbolRange(loopChildSymbol, ref range, points);
945

    
946
            ReleaseCOMObjects(_ChildSymbol);
947
        }
948

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

    
963
            foreach (var loopChildSymbol in childSymbol.ChildSymbols)
964
                GetSPPIDChildSymbolRange(loopChildSymbol, ref range);
965

    
966
            ReleaseCOMObjects(_ChildSymbol);
967
        }
968

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

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

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

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

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

    
1040
                                        if (result)
1041
                                            break;
1042
                                    }
1043

    
1044
                                    if (result)
1045
                                        break;
1046
                                }
1047
                            }
1048

    
1049
                            _LMLabelPresist.Commit();
1050
                            ReleaseCOMObjects(_LMLabelPresist);
1051
                        }
1052

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

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

    
1078
                    foreach (var item in connectorVertices)
1079
                        if (item.Key != null)
1080
                            ReleaseCOMObjects(item.Key);
1081
                }
1082
            }
1083
        }
1084

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

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

    
1102
            SPPIDUtil.ConvertGridPoint(ref x, ref y);
1103

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

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

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

    
1148
            if (targetItem != null)
1149
            {
1150
                ReleaseCOMObjects(targetItem);
1151
            }
1152

    
1153
            ReleaseCOMObjects(_LMSymbol);
1154
        }
1155

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

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

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

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

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

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

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

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

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

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

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

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

    
1353

    
1354
            ReleaseCOMObjects(_LMSymbol);
1355
        }
1356

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

    
1362
            List<Line> group = new List<Line>();
1363
            GetConnectedLineGroup(line, group);
1364
            LineCoordinateCorrection(group);
1365

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

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

    
1422
                            placeRunInputs.AddPoint(x, y);
1423

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

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

    
1448
                    if (bRemodelingStart || bRemodelingEnd)
1449
                        ReModelingLine(groupLine, _lMConnector, _LMSymbolStart, _LMSymbolEnd, bRemodelingStart, bRemodelingEnd);
1450
                    
1451
                    ReleaseCOMObjects(_lMConnector);
1452
                }
1453

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

    
1461
                foreach (var item in removeLines)
1462
                    RemoveLineForModeling(item as Line);
1463

    
1464
                if (_LMAItem != null)
1465
                    ReleaseCOMObjects(_LMAItem);
1466
                if (placeRunInputs != null)
1467
                    ReleaseCOMObjects(placeRunInputs);
1468
                if (_LMSymbolStart != null)
1469
                    ReleaseCOMObjects(_LMSymbolStart);
1470
                if (_LMSymbolEnd != null)
1471
                    ReleaseCOMObjects(_LMSymbolEnd);
1472

    
1473
                if (isBranchModeling && NewBranchLines.Contains(groupLine))
1474
                    NewBranchLines.Remove(groupLine);
1475
            }
1476
        }
1477

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

    
1506
                ReleaseCOMObjects(modelItem);
1507
            }
1508
        }
1509

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

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

    
1533
            // 역으로 심볼이 있을 경우 좌표 보정
1534
            LineCoordinateCorrectionForLastLine(group);
1535
        }
1536

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

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

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

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

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

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

    
1621
            ChangeLineSPPIDCoordinateByConnector(line, connItem, x, y);
1622
        }
1623

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

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

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

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

    
1692
                SpecBreak startSpecBreak = document.SpecBreaks.Find(x =>
1693
                (x.DownStreamUID == symbolUID || x.UpStreamUID == symbolUID) &&
1694
                (x.DownStreamUID == lineUID || x.UpStreamUID == lineUID));
1695

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

    
1700
                if (startSpecBreak != null || startEndBreak != null)
1701
                    result = true;
1702
            }
1703
        }
1704

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

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

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

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

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

    
1796
            _placement.PIDRemovePlacement(prevLMConnector.AsLMRepresentation());
1797
            newConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
1798

    
1799
            ReleaseCOMObjects(placeRunInputs);
1800
            ReleaseCOMObjects(_LMAItem);
1801
            ReleaseCOMObjects(modelItem);
1802

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

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

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

    
1845
                line.SPPID.ModelItemId = newConnector.ModelItemID;
1846
                ReleaseCOMObjects(newConnector);
1847
            }
1848

    
1849
            ReleaseCOMObjects(modelItem);
1850
        }
1851

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

    
1874
                    if (result)
1875
                        break;
1876
                }
1877
            }
1878
            foreach (var item in dicVertices)
1879
                ReleaseCOMObjects(item.Key);
1880
        }
1881

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

    
1907
                            if (child != null)
1908
                                break;
1909
                        }
1910

    
1911
                        if (child != null)
1912
                            _LMSymbol = dataSource.GetSymbol(child.SPPID.RepresentationId);
1913
                    }
1914

    
1915
                    break;
1916
                }
1917
            }
1918

    
1919
            return _LMSymbol;
1920
        }
1921

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

    
1938
            return null;
1939
        }
1940

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

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

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

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

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

    
1997
                placeRunInputs.AddSymbolTarget(connector.ConnectItem1SymbolObject, x, y);
1998
                placeRunInputs.AddSymbolTarget(connector.ConnectItem2SymbolObject, x, y);
1999

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

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

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

    
2037
                _placement.PIDRemovePlacement(connector.AsLMRepresentation());
2038
                newConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
2039

    
2040
                foreach (var line in lines)
2041
                    line.SPPID.ModelItemId = newConnector.ModelItemID;
2042
            }
2043

    
2044

    
2045
            return newConnector;
2046
        }
2047

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

    
2057
            if (upStreamObj != null &&
2058
                downStreamObj != null)
2059
            {
2060
                LMConnector targetLMConnector = FindBreakLineTarget(upStreamObj, downStreamObj);
2061

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

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

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

    
2099
                foreach (LMConnector connector in targetLMSymbol.Avoid1Connectors)
2100
                {
2101
                    if (connector.get_ItemStatus() != "Active")
2102
                        continue;
2103

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

    
2116
                foreach (LMConnector connector in targetLMSymbol.Avoid2Connectors)
2117
                {
2118
                    if (connector.get_ItemStatus() != "Active")
2119
                        continue;
2120

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

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

    
2141
                if (targetModelItem != null && connectedModelItem != null)
2142
                {
2143
                    foreach (LMRepresentation rep in targetModelItem.Representations)
2144
                    {
2145
                        if (targetConnector != null)
2146
                            break;
2147

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

    
2152
                            if (IsConnected(_LMConnector, connectedModelItem))
2153
                                targetConnector = _LMConnector;
2154
                            else
2155
                                ReleaseCOMObjects(_LMConnector);
2156
                        }
2157
                    }
2158

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

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

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

    
2189
                        if (IsConnected(connector, targetModelItem))
2190
                        {
2191
                            targetConnector = connector;
2192
                            break;
2193
                        }
2194
                    }
2195

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

    
2203
                            if (IsConnected(connector, targetModelItem))
2204
                            {
2205
                                targetConnector = connector;
2206
                                break;
2207
                            }
2208
                        }
2209
                    }
2210
                }
2211

    
2212
            }
2213

    
2214
            return targetConnector;
2215
        }
2216

    
2217
        private bool IsConnected(LMConnector connector, LMModelItem modelItem)
2218
        {
2219
            bool result = false;
2220

    
2221
            foreach (LMRepresentation rep in modelItem.Representations)
2222
            {
2223
                if (result)
2224
                    break;
2225

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

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

    
2263
                    ReleaseCOMObjects(_LMConnector);
2264
                }
2265
            }
2266

    
2267

    
2268
            return result;
2269
        }
2270

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

    
2283
            // item2가 item1으로 조인
2284
            _placement.PIDJoinRuns(ref item1, ref item2);
2285
            item1.Commit();
2286
            item2.Commit();
2287

    
2288
            List<Line> lines = SPPIDUtil.FindLinesByModelId(document, fromModelItemId);
2289
            foreach (var line in lines)
2290
                line.SPPID.ModelItemId = toModelItemId;
2291

    
2292
            ReleaseCOMObjects(modelItem1);
2293
            ReleaseCOMObjects(item1);
2294
            ReleaseCOMObjects(modelItem2);
2295
            ReleaseCOMObjects(item2);
2296
        }
2297

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

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

    
2321
            ReleaseCOMObjects(modelItem);
2322
            ReleaseCOMObjects(item);
2323
        }
2324

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

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

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

    
2377
                ReleaseCOMObjects(modelItem);
2378
            }
2379

    
2380
            return connectorVertices;
2381
        }
2382

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

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

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

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

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

    
2446
            return targetConnector;
2447
        }
2448

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

    
2453
            double length = double.MaxValue;
2454
            LMConnector targetConnector = null;
2455
            double[] resultPoint = null;
2456
            List<double[]> targetVertices = null;
2457

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

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

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

    
2488
                    double maxLineX = Math.Max(point1[0], point2[0]);
2489
                    double minLineX = Math.Min(point1[0], point2[0]);
2490
                    double maxLineY = Math.Max(point1[1], point2[1]);
2491
                    double minLineY = Math.Min(point1[1], point2[1]);
2492

    
2493
                    SlopeType slope = SPPIDUtil.CalcSlope(minLineX, minLineY, maxLineX, maxLineY);
2494

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

    
2531
            foreach (var item in vertices)
2532
                if (item.Key != null && item.Key != targetConnector)
2533
                    ReleaseCOMObjects(item.Key);
2534

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

    
2541
                GridSetting gridSetting = GridSetting.GetInstance();
2542

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

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

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

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

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

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

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

    
2702
            x = resultPoint[0];
2703
            y = resultPoint[1];
2704

    
2705
            return targetConnector;
2706
        }
2707

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

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

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

    
2740
                    }
2741
                }
2742
            }
2743
        }
2744

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

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

    
2773
                if (line.SPPID.START_X < line.SPPID.END_X)
2774
                    tempX = line.SPPID.START_X + gapX;
2775
                else
2776
                    tempX = line.SPPID.START_X - gapX;
2777

    
2778
                if (line.SPPID.START_Y < line.SPPID.END_Y)
2779
                    tempY = line.SPPID.START_Y + gapY;
2780
                else
2781
                    tempY = line.SPPID.START_Y - gapY;
2782

    
2783

    
2784

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

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

    
2808
                                startPoint = point1;
2809
                                endPoint = point2;
2810
                            }
2811

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

    
2818
                                startPoint = point1;
2819
                                endPoint = point2;
2820
                            }
2821
                        }
2822
                    }
2823
                }
2824

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

    
2842
                    if (startPoint[0] < endPoint[0])
2843
                        x = startPoint[0] + gapX;
2844
                    else
2845
                        x = startPoint[0] - gapX;
2846

    
2847
                    if (startPoint[1] < endPoint[1])
2848
                        y = startPoint[1] + gapY;
2849
                    else
2850
                        y = startPoint[1] - gapY;
2851

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

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

    
2870
                    if (_LMSymbol != null)
2871
                    {
2872
                        ReleaseCOMObjects(_LMSymbol);
2873
                    }
2874

    
2875
                }
2876

    
2877
                foreach (var item in connectorVertices)
2878
                    ReleaseCOMObjects(item.Key);
2879
            }
2880
        }
2881

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

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

    
2946
                                        }
2947
                                    }
2948
                                }
2949
                                _LMModelItem.Commit();
2950
                            }
2951
                            if (_LMModelItem != null)
2952
                                ReleaseCOMObjects(_LMModelItem);
2953
                        }
2954
                    }
2955
                }
2956
            }
2957
        }
2958

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

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

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

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

    
2991
                ReleaseCOMObjects(_Attributes);
2992
                ReleaseCOMObjects(_LMModelItem);
2993
                ReleaseCOMObjects(_LMSymbol);
2994
            }
2995
        }
2996

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

    
3006
            if (upStreamObj != null &&
3007
                downStreamObj != null)
3008
            {
3009
                LMConnector targetLMConnector = FindBreakLineTarget(upStreamObj, downStreamObj);
3010

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

    
3028
                                    InputAttributeForSpecBreak(upStreamObj, downStreamObj, upStreamValue, downStreamValue, mapping.SPPIDATTRIBUTENAME);
3029
                                }
3030
                            }
3031
                        }
3032
                    }
3033

    
3034
                    ReleaseCOMObjects(targetLMConnector);
3035
                }
3036
            }
3037

    
3038

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

    
3059
                    foreach (LMConnector connector in upStreamLMSymbol.Avoid1Connectors)
3060
                    {
3061
                        if (connector.get_ItemStatus() != "Active")
3062
                            continue;
3063

    
3064
                        if (connector.Id != zeroLenthConnector.Id)
3065
                            InputLineAttributeForSpecBreakLMConnector(connector, sppidAttributeName, upStreamValue);
3066
                    }
3067

    
3068
                    foreach (LMConnector connector in upStreamLMSymbol.Avoid2Connectors)
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 downStreamLMSymbol.Avoid1Connectors)
3078
                    {
3079
                        if (connector.get_ItemStatus() != "Active")
3080
                            continue;
3081

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

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

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

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

    
3105
                    foreach (LMConnector connector in upStreamLMSymbol.Avoid1Connectors)
3106
                    {
3107
                        if (connector.get_ItemStatus() != "Active")
3108
                            continue;
3109

    
3110
                        if (connector.Id != zeroLenthConnector.Id)
3111
                            InputLineAttributeForSpecBreakLMConnector(connector, sppidAttributeName, upStreamValue);
3112
                    }
3113

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

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

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

    
3132
                    foreach (LMConnector connector in downStreamLMSymbol.Avoid1Connectors)
3133
                    {
3134
                        if (connector.get_ItemStatus() != "Active")
3135
                            continue;
3136

    
3137
                        if (connector.Id != zeroLenthConnector.Id)
3138
                            InputLineAttributeForSpecBreakLMConnector(connector, sppidAttributeName, downStreamValue);
3139
                    }
3140

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

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

    
3150
                    ReleaseCOMObjects(zeroLenthConnector);
3151
                    ReleaseCOMObjects(downStreamLMSymbol);
3152
                }
3153
            }
3154

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

    
3169
                    _LMModelItem.Commit();
3170
                }
3171
                if (_LMModelItem != null)
3172
                    ReleaseCOMObjects(_LMModelItem);
3173
            }
3174

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

    
3189
                    _LMModelItem.Commit();
3190
                }
3191
                if (_LMModelItem != null)
3192
                    ReleaseCOMObjects(_LMModelItem);
3193
            }
3194
            #endregion
3195
        }
3196

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

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

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

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

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

    
3251
                            if (mapping != null)
3252
                            {
3253
                                double x = 0;
3254
                                double y = 0;
3255

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

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

    
3275
                double x = 0;
3276
                double y = 0;
3277
                double angle = text.ANGLE;
3278
                CalcLabelLocation(ref x, ref y, text.SPPID.ORIGINAL_X, text.SPPID.ORIGINAL_Y, text.SPPIDLabelLocation, _ETCSetting.TextLocation);
3279

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

    
3288
                if (_LMAAttribute != null)
3289
                    ReleaseCOMObjects(_LMAAttribute);
3290
                if (_LMItemNote != null)
3291
                    ReleaseCOMObjects(_LMItemNote);
3292
            }
3293
            if (_LMSymbol != null)
3294
                ReleaseCOMObjects(_LMSymbol);
3295
        }
3296

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

    
3307
            if (string.IsNullOrEmpty(note.OWNER) || note.OWNER == "None")
3308
            {
3309
                double x = 0;
3310
                double y = 0;
3311

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

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

    
3323
            if (_LMAAttribute != null)
3324
                ReleaseCOMObjects(_LMAAttribute);
3325
            if (_LMItemNote != null)
3326
                ReleaseCOMObjects(_LMItemNote);
3327
            if (_LMSymbol != null)
3328
                ReleaseCOMObjects(_LMSymbol);
3329
        }
3330

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

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

    
3349
                ReleaseCOMObjects(_LMSymbol);
3350

    
3351
                InputSymbolAttribute(symbol, symbol.ATTRIBUTES);
3352
            }
3353
        }
3354

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

    
3379
                if (location.HasFlag(Location.Left))
3380
                    x = SPPIDLabelLocation.X1;
3381
                else if (location.HasFlag(Location.Right))
3382
                    x = SPPIDLabelLocation.X2;
3383

    
3384
                if (location.HasFlag(Location.Down))
3385
                    y = SPPIDLabelLocation.Y1;
3386
                else if (location.HasFlag(Location.Up))
3387
                    y = SPPIDLabelLocation.Y2;
3388
            }
3389
        }
3390

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

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

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

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

    
3428
                    if ((slopeType1 == SlopeType.VERTICAL && slopeType2 == SlopeType.HORIZONTAL) ||
3429
                        (slopeType2 == SlopeType.VERTICAL && slopeType1 == SlopeType.HORIZONTAL))
3430
                        symbols.Add(symbol);
3431
                }
3432
            }
3433

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

    
3444
            return symbols;
3445
        }
3446

    
3447
        private void SetPriorityLine()
3448
        {
3449
            document.LINES.Sort(SortLinePriority);
3450

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

    
3486
                            }
3487
                        }
3488
                    }
3489
                }
3490

    
3491
                return 0;
3492
            }
3493

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

    
3500
                List<Connector> connectorsB = b.CONNECTORS
3501
                    .Where(conn => conn.ConnectedObject != null && conn.ConnectedObject.GetType() == typeof(Symbol))
3502
                    .ToList();
3503

    
3504
                // 오름차순
3505
                return connectorsB.Count.CompareTo(connectorsA.Count);
3506
            }
3507

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

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

    
3522
                // 오름차순
3523
                return connectorsB.Count.CompareTo(connectorsA.Count);
3524
            }
3525

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

    
3535
                // 내림차순
3536
                return connectorsA.Count.CompareTo(connectorsB.Count);
3537
            }
3538

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

    
3545
                List<Connector> connectorsB = b.CONNECTORS
3546
                    .Where(conn => conn.ConnectedObject == null)
3547
                    .ToList();
3548

    
3549
                // 오름차순
3550
                return connectorsB.Count.CompareTo(connectorsA.Count);
3551
            }
3552
        }
3553

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

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

    
3569
                // 내림차순
3570
                return countA.CompareTo(countB);
3571
            }
3572
        }
3573

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

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

    
3601
                Thread.Sleep(milliseconds);
3602
            }
3603
        }
3604

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