프로젝트

일반

사용자정보

통계
| 브랜치(Branch): | 개정판:

hytos / DTI_PID / SPPIDConverter / AutoModeling.cs @ f1a7faf9

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

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

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

    
38
        public string DocumentLabelText { get; set; }
39

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

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

    
53
        private int CalcProgressCount()
54
        {
55
            int EquipCount = 0;
56
            int SymbolCount = 0;
57
            int LineCount = 0;
58
            int NoteCount = 0;
59
            int TextCount = 0;
60
            int EndBreakCount = 0;
61
            int LineNumberCount = 0;
62

    
63
            EquipCount = document.Equipments.Count;
64
            SymbolCount = document.SYMBOLS.Count;
65
            SymbolCount = SymbolCount * 3;
66

    
67
            foreach (LineNumber lineNumber in document.LINENUMBERS)
68
                foreach (LineRun run in lineNumber.RUNS)
69
                    foreach (var item in run.RUNITEMS)
70
                        if (item.GetType() == typeof(Line))
71
                            LineCount++;
72
            foreach (TrimLine trimLine in document.TRIMLINES)
73
                foreach (LineRun run in trimLine.RUNS)
74
                    foreach (var item in run.RUNITEMS)
75
                        if (item.GetType() == typeof(Line))
76
                            LineCount++;
77

    
78
            LineCount = LineCount * 2;
79
            NoteCount = document.NOTES.Count;
80
            TextCount = document.TEXTINFOS.Count;
81
            EndBreakCount = document.EndBreaks.Count;
82
            LineNumberCount = document.LINENUMBERS.Count;
83
            LineNumberCount = LineNumberCount * 2;
84

    
85
            return EquipCount + SymbolCount + LineCount + NoteCount + TextCount + EndBreakCount;
86
        }
87

    
88
        private void SetSystemEditingCommand(bool value)
89
        {
90
            foreach (var item in radApp.Commands)
91
            {
92
                if (item.Argument == "SystemEditingCmd.SystemEditing")
93
                {
94
                    if (item.Checked != value)
95
                    {
96
                        radApp.RunMacro("systemeditingcmd.dll");
97
                        break;
98
                    }
99

    
100
                }
101
            }
102
        }
103

    
104
        /// <summary>
105
        /// 도면 단위당 실행되는 메서드
106
        /// </summary>
107
        public void Run()
108
        {
109
            string drawingNumber = document.DrawingNumber;
110
            string drawingName = document.DrawingName;
111
            try
112
            {
113
                _placement = new Placement();
114
                dataSource = _placement.PIDDataSource;
115

    
116
                CreateDocument(ref drawingNumber, ref drawingName);
117

    
118
                if (DocumentCoordinateCorrection())
119
                {
120
                    int AllCount = CalcProgressCount();
121
                    Log.Write("Start Modeling");
122
                    SplashScreenManager.ShowForm(typeof(SPPIDSplashScreen), true, true);
123
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetParent, (IntPtr)radApp.HWnd);
124
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetAllStep, AllCount);
125
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetDocumentName, DocumentLabelText);
126

    
127
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Priority Symbol Modeling");
128
                    prioritySymbols = GetPrioritySymbol();
129
                    foreach (var item in prioritySymbols)
130
                    {
131
                        try
132
                        {
133
                            SymbolModelingBySymbol(item);
134
                        }
135
                        catch (Exception ex)
136
                        {
137
                            Log.Write("Error in SymbolModelingByPriority");
138
                            Log.Write("UID : " + item.UID);
139
                            Log.Write(ex.Message);
140
                            Log.Write(ex.StackTrace);
141
                        }
142
                    }
143

    
144
                    // Equipment Modeling
145
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Equipments Modeling");
146
                    foreach (Equipment equipment in document.Equipments)
147
                    {
148
                        try
149
                        {
150
                            EquipmentModeling(equipment);
151
                        }
152
                        catch (Exception ex)
153
                        {
154
                            Log.Write("Error in EquipmentModeling");
155
                            Log.Write("UID : " + equipment.UID);
156
                            Log.Write(ex.Message);
157
                            Log.Write(ex.StackTrace);
158
                        }
159
                    }
160

    
161
                    // LineRun Symbol Modeling
162
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Symbols Modeling");
163
                    foreach (LineNumber lineNumber in document.LINENUMBERS)
164
                        try
165
                        {
166
                            foreach (LineRun run in lineNumber.RUNS)
167
                                SymbolModelingByRun(run);
168
                        }
169
                        catch (Exception ex)
170
                        {
171
                            Log.Write("Error in SymbolModelingByRun");
172
                            Log.Write("UID : " + lineNumber.UID);
173
                            Log.Write(ex.Message);
174
                            Log.Write(ex.StackTrace);
175
                        }
176
                    // TrimLineRun Symbol Modeling
177
                    foreach (TrimLine trimLine in document.TRIMLINES)
178
                        try
179
                        {
180
                            foreach (LineRun run in trimLine.RUNS)
181
                                SymbolModelingByRun(run);
182
                        }
183
                        catch (Exception ex)
184
                        {
185
                            Log.Write("Error in SymbolModelingByRun");
186
                            Log.Write("UID : " + trimLine.UID);
187
                            Log.Write(ex.Message);
188
                            Log.Write(ex.StackTrace);
189
                        }
190

    
191

    
192
                    // LineRun Line Modeling
193
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Lines Modeling");
194
                    foreach (LineNumber lineNumber in document.LINENUMBERS)
195
                        try
196
                        {
197
                            foreach (LineRun run in lineNumber.RUNS)
198
                                LineModelingByRun(run);
199
                        }
200
                        catch (Exception ex)
201
                        {
202
                            Log.Write("Error in LineModelingByRun");
203
                            Log.Write("UID : " + lineNumber.UID);
204
                            Log.Write(ex.Message);
205
                            Log.Write(ex.StackTrace);
206
                        }
207
                    // TrimLineRun Line Modeling
208
                    foreach (TrimLine trimLine in document.TRIMLINES)
209
                        try
210
                        {
211
                            foreach (LineRun run in trimLine.RUNS)
212
                                LineModelingByRun(run);
213
                        }
214
                        catch (Exception ex)
215
                        {
216
                            Log.Write("Error in LineModelingByRun");
217
                            Log.Write("UID : " + trimLine.UID);
218
                            Log.Write(ex.Message);
219
                            Log.Write(ex.StackTrace);
220
                        }
221
             
222

    
223
                    // Branch Line Modeling
224
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Branch Lines Modeling");
225
                    foreach (var item in BranchLines)
226
                        try
227
                        {
228
                            BranchLineModeling(item);
229
                        }
230
                        catch (Exception ex)
231
                        {
232
                            Log.Write("Error in BranchLineModeling");
233
                            if (item.Item2 != null)
234
                                Log.Write("UID : " + item.Item2.UID);
235
                            if (item.Item3 != null)
236
                                Log.Write("UID : " + item.Item3.UID);
237
                            Log.Write(ex.Message);
238
                            Log.Write(ex.StackTrace);
239
                        }
240
                    
241

    
242
                    // EndBreak Modeling
243
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "EndBreaks Modeling");
244
                    foreach (var item in document.EndBreaks)
245
                        try
246
                        {
247
                            EndBreakModeling(item);
248
                        }
249
                        catch (Exception ex)
250
                        {
251
                            Log.Write("Error in EndBreakModeling");
252
                            Log.Write("UID : " + item.UID);
253
                            Log.Write(ex.Message);
254
                            Log.Write(ex.StackTrace);
255
                        }
256

    
257

    
258
                    // SpecBreak Modeling
259
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "SpecBreaks Modeling");
260
                    foreach (var item in document.SpecBreaks)
261
                        try
262
                        {
263
                            SpecBreakModeling(item);
264
                        }
265
                        catch (Exception ex)
266
                        {
267
                            Log.Write("Error in SpecBreakModeling");
268
                            Log.Write("UID : " + item.UID);
269
                            Log.Write(ex.Message);
270
                            Log.Write(ex.StackTrace);
271
                        }
272

    
273

    
274
                    // LineNumber Modeling
275
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "LineNumbers Modeling");
276
                    foreach (var item in document.LINENUMBERS)
277
                        try
278
                        {
279
                            LineNumberModeling(item);
280
                        }
281
                        catch (Exception ex)
282
                        {
283
                            Log.Write("Error in LineNumberModeling");
284
                            Log.Write("UID : " + item.UID);
285
                            Log.Write(ex.Message);
286
                            Log.Write(ex.StackTrace);
287
                        }
288

    
289

    
290
                    // FlowMark Modeling
291
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Flow Mark Modeling");
292
                    foreach (var item in document.LINES)
293
                        try
294
                        {
295
                            FlowMarkModeling(item);
296
                        }
297
                        catch (Exception ex)
298
                        {
299
                            Log.Write("Error in FlowMarkModeling");
300
                            Log.Write("UID : " + item.UID);
301
                            Log.Write(ex.Message);
302
                            Log.Write(ex.StackTrace);
303
                        }
304

    
305

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

    
321

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

    
337
                    // Text Modeling
338
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Texts Modeling");
339
                    foreach (var item in document.TEXTINFOS)
340
                        try
341
                        {
342
                            TextModeling(item);
343
                        }
344
                        catch (Exception ex)
345
                        {
346
                            Log.Write("Error in TextModeling");
347
                            Log.Write("UID : " + item.UID);
348
                            Log.Write(ex.Message);
349
                            Log.Write(ex.StackTrace);
350
                        }
351

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

    
367
                    // Input Symbol Attribute
368
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Set Symbols Attribute");
369
                    foreach (var item in document.SYMBOLS)
370
                        try
371
                        {
372
                            InputSymbolAttribute(item, item.ATTRIBUTES);
373
                        }
374
                        catch (Exception ex)
375
                        {
376
                            Log.Write("Error in InputSymbolAttribute");
377
                            Log.Write("UID : " + item.UID);
378
                            Log.Write(ex.Message);
379
                            Log.Write(ex.StackTrace);
380
                        }
381

    
382
                    // Input SpecBreak Attribute
383
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Set Symbols Attribute");
384
                    foreach (var item in document.SpecBreaks)
385
                        try
386
                        {
387
                            InputSpecBreakAttribute(item);
388
                        }
389
                        catch (Exception ex)
390
                        {
391
                            Log.Write("Error in InputSpecBreakAttribute");
392
                            Log.Write("UID : " + item.UID);
393
                            Log.Write(ex.Message);
394
                            Log.Write(ex.StackTrace);
395
                        }
396

    
397
                    // Label Symbol Modeling
398
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Labels Modeling");
399
                    foreach (var item in document.SYMBOLS)
400
                        try
401
                        {
402
                            LabelSymbolModeling(item);
403
                        }
404
                        catch (Exception ex)
405
                        {
406
                            Log.Write("Error in LabelSymbolModeling");
407
                            Log.Write("UID : " + item.UID);
408
                            Log.Write(ex.Message);
409
                            Log.Write(ex.StackTrace);
410
                        }
411

    
412
                    // LineRun Line Join
413
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Join LineRuns");
414
                    foreach (LineNumber lineNumber in document.LINENUMBERS)
415
                        try
416
                        {
417
                            foreach (LineRun run in lineNumber.RUNS)
418
                                JoinRunLine(run);
419
                        }
420
                        catch (Exception ex)
421
                        {
422
                            Log.Write("Error in JoinRunLine");
423
                            Log.Write("UID : " + lineNumber.UID);
424
                            Log.Write(ex.Message);
425
                            Log.Write(ex.StackTrace);
426
                        }
427

    
428
                    // TrimLineRun Line Join
429
                    foreach (TrimLine trimLine in document.TRIMLINES)
430
                        try
431
                        {
432
                            foreach (LineRun run in trimLine.RUNS)
433
                                JoinRunLine(run);
434
                        }
435
                        catch (Exception ex)
436
                        {
437
                            Log.Write("Error in JoinRunLine");
438
                            Log.Write("UID : " + trimLine.UID);
439
                            Log.Write(ex.Message);
440
                            Log.Write(ex.StackTrace);
441
                        }
442

    
443
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetProgress, AllCount);
444
                }
445
            }
446
            catch (Exception ex)
447
            {
448
                SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.ClearParent, null);
449
                SplashScreenManager.CloseForm(false);
450
                System.Windows.Forms.MessageBox.Show(ex.Message + "\r\n" + ex.StackTrace);
451
            }
452
            finally
453
            {
454
                Log.Write("End Modeling");
455
                application.ActiveWindow.Fit();
456

    
457
                if (radApp.ActiveDocument != null)
458
                {
459
                    //radApp.ActiveDocument.Save();
460
                    //radApp.ActiveDocument.SaveOnClose = false;
461
                    //radApp.ActiveDocument.Close(false);
462

    
463
                    ReleaseCOMObjects(newDrawing);
464
                }
465

    
466
                ReleaseCOMObjects(dataSource);
467
                ReleaseCOMObjects(_placement);
468

    
469
                Project_DB.InsertDrawingInfo(document.PATH, drawingNumber, drawingName, document);
470
                if (SplashScreenManager.Default.IsSplashFormVisible)
471
                {
472
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.ClearParent, null);
473
                    SplashScreenManager.CloseForm(false);
474
                    Log.Write("\r\n");
475
                }
476
            }
477
        }
478

    
479
        /// <summary>
480
        /// 도면 생성 메서드
481
        /// </summary>
482
        private void CreateDocument(ref string drawingNumber, ref string drawingName)
483
        {
484
            Log.Write("------------------ Start create document ------------------");
485
            GetDrawingNameAndNumber(ref drawingName, ref drawingNumber);
486
            Log.Write("Drawing name : " + drawingName);
487
            Log.Write("Drawing number : " + drawingNumber);
488
            newDrawing = application.Drawings.Add(document.Unit, document.Template, drawingNumber, drawingName);
489
            document.SPPID_DrawingNumber = drawingNumber;
490
            document.SPPID_DrawingName = drawingName;
491
            application.ActiveWindow.Fit();
492
            Thread.Sleep(1000);
493
            application.ActiveWindow.Zoom = 2000;
494
            Thread.Sleep(2000);
495
        }
496

    
497
        /// <summary>
498
        /// DrawingName, DrawingNumber를 확인하여 중복이 있으면 _1을 붙이고 +1씩 한다.
499
        /// </summary>
500
        /// <param name="drawingName"></param>
501
        /// <param name="drawingNumber"></param>
502
        private void GetDrawingNameAndNumber(ref string drawingName, ref string drawingNumber)
503
        {
504
            LMDrawings drawings = new LMDrawings();
505
            drawings.Collect(dataSource);
506

    
507
            List<string> drawingNameList = new List<string>();
508
            List<string> drawingNumberList = new List<string>();
509

    
510
            foreach (LMDrawing item in drawings)
511
            {
512
                drawingNameList.Add(item.Attributes["Name"].get_Value().ToString());
513
                drawingNumberList.Add(item.Attributes["DrawingNumber"].get_Value().ToString());
514
            }
515

    
516
            int nameLength = drawingName.Length;
517
            while (drawingNameList.Contains(drawingName))
518
            {
519
                if (nameLength == drawingName.Length)
520
                    drawingName += "-1";
521
                else
522
                {
523
                    int index = Convert.ToInt32(drawingName.Remove(0, nameLength + 1));
524
                    drawingName = drawingName.Substring(0, nameLength + 1);
525
                    drawingName += ++index;
526
                }
527
            }
528

    
529
            int numberLength = drawingNumber.Length;
530
            while (drawingNameList.Contains(drawingNumber))
531
            {
532
                if (numberLength == drawingNumber.Length)
533
                    drawingNumber += "-1";
534
                else
535
                {
536
                    int index = Convert.ToInt32(drawingNumber.Remove(0, numberLength + 1));
537
                    drawingNumber = drawingNumber.Substring(0, numberLength + 1);
538
                    drawingNumber += ++index;
539
                }
540
            }
541

    
542
            ReleaseCOMObjects(drawings);
543
        }
544

    
545
        /// <summary>
546
        /// 도면 크기 구하는 메서드
547
        /// </summary>
548
        /// <returns></returns>
549
        private bool DocumentCoordinateCorrection()
550
        {
551
            if (Settings.Default.DrawingX != 0 && Settings.Default.DrawingY != 0)
552
            {
553
                Log.Write("Setting Drawing X, Drawing Y");
554
                document.SetSPPIDLocation(Settings.Default.DrawingX, Settings.Default.DrawingY);
555
                Log.Write("Start coordinate correction");
556
                document.CoordinateCorrection();
557
                return true;
558
            }
559
            else
560
            {
561
                Log.Write("Need Drawing X, Y");
562
                return false;
563
            }
564
        }
565

    
566
        /// <summary>
567
        /// 라인을 Run 단위로 모델링하는 진입 메서드
568
        /// </summary>
569
        /// <param name="run"></param>
570
        private void LineModelingByRun(LineRun run)
571
        {
572
            Line prevLine = null;
573
            List<Line> lines = new List<Line>();
574
            foreach (var item in run.RUNITEMS)
575
            {
576
                // Line일 경우
577
                if (item.GetType() == typeof(Line))
578
                {
579
                    Line line = item as Line;
580
                    if (prevLine == null)
581
                        lines.Add(line);
582
                    else if (prevLine != null)
583
                    {
584
                        if (prevLine.SPPID.MAPPINGNAME == line.SPPID.MAPPINGNAME)
585
                            lines.Add(line);
586
                        else
587
                        {
588
                            if (lines.Count > 0)
589
                            {
590
                                LineModeling(lines);
591
                                lines.Clear();
592
                            }
593
                            lines.Add(line);
594
                        }
595
                    }
596

    
597
                    prevLine = line;
598

    
599
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetProgress, ++CurrentCount);
600
                }
601
                // Symbol 일 경우
602
                else if (item.GetType() == typeof(Symbol))
603
                {
604
                    if (lines.Count > 0)
605
                    {
606
                        LineModeling(lines);
607
                        lines.Clear();
608
                    }
609
                }
610
            }
611

    
612
            if (lines.Count > 0)
613
                LineModeling(lines);
614
        }
615

    
616
        /// <summary>
617
        /// 심볼을 Run 단위로 모델링하는 진입 메서드
618
        /// </summary>
619
        /// <param name="run"></param>
620
        private void SymbolModelingByRun(LineRun run)
621
        {
622
            // 양끝 Symbol 검사 후 Line이 나올때까지만 Symbol Modeling
623
            if (run.RUNITEMS.Count > 0)
624
            {
625
                if (run.RUNITEMS[0].GetType() == typeof(Symbol))
626
                    SymbolModelingByRunStart(run.RUNITEMS[0] as Symbol, run);
627

    
628
                if (run.RUNITEMS[run.RUNITEMS.Count - 1].GetType() == typeof(Symbol))
629
                    SymbolModelingByRunEnd(run.RUNITEMS[run.RUNITEMS.Count - 1] as Symbol, run);
630
            }
631

    
632
            Symbol targetSymbol = null;
633
            foreach (var item in run.RUNITEMS)
634
            {
635
                if (item.GetType() == typeof(Symbol))
636
                {
637
                    Symbol symbol = item as Symbol;
638
                    SymbolModeling(symbol, targetSymbol);
639
                    targetSymbol = symbol;
640
                }
641
                else
642
                {
643
                    targetSymbol = null;
644
                }
645
            }
646
        }
647

    
648
        /// <summary>
649
        /// Run에 있는 심볼을 모델링하는데 기준이 Run의 시작점
650
        /// </summary>
651
        /// <param name="symbol"></param>
652
        /// <param name="run"></param>
653
        private void SymbolModelingByRunStart(Symbol symbol, LineRun run)
654
        {
655
            foreach (var connector in symbol.CONNECTORS)
656
            {
657
                object targetItem = SPPIDUtil.FindObjectByUID(document, connector.CONNECTEDITEM);
658
                if (targetItem != null &&
659
                    (targetItem.GetType() == typeof(Symbol) || targetItem.GetType() == typeof(Equipment)) &&
660
                    !IsSameLineRun(symbol, targetItem))
661
                {
662
                    SymbolModeling(symbol, targetItem as Symbol);
663
                    for (int i = 1; i < run.RUNITEMS.Count; i++)
664
                    {
665
                        object item = run.RUNITEMS[i];
666
                        if (item.GetType() == typeof(Symbol))
667
                            SymbolModeling(item as Symbol, run.RUNITEMS[i - 1] as Symbol);
668
                        else
669
                            break;
670
                    }
671
                    break;
672
                }
673
            }
674

    
675

    
676
        }
677

    
678
        /// <summary>
679
        /// Run에 있는 심볼을 모델링하는데 기준이 Run의 끝점
680
        /// </summary>
681
        /// <param name="symbol"></param>
682
        /// <param name="run"></param>
683
        private void SymbolModelingByRunEnd(Symbol symbol, LineRun run)
684
        {
685
            foreach (var connector in symbol.CONNECTORS)
686
            {
687
                object targetItem = SPPIDUtil.FindObjectByUID(document, connector.CONNECTEDITEM);
688
                if (targetItem != null &&
689
                    (targetItem.GetType() == typeof(Symbol) || targetItem.GetType() == typeof(Equipment)) &&
690
                    !IsSameLineRun(symbol, targetItem))
691
                {
692
                    SymbolModeling(symbol, targetItem as Symbol);
693
                    for (int i = run.RUNITEMS.Count - 2; i >= 0; i--)
694
                    {
695
                        object item = run.RUNITEMS[i];
696
                        if (item.GetType() == typeof(Symbol))
697
                            SymbolModeling(item as Symbol, run.RUNITEMS[i + 1] as Symbol);
698
                        else
699
                            break;
700
                    }
701
                    break;
702
                }
703
            }
704
        }
705

    
706
        /// <summary>
707
        /// 심볼을 실제로 Modeling 메서드
708
        /// </summary>
709
        /// <param name="symbol"></param>
710
        /// <param name="targetSymbol"></param>
711
        /// <param name="prevSymbol"></param>
712
        private void SymbolModeling(Symbol symbol, Symbol targetSymbol)
713
        {
714
            // OWNERSYMBOL Attribute, 값을 가지고 있을 경우
715
            BaseModel.Attribute itemAttribute = symbol.ATTRIBUTES.Find(attr => attr.ATTRIBUTE == "OWNERSYMBOL");
716
            if (itemAttribute != null && (string.IsNullOrEmpty(itemAttribute.VALUE) || itemAttribute.VALUE != "None"))
717
                return;
718
            // 이미 모델링 됐을 경우
719
            else if (!string.IsNullOrEmpty(symbol.SPPID.RepresentationId))
720
                return;
721

    
722
            LMSymbol _LMSymbol = null;
723

    
724
            string mappingPath = symbol.SPPID.MAPPINGNAME;
725
            double x = symbol.SPPID.ORIGINAL_X;
726
            double y = symbol.SPPID.ORIGINAL_Y;
727
            int mirror = 0;
728
            double angle = symbol.ANGLE;
729

    
730
            // OPC 일경우 180도 일때 Mirror
731
            if (mappingPath.Contains("Piping OPC's") && angle == Math.PI)
732
                mirror = 1;
733

    
734
            // Mirror 계산
735
            if (symbol.FLIP == 1)
736
            {
737
                mirror = 1;
738
                angle += Math.PI;
739
            }
740

    
741
            if (targetSymbol != null && !string.IsNullOrEmpty(targetSymbol.SPPID.RepresentationId))
742
            {
743
                LMSymbol _TargetItem = dataSource.GetSymbol(targetSymbol.SPPID.RepresentationId);
744
                Connector connector = SPPIDUtil.FindSymbolConnectorByUID(document, symbol.UID, targetSymbol);
745
                if (connector != null)
746
                    GetTargetSymbolConnectorPoint(connector, targetSymbol, ref x, ref y);
747

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

    
750
                if (_LMSymbol != null && _TargetItem != null)
751
                {
752
                    symbol.SPPID.RepresentationId = _LMSymbol.AsLMRepresentation().Id;
753
                    LMConnector reModelingConnector = FindBreakLineTarget(symbol, targetSymbol);
754

    
755
                    if (reModelingConnector != null)
756
                        ReModelingLMConnector(reModelingConnector);
757
                }
758

    
759
                ReleaseCOMObjects(_TargetItem);
760
            }
761
            else
762
                _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle);
763

    
764
            if (_LMSymbol != null)
765
            {
766
                _LMSymbol.Commit();
767
                symbol.SPPID.RepresentationId = _LMSymbol.AsLMRepresentation().Id;
768
                symbol.SPPID.ModelItemID = _LMSymbol.ModelItemID;
769
                symbol.SPPID.GraphicOID = _LMSymbol.get_GraphicOID();
770

    
771
                foreach (var item in symbol.ChildSymbols)
772
                    CreateChildSymbol(item, _LMSymbol);
773

    
774
                symbol.SPPID.SPPID_X = _LMSymbol.get_XCoordinate();
775
                symbol.SPPID.SPPID_Y = _LMSymbol.get_YCoordinate();
776

    
777
                double[] range = null;
778
                GetSPPIDSymbolRange(symbol, ref range);
779
                symbol.SPPID.SPPID_Min_X = range[0];
780
                symbol.SPPID.SPPID_Min_Y = range[1];
781
                symbol.SPPID.SPPID_Max_X = range[2];
782
                symbol.SPPID.SPPID_Max_Y = range[3];
783

    
784
                foreach (var item in symbol.SPPID.CorrectionX_GroupSymbols)
785
                    item.SPPID.ORIGINAL_X = symbol.SPPID.SPPID_X;
786
                foreach (var item in symbol.SPPID.CorrectionY_GroupSymbols)
787
                    item.SPPID.ORIGINAL_Y = symbol.SPPID.SPPID_Y;
788

    
789
                ReleaseCOMObjects(_LMSymbol);
790
            }
791

    
792
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetProgress, ++CurrentCount);
793
        }
794

    
795
        private void RemoveSymbol(Symbol symbol)
796
        {
797
            if (!string.IsNullOrEmpty(symbol.SPPID.RepresentationId))
798
            {
799
                LMSymbol _LMSymbol = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
800
                if (_LMSymbol != null)
801
                {
802
                    _placement.PIDRemovePlacement(_LMSymbol.AsLMRepresentation());
803
                    ReleaseCOMObjects(_LMSymbol);
804
                }
805
            }
806

    
807
            symbol.SPPID.RepresentationId = string.Empty;
808
            symbol.SPPID.ModelItemID = string.Empty;
809
            symbol.SPPID.SPPID_X = double.NaN;
810
            symbol.SPPID.SPPID_Y = double.NaN;
811
            symbol.SPPID.SPPID_Min_X = double.NaN;
812
            symbol.SPPID.SPPID_Min_Y = double.NaN;
813
            symbol.SPPID.SPPID_Max_X = double.NaN;
814
            symbol.SPPID.SPPID_Max_Y = double.NaN;
815
        }
816

    
817
        private void RemoveSymbol(List<Symbol> symbols)
818
        {
819
            foreach (var symbol in symbols)
820
            {
821
                if (!string.IsNullOrEmpty(symbol.SPPID.RepresentationId))
822
                {
823
                    LMSymbol _LMSymbol = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
824
                    if (_LMSymbol != null)
825
                    {
826
                        _placement.PIDRemovePlacement(_LMSymbol.AsLMRepresentation());
827
                        ReleaseCOMObjects(_LMSymbol);
828
                    }
829
                }
830

    
831
                symbol.SPPID.RepresentationId = string.Empty;
832
                symbol.SPPID.ModelItemID = string.Empty;
833
                symbol.SPPID.SPPID_X = double.NaN;
834
                symbol.SPPID.SPPID_Y = double.NaN;
835
                symbol.SPPID.SPPID_Min_X = double.NaN;
836
                symbol.SPPID.SPPID_Min_Y = double.NaN;
837
                symbol.SPPID.SPPID_Max_X = double.NaN;
838
                symbol.SPPID.SPPID_Max_Y = double.NaN;
839
            }
840
        }
841

    
842
        /// <summary>
843
        /// ID2의 Symbol Width와 Height를 비교해서 상대적인 SPPID Connector좌표를 가져온다.
844
        /// </summary>
845
        /// <param name="targetConnector"></param>
846
        /// <param name="targetSymbol"></param>
847
        /// <param name="x"></param>
848
        /// <param name="y"></param>
849
        private void GetTargetSymbolConnectorPoint(Connector targetConnector, Symbol targetSymbol, ref double x, ref double y)
850
        {
851
            LMSymbol _TargetItem = dataSource.GetSymbol(targetSymbol.SPPID.RepresentationId);
852

    
853
            double[] range = null;
854
            List<double[]> points = new List<double[]>();
855
            GetSPPIDSymbolRangeAndConnectionPoints(targetSymbol, ref range, points);
856
            double x1 = range[0];
857
            double y1 = range[1];
858
            double x2 = range[2];
859
            double y2 = range[3];
860

    
861
            // Origin 기준 Connector의 위치차이
862
            double sceneX = 0;
863
            double sceneY = 0;
864
            SPPIDUtil.ConvertPointBystring(targetConnector.SCENECONNECTPOINT, ref sceneX, ref sceneY);
865
            double originX = 0;
866
            double originY = 0;
867
            SPPIDUtil.ConvertPointBystring(targetSymbol.ORIGINALPOINT, ref originX, ref originY);
868
            double gapX = originX - sceneX;
869
            double gapY = originY - sceneY;
870

    
871
            // SPPID Symbol과 ID2 심볼의 크기 차이
872
            double sizeWidth = 0;
873
            double sizeHeight = 0;
874
            SPPIDUtil.ConvertPointBystring(targetSymbol.SIZE, ref sizeWidth, ref sizeHeight);
875
            double percentX = (x2 - x1) / sizeWidth;
876
            double percentY = (y2 - y1) / sizeHeight;
877

    
878
            double SPPIDgapX = gapX * percentX;
879
            double SPPIDgapY = gapY * percentY;
880

    
881
            double[] SPPIDOriginPoint = new double[] { _TargetItem.get_XCoordinate() - SPPIDgapX, _TargetItem.get_YCoordinate() + SPPIDgapY };
882
            double distance = double.MaxValue;
883
            double[] resultPoint;
884
            foreach (var point in points)
885
            {
886
                double result = SPPIDUtil.CalcPointToPointdDistance(point[0], point[1], SPPIDOriginPoint[0], SPPIDOriginPoint[1]);
887
                if (distance > result)
888
                {
889
                    distance = result;
890
                    resultPoint = point;
891
                    x = point[0];
892
                    y = point[1];
893
                }
894
            }
895

    
896
            ReleaseCOMObjects(_TargetItem);
897
        }
898

    
899
        /// <summary>
900
        /// SPPID Symbol의 Range를 구한다.
901
        /// </summary>
902
        /// <param name="symbol"></param>
903
        /// <param name="range"></param>
904
        private void GetSPPIDSymbolRangeAndConnectionPoints(Symbol symbol, ref double[] range, List<double[]> points)
905
        {
906
            LMSymbol _TargetItem = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
907
            Ingr.RAD2D.Symbol2d symbol2d = radApp.ActiveDocument.ActiveSheet.DrawingObjects[_TargetItem.get_GraphicOID().ToString()];
908
            double x1 = 0;
909
            double y1 = 0;
910
            double x2 = 0;
911
            double y2 = 0;
912
            symbol2d.Range(out x1, out y1, out x2, out y2);
913
            range = new double[] { x1, y1, x2, y2 };
914

    
915
            for (int i = 1; i < int.MaxValue; i++)
916
            {
917
                double connX = 0;
918
                double connY = 0;
919
                if (_placement.PIDConnectPointLocation(_TargetItem, i, ref connX, ref connY))
920
                    points.Add(new double[] { connX, connY });
921
                else
922
                    break;
923
            }
924

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

    
928
            ReleaseCOMObjects(_TargetItem);
929
        }
930

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

    
942
            foreach (var childSymbol in symbol.ChildSymbols)
943
                GetSPPIDChildSymbolRange(childSymbol, ref range);
944

    
945
            ReleaseCOMObjects(_TargetItem);
946
        }
947

    
948
        private void GetSPPIDSymbolRange(List<Symbol> symbols, ref double[] range)
949
        {
950
            double[] tempRange = new double[] { double.MaxValue, double.MaxValue, double.MinValue, double.MinValue };
951
            foreach (var symbol in symbols)
952
            {
953
                LMSymbol _TargetItem = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
954
                Ingr.RAD2D.Symbol2d symbol2d = radApp.ActiveDocument.ActiveSheet.DrawingObjects[_TargetItem.get_GraphicOID().ToString()];
955
                double x1 = 0;
956
                double y1 = 0;
957
                double x2 = 0;
958
                double y2 = 0;
959
                symbol2d.Range(out x1, out y1, out x2, out y2);
960

    
961
                tempRange[0] = Math.Min(tempRange[0], x1);
962
                tempRange[1] = Math.Min(tempRange[1], y1);
963
                tempRange[2] = Math.Max(tempRange[2], x2);
964
                tempRange[3] = Math.Max(tempRange[3], y2);
965

    
966
                foreach (var childSymbol in symbol.ChildSymbols)
967
                    GetSPPIDChildSymbolRange(childSymbol, ref tempRange);
968

    
969
                ReleaseCOMObjects(_TargetItem);
970
            }
971

    
972
            range = tempRange;
973
        }
974

    
975
        /// <summary>
976
        /// Child Modeling 된 Symbol의 Range를 구한다.
977
        /// </summary>
978
        /// <param name="childSymbol"></param>
979
        /// <param name="range"></param>
980
        private void GetSPPIDChildSymbolRange(ChildSymbol childSymbol, ref double[] range, List<double[]> points)
981
        {
982
            LMSymbol _ChildSymbol = dataSource.GetSymbol(childSymbol.SPPID.RepresentationId);
983
            Ingr.RAD2D.Symbol2d symbol2d = radApp.ActiveDocument.ActiveSheet.DrawingObjects[_ChildSymbol.get_GraphicOID().ToString()];
984
            double x1 = 0;
985
            double y1 = 0;
986
            double x2 = 0;
987
            double y2 = 0;
988
            symbol2d.Range(out x1, out y1, out x2, out y2);
989
            range[0] = Math.Min(range[0], x1);
990
            range[1] = Math.Min(range[1], y1);
991
            range[2] = Math.Max(range[2], x2);
992
            range[3] = Math.Max(range[3], y2);
993

    
994
            for (int i = 1; i < int.MaxValue; i++)
995
            {
996
                double connX = 0;
997
                double connY = 0;
998
                if (_placement.PIDConnectPointLocation(_ChildSymbol, i, ref connX, ref connY))
999
                    points.Add(new double[] { connX, connY });
1000
                else
1001
                    break;
1002
            }
1003

    
1004
            foreach (var loopChildSymbol in childSymbol.ChildSymbols)
1005
                GetSPPIDChildSymbolRange(loopChildSymbol, ref range, points);
1006

    
1007
            ReleaseCOMObjects(_ChildSymbol);
1008
        }
1009

    
1010
        private void GetSPPIDChildSymbolRange(ChildSymbol childSymbol, ref double[] range)
1011
        {
1012
            LMSymbol _ChildSymbol = dataSource.GetSymbol(childSymbol.SPPID.RepresentationId);
1013
            Ingr.RAD2D.Symbol2d symbol2d = radApp.ActiveDocument.ActiveSheet.DrawingObjects[_ChildSymbol.get_GraphicOID().ToString()];
1014
            double x1 = 0;
1015
            double y1 = 0;
1016
            double x2 = 0;
1017
            double y2 = 0;
1018
            symbol2d.Range(out x1, out y1, out x2, out y2);
1019
            range[0] = Math.Min(range[0], x1);
1020
            range[1] = Math.Min(range[1], y1);
1021
            range[2] = Math.Max(range[2], x2);
1022
            range[3] = Math.Max(range[3], y2);
1023

    
1024
            foreach (var loopChildSymbol in childSymbol.ChildSymbols)
1025
                GetSPPIDChildSymbolRange(loopChildSymbol, ref range);
1026

    
1027
            ReleaseCOMObjects(_ChildSymbol);
1028
        }
1029

    
1030
        /// <summary>
1031
        /// Label Symbol Modeling
1032
        /// </summary>
1033
        /// <param name="symbol"></param>
1034
        private void LabelSymbolModeling(Symbol symbol)
1035
        {
1036
            if (string.IsNullOrEmpty(symbol.SPPID.RepresentationId))
1037
            {
1038
                BaseModel.Attribute itemAttribute = symbol.ATTRIBUTES.Find(x => x.ATTRIBUTE == "OWNERSYMBOL");
1039
                if (itemAttribute == null || string.IsNullOrEmpty(itemAttribute.VALUE) || itemAttribute.VALUE == "None")
1040
                    return;
1041
                Array points = new double[] { 0, symbol.SPPID.ORIGINAL_X, symbol.SPPID.ORIGINAL_Y };
1042

    
1043
                string symbolUID = itemAttribute.VALUE;
1044
                object targetItem = SPPIDUtil.FindObjectByUID(document, symbolUID);
1045
                if (targetItem != null &&
1046
                    (targetItem.GetType() == typeof(Symbol) ||
1047
                    targetItem.GetType() == typeof(Equipment)))
1048
                {
1049
                    // Object 아이템이 Symbol일 경우 Equipment일 경우 
1050
                    string sRep = null;
1051
                    if (targetItem.GetType() == typeof(Symbol))
1052
                        sRep = ((Symbol)targetItem).SPPID.RepresentationId;
1053
                    else if (targetItem.GetType() == typeof(Equipment))
1054
                        sRep = ((Equipment)targetItem).SPPID.RepresentationId;
1055
                    if (!string.IsNullOrEmpty(sRep))
1056
                    {
1057
                        // LEADER Line 검사
1058
                        bool leaderLine = false;
1059
                        SymbolMapping symbolMapping = document.SymbolMappings.Find(x => x.UID == symbol.DBUID);
1060
                        if (symbolMapping != null)
1061
                            leaderLine = symbolMapping.LEADERLINE;
1062

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

    
1067
                        //Leader 선 센터로
1068
                        if (_LMLabelPresist != null)
1069
                        {
1070
                            // Target Item에 Label의 Attribute Input
1071
                            InputSymbolAttribute(targetItem, symbol.ATTRIBUTES);
1072

    
1073
                            string OID = _LMLabelPresist.get_GraphicOID();
1074
                            DependencyObject dependency = radApp.ActiveDocument.ActiveSheet.DrawingObjects[OID] as DependencyObject;
1075
                            if (dependency != null)
1076
                            {
1077
                                bool result = false;
1078
                                foreach (var attributes in dependency.AttributeSets)
1079
                                {
1080
                                    foreach (var attribute in attributes)
1081
                                    {
1082
                                        string name = attribute.Name;
1083
                                        string value = attribute.GetValue().ToString();
1084
                                        if (name == "DrawingItemType" && value == "LabelPersist")
1085
                                        {
1086
                                            foreach (DrawingObjectBase drawingObject in dependency.DrawingObjects)
1087
                                            {
1088
                                                if (drawingObject.Type == Ingr.RAD2D.ObjectType.igLineString2d)
1089
                                                {
1090
                                                    Ingr.RAD2D.LineString2d lineString2D = drawingObject as Ingr.RAD2D.LineString2d;
1091
                                                    double prevX = _TargetItem.get_XCoordinate();
1092
                                                    double prevY = _TargetItem.get_YCoordinate();
1093
                                                    lineString2D.InsertVertex(lineString2D.VertexCount, prevX, prevY);
1094
                                                    lineString2D.RemoveVertex(lineString2D.VertexCount);
1095
                                                    result = true;
1096
                                                    break;
1097
                                                }
1098
                                            }
1099
                                        }
1100

    
1101
                                        if (result)
1102
                                            break;
1103
                                    }
1104

    
1105
                                    if (result)
1106
                                        break;
1107
                                }
1108
                            }
1109

    
1110
                            _LMLabelPresist.Commit();
1111
                            ReleaseCOMObjects(_LMLabelPresist);
1112
                        }
1113

    
1114
                        ReleaseCOMObjects(_TargetItem);
1115
                    }
1116
                }
1117
                else if (targetItem != null && targetItem.GetType() == typeof(Line))
1118
                {
1119
                    Line targetLine = targetItem as Line;
1120
                    Dictionary<LMConnector, List<double[]>> connectorVertices = GetPipeRunVertices(targetLine.SPPID.ModelItemId);
1121
                    LMConnector connectedLMConnector = FindTargetLMConnectorForLabel(connectorVertices, symbol.SPPID.ORIGINAL_X, symbol.SPPID.ORIGINAL_Y);
1122
                    if (connectedLMConnector != null)
1123
                    {
1124
                        // LEADER Line 검사
1125
                        bool leaderLine = false;
1126
                        SymbolMapping symbolMapping = document.SymbolMappings.Find(x => x.UID == symbol.DBUID);
1127
                        if (symbolMapping != null)
1128
                            leaderLine = symbolMapping.LEADERLINE;
1129

    
1130
                        LMLabelPersist _LMLabelPresist = _placement.PIDPlaceLabel(symbol.SPPID.MAPPINGNAME, ref points, Rotation: 0, LabeledItem: connectedLMConnector.AsLMRepresentation(), IsLeaderVisible: leaderLine);
1131
                        if (_LMLabelPresist != null)
1132
                        {
1133
                            _LMLabelPresist.Commit();
1134
                            ReleaseCOMObjects(_LMLabelPresist);
1135
                        }
1136
                        ReleaseCOMObjects(connectedLMConnector);
1137
                    }
1138

    
1139
                    foreach (var item in connectorVertices)
1140
                        if (item.Key != null)
1141
                            ReleaseCOMObjects(item.Key);
1142
                }
1143
            }
1144

    
1145
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetProgress, ++CurrentCount);
1146
        }
1147

    
1148
        /// <summary>
1149
        /// Equipment를 실제로 Modeling 메서드
1150
        /// </summary>
1151
        /// <param name="equipment"></param>
1152
        private void EquipmentModeling(Equipment equipment)
1153
        {
1154
            if (!string.IsNullOrEmpty(equipment.SPPID.RepresentationId))
1155
                return;
1156

    
1157
            LMSymbol _LMSymbol = null;
1158
            LMSymbol targetItem = null;
1159
            string mappingPath = equipment.SPPID.MAPPINGNAME;
1160
            double x = equipment.SPPID.ORIGINAL_X;
1161
            double y = equipment.SPPID.ORIGINAL_Y;
1162
            int mirror = 0;
1163
            double angle = equipment.ANGLE;
1164

    
1165
            SPPIDUtil.ConvertGridPoint(ref x, ref y);
1166

    
1167
            Connector connector = equipment.CONNECTORS.Find(conn => !string.IsNullOrEmpty(conn.CONNECTEDITEM) && conn.CONNECTEDITEM != "None");
1168
            if (connector != null)
1169
            {
1170
                Equipment connEquipment = SPPIDUtil.FindObjectByUID(document, connector.CONNECTEDITEM) as Equipment;
1171
                if (connEquipment != null)
1172
                {
1173
                    if (string.IsNullOrEmpty(connEquipment.SPPID.RepresentationId))
1174
                        EquipmentModeling(connEquipment);
1175

    
1176
                    if (!string.IsNullOrEmpty(connEquipment.SPPID.RepresentationId))
1177
                    {
1178
                        targetItem = dataSource.GetSymbol(connEquipment.SPPID.RepresentationId);
1179
                        if (targetItem != null)
1180
                        {
1181
                            _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle, TargetItem: targetItem);
1182
                        }
1183
                        else
1184
                        {
1185
                            _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle);
1186
                        }
1187
                    }
1188
                    else
1189
                    {
1190
                        _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle);
1191
                    }
1192
                }
1193
                else
1194
                {
1195
                    _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle);
1196
                }
1197
            }
1198
            else
1199
            {
1200
                _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle);
1201
            }
1202

    
1203
            if (_LMSymbol != null)
1204
            {
1205
                _LMSymbol.Commit();
1206
                equipment.SPPID.RepresentationId = _LMSymbol.AsLMRepresentation().Id;
1207
                equipment.SPPID.GraphicOID = _LMSymbol.get_GraphicOID();
1208
                ReleaseCOMObjects(_LMSymbol);
1209
            }
1210

    
1211
            if (targetItem != null)
1212
            {
1213
                ReleaseCOMObjects(targetItem);
1214
            }
1215

    
1216
            ReleaseCOMObjects(_LMSymbol);
1217

    
1218
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetProgress, ++CurrentCount);
1219
        }
1220

    
1221
        /// <summary>
1222
        /// 첫 진입점
1223
        /// </summary>
1224
        /// <param name="symbol"></param>
1225
        private void SymbolModelingBySymbol(Symbol symbol)
1226
        {
1227
            SymbolModeling(symbol, null);
1228
            List<object> endObjects = new List<object>();
1229
            endObjects.Add(symbol);
1230

    
1231
            foreach (var connector in symbol.CONNECTORS)
1232
            {
1233
                object connItem = SPPIDUtil.FindObjectByUID(document, connector.CONNECTEDITEM);
1234
                if (connItem != null && connItem.GetType() != typeof(Equipment))
1235
                {
1236
                    endObjects.Add(connItem);
1237
                    if (connItem.GetType() == typeof(Symbol))
1238
                    {
1239
                        Symbol connSymbol = connItem as Symbol;
1240
                        if (string.IsNullOrEmpty(connSymbol.SPPID.RepresentationId))
1241
                        {
1242
                            SymbolModeling(connSymbol, symbol);
1243
                        }
1244
                        SymbolModelingByNeerSymbolLoop(connSymbol, endObjects);
1245
                    }
1246
                    else if (connItem.GetType() == typeof(Line))
1247
                    {
1248
                        Line connLine = connItem as Line;
1249
                        SymbolModelingByNeerLineLoop(connLine, endObjects, symbol);
1250
                    }
1251
                }
1252
            }
1253
        }
1254

    
1255
        private void SymbolModelingByNeerSymbolLoop(Symbol symbol, List<object> endObjects)
1256
        {
1257
            foreach (var connector in symbol.CONNECTORS)
1258
            {
1259
                object connItem = SPPIDUtil.FindObjectByUID(document, connector.CONNECTEDITEM);
1260
                if (connItem != null && connItem.GetType() != typeof(Equipment))
1261
                {
1262
                    if (!endObjects.Contains(connItem))
1263
                    {
1264
                        endObjects.Add(connItem);
1265
                        if (connItem.GetType() == typeof(Symbol))
1266
                        {
1267
                            Symbol connSymbol = connItem as Symbol;
1268
                            if (string.IsNullOrEmpty(connSymbol.SPPID.RepresentationId))
1269
                            {
1270
                                SymbolModeling(connSymbol, symbol);
1271
                            }
1272
                            SymbolModelingByNeerSymbolLoop(connSymbol, endObjects);
1273
                        }
1274
                        else if (connItem.GetType() == typeof(Line))
1275
                        {
1276
                            Line connLine = connItem as Line;
1277
                            SymbolModelingByNeerLineLoop(connLine, endObjects, symbol);
1278
                        }
1279
                    }
1280
                }
1281
            }
1282
        }
1283

    
1284
        private void SymbolModelingByNeerLineLoop(Line line, List<object> endObjects, Symbol prevSymbol)
1285
        {
1286
            foreach (var connector in line.CONNECTORS)
1287
            {
1288
                object connItem = SPPIDUtil.FindObjectByUID(document, connector.CONNECTEDITEM);
1289
                if (connItem != null && connItem.GetType() != typeof(Equipment))
1290
                {
1291
                    if (!endObjects.Contains(connItem))
1292
                    {
1293
                        endObjects.Add(connItem);
1294
                        if (connItem.GetType() == typeof(Symbol))
1295
                        {
1296
                            Symbol connSymbol = connItem as Symbol;
1297
                            if (string.IsNullOrEmpty(connSymbol.SPPID.RepresentationId))
1298
                            {
1299
                                List<Symbol> group = new List<Symbol>();
1300
                                SPPIDUtil.FindConnectedSymbolGroup(document, connSymbol, group);
1301
                                Symbol priority = prioritySymbols.Find(x => group.Contains(x));
1302
                                List<Symbol> endModelingGroup = new List<Symbol>();
1303
                                if (priority != null)
1304
                                {
1305
                                    SymbolGroupModeling(priority, group);
1306

    
1307
                                    // Range 겹치는지 확인해야함
1308
                                    double[] prevRange = null;
1309
                                    GetSPPIDSymbolRange(prevSymbol, ref prevRange);
1310
                                    double[] groupRange = null;
1311
                                    GetSPPIDSymbolRange(group, ref groupRange);
1312

    
1313
                                    double distanceX = 0;
1314
                                    double distanceY = 0;
1315
                                    bool overlapX = false;
1316
                                    bool overlapY = false;
1317
                                    SlopeType slopeType = SPPIDUtil.CalcSlope(prevSymbol.SPPID.ORIGINAL_X, prevSymbol.SPPID.ORIGINAL_Y, connSymbol.SPPID.ORIGINAL_X, connSymbol.SPPID.ORIGINAL_Y);
1318
                                    SPPIDUtil.CalcOverlap(prevRange, groupRange, ref distanceX, ref distanceY, ref overlapX, ref overlapY);
1319
                                    if ((slopeType == SlopeType.HORIZONTAL && overlapX) ||
1320
                                        (slopeType == SlopeType.VERTICAL && overlapY))
1321
                                    {
1322
                                        RemoveSymbol(group);
1323
                                        foreach (var _temp in group)
1324
                                            SPPIDUtil.CalcNewCoordinateForSymbol(_temp, prevSymbol, distanceX, distanceY);
1325

    
1326
                                        SymbolGroupModeling(priority, group);
1327
                                    }
1328
                                }
1329
                                else
1330
                                {
1331
                                    SymbolModeling(connSymbol, null);
1332
                                    // Range 겹치는지 확인해야함
1333
                                    double[] prevRange = null;
1334
                                    GetSPPIDSymbolRange(prevSymbol, ref prevRange);
1335
                                    double[] connRange = null;
1336
                                    GetSPPIDSymbolRange(connSymbol, ref connRange);
1337

    
1338
                                    double distanceX = 0;
1339
                                    double distanceY = 0;
1340
                                    bool overlapX = false;
1341
                                    bool overlapY = false;
1342
                                    SlopeType slopeType = SPPIDUtil.CalcSlope(prevSymbol.SPPID.ORIGINAL_X, prevSymbol.SPPID.ORIGINAL_Y, connSymbol.SPPID.ORIGINAL_X, connSymbol.SPPID.ORIGINAL_Y);
1343
                                    SPPIDUtil.CalcOverlap(prevRange, connRange, ref distanceX, ref distanceY, ref overlapX, ref overlapY);
1344
                                    if ((slopeType == SlopeType.HORIZONTAL && overlapX) ||
1345
                                        (slopeType == SlopeType.VERTICAL && overlapY))
1346
                                    {
1347
                                        RemoveSymbol(connSymbol);
1348
                                        SPPIDUtil.CalcNewCoordinateForSymbol(connSymbol, prevSymbol, distanceX, distanceY);
1349

    
1350
                                        SymbolModeling(connSymbol, null);
1351
                                    }
1352
                                }
1353
                            }
1354
                            SymbolModelingByNeerSymbolLoop(connSymbol, endObjects);
1355
                        }
1356
                        else if (connItem.GetType() == typeof(Line))
1357
                        {
1358
                            Line connLine = connItem as Line;
1359
                            if (!SPPIDUtil.IsBranchLine(connLine, line))
1360
                                SymbolModelingByNeerLineLoop(connLine, endObjects, prevSymbol);
1361
                        }
1362
                    }
1363
                }
1364
            }
1365
        }
1366

    
1367
        private void SymbolGroupModeling(Symbol firstSymbol, List<Symbol> group)
1368
        {
1369
            List<Symbol> endModelingGroup = new List<Symbol>();
1370
            SymbolModeling(firstSymbol, null);
1371
            endModelingGroup.Add(firstSymbol);
1372
            while (endModelingGroup.Count != group.Count)
1373
            {
1374
                foreach (var _symbol in group)
1375
                {
1376
                    if (!endModelingGroup.Contains(_symbol))
1377
                    {
1378
                        foreach (var _connector in _symbol.CONNECTORS)
1379
                        {
1380
                            Symbol _connSymbol = SPPIDUtil.FindObjectByUID(document, _connector.CONNECTEDITEM) as Symbol;
1381
                            if (_connSymbol != null && endModelingGroup.Contains(_connSymbol))
1382
                            {
1383
                                SymbolModeling(_symbol, _connSymbol);
1384
                                endModelingGroup.Add(_symbol);
1385
                                break;
1386
                            }
1387
                        }
1388
                    }
1389
                }
1390
            }
1391
        }
1392

    
1393
        /// <summary>
1394
        /// 심볼을 실제로 Modeling할때 ChildSymbol이 있다면 Modeling하는 메서드
1395
        /// </summary>
1396
        /// <param name="childSymbol"></param>
1397
        /// <param name="parentSymbol"></param>
1398
        private void CreateChildSymbol(ChildSymbol childSymbol, LMSymbol parentSymbol)
1399
        {
1400
            Ingr.RAD2D.Symbol2d symbol2d = radApp.ActiveDocument.ActiveSheet.DrawingObjects[parentSymbol.get_GraphicOID().ToString()];
1401
            double x1 = 0;
1402
            double x2 = 0;
1403
            double y1 = 0;
1404
            double y2 = 0;
1405
            symbol2d.Range(out x1, out y1, out x2, out y2);
1406

    
1407
            LMSymbol _LMSymbol = _placement.PIDPlaceSymbol(childSymbol.SPPID.MAPPINGNAME, (x1 + x2) / 2, (y1 + y2) / 2, TargetItem: parentSymbol);
1408
            if (_LMSymbol != null)
1409
            {
1410
                childSymbol.SPPID.RepresentationId = _LMSymbol.AsLMRepresentation().Id;
1411
                foreach (var item in childSymbol.ChildSymbols)
1412
                    CreateChildSymbol(item, _LMSymbol);
1413
            }
1414

    
1415

    
1416
            ReleaseCOMObjects(_LMSymbol);
1417
        }
1418

    
1419
        /// <summary>
1420
        /// item이 TargetItem과 같은 LineRun에 있는지 검사
1421
        /// </summary>
1422
        /// <param name="item"></param>
1423
        /// <param name="targetItem"></param>
1424
        /// <returns></returns>
1425
        private bool IsSameLineRun(object item, object targetItem)
1426
        {
1427
            foreach (var lineNumber in document.LINENUMBERS)
1428
            {
1429
                foreach (var run in lineNumber.RUNS)
1430
                {
1431
                    foreach (var runItem in run.RUNITEMS)
1432
                    {
1433
                        if (runItem == item)
1434
                        {
1435
                            foreach (var findItem in run.RUNITEMS)
1436
                            {
1437
                                if (findItem == targetItem)
1438
                                {
1439
                                    return true;
1440
                                }
1441
                            }
1442

    
1443
                            return false;
1444

    
1445
                        }
1446
                    }
1447
                }
1448
            }
1449

    
1450
            return false;
1451
        }
1452

    
1453
        /// <summary>
1454
        /// Line을 실제로 모델링하는 메서드
1455
        /// </summary>
1456
        /// <param name="lines"></param>
1457
        private void LineModeling(List<Line> lines)
1458
        {
1459
            _LMAItem _LMAItem = _placement.PIDCreateItem(lines[0].SPPID.MAPPINGNAME);
1460
            PlaceRunInputs placeRunInputs = new PlaceRunInputs();
1461
            LMSymbol _LMSymbol1 = null;
1462
            LMSymbol _LMSymbol2 = null;
1463
            Dictionary<LMConnector, List<double[]>> connectorVertices1 = new Dictionary<LMConnector, List<double[]>>();
1464
            LMConnector targetConnector1 = null;
1465
            Dictionary<LMConnector, List<double[]>> connectorVertices2 = new Dictionary<LMConnector, List<double[]>>();
1466
            LMConnector targetConnector2 = null;
1467

    
1468
            Line startBranchLine = null;
1469
            Line endBranchLine = null;
1470

    
1471
            // Type, Line, TargetObjet, x, y
1472
            List<Tuple<string, Line, object, double, double>> linePointInfo = new List<Tuple<string, Line, object, double, double>>();
1473
            // Point 정리
1474
            for (int i = 0; i < lines.Count; i++)
1475
            {
1476
                Line line = lines[i];
1477
                if (i == 0 || i + 1 != lines.Count)
1478
                {
1479
                    // 시작점에 연결된 Symbol 찾기
1480
                    object connItem = SPPIDUtil.FindObjectByUID(document, line.CONNECTORS[0].CONNECTEDITEM);
1481
                    if (connItem != null && connItem.GetType() == typeof(Symbol))
1482
                    {
1483
                        Symbol symbol1 = connItem as Symbol;
1484
                        _LMSymbol1 = GetTargetSymbol(symbol1, line);
1485
                        if (_LMSymbol1 != null)
1486
                        {
1487
                            double x = line.SPPID.START_X;
1488
                            double y = line.SPPID.START_Y;
1489
                            Connector connector = SPPIDUtil.FindSymbolConnectorByUID(document, line.UID, symbol1);
1490
                            if (connector != null)
1491
                            {
1492
                                GetTargetSymbolConnectorPoint(connector, symbol1, ref x, ref y);
1493
                                line.SPPID.START_X = x;
1494
                                line.SPPID.START_Y = y;
1495
                            }
1496

    
1497
                            linePointInfo.Add(new Tuple<string, Line, object, double, double>("SYMBOL", line, _LMSymbol1, x, y));
1498
                        }
1499
                        else
1500
                            linePointInfo.Add(new Tuple<string, Line, object, double, double>(null, line, null, line.SPPID.START_X, line.SPPID.START_Y));
1501
                    }
1502
                    else if (connItem != null && connItem.GetType() == typeof(Line) && !lines.Contains(connItem))
1503
                    {
1504
                        connectorVertices1 = GetPipeRunVertices(((Line)connItem).SPPID.ModelItemId);
1505
                        targetConnector1 = FindTargetLMConnector(connectorVertices1, line.SPPID.START_X, line.SPPID.START_Y, line.SPPID.END_X, line.SPPID.END_Y);
1506

    
1507
                        if (targetConnector1 != null)
1508
                            linePointInfo.Add(new Tuple<string, Line, object, double, double>("LINE", line, targetConnector1, line.SPPID.START_X, line.SPPID.START_Y));
1509
                        else
1510
                        {
1511
                            startBranchLine = connItem as Line;
1512
                            linePointInfo.Add(new Tuple<string, Line, object, double, double>(null, line, null, line.SPPID.START_X, line.SPPID.START_Y));
1513
                        }
1514
                    }
1515
                    else
1516
                        linePointInfo.Add(new Tuple<string, Line, object, double, double>(null, line, null, line.SPPID.START_X, line.SPPID.START_Y));
1517

    
1518
                }
1519
                if (i + 1 == lines.Count)
1520
                {
1521
                    // 끝점에 연결된 Symbol 찾기
1522
                    object connItem = SPPIDUtil.FindObjectByUID(document, line.CONNECTORS[1].CONNECTEDITEM);
1523

    
1524
                    if (i != 0)
1525
                        linePointInfo.Add(new Tuple<string, Line, object, double, double>(null, line, null, line.SPPID.START_X, line.SPPID.START_Y));
1526

    
1527
                    if (connItem != null && connItem.GetType() == typeof(Symbol))
1528
                    {
1529
                        Symbol symbol2 = connItem as Symbol;
1530
                        _LMSymbol2 = GetTargetSymbol(connItem as Symbol, line);
1531
                        if (_LMSymbol2 != null)
1532
                        {
1533
                            double x = line.SPPID.END_X;
1534
                            double y = line.SPPID.END_Y;
1535
                            Connector connector = SPPIDUtil.FindSymbolConnectorByUID(document, line.UID, symbol2);
1536
                            if (connector != null)
1537
                            {
1538
                                GetTargetSymbolConnectorPoint(connector, symbol2, ref x, ref y);
1539
                                line.SPPID.END_X = x;
1540
                                line.SPPID.END_Y = y;
1541
                            }
1542

    
1543
                            linePointInfo.Add(new Tuple<string, Line, object, double, double>("SYMBOL", line, _LMSymbol2, x, y));
1544
                        }
1545
                        else
1546
                            linePointInfo.Add(new Tuple<string, Line, object, double, double>(null, line, null, line.SPPID.END_X, line.SPPID.END_Y));
1547
                    }
1548
                    else if (connItem != null && connItem.GetType() == typeof(Line) && !lines.Contains(connItem))
1549
                    {
1550
                        connectorVertices2 = GetPipeRunVertices(((Line)connItem).SPPID.ModelItemId);
1551
                        targetConnector2 = FindTargetLMConnector(connectorVertices2, line.SPPID.END_X, line.SPPID.END_Y, line.SPPID.START_X, line.SPPID.START_Y);
1552

    
1553
                        if (targetConnector2 != null)
1554
                            linePointInfo.Add(new Tuple<string, Line, object, double, double>("LINE", line, targetConnector2, line.SPPID.END_X, line.SPPID.END_Y));
1555
                        else
1556
                        {
1557
                            endBranchLine = connItem as Line;
1558
                            linePointInfo.Add(new Tuple<string, Line, object, double, double>(null, line, null, line.SPPID.END_X, line.SPPID.END_Y));
1559
                        }
1560
                    }
1561
                    else
1562
                        linePointInfo.Add(new Tuple<string, Line, object, double, double>(null, line, null, line.SPPID.END_X, line.SPPID.END_Y));
1563
                }
1564
            }
1565

    
1566
            double prevX = double.NaN;
1567
            double prevY = double.NaN;
1568
            SlopeType prevSlopeType = SlopeType.None;
1569
            for (int i = 0; i < linePointInfo.Count; i++)
1570
            {
1571
                Tuple<string, Line, object, double, double> item = linePointInfo[i];
1572
                Line line = item.Item2;
1573
                double x = item.Item4;
1574
                double y = item.Item5;
1575
                SlopeType slopeType = SPPIDUtil.CalcSlope(line.SPPID.START_X, line.SPPID.START_Y, line.SPPID.END_X, line.SPPID.END_Y);
1576
                // Symbol일 경우 바로 Input Point
1577
                if (item.Item1 == "SYMBOL")
1578
                {
1579
                    LMSymbol targetSymbol = item.Item3 as LMSymbol;
1580
                    placeRunInputs.AddSymbolTarget(targetSymbol, x, y);
1581
                }
1582
                else
1583
                {
1584
                    SPPIDUtil.ConvertGridPoint(ref x, ref y);
1585
                    // i == 0은 그대로 사용
1586
                    if (i != 0)
1587
                    {
1588
                        Tuple<string, Line, object, double, double> prevItem = linePointInfo[i - 1];
1589
                        // y 좌표가 같아야함 및 Symbol 좌표가 정확하지 않으므로 한번더 보정
1590
                        if (prevSlopeType == SlopeType.HORIZONTAL)
1591
                        {
1592
                            y = prevY;
1593
                            SPPIDUtil.ConvertGridPointOnlyOnePoint(ref y);
1594
                        }
1595
                        else if (prevSlopeType == SlopeType.VERTICAL)
1596
                        {
1597
                            x = prevX;
1598
                            SPPIDUtil.ConvertGridPointOnlyOnePoint(ref x);
1599
                        }
1600

    
1601
                        // 마지막이 Symbol일 경우는 Symbol의 좌표를 따라감
1602
                        if (i + 1 == linePointInfo.Count - 1 && linePointInfo[i + 1].Item1 == "SYMBOL")
1603
                        {
1604
                            Line nextLine = linePointInfo[i + 1].Item2;
1605
                            SlopeType nextSlopeType = SPPIDUtil.CalcSlope(nextLine.SPPID.START_X, nextLine.SPPID.START_Y, nextLine.SPPID.END_X, nextLine.SPPID.END_Y);
1606
                            if (slopeType == SlopeType.HORIZONTAL)
1607
                                y = linePointInfo[i + 1].Item5;
1608
                            else if (slopeType == SlopeType.VERTICAL)
1609
                                x = linePointInfo[i + 1].Item4;
1610
                        }
1611
                    }
1612

    
1613
                    if (item.Item1 == "LINE")
1614
                    {
1615
                        LMConnector targetConnector = item.Item3 as LMConnector;
1616
                        placeRunInputs.AddConnectorTarget(targetConnector, x, y);
1617
                    }
1618

    
1619
                    else
1620
                        placeRunInputs.AddPoint(x, y);
1621
                }
1622

    
1623
                prevX = x;
1624
                prevY = y;
1625
                prevSlopeType = slopeType;
1626
            }
1627

    
1628
            LMConnector _lMConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
1629

    
1630
            if (_lMConnector != null)
1631
            {
1632
                bool bStart = false;
1633
                bool bEnd = false;
1634

    
1635
                NeedReModeling(lines[0], _LMSymbol1, ref bStart);
1636
                NeedReModeling(lines[lines.Count - 1], _LMSymbol2, ref bEnd);
1637

    
1638
                if (bStart || bEnd)
1639
                    ReModelingLine(lines, _lMConnector, _LMSymbol1, _LMSymbol2, bStart, bEnd);
1640
                else
1641
                {
1642
                    foreach (var line in lines)
1643
                        line.SPPID.ModelItemId = _lMConnector.ModelItemID;
1644
                    _lMConnector.Commit();
1645
                }
1646

    
1647
                if (startBranchLine != null || endBranchLine != null)
1648
                    BranchLines.Add(new Tuple<string, Line, Line>(lines[0].SPPID.ModelItemId, startBranchLine, endBranchLine));
1649
            }
1650

    
1651
            if (_LMSymbol1 != null)
1652
                ReleaseCOMObjects(_LMSymbol1);
1653
            if (_LMSymbol2 != null)
1654
                ReleaseCOMObjects(_LMSymbol2);
1655
            if (targetConnector1 != null)
1656
                ReleaseCOMObjects(targetConnector1);
1657
            if (targetConnector2 != null)
1658
                ReleaseCOMObjects(targetConnector2);
1659
            foreach (var item in connectorVertices1)
1660
                ReleaseCOMObjects(item.Key);
1661
            foreach (var item in connectorVertices2)
1662
                ReleaseCOMObjects(item.Key);
1663

    
1664
            ReleaseCOMObjects(_lMConnector);
1665
            ReleaseCOMObjects(placeRunInputs);
1666
            ReleaseCOMObjects(_LMAItem);
1667
        }
1668

    
1669
        private void NeedReModeling(Line line, LMSymbol symbol, ref bool result)
1670
        {
1671
            if (symbol != null)
1672
            {
1673
                string repID = symbol.AsLMRepresentation().Id;
1674
                string symbolUID = SPPIDUtil.FindSymbolByRepresentationID(document, repID).UID;
1675
                string lineUID = line.UID;
1676

    
1677
                SpecBreak startSpecBreak = document.SpecBreaks.Find(x =>
1678
                (x.DownStreamUID == symbolUID || x.UpStreamUID == symbolUID) &&
1679
                (x.DownStreamUID == lineUID || x.UpStreamUID == lineUID));
1680

    
1681
                EndBreak startEndBreak = document.EndBreaks.Find(x =>
1682
                (x.OWNER == symbolUID || x.PROPERTIES.Find(y => y.ATTRIBUTE == "Connected Item").VALUE == symbolUID) &&
1683
                (x.OWNER == lineUID || x.PROPERTIES.Find(y => y.ATTRIBUTE == "Connected Item").VALUE == lineUID));
1684

    
1685
                if (startSpecBreak != null || startEndBreak != null)
1686
                    result = true;
1687
            }
1688
        }
1689

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

    
1719
            List<double[]> vertices = new List<double[]>();
1720
            for (int i = 1; i <= verticesCount; i++)
1721
            {
1722
                double x = 0;
1723
                double y = 0;
1724
                lineStringGeometry.GetVertex(i, ref x, ref y);
1725
                vertices.Add(new double[] { x, y });
1726
            }
1727

    
1728
            for (int i = 0; i < vertices.Count; i++)
1729
            {
1730
                double[] points = vertices[i];
1731
                // 시작 심볼이 있고 첫번째 좌표일 때
1732
                if (startSymbol != null && i == 0)
1733
                {
1734
                    if (bStart)
1735
                    {
1736
                        SlopeType slopeType = SPPIDUtil.CalcSlope(points[0], points[1], vertices[i + 1][0], vertices[i + 1][1]);
1737
                        if (slopeType == SlopeType.HORIZONTAL)
1738
                            placeRunInputs.AddPoint(points[0], -0.1);
1739
                        else if (slopeType == SlopeType.VERTICAL)
1740
                            placeRunInputs.AddPoint(-0.1, points[1]);
1741
                        else
1742
                            placeRunInputs.AddPoint(points[0], -0.1);
1743

    
1744
                        placeRunInputs.AddPoint(points[0], points[1]);
1745
                    }
1746
                    else
1747
                    {
1748
                        placeRunInputs.AddSymbolTarget(startSymbol, points[0], points[1]);
1749
                    }
1750
                }
1751
                // 마지막 심볼이 있고 마지막 좌표일 때
1752
                else if (endSymbol != null && i == vertices.Count - 1)
1753
                {
1754
                    if (bEnd)
1755
                    {
1756
                        placeRunInputs.AddPoint(points[0], points[1]);
1757

    
1758
                        SlopeType slopeType = SPPIDUtil.CalcSlope(points[0], points[1], vertices[i - 1][0], vertices[i - 1][1]);
1759
                        if (slopeType == SlopeType.HORIZONTAL)
1760
                            placeRunInputs.AddPoint(points[0], -0.1);
1761
                        else if (slopeType == SlopeType.VERTICAL)
1762
                            placeRunInputs.AddPoint(-0.1, points[1]);
1763
                        else
1764
                            placeRunInputs.AddPoint(points[0], -0.1);
1765
                    }
1766
                    else
1767
                    {
1768
                        placeRunInputs.AddSymbolTarget(endSymbol, points[0], points[1]);
1769
                    }
1770
                }
1771
                // 첫번째이며 시작 심볼이 아니고 Connecotr일 경우
1772
                else if (i == 0 && prevLMConnector.ConnectItem1SymbolObject != null)
1773
                    placeRunInputs.AddSymbolTarget(prevLMConnector.ConnectItem1SymbolObject, points[0], points[1]);
1774
                // 마지막이며 마지막 심볼이 아니고 Connecotr일 경우
1775
                else if (i == vertices.Count - 1 && prevLMConnector.ConnectItem2SymbolObject != null)
1776
                    placeRunInputs.AddSymbolTarget(prevLMConnector.ConnectItem2SymbolObject, points[0], points[1]);
1777
                else
1778
                    placeRunInputs.AddPoint(points[0], points[1]);
1779
            }
1780

    
1781
            _placement.PIDRemovePlacement(prevLMConnector.AsLMRepresentation());
1782
            newConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
1783

    
1784
            ReleaseCOMObjects(placeRunInputs);
1785
            ReleaseCOMObjects(_LMAItem);
1786
            ReleaseCOMObjects(modelItem);
1787

    
1788
            if (newConnector != null)
1789
            {
1790
                if (startSymbol != null && bStart)
1791
                {
1792
                    _LMAItem = _placement.PIDCreateItem(symbolPath);
1793
                    placeRunInputs = new PlaceRunInputs();
1794
                    placeRunInputs.AddSymbolTarget(startSymbol, vertices[0][0], vertices[0][1]);
1795
                    placeRunInputs.AddConnectorTarget(newConnector, vertices[0][0], vertices[0][1]);
1796
                    LMConnector _LMConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
1797
                    if (_LMConnector != null)
1798
                    {
1799
                        RemoveConnectorForReModelingLine(newConnector);
1800
                        ZeroLengthModelItemID.Add(_LMConnector.ModelItemID);
1801
                        ReleaseCOMObjects(_LMConnector);
1802
                    }
1803
                    ReleaseCOMObjects(placeRunInputs);
1804
                    ReleaseCOMObjects(_LMAItem);
1805
                }
1806

    
1807
                if (endSymbol != null && bEnd)
1808
                {
1809
                    if (startSymbol != null)
1810
                    {
1811
                        Dictionary<LMConnector, List<double[]>> dicVertices = GetPipeRunVertices(newConnector.ModelItemID);
1812
                        newConnector = dicVertices.First().Key;
1813
                    }
1814

    
1815
                    _LMAItem = _placement.PIDCreateItem(symbolPath);
1816
                    placeRunInputs = new PlaceRunInputs();
1817
                    placeRunInputs.AddSymbolTarget(endSymbol, vertices[vertices.Count - 1][0], vertices[vertices.Count - 1][1]);
1818
                    placeRunInputs.AddConnectorTarget(newConnector, vertices[vertices.Count - 1][0], vertices[vertices.Count - 1][1]);
1819
                    LMConnector _LMConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
1820
                    if (_LMConnector != null)
1821
                    {
1822
                        RemoveConnectorForReModelingLine(newConnector);
1823
                        ZeroLengthModelItemID.Add(_LMConnector.ModelItemID);
1824
                        ReleaseCOMObjects(_LMConnector);
1825
                    }
1826
                    ReleaseCOMObjects(placeRunInputs);
1827
                    ReleaseCOMObjects(_LMAItem);
1828
                }
1829

    
1830
                foreach (var line in lines)
1831
                    line.SPPID.ModelItemId = newConnector.ModelItemID;
1832
                ReleaseCOMObjects(newConnector);
1833
            }
1834

    
1835
            ReleaseCOMObjects(modelItem);
1836
        }
1837

    
1838
        /// <summary>
1839
        /// Remodeling 과정에서 생긴 불필요한 Connector 제거
1840
        /// </summary>
1841
        /// <param name="connector"></param>
1842
        private void RemoveConnectorForReModelingLine(LMConnector connector)
1843
        {
1844
            Dictionary<LMConnector, List<double[]>> dicVertices = GetPipeRunVertices(connector.ModelItemID);
1845
            foreach (var item in dicVertices)
1846
            {
1847
                bool result = false;
1848
                foreach (var point in item.Value)
1849
                {
1850
                    if (point[0] < 0 || point[1] < 0)
1851
                    {
1852
                        result = true;
1853
                        _placement.PIDRemovePlacement(item.Key.AsLMRepresentation());
1854
                        break;
1855
                    }
1856
                }
1857

    
1858
                if (result)
1859
                    break;
1860
            }
1861
            foreach (var item in dicVertices)
1862
                ReleaseCOMObjects(item.Key);
1863
        }
1864

    
1865
        /// <summary>
1866
        /// Symbol이 모델링된 SPPPID Symbol Object를 반환 - 연결된 Symbol이 ChildSymbol일 수도 있기때문에 메서드 개발
1867
        /// </summary>
1868
        /// <param name="symbol"></param>
1869
        /// <param name="line"></param>
1870
        /// <returns></returns>
1871
        private LMSymbol GetTargetSymbol(Symbol symbol, Line line)
1872
        {
1873
            LMSymbol _LMSymbol = null;
1874
            foreach (var connector in symbol.CONNECTORS)
1875
            {
1876
                if (connector.CONNECTEDITEM == line.UID)
1877
                {
1878
                    if (connector.Index == 0)
1879
                        _LMSymbol = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
1880
                    else
1881
                    {
1882
                        ChildSymbol child = null;
1883
                        foreach (var childSymbol in symbol.ChildSymbols)
1884
                        {
1885
                            if (childSymbol.Connectors.Contains(connector))
1886
                                child = childSymbol;
1887
                            else
1888
                                child = GetChildSymbolByConnector(childSymbol, connector);
1889

    
1890
                            if (child != null)
1891
                                break;
1892
                        }
1893

    
1894
                        if (child != null)
1895
                            _LMSymbol = dataSource.GetSymbol(child.SPPID.RepresentationId);
1896
                    }
1897

    
1898
                    break;
1899
                }
1900
            }
1901

    
1902
            return _LMSymbol;
1903
        }
1904

    
1905
        /// <summary>
1906
        /// Connector를 가지고 있는 ChildSymbol Object 반환
1907
        /// </summary>
1908
        /// <param name="item"></param>
1909
        /// <param name="connector"></param>
1910
        /// <returns></returns>
1911
        private ChildSymbol GetChildSymbolByConnector(ChildSymbol item, Connector connector)
1912
        {
1913
            foreach (var childSymbol in item.ChildSymbols)
1914
            {
1915
                if (childSymbol.Connectors.Contains(connector))
1916
                    return childSymbol;
1917
                else
1918
                    return GetChildSymbolByConnector(childSymbol, connector);
1919
            }
1920

    
1921
            return null;
1922
        }
1923

    
1924
        /// <summary>
1925
        /// Branch 라인을 다시 모델링하는 진입 메서드
1926
        /// </summary>
1927
        /// <param name="branch"></param>
1928
        private void BranchLineModeling(Tuple<string, Line, Line> branch)
1929
        {
1930
            List<Line> lines = SPPIDUtil.FindLinesByModelId(document, branch.Item1);
1931

    
1932
            Dictionary<LMConnector, List<double[]>> connectorVertices = GetPipeRunVertices(branch.Item1);
1933
            LMConnector _StartConnector = null;
1934
            LMConnector _EndConnector = null;
1935
            double lengthStart = double.MaxValue;
1936
            double lengthEnd = double.MaxValue;
1937
            List<double[]> startPoints = new List<double[]>();
1938
            List<double[]> endPoints = new List<double[]>();
1939

    
1940
            foreach (var item in connectorVertices)
1941
            {
1942
                foreach (var point in item.Value)
1943
                {
1944
                    // Start Point가 Branch
1945
                    if (branch.Item2 != null)
1946
                    {
1947
                        Line targetLine = branch.Item2;
1948
                        double distance = SPPIDUtil.CalcLineToPointDistance(targetLine.SPPID.START_X, targetLine.SPPID.START_Y, targetLine.SPPID.END_X, targetLine.SPPID.END_Y, point[0], point[1]);
1949
                        if (lengthStart > distance)
1950
                        {
1951
                            _StartConnector = item.Key;
1952
                            lengthStart = distance;
1953
                            startPoints = item.Value;
1954
                        }
1955
                    }
1956
                    // End Point가 Branch
1957
                    if (branch.Item3 != null)
1958
                    {
1959
                        Line targetLine = branch.Item3;
1960
                        double distance = SPPIDUtil.CalcLineToPointDistance(targetLine.SPPID.START_X, targetLine.SPPID.START_Y, targetLine.SPPID.END_X, targetLine.SPPID.END_Y, point[0], point[1]);
1961
                        if (lengthEnd > distance)
1962
                        {
1963
                            _EndConnector = item.Key;
1964
                            lengthEnd = distance;
1965
                            endPoints = item.Value;
1966
                        }
1967
                    }
1968
                }
1969
            }
1970
            #region Branch가 양쪽 전부일 때
1971
            if (_StartConnector != null && _StartConnector == _EndConnector)
1972
            {
1973
                _placement.PIDRemovePlacement(_StartConnector.AsLMRepresentation());
1974

    
1975
                _LMAItem _LMAItem = _placement.PIDCreateItem(lines[0].SPPID.MAPPINGNAME);
1976
                PlaceRunInputs placeRunInputs = new PlaceRunInputs();
1977

    
1978
                Dictionary<LMConnector, List<double[]>> startConnectorVertices = GetPipeRunVertices(branch.Item2.SPPID.ModelItemId);
1979
                LMConnector _StartTargetConnector = FindTargetLMConnector(startConnectorVertices, startPoints[0][0], startPoints[0][1], startPoints[1][0], startPoints[1][1]);
1980
                Dictionary<LMConnector, List<double[]>> endConnectorVertices = GetPipeRunVertices(branch.Item3.SPPID.ModelItemId);
1981
                LMConnector _EndTargetConnector = FindTargetLMConnector(endConnectorVertices,
1982
                   startPoints[startPoints.Count - 1][0],
1983
                   startPoints[startPoints.Count - 1][1],
1984
                   startPoints[startPoints.Count - 2][0],
1985
                   startPoints[startPoints.Count - 2][1]);
1986

    
1987
                for (int i = 0; i < startPoints.Count; i++)
1988
                {
1989
                    double[] point = startPoints[i];
1990
                    if (i == 0)
1991
                        placeRunInputs.AddConnectorTarget(_StartTargetConnector, point[0], point[1]);
1992
                    else if (i == startPoints.Count - 1)
1993
                        placeRunInputs.AddConnectorTarget(_EndTargetConnector, point[0], point[1]);
1994
                    else
1995
                        placeRunInputs.AddPoint(point[0], point[1]);
1996
                }
1997

    
1998
                LMConnector _LMConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
1999
                if (_LMConnector != null)
2000
                {
2001
                    _LMConnector.Commit();
2002
                    foreach (var item in lines)
2003
                        item.SPPID.ModelItemId = _LMConnector.ModelItemID;
2004
                }
2005

    
2006
                foreach (var item in startConnectorVertices)
2007
                    ReleaseCOMObjects(item.Key);
2008
                foreach (var item in endConnectorVertices)
2009
                    ReleaseCOMObjects(item.Key);
2010
                ReleaseCOMObjects(placeRunInputs);
2011
                ReleaseCOMObjects(_LMAItem);
2012
                ReleaseCOMObjects(_LMConnector);
2013
            }
2014
            #endregion
2015
            #region 양쪽이 다른 Branch 
2016
            else
2017
            {
2018
                // Branch 시작 Connector
2019
                if (_StartConnector != null)
2020
                    BranchLineModelingByConnector(branch, _StartConnector, startPoints, true);
2021
                // Branch 끝 Connector
2022
                else if (_EndConnector != null)
2023
                    BranchLineModelingByConnector(branch, _EndConnector, endPoints, false);
2024
            }
2025
            #endregion
2026

    
2027
            if (_StartConnector != null)
2028
                ReleaseCOMObjects(_StartConnector);
2029
            if (_EndConnector != null)
2030
                ReleaseCOMObjects(_EndConnector);
2031
            foreach (var item in connectorVertices)
2032
                ReleaseCOMObjects(item.Key);
2033
        }
2034

    
2035
        /// <summary>
2036
        /// Branch 라인을 다시 실제로 모델링하는 메서드
2037
        /// </summary>
2038
        /// <param name="branch"></param>
2039
        /// <param name="_Connector"></param>
2040
        /// <param name="points"></param>
2041
        /// <param name="IsStart"></param>
2042
        private void BranchLineModelingByConnector(Tuple<string, Line, Line> branch, LMConnector _Connector, List<double[]> points, bool IsStart)
2043
        {
2044
            List<Line> lines = SPPIDUtil.FindLinesByModelId(document, branch.Item1);
2045
            Dictionary<LMConnector, List<double[]>> connectorVertices = GetPipeRunVertices(branch.Item1);
2046
            LMConnector _SameRunTargetConnector = null;
2047
            LMSymbol _SameRunTargetLMSymbol = null;
2048
            Symbol _SameRunTargetSymbol = null;
2049
            Dictionary<LMConnector, List<double[]>> branchConnectorVertices = null;
2050
            LMConnector _BranchTargetConnector = null;
2051
            PlaceRunInputs placeRunInputs = new PlaceRunInputs();
2052

    
2053
            // 같은 Line Run의 Connector 찾기
2054
            foreach (var item in connectorVertices)
2055
            {
2056
                if (item.Key == _Connector)
2057
                    continue;
2058

    
2059
                if (IsStart &&
2060
                    !DBNull.Value.Equals(item.Key.ConnectItem1SymbolID) &&
2061
                    !DBNull.Value.Equals(_Connector.ConnectItem2SymbolID) &&
2062
                    item.Key.ConnectItem1SymbolID == _Connector.ConnectItem2SymbolID)
2063
                {
2064
                    _SameRunTargetConnector = item.Key;
2065
                    break;
2066
                }
2067
                else if (!IsStart &&
2068
                    !DBNull.Value.Equals(item.Key.ConnectItem2SymbolID) &&
2069
                    !DBNull.Value.Equals(_Connector.ConnectItem1SymbolID) &&
2070
                    item.Key.ConnectItem2SymbolID == _Connector.ConnectItem1SymbolID)
2071
                {
2072
                    _SameRunTargetConnector = item.Key;
2073
                    break;
2074
                }
2075
            }
2076

    
2077
            // Branch 반대편이 Symbol
2078
            if (_SameRunTargetConnector == null)
2079
            {
2080
                foreach (var line in lines)
2081
                {
2082
                    foreach (var connector in line.CONNECTORS)
2083
                    {
2084
                        Symbol symbol = SPPIDUtil.FindObjectByUID(document, connector.CONNECTEDITEM) as Symbol;
2085
                        if (symbol != null)
2086
                        {
2087
                            _SameRunTargetSymbol = symbol;
2088
                            _SameRunTargetLMSymbol = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
2089
                            break;
2090
                        }
2091
                    }
2092
                }
2093
            }
2094

    
2095
            // 기존 Connector 제거
2096
            _placement.PIDRemovePlacement(_Connector.AsLMRepresentation());
2097

    
2098
            // 시작 Connector일 경우 첫 Point가 TargetConnector를 찾아야함
2099
            if (IsStart)
2100
            {
2101
                branchConnectorVertices = GetPipeRunVertices(branch.Item2.SPPID.ModelItemId);
2102
                _BranchTargetConnector = FindTargetLMConnector(branchConnectorVertices, points[0][0], points[0][1], points[1][0], points[1][1]);
2103
            }
2104
            // 끝 Conenctor일 경우 마지막 Point가 TargetConnector를 찾아야함
2105
            else
2106
            {
2107
                branchConnectorVertices = GetPipeRunVertices(branch.Item3.SPPID.ModelItemId);
2108
                _BranchTargetConnector = FindTargetLMConnector(branchConnectorVertices,
2109
                    points[points.Count - 1][0],
2110
                    points[points.Count - 1][1],
2111
                    points[points.Count - 2][0],
2112
                    points[points.Count - 2][1]);
2113
            }
2114

    
2115

    
2116
            bool bNeedRemodeling = false;
2117
            if (_SameRunTargetLMSymbol != null)
2118
            {
2119
                Line line = lines.Find(x => x.CONNECTORS.Find(y => y.CONNECTEDITEM == _SameRunTargetSymbol.UID) != null);
2120
                NeedReModeling(line, _SameRunTargetLMSymbol, ref bNeedRemodeling);
2121
            }
2122

    
2123

    
2124

    
2125
            for (int i = 0; i < points.Count; i++)
2126
            {
2127
                double[] point = points[i];
2128
                if (i == 0)
2129
                {
2130
                    if (IsStart)
2131
                    {
2132
                        if (_BranchTargetConnector != null)
2133
                        {
2134
                            placeRunInputs.AddConnectorTarget(_BranchTargetConnector, point[0], point[1]);
2135
                        }
2136
                        else
2137
                        {
2138
                            placeRunInputs.AddPoint(point[0], point[1]);
2139
                        }
2140
                    }
2141
                    else
2142
                    {
2143
                        if (_SameRunTargetConnector != null)
2144
                            placeRunInputs.AddConnectorTarget(_SameRunTargetConnector, point[0], point[1]);
2145
                        else if (_SameRunTargetLMSymbol != null && bNeedRemodeling)
2146
                        {
2147
                            SlopeType slopeType = SPPIDUtil.CalcSlope(point[0], point[1], points[i + 1][0], points[i + 1][1]);
2148
                            if (slopeType == SlopeType.HORIZONTAL)
2149
                                placeRunInputs.AddPoint(point[0], -0.1);
2150
                            else if (slopeType == SlopeType.VERTICAL)
2151
                                placeRunInputs.AddPoint(-0.1, point[1]);
2152
                            else
2153
                                placeRunInputs.AddPoint(point[0], -0.1);
2154

    
2155
                            placeRunInputs.AddPoint(point[0], point[1]);
2156
                        }
2157
                        else if (_SameRunTargetLMSymbol != null)
2158
                            placeRunInputs.AddSymbolTarget(_SameRunTargetLMSymbol, point[0], point[1]);
2159
                        else
2160
                            placeRunInputs.AddPoint(point[0], point[1]);
2161
                    }
2162
                }
2163
                else if (i == points.Count - 1)
2164
                {
2165
                    if (IsStart)
2166
                    {
2167
                        if (_SameRunTargetConnector != null)
2168
                            placeRunInputs.AddConnectorTarget(_SameRunTargetConnector, point[0], point[1]);
2169
                        else if (_SameRunTargetLMSymbol != null && bNeedRemodeling)
2170
                        {
2171
                            placeRunInputs.AddPoint(point[0], point[1]);
2172

    
2173
                            SlopeType slopeType = SPPIDUtil.CalcSlope(point[0], point[1], points[i - 1][0], points[i - 1][1]);
2174
                            if (slopeType == SlopeType.HORIZONTAL)
2175
                                placeRunInputs.AddPoint(point[0], -0.1);
2176
                            else if (slopeType == SlopeType.VERTICAL)
2177
                                placeRunInputs.AddPoint(-0.1, point[1]);
2178
                            else
2179
                                placeRunInputs.AddPoint(point[0], -0.1);
2180
                        }
2181
                        else if (_SameRunTargetLMSymbol != null)
2182
                            placeRunInputs.AddSymbolTarget(_SameRunTargetLMSymbol, point[0], point[1]);
2183
                        else
2184
                            placeRunInputs.AddPoint(point[0], point[1]);
2185
                    }
2186
                    else
2187
                    {
2188
                        if (_BranchTargetConnector != null)
2189
                        {
2190
                            placeRunInputs.AddConnectorTarget(_BranchTargetConnector, point[0], point[1]);
2191
                        }
2192
                        else
2193
                        {
2194
                            placeRunInputs.AddPoint(point[0], point[1]);
2195
                        }
2196
                    }
2197
                }
2198
                else
2199
                    placeRunInputs.AddPoint(point[0], point[1]);
2200
            }
2201
            _LMAItem _LMAItem = _placement.PIDCreateItem(lines[0].SPPID.MAPPINGNAME);
2202
            LMConnector _LMConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
2203

    
2204
            if (_LMConnector != null)
2205
            {
2206
                if (_SameRunTargetConnector != null)
2207
                {
2208
                    JoinPipeRun(_LMConnector.ModelItemID, _SameRunTargetConnector.ModelItemID);
2209
                }
2210
                else
2211
                {
2212
                    if (_SameRunTargetLMSymbol != null && bNeedRemodeling)
2213
                    {
2214
                        string symbolPath = string.Empty;
2215
                        #region get symbol path
2216
                        LMModelItem modelItem = dataSource.GetModelItem(_LMConnector.ModelItemID);
2217
                        foreach (LMRepresentation rep in modelItem.Representations)
2218
                        {
2219
                            if (!DBNull.Value.Equals(rep.get_FileName()) && !string.IsNullOrEmpty(rep.get_FileName()))
2220
                            {
2221
                                symbolPath = rep.get_FileName();
2222
                                break;
2223
                            }
2224
                        }
2225
                        ReleaseCOMObjects(modelItem);
2226
                        #endregion
2227

    
2228
                        double[] point = null;
2229
                        if (IsStart)
2230
                            point = points[points.Count - 1];
2231
                        else
2232
                            point = points[0];
2233

    
2234
                        _LMAItem = _placement.PIDCreateItem(symbolPath);
2235
                        placeRunInputs = new PlaceRunInputs();
2236
                        placeRunInputs.AddSymbolTarget(_SameRunTargetLMSymbol, point[0], point[1]);
2237
                        placeRunInputs.AddConnectorTarget(_LMConnector, point[0], point[1]);
2238
                        LMConnector _ZeroLengthLMConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
2239
                        if (_ZeroLengthLMConnector != null)
2240
                        {
2241
                            RemoveConnectorForReModelingLine(_LMConnector);
2242
                            ZeroLengthModelItemID.Add(_ZeroLengthLMConnector.ModelItemID);
2243
                            ReleaseCOMObjects(_ZeroLengthLMConnector);
2244
                        }
2245
                        ReleaseCOMObjects(placeRunInputs);
2246
                        ReleaseCOMObjects(_LMAItem);
2247
                    }
2248

    
2249
                    foreach (var item in lines)
2250
                        item.SPPID.ModelItemId = _LMConnector.ModelItemID;
2251
                }
2252

    
2253
                _LMConnector.Commit();
2254
                ReleaseCOMObjects(_LMConnector);
2255
            }
2256

    
2257
            ReleaseCOMObjects(placeRunInputs);
2258
            ReleaseCOMObjects(_LMAItem);
2259
            if (_BranchTargetConnector != null)
2260
                ReleaseCOMObjects(_BranchTargetConnector);
2261
            if (_SameRunTargetConnector != null)
2262
                ReleaseCOMObjects(_SameRunTargetConnector);
2263
            if (_SameRunTargetLMSymbol != null)
2264
                ReleaseCOMObjects(_SameRunTargetLMSymbol);
2265
            foreach (var item in connectorVertices)
2266
                ReleaseCOMObjects(item.Key);
2267
            foreach (var item in branchConnectorVertices)
2268
                ReleaseCOMObjects(item.Key);
2269
        }
2270

    
2271
        /// <summary>
2272
        /// EndBreak 모델링 메서드
2273
        /// </summary>
2274
        /// <param name="endBreak"></param>
2275
        private void EndBreakModeling(EndBreak endBreak)
2276
        {
2277
            object ownerObj = SPPIDUtil.FindObjectByUID(document, endBreak.OWNER);
2278
            object connectedItem = SPPIDUtil.FindObjectByUID(document, endBreak.PROPERTIES.Find(x => x.ATTRIBUTE == "Connected Item").VALUE);
2279
            LMConnector targetLMConnector = FindBreakLineTarget(ownerObj, connectedItem);
2280

    
2281
            if (targetLMConnector != null)
2282
            {
2283
                Array array = new double[] { 0, endBreak.SPPID.ORIGINAL_X, endBreak.SPPID.ORIGINAL_Y };
2284
                LMLabelPersist _LmLabelPersist = _placement.PIDPlaceLabel(endBreak.SPPID.MAPPINGNAME, ref array, Rotation: 0, LabeledItem: targetLMConnector.AsLMRepresentation(), IsLeaderVisible: true);
2285
            }
2286

    
2287
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetProgress, ++CurrentCount);
2288
        }
2289

    
2290
        private LMConnector ReModelingLMConnector(LMConnector connector)
2291
        {
2292
            string symbolPath = string.Empty;
2293
            #region get symbol path
2294
            LMModelItem modelItem = dataSource.GetModelItem(connector.ModelItemID);
2295
            foreach (LMRepresentation rep in modelItem.Representations)
2296
            {
2297
                if (!DBNull.Value.Equals(rep.get_FileName()) && !string.IsNullOrEmpty(rep.get_FileName()))
2298
                {
2299
                    symbolPath = rep.get_FileName();
2300
                    break;
2301
                }
2302
            }
2303
            #endregion
2304

    
2305
            LMConnector newConnector = null;
2306
            dynamic OID = connector.get_GraphicOID();
2307
            DependencyObject drawingObject = radApp.ActiveDocument.ActiveSheet.DrawingObjects[OID];
2308
            Ingr.RAD2D.LineStringGeometry2d lineStringGeometry = drawingObject.GetGeometry() as Ingr.RAD2D.LineStringGeometry2d;
2309
            int verticesCount = lineStringGeometry.VertexCount;
2310
            PlaceRunInputs placeRunInputs = new PlaceRunInputs();
2311
            _LMAItem _LMAItem = _placement.PIDCreateItem(symbolPath);
2312

    
2313
            if (Convert.ToBoolean(connector.get_IsZeroLength()))
2314
            {
2315
                double[] vertices = null;
2316
                lineStringGeometry.GetVertices(ref verticesCount, ref vertices);
2317
                double x = 0;
2318
                double y = 0;
2319
                lineStringGeometry.GetVertex(1, ref x, ref y);
2320

    
2321
                placeRunInputs.AddSymbolTarget(connector.ConnectItem1SymbolObject, x, y);
2322
                placeRunInputs.AddSymbolTarget(connector.ConnectItem2SymbolObject, x, y);
2323

    
2324
                _placement.PIDRemovePlacement(connector.AsLMRepresentation());
2325
                newConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
2326
            }
2327
            else
2328
            {
2329
                List<double[]> vertices = new List<double[]>();
2330
                for (int i = 1; i <= verticesCount; i++)
2331
                {
2332
                    double x = 0;
2333
                    double y = 0;
2334
                    lineStringGeometry.GetVertex(i, ref x, ref y);
2335
                    vertices.Add(new double[] { x, y });
2336
                }
2337

    
2338
                for (int i = 0; i < vertices.Count; i++)
2339
                {
2340
                    double[] points = vertices[i];
2341
                    if (i == 0)
2342
                    {
2343
                        if (connector.ConnectItem1SymbolObject != null)
2344
                            placeRunInputs.AddSymbolTarget(connector.ConnectItem1SymbolObject, points[0], points[1]);
2345
                        else
2346
                            placeRunInputs.AddPoint(points[0], points[1]);
2347
                    }
2348
                    else if (i == vertices.Count - 1)
2349
                    {
2350
                        if (connector.ConnectItem2SymbolObject != null)
2351
                            placeRunInputs.AddSymbolTarget(connector.ConnectItem2SymbolObject, points[0], points[1]);
2352
                        else
2353
                            placeRunInputs.AddPoint(points[0], points[1]);
2354
                    }
2355
                    else
2356
                        placeRunInputs.AddPoint(points[0], points[1]);
2357
                }
2358

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

    
2361
                _placement.PIDRemovePlacement(connector.AsLMRepresentation());
2362
                newConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
2363

    
2364
                foreach (var line in lines)
2365
                    line.SPPID.ModelItemId = newConnector.ModelItemID;
2366
            }
2367

    
2368

    
2369
            return newConnector;
2370
        }
2371

    
2372
        /// <summary>
2373
        /// SpecBreak Modeling 메서드
2374
        /// </summary>
2375
        /// <param name="specBreak"></param>
2376
        private void SpecBreakModeling(SpecBreak specBreak)
2377
        {
2378
            object upStreamObj = SPPIDUtil.FindObjectByUID(document, specBreak.UpStreamUID);
2379
            object downStreamObj = SPPIDUtil.FindObjectByUID(document, specBreak.DownStreamUID);
2380

    
2381
            if (upStreamObj != null &&
2382
                downStreamObj != null)
2383
            {
2384
                LMConnector targetLMConnector = FindBreakLineTarget(upStreamObj, downStreamObj);
2385

    
2386
                if (targetLMConnector != null)
2387
                {
2388
                    foreach (var attribute in specBreak.ATTRIBUTES)
2389
                    {
2390
                        AttributeMapping mapping = document.AttributeMappings.Find(x => x.UID == attribute.UID);
2391
                        if (mapping != null && !string.IsNullOrEmpty(mapping.SPPIDSYMBOLNAME) && mapping.SPPIDSYMBOLNAME != "None")
2392
                        {
2393
                            string MappingPath = mapping.SPPIDSYMBOLNAME;
2394
                            Array array = new double[] { 0, specBreak.SPPID.ORIGINAL_X, specBreak.SPPID.ORIGINAL_Y };
2395
                            LMLabelPersist _LmLabelPersist = _placement.PIDPlaceLabel(MappingPath, ref array, Rotation: specBreak.ANGLE, LabeledItem: targetLMConnector.AsLMRepresentation(), IsLeaderVisible: mapping.LeaderLine);
2396

    
2397
                            if (_LmLabelPersist != null)
2398
                            {
2399
                                ReleaseCOMObjects(_LmLabelPersist);
2400
                            }
2401
                        }
2402
                    }
2403
                    ReleaseCOMObjects(targetLMConnector);
2404
                }
2405
            }
2406
        }
2407

    
2408
        private LMConnector FindBreakLineTarget(object targetObj, object connectedObj)
2409
        {
2410
            LMConnector targetConnector = null;
2411
            Symbol targetSymbol = targetObj as Symbol;
2412
            Symbol connectedSymbol = connectedObj as Symbol;
2413
            Line targetLine = targetObj as Line;
2414
            Line connectedLine = connectedObj as Line;
2415
            if (targetSymbol != null && connectedSymbol != null)
2416
            {
2417
                LMSymbol targetLMSymbol = dataSource.GetSymbol(targetSymbol.SPPID.RepresentationId);
2418
                LMSymbol connectedLMSymbol = dataSource.GetSymbol(connectedSymbol.SPPID.RepresentationId);
2419

    
2420
                foreach (LMConnector connector in targetLMSymbol.Avoid1Connectors)
2421
                {
2422
                    if (connector.get_ItemStatus() != "Active")
2423
                        continue;
2424

    
2425
                    if (connector.ConnectItem1SymbolObject.Id == connectedLMSymbol.Id)
2426
                    {
2427
                        targetConnector = connector;
2428
                        break;
2429
                    }
2430
                    else if (connector.ConnectItem2SymbolObject.Id == connectedLMSymbol.Id)
2431
                    {
2432
                        targetConnector = connector;
2433
                        break;
2434
                    }
2435
                }
2436

    
2437
                foreach (LMConnector connector in targetLMSymbol.Avoid2Connectors)
2438
                {
2439
                    if (connector.get_ItemStatus() != "Active")
2440
                        continue;
2441

    
2442
                    if (connector.ConnectItem1SymbolObject.Id == connectedLMSymbol.Id)
2443
                    {
2444
                        targetConnector = connector;
2445
                        break;
2446
                    }
2447
                    else if (connector.ConnectItem2SymbolObject.Id == connectedLMSymbol.Id)
2448
                    {
2449
                        targetConnector = connector;
2450
                        break;
2451
                    }
2452
                }
2453

    
2454
                ReleaseCOMObjects(targetLMSymbol);
2455
                ReleaseCOMObjects(connectedLMSymbol);
2456
            }
2457
            else if (targetLine != null && connectedLine != null)
2458
            {
2459
                LMModelItem targetModelItem = dataSource.GetModelItem(targetLine.SPPID.ModelItemId);
2460
                LMModelItem connectedModelItem = dataSource.GetModelItem(connectedLine.SPPID.ModelItemId);
2461

    
2462
                if (targetModelItem != null && connectedModelItem != null)
2463
                {
2464
                    foreach (LMRepresentation rep in targetModelItem.Representations)
2465
                    {
2466
                        if (targetConnector != null)
2467
                            break;
2468

    
2469
                        if (rep.Attributes["RepresentationType"].get_Value() == "Connector" && rep.Attributes["ItemStatus"].get_Value() == "Active")
2470
                        {
2471
                            LMConnector _LMConnector = dataSource.GetConnector(rep.Id);
2472

    
2473
                            if (IsConnected(_LMConnector, connectedModelItem))
2474
                                targetConnector = _LMConnector;
2475
                            else
2476
                                ReleaseCOMObjects(_LMConnector);
2477
                        }
2478
                    }
2479

    
2480
                    ReleaseCOMObjects(targetModelItem);
2481
                }
2482
            }
2483
            else
2484
            {
2485
                LMSymbol connectedLMSymbol = null;
2486
                if (connectedSymbol != null)
2487
                    connectedLMSymbol = dataSource.GetSymbol(connectedSymbol.SPPID.RepresentationId);
2488
                else if (targetSymbol != null)
2489
                    connectedLMSymbol = dataSource.GetSymbol(targetSymbol.SPPID.RepresentationId);
2490
                else
2491
                {
2492

    
2493
                }
2494
                LMModelItem targetModelItem = null;
2495
                if (targetLine != null)
2496
                    targetModelItem = dataSource.GetModelItem(targetLine.SPPID.ModelItemId);
2497
                else if (connectedLine != null)
2498
                    targetModelItem = dataSource.GetModelItem(connectedLine.SPPID.ModelItemId);
2499
                else
2500
                {
2501

    
2502
                }
2503
                if (connectedLMSymbol != null && targetModelItem != null)
2504
                {
2505
                    foreach (LMConnector connector in connectedLMSymbol.Avoid1Connectors)
2506
                    {
2507
                        if (connector.get_ItemStatus() != "Active")
2508
                            continue;
2509

    
2510
                        if (IsConnected(connector, targetModelItem))
2511
                        {
2512
                            targetConnector = connector;
2513
                            break;
2514
                        }
2515
                    }
2516

    
2517
                    if (targetConnector == null)
2518
                    {
2519
                        foreach (LMConnector connector in connectedLMSymbol.Avoid2Connectors)
2520
                        {
2521
                            if (connector.get_ItemStatus() != "Active")
2522
                                continue;
2523

    
2524
                            if (IsConnected(connector, targetModelItem))
2525
                            {
2526
                                targetConnector = connector;
2527
                                break;
2528
                            }
2529
                        }
2530
                    }
2531
                }
2532

    
2533
            }
2534

    
2535
            return targetConnector;
2536
        }
2537

    
2538
        private bool IsConnected(LMConnector connector, LMModelItem modelItem)
2539
        {
2540
            bool result = false;
2541

    
2542
            foreach (LMRepresentation rep in modelItem.Representations)
2543
            {
2544
                if (result)
2545
                    break;
2546

    
2547
                if (rep.Attributes["RepresentationType"].get_Value() == "Connector" && rep.Attributes["ItemStatus"].get_Value() == "Active")
2548
                {
2549
                    LMConnector _LMConnector = dataSource.GetConnector(rep.Id);
2550

    
2551
                    if (_LMConnector.ConnectItem1SymbolObject != null &&
2552
                        connector.ConnectItem1SymbolObject != null &&
2553
                        _LMConnector.ConnectItem1SymbolObject.Id == connector.ConnectItem1SymbolObject.Id)
2554
                    {
2555
                        result = true;
2556
                        ReleaseCOMObjects(_LMConnector);
2557
                        break;
2558
                    }
2559
                    else if (_LMConnector.ConnectItem1SymbolObject != null &&
2560
                        connector.ConnectItem2SymbolObject != null &&
2561
                        _LMConnector.ConnectItem1SymbolObject.Id == connector.ConnectItem2SymbolObject.Id)
2562
                    {
2563
                        result = true;
2564
                        ReleaseCOMObjects(_LMConnector);
2565
                        break;
2566
                    }
2567
                    else if (_LMConnector.ConnectItem2SymbolObject != null &&
2568
                        connector.ConnectItem1SymbolObject != null &&
2569
                        _LMConnector.ConnectItem2SymbolObject.Id == connector.ConnectItem1SymbolObject.Id)
2570
                    {
2571
                        result = true;
2572
                        ReleaseCOMObjects(_LMConnector);
2573
                        break;
2574
                    }
2575
                    else if (_LMConnector.ConnectItem2SymbolObject != null &&
2576
                        connector.ConnectItem2SymbolObject != null &&
2577
                        _LMConnector.ConnectItem2SymbolObject.Id == connector.ConnectItem2SymbolObject.Id)
2578
                    {
2579
                        result = true;
2580
                        ReleaseCOMObjects(_LMConnector);
2581
                        break;
2582
                    }
2583

    
2584
                    ReleaseCOMObjects(_LMConnector);
2585
                }
2586
            }
2587

    
2588

    
2589
            return result;
2590
        }
2591

    
2592
        /// <summary>
2593
        /// FromModelItem을 ToModelItem으로 PipeRunJoin하는 메서드
2594
        /// </summary>
2595
        /// <param name="fromModelItemId"></param>
2596
        /// <param name="toModelItemId"></param>
2597
        private void JoinPipeRun(string fromModelItemId, string toModelItemId)
2598
        {
2599
            LMModelItem modelItem1 = dataSource.GetModelItem(toModelItemId);
2600
            _LMAItem item1 = modelItem1.AsLMAItem();
2601
            LMModelItem modelItem2 = dataSource.GetModelItem(fromModelItemId);
2602
            _LMAItem item2 = modelItem2.AsLMAItem();
2603

    
2604
            // item2가 item1으로 조인
2605
            _placement.PIDJoinRuns(ref item1, ref item2);
2606
            item1.Commit();
2607
            item2.Commit();
2608

    
2609
            List<Line> lines = SPPIDUtil.FindLinesByModelId(document, fromModelItemId);
2610
            foreach (var line in lines)
2611
                line.SPPID.ModelItemId = toModelItemId;
2612

    
2613
            ReleaseCOMObjects(modelItem1);
2614
            ReleaseCOMObjects(item1);
2615
            ReleaseCOMObjects(modelItem2);
2616
            ReleaseCOMObjects(item2);
2617
        }
2618

    
2619
        /// <summary>
2620
        /// PipeRun을 자동으로 Join하는 메서드
2621
        /// </summary>
2622
        /// <param name="modelItemId"></param>
2623
        private void AutoJoinPipeRun(string modelItemId)
2624
        {
2625
            LMModelItem modelItem = dataSource.GetModelItem(modelItemId);
2626
            _LMAItem item = modelItem.AsLMAItem();
2627
            if (modelItem.get_ItemStatus() == "Active")
2628
            {
2629
                string modelitemID = item.Id;
2630
                _placement.PIDAutoJoin(item, AutoJoinEndConstants.autoJoin_Both, ref item);
2631
                string afterModelItemID = item.Id;
2632

    
2633
                if (modelitemID != afterModelItemID)
2634
                {
2635
                    List<Line> lines = SPPIDUtil.FindLinesByModelId(document, modelitemID);
2636
                    foreach (var line in lines)
2637
                        line.SPPID.ModelItemId = afterModelItemID;
2638
                }
2639
                item.Commit();
2640
            }
2641

    
2642
            ReleaseCOMObjects(modelItem);
2643
            ReleaseCOMObjects(item);
2644
        }
2645

    
2646
        /// <summary>
2647
        /// LineRun에 있는 Line들을 Join하는 진입 메서드
2648
        /// </summary>
2649
        /// <param name="run"></param>
2650
        private void JoinRunLine(LineRun run)
2651
        {
2652
            string modelItemId = string.Empty;
2653
            foreach (var item in run.RUNITEMS)
2654
            {
2655
                if (item.GetType() == typeof(Line))
2656
                {
2657
                    Line line = item as Line;
2658
                    AutoJoinPipeRun(line.SPPID.ModelItemId);
2659
                    modelItemId = line.SPPID.ModelItemId;
2660

    
2661
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetProgress, ++CurrentCount);
2662
                }
2663
            }
2664
        }
2665

    
2666
        /// <summary>
2667
        /// PipeRun의 좌표를 가져오는 메서드
2668
        /// </summary>
2669
        /// <param name="modelId"></param>
2670
        /// <returns></returns>
2671
        private Dictionary<LMConnector, List<double[]>> GetPipeRunVertices(string modelId)
2672
        {
2673
            Dictionary<LMConnector, List<double[]>> connectorVertices = new Dictionary<LMConnector, List<double[]>>();
2674
            LMModelItem modelItem = dataSource.GetModelItem(modelId);
2675

    
2676
            if (modelItem != null)
2677
            {
2678
                foreach (LMRepresentation rep in modelItem.Representations)
2679
                {
2680
                    if (rep.Attributes["RepresentationType"].get_Value() == "Connector" && rep.Attributes["ItemStatus"].get_Value() == "Active")
2681
                    {
2682
                        LMConnector _LMConnector = dataSource.GetConnector(rep.Id);
2683
                        connectorVertices.Add(_LMConnector, new List<double[]>());
2684
                        dynamic OID = rep.get_GraphicOID();
2685
                        DependencyObject drawingObject = radApp.ActiveDocument.ActiveSheet.DrawingObjects[OID];
2686
                        Ingr.RAD2D.LineStringGeometry2d lineStringGeometry = drawingObject.GetGeometry() as Ingr.RAD2D.LineStringGeometry2d;
2687
                        int verticesCount = lineStringGeometry.VertexCount;
2688
                        double[] vertices = null;
2689
                        lineStringGeometry.GetVertices(ref verticesCount, ref vertices);
2690
                        for (int i = 0; i < verticesCount; i++)
2691
                        {
2692
                            double x = 0;
2693
                            double y = 0;
2694
                            lineStringGeometry.GetVertex(i + 1, ref x, ref y);
2695
                            connectorVertices[_LMConnector].Add(new double[] { Math.Round(x, 10), Math.Round(y, 10) });
2696
                        }
2697
                    }
2698
                }
2699

    
2700
                ReleaseCOMObjects(modelItem);
2701
            }
2702

    
2703
            return connectorVertices;
2704
        }
2705

    
2706
        /// <summary>
2707
        /// LMConnector들을 가져온다.
2708
        /// </summary>
2709
        /// <param name="modelId"></param>
2710
        /// <returns></returns>
2711
        private List<LMConnector> GetConnectors(string modelId)
2712
        {
2713
            List<LMConnector> connectors = new List<LMConnector>();
2714
            LMModelItem modelItem = dataSource.GetModelItem(modelId);
2715

    
2716
            if (modelItem != null)
2717
            {
2718
                foreach (LMRepresentation rep in modelItem.Representations)
2719
                {
2720
                    if (rep.Attributes["RepresentationType"].get_Value() == "Connector" && rep.Attributes["ItemStatus"].get_Value() == "Active")
2721
                    {
2722
                        LMConnector _LMConnector = dataSource.GetConnector(rep.Id);
2723
                        connectors.Add(_LMConnector);
2724
                    }
2725
                }
2726

    
2727
                ReleaseCOMObjects(modelItem);
2728
            }
2729

    
2730
            return connectors;
2731
        }
2732

    
2733
        /// <summary>
2734
        /// 좌표로 PipeRun의 Connector중에 어느 Connector에 가까운지/붙을지 가져오는 메서드 - 두점으로 라인의 교차점을 기준으로 구함
2735
        /// </summary>
2736
        /// <param name="connectorVertices"></param>
2737
        /// <param name="connX"></param>
2738
        /// <param name="connY"></param>
2739
        /// <param name="x2"></param>
2740
        /// <param name="y2"></param>
2741
        /// <returns></returns>
2742
        private LMConnector FindTargetLMConnector(Dictionary<LMConnector, List<double[]>> connectorVertices, double connX, double connY, double x2, double y2)
2743
        {
2744
            double length = double.MaxValue;
2745
            LMConnector targetConnector = null;
2746
            foreach (var item in connectorVertices)
2747
            {
2748
                List<double[]> points = item.Value;
2749
                for (int i = 0; i < points.Count - 1; i++)
2750
                {
2751
                    double[] point1 = points[i];
2752
                    double[] point2 = points[i + 1];
2753

    
2754
                    double maxLineX = Math.Max(point1[0], point2[0]);
2755
                    double minLineX = Math.Min(point1[0], point2[0]);
2756
                    double maxLineY = Math.Max(point1[1], point2[1]);
2757
                    double minLineY = Math.Min(point1[1], point2[1]);
2758

    
2759
                    SlopeType slope = SPPIDUtil.CalcSlope(minLineX, minLineY, maxLineX, maxLineY);
2760

    
2761
                    // 두직선의 교차점
2762
                    double[] crossingPoint = SPPIDUtil.CalcLineCrossingPoint(connX, connY, x2, y2, point1[0], point1[1], point2[0], point2[1]);
2763
                    if (crossingPoint != null)
2764
                    {
2765
                        double distance = SPPIDUtil.CalcPointToPointdDistance(connX, connY, crossingPoint[0], crossingPoint[1]);
2766
                        if (length >= distance)
2767
                        {
2768
                            if (slope == SlopeType.Slope &&
2769
                                minLineX <= crossingPoint[0] && maxLineX >= crossingPoint[0] &&
2770
                                minLineY <= crossingPoint[1] && maxLineY >= crossingPoint[1])
2771
                            {
2772
                                targetConnector = item.Key;
2773
                                length = distance;
2774
                            }
2775
                            else if (slope == SlopeType.HORIZONTAL &&
2776
                                minLineX <= crossingPoint[0] && maxLineX >= crossingPoint[0])
2777
                            {
2778
                                targetConnector = item.Key;
2779
                                length = distance;
2780
                            }
2781
                            else if (slope == SlopeType.VERTICAL &&
2782
                               minLineY <= crossingPoint[1] && maxLineY >= crossingPoint[1])
2783
                            {
2784
                                targetConnector = item.Key;
2785
                                length = distance;
2786
                            }
2787
                        }
2788
                    }
2789
                }
2790

    
2791

    
2792
            }
2793

    
2794
            if (targetConnector == null)
2795
            {
2796
                foreach (var item in connectorVertices)
2797
                {
2798
                    List<double[]> points = item.Value;
2799
                    foreach (var point in points)
2800
                    {
2801
                        double distance = SPPIDUtil.CalcPointToPointdDistance(connX, connY, point[0], point[1]);
2802
                        if (length >= distance)
2803
                        {
2804
                            targetConnector = item.Key;
2805
                            length = distance;
2806
                        }
2807
                    }
2808
                }
2809

    
2810
            }
2811

    
2812
            return targetConnector;
2813
        }
2814

    
2815
        /// <summary>
2816
        /// 좌표로 PipeRun의 Connector중에 어느 Connector에 가까운지/붙을지 가져오는 메서드 - 한점으로 제일 가까운 기준으로 구함(단순)
2817
        /// </summary>
2818
        /// <param name="connectorVertices"></param>
2819
        /// <param name="connX"></param>
2820
        /// <param name="connY"></param>
2821
        /// <returns></returns>
2822
        private LMConnector FindTargetLMConnectorByPoint(Dictionary<LMConnector, List<double[]>> connectorVertices, double connX, double connY)
2823
        {
2824
            double length = double.MaxValue;
2825
            LMConnector targetConnector = null;
2826
            foreach (var item in connectorVertices)
2827
            {
2828
                List<double[]> points = item.Value;
2829

    
2830
                foreach (double[] point in points)
2831
                {
2832
                    double distance = SPPIDUtil.CalcPointToPointdDistance(point[0], point[1], connX, connY);
2833
                    if (length >= distance)
2834
                    {
2835
                        targetConnector = item.Key;
2836
                        length = distance;
2837
                    }
2838
                }
2839
            }
2840

    
2841
            return targetConnector;
2842
        }
2843

    
2844
        /// <summary>
2845
        /// 좌표로 PipeRun의 Connector중에 어느 Connector에 가까운지/붙을지 가져오는 메서드 - 조건에 안맞아서 못찾을시 제일 가까운 점으로 가져오는 방식
2846
        /// </summary>
2847
        /// <param name="connectorVertices"></param>
2848
        /// <param name="connX"></param>
2849
        /// <param name="connY"></param>
2850
        /// <returns></returns>
2851
        private LMConnector FindTargetLMConnectorForLabel(Dictionary<LMConnector, List<double[]>> connectorVertices, double connX, double connY)
2852
        {
2853
            double length = double.MaxValue;
2854
            LMConnector targetConnector = null;
2855
            foreach (var item in connectorVertices)
2856
            {
2857
                List<double[]> points = item.Value;
2858
                for (int i = 0; i < points.Count - 1; i++)
2859
                {
2860
                    double[] point1 = points[i];
2861
                    double[] point2 = points[i + 1];
2862
                    double x1 = Math.Min(point1[0], point2[0]);
2863
                    double y1 = Math.Min(point1[1], point2[1]);
2864
                    double x2 = Math.Max(point1[0], point2[0]);
2865
                    double y2 = Math.Max(point1[1], point2[1]);
2866

    
2867
                    if ((x1 <= connX && x2 >= connX) ||
2868
                        (y1 <= connY && y2 >= connY))
2869
                    {
2870
                        double distance = SPPIDUtil.CalcPointToPointdDistance(point1[0], point1[1], connX, connY);
2871
                        if (length >= distance)
2872
                        {
2873
                            targetConnector = item.Key;
2874
                            length = distance;
2875
                        }
2876

    
2877
                        distance = SPPIDUtil.CalcPointToPointdDistance(point2[0], point2[1], connX, connY);
2878
                        if (length >= distance)
2879
                        {
2880
                            targetConnector = item.Key;
2881
                            length = distance;
2882
                        }
2883
                    }
2884
                }
2885
            }
2886

    
2887
            // 못찾았을때.
2888
            length = double.MaxValue;
2889
            if (targetConnector == null)
2890
            {
2891
                foreach (var item in connectorVertices)
2892
                {
2893
                    List<double[]> points = item.Value;
2894

    
2895
                    foreach (double[] point in points)
2896
                    {
2897
                        double distance = SPPIDUtil.CalcPointToPointdDistance(point[0], point[1], connX, connY);
2898
                        if (length >= distance)
2899
                        {
2900
                            targetConnector = item.Key;
2901
                            length = distance;
2902
                        }
2903
                    }
2904
                }
2905
            }
2906

    
2907
            return targetConnector;
2908
        }
2909

    
2910
        /// <summary>
2911
        /// Line Number Symbol을 실제로 Modeling하는 메서드
2912
        /// </summary>
2913
        /// <param name="lineNumber"></param>
2914
        private void LineNumberModeling(LineNumber lineNumber)
2915
        {
2916
            object obj = SPPIDUtil.FindObjectByUID(document, lineNumber.CONNLINE);
2917
            if (obj != null)
2918
            {
2919
                Line line = obj as Line;
2920
                Dictionary<LMConnector, List<double[]>> connectorVertices = GetPipeRunVertices(line.SPPID.ModelItemId);
2921
                LMConnector connectedLMConnector = FindTargetLMConnectorForLabel(connectorVertices, lineNumber.SPPID.ORIGINAL_X, lineNumber.SPPID.ORIGINAL_Y);
2922
                if (connectedLMConnector != null)
2923
                {
2924
                    double x = 0;
2925
                    double y = 0;
2926
                    CalcLabelLocation(ref x, ref y, lineNumber.SPPID.ORIGINAL_X, lineNumber.SPPID.ORIGINAL_Y, lineNumber.SPPIDLabelLocation, _ETCSetting.LineNumberLocation);
2927

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

    
2931
                    foreach (var item in connectorVertices)
2932
                        ReleaseCOMObjects(item.Key);
2933
                    if (_LmLabelPresist != null)
2934
                    {
2935
                        _LmLabelPresist.Commit();
2936
                        lineNumber.SPPID.RepresentationId = _LmLabelPresist.AsLMRepresentation().Id;
2937
                        ReleaseCOMObjects(_LmLabelPresist);
2938
                    }
2939
                    else
2940
                    {
2941

    
2942
                    }
2943
                }
2944
            }
2945

    
2946
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetProgress, ++CurrentCount);
2947
        }
2948

    
2949
        /// <summary>
2950
        /// Flow Mark Modeling
2951
        /// </summary>
2952
        /// <param name="line"></param>
2953
        private void FlowMarkModeling(Line line)
2954
        {
2955
            if (line.FLOWMARK && !string.IsNullOrEmpty(line.SPPID.ModelItemId) && !string.IsNullOrEmpty(_ETCSetting.FlowMarkSymbolPath))
2956
            {
2957
                SlopeType targetSlopeType = SPPIDUtil.CalcSlope(line.SPPID.START_X, line.SPPID.START_Y, line.SPPID.END_X, line.SPPID.END_Y);
2958
                string mappingPath = _ETCSetting.FlowMarkSymbolPath;
2959
                double percent = line.FLOWMARK_PERCENT;
2960
                double tempX = 0;
2961
                double tempY = 0;
2962

    
2963
                double gapX;
2964
                double gapY;
2965
                // ID2 기준의 Gap을 구함
2966
                if (percent == 0)
2967
                {
2968
                    gapX = 0;
2969
                    gapY = 0;
2970
                }
2971
                else
2972
                {
2973
                    gapX = Math.Abs(line.SPPID.START_X - line.SPPID.END_X) / 100 * percent;
2974
                    gapY = Math.Abs(line.SPPID.START_Y - line.SPPID.END_Y) / 100 * percent;
2975
                }
2976

    
2977
                if (line.SPPID.START_X < line.SPPID.END_X)
2978
                    tempX = line.SPPID.START_X + gapX;
2979
                else
2980
                    tempX = line.SPPID.START_X - gapX;
2981

    
2982
                if (line.SPPID.START_Y < line.SPPID.END_Y)
2983
                    tempY = line.SPPID.START_Y + gapY;
2984
                else
2985
                    tempY = line.SPPID.START_Y - gapY;
2986

    
2987

    
2988

    
2989
                Dictionary<LMConnector, List<double[]>> connectorVertices = GetPipeRunVertices(line.SPPID.ModelItemId);
2990
                LMConnector _TargetItem = null;
2991
                double distance = double.MaxValue;
2992
                double[] startPoint = null;
2993
                double[] endPoint = null;
2994
                // ID2의 기준 Gap으로 제일 가까운 Line 찾음(Slope도 같은조건)
2995
                foreach (var item in connectorVertices)
2996
                {
2997
                    for (int i = 0; i < item.Value.Count - 1; i++)
2998
                    {
2999
                        List<double[]> points = item.Value;
3000
                        double[] point1 = points[i];
3001
                        double[] point2 = points[i + 1];
3002

    
3003
                        SlopeType slopeType = SPPIDUtil.CalcSlope(point1[0], point1[1], point2[0], point2[1]);
3004
                        if (slopeType == targetSlopeType)
3005
                        {
3006
                            double result = SPPIDUtil.CalcPointToPointdDistance(point1[0], point1[1], tempX, tempY);
3007
                            if (result < distance)
3008
                            {
3009
                                distance = result;
3010
                                _TargetItem = item.Key;
3011

    
3012
                                startPoint = point1;
3013
                                endPoint = point2;
3014
                            }
3015

    
3016
                            result = SPPIDUtil.CalcPointToPointdDistance(point2[0], point2[1], tempX, tempY);
3017
                            if (result < distance)
3018
                            {
3019
                                distance = result;
3020
                                _TargetItem = item.Key;
3021

    
3022
                                startPoint = point1;
3023
                                endPoint = point2;
3024
                            }
3025
                        }
3026
                    }
3027
                }
3028

    
3029
                if (_TargetItem != null)
3030
                {
3031
                    double x = 0;
3032
                    double y = 0;
3033
                    double angle = 0;
3034
                    // SPPID 기준의 Gap으로 실 좌표를 구함
3035
                    if (percent == 0)
3036
                    {
3037
                        gapX = 0;
3038
                        gapY = 0;
3039
                    }
3040
                    else
3041
                    {
3042
                        gapX = Math.Abs(startPoint[0] - endPoint[0]) / 100 * percent;
3043
                        gapY = Math.Abs(startPoint[1] - endPoint[1]) / 100 * percent;
3044
                    }
3045

    
3046
                    if (startPoint[0] < endPoint[0])
3047
                        x = startPoint[0] + gapX;
3048
                    else
3049
                        x = startPoint[0] - gapX;
3050

    
3051
                    if (startPoint[1] < endPoint[1])
3052
                        y = startPoint[1] + gapY;
3053
                    else
3054
                        y = startPoint[1] - gapY;
3055

    
3056
                    if (targetSlopeType == SlopeType.HORIZONTAL)
3057
                    {
3058
                        if (startPoint[0] < endPoint[0])
3059
                            angle = 0;
3060
                        else
3061
                            angle = Math.PI;
3062
                    }
3063
                    // 90 270
3064
                    else if (targetSlopeType == SlopeType.VERTICAL)
3065
                    {
3066
                        if (startPoint[1] < endPoint[1])
3067
                            angle = 90 * Math.PI / 180;
3068
                        else
3069
                            angle = 270 * Math.PI / 180;
3070
                    }
3071

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

    
3074
                    if (_LMSymbol != null)
3075
                    {
3076
                        ReleaseCOMObjects(_LMSymbol);
3077
                    }
3078

    
3079
                }
3080

    
3081
                foreach (var item in connectorVertices)
3082
                    ReleaseCOMObjects(item.Key);
3083
            }
3084
        }
3085

    
3086
        /// <summary>
3087
        /// Line Number 기준으로 모든 Item에 Line Number의 Attribute Input
3088
        /// </summary>
3089
        /// <param name="lineNumber"></param>
3090
        private void InputLineNumberAttribute(LineNumber lineNumber)
3091
        {
3092
            foreach (LineRun run in lineNumber.RUNS)
3093
            {
3094
                foreach (var item in run.RUNITEMS)
3095
                {
3096
                    if (item.GetType() == typeof(Symbol))
3097
                    {
3098
                        Symbol symbol = item as Symbol;
3099
                        LMSymbol _LMSymbol = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
3100
                        if (_LMSymbol != null)
3101
                        {
3102
                            LMModelItem _LMModelItem = _LMSymbol.ModelItemObject;
3103

    
3104
                            if (_LMModelItem != null && _LMModelItem.get_ItemStatus() == "Active")
3105
                            {
3106
                                foreach (var attribute in lineNumber.ATTRIBUTES)
3107
                                {
3108
                                    LineNumberMapping mapping = document.LineNumberMappings.Find(x => x.UID == attribute.UID);
3109
                                    if (mapping != null && !string.IsNullOrEmpty(attribute.VALUE) && attribute.VALUE != "None")
3110
                                    {
3111
                                        LMAAttribute _LMAAttribute = _LMModelItem.Attributes[mapping.SPPIDATTRIBUTENAME];
3112
                                        if (_LMAAttribute != null)
3113
                                        {
3114
                                            if (DBNull.Value.Equals(_LMAAttribute.get_Value()))
3115
                                                _LMAAttribute.set_Value(attribute.VALUE);
3116
                                            else if (_LMAAttribute.get_Value() != attribute.VALUE)
3117
                                                _LMAAttribute.set_Value(attribute.VALUE);
3118
                                        }
3119
                                    }
3120
                                }
3121
                                _LMModelItem.Commit();
3122
                            }
3123
                            if (_LMModelItem != null)
3124
                                ReleaseCOMObjects(_LMModelItem);
3125
                        }
3126
                        if (_LMSymbol != null)
3127
                            ReleaseCOMObjects(_LMSymbol);
3128
                    }
3129
                    else if (item.GetType() == typeof(Line))
3130
                    {
3131
                        Line line = item as Line;
3132
                        if (line != null)
3133
                        {
3134
                            LMModelItem _LMModelItem = dataSource.GetModelItem(line.SPPID.ModelItemId);
3135
                            if (_LMModelItem != null && _LMModelItem.get_ItemStatus() == "Active")
3136
                            {
3137
                                foreach (var attribute in lineNumber.ATTRIBUTES)
3138
                                {
3139
                                    LineNumberMapping mapping = document.LineNumberMappings.Find(x => x.UID == attribute.UID);
3140
                                    if (mapping != null && !string.IsNullOrEmpty(attribute.VALUE) && attribute.VALUE != "None")
3141
                                    {
3142
                                        LMAAttribute _LMAAttribute = _LMModelItem.Attributes[mapping.SPPIDATTRIBUTENAME];
3143
                                        if (_LMAAttribute != null)
3144
                                        {
3145
                                            if (DBNull.Value.Equals(_LMAAttribute.get_Value()))
3146
                                                _LMAAttribute.set_Value(attribute.VALUE);
3147
                                            else if (_LMAAttribute.get_Value() != attribute.VALUE)
3148
                                                _LMAAttribute.set_Value(attribute.VALUE);
3149

    
3150
                                        }
3151
                                    }
3152
                                }
3153
                                _LMModelItem.Commit();
3154
                            }
3155
                            if (_LMModelItem != null)
3156
                                ReleaseCOMObjects(_LMModelItem);
3157
                        }
3158
                    }
3159
                }
3160
            }
3161

    
3162
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetProgress, ++CurrentCount);
3163
        }
3164

    
3165
        /// <summary>
3166
        /// Symbol Attribute 입력 메서드
3167
        /// </summary>
3168
        /// <param name="item"></param>
3169
        private void InputSymbolAttribute(object targetItem, List<BaseModel.Attribute> targetAttributes)
3170
        {
3171

    
3172
            // Object 아이템이 Symbol일 경우 Equipment일 경우 
3173
            string sRep = null;
3174
            if (targetItem.GetType() == typeof(Symbol))
3175
                sRep = ((Symbol)targetItem).SPPID.RepresentationId;
3176
            else if (targetItem.GetType() == typeof(Equipment))
3177
                sRep = ((Equipment)targetItem).SPPID.RepresentationId;
3178

    
3179
            if (!string.IsNullOrEmpty(sRep))
3180
            {
3181
                LMSymbol _LMSymbol = dataSource.GetSymbol(sRep);
3182
                LMModelItem _LMModelItem = _LMSymbol.ModelItemObject;
3183
                LMAAttributes _Attributes = _LMModelItem.Attributes;
3184

    
3185
                foreach (var item in targetAttributes)
3186
                {
3187
                    AttributeMapping mapping = document.AttributeMappings.Find(x => x.UID == item.UID);
3188
                    if (mapping != null && !string.IsNullOrEmpty(item.VALUE) && item.VALUE != "None")
3189
                    {
3190
                        LMAAttribute _Attribute = _Attributes[mapping.SPPIDATTRIBUTENAME];
3191
                        if (_Attribute != null)
3192
                            _Attribute.set_Value(item.VALUE);
3193
                    }
3194
                }
3195
                _LMModelItem.Commit();
3196

    
3197
                ReleaseCOMObjects(_Attributes);
3198
                ReleaseCOMObjects(_LMModelItem);
3199
                ReleaseCOMObjects(_LMSymbol);
3200
            }
3201

    
3202
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetProgress, ++CurrentCount);
3203
        }
3204

    
3205
        /// <summary>
3206
        /// Input SpecBreak Attribute
3207
        /// </summary>
3208
        /// <param name="specBreak"></param>
3209
        private void InputSpecBreakAttribute(SpecBreak specBreak)
3210
        {
3211
            object upStreamObj = SPPIDUtil.FindObjectByUID(document, specBreak.UpStreamUID);
3212
            object downStreamObj = SPPIDUtil.FindObjectByUID(document, specBreak.DownStreamUID);
3213

    
3214
            if (upStreamObj != null &&
3215
                downStreamObj != null)
3216
            {
3217
                LMConnector targetLMConnector = FindBreakLineTarget(upStreamObj, downStreamObj);
3218

    
3219
                if (targetLMConnector != null)
3220
                {
3221
                    foreach (LMLabelPersist _LMLabelPersist in targetLMConnector.LabelPersists)
3222
                    {
3223
                        string symbolPath = _LMLabelPersist.get_FileName();
3224
                        AttributeMapping mapping = document.AttributeMappings.Find(x => x.SPPIDSYMBOLNAME == symbolPath);
3225
                        if (mapping != null)
3226
                        {
3227
                            BaseModel.Attribute attribute = specBreak.ATTRIBUTES.Find(y => y.UID == mapping.UID);
3228
                            if (attribute != null && !string.IsNullOrEmpty(attribute.VALUE) && attribute.VALUE != "None")
3229
                            {
3230
                                string[] values = attribute.VALUE.Split(new char[] { ',' });
3231
                                if (values.Length == 2)
3232
                                {
3233
                                    string upStreamValue = values[0];
3234
                                    string downStreamValue = values[1];
3235

    
3236
                                    InputAttributeForSpecBreak(upStreamObj, downStreamObj, upStreamValue, downStreamValue, mapping.SPPIDATTRIBUTENAME);
3237
                                }
3238
                            }
3239
                        }
3240
                    }
3241

    
3242
                    ReleaseCOMObjects(targetLMConnector);
3243
                }
3244
            }
3245

    
3246

    
3247
            #region 내부에서만 쓰는 메서드
3248
            void InputAttributeForSpecBreak(object _upStreamObj, object _downStreamObj, string upStreamValue, string downStreamValue, string sppidAttributeName)
3249
            {
3250
                Symbol upStreamSymbol = _upStreamObj as Symbol;
3251
                Line upStreamLine = _upStreamObj as Line;
3252
                Symbol downStreamSymbol = _downStreamObj as Symbol;
3253
                Line downStreamLine = _downStreamObj as Line;
3254
                // 둘다 Line일 경우
3255
                if (upStreamLine != null && downStreamLine != null)
3256
                {
3257
                    InputLineAttributeForSpecBreakLine(upStreamLine, sppidAttributeName, upStreamValue);
3258
                    InputLineAttributeForSpecBreakLine(downStreamLine, sppidAttributeName, downStreamValue);
3259
                }
3260
                // 둘다 Symbol일 경우
3261
                else if (upStreamSymbol != null && downStreamSymbol != null)
3262
                {
3263
                    LMConnector zeroLenthConnector = FindBreakLineTarget(upStreamSymbol, downStreamSymbol);
3264
                    LMSymbol upStreamLMSymbol = dataSource.GetSymbol(upStreamSymbol.SPPID.RepresentationId);
3265
                    LMSymbol downStreamLMSymbol = dataSource.GetSymbol(downStreamSymbol.SPPID.RepresentationId);
3266

    
3267
                    foreach (LMConnector connector in upStreamLMSymbol.Avoid1Connectors)
3268
                    {
3269
                        if (connector.get_ItemStatus() != "Active")
3270
                            continue;
3271

    
3272
                        if (connector.Id != zeroLenthConnector.Id)
3273
                            InputLineAttributeForSpecBreakLMConnector(connector, sppidAttributeName, upStreamValue);
3274
                    }
3275

    
3276
                    foreach (LMConnector connector in upStreamLMSymbol.Avoid2Connectors)
3277
                    {
3278
                        if (connector.get_ItemStatus() != "Active")
3279
                            continue;
3280

    
3281
                        if (connector.Id != zeroLenthConnector.Id)
3282
                            InputLineAttributeForSpecBreakLMConnector(connector, sppidAttributeName, upStreamValue);
3283
                    }
3284

    
3285
                    foreach (LMConnector connector in downStreamLMSymbol.Avoid1Connectors)
3286
                    {
3287
                        if (connector.get_ItemStatus() != "Active")
3288
                            continue;
3289

    
3290
                        if (connector.Id != zeroLenthConnector.Id)
3291
                            InputLineAttributeForSpecBreakLMConnector(connector, sppidAttributeName, downStreamValue);
3292
                    }
3293

    
3294
                    foreach (LMConnector connector in downStreamLMSymbol.Avoid2Connectors)
3295
                    {
3296
                        if (connector.get_ItemStatus() != "Active")
3297
                            continue;
3298

    
3299
                        if (connector.Id != zeroLenthConnector.Id)
3300
                            InputLineAttributeForSpecBreakLMConnector(connector, sppidAttributeName, downStreamValue);
3301
                    }
3302

    
3303
                    ReleaseCOMObjects(zeroLenthConnector);
3304
                    ReleaseCOMObjects(upStreamLMSymbol);
3305
                    ReleaseCOMObjects(downStreamLMSymbol);
3306
                }
3307
                else if (upStreamSymbol != null && downStreamLine != null)
3308
                {
3309
                    LMConnector zeroLenthConnector = FindBreakLineTarget(upStreamSymbol, downStreamLine);
3310
                    InputLineAttributeForSpecBreakLine(downStreamLine, sppidAttributeName, downStreamValue);
3311
                    LMSymbol upStreamLMSymbol = dataSource.GetSymbol(upStreamSymbol.SPPID.RepresentationId);
3312

    
3313
                    foreach (LMConnector connector in upStreamLMSymbol.Avoid1Connectors)
3314
                    {
3315
                        if (connector.get_ItemStatus() != "Active")
3316
                            continue;
3317

    
3318
                        if (connector.Id != zeroLenthConnector.Id)
3319
                            InputLineAttributeForSpecBreakLMConnector(connector, sppidAttributeName, upStreamValue);
3320
                    }
3321

    
3322
                    foreach (LMConnector connector in upStreamLMSymbol.Avoid2Connectors)
3323
                    {
3324
                        if (connector.get_ItemStatus() != "Active")
3325
                            continue;
3326

    
3327
                        if (connector.Id != zeroLenthConnector.Id)
3328
                            InputLineAttributeForSpecBreakLMConnector(connector, sppidAttributeName, upStreamValue);
3329
                    }
3330

    
3331
                    ReleaseCOMObjects(zeroLenthConnector);
3332
                    ReleaseCOMObjects(upStreamLMSymbol);
3333
                }
3334
                else if (upStreamLine != null && downStreamSymbol != null)
3335
                {
3336
                    LMConnector zeroLenthConnector = FindBreakLineTarget(upStreamLine, downStreamSymbol);
3337
                    InputLineAttributeForSpecBreakLine(upStreamLine, sppidAttributeName, upStreamValue);
3338
                    LMSymbol downStreamLMSymbol = dataSource.GetSymbol(downStreamSymbol.SPPID.RepresentationId);
3339

    
3340
                    foreach (LMConnector connector in downStreamLMSymbol.Avoid1Connectors)
3341
                    {
3342
                        if (connector.get_ItemStatus() != "Active")
3343
                            continue;
3344

    
3345
                        if (connector.Id != zeroLenthConnector.Id)
3346
                            InputLineAttributeForSpecBreakLMConnector(connector, sppidAttributeName, downStreamValue);
3347
                    }
3348

    
3349
                    foreach (LMConnector connector in downStreamLMSymbol.Avoid2Connectors)
3350
                    {
3351
                        if (connector.get_ItemStatus() != "Active")
3352
                            continue;
3353

    
3354
                        if (connector.Id != zeroLenthConnector.Id)
3355
                            InputLineAttributeForSpecBreakLMConnector(connector, sppidAttributeName, downStreamValue);
3356
                    }
3357

    
3358
                    ReleaseCOMObjects(zeroLenthConnector);
3359
                    ReleaseCOMObjects(downStreamLMSymbol);
3360
                }
3361
            }
3362

    
3363
            void InputLineAttributeForSpecBreakLine(Line line, string attrName, string value)
3364
            {
3365
                LMModelItem _LMModelItem = dataSource.GetModelItem(line.SPPID.ModelItemId);
3366
                if (_LMModelItem != null && _LMModelItem.get_ItemStatus() == "Active")
3367
                {
3368
                    LMAAttribute _LMAAttribute = _LMModelItem.Attributes[attrName];
3369
                    if (_LMAAttribute != null)
3370
                    {
3371
                        if (DBNull.Value.Equals(_LMAAttribute.get_Value()))
3372
                            _LMAAttribute.set_Value(value);
3373
                        else if (_LMAAttribute.get_Value() != value)
3374
                            _LMAAttribute.set_Value(value);
3375
                    }
3376

    
3377
                    _LMModelItem.Commit();
3378
                }
3379
                if (_LMModelItem != null)
3380
                    ReleaseCOMObjects(_LMModelItem);
3381
            }
3382

    
3383
            void InputLineAttributeForSpecBreakLMConnector(LMConnector connector, string attrName, string value)
3384
            {
3385
                LMModelItem _LMModelItem = dataSource.GetModelItem(connector.ModelItemID);
3386
                if (_LMModelItem != null && _LMModelItem.get_ItemStatus() == "Active")
3387
                {
3388
                    LMAAttribute _LMAAttribute = _LMModelItem.Attributes[attrName];
3389
                    if (_LMAAttribute != null)
3390
                    {
3391
                        if (DBNull.Value.Equals(_LMAAttribute.get_Value()))
3392
                            _LMAAttribute.set_Value(value);
3393
                        else if (_LMAAttribute.get_Value() != value)
3394
                            _LMAAttribute.set_Value(value);
3395
                    }
3396

    
3397
                    _LMModelItem.Commit();
3398
                }
3399
                if (_LMModelItem != null)
3400
                    ReleaseCOMObjects(_LMModelItem);
3401
            }
3402
            #endregion
3403
        }
3404

    
3405
        /// <summary>
3406
        /// Text Modeling - Association일 경우는 Text대신 해당 맵핑된 Symbol로 모델링
3407
        /// </summary>
3408
        /// <param name="text"></param>
3409
        private void TextModeling(Text text)
3410
        {
3411
            LMSymbol _LMSymbol = null;
3412
            LMConnector connectedLMConnector = null;
3413
            //if (text.ASSOCIATION && !string.IsNullOrEmpty(text.OWNER) && text.OWNER != "None")
3414
            if (text.ASSOCIATION)
3415
            {
3416
                object owner = SPPIDUtil.FindObjectByUID(document, text.OWNER);
3417
                if (owner.GetType() == typeof(Symbol))
3418
                {
3419
                    Symbol symbol = owner as Symbol;
3420
                    _LMSymbol = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
3421
                    if (_LMSymbol != null)
3422
                    {
3423
                        BaseModel.Attribute attribute = symbol.ATTRIBUTES.Find(x => x.ASSOCITEM == text.UID);
3424
                        if (attribute != null && !string.IsNullOrEmpty(attribute.VALUE) && attribute.VALUE != "None")
3425
                        {
3426
                            AttributeMapping mapping = document.AttributeMappings.Find(x => x.UID == attribute.UID && !string.IsNullOrEmpty(x.SPPIDSYMBOLNAME));
3427

    
3428
                            if (mapping != null)
3429
                            {
3430
                                double x = 0;
3431
                                double y = 0;
3432

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

    
3436
                                LMLabelPersist _LMLabelPersist = _placement.PIDPlaceLabel(mapping.SPPIDSYMBOLNAME, ref array, Rotation: text.ANGLE, LabeledItem: _LMSymbol.AsLMRepresentation(), IsLeaderVisible: mapping.LeaderLine);
3437
                                if (_LMLabelPersist != null)
3438
                                {
3439
                                    _LMLabelPersist.Commit();
3440
                                    ReleaseCOMObjects(_LMLabelPersist);
3441
                                }
3442
                            }
3443
                        }
3444
                    }
3445
                }
3446
                else if (owner.GetType() == typeof(Line))
3447
                {
3448
                    Line line = owner as Line;
3449
                    Dictionary<LMConnector, List<double[]>> connectorVertices = GetPipeRunVertices(line.SPPID.ModelItemId);
3450
                    connectedLMConnector = FindTargetLMConnectorForLabel(connectorVertices, text.SPPID.ORIGINAL_X, text.SPPID.ORIGINAL_Y);
3451

    
3452
                    if (connectedLMConnector != null)
3453
                    {
3454
                        BaseModel.Attribute attribute = line.ATTRIBUTES.Find(x => x.ASSOCITEM == text.UID);
3455
                        if (attribute != null && !string.IsNullOrEmpty(attribute.VALUE) && attribute.VALUE != "None")
3456
                        {
3457
                            AttributeMapping mapping = document.AttributeMappings.Find(x => x.UID == attribute.UID && !string.IsNullOrEmpty(x.SPPIDSYMBOLNAME));
3458

    
3459
                            if (mapping != null)
3460
                            {
3461
                                double x = 0;
3462
                                double y = 0;
3463

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

    
3467
                                LMLabelPersist _LMLabelPersist = _placement.PIDPlaceLabel(mapping.SPPIDSYMBOLNAME, ref array, Rotation: text.ANGLE, LabeledItem: connectedLMConnector.AsLMRepresentation(), IsLeaderVisible: mapping.LeaderLine);
3468
                                if (_LMLabelPersist != null)
3469
                                {
3470
                                    _LMLabelPersist.Commit();
3471
                                    ReleaseCOMObjects(_LMLabelPersist);
3472
                                }
3473
                            }
3474
                        }
3475
                    }
3476
                }
3477
            }
3478
            else
3479
            {
3480
                LMItemNote _LMItemNote = null;
3481
                LMAAttribute _LMAAttribute = null;
3482

    
3483
                double x = 0;
3484
                double y = 0;
3485
                double angle = text.ANGLE;
3486
                CalcLabelLocation(ref x, ref y, text.SPPID.ORIGINAL_X, text.SPPID.ORIGINAL_Y, text.SPPIDLabelLocation, _ETCSetting.TextLocation);
3487

    
3488
                _LMSymbol = _placement.PIDPlaceSymbol(text.SPPID.MAPPINGNAME, x, y, Rotation: angle);
3489
                _LMSymbol.Commit();
3490
                _LMItemNote = _placement.PIDDataSource.GetItemNote(_LMSymbol.ModelItemID);
3491
                _LMItemNote.Commit();
3492
                _LMAAttribute = _LMItemNote.Attributes["Note.Body"];
3493
                _LMAAttribute.set_Value(text.VALUE);
3494
                _LMItemNote.Commit();
3495

    
3496
                if (_LMAAttribute != null)
3497
                    ReleaseCOMObjects(_LMAAttribute);
3498
                if (_LMItemNote != null)
3499
                    ReleaseCOMObjects(_LMItemNote);
3500
            }
3501
            if (_LMSymbol != null)
3502
                ReleaseCOMObjects(_LMSymbol);
3503

    
3504
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetProgress, ++CurrentCount);
3505
        }
3506

    
3507
        /// <summary>
3508
        /// Note Modeling
3509
        /// </summary>
3510
        /// <param name="note"></param>
3511
        private void NoteModeling(Note note)
3512
        {
3513
            LMSymbol _LMSymbol = null;
3514
            LMItemNote _LMItemNote = null;
3515
            LMAAttribute _LMAAttribute = null;
3516

    
3517
            if (string.IsNullOrEmpty(note.OWNER) || note.OWNER == "None")
3518
            {
3519
                double x = 0;
3520
                double y = 0;
3521

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

    
3524
                _LMSymbol = _placement.PIDPlaceSymbol(note.SPPID.MAPPINGNAME, x, y);
3525
                _LMSymbol.Commit();
3526
                _LMItemNote = _placement.PIDDataSource.GetItemNote(_LMSymbol.ModelItemID);
3527
                _LMItemNote.Commit();
3528
                _LMAAttribute = _LMItemNote.Attributes["Note.Body"];
3529
                _LMAAttribute.set_Value(note.VALUE);
3530
                _LMItemNote.Commit();
3531
            }
3532

    
3533
            if (_LMAAttribute != null)
3534
                ReleaseCOMObjects(_LMAAttribute);
3535
            if (_LMItemNote != null)
3536
                ReleaseCOMObjects(_LMItemNote);
3537
            if (_LMSymbol != null)
3538
                ReleaseCOMObjects(_LMSymbol);
3539

    
3540
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetProgress, ++CurrentCount);
3541
        }
3542

    
3543
        /// <summary>
3544
        /// Note Symbol Modeling
3545
        /// </summary>
3546
        /// <param name="symbol"></param>
3547
        private void NoteSymbolModeling(Symbol symbol)
3548
        {
3549
            if (symbol.TYPE == "Notes")
3550
            {
3551
                string mappingPath = symbol.SPPID.MAPPINGNAME;
3552
                double x = symbol.SPPID.ORIGINAL_X;
3553
                double y = symbol.SPPID.ORIGINAL_Y;
3554
                int mirror = 0;
3555
                double angle = symbol.ANGLE;
3556

    
3557
                LMSymbol _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle);
3558
                symbol.SPPID.RepresentationId = _LMSymbol.AsLMRepresentation().Id;
3559
                symbol.SPPID.ModelItemID = _LMSymbol.ModelItemID;
3560

    
3561
                ReleaseCOMObjects(_LMSymbol);
3562

    
3563
                InputSymbolAttribute(symbol, symbol.ATTRIBUTES);
3564
            }
3565
        }
3566

    
3567
        /// <summary>
3568
        /// Label의 좌표를 구하는 메서드(ID2 기준의 좌표 -> SPPID 좌표)
3569
        /// </summary>
3570
        /// <param name="x"></param>
3571
        /// <param name="y"></param>
3572
        /// <param name="originX"></param>
3573
        /// <param name="originY"></param>
3574
        /// <param name="SPPIDLabelLocation"></param>
3575
        /// <param name="location"></param>
3576
        private void CalcLabelLocation(ref double x, ref double y, double originX, double originY, SPPIDLabelLocationInfo SPPIDLabelLocation, Location location)
3577
        {
3578
            if (location == Location.None)
3579
            {
3580
                x = originX;
3581
                y = originY;
3582
            }
3583
            else
3584
            {
3585
                if (location.HasFlag(Location.Center))
3586
                {
3587
                    x = (SPPIDLabelLocation.X1 + SPPIDLabelLocation.X2) / 2;
3588
                    y = (SPPIDLabelLocation.Y1 + SPPIDLabelLocation.Y2) / 2;
3589
                }
3590

    
3591
                if (location.HasFlag(Location.Left))
3592
                    x = SPPIDLabelLocation.X1;
3593
                else if (location.HasFlag(Location.Right))
3594
                    x = SPPIDLabelLocation.X2;
3595

    
3596
                if (location.HasFlag(Location.Down))
3597
                    y = SPPIDLabelLocation.Y1;
3598
                else if (location.HasFlag(Location.Up))
3599
                    y = SPPIDLabelLocation.Y2;
3600
            }
3601
        }
3602

    
3603
        /// <summary>
3604
        /// Symbol의 우선순위 Modeling 목록을 가져온다.
3605
        /// 1. Angle Valve
3606
        /// 2. 3개로 이루어진 Symbol Group
3607
        /// </summary>
3608
        /// <returns></returns>
3609
        private List<Symbol> GetPrioritySymbol()
3610
        {
3611
            DataTable symbolTable = document.SymbolTable;
3612
            // List에 순서대로 쌓는다.
3613
            List<Symbol> symbols = new List<Symbol>();
3614

    
3615
            // Angle Valve 부터
3616
            foreach (var symbol in document.SYMBOLS.FindAll(x => x.CONNECTORS.FindAll(y => y.Index == 0).Count == 2))
3617
            {
3618
                if (!symbols.Contains(symbol))
3619
                {
3620
                    double originX = 0;
3621
                    double originY = 0;
3622

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

    
3627
                    SlopeType slopeType1 = SlopeType.None;
3628
                    SlopeType slopeType2 = SlopeType.None;
3629
                    foreach (Connector connector in symbol.CONNECTORS.FindAll(x => x.Index == 0))
3630
                    {
3631
                        double connectorX = 0;
3632
                        double connectorY = 0;
3633
                        SPPIDUtil.ConvertPointBystring(connector.CONNECTPOINT, ref connectorX, ref connectorY);
3634
                        if (slopeType1 == SlopeType.None)
3635
                            slopeType1 = SPPIDUtil.CalcSlope(originX, originY, connectorX, connectorY);
3636
                        else
3637
                            slopeType2 = SPPIDUtil.CalcSlope(originX, originY, connectorX, connectorY);
3638
                    }
3639

    
3640
                    if ((slopeType1 == SlopeType.VERTICAL && slopeType2 == SlopeType.HORIZONTAL) ||
3641
                        (slopeType2 == SlopeType.VERTICAL && slopeType1 == SlopeType.HORIZONTAL))
3642
                        symbols.Add(symbol);
3643
                }
3644
            }
3645

    
3646
            List<Symbol> tempSymbols = new List<Symbol>();
3647
            // Conn 갯수 기준
3648
            foreach (var item in document.SYMBOLS)
3649
            {
3650
                if (!symbols.Contains(item))
3651
                    tempSymbols.Add(item);
3652
            }
3653
            tempSymbols.Sort(SortSymbolPriority);
3654
            symbols.AddRange(tempSymbols);
3655

    
3656
            return symbols;
3657
        }
3658

    
3659
        private static int SortSymbolPriority(Symbol a, Symbol b)
3660
        {
3661
            int countA = a.CONNECTORS.FindAll(x => !string.IsNullOrEmpty(x.CONNECTEDITEM) && x.CONNECTEDITEM != "None").Count;
3662
            int countB = b.CONNECTORS.FindAll(x => !string.IsNullOrEmpty(x.CONNECTEDITEM) && x.CONNECTEDITEM != "None").Count;
3663
            int retval = countB.CompareTo(countA);
3664
            if (retval != 0)
3665
                return retval;
3666
            else
3667
                return a.SPPID.ORIGINAL_X.CompareTo(b.SPPID.ORIGINAL_X);
3668
        }
3669

    
3670
        /// <summary>
3671
        /// Graphic OID로 해당 Symbol의 크기를 구하여 Zoom
3672
        /// </summary>
3673
        /// <param name="graphicOID"></param>
3674
        /// <param name="milliseconds"></param>
3675
        private void ZoomObjectByGraphicOID(string graphicOID, int milliseconds = 150)
3676
        {
3677
            if (radApp.ActiveDocument.ActiveSheet.DrawingObjects[graphicOID] != null)
3678
            {
3679
                double minX = 0;
3680
                double minY = 0;
3681
                double maxX = 0;
3682
                double maxY = 0;
3683
                radApp.ActiveDocument.ActiveSheet.DrawingObjects[graphicOID].Range(out minX, out minY, out maxX, out maxY);
3684
                radApp.ActiveWindow.ZoomArea2(minX - 0.007, minY - 0.007, maxX + 0.007, maxY + 0.007, null);
3685

    
3686
                Thread.Sleep(milliseconds);
3687
            }
3688
        }
3689

    
3690
        /// <summary>
3691
        /// ComObject를 Release
3692
        /// </summary>
3693
        /// <param name="objVars"></param>
3694
        public void ReleaseCOMObjects(params object[] objVars)
3695
        {
3696
            int intNewRefCount = 0;
3697
            foreach (object obj in objVars)
3698
            {
3699
                if (!Information.IsNothing(obj) && System.Runtime.InteropServices.Marshal.IsComObject(obj))
3700
                    intNewRefCount = intNewRefCount + System.Runtime.InteropServices.Marshal.FinalReleaseComObject(obj);
3701
            }
3702
        }
3703
    }
3704
}
클립보드 이미지 추가 (최대 크기: 500 MB)