프로젝트

일반

사용자정보

통계
| 개정판:

hytos / DTI_PID / SPPIDConverter / AutoModeling.cs @ 2fac2754

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

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

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

    
62
            EquipCount = document.Equipments.Count;
63
            SymbolCount = document.SYMBOLS.Count;
64
            SymbolCount = SymbolCount * 3;
65
            
66
            foreach (LineNumber lineNumber in document.LINENUMBERS)
67
                foreach (LineRun run in lineNumber.RUNS)
68
                    foreach (var item in run.RUNITEMS)
69
                        if (item.GetType() == typeof(Line))
70
                            LineCount++;
71
            foreach (TrimLine trimLine in document.TRIMLINES)
72
                foreach (LineRun run in trimLine.RUNS)
73
                    foreach (var item in run.RUNITEMS)
74
                        if (item.GetType() == typeof(Line))
75
                            LineCount++;
76

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

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

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

    
99
                }
100
            }
101
        }
102

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

    
115
                CreateDocument(ref drawingNumber, ref drawingName);
116

    
117
                if (DocumentCoordinateCorrection())
118
                {
119
                    int AllCount = CalcProgressCount();
120

    
121
                    SplashScreenManager.ShowForm(typeof(SPPIDSplashScreen), true, true);
122
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetAllStep, AllCount);
123
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetDocumentName, DocumentLabelText);
124

    
125
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Priority Symbol Modeling");
126
                    List<Symbol> prioritySymbols = GetPrioritySymbol();
127
                    foreach (var item in prioritySymbols)
128
                        SymbolModelingByPriority(item);
129

    
130
                    // Equipment Modeling
131
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Equipments Modeling");
132
                    foreach (Equipment equipment in document.Equipments)
133
                        EquipmentModeling(equipment);
134

    
135
                    // LineRun Symbol Modeling
136
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Symbols Modeling");
137
                    foreach (LineNumber lineNumber in document.LINENUMBERS)
138
                        foreach (LineRun run in lineNumber.RUNS)
139
                            SymbolModelingByRun(run);
140
                    // TrimLineRun Symbol Modeling
141
                    foreach (TrimLine trimLine in document.TRIMLINES)
142
                        foreach (LineRun run in trimLine.RUNS)
143
                            SymbolModelingByRun(run);
144

    
145
                    // LineRun Line Modeling
146
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Lines Modeling");
147
                    foreach (LineNumber lineNumber in document.LINENUMBERS)
148
                        foreach (LineRun run in lineNumber.RUNS)
149
                            LineModelingByRun(run);
150
                    // TrimLineRun Line Modeling
151
                    foreach (TrimLine trimLine in document.TRIMLINES)
152
                        foreach (LineRun run in trimLine.RUNS)
153
                            LineModelingByRun(run);
154

    
155
                    // Branch Line Modeling
156
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Branch Lines Modeling");
157
                    foreach (var item in BranchLines)
158
                        BranchLineModeling(item);
159

    
160
                    // EndBreak Modeling
161
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "EndBreaks Modeling");
162
                    foreach (var item in document.EndBreaks)
163
                        EndBreakModeling(item);
164

    
165
                    // SpecBreak Modeling
166
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "SpecBreaks Modeling");
167
                    foreach (var item in document.SpecBreaks)
168
                        SpecBreakModeling(item);
169

    
170
                    // LineNumber Modeling
171
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "LineNumbers Modeling");
172
                    foreach (var item in document.LINENUMBERS)
173
                        LineNumberModeling(item);
174

    
175
                    // FlowMark Modeling
176
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Flow Mark Modeling");
177
                    foreach (var item in document.LINES)
178
                        FlowMarkModeling(item);
179

    
180
                    // Note Symbol Modeling
181
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Note Symbol Modeling");
182
                    foreach (var item in document.SYMBOLS)
183
                        NoteSymbolModeling(item);
184

    
185
                    // Note Modeling
186
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Notes Modeling");
187
                    foreach (var item in document.NOTES)
188
                        NoteModeling(item);
189

    
190
                    // Text Modeling
191
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Texts Modeling");
192
                    foreach (var item in document.TEXTINFOS)
193
                        TextModeling(item);
194

    
195
                    // Input LineNumber Attribute
196
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Set LineNumbers Attribute");
197
                    foreach (var item in document.LINENUMBERS)
198
                        InputLineNumberAttribute(item);
199

    
200
                    // Input Symbol Attribute
201
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Set Symbols Attribute");
202
                    foreach (var item in document.SYMBOLS)
203
                        InputSymbolAttribute(item, item.ATTRIBUTES);
204

    
205
                    // Input SpecBreak Attribute
206
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Set Symbols Attribute");
207
                    foreach (var item in document.SpecBreaks)
208
                        InputSpecBreakAttribute(item);
209

    
210
                    // Label Symbol Modeling
211
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Labels Modeling");
212
                    foreach (var item in document.SYMBOLS)
213
                        LabelSymbolModeling(item);
214

    
215
                    // LineRun Line Join
216
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Join LineRuns");
217
                    foreach (LineNumber lineNumber in document.LINENUMBERS)
218
                        foreach (LineRun run in lineNumber.RUNS)
219
                            JoinRunLine(run);
220
                    // TrimLineRun Line Join
221
                    foreach (TrimLine trimLine in document.TRIMLINES)
222
                        foreach (LineRun run in trimLine.RUNS)
223
                            JoinRunLine(run);
224

    
225
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetProgress, AllCount);
226
                }
227
            }
228
            catch (Exception ex)
229
            {
230
                System.Windows.Forms.MessageBox.Show(ex.Message + "\r\n" + ex.StackTrace);
231
            }
232
            finally
233
            {
234
                application.ActiveWindow.Fit();
235

    
236
                if (radApp.ActiveDocument != null)
237
                {
238
                    //radApp.ActiveDocument.Save();
239
                    //radApp.ActiveDocument.SaveOnClose = false;
240
                    //radApp.ActiveDocument.Close(false);
241

    
242
                    ReleaseCOMObjects(newDrawing);
243
                }
244

    
245
                ReleaseCOMObjects(dataSource);
246
                ReleaseCOMObjects(_placement);
247

    
248
                Project_DB.InsertDrawingInfo(document.PATH, drawingNumber, drawingName, document);
249
                //SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.ClearParent, null);
250
                SplashScreenManager.CloseForm(false);
251
            }
252
        }
253

    
254
        /// <summary>
255
        /// 도면 생성 메서드
256
        /// </summary>
257
        private void CreateDocument(ref string drawingNumber, ref string drawingName)
258
        {
259
            GetDrawingNameAndNumber(ref drawingName, ref drawingNumber);
260

    
261
            newDrawing = application.Drawings.Add(document.Unit, document.Template, drawingNumber, drawingName);
262
            application.ActiveWindow.Fit();
263
            Thread.Sleep(1000);
264
            application.ActiveWindow.Zoom = 2000;
265
            Thread.Sleep(2000);
266
        }
267

    
268
        /// <summary>
269
        /// DrawingName, DrawingNumber를 확인하여 중복이 있으면 _1을 붙이고 +1씩 한다.
270
        /// </summary>
271
        /// <param name="drawingName"></param>
272
        /// <param name="drawingNumber"></param>
273
        private void GetDrawingNameAndNumber(ref string drawingName, ref string drawingNumber)
274
        {
275
            LMDrawings drawings = new LMDrawings();
276
            drawings.Collect(dataSource);
277
            
278
            List<string> drawingNameList = new List<string>();
279
            List<string> drawingNumberList = new List<string>();
280

    
281
            foreach (LMDrawing item in drawings)
282
            {
283
                drawingNameList.Add(item.Attributes["Name"].get_Value().ToString());
284
                drawingNumberList.Add(item.Attributes["DrawingNumber"].get_Value().ToString());
285
            }
286

    
287
            int nameLength = drawingName.Length;
288
            while (drawingNameList.Contains(drawingName))
289
            {
290
                if (nameLength == drawingName.Length)
291
                    drawingName += "-1";
292
                else
293
                {
294
                    int index = Convert.ToInt32(drawingName.Remove(0, nameLength + 1));
295
                    drawingName = drawingName.Substring(0, nameLength + 1);
296
                    drawingName += ++index;
297
                }
298
            }
299

    
300
            int numberLength = drawingNumber.Length;
301
            while (drawingNameList.Contains(drawingNumber))
302
            {
303
                if (numberLength == drawingNumber.Length)
304
                    drawingNumber += "-1";
305
                else
306
                {
307
                    int index = Convert.ToInt32(drawingNumber.Remove(0, numberLength + 1));
308
                    drawingNumber = drawingNumber.Substring(0, numberLength + 1);
309
                    drawingNumber += ++index;
310
                }
311
            }
312

    
313
            ReleaseCOMObjects(drawings);
314
        }
315

    
316
        /// <summary>
317
        /// 도면 크기 구하는 메서드
318
        /// </summary>
319
        /// <returns></returns>
320
        private bool DocumentCoordinateCorrection()
321
        {
322
            if (Settings.Default.DrawingX != 0 && Settings.Default.DrawingY != 0)
323
            {
324
                document.SetSPPIDLocation(Settings.Default.DrawingX, Settings.Default.DrawingY);
325
                document.CoordinateCorrection();
326
                return true;
327
            }
328
            else
329
                return false;
330
        }
331

    
332
        /// <summary>
333
        /// 라인을 Run 단위로 모델링하는 진입 메서드
334
        /// </summary>
335
        /// <param name="run"></param>
336
        private void LineModelingByRun(LineRun run)
337
        {
338
            Line prevLine = null;
339
            List<Line> lines = new List<Line>();
340
            foreach (var item in run.RUNITEMS)
341
            {
342
                // Line일 경우
343
                if (item.GetType() == typeof(Line))
344
                {
345
                    Line line = item as Line;
346
                    if (prevLine == null)
347
                        lines.Add(line);
348
                    else if (prevLine != null)
349
                    {
350
                        if (prevLine.SPPID.MAPPINGNAME == line.SPPID.MAPPINGNAME)
351
                            lines.Add(line);
352
                        else
353
                        {
354
                            if (lines.Count > 0)
355
                            {
356
                                LineModeling(lines);
357
                                lines.Clear();
358
                            }
359
                            lines.Add(line);
360
                        }
361
                    }
362

    
363
                    prevLine = line;
364

    
365
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetProgress, ++CurrentCount);
366
                }
367
                // Symbol 일 경우
368
                else if (item.GetType() == typeof(Symbol))
369
                {
370
                    if (lines.Count > 0)
371
                    {
372
                        LineModeling(lines);
373
                        lines.Clear();
374
                    }
375
                }
376
            }
377

    
378
            if (lines.Count > 0)
379
                LineModeling(lines);
380
        }
381

    
382
        /// <summary>
383
        /// 심볼을 Run 단위로 모델링하는 진입 메서드
384
        /// </summary>
385
        /// <param name="run"></param>
386
        private void SymbolModelingByRun(LineRun run)
387
        {
388
            // 양끝 Symbol 검사 후 Line이 나올때까지만 Symbol Modeling
389
            if (run.RUNITEMS.Count > 0)
390
            {
391
                if (run.RUNITEMS[0].GetType() == typeof(Symbol))
392
                    SymbolModelingByRunStart(run.RUNITEMS[0] as Symbol, run);
393

    
394
                if (run.RUNITEMS[run.RUNITEMS.Count - 1].GetType() == typeof(Symbol))
395
                    SymbolModelingByRunEnd(run.RUNITEMS[run.RUNITEMS.Count - 1] as Symbol, run);
396
            }
397

    
398
            Symbol targetSymbol = null;
399
            foreach (var item in run.RUNITEMS)
400
            {
401
                if (item.GetType() == typeof(Symbol))
402
                {
403
                    Symbol symbol = item as Symbol;
404
                    SymbolModeling(symbol, targetSymbol);
405
                    targetSymbol = symbol;
406
                }
407
                else
408
                {
409
                    targetSymbol = null;
410
                }
411
            }
412
        }
413

    
414
        /// <summary>
415
        /// Run에 있는 심볼을 모델링하는데 기준이 Run의 시작점
416
        /// </summary>
417
        /// <param name="symbol"></param>
418
        /// <param name="run"></param>
419
        private void SymbolModelingByRunStart(Symbol symbol, LineRun run)
420
        {
421
            foreach (var connector in symbol.CONNECTORS)
422
            {
423
                object targetItem = SPPIDUtil.FindObjectByUID(document, connector.CONNECTEDITEM);
424
                if (targetItem != null &&
425
                    (targetItem.GetType() == typeof(Symbol) || targetItem.GetType() == typeof(Equipment)) &&
426
                    !IsSameLineRun(symbol, targetItem))
427
                {
428
                    SymbolModeling(symbol, targetItem as Symbol);
429
                    for (int i = 1; i < run.RUNITEMS.Count; i++)
430
                    {
431
                        object item = run.RUNITEMS[i];
432
                        if (item.GetType() == typeof(Symbol))
433
                            SymbolModeling(item as Symbol, run.RUNITEMS[i - 1] as Symbol);
434
                        else
435
                            break;
436
                    }
437
                    break;
438
                }
439
            }
440

    
441

    
442
        }
443

    
444
        /// <summary>
445
        /// Run에 있는 심볼을 모델링하는데 기준이 Run의 끝점
446
        /// </summary>
447
        /// <param name="symbol"></param>
448
        /// <param name="run"></param>
449
        private void SymbolModelingByRunEnd(Symbol symbol, LineRun run)
450
        {
451
            foreach (var connector in symbol.CONNECTORS)
452
            {
453
                object targetItem = SPPIDUtil.FindObjectByUID(document, connector.CONNECTEDITEM);
454
                if (targetItem != null &&
455
                    (targetItem.GetType() == typeof(Symbol) || targetItem.GetType() == typeof(Equipment)) &&
456
                    !IsSameLineRun(symbol, targetItem))
457
                {
458
                    SymbolModeling(symbol, targetItem as Symbol);
459
                    for (int i = run.RUNITEMS.Count - 2; i >= 0; i--)
460
                    {
461
                        object item = run.RUNITEMS[i];
462
                        if (item.GetType() == typeof(Symbol))
463
                            SymbolModeling(item as Symbol, run.RUNITEMS[i + 1] as Symbol);
464
                        else
465
                            break;
466
                    }
467
                    break;
468
                }
469
            }
470
        }
471

    
472
        /// <summary>
473
        /// 심볼을 실제로 Modeling 메서드
474
        /// </summary>
475
        /// <param name="symbol"></param>
476
        /// <param name="targetSymbol"></param>
477
        /// <param name="prevSymbol"></param>
478
        private void SymbolModeling(Symbol symbol, Symbol targetSymbol)
479
        {
480
#if DEBUG
481
            try
482
            {
483
#endif
484
                // OWNERSYMBOL Attribute, 값을 가지고 있을 경우
485
                BaseModel.Attribute itemAttribute = symbol.ATTRIBUTES.Find(attr => attr.ATTRIBUTE == "OWNERSYMBOL");
486
                if (itemAttribute != null && (string.IsNullOrEmpty(itemAttribute.VALUE) || itemAttribute.VALUE != "None"))
487
                    return;
488
                // 이미 모델링 됐을 경우
489
                else if (!string.IsNullOrEmpty(symbol.SPPID.RepresentationId))
490
                    return;
491

    
492
                LMSymbol _LMSymbol = null;
493

    
494
                string mappingPath = symbol.SPPID.MAPPINGNAME;
495
                double x = symbol.SPPID.ORIGINAL_X;
496
                double y = symbol.SPPID.ORIGINAL_Y;
497
                int mirror = 0;
498
                double angle = symbol.ANGLE;
499

    
500
                SPPIDUtil.ConvertGridPoint(ref x, ref y);
501

    
502
                // OPC 일경우 180도 일때 Mirror
503
                if (mappingPath.Contains("Piping OPC's") && angle == Math.PI)
504
                    mirror = 1;
505

    
506
                // Mirror 계산
507
                if (symbol.FLIP == 1)
508
                {
509
                    mirror = 1;
510
                    angle += Math.PI;
511
                }
512

    
513
                if (targetSymbol != null && !string.IsNullOrEmpty(targetSymbol.SPPID.RepresentationId))
514
                {
515
                    LMSymbol _TargetItem = dataSource.GetSymbol(targetSymbol.SPPID.RepresentationId);
516
                    Connector connector = SPPIDUtil.FindSymbolConnectorByUID(document, symbol.UID, targetSymbol);
517
                    if (connector != null)
518
                        GetTargetSymbolConnectorPoint(connector, targetSymbol, ref x, ref y);
519

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

    
522
                    if (_LMSymbol != null && _TargetItem != null)
523
                    {
524
                        symbol.SPPID.RepresentationId = _LMSymbol.AsLMRepresentation().Id;
525
                        LMConnector reModelingConnector = FindBreakLineTarget(symbol, targetSymbol);
526

    
527
                        if (reModelingConnector != null)
528
                            ReModelingLMConnector(reModelingConnector);
529
                    }
530

    
531
                    ReleaseCOMObjects(_TargetItem);
532
                }
533
                else
534
                    _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle);
535

    
536

    
537
                if (_LMSymbol != null)
538
                {
539
                    _LMSymbol.Commit();
540
                    symbol.SPPID.RepresentationId = _LMSymbol.AsLMRepresentation().Id;
541
                    symbol.SPPID.ModelItemID = _LMSymbol.ModelItemID;
542
                    symbol.SPPID.GraphicOID = _LMSymbol.get_GraphicOID();
543

    
544
                    foreach (var item in symbol.ChildSymbols)
545
                        CreateChildSymbol(item, _LMSymbol);
546
                }
547

    
548
                ReleaseCOMObjects(_LMSymbol);
549
                SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetProgress, ++CurrentCount);
550
#if DEBUG
551

    
552
            }
553
            catch (Exception ex)
554
            {
555
                System.Windows.Forms.MessageBox.Show(ex.StackTrace);
556
            }
557
#endif
558
        }
559

    
560
        /// <summary>
561
        /// ID2의 Symbol Width와 Height를 비교해서 상대적인 SPPID Connector좌표를 가져온다.
562
        /// </summary>
563
        /// <param name="targetConnector"></param>
564
        /// <param name="targetSymbol"></param>
565
        /// <param name="x"></param>
566
        /// <param name="y"></param>
567
        private void GetTargetSymbolConnectorPoint(Connector targetConnector, Symbol targetSymbol, ref double x, ref double y)
568
        {
569
            LMSymbol _TargetItem = dataSource.GetSymbol(targetSymbol.SPPID.RepresentationId);
570

    
571
            double[] range = null;
572
            List<double[]> points = new List<double[]>();
573
            GetSPPIDSymbolRangeAndConnectionPoints(targetSymbol, ref range, points);
574
            double x1 = range[0];
575
            double y1 = range[1];
576
            double x2 = range[2];
577
            double y2 = range[3];
578

    
579
            // Origin 기준 Connector의 위치차이
580
            double sceneX = 0;
581
            double sceneY = 0;
582
            SPPIDUtil.ConvertPointBystring(targetConnector.SCENECONNECTPOINT, ref sceneX, ref sceneY);
583
            double originX = 0;
584
            double originY = 0;
585
            SPPIDUtil.ConvertPointBystring(targetSymbol.ORIGINALPOINT, ref originX, ref originY);
586
            double gapX = originX - sceneX;
587
            double gapY = originY - sceneY;
588

    
589
            // SPPID Symbol과 ID2 심볼의 크기 차이
590
            double sizeWidth = 0;
591
            double sizeHeight = 0;
592
            SPPIDUtil.ConvertPointBystring(targetSymbol.SIZE, ref sizeWidth, ref sizeHeight);
593
            double percentX = (x2 - x1) / sizeWidth;
594
            double percentY = (y2 - y1) / sizeHeight;
595

    
596
            double SPPIDgapX = gapX * percentX;
597
            double SPPIDgapY = gapY * percentY;
598

    
599
            double[] SPPIDOriginPoint = new double[] { _TargetItem.get_XCoordinate() - SPPIDgapX, _TargetItem.get_YCoordinate() + SPPIDgapY };
600
            double distance = double.MaxValue;
601
            double[] resultPoint;
602
            foreach (var point in points)
603
            {
604
                double result = SPPIDUtil.CalcPointToPointdDistance(point[0], point[1], SPPIDOriginPoint[0], SPPIDOriginPoint[1]);
605
                if (distance > result)
606
                {
607
                    distance = result;
608
                    resultPoint = point;
609
                    x = point[0];
610
                    y = point[1];
611
                }
612
            }
613

    
614
            ReleaseCOMObjects(_TargetItem);
615
        }
616

    
617
        /// <summary>
618
        /// SPPID Symbol의 Range를 구한다.
619
        /// </summary>
620
        /// <param name="symbol"></param>
621
        /// <param name="range"></param>
622
        private void GetSPPIDSymbolRangeAndConnectionPoints(Symbol symbol, ref double[] range, List<double[]> points)
623
        {
624
            LMSymbol _TargetItem = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
625
            Ingr.RAD2D.Symbol2d symbol2d = radApp.ActiveDocument.ActiveSheet.DrawingObjects[_TargetItem.get_GraphicOID().ToString()];
626
            double x1 = 0;
627
            double y1 = 0;
628
            double x2 = 0;
629
            double y2 = 0;
630
            symbol2d.Range(out x1, out y1, out x2, out y2);
631
            range = new double[] { x1, y1, x2, y2 };
632

    
633
            for (int i = 1; i < int.MaxValue; i++)
634
            {
635
                double connX = 0;
636
                double connY = 0;
637
                if (_placement.PIDConnectPointLocation(_TargetItem, i, ref connX, ref connY))
638
                    points.Add(new double[] { connX, connY });
639
                else
640
                    break;
641
            }
642

    
643
            foreach (var childSymbol in symbol.ChildSymbols)
644
                GetSPPIDChildSymbolRange(childSymbol, ref range, points);
645

    
646
            ReleaseCOMObjects(_TargetItem);
647
        }
648

    
649
        /// <summary>
650
        /// Child Modeling 된 Symbol의 Range를 구한다.
651
        /// </summary>
652
        /// <param name="childSymbol"></param>
653
        /// <param name="range"></param>
654
        private void GetSPPIDChildSymbolRange(ChildSymbol childSymbol, ref double[] range, List<double[]> points)
655
        {
656
            LMSymbol _ChildSymbol = dataSource.GetSymbol(childSymbol.SPPID.RepresentationId);
657
            Ingr.RAD2D.Symbol2d symbol2d = radApp.ActiveDocument.ActiveSheet.DrawingObjects[_ChildSymbol.get_GraphicOID().ToString()];
658
            double x1 = 0;
659
            double y1 = 0;
660
            double x2 = 0;
661
            double y2 = 0;
662
            symbol2d.Range(out x1, out y1, out x2, out y2);
663
            range[0] = Math.Min(range[0], x1);
664
            range[1] = Math.Min(range[1], y1);
665
            range[2] = Math.Max(range[2], x2);
666
            range[3] = Math.Max(range[3], y2);
667

    
668
            for (int i = 1; i < int.MaxValue; i++)
669
            {
670
                double connX = 0;
671
                double connY = 0;
672
                if (_placement.PIDConnectPointLocation(_ChildSymbol, i, ref connX, ref connY))
673
                    points.Add(new double[] { connX, connY });
674
                else
675
                    break;
676
            }
677

    
678
            foreach (var loopChildSymbol in childSymbol.ChildSymbols)
679
                GetSPPIDChildSymbolRange(loopChildSymbol, ref range, points);
680

    
681
            ReleaseCOMObjects(_ChildSymbol);
682
        }
683

    
684
        /// <summary>
685
        /// Label Symbol Modeling
686
        /// </summary>
687
        /// <param name="symbol"></param>
688
        private void LabelSymbolModeling(Symbol symbol)
689
        {
690
            if (string.IsNullOrEmpty(symbol.SPPID.RepresentationId))
691
            {
692
                BaseModel.Attribute itemAttribute = symbol.ATTRIBUTES.Find(x => x.ATTRIBUTE == "OWNERSYMBOL");
693
                if (itemAttribute == null || string.IsNullOrEmpty(itemAttribute.VALUE) || itemAttribute.VALUE == "None")
694
                    return;
695
                Array points = new double[] { 0, symbol.SPPID.ORIGINAL_X, symbol.SPPID.ORIGINAL_Y };
696

    
697
                string symbolUID = itemAttribute.VALUE;
698
                object targetItem = SPPIDUtil.FindObjectByUID(document, symbolUID);
699
                if (targetItem != null &&
700
                    (targetItem.GetType() == typeof(Symbol) ||
701
                    targetItem.GetType() == typeof(Equipment)))
702
                {
703
                    // Object 아이템이 Symbol일 경우 Equipment일 경우 
704
                    string sRep = null;
705
                    if (targetItem.GetType() == typeof(Symbol))
706
                        sRep = ((Symbol)targetItem).SPPID.RepresentationId;
707
                    else if (targetItem.GetType() == typeof(Equipment))
708
                        sRep = ((Equipment)targetItem).SPPID.RepresentationId;
709
                    if (!string.IsNullOrEmpty(sRep))
710
                    {
711
                        // LEADER Line 검사
712
                        bool leaderLine = false;
713
                        SymbolMapping symbolMapping = document.SymbolMappings.Find(x => x.UID == symbol.DBUID);
714
                        if (symbolMapping != null)
715
                            leaderLine = symbolMapping.LEADERLINE;
716

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

    
721
                        //Leader 선 센터로
722
                        if (_LMLabelPresist != null)
723
                        {
724
                            // Target Item에 Label의 Attribute Input
725
                            InputSymbolAttribute(targetItem, symbol.ATTRIBUTES);
726

    
727
                            string OID = _LMLabelPresist.get_GraphicOID();
728
                            DependencyObject dependency = radApp.ActiveDocument.ActiveSheet.DrawingObjects[OID] as DependencyObject;
729
                            if (dependency != null)
730
                            {
731
                                bool result = false;
732
                                foreach (var attributes in dependency.AttributeSets)
733
                                {
734
                                    foreach (var attribute in attributes)
735
                                    {
736
                                        string name = attribute.Name;
737
                                        string value = attribute.GetValue().ToString();
738
                                        if (name == "DrawingItemType" && value == "LabelPersist")
739
                                        {
740
                                            foreach (DrawingObjectBase drawingObject in dependency.DrawingObjects)
741
                                            {
742
                                                if (drawingObject.Type == Ingr.RAD2D.ObjectType.igLineString2d)
743
                                                {
744
                                                    Ingr.RAD2D.LineString2d lineString2D = drawingObject as Ingr.RAD2D.LineString2d;
745
                                                    double prevX = _TargetItem.get_XCoordinate();
746
                                                    double prevY = _TargetItem.get_YCoordinate();
747
                                                    lineString2D.InsertVertex(lineString2D.VertexCount, prevX, prevY);
748
                                                    lineString2D.RemoveVertex(lineString2D.VertexCount);
749
                                                    result = true;
750
                                                    break;
751
                                                }
752
                                            }
753
                                        }
754

    
755
                                        if (result)
756
                                            break;
757
                                    }
758

    
759
                                    if (result)
760
                                        break;
761
                                }
762
                            }
763

    
764
                            _LMLabelPresist.Commit();
765
                            ReleaseCOMObjects(_LMLabelPresist);
766
                        }
767

    
768
                        ReleaseCOMObjects(_TargetItem);
769
                    }
770
                }
771
                else if (targetItem != null && targetItem.GetType() == typeof(Line))
772
                {
773
                    Line targetLine = targetItem as Line;
774
                    Dictionary<LMConnector, List<double[]>> connectorVertices = GetPipeRunVertices(targetLine.SPPID.ModelItemId);
775
                    LMConnector connectedLMConnector = FindTargetLMConnectorForLabel(connectorVertices, symbol.SPPID.ORIGINAL_X, symbol.SPPID.ORIGINAL_Y);
776
                    if (connectedLMConnector != null)
777
                    {
778
                        // LEADER Line 검사
779
                        bool leaderLine = false;
780
                        SymbolMapping symbolMapping = document.SymbolMappings.Find(x => x.UID == symbol.DBUID);
781
                        if (symbolMapping != null)
782
                            leaderLine = symbolMapping.LEADERLINE;
783

    
784
                        LMLabelPersist _LMLabelPresist = _placement.PIDPlaceLabel(symbol.SPPID.MAPPINGNAME, ref points, Rotation: 0, LabeledItem: connectedLMConnector.AsLMRepresentation(), IsLeaderVisible: leaderLine);
785
                        if (_LMLabelPresist != null)
786
                        {
787
                            _LMLabelPresist.Commit();
788
                            ReleaseCOMObjects(_LMLabelPresist);
789
                        }
790
                        ReleaseCOMObjects(connectedLMConnector);
791
                    }
792

    
793
                    foreach (var item in connectorVertices)
794
                        if (item.Key != null)
795
                            ReleaseCOMObjects(item.Key);
796
                }
797
            }
798

    
799
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetProgress, ++CurrentCount);
800
        }
801

    
802
        /// <summary>
803
        /// Equipment를 실제로 Modeling 메서드
804
        /// </summary>
805
        /// <param name="equipment"></param>
806
        private void EquipmentModeling(Equipment equipment)
807
        {
808
            if (!string.IsNullOrEmpty(equipment.SPPID.RepresentationId))
809
                return;
810

    
811
            LMSymbol _LMSymbol = null;
812
            LMSymbol targetItem = null;
813
            string mappingPath = equipment.SPPID.MAPPINGNAME;
814
            double x = equipment.SPPID.ORIGINAL_X;
815
            double y = equipment.SPPID.ORIGINAL_Y;
816
            int mirror = 0;
817
            double angle = equipment.ANGLE;
818

    
819
            SPPIDUtil.ConvertGridPoint(ref x, ref y);
820

    
821
            Connector connector = equipment.CONNECTORS.Find(conn => !string.IsNullOrEmpty(conn.CONNECTEDITEM) && conn.CONNECTEDITEM != "None");
822
            if (connector != null)
823
            {
824
                Equipment connEquipment = SPPIDUtil.FindObjectByUID(document, connector.CONNECTEDITEM) as Equipment;
825
                if (connEquipment != null)
826
                {
827
                    if (string.IsNullOrEmpty(connEquipment.SPPID.RepresentationId))
828
                        EquipmentModeling(connEquipment);
829

    
830
                    if (!string.IsNullOrEmpty(connEquipment.SPPID.RepresentationId))
831
                    {
832
                        targetItem = dataSource.GetSymbol(connEquipment.SPPID.RepresentationId);
833
                        if (targetItem != null)
834
                        {
835
                            _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle, TargetItem: targetItem);
836
                        }
837
                        else
838
                        {
839
                            _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle);
840
                        }
841
                    }
842
                    else
843
                    {
844
                        _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle);
845
                    }
846
                }
847
                else
848
                {
849
                    _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle);
850
                }
851
            }
852
            else
853
            {
854
                _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle);
855
            }
856

    
857
            if (_LMSymbol != null)
858
            {
859
                _LMSymbol.Commit();
860
                equipment.SPPID.RepresentationId = _LMSymbol.AsLMRepresentation().Id;
861
                equipment.SPPID.GraphicOID = _LMSymbol.get_GraphicOID();
862
                ReleaseCOMObjects(_LMSymbol);
863
            }
864

    
865
            if (targetItem != null)
866
            {
867
                ReleaseCOMObjects(targetItem);
868
            }
869
            
870
            ReleaseCOMObjects(_LMSymbol);
871

    
872
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetProgress, ++CurrentCount);
873
        }
874

    
875
        private void SymbolModelingByPriority(Symbol symbol)
876
        {
877
            // Angle, Center, 우선순위 모델링
878
            SymbolModeling(symbol, null);
879
            List<Symbol> group = new List<Symbol>() { symbol };
880
            SPPIDUtil.FindConnectedSymbolGroup(document, symbol, group);
881

    
882
            List<Symbol> endModeling = new List<Symbol>() { symbol };
883
            while (endModeling.Count != group.Count)
884
            {
885
                foreach (var item in group)
886
                {
887
                    if (!endModeling.Contains(item))
888
                    {
889
                        bool result = false;
890
                        foreach (var connector in item.CONNECTORS)
891
                        {
892
                            Symbol connSymbol = group.Find(x => x.UID == connector.CONNECTEDITEM);
893
                            if (connSymbol == item)
894
                                throw new Exception(connSymbol.UID);
895

    
896
                            if (connSymbol != null && endModeling.Contains(connSymbol))
897
                            {
898
                                SymbolModeling(item, connSymbol);
899
                                endModeling.Add(item);
900
                                result = true;
901
                                break;
902
                            }
903
                        }
904

    
905
                        if (result)
906
                            break;
907
                    }
908
                }
909
            }
910
        }
911

    
912
        /// <summary>
913
        /// 심볼을 실제로 Modeling할때 ChildSymbol이 있다면 Modeling하는 메서드
914
        /// </summary>
915
        /// <param name="childSymbol"></param>
916
        /// <param name="parentSymbol"></param>
917
        private void CreateChildSymbol(ChildSymbol childSymbol, LMSymbol parentSymbol)
918
        {
919
            Ingr.RAD2D.Symbol2d symbol2d = radApp.ActiveDocument.ActiveSheet.DrawingObjects[parentSymbol.get_GraphicOID().ToString()];
920
            double x1 = 0;
921
            double x2 = 0;
922
            double y1 = 0;
923
            double y2 = 0;
924
            symbol2d.Range(out x1, out y1, out x2, out y2);
925

    
926
            LMSymbol _LMSymbol = _placement.PIDPlaceSymbol(childSymbol.SPPID.MAPPINGNAME, (x1 + x2) / 2, (y1 + y2) / 2, TargetItem: parentSymbol);
927
            if (_LMSymbol != null)
928
            {
929
                childSymbol.SPPID.RepresentationId = _LMSymbol.AsLMRepresentation().Id;
930
                foreach (var item in childSymbol.ChildSymbols)
931
                    CreateChildSymbol(item, _LMSymbol);
932
            }
933
            
934

    
935
            ReleaseCOMObjects(_LMSymbol);
936
        }
937

    
938
        /// <summary>
939
        /// item이 TargetItem과 같은 LineRun에 있는지 검사
940
        /// </summary>
941
        /// <param name="item"></param>
942
        /// <param name="targetItem"></param>
943
        /// <returns></returns>
944
        private bool IsSameLineRun(object item, object targetItem)
945
        {
946
            foreach (var lineNumber in document.LINENUMBERS)
947
            {
948
                foreach (var run in lineNumber.RUNS)
949
                {
950
                    foreach (var runItem in run.RUNITEMS)
951
                    {
952
                        if (runItem == item)
953
                        {
954
                            foreach (var findItem in run.RUNITEMS)
955
                            {
956
                                if (findItem == targetItem)
957
                                {
958
                                    return true;
959
                                }
960
                            }
961

    
962
                            return false;
963

    
964
                        }
965
                    }
966
                }
967
            }
968

    
969
            return false;
970
        }
971

    
972
        /// <summary>
973
        /// Line을 실제로 모델링하는 메서드
974
        /// </summary>
975
        /// <param name="lines"></param>
976
        private void LineModeling(List<Line> lines)
977
        {
978
            try
979
            {
980
                _LMAItem _LMAItem = _placement.PIDCreateItem(lines[0].SPPID.MAPPINGNAME);
981
                PlaceRunInputs placeRunInputs = new PlaceRunInputs();
982
                LMSymbol _LMSymbol1 = null;
983
                LMSymbol _LMSymbol2 = null;
984
                Dictionary<LMConnector, List<double[]>> connectorVertices1 = new Dictionary<LMConnector, List<double[]>>();
985
                LMConnector targetConnector1 = null;
986
                Dictionary<LMConnector, List<double[]>> connectorVertices2 = new Dictionary<LMConnector, List<double[]>>();
987
                LMConnector targetConnector2 = null;
988

    
989
                Line startBranchLine = null;
990
                Line endBranchLine = null;
991

    
992
                // Type, Line, TargetObjet, x, y
993
                List<Tuple<string, Line, object, double, double>> linePointInfo = new List<Tuple<string, Line, object, double, double>>();
994
                // Point 정리
995
                for (int i = 0; i < lines.Count; i++)
996
                {
997
                    Line line = lines[i];
998
                    if (i == 0 || i + 1 != lines.Count)
999
                    {
1000
                        // 시작점에 연결된 Symbol 찾기
1001
                        object connItem = SPPIDUtil.FindObjectByUID(document, line.CONNECTORS[0].CONNECTEDITEM);
1002
                        if (connItem != null && connItem.GetType() == typeof(Symbol))
1003
                        {
1004
                            Symbol symbol1 = connItem as Symbol;
1005
                            _LMSymbol1 = GetTargetSymbol(symbol1, line);
1006
                            if (_LMSymbol1 != null)
1007
                            {
1008
                                double x = line.SPPID.START_X;
1009
                                double y = line.SPPID.START_Y;
1010
                                Connector connector = SPPIDUtil.FindSymbolConnectorByUID(document, line.UID, symbol1);
1011
                                if (connector != null)
1012
                                {
1013
                                    GetTargetSymbolConnectorPoint(connector, symbol1, ref x, ref y);
1014
                                    line.SPPID.START_X = x;
1015
                                    line.SPPID.START_Y = y;
1016
                                }
1017

    
1018
                                linePointInfo.Add(new Tuple<string, Line, object, double, double>("SYMBOL", line, _LMSymbol1, x, y));
1019
                            }
1020
                            else
1021
                                linePointInfo.Add(new Tuple<string, Line, object, double, double>(null, line, null, line.SPPID.START_X, line.SPPID.START_Y));
1022
                        }
1023
                        else if (connItem != null && connItem.GetType() == typeof(Line) && !lines.Contains(connItem))
1024
                        {
1025
                            connectorVertices1 = GetPipeRunVertices(((Line)connItem).SPPID.ModelItemId);
1026
                            targetConnector1 = FindTargetLMConnector(connectorVertices1, line.SPPID.START_X, line.SPPID.START_Y, line.SPPID.END_X, line.SPPID.END_Y);
1027

    
1028
                            if (targetConnector1 != null)
1029
                                linePointInfo.Add(new Tuple<string, Line, object, double, double>("LINE", line, targetConnector1, line.SPPID.START_X, line.SPPID.START_Y));
1030
                            else
1031
                            {
1032
                                startBranchLine = connItem as Line;
1033
                                linePointInfo.Add(new Tuple<string, Line, object, double, double>(null, line, null, line.SPPID.START_X, line.SPPID.START_Y));
1034
                            }
1035
                        }
1036
                        else
1037
                            linePointInfo.Add(new Tuple<string, Line, object, double, double>(null, line, null, line.SPPID.START_X, line.SPPID.START_Y));
1038

    
1039
                    }
1040
                    if (i + 1 == lines.Count)
1041
                    {
1042
                        // 끝점에 연결된 Symbol 찾기
1043
                        object connItem = SPPIDUtil.FindObjectByUID(document, line.CONNECTORS[1].CONNECTEDITEM);
1044

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

    
1048
                        if (connItem != null && connItem.GetType() == typeof(Symbol))
1049
                        {
1050
                            Symbol symbol2 = connItem as Symbol;
1051
                            _LMSymbol2 = GetTargetSymbol(connItem as Symbol, line);
1052
                            if (_LMSymbol2 != null)
1053
                            {
1054
                                double x = line.SPPID.END_X;
1055
                                double y = line.SPPID.END_Y;
1056
                                Connector connector = SPPIDUtil.FindSymbolConnectorByUID(document, line.UID, symbol2);
1057
                                if (connector != null)
1058
                                {
1059
                                    GetTargetSymbolConnectorPoint(connector, symbol2, ref x, ref y);
1060
                                    line.SPPID.END_X = x;
1061
                                    line.SPPID.END_Y = y;
1062
                                }
1063

    
1064
                                linePointInfo.Add(new Tuple<string, Line, object, double, double>("SYMBOL", line, _LMSymbol2, x, y));
1065
                            }
1066
                            else
1067
                                linePointInfo.Add(new Tuple<string, Line, object, double, double>(null, line, null, line.SPPID.END_X, line.SPPID.END_Y));
1068
                        }
1069
                        else if (connItem != null && connItem.GetType() == typeof(Line) && !lines.Contains(connItem))
1070
                        {
1071
                            connectorVertices2 = GetPipeRunVertices(((Line)connItem).SPPID.ModelItemId);
1072
                            targetConnector2 = FindTargetLMConnector(connectorVertices2, line.SPPID.END_X, line.SPPID.END_Y, line.SPPID.START_X, line.SPPID.START_Y);
1073

    
1074
                            if (targetConnector2 != null)
1075
                                linePointInfo.Add(new Tuple<string, Line, object, double, double>("LINE", line, targetConnector2, line.SPPID.END_X, line.SPPID.END_Y));
1076
                            else
1077
                            {
1078
                                endBranchLine = connItem as Line;
1079
                                linePointInfo.Add(new Tuple<string, Line, object, double, double>(null, line, null, line.SPPID.END_X, line.SPPID.END_Y));
1080
                            }
1081
                        }
1082
                        else
1083
                            linePointInfo.Add(new Tuple<string, Line, object, double, double>(null, line, null, line.SPPID.END_X, line.SPPID.END_Y));
1084
                    }
1085
                }
1086

    
1087
                double prevX = double.NaN;
1088
                double prevY = double.NaN;
1089
                SlopeType prevSlopeType = SlopeType.None;
1090
                for (int i = 0; i < linePointInfo.Count; i++)
1091
                {
1092
                    Tuple<string, Line, object, double, double> item = linePointInfo[i];
1093
                    Line line = item.Item2;
1094
                    double x = item.Item4;
1095
                    double y = item.Item5;
1096
                    SlopeType slopeType = SPPIDUtil.CalcSlope(line.SPPID.START_X, line.SPPID.START_Y, line.SPPID.END_X, line.SPPID.END_Y);
1097
                    // Symbol일 경우 바로 Input Point
1098
                    if (item.Item1 == "SYMBOL")
1099
                    {
1100
                        LMSymbol targetSymbol = item.Item3 as LMSymbol;
1101
                        placeRunInputs.AddSymbolTarget(targetSymbol, x, y);
1102
                    }
1103
                    else
1104
                    {
1105
                        SPPIDUtil.ConvertGridPoint(ref x, ref y);
1106
                        // i == 0은 그대로 사용
1107
                        if (i != 0)
1108
                        {
1109
                            Tuple<string, Line, object, double, double> prevItem = linePointInfo[i - 1];
1110
                            // y 좌표가 같아야함 및 Symbol 좌표가 정확하지 않으므로 한번더 보정
1111
                            if (prevSlopeType == SlopeType.HORIZONTAL)
1112
                            {
1113
                                y = prevY;
1114
                                SPPIDUtil.ConvertGridPointOnlyOnePoint(ref y);
1115
                            }
1116
                            else if (prevSlopeType == SlopeType.VERTICAL)
1117
                            {
1118
                                x = prevX;
1119
                                SPPIDUtil.ConvertGridPointOnlyOnePoint(ref x);
1120
                            }
1121

    
1122
                            // 마지막이 Symbol일 경우는 Symbol의 좌표를 따라감
1123
                            if (i + 1 == linePointInfo.Count - 1 && linePointInfo[i + 1].Item1 == "SYMBOL")
1124
                            {
1125
                                Line nextLine = linePointInfo[i + 1].Item2;
1126
                                SlopeType nextSlopeType = SPPIDUtil.CalcSlope(nextLine.SPPID.START_X, nextLine.SPPID.START_Y, nextLine.SPPID.END_X, nextLine.SPPID.END_Y);
1127
                                if (slopeType == SlopeType.HORIZONTAL)
1128
                                    y = linePointInfo[i + 1].Item5;
1129
                                else if (slopeType == SlopeType.VERTICAL)
1130
                                    x = linePointInfo[i + 1].Item4;
1131
                            }
1132
                        }
1133

    
1134
                        if (item.Item1 == "LINE")
1135
                        {
1136
                            LMConnector targetConnector = item.Item3 as LMConnector;
1137
                            placeRunInputs.AddConnectorTarget(targetConnector, x, y);
1138
                        }
1139
                            
1140
                        else
1141
                            placeRunInputs.AddPoint(x, y);
1142
                    }
1143

    
1144
                    prevX = x;
1145
                    prevY = y;
1146
                    prevSlopeType = slopeType;
1147
                }
1148

    
1149
                LMConnector _lMConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
1150

    
1151
                if (_lMConnector != null)
1152
                {
1153
                    bool bStart = false;
1154
                    bool bEnd = false;
1155

    
1156
                    NeedReModeling(lines[0], _LMSymbol1, ref bStart);
1157
                    NeedReModeling(lines[lines.Count - 1], _LMSymbol2, ref bEnd);
1158

    
1159
                    if (bStart || bEnd)
1160
                        ReModelingLine(lines, _lMConnector, _LMSymbol1, _LMSymbol2, bStart, bEnd);
1161
                    else
1162
                    {
1163
                        foreach (var line in lines)
1164
                            line.SPPID.ModelItemId = _lMConnector.ModelItemID;
1165
                        _lMConnector.Commit();
1166
                    }
1167

    
1168
                    if (startBranchLine != null || endBranchLine != null)
1169
                        BranchLines.Add(new Tuple<string, Line, Line>(lines[0].SPPID.ModelItemId, startBranchLine, endBranchLine));
1170
                }
1171

    
1172
                if (_LMSymbol1 != null)
1173
                    ReleaseCOMObjects(_LMSymbol1);
1174
                if (_LMSymbol2 != null)
1175
                    ReleaseCOMObjects(_LMSymbol2);
1176
                if (targetConnector1 != null)
1177
                    ReleaseCOMObjects(targetConnector1);
1178
                if (targetConnector2 != null)
1179
                    ReleaseCOMObjects(targetConnector2);
1180
                foreach (var item in connectorVertices1)
1181
                    ReleaseCOMObjects(item.Key);
1182
                foreach (var item in connectorVertices2)
1183
                    ReleaseCOMObjects(item.Key);
1184

    
1185
                ReleaseCOMObjects(_lMConnector);
1186
                ReleaseCOMObjects(placeRunInputs);
1187
                ReleaseCOMObjects(_LMAItem);
1188
            }
1189
            catch (Exception ex)
1190
            {
1191
                StringBuilder sb = new StringBuilder();
1192
                foreach (var item in lines)
1193
                {
1194
                    sb.AppendLine(item.UID);
1195
                }
1196
                throw new Exception(sb.ToString());
1197
            }
1198
        }
1199

    
1200
        private void NeedReModeling(Line line, LMSymbol symbol, ref bool result)
1201
        {
1202
            if (symbol != null)
1203
            {
1204
                string repID = symbol.AsLMRepresentation().Id;
1205
                string symbolUID = SPPIDUtil.FindSymbolByRepresentationID(document, repID).UID;
1206
                string lineUID = line.UID;
1207

    
1208
                SpecBreak startSpecBreak = document.SpecBreaks.Find(x =>
1209
                (x.DownStreamUID == symbolUID || x.UpStreamUID == symbolUID) &&
1210
                (x.DownStreamUID == lineUID || x.UpStreamUID == lineUID));
1211

    
1212
                EndBreak startEndBreak = document.EndBreaks.Find(x =>
1213
                (x.OWNER == symbolUID || x.PROPERTIES.Find(y => y.ATTRIBUTE == "Connected Item").VALUE == symbolUID) &&
1214
                (x.OWNER == lineUID || x.PROPERTIES.Find(y => y.ATTRIBUTE == "Connected Item").VALUE == lineUID));
1215

    
1216
                if (startSpecBreak != null || startEndBreak != null)
1217
                    result = true;
1218
            }
1219
        }
1220
        
1221
        /// <summary>
1222
        /// Symbol에 붙을 경우 Line을 Remodeling 한다.
1223
        /// </summary>
1224
        /// <param name="lines"></param>
1225
        /// <param name="prevLMConnector"></param>
1226
        /// <param name="startSymbol"></param>
1227
        /// <param name="endSymbol"></param>
1228
        private void ReModelingLine(List<Line> lines, LMConnector prevLMConnector, LMSymbol startSymbol, LMSymbol endSymbol, bool bStart, bool bEnd)
1229
        {
1230
            string symbolPath = string.Empty;
1231
            #region get symbol path
1232
            LMModelItem modelItem = dataSource.GetModelItem(prevLMConnector.ModelItemID);
1233
            foreach (LMRepresentation rep in modelItem.Representations)
1234
            {
1235
                if (!DBNull.Value.Equals(rep.get_FileName()) && !string.IsNullOrEmpty(rep.get_FileName()))
1236
                {
1237
                    symbolPath = rep.get_FileName();
1238
                    break;
1239
                }
1240
            }
1241
            #endregion
1242
            _LMAItem _LMAItem = _placement.PIDCreateItem(symbolPath);
1243
            LMConnector newConnector = null;
1244
            dynamic OID = prevLMConnector.get_GraphicOID();
1245
            DependencyObject drawingObject = radApp.ActiveDocument.ActiveSheet.DrawingObjects[OID];
1246
            Ingr.RAD2D.LineStringGeometry2d lineStringGeometry = drawingObject.GetGeometry() as Ingr.RAD2D.LineStringGeometry2d;
1247
            int verticesCount = lineStringGeometry.VertexCount;
1248
            PlaceRunInputs placeRunInputs = new PlaceRunInputs();
1249
            
1250
            List<double[]> vertices = new List<double[]>();
1251
            for (int i = 1; i <= verticesCount; i++)
1252
            {
1253
                double x = 0;
1254
                double y = 0;
1255
                lineStringGeometry.GetVertex(i, ref x, ref y);
1256
                vertices.Add(new double[] { x, y });
1257
            }
1258

    
1259
            for (int i = 0; i < vertices.Count; i++)
1260
            {
1261
                double[] points = vertices[i];
1262
                // 시작 심볼이 있고 첫번째 좌표일 때
1263
                if (startSymbol != null && i == 0)
1264
                {
1265
                    if (bStart)
1266
                    {
1267
                        SlopeType slopeType = SPPIDUtil.CalcSlope(points[0], points[1], vertices[i + 1][0], vertices[i + 1][1]);
1268
                        if (slopeType == SlopeType.HORIZONTAL)
1269
                            placeRunInputs.AddPoint(points[0], -0.1);
1270
                        else if (slopeType == SlopeType.VERTICAL)
1271
                            placeRunInputs.AddPoint(-0.1, points[1]);
1272
                        else
1273
                            placeRunInputs.AddPoint(points[0], -0.1);
1274

    
1275
                        placeRunInputs.AddPoint(points[0], points[1]);
1276
                    }
1277
                    else
1278
                    {
1279
                        placeRunInputs.AddSymbolTarget(startSymbol, points[0], points[1]);
1280
                    }
1281
                }
1282
                // 마지막 심볼이 있고 마지막 좌표일 때
1283
                else if (endSymbol != null && i == vertices.Count - 1)
1284
                {
1285
                    if (bEnd)
1286
                    {
1287
                        placeRunInputs.AddPoint(points[0], points[1]);
1288

    
1289
                        SlopeType slopeType = SPPIDUtil.CalcSlope(points[0], points[1], vertices[i - 1][0], vertices[i - 1][1]);
1290
                        if (slopeType == SlopeType.HORIZONTAL)
1291
                            placeRunInputs.AddPoint(points[0], -0.1);
1292
                        else if (slopeType == SlopeType.VERTICAL)
1293
                            placeRunInputs.AddPoint(-0.1, points[1]);
1294
                        else
1295
                            placeRunInputs.AddPoint(points[0], -0.1);
1296
                    }
1297
                    else
1298
                    {
1299
                        placeRunInputs.AddSymbolTarget(endSymbol, points[0], points[1]);
1300
                    }
1301
                }
1302
                // 첫번째이며 시작 심볼이 아니고 Connecotr일 경우
1303
                else if (i == 0 && prevLMConnector.ConnectItem1SymbolObject != null)
1304
                    placeRunInputs.AddSymbolTarget(prevLMConnector.ConnectItem1SymbolObject, points[0], points[1]);
1305
                // 마지막이며 마지막 심볼이 아니고 Connecotr일 경우
1306
                else if (i == vertices.Count - 1 && prevLMConnector.ConnectItem2SymbolObject != null)
1307
                    placeRunInputs.AddSymbolTarget(prevLMConnector.ConnectItem2SymbolObject, points[0], points[1]);
1308
                else
1309
                    placeRunInputs.AddPoint(points[0], points[1]);
1310
            }
1311

    
1312
            _placement.PIDRemovePlacement(prevLMConnector.AsLMRepresentation());
1313
            newConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
1314

    
1315
            ReleaseCOMObjects(placeRunInputs);
1316
            ReleaseCOMObjects(_LMAItem);
1317
            ReleaseCOMObjects(modelItem);
1318

    
1319
            if (newConnector != null)
1320
            {
1321
                if (startSymbol != null && bStart)
1322
                {
1323
                    _LMAItem = _placement.PIDCreateItem(symbolPath);
1324
                    placeRunInputs = new PlaceRunInputs();
1325
                    placeRunInputs.AddSymbolTarget(startSymbol, vertices[0][0], vertices[0][1]);
1326
                    placeRunInputs.AddConnectorTarget(newConnector, vertices[0][0], vertices[0][1]);
1327
                    LMConnector _LMConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
1328
                    if (_LMConnector != null)
1329
                    {
1330
                        RemoveConnectorForReModelingLine(newConnector);
1331
                        ZeroLengthModelItemID.Add(_LMConnector.ModelItemID);
1332
                        ReleaseCOMObjects(_LMConnector);
1333
                    }
1334
                    ReleaseCOMObjects(placeRunInputs);
1335
                    ReleaseCOMObjects(_LMAItem);
1336
                }
1337

    
1338
                if (endSymbol != null && bEnd)
1339
                {
1340
                    if (startSymbol != null)
1341
                    {
1342
                        Dictionary<LMConnector, List<double[]>> dicVertices = GetPipeRunVertices(newConnector.ModelItemID);
1343
                        newConnector = dicVertices.First().Key;
1344
                    }
1345

    
1346
                    _LMAItem = _placement.PIDCreateItem(symbolPath);
1347
                    placeRunInputs = new PlaceRunInputs();
1348
                    placeRunInputs.AddSymbolTarget(endSymbol, vertices[vertices.Count - 1][0], vertices[vertices.Count - 1][1]);
1349
                    placeRunInputs.AddConnectorTarget(newConnector, vertices[vertices.Count - 1][0], vertices[vertices.Count - 1][1]);
1350
                    LMConnector _LMConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
1351
                    if (_LMConnector != null)
1352
                    {
1353
                        RemoveConnectorForReModelingLine(newConnector);
1354
                        ZeroLengthModelItemID.Add(_LMConnector.ModelItemID);
1355
                        ReleaseCOMObjects(_LMConnector);
1356
                    }
1357
                    ReleaseCOMObjects(placeRunInputs);
1358
                    ReleaseCOMObjects(_LMAItem);
1359
                }
1360

    
1361
                foreach (var line in lines)
1362
                    line.SPPID.ModelItemId = newConnector.ModelItemID;
1363
                ReleaseCOMObjects(newConnector);
1364
            }
1365

    
1366
            ReleaseCOMObjects(modelItem);
1367
        }
1368

    
1369
        /// <summary>
1370
        /// Remodeling 과정에서 생긴 불필요한 Connector 제거
1371
        /// </summary>
1372
        /// <param name="connector"></param>
1373
        private void RemoveConnectorForReModelingLine(LMConnector connector)
1374
        {
1375
            Dictionary<LMConnector, List<double[]>> dicVertices = GetPipeRunVertices(connector.ModelItemID);
1376
            foreach (var item in dicVertices)
1377
            {
1378
                bool result = false;
1379
                foreach (var point in item.Value)
1380
                {
1381
                    if (point[0] < 0 || point[1] < 0)
1382
                    {
1383
                        result = true;
1384
                        _placement.PIDRemovePlacement(item.Key.AsLMRepresentation());
1385
                        break;
1386
                    }
1387
                }
1388

    
1389
                if (result)
1390
                    break;
1391
            }
1392
            foreach (var item in dicVertices)
1393
                ReleaseCOMObjects(item.Key);
1394
        }
1395

    
1396
        /// <summary>
1397
        /// Symbol이 모델링된 SPPPID Symbol Object를 반환 - 연결된 Symbol이 ChildSymbol일 수도 있기때문에 메서드 개발
1398
        /// </summary>
1399
        /// <param name="symbol"></param>
1400
        /// <param name="line"></param>
1401
        /// <returns></returns>
1402
        private LMSymbol GetTargetSymbol(Symbol symbol, Line line)
1403
        {
1404
            LMSymbol _LMSymbol = null;
1405
            foreach (var connector in symbol.CONNECTORS)
1406
            {
1407
                if (connector.CONNECTEDITEM == line.UID)
1408
                {
1409
                    if (connector.Index == 0)
1410
                        _LMSymbol = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
1411
                    else
1412
                    {
1413
                        ChildSymbol child = null;
1414
                        foreach (var childSymbol in symbol.ChildSymbols)
1415
                        {
1416
                            if (childSymbol.Connectors.Contains(connector))
1417
                                child = childSymbol;
1418
                            else
1419
                                child = GetChildSymbolByConnector(childSymbol, connector);
1420

    
1421
                            if (child != null)
1422
                                break;
1423
                        }
1424

    
1425
                        if (child != null)
1426
                            _LMSymbol = dataSource.GetSymbol(child.SPPID.RepresentationId);
1427
                    }
1428

    
1429
                    break;  
1430
                }
1431
            }
1432

    
1433
            return _LMSymbol;
1434
        }
1435

    
1436
        /// <summary>
1437
        /// Connector를 가지고 있는 ChildSymbol Object 반환
1438
        /// </summary>
1439
        /// <param name="item"></param>
1440
        /// <param name="connector"></param>
1441
        /// <returns></returns>
1442
        private ChildSymbol GetChildSymbolByConnector(ChildSymbol item, Connector connector)
1443
        {
1444
            foreach (var childSymbol in item.ChildSymbols)
1445
            {
1446
                if (childSymbol.Connectors.Contains(connector))
1447
                    return childSymbol;
1448
                else
1449
                    return GetChildSymbolByConnector(childSymbol, connector);
1450
            }
1451

    
1452
            return null;
1453
        }
1454

    
1455
        /// <summary>
1456
        /// Branch 라인을 다시 모델링하는 진입 메서드
1457
        /// </summary>
1458
        /// <param name="branch"></param>
1459
        private void BranchLineModeling(Tuple<string, Line, Line> branch)
1460
        {
1461
            List<Line> lines = SPPIDUtil.FindLinesByModelId(document, branch.Item1);
1462

    
1463
            Dictionary<LMConnector, List<double[]>> connectorVertices = GetPipeRunVertices(branch.Item1);
1464
            LMConnector _StartConnector = null;
1465
            LMConnector _EndConnector = null;
1466
            double lengthStart = double.MaxValue;
1467
            double lengthEnd = double.MaxValue;
1468
            List<double[]> startPoints = new List<double[]>();
1469
            List<double[]> endPoints = new List<double[]>();
1470

    
1471
            foreach (var item in connectorVertices)
1472
            {
1473
                foreach (var point in item.Value)
1474
                {
1475
                    // Start Point가 Branch
1476
                    if (branch.Item2 != null)
1477
                    {
1478
                        Line targetLine = branch.Item2;
1479
                        double distance = SPPIDUtil.CalcLineToPointDistance(targetLine.SPPID.START_X, targetLine.SPPID.START_Y, targetLine.SPPID.END_X, targetLine.SPPID.END_Y, point[0], point[1]);
1480
                        if (lengthStart > distance)
1481
                        {
1482
                            _StartConnector = item.Key;
1483
                            lengthStart = distance;
1484
                            startPoints = item.Value;
1485
                        }
1486
                    }
1487
                    // End Point가 Branch
1488
                    if (branch.Item3 != null)
1489
                    {
1490
                        Line targetLine = branch.Item3;
1491
                        double distance = SPPIDUtil.CalcLineToPointDistance(targetLine.SPPID.START_X, targetLine.SPPID.START_Y, targetLine.SPPID.END_X, targetLine.SPPID.END_Y, point[0], point[1]);
1492
                        if (lengthEnd > distance)
1493
                        {
1494
                            _EndConnector = item.Key;
1495
                            lengthEnd = distance;
1496
                            endPoints = item.Value;
1497
                        }
1498
                    }
1499
                }
1500
            }
1501
            #region Branch가 양쪽 전부일 때
1502
            if (_StartConnector != null && _StartConnector == _EndConnector)
1503
            {
1504
                _placement.PIDRemovePlacement(_StartConnector.AsLMRepresentation());
1505

    
1506
                _LMAItem _LMAItem = _placement.PIDCreateItem(lines[0].SPPID.MAPPINGNAME);
1507
                PlaceRunInputs placeRunInputs = new PlaceRunInputs();
1508

    
1509
                Dictionary<LMConnector, List<double[]>> startConnectorVertices = GetPipeRunVertices(branch.Item2.SPPID.ModelItemId);
1510
                LMConnector _StartTargetConnector = FindTargetLMConnector(startConnectorVertices, startPoints[0][0], startPoints[0][1], startPoints[1][0], startPoints[1][1]);
1511
                Dictionary<LMConnector, List<double[]>> endConnectorVertices = GetPipeRunVertices(branch.Item3.SPPID.ModelItemId);
1512
                LMConnector _EndTargetConnector = FindTargetLMConnector(endConnectorVertices,
1513
                   startPoints[startPoints.Count - 1][0],
1514
                   startPoints[startPoints.Count - 1][1],
1515
                   startPoints[startPoints.Count - 2][0],
1516
                   startPoints[startPoints.Count - 2][1]);
1517

    
1518
                for (int i = 0; i < startPoints.Count; i++)
1519
                {
1520
                    double[] point = startPoints[i];
1521
                    if (i == 0)
1522
                        placeRunInputs.AddConnectorTarget(_StartTargetConnector, point[0], point[1]);
1523
                    else if (i == startPoints.Count - 1)
1524
                        placeRunInputs.AddConnectorTarget(_EndTargetConnector, point[0], point[1]);
1525
                    else
1526
                        placeRunInputs.AddPoint(point[0], point[1]);
1527
                }
1528

    
1529
                LMConnector _LMConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
1530
                if (_LMConnector != null)
1531
                {
1532
                    _LMConnector.Commit();
1533
                    foreach (var item in lines)
1534
                        item.SPPID.ModelItemId = _LMConnector.ModelItemID;
1535
                }
1536

    
1537
                foreach (var item in startConnectorVertices)
1538
                    ReleaseCOMObjects(item.Key);
1539
                foreach (var item in endConnectorVertices)
1540
                    ReleaseCOMObjects(item.Key);
1541
                ReleaseCOMObjects(placeRunInputs);
1542
                ReleaseCOMObjects(_LMAItem);
1543
                ReleaseCOMObjects(_LMConnector);
1544
            }
1545
            #endregion
1546
            #region 양쪽이 다른 Branch 
1547
            else
1548
            {
1549
                // Branch 시작 Connector
1550
                if (_StartConnector != null)
1551
                    BranchLineModelingByConnector(branch, _StartConnector, startPoints, true);
1552
                // Branch 끝 Connector
1553
                else if (_EndConnector != null)
1554
                    BranchLineModelingByConnector(branch, _EndConnector, endPoints, false);
1555
            }
1556
            #endregion
1557

    
1558
            if (_StartConnector != null)
1559
                ReleaseCOMObjects(_StartConnector);
1560
            if (_EndConnector != null)
1561
                ReleaseCOMObjects(_EndConnector);
1562
            foreach (var item in connectorVertices)
1563
                ReleaseCOMObjects(item.Key);
1564
        }
1565

    
1566
        /// <summary>
1567
        /// Branch 라인을 다시 실제로 모델링하는 메서드
1568
        /// </summary>
1569
        /// <param name="branch"></param>
1570
        /// <param name="_Connector"></param>
1571
        /// <param name="points"></param>
1572
        /// <param name="IsStart"></param>
1573
        private void BranchLineModelingByConnector(Tuple<string, Line, Line> branch, LMConnector _Connector, List<double[]> points, bool IsStart)
1574
        {
1575
            List<Line> lines = SPPIDUtil.FindLinesByModelId(document, branch.Item1);
1576
            Dictionary<LMConnector, List<double[]>> connectorVertices = GetPipeRunVertices(branch.Item1);
1577
            LMConnector _SameRunTargetConnector = null;
1578
            LMSymbol _SameRunTargetLMSymbol = null;
1579
            Symbol _SameRunTargetSymbol = null;
1580
            Dictionary<LMConnector, List<double[]>> branchConnectorVertices = null;
1581
            LMConnector _BranchTargetConnector = null;
1582
            PlaceRunInputs placeRunInputs = new PlaceRunInputs();
1583

    
1584
            // 같은 Line Run의 Connector 찾기
1585
            foreach (var item in connectorVertices)
1586
            {
1587
                if (item.Key == _Connector)
1588
                    continue;
1589

    
1590
                if (IsStart &&
1591
                    !DBNull.Value.Equals(item.Key.ConnectItem1SymbolID) &&
1592
                    !DBNull.Value.Equals(_Connector.ConnectItem2SymbolID)&& 
1593
                    item.Key.ConnectItem1SymbolID == _Connector.ConnectItem2SymbolID)
1594
                {
1595
                    _SameRunTargetConnector = item.Key;
1596
                    break;
1597
                }
1598
                else if (!IsStart &&
1599
                    !DBNull.Value.Equals(item.Key.ConnectItem2SymbolID) &&
1600
                    !DBNull.Value.Equals(_Connector.ConnectItem1SymbolID) && 
1601
                    item.Key.ConnectItem2SymbolID == _Connector.ConnectItem1SymbolID)
1602
                {
1603
                    _SameRunTargetConnector = item.Key;
1604
                    break;
1605
                }
1606
            }
1607

    
1608
            // Branch 반대편이 Symbol
1609
            if (_SameRunTargetConnector == null)
1610
            {
1611
                foreach (var line in lines)
1612
                {
1613
                    foreach (var connector in line.CONNECTORS)
1614
                    {
1615
                        Symbol symbol = SPPIDUtil.FindObjectByUID(document, connector.CONNECTEDITEM) as Symbol;
1616
                        if (symbol != null)
1617
                        {
1618
                            _SameRunTargetSymbol = symbol;
1619
                            _SameRunTargetLMSymbol = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
1620
                            break;
1621
                        }
1622
                    }
1623
                }
1624
            }
1625

    
1626
            // 기존 Connector 제거
1627
            _placement.PIDRemovePlacement(_Connector.AsLMRepresentation());
1628
            
1629
            // 시작 Connector일 경우 첫 Point가 TargetConnector를 찾아야함
1630
            if (IsStart)
1631
            {
1632
                branchConnectorVertices = GetPipeRunVertices(branch.Item2.SPPID.ModelItemId);
1633
                _BranchTargetConnector = FindTargetLMConnector(branchConnectorVertices, points[0][0], points[0][1], points[1][0], points[1][1]);
1634
            }
1635
            // 끝 Conenctor일 경우 마지막 Point가 TargetConnector를 찾아야함
1636
            else
1637
            {
1638
                branchConnectorVertices = GetPipeRunVertices(branch.Item3.SPPID.ModelItemId);
1639
                _BranchTargetConnector = FindTargetLMConnector(branchConnectorVertices,
1640
                    points[points.Count - 1][0],
1641
                    points[points.Count - 1][1],
1642
                    points[points.Count - 2][0],
1643
                    points[points.Count - 2][1]);
1644
            }
1645

    
1646

    
1647
            bool bNeedRemodeling = false;
1648
            if (_SameRunTargetLMSymbol != null)
1649
            {
1650
                Line line = lines.Find(x => x.CONNECTORS.Find(y => y.CONNECTEDITEM == _SameRunTargetSymbol.UID) != null);
1651
                NeedReModeling(line, _SameRunTargetLMSymbol, ref bNeedRemodeling);
1652
            }
1653

    
1654

    
1655

    
1656
            for (int i = 0; i < points.Count; i++)
1657
            {
1658
                double[] point = points[i];
1659
                if (i == 0)
1660
                {
1661
                    if (IsStart)
1662
                    {
1663
                        if (_BranchTargetConnector != null)
1664
                        {
1665
                            placeRunInputs.AddConnectorTarget(_BranchTargetConnector, point[0], point[1]);
1666
                        }
1667
                        else
1668
                        {
1669
                            placeRunInputs.AddPoint(point[0], point[1]);
1670
                        }
1671
                    }
1672
                    else
1673
                    {
1674
                        if (_SameRunTargetConnector != null)
1675
                            placeRunInputs.AddConnectorTarget(_SameRunTargetConnector, point[0], point[1]);
1676
                        else if (_SameRunTargetLMSymbol != null && bNeedRemodeling)
1677
                        {
1678
                            SlopeType slopeType = SPPIDUtil.CalcSlope(point[0], point[1], points[i + 1][0], points[i + 1][1]);
1679
                            if (slopeType == SlopeType.HORIZONTAL)
1680
                                placeRunInputs.AddPoint(point[0], -0.1);
1681
                            else if (slopeType == SlopeType.VERTICAL)
1682
                                placeRunInputs.AddPoint(-0.1, point[1]);
1683
                            else
1684
                                placeRunInputs.AddPoint(point[0], -0.1);
1685

    
1686
                            placeRunInputs.AddPoint(point[0], point[1]);
1687
                        }
1688
                        else if (_SameRunTargetLMSymbol != null)
1689
                            placeRunInputs.AddSymbolTarget(_SameRunTargetLMSymbol, point[0], point[1]);
1690
                        else
1691
                            placeRunInputs.AddPoint(point[0], point[1]);
1692
                    }
1693
                }
1694
                else if (i == points.Count - 1)
1695
                {
1696
                    if (IsStart)
1697
                    {
1698
                        if (_SameRunTargetConnector != null)
1699
                            placeRunInputs.AddConnectorTarget(_SameRunTargetConnector, point[0], point[1]);
1700
                        else if (_SameRunTargetLMSymbol != null && bNeedRemodeling)
1701
                        {
1702
                            placeRunInputs.AddPoint(point[0], point[1]);
1703

    
1704
                            SlopeType slopeType = SPPIDUtil.CalcSlope(point[0], point[1], points[i - 1][0], points[i - 1][1]);
1705
                            if (slopeType == SlopeType.HORIZONTAL)
1706
                                placeRunInputs.AddPoint(point[0], -0.1);
1707
                            else if (slopeType == SlopeType.VERTICAL)
1708
                                placeRunInputs.AddPoint(-0.1, point[1]);
1709
                            else
1710
                                placeRunInputs.AddPoint(point[0], -0.1);
1711
                        }
1712
                        else if (_SameRunTargetLMSymbol != null)
1713
                            placeRunInputs.AddSymbolTarget(_SameRunTargetLMSymbol, point[0], point[1]);
1714
                        else
1715
                            placeRunInputs.AddPoint(point[0], point[1]);
1716
                    }
1717
                    else
1718
                    {
1719
                        if (_BranchTargetConnector != null)
1720
                        {
1721
                            placeRunInputs.AddConnectorTarget(_BranchTargetConnector, point[0], point[1]);
1722
                        }
1723
                        else
1724
                        {
1725
                            placeRunInputs.AddPoint(point[0], point[1]);
1726
                        }
1727
                    }
1728
                }
1729
                else
1730
                    placeRunInputs.AddPoint(point[0], point[1]);
1731
            }
1732
            _LMAItem _LMAItem = _placement.PIDCreateItem(lines[0].SPPID.MAPPINGNAME);
1733
            LMConnector _LMConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
1734

    
1735
            if (_LMConnector != null)
1736
            {
1737
                if (_SameRunTargetConnector != null)
1738
                {
1739
                    JoinPipeRun(_LMConnector.ModelItemID, _SameRunTargetConnector.ModelItemID);
1740
                }
1741
                else
1742
                {
1743
                    if (_SameRunTargetLMSymbol != null && bNeedRemodeling)
1744
                    {
1745
                        string symbolPath = string.Empty;
1746
                        #region get symbol path
1747
                        LMModelItem modelItem = dataSource.GetModelItem(_LMConnector.ModelItemID);
1748
                        foreach (LMRepresentation rep in modelItem.Representations)
1749
                        {
1750
                            if (!DBNull.Value.Equals(rep.get_FileName()) && !string.IsNullOrEmpty(rep.get_FileName()))
1751
                            {
1752
                                symbolPath = rep.get_FileName();
1753
                                break;
1754
                            }
1755
                        }
1756
                        ReleaseCOMObjects(modelItem);
1757
                        #endregion
1758

    
1759
                        double[] point = null;
1760
                        if (IsStart)
1761
                            point = points[points.Count - 1];
1762
                        else
1763
                            point = points[0];
1764

    
1765
                        _LMAItem = _placement.PIDCreateItem(symbolPath);
1766
                        placeRunInputs = new PlaceRunInputs();
1767
                        placeRunInputs.AddSymbolTarget(_SameRunTargetLMSymbol, point[0], point[1]);
1768
                        placeRunInputs.AddConnectorTarget(_LMConnector, point[0], point[1]);
1769
                        LMConnector _ZeroLengthLMConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
1770
                        if (_ZeroLengthLMConnector != null)
1771
                        {
1772
                            RemoveConnectorForReModelingLine(_LMConnector);
1773
                            ZeroLengthModelItemID.Add(_ZeroLengthLMConnector.ModelItemID);
1774
                            ReleaseCOMObjects(_ZeroLengthLMConnector);
1775
                        }
1776
                        ReleaseCOMObjects(placeRunInputs);
1777
                        ReleaseCOMObjects(_LMAItem);
1778
                    }
1779

    
1780
                    foreach (var item in lines)
1781
                        item.SPPID.ModelItemId = _LMConnector.ModelItemID;
1782
                }
1783

    
1784
                _LMConnector.Commit();
1785
                ReleaseCOMObjects(_LMConnector);
1786
            }
1787

    
1788
            ReleaseCOMObjects(placeRunInputs);
1789
            ReleaseCOMObjects(_LMAItem);
1790
            if (_BranchTargetConnector != null)
1791
                ReleaseCOMObjects(_BranchTargetConnector);
1792
            if (_SameRunTargetConnector != null)
1793
                ReleaseCOMObjects(_SameRunTargetConnector);
1794
            if (_SameRunTargetLMSymbol != null)
1795
                ReleaseCOMObjects(_SameRunTargetLMSymbol);
1796
            foreach (var item in connectorVertices)
1797
                ReleaseCOMObjects(item.Key);
1798
            foreach (var item in branchConnectorVertices)
1799
                ReleaseCOMObjects(item.Key);
1800
        }
1801

    
1802
        /// <summary>
1803
        /// EndBreak 모델링 메서드
1804
        /// </summary>
1805
        /// <param name="endBreak"></param>
1806
        private void EndBreakModeling(EndBreak endBreak)
1807
        {
1808
            object ownerObj = SPPIDUtil.FindObjectByUID(document, endBreak.OWNER);
1809
            object connectedItem = SPPIDUtil.FindObjectByUID(document, endBreak.PROPERTIES.Find(x => x.ATTRIBUTE == "Connected Item").VALUE);
1810
            LMConnector targetLMConnector = FindBreakLineTarget(ownerObj, connectedItem);
1811

    
1812
            if (targetLMConnector != null)
1813
            {
1814
                Array array = new double[] { 0, endBreak.SPPID.ORIGINAL_X, endBreak.SPPID.ORIGINAL_Y };
1815
                LMLabelPersist _LmLabelPersist = _placement.PIDPlaceLabel(endBreak.SPPID.MAPPINGNAME, ref array, Rotation: 0, LabeledItem: targetLMConnector.AsLMRepresentation(), IsLeaderVisible: true);
1816
            }
1817
            
1818
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetProgress, ++CurrentCount);
1819
        }
1820

    
1821
        private LMConnector ReModelingLMConnector(LMConnector connector)
1822
        {
1823
            string symbolPath = string.Empty;
1824
            #region get symbol path
1825
            LMModelItem modelItem = dataSource.GetModelItem(connector.ModelItemID);
1826
            foreach (LMRepresentation rep in modelItem.Representations)
1827
            {
1828
                if (!DBNull.Value.Equals(rep.get_FileName()) && !string.IsNullOrEmpty(rep.get_FileName()))
1829
                {
1830
                    symbolPath = rep.get_FileName();
1831
                    break;
1832
                }
1833
            }
1834
            #endregion
1835

    
1836
            LMConnector newConnector = null;
1837
            dynamic OID = connector.get_GraphicOID();
1838
            DependencyObject drawingObject = radApp.ActiveDocument.ActiveSheet.DrawingObjects[OID];
1839
            Ingr.RAD2D.LineStringGeometry2d lineStringGeometry = drawingObject.GetGeometry() as Ingr.RAD2D.LineStringGeometry2d;
1840
            int verticesCount = lineStringGeometry.VertexCount;
1841
            PlaceRunInputs placeRunInputs = new PlaceRunInputs();
1842
            _LMAItem _LMAItem = _placement.PIDCreateItem(symbolPath);
1843

    
1844
            if (Convert.ToBoolean(connector.get_IsZeroLength()))
1845
            {
1846
                double[] vertices = null;
1847
                lineStringGeometry.GetVertices(ref verticesCount, ref vertices);
1848
                double x = 0;
1849
                double y = 0;
1850
                lineStringGeometry.GetVertex(1, ref x, ref y);
1851

    
1852
                placeRunInputs.AddSymbolTarget(connector.ConnectItem1SymbolObject, x, y);
1853
                placeRunInputs.AddSymbolTarget(connector.ConnectItem2SymbolObject, x, y);
1854

    
1855
                _placement.PIDRemovePlacement(connector.AsLMRepresentation());
1856
                newConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
1857
            }
1858
            else
1859
            {
1860
                List<double[]> vertices = new List<double[]>();
1861
                for (int i = 1; i <= verticesCount; i++)
1862
                {
1863
                    double x = 0;
1864
                    double y = 0;
1865
                    lineStringGeometry.GetVertex(i, ref x, ref y);
1866
                    vertices.Add(new double[] { x, y });
1867
                }
1868

    
1869
                for (int i = 0; i < vertices.Count; i++)
1870
                {
1871
                    double[] points = vertices[i];
1872
                    if (i == 0)
1873
                    {
1874
                        if (connector.ConnectItem1SymbolObject != null)
1875
                            placeRunInputs.AddSymbolTarget(connector.ConnectItem1SymbolObject, points[0], points[1]);
1876
                        else
1877
                            placeRunInputs.AddPoint(points[0], points[1]);
1878
                    }
1879
                    else if (i == vertices.Count - 1)
1880
                    {
1881
                        if (connector.ConnectItem2SymbolObject != null)
1882
                            placeRunInputs.AddSymbolTarget(connector.ConnectItem2SymbolObject, points[0], points[1]);
1883
                        else
1884
                            placeRunInputs.AddPoint(points[0], points[1]);
1885
                    }
1886
                    else
1887
                        placeRunInputs.AddPoint(points[0], points[1]);
1888
                }
1889

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

    
1892
                _placement.PIDRemovePlacement(connector.AsLMRepresentation());
1893
                newConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
1894

    
1895
                foreach (var line in lines)
1896
                    line.SPPID.ModelItemId = newConnector.ModelItemID;
1897
            }
1898

    
1899

    
1900
            return newConnector;
1901
        }
1902

    
1903
        /// <summary>
1904
        /// SpecBreak Modeling 메서드
1905
        /// </summary>
1906
        /// <param name="specBreak"></param>
1907
        private void SpecBreakModeling(SpecBreak specBreak)
1908
        {
1909
            object upStreamObj = SPPIDUtil.FindObjectByUID(document, specBreak.UpStreamUID);
1910
            object downStreamObj = SPPIDUtil.FindObjectByUID(document, specBreak.DownStreamUID);
1911

    
1912
            if (upStreamObj != null &&
1913
                downStreamObj != null)
1914
            {
1915
                LMConnector targetLMConnector = FindBreakLineTarget(upStreamObj, downStreamObj);
1916

    
1917
                if (targetLMConnector != null)
1918
                {
1919
                    foreach (var attribute in specBreak.ATTRIBUTES)
1920
                    {
1921
                        AttributeMapping mapping = document.AttributeMappings.Find(x => x.UID == attribute.UID);
1922
                        if (mapping != null && !string.IsNullOrEmpty(mapping.SPPIDSYMBOLNAME) && mapping.SPPIDSYMBOLNAME != "None")
1923
                        {
1924
                            string MappingPath = mapping.SPPIDSYMBOLNAME;
1925
                            Array array = new double[] { 0, specBreak.SPPID.ORIGINAL_X, specBreak.SPPID.ORIGINAL_Y };
1926
                            LMLabelPersist _LmLabelPersist = _placement.PIDPlaceLabel(MappingPath, ref array, Rotation: specBreak.ANGLE, LabeledItem: targetLMConnector.AsLMRepresentation(), IsLeaderVisible: mapping.LeaderLine);
1927

    
1928
                            if (_LmLabelPersist != null)
1929
                            {
1930
                                ReleaseCOMObjects(_LmLabelPersist);
1931
                            }
1932
                        }
1933
                    }
1934
                    ReleaseCOMObjects(targetLMConnector);
1935
                }
1936
            }
1937
        }
1938

    
1939
        private LMConnector FindBreakLineTarget(object targetObj, object connectedObj)
1940
        {
1941
            LMConnector targetConnector = null;
1942
            Symbol targetSymbol = targetObj as Symbol;
1943
            Symbol connectedSymbol = connectedObj as Symbol;
1944
            Line targetLine = targetObj as Line;
1945
            Line connectedLine = connectedObj as Line;
1946
            if (targetSymbol != null && connectedSymbol != null)
1947
            {
1948
                LMSymbol targetLMSymbol = dataSource.GetSymbol(targetSymbol.SPPID.RepresentationId);
1949
                LMSymbol connectedLMSymbol = dataSource.GetSymbol(connectedSymbol.SPPID.RepresentationId);
1950

    
1951
                foreach (LMConnector connector in targetLMSymbol.Avoid1Connectors)
1952
                {
1953
                    if (connector.get_ItemStatus() != "Active")
1954
                        continue;
1955

    
1956
                    if (connector.ConnectItem1SymbolObject.Id == connectedLMSymbol.Id)
1957
                    {
1958
                        targetConnector = connector;
1959
                        break;
1960
                    }
1961
                    else if (connector.ConnectItem2SymbolObject.Id == connectedLMSymbol.Id)
1962
                    {
1963
                        targetConnector = connector;
1964
                        break;
1965
                    }
1966
                }
1967

    
1968
                foreach (LMConnector connector in targetLMSymbol.Avoid2Connectors)
1969
                {
1970
                    if (connector.get_ItemStatus() != "Active")
1971
                        continue;
1972

    
1973
                    if (connector.ConnectItem1SymbolObject.Id == connectedLMSymbol.Id)
1974
                    {
1975
                        targetConnector = connector;
1976
                        break;
1977
                    }
1978
                    else if (connector.ConnectItem2SymbolObject.Id == connectedLMSymbol.Id)
1979
                    {
1980
                        targetConnector = connector;
1981
                        break;
1982
                    }
1983
                }
1984

    
1985
                ReleaseCOMObjects(targetLMSymbol);
1986
                ReleaseCOMObjects(connectedLMSymbol);
1987
            }
1988
            else if (targetLine != null && connectedLine != null)
1989
            {
1990
                LMModelItem targetModelItem = dataSource.GetModelItem(targetLine.SPPID.ModelItemId);
1991
                LMModelItem connectedModelItem = dataSource.GetModelItem(connectedLine.SPPID.ModelItemId);
1992

    
1993
                if (targetModelItem != null && connectedModelItem != null)
1994
                {
1995
                    foreach (LMRepresentation rep in targetModelItem.Representations)
1996
                    {
1997
                        if (targetConnector != null)
1998
                            break;
1999

    
2000
                        if (rep.Attributes["RepresentationType"].get_Value() == "Connector" && rep.Attributes["ItemStatus"].get_Value() == "Active")
2001
                        {
2002
                            LMConnector _LMConnector = dataSource.GetConnector(rep.Id);
2003

    
2004
                            if (IsConnected(_LMConnector, connectedModelItem))
2005
                                targetConnector = _LMConnector;
2006
                            else
2007
                                ReleaseCOMObjects(_LMConnector);
2008
                        }
2009
                    }
2010

    
2011
                    ReleaseCOMObjects(targetModelItem);
2012
                }
2013
            }
2014
            else
2015
            {
2016
                LMSymbol connectedLMSymbol = null;
2017
                if (connectedSymbol != null)
2018
                    connectedLMSymbol = dataSource.GetSymbol(connectedSymbol.SPPID.RepresentationId);
2019
                else if (targetSymbol != null)
2020
                    connectedLMSymbol = dataSource.GetSymbol(targetSymbol.SPPID.RepresentationId);
2021
                else
2022
                {
2023

    
2024
                }
2025
                LMModelItem targetModelItem = null;
2026
                if (targetLine != null)
2027
                    targetModelItem = dataSource.GetModelItem(targetLine.SPPID.ModelItemId);
2028
                else if (connectedLine != null)
2029
                    targetModelItem = dataSource.GetModelItem(connectedLine.SPPID.ModelItemId);
2030
                else
2031
                {
2032

    
2033
                }
2034
                if (connectedLMSymbol != null && targetModelItem != null)
2035
                {
2036
                    foreach (LMConnector connector in connectedLMSymbol.Avoid1Connectors)
2037
                    {
2038
                        if (connector.get_ItemStatus() != "Active")
2039
                            continue;
2040

    
2041
                        if (IsConnected(connector, targetModelItem))
2042
                        {
2043
                            targetConnector = connector;
2044
                            break;
2045
                        }
2046
                    }
2047

    
2048
                    if (targetConnector == null)
2049
                    {
2050
                        foreach (LMConnector connector in connectedLMSymbol.Avoid2Connectors)
2051
                        {
2052
                            if (connector.get_ItemStatus() != "Active")
2053
                                continue;
2054

    
2055
                            if (IsConnected(connector, targetModelItem))
2056
                            {
2057
                                targetConnector = connector;
2058
                                break;
2059
                            }
2060
                        }
2061
                    }
2062
                }
2063

    
2064
            }
2065

    
2066
            return targetConnector;
2067
        }
2068

    
2069
        private bool IsConnected(LMConnector connector, LMModelItem modelItem)
2070
        {
2071
            bool result = false;
2072

    
2073
            foreach (LMRepresentation rep in modelItem.Representations)
2074
            {
2075
                if (result)
2076
                    break;
2077

    
2078
                if (rep.Attributes["RepresentationType"].get_Value() == "Connector" && rep.Attributes["ItemStatus"].get_Value() == "Active")
2079
                {
2080
                    LMConnector _LMConnector = dataSource.GetConnector(rep.Id);
2081

    
2082
                    if (_LMConnector.ConnectItem1SymbolObject != null &&
2083
                        connector.ConnectItem1SymbolObject != null &&
2084
                        _LMConnector.ConnectItem1SymbolObject.Id == connector.ConnectItem1SymbolObject.Id)
2085
                    {
2086
                        result = true;
2087
                        ReleaseCOMObjects(_LMConnector);
2088
                        break;
2089
                    }
2090
                    else if (_LMConnector.ConnectItem1SymbolObject != null &&
2091
                        connector.ConnectItem2SymbolObject != null &&
2092
                        _LMConnector.ConnectItem1SymbolObject.Id == connector.ConnectItem2SymbolObject.Id)
2093
                    {
2094
                        result = true;
2095
                        ReleaseCOMObjects(_LMConnector);
2096
                        break;
2097
                    }
2098
                    else if (_LMConnector.ConnectItem2SymbolObject != null &&
2099
                        connector.ConnectItem1SymbolObject != null &&
2100
                        _LMConnector.ConnectItem2SymbolObject.Id == connector.ConnectItem1SymbolObject.Id)
2101
                    {
2102
                        result = true;
2103
                        ReleaseCOMObjects(_LMConnector);
2104
                        break;
2105
                    }
2106
                    else if (_LMConnector.ConnectItem2SymbolObject != null &&
2107
                        connector.ConnectItem2SymbolObject != null &&
2108
                        _LMConnector.ConnectItem2SymbolObject.Id == connector.ConnectItem2SymbolObject.Id)
2109
                    {
2110
                        result = true;
2111
                        ReleaseCOMObjects(_LMConnector);
2112
                        break;
2113
                    }
2114

    
2115
                    ReleaseCOMObjects(_LMConnector);
2116
                }
2117
            }
2118

    
2119

    
2120
            return result;
2121
        }
2122

    
2123
        /// <summary>
2124
        /// FromModelItem을 ToModelItem으로 PipeRunJoin하는 메서드
2125
        /// </summary>
2126
        /// <param name="fromModelItemId"></param>
2127
        /// <param name="toModelItemId"></param>
2128
        private void JoinPipeRun(string fromModelItemId, string toModelItemId)
2129
        {
2130
            LMModelItem modelItem1 = dataSource.GetModelItem(toModelItemId);
2131
            _LMAItem item1 = modelItem1.AsLMAItem();
2132
            LMModelItem modelItem2 = dataSource.GetModelItem(fromModelItemId);
2133
            _LMAItem item2 = modelItem2.AsLMAItem();
2134
            
2135
            // item2가 item1으로 조인
2136
            try
2137
            {
2138
                _placement.PIDJoinRuns(ref item1, ref item2);
2139
                item1.Commit();
2140
                item2.Commit();
2141

    
2142
                List<Line> lines = SPPIDUtil.FindLinesByModelId(document, fromModelItemId);
2143
                foreach (var line in lines)
2144
                    line.SPPID.ModelItemId = toModelItemId;
2145
            }
2146
            catch (Exception ex)
2147
            {
2148
                System.Windows.Forms.MessageBox.Show(ex.Message + "\r\n" + ex.StackTrace);
2149
            }
2150
            finally
2151
            {
2152
                ReleaseCOMObjects(modelItem1);
2153
                ReleaseCOMObjects(item1);
2154
                ReleaseCOMObjects(modelItem2);
2155
                ReleaseCOMObjects(item2);
2156
            }
2157
        }
2158

    
2159
        /// <summary>
2160
        /// PipeRun을 자동으로 Join하는 메서드
2161
        /// </summary>
2162
        /// <param name="modelItemId"></param>
2163
        private void AutoJoinPipeRun(string modelItemId)
2164
        {
2165
            LMModelItem modelItem = dataSource.GetModelItem(modelItemId);
2166
            _LMAItem item = modelItem.AsLMAItem();
2167
            try
2168
            {
2169
                if (modelItem.get_ItemStatus() == "Active")
2170
                {
2171
                    string modelitemID = item.Id;
2172
                    _placement.PIDAutoJoin(item, AutoJoinEndConstants.autoJoin_Both, ref item);
2173
                    string afterModelItemID = item.Id;
2174

    
2175
                    if (modelitemID != afterModelItemID)
2176
                    {
2177
                        List<Line> lines = SPPIDUtil.FindLinesByModelId(document, modelitemID);
2178
                        foreach (var line in lines)
2179
                            line.SPPID.ModelItemId = afterModelItemID;
2180
                    }
2181
                    item.Commit();
2182
                }
2183
            }
2184
            catch (Exception ex)
2185
            {
2186
                System.Windows.Forms.MessageBox.Show(ex.Message + "\r\n" + ex.StackTrace);
2187
            }
2188
            finally
2189
            {
2190
                ReleaseCOMObjects(modelItem);
2191
                ReleaseCOMObjects(item);
2192
            }
2193
        }
2194

    
2195
        /// <summary>
2196
        /// LineRun에 있는 Line들을 Join하는 진입 메서드
2197
        /// </summary>
2198
        /// <param name="run"></param>
2199
        private void JoinRunLine(LineRun run)
2200
        {
2201
            string modelItemId = string.Empty;
2202
            foreach (var item in run.RUNITEMS)
2203
            {
2204
                if (item.GetType() == typeof(Line))
2205
                {
2206
                    Line line = item as Line;
2207
                    AutoJoinPipeRun(line.SPPID.ModelItemId);
2208
                    modelItemId = line.SPPID.ModelItemId;
2209

    
2210
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetProgress, ++CurrentCount);
2211
                }
2212
            }
2213
        }
2214

    
2215
        /// <summary>
2216
        /// PipeRun의 좌표를 가져오는 메서드
2217
        /// </summary>
2218
        /// <param name="modelId"></param>
2219
        /// <returns></returns>
2220
        private Dictionary<LMConnector, List<double[]>> GetPipeRunVertices(string modelId)
2221
        {
2222
            Dictionary<LMConnector, List<double[]>> connectorVertices = new Dictionary<LMConnector, List<double[]>>();
2223
            LMModelItem modelItem = dataSource.GetModelItem(modelId);
2224

    
2225
            if (modelItem != null)
2226
            {
2227
                foreach (LMRepresentation rep in modelItem.Representations)
2228
                {
2229
                    if (rep.Attributes["RepresentationType"].get_Value() == "Connector" && rep.Attributes["ItemStatus"].get_Value() == "Active")
2230
                    {
2231
                        LMConnector _LMConnector = dataSource.GetConnector(rep.Id);
2232
                        connectorVertices.Add(_LMConnector, new List<double[]>());
2233
                        dynamic OID = rep.get_GraphicOID();
2234
                        DependencyObject drawingObject = radApp.ActiveDocument.ActiveSheet.DrawingObjects[OID];
2235
                        Ingr.RAD2D.LineStringGeometry2d lineStringGeometry = drawingObject.GetGeometry() as Ingr.RAD2D.LineStringGeometry2d;
2236
                        int verticesCount = lineStringGeometry.VertexCount;
2237
                        double[] vertices = null;
2238
                        lineStringGeometry.GetVertices(ref verticesCount, ref vertices);
2239
                        for (int i = 0; i < verticesCount; i++)
2240
                        {
2241
                            double x = 0;
2242
                            double y = 0;
2243
                            lineStringGeometry.GetVertex(i + 1, ref x, ref y);
2244
                            connectorVertices[_LMConnector].Add(new double[] { Math.Round(x, 10), Math.Round(y, 10) });
2245
                        }
2246
                    }
2247
                }
2248

    
2249
                ReleaseCOMObjects(modelItem);
2250
            }
2251

    
2252
            return connectorVertices;
2253
        }
2254

    
2255
        /// <summary>
2256
        /// LMConnector들을 가져온다.
2257
        /// </summary>
2258
        /// <param name="modelId"></param>
2259
        /// <returns></returns>
2260
        private List<LMConnector> GetConnectors(string modelId)
2261
        {
2262
            List<LMConnector> connectors = new List<LMConnector>();
2263
            LMModelItem modelItem = dataSource.GetModelItem(modelId);
2264

    
2265
            if (modelItem != null)
2266
            {
2267
                foreach (LMRepresentation rep in modelItem.Representations)
2268
                {
2269
                    if (rep.Attributes["RepresentationType"].get_Value() == "Connector" && rep.Attributes["ItemStatus"].get_Value() == "Active")
2270
                    {
2271
                        LMConnector _LMConnector = dataSource.GetConnector(rep.Id);
2272
                        connectors.Add(_LMConnector);
2273
                    }
2274
                }
2275

    
2276
                ReleaseCOMObjects(modelItem);
2277
            }
2278

    
2279
            return connectors;
2280
        }
2281

    
2282
        /// <summary>
2283
        /// 좌표로 PipeRun의 Connector중에 어느 Connector에 가까운지/붙을지 가져오는 메서드 - 두점으로 라인의 교차점을 기준으로 구함
2284
        /// </summary>
2285
        /// <param name="connectorVertices"></param>
2286
        /// <param name="connX"></param>
2287
        /// <param name="connY"></param>
2288
        /// <param name="x2"></param>
2289
        /// <param name="y2"></param>
2290
        /// <returns></returns>
2291
        private LMConnector FindTargetLMConnector(Dictionary<LMConnector, List<double[]>> connectorVertices, double connX, double connY, double x2, double y2)
2292
        {
2293
            double length = double.MaxValue;
2294
            LMConnector targetConnector = null;
2295
            foreach (var item in connectorVertices)
2296
            {
2297
                List<double[]> points = item.Value;
2298
                for (int i = 0; i < points.Count - 1; i++)
2299
                {
2300
                    double[] point1 = points[i];
2301
                    double[] point2 = points[i + 1];
2302

    
2303
                    double maxLineX = Math.Max(point1[0], point2[0]);
2304
                    double minLineX = Math.Min(point1[0], point2[0]);
2305
                    double maxLineY = Math.Max(point1[1], point2[1]);
2306
                    double minLineY = Math.Min(point1[1], point2[1]);
2307

    
2308
                    SlopeType slope = SPPIDUtil.CalcSlope(minLineX, minLineY, maxLineX, maxLineY);
2309

    
2310
                    // 두직선의 교차점
2311
                    double[] crossingPoint = SPPIDUtil.CalcLineCrossingPoint(connX, connY, x2, y2, point1[0], point1[1], point2[0], point2[1]);
2312
                    if (crossingPoint != null)
2313
                    {
2314
                        double distance = SPPIDUtil.CalcPointToPointdDistance(connX, connY, crossingPoint[0], crossingPoint[1]);
2315
                        if (length >= distance)
2316
                        {
2317
                            if (slope == SlopeType.Slope &&
2318
                                minLineX <= crossingPoint[0] && maxLineX >= crossingPoint[0] &&
2319
                                minLineY <= crossingPoint[1] && maxLineY >= crossingPoint[1])
2320
                            {
2321
                                targetConnector = item.Key;
2322
                                length = distance;
2323
                            }
2324
                            else if (slope == SlopeType.HORIZONTAL &&
2325
                                minLineX <= crossingPoint[0] && maxLineX >= crossingPoint[0])
2326
                            {
2327
                                targetConnector = item.Key;
2328
                                length = distance;
2329
                            }
2330
                            else if (slope == SlopeType.VERTICAL &&
2331
                               minLineY <= crossingPoint[1] && maxLineY >= crossingPoint[1])
2332
                            {
2333
                                targetConnector = item.Key;
2334
                                length = distance;
2335
                            }
2336
                        }
2337
                    }
2338
                }
2339

    
2340

    
2341
            }
2342

    
2343
            if (targetConnector == null)
2344
            {
2345
                foreach (var item in connectorVertices)
2346
                {
2347
                    List<double[]> points = item.Value;
2348
                    foreach (var point in points)
2349
                    {
2350
                        double distance = SPPIDUtil.CalcPointToPointdDistance(connX, connY, point[0], point[1]);
2351
                        if (length >= distance)
2352
                        {
2353
                            targetConnector = item.Key;
2354
                            length = distance;
2355
                        }
2356
                    }
2357
                }
2358

    
2359
            }
2360

    
2361
            return targetConnector;
2362
        }
2363

    
2364
        /// <summary>
2365
        /// 좌표로 PipeRun의 Connector중에 어느 Connector에 가까운지/붙을지 가져오는 메서드 - 한점으로 제일 가까운 기준으로 구함(단순)
2366
        /// </summary>
2367
        /// <param name="connectorVertices"></param>
2368
        /// <param name="connX"></param>
2369
        /// <param name="connY"></param>
2370
        /// <returns></returns>
2371
        private LMConnector FindTargetLMConnectorByPoint(Dictionary<LMConnector, List<double[]>> connectorVertices, double connX, double connY)
2372
        {
2373
            double length = double.MaxValue;
2374
            LMConnector targetConnector = null;
2375
            foreach (var item in connectorVertices)
2376
            {
2377
                List<double[]> points = item.Value;
2378

    
2379
                foreach (double[] point in points)
2380
                {
2381
                    double distance = SPPIDUtil.CalcPointToPointdDistance(point[0], point[1], connX, connY);
2382
                    if (length >= distance)
2383
                    {
2384
                        targetConnector = item.Key;
2385
                        length = distance;
2386
                    }
2387
                }
2388
            }
2389

    
2390
            return targetConnector;
2391
        }
2392

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

    
2416
                    if ((x1 <= connX && x2 >= connX) ||
2417
                        (y1 <= connY && y2 >= connY))
2418
                    {
2419
                        double distance = SPPIDUtil.CalcPointToPointdDistance(point1[0], point1[1], connX, connY);
2420
                        if (length >= distance)
2421
                        {
2422
                            targetConnector = item.Key;
2423
                            length = distance;
2424
                        }
2425

    
2426
                        distance = SPPIDUtil.CalcPointToPointdDistance(point2[0], point2[1], connX, connY);
2427
                        if (length >= distance)
2428
                        {
2429
                            targetConnector = item.Key;
2430
                            length = distance;
2431
                        }
2432
                    }
2433
                }
2434
            }
2435

    
2436
            // 못찾았을때.
2437
            length = double.MaxValue;
2438
            if (targetConnector == null)
2439
            {
2440
                foreach (var item in connectorVertices)
2441
                {
2442
                    List<double[]> points = item.Value;
2443

    
2444
                    foreach (double[] point in points)
2445
                    {
2446
                        double distance = SPPIDUtil.CalcPointToPointdDistance(point[0], point[1], connX, connY);
2447
                        if (length >= distance)
2448
                        {
2449
                            targetConnector = item.Key;
2450
                            length = distance;
2451
                        }
2452
                    }
2453
                }
2454
            }
2455

    
2456
            return targetConnector;
2457
        }
2458

    
2459
        /// <summary>
2460
        /// Line Number Symbol을 실제로 Modeling하는 메서드
2461
        /// </summary>
2462
        /// <param name="lineNumber"></param>
2463
        private void LineNumberModeling(LineNumber lineNumber)
2464
        {
2465
            object obj = SPPIDUtil.FindObjectByUID(document, lineNumber.CONNLINE);
2466
            if (obj != null)
2467
            {
2468
                Line line = obj as Line;
2469
                Dictionary<LMConnector, List<double[]>> connectorVertices = GetPipeRunVertices(line.SPPID.ModelItemId);
2470
                LMConnector connectedLMConnector = FindTargetLMConnectorForLabel(connectorVertices, lineNumber.SPPID.ORIGINAL_X, lineNumber.SPPID.ORIGINAL_Y);
2471
                if (connectedLMConnector != null)
2472
                {
2473
                    double x = 0;
2474
                    double y = 0;
2475
                    CalcLabelLocation(ref x, ref y, lineNumber.SPPID.ORIGINAL_X, lineNumber.SPPID.ORIGINAL_Y, lineNumber.SPPIDLabelLocation, _ETCSetting.LineNumberLocation);
2476

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

    
2480
                    foreach (var item in connectorVertices)
2481
                        ReleaseCOMObjects(item.Key);
2482
                    if (_LmLabelPresist != null)
2483
                    {
2484
                        _LmLabelPresist.Commit();
2485
                        lineNumber.SPPID.RepresentationId = _LmLabelPresist.AsLMRepresentation().Id;
2486
                        ReleaseCOMObjects(_LmLabelPresist);
2487
                    }
2488
                    else
2489
                    {
2490

    
2491
                    }
2492
                }
2493
            }
2494

    
2495
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetProgress, ++CurrentCount);
2496
        }
2497

    
2498
        /// <summary>
2499
        /// Flow Mark Modeling
2500
        /// </summary>
2501
        /// <param name="line"></param>
2502
        private void FlowMarkModeling(Line line)
2503
        {
2504
            if (line.FLOWMARK && !string.IsNullOrEmpty(line.SPPID.ModelItemId) && !string.IsNullOrEmpty(_ETCSetting.FlowMarkSymbolPath))
2505
            {
2506
                SlopeType targetSlopeType = SPPIDUtil.CalcSlope(line.SPPID.START_X, line.SPPID.START_Y, line.SPPID.END_X, line.SPPID.END_Y);
2507
                string mappingPath = _ETCSetting.FlowMarkSymbolPath;
2508
                double percent = line.FLOWMARK_PERCENT;
2509
                double tempX = 0;
2510
                double tempY = 0;
2511

    
2512
                double gapX;
2513
                double gapY;
2514
                // ID2 기준의 Gap을 구함
2515
                if (percent == 0)
2516
                {
2517
                    gapX = 0;
2518
                    gapY = 0;
2519
                }
2520
                else
2521
                {
2522
                    gapX = Math.Abs(line.SPPID.START_X - line.SPPID.END_X) / 100 * percent;
2523
                    gapY = Math.Abs(line.SPPID.START_Y - line.SPPID.END_Y) / 100 * percent;
2524
                }
2525

    
2526
                if (line.SPPID.START_X < line.SPPID.END_X)
2527
                    tempX = line.SPPID.START_X + gapX;
2528
                else
2529
                    tempX = line.SPPID.START_X - gapX;
2530

    
2531
                if (line.SPPID.START_Y < line.SPPID.END_Y)
2532
                    tempY = line.SPPID.START_Y + gapY;
2533
                else
2534
                    tempY = line.SPPID.START_Y - gapY;
2535

    
2536
                
2537
               
2538
                Dictionary<LMConnector, List<double[]>> connectorVertices = GetPipeRunVertices(line.SPPID.ModelItemId);
2539
                LMConnector _TargetItem = null;
2540
                double distance = double.MaxValue;
2541
                double[] startPoint = null;
2542
                double[] endPoint = null;
2543
                // ID2의 기준 Gap으로 제일 가까운 Line 찾음(Slope도 같은조건)
2544
                foreach (var item in connectorVertices)
2545
                {
2546
                    for (int i = 0; i < item.Value.Count - 1; i++)
2547
                    {
2548
                        List<double[]> points = item.Value;
2549
                        double[] point1 = points[i];
2550
                        double[] point2 = points[i + 1];
2551

    
2552
                        SlopeType slopeType = SPPIDUtil.CalcSlope(point1[0], point1[1], point2[0], point2[1]);
2553
                        if (slopeType == targetSlopeType)
2554
                        {
2555
                            double result = SPPIDUtil.CalcPointToPointdDistance(point1[0], point1[1], tempX, tempY);
2556
                            if (result < distance)
2557
                            {
2558
                                distance = result;
2559
                                _TargetItem = item.Key;
2560

    
2561
                                startPoint = point1;
2562
                                endPoint = point2;
2563
                            }
2564

    
2565
                            result = SPPIDUtil.CalcPointToPointdDistance(point2[0], point2[1], tempX, tempY);
2566
                            if (result < distance)
2567
                            {
2568
                                distance = result;
2569
                                _TargetItem = item.Key;
2570

    
2571
                                startPoint = point1;
2572
                                endPoint = point2;
2573
                            }
2574
                        }
2575
                    }
2576
                }
2577

    
2578
                if (_TargetItem != null)
2579
                {
2580
                    double x = 0;
2581
                    double y = 0;
2582
                    double angle = 0;
2583
                    // SPPID 기준의 Gap으로 실 좌표를 구함
2584
                    if (percent == 0)
2585
                    {
2586
                        gapX = 0;
2587
                        gapY = 0;
2588
                    }
2589
                    else
2590
                    {
2591
                        gapX = Math.Abs(startPoint[0] - endPoint[0]) / 100 * percent;
2592
                        gapY = Math.Abs(startPoint[1] - endPoint[1]) / 100 * percent;
2593
                    }
2594

    
2595
                    if (startPoint[0] < endPoint[0])
2596
                        x = startPoint[0] + gapX;
2597
                    else
2598
                        x = startPoint[0] - gapX;
2599

    
2600
                    if (startPoint[1] < endPoint[1])
2601
                        y = startPoint[1] + gapY;
2602
                    else
2603
                        y = startPoint[1] - gapY;
2604
                    
2605
                    if (targetSlopeType == SlopeType.HORIZONTAL)
2606
                    {
2607
                        if (startPoint[0] < endPoint[0])
2608
                            angle = 0;
2609
                        else
2610
                            angle = Math.PI;
2611
                    }
2612
                    // 90 270
2613
                    else if (targetSlopeType == SlopeType.VERTICAL)
2614
                    {
2615
                        if (startPoint[1] < endPoint[1])
2616
                            angle = 90 * Math.PI / 180;
2617
                        else
2618
                            angle = 270 * Math.PI / 180;
2619
                    }
2620

    
2621
                    LMSymbol _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: 0, Rotation: angle, TargetItem: _TargetItem);
2622
                    
2623
                    if (_LMSymbol != null)
2624
                    {
2625
                        ReleaseCOMObjects(_LMSymbol);
2626
                    }
2627
                        
2628
                }
2629

    
2630
                foreach (var item in connectorVertices)
2631
                    ReleaseCOMObjects(item.Key);
2632
            }
2633
        }
2634

    
2635
        /// <summary>
2636
        /// Line Number 기준으로 모든 Item에 Line Number의 Attribute Input
2637
        /// </summary>
2638
        /// <param name="lineNumber"></param>
2639
        private void InputLineNumberAttribute(LineNumber lineNumber)
2640
        {
2641
            foreach (LineRun run in lineNumber.RUNS)
2642
            {
2643
                foreach (var item in run.RUNITEMS)
2644
                {
2645
                    if (item.GetType() == typeof(Symbol))
2646
                    {
2647
                        Symbol symbol = item as Symbol;
2648
                        LMSymbol _LMSymbol = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
2649
                        if (_LMSymbol != null)
2650
                        {
2651
                            LMModelItem _LMModelItem = _LMSymbol.ModelItemObject;
2652

    
2653
                            if (_LMModelItem != null && _LMModelItem.get_ItemStatus() == "Active")
2654
                            {
2655
                                foreach (var attribute in lineNumber.ATTRIBUTES)
2656
                                {
2657
                                    LineNumberMapping mapping = document.LineNumberMappings.Find(x => x.UID == attribute.UID);
2658
                                    if (mapping != null && !string.IsNullOrEmpty(attribute.VALUE) && attribute.VALUE != "None")
2659
                                    {
2660
                                        LMAAttribute _LMAAttribute = _LMModelItem.Attributes[mapping.SPPIDATTRIBUTENAME];
2661
                                        if (_LMAAttribute != null)
2662
                                        {
2663
                                            if (DBNull.Value.Equals(_LMAAttribute.get_Value()))
2664
                                                _LMAAttribute.set_Value(attribute.VALUE);
2665
                                            else if (_LMAAttribute.get_Value() != attribute.VALUE)
2666
                                                _LMAAttribute.set_Value(attribute.VALUE);
2667
                                        }
2668
                                    }
2669
                                }
2670
                                _LMModelItem.Commit();
2671
                            }
2672
                            if (_LMModelItem != null)
2673
                                ReleaseCOMObjects(_LMModelItem);
2674
                        }
2675
                        if (_LMSymbol != null)
2676
                            ReleaseCOMObjects(_LMSymbol);
2677
                    }
2678
                    else if (item.GetType() == typeof(Line))
2679
                    {
2680
                        Line line = item as Line;
2681
                        if (line != null)
2682
                        {
2683
                            LMModelItem _LMModelItem = dataSource.GetModelItem(line.SPPID.ModelItemId);
2684
                            if (_LMModelItem != null && _LMModelItem.get_ItemStatus() == "Active")
2685
                            {
2686
                                foreach (var attribute in lineNumber.ATTRIBUTES)
2687
                                {
2688
                                    LineNumberMapping mapping = document.LineNumberMappings.Find(x => x.UID == attribute.UID);
2689
                                    if (mapping != null && !string.IsNullOrEmpty(attribute.VALUE) && attribute.VALUE != "None")
2690
                                    {
2691
                                        LMAAttribute _LMAAttribute = _LMModelItem.Attributes[mapping.SPPIDATTRIBUTENAME];
2692
                                        if (_LMAAttribute != null)
2693
                                        {
2694
                                            if (DBNull.Value.Equals(_LMAAttribute.get_Value()))
2695
                                                _LMAAttribute.set_Value(attribute.VALUE);
2696
                                            else if (_LMAAttribute.get_Value() != attribute.VALUE)
2697
                                                _LMAAttribute.set_Value(attribute.VALUE);
2698
                                            
2699
                                        }
2700
                                    }
2701
                                }
2702
                                _LMModelItem.Commit();
2703
                            }
2704
                            if (_LMModelItem != null)
2705
                                ReleaseCOMObjects(_LMModelItem);
2706
                        }
2707
                    }
2708
                }
2709
            }
2710

    
2711
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetProgress, ++CurrentCount);
2712
        }
2713

    
2714
        /// <summary>
2715
        /// Symbol Attribute 입력 메서드
2716
        /// </summary>
2717
        /// <param name="item"></param>
2718
        private void InputSymbolAttribute(object targetItem, List<BaseModel.Attribute> targetAttributes)
2719
        {
2720
            try
2721
            {
2722
                // Object 아이템이 Symbol일 경우 Equipment일 경우 
2723
                string sRep = null;
2724
                if (targetItem.GetType() == typeof(Symbol))
2725
                    sRep = ((Symbol)targetItem).SPPID.RepresentationId;
2726
                else if (targetItem.GetType() == typeof(Equipment))
2727
                    sRep = ((Equipment)targetItem).SPPID.RepresentationId;
2728
                    
2729
                if (!string.IsNullOrEmpty(sRep))
2730
                {
2731
                    LMSymbol _LMSymbol = dataSource.GetSymbol(sRep);
2732
                    LMModelItem _LMModelItem = _LMSymbol.ModelItemObject;
2733
                    LMAAttributes _Attributes = _LMModelItem.Attributes;
2734

    
2735
                    foreach (var item in targetAttributes)
2736
                    {
2737
                        AttributeMapping mapping = document.AttributeMappings.Find(x => x.UID == item.UID);
2738
                        if (mapping != null && !string.IsNullOrEmpty(item.VALUE) && item.VALUE != "None")
2739
                        {
2740
                            LMAAttribute _Attribute = _Attributes[mapping.SPPIDATTRIBUTENAME];
2741
                            if (_Attribute != null)
2742
                                _Attribute.set_Value(item.VALUE);
2743
                        }
2744
                    }
2745
                    _LMModelItem.Commit();
2746
                    
2747
                    ReleaseCOMObjects(_Attributes);
2748
                    ReleaseCOMObjects(_LMModelItem);
2749
                    ReleaseCOMObjects(_LMSymbol);
2750
                }
2751
            }
2752
            catch (Exception ex)
2753
            {
2754
                System.Windows.Forms.MessageBox.Show(ex.Message + "\r\n" + ex.StackTrace);
2755
            }
2756

    
2757
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetProgress, ++CurrentCount);
2758
        }
2759

    
2760
        /// <summary>
2761
        /// Input SpecBreak Attribute
2762
        /// </summary>
2763
        /// <param name="specBreak"></param>
2764
        private void InputSpecBreakAttribute(SpecBreak specBreak)
2765
        {
2766
            try
2767
            {
2768
                object upStreamObj = SPPIDUtil.FindObjectByUID(document, specBreak.UpStreamUID);
2769
                object downStreamObj = SPPIDUtil.FindObjectByUID(document, specBreak.DownStreamUID);
2770

    
2771
                if (upStreamObj != null &&
2772
                    downStreamObj != null)
2773
                {
2774
                    LMConnector targetLMConnector = FindBreakLineTarget(upStreamObj, downStreamObj);
2775

    
2776
                    if (targetLMConnector != null)
2777
                    {
2778
                        foreach (LMLabelPersist _LMLabelPersist in targetLMConnector.LabelPersists)
2779
                        {
2780
                            string symbolPath = _LMLabelPersist.get_FileName();
2781
                            AttributeMapping mapping = document.AttributeMappings.Find(x => x.SPPIDSYMBOLNAME == symbolPath);
2782
                            if (mapping != null)
2783
                            {
2784
                                BaseModel.Attribute attribute = specBreak.ATTRIBUTES.Find(y => y.UID == mapping.UID);
2785
                                if (attribute != null && !string.IsNullOrEmpty(attribute.VALUE) && attribute.VALUE != "None")
2786
                                {
2787
                                    string[] values = attribute.VALUE.Split(new char[] { ',' });
2788
                                    if (values.Length == 2)
2789
                                    {
2790
                                        string upStreamValue = values[0];
2791
                                        string downStreamValue = values[1];
2792

    
2793
                                        InputAttributeForSpecBreak(upStreamObj, downStreamObj, upStreamValue, downStreamValue, mapping.SPPIDATTRIBUTENAME);
2794
                                    }
2795
                                }
2796
                            }
2797
                        }
2798

    
2799
                        ReleaseCOMObjects(targetLMConnector);
2800
                    }
2801
                }
2802
            }
2803
            catch (Exception ex)
2804
            {
2805
                System.Windows.Forms.MessageBox.Show(ex.Message + "\r\n" + ex.StackTrace);
2806
            }
2807

    
2808
            #region 내부에서만 쓰는 메서드
2809
            void InputAttributeForSpecBreak(object upStreamObj, object downStreamObj, string upStreamValue, string downStreamValue, string sppidAttributeName)
2810
            {
2811
                try
2812
                {
2813
                    Symbol upStreamSymbol = upStreamObj as Symbol;
2814
                    Line upStreamLine = upStreamObj as Line;
2815
                    Symbol downStreamSymbol = downStreamObj as Symbol;
2816
                    Line downStreamLine = downStreamObj as Line;
2817
                    // 둘다 Line일 경우
2818
                    if (upStreamLine != null && downStreamLine != null)
2819
                    {
2820
                        InputLineAttributeForSpecBreakLine(upStreamLine, sppidAttributeName, upStreamValue);
2821
                        InputLineAttributeForSpecBreakLine(downStreamLine, sppidAttributeName, downStreamValue);
2822
                    }
2823
                    // 둘다 Symbol일 경우
2824
                    else if (upStreamSymbol != null && downStreamSymbol != null)
2825
                    {
2826
                        LMConnector zeroLenthConnector = FindBreakLineTarget(upStreamSymbol, downStreamSymbol);
2827
                        LMSymbol upStreamLMSymbol = dataSource.GetSymbol(upStreamSymbol.SPPID.RepresentationId);
2828
                        LMSymbol downStreamLMSymbol = dataSource.GetSymbol(downStreamSymbol.SPPID.RepresentationId);
2829

    
2830
                        foreach (LMConnector connector in upStreamLMSymbol.Avoid1Connectors)
2831
                        {
2832
                            if (connector.get_ItemStatus() != "Active")
2833
                                continue;
2834

    
2835
                            if (connector.Id != zeroLenthConnector.Id)
2836
                                InputLineAttributeForSpecBreakLMConnector(connector, sppidAttributeName, upStreamValue);
2837
                        }
2838

    
2839
                        foreach (LMConnector connector in upStreamLMSymbol.Avoid2Connectors)
2840
                        {
2841
                            if (connector.get_ItemStatus() != "Active")
2842
                                continue;
2843

    
2844
                            if (connector.Id != zeroLenthConnector.Id)
2845
                                InputLineAttributeForSpecBreakLMConnector(connector, sppidAttributeName, upStreamValue);
2846
                        }
2847

    
2848
                        foreach (LMConnector connector in downStreamLMSymbol.Avoid1Connectors)
2849
                        {
2850
                            if (connector.get_ItemStatus() != "Active")
2851
                                continue;
2852

    
2853
                            if (connector.Id != zeroLenthConnector.Id)
2854
                                InputLineAttributeForSpecBreakLMConnector(connector, sppidAttributeName, downStreamValue);
2855
                        }
2856

    
2857
                        foreach (LMConnector connector in downStreamLMSymbol.Avoid2Connectors)
2858
                        {
2859
                            if (connector.get_ItemStatus() != "Active")
2860
                                continue;
2861

    
2862
                            if (connector.Id != zeroLenthConnector.Id)
2863
                                InputLineAttributeForSpecBreakLMConnector(connector, sppidAttributeName, downStreamValue);
2864
                        }
2865

    
2866
                        ReleaseCOMObjects(zeroLenthConnector);
2867
                        ReleaseCOMObjects(upStreamLMSymbol);
2868
                        ReleaseCOMObjects(downStreamLMSymbol);
2869
                    }
2870
                    else if (upStreamSymbol != null && downStreamLine != null)
2871
                    {
2872
                        LMConnector zeroLenthConnector = FindBreakLineTarget(upStreamSymbol, downStreamLine);
2873
                        InputLineAttributeForSpecBreakLine(downStreamLine, sppidAttributeName, downStreamValue);
2874
                        LMSymbol upStreamLMSymbol = dataSource.GetSymbol(upStreamSymbol.SPPID.RepresentationId);
2875

    
2876
                        foreach (LMConnector connector in upStreamLMSymbol.Avoid1Connectors)
2877
                        {
2878
                            if (connector.get_ItemStatus() != "Active")
2879
                                continue;
2880

    
2881
                            if (connector.Id != zeroLenthConnector.Id)
2882
                                InputLineAttributeForSpecBreakLMConnector(connector, sppidAttributeName, upStreamValue);
2883
                        }
2884

    
2885
                        foreach (LMConnector connector in upStreamLMSymbol.Avoid2Connectors)
2886
                        {
2887
                            if (connector.get_ItemStatus() != "Active")
2888
                                continue;
2889

    
2890
                            if (connector.Id != zeroLenthConnector.Id)
2891
                                InputLineAttributeForSpecBreakLMConnector(connector, sppidAttributeName, upStreamValue);
2892
                        }
2893

    
2894
                        ReleaseCOMObjects(zeroLenthConnector);
2895
                        ReleaseCOMObjects(upStreamLMSymbol);
2896
                    }
2897
                    else if (upStreamLine != null && downStreamSymbol != null)
2898
                    {
2899
                        LMConnector zeroLenthConnector = FindBreakLineTarget(upStreamLine, downStreamSymbol);
2900
                        InputLineAttributeForSpecBreakLine(upStreamLine, sppidAttributeName, upStreamValue);
2901
                        LMSymbol downStreamLMSymbol = dataSource.GetSymbol(downStreamSymbol.SPPID.RepresentationId);
2902

    
2903
                        foreach (LMConnector connector in downStreamLMSymbol.Avoid1Connectors)
2904
                        {
2905
                            if (connector.get_ItemStatus() != "Active")
2906
                                continue;
2907

    
2908
                            if (connector.Id != zeroLenthConnector.Id)
2909
                                InputLineAttributeForSpecBreakLMConnector(connector, sppidAttributeName, downStreamValue);
2910
                        }
2911

    
2912
                        foreach (LMConnector connector in downStreamLMSymbol.Avoid2Connectors)
2913
                        {
2914
                            if (connector.get_ItemStatus() != "Active")
2915
                                continue;
2916

    
2917
                            if (connector.Id != zeroLenthConnector.Id)
2918
                                InputLineAttributeForSpecBreakLMConnector(connector, sppidAttributeName, downStreamValue);
2919
                        }
2920

    
2921
                        ReleaseCOMObjects(zeroLenthConnector);
2922
                        ReleaseCOMObjects(downStreamLMSymbol);
2923
                    }
2924
                }
2925
                catch (Exception ex)
2926
                {
2927
                    System.Windows.Forms.MessageBox.Show(ex.Message + "\r\n" + ex.StackTrace);
2928
                }
2929
            }
2930

    
2931
            void InputLineAttributeForSpecBreakLine(Line line, string attrName, string value)
2932
            {
2933
                try
2934
                {
2935
                    LMModelItem _LMModelItem = dataSource.GetModelItem(line.SPPID.ModelItemId);
2936
                    if (_LMModelItem != null && _LMModelItem.get_ItemStatus() == "Active")
2937
                    {
2938
                        LMAAttribute _LMAAttribute = _LMModelItem.Attributes[attrName];
2939
                        if (_LMAAttribute != null)
2940
                        {
2941
                            if (DBNull.Value.Equals(_LMAAttribute.get_Value()))
2942
                                _LMAAttribute.set_Value(value);
2943
                            else if (_LMAAttribute.get_Value() != value)
2944
                                _LMAAttribute.set_Value(value);
2945
                        }
2946

    
2947
                        _LMModelItem.Commit();
2948
                    }
2949
                    if (_LMModelItem != null)
2950
                        ReleaseCOMObjects(_LMModelItem);
2951
                }
2952
                catch (Exception ex)
2953
                {
2954
                    System.Windows.Forms.MessageBox.Show(ex.Message + "\r\n" + ex.StackTrace);
2955
                }
2956
            }
2957

    
2958
            void InputLineAttributeForSpecBreakLMConnector(LMConnector connector, string attrName, string value)
2959
            {
2960
                try
2961
                {
2962
                    LMModelItem _LMModelItem = dataSource.GetModelItem(connector.ModelItemID);
2963
                    if (_LMModelItem != null && _LMModelItem.get_ItemStatus() == "Active")
2964
                    {
2965
                        LMAAttribute _LMAAttribute = _LMModelItem.Attributes[attrName];
2966
                        if (_LMAAttribute != null)
2967
                        {
2968
                            if (DBNull.Value.Equals(_LMAAttribute.get_Value()))
2969
                                _LMAAttribute.set_Value(value);
2970
                            else if (_LMAAttribute.get_Value() != value)
2971
                                _LMAAttribute.set_Value(value);
2972
                        }
2973

    
2974
                        _LMModelItem.Commit();
2975
                    }
2976
                    if (_LMModelItem != null)
2977
                        ReleaseCOMObjects(_LMModelItem);
2978
                }
2979
                catch (Exception ex)
2980
                {
2981
                    System.Windows.Forms.MessageBox.Show(ex.Message + "\r\n" + ex.StackTrace);
2982
                }
2983
            }
2984
            #endregion
2985
        }
2986

    
2987
        /// <summary>
2988
        /// Text Modeling - Association일 경우는 Text대신 해당 맵핑된 Symbol로 모델링
2989
        /// </summary>
2990
        /// <param name="text"></param>
2991
        private void TextModeling(Text text)
2992
        {
2993
            LMSymbol _LMSymbol = null;
2994
            LMConnector connectedLMConnector = null;
2995
            try
2996
            {
2997
                //if (text.ASSOCIATION && !string.IsNullOrEmpty(text.OWNER) && text.OWNER != "None")
2998
                if (text.ASSOCIATION)
2999
                {
3000
                    object owner = SPPIDUtil.FindObjectByUID(document, text.OWNER);
3001
                    if (owner.GetType() == typeof(Symbol))
3002
                    {
3003
                        Symbol symbol = owner as Symbol;
3004
                        _LMSymbol = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
3005
                        if (_LMSymbol != null)
3006
                        {
3007
                            BaseModel.Attribute attribute = symbol.ATTRIBUTES.Find(x => x.ASSOCITEM == text.UID);
3008
                            if (!string.IsNullOrEmpty(attribute.VALUE) && attribute.VALUE != "None")
3009
                            {
3010
                                AttributeMapping mapping = document.AttributeMappings.Find(x => x.UID == attribute.UID && !string.IsNullOrEmpty(x.SPPIDSYMBOLNAME));
3011

    
3012
                                if (mapping != null)
3013
                                {
3014
                                    double x = 0;
3015
                                    double y = 0;
3016

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

    
3020
                                    LMLabelPersist _LMLabelPersist = _placement.PIDPlaceLabel(mapping.SPPIDSYMBOLNAME, ref array, Rotation: text.ANGLE, LabeledItem: _LMSymbol.AsLMRepresentation(), IsLeaderVisible: mapping.LeaderLine);
3021
                                    if (_LMLabelPersist != null)
3022
                                    {
3023
                                        _LMLabelPersist.Commit();
3024
                                        ReleaseCOMObjects(_LMLabelPersist);
3025
                                    }
3026
                                }
3027
                            }
3028
                        }
3029
                    }
3030
                    else if (owner.GetType() == typeof(Line))
3031
                    {
3032
                        Line line = owner as Line;
3033
                        Dictionary<LMConnector, List<double[]>> connectorVertices = GetPipeRunVertices(line.SPPID.ModelItemId);
3034
                        connectedLMConnector = FindTargetLMConnectorForLabel(connectorVertices, text.SPPID.ORIGINAL_X, text.SPPID.ORIGINAL_Y);
3035

    
3036
                        if (connectedLMConnector != null)
3037
                        {
3038
                            Association association = line.ASSOCIATIONS.Find(x => x.VALUE == text.UID);
3039
                            List<BaseModel.Attribute> attributes = line.ATTRIBUTES.FindAll(x => x.ATTRIBUTETYPE == association.TYPE);
3040
                            AttributeMapping mapping = null;
3041
                            foreach (var attribute in attributes)
3042
                            {
3043
                                if (string.IsNullOrEmpty(attribute.VALUE) || attribute.VALUE == "None")
3044
                                    continue;
3045

    
3046
                                mapping = document.AttributeMappings.Find(x => x.UID == attribute.UID && !string.IsNullOrEmpty(x.SPPIDSYMBOLNAME));
3047
                                if (mapping != null)
3048
                                    break;
3049
                            }
3050

    
3051
                            if (mapping != null)
3052
                            {
3053
                                double x = 0;
3054
                                double y = 0;
3055

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

    
3059
                                LMLabelPersist _LMLabelPersist = _placement.PIDPlaceLabel(mapping.SPPIDSYMBOLNAME, ref array, Rotation: text.ANGLE, LabeledItem: connectedLMConnector.AsLMRepresentation(), IsLeaderVisible: mapping.LeaderLine);
3060
                                if (_LMLabelPersist != null)
3061
                                {
3062
                                    _LMLabelPersist.Commit();
3063
                                    ReleaseCOMObjects(_LMLabelPersist);
3064
                                }
3065
                            }
3066
                        }
3067
                    }
3068
                }
3069
                else
3070
                {
3071
                    LMItemNote _LMItemNote = null;
3072
                    LMAAttribute _LMAAttribute = null;
3073

    
3074
                    double x = 0;
3075
                    double y = 0;
3076

    
3077
                    CalcLabelLocation(ref x, ref y, text.SPPID.ORIGINAL_X, text.SPPID.ORIGINAL_Y, text.SPPIDLabelLocation, _ETCSetting.TextLocation);
3078

    
3079
                    _LMSymbol = _placement.PIDPlaceSymbol(text.SPPID.MAPPINGNAME, x, y);
3080
                    _LMSymbol.Commit();
3081
                    _LMItemNote = _placement.PIDDataSource.GetItemNote(_LMSymbol.ModelItemID);
3082
                    _LMItemNote.Commit();
3083
                    _LMAAttribute = _LMItemNote.Attributes["Note.Body"];
3084
                    _LMAAttribute.set_Value(text.VALUE);
3085
                    _LMItemNote.Commit();
3086

    
3087
                    if (_LMAAttribute != null)
3088
                        ReleaseCOMObjects(_LMAAttribute);
3089
                    if (_LMItemNote != null)
3090
                        ReleaseCOMObjects(_LMItemNote);
3091
                }
3092
            }
3093
            catch (Exception ex)
3094
            {
3095

    
3096
            }
3097
            finally
3098
            {
3099
                if (_LMSymbol != null)
3100
                    ReleaseCOMObjects(_LMSymbol);
3101
            }
3102

    
3103
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetProgress, ++CurrentCount);
3104
        }
3105

    
3106
        /// <summary>
3107
        /// Note Modeling
3108
        /// </summary>
3109
        /// <param name="note"></param>
3110
        private void NoteModeling(Note note)
3111
        {
3112
            LMSymbol _LMSymbol = null;
3113
            LMItemNote _LMItemNote = null;
3114
            LMAAttribute _LMAAttribute = null;
3115

    
3116
            try
3117
            {
3118
                //if (string.IsNullOrEmpty(note.OWNER) || note.OWNER == "None")
3119
                //{
3120
                //    double x = 0;
3121
                //    double y = 0;
3122

    
3123
                //    CalcLabelLocation(ref x, ref y, note.SPPID.ORIGINAL_X, note.SPPID.ORIGINAL_Y, note.SPPIDLabelLocation, _ETCSetting.NoteLocation);
3124

    
3125
                //    _LMSymbol = _placement.PIDPlaceSymbol(note.SPPID.MAPPINGNAME, x, y);
3126
                //    _LMSymbol.Commit();
3127
                //    _LMItemNote = _placement.PIDDataSource.GetItemNote(_LMSymbol.ModelItemID);
3128
                //    _LMItemNote.Commit();
3129
                //    _LMAAttribute = _LMItemNote.Attributes["Note.Body"];
3130
                //    _LMAAttribute.set_Value(note.VALUE);
3131
                //    _LMItemNote.Commit();
3132
                //}
3133
            }
3134
            catch (Exception ex)
3135
            {
3136

    
3137
            }
3138
            finally
3139
            {
3140
                if (_LMAAttribute != null)
3141
                    ReleaseCOMObjects(_LMAAttribute);
3142
                if (_LMItemNote != null)
3143
                    ReleaseCOMObjects(_LMItemNote);
3144
                if (_LMSymbol != null)
3145
                    ReleaseCOMObjects(_LMSymbol);
3146
            }
3147

    
3148
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetProgress, ++CurrentCount);
3149
        }
3150

    
3151
        private void NoteSymbolModeling(Symbol symbol)
3152
        {
3153
            if (symbol.TYPE == "Notes")
3154
            {
3155
                string mappingPath = symbol.SPPID.MAPPINGNAME;
3156
                double x = symbol.SPPID.ORIGINAL_X;
3157
                double y = symbol.SPPID.ORIGINAL_Y;
3158
                int mirror = 0;
3159
                double angle = symbol.ANGLE;
3160

    
3161
                LMSymbol _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle);
3162
                ReleaseCOMObjects(_LMSymbol);
3163

    
3164
                InputSymbolAttribute(symbol, symbol.ATTRIBUTES);
3165
            }
3166
        }
3167

    
3168
        /// <summary>
3169
        /// Label의 좌표를 구하는 메서드(ID2 기준의 좌표 -> SPPID 좌표)
3170
        /// </summary>
3171
        /// <param name="x"></param>
3172
        /// <param name="y"></param>
3173
        /// <param name="originX"></param>
3174
        /// <param name="originY"></param>
3175
        /// <param name="SPPIDLabelLocation"></param>
3176
        /// <param name="location"></param>
3177
        private void CalcLabelLocation(ref double x, ref double y, double originX, double originY, SPPIDLabelLocationInfo SPPIDLabelLocation, Location location)
3178
        {
3179
            if (location == Location.None)
3180
            {
3181
                x = originX;
3182
                y = originY;
3183
            }
3184
            else
3185
            {
3186
                if (location.HasFlag(Location.Center))
3187
                {
3188
                    x = (SPPIDLabelLocation.X1 + SPPIDLabelLocation.X2) / 2;
3189
                    y = (SPPIDLabelLocation.Y1 + SPPIDLabelLocation.Y2) / 2;
3190
                }
3191

    
3192
                if (location.HasFlag(Location.Left))
3193
                    x = SPPIDLabelLocation.X1;
3194
                else if (location.HasFlag(Location.Right))
3195
                    x = SPPIDLabelLocation.X2;
3196

    
3197
                if (location.HasFlag(Location.Down))
3198
                    y = SPPIDLabelLocation.Y1;
3199
                else if (location.HasFlag(Location.Up))
3200
                    y = SPPIDLabelLocation.Y2;
3201
            }
3202
        }
3203

    
3204
        /// <summary>
3205
        /// Symbol의 우선순위 Modeling 목록을 가져온다.
3206
        /// 1. Angle Valve
3207
        /// 2. 3개로 이루어진 Symbol Group
3208
        /// </summary>
3209
        /// <returns></returns>
3210
        private List<Symbol> GetPrioritySymbol()
3211
        {
3212
            DataTable symbolTable = document.SymbolTable;
3213

    
3214
            // List에 순서대로 쌓는다.
3215
            List<Symbol> symbols = new List<Symbol>();
3216
            // Angle Valve 부터
3217
            foreach (var symbol in document.SYMBOLS.FindAll(x => x.CONNECTORS.Count == 2))
3218
            {
3219
                if (!symbols.Contains(symbol))
3220
                {
3221
                    double originX = 0;
3222
                    double originY = 0;
3223

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

    
3228
                    SlopeType slopeType1 = SlopeType.None;
3229
                    SlopeType slopeType2 = SlopeType.None;
3230
                    foreach (Connector connector in symbol.CONNECTORS)
3231
                    {
3232
                        double connectorX = 0;
3233
                        double connectorY = 0;
3234
                        SPPIDUtil.ConvertPointBystring(connector.CONNECTPOINT, ref connectorX, ref connectorY);
3235
                        if (slopeType1 == SlopeType.None)
3236
                            slopeType1 = SPPIDUtil.CalcSlope(originX, originY, connectorX, connectorY);
3237
                        else
3238
                            slopeType2 = SPPIDUtil.CalcSlope(originX, originY, connectorX, connectorY);
3239
                    }
3240

    
3241
                    if ((slopeType1 == SlopeType.VERTICAL && slopeType2 == SlopeType.HORIZONTAL) ||
3242
                        (slopeType2 == SlopeType.VERTICAL && slopeType1 == SlopeType.HORIZONTAL))
3243
                        symbols.Add(symbol);
3244
                }
3245
            }
3246

    
3247
            // 3개의 Symbol이 뭉쳐 있을 때
3248
            foreach (var item in document.SYMBOLS)
3249
            {
3250
                List<Symbol> group = new List<Symbol>();
3251
                SPPIDUtil.FindConnectedSymbolGroup(document, item, group);
3252
                if (group.Count == 3)
3253
                {
3254
                    Symbol symbol = SPPIDUtil.FindCenterAtThreeSymbols(document, group);
3255
                    if (!symbols.Contains(symbol))
3256
                        symbols.Add(symbol);
3257
                }
3258
            }
3259

    
3260
            // Connection Point가 3개 이상
3261
            foreach (var symbol in document.SYMBOLS)
3262
                if (symbol.CONNECTORS.Count > 2 && !symbols.Contains(symbol))
3263
                    symbols.Add(symbol);
3264

    
3265
            return symbols;
3266
        }
3267

    
3268
        /// <summary>
3269
        /// Graphic OID로 해당 Symbol의 크기를 구하여 Zoom
3270
        /// </summary>
3271
        /// <param name="graphicOID"></param>
3272
        /// <param name="milliseconds"></param>
3273
        private void ZoomObjectByGraphicOID(string graphicOID, int milliseconds = 150)
3274
        {
3275
            if (radApp.ActiveDocument.ActiveSheet.DrawingObjects[graphicOID] != null)
3276
            {
3277
                double minX = 0;
3278
                double minY = 0;
3279
                double maxX = 0;
3280
                double maxY = 0;
3281
                radApp.ActiveDocument.ActiveSheet.DrawingObjects[graphicOID].Range(out minX, out minY, out maxX, out maxY);
3282
                radApp.ActiveWindow.ZoomArea2(minX - 0.007, minY - 0.007, maxX + 0.007, maxY + 0.007, null);
3283

    
3284
                Thread.Sleep(milliseconds);
3285
            }
3286
        }
3287

    
3288
        /// <summary>
3289
        /// ComObject를 Release
3290
        /// </summary>
3291
        /// <param name="objVars"></param>
3292
        public void ReleaseCOMObjects(params object[] objVars)
3293
        {
3294
            int intNewRefCount = 0;
3295
            foreach (object obj in objVars)
3296
            {
3297
                if (!Information.IsNothing(obj) && System.Runtime.InteropServices.Marshal.IsComObject(obj))
3298
                    intNewRefCount = intNewRefCount + System.Runtime.InteropServices.Marshal.FinalReleaseComObject(obj);
3299
            }
3300
        }
3301
    }
3302
}
클립보드 이미지 추가 (최대 크기: 500 MB)