프로젝트

일반

사용자정보

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

hytos / DTI_PID / SPPIDConverter / AutoModeling.cs @ b2064e69

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

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

    
39
        public string DocumentLabelText { get; set; }
40

    
41
        List<Line> BranchLines = new List<Line>();
42
        List<string> ZeroLengthSymbolToSymbolModelItemID = new List<string>();
43
        List<string> ZeroLengthModelItemID = new List<string>();
44
        List<string> ZeroLengthModelItemIDReverse = new List<string>();
45
        List<Symbol> prioritySymbols;
46

    
47
        public AutoModeling(SPPID_Document document, bool closeDocument)
48
        {
49
            application = Interaction.GetObject("", "PIDAutomation.Application");
50
            WrapperApplication wApp = new WrapperApplication(application.Application);
51
            radApp = wApp.RADApplication;
52

    
53
            this.closeDocument = closeDocument;
54
            this.document = document;
55
            this._ETCSetting = ETCSetting.GetInstance();
56
        }
57

    
58
        private void SetSystemEditingCommand(bool value)
59
        {
60
            foreach (var item in radApp.Commands)
61
            {
62
                if (item.Argument == "SystemEditingCmd.SystemEditing")
63
                {
64
                    if (item.Checked != value)
65
                    {
66
                        radApp.RunMacro("systemeditingcmd.dll");
67
                        break;
68
                    }
69

    
70
                }
71
            }
72
        }
73

    
74
        /// <summary>
75
        /// 도면 단위당 실행되는 메서드
76
        /// </summary>
77
        public void Run()
78
        {
79
            string drawingNumber = document.DrawingNumber;
80
            string drawingName = document.DrawingName;
81
            try
82
            {
83
                _placement = new Placement();
84
                dataSource = _placement.PIDDataSource;
85
                
86
                if (CreateDocument(ref drawingNumber, ref drawingName) && DocumentCoordinateCorrection())
87
                {
88
                    Log.Write("Start Modeling");
89
                    SplashScreenManager.ShowForm(typeof(SPPIDSplashScreen), true, true);
90
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetParent, (IntPtr)radApp.HWnd);
91
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetAllStepCount, 23);
92
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetDocumentName, DocumentLabelText);
93

    
94
                    // VendorPackage Modeling
95
                    RunVendorPackageModeling();
96
                    // Equipment Modeling
97
                    RunEquipmentModeling();
98
                    // Symbol Modeling
99
                    RunSymbolModeling();
100
                    // LineRun Line Modeling
101
                    RunLineModeling();
102
                    // Vent Drain Modeling
103
                    RunVentDrainModeling();
104
                    // Clear Attribute
105
                    RunClearNominalDiameter();
106
                    // Join SameConnector
107
                    RunJoinRunForSameConnector();
108
                    // Join Run
109
                    RunJoinRun();
110
                    // EndBreak Modeling
111
                    RunEndBreakModeling();
112
                    // SpecBreak Modeling
113
                    RunSpecBreakModeling();
114
                    //Line Number Modeling
115
                    RunLineNumberModeling();
116
                    // Note Modeling
117
                    RunNoteModeling();
118
                    // Text Modeling
119
                    RunTextModeling();
120
                    // Input LineNumber Attribute
121
                    RunInputLineNumberAttribute();
122
                    // Input Symbol Attribute
123
                    RunInputSymbolAttribute();
124
                    // Input SpecBreak Attribute
125
                    RunInputSpecBreakAttribute();
126
                    // Input EndBreak Attribute
127
                    RunInputEndBreakAttribute();
128
                    // Label Symbol Modeling
129
                    RunLabelSymbolModeling();
130
                    // Correct Text
131
                    RunCorrectAssociationText();
132

    
133
                    // Result Logging
134
                    document.CheckModelingResult();
135
                }
136
            }
137
            catch (Exception ex)
138
            {
139
                if (SplashScreenManager.Default != null && SplashScreenManager.Default.IsSplashFormVisible)
140
                {
141
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.ClearParent, null);
142
                    SplashScreenManager.CloseForm(false);
143
                    Log.Write("\r\n");
144
                }
145
                System.Windows.Forms.MessageBox.Show(ex.Message + "\r\n" + ex.StackTrace);
146
            }
147
            finally
148
            {
149
                Project_DB.InsertDrawingInfoAndOPCInfo(document.PATH, drawingNumber, drawingName, document);
150
                if (SplashScreenManager.Default != null && SplashScreenManager.Default.IsSplashFormVisible)
151
                {
152
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.ClearParent, null);
153
                    SplashScreenManager.CloseForm(false);
154
                    Log.Write("\r\n");
155
                }
156
                Thread.Sleep(1000);
157

    
158
                Log.Write("End Modeling");
159
                radApp.ActiveWindow.Fit();
160

    
161
                ReleaseCOMObjects(application);
162
                application = null;
163
                if (radApp.ActiveDocument != null)
164
                {
165
                    if (closeDocument && newDrawing != null)
166
                    {
167
                        newDrawing.Save();
168
                        newDrawing.CloseDrawing(true);
169
                        ReleaseCOMObjects(newDrawing);
170
                        newDrawing = null;
171
                    }
172
                    else if (newDrawing == null)
173
                    {
174
                        Log.Write("error document");
175
                    }
176
                }
177

    
178
                ReleaseCOMObjects(dataSource);
179
                dataSource = null;
180
                ReleaseCOMObjects(_placement);
181
                _placement = null;
182

    
183
                Thread.Sleep(1000);
184
            }
185
        }
186

    
187
        private void RunVendorPackageModeling()
188
        {
189
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetAllProgress, document.VendorPackages.Count);
190
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "VendorPackages Modeling");
191
            foreach (VendorPackage item in document.VendorPackages)
192
            {
193
                try
194
                {
195
                    VendorPackageModeling(item);
196
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
197
                }
198
                catch (Exception ex)
199
                {
200
                    Log.Write("Error in RunVendorPackageModeling");
201
                    Log.Write("UID : " + item.UID);
202
                    Log.Write(ex.Message);
203
                    Log.Write(ex.StackTrace);
204
                }
205
            }
206
        }
207
        private void RunEquipmentModeling()
208
        {
209
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetAllProgress, document.Equipments.Count);
210
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Equipments Modeling");
211
            foreach (Equipment item in document.Equipments)
212
            {
213
                try
214
                {
215
                    EquipmentModeling(item);
216
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetProgress, document.Equipments.FindAll(x => !string.IsNullOrEmpty(x.SPPID.RepresentationId)).Count);
217
                }
218
                catch (Exception ex)
219
                {
220
                    Log.Write("Error in EquipmentModeling");
221
                    Log.Write("UID : " + item.UID);
222
                    Log.Write(ex.Message);
223
                    Log.Write(ex.StackTrace);
224
                }
225
            }
226
        }
227
        private void RunSymbolModeling()
228
        {
229
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetAllProgress, document.SYMBOLS.Count);
230
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Symbol Modeling");
231
            prioritySymbols = GetPrioritySymbol();
232
            foreach (var item in prioritySymbols)
233
            {
234
                try
235
                {
236
                    if (document.VentDrainSymbol.Contains(item))
237
                        continue;
238
                    SymbolModelingBySymbol(item);
239
                }
240
                catch (Exception ex)
241
                {
242
                    Log.Write("Error in SymbolModelingByPriority");
243
                    Log.Write("UID : " + item.UID);
244
                    Log.Write(ex.Message);
245
                    Log.Write(ex.StackTrace);
246
                }
247
            }
248
        }
249
        private void RunLineModeling()
250
        {
251
            List<Line> AllLine = document.LINES.ToList();
252
            List<Line> stepLast_Line = document.LINES.FindAll(x => x.CONNECTORS.FindAll(y => y.ConnectedObject != null && y.ConnectedObject.GetType() == typeof(Symbol)).Count == 2 &&
253
            !SPPIDUtil.IsBranchedLine(document, x));
254
            List<Line> step1_Line = AllLine.FindAll(x => !stepLast_Line.Contains(x));
255

    
256
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetAllProgress, step1_Line.Count);
257
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Line Modeling - 1");
258

    
259
            SetPriorityLine(step1_Line);
260
            foreach (var item in step1_Line)
261
            {
262
                try
263
                {
264
                    if (document.VentDrainLine.Contains(item))
265
                        continue;
266
                    NewLineModeling(item);
267
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
268
                }
269
                catch (Exception ex)
270
                {
271
                    Log.Write("Error in NewLineModeling");
272
                    Log.Write("UID : " + item.UID);
273
                    Log.Write(ex.Message);
274
                    Log.Write(ex.StackTrace);
275
                }
276
            }
277

    
278
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetAllProgress, BranchLines.Count);
279
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Line Modeling - 2");
280
            int branchCount = BranchLines.Count;
281
            while (BranchLines.Count > 0)
282
            {
283
                try
284
                {
285
                    SortBranchLines();
286
                    Line item = BranchLines[0];
287
                    NewLineModeling(item, true);
288
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
289
                }
290
                catch (Exception ex)
291
                {
292
                    Log.Write("Error in NewLineModeling");
293
                    Log.Write("UID : " + BranchLines[0].UID);
294
                    Log.Write(ex.Message);
295
                    Log.Write(ex.StackTrace);
296
                    BranchLines.Remove(BranchLines[0]);
297
                }
298
            }
299

    
300
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetAllProgress, stepLast_Line.Count);
301
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Line Modeling - 3");
302
            foreach (var item in stepLast_Line)
303
            {
304
                try
305
                {
306
                    if (document.VentDrainLine.Contains(item))
307
                        continue;
308
                    NewLineModeling(item);
309
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
310
                }
311
                catch (Exception ex)
312
                {
313
                    Log.Write("Error in NewLineModeling");
314
                    Log.Write("UID : " + item.UID);
315
                    Log.Write(ex.Message);
316
                    Log.Write(ex.StackTrace);
317
                }
318
            }
319
        }
320
        private void RunVentDrainModeling()
321
        {
322
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetAllProgress, document.VentDrainLine.Count);
323
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Vent Drain Modeling");
324
            foreach (var item in document.VentDrainLine)
325
            {
326
                try
327
                {
328
                    Connector connector = item.CONNECTORS.Find(x => x.ConnectedObject != null && x.ConnectedObject.GetType() == typeof(Symbol));
329
                    if (connector != null)
330
                    {
331
                        SetCoordinate();
332
                        SymbolModelingBySymbol(connector.ConnectedObject as Symbol);
333
                        NewLineModeling(item, true);
334
                    }
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
                void SetCoordinate()
346
                {
347
                    Connector branchConnector = item.CONNECTORS.Find(loop => loop.ConnectedObject != null && loop.ConnectedObject.GetType() == typeof(Line));
348
                    if (branchConnector != null)
349
                    {
350
                        Line connLine = branchConnector.ConnectedObject as Line;
351
                        double x = 0;
352
                        double y = 0;
353
                        GetTargetLineConnectorPoint(branchConnector, item, ref x, ref y);
354
                        LMConnector targetConnector = FindTargetLMConnectorForBranch(item, connLine, ref x, ref y);
355
                        if (targetConnector != null)
356
                        {
357
                            List<Symbol> group = new List<Symbol>();
358
                            SPPIDUtil.FindConnectedSymbolGroup(document, item.CONNECTORS.Find(loop => loop != branchConnector).ConnectedObject as Symbol, group);
359
                            if (item.SlopeType == SlopeType.HORIZONTAL)
360
                            {
361
                                item.SPPID.START_Y = y;
362
                                item.SPPID.END_Y = y;
363
                                foreach (var symbol in group)
364
                                {
365
                                    symbol.SPPID.ORIGINAL_Y = y;
366
                                    symbol.SPPID.SPPID_Y = y;
367
                                }
368
                            }
369
                            else if (item.SlopeType == SlopeType.VERTICAL)
370
                            {
371
                                item.SPPID.START_X = x;
372
                                item.SPPID.END_X = x;
373
                                foreach (var symbol in group)
374
                                {
375
                                    symbol.SPPID.ORIGINAL_X = x;
376
                                    symbol.SPPID.SPPID_X = x;
377
                                }
378
                            }
379
                        }
380
                        ReleaseCOMObjects(targetConnector);
381
                        targetConnector = null;
382
                    }
383
                }
384
            }
385
        }
386
        private void RunClearNominalDiameter()
387
        {
388
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetAllProgress, document.SYMBOLS.Count + document.LINES.Count);
389
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Clear Attribute");
390
            List<string> endClearModelItemID = new List<string>();
391
            for (int i = 0; i < document.LINES.Count; i++)
392
            {
393
                Line item = document.LINES[i];
394
                string modelItemID = item.SPPID.ModelItemId;
395
                if (!string.IsNullOrEmpty(modelItemID))
396
                {
397
                    LMModelItem modelItem = dataSource.GetModelItem(modelItemID);
398
                    if (modelItem != null)
399
                    {
400
                        LMAAttribute attribute = modelItem.Attributes["NominalDiameter"];
401
                        if (attribute != null)
402
                            attribute.set_Value(DBNull.Value);
403

    
404
                        modelItem.Commit();
405
                        ReleaseCOMObjects(modelItem);
406
                        modelItem = null;
407
                    }
408
                }
409
                if (!endClearModelItemID.Contains(modelItemID))
410
                    endClearModelItemID.Add(modelItemID);
411
                SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
412
            }
413
            for (int i = 0; i < document.SYMBOLS.Count; i++)
414
            {
415
                Symbol item = document.SYMBOLS[i];
416
                string repID = item.SPPID.RepresentationId;
417
                string modelItemID = item.SPPID.ModelItemID;
418
                if (!string.IsNullOrEmpty(modelItemID))
419
                {
420
                    LMModelItem modelItem = dataSource.GetModelItem(modelItemID);
421
                    if (modelItem != null)
422
                    {
423
                        LMAAttribute attribute = modelItem.Attributes["NominalDiameter"];
424
                        if (attribute != null)
425
                            attribute.set_Value(DBNull.Value);
426
                        int index = 1;
427
                        while (true)
428
                        {
429
                            attribute = modelItem.Attributes[string.Format("PipingPoint{0}.NominalDiameter", index)];
430
                            if (attribute != null)
431
                                attribute.set_Value(DBNull.Value);
432
                            else
433
                                break;
434
                            index++;
435
                        }
436
                        modelItem.Commit();
437
                        ReleaseCOMObjects(modelItem);
438
                        modelItem = null;
439
                    }
440
                }
441
                if (!string.IsNullOrEmpty(repID))
442
                {
443
                    LMSymbol symbol = dataSource.GetSymbol(repID);
444
                    if (symbol != null)
445
                    {
446
                        foreach (LMConnector connector in symbol.Connect1Connectors)
447
                        {
448
                            if (connector.get_ItemStatus() == "Active" && !endClearModelItemID.Contains(connector.ModelItemID))
449
                            {
450
                                endClearModelItemID.Add(connector.ModelItemID);
451
                                LMModelItem modelItem = connector.ModelItemObject;
452
                                if (modelItem != null)
453
                                {
454
                                    LMAAttribute attribute = modelItem.Attributes["NominalDiameter"];
455
                                    if (attribute != null)
456
                                        attribute.set_Value(DBNull.Value);
457

    
458
                                    modelItem.Commit();
459
                                    ReleaseCOMObjects(modelItem);
460
                                    modelItem = null;
461
                                }
462
                            }
463
                        }
464
                        foreach (LMConnector connector in symbol.Connect2Connectors)
465
                        {
466
                            if (connector.get_ItemStatus() == "Active" && !endClearModelItemID.Contains(connector.ModelItemID))
467
                            {
468
                                endClearModelItemID.Add(connector.ModelItemID);
469
                                LMModelItem modelItem = connector.ModelItemObject;
470
                                if (modelItem != null)
471
                                {
472
                                    LMAAttribute attribute = modelItem.Attributes["NominalDiameter"];
473
                                    if (attribute != null)
474
                                        attribute.set_Value(DBNull.Value);
475

    
476
                                    modelItem.Commit();
477
                                    ReleaseCOMObjects(modelItem);
478
                                    modelItem = null;
479
                                }
480
                            }
481
                        }
482
                    }
483
                    ReleaseCOMObjects(symbol);
484
                    symbol = null;
485
                }
486
                SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
487
            }
488
        }
489
        private void RunClearValueInconsistancy()
490
        {
491
            int count = 1;
492
            bool loop = true;
493
            while (loop)
494
            {
495
                loop = false;
496
                LMAFilter filter = new LMAFilter();
497
                LMACriterion criterion = new LMACriterion();
498
                filter.ItemType = "Relationship";
499
                criterion.SourceAttributeName = "SP_DRAWINGID";
500
                criterion.Operator = "=";
501
                criterion.set_ValueAttribute(drawingID);
502
                filter.get_Criteria().Add(criterion);
503

    
504
                LMRelationships relationships = new LMRelationships();
505
                relationships.Collect(dataSource, Filter: filter);
506

    
507
                SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetAllProgress, relationships.Count);
508
                if (count > 1)
509
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStepMinus, null);
510
                SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Clear Inconsistent Property Value - " + count);
511
                foreach (LMRelationship relationship in relationships)
512
                {
513
                    foreach (LMInconsistency inconsistency in relationship.Inconsistencies)
514
                    {
515
                        if (inconsistency.get_InconsistencyTypeIndex() == 1)
516
                        {
517
                            LMModelItem modelItem1 = relationship.Item1RepresentationObject == null ? null : relationship.Item1RepresentationObject.ModelItemObject;
518
                            LMModelItem modelItem2 = relationship.Item2RepresentationObject == null ? null : relationship.Item2RepresentationObject.ModelItemObject;
519
                            string[] array = inconsistency.get_Name().ToString().Split(new char[] { '=' });
520
                            if (modelItem1 != null)
521
                            {
522
                                string attrName = array[0];
523
                                if (attrName.Contains("PipingPoint"))
524
                                {
525
                                    string originalAttr = attrName.Split(new char[] { '.' })[1];
526
                                    int index = Convert.ToInt32(relationship.get_Item1Location());
527
                                    LMAAttribute attribute1 = modelItem1.Attributes["PipingPoint" + index + "." + originalAttr];
528
                                    if (attribute1 != null && !DBNull.Value.Equals(attribute1.get_Value()))
529
                                    {
530
                                        loop = true;
531
                                        attribute1.set_Value(DBNull.Value);
532
                                    }
533
                                    attribute1 = null;
534
                                }
535
                                else
536
                                {
537
                                    LMAAttribute attribute1 = modelItem1.Attributes[attrName];
538
                                    if (attribute1 != null && !DBNull.Value.Equals(attribute1.get_Value()))
539
                                    {
540
                                        loop = true;
541
                                        attribute1.set_Value(DBNull.Value);
542
                                    }
543
                                    attribute1 = null;
544
                                }
545
                                modelItem1.Commit();
546
                            }
547
                            if (modelItem2 != null)
548
                            {
549
                                string attrName = array[1];
550
                                if (attrName.Contains("PipingPoint"))
551
                                {
552
                                    string originalAttr = attrName.Split(new char[] { '.' })[1];
553
                                    int index = Convert.ToInt32(relationship.get_Item2Location());
554
                                    LMAAttribute attribute2 = modelItem2.Attributes["PipingPoint" + index + "." + originalAttr];
555
                                    if (attribute2 != null && !DBNull.Value.Equals(attribute2.get_Value()))
556
                                    {
557
                                        attribute2.set_Value(DBNull.Value);
558
                                        loop = true;
559
                                    }
560
                                    attribute2 = null;
561
                                }
562
                                else
563
                                {
564
                                    LMAAttribute attribute2 = modelItem2.Attributes[attrName];
565
                                    if (attribute2 != null && !DBNull.Value.Equals(attribute2.get_Value()))
566
                                    {
567
                                        attribute2.set_Value(DBNull.Value);
568
                                        loop = true;
569
                                    }
570
                                    attribute2 = null;
571
                                }
572
                                modelItem2.Commit();
573
                            }
574
                            ReleaseCOMObjects(modelItem1);
575
                            modelItem1 = null;
576
                            ReleaseCOMObjects(modelItem2);
577
                            modelItem2 = null;
578
                            inconsistency.Commit();
579
                        }
580
                        ReleaseCOMObjects(inconsistency);
581
                    }
582
                    relationship.Commit();
583
                    ReleaseCOMObjects(relationship);
584
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
585
                }
586
                ReleaseCOMObjects(filter);
587
                filter = null;
588
                ReleaseCOMObjects(criterion);
589
                criterion = null;
590
                ReleaseCOMObjects(relationships);
591
                relationships = null;
592
                count++;
593
            }
594
        }
595
        private void RunEndBreakModeling()
596
        {
597
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetAllProgress, document.EndBreaks.Count);
598
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "EndBreaks Modeling");
599
            foreach (var item in document.EndBreaks)
600
                try
601
                {
602
                    EndBreakModeling(item);
603
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
604
                }
605
                catch (Exception ex)
606
                {
607
                    Log.Write("Error in EndBreakModeling");
608
                    Log.Write("UID : " + item.UID);
609
                    Log.Write(ex.Message);
610
                    Log.Write(ex.StackTrace);
611
                }
612
        }
613
        private void RunSpecBreakModeling()
614
        {
615
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetAllProgress, document.SpecBreaks.Count);
616
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "SpecBreaks Modeling");
617
            foreach (var item in document.SpecBreaks)
618
                try
619
                {
620
                    SpecBreakModeling(item);
621
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
622
                }
623
                catch (Exception ex)
624
                {
625
                    Log.Write("Error in SpecBreakModeling");
626
                    Log.Write("UID : " + item.UID);
627
                    Log.Write(ex.Message);
628
                    Log.Write(ex.StackTrace);
629
                }
630
        }
631
        private void RunJoinRunForSameConnector()
632
        {
633
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetAllProgress, document.LINES.Count);
634
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "PipeRun Join - 1");
635
            foreach (var line in document.LINES)
636
            {
637
                Dictionary<LMConnector, List<double[]>> vertices = GetPipeRunVertices(line.SPPID.ModelItemId, false);
638
                List<List<double[]>> result = new List<List<double[]>>();
639
                foreach (var item in vertices)
640
                {
641
                    ReleaseCOMObjects(item.Key);
642
                    result.Add(item.Value);
643
                }
644
                line.SPPID.Vertices = result;
645
                vertices = null;
646
            }
647

    
648
            foreach (var line in document.LINES)
649
            {
650
                foreach (var connector in line.CONNECTORS)
651
                {
652
                    if (connector.ConnectedObject != null &&
653
                        connector.ConnectedObject.GetType() == typeof(Line) &&
654
                        !SPPIDUtil.IsBranchLine(line, connector.ConnectedObject as Line))
655
                    {
656
                        Line connLine = connector.ConnectedObject as Line;
657
                        if (line.SPPID.ModelItemId != connLine.SPPID.ModelItemId &&
658
                            !string.IsNullOrEmpty(line.SPPID.ModelItemId) &&
659
                            !string.IsNullOrEmpty(connLine.SPPID.ModelItemId) &&
660
                            !SPPIDUtil.IsSegment(document, line, connLine))
661
                        {
662
                            string survivorId = string.Empty;
663
                            JoinRun(connLine.SPPID.ModelItemId, line.SPPID.ModelItemId, ref survivorId);
664
                        }
665

    
666
                    }
667
                }
668
                SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
669
            }
670

    
671
            foreach (var line in document.LINES)
672
                line.SPPID.Representations = GetRepresentations(line.SPPID.ModelItemId);
673
        }
674
        private void RunJoinRun()
675
        {
676
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetAllProgress, document.LINES.Count);
677
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "PipeRun Join - 2");
678
            List<string> endModelID = new List<string>();
679
            foreach (var line in document.LINES)
680
            {
681
                if (!endModelID.Contains(line.SPPID.ModelItemId))
682
                {
683
                    while (!endModelID.Contains(line.SPPID.ModelItemId))
684
                    {
685
                        string survivorId = string.Empty;
686
                        JoinRunBySameType(line.SPPID.ModelItemId, ref survivorId);
687
                        if (string.IsNullOrEmpty(survivorId))
688
                        {
689
                            endModelID.Add(line.SPPID.ModelItemId);
690
                        }
691
                    }
692
                }
693
                SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
694
            }
695
        }
696
        private void RunLineNumberModeling()
697
        {
698
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetAllProgress, document.LINENUMBERS.Count);
699
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Line Number Modeling");
700
            foreach (var item in document.LINENUMBERS)
701
            {
702
                LMLabelPersist label = dataSource.GetLabelPersist(item.SPPID.RepresentationId);
703
                if (label == null || (label != null && label.get_ItemStatus() != "Active"))
704
                {
705
                    ReleaseCOMObjects(label);
706
                    item.SPPID.RepresentationId = null;
707
                    LineNumberModeling(item);
708
                }
709
                SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
710
            }
711
        }
712
        private void RunNoteModeling()
713
        {
714
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetAllProgress, document.SYMBOLS.Count);
715
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Notes Modeling");
716
            List<Note> correctList = new List<Note>();
717
            foreach (var item in document.NOTES)
718
                try
719
                {
720
                    NoteModeling(item, correctList);
721
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
722
                }
723
                catch (Exception ex)
724
                {
725
                    Log.Write("Error in NoteModeling");
726
                    Log.Write("UID : " + item.UID);
727
                    Log.Write(ex.Message);
728
                    Log.Write(ex.StackTrace);
729
                }
730

    
731
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetAllProgress, correctList.Count);
732
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Correct Note");
733
            SortNote(correctList);
734
            List<Note> endList = new List<Note>();
735
            if (correctList.Count > 0)
736
                endList.Add(correctList[0]);
737
            foreach (var item in correctList)
738
                try
739
                {
740
                    if (!endList.Contains(item))
741
                        NoteCorrectModeling(item, endList);
742
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
743
                }
744
                catch (Exception ex)
745
                {
746
                    Log.Write("Error in NoteModeling");
747
                    Log.Write("UID : " + item.UID);
748
                    Log.Write(ex.Message);
749
                    Log.Write(ex.StackTrace);
750
                }
751
        }
752
        private void RunTextModeling()
753
        {
754
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetAllProgress, document.TEXTINFOS.Count);
755
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Texts Modeling");
756
            SortText(document.TEXTINFOS);
757
            foreach (var item in document.TEXTINFOS)
758
                try
759
                {
760
                    if (item.ASSOCIATION)
761
                        AssociationTextModeling(item);
762
                    else
763
                        NormalTextModeling(item);
764
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
765
                }
766
                catch (Exception ex)
767
                {
768
                    Log.Write("Error in TextModeling");
769
                    Log.Write("UID : " + item.UID);
770
                    Log.Write(ex.Message);
771
                    Log.Write(ex.StackTrace);
772
                }
773
        }
774
        private void RunInputLineNumberAttribute()
775
        {
776
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetAllProgress, document.LINENUMBERS.Count);
777
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Set LineNumbers Attribute");
778
            List<string> endLine = new List<string>();
779
            foreach (var item in document.LINENUMBERS)
780
                try
781
                {
782
                    InputLineNumberAttribute(item, endLine);
783
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
784
                }
785
                catch (Exception ex)
786
                {
787
                    Log.Write("Error in InputLineNumberAttribute");
788
                    Log.Write("UID : " + item.UID);
789
                    Log.Write(ex.Message);
790
                    Log.Write(ex.StackTrace);
791
                }
792
        }
793
        private void RunInputSymbolAttribute()
794
        {
795
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetAllProgress, document.SYMBOLS.Count);
796
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Set Symbols Attribute");
797
            foreach (var item in document.SYMBOLS)
798
                try
799
                {
800
                    InputSymbolAttribute(item, item.ATTRIBUTES);
801
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
802
                }
803
                catch (Exception ex)
804
                {
805
                    Log.Write("Error in InputSymbolAttribute");
806
                    Log.Write("UID : " + item.UID);
807
                    Log.Write(ex.Message);
808
                    Log.Write(ex.StackTrace);
809
                }
810
        }
811
        private void RunInputSpecBreakAttribute()
812
        {
813
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetAllProgress, document.SpecBreaks.Count);
814
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Set SpecBreak Attribute");
815
            foreach (var item in document.SpecBreaks)
816
                try
817
                {
818
                    InputSpecBreakAttribute(item);
819
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
820
                }
821
                catch (Exception ex)
822
                {
823
                    Log.Write("Error in InputSpecBreakAttribute");
824
                    Log.Write("UID : " + item.UID);
825
                    Log.Write(ex.Message);
826
                    Log.Write(ex.StackTrace);
827
                }
828
        }
829
        private void RunInputEndBreakAttribute()
830
        {
831
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetAllProgress, document.EndBreaks.Count);
832
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Set EndBreak Attribute");
833
            foreach (var item in document.EndBreaks)
834
                try
835
                {
836
                    InputEndBreakAttribute(item);
837
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
838
                }
839
                catch (Exception ex)
840
                {
841
                    Log.Write("Error in RunInputEndBreakAttribute");
842
                    Log.Write("UID : " + item.UID);
843
                    Log.Write(ex.Message);
844
                    Log.Write(ex.StackTrace);
845
                }
846
        }
847
        private void RunLabelSymbolModeling()
848
        {
849
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetAllProgress, document.SYMBOLS.Count);
850
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Labels Modeling");
851
            foreach (var item in document.SYMBOLS)
852
                try
853
                {
854
                    LabelSymbolModeling(item);
855
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
856
                }
857
                catch (Exception ex)
858
                {
859
                    Log.Write("Error in LabelSymbolModeling");
860
                    Log.Write("UID : " + item.UID);
861
                    Log.Write(ex.Message);
862
                    Log.Write(ex.StackTrace);
863
                }
864
        }
865
        private void RunCorrectAssociationText()
866
        {
867
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetAllProgress, document.TEXTINFOS.Count + document.LINENUMBERS.Count);
868
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Correct Labels");
869
            List<Text> endTexts = new List<Text>();
870
            foreach (var item in document.TEXTINFOS)
871
            {
872
                try
873
                {
874
                    if (item.ASSOCIATION && !endTexts.Contains(item))
875
                        AssociationTextCorrectModeling(item, endTexts);
876
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
877
                }
878
                catch (Exception ex)
879
                {
880
                    Log.Write("Error in RunCorrectAssociationText");
881
                    Log.Write("UID : " + item.UID);
882
                    Log.Write(ex.Message);
883
                    Log.Write(ex.StackTrace);
884
                }
885
                
886
            }
887

    
888
            foreach (var item in document.LINENUMBERS)
889
            {
890
                try
891
                {
892
                    LineNumberCorrectModeling(item);
893
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
894
                }
895
                catch (Exception ex)
896
                {
897
                    Log.Write("Error in RunCorrectAssociationText");
898
                    Log.Write("UID : " + item.UID);
899
                    Log.Write(ex.Message);
900
                    Log.Write(ex.StackTrace);
901
                }
902
            }
903
        }
904
        /// <summary>
905
        /// 도면 생성 메서드
906
        /// </summary>
907
        private bool CreateDocument(ref string drawingNumber, ref string drawingName)
908
        {
909
            Log.Write("------------------ Start create document ------------------");
910
            GetDrawingNameAndNumber(ref drawingName, ref drawingNumber);
911
            Log.Write("Drawing name : " + drawingName);
912
            Log.Write("Drawing number : " + drawingNumber);
913
            Thread.Sleep(1000);
914
            newDrawing = application.Drawings.Add(document.Unit, document.Template, drawingNumber, drawingName);
915
            if (newDrawing != null)
916
            {
917
                document.SPPID_DrawingNumber = drawingNumber;
918
                document.SPPID_DrawingName = drawingName;
919
                Thread.Sleep(1000);
920
                radApp.ActiveWindow.Fit();
921
                Thread.Sleep(1000);
922
                radApp.ActiveWindow.Zoom = 2000;
923
                Thread.Sleep(2000);
924

    
925
                //current LMDrawing 가져오기
926
                LMAFilter filter = new LMAFilter();
927
                LMACriterion criterion = new LMACriterion();
928
                filter.ItemType = "Drawing";
929
                criterion.SourceAttributeName = "Name";
930
                criterion.Operator = "=";
931
                criterion.set_ValueAttribute(drawingName);
932
                filter.get_Criteria().Add(criterion);
933

    
934
                LMDrawings drawings = new LMDrawings();
935
                drawings.Collect(dataSource, Filter: filter);
936

    
937
                drawingID = ((dynamic)drawings).Nth(1).Id;
938
                ReleaseCOMObjects(filter);
939
                ReleaseCOMObjects(criterion);
940
                ReleaseCOMObjects(drawings);
941
                filter = null;
942
                criterion = null;
943
                drawings = null;
944
            }
945
            else
946
                Log.Write("Fail Create Drawing");
947

    
948
            if (newDrawing != null)
949
                return true;
950
            else
951
                return false;
952
        }
953

    
954
        /// <summary>
955
        /// DrawingName, DrawingNumber를 확인하여 중복이 있으면 _1을 붙이고 +1씩 한다.
956
        /// </summary>
957
        /// <param name="drawingName"></param>
958
        /// <param name="drawingNumber"></param>
959
        private void GetDrawingNameAndNumber(ref string drawingName, ref string drawingNumber)
960
        {
961
            LMDrawings drawings = new LMDrawings();
962
            drawings.Collect(dataSource);
963

    
964
            List<string> drawingNameList = new List<string>();
965
            List<string> drawingNumberList = new List<string>();
966

    
967
            foreach (LMDrawing item in drawings)
968
            {
969
                drawingNameList.Add(item.Attributes["Name"].get_Value().ToString());
970
                drawingNumberList.Add(item.Attributes["DrawingNumber"].get_Value().ToString());
971
            }
972

    
973
            int nameLength = drawingName.Length;
974
            while (drawingNameList.Contains(drawingName))
975
            {
976
                if (nameLength == drawingName.Length)
977
                    drawingName += "-1";
978
                else
979
                {
980
                    int index = Convert.ToInt32(drawingName.Remove(0, nameLength + 1));
981
                    drawingName = drawingName.Substring(0, nameLength + 1);
982
                    drawingName += ++index;
983
                }
984
            }
985

    
986
            int numberLength = drawingNumber.Length;
987
            while (drawingNameList.Contains(drawingNumber))
988
            {
989
                if (numberLength == drawingNumber.Length)
990
                    drawingNumber += "-1";
991
                else
992
                {
993
                    int index = Convert.ToInt32(drawingNumber.Remove(0, numberLength + 1));
994
                    drawingNumber = drawingNumber.Substring(0, numberLength + 1);
995
                    drawingNumber += ++index;
996
                }
997
            }
998
            ReleaseCOMObjects(drawings);
999
            drawings = null;
1000
        }
1001

    
1002
        /// <summary>
1003
        /// 도면 크기 구하는 메서드
1004
        /// </summary>
1005
        /// <returns></returns>
1006
        private bool DocumentCoordinateCorrection()
1007
        {
1008
            if (Settings.Default.DrawingX != 0 && Settings.Default.DrawingY != 0)
1009
            {
1010
                Log.Write("Setting Drawing X, Drawing Y");
1011
                document.SetSPPIDLocation(Settings.Default.DrawingX, Settings.Default.DrawingY);
1012
                Log.Write("Start coordinate correction");
1013
                document.CoordinateCorrection();
1014
                return true;
1015
            }
1016
            else
1017
            {
1018
                Log.Write("Need Drawing X, Y");
1019
                return false;
1020
            }
1021
        }
1022

    
1023
        /// <summary>
1024
        /// 심볼을 실제로 Modeling 메서드
1025
        /// </summary>
1026
        /// <param name="symbol">생성할 심볼</param>
1027
        /// <param name="targetSymbol">연결되어 있는 심볼</param>
1028
        private void SymbolModeling(Symbol symbol, Symbol targetSymbol)
1029
        {
1030
            // OWNERSYMBOL Attribute, 값을 가지고 있을 경우
1031
            BaseModel.Attribute itemAttribute = symbol.ATTRIBUTES.Find(attr => attr.ATTRIBUTE == "OWNERSYMBOL");
1032
            if (itemAttribute != null && (string.IsNullOrEmpty(itemAttribute.VALUE) || itemAttribute.VALUE != "None"))
1033
                return;
1034
            // 이미 모델링 됐을 경우
1035
            else if (!string.IsNullOrEmpty(symbol.SPPID.RepresentationId))
1036
                return;
1037

    
1038
            LMSymbol _LMSymbol = null;
1039

    
1040
            string mappingPath = symbol.SPPID.MAPPINGNAME;
1041
            double x = symbol.SPPID.ORIGINAL_X;
1042
            double y = symbol.SPPID.ORIGINAL_Y;
1043
            int mirror = 0;
1044
            double angle = symbol.ANGLE;
1045

    
1046
            // OPC 일경우 180도 일때 Mirror
1047
            if (mappingPath.Contains("Piping OPC's") && angle == Math.PI)
1048
                mirror = 1;
1049

    
1050
            // Mirror 계산
1051
            if (symbol.FLIP == 1)
1052
            {
1053
                mirror = 1;
1054
                angle += Math.PI;
1055
            }
1056

    
1057
            if (targetSymbol != null && !string.IsNullOrEmpty(targetSymbol.SPPID.RepresentationId))
1058
            {
1059
                LMSymbol _TargetItem = dataSource.GetSymbol(targetSymbol.SPPID.RepresentationId);   /// RepresentationId로 SPPID 심볼을 찾음
1060
                Connector connector = SPPIDUtil.FindSymbolConnectorByUID(document, symbol.UID, targetSymbol);
1061
                if (connector != null)
1062
                    GetTargetSymbolConnectorPoint(connector, targetSymbol, ref x, ref y);
1063

    
1064
                LMConnector temp = LineModelingForSymbolZeroLength(symbol, _TargetItem, x, y);
1065
                _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle, TargetItem: _TargetItem);
1066
                if (temp != null)
1067
                    _placement.PIDRemovePlacement(temp.AsLMRepresentation());
1068
                ReleaseCOMObjects(temp);
1069
                temp = null;
1070

    
1071
                if (_LMSymbol != null && _TargetItem != null)
1072
                    symbol.SPPID.RepresentationId = _LMSymbol.AsLMRepresentation().Id;
1073

    
1074
                ReleaseCOMObjects(_TargetItem);
1075
            }
1076
            else
1077
                _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle);
1078

    
1079
            if (_LMSymbol != null)
1080
            {
1081
                _LMSymbol.Commit();
1082

    
1083
                // ConnCheck
1084
                List<string> ids = new List<string>();
1085
                foreach (LMConnector item in _LMSymbol.Connect1Connectors)
1086
                {
1087
                    if (item.get_ItemStatus() == "Active" && !ids.Contains(item.Id))
1088
                        ids.Add(item.Id);
1089
                    ReleaseCOMObjects(item);
1090
                }
1091
                foreach (LMConnector item in _LMSymbol.Connect2Connectors)
1092
                {
1093
                    if (item.get_ItemStatus() == "Active" && !ids.Contains(item.Id))
1094
                        ids.Add(item.Id);
1095
                    ReleaseCOMObjects(item);
1096
                }
1097

    
1098
                int createdSymbolCount = document.SYMBOLS.FindAll(i => i.CONNECTORS.Find(j => j.CONNECTEDITEM == symbol.UID) != null && !string.IsNullOrEmpty(i.SPPID.RepresentationId)).Count;
1099
                if (targetSymbol == null && ids.Count != createdSymbolCount)
1100
                {
1101
                    double currentX = _LMSymbol.get_XCoordinate();
1102
                    double currentY = _LMSymbol.get_YCoordinate();
1103

    
1104

    
1105
                }
1106

    
1107
                symbol.SPPID.RepresentationId = _LMSymbol.AsLMRepresentation().Id;
1108
                symbol.SPPID.ModelItemID = _LMSymbol.ModelItemID;
1109
                symbol.SPPID.GraphicOID = _LMSymbol.get_GraphicOID().ToString();
1110

    
1111
                foreach (var item in symbol.ChildSymbols)
1112
                    CreateChildSymbol(item, _LMSymbol, symbol);
1113

    
1114
                symbol.SPPID.SPPID_X = _LMSymbol.get_XCoordinate();
1115
                symbol.SPPID.SPPID_Y = _LMSymbol.get_YCoordinate();
1116

    
1117
                double[] range = null;
1118
                GetSPPIDSymbolRange(symbol, ref range);
1119
                symbol.SPPID.SPPID_Min_X = range[0];
1120
                symbol.SPPID.SPPID_Min_Y = range[1];
1121
                symbol.SPPID.SPPID_Max_X = range[2];
1122
                symbol.SPPID.SPPID_Max_Y = range[3];
1123

    
1124
                foreach (var item in symbol.SPPID.CorrectionX_GroupSymbols)
1125
                    item.SPPID.ORIGINAL_X = symbol.SPPID.SPPID_X;
1126
                foreach (var item in symbol.SPPID.CorrectionY_GroupSymbols)
1127
                    item.SPPID.ORIGINAL_Y = symbol.SPPID.SPPID_Y;
1128

    
1129
                ReleaseCOMObjects(_LMSymbol);
1130
            }
1131
        }
1132
        /// <summary>
1133
        /// targetX와 targetY 기준 제일 먼 PipingPoint에 TempLine Modeling
1134
        /// Signal Point는 고려하지 않음
1135
        /// </summary>
1136
        /// <param name="symbol"></param>
1137
        /// <param name="_TargetItem"></param>
1138
        /// <param name="targetX"></param>
1139
        /// <param name="targetY"></param>
1140
        /// <returns></returns>
1141
        private LMConnector LineModelingForSymbolZeroLength(Symbol symbol, LMSymbol _TargetItem, double targetX, double targetY)
1142
        {
1143
            LMConnector tempConnector = null;
1144

    
1145
            List<Symbol> group = new List<Symbol>();
1146
            SPPIDUtil.FindConnectedSymbolGroup(document, symbol, group);
1147
            if (group.FindAll(loopX => !string.IsNullOrEmpty(loopX.SPPID.RepresentationId)).Count == 1)
1148
            {
1149
                List<Connector> connectors = new List<Connector>();
1150
                foreach (var item in group)
1151
                    connectors.AddRange(item.CONNECTORS.FindAll(loopX => loopX.ConnectedObject != null && loopX.ConnectedObject.GetType() == typeof(Line)));
1152
                /// Primary or Secondary Type Line만 고려
1153
                Connector _connector = connectors.Find(loopX => loopX.ConnectedObject != null && loopX.ConnectedObject.GetType() == typeof(Line) &&
1154
                (((Line)loopX.ConnectedObject).TYPE == "Primary" || ((Line)loopX.ConnectedObject).TYPE == "Secondary"));
1155
                if (_connector != null)
1156
                {
1157
                    string sppidLine = ((Line)_connector.ConnectedObject).SPPID.MAPPINGNAME;
1158
                    List<double[]> pointInfos = getPipingPoints(_TargetItem);
1159
                    /// PipingPoint가 2개 이상만
1160
                    if (pointInfos.Count >= 2)
1161
                    {
1162
                        double lineX = 0;
1163
                        double lineY = 0;
1164
                        double length = 0;
1165
                        foreach (var item in pointInfos)
1166
                        {
1167
                            double tempX = item[1];
1168
                            double tempY = item[2];
1169

    
1170
                            double calcDistance = SPPIDUtil.CalcPointToPointdDistance(targetX, targetY, tempX, tempY);
1171
                            if (calcDistance > length)
1172
                            {
1173
                                lineX = tempX;
1174
                                lineY = tempY;
1175
                            }
1176
                        }
1177

    
1178
                        _LMAItem _LMAItem = _placement.PIDCreateItem(sppidLine);
1179
                        PlaceRunInputs placeRunInputs = new PlaceRunInputs();
1180
                        placeRunInputs.AddPoint(-1, -1);
1181
                        placeRunInputs.AddSymbolTarget(_TargetItem, lineX, lineY);
1182
                        tempConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
1183
                        if (tempConnector != null)
1184
                            tempConnector.Commit();
1185
                        ReleaseCOMObjects(_LMAItem);
1186
                        _LMAItem = null;
1187
                        ReleaseCOMObjects(placeRunInputs);
1188
                        placeRunInputs = null;
1189
                    }
1190
                }
1191
            }
1192

    
1193
            return tempConnector;
1194
        }
1195
        /// <summary>
1196
        /// Symbol의 PipingPoints를 구함
1197
        /// SignalPoint는 고려하지 않음
1198
        /// </summary>
1199
        /// <param name="symbol"></param>
1200
        /// <returns></returns>
1201
        private List<double[]> getPipingPoints(LMSymbol symbol)
1202
        {
1203
            LMModelItem modelItem = symbol.ModelItemObject;
1204
            LMPipingPoints pipingPoints = null;
1205
            if (modelItem.get_ItemTypeName() == "PipingComp")
1206
            {
1207
                LMPipingComp pipingComp = dataSource.GetPipingComp(modelItem.Id);
1208
                pipingPoints = pipingComp.PipingPoints;
1209
                ReleaseCOMObjects(pipingComp);
1210
                pipingComp = null;
1211
            }
1212
            else if (modelItem.get_ItemTypeName() == "Instrument")
1213
            {
1214
                LMInstrument instrument = dataSource.GetInstrument(modelItem.Id);
1215
                pipingPoints = instrument.PipingPoints;
1216
                ReleaseCOMObjects(instrument);
1217
                instrument = null;
1218
            }
1219
            else
1220
                Log.Write("다른 Type");
1221

    
1222
            List<double[]> info = new List<double[]>();
1223
            if (pipingPoints != null)
1224
            {
1225
                foreach (LMPipingPoint pipingPoint in pipingPoints)
1226
                {
1227
                    foreach (LMAAttribute attribute in pipingPoint.Attributes)
1228
                    {
1229
                        if (attribute.Name == "PipingPointNumber")
1230
                        {
1231
                            int index = Convert.ToInt32(attribute.get_Value());
1232
                            if (info.Find(loopX => loopX[0] == index) == null)
1233
                            {
1234
                                double x = 0;
1235
                                double y = 0;
1236
                                if (_placement.PIDConnectPointLocation(symbol, index, ref x, ref y))
1237
                                    info.Add(new double[] { index, x, y });
1238
                            }
1239
                        }
1240
                    }
1241
                }
1242
            }
1243
            ReleaseCOMObjects(modelItem);
1244
            modelItem = null;
1245
            ReleaseCOMObjects(pipingPoints);
1246
            pipingPoints = null;
1247

    
1248
            return info;
1249
        }
1250

    
1251
        private void RemoveSymbol(Symbol symbol)
1252
        {
1253
            if (!string.IsNullOrEmpty(symbol.SPPID.RepresentationId))
1254
            {
1255
                LMSymbol _LMSymbol = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
1256
                if (_LMSymbol != null)
1257
                {
1258
                    _placement.PIDRemovePlacement(_LMSymbol.AsLMRepresentation());
1259
                    ReleaseCOMObjects(_LMSymbol);
1260
                }
1261
            }
1262

    
1263
            symbol.SPPID.RepresentationId = string.Empty;
1264
            symbol.SPPID.ModelItemID = string.Empty;
1265
            symbol.SPPID.SPPID_X = double.NaN;
1266
            symbol.SPPID.SPPID_Y = double.NaN;
1267
            symbol.SPPID.SPPID_Min_X = double.NaN;
1268
            symbol.SPPID.SPPID_Min_Y = double.NaN;
1269
            symbol.SPPID.SPPID_Max_X = double.NaN;
1270
            symbol.SPPID.SPPID_Max_Y = double.NaN;
1271
        }
1272

    
1273
        private void RemoveSymbol(List<Symbol> symbols)
1274
        {
1275
            foreach (var symbol in symbols)
1276
            {
1277
                if (!string.IsNullOrEmpty(symbol.SPPID.RepresentationId))
1278
                {
1279
                    LMSymbol _LMSymbol = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
1280
                    if (_LMSymbol != null)
1281
                    {
1282
                        _placement.PIDRemovePlacement(_LMSymbol.AsLMRepresentation());
1283
                        ReleaseCOMObjects(_LMSymbol);
1284
                    }
1285
                }
1286

    
1287
                symbol.SPPID.RepresentationId = string.Empty;
1288
                symbol.SPPID.ModelItemID = string.Empty;
1289
                symbol.SPPID.SPPID_X = double.NaN;
1290
                symbol.SPPID.SPPID_Y = double.NaN;
1291
                symbol.SPPID.SPPID_Min_X = double.NaN;
1292
                symbol.SPPID.SPPID_Min_Y = double.NaN;
1293
                symbol.SPPID.SPPID_Max_X = double.NaN;
1294
                symbol.SPPID.SPPID_Max_Y = double.NaN;
1295
            }
1296
        }
1297

    
1298
        /// <summary>
1299
        /// ID2의 Symbol Width와 Height를 비교해서 상대적인 SPPID Connector좌표를 가져온다.
1300
        /// </summary>
1301
        /// <param name="targetConnector"></param>
1302
        /// <param name="targetSymbol"></param>
1303
        /// <param name="x"></param>
1304
        /// <param name="y"></param>
1305
        private void GetTargetSymbolConnectorPoint(Connector targetConnector, Symbol targetSymbol, ref double x, ref double y)
1306
        {
1307
            LMSymbol _TargetItem = dataSource.GetSymbol(targetSymbol.SPPID.RepresentationId);
1308

    
1309
            double[] range = null;
1310
            List<double[]> points = new List<double[]>();
1311
            GetSPPIDSymbolRangeAndConnectionPoints(targetSymbol, ref range, points);
1312
            double x1 = range[0];
1313
            double y1 = range[1];
1314
            double x2 = range[2];
1315
            double y2 = range[3];
1316

    
1317
            // Origin 기준 Connector의 위치차이
1318
            double sceneX = 0;
1319
            double sceneY = 0;
1320
            SPPIDUtil.ConvertPointBystring(targetConnector.SCENECONNECTPOINT, ref sceneX, ref sceneY);
1321
            double originX = 0;
1322
            double originY = 0;
1323
            SPPIDUtil.ConvertPointBystring(targetSymbol.ORIGINALPOINT, ref originX, ref originY);
1324
            double gapX = originX - sceneX;
1325
            double gapY = originY - sceneY;
1326

    
1327
            // SPPID Symbol과 ID2 심볼의 크기 차이
1328
            double sizeWidth = 0;
1329
            double sizeHeight = 0;
1330
            SPPIDUtil.ConvertPointBystring(targetSymbol.SIZE, ref sizeWidth, ref sizeHeight);
1331
            if (sizeWidth == 0 || sizeHeight == 0)
1332
                throw new Exception("Check symbol size! \r\nUID : " + targetSymbol.UID);
1333

    
1334
            double percentX = (x2 - x1) / sizeWidth;
1335
            double percentY = (y2 - y1) / sizeHeight;
1336

    
1337
            double SPPIDgapX = gapX * percentX;
1338
            double SPPIDgapY = gapY * percentY;
1339

    
1340
            double[] SPPIDOriginPoint = new double[] { _TargetItem.get_XCoordinate() - SPPIDgapX, _TargetItem.get_YCoordinate() + SPPIDgapY };
1341
            double distance = double.MaxValue;
1342
            double[] resultPoint;
1343
            foreach (var point in points)
1344
            {
1345
                double result = SPPIDUtil.CalcPointToPointdDistance(point[0], point[1], SPPIDOriginPoint[0], SPPIDOriginPoint[1]);
1346
                if (distance > result)
1347
                {
1348
                    distance = result;
1349
                    resultPoint = point;
1350
                    x = point[0];
1351
                    y = point[1];
1352
                }
1353
            }
1354

    
1355
            ReleaseCOMObjects(_TargetItem);
1356
        }
1357

    
1358
        private void GetTargetLineConnectorPoint(Connector targetConnector, Line targetLine, ref double x, ref double y)
1359
        {
1360
            int index = targetLine.CONNECTORS.IndexOf(targetConnector);
1361
            if (index == 0)
1362
            {
1363
                x = targetLine.SPPID.START_X;
1364
                y = targetLine.SPPID.START_Y;
1365
            }
1366
            else
1367
            {
1368
                x = targetLine.SPPID.END_X;
1369
                y = targetLine.SPPID.END_Y;
1370
            }
1371
        }
1372

    
1373
        /// <summary>
1374
        /// SPPID Symbol의 Range를 구한다.
1375
        /// </summary>
1376
        /// <param name="symbol"></param>
1377
        /// <param name="range"></param>
1378
        private void GetSPPIDSymbolRangeAndConnectionPoints(Symbol symbol, ref double[] range, List<double[]> points)
1379
        {
1380
            LMSymbol _TargetItem = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
1381
            Ingr.RAD2D.Symbol2d symbol2d = radApp.ActiveDocument.ActiveSheet.DrawingObjects[_TargetItem.get_GraphicOID().ToString()];
1382
            double x1 = 0;
1383
            double y1 = 0;
1384
            double x2 = 0;
1385
            double y2 = 0;
1386
            symbol2d.Range(out x1, out y1, out x2, out y2);
1387
            range = new double[] { x1, y1, x2, y2 };
1388

    
1389
            for (int i = 1; i < int.MaxValue; i++)
1390
            {
1391
                double connX = 0;
1392
                double connY = 0;
1393
                if (_placement.PIDConnectPointLocation(_TargetItem, i, ref connX, ref connY))
1394
                    points.Add(new double[] { connX, connY });
1395
                else
1396
                    break;
1397
            }
1398

    
1399
            foreach (var childSymbol in symbol.ChildSymbols)
1400
                GetSPPIDChildSymbolRange(childSymbol, ref range, points);
1401

    
1402
            ReleaseCOMObjects(_TargetItem);
1403
        }
1404

    
1405
        private void GetSPPIDSymbolRange(Symbol symbol, ref double[] range, bool bOnlySymbol = false, bool bForGraphic = false)
1406
        {
1407
            LMSymbol _TargetItem = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
1408
            if (_TargetItem != null)
1409
            {
1410
                Ingr.RAD2D.Symbol2d symbol2d = radApp.ActiveDocument.ActiveSheet.DrawingObjects[_TargetItem.get_GraphicOID().ToString()];
1411
                double x1 = 0;
1412
                double y1 = 0;
1413
                double x2 = 0;
1414
                double y2 = 0;
1415
                if (!bForGraphic)
1416
                {
1417
                    symbol2d.Range(out x1, out y1, out x2, out y2);
1418
                    range = new double[] { x1, y1, x2, y2 };
1419
                }
1420
                else
1421
                {
1422
                    x1 = double.MaxValue;
1423
                    y1 = double.MaxValue;
1424
                    x2 = double.MinValue;
1425
                    y2 = double.MinValue;
1426
                    range = new double[] { x1, y1, x2, y2 };
1427

    
1428
                    foreach (var item in symbol2d.DrawingObjects)
1429
                    {
1430
                        if (item.GetType() == typeof(Ingr.RAD2D.Line2d))
1431
                        {
1432
                            Ingr.RAD2D.Line2d rangeObject = item as Ingr.RAD2D.Line2d;
1433
                            if (rangeObject.Layer == "Default")
1434
                            {
1435
                                rangeObject.Range(out x1, out y1, out x2, out y2);
1436
                                range = new double[] {
1437
                                Math.Min(x1, range[0]),
1438
                                Math.Min(y1, range[1]),
1439
                                Math.Max(x2, range[2]),
1440
                                Math.Max(y2, range[3])
1441
                            };
1442
                            }
1443
                        }
1444
                        else if (item.GetType() == typeof(Ingr.RAD2D.Circle2d))
1445
                        {
1446
                            Ingr.RAD2D.Circle2d rangeObject = item as Ingr.RAD2D.Circle2d;
1447
                            if (rangeObject.Layer == "Default")
1448
                            {
1449
                                rangeObject.Range(out x1, out y1, out x2, out y2);
1450
                                range = new double[] {
1451
                                Math.Min(x1, range[0]),
1452
                                Math.Min(y1, range[1]),
1453
                                Math.Max(x2, range[2]),
1454
                                Math.Max(y2, range[3])
1455
                            };
1456
                            }
1457
                        }
1458
                        else if (item.GetType() == typeof(Ingr.RAD2D.Rectangle2d))
1459
                        {
1460
                            Ingr.RAD2D.Rectangle2d rangeObject = item as Ingr.RAD2D.Rectangle2d;
1461
                            if (rangeObject.Layer == "Default")
1462
                            {
1463
                                rangeObject.Range(out x1, out y1, out x2, out y2);
1464
                                range = new double[] {
1465
                                Math.Min(x1, range[0]),
1466
                                Math.Min(y1, range[1]),
1467
                                Math.Max(x2, range[2]),
1468
                                Math.Max(y2, range[3])
1469
                            };
1470
                            }
1471
                        }
1472
                        else if (item.GetType() == typeof(Ingr.RAD2D.Arc2d))
1473
                        {
1474
                            Ingr.RAD2D.Arc2d rangeObject = item as Ingr.RAD2D.Arc2d;
1475
                            if (rangeObject.Layer == "Default")
1476
                            {
1477
                                rangeObject.Range(out x1, out y1, out x2, out y2);
1478
                                range = new double[] {
1479
                                Math.Min(x1, range[0]),
1480
                                Math.Min(y1, range[1]),
1481
                                Math.Max(x2, range[2]),
1482
                                Math.Max(y2, range[3])
1483
                            };
1484
                            }
1485
                        }
1486
                    }
1487
                }
1488

    
1489
                if (!bOnlySymbol)
1490
                {
1491
                    foreach (var childSymbol in symbol.ChildSymbols)
1492
                        GetSPPIDChildSymbolRange(childSymbol, ref range);
1493
                }
1494
                ReleaseCOMObjects(_TargetItem);
1495
            }
1496
        }
1497

    
1498
        private void GetSPPIDSymbolRange(LMLabelPersist labelPersist, ref double[] range)
1499
        {
1500
            if (labelPersist != null)
1501
            {
1502
                double x1 = double.MaxValue;
1503
                double y1 = double.MaxValue;
1504
                double x2 = double.MinValue;
1505
                double y2 = double.MinValue;
1506
                range = new double[] { x1, y1, x2, y2 };
1507

    
1508
                Ingr.RAD2D.DependencyObject dependency = radApp.ActiveDocument.ActiveSheet.DrawingObjects[labelPersist.get_GraphicOID().ToString()] as DependencyObject;
1509
                foreach (var item in dependency.DrawingObjects)
1510
                {
1511
                    Ingr.RAD2D.TextBox textBox = item as Ingr.RAD2D.TextBox;
1512
                    if (textBox != null)
1513
                    {
1514
                        if (dependency != null)
1515
                        {
1516
                            double tempX1;
1517
                            double tempY1;
1518
                            double tempX2;
1519
                            double tempY2;
1520
                            textBox.Range(out tempX1, out tempY1, out tempX2, out tempY2);
1521
                            x1 = Math.Min(x1, tempX1);
1522
                            y1 = Math.Min(y1, tempY1);
1523
                            x2 = Math.Max(x2, tempX2);
1524
                            y2 = Math.Max(y2, tempY2);
1525

    
1526
                            range = new double[] { x1, y1, x2, y2 };
1527
                        }
1528
                    }
1529
                }
1530
                
1531
            }
1532
        }
1533

    
1534
        private void GetSPPIDSymbolRange(List<Symbol> symbols, ref double[] range)
1535
        {
1536
            double[] tempRange = new double[] { double.MaxValue, double.MaxValue, double.MinValue, double.MinValue };
1537
            foreach (var symbol in symbols)
1538
            {
1539
                LMSymbol _TargetItem = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
1540
                Ingr.RAD2D.Symbol2d symbol2d = radApp.ActiveDocument.ActiveSheet.DrawingObjects[_TargetItem.get_GraphicOID().ToString()];
1541
                double x1 = 0;
1542
                double y1 = 0;
1543
                double x2 = 0;
1544
                double y2 = 0;
1545
                symbol2d.Range(out x1, out y1, out x2, out y2);
1546

    
1547
                tempRange[0] = Math.Min(tempRange[0], x1);
1548
                tempRange[1] = Math.Min(tempRange[1], y1);
1549
                tempRange[2] = Math.Max(tempRange[2], x2);
1550
                tempRange[3] = Math.Max(tempRange[3], y2);
1551

    
1552
                foreach (var childSymbol in symbol.ChildSymbols)
1553
                    GetSPPIDChildSymbolRange(childSymbol, ref tempRange);
1554

    
1555
                ReleaseCOMObjects(_TargetItem);
1556
            }
1557

    
1558
            range = tempRange;
1559
        }
1560

    
1561
        /// <summary>
1562
        /// Child Modeling 된 Symbol의 Range를 구한다.
1563
        /// </summary>
1564
        /// <param name="childSymbol"></param>
1565
        /// <param name="range"></param>
1566
        private void GetSPPIDChildSymbolRange(ChildSymbol childSymbol, ref double[] range, List<double[]> points)
1567
        {
1568
            LMSymbol _ChildSymbol = dataSource.GetSymbol(childSymbol.SPPID.RepresentationId);
1569
            if (_ChildSymbol != null)
1570
            {
1571
                Ingr.RAD2D.Symbol2d symbol2d = radApp.ActiveDocument.ActiveSheet.DrawingObjects[_ChildSymbol.get_GraphicOID().ToString()];
1572
                double x1 = 0;
1573
                double y1 = 0;
1574
                double x2 = 0;
1575
                double y2 = 0;
1576
                symbol2d.Range(out x1, out y1, out x2, out y2);
1577
                range[0] = Math.Min(range[0], x1);
1578
                range[1] = Math.Min(range[1], y1);
1579
                range[2] = Math.Max(range[2], x2);
1580
                range[3] = Math.Max(range[3], y2);
1581

    
1582
                for (int i = 1; i < int.MaxValue; i++)
1583
                {
1584
                    double connX = 0;
1585
                    double connY = 0;
1586
                    if (_placement.PIDConnectPointLocation(_ChildSymbol, i, ref connX, ref connY))
1587
                        points.Add(new double[] { connX, connY });
1588
                    else
1589
                        break;
1590
                }
1591

    
1592
                foreach (var loopChildSymbol in childSymbol.ChildSymbols)
1593
                    GetSPPIDChildSymbolRange(loopChildSymbol, ref range, points);
1594

    
1595
                ReleaseCOMObjects(_ChildSymbol);
1596
            }
1597
        }
1598

    
1599
        private void GetSPPIDChildSymbolRange(ChildSymbol childSymbol, ref double[] range)
1600
        {
1601
            LMSymbol _ChildSymbol = dataSource.GetSymbol(childSymbol.SPPID.RepresentationId);
1602
            if (_ChildSymbol != null)
1603
            {
1604
                Ingr.RAD2D.Symbol2d symbol2d = radApp.ActiveDocument.ActiveSheet.DrawingObjects[_ChildSymbol.get_GraphicOID().ToString()];
1605
                double x1 = 0;
1606
                double y1 = 0;
1607
                double x2 = 0;
1608
                double y2 = 0;
1609
                symbol2d.Range(out x1, out y1, out x2, out y2);
1610
                range[0] = Math.Min(range[0], x1);
1611
                range[1] = Math.Min(range[1], y1);
1612
                range[2] = Math.Max(range[2], x2);
1613
                range[3] = Math.Max(range[3], y2);
1614

    
1615
                foreach (var loopChildSymbol in childSymbol.ChildSymbols)
1616
                    GetSPPIDChildSymbolRange(loopChildSymbol, ref range);
1617
                ReleaseCOMObjects(_ChildSymbol);
1618
            }
1619
        }
1620

    
1621
        /// <summary>
1622
        /// Label Symbol Modeling
1623
        /// </summary>
1624
        /// <param name="symbol"></param>
1625
        private void LabelSymbolModeling(Symbol symbol)
1626
        {
1627
            if (string.IsNullOrEmpty(symbol.SPPID.RepresentationId))
1628
            {
1629
                BaseModel.Attribute itemAttribute = symbol.ATTRIBUTES.Find(x => x.ATTRIBUTE == "OWNERSYMBOL");
1630
                if (itemAttribute == null || string.IsNullOrEmpty(itemAttribute.VALUE) || itemAttribute.VALUE == "None")
1631
                    return;
1632
                Array points = new double[] { 0, symbol.SPPID.ORIGINAL_X, symbol.SPPID.ORIGINAL_Y };
1633

    
1634
                string symbolUID = itemAttribute.VALUE;
1635
                object targetItem = SPPIDUtil.FindObjectByUID(document, symbolUID);
1636
                if (targetItem != null &&
1637
                    (targetItem.GetType() == typeof(Symbol) ||
1638
                    targetItem.GetType() == typeof(Equipment)))
1639
                {
1640
                    // Object 아이템이 Symbol일 경우 Equipment일 경우 
1641
                    string sRep = null;
1642
                    if (targetItem.GetType() == typeof(Symbol))
1643
                        sRep = ((Symbol)targetItem).SPPID.RepresentationId;
1644
                    else if (targetItem.GetType() == typeof(Equipment))
1645
                        sRep = ((Equipment)targetItem).SPPID.RepresentationId;
1646
                    if (!string.IsNullOrEmpty(sRep))
1647
                    {
1648
                        // LEADER Line 검사
1649
                        bool leaderLine = false;
1650
                        SymbolMapping symbolMapping = document.SymbolMappings.Find(x => x.UID == symbol.DBUID);
1651
                        if (symbolMapping != null)
1652
                            leaderLine = symbolMapping.LEADERLINE;
1653

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

    
1658
                        //Leader 선 센터로
1659
                        if (_LMLabelPresist != null)
1660
                        {
1661
                            // Target Item에 Label의 Attribute Input
1662
                            InputSymbolAttribute(targetItem, symbol.ATTRIBUTES);
1663

    
1664
                            string OID = _LMLabelPresist.get_GraphicOID().ToString();
1665
                            DependencyObject dependency = radApp.ActiveDocument.ActiveSheet.DrawingObjects[OID] as DependencyObject;
1666
                            if (dependency != null)
1667
                            {
1668
                                bool result = false;
1669
                                foreach (var attributes in dependency.AttributeSets)
1670
                                {
1671
                                    foreach (var attribute in attributes)
1672
                                    {
1673
                                        string name = attribute.Name;
1674
                                        string value = attribute.GetValue().ToString();
1675
                                        if (name == "DrawingItemType" && value == "LabelPersist")
1676
                                        {
1677
                                            foreach (DrawingObjectBase drawingObject in dependency.DrawingObjects)
1678
                                            {
1679
                                                if (drawingObject.Type == Ingr.RAD2D.ObjectType.igLineString2d)
1680
                                                {
1681
                                                    Ingr.RAD2D.LineString2d lineString2D = drawingObject as Ingr.RAD2D.LineString2d;
1682
                                                    double prevX = _TargetItem.get_XCoordinate();
1683
                                                    double prevY = _TargetItem.get_YCoordinate();
1684
                                                    lineString2D.InsertVertex(lineString2D.VertexCount, prevX, prevY);
1685
                                                    lineString2D.RemoveVertex(lineString2D.VertexCount);
1686
                                                    result = true;
1687
                                                    break;
1688
                                                }
1689
                                            }
1690
                                        }
1691

    
1692
                                        if (result)
1693
                                            break;
1694
                                    }
1695

    
1696
                                    if (result)
1697
                                        break;
1698
                                }
1699
                            }
1700

    
1701
                            symbol.SPPID.RepresentationId = _LMLabelPresist.AsLMRepresentation().Id;
1702
                            _LMLabelPresist.Commit();
1703
                            ReleaseCOMObjects(_LMLabelPresist);
1704
                        }
1705

    
1706
                        ReleaseCOMObjects(_TargetItem);
1707
                    }
1708
                }
1709
                else if (targetItem != null && targetItem.GetType() == typeof(Line))
1710
                {
1711
                    Line targetLine = targetItem as Line;
1712
                    Dictionary<LMConnector, List<double[]>> connectorVertices = GetPipeRunVertices(targetLine.SPPID.ModelItemId);
1713
                    LMConnector connectedLMConnector = FindTargetLMConnectorForLabel(connectorVertices, symbol.SPPID.ORIGINAL_X, symbol.SPPID.ORIGINAL_Y);
1714
                    if (connectedLMConnector != null)
1715
                    {
1716
                        // LEADER Line 검사
1717
                        bool leaderLine = false;
1718
                        SymbolMapping symbolMapping = document.SymbolMappings.Find(x => x.UID == symbol.DBUID);
1719
                        if (symbolMapping != null)
1720
                            leaderLine = symbolMapping.LEADERLINE;
1721

    
1722
                        LMLabelPersist _LMLabelPresist = _placement.PIDPlaceLabel(symbol.SPPID.MAPPINGNAME, ref points, Rotation: 0, LabeledItem: connectedLMConnector.AsLMRepresentation(), IsLeaderVisible: leaderLine);
1723
                        if (_LMLabelPresist != null)
1724
                        {
1725
                            symbol.SPPID.RepresentationId = _LMLabelPresist.AsLMRepresentation().Id;
1726
                            _LMLabelPresist.Commit();
1727
                            ReleaseCOMObjects(_LMLabelPresist);
1728
                        }
1729
                        ReleaseCOMObjects(connectedLMConnector);
1730
                    }
1731

    
1732
                    foreach (var item in connectorVertices)
1733
                        if (item.Key != null)
1734
                            ReleaseCOMObjects(item.Key);
1735
                }
1736
            }
1737
        }
1738

    
1739
        /// <summary>
1740
        /// Equipment를 실제로 Modeling 메서드
1741
        /// </summary>
1742
        /// <param name="equipment"></param>
1743
        private void EquipmentModeling(Equipment equipment)
1744
        {
1745
            if (!string.IsNullOrEmpty(equipment.SPPID.RepresentationId))
1746
                return;
1747

    
1748
            LMSymbol _LMSymbol = null;
1749
            LMSymbol targetItem = null;
1750
            string mappingPath = equipment.SPPID.MAPPINGNAME;
1751
            double x = equipment.SPPID.ORIGINAL_X;
1752
            double y = equipment.SPPID.ORIGINAL_Y;
1753
            int mirror = 0;
1754
            double angle = equipment.ANGLE;
1755

    
1756
            SPPIDUtil.ConvertGridPoint(ref x, ref y);
1757

    
1758
            Connector connector = equipment.CONNECTORS.Find(conn => !string.IsNullOrEmpty(conn.CONNECTEDITEM) && conn.CONNECTEDITEM != "None");
1759
            if (connector != null)
1760
            {
1761
                Equipment connEquipment = SPPIDUtil.FindObjectByUID(document, connector.CONNECTEDITEM) as Equipment;
1762
                VendorPackage connVendorPackage = SPPIDUtil.FindObjectByUID(document, connector.CONNECTEDITEM) as VendorPackage;
1763
                if (connEquipment != null)
1764
                {
1765
                    if (string.IsNullOrEmpty(connEquipment.SPPID.RepresentationId))
1766
                        EquipmentModeling(connEquipment);
1767

    
1768
                    if (!string.IsNullOrEmpty(connEquipment.SPPID.RepresentationId))
1769
                    {
1770
                        targetItem = dataSource.GetSymbol(connEquipment.SPPID.RepresentationId);
1771
                        if (targetItem != null)
1772
                            _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle, TargetItem: targetItem);
1773
                        else
1774
                            _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle);
1775
                    }
1776
                    else
1777
                    {
1778
                        _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle);
1779
                    }
1780
                }
1781
                else if (connVendorPackage != null)
1782
                {
1783
                    if (!string.IsNullOrEmpty(connVendorPackage.SPPID.RepresentationId))
1784
                    {
1785
                        targetItem = dataSource.GetSymbol(connVendorPackage.SPPID.RepresentationId);
1786
                        if (targetItem != null)
1787
                            _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle, TargetItem: targetItem);
1788
                        else
1789
                            _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle);
1790
                    }
1791
                }
1792
                else
1793
                {
1794
                    _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle);
1795
                }
1796
            }
1797
            else
1798
            {
1799
                _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle);
1800
            }
1801

    
1802
            if (_LMSymbol != null)
1803
            {
1804
                _LMSymbol.Commit();
1805
                equipment.SPPID.RepresentationId = _LMSymbol.AsLMRepresentation().Id;
1806
                equipment.SPPID.GraphicOID = _LMSymbol.get_GraphicOID().ToString();
1807
                ReleaseCOMObjects(_LMSymbol);
1808
            }
1809

    
1810
            if (targetItem != null)
1811
            {
1812
                ReleaseCOMObjects(targetItem);
1813
            }
1814

    
1815
            ReleaseCOMObjects(_LMSymbol);
1816
        }
1817

    
1818
        private void VendorPackageModeling(VendorPackage vendorPackage)
1819
        {
1820
            ETCSetting setting = ETCSetting.GetInstance();
1821
            if (!string.IsNullOrEmpty(setting.VendorPackageSymbolPath))
1822
            {
1823
                string symbolPath = setting.VendorPackageSymbolPath;
1824
                double x = vendorPackage.SPPID.ORIGINAL_X;
1825
                double y = vendorPackage.SPPID.ORIGINAL_Y;
1826

    
1827
                LMSymbol symbol = _placement.PIDPlaceSymbol(symbolPath, x, y);
1828
                if (symbol != null)
1829
                {
1830
                    symbol.Commit();
1831
                    vendorPackage.SPPID.RepresentationId = symbol.AsLMRepresentation().Id;
1832
                }
1833

    
1834
                ReleaseCOMObjects(symbol);
1835
                symbol = null;
1836
            }
1837
        }
1838

    
1839
        /// <summary>
1840
        /// 첫 진입점
1841
        /// </summary>
1842
        /// <param name="symbol"></param>
1843
        private void SymbolModelingBySymbol(Symbol symbol)
1844
        {
1845
            SymbolModeling(symbol, null);   /// 심볼을 생성한다
1846
            List<object> endObjects = new List<object>();
1847
            endObjects.Add(symbol);
1848

    
1849
            /// 심볼에 연결되어 있는 항목들을 모델링한다
1850
            foreach (var connector in symbol.CONNECTORS)
1851
            {
1852
                object connItem = SPPIDUtil.FindObjectByUID(document, connector.CONNECTEDITEM);
1853
                if (connItem != null && connItem.GetType() != typeof(Equipment))
1854
                {
1855
                    endObjects.Add(connItem);
1856
                    if (connItem.GetType() == typeof(Symbol))
1857
                    {
1858
                        Symbol connSymbol = connItem as Symbol;
1859
                        if (string.IsNullOrEmpty(connSymbol.SPPID.RepresentationId))
1860
                        {
1861
                            SymbolModeling(connSymbol, symbol);
1862
                        }
1863
                        SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetProgress, document.SYMBOLS.FindAll(x => !string.IsNullOrEmpty(x.SPPID.RepresentationId)).Count);
1864
                        SymbolModelingByNeerSymbolLoop(connSymbol, endObjects);
1865
                    }
1866
                    else if (connItem.GetType() == typeof(Line))
1867
                    {
1868
                        Line connLine = connItem as Line;
1869
                        SymbolModelingByNeerLineLoop(connLine, endObjects, symbol);
1870
                    }
1871
                }
1872
            }
1873
        }
1874

    
1875
        private void SymbolModelingByNeerSymbolLoop(Symbol symbol, List<object> endObjects)
1876
        {
1877
            foreach (var connector in symbol.CONNECTORS)
1878
            {
1879
                object connItem = SPPIDUtil.FindObjectByUID(document, connector.CONNECTEDITEM);
1880
                if (connItem != null && connItem.GetType() != typeof(Equipment))
1881
                {
1882
                    if (!endObjects.Contains(connItem))
1883
                    {
1884
                        endObjects.Add(connItem);
1885
                        if (connItem.GetType() == typeof(Symbol))
1886
                        {
1887
                            Symbol connSymbol = connItem as Symbol;
1888
                            if (string.IsNullOrEmpty(connSymbol.SPPID.RepresentationId))
1889
                            {
1890
                                SymbolModeling(connSymbol, symbol);
1891
                            }
1892
                            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetProgress, document.SYMBOLS.FindAll(x => !string.IsNullOrEmpty(x.SPPID.RepresentationId)).Count);
1893
                            SymbolModelingByNeerSymbolLoop(connSymbol, endObjects);
1894
                        }
1895
                        else if (connItem.GetType() == typeof(Line))
1896
                        {
1897
                            Line connLine = connItem as Line;
1898
                            SymbolModelingByNeerLineLoop(connLine, endObjects, symbol);
1899
                        }
1900
                    }
1901
                }
1902
            }
1903
        }
1904

    
1905
        private void SymbolModelingByNeerLineLoop(Line line, List<object> endObjects, Symbol prevSymbol)
1906
        {
1907
            foreach (var connector in line.CONNECTORS)
1908
            {
1909
                object connItem = SPPIDUtil.FindObjectByUID(document, connector.CONNECTEDITEM);
1910
                if (connItem != null && connItem.GetType() != typeof(Equipment))
1911
                {
1912
                    if (!endObjects.Contains(connItem))
1913
                    {
1914
                        endObjects.Add(connItem);
1915
                        if (connItem.GetType() == typeof(Symbol))
1916
                        {
1917
                            Symbol connSymbol = connItem as Symbol;
1918
                            if (string.IsNullOrEmpty(connSymbol.SPPID.RepresentationId))
1919
                            {
1920
                                Line connLine = SPPIDUtil.GetConnectedLine(prevSymbol, connSymbol);
1921
                                int branchCount = 0;
1922
                                if (connLine != null)
1923
                                    branchCount = SPPIDUtil.GetBranchLineCount(document, connLine);
1924

    
1925
                                List<Symbol> group = new List<Symbol>();
1926
                                SPPIDUtil.FindConnectedSymbolGroup(document, connSymbol, group);
1927
                                Symbol priority = prioritySymbols.Find(x => group.Contains(x));
1928
                                List<Symbol> endModelingGroup = new List<Symbol>();
1929
                                if (priority != null)
1930
                                {
1931
                                    SymbolGroupModeling(priority, group);
1932

    
1933
                                    // Range 겹치는지 확인해야함
1934
                                    double[] prevRange = null;
1935
                                    GetSPPIDSymbolRange(prevSymbol, ref prevRange);
1936
                                    double[] groupRange = null;
1937
                                    GetSPPIDSymbolRange(group, ref groupRange);
1938

    
1939
                                    double distanceX = 0;
1940
                                    double distanceY = 0;
1941
                                    bool overlapX = false;
1942
                                    bool overlapY = false;
1943
                                    SlopeType slopeType = SPPIDUtil.CalcSlope(prevSymbol.SPPID.ORIGINAL_X, prevSymbol.SPPID.ORIGINAL_Y, connSymbol.SPPID.ORIGINAL_X, connSymbol.SPPID.ORIGINAL_Y);
1944
                                    SPPIDUtil.CalcOverlap(prevRange, groupRange, ref distanceX, ref distanceY, ref overlapX, ref overlapY);
1945
                                    if ((slopeType == SlopeType.HORIZONTAL && overlapX) ||
1946
                                        (slopeType == SlopeType.VERTICAL && overlapY))
1947
                                    {
1948
                                        RemoveSymbol(group);
1949
                                        foreach (var _temp in group)
1950
                                            SPPIDUtil.CalcNewCoordinateForSymbol(_temp, prevSymbol, distanceX, distanceY);
1951

    
1952
                                        SymbolGroupModeling(priority, group);
1953
                                    }
1954
                                    else if (branchCount > 0)
1955
                                    {
1956
                                        LMConnector _connector = JustLineModeling(connLine);
1957
                                        if (_connector != null)
1958
                                        {
1959
                                            double distance = GetConnectorDistance(_connector);
1960
                                            int cellCount = (int)Math.Truncate(distance / GridSetting.GetInstance().Length);
1961
                                            _placement.PIDRemovePlacement(_connector.AsLMRepresentation());
1962
                                            _connector.Commit();
1963
                                            ReleaseCOMObjects(_connector);
1964
                                            _connector = null;
1965
                                            if (cellCount < branchCount + 1)
1966
                                            {
1967
                                                int moveCount = branchCount - cellCount;
1968
                                                RemoveSymbol(group);
1969
                                                foreach (var _temp in group)
1970
                                                    SPPIDUtil.CalcNewCoordinateForSymbol(_temp, prevSymbol, moveCount * GridSetting.GetInstance().Length, moveCount * GridSetting.GetInstance().Length);
1971

    
1972
                                                SymbolGroupModeling(priority, group);
1973
                                            }
1974
                                        }
1975
                                    }
1976
                                }
1977
                                else
1978
                                {
1979
                                    SymbolModeling(connSymbol, null);
1980
                                    // Range 겹치는지 확인해야함
1981
                                    double[] prevRange = null;
1982
                                    GetSPPIDSymbolRange(prevSymbol, ref prevRange);
1983
                                    double[] connRange = null;
1984
                                    GetSPPIDSymbolRange(connSymbol, ref connRange);
1985

    
1986
                                    double distanceX = 0;
1987
                                    double distanceY = 0;
1988
                                    bool overlapX = false;
1989
                                    bool overlapY = false;
1990
                                    SlopeType slopeType = SPPIDUtil.CalcSlope(prevSymbol.SPPID.ORIGINAL_X, prevSymbol.SPPID.ORIGINAL_Y, connSymbol.SPPID.ORIGINAL_X, connSymbol.SPPID.ORIGINAL_Y);
1991
                                    SPPIDUtil.CalcOverlap(prevRange, connRange, ref distanceX, ref distanceY, ref overlapX, ref overlapY);
1992
                                    if ((slopeType == SlopeType.HORIZONTAL && overlapX) ||
1993
                                        (slopeType == SlopeType.VERTICAL && overlapY))
1994
                                    {
1995
                                        RemoveSymbol(connSymbol);
1996
                                        SPPIDUtil.CalcNewCoordinateForSymbol(connSymbol, prevSymbol, distanceX, distanceY);
1997

    
1998
                                        SymbolModeling(connSymbol, null);
1999
                                    }
2000
                                    else if (branchCount > 0)
2001
                                    {
2002
                                        LMConnector _connector = JustLineModeling(connLine);
2003
                                        if (_connector != null)
2004
                                        {
2005
                                            double distance = GetConnectorDistance(_connector);
2006
                                            int cellCount = (int)Math.Truncate(distance / GridSetting.GetInstance().Length);
2007
                                            _placement.PIDRemovePlacement(_connector.AsLMRepresentation());
2008
                                            _connector.Commit();
2009
                                            ReleaseCOMObjects(_connector);
2010
                                            _connector = null;
2011
                                            if (cellCount < branchCount + 1)
2012
                                            {
2013
                                                int moveCount = branchCount - cellCount;
2014
                                                RemoveSymbol(group);
2015
                                                foreach (var _temp in group)
2016
                                                    SPPIDUtil.CalcNewCoordinateForSymbol(_temp, prevSymbol, moveCount * GridSetting.GetInstance().Length, moveCount * GridSetting.GetInstance().Length);
2017

    
2018
                                                SymbolGroupModeling(priority, group);
2019
                                            }
2020
                                        }
2021
                                    }
2022
                                }
2023
                            }
2024
                            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetProgress, document.SYMBOLS.FindAll(x => !string.IsNullOrEmpty(x.SPPID.RepresentationId)).Count);
2025
                            SymbolModelingByNeerSymbolLoop(connSymbol, endObjects);
2026
                        }
2027
                        else if (connItem.GetType() == typeof(Line))
2028
                        {
2029
                            Line connLine = connItem as Line;
2030
                            if (!SPPIDUtil.IsBranchLine(connLine, line))
2031
                                SymbolModelingByNeerLineLoop(connLine, endObjects, prevSymbol);
2032
                        }
2033
                    }
2034
                }
2035
            }
2036
        }
2037

    
2038
        private void SymbolGroupModeling(Symbol firstSymbol, List<Symbol> group)
2039
        {
2040
            List<Symbol> endModelingGroup = new List<Symbol>();
2041
            SymbolModeling(firstSymbol, null);
2042
            endModelingGroup.Add(firstSymbol);
2043
            while (endModelingGroup.Count != group.Count)
2044
            {
2045
                foreach (var _symbol in group)
2046
                {
2047
                    if (!endModelingGroup.Contains(_symbol))
2048
                    {
2049
                        foreach (var _connector in _symbol.CONNECTORS)
2050
                        {
2051
                            Symbol _connSymbol = SPPIDUtil.FindObjectByUID(document, _connector.CONNECTEDITEM) as Symbol;
2052
                            if (_connSymbol != null && endModelingGroup.Contains(_connSymbol))
2053
                            {
2054
                                SymbolModeling(_symbol, _connSymbol);
2055
                                endModelingGroup.Add(_symbol);
2056
                                break;
2057
                            }
2058
                        }
2059
                    }
2060
                }
2061
            }
2062
        }
2063

    
2064
        /// <summary>
2065
        /// 심볼을 실제로 Modeling할때 ChildSymbol이 있다면 Modeling하는 메서드
2066
        /// </summary>
2067
        /// <param name="childSymbol"></param>
2068
        /// <param name="parentSymbol"></param>
2069
        private void CreateChildSymbol(ChildSymbol childSymbol, LMSymbol parentSymbol, Symbol parent)
2070
        {
2071
            Ingr.RAD2D.Symbol2d symbol2d = radApp.ActiveDocument.ActiveSheet.DrawingObjects[parentSymbol.get_GraphicOID().ToString()];
2072
            double x1 = 0;
2073
            double x2 = 0;
2074
            double y1 = 0;
2075
            double y2 = 0;
2076
            symbol2d.Range(out x1, out y1, out x2, out y2);
2077

    
2078
            LMSymbol _LMSymbol = _placement.PIDPlaceSymbol(childSymbol.SPPID.MAPPINGNAME, (x1 + x2) / 2, (y1 + y2) / 2, TargetItem: parentSymbol);
2079
            if (_LMSymbol != null)
2080
            {
2081
                _LMSymbol.Commit();
2082
                childSymbol.SPPID.RepresentationId = _LMSymbol.AsLMRepresentation().Id;
2083
                foreach (var item in childSymbol.ChildSymbols)
2084
                    CreateChildSymbol(item, _LMSymbol, parent);
2085
            }
2086

    
2087

    
2088
            ReleaseCOMObjects(_LMSymbol);
2089
        }
2090
        double index = 0;
2091
        private void NewLineModeling(Line line, bool isBranchModeling = false)
2092
        {
2093
            if (!string.IsNullOrEmpty(line.SPPID.ModelItemId) || (BranchLines.Contains(line) && !isBranchModeling))
2094
                return;
2095

    
2096
            List<Line> group = new List<Line>();
2097
            GetConnectedLineGroup(line, group);
2098
            LineCoordinateCorrection(group);
2099

    
2100
            foreach (var groupLine in group)
2101
            {
2102
                if (!isBranchModeling && SPPIDUtil.IsBranchLine(groupLine))
2103
                {
2104
                    BranchLines.Add(groupLine);
2105
                    continue;
2106
                }
2107

    
2108
                bool diagonal = false;
2109
                if (groupLine.SlopeType != SlopeType.HORIZONTAL && groupLine.SlopeType != SlopeType.VERTICAL)
2110
                    diagonal = true;
2111
                _LMAItem _LMAItem = _placement.PIDCreateItem(groupLine.SPPID.MAPPINGNAME);
2112
                LMSymbol _LMSymbolStart = null;
2113
                LMSymbol _LMSymbolEnd = null;
2114
                PlaceRunInputs placeRunInputs = new PlaceRunInputs();
2115
                foreach (var connector in groupLine.CONNECTORS)
2116
                {
2117
                    double x = 0;
2118
                    double y = 0;
2119
                    GetTargetLineConnectorPoint(connector, groupLine, ref x, ref y);
2120
                    if (connector.ConnectedObject == null)
2121
                    {
2122
                        placeRunInputs.AddPoint(x, y);
2123
                    }
2124
                    else if (connector.ConnectedObject.GetType() == typeof(Symbol))
2125
                    {
2126
                        Symbol targetSymbol = connector.ConnectedObject as Symbol;
2127
                        GetTargetSymbolConnectorPoint(targetSymbol.CONNECTORS.Find(z => z.ConnectedObject == groupLine), targetSymbol, ref x, ref y);
2128
                        if (groupLine.CONNECTORS.IndexOf(connector) == 0)
2129
                        {
2130
                            _LMSymbolStart = GetTargetSymbol(targetSymbol, groupLine);
2131
                            if (_LMSymbolStart != null)
2132
                                placeRunInputs.AddSymbolTarget(_LMSymbolStart, x, y, diagonal);
2133
                            else
2134
                                placeRunInputs.AddPoint(x, y);
2135
                        }
2136
                        else
2137
                        {
2138
                            _LMSymbolEnd = GetTargetSymbol(targetSymbol, groupLine);
2139
                            if (_LMSymbolEnd != null)
2140
                                placeRunInputs.AddSymbolTarget(_LMSymbolEnd, x, y, diagonal);
2141
                            else
2142
                                placeRunInputs.AddPoint(x, y);
2143
                        }
2144
                    }
2145
                    else if (connector.ConnectedObject.GetType() == typeof(Line))
2146
                    {
2147
                        Line targetLine = connector.ConnectedObject as Line;
2148
                        if (!string.IsNullOrEmpty(targetLine.SPPID.ModelItemId))
2149
                        {
2150
                            LMConnector targetConnector = FindTargetLMConnectorForBranch(line, targetLine, ref x, ref y);
2151
                            if (targetConnector != null)
2152
                            {
2153
                                placeRunInputs.AddConnectorTarget(targetConnector, x, y, diagonal);
2154
                                ChangeLineSPPIDCoordinateByConnector(groupLine, targetLine, x, y, false);
2155
                            }
2156
                            else
2157
                            {
2158
                                placeRunInputs.AddPoint( x, y);
2159
                                ChangeLineSPPIDCoordinateByConnector(groupLine, targetLine, x, y, false);
2160
                            }
2161
                        }
2162
                        else
2163
                        {
2164
                            if (groupLine.CONNECTORS.IndexOf(connector) == 0)
2165
                            {
2166
                                index += 0.01;
2167
                                if (groupLine.SlopeType == SlopeType.HORIZONTAL)
2168
                                    placeRunInputs.AddPoint(x, -0.1 - index);
2169
                                else if (groupLine.SlopeType == SlopeType.VERTICAL)
2170
                                    placeRunInputs.AddPoint(-0.1 - index, y);
2171
                                else
2172
                                {
2173
                                    Line nextLine = groupLine.CONNECTORS[0].ConnectedObject as Line;
2174
                                    if (SPPIDUtil.CalcAngle(nextLine.SPPID.START_X, nextLine.SPPID.START_Y, nextLine.SPPID.END_X, nextLine.SPPID.END_Y) < 45)
2175
                                        placeRunInputs.AddPoint(-0.1 - index, y);
2176
                                    else
2177
                                        placeRunInputs.AddPoint(x, -0.1 - index);
2178
                                }
2179
                            }
2180

    
2181
                            placeRunInputs.AddPoint(x, y);
2182

    
2183
                            if (groupLine.CONNECTORS.IndexOf(connector) == 1)
2184
                            {
2185
                                index += 0.01;
2186
                                if (groupLine.SlopeType == SlopeType.HORIZONTAL)
2187
                                    placeRunInputs.AddPoint(x, -0.1 - index);
2188
                                else if (groupLine.SlopeType == SlopeType.VERTICAL)
2189
                                    placeRunInputs.AddPoint(-0.1 - index, y);
2190
                                else
2191
                                {
2192
                                    Line nextLine = groupLine.CONNECTORS[1].ConnectedObject as Line;
2193
                                    if (SPPIDUtil.CalcAngle(nextLine.SPPID.START_X, nextLine.SPPID.START_Y, nextLine.SPPID.END_X, nextLine.SPPID.END_Y) < 45)
2194
                                        placeRunInputs.AddPoint(-0.1 - index, y);
2195
                                    else
2196
                                        placeRunInputs.AddPoint(x, -0.1 - index);
2197
                                }
2198
                            }
2199
                        }
2200
                    }
2201
                }
2202

    
2203
                LMConnector _lMConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
2204
                if (_lMConnector != null)
2205
                {
2206
                    _lMConnector.Commit();
2207
                    groupLine.SPPID.ModelItemId = _lMConnector.ModelItemID;
2208

    
2209
                    bool bRemodelingStart = false;
2210
                    if (_LMSymbolStart != null)
2211
                        NeedReModeling(groupLine, _LMSymbolStart, ref bRemodelingStart);
2212
                    bool bRemodelingEnd = false;
2213
                    if (_LMSymbolEnd != null)
2214
                        NeedReModeling(groupLine, _LMSymbolEnd, ref bRemodelingEnd);
2215

    
2216
                    if (bRemodelingStart || bRemodelingEnd)
2217
                        ReModelingLine(groupLine, _lMConnector, _LMSymbolStart, _LMSymbolEnd, bRemodelingStart, bRemodelingEnd);
2218

    
2219
                    FlowMarkModeling(groupLine);
2220
                    LineNumberModelingOnlyOne(groupLine);
2221

    
2222
                    ReleaseCOMObjects(_lMConnector);
2223

    
2224
                    LMModelItem modelItem = dataSource.GetModelItem(groupLine.SPPID.ModelItemId);
2225
                    if (modelItem != null)
2226
                    {
2227
                        LMAAttribute attribute = modelItem.Attributes["FlowDirection"];
2228
                        if (attribute != null)
2229
                            attribute.set_Value("End 1 is upstream (Inlet)");
2230
                        modelItem.Commit();
2231
                    }
2232
                    ReleaseCOMObjects(modelItem);
2233
                    modelItem = null;
2234
                }
2235
                else if (!isBranchModeling)
2236
                {
2237
                    Log.Write("Main Line Modeling : " + groupLine.UID);
2238
                }
2239

    
2240
                List<object> removeLines = groupLine.CONNECTORS.FindAll(x =>
2241
                x.ConnectedObject != null &&
2242
                x.ConnectedObject.GetType() == typeof(Line) &&
2243
                !string.IsNullOrEmpty(((Line)x.ConnectedObject).SPPID.ModelItemId))
2244
                .Select(x => x.ConnectedObject)
2245
                .ToList();
2246

    
2247
                foreach (var item in removeLines)
2248
                    RemoveLineForModeling(item as Line);
2249

    
2250
                ReleaseCOMObjects(_LMAItem);
2251
                _LMAItem = null;
2252
                ReleaseCOMObjects(placeRunInputs);
2253
                placeRunInputs = null;
2254
                ReleaseCOMObjects(_LMSymbolStart);
2255
                _LMSymbolStart = null;
2256
                ReleaseCOMObjects(_LMSymbolEnd);
2257
                _LMSymbolEnd = null;
2258

    
2259
                if (isBranchModeling && BranchLines.Contains(groupLine))
2260
                    BranchLines.Remove(groupLine);
2261
            }
2262
        }
2263

    
2264
        private LMConnector JustLineModeling(Line line)
2265
        {
2266
            bool diagonal = false;
2267
            if (line.SlopeType != SlopeType.HORIZONTAL && line.SlopeType != SlopeType.VERTICAL)
2268
                diagonal = true;
2269
            _LMAItem _LMAItem = _placement.PIDCreateItem(line.SPPID.MAPPINGNAME);
2270
            LMSymbol _LMSymbolStart = null;
2271
            LMSymbol _LMSymbolEnd = null;
2272
            PlaceRunInputs placeRunInputs = new PlaceRunInputs();
2273
            foreach (var connector in line.CONNECTORS)
2274
            {
2275
                double x = 0;
2276
                double y = 0;
2277
                GetTargetLineConnectorPoint(connector, line, ref x, ref y);
2278
                if (connector.ConnectedObject == null)
2279
                {
2280
                    placeRunInputs.AddPoint(x, y);
2281
                }
2282
                else if (connector.ConnectedObject.GetType() == typeof(Symbol))
2283
                {
2284
                    Symbol targetSymbol = connector.ConnectedObject as Symbol;
2285
                    GetTargetSymbolConnectorPoint(targetSymbol.CONNECTORS.Find(z => z.ConnectedObject == line), targetSymbol, ref x, ref y);
2286
                    if (line.CONNECTORS.IndexOf(connector) == 0)
2287
                    {
2288
                        _LMSymbolStart = GetTargetSymbol(targetSymbol, line);
2289
                        if (_LMSymbolStart != null)
2290
                            placeRunInputs.AddSymbolTarget(_LMSymbolStart, x, y, diagonal);
2291
                        else
2292
                            placeRunInputs.AddPoint(x, y);
2293
                    }
2294
                    else
2295
                    {
2296
                        _LMSymbolEnd = GetTargetSymbol(targetSymbol, line);
2297
                        if (_LMSymbolEnd != null)
2298
                            placeRunInputs.AddSymbolTarget(_LMSymbolEnd, x, y, diagonal);
2299
                        else
2300
                            placeRunInputs.AddPoint(x, y);
2301
                    }
2302
                }
2303
                else if (connector.ConnectedObject.GetType() == typeof(Line))
2304
                {
2305
                    Line targetLine = connector.ConnectedObject as Line;
2306
                    if (!string.IsNullOrEmpty(targetLine.SPPID.ModelItemId))
2307
                    {
2308
                        LMConnector targetConnector = FindTargetLMConnectorForBranch(line, targetLine, ref x, ref y);
2309
                        if (targetConnector != null)
2310
                        {
2311
                            placeRunInputs.AddConnectorTarget(targetConnector, x, y, diagonal);
2312
                            ChangeLineSPPIDCoordinateByConnector(line, targetLine, x, y, false);
2313
                        }
2314
                        else
2315
                        {
2316
                            placeRunInputs.AddPoint(x, y);
2317
                            ChangeLineSPPIDCoordinateByConnector(line, targetLine, x, y, false);
2318
                        }
2319
                    }
2320
                    else
2321
                        placeRunInputs.AddPoint(x, y);
2322
                }
2323
            }
2324

    
2325
            LMConnector _lMConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
2326
            if (_lMConnector != null)
2327
                _lMConnector.Commit();
2328

    
2329
            ReleaseCOMObjects(_LMAItem);
2330
            _LMAItem = null;
2331
            ReleaseCOMObjects(placeRunInputs);
2332
            placeRunInputs = null;
2333
            ReleaseCOMObjects(_LMSymbolStart);
2334
            _LMSymbolStart = null;
2335
            ReleaseCOMObjects(_LMSymbolEnd);
2336
            _LMSymbolEnd = null;
2337

    
2338
            return _lMConnector;
2339
        }
2340

    
2341
        private void RemoveLineForModeling(Line line)
2342
        {
2343
            LMModelItem modelItem = dataSource.GetModelItem(line.SPPID.ModelItemId);
2344
            if (modelItem != null)
2345
            {
2346
                foreach (LMRepresentation rep in modelItem.Representations)
2347
                {
2348
                    if (rep.Attributes["RepresentationType"].get_Value() == "Connector" && rep.Attributes["ItemStatus"].get_Value() == "Active")
2349
                    {
2350
                        LMConnector _LMConnector = dataSource.GetConnector(rep.Id);
2351
                        dynamic OID = rep.get_GraphicOID().ToString();
2352
                        DependencyObject drawingObject = radApp.ActiveDocument.ActiveSheet.DrawingObjects[OID];
2353
                        Ingr.RAD2D.LineStringGeometry2d lineStringGeometry = drawingObject.GetGeometry() as Ingr.RAD2D.LineStringGeometry2d;
2354
                        int verticesCount = lineStringGeometry.VertexCount;
2355
                        double[] vertices = null;
2356
                        lineStringGeometry.GetVertices(ref verticesCount, ref vertices);
2357
                        for (int i = 0; i < verticesCount; i++)
2358
                        {
2359
                            double x = 0;
2360
                            double y = 0;
2361
                            lineStringGeometry.GetVertex(i + 1, ref x, ref y);
2362
                            if (verticesCount == 2 && (x < 0 || y < 0))
2363
                                _placement.PIDRemovePlacement(rep);
2364
                        }
2365
                        ReleaseCOMObjects(_LMConnector);
2366
                    }
2367
                }
2368

    
2369
                ReleaseCOMObjects(modelItem);
2370
            }
2371
        }
2372

    
2373
        private void GetConnectedLineGroup(Line line, List<Line> group)
2374
        {
2375
            if (!group.Contains(line))
2376
                group.Add(line);
2377
            foreach (var connector in line.CONNECTORS)
2378
            {
2379
                if (connector.ConnectedObject != null &&
2380
                    connector.ConnectedObject.GetType() == typeof(Line) &&
2381
                    !group.Contains(connector.ConnectedObject) &&
2382
                    string.IsNullOrEmpty(((Line)connector.ConnectedObject).SPPID.ModelItemId))
2383
                {
2384
                    Line connLine = connector.ConnectedObject as Line;
2385
                    if (!SPPIDUtil.IsBranchLine(connLine, line))
2386
                    {
2387
                        if (line.CONNECTORS.IndexOf(connector) == 0)
2388
                            group.Insert(0, connLine);
2389
                        else
2390
                            group.Add(connLine);
2391
                        GetConnectedLineGroup(connLine, group);
2392
                    }
2393
                }
2394
            }
2395
        }
2396

    
2397
        private void LineCoordinateCorrection(List<Line> group)
2398
        {
2399
            // 순서대로 전 Item 기준 정렬
2400
            LineCoordinateCorrectionByStart(group);
2401

    
2402
            // 역으로 심볼이 있을 경우 좌표 보정
2403
            LineCoordinateCorrectionForLastLine(group);
2404
        }
2405

    
2406
        private void LineCoordinateCorrectionByStart(List<Line> group)
2407
        {
2408
            for (int i = 0; i < group.Count; i++)
2409
            {
2410
                Line line = group[i];
2411
                if (i == 0)
2412
                {
2413
                    Connector symbolConnector = line.CONNECTORS.Find(x => x.ConnectedObject != null && x.ConnectedObject.GetType() == typeof(Symbol));
2414
                    if (symbolConnector != null)
2415
                        LineCoordinateCorrectionByConnItem(line, symbolConnector.ConnectedObject);
2416
                }
2417
                else if (i != 0)
2418
                {
2419
                    LineCoordinateCorrectionByConnItem(line, group[i - 1]);
2420
                }
2421
            }
2422
        }
2423

    
2424
        private void LineCoordinateCorrectionForLastLine(List<Line> group)
2425
        {
2426
            Line checkLine = group[group.Count - 1];
2427
            Connector lastSymbolConnector = checkLine.CONNECTORS.Find(x => x.ConnectedObject != null && x.ConnectedObject.GetType() == typeof(Symbol));
2428
            if (lastSymbolConnector != null)
2429
            {
2430
                LineCoordinateCorrectionByConnItem(checkLine, lastSymbolConnector.ConnectedObject);
2431
                for (int i = group.Count - 2; i >= 0; i--)
2432
                {
2433
                    Line line = group[i + 1];
2434
                    Line prevLine = group[i];
2435

    
2436
                    // 같으면 보정
2437
                    if (line.SlopeType == prevLine.SlopeType)
2438
                        LineCoordinateCorrectionByConnItem(prevLine, line);
2439
                    else
2440
                    {
2441
                        if (line.SlopeType == SlopeType.HORIZONTAL)
2442
                        {
2443
                            double prevX = 0;
2444
                            double prevY = 0;
2445
                            GetTargetLineConnectorPoint(prevLine.CONNECTORS.Find(z => z.ConnectedObject == line), prevLine, ref prevX, ref prevY);
2446
                            ChangeLineSPPIDCoordinateByConnectorOnlyX(line, prevLine, prevX);
2447

    
2448
                            double x = 0;
2449
                            double y = 0;
2450
                            GetTargetLineConnectorPoint(line.CONNECTORS.Find(z => z.ConnectedObject == prevLine), line, ref x, ref y);
2451
                            ChangeLineSPPIDCoordinateByConnectorOnlyY(prevLine, line, y);
2452
                        }
2453
                        else if (line.SlopeType == SlopeType.VERTICAL)
2454
                        {
2455
                            double prevX = 0;
2456
                            double prevY = 0;
2457
                            GetTargetLineConnectorPoint(prevLine.CONNECTORS.Find(z => z.ConnectedObject == line), prevLine, ref prevX, ref prevY);
2458
                            ChangeLineSPPIDCoordinateByConnectorOnlyY(line, prevLine, prevY);
2459

    
2460
                            double x = 0;
2461
                            double y = 0;
2462
                            GetTargetLineConnectorPoint(line.CONNECTORS.Find(z => z.ConnectedObject == prevLine), line, ref x, ref y);
2463
                            ChangeLineSPPIDCoordinateByConnectorOnlyX(prevLine, line, x);
2464
                        }
2465
                        break;
2466
                    }
2467
                }
2468
            }
2469
        }
2470

    
2471
        private void LineCoordinateCorrectionByConnItem(Line line, object connItem)
2472
        {
2473
            double x = 0;
2474
            double y = 0;
2475
            if (connItem.GetType() == typeof(Symbol))
2476
            {
2477
                Symbol targetSymbol = connItem as Symbol;
2478
                Connector targetConnector = targetSymbol.CONNECTORS.Find(z => z.ConnectedObject == line);
2479
                if (targetConnector != null)
2480
                    GetTargetSymbolConnectorPoint(targetConnector, targetSymbol, ref x, ref y);
2481
                else
2482
                    throw new Exception("Target symbol UID : " + targetSymbol.UID + "\r\nLine UID : " + line.UID);
2483
            }
2484
            else if (connItem.GetType() == typeof(Line))
2485
            {
2486
                Line targetLine = connItem as Line;
2487
                GetTargetLineConnectorPoint(targetLine.CONNECTORS.Find(z => z.ConnectedObject == line), targetLine, ref x, ref y);
2488
            }
2489

    
2490
            ChangeLineSPPIDCoordinateByConnector(line, connItem, x, y);
2491
        }
2492

    
2493
        private void ChangeLineSPPIDCoordinateByConnector(Line line, object connItem, double x, double y, bool changeOtherCoordinate = true)
2494
        {
2495
            Connector connector = line.CONNECTORS.Find(z => z.ConnectedObject == connItem);
2496
            int index = line.CONNECTORS.IndexOf(connector);
2497
            if (index == 0)
2498
            {
2499
                line.SPPID.START_X = x;
2500
                line.SPPID.START_Y = y;
2501
                if (line.SlopeType == SlopeType.HORIZONTAL && changeOtherCoordinate)
2502
                    line.SPPID.END_Y = y;
2503
                else if (line.SlopeType == SlopeType.VERTICAL && changeOtherCoordinate)
2504
                    line.SPPID.END_X = x;
2505
            }
2506
            else
2507
            {
2508
                line.SPPID.END_X = x;
2509
                line.SPPID.END_Y = y;
2510
                if (line.SlopeType == SlopeType.HORIZONTAL && changeOtherCoordinate)
2511
                    line.SPPID.START_Y = y;
2512
                else if (line.SlopeType == SlopeType.VERTICAL && changeOtherCoordinate)
2513
                    line.SPPID.START_X = x;
2514
            }
2515
        }
2516

    
2517
        private void ChangeLineSPPIDCoordinateByConnectorOnlyX(Line line, object connItem, double x)
2518
        {
2519
            Connector connector = line.CONNECTORS.Find(z => z.ConnectedObject == connItem);
2520
            int index = line.CONNECTORS.IndexOf(connector);
2521
            if (index == 0)
2522
            {
2523
                line.SPPID.START_X = x;
2524
                if (line.SlopeType == SlopeType.VERTICAL)
2525
                    line.SPPID.END_X = x;
2526
            }
2527
            else
2528
            {
2529
                line.SPPID.END_X = x;
2530
                if (line.SlopeType == SlopeType.VERTICAL)
2531
                    line.SPPID.START_X = x;
2532
            }
2533
        }
2534

    
2535
        private void ChangeLineSPPIDCoordinateByConnectorOnlyY(Line line, object connItem, double y)
2536
        {
2537
            Connector connector = line.CONNECTORS.Find(z => z.ConnectedObject == connItem);
2538
            int index = line.CONNECTORS.IndexOf(connector);
2539
            if (index == 0)
2540
            {
2541
                line.SPPID.START_Y = y;
2542
                if (line.SlopeType == SlopeType.HORIZONTAL)
2543
                    line.SPPID.END_Y = y;
2544
            }
2545
            else
2546
            {
2547
                line.SPPID.END_Y = y;
2548
                if (line.SlopeType == SlopeType.HORIZONTAL)
2549
                    line.SPPID.START_Y = y;
2550
            }
2551
        }
2552

    
2553
        private void NeedReModeling(Line line, LMSymbol symbol, ref bool result)
2554
        {
2555
            if (symbol != null)
2556
            {
2557
                string repID = symbol.AsLMRepresentation().Id;
2558
                string symbolUID = SPPIDUtil.FindSymbolByRepresentationID(document, repID).UID;
2559
                string lineUID = line.UID;
2560

    
2561
                SpecBreak startSpecBreak = document.SpecBreaks.Find(x =>
2562
                (x.DownStreamUID == symbolUID || x.UpStreamUID == symbolUID) &&
2563
                (x.DownStreamUID == lineUID || x.UpStreamUID == lineUID));
2564

    
2565
                EndBreak startEndBreak = document.EndBreaks.Find(x =>
2566
                (x.OWNER == symbolUID || x.PROPERTIES.Find(y => y.ATTRIBUTE == "Connected Item").VALUE == symbolUID) &&
2567
                (x.OWNER == lineUID || x.PROPERTIES.Find(y => y.ATTRIBUTE == "Connected Item").VALUE == lineUID));
2568

    
2569
                if (startSpecBreak != null || startEndBreak != null)
2570
                    result = true;
2571
            }
2572
        }
2573

    
2574
        /// <summary>
2575
        /// Symbol에 붙을 경우 Line을 Remodeling 한다.
2576
        /// </summary>
2577
        /// <param name="lines"></param>
2578
        /// <param name="prevLMConnector"></param>
2579
        /// <param name="startSymbol"></param>
2580
        /// <param name="endSymbol"></param>
2581
        private void ReModelingLine(Line line, LMConnector prevLMConnector, LMSymbol startSymbol, LMSymbol endSymbol, bool bStart, bool bEnd)
2582
        {
2583
            string symbolPath = string.Empty;
2584
            #region get symbol path
2585
            LMModelItem modelItem = dataSource.GetModelItem(prevLMConnector.ModelItemID);
2586
            symbolPath = GetSPPIDFileName(modelItem);
2587
            ReleaseCOMObjects(modelItem);
2588
            #endregion
2589
            bool diagonal = false;
2590
            if (line.SlopeType != SlopeType.HORIZONTAL && line.SlopeType != SlopeType.VERTICAL)
2591
                diagonal = true;
2592
            _LMAItem _LMAItem = _placement.PIDCreateItem(symbolPath);
2593
            LMConnector newConnector = null;
2594
            dynamic OID = prevLMConnector.get_GraphicOID().ToString();
2595
            DependencyObject drawingObject = radApp.ActiveDocument.ActiveSheet.DrawingObjects[OID];
2596
            Ingr.RAD2D.LineStringGeometry2d lineStringGeometry = drawingObject.GetGeometry() as Ingr.RAD2D.LineStringGeometry2d;
2597
            int verticesCount = lineStringGeometry.VertexCount;
2598
            PlaceRunInputs placeRunInputs = new PlaceRunInputs();
2599

    
2600
            List<double[]> vertices = new List<double[]>();
2601
            for (int i = 1; i <= verticesCount; i++)
2602
            {
2603
                double x = 0;
2604
                double y = 0;
2605
                lineStringGeometry.GetVertex(i, ref x, ref y);
2606
                vertices.Add(new double[] { x, y });
2607
            }
2608

    
2609
            for (int i = 0; i < vertices.Count; i++)
2610
            {
2611
                double[] points = vertices[i];
2612
                // 시작 심볼이 있고 첫번째 좌표일 때
2613
                if (startSymbol != null && i == 0)
2614
                {
2615
                    if (bStart)
2616
                    {
2617
                        SlopeType slopeType = SPPIDUtil.CalcSlope(points[0], points[1], vertices[i + 1][0], vertices[i + 1][1]);
2618
                        if (slopeType == SlopeType.HORIZONTAL)
2619
                            placeRunInputs.AddPoint(points[0], -0.1);
2620
                        else if (slopeType == SlopeType.VERTICAL)
2621
                            placeRunInputs.AddPoint(-0.1, points[1]);
2622
                        else
2623
                            placeRunInputs.AddPoint(points[0], -0.1);
2624

    
2625
                        placeRunInputs.AddPoint(points[0], points[1]);
2626
                    }
2627
                    else
2628
                    {
2629
                        placeRunInputs.AddSymbolTarget(startSymbol, points[0], points[1], diagonal);
2630
                    }
2631
                }
2632
                // 마지막 심볼이 있고 마지막 좌표일 때
2633
                else if (endSymbol != null && i == vertices.Count - 1)
2634
                {
2635
                    if (bEnd)
2636
                    {
2637
                        placeRunInputs.AddPoint(points[0], points[1]);
2638

    
2639
                        SlopeType slopeType = SPPIDUtil.CalcSlope(points[0], points[1], vertices[i - 1][0], vertices[i - 1][1]);
2640
                        if (slopeType == SlopeType.HORIZONTAL)
2641
                            placeRunInputs.AddPoint(points[0], -0.1);
2642
                        else if (slopeType == SlopeType.VERTICAL)
2643
                            placeRunInputs.AddPoint(-0.1, points[1]);
2644
                        else
2645
                            placeRunInputs.AddPoint(points[0], -0.1);
2646
                    }
2647
                    else
2648
                    {
2649
                        placeRunInputs.AddSymbolTarget(endSymbol, points[0], points[1], diagonal);
2650
                    }
2651
                }
2652
                // 첫번째이며 시작 심볼이 아니고 Connecotr일 경우
2653
                else if (i == 0 && prevLMConnector.ConnectItem1SymbolObject != null)
2654
                    placeRunInputs.AddSymbolTarget(prevLMConnector.ConnectItem1SymbolObject, points[0], points[1], diagonal);
2655
                // 마지막이며 마지막 심볼이 아니고 Connecotr일 경우
2656
                else if (i == vertices.Count - 1 && prevLMConnector.ConnectItem2SymbolObject != null)
2657
                    placeRunInputs.AddSymbolTarget(prevLMConnector.ConnectItem2SymbolObject, points[0], points[1], diagonal);
2658
                else
2659
                    placeRunInputs.AddPoint(points[0], points[1]);
2660
            }
2661

    
2662
            _placement.PIDRemovePlacement(prevLMConnector.AsLMRepresentation());
2663
            newConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
2664

    
2665
            ReleaseCOMObjects(placeRunInputs);
2666
            ReleaseCOMObjects(_LMAItem);
2667
            ReleaseCOMObjects(modelItem);
2668

    
2669
            if (newConnector != null)
2670
            {
2671
                newConnector.Commit();
2672
                if (startSymbol != null && bStart)
2673
                {
2674
                    _LMAItem = _placement.PIDCreateItem(symbolPath);
2675
                    placeRunInputs = new PlaceRunInputs();
2676
                    placeRunInputs.AddSymbolTarget(startSymbol, vertices[0][0], vertices[0][1]);
2677
                    placeRunInputs.AddConnectorTarget(newConnector, vertices[0][0], vertices[0][1]);
2678
                    LMConnector _LMConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
2679
                    if (_LMConnector != null)
2680
                    {
2681
                        _LMConnector.Commit();
2682
                        RemoveConnectorForReModelingLine(newConnector);
2683
                        ZeroLengthModelItemID.Add(_LMConnector.ModelItemID);
2684
                        ReleaseCOMObjects(_LMConnector);
2685
                    }
2686
                    ReleaseCOMObjects(placeRunInputs);
2687
                    ReleaseCOMObjects(_LMAItem);
2688
                }
2689

    
2690
                if (endSymbol != null && bEnd)
2691
                {
2692
                    if (startSymbol != null)
2693
                    {
2694
                        Dictionary<LMConnector, List<double[]>> dicVertices = GetPipeRunVertices(newConnector.ModelItemID);
2695
                        newConnector = dicVertices.First().Key;
2696
                    }
2697

    
2698
                    _LMAItem = _placement.PIDCreateItem(symbolPath);
2699
                    placeRunInputs = new PlaceRunInputs();
2700
                    placeRunInputs.AddSymbolTarget(endSymbol, vertices[vertices.Count - 1][0], vertices[vertices.Count - 1][1]);
2701
                    placeRunInputs.AddConnectorTarget(newConnector, vertices[vertices.Count - 1][0], vertices[vertices.Count - 1][1]);
2702
                    LMConnector _LMConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
2703
                    if (_LMConnector != null)
2704
                    {
2705
                        _LMConnector.Commit();
2706
                        RemoveConnectorForReModelingLine(newConnector);
2707
                        ZeroLengthModelItemIDReverse.Add(_LMConnector.ModelItemID);
2708
                        ReleaseCOMObjects(_LMConnector);
2709
                    }
2710
                    ReleaseCOMObjects(placeRunInputs);
2711
                    ReleaseCOMObjects(_LMAItem);
2712
                }
2713

    
2714
                line.SPPID.ModelItemId = newConnector.ModelItemID;
2715
                ReleaseCOMObjects(newConnector);
2716
            }
2717

    
2718
            ReleaseCOMObjects(modelItem);
2719
        }
2720

    
2721
        /// <summary>
2722
        /// Remodeling 과정에서 생긴 불필요한 Connector 제거
2723
        /// </summary>
2724
        /// <param name="connector"></param>
2725
        private void RemoveConnectorForReModelingLine(LMConnector connector)
2726
        {
2727
            Dictionary<LMConnector, List<double[]>> dicVertices = GetPipeRunVertices(connector.ModelItemID);
2728
            foreach (var item in dicVertices)
2729
            {
2730
                if (item.Value.Count == 2)
2731
                {
2732
                    bool result = false;
2733
                    foreach (var point in item.Value)
2734
                    {
2735
                        if (point[0] < 0 || point[1] < 0)
2736
                        {
2737
                            result = true;
2738
                            _placement.PIDRemovePlacement(item.Key.AsLMRepresentation());
2739
                            break;
2740
                        }
2741
                    }
2742

    
2743
                    if (result)
2744
                        break;
2745
                }
2746
            }
2747
            foreach (var item in dicVertices)
2748
                ReleaseCOMObjects(item.Key);
2749
        }
2750

    
2751
        /// <summary>
2752
        /// Symbol이 모델링된 SPPPID Symbol Object를 반환 - 연결된 Symbol이 ChildSymbol일 수도 있기때문에 메서드 개발
2753
        /// </summary>
2754
        /// <param name="symbol"></param>
2755
        /// <param name="line"></param>
2756
        /// <returns></returns>
2757
        private LMSymbol GetTargetSymbol(Symbol symbol, Line line)
2758
        {
2759
            LMSymbol _LMSymbol = null;
2760
            foreach (var connector in symbol.CONNECTORS)
2761
            {
2762
                if (connector.CONNECTEDITEM == line.UID)
2763
                {
2764
                    if (connector.Index == 0)
2765
                        _LMSymbol = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
2766
                    else
2767
                    {
2768
                        ChildSymbol child = null;
2769
                        foreach (var childSymbol in symbol.ChildSymbols)
2770
                        {
2771
                            if (childSymbol.Connectors.Contains(connector))
2772
                                child = childSymbol;
2773
                            else
2774
                                child = GetChildSymbolByConnector(childSymbol, connector);
2775

    
2776
                            if (child != null)
2777
                                break;
2778
                        }
2779

    
2780
                        if (child != null)
2781
                            _LMSymbol = dataSource.GetSymbol(child.SPPID.RepresentationId);
2782
                    }
2783

    
2784
                    break;
2785
                }
2786
            }
2787

    
2788
            return _LMSymbol;
2789
        }
2790

    
2791
        /// <summary>
2792
        /// Connector를 가지고 있는 ChildSymbol Object 반환
2793
        /// </summary>
2794
        /// <param name="item"></param>
2795
        /// <param name="connector"></param>
2796
        /// <returns></returns>
2797
        private ChildSymbol GetChildSymbolByConnector(ChildSymbol item, Connector connector)
2798
        {
2799
            foreach (var childSymbol in item.ChildSymbols)
2800
            {
2801
                if (childSymbol.Connectors.Contains(connector))
2802
                    return childSymbol;
2803
                else
2804
                    return GetChildSymbolByConnector(childSymbol, connector);
2805
            }
2806

    
2807
            return null;
2808
        }
2809

    
2810
        /// <summary>
2811
        /// EndBreak 모델링 메서드
2812
        /// </summary>
2813
        /// <param name="endBreak"></param>
2814
        private void EndBreakModeling(EndBreak endBreak)
2815
        {
2816
            object ownerObj = SPPIDUtil.FindObjectByUID(document, endBreak.OWNER);
2817
            object connectedItem = SPPIDUtil.FindObjectByUID(document, endBreak.PROPERTIES.Find(x => x.ATTRIBUTE == "Connected Item").VALUE);
2818

    
2819
            LMConnector targetLMConnector = FindBreakLineTarget(ownerObj, connectedItem);
2820
            if (ownerObj.GetType() == typeof(Symbol) && connectedItem.GetType() == typeof(Symbol) && targetLMConnector != null)
2821
                targetLMConnector = ReModelingZeroLengthLMConnectorForSegment(targetLMConnector);
2822

    
2823
            if (targetLMConnector != null)
2824
            {
2825
                double[] point = GetSegmentPoint(ownerObj, connectedItem, targetLMConnector);
2826
                Array array = null;
2827
                if (point != null)
2828
                    array = new double[] { 0, point[0], point[1] };
2829
                else
2830
                    array = new double[] { 0, endBreak.SPPID.ORIGINAL_X, endBreak.SPPID.ORIGINAL_Y };
2831
                LMLabelPersist _LmLabelPersist = _placement.PIDPlaceLabel(endBreak.SPPID.MAPPINGNAME, ref array, LabeledItem: targetLMConnector.AsLMRepresentation(), IsLeaderVisible: true);
2832
                if (_LmLabelPersist != null)
2833
                {
2834
                    _LmLabelPersist.Commit();
2835
                    endBreak.SPPID.RepresentationId = _LmLabelPersist.AsLMRepresentation().Id;
2836
                    if (_LmLabelPersist.ModelItemObject != null)
2837
                        endBreak.SPPID.ModelItemID = _LmLabelPersist.ModelItemID;
2838
                    endBreak.SPPID.GraphicOID = _LmLabelPersist.get_GraphicOID().ToString();
2839
                    ReleaseCOMObjects(_LmLabelPersist);
2840
                }
2841
                ReleaseCOMObjects(targetLMConnector);
2842
            }
2843
            else
2844
            {
2845
                Log.Write("End Break UID : " + endBreak.UID);
2846
                Log.Write("Can't find targetLMConnector");
2847
            }
2848
        }
2849

    
2850
        private LMConnector ReModelingZeroLengthLMConnectorForSegment(LMConnector connector, string changeSymbolPath = null)
2851
        {
2852
            string symbolPath = string.Empty;
2853
            #region get symbol path
2854
            if (string.IsNullOrEmpty(changeSymbolPath))
2855
            {
2856
                LMModelItem modelItem = dataSource.GetModelItem(connector.ModelItemID);
2857
                symbolPath = GetSPPIDFileName(modelItem);
2858
                ReleaseCOMObjects(modelItem);
2859
            }
2860
            else
2861
                symbolPath = changeSymbolPath;
2862
            
2863
            #endregion
2864

    
2865
            LMConnector newConnector = null;
2866
            dynamic OID = connector.get_GraphicOID().ToString();
2867
            DependencyObject drawingObject = radApp.ActiveDocument.ActiveSheet.DrawingObjects[OID];
2868
            Ingr.RAD2D.LineStringGeometry2d lineStringGeometry = drawingObject.GetGeometry() as Ingr.RAD2D.LineStringGeometry2d;
2869
            int verticesCount = lineStringGeometry.VertexCount;
2870
            PlaceRunInputs placeRunInputs = new PlaceRunInputs();
2871
            _LMAItem _LMAItem = _placement.PIDCreateItem(symbolPath);
2872

    
2873
            if (Convert.ToBoolean(connector.get_IsZeroLength()))
2874
            {
2875
                double[] vertices = null;
2876
                lineStringGeometry.GetVertices(ref verticesCount, ref vertices);
2877
                double x = 0;
2878
                double y = 0;
2879
                lineStringGeometry.GetVertex(1, ref x, ref y);
2880

    
2881
                string flowDirection = string.Empty;
2882
                LMAAttribute flowAttribute = connector.ModelItemObject.Attributes["FlowDirection"];
2883
                if (flowAttribute != null && !DBNull.Value.Equals(flowAttribute.get_Value()))
2884
                    flowDirection = flowAttribute.get_Value().ToString();
2885

    
2886
                if (flowDirection == "End 1 is downstream (Outlet)")
2887
                {
2888
                    placeRunInputs.AddSymbolTarget(connector.ConnectItem2SymbolObject, x, y);
2889
                    placeRunInputs.AddSymbolTarget(connector.ConnectItem1SymbolObject, x, y);
2890
                    flowDirection = "End 1 is upstream (Inlet)";
2891
                }
2892
                else
2893
                {
2894
                    placeRunInputs.AddSymbolTarget(connector.ConnectItem1SymbolObject, x, y);
2895
                    placeRunInputs.AddSymbolTarget(connector.ConnectItem2SymbolObject, x, y);
2896
                }
2897
                string oldModelItemId = connector.ModelItemID;
2898
                _placement.PIDRemovePlacement(connector.AsLMRepresentation());
2899
                newConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
2900
                newConnector.Commit();
2901
                ZeroLengthSymbolToSymbolModelItemID.Add(newConnector.ModelItemID);
2902
                if (!string.IsNullOrEmpty(flowDirection))
2903
                    newConnector.ModelItemObject.Attributes["FlowDirection"].set_Value(flowDirection);
2904
                ReleaseCOMObjects(connector);
2905

    
2906
                foreach (var line in document.LINES.FindAll(z => z.SPPID.ModelItemId == oldModelItemId))
2907
                {
2908
                    foreach (var repId in line.SPPID.Representations)
2909
                    {
2910
                        LMConnector _connector = dataSource.GetConnector(repId);
2911
                        if (_connector != null && _connector.get_ItemStatus() == "Active")
2912
                        {
2913
                            if (line.SPPID.ModelItemId != _connector.ModelItemID)
2914
                            {
2915
                                line.SPPID.ModelItemId = _connector.ModelItemID;
2916
                                line.SPPID.Representations = GetRepresentations(line.SPPID.ModelItemId);
2917
                            }
2918
                        }
2919
                        ReleaseCOMObjects(_connector);
2920
                        _connector = null;
2921
                    }
2922
                }
2923
            }
2924

    
2925
            return newConnector;
2926
        }
2927

    
2928
        /// <summary>
2929
        /// SpecBreak Modeling 메서드
2930
        /// </summary>
2931
        /// <param name="specBreak"></param>
2932
        private void SpecBreakModeling(SpecBreak specBreak)
2933
        {
2934
            object upStreamObj = SPPIDUtil.FindObjectByUID(document, specBreak.UpStreamUID);
2935
            object downStreamObj = SPPIDUtil.FindObjectByUID(document, specBreak.DownStreamUID);
2936

    
2937
            if (upStreamObj != null &&
2938
                downStreamObj != null)
2939
            {
2940
                LMConnector targetLMConnector = FindBreakLineTarget(upStreamObj, downStreamObj);
2941
                if (upStreamObj.GetType() == typeof(Symbol) && downStreamObj.GetType() == typeof(Symbol) && targetLMConnector != null)
2942
                    targetLMConnector = ReModelingZeroLengthLMConnectorForSegment(targetLMConnector);
2943

    
2944
                if (targetLMConnector != null)
2945
                {
2946
                    foreach (var attribute in specBreak.ATTRIBUTES)
2947
                    {
2948
                        AttributeMapping mapping = document.AttributeMappings.Find(x => x.UID == attribute.UID);
2949
                        if (mapping != null && !string.IsNullOrEmpty(mapping.SPPIDSYMBOLNAME) && mapping.SPPIDSYMBOLNAME != "None")
2950
                        {
2951
                            string MappingPath = mapping.SPPIDSYMBOLNAME;
2952
                            double[] point = GetSegmentPoint(upStreamObj, downStreamObj, targetLMConnector);
2953
                            Array array = null;
2954
                            if (point != null)
2955
                                array = new double[] { 0, point[0], point[1] };
2956
                            else
2957
                                array = new double[] { 0, specBreak.SPPID.ORIGINAL_X, specBreak.SPPID.ORIGINAL_Y };
2958
                            LMLabelPersist _LmLabelPersist = _placement.PIDPlaceLabel(MappingPath, ref array, LabeledItem: targetLMConnector.AsLMRepresentation(), IsLeaderVisible: mapping.LeaderLine);
2959

    
2960
                            if (_LmLabelPersist != null)
2961
                            {
2962
                                _LmLabelPersist.Commit();
2963
                                specBreak.SPPID.RepresentationId = _LmLabelPersist.AsLMRepresentation().Id;
2964
                                if (_LmLabelPersist.ModelItemObject != null)
2965
                                    specBreak.SPPID.ModelItemID = _LmLabelPersist.ModelItemID;
2966
                                specBreak.SPPID.GraphicOID = _LmLabelPersist.get_GraphicOID().ToString();
2967
                                ReleaseCOMObjects(_LmLabelPersist);
2968
                            }
2969
                        }
2970
                    }
2971
                    ReleaseCOMObjects(targetLMConnector);
2972
                }
2973
                else
2974
                {
2975
                    Log.Write("Spec Break UID : " + specBreak.UID);
2976
                    Log.Write("Can't find targetLMConnector");
2977
                }
2978
            }
2979
        }
2980

    
2981
        private LMConnector FindBreakLineTarget(object targetObj, object connectedObj)
2982
        {
2983
            LMConnector targetConnector = null;
2984
            Symbol targetSymbol = targetObj as Symbol;
2985
            Symbol connectedSymbol = connectedObj as Symbol;
2986
            Line targetLine = targetObj as Line;
2987
            Line connectedLine = connectedObj as Line;
2988
            if (targetSymbol != null && connectedSymbol != null)
2989
            {
2990
                LMSymbol targetLMSymbol = dataSource.GetSymbol(targetSymbol.SPPID.RepresentationId);
2991
                LMSymbol connectedLMSymbol = dataSource.GetSymbol(connectedSymbol.SPPID.RepresentationId);
2992

    
2993
                foreach (LMConnector connector in targetLMSymbol.Avoid1Connectors)
2994
                {
2995
                    if (connector.get_ItemStatus() != "Active")
2996
                        continue;
2997

    
2998
                    if (connector.ConnectItem1SymbolObject.Id == connectedLMSymbol.Id)
2999
                    {
3000
                        targetConnector = connector;
3001
                        break;
3002
                    }
3003
                    else if (connector.ConnectItem2SymbolObject.Id == connectedLMSymbol.Id)
3004
                    {
3005
                        targetConnector = connector;
3006
                        break;
3007
                    }
3008
                }
3009

    
3010
                foreach (LMConnector connector in targetLMSymbol.Avoid2Connectors)
3011
                {
3012
                    if (connector.get_ItemStatus() != "Active")
3013
                        continue;
3014

    
3015
                    if (connector.ConnectItem1SymbolObject.Id == connectedLMSymbol.Id)
3016
                    {
3017
                        targetConnector = connector;
3018
                        break;
3019
                    }
3020
                    else if (connector.ConnectItem2SymbolObject.Id == connectedLMSymbol.Id)
3021
                    {
3022
                        targetConnector = connector;
3023
                        break;
3024
                    }
3025
                }
3026

    
3027
                ReleaseCOMObjects(targetLMSymbol);
3028
                ReleaseCOMObjects(connectedLMSymbol);
3029
            }
3030
            else if (targetLine != null && connectedLine != null)
3031
            {
3032
                LMModelItem targetModelItem = dataSource.GetModelItem(targetLine.SPPID.ModelItemId);
3033
                LMModelItem connectedModelItem = dataSource.GetModelItem(connectedLine.SPPID.ModelItemId);
3034

    
3035
                if (targetModelItem != null && targetModelItem.get_ItemStatus() == "Active" && connectedModelItem != null && connectedModelItem.get_ItemStatus() == "Active")
3036
                {
3037
                    foreach (LMRepresentation rep in targetModelItem.Representations)
3038
                    {
3039
                        if (targetConnector != null)
3040
                            break;
3041

    
3042
                        if (rep.Attributes["RepresentationType"].get_Value() == "Connector" && rep.Attributes["ItemStatus"].get_Value() == "Active")
3043
                        {
3044
                            LMConnector _LMConnector = dataSource.GetConnector(rep.Id);
3045

    
3046
                            if (IsConnected(_LMConnector, connectedModelItem))
3047
                                targetConnector = _LMConnector;
3048
                            else
3049
                                ReleaseCOMObjects(_LMConnector);
3050
                        }
3051
                    }
3052

    
3053
                    ReleaseCOMObjects(targetModelItem);
3054
                }
3055
            }
3056
            else
3057
            {
3058
                LMSymbol connectedLMSymbol = null;
3059
                if (connectedSymbol != null)
3060
                    connectedLMSymbol = dataSource.GetSymbol(connectedSymbol.SPPID.RepresentationId);
3061
                else if (targetSymbol != null)
3062
                    connectedLMSymbol = dataSource.GetSymbol(targetSymbol.SPPID.RepresentationId);
3063
                else
3064
                {
3065

    
3066
                }
3067
                LMModelItem targetModelItem = null;
3068
                if (targetLine != null)
3069
                    targetModelItem = dataSource.GetModelItem(targetLine.SPPID.ModelItemId);
3070
                else if (connectedLine != null)
3071
                    targetModelItem = dataSource.GetModelItem(connectedLine.SPPID.ModelItemId);
3072
                else
3073
                {
3074

    
3075
                }
3076
                if (connectedLMSymbol != null && targetModelItem != null)
3077
                {
3078
                    foreach (LMConnector connector in connectedLMSymbol.Avoid1Connectors)
3079
                    {
3080
                        if (connector.get_ItemStatus() != "Active")
3081
                            continue;
3082

    
3083
                        if (IsConnected(connector, targetModelItem))
3084
                        {
3085
                            targetConnector = connector;
3086
                            break;
3087
                        }
3088
                    }
3089

    
3090
                    if (targetConnector == null)
3091
                    {
3092
                        foreach (LMConnector connector in connectedLMSymbol.Avoid2Connectors)
3093
                        {
3094
                            if (connector.get_ItemStatus() != "Active")
3095
                                continue;
3096

    
3097
                            if (IsConnected(connector, targetModelItem))
3098
                            {
3099
                                targetConnector = connector;
3100
                                break;
3101
                            }
3102
                        }
3103
                    }
3104
                }
3105

    
3106
            }
3107

    
3108
            return targetConnector;
3109
        }
3110

    
3111
        private double[] GetSegmentPoint(object targetObj, object connObj, LMConnector targetConnector)
3112
        {
3113
            double[] result = null;
3114
            Line targetLine = targetObj as Line;
3115
            Symbol targetSymbol = targetObj as Symbol;
3116
            Line connLine = connObj as Line;
3117
            Symbol connSymbol = connObj as Symbol;
3118

    
3119
            double zeroLengthMove = GridSetting.GetInstance().Length * 3;
3120
            double lineMove = GridSetting.GetInstance().Length * 3;
3121
            if (Convert.ToBoolean(targetConnector.get_IsZeroLength()))
3122
            {
3123
                result = GetConnectorVertices(targetConnector)[0];
3124
                if (targetSymbol != null && connSymbol != null)
3125
                {
3126
                    SlopeType slopeType = SPPIDUtil.CalcSlope(targetSymbol.SPPID.SPPID_X, targetSymbol.SPPID.SPPID_Y, connSymbol.SPPID.SPPID_X, connSymbol.SPPID.SPPID_Y);
3127
                    if (slopeType == SlopeType.HORIZONTAL)
3128
                        result = new double[] { result[0], result[1] - zeroLengthMove };
3129
                    else if (slopeType == SlopeType.VERTICAL)
3130
                        result = new double[] { result[0] + zeroLengthMove, result[1] };
3131
                }
3132
                else if (targetLine != null)
3133
                {
3134
                    if (targetLine.SlopeType == SlopeType.HORIZONTAL)
3135
                        result = new double[] { result[0], result[1] - zeroLengthMove };
3136
                    else if (targetLine.SlopeType == SlopeType.VERTICAL)
3137
                        result = new double[] { result[0] + zeroLengthMove, result[1] };
3138
                }
3139
                else if (connLine != null)
3140
                {
3141
                    if (connLine.SlopeType == SlopeType.HORIZONTAL)
3142
                        result = new double[] { result[0], result[1] - zeroLengthMove };
3143
                    else if (connLine.SlopeType == SlopeType.VERTICAL)
3144
                        result = new double[] { result[0] + zeroLengthMove, result[1] };
3145
                }
3146
            }
3147
            else
3148
            {
3149
                if (targetObj.GetType() == typeof(Line) && connObj.GetType() == typeof(Line))
3150
                {
3151
                    Line line = connObj as Line;
3152
                    LMConnector connectedConnector = null;
3153
                    int connIndex = 0;
3154
                    LMModelItem modelItem = dataSource.GetModelItem(line.SPPID.ModelItemId);
3155
                    FindConnectedConnector(targetConnector, modelItem, ref connectedConnector, ref connIndex);
3156

    
3157
                    List<double[]> vertices = GetConnectorVertices(targetConnector);
3158

    
3159
                    ReleaseCOMObjects(modelItem);
3160
                    ReleaseCOMObjects(connectedConnector);
3161

    
3162
                    if (vertices.Count > 0)
3163
                    {
3164
                        if (connIndex == 1)
3165
                            result = vertices[0];
3166
                        else if (connIndex == 2)
3167
                            result = vertices[vertices.Count - 1];
3168

    
3169
                        if (targetLine.SlopeType == SlopeType.HORIZONTAL)
3170
                        {
3171
                            result = new double[] { result[0], result[1] - lineMove };
3172
                            if (targetLine.CONNECTORS[0].CONNECTEDITEM == connLine.UID && targetLine.SPPID.START_X < targetLine.SPPID.END_X)
3173
                            {
3174
                                result = new double[] { result[0] - lineMove, result[1] };
3175
                            }
3176
                            else if (targetLine.CONNECTORS[0].CONNECTEDITEM == connLine.UID && targetLine.SPPID.START_X > targetLine.SPPID.END_X)
3177
                            {
3178
                                result = new double[] { result[0] + lineMove, result[1] };
3179
                            }
3180
                            else if (targetLine.CONNECTORS[1].CONNECTEDITEM == connLine.UID && targetLine.SPPID.START_X < targetLine.SPPID.END_X)
3181
                            {
3182
                                result = new double[] { result[0] + lineMove, result[1] };
3183
                            }
3184
                            else if (targetLine.CONNECTORS[1].CONNECTEDITEM == connLine.UID && targetLine.SPPID.START_X > targetLine.SPPID.END_X)
3185
                            {
3186
                                result = new double[] { result[0] - lineMove, result[1] };
3187
                            }
3188
                        }
3189
                        else if (targetLine.SlopeType == SlopeType.VERTICAL)
3190
                        {
3191
                            result = new double[] { result[0] - lineMove, result[1] };
3192
                            if (targetLine.CONNECTORS[0].CONNECTEDITEM == connLine.UID && targetLine.SPPID.START_Y < targetLine.SPPID.END_Y)
3193
                            {
3194
                                result = new double[] { result[0], result[1] - lineMove };
3195
                            }
3196
                            else if (targetLine.CONNECTORS[0].CONNECTEDITEM == connLine.UID && targetLine.SPPID.START_Y > targetLine.SPPID.END_Y)
3197
                            {
3198
                                result = new double[] { result[0], result[1] + lineMove };
3199
                            }
3200
                            else if (targetLine.CONNECTORS[1].CONNECTEDITEM == connLine.UID && targetLine.SPPID.START_Y < targetLine.SPPID.END_Y)
3201
                            {
3202
                                result = new double[] { result[0], result[1] + lineMove };
3203
                            }
3204
                            else if (targetLine.CONNECTORS[1].CONNECTEDITEM == connLine.UID && targetLine.SPPID.START_Y > targetLine.SPPID.END_Y)
3205
                            {
3206
                                result = new double[] { result[0], result[1] - lineMove };
3207
                            }
3208
                        }
3209
                            
3210
                    }
3211
                }
3212
                else
3213
                {
3214
                    Log.Write("error in GetSegemtPoint");
3215
                }
3216
            }
3217

    
3218
            return result;
3219
        }
3220

    
3221
        private bool IsConnected(LMConnector connector, LMModelItem modelItem)
3222
        {
3223
            bool result = false;
3224

    
3225
            foreach (LMRepresentation rep in modelItem.Representations)
3226
            {
3227
                if (result)
3228
                    break;
3229

    
3230
                if (rep.Attributes["RepresentationType"].get_Value() == "Connector" && rep.Attributes["ItemStatus"].get_Value() == "Active")
3231
                {
3232
                    LMConnector _LMConnector = dataSource.GetConnector(rep.Id);
3233

    
3234
                    if (_LMConnector.ConnectItem1SymbolObject != null &&
3235
                        connector.ConnectItem1SymbolObject != null &&
3236
                        _LMConnector.ConnectItem1SymbolObject.Id == connector.ConnectItem1SymbolObject.Id)
3237
                    {
3238
                        result = true;
3239
                        ReleaseCOMObjects(_LMConnector);
3240
                        break;
3241
                    }
3242
                    else if (_LMConnector.ConnectItem1SymbolObject != null &&
3243
                        connector.ConnectItem2SymbolObject != null &&
3244
                        _LMConnector.ConnectItem1SymbolObject.Id == connector.ConnectItem2SymbolObject.Id)
3245
                    {
3246
                        result = true;
3247
                        ReleaseCOMObjects(_LMConnector);
3248
                        break;
3249
                    }
3250
                    else if (_LMConnector.ConnectItem2SymbolObject != null &&
3251
                        connector.ConnectItem1SymbolObject != null &&
3252
                        _LMConnector.ConnectItem2SymbolObject.Id == connector.ConnectItem1SymbolObject.Id)
3253
                    {
3254
                        result = true;
3255
                        ReleaseCOMObjects(_LMConnector);
3256
                        break;
3257
                    }
3258
                    else if (_LMConnector.ConnectItem2SymbolObject != null &&
3259
                        connector.ConnectItem2SymbolObject != null &&
3260
                        _LMConnector.ConnectItem2SymbolObject.Id == connector.ConnectItem2SymbolObject.Id)
3261
                    {
3262
                        result = true;
3263
                        ReleaseCOMObjects(_LMConnector);
3264
                        break;
3265
                    }
3266

    
3267
                    ReleaseCOMObjects(_LMConnector);
3268
                }
3269
            }
3270

    
3271

    
3272
            return result;
3273
        }
3274

    
3275
        private void FindConnectedConnector(LMConnector connector, LMModelItem modelItem, ref LMConnector connectedConnector, ref int connectorIndex)
3276
        {
3277
            foreach (LMRepresentation rep in modelItem.Representations)
3278
            {
3279
                if (connectedConnector != null)
3280
                    break;
3281

    
3282
                if (rep.Attributes["RepresentationType"].get_Value() == "Connector" && rep.Attributes["ItemStatus"].get_Value() == "Active")
3283
                {
3284
                    LMConnector _LMConnector = dataSource.GetConnector(rep.Id);
3285

    
3286
                    if (_LMConnector.ConnectItem1SymbolObject != null &&
3287
                        connector.ConnectItem1SymbolObject != null &&
3288
                        _LMConnector.ConnectItem1SymbolObject.Id == connector.ConnectItem1SymbolObject.Id)
3289
                    {
3290
                        connectedConnector = _LMConnector;
3291
                        connectorIndex = 1;
3292
                        break;
3293
                    }
3294
                    else if (_LMConnector.ConnectItem1SymbolObject != null &&
3295
                        connector.ConnectItem2SymbolObject != null &&
3296
                        _LMConnector.ConnectItem1SymbolObject.Id == connector.ConnectItem2SymbolObject.Id)
3297
                    {
3298
                        connectedConnector = _LMConnector;
3299
                        connectorIndex = 2;
3300
                        break;
3301
                    }
3302
                    else if (_LMConnector.ConnectItem2SymbolObject != null &&
3303
                        connector.ConnectItem1SymbolObject != null &&
3304
                        _LMConnector.ConnectItem2SymbolObject.Id == connector.ConnectItem1SymbolObject.Id)
3305
                    {
3306
                        connectedConnector = _LMConnector;
3307
                        connectorIndex = 1;
3308
                        break;
3309
                    }
3310
                    else if (_LMConnector.ConnectItem2SymbolObject != null &&
3311
                        connector.ConnectItem2SymbolObject != null &&
3312
                        _LMConnector.ConnectItem2SymbolObject.Id == connector.ConnectItem2SymbolObject.Id)
3313
                    {
3314
                        connectedConnector = _LMConnector;
3315
                        connectorIndex = 2;
3316
                        break;
3317
                    }
3318

    
3319
                    if (connectedConnector == null)
3320
                        ReleaseCOMObjects(_LMConnector);
3321
                }
3322
            }
3323
        }
3324

    
3325
        /// <summary>
3326
        /// FromModelItem을 ToModelItem으로 PipeRunJoin하는 메서드
3327
        /// </summary>
3328
        /// <param name="modelItemID1"></param>
3329
        /// <param name="modelItemID2"></param>
3330
        private void JoinRun(string modelId1, string modelId2, ref string survivorId, bool IsSameConnector = true)
3331
        {
3332
            try
3333
            {
3334
                LMModelItem modelItem1 = dataSource.GetModelItem(modelId1);
3335
                LMConnector connector1 = GetLMConnectorFirst(modelId1);
3336
                List<double[]> vertices1 = null;
3337
                string graphicOID1 = string.Empty;
3338
                if (connector1 != null)
3339
                {
3340
                    vertices1 = GetConnectorVertices(connector1);
3341
                    graphicOID1 = connector1.get_GraphicOID();
3342
                }
3343
                _LMAItem item1 = modelItem1.AsLMAItem();
3344
                ReleaseCOMObjects(connector1);
3345
                connector1 = null;
3346

    
3347
                LMModelItem modelItem2 = dataSource.GetModelItem(modelId2);
3348
                LMConnector connector2 = GetLMConnectorFirst(modelId2);
3349
                List<double[]> vertices2 = null;
3350
                string graphicOID2 = string.Empty;
3351
                if (connector2 != null)
3352
                {
3353
                    vertices2 = GetConnectorVertices(connector2);
3354
                    graphicOID2 = connector2.get_GraphicOID();
3355
                }
3356
                _LMAItem item2 = modelItem2.AsLMAItem();
3357
                ReleaseCOMObjects(connector2);
3358
                connector2 = null;
3359

    
3360
                // item2가 item1으로 조인
3361
                _placement.PIDJoinRuns(ref item1, ref item2);
3362
                item1.Commit();
3363
                item2.Commit();
3364

    
3365
                string beforeID = string.Empty;
3366
                string afterID = string.Empty;
3367

    
3368
                if (modelItem1.get_ItemStatus() == "Active" && modelItem2.get_ItemStatus() != "Active")
3369
                {
3370
                    beforeID = modelItem2.Id;
3371
                    afterID = modelItem1.Id;
3372
                    survivorId = afterID;
3373
                }
3374
                else if (modelItem1.get_ItemStatus() != "Active" && modelItem2.get_ItemStatus() == "Active")
3375
                {
3376
                    beforeID = modelItem1.Id;
3377
                    afterID = modelItem2.Id;
3378
                    survivorId = afterID;
3379
                }
3380
                else if (modelItem1.get_ItemStatus() == "Active" && modelItem2.get_ItemStatus() == "Active")
3381
                {
3382
                    int model1Cnt = GetConnectorCount(modelId1);
3383
                    int model2Cnt = GetConnectorCount(modelId2);
3384
                    if (model1Cnt == 0)
3385
                    {
3386
                        beforeID = modelItem1.Id;
3387
                        afterID = modelItem2.Id;
3388
                        survivorId = afterID;
3389
                    }
3390
                    else if (model2Cnt == 0)
3391
                    {
3392
                        beforeID = modelItem2.Id;
3393
                        afterID = modelItem1.Id;
3394
                        survivorId = afterID;
3395
                    }
3396
                    else
3397
                        survivorId = null;
3398
                }
3399
                else
3400
                {
3401
                    Log.Write("잘못된 경우");
3402
                    survivorId = null;
3403
                }
3404

    
3405
                if (!string.IsNullOrEmpty(beforeID) && !string.IsNullOrEmpty(afterID))
3406
                {
3407
                    List<Line> lines = SPPIDUtil.FindLinesByModelId(document, beforeID);
3408
                    foreach (var line in lines)
3409
                        line.SPPID.ModelItemId = afterID;
3410
                }
3411

    
3412
                ReleaseCOMObjects(modelItem1);
3413
                ReleaseCOMObjects(item1);
3414
                ReleaseCOMObjects(modelItem2);
3415
                ReleaseCOMObjects(item2);
3416
            }
3417
            catch (Exception ex)
3418
            {
3419
                Log.Write("Join Error");
3420
                Log.Write(ex.Message + "\r\n" + ex.StackTrace);
3421
            }
3422
        }
3423

    
3424
        private List<string> FindOtherModelItemBySymbolWhereTypePipeRun(LMSymbol symbol, string modelId)
3425
        {
3426
            List<string> temp = new List<string>();
3427
            List<LMConnector> connectors = new List<LMConnector>();
3428
            foreach (LMConnector connector in symbol.Avoid1Connectors)
3429
            {
3430
                if (connector.get_ItemStatus() != "Active")
3431
                    continue;
3432

    
3433
                LMModelItem modelItem = connector.ModelItemObject;
3434
                LMSymbol connOtherSymbol = FindOtherConnectedSymbol(connector);
3435
                if (modelItem.get_ItemStatus() == "Active" && modelItem.get_ItemTypeName().ToString() == "PipeRun" && modelItem.Id != modelId && !temp.Contains(modelItem.Id))
3436
                    temp.Add(modelItem.Id);
3437

    
3438
                if (temp.Contains(modelItem.Id) &&
3439
                    connOtherSymbol != null &&
3440
                    connOtherSymbol.get_RepresentationType() == "Branch" &&
3441
                    Convert.ToBoolean(connector.get_IsZeroLength()))
3442
                    temp.Remove(modelItem.Id);
3443

    
3444

    
3445
                if (temp.Contains(modelItem.Id))
3446
                    connectors.Add(connector);
3447
                ReleaseCOMObjects(connOtherSymbol);
3448
                connOtherSymbol = null;
3449
                ReleaseCOMObjects(modelItem);
3450
                modelItem = null;
3451
            }
3452

    
3453
            foreach (LMConnector connector in symbol.Avoid2Connectors)
3454
            {
3455
                if (connector.get_ItemStatus() != "Active")
3456
                    continue;
3457

    
3458
                LMModelItem modelItem = connector.ModelItemObject;
3459
                LMSymbol connOtherSymbol = FindOtherConnectedSymbol(connector);
3460
                if (modelItem.get_ItemStatus() == "Active" && modelItem.get_ItemTypeName().ToString() == "PipeRun" && modelItem.Id != modelId && !temp.Contains(modelItem.Id))
3461
                    temp.Add(modelItem.Id);
3462

    
3463
                if (temp.Contains(modelItem.Id) &&
3464
                    connOtherSymbol != null &&
3465
                    connOtherSymbol.get_RepresentationType() == "Branch" &&
3466
                    Convert.ToBoolean(connector.get_IsZeroLength()))
3467
                    temp.Remove(modelItem.Id);
3468

    
3469
                if (temp.Contains(modelItem.Id))
3470
                    connectors.Add(connector);
3471
                ReleaseCOMObjects(connOtherSymbol);
3472
                connOtherSymbol = null;
3473
                ReleaseCOMObjects(modelItem);
3474
                modelItem = null;
3475
            }
3476

    
3477

    
3478
            List<string> result = new List<string>();
3479
            string originalName = GetSPPIDFileName(modelId);
3480
            foreach (var connector in connectors)
3481
            {
3482
                string fileName = GetSPPIDFileName(connector.ModelItemID);
3483
                if (originalName == fileName)
3484
                    result.Add(connector.ModelItemID);
3485
                else
3486
                {
3487
                    if (document.LINES.Find(x => x.SPPID.ModelItemId == connector.ModelItemID) == null && Convert.ToBoolean(connector.get_IsZeroLength()))
3488
                        result.Add(connector.ModelItemID);
3489
                    else
3490
                    {
3491
                        Line line1 = document.LINES.Find(x => x.SPPID.ModelItemId == modelId);
3492
                        Line line2 = document.LINES.Find(x => x.SPPID.ModelItemId == connector.ModelItemID.ToString());
3493
                        if (line1 != null && line2 != null && line1.TYPE == line2.TYPE)
3494
                            result.Add(connector.ModelItemID);
3495
                    }
3496
                }
3497
            }
3498

    
3499
            foreach (var connector in connectors)
3500
                ReleaseCOMObjects(connector);
3501
            
3502
            return result;
3503

    
3504

    
3505
            LMSymbol FindOtherConnectedSymbol(LMConnector connector)
3506
            {
3507
                LMSymbol findResult = null;
3508
                if (connector.ConnectItem1SymbolObject != null && connector.ConnectItem1SymbolObject.Id != symbol.Id && connector.ConnectItem1SymbolObject.get_ItemStatus() == "Active")
3509
                    findResult = connector.ConnectItem1SymbolObject;
3510
                else if (connector.ConnectItem2SymbolObject != null && connector.ConnectItem2SymbolObject.Id != symbol.Id && connector.ConnectItem2SymbolObject.get_ItemStatus() == "Active")
3511
                    findResult = connector.ConnectItem2SymbolObject;
3512

    
3513
                return findResult;
3514
            }
3515
        }
3516

    
3517
        /// <summary>
3518
        /// PipeRun의 좌표를 가져오는 메서드
3519
        /// </summary>
3520
        /// <param name="modelId"></param>
3521
        /// <returns></returns>
3522
        private Dictionary<LMConnector, List<double[]>> GetPipeRunVertices(string modelId, bool ContainZeroLength = true)
3523
        {
3524
            Dictionary<LMConnector, List<double[]>> connectorVertices = new Dictionary<LMConnector, List<double[]>>();
3525
            LMModelItem modelItem = dataSource.GetModelItem(modelId);
3526

    
3527
            if (modelItem != null)
3528
            {
3529
                foreach (LMRepresentation rep in modelItem.Representations)
3530
                {
3531
                    if (rep.Attributes["RepresentationType"].get_Value() == "Connector" && rep.Attributes["ItemStatus"].get_Value() == "Active")
3532
                    {
3533
                        LMConnector _LMConnector = dataSource.GetConnector(rep.Id);
3534
                        if (!ContainZeroLength && Convert.ToBoolean(_LMConnector.get_IsZeroLength()))
3535
                        {
3536
                            ReleaseCOMObjects(_LMConnector);
3537
                            _LMConnector = null;
3538
                            continue;
3539
                        }
3540
                        connectorVertices.Add(_LMConnector, new List<double[]>());
3541
                        dynamic OID = rep.get_GraphicOID().ToString();
3542
                        DependencyObject drawingObject = radApp.ActiveDocument.ActiveSheet.DrawingObjects[OID];
3543
                        Ingr.RAD2D.LineStringGeometry2d lineStringGeometry = drawingObject.GetGeometry() as Ingr.RAD2D.LineStringGeometry2d;
3544
                        int verticesCount = lineStringGeometry.VertexCount;
3545
                        double[] vertices = null;
3546
                        lineStringGeometry.GetVertices(ref verticesCount, ref vertices);
3547
                        for (int i = 0; i < verticesCount; i++)
3548
                        {
3549
                            double x = 0;
3550
                            double y = 0;
3551
                            lineStringGeometry.GetVertex(i + 1, ref x, ref y);
3552
                            connectorVertices[_LMConnector].Add(new double[] { x, y });
3553
                        }
3554
                    }
3555
                }
3556

    
3557
                ReleaseCOMObjects(modelItem);
3558
            }
3559

    
3560
            return connectorVertices;
3561
        }
3562

    
3563
        private List<double[]> GetConnectorVertices(LMConnector connector)
3564
        {
3565
            List<double[]> vertices = new List<double[]>();
3566
            if (connector != null)
3567
            {
3568
                dynamic OID = connector.get_GraphicOID().ToString();
3569
                DependencyObject drawingObject = radApp.ActiveDocument.ActiveSheet.DrawingObjects[OID];
3570
                Ingr.RAD2D.LineStringGeometry2d lineStringGeometry = drawingObject.GetGeometry() as Ingr.RAD2D.LineStringGeometry2d;
3571
                int verticesCount = lineStringGeometry.VertexCount;
3572
                double[] value = null;
3573
                lineStringGeometry.GetVertices(ref verticesCount, ref value);
3574
                for (int i = 0; i < verticesCount; i++)
3575
                {
3576
                    double x = 0;
3577
                    double y = 0;
3578
                    lineStringGeometry.GetVertex(i + 1, ref x, ref y);
3579
                    vertices.Add(new double[] { x, y });
3580
                }
3581
            }
3582
            return vertices;
3583
        }
3584

    
3585
        private double GetConnectorDistance(LMConnector connector)
3586
        {
3587
            double result = 0;
3588
            List<double[]> vertices = new List<double[]>();
3589
            if (connector != null)
3590
            {
3591
                dynamic OID = connector.get_GraphicOID().ToString();
3592
                DependencyObject drawingObject = radApp.ActiveDocument.ActiveSheet.DrawingObjects[OID];
3593
                Ingr.RAD2D.LineStringGeometry2d lineStringGeometry = drawingObject.GetGeometry() as Ingr.RAD2D.LineStringGeometry2d;
3594
                int verticesCount = lineStringGeometry.VertexCount;
3595
                double[] value = null;
3596
                lineStringGeometry.GetVertices(ref verticesCount, ref value);
3597
                for (int i = 0; i < verticesCount; i++)
3598
                {
3599
                    double x = 0;
3600
                    double y = 0;
3601
                    lineStringGeometry.GetVertex(i + 1, ref x, ref y);
3602
                    vertices.Add(new double[] { x, y });
3603
                    if (vertices.Count > 1)
3604
                    {
3605
                        result += SPPIDUtil.CalcPointToPointdDistance(vertices[vertices.Count - 2][0], vertices[vertices.Count - 2][1], x, y);
3606
                    }
3607
                }
3608
            }
3609
            return result;
3610
        }
3611
        private List<double[]> GetConnectorVertices(dynamic graphicOID)
3612
        {
3613
            List<double[]> vertices = null;
3614
            DependencyObject drawingObject = radApp.ActiveDocument.ActiveSheet.DrawingObjects[graphicOID];
3615
            if (drawingObject != null)
3616
            {
3617
                vertices = new List<double[]>();
3618
                Ingr.RAD2D.LineStringGeometry2d lineStringGeometry = drawingObject.GetGeometry() as Ingr.RAD2D.LineStringGeometry2d;
3619
                int verticesCount = lineStringGeometry.VertexCount;
3620
                double[] value = null;
3621
                lineStringGeometry.GetVertices(ref verticesCount, ref value);
3622
                for (int i = 0; i < verticesCount; i++)
3623
                {
3624
                    double x = 0;
3625
                    double y = 0;
3626
                    lineStringGeometry.GetVertex(i + 1, ref x, ref y);
3627
                    vertices.Add(new double[] { x, y });
3628
                }
3629
            }
3630
            return vertices;
3631
        }
3632
        /// <summary>
3633
        /// 좌표로 PipeRun의 Connector중에 어느 Connector에 가까운지/붙을지 가져오는 메서드 - 조건에 안맞아서 못찾을시 제일 가까운 점으로 가져오는 방식
3634
        /// </summary>
3635
        /// <param name="connectorVertices"></param>
3636
        /// <param name="connX"></param>
3637
        /// <param name="connY"></param>
3638
        /// <returns></returns>
3639
        private LMConnector FindTargetLMConnectorForLabel(Dictionary<LMConnector, List<double[]>> connectorVertices, double connX, double connY)
3640
        {
3641
            double length = double.MaxValue;
3642
            LMConnector targetConnector = null;
3643
            foreach (var item in connectorVertices)
3644
            {
3645
                List<double[]> points = item.Value;
3646
                for (int i = 0; i < points.Count - 1; i++)
3647
                {
3648
                    double[] point1 = points[i];
3649
                    double[] point2 = points[i + 1];
3650
                    double x1 = Math.Min(point1[0], point2[0]);
3651
                    double y1 = Math.Min(point1[1], point2[1]);
3652
                    double x2 = Math.Max(point1[0], point2[0]);
3653
                    double y2 = Math.Max(point1[1], point2[1]);
3654

    
3655
                    if ((x1 <= connX && x2 >= connX) ||
3656
                        (y1 <= connY && y2 >= connY))
3657
                    {
3658
                        double distance = SPPIDUtil.CalcPointToPointdDistance(point1[0], point1[1], connX, connY);
3659
                        if (length >= distance)
3660
                        {
3661
                            targetConnector = item.Key;
3662
                            length = distance;
3663
                        }
3664

    
3665
                        distance = SPPIDUtil.CalcPointToPointdDistance(point2[0], point2[1], connX, connY);
3666
                        if (length >= distance)
3667
                        {
3668
                            targetConnector = item.Key;
3669
                            length = distance;
3670
                        }
3671
                    }
3672
                }
3673
            }
3674

    
3675
            // 못찾았을때.
3676
            length = double.MaxValue;
3677
            if (targetConnector == null)
3678
            {
3679
                foreach (var item in connectorVertices)
3680
                {
3681
                    List<double[]> points = item.Value;
3682

    
3683
                    foreach (double[] point in points)
3684
                    {
3685
                        double distance = SPPIDUtil.CalcPointToPointdDistance(point[0], point[1], connX, connY);
3686
                        if (length >= distance)
3687
                        {
3688
                            targetConnector = item.Key;
3689
                            length = distance;
3690
                        }
3691
                    }
3692
                }
3693
            }
3694

    
3695
            return targetConnector;
3696
        }
3697

    
3698
        private LMConnector FindTargetLMConnectorForBranch(Line line, Line targetLine, ref double x, ref double y)
3699
        {
3700
            Dictionary<LMConnector, List<double[]>> vertices = GetPipeRunVertices(targetLine.SPPID.ModelItemId);
3701
            if (vertices.Count == 0)
3702
                return null;
3703

    
3704
            double length = double.MaxValue;
3705
            LMConnector targetConnector = null;
3706
            double[] resultPoint = null;
3707
            List<double[]> targetVertices = null;
3708

    
3709
            // Vertices 포인트에 제일 가까운곳
3710
            foreach (var item in vertices)
3711
            {
3712
                List<double[]> points = item.Value;
3713
                for (int i = 0; i < points.Count; i++)
3714
                {
3715
                    double[] point = points[i];
3716
                    double tempX = point[0];
3717
                    double tempY = point[1];
3718

    
3719
                    double distance = SPPIDUtil.CalcPointToPointdDistance(tempX, tempY, x, y);
3720
                    if (length >= distance)
3721
                    {
3722
                        targetConnector = item.Key;
3723
                        length = distance;
3724
                        resultPoint = point;
3725
                        targetVertices = item.Value;
3726
                    }
3727
                }
3728
            }
3729

    
3730
            // Vertices Cross에 제일 가까운곳
3731
            foreach (var item in vertices)
3732
            {
3733
                List<double[]> points = item.Value;
3734
                for (int i = 0; i < points.Count - 1; i++)
3735
                {
3736
                    double[] point1 = points[i];
3737
                    double[] point2 = points[i + 1];
3738

    
3739
                    double maxLineX = Math.Max(point1[0], point2[0]);
3740
                    double minLineX = Math.Min(point1[0], point2[0]);
3741
                    double maxLineY = Math.Max(point1[1], point2[1]);
3742
                    double minLineY = Math.Min(point1[1], point2[1]);
3743

    
3744
                    SlopeType slope = SPPIDUtil.CalcSlope(minLineX, minLineY, maxLineX, maxLineY);
3745

    
3746
                    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]);
3747
                    if (crossingPoint != null)
3748
                    {
3749
                        double distance = SPPIDUtil.CalcPointToPointdDistance(crossingPoint[0], crossingPoint[1], x, y);
3750
                        if (length >= distance)
3751
                        {
3752
                            if (slope == SlopeType.Slope &&
3753
                                minLineX <= crossingPoint[0] && maxLineX >= crossingPoint[0] &&
3754
                                minLineY <= crossingPoint[1] && maxLineY >= crossingPoint[1])
3755
                            {
3756
                                targetConnector = item.Key;
3757
                                length = distance;
3758
                                resultPoint = crossingPoint;
3759
                                targetVertices = item.Value;
3760
                            }
3761
                            else if (slope == SlopeType.HORIZONTAL &&
3762
                                minLineX <= crossingPoint[0] && maxLineX >= crossingPoint[0])
3763
                            {
3764
                                targetConnector = item.Key;
3765
                                length = distance;
3766
                                resultPoint = crossingPoint;
3767
                                targetVertices = item.Value;
3768
                            }
3769
                            else if (slope == SlopeType.VERTICAL &&
3770
                               minLineY <= crossingPoint[1] && maxLineY >= crossingPoint[1])
3771
                            {
3772
                                targetConnector = item.Key;
3773
                                length = distance;
3774
                                resultPoint = crossingPoint;
3775
                                targetVertices = item.Value;
3776
                            }
3777
                        }
3778
                    }
3779
                }
3780
            }
3781

    
3782
            foreach (var item in vertices)
3783
                if (item.Key != null && item.Key != targetConnector)
3784
                    ReleaseCOMObjects(item.Key);
3785

    
3786
            if (SPPIDUtil.IsBranchLine(line, targetLine))
3787
            {
3788
                double tempResultX = resultPoint[0];
3789
                double tempResultY = resultPoint[1];
3790
                SPPIDUtil.ConvertGridPoint(ref tempResultX, ref tempResultY);
3791

    
3792
                GridSetting gridSetting = GridSetting.GetInstance();
3793

    
3794
                for (int i = 0; i < targetVertices.Count; i++)
3795
                {
3796
                    double[] point = targetVertices[i];
3797
                    double tempX = targetVertices[i][0];
3798
                    double tempY = targetVertices[i][1];
3799
                    SPPIDUtil.ConvertGridPoint(ref tempX, ref tempY);
3800
                    if (tempX == tempResultX && tempY == tempResultY)
3801
                    {
3802
                        if (i == 0)
3803
                        {
3804
                            LMSymbol connSymbol = targetConnector.ConnectItem1SymbolObject;
3805
                            bool containZeroLength = false;
3806
                            if (connSymbol != null)
3807
                            {
3808
                                foreach (LMConnector connector in connSymbol.Connect1Connectors)
3809
                                {
3810
                                    if (connector.get_ItemStatus() == "Active" && Convert.ToBoolean(connector.get_IsZeroLength()) == true)
3811
                                        containZeroLength = true;
3812
                                }
3813
                                foreach (LMConnector connector in connSymbol.Connect2Connectors)
3814
                                {
3815
                                    if (connector.get_ItemStatus() == "Active" && Convert.ToBoolean(connector.get_IsZeroLength()) == true)
3816
                                        containZeroLength = true;
3817
                                }
3818
                            }
3819

    
3820
                            if (connSymbol == null ||
3821
                                (connSymbol != null && connSymbol.get_ItemStatus() == "Active" && connSymbol.get_RepresentationType() != "Branch") ||
3822
                                containZeroLength)
3823
                            {
3824
                                bool bCalcX = false;
3825
                                bool bCalcY = false;
3826
                                if (targetLine.SlopeType == SlopeType.HORIZONTAL)
3827
                                    bCalcX = true;
3828
                                else if (targetLine.SlopeType == SlopeType.VERTICAL)
3829
                                    bCalcY = true;
3830
                                else
3831
                                {
3832
                                    bCalcX = true;
3833
                                    bCalcY = true;
3834
                                }
3835

    
3836
                                if (bCalcX)
3837
                                {
3838
                                    double nextX = targetVertices[i + 1][0];
3839
                                    double newX = 0;
3840
                                    if (nextX > tempX)
3841
                                    {
3842
                                        newX = tempX + gridSetting.Length;
3843
                                        if (newX > nextX)
3844
                                            newX = (point[0] + nextX) / 2;
3845
                                    }
3846
                                    else
3847
                                    {
3848
                                        newX = tempX - gridSetting.Length;
3849
                                        if (newX < nextX)
3850
                                            newX = (point[0] + nextX) / 2;
3851
                                    }
3852
                                    resultPoint = new double[] { newX, resultPoint[1] };
3853
                                }
3854

    
3855
                                if (bCalcY)
3856
                                {
3857
                                    double nextY = targetVertices[i + 1][1];
3858
                                    double newY = 0;
3859
                                    if (nextY > tempY)
3860
                                    {
3861
                                        newY = tempY + gridSetting.Length;
3862
                                        if (newY > nextY)
3863
                                            newY = (point[1] + nextY) / 2;
3864
                                    }
3865
                                    else
3866
                                    {
3867
                                        newY = tempY - gridSetting.Length;
3868
                                        if (newY < nextY)
3869
                                            newY = (point[1] + nextY) / 2;
3870
                                    }
3871
                                    resultPoint = new double[] { resultPoint[0], newY };
3872
                                }
3873
                            }
3874
                        }
3875
                        else if (i == targetVertices.Count - 1)
3876
                        {
3877
                            LMSymbol connSymbol = targetConnector.ConnectItem2SymbolObject;
3878
                            bool containZeroLength = false;
3879
                            if (connSymbol != null)
3880
                            {
3881
                                foreach (LMConnector connector in connSymbol.Connect1Connectors)
3882
                                {
3883
                                    if (connector.get_ItemStatus() == "Active" && Convert.ToBoolean(connector.get_IsZeroLength()) == true)
3884
                                        containZeroLength = true;
3885
                                }
3886
                                foreach (LMConnector connector in connSymbol.Connect2Connectors)
3887
                                {
3888
                                    if (connector.get_ItemStatus() == "Active" && Convert.ToBoolean(connector.get_IsZeroLength()) == true)
3889
                                        containZeroLength = true;
3890
                                }
3891
                            }
3892

    
3893
                            if (connSymbol == null ||
3894
                                 (connSymbol != null && connSymbol.get_ItemStatus() == "Active" && connSymbol.get_RepresentationType() != "Branch") ||
3895
                                containZeroLength)
3896
                            {
3897
                                bool bCalcX = false;
3898
                                bool bCalcY = false;
3899
                                if (targetLine.SlopeType == SlopeType.HORIZONTAL)
3900
                                    bCalcX = true;
3901
                                else if (targetLine.SlopeType == SlopeType.VERTICAL)
3902
                                    bCalcY = true;
3903
                                else
3904
                                {
3905
                                    bCalcX = true;
3906
                                    bCalcY = true;
3907
                                }
3908

    
3909
                                if (bCalcX)
3910
                                {
3911
                                    double nextX = targetVertices[i - 1][0];
3912
                                    double newX = 0;
3913
                                    if (nextX > tempX)
3914
                                    {
3915
                                        newX = tempX + gridSetting.Length;
3916
                                        if (newX > nextX)
3917
                                            newX = (point[0] + nextX) / 2;
3918
                                    }
3919
                                    else
3920
                                    {
3921
                                        newX = tempX - gridSetting.Length;
3922
                                        if (newX < nextX)
3923
                                            newX = (point[0] + nextX) / 2;
3924
                                    }
3925
                                    resultPoint = new double[] { newX, resultPoint[1] };
3926
                                }
3927

    
3928
                                if (bCalcY)
3929
                                {
3930
                                    double nextY = targetVertices[i - 1][1];
3931
                                    double newY = 0;
3932
                                    if (nextY > tempY)
3933
                                    {
3934
                                        newY = tempY + gridSetting.Length;
3935
                                        if (newY > nextY)
3936
                                            newY = (point[1] + nextY) / 2;
3937
                                    }
3938
                                    else
3939
                                    {
3940
                                        newY = tempY - gridSetting.Length;
3941
                                        if (newY < nextY)
3942
                                            newY = (point[1] + nextY) / 2;
3943
                                    }
3944
                                    resultPoint = new double[] { resultPoint[0], newY };
3945
                                }
3946
                            }
3947
                        }
3948
                        break;
3949
                    }
3950
                }
3951
            }
3952

    
3953
            x = resultPoint[0];
3954
            y = resultPoint[1];
3955

    
3956
            return targetConnector;
3957
        }
3958

    
3959
        private LMConnector GetLMConnectorOnlyOne(string modelItemID)
3960
        {
3961
            LMConnector result = null;
3962
            List<LMConnector> connectors = new List<LMConnector>();
3963
            LMModelItem modelItem = dataSource.GetModelItem(modelItemID);
3964

    
3965
            if (modelItem != null)
3966
            {
3967
                foreach (LMRepresentation rep in modelItem.Representations)
3968
                {
3969
                    if (rep.Attributes["RepresentationType"].get_Value() == "Connector" && rep.Attributes["ItemStatus"].get_Value() == "Active")
3970
                        connectors.Add(dataSource.GetConnector(rep.Id));
3971
                }
3972

    
3973
                ReleaseCOMObjects(modelItem);
3974
            }
3975

    
3976
            if (connectors.Count == 1)
3977
                result = connectors[0];
3978
            else
3979
                foreach (var item in connectors)
3980
                    ReleaseCOMObjects(item);
3981

    
3982
            return result;
3983
        }
3984

    
3985
        private LMConnector GetLMConnectorFirst(string modelItemID)
3986
        {
3987
            LMConnector result = null;
3988
            LMModelItem modelItem = dataSource.GetModelItem(modelItemID);
3989

    
3990
            if (modelItem != null)
3991
            {
3992
                foreach (LMRepresentation rep in modelItem.Representations)
3993
                {
3994
                    if (rep.Attributes["RepresentationType"].get_Value() == "Connector" && 
3995
                        rep.Attributes["ItemStatus"].get_Value() == "Active")
3996
                    {
3997
                        LMConnector connector = dataSource.GetConnector(rep.Id);
3998
                        if (!Convert.ToBoolean(connector.get_IsZeroLength()))
3999
                        {
4000
                            result = connector;
4001
                            break;
4002
                        }
4003
                        else
4004
                        {
4005
                            ReleaseCOMObjects(connector);
4006
                            connector = null;
4007
                        }
4008
                    }
4009
                }
4010
                ReleaseCOMObjects(modelItem);
4011
                modelItem = null;
4012
            }
4013

    
4014
            return result;
4015
        }
4016

    
4017
        private int GetConnectorCount(string modelItemID)
4018
        {
4019
            LMModelItem modelItem = dataSource.GetModelItem(modelItemID);
4020
            int result = 0;
4021
            if (modelItem != null)
4022
            {
4023
                foreach (LMRepresentation rep in modelItem.Representations)
4024
                {
4025
                    if (rep.Attributes["RepresentationType"].get_Value() == "Connector" && rep.Attributes["ItemStatus"].get_Value() == "Active")
4026
                        result++;
4027
                    ReleaseCOMObjects(rep);
4028
                }
4029
                ReleaseCOMObjects(modelItem);
4030
            }
4031

    
4032
            return result;
4033
        }
4034

    
4035
        public List<string> GetRepresentations(string modelItemID)
4036
        {
4037
            List<string> result = new List<string>(); ;
4038
            LMModelItem modelItem = dataSource.GetModelItem(modelItemID);
4039
            if (modelItem != null)
4040
            {
4041
                foreach (LMRepresentation rep in modelItem.Representations)
4042
                {
4043
                    if (rep.Attributes["RepresentationType"].get_Value() == "Connector" && rep.Attributes["ItemStatus"].get_Value() == "Active")
4044
                        result.Add(rep.Id);
4045
                }
4046
                ReleaseCOMObjects(modelItem);
4047
            }
4048

    
4049
            return result;
4050
        }
4051

    
4052
        /// <summary>
4053
        /// Line Number Symbol을 실제로 Modeling하는 메서드
4054
        /// </summary>
4055
        /// <param name="lineNumber"></param>
4056
        private void LineNumberModelingOnlyOne(Line line)
4057
        {
4058
            LineNumber lineNumber = document.LINENUMBERS.Find(x => x.CONNLINE == line.UID);
4059
            if (lineNumber != null)
4060
            {
4061
                LMConnector connectedLMConnector = GetLMConnectorOnlyOne(line.SPPID.ModelItemId);
4062
                if (connectedLMConnector != null)
4063
                {
4064
                    double x = 0;
4065
                    double y = 0;
4066
                    CalcLabelLocation(ref x, ref y, lineNumber.SPPID.ORIGINAL_X, lineNumber.SPPID.ORIGINAL_Y, lineNumber.SPPIDLabelLocation, _ETCSetting.LineNumberLocation);
4067

    
4068
                    Array points = new double[] { 0, x, y };
4069
                    lineNumber.SPPID.SPPID_X = x;
4070
                    lineNumber.SPPID.SPPID_Y = y;
4071
                    LMLabelPersist _LmLabelPresist = _placement.PIDPlaceLabel(lineNumber.SPPID.MAPPINGNAME, ref points, Rotation: lineNumber.ANGLE, LabeledItem: connectedLMConnector.AsLMRepresentation(), IsLeaderVisible: false);
4072

    
4073
                    if (_LmLabelPresist != null)
4074
                    {
4075
                        _LmLabelPresist.Commit();
4076
                        lineNumber.SPPID.RepresentationId = _LmLabelPresist.AsLMRepresentation().Id;
4077
                        ReleaseCOMObjects(_LmLabelPresist);
4078
                    }
4079
                }
4080
            }
4081
        }
4082

    
4083
        private void LineNumberModeling(LineNumber lineNumber)
4084
        {
4085
            Line line = SPPIDUtil.FindObjectByUID(document, lineNumber.CONNLINE) as Line;
4086
            if (line != null)
4087
            {
4088
                double x = 0;
4089
                double y = 0;
4090
                CalcLabelLocation(ref x, ref y, lineNumber.SPPID.ORIGINAL_X, lineNumber.SPPID.ORIGINAL_Y, lineNumber.SPPIDLabelLocation, _ETCSetting.LineNumberLocation);
4091

    
4092
                Dictionary<LMConnector, List<double[]>> connectorVertices = GetPipeRunVertices(line.SPPID.ModelItemId);
4093
                LMConnector connectedLMConnector = FindTargetLMConnectorForLabel(connectorVertices, x, y);
4094
                if (connectedLMConnector != null)
4095
                {
4096
                    Array points = new double[] { 0, x, y };
4097
                    lineNumber.SPPID.SPPID_X = x;
4098
                    lineNumber.SPPID.SPPID_Y = y;
4099
                    LMLabelPersist _LmLabelPresist = _placement.PIDPlaceLabel(lineNumber.SPPID.MAPPINGNAME, ref points, Rotation: lineNumber.ANGLE, LabeledItem: connectedLMConnector.AsLMRepresentation(), IsLeaderVisible: false);
4100

    
4101
                    if (_LmLabelPresist != null)
4102
                    {
4103
                        _LmLabelPresist.Commit();
4104
                        lineNumber.SPPID.RepresentationId = _LmLabelPresist.AsLMRepresentation().Id;
4105
                        ReleaseCOMObjects(_LmLabelPresist);
4106
                    }
4107
                }
4108

    
4109
                foreach (var item in connectorVertices)
4110
                    ReleaseCOMObjects(item.Key);
4111
            }
4112
        }
4113
        private void LineNumberCorrectModeling(LineNumber lineNumber)
4114
        {
4115
            Line line = SPPIDUtil.FindObjectByUID(document, lineNumber.CONNLINE) as Line;
4116
            if (line == null || line.SPPID.Vertices == null)
4117
                return;
4118

    
4119
            if (!string.IsNullOrEmpty(lineNumber.SPPID.RepresentationId))
4120
            {
4121
                LMLabelPersist removeLabel = dataSource.GetLabelPersist(lineNumber.SPPID.RepresentationId);
4122
                if (removeLabel != null)
4123
                {
4124
                    GridSetting gridSetting = GridSetting.GetInstance();
4125
                    double[] labelRange = null;
4126
                    GetSPPIDSymbolRange(removeLabel, ref labelRange);
4127
                    LMConnector connector = dataSource.GetConnector(removeLabel.RepresentationID);
4128
                    List<double[]> vertices = GetConnectorVertices(connector);
4129

    
4130
                    double[] resultStart = null;
4131
                    double[] resultEnd = null;
4132
                    double distance = double.MaxValue;
4133
                    for (int i = 0; i < vertices.Count - 1; i++)
4134
                    {
4135
                        double[] startPoint = vertices[i];
4136
                        double[] endPoint = vertices[i + 1];
4137
                        foreach (var item in line.SPPID.Vertices)
4138
                        {
4139
                            double[] lineStartPoint = item[0];
4140
                            double[] lineEndPoint = item[item.Count - 1];
4141

    
4142
                            double tempDistance = SPPIDUtil.CalcPointToPointdDistance(startPoint[0], startPoint[1], lineStartPoint[0], lineStartPoint[1]) +
4143
                                SPPIDUtil.CalcPointToPointdDistance(endPoint[0], endPoint[1], lineEndPoint[0], lineEndPoint[1]);
4144
                            if (tempDistance < distance)
4145
                            {
4146
                                distance = tempDistance;
4147
                                resultStart = startPoint;
4148
                                resultEnd = endPoint;
4149
                            }
4150
                            tempDistance = SPPIDUtil.CalcPointToPointdDistance(startPoint[0], startPoint[1], lineEndPoint[0], lineEndPoint[1]) +
4151
                                SPPIDUtil.CalcPointToPointdDistance(endPoint[0], endPoint[1], lineStartPoint[0], lineStartPoint[1]);
4152
                            if (tempDistance < distance)
4153
                            {
4154
                                distance = tempDistance;
4155
                                resultStart = startPoint;
4156
                                resultEnd = endPoint;
4157
                            }
4158
                        }
4159
                    }
4160

    
4161
                    if (resultStart != null && resultEnd != null)
4162
                    {
4163
                        SlopeType slope = SPPIDUtil.CalcSlope(resultStart[0], resultStart[1], resultEnd[0], resultEnd[1]);
4164
                        double lineStartX = 0;
4165
                        double lineStartY = 0;
4166
                        double lineEndX = 0;
4167
                        double lineEndY = 0;
4168
                        double lineNumberX = 0;
4169
                        double lineNumberY = 0;
4170
                        SPPIDUtil.ConvertPointBystring(line.STARTPOINT, ref lineStartX, ref lineStartY);
4171
                        SPPIDUtil.ConvertPointBystring(line.ENDPOINT, ref lineEndX, ref lineEndY);
4172

    
4173
                        double lineX = (lineStartX + lineEndX) / 2;
4174
                        double lineY = (lineStartY + lineEndY) / 2;
4175
                        lineNumberX = (lineNumber.X1 + lineNumber.X2) / 2;
4176
                        lineNumberY = (lineNumber.Y1 + lineNumber.Y2) / 2;
4177

    
4178
                        double SPPIDCenterX = (resultStart[0] + resultEnd[0]) / 2;
4179
                        double SPPIDCenterY = (resultStart[1] + resultEnd[1]) / 2;
4180
                        double labelCenterX = (labelRange[0] + labelRange[2]) / 2;
4181
                        double labelCenterY = (labelRange[1] + labelRange[3]) / 2;
4182

    
4183
                        double offsetX = 0;
4184
                        double offsetY = 0;
4185
                        if (slope == SlopeType.HORIZONTAL)
4186
                        {
4187
                            // Line Number 아래
4188
                            if (lineY < lineNumberY)
4189
                            {
4190
                                offsetX = labelCenterX - SPPIDCenterX;
4191
                                offsetY = labelRange[3] - SPPIDCenterY + gridSetting.Length;
4192
                                MoveLineNumber(lineNumber, offsetX, offsetY);
4193
                            }
4194
                            // Line Number 위
4195
                            else
4196
                            {
4197
                                offsetX = labelCenterX - SPPIDCenterX;
4198
                                offsetY = labelRange[1] - SPPIDCenterY - gridSetting.Length;
4199
                                MoveLineNumber(lineNumber, offsetX, offsetY);
4200
                            }
4201
                        }
4202
                        else if (slope == SlopeType.VERTICAL)
4203
                        {
4204
                            // Line Number 오르쪽
4205
                            if (lineX < lineNumberX)
4206
                            {
4207
                                offsetX = labelRange[0] - SPPIDCenterX - gridSetting.Length;
4208
                                offsetY = labelCenterY - SPPIDCenterY;
4209
                                MoveLineNumber(lineNumber, offsetX, offsetY);
4210
                            }
4211
                            // Line Number 왼쪽
4212
                            else
4213
                            {
4214
                                offsetX = labelRange[2] - SPPIDCenterX + gridSetting.Length;
4215
                                offsetY = labelCenterY - SPPIDCenterY;
4216
                                MoveLineNumber(lineNumber, offsetX, offsetY);
4217
                            }
4218
                        }
4219

    
4220
                        if (offsetY != 0 && offsetY != 0)
4221
                        {
4222
                            _placement.PIDRemovePlacement(removeLabel.AsLMRepresentation());
4223
                            Array points = new double[] { 0, lineNumber.SPPID.SPPID_X, lineNumber.SPPID.SPPID_Y };
4224
                            LMLabelPersist _LmLabelPresist = _placement.PIDPlaceLabel(lineNumber.SPPID.MAPPINGNAME, ref points, Rotation: lineNumber.ANGLE, LabeledItem: connector.AsLMRepresentation(), IsLeaderVisible: false);
4225

    
4226
                            if (_LmLabelPresist != null)
4227
                            {
4228
                                _LmLabelPresist.Commit();
4229
                                lineNumber.SPPID.RepresentationId = _LmLabelPresist.AsLMRepresentation().Id;
4230
                            }
4231
                            ReleaseCOMObjects(_LmLabelPresist);
4232
                            _LmLabelPresist = null; 
4233
                        }
4234

    
4235
                        void MoveLineNumber(LineNumber moveLineNumber, double x, double y)
4236
                        {
4237
                            moveLineNumber.SPPID.SPPID_X = moveLineNumber.SPPID.SPPID_X - x;
4238
                            moveLineNumber.SPPID.SPPID_Y = moveLineNumber.SPPID.SPPID_Y - y;
4239
                        }
4240
                    }
4241

    
4242

    
4243
                    ReleaseCOMObjects(connector);
4244
                    connector = null;
4245
                }
4246

    
4247
                ReleaseCOMObjects(removeLabel);
4248
                removeLabel = null;
4249
            }
4250
        }
4251
        /// <summary>
4252
        /// Flow Mark Modeling
4253
        /// </summary>
4254
        /// <param name="line"></param>
4255
        private void FlowMarkModeling(Line line)
4256
        {
4257
            if (line.FLOWMARK && !string.IsNullOrEmpty(line.SPPID.ModelItemId) && !string.IsNullOrEmpty(_ETCSetting.FlowMarkSymbolPath))
4258
            {
4259
                LMConnector connector = GetLMConnectorOnlyOne(line.SPPID.ModelItemId);
4260
                if (connector != null)
4261
                {
4262
                    string mappingPath = _ETCSetting.FlowMarkSymbolPath;
4263
                    List<double[]> vertices = GetConnectorVertices(connector);
4264
                    vertices = vertices.FindAll(x => x[0] > 0 && x[1] > 0);
4265
                    double[] point = vertices[vertices.Count - 1];
4266
                    Array array = new double[] { 0, point[0], point[1] };
4267
                    LMLabelPersist _LMLabelPersist = _placement.PIDPlaceLabel(mappingPath, ref array, LabeledItem: connector.AsLMRepresentation());
4268
                    if (_LMLabelPersist != null)
4269
                    {
4270
                        _LMLabelPersist.Commit();
4271
                        ReleaseCOMObjects(_LMLabelPersist);
4272
                    }
4273
                        
4274
                }
4275
            }
4276
        }
4277

    
4278
        /// <summary>
4279
        /// Line Number 기준으로 모든 Item에 Line Number의 Attribute Input
4280
        /// </summary>
4281
        /// <param name="lineNumber"></param>
4282
        private void InputLineNumberAttribute(LineNumber lineNumber, List<string> endLine)
4283
        {
4284
            foreach (LineRun run in lineNumber.RUNS)
4285
            {
4286
                foreach (var item in run.RUNITEMS)
4287
                {
4288
                    if (item.GetType() == typeof(Symbol))
4289
                    {
4290
                        Symbol symbol = item as Symbol;
4291
                        LMSymbol _LMSymbol = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
4292
                        if (_LMSymbol != null)
4293
                        {
4294
                            LMModelItem _LMModelItem = _LMSymbol.ModelItemObject;
4295

    
4296
                            if (_LMModelItem != null && _LMModelItem.get_ItemStatus() == "Active")
4297
                            {
4298
                                foreach (var attribute in lineNumber.ATTRIBUTES)
4299
                                {
4300
                                    LineNumberMapping mapping = document.LineNumberMappings.Find(x => x.UID == attribute.UID);
4301
                                    if (mapping != null && !string.IsNullOrEmpty(attribute.VALUE) && attribute.VALUE != "None")
4302
                                    {
4303
                                        LMAAttribute _LMAAttribute = _LMModelItem.Attributes[mapping.SPPIDATTRIBUTENAME];
4304
                                        if (_LMAAttribute != null)
4305
                                        {
4306
                                            if (DBNull.Value.Equals(_LMAAttribute.get_Value()))
4307
                                                _LMAAttribute.set_Value(attribute.VALUE);
4308
                                            else if (_LMAAttribute.get_Value() != attribute.VALUE)
4309
                                                _LMAAttribute.set_Value(attribute.VALUE);
4310
                                        }
4311
                                    }
4312
                                }
4313
                                _LMModelItem.Commit();
4314
                            }
4315
                            if (_LMModelItem != null)
4316
                                ReleaseCOMObjects(_LMModelItem);
4317
                        }
4318
                        if (_LMSymbol != null)
4319
                            ReleaseCOMObjects(_LMSymbol);
4320
                    }
4321
                    else if (item.GetType() == typeof(Line))
4322
                    {
4323
                        Line line = item as Line;
4324
                        if (line != null && !endLine.Contains(line.SPPID.ModelItemId))
4325
                        {
4326
                            LMModelItem _LMModelItem = dataSource.GetModelItem(line.SPPID.ModelItemId);
4327
                            if (_LMModelItem != null && _LMModelItem.get_ItemStatus() == "Active")
4328
                            {
4329
                                foreach (var attribute in lineNumber.ATTRIBUTES)
4330
                                {
4331
                                    LineNumberMapping mapping = document.LineNumberMappings.Find(x => x.UID == attribute.UID);
4332
                                    if (mapping != null && !string.IsNullOrEmpty(attribute.VALUE) && attribute.VALUE != "None")
4333
                                    {
4334
                                        LMAAttribute _LMAAttribute = _LMModelItem.Attributes[mapping.SPPIDATTRIBUTENAME];
4335
                                        if (_LMAAttribute != null)
4336
                                        {
4337
                                            if (DBNull.Value.Equals(_LMAAttribute.get_Value()))
4338
                                                _LMAAttribute.set_Value(attribute.VALUE);
4339
                                            else if (_LMAAttribute.get_Value() != attribute.VALUE)
4340
                                                _LMAAttribute.set_Value(attribute.VALUE);
4341

    
4342
                                        }
4343
                                    }
4344
                                }
4345
                                _LMModelItem.Commit();
4346
                            }
4347
                            if (_LMModelItem != null)
4348
                                ReleaseCOMObjects(_LMModelItem);
4349
                            endLine.Add(line.SPPID.ModelItemId);
4350
                        }
4351
                    }
4352
                }
4353
            }
4354
        }
4355

    
4356
        /// <summary>
4357
        /// Symbol Attribute 입력 메서드
4358
        /// </summary>
4359
        /// <param name="item"></param>
4360
        private void InputSymbolAttribute(object targetItem, List<BaseModel.Attribute> targetAttributes)
4361
        {
4362
            // Object 아이템이 Symbol일 경우 Equipment일 경우 
4363
            string sRep = null;
4364
            if (targetItem.GetType() == typeof(Symbol))
4365
                sRep = ((Symbol)targetItem).SPPID.RepresentationId;
4366
            else if (targetItem.GetType() == typeof(Equipment))
4367
                sRep = ((Equipment)targetItem).SPPID.RepresentationId;
4368

    
4369
            if (!string.IsNullOrEmpty(sRep))
4370
            {
4371
                LMSymbol _LMSymbol = dataSource.GetSymbol(sRep);
4372
                LMModelItem _LMModelItem = _LMSymbol.ModelItemObject;
4373
                LMAAttributes _Attributes = _LMModelItem.Attributes;
4374
                
4375
                foreach (var item in targetAttributes)
4376
                {
4377
                    AttributeMapping mapping = document.AttributeMappings.Find(x => x.UID == item.UID);
4378
                    if (mapping != null && !string.IsNullOrEmpty(item.VALUE) && item.VALUE != "None")
4379
                    {
4380
                        LMAAttribute _Attribute = _Attributes[mapping.SPPIDATTRIBUTENAME];
4381
                        if (_Attribute != null)
4382
                        {
4383
                            //object associItem = SPPIDUtil.FindObjectByUID(document, item.ASSOCITEM);
4384
                            //if (associItem != null)
4385
                            //{
4386
                            //    if (associItem.GetType() == typeof(Text))
4387
                            //    {
4388
                            //        Text text = associItem as Text;
4389
                            //        text.SPPID.RepresentationId = "Attribute";
4390
                            //    }
4391
                            //    else if (associItem.GetType() == typeof(Note))
4392
                            //    {
4393
                            //        Note note = associItem as Note;
4394
                            //        note.SPPID.RepresentationId = "Attribute";
4395
                            //    }
4396
                            //}
4397
                            _Attribute.set_Value(item.VALUE);
4398
                            // OPC 일경우 Attribute 저장
4399
                            if (targetItem.GetType() == typeof(Symbol))
4400
                            {
4401
                                Symbol symbol = targetItem as Symbol;
4402
                                if (symbol.TYPE == "Piping OPC's" || symbol.TYPE == "Instrument OPC's")
4403
                                    symbol.SPPID.Attributes.Add(new string[] { mapping.SPPIDATTRIBUTENAME, item.VALUE });
4404
                            }
4405
                        }
4406
                    }
4407
                }
4408
                _LMModelItem.Commit();
4409

    
4410
                ReleaseCOMObjects(_Attributes);
4411
                ReleaseCOMObjects(_LMModelItem);
4412
                ReleaseCOMObjects(_LMSymbol);
4413
            }
4414
        }
4415

    
4416
        /// <summary>
4417
        /// Input SpecBreak Attribute
4418
        /// </summary>
4419
        /// <param name="specBreak"></param>
4420
        private void InputSpecBreakAttribute(SpecBreak specBreak)
4421
        {
4422
            object upStreamObj = SPPIDUtil.FindObjectByUID(document, specBreak.UpStreamUID);
4423
            object downStreamObj = SPPIDUtil.FindObjectByUID(document, specBreak.DownStreamUID);
4424

    
4425
            if (upStreamObj != null &&
4426
                downStreamObj != null)
4427
            {
4428
                LMConnector targetLMConnector = FindBreakLineTarget(upStreamObj, downStreamObj);
4429

    
4430
                if (targetLMConnector != null)
4431
                {
4432
                    foreach (LMLabelPersist _LMLabelPersist in targetLMConnector.LabelPersists)
4433
                    {
4434
                        string symbolPath = _LMLabelPersist.get_FileName();
4435
                        AttributeMapping mapping = document.AttributeMappings.Find(x => x.SPPIDSYMBOLNAME == symbolPath);
4436
                        if (mapping != null)
4437
                        {
4438
                            BaseModel.Attribute attribute = specBreak.ATTRIBUTES.Find(y => y.UID == mapping.UID);
4439
                            if (attribute != null && !string.IsNullOrEmpty(attribute.VALUE) && attribute.VALUE != "None")
4440
                            {
4441
                                string[] values = attribute.VALUE.Split(new char[] { ',' });
4442
                                if (values.Length == 2)
4443
                                {
4444
                                    string upStreamValue = values[0];
4445
                                    string downStreamValue = values[1];
4446

    
4447
                                    InputAttributeForSpecBreak(upStreamObj, downStreamObj, upStreamValue, downStreamValue, mapping.SPPIDATTRIBUTENAME);
4448
                                }
4449
                            }
4450
                        }
4451
                    }
4452

    
4453
                    ReleaseCOMObjects(targetLMConnector);
4454
                }
4455
            }
4456

    
4457

    
4458
            #region 내부에서만 쓰는 메서드
4459
            void InputAttributeForSpecBreak(object _upStreamObj, object _downStreamObj, string upStreamValue, string downStreamValue, string sppidAttributeName)
4460
            {
4461
                Symbol upStreamSymbol = _upStreamObj as Symbol;
4462
                Line upStreamLine = _upStreamObj as Line;
4463
                Symbol downStreamSymbol = _downStreamObj as Symbol;
4464
                Line downStreamLine = _downStreamObj as Line;
4465
                // 둘다 Line일 경우
4466
                if (upStreamLine != null && downStreamLine != null)
4467
                {
4468
                    InputLineAttributeForSpecBreakLine(upStreamLine, sppidAttributeName, upStreamValue);
4469
                    InputLineAttributeForSpecBreakLine(downStreamLine, sppidAttributeName, downStreamValue);
4470
                }
4471
                // 둘다 Symbol일 경우
4472
                else if (upStreamSymbol != null && downStreamSymbol != null)
4473
                {
4474
                    LMConnector zeroLenthConnector = FindBreakLineTarget(upStreamSymbol, downStreamSymbol);
4475
                    LMSymbol upStreamLMSymbol = dataSource.GetSymbol(upStreamSymbol.SPPID.RepresentationId);
4476
                    LMSymbol downStreamLMSymbol = dataSource.GetSymbol(downStreamSymbol.SPPID.RepresentationId);
4477

    
4478
                    foreach (LMConnector connector in upStreamLMSymbol.Avoid1Connectors)
4479
                    {
4480
                        if (connector.get_ItemStatus() != "Active")
4481
                            continue;
4482

    
4483
                        if (connector.Id != zeroLenthConnector.Id)
4484
                            InputLineAttributeForSpecBreakLMConnector(connector, sppidAttributeName, upStreamValue);
4485
                    }
4486

    
4487
                    foreach (LMConnector connector in upStreamLMSymbol.Avoid2Connectors)
4488
                    {
4489
                        if (connector.get_ItemStatus() != "Active")
4490
                            continue;
4491

    
4492
                        if (connector.Id != zeroLenthConnector.Id)
4493
                            InputLineAttributeForSpecBreakLMConnector(connector, sppidAttributeName, upStreamValue);
4494
                    }
4495

    
4496
                    foreach (LMConnector connector in downStreamLMSymbol.Avoid1Connectors)
4497
                    {
4498
                        if (connector.get_ItemStatus() != "Active")
4499
                            continue;
4500

    
4501
                        if (connector.Id != zeroLenthConnector.Id)
4502
                            InputLineAttributeForSpecBreakLMConnector(connector, sppidAttributeName, downStreamValue);
4503
                    }
4504

    
4505
                    foreach (LMConnector connector in downStreamLMSymbol.Avoid2Connectors)
4506
                    {
4507
                        if (connector.get_ItemStatus() != "Active")
4508
                            continue;
4509

    
4510
                        if (connector.Id != zeroLenthConnector.Id)
4511
                            InputLineAttributeForSpecBreakLMConnector(connector, sppidAttributeName, downStreamValue);
4512
                    }
4513

    
4514
                    ReleaseCOMObjects(zeroLenthConnector);
4515
                    ReleaseCOMObjects(upStreamLMSymbol);
4516
                    ReleaseCOMObjects(downStreamLMSymbol);
4517
                }
4518
                else if (upStreamSymbol != null && downStreamLine != null)
4519
                {
4520
                    LMConnector zeroLenthConnector = FindBreakLineTarget(upStreamSymbol, downStreamLine);
4521
                    InputLineAttributeForSpecBreakLine(downStreamLine, sppidAttributeName, downStreamValue);
4522
                    LMSymbol upStreamLMSymbol = dataSource.GetSymbol(upStreamSymbol.SPPID.RepresentationId);
4523

    
4524
                    foreach (LMConnector connector in upStreamLMSymbol.Avoid1Connectors)
4525
                    {
4526
                        if (connector.get_ItemStatus() != "Active")
4527
                            continue;
4528

    
4529
                        if (connector.Id == zeroLenthConnector.Id)
4530
                            InputLineAttributeForSpecBreakLMConnector(connector, sppidAttributeName, upStreamValue);
4531
                    }
4532

    
4533
                    foreach (LMConnector connector in upStreamLMSymbol.Avoid2Connectors)
4534
                    {
4535
                        if (connector.get_ItemStatus() != "Active")
4536
                            continue;
4537

    
4538
                        if (connector.Id == zeroLenthConnector.Id)
4539
                            InputLineAttributeForSpecBreakLMConnector(connector, sppidAttributeName, upStreamValue);
4540
                    }
4541

    
4542
                    ReleaseCOMObjects(zeroLenthConnector);
4543
                    ReleaseCOMObjects(upStreamLMSymbol);
4544
                }
4545
                else if (upStreamLine != null && downStreamSymbol != null)
4546
                {
4547
                    LMConnector zeroLenthConnector = FindBreakLineTarget(upStreamLine, downStreamSymbol);
4548
                    InputLineAttributeForSpecBreakLine(upStreamLine, sppidAttributeName, upStreamValue);
4549
                    LMSymbol downStreamLMSymbol = dataSource.GetSymbol(downStreamSymbol.SPPID.RepresentationId);
4550

    
4551
                    foreach (LMConnector connector in downStreamLMSymbol.Avoid1Connectors)
4552
                    {
4553
                        if (connector.get_ItemStatus() != "Active")
4554
                            continue;
4555

    
4556
                        if (connector.Id == zeroLenthConnector.Id)
4557
                            InputLineAttributeForSpecBreakLMConnector(connector, sppidAttributeName, downStreamValue);
4558
                    }
4559

    
4560
                    foreach (LMConnector connector in downStreamLMSymbol.Avoid2Connectors)
4561
                    {
4562
                        if (connector.get_ItemStatus() != "Active")
4563
                            continue;
4564

    
4565
                        if (connector.Id == zeroLenthConnector.Id)
4566
                            InputLineAttributeForSpecBreakLMConnector(connector, sppidAttributeName, downStreamValue);
4567
                    }
4568

    
4569
                    ReleaseCOMObjects(zeroLenthConnector);
4570
                    ReleaseCOMObjects(downStreamLMSymbol);
4571
                }
4572
            }
4573

    
4574
            void InputLineAttributeForSpecBreakLine(Line line, string attrName, string value)
4575
            {
4576
                LMModelItem _LMModelItem = dataSource.GetModelItem(line.SPPID.ModelItemId);
4577
                if (_LMModelItem != null && _LMModelItem.get_ItemStatus() == "Active")
4578
                {
4579
                    LMAAttribute _LMAAttribute = _LMModelItem.Attributes[attrName];
4580
                    if (_LMAAttribute != null)
4581
                    {
4582
                        if (DBNull.Value.Equals(_LMAAttribute.get_Value()))
4583
                            _LMAAttribute.set_Value(value);
4584
                        else if (_LMAAttribute.get_Value() != value)
4585
                            _LMAAttribute.set_Value(value);
4586
                    }
4587

    
4588
                    _LMModelItem.Commit();
4589
                }
4590
                if (_LMModelItem != null)
4591
                    ReleaseCOMObjects(_LMModelItem);
4592
            }
4593

    
4594
            void InputLineAttributeForSpecBreakLMConnector(LMConnector connector, string attrName, string value)
4595
            {
4596
                LMModelItem _LMModelItem = dataSource.GetModelItem(connector.ModelItemID);
4597
                if (_LMModelItem != null && _LMModelItem.get_ItemStatus() == "Active")
4598
                {
4599
                    LMAAttribute _LMAAttribute = _LMModelItem.Attributes[attrName];
4600
                    if (_LMAAttribute != null)
4601
                    {
4602
                        if (DBNull.Value.Equals(_LMAAttribute.get_Value()))
4603
                            _LMAAttribute.set_Value(value);
4604
                        else if (_LMAAttribute.get_Value() != value)
4605
                            _LMAAttribute.set_Value(value);
4606
                    }
4607

    
4608
                    _LMModelItem.Commit();
4609
                }
4610
                if (_LMModelItem != null)
4611
                    ReleaseCOMObjects(_LMModelItem);
4612
            }
4613
            #endregion
4614
        }
4615

    
4616
        private void InputEndBreakAttribute(EndBreak endBreak)
4617
        {
4618
            object ownerObj = SPPIDUtil.FindObjectByUID(document, endBreak.OWNER);
4619
            object connectedItem = SPPIDUtil.FindObjectByUID(document, endBreak.PROPERTIES.Find(x => x.ATTRIBUTE == "Connected Item").VALUE);
4620

    
4621
            if ((ownerObj.GetType() == typeof(Symbol) && connectedItem.GetType() == typeof(Line)) ||
4622
                (ownerObj.GetType() == typeof(Line) && connectedItem.GetType() == typeof(Symbol)))
4623
            {
4624
                LMLabelPersist labelPersist = dataSource.GetLabelPersist(endBreak.SPPID.RepresentationId);
4625
                if (labelPersist != null)
4626
                {
4627
                    LMRepresentation representation = labelPersist.RepresentationObject;
4628
                    if (representation != null)
4629
                    {
4630
                        LMConnector connector = dataSource.GetConnector(representation.Id);
4631
                        LMModelItem ZeroLengthModelItem = connector.ModelItemObject;
4632
                        string modelItemID = connector.ModelItemID;
4633
                        if (Convert.ToBoolean(connector.get_IsZeroLength()))
4634
                        {
4635
                            List<string> modelItemIDs = new List<string>();
4636
                            if (connector.ConnectItem1SymbolObject != null && connector.ConnectItem1SymbolObject.get_RepresentationType() != "Branch")
4637
                            {
4638
                                LMSymbol symbol = connector.ConnectItem1SymbolObject;
4639
                                foreach (LMConnector item in symbol.Connect1Connectors)
4640
                                {
4641
                                    if (item.get_ItemStatus() == "Active" && item.ModelItemID != modelItemID)
4642
                                        modelItemIDs.Add(item.ModelItemID);
4643
                                    ReleaseCOMObjects(item);
4644
                                }
4645
                                foreach (LMConnector item in symbol.Connect2Connectors)
4646
                                {
4647
                                    if (item.get_ItemStatus() == "Active" && item.ModelItemID != modelItemID)
4648
                                        modelItemIDs.Add(item.ModelItemID);
4649
                                    ReleaseCOMObjects(item);
4650
                                }
4651
                                ReleaseCOMObjects(symbol);
4652
                                symbol = null;
4653
                            }
4654
                            else if (connector.ConnectItem2SymbolObject != null && connector.ConnectItem2SymbolObject.get_RepresentationType() != "Branch")
4655
                            {
4656
                                LMSymbol symbol = connector.ConnectItem2SymbolObject;
4657
                                foreach (LMConnector item in symbol.Connect1Connectors)
4658
                                {
4659
                                    if (item.get_ItemStatus() == "Active" && item.ModelItemID != modelItemID)
4660
                                        modelItemIDs.Add(item.ModelItemID);
4661
                                    ReleaseCOMObjects(item);
4662
                                }
4663
                                foreach (LMConnector item in symbol.Connect2Connectors)
4664
                                {
4665
                                    if (item.get_ItemStatus() == "Active" && item.ModelItemID != modelItemID)
4666
                                        modelItemIDs.Add(item.ModelItemID);
4667
                                    ReleaseCOMObjects(item);
4668
                                }
4669
                                ReleaseCOMObjects(symbol);
4670
                                symbol = null;
4671
                            }
4672

    
4673
                            modelItemIDs = modelItemIDs.Distinct().ToList();
4674
                            if (modelItemIDs.Count == 1)
4675
                            {
4676
                                LMModelItem modelItem = dataSource.GetModelItem(modelItemIDs[0]);
4677
                                LMConnector onlyOne = GetLMConnectorOnlyOne(modelItem.Id);
4678
                                if (onlyOne != null && Convert.ToBoolean(onlyOne.get_IsZeroLength()))
4679
                                {
4680
                                    bool result = false;
4681
                                    foreach (LMLabelPersist loop in onlyOne.LabelPersists)
4682
                                    {
4683
                                        if (document.EndBreaks.Find(x => x.SPPID.RepresentationId == loop.RepresentationID) != null)
4684
                                            result = true;
4685
                                        ReleaseCOMObjects(loop);
4686
                                    }
4687

    
4688
                                    if (result)
4689
                                    {
4690
                                        object value = modelItem.Attributes["TagSequenceNo"].get_Value();
4691
                                        ZeroLengthModelItem.Attributes["TagSequenceNo"].set_Value(value);
4692
                                        ZeroLengthModelItem.Commit();
4693
                                    }
4694
                                    else
4695
                                    {
4696
                                        List<string> loopModelItems = new List<string>();
4697
                                        if (onlyOne.ConnectItem1SymbolObject.get_RepresentationType() == "Branch")
4698
                                        {
4699
                                            LMSymbol _symbol = onlyOne.ConnectItem1SymbolObject;
4700
                                            foreach (LMConnector loop in _symbol.Connect1Connectors)
4701
                                            {
4702
                                                if (loop.get_ItemStatus() == "Active" && loop.ModelItemID != onlyOne.ModelItemID)
4703
                                                    loopModelItems.Add(loop.ModelItemID);
4704
                                                ReleaseCOMObjects(loop);
4705
                                            }
4706
                                            foreach (LMConnector loop in _symbol.Connect2Connectors)
4707
                                            {
4708
                                                if (loop.get_ItemStatus() == "Active" && loop.ModelItemID != onlyOne.ModelItemID)
4709
                                                    loopModelItems.Add(loop.ModelItemID);
4710
                                                ReleaseCOMObjects(loop);
4711
                                            }
4712
                                            ReleaseCOMObjects(_symbol);
4713
                                            _symbol = null;
4714
                                        }
4715
                                        else if (onlyOne.ConnectItem2SymbolObject.get_RepresentationType() == "Branch")
4716
                                        {
4717
                                            LMSymbol _symbol = onlyOne.ConnectItem2SymbolObject;
4718
                                            foreach (LMConnector loop in _symbol.Connect1Connectors)
4719
                                            {
4720
                                                if (loop.get_ItemStatus() == "Active" && loop.ModelItemID != onlyOne.ModelItemID)
4721
                                                    loopModelItems.Add(loop.ModelItemID);
4722
                                                ReleaseCOMObjects(loop);
4723
                                            }
4724
                                            foreach (LMConnector loop in _symbol.Connect2Connectors)
4725
                                            {
4726
                                                if (loop.get_ItemStatus() == "Active" && loop.ModelItemID != onlyOne.ModelItemID)
4727
                                                    loopModelItems.Add(loop.ModelItemID);
4728
                                                ReleaseCOMObjects(loop);
4729
                                            }
4730
                                            ReleaseCOMObjects(_symbol);
4731
                                            _symbol = null;
4732
                                        }
4733

    
4734
                                        loopModelItems = loopModelItems.Distinct().ToList();
4735
                                        if (loopModelItems.Count == 1)
4736
                                        {
4737
                                            LMModelItem loopModelItem = dataSource.GetModelItem(loopModelItems[0]);
4738
                                            object value = loopModelItem.Attributes["TagSequenceNo"].get_Value();
4739
                                            modelItem.Attributes["TagSequenceNo"].set_Value(value);
4740
                                            modelItem.Commit();
4741
                                            ZeroLengthModelItem.Attributes["TagSequenceNo"].set_Value(value);
4742
                                            ZeroLengthModelItem.Commit();
4743

    
4744
                                            ReleaseCOMObjects(loopModelItem);
4745
                                            loopModelItem = null;
4746
                                        }
4747
                                    }
4748
                                }
4749
                                else
4750
                                {
4751
                                    object value = modelItem.Attributes["TagSequenceNo"].get_Value();
4752
                                    ZeroLengthModelItem.Attributes["TagSequenceNo"].set_Value(value);
4753
                                    ZeroLengthModelItem.Commit();
4754
                                }
4755
                                ReleaseCOMObjects(modelItem);
4756
                                modelItem = null;
4757
                                ReleaseCOMObjects(onlyOne);
4758
                                onlyOne = null;
4759
                            }
4760
                        }
4761
                        ReleaseCOMObjects(connector);
4762
                        connector = null;
4763
                        ReleaseCOMObjects(ZeroLengthModelItem);
4764
                        ZeroLengthModelItem = null;
4765
                    }
4766
                    ReleaseCOMObjects(representation);
4767
                    representation = null;
4768
                }
4769
                ReleaseCOMObjects(labelPersist);
4770
                labelPersist = null;
4771
            }
4772
        }
4773

    
4774
        /// <summary>
4775
        /// Text Modeling - Association일 경우는 Text대신 해당 맵핑된 Symbol로 모델링
4776
        /// </summary>
4777
        /// <param name="text"></param>
4778
        private void NormalTextModeling(Text text)
4779
        {
4780
            LMSymbol _LMSymbol = null;
4781

    
4782
            LMItemNote _LMItemNote = null;
4783
            LMAAttribute _LMAAttribute = null;
4784

    
4785
            double x = 0;
4786
            double y = 0;
4787
            double angle = text.ANGLE;
4788
            CalcLabelLocation(ref x, ref y, text.SPPID.ORIGINAL_X, text.SPPID.ORIGINAL_Y, text.SPPIDLabelLocation, _ETCSetting.TextLocation);
4789

    
4790
            SPPIDUtil.ConvertGridPoint(ref x, ref y);
4791
            text.SPPID.SPPID_X = x;
4792
            text.SPPID.SPPID_Y = y;
4793

    
4794
            _LMSymbol = _placement.PIDPlaceSymbol(text.SPPID.MAPPINGNAME, x, y, Rotation: angle);
4795
            if (_LMSymbol != null)
4796
            {
4797
                _LMSymbol.Commit();
4798
                _LMItemNote = _placement.PIDDataSource.GetItemNote(_LMSymbol.ModelItemID);
4799
                if (_LMItemNote != null)
4800
                {
4801
                    _LMItemNote.Commit();
4802
                    _LMAAttribute = _LMItemNote.Attributes["Note.Body"];
4803
                    if (_LMAAttribute != null)
4804
                    {
4805
                        _LMAAttribute.set_Value(text.VALUE);
4806
                        text.SPPID.RepresentationId = _LMSymbol.AsLMRepresentation().Id;
4807
                        _LMItemNote.Commit();
4808

    
4809

    
4810
                        double[] range = null;
4811
                        foreach (LMLabelPersist labelPersist in _LMSymbol.LabelPersists)
4812
                        {
4813
                            double[] temp = null;
4814
                            GetSPPIDSymbolRange(labelPersist, ref temp);
4815
                            if (temp != null)
4816
                            {
4817
                                if (range == null)
4818
                                    range = temp;
4819
                                else
4820
                                {
4821
                                    range = new double[] {
4822
                                            Math.Min(range[0], temp[0]),
4823
                                            Math.Min(range[1], temp[1]),
4824
                                            Math.Max(range[2], temp[2]),
4825
                                            Math.Max(range[3], temp[3])
4826
                                        };
4827
                                }
4828
                            }
4829
                        }
4830
                        text.SPPID.Range = range;
4831

    
4832
                        if (_LMAAttribute != null)
4833
                            ReleaseCOMObjects(_LMAAttribute);
4834
                        if (_LMItemNote != null)
4835
                            ReleaseCOMObjects(_LMItemNote);
4836
                    }
4837

    
4838
                    TextCorrectModeling(text);
4839
                }
4840
            }
4841
            if (_LMSymbol != null)
4842
                ReleaseCOMObjects(_LMSymbol);
4843
        }
4844

    
4845
        private void AssociationTextModeling(Text text)
4846
        {
4847
            LMSymbol _LMSymbol = null;
4848
            LMConnector connectedLMConnector = null;
4849
            object owner = SPPIDUtil.FindObjectByUID(document, text.OWNER);
4850
            if (owner != null && owner.GetType() == typeof(Symbol))
4851
            {
4852
                Symbol symbol = owner as Symbol;
4853
                _LMSymbol = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
4854
                if (_LMSymbol != null)
4855
                {
4856
                    BaseModel.Attribute attribute = symbol.ATTRIBUTES.Find(x => x.ASSOCITEM == text.UID);
4857
                    if (attribute != null && !string.IsNullOrEmpty(attribute.VALUE) && attribute.VALUE != "None")
4858
                    {
4859
                        AttributeMapping mapping = document.AttributeMappings.Find(x => x.UID == attribute.UID && !string.IsNullOrEmpty(x.SPPIDSYMBOLNAME));
4860

    
4861
                        if (mapping != null)
4862
                        {
4863
                            double x = 0;
4864
                            double y = 0;
4865

    
4866
                            CalcLabelLocation(ref x, ref y, text.SPPID.ORIGINAL_X, text.SPPID.ORIGINAL_Y, text.SPPIDLabelLocation, mapping.Location);
4867
                            SPPIDUtil.ConvertGridPoint(ref x, ref y);
4868
                            Array array = new double[] { 0, x, y };
4869
                            text.SPPID.SPPID_X = x;
4870
                            text.SPPID.SPPID_Y = y;
4871
                            LMLabelPersist _LMLabelPersist = _placement.PIDPlaceLabel(mapping.SPPIDSYMBOLNAME, ref array, Rotation: text.ANGLE, LabeledItem: _LMSymbol.AsLMRepresentation(), IsLeaderVisible: mapping.LeaderLine);
4872
                            if (_LMLabelPersist != null)
4873
                            {
4874
                                text.SPPID.RepresentationId = _LMLabelPersist.AsLMRepresentation().Id;
4875
                                _LMLabelPersist.Commit();
4876
                                ReleaseCOMObjects(_LMLabelPersist);
4877
                            }
4878
                        }
4879
                    }
4880
                }
4881
            }
4882
            else if (owner != null && owner.GetType() == typeof(Line))
4883
            {
4884
                Line line = owner as Line;
4885
                Dictionary<LMConnector, List<double[]>> connectorVertices = GetPipeRunVertices(line.SPPID.ModelItemId);
4886
                connectedLMConnector = FindTargetLMConnectorForLabel(connectorVertices, text.SPPID.ORIGINAL_X, text.SPPID.ORIGINAL_Y);
4887

    
4888
                if (connectedLMConnector != null)
4889
                {
4890
                    BaseModel.Attribute attribute = line.ATTRIBUTES.Find(x => x.ASSOCITEM == text.UID);
4891
                    if (attribute != null && !string.IsNullOrEmpty(attribute.VALUE) && attribute.VALUE != "None")
4892
                    {
4893
                        AttributeMapping mapping = document.AttributeMappings.Find(x => x.UID == attribute.UID && !string.IsNullOrEmpty(x.SPPIDSYMBOLNAME));
4894

    
4895
                        if (mapping != null)
4896
                        {
4897
                            double x = 0;
4898
                            double y = 0;
4899
                            CalcLabelLocation(ref x, ref y, text.SPPID.ORIGINAL_X, text.SPPID.ORIGINAL_Y, text.SPPIDLabelLocation, mapping.Location);
4900
                            SPPIDUtil.ConvertGridPoint(ref x, ref y);
4901
                            Array array = new double[] { 0, x, y };
4902

    
4903
                            LMLabelPersist _LMLabelPersist = _placement.PIDPlaceLabel(mapping.SPPIDSYMBOLNAME, ref array, Rotation: text.ANGLE, LabeledItem: connectedLMConnector.AsLMRepresentation(), IsLeaderVisible: mapping.LeaderLine);
4904
                            if (_LMLabelPersist != null)
4905
                            {
4906
                                text.SPPID.RepresentationId = _LMLabelPersist.AsLMRepresentation().Id;
4907
                                _LMLabelPersist.Commit();
4908
                                ReleaseCOMObjects(_LMLabelPersist);
4909
                            }
4910
                        }
4911
                    }
4912
                }
4913
            }
4914
            if (_LMSymbol != null)
4915
                ReleaseCOMObjects(_LMSymbol);
4916
        }
4917

    
4918
        private void TextCorrectModeling(Text text)
4919
        {
4920
            if (text.SPPID.Range == null)
4921
                return;
4922

    
4923
            bool needRemodeling = false;
4924
            bool loop = true;
4925
            GridSetting gridSetting = GridSetting.GetInstance();
4926
            while (loop)
4927
            {
4928
                loop = false;
4929
                foreach (var overlapText in document.TEXTINFOS)
4930
                {
4931
                    if (overlapText.ASSOCIATION || overlapText == text || overlapText.SPPID.Range == null)
4932
                        continue;
4933

    
4934
                    if (SPPIDUtil.IsOverlap(overlapText.SPPID.Range, text.SPPID.Range))
4935
                    {
4936
                        double percentX = 0;
4937
                        double percentY = 0;
4938
                        if (overlapText.X1 <= text.X2 && overlapText.X2 >= text.X1)
4939
                        {
4940
                            double gapX = Math.Min(overlapText.X2, text.X2) - Math.Max(overlapText.X1, text.X1);
4941
                            percentX = Math.Max(gapX / (overlapText.X2 - overlapText.X1), gapX / (text.X2 - text.X1));
4942
                        }
4943
                        if (overlapText.Y1 <= text.Y2 && overlapText.Y2 >= text.Y1)
4944
                        {
4945
                            double gapY = Math.Min(overlapText.Y2, text.Y2) - Math.Max(overlapText.Y1, text.Y1);
4946
                            percentY = Math.Max(gapY / (overlapText.Y2 - overlapText.Y1), gapY / (text.Y2 - text.Y1));
4947
                        }
4948

    
4949
                        double tempX = 0;
4950
                        double tempY = 0;
4951
                        bool overlapX = false;
4952
                        bool overlapY = false;
4953
                        SPPIDUtil.CalcOverlap(text.SPPID.Range, overlapText.SPPID.Range, ref tempX, ref tempY, ref overlapX, ref overlapY);
4954
                        if (percentX >= percentY)
4955
                        {
4956
                            int count = Convert.ToInt32(tempY / gridSetting.Length) + 1;
4957
                            double move = gridSetting.Length * count;
4958
                            text.SPPID.SPPID_Y = text.SPPID.SPPID_Y - move;
4959
                            text.SPPID.Range = new double[] { text.SPPID.Range[0], text.SPPID.Range[1] - move, text.SPPID.Range[2], text.SPPID.Range[3] - move };
4960
                            needRemodeling = true;
4961
                            loop = true;
4962
                        }
4963
                        else
4964
                        {
4965
                            int count = Convert.ToInt32(tempX / gridSetting.Length) + 1;
4966
                            double move = gridSetting.Length * count;
4967
                            text.SPPID.SPPID_X = text.SPPID.SPPID_X + move;
4968
                            text.SPPID.Range = new double[] { text.SPPID.Range[0] + move, text.SPPID.Range[1], text.SPPID.Range[2] + move, text.SPPID.Range[3] };
4969
                            needRemodeling = true;
4970
                            loop = true;
4971
                        }
4972
                    }
4973
                }
4974
            }
4975
            
4976

    
4977
            if (needRemodeling)
4978
            {
4979
                LMSymbol symbol = dataSource.GetSymbol(text.SPPID.RepresentationId);
4980
                _placement.PIDRemovePlacement(symbol.AsLMRepresentation());
4981
                text.SPPID.RepresentationId = null;
4982

    
4983
                LMItemNote _LMItemNote = null;
4984
                LMAAttribute _LMAAttribute = null;
4985
                LMSymbol _LMSymbol = _placement.PIDPlaceSymbol(text.SPPID.MAPPINGNAME, text.SPPID.SPPID_X, text.SPPID.SPPID_Y, Rotation: text.ANGLE);
4986
                if (_LMSymbol != null)
4987
                {
4988
                    _LMSymbol.Commit();
4989
                    _LMItemNote = _placement.PIDDataSource.GetItemNote(_LMSymbol.ModelItemID);
4990
                    if (_LMItemNote != null)
4991
                    {
4992
                        _LMItemNote.Commit();
4993
                        _LMAAttribute = _LMItemNote.Attributes["Note.Body"];
4994
                        if (_LMAAttribute != null)
4995
                        {
4996
                            _LMAAttribute.set_Value(text.VALUE);
4997
                            text.SPPID.RepresentationId = _LMSymbol.AsLMRepresentation().Id;
4998
                            _LMItemNote.Commit();
4999

    
5000
                            ReleaseCOMObjects(_LMAAttribute);
5001
                            ReleaseCOMObjects(_LMItemNote);
5002
                        }
5003
                    }
5004
                }
5005

    
5006
                ReleaseCOMObjects(symbol);
5007
                symbol = null;
5008
                ReleaseCOMObjects(_LMItemNote);
5009
                _LMItemNote = null;
5010
                ReleaseCOMObjects(_LMAAttribute);
5011
                _LMAAttribute = null;
5012
                ReleaseCOMObjects(_LMSymbol);
5013
                _LMSymbol = null;
5014
            }
5015
        }
5016

    
5017
        private void AssociationTextCorrectModeling(Text text, List<Text> endTexts)
5018
        {
5019
            if (!string.IsNullOrEmpty(text.SPPID.RepresentationId))
5020
            {
5021
                List<Text> texts = new List<Text>();
5022
                LMLabelPersist targetLabel = dataSource.GetLabelPersist(text.SPPID.RepresentationId);
5023
                LMRepresentation representation = targetLabel.RepresentationObject;
5024
                Symbol symbol = document.SYMBOLS.Find(x => x.SPPID.RepresentationId == representation.Id);
5025
                if (targetLabel.RepresentationObject != null && symbol != null)
5026
                {
5027
                    double[] symbolRange = null;
5028
                    GetSPPIDSymbolRange(symbol, ref symbolRange, true, true);
5029
                    if (symbolRange != null)
5030
                    {
5031
                        foreach (LMLabelPersist labelPersist in representation.LabelPersists)
5032
                        {
5033
                            Text findText = document.TEXTINFOS.Find(x => x.SPPID.RepresentationId == labelPersist.AsLMRepresentation().Id && x.ASSOCIATION);
5034
                            if (findText != null)
5035
                            {
5036
                                double[] range = null;
5037
                                GetSPPIDSymbolRange(labelPersist, ref range);
5038
                                findText.SPPID.Range = range;
5039
                                texts.Add(findText);
5040
                            }
5041

    
5042
                            ReleaseCOMObjects(labelPersist);
5043
                        }
5044

    
5045
                        if (texts.Count > 0)
5046
                        {
5047
                            #region Sort Text By Y
5048
                            texts.Sort(SortTextByY);
5049
                            int SortTextByY(Text a, Text b)
5050
                            {
5051
                                return b.SPPID.Range[3].CompareTo(a.SPPID.Range[3]);
5052
                            }
5053
                            #endregion
5054

    
5055
                            #region 첫번째 Text로 기준 맞춤
5056
                            for (int i = 0; i < texts.Count; i++)
5057
                            {
5058
                                if (i != 0)
5059
                                {
5060
                                    Text currentText = texts[i];
5061
                                    Text prevText = texts[i - 1];
5062
                                    double minY = prevText.SPPID.Range[1];
5063
                                    double centerPrevX = (prevText.SPPID.Range[0] + prevText.SPPID.Range[2]) / 2;
5064
                                    double centerX = (currentText.SPPID.Range[0] + currentText.SPPID.Range[2]) / 2;
5065
                                    double _gapX = centerX - centerPrevX;
5066
                                    double _gapY = currentText.SPPID.Range[3] - minY;
5067
                                    MoveText(currentText, _gapX, _gapY);
5068
                                }
5069
                            }
5070
                            List<double> rangeMinX = texts.Select(loopX => loopX.SPPID.Range[0]).ToList();
5071
                            List<double> rangeMinY = texts.Select(loopX => loopX.SPPID.Range[1]).ToList();
5072
                            List<double> rangeMaxX = texts.Select(loopX => loopX.SPPID.Range[2]).ToList();
5073
                            List<double> rangeMaxY = texts.Select(loopX => loopX.SPPID.Range[3]).ToList();
5074
                            rangeMinX.Sort();
5075
                            rangeMinY.Sort();
5076
                            rangeMaxX.Sort();
5077
                            rangeMaxY.Sort();
5078
                            double allTextCenterX = (rangeMinX[0] + rangeMaxX[rangeMaxX.Count - 1]) / 2;
5079
                            double allTextCenterY = (rangeMinY[0] + rangeMaxY[rangeMaxY.Count - 1]) / 2;
5080
                            #endregion
5081

    
5082
                            Text correctBySymbol = texts[0];
5083
                            double textCenterX = (text.X1 + text.X2) / 2;
5084
                            double textCenterY = (text.Y1 + text.Y2) / 2;
5085
                            double originX = 0;
5086
                            double originY = 0;
5087
                            SPPIDUtil.ConvertPointBystring(symbol.ORIGINALPOINT, ref originX, ref originY);
5088
                            double angle = SPPIDUtil.CalcAngle(textCenterX, textCenterY, originX, originY);
5089
                            double symbolCenterX = (symbolRange[0] + symbolRange[2]) / 2;
5090
                            double symbolCenterY = (symbolRange[1] + symbolRange[3]) / 2;
5091

    
5092
                            double gapX = 0;
5093
                            double gapY = 0;
5094
                            if (angle < 45)
5095
                            {
5096
                                // Text 오른쪽
5097
                                if (textCenterX > originX)
5098
                                {
5099
                                    gapX = rangeMinX[0] - symbolRange[2];
5100
                                    gapY = allTextCenterY - symbolCenterY;
5101
                                }
5102
                                // Text 왼쪽
5103
                                else
5104
                                {
5105
                                    gapX = rangeMaxX[rangeMaxX.Count - 1] - symbolRange[0];
5106
                                    gapY = allTextCenterY - symbolCenterY;
5107
                                }
5108
                            }
5109
                            else
5110
                            {
5111
                                // Text 아래쪽
5112
                                if (textCenterY > originY)
5113
                                {
5114
                                    gapX = allTextCenterX - symbolCenterX;
5115
                                    gapY = rangeMaxY[rangeMaxY.Count - 1] - symbolRange[1];
5116
                                }
5117
                                // Text 위쪽
5118
                                else
5119
                                {
5120
                                    gapX = allTextCenterX - symbolCenterX;
5121
                                    gapY = rangeMinY[0] - symbolRange[3];
5122
                                }
5123
                            }
5124

    
5125
                            foreach (var item in texts)
5126
                            {
5127
                                MoveText(item, gapX, gapY);
5128
                                RemodelingAssociationText(item);
5129
                            }
5130
                        }
5131
                    }
5132
                }
5133

    
5134
                void MoveText(Text moveText, double x, double y)
5135
                {
5136
                    moveText.SPPID.SPPID_X = moveText.SPPID.SPPID_X - x;
5137
                    moveText.SPPID.SPPID_Y = moveText.SPPID.SPPID_Y - y;
5138
                    moveText.SPPID.Range = new double[] {
5139
                        moveText.SPPID.Range[0] - x,
5140
                        moveText.SPPID.Range[1]- y,
5141
                        moveText.SPPID.Range[2]- x,
5142
                        moveText.SPPID.Range[3]- y
5143
                    };
5144
                }
5145

    
5146
                endTexts.AddRange(texts);
5147

    
5148
                ReleaseCOMObjects(targetLabel);
5149
                targetLabel = null;
5150
                ReleaseCOMObjects(representation);
5151
                representation = null;
5152
            }
5153
        }
5154

    
5155
        private void RemodelingAssociationText(Text text)
5156
        {
5157
            LMLabelPersist removeLabel = dataSource.GetLabelPersist(text.SPPID.RepresentationId);
5158
            _placement.PIDRemovePlacement(removeLabel.AsLMRepresentation());
5159
            removeLabel.Commit();
5160
            ReleaseCOMObjects(removeLabel);
5161
            removeLabel = null;
5162

    
5163
            object owner = SPPIDUtil.FindObjectByUID(document, text.OWNER);
5164
            if (owner != null && owner.GetType() == typeof(Symbol))
5165
            {
5166
                Symbol symbol = owner as Symbol;
5167
                _LMSymbol _LMSymbol = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
5168
                if (_LMSymbol != null)
5169
                {
5170
                    BaseModel.Attribute attribute = symbol.ATTRIBUTES.Find(x => x.ASSOCITEM == text.UID);
5171
                    if (attribute != null && !string.IsNullOrEmpty(attribute.VALUE) && attribute.VALUE != "None")
5172
                    {
5173
                        AttributeMapping mapping = document.AttributeMappings.Find(x => x.UID == attribute.UID && !string.IsNullOrEmpty(x.SPPIDSYMBOLNAME));
5174

    
5175
                        if (mapping != null)
5176
                        {
5177
                            double x = 0;
5178
                            double y = 0;
5179

    
5180
                            Array array = new double[] { 0, text.SPPID.SPPID_X, text.SPPID.SPPID_Y };
5181
                            LMLabelPersist _LMLabelPersist = _placement.PIDPlaceLabel(mapping.SPPIDSYMBOLNAME, ref array, Rotation: text.ANGLE, LabeledItem: _LMSymbol.AsLMRepresentation(), IsLeaderVisible: mapping.LeaderLine);
5182
                            if (_LMLabelPersist != null)
5183
                            {
5184
                                text.SPPID.RepresentationId = _LMLabelPersist.AsLMRepresentation().Id;
5185
                                _LMLabelPersist.Commit();
5186
                            }
5187
                            ReleaseCOMObjects(_LMLabelPersist);
5188
                            _LMLabelPersist = null;
5189
                        }
5190
                    }
5191
                }
5192
                ReleaseCOMObjects(_LMSymbol);
5193
                _LMSymbol = null;
5194
            }
5195
        }
5196

    
5197
        /// <summary>
5198
        /// Note Modeling
5199
        /// </summary>
5200
        /// <param name="note"></param>
5201
        private void NoteModeling(Note note, List<Note> correctList)
5202
        {
5203
            LMSymbol _LMSymbol = null;
5204
            LMItemNote _LMItemNote = null;
5205
            LMAAttribute _LMAAttribute = null;
5206

    
5207
            if (string.IsNullOrEmpty(note.OWNER) || note.OWNER == "None")
5208
            {
5209
                double x = 0;
5210
                double y = 0;
5211

    
5212
                CalcLabelLocation(ref x, ref y, note.SPPID.ORIGINAL_X, note.SPPID.ORIGINAL_Y, note.SPPIDLabelLocation, _ETCSetting.NoteLocation);
5213
                SPPIDUtil.ConvertGridPoint(ref x, ref y);
5214
                note.SPPID.SPPID_X = x;
5215
                note.SPPID.SPPID_Y = y;
5216

    
5217
                _LMSymbol = _placement.PIDPlaceSymbol(note.SPPID.MAPPINGNAME, x, y);
5218
                if (_LMSymbol != null)
5219
                {
5220
                    _LMSymbol.Commit();
5221
                    _LMItemNote = _placement.PIDDataSource.GetItemNote(_LMSymbol.ModelItemID);
5222
                    if (_LMItemNote != null)
5223
                    {
5224
                        _LMItemNote.Commit();
5225
                        _LMAAttribute = _LMItemNote.Attributes["Note.Body"];
5226
                        if (_LMAAttribute != null)
5227
                        {
5228
                            _LMAAttribute.set_Value(note.VALUE);
5229
                            note.SPPID.RepresentationId = _LMSymbol.AsLMRepresentation().Id;
5230

    
5231
                            double[] range = null;
5232
                            foreach (LMLabelPersist labelPersist in _LMSymbol.LabelPersists)
5233
                            {
5234
                                double[] temp = null;
5235
                                GetSPPIDSymbolRange(labelPersist, ref temp);
5236
                                if (temp != null)
5237
                                {
5238
                                    if (range == null)
5239
                                        range = temp;
5240
                                    else
5241
                                    {
5242
                                        range = new double[] {
5243
                                            Math.Min(range[0], temp[0]),
5244
                                            Math.Min(range[1], temp[1]),
5245
                                            Math.Max(range[2], temp[2]),
5246
                                            Math.Max(range[3], temp[3])
5247
                                        };
5248
                                    }
5249
                                }
5250
                            }
5251
                            if (range != null)
5252
                                correctList.Add(note);
5253
                            note.SPPID.Range = range;
5254

    
5255

    
5256
                            _LMItemNote.Commit();
5257
                        }
5258
                    }
5259
                }
5260
            }
5261

    
5262
            if (_LMAAttribute != null)
5263
                ReleaseCOMObjects(_LMAAttribute);
5264
            if (_LMItemNote != null)
5265
                ReleaseCOMObjects(_LMItemNote);
5266
            if (_LMSymbol != null)
5267
                ReleaseCOMObjects(_LMSymbol);
5268
        }
5269

    
5270
        private void NoteCorrectModeling(Note note, List<Note> endList)
5271
        {
5272
            bool needRemodeling = false;
5273
            bool loop = true;
5274
            GridSetting gridSetting = GridSetting.GetInstance();
5275
            while (loop)
5276
            {
5277
                loop = false;
5278
                foreach (var overlap in endList)
5279
                {
5280
                    if (SPPIDUtil.IsOverlap(overlap.SPPID.Range, note.SPPID.Range))
5281
                    {
5282
                        double tempX = 0;
5283
                        double tempY = 0;
5284
                        bool overlapX = false;
5285
                        bool overlapY = false;
5286
                        SPPIDUtil.CalcOverlap(note.SPPID.Range, overlap.SPPID.Range, ref tempX, ref tempY, ref overlapX, ref overlapY);
5287
                        double angle = SPPIDUtil.CalcAngle(note.LOCATION_X, note.LOCATION_Y, overlap.LOCATION_X, overlap.LOCATION_Y);
5288
                        if (overlapY && angle >= 45)
5289
                        {
5290
                            int count = Convert.ToInt32(tempY / gridSetting.Length) + 1;
5291
                            double move = gridSetting.Length * count;
5292
                            note.SPPID.SPPID_Y = note.SPPID.SPPID_Y - move;
5293
                            note.SPPID.Range = new double[] { note.SPPID.Range[0], note.SPPID.Range[1] - move, note.SPPID.Range[2], note.SPPID.Range[3] - move };
5294
                            needRemodeling = true;
5295
                            loop = true;
5296
                        }
5297
                        if (overlapX && angle <= 45)
5298
                        {
5299
                            int count = Convert.ToInt32(tempX / gridSetting.Length) + 1;
5300
                            double move = gridSetting.Length * count;
5301
                            note.SPPID.SPPID_X = note.SPPID.SPPID_X + move;
5302
                            note.SPPID.Range = new double[] { note.SPPID.Range[0] + move, note.SPPID.Range[1], note.SPPID.Range[2] + move, note.SPPID.Range[3] };
5303
                            needRemodeling = true;
5304
                            loop = true;
5305
                        }
5306
                    }
5307
                }
5308
            }
5309

    
5310

    
5311
            if (needRemodeling)
5312
            {
5313
                LMSymbol symbol = dataSource.GetSymbol(note.SPPID.RepresentationId);
5314
                _placement.PIDRemovePlacement(symbol.AsLMRepresentation());
5315
                note.SPPID.RepresentationId = null;
5316

    
5317
                LMItemNote _LMItemNote = null;
5318
                LMAAttribute _LMAAttribute = null;
5319
                LMSymbol _LMSymbol = _placement.PIDPlaceSymbol(note.SPPID.MAPPINGNAME, note.SPPID.SPPID_X, note.SPPID.SPPID_Y, Rotation: note.ANGLE);
5320
                if (_LMSymbol != null)
5321
                {
5322
                    _LMSymbol.Commit();
5323
                    _LMItemNote = _placement.PIDDataSource.GetItemNote(_LMSymbol.ModelItemID);
5324
                    if (_LMItemNote != null)
5325
                    {
5326
                        _LMItemNote.Commit();
5327
                        _LMAAttribute = _LMItemNote.Attributes["Note.Body"];
5328
                        if (_LMAAttribute != null)
5329
                        {
5330
                            _LMAAttribute.set_Value(note.VALUE);
5331
                            note.SPPID.RepresentationId = _LMSymbol.AsLMRepresentation().Id;
5332
                            _LMItemNote.Commit();
5333

    
5334
                            ReleaseCOMObjects(_LMAAttribute);
5335
                            ReleaseCOMObjects(_LMItemNote);
5336
                        }
5337
                    }
5338
                }
5339

    
5340
                ReleaseCOMObjects(symbol);
5341
                symbol = null;
5342
                ReleaseCOMObjects(_LMItemNote);
5343
                _LMItemNote = null;
5344
                ReleaseCOMObjects(_LMAAttribute);
5345
                _LMAAttribute = null;
5346
                ReleaseCOMObjects(_LMSymbol);
5347
                _LMSymbol = null;
5348
            }
5349

    
5350
            endList.Add(note);
5351
        }
5352

    
5353
        private void JoinRunBySameType(string modelItemId, ref string survivorId)
5354
        {
5355
            LMModelItem modelItem = dataSource.GetModelItem(modelItemId);
5356
            if (modelItem != null)
5357
            {
5358
                foreach (LMRepresentation rep in modelItem.Representations)
5359
                {
5360
                    if (rep.Attributes["RepresentationType"].get_Value() == "Connector" && rep.Attributes["ItemStatus"].get_Value() == "Active")
5361
                    {
5362
                        LMConnector connector = dataSource.GetConnector(rep.Id);
5363
                        if (connector.ConnectItem1SymbolObject != null && connector.ConnectItem1SymbolObject.get_RepresentationType() != "Branch")
5364
                        {
5365
                            LMSymbol symbol = connector.ConnectItem1SymbolObject;
5366
                            List<string> modelItemIds = FindOtherModelItemBySymbolWhereTypePipeRun(symbol, modelItem.Id);
5367
                            if (modelItemIds.Count == 1)
5368
                            {
5369
                                string joinModelItemId = modelItemIds[0];
5370
                                JoinRun(joinModelItemId, modelItemId, ref survivorId, false);
5371
                                if (survivorId != null)
5372
                                    break;
5373
                            }
5374
                        }
5375
                        if (connector.ConnectItem2SymbolObject != null && connector.ConnectItem2SymbolObject.get_RepresentationType() != "Branch")
5376
                        {
5377
                            LMSymbol symbol = connector.ConnectItem2SymbolObject;
5378
                            List<string> modelItemIds = FindOtherModelItemBySymbolWhereTypePipeRun(symbol, modelItem.Id);
5379
                            if (modelItemIds.Count == 1)
5380
                            {
5381
                                string joinModelItemId = modelItemIds[0];
5382
                                JoinRun(joinModelItemId, modelItemId, ref survivorId, false);
5383
                                if (survivorId != null)
5384
                                    break;
5385
                            }
5386
                        }
5387
                    }
5388
                }
5389
            }
5390
        }
5391

    
5392
        /// <summary>
5393
        /// Label의 좌표를 구하는 메서드(ID2 기준의 좌표 -> SPPID 좌표)
5394
        /// </summary>
5395
        /// <param name="x"></param>
5396
        /// <param name="y"></param>
5397
        /// <param name="originX"></param>
5398
        /// <param name="originY"></param>
5399
        /// <param name="SPPIDLabelLocation"></param>
5400
        /// <param name="location"></param>
5401
        private void CalcLabelLocation(ref double x, ref double y, double originX, double originY, SPPIDLabelLocationInfo SPPIDLabelLocation, Location location)
5402
        {
5403
            if (location == Location.None)
5404
            {
5405
                x = originX;
5406
                y = originY;
5407
            }
5408
            else
5409
            {
5410
                if (location.HasFlag(Location.Center))
5411
                {
5412
                    x = (SPPIDLabelLocation.X1 + SPPIDLabelLocation.X2) / 2;
5413
                    y = (SPPIDLabelLocation.Y1 + SPPIDLabelLocation.Y2) / 2;
5414
                }
5415

    
5416
                if (location.HasFlag(Location.Left))
5417
                    x = SPPIDLabelLocation.X1;
5418
                else if (location.HasFlag(Location.Right))
5419
                    x = SPPIDLabelLocation.X2;
5420

    
5421
                if (location.HasFlag(Location.Down))
5422
                    y = SPPIDLabelLocation.Y1;
5423
                else if (location.HasFlag(Location.Up))
5424
                    y = SPPIDLabelLocation.Y2;
5425
            }
5426
        }
5427

    
5428
        /// <summary>
5429
        /// Symbol의 우선순위 Modeling 목록을 가져온다.
5430
        /// 1. Angle Valve
5431
        /// 2. 3개로 이루어진 Symbol Group
5432
        /// </summary>
5433
        /// <returns></returns>
5434
        private List<Symbol> GetPrioritySymbol()
5435
        {
5436
            DataTable symbolTable = document.SymbolTable;
5437
            // List에 순서대로 쌓는다.
5438
            List<Symbol> symbols = new List<Symbol>();
5439

    
5440
            // Angle Valve 부터
5441
            foreach (var symbol in document.SYMBOLS.FindAll(x => x.CONNECTORS.FindAll(y => y.Index == 0).Count == 2))
5442
            {
5443
                if (!symbols.Contains(symbol))
5444
                {
5445
                    double originX = 0;
5446
                    double originY = 0;
5447

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

    
5452
                    SlopeType slopeType1 = SlopeType.None;
5453
                    SlopeType slopeType2 = SlopeType.None;
5454
                    foreach (Connector connector in symbol.CONNECTORS.FindAll(x => x.Index == 0))
5455
                    {
5456
                        double connectorX = 0;
5457
                        double connectorY = 0;
5458
                        SPPIDUtil.ConvertPointBystring(connector.CONNECTPOINT, ref connectorX, ref connectorY);
5459
                        if (slopeType1 == SlopeType.None)
5460
                            slopeType1 = SPPIDUtil.CalcSlope(originX, originY, connectorX, connectorY);
5461
                        else
5462
                            slopeType2 = SPPIDUtil.CalcSlope(originX, originY, connectorX, connectorY);
5463
                    }
5464

    
5465
                    if ((slopeType1 == SlopeType.VERTICAL && slopeType2 == SlopeType.HORIZONTAL) ||
5466
                        (slopeType2 == SlopeType.VERTICAL && slopeType1 == SlopeType.HORIZONTAL))
5467
                        symbols.Add(symbol);
5468
                }
5469
            }
5470

    
5471
            List<Symbol> tempSymbols = new List<Symbol>();
5472
            // Conn 갯수 기준
5473
            foreach (var item in document.SYMBOLS)
5474
            {
5475
                if (!symbols.Contains(item))
5476
                    tempSymbols.Add(item);
5477
            }
5478
            tempSymbols.Sort(SortSymbolPriority);
5479
            symbols.AddRange(tempSymbols);
5480

    
5481
            return symbols;
5482
        }
5483

    
5484
        private void SetPriorityLine(List<Line> lines)
5485
        {
5486
            lines.Sort(SortLinePriority);
5487

    
5488
            int SortLinePriority(Line a, Line b)
5489
            {
5490
                // Branch 없는것부터
5491
                int branchRetval = CompareBranchLine(a, b);
5492
                if (branchRetval != 0)
5493
                {
5494
                    return branchRetval;
5495
                }
5496
                else
5497
                {
5498
                    // Symbol 연결 갯수
5499
                    int connSymbolRetval = CompareConnSymbol(a, b);
5500
                    if (connSymbolRetval != 0)
5501
                    {
5502
                        return connSymbolRetval;
5503
                    }
5504
                    else
5505
                    {
5506
                        // 아이템 연결 갯수(심볼, Line이면서 Not Branch)
5507
                        int connItemRetval = CompareConnItem(a, b);
5508
                        if (connItemRetval != 0)
5509
                        {
5510
                            return connItemRetval;
5511
                        }
5512
                        else
5513
                        {
5514
                            // ConnectedItem이 없는것
5515
                            int noneConnRetval = CompareNoneConn(a, b);
5516
                            if (noneConnRetval != 0)
5517
                            {
5518
                                return noneConnRetval;
5519
                            }
5520
                            else
5521
                            {
5522

    
5523
                            }
5524
                        }
5525
                    }
5526
                }
5527

    
5528
                return 0;
5529
            }
5530

    
5531
            int CompareNotSegmentLine(Line a, Line b)
5532
            {
5533
                List<Connector> connectorsA = a.CONNECTORS
5534
                    .Where(conn => conn.ConnectedObject != null && conn.ConnectedObject.GetType() == typeof(Symbol))
5535
                    .ToList();
5536

    
5537
                List<Connector> connectorsB = b.CONNECTORS
5538
                    .Where(conn => conn.ConnectedObject != null && conn.ConnectedObject.GetType() == typeof(Symbol))
5539
                    .ToList();
5540

    
5541
                // 오름차순
5542
                return connectorsB.Count.CompareTo(connectorsA.Count);
5543
            }
5544

    
5545
            int CompareConnSymbol(Line a, Line b)
5546
            {
5547
                List<Connector> connectorsA = a.CONNECTORS
5548
                    .Where(conn => conn.ConnectedObject != null && conn.ConnectedObject.GetType() == typeof(Symbol))
5549
                    .ToList();
5550

    
5551
                List<Connector> connectorsB = b.CONNECTORS
5552
                    .Where(conn => conn.ConnectedObject != null && conn.ConnectedObject.GetType() == typeof(Symbol))
5553
                    .ToList();
5554

    
5555
                // 오름차순
5556
                return connectorsB.Count.CompareTo(connectorsA.Count);
5557
            }
5558

    
5559
            int CompareConnItem(Line a, Line b)
5560
            {
5561
                List<Connector> connectorsA = a.CONNECTORS
5562
                    .Where(conn => conn.ConnectedObject != null && 
5563
                    (conn.ConnectedObject.GetType() == typeof(Symbol) || 
5564
                    (conn.ConnectedObject.GetType() == typeof(Line) && !SPPIDUtil.IsBranchLine((Line)conn.ConnectedObject, a))))
5565
                    .ToList();
5566

    
5567
                List<Connector> connectorsB = b.CONNECTORS
5568
                    .Where(conn => conn.ConnectedObject != null &&
5569
                    (conn.ConnectedObject.GetType() == typeof(Symbol) ||
5570
                    (conn.ConnectedObject.GetType() == typeof(Line) && !SPPIDUtil.IsBranchLine((Line)conn.ConnectedObject, b))))
5571
                    .ToList();
5572

    
5573
                // 오름차순
5574
                return connectorsB.Count.CompareTo(connectorsA.Count);
5575
            }
5576

    
5577
            int CompareBranchLine(Line a, Line b)
5578
            {
5579
                List<Connector> connectorsA = a.CONNECTORS
5580
                    .Where(conn => conn.ConnectedObject != null && conn.ConnectedObject.GetType() == typeof(Line) && SPPIDUtil.IsBranchLine(a, conn.ConnectedObject as Line))
5581
                    .ToList();
5582
                List<Connector> connectorsB = b.CONNECTORS
5583
                    .Where(conn => conn.ConnectedObject != null && conn.ConnectedObject.GetType() == typeof(Line) && SPPIDUtil.IsBranchLine(b, conn.ConnectedObject as Line))
5584
                    .ToList();
5585

    
5586
                // 내림차순
5587
                return connectorsA.Count.CompareTo(connectorsB.Count);
5588
            }
5589

    
5590
            int CompareNoneConn(Line a, Line b)
5591
            {
5592
                List<Connector> connectorsA = a.CONNECTORS
5593
                    .Where(conn => conn.ConnectedObject == null)
5594
                    .ToList();
5595

    
5596
                List<Connector> connectorsB = b.CONNECTORS
5597
                    .Where(conn => conn.ConnectedObject == null)
5598
                    .ToList();
5599

    
5600
                // 오름차순
5601
                return connectorsB.Count.CompareTo(connectorsA.Count);
5602
            }
5603
        }
5604

    
5605
        private void SortText(List<Text> texts)
5606
        {
5607
            texts.Sort(Sort);
5608

    
5609
            int Sort(Text a, Text b)
5610
            {
5611
                int yRetval = CompareY(a, b);
5612
                if (yRetval != 0)
5613
                {
5614
                    return yRetval;
5615
                }
5616
                else
5617
                {
5618
                    return CompareX(a, b);
5619
                }
5620
            }
5621

    
5622
            int CompareY(Text a, Text b)
5623
            {
5624
                return a.LOCATION_Y.CompareTo(b.LOCATION_Y);
5625
            }
5626

    
5627
            int CompareX(Text a, Text b)
5628
            {
5629
                return a.LOCATION_X.CompareTo(b.LOCATION_X);
5630
            }
5631
        }
5632
        private void SortNote(List<Note> notes)
5633
        {
5634
            notes.Sort(Sort);
5635

    
5636
            int Sort(Note a, Note b)
5637
            {
5638
                int yRetval = CompareY(a, b);
5639
                if (yRetval != 0)
5640
                {
5641
                    return yRetval;
5642
                }
5643
                else
5644
                {
5645
                    return CompareX(a, b);
5646
                }
5647
            }
5648

    
5649
            int CompareY(Note a, Note b)
5650
            {
5651
                return a.LOCATION_Y.CompareTo(b.LOCATION_Y);
5652
            }
5653

    
5654
            int CompareX(Note a, Note b)
5655
            {
5656
                return a.LOCATION_X.CompareTo(b.LOCATION_X);
5657
            }
5658
        }
5659

    
5660
        private void SortBranchLines()
5661
        {
5662
            BranchLines.Sort(SortBranchLine);
5663
            int SortBranchLine(Line a, Line b)
5664
            {
5665
                int countA = a.CONNECTORS.FindAll(x => x.ConnectedObject != null &&
5666
                 x.ConnectedObject.GetType() == typeof(Line) &&
5667
                 SPPIDUtil.IsBranchLine(x.ConnectedObject as Line, a) &&
5668
                 string.IsNullOrEmpty(((Line)x.ConnectedObject).SPPID.ModelItemId)).Count;
5669

    
5670
                int countB = b.CONNECTORS.FindAll(x => x.ConnectedObject != null &&
5671
                 x.ConnectedObject.GetType() == typeof(Line) &&
5672
                 SPPIDUtil.IsBranchLine(x.ConnectedObject as Line, b) &&
5673
                 string.IsNullOrEmpty(((Line)x.ConnectedObject).SPPID.ModelItemId)).Count;
5674

    
5675
                // 내림차순
5676
                return countA.CompareTo(countB);
5677
            }
5678
        }
5679

    
5680
        private static int SortSymbolPriority(Symbol a, Symbol b)
5681
        {
5682
            int countA = a.CONNECTORS.FindAll(x => !string.IsNullOrEmpty(x.CONNECTEDITEM) && x.CONNECTEDITEM != "None").Count;
5683
            int countB = b.CONNECTORS.FindAll(x => !string.IsNullOrEmpty(x.CONNECTEDITEM) && x.CONNECTEDITEM != "None").Count;
5684
            int retval = countB.CompareTo(countA);
5685
            if (retval != 0)
5686
                return retval;
5687
            else
5688
                return a.SPPID.ORIGINAL_X.CompareTo(b.SPPID.ORIGINAL_X);
5689
        }
5690

    
5691
        private string GetSPPIDFileName(LMModelItem modelItem)
5692
        {
5693
            string symbolPath = null;
5694
            foreach (LMRepresentation rep in modelItem.Representations)
5695
            {
5696
                if (!DBNull.Value.Equals(rep.get_FileName()) && !string.IsNullOrEmpty(rep.get_FileName()))
5697
                {
5698
                    symbolPath = rep.get_FileName();
5699
                    break;
5700
                }
5701
            }
5702
            return symbolPath;
5703
        }
5704

    
5705
        private string GetSPPIDFileName(string modelItemId)
5706
        {
5707
            LMModelItem modelItem = dataSource.GetModelItem(modelItemId);
5708
            string symbolPath = null;
5709
            foreach (LMRepresentation rep in modelItem.Representations)
5710
            {
5711
                if (!DBNull.Value.Equals(rep.get_FileName()) && !string.IsNullOrEmpty(rep.get_FileName()))
5712
                {
5713
                    symbolPath = rep.get_FileName();
5714
                    break;
5715
                }
5716
            }
5717
            ReleaseCOMObjects(modelItem);
5718
            return symbolPath;
5719
        }
5720

    
5721
        /// <summary>
5722
        /// Graphic OID로 해당 Symbol의 크기를 구하여 Zoom
5723
        /// </summary>
5724
        /// <param name="graphicOID"></param>
5725
        /// <param name="milliseconds"></param>
5726
        private void ZoomObjectByGraphicOID(string graphicOID, int milliseconds = 150)
5727
        {
5728
            if (radApp.ActiveDocument.ActiveSheet.DrawingObjects[graphicOID] != null)
5729
            {
5730
                double minX = 0;
5731
                double minY = 0;
5732
                double maxX = 0;
5733
                double maxY = 0;
5734
                radApp.ActiveDocument.ActiveSheet.DrawingObjects[graphicOID].Range(out minX, out minY, out maxX, out maxY);
5735
                radApp.ActiveWindow.ZoomArea2(minX - 0.007, minY - 0.007, maxX + 0.007, maxY + 0.007, null);
5736

    
5737
                Thread.Sleep(milliseconds);
5738
            }
5739
        }
5740

    
5741
        /// <summary>
5742
        /// ComObject를 Release
5743
        /// </summary>
5744
        /// <param name="objVars"></param>
5745
        public void ReleaseCOMObjects(params object[] objVars)
5746
        {
5747
            if (objVars != null)
5748
            {
5749
                int intNewRefCount = 0;
5750
                foreach (object obj in objVars)
5751
                {
5752
                    if (!Information.IsNothing(obj) && System.Runtime.InteropServices.Marshal.IsComObject(obj))
5753
                        intNewRefCount = intNewRefCount + System.Runtime.InteropServices.Marshal.FinalReleaseComObject(obj);
5754
                }
5755
            }
5756
        }
5757

    
5758
        /// IDisposable 구현
5759
        ~AutoModeling()
5760
        {
5761
            this.Dispose(false);
5762
        }
5763

    
5764
        private bool disposed;
5765
        public void Dispose()
5766
        {
5767
            this.Dispose(true);
5768
            GC.SuppressFinalize(this);
5769
        }
5770

    
5771
        protected virtual void Dispose(bool disposing)
5772
        {
5773
            if (this.disposed) return;
5774
            if (disposing)
5775
            {
5776
                // IDisposable 인터페이스를 구현하는 멤버들을 여기서 정리합니다.
5777
            }
5778
            // .NET Framework에 의하여 관리되지 않는 외부 리소스들을 여기서 정리합니다.
5779
            this.disposed = true;
5780
        }
5781
    }
5782
}
클립보드 이미지 추가 (최대 크기: 500 MB)