프로젝트

일반

사용자정보

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

hytos / DTI_PID / SPPIDConverter / AutoModeling.cs @ 588b4713

이력 | 보기 | 이력해설 | 다운로드 (297 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 Ingr.RAD2D.Interop.RAD2D;
8
using Ingr.RAD2D.Internal;
9
using Ingr.RAD2D.Helper;
10
using Converter.BaseModel;
11
using Converter.SPPID.Model;
12
using Converter.SPPID.Properties;
13
using Converter.SPPID.Util;
14
using Converter.SPPID.DB;
15
using Ingr.RAD2D.MacroControls.CmdCtrl;
16
using Ingr.RAD2D;
17
using System.Windows;
18
using System.Threading;
19
using System.Drawing;
20
using Microsoft.VisualBasic;
21
using Newtonsoft.Json;
22
using DevExpress.XtraSplashScreen;
23
using Ingr.SPPID.Llama;
24
using Ingr.SPPID.Plaice;
25

    
26
namespace Converter.SPPID
27
{
28
    [Flags]
29
    public enum SegmentLocation
30
    {
31
        None = 0,
32
        Right = 1,
33
        Left = 2,
34
        Down = 4,
35
        Up = 8
36
    }
37
    public class AutoModeling : IDisposable
38
    {
39
        Placement _placement;
40
        LMADataSource dataSource;
41
        string drawingID;
42
        Ingr.SPPID.PIDAuto.Drawing newDrawing;
43
        dynamic application;
44
        bool closeDocument;
45
        Ingr.RAD2D.Application radApp;
46
        Ingr.SPPID.PIDAuto.Application pidApp;
47
        SPPID_Document document;
48
        ETCSetting _ETCSetting;
49
        DataTable nominalDiameterTable = null;
50
        public string DocumentLabelText { get; set; }
51

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

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

    
65
            pidApp = new Ingr.SPPID.PIDAuto.Application();
66

    
67
            this.closeDocument = closeDocument;
68
            this.document = document;
69
            this._ETCSetting = ETCSetting.GetInstance();
70
        }
71

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

    
84
                }
85
            }
86
        }
87

    
88
        /// <summary>
89
        /// 도면 단위당 실행되는 메서드
90
        /// </summary>
91
        public void Run()
92
        {
93
            string drawingNumber = document.DrawingNumber;
94
            string drawingName = document.DrawingName;
95
            try
96
            {
97
                nominalDiameterTable = Project_DB.SelectProjectNominalDiameter();
98
                _placement = new Placement();
99
                dataSource = _placement.PIDDataSource;
100

    
101
                if (CreateDocument(ref drawingNumber, ref drawingName) && DocumentCoordinateCorrection())
102
                {
103
                    Log.Write("Start Modeling");
104
                    SplashScreenManager.ShowForm(typeof(SPPIDSplashScreen), true, true);
105
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetParent, (IntPtr)radApp.HWnd);
106
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetAllStepCount, 25);
107
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetDocumentName, DocumentLabelText);
108

    
109
                    // VendorPackage Modeling
110
                    RunVendorPackageModeling();
111
                    // Equipment Modeling
112
                    RunEquipmentModeling();
113
                    // Symbol Modeling
114
                    RunSymbolModeling();
115
                    // LineRun Line Modeling
116
                    RunLineModeling();
117
                    // Vent Drain Modeling
118
                    RunVentDrainModeling();
119
                    // Clear Attribute
120
                    RunClearNominalDiameter();
121
                    // Join SameConnector
122
                    RunJoinRunForSameConnector();
123
                    // Join Run
124
                    RunJoinRun();
125
                    // EndBreak Modeling
126
                    RunEndBreakModeling();
127
                    // SpecBreak Modeling
128
                    RunSpecBreakModeling();
129
                    //Line Number Modeling
130
                    RunLineNumberModeling();
131
                    // Note Modeling
132
                    RunNoteModeling();
133
                    // Text Modeling
134
                    RunTextModeling();
135
                    // Input LineNumber Attribute
136
                    RunInputLineNumberAttribute();
137
                    // Input Symbol Attribute
138
                    RunInputSymbolAttribute();
139
                    // Input SpecBreak Attribute
140
                    RunInputSpecBreakAttribute();
141
                    // Input EndBreak Attribute
142
                    RunInputEndBreakAttribute();
143
                    // Label Symbol Modeling
144
                    RunLabelSymbolModeling();
145
                    // Correct Text
146
                    RunCorrectAssociationText();
147
                    // ETC
148
                    RunETC();
149
                    // BulkAttribute
150
                    RunBulkAttribute();
151
                    // Result Logging
152
                    document.CheckModelingResult();
153
                }
154
            }
155
            catch (Exception ex)
156
            {
157
                if (SplashScreenManager.Default != null && SplashScreenManager.Default.IsSplashFormVisible)
158
                {
159
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.ClearParent, null);
160
                    SplashScreenManager.CloseForm(false);
161
                    Log.Write("\r\n");
162
                }
163
                System.Windows.Forms.MessageBox.Show(ex.Message + "\r\n" + ex.StackTrace);
164
            }
165
            finally
166
            {
167
                Project_DB.InsertDrawingInfoAndOPCInfo(document.PATH, drawingNumber, drawingName, document);
168
                //Project_DB.InsertLineNumberInfo(document.PATH, drawingNumber, drawingName, document);
169

    
170
                if (SplashScreenManager.Default != null && SplashScreenManager.Default.IsSplashFormVisible)
171
                {
172
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.ClearParent, null);
173
                    SplashScreenManager.CloseForm(false);
174
                    Log.Write("\r\n");
175
                }
176
                Thread.Sleep(1000);
177

    
178
                Log.Write("End Modeling");
179
                
180
                // TODO : ZOOM FIT
181
                //radApp.ActiveDocument.ActiveSheet.Fit();
182

    
183
                ReleaseCOMObjects(application);
184
                application = null;
185
                if (radApp.ActiveDocument != null)
186
                {
187
                    if (closeDocument && newDrawing != null)
188
                    {
189
                        newDrawing.Save();
190
                        newDrawing.CloseDrawing(true);
191
                        newDrawing = null;
192
                    }
193
                    else if (newDrawing == null)
194
                    {
195
                        Log.Write("error document");
196
                    }
197
                }
198

    
199
                ReleaseCOMObjects(dataSource);
200
                dataSource = null;
201
                ReleaseCOMObjects(_placement);
202
                _placement = null;
203

    
204
                Thread.Sleep(1000);
205
            }
206
        }
207

    
208
        private void RunVendorPackageModeling()
209
        {
210
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetAllProgress, document.VendorPackages.Count);
211
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "VendorPackages Modeling");
212
            foreach (VendorPackage item in document.VendorPackages)
213
            {
214
                try
215
                {
216
                    VendorPackageModeling(item);
217
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
218
                }
219
                catch (Exception ex)
220
                {
221
                    Log.Write("Error in RunVendorPackageModeling");
222
                    Log.Write("UID : " + item.UID);
223
                    Log.Write(ex.Message);
224
                    Log.Write(ex.StackTrace);
225
                }
226
            }
227
        }
228
        private void RunEquipmentModeling()
229
        {
230
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetAllProgress, document.Equipments.Count);
231
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Equipments Modeling");
232
            for (int i = 0; i < document.Equipments.Count; i++)
233
            {
234
                Equipment item = document.Equipments[i];
235
                try
236
                {
237
                    if (!string.IsNullOrEmpty(item.SPPID.RepresentationId))
238
                        continue;
239
                    EquipmentModeling(item);
240
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetProgress, document.Equipments.FindAll(x => !string.IsNullOrEmpty(x.SPPID.RepresentationId)).Count);
241
                    if (!string.IsNullOrEmpty(item.SPPID.RepresentationId))
242
                        i = -1;
243
                }
244
                catch (Exception ex)
245
                {
246
                    Log.Write("Error in EquipmentModeling");
247
                    Log.Write("UID : " + item.UID);
248
                    Log.Write(ex.Message);
249
                    Log.Write(ex.StackTrace);
250
                }
251
            }
252
        }
253
        private void RunSymbolModeling()
254
        {
255
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetAllProgress, document.SYMBOLS.Count);
256
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Symbol Modeling");
257
            prioritySymbols = GetPrioritySymbol();
258
            foreach (var item in prioritySymbols)
259
            {
260
                try
261
                {
262
                    if (document.VentDrainSymbol.Contains(item))
263
                        continue;
264
                    SymbolModelingBySymbol(item);
265
                }
266
                catch (Exception ex)
267
                {
268
                    Log.Write("Error in SymbolModelingByPriority");
269
                    Log.Write("UID : " + item.UID);
270
                    Log.Write(ex.Message);
271
                    Log.Write(ex.StackTrace);
272
                }
273
            }
274
        }
275
        private void RunLineModeling()
276
        {
277
            List<Line> AllLine = document.LINES.ToList();
278
            List<Line> stepLast_Line = document.LINES.FindAll(x => x.CONNECTORS.FindAll(y => y.ConnectedObject != null && y.ConnectedObject.GetType() == typeof(Symbol)).Count == 2 &&
279
            !SPPIDUtil.IsBranchedLine(document, x));
280
            List<Line> step1_Line = AllLine.FindAll(x => !stepLast_Line.Contains(x));
281

    
282
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetAllProgress, step1_Line.Count);
283
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Line Modeling - 1");
284

    
285
            SetPriorityLine(step1_Line);
286
            foreach (var item in step1_Line)
287
            {
288
                try
289
                {
290
                    if (document.VentDrainLine.Contains(item))
291
                        continue;
292
                    NewLineModeling(item);
293
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
294
                }
295
                catch (Exception ex)
296
                {
297
                    Log.Write("Error in NewLineModeling");
298
                    Log.Write("UID : " + item.UID);
299
                    Log.Write(ex.Message);
300
                    Log.Write(ex.StackTrace);
301
                }
302
            }
303

    
304
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetAllProgress, BranchLines.Count);
305
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Line Modeling - 2");
306
            int branchCount = BranchLines.Count;
307
            while (BranchLines.Count > 0)
308
            {
309
                try
310
                {
311
                    SortBranchLines();
312
                    Line item = BranchLines[0];
313
                    NewLineModeling(item, true);
314
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
315
                }
316
                catch (Exception ex)
317
                {
318
                    Log.Write("Error in NewLineModeling");
319
                    Log.Write("UID : " + BranchLines[0].UID);
320
                    Log.Write(ex.Message);
321
                    Log.Write(ex.StackTrace);
322
                    BranchLines.Remove(BranchLines[0]);
323
                }
324
            }
325

    
326
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetAllProgress, stepLast_Line.Count);
327
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Line Modeling - 3");
328
            foreach (var item in stepLast_Line)
329
            {
330
                try
331
                {
332
                    if (document.VentDrainLine.Contains(item))
333
                        continue;
334
                    NewLineModeling(item);
335
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
336
                }
337
                catch (Exception ex)
338
                {
339
                    Log.Write("Error in NewLineModeling");
340
                    Log.Write("UID : " + item.UID);
341
                    Log.Write(ex.Message);
342
                    Log.Write(ex.StackTrace);
343
                }
344
            }
345
        }
346
        private void RunVentDrainModeling()
347
        {
348
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetAllProgress, document.VentDrainLine.Count);
349
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Vent Drain Modeling");
350
            foreach (var item in document.VentDrainLine)
351
            {
352
                try
353
                {
354
                    Connector connector = item.CONNECTORS.Find(x => x.ConnectedObject != null && x.ConnectedObject.GetType() == typeof(Symbol));
355
                    if (connector != null)
356
                    {
357
                        SetCoordinate();
358
                        Symbol connSymbol = connector.ConnectedObject as Symbol;
359
                        SymbolModeling(connSymbol, null);
360
                        NewLineModeling(item, true);
361

    
362
                        GridSetting grid = GridSetting.GetInstance();
363
                        int count = grid.DrainValveCellCount;
364
                        double length = grid.Length;
365

    
366
                        // 길이 확인
367
                        if (!string.IsNullOrEmpty(item.SPPID.ModelItemId))
368
                        {
369
                            LMConnector _LMConnector = GetLMConnectorOnlyOne(item.SPPID.ModelItemId);
370
                            if (_LMConnector != null)
371
                            {
372
                                double[] connectorRange = GetConnectorRange(_LMConnector);
373
                                double connectorLength = double.NaN;
374
                                if (item.SlopeType == SlopeType.HORIZONTAL)
375
                                    connectorLength = connectorRange[2] - connectorRange[0];
376
                                else if (item.SlopeType == SlopeType.VERTICAL)
377
                                    connectorLength = connectorRange[3] - connectorRange[1];
378

    
379
                                if (!double.IsNaN(connectorLength) && connectorLength != count * length)
380
                                {
381
                                    double move = count * length - connectorLength;
382
                                    List<Symbol> group = new List<Symbol>();
383
                                    SPPIDUtil.FindConnectedSymbolGroup(document, connSymbol, group);
384
                                    foreach (var symbol in group)
385
                                    {
386
                                        int connSymbolIndex = item.CONNECTORS.IndexOf(item.CONNECTORS.Find(x => x.ConnectedObject == connSymbol));
387
                                        if (item.SlopeType == SlopeType.HORIZONTAL)
388
                                        {
389
                                            if (connSymbolIndex == 0)
390
                                            {
391
                                                if (item.SPPID.START_X > item.SPPID.END_X)
392
                                                    symbol.SPPID.ORIGINAL_X = symbol.SPPID.ORIGINAL_X + move;
393
                                                else
394
                                                    symbol.SPPID.ORIGINAL_X = symbol.SPPID.ORIGINAL_X - move;
395
                                            }
396
                                            else
397
                                            {
398
                                                if (item.SPPID.START_X < item.SPPID.END_X)
399
                                                    symbol.SPPID.ORIGINAL_X = symbol.SPPID.ORIGINAL_X + move;
400
                                                else
401
                                                    symbol.SPPID.ORIGINAL_X = symbol.SPPID.ORIGINAL_X - move;
402
                                            }
403
                                        }
404
                                        else if (item.SlopeType == SlopeType.VERTICAL)
405
                                        {
406
                                            if (connSymbolIndex == 0)
407
                                            {
408
                                                if (item.SPPID.START_Y > item.SPPID.END_Y)
409
                                                    symbol.SPPID.ORIGINAL_Y = symbol.SPPID.ORIGINAL_Y + move;
410
                                                else
411
                                                    symbol.SPPID.ORIGINAL_Y = symbol.SPPID.ORIGINAL_Y - move;
412
                                            }
413
                                            else
414
                                            {
415
                                                if (item.SPPID.START_Y < item.SPPID.END_Y)
416
                                                    symbol.SPPID.ORIGINAL_Y = symbol.SPPID.ORIGINAL_Y + move;
417
                                                else
418
                                                    symbol.SPPID.ORIGINAL_Y = symbol.SPPID.ORIGINAL_Y - move;
419
                                            }
420
                                        }
421
                                    }
422

    
423
                                    // 제거
424
                                    RemoveSymbol(connSymbol);
425
                                    RemoveLine(item);
426

    
427
                                    // 재생성
428
                                    SymbolModelingBySymbol(connSymbol);
429
                                    NewLineModeling(item, true);
430
                                }
431
                            }
432

    
433
                            ReleaseCOMObjects(_LMConnector);
434
                            _LMConnector = null;
435
                        }
436
                    }
437
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
438
                }
439
                catch (Exception ex)
440
                {
441
                    Log.Write("Error in NewLineModeling");
442
                    Log.Write("UID : " + item.UID);
443
                    Log.Write(ex.Message);
444
                    Log.Write(ex.StackTrace);
445
                }
446

    
447
                void SetCoordinate()
448
                {
449
                    Connector branchConnector = item.CONNECTORS.Find(loop => loop.ConnectedObject != null && loop.ConnectedObject.GetType() == typeof(Line));
450
                    if (branchConnector != null)
451
                    {
452
                        Line connLine = branchConnector.ConnectedObject as Line;
453
                        double x = 0;
454
                        double y = 0;
455
                        GetTargetLineConnectorPoint(branchConnector, item, ref x, ref y);
456
                        LMConnector targetConnector = FindTargetLMConnectorForBranch(item, connLine, ref x, ref y);
457
                        if (targetConnector != null)
458
                        {
459
                            List<Symbol> group = new List<Symbol>();
460
                            SPPIDUtil.FindConnectedSymbolGroup(document, item.CONNECTORS.Find(loop => loop != branchConnector).ConnectedObject as Symbol, group);
461
                            if (item.SlopeType == SlopeType.HORIZONTAL)
462
                            {
463
                                item.SPPID.START_Y = y;
464
                                item.SPPID.END_Y = y;
465
                                foreach (var symbol in group)
466
                                {
467
                                    symbol.SPPID.ORIGINAL_Y = y;
468
                                    symbol.SPPID.SPPID_Y = y;
469
                                }
470
                            }
471
                            else if (item.SlopeType == SlopeType.VERTICAL)
472
                            {
473
                                item.SPPID.START_X = x;
474
                                item.SPPID.END_X = x;
475
                                foreach (var symbol in group)
476
                                {
477
                                    symbol.SPPID.ORIGINAL_X = x;
478
                                    symbol.SPPID.SPPID_X = x;
479
                                }
480
                            }
481
                        }
482
                        ReleaseCOMObjects(targetConnector);
483
                        targetConnector = null;
484
                    }
485
                }
486
            }
487
        }
488
        private void RunClearNominalDiameter()
489
        {
490
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetAllProgress, document.SYMBOLS.Count + document.LINES.Count);
491
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Clear Attribute");
492
            return;
493

    
494
            List<string> endClearModelItemID = new List<string>();
495
            for (int i = 0; i < document.LINES.Count; i++)
496
            {
497
                Line item = document.LINES[i];
498
                string modelItemID = item.SPPID.ModelItemId;
499
                if (!string.IsNullOrEmpty(modelItemID))
500
                {
501
                    LMModelItem modelItem = dataSource.GetModelItem(modelItemID);
502
                    if (modelItem != null)
503
                    {
504
                        LMAAttribute attribute = modelItem.Attributes["NominalDiameter"];
505
                        if (attribute != null)
506
                            attribute.Value = DBNull.Value;
507

    
508
                        modelItem.Commit();
509
                        ReleaseCOMObjects(modelItem);
510
                        modelItem = null;
511
                    }
512
                }
513
                if (!endClearModelItemID.Contains(modelItemID))
514
                    endClearModelItemID.Add(modelItemID);
515
                SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
516
            }
517
            for (int i = 0; i < document.SYMBOLS.Count; i++)
518
            {
519
                Symbol item = document.SYMBOLS[i];
520
                string repID = item.SPPID.RepresentationId;
521
                string modelItemID = item.SPPID.ModelItemID;
522
                if (!string.IsNullOrEmpty(modelItemID))
523
                {
524
                    LMModelItem modelItem = dataSource.GetModelItem(modelItemID);
525
                    if (modelItem != null)
526
                    {
527
                        LMAAttribute attribute = modelItem.Attributes["NominalDiameter"];
528
                        if (attribute != null)
529
                            attribute.Value = DBNull.Value;
530
                        int index = 1;
531
                        while (true)
532
                        {
533
                            attribute = modelItem.Attributes[string.Format("PipingPoint{0}.NominalDiameter", index)];
534
                            if (attribute != null)
535
                                attribute.Value = DBNull.Value;
536
                            else
537
                                break;
538
                            index++;
539
                        }
540
                        modelItem.Commit();
541
                        ReleaseCOMObjects(modelItem);
542
                        modelItem = null;
543
                    }
544
                }
545
                if (!string.IsNullOrEmpty(repID))
546
                {
547
                    LMSymbol symbol = dataSource.GetSymbol(repID);
548
                    if (symbol != null)
549
                    {
550
                        foreach (LMConnector connector in symbol.Connect1Connectors)
551
                        {
552
                            if (connector.ItemStatus.ToString() == "Active" && !endClearModelItemID.Contains(connector.ModelItemID))
553
                            {
554
                                endClearModelItemID.Add(connector.ModelItemID.ToString());
555
                                LMModelItem modelItem = connector.ModelItemObject;
556
                                if (modelItem != null)
557
                                {
558
                                    LMAAttribute attribute = modelItem.Attributes["NominalDiameter"];
559
                                    if (attribute != null)
560
                                        attribute.Value = DBNull.Value;
561

    
562
                                    modelItem.Commit();
563
                                    ReleaseCOMObjects(modelItem);
564
                                    modelItem = null;
565
                                }
566
                            }
567
                        }
568
                        foreach (LMConnector connector in symbol.Connect2Connectors)
569
                        {
570
                            if (connector.ItemStatus.ToString() == "Active" && !endClearModelItemID.Contains(connector.ModelItemID))
571
                            {
572
                                endClearModelItemID.Add(connector.ModelItemID.ToString());
573
                                LMModelItem modelItem = connector.ModelItemObject;
574
                                if (modelItem != null)
575
                                {
576
                                    LMAAttribute attribute = modelItem.Attributes["NominalDiameter"];
577
                                    if (attribute != null)
578
                                        attribute.Value = DBNull.Value;
579

    
580
                                    modelItem.Commit();
581
                                    ReleaseCOMObjects(modelItem);
582
                                    modelItem = null;
583
                                }
584
                            }
585
                        }
586
                    }
587
                    ReleaseCOMObjects(symbol);
588
                    symbol = null;
589
                }
590
                SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
591
            }
592
        }
593
        private void RunClearValueInconsistancy()
594
        {
595
            int count = 1;
596
            bool loop = true;
597
            while (loop)
598
            {
599
                loop = false;
600
                LMAFilter filter = new LMAFilter();
601
                LMACriterion criterion = new LMACriterion();
602
                filter.ItemType = "Relationship";
603
                criterion.SourceAttributeName = "SP_DRAWINGID";
604
                criterion.Operator = "=";
605
                criterion.ValueAttribute = drawingID;
606
                filter.Criteria.Add(criterion);
607

    
608
                LMRelationships relationships = new LMRelationships();
609
                relationships.Collect(dataSource, Filter: filter);
610

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

    
752
            foreach (var line in document.LINES)
753
            {
754
                foreach (var connector in line.CONNECTORS)
755
                {
756
                    if (connector.ConnectedObject != null &&
757
                        connector.ConnectedObject.GetType() == typeof(Line) &&
758
                        !SPPIDUtil.IsBranchLine(line, connector.ConnectedObject as Line))
759
                    {
760
                        Line connLine = connector.ConnectedObject as Line;
761
                        if (line.SPPID.ModelItemId != connLine.SPPID.ModelItemId &&
762
                            !string.IsNullOrEmpty(line.SPPID.ModelItemId) &&
763
                            !string.IsNullOrEmpty(connLine.SPPID.ModelItemId) &&
764
                            !SPPIDUtil.IsSegment(document, line, connLine))
765
                        {
766
                            string survivorId = string.Empty;
767
                            JoinRun(connLine.SPPID.ModelItemId, line.SPPID.ModelItemId, ref survivorId);
768
                        }
769

    
770
                    }
771
                }
772
                SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
773
            }
774

    
775
            foreach (var line in document.LINES)
776
                line.SPPID.Representations = GetRepresentations(line.SPPID.ModelItemId);
777
        }
778
        private void RunJoinRun()
779
        {
780
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetAllProgress, document.LINES.Count);
781
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "PipeRun Join - 2");
782
            List<string> endModelID = new List<string>();
783
            foreach (var line in document.LINES)
784
            {
785
                if (!endModelID.Contains(line.SPPID.ModelItemId))
786
                {
787
                    while (!endModelID.Contains(line.SPPID.ModelItemId))
788
                    {
789
                        string survivorId = string.Empty;
790
                        JoinRunBySameType(line.SPPID.ModelItemId, ref survivorId);
791
                        if (string.IsNullOrEmpty(survivorId))
792
                        {
793
                            endModelID.Add(line.SPPID.ModelItemId);
794
                        }
795
                    }
796
                }
797
                SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
798
            }
799
        }
800
        private void RunLineNumberModeling()
801
        {
802
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetAllProgress, document.LINENUMBERS.Count);
803
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Line Number Modeling");
804
            foreach (var item in document.LINENUMBERS)
805
            {
806
                LMLabelPersist label = dataSource.GetLabelPersist(item.SPPID.RepresentationId);
807
                if (label == null || (label != null && label.ItemStatus.ToString() != "Active"))
808
                {
809
                    ReleaseCOMObjects(label);
810
                    item.SPPID.RepresentationId = null;
811
                    LineNumberModeling(item);
812
                }
813
                SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
814
            }
815
        }
816
        private void RunNoteModeling()
817
        {
818
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetAllProgress, document.SYMBOLS.Count);
819
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Notes Modeling");
820
            List<Note> correctList = new List<Note>();
821
            foreach (var item in document.NOTES)
822
                try
823
                {
824
                    NoteModeling(item, correctList);
825
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
826
                }
827
                catch (Exception ex)
828
                {
829
                    Log.Write("Error in NoteModeling");
830
                    Log.Write("UID : " + item.UID);
831
                    Log.Write(ex.Message);
832
                    Log.Write(ex.StackTrace);
833
                }
834

    
835
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetAllProgress, correctList.Count);
836
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Correct Note");
837
            SortNote(correctList);
838
            List<Note> endList = new List<Note>();
839
            if (correctList.Count > 0)
840
                endList.Add(correctList[0]);
841
            foreach (var item in correctList)
842
                try
843
                {
844
                    if (!endList.Contains(item))
845
                        NoteCorrectModeling(item, endList);
846
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
847
                }
848
                catch (Exception ex)
849
                {
850
                    Log.Write("Error in NoteModeling");
851
                    Log.Write("UID : " + item.UID);
852
                    Log.Write(ex.Message);
853
                    Log.Write(ex.StackTrace);
854
                }
855
        }
856
        private void RunTextModeling()
857
        {
858
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetAllProgress, document.TEXTINFOS.Count);
859
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Texts Modeling");
860
            SortText(document.TEXTINFOS);
861
            foreach (var item in document.TEXTINFOS)
862
                try
863
                {
864
                    if (item.ASSOCIATION)
865
                        AssociationTextModeling(item);
866
                    else
867
                        NormalTextModeling(item);
868
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
869
                }
870
                catch (Exception ex)
871
                {
872
                    Log.Write("Error in TextModeling");
873
                    Log.Write("UID : " + item.UID);
874
                    Log.Write(ex.Message);
875
                    Log.Write(ex.StackTrace);
876
                }
877
        }
878
        private void RunInputLineNumberAttribute()
879
        {
880
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetAllProgress, document.LINENUMBERS.Count);
881
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Set LineNumbers Attribute");
882
            List<string> endLine = new List<string>();
883
            foreach (var item in document.LINENUMBERS)
884
                try
885
                {
886
                    InputLineNumberAttribute(item, endLine);
887
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
888
                }
889
                catch (Exception ex)
890
                {
891
                    Log.Write("Error in InputLineNumberAttribute");
892
                    Log.Write("UID : " + item.UID);
893
                    Log.Write(ex.Message);
894
                    Log.Write(ex.StackTrace);
895
                }
896
        }
897
        private void RunInputSymbolAttribute()
898
        {
899
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetAllProgress, document.SYMBOLS.Count + document.Equipments.Count + document.LINES.Count);
900
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Set Symbols Attribute");
901
            foreach (var item in document.SYMBOLS)
902
                try
903
                {
904
                    InputSymbolAttribute(item, item.ATTRIBUTES);
905
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
906
                }
907
                catch (Exception ex)
908
                {
909
                    Log.Write("Error in InputSymbolAttribute");
910
                    Log.Write("UID : " + item.UID);
911
                    Log.Write(ex.Message);
912
                    Log.Write(ex.StackTrace);
913
                }
914

    
915
            foreach (var item in document.Equipments)
916
                try
917
                {
918
                    InputSymbolAttribute(item, item.ATTRIBUTES);
919
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
920
                }
921
                catch (Exception ex)
922
                {
923
                    Log.Write("Error in InputSymbolAttribute");
924
                    Log.Write("UID : " + item.UID);
925
                    Log.Write(ex.Message);
926
                    Log.Write(ex.StackTrace);
927
                }
928
            foreach (var item in document.LINES)
929
                try
930
                {
931
                    InputSymbolAttribute(item, item.ATTRIBUTES);
932
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
933
                }
934
                catch (Exception ex)
935
                {
936
                    Log.Write("Error in InputSymbolAttribute");
937
                    Log.Write("UID : " + item.UID);
938
                    Log.Write(ex.Message);
939
                    Log.Write(ex.StackTrace);
940
                }
941
        }
942
        private void RunInputSpecBreakAttribute()
943
        {
944
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetAllProgress, document.SpecBreaks.Count);
945
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Set SpecBreak Attribute");
946
            foreach (var item in document.SpecBreaks)
947
                try
948
                {
949
                    InputSpecBreakAttribute(item);
950
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
951
                }
952
                catch (Exception ex)
953
                {
954
                    Log.Write("Error in InputSpecBreakAttribute");
955
                    Log.Write("UID : " + item.UID);
956
                    Log.Write(ex.Message);
957
                    Log.Write(ex.StackTrace);
958
                }
959
        }
960
        private void RunInputEndBreakAttribute()
961
        {
962
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetAllProgress, document.EndBreaks.Count);
963
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Set EndBreak Attribute");
964
            foreach (var item in document.EndBreaks)
965
                try
966
                {
967
                    InputEndBreakAttribute(item);
968
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
969
                }
970
                catch (Exception ex)
971
                {
972
                    Log.Write("Error in RunInputEndBreakAttribute");
973
                    Log.Write("UID : " + item.UID);
974
                    Log.Write(ex.Message);
975
                    Log.Write(ex.StackTrace);
976
                }
977
        }
978
        private void RunLabelSymbolModeling()
979
        {
980
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetAllProgress, document.SYMBOLS.Count);
981
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Labels Modeling");
982
            foreach (var item in document.SYMBOLS)
983
                try
984
                {
985
                    LabelSymbolModeling(item);
986
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
987
                }
988
                catch (Exception ex)
989
                {
990
                    Log.Write("Error in LabelSymbolModeling");
991
                    Log.Write("UID : " + item.UID);
992
                    Log.Write(ex.Message);
993
                    Log.Write(ex.StackTrace);
994
                }
995

    
996
        }
997
        private void RunCorrectAssociationText()
998
        {
999
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetAllProgress, document.TEXTINFOS.Count + document.LINENUMBERS.Count);
1000
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Correct Labels");
1001
            List<Text> endTexts = new List<Text>();
1002
            foreach (var item in document.TEXTINFOS)
1003
            {
1004
                try
1005
                {
1006
                    if (item.ASSOCIATION && !endTexts.Contains(item))
1007
                        AssociationTextCorrectModeling(item, endTexts);
1008
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
1009
                }
1010
                catch (Exception ex)
1011
                {
1012
                    Log.Write("Error in RunCorrectAssociationText");
1013
                    Log.Write("UID : " + item.UID);
1014
                    Log.Write(ex.Message);
1015
                    Log.Write(ex.StackTrace);
1016
                }
1017

    
1018
            }
1019

    
1020
            foreach (var item in document.LINENUMBERS)
1021
            {
1022
                try
1023
                {
1024
                    LineNumberCorrectModeling(item);
1025
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
1026
                }
1027
                catch (Exception ex)
1028
                {
1029
                    Log.Write("Error in RunCorrectAssociationText");
1030
                    Log.Write("UID : " + item.UID);
1031
                    Log.Write(ex.Message);
1032
                    Log.Write(ex.StackTrace);
1033
                }
1034
            }
1035
        }
1036
        private void RunETC()
1037
        {
1038
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetAllProgress, FlowMarkRepIds.Count);
1039
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "ETC");
1040
            foreach (var item in FlowMarkRepIds)
1041
            {
1042
                LMLabelPersist label = dataSource.GetLabelPersist(item);
1043
                if (label != null)
1044
                {
1045
                    Ingr.RAD2D.Components.SmartLabel.SmartLabel dependency = radApp.ActiveDocument.ActiveSheet.DrawingObjects[label.GraphicOID.ToString()] as Ingr.RAD2D.Components.SmartLabel.SmartLabel;
1046
                    if (dependency != null)
1047
                        dependency.BringToFront();
1048
                }
1049
                ReleaseCOMObjects(label);
1050
                label = null;
1051
                SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
1052
            }
1053
        }
1054
        private void RunBulkAttribute()
1055
        {
1056
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetAllProgress, 2);
1057
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Bulk Attribute");
1058

    
1059
            List<SPPIDModel.BulkAttribute> select = _ETCSetting.BulkAttributes.FindAll(x => x.RuleName.Equals(document.BulkAttributeName));
1060
            if (select.Count > 0)
1061
                SPPIDUtil.BulkAttribute(dataSource, select, BulkAttributeItemType.PipeRun);
1062
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
1063
            if (select.Count > 0)
1064
                SPPIDUtil.BulkAttribute(dataSource, select, BulkAttributeItemType.Symbol);
1065
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
1066
        }
1067
        /// <summary>
1068
        /// 도면 생성 메서드
1069
        /// </summary>
1070
        private bool CreateDocument(ref string drawingNumber, ref string drawingName)
1071
        {
1072
            Log.Write("------------------ Start create document ------------------");
1073
            GetDrawingNameAndNumber(ref drawingName, ref drawingNumber);
1074
            Log.Write("Drawing name : " + drawingName);
1075
            Log.Write("Drawing number : " + drawingNumber);
1076
            Thread.Sleep(1000);
1077
            
1078
            string drawingTemplate = document.Template;
1079
            newDrawing = pidApp.Drawings.Add(document.Unit, ref drawingTemplate, drawingNumber, ref drawingName);
1080

    
1081
            if (newDrawing != null)
1082
            {
1083
                document.SPPID_DrawingNumber = drawingNumber;
1084
                document.SPPID_DrawingName = drawingName;
1085
                Thread.Sleep(1000);
1086
                // TODO : ZOOM FIT
1087
                //radApp.ActiveWindow.Fit();
1088
                Thread.Sleep(1000);
1089
                // TODO : ZOOM FIT
1090
                //radApp.ActiveWindow.Zoom = 2000;
1091
                Thread.Sleep(2000);
1092

    
1093
                //current LMDrawing 가져오기
1094
                LMAFilter filter = new LMAFilter();
1095
                LMACriterion criterion = new LMACriterion();
1096
                filter.ItemType = "Drawing";
1097
                criterion.SourceAttributeName = "Name";
1098
                criterion.Operator = "=";
1099
                criterion.ValueAttribute = drawingName;
1100
                filter.Criteria.Add(criterion);
1101

    
1102
                LMDrawings drawings = new LMDrawings();
1103
                drawings.Collect(dataSource, Filter: filter);
1104

    
1105
                // Input Drawing Attribute
1106
                LMDrawing drawing = drawings.Nth(1);
1107
                if (drawing != null)
1108
                {
1109
                    using (DataTable drawingAttributeDT = Project_DB.SelectDrawingProjectAttribute())
1110
                    {
1111
                        foreach (DataRow row in drawingAttributeDT.Rows)
1112
                        {
1113
                            string mappingName = DBNull.Value.Equals(row["SPPID_ATTRIBUTE"]) ? string.Empty : row["SPPID_ATTRIBUTE"].ToString();
1114
                            if (!string.IsNullOrEmpty(mappingName))
1115
                            {
1116
                                string uid = row["UID"].ToString();
1117
                                string name = row["NAME"].ToString();
1118
                                Text text = document.TEXTINFOS.Find(x => x.AREA == uid);
1119
                                if (text != null)
1120
                                {
1121
                                    string value = text.VALUE;
1122
                                    LMAAttribute attribute = drawing.Attributes[mappingName];
1123
                                    if (attribute != null)
1124
                                        attribute.Value = value;
1125
                                    ReleaseCOMObjects(attribute);
1126
                                    document.TEXTINFOS.Remove(text);
1127
                                }
1128
                            }
1129
                        }
1130

    
1131
                        drawingAttributeDT.Dispose();
1132
                    }
1133

    
1134
                    ReleaseCOMObjects(drawing);
1135
                }
1136

    
1137
                drawingID = ((dynamic)drawings).Nth(1).Id;
1138
                ReleaseCOMObjects(filter);
1139
                ReleaseCOMObjects(criterion);
1140
                ReleaseCOMObjects(drawings);
1141
                filter = null;
1142
                criterion = null;
1143
                drawings = null;
1144
            }
1145
            else
1146
                Log.Write("Fail Create Drawing");
1147

    
1148
            if (newDrawing != null)
1149
            {
1150
                SetBorderFile();
1151
                return true;
1152
            }
1153
            else
1154
                return false;
1155
        }
1156

    
1157
        private void SetBorderFile()
1158
        {
1159
            ETCSetting setting = ETCSetting.GetInstance();
1160

    
1161
            if (!string.IsNullOrEmpty(setting.BorderFilePath) && System.IO.File.Exists(setting.BorderFilePath))
1162
            {
1163
                foreach (Ingr.RAD2D.SmartFrame2d smartFrame in radApp.ActiveDocument.ActiveSheet.SmartFrames2d)
1164
                {
1165
                    if (!string.IsNullOrEmpty(smartFrame.LinkMoniker) && smartFrame.LinkMoniker != setting.BorderFilePath)
1166
                    {
1167
                        smartFrame.ChangeSource(Ingr.RAD2D.OLEInsertionTypeConstant.igOLELinked, setting.BorderFilePath, true);
1168
                        smartFrame.Update();
1169
                    }
1170

    
1171
                }
1172
            }
1173
        }
1174

    
1175
        /// <summary>
1176
        /// DrawingName, DrawingNumber를 확인하여 중복이 있으면 _1을 붙이고 +1씩 한다.
1177
        /// </summary>
1178
        /// <param name="drawingName"></param>
1179
        /// <param name="drawingNumber"></param>
1180
        private void GetDrawingNameAndNumber(ref string drawingName, ref string drawingNumber)
1181
        {
1182
            LMDrawings drawings = new LMDrawings();
1183
            drawings.Collect(dataSource);
1184

    
1185
            List<string> drawingNameList = new List<string>();
1186
            List<string> drawingNumberList = new List<string>();
1187

    
1188
            foreach (LMDrawing item in drawings)
1189
            {
1190
                drawingNameList.Add(item.Attributes["Name"].Value.ToString());
1191
                drawingNumberList.Add(item.Attributes["DrawingNumber"].Value.ToString());
1192
            }
1193

    
1194
            int nameLength = drawingName.Length;
1195
            while (drawingNameList.Contains(drawingName))
1196
            {
1197
                if (nameLength == drawingName.Length)
1198
                    drawingName += "-1";
1199
                else
1200
                {
1201
                    int index = Convert.ToInt32(drawingName.Remove(0, nameLength + 1));
1202
                    drawingName = drawingName.Substring(0, nameLength + 1);
1203
                    drawingName += ++index;
1204
                }
1205
            }
1206

    
1207
            int numberLength = drawingNumber.Length;
1208
            while (drawingNameList.Contains(drawingNumber))
1209
            {
1210
                if (numberLength == drawingNumber.Length)
1211
                    drawingNumber += "-1";
1212
                else
1213
                {
1214
                    int index = Convert.ToInt32(drawingNumber.Remove(0, numberLength + 1));
1215
                    drawingNumber = drawingNumber.Substring(0, numberLength + 1);
1216
                    drawingNumber += ++index;
1217
                }
1218
            }
1219
            ReleaseCOMObjects(drawings);
1220
            drawings = null;
1221
        }
1222

    
1223
        /// <summary>
1224
        /// 도면 크기 구하는 메서드
1225
        /// </summary>
1226
        /// <returns></returns>
1227
        private bool DocumentCoordinateCorrection()
1228
        {
1229
            //if (radApp.ActiveDocument.ActiveSheet.SmartFrames2d.Count > 0)
1230
            //{
1231
            //    double x = 0;
1232
            //    double y = 0;
1233
            //    foreach (Ingr.RAD2D.SmartFrame2d smartFrame in radApp.ActiveDocument.ActiveSheet.SmartFrames2d)
1234
            //    {
1235
            //        x = Math.Max(smartFrame.CropRight, x);
1236
            //        y = Math.Max(smartFrame.CropTop, y);
1237
            //    }
1238
            //    document.SetSPPIDLocation(x, y);
1239
            //    document.CoordinateCorrection();
1240
            //    return true;
1241
            //}
1242
            //else
1243
            //{
1244
            //    Log.Write("Need Border!");
1245
            //    return false;
1246
            //}
1247

    
1248
            if (Settings.Default.DrawingX != 0 && Settings.Default.DrawingY != 0)
1249
            {
1250
                Log.Write("Setting Drawing X, Drawing Y");
1251
                document.SetSPPIDLocation(Settings.Default.DrawingX, Settings.Default.DrawingY);
1252
                Log.Write("Start coordinate correction");
1253
                document.CoordinateCorrection();
1254
                return true;
1255
            }
1256
            else
1257
            {
1258
                Log.Write("Need Drawing X, Y");
1259
                return false;
1260
            }
1261
        }
1262

    
1263
        /// <summary>
1264
        /// 심볼을 실제로 Modeling 메서드
1265
        /// </summary>
1266
        /// <param name="symbol">생성할 심볼</param>
1267
        /// <param name="targetSymbol">연결되어 있는 심볼</param>
1268
        private void SymbolModeling(Symbol symbol, Symbol targetSymbol)
1269
        {
1270
            // OWNERSYMBOL Attribute, 값을 가지고 있을 경우
1271
            BaseModel.Attribute itemAttribute = symbol.ATTRIBUTES.Find(attr => attr.ATTRIBUTE == "OWNERSYMBOL");
1272
            if (itemAttribute != null && (string.IsNullOrEmpty(itemAttribute.VALUE) || itemAttribute.VALUE != "None"))
1273
                return;
1274
            // 이미 모델링 됐을 경우
1275
            else if (!string.IsNullOrEmpty(symbol.SPPID.RepresentationId))
1276
                return;
1277

    
1278
            LMSymbol _LMSymbol = null;
1279

    
1280
            string mappingPath = symbol.SPPID.MAPPINGNAME;
1281
            double x = symbol.SPPID.ORIGINAL_X;
1282
            double y = symbol.SPPID.ORIGINAL_Y;
1283
            int mirror = 0;
1284
            double angle = symbol.ANGLE;
1285

    
1286
            // OPC 일경우 180도 일때 Mirror
1287
            if (mappingPath.Contains("Piping OPC's") && angle == Math.PI)
1288
                mirror = 1;
1289

    
1290
            // Mirror 계산
1291
            if (symbol.FLIP == 1)
1292
            {
1293
                mirror = 1;
1294
                if (angle == Math.PI || angle == 0)
1295
                    angle += Math.PI;
1296
            }
1297

    
1298
            if (targetSymbol != null && !string.IsNullOrEmpty(targetSymbol.SPPID.RepresentationId))
1299
            {
1300
                LMSymbol _TargetItem = dataSource.GetSymbol(targetSymbol.SPPID.RepresentationId);   /// RepresentationId로 SPPID 심볼을 찾음
1301
                Connector connector = SPPIDUtil.FindSymbolConnectorByUID(document, symbol.UID, targetSymbol);
1302
                if (connector != null)
1303
                    GetTargetSymbolConnectorPoint(connector, targetSymbol, ref x, ref y);
1304

    
1305
                LMConnector temp = LineModelingForSymbolZeroLength(symbol, _TargetItem, x, y);
1306
                _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle, null, TargetItem: _TargetItem.AsLMRepresentation());
1307
                if (temp != null)
1308
                    _placement.PIDRemovePlacement(temp.AsLMRepresentation());
1309
                ReleaseCOMObjects(temp);
1310
                temp = null;
1311

    
1312
                if (_LMSymbol != null && _TargetItem != null)
1313
                    symbol.SPPID.RepresentationId = _LMSymbol.AsLMRepresentation().Id;
1314

    
1315
                ReleaseCOMObjects(_TargetItem);
1316
            }
1317
            else
1318
                _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle);
1319

    
1320
            if (_LMSymbol != null)
1321
            {
1322
                _LMSymbol.Commit();
1323

    
1324
                // ConnCheck
1325
                List<string> ids = new List<string>();
1326
                foreach (LMConnector item in _LMSymbol.Connect1Connectors)
1327
                {
1328
                    if (item.ItemStatus.ToString() == "Active" && !ids.Contains(item.Id))
1329
                        ids.Add(item.Id);
1330
                    ReleaseCOMObjects(item);
1331
                }
1332
                foreach (LMConnector item in _LMSymbol.Connect2Connectors)
1333
                {
1334
                    if (item.ItemStatus.ToString() == "Active" && !ids.Contains(item.Id))
1335
                        ids.Add(item.Id);
1336
                    ReleaseCOMObjects(item);
1337
                }
1338

    
1339
                int createdSymbolCount = document.SYMBOLS.FindAll(i => i.CONNECTORS.Find(j => j.CONNECTEDITEM == symbol.UID) != null && !string.IsNullOrEmpty(i.SPPID.RepresentationId)).Count;
1340
                if (targetSymbol == null && ids.Count != createdSymbolCount)
1341
                {
1342
                    double currentX = Convert.ToDouble(_LMSymbol.XCoordinate);
1343
                    double currentY = Convert.ToDouble(_LMSymbol.YCoordinate);
1344

    
1345

    
1346
                }
1347

    
1348
                symbol.SPPID.RepresentationId = _LMSymbol.AsLMRepresentation().Id;
1349
                symbol.SPPID.ModelItemID = _LMSymbol.ModelItemID.ToString();
1350
                symbol.SPPID.GraphicOID = _LMSymbol.GraphicOID.ToString();
1351

    
1352
                foreach (var item in symbol.ChildSymbols)
1353
                    CreateChildSymbol(item, _LMSymbol, symbol);
1354

    
1355
                symbol.SPPID.SPPID_X = Convert.ToDouble(_LMSymbol.XCoordinate);
1356
                symbol.SPPID.SPPID_Y = Convert.ToDouble(_LMSymbol.YCoordinate);
1357

    
1358
                double[] range = null;
1359
                GetSPPIDSymbolRange(symbol, ref range);
1360
                symbol.SPPID.SPPID_Min_X = range[0];
1361
                symbol.SPPID.SPPID_Min_Y = range[1];
1362
                symbol.SPPID.SPPID_Max_X = range[2];
1363
                symbol.SPPID.SPPID_Max_Y = range[3];
1364

    
1365
                foreach (var item in symbol.SPPID.CorrectionX_GroupSymbols)
1366
                    item.SPPID.ORIGINAL_X = symbol.SPPID.SPPID_X;
1367
                foreach (var item in symbol.SPPID.CorrectionY_GroupSymbols)
1368
                    item.SPPID.ORIGINAL_Y = symbol.SPPID.SPPID_Y;
1369

    
1370
                ReleaseCOMObjects(_LMSymbol);
1371
            }
1372
        }
1373
        /// <summary>
1374
        /// targetX와 targetY 기준 제일 먼 PipingPoint에 TempLine Modeling
1375
        /// Signal Point는 고려하지 않음
1376
        /// </summary>
1377
        /// <param name="symbol"></param>
1378
        /// <param name="_TargetItem"></param>
1379
        /// <param name="targetX"></param>
1380
        /// <param name="targetY"></param>
1381
        /// <returns></returns>
1382
        private LMConnector LineModelingForSymbolZeroLength(Symbol symbol, LMSymbol _TargetItem, double targetX, double targetY)
1383
        {
1384
            LMConnector tempConnector = null;
1385

    
1386
            List<Symbol> group = new List<Symbol>();
1387
            SPPIDUtil.FindConnectedSymbolGroup(document, symbol, group);
1388
            if (group.FindAll(loopX => !string.IsNullOrEmpty(loopX.SPPID.RepresentationId)).Count == 1)
1389
            {
1390
                List<Connector> connectors = new List<Connector>();
1391
                foreach (var item in group)
1392
                    connectors.AddRange(item.CONNECTORS.FindAll(loopX => loopX.ConnectedObject != null && loopX.ConnectedObject.GetType() == typeof(Line)));
1393
                /// Primary or Secondary Type Line만 고려
1394
                Connector _connector = connectors.Find(loopX => loopX.ConnectedObject != null && loopX.ConnectedObject.GetType() == typeof(Line) &&
1395
                (((Line)loopX.ConnectedObject).TYPE == "Primary" || ((Line)loopX.ConnectedObject).TYPE == "Secondary"));
1396
                if (_connector != null)
1397
                {
1398
                    string sppidLine = ((Line)_connector.ConnectedObject).SPPID.MAPPINGNAME;
1399
                    List<double[]> pointInfos = getPipingPoints(_TargetItem);
1400
                    /// PipingPoint가 2개 이상만
1401
                    if (pointInfos.Count >= 2)
1402
                    {
1403
                        double lineX = 0;
1404
                        double lineY = 0;
1405
                        double length = 0;
1406
                        foreach (var item in pointInfos)
1407
                        {
1408
                            double tempX = item[1];
1409
                            double tempY = item[2];
1410

    
1411
                            double calcDistance = SPPIDUtil.CalcPointToPointdDistance(targetX, targetY, tempX, tempY);
1412
                            if (calcDistance > length)
1413
                            {
1414
                                lineX = tempX;
1415
                                lineY = tempY;
1416
                            }
1417
                        }
1418

    
1419
                        LMAItem _LMAItem = _placement.PIDCreateItem(sppidLine);
1420
                        PlaceRunInputs placeRunInputs = new PlaceRunInputs();
1421
                        placeRunInputs.AddPoint(-1, -1);
1422
                        placeRunInputs.AddSymbolTarget(_TargetItem, lineX, lineY);
1423
                        tempConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
1424
                        if (tempConnector != null)
1425
                            tempConnector.Commit();
1426
                        ReleaseCOMObjects(_LMAItem);
1427
                        _LMAItem = null;
1428
                        ReleaseCOMObjects(placeRunInputs);
1429
                        placeRunInputs = null;
1430
                    }
1431
                }
1432
            }
1433

    
1434
            return tempConnector;
1435
        }
1436
        /// <summary>
1437
        /// Symbol의 PipingPoints를 구함
1438
        /// SignalPoint는 고려하지 않음
1439
        /// </summary>
1440
        /// <param name="symbol"></param>
1441
        /// <returns></returns>
1442
        private List<double[]> getPipingPoints(LMSymbol symbol)
1443
        {
1444
            LMModelItem modelItem = symbol.ModelItemObject;
1445
            LMPipingPoints pipingPoints = null;
1446
            if (modelItem.ItemTypeName.ToString() == "PipingComp")
1447
            {
1448
                LMPipingComp pipingComp = dataSource.GetPipingComp(modelItem.Id);
1449
                pipingPoints = pipingComp.PipingPoints;
1450
                ReleaseCOMObjects(pipingComp);
1451
                pipingComp = null;
1452
            }
1453
            else if (modelItem.ItemTypeName.ToString() == "Instrument")
1454
            {
1455
                LMInstrument instrument = dataSource.GetInstrument(modelItem.Id);
1456
                pipingPoints = instrument.PipingPoints;
1457
                ReleaseCOMObjects(instrument);
1458
                instrument = null;
1459
            }
1460
            else
1461
                Log.Write("다른 Type");
1462

    
1463
            List<double[]> info = new List<double[]>();
1464
            if (pipingPoints != null)
1465
            {
1466
                foreach (LMPipingPoint pipingPoint in pipingPoints)
1467
                {
1468
                    foreach (LMAAttribute attribute in pipingPoint.Attributes)
1469
                    {
1470
                        if (attribute.Name == "PipingPointNumber")
1471
                        {
1472
                            int index = Convert.ToInt32(attribute.Value);
1473
                            if (info.Find(loopX => loopX[0] == index) == null)
1474
                            {
1475
                                double x = 0;
1476
                                double y = 0;
1477
                                if (_placement.PIDConnectPointLocation(symbol, index, ref x, ref y))
1478
                                    info.Add(new double[] { index, x, y });
1479
                            }
1480
                        }
1481
                    }
1482
                }
1483
            }
1484
            ReleaseCOMObjects(modelItem);
1485
            modelItem = null;
1486
            ReleaseCOMObjects(pipingPoints);
1487
            pipingPoints = null;
1488

    
1489
            return info;
1490
        }
1491

    
1492
        private void RemoveSymbol(Symbol symbol)
1493
        {
1494
            if (!string.IsNullOrEmpty(symbol.SPPID.RepresentationId))
1495
            {
1496
                LMSymbol _LMSymbol = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
1497
                if (_LMSymbol != null)
1498
                {
1499
                    _placement.PIDRemovePlacement(_LMSymbol.AsLMRepresentation());
1500
                    ReleaseCOMObjects(_LMSymbol);
1501
                }
1502
            }
1503

    
1504
            symbol.SPPID.RepresentationId = string.Empty;
1505
            symbol.SPPID.ModelItemID = string.Empty;
1506
            symbol.SPPID.SPPID_X = double.NaN;
1507
            symbol.SPPID.SPPID_Y = double.NaN;
1508
            symbol.SPPID.SPPID_Min_X = double.NaN;
1509
            symbol.SPPID.SPPID_Min_Y = double.NaN;
1510
            symbol.SPPID.SPPID_Max_X = double.NaN;
1511
            symbol.SPPID.SPPID_Max_Y = double.NaN;
1512
        }
1513

    
1514
        private void RemoveSymbol(List<Symbol> symbols)
1515
        {
1516
            foreach (var symbol in symbols)
1517
            {
1518
                if (!string.IsNullOrEmpty(symbol.SPPID.RepresentationId))
1519
                {
1520
                    LMSymbol _LMSymbol = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
1521
                    if (_LMSymbol != null)
1522
                    {
1523
                        _placement.PIDRemovePlacement(_LMSymbol.AsLMRepresentation());
1524
                        ReleaseCOMObjects(_LMSymbol);
1525
                    }
1526
                }
1527

    
1528
                symbol.SPPID.RepresentationId = string.Empty;
1529
                symbol.SPPID.ModelItemID = string.Empty;
1530
                symbol.SPPID.SPPID_X = double.NaN;
1531
                symbol.SPPID.SPPID_Y = double.NaN;
1532
                symbol.SPPID.SPPID_Min_X = double.NaN;
1533
                symbol.SPPID.SPPID_Min_Y = double.NaN;
1534
                symbol.SPPID.SPPID_Max_X = double.NaN;
1535
                symbol.SPPID.SPPID_Max_Y = double.NaN;
1536
            }
1537
        }
1538

    
1539
        /// <summary>
1540
        /// ID2의 Symbol Width와 Height를 비교해서 상대적인 SPPID Connector좌표를 가져온다.
1541
        /// </summary>
1542
        /// <param name="targetConnector"></param>
1543
        /// <param name="targetSymbol"></param>
1544
        /// <param name="x"></param>
1545
        /// <param name="y"></param>
1546
        private void GetTargetSymbolConnectorPoint(Connector targetConnector, Symbol targetSymbol, ref double x, ref double y)
1547
        {
1548
            LMSymbol _TargetItem = dataSource.GetSymbol(targetSymbol.SPPID.RepresentationId);
1549

    
1550
            double[] range = null;
1551
            List<double[]> points = new List<double[]>();
1552
            GetSPPIDSymbolRangeAndConnectionPoints(targetSymbol, ref range, points);
1553
            double x1 = range[0];
1554
            double y1 = range[1];
1555
            double x2 = range[2];
1556
            double y2 = range[3];
1557

    
1558
            // Origin 기준 Connector의 위치차이
1559
            double sceneX = 0;
1560
            double sceneY = 0;
1561
            SPPIDUtil.ConvertPointBystring(targetConnector.SCENECONNECTPOINT, ref sceneX, ref sceneY);
1562
            double originX = 0;
1563
            double originY = 0;
1564
            SPPIDUtil.ConvertPointBystring(targetSymbol.ORIGINALPOINT, ref originX, ref originY);
1565
            double gapX = originX - sceneX;
1566
            double gapY = originY - sceneY;
1567

    
1568
            // SPPID Symbol과 ID2 심볼의 크기 차이
1569
            double sizeWidth = 0;
1570
            double sizeHeight = 0;
1571
            SPPIDUtil.ConvertPointBystring(targetSymbol.SIZE, ref sizeWidth, ref sizeHeight);
1572
            if (sizeWidth == 0 || sizeHeight == 0)
1573
                throw new Exception("Check symbol size! \r\nUID : " + targetSymbol.UID);
1574

    
1575
            double percentX = (x2 - x1) / sizeWidth;
1576
            double percentY = (y2 - y1) / sizeHeight;
1577

    
1578
            double SPPIDgapX = gapX * percentX;
1579
            double SPPIDgapY = gapY * percentY;
1580

    
1581
            double[] SPPIDOriginPoint = new double[] { Convert.ToDouble(_TargetItem.XCoordinate) - SPPIDgapX, Convert.ToDouble(_TargetItem.YCoordinate) + SPPIDgapY };
1582
            double distance = double.MaxValue;
1583
            double[] resultPoint;
1584
            foreach (var point in points)
1585
            {
1586
                double result = SPPIDUtil.CalcPointToPointdDistance(point[0], point[1], SPPIDOriginPoint[0], SPPIDOriginPoint[1]);
1587
                if (distance > result)
1588
                {
1589
                    distance = result;
1590
                    resultPoint = point;
1591
                    x = point[0];
1592
                    y = point[1];
1593
                }
1594
            }
1595

    
1596
            ReleaseCOMObjects(_TargetItem);
1597
        }
1598

    
1599
        private void GetTargetLineConnectorPoint(Connector targetConnector, Line targetLine, ref double x, ref double y)
1600
        {
1601
            int index = targetLine.CONNECTORS.IndexOf(targetConnector);
1602
            if (index == 0)
1603
            {
1604
                x = targetLine.SPPID.START_X;
1605
                y = targetLine.SPPID.START_Y;
1606
            }
1607
            else
1608
            {
1609
                x = targetLine.SPPID.END_X;
1610
                y = targetLine.SPPID.END_Y;
1611
            }
1612
        }
1613

    
1614
        /// <summary>
1615
        /// SPPID Symbol의 Range를 구한다.
1616
        /// </summary>
1617
        /// <param name="symbol"></param>
1618
        /// <param name="range"></param>
1619
        private void GetSPPIDSymbolRangeAndConnectionPoints(Symbol symbol, ref double[] range, List<double[]> points)
1620
        {
1621
            LMSymbol _TargetItem = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
1622
            Ingr.RAD2D.Symbol2d symbol2d = radApp.ActiveDocument.ActiveSheet.DrawingObjects[_TargetItem.GraphicOID.ToString()] as Ingr.RAD2D.Symbol2d;
1623
            double x1 = 0;
1624
            double y1 = 0;
1625
            double x2 = 0;
1626
            double y2 = 0;
1627
            symbol2d.Range(out x1, out y1, out x2, out y2);
1628
            range = new double[] { x1, y1, x2, y2 };
1629

    
1630
            for (int i = 1; i < 30; i++)
1631
            {
1632
                double connX = 0;
1633
                double connY = 0;
1634
                if (_placement.PIDConnectPointLocation(_TargetItem, i, ref connX, ref connY))
1635
                    points.Add(new double[] { connX, connY });
1636
            }
1637

    
1638
            foreach (var childSymbol in symbol.ChildSymbols)
1639
                GetSPPIDChildSymbolRange(childSymbol, ref range, points);
1640

    
1641
            ReleaseCOMObjects(_TargetItem);
1642
        }
1643

    
1644
        private void GetSPPIDSymbolRange(Symbol symbol, ref double[] range, bool bOnlySymbol = false, bool bForGraphic = false)
1645
        {
1646
            LMSymbol _TargetItem = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
1647
            if (_TargetItem != null)
1648
            {
1649
                Ingr.RAD2D.Symbol2d symbol2d = radApp.ActiveDocument.ActiveSheet.DrawingObjects[_TargetItem.GraphicOID.ToString()] as Ingr.RAD2D.Symbol2d;
1650
                double x1 = 0;
1651
                double y1 = 0;
1652
                double x2 = 0;
1653
                double y2 = 0;
1654
                if (!bForGraphic)
1655
                {
1656
                    symbol2d.Range(out x1, out y1, out x2, out y2);
1657
                    range = new double[] { x1, y1, x2, y2 };
1658
                }
1659
                else
1660
                {
1661
                    x1 = double.MaxValue;
1662
                    y1 = double.MaxValue;
1663
                    x2 = double.MinValue;
1664
                    y2 = double.MinValue;
1665
                    range = new double[] { x1, y1, x2, y2 };
1666

    
1667
                    foreach (var item in symbol2d.DrawingObjects)
1668
                    {
1669
                        if (item.GetType() == typeof(Ingr.RAD2D.Line2d))
1670
                        {
1671
                            Ingr.RAD2D.Line2d rangeObject = item as Ingr.RAD2D.Line2d;
1672
                            if (rangeObject.Layer == "Default")
1673
                            {
1674
                                rangeObject.Range(out x1, out y1, out x2, out y2);
1675
                                range = new double[] {
1676
                                Math.Min(x1, range[0]),
1677
                                Math.Min(y1, range[1]),
1678
                                Math.Max(x2, range[2]),
1679
                                Math.Max(y2, range[3])
1680
                            };
1681
                            }
1682
                        }
1683
                        else if (item.GetType() == typeof(Ingr.RAD2D.Circle2d))
1684
                        {
1685
                            Ingr.RAD2D.Circle2d rangeObject = item as Ingr.RAD2D.Circle2d;
1686
                            if (rangeObject.Layer == "Default")
1687
                            {
1688
                                rangeObject.Range(out x1, out y1, out x2, out y2);
1689
                                range = new double[] {
1690
                                Math.Min(x1, range[0]),
1691
                                Math.Min(y1, range[1]),
1692
                                Math.Max(x2, range[2]),
1693
                                Math.Max(y2, range[3])
1694
                            };
1695
                            }
1696
                        }
1697
                        else if (item.GetType() == typeof(Ingr.RAD2D.Rectangle2d))
1698
                        {
1699
                            Ingr.RAD2D.Rectangle2d rangeObject = item as Ingr.RAD2D.Rectangle2d;
1700
                            if (rangeObject.Layer == "Default")
1701
                            {
1702
                                rangeObject.Range(out x1, out y1, out x2, out y2);
1703
                                range = new double[] {
1704
                                Math.Min(x1, range[0]),
1705
                                Math.Min(y1, range[1]),
1706
                                Math.Max(x2, range[2]),
1707
                                Math.Max(y2, range[3])
1708
                            };
1709
                            }
1710
                        }
1711
                        else if (item.GetType() == typeof(Ingr.RAD2D.Arc2d))
1712
                        {
1713
                            Ingr.RAD2D.Arc2d rangeObject = item as Ingr.RAD2D.Arc2d;
1714
                            if (rangeObject.Layer == "Default")
1715
                            {
1716
                                rangeObject.Range(out x1, out y1, out x2, out y2);
1717
                                range = new double[] {
1718
                                Math.Min(x1, range[0]),
1719
                                Math.Min(y1, range[1]),
1720
                                Math.Max(x2, range[2]),
1721
                                Math.Max(y2, range[3])
1722
                            };
1723
                            }
1724
                        }
1725
                    }
1726
                }
1727

    
1728
                if (!bOnlySymbol)
1729
                {
1730
                    foreach (var childSymbol in symbol.ChildSymbols)
1731
                        GetSPPIDChildSymbolRange(childSymbol, ref range);
1732
                }
1733
                ReleaseCOMObjects(_TargetItem);
1734
            }
1735
        }
1736

    
1737
        private void GetSPPIDSymbolRange(LMLabelPersist labelPersist, ref double[] range)
1738
        {
1739
            if (labelPersist != null)
1740
            {
1741
                double x1 = double.MaxValue;
1742
                double y1 = double.MaxValue;
1743
                double x2 = double.MinValue;
1744
                double y2 = double.MinValue;
1745
                range = new double[] { x1, y1, x2, y2 };
1746

    
1747
                Ingr.RAD2D.Components.SmartLabel.SmartLabel dependency = radApp.ActiveDocument.ActiveSheet.DrawingObjects[labelPersist.GraphicOID.ToString()] as Ingr.RAD2D.Components.SmartLabel.SmartLabel;
1748
                foreach (var item in dependency.DrawingObjects)
1749
                {
1750
                    Ingr.RAD2D.TextBox textBox = item as Ingr.RAD2D.TextBox;
1751
                    if (textBox != null)
1752
                    {
1753
                        if (dependency != null)
1754
                        {
1755
                            double tempX1;
1756
                            double tempY1;
1757
                            double tempX2;
1758
                            double tempY2;
1759
                            textBox.Range(out tempX1, out tempY1, out tempX2, out tempY2);
1760
                            x1 = Math.Min(x1, tempX1);
1761
                            y1 = Math.Min(y1, tempY1);
1762
                            x2 = Math.Max(x2, tempX2);
1763
                            y2 = Math.Max(y2, tempY2);
1764

    
1765
                            range = new double[] { x1, y1, x2, y2 };
1766
                        }
1767
                    }
1768
                }
1769

    
1770
            }
1771
        }
1772

    
1773
        private void GetSPPIDSymbolRange(List<Symbol> symbols, ref double[] range, bool bOnlySymbol = false, bool bForGraphic = false)
1774
        {
1775
            double[] tempRange = new double[] { double.MaxValue, double.MaxValue, double.MinValue, double.MinValue };
1776
            foreach (var symbol in symbols)
1777
            {
1778
                double[] symbolRange = null;
1779
                GetSPPIDSymbolRange(symbol, ref symbolRange, bOnlySymbol, bForGraphic);
1780

    
1781
                tempRange[0] = Math.Min(tempRange[0], symbolRange[0]);
1782
                tempRange[1] = Math.Min(tempRange[1], symbolRange[1]);
1783
                tempRange[2] = Math.Max(tempRange[2], symbolRange[2]);
1784
                tempRange[3] = Math.Max(tempRange[3], symbolRange[3]);
1785

    
1786
                foreach (var childSymbol in symbol.ChildSymbols)
1787
                    GetSPPIDChildSymbolRange(childSymbol, ref tempRange);
1788
            }
1789

    
1790
            range = tempRange;
1791
        }
1792

    
1793
        /// <summary>
1794
        /// Child Modeling 된 Symbol의 Range를 구한다.
1795
        /// </summary>
1796
        /// <param name="childSymbol"></param>
1797
        /// <param name="range"></param>
1798
        private void GetSPPIDChildSymbolRange(ChildSymbol childSymbol, ref double[] range, List<double[]> points)
1799
        {
1800
            LMSymbol _ChildSymbol = dataSource.GetSymbol(childSymbol.SPPID.RepresentationId);
1801
            if (_ChildSymbol != null)
1802
            {
1803
                Ingr.RAD2D.Symbol2d symbol2d = radApp.ActiveDocument.ActiveSheet.DrawingObjects[_ChildSymbol.GraphicOID.ToString()] as Ingr.RAD2D.Symbol2d;
1804
                double x1 = 0;
1805
                double y1 = 0;
1806
                double x2 = 0;
1807
                double y2 = 0;
1808
                symbol2d.Range(out x1, out y1, out x2, out y2);
1809
                range[0] = Math.Min(range[0], x1);
1810
                range[1] = Math.Min(range[1], y1);
1811
                range[2] = Math.Max(range[2], x2);
1812
                range[3] = Math.Max(range[3], y2);
1813

    
1814
                for (int i = 1; i < int.MaxValue; i++)
1815
                {
1816
                    double connX = 0;
1817
                    double connY = 0;
1818
                    if (_placement.PIDConnectPointLocation(_ChildSymbol, i, ref connX, ref connY))
1819
                        points.Add(new double[] { connX, connY });
1820
                    else
1821
                        break;
1822
                }
1823

    
1824
                foreach (var loopChildSymbol in childSymbol.ChildSymbols)
1825
                    GetSPPIDChildSymbolRange(loopChildSymbol, ref range, points);
1826

    
1827
                ReleaseCOMObjects(_ChildSymbol);
1828
            }
1829
        }
1830

    
1831
        private void GetSPPIDChildSymbolRange(ChildSymbol childSymbol, ref double[] range)
1832
        {
1833
            LMSymbol _ChildSymbol = dataSource.GetSymbol(childSymbol.SPPID.RepresentationId);
1834
            if (_ChildSymbol != null)
1835
            {
1836
                Ingr.RAD2D.Symbol2d symbol2d = radApp.ActiveDocument.ActiveSheet.DrawingObjects[_ChildSymbol.GraphicOID.ToString()] as Ingr.RAD2D.Symbol2d;
1837
                double x1 = 0;
1838
                double y1 = 0;
1839
                double x2 = 0;
1840
                double y2 = 0;
1841
                symbol2d.Range(out x1, out y1, out x2, out y2);
1842
                range[0] = Math.Min(range[0], x1);
1843
                range[1] = Math.Min(range[1], y1);
1844
                range[2] = Math.Max(range[2], x2);
1845
                range[3] = Math.Max(range[3], y2);
1846

    
1847
                foreach (var loopChildSymbol in childSymbol.ChildSymbols)
1848
                    GetSPPIDChildSymbolRange(loopChildSymbol, ref range);
1849
                ReleaseCOMObjects(_ChildSymbol);
1850
            }
1851
        }
1852

    
1853
        /// <summary>
1854
        /// Label Symbol Modeling
1855
        /// </summary>
1856
        /// <param name="symbol"></param>
1857
        private void LabelSymbolModeling(Symbol symbol)
1858
        {
1859
            if (string.IsNullOrEmpty(symbol.SPPID.RepresentationId))
1860
            {
1861
                BaseModel.Attribute itemAttribute = symbol.ATTRIBUTES.Find(x => x.ATTRIBUTE == "OWNERSYMBOL");
1862
                if (itemAttribute == null || string.IsNullOrEmpty(itemAttribute.VALUE) || itemAttribute.VALUE == "None")
1863
                    return;
1864
                double[] points = new double[] { 0, symbol.SPPID.ORIGINAL_X, symbol.SPPID.ORIGINAL_Y };
1865

    
1866
                string symbolUID = itemAttribute.VALUE;
1867
                object targetItem = SPPIDUtil.FindObjectByUID(document, symbolUID);
1868
                if (targetItem != null &&
1869
                    (targetItem.GetType() == typeof(Symbol) ||
1870
                    targetItem.GetType() == typeof(Equipment)))
1871
                {
1872
                    // Object 아이템이 Symbol일 경우 Equipment일 경우 
1873
                    string sRep = null;
1874
                    if (targetItem.GetType() == typeof(Symbol))
1875
                        sRep = ((Symbol)targetItem).SPPID.RepresentationId;
1876
                    else if (targetItem.GetType() == typeof(Equipment))
1877
                        sRep = ((Equipment)targetItem).SPPID.RepresentationId;
1878
                    if (!string.IsNullOrEmpty(sRep))
1879
                    {
1880
                        // LEADER Line 검사
1881
                        bool leaderLine = false;
1882
                        SymbolMapping symbolMapping = document.SymbolMappings.Find(x => x.UID == symbol.DBUID);
1883
                        if (symbolMapping != null)
1884
                            leaderLine = symbolMapping.LEADERLINE;
1885

    
1886
                        // Target Symbol Item 가져오고 Label Modeling
1887
                        LMSymbol _TargetItem = dataSource.GetSymbol(sRep);
1888
                        LMLabelPersist _LMLabelPresist = _placement.PIDPlaceLabel(symbol.SPPID.MAPPINGNAME, points, null, Rotation: symbol.ANGLE, LabeledItem: _TargetItem.AsLMRepresentation(), IsLeaderVisible: leaderLine);
1889

    
1890
                        //Leader 선 센터로
1891
                        if (_LMLabelPresist != null)
1892
                        {
1893
                            // Target Item에 Label의 Attribute Input
1894
                            InputSymbolAttribute(targetItem, symbol.ATTRIBUTES);
1895

    
1896
                            string OID = _LMLabelPresist.GraphicOID.ToString();
1897
                            Ingr.RAD2D.Components.SmartLabel.SmartLabel dependency = radApp.ActiveDocument.ActiveSheet.DrawingObjects[OID] as Ingr.RAD2D.Components.SmartLabel.SmartLabel;
1898
                            if (dependency != null)
1899
                            {
1900
                                bool result = false;
1901
                                foreach (var attributes in dependency.AttributeSets)
1902
                                {
1903
                                    foreach (var attribute in attributes)
1904
                                    {
1905
                                        string name = attribute.Name;
1906
                                        string value = attribute.GetValue().ToString();
1907
                                        if (name == "DrawingItemType" && value == "LabelPersist")
1908
                                        {
1909
                                            foreach (DrawingObjectBase drawingObject in dependency.DrawingObjects)
1910
                                            {
1911
                                                if (drawingObject.Type == Ingr.RAD2D.ObjectType.igLineString2d)
1912
                                                {
1913
                                                    Ingr.RAD2D.LineString2d lineString2D = drawingObject as Ingr.RAD2D.LineString2d;
1914
                                                    double prevX = Convert.ToDouble(_TargetItem.XCoordinate);
1915
                                                    double prevY = Convert.ToDouble(_TargetItem.YCoordinate);
1916
                                                    lineString2D.InsertVertex(lineString2D.VertexCount, prevX, prevY);
1917
                                                    lineString2D.RemoveVertex(lineString2D.VertexCount);
1918
                                                    result = true;
1919
                                                    break;
1920
                                                }
1921
                                            }
1922
                                        }
1923

    
1924
                                        if (result)
1925
                                            break;
1926
                                    }
1927

    
1928
                                    if (result)
1929
                                        break;
1930
                                }
1931
                            }
1932

    
1933
                            symbol.SPPID.RepresentationId = _LMLabelPresist.AsLMRepresentation().Id;
1934
                            _LMLabelPresist.Commit();
1935
                            ReleaseCOMObjects(_LMLabelPresist);
1936
                        }
1937

    
1938
                        ReleaseCOMObjects(_TargetItem);
1939
                    }
1940
                }
1941
                else if (targetItem != null && targetItem.GetType() == typeof(Line))
1942
                {
1943
                    Line targetLine = targetItem as Line;
1944
                    Dictionary<LMConnector, List<double[]>> connectorVertices = GetPipeRunVertices(targetLine.SPPID.ModelItemId);
1945
                    LMConnector connectedLMConnector = FindTargetLMConnectorForLabel(connectorVertices, symbol.SPPID.ORIGINAL_X, symbol.SPPID.ORIGINAL_Y);
1946
                    if (connectedLMConnector != null)
1947
                    {
1948
                        // Target Item에 Label의 Attribute Input
1949
                        InputSymbolAttribute(targetItem, symbol.ATTRIBUTES);
1950

    
1951
                        // LEADER Line 검사
1952
                        bool leaderLine = false;
1953
                        SymbolMapping symbolMapping = document.SymbolMappings.Find(x => x.UID == symbol.DBUID);
1954
                        if (symbolMapping != null)
1955
                            leaderLine = symbolMapping.LEADERLINE;
1956

    
1957
                        LMLabelPersist _LMLabelPresist = _placement.PIDPlaceLabel(symbol.SPPID.MAPPINGNAME, points, null, Rotation: symbol.ANGLE, LabeledItem: connectedLMConnector.AsLMRepresentation(), IsLeaderVisible: leaderLine);
1958
                        if (_LMLabelPresist != null)
1959
                        {
1960
                            symbol.SPPID.RepresentationId = _LMLabelPresist.AsLMRepresentation().Id;
1961
                            _LMLabelPresist.Commit();
1962
                            ReleaseCOMObjects(_LMLabelPresist);
1963
                        }
1964
                        ReleaseCOMObjects(connectedLMConnector);
1965
                    }
1966

    
1967
                    foreach (var item in connectorVertices)
1968
                        if (item.Key != null)
1969
                            ReleaseCOMObjects(item.Key);
1970
                }
1971
            }
1972
        }
1973

    
1974
        /// <summary>
1975
        /// Equipment를 실제로 Modeling 메서드
1976
        /// </summary>
1977
        /// <param name="equipment"></param>
1978
        private void EquipmentModeling(Equipment equipment)
1979
        {
1980
            if (!string.IsNullOrEmpty(equipment.SPPID.RepresentationId))
1981
                return;
1982

    
1983
            LMSymbol _LMSymbol = null;
1984
            LMSymbol targetItem = null;
1985
            string mappingPath = equipment.SPPID.MAPPINGNAME;
1986
            double x = equipment.SPPID.ORIGINAL_X;
1987
            double y = equipment.SPPID.ORIGINAL_Y;
1988
            int mirror = 0;
1989
            double angle = equipment.ANGLE;
1990

    
1991
            // Mirror 계산
1992
            if (equipment.FLIP == 1)
1993
            {
1994
                mirror = 1;
1995
                if (angle == Math.PI || angle == 0)
1996
                    angle += Math.PI;
1997
            }
1998

    
1999
            SPPIDUtil.ConvertGridPoint(ref x, ref y);
2000

    
2001
            Connector connector = equipment.CONNECTORS.Find(conn =>
2002
            !string.IsNullOrEmpty(conn.CONNECTEDITEM) &&
2003
            conn.CONNECTEDITEM != "None" &&
2004
            conn.ConnectedObject.GetType() == typeof(Equipment));
2005

    
2006
            if (connector != null)
2007
            {
2008
                Equipment connEquipment = SPPIDUtil.FindObjectByUID(document, connector.CONNECTEDITEM) as Equipment;
2009
                VendorPackage connVendorPackage = SPPIDUtil.FindObjectByUID(document, connector.CONNECTEDITEM) as VendorPackage;
2010
                if (connEquipment != null)
2011
                {
2012
                    //if (string.IsNullOrEmpty(connEquipment.SPPID.RepresentationId))
2013
                    //    EquipmentModeling(connEquipment);
2014

    
2015
                    if (!string.IsNullOrEmpty(connEquipment.SPPID.RepresentationId))
2016
                    {
2017
                        targetItem = dataSource.GetSymbol(connEquipment.SPPID.RepresentationId);
2018
                        if (targetItem != null)
2019
                            _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle, null, TargetItem: targetItem.AsLMRepresentation());
2020
                        else
2021
                            _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle);
2022
                    }
2023
                    else
2024
                    {
2025
                        _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle);
2026
                    }
2027
                }
2028
                else if (connVendorPackage != null)
2029
                {
2030
                    if (!string.IsNullOrEmpty(connVendorPackage.SPPID.RepresentationId))
2031
                    {
2032
                        targetItem = dataSource.GetSymbol(connVendorPackage.SPPID.RepresentationId);
2033
                        if (targetItem != null)
2034
                            _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle, null, TargetItem: targetItem.AsLMRepresentation());
2035
                        else
2036
                            _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle);
2037
                    }
2038
                }
2039
                else
2040
                {
2041
                    _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle);
2042
                }
2043
            }
2044
            else
2045
            {
2046
                _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle);
2047
            }
2048

    
2049
            if (_LMSymbol != null)
2050
            {
2051
                _LMSymbol.Commit();
2052
                equipment.SPPID.RepresentationId = _LMSymbol.AsLMRepresentation().Id;
2053
                equipment.SPPID.GraphicOID = _LMSymbol.GraphicOID.ToString();
2054
                ReleaseCOMObjects(_LMSymbol);
2055
            }
2056

    
2057
            if (targetItem != null)
2058
            {
2059
                ReleaseCOMObjects(targetItem);
2060
            }
2061

    
2062
            ReleaseCOMObjects(_LMSymbol);
2063
        }
2064

    
2065
        private void VendorPackageModeling(VendorPackage vendorPackage)
2066
        {
2067
            ETCSetting setting = ETCSetting.GetInstance();
2068
            if (!string.IsNullOrEmpty(setting.VendorPackageSymbolPath))
2069
            {
2070
                string symbolPath = setting.VendorPackageSymbolPath;
2071
                double x = vendorPackage.SPPID.ORIGINAL_X;
2072
                double y = vendorPackage.SPPID.ORIGINAL_Y;
2073

    
2074
                LMSymbol symbol = _placement.PIDPlaceSymbol(symbolPath, x, y);
2075
                if (symbol != null)
2076
                {
2077
                    symbol.Commit();
2078
                    vendorPackage.SPPID.RepresentationId = symbol.AsLMRepresentation().Id;
2079
                }
2080

    
2081
                ReleaseCOMObjects(symbol);
2082
                symbol = null;
2083
            }
2084
        }
2085

    
2086
        /// <summary>
2087
        /// 첫 진입점
2088
        /// </summary>
2089
        /// <param name="symbol"></param>
2090
        private void SymbolModelingBySymbol(Symbol symbol)
2091
        {
2092
            SymbolModeling(symbol, null);   /// 심볼을 생성한다
2093
            List<object> endObjects = new List<object>();
2094
            endObjects.Add(symbol);
2095

    
2096
            /// 심볼에 연결되어 있는 항목들을 모델링한다
2097
            foreach (var connector in symbol.CONNECTORS)
2098
            {
2099
                object connItem = SPPIDUtil.FindObjectByUID(document, connector.CONNECTEDITEM);
2100
                if (connItem != null && connItem.GetType() != typeof(Equipment))
2101
                {
2102
                    endObjects.Add(connItem);
2103
                    if (connItem.GetType() == typeof(Symbol))
2104
                    {
2105
                        Symbol connSymbol = connItem as Symbol;
2106
                        if (string.IsNullOrEmpty(connSymbol.SPPID.RepresentationId))
2107
                        {
2108
                            SymbolModeling(connSymbol, symbol);
2109
                        }
2110
                        SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetProgress, document.SYMBOLS.FindAll(x => !string.IsNullOrEmpty(x.SPPID.RepresentationId)).Count);
2111
                        SymbolModelingByNeerSymbolLoop(connSymbol, endObjects);
2112
                    }
2113
                    else if (connItem.GetType() == typeof(Line))
2114
                    {
2115
                        Line connLine = connItem as Line;
2116
                        SymbolModelingByNeerLineLoop(connLine, endObjects, symbol);
2117
                    }
2118
                }
2119
            }
2120
        }
2121

    
2122
        private void SymbolModelingByNeerSymbolLoop(Symbol symbol, List<object> endObjects)
2123
        {
2124
            foreach (var connector in symbol.CONNECTORS)
2125
            {
2126
                object connItem = SPPIDUtil.FindObjectByUID(document, connector.CONNECTEDITEM);
2127
                if (connItem != null && connItem.GetType() != typeof(Equipment))
2128
                {
2129
                    if (!endObjects.Contains(connItem))
2130
                    {
2131
                        endObjects.Add(connItem);
2132
                        if (connItem.GetType() == typeof(Symbol))
2133
                        {
2134
                            Symbol connSymbol = connItem as Symbol;
2135
                            if (string.IsNullOrEmpty(connSymbol.SPPID.RepresentationId))
2136
                            {
2137
                                SymbolModeling(connSymbol, symbol);
2138
                            }
2139
                            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetProgress, document.SYMBOLS.FindAll(x => !string.IsNullOrEmpty(x.SPPID.RepresentationId)).Count);
2140
                            SymbolModelingByNeerSymbolLoop(connSymbol, endObjects);
2141
                        }
2142
                        else if (connItem.GetType() == typeof(Line))
2143
                        {
2144
                            Line connLine = connItem as Line;
2145
                            SymbolModelingByNeerLineLoop(connLine, endObjects, symbol);
2146
                        }
2147
                    }
2148
                }
2149
            }
2150
        }
2151

    
2152
        private void SymbolModelingByNeerLineLoop(Line line, List<object> endObjects, Symbol prevSymbol)
2153
        {
2154
            foreach (var connector in line.CONNECTORS)
2155
            {
2156
                object connItem = SPPIDUtil.FindObjectByUID(document, connector.CONNECTEDITEM);
2157
                if (connItem != null && connItem.GetType() != typeof(Equipment))
2158
                {
2159
                    if (!endObjects.Contains(connItem))
2160
                    {
2161
                        endObjects.Add(connItem);
2162
                        if (connItem.GetType() == typeof(Symbol))
2163
                        {
2164
                            Symbol connSymbol = connItem as Symbol;
2165
                            if (string.IsNullOrEmpty(connSymbol.SPPID.RepresentationId))
2166
                            {
2167
                                Line connLine = SPPIDUtil.GetConnectedLine(prevSymbol, connSymbol);
2168
                                int branchCount = 0;
2169
                                if (connLine != null)
2170
                                    branchCount = SPPIDUtil.GetBranchLineCount(document, connLine);
2171

    
2172
                                List<Symbol> group = new List<Symbol>();
2173
                                SPPIDUtil.FindConnectedSymbolGroup(document, connSymbol, group);
2174
                                Symbol priority = prioritySymbols.Find(x => group.Contains(x));
2175
                                List<Symbol> endModelingGroup = new List<Symbol>();
2176
                                if (priority != null)
2177
                                {
2178
                                    SymbolGroupModeling(priority, group);
2179

    
2180
                                    // Range 겹치는지 확인해야함
2181
                                    double[] prevRange = null;
2182
                                    GetSPPIDSymbolRange(prevSymbol, ref prevRange, bForGraphic: true);
2183
                                    double[] groupRange = null;
2184
                                    GetSPPIDSymbolRange(group, ref groupRange, bForGraphic: true);
2185

    
2186
                                    double distanceX = 0;
2187
                                    double distanceY = 0;
2188
                                    bool overlapX = false;
2189
                                    bool overlapY = false;
2190
                                    SlopeType slopeType = SPPIDUtil.CalcSlope(prevSymbol.SPPID.ORIGINAL_X, prevSymbol.SPPID.ORIGINAL_Y, connSymbol.SPPID.ORIGINAL_X, connSymbol.SPPID.ORIGINAL_Y);
2191
                                    SPPIDUtil.CalcOverlap(prevRange, groupRange, ref distanceX, ref distanceY, ref overlapX, ref overlapY);
2192
                                    if ((slopeType == SlopeType.HORIZONTAL && overlapX) ||
2193
                                        (slopeType == SlopeType.VERTICAL && overlapY))
2194
                                    {
2195
                                        RemoveSymbol(group);
2196
                                        foreach (var _temp in group)
2197
                                            SPPIDUtil.CalcNewCoordinateForSymbol(_temp, prevSymbol, distanceX, distanceY);
2198

    
2199
                                        SymbolGroupModeling(priority, group);
2200
                                    }
2201
                                    else if (branchCount > 0)
2202
                                    {
2203
                                        LMConnector _connector = JustLineModeling(connLine);
2204
                                        if (_connector != null)
2205
                                        {
2206
                                            double distance = GetConnectorDistance(_connector);
2207
                                            int cellCount = (int)Math.Truncate(distance / GridSetting.GetInstance().Length);
2208
                                            _placement.PIDRemovePlacement(_connector.AsLMRepresentation());
2209
                                            _connector.Commit();
2210
                                            ReleaseCOMObjects(_connector);
2211
                                            _connector = null;
2212
                                            if (cellCount < branchCount + 1)
2213
                                            {
2214
                                                int moveCount = branchCount - cellCount;
2215
                                                RemoveSymbol(group);
2216
                                                foreach (var _temp in group)
2217
                                                    SPPIDUtil.CalcNewCoordinateForSymbol(_temp, prevSymbol, moveCount * GridSetting.GetInstance().Length, moveCount * GridSetting.GetInstance().Length);
2218

    
2219
                                                SymbolGroupModeling(priority, group);
2220
                                            }
2221
                                        }
2222
                                    }
2223
                                }
2224
                                else
2225
                                {
2226
                                    SymbolModeling(connSymbol, null);
2227
                                    // Range 겹치는지 확인해야함
2228
                                    double[] prevRange = null;
2229
                                    GetSPPIDSymbolRange(prevSymbol, ref prevRange, bForGraphic: true);
2230
                                    double[] connRange = null;
2231
                                    GetSPPIDSymbolRange(connSymbol, ref connRange, bForGraphic: true);
2232

    
2233
                                    double distanceX = 0;
2234
                                    double distanceY = 0;
2235
                                    bool overlapX = false;
2236
                                    bool overlapY = false;
2237
                                    SlopeType slopeType = SPPIDUtil.CalcSlope(prevSymbol.SPPID.ORIGINAL_X, prevSymbol.SPPID.ORIGINAL_Y, connSymbol.SPPID.ORIGINAL_X, connSymbol.SPPID.ORIGINAL_Y);
2238
                                    SPPIDUtil.CalcOverlap(prevRange, connRange, ref distanceX, ref distanceY, ref overlapX, ref overlapY);
2239
                                    if ((slopeType == SlopeType.HORIZONTAL && overlapX) ||
2240
                                        (slopeType == SlopeType.VERTICAL && overlapY))
2241
                                    {
2242
                                        RemoveSymbol(connSymbol);
2243
                                        SPPIDUtil.CalcNewCoordinateForSymbol(connSymbol, prevSymbol, distanceX, distanceY);
2244

    
2245
                                        SymbolModeling(connSymbol, null);
2246
                                    }
2247
                                    else if (branchCount > 0)
2248
                                    {
2249
                                        LMConnector _connector = JustLineModeling(connLine);
2250
                                        if (_connector != null)
2251
                                        {
2252
                                            double distance = GetConnectorDistance(_connector);
2253
                                            int cellCount = (int)Math.Truncate(distance / GridSetting.GetInstance().Length);
2254
                                            _placement.PIDRemovePlacement(_connector.AsLMRepresentation());
2255
                                            _connector.Commit();
2256
                                            ReleaseCOMObjects(_connector);
2257
                                            _connector = null;
2258
                                            if (cellCount < branchCount + 1)
2259
                                            {
2260
                                                int moveCount = branchCount - cellCount;
2261
                                                RemoveSymbol(group);
2262
                                                foreach (var _temp in group)
2263
                                                    SPPIDUtil.CalcNewCoordinateForSymbol(_temp, prevSymbol, moveCount * GridSetting.GetInstance().Length, moveCount * GridSetting.GetInstance().Length);
2264

    
2265
                                                SymbolGroupModeling(priority, group);
2266
                                            }
2267
                                        }
2268
                                    }
2269
                                }
2270
                            }
2271
                            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetProgress, document.SYMBOLS.FindAll(x => !string.IsNullOrEmpty(x.SPPID.RepresentationId)).Count);
2272
                            SymbolModelingByNeerSymbolLoop(connSymbol, endObjects);
2273
                        }
2274
                        else if (connItem.GetType() == typeof(Line))
2275
                        {
2276
                            Line connLine = connItem as Line;
2277
                            if (!SPPIDUtil.IsBranchLine(connLine, line))
2278
                                SymbolModelingByNeerLineLoop(connLine, endObjects, prevSymbol);
2279
                        }
2280
                    }
2281
                }
2282
            }
2283
        }
2284

    
2285
        private void SymbolGroupModeling(Symbol firstSymbol, List<Symbol> group)
2286
        {
2287
            List<Symbol> endModelingGroup = new List<Symbol>();
2288
            SymbolModeling(firstSymbol, null);
2289
            endModelingGroup.Add(firstSymbol);
2290
            while (endModelingGroup.Count != group.Count)
2291
            {
2292
                foreach (var _symbol in group)
2293
                {
2294
                    if (!endModelingGroup.Contains(_symbol))
2295
                    {
2296
                        foreach (var _connector in _symbol.CONNECTORS)
2297
                        {
2298
                            Symbol _connSymbol = SPPIDUtil.FindObjectByUID(document, _connector.CONNECTEDITEM) as Symbol;
2299
                            if (_connSymbol != null && endModelingGroup.Contains(_connSymbol))
2300
                            {
2301
                                SymbolModeling(_symbol, _connSymbol);
2302
                                endModelingGroup.Add(_symbol);
2303
                                break;
2304
                            }
2305
                        }
2306
                    }
2307
                }
2308
            }
2309
        }
2310

    
2311
        /// <summary>
2312
        /// 심볼을 실제로 Modeling할때 ChildSymbol이 있다면 Modeling하는 메서드
2313
        /// </summary>
2314
        /// <param name="childSymbol"></param>
2315
        /// <param name="parentSymbol"></param>
2316
        private void CreateChildSymbol(ChildSymbol childSymbol, LMSymbol parentSymbol, Symbol parent)
2317
        {
2318
            Ingr.RAD2D.Symbol2d symbol2d = radApp.ActiveDocument.ActiveSheet.DrawingObjects[parentSymbol.GraphicOID.ToString()] as Ingr.RAD2D.Symbol2d;
2319
            double x1 = 0;
2320
            double x2 = 0;
2321
            double y1 = 0;
2322
            double y2 = 0;
2323
            symbol2d.Range(out x1, out y1, out x2, out y2);
2324

    
2325
            LMSymbol _LMSymbol = _placement.PIDPlaceSymbol(childSymbol.SPPID.MAPPINGNAME, (x1 + x2) / 2, (y1 + y2) / 2, null, null, null, TargetItem: parentSymbol.AsLMRepresentation());
2326
            if (_LMSymbol != null)
2327
            {
2328
                _LMSymbol.Commit();
2329
                childSymbol.SPPID.RepresentationId = _LMSymbol.AsLMRepresentation().Id;
2330
                foreach (var item in childSymbol.ChildSymbols)
2331
                    CreateChildSymbol(item, _LMSymbol, parent);
2332
            }
2333

    
2334

    
2335
            ReleaseCOMObjects(_LMSymbol);
2336
        }
2337
        double index = 0;
2338
        private void NewLineModeling(Line line, bool isBranchModeling = false)
2339
        {
2340
            if (!string.IsNullOrEmpty(line.SPPID.ModelItemId) || (BranchLines.Contains(line) && !isBranchModeling))
2341
                return;
2342

    
2343
            List<Line> group = new List<Line>();
2344
            GetConnectedLineGroup(line, group);
2345
            LineCoordinateCorrection(group);
2346

    
2347
            foreach (var groupLine in group)
2348
            {
2349
                if (!isBranchModeling && SPPIDUtil.IsBranchLine(groupLine))
2350
                {
2351
                    BranchLines.Add(groupLine);
2352
                    continue;
2353
                }
2354

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

    
2426
                            placeRunInputs.AddPoint(x, y);
2427

    
2428
                            if (groupLine.CONNECTORS.IndexOf(connector) == 1)
2429
                            {
2430
                                index += 0.01;
2431
                                if (groupLine.SlopeType == SlopeType.HORIZONTAL)
2432
                                    placeRunInputs.AddPoint(x, -0.1 - index);
2433
                                else if (groupLine.SlopeType == SlopeType.VERTICAL)
2434
                                    placeRunInputs.AddPoint(-0.1 - index, y);
2435
                                else
2436
                                {
2437
                                    Line nextLine = groupLine.CONNECTORS[1].ConnectedObject as Line;
2438
                                    if (SPPIDUtil.CalcAngle(nextLine.SPPID.START_X, nextLine.SPPID.START_Y, nextLine.SPPID.END_X, nextLine.SPPID.END_Y) < 45)
2439
                                        placeRunInputs.AddPoint(-0.1 - index, y);
2440
                                    else
2441
                                        placeRunInputs.AddPoint(x, -0.1 - index);
2442
                                }
2443
                            }
2444
                        }
2445
                    }
2446
                    else
2447
                        placeRunInputs.AddPoint(x, y);
2448
                }
2449

    
2450
                LMConnector _lMConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
2451
                if (_lMConnector != null)
2452
                {
2453
                    _lMConnector.Commit();
2454
                    groupLine.SPPID.ModelItemId = _lMConnector.ModelItemID.ToString();
2455

    
2456
                    bool bRemodelingStart = false;
2457
                    if (_LMSymbolStart != null)
2458
                        NeedReModeling(groupLine, _LMSymbolStart, ref bRemodelingStart);
2459
                    bool bRemodelingEnd = false;
2460
                    if (_LMSymbolEnd != null)
2461
                        NeedReModeling(groupLine, _LMSymbolEnd, ref bRemodelingEnd);
2462

    
2463
                    if (bRemodelingStart || bRemodelingEnd)
2464
                        ReModelingLine(groupLine, _lMConnector, _LMSymbolStart, _LMSymbolEnd, bRemodelingStart, bRemodelingEnd);
2465

    
2466
                    FlowMarkModeling(groupLine);
2467

    
2468
                    ReleaseCOMObjects(_lMConnector);
2469

    
2470
                    LMModelItem modelItem = dataSource.GetModelItem(groupLine.SPPID.ModelItemId);
2471
                    if (modelItem != null)
2472
                    {
2473
                        LMAAttribute attribute = modelItem.Attributes["FlowDirection"];
2474
                        if (attribute != null)
2475
                            attribute.Value = "End 1 is upstream (Inlet)";
2476
                        modelItem.Commit();
2477
                    }
2478
                    ReleaseCOMObjects(modelItem);
2479
                    modelItem = null;
2480
                }
2481
                else if (!isBranchModeling)
2482
                {
2483
                    Log.Write("Main Line Modeling : " + groupLine.UID);
2484
                }
2485

    
2486
                List<object> removeLines = groupLine.CONNECTORS.FindAll(x =>
2487
                x.ConnectedObject != null &&
2488
                x.ConnectedObject.GetType() == typeof(Line) &&
2489
                !string.IsNullOrEmpty(((Line)x.ConnectedObject).SPPID.ModelItemId))
2490
                .Select(x => x.ConnectedObject)
2491
                .ToList();
2492

    
2493
                foreach (var item in removeLines)
2494
                    RemoveLineForModeling(item as Line);
2495

    
2496
                ReleaseCOMObjects(_LMAItem);
2497
                _LMAItem = null;
2498
                ReleaseCOMObjects(placeRunInputs);
2499
                placeRunInputs = null;
2500
                ReleaseCOMObjects(_LMSymbolStart);
2501
                _LMSymbolStart = null;
2502
                ReleaseCOMObjects(_LMSymbolEnd);
2503
                _LMSymbolEnd = null;
2504

    
2505
                if (isBranchModeling && BranchLines.Contains(groupLine))
2506
                    BranchLines.Remove(groupLine);
2507
            }
2508
        }
2509

    
2510
        private LMConnector JustLineModeling(Line line)
2511
        {
2512
            bool diagonal = false;
2513
            if (line.SlopeType != SlopeType.HORIZONTAL && line.SlopeType != SlopeType.VERTICAL)
2514
                diagonal = true;
2515
            LMAItem _LMAItem = _placement.PIDCreateItem(line.SPPID.MAPPINGNAME);
2516
            LMSymbol _LMSymbolStart = null;
2517
            LMSymbol _LMSymbolEnd = null;
2518
            PlaceRunInputs placeRunInputs = new PlaceRunInputs();
2519
            foreach (var connector in line.CONNECTORS)
2520
            {
2521
                double x = 0;
2522
                double y = 0;
2523
                GetTargetLineConnectorPoint(connector, line, ref x, ref y);
2524
                if (connector.ConnectedObject == null)
2525
                {
2526
                    placeRunInputs.AddPoint(x, y);
2527
                }
2528
                else if (connector.ConnectedObject.GetType() == typeof(Symbol))
2529
                {
2530
                    Symbol targetSymbol = connector.ConnectedObject as Symbol;
2531
                    GetTargetSymbolConnectorPoint(targetSymbol.CONNECTORS.Find(z => z.ConnectedObject == line), targetSymbol, ref x, ref y);
2532
                    if (line.CONNECTORS.IndexOf(connector) == 0)
2533
                    {
2534
                        _LMSymbolStart = GetTargetSymbol(targetSymbol, line);
2535
                        if (_LMSymbolStart != null)
2536
                            placeRunInputs.AddSymbolTarget(_LMSymbolStart, x, y, diagonal);
2537
                        else
2538
                            placeRunInputs.AddPoint(x, y);
2539
                    }
2540
                    else
2541
                    {
2542
                        _LMSymbolEnd = GetTargetSymbol(targetSymbol, line);
2543
                        if (_LMSymbolEnd != null)
2544
                            placeRunInputs.AddSymbolTarget(_LMSymbolEnd, x, y, diagonal);
2545
                        else
2546
                            placeRunInputs.AddPoint(x, y);
2547
                    }
2548
                }
2549
                else if (connector.ConnectedObject.GetType() == typeof(Line))
2550
                {
2551
                    Line targetLine = connector.ConnectedObject as Line;
2552
                    if (!string.IsNullOrEmpty(targetLine.SPPID.ModelItemId))
2553
                    {
2554
                        LMConnector targetConnector = FindTargetLMConnectorForBranch(line, targetLine, ref x, ref y);
2555
                        if (targetConnector != null)
2556
                        {
2557
                            placeRunInputs.AddConnectorTarget(targetConnector, x, y, diagonal);
2558
                            ChangeLineSPPIDCoordinateByConnector(line, targetLine, x, y, false);
2559
                        }
2560
                        else
2561
                        {
2562
                            placeRunInputs.AddPoint(x, y);
2563
                            ChangeLineSPPIDCoordinateByConnector(line, targetLine, x, y, false);
2564
                        }
2565
                    }
2566
                    else
2567
                        placeRunInputs.AddPoint(x, y);
2568
                }
2569
            }
2570

    
2571
            LMConnector _lMConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
2572
            if (_lMConnector != null)
2573
                _lMConnector.Commit();
2574

    
2575
            ReleaseCOMObjects(_LMAItem);
2576
            _LMAItem = null;
2577
            ReleaseCOMObjects(placeRunInputs);
2578
            placeRunInputs = null;
2579
            ReleaseCOMObjects(_LMSymbolStart);
2580
            _LMSymbolStart = null;
2581
            ReleaseCOMObjects(_LMSymbolEnd);
2582
            _LMSymbolEnd = null;
2583

    
2584
            return _lMConnector;
2585
        }
2586

    
2587
        private void RemoveLineForModeling(Line line)
2588
        {
2589
            LMModelItem modelItem = dataSource.GetModelItem(line.SPPID.ModelItemId);
2590
            if (modelItem != null)
2591
            {
2592
                foreach (LMRepresentation rep in modelItem.Representations)
2593
                {
2594
                    if (rep.Attributes["RepresentationType"].Value.ToString() == "Connector" && rep.Attributes["ItemStatus"].Value.ToString() == "Active")
2595
                    {
2596
                        LMConnector _LMConnector = dataSource.GetConnector(rep.Id);
2597
                        dynamic OID = rep.GraphicOID.ToString();
2598
                        Ingr.RAD2D.Components.Connector.Connector drawingObject = radApp.ActiveDocument.ActiveSheet.DrawingObjects[OID] as Ingr.RAD2D.Components.Connector.Connector;
2599
                        Ingr.RAD2D.LineStringGeometry2d lineStringGeometry = drawingObject.GetGeometry() as Ingr.RAD2D.LineStringGeometry2d;
2600
                        int verticesCount = lineStringGeometry.VertexCount;
2601
                        double[] vertices = null;
2602
                        lineStringGeometry.GetVertices(ref verticesCount, ref vertices);
2603
                        for (int i = 0; i < verticesCount; i++)
2604
                        {
2605
                            double x = 0;
2606
                            double y = 0;
2607
                            lineStringGeometry.GetVertex(i + 1, ref x, ref y);
2608
                            if (verticesCount == 2 && (x < 0 || y < 0))
2609
                                _placement.PIDRemovePlacement(rep);
2610
                        }
2611
                        ReleaseCOMObjects(_LMConnector);
2612
                    }
2613
                }
2614

    
2615
                ReleaseCOMObjects(modelItem);
2616
            }
2617
        }
2618

    
2619
        private void RemoveLine(Line line)
2620
        {
2621
            LMModelItem modelItem = dataSource.GetModelItem(line.SPPID.ModelItemId);
2622
            if (modelItem != null)
2623
            {
2624
                foreach (LMRepresentation rep in modelItem.Representations)
2625
                {
2626
                    if (rep.Attributes["RepresentationType"].Value.ToString() == "Connector" && rep.Attributes["ItemStatus"].Value.ToString() == "Active")
2627
                        _placement.PIDRemovePlacement(rep);
2628
                }
2629
                ReleaseCOMObjects(modelItem);
2630
            }
2631
            line.SPPID.ModelItemId = null;
2632
        }
2633

    
2634
        private void GetConnectedLineGroup(Line line, List<Line> group)
2635
        {
2636
            if (!group.Contains(line))
2637
                group.Add(line);
2638
            foreach (var connector in line.CONNECTORS)
2639
            {
2640
                if (connector.ConnectedObject != null &&
2641
                    connector.ConnectedObject.GetType() == typeof(Line) &&
2642
                    !group.Contains(connector.ConnectedObject) &&
2643
                    string.IsNullOrEmpty(((Line)connector.ConnectedObject).SPPID.ModelItemId))
2644
                {
2645
                    Line connLine = connector.ConnectedObject as Line;
2646
                    if (!SPPIDUtil.IsBranchLine(connLine, line))
2647
                    {
2648
                        if (line.CONNECTORS.IndexOf(connector) == 0)
2649
                            group.Insert(0, connLine);
2650
                        else
2651
                            group.Add(connLine);
2652
                        GetConnectedLineGroup(connLine, group);
2653
                    }
2654
                }
2655
            }
2656
        }
2657

    
2658
        private void LineCoordinateCorrection(List<Line> group)
2659
        {
2660
            // 순서대로 전 Item 기준 정렬
2661
            LineCoordinateCorrectionByStart(group);
2662

    
2663
            // 역으로 심볼이 있을 경우 좌표 보정
2664
            LineCoordinateCorrectionForLastLine(group);
2665
        }
2666

    
2667
        private void LineCoordinateCorrectionByStart(List<Line> group)
2668
        {
2669
            for (int i = 0; i < group.Count; i++)
2670
            {
2671
                Line line = group[i];
2672
                if (i == 0)
2673
                {
2674
                    Connector symbolConnector = line.CONNECTORS.Find(x => x.ConnectedObject != null && x.ConnectedObject.GetType() == typeof(Symbol));
2675
                    if (symbolConnector != null)
2676
                        LineCoordinateCorrectionByConnItem(line, symbolConnector.ConnectedObject);
2677
                }
2678
                else if (i != 0)
2679
                {
2680
                    LineCoordinateCorrectionByConnItem(line, group[i - 1]);
2681
                }
2682
            }
2683
        }
2684

    
2685
        private void LineCoordinateCorrectionForLastLine(List<Line> group)
2686
        {
2687
            Line checkLine = group[group.Count - 1];
2688
            Connector lastSymbolConnector = checkLine.CONNECTORS.Find(x => x.ConnectedObject != null && x.ConnectedObject.GetType() == typeof(Symbol));
2689
            if (lastSymbolConnector != null)
2690
            {
2691
                LineCoordinateCorrectionByConnItem(checkLine, lastSymbolConnector.ConnectedObject);
2692
                for (int i = group.Count - 2; i >= 0; i--)
2693
                {
2694
                    Line line = group[i + 1];
2695
                    Line prevLine = group[i];
2696

    
2697
                    // 같으면 보정
2698
                    if (line.SlopeType == prevLine.SlopeType)
2699
                        LineCoordinateCorrectionByConnItem(prevLine, line);
2700
                    else
2701
                    {
2702
                        if (line.SlopeType == SlopeType.HORIZONTAL)
2703
                        {
2704
                            double prevX = 0;
2705
                            double prevY = 0;
2706
                            GetTargetLineConnectorPoint(prevLine.CONNECTORS.Find(z => z.ConnectedObject == line), prevLine, ref prevX, ref prevY);
2707
                            ChangeLineSPPIDCoordinateByConnectorOnlyX(line, prevLine, prevX);
2708

    
2709
                            double x = 0;
2710
                            double y = 0;
2711
                            GetTargetLineConnectorPoint(line.CONNECTORS.Find(z => z.ConnectedObject == prevLine), line, ref x, ref y);
2712
                            ChangeLineSPPIDCoordinateByConnectorOnlyY(prevLine, line, y);
2713
                        }
2714
                        else if (line.SlopeType == SlopeType.VERTICAL)
2715
                        {
2716
                            double prevX = 0;
2717
                            double prevY = 0;
2718
                            GetTargetLineConnectorPoint(prevLine.CONNECTORS.Find(z => z.ConnectedObject == line), prevLine, ref prevX, ref prevY);
2719
                            ChangeLineSPPIDCoordinateByConnectorOnlyY(line, prevLine, prevY);
2720

    
2721
                            double x = 0;
2722
                            double y = 0;
2723
                            GetTargetLineConnectorPoint(line.CONNECTORS.Find(z => z.ConnectedObject == prevLine), line, ref x, ref y);
2724
                            ChangeLineSPPIDCoordinateByConnectorOnlyX(prevLine, line, x);
2725
                        }
2726
                        break;
2727
                    }
2728
                }
2729
            }
2730
        }
2731

    
2732
        private void LineCoordinateCorrectionByConnItem(Line line, object connItem)
2733
        {
2734
            double x = 0;
2735
            double y = 0;
2736
            if (connItem.GetType() == typeof(Symbol))
2737
            {
2738
                Symbol targetSymbol = connItem as Symbol;
2739
                Connector targetConnector = targetSymbol.CONNECTORS.Find(z => z.ConnectedObject == line);
2740
                if (targetConnector != null)
2741
                    GetTargetSymbolConnectorPoint(targetConnector, targetSymbol, ref x, ref y);
2742
                else
2743
                    throw new Exception("Target symbol UID : " + targetSymbol.UID + "\r\nLine UID : " + line.UID);
2744
            }
2745
            else if (connItem.GetType() == typeof(Line))
2746
            {
2747
                Line targetLine = connItem as Line;
2748
                GetTargetLineConnectorPoint(targetLine.CONNECTORS.Find(z => z.ConnectedObject == line), targetLine, ref x, ref y);
2749
            }
2750

    
2751
            ChangeLineSPPIDCoordinateByConnector(line, connItem, x, y);
2752
        }
2753

    
2754
        private void ChangeLineSPPIDCoordinateByConnector(Line line, object connItem, double x, double y, bool changeOtherCoordinate = true)
2755
        {
2756
            Connector connector = line.CONNECTORS.Find(z => z.ConnectedObject == connItem);
2757
            int index = line.CONNECTORS.IndexOf(connector);
2758
            if (index == 0)
2759
            {
2760
                line.SPPID.START_X = x;
2761
                line.SPPID.START_Y = y;
2762
                if (line.SlopeType == SlopeType.HORIZONTAL && changeOtherCoordinate)
2763
                    line.SPPID.END_Y = y;
2764
                else if (line.SlopeType == SlopeType.VERTICAL && changeOtherCoordinate)
2765
                    line.SPPID.END_X = x;
2766
            }
2767
            else
2768
            {
2769
                line.SPPID.END_X = x;
2770
                line.SPPID.END_Y = y;
2771
                if (line.SlopeType == SlopeType.HORIZONTAL && changeOtherCoordinate)
2772
                    line.SPPID.START_Y = y;
2773
                else if (line.SlopeType == SlopeType.VERTICAL && changeOtherCoordinate)
2774
                    line.SPPID.START_X = x;
2775
            }
2776
        }
2777

    
2778
        private void ChangeLineSPPIDCoordinateByConnectorOnlyX(Line line, object connItem, double x)
2779
        {
2780
            Connector connector = line.CONNECTORS.Find(z => z.ConnectedObject == connItem);
2781
            int index = line.CONNECTORS.IndexOf(connector);
2782
            if (index == 0)
2783
            {
2784
                line.SPPID.START_X = x;
2785
                if (line.SlopeType == SlopeType.VERTICAL)
2786
                    line.SPPID.END_X = x;
2787
            }
2788
            else
2789
            {
2790
                line.SPPID.END_X = x;
2791
                if (line.SlopeType == SlopeType.VERTICAL)
2792
                    line.SPPID.START_X = x;
2793
            }
2794
        }
2795

    
2796
        private void ChangeLineSPPIDCoordinateByConnectorOnlyY(Line line, object connItem, double y)
2797
        {
2798
            Connector connector = line.CONNECTORS.Find(z => z.ConnectedObject == connItem);
2799
            int index = line.CONNECTORS.IndexOf(connector);
2800
            if (index == 0)
2801
            {
2802
                line.SPPID.START_Y = y;
2803
                if (line.SlopeType == SlopeType.HORIZONTAL)
2804
                    line.SPPID.END_Y = y;
2805
            }
2806
            else
2807
            {
2808
                line.SPPID.END_Y = y;
2809
                if (line.SlopeType == SlopeType.HORIZONTAL)
2810
                    line.SPPID.START_Y = y;
2811
            }
2812
        }
2813

    
2814
        private void NeedReModeling(Line line, LMSymbol symbol, ref bool result)
2815
        {
2816
            if (symbol != null)
2817
            {
2818
                string repID = symbol.AsLMRepresentation().Id;
2819
                string symbolUID = SPPIDUtil.FindSymbolByRepresentationID(document, repID).UID;
2820
                string lineUID = line.UID;
2821

    
2822
                SpecBreak startSpecBreak = document.SpecBreaks.Find(x =>
2823
                (x.DownStreamUID == symbolUID || x.UpStreamUID == symbolUID) &&
2824
                (x.DownStreamUID == lineUID || x.UpStreamUID == lineUID));
2825

    
2826
                EndBreak startEndBreak = document.EndBreaks.Find(x =>
2827
                (x.OWNER == symbolUID || x.PROPERTIES.Find(y => y.ATTRIBUTE == "Connected Item").VALUE == symbolUID) &&
2828
                (x.OWNER == lineUID || x.PROPERTIES.Find(y => y.ATTRIBUTE == "Connected Item").VALUE == lineUID));
2829

    
2830
                if (startSpecBreak != null || startEndBreak != null)
2831
                    result = true;
2832
            }
2833
        }
2834

    
2835
        /// <summary>
2836
        /// Symbol에 붙을 경우 Line을 Remodeling 한다.
2837
        /// </summary>
2838
        /// <param name="lines"></param>
2839
        /// <param name="prevLMConnector"></param>
2840
        /// <param name="startSymbol"></param>
2841
        /// <param name="endSymbol"></param>
2842
        private void ReModelingLine(Line line, LMConnector prevLMConnector, LMSymbol startSymbol, LMSymbol endSymbol, bool bStart, bool bEnd)
2843
        {
2844
            string symbolPath = string.Empty;
2845
            #region get symbol path
2846
            LMModelItem modelItem = dataSource.GetModelItem(prevLMConnector.ModelItemID);
2847
            symbolPath = GetSPPIDFileName(modelItem);
2848
            ReleaseCOMObjects(modelItem);
2849
            #endregion
2850
            bool diagonal = false;
2851
            if (line.SlopeType != SlopeType.HORIZONTAL && line.SlopeType != SlopeType.VERTICAL)
2852
                diagonal = true;
2853
            LMAItem _LMAItem = _placement.PIDCreateItem(symbolPath);
2854
            LMConnector newConnector = null;
2855
            dynamic OID = prevLMConnector.GraphicOID.ToString();
2856
            Ingr.RAD2D.Components.Connector.Connector drawingObject = radApp.ActiveDocument.ActiveSheet.DrawingObjects[OID] as Ingr.RAD2D.Components.Connector.Connector;
2857
            Ingr.RAD2D.LineStringGeometry2d lineStringGeometry = drawingObject.GetGeometry() as Ingr.RAD2D.LineStringGeometry2d;
2858
            int verticesCount = lineStringGeometry.VertexCount;
2859
            PlaceRunInputs placeRunInputs = new PlaceRunInputs();
2860

    
2861
            List<double[]> vertices = new List<double[]>();
2862
            for (int i = 1; i <= verticesCount; i++)
2863
            {
2864
                double x = 0;
2865
                double y = 0;
2866
                lineStringGeometry.GetVertex(i, ref x, ref y);
2867
                vertices.Add(new double[] { x, y });
2868
            }
2869

    
2870
            for (int i = 0; i < vertices.Count; i++)
2871
            {
2872
                double[] points = vertices[i];
2873
                // 시작 심볼이 있고 첫번째 좌표일 때
2874
                if (startSymbol != null && i == 0)
2875
                {
2876
                    if (bStart)
2877
                    {
2878
                        SlopeType slopeType = SPPIDUtil.CalcSlope(points[0], points[1], vertices[i + 1][0], vertices[i + 1][1]);
2879
                        if (slopeType == SlopeType.HORIZONTAL)
2880
                            placeRunInputs.AddPoint(points[0], -0.1);
2881
                        else if (slopeType == SlopeType.VERTICAL)
2882
                            placeRunInputs.AddPoint(-0.1, points[1]);
2883
                        else
2884
                            placeRunInputs.AddPoint(points[0], -0.1);
2885

    
2886
                        placeRunInputs.AddPoint(points[0], points[1]);
2887
                    }
2888
                    else
2889
                    {
2890
                        placeRunInputs.AddSymbolTarget(startSymbol, points[0], points[1], diagonal);
2891
                    }
2892
                }
2893
                // 마지막 심볼이 있고 마지막 좌표일 때
2894
                else if (endSymbol != null && i == vertices.Count - 1)
2895
                {
2896
                    if (bEnd)
2897
                    {
2898
                        placeRunInputs.AddPoint(points[0], points[1]);
2899

    
2900
                        SlopeType slopeType = SPPIDUtil.CalcSlope(points[0], points[1], vertices[i - 1][0], vertices[i - 1][1]);
2901
                        if (slopeType == SlopeType.HORIZONTAL)
2902
                            placeRunInputs.AddPoint(points[0], -0.1);
2903
                        else if (slopeType == SlopeType.VERTICAL)
2904
                            placeRunInputs.AddPoint(-0.1, points[1]);
2905
                        else
2906
                            placeRunInputs.AddPoint(points[0], -0.1);
2907
                    }
2908
                    else
2909
                    {
2910
                        placeRunInputs.AddSymbolTarget(endSymbol, points[0], points[1], diagonal);
2911
                    }
2912
                }
2913
                // 첫번째이며 시작 심볼이 아니고 Connecotr일 경우
2914
                else if (i == 0 && prevLMConnector.ConnectItem1SymbolObject != null)
2915
                    placeRunInputs.AddSymbolTarget(prevLMConnector.ConnectItem1SymbolObject, points[0], points[1], diagonal);
2916
                // 마지막이며 마지막 심볼이 아니고 Connecotr일 경우
2917
                else if (i == vertices.Count - 1 && prevLMConnector.ConnectItem2SymbolObject != null)
2918
                    placeRunInputs.AddSymbolTarget(prevLMConnector.ConnectItem2SymbolObject, points[0], points[1], diagonal);
2919
                else
2920
                    placeRunInputs.AddPoint(points[0], points[1]);
2921
            }
2922

    
2923
            _placement.PIDRemovePlacement(prevLMConnector.AsLMRepresentation());
2924
            newConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
2925

    
2926
            ReleaseCOMObjects(placeRunInputs);
2927
            ReleaseCOMObjects(_LMAItem);
2928
            ReleaseCOMObjects(modelItem);
2929

    
2930
            if (newConnector != null)
2931
            {
2932
                newConnector.Commit();
2933
                if (startSymbol != null && bStart)
2934
                {
2935
                    _LMAItem = _placement.PIDCreateItem(symbolPath);
2936
                    placeRunInputs = new PlaceRunInputs();
2937
                    placeRunInputs.AddSymbolTarget(startSymbol, vertices[0][0], vertices[0][1]);
2938
                    placeRunInputs.AddConnectorTarget(newConnector, vertices[0][0], vertices[0][1]);
2939
                    LMConnector _LMConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
2940
                    if (_LMConnector != null)
2941
                    {
2942
                        _LMConnector.Commit();
2943
                        RemoveConnectorForReModelingLine(newConnector);
2944
                        ZeroLengthModelItemID.Add(_LMConnector.ModelItemID.ToString());
2945
                        ReleaseCOMObjects(_LMConnector);
2946
                    }
2947
                    ReleaseCOMObjects(placeRunInputs);
2948
                    ReleaseCOMObjects(_LMAItem);
2949
                }
2950

    
2951
                if (endSymbol != null && bEnd)
2952
                {
2953
                    if (startSymbol != null)
2954
                    {
2955
                        Dictionary<LMConnector, List<double[]>> dicVertices = GetPipeRunVertices(newConnector.ModelItemID.ToString());
2956
                        newConnector = dicVertices.First().Key;
2957
                    }
2958

    
2959
                    _LMAItem = _placement.PIDCreateItem(symbolPath);
2960
                    placeRunInputs = new PlaceRunInputs();
2961
                    placeRunInputs.AddSymbolTarget(endSymbol, vertices[vertices.Count - 1][0], vertices[vertices.Count - 1][1]);
2962
                    placeRunInputs.AddConnectorTarget(newConnector, vertices[vertices.Count - 1][0], vertices[vertices.Count - 1][1]);
2963
                    LMConnector _LMConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
2964
                    if (_LMConnector != null)
2965
                    {
2966
                        _LMConnector.Commit();
2967
                        RemoveConnectorForReModelingLine(newConnector);
2968
                        ZeroLengthModelItemIDReverse.Add(_LMConnector.ModelItemID.ToString());
2969
                        ReleaseCOMObjects(_LMConnector);
2970
                    }
2971
                    ReleaseCOMObjects(placeRunInputs);
2972
                    ReleaseCOMObjects(_LMAItem);
2973
                }
2974

    
2975
                line.SPPID.ModelItemId = newConnector.ModelItemID.ToString();
2976
                ReleaseCOMObjects(newConnector);
2977
            }
2978

    
2979
            ReleaseCOMObjects(modelItem);
2980
        }
2981

    
2982
        /// <summary>
2983
        /// Remodeling 과정에서 생긴 불필요한 Connector 제거
2984
        /// </summary>
2985
        /// <param name="connector"></param>
2986
        private void RemoveConnectorForReModelingLine(LMConnector connector)
2987
        {
2988
            Dictionary<LMConnector, List<double[]>> dicVertices = GetPipeRunVertices(connector.ModelItemID.ToString());
2989
            foreach (var item in dicVertices)
2990
            {
2991
                if (item.Value.Count == 2)
2992
                {
2993
                    bool result = false;
2994
                    foreach (var point in item.Value)
2995
                    {
2996
                        if (point[0] < 0 || point[1] < 0)
2997
                        {
2998
                            result = true;
2999
                            _placement.PIDRemovePlacement(item.Key.AsLMRepresentation());
3000
                            break;
3001
                        }
3002
                    }
3003

    
3004
                    if (result)
3005
                        break;
3006
                }
3007
            }
3008
            foreach (var item in dicVertices)
3009
                ReleaseCOMObjects(item.Key);
3010
        }
3011

    
3012
        /// <summary>
3013
        /// Symbol이 모델링된 SPPPID Symbol Object를 반환 - 연결된 Symbol이 ChildSymbol일 수도 있기때문에 메서드 개발
3014
        /// </summary>
3015
        /// <param name="symbol"></param>
3016
        /// <param name="line"></param>
3017
        /// <returns></returns>
3018
        private LMSymbol GetTargetSymbol(Symbol symbol, Line line)
3019
        {
3020
            LMSymbol _LMSymbol = null;
3021
            foreach (var connector in symbol.CONNECTORS)
3022
            {
3023
                if (connector.CONNECTEDITEM == line.UID)
3024
                {
3025
                    if (connector.Index == 0)
3026
                        _LMSymbol = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
3027
                    else
3028
                    {
3029
                        ChildSymbol child = null;
3030
                        foreach (var childSymbol in symbol.ChildSymbols)
3031
                        {
3032
                            if (childSymbol.Connectors.Contains(connector))
3033
                                child = childSymbol;
3034
                            else
3035
                                child = GetChildSymbolByConnector(childSymbol, connector);
3036

    
3037
                            if (child != null)
3038
                                break;
3039
                        }
3040

    
3041
                        if (child != null)
3042
                            _LMSymbol = dataSource.GetSymbol(child.SPPID.RepresentationId);
3043
                    }
3044

    
3045
                    break;
3046
                }
3047
            }
3048

    
3049
            return _LMSymbol;
3050
        }
3051

    
3052
        /// <summary>
3053
        /// Connector를 가지고 있는 ChildSymbol Object 반환
3054
        /// </summary>
3055
        /// <param name="item"></param>
3056
        /// <param name="connector"></param>
3057
        /// <returns></returns>
3058
        private ChildSymbol GetChildSymbolByConnector(ChildSymbol item, Connector connector)
3059
        {
3060
            foreach (var childSymbol in item.ChildSymbols)
3061
            {
3062
                if (childSymbol.Connectors.Contains(connector))
3063
                    return childSymbol;
3064
                else
3065
                    return GetChildSymbolByConnector(childSymbol, connector);
3066
            }
3067

    
3068
            return null;
3069
        }
3070

    
3071
        /// <summary>
3072
        /// EndBreak 모델링 메서드
3073
        /// </summary>
3074
        /// <param name="endBreak"></param>
3075
        private void EndBreakModeling(EndBreak endBreak)
3076
        {
3077
            object ownerObj = SPPIDUtil.FindObjectByUID(document, endBreak.OWNER);
3078
            object connectedItem = SPPIDUtil.FindObjectByUID(document, endBreak.PROPERTIES.Find(x => x.ATTRIBUTE == "Connected Item").VALUE);
3079

    
3080
            LMConnector targetLMConnector = FindBreakLineTarget(ownerObj, connectedItem);
3081
            if (ownerObj.GetType() == typeof(Symbol) && connectedItem.GetType() == typeof(Symbol) && targetLMConnector != null)
3082
                targetLMConnector = ReModelingZeroLengthLMConnectorForSegment(targetLMConnector);
3083

    
3084
            if (targetLMConnector != null)
3085
            {
3086
                // LEADER Line 검사
3087
                bool leaderLine = false;
3088
                SymbolMapping symbolMapping = document.SymbolMappings.Find(x => x.UID == endBreak.DBUID);
3089
                if (symbolMapping != null)
3090
                    leaderLine = symbolMapping.LEADERLINE;
3091

    
3092
                SegmentLocation location;
3093
                double[] point = GetSegmentPoint(ownerObj, connectedItem, targetLMConnector, out location);
3094
                double[] array = null;
3095
                if (point != null)
3096
                    array = new double[] { 0, point[0], point[1] };
3097
                else
3098
                    array = new double[] { 0, endBreak.SPPID.ORIGINAL_X, endBreak.SPPID.ORIGINAL_Y };
3099
                LMLabelPersist _LmLabelPersist = _placement.PIDPlaceLabel(endBreak.SPPID.MAPPINGNAME, array, null, null, LabeledItem: targetLMConnector.AsLMRepresentation(), IsLeaderVisible: leaderLine);
3100
                if (_LmLabelPersist != null)
3101
                {
3102
                    _LmLabelPersist.Commit();
3103
                    endBreak.SPPID.RepresentationId = _LmLabelPersist.AsLMRepresentation().Id;
3104
                    if (_LmLabelPersist.ModelItemObject != null)
3105
                        endBreak.SPPID.ModelItemID = _LmLabelPersist.ModelItemID.ToString();
3106
                    endBreak.SPPID.GraphicOID = _LmLabelPersist.GraphicOID.ToString();
3107

    
3108
                    MoveSmartLabelObject(endBreak.SPPID.GraphicOID, location);
3109

    
3110
                    ReleaseCOMObjects(_LmLabelPersist);
3111
                }
3112
                ReleaseCOMObjects(targetLMConnector);
3113
            }
3114
            else
3115
            {
3116
                Log.Write("End Break UID : " + endBreak.UID);
3117
                Log.Write("Can't find targetLMConnector");
3118
            }
3119
        }
3120

    
3121
        private void MoveSmartLabelObject(string graphicOID, SegmentLocation location)
3122
        {
3123
            double x = 0, y = 0;
3124
            if (location.HasFlag(SegmentLocation.Up))
3125
                y = GridSetting.GetInstance().Length * 3;
3126
            else if (location.HasFlag(SegmentLocation.Down))
3127
                y = -GridSetting.GetInstance().Length * 3;
3128

    
3129
            if (location.HasFlag(SegmentLocation.Right))
3130
                x = GridSetting.GetInstance().Length * 3;
3131
            else if (location.HasFlag(SegmentLocation.Left))
3132
                x = -GridSetting.GetInstance().Length * 3;
3133

    
3134
            if (x != 0 || y != 0)
3135
            {
3136
                radApp.ActiveSelectSet.RemoveAll();
3137
                Ingr.RAD2D.Components.SmartLabel.SmartLabel dependency = radApp.ActiveDocument.ActiveSheet.DrawingObjects[graphicOID] as Ingr.RAD2D.Components.SmartLabel.SmartLabel;
3138
                if (dependency != null)
3139
                {
3140
                    radApp.ActiveSelectSet.Add(dependency);
3141
                    Ingr.RAD2D.Transform transform = dependency.GetTransform();
3142
                    transform.DefineByMove2d(x, y);
3143
                    radApp.ActiveSelectSet.Transform(transform, true);
3144
                    radApp.ActiveSelectSet.RemoveAll();
3145
                }
3146
            }
3147
        }
3148

    
3149
        private LMConnector ReModelingZeroLengthLMConnectorForSegment(LMConnector connector, string changeSymbolPath = null)
3150
        {
3151
            string symbolPath = string.Empty;
3152
            #region get symbol path
3153
            if (string.IsNullOrEmpty(changeSymbolPath))
3154
            {
3155
                LMModelItem modelItem = dataSource.GetModelItem(connector.ModelItemID);
3156
                symbolPath = GetSPPIDFileName(modelItem);
3157
                ReleaseCOMObjects(modelItem);
3158
            }
3159
            else
3160
                symbolPath = changeSymbolPath;
3161

    
3162
            #endregion
3163

    
3164
            LMConnector newConnector = null;
3165
            dynamic OID = connector.GraphicOID.ToString();
3166
            Ingr.RAD2D.Components.Connector.Connector drawingObject = radApp.ActiveDocument.ActiveSheet.DrawingObjects[OID] as Ingr.RAD2D.Components.Connector.Connector;
3167
            Ingr.RAD2D.LineStringGeometry2d lineStringGeometry = drawingObject.GetGeometry() as Ingr.RAD2D.LineStringGeometry2d;
3168
            int verticesCount = lineStringGeometry.VertexCount;
3169
            PlaceRunInputs placeRunInputs = new PlaceRunInputs();
3170
            LMAItem _LMAItem = _placement.PIDCreateItem(symbolPath);
3171

    
3172
            if (Convert.ToBoolean(connector.IsZeroLength))
3173
            {
3174
                double[] vertices = null;
3175
                lineStringGeometry.GetVertices(ref verticesCount, ref vertices);
3176
                double x = 0;
3177
                double y = 0;
3178
                lineStringGeometry.GetVertex(1, ref x, ref y);
3179

    
3180
                string flowDirection = string.Empty;
3181
                LMAAttribute flowAttribute = connector.ModelItemObject.Attributes["FlowDirection"];
3182
                if (flowAttribute != null && !DBNull.Value.Equals(flowAttribute.Value))
3183
                    flowDirection = flowAttribute.Value.ToString();
3184

    
3185
                if (flowDirection == "End 1 is downstream (Outlet)")
3186
                {
3187
                    placeRunInputs.AddSymbolTarget(connector.ConnectItem2SymbolObject, x, y);
3188
                    placeRunInputs.AddSymbolTarget(connector.ConnectItem1SymbolObject, x, y);
3189
                    flowDirection = "End 1 is upstream (Inlet)";
3190
                }
3191
                else
3192
                {
3193
                    placeRunInputs.AddSymbolTarget(connector.ConnectItem1SymbolObject, x, y);
3194
                    placeRunInputs.AddSymbolTarget(connector.ConnectItem2SymbolObject, x, y);
3195
                }
3196
                string oldModelItemId = connector.ModelItemID.ToString();
3197
                _placement.PIDRemovePlacement(connector.AsLMRepresentation());
3198
                newConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
3199
                newConnector.Commit();
3200
                ZeroLengthSymbolToSymbolModelItemID.Add(newConnector.ModelItemID.ToString());
3201
                if (!string.IsNullOrEmpty(flowDirection))
3202
                    newConnector.ModelItemObject.Attributes["FlowDirection"].Value = flowDirection;
3203
                ReleaseCOMObjects(connector);
3204

    
3205
                foreach (var line in document.LINES.FindAll(z => z.SPPID.ModelItemId == oldModelItemId))
3206
                {
3207
                    foreach (var repId in line.SPPID.Representations)
3208
                    {
3209
                        LMConnector _connector = dataSource.GetConnector(repId);
3210
                        if (_connector != null && _connector.ItemStatus.ToString() == "Active")
3211
                        {
3212
                            if (line.SPPID.ModelItemId != _connector.ModelItemID.ToString())
3213
                            {
3214
                                line.SPPID.ModelItemId = _connector.ModelItemID.ToString();
3215
                                line.SPPID.Representations = GetRepresentations(line.SPPID.ModelItemId);
3216
                            }
3217
                        }
3218
                        ReleaseCOMObjects(_connector);
3219
                        _connector = null;
3220
                    }
3221
                }
3222
            }
3223

    
3224
            return newConnector;
3225
        }
3226

    
3227
        /// <summary>
3228
        /// SpecBreak Modeling 메서드
3229
        /// </summary>
3230
        /// <param name="specBreak"></param>
3231
        private void SpecBreakModeling(SpecBreak specBreak)
3232
        {
3233
            object upStreamObj = SPPIDUtil.FindObjectByUID(document, specBreak.UpStreamUID);
3234
            object downStreamObj = SPPIDUtil.FindObjectByUID(document, specBreak.DownStreamUID);
3235

    
3236
            if (upStreamObj != null &&
3237
                downStreamObj != null)
3238
            {
3239
                LMConnector targetLMConnector = FindBreakLineTarget(upStreamObj, downStreamObj);
3240
                if (upStreamObj.GetType() == typeof(Symbol) && downStreamObj.GetType() == typeof(Symbol) &&
3241
                    targetLMConnector != null &&
3242
                    !IsModelingEndBreak(upStreamObj as Symbol, downStreamObj as Symbol))
3243
                    targetLMConnector = ReModelingZeroLengthLMConnectorForSegment(targetLMConnector);
3244

    
3245
                if (targetLMConnector != null)
3246
                {
3247
                    foreach (var attribute in specBreak.ATTRIBUTES)
3248
                    {
3249
                        AttributeMapping mapping = document.AttributeMappings.Find(x => x.UID == attribute.UID);
3250
                        if (mapping != null && !string.IsNullOrEmpty(mapping.SPPIDSYMBOLNAME) && mapping.SPPIDSYMBOLNAME != "None")
3251
                        {
3252
                            string MappingPath = mapping.SPPIDSYMBOLNAME;
3253
                            SegmentLocation location;
3254
                            double[] point = GetSegmentPoint(upStreamObj, downStreamObj, targetLMConnector, out location);
3255
                            double[] array = null;
3256
                            if (point != null)
3257
                                array = new double[] { 0, point[0], point[1] };
3258
                            else
3259
                                array = new double[] { 0, specBreak.SPPID.ORIGINAL_X, specBreak.SPPID.ORIGINAL_Y };
3260
                            LMLabelPersist _LmLabelPersist = _placement.PIDPlaceLabel(MappingPath, array, null, null, LabeledItem: targetLMConnector.AsLMRepresentation(), IsLeaderVisible: mapping.LeaderLine);
3261

    
3262
                            if (_LmLabelPersist != null)
3263
                            {
3264
                                _LmLabelPersist.Commit();
3265
                                specBreak.SPPID.RepresentationId = _LmLabelPersist.AsLMRepresentation().Id;
3266
                                if (_LmLabelPersist.ModelItemObject != null)
3267
                                    specBreak.SPPID.ModelItemID = _LmLabelPersist.ModelItemID.ToString();
3268
                                specBreak.SPPID.GraphicOID = _LmLabelPersist.GraphicOID.ToString();
3269

    
3270
                                MoveSmartLabelObject(specBreak.SPPID.GraphicOID, location);
3271

    
3272
                                ReleaseCOMObjects(_LmLabelPersist);
3273
                            }
3274
                        }
3275
                    }
3276

    
3277
                    Property property = specBreak.PROPERTIES.Find(loop => loop.ATTRIBUTE == "Show");
3278
                    if (property != null && !string.IsNullOrEmpty(property.VALUE) && property.VALUE.Equals("True"))
3279
                    {
3280
                        // temp
3281
                        ReleaseCOMObjects(_placement.PIDPlaceSymbol(@"\Design\Annotation\Graphics\Break.sym", specBreak.SPPID.ORIGINAL_X, specBreak.SPPID.ORIGINAL_Y, null, Rotation: specBreak.ANGLE));
3282
                    }
3283
                    ReleaseCOMObjects(targetLMConnector);
3284
                }
3285
                else
3286
                {
3287
                    Log.Write("Spec Break UID : " + specBreak.UID);
3288
                    Log.Write("Can't find targetLMConnector");
3289
                }
3290
            }
3291
        }
3292

    
3293
        private LMConnector FindBreakLineTarget(object targetObj, object connectedObj)
3294
        {
3295
            LMConnector targetConnector = null;
3296
            Symbol targetSymbol = targetObj as Symbol;
3297
            Symbol connectedSymbol = connectedObj as Symbol;
3298
            Line targetLine = targetObj as Line;
3299
            Line connectedLine = connectedObj as Line;
3300
            if (targetSymbol != null && connectedSymbol != null)
3301
            {
3302
                LMSymbol targetLMSymbol = dataSource.GetSymbol(targetSymbol.SPPID.RepresentationId);
3303
                LMSymbol connectedLMSymbol = dataSource.GetSymbol(connectedSymbol.SPPID.RepresentationId);
3304

    
3305
                foreach (LMConnector connector in targetLMSymbol.Avoid1Connectors)
3306
                {
3307
                    if (connector.ItemStatus.ToString() != "Active")
3308
                        continue;
3309

    
3310
                    if (connector.ConnectItem1SymbolObject.Id == connectedLMSymbol.Id)
3311
                    {
3312
                        targetConnector = connector;
3313
                        break;
3314
                    }
3315
                    else if (connector.ConnectItem2SymbolObject.Id == connectedLMSymbol.Id)
3316
                    {
3317
                        targetConnector = connector;
3318
                        break;
3319
                    }
3320
                }
3321

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

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

    
3339
                ReleaseCOMObjects(targetLMSymbol);
3340
                ReleaseCOMObjects(connectedLMSymbol);
3341
            }
3342
            else if (targetLine != null && connectedLine != null)
3343
            {
3344
                LMModelItem targetModelItem = dataSource.GetModelItem(targetLine.SPPID.ModelItemId);
3345
                LMModelItem connectedModelItem = dataSource.GetModelItem(connectedLine.SPPID.ModelItemId);
3346

    
3347
                if (targetModelItem != null && targetModelItem.ItemStatus.ToString() == "Active" && connectedModelItem != null && connectedModelItem.ItemStatus.ToString() == "Active")
3348
                {
3349
                    foreach (LMRepresentation rep in targetModelItem.Representations)
3350
                    {
3351
                        if (targetConnector != null)
3352
                            break;
3353

    
3354
                        if (rep.Attributes["RepresentationType"].Value.ToString() == "Connector" && rep.Attributes["ItemStatus"].Value.ToString() == "Active")
3355
                        {
3356
                            LMConnector _LMConnector = dataSource.GetConnector(rep.Id);
3357

    
3358
                            if (IsConnected(_LMConnector, connectedModelItem))
3359
                                targetConnector = _LMConnector;
3360
                            else
3361
                                ReleaseCOMObjects(_LMConnector);
3362
                        }
3363
                    }
3364

    
3365
                    ReleaseCOMObjects(targetModelItem);
3366
                }
3367
            }
3368
            else
3369
            {
3370
                LMSymbol connectedLMSymbol = null;
3371
                if (connectedSymbol != null)
3372
                    connectedLMSymbol = dataSource.GetSymbol(connectedSymbol.SPPID.RepresentationId);
3373
                else if (targetSymbol != null)
3374
                    connectedLMSymbol = dataSource.GetSymbol(targetSymbol.SPPID.RepresentationId);
3375
                else
3376
                {
3377

    
3378
                }
3379
                LMModelItem targetModelItem = null;
3380
                if (targetLine != null)
3381
                    targetModelItem = dataSource.GetModelItem(targetLine.SPPID.ModelItemId);
3382
                else if (connectedLine != null)
3383
                    targetModelItem = dataSource.GetModelItem(connectedLine.SPPID.ModelItemId);
3384
                else
3385
                {
3386

    
3387
                }
3388
                if (connectedLMSymbol != null && targetModelItem != null)
3389
                {
3390
                    foreach (LMConnector connector in connectedLMSymbol.Avoid1Connectors)
3391
                    {
3392
                        if (connector.ItemStatus.ToString() != "Active")
3393
                            continue;
3394

    
3395
                        if (IsConnected(connector, targetModelItem))
3396
                        {
3397
                            targetConnector = connector;
3398
                            break;
3399
                        }
3400
                    }
3401

    
3402
                    if (targetConnector == null)
3403
                    {
3404
                        foreach (LMConnector connector in connectedLMSymbol.Avoid2Connectors)
3405
                        {
3406
                            if (connector.ItemStatus.ToString() != "Active")
3407
                                continue;
3408

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

    
3418
            }
3419

    
3420
            return targetConnector;
3421
        }
3422

    
3423
        private double[] GetSegmentPoint(object targetObj, object connObj, LMConnector targetConnector, out SegmentLocation location)
3424
        {
3425
            double[] result = null;
3426
            Line targetLine = targetObj as Line;
3427
            Symbol targetSymbol = targetObj as Symbol;
3428
            Line connLine = connObj as Line;
3429
            Symbol connSymbol = connObj as Symbol;
3430
            location = SegmentLocation.None;
3431
            if (Convert.ToBoolean(targetConnector.IsZeroLength))
3432
            {
3433
                result = GetConnectorVertices(targetConnector)[0];
3434
                if (targetSymbol != null && connSymbol != null)
3435
                {
3436
                    SlopeType slopeType = SPPIDUtil.CalcSlope(targetSymbol.SPPID.SPPID_X, targetSymbol.SPPID.SPPID_Y, connSymbol.SPPID.SPPID_X, connSymbol.SPPID.SPPID_Y);
3437
                    result = new double[] { result[0], result[1] };
3438
                    if (slopeType == SlopeType.HORIZONTAL)
3439
                        location = SegmentLocation.Up;
3440
                    else if (slopeType == SlopeType.VERTICAL)
3441
                        location = SegmentLocation.Right;
3442
                }
3443
                else if (targetLine != null)
3444
                {
3445
                    result = new double[] { result[0], result[1] };
3446
                    if (targetLine.SlopeType == SlopeType.HORIZONTAL)
3447
                        location = SegmentLocation.Up;
3448
                    else if (targetLine.SlopeType == SlopeType.VERTICAL)
3449
                        location = SegmentLocation.Right;
3450
                }
3451
                else if (connLine != null)
3452
                {
3453
                    result = new double[] { result[0], result[1] };
3454
                    if (connLine.SlopeType == SlopeType.HORIZONTAL)
3455
                        location = SegmentLocation.Up;
3456
                    else if (connLine.SlopeType == SlopeType.VERTICAL)
3457
                        location = SegmentLocation.Right;
3458
                }
3459
            }
3460
            else
3461
            {
3462
                if (targetObj.GetType() == typeof(Line) && connObj.GetType() == typeof(Line))
3463
                {
3464
                    Line line = connObj as Line;
3465
                    LMConnector connectedConnector = null;
3466
                    int connIndex = 0;
3467
                    LMModelItem modelItem = dataSource.GetModelItem(line.SPPID.ModelItemId);
3468
                    FindConnectedConnector(targetConnector, modelItem, ref connectedConnector, ref connIndex);
3469

    
3470
                    List<double[]> vertices = GetConnectorVertices(targetConnector);
3471

    
3472
                    ReleaseCOMObjects(modelItem);
3473
                    ReleaseCOMObjects(connectedConnector);
3474

    
3475
                    if (vertices.Count > 0)
3476
                    {
3477
                        if (connIndex == 1)
3478
                            result = vertices[0];
3479
                        else if (connIndex == 2)
3480
                            result = vertices[vertices.Count - 1];
3481

    
3482
                        if (targetLine.SlopeType == SlopeType.HORIZONTAL)
3483
                        {
3484
                            result = new double[] { result[0], result[1] };
3485
                            location = SegmentLocation.Up;
3486
                            if (targetLine.CONNECTORS[0].CONNECTEDITEM == connLine.UID && targetLine.SPPID.START_X < targetLine.SPPID.END_X)
3487
                                location = location | SegmentLocation.Right;
3488
                            else if (targetLine.CONNECTORS[0].CONNECTEDITEM == connLine.UID && targetLine.SPPID.START_X > targetLine.SPPID.END_X)
3489
                                location = location | SegmentLocation.Left;
3490
                            else if (targetLine.CONNECTORS[1].CONNECTEDITEM == connLine.UID && targetLine.SPPID.START_X < targetLine.SPPID.END_X)
3491
                                location = location | SegmentLocation.Left;
3492
                            else if (targetLine.CONNECTORS[1].CONNECTEDITEM == connLine.UID && targetLine.SPPID.START_X > targetLine.SPPID.END_X)
3493
                                location = location | SegmentLocation.Right;
3494
                        }
3495
                        else if (targetLine.SlopeType == SlopeType.VERTICAL)
3496
                        {
3497
                            result = new double[] { result[0], result[1] };
3498
                            location = SegmentLocation.Right;
3499
                            if (targetLine.CONNECTORS[0].CONNECTEDITEM == connLine.UID && targetLine.SPPID.START_Y < targetLine.SPPID.END_Y)
3500
                                location = location | SegmentLocation.Up;
3501
                            else if (targetLine.CONNECTORS[0].CONNECTEDITEM == connLine.UID && targetLine.SPPID.START_Y > targetLine.SPPID.END_Y)
3502
                                location = location | SegmentLocation.Down;
3503
                            else if (targetLine.CONNECTORS[1].CONNECTEDITEM == connLine.UID && targetLine.SPPID.START_Y < targetLine.SPPID.END_Y)
3504
                                location = location | SegmentLocation.Down;
3505
                            else if (targetLine.CONNECTORS[1].CONNECTEDITEM == connLine.UID && targetLine.SPPID.START_Y > targetLine.SPPID.END_Y)
3506
                                location = location | SegmentLocation.Up;
3507
                        }
3508

    
3509
                    }
3510
                }
3511
                else
3512
                {
3513
                    Log.Write("error in GetSegemtPoint");
3514
                }
3515
            }
3516

    
3517
            return result;
3518
        }
3519

    
3520
        private bool IsConnected(LMConnector connector, LMModelItem modelItem)
3521
        {
3522
            bool result = false;
3523

    
3524
            foreach (LMRepresentation rep in modelItem.Representations)
3525
            {
3526
                if (result)
3527
                    break;
3528

    
3529
                if (rep.Attributes["RepresentationType"].Value.ToString() == "Connector" && rep.Attributes["ItemStatus"].Value.ToString() == "Active")
3530
                {
3531
                    LMConnector _LMConnector = dataSource.GetConnector(rep.Id);
3532

    
3533
                    if (_LMConnector.ConnectItem1SymbolObject != null &&
3534
                        connector.ConnectItem1SymbolObject != null &&
3535
                        _LMConnector.ConnectItem1SymbolObject.Id == connector.ConnectItem1SymbolObject.Id)
3536
                    {
3537
                        result = true;
3538
                        ReleaseCOMObjects(_LMConnector);
3539
                        break;
3540
                    }
3541
                    else if (_LMConnector.ConnectItem1SymbolObject != null &&
3542
                        connector.ConnectItem2SymbolObject != null &&
3543
                        _LMConnector.ConnectItem1SymbolObject.Id == connector.ConnectItem2SymbolObject.Id)
3544
                    {
3545
                        result = true;
3546
                        ReleaseCOMObjects(_LMConnector);
3547
                        break;
3548
                    }
3549
                    else if (_LMConnector.ConnectItem2SymbolObject != null &&
3550
                        connector.ConnectItem1SymbolObject != null &&
3551
                        _LMConnector.ConnectItem2SymbolObject.Id == connector.ConnectItem1SymbolObject.Id)
3552
                    {
3553
                        result = true;
3554
                        ReleaseCOMObjects(_LMConnector);
3555
                        break;
3556
                    }
3557
                    else if (_LMConnector.ConnectItem2SymbolObject != null &&
3558
                        connector.ConnectItem2SymbolObject != null &&
3559
                        _LMConnector.ConnectItem2SymbolObject.Id == connector.ConnectItem2SymbolObject.Id)
3560
                    {
3561
                        result = true;
3562
                        ReleaseCOMObjects(_LMConnector);
3563
                        break;
3564
                    }
3565

    
3566
                    ReleaseCOMObjects(_LMConnector);
3567
                }
3568
            }
3569

    
3570

    
3571
            return result;
3572
        }
3573

    
3574
        private void FindConnectedConnector(LMConnector connector, LMModelItem modelItem, ref LMConnector connectedConnector, ref int connectorIndex)
3575
        {
3576
            foreach (LMRepresentation rep in modelItem.Representations)
3577
            {
3578
                if (connectedConnector != null)
3579
                    break;
3580

    
3581
                if (rep.Attributes["RepresentationType"].Value.ToString() == "Connector" && rep.Attributes["ItemStatus"].Value.ToString() == "Active")
3582
                {
3583
                    LMConnector _LMConnector = dataSource.GetConnector(rep.Id);
3584

    
3585
                    if (_LMConnector.ConnectItem1SymbolObject != null &&
3586
                        connector.ConnectItem1SymbolObject != null &&
3587
                        _LMConnector.ConnectItem1SymbolObject.Id == connector.ConnectItem1SymbolObject.Id)
3588
                    {
3589
                        connectedConnector = _LMConnector;
3590
                        connectorIndex = 1;
3591
                        break;
3592
                    }
3593
                    else if (_LMConnector.ConnectItem1SymbolObject != null &&
3594
                        connector.ConnectItem2SymbolObject != null &&
3595
                        _LMConnector.ConnectItem1SymbolObject.Id == connector.ConnectItem2SymbolObject.Id)
3596
                    {
3597
                        connectedConnector = _LMConnector;
3598
                        connectorIndex = 2;
3599
                        break;
3600
                    }
3601
                    else if (_LMConnector.ConnectItem2SymbolObject != null &&
3602
                        connector.ConnectItem1SymbolObject != null &&
3603
                        _LMConnector.ConnectItem2SymbolObject.Id == connector.ConnectItem1SymbolObject.Id)
3604
                    {
3605
                        connectedConnector = _LMConnector;
3606
                        connectorIndex = 1;
3607
                        break;
3608
                    }
3609
                    else if (_LMConnector.ConnectItem2SymbolObject != null &&
3610
                        connector.ConnectItem2SymbolObject != null &&
3611
                        _LMConnector.ConnectItem2SymbolObject.Id == connector.ConnectItem2SymbolObject.Id)
3612
                    {
3613
                        connectedConnector = _LMConnector;
3614
                        connectorIndex = 2;
3615
                        break;
3616
                    }
3617

    
3618
                    if (connectedConnector == null)
3619
                        ReleaseCOMObjects(_LMConnector);
3620
                }
3621
            }
3622
        }
3623

    
3624
        /// <summary>
3625
        /// FromModelItem을 ToModelItem으로 PipeRunJoin하는 메서드
3626
        /// </summary>
3627
        /// <param name="modelItemID1"></param>
3628
        /// <param name="modelItemID2"></param>
3629
        private void JoinRun(string modelId1, string modelId2, ref string survivorId, bool IsSameConnector = true)
3630
        {
3631
            try
3632
            {
3633
                LMModelItem modelItem1 = dataSource.GetModelItem(modelId1);
3634
                LMConnector connector1 = GetLMConnectorFirst(modelId1);
3635
                List<double[]> vertices1 = null;
3636
                string graphicOID1 = string.Empty;
3637
                if (connector1 != null)
3638
                {
3639
                    vertices1 = GetConnectorVertices(connector1);
3640
                    graphicOID1 = connector1.GraphicOID.ToString();
3641
                }
3642
                LMAItem item1 = modelItem1.AsLMAItem();
3643
                ReleaseCOMObjects(connector1);
3644
                connector1 = null;
3645

    
3646
                LMModelItem modelItem2 = dataSource.GetModelItem(modelId2);
3647
                LMConnector connector2 = GetLMConnectorFirst(modelId2);
3648
                List<double[]> vertices2 = null;
3649
                string graphicOID2 = string.Empty;
3650
                if (connector2 != null)
3651
                {
3652
                    vertices2 = GetConnectorVertices(connector2);
3653
                    graphicOID2 = connector2.GraphicOID.ToString();
3654
                }
3655
                LMAItem item2 = modelItem2.AsLMAItem();
3656
                ReleaseCOMObjects(connector2);
3657
                connector2 = null;
3658

    
3659
                // item2가 item1으로 조인
3660
                _placement.PIDJoinRuns(ref item1, ref item2);
3661
                item1.Commit();
3662
                item2.Commit();
3663

    
3664
                string beforeID = string.Empty;
3665
                string afterID = string.Empty;
3666

    
3667
                if (modelItem1.ItemStatus.ToString() == "Active" && modelItem2.ItemStatus.ToString() != "Active")
3668
                {
3669
                    beforeID = modelItem2.Id;
3670
                    afterID = modelItem1.Id;
3671
                    survivorId = afterID;
3672
                }
3673
                else if (modelItem1.ItemStatus.ToString() != "Active" && modelItem2.ItemStatus.ToString() == "Active")
3674
                {
3675
                    beforeID = modelItem1.Id;
3676
                    afterID = modelItem2.Id;
3677
                    survivorId = afterID;
3678
                }
3679
                else if (modelItem1.ItemStatus.ToString() == "Active" && modelItem2.ItemStatus.ToString() == "Active")
3680
                {
3681
                    int model1Cnt = GetConnectorCount(modelId1);
3682
                    int model2Cnt = GetConnectorCount(modelId2);
3683
                    if (model1Cnt == 0)
3684
                    {
3685
                        beforeID = modelItem1.Id;
3686
                        afterID = modelItem2.Id;
3687
                        survivorId = afterID;
3688
                    }
3689
                    else if (model2Cnt == 0)
3690
                    {
3691
                        beforeID = modelItem2.Id;
3692
                        afterID = modelItem1.Id;
3693
                        survivorId = afterID;
3694
                    }
3695
                    else
3696
                        survivorId = null;
3697
                }
3698
                else
3699
                {
3700
                    Log.Write("잘못된 경우");
3701
                    survivorId = null;
3702
                }
3703

    
3704
                if (!string.IsNullOrEmpty(beforeID) && !string.IsNullOrEmpty(afterID))
3705
                {
3706
                    List<Line> lines = SPPIDUtil.FindLinesByModelId(document, beforeID);
3707
                    foreach (var line in lines)
3708
                        line.SPPID.ModelItemId = afterID;
3709
                }
3710

    
3711
                ReleaseCOMObjects(modelItem1);
3712
                ReleaseCOMObjects(item1);
3713
                ReleaseCOMObjects(modelItem2);
3714
                ReleaseCOMObjects(item2);
3715
            }
3716
            catch (Exception ex)
3717
            {
3718
                Log.Write("Join Error");
3719
                Log.Write(ex.Message + "\r\n" + ex.StackTrace);
3720
            }
3721
        }
3722

    
3723
        private bool IsModelingEndBreak(Symbol symbol1, Symbol symbol2)
3724
        {
3725
            bool result = false;
3726
            List<EndBreak> endBreaks = document.EndBreaks.FindAll(x =>
3727
           (x.OWNER == symbol1.UID || x.OWNER == symbol2.UID) &&
3728
           (x.PROPERTIES.Find(y => y.ATTRIBUTE == "Connected Item").VALUE == symbol1.UID || x.PROPERTIES.Find(y => y.ATTRIBUTE == "Connected Item").VALUE == symbol2.UID));
3729

    
3730
            foreach (var item in endBreaks)
3731
            {
3732
                if (!string.IsNullOrEmpty(item.SPPID.RepresentationId))
3733
                {
3734
                    result = true;
3735
                    break;
3736
                }
3737
            }
3738

    
3739
            return result;
3740
        }
3741
        private List<string> FindOtherModelItemBySymbolWhereTypePipeRun(LMSymbol symbol, string modelId)
3742
        {
3743
            List<string> temp = new List<string>();
3744
            List<LMConnector> connectors = new List<LMConnector>();
3745
            foreach (LMConnector connector in symbol.Avoid1Connectors)
3746
            {
3747
                if (connector.ItemStatus.ToString() != "Active")
3748
                    continue;
3749

    
3750
                LMModelItem modelItem = connector.ModelItemObject;
3751
                LMSymbol connOtherSymbol = FindOtherConnectedSymbol(connector);
3752
                if (modelItem.ItemStatus.ToString() == "Active" && modelItem.ItemTypeName.ToString() == "PipeRun" && modelItem.Id != modelId && !temp.Contains(modelItem.Id))
3753
                    temp.Add(modelItem.Id);
3754

    
3755
                if (temp.Contains(modelItem.Id) &&
3756
                    connOtherSymbol != null &&
3757
                    connOtherSymbol.RepresentationType.ToString() == "Branch" &&
3758
                    Convert.ToBoolean(connector.IsZeroLength))
3759
                    temp.Remove(modelItem.Id);
3760

    
3761

    
3762
                if (temp.Contains(modelItem.Id))
3763
                    connectors.Add(connector);
3764
                ReleaseCOMObjects(connOtherSymbol);
3765
                connOtherSymbol = null;
3766
                ReleaseCOMObjects(modelItem);
3767
                modelItem = null;
3768
            }
3769

    
3770
            foreach (LMConnector connector in symbol.Avoid2Connectors)
3771
            {
3772
                if (connector.ItemStatus.ToString() != "Active")
3773
                    continue;
3774

    
3775
                LMModelItem modelItem = connector.ModelItemObject;
3776
                LMSymbol connOtherSymbol = FindOtherConnectedSymbol(connector);
3777
                if (modelItem.ItemStatus.ToString() == "Active" && modelItem.ItemTypeName.ToString() == "PipeRun" && modelItem.Id != modelId && !temp.Contains(modelItem.Id))
3778
                    temp.Add(modelItem.Id);
3779

    
3780
                if (temp.Contains(modelItem.Id) &&
3781
                    connOtherSymbol != null &&
3782
                    connOtherSymbol.RepresentationType.ToString() == "Branch" &&
3783
                    Convert.ToBoolean(connector.IsZeroLength))
3784
                    temp.Remove(modelItem.Id);
3785

    
3786
                if (temp.Contains(modelItem.Id))
3787
                    connectors.Add(connector);
3788
                ReleaseCOMObjects(connOtherSymbol);
3789
                connOtherSymbol = null;
3790
                ReleaseCOMObjects(modelItem);
3791
                modelItem = null;
3792
            }
3793

    
3794

    
3795
            List<string> result = new List<string>();
3796
            string originalName = GetSPPIDFileName(modelId);
3797
            foreach (var connector in connectors)
3798
            {
3799
                string fileName = GetSPPIDFileName(connector.ModelItemID.ToString());
3800
                if (originalName == fileName)
3801
                    result.Add(connector.ModelItemID.ToString());
3802
                else
3803
                {
3804
                    if (document.LINES.Find(x => x.SPPID.ModelItemId == connector.ModelItemID.ToString()) == null && Convert.ToBoolean(connector.IsZeroLength))
3805
                        result.Add(connector.ModelItemID.ToString());
3806
                    else
3807
                    {
3808
                        Line line1 = document.LINES.Find(x => x.SPPID.ModelItemId == modelId);
3809
                        Line line2 = document.LINES.Find(x => x.SPPID.ModelItemId == connector.ModelItemID.ToString());
3810
                        if (line1 != null && line2 != null && line1.TYPE == line2.TYPE)
3811
                            result.Add(connector.ModelItemID.ToString());
3812
                    }
3813
                }
3814
            }
3815

    
3816
            foreach (var connector in connectors)
3817
                ReleaseCOMObjects(connector);
3818

    
3819
            return result;
3820

    
3821

    
3822
            LMSymbol FindOtherConnectedSymbol(LMConnector connector)
3823
            {
3824
                LMSymbol findResult = null;
3825
                if (connector.ConnectItem1SymbolObject != null && connector.ConnectItem1SymbolObject.Id != symbol.Id && connector.ConnectItem1SymbolObject.ItemStatus.ToString() == "Active")
3826
                    findResult = connector.ConnectItem1SymbolObject;
3827
                else if (connector.ConnectItem2SymbolObject != null && connector.ConnectItem2SymbolObject.Id != symbol.Id && connector.ConnectItem2SymbolObject.ItemStatus.ToString() == "Active")
3828
                    findResult = connector.ConnectItem2SymbolObject;
3829

    
3830
                return findResult;
3831
            }
3832
        }
3833

    
3834
        /// <summary>
3835
        /// PipeRun의 좌표를 가져오는 메서드
3836
        /// </summary>
3837
        /// <param name="modelId"></param>
3838
        /// <returns></returns>
3839
        private Dictionary<LMConnector, List<double[]>> GetPipeRunVertices(string modelId, bool ContainZeroLength = true)
3840
        {
3841
            Dictionary<LMConnector, List<double[]>> connectorVertices = new Dictionary<LMConnector, List<double[]>>();
3842
            LMModelItem modelItem = dataSource.GetModelItem(modelId);
3843

    
3844
            if (modelItem != null)
3845
            {
3846
                foreach (LMRepresentation rep in modelItem.Representations)
3847
                {
3848
                    if (rep.Attributes["RepresentationType"].Value.ToString() == "Connector" && rep.Attributes["ItemStatus"].Value.ToString() == "Active")
3849
                    {
3850
                        LMConnector _LMConnector = dataSource.GetConnector(rep.Id);
3851
                        if (!ContainZeroLength && Convert.ToBoolean(_LMConnector.IsZeroLength))
3852
                        {
3853
                            ReleaseCOMObjects(_LMConnector);
3854
                            _LMConnector = null;
3855
                            continue;
3856
                        }
3857
                        connectorVertices.Add(_LMConnector, new List<double[]>());
3858
                        dynamic OID = rep.GraphicOID.ToString();
3859
                        Ingr.RAD2D.Components.Connector.Connector drawingObject = radApp.ActiveDocument.ActiveSheet.DrawingObjects[OID] as Ingr.RAD2D.Components.Connector.Connector;
3860
                        Ingr.RAD2D.LineStringGeometry2d lineStringGeometry = drawingObject.GetGeometry() as Ingr.RAD2D.LineStringGeometry2d;
3861
                        int verticesCount = lineStringGeometry.VertexCount;
3862
                        double[] vertices = null;
3863
                        lineStringGeometry.GetVertices(ref verticesCount, ref vertices);
3864
                        for (int i = 0; i < verticesCount; i++)
3865
                        {
3866
                            double x = 0;
3867
                            double y = 0;
3868
                            //TODO : ddd
3869
                            lineStringGeometry.GetVertex(i, ref x, ref y);
3870
                            connectorVertices[_LMConnector].Add(new double[] { x, y });
3871
                        }
3872
                    }
3873
                }
3874

    
3875
                ReleaseCOMObjects(modelItem);
3876
            }
3877

    
3878
            return connectorVertices;
3879
        }
3880

    
3881
        private List<double[]> GetConnectorVertices(LMConnector connector)
3882
        {
3883
            List<double[]> vertices = new List<double[]>();
3884
            if (connector != null)
3885
            {
3886
                dynamic OID = connector.GraphicOID.ToString();
3887
                Ingr.RAD2D.Components.Connector.Connector drawingObject = radApp.ActiveDocument.ActiveSheet.DrawingObjects[OID] as Ingr.RAD2D.Components.Connector.Connector;
3888
                Ingr.RAD2D.LineStringGeometry2d lineStringGeometry = drawingObject.GetGeometry() as Ingr.RAD2D.LineStringGeometry2d;
3889
                int verticesCount = lineStringGeometry.VertexCount;
3890
                double[] value = null;
3891
                lineStringGeometry.GetVertices(ref verticesCount, ref value);
3892
                for (int i = 0; i < verticesCount; i++)
3893
                {
3894
                    double x = 0;
3895
                    double y = 0;
3896
                    lineStringGeometry.GetVertex(i + 1, ref x, ref y);
3897
                    vertices.Add(new double[] { x, y });
3898
                }
3899
            }
3900
            return vertices;
3901
        }
3902

    
3903
        private double GetConnectorDistance(LMConnector connector)
3904
        {
3905
            double result = 0;
3906
            List<double[]> vertices = new List<double[]>();
3907
            if (connector != null)
3908
            {
3909
                dynamic OID = connector.GraphicOID.ToString();
3910
                Ingr.RAD2D.Components.Connector.Connector drawingObject = radApp.ActiveDocument.ActiveSheet.DrawingObjects[OID] as Ingr.RAD2D.Components.Connector.Connector;
3911
                Ingr.RAD2D.LineStringGeometry2d lineStringGeometry = drawingObject.GetGeometry() as Ingr.RAD2D.LineStringGeometry2d;
3912
                int verticesCount = lineStringGeometry.VertexCount;
3913
                double[] value = null;
3914
                lineStringGeometry.GetVertices(ref verticesCount, ref value);
3915
                for (int i = 0; i < verticesCount; i++)
3916
                {
3917
                    double x = 0;
3918
                    double y = 0;
3919
                    lineStringGeometry.GetVertex(i + 1, ref x, ref y);
3920
                    vertices.Add(new double[] { x, y });
3921
                    if (vertices.Count > 1)
3922
                    {
3923
                        result += SPPIDUtil.CalcPointToPointdDistance(vertices[vertices.Count - 2][0], vertices[vertices.Count - 2][1], x, y);
3924
                    }
3925
                }
3926
            }
3927
            return result;
3928
        }
3929
        private double[] GetConnectorRange(LMConnector connector)
3930
        {
3931
            double[] result = null;
3932
            List<double[]> vertices = new List<double[]>();
3933
            if (connector != null)
3934
            {
3935
                dynamic OID = connector.GraphicOID.ToString();
3936
                Ingr.RAD2D.Components.Connector.Connector drawingObject = radApp.ActiveDocument.ActiveSheet.DrawingObjects[OID] as Ingr.RAD2D.Components.Connector.Connector;
3937
                double minX = 0;
3938
                double minY = 0;
3939
                double maxX = 0;
3940
                double maxY = 0;
3941

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

    
3991
                    if ((x1 <= connX && x2 >= connX) ||
3992
                        (y1 <= connY && y2 >= connY))
3993
                    {
3994
                        double distance = SPPIDUtil.CalcPointToPointdDistance(point1[0], point1[1], connX, connY);
3995
                        if (length >= distance)
3996
                        {
3997
                            targetConnector = item.Key;
3998
                            length = distance;
3999
                        }
4000

    
4001
                        distance = SPPIDUtil.CalcPointToPointdDistance(point2[0], point2[1], connX, connY);
4002
                        if (length >= distance)
4003
                        {
4004
                            targetConnector = item.Key;
4005
                            length = distance;
4006
                        }
4007
                    }
4008
                }
4009
            }
4010

    
4011
            // 못찾았을때.
4012
            length = double.MaxValue;
4013
            if (targetConnector == null)
4014
            {
4015
                foreach (var item in connectorVertices)
4016
                {
4017
                    List<double[]> points = item.Value;
4018

    
4019
                    foreach (double[] point in points)
4020
                    {
4021
                        double distance = SPPIDUtil.CalcPointToPointdDistance(point[0], point[1], connX, connY);
4022
                        if (length >= distance)
4023
                        {
4024
                            targetConnector = item.Key;
4025
                            length = distance;
4026
                        }
4027
                    }
4028
                }
4029
            }
4030

    
4031
            return targetConnector;
4032
        }
4033

    
4034
        private LMConnector FindTargetLMConnectorForBranch(Line line, Line targetLine, ref double x, ref double y)
4035
        {
4036
            Dictionary<LMConnector, List<double[]>> vertices = GetPipeRunVertices(targetLine.SPPID.ModelItemId);
4037
            if (vertices.Count == 0)
4038
                return null;
4039

    
4040
            double length = double.MaxValue;
4041
            LMConnector targetConnector = null;
4042
            double[] resultPoint = null;
4043
            List<double[]> targetVertices = null;
4044

    
4045
            // Vertices 포인트에 제일 가까운곳
4046
            foreach (var item in vertices)
4047
            {
4048
                List<double[]> points = item.Value;
4049
                for (int i = 0; i < points.Count; i++)
4050
                {
4051
                    double[] point = points[i];
4052
                    double tempX = point[0];
4053
                    double tempY = point[1];
4054

    
4055
                    double distance = SPPIDUtil.CalcPointToPointdDistance(tempX, tempY, x, y);
4056
                    if (length >= distance)
4057
                    {
4058
                        targetConnector = item.Key;
4059
                        length = distance;
4060
                        resultPoint = point;
4061
                        targetVertices = item.Value;
4062
                    }
4063
                }
4064
            }
4065

    
4066
            // Vertices Cross에 제일 가까운곳
4067
            foreach (var item in vertices)
4068
            {
4069
                List<double[]> points = item.Value;
4070
                for (int i = 0; i < points.Count - 1; i++)
4071
                {
4072
                    double[] point1 = points[i];
4073
                    double[] point2 = points[i + 1];
4074

    
4075
                    double maxLineX = Math.Max(point1[0], point2[0]);
4076
                    double minLineX = Math.Min(point1[0], point2[0]);
4077
                    double maxLineY = Math.Max(point1[1], point2[1]);
4078
                    double minLineY = Math.Min(point1[1], point2[1]);
4079

    
4080
                    SlopeType slope = SPPIDUtil.CalcSlope(minLineX, minLineY, maxLineX, maxLineY);
4081

    
4082
                    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]);
4083
                    if (crossingPoint != null)
4084
                    {
4085
                        double distance = SPPIDUtil.CalcPointToPointdDistance(crossingPoint[0], crossingPoint[1], x, y);
4086
                        if (length >= distance)
4087
                        {
4088
                            if (slope == SlopeType.Slope &&
4089
                                minLineX <= crossingPoint[0] && maxLineX >= crossingPoint[0] &&
4090
                                minLineY <= crossingPoint[1] && maxLineY >= crossingPoint[1])
4091
                            {
4092
                                targetConnector = item.Key;
4093
                                length = distance;
4094
                                resultPoint = crossingPoint;
4095
                                targetVertices = item.Value;
4096
                            }
4097
                            else if (slope == SlopeType.HORIZONTAL &&
4098
                                minLineX <= crossingPoint[0] && maxLineX >= crossingPoint[0])
4099
                            {
4100
                                targetConnector = item.Key;
4101
                                length = distance;
4102
                                resultPoint = crossingPoint;
4103
                                targetVertices = item.Value;
4104
                            }
4105
                            else if (slope == SlopeType.VERTICAL &&
4106
                               minLineY <= crossingPoint[1] && maxLineY >= crossingPoint[1])
4107
                            {
4108
                                targetConnector = item.Key;
4109
                                length = distance;
4110
                                resultPoint = crossingPoint;
4111
                                targetVertices = item.Value;
4112
                            }
4113
                        }
4114
                    }
4115
                }
4116
            }
4117

    
4118
            foreach (var item in vertices)
4119
                if (item.Key != null && item.Key != targetConnector)
4120
                    ReleaseCOMObjects(item.Key);
4121

    
4122
            if (SPPIDUtil.IsBranchLine(line, targetLine))
4123
            {
4124
                double tempResultX = resultPoint[0];
4125
                double tempResultY = resultPoint[1];
4126
                SPPIDUtil.ConvertGridPoint(ref tempResultX, ref tempResultY);
4127

    
4128
                GridSetting gridSetting = GridSetting.GetInstance();
4129

    
4130
                for (int i = 0; i < targetVertices.Count; i++)
4131
                {
4132
                    double[] point = targetVertices[i];
4133
                    double tempX = targetVertices[i][0];
4134
                    double tempY = targetVertices[i][1];
4135
                    SPPIDUtil.ConvertGridPoint(ref tempX, ref tempY);
4136
                    if (tempX == tempResultX && tempY == tempResultY)
4137
                    {
4138
                        if (i == 0)
4139
                        {
4140
                            LMSymbol connSymbol = targetConnector.ConnectItem1SymbolObject;
4141
                            bool containZeroLength = false;
4142
                            if (connSymbol != null)
4143
                            {
4144
                                foreach (LMConnector connector in connSymbol.Connect1Connectors)
4145
                                {
4146
                                    if (connector.ItemStatus.ToString() == "Active" && Convert.ToBoolean(connector.IsZeroLength) == true)
4147
                                        containZeroLength = true;
4148
                                }
4149
                                foreach (LMConnector connector in connSymbol.Connect2Connectors)
4150
                                {
4151
                                    if (connector.ItemStatus.ToString() == "Active" && Convert.ToBoolean(connector.IsZeroLength) == true)
4152
                                        containZeroLength = true;
4153
                                }
4154
                            }
4155

    
4156
                            if (connSymbol == null ||
4157
                                (connSymbol != null && connSymbol.ItemStatus.ToString() == "Active" && connSymbol.RepresentationType.ToString() != "Branch") ||
4158
                                containZeroLength)
4159
                            {
4160
                                bool bCalcX = false;
4161
                                bool bCalcY = false;
4162
                                if (targetLine.SlopeType == SlopeType.HORIZONTAL)
4163
                                    bCalcX = true;
4164
                                else if (targetLine.SlopeType == SlopeType.VERTICAL)
4165
                                    bCalcY = true;
4166
                                else
4167
                                {
4168
                                    bCalcX = true;
4169
                                    bCalcY = true;
4170
                                }
4171

    
4172
                                if (bCalcX)
4173
                                {
4174
                                    double nextX = targetVertices[i + 1][0];
4175
                                    double newX = 0;
4176
                                    if (nextX > tempX)
4177
                                    {
4178
                                        newX = tempX + gridSetting.Length;
4179
                                        if (newX > nextX)
4180
                                            newX = (point[0] + nextX) / 2;
4181
                                    }
4182
                                    else
4183
                                    {
4184
                                        newX = tempX - gridSetting.Length;
4185
                                        if (newX < nextX)
4186
                                            newX = (point[0] + nextX) / 2;
4187
                                    }
4188
                                    resultPoint = new double[] { newX, resultPoint[1] };
4189
                                }
4190

    
4191
                                if (bCalcY)
4192
                                {
4193
                                    double nextY = targetVertices[i + 1][1];
4194
                                    double newY = 0;
4195
                                    if (nextY > tempY)
4196
                                    {
4197
                                        newY = tempY + gridSetting.Length;
4198
                                        if (newY > nextY)
4199
                                            newY = (point[1] + nextY) / 2;
4200
                                    }
4201
                                    else
4202
                                    {
4203
                                        newY = tempY - gridSetting.Length;
4204
                                        if (newY < nextY)
4205
                                            newY = (point[1] + nextY) / 2;
4206
                                    }
4207
                                    resultPoint = new double[] { resultPoint[0], newY };
4208
                                }
4209
                            }
4210
                        }
4211
                        else if (i == targetVertices.Count - 1)
4212
                        {
4213
                            LMSymbol connSymbol = targetConnector.ConnectItem2SymbolObject;
4214
                            bool containZeroLength = false;
4215
                            if (connSymbol != null)
4216
                            {
4217
                                foreach (LMConnector connector in connSymbol.Connect1Connectors)
4218
                                {
4219
                                    if (connector.ItemStatus.ToString() == "Active" && Convert.ToBoolean(connector.IsZeroLength) == true)
4220
                                        containZeroLength = true;
4221
                                }
4222
                                foreach (LMConnector connector in connSymbol.Connect2Connectors)
4223
                                {
4224
                                    if (connector.ItemStatus.ToString() == "Active" && Convert.ToBoolean(connector.IsZeroLength) == true)
4225
                                        containZeroLength = true;
4226
                                }
4227
                            }
4228

    
4229
                            if (connSymbol == null ||
4230
                                 (connSymbol != null && connSymbol.ItemStatus.ToString() == "Active" && connSymbol.RepresentationType.ToString() != "Branch") ||
4231
                                containZeroLength)
4232
                            {
4233
                                bool bCalcX = false;
4234
                                bool bCalcY = false;
4235
                                if (targetLine.SlopeType == SlopeType.HORIZONTAL)
4236
                                    bCalcX = true;
4237
                                else if (targetLine.SlopeType == SlopeType.VERTICAL)
4238
                                    bCalcY = true;
4239
                                else
4240
                                {
4241
                                    bCalcX = true;
4242
                                    bCalcY = true;
4243
                                }
4244

    
4245
                                if (bCalcX)
4246
                                {
4247
                                    double nextX = targetVertices[i - 1][0];
4248
                                    double newX = 0;
4249
                                    if (nextX > tempX)
4250
                                    {
4251
                                        newX = tempX + gridSetting.Length;
4252
                                        if (newX > nextX)
4253
                                            newX = (point[0] + nextX) / 2;
4254
                                    }
4255
                                    else
4256
                                    {
4257
                                        newX = tempX - gridSetting.Length;
4258
                                        if (newX < nextX)
4259
                                            newX = (point[0] + nextX) / 2;
4260
                                    }
4261
                                    resultPoint = new double[] { newX, resultPoint[1] };
4262
                                }
4263

    
4264
                                if (bCalcY)
4265
                                {
4266
                                    double nextY = targetVertices[i - 1][1];
4267
                                    double newY = 0;
4268
                                    if (nextY > tempY)
4269
                                    {
4270
                                        newY = tempY + gridSetting.Length;
4271
                                        if (newY > nextY)
4272
                                            newY = (point[1] + nextY) / 2;
4273
                                    }
4274
                                    else
4275
                                    {
4276
                                        newY = tempY - gridSetting.Length;
4277
                                        if (newY < nextY)
4278
                                            newY = (point[1] + nextY) / 2;
4279
                                    }
4280
                                    resultPoint = new double[] { resultPoint[0], newY };
4281
                                }
4282
                            }
4283
                        }
4284
                        break;
4285
                    }
4286
                }
4287
            }
4288

    
4289
            x = resultPoint[0];
4290
            y = resultPoint[1];
4291

    
4292
            return targetConnector;
4293
        }
4294

    
4295
        private LMConnector GetLMConnectorOnlyOne(string modelItemID)
4296
        {
4297
            LMConnector result = null;
4298
            List<LMConnector> connectors = new List<LMConnector>();
4299
            LMModelItem modelItem = dataSource.GetModelItem(modelItemID);
4300

    
4301
            if (modelItem != null)
4302
            {
4303
                foreach (LMRepresentation rep in modelItem.Representations)
4304
                {
4305
                    if (rep.Attributes["RepresentationType"].Value.ToString() == "Connector" && rep.Attributes["ItemStatus"].Value.ToString() == "Active")
4306
                        connectors.Add(dataSource.GetConnector(rep.Id));
4307
                }
4308

    
4309
                ReleaseCOMObjects(modelItem);
4310
            }
4311

    
4312
            if (connectors.Count == 1)
4313
                result = connectors[0];
4314
            else
4315
                foreach (var item in connectors)
4316
                    ReleaseCOMObjects(item);
4317

    
4318
            return result;
4319
        }
4320

    
4321
        private LMConnector GetLMConnectorFirst(string modelItemID)
4322
        {
4323
            LMConnector result = null;
4324
            LMModelItem modelItem = dataSource.GetModelItem(modelItemID);
4325

    
4326
            if (modelItem != null)
4327
            {
4328
                foreach (LMRepresentation rep in modelItem.Representations)
4329
                {
4330
                    if (rep.Attributes["RepresentationType"].Value.ToString() == "Connector" &&
4331
                        rep.Attributes["ItemStatus"].Value.ToString() == "Active")
4332
                    {
4333
                        LMConnector connector = dataSource.GetConnector(rep.Id);
4334
                        if (!Convert.ToBoolean(connector.IsZeroLength))
4335
                        {
4336
                            result = connector;
4337
                            break;
4338
                        }
4339
                        else
4340
                        {
4341
                            ReleaseCOMObjects(connector);
4342
                            connector = null;
4343
                        }
4344
                    }
4345
                }
4346
                ReleaseCOMObjects(modelItem);
4347
                modelItem = null;
4348
            }
4349

    
4350
            return result;
4351
        }
4352

    
4353
        private int GetConnectorCount(string modelItemID)
4354
        {
4355
            LMModelItem modelItem = dataSource.GetModelItem(modelItemID);
4356
            int result = 0;
4357
            if (modelItem != null)
4358
            {
4359
                foreach (LMRepresentation rep in modelItem.Representations)
4360
                {
4361
                    if (rep.Attributes["RepresentationType"].Value.ToString() == "Connector" && rep.Attributes["ItemStatus"].Value.ToString() == "Active")
4362
                        result++;
4363
                    ReleaseCOMObjects(rep);
4364
                }
4365
                ReleaseCOMObjects(modelItem);
4366
            }
4367

    
4368
            return result;
4369
        }
4370

    
4371
        public List<string> GetRepresentations(string modelItemID)
4372
        {
4373
            List<string> result = new List<string>(); ;
4374
            LMModelItem modelItem = dataSource.GetModelItem(modelItemID);
4375
            if (modelItem != null)
4376
            {
4377
                foreach (LMRepresentation rep in modelItem.Representations)
4378
                {
4379
                    if (rep.Attributes["RepresentationType"].Value.ToString() == "Connector" && rep.Attributes["ItemStatus"].Value.ToString() == "Active")
4380
                        result.Add(rep.Id);
4381
                }
4382
                ReleaseCOMObjects(modelItem);
4383
            }
4384

    
4385
            return result;
4386
        }
4387

    
4388
        private void LineNumberModeling(LineNumber lineNumber)
4389
        {
4390
            Line line = SPPIDUtil.FindObjectByUID(document, lineNumber.CONNLINE) as Line;
4391
            if (line != null)
4392
            {
4393
                double x = 0;
4394
                double y = 0;
4395
                CalcLabelLocation(ref x, ref y, lineNumber.SPPID.ORIGINAL_X, lineNumber.SPPID.ORIGINAL_Y, lineNumber.SPPIDLabelLocation, _ETCSetting.LineNumberLocation);
4396

    
4397
                Dictionary<LMConnector, List<double[]>> connectorVertices = GetPipeRunVertices(line.SPPID.ModelItemId);
4398
                LMConnector connectedLMConnector = FindTargetLMConnectorForLabel(connectorVertices, x, y);
4399
                if (connectedLMConnector != null)
4400
                {
4401
                    double[] points = new double[] { 0, x, y };
4402
                    lineNumber.SPPID.SPPID_X = x;
4403
                    lineNumber.SPPID.SPPID_Y = y;
4404
                    LMLabelPersist _LmLabelPresist = _placement.PIDPlaceLabel(lineNumber.SPPID.MAPPINGNAME, points, null, Rotation: lineNumber.ANGLE, LabeledItem: connectedLMConnector.AsLMRepresentation(), IsLeaderVisible: false);
4405

    
4406
                    if (_LmLabelPresist != null)
4407
                    {
4408
                        _LmLabelPresist.Commit();
4409
                        lineNumber.SPPID.RepresentationId = _LmLabelPresist.AsLMRepresentation().Id;
4410
                        ReleaseCOMObjects(_LmLabelPresist);
4411
                    }
4412
                }
4413

    
4414
                foreach (var item in connectorVertices)
4415
                    ReleaseCOMObjects(item.Key);
4416
            }
4417
        }
4418
        private void LineNumberCorrectModeling(LineNumber lineNumber)
4419
        {
4420
            Line line = SPPIDUtil.FindObjectByUID(document, lineNumber.CONNLINE) as Line;
4421
            if (line == null || line.SPPID.Vertices == null)
4422
                return;
4423

    
4424
            if (!string.IsNullOrEmpty(lineNumber.SPPID.RepresentationId))
4425
            {
4426
                LMLabelPersist removeLabel = dataSource.GetLabelPersist(lineNumber.SPPID.RepresentationId);
4427
                if (removeLabel != null)
4428
                {
4429
                    lineNumber.SPPID.SPPID_X = Convert.ToDouble(removeLabel.XCoordinate);
4430
                    lineNumber.SPPID.SPPID_Y = Convert.ToDouble(removeLabel.YCoordinate);
4431

    
4432
                    GridSetting gridSetting = GridSetting.GetInstance();
4433
                    LMConnector connector = dataSource.GetConnector(removeLabel.RepresentationID);
4434

    
4435
                    double[] labelRange = null;
4436
                    GetSPPIDSymbolRange(removeLabel, ref labelRange);
4437
                    List<double[]> vertices = GetConnectorVertices(connector);
4438

    
4439
                    double[] resultStart = null;
4440
                    double[] resultEnd = null;
4441
                    double distance = double.MaxValue;
4442
                    for (int i = 0; i < vertices.Count - 1; i++)
4443
                    {
4444
                        double[] startPoint = vertices[i];
4445
                        double[] endPoint = vertices[i + 1];
4446
                        foreach (var item in line.SPPID.Vertices)
4447
                        {
4448
                            double[] lineStartPoint = item[0];
4449
                            double[] lineEndPoint = item[item.Count - 1];
4450

    
4451
                            double tempDistance = SPPIDUtil.CalcPointToPointdDistance(startPoint[0], startPoint[1], lineStartPoint[0], lineStartPoint[1]) +
4452
                                SPPIDUtil.CalcPointToPointdDistance(endPoint[0], endPoint[1], lineEndPoint[0], lineEndPoint[1]);
4453
                            if (tempDistance < distance)
4454
                            {
4455
                                distance = tempDistance;
4456
                                resultStart = startPoint;
4457
                                resultEnd = endPoint;
4458
                            }
4459
                            tempDistance = SPPIDUtil.CalcPointToPointdDistance(startPoint[0], startPoint[1], lineEndPoint[0], lineEndPoint[1]) +
4460
                                SPPIDUtil.CalcPointToPointdDistance(endPoint[0], endPoint[1], lineStartPoint[0], lineStartPoint[1]);
4461
                            if (tempDistance < distance)
4462
                            {
4463
                                distance = tempDistance;
4464
                                resultStart = startPoint;
4465
                                resultEnd = endPoint;
4466
                            }
4467
                        }
4468
                    }
4469

    
4470
                    if (resultStart != null && resultEnd != null)
4471
                    {
4472
                        SlopeType slope = SPPIDUtil.CalcSlope(resultStart[0], resultStart[1], resultEnd[0], resultEnd[1]);
4473
                        double lineStartX = 0;
4474
                        double lineStartY = 0;
4475
                        double lineEndX = 0;
4476
                        double lineEndY = 0;
4477
                        double lineNumberX = 0;
4478
                        double lineNumberY = 0;
4479
                        SPPIDUtil.ConvertPointBystring(line.STARTPOINT, ref lineStartX, ref lineStartY);
4480
                        SPPIDUtil.ConvertPointBystring(line.ENDPOINT, ref lineEndX, ref lineEndY);
4481

    
4482
                        double lineX = (lineStartX + lineEndX) / 2;
4483
                        double lineY = (lineStartY + lineEndY) / 2;
4484
                        lineNumberX = (lineNumber.X1 + lineNumber.X2) / 2;
4485
                        lineNumberY = (lineNumber.Y1 + lineNumber.Y2) / 2;
4486

    
4487
                        double SPPIDCenterX = (resultStart[0] + resultEnd[0]) / 2;
4488
                        double SPPIDCenterY = (resultStart[1] + resultEnd[1]) / 2;
4489
                        double labelCenterX = (labelRange[0] + labelRange[2]) / 2;
4490
                        double labelCenterY = (labelRange[1] + labelRange[3]) / 2;
4491

    
4492
                        double offsetX = 0;
4493
                        double offsetY = 0;
4494
                        if (slope == SlopeType.HORIZONTAL)
4495
                        {
4496
                            // Line Number 아래
4497
                            if (lineY < lineNumberY)
4498
                            {
4499
                                offsetX = labelCenterX - SPPIDCenterX;
4500
                                offsetY = labelRange[3] - SPPIDCenterY + gridSetting.Length;
4501
                                MoveLineNumber(lineNumber, offsetX, offsetY);
4502
                            }
4503
                            // Line Number 위
4504
                            else
4505
                            {
4506
                                offsetX = labelCenterX - SPPIDCenterX;
4507
                                offsetY = labelRange[1] - SPPIDCenterY - gridSetting.Length;
4508
                                MoveLineNumber(lineNumber, offsetX, offsetY);
4509
                            }
4510
                        }
4511
                        else if (slope == SlopeType.VERTICAL)
4512
                        {
4513
                            // Line Number 오르쪽
4514
                            if (lineX < lineNumberX)
4515
                            {
4516
                                offsetX = labelRange[0] - SPPIDCenterX - gridSetting.Length;
4517
                                offsetY = labelCenterY - SPPIDCenterY;
4518
                                MoveLineNumber(lineNumber, offsetX, offsetY);
4519
                            }
4520
                            // Line Number 왼쪽
4521
                            else
4522
                            {
4523
                                offsetX = labelRange[2] - SPPIDCenterX + gridSetting.Length;
4524
                                offsetY = labelCenterY - SPPIDCenterY;
4525
                                MoveLineNumber(lineNumber, offsetX, offsetY);
4526
                            }
4527
                        }
4528

    
4529
                        if (offsetY != 0 || offsetY != 0)
4530
                        {
4531
                            if (connector.ConnectItem1SymbolObject != null &&
4532
                                connector.ConnectItem1SymbolObject.RepresentationType.ToString() == "OPC")
4533
                            {
4534
                                Ingr.RAD2D.Symbol2d symbol = radApp.ActiveDocument.ActiveSheet.DrawingObjects[connector.ConnectItem1SymbolObject.GraphicOID.ToString()] as Ingr.RAD2D.Symbol2d;
4535

    
4536
                                double x1, y1, x2, y2, originX, originY;
4537
                                symbol.Range(out x1, out y1, out x2, out y2);
4538
                                symbol.GetOrigin(out originX, out originY);
4539
                                if (originX < lineNumber.SPPID.SPPID_X)
4540
                                    offsetX = -1 * (originX + gridSetting.Length * 30 - labelCenterX);
4541
                                else
4542
                                    offsetX = -1 * (originX - gridSetting.Length * 30 - labelCenterX);
4543
                            }
4544
                            else if (connector.ConnectItem2SymbolObject != null &&
4545
                                    connector.ConnectItem2SymbolObject.RepresentationType.ToString() == "OPC")
4546
                            {
4547
                                Ingr.RAD2D.Symbol2d symbol = radApp.ActiveDocument.ActiveSheet.DrawingObjects[connector.ConnectItem2SymbolObject.GraphicOID.ToString()] as Ingr.RAD2D.Symbol2d;
4548

    
4549
                                double x1, y1, x2, y2, originX, originY;
4550
                                symbol.Range(out x1, out y1, out x2, out y2);
4551
                                symbol.GetOrigin(out originX, out originY);
4552
                                if (originX < lineNumber.SPPID.SPPID_X)
4553
                                    offsetX = -1 * (originX + gridSetting.Length * 30 - labelCenterX);
4554
                                else
4555
                                    offsetX = -1 * (originX - gridSetting.Length * 30 - labelCenterX);
4556
                            }
4557

    
4558
                            radApp.ActiveSelectSet.RemoveAll();
4559
                            Ingr.RAD2D.Components.SmartLabel.SmartLabel dependency = radApp.ActiveDocument.ActiveSheet.DrawingObjects[removeLabel.GraphicOID.ToString()] as Ingr.RAD2D.Components.SmartLabel.SmartLabel;
4560
                            if (dependency != null)
4561
                            {
4562
                                radApp.ActiveSelectSet.Add(dependency);
4563
                                Ingr.RAD2D.Transform transform = dependency.GetTransform();
4564
                                transform.DefineByMove2d(-offsetX, -offsetY);
4565
                                radApp.ActiveSelectSet.Transform(transform, true);
4566
                                radApp.ActiveSelectSet.RemoveAll();
4567
                            }
4568
                        }
4569

    
4570
                        void MoveLineNumber(LineNumber moveLineNumber, double x, double y)
4571
                        {
4572
                            moveLineNumber.SPPID.SPPID_X = moveLineNumber.SPPID.SPPID_X - x;
4573
                            moveLineNumber.SPPID.SPPID_Y = moveLineNumber.SPPID.SPPID_Y - y;
4574
                        }
4575
                    }
4576

    
4577

    
4578
                    ReleaseCOMObjects(connector);
4579
                    connector = null;
4580
                }
4581

    
4582
                ReleaseCOMObjects(removeLabel);
4583
                removeLabel = null;
4584
            }
4585
        }
4586
        /// <summary>
4587
        /// Flow Mark Modeling
4588
        /// </summary>
4589
        /// <param name="line"></param>
4590
        private void FlowMarkModeling(Line line)
4591
        {
4592
            if (line.FLOWMARK && !string.IsNullOrEmpty(line.SPPID.ModelItemId) && !string.IsNullOrEmpty(_ETCSetting.FlowMarkSymbolPath))
4593
            {
4594
                LMConnector connector = GetLMConnectorOnlyOne(line.SPPID.ModelItemId);
4595
                if (connector != null)
4596
                {
4597
                    string mappingPath = _ETCSetting.FlowMarkSymbolPath;
4598
                    List<double[]> vertices = GetConnectorVertices(connector);
4599
                    vertices = vertices.FindAll(x => x[0] > 0 && x[1] > 0);
4600
                    double[] point = vertices[vertices.Count - 1];
4601
                    double[] array = new double[] { 0, point[0], point[1] };
4602
                    LMLabelPersist _LMLabelPersist = _placement.PIDPlaceLabel(mappingPath, array, null, null, LabeledItem: connector.AsLMRepresentation());
4603
                    if (_LMLabelPersist != null)
4604
                    {
4605
                        _LMLabelPersist.Commit();
4606
                        FlowMarkRepIds.Add(_LMLabelPersist.Id);
4607
                        ReleaseCOMObjects(_LMLabelPersist);
4608
                    }
4609
                }
4610
            }
4611
        }
4612

    
4613
        /// <summary>
4614
        /// Line Number 기준으로 모든 Item에 Line Number의 Attribute Input
4615
        /// </summary>
4616
        /// <param name="lineNumber"></param>
4617
        private void InputLineNumberAttribute(LineNumber lineNumber, List<string> endLine)
4618
        {
4619
            lineNumber.ATTRIBUTES.Sort(SortAttribute);
4620
            int SortAttribute(BaseModel.Attribute a, BaseModel.Attribute b)
4621
            {
4622
                if (a.ATTRIBUTE == "Tag Seq No")
4623
                    return 1;
4624
                else if (b.ATTRIBUTE == "Tag Seq No")
4625
                    return -1;
4626

    
4627
                return 0;
4628
            }
4629

    
4630
            foreach (LineRun run in lineNumber.RUNS)
4631
            {
4632
                foreach (var item in run.RUNITEMS)
4633
                {
4634
                    if (item.GetType() == typeof(Line))
4635
                    {
4636
                        Line line = item as Line;
4637
                        if (line != null && !endLine.Contains(line.SPPID.ModelItemId))
4638
                        {
4639
                            LMModelItem _LMModelItem = dataSource.GetModelItem(line.SPPID.ModelItemId);
4640
                            if (_LMModelItem != null && _LMModelItem.ItemStatus.ToString() == "Active")
4641
                            {
4642
                                foreach (var attribute in lineNumber.ATTRIBUTES)
4643
                                {
4644
                                    LineNumberMapping mapping = document.LineNumberMappings.Find(x => x.UID == attribute.UID);
4645
                                    if (mapping != null && !string.IsNullOrEmpty(attribute.VALUE) && attribute.VALUE != "None")
4646
                                    {
4647
                                        LMAAttribute _LMAAttribute = _LMModelItem.Attributes[mapping.SPPIDATTRIBUTENAME];
4648
                                        if (mapping.SPPIDATTRIBUTENAME == "OperFluidCode" && !string.IsNullOrEmpty(attribute.VALUE))
4649
                                        {
4650
                                            LMAAttribute _FluidSystemAttribute = _LMModelItem.Attributes["FluidSystem"];
4651
                                            if (_FluidSystemAttribute != null)
4652
                                            {
4653
                                                DataTable dt = SPPID_DB.GetFluidSystemInfo(attribute.VALUE);
4654
                                                if (dt.Rows.Count == 1)
4655
                                                {
4656
                                                    string fluidSystem = dt.Rows[0]["CODELIST_TEXT"].ToString();
4657
                                                    if (DBNull.Value.Equals(_FluidSystemAttribute.Value))
4658
                                                        _FluidSystemAttribute.Value = fluidSystem;
4659
                                                    else if (_FluidSystemAttribute.Value.ToString() != fluidSystem)
4660
                                                        _FluidSystemAttribute.Value = fluidSystem;
4661

    
4662
                                                    if (_LMAAttribute != null)
4663
                                                    {
4664
                                                        if (DBNull.Value.Equals(_LMAAttribute.Value))
4665
                                                            _LMAAttribute.Value = attribute.VALUE;
4666
                                                        else if (_LMAAttribute.Value.ToString() != attribute.VALUE)
4667
                                                            _LMAAttribute.Value = attribute.VALUE;
4668
                                                    }
4669
                                                }
4670
                                                if (dt != null)
4671
                                                    dt.Dispose();
4672
                                            }
4673
                                        }
4674
                                        else if (mapping.SPPIDATTRIBUTENAME.Equals("NominalDiameter") && !string.IsNullOrEmpty(attribute.VALUE) && _LMAAttribute != null)
4675
                                        {
4676
                                            DataRow[] rows = nominalDiameterTable.Select(string.Format("MetricStr = '{0}' OR InchStr = '{0}'", attribute.VALUE));
4677

    
4678
                                            if (rows.Length.Equals(1))
4679
                                            {
4680
                                                if (_ETCSetting.UnitSetting != null && _ETCSetting.UnitSetting.Equals("Metric"))
4681
                                                    attribute.VALUE = rows[0]["MetricStr"].ToString();
4682
                                                else
4683
                                                    attribute.VALUE = rows[0]["InchStr"].ToString();
4684

    
4685
                                                if (DBNull.Value.Equals(_LMAAttribute.Value))
4686
                                                    _LMAAttribute.Value = attribute.VALUE;
4687
                                                else if (_LMAAttribute.Value.ToString() != attribute.VALUE)
4688
                                                    _LMAAttribute.Value = attribute.VALUE;
4689
                                            }
4690
                                        }
4691
                                        else if (_LMAAttribute != null)
4692
                                        {
4693
                                            if (DBNull.Value.Equals(_LMAAttribute.Value))
4694
                                                _LMAAttribute.Value = attribute.VALUE;
4695
                                            else if (_LMAAttribute.Value.ToString() != attribute.VALUE)
4696
                                                _LMAAttribute.Value = attribute.VALUE;
4697
                                        }
4698
                                    }
4699
                                }
4700
                                _LMModelItem.Commit();
4701
                            }
4702
                            if (_LMModelItem != null)
4703
                                ReleaseCOMObjects(_LMModelItem);
4704
                            endLine.Add(line.SPPID.ModelItemId);
4705
                        }
4706
                    }
4707
                }
4708
            }
4709
        }
4710

    
4711
        /// <summary>
4712
        /// Symbol Attribute 입력 메서드
4713
        /// </summary>
4714
        /// <param name="item"></param>
4715
        private void InputSymbolAttribute(object targetItem, List<BaseModel.Attribute> targetAttributes)
4716
        {
4717
            // Object 아이템이 Symbol일 경우 Equipment일 경우 
4718
            string sRep = null;
4719
            string sModelID = null;
4720
            if (targetItem.GetType() == typeof(Symbol))
4721
                sRep = ((Symbol)targetItem).SPPID.RepresentationId;
4722
            else if (targetItem.GetType() == typeof(Equipment))
4723
                sRep = ((Equipment)targetItem).SPPID.RepresentationId;
4724
            else if (targetItem.GetType() == typeof(Line))
4725
                sModelID = ((Line)targetItem).SPPID.ModelItemId;
4726

    
4727
            if (!string.IsNullOrEmpty(sRep))
4728
            {
4729
                LMSymbol _LMSymbol = dataSource.GetSymbol(sRep);
4730
                LMModelItem _LMModelItem = _LMSymbol.ModelItemObject;
4731
                LMAAttributes _Attributes = _LMModelItem.Attributes;
4732

    
4733
                foreach (var item in targetAttributes)
4734
                {
4735
                    AttributeMapping mapping = document.AttributeMappings.Find(x => x.UID == item.UID);
4736
                    if (mapping != null && !string.IsNullOrEmpty(item.VALUE) && item.VALUE != "None")
4737
                    {
4738
                        if (!mapping.IsText)
4739
                        {
4740
                            LMAAttribute _LMAAttribute = _Attributes[mapping.SPPIDATTRIBUTENAME];
4741
                            if (mapping.SPPIDATTRIBUTENAME.Equals("NominalDiameter") && !string.IsNullOrEmpty(item.VALUE) && _LMAAttribute != null)
4742
                            {
4743
                                DataRow[] rows = nominalDiameterTable.Select(string.Format("MetricStr = '{0}' OR InchStr = '{0}'", item.VALUE));
4744

    
4745
                                if (rows.Length.Equals(1))
4746
                                {
4747
                                    if (_ETCSetting.UnitSetting != null && _ETCSetting.UnitSetting.Equals("Metric"))
4748
                                        item.VALUE = rows[0]["MetricStr"].ToString();
4749
                                    else
4750
                                        item.VALUE = rows[0]["InchStr"].ToString();
4751

    
4752
                                    if (DBNull.Value.Equals(_LMAAttribute.Value))
4753
                                        _LMAAttribute.Value = item.VALUE;
4754
                                    else if (_LMAAttribute.Value.ToString() != item.VALUE)
4755
                                        _LMAAttribute.Value = item.VALUE;
4756
                                }
4757
                            }
4758
                            else if (_LMAAttribute != null)
4759
                            {
4760
                                _LMAAttribute.Value = item.VALUE;
4761
                                // OPC 일경우 Attribute 저장
4762
                                if (targetItem.GetType() == typeof(Symbol))
4763
                                {
4764
                                    Symbol symbol = targetItem as Symbol;
4765
                                    if (symbol.TYPE == "Piping OPC's" || symbol.TYPE == "Instrument OPC's")
4766
                                        symbol.SPPID.Attributes.Add(new string[] { mapping.SPPIDATTRIBUTENAME, item.VALUE });
4767
                                }
4768
                            }
4769
                        }
4770
                        else
4771
                            DefaultTextModeling(item.VALUE, Convert.ToDouble(_LMSymbol.XCoordinate), Convert.ToDouble(_LMSymbol.YCoordinate));
4772
                    }
4773
                }
4774
                _LMModelItem.Commit();
4775

    
4776
                ReleaseCOMObjects(_Attributes);
4777
                ReleaseCOMObjects(_LMModelItem);
4778
                ReleaseCOMObjects(_LMSymbol);
4779
            }
4780
            else if (!string.IsNullOrEmpty(sModelID))
4781
            {
4782
                LMModelItem _LMModelItem = dataSource.GetModelItem(sModelID);
4783
                LMAAttributes _Attributes = _LMModelItem.Attributes;
4784

    
4785
                foreach (var item in targetAttributes)
4786
                {
4787
                    AttributeMapping mapping = document.AttributeMappings.Find(x => x.UID == item.UID);
4788
                    if (mapping == null)
4789
                        continue;
4790
                    
4791
                    LMAAttribute _LMAAttribute = _LMModelItem.Attributes[mapping.SPPIDATTRIBUTENAME];
4792
                    if (mapping.SPPIDATTRIBUTENAME == "OperFluidCode" && !string.IsNullOrEmpty(item.VALUE))
4793
                    {
4794
                        LMAAttribute _FluidSystemAttribute = _LMModelItem.Attributes["FluidSystem"];
4795
                        if (_FluidSystemAttribute != null)
4796
                        {
4797
                            DataTable dt = SPPID_DB.GetFluidSystemInfo(item.VALUE);
4798
                            if (dt.Rows.Count == 1)
4799
                            {
4800
                                string fluidSystem = dt.Rows[0]["CODELIST_TEXT"].ToString();
4801
                                if (DBNull.Value.Equals(_FluidSystemAttribute.Value))
4802
                                    _FluidSystemAttribute.Value = fluidSystem;
4803
                                else if (_FluidSystemAttribute.Value.ToString() != fluidSystem)
4804
                                    _FluidSystemAttribute.Value = fluidSystem;
4805

    
4806
                                if (_LMAAttribute != null)
4807
                                {
4808
                                    if (DBNull.Value.Equals(_LMAAttribute.Value))
4809
                                        _LMAAttribute.Value = item.VALUE;
4810
                                    else if (_LMAAttribute.Value.ToString() != item.VALUE)
4811
                                        _LMAAttribute.Value = item.VALUE;
4812
                                }
4813
                            }
4814
                            if (dt != null)
4815
                                dt.Dispose();
4816
                        }
4817
                    }
4818
                    else if (mapping.SPPIDATTRIBUTENAME.Equals("NominalDiameter") && !string.IsNullOrEmpty(item.VALUE) && _LMAAttribute != null)
4819
                    {
4820
                        DataRow[] rows = nominalDiameterTable.Select(string.Format("MetricStr = '{0}' OR InchStr = '{0}'", item.VALUE));
4821

    
4822
                        if (rows.Length.Equals(1))
4823
                        {
4824
                            if (_ETCSetting.UnitSetting != null && _ETCSetting.UnitSetting.Equals("Metric"))
4825
                                item.VALUE = rows[0]["MetricStr"].ToString();
4826
                            else
4827
                                item.VALUE = rows[0]["InchStr"].ToString();
4828

    
4829
                            if (DBNull.Value.Equals(_LMAAttribute.Value))
4830
                                _LMAAttribute.Value = item.VALUE;
4831
                            else if (_LMAAttribute.Value.ToString() != item.VALUE)
4832
                                _LMAAttribute.Value = item.VALUE;
4833
                        }
4834
                    }
4835
                    else if (mapping != null && !string.IsNullOrEmpty(item.VALUE) && item.VALUE != "None")
4836
                    {
4837
                        if (!mapping.IsText)
4838
                        {
4839
                            LMAAttribute _Attribute = _Attributes[mapping.SPPIDATTRIBUTENAME];
4840
                            if (_Attribute != null)
4841
                                _Attribute.Value = item.VALUE;
4842
                        }
4843
                    }
4844
                }
4845
                _LMModelItem.Commit();
4846

    
4847
                ReleaseCOMObjects(_Attributes);
4848
                ReleaseCOMObjects(_LMModelItem);
4849
            }
4850
        }
4851

    
4852
        /// <summary>
4853
        /// Input SpecBreak Attribute
4854
        /// </summary>
4855
        /// <param name="specBreak"></param>
4856
        private void InputSpecBreakAttribute(SpecBreak specBreak)
4857
        {
4858
            object upStreamObj = SPPIDUtil.FindObjectByUID(document, specBreak.UpStreamUID);
4859
            object downStreamObj = SPPIDUtil.FindObjectByUID(document, specBreak.DownStreamUID);
4860

    
4861
            if (upStreamObj != null &&
4862
                downStreamObj != null)
4863
            {
4864
                LMConnector targetLMConnector = FindBreakLineTarget(upStreamObj, downStreamObj);
4865

    
4866
                if (targetLMConnector != null)
4867
                {
4868
                    foreach (LMLabelPersist _LMLabelPersist in targetLMConnector.LabelPersists)
4869
                    {
4870
                        string symbolPath = _LMLabelPersist.FileName.ToString();
4871
                        AttributeMapping mapping = document.AttributeMappings.Find(x => x.SPPIDSYMBOLNAME == symbolPath);
4872
                        if (mapping != null)
4873
                        {
4874
                            BaseModel.Attribute attribute = specBreak.ATTRIBUTES.Find(y => y.UID == mapping.UID);
4875
                            if (attribute != null && !string.IsNullOrEmpty(attribute.VALUE) && attribute.VALUE != "None")
4876
                            {
4877
                                string[] values = attribute.VALUE.Split(new char[] { ',' });
4878
                                if (values.Length == 2)
4879
                                {
4880
                                    string upStreamValue = values[0];
4881
                                    string downStreamValue = values[1];
4882

    
4883
                                    InputAttributeForSpecBreak(upStreamObj, downStreamObj, upStreamValue, downStreamValue, mapping.SPPIDATTRIBUTENAME);
4884
                                }
4885
                            }
4886
                        }
4887
                    }
4888

    
4889
                    ReleaseCOMObjects(targetLMConnector);
4890
                }
4891
            }
4892

    
4893

    
4894
            #region 내부에서만 쓰는 메서드
4895
            void InputAttributeForSpecBreak(object _upStreamObj, object _downStreamObj, string upStreamValue, string downStreamValue, string sppidAttributeName)
4896
            {
4897
                Symbol upStreamSymbol = _upStreamObj as Symbol;
4898
                Line upStreamLine = _upStreamObj as Line;
4899
                Symbol downStreamSymbol = _downStreamObj as Symbol;
4900
                Line downStreamLine = _downStreamObj as Line;
4901
                // 둘다 Line일 경우
4902
                if (upStreamLine != null && downStreamLine != null)
4903
                {
4904
                    InputLineAttributeForSpecBreakLine(upStreamLine, sppidAttributeName, upStreamValue);
4905
                    InputLineAttributeForSpecBreakLine(downStreamLine, sppidAttributeName, downStreamValue);
4906
                }
4907
                // 둘다 Symbol일 경우
4908
                else if (upStreamSymbol != null && downStreamSymbol != null)
4909
                {
4910
                    LMConnector zeroLenthConnector = FindBreakLineTarget(upStreamSymbol, downStreamSymbol);
4911
                    LMSymbol upStreamLMSymbol = dataSource.GetSymbol(upStreamSymbol.SPPID.RepresentationId);
4912
                    LMSymbol downStreamLMSymbol = dataSource.GetSymbol(downStreamSymbol.SPPID.RepresentationId);
4913

    
4914
                    foreach (LMConnector connector in upStreamLMSymbol.Avoid1Connectors)
4915
                    {
4916
                        if (connector.ItemStatus.ToString() != "Active")
4917
                            continue;
4918

    
4919
                        if (connector.Id != zeroLenthConnector.Id)
4920
                            InputLineAttributeForSpecBreakLMConnector(connector, sppidAttributeName, upStreamValue);
4921
                    }
4922

    
4923
                    foreach (LMConnector connector in upStreamLMSymbol.Avoid2Connectors)
4924
                    {
4925
                        if (connector.ItemStatus.ToString() != "Active")
4926
                            continue;
4927

    
4928
                        if (connector.Id != zeroLenthConnector.Id)
4929
                            InputLineAttributeForSpecBreakLMConnector(connector, sppidAttributeName, upStreamValue);
4930
                    }
4931

    
4932
                    foreach (LMConnector connector in downStreamLMSymbol.Avoid1Connectors)
4933
                    {
4934
                        if (connector.ItemStatus.ToString() != "Active")
4935
                            continue;
4936

    
4937
                        if (connector.Id != zeroLenthConnector.Id)
4938
                            InputLineAttributeForSpecBreakLMConnector(connector, sppidAttributeName, downStreamValue);
4939
                    }
4940

    
4941
                    foreach (LMConnector connector in downStreamLMSymbol.Avoid2Connectors)
4942
                    {
4943
                        if (connector.ItemStatus.ToString() != "Active")
4944
                            continue;
4945

    
4946
                        if (connector.Id != zeroLenthConnector.Id)
4947
                            InputLineAttributeForSpecBreakLMConnector(connector, sppidAttributeName, downStreamValue);
4948
                    }
4949

    
4950
                    ReleaseCOMObjects(zeroLenthConnector);
4951
                    ReleaseCOMObjects(upStreamLMSymbol);
4952
                    ReleaseCOMObjects(downStreamLMSymbol);
4953
                }
4954
                else if (upStreamSymbol != null && downStreamLine != null)
4955
                {
4956
                    LMConnector zeroLenthConnector = FindBreakLineTarget(upStreamSymbol, downStreamLine);
4957
                    InputLineAttributeForSpecBreakLine(downStreamLine, sppidAttributeName, downStreamValue);
4958
                    LMSymbol upStreamLMSymbol = dataSource.GetSymbol(upStreamSymbol.SPPID.RepresentationId);
4959

    
4960
                    foreach (LMConnector connector in upStreamLMSymbol.Avoid1Connectors)
4961
                    {
4962
                        if (connector.ItemStatus.ToString() != "Active")
4963
                            continue;
4964

    
4965
                        if (connector.Id == zeroLenthConnector.Id)
4966
                            InputLineAttributeForSpecBreakLMConnector(connector, sppidAttributeName, upStreamValue);
4967
                    }
4968

    
4969
                    foreach (LMConnector connector in upStreamLMSymbol.Avoid2Connectors)
4970
                    {
4971
                        if (connector.ItemStatus.ToString() != "Active")
4972
                            continue;
4973

    
4974
                        if (connector.Id == zeroLenthConnector.Id)
4975
                            InputLineAttributeForSpecBreakLMConnector(connector, sppidAttributeName, upStreamValue);
4976
                    }
4977

    
4978
                    ReleaseCOMObjects(zeroLenthConnector);
4979
                    ReleaseCOMObjects(upStreamLMSymbol);
4980
                }
4981
                else if (upStreamLine != null && downStreamSymbol != null)
4982
                {
4983
                    LMConnector zeroLenthConnector = FindBreakLineTarget(upStreamLine, downStreamSymbol);
4984
                    InputLineAttributeForSpecBreakLine(upStreamLine, sppidAttributeName, upStreamValue);
4985
                    LMSymbol downStreamLMSymbol = dataSource.GetSymbol(downStreamSymbol.SPPID.RepresentationId);
4986

    
4987
                    foreach (LMConnector connector in downStreamLMSymbol.Avoid1Connectors)
4988
                    {
4989
                        if (connector.ItemStatus.ToString() != "Active")
4990
                            continue;
4991

    
4992
                        if (connector.Id == zeroLenthConnector.Id)
4993
                            InputLineAttributeForSpecBreakLMConnector(connector, sppidAttributeName, downStreamValue);
4994
                    }
4995

    
4996
                    foreach (LMConnector connector in downStreamLMSymbol.Avoid2Connectors)
4997
                    {
4998
                        if (connector.ItemStatus.ToString() != "Active")
4999
                            continue;
5000

    
5001
                        if (connector.Id == zeroLenthConnector.Id)
5002
                            InputLineAttributeForSpecBreakLMConnector(connector, sppidAttributeName, downStreamValue);
5003
                    }
5004

    
5005
                    ReleaseCOMObjects(zeroLenthConnector);
5006
                    ReleaseCOMObjects(downStreamLMSymbol);
5007
                }
5008
            }
5009

    
5010
            void InputLineAttributeForSpecBreakLine(Line line, string attrName, string value)
5011
            {
5012
                LMModelItem _LMModelItem = dataSource.GetModelItem(line.SPPID.ModelItemId);
5013
                if (_LMModelItem != null && _LMModelItem.ItemStatus.ToString() == "Active")
5014
                {
5015
                    LMAAttribute _LMAAttribute = _LMModelItem.Attributes[attrName];
5016
                    if (_LMAAttribute != null)
5017
                    {
5018
                        if (DBNull.Value.Equals(_LMAAttribute.Value))
5019
                            _LMAAttribute.Value = value;
5020
                        else if (_LMAAttribute.Value.ToString() != value)
5021
                            _LMAAttribute.Value = value;
5022
                    }
5023

    
5024
                    _LMModelItem.Commit();
5025
                }
5026
                if (_LMModelItem != null)
5027
                    ReleaseCOMObjects(_LMModelItem);
5028
            }
5029

    
5030
            void InputLineAttributeForSpecBreakLMConnector(LMConnector connector, string attrName, string value)
5031
            {
5032
                LMModelItem _LMModelItem = dataSource.GetModelItem(connector.ModelItemID);
5033
                if (_LMModelItem != null && _LMModelItem.ItemStatus.ToString() == "Active")
5034
                {
5035
                    LMAAttribute _LMAAttribute = _LMModelItem.Attributes[attrName];
5036
                    if (_LMAAttribute != null)
5037
                    {
5038
                        if (DBNull.Value.Equals(_LMAAttribute.Value))
5039
                            _LMAAttribute.Value = value;
5040
                        else if (_LMAAttribute.Value.ToString() != value)
5041
                            _LMAAttribute.Value = value;
5042
                    }
5043

    
5044
                    _LMModelItem.Commit();
5045
                }
5046
                if (_LMModelItem != null)
5047
                    ReleaseCOMObjects(_LMModelItem);
5048
            }
5049
            #endregion
5050
        }
5051

    
5052
        private void InputEndBreakAttribute(EndBreak endBreak)
5053
        {
5054
            object ownerObj = SPPIDUtil.FindObjectByUID(document, endBreak.OWNER);
5055
            object connectedItem = SPPIDUtil.FindObjectByUID(document, endBreak.PROPERTIES.Find(x => x.ATTRIBUTE == "Connected Item").VALUE);
5056

    
5057
            if ((ownerObj.GetType() == typeof(Symbol) && connectedItem.GetType() == typeof(Line)) ||
5058
                (ownerObj.GetType() == typeof(Line) && connectedItem.GetType() == typeof(Symbol)))
5059
            {
5060
                LMLabelPersist labelPersist = dataSource.GetLabelPersist(endBreak.SPPID.RepresentationId);
5061
                if (labelPersist != null)
5062
                {
5063
                    LMRepresentation representation = labelPersist.RepresentationObject;
5064
                    if (representation != null)
5065
                    {
5066
                        LMConnector connector = dataSource.GetConnector(representation.Id);
5067
                        LMModelItem ZeroLengthModelItem = connector.ModelItemObject;
5068
                        string modelItemID = connector.ModelItemID.ToString();
5069
                        if (Convert.ToBoolean(connector.IsZeroLength))
5070
                        {
5071
                            List<string> modelItemIDs = new List<string>();
5072
                            if (connector.ConnectItem1SymbolObject != null && connector.ConnectItem1SymbolObject.RepresentationType.ToString() != "Branch")
5073
                            {
5074
                                LMSymbol symbol = connector.ConnectItem1SymbolObject;
5075
                                foreach (LMConnector item in symbol.Connect1Connectors)
5076
                                {
5077
                                    if (item.ItemStatus.ToString() == "Active" && item.ModelItemID.ToString() != modelItemID)
5078
                                        modelItemIDs.Add(item.ModelItemID.ToString());
5079
                                    ReleaseCOMObjects(item);
5080
                                }
5081
                                foreach (LMConnector item in symbol.Connect2Connectors)
5082
                                {
5083
                                    if (item.ItemStatus.ToString() == "Active" && item.ModelItemID.ToString() != modelItemID)
5084
                                        modelItemIDs.Add(item.ModelItemID.ToString());
5085
                                    ReleaseCOMObjects(item);
5086
                                }
5087
                                ReleaseCOMObjects(symbol);
5088
                                symbol = null;
5089
                            }
5090
                            else if (connector.ConnectItem2SymbolObject != null && connector.ConnectItem2SymbolObject.RepresentationType.ToString() != "Branch")
5091
                            {
5092
                                LMSymbol symbol = connector.ConnectItem2SymbolObject;
5093
                                foreach (LMConnector item in symbol.Connect1Connectors)
5094
                                {
5095
                                    if (item.ItemStatus.ToString() == "Active" && item.ModelItemID.ToString() != modelItemID)
5096
                                        modelItemIDs.Add(item.ModelItemID.ToString());
5097
                                    ReleaseCOMObjects(item);
5098
                                }
5099
                                foreach (LMConnector item in symbol.Connect2Connectors)
5100
                                {
5101
                                    if (item.ItemStatus.ToString() == "Active" && item.ModelItemID.ToString() != modelItemID)
5102
                                        modelItemIDs.Add(item.ModelItemID.ToString());
5103
                                    ReleaseCOMObjects(item);
5104
                                }
5105
                                ReleaseCOMObjects(symbol);
5106
                                symbol = null;
5107
                            }
5108

    
5109
                            modelItemIDs = modelItemIDs.Distinct().ToList();
5110
                            if (modelItemIDs.Count == 1)
5111
                            {
5112
                                LMModelItem modelItem = dataSource.GetModelItem(modelItemIDs[0]);
5113
                                LMConnector onlyOne = GetLMConnectorOnlyOne(modelItem.Id);
5114
                                if (onlyOne != null && Convert.ToBoolean(onlyOne.IsZeroLength))
5115
                                {
5116
                                    bool result = false;
5117
                                    foreach (LMLabelPersist loop in onlyOne.LabelPersists)
5118
                                    {
5119
                                        if (document.EndBreaks.Find(x => x.SPPID.RepresentationId == loop.RepresentationID.ToString()) != null)
5120
                                            result = true;
5121
                                        ReleaseCOMObjects(loop);
5122
                                    }
5123

    
5124
                                    if (result)
5125
                                    {
5126
                                        object value = modelItem.Attributes["TagSequenceNo"].Value;
5127
                                        ZeroLengthModelItem.Attributes["TagSequenceNo"].Value = value;
5128
                                        ZeroLengthModelItem.Commit();
5129
                                    }
5130
                                    else
5131
                                    {
5132
                                        List<string> loopModelItems = new List<string>();
5133
                                        if (onlyOne.ConnectItem1SymbolObject.RepresentationType.ToString() == "Branch")
5134
                                        {
5135
                                            LMSymbol _symbol = onlyOne.ConnectItem1SymbolObject;
5136
                                            foreach (LMConnector loop in _symbol.Connect1Connectors)
5137
                                            {
5138
                                                if (loop.ItemStatus.ToString() == "Active" && loop.ModelItemID != onlyOne.ModelItemID)
5139
                                                    loopModelItems.Add(loop.ModelItemID.ToString());
5140
                                                ReleaseCOMObjects(loop);
5141
                                            }
5142
                                            foreach (LMConnector loop in _symbol.Connect2Connectors)
5143
                                            {
5144
                                                if (loop.ItemStatus.ToString() == "Active" && loop.ModelItemID != onlyOne.ModelItemID)
5145
                                                    loopModelItems.Add(loop.ModelItemID.ToString());
5146
                                                ReleaseCOMObjects(loop);
5147
                                            }
5148
                                            ReleaseCOMObjects(_symbol);
5149
                                            _symbol = null;
5150
                                        }
5151
                                        else if (onlyOne.ConnectItem2SymbolObject.RepresentationType.ToString() == "Branch")
5152
                                        {
5153
                                            LMSymbol _symbol = onlyOne.ConnectItem2SymbolObject;
5154
                                            foreach (LMConnector loop in _symbol.Connect1Connectors)
5155
                                            {
5156
                                                if (loop.ItemStatus.ToString() == "Active" && loop.ModelItemID != onlyOne.ModelItemID)
5157
                                                    loopModelItems.Add(loop.ModelItemID.ToString());
5158
                                                ReleaseCOMObjects(loop);
5159
                                            }
5160
                                            foreach (LMConnector loop in _symbol.Connect2Connectors)
5161
                                            {
5162
                                                if (loop.ItemStatus.ToString() == "Active" && loop.ModelItemID != onlyOne.ModelItemID)
5163
                                                    loopModelItems.Add(loop.ModelItemID.ToString());
5164
                                                ReleaseCOMObjects(loop);
5165
                                            }
5166
                                            ReleaseCOMObjects(_symbol);
5167
                                            _symbol = null;
5168
                                        }
5169

    
5170
                                        loopModelItems = loopModelItems.Distinct().ToList();
5171
                                        if (loopModelItems.Count == 1)
5172
                                        {
5173
                                            LMModelItem loopModelItem = dataSource.GetModelItem(loopModelItems[0]);
5174
                                            object value = loopModelItem.Attributes["TagSequenceNo"].Value;
5175
                                            modelItem.Attributes["TagSequenceNo"].Value = value;
5176
                                            modelItem.Commit();
5177
                                            ZeroLengthModelItem.Attributes["TagSequenceNo"].Value = value;
5178
                                            ZeroLengthModelItem.Commit();
5179

    
5180
                                            ReleaseCOMObjects(loopModelItem);
5181
                                            loopModelItem = null;
5182
                                        }
5183
                                    }
5184
                                }
5185
                                else
5186
                                {
5187
                                    object value = modelItem.Attributes["TagSequenceNo"].Value;
5188
                                    ZeroLengthModelItem.Attributes["TagSequenceNo"].Value = value;
5189
                                    ZeroLengthModelItem.Commit();
5190
                                }
5191
                                ReleaseCOMObjects(modelItem);
5192
                                modelItem = null;
5193
                                ReleaseCOMObjects(onlyOne);
5194
                                onlyOne = null;
5195
                            }
5196
                        }
5197
                        ReleaseCOMObjects(connector);
5198
                        connector = null;
5199
                        ReleaseCOMObjects(ZeroLengthModelItem);
5200
                        ZeroLengthModelItem = null;
5201
                    }
5202
                    ReleaseCOMObjects(representation);
5203
                    representation = null;
5204
                }
5205
                ReleaseCOMObjects(labelPersist);
5206
                labelPersist = null;
5207
            }
5208
        }
5209

    
5210
        /// <summary>
5211
        /// Text Modeling - Association일 경우는 Text대신 해당 맵핑된 Symbol로 모델링
5212
        /// </summary>
5213
        /// <param name="text"></param>
5214
        private void NormalTextModeling(Text text)
5215
        {
5216
            LMSymbol _LMSymbol = null;
5217

    
5218
            LMItemNote _LMItemNote = null;
5219
            LMAAttribute _LMAAttribute = null;
5220

    
5221
            double x = 0;
5222
            double y = 0;
5223
            double angle = text.ANGLE;
5224
            CalcLabelLocation(ref x, ref y, text.SPPID.ORIGINAL_X, text.SPPID.ORIGINAL_Y, text.SPPIDLabelLocation, _ETCSetting.TextLocation);
5225

    
5226
            SPPIDUtil.ConvertGridPoint(ref x, ref y);
5227
            text.SPPID.SPPID_X = x;
5228
            text.SPPID.SPPID_Y = y;
5229

    
5230
            _LMSymbol = _placement.PIDPlaceSymbol(text.SPPID.MAPPINGNAME, x, y, null, Rotation: angle);
5231
            if (_LMSymbol != null)
5232
            {
5233
                _LMSymbol.Commit();
5234
                _LMItemNote = _placement.PIDDataSource.GetItemNote(_LMSymbol.ModelItemID);
5235
                if (_LMItemNote != null)
5236
                {
5237
                    _LMItemNote.Commit();
5238
                    _LMAAttribute = _LMItemNote.Attributes["Note.Body"];
5239
                    if (_LMAAttribute != null)
5240
                    {
5241
                        _LMAAttribute.Value = text.VALUE;
5242
                        text.SPPID.RepresentationId = _LMSymbol.AsLMRepresentation().Id;
5243
                        _LMItemNote.Commit();
5244

    
5245

    
5246
                        double[] range = null;
5247
                        foreach (LMLabelPersist labelPersist in _LMSymbol.LabelPersists)
5248
                        {
5249
                            double[] temp = null;
5250
                            GetSPPIDSymbolRange(labelPersist, ref temp);
5251
                            if (temp != null)
5252
                            {
5253
                                if (range == null)
5254
                                    range = temp;
5255
                                else
5256
                                {
5257
                                    range = new double[] {
5258
                                            Math.Min(range[0], temp[0]),
5259
                                            Math.Min(range[1], temp[1]),
5260
                                            Math.Max(range[2], temp[2]),
5261
                                            Math.Max(range[3], temp[3])
5262
                                        };
5263
                                }
5264
                            }
5265
                        }
5266
                        text.SPPID.Range = range;
5267

    
5268
                        if (_LMAAttribute != null)
5269
                            ReleaseCOMObjects(_LMAAttribute);
5270
                        if (_LMItemNote != null)
5271
                            ReleaseCOMObjects(_LMItemNote);
5272
                    }
5273

    
5274
                    TextCorrectModeling(text);
5275
                }
5276
            }
5277
            if (_LMSymbol != null)
5278
                ReleaseCOMObjects(_LMSymbol);
5279
        }
5280

    
5281
        private void DefaultTextModeling(string value, double x, double y)
5282
        {
5283
            LMSymbol _LMSymbol = null;
5284

    
5285
            LMItemNote _LMItemNote = null;
5286
            LMAAttribute _LMAAttribute = null;
5287

    
5288
            _LMSymbol = _placement.PIDPlaceSymbol(_ETCSetting.TextSymbolPath, x, y, null, Rotation: 0);
5289
            if (_LMSymbol != null)
5290
            {
5291
                _LMSymbol.Commit();
5292
                _LMItemNote = _placement.PIDDataSource.GetItemNote(_LMSymbol.ModelItemID);
5293
                if (_LMItemNote != null)
5294
                {
5295
                    _LMItemNote.Commit();
5296
                    _LMAAttribute = _LMItemNote.Attributes["Note.Body"];
5297
                    if (_LMAAttribute != null)
5298
                    {
5299
                        _LMAAttribute.Value = value;
5300
                        _LMItemNote.Commit();
5301

    
5302
                        if (_LMAAttribute != null)
5303
                            ReleaseCOMObjects(_LMAAttribute);
5304
                        if (_LMItemNote != null)
5305
                            ReleaseCOMObjects(_LMItemNote);
5306
                    }
5307
                }
5308
            }
5309
            if (_LMSymbol != null)
5310
                ReleaseCOMObjects(_LMSymbol);
5311
        }
5312

    
5313
        private void AssociationTextModeling(Text text)
5314
        {
5315
            LMSymbol _LMSymbol = null;
5316
            LMConnector connectedLMConnector = null;
5317
            object owner = SPPIDUtil.FindObjectByUID(document, text.OWNER);
5318
            if (owner != null && (owner.GetType() == typeof(Symbol) || owner.GetType() == typeof(Equipment)))
5319
            {
5320
                Symbol symbol = owner as Symbol;
5321
                _LMSymbol = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
5322
                if (_LMSymbol != null)
5323
                {
5324
                    foreach (BaseModel.Attribute attribute in symbol.ATTRIBUTES.FindAll(x => x.ASSOCITEM == text.UID))
5325
                    {
5326
                        if (attribute != null && !string.IsNullOrEmpty(attribute.VALUE) && attribute.VALUE != "None")
5327
                        {
5328
                            AttributeMapping mapping = document.AttributeMappings.Find(x => x.UID == attribute.UID && !string.IsNullOrEmpty(x.SPPIDSYMBOLNAME));
5329

    
5330
                            if (mapping != null)
5331
                            {
5332
                                double x = 0;
5333
                                double y = 0;
5334

    
5335
                                CalcLabelLocation(ref x, ref y, text.SPPID.ORIGINAL_X, text.SPPID.ORIGINAL_Y, text.SPPIDLabelLocation, mapping.Location);
5336
                                SPPIDUtil.ConvertGridPoint(ref x, ref y);
5337
                                double[] array = new double[] { 0, x, y };
5338
                                text.SPPID.SPPID_X = x;
5339
                                text.SPPID.SPPID_Y = y;
5340
                                LMLabelPersist _LMLabelPersist = _placement.PIDPlaceLabel(mapping.SPPIDSYMBOLNAME, array, null, Rotation: text.ANGLE, LabeledItem: _LMSymbol.AsLMRepresentation(), IsLeaderVisible: mapping.LeaderLine);
5341
                                if (_LMLabelPersist != null)
5342
                                {
5343
                                    text.SPPID.RepresentationId = _LMLabelPersist.AsLMRepresentation().Id;
5344
                                    _LMLabelPersist.Commit();
5345
                                    ReleaseCOMObjects(_LMLabelPersist);
5346
                                }
5347
                            }
5348
                        }
5349
                    }
5350
                }
5351
            }
5352
            else if (owner != null && owner.GetType() == typeof(Line))
5353
            {
5354
                Line line = owner as Line;
5355
                Dictionary<LMConnector, List<double[]>> connectorVertices = GetPipeRunVertices(line.SPPID.ModelItemId);
5356
                connectedLMConnector = FindTargetLMConnectorForLabel(connectorVertices, text.SPPID.ORIGINAL_X, text.SPPID.ORIGINAL_Y);
5357

    
5358
                if (connectedLMConnector != null)
5359
                {
5360
                    foreach (BaseModel.Attribute attribute in line.ATTRIBUTES.FindAll(x => x.ASSOCITEM == text.UID))
5361
                    {
5362
                        if (attribute != null && !string.IsNullOrEmpty(attribute.VALUE) && attribute.VALUE != "None")
5363
                        {
5364
                            AttributeMapping mapping = document.AttributeMappings.Find(x => x.UID == attribute.UID && !string.IsNullOrEmpty(x.SPPIDSYMBOLNAME));
5365

    
5366
                            if (mapping != null)
5367
                            {
5368
                                double x = 0;
5369
                                double y = 0;
5370
                                CalcLabelLocation(ref x, ref y, text.SPPID.ORIGINAL_X, text.SPPID.ORIGINAL_Y, text.SPPIDLabelLocation, mapping.Location);
5371
                                SPPIDUtil.ConvertGridPoint(ref x, ref y);
5372
                                double[] array = new double[] { 0, x, y };
5373

    
5374
                                LMLabelPersist _LMLabelPersist = _placement.PIDPlaceLabel(mapping.SPPIDSYMBOLNAME, array, null, Rotation: text.ANGLE, LabeledItem: connectedLMConnector.AsLMRepresentation(), IsLeaderVisible: mapping.LeaderLine);
5375
                                if (_LMLabelPersist != null)
5376
                                {
5377
                                    text.SPPID.RepresentationId = _LMLabelPersist.AsLMRepresentation().Id;
5378
                                    _LMLabelPersist.Commit();
5379

    
5380
                                    Ingr.RAD2D.Components.SmartLabel.SmartLabel dependency = radApp.ActiveDocument.ActiveSheet.DrawingObjects[_LMLabelPersist.GraphicOID.ToString()] as Ingr.RAD2D.Components.SmartLabel.SmartLabel;
5381
                                    if (dependency != null)
5382
                                    {
5383
                                        radApp.ActiveSelectSet.RemoveAll();
5384
                                        radApp.ActiveSelectSet.Add(dependency);
5385
                                        Ingr.RAD2D.Transform transform = dependency.GetTransform();
5386
                                        transform.DefineByMove2d(x - Convert.ToDouble(_LMLabelPersist.XCoordinate), y - Convert.ToDouble(_LMLabelPersist.YCoordinate));
5387
                                        radApp.ActiveSelectSet.Transform(transform, true);
5388
                                        radApp.ActiveSelectSet.RemoveAll();
5389
                                    }
5390

    
5391
                                    ReleaseCOMObjects(_LMLabelPersist);
5392
                                }
5393
                            }
5394
                        }
5395
                    }
5396
                }
5397
            }
5398
            if (_LMSymbol != null)
5399
                ReleaseCOMObjects(_LMSymbol);
5400
        }
5401

    
5402
        private void TextCorrectModeling(Text text)
5403
        {
5404
            if (text.SPPID.Range == null)
5405
                return;
5406

    
5407
            bool needRemodeling = false;
5408
            bool loop = true;
5409
            GridSetting gridSetting = GridSetting.GetInstance();
5410
            while (loop)
5411
            {
5412
                loop = false;
5413
                foreach (var overlapText in document.TEXTINFOS)
5414
                {
5415
                    if (overlapText.ASSOCIATION || overlapText == text || overlapText.SPPID.Range == null)
5416
                        continue;
5417

    
5418
                    if (SPPIDUtil.IsOverlap(overlapText.SPPID.Range, text.SPPID.Range))
5419
                    {
5420
                        double percentX = 0;
5421
                        double percentY = 0;
5422
                        if (overlapText.X1 <= text.X2 && overlapText.X2 >= text.X1)
5423
                        {
5424
                            double gapX = Math.Min(overlapText.X2, text.X2) - Math.Max(overlapText.X1, text.X1);
5425
                            percentX = Math.Max(gapX / (overlapText.X2 - overlapText.X1), gapX / (text.X2 - text.X1));
5426
                        }
5427
                        if (overlapText.Y1 <= text.Y2 && overlapText.Y2 >= text.Y1)
5428
                        {
5429
                            double gapY = Math.Min(overlapText.Y2, text.Y2) - Math.Max(overlapText.Y1, text.Y1);
5430
                            percentY = Math.Max(gapY / (overlapText.Y2 - overlapText.Y1), gapY / (text.Y2 - text.Y1));
5431
                        }
5432

    
5433
                        double tempX = 0;
5434
                        double tempY = 0;
5435
                        bool overlapX = false;
5436
                        bool overlapY = false;
5437
                        SPPIDUtil.CalcOverlap(text.SPPID.Range, overlapText.SPPID.Range, ref tempX, ref tempY, ref overlapX, ref overlapY);
5438
                        if (percentX >= percentY)
5439
                        {
5440
                            int count = Convert.ToInt32(tempY / gridSetting.Length) + 1;
5441
                            double move = gridSetting.Length * count;
5442
                            text.SPPID.SPPID_Y = text.SPPID.SPPID_Y - move;
5443
                            text.SPPID.Range = new double[] { text.SPPID.Range[0], text.SPPID.Range[1] - move, text.SPPID.Range[2], text.SPPID.Range[3] - move };
5444
                            needRemodeling = true;
5445
                            loop = true;
5446
                        }
5447
                        else
5448
                        {
5449
                            int count = Convert.ToInt32(tempX / gridSetting.Length) + 1;
5450
                            double move = gridSetting.Length * count;
5451
                            text.SPPID.SPPID_X = text.SPPID.SPPID_X + move;
5452
                            text.SPPID.Range = new double[] { text.SPPID.Range[0] + move, text.SPPID.Range[1], text.SPPID.Range[2] + move, text.SPPID.Range[3] };
5453
                            needRemodeling = true;
5454
                            loop = true;
5455
                        }
5456
                    }
5457
                }
5458
            }
5459

    
5460

    
5461
            if (needRemodeling)
5462
            {
5463
                LMSymbol symbol = dataSource.GetSymbol(text.SPPID.RepresentationId);
5464
                _placement.PIDRemovePlacement(symbol.AsLMRepresentation());
5465
                text.SPPID.RepresentationId = null;
5466

    
5467
                LMItemNote _LMItemNote = null;
5468
                LMAAttribute _LMAAttribute = null;
5469
                LMSymbol _LMSymbol = _placement.PIDPlaceSymbol(text.SPPID.MAPPINGNAME, text.SPPID.SPPID_X, text.SPPID.SPPID_Y, null, Rotation: text.ANGLE);
5470
                if (_LMSymbol != null)
5471
                {
5472
                    _LMSymbol.Commit();
5473
                    _LMItemNote = _placement.PIDDataSource.GetItemNote(_LMSymbol.ModelItemID);
5474
                    if (_LMItemNote != null)
5475
                    {
5476
                        _LMItemNote.Commit();
5477
                        _LMAAttribute = _LMItemNote.Attributes["Note.Body"];
5478
                        if (_LMAAttribute != null)
5479
                        {
5480
                            _LMAAttribute.Value = text.VALUE;
5481
                            text.SPPID.RepresentationId = _LMSymbol.AsLMRepresentation().Id;
5482
                            _LMItemNote.Commit();
5483

    
5484
                            ReleaseCOMObjects(_LMAAttribute);
5485
                            ReleaseCOMObjects(_LMItemNote);
5486
                        }
5487
                    }
5488
                }
5489

    
5490
                ReleaseCOMObjects(symbol);
5491
                symbol = null;
5492
                ReleaseCOMObjects(_LMItemNote);
5493
                _LMItemNote = null;
5494
                ReleaseCOMObjects(_LMAAttribute);
5495
                _LMAAttribute = null;
5496
                ReleaseCOMObjects(_LMSymbol);
5497
                _LMSymbol = null;
5498
            }
5499
        }
5500

    
5501
        private void AssociationTextCorrectModeling(Text text, List<Text> endTexts)
5502
        {
5503
            if (!string.IsNullOrEmpty(text.SPPID.RepresentationId))
5504
            {
5505
                List<Text> allTexts = new List<Text>();
5506
                LMLabelPersist targetLabel = dataSource.GetLabelPersist(text.SPPID.RepresentationId);
5507
                LMRepresentation representation = targetLabel.RepresentationObject;
5508
                Symbol symbol = document.SYMBOLS.Find(x => x.SPPID.RepresentationId == representation.Id);
5509
                if (targetLabel.RepresentationObject != null && symbol != null)
5510
                {
5511
                    double[] symbolRange = null;
5512
                    GetSPPIDSymbolRange(symbol, ref symbolRange, true, true);
5513
                    if (symbolRange != null)
5514
                    {
5515
                        foreach (LMLabelPersist labelPersist in representation.LabelPersists)
5516
                        {
5517
                            Text findText = document.TEXTINFOS.Find(x => x.SPPID.RepresentationId == labelPersist.AsLMRepresentation().Id && x.ASSOCIATION);
5518
                            if (findText != null)
5519
                            {
5520
                                double[] range = null;
5521
                                GetSPPIDSymbolRange(labelPersist, ref range);
5522
                                findText.SPPID.Range = range;
5523
                                allTexts.Add(findText);
5524
                            }
5525

    
5526
                            ReleaseCOMObjects(labelPersist);
5527
                        }
5528

    
5529
                        if (allTexts.Count > 0)
5530
                        {
5531
                            #region Sort Text By Y
5532
                            allTexts.Sort(SortTextByY);
5533
                            int SortTextByY(Text a, Text b)
5534
                            {
5535
                                return b.SPPID.Range[3].CompareTo(a.SPPID.Range[3]);
5536
                            }
5537
                            #endregion
5538

    
5539
                            #region 정렬하기전 방향
5540
                            List<Text> left = new List<Text>();
5541
                            List<Text> down = new List<Text>();
5542
                            List<Text> right = new List<Text>();
5543
                            List<Text> up = new List<Text>();
5544
                            List<List<Text>> sortTexts = new List<List<Text>>() { left, down, right, up };
5545
                            foreach (var loopText in allTexts)
5546
                            {
5547
                                double textCenterX = (loopText.X1 + loopText.X2) / 2;
5548
                                double textCenterY = (loopText.Y1 + loopText.Y2) / 2;
5549
                                double originX = 0;
5550
                                double originY = 0;
5551
                                SPPIDUtil.ConvertPointBystring(symbol.ORIGINALPOINT, ref originX, ref originY);
5552
                                double angle = SPPIDUtil.CalcAngle(textCenterX, textCenterY, originX, originY);
5553

    
5554
                                if (angle < 45)
5555
                                {
5556
                                    // Text 오른쪽
5557
                                    if (textCenterX > originX)
5558
                                        right.Add(loopText);
5559
                                    // Text 왼쪽
5560
                                    else
5561
                                        left.Add(loopText);
5562
                                }
5563
                                else
5564
                                {
5565
                                    // Text 아래쪽
5566
                                    if (textCenterY > originY)
5567
                                        down.Add(loopText);
5568
                                    // Text 위쪽
5569
                                    else
5570
                                        up.Add(loopText);
5571
                                }
5572
                            }
5573

    
5574
                            #endregion
5575

    
5576
                            foreach (var texts in sortTexts)
5577
                            {
5578
                                if (texts.Count == 0)
5579
                                    continue;
5580

    
5581
                                #region 첫번째 Text로 기준 맞춤
5582
                                for (int i = 0; i < texts.Count; i++)
5583
                                {
5584
                                    if (i != 0)
5585
                                    {
5586
                                        Text currentText = texts[i];
5587
                                        Text prevText = texts[i - 1];
5588
                                        double minY = prevText.SPPID.Range[1];
5589
                                        double centerPrevX = (prevText.SPPID.Range[0] + prevText.SPPID.Range[2]) / 2;
5590
                                        double centerX = (currentText.SPPID.Range[0] + currentText.SPPID.Range[2]) / 2;
5591
                                        double _gapX = centerX - centerPrevX;
5592
                                        double _gapY = currentText.SPPID.Range[3] - minY;
5593
                                        MoveText(currentText, _gapX, _gapY);
5594
                                    }
5595
                                }
5596
                                List<double> rangeMinX = texts.Select(loopX => loopX.SPPID.Range[0]).ToList();
5597
                                List<double> rangeMinY = texts.Select(loopX => loopX.SPPID.Range[1]).ToList();
5598
                                List<double> rangeMaxX = texts.Select(loopX => loopX.SPPID.Range[2]).ToList();
5599
                                List<double> rangeMaxY = texts.Select(loopX => loopX.SPPID.Range[3]).ToList();
5600
                                rangeMinX.Sort();
5601
                                rangeMinY.Sort();
5602
                                rangeMaxX.Sort();
5603
                                rangeMaxY.Sort();
5604
                                double allTextCenterX = (rangeMinX[0] + rangeMaxX[rangeMaxX.Count - 1]) / 2;
5605
                                double allTextCenterY = (rangeMinY[0] + rangeMaxY[rangeMaxY.Count - 1]) / 2;
5606
                                #endregion
5607
                                #region 정렬
5608
                                Text correctBySymbol = texts[0];
5609
                                double textCenterX = (correctBySymbol.X1 + correctBySymbol.X2) / 2;
5610
                                double textCenterY = (correctBySymbol.Y1 + correctBySymbol.Y2) / 2;
5611
                                double originX = 0;
5612
                                double originY = 0;
5613
                                SPPIDUtil.ConvertPointBystring(symbol.ORIGINALPOINT, ref originX, ref originY);
5614
                                double angle = SPPIDUtil.CalcAngle(textCenterX, textCenterY, originX, originY);
5615
                                double symbolCenterX = (symbolRange[0] + symbolRange[2]) / 2;
5616
                                double symbolCenterY = (symbolRange[1] + symbolRange[3]) / 2;
5617

    
5618
                                double gapX = 0;
5619
                                double gapY = 0;
5620
                                if (angle < 45)
5621
                                {
5622
                                    // Text 오른쪽
5623
                                    if (textCenterX > originX)
5624
                                    {
5625
                                        gapX = rangeMinX[0] - symbolRange[2];
5626
                                        gapY = allTextCenterY - symbolCenterY;
5627
                                    }
5628
                                    // Text 왼쪽
5629
                                    else
5630
                                    {
5631
                                        gapX = rangeMaxX[rangeMaxX.Count - 1] - symbolRange[0];
5632
                                        gapY = allTextCenterY - symbolCenterY;
5633
                                    }
5634
                                }
5635
                                else
5636
                                {
5637
                                    // Text 아래쪽
5638
                                    if (textCenterY > originY)
5639
                                    {
5640
                                        gapX = allTextCenterX - symbolCenterX;
5641
                                        gapY = rangeMaxY[rangeMaxY.Count - 1] - symbolRange[1];
5642
                                    }
5643
                                    // Text 위쪽
5644
                                    else
5645
                                    {
5646
                                        gapX = allTextCenterX - symbolCenterX;
5647
                                        gapY = rangeMinY[0] - symbolRange[3];
5648
                                    }
5649
                                }
5650

    
5651
                                foreach (var item in texts)
5652
                                {
5653
                                    MoveText(item, gapX, gapY);
5654
                                    RemodelingAssociationText(item);
5655
                                }
5656
                                #endregion
5657
                            }
5658
                        }
5659
                    }
5660
                }
5661

    
5662
                void MoveText(Text moveText, double x, double y)
5663
                {
5664
                    moveText.SPPID.SPPID_X = moveText.SPPID.SPPID_X - x;
5665
                    moveText.SPPID.SPPID_Y = moveText.SPPID.SPPID_Y - y;
5666
                    moveText.SPPID.Range = new double[] {
5667
                        moveText.SPPID.Range[0] - x,
5668
                        moveText.SPPID.Range[1]- y,
5669
                        moveText.SPPID.Range[2]- x,
5670
                        moveText.SPPID.Range[3]- y
5671
                    };
5672
                }
5673

    
5674
                endTexts.AddRange(allTexts);
5675

    
5676
                ReleaseCOMObjects(targetLabel);
5677
                targetLabel = null;
5678
                ReleaseCOMObjects(representation);
5679
                representation = null;
5680
            }
5681
        }
5682

    
5683
        private void RemodelingAssociationText(Text text)
5684
        {
5685
            LMLabelPersist removeLabel = dataSource.GetLabelPersist(text.SPPID.RepresentationId);
5686
            _placement.PIDRemovePlacement(removeLabel.AsLMRepresentation());
5687
            removeLabel.Commit();
5688
            ReleaseCOMObjects(removeLabel);
5689
            removeLabel = null;
5690

    
5691
            object owner = SPPIDUtil.FindObjectByUID(document, text.OWNER);
5692
            if (owner != null && owner.GetType() == typeof(Symbol))
5693
            {
5694
                Symbol symbol = owner as Symbol;
5695
                LMSymbol _LMSymbol = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
5696
                if (_LMSymbol != null)
5697
                {
5698
                    BaseModel.Attribute attribute = symbol.ATTRIBUTES.Find(x => x.ASSOCITEM == text.UID);
5699
                    if (attribute != null && !string.IsNullOrEmpty(attribute.VALUE) && attribute.VALUE != "None")
5700
                    {
5701
                        AttributeMapping mapping = document.AttributeMappings.Find(x => x.UID == attribute.UID && !string.IsNullOrEmpty(x.SPPIDSYMBOLNAME));
5702

    
5703
                        if (mapping != null)
5704
                        {
5705
                            double x = 0;
5706
                            double y = 0;
5707

    
5708
                            double[] array = new double[] { 0, text.SPPID.SPPID_X, text.SPPID.SPPID_Y };
5709
                            LMLabelPersist _LMLabelPersist = _placement.PIDPlaceLabel(mapping.SPPIDSYMBOLNAME, array, null, Rotation: text.ANGLE, LabeledItem: _LMSymbol.AsLMRepresentation(), IsLeaderVisible: mapping.LeaderLine);
5710
                            if (_LMLabelPersist != null)
5711
                            {
5712
                                text.SPPID.RepresentationId = _LMLabelPersist.AsLMRepresentation().Id;
5713
                                _LMLabelPersist.Commit();
5714
                            }
5715
                            ReleaseCOMObjects(_LMLabelPersist);
5716
                            _LMLabelPersist = null;
5717
                        }
5718
                    }
5719
                }
5720
                ReleaseCOMObjects(_LMSymbol);
5721
                _LMSymbol = null;
5722
            }
5723
        }
5724

    
5725
        /// <summary>
5726
        /// Note Modeling
5727
        /// </summary>
5728
        /// <param name="note"></param>
5729
        private void NoteModeling(Note note, List<Note> correctList)
5730
        {
5731
            LMSymbol _LMSymbol = null;
5732
            LMItemNote _LMItemNote = null;
5733
            LMAAttribute _LMAAttribute = null;
5734

    
5735
            if (string.IsNullOrEmpty(note.OWNER) || note.OWNER == "None")
5736
            {
5737
                double x = 0;
5738
                double y = 0;
5739

    
5740
                CalcLabelLocation(ref x, ref y, note.SPPID.ORIGINAL_X, note.SPPID.ORIGINAL_Y, note.SPPIDLabelLocation, _ETCSetting.NoteLocation);
5741
                SPPIDUtil.ConvertGridPoint(ref x, ref y);
5742
                note.SPPID.SPPID_X = x;
5743
                note.SPPID.SPPID_Y = y;
5744

    
5745
                _LMSymbol = _placement.PIDPlaceSymbol(note.SPPID.MAPPINGNAME, x, y);
5746
                if (_LMSymbol != null)
5747
                {
5748
                    _LMSymbol.Commit();
5749
                    _LMItemNote = _placement.PIDDataSource.GetItemNote(_LMSymbol.ModelItemID);
5750
                    if (_LMItemNote != null)
5751
                    {
5752
                        _LMItemNote.Commit();
5753
                        _LMAAttribute = _LMItemNote.Attributes["Note.Body"];
5754
                        if (_LMAAttribute != null)
5755
                        {
5756
                            _LMAAttribute.Value = note.VALUE;
5757
                            note.SPPID.RepresentationId = _LMSymbol.AsLMRepresentation().Id;
5758

    
5759
                            double[] range = null;
5760
                            foreach (LMLabelPersist labelPersist in _LMSymbol.LabelPersists)
5761
                            {
5762
                                double[] temp = null;
5763
                                GetSPPIDSymbolRange(labelPersist, ref temp);
5764
                                if (temp != null)
5765
                                {
5766
                                    if (range == null)
5767
                                        range = temp;
5768
                                    else
5769
                                    {
5770
                                        range = new double[] {
5771
                                            Math.Min(range[0], temp[0]),
5772
                                            Math.Min(range[1], temp[1]),
5773
                                            Math.Max(range[2], temp[2]),
5774
                                            Math.Max(range[3], temp[3])
5775
                                        };
5776
                                    }
5777
                                }
5778
                            }
5779
                            if (range != null)
5780
                                correctList.Add(note);
5781
                            note.SPPID.Range = range;
5782

    
5783

    
5784
                            _LMItemNote.Commit();
5785
                        }
5786
                    }
5787
                }
5788
            }
5789

    
5790
            if (_LMAAttribute != null)
5791
                ReleaseCOMObjects(_LMAAttribute);
5792
            if (_LMItemNote != null)
5793
                ReleaseCOMObjects(_LMItemNote);
5794
            if (_LMSymbol != null)
5795
                ReleaseCOMObjects(_LMSymbol);
5796
        }
5797

    
5798
        private void NoteCorrectModeling(Note note, List<Note> endList)
5799
        {
5800
            bool needRemodeling = false;
5801
            bool loop = true;
5802
            GridSetting gridSetting = GridSetting.GetInstance();
5803
            while (loop)
5804
            {
5805
                loop = false;
5806
                foreach (var overlap in endList)
5807
                {
5808
                    if (SPPIDUtil.IsOverlap(overlap.SPPID.Range, note.SPPID.Range))
5809
                    {
5810
                        double tempX = 0;
5811
                        double tempY = 0;
5812
                        bool overlapX = false;
5813
                        bool overlapY = false;
5814
                        SPPIDUtil.CalcOverlap(note.SPPID.Range, overlap.SPPID.Range, ref tempX, ref tempY, ref overlapX, ref overlapY);
5815
                        double angle = SPPIDUtil.CalcAngle(note.LOCATION_X, note.LOCATION_Y, overlap.LOCATION_X, overlap.LOCATION_Y);
5816
                        if (overlapY && angle >= 45)
5817
                        {
5818
                            int count = Convert.ToInt32(tempY / gridSetting.Length) + 1;
5819
                            double move = gridSetting.Length * count;
5820
                            note.SPPID.SPPID_Y = note.SPPID.SPPID_Y - move;
5821
                            note.SPPID.Range = new double[] { note.SPPID.Range[0], note.SPPID.Range[1] - move, note.SPPID.Range[2], note.SPPID.Range[3] - move };
5822
                            needRemodeling = true;
5823
                            loop = true;
5824
                        }
5825
                        if (overlapX && angle <= 45)
5826
                        {
5827
                            int count = Convert.ToInt32(tempX / gridSetting.Length) + 1;
5828
                            double move = gridSetting.Length * count;
5829
                            note.SPPID.SPPID_X = note.SPPID.SPPID_X + move;
5830
                            note.SPPID.Range = new double[] { note.SPPID.Range[0] + move, note.SPPID.Range[1], note.SPPID.Range[2] + move, note.SPPID.Range[3] };
5831
                            needRemodeling = true;
5832
                            loop = true;
5833
                        }
5834
                    }
5835
                }
5836
            }
5837

    
5838

    
5839
            if (needRemodeling)
5840
            {
5841
                LMSymbol symbol = dataSource.GetSymbol(note.SPPID.RepresentationId);
5842
                _placement.PIDRemovePlacement(symbol.AsLMRepresentation());
5843
                note.SPPID.RepresentationId = null;
5844

    
5845
                LMItemNote _LMItemNote = null;
5846
                LMAAttribute _LMAAttribute = null;
5847
                LMSymbol _LMSymbol = _placement.PIDPlaceSymbol(note.SPPID.MAPPINGNAME, note.SPPID.SPPID_X, note.SPPID.SPPID_Y, null, Rotation: note.ANGLE);
5848
                if (_LMSymbol != null)
5849
                {
5850
                    _LMSymbol.Commit();
5851
                    _LMItemNote = _placement.PIDDataSource.GetItemNote(_LMSymbol.ModelItemID);
5852
                    if (_LMItemNote != null)
5853
                    {
5854
                        _LMItemNote.Commit();
5855
                        _LMAAttribute = _LMItemNote.Attributes["Note.Body"];
5856
                        if (_LMAAttribute != null)
5857
                        {
5858
                            _LMAAttribute.Value = note.VALUE;
5859
                            note.SPPID.RepresentationId = _LMSymbol.AsLMRepresentation().Id;
5860
                            _LMItemNote.Commit();
5861

    
5862
                            ReleaseCOMObjects(_LMAAttribute);
5863
                            ReleaseCOMObjects(_LMItemNote);
5864
                        }
5865
                    }
5866
                }
5867

    
5868
                ReleaseCOMObjects(symbol);
5869
                symbol = null;
5870
                ReleaseCOMObjects(_LMItemNote);
5871
                _LMItemNote = null;
5872
                ReleaseCOMObjects(_LMAAttribute);
5873
                _LMAAttribute = null;
5874
                ReleaseCOMObjects(_LMSymbol);
5875
                _LMSymbol = null;
5876
            }
5877

    
5878
            endList.Add(note);
5879
        }
5880

    
5881
        private void JoinRunBySameType(string modelItemId, ref string survivorId)
5882
        {
5883
            LMModelItem modelItem = dataSource.GetModelItem(modelItemId);
5884
            if (modelItem != null)
5885
            {
5886
                foreach (LMRepresentation rep in modelItem.Representations)
5887
                {
5888
                    if (rep.Attributes["RepresentationType"].Value.ToString() == "Connector" && rep.Attributes["ItemStatus"].Value.ToString() == "Active")
5889
                    {
5890
                        LMConnector connector = dataSource.GetConnector(rep.Id);
5891
                        if (connector.ConnectItem1SymbolObject != null && connector.ConnectItem1SymbolObject.RepresentationType.ToString() != "Branch")
5892
                        {
5893
                            LMSymbol symbol = connector.ConnectItem1SymbolObject;
5894
                            List<string> modelItemIds = FindOtherModelItemBySymbolWhereTypePipeRun(symbol, modelItem.Id);
5895
                            if (modelItemIds.Count == 1)
5896
                            {
5897
                                string joinModelItemId = modelItemIds[0];
5898
                                JoinRun(joinModelItemId, modelItemId, ref survivorId, false);
5899
                                if (survivorId != null)
5900
                                    break;
5901
                            }
5902
                        }
5903
                        if (connector.ConnectItem2SymbolObject != null && connector.ConnectItem2SymbolObject.RepresentationType.ToString() != "Branch")
5904
                        {
5905
                            LMSymbol symbol = connector.ConnectItem2SymbolObject;
5906
                            List<string> modelItemIds = FindOtherModelItemBySymbolWhereTypePipeRun(symbol, modelItem.Id);
5907
                            if (modelItemIds.Count == 1)
5908
                            {
5909
                                string joinModelItemId = modelItemIds[0];
5910
                                JoinRun(joinModelItemId, modelItemId, ref survivorId, false);
5911
                                if (survivorId != null)
5912
                                    break;
5913
                            }
5914
                        }
5915
                    }
5916
                }
5917
            }
5918
        }
5919

    
5920
        /// <summary>
5921
        /// Label의 좌표를 구하는 메서드(ID2 기준의 좌표 -> SPPID 좌표)
5922
        /// </summary>
5923
        /// <param name="x"></param>
5924
        /// <param name="y"></param>
5925
        /// <param name="originX"></param>
5926
        /// <param name="originY"></param>
5927
        /// <param name="SPPIDLabelLocation"></param>
5928
        /// <param name="location"></param>
5929
        private void CalcLabelLocation(ref double x, ref double y, double originX, double originY, SPPIDLabelLocationInfo SPPIDLabelLocation, Location location)
5930
        {
5931
            if (location == Location.None)
5932
            {
5933
                x = originX;
5934
                y = originY;
5935
            }
5936
            else
5937
            {
5938
                if (location.HasFlag(Location.Center))
5939
                {
5940
                    x = (SPPIDLabelLocation.X1 + SPPIDLabelLocation.X2) / 2;
5941
                    y = (SPPIDLabelLocation.Y1 + SPPIDLabelLocation.Y2) / 2;
5942
                }
5943

    
5944
                if (location.HasFlag(Location.Left))
5945
                    x = SPPIDLabelLocation.X1;
5946
                else if (location.HasFlag(Location.Right))
5947
                    x = SPPIDLabelLocation.X2;
5948

    
5949
                if (location.HasFlag(Location.Down))
5950
                    y = SPPIDLabelLocation.Y1;
5951
                else if (location.HasFlag(Location.Up))
5952
                    y = SPPIDLabelLocation.Y2;
5953
            }
5954
        }
5955

    
5956
        /// <summary>
5957
        /// Symbol의 우선순위 Modeling 목록을 가져온다.
5958
        /// 1. Angle Valve
5959
        /// 2. 3개로 이루어진 Symbol Group
5960
        /// </summary>
5961
        /// <returns></returns>
5962
        private List<Symbol> GetPrioritySymbol()
5963
        {
5964
            DataTable symbolTable = document.SymbolTable;
5965
            // List에 순서대로 쌓는다.
5966
            List<Symbol> symbols = new List<Symbol>();
5967

    
5968
            // Angle Valve 부터
5969
            foreach (var symbol in document.SYMBOLS.FindAll(x => x.CONNECTORS.FindAll(y => y.Index == 0).Count == 2))
5970
            {
5971
                if (!symbols.Contains(symbol))
5972
                {
5973
                    double originX = 0;
5974
                    double originY = 0;
5975

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

    
5980
                    SlopeType slopeType1 = SlopeType.None;
5981
                    SlopeType slopeType2 = SlopeType.None;
5982
                    foreach (Connector connector in symbol.CONNECTORS.FindAll(x => x.Index == 0))
5983
                    {
5984
                        double connectorX = 0;
5985
                        double connectorY = 0;
5986
                        SPPIDUtil.ConvertPointBystring(connector.CONNECTPOINT, ref connectorX, ref connectorY);
5987
                        if (slopeType1 == SlopeType.None)
5988
                            slopeType1 = SPPIDUtil.CalcSlope(originX, originY, connectorX, connectorY);
5989
                        else
5990
                            slopeType2 = SPPIDUtil.CalcSlope(originX, originY, connectorX, connectorY);
5991
                    }
5992

    
5993
                    if ((slopeType1 == SlopeType.VERTICAL && slopeType2 == SlopeType.HORIZONTAL) ||
5994
                        (slopeType2 == SlopeType.VERTICAL && slopeType1 == SlopeType.HORIZONTAL))
5995
                        symbols.Add(symbol);
5996
                }
5997
            }
5998

    
5999
            List<Symbol> tempSymbols = new List<Symbol>();
6000
            // Conn 갯수 기준
6001
            foreach (var item in document.SYMBOLS)
6002
            {
6003
                if (!symbols.Contains(item))
6004
                    tempSymbols.Add(item);
6005
            }
6006
            tempSymbols.Sort(SortSymbolPriority);
6007
            symbols.AddRange(tempSymbols);
6008

    
6009
            return symbols;
6010
        }
6011

    
6012
        private void SetPriorityLine(List<Line> lines)
6013
        {
6014
            lines.Sort(SortLinePriority);
6015

    
6016
            int SortLinePriority(Line a, Line b)
6017
            {
6018
                // Branch 없는것부터
6019
                int branchRetval = CompareBranchLine(a, b);
6020
                if (branchRetval != 0)
6021
                {
6022
                    return branchRetval;
6023
                }
6024
                else
6025
                {
6026
                    // Symbol 연결 갯수
6027
                    int connSymbolRetval = CompareConnSymbol(a, b);
6028
                    if (connSymbolRetval != 0)
6029
                    {
6030
                        return connSymbolRetval;
6031
                    }
6032
                    else
6033
                    {
6034
                        // 아이템 연결 갯수(심볼, Line이면서 Not Branch)
6035
                        int connItemRetval = CompareConnItem(a, b);
6036
                        if (connItemRetval != 0)
6037
                        {
6038
                            return connItemRetval;
6039
                        }
6040
                        else
6041
                        {
6042
                            // ConnectedItem이 없는것
6043
                            int noneConnRetval = CompareNoneConn(a, b);
6044
                            if (noneConnRetval != 0)
6045
                            {
6046
                                return noneConnRetval;
6047
                            }
6048
                            else
6049
                            {
6050

    
6051
                            }
6052
                        }
6053
                    }
6054
                }
6055

    
6056
                return 0;
6057
            }
6058

    
6059
            int CompareNotSegmentLine(Line a, Line b)
6060
            {
6061
                List<Connector> connectorsA = a.CONNECTORS
6062
                    .Where(conn => conn.ConnectedObject != null && conn.ConnectedObject.GetType() == typeof(Symbol))
6063
                    .ToList();
6064

    
6065
                List<Connector> connectorsB = b.CONNECTORS
6066
                    .Where(conn => conn.ConnectedObject != null && conn.ConnectedObject.GetType() == typeof(Symbol))
6067
                    .ToList();
6068

    
6069
                // 오름차순
6070
                return connectorsB.Count.CompareTo(connectorsA.Count);
6071
            }
6072

    
6073
            int CompareConnSymbol(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 CompareConnItem(Line a, Line b)
6088
            {
6089
                List<Connector> connectorsA = a.CONNECTORS
6090
                    .Where(conn => conn.ConnectedObject != null &&
6091
                    (conn.ConnectedObject.GetType() == typeof(Symbol) ||
6092
                    (conn.ConnectedObject.GetType() == typeof(Line) && !SPPIDUtil.IsBranchLine((Line)conn.ConnectedObject, a))))
6093
                    .ToList();
6094

    
6095
                List<Connector> connectorsB = b.CONNECTORS
6096
                    .Where(conn => conn.ConnectedObject != null &&
6097
                    (conn.ConnectedObject.GetType() == typeof(Symbol) ||
6098
                    (conn.ConnectedObject.GetType() == typeof(Line) && !SPPIDUtil.IsBranchLine((Line)conn.ConnectedObject, b))))
6099
                    .ToList();
6100

    
6101
                // 오름차순
6102
                return connectorsB.Count.CompareTo(connectorsA.Count);
6103
            }
6104

    
6105
            int CompareBranchLine(Line a, Line b)
6106
            {
6107
                List<Connector> connectorsA = a.CONNECTORS
6108
                    .Where(conn => conn.ConnectedObject != null && conn.ConnectedObject.GetType() == typeof(Line) && SPPIDUtil.IsBranchLine(a, conn.ConnectedObject as Line))
6109
                    .ToList();
6110
                List<Connector> connectorsB = b.CONNECTORS
6111
                    .Where(conn => conn.ConnectedObject != null && conn.ConnectedObject.GetType() == typeof(Line) && SPPIDUtil.IsBranchLine(b, conn.ConnectedObject as Line))
6112
                    .ToList();
6113

    
6114
                // 내림차순
6115
                return connectorsA.Count.CompareTo(connectorsB.Count);
6116
            }
6117

    
6118
            int CompareNoneConn(Line a, Line b)
6119
            {
6120
                List<Connector> connectorsA = a.CONNECTORS
6121
                    .Where(conn => conn.ConnectedObject == null)
6122
                    .ToList();
6123

    
6124
                List<Connector> connectorsB = b.CONNECTORS
6125
                    .Where(conn => conn.ConnectedObject == null)
6126
                    .ToList();
6127

    
6128
                // 오름차순
6129
                return connectorsB.Count.CompareTo(connectorsA.Count);
6130
            }
6131
        }
6132

    
6133
        private void SortText(List<Text> texts)
6134
        {
6135
            texts.Sort(Sort);
6136

    
6137
            int Sort(Text a, Text b)
6138
            {
6139
                int yRetval = CompareY(a, b);
6140
                if (yRetval != 0)
6141
                {
6142
                    return yRetval;
6143
                }
6144
                else
6145
                {
6146
                    return CompareX(a, b);
6147
                }
6148
            }
6149

    
6150
            int CompareY(Text a, Text b)
6151
            {
6152
                return a.LOCATION_Y.CompareTo(b.LOCATION_Y);
6153
            }
6154

    
6155
            int CompareX(Text a, Text b)
6156
            {
6157
                return a.LOCATION_X.CompareTo(b.LOCATION_X);
6158
            }
6159
        }
6160
        private void SortNote(List<Note> notes)
6161
        {
6162
            notes.Sort(Sort);
6163

    
6164
            int Sort(Note a, Note b)
6165
            {
6166
                int yRetval = CompareY(a, b);
6167
                if (yRetval != 0)
6168
                {
6169
                    return yRetval;
6170
                }
6171
                else
6172
                {
6173
                    return CompareX(a, b);
6174
                }
6175
            }
6176

    
6177
            int CompareY(Note a, Note b)
6178
            {
6179
                return a.LOCATION_Y.CompareTo(b.LOCATION_Y);
6180
            }
6181

    
6182
            int CompareX(Note a, Note b)
6183
            {
6184
                return a.LOCATION_X.CompareTo(b.LOCATION_X);
6185
            }
6186
        }
6187

    
6188
        private void SortBranchLines()
6189
        {
6190
            BranchLines.Sort(SortBranchLine);
6191
            int SortBranchLine(Line a, Line b)
6192
            {
6193
                int countA = a.CONNECTORS.FindAll(x => x.ConnectedObject != null &&
6194
                 x.ConnectedObject.GetType() == typeof(Line) &&
6195
                 SPPIDUtil.IsBranchLine(x.ConnectedObject as Line, a) &&
6196
                 string.IsNullOrEmpty(((Line)x.ConnectedObject).SPPID.ModelItemId)).Count;
6197

    
6198
                int countB = b.CONNECTORS.FindAll(x => x.ConnectedObject != null &&
6199
                 x.ConnectedObject.GetType() == typeof(Line) &&
6200
                 SPPIDUtil.IsBranchLine(x.ConnectedObject as Line, b) &&
6201
                 string.IsNullOrEmpty(((Line)x.ConnectedObject).SPPID.ModelItemId)).Count;
6202

    
6203
                // 내림차순
6204
                return countA.CompareTo(countB);
6205
            }
6206
        }
6207

    
6208
        private static int SortSymbolPriority(Symbol a, Symbol b)
6209
        {
6210
            int countA = a.CONNECTORS.FindAll(x => !string.IsNullOrEmpty(x.CONNECTEDITEM) && x.CONNECTEDITEM != "None").Count;
6211
            int countB = b.CONNECTORS.FindAll(x => !string.IsNullOrEmpty(x.CONNECTEDITEM) && x.CONNECTEDITEM != "None").Count;
6212
            int retval = countB.CompareTo(countA);
6213
            if (retval != 0)
6214
                return retval;
6215
            else
6216
                return a.SPPID.ORIGINAL_X.CompareTo(b.SPPID.ORIGINAL_X);
6217
        }
6218

    
6219
        private string GetSPPIDFileName(LMModelItem modelItem)
6220
        {
6221
            string symbolPath = null;
6222
            foreach (LMRepresentation rep in modelItem.Representations)
6223
            {
6224
                if (!DBNull.Value.Equals(rep.FileName) && !string.IsNullOrEmpty(rep.FileName.ToString()))
6225
                {
6226
                    symbolPath = rep.FileName.ToString();
6227
                    break;
6228
                }
6229
            }
6230
            return symbolPath;
6231
        }
6232

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

    
6249
        /// <summary>
6250
        /// ComObject를 Release
6251
        /// </summary>
6252
        /// <param name="objVars"></param>
6253
        public void ReleaseCOMObjects(params object[] objVars)
6254
        {
6255
            if (objVars != null)
6256
            {
6257
                int intNewRefCount = 0;
6258
                foreach (object obj in objVars)
6259
                {
6260
                    if (!Information.IsNothing(obj) && System.Runtime.InteropServices.Marshal.IsComObject(obj))
6261
                        intNewRefCount = intNewRefCount + System.Runtime.InteropServices.Marshal.FinalReleaseComObject(obj);
6262
                }
6263
            }
6264
        }
6265

    
6266
        /// IDisposable 구현
6267
        ~AutoModeling()
6268
        {
6269
            this.Dispose(false);
6270
        }
6271

    
6272
        private bool disposed;
6273
        public void Dispose()
6274
        {
6275
            this.Dispose(true);
6276
            GC.SuppressFinalize(this);
6277
        }
6278

    
6279
        protected virtual void Dispose(bool disposing)
6280
        {
6281
            if (this.disposed) return;
6282
            if (disposing)
6283
            {
6284
                // IDisposable 인터페이스를 구현하는 멤버들을 여기서 정리합니다.
6285
            }
6286
            // .NET Framework에 의하여 관리되지 않는 외부 리소스들을 여기서 정리합니다.
6287
            this.disposed = true;
6288
        }
6289
    }
6290
}