프로젝트

일반

사용자정보

통계
| 개정판:

hytos / DTI_PID / SPPIDConverter / AutoModeling.cs @ eaa41534

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

1
using System;
2
using System.Collections.Generic;
3
using System.Linq;
4
using System.Text;
5
using System.Threading.Tasks;
6
using System.Data;
7
using Llama;
8
using Plaice;
9
using Ingr.RAD2D.Interop.RAD2D;
10
using Ingr.RAD2D.Internal;
11
using Ingr.RAD2D.Helper;
12
using Converter.BaseModel;
13
using Converter.SPPID.Model;
14
using Converter.SPPID.Properties;
15
using Converter.SPPID.Util;
16
using Converter.SPPID.DB;
17
using Ingr.RAD2D.MacroControls.CmdCtrl;
18
using Ingr.RAD2D;
19
using System.Windows;
20
using System.Threading;
21
using System.Drawing;
22
using Microsoft.VisualBasic;
23
using Newtonsoft.Json;
24
using DevExpress.XtraSplashScreen;
25
namespace Converter.SPPID
26
{
27
    [Flags]
28
    public enum SegmentLocation
29
    {
30
        None = 0,
31
        Right = 1,
32
        Left = 2,
33
        Down = 4,
34
        Up = 8
35
    }
36
    public class AutoModeling : IDisposable
37
    {
38
        Placement _placement;
39
        LMADataSource dataSource;
40
        string drawingID;
41
        dynamic newDrawing;
42
        dynamic application;
43
        bool closeDocument;
44
        Ingr.RAD2D.Application radApp;
45
        SPPID_Document document;
46
        ETCSetting _ETCSetting;
47
        DataTable nominalDiameterTable = null;
48
        public string DocumentLabelText { get; set; }
49

    
50
        List<Line> BranchLines = new List<Line>();
51
        List<string> ZeroLengthSymbolToSymbolModelItemID = new List<string>();
52
        List<string> ZeroLengthModelItemID = new List<string>();
53
        List<string> ZeroLengthModelItemIDReverse = new List<string>();
54
        List<Symbol> prioritySymbols;
55
        List<string> FlowMarkRepIds = new List<string>();
56

    
57
        public AutoModeling(SPPID_Document document, bool closeDocument)
58
        {
59
            application = Interaction.GetObject("", "PIDAutomation.Application");
60
            WrapperApplication wApp = new WrapperApplication(application.Application);
61
            radApp = wApp.RADApplication;
62

    
63
            this.closeDocument = closeDocument;
64
            this.document = document;
65
            this._ETCSetting = ETCSetting.GetInstance();
66
        }
67

    
68
        private void SetSystemEditingCommand(bool value)
69
        {
70
            foreach (var item in radApp.Commands)
71
            {
72
                if (item.Argument == "SystemEditingCmd.SystemEditing")
73
                {
74
                    if (item.Checked != value)
75
                    {
76
                        radApp.RunMacro("systemeditingcmd.dll");
77
                        break;
78
                    }
79

    
80
                }
81
            }
82
        }
83

    
84
        /// <summary>
85
        /// 도면 단위당 실행되는 메서드
86
        /// </summary>
87
        public void Run()
88
        {
89
            string drawingNumber = document.DrawingNumber;
90
            string drawingName = document.DrawingName;
91
            try
92
            {
93
                nominalDiameterTable = Project_DB.SelectProjectNominalDiameter();
94
                _placement = new Placement();
95
                dataSource = _placement.PIDDataSource;
96
                
97
                if (CreateDocument(ref drawingNumber, ref drawingName) && DocumentCoordinateCorrection())
98
                {
99
                    Log.Write("Start Modeling");
100
                    SplashScreenManager.ShowForm(typeof(SPPIDSplashScreen), true, true);
101
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetParent, (IntPtr)radApp.HWnd);
102
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetAllStepCount, 25);
103
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetDocumentName, DocumentLabelText);
104

    
105
                    // VendorPackage Modeling
106
                    // ID2에서 VendorPackage로 된 Symbol을 SPPID에서 그림
107
                    RunVendorPackageModeling();
108
                    // Equipment Modeling
109
                    // Id2에서 인식한 Equipment일 경우 SPPID에 Draft
110
                    RunEquipmentModeling();
111
                    // Symbol Modeling
112
                    // ID2의 Symbol Draft
113
                    // 단 Symbol draft할 때 붙어 있는 symbol도 draft함
114
                    RunSymbolModeling();
115
                    // LineRun Line Modeling
116
                    // Line 그리는 우선 순위 
117
                    // 1. Branch 없는 것
118
                    // 2. Symbol 연결 개수
119
                    // 3. Symbol 제외 Item 연결 개수
120
                    // 4. ConnectedItem이 없는것
121
                    RunLineModeling();
122
                    // Vent Drain Modeling
123
                    // Vent/Drain으로 인식한 Item draft
124
                    // 인식 조건
125
                    // 1. ID2에서 Line이 하나며 Branch된 Line이 있고
126
                    // 2. Valve가 line에 붙어있다.
127
                    RunVentDrainModeling();
128
                    // Clear Attribute
129
                    // SPPID에서 Line 생성 시 자동으로 Nominal Diameter가 입력되는 경우가 있음
130
                    // 모든 Item의 Nominal Diameter 속성값 초기화
131
                    RunClearNominalDiameter();
132
                    // Join SameConnector
133
                    // 기존 Line을 그릴때 SPPID에서는 같은 Run으로 생성되지 않고 각각 PipeRun이 생성됨
134
                    // ID2의 EndBreak등 segmentbreak가 없으면 Line을 합침
135
                    RunJoinRunForSameConnector();
136
                    // Join Run
137
                    // 같은 Type의 Line일 경우 Join함
138
                    RunJoinRun();
139

    
140
                    // EndBreak Modeling
141
                    RunEndBreakModeling();
142
                    // SpecBreak Modeling
143
                    RunSpecBreakModeling();
144
                    //Line Number Modeling
145
                    // Label만 draft
146
                    RunLineNumberModeling();
147
                    // Note Modeling
148
                    RunNoteModeling();
149
                    // Text Modeling
150
                    RunTextModeling();
151
                    // Input LineNumber Attribute
152
                    RunInputLineNumberAttribute();
153
                    // Input Symbol Attribute
154
                    RunInputSymbolAttribute();
155
                    // Input SpecBreak Attribute
156
                    RunInputSpecBreakAttribute();
157
                    // Input EndBreak Attribute
158
                    RunInputEndBreakAttribute();
159
                    // Label Symbol Modeling
160
                    RunLabelSymbolModeling();
161

    
162
                    // Correct Text
163
                    // LabelPersist 정렬 로직
164
                    // 예) Valve Size label 등
165
                    RunCorrectAssociationText();
166
                    // ETC
167
                    // Label을 Front로 옮김
168
                    RunETC();
169
                    // input bulk attribute
170
                    RunBulkAttribute();
171
                    // log file 생성
172
                    document.CheckModelingResult();
173
                }
174
            }
175
            catch (Exception ex)
176
            {
177
                if (SplashScreenManager.Default != null && SplashScreenManager.Default.IsSplashFormVisible)
178
                {
179
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.ClearParent, null);
180
                    SplashScreenManager.CloseForm(false);
181
                    Log.Write("\r\n");
182
                }
183
                System.Windows.Forms.MessageBox.Show(ex.Message + "\r\n" + ex.StackTrace);
184
            }
185
            finally
186
            {
187
                Project_DB.InsertDrawingInfoAndOPCInfo(document.PATH, drawingNumber, drawingName, document);
188
                //Project_DB.InsertLineNumberInfo(document.PATH, drawingNumber, drawingName, document);
189

    
190
                if (SplashScreenManager.Default != null && SplashScreenManager.Default.IsSplashFormVisible)
191
                {
192
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.ClearParent, null);
193
                    SplashScreenManager.CloseForm(false);
194
                    Log.Write("\r\n");
195
                }
196
                Thread.Sleep(1000);
197

    
198
                Log.Write("End Modeling");
199
                radApp.ActiveWindow.Fit();
200

    
201
                ReleaseCOMObjects(application);
202
                application = null;
203
                if (radApp.ActiveDocument != null)
204
                {
205
                    if (closeDocument && newDrawing != null)
206
                    {
207
                        newDrawing.Save();
208
                        newDrawing.CloseDrawing(true);
209
                        ReleaseCOMObjects(newDrawing); 
210
                        newDrawing = null;
211
                    }
212
                    else if (newDrawing == null)
213
                    {
214
                        Log.Write("error document");
215
                    }
216
                }
217

    
218
                ReleaseCOMObjects(dataSource);
219
                dataSource = null;
220
                ReleaseCOMObjects(_placement);
221
                _placement = null;
222

    
223
                Thread.Sleep(1000);
224
            }
225
        }
226

    
227
        private void RunVendorPackageModeling()
228
        {
229
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetAllProgress, document.VendorPackages.Count);
230
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "VendorPackages Modeling");
231
            foreach (VendorPackage item in document.VendorPackages)
232
            {
233
                try
234
                {
235
                    VendorPackageModeling(item);
236
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
237
                }
238
                catch (Exception ex)
239
                {
240
                    Log.Write("Error in RunVendorPackageModeling");
241
                    Log.Write("UID : " + item.UID);
242
                    Log.Write(ex.Message);
243
                    Log.Write(ex.StackTrace);
244
                }
245
            }
246
        }
247
        private void RunEquipmentModeling()
248
        {
249
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetAllProgress, document.Equipments.Count);
250
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Equipments Modeling");
251
            for (int i = 0; i < document.Equipments.Count; i++)
252
            {
253
                Equipment item = document.Equipments[i];
254
                try
255
                {
256
                    if (!string.IsNullOrEmpty(item.SPPID.RepresentationId))
257
                        continue;
258
                    EquipmentModeling(item);
259
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetProgress, document.Equipments.FindAll(x => !string.IsNullOrEmpty(x.SPPID.RepresentationId)).Count);
260
                    if (!string.IsNullOrEmpty(item.SPPID.RepresentationId))
261
                        i = -1;
262
                }
263
                catch (Exception ex)
264
                {
265
                    Log.Write("Error in EquipmentModeling");
266
                    Log.Write("UID : " + item.UID);
267
                    Log.Write(ex.Message);
268
                    Log.Write(ex.StackTrace);
269
                }
270
            }
271
        }
272
        private void RunSymbolModeling()
273
        {
274
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetAllProgress, document.SYMBOLS.Count);
275
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Symbol Modeling");
276
            prioritySymbols = GetPrioritySymbol();
277
            foreach (var item in prioritySymbols)
278
            {
279
                try
280
                {
281
                    if (document.VentDrainSymbol.Contains(item))
282
                        continue;
283
                    SymbolModelingBySymbol(item);
284
                }
285
                catch (Exception ex)
286
                {
287
                    Log.Write("Error in SymbolModelingByPriority");
288
                    Log.Write("UID : " + item.UID);
289
                    Log.Write(ex.Message);
290
                    Log.Write(ex.StackTrace);
291
                }
292
            }
293
        }
294
        private void RunLineModeling()
295
        {
296
            List<Line> AllLine = document.LINES.ToList();
297
            List<Line> stepLast_Line = document.LINES.FindAll(x => x.CONNECTORS.FindAll(y => y.ConnectedObject != null && y.ConnectedObject.GetType() == typeof(Symbol)).Count == 2 &&
298
            !SPPIDUtil.IsBranchedLine(document, x));
299
            List<Line> step1_Line = AllLine.FindAll(x => !stepLast_Line.Contains(x));
300

    
301
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetAllProgress, step1_Line.Count);
302
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Line Modeling - 1");
303

    
304
            SetPriorityLine(step1_Line);
305
            foreach (var item in step1_Line)
306
            {
307
                try
308
                {
309
                    if (document.VentDrainLine.Contains(item))
310
                        continue;
311
                    NewLineModeling(item);
312
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
313
                }
314
                catch (Exception ex)
315
                {
316
                    Log.Write("Error in NewLineModeling");
317
                    Log.Write("UID : " + item.UID);
318
                    Log.Write(ex.Message);
319
                    Log.Write(ex.StackTrace);
320
                }
321
            }
322

    
323
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetAllProgress, BranchLines.Count);
324
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Line Modeling - 2");
325
            int branchCount = BranchLines.Count;
326
            while (BranchLines.Count > 0)
327
            {
328
                try
329
                {
330
                    SortBranchLines();
331
                    Line item = BranchLines[0];
332
                    NewLineModeling(item, true);
333
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
334
                }
335
                catch (Exception ex)
336
                {
337
                    Log.Write("Error in NewLineModeling");
338
                    Log.Write("UID : " + BranchLines[0].UID);
339
                    Log.Write(ex.Message);
340
                    Log.Write(ex.StackTrace);
341
                    BranchLines.Remove(BranchLines[0]);
342
                }
343
            }
344

    
345
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetAllProgress, stepLast_Line.Count);
346
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Line Modeling - 3");
347
            foreach (var item in stepLast_Line)
348
            {
349
                try
350
                {
351
                    if (document.VentDrainLine.Contains(item))
352
                        continue;
353
                    NewLineModeling(item);
354
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
355
                }
356
                catch (Exception ex)
357
                {
358
                    Log.Write("Error in NewLineModeling");
359
                    Log.Write("UID : " + item.UID);
360
                    Log.Write(ex.Message);
361
                    Log.Write(ex.StackTrace);
362
                }
363
            }
364
        }
365
        private void RunVentDrainModeling()
366
        {
367
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetAllProgress, document.VentDrainLine.Count);
368
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Vent Drain Modeling");
369
            foreach (var item in document.VentDrainLine)
370
            {
371
                try
372
                {
373
                    Connector connector = item.CONNECTORS.Find(x => x.ConnectedObject != null && x.ConnectedObject.GetType() == typeof(Symbol));
374
                    if (connector != null)
375
                    {
376
                        SetCoordinate();
377
                        Symbol connSymbol = connector.ConnectedObject as Symbol;
378
                        SymbolModeling(connSymbol, null);
379
                        NewLineModeling(item, true);
380

    
381
                        GridSetting grid = GridSetting.GetInstance();
382
                        int count = grid.DrainValveCellCount;
383
                        double length = grid.Length;
384

    
385
                        // 길이 확인
386
                        if (!string.IsNullOrEmpty(item.SPPID.ModelItemId))
387
                        {
388
                            LMConnector _LMConnector = GetLMConnectorOnlyOne(item.SPPID.ModelItemId);
389
                            if (_LMConnector != null)
390
                            {
391
                                double[] connectorRange = GetConnectorRange(_LMConnector);
392
                                double connectorLength = double.NaN;
393
                                if (item.SlopeType == SlopeType.HORIZONTAL)
394
                                    connectorLength = connectorRange[2] - connectorRange[0];
395
                                else if (item.SlopeType == SlopeType.VERTICAL)
396
                                    connectorLength = connectorRange[3] - connectorRange[1];
397

    
398
                                if (!double.IsNaN(connectorLength) && connectorLength != count * length)
399
                                {
400
                                    double move = count * length - connectorLength;
401
                                    List<Symbol> group = new List<Symbol>();
402
                                    SPPIDUtil.FindConnectedSymbolGroup(document, connSymbol, group);
403
                                    foreach (var symbol in group)
404
                                    {
405
                                        int connSymbolIndex = item.CONNECTORS.IndexOf(item.CONNECTORS.Find(x => x.ConnectedObject == connSymbol));
406
                                        if (item.SlopeType == SlopeType.HORIZONTAL)
407
                                        {
408
                                            if (connSymbolIndex == 0)
409
                                            {
410
                                                if (item.SPPID.START_X > item.SPPID.END_X)
411
                                                    symbol.SPPID.ORIGINAL_X = symbol.SPPID.ORIGINAL_X + move;
412
                                                else
413
                                                    symbol.SPPID.ORIGINAL_X = symbol.SPPID.ORIGINAL_X - move;
414
                                            }
415
                                            else
416
                                            {
417
                                                if (item.SPPID.START_X < item.SPPID.END_X)
418
                                                    symbol.SPPID.ORIGINAL_X = symbol.SPPID.ORIGINAL_X + move;
419
                                                else
420
                                                    symbol.SPPID.ORIGINAL_X = symbol.SPPID.ORIGINAL_X - move;
421
                                            }
422
                                        }
423
                                        else if (item.SlopeType == SlopeType.VERTICAL)
424
                                        {
425
                                            if (connSymbolIndex == 0)
426
                                            {
427
                                                if (item.SPPID.START_Y > item.SPPID.END_Y)
428
                                                    symbol.SPPID.ORIGINAL_Y = symbol.SPPID.ORIGINAL_Y + move;
429
                                                else
430
                                                    symbol.SPPID.ORIGINAL_Y = symbol.SPPID.ORIGINAL_Y - move;
431
                                            }
432
                                            else
433
                                            {
434
                                                if (item.SPPID.START_Y < item.SPPID.END_Y)
435
                                                    symbol.SPPID.ORIGINAL_Y = symbol.SPPID.ORIGINAL_Y + move;
436
                                                else
437
                                                    symbol.SPPID.ORIGINAL_Y = symbol.SPPID.ORIGINAL_Y - move;
438
                                            }
439
                                        }
440
                                    }
441

    
442
                                    // 제거
443
                                    RemoveSymbol(connSymbol);
444
                                    RemoveLine(item);
445

    
446
                                    // 재생성
447
                                    SymbolModelingBySymbol(connSymbol);
448
                                    NewLineModeling(item, true);
449
                                }
450
                            }
451

    
452
                            ReleaseCOMObjects(_LMConnector);
453
                            _LMConnector = null;
454
                        }
455
                    }
456
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
457
                }
458
                catch (Exception ex)
459
                {
460
                    Log.Write("Error in NewLineModeling");
461
                    Log.Write("UID : " + item.UID);
462
                    Log.Write(ex.Message);
463
                    Log.Write(ex.StackTrace);
464
                }
465

    
466
                void SetCoordinate()
467
                {
468
                    Connector branchConnector = item.CONNECTORS.Find(loop => loop.ConnectedObject != null && loop.ConnectedObject.GetType() == typeof(Line));
469
                    if (branchConnector != null)
470
                    {
471
                        Line connLine = branchConnector.ConnectedObject as Line;
472
                        double x = 0;
473
                        double y = 0;
474
                        GetTargetLineConnectorPoint(branchConnector, item, ref x, ref y);
475
                        LMConnector targetConnector = FindTargetLMConnectorForBranch(item, connLine, ref x, ref y);
476
                        if (targetConnector != null)
477
                        {
478
                            List<Symbol> group = new List<Symbol>();
479
                            SPPIDUtil.FindConnectedSymbolGroup(document, item.CONNECTORS.Find(loop => loop != branchConnector).ConnectedObject as Symbol, group);
480
                            if (item.SlopeType == SlopeType.HORIZONTAL)
481
                            {
482
                                item.SPPID.START_Y = y;
483
                                item.SPPID.END_Y = y;
484
                                foreach (var symbol in group)
485
                                {
486
                                    symbol.SPPID.ORIGINAL_Y = y;
487
                                    symbol.SPPID.SPPID_Y = y;
488
                                }
489
                            }
490
                            else if (item.SlopeType == SlopeType.VERTICAL)
491
                            {
492
                                item.SPPID.START_X = x;
493
                                item.SPPID.END_X = x;
494
                                foreach (var symbol in group)
495
                                {
496
                                    symbol.SPPID.ORIGINAL_X = x;
497
                                    symbol.SPPID.SPPID_X = x;
498
                                }
499
                            }
500
                        }
501
                        ReleaseCOMObjects(targetConnector);
502
                        targetConnector = null;
503
                    }
504
                }
505
            }
506
        }
507
        private void RunClearNominalDiameter()
508
        {
509
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetAllProgress, document.SYMBOLS.Count + document.LINES.Count);
510
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Clear Attribute");
511
            return;
512

    
513
            List<string> endClearModelItemID = new List<string>();
514
            for (int i = 0; i < document.LINES.Count; i++)
515
            {
516
                Line item = document.LINES[i];
517
                string modelItemID = item.SPPID.ModelItemId;
518
                if (!string.IsNullOrEmpty(modelItemID))
519
                {
520
                    LMModelItem modelItem = dataSource.GetModelItem(modelItemID);
521
                    if (modelItem != null)
522
                    {
523
                        LMAAttribute attribute = modelItem.Attributes["NominalDiameter"];
524
                        if (attribute != null)
525
                            attribute.set_Value(DBNull.Value);
526

    
527
                        modelItem.Commit();
528
                        ReleaseCOMObjects(modelItem);
529
                        modelItem = null;
530
                    }
531
                }
532
                if (!endClearModelItemID.Contains(modelItemID))
533
                    endClearModelItemID.Add(modelItemID);
534
                SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
535
            }
536
            for (int i = 0; i < document.SYMBOLS.Count; i++)
537
            {
538
                Symbol item = document.SYMBOLS[i];
539
                string repID = item.SPPID.RepresentationId;
540
                string modelItemID = item.SPPID.ModelItemID;
541
                if (!string.IsNullOrEmpty(modelItemID))
542
                {
543
                    LMModelItem modelItem = dataSource.GetModelItem(modelItemID);
544
                    if (modelItem != null)
545
                    {
546
                        LMAAttribute attribute = modelItem.Attributes["NominalDiameter"];
547
                        if (attribute != null)
548
                            attribute.set_Value(DBNull.Value);
549
                        int index = 1;
550
                        while (true)
551
                        {
552
                            attribute = modelItem.Attributes[string.Format("PipingPoint{0}.NominalDiameter", index)];
553
                            if (attribute != null)
554
                                attribute.set_Value(DBNull.Value);
555
                            else
556
                                break;
557
                            index++;
558
                        }
559
                        modelItem.Commit();
560
                        ReleaseCOMObjects(modelItem);
561
                        modelItem = null;
562
                    }
563
                }
564
                if (!string.IsNullOrEmpty(repID))
565
                {
566
                    LMSymbol symbol = dataSource.GetSymbol(repID);
567
                    if (symbol != null)
568
                    {
569
                        foreach (LMConnector connector in symbol.Connect1Connectors)
570
                        {
571
                            if (connector.get_ItemStatus() == "Active" && !endClearModelItemID.Contains(connector.ModelItemID))
572
                            {
573
                                endClearModelItemID.Add(connector.ModelItemID);
574
                                LMModelItem modelItem = connector.ModelItemObject;
575
                                if (modelItem != null)
576
                                {
577
                                    LMAAttribute attribute = modelItem.Attributes["NominalDiameter"];
578
                                    if (attribute != null)
579
                                        attribute.set_Value(DBNull.Value);
580

    
581
                                    modelItem.Commit();
582
                                    ReleaseCOMObjects(modelItem);
583
                                    modelItem = null;
584
                                }
585
                            }
586
                        }
587
                        foreach (LMConnector connector in symbol.Connect2Connectors)
588
                        {
589
                            if (connector.get_ItemStatus() == "Active" && !endClearModelItemID.Contains(connector.ModelItemID))
590
                            {
591
                                endClearModelItemID.Add(connector.ModelItemID);
592
                                LMModelItem modelItem = connector.ModelItemObject;
593
                                if (modelItem != null)
594
                                {
595
                                    LMAAttribute attribute = modelItem.Attributes["NominalDiameter"];
596
                                    if (attribute != null)
597
                                        attribute.set_Value(DBNull.Value);
598

    
599
                                    modelItem.Commit();
600
                                    ReleaseCOMObjects(modelItem);
601
                                    modelItem = null;
602
                                }
603
                            }
604
                        }
605
                    }
606
                    ReleaseCOMObjects(symbol);
607
                    symbol = null;
608
                }
609
                SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
610
            }
611
        }
612
        private void RunClearValueInconsistancy()
613
        {
614
            int count = 1;
615
            bool loop = true;
616
            while (loop)
617
            {
618
                loop = false;
619
                LMAFilter filter = new LMAFilter();
620
                LMACriterion criterion = new LMACriterion();
621
                filter.ItemType = "Relationship";
622
                criterion.SourceAttributeName = "SP_DRAWINGID";
623
                criterion.Operator = "=";
624
                criterion.set_ValueAttribute(drawingID);
625
                filter.get_Criteria().Add(criterion);
626

    
627
                LMRelationships relationships = new LMRelationships();
628
                relationships.Collect(dataSource, Filter: filter);
629

    
630
                SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetAllProgress, relationships.Count);
631
                if (count > 1)
632
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStepMinus, null);
633
                SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Clear Inconsistent Property Value - " + count);
634
                foreach (LMRelationship relationship in relationships)
635
                {
636
                    foreach (LMInconsistency inconsistency in relationship.Inconsistencies)
637
                    {
638
                        if (inconsistency.get_InconsistencyTypeIndex() == 1)
639
                        {
640
                            LMModelItem modelItem1 = relationship.Item1RepresentationObject == null ? null : relationship.Item1RepresentationObject.ModelItemObject;
641
                            LMModelItem modelItem2 = relationship.Item2RepresentationObject == null ? null : relationship.Item2RepresentationObject.ModelItemObject;
642
                            string[] array = inconsistency.get_Name().ToString().Split(new char[] { '=' });
643
                            if (modelItem1 != null)
644
                            {
645
                                string attrName = array[0];
646
                                if (attrName.Contains("PipingPoint"))
647
                                {
648
                                    string originalAttr = attrName.Split(new char[] { '.' })[1];
649
                                    int index = Convert.ToInt32(relationship.get_Item1Location());
650
                                    LMAAttribute attribute1 = modelItem1.Attributes["PipingPoint" + index + "." + originalAttr];
651
                                    if (attribute1 != null && !DBNull.Value.Equals(attribute1.get_Value()))
652
                                    {
653
                                        loop = true;
654
                                        attribute1.set_Value(DBNull.Value);
655
                                    }
656
                                    attribute1 = null;
657
                                }
658
                                else
659
                                {
660
                                    LMAAttribute attribute1 = modelItem1.Attributes[attrName];
661
                                    if (attribute1 != null && !DBNull.Value.Equals(attribute1.get_Value()))
662
                                    {
663
                                        loop = true;
664
                                        attribute1.set_Value(DBNull.Value);
665
                                    }
666
                                    attribute1 = null;
667
                                }
668
                                modelItem1.Commit();
669
                            }
670
                            if (modelItem2 != null)
671
                            {
672
                                string attrName = array[1];
673
                                if (attrName.Contains("PipingPoint"))
674
                                {
675
                                    string originalAttr = attrName.Split(new char[] { '.' })[1];
676
                                    int index = Convert.ToInt32(relationship.get_Item2Location());
677
                                    LMAAttribute attribute2 = modelItem2.Attributes["PipingPoint" + index + "." + originalAttr];
678
                                    if (attribute2 != null && !DBNull.Value.Equals(attribute2.get_Value()))
679
                                    {
680
                                        attribute2.set_Value(DBNull.Value);
681
                                        loop = true;
682
                                    }
683
                                    attribute2 = null;
684
                                }
685
                                else
686
                                {
687
                                    LMAAttribute attribute2 = modelItem2.Attributes[attrName];
688
                                    if (attribute2 != null && !DBNull.Value.Equals(attribute2.get_Value()))
689
                                    {
690
                                        attribute2.set_Value(DBNull.Value);
691
                                        loop = true;
692
                                    }
693
                                    attribute2 = null;
694
                                }
695
                                modelItem2.Commit();
696
                            }
697
                            ReleaseCOMObjects(modelItem1);
698
                            modelItem1 = null;
699
                            ReleaseCOMObjects(modelItem2);
700
                            modelItem2 = null;
701
                            inconsistency.Commit();
702
                        }
703
                        ReleaseCOMObjects(inconsistency);
704
                    }
705
                    relationship.Commit();
706
                    ReleaseCOMObjects(relationship);
707
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
708
                }
709
                ReleaseCOMObjects(filter);
710
                filter = null;
711
                ReleaseCOMObjects(criterion);
712
                criterion = null;
713
                ReleaseCOMObjects(relationships);
714
                relationships = null;
715
                count++;
716
            }
717
        }
718
        private void RunEndBreakModeling()
719
        {
720
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetAllProgress, document.EndBreaks.Count);
721
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "EndBreaks Modeling");
722
            foreach (var item in document.EndBreaks)
723
                try
724
                {
725
                    EndBreakModeling(item);
726
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
727
                }
728
                catch (Exception ex)
729
                {
730
                    Log.Write("Error in EndBreakModeling");
731
                    Log.Write("UID : " + item.UID);
732
                    Log.Write(ex.Message);
733
                    Log.Write(ex.StackTrace);
734
                }
735
        }
736
        private void RunSpecBreakModeling()
737
        {
738
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetAllProgress, document.SpecBreaks.Count);
739
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "SpecBreaks Modeling");
740
            foreach (var item in document.SpecBreaks)
741
                try
742
                {
743
                    SpecBreakModeling(item);
744
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
745
                }
746
                catch (Exception ex)
747
                {
748
                    Log.Write("Error in SpecBreakModeling");
749
                    Log.Write("UID : " + item.UID);
750
                    Log.Write(ex.Message);
751
                    Log.Write(ex.StackTrace);
752
                }
753
        }
754
        private void RunJoinRunForSameConnector()
755
        {
756
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetAllProgress, document.LINES.Count);
757
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "PipeRun Join - 1");
758
            foreach (var line in document.LINES)
759
            {
760
                Dictionary<LMConnector, List<double[]>> vertices = GetPipeRunVertices(line.SPPID.ModelItemId, false);
761
                List<List<double[]>> result = new List<List<double[]>>();
762
                foreach (var item in vertices)
763
                {
764
                    ReleaseCOMObjects(item.Key);
765
                    result.Add(item.Value);
766
                }
767
                line.SPPID.Vertices = result;
768
                vertices = null;
769
            }
770

    
771
            foreach (var line in document.LINES)
772
            {
773
                foreach (var connector in line.CONNECTORS)
774
                {
775
                    if (connector.ConnectedObject != null &&
776
                        connector.ConnectedObject.GetType() == typeof(Line) &&
777
                        !SPPIDUtil.IsBranchLine(line, connector.ConnectedObject as Line))
778
                    {
779
                        Line connLine = connector.ConnectedObject as Line;
780
                        if (line.SPPID.ModelItemId != connLine.SPPID.ModelItemId &&
781
                            !string.IsNullOrEmpty(line.SPPID.ModelItemId) &&
782
                            !string.IsNullOrEmpty(connLine.SPPID.ModelItemId) &&
783
                            !SPPIDUtil.IsSegment(document, line, connLine))
784
                        {
785
                            string survivorId = string.Empty;
786
                            JoinRun(connLine.SPPID.ModelItemId, line.SPPID.ModelItemId, ref survivorId);
787
                        }
788

    
789
                    }
790
                }
791
                SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
792
            }
793

    
794
            foreach (var line in document.LINES)
795
                line.SPPID.Representations = GetRepresentations(line.SPPID.ModelItemId);
796
        }
797
        private void RunJoinRun()
798
        {
799
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetAllProgress, document.LINES.Count);
800
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "PipeRun Join - 2");
801
            List<string> endModelID = new List<string>();
802
            foreach (var line in document.LINES)
803
            {
804
                if (!endModelID.Contains(line.SPPID.ModelItemId))
805
                {
806
                    while (!endModelID.Contains(line.SPPID.ModelItemId))
807
                    {
808
                        string survivorId = string.Empty;
809
                        JoinRunBySameType(line.SPPID.ModelItemId, ref survivorId);
810
                        if (string.IsNullOrEmpty(survivorId))
811
                        {
812
                            endModelID.Add(line.SPPID.ModelItemId);
813
                        }
814
                    }
815
                }
816
                SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
817
            }
818
        }
819
        private void RunLineNumberModeling()
820
        {
821
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetAllProgress, document.LINENUMBERS.Count);
822
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Line Number Modeling");
823
            foreach (var item in document.LINENUMBERS)
824
            {
825
                LMLabelPersist label = dataSource.GetLabelPersist(item.SPPID.RepresentationId);
826
                if (label == null || (label != null && label.get_ItemStatus() != "Active"))
827
                {
828
                    ReleaseCOMObjects(label);
829
                    item.SPPID.RepresentationId = null;
830
                    LineNumberModeling(item);
831
                }
832
                SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
833
            }
834
        }
835
        private void RunNoteModeling()
836
        {
837
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetAllProgress, document.SYMBOLS.Count);
838
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Notes Modeling");
839
            List<Note> correctList = new List<Note>();
840
            foreach (var item in document.NOTES)
841
                try
842
                {
843
                    NoteModeling(item, correctList);
844
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
845
                }
846
                catch (Exception ex)
847
                {
848
                    Log.Write("Error in NoteModeling");
849
                    Log.Write("UID : " + item.UID);
850
                    Log.Write(ex.Message);
851
                    Log.Write(ex.StackTrace);
852
                }
853

    
854
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetAllProgress, correctList.Count);
855
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Correct Note");
856
            SortNote(correctList);
857
            List<Note> endList = new List<Note>();
858
            if (correctList.Count > 0)
859
                endList.Add(correctList[0]);
860
            foreach (var item in correctList)
861
                try
862
                {
863
                    if (!endList.Contains(item))
864
                        NoteCorrectModeling(item, endList);
865
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
866
                }
867
                catch (Exception ex)
868
                {
869
                    Log.Write("Error in NoteModeling");
870
                    Log.Write("UID : " + item.UID);
871
                    Log.Write(ex.Message);
872
                    Log.Write(ex.StackTrace);
873
                }
874
        }
875
        private void RunTextModeling()
876
        {
877
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetAllProgress, document.TEXTINFOS.Count);
878
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Texts Modeling");
879
            SortText(document.TEXTINFOS);
880
            foreach (var item in document.TEXTINFOS)
881
                try
882
                {
883
                    if (item.ASSOCIATION)
884
                        AssociationTextModeling(item);
885
                    else
886
                        NormalTextModeling(item);
887
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
888
                }
889
                catch (Exception ex)
890
                {
891
                    Log.Write("Error in TextModeling");
892
                    Log.Write("UID : " + item.UID);
893
                    Log.Write(ex.Message);
894
                    Log.Write(ex.StackTrace);
895
                }
896
        }
897
        private void RunInputLineNumberAttribute()
898
        {
899
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetAllProgress, document.LINENUMBERS.Count);
900
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Set LineNumbers Attribute");
901
            List<string> endLine = new List<string>();
902
            foreach (var item in document.LINENUMBERS)
903
                try
904
                {
905
                    InputLineNumberAttribute(item, endLine);
906
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
907
                }
908
                catch (Exception ex)
909
                {
910
                    Log.Write("Error in InputLineNumberAttribute");
911
                    Log.Write("UID : " + item.UID);
912
                    Log.Write(ex.Message);
913
                    Log.Write(ex.StackTrace);
914
                }
915
        }
916
        private void RunInputSymbolAttribute()
917
        {
918
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetAllProgress, document.SYMBOLS.Count + document.Equipments.Count + document.LINES.Count);
919
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Set Symbols Attribute");
920
            foreach (var item in document.SYMBOLS)
921
                try
922
                {
923
                    InputSymbolAttribute(item, item.ATTRIBUTES);
924
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
925
                }
926
                catch (Exception ex)
927
                {
928
                    Log.Write("Error in InputSymbolAttribute");
929
                    Log.Write("UID : " + item.UID);
930
                    Log.Write(ex.Message);
931
                    Log.Write(ex.StackTrace);
932
                }
933

    
934
            foreach (var item in document.Equipments)
935
                try
936
                {
937
                    InputSymbolAttribute(item, item.ATTRIBUTES);
938
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
939
                }
940
                catch (Exception ex)
941
                {
942
                    Log.Write("Error in InputSymbolAttribute");
943
                    Log.Write("UID : " + item.UID);
944
                    Log.Write(ex.Message);
945
                    Log.Write(ex.StackTrace);
946
                }
947
            foreach (var item in document.LINES)
948
                try
949
                {
950
                    InputSymbolAttribute(item, item.ATTRIBUTES);
951
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
952
                }
953
                catch (Exception ex)
954
                {
955
                    Log.Write("Error in InputSymbolAttribute");
956
                    Log.Write("UID : " + item.UID);
957
                    Log.Write(ex.Message);
958
                    Log.Write(ex.StackTrace);
959
                }
960
        }
961
        private void RunInputSpecBreakAttribute()
962
        {
963
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetAllProgress, document.SpecBreaks.Count);
964
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Set SpecBreak Attribute");
965
            foreach (var item in document.SpecBreaks)
966
                try
967
                {
968
                    InputSpecBreakAttribute(item);
969
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
970
                }
971
                catch (Exception ex)
972
                {
973
                    Log.Write("Error in InputSpecBreakAttribute");
974
                    Log.Write("UID : " + item.UID);
975
                    Log.Write(ex.Message);
976
                    Log.Write(ex.StackTrace);
977
                }
978
        }
979
        private void RunInputEndBreakAttribute()
980
        {
981
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetAllProgress, document.EndBreaks.Count);
982
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Set EndBreak Attribute");
983
            foreach (var item in document.EndBreaks)
984
                try
985
                {
986
                    InputEndBreakAttribute(item);
987
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
988
                }
989
                catch (Exception ex)
990
                {
991
                    Log.Write("Error in RunInputEndBreakAttribute");
992
                    Log.Write("UID : " + item.UID);
993
                    Log.Write(ex.Message);
994
                    Log.Write(ex.StackTrace);
995
                }
996
        }
997
        private void RunLabelSymbolModeling()
998
        {
999
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetAllProgress, document.SYMBOLS.Count);
1000
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Labels Modeling");
1001
            foreach (var item in document.SYMBOLS)
1002
                try
1003
                {
1004
                    LabelSymbolModeling(item);
1005
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
1006
                }
1007
                catch (Exception ex)
1008
                {
1009
                    Log.Write("Error in LabelSymbolModeling");
1010
                    Log.Write("UID : " + item.UID);
1011
                    Log.Write(ex.Message);
1012
                    Log.Write(ex.StackTrace);
1013
                }
1014

    
1015
        }
1016
        private void RunCorrectAssociationText()
1017
        {
1018
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetAllProgress, document.TEXTINFOS.Count + document.LINENUMBERS.Count);
1019
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Correct Labels");
1020
            List<Text> endTexts = new List<Text>();
1021
            foreach (var item in document.TEXTINFOS)
1022
            {
1023
                try
1024
                {
1025
                    if (item.ASSOCIATION && !endTexts.Contains(item))
1026
                        AssociationTextCorrectModeling(item, endTexts);
1027
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
1028
                }
1029
                catch (Exception ex)
1030
                {
1031
                    Log.Write("Error in RunCorrectAssociationText");
1032
                    Log.Write("UID : " + item.UID);
1033
                    Log.Write(ex.Message);
1034
                    Log.Write(ex.StackTrace);
1035
                }
1036

    
1037
            }
1038

    
1039
            foreach (var item in document.LINENUMBERS)
1040
            {
1041
                try
1042
                {
1043
                    LineNumberCorrectModeling(item);
1044
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
1045
                }
1046
                catch (Exception ex)
1047
                {
1048
                    Log.Write("Error in RunCorrectAssociationText");
1049
                    Log.Write("UID : " + item.UID);
1050
                    Log.Write(ex.Message);
1051
                    Log.Write(ex.StackTrace);
1052
                }
1053
            }
1054
        }
1055
        private void RunETC()
1056
        {
1057
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetAllProgress, FlowMarkRepIds.Count);
1058
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "ETC");
1059
            foreach (var item in FlowMarkRepIds)
1060
            {
1061
                LMLabelPersist label = dataSource.GetLabelPersist(item);
1062
                if (label != null)
1063
                {
1064
                    label.get_GraphicOID();
1065
                    DependencyObject dependency = radApp.ActiveDocument.ActiveSheet.DrawingObjects[label.get_GraphicOID().ToString()] as DependencyObject;
1066
                    if (dependency != null)
1067
                        dependency.BringToFront();
1068
                }
1069
                ReleaseCOMObjects(label);
1070
                label = null;
1071
                SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
1072
            }
1073
        }
1074
        private void RunBulkAttribute()
1075
        {
1076
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetAllProgress, 2);
1077
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Bulk Attribute");
1078

    
1079
            List<SPPIDModel.BulkAttribute> select = _ETCSetting.BulkAttributes.FindAll(x => x.RuleName.Equals(document.BulkAttributeName));
1080
            if (select.Count > 0)
1081
                SPPIDUtil.BulkAttribute(dataSource, select, BulkAttributeItemType.PipeRun);
1082
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
1083
            if (select.Count > 0)
1084
                SPPIDUtil.BulkAttribute(dataSource, select, BulkAttributeItemType.Symbol);
1085
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
1086
        }
1087
        /// <summary>
1088
        /// 도면 생성 메서드
1089
        /// </summary>
1090
        private bool CreateDocument(ref string drawingNumber, ref string drawingName)
1091
        {
1092
            Log.Write("------------------ Start create document ------------------");
1093
            GetDrawingNameAndNumber(ref drawingName, ref drawingNumber);
1094
            Log.Write("Drawing name : " + drawingName);
1095
            Log.Write("Drawing number : " + drawingNumber);
1096
            Thread.Sleep(1000);
1097
            newDrawing = application.Drawings.Add(document.Unit, document.Template, drawingNumber, drawingName);
1098
            if (newDrawing != null)
1099
            {
1100
                document.SPPID_DrawingNumber = drawingNumber;
1101
                document.SPPID_DrawingName = drawingName;
1102
                Thread.Sleep(1000);
1103
                radApp.ActiveWindow.Fit();
1104
                Thread.Sleep(1000);
1105
                radApp.ActiveWindow.Zoom = 2000;
1106
                Thread.Sleep(2000);
1107

    
1108
                //current LMDrawing 가져오기
1109
                LMAFilter filter = new LMAFilter();
1110
                LMACriterion criterion = new LMACriterion();
1111
                filter.ItemType = "Drawing";
1112
                criterion.SourceAttributeName = "Name";
1113
                criterion.Operator = "=";
1114
                criterion.set_ValueAttribute(drawingName);
1115
                filter.get_Criteria().Add(criterion);
1116

    
1117
                LMDrawings drawings = new LMDrawings();
1118
                drawings.Collect(dataSource, Filter: filter);
1119

    
1120
                // Input Drawing Attribute
1121
                LMDrawing drawing = ((dynamic)drawings).Nth(1);
1122
                if (drawing != null)
1123
                {
1124
                    using (DataTable drawingAttributeDT = Project_DB.SelectDrawingProjectAttribute())
1125
                    {
1126
                        foreach (DataRow row in drawingAttributeDT.Rows)
1127
                        {
1128
                            string mappingName = DBNull.Value.Equals(row["SPPID_ATTRIBUTE"]) ? string.Empty : row["SPPID_ATTRIBUTE"].ToString();
1129
                            if (!string.IsNullOrEmpty(mappingName))
1130
                            {
1131
                                string uid = row["UID"].ToString();
1132
                                string name = row["NAME"].ToString();
1133
                                Text text = document.TEXTINFOS.Find(x => x.AREA == uid);
1134
                                if (text != null)
1135
                                {
1136
                                    string value = text.VALUE;
1137
                                    LMAAttribute attribute = drawing.Attributes[mappingName];
1138
                                    if (attribute != null)
1139
                                        attribute.set_Value(value);
1140
                                    ReleaseCOMObjects(attribute);
1141
                                    document.TEXTINFOS.Remove(text);
1142
                                }
1143
                            }
1144
                        }
1145

    
1146
                        drawingAttributeDT.Dispose();
1147
                    }
1148

    
1149
                    ReleaseCOMObjects(drawing);
1150
                }
1151

    
1152
                drawingID = ((dynamic)drawings).Nth(1).Id;
1153
                ReleaseCOMObjects(filter);
1154
                ReleaseCOMObjects(criterion);
1155
                ReleaseCOMObjects(drawings);
1156
                filter = null;
1157
                criterion = null;
1158
                drawings = null;
1159
            }
1160
            else
1161
                Log.Write("Fail Create Drawing");
1162

    
1163
            if (newDrawing != null)
1164
            {
1165
                SetBorderFile();
1166
                return true;
1167
            }
1168
            else
1169
                return false;
1170
        }
1171

    
1172
        private void SetBorderFile()
1173
        {
1174
            ETCSetting setting = ETCSetting.GetInstance();
1175

    
1176
            if (!string.IsNullOrEmpty(setting.BorderFilePath) && System.IO.File.Exists(setting.BorderFilePath))
1177
            {
1178
                foreach (Ingr.RAD2D.SmartFrame2d smartFrame in radApp.ActiveDocument.ActiveSheet.SmartFrames2d)
1179
                {
1180
                    if (!string.IsNullOrEmpty(smartFrame.LinkMoniker) && smartFrame.LinkMoniker != setting.BorderFilePath)
1181
                    {
1182
                        smartFrame.ChangeSource(Ingr.RAD2D.OLEInsertionTypeConstant.igOLELinked, setting.BorderFilePath, true);
1183
                        smartFrame.Update();
1184
                    }
1185

    
1186
                }
1187
            }
1188
        }
1189

    
1190
        /// <summary>
1191
        /// DrawingName, DrawingNumber를 확인하여 중복이 있으면 _1을 붙이고 +1씩 한다.
1192
        /// </summary>
1193
        /// <param name="drawingName"></param>
1194
        /// <param name="drawingNumber"></param>
1195
        private void GetDrawingNameAndNumber(ref string drawingName, ref string drawingNumber)
1196
        {
1197
            LMDrawings drawings = new LMDrawings();
1198
            drawings.Collect(dataSource);
1199

    
1200
            List<string> drawingNameList = new List<string>();
1201
            List<string> drawingNumberList = new List<string>();
1202

    
1203
            foreach (LMDrawing item in drawings)
1204
            {
1205
                drawingNameList.Add(item.Attributes["Name"].get_Value().ToString());
1206
                drawingNumberList.Add(item.Attributes["DrawingNumber"].get_Value().ToString());
1207
            }
1208

    
1209
            int nameLength = drawingName.Length;
1210
            while (drawingNameList.Contains(drawingName))
1211
            {
1212
                if (nameLength == drawingName.Length)
1213
                    drawingName += "-1";
1214
                else
1215
                {
1216
                    int index = Convert.ToInt32(drawingName.Remove(0, nameLength + 1));
1217
                    drawingName = drawingName.Substring(0, nameLength + 1);
1218
                    drawingName += ++index;
1219
                }
1220
            }
1221

    
1222
            int numberLength = drawingNumber.Length;
1223
            while (drawingNameList.Contains(drawingNumber))
1224
            {
1225
                if (numberLength == drawingNumber.Length)
1226
                    drawingNumber += "-1";
1227
                else
1228
                {
1229
                    int index = Convert.ToInt32(drawingNumber.Remove(0, numberLength + 1));
1230
                    drawingNumber = drawingNumber.Substring(0, numberLength + 1);
1231
                    drawingNumber += ++index;
1232
                }
1233
            }
1234
            ReleaseCOMObjects(drawings);
1235
            drawings = null;
1236
        }
1237

    
1238
        /// <summary>
1239
        /// 도면 크기 구하는 메서드
1240
        /// </summary>
1241
        /// <returns></returns>
1242
        private bool DocumentCoordinateCorrection()
1243
        {
1244
            //if (radApp.ActiveDocument.ActiveSheet.SmartFrames2d.Count > 0)
1245
            //{
1246
            //    double x = 0;
1247
            //    double y = 0;
1248
            //    foreach (Ingr.RAD2D.SmartFrame2d smartFrame in radApp.ActiveDocument.ActiveSheet.SmartFrames2d)
1249
            //    {
1250
            //        x = Math.Max(smartFrame.CropRight, x);
1251
            //        y = Math.Max(smartFrame.CropTop, y);
1252
            //    }
1253
            //    document.SetSPPIDLocation(x, y);
1254
            //    document.CoordinateCorrection();
1255
            //    return true;
1256
            //}
1257
            //else
1258
            //{
1259
            //    Log.Write("Need Border!");
1260
            //    return false;
1261
            //}
1262

    
1263
            if (Settings.Default.DrawingX != 0 && Settings.Default.DrawingY != 0)
1264
            {
1265
                Log.Write("Setting Drawing X, Drawing Y");
1266
                document.SetSPPIDLocation(Settings.Default.DrawingX, Settings.Default.DrawingY);
1267
                Log.Write("Start coordinate correction");
1268
                document.CoordinateCorrection();
1269
                return true;
1270
            }
1271
            else
1272
            {
1273
                Log.Write("Need Drawing X, Y");
1274
                return false;
1275
            }
1276
        }
1277

    
1278
        /// <summary>
1279
        /// 심볼을 실제로 Modeling 메서드
1280
        /// </summary>
1281
        /// <param name="symbol">생성할 심볼</param>
1282
        /// <param name="targetSymbol">연결되어 있는 심볼</param>
1283
        private void SymbolModeling(Symbol symbol, Symbol targetSymbol)
1284
        {
1285
            // OWNERSYMBOL Attribute, 값을 가지고 있을 경우
1286
            BaseModel.Attribute itemAttribute = symbol.ATTRIBUTES.Find(attr => attr.ATTRIBUTE == "OWNERSYMBOL");
1287
            if (itemAttribute != null && (string.IsNullOrEmpty(itemAttribute.VALUE) || itemAttribute.VALUE != "None"))
1288
                return;
1289
            // 이미 모델링 됐을 경우
1290
            else if (!string.IsNullOrEmpty(symbol.SPPID.RepresentationId))
1291
                return;
1292

    
1293
            LMSymbol _LMSymbol = null;
1294

    
1295
            string mappingPath = symbol.SPPID.MAPPINGNAME;
1296
            double x = symbol.SPPID.ORIGINAL_X;
1297
            double y = symbol.SPPID.ORIGINAL_Y;
1298
            int mirror = 0;
1299
            double angle = symbol.ANGLE;
1300

    
1301
            // OPC 일경우 180도 일때 Mirror
1302
            if (mappingPath.Contains("Piping OPC's") && angle == Math.PI)
1303
                mirror = 1;
1304

    
1305
            // Mirror 계산
1306
            if (symbol.FLIP == 1)
1307
            {
1308
                mirror = 1;
1309
                if (angle == Math.PI || angle == 0)
1310
                    angle += Math.PI;
1311
            }
1312

    
1313
            if (targetSymbol != null && !string.IsNullOrEmpty(targetSymbol.SPPID.RepresentationId))
1314
            {
1315
                LMSymbol _TargetItem = dataSource.GetSymbol(targetSymbol.SPPID.RepresentationId);   /// RepresentationId로 SPPID 심볼을 찾음
1316
                Connector connector = SPPIDUtil.FindSymbolConnectorByUID(document, symbol.UID, targetSymbol);
1317
                if (connector != null)
1318
                    GetTargetSymbolConnectorPoint(connector, targetSymbol, ref x, ref y);
1319

    
1320
                LMConnector temp = LineModelingForSymbolZeroLength(symbol, _TargetItem, x, y);
1321
                _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle, TargetItem: _TargetItem);
1322
                if (temp != null)
1323
                    _placement.PIDRemovePlacement(temp.AsLMRepresentation());
1324
                ReleaseCOMObjects(temp);
1325
                temp = null;
1326

    
1327
                if (_LMSymbol != null && _TargetItem != null)
1328
                    symbol.SPPID.RepresentationId = _LMSymbol.AsLMRepresentation().Id;
1329

    
1330
                ReleaseCOMObjects(_TargetItem);
1331
            }
1332
            else
1333
                _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle);
1334

    
1335
            if (_LMSymbol != null)
1336
            {
1337
                _LMSymbol.Commit();
1338

    
1339
                // ConnCheck
1340
                List<string> ids = new List<string>();
1341
                foreach (LMConnector item in _LMSymbol.Connect1Connectors)
1342
                {
1343
                    if (item.get_ItemStatus() == "Active" && !ids.Contains(item.Id))
1344
                        ids.Add(item.Id);
1345
                    ReleaseCOMObjects(item);
1346
                }
1347
                foreach (LMConnector item in _LMSymbol.Connect2Connectors)
1348
                {
1349
                    if (item.get_ItemStatus() == "Active" && !ids.Contains(item.Id))
1350
                        ids.Add(item.Id);
1351
                    ReleaseCOMObjects(item);
1352
                }
1353

    
1354
                int createdSymbolCount = document.SYMBOLS.FindAll(i => i.CONNECTORS.Find(j => j.CONNECTEDITEM == symbol.UID) != null && !string.IsNullOrEmpty(i.SPPID.RepresentationId)).Count;
1355
                if (targetSymbol == null && ids.Count != createdSymbolCount)
1356
                {
1357
                    double currentX = _LMSymbol.get_XCoordinate();
1358
                    double currentY = _LMSymbol.get_YCoordinate();
1359

    
1360

    
1361
                }
1362

    
1363
                symbol.SPPID.RepresentationId = _LMSymbol.AsLMRepresentation().Id;
1364
                symbol.SPPID.ModelItemID = _LMSymbol.ModelItemID;
1365
                symbol.SPPID.GraphicOID = _LMSymbol.get_GraphicOID().ToString();
1366

    
1367
                foreach (var item in symbol.ChildSymbols)
1368
                    CreateChildSymbol(item, _LMSymbol, symbol);
1369

    
1370
                symbol.SPPID.SPPID_X = _LMSymbol.get_XCoordinate();
1371
                symbol.SPPID.SPPID_Y = _LMSymbol.get_YCoordinate();
1372

    
1373
                double[] range = null;
1374
                GetSPPIDSymbolRange(symbol, ref range);
1375
                symbol.SPPID.SPPID_Min_X = range[0];
1376
                symbol.SPPID.SPPID_Min_Y = range[1];
1377
                symbol.SPPID.SPPID_Max_X = range[2];
1378
                symbol.SPPID.SPPID_Max_Y = range[3];
1379

    
1380
                foreach (var item in symbol.SPPID.CorrectionX_GroupSymbols)
1381
                    item.SPPID.ORIGINAL_X = symbol.SPPID.SPPID_X;
1382
                foreach (var item in symbol.SPPID.CorrectionY_GroupSymbols)
1383
                    item.SPPID.ORIGINAL_Y = symbol.SPPID.SPPID_Y;
1384

    
1385
                ReleaseCOMObjects(_LMSymbol);
1386
            }
1387
        }
1388
        /// <summary>
1389
        /// targetX와 targetY 기준 제일 먼 PipingPoint에 TempLine Modeling
1390
        /// Signal Point는 고려하지 않음
1391
        /// </summary>
1392
        /// <param name="symbol"></param>
1393
        /// <param name="_TargetItem"></param>
1394
        /// <param name="targetX"></param>
1395
        /// <param name="targetY"></param>
1396
        /// <returns></returns>
1397
        private LMConnector LineModelingForSymbolZeroLength(Symbol symbol, LMSymbol _TargetItem, double targetX, double targetY)
1398
        {
1399
            LMConnector tempConnector = null;
1400

    
1401
            List<Symbol> group = new List<Symbol>();
1402
            SPPIDUtil.FindConnectedSymbolGroup(document, symbol, group);
1403
            if (group.FindAll(loopX => !string.IsNullOrEmpty(loopX.SPPID.RepresentationId)).Count == 1)
1404
            {
1405
                List<Connector> connectors = new List<Connector>();
1406
                foreach (var item in group)
1407
                    connectors.AddRange(item.CONNECTORS.FindAll(loopX => loopX.ConnectedObject != null && loopX.ConnectedObject.GetType() == typeof(Line)));
1408
                /// Primary or Secondary Type Line만 고려
1409
                Connector _connector = connectors.Find(loopX => loopX.ConnectedObject != null && loopX.ConnectedObject.GetType() == typeof(Line) &&
1410
                (((Line)loopX.ConnectedObject).TYPE == "Primary" || ((Line)loopX.ConnectedObject).TYPE == "Secondary"));
1411
                if (_connector != null)
1412
                {
1413
                    string sppidLine = ((Line)_connector.ConnectedObject).SPPID.MAPPINGNAME;
1414
                    List<double[]> pointInfos = getPipingPoints(_TargetItem);
1415
                    /// PipingPoint가 2개 이상만
1416
                    if (pointInfos.Count >= 2)
1417
                    {
1418
                        double lineX = 0;
1419
                        double lineY = 0;
1420
                        double length = 0;
1421
                        foreach (var item in pointInfos)
1422
                        {
1423
                            double tempX = item[1];
1424
                            double tempY = item[2];
1425

    
1426
                            double calcDistance = SPPIDUtil.CalcPointToPointdDistance(targetX, targetY, tempX, tempY);
1427
                            if (calcDistance > length)
1428
                            {
1429
                                lineX = tempX;
1430
                                lineY = tempY;
1431
                            }
1432
                        }
1433

    
1434
                        _LMAItem _LMAItem = _placement.PIDCreateItem(sppidLine);
1435
                        PlaceRunInputs placeRunInputs = new PlaceRunInputs();
1436
                        placeRunInputs.AddPoint(-1, -1);
1437
                        placeRunInputs.AddSymbolTarget(_TargetItem, lineX, lineY);
1438
                        tempConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
1439
                        if (tempConnector != null)
1440
                            tempConnector.Commit();
1441
                        ReleaseCOMObjects(_LMAItem);
1442
                        _LMAItem = null;
1443
                        ReleaseCOMObjects(placeRunInputs);
1444
                        placeRunInputs = null;
1445
                    }
1446
                }
1447
            }
1448

    
1449
            return tempConnector;
1450
        }
1451
        /// <summary>
1452
        /// Symbol의 PipingPoints를 구함
1453
        /// SignalPoint는 고려하지 않음
1454
        /// </summary>
1455
        /// <param name="symbol"></param>
1456
        /// <returns></returns>
1457
        private List<double[]> getPipingPoints(LMSymbol symbol)
1458
        {
1459
            LMModelItem modelItem = symbol.ModelItemObject;
1460
            LMPipingPoints pipingPoints = null;
1461
            if (modelItem.get_ItemTypeName() == "PipingComp")
1462
            {
1463
                LMPipingComp pipingComp = dataSource.GetPipingComp(modelItem.Id);
1464
                pipingPoints = pipingComp.PipingPoints;
1465
                ReleaseCOMObjects(pipingComp);
1466
                pipingComp = null;
1467
            }
1468
            else if (modelItem.get_ItemTypeName() == "Instrument")
1469
            {
1470
                LMInstrument instrument = dataSource.GetInstrument(modelItem.Id);
1471
                pipingPoints = instrument.PipingPoints;
1472
                ReleaseCOMObjects(instrument);
1473
                instrument = null;
1474
            }
1475
            else
1476
                Log.Write("다른 Type");
1477

    
1478
            List<double[]> info = new List<double[]>();
1479
            if (pipingPoints != null)
1480
            {
1481
                foreach (LMPipingPoint pipingPoint in pipingPoints)
1482
                {
1483
                    foreach (LMAAttribute attribute in pipingPoint.Attributes)
1484
                    {
1485
                        if (attribute.Name == "PipingPointNumber")
1486
                        {
1487
                            int index = Convert.ToInt32(attribute.get_Value());
1488
                            if (info.Find(loopX => loopX[0] == index) == null)
1489
                            {
1490
                                double x = 0;
1491
                                double y = 0;
1492
                                if (_placement.PIDConnectPointLocation(symbol, index, ref x, ref y))
1493
                                    info.Add(new double[] { index, x, y });
1494
                            }
1495
                        }
1496
                    }
1497
                }
1498
            }
1499
            ReleaseCOMObjects(modelItem);
1500
            modelItem = null;
1501
            ReleaseCOMObjects(pipingPoints);
1502
            pipingPoints = null;
1503

    
1504
            return info;
1505
        }
1506

    
1507
        private void RemoveSymbol(Symbol symbol)
1508
        {
1509
            if (!string.IsNullOrEmpty(symbol.SPPID.RepresentationId))
1510
            {
1511
                LMSymbol _LMSymbol = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
1512
                if (_LMSymbol != null)
1513
                {
1514
                    _placement.PIDRemovePlacement(_LMSymbol.AsLMRepresentation());
1515
                    ReleaseCOMObjects(_LMSymbol);
1516
                }
1517
            }
1518

    
1519
            symbol.SPPID.RepresentationId = string.Empty;
1520
            symbol.SPPID.ModelItemID = string.Empty;
1521
            symbol.SPPID.SPPID_X = double.NaN;
1522
            symbol.SPPID.SPPID_Y = double.NaN;
1523
            symbol.SPPID.SPPID_Min_X = double.NaN;
1524
            symbol.SPPID.SPPID_Min_Y = double.NaN;
1525
            symbol.SPPID.SPPID_Max_X = double.NaN;
1526
            symbol.SPPID.SPPID_Max_Y = double.NaN;
1527
        }
1528

    
1529
        private void RemoveSymbol(List<Symbol> symbols)
1530
        {
1531
            foreach (var symbol in symbols)
1532
            {
1533
                if (!string.IsNullOrEmpty(symbol.SPPID.RepresentationId))
1534
                {
1535
                    LMSymbol _LMSymbol = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
1536
                    if (_LMSymbol != null)
1537
                    {
1538
                        _placement.PIDRemovePlacement(_LMSymbol.AsLMRepresentation());
1539
                        ReleaseCOMObjects(_LMSymbol);
1540
                    }
1541
                }
1542

    
1543
                symbol.SPPID.RepresentationId = string.Empty;
1544
                symbol.SPPID.ModelItemID = string.Empty;
1545
                symbol.SPPID.SPPID_X = double.NaN;
1546
                symbol.SPPID.SPPID_Y = double.NaN;
1547
                symbol.SPPID.SPPID_Min_X = double.NaN;
1548
                symbol.SPPID.SPPID_Min_Y = double.NaN;
1549
                symbol.SPPID.SPPID_Max_X = double.NaN;
1550
                symbol.SPPID.SPPID_Max_Y = double.NaN;
1551
            }
1552
        }
1553

    
1554
        /// <summary>
1555
        /// ID2의 Symbol Width와 Height를 비교해서 상대적인 SPPID Connector좌표를 가져온다.
1556
        /// </summary>
1557
        /// <param name="targetConnector"></param>
1558
        /// <param name="targetSymbol"></param>
1559
        /// <param name="x"></param>
1560
        /// <param name="y"></param>
1561
        private void GetTargetSymbolConnectorPoint(Connector targetConnector, Symbol targetSymbol, ref double x, ref double y)
1562
        {
1563
            LMSymbol _TargetItem = dataSource.GetSymbol(targetSymbol.SPPID.RepresentationId);
1564

    
1565
            double[] range = null;
1566
            List<double[]> points = new List<double[]>();
1567
            GetSPPIDSymbolRangeAndConnectionPoints(targetSymbol, ref range, points);
1568
            double x1 = range[0];
1569
            double y1 = range[1];
1570
            double x2 = range[2];
1571
            double y2 = range[3];
1572

    
1573
            // Origin 기준 Connector의 위치차이
1574
            double sceneX = 0;
1575
            double sceneY = 0;
1576
            SPPIDUtil.ConvertPointBystring(targetConnector.SCENECONNECTPOINT, ref sceneX, ref sceneY);
1577
            double originX = 0;
1578
            double originY = 0;
1579
            SPPIDUtil.ConvertPointBystring(targetSymbol.ORIGINALPOINT, ref originX, ref originY);
1580
            double gapX = originX - sceneX;
1581
            double gapY = originY - sceneY;
1582

    
1583
            // SPPID Symbol과 ID2 심볼의 크기 차이
1584
            double sizeWidth = 0;
1585
            double sizeHeight = 0;
1586
            SPPIDUtil.ConvertPointBystring(targetSymbol.SIZE, ref sizeWidth, ref sizeHeight);
1587
            if (sizeWidth == 0 || sizeHeight == 0)
1588
                throw new Exception("Check symbol size! \r\nUID : " + targetSymbol.UID);
1589

    
1590
            double percentX = (x2 - x1) / sizeWidth;
1591
            double percentY = (y2 - y1) / sizeHeight;
1592

    
1593
            double SPPIDgapX = gapX * percentX;
1594
            double SPPIDgapY = gapY * percentY;
1595

    
1596
            double[] SPPIDOriginPoint = new double[] { _TargetItem.get_XCoordinate() - SPPIDgapX, _TargetItem.get_YCoordinate() + SPPIDgapY };
1597
            double distance = double.MaxValue;
1598
            double[] resultPoint;
1599
            foreach (var point in points)
1600
            {
1601
                double result = SPPIDUtil.CalcPointToPointdDistance(point[0], point[1], SPPIDOriginPoint[0], SPPIDOriginPoint[1]);
1602
                if (distance > result)
1603
                {
1604
                    distance = result;
1605
                    resultPoint = point;
1606
                    x = point[0];
1607
                    y = point[1];
1608
                }
1609
            }
1610

    
1611
            ReleaseCOMObjects(_TargetItem);
1612
        }
1613

    
1614
        private void GetTargetLineConnectorPoint(Connector targetConnector, Line targetLine, ref double x, ref double y)
1615
        {
1616
            int index = targetLine.CONNECTORS.IndexOf(targetConnector);
1617
            if (index == 0)
1618
            {
1619
                x = targetLine.SPPID.START_X;
1620
                y = targetLine.SPPID.START_Y;
1621
            }
1622
            else
1623
            {
1624
                x = targetLine.SPPID.END_X;
1625
                y = targetLine.SPPID.END_Y;
1626
            }
1627
        }
1628

    
1629
        /// <summary>
1630
        /// SPPID Symbol의 Range를 구한다.
1631
        /// </summary>
1632
        /// <param name="symbol"></param>
1633
        /// <param name="range"></param>
1634
        private void GetSPPIDSymbolRangeAndConnectionPoints(Symbol symbol, ref double[] range, List<double[]> points)
1635
        {
1636
            LMSymbol _TargetItem = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
1637
            Ingr.RAD2D.Symbol2d symbol2d = radApp.ActiveDocument.ActiveSheet.DrawingObjects[_TargetItem.get_GraphicOID().ToString()];
1638
            double x1 = 0;
1639
            double y1 = 0;
1640
            double x2 = 0;
1641
            double y2 = 0;
1642
            symbol2d.Range(out x1, out y1, out x2, out y2);
1643
            range = new double[] { x1, y1, x2, y2 };
1644

    
1645
            for (int i = 1; i < 30; i++)
1646
            {
1647
                double connX = 0;
1648
                double connY = 0;
1649
                if (_placement.PIDConnectPointLocation(_TargetItem, i, ref connX, ref connY))
1650
                    points.Add(new double[] { connX, connY });
1651
            }
1652

    
1653
            foreach (var childSymbol in symbol.ChildSymbols)
1654
                GetSPPIDChildSymbolRange(childSymbol, ref range, points);
1655

    
1656
            ReleaseCOMObjects(_TargetItem);
1657
        }
1658

    
1659
        private void GetSPPIDSymbolRange(Symbol symbol, ref double[] range, bool bOnlySymbol = false, bool bForGraphic = false)
1660
        {
1661
            LMSymbol _TargetItem = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
1662
            if (_TargetItem != null)
1663
            {
1664
                Ingr.RAD2D.Symbol2d symbol2d = radApp.ActiveDocument.ActiveSheet.DrawingObjects[_TargetItem.get_GraphicOID().ToString()];
1665
                double x1 = 0;
1666
                double y1 = 0;
1667
                double x2 = 0;
1668
                double y2 = 0;
1669
                if (!bForGraphic)
1670
                {
1671
                    symbol2d.Range(out x1, out y1, out x2, out y2);
1672
                    range = new double[] { x1, y1, x2, y2 };
1673
                }
1674
                else
1675
                {
1676
                    x1 = double.MaxValue;
1677
                    y1 = double.MaxValue;
1678
                    x2 = double.MinValue;
1679
                    y2 = double.MinValue;
1680
                    range = new double[] { x1, y1, x2, y2 };
1681

    
1682
                    foreach (var item in symbol2d.DrawingObjects)
1683
                    {
1684
                        if (item.GetType() == typeof(Ingr.RAD2D.Line2d))
1685
                        {
1686
                            Ingr.RAD2D.Line2d rangeObject = item as Ingr.RAD2D.Line2d;
1687
                            if (rangeObject.Layer == "Default")
1688
                            {
1689
                                rangeObject.Range(out x1, out y1, out x2, out y2);
1690
                                range = new double[] {
1691
                                Math.Min(x1, range[0]),
1692
                                Math.Min(y1, range[1]),
1693
                                Math.Max(x2, range[2]),
1694
                                Math.Max(y2, range[3])
1695
                            };
1696
                            }
1697
                        }
1698
                        else if (item.GetType() == typeof(Ingr.RAD2D.Circle2d))
1699
                        {
1700
                            Ingr.RAD2D.Circle2d rangeObject = item as Ingr.RAD2D.Circle2d;
1701
                            if (rangeObject.Layer == "Default")
1702
                            {
1703
                                rangeObject.Range(out x1, out y1, out x2, out y2);
1704
                                range = new double[] {
1705
                                Math.Min(x1, range[0]),
1706
                                Math.Min(y1, range[1]),
1707
                                Math.Max(x2, range[2]),
1708
                                Math.Max(y2, range[3])
1709
                            };
1710
                            }
1711
                        }
1712
                        else if (item.GetType() == typeof(Ingr.RAD2D.Rectangle2d))
1713
                        {
1714
                            Ingr.RAD2D.Rectangle2d rangeObject = item as Ingr.RAD2D.Rectangle2d;
1715
                            if (rangeObject.Layer == "Default")
1716
                            {
1717
                                rangeObject.Range(out x1, out y1, out x2, out y2);
1718
                                range = new double[] {
1719
                                Math.Min(x1, range[0]),
1720
                                Math.Min(y1, range[1]),
1721
                                Math.Max(x2, range[2]),
1722
                                Math.Max(y2, range[3])
1723
                            };
1724
                            }
1725
                        }
1726
                        else if (item.GetType() == typeof(Ingr.RAD2D.Arc2d))
1727
                        {
1728
                            Ingr.RAD2D.Arc2d rangeObject = item as Ingr.RAD2D.Arc2d;
1729
                            if (rangeObject.Layer == "Default")
1730
                            {
1731
                                rangeObject.Range(out x1, out y1, out x2, out y2);
1732
                                range = new double[] {
1733
                                Math.Min(x1, range[0]),
1734
                                Math.Min(y1, range[1]),
1735
                                Math.Max(x2, range[2]),
1736
                                Math.Max(y2, range[3])
1737
                            };
1738
                            }
1739
                        }
1740
                    }
1741
                }
1742

    
1743
                if (!bOnlySymbol)
1744
                {
1745
                    foreach (var childSymbol in symbol.ChildSymbols)
1746
                        GetSPPIDChildSymbolRange(childSymbol, ref range);
1747
                }
1748
                ReleaseCOMObjects(_TargetItem);
1749
            }
1750
        }
1751

    
1752
        private void GetSPPIDSymbolRange(LMLabelPersist labelPersist, ref double[] range)
1753
        {
1754
            if (labelPersist != null)
1755
            {
1756
                double x1 = double.MaxValue;
1757
                double y1 = double.MaxValue;
1758
                double x2 = double.MinValue;
1759
                double y2 = double.MinValue;
1760
                range = new double[] { x1, y1, x2, y2 };
1761

    
1762
                Ingr.RAD2D.DependencyObject dependency = radApp.ActiveDocument.ActiveSheet.DrawingObjects[labelPersist.get_GraphicOID().ToString()] as DependencyObject;
1763
                foreach (var item in dependency.DrawingObjects)
1764
                {
1765
                    Ingr.RAD2D.TextBox textBox = item as Ingr.RAD2D.TextBox;
1766
                    if (textBox != null)
1767
                    {
1768
                        if (dependency != null)
1769
                        {
1770
                            double tempX1;
1771
                            double tempY1;
1772
                            double tempX2;
1773
                            double tempY2;
1774
                            textBox.Range(out tempX1, out tempY1, out tempX2, out tempY2);
1775
                            x1 = Math.Min(x1, tempX1);
1776
                            y1 = Math.Min(y1, tempY1);
1777
                            x2 = Math.Max(x2, tempX2);
1778
                            y2 = Math.Max(y2, tempY2);
1779

    
1780
                            range = new double[] { x1, y1, x2, y2 };
1781
                        }
1782
                    }
1783
                }
1784

    
1785
            }
1786
        }
1787

    
1788
        private void GetSPPIDSymbolRange(List<Symbol> symbols, ref double[] range, bool bOnlySymbol = false, bool bForGraphic = false)
1789
        {
1790
            double[] tempRange = new double[] { double.MaxValue, double.MaxValue, double.MinValue, double.MinValue };
1791
            foreach (var symbol in symbols)
1792
            {
1793
                double[] symbolRange = null;
1794
                GetSPPIDSymbolRange(symbol, ref symbolRange, bOnlySymbol, bForGraphic);
1795

    
1796
                tempRange[0] = Math.Min(tempRange[0], symbolRange[0]);
1797
                tempRange[1] = Math.Min(tempRange[1], symbolRange[1]);
1798
                tempRange[2] = Math.Max(tempRange[2], symbolRange[2]);
1799
                tempRange[3] = Math.Max(tempRange[3], symbolRange[3]);
1800

    
1801
                foreach (var childSymbol in symbol.ChildSymbols)
1802
                    GetSPPIDChildSymbolRange(childSymbol, ref tempRange);
1803
            }
1804

    
1805
            range = tempRange;
1806
        }
1807

    
1808
        /// <summary>
1809
        /// Child Modeling 된 Symbol의 Range를 구한다.
1810
        /// </summary>
1811
        /// <param name="childSymbol"></param>
1812
        /// <param name="range"></param>
1813
        private void GetSPPIDChildSymbolRange(ChildSymbol childSymbol, ref double[] range, List<double[]> points)
1814
        {
1815
            LMSymbol _ChildSymbol = dataSource.GetSymbol(childSymbol.SPPID.RepresentationId);
1816
            if (_ChildSymbol != null)
1817
            {
1818
                Ingr.RAD2D.Symbol2d symbol2d = radApp.ActiveDocument.ActiveSheet.DrawingObjects[_ChildSymbol.get_GraphicOID().ToString()];
1819
                double x1 = 0;
1820
                double y1 = 0;
1821
                double x2 = 0;
1822
                double y2 = 0;
1823
                symbol2d.Range(out x1, out y1, out x2, out y2);
1824
                range[0] = Math.Min(range[0], x1);
1825
                range[1] = Math.Min(range[1], y1);
1826
                range[2] = Math.Max(range[2], x2);
1827
                range[3] = Math.Max(range[3], y2);
1828

    
1829
                for (int i = 1; i < int.MaxValue; i++)
1830
                {
1831
                    double connX = 0;
1832
                    double connY = 0;
1833
                    if (_placement.PIDConnectPointLocation(_ChildSymbol, i, ref connX, ref connY))
1834
                        points.Add(new double[] { connX, connY });
1835
                    else
1836
                        break;
1837
                }
1838

    
1839
                foreach (var loopChildSymbol in childSymbol.ChildSymbols)
1840
                    GetSPPIDChildSymbolRange(loopChildSymbol, ref range, points);
1841

    
1842
                ReleaseCOMObjects(_ChildSymbol);
1843
            }
1844
        }
1845

    
1846
        private void GetSPPIDChildSymbolRange(ChildSymbol childSymbol, ref double[] range)
1847
        {
1848
            LMSymbol _ChildSymbol = dataSource.GetSymbol(childSymbol.SPPID.RepresentationId);
1849
            if (_ChildSymbol != null)
1850
            {
1851
                Ingr.RAD2D.Symbol2d symbol2d = radApp.ActiveDocument.ActiveSheet.DrawingObjects[_ChildSymbol.get_GraphicOID().ToString()];
1852
                double x1 = 0;
1853
                double y1 = 0;
1854
                double x2 = 0;
1855
                double y2 = 0;
1856
                symbol2d.Range(out x1, out y1, out x2, out y2);
1857
                range[0] = Math.Min(range[0], x1);
1858
                range[1] = Math.Min(range[1], y1);
1859
                range[2] = Math.Max(range[2], x2);
1860
                range[3] = Math.Max(range[3], y2);
1861

    
1862
                foreach (var loopChildSymbol in childSymbol.ChildSymbols)
1863
                    GetSPPIDChildSymbolRange(loopChildSymbol, ref range);
1864
                ReleaseCOMObjects(_ChildSymbol);
1865
            }
1866
        }
1867

    
1868
        /// <summary>
1869
        /// Label Symbol Modeling
1870
        /// </summary>
1871
        /// <param name="symbol"></param>
1872
        private void LabelSymbolModeling(Symbol symbol)
1873
        {
1874
            if (string.IsNullOrEmpty(symbol.SPPID.RepresentationId))
1875
            {
1876
                BaseModel.Attribute itemAttribute = symbol.ATTRIBUTES.Find(x => x.ATTRIBUTE == "OWNERSYMBOL");
1877
                if (itemAttribute == null || string.IsNullOrEmpty(itemAttribute.VALUE) || itemAttribute.VALUE == "None")
1878
                    return;
1879
                Array points = new double[] { 0, symbol.SPPID.ORIGINAL_X, symbol.SPPID.ORIGINAL_Y };
1880

    
1881
                string symbolUID = itemAttribute.VALUE;
1882
                object targetItem = SPPIDUtil.FindObjectByUID(document, symbolUID);
1883
                if (targetItem != null &&
1884
                    (targetItem.GetType() == typeof(Symbol) ||
1885
                    targetItem.GetType() == typeof(Equipment)))
1886
                {
1887
                    // Object 아이템이 Symbol일 경우 Equipment일 경우 
1888
                    string sRep = null;
1889
                    if (targetItem.GetType() == typeof(Symbol))
1890
                        sRep = ((Symbol)targetItem).SPPID.RepresentationId;
1891
                    else if (targetItem.GetType() == typeof(Equipment))
1892
                        sRep = ((Equipment)targetItem).SPPID.RepresentationId;
1893
                    if (!string.IsNullOrEmpty(sRep))
1894
                    {
1895
                        // LEADER Line 검사
1896
                        bool leaderLine = false;
1897
                        SymbolMapping symbolMapping = document.SymbolMappings.Find(x => x.UID == symbol.DBUID);
1898
                        if (symbolMapping != null)
1899
                            leaderLine = symbolMapping.LEADERLINE;
1900

    
1901
                        // Target Symbol Item 가져오고 Label Modeling
1902
                        LMSymbol _TargetItem = dataSource.GetSymbol(sRep);
1903
                        LMLabelPersist _LMLabelPresist = _placement.PIDPlaceLabel(symbol.SPPID.MAPPINGNAME, ref points, Rotation: symbol.ANGLE, LabeledItem: _TargetItem.AsLMRepresentation(), IsLeaderVisible: leaderLine);
1904

    
1905
                        //Leader 선 센터로
1906
                        if (_LMLabelPresist != null)
1907
                        {
1908
                            // Target Item에 Label의 Attribute Input
1909
                            InputSymbolAttribute(targetItem, symbol.ATTRIBUTES);
1910

    
1911
                            string OID = _LMLabelPresist.get_GraphicOID().ToString();
1912
                            DependencyObject dependency = radApp.ActiveDocument.ActiveSheet.DrawingObjects[OID] as DependencyObject;
1913
                            if (dependency != null)
1914
                            {
1915
                                bool result = false;
1916
                                foreach (var attributes in dependency.AttributeSets)
1917
                                {
1918
                                    foreach (var attribute in attributes)
1919
                                    {
1920
                                        string name = attribute.Name;
1921
                                        string value = attribute.GetValue().ToString();
1922
                                        if (name == "DrawingItemType" && value == "LabelPersist")
1923
                                        {
1924
                                            foreach (DrawingObjectBase drawingObject in dependency.DrawingObjects)
1925
                                            {
1926
                                                if (drawingObject.Type == Ingr.RAD2D.ObjectType.igLineString2d)
1927
                                                {
1928
                                                    Ingr.RAD2D.LineString2d lineString2D = drawingObject as Ingr.RAD2D.LineString2d;
1929
                                                    double prevX = _TargetItem.get_XCoordinate();
1930
                                                    double prevY = _TargetItem.get_YCoordinate();
1931
                                                    lineString2D.InsertVertex(lineString2D.VertexCount, prevX, prevY);
1932
                                                    lineString2D.RemoveVertex(lineString2D.VertexCount);
1933
                                                    result = true;
1934
                                                    break;
1935
                                                }
1936
                                            }
1937
                                        }
1938

    
1939
                                        if (result)
1940
                                            break;
1941
                                    }
1942

    
1943
                                    if (result)
1944
                                        break;
1945
                                }
1946
                            }
1947

    
1948
                            symbol.SPPID.RepresentationId = _LMLabelPresist.AsLMRepresentation().Id;
1949
                            _LMLabelPresist.Commit();
1950
                            ReleaseCOMObjects(_LMLabelPresist);
1951
                        }
1952

    
1953
                        ReleaseCOMObjects(_TargetItem);
1954
                    }
1955
                }
1956
                else if (targetItem != null && targetItem.GetType() == typeof(Line))
1957
                {
1958
                    Line targetLine = targetItem as Line;
1959
                    Dictionary<LMConnector, List<double[]>> connectorVertices = GetPipeRunVertices(targetLine.SPPID.ModelItemId);
1960
                    LMConnector connectedLMConnector = FindTargetLMConnectorForLabel(connectorVertices, symbol.SPPID.ORIGINAL_X, symbol.SPPID.ORIGINAL_Y);
1961
                    if (connectedLMConnector != null)
1962
                    {
1963
                        // Target Item에 Label의 Attribute Input
1964
                        InputSymbolAttribute(targetItem, symbol.ATTRIBUTES);
1965

    
1966
                        // LEADER Line 검사
1967
                        bool leaderLine = false;
1968
                        SymbolMapping symbolMapping = document.SymbolMappings.Find(x => x.UID == symbol.DBUID);
1969
                        if (symbolMapping != null)
1970
                            leaderLine = symbolMapping.LEADERLINE;
1971

    
1972
                        LMLabelPersist _LMLabelPresist = _placement.PIDPlaceLabel(symbol.SPPID.MAPPINGNAME, ref points, Rotation: symbol.ANGLE, LabeledItem: connectedLMConnector.AsLMRepresentation(), IsLeaderVisible: leaderLine);
1973
                        if (_LMLabelPresist != null)
1974
                        {
1975
                            symbol.SPPID.RepresentationId = _LMLabelPresist.AsLMRepresentation().Id;
1976
                            _LMLabelPresist.Commit();
1977
                            ReleaseCOMObjects(_LMLabelPresist);
1978
                        }
1979
                        ReleaseCOMObjects(connectedLMConnector);
1980
                    }
1981

    
1982
                    foreach (var item in connectorVertices)
1983
                        if (item.Key != null)
1984
                            ReleaseCOMObjects(item.Key);
1985
                }
1986
            }
1987
        }
1988

    
1989
        /// <summary>
1990
        /// Equipment를 실제로 Modeling 메서드
1991
        /// </summary>
1992
        /// <param name="equipment"></param>
1993
        private void EquipmentModeling(Equipment equipment)
1994
        {
1995
            if (!string.IsNullOrEmpty(equipment.SPPID.RepresentationId))
1996
                return;
1997

    
1998
            LMSymbol _LMSymbol = null;
1999
            LMSymbol targetItem = null;
2000
            string mappingPath = equipment.SPPID.MAPPINGNAME;
2001
            double x = equipment.SPPID.ORIGINAL_X;
2002
            double y = equipment.SPPID.ORIGINAL_Y;
2003
            int mirror = 0;
2004
            double angle = equipment.ANGLE;
2005

    
2006
            // Mirror 계산
2007
            if (equipment.FLIP == 1)
2008
            {
2009
                mirror = 1;
2010
                if (angle == Math.PI || angle == 0)
2011
                    angle += Math.PI;
2012
            }
2013

    
2014
            SPPIDUtil.ConvertGridPoint(ref x, ref y);
2015

    
2016
            Connector connector = equipment.CONNECTORS.Find(conn =>
2017
            !string.IsNullOrEmpty(conn.CONNECTEDITEM) &&
2018
            conn.CONNECTEDITEM != "None" &&
2019
            conn.ConnectedObject.GetType() == typeof(Equipment));
2020

    
2021
            if (connector != null)
2022
            {
2023
                Equipment connEquipment = SPPIDUtil.FindObjectByUID(document, connector.CONNECTEDITEM) as Equipment;
2024
                VendorPackage connVendorPackage = SPPIDUtil.FindObjectByUID(document, connector.CONNECTEDITEM) as VendorPackage;
2025
                if (connEquipment != null)
2026
                {
2027
                    //if (string.IsNullOrEmpty(connEquipment.SPPID.RepresentationId))
2028
                    //    EquipmentModeling(connEquipment);
2029

    
2030
                    if (!string.IsNullOrEmpty(connEquipment.SPPID.RepresentationId))
2031
                    {
2032
                        targetItem = dataSource.GetSymbol(connEquipment.SPPID.RepresentationId);
2033
                        if (targetItem != null)
2034
                            _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle, TargetItem: targetItem);
2035
                        else
2036
                            _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle);
2037
                    }
2038
                    else
2039
                    {
2040
                        _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle);
2041
                    }
2042
                }
2043
                else if (connVendorPackage != null)
2044
                {
2045
                    if (!string.IsNullOrEmpty(connVendorPackage.SPPID.RepresentationId))
2046
                    {
2047
                        targetItem = dataSource.GetSymbol(connVendorPackage.SPPID.RepresentationId);
2048
                        if (targetItem != null)
2049
                            _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle, TargetItem: targetItem);
2050
                        else
2051
                            _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle);
2052
                    }
2053
                }
2054
                else
2055
                {
2056
                    _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle);
2057
                }
2058
            }
2059
            else
2060
            {
2061
                _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle);
2062
            }
2063

    
2064
            if (_LMSymbol != null)
2065
            {
2066
                _LMSymbol.Commit();
2067
                equipment.SPPID.RepresentationId = _LMSymbol.AsLMRepresentation().Id;
2068
                equipment.SPPID.GraphicOID = _LMSymbol.get_GraphicOID().ToString();
2069
                ReleaseCOMObjects(_LMSymbol);
2070
            }
2071

    
2072
            if (targetItem != null)
2073
            {
2074
                ReleaseCOMObjects(targetItem);
2075
            }
2076

    
2077
            ReleaseCOMObjects(_LMSymbol);
2078
        }
2079

    
2080
        private void VendorPackageModeling(VendorPackage vendorPackage)
2081
        {
2082
            ETCSetting setting = ETCSetting.GetInstance();
2083
            if (!string.IsNullOrEmpty(setting.VendorPackageSymbolPath))
2084
            {
2085
                string symbolPath = setting.VendorPackageSymbolPath;
2086
                double x = vendorPackage.SPPID.ORIGINAL_X;
2087
                double y = vendorPackage.SPPID.ORIGINAL_Y;
2088

    
2089
                LMSymbol symbol = _placement.PIDPlaceSymbol(symbolPath, x, y);
2090
                if (symbol != null)
2091
                {
2092
                    symbol.Commit();
2093
                    vendorPackage.SPPID.RepresentationId = symbol.AsLMRepresentation().Id;
2094
                }
2095

    
2096
                ReleaseCOMObjects(symbol);
2097
                symbol = null;
2098
            }
2099
        }
2100

    
2101
        /// <summary>
2102
        /// 첫 진입점
2103
        /// </summary>
2104
        /// <param name="symbol"></param>
2105
        private void SymbolModelingBySymbol(Symbol symbol)
2106
        {
2107
            SymbolModeling(symbol, null);   /// 심볼을 생성한다
2108
            List<object> endObjects = new List<object>();
2109
            endObjects.Add(symbol);
2110

    
2111
            /// 심볼에 연결되어 있는 항목들을 모델링한다
2112
            foreach (var connector in symbol.CONNECTORS)
2113
            {
2114
                object connItem = SPPIDUtil.FindObjectByUID(document, connector.CONNECTEDITEM);
2115
                if (connItem != null && connItem.GetType() != typeof(Equipment))
2116
                {
2117
                    endObjects.Add(connItem);
2118
                    if (connItem.GetType() == typeof(Symbol))
2119
                    {
2120
                        Symbol connSymbol = connItem as Symbol;
2121
                        if (string.IsNullOrEmpty(connSymbol.SPPID.RepresentationId))
2122
                        {
2123
                            SymbolModeling(connSymbol, symbol);
2124
                        }
2125
                        SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetProgress, document.SYMBOLS.FindAll(x => !string.IsNullOrEmpty(x.SPPID.RepresentationId)).Count);
2126
                        SymbolModelingByNeerSymbolLoop(connSymbol, endObjects);
2127
                    }
2128
                    else if (connItem.GetType() == typeof(Line))
2129
                    {
2130
                        Line connLine = connItem as Line;
2131
                        SymbolModelingByNeerLineLoop(connLine, endObjects, symbol);
2132
                    }
2133
                }
2134
            }
2135
        }
2136

    
2137
        private void SymbolModelingByNeerSymbolLoop(Symbol symbol, List<object> endObjects)
2138
        {
2139
            foreach (var connector in symbol.CONNECTORS)
2140
            {
2141
                object connItem = SPPIDUtil.FindObjectByUID(document, connector.CONNECTEDITEM);
2142
                if (connItem != null && connItem.GetType() != typeof(Equipment))
2143
                {
2144
                    if (!endObjects.Contains(connItem))
2145
                    {
2146
                        endObjects.Add(connItem);
2147
                        if (connItem.GetType() == typeof(Symbol))
2148
                        {
2149
                            Symbol connSymbol = connItem as Symbol;
2150
                            if (string.IsNullOrEmpty(connSymbol.SPPID.RepresentationId))
2151
                            {
2152
                                SymbolModeling(connSymbol, symbol);
2153
                            }
2154
                            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetProgress, document.SYMBOLS.FindAll(x => !string.IsNullOrEmpty(x.SPPID.RepresentationId)).Count);
2155
                            SymbolModelingByNeerSymbolLoop(connSymbol, endObjects);
2156
                        }
2157
                        else if (connItem.GetType() == typeof(Line))
2158
                        {
2159
                            Line connLine = connItem as Line;
2160
                            SymbolModelingByNeerLineLoop(connLine, endObjects, symbol);
2161
                        }
2162
                    }
2163
                }
2164
            }
2165
        }
2166

    
2167
        private void SymbolModelingByNeerLineLoop(Line line, List<object> endObjects, Symbol prevSymbol)
2168
        {
2169
            foreach (var connector in line.CONNECTORS)
2170
            {
2171
                object connItem = SPPIDUtil.FindObjectByUID(document, connector.CONNECTEDITEM);
2172
                if (connItem != null && connItem.GetType() != typeof(Equipment))
2173
                {
2174
                    if (!endObjects.Contains(connItem))
2175
                    {
2176
                        endObjects.Add(connItem);
2177
                        if (connItem.GetType() == typeof(Symbol))
2178
                        {
2179
                            Symbol connSymbol = connItem as Symbol;
2180
                            if (string.IsNullOrEmpty(connSymbol.SPPID.RepresentationId))
2181
                            {
2182
                                Line connLine = SPPIDUtil.GetConnectedLine(prevSymbol, connSymbol);
2183
                                int branchCount = 0;
2184
                                if (connLine != null)
2185
                                    branchCount = SPPIDUtil.GetBranchLineCount(document, connLine);
2186

    
2187
                                List<Symbol> group = new List<Symbol>();
2188
                                SPPIDUtil.FindConnectedSymbolGroup(document, connSymbol, group);
2189
                                Symbol priority = prioritySymbols.Find(x => group.Contains(x));
2190
                                List<Symbol> endModelingGroup = new List<Symbol>();
2191
                                if (priority != null)
2192
                                {
2193
                                    SymbolGroupModeling(priority, group);
2194

    
2195
                                    // Range 겹치는지 확인해야함
2196
                                    double[] prevRange = null;
2197
                                    GetSPPIDSymbolRange(prevSymbol, ref prevRange, bForGraphic: true);
2198
                                    double[] groupRange = null;
2199
                                    GetSPPIDSymbolRange(group, ref groupRange, bForGraphic: true);
2200

    
2201
                                    double distanceX = 0;
2202
                                    double distanceY = 0;
2203
                                    bool overlapX = false;
2204
                                    bool overlapY = false;
2205
                                    SlopeType slopeType = SPPIDUtil.CalcSlope(prevSymbol.SPPID.ORIGINAL_X, prevSymbol.SPPID.ORIGINAL_Y, connSymbol.SPPID.ORIGINAL_X, connSymbol.SPPID.ORIGINAL_Y);
2206
                                    SPPIDUtil.CalcOverlap(prevRange, groupRange, ref distanceX, ref distanceY, ref overlapX, ref overlapY);
2207
                                    if ((slopeType == SlopeType.HORIZONTAL && overlapX) ||
2208
                                        (slopeType == SlopeType.VERTICAL && overlapY))
2209
                                    {
2210
                                        RemoveSymbol(group);
2211
                                        foreach (var _temp in group)
2212
                                            SPPIDUtil.CalcNewCoordinateForSymbol(_temp, prevSymbol, distanceX, distanceY);
2213

    
2214
                                        SymbolGroupModeling(priority, group);
2215
                                    }
2216
                                    else if (branchCount > 0)
2217
                                    {
2218
                                        LMConnector _connector = JustLineModeling(connLine);
2219
                                        if (_connector != null)
2220
                                        {
2221
                                            double distance = GetConnectorDistance(_connector);
2222
                                            int cellCount = (int)Math.Truncate(distance / GridSetting.GetInstance().Length);
2223
                                            _placement.PIDRemovePlacement(_connector.AsLMRepresentation());
2224
                                            _connector.Commit();
2225
                                            ReleaseCOMObjects(_connector);
2226
                                            _connector = null;
2227
                                            if (cellCount < branchCount + 1)
2228
                                            {
2229
                                                int moveCount = branchCount - cellCount;
2230
                                                RemoveSymbol(group);
2231
                                                foreach (var _temp in group)
2232
                                                    SPPIDUtil.CalcNewCoordinateForSymbol(_temp, prevSymbol, moveCount * GridSetting.GetInstance().Length, moveCount * GridSetting.GetInstance().Length);
2233

    
2234
                                                SymbolGroupModeling(priority, group);
2235
                                            }
2236
                                        }
2237
                                    }
2238
                                }
2239
                                else
2240
                                {
2241
                                    SymbolModeling(connSymbol, null);
2242
                                    // Range 겹치는지 확인해야함
2243
                                    double[] prevRange = null;
2244
                                    GetSPPIDSymbolRange(prevSymbol, ref prevRange, bForGraphic: true);
2245
                                    double[] connRange = null;
2246
                                    GetSPPIDSymbolRange(connSymbol, ref connRange, bForGraphic: true);
2247

    
2248
                                    double distanceX = 0;
2249
                                    double distanceY = 0;
2250
                                    bool overlapX = false;
2251
                                    bool overlapY = false;
2252
                                    SlopeType slopeType = SPPIDUtil.CalcSlope(prevSymbol.SPPID.ORIGINAL_X, prevSymbol.SPPID.ORIGINAL_Y, connSymbol.SPPID.ORIGINAL_X, connSymbol.SPPID.ORIGINAL_Y);
2253
                                    SPPIDUtil.CalcOverlap(prevRange, connRange, ref distanceX, ref distanceY, ref overlapX, ref overlapY);
2254
                                    if ((slopeType == SlopeType.HORIZONTAL && overlapX) ||
2255
                                        (slopeType == SlopeType.VERTICAL && overlapY))
2256
                                    {
2257
                                        RemoveSymbol(connSymbol);
2258
                                        SPPIDUtil.CalcNewCoordinateForSymbol(connSymbol, prevSymbol, distanceX, distanceY);
2259

    
2260
                                        SymbolModeling(connSymbol, null);
2261
                                    }
2262
                                    else if (branchCount > 0)
2263
                                    {
2264
                                        LMConnector _connector = JustLineModeling(connLine);
2265
                                        if (_connector != null)
2266
                                        {
2267
                                            double distance = GetConnectorDistance(_connector);
2268
                                            int cellCount = (int)Math.Truncate(distance / GridSetting.GetInstance().Length);
2269
                                            _placement.PIDRemovePlacement(_connector.AsLMRepresentation());
2270
                                            _connector.Commit();
2271
                                            ReleaseCOMObjects(_connector);
2272
                                            _connector = null;
2273
                                            if (cellCount < branchCount + 1)
2274
                                            {
2275
                                                int moveCount = branchCount - cellCount;
2276
                                                RemoveSymbol(group);
2277
                                                foreach (var _temp in group)
2278
                                                    SPPIDUtil.CalcNewCoordinateForSymbol(_temp, prevSymbol, moveCount * GridSetting.GetInstance().Length, moveCount * GridSetting.GetInstance().Length);
2279

    
2280
                                                SymbolGroupModeling(priority, group);
2281
                                            }
2282
                                        }
2283
                                    }
2284
                                }
2285
                            }
2286
                            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetProgress, document.SYMBOLS.FindAll(x => !string.IsNullOrEmpty(x.SPPID.RepresentationId)).Count);
2287
                            SymbolModelingByNeerSymbolLoop(connSymbol, endObjects);
2288
                        }
2289
                        else if (connItem.GetType() == typeof(Line))
2290
                        {
2291
                            Line connLine = connItem as Line;
2292
                            if (!SPPIDUtil.IsBranchLine(connLine, line))
2293
                                SymbolModelingByNeerLineLoop(connLine, endObjects, prevSymbol);
2294
                        }
2295
                    }
2296
                }
2297
            }
2298
        }
2299

    
2300
        private void SymbolGroupModeling(Symbol firstSymbol, List<Symbol> group)
2301
        {
2302
            List<Symbol> endModelingGroup = new List<Symbol>();
2303
            SymbolModeling(firstSymbol, null);
2304
            endModelingGroup.Add(firstSymbol);
2305
            while (endModelingGroup.Count != group.Count)
2306
            {
2307
                foreach (var _symbol in group)
2308
                {
2309
                    if (!endModelingGroup.Contains(_symbol))
2310
                    {
2311
                        foreach (var _connector in _symbol.CONNECTORS)
2312
                        {
2313
                            Symbol _connSymbol = SPPIDUtil.FindObjectByUID(document, _connector.CONNECTEDITEM) as Symbol;
2314
                            if (_connSymbol != null && endModelingGroup.Contains(_connSymbol))
2315
                            {
2316
                                SymbolModeling(_symbol, _connSymbol);
2317
                                endModelingGroup.Add(_symbol);
2318
                                break;
2319
                            }
2320
                        }
2321
                    }
2322
                }
2323
            }
2324
        }
2325

    
2326
        /// <summary>
2327
        /// 심볼을 실제로 Modeling할때 ChildSymbol이 있다면 Modeling하는 메서드
2328
        /// </summary>
2329
        /// <param name="childSymbol"></param>
2330
        /// <param name="parentSymbol"></param>
2331
        private void CreateChildSymbol(ChildSymbol childSymbol, LMSymbol parentSymbol, Symbol parent)
2332
        {
2333
            Ingr.RAD2D.Symbol2d symbol2d = radApp.ActiveDocument.ActiveSheet.DrawingObjects[parentSymbol.get_GraphicOID().ToString()];
2334
            double x1 = 0;
2335
            double x2 = 0;
2336
            double y1 = 0;
2337
            double y2 = 0;
2338
            symbol2d.Range(out x1, out y1, out x2, out y2);
2339

    
2340
            LMSymbol _LMSymbol = _placement.PIDPlaceSymbol(childSymbol.SPPID.MAPPINGNAME, (x1 + x2) / 2, (y1 + y2) / 2, TargetItem: parentSymbol);
2341
            if (_LMSymbol != null)
2342
            {
2343
                _LMSymbol.Commit();
2344
                childSymbol.SPPID.RepresentationId = _LMSymbol.AsLMRepresentation().Id;
2345
                foreach (var item in childSymbol.ChildSymbols)
2346
                    CreateChildSymbol(item, _LMSymbol, parent);
2347
            }
2348

    
2349

    
2350
            ReleaseCOMObjects(_LMSymbol);
2351
        }
2352
        double index = 0;
2353
        private void NewLineModeling(Line line, bool isBranchModeling = false)
2354
        {
2355
            if (!string.IsNullOrEmpty(line.SPPID.ModelItemId) || (BranchLines.Contains(line) && !isBranchModeling))
2356
                return;
2357

    
2358
            List<Line> group = new List<Line>();
2359
            GetConnectedLineGroup(line, group);
2360
            LineCoordinateCorrection(group);
2361

    
2362
            foreach (var groupLine in group)
2363
            {
2364
                if (!isBranchModeling && SPPIDUtil.IsBranchLine(groupLine))
2365
                {
2366
                    BranchLines.Add(groupLine);
2367
                    continue;
2368
                }
2369

    
2370
                bool diagonal = false;
2371
                if (groupLine.SlopeType != SlopeType.HORIZONTAL && groupLine.SlopeType != SlopeType.VERTICAL)
2372
                    diagonal = true;
2373
                _LMAItem _LMAItem = _placement.PIDCreateItem(groupLine.SPPID.MAPPINGNAME);
2374
                LMSymbol _LMSymbolStart = null;
2375
                LMSymbol _LMSymbolEnd = null;
2376
                PlaceRunInputs placeRunInputs = new PlaceRunInputs();
2377
                foreach (var connector in groupLine.CONNECTORS)
2378
                {
2379
                    double x = 0;
2380
                    double y = 0;
2381
                    GetTargetLineConnectorPoint(connector, groupLine, ref x, ref y);
2382
                    if (connector.ConnectedObject == null)
2383
                        placeRunInputs.AddPoint(x, y);
2384
                    else if (connector.ConnectedObject.GetType() == typeof(Symbol))
2385
                    {
2386
                        Symbol targetSymbol = connector.ConnectedObject as Symbol;
2387
                        GetTargetSymbolConnectorPoint(targetSymbol.CONNECTORS.Find(z => z.ConnectedObject == groupLine), targetSymbol, ref x, ref y);
2388
                        if (groupLine.CONNECTORS.IndexOf(connector) == 0)
2389
                        {
2390
                            _LMSymbolStart = GetTargetSymbol(targetSymbol, groupLine);
2391
                            if (_LMSymbolStart != null)
2392
                                placeRunInputs.AddSymbolTarget(_LMSymbolStart, x, y, diagonal);
2393
                            else
2394
                                placeRunInputs.AddPoint(x, y);
2395
                        }
2396
                        else
2397
                        {
2398
                            _LMSymbolEnd = GetTargetSymbol(targetSymbol, groupLine);
2399
                            if (_LMSymbolEnd != null)
2400
                                placeRunInputs.AddSymbolTarget(_LMSymbolEnd, x, y, diagonal);
2401
                            else
2402
                                placeRunInputs.AddPoint(x, y);
2403
                        }
2404
                    }
2405
                    else if (connector.ConnectedObject.GetType() == typeof(Line))
2406
                    {
2407
                        Line targetLine = connector.ConnectedObject as Line;
2408
                        if (!string.IsNullOrEmpty(targetLine.SPPID.ModelItemId))
2409
                        {
2410
                            LMConnector targetConnector = FindTargetLMConnectorForBranch(line, targetLine, ref x, ref y);
2411
                            if (targetConnector != null)
2412
                            {
2413
                                placeRunInputs.AddConnectorTarget(targetConnector, x, y, diagonal);
2414
                                ChangeLineSPPIDCoordinateByConnector(groupLine, targetLine, x, y, false);
2415
                            }
2416
                            else
2417
                            {
2418
                                placeRunInputs.AddPoint(x, y);
2419
                                ChangeLineSPPIDCoordinateByConnector(groupLine, targetLine, x, y, false);
2420
                            }
2421
                        }
2422
                        else
2423
                        {
2424
                            if (groupLine.CONNECTORS.IndexOf(connector) == 0)
2425
                            {
2426
                                index += 0.01;
2427
                                if (groupLine.SlopeType == SlopeType.HORIZONTAL)
2428
                                    placeRunInputs.AddPoint(x, -0.1 - index);
2429
                                else if (groupLine.SlopeType == SlopeType.VERTICAL)
2430
                                    placeRunInputs.AddPoint(-0.1 - index, y);
2431
                                else
2432
                                {
2433
                                    Line nextLine = groupLine.CONNECTORS[0].ConnectedObject as Line;
2434
                                    if (SPPIDUtil.CalcAngle(nextLine.SPPID.START_X, nextLine.SPPID.START_Y, nextLine.SPPID.END_X, nextLine.SPPID.END_Y) < 45)
2435
                                        placeRunInputs.AddPoint(-0.1 - index, y);
2436
                                    else
2437
                                        placeRunInputs.AddPoint(x, -0.1 - index);
2438
                                }
2439
                            }
2440

    
2441
                            placeRunInputs.AddPoint(x, y);
2442

    
2443
                            if (groupLine.CONNECTORS.IndexOf(connector) == 1)
2444
                            {
2445
                                index += 0.01;
2446
                                if (groupLine.SlopeType == SlopeType.HORIZONTAL)
2447
                                    placeRunInputs.AddPoint(x, -0.1 - index);
2448
                                else if (groupLine.SlopeType == SlopeType.VERTICAL)
2449
                                    placeRunInputs.AddPoint(-0.1 - index, y);
2450
                                else
2451
                                {
2452
                                    Line nextLine = groupLine.CONNECTORS[1].ConnectedObject as Line;
2453
                                    if (SPPIDUtil.CalcAngle(nextLine.SPPID.START_X, nextLine.SPPID.START_Y, nextLine.SPPID.END_X, nextLine.SPPID.END_Y) < 45)
2454
                                        placeRunInputs.AddPoint(-0.1 - index, y);
2455
                                    else
2456
                                        placeRunInputs.AddPoint(x, -0.1 - index);
2457
                                }
2458
                            }
2459
                        }
2460
                    }
2461
                    else
2462
                        placeRunInputs.AddPoint(x, y);
2463
                }
2464

    
2465
                LMConnector _lMConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
2466
                if (_lMConnector != null)
2467
                {
2468
                    _lMConnector.Commit();
2469
                    groupLine.SPPID.ModelItemId = _lMConnector.ModelItemID;
2470

    
2471
                    bool bRemodelingStart = false;
2472
                    if (_LMSymbolStart != null)
2473
                        NeedReModeling(groupLine, _LMSymbolStart, ref bRemodelingStart);
2474
                    bool bRemodelingEnd = false;
2475
                    if (_LMSymbolEnd != null)
2476
                        NeedReModeling(groupLine, _LMSymbolEnd, ref bRemodelingEnd);
2477

    
2478
                    if (bRemodelingStart || bRemodelingEnd)
2479
                        ReModelingLine(groupLine, _lMConnector, _LMSymbolStart, _LMSymbolEnd, bRemodelingStart, bRemodelingEnd);
2480

    
2481
                    FlowMarkModeling(groupLine);
2482

    
2483
                    ReleaseCOMObjects(_lMConnector);
2484

    
2485
                    LMModelItem modelItem = dataSource.GetModelItem(groupLine.SPPID.ModelItemId);
2486
                    if (modelItem != null)
2487
                    {
2488
                        LMAAttribute attribute = modelItem.Attributes["FlowDirection"];
2489
                        if (attribute != null)
2490
                            attribute.set_Value("End 1 is upstream (Inlet)");
2491
                        modelItem.Commit();
2492
                    }
2493
                    ReleaseCOMObjects(modelItem);
2494
                    modelItem = null;
2495
                }
2496
                else if (!isBranchModeling)
2497
                {
2498
                    Log.Write("Main Line Modeling : " + groupLine.UID);
2499
                }
2500

    
2501
                List<object> removeLines = groupLine.CONNECTORS.FindAll(x =>
2502
                x.ConnectedObject != null &&
2503
                x.ConnectedObject.GetType() == typeof(Line) &&
2504
                !string.IsNullOrEmpty(((Line)x.ConnectedObject).SPPID.ModelItemId))
2505
                .Select(x => x.ConnectedObject)
2506
                .ToList();
2507

    
2508
                foreach (var item in removeLines)
2509
                    RemoveLineForModeling(item as Line);
2510

    
2511
                ReleaseCOMObjects(_LMAItem);
2512
                _LMAItem = null;
2513
                ReleaseCOMObjects(placeRunInputs);
2514
                placeRunInputs = null;
2515
                ReleaseCOMObjects(_LMSymbolStart);
2516
                _LMSymbolStart = null;
2517
                ReleaseCOMObjects(_LMSymbolEnd);
2518
                _LMSymbolEnd = null;
2519

    
2520
                if (isBranchModeling && BranchLines.Contains(groupLine))
2521
                    BranchLines.Remove(groupLine);
2522
            }
2523
        }
2524

    
2525
        private LMConnector JustLineModeling(Line line)
2526
        {
2527
            bool diagonal = false;
2528
            if (line.SlopeType != SlopeType.HORIZONTAL && line.SlopeType != SlopeType.VERTICAL)
2529
                diagonal = true;
2530
            _LMAItem _LMAItem = _placement.PIDCreateItem(line.SPPID.MAPPINGNAME);
2531
            LMSymbol _LMSymbolStart = null;
2532
            LMSymbol _LMSymbolEnd = null;
2533
            PlaceRunInputs placeRunInputs = new PlaceRunInputs();
2534
            foreach (var connector in line.CONNECTORS)
2535
            {
2536
                double x = 0;
2537
                double y = 0;
2538
                GetTargetLineConnectorPoint(connector, line, ref x, ref y);
2539
                if (connector.ConnectedObject == null)
2540
                {
2541
                    placeRunInputs.AddPoint(x, y);
2542
                }
2543
                else if (connector.ConnectedObject.GetType() == typeof(Symbol))
2544
                {
2545
                    Symbol targetSymbol = connector.ConnectedObject as Symbol;
2546
                    GetTargetSymbolConnectorPoint(targetSymbol.CONNECTORS.Find(z => z.ConnectedObject == line), targetSymbol, ref x, ref y);
2547
                    if (line.CONNECTORS.IndexOf(connector) == 0)
2548
                    {
2549
                        _LMSymbolStart = GetTargetSymbol(targetSymbol, line);
2550
                        if (_LMSymbolStart != null)
2551
                            placeRunInputs.AddSymbolTarget(_LMSymbolStart, x, y, diagonal);
2552
                        else
2553
                            placeRunInputs.AddPoint(x, y);
2554
                    }
2555
                    else
2556
                    {
2557
                        _LMSymbolEnd = GetTargetSymbol(targetSymbol, line);
2558
                        if (_LMSymbolEnd != null)
2559
                            placeRunInputs.AddSymbolTarget(_LMSymbolEnd, x, y, diagonal);
2560
                        else
2561
                            placeRunInputs.AddPoint(x, y);
2562
                    }
2563
                }
2564
                else if (connector.ConnectedObject.GetType() == typeof(Line))
2565
                {
2566
                    Line targetLine = connector.ConnectedObject as Line;
2567
                    if (!string.IsNullOrEmpty(targetLine.SPPID.ModelItemId))
2568
                    {
2569
                        LMConnector targetConnector = FindTargetLMConnectorForBranch(line, targetLine, ref x, ref y);
2570
                        if (targetConnector != null)
2571
                        {
2572
                            placeRunInputs.AddConnectorTarget(targetConnector, x, y, diagonal);
2573
                            ChangeLineSPPIDCoordinateByConnector(line, targetLine, x, y, false);
2574
                        }
2575
                        else
2576
                        {
2577
                            placeRunInputs.AddPoint(x, y);
2578
                            ChangeLineSPPIDCoordinateByConnector(line, targetLine, x, y, false);
2579
                        }
2580
                    }
2581
                    else
2582
                        placeRunInputs.AddPoint(x, y);
2583
                }
2584
            }
2585

    
2586
            LMConnector _lMConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
2587
            if (_lMConnector != null)
2588
                _lMConnector.Commit();
2589

    
2590
            ReleaseCOMObjects(_LMAItem);
2591
            _LMAItem = null;
2592
            ReleaseCOMObjects(placeRunInputs);
2593
            placeRunInputs = null;
2594
            ReleaseCOMObjects(_LMSymbolStart);
2595
            _LMSymbolStart = null;
2596
            ReleaseCOMObjects(_LMSymbolEnd);
2597
            _LMSymbolEnd = null;
2598

    
2599
            return _lMConnector;
2600
        }
2601

    
2602
        private void RemoveLineForModeling(Line line)
2603
        {
2604
            LMModelItem modelItem = dataSource.GetModelItem(line.SPPID.ModelItemId);
2605
            if (modelItem != null)
2606
            {
2607
                foreach (LMRepresentation rep in modelItem.Representations)
2608
                {
2609
                    if (rep.Attributes["RepresentationType"].get_Value() == "Connector" && rep.Attributes["ItemStatus"].get_Value() == "Active")
2610
                    {
2611
                        LMConnector _LMConnector = dataSource.GetConnector(rep.Id);
2612
                        dynamic OID = rep.get_GraphicOID().ToString();
2613
                        DependencyObject drawingObject = radApp.ActiveDocument.ActiveSheet.DrawingObjects[OID];
2614
                        Ingr.RAD2D.LineStringGeometry2d lineStringGeometry = drawingObject.GetGeometry() as Ingr.RAD2D.LineStringGeometry2d;
2615
                        int verticesCount = lineStringGeometry.VertexCount;
2616
                        double[] vertices = null;
2617
                        lineStringGeometry.GetVertices(ref verticesCount, ref vertices);
2618
                        for (int i = 0; i < verticesCount; i++)
2619
                        {
2620
                            double x = 0;
2621
                            double y = 0;
2622
                            lineStringGeometry.GetVertex(i + 1, ref x, ref y);
2623
                            if (verticesCount == 2 && (x < 0 || y < 0))
2624
                                _placement.PIDRemovePlacement(rep);
2625
                        }
2626
                        ReleaseCOMObjects(_LMConnector);
2627
                    }
2628
                }
2629

    
2630
                ReleaseCOMObjects(modelItem);
2631
            }
2632
        }
2633

    
2634
        private void RemoveLine(Line line)
2635
        {
2636
            LMModelItem modelItem = dataSource.GetModelItem(line.SPPID.ModelItemId);
2637
            if (modelItem != null)
2638
            {
2639
                foreach (LMRepresentation rep in modelItem.Representations)
2640
                {
2641
                    if (rep.Attributes["RepresentationType"].get_Value() == "Connector" && rep.Attributes["ItemStatus"].get_Value() == "Active")
2642
                        _placement.PIDRemovePlacement(rep);
2643
                }
2644
                ReleaseCOMObjects(modelItem);
2645
            }
2646
            line.SPPID.ModelItemId = null;
2647
        }
2648

    
2649
        private void GetConnectedLineGroup(Line line, List<Line> group)
2650
        {
2651
            if (!group.Contains(line))
2652
                group.Add(line);
2653
            foreach (var connector in line.CONNECTORS)
2654
            {
2655
                if (connector.ConnectedObject != null &&
2656
                    connector.ConnectedObject.GetType() == typeof(Line) &&
2657
                    !group.Contains(connector.ConnectedObject) &&
2658
                    string.IsNullOrEmpty(((Line)connector.ConnectedObject).SPPID.ModelItemId))
2659
                {
2660
                    Line connLine = connector.ConnectedObject as Line;
2661
                    if (!SPPIDUtil.IsBranchLine(connLine, line))
2662
                    {
2663
                        if (line.CONNECTORS.IndexOf(connector) == 0)
2664
                            group.Insert(0, connLine);
2665
                        else
2666
                            group.Add(connLine);
2667
                        GetConnectedLineGroup(connLine, group);
2668
                    }
2669
                }
2670
            }
2671
        }
2672

    
2673
        private void LineCoordinateCorrection(List<Line> group)
2674
        {
2675
            // 순서대로 전 Item 기준 정렬
2676
            LineCoordinateCorrectionByStart(group);
2677

    
2678
            // 역으로 심볼이 있을 경우 좌표 보정
2679
            LineCoordinateCorrectionForLastLine(group);
2680
        }
2681

    
2682
        private void LineCoordinateCorrectionByStart(List<Line> group)
2683
        {
2684
            for (int i = 0; i < group.Count; i++)
2685
            {
2686
                Line line = group[i];
2687
                if (i == 0)
2688
                {
2689
                    Connector symbolConnector = line.CONNECTORS.Find(x => x.ConnectedObject != null && x.ConnectedObject.GetType() == typeof(Symbol));
2690
                    if (symbolConnector != null)
2691
                        LineCoordinateCorrectionByConnItem(line, symbolConnector.ConnectedObject);
2692
                }
2693
                else if (i != 0)
2694
                {
2695
                    LineCoordinateCorrectionByConnItem(line, group[i - 1]);
2696
                }
2697
            }
2698
        }
2699

    
2700
        private void LineCoordinateCorrectionForLastLine(List<Line> group)
2701
        {
2702
            Line checkLine = group[group.Count - 1];
2703
            Connector lastSymbolConnector = checkLine.CONNECTORS.Find(x => x.ConnectedObject != null && x.ConnectedObject.GetType() == typeof(Symbol));
2704
            if (lastSymbolConnector != null)
2705
            {
2706
                LineCoordinateCorrectionByConnItem(checkLine, lastSymbolConnector.ConnectedObject);
2707
                for (int i = group.Count - 2; i >= 0; i--)
2708
                {
2709
                    Line line = group[i + 1];
2710
                    Line prevLine = group[i];
2711

    
2712
                    // 같으면 보정
2713
                    if (line.SlopeType == prevLine.SlopeType)
2714
                        LineCoordinateCorrectionByConnItem(prevLine, line);
2715
                    else
2716
                    {
2717
                        if (line.SlopeType == SlopeType.HORIZONTAL)
2718
                        {
2719
                            double prevX = 0;
2720
                            double prevY = 0;
2721
                            GetTargetLineConnectorPoint(prevLine.CONNECTORS.Find(z => z.ConnectedObject == line), prevLine, ref prevX, ref prevY);
2722
                            ChangeLineSPPIDCoordinateByConnectorOnlyX(line, prevLine, prevX);
2723

    
2724
                            double x = 0;
2725
                            double y = 0;
2726
                            GetTargetLineConnectorPoint(line.CONNECTORS.Find(z => z.ConnectedObject == prevLine), line, ref x, ref y);
2727
                            ChangeLineSPPIDCoordinateByConnectorOnlyY(prevLine, line, y);
2728
                        }
2729
                        else if (line.SlopeType == SlopeType.VERTICAL)
2730
                        {
2731
                            double prevX = 0;
2732
                            double prevY = 0;
2733
                            GetTargetLineConnectorPoint(prevLine.CONNECTORS.Find(z => z.ConnectedObject == line), prevLine, ref prevX, ref prevY);
2734
                            ChangeLineSPPIDCoordinateByConnectorOnlyY(line, prevLine, prevY);
2735

    
2736
                            double x = 0;
2737
                            double y = 0;
2738
                            GetTargetLineConnectorPoint(line.CONNECTORS.Find(z => z.ConnectedObject == prevLine), line, ref x, ref y);
2739
                            ChangeLineSPPIDCoordinateByConnectorOnlyX(prevLine, line, x);
2740
                        }
2741
                        break;
2742
                    }
2743
                }
2744
            }
2745
        }
2746

    
2747
        private void LineCoordinateCorrectionByConnItem(Line line, object connItem)
2748
        {
2749
            double x = 0;
2750
            double y = 0;
2751
            if (connItem.GetType() == typeof(Symbol))
2752
            {
2753
                Symbol targetSymbol = connItem as Symbol;
2754
                Connector targetConnector = targetSymbol.CONNECTORS.Find(z => z.ConnectedObject == line);
2755
                if (targetConnector != null)
2756
                    GetTargetSymbolConnectorPoint(targetConnector, targetSymbol, ref x, ref y);
2757
                else
2758
                    throw new Exception("Target symbol UID : " + targetSymbol.UID + "\r\nLine UID : " + line.UID);
2759
            }
2760
            else if (connItem.GetType() == typeof(Line))
2761
            {
2762
                Line targetLine = connItem as Line;
2763
                GetTargetLineConnectorPoint(targetLine.CONNECTORS.Find(z => z.ConnectedObject == line), targetLine, ref x, ref y);
2764
            }
2765

    
2766
            ChangeLineSPPIDCoordinateByConnector(line, connItem, x, y);
2767
        }
2768

    
2769
        private void ChangeLineSPPIDCoordinateByConnector(Line line, object connItem, double x, double y, bool changeOtherCoordinate = true)
2770
        {
2771
            Connector connector = line.CONNECTORS.Find(z => z.ConnectedObject == connItem);
2772
            int index = line.CONNECTORS.IndexOf(connector);
2773
            if (index == 0)
2774
            {
2775
                line.SPPID.START_X = x;
2776
                line.SPPID.START_Y = y;
2777
                if (line.SlopeType == SlopeType.HORIZONTAL && changeOtherCoordinate)
2778
                    line.SPPID.END_Y = y;
2779
                else if (line.SlopeType == SlopeType.VERTICAL && changeOtherCoordinate)
2780
                    line.SPPID.END_X = x;
2781
            }
2782
            else
2783
            {
2784
                line.SPPID.END_X = x;
2785
                line.SPPID.END_Y = y;
2786
                if (line.SlopeType == SlopeType.HORIZONTAL && changeOtherCoordinate)
2787
                    line.SPPID.START_Y = y;
2788
                else if (line.SlopeType == SlopeType.VERTICAL && changeOtherCoordinate)
2789
                    line.SPPID.START_X = x;
2790
            }
2791
        }
2792

    
2793
        private void ChangeLineSPPIDCoordinateByConnectorOnlyX(Line line, object connItem, double x)
2794
        {
2795
            Connector connector = line.CONNECTORS.Find(z => z.ConnectedObject == connItem);
2796
            int index = line.CONNECTORS.IndexOf(connector);
2797
            if (index == 0)
2798
            {
2799
                line.SPPID.START_X = x;
2800
                if (line.SlopeType == SlopeType.VERTICAL)
2801
                    line.SPPID.END_X = x;
2802
            }
2803
            else
2804
            {
2805
                line.SPPID.END_X = x;
2806
                if (line.SlopeType == SlopeType.VERTICAL)
2807
                    line.SPPID.START_X = x;
2808
            }
2809
        }
2810

    
2811
        private void ChangeLineSPPIDCoordinateByConnectorOnlyY(Line line, object connItem, double y)
2812
        {
2813
            Connector connector = line.CONNECTORS.Find(z => z.ConnectedObject == connItem);
2814
            int index = line.CONNECTORS.IndexOf(connector);
2815
            if (index == 0)
2816
            {
2817
                line.SPPID.START_Y = y;
2818
                if (line.SlopeType == SlopeType.HORIZONTAL)
2819
                    line.SPPID.END_Y = y;
2820
            }
2821
            else
2822
            {
2823
                line.SPPID.END_Y = y;
2824
                if (line.SlopeType == SlopeType.HORIZONTAL)
2825
                    line.SPPID.START_Y = y;
2826
            }
2827
        }
2828

    
2829
        private void NeedReModeling(Line line, LMSymbol symbol, ref bool result)
2830
        {
2831
            if (symbol != null)
2832
            {
2833
                string repID = symbol.AsLMRepresentation().Id;
2834
                string symbolUID = SPPIDUtil.FindSymbolByRepresentationID(document, repID).UID;
2835
                string lineUID = line.UID;
2836

    
2837
                SpecBreak startSpecBreak = document.SpecBreaks.Find(x =>
2838
                (x.DownStreamUID == symbolUID || x.UpStreamUID == symbolUID) &&
2839
                (x.DownStreamUID == lineUID || x.UpStreamUID == lineUID));
2840

    
2841
                EndBreak startEndBreak = document.EndBreaks.Find(x =>
2842
                (x.OWNER == symbolUID || x.PROPERTIES.Find(y => y.ATTRIBUTE == "Connected Item").VALUE == symbolUID) &&
2843
                (x.OWNER == lineUID || x.PROPERTIES.Find(y => y.ATTRIBUTE == "Connected Item").VALUE == lineUID));
2844

    
2845
                if (startSpecBreak != null || startEndBreak != null)
2846
                    result = true;
2847
            }
2848
        }
2849

    
2850
        /// <summary>
2851
        /// Symbol에 붙을 경우 Line을 Remodeling 한다.
2852
        /// </summary>
2853
        /// <param name="lines"></param>
2854
        /// <param name="prevLMConnector"></param>
2855
        /// <param name="startSymbol"></param>
2856
        /// <param name="endSymbol"></param>
2857
        private void ReModelingLine(Line line, LMConnector prevLMConnector, LMSymbol startSymbol, LMSymbol endSymbol, bool bStart, bool bEnd)
2858
        {
2859
            string symbolPath = string.Empty;
2860
            #region get symbol path
2861
            LMModelItem modelItem = dataSource.GetModelItem(prevLMConnector.ModelItemID);
2862
            symbolPath = GetSPPIDFileName(modelItem);
2863
            ReleaseCOMObjects(modelItem);
2864
            #endregion
2865
            bool diagonal = false;
2866
            if (line.SlopeType != SlopeType.HORIZONTAL && line.SlopeType != SlopeType.VERTICAL)
2867
                diagonal = true;
2868
            _LMAItem _LMAItem = _placement.PIDCreateItem(symbolPath);
2869
            LMConnector newConnector = null;
2870
            dynamic OID = prevLMConnector.get_GraphicOID().ToString();
2871
            DependencyObject drawingObject = radApp.ActiveDocument.ActiveSheet.DrawingObjects[OID];
2872
            Ingr.RAD2D.LineStringGeometry2d lineStringGeometry = drawingObject.GetGeometry() as Ingr.RAD2D.LineStringGeometry2d;
2873
            int verticesCount = lineStringGeometry.VertexCount;
2874
            PlaceRunInputs placeRunInputs = new PlaceRunInputs();
2875

    
2876
            List<double[]> vertices = new List<double[]>();
2877
            for (int i = 1; i <= verticesCount; i++)
2878
            {
2879
                double x = 0;
2880
                double y = 0;
2881
                lineStringGeometry.GetVertex(i, ref x, ref y);
2882
                vertices.Add(new double[] { x, y });
2883
            }
2884

    
2885
            for (int i = 0; i < vertices.Count; i++)
2886
            {
2887
                double[] points = vertices[i];
2888
                // 시작 심볼이 있고 첫번째 좌표일 때
2889
                if (startSymbol != null && i == 0)
2890
                {
2891
                    if (bStart)
2892
                    {
2893
                        SlopeType slopeType = SPPIDUtil.CalcSlope(points[0], points[1], vertices[i + 1][0], vertices[i + 1][1]);
2894
                        if (slopeType == SlopeType.HORIZONTAL)
2895
                            placeRunInputs.AddPoint(points[0], -0.1);
2896
                        else if (slopeType == SlopeType.VERTICAL)
2897
                            placeRunInputs.AddPoint(-0.1, points[1]);
2898
                        else
2899
                            placeRunInputs.AddPoint(points[0], -0.1);
2900

    
2901
                        placeRunInputs.AddPoint(points[0], points[1]);
2902
                    }
2903
                    else
2904
                    {
2905
                        placeRunInputs.AddSymbolTarget(startSymbol, points[0], points[1], diagonal);
2906
                    }
2907
                }
2908
                // 마지막 심볼이 있고 마지막 좌표일 때
2909
                else if (endSymbol != null && i == vertices.Count - 1)
2910
                {
2911
                    if (bEnd)
2912
                    {
2913
                        placeRunInputs.AddPoint(points[0], points[1]);
2914

    
2915
                        SlopeType slopeType = SPPIDUtil.CalcSlope(points[0], points[1], vertices[i - 1][0], vertices[i - 1][1]);
2916
                        if (slopeType == SlopeType.HORIZONTAL)
2917
                            placeRunInputs.AddPoint(points[0], -0.1);
2918
                        else if (slopeType == SlopeType.VERTICAL)
2919
                            placeRunInputs.AddPoint(-0.1, points[1]);
2920
                        else
2921
                            placeRunInputs.AddPoint(points[0], -0.1);
2922
                    }
2923
                    else
2924
                    {
2925
                        placeRunInputs.AddSymbolTarget(endSymbol, points[0], points[1], diagonal);
2926
                    }
2927
                }
2928
                // 첫번째이며 시작 심볼이 아니고 Connecotr일 경우
2929
                else if (i == 0 && prevLMConnector.ConnectItem1SymbolObject != null)
2930
                    placeRunInputs.AddSymbolTarget(prevLMConnector.ConnectItem1SymbolObject, points[0], points[1], diagonal);
2931
                // 마지막이며 마지막 심볼이 아니고 Connecotr일 경우
2932
                else if (i == vertices.Count - 1 && prevLMConnector.ConnectItem2SymbolObject != null)
2933
                    placeRunInputs.AddSymbolTarget(prevLMConnector.ConnectItem2SymbolObject, points[0], points[1], diagonal);
2934
                else
2935
                    placeRunInputs.AddPoint(points[0], points[1]);
2936
            }
2937

    
2938
            _placement.PIDRemovePlacement(prevLMConnector.AsLMRepresentation());
2939
            newConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
2940

    
2941
            ReleaseCOMObjects(placeRunInputs);
2942
            ReleaseCOMObjects(_LMAItem);
2943
            ReleaseCOMObjects(modelItem);
2944

    
2945
            if (newConnector != null)
2946
            {
2947
                newConnector.Commit();
2948
                if (startSymbol != null && bStart)
2949
                {
2950
                    _LMAItem = _placement.PIDCreateItem(symbolPath);
2951
                    placeRunInputs = new PlaceRunInputs();
2952
                    placeRunInputs.AddSymbolTarget(startSymbol, vertices[0][0], vertices[0][1]);
2953
                    placeRunInputs.AddConnectorTarget(newConnector, vertices[0][0], vertices[0][1]);
2954
                    LMConnector _LMConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
2955
                    if (_LMConnector != null)
2956
                    {
2957
                        _LMConnector.Commit();
2958
                        RemoveConnectorForReModelingLine(newConnector);
2959
                        ZeroLengthModelItemID.Add(_LMConnector.ModelItemID);
2960
                        ReleaseCOMObjects(_LMConnector);
2961
                    }
2962
                    ReleaseCOMObjects(placeRunInputs);
2963
                    ReleaseCOMObjects(_LMAItem);
2964
                }
2965

    
2966
                if (endSymbol != null && bEnd)
2967
                {
2968
                    if (startSymbol != null)
2969
                    {
2970
                        Dictionary<LMConnector, List<double[]>> dicVertices = GetPipeRunVertices(newConnector.ModelItemID);
2971
                        newConnector = dicVertices.First().Key;
2972
                    }
2973

    
2974
                    _LMAItem = _placement.PIDCreateItem(symbolPath);
2975
                    placeRunInputs = new PlaceRunInputs();
2976
                    placeRunInputs.AddSymbolTarget(endSymbol, vertices[vertices.Count - 1][0], vertices[vertices.Count - 1][1]);
2977
                    placeRunInputs.AddConnectorTarget(newConnector, vertices[vertices.Count - 1][0], vertices[vertices.Count - 1][1]);
2978
                    LMConnector _LMConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
2979
                    if (_LMConnector != null)
2980
                    {
2981
                        _LMConnector.Commit();
2982
                        RemoveConnectorForReModelingLine(newConnector);
2983
                        ZeroLengthModelItemIDReverse.Add(_LMConnector.ModelItemID);
2984
                        ReleaseCOMObjects(_LMConnector);
2985
                    }
2986
                    ReleaseCOMObjects(placeRunInputs);
2987
                    ReleaseCOMObjects(_LMAItem);
2988
                }
2989

    
2990
                line.SPPID.ModelItemId = newConnector.ModelItemID;
2991
                ReleaseCOMObjects(newConnector);
2992
            }
2993

    
2994
            ReleaseCOMObjects(modelItem);
2995
        }
2996

    
2997
        /// <summary>
2998
        /// Remodeling 과정에서 생긴 불필요한 Connector 제거
2999
        /// </summary>
3000
        /// <param name="connector"></param>
3001
        private void RemoveConnectorForReModelingLine(LMConnector connector)
3002
        {
3003
            Dictionary<LMConnector, List<double[]>> dicVertices = GetPipeRunVertices(connector.ModelItemID);
3004
            foreach (var item in dicVertices)
3005
            {
3006
                if (item.Value.Count == 2)
3007
                {
3008
                    bool result = false;
3009
                    foreach (var point in item.Value)
3010
                    {
3011
                        if (point[0] < 0 || point[1] < 0)
3012
                        {
3013
                            result = true;
3014
                            _placement.PIDRemovePlacement(item.Key.AsLMRepresentation());
3015
                            break;
3016
                        }
3017
                    }
3018

    
3019
                    if (result)
3020
                        break;
3021
                }
3022
            }
3023
            foreach (var item in dicVertices)
3024
                ReleaseCOMObjects(item.Key);
3025
        }
3026

    
3027
        /// <summary>
3028
        /// Symbol이 모델링된 SPPPID Symbol Object를 반환 - 연결된 Symbol이 ChildSymbol일 수도 있기때문에 메서드 개발
3029
        /// </summary>
3030
        /// <param name="symbol"></param>
3031
        /// <param name="line"></param>
3032
        /// <returns></returns>
3033
        private LMSymbol GetTargetSymbol(Symbol symbol, Line line)
3034
        {
3035
            LMSymbol _LMSymbol = null;
3036
            foreach (var connector in symbol.CONNECTORS)
3037
            {
3038
                if (connector.CONNECTEDITEM == line.UID)
3039
                {
3040
                    if (connector.Index == 0)
3041
                        _LMSymbol = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
3042
                    else
3043
                    {
3044
                        ChildSymbol child = null;
3045
                        foreach (var childSymbol in symbol.ChildSymbols)
3046
                        {
3047
                            if (childSymbol.Connectors.Contains(connector))
3048
                                child = childSymbol;
3049
                            else
3050
                                child = GetChildSymbolByConnector(childSymbol, connector);
3051

    
3052
                            if (child != null)
3053
                                break;
3054
                        }
3055

    
3056
                        if (child != null)
3057
                            _LMSymbol = dataSource.GetSymbol(child.SPPID.RepresentationId);
3058
                    }
3059

    
3060
                    break;
3061
                }
3062
            }
3063

    
3064
            return _LMSymbol;
3065
        }
3066

    
3067
        /// <summary>
3068
        /// Connector를 가지고 있는 ChildSymbol Object 반환
3069
        /// </summary>
3070
        /// <param name="item"></param>
3071
        /// <param name="connector"></param>
3072
        /// <returns></returns>
3073
        private ChildSymbol GetChildSymbolByConnector(ChildSymbol item, Connector connector)
3074
        {
3075
            foreach (var childSymbol in item.ChildSymbols)
3076
            {
3077
                if (childSymbol.Connectors.Contains(connector))
3078
                    return childSymbol;
3079
                else
3080
                    return GetChildSymbolByConnector(childSymbol, connector);
3081
            }
3082

    
3083
            return null;
3084
        }
3085

    
3086
        /// <summary>
3087
        /// EndBreak 모델링 메서드
3088
        /// </summary>
3089
        /// <param name="endBreak"></param>
3090
        private void EndBreakModeling(EndBreak endBreak)
3091
        {
3092
            object ownerObj = SPPIDUtil.FindObjectByUID(document, endBreak.OWNER);
3093
            object connectedItem = SPPIDUtil.FindObjectByUID(document, endBreak.PROPERTIES.Find(x => x.ATTRIBUTE == "Connected Item").VALUE);
3094

    
3095
            LMConnector targetLMConnector = FindBreakLineTarget(ownerObj, connectedItem);
3096
            if (ownerObj.GetType() == typeof(Symbol) && connectedItem.GetType() == typeof(Symbol) && targetLMConnector != null)
3097
                targetLMConnector = ReModelingZeroLengthLMConnectorForSegment(targetLMConnector);
3098

    
3099
            if (targetLMConnector != null)
3100
            {
3101
                // LEADER Line 검사
3102
                bool leaderLine = false;
3103
                SymbolMapping symbolMapping = document.SymbolMappings.Find(x => x.UID == endBreak.DBUID);
3104
                if (symbolMapping != null)
3105
                    leaderLine = symbolMapping.LEADERLINE;
3106

    
3107
                SegmentLocation location;
3108
                double[] point = GetSegmentPoint(ownerObj, connectedItem, targetLMConnector, out location);
3109
                Array array = null;
3110
                if (point != null)
3111
                    array = new double[] { 0, point[0], point[1] };
3112
                else
3113
                    array = new double[] { 0, endBreak.SPPID.ORIGINAL_X, endBreak.SPPID.ORIGINAL_Y };
3114
                LMLabelPersist _LmLabelPersist = _placement.PIDPlaceLabel(endBreak.SPPID.MAPPINGNAME, ref array, LabeledItem: targetLMConnector.AsLMRepresentation(), IsLeaderVisible: leaderLine);
3115
                if (_LmLabelPersist != null)
3116
                {
3117
                    _LmLabelPersist.Commit();
3118
                    endBreak.SPPID.RepresentationId = _LmLabelPersist.AsLMRepresentation().Id;
3119
                    if (_LmLabelPersist.ModelItemObject != null)
3120
                        endBreak.SPPID.ModelItemID = _LmLabelPersist.ModelItemID;
3121
                    endBreak.SPPID.GraphicOID = _LmLabelPersist.get_GraphicOID().ToString();
3122

    
3123
                    MoveDependencyObject(endBreak.SPPID.GraphicOID, location);
3124

    
3125
                    ReleaseCOMObjects(_LmLabelPersist);
3126
                }
3127
                ReleaseCOMObjects(targetLMConnector);
3128
            }
3129
            else
3130
            {
3131
                Log.Write("End Break UID : " + endBreak.UID);
3132
                Log.Write("Can't find targetLMConnector");
3133
            }
3134
        }
3135

    
3136
        private void MoveDependencyObject(string graphicOID, SegmentLocation location)
3137
        {
3138
            double x = 0, y = 0;
3139
            if (location.HasFlag(SegmentLocation.Up))
3140
                y = GridSetting.GetInstance().Length * 3;
3141
            else if (location.HasFlag(SegmentLocation.Down))
3142
                y = -GridSetting.GetInstance().Length * 3;
3143

    
3144
            if (location.HasFlag(SegmentLocation.Right))
3145
                x = GridSetting.GetInstance().Length * 3;
3146
            else if (location.HasFlag(SegmentLocation.Left))
3147
                x = -GridSetting.GetInstance().Length * 3;
3148

    
3149
            if (x != 0 || y != 0)
3150
            {
3151
                radApp.ActiveSelectSet.RemoveAll();
3152
                DependencyObject dependency = radApp.ActiveDocument.ActiveSheet.DrawingObjects[graphicOID] as DependencyObject;
3153
                if (dependency != null)
3154
                {
3155
                    radApp.ActiveSelectSet.Add(dependency);
3156
                    Ingr.RAD2D.Transform transform = dependency.GetTransform();
3157
                    transform.DefineByMove2d(x, y);
3158
                    radApp.ActiveSelectSet.Transform(transform, true);
3159
                    radApp.ActiveSelectSet.RemoveAll();
3160
                }
3161
            }
3162
        }
3163

    
3164
        private LMConnector ReModelingZeroLengthLMConnectorForSegment(LMConnector connector, string changeSymbolPath = null)
3165
        {
3166
            string symbolPath = string.Empty;
3167
            #region get symbol path
3168
            if (string.IsNullOrEmpty(changeSymbolPath))
3169
            {
3170
                LMModelItem modelItem = dataSource.GetModelItem(connector.ModelItemID);
3171
                symbolPath = GetSPPIDFileName(modelItem);
3172
                ReleaseCOMObjects(modelItem);
3173
            }
3174
            else
3175
                symbolPath = changeSymbolPath;
3176

    
3177
            #endregion
3178

    
3179
            LMConnector newConnector = null;
3180
            dynamic OID = connector.get_GraphicOID().ToString();
3181
            DependencyObject drawingObject = radApp.ActiveDocument.ActiveSheet.DrawingObjects[OID];
3182
            Ingr.RAD2D.LineStringGeometry2d lineStringGeometry = drawingObject.GetGeometry() as Ingr.RAD2D.LineStringGeometry2d;
3183
            int verticesCount = lineStringGeometry.VertexCount;
3184
            PlaceRunInputs placeRunInputs = new PlaceRunInputs();
3185
            _LMAItem _LMAItem = _placement.PIDCreateItem(symbolPath);
3186

    
3187
            if (Convert.ToBoolean(connector.get_IsZeroLength()))
3188
            {
3189
                double[] vertices = null;
3190
                lineStringGeometry.GetVertices(ref verticesCount, ref vertices);
3191
                double x = 0;
3192
                double y = 0;
3193
                lineStringGeometry.GetVertex(1, ref x, ref y);
3194

    
3195
                string flowDirection = string.Empty;
3196
                LMAAttribute flowAttribute = connector.ModelItemObject.Attributes["FlowDirection"];
3197
                if (flowAttribute != null && !DBNull.Value.Equals(flowAttribute.get_Value()))
3198
                    flowDirection = flowAttribute.get_Value().ToString();
3199

    
3200
                if (flowDirection == "End 1 is downstream (Outlet)")
3201
                {
3202
                    placeRunInputs.AddSymbolTarget(connector.ConnectItem2SymbolObject, x, y);
3203
                    placeRunInputs.AddSymbolTarget(connector.ConnectItem1SymbolObject, x, y);
3204
                    flowDirection = "End 1 is upstream (Inlet)";
3205
                }
3206
                else
3207
                {
3208
                    placeRunInputs.AddSymbolTarget(connector.ConnectItem1SymbolObject, x, y);
3209
                    placeRunInputs.AddSymbolTarget(connector.ConnectItem2SymbolObject, x, y);
3210
                }
3211
                string oldModelItemId = connector.ModelItemID;
3212
                _placement.PIDRemovePlacement(connector.AsLMRepresentation());
3213
                newConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
3214
                newConnector.Commit();
3215
                ZeroLengthSymbolToSymbolModelItemID.Add(newConnector.ModelItemID);
3216
                if (!string.IsNullOrEmpty(flowDirection))
3217
                    newConnector.ModelItemObject.Attributes["FlowDirection"].set_Value(flowDirection);
3218
                ReleaseCOMObjects(connector);
3219

    
3220
                foreach (var line in document.LINES.FindAll(z => z.SPPID.ModelItemId == oldModelItemId))
3221
                {
3222
                    foreach (var repId in line.SPPID.Representations)
3223
                    {
3224
                        LMConnector _connector = dataSource.GetConnector(repId);
3225
                        if (_connector != null && _connector.get_ItemStatus() == "Active")
3226
                        {
3227
                            if (line.SPPID.ModelItemId != _connector.ModelItemID)
3228
                            {
3229
                                line.SPPID.ModelItemId = _connector.ModelItemID;
3230
                                line.SPPID.Representations = GetRepresentations(line.SPPID.ModelItemId);
3231
                            }
3232
                        }
3233
                        ReleaseCOMObjects(_connector);
3234
                        _connector = null;
3235
                    }
3236
                }
3237
            }
3238

    
3239
            return newConnector;
3240
        }
3241

    
3242
        /// <summary>
3243
        /// SpecBreak Modeling 메서드
3244
        /// </summary>
3245
        /// <param name="specBreak"></param>
3246
        private void SpecBreakModeling(SpecBreak specBreak)
3247
        {
3248
            object upStreamObj = SPPIDUtil.FindObjectByUID(document, specBreak.UpStreamUID);
3249
            object downStreamObj = SPPIDUtil.FindObjectByUID(document, specBreak.DownStreamUID);
3250

    
3251
            if (upStreamObj != null &&
3252
                downStreamObj != null)
3253
            {
3254
                LMConnector targetLMConnector = FindBreakLineTarget(upStreamObj, downStreamObj);
3255
                if (upStreamObj.GetType() == typeof(Symbol) && downStreamObj.GetType() == typeof(Symbol) &&
3256
                    targetLMConnector != null &&
3257
                    !IsModelingEndBreak(upStreamObj as Symbol, downStreamObj as Symbol))
3258
                    targetLMConnector = ReModelingZeroLengthLMConnectorForSegment(targetLMConnector);
3259

    
3260
                if (targetLMConnector != null)
3261
                {
3262
                    foreach (var attribute in specBreak.ATTRIBUTES)
3263
                    {
3264
                        AttributeMapping mapping = document.AttributeMappings.Find(x => x.UID == attribute.UID);
3265
                        if (mapping != null && !string.IsNullOrEmpty(mapping.SPPIDSYMBOLNAME) && mapping.SPPIDSYMBOLNAME != "None")
3266
                        {
3267
                            string MappingPath = mapping.SPPIDSYMBOLNAME;
3268
                            SegmentLocation location;
3269
                            double[] point = GetSegmentPoint(upStreamObj, downStreamObj, targetLMConnector, out location);
3270
                            Array array = null;
3271
                            if (point != null)
3272
                                array = new double[] { 0, point[0], point[1] };
3273
                            else
3274
                                array = new double[] { 0, specBreak.SPPID.ORIGINAL_X, specBreak.SPPID.ORIGINAL_Y };
3275
                            LMLabelPersist _LmLabelPersist = _placement.PIDPlaceLabel(MappingPath, ref array, LabeledItem: targetLMConnector.AsLMRepresentation(), IsLeaderVisible: mapping.LeaderLine);
3276

    
3277
                            if (_LmLabelPersist != null)
3278
                            {
3279
                                _LmLabelPersist.Commit();
3280
                                specBreak.SPPID.RepresentationId = _LmLabelPersist.AsLMRepresentation().Id;
3281
                                if (_LmLabelPersist.ModelItemObject != null)
3282
                                    specBreak.SPPID.ModelItemID = _LmLabelPersist.ModelItemID;
3283
                                specBreak.SPPID.GraphicOID = _LmLabelPersist.get_GraphicOID().ToString();
3284

    
3285
                                MoveDependencyObject(specBreak.SPPID.GraphicOID, location);
3286

    
3287
                                ReleaseCOMObjects(_LmLabelPersist);
3288
                            }
3289
                        }
3290
                    }
3291

    
3292
                    Property property = specBreak.PROPERTIES.Find(loop => loop.ATTRIBUTE == "Show");
3293
                    if (property != null && !string.IsNullOrEmpty(property.VALUE) && property.VALUE.Equals("True"))
3294
                    {
3295
                        // temp
3296
                        ReleaseCOMObjects(_placement.PIDPlaceSymbol(@"\Design\Annotation\Graphics\Break.sym", specBreak.SPPID.ORIGINAL_X, specBreak.SPPID.ORIGINAL_Y, Rotation: specBreak.ANGLE));
3297
                    }
3298
                    ReleaseCOMObjects(targetLMConnector);
3299
                }
3300
                else
3301
                {
3302
                    Log.Write("Spec Break UID : " + specBreak.UID);
3303
                    Log.Write("Can't find targetLMConnector");
3304
                }
3305
            }
3306
        }
3307

    
3308
        private LMConnector FindBreakLineTarget(object targetObj, object connectedObj)
3309
        {
3310
            LMConnector targetConnector = null;
3311
            Symbol targetSymbol = targetObj as Symbol;
3312
            Symbol connectedSymbol = connectedObj as Symbol;
3313
            Line targetLine = targetObj as Line;
3314
            Line connectedLine = connectedObj as Line;
3315
            if (targetSymbol != null && connectedSymbol != null)
3316
            {
3317
                LMSymbol targetLMSymbol = dataSource.GetSymbol(targetSymbol.SPPID.RepresentationId);
3318
                LMSymbol connectedLMSymbol = dataSource.GetSymbol(connectedSymbol.SPPID.RepresentationId);
3319

    
3320
                foreach (LMConnector connector in targetLMSymbol.Avoid1Connectors)
3321
                {
3322
                    if (connector.get_ItemStatus() != "Active")
3323
                        continue;
3324

    
3325
                    if (connector.ConnectItem1SymbolObject.Id == connectedLMSymbol.Id)
3326
                    {
3327
                        targetConnector = connector;
3328
                        break;
3329
                    }
3330
                    else if (connector.ConnectItem2SymbolObject.Id == connectedLMSymbol.Id)
3331
                    {
3332
                        targetConnector = connector;
3333
                        break;
3334
                    }
3335
                }
3336

    
3337
                foreach (LMConnector connector in targetLMSymbol.Avoid2Connectors)
3338
                {
3339
                    if (connector.get_ItemStatus() != "Active")
3340
                        continue;
3341

    
3342
                    if (connector.ConnectItem1SymbolObject.Id == connectedLMSymbol.Id)
3343
                    {
3344
                        targetConnector = connector;
3345
                        break;
3346
                    }
3347
                    else if (connector.ConnectItem2SymbolObject.Id == connectedLMSymbol.Id)
3348
                    {
3349
                        targetConnector = connector;
3350
                        break;
3351
                    }
3352
                }
3353

    
3354
                ReleaseCOMObjects(targetLMSymbol);
3355
                ReleaseCOMObjects(connectedLMSymbol);
3356
            }
3357
            else if (targetLine != null && connectedLine != null)
3358
            {
3359
                LMModelItem targetModelItem = dataSource.GetModelItem(targetLine.SPPID.ModelItemId);
3360
                LMModelItem connectedModelItem = dataSource.GetModelItem(connectedLine.SPPID.ModelItemId);
3361

    
3362
                if (targetModelItem != null && targetModelItem.get_ItemStatus() == "Active" && connectedModelItem != null && connectedModelItem.get_ItemStatus() == "Active")
3363
                {
3364
                    foreach (LMRepresentation rep in targetModelItem.Representations)
3365
                    {
3366
                        if (targetConnector != null)
3367
                            break;
3368

    
3369
                        if (rep.Attributes["RepresentationType"].get_Value() == "Connector" && rep.Attributes["ItemStatus"].get_Value() == "Active")
3370
                        {
3371
                            LMConnector _LMConnector = dataSource.GetConnector(rep.Id);
3372

    
3373
                            if (IsConnected(_LMConnector, connectedModelItem))
3374
                                targetConnector = _LMConnector;
3375
                            else
3376
                                ReleaseCOMObjects(_LMConnector);
3377
                        }
3378
                    }
3379

    
3380
                    ReleaseCOMObjects(targetModelItem);
3381
                }
3382
            }
3383
            else
3384
            {
3385
                LMSymbol connectedLMSymbol = null;
3386
                if (connectedSymbol != null)
3387
                    connectedLMSymbol = dataSource.GetSymbol(connectedSymbol.SPPID.RepresentationId);
3388
                else if (targetSymbol != null)
3389
                    connectedLMSymbol = dataSource.GetSymbol(targetSymbol.SPPID.RepresentationId);
3390
                else
3391
                {
3392

    
3393
                }
3394
                LMModelItem targetModelItem = null;
3395
                if (targetLine != null)
3396
                    targetModelItem = dataSource.GetModelItem(targetLine.SPPID.ModelItemId);
3397
                else if (connectedLine != null)
3398
                    targetModelItem = dataSource.GetModelItem(connectedLine.SPPID.ModelItemId);
3399
                else
3400
                {
3401

    
3402
                }
3403
                if (connectedLMSymbol != null && targetModelItem != null)
3404
                {
3405
                    foreach (LMConnector connector in connectedLMSymbol.Avoid1Connectors)
3406
                    {
3407
                        if (connector.get_ItemStatus() != "Active")
3408
                            continue;
3409

    
3410
                        if (IsConnected(connector, targetModelItem))
3411
                        {
3412
                            targetConnector = connector;
3413
                            break;
3414
                        }
3415
                    }
3416

    
3417
                    if (targetConnector == null)
3418
                    {
3419
                        foreach (LMConnector connector in connectedLMSymbol.Avoid2Connectors)
3420
                        {
3421
                            if (connector.get_ItemStatus() != "Active")
3422
                                continue;
3423

    
3424
                            if (IsConnected(connector, targetModelItem))
3425
                            {
3426
                                targetConnector = connector;
3427
                                break;
3428
                            }
3429
                        }
3430
                    }
3431
                }
3432

    
3433
            }
3434

    
3435
            return targetConnector;
3436
        }
3437

    
3438
        private double[] GetSegmentPoint(object targetObj, object connObj, LMConnector targetConnector, out SegmentLocation location)
3439
        {
3440
            double[] result = null;
3441
            Line targetLine = targetObj as Line;
3442
            Symbol targetSymbol = targetObj as Symbol;
3443
            Line connLine = connObj as Line;
3444
            Symbol connSymbol = connObj as Symbol;
3445
            location = SegmentLocation.None;
3446
            if (Convert.ToBoolean(targetConnector.get_IsZeroLength()))
3447
            {
3448
                result = GetConnectorVertices(targetConnector)[0];
3449
                if (targetSymbol != null && connSymbol != null)
3450
                {
3451
                    SlopeType slopeType = SPPIDUtil.CalcSlope(targetSymbol.SPPID.SPPID_X, targetSymbol.SPPID.SPPID_Y, connSymbol.SPPID.SPPID_X, connSymbol.SPPID.SPPID_Y);
3452
                    result = new double[] { result[0], result[1] };
3453
                    if (slopeType == SlopeType.HORIZONTAL)
3454
                        location = SegmentLocation.Up;
3455
                    else if (slopeType == SlopeType.VERTICAL)
3456
                        location = SegmentLocation.Right;
3457
                }
3458
                else if (targetLine != null)
3459
                {
3460
                    result = new double[] { result[0], result[1] };
3461
                    if (targetLine.SlopeType == SlopeType.HORIZONTAL)
3462
                        location = SegmentLocation.Up;
3463
                    else if (targetLine.SlopeType == SlopeType.VERTICAL)
3464
                        location = SegmentLocation.Right;
3465
                }
3466
                else if (connLine != null)
3467
                {
3468
                    result = new double[] { result[0], result[1] };
3469
                    if (connLine.SlopeType == SlopeType.HORIZONTAL)
3470
                        location = SegmentLocation.Up;
3471
                    else if (connLine.SlopeType == SlopeType.VERTICAL)
3472
                        location = SegmentLocation.Right;
3473
                }
3474
            }
3475
            else
3476
            {
3477
                if (targetObj.GetType() == typeof(Line) && connObj.GetType() == typeof(Line))
3478
                {
3479
                    Line line = connObj as Line;
3480
                    LMConnector connectedConnector = null;
3481
                    int connIndex = 0;
3482
                    LMModelItem modelItem = dataSource.GetModelItem(line.SPPID.ModelItemId);
3483
                    FindConnectedConnector(targetConnector, modelItem, ref connectedConnector, ref connIndex);
3484

    
3485
                    List<double[]> vertices = GetConnectorVertices(targetConnector);
3486

    
3487
                    ReleaseCOMObjects(modelItem);
3488
                    ReleaseCOMObjects(connectedConnector);
3489

    
3490
                    if (vertices.Count > 0)
3491
                    {
3492
                        if (connIndex == 1)
3493
                            result = vertices[0];
3494
                        else if (connIndex == 2)
3495
                            result = vertices[vertices.Count - 1];
3496

    
3497
                        if (targetLine.SlopeType == SlopeType.HORIZONTAL)
3498
                        {
3499
                            result = new double[] { result[0], result[1] };
3500
                            location = SegmentLocation.Up;
3501
                            if (targetLine.CONNECTORS[0].CONNECTEDITEM == connLine.UID && targetLine.SPPID.START_X < targetLine.SPPID.END_X)
3502
                                location = location | SegmentLocation.Right;
3503
                            else if (targetLine.CONNECTORS[0].CONNECTEDITEM == connLine.UID && targetLine.SPPID.START_X > targetLine.SPPID.END_X)
3504
                                location = location | SegmentLocation.Left;
3505
                            else if (targetLine.CONNECTORS[1].CONNECTEDITEM == connLine.UID && targetLine.SPPID.START_X < targetLine.SPPID.END_X)
3506
                                location = location | SegmentLocation.Left;
3507
                            else if (targetLine.CONNECTORS[1].CONNECTEDITEM == connLine.UID && targetLine.SPPID.START_X > targetLine.SPPID.END_X)
3508
                                location = location | SegmentLocation.Right;
3509
                        }
3510
                        else if (targetLine.SlopeType == SlopeType.VERTICAL)
3511
                        {
3512
                            result = new double[] { result[0], result[1] };
3513
                            location = SegmentLocation.Right;
3514
                            if (targetLine.CONNECTORS[0].CONNECTEDITEM == connLine.UID && targetLine.SPPID.START_Y < targetLine.SPPID.END_Y)
3515
                                location = location | SegmentLocation.Up;
3516
                            else if (targetLine.CONNECTORS[0].CONNECTEDITEM == connLine.UID && targetLine.SPPID.START_Y > targetLine.SPPID.END_Y)
3517
                                location = location | SegmentLocation.Down;
3518
                            else if (targetLine.CONNECTORS[1].CONNECTEDITEM == connLine.UID && targetLine.SPPID.START_Y < targetLine.SPPID.END_Y)
3519
                                location = location | SegmentLocation.Down;
3520
                            else if (targetLine.CONNECTORS[1].CONNECTEDITEM == connLine.UID && targetLine.SPPID.START_Y > targetLine.SPPID.END_Y)
3521
                                location = location | SegmentLocation.Up;
3522
                        }
3523

    
3524
                    }
3525
                }
3526
                else
3527
                {
3528
                    Log.Write("error in GetSegemtPoint");
3529
                }
3530
            }
3531

    
3532
            return result;
3533
        }
3534

    
3535
        private bool IsConnected(LMConnector connector, LMModelItem modelItem)
3536
        {
3537
            bool result = false;
3538

    
3539
            foreach (LMRepresentation rep in modelItem.Representations)
3540
            {
3541
                if (result)
3542
                    break;
3543

    
3544
                if (rep.Attributes["RepresentationType"].get_Value() == "Connector" && rep.Attributes["ItemStatus"].get_Value() == "Active")
3545
                {
3546
                    LMConnector _LMConnector = dataSource.GetConnector(rep.Id);
3547

    
3548
                    if (_LMConnector.ConnectItem1SymbolObject != null &&
3549
                        connector.ConnectItem1SymbolObject != null &&
3550
                        _LMConnector.ConnectItem1SymbolObject.Id == connector.ConnectItem1SymbolObject.Id)
3551
                    {
3552
                        result = true;
3553
                        ReleaseCOMObjects(_LMConnector);
3554
                        break;
3555
                    }
3556
                    else if (_LMConnector.ConnectItem1SymbolObject != null &&
3557
                        connector.ConnectItem2SymbolObject != null &&
3558
                        _LMConnector.ConnectItem1SymbolObject.Id == connector.ConnectItem2SymbolObject.Id)
3559
                    {
3560
                        result = true;
3561
                        ReleaseCOMObjects(_LMConnector);
3562
                        break;
3563
                    }
3564
                    else if (_LMConnector.ConnectItem2SymbolObject != null &&
3565
                        connector.ConnectItem1SymbolObject != null &&
3566
                        _LMConnector.ConnectItem2SymbolObject.Id == connector.ConnectItem1SymbolObject.Id)
3567
                    {
3568
                        result = true;
3569
                        ReleaseCOMObjects(_LMConnector);
3570
                        break;
3571
                    }
3572
                    else if (_LMConnector.ConnectItem2SymbolObject != null &&
3573
                        connector.ConnectItem2SymbolObject != null &&
3574
                        _LMConnector.ConnectItem2SymbolObject.Id == connector.ConnectItem2SymbolObject.Id)
3575
                    {
3576
                        result = true;
3577
                        ReleaseCOMObjects(_LMConnector);
3578
                        break;
3579
                    }
3580

    
3581
                    ReleaseCOMObjects(_LMConnector);
3582
                }
3583
            }
3584

    
3585

    
3586
            return result;
3587
        }
3588

    
3589
        private void FindConnectedConnector(LMConnector connector, LMModelItem modelItem, ref LMConnector connectedConnector, ref int connectorIndex)
3590
        {
3591
            foreach (LMRepresentation rep in modelItem.Representations)
3592
            {
3593
                if (connectedConnector != null)
3594
                    break;
3595

    
3596
                if (rep.Attributes["RepresentationType"].get_Value() == "Connector" && rep.Attributes["ItemStatus"].get_Value() == "Active")
3597
                {
3598
                    LMConnector _LMConnector = dataSource.GetConnector(rep.Id);
3599

    
3600
                    if (_LMConnector.ConnectItem1SymbolObject != null &&
3601
                        connector.ConnectItem1SymbolObject != null &&
3602
                        _LMConnector.ConnectItem1SymbolObject.Id == connector.ConnectItem1SymbolObject.Id)
3603
                    {
3604
                        connectedConnector = _LMConnector;
3605
                        connectorIndex = 1;
3606
                        break;
3607
                    }
3608
                    else if (_LMConnector.ConnectItem1SymbolObject != null &&
3609
                        connector.ConnectItem2SymbolObject != null &&
3610
                        _LMConnector.ConnectItem1SymbolObject.Id == connector.ConnectItem2SymbolObject.Id)
3611
                    {
3612
                        connectedConnector = _LMConnector;
3613
                        connectorIndex = 2;
3614
                        break;
3615
                    }
3616
                    else if (_LMConnector.ConnectItem2SymbolObject != null &&
3617
                        connector.ConnectItem1SymbolObject != null &&
3618
                        _LMConnector.ConnectItem2SymbolObject.Id == connector.ConnectItem1SymbolObject.Id)
3619
                    {
3620
                        connectedConnector = _LMConnector;
3621
                        connectorIndex = 1;
3622
                        break;
3623
                    }
3624
                    else if (_LMConnector.ConnectItem2SymbolObject != null &&
3625
                        connector.ConnectItem2SymbolObject != null &&
3626
                        _LMConnector.ConnectItem2SymbolObject.Id == connector.ConnectItem2SymbolObject.Id)
3627
                    {
3628
                        connectedConnector = _LMConnector;
3629
                        connectorIndex = 2;
3630
                        break;
3631
                    }
3632

    
3633
                    if (connectedConnector == null)
3634
                        ReleaseCOMObjects(_LMConnector);
3635
                }
3636
            }
3637
        }
3638

    
3639
        /// <summary>
3640
        /// FromModelItem을 ToModelItem으로 PipeRunJoin하는 메서드
3641
        /// </summary>
3642
        /// <param name="modelItemID1"></param>
3643
        /// <param name="modelItemID2"></param>
3644
        private void JoinRun(string modelId1, string modelId2, ref string survivorId, bool IsSameConnector = true)
3645
        {
3646
            try
3647
            {
3648
                LMModelItem modelItem1 = dataSource.GetModelItem(modelId1);
3649
                LMConnector connector1 = GetLMConnectorFirst(modelId1);
3650
                List<double[]> vertices1 = null;
3651
                string graphicOID1 = string.Empty;
3652
                if (connector1 != null)
3653
                {
3654
                    vertices1 = GetConnectorVertices(connector1);
3655
                    graphicOID1 = connector1.get_GraphicOID();
3656
                }
3657
                _LMAItem item1 = modelItem1.AsLMAItem();
3658
                ReleaseCOMObjects(connector1);
3659
                connector1 = null;
3660

    
3661
                LMModelItem modelItem2 = dataSource.GetModelItem(modelId2);
3662
                LMConnector connector2 = GetLMConnectorFirst(modelId2);
3663
                List<double[]> vertices2 = null;
3664
                string graphicOID2 = string.Empty;
3665
                if (connector2 != null)
3666
                {
3667
                    vertices2 = GetConnectorVertices(connector2);
3668
                    graphicOID2 = connector2.get_GraphicOID();
3669
                }
3670
                _LMAItem item2 = modelItem2.AsLMAItem();
3671
                ReleaseCOMObjects(connector2);
3672
                connector2 = null;
3673

    
3674
                // item2가 item1으로 조인
3675
                _placement.PIDJoinRuns(ref item1, ref item2);
3676
                item1.Commit();
3677
                item2.Commit();
3678

    
3679
                string beforeID = string.Empty;
3680
                string afterID = string.Empty;
3681

    
3682
                if (modelItem1.get_ItemStatus() == "Active" && modelItem2.get_ItemStatus() != "Active")
3683
                {
3684
                    beforeID = modelItem2.Id;
3685
                    afterID = modelItem1.Id;
3686
                    survivorId = afterID;
3687
                }
3688
                else if (modelItem1.get_ItemStatus() != "Active" && modelItem2.get_ItemStatus() == "Active")
3689
                {
3690
                    beforeID = modelItem1.Id;
3691
                    afterID = modelItem2.Id;
3692
                    survivorId = afterID;
3693
                }
3694
                else if (modelItem1.get_ItemStatus() == "Active" && modelItem2.get_ItemStatus() == "Active")
3695
                {
3696
                    int model1Cnt = GetConnectorCount(modelId1);
3697
                    int model2Cnt = GetConnectorCount(modelId2);
3698
                    if (model1Cnt == 0)
3699
                    {
3700
                        beforeID = modelItem1.Id;
3701
                        afterID = modelItem2.Id;
3702
                        survivorId = afterID;
3703
                    }
3704
                    else if (model2Cnt == 0)
3705
                    {
3706
                        beforeID = modelItem2.Id;
3707
                        afterID = modelItem1.Id;
3708
                        survivorId = afterID;
3709
                    }
3710
                    else
3711
                        survivorId = null;
3712
                }
3713
                else
3714
                {
3715
                    Log.Write("잘못된 경우");
3716
                    survivorId = null;
3717
                }
3718

    
3719
                if (!string.IsNullOrEmpty(beforeID) && !string.IsNullOrEmpty(afterID))
3720
                {
3721
                    List<Line> lines = SPPIDUtil.FindLinesByModelId(document, beforeID);
3722
                    foreach (var line in lines)
3723
                        line.SPPID.ModelItemId = afterID;
3724
                }
3725

    
3726
                ReleaseCOMObjects(modelItem1);
3727
                ReleaseCOMObjects(item1);
3728
                ReleaseCOMObjects(modelItem2);
3729
                ReleaseCOMObjects(item2);
3730
            }
3731
            catch (Exception ex)
3732
            {
3733
                Log.Write("Join Error");
3734
                Log.Write(ex.Message + "\r\n" + ex.StackTrace);
3735
            }
3736
        }
3737

    
3738
        private bool IsModelingEndBreak(Symbol symbol1, Symbol symbol2)
3739
        {
3740
            bool result = false;
3741
            List<EndBreak> endBreaks = document.EndBreaks.FindAll(x =>
3742
           (x.OWNER == symbol1.UID || x.OWNER == symbol2.UID) &&
3743
           (x.PROPERTIES.Find(y => y.ATTRIBUTE == "Connected Item").VALUE == symbol1.UID || x.PROPERTIES.Find(y => y.ATTRIBUTE == "Connected Item").VALUE == symbol2.UID));
3744

    
3745
            foreach (var item in endBreaks)
3746
            {
3747
                if (!string.IsNullOrEmpty(item.SPPID.RepresentationId))
3748
                {
3749
                    result = true;
3750
                    break;
3751
                }
3752
            }
3753

    
3754
            return result;
3755
        }
3756
        private List<string> FindOtherModelItemBySymbolWhereTypePipeRun(LMSymbol symbol, string modelId)
3757
        {
3758
            List<string> temp = new List<string>();
3759
            List<LMConnector> connectors = new List<LMConnector>();
3760
            foreach (LMConnector connector in symbol.Avoid1Connectors)
3761
            {
3762
                if (connector.get_ItemStatus() != "Active")
3763
                    continue;
3764

    
3765
                LMModelItem modelItem = connector.ModelItemObject;
3766
                LMSymbol connOtherSymbol = FindOtherConnectedSymbol(connector);
3767
                if (modelItem.get_ItemStatus() == "Active" && modelItem.get_ItemTypeName().ToString() == "PipeRun" && modelItem.Id != modelId && !temp.Contains(modelItem.Id))
3768
                    temp.Add(modelItem.Id);
3769

    
3770
                if (temp.Contains(modelItem.Id) &&
3771
                    connOtherSymbol != null &&
3772
                    connOtherSymbol.get_RepresentationType() == "Branch" &&
3773
                    Convert.ToBoolean(connector.get_IsZeroLength()))
3774
                    temp.Remove(modelItem.Id);
3775

    
3776

    
3777
                if (temp.Contains(modelItem.Id))
3778
                    connectors.Add(connector);
3779
                ReleaseCOMObjects(connOtherSymbol);
3780
                connOtherSymbol = null;
3781
                ReleaseCOMObjects(modelItem);
3782
                modelItem = null;
3783
            }
3784

    
3785
            foreach (LMConnector connector in symbol.Avoid2Connectors)
3786
            {
3787
                if (connector.get_ItemStatus() != "Active")
3788
                    continue;
3789

    
3790
                LMModelItem modelItem = connector.ModelItemObject;
3791
                LMSymbol connOtherSymbol = FindOtherConnectedSymbol(connector);
3792
                if (modelItem.get_ItemStatus() == "Active" && modelItem.get_ItemTypeName().ToString() == "PipeRun" && modelItem.Id != modelId && !temp.Contains(modelItem.Id))
3793
                    temp.Add(modelItem.Id);
3794

    
3795
                if (temp.Contains(modelItem.Id) &&
3796
                    connOtherSymbol != null &&
3797
                    connOtherSymbol.get_RepresentationType() == "Branch" &&
3798
                    Convert.ToBoolean(connector.get_IsZeroLength()))
3799
                    temp.Remove(modelItem.Id);
3800

    
3801
                if (temp.Contains(modelItem.Id))
3802
                    connectors.Add(connector);
3803
                ReleaseCOMObjects(connOtherSymbol);
3804
                connOtherSymbol = null;
3805
                ReleaseCOMObjects(modelItem);
3806
                modelItem = null;
3807
            }
3808

    
3809

    
3810
            List<string> result = new List<string>();
3811
            string originalName = GetSPPIDFileName(modelId);
3812
            foreach (var connector in connectors)
3813
            {
3814
                string fileName = GetSPPIDFileName(connector.ModelItemID);
3815
                if (originalName == fileName)
3816
                    result.Add(connector.ModelItemID);
3817
                else
3818
                {
3819
                    if (document.LINES.Find(x => x.SPPID.ModelItemId == connector.ModelItemID) == null && Convert.ToBoolean(connector.get_IsZeroLength()))
3820
                        result.Add(connector.ModelItemID);
3821
                    else
3822
                    {
3823
                        Line line1 = document.LINES.Find(x => x.SPPID.ModelItemId == modelId);
3824
                        Line line2 = document.LINES.Find(x => x.SPPID.ModelItemId == connector.ModelItemID.ToString());
3825
                        if (line1 != null && line2 != null && line1.TYPE == line2.TYPE)
3826
                            result.Add(connector.ModelItemID);
3827
                    }
3828
                }
3829
            }
3830

    
3831
            foreach (var connector in connectors)
3832
                ReleaseCOMObjects(connector);
3833

    
3834
            return result;
3835

    
3836

    
3837
            LMSymbol FindOtherConnectedSymbol(LMConnector connector)
3838
            {
3839
                LMSymbol findResult = null;
3840
                if (connector.ConnectItem1SymbolObject != null && connector.ConnectItem1SymbolObject.Id != symbol.Id && connector.ConnectItem1SymbolObject.get_ItemStatus() == "Active")
3841
                    findResult = connector.ConnectItem1SymbolObject;
3842
                else if (connector.ConnectItem2SymbolObject != null && connector.ConnectItem2SymbolObject.Id != symbol.Id && connector.ConnectItem2SymbolObject.get_ItemStatus() == "Active")
3843
                    findResult = connector.ConnectItem2SymbolObject;
3844

    
3845
                return findResult;
3846
            }
3847
        }
3848

    
3849
        /// <summary>
3850
        /// PipeRun의 좌표를 가져오는 메서드
3851
        /// </summary>
3852
        /// <param name="modelId"></param>
3853
        /// <returns></returns>
3854
        private Dictionary<LMConnector, List<double[]>> GetPipeRunVertices(string modelId, bool ContainZeroLength = true)
3855
        {
3856
            Dictionary<LMConnector, List<double[]>> connectorVertices = new Dictionary<LMConnector, List<double[]>>();
3857
            LMModelItem modelItem = dataSource.GetModelItem(modelId);
3858

    
3859
            if (modelItem != null)
3860
            {
3861
                foreach (LMRepresentation rep in modelItem.Representations)
3862
                {
3863
                    if (rep.Attributes["RepresentationType"].get_Value() == "Connector" && rep.Attributes["ItemStatus"].get_Value() == "Active")
3864
                    {
3865
                        LMConnector _LMConnector = dataSource.GetConnector(rep.Id);
3866
                        if (!ContainZeroLength && Convert.ToBoolean(_LMConnector.get_IsZeroLength()))
3867
                        {
3868
                            ReleaseCOMObjects(_LMConnector);
3869
                            _LMConnector = null;
3870
                            continue;
3871
                        }
3872
                        connectorVertices.Add(_LMConnector, new List<double[]>());
3873
                        dynamic OID = rep.get_GraphicOID().ToString();
3874
                        DependencyObject drawingObject = radApp.ActiveDocument.ActiveSheet.DrawingObjects[OID];
3875
                        Ingr.RAD2D.LineStringGeometry2d lineStringGeometry = drawingObject.GetGeometry() as Ingr.RAD2D.LineStringGeometry2d;
3876
                        int verticesCount = lineStringGeometry.VertexCount;
3877
                        double[] vertices = null;
3878
                        lineStringGeometry.GetVertices(ref verticesCount, ref vertices);
3879
                        for (int i = 0; i < verticesCount; i++)
3880
                        {
3881
                            double x = 0;
3882
                            double y = 0;
3883
                            lineStringGeometry.GetVertex(i + 1, ref x, ref y);
3884
                            connectorVertices[_LMConnector].Add(new double[] { x, y });
3885
                        }
3886
                    }
3887
                }
3888

    
3889
                ReleaseCOMObjects(modelItem);
3890
            }
3891

    
3892
            return connectorVertices;
3893
        }
3894

    
3895
        private List<double[]> GetConnectorVertices(LMConnector connector)
3896
        {
3897
            List<double[]> vertices = new List<double[]>();
3898
            if (connector != null)
3899
            {
3900
                dynamic OID = connector.get_GraphicOID().ToString();
3901
                DependencyObject drawingObject = radApp.ActiveDocument.ActiveSheet.DrawingObjects[OID];
3902
                Ingr.RAD2D.LineStringGeometry2d lineStringGeometry = drawingObject.GetGeometry() as Ingr.RAD2D.LineStringGeometry2d;
3903
                int verticesCount = lineStringGeometry.VertexCount;
3904
                double[] value = null;
3905
                lineStringGeometry.GetVertices(ref verticesCount, ref value);
3906
                for (int i = 0; i < verticesCount; i++)
3907
                {
3908
                    double x = 0;
3909
                    double y = 0;
3910
                    lineStringGeometry.GetVertex(i + 1, ref x, ref y);
3911
                    vertices.Add(new double[] { x, y });
3912
                }
3913
            }
3914
            return vertices;
3915
        }
3916

    
3917
        private double GetConnectorDistance(LMConnector connector)
3918
        {
3919
            double result = 0;
3920
            List<double[]> vertices = new List<double[]>();
3921
            if (connector != null)
3922
            {
3923
                dynamic OID = connector.get_GraphicOID().ToString();
3924
                DependencyObject drawingObject = radApp.ActiveDocument.ActiveSheet.DrawingObjects[OID];
3925
                Ingr.RAD2D.LineStringGeometry2d lineStringGeometry = drawingObject.GetGeometry() as Ingr.RAD2D.LineStringGeometry2d;
3926
                int verticesCount = lineStringGeometry.VertexCount;
3927
                double[] value = null;
3928
                lineStringGeometry.GetVertices(ref verticesCount, ref value);
3929
                for (int i = 0; i < verticesCount; i++)
3930
                {
3931
                    double x = 0;
3932
                    double y = 0;
3933
                    lineStringGeometry.GetVertex(i + 1, ref x, ref y);
3934
                    vertices.Add(new double[] { x, y });
3935
                    if (vertices.Count > 1)
3936
                    {
3937
                        result += SPPIDUtil.CalcPointToPointdDistance(vertices[vertices.Count - 2][0], vertices[vertices.Count - 2][1], x, y);
3938
                    }
3939
                }
3940
            }
3941
            return result;
3942
        }
3943
        private double[] GetConnectorRange(LMConnector connector)
3944
        {
3945
            double[] result = null;
3946
            List<double[]> vertices = new List<double[]>();
3947
            if (connector != null)
3948
            {
3949
                dynamic OID = connector.get_GraphicOID().ToString();
3950
                DependencyObject drawingObject = radApp.ActiveDocument.ActiveSheet.DrawingObjects[OID];
3951
                double minX = 0;
3952
                double minY = 0;
3953
                double maxX = 0;
3954
                double maxY = 0;
3955

    
3956
                drawingObject.Range(out minX, out minY, out maxX, out maxY);
3957
                result = new double[] { minX, minY, maxX, maxY };
3958
            }
3959
            return result;
3960
        }
3961
        private List<double[]> GetConnectorVertices(dynamic graphicOID)
3962
        {
3963
            List<double[]> vertices = null;
3964
            DependencyObject drawingObject = radApp.ActiveDocument.ActiveSheet.DrawingObjects[graphicOID];
3965
            if (drawingObject != null)
3966
            {
3967
                vertices = new List<double[]>();
3968
                Ingr.RAD2D.LineStringGeometry2d lineStringGeometry = drawingObject.GetGeometry() as Ingr.RAD2D.LineStringGeometry2d;
3969
                int verticesCount = lineStringGeometry.VertexCount;
3970
                double[] value = null;
3971
                lineStringGeometry.GetVertices(ref verticesCount, ref value);
3972
                for (int i = 0; i < verticesCount; i++)
3973
                {
3974
                    double x = 0;
3975
                    double y = 0;
3976
                    lineStringGeometry.GetVertex(i + 1, ref x, ref y);
3977
                    vertices.Add(new double[] { x, y });
3978
                }
3979
            }
3980
            return vertices;
3981
        }
3982
        /// <summary>
3983
        /// 좌표로 PipeRun의 Connector중에 어느 Connector에 가까운지/붙을지 가져오는 메서드 - 조건에 안맞아서 못찾을시 제일 가까운 점으로 가져오는 방식
3984
        /// </summary>
3985
        /// <param name="connectorVertices"></param>
3986
        /// <param name="connX"></param>
3987
        /// <param name="connY"></param>
3988
        /// <returns></returns>
3989
        private LMConnector FindTargetLMConnectorForLabel(Dictionary<LMConnector, List<double[]>> connectorVertices, double connX, double connY)
3990
        {
3991
            double length = double.MaxValue;
3992
            LMConnector targetConnector = null;
3993
            foreach (var item in connectorVertices)
3994
            {
3995
                List<double[]> points = item.Value;
3996
                for (int i = 0; i < points.Count - 1; i++)
3997
                {
3998
                    double[] point1 = points[i];
3999
                    double[] point2 = points[i + 1];
4000
                    double x1 = Math.Min(point1[0], point2[0]);
4001
                    double y1 = Math.Min(point1[1], point2[1]);
4002
                    double x2 = Math.Max(point1[0], point2[0]);
4003
                    double y2 = Math.Max(point1[1], point2[1]);
4004

    
4005
                    if ((x1 <= connX && x2 >= connX) ||
4006
                        (y1 <= connY && y2 >= connY))
4007
                    {
4008
                        double distance = SPPIDUtil.CalcPointToPointdDistance(point1[0], point1[1], connX, connY);
4009
                        if (length >= distance)
4010
                        {
4011
                            targetConnector = item.Key;
4012
                            length = distance;
4013
                        }
4014

    
4015
                        distance = SPPIDUtil.CalcPointToPointdDistance(point2[0], point2[1], connX, connY);
4016
                        if (length >= distance)
4017
                        {
4018
                            targetConnector = item.Key;
4019
                            length = distance;
4020
                        }
4021
                    }
4022
                }
4023
            }
4024

    
4025
            // 못찾았을때.
4026
            length = double.MaxValue;
4027
            if (targetConnector == null)
4028
            {
4029
                foreach (var item in connectorVertices)
4030
                {
4031
                    List<double[]> points = item.Value;
4032

    
4033
                    foreach (double[] point in points)
4034
                    {
4035
                        double distance = SPPIDUtil.CalcPointToPointdDistance(point[0], point[1], connX, connY);
4036
                        if (length >= distance)
4037
                        {
4038
                            targetConnector = item.Key;
4039
                            length = distance;
4040
                        }
4041
                    }
4042
                }
4043
            }
4044

    
4045
            return targetConnector;
4046
        }
4047

    
4048
        private LMConnector FindTargetLMConnectorForBranch(Line line, Line targetLine, ref double x, ref double y)
4049
        {
4050
            Dictionary<LMConnector, List<double[]>> vertices = GetPipeRunVertices(targetLine.SPPID.ModelItemId);
4051
            if (vertices.Count == 0)
4052
                return null;
4053

    
4054
            double length = double.MaxValue;
4055
            LMConnector targetConnector = null;
4056
            double[] resultPoint = null;
4057
            List<double[]> targetVertices = null;
4058

    
4059
            // Vertices 포인트에 제일 가까운곳
4060
            foreach (var item in vertices)
4061
            {
4062
                List<double[]> points = item.Value;
4063
                for (int i = 0; i < points.Count; i++)
4064
                {
4065
                    double[] point = points[i];
4066
                    double tempX = point[0];
4067
                    double tempY = point[1];
4068

    
4069
                    double distance = SPPIDUtil.CalcPointToPointdDistance(tempX, tempY, x, y);
4070
                    if (length >= distance)
4071
                    {
4072
                        targetConnector = item.Key;
4073
                        length = distance;
4074
                        resultPoint = point;
4075
                        targetVertices = item.Value;
4076
                    }
4077
                }
4078
            }
4079

    
4080
            // Vertices Cross에 제일 가까운곳
4081
            foreach (var item in vertices)
4082
            {
4083
                List<double[]> points = item.Value;
4084
                for (int i = 0; i < points.Count - 1; i++)
4085
                {
4086
                    double[] point1 = points[i];
4087
                    double[] point2 = points[i + 1];
4088

    
4089
                    double maxLineX = Math.Max(point1[0], point2[0]);
4090
                    double minLineX = Math.Min(point1[0], point2[0]);
4091
                    double maxLineY = Math.Max(point1[1], point2[1]);
4092
                    double minLineY = Math.Min(point1[1], point2[1]);
4093

    
4094
                    SlopeType slope = SPPIDUtil.CalcSlope(minLineX, minLineY, maxLineX, maxLineY);
4095

    
4096
                    double[] crossingPoint = SPPIDUtil.CalcLineCrossingPoint(line.SPPID.START_X, line.SPPID.START_Y, line.SPPID.END_X, line.SPPID.END_Y, point1[0], point1[1], point2[0], point2[1]);
4097
                    if (crossingPoint != null)
4098
                    {
4099
                        double distance = SPPIDUtil.CalcPointToPointdDistance(crossingPoint[0], crossingPoint[1], x, y);
4100
                        if (length >= distance)
4101
                        {
4102
                            if (slope == SlopeType.Slope &&
4103
                                minLineX <= crossingPoint[0] && maxLineX >= crossingPoint[0] &&
4104
                                minLineY <= crossingPoint[1] && maxLineY >= crossingPoint[1])
4105
                            {
4106
                                targetConnector = item.Key;
4107
                                length = distance;
4108
                                resultPoint = crossingPoint;
4109
                                targetVertices = item.Value;
4110
                            }
4111
                            else if (slope == SlopeType.HORIZONTAL &&
4112
                                minLineX <= crossingPoint[0] && maxLineX >= crossingPoint[0])
4113
                            {
4114
                                targetConnector = item.Key;
4115
                                length = distance;
4116
                                resultPoint = crossingPoint;
4117
                                targetVertices = item.Value;
4118
                            }
4119
                            else if (slope == SlopeType.VERTICAL &&
4120
                               minLineY <= crossingPoint[1] && maxLineY >= crossingPoint[1])
4121
                            {
4122
                                targetConnector = item.Key;
4123
                                length = distance;
4124
                                resultPoint = crossingPoint;
4125
                                targetVertices = item.Value;
4126
                            }
4127
                        }
4128
                    }
4129
                }
4130
            }
4131

    
4132
            foreach (var item in vertices)
4133
                if (item.Key != null && item.Key != targetConnector)
4134
                    ReleaseCOMObjects(item.Key);
4135

    
4136
            if (SPPIDUtil.IsBranchLine(line, targetLine))
4137
            {
4138
                double tempResultX = resultPoint[0];
4139
                double tempResultY = resultPoint[1];
4140
                SPPIDUtil.ConvertGridPoint(ref tempResultX, ref tempResultY);
4141

    
4142
                GridSetting gridSetting = GridSetting.GetInstance();
4143

    
4144
                for (int i = 0; i < targetVertices.Count; i++)
4145
                {
4146
                    double[] point = targetVertices[i];
4147
                    double tempX = targetVertices[i][0];
4148
                    double tempY = targetVertices[i][1];
4149
                    SPPIDUtil.ConvertGridPoint(ref tempX, ref tempY);
4150
                    if (tempX == tempResultX && tempY == tempResultY)
4151
                    {
4152
                        if (i == 0)
4153
                        {
4154
                            LMSymbol connSymbol = targetConnector.ConnectItem1SymbolObject;
4155
                            bool containZeroLength = false;
4156
                            if (connSymbol != null)
4157
                            {
4158
                                foreach (LMConnector connector in connSymbol.Connect1Connectors)
4159
                                {
4160
                                    if (connector.get_ItemStatus() == "Active" && Convert.ToBoolean(connector.get_IsZeroLength()) == true)
4161
                                        containZeroLength = true;
4162
                                }
4163
                                foreach (LMConnector connector in connSymbol.Connect2Connectors)
4164
                                {
4165
                                    if (connector.get_ItemStatus() == "Active" && Convert.ToBoolean(connector.get_IsZeroLength()) == true)
4166
                                        containZeroLength = true;
4167
                                }
4168
                            }
4169

    
4170
                            if (connSymbol == null ||
4171
                                (connSymbol != null && connSymbol.get_ItemStatus() == "Active" && connSymbol.get_RepresentationType() != "Branch") ||
4172
                                containZeroLength)
4173
                            {
4174
                                bool bCalcX = false;
4175
                                bool bCalcY = false;
4176
                                if (targetLine.SlopeType == SlopeType.HORIZONTAL)
4177
                                    bCalcX = true;
4178
                                else if (targetLine.SlopeType == SlopeType.VERTICAL)
4179
                                    bCalcY = true;
4180
                                else
4181
                                {
4182
                                    bCalcX = true;
4183
                                    bCalcY = true;
4184
                                }
4185

    
4186
                                if (bCalcX)
4187
                                {
4188
                                    double nextX = targetVertices[i + 1][0];
4189
                                    double newX = 0;
4190
                                    if (nextX > tempX)
4191
                                    {
4192
                                        newX = tempX + gridSetting.Length;
4193
                                        if (newX > nextX)
4194
                                            newX = (point[0] + nextX) / 2;
4195
                                    }
4196
                                    else
4197
                                    {
4198
                                        newX = tempX - gridSetting.Length;
4199
                                        if (newX < nextX)
4200
                                            newX = (point[0] + nextX) / 2;
4201
                                    }
4202
                                    resultPoint = new double[] { newX, resultPoint[1] };
4203
                                }
4204

    
4205
                                if (bCalcY)
4206
                                {
4207
                                    double nextY = targetVertices[i + 1][1];
4208
                                    double newY = 0;
4209
                                    if (nextY > tempY)
4210
                                    {
4211
                                        newY = tempY + gridSetting.Length;
4212
                                        if (newY > nextY)
4213
                                            newY = (point[1] + nextY) / 2;
4214
                                    }
4215
                                    else
4216
                                    {
4217
                                        newY = tempY - gridSetting.Length;
4218
                                        if (newY < nextY)
4219
                                            newY = (point[1] + nextY) / 2;
4220
                                    }
4221
                                    resultPoint = new double[] { resultPoint[0], newY };
4222
                                }
4223
                            }
4224
                        }
4225
                        else if (i == targetVertices.Count - 1)
4226
                        {
4227
                            LMSymbol connSymbol = targetConnector.ConnectItem2SymbolObject;
4228
                            bool containZeroLength = false;
4229
                            if (connSymbol != null)
4230
                            {
4231
                                foreach (LMConnector connector in connSymbol.Connect1Connectors)
4232
                                {
4233
                                    if (connector.get_ItemStatus() == "Active" && Convert.ToBoolean(connector.get_IsZeroLength()) == true)
4234
                                        containZeroLength = true;
4235
                                }
4236
                                foreach (LMConnector connector in connSymbol.Connect2Connectors)
4237
                                {
4238
                                    if (connector.get_ItemStatus() == "Active" && Convert.ToBoolean(connector.get_IsZeroLength()) == true)
4239
                                        containZeroLength = true;
4240
                                }
4241
                            }
4242

    
4243
                            if (connSymbol == null ||
4244
                                 (connSymbol != null && connSymbol.get_ItemStatus() == "Active" && connSymbol.get_RepresentationType() != "Branch") ||
4245
                                containZeroLength)
4246
                            {
4247
                                bool bCalcX = false;
4248
                                bool bCalcY = false;
4249
                                if (targetLine.SlopeType == SlopeType.HORIZONTAL)
4250
                                    bCalcX = true;
4251
                                else if (targetLine.SlopeType == SlopeType.VERTICAL)
4252
                                    bCalcY = true;
4253
                                else
4254
                                {
4255
                                    bCalcX = true;
4256
                                    bCalcY = true;
4257
                                }
4258

    
4259
                                if (bCalcX)
4260
                                {
4261
                                    double nextX = targetVertices[i - 1][0];
4262
                                    double newX = 0;
4263
                                    if (nextX > tempX)
4264
                                    {
4265
                                        newX = tempX + gridSetting.Length;
4266
                                        if (newX > nextX)
4267
                                            newX = (point[0] + nextX) / 2;
4268
                                    }
4269
                                    else
4270
                                    {
4271
                                        newX = tempX - gridSetting.Length;
4272
                                        if (newX < nextX)
4273
                                            newX = (point[0] + nextX) / 2;
4274
                                    }
4275
                                    resultPoint = new double[] { newX, resultPoint[1] };
4276
                                }
4277

    
4278
                                if (bCalcY)
4279
                                {
4280
                                    double nextY = targetVertices[i - 1][1];
4281
                                    double newY = 0;
4282
                                    if (nextY > tempY)
4283
                                    {
4284
                                        newY = tempY + gridSetting.Length;
4285
                                        if (newY > nextY)
4286
                                            newY = (point[1] + nextY) / 2;
4287
                                    }
4288
                                    else
4289
                                    {
4290
                                        newY = tempY - gridSetting.Length;
4291
                                        if (newY < nextY)
4292
                                            newY = (point[1] + nextY) / 2;
4293
                                    }
4294
                                    resultPoint = new double[] { resultPoint[0], newY };
4295
                                }
4296
                            }
4297
                        }
4298
                        break;
4299
                    }
4300
                }
4301
            }
4302

    
4303
            x = resultPoint[0];
4304
            y = resultPoint[1];
4305

    
4306
            return targetConnector;
4307
        }
4308

    
4309
        private LMConnector GetLMConnectorOnlyOne(string modelItemID)
4310
        {
4311
            LMConnector result = null;
4312
            List<LMConnector> connectors = new List<LMConnector>();
4313
            LMModelItem modelItem = dataSource.GetModelItem(modelItemID);
4314

    
4315
            if (modelItem != null)
4316
            {
4317
                foreach (LMRepresentation rep in modelItem.Representations)
4318
                {
4319
                    if (rep.Attributes["RepresentationType"].get_Value() == "Connector" && rep.Attributes["ItemStatus"].get_Value() == "Active")
4320
                        connectors.Add(dataSource.GetConnector(rep.Id));
4321
                }
4322

    
4323
                ReleaseCOMObjects(modelItem);
4324
            }
4325

    
4326
            if (connectors.Count == 1)
4327
                result = connectors[0];
4328
            else
4329
                foreach (var item in connectors)
4330
                    ReleaseCOMObjects(item);
4331

    
4332
            return result;
4333
        }
4334

    
4335
        private LMConnector GetLMConnectorFirst(string modelItemID)
4336
        {
4337
            LMConnector result = null;
4338
            LMModelItem modelItem = dataSource.GetModelItem(modelItemID);
4339

    
4340
            if (modelItem != null)
4341
            {
4342
                foreach (LMRepresentation rep in modelItem.Representations)
4343
                {
4344
                    if (rep.Attributes["RepresentationType"].get_Value() == "Connector" &&
4345
                        rep.Attributes["ItemStatus"].get_Value() == "Active")
4346
                    {
4347
                        LMConnector connector = dataSource.GetConnector(rep.Id);
4348
                        if (!Convert.ToBoolean(connector.get_IsZeroLength()))
4349
                        {
4350
                            result = connector;
4351
                            break;
4352
                        }
4353
                        else
4354
                        {
4355
                            ReleaseCOMObjects(connector);
4356
                            connector = null;
4357
                        }
4358
                    }
4359
                }
4360
                ReleaseCOMObjects(modelItem);
4361
                modelItem = null;
4362
            }
4363

    
4364
            return result;
4365
        }
4366

    
4367
        private int GetConnectorCount(string modelItemID)
4368
        {
4369
            LMModelItem modelItem = dataSource.GetModelItem(modelItemID);
4370
            int result = 0;
4371
            if (modelItem != null)
4372
            {
4373
                foreach (LMRepresentation rep in modelItem.Representations)
4374
                {
4375
                    if (rep.Attributes["RepresentationType"].get_Value() == "Connector" && rep.Attributes["ItemStatus"].get_Value() == "Active")
4376
                        result++;
4377
                    ReleaseCOMObjects(rep);
4378
                }
4379
                ReleaseCOMObjects(modelItem);
4380
            }
4381

    
4382
            return result;
4383
        }
4384

    
4385
        public List<string> GetRepresentations(string modelItemID)
4386
        {
4387
            List<string> result = new List<string>(); ;
4388
            LMModelItem modelItem = dataSource.GetModelItem(modelItemID);
4389
            if (modelItem != null)
4390
            {
4391
                foreach (LMRepresentation rep in modelItem.Representations)
4392
                {
4393
                    if (rep.Attributes["RepresentationType"].get_Value() == "Connector" && rep.Attributes["ItemStatus"].get_Value() == "Active")
4394
                        result.Add(rep.Id);
4395
                }
4396
                ReleaseCOMObjects(modelItem);
4397
            }
4398

    
4399
            return result;
4400
        }
4401

    
4402
        private void LineNumberModeling(LineNumber lineNumber)
4403
        {
4404
            Line line = SPPIDUtil.FindObjectByUID(document, lineNumber.CONNLINE) as Line;
4405
            if (line != null)
4406
            {
4407
                double x = 0;
4408
                double y = 0;
4409
                CalcLabelLocation(ref x, ref y, lineNumber.SPPID.ORIGINAL_X, lineNumber.SPPID.ORIGINAL_Y, lineNumber.SPPIDLabelLocation, _ETCSetting.LineNumberLocation);
4410

    
4411
                Dictionary<LMConnector, List<double[]>> connectorVertices = GetPipeRunVertices(line.SPPID.ModelItemId);
4412
                LMConnector connectedLMConnector = FindTargetLMConnectorForLabel(connectorVertices, x, y);
4413
                if (connectedLMConnector != null)
4414
                {
4415
                    Array points = new double[] { 0, x, y };
4416
                    lineNumber.SPPID.SPPID_X = x;
4417
                    lineNumber.SPPID.SPPID_Y = y;
4418
                    LMLabelPersist _LmLabelPresist = _placement.PIDPlaceLabel(lineNumber.SPPID.MAPPINGNAME, ref points, Rotation: lineNumber.ANGLE, LabeledItem: connectedLMConnector.AsLMRepresentation(), IsLeaderVisible: false);
4419

    
4420
                    if (_LmLabelPresist != null)
4421
                    {
4422
                        _LmLabelPresist.Commit();
4423
                        lineNumber.SPPID.RepresentationId = _LmLabelPresist.AsLMRepresentation().Id;
4424
                        ReleaseCOMObjects(_LmLabelPresist);
4425
                    }
4426
                }
4427

    
4428
                foreach (var item in connectorVertices)
4429
                    ReleaseCOMObjects(item.Key);
4430
            }
4431
        }
4432
        private void LineNumberCorrectModeling(LineNumber lineNumber)
4433
        {
4434
            Line line = SPPIDUtil.FindObjectByUID(document, lineNumber.CONNLINE) as Line;
4435
            if (line == null || line.SPPID.Vertices == null)
4436
                return;
4437

    
4438
            if (!string.IsNullOrEmpty(lineNumber.SPPID.RepresentationId))
4439
            {
4440
                LMLabelPersist removeLabel = dataSource.GetLabelPersist(lineNumber.SPPID.RepresentationId);
4441
                if (removeLabel != null)
4442
                {
4443
                    lineNumber.SPPID.SPPID_X = removeLabel.get_XCoordinate();
4444
                    lineNumber.SPPID.SPPID_Y = removeLabel.get_YCoordinate();
4445

    
4446
                    GridSetting gridSetting = GridSetting.GetInstance();
4447
                    LMConnector connector = dataSource.GetConnector(removeLabel.RepresentationID);
4448

    
4449
                    double[] labelRange = null;
4450
                    GetSPPIDSymbolRange(removeLabel, ref labelRange);
4451
                    List<double[]> vertices = GetConnectorVertices(connector);
4452

    
4453
                    double[] resultStart = null;
4454
                    double[] resultEnd = null;
4455
                    double distance = double.MaxValue;
4456
                    for (int i = 0; i < vertices.Count - 1; i++)
4457
                    {
4458
                        double[] startPoint = vertices[i];
4459
                        double[] endPoint = vertices[i + 1];
4460
                        foreach (var item in line.SPPID.Vertices)
4461
                        {
4462
                            double[] lineStartPoint = item[0];
4463
                            double[] lineEndPoint = item[item.Count - 1];
4464

    
4465
                            double tempDistance = SPPIDUtil.CalcPointToPointdDistance(startPoint[0], startPoint[1], lineStartPoint[0], lineStartPoint[1]) +
4466
                                SPPIDUtil.CalcPointToPointdDistance(endPoint[0], endPoint[1], lineEndPoint[0], lineEndPoint[1]);
4467
                            if (tempDistance < distance)
4468
                            {
4469
                                distance = tempDistance;
4470
                                resultStart = startPoint;
4471
                                resultEnd = endPoint;
4472
                            }
4473
                            tempDistance = SPPIDUtil.CalcPointToPointdDistance(startPoint[0], startPoint[1], lineEndPoint[0], lineEndPoint[1]) +
4474
                                SPPIDUtil.CalcPointToPointdDistance(endPoint[0], endPoint[1], lineStartPoint[0], lineStartPoint[1]);
4475
                            if (tempDistance < distance)
4476
                            {
4477
                                distance = tempDistance;
4478
                                resultStart = startPoint;
4479
                                resultEnd = endPoint;
4480
                            }
4481
                        }
4482
                    }
4483

    
4484
                    if (resultStart != null && resultEnd != null)
4485
                    {
4486
                        SlopeType slope = SPPIDUtil.CalcSlope(resultStart[0], resultStart[1], resultEnd[0], resultEnd[1]);
4487
                        double lineStartX = 0;
4488
                        double lineStartY = 0;
4489
                        double lineEndX = 0;
4490
                        double lineEndY = 0;
4491
                        double lineNumberX = 0;
4492
                        double lineNumberY = 0;
4493
                        SPPIDUtil.ConvertPointBystring(line.STARTPOINT, ref lineStartX, ref lineStartY);
4494
                        SPPIDUtil.ConvertPointBystring(line.ENDPOINT, ref lineEndX, ref lineEndY);
4495

    
4496
                        double lineX = (lineStartX + lineEndX) / 2;
4497
                        double lineY = (lineStartY + lineEndY) / 2;
4498
                        lineNumberX = (lineNumber.X1 + lineNumber.X2) / 2;
4499
                        lineNumberY = (lineNumber.Y1 + lineNumber.Y2) / 2;
4500

    
4501
                        double SPPIDCenterX = (resultStart[0] + resultEnd[0]) / 2;
4502
                        double SPPIDCenterY = (resultStart[1] + resultEnd[1]) / 2;
4503
                        double labelCenterX = (labelRange[0] + labelRange[2]) / 2;
4504
                        double labelCenterY = (labelRange[1] + labelRange[3]) / 2;
4505

    
4506
                        double offsetX = 0;
4507
                        double offsetY = 0;
4508
                        if (slope == SlopeType.HORIZONTAL)
4509
                        {
4510
                            // Line Number 아래
4511
                            if (lineY < lineNumberY)
4512
                            {
4513
                                offsetX = labelCenterX - SPPIDCenterX;
4514
                                offsetY = labelRange[3] - SPPIDCenterY + gridSetting.Length;
4515
                                MoveLineNumber(lineNumber, offsetX, offsetY);
4516
                            }
4517
                            // Line Number 위
4518
                            else
4519
                            {
4520
                                offsetX = labelCenterX - SPPIDCenterX;
4521
                                offsetY = labelRange[1] - SPPIDCenterY - gridSetting.Length;
4522
                                MoveLineNumber(lineNumber, offsetX, offsetY);
4523
                            }
4524
                        }
4525
                        else if (slope == SlopeType.VERTICAL)
4526
                        {
4527
                            // Line Number 오르쪽
4528
                            if (lineX < lineNumberX)
4529
                            {
4530
                                offsetX = labelRange[0] - SPPIDCenterX - gridSetting.Length;
4531
                                offsetY = labelCenterY - SPPIDCenterY;
4532
                                MoveLineNumber(lineNumber, offsetX, offsetY);
4533
                            }
4534
                            // Line Number 왼쪽
4535
                            else
4536
                            {
4537
                                offsetX = labelRange[2] - SPPIDCenterX + gridSetting.Length;
4538
                                offsetY = labelCenterY - SPPIDCenterY;
4539
                                MoveLineNumber(lineNumber, offsetX, offsetY);
4540
                            }
4541
                        }
4542

    
4543
                        if (offsetY != 0 || offsetY != 0)
4544
                        {
4545
                            if (connector.ConnectItem1SymbolObject != null &&
4546
                                connector.ConnectItem1SymbolObject.get_RepresentationType() == "OPC")
4547
                            {
4548
                                Ingr.RAD2D.Symbol2d symbol = radApp.ActiveDocument.ActiveSheet.DrawingObjects[connector.ConnectItem1SymbolObject.get_GraphicOID().ToString()];
4549

    
4550
                                double x1, y1, x2, y2, originX, originY;
4551
                                symbol.Range(out x1, out y1, out x2, out y2);
4552
                                symbol.GetOrigin(out originX, out originY);
4553
                                if (originX < lineNumber.SPPID.SPPID_X)
4554
                                    offsetX = -1 * (originX + gridSetting.Length * 30 - labelCenterX);
4555
                                else
4556
                                    offsetX = -1 * (originX - gridSetting.Length * 30 - labelCenterX);
4557
                            }
4558
                            else if (connector.ConnectItem2SymbolObject != null &&
4559
                                    connector.ConnectItem2SymbolObject.get_RepresentationType() == "OPC")
4560
                            {
4561
                                Ingr.RAD2D.Symbol2d symbol = radApp.ActiveDocument.ActiveSheet.DrawingObjects[connector.ConnectItem2SymbolObject.get_GraphicOID().ToString()];
4562

    
4563
                                double x1, y1, x2, y2, originX, originY;
4564
                                symbol.Range(out x1, out y1, out x2, out y2);
4565
                                symbol.GetOrigin(out originX, out originY);
4566
                                if (originX < lineNumber.SPPID.SPPID_X)
4567
                                    offsetX = -1 * (originX + gridSetting.Length * 30 - labelCenterX);
4568
                                else
4569
                                    offsetX = -1 * (originX - gridSetting.Length * 30 - labelCenterX);
4570
                            }
4571

    
4572
                            radApp.ActiveSelectSet.RemoveAll();
4573
                            DependencyObject dependency = radApp.ActiveDocument.ActiveSheet.DrawingObjects[removeLabel.get_GraphicOID().ToString()] as DependencyObject;
4574
                            if (dependency != null)
4575
                            {
4576
                                radApp.ActiveSelectSet.Add(dependency);
4577
                                Ingr.RAD2D.Transform transform = dependency.GetTransform();
4578
                                transform.DefineByMove2d(-offsetX, -offsetY);
4579
                                radApp.ActiveSelectSet.Transform(transform, true);
4580
                                radApp.ActiveSelectSet.RemoveAll();
4581
                            }
4582
                        }
4583

    
4584
                        void MoveLineNumber(LineNumber moveLineNumber, double x, double y)
4585
                        {
4586
                            moveLineNumber.SPPID.SPPID_X = moveLineNumber.SPPID.SPPID_X - x;
4587
                            moveLineNumber.SPPID.SPPID_Y = moveLineNumber.SPPID.SPPID_Y - y;
4588
                        }
4589
                    }
4590

    
4591

    
4592
                    ReleaseCOMObjects(connector);
4593
                    connector = null;
4594
                }
4595

    
4596
                ReleaseCOMObjects(removeLabel);
4597
                removeLabel = null;
4598
            }
4599
        }
4600
        /// <summary>
4601
        /// Flow Mark Modeling
4602
        /// </summary>
4603
        /// <param name="line"></param>
4604
        private void FlowMarkModeling(Line line)
4605
        {
4606
            if (line.FLOWMARK && !string.IsNullOrEmpty(line.SPPID.ModelItemId) && !string.IsNullOrEmpty(_ETCSetting.FlowMarkSymbolPath))
4607
            {
4608
                LMConnector connector = GetLMConnectorOnlyOne(line.SPPID.ModelItemId);
4609
                if (connector != null)
4610
                {
4611
                    string mappingPath = _ETCSetting.FlowMarkSymbolPath;
4612
                    List<double[]> vertices = GetConnectorVertices(connector);
4613
                    vertices = vertices.FindAll(x => x[0] > 0 && x[1] > 0);
4614
                    double[] point = vertices[vertices.Count - 1];
4615
                    Array array = new double[] { 0, point[0], point[1] };
4616
                    LMLabelPersist _LMLabelPersist = _placement.PIDPlaceLabel(mappingPath, ref array, LabeledItem: connector.AsLMRepresentation());
4617
                    if (_LMLabelPersist != null)
4618
                    {
4619
                        _LMLabelPersist.Commit();
4620
                        FlowMarkRepIds.Add(_LMLabelPersist.Id);
4621
                        ReleaseCOMObjects(_LMLabelPersist);
4622
                    }
4623
                }
4624
            }
4625
        }
4626

    
4627
        /// <summary>
4628
        /// Line Number 기준으로 모든 Item에 Line Number의 Attribute Input
4629
        /// </summary>
4630
        /// <param name="lineNumber"></param>
4631
        private void InputLineNumberAttribute(LineNumber lineNumber, List<string> endLine)
4632
        {
4633
            lineNumber.ATTRIBUTES.Sort(SortAttribute);
4634
            int SortAttribute(BaseModel.Attribute a, BaseModel.Attribute b)
4635
            {
4636
                if (a.ATTRIBUTE == "Tag Seq No")
4637
                    return 1;
4638
                else if (b.ATTRIBUTE == "Tag Seq No")
4639
                    return -1;
4640

    
4641
                return 0;
4642
            }
4643

    
4644
            foreach (LineRun run in lineNumber.RUNS)
4645
            {
4646
                foreach (var item in run.RUNITEMS)
4647
                {
4648
                    if (item.GetType() == typeof(Line))
4649
                    {
4650
                        Line line = item as Line;
4651
                        if (line != null && !endLine.Contains(line.SPPID.ModelItemId))
4652
                        {
4653
                            LMModelItem _LMModelItem = dataSource.GetModelItem(line.SPPID.ModelItemId);
4654
                            if (_LMModelItem != null && _LMModelItem.get_ItemStatus() == "Active")
4655
                            {
4656
                                foreach (var attribute in lineNumber.ATTRIBUTES)
4657
                                {
4658
                                    LineNumberMapping mapping = document.LineNumberMappings.Find(x => x.UID == attribute.UID);
4659
                                    if (mapping != null && !string.IsNullOrEmpty(attribute.VALUE) && attribute.VALUE != "None")
4660
                                    {
4661
                                        LMAAttribute _LMAAttribute = _LMModelItem.Attributes[mapping.SPPIDATTRIBUTENAME];
4662
                                        if (mapping.SPPIDATTRIBUTENAME == "OperFluidCode" && !string.IsNullOrEmpty(attribute.VALUE))
4663
                                        {
4664
                                            LMAAttribute _FluidSystemAttribute = _LMModelItem.Attributes["FluidSystem"];
4665
                                            if (_FluidSystemAttribute != null)
4666
                                            {
4667
                                                DataTable dt = SPPID_DB.GetFluidSystemInfo(attribute.VALUE);
4668
                                                if (dt.Rows.Count == 1)
4669
                                                {
4670
                                                    string fluidSystem = dt.Rows[0]["CODELIST_TEXT"].ToString();
4671
                                                    if (DBNull.Value.Equals(_FluidSystemAttribute.get_Value()))
4672
                                                        _FluidSystemAttribute.set_Value(fluidSystem);
4673
                                                    else if (_FluidSystemAttribute.get_Value() != fluidSystem)
4674
                                                        _FluidSystemAttribute.set_Value(fluidSystem);
4675

    
4676
                                                    if (_LMAAttribute != null)
4677
                                                    {
4678
                                                        if (DBNull.Value.Equals(_LMAAttribute.get_Value()))
4679
                                                            _LMAAttribute.set_Value(attribute.VALUE);
4680
                                                        else if (_LMAAttribute.get_Value() != attribute.VALUE)
4681
                                                            _LMAAttribute.set_Value(attribute.VALUE);
4682
                                                    }
4683
                                                }
4684
                                                if (dt != null)
4685
                                                    dt.Dispose();
4686
                                            }
4687
                                        }
4688
                                        else if (mapping.SPPIDATTRIBUTENAME.Equals("NominalDiameter") && !string.IsNullOrEmpty(attribute.VALUE) && _LMAAttribute != null)
4689
                                        {
4690
                                            DataRow[] rows = nominalDiameterTable.Select(string.Format("MetricStr = '{0}' OR InchStr = '{0}'", attribute.VALUE));
4691

    
4692
                                            if (rows.Length.Equals(1))
4693
                                            {
4694
                                                if (_ETCSetting.UnitSetting != null && _ETCSetting.UnitSetting.Equals("Metric"))
4695
                                                    attribute.VALUE = rows[0]["MetricStr"].ToString();
4696
                                                else
4697
                                                    attribute.VALUE = rows[0]["InchStr"].ToString();
4698

    
4699
                                                if (DBNull.Value.Equals(_LMAAttribute.get_Value()))
4700
                                                    _LMAAttribute.set_Value(attribute.VALUE);
4701
                                                else if (_LMAAttribute.get_Value() != attribute.VALUE)
4702
                                                    _LMAAttribute.set_Value(attribute.VALUE);
4703
                                            }
4704
                                        }
4705
                                        else if (_LMAAttribute != null)
4706
                                        {
4707
                                            if (DBNull.Value.Equals(_LMAAttribute.get_Value()))
4708
                                                _LMAAttribute.set_Value(attribute.VALUE);
4709
                                            else if (_LMAAttribute.get_Value() != attribute.VALUE)
4710
                                                _LMAAttribute.set_Value(attribute.VALUE);
4711
                                        }
4712
                                    }
4713
                                }
4714
                                _LMModelItem.Commit();
4715
                            }
4716
                            if (_LMModelItem != null)
4717
                                ReleaseCOMObjects(_LMModelItem);
4718
                            endLine.Add(line.SPPID.ModelItemId);
4719
                        }
4720
                    }
4721
                }
4722
            }
4723
        }
4724

    
4725
        /// <summary>
4726
        /// Symbol Attribute 입력 메서드
4727
        /// </summary>
4728
        /// <param name="item"></param>
4729
        private void InputSymbolAttribute(object targetItem, List<BaseModel.Attribute> targetAttributes)
4730
        {
4731
            // Object 아이템이 Symbol일 경우 Equipment일 경우 
4732
            string sRep = null;
4733
            string sModelID = null;
4734
            if (targetItem.GetType() == typeof(Symbol))
4735
                sRep = ((Symbol)targetItem).SPPID.RepresentationId;
4736
            else if (targetItem.GetType() == typeof(Equipment))
4737
                sRep = ((Equipment)targetItem).SPPID.RepresentationId;
4738
            else if (targetItem.GetType() == typeof(Line))
4739
                sModelID = ((Line)targetItem).SPPID.ModelItemId;
4740

    
4741
            if (!string.IsNullOrEmpty(sRep))
4742
            {
4743
                LMSymbol _LMSymbol = dataSource.GetSymbol(sRep);
4744
                LMModelItem _LMModelItem = _LMSymbol.ModelItemObject;
4745
                LMAAttributes _Attributes = _LMModelItem.Attributes;
4746

    
4747
                foreach (var item in targetAttributes)
4748
                {
4749
                    AttributeMapping mapping = document.AttributeMappings.Find(x => x.UID == item.UID);
4750
                    if (mapping != null && !string.IsNullOrEmpty(item.VALUE) && item.VALUE != "None")
4751
                    {
4752
                        if (!mapping.IsText)
4753
                        {
4754
                            LMAAttribute _LMAAttribute = _Attributes[mapping.SPPIDATTRIBUTENAME];
4755
                            if (mapping.SPPIDATTRIBUTENAME.Equals("NominalDiameter") && !string.IsNullOrEmpty(item.VALUE) && _LMAAttribute != null)
4756
                            {
4757
                                DataRow[] rows = nominalDiameterTable.Select(string.Format("MetricStr = '{0}' OR InchStr = '{0}'", item.VALUE));
4758

    
4759
                                if (rows.Length.Equals(1))
4760
                                {
4761
                                    if (_ETCSetting.UnitSetting != null && _ETCSetting.UnitSetting.Equals("Metric"))
4762
                                        item.VALUE = rows[0]["MetricStr"].ToString();
4763
                                    else
4764
                                        item.VALUE = rows[0]["InchStr"].ToString();
4765

    
4766
                                    if (DBNull.Value.Equals(_LMAAttribute.get_Value()))
4767
                                        _LMAAttribute.set_Value(item.VALUE);
4768
                                    else if (_LMAAttribute.get_Value() != item.VALUE)
4769
                                        _LMAAttribute.set_Value(item.VALUE);
4770
                                }
4771
                            }
4772
                            else if (_LMAAttribute != null)
4773
                            {
4774
                                _LMAAttribute.set_Value(item.VALUE);
4775
                                // OPC 일경우 Attribute 저장
4776
                                if (targetItem.GetType() == typeof(Symbol))
4777
                                {
4778
                                    Symbol symbol = targetItem as Symbol;
4779
                                    if (symbol.TYPE == "Piping OPC's" || symbol.TYPE == "Instrument OPC's")
4780
                                        symbol.SPPID.Attributes.Add(new string[] { mapping.SPPIDATTRIBUTENAME, item.VALUE });
4781
                                }
4782
                            }
4783
                        }
4784
                        else
4785
                            DefaultTextModeling(item.VALUE, _LMSymbol.get_XCoordinate(), _LMSymbol.get_YCoordinate());
4786
                    }
4787
                }
4788
                _LMModelItem.Commit();
4789

    
4790
                ReleaseCOMObjects(_Attributes);
4791
                ReleaseCOMObjects(_LMModelItem);
4792
                ReleaseCOMObjects(_LMSymbol);
4793
            }
4794
            else if (!string.IsNullOrEmpty(sModelID))
4795
            {
4796
                LMModelItem _LMModelItem = dataSource.GetModelItem(sModelID);
4797
                LMAAttributes _Attributes = _LMModelItem.Attributes;
4798

    
4799
                foreach (var item in targetAttributes)
4800
                {
4801
                    AttributeMapping mapping = document.AttributeMappings.Find(x => x.UID == item.UID);
4802
                    if (mapping == null)
4803
                        continue;
4804
                    
4805
                    LMAAttribute _LMAAttribute = _LMModelItem.Attributes[mapping.SPPIDATTRIBUTENAME];
4806
                    if (mapping.SPPIDATTRIBUTENAME == "OperFluidCode" && !string.IsNullOrEmpty(item.VALUE))
4807
                    {
4808
                        LMAAttribute _FluidSystemAttribute = _LMModelItem.Attributes["FluidSystem"];
4809
                        if (_FluidSystemAttribute != null)
4810
                        {
4811
                            DataTable dt = SPPID_DB.GetFluidSystemInfo(item.VALUE);
4812
                            if (dt.Rows.Count == 1)
4813
                            {
4814
                                string fluidSystem = dt.Rows[0]["CODELIST_TEXT"].ToString();
4815
                                if (DBNull.Value.Equals(_FluidSystemAttribute.get_Value()))
4816
                                    _FluidSystemAttribute.set_Value(fluidSystem);
4817
                                else if (_FluidSystemAttribute.get_Value() != fluidSystem)
4818
                                    _FluidSystemAttribute.set_Value(fluidSystem);
4819

    
4820
                                if (_LMAAttribute != null)
4821
                                {
4822
                                    if (DBNull.Value.Equals(_LMAAttribute.get_Value()))
4823
                                        _LMAAttribute.set_Value(item.VALUE);
4824
                                    else if (_LMAAttribute.get_Value() != item.VALUE)
4825
                                        _LMAAttribute.set_Value(item.VALUE);
4826
                                }
4827
                            }
4828
                            if (dt != null)
4829
                                dt.Dispose();
4830
                        }
4831
                    }
4832
                    else if (mapping.SPPIDATTRIBUTENAME.Equals("NominalDiameter") && !string.IsNullOrEmpty(item.VALUE) && _LMAAttribute != null)
4833
                    {
4834
                        DataRow[] rows = nominalDiameterTable.Select(string.Format("MetricStr = '{0}' OR InchStr = '{0}'", item.VALUE));
4835

    
4836
                        if (rows.Length.Equals(1))
4837
                        {
4838
                            if (_ETCSetting.UnitSetting != null && _ETCSetting.UnitSetting.Equals("Metric"))
4839
                                item.VALUE = rows[0]["MetricStr"].ToString();
4840
                            else
4841
                                item.VALUE = rows[0]["InchStr"].ToString();
4842

    
4843
                            if (DBNull.Value.Equals(_LMAAttribute.get_Value()))
4844
                                _LMAAttribute.set_Value(item.VALUE);
4845
                            else if (_LMAAttribute.get_Value() != item.VALUE)
4846
                                _LMAAttribute.set_Value(item.VALUE);
4847
                        }
4848
                    }
4849
                    else if (mapping != null && !string.IsNullOrEmpty(item.VALUE) && item.VALUE != "None")
4850
                    {
4851
                        if (!mapping.IsText)
4852
                        {
4853
                            LMAAttribute _Attribute = _Attributes[mapping.SPPIDATTRIBUTENAME];
4854
                            if (_Attribute != null)
4855
                                _Attribute.set_Value(item.VALUE);
4856
                        }
4857
                    }
4858
                }
4859
                _LMModelItem.Commit();
4860

    
4861
                ReleaseCOMObjects(_Attributes);
4862
                ReleaseCOMObjects(_LMModelItem);
4863
            }
4864
        }
4865

    
4866
        /// <summary>
4867
        /// Input SpecBreak Attribute
4868
        /// </summary>
4869
        /// <param name="specBreak"></param>
4870
        private void InputSpecBreakAttribute(SpecBreak specBreak)
4871
        {
4872
            object upStreamObj = SPPIDUtil.FindObjectByUID(document, specBreak.UpStreamUID);
4873
            object downStreamObj = SPPIDUtil.FindObjectByUID(document, specBreak.DownStreamUID);
4874

    
4875
            if (upStreamObj != null &&
4876
                downStreamObj != null)
4877
            {
4878
                LMConnector targetLMConnector = FindBreakLineTarget(upStreamObj, downStreamObj);
4879

    
4880
                if (targetLMConnector != null)
4881
                {
4882
                    foreach (LMLabelPersist _LMLabelPersist in targetLMConnector.LabelPersists)
4883
                    {
4884
                        string symbolPath = _LMLabelPersist.get_FileName();
4885
                        AttributeMapping mapping = document.AttributeMappings.Find(x => x.SPPIDSYMBOLNAME == symbolPath);
4886
                        if (mapping != null)
4887
                        {
4888
                            BaseModel.Attribute attribute = specBreak.ATTRIBUTES.Find(y => y.UID == mapping.UID);
4889
                            if (attribute != null && !string.IsNullOrEmpty(attribute.VALUE) && attribute.VALUE != "None")
4890
                            {
4891
                                string[] values = attribute.VALUE.Split(new char[] { ',' });
4892
                                if (values.Length == 2)
4893
                                {
4894
                                    string upStreamValue = values[0];
4895
                                    string downStreamValue = values[1];
4896

    
4897
                                    InputAttributeForSpecBreak(upStreamObj, downStreamObj, upStreamValue, downStreamValue, mapping.SPPIDATTRIBUTENAME);
4898
                                }
4899
                            }
4900
                        }
4901
                    }
4902

    
4903
                    ReleaseCOMObjects(targetLMConnector);
4904
                }
4905
            }
4906

    
4907

    
4908
            #region 내부에서만 쓰는 메서드
4909
            void InputAttributeForSpecBreak(object _upStreamObj, object _downStreamObj, string upStreamValue, string downStreamValue, string sppidAttributeName)
4910
            {
4911
                Symbol upStreamSymbol = _upStreamObj as Symbol;
4912
                Line upStreamLine = _upStreamObj as Line;
4913
                Symbol downStreamSymbol = _downStreamObj as Symbol;
4914
                Line downStreamLine = _downStreamObj as Line;
4915
                // 둘다 Line일 경우
4916
                if (upStreamLine != null && downStreamLine != null)
4917
                {
4918
                    InputLineAttributeForSpecBreakLine(upStreamLine, sppidAttributeName, upStreamValue);
4919
                    InputLineAttributeForSpecBreakLine(downStreamLine, sppidAttributeName, downStreamValue);
4920
                }
4921
                // 둘다 Symbol일 경우
4922
                else if (upStreamSymbol != null && downStreamSymbol != null)
4923
                {
4924
                    LMConnector zeroLenthConnector = FindBreakLineTarget(upStreamSymbol, downStreamSymbol);
4925
                    LMSymbol upStreamLMSymbol = dataSource.GetSymbol(upStreamSymbol.SPPID.RepresentationId);
4926
                    LMSymbol downStreamLMSymbol = dataSource.GetSymbol(downStreamSymbol.SPPID.RepresentationId);
4927

    
4928
                    foreach (LMConnector connector in upStreamLMSymbol.Avoid1Connectors)
4929
                    {
4930
                        if (connector.get_ItemStatus() != "Active")
4931
                            continue;
4932

    
4933
                        if (connector.Id != zeroLenthConnector.Id)
4934
                            InputLineAttributeForSpecBreakLMConnector(connector, sppidAttributeName, upStreamValue);
4935
                    }
4936

    
4937
                    foreach (LMConnector connector in upStreamLMSymbol.Avoid2Connectors)
4938
                    {
4939
                        if (connector.get_ItemStatus() != "Active")
4940
                            continue;
4941

    
4942
                        if (connector.Id != zeroLenthConnector.Id)
4943
                            InputLineAttributeForSpecBreakLMConnector(connector, sppidAttributeName, upStreamValue);
4944
                    }
4945

    
4946
                    foreach (LMConnector connector in downStreamLMSymbol.Avoid1Connectors)
4947
                    {
4948
                        if (connector.get_ItemStatus() != "Active")
4949
                            continue;
4950

    
4951
                        if (connector.Id != zeroLenthConnector.Id)
4952
                            InputLineAttributeForSpecBreakLMConnector(connector, sppidAttributeName, downStreamValue);
4953
                    }
4954

    
4955
                    foreach (LMConnector connector in downStreamLMSymbol.Avoid2Connectors)
4956
                    {
4957
                        if (connector.get_ItemStatus() != "Active")
4958
                            continue;
4959

    
4960
                        if (connector.Id != zeroLenthConnector.Id)
4961
                            InputLineAttributeForSpecBreakLMConnector(connector, sppidAttributeName, downStreamValue);
4962
                    }
4963

    
4964
                    ReleaseCOMObjects(zeroLenthConnector);
4965
                    ReleaseCOMObjects(upStreamLMSymbol);
4966
                    ReleaseCOMObjects(downStreamLMSymbol);
4967
                }
4968
                else if (upStreamSymbol != null && downStreamLine != null)
4969
                {
4970
                    LMConnector zeroLenthConnector = FindBreakLineTarget(upStreamSymbol, downStreamLine);
4971
                    InputLineAttributeForSpecBreakLine(downStreamLine, sppidAttributeName, downStreamValue);
4972
                    LMSymbol upStreamLMSymbol = dataSource.GetSymbol(upStreamSymbol.SPPID.RepresentationId);
4973

    
4974
                    foreach (LMConnector connector in upStreamLMSymbol.Avoid1Connectors)
4975
                    {
4976
                        if (connector.get_ItemStatus() != "Active")
4977
                            continue;
4978

    
4979
                        if (connector.Id == zeroLenthConnector.Id)
4980
                            InputLineAttributeForSpecBreakLMConnector(connector, sppidAttributeName, upStreamValue);
4981
                    }
4982

    
4983
                    foreach (LMConnector connector in upStreamLMSymbol.Avoid2Connectors)
4984
                    {
4985
                        if (connector.get_ItemStatus() != "Active")
4986
                            continue;
4987

    
4988
                        if (connector.Id == zeroLenthConnector.Id)
4989
                            InputLineAttributeForSpecBreakLMConnector(connector, sppidAttributeName, upStreamValue);
4990
                    }
4991

    
4992
                    ReleaseCOMObjects(zeroLenthConnector);
4993
                    ReleaseCOMObjects(upStreamLMSymbol);
4994
                }
4995
                else if (upStreamLine != null && downStreamSymbol != null)
4996
                {
4997
                    LMConnector zeroLenthConnector = FindBreakLineTarget(upStreamLine, downStreamSymbol);
4998
                    InputLineAttributeForSpecBreakLine(upStreamLine, sppidAttributeName, upStreamValue);
4999
                    LMSymbol downStreamLMSymbol = dataSource.GetSymbol(downStreamSymbol.SPPID.RepresentationId);
5000

    
5001
                    foreach (LMConnector connector in downStreamLMSymbol.Avoid1Connectors)
5002
                    {
5003
                        if (connector.get_ItemStatus() != "Active")
5004
                            continue;
5005

    
5006
                        if (connector.Id == zeroLenthConnector.Id)
5007
                            InputLineAttributeForSpecBreakLMConnector(connector, sppidAttributeName, downStreamValue);
5008
                    }
5009

    
5010
                    foreach (LMConnector connector in downStreamLMSymbol.Avoid2Connectors)
5011
                    {
5012
                        if (connector.get_ItemStatus() != "Active")
5013
                            continue;
5014

    
5015
                        if (connector.Id == zeroLenthConnector.Id)
5016
                            InputLineAttributeForSpecBreakLMConnector(connector, sppidAttributeName, downStreamValue);
5017
                    }
5018

    
5019
                    ReleaseCOMObjects(zeroLenthConnector);
5020
                    ReleaseCOMObjects(downStreamLMSymbol);
5021
                }
5022
            }
5023

    
5024
            void InputLineAttributeForSpecBreakLine(Line line, string attrName, string value)
5025
            {
5026
                LMModelItem _LMModelItem = dataSource.GetModelItem(line.SPPID.ModelItemId);
5027
                if (_LMModelItem != null && _LMModelItem.get_ItemStatus() == "Active")
5028
                {
5029
                    LMAAttribute _LMAAttribute = _LMModelItem.Attributes[attrName];
5030
                    if (_LMAAttribute != null)
5031
                    {
5032
                        if (DBNull.Value.Equals(_LMAAttribute.get_Value()))
5033
                            _LMAAttribute.set_Value(value);
5034
                        else if (_LMAAttribute.get_Value() != value)
5035
                            _LMAAttribute.set_Value(value);
5036
                    }
5037

    
5038
                    _LMModelItem.Commit();
5039
                }
5040
                if (_LMModelItem != null)
5041
                    ReleaseCOMObjects(_LMModelItem);
5042
            }
5043

    
5044
            void InputLineAttributeForSpecBreakLMConnector(LMConnector connector, string attrName, string value)
5045
            {
5046
                LMModelItem _LMModelItem = dataSource.GetModelItem(connector.ModelItemID);
5047
                if (_LMModelItem != null && _LMModelItem.get_ItemStatus() == "Active")
5048
                {
5049
                    LMAAttribute _LMAAttribute = _LMModelItem.Attributes[attrName];
5050
                    if (_LMAAttribute != null)
5051
                    {
5052
                        if (DBNull.Value.Equals(_LMAAttribute.get_Value()))
5053
                            _LMAAttribute.set_Value(value);
5054
                        else if (_LMAAttribute.get_Value() != value)
5055
                            _LMAAttribute.set_Value(value);
5056
                    }
5057

    
5058
                    _LMModelItem.Commit();
5059
                }
5060
                if (_LMModelItem != null)
5061
                    ReleaseCOMObjects(_LMModelItem);
5062
            }
5063
            #endregion
5064
        }
5065

    
5066
        private void InputEndBreakAttribute(EndBreak endBreak)
5067
        {
5068
            object ownerObj = SPPIDUtil.FindObjectByUID(document, endBreak.OWNER);
5069
            object connectedItem = SPPIDUtil.FindObjectByUID(document, endBreak.PROPERTIES.Find(x => x.ATTRIBUTE == "Connected Item").VALUE);
5070

    
5071
            if ((ownerObj.GetType() == typeof(Symbol) && connectedItem.GetType() == typeof(Line)) ||
5072
                (ownerObj.GetType() == typeof(Line) && connectedItem.GetType() == typeof(Symbol)))
5073
            {
5074
                LMLabelPersist labelPersist = dataSource.GetLabelPersist(endBreak.SPPID.RepresentationId);
5075
                if (labelPersist != null)
5076
                {
5077
                    LMRepresentation representation = labelPersist.RepresentationObject;
5078
                    if (representation != null)
5079
                    {
5080
                        LMConnector connector = dataSource.GetConnector(representation.Id);
5081
                        LMModelItem ZeroLengthModelItem = connector.ModelItemObject;
5082
                        string modelItemID = connector.ModelItemID;
5083
                        if (Convert.ToBoolean(connector.get_IsZeroLength()))
5084
                        {
5085
                            List<string> modelItemIDs = new List<string>();
5086
                            if (connector.ConnectItem1SymbolObject != null && connector.ConnectItem1SymbolObject.get_RepresentationType() != "Branch")
5087
                            {
5088
                                LMSymbol symbol = connector.ConnectItem1SymbolObject;
5089
                                foreach (LMConnector item in symbol.Connect1Connectors)
5090
                                {
5091
                                    if (item.get_ItemStatus() == "Active" && item.ModelItemID != modelItemID)
5092
                                        modelItemIDs.Add(item.ModelItemID);
5093
                                    ReleaseCOMObjects(item);
5094
                                }
5095
                                foreach (LMConnector item in symbol.Connect2Connectors)
5096
                                {
5097
                                    if (item.get_ItemStatus() == "Active" && item.ModelItemID != modelItemID)
5098
                                        modelItemIDs.Add(item.ModelItemID);
5099
                                    ReleaseCOMObjects(item);
5100
                                }
5101
                                ReleaseCOMObjects(symbol);
5102
                                symbol = null;
5103
                            }
5104
                            else if (connector.ConnectItem2SymbolObject != null && connector.ConnectItem2SymbolObject.get_RepresentationType() != "Branch")
5105
                            {
5106
                                LMSymbol symbol = connector.ConnectItem2SymbolObject;
5107
                                foreach (LMConnector item in symbol.Connect1Connectors)
5108
                                {
5109
                                    if (item.get_ItemStatus() == "Active" && item.ModelItemID != modelItemID)
5110
                                        modelItemIDs.Add(item.ModelItemID);
5111
                                    ReleaseCOMObjects(item);
5112
                                }
5113
                                foreach (LMConnector item in symbol.Connect2Connectors)
5114
                                {
5115
                                    if (item.get_ItemStatus() == "Active" && item.ModelItemID != modelItemID)
5116
                                        modelItemIDs.Add(item.ModelItemID);
5117
                                    ReleaseCOMObjects(item);
5118
                                }
5119
                                ReleaseCOMObjects(symbol);
5120
                                symbol = null;
5121
                            }
5122

    
5123
                            modelItemIDs = modelItemIDs.Distinct().ToList();
5124
                            if (modelItemIDs.Count == 1)
5125
                            {
5126
                                LMModelItem modelItem = dataSource.GetModelItem(modelItemIDs[0]);
5127
                                LMConnector onlyOne = GetLMConnectorOnlyOne(modelItem.Id);
5128
                                if (onlyOne != null && Convert.ToBoolean(onlyOne.get_IsZeroLength()))
5129
                                {
5130
                                    bool result = false;
5131
                                    foreach (LMLabelPersist loop in onlyOne.LabelPersists)
5132
                                    {
5133
                                        if (document.EndBreaks.Find(x => x.SPPID.RepresentationId == loop.RepresentationID) != null)
5134
                                            result = true;
5135
                                        ReleaseCOMObjects(loop);
5136
                                    }
5137

    
5138
                                    if (result)
5139
                                    {
5140
                                        object value = modelItem.Attributes["TagSequenceNo"].get_Value();
5141
                                        ZeroLengthModelItem.Attributes["TagSequenceNo"].set_Value(value);
5142
                                        ZeroLengthModelItem.Commit();
5143
                                    }
5144
                                    else
5145
                                    {
5146
                                        List<string> loopModelItems = new List<string>();
5147
                                        if (onlyOne.ConnectItem1SymbolObject.get_RepresentationType() == "Branch")
5148
                                        {
5149
                                            LMSymbol _symbol = onlyOne.ConnectItem1SymbolObject;
5150
                                            foreach (LMConnector loop in _symbol.Connect1Connectors)
5151
                                            {
5152
                                                if (loop.get_ItemStatus() == "Active" && loop.ModelItemID != onlyOne.ModelItemID)
5153
                                                    loopModelItems.Add(loop.ModelItemID);
5154
                                                ReleaseCOMObjects(loop);
5155
                                            }
5156
                                            foreach (LMConnector loop in _symbol.Connect2Connectors)
5157
                                            {
5158
                                                if (loop.get_ItemStatus() == "Active" && loop.ModelItemID != onlyOne.ModelItemID)
5159
                                                    loopModelItems.Add(loop.ModelItemID);
5160
                                                ReleaseCOMObjects(loop);
5161
                                            }
5162
                                            ReleaseCOMObjects(_symbol);
5163
                                            _symbol = null;
5164
                                        }
5165
                                        else if (onlyOne.ConnectItem2SymbolObject.get_RepresentationType() == "Branch")
5166
                                        {
5167
                                            LMSymbol _symbol = onlyOne.ConnectItem2SymbolObject;
5168
                                            foreach (LMConnector loop in _symbol.Connect1Connectors)
5169
                                            {
5170
                                                if (loop.get_ItemStatus() == "Active" && loop.ModelItemID != onlyOne.ModelItemID)
5171
                                                    loopModelItems.Add(loop.ModelItemID);
5172
                                                ReleaseCOMObjects(loop);
5173
                                            }
5174
                                            foreach (LMConnector loop in _symbol.Connect2Connectors)
5175
                                            {
5176
                                                if (loop.get_ItemStatus() == "Active" && loop.ModelItemID != onlyOne.ModelItemID)
5177
                                                    loopModelItems.Add(loop.ModelItemID);
5178
                                                ReleaseCOMObjects(loop);
5179
                                            }
5180
                                            ReleaseCOMObjects(_symbol);
5181
                                            _symbol = null;
5182
                                        }
5183

    
5184
                                        loopModelItems = loopModelItems.Distinct().ToList();
5185
                                        if (loopModelItems.Count == 1)
5186
                                        {
5187
                                            LMModelItem loopModelItem = dataSource.GetModelItem(loopModelItems[0]);
5188
                                            object value = loopModelItem.Attributes["TagSequenceNo"].get_Value();
5189
                                            modelItem.Attributes["TagSequenceNo"].set_Value(value);
5190
                                            modelItem.Commit();
5191
                                            ZeroLengthModelItem.Attributes["TagSequenceNo"].set_Value(value);
5192
                                            ZeroLengthModelItem.Commit();
5193

    
5194
                                            ReleaseCOMObjects(loopModelItem);
5195
                                            loopModelItem = null;
5196
                                        }
5197
                                    }
5198
                                }
5199
                                else
5200
                                {
5201
                                    object value = modelItem.Attributes["TagSequenceNo"].get_Value();
5202
                                    ZeroLengthModelItem.Attributes["TagSequenceNo"].set_Value(value);
5203
                                    ZeroLengthModelItem.Commit();
5204
                                }
5205
                                ReleaseCOMObjects(modelItem);
5206
                                modelItem = null;
5207
                                ReleaseCOMObjects(onlyOne);
5208
                                onlyOne = null;
5209
                            }
5210
                        }
5211
                        ReleaseCOMObjects(connector);
5212
                        connector = null;
5213
                        ReleaseCOMObjects(ZeroLengthModelItem);
5214
                        ZeroLengthModelItem = null;
5215
                    }
5216
                    ReleaseCOMObjects(representation);
5217
                    representation = null;
5218
                }
5219
                ReleaseCOMObjects(labelPersist);
5220
                labelPersist = null;
5221
            }
5222
        }
5223

    
5224
        /// <summary>
5225
        /// Text Modeling - Association일 경우는 Text대신 해당 맵핑된 Symbol로 모델링
5226
        /// </summary>
5227
        /// <param name="text"></param>
5228
        private void NormalTextModeling(Text text)
5229
        {
5230
            LMSymbol _LMSymbol = null;
5231

    
5232
            LMItemNote _LMItemNote = null;
5233
            LMAAttribute _LMAAttribute = null;
5234

    
5235
            double x = 0;
5236
            double y = 0;
5237
            double angle = text.ANGLE;
5238
            CalcLabelLocation(ref x, ref y, text.SPPID.ORIGINAL_X, text.SPPID.ORIGINAL_Y, text.SPPIDLabelLocation, _ETCSetting.TextLocation);
5239

    
5240
            SPPIDUtil.ConvertGridPoint(ref x, ref y);
5241
            text.SPPID.SPPID_X = x;
5242
            text.SPPID.SPPID_Y = y;
5243

    
5244
            _LMSymbol = _placement.PIDPlaceSymbol(text.SPPID.MAPPINGNAME, x, y, Rotation: angle);
5245
            if (_LMSymbol != null)
5246
            {
5247
                _LMSymbol.Commit();
5248
                _LMItemNote = _placement.PIDDataSource.GetItemNote(_LMSymbol.ModelItemID);
5249
                if (_LMItemNote != null)
5250
                {
5251
                    _LMItemNote.Commit();
5252
                    _LMAAttribute = _LMItemNote.Attributes["Note.Body"];
5253
                    if (_LMAAttribute != null)
5254
                    {
5255
                        _LMAAttribute.set_Value(text.VALUE);
5256
                        text.SPPID.RepresentationId = _LMSymbol.AsLMRepresentation().Id;
5257
                        _LMItemNote.Commit();
5258

    
5259

    
5260
                        double[] range = null;
5261
                        foreach (LMLabelPersist labelPersist in _LMSymbol.LabelPersists)
5262
                        {
5263
                            double[] temp = null;
5264
                            GetSPPIDSymbolRange(labelPersist, ref temp);
5265
                            if (temp != null)
5266
                            {
5267
                                if (range == null)
5268
                                    range = temp;
5269
                                else
5270
                                {
5271
                                    range = new double[] {
5272
                                            Math.Min(range[0], temp[0]),
5273
                                            Math.Min(range[1], temp[1]),
5274
                                            Math.Max(range[2], temp[2]),
5275
                                            Math.Max(range[3], temp[3])
5276
                                        };
5277
                                }
5278
                            }
5279
                        }
5280
                        text.SPPID.Range = range;
5281

    
5282
                        if (_LMAAttribute != null)
5283
                            ReleaseCOMObjects(_LMAAttribute);
5284
                        if (_LMItemNote != null)
5285
                            ReleaseCOMObjects(_LMItemNote);
5286
                    }
5287

    
5288
                    TextCorrectModeling(text);
5289
                }
5290
            }
5291
            if (_LMSymbol != null)
5292
                ReleaseCOMObjects(_LMSymbol);
5293
        }
5294

    
5295
        private void DefaultTextModeling(string value, double x, double y)
5296
        {
5297
            LMSymbol _LMSymbol = null;
5298

    
5299
            LMItemNote _LMItemNote = null;
5300
            LMAAttribute _LMAAttribute = null;
5301

    
5302
            _LMSymbol = _placement.PIDPlaceSymbol(_ETCSetting.TextSymbolPath, x, y, Rotation: 0);
5303
            if (_LMSymbol != null)
5304
            {
5305
                _LMSymbol.Commit();
5306
                _LMItemNote = _placement.PIDDataSource.GetItemNote(_LMSymbol.ModelItemID);
5307
                if (_LMItemNote != null)
5308
                {
5309
                    _LMItemNote.Commit();
5310
                    _LMAAttribute = _LMItemNote.Attributes["Note.Body"];
5311
                    if (_LMAAttribute != null)
5312
                    {
5313
                        _LMAAttribute.set_Value(value);
5314
                        _LMItemNote.Commit();
5315

    
5316
                        if (_LMAAttribute != null)
5317
                            ReleaseCOMObjects(_LMAAttribute);
5318
                        if (_LMItemNote != null)
5319
                            ReleaseCOMObjects(_LMItemNote);
5320
                    }
5321
                }
5322
            }
5323
            if (_LMSymbol != null)
5324
                ReleaseCOMObjects(_LMSymbol);
5325
        }
5326

    
5327
        private void AssociationTextModeling(Text text)
5328
        {
5329
            LMSymbol _LMSymbol = null;
5330
            LMConnector connectedLMConnector = null;
5331
            object owner = SPPIDUtil.FindObjectByUID(document, text.OWNER);
5332
            if (owner != null && (owner.GetType() == typeof(Symbol) || owner.GetType() == typeof(Equipment)))
5333
            {
5334
                Symbol symbol = owner as Symbol;
5335
                _LMSymbol = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
5336
                if (_LMSymbol != null)
5337
                {
5338
                    foreach (BaseModel.Attribute attribute in symbol.ATTRIBUTES.FindAll(x => x.ASSOCITEM == text.UID))
5339
                    {
5340
                        if (attribute != null && !string.IsNullOrEmpty(attribute.VALUE) && attribute.VALUE != "None")
5341
                        {
5342
                            AttributeMapping mapping = document.AttributeMappings.Find(x => x.UID == attribute.UID && !string.IsNullOrEmpty(x.SPPIDSYMBOLNAME));
5343

    
5344
                            if (mapping != null)
5345
                            {
5346
                                double x = 0;
5347
                                double y = 0;
5348

    
5349
                                CalcLabelLocation(ref x, ref y, text.SPPID.ORIGINAL_X, text.SPPID.ORIGINAL_Y, text.SPPIDLabelLocation, mapping.Location);
5350
                                SPPIDUtil.ConvertGridPoint(ref x, ref y);
5351
                                Array array = new double[] { 0, x, y };
5352
                                text.SPPID.SPPID_X = x;
5353
                                text.SPPID.SPPID_Y = y;
5354
                                LMLabelPersist _LMLabelPersist = _placement.PIDPlaceLabel(mapping.SPPIDSYMBOLNAME, ref array, Rotation: text.ANGLE, LabeledItem: _LMSymbol.AsLMRepresentation(), IsLeaderVisible: mapping.LeaderLine);
5355
                                if (_LMLabelPersist != null)
5356
                                {
5357
                                    text.SPPID.RepresentationId = _LMLabelPersist.AsLMRepresentation().Id;
5358
                                    _LMLabelPersist.Commit();
5359
                                    ReleaseCOMObjects(_LMLabelPersist);
5360
                                }
5361
                            }
5362
                        }
5363
                    }
5364
                }
5365
            }
5366
            else if (owner != null && owner.GetType() == typeof(Line))
5367
            {
5368
                Line line = owner as Line;
5369
                Dictionary<LMConnector, List<double[]>> connectorVertices = GetPipeRunVertices(line.SPPID.ModelItemId);
5370
                connectedLMConnector = FindTargetLMConnectorForLabel(connectorVertices, text.SPPID.ORIGINAL_X, text.SPPID.ORIGINAL_Y);
5371

    
5372
                if (connectedLMConnector != null)
5373
                {
5374
                    foreach (BaseModel.Attribute attribute in line.ATTRIBUTES.FindAll(x => x.ASSOCITEM == text.UID))
5375
                    {
5376
                        if (attribute != null && !string.IsNullOrEmpty(attribute.VALUE) && attribute.VALUE != "None")
5377
                        {
5378
                            AttributeMapping mapping = document.AttributeMappings.Find(x => x.UID == attribute.UID && !string.IsNullOrEmpty(x.SPPIDSYMBOLNAME));
5379

    
5380
                            if (mapping != null)
5381
                            {
5382
                                double x = 0;
5383
                                double y = 0;
5384
                                CalcLabelLocation(ref x, ref y, text.SPPID.ORIGINAL_X, text.SPPID.ORIGINAL_Y, text.SPPIDLabelLocation, mapping.Location);
5385
                                SPPIDUtil.ConvertGridPoint(ref x, ref y);
5386
                                Array array = new double[] { 0, x, y };
5387

    
5388
                                LMLabelPersist _LMLabelPersist = _placement.PIDPlaceLabel(mapping.SPPIDSYMBOLNAME, ref array, Rotation: text.ANGLE, LabeledItem: connectedLMConnector.AsLMRepresentation(), IsLeaderVisible: mapping.LeaderLine);
5389
                                if (_LMLabelPersist != null)
5390
                                {
5391
                                    text.SPPID.RepresentationId = _LMLabelPersist.AsLMRepresentation().Id;
5392
                                    _LMLabelPersist.Commit();
5393

    
5394
                                    DependencyObject dependency = radApp.ActiveDocument.ActiveSheet.DrawingObjects[_LMLabelPersist.get_GraphicOID().ToString()] as DependencyObject;
5395
                                    if (dependency != null)
5396
                                    {
5397
                                        radApp.ActiveSelectSet.RemoveAll();
5398
                                        radApp.ActiveSelectSet.Add(dependency);
5399
                                        Ingr.RAD2D.Transform transform = dependency.GetTransform();
5400
                                        transform.DefineByMove2d(x - _LMLabelPersist.get_XCoordinate(), y - _LMLabelPersist.get_YCoordinate());
5401
                                        radApp.ActiveSelectSet.Transform(transform, true);
5402
                                        radApp.ActiveSelectSet.RemoveAll();
5403
                                    }
5404

    
5405
                                    ReleaseCOMObjects(_LMLabelPersist);
5406
                                }
5407
                            }
5408
                        }
5409
                    }
5410
                }
5411
            }
5412
            if (_LMSymbol != null)
5413
                ReleaseCOMObjects(_LMSymbol);
5414
        }
5415

    
5416
        private void TextCorrectModeling(Text text)
5417
        {
5418
            if (text.SPPID.Range == null)
5419
                return;
5420

    
5421
            bool needRemodeling = false;
5422
            bool loop = true;
5423
            GridSetting gridSetting = GridSetting.GetInstance();
5424
            while (loop)
5425
            {
5426
                loop = false;
5427
                foreach (var overlapText in document.TEXTINFOS)
5428
                {
5429
                    if (overlapText.ASSOCIATION || overlapText == text || overlapText.SPPID.Range == null)
5430
                        continue;
5431

    
5432
                    if (SPPIDUtil.IsOverlap(overlapText.SPPID.Range, text.SPPID.Range))
5433
                    {
5434
                        double percentX = 0;
5435
                        double percentY = 0;
5436
                        if (overlapText.X1 <= text.X2 && overlapText.X2 >= text.X1)
5437
                        {
5438
                            double gapX = Math.Min(overlapText.X2, text.X2) - Math.Max(overlapText.X1, text.X1);
5439
                            percentX = Math.Max(gapX / (overlapText.X2 - overlapText.X1), gapX / (text.X2 - text.X1));
5440
                        }
5441
                        if (overlapText.Y1 <= text.Y2 && overlapText.Y2 >= text.Y1)
5442
                        {
5443
                            double gapY = Math.Min(overlapText.Y2, text.Y2) - Math.Max(overlapText.Y1, text.Y1);
5444
                            percentY = Math.Max(gapY / (overlapText.Y2 - overlapText.Y1), gapY / (text.Y2 - text.Y1));
5445
                        }
5446

    
5447
                        double tempX = 0;
5448
                        double tempY = 0;
5449
                        bool overlapX = false;
5450
                        bool overlapY = false;
5451
                        SPPIDUtil.CalcOverlap(text.SPPID.Range, overlapText.SPPID.Range, ref tempX, ref tempY, ref overlapX, ref overlapY);
5452
                        if (percentX >= percentY)
5453
                        {
5454
                            int count = Convert.ToInt32(tempY / gridSetting.Length) + 1;
5455
                            double move = gridSetting.Length * count;
5456
                            text.SPPID.SPPID_Y = text.SPPID.SPPID_Y - move;
5457
                            text.SPPID.Range = new double[] { text.SPPID.Range[0], text.SPPID.Range[1] - move, text.SPPID.Range[2], text.SPPID.Range[3] - move };
5458
                            needRemodeling = true;
5459
                            loop = true;
5460
                        }
5461
                        else
5462
                        {
5463
                            int count = Convert.ToInt32(tempX / gridSetting.Length) + 1;
5464
                            double move = gridSetting.Length * count;
5465
                            text.SPPID.SPPID_X = text.SPPID.SPPID_X + move;
5466
                            text.SPPID.Range = new double[] { text.SPPID.Range[0] + move, text.SPPID.Range[1], text.SPPID.Range[2] + move, text.SPPID.Range[3] };
5467
                            needRemodeling = true;
5468
                            loop = true;
5469
                        }
5470
                    }
5471
                }
5472
            }
5473

    
5474

    
5475
            if (needRemodeling)
5476
            {
5477
                LMSymbol symbol = dataSource.GetSymbol(text.SPPID.RepresentationId);
5478
                _placement.PIDRemovePlacement(symbol.AsLMRepresentation());
5479
                text.SPPID.RepresentationId = null;
5480

    
5481
                LMItemNote _LMItemNote = null;
5482
                LMAAttribute _LMAAttribute = null;
5483
                LMSymbol _LMSymbol = _placement.PIDPlaceSymbol(text.SPPID.MAPPINGNAME, text.SPPID.SPPID_X, text.SPPID.SPPID_Y, Rotation: text.ANGLE);
5484
                if (_LMSymbol != null)
5485
                {
5486
                    _LMSymbol.Commit();
5487
                    _LMItemNote = _placement.PIDDataSource.GetItemNote(_LMSymbol.ModelItemID);
5488
                    if (_LMItemNote != null)
5489
                    {
5490
                        _LMItemNote.Commit();
5491
                        _LMAAttribute = _LMItemNote.Attributes["Note.Body"];
5492
                        if (_LMAAttribute != null)
5493
                        {
5494
                            _LMAAttribute.set_Value(text.VALUE);
5495
                            text.SPPID.RepresentationId = _LMSymbol.AsLMRepresentation().Id;
5496
                            _LMItemNote.Commit();
5497

    
5498
                            ReleaseCOMObjects(_LMAAttribute);
5499
                            ReleaseCOMObjects(_LMItemNote);
5500
                        }
5501
                    }
5502
                }
5503

    
5504
                ReleaseCOMObjects(symbol);
5505
                symbol = null;
5506
                ReleaseCOMObjects(_LMItemNote);
5507
                _LMItemNote = null;
5508
                ReleaseCOMObjects(_LMAAttribute);
5509
                _LMAAttribute = null;
5510
                ReleaseCOMObjects(_LMSymbol);
5511
                _LMSymbol = null;
5512
            }
5513
        }
5514

    
5515
        private void AssociationTextCorrectModeling(Text text, List<Text> endTexts)
5516
        {
5517
            if (!string.IsNullOrEmpty(text.SPPID.RepresentationId))
5518
            {
5519
                List<Text> allTexts = new List<Text>();
5520
                LMLabelPersist targetLabel = dataSource.GetLabelPersist(text.SPPID.RepresentationId);
5521
                LMRepresentation representation = targetLabel.RepresentationObject;
5522
                Symbol symbol = document.SYMBOLS.Find(x => x.SPPID.RepresentationId == representation.Id);
5523
                if (targetLabel.RepresentationObject != null && symbol != null)
5524
                {
5525
                    double[] symbolRange = null;
5526
                    GetSPPIDSymbolRange(symbol, ref symbolRange, true, true);
5527
                    if (symbolRange != null)
5528
                    {
5529
                        foreach (LMLabelPersist labelPersist in representation.LabelPersists)
5530
                        {
5531
                            Text findText = document.TEXTINFOS.Find(x => x.SPPID.RepresentationId == labelPersist.AsLMRepresentation().Id && x.ASSOCIATION);
5532
                            if (findText != null)
5533
                            {
5534
                                double[] range = null;
5535
                                GetSPPIDSymbolRange(labelPersist, ref range);
5536
                                findText.SPPID.Range = range;
5537
                                allTexts.Add(findText);
5538
                            }
5539

    
5540
                            ReleaseCOMObjects(labelPersist);
5541
                        }
5542

    
5543
                        if (allTexts.Count > 0)
5544
                        {
5545
                            #region Sort Text By Y
5546
                            allTexts.Sort(SortTextByY);
5547
                            int SortTextByY(Text a, Text b)
5548
                            {
5549
                                return b.SPPID.Range[3].CompareTo(a.SPPID.Range[3]);
5550
                            }
5551
                            #endregion
5552

    
5553
                            #region 정렬하기전 방향
5554
                            List<Text> left = new List<Text>();
5555
                            List<Text> down = new List<Text>();
5556
                            List<Text> right = new List<Text>();
5557
                            List<Text> up = new List<Text>();
5558
                            List<List<Text>> sortTexts = new List<List<Text>>() { left, down, right, up };
5559
                            foreach (var loopText in allTexts)
5560
                            {
5561
                                double textCenterX = (loopText.X1 + loopText.X2) / 2;
5562
                                double textCenterY = (loopText.Y1 + loopText.Y2) / 2;
5563
                                double originX = 0;
5564
                                double originY = 0;
5565
                                SPPIDUtil.ConvertPointBystring(symbol.ORIGINALPOINT, ref originX, ref originY);
5566
                                double angle = SPPIDUtil.CalcAngle(textCenterX, textCenterY, originX, originY);
5567

    
5568
                                if (angle < 45)
5569
                                {
5570
                                    // Text 오른쪽
5571
                                    if (textCenterX > originX)
5572
                                        right.Add(loopText);
5573
                                    // Text 왼쪽
5574
                                    else
5575
                                        left.Add(loopText);
5576
                                }
5577
                                else
5578
                                {
5579
                                    // Text 아래쪽
5580
                                    if (textCenterY > originY)
5581
                                        down.Add(loopText);
5582
                                    // Text 위쪽
5583
                                    else
5584
                                        up.Add(loopText);
5585
                                }
5586
                            }
5587

    
5588
                            #endregion
5589

    
5590
                            foreach (var texts in sortTexts)
5591
                            {
5592
                                if (texts.Count == 0)
5593
                                    continue;
5594

    
5595
                                #region 첫번째 Text로 기준 맞춤
5596
                                for (int i = 0; i < texts.Count; i++)
5597
                                {
5598
                                    if (i != 0)
5599
                                    {
5600
                                        Text currentText = texts[i];
5601
                                        Text prevText = texts[i - 1];
5602
                                        double minY = prevText.SPPID.Range[1];
5603
                                        double centerPrevX = (prevText.SPPID.Range[0] + prevText.SPPID.Range[2]) / 2;
5604
                                        double centerX = (currentText.SPPID.Range[0] + currentText.SPPID.Range[2]) / 2;
5605
                                        double _gapX = centerX - centerPrevX;
5606
                                        double _gapY = currentText.SPPID.Range[3] - minY;
5607
                                        MoveText(currentText, _gapX, _gapY);
5608
                                    }
5609
                                }
5610
                                List<double> rangeMinX = texts.Select(loopX => loopX.SPPID.Range[0]).ToList();
5611
                                List<double> rangeMinY = texts.Select(loopX => loopX.SPPID.Range[1]).ToList();
5612
                                List<double> rangeMaxX = texts.Select(loopX => loopX.SPPID.Range[2]).ToList();
5613
                                List<double> rangeMaxY = texts.Select(loopX => loopX.SPPID.Range[3]).ToList();
5614
                                rangeMinX.Sort();
5615
                                rangeMinY.Sort();
5616
                                rangeMaxX.Sort();
5617
                                rangeMaxY.Sort();
5618
                                double allTextCenterX = (rangeMinX[0] + rangeMaxX[rangeMaxX.Count - 1]) / 2;
5619
                                double allTextCenterY = (rangeMinY[0] + rangeMaxY[rangeMaxY.Count - 1]) / 2;
5620
                                #endregion
5621
                                #region 정렬
5622
                                Text correctBySymbol = texts[0];
5623
                                double textCenterX = (correctBySymbol.X1 + correctBySymbol.X2) / 2;
5624
                                double textCenterY = (correctBySymbol.Y1 + correctBySymbol.Y2) / 2;
5625
                                double originX = 0;
5626
                                double originY = 0;
5627
                                SPPIDUtil.ConvertPointBystring(symbol.ORIGINALPOINT, ref originX, ref originY);
5628
                                double angle = SPPIDUtil.CalcAngle(textCenterX, textCenterY, originX, originY);
5629
                                double symbolCenterX = (symbolRange[0] + symbolRange[2]) / 2;
5630
                                double symbolCenterY = (symbolRange[1] + symbolRange[3]) / 2;
5631

    
5632
                                double gapX = 0;
5633
                                double gapY = 0;
5634
                                if (angle < 45)
5635
                                {
5636
                                    // Text 오른쪽
5637
                                    if (textCenterX > originX)
5638
                                    {
5639
                                        gapX = rangeMinX[0] - symbolRange[2];
5640
                                        gapY = allTextCenterY - symbolCenterY;
5641
                                    }
5642
                                    // Text 왼쪽
5643
                                    else
5644
                                    {
5645
                                        gapX = rangeMaxX[rangeMaxX.Count - 1] - symbolRange[0];
5646
                                        gapY = allTextCenterY - symbolCenterY;
5647
                                    }
5648
                                }
5649
                                else
5650
                                {
5651
                                    // Text 아래쪽
5652
                                    if (textCenterY > originY)
5653
                                    {
5654
                                        gapX = allTextCenterX - symbolCenterX;
5655
                                        gapY = rangeMaxY[rangeMaxY.Count - 1] - symbolRange[1];
5656
                                    }
5657
                                    // Text 위쪽
5658
                                    else
5659
                                    {
5660
                                        gapX = allTextCenterX - symbolCenterX;
5661
                                        gapY = rangeMinY[0] - symbolRange[3];
5662
                                    }
5663
                                }
5664

    
5665
                                foreach (var item in texts)
5666
                                {
5667
                                    MoveText(item, gapX, gapY);
5668
                                    RemodelingAssociationText(item);
5669
                                }
5670
                                #endregion
5671
                            }
5672
                        }
5673
                    }
5674
                }
5675

    
5676
                void MoveText(Text moveText, double x, double y)
5677
                {
5678
                    moveText.SPPID.SPPID_X = moveText.SPPID.SPPID_X - x;
5679
                    moveText.SPPID.SPPID_Y = moveText.SPPID.SPPID_Y - y;
5680
                    moveText.SPPID.Range = new double[] {
5681
                        moveText.SPPID.Range[0] - x,
5682
                        moveText.SPPID.Range[1]- y,
5683
                        moveText.SPPID.Range[2]- x,
5684
                        moveText.SPPID.Range[3]- y
5685
                    };
5686
                }
5687

    
5688
                endTexts.AddRange(allTexts);
5689

    
5690
                ReleaseCOMObjects(targetLabel);
5691
                targetLabel = null;
5692
                ReleaseCOMObjects(representation);
5693
                representation = null;
5694
            }
5695
        }
5696

    
5697
        private void RemodelingAssociationText(Text text)
5698
        {
5699
            LMLabelPersist removeLabel = dataSource.GetLabelPersist(text.SPPID.RepresentationId);
5700
            _placement.PIDRemovePlacement(removeLabel.AsLMRepresentation());
5701
            removeLabel.Commit();
5702
            ReleaseCOMObjects(removeLabel);
5703
            removeLabel = null;
5704

    
5705
            object owner = SPPIDUtil.FindObjectByUID(document, text.OWNER);
5706
            if (owner != null && owner.GetType() == typeof(Symbol))
5707
            {
5708
                Symbol symbol = owner as Symbol;
5709
                _LMSymbol _LMSymbol = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
5710
                if (_LMSymbol != null)
5711
                {
5712
                    BaseModel.Attribute attribute = symbol.ATTRIBUTES.Find(x => x.ASSOCITEM == text.UID);
5713
                    if (attribute != null && !string.IsNullOrEmpty(attribute.VALUE) && attribute.VALUE != "None")
5714
                    {
5715
                        AttributeMapping mapping = document.AttributeMappings.Find(x => x.UID == attribute.UID && !string.IsNullOrEmpty(x.SPPIDSYMBOLNAME));
5716

    
5717
                        if (mapping != null)
5718
                        {
5719
                            double x = 0;
5720
                            double y = 0;
5721

    
5722
                            Array array = new double[] { 0, text.SPPID.SPPID_X, text.SPPID.SPPID_Y };
5723
                            LMLabelPersist _LMLabelPersist = _placement.PIDPlaceLabel(mapping.SPPIDSYMBOLNAME, ref array, Rotation: text.ANGLE, LabeledItem: _LMSymbol.AsLMRepresentation(), IsLeaderVisible: mapping.LeaderLine);
5724
                            if (_LMLabelPersist != null)
5725
                            {
5726
                                text.SPPID.RepresentationId = _LMLabelPersist.AsLMRepresentation().Id;
5727
                                _LMLabelPersist.Commit();
5728
                            }
5729
                            ReleaseCOMObjects(_LMLabelPersist);
5730
                            _LMLabelPersist = null;
5731
                        }
5732
                    }
5733
                }
5734
                ReleaseCOMObjects(_LMSymbol);
5735
                _LMSymbol = null;
5736
            }
5737
        }
5738

    
5739
        /// <summary>
5740
        /// Note Modeling
5741
        /// </summary>
5742
        /// <param name="note"></param>
5743
        private void NoteModeling(Note note, List<Note> correctList)
5744
        {
5745
            LMSymbol _LMSymbol = null;
5746
            LMItemNote _LMItemNote = null;
5747
            LMAAttribute _LMAAttribute = null;
5748

    
5749
            if (string.IsNullOrEmpty(note.OWNER) || note.OWNER == "None")
5750
            {
5751
                double x = 0;
5752
                double y = 0;
5753

    
5754
                CalcLabelLocation(ref x, ref y, note.SPPID.ORIGINAL_X, note.SPPID.ORIGINAL_Y, note.SPPIDLabelLocation, _ETCSetting.NoteLocation);
5755
                SPPIDUtil.ConvertGridPoint(ref x, ref y);
5756
                note.SPPID.SPPID_X = x;
5757
                note.SPPID.SPPID_Y = y;
5758

    
5759
                _LMSymbol = _placement.PIDPlaceSymbol(note.SPPID.MAPPINGNAME, x, y);
5760
                if (_LMSymbol != null)
5761
                {
5762
                    _LMSymbol.Commit();
5763
                    _LMItemNote = _placement.PIDDataSource.GetItemNote(_LMSymbol.ModelItemID);
5764
                    if (_LMItemNote != null)
5765
                    {
5766
                        _LMItemNote.Commit();
5767
                        _LMAAttribute = _LMItemNote.Attributes["Note.Body"];
5768
                        if (_LMAAttribute != null)
5769
                        {
5770
                            _LMAAttribute.set_Value(note.VALUE);
5771
                            note.SPPID.RepresentationId = _LMSymbol.AsLMRepresentation().Id;
5772

    
5773
                            double[] range = null;
5774
                            foreach (LMLabelPersist labelPersist in _LMSymbol.LabelPersists)
5775
                            {
5776
                                double[] temp = null;
5777
                                GetSPPIDSymbolRange(labelPersist, ref temp);
5778
                                if (temp != null)
5779
                                {
5780
                                    if (range == null)
5781
                                        range = temp;
5782
                                    else
5783
                                    {
5784
                                        range = new double[] {
5785
                                            Math.Min(range[0], temp[0]),
5786
                                            Math.Min(range[1], temp[1]),
5787
                                            Math.Max(range[2], temp[2]),
5788
                                            Math.Max(range[3], temp[3])
5789
                                        };
5790
                                    }
5791
                                }
5792
                            }
5793
                            if (range != null)
5794
                                correctList.Add(note);
5795
                            note.SPPID.Range = range;
5796

    
5797

    
5798
                            _LMItemNote.Commit();
5799
                        }
5800
                    }
5801
                }
5802
            }
5803

    
5804
            if (_LMAAttribute != null)
5805
                ReleaseCOMObjects(_LMAAttribute);
5806
            if (_LMItemNote != null)
5807
                ReleaseCOMObjects(_LMItemNote);
5808
            if (_LMSymbol != null)
5809
                ReleaseCOMObjects(_LMSymbol);
5810
        }
5811

    
5812
        private void NoteCorrectModeling(Note note, List<Note> endList)
5813
        {
5814
            bool needRemodeling = false;
5815
            bool loop = true;
5816
            GridSetting gridSetting = GridSetting.GetInstance();
5817
            while (loop)
5818
            {
5819
                loop = false;
5820
                foreach (var overlap in endList)
5821
                {
5822
                    if (SPPIDUtil.IsOverlap(overlap.SPPID.Range, note.SPPID.Range))
5823
                    {
5824
                        double tempX = 0;
5825
                        double tempY = 0;
5826
                        bool overlapX = false;
5827
                        bool overlapY = false;
5828
                        SPPIDUtil.CalcOverlap(note.SPPID.Range, overlap.SPPID.Range, ref tempX, ref tempY, ref overlapX, ref overlapY);
5829
                        double angle = SPPIDUtil.CalcAngle(note.LOCATION_X, note.LOCATION_Y, overlap.LOCATION_X, overlap.LOCATION_Y);
5830
                        if (overlapY && angle >= 45)
5831
                        {
5832
                            int count = Convert.ToInt32(tempY / gridSetting.Length) + 1;
5833
                            double move = gridSetting.Length * count;
5834
                            note.SPPID.SPPID_Y = note.SPPID.SPPID_Y - move;
5835
                            note.SPPID.Range = new double[] { note.SPPID.Range[0], note.SPPID.Range[1] - move, note.SPPID.Range[2], note.SPPID.Range[3] - move };
5836
                            needRemodeling = true;
5837
                            loop = true;
5838
                        }
5839
                        if (overlapX && angle <= 45)
5840
                        {
5841
                            int count = Convert.ToInt32(tempX / gridSetting.Length) + 1;
5842
                            double move = gridSetting.Length * count;
5843
                            note.SPPID.SPPID_X = note.SPPID.SPPID_X + move;
5844
                            note.SPPID.Range = new double[] { note.SPPID.Range[0] + move, note.SPPID.Range[1], note.SPPID.Range[2] + move, note.SPPID.Range[3] };
5845
                            needRemodeling = true;
5846
                            loop = true;
5847
                        }
5848
                    }
5849
                }
5850
            }
5851

    
5852

    
5853
            if (needRemodeling)
5854
            {
5855
                LMSymbol symbol = dataSource.GetSymbol(note.SPPID.RepresentationId);
5856
                _placement.PIDRemovePlacement(symbol.AsLMRepresentation());
5857
                note.SPPID.RepresentationId = null;
5858

    
5859
                LMItemNote _LMItemNote = null;
5860
                LMAAttribute _LMAAttribute = null;
5861
                LMSymbol _LMSymbol = _placement.PIDPlaceSymbol(note.SPPID.MAPPINGNAME, note.SPPID.SPPID_X, note.SPPID.SPPID_Y, Rotation: note.ANGLE);
5862
                if (_LMSymbol != null)
5863
                {
5864
                    _LMSymbol.Commit();
5865
                    _LMItemNote = _placement.PIDDataSource.GetItemNote(_LMSymbol.ModelItemID);
5866
                    if (_LMItemNote != null)
5867
                    {
5868
                        _LMItemNote.Commit();
5869
                        _LMAAttribute = _LMItemNote.Attributes["Note.Body"];
5870
                        if (_LMAAttribute != null)
5871
                        {
5872
                            _LMAAttribute.set_Value(note.VALUE);
5873
                            note.SPPID.RepresentationId = _LMSymbol.AsLMRepresentation().Id;
5874
                            _LMItemNote.Commit();
5875

    
5876
                            ReleaseCOMObjects(_LMAAttribute);
5877
                            ReleaseCOMObjects(_LMItemNote);
5878
                        }
5879
                    }
5880
                }
5881

    
5882
                ReleaseCOMObjects(symbol);
5883
                symbol = null;
5884
                ReleaseCOMObjects(_LMItemNote);
5885
                _LMItemNote = null;
5886
                ReleaseCOMObjects(_LMAAttribute);
5887
                _LMAAttribute = null;
5888
                ReleaseCOMObjects(_LMSymbol);
5889
                _LMSymbol = null;
5890
            }
5891

    
5892
            endList.Add(note);
5893
        }
5894

    
5895
        private void JoinRunBySameType(string modelItemId, ref string survivorId)
5896
        {
5897
            LMModelItem modelItem = dataSource.GetModelItem(modelItemId);
5898
            if (modelItem != null)
5899
            {
5900
                foreach (LMRepresentation rep in modelItem.Representations)
5901
                {
5902
                    if (rep.Attributes["RepresentationType"].get_Value() == "Connector" && rep.Attributes["ItemStatus"].get_Value() == "Active")
5903
                    {
5904
                        LMConnector connector = dataSource.GetConnector(rep.Id);
5905
                        if (connector.ConnectItem1SymbolObject != null && connector.ConnectItem1SymbolObject.get_RepresentationType() != "Branch")
5906
                        {
5907
                            LMSymbol symbol = connector.ConnectItem1SymbolObject;
5908
                            List<string> modelItemIds = FindOtherModelItemBySymbolWhereTypePipeRun(symbol, modelItem.Id);
5909
                            if (modelItemIds.Count == 1)
5910
                            {
5911
                                string joinModelItemId = modelItemIds[0];
5912
                                JoinRun(joinModelItemId, modelItemId, ref survivorId, false);
5913
                                if (survivorId != null)
5914
                                    break;
5915
                            }
5916
                        }
5917
                        if (connector.ConnectItem2SymbolObject != null && connector.ConnectItem2SymbolObject.get_RepresentationType() != "Branch")
5918
                        {
5919
                            LMSymbol symbol = connector.ConnectItem2SymbolObject;
5920
                            List<string> modelItemIds = FindOtherModelItemBySymbolWhereTypePipeRun(symbol, modelItem.Id);
5921
                            if (modelItemIds.Count == 1)
5922
                            {
5923
                                string joinModelItemId = modelItemIds[0];
5924
                                JoinRun(joinModelItemId, modelItemId, ref survivorId, false);
5925
                                if (survivorId != null)
5926
                                    break;
5927
                            }
5928
                        }
5929
                    }
5930
                }
5931
            }
5932
        }
5933

    
5934
        /// <summary>
5935
        /// Label의 좌표를 구하는 메서드(ID2 기준의 좌표 -> SPPID 좌표)
5936
        /// </summary>
5937
        /// <param name="x"></param>
5938
        /// <param name="y"></param>
5939
        /// <param name="originX"></param>
5940
        /// <param name="originY"></param>
5941
        /// <param name="SPPIDLabelLocation"></param>
5942
        /// <param name="location"></param>
5943
        private void CalcLabelLocation(ref double x, ref double y, double originX, double originY, SPPIDLabelLocationInfo SPPIDLabelLocation, Location location)
5944
        {
5945
            if (location == Location.None)
5946
            {
5947
                x = originX;
5948
                y = originY;
5949
            }
5950
            else
5951
            {
5952
                if (location.HasFlag(Location.Center))
5953
                {
5954
                    x = (SPPIDLabelLocation.X1 + SPPIDLabelLocation.X2) / 2;
5955
                    y = (SPPIDLabelLocation.Y1 + SPPIDLabelLocation.Y2) / 2;
5956
                }
5957

    
5958
                if (location.HasFlag(Location.Left))
5959
                    x = SPPIDLabelLocation.X1;
5960
                else if (location.HasFlag(Location.Right))
5961
                    x = SPPIDLabelLocation.X2;
5962

    
5963
                if (location.HasFlag(Location.Down))
5964
                    y = SPPIDLabelLocation.Y1;
5965
                else if (location.HasFlag(Location.Up))
5966
                    y = SPPIDLabelLocation.Y2;
5967
            }
5968
        }
5969

    
5970
        /// <summary>
5971
        /// Symbol의 우선순위 Modeling 목록을 가져온다.
5972
        /// 1. Angle Valve
5973
        /// 2. 3개로 이루어진 Symbol Group
5974
        /// </summary>
5975
        /// <returns></returns>
5976
        private List<Symbol> GetPrioritySymbol()
5977
        {
5978
            DataTable symbolTable = document.SymbolTable;
5979
            // List에 순서대로 쌓는다.
5980
            List<Symbol> symbols = new List<Symbol>();
5981

    
5982
            // Angle Valve 부터
5983
            foreach (var symbol in document.SYMBOLS.FindAll(x => x.CONNECTORS.FindAll(y => y.Index == 0).Count == 2))
5984
            {
5985
                if (!symbols.Contains(symbol))
5986
                {
5987
                    double originX = 0;
5988
                    double originY = 0;
5989

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

    
5994
                    SlopeType slopeType1 = SlopeType.None;
5995
                    SlopeType slopeType2 = SlopeType.None;
5996
                    foreach (Connector connector in symbol.CONNECTORS.FindAll(x => x.Index == 0))
5997
                    {
5998
                        double connectorX = 0;
5999
                        double connectorY = 0;
6000
                        SPPIDUtil.ConvertPointBystring(connector.CONNECTPOINT, ref connectorX, ref connectorY);
6001
                        if (slopeType1 == SlopeType.None)
6002
                            slopeType1 = SPPIDUtil.CalcSlope(originX, originY, connectorX, connectorY);
6003
                        else
6004
                            slopeType2 = SPPIDUtil.CalcSlope(originX, originY, connectorX, connectorY);
6005
                    }
6006

    
6007
                    if ((slopeType1 == SlopeType.VERTICAL && slopeType2 == SlopeType.HORIZONTAL) ||
6008
                        (slopeType2 == SlopeType.VERTICAL && slopeType1 == SlopeType.HORIZONTAL))
6009
                        symbols.Add(symbol);
6010
                }
6011
            }
6012

    
6013
            List<Symbol> tempSymbols = new List<Symbol>();
6014
            // Conn 갯수 기준
6015
            foreach (var item in document.SYMBOLS)
6016
            {
6017
                if (!symbols.Contains(item))
6018
                    tempSymbols.Add(item);
6019
            }
6020
            tempSymbols.Sort(SortSymbolPriority);
6021
            symbols.AddRange(tempSymbols);
6022

    
6023
            return symbols;
6024
        }
6025

    
6026
        private void SetPriorityLine(List<Line> lines)
6027
        {
6028
            lines.Sort(SortLinePriority);
6029

    
6030
            int SortLinePriority(Line a, Line b)
6031
            {
6032
                // Branch 없는것부터
6033
                int branchRetval = CompareBranchLine(a, b);
6034
                if (branchRetval != 0)
6035
                {
6036
                    return branchRetval;
6037
                }
6038
                else
6039
                {
6040
                    // Symbol 연결 갯수
6041
                    int connSymbolRetval = CompareConnSymbol(a, b);
6042
                    if (connSymbolRetval != 0)
6043
                    {
6044
                        return connSymbolRetval;
6045
                    }
6046
                    else
6047
                    {
6048
                        // 아이템 연결 갯수(심볼, Line이면서 Not Branch)
6049
                        int connItemRetval = CompareConnItem(a, b);
6050
                        if (connItemRetval != 0)
6051
                        {
6052
                            return connItemRetval;
6053
                        }
6054
                        else
6055
                        {
6056
                            // ConnectedItem이 없는것
6057
                            int noneConnRetval = CompareNoneConn(a, b);
6058
                            if (noneConnRetval != 0)
6059
                            {
6060
                                return noneConnRetval;
6061
                            }
6062
                            else
6063
                            {
6064

    
6065
                            }
6066
                        }
6067
                    }
6068
                }
6069

    
6070
                return 0;
6071
            }
6072

    
6073
            int CompareNotSegmentLine(Line a, Line b)
6074
            {
6075
                List<Connector> connectorsA = a.CONNECTORS
6076
                    .Where(conn => conn.ConnectedObject != null && conn.ConnectedObject.GetType() == typeof(Symbol))
6077
                    .ToList();
6078

    
6079
                List<Connector> connectorsB = b.CONNECTORS
6080
                    .Where(conn => conn.ConnectedObject != null && conn.ConnectedObject.GetType() == typeof(Symbol))
6081
                    .ToList();
6082

    
6083
                // 오름차순
6084
                return connectorsB.Count.CompareTo(connectorsA.Count);
6085
            }
6086

    
6087
            int CompareConnSymbol(Line a, Line b)
6088
            {
6089
                List<Connector> connectorsA = a.CONNECTORS
6090
                    .Where(conn => conn.ConnectedObject != null && conn.ConnectedObject.GetType() == typeof(Symbol))
6091
                    .ToList();
6092

    
6093
                List<Connector> connectorsB = b.CONNECTORS
6094
                    .Where(conn => conn.ConnectedObject != null && conn.ConnectedObject.GetType() == typeof(Symbol))
6095
                    .ToList();
6096

    
6097
                // 오름차순
6098
                return connectorsB.Count.CompareTo(connectorsA.Count);
6099
            }
6100

    
6101
            int CompareConnItem(Line a, Line b)
6102
            {
6103
                List<Connector> connectorsA = a.CONNECTORS
6104
                    .Where(conn => conn.ConnectedObject != null &&
6105
                    (conn.ConnectedObject.GetType() == typeof(Symbol) ||
6106
                    (conn.ConnectedObject.GetType() == typeof(Line) && !SPPIDUtil.IsBranchLine((Line)conn.ConnectedObject, a))))
6107
                    .ToList();
6108

    
6109
                List<Connector> connectorsB = b.CONNECTORS
6110
                    .Where(conn => conn.ConnectedObject != null &&
6111
                    (conn.ConnectedObject.GetType() == typeof(Symbol) ||
6112
                    (conn.ConnectedObject.GetType() == typeof(Line) && !SPPIDUtil.IsBranchLine((Line)conn.ConnectedObject, b))))
6113
                    .ToList();
6114

    
6115
                // 오름차순
6116
                return connectorsB.Count.CompareTo(connectorsA.Count);
6117
            }
6118

    
6119
            int CompareBranchLine(Line a, Line b)
6120
            {
6121
                List<Connector> connectorsA = a.CONNECTORS
6122
                    .Where(conn => conn.ConnectedObject != null && conn.ConnectedObject.GetType() == typeof(Line) && SPPIDUtil.IsBranchLine(a, conn.ConnectedObject as Line))
6123
                    .ToList();
6124
                List<Connector> connectorsB = b.CONNECTORS
6125
                    .Where(conn => conn.ConnectedObject != null && conn.ConnectedObject.GetType() == typeof(Line) && SPPIDUtil.IsBranchLine(b, conn.ConnectedObject as Line))
6126
                    .ToList();
6127

    
6128
                // 내림차순
6129
                return connectorsA.Count.CompareTo(connectorsB.Count);
6130
            }
6131

    
6132
            int CompareNoneConn(Line a, Line b)
6133
            {
6134
                List<Connector> connectorsA = a.CONNECTORS
6135
                    .Where(conn => conn.ConnectedObject == null)
6136
                    .ToList();
6137

    
6138
                List<Connector> connectorsB = b.CONNECTORS
6139
                    .Where(conn => conn.ConnectedObject == null)
6140
                    .ToList();
6141

    
6142
                // 오름차순
6143
                return connectorsB.Count.CompareTo(connectorsA.Count);
6144
            }
6145
        }
6146

    
6147
        private void SortText(List<Text> texts)
6148
        {
6149
            texts.Sort(Sort);
6150

    
6151
            int Sort(Text a, Text b)
6152
            {
6153
                int yRetval = CompareY(a, b);
6154
                if (yRetval != 0)
6155
                {
6156
                    return yRetval;
6157
                }
6158
                else
6159
                {
6160
                    return CompareX(a, b);
6161
                }
6162
            }
6163

    
6164
            int CompareY(Text a, Text b)
6165
            {
6166
                return a.LOCATION_Y.CompareTo(b.LOCATION_Y);
6167
            }
6168

    
6169
            int CompareX(Text a, Text b)
6170
            {
6171
                return a.LOCATION_X.CompareTo(b.LOCATION_X);
6172
            }
6173
        }
6174
        private void SortNote(List<Note> notes)
6175
        {
6176
            notes.Sort(Sort);
6177

    
6178
            int Sort(Note a, Note b)
6179
            {
6180
                int yRetval = CompareY(a, b);
6181
                if (yRetval != 0)
6182
                {
6183
                    return yRetval;
6184
                }
6185
                else
6186
                {
6187
                    return CompareX(a, b);
6188
                }
6189
            }
6190

    
6191
            int CompareY(Note a, Note b)
6192
            {
6193
                return a.LOCATION_Y.CompareTo(b.LOCATION_Y);
6194
            }
6195

    
6196
            int CompareX(Note a, Note b)
6197
            {
6198
                return a.LOCATION_X.CompareTo(b.LOCATION_X);
6199
            }
6200
        }
6201

    
6202
        private void SortBranchLines()
6203
        {
6204
            BranchLines.Sort(SortBranchLine);
6205
            int SortBranchLine(Line a, Line b)
6206
            {
6207
                int countA = a.CONNECTORS.FindAll(x => x.ConnectedObject != null &&
6208
                 x.ConnectedObject.GetType() == typeof(Line) &&
6209
                 SPPIDUtil.IsBranchLine(x.ConnectedObject as Line, a) &&
6210
                 string.IsNullOrEmpty(((Line)x.ConnectedObject).SPPID.ModelItemId)).Count;
6211

    
6212
                int countB = b.CONNECTORS.FindAll(x => x.ConnectedObject != null &&
6213
                 x.ConnectedObject.GetType() == typeof(Line) &&
6214
                 SPPIDUtil.IsBranchLine(x.ConnectedObject as Line, b) &&
6215
                 string.IsNullOrEmpty(((Line)x.ConnectedObject).SPPID.ModelItemId)).Count;
6216

    
6217
                // 내림차순
6218
                return countA.CompareTo(countB);
6219
            }
6220
        }
6221

    
6222
        private static int SortSymbolPriority(Symbol a, Symbol b)
6223
        {
6224
            int countA = a.CONNECTORS.FindAll(x => !string.IsNullOrEmpty(x.CONNECTEDITEM) && x.CONNECTEDITEM != "None").Count;
6225
            int countB = b.CONNECTORS.FindAll(x => !string.IsNullOrEmpty(x.CONNECTEDITEM) && x.CONNECTEDITEM != "None").Count;
6226
            int retval = countB.CompareTo(countA);
6227
            if (retval != 0)
6228
                return retval;
6229
            else
6230
                return a.SPPID.ORIGINAL_X.CompareTo(b.SPPID.ORIGINAL_X);
6231
        }
6232

    
6233
        private string GetSPPIDFileName(LMModelItem modelItem)
6234
        {
6235
            string symbolPath = null;
6236
            foreach (LMRepresentation rep in modelItem.Representations)
6237
            {
6238
                if (!DBNull.Value.Equals(rep.get_FileName()) && !string.IsNullOrEmpty(rep.get_FileName()))
6239
                {
6240
                    symbolPath = rep.get_FileName();
6241
                    break;
6242
                }
6243
            }
6244
            return symbolPath;
6245
        }
6246

    
6247
        private string GetSPPIDFileName(string modelItemId)
6248
        {
6249
            LMModelItem modelItem = dataSource.GetModelItem(modelItemId);
6250
            string symbolPath = null;
6251
            foreach (LMRepresentation rep in modelItem.Representations)
6252
            {
6253
                if (!DBNull.Value.Equals(rep.get_FileName()) && !string.IsNullOrEmpty(rep.get_FileName()))
6254
                {
6255
                    symbolPath = rep.get_FileName();
6256
                    break;
6257
                }
6258
            }
6259
            ReleaseCOMObjects(modelItem);
6260
            return symbolPath;
6261
        }
6262

    
6263
        /// <summary>
6264
        /// Graphic OID로 해당 Symbol의 크기를 구하여 Zoom
6265
        /// </summary>
6266
        /// <param name="graphicOID"></param>
6267
        /// <param name="milliseconds"></param>
6268
        private void ZoomObjectByGraphicOID(string graphicOID, int milliseconds = 150)
6269
        {
6270
            if (radApp.ActiveDocument.ActiveSheet.DrawingObjects[graphicOID] != null)
6271
            {
6272
                double minX = 0;
6273
                double minY = 0;
6274
                double maxX = 0;
6275
                double maxY = 0;
6276
                radApp.ActiveDocument.ActiveSheet.DrawingObjects[graphicOID].Range(out minX, out minY, out maxX, out maxY);
6277
                radApp.ActiveWindow.ZoomArea2(minX - 0.007, minY - 0.007, maxX + 0.007, maxY + 0.007, null);
6278

    
6279
                Thread.Sleep(milliseconds);
6280
            }
6281
        }
6282

    
6283
        /// <summary>
6284
        /// ComObject를 Release
6285
        /// </summary>
6286
        /// <param name="objVars"></param>
6287
        public void ReleaseCOMObjects(params object[] objVars)
6288
        {
6289
            if (objVars != null)
6290
            {
6291
                int intNewRefCount = 0;
6292
                foreach (object obj in objVars)
6293
                {
6294
                    if (!Information.IsNothing(obj) && System.Runtime.InteropServices.Marshal.IsComObject(obj))
6295
                        intNewRefCount = intNewRefCount + System.Runtime.InteropServices.Marshal.FinalReleaseComObject(obj);
6296
                }
6297
            }
6298
        }
6299

    
6300
        /// IDisposable 구현
6301
        ~AutoModeling()
6302
        {
6303
            this.Dispose(false);
6304
        }
6305

    
6306
        private bool disposed;
6307
        public void Dispose()
6308
        {
6309
            this.Dispose(true);
6310
            GC.SuppressFinalize(this);
6311
        }
6312

    
6313
        protected virtual void Dispose(bool disposing)
6314
        {
6315
            if (this.disposed) return;
6316
            if (disposing)
6317
            {
6318
                // IDisposable 인터페이스를 구현하는 멤버들을 여기서 정리합니다.
6319
            }
6320
            // .NET Framework에 의하여 관리되지 않는 외부 리소스들을 여기서 정리합니다.
6321
            this.disposed = true;
6322
        }
6323
    }
6324
}
클립보드 이미지 추가 (최대 크기: 500 MB)