프로젝트

일반

사용자정보

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

hytos / DTI_PID / SPPIDConverter / AutoModeling.cs @ c5b2c7ff

이력 | 보기 | 이력해설 | 다운로드 (270 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
        List<string> FlowMarkRepIds = new List<string>();
47

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

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

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

    
71
                }
72
            }
73
        }
74

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

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

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

    
161
                Log.Write("End Modeling");
162
                radApp.ActiveWindow.Fit();
163

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

    
181
                ReleaseCOMObjects(dataSource);
182
                dataSource = null;
183
                ReleaseCOMObjects(_placement);
184
                _placement = null;
185

    
186
                Thread.Sleep(1000);
187
            }
188
        }
189

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

    
259
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetAllProgress, step1_Line.Count);
260
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Line Modeling - 1");
261

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

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

    
303
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetAllProgress, stepLast_Line.Count);
304
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Line Modeling - 3");
305
            foreach (var item in stepLast_Line)
306
            {
307
                try
308
                {
309
                    if (document.VentDrainLine.Contains(item))
310
                        continue;
311
                    NewLineModeling(item);
312
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
313
                }
314
                catch (Exception ex)
315
                {
316
                    Log.Write("Error in NewLineModeling");
317
                    Log.Write("UID : " + item.UID);
318
                    Log.Write(ex.Message);
319
                    Log.Write(ex.StackTrace);
320
                }
321
            }
322
        }
323
        private void RunVentDrainModeling()
324
        {
325
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetAllProgress, document.VentDrainLine.Count);
326
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Vent Drain Modeling");
327
            foreach (var item in document.VentDrainLine)
328
            {
329
                try
330
                {
331
                    Connector connector = item.CONNECTORS.Find(x => x.ConnectedObject != null && x.ConnectedObject.GetType() == typeof(Symbol));
332
                    if (connector != null)
333
                    {
334
                        SetCoordinate();
335
                        SymbolModelingBySymbol(connector.ConnectedObject as Symbol);
336
                        NewLineModeling(item, true);
337
                    }
338
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
339
                }
340
                catch (Exception ex)
341
                {
342
                    Log.Write("Error in NewLineModeling");
343
                    Log.Write("UID : " + item.UID);
344
                    Log.Write(ex.Message);
345
                    Log.Write(ex.StackTrace);
346
                }
347

    
348
                void SetCoordinate()
349
                {
350
                    Connector branchConnector = item.CONNECTORS.Find(loop => loop.ConnectedObject != null && loop.ConnectedObject.GetType() == typeof(Line));
351
                    if (branchConnector != null)
352
                    {
353
                        Line connLine = branchConnector.ConnectedObject as Line;
354
                        double x = 0;
355
                        double y = 0;
356
                        GetTargetLineConnectorPoint(branchConnector, item, ref x, ref y);
357
                        LMConnector targetConnector = FindTargetLMConnectorForBranch(item, connLine, ref x, ref y);
358
                        if (targetConnector != null)
359
                        {
360
                            List<Symbol> group = new List<Symbol>();
361
                            SPPIDUtil.FindConnectedSymbolGroup(document, item.CONNECTORS.Find(loop => loop != branchConnector).ConnectedObject as Symbol, group);
362
                            if (item.SlopeType == SlopeType.HORIZONTAL)
363
                            {
364
                                item.SPPID.START_Y = y;
365
                                item.SPPID.END_Y = y;
366
                                foreach (var symbol in group)
367
                                {
368
                                    symbol.SPPID.ORIGINAL_Y = y;
369
                                    symbol.SPPID.SPPID_Y = y;
370
                                }
371
                            }
372
                            else if (item.SlopeType == SlopeType.VERTICAL)
373
                            {
374
                                item.SPPID.START_X = x;
375
                                item.SPPID.END_X = x;
376
                                foreach (var symbol in group)
377
                                {
378
                                    symbol.SPPID.ORIGINAL_X = x;
379
                                    symbol.SPPID.SPPID_X = x;
380
                                }
381
                            }
382
                        }
383
                        ReleaseCOMObjects(targetConnector);
384
                        targetConnector = null;
385
                    }
386
                }
387
            }
388
        }
389
        private void RunClearNominalDiameter()
390
        {
391
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetAllProgress, document.SYMBOLS.Count + document.LINES.Count);
392
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Clear Attribute");
393
            List<string> endClearModelItemID = new List<string>();
394
            for (int i = 0; i < document.LINES.Count; i++)
395
            {
396
                Line item = document.LINES[i];
397
                string modelItemID = item.SPPID.ModelItemId;
398
                if (!string.IsNullOrEmpty(modelItemID))
399
                {
400
                    LMModelItem modelItem = dataSource.GetModelItem(modelItemID);
401
                    if (modelItem != null)
402
                    {
403
                        LMAAttribute attribute = modelItem.Attributes["NominalDiameter"];
404
                        if (attribute != null)
405
                            attribute.set_Value(DBNull.Value);
406

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

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

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

    
507
                LMRelationships relationships = new LMRelationships();
508
                relationships.Collect(dataSource, Filter: filter);
509

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

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

    
669
                    }
670
                }
671
                SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
672
            }
673

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

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

    
891
            foreach (var item in document.LINENUMBERS)
892
            {
893
                try
894
                {
895
                    LineNumberCorrectModeling(item);
896
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
897
                }
898
                catch (Exception ex)
899
                {
900
                    Log.Write("Error in RunCorrectAssociationText");
901
                    Log.Write("UID : " + item.UID);
902
                    Log.Write(ex.Message);
903
                    Log.Write(ex.StackTrace);
904
                }
905
            }
906
        }
907
        private void RunETC()
908
        {
909
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetAllProgress, FlowMarkRepIds.Count);
910
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "ETC");
911
            foreach (var item in FlowMarkRepIds)
912
            {
913
                LMLabelPersist label = dataSource.GetLabelPersist(item);
914
                if (label != null)
915
                {
916
                    label.get_GraphicOID();
917
                    DependencyObject dependency = radApp.ActiveDocument.ActiveSheet.DrawingObjects[label.get_GraphicOID().ToString()] as DependencyObject;
918
                    if (dependency != null)
919
                        dependency.BringToFront();
920
                }
921
                ReleaseCOMObjects(label);
922
                label = null;
923
                SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
924
            }
925
        }
926
        /// <summary>
927
        /// 도면 생성 메서드
928
        /// </summary>
929
        private bool CreateDocument(ref string drawingNumber, ref string drawingName)
930
        {
931
            Log.Write("------------------ Start create document ------------------");
932
            GetDrawingNameAndNumber(ref drawingName, ref drawingNumber);
933
            Log.Write("Drawing name : " + drawingName);
934
            Log.Write("Drawing number : " + drawingNumber);
935
            Thread.Sleep(1000);
936
            newDrawing = application.Drawings.Add(document.Unit, document.Template, drawingNumber, drawingName);
937
            if (newDrawing != null)
938
            {
939
                document.SPPID_DrawingNumber = drawingNumber;
940
                document.SPPID_DrawingName = drawingName;
941
                Thread.Sleep(1000);
942
                radApp.ActiveWindow.Fit();
943
                Thread.Sleep(1000);
944
                radApp.ActiveWindow.Zoom = 2000;
945
                Thread.Sleep(2000);
946

    
947
                //current LMDrawing 가져오기
948
                LMAFilter filter = new LMAFilter();
949
                LMACriterion criterion = new LMACriterion();
950
                filter.ItemType = "Drawing";
951
                criterion.SourceAttributeName = "Name";
952
                criterion.Operator = "=";
953
                criterion.set_ValueAttribute(drawingName);
954
                filter.get_Criteria().Add(criterion);
955

    
956
                LMDrawings drawings = new LMDrawings();
957
                drawings.Collect(dataSource, Filter: filter);
958

    
959
                drawingID = ((dynamic)drawings).Nth(1).Id;
960
                ReleaseCOMObjects(filter);
961
                ReleaseCOMObjects(criterion);
962
                ReleaseCOMObjects(drawings);
963
                filter = null;
964
                criterion = null;
965
                drawings = null;
966
            }
967
            else
968
                Log.Write("Fail Create Drawing");
969

    
970
            if (newDrawing != null)
971
                return true;
972
            else
973
                return false;
974
        }
975

    
976
        /// <summary>
977
        /// DrawingName, DrawingNumber를 확인하여 중복이 있으면 _1을 붙이고 +1씩 한다.
978
        /// </summary>
979
        /// <param name="drawingName"></param>
980
        /// <param name="drawingNumber"></param>
981
        private void GetDrawingNameAndNumber(ref string drawingName, ref string drawingNumber)
982
        {
983
            LMDrawings drawings = new LMDrawings();
984
            drawings.Collect(dataSource);
985

    
986
            List<string> drawingNameList = new List<string>();
987
            List<string> drawingNumberList = new List<string>();
988

    
989
            foreach (LMDrawing item in drawings)
990
            {
991
                drawingNameList.Add(item.Attributes["Name"].get_Value().ToString());
992
                drawingNumberList.Add(item.Attributes["DrawingNumber"].get_Value().ToString());
993
            }
994

    
995
            int nameLength = drawingName.Length;
996
            while (drawingNameList.Contains(drawingName))
997
            {
998
                if (nameLength == drawingName.Length)
999
                    drawingName += "-1";
1000
                else
1001
                {
1002
                    int index = Convert.ToInt32(drawingName.Remove(0, nameLength + 1));
1003
                    drawingName = drawingName.Substring(0, nameLength + 1);
1004
                    drawingName += ++index;
1005
                }
1006
            }
1007

    
1008
            int numberLength = drawingNumber.Length;
1009
            while (drawingNameList.Contains(drawingNumber))
1010
            {
1011
                if (numberLength == drawingNumber.Length)
1012
                    drawingNumber += "-1";
1013
                else
1014
                {
1015
                    int index = Convert.ToInt32(drawingNumber.Remove(0, numberLength + 1));
1016
                    drawingNumber = drawingNumber.Substring(0, numberLength + 1);
1017
                    drawingNumber += ++index;
1018
                }
1019
            }
1020
            ReleaseCOMObjects(drawings);
1021
            drawings = null;
1022
        }
1023

    
1024
        /// <summary>
1025
        /// 도면 크기 구하는 메서드
1026
        /// </summary>
1027
        /// <returns></returns>
1028
        private bool DocumentCoordinateCorrection()
1029
        {
1030
            if (Settings.Default.DrawingX != 0 && Settings.Default.DrawingY != 0)
1031
            {
1032
                Log.Write("Setting Drawing X, Drawing Y");
1033
                document.SetSPPIDLocation(Settings.Default.DrawingX, Settings.Default.DrawingY);
1034
                Log.Write("Start coordinate correction");
1035
                document.CoordinateCorrection();
1036
                return true;
1037
            }
1038
            else
1039
            {
1040
                Log.Write("Need Drawing X, Y");
1041
                return false;
1042
            }
1043
        }
1044

    
1045
        /// <summary>
1046
        /// 심볼을 실제로 Modeling 메서드
1047
        /// </summary>
1048
        /// <param name="symbol">생성할 심볼</param>
1049
        /// <param name="targetSymbol">연결되어 있는 심볼</param>
1050
        private void SymbolModeling(Symbol symbol, Symbol targetSymbol)
1051
        {
1052
            // OWNERSYMBOL Attribute, 값을 가지고 있을 경우
1053
            BaseModel.Attribute itemAttribute = symbol.ATTRIBUTES.Find(attr => attr.ATTRIBUTE == "OWNERSYMBOL");
1054
            if (itemAttribute != null && (string.IsNullOrEmpty(itemAttribute.VALUE) || itemAttribute.VALUE != "None"))
1055
                return;
1056
            // 이미 모델링 됐을 경우
1057
            else if (!string.IsNullOrEmpty(symbol.SPPID.RepresentationId))
1058
                return;
1059

    
1060
            LMSymbol _LMSymbol = null;
1061

    
1062
            string mappingPath = symbol.SPPID.MAPPINGNAME;
1063
            double x = symbol.SPPID.ORIGINAL_X;
1064
            double y = symbol.SPPID.ORIGINAL_Y;
1065
            int mirror = 0;
1066
            double angle = symbol.ANGLE;
1067

    
1068
            // OPC 일경우 180도 일때 Mirror
1069
            if (mappingPath.Contains("Piping OPC's") && angle == Math.PI)
1070
                mirror = 1;
1071

    
1072
            // Mirror 계산
1073
            if (symbol.FLIP == 1)
1074
            {
1075
                mirror = 1;
1076
                angle += Math.PI;
1077
            }
1078

    
1079
            if (targetSymbol != null && !string.IsNullOrEmpty(targetSymbol.SPPID.RepresentationId))
1080
            {
1081
                LMSymbol _TargetItem = dataSource.GetSymbol(targetSymbol.SPPID.RepresentationId);   /// RepresentationId로 SPPID 심볼을 찾음
1082
                Connector connector = SPPIDUtil.FindSymbolConnectorByUID(document, symbol.UID, targetSymbol);
1083
                if (connector != null)
1084
                    GetTargetSymbolConnectorPoint(connector, targetSymbol, ref x, ref y);
1085

    
1086
                LMConnector temp = LineModelingForSymbolZeroLength(symbol, _TargetItem, x, y);
1087
                _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle, TargetItem: _TargetItem);
1088
                if (temp != null)
1089
                    _placement.PIDRemovePlacement(temp.AsLMRepresentation());
1090
                ReleaseCOMObjects(temp);
1091
                temp = null;
1092

    
1093
                if (_LMSymbol != null && _TargetItem != null)
1094
                    symbol.SPPID.RepresentationId = _LMSymbol.AsLMRepresentation().Id;
1095

    
1096
                ReleaseCOMObjects(_TargetItem);
1097
            }
1098
            else
1099
                _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle);
1100

    
1101
            if (_LMSymbol != null)
1102
            {
1103
                _LMSymbol.Commit();
1104

    
1105
                // ConnCheck
1106
                List<string> ids = new List<string>();
1107
                foreach (LMConnector item in _LMSymbol.Connect1Connectors)
1108
                {
1109
                    if (item.get_ItemStatus() == "Active" && !ids.Contains(item.Id))
1110
                        ids.Add(item.Id);
1111
                    ReleaseCOMObjects(item);
1112
                }
1113
                foreach (LMConnector item in _LMSymbol.Connect2Connectors)
1114
                {
1115
                    if (item.get_ItemStatus() == "Active" && !ids.Contains(item.Id))
1116
                        ids.Add(item.Id);
1117
                    ReleaseCOMObjects(item);
1118
                }
1119

    
1120
                int createdSymbolCount = document.SYMBOLS.FindAll(i => i.CONNECTORS.Find(j => j.CONNECTEDITEM == symbol.UID) != null && !string.IsNullOrEmpty(i.SPPID.RepresentationId)).Count;
1121
                if (targetSymbol == null && ids.Count != createdSymbolCount)
1122
                {
1123
                    double currentX = _LMSymbol.get_XCoordinate();
1124
                    double currentY = _LMSymbol.get_YCoordinate();
1125

    
1126

    
1127
                }
1128

    
1129
                symbol.SPPID.RepresentationId = _LMSymbol.AsLMRepresentation().Id;
1130
                symbol.SPPID.ModelItemID = _LMSymbol.ModelItemID;
1131
                symbol.SPPID.GraphicOID = _LMSymbol.get_GraphicOID().ToString();
1132

    
1133
                foreach (var item in symbol.ChildSymbols)
1134
                    CreateChildSymbol(item, _LMSymbol, symbol);
1135

    
1136
                symbol.SPPID.SPPID_X = _LMSymbol.get_XCoordinate();
1137
                symbol.SPPID.SPPID_Y = _LMSymbol.get_YCoordinate();
1138

    
1139
                double[] range = null;
1140
                GetSPPIDSymbolRange(symbol, ref range);
1141
                symbol.SPPID.SPPID_Min_X = range[0];
1142
                symbol.SPPID.SPPID_Min_Y = range[1];
1143
                symbol.SPPID.SPPID_Max_X = range[2];
1144
                symbol.SPPID.SPPID_Max_Y = range[3];
1145

    
1146
                foreach (var item in symbol.SPPID.CorrectionX_GroupSymbols)
1147
                    item.SPPID.ORIGINAL_X = symbol.SPPID.SPPID_X;
1148
                foreach (var item in symbol.SPPID.CorrectionY_GroupSymbols)
1149
                    item.SPPID.ORIGINAL_Y = symbol.SPPID.SPPID_Y;
1150

    
1151
                ReleaseCOMObjects(_LMSymbol);
1152
            }
1153
        }
1154
        /// <summary>
1155
        /// targetX와 targetY 기준 제일 먼 PipingPoint에 TempLine Modeling
1156
        /// Signal Point는 고려하지 않음
1157
        /// </summary>
1158
        /// <param name="symbol"></param>
1159
        /// <param name="_TargetItem"></param>
1160
        /// <param name="targetX"></param>
1161
        /// <param name="targetY"></param>
1162
        /// <returns></returns>
1163
        private LMConnector LineModelingForSymbolZeroLength(Symbol symbol, LMSymbol _TargetItem, double targetX, double targetY)
1164
        {
1165
            LMConnector tempConnector = null;
1166

    
1167
            List<Symbol> group = new List<Symbol>();
1168
            SPPIDUtil.FindConnectedSymbolGroup(document, symbol, group);
1169
            if (group.FindAll(loopX => !string.IsNullOrEmpty(loopX.SPPID.RepresentationId)).Count == 1)
1170
            {
1171
                List<Connector> connectors = new List<Connector>();
1172
                foreach (var item in group)
1173
                    connectors.AddRange(item.CONNECTORS.FindAll(loopX => loopX.ConnectedObject != null && loopX.ConnectedObject.GetType() == typeof(Line)));
1174
                /// Primary or Secondary Type Line만 고려
1175
                Connector _connector = connectors.Find(loopX => loopX.ConnectedObject != null && loopX.ConnectedObject.GetType() == typeof(Line) &&
1176
                (((Line)loopX.ConnectedObject).TYPE == "Primary" || ((Line)loopX.ConnectedObject).TYPE == "Secondary"));
1177
                if (_connector != null)
1178
                {
1179
                    string sppidLine = ((Line)_connector.ConnectedObject).SPPID.MAPPINGNAME;
1180
                    List<double[]> pointInfos = getPipingPoints(_TargetItem);
1181
                    /// PipingPoint가 2개 이상만
1182
                    if (pointInfos.Count >= 2)
1183
                    {
1184
                        double lineX = 0;
1185
                        double lineY = 0;
1186
                        double length = 0;
1187
                        foreach (var item in pointInfos)
1188
                        {
1189
                            double tempX = item[1];
1190
                            double tempY = item[2];
1191

    
1192
                            double calcDistance = SPPIDUtil.CalcPointToPointdDistance(targetX, targetY, tempX, tempY);
1193
                            if (calcDistance > length)
1194
                            {
1195
                                lineX = tempX;
1196
                                lineY = tempY;
1197
                            }
1198
                        }
1199

    
1200
                        _LMAItem _LMAItem = _placement.PIDCreateItem(sppidLine);
1201
                        PlaceRunInputs placeRunInputs = new PlaceRunInputs();
1202
                        placeRunInputs.AddPoint(-1, -1);
1203
                        placeRunInputs.AddSymbolTarget(_TargetItem, lineX, lineY);
1204
                        tempConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
1205
                        if (tempConnector != null)
1206
                            tempConnector.Commit();
1207
                        ReleaseCOMObjects(_LMAItem);
1208
                        _LMAItem = null;
1209
                        ReleaseCOMObjects(placeRunInputs);
1210
                        placeRunInputs = null;
1211
                    }
1212
                }
1213
            }
1214

    
1215
            return tempConnector;
1216
        }
1217
        /// <summary>
1218
        /// Symbol의 PipingPoints를 구함
1219
        /// SignalPoint는 고려하지 않음
1220
        /// </summary>
1221
        /// <param name="symbol"></param>
1222
        /// <returns></returns>
1223
        private List<double[]> getPipingPoints(LMSymbol symbol)
1224
        {
1225
            LMModelItem modelItem = symbol.ModelItemObject;
1226
            LMPipingPoints pipingPoints = null;
1227
            if (modelItem.get_ItemTypeName() == "PipingComp")
1228
            {
1229
                LMPipingComp pipingComp = dataSource.GetPipingComp(modelItem.Id);
1230
                pipingPoints = pipingComp.PipingPoints;
1231
                ReleaseCOMObjects(pipingComp);
1232
                pipingComp = null;
1233
            }
1234
            else if (modelItem.get_ItemTypeName() == "Instrument")
1235
            {
1236
                LMInstrument instrument = dataSource.GetInstrument(modelItem.Id);
1237
                pipingPoints = instrument.PipingPoints;
1238
                ReleaseCOMObjects(instrument);
1239
                instrument = null;
1240
            }
1241
            else
1242
                Log.Write("다른 Type");
1243

    
1244
            List<double[]> info = new List<double[]>();
1245
            if (pipingPoints != null)
1246
            {
1247
                foreach (LMPipingPoint pipingPoint in pipingPoints)
1248
                {
1249
                    foreach (LMAAttribute attribute in pipingPoint.Attributes)
1250
                    {
1251
                        if (attribute.Name == "PipingPointNumber")
1252
                        {
1253
                            int index = Convert.ToInt32(attribute.get_Value());
1254
                            if (info.Find(loopX => loopX[0] == index) == null)
1255
                            {
1256
                                double x = 0;
1257
                                double y = 0;
1258
                                if (_placement.PIDConnectPointLocation(symbol, index, ref x, ref y))
1259
                                    info.Add(new double[] { index, x, y });
1260
                            }
1261
                        }
1262
                    }
1263
                }
1264
            }
1265
            ReleaseCOMObjects(modelItem);
1266
            modelItem = null;
1267
            ReleaseCOMObjects(pipingPoints);
1268
            pipingPoints = null;
1269

    
1270
            return info;
1271
        }
1272

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

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

    
1295
        private void RemoveSymbol(List<Symbol> symbols)
1296
        {
1297
            foreach (var symbol in symbols)
1298
            {
1299
                if (!string.IsNullOrEmpty(symbol.SPPID.RepresentationId))
1300
                {
1301
                    LMSymbol _LMSymbol = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
1302
                    if (_LMSymbol != null)
1303
                    {
1304
                        _placement.PIDRemovePlacement(_LMSymbol.AsLMRepresentation());
1305
                        ReleaseCOMObjects(_LMSymbol);
1306
                    }
1307
                }
1308

    
1309
                symbol.SPPID.RepresentationId = string.Empty;
1310
                symbol.SPPID.ModelItemID = string.Empty;
1311
                symbol.SPPID.SPPID_X = double.NaN;
1312
                symbol.SPPID.SPPID_Y = double.NaN;
1313
                symbol.SPPID.SPPID_Min_X = double.NaN;
1314
                symbol.SPPID.SPPID_Min_Y = double.NaN;
1315
                symbol.SPPID.SPPID_Max_X = double.NaN;
1316
                symbol.SPPID.SPPID_Max_Y = double.NaN;
1317
            }
1318
        }
1319

    
1320
        /// <summary>
1321
        /// ID2의 Symbol Width와 Height를 비교해서 상대적인 SPPID Connector좌표를 가져온다.
1322
        /// </summary>
1323
        /// <param name="targetConnector"></param>
1324
        /// <param name="targetSymbol"></param>
1325
        /// <param name="x"></param>
1326
        /// <param name="y"></param>
1327
        private void GetTargetSymbolConnectorPoint(Connector targetConnector, Symbol targetSymbol, ref double x, ref double y)
1328
        {
1329
            LMSymbol _TargetItem = dataSource.GetSymbol(targetSymbol.SPPID.RepresentationId);
1330

    
1331
            double[] range = null;
1332
            List<double[]> points = new List<double[]>();
1333
            GetSPPIDSymbolRangeAndConnectionPoints(targetSymbol, ref range, points);
1334
            double x1 = range[0];
1335
            double y1 = range[1];
1336
            double x2 = range[2];
1337
            double y2 = range[3];
1338

    
1339
            // Origin 기준 Connector의 위치차이
1340
            double sceneX = 0;
1341
            double sceneY = 0;
1342
            SPPIDUtil.ConvertPointBystring(targetConnector.SCENECONNECTPOINT, ref sceneX, ref sceneY);
1343
            double originX = 0;
1344
            double originY = 0;
1345
            SPPIDUtil.ConvertPointBystring(targetSymbol.ORIGINALPOINT, ref originX, ref originY);
1346
            double gapX = originX - sceneX;
1347
            double gapY = originY - sceneY;
1348

    
1349
            // SPPID Symbol과 ID2 심볼의 크기 차이
1350
            double sizeWidth = 0;
1351
            double sizeHeight = 0;
1352
            SPPIDUtil.ConvertPointBystring(targetSymbol.SIZE, ref sizeWidth, ref sizeHeight);
1353
            if (sizeWidth == 0 || sizeHeight == 0)
1354
                throw new Exception("Check symbol size! \r\nUID : " + targetSymbol.UID);
1355

    
1356
            double percentX = (x2 - x1) / sizeWidth;
1357
            double percentY = (y2 - y1) / sizeHeight;
1358

    
1359
            double SPPIDgapX = gapX * percentX;
1360
            double SPPIDgapY = gapY * percentY;
1361

    
1362
            double[] SPPIDOriginPoint = new double[] { _TargetItem.get_XCoordinate() - SPPIDgapX, _TargetItem.get_YCoordinate() + SPPIDgapY };
1363
            double distance = double.MaxValue;
1364
            double[] resultPoint;
1365
            foreach (var point in points)
1366
            {
1367
                double result = SPPIDUtil.CalcPointToPointdDistance(point[0], point[1], SPPIDOriginPoint[0], SPPIDOriginPoint[1]);
1368
                if (distance > result)
1369
                {
1370
                    distance = result;
1371
                    resultPoint = point;
1372
                    x = point[0];
1373
                    y = point[1];
1374
                }
1375
            }
1376

    
1377
            ReleaseCOMObjects(_TargetItem);
1378
        }
1379

    
1380
        private void GetTargetLineConnectorPoint(Connector targetConnector, Line targetLine, ref double x, ref double y)
1381
        {
1382
            int index = targetLine.CONNECTORS.IndexOf(targetConnector);
1383
            if (index == 0)
1384
            {
1385
                x = targetLine.SPPID.START_X;
1386
                y = targetLine.SPPID.START_Y;
1387
            }
1388
            else
1389
            {
1390
                x = targetLine.SPPID.END_X;
1391
                y = targetLine.SPPID.END_Y;
1392
            }
1393
        }
1394

    
1395
        /// <summary>
1396
        /// SPPID Symbol의 Range를 구한다.
1397
        /// </summary>
1398
        /// <param name="symbol"></param>
1399
        /// <param name="range"></param>
1400
        private void GetSPPIDSymbolRangeAndConnectionPoints(Symbol symbol, ref double[] range, List<double[]> points)
1401
        {
1402
            LMSymbol _TargetItem = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
1403
            Ingr.RAD2D.Symbol2d symbol2d = radApp.ActiveDocument.ActiveSheet.DrawingObjects[_TargetItem.get_GraphicOID().ToString()];
1404
            double x1 = 0;
1405
            double y1 = 0;
1406
            double x2 = 0;
1407
            double y2 = 0;
1408
            symbol2d.Range(out x1, out y1, out x2, out y2);
1409
            range = new double[] { x1, y1, x2, y2 };
1410

    
1411
            for (int i = 1; i < int.MaxValue; i++)
1412
            {
1413
                double connX = 0;
1414
                double connY = 0;
1415
                if (_placement.PIDConnectPointLocation(_TargetItem, i, ref connX, ref connY))
1416
                    points.Add(new double[] { connX, connY });
1417
                else
1418
                    break;
1419
            }
1420

    
1421
            foreach (var childSymbol in symbol.ChildSymbols)
1422
                GetSPPIDChildSymbolRange(childSymbol, ref range, points);
1423

    
1424
            ReleaseCOMObjects(_TargetItem);
1425
        }
1426

    
1427
        private void GetSPPIDSymbolRange(Symbol symbol, ref double[] range, bool bOnlySymbol = false, bool bForGraphic = false)
1428
        {
1429
            LMSymbol _TargetItem = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
1430
            if (_TargetItem != null)
1431
            {
1432
                Ingr.RAD2D.Symbol2d symbol2d = radApp.ActiveDocument.ActiveSheet.DrawingObjects[_TargetItem.get_GraphicOID().ToString()];
1433
                double x1 = 0;
1434
                double y1 = 0;
1435
                double x2 = 0;
1436
                double y2 = 0;
1437
                if (!bForGraphic)
1438
                {
1439
                    symbol2d.Range(out x1, out y1, out x2, out y2);
1440
                    range = new double[] { x1, y1, x2, y2 };
1441
                }
1442
                else
1443
                {
1444
                    x1 = double.MaxValue;
1445
                    y1 = double.MaxValue;
1446
                    x2 = double.MinValue;
1447
                    y2 = double.MinValue;
1448
                    range = new double[] { x1, y1, x2, y2 };
1449

    
1450
                    foreach (var item in symbol2d.DrawingObjects)
1451
                    {
1452
                        if (item.GetType() == typeof(Ingr.RAD2D.Line2d))
1453
                        {
1454
                            Ingr.RAD2D.Line2d rangeObject = item as Ingr.RAD2D.Line2d;
1455
                            if (rangeObject.Layer == "Default")
1456
                            {
1457
                                rangeObject.Range(out x1, out y1, out x2, out y2);
1458
                                range = new double[] {
1459
                                Math.Min(x1, range[0]),
1460
                                Math.Min(y1, range[1]),
1461
                                Math.Max(x2, range[2]),
1462
                                Math.Max(y2, range[3])
1463
                            };
1464
                            }
1465
                        }
1466
                        else if (item.GetType() == typeof(Ingr.RAD2D.Circle2d))
1467
                        {
1468
                            Ingr.RAD2D.Circle2d rangeObject = item as Ingr.RAD2D.Circle2d;
1469
                            if (rangeObject.Layer == "Default")
1470
                            {
1471
                                rangeObject.Range(out x1, out y1, out x2, out y2);
1472
                                range = new double[] {
1473
                                Math.Min(x1, range[0]),
1474
                                Math.Min(y1, range[1]),
1475
                                Math.Max(x2, range[2]),
1476
                                Math.Max(y2, range[3])
1477
                            };
1478
                            }
1479
                        }
1480
                        else if (item.GetType() == typeof(Ingr.RAD2D.Rectangle2d))
1481
                        {
1482
                            Ingr.RAD2D.Rectangle2d rangeObject = item as Ingr.RAD2D.Rectangle2d;
1483
                            if (rangeObject.Layer == "Default")
1484
                            {
1485
                                rangeObject.Range(out x1, out y1, out x2, out y2);
1486
                                range = new double[] {
1487
                                Math.Min(x1, range[0]),
1488
                                Math.Min(y1, range[1]),
1489
                                Math.Max(x2, range[2]),
1490
                                Math.Max(y2, range[3])
1491
                            };
1492
                            }
1493
                        }
1494
                        else if (item.GetType() == typeof(Ingr.RAD2D.Arc2d))
1495
                        {
1496
                            Ingr.RAD2D.Arc2d rangeObject = item as Ingr.RAD2D.Arc2d;
1497
                            if (rangeObject.Layer == "Default")
1498
                            {
1499
                                rangeObject.Range(out x1, out y1, out x2, out y2);
1500
                                range = new double[] {
1501
                                Math.Min(x1, range[0]),
1502
                                Math.Min(y1, range[1]),
1503
                                Math.Max(x2, range[2]),
1504
                                Math.Max(y2, range[3])
1505
                            };
1506
                            }
1507
                        }
1508
                    }
1509
                }
1510

    
1511
                if (!bOnlySymbol)
1512
                {
1513
                    foreach (var childSymbol in symbol.ChildSymbols)
1514
                        GetSPPIDChildSymbolRange(childSymbol, ref range);
1515
                }
1516
                ReleaseCOMObjects(_TargetItem);
1517
            }
1518
        }
1519

    
1520
        private void GetSPPIDSymbolRange(LMLabelPersist labelPersist, ref double[] range)
1521
        {
1522
            if (labelPersist != null)
1523
            {
1524
                double x1 = double.MaxValue;
1525
                double y1 = double.MaxValue;
1526
                double x2 = double.MinValue;
1527
                double y2 = double.MinValue;
1528
                range = new double[] { x1, y1, x2, y2 };
1529

    
1530
                Ingr.RAD2D.DependencyObject dependency = radApp.ActiveDocument.ActiveSheet.DrawingObjects[labelPersist.get_GraphicOID().ToString()] as DependencyObject;
1531
                foreach (var item in dependency.DrawingObjects)
1532
                {
1533
                    Ingr.RAD2D.TextBox textBox = item as Ingr.RAD2D.TextBox;
1534
                    if (textBox != null)
1535
                    {
1536
                        if (dependency != null)
1537
                        {
1538
                            double tempX1;
1539
                            double tempY1;
1540
                            double tempX2;
1541
                            double tempY2;
1542
                            textBox.Range(out tempX1, out tempY1, out tempX2, out tempY2);
1543
                            x1 = Math.Min(x1, tempX1);
1544
                            y1 = Math.Min(y1, tempY1);
1545
                            x2 = Math.Max(x2, tempX2);
1546
                            y2 = Math.Max(y2, tempY2);
1547

    
1548
                            range = new double[] { x1, y1, x2, y2 };
1549
                        }
1550
                    }
1551
                }
1552
                
1553
            }
1554
        }
1555

    
1556
        private void GetSPPIDSymbolRange(List<Symbol> symbols, ref double[] range)
1557
        {
1558
            double[] tempRange = new double[] { double.MaxValue, double.MaxValue, double.MinValue, double.MinValue };
1559
            foreach (var symbol in symbols)
1560
            {
1561
                LMSymbol _TargetItem = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
1562
                Ingr.RAD2D.Symbol2d symbol2d = radApp.ActiveDocument.ActiveSheet.DrawingObjects[_TargetItem.get_GraphicOID().ToString()];
1563
                double x1 = 0;
1564
                double y1 = 0;
1565
                double x2 = 0;
1566
                double y2 = 0;
1567
                symbol2d.Range(out x1, out y1, out x2, out y2);
1568

    
1569
                tempRange[0] = Math.Min(tempRange[0], x1);
1570
                tempRange[1] = Math.Min(tempRange[1], y1);
1571
                tempRange[2] = Math.Max(tempRange[2], x2);
1572
                tempRange[3] = Math.Max(tempRange[3], y2);
1573

    
1574
                foreach (var childSymbol in symbol.ChildSymbols)
1575
                    GetSPPIDChildSymbolRange(childSymbol, ref tempRange);
1576

    
1577
                ReleaseCOMObjects(_TargetItem);
1578
            }
1579

    
1580
            range = tempRange;
1581
        }
1582

    
1583
        /// <summary>
1584
        /// Child Modeling 된 Symbol의 Range를 구한다.
1585
        /// </summary>
1586
        /// <param name="childSymbol"></param>
1587
        /// <param name="range"></param>
1588
        private void GetSPPIDChildSymbolRange(ChildSymbol childSymbol, ref double[] range, List<double[]> points)
1589
        {
1590
            LMSymbol _ChildSymbol = dataSource.GetSymbol(childSymbol.SPPID.RepresentationId);
1591
            if (_ChildSymbol != null)
1592
            {
1593
                Ingr.RAD2D.Symbol2d symbol2d = radApp.ActiveDocument.ActiveSheet.DrawingObjects[_ChildSymbol.get_GraphicOID().ToString()];
1594
                double x1 = 0;
1595
                double y1 = 0;
1596
                double x2 = 0;
1597
                double y2 = 0;
1598
                symbol2d.Range(out x1, out y1, out x2, out y2);
1599
                range[0] = Math.Min(range[0], x1);
1600
                range[1] = Math.Min(range[1], y1);
1601
                range[2] = Math.Max(range[2], x2);
1602
                range[3] = Math.Max(range[3], y2);
1603

    
1604
                for (int i = 1; i < int.MaxValue; i++)
1605
                {
1606
                    double connX = 0;
1607
                    double connY = 0;
1608
                    if (_placement.PIDConnectPointLocation(_ChildSymbol, i, ref connX, ref connY))
1609
                        points.Add(new double[] { connX, connY });
1610
                    else
1611
                        break;
1612
                }
1613

    
1614
                foreach (var loopChildSymbol in childSymbol.ChildSymbols)
1615
                    GetSPPIDChildSymbolRange(loopChildSymbol, ref range, points);
1616

    
1617
                ReleaseCOMObjects(_ChildSymbol);
1618
            }
1619
        }
1620

    
1621
        private void GetSPPIDChildSymbolRange(ChildSymbol childSymbol, ref double[] range)
1622
        {
1623
            LMSymbol _ChildSymbol = dataSource.GetSymbol(childSymbol.SPPID.RepresentationId);
1624
            if (_ChildSymbol != null)
1625
            {
1626
                Ingr.RAD2D.Symbol2d symbol2d = radApp.ActiveDocument.ActiveSheet.DrawingObjects[_ChildSymbol.get_GraphicOID().ToString()];
1627
                double x1 = 0;
1628
                double y1 = 0;
1629
                double x2 = 0;
1630
                double y2 = 0;
1631
                symbol2d.Range(out x1, out y1, out x2, out y2);
1632
                range[0] = Math.Min(range[0], x1);
1633
                range[1] = Math.Min(range[1], y1);
1634
                range[2] = Math.Max(range[2], x2);
1635
                range[3] = Math.Max(range[3], y2);
1636

    
1637
                foreach (var loopChildSymbol in childSymbol.ChildSymbols)
1638
                    GetSPPIDChildSymbolRange(loopChildSymbol, ref range);
1639
                ReleaseCOMObjects(_ChildSymbol);
1640
            }
1641
        }
1642

    
1643
        /// <summary>
1644
        /// Label Symbol Modeling
1645
        /// </summary>
1646
        /// <param name="symbol"></param>
1647
        private void LabelSymbolModeling(Symbol symbol)
1648
        {
1649
            if (string.IsNullOrEmpty(symbol.SPPID.RepresentationId))
1650
            {
1651
                BaseModel.Attribute itemAttribute = symbol.ATTRIBUTES.Find(x => x.ATTRIBUTE == "OWNERSYMBOL");
1652
                if (itemAttribute == null || string.IsNullOrEmpty(itemAttribute.VALUE) || itemAttribute.VALUE == "None")
1653
                    return;
1654
                Array points = new double[] { 0, symbol.SPPID.ORIGINAL_X, symbol.SPPID.ORIGINAL_Y };
1655

    
1656
                string symbolUID = itemAttribute.VALUE;
1657
                object targetItem = SPPIDUtil.FindObjectByUID(document, symbolUID);
1658
                if (targetItem != null &&
1659
                    (targetItem.GetType() == typeof(Symbol) ||
1660
                    targetItem.GetType() == typeof(Equipment)))
1661
                {
1662
                    // Object 아이템이 Symbol일 경우 Equipment일 경우 
1663
                    string sRep = null;
1664
                    if (targetItem.GetType() == typeof(Symbol))
1665
                        sRep = ((Symbol)targetItem).SPPID.RepresentationId;
1666
                    else if (targetItem.GetType() == typeof(Equipment))
1667
                        sRep = ((Equipment)targetItem).SPPID.RepresentationId;
1668
                    if (!string.IsNullOrEmpty(sRep))
1669
                    {
1670
                        // LEADER Line 검사
1671
                        bool leaderLine = false;
1672
                        SymbolMapping symbolMapping = document.SymbolMappings.Find(x => x.UID == symbol.DBUID);
1673
                        if (symbolMapping != null)
1674
                            leaderLine = symbolMapping.LEADERLINE;
1675

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

    
1680
                        //Leader 선 센터로
1681
                        if (_LMLabelPresist != null)
1682
                        {
1683
                            // Target Item에 Label의 Attribute Input
1684
                            InputSymbolAttribute(targetItem, symbol.ATTRIBUTES);
1685

    
1686
                            string OID = _LMLabelPresist.get_GraphicOID().ToString();
1687
                            DependencyObject dependency = radApp.ActiveDocument.ActiveSheet.DrawingObjects[OID] as DependencyObject;
1688
                            if (dependency != null)
1689
                            {
1690
                                bool result = false;
1691
                                foreach (var attributes in dependency.AttributeSets)
1692
                                {
1693
                                    foreach (var attribute in attributes)
1694
                                    {
1695
                                        string name = attribute.Name;
1696
                                        string value = attribute.GetValue().ToString();
1697
                                        if (name == "DrawingItemType" && value == "LabelPersist")
1698
                                        {
1699
                                            foreach (DrawingObjectBase drawingObject in dependency.DrawingObjects)
1700
                                            {
1701
                                                if (drawingObject.Type == Ingr.RAD2D.ObjectType.igLineString2d)
1702
                                                {
1703
                                                    Ingr.RAD2D.LineString2d lineString2D = drawingObject as Ingr.RAD2D.LineString2d;
1704
                                                    double prevX = _TargetItem.get_XCoordinate();
1705
                                                    double prevY = _TargetItem.get_YCoordinate();
1706
                                                    lineString2D.InsertVertex(lineString2D.VertexCount, prevX, prevY);
1707
                                                    lineString2D.RemoveVertex(lineString2D.VertexCount);
1708
                                                    result = true;
1709
                                                    break;
1710
                                                }
1711
                                            }
1712
                                        }
1713

    
1714
                                        if (result)
1715
                                            break;
1716
                                    }
1717

    
1718
                                    if (result)
1719
                                        break;
1720
                                }
1721
                            }
1722

    
1723
                            symbol.SPPID.RepresentationId = _LMLabelPresist.AsLMRepresentation().Id;
1724
                            _LMLabelPresist.Commit();
1725
                            ReleaseCOMObjects(_LMLabelPresist);
1726
                        }
1727

    
1728
                        ReleaseCOMObjects(_TargetItem);
1729
                    }
1730
                }
1731
                else if (targetItem != null && targetItem.GetType() == typeof(Line))
1732
                {
1733
                    Line targetLine = targetItem as Line;
1734
                    Dictionary<LMConnector, List<double[]>> connectorVertices = GetPipeRunVertices(targetLine.SPPID.ModelItemId);
1735
                    LMConnector connectedLMConnector = FindTargetLMConnectorForLabel(connectorVertices, symbol.SPPID.ORIGINAL_X, symbol.SPPID.ORIGINAL_Y);
1736
                    if (connectedLMConnector != null)
1737
                    {
1738
                        // LEADER Line 검사
1739
                        bool leaderLine = false;
1740
                        SymbolMapping symbolMapping = document.SymbolMappings.Find(x => x.UID == symbol.DBUID);
1741
                        if (symbolMapping != null)
1742
                            leaderLine = symbolMapping.LEADERLINE;
1743

    
1744
                        LMLabelPersist _LMLabelPresist = _placement.PIDPlaceLabel(symbol.SPPID.MAPPINGNAME, ref points, Rotation: 0, LabeledItem: connectedLMConnector.AsLMRepresentation(), IsLeaderVisible: leaderLine);
1745
                        if (_LMLabelPresist != null)
1746
                        {
1747
                            symbol.SPPID.RepresentationId = _LMLabelPresist.AsLMRepresentation().Id;
1748
                            _LMLabelPresist.Commit();
1749
                            ReleaseCOMObjects(_LMLabelPresist);
1750
                        }
1751
                        ReleaseCOMObjects(connectedLMConnector);
1752
                    }
1753

    
1754
                    foreach (var item in connectorVertices)
1755
                        if (item.Key != null)
1756
                            ReleaseCOMObjects(item.Key);
1757
                }
1758
            }
1759
        }
1760

    
1761
        /// <summary>
1762
        /// Equipment를 실제로 Modeling 메서드
1763
        /// </summary>
1764
        /// <param name="equipment"></param>
1765
        private void EquipmentModeling(Equipment equipment)
1766
        {
1767
            if (!string.IsNullOrEmpty(equipment.SPPID.RepresentationId))
1768
                return;
1769

    
1770
            LMSymbol _LMSymbol = null;
1771
            LMSymbol targetItem = null;
1772
            string mappingPath = equipment.SPPID.MAPPINGNAME;
1773
            double x = equipment.SPPID.ORIGINAL_X;
1774
            double y = equipment.SPPID.ORIGINAL_Y;
1775
            int mirror = 0;
1776
            double angle = equipment.ANGLE;
1777

    
1778
            SPPIDUtil.ConvertGridPoint(ref x, ref y);
1779

    
1780
            Connector connector = equipment.CONNECTORS.Find(conn => !string.IsNullOrEmpty(conn.CONNECTEDITEM) && conn.CONNECTEDITEM != "None");
1781
            if (connector != null)
1782
            {
1783
                Equipment connEquipment = SPPIDUtil.FindObjectByUID(document, connector.CONNECTEDITEM) as Equipment;
1784
                VendorPackage connVendorPackage = SPPIDUtil.FindObjectByUID(document, connector.CONNECTEDITEM) as VendorPackage;
1785
                if (connEquipment != null)
1786
                {
1787
                    if (string.IsNullOrEmpty(connEquipment.SPPID.RepresentationId))
1788
                        EquipmentModeling(connEquipment);
1789

    
1790
                    if (!string.IsNullOrEmpty(connEquipment.SPPID.RepresentationId))
1791
                    {
1792
                        targetItem = dataSource.GetSymbol(connEquipment.SPPID.RepresentationId);
1793
                        if (targetItem != null)
1794
                            _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle, TargetItem: targetItem);
1795
                        else
1796
                            _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle);
1797
                    }
1798
                    else
1799
                    {
1800
                        _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle);
1801
                    }
1802
                }
1803
                else if (connVendorPackage != null)
1804
                {
1805
                    if (!string.IsNullOrEmpty(connVendorPackage.SPPID.RepresentationId))
1806
                    {
1807
                        targetItem = dataSource.GetSymbol(connVendorPackage.SPPID.RepresentationId);
1808
                        if (targetItem != null)
1809
                            _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle, TargetItem: targetItem);
1810
                        else
1811
                            _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle);
1812
                    }
1813
                }
1814
                else
1815
                {
1816
                    _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle);
1817
                }
1818
            }
1819
            else
1820
            {
1821
                _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle);
1822
            }
1823

    
1824
            if (_LMSymbol != null)
1825
            {
1826
                _LMSymbol.Commit();
1827
                equipment.SPPID.RepresentationId = _LMSymbol.AsLMRepresentation().Id;
1828
                equipment.SPPID.GraphicOID = _LMSymbol.get_GraphicOID().ToString();
1829
                ReleaseCOMObjects(_LMSymbol);
1830
            }
1831

    
1832
            if (targetItem != null)
1833
            {
1834
                ReleaseCOMObjects(targetItem);
1835
            }
1836

    
1837
            ReleaseCOMObjects(_LMSymbol);
1838
        }
1839

    
1840
        private void VendorPackageModeling(VendorPackage vendorPackage)
1841
        {
1842
            ETCSetting setting = ETCSetting.GetInstance();
1843
            if (!string.IsNullOrEmpty(setting.VendorPackageSymbolPath))
1844
            {
1845
                string symbolPath = setting.VendorPackageSymbolPath;
1846
                double x = vendorPackage.SPPID.ORIGINAL_X;
1847
                double y = vendorPackage.SPPID.ORIGINAL_Y;
1848

    
1849
                LMSymbol symbol = _placement.PIDPlaceSymbol(symbolPath, x, y);
1850
                if (symbol != null)
1851
                {
1852
                    symbol.Commit();
1853
                    vendorPackage.SPPID.RepresentationId = symbol.AsLMRepresentation().Id;
1854
                }
1855

    
1856
                ReleaseCOMObjects(symbol);
1857
                symbol = null;
1858
            }
1859
        }
1860

    
1861
        /// <summary>
1862
        /// 첫 진입점
1863
        /// </summary>
1864
        /// <param name="symbol"></param>
1865
        private void SymbolModelingBySymbol(Symbol symbol)
1866
        {
1867
            SymbolModeling(symbol, null);   /// 심볼을 생성한다
1868
            List<object> endObjects = new List<object>();
1869
            endObjects.Add(symbol);
1870

    
1871
            /// 심볼에 연결되어 있는 항목들을 모델링한다
1872
            foreach (var connector in symbol.CONNECTORS)
1873
            {
1874
                object connItem = SPPIDUtil.FindObjectByUID(document, connector.CONNECTEDITEM);
1875
                if (connItem != null && connItem.GetType() != typeof(Equipment))
1876
                {
1877
                    endObjects.Add(connItem);
1878
                    if (connItem.GetType() == typeof(Symbol))
1879
                    {
1880
                        Symbol connSymbol = connItem as Symbol;
1881
                        if (string.IsNullOrEmpty(connSymbol.SPPID.RepresentationId))
1882
                        {
1883
                            SymbolModeling(connSymbol, symbol);
1884
                        }
1885
                        SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetProgress, document.SYMBOLS.FindAll(x => !string.IsNullOrEmpty(x.SPPID.RepresentationId)).Count);
1886
                        SymbolModelingByNeerSymbolLoop(connSymbol, endObjects);
1887
                    }
1888
                    else if (connItem.GetType() == typeof(Line))
1889
                    {
1890
                        Line connLine = connItem as Line;
1891
                        SymbolModelingByNeerLineLoop(connLine, endObjects, symbol);
1892
                    }
1893
                }
1894
            }
1895
        }
1896

    
1897
        private void SymbolModelingByNeerSymbolLoop(Symbol symbol, List<object> endObjects)
1898
        {
1899
            foreach (var connector in symbol.CONNECTORS)
1900
            {
1901
                object connItem = SPPIDUtil.FindObjectByUID(document, connector.CONNECTEDITEM);
1902
                if (connItem != null && connItem.GetType() != typeof(Equipment))
1903
                {
1904
                    if (!endObjects.Contains(connItem))
1905
                    {
1906
                        endObjects.Add(connItem);
1907
                        if (connItem.GetType() == typeof(Symbol))
1908
                        {
1909
                            Symbol connSymbol = connItem as Symbol;
1910
                            if (string.IsNullOrEmpty(connSymbol.SPPID.RepresentationId))
1911
                            {
1912
                                SymbolModeling(connSymbol, symbol);
1913
                            }
1914
                            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetProgress, document.SYMBOLS.FindAll(x => !string.IsNullOrEmpty(x.SPPID.RepresentationId)).Count);
1915
                            SymbolModelingByNeerSymbolLoop(connSymbol, endObjects);
1916
                        }
1917
                        else if (connItem.GetType() == typeof(Line))
1918
                        {
1919
                            Line connLine = connItem as Line;
1920
                            SymbolModelingByNeerLineLoop(connLine, endObjects, symbol);
1921
                        }
1922
                    }
1923
                }
1924
            }
1925
        }
1926

    
1927
        private void SymbolModelingByNeerLineLoop(Line line, List<object> endObjects, Symbol prevSymbol)
1928
        {
1929
            foreach (var connector in line.CONNECTORS)
1930
            {
1931
                object connItem = SPPIDUtil.FindObjectByUID(document, connector.CONNECTEDITEM);
1932
                if (connItem != null && connItem.GetType() != typeof(Equipment))
1933
                {
1934
                    if (!endObjects.Contains(connItem))
1935
                    {
1936
                        endObjects.Add(connItem);
1937
                        if (connItem.GetType() == typeof(Symbol))
1938
                        {
1939
                            Symbol connSymbol = connItem as Symbol;
1940
                            if (string.IsNullOrEmpty(connSymbol.SPPID.RepresentationId))
1941
                            {
1942
                                Line connLine = SPPIDUtil.GetConnectedLine(prevSymbol, connSymbol);
1943
                                int branchCount = 0;
1944
                                if (connLine != null)
1945
                                    branchCount = SPPIDUtil.GetBranchLineCount(document, connLine);
1946

    
1947
                                List<Symbol> group = new List<Symbol>();
1948
                                SPPIDUtil.FindConnectedSymbolGroup(document, connSymbol, group);
1949
                                Symbol priority = prioritySymbols.Find(x => group.Contains(x));
1950
                                List<Symbol> endModelingGroup = new List<Symbol>();
1951
                                if (priority != null)
1952
                                {
1953
                                    SymbolGroupModeling(priority, group);
1954

    
1955
                                    // Range 겹치는지 확인해야함
1956
                                    double[] prevRange = null;
1957
                                    GetSPPIDSymbolRange(prevSymbol, ref prevRange);
1958
                                    double[] groupRange = null;
1959
                                    GetSPPIDSymbolRange(group, ref groupRange);
1960

    
1961
                                    double distanceX = 0;
1962
                                    double distanceY = 0;
1963
                                    bool overlapX = false;
1964
                                    bool overlapY = false;
1965
                                    SlopeType slopeType = SPPIDUtil.CalcSlope(prevSymbol.SPPID.ORIGINAL_X, prevSymbol.SPPID.ORIGINAL_Y, connSymbol.SPPID.ORIGINAL_X, connSymbol.SPPID.ORIGINAL_Y);
1966
                                    SPPIDUtil.CalcOverlap(prevRange, groupRange, ref distanceX, ref distanceY, ref overlapX, ref overlapY);
1967
                                    if ((slopeType == SlopeType.HORIZONTAL && overlapX) ||
1968
                                        (slopeType == SlopeType.VERTICAL && overlapY))
1969
                                    {
1970
                                        RemoveSymbol(group);
1971
                                        foreach (var _temp in group)
1972
                                            SPPIDUtil.CalcNewCoordinateForSymbol(_temp, prevSymbol, distanceX, distanceY);
1973

    
1974
                                        SymbolGroupModeling(priority, group);
1975
                                    }
1976
                                    else if (branchCount > 0)
1977
                                    {
1978
                                        LMConnector _connector = JustLineModeling(connLine);
1979
                                        if (_connector != null)
1980
                                        {
1981
                                            double distance = GetConnectorDistance(_connector);
1982
                                            int cellCount = (int)Math.Truncate(distance / GridSetting.GetInstance().Length);
1983
                                            _placement.PIDRemovePlacement(_connector.AsLMRepresentation());
1984
                                            _connector.Commit();
1985
                                            ReleaseCOMObjects(_connector);
1986
                                            _connector = null;
1987
                                            if (cellCount < branchCount + 1)
1988
                                            {
1989
                                                int moveCount = branchCount - cellCount;
1990
                                                RemoveSymbol(group);
1991
                                                foreach (var _temp in group)
1992
                                                    SPPIDUtil.CalcNewCoordinateForSymbol(_temp, prevSymbol, moveCount * GridSetting.GetInstance().Length, moveCount * GridSetting.GetInstance().Length);
1993

    
1994
                                                SymbolGroupModeling(priority, group);
1995
                                            }
1996
                                        }
1997
                                    }
1998
                                }
1999
                                else
2000
                                {
2001
                                    SymbolModeling(connSymbol, null);
2002
                                    // Range 겹치는지 확인해야함
2003
                                    double[] prevRange = null;
2004
                                    GetSPPIDSymbolRange(prevSymbol, ref prevRange);
2005
                                    double[] connRange = null;
2006
                                    GetSPPIDSymbolRange(connSymbol, ref connRange);
2007

    
2008
                                    double distanceX = 0;
2009
                                    double distanceY = 0;
2010
                                    bool overlapX = false;
2011
                                    bool overlapY = false;
2012
                                    SlopeType slopeType = SPPIDUtil.CalcSlope(prevSymbol.SPPID.ORIGINAL_X, prevSymbol.SPPID.ORIGINAL_Y, connSymbol.SPPID.ORIGINAL_X, connSymbol.SPPID.ORIGINAL_Y);
2013
                                    SPPIDUtil.CalcOverlap(prevRange, connRange, ref distanceX, ref distanceY, ref overlapX, ref overlapY);
2014
                                    if ((slopeType == SlopeType.HORIZONTAL && overlapX) ||
2015
                                        (slopeType == SlopeType.VERTICAL && overlapY))
2016
                                    {
2017
                                        RemoveSymbol(connSymbol);
2018
                                        SPPIDUtil.CalcNewCoordinateForSymbol(connSymbol, prevSymbol, distanceX, distanceY);
2019

    
2020
                                        SymbolModeling(connSymbol, null);
2021
                                    }
2022
                                    else if (branchCount > 0)
2023
                                    {
2024
                                        LMConnector _connector = JustLineModeling(connLine);
2025
                                        if (_connector != null)
2026
                                        {
2027
                                            double distance = GetConnectorDistance(_connector);
2028
                                            int cellCount = (int)Math.Truncate(distance / GridSetting.GetInstance().Length);
2029
                                            _placement.PIDRemovePlacement(_connector.AsLMRepresentation());
2030
                                            _connector.Commit();
2031
                                            ReleaseCOMObjects(_connector);
2032
                                            _connector = null;
2033
                                            if (cellCount < branchCount + 1)
2034
                                            {
2035
                                                int moveCount = branchCount - cellCount;
2036
                                                RemoveSymbol(group);
2037
                                                foreach (var _temp in group)
2038
                                                    SPPIDUtil.CalcNewCoordinateForSymbol(_temp, prevSymbol, moveCount * GridSetting.GetInstance().Length, moveCount * GridSetting.GetInstance().Length);
2039

    
2040
                                                SymbolGroupModeling(priority, group);
2041
                                            }
2042
                                        }
2043
                                    }
2044
                                }
2045
                            }
2046
                            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetProgress, document.SYMBOLS.FindAll(x => !string.IsNullOrEmpty(x.SPPID.RepresentationId)).Count);
2047
                            SymbolModelingByNeerSymbolLoop(connSymbol, endObjects);
2048
                        }
2049
                        else if (connItem.GetType() == typeof(Line))
2050
                        {
2051
                            Line connLine = connItem as Line;
2052
                            if (!SPPIDUtil.IsBranchLine(connLine, line))
2053
                                SymbolModelingByNeerLineLoop(connLine, endObjects, prevSymbol);
2054
                        }
2055
                    }
2056
                }
2057
            }
2058
        }
2059

    
2060
        private void SymbolGroupModeling(Symbol firstSymbol, List<Symbol> group)
2061
        {
2062
            List<Symbol> endModelingGroup = new List<Symbol>();
2063
            SymbolModeling(firstSymbol, null);
2064
            endModelingGroup.Add(firstSymbol);
2065
            while (endModelingGroup.Count != group.Count)
2066
            {
2067
                foreach (var _symbol in group)
2068
                {
2069
                    if (!endModelingGroup.Contains(_symbol))
2070
                    {
2071
                        foreach (var _connector in _symbol.CONNECTORS)
2072
                        {
2073
                            Symbol _connSymbol = SPPIDUtil.FindObjectByUID(document, _connector.CONNECTEDITEM) as Symbol;
2074
                            if (_connSymbol != null && endModelingGroup.Contains(_connSymbol))
2075
                            {
2076
                                SymbolModeling(_symbol, _connSymbol);
2077
                                endModelingGroup.Add(_symbol);
2078
                                break;
2079
                            }
2080
                        }
2081
                    }
2082
                }
2083
            }
2084
        }
2085

    
2086
        /// <summary>
2087
        /// 심볼을 실제로 Modeling할때 ChildSymbol이 있다면 Modeling하는 메서드
2088
        /// </summary>
2089
        /// <param name="childSymbol"></param>
2090
        /// <param name="parentSymbol"></param>
2091
        private void CreateChildSymbol(ChildSymbol childSymbol, LMSymbol parentSymbol, Symbol parent)
2092
        {
2093
            Ingr.RAD2D.Symbol2d symbol2d = radApp.ActiveDocument.ActiveSheet.DrawingObjects[parentSymbol.get_GraphicOID().ToString()];
2094
            double x1 = 0;
2095
            double x2 = 0;
2096
            double y1 = 0;
2097
            double y2 = 0;
2098
            symbol2d.Range(out x1, out y1, out x2, out y2);
2099

    
2100
            LMSymbol _LMSymbol = _placement.PIDPlaceSymbol(childSymbol.SPPID.MAPPINGNAME, (x1 + x2) / 2, (y1 + y2) / 2, TargetItem: parentSymbol);
2101
            if (_LMSymbol != null)
2102
            {
2103
                _LMSymbol.Commit();
2104
                childSymbol.SPPID.RepresentationId = _LMSymbol.AsLMRepresentation().Id;
2105
                foreach (var item in childSymbol.ChildSymbols)
2106
                    CreateChildSymbol(item, _LMSymbol, parent);
2107
            }
2108

    
2109

    
2110
            ReleaseCOMObjects(_LMSymbol);
2111
        }
2112
        double index = 0;
2113
        private void NewLineModeling(Line line, bool isBranchModeling = false)
2114
        {
2115
            if (!string.IsNullOrEmpty(line.SPPID.ModelItemId) || (BranchLines.Contains(line) && !isBranchModeling))
2116
                return;
2117

    
2118
            List<Line> group = new List<Line>();
2119
            GetConnectedLineGroup(line, group);
2120
            LineCoordinateCorrection(group);
2121

    
2122
            foreach (var groupLine in group)
2123
            {
2124
                if (!isBranchModeling && SPPIDUtil.IsBranchLine(groupLine))
2125
                {
2126
                    BranchLines.Add(groupLine);
2127
                    continue;
2128
                }
2129

    
2130
                bool diagonal = false;
2131
                if (groupLine.SlopeType != SlopeType.HORIZONTAL && groupLine.SlopeType != SlopeType.VERTICAL)
2132
                    diagonal = true;
2133
                _LMAItem _LMAItem = _placement.PIDCreateItem(groupLine.SPPID.MAPPINGNAME);
2134
                LMSymbol _LMSymbolStart = null;
2135
                LMSymbol _LMSymbolEnd = null;
2136
                PlaceRunInputs placeRunInputs = new PlaceRunInputs();
2137
                foreach (var connector in groupLine.CONNECTORS)
2138
                {
2139
                    double x = 0;
2140
                    double y = 0;
2141
                    GetTargetLineConnectorPoint(connector, groupLine, ref x, ref y);
2142
                    if (connector.ConnectedObject == null)
2143
                    {
2144
                        placeRunInputs.AddPoint(x, y);
2145
                    }
2146
                    else if (connector.ConnectedObject.GetType() == typeof(Symbol))
2147
                    {
2148
                        Symbol targetSymbol = connector.ConnectedObject as Symbol;
2149
                        GetTargetSymbolConnectorPoint(targetSymbol.CONNECTORS.Find(z => z.ConnectedObject == groupLine), targetSymbol, ref x, ref y);
2150
                        if (groupLine.CONNECTORS.IndexOf(connector) == 0)
2151
                        {
2152
                            _LMSymbolStart = GetTargetSymbol(targetSymbol, groupLine);
2153
                            if (_LMSymbolStart != null)
2154
                                placeRunInputs.AddSymbolTarget(_LMSymbolStart, x, y, diagonal);
2155
                            else
2156
                                placeRunInputs.AddPoint(x, y);
2157
                        }
2158
                        else
2159
                        {
2160
                            _LMSymbolEnd = GetTargetSymbol(targetSymbol, groupLine);
2161
                            if (_LMSymbolEnd != null)
2162
                                placeRunInputs.AddSymbolTarget(_LMSymbolEnd, x, y, diagonal);
2163
                            else
2164
                                placeRunInputs.AddPoint(x, y);
2165
                        }
2166
                    }
2167
                    else if (connector.ConnectedObject.GetType() == typeof(Line))
2168
                    {
2169
                        Line targetLine = connector.ConnectedObject as Line;
2170
                        if (!string.IsNullOrEmpty(targetLine.SPPID.ModelItemId))
2171
                        {
2172
                            LMConnector targetConnector = FindTargetLMConnectorForBranch(line, targetLine, ref x, ref y);
2173
                            if (targetConnector != null)
2174
                            {
2175
                                placeRunInputs.AddConnectorTarget(targetConnector, x, y, diagonal);
2176
                                ChangeLineSPPIDCoordinateByConnector(groupLine, targetLine, x, y, false);
2177
                            }
2178
                            else
2179
                            {
2180
                                placeRunInputs.AddPoint( x, y);
2181
                                ChangeLineSPPIDCoordinateByConnector(groupLine, targetLine, x, y, false);
2182
                            }
2183
                        }
2184
                        else
2185
                        {
2186
                            if (groupLine.CONNECTORS.IndexOf(connector) == 0)
2187
                            {
2188
                                index += 0.01;
2189
                                if (groupLine.SlopeType == SlopeType.HORIZONTAL)
2190
                                    placeRunInputs.AddPoint(x, -0.1 - index);
2191
                                else if (groupLine.SlopeType == SlopeType.VERTICAL)
2192
                                    placeRunInputs.AddPoint(-0.1 - index, y);
2193
                                else
2194
                                {
2195
                                    Line nextLine = groupLine.CONNECTORS[0].ConnectedObject as Line;
2196
                                    if (SPPIDUtil.CalcAngle(nextLine.SPPID.START_X, nextLine.SPPID.START_Y, nextLine.SPPID.END_X, nextLine.SPPID.END_Y) < 45)
2197
                                        placeRunInputs.AddPoint(-0.1 - index, y);
2198
                                    else
2199
                                        placeRunInputs.AddPoint(x, -0.1 - index);
2200
                                }
2201
                            }
2202

    
2203
                            placeRunInputs.AddPoint(x, y);
2204

    
2205
                            if (groupLine.CONNECTORS.IndexOf(connector) == 1)
2206
                            {
2207
                                index += 0.01;
2208
                                if (groupLine.SlopeType == SlopeType.HORIZONTAL)
2209
                                    placeRunInputs.AddPoint(x, -0.1 - index);
2210
                                else if (groupLine.SlopeType == SlopeType.VERTICAL)
2211
                                    placeRunInputs.AddPoint(-0.1 - index, y);
2212
                                else
2213
                                {
2214
                                    Line nextLine = groupLine.CONNECTORS[1].ConnectedObject as Line;
2215
                                    if (SPPIDUtil.CalcAngle(nextLine.SPPID.START_X, nextLine.SPPID.START_Y, nextLine.SPPID.END_X, nextLine.SPPID.END_Y) < 45)
2216
                                        placeRunInputs.AddPoint(-0.1 - index, y);
2217
                                    else
2218
                                        placeRunInputs.AddPoint(x, -0.1 - index);
2219
                                }
2220
                            }
2221
                        }
2222
                    }
2223
                }
2224

    
2225
                LMConnector _lMConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
2226
                if (_lMConnector != null)
2227
                {
2228
                    _lMConnector.Commit();
2229
                    groupLine.SPPID.ModelItemId = _lMConnector.ModelItemID;
2230

    
2231
                    bool bRemodelingStart = false;
2232
                    if (_LMSymbolStart != null)
2233
                        NeedReModeling(groupLine, _LMSymbolStart, ref bRemodelingStart);
2234
                    bool bRemodelingEnd = false;
2235
                    if (_LMSymbolEnd != null)
2236
                        NeedReModeling(groupLine, _LMSymbolEnd, ref bRemodelingEnd);
2237

    
2238
                    if (bRemodelingStart || bRemodelingEnd)
2239
                        ReModelingLine(groupLine, _lMConnector, _LMSymbolStart, _LMSymbolEnd, bRemodelingStart, bRemodelingEnd);
2240

    
2241
                    FlowMarkModeling(groupLine);
2242
                    LineNumberModelingOnlyOne(groupLine);
2243

    
2244
                    ReleaseCOMObjects(_lMConnector);
2245

    
2246
                    LMModelItem modelItem = dataSource.GetModelItem(groupLine.SPPID.ModelItemId);
2247
                    if (modelItem != null)
2248
                    {
2249
                        LMAAttribute attribute = modelItem.Attributes["FlowDirection"];
2250
                        if (attribute != null)
2251
                            attribute.set_Value("End 1 is upstream (Inlet)");
2252
                        modelItem.Commit();
2253
                    }
2254
                    ReleaseCOMObjects(modelItem);
2255
                    modelItem = null;
2256
                }
2257
                else if (!isBranchModeling)
2258
                {
2259
                    Log.Write("Main Line Modeling : " + groupLine.UID);
2260
                }
2261

    
2262
                List<object> removeLines = groupLine.CONNECTORS.FindAll(x =>
2263
                x.ConnectedObject != null &&
2264
                x.ConnectedObject.GetType() == typeof(Line) &&
2265
                !string.IsNullOrEmpty(((Line)x.ConnectedObject).SPPID.ModelItemId))
2266
                .Select(x => x.ConnectedObject)
2267
                .ToList();
2268

    
2269
                foreach (var item in removeLines)
2270
                    RemoveLineForModeling(item as Line);
2271

    
2272
                ReleaseCOMObjects(_LMAItem);
2273
                _LMAItem = null;
2274
                ReleaseCOMObjects(placeRunInputs);
2275
                placeRunInputs = null;
2276
                ReleaseCOMObjects(_LMSymbolStart);
2277
                _LMSymbolStart = null;
2278
                ReleaseCOMObjects(_LMSymbolEnd);
2279
                _LMSymbolEnd = null;
2280

    
2281
                if (isBranchModeling && BranchLines.Contains(groupLine))
2282
                    BranchLines.Remove(groupLine);
2283
            }
2284
        }
2285

    
2286
        private LMConnector JustLineModeling(Line line)
2287
        {
2288
            bool diagonal = false;
2289
            if (line.SlopeType != SlopeType.HORIZONTAL && line.SlopeType != SlopeType.VERTICAL)
2290
                diagonal = true;
2291
            _LMAItem _LMAItem = _placement.PIDCreateItem(line.SPPID.MAPPINGNAME);
2292
            LMSymbol _LMSymbolStart = null;
2293
            LMSymbol _LMSymbolEnd = null;
2294
            PlaceRunInputs placeRunInputs = new PlaceRunInputs();
2295
            foreach (var connector in line.CONNECTORS)
2296
            {
2297
                double x = 0;
2298
                double y = 0;
2299
                GetTargetLineConnectorPoint(connector, line, ref x, ref y);
2300
                if (connector.ConnectedObject == null)
2301
                {
2302
                    placeRunInputs.AddPoint(x, y);
2303
                }
2304
                else if (connector.ConnectedObject.GetType() == typeof(Symbol))
2305
                {
2306
                    Symbol targetSymbol = connector.ConnectedObject as Symbol;
2307
                    GetTargetSymbolConnectorPoint(targetSymbol.CONNECTORS.Find(z => z.ConnectedObject == line), targetSymbol, ref x, ref y);
2308
                    if (line.CONNECTORS.IndexOf(connector) == 0)
2309
                    {
2310
                        _LMSymbolStart = GetTargetSymbol(targetSymbol, line);
2311
                        if (_LMSymbolStart != null)
2312
                            placeRunInputs.AddSymbolTarget(_LMSymbolStart, x, y, diagonal);
2313
                        else
2314
                            placeRunInputs.AddPoint(x, y);
2315
                    }
2316
                    else
2317
                    {
2318
                        _LMSymbolEnd = GetTargetSymbol(targetSymbol, line);
2319
                        if (_LMSymbolEnd != null)
2320
                            placeRunInputs.AddSymbolTarget(_LMSymbolEnd, x, y, diagonal);
2321
                        else
2322
                            placeRunInputs.AddPoint(x, y);
2323
                    }
2324
                }
2325
                else if (connector.ConnectedObject.GetType() == typeof(Line))
2326
                {
2327
                    Line targetLine = connector.ConnectedObject as Line;
2328
                    if (!string.IsNullOrEmpty(targetLine.SPPID.ModelItemId))
2329
                    {
2330
                        LMConnector targetConnector = FindTargetLMConnectorForBranch(line, targetLine, ref x, ref y);
2331
                        if (targetConnector != null)
2332
                        {
2333
                            placeRunInputs.AddConnectorTarget(targetConnector, x, y, diagonal);
2334
                            ChangeLineSPPIDCoordinateByConnector(line, targetLine, x, y, false);
2335
                        }
2336
                        else
2337
                        {
2338
                            placeRunInputs.AddPoint(x, y);
2339
                            ChangeLineSPPIDCoordinateByConnector(line, targetLine, x, y, false);
2340
                        }
2341
                    }
2342
                    else
2343
                        placeRunInputs.AddPoint(x, y);
2344
                }
2345
            }
2346

    
2347
            LMConnector _lMConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
2348
            if (_lMConnector != null)
2349
                _lMConnector.Commit();
2350

    
2351
            ReleaseCOMObjects(_LMAItem);
2352
            _LMAItem = null;
2353
            ReleaseCOMObjects(placeRunInputs);
2354
            placeRunInputs = null;
2355
            ReleaseCOMObjects(_LMSymbolStart);
2356
            _LMSymbolStart = null;
2357
            ReleaseCOMObjects(_LMSymbolEnd);
2358
            _LMSymbolEnd = null;
2359

    
2360
            return _lMConnector;
2361
        }
2362

    
2363
        private void RemoveLineForModeling(Line line)
2364
        {
2365
            LMModelItem modelItem = dataSource.GetModelItem(line.SPPID.ModelItemId);
2366
            if (modelItem != null)
2367
            {
2368
                foreach (LMRepresentation rep in modelItem.Representations)
2369
                {
2370
                    if (rep.Attributes["RepresentationType"].get_Value() == "Connector" && rep.Attributes["ItemStatus"].get_Value() == "Active")
2371
                    {
2372
                        LMConnector _LMConnector = dataSource.GetConnector(rep.Id);
2373
                        dynamic OID = rep.get_GraphicOID().ToString();
2374
                        DependencyObject drawingObject = radApp.ActiveDocument.ActiveSheet.DrawingObjects[OID];
2375
                        Ingr.RAD2D.LineStringGeometry2d lineStringGeometry = drawingObject.GetGeometry() as Ingr.RAD2D.LineStringGeometry2d;
2376
                        int verticesCount = lineStringGeometry.VertexCount;
2377
                        double[] vertices = null;
2378
                        lineStringGeometry.GetVertices(ref verticesCount, ref vertices);
2379
                        for (int i = 0; i < verticesCount; i++)
2380
                        {
2381
                            double x = 0;
2382
                            double y = 0;
2383
                            lineStringGeometry.GetVertex(i + 1, ref x, ref y);
2384
                            if (verticesCount == 2 && (x < 0 || y < 0))
2385
                                _placement.PIDRemovePlacement(rep);
2386
                        }
2387
                        ReleaseCOMObjects(_LMConnector);
2388
                    }
2389
                }
2390

    
2391
                ReleaseCOMObjects(modelItem);
2392
            }
2393
        }
2394

    
2395
        private void GetConnectedLineGroup(Line line, List<Line> group)
2396
        {
2397
            if (!group.Contains(line))
2398
                group.Add(line);
2399
            foreach (var connector in line.CONNECTORS)
2400
            {
2401
                if (connector.ConnectedObject != null &&
2402
                    connector.ConnectedObject.GetType() == typeof(Line) &&
2403
                    !group.Contains(connector.ConnectedObject) &&
2404
                    string.IsNullOrEmpty(((Line)connector.ConnectedObject).SPPID.ModelItemId))
2405
                {
2406
                    Line connLine = connector.ConnectedObject as Line;
2407
                    if (!SPPIDUtil.IsBranchLine(connLine, line))
2408
                    {
2409
                        if (line.CONNECTORS.IndexOf(connector) == 0)
2410
                            group.Insert(0, connLine);
2411
                        else
2412
                            group.Add(connLine);
2413
                        GetConnectedLineGroup(connLine, group);
2414
                    }
2415
                }
2416
            }
2417
        }
2418

    
2419
        private void LineCoordinateCorrection(List<Line> group)
2420
        {
2421
            // 순서대로 전 Item 기준 정렬
2422
            LineCoordinateCorrectionByStart(group);
2423

    
2424
            // 역으로 심볼이 있을 경우 좌표 보정
2425
            LineCoordinateCorrectionForLastLine(group);
2426
        }
2427

    
2428
        private void LineCoordinateCorrectionByStart(List<Line> group)
2429
        {
2430
            for (int i = 0; i < group.Count; i++)
2431
            {
2432
                Line line = group[i];
2433
                if (i == 0)
2434
                {
2435
                    Connector symbolConnector = line.CONNECTORS.Find(x => x.ConnectedObject != null && x.ConnectedObject.GetType() == typeof(Symbol));
2436
                    if (symbolConnector != null)
2437
                        LineCoordinateCorrectionByConnItem(line, symbolConnector.ConnectedObject);
2438
                }
2439
                else if (i != 0)
2440
                {
2441
                    LineCoordinateCorrectionByConnItem(line, group[i - 1]);
2442
                }
2443
            }
2444
        }
2445

    
2446
        private void LineCoordinateCorrectionForLastLine(List<Line> group)
2447
        {
2448
            Line checkLine = group[group.Count - 1];
2449
            Connector lastSymbolConnector = checkLine.CONNECTORS.Find(x => x.ConnectedObject != null && x.ConnectedObject.GetType() == typeof(Symbol));
2450
            if (lastSymbolConnector != null)
2451
            {
2452
                LineCoordinateCorrectionByConnItem(checkLine, lastSymbolConnector.ConnectedObject);
2453
                for (int i = group.Count - 2; i >= 0; i--)
2454
                {
2455
                    Line line = group[i + 1];
2456
                    Line prevLine = group[i];
2457

    
2458
                    // 같으면 보정
2459
                    if (line.SlopeType == prevLine.SlopeType)
2460
                        LineCoordinateCorrectionByConnItem(prevLine, line);
2461
                    else
2462
                    {
2463
                        if (line.SlopeType == SlopeType.HORIZONTAL)
2464
                        {
2465
                            double prevX = 0;
2466
                            double prevY = 0;
2467
                            GetTargetLineConnectorPoint(prevLine.CONNECTORS.Find(z => z.ConnectedObject == line), prevLine, ref prevX, ref prevY);
2468
                            ChangeLineSPPIDCoordinateByConnectorOnlyX(line, prevLine, prevX);
2469

    
2470
                            double x = 0;
2471
                            double y = 0;
2472
                            GetTargetLineConnectorPoint(line.CONNECTORS.Find(z => z.ConnectedObject == prevLine), line, ref x, ref y);
2473
                            ChangeLineSPPIDCoordinateByConnectorOnlyY(prevLine, line, y);
2474
                        }
2475
                        else if (line.SlopeType == SlopeType.VERTICAL)
2476
                        {
2477
                            double prevX = 0;
2478
                            double prevY = 0;
2479
                            GetTargetLineConnectorPoint(prevLine.CONNECTORS.Find(z => z.ConnectedObject == line), prevLine, ref prevX, ref prevY);
2480
                            ChangeLineSPPIDCoordinateByConnectorOnlyY(line, prevLine, prevY);
2481

    
2482
                            double x = 0;
2483
                            double y = 0;
2484
                            GetTargetLineConnectorPoint(line.CONNECTORS.Find(z => z.ConnectedObject == prevLine), line, ref x, ref y);
2485
                            ChangeLineSPPIDCoordinateByConnectorOnlyX(prevLine, line, x);
2486
                        }
2487
                        break;
2488
                    }
2489
                }
2490
            }
2491
        }
2492

    
2493
        private void LineCoordinateCorrectionByConnItem(Line line, object connItem)
2494
        {
2495
            double x = 0;
2496
            double y = 0;
2497
            if (connItem.GetType() == typeof(Symbol))
2498
            {
2499
                Symbol targetSymbol = connItem as Symbol;
2500
                Connector targetConnector = targetSymbol.CONNECTORS.Find(z => z.ConnectedObject == line);
2501
                if (targetConnector != null)
2502
                    GetTargetSymbolConnectorPoint(targetConnector, targetSymbol, ref x, ref y);
2503
                else
2504
                    throw new Exception("Target symbol UID : " + targetSymbol.UID + "\r\nLine UID : " + line.UID);
2505
            }
2506
            else if (connItem.GetType() == typeof(Line))
2507
            {
2508
                Line targetLine = connItem as Line;
2509
                GetTargetLineConnectorPoint(targetLine.CONNECTORS.Find(z => z.ConnectedObject == line), targetLine, ref x, ref y);
2510
            }
2511

    
2512
            ChangeLineSPPIDCoordinateByConnector(line, connItem, x, y);
2513
        }
2514

    
2515
        private void ChangeLineSPPIDCoordinateByConnector(Line line, object connItem, double x, double y, bool changeOtherCoordinate = true)
2516
        {
2517
            Connector connector = line.CONNECTORS.Find(z => z.ConnectedObject == connItem);
2518
            int index = line.CONNECTORS.IndexOf(connector);
2519
            if (index == 0)
2520
            {
2521
                line.SPPID.START_X = x;
2522
                line.SPPID.START_Y = y;
2523
                if (line.SlopeType == SlopeType.HORIZONTAL && changeOtherCoordinate)
2524
                    line.SPPID.END_Y = y;
2525
                else if (line.SlopeType == SlopeType.VERTICAL && changeOtherCoordinate)
2526
                    line.SPPID.END_X = x;
2527
            }
2528
            else
2529
            {
2530
                line.SPPID.END_X = x;
2531
                line.SPPID.END_Y = y;
2532
                if (line.SlopeType == SlopeType.HORIZONTAL && changeOtherCoordinate)
2533
                    line.SPPID.START_Y = y;
2534
                else if (line.SlopeType == SlopeType.VERTICAL && changeOtherCoordinate)
2535
                    line.SPPID.START_X = x;
2536
            }
2537
        }
2538

    
2539
        private void ChangeLineSPPIDCoordinateByConnectorOnlyX(Line line, object connItem, double x)
2540
        {
2541
            Connector connector = line.CONNECTORS.Find(z => z.ConnectedObject == connItem);
2542
            int index = line.CONNECTORS.IndexOf(connector);
2543
            if (index == 0)
2544
            {
2545
                line.SPPID.START_X = x;
2546
                if (line.SlopeType == SlopeType.VERTICAL)
2547
                    line.SPPID.END_X = x;
2548
            }
2549
            else
2550
            {
2551
                line.SPPID.END_X = x;
2552
                if (line.SlopeType == SlopeType.VERTICAL)
2553
                    line.SPPID.START_X = x;
2554
            }
2555
        }
2556

    
2557
        private void ChangeLineSPPIDCoordinateByConnectorOnlyY(Line line, object connItem, double y)
2558
        {
2559
            Connector connector = line.CONNECTORS.Find(z => z.ConnectedObject == connItem);
2560
            int index = line.CONNECTORS.IndexOf(connector);
2561
            if (index == 0)
2562
            {
2563
                line.SPPID.START_Y = y;
2564
                if (line.SlopeType == SlopeType.HORIZONTAL)
2565
                    line.SPPID.END_Y = y;
2566
            }
2567
            else
2568
            {
2569
                line.SPPID.END_Y = y;
2570
                if (line.SlopeType == SlopeType.HORIZONTAL)
2571
                    line.SPPID.START_Y = y;
2572
            }
2573
        }
2574

    
2575
        private void NeedReModeling(Line line, LMSymbol symbol, ref bool result)
2576
        {
2577
            if (symbol != null)
2578
            {
2579
                string repID = symbol.AsLMRepresentation().Id;
2580
                string symbolUID = SPPIDUtil.FindSymbolByRepresentationID(document, repID).UID;
2581
                string lineUID = line.UID;
2582

    
2583
                SpecBreak startSpecBreak = document.SpecBreaks.Find(x =>
2584
                (x.DownStreamUID == symbolUID || x.UpStreamUID == symbolUID) &&
2585
                (x.DownStreamUID == lineUID || x.UpStreamUID == lineUID));
2586

    
2587
                EndBreak startEndBreak = document.EndBreaks.Find(x =>
2588
                (x.OWNER == symbolUID || x.PROPERTIES.Find(y => y.ATTRIBUTE == "Connected Item").VALUE == symbolUID) &&
2589
                (x.OWNER == lineUID || x.PROPERTIES.Find(y => y.ATTRIBUTE == "Connected Item").VALUE == lineUID));
2590

    
2591
                if (startSpecBreak != null || startEndBreak != null)
2592
                    result = true;
2593
            }
2594
        }
2595

    
2596
        /// <summary>
2597
        /// Symbol에 붙을 경우 Line을 Remodeling 한다.
2598
        /// </summary>
2599
        /// <param name="lines"></param>
2600
        /// <param name="prevLMConnector"></param>
2601
        /// <param name="startSymbol"></param>
2602
        /// <param name="endSymbol"></param>
2603
        private void ReModelingLine(Line line, LMConnector prevLMConnector, LMSymbol startSymbol, LMSymbol endSymbol, bool bStart, bool bEnd)
2604
        {
2605
            string symbolPath = string.Empty;
2606
            #region get symbol path
2607
            LMModelItem modelItem = dataSource.GetModelItem(prevLMConnector.ModelItemID);
2608
            symbolPath = GetSPPIDFileName(modelItem);
2609
            ReleaseCOMObjects(modelItem);
2610
            #endregion
2611
            bool diagonal = false;
2612
            if (line.SlopeType != SlopeType.HORIZONTAL && line.SlopeType != SlopeType.VERTICAL)
2613
                diagonal = true;
2614
            _LMAItem _LMAItem = _placement.PIDCreateItem(symbolPath);
2615
            LMConnector newConnector = null;
2616
            dynamic OID = prevLMConnector.get_GraphicOID().ToString();
2617
            DependencyObject drawingObject = radApp.ActiveDocument.ActiveSheet.DrawingObjects[OID];
2618
            Ingr.RAD2D.LineStringGeometry2d lineStringGeometry = drawingObject.GetGeometry() as Ingr.RAD2D.LineStringGeometry2d;
2619
            int verticesCount = lineStringGeometry.VertexCount;
2620
            PlaceRunInputs placeRunInputs = new PlaceRunInputs();
2621

    
2622
            List<double[]> vertices = new List<double[]>();
2623
            for (int i = 1; i <= verticesCount; i++)
2624
            {
2625
                double x = 0;
2626
                double y = 0;
2627
                lineStringGeometry.GetVertex(i, ref x, ref y);
2628
                vertices.Add(new double[] { x, y });
2629
            }
2630

    
2631
            for (int i = 0; i < vertices.Count; i++)
2632
            {
2633
                double[] points = vertices[i];
2634
                // 시작 심볼이 있고 첫번째 좌표일 때
2635
                if (startSymbol != null && i == 0)
2636
                {
2637
                    if (bStart)
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
                        placeRunInputs.AddPoint(points[0], points[1]);
2648
                    }
2649
                    else
2650
                    {
2651
                        placeRunInputs.AddSymbolTarget(startSymbol, points[0], points[1], diagonal);
2652
                    }
2653
                }
2654
                // 마지막 심볼이 있고 마지막 좌표일 때
2655
                else if (endSymbol != null && i == vertices.Count - 1)
2656
                {
2657
                    if (bEnd)
2658
                    {
2659
                        placeRunInputs.AddPoint(points[0], points[1]);
2660

    
2661
                        SlopeType slopeType = SPPIDUtil.CalcSlope(points[0], points[1], vertices[i - 1][0], vertices[i - 1][1]);
2662
                        if (slopeType == SlopeType.HORIZONTAL)
2663
                            placeRunInputs.AddPoint(points[0], -0.1);
2664
                        else if (slopeType == SlopeType.VERTICAL)
2665
                            placeRunInputs.AddPoint(-0.1, points[1]);
2666
                        else
2667
                            placeRunInputs.AddPoint(points[0], -0.1);
2668
                    }
2669
                    else
2670
                    {
2671
                        placeRunInputs.AddSymbolTarget(endSymbol, points[0], points[1], diagonal);
2672
                    }
2673
                }
2674
                // 첫번째이며 시작 심볼이 아니고 Connecotr일 경우
2675
                else if (i == 0 && prevLMConnector.ConnectItem1SymbolObject != null)
2676
                    placeRunInputs.AddSymbolTarget(prevLMConnector.ConnectItem1SymbolObject, points[0], points[1], diagonal);
2677
                // 마지막이며 마지막 심볼이 아니고 Connecotr일 경우
2678
                else if (i == vertices.Count - 1 && prevLMConnector.ConnectItem2SymbolObject != null)
2679
                    placeRunInputs.AddSymbolTarget(prevLMConnector.ConnectItem2SymbolObject, points[0], points[1], diagonal);
2680
                else
2681
                    placeRunInputs.AddPoint(points[0], points[1]);
2682
            }
2683

    
2684
            _placement.PIDRemovePlacement(prevLMConnector.AsLMRepresentation());
2685
            newConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
2686

    
2687
            ReleaseCOMObjects(placeRunInputs);
2688
            ReleaseCOMObjects(_LMAItem);
2689
            ReleaseCOMObjects(modelItem);
2690

    
2691
            if (newConnector != null)
2692
            {
2693
                newConnector.Commit();
2694
                if (startSymbol != null && bStart)
2695
                {
2696
                    _LMAItem = _placement.PIDCreateItem(symbolPath);
2697
                    placeRunInputs = new PlaceRunInputs();
2698
                    placeRunInputs.AddSymbolTarget(startSymbol, vertices[0][0], vertices[0][1]);
2699
                    placeRunInputs.AddConnectorTarget(newConnector, vertices[0][0], vertices[0][1]);
2700
                    LMConnector _LMConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
2701
                    if (_LMConnector != null)
2702
                    {
2703
                        _LMConnector.Commit();
2704
                        RemoveConnectorForReModelingLine(newConnector);
2705
                        ZeroLengthModelItemID.Add(_LMConnector.ModelItemID);
2706
                        ReleaseCOMObjects(_LMConnector);
2707
                    }
2708
                    ReleaseCOMObjects(placeRunInputs);
2709
                    ReleaseCOMObjects(_LMAItem);
2710
                }
2711

    
2712
                if (endSymbol != null && bEnd)
2713
                {
2714
                    if (startSymbol != null)
2715
                    {
2716
                        Dictionary<LMConnector, List<double[]>> dicVertices = GetPipeRunVertices(newConnector.ModelItemID);
2717
                        newConnector = dicVertices.First().Key;
2718
                    }
2719

    
2720
                    _LMAItem = _placement.PIDCreateItem(symbolPath);
2721
                    placeRunInputs = new PlaceRunInputs();
2722
                    placeRunInputs.AddSymbolTarget(endSymbol, vertices[vertices.Count - 1][0], vertices[vertices.Count - 1][1]);
2723
                    placeRunInputs.AddConnectorTarget(newConnector, vertices[vertices.Count - 1][0], vertices[vertices.Count - 1][1]);
2724
                    LMConnector _LMConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
2725
                    if (_LMConnector != null)
2726
                    {
2727
                        _LMConnector.Commit();
2728
                        RemoveConnectorForReModelingLine(newConnector);
2729
                        ZeroLengthModelItemIDReverse.Add(_LMConnector.ModelItemID);
2730
                        ReleaseCOMObjects(_LMConnector);
2731
                    }
2732
                    ReleaseCOMObjects(placeRunInputs);
2733
                    ReleaseCOMObjects(_LMAItem);
2734
                }
2735

    
2736
                line.SPPID.ModelItemId = newConnector.ModelItemID;
2737
                ReleaseCOMObjects(newConnector);
2738
            }
2739

    
2740
            ReleaseCOMObjects(modelItem);
2741
        }
2742

    
2743
        /// <summary>
2744
        /// Remodeling 과정에서 생긴 불필요한 Connector 제거
2745
        /// </summary>
2746
        /// <param name="connector"></param>
2747
        private void RemoveConnectorForReModelingLine(LMConnector connector)
2748
        {
2749
            Dictionary<LMConnector, List<double[]>> dicVertices = GetPipeRunVertices(connector.ModelItemID);
2750
            foreach (var item in dicVertices)
2751
            {
2752
                if (item.Value.Count == 2)
2753
                {
2754
                    bool result = false;
2755
                    foreach (var point in item.Value)
2756
                    {
2757
                        if (point[0] < 0 || point[1] < 0)
2758
                        {
2759
                            result = true;
2760
                            _placement.PIDRemovePlacement(item.Key.AsLMRepresentation());
2761
                            break;
2762
                        }
2763
                    }
2764

    
2765
                    if (result)
2766
                        break;
2767
                }
2768
            }
2769
            foreach (var item in dicVertices)
2770
                ReleaseCOMObjects(item.Key);
2771
        }
2772

    
2773
        /// <summary>
2774
        /// Symbol이 모델링된 SPPPID Symbol Object를 반환 - 연결된 Symbol이 ChildSymbol일 수도 있기때문에 메서드 개발
2775
        /// </summary>
2776
        /// <param name="symbol"></param>
2777
        /// <param name="line"></param>
2778
        /// <returns></returns>
2779
        private LMSymbol GetTargetSymbol(Symbol symbol, Line line)
2780
        {
2781
            LMSymbol _LMSymbol = null;
2782
            foreach (var connector in symbol.CONNECTORS)
2783
            {
2784
                if (connector.CONNECTEDITEM == line.UID)
2785
                {
2786
                    if (connector.Index == 0)
2787
                        _LMSymbol = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
2788
                    else
2789
                    {
2790
                        ChildSymbol child = null;
2791
                        foreach (var childSymbol in symbol.ChildSymbols)
2792
                        {
2793
                            if (childSymbol.Connectors.Contains(connector))
2794
                                child = childSymbol;
2795
                            else
2796
                                child = GetChildSymbolByConnector(childSymbol, connector);
2797

    
2798
                            if (child != null)
2799
                                break;
2800
                        }
2801

    
2802
                        if (child != null)
2803
                            _LMSymbol = dataSource.GetSymbol(child.SPPID.RepresentationId);
2804
                    }
2805

    
2806
                    break;
2807
                }
2808
            }
2809

    
2810
            return _LMSymbol;
2811
        }
2812

    
2813
        /// <summary>
2814
        /// Connector를 가지고 있는 ChildSymbol Object 반환
2815
        /// </summary>
2816
        /// <param name="item"></param>
2817
        /// <param name="connector"></param>
2818
        /// <returns></returns>
2819
        private ChildSymbol GetChildSymbolByConnector(ChildSymbol item, Connector connector)
2820
        {
2821
            foreach (var childSymbol in item.ChildSymbols)
2822
            {
2823
                if (childSymbol.Connectors.Contains(connector))
2824
                    return childSymbol;
2825
                else
2826
                    return GetChildSymbolByConnector(childSymbol, connector);
2827
            }
2828

    
2829
            return null;
2830
        }
2831

    
2832
        /// <summary>
2833
        /// EndBreak 모델링 메서드
2834
        /// </summary>
2835
        /// <param name="endBreak"></param>
2836
        private void EndBreakModeling(EndBreak endBreak)
2837
        {
2838
            object ownerObj = SPPIDUtil.FindObjectByUID(document, endBreak.OWNER);
2839
            object connectedItem = SPPIDUtil.FindObjectByUID(document, endBreak.PROPERTIES.Find(x => x.ATTRIBUTE == "Connected Item").VALUE);
2840

    
2841
            LMConnector targetLMConnector = FindBreakLineTarget(ownerObj, connectedItem);
2842
            if (ownerObj.GetType() == typeof(Symbol) && connectedItem.GetType() == typeof(Symbol) && targetLMConnector != null)
2843
                targetLMConnector = ReModelingZeroLengthLMConnectorForSegment(targetLMConnector);
2844

    
2845
            if (targetLMConnector != null)
2846
            {
2847
                double[] point = GetSegmentPoint(ownerObj, connectedItem, targetLMConnector);
2848
                Array array = null;
2849
                if (point != null)
2850
                    array = new double[] { 0, point[0], point[1] };
2851
                else
2852
                    array = new double[] { 0, endBreak.SPPID.ORIGINAL_X, endBreak.SPPID.ORIGINAL_Y };
2853
                LMLabelPersist _LmLabelPersist = _placement.PIDPlaceLabel(endBreak.SPPID.MAPPINGNAME, ref array, LabeledItem: targetLMConnector.AsLMRepresentation(), IsLeaderVisible: true);
2854
                if (_LmLabelPersist != null)
2855
                {
2856
                    _LmLabelPersist.Commit();
2857
                    endBreak.SPPID.RepresentationId = _LmLabelPersist.AsLMRepresentation().Id;
2858
                    if (_LmLabelPersist.ModelItemObject != null)
2859
                        endBreak.SPPID.ModelItemID = _LmLabelPersist.ModelItemID;
2860
                    endBreak.SPPID.GraphicOID = _LmLabelPersist.get_GraphicOID().ToString();
2861
                    ReleaseCOMObjects(_LmLabelPersist);
2862
                }
2863
                ReleaseCOMObjects(targetLMConnector);
2864
            }
2865
            else
2866
            {
2867
                Log.Write("End Break UID : " + endBreak.UID);
2868
                Log.Write("Can't find targetLMConnector");
2869
            }
2870
        }
2871

    
2872
        private LMConnector ReModelingZeroLengthLMConnectorForSegment(LMConnector connector, string changeSymbolPath = null)
2873
        {
2874
            string symbolPath = string.Empty;
2875
            #region get symbol path
2876
            if (string.IsNullOrEmpty(changeSymbolPath))
2877
            {
2878
                LMModelItem modelItem = dataSource.GetModelItem(connector.ModelItemID);
2879
                symbolPath = GetSPPIDFileName(modelItem);
2880
                ReleaseCOMObjects(modelItem);
2881
            }
2882
            else
2883
                symbolPath = changeSymbolPath;
2884
            
2885
            #endregion
2886

    
2887
            LMConnector newConnector = null;
2888
            dynamic OID = connector.get_GraphicOID().ToString();
2889
            DependencyObject drawingObject = radApp.ActiveDocument.ActiveSheet.DrawingObjects[OID];
2890
            Ingr.RAD2D.LineStringGeometry2d lineStringGeometry = drawingObject.GetGeometry() as Ingr.RAD2D.LineStringGeometry2d;
2891
            int verticesCount = lineStringGeometry.VertexCount;
2892
            PlaceRunInputs placeRunInputs = new PlaceRunInputs();
2893
            _LMAItem _LMAItem = _placement.PIDCreateItem(symbolPath);
2894

    
2895
            if (Convert.ToBoolean(connector.get_IsZeroLength()))
2896
            {
2897
                double[] vertices = null;
2898
                lineStringGeometry.GetVertices(ref verticesCount, ref vertices);
2899
                double x = 0;
2900
                double y = 0;
2901
                lineStringGeometry.GetVertex(1, ref x, ref y);
2902

    
2903
                string flowDirection = string.Empty;
2904
                LMAAttribute flowAttribute = connector.ModelItemObject.Attributes["FlowDirection"];
2905
                if (flowAttribute != null && !DBNull.Value.Equals(flowAttribute.get_Value()))
2906
                    flowDirection = flowAttribute.get_Value().ToString();
2907

    
2908
                if (flowDirection == "End 1 is downstream (Outlet)")
2909
                {
2910
                    placeRunInputs.AddSymbolTarget(connector.ConnectItem2SymbolObject, x, y);
2911
                    placeRunInputs.AddSymbolTarget(connector.ConnectItem1SymbolObject, x, y);
2912
                    flowDirection = "End 1 is upstream (Inlet)";
2913
                }
2914
                else
2915
                {
2916
                    placeRunInputs.AddSymbolTarget(connector.ConnectItem1SymbolObject, x, y);
2917
                    placeRunInputs.AddSymbolTarget(connector.ConnectItem2SymbolObject, x, y);
2918
                }
2919
                string oldModelItemId = connector.ModelItemID;
2920
                _placement.PIDRemovePlacement(connector.AsLMRepresentation());
2921
                newConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
2922
                newConnector.Commit();
2923
                ZeroLengthSymbolToSymbolModelItemID.Add(newConnector.ModelItemID);
2924
                if (!string.IsNullOrEmpty(flowDirection))
2925
                    newConnector.ModelItemObject.Attributes["FlowDirection"].set_Value(flowDirection);
2926
                ReleaseCOMObjects(connector);
2927

    
2928
                foreach (var line in document.LINES.FindAll(z => z.SPPID.ModelItemId == oldModelItemId))
2929
                {
2930
                    foreach (var repId in line.SPPID.Representations)
2931
                    {
2932
                        LMConnector _connector = dataSource.GetConnector(repId);
2933
                        if (_connector != null && _connector.get_ItemStatus() == "Active")
2934
                        {
2935
                            if (line.SPPID.ModelItemId != _connector.ModelItemID)
2936
                            {
2937
                                line.SPPID.ModelItemId = _connector.ModelItemID;
2938
                                line.SPPID.Representations = GetRepresentations(line.SPPID.ModelItemId);
2939
                            }
2940
                        }
2941
                        ReleaseCOMObjects(_connector);
2942
                        _connector = null;
2943
                    }
2944
                }
2945
            }
2946

    
2947
            return newConnector;
2948
        }
2949

    
2950
        /// <summary>
2951
        /// SpecBreak Modeling 메서드
2952
        /// </summary>
2953
        /// <param name="specBreak"></param>
2954
        private void SpecBreakModeling(SpecBreak specBreak)
2955
        {
2956
            object upStreamObj = SPPIDUtil.FindObjectByUID(document, specBreak.UpStreamUID);
2957
            object downStreamObj = SPPIDUtil.FindObjectByUID(document, specBreak.DownStreamUID);
2958

    
2959
            if (upStreamObj != null &&
2960
                downStreamObj != null)
2961
            {
2962
                LMConnector targetLMConnector = FindBreakLineTarget(upStreamObj, downStreamObj);
2963
                if (upStreamObj.GetType() == typeof(Symbol) && downStreamObj.GetType() == typeof(Symbol) && targetLMConnector != null)
2964
                    targetLMConnector = ReModelingZeroLengthLMConnectorForSegment(targetLMConnector);
2965

    
2966
                if (targetLMConnector != null)
2967
                {
2968
                    foreach (var attribute in specBreak.ATTRIBUTES)
2969
                    {
2970
                        AttributeMapping mapping = document.AttributeMappings.Find(x => x.UID == attribute.UID);
2971
                        if (mapping != null && !string.IsNullOrEmpty(mapping.SPPIDSYMBOLNAME) && mapping.SPPIDSYMBOLNAME != "None")
2972
                        {
2973
                            string MappingPath = mapping.SPPIDSYMBOLNAME;
2974
                            double[] point = GetSegmentPoint(upStreamObj, downStreamObj, targetLMConnector);
2975
                            Array array = null;
2976
                            if (point != null)
2977
                                array = new double[] { 0, point[0], point[1] };
2978
                            else
2979
                                array = new double[] { 0, specBreak.SPPID.ORIGINAL_X, specBreak.SPPID.ORIGINAL_Y };
2980
                            LMLabelPersist _LmLabelPersist = _placement.PIDPlaceLabel(MappingPath, ref array, LabeledItem: targetLMConnector.AsLMRepresentation(), IsLeaderVisible: mapping.LeaderLine);
2981

    
2982
                            if (_LmLabelPersist != null)
2983
                            {
2984
                                _LmLabelPersist.Commit();
2985
                                specBreak.SPPID.RepresentationId = _LmLabelPersist.AsLMRepresentation().Id;
2986
                                if (_LmLabelPersist.ModelItemObject != null)
2987
                                    specBreak.SPPID.ModelItemID = _LmLabelPersist.ModelItemID;
2988
                                specBreak.SPPID.GraphicOID = _LmLabelPersist.get_GraphicOID().ToString();
2989
                                ReleaseCOMObjects(_LmLabelPersist);
2990
                            }
2991

    
2992
                            // temp
2993
                            ReleaseCOMObjects(_placement.PIDPlaceSymbol(@"\Design\Annotation\Graphics\Break.sym", specBreak.SPPID.ORIGINAL_X, specBreak.SPPID.ORIGINAL_Y, Rotation: specBreak.ANGLE));
2994
                        }
2995
                    }
2996
                    ReleaseCOMObjects(targetLMConnector);
2997
                }
2998
                else
2999
                {
3000
                    Log.Write("Spec Break UID : " + specBreak.UID);
3001
                    Log.Write("Can't find targetLMConnector");
3002
                }
3003
            }
3004
        }
3005

    
3006
        private LMConnector FindBreakLineTarget(object targetObj, object connectedObj)
3007
        {
3008
            LMConnector targetConnector = null;
3009
            Symbol targetSymbol = targetObj as Symbol;
3010
            Symbol connectedSymbol = connectedObj as Symbol;
3011
            Line targetLine = targetObj as Line;
3012
            Line connectedLine = connectedObj as Line;
3013
            if (targetSymbol != null && connectedSymbol != null)
3014
            {
3015
                LMSymbol targetLMSymbol = dataSource.GetSymbol(targetSymbol.SPPID.RepresentationId);
3016
                LMSymbol connectedLMSymbol = dataSource.GetSymbol(connectedSymbol.SPPID.RepresentationId);
3017

    
3018
                foreach (LMConnector connector in targetLMSymbol.Avoid1Connectors)
3019
                {
3020
                    if (connector.get_ItemStatus() != "Active")
3021
                        continue;
3022

    
3023
                    if (connector.ConnectItem1SymbolObject.Id == connectedLMSymbol.Id)
3024
                    {
3025
                        targetConnector = connector;
3026
                        break;
3027
                    }
3028
                    else if (connector.ConnectItem2SymbolObject.Id == connectedLMSymbol.Id)
3029
                    {
3030
                        targetConnector = connector;
3031
                        break;
3032
                    }
3033
                }
3034

    
3035
                foreach (LMConnector connector in targetLMSymbol.Avoid2Connectors)
3036
                {
3037
                    if (connector.get_ItemStatus() != "Active")
3038
                        continue;
3039

    
3040
                    if (connector.ConnectItem1SymbolObject.Id == connectedLMSymbol.Id)
3041
                    {
3042
                        targetConnector = connector;
3043
                        break;
3044
                    }
3045
                    else if (connector.ConnectItem2SymbolObject.Id == connectedLMSymbol.Id)
3046
                    {
3047
                        targetConnector = connector;
3048
                        break;
3049
                    }
3050
                }
3051

    
3052
                ReleaseCOMObjects(targetLMSymbol);
3053
                ReleaseCOMObjects(connectedLMSymbol);
3054
            }
3055
            else if (targetLine != null && connectedLine != null)
3056
            {
3057
                LMModelItem targetModelItem = dataSource.GetModelItem(targetLine.SPPID.ModelItemId);
3058
                LMModelItem connectedModelItem = dataSource.GetModelItem(connectedLine.SPPID.ModelItemId);
3059

    
3060
                if (targetModelItem != null && targetModelItem.get_ItemStatus() == "Active" && connectedModelItem != null && connectedModelItem.get_ItemStatus() == "Active")
3061
                {
3062
                    foreach (LMRepresentation rep in targetModelItem.Representations)
3063
                    {
3064
                        if (targetConnector != null)
3065
                            break;
3066

    
3067
                        if (rep.Attributes["RepresentationType"].get_Value() == "Connector" && rep.Attributes["ItemStatus"].get_Value() == "Active")
3068
                        {
3069
                            LMConnector _LMConnector = dataSource.GetConnector(rep.Id);
3070

    
3071
                            if (IsConnected(_LMConnector, connectedModelItem))
3072
                                targetConnector = _LMConnector;
3073
                            else
3074
                                ReleaseCOMObjects(_LMConnector);
3075
                        }
3076
                    }
3077

    
3078
                    ReleaseCOMObjects(targetModelItem);
3079
                }
3080
            }
3081
            else
3082
            {
3083
                LMSymbol connectedLMSymbol = null;
3084
                if (connectedSymbol != null)
3085
                    connectedLMSymbol = dataSource.GetSymbol(connectedSymbol.SPPID.RepresentationId);
3086
                else if (targetSymbol != null)
3087
                    connectedLMSymbol = dataSource.GetSymbol(targetSymbol.SPPID.RepresentationId);
3088
                else
3089
                {
3090

    
3091
                }
3092
                LMModelItem targetModelItem = null;
3093
                if (targetLine != null)
3094
                    targetModelItem = dataSource.GetModelItem(targetLine.SPPID.ModelItemId);
3095
                else if (connectedLine != null)
3096
                    targetModelItem = dataSource.GetModelItem(connectedLine.SPPID.ModelItemId);
3097
                else
3098
                {
3099

    
3100
                }
3101
                if (connectedLMSymbol != null && targetModelItem != null)
3102
                {
3103
                    foreach (LMConnector connector in connectedLMSymbol.Avoid1Connectors)
3104
                    {
3105
                        if (connector.get_ItemStatus() != "Active")
3106
                            continue;
3107

    
3108
                        if (IsConnected(connector, targetModelItem))
3109
                        {
3110
                            targetConnector = connector;
3111
                            break;
3112
                        }
3113
                    }
3114

    
3115
                    if (targetConnector == null)
3116
                    {
3117
                        foreach (LMConnector connector in connectedLMSymbol.Avoid2Connectors)
3118
                        {
3119
                            if (connector.get_ItemStatus() != "Active")
3120
                                continue;
3121

    
3122
                            if (IsConnected(connector, targetModelItem))
3123
                            {
3124
                                targetConnector = connector;
3125
                                break;
3126
                            }
3127
                        }
3128
                    }
3129
                }
3130

    
3131
            }
3132

    
3133
            return targetConnector;
3134
        }
3135

    
3136
        private double[] GetSegmentPoint(object targetObj, object connObj, LMConnector targetConnector)
3137
        {
3138
            double[] result = null;
3139
            Line targetLine = targetObj as Line;
3140
            Symbol targetSymbol = targetObj as Symbol;
3141
            Line connLine = connObj as Line;
3142
            Symbol connSymbol = connObj as Symbol;
3143

    
3144
            double zeroLengthMove = GridSetting.GetInstance().Length * 3;
3145
            double lineMove = GridSetting.GetInstance().Length * 3;
3146
            if (Convert.ToBoolean(targetConnector.get_IsZeroLength()))
3147
            {
3148
                result = GetConnectorVertices(targetConnector)[0];
3149
                if (targetSymbol != null && connSymbol != null)
3150
                {
3151
                    SlopeType slopeType = SPPIDUtil.CalcSlope(targetSymbol.SPPID.SPPID_X, targetSymbol.SPPID.SPPID_Y, connSymbol.SPPID.SPPID_X, connSymbol.SPPID.SPPID_Y);
3152
                    if (slopeType == SlopeType.HORIZONTAL)
3153
                        result = new double[] { result[0], result[1] - zeroLengthMove };
3154
                    else if (slopeType == SlopeType.VERTICAL)
3155
                        result = new double[] { result[0] + zeroLengthMove, result[1] };
3156
                }
3157
                else if (targetLine != null)
3158
                {
3159
                    if (targetLine.SlopeType == SlopeType.HORIZONTAL)
3160
                        result = new double[] { result[0], result[1] - zeroLengthMove };
3161
                    else if (targetLine.SlopeType == SlopeType.VERTICAL)
3162
                        result = new double[] { result[0] + zeroLengthMove, result[1] };
3163
                }
3164
                else if (connLine != null)
3165
                {
3166
                    if (connLine.SlopeType == SlopeType.HORIZONTAL)
3167
                        result = new double[] { result[0], result[1] - zeroLengthMove };
3168
                    else if (connLine.SlopeType == SlopeType.VERTICAL)
3169
                        result = new double[] { result[0] + zeroLengthMove, result[1] };
3170
                }
3171
            }
3172
            else
3173
            {
3174
                if (targetObj.GetType() == typeof(Line) && connObj.GetType() == typeof(Line))
3175
                {
3176
                    Line line = connObj as Line;
3177
                    LMConnector connectedConnector = null;
3178
                    int connIndex = 0;
3179
                    LMModelItem modelItem = dataSource.GetModelItem(line.SPPID.ModelItemId);
3180
                    FindConnectedConnector(targetConnector, modelItem, ref connectedConnector, ref connIndex);
3181

    
3182
                    List<double[]> vertices = GetConnectorVertices(targetConnector);
3183

    
3184
                    ReleaseCOMObjects(modelItem);
3185
                    ReleaseCOMObjects(connectedConnector);
3186

    
3187
                    if (vertices.Count > 0)
3188
                    {
3189
                        if (connIndex == 1)
3190
                            result = vertices[0];
3191
                        else if (connIndex == 2)
3192
                            result = vertices[vertices.Count - 1];
3193

    
3194
                        if (targetLine.SlopeType == SlopeType.HORIZONTAL)
3195
                        {
3196
                            result = new double[] { result[0], result[1] - lineMove };
3197
                            if (targetLine.CONNECTORS[0].CONNECTEDITEM == connLine.UID && targetLine.SPPID.START_X < targetLine.SPPID.END_X)
3198
                            {
3199
                                result = new double[] { result[0] - lineMove, result[1] };
3200
                            }
3201
                            else if (targetLine.CONNECTORS[0].CONNECTEDITEM == connLine.UID && targetLine.SPPID.START_X > targetLine.SPPID.END_X)
3202
                            {
3203
                                result = new double[] { result[0] + lineMove, result[1] };
3204
                            }
3205
                            else if (targetLine.CONNECTORS[1].CONNECTEDITEM == connLine.UID && targetLine.SPPID.START_X < targetLine.SPPID.END_X)
3206
                            {
3207
                                result = new double[] { result[0] + lineMove, result[1] };
3208
                            }
3209
                            else if (targetLine.CONNECTORS[1].CONNECTEDITEM == connLine.UID && targetLine.SPPID.START_X > targetLine.SPPID.END_X)
3210
                            {
3211
                                result = new double[] { result[0] - lineMove, result[1] };
3212
                            }
3213
                        }
3214
                        else if (targetLine.SlopeType == SlopeType.VERTICAL)
3215
                        {
3216
                            result = new double[] { result[0] - lineMove, result[1] };
3217
                            if (targetLine.CONNECTORS[0].CONNECTEDITEM == connLine.UID && targetLine.SPPID.START_Y < targetLine.SPPID.END_Y)
3218
                            {
3219
                                result = new double[] { result[0], result[1] - lineMove };
3220
                            }
3221
                            else if (targetLine.CONNECTORS[0].CONNECTEDITEM == connLine.UID && targetLine.SPPID.START_Y > targetLine.SPPID.END_Y)
3222
                            {
3223
                                result = new double[] { result[0], result[1] + lineMove };
3224
                            }
3225
                            else if (targetLine.CONNECTORS[1].CONNECTEDITEM == connLine.UID && targetLine.SPPID.START_Y < targetLine.SPPID.END_Y)
3226
                            {
3227
                                result = new double[] { result[0], result[1] + lineMove };
3228
                            }
3229
                            else if (targetLine.CONNECTORS[1].CONNECTEDITEM == connLine.UID && targetLine.SPPID.START_Y > targetLine.SPPID.END_Y)
3230
                            {
3231
                                result = new double[] { result[0], result[1] - lineMove };
3232
                            }
3233
                        }
3234
                            
3235
                    }
3236
                }
3237
                else
3238
                {
3239
                    Log.Write("error in GetSegemtPoint");
3240
                }
3241
            }
3242

    
3243
            return result;
3244
        }
3245

    
3246
        private bool IsConnected(LMConnector connector, LMModelItem modelItem)
3247
        {
3248
            bool result = false;
3249

    
3250
            foreach (LMRepresentation rep in modelItem.Representations)
3251
            {
3252
                if (result)
3253
                    break;
3254

    
3255
                if (rep.Attributes["RepresentationType"].get_Value() == "Connector" && rep.Attributes["ItemStatus"].get_Value() == "Active")
3256
                {
3257
                    LMConnector _LMConnector = dataSource.GetConnector(rep.Id);
3258

    
3259
                    if (_LMConnector.ConnectItem1SymbolObject != null &&
3260
                        connector.ConnectItem1SymbolObject != null &&
3261
                        _LMConnector.ConnectItem1SymbolObject.Id == connector.ConnectItem1SymbolObject.Id)
3262
                    {
3263
                        result = true;
3264
                        ReleaseCOMObjects(_LMConnector);
3265
                        break;
3266
                    }
3267
                    else if (_LMConnector.ConnectItem1SymbolObject != null &&
3268
                        connector.ConnectItem2SymbolObject != null &&
3269
                        _LMConnector.ConnectItem1SymbolObject.Id == connector.ConnectItem2SymbolObject.Id)
3270
                    {
3271
                        result = true;
3272
                        ReleaseCOMObjects(_LMConnector);
3273
                        break;
3274
                    }
3275
                    else if (_LMConnector.ConnectItem2SymbolObject != null &&
3276
                        connector.ConnectItem1SymbolObject != null &&
3277
                        _LMConnector.ConnectItem2SymbolObject.Id == connector.ConnectItem1SymbolObject.Id)
3278
                    {
3279
                        result = true;
3280
                        ReleaseCOMObjects(_LMConnector);
3281
                        break;
3282
                    }
3283
                    else if (_LMConnector.ConnectItem2SymbolObject != null &&
3284
                        connector.ConnectItem2SymbolObject != null &&
3285
                        _LMConnector.ConnectItem2SymbolObject.Id == connector.ConnectItem2SymbolObject.Id)
3286
                    {
3287
                        result = true;
3288
                        ReleaseCOMObjects(_LMConnector);
3289
                        break;
3290
                    }
3291

    
3292
                    ReleaseCOMObjects(_LMConnector);
3293
                }
3294
            }
3295

    
3296

    
3297
            return result;
3298
        }
3299

    
3300
        private void FindConnectedConnector(LMConnector connector, LMModelItem modelItem, ref LMConnector connectedConnector, ref int connectorIndex)
3301
        {
3302
            foreach (LMRepresentation rep in modelItem.Representations)
3303
            {
3304
                if (connectedConnector != null)
3305
                    break;
3306

    
3307
                if (rep.Attributes["RepresentationType"].get_Value() == "Connector" && rep.Attributes["ItemStatus"].get_Value() == "Active")
3308
                {
3309
                    LMConnector _LMConnector = dataSource.GetConnector(rep.Id);
3310

    
3311
                    if (_LMConnector.ConnectItem1SymbolObject != null &&
3312
                        connector.ConnectItem1SymbolObject != null &&
3313
                        _LMConnector.ConnectItem1SymbolObject.Id == connector.ConnectItem1SymbolObject.Id)
3314
                    {
3315
                        connectedConnector = _LMConnector;
3316
                        connectorIndex = 1;
3317
                        break;
3318
                    }
3319
                    else if (_LMConnector.ConnectItem1SymbolObject != null &&
3320
                        connector.ConnectItem2SymbolObject != null &&
3321
                        _LMConnector.ConnectItem1SymbolObject.Id == connector.ConnectItem2SymbolObject.Id)
3322
                    {
3323
                        connectedConnector = _LMConnector;
3324
                        connectorIndex = 2;
3325
                        break;
3326
                    }
3327
                    else if (_LMConnector.ConnectItem2SymbolObject != null &&
3328
                        connector.ConnectItem1SymbolObject != null &&
3329
                        _LMConnector.ConnectItem2SymbolObject.Id == connector.ConnectItem1SymbolObject.Id)
3330
                    {
3331
                        connectedConnector = _LMConnector;
3332
                        connectorIndex = 1;
3333
                        break;
3334
                    }
3335
                    else if (_LMConnector.ConnectItem2SymbolObject != null &&
3336
                        connector.ConnectItem2SymbolObject != null &&
3337
                        _LMConnector.ConnectItem2SymbolObject.Id == connector.ConnectItem2SymbolObject.Id)
3338
                    {
3339
                        connectedConnector = _LMConnector;
3340
                        connectorIndex = 2;
3341
                        break;
3342
                    }
3343

    
3344
                    if (connectedConnector == null)
3345
                        ReleaseCOMObjects(_LMConnector);
3346
                }
3347
            }
3348
        }
3349

    
3350
        /// <summary>
3351
        /// FromModelItem을 ToModelItem으로 PipeRunJoin하는 메서드
3352
        /// </summary>
3353
        /// <param name="modelItemID1"></param>
3354
        /// <param name="modelItemID2"></param>
3355
        private void JoinRun(string modelId1, string modelId2, ref string survivorId, bool IsSameConnector = true)
3356
        {
3357
            try
3358
            {
3359
                LMModelItem modelItem1 = dataSource.GetModelItem(modelId1);
3360
                LMConnector connector1 = GetLMConnectorFirst(modelId1);
3361
                List<double[]> vertices1 = null;
3362
                string graphicOID1 = string.Empty;
3363
                if (connector1 != null)
3364
                {
3365
                    vertices1 = GetConnectorVertices(connector1);
3366
                    graphicOID1 = connector1.get_GraphicOID();
3367
                }
3368
                _LMAItem item1 = modelItem1.AsLMAItem();
3369
                ReleaseCOMObjects(connector1);
3370
                connector1 = null;
3371

    
3372
                LMModelItem modelItem2 = dataSource.GetModelItem(modelId2);
3373
                LMConnector connector2 = GetLMConnectorFirst(modelId2);
3374
                List<double[]> vertices2 = null;
3375
                string graphicOID2 = string.Empty;
3376
                if (connector2 != null)
3377
                {
3378
                    vertices2 = GetConnectorVertices(connector2);
3379
                    graphicOID2 = connector2.get_GraphicOID();
3380
                }
3381
                _LMAItem item2 = modelItem2.AsLMAItem();
3382
                ReleaseCOMObjects(connector2);
3383
                connector2 = null;
3384

    
3385
                // item2가 item1으로 조인
3386
                _placement.PIDJoinRuns(ref item1, ref item2);
3387
                item1.Commit();
3388
                item2.Commit();
3389

    
3390
                string beforeID = string.Empty;
3391
                string afterID = string.Empty;
3392

    
3393
                if (modelItem1.get_ItemStatus() == "Active" && modelItem2.get_ItemStatus() != "Active")
3394
                {
3395
                    beforeID = modelItem2.Id;
3396
                    afterID = modelItem1.Id;
3397
                    survivorId = afterID;
3398
                }
3399
                else if (modelItem1.get_ItemStatus() != "Active" && modelItem2.get_ItemStatus() == "Active")
3400
                {
3401
                    beforeID = modelItem1.Id;
3402
                    afterID = modelItem2.Id;
3403
                    survivorId = afterID;
3404
                }
3405
                else if (modelItem1.get_ItemStatus() == "Active" && modelItem2.get_ItemStatus() == "Active")
3406
                {
3407
                    int model1Cnt = GetConnectorCount(modelId1);
3408
                    int model2Cnt = GetConnectorCount(modelId2);
3409
                    if (model1Cnt == 0)
3410
                    {
3411
                        beforeID = modelItem1.Id;
3412
                        afterID = modelItem2.Id;
3413
                        survivorId = afterID;
3414
                    }
3415
                    else if (model2Cnt == 0)
3416
                    {
3417
                        beforeID = modelItem2.Id;
3418
                        afterID = modelItem1.Id;
3419
                        survivorId = afterID;
3420
                    }
3421
                    else
3422
                        survivorId = null;
3423
                }
3424
                else
3425
                {
3426
                    Log.Write("잘못된 경우");
3427
                    survivorId = null;
3428
                }
3429

    
3430
                if (!string.IsNullOrEmpty(beforeID) && !string.IsNullOrEmpty(afterID))
3431
                {
3432
                    List<Line> lines = SPPIDUtil.FindLinesByModelId(document, beforeID);
3433
                    foreach (var line in lines)
3434
                        line.SPPID.ModelItemId = afterID;
3435
                }
3436

    
3437
                ReleaseCOMObjects(modelItem1);
3438
                ReleaseCOMObjects(item1);
3439
                ReleaseCOMObjects(modelItem2);
3440
                ReleaseCOMObjects(item2);
3441
            }
3442
            catch (Exception ex)
3443
            {
3444
                Log.Write("Join Error");
3445
                Log.Write(ex.Message + "\r\n" + ex.StackTrace);
3446
            }
3447
        }
3448

    
3449
        private List<string> FindOtherModelItemBySymbolWhereTypePipeRun(LMSymbol symbol, string modelId)
3450
        {
3451
            List<string> temp = new List<string>();
3452
            List<LMConnector> connectors = new List<LMConnector>();
3453
            foreach (LMConnector connector in symbol.Avoid1Connectors)
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

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

    
3478
            foreach (LMConnector connector in symbol.Avoid2Connectors)
3479
            {
3480
                if (connector.get_ItemStatus() != "Active")
3481
                    continue;
3482

    
3483
                LMModelItem modelItem = connector.ModelItemObject;
3484
                LMSymbol connOtherSymbol = FindOtherConnectedSymbol(connector);
3485
                if (modelItem.get_ItemStatus() == "Active" && modelItem.get_ItemTypeName().ToString() == "PipeRun" && modelItem.Id != modelId && !temp.Contains(modelItem.Id))
3486
                    temp.Add(modelItem.Id);
3487

    
3488
                if (temp.Contains(modelItem.Id) &&
3489
                    connOtherSymbol != null &&
3490
                    connOtherSymbol.get_RepresentationType() == "Branch" &&
3491
                    Convert.ToBoolean(connector.get_IsZeroLength()))
3492
                    temp.Remove(modelItem.Id);
3493

    
3494
                if (temp.Contains(modelItem.Id))
3495
                    connectors.Add(connector);
3496
                ReleaseCOMObjects(connOtherSymbol);
3497
                connOtherSymbol = null;
3498
                ReleaseCOMObjects(modelItem);
3499
                modelItem = null;
3500
            }
3501

    
3502

    
3503
            List<string> result = new List<string>();
3504
            string originalName = GetSPPIDFileName(modelId);
3505
            foreach (var connector in connectors)
3506
            {
3507
                string fileName = GetSPPIDFileName(connector.ModelItemID);
3508
                if (originalName == fileName)
3509
                    result.Add(connector.ModelItemID);
3510
                else
3511
                {
3512
                    if (document.LINES.Find(x => x.SPPID.ModelItemId == connector.ModelItemID) == null && Convert.ToBoolean(connector.get_IsZeroLength()))
3513
                        result.Add(connector.ModelItemID);
3514
                    else
3515
                    {
3516
                        Line line1 = document.LINES.Find(x => x.SPPID.ModelItemId == modelId);
3517
                        Line line2 = document.LINES.Find(x => x.SPPID.ModelItemId == connector.ModelItemID.ToString());
3518
                        if (line1 != null && line2 != null && line1.TYPE == line2.TYPE)
3519
                            result.Add(connector.ModelItemID);
3520
                    }
3521
                }
3522
            }
3523

    
3524
            foreach (var connector in connectors)
3525
                ReleaseCOMObjects(connector);
3526
            
3527
            return result;
3528

    
3529

    
3530
            LMSymbol FindOtherConnectedSymbol(LMConnector connector)
3531
            {
3532
                LMSymbol findResult = null;
3533
                if (connector.ConnectItem1SymbolObject != null && connector.ConnectItem1SymbolObject.Id != symbol.Id && connector.ConnectItem1SymbolObject.get_ItemStatus() == "Active")
3534
                    findResult = connector.ConnectItem1SymbolObject;
3535
                else if (connector.ConnectItem2SymbolObject != null && connector.ConnectItem2SymbolObject.Id != symbol.Id && connector.ConnectItem2SymbolObject.get_ItemStatus() == "Active")
3536
                    findResult = connector.ConnectItem2SymbolObject;
3537

    
3538
                return findResult;
3539
            }
3540
        }
3541

    
3542
        /// <summary>
3543
        /// PipeRun의 좌표를 가져오는 메서드
3544
        /// </summary>
3545
        /// <param name="modelId"></param>
3546
        /// <returns></returns>
3547
        private Dictionary<LMConnector, List<double[]>> GetPipeRunVertices(string modelId, bool ContainZeroLength = true)
3548
        {
3549
            Dictionary<LMConnector, List<double[]>> connectorVertices = new Dictionary<LMConnector, List<double[]>>();
3550
            LMModelItem modelItem = dataSource.GetModelItem(modelId);
3551

    
3552
            if (modelItem != null)
3553
            {
3554
                foreach (LMRepresentation rep in modelItem.Representations)
3555
                {
3556
                    if (rep.Attributes["RepresentationType"].get_Value() == "Connector" && rep.Attributes["ItemStatus"].get_Value() == "Active")
3557
                    {
3558
                        LMConnector _LMConnector = dataSource.GetConnector(rep.Id);
3559
                        if (!ContainZeroLength && Convert.ToBoolean(_LMConnector.get_IsZeroLength()))
3560
                        {
3561
                            ReleaseCOMObjects(_LMConnector);
3562
                            _LMConnector = null;
3563
                            continue;
3564
                        }
3565
                        connectorVertices.Add(_LMConnector, new List<double[]>());
3566
                        dynamic OID = rep.get_GraphicOID().ToString();
3567
                        DependencyObject drawingObject = radApp.ActiveDocument.ActiveSheet.DrawingObjects[OID];
3568
                        Ingr.RAD2D.LineStringGeometry2d lineStringGeometry = drawingObject.GetGeometry() as Ingr.RAD2D.LineStringGeometry2d;
3569
                        int verticesCount = lineStringGeometry.VertexCount;
3570
                        double[] vertices = null;
3571
                        lineStringGeometry.GetVertices(ref verticesCount, ref vertices);
3572
                        for (int i = 0; i < verticesCount; i++)
3573
                        {
3574
                            double x = 0;
3575
                            double y = 0;
3576
                            lineStringGeometry.GetVertex(i + 1, ref x, ref y);
3577
                            connectorVertices[_LMConnector].Add(new double[] { x, y });
3578
                        }
3579
                    }
3580
                }
3581

    
3582
                ReleaseCOMObjects(modelItem);
3583
            }
3584

    
3585
            return connectorVertices;
3586
        }
3587

    
3588
        private List<double[]> GetConnectorVertices(LMConnector connector)
3589
        {
3590
            List<double[]> vertices = new List<double[]>();
3591
            if (connector != null)
3592
            {
3593
                dynamic OID = connector.get_GraphicOID().ToString();
3594
                DependencyObject drawingObject = radApp.ActiveDocument.ActiveSheet.DrawingObjects[OID];
3595
                Ingr.RAD2D.LineStringGeometry2d lineStringGeometry = drawingObject.GetGeometry() as Ingr.RAD2D.LineStringGeometry2d;
3596
                int verticesCount = lineStringGeometry.VertexCount;
3597
                double[] value = null;
3598
                lineStringGeometry.GetVertices(ref verticesCount, ref value);
3599
                for (int i = 0; i < verticesCount; i++)
3600
                {
3601
                    double x = 0;
3602
                    double y = 0;
3603
                    lineStringGeometry.GetVertex(i + 1, ref x, ref y);
3604
                    vertices.Add(new double[] { x, y });
3605
                }
3606
            }
3607
            return vertices;
3608
        }
3609

    
3610
        private double GetConnectorDistance(LMConnector connector)
3611
        {
3612
            double result = 0;
3613
            List<double[]> vertices = new List<double[]>();
3614
            if (connector != null)
3615
            {
3616
                dynamic OID = connector.get_GraphicOID().ToString();
3617
                DependencyObject drawingObject = radApp.ActiveDocument.ActiveSheet.DrawingObjects[OID];
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
                    if (vertices.Count > 1)
3629
                    {
3630
                        result += SPPIDUtil.CalcPointToPointdDistance(vertices[vertices.Count - 2][0], vertices[vertices.Count - 2][1], x, y);
3631
                    }
3632
                }
3633
            }
3634
            return result;
3635
        }
3636
        private List<double[]> GetConnectorVertices(dynamic graphicOID)
3637
        {
3638
            List<double[]> vertices = null;
3639
            DependencyObject drawingObject = radApp.ActiveDocument.ActiveSheet.DrawingObjects[graphicOID];
3640
            if (drawingObject != null)
3641
            {
3642
                vertices = new List<double[]>();
3643
                Ingr.RAD2D.LineStringGeometry2d lineStringGeometry = drawingObject.GetGeometry() as Ingr.RAD2D.LineStringGeometry2d;
3644
                int verticesCount = lineStringGeometry.VertexCount;
3645
                double[] value = null;
3646
                lineStringGeometry.GetVertices(ref verticesCount, ref value);
3647
                for (int i = 0; i < verticesCount; i++)
3648
                {
3649
                    double x = 0;
3650
                    double y = 0;
3651
                    lineStringGeometry.GetVertex(i + 1, ref x, ref y);
3652
                    vertices.Add(new double[] { x, y });
3653
                }
3654
            }
3655
            return vertices;
3656
        }
3657
        /// <summary>
3658
        /// 좌표로 PipeRun의 Connector중에 어느 Connector에 가까운지/붙을지 가져오는 메서드 - 조건에 안맞아서 못찾을시 제일 가까운 점으로 가져오는 방식
3659
        /// </summary>
3660
        /// <param name="connectorVertices"></param>
3661
        /// <param name="connX"></param>
3662
        /// <param name="connY"></param>
3663
        /// <returns></returns>
3664
        private LMConnector FindTargetLMConnectorForLabel(Dictionary<LMConnector, List<double[]>> connectorVertices, double connX, double connY)
3665
        {
3666
            double length = double.MaxValue;
3667
            LMConnector targetConnector = null;
3668
            foreach (var item in connectorVertices)
3669
            {
3670
                List<double[]> points = item.Value;
3671
                for (int i = 0; i < points.Count - 1; i++)
3672
                {
3673
                    double[] point1 = points[i];
3674
                    double[] point2 = points[i + 1];
3675
                    double x1 = Math.Min(point1[0], point2[0]);
3676
                    double y1 = Math.Min(point1[1], point2[1]);
3677
                    double x2 = Math.Max(point1[0], point2[0]);
3678
                    double y2 = Math.Max(point1[1], point2[1]);
3679

    
3680
                    if ((x1 <= connX && x2 >= connX) ||
3681
                        (y1 <= connY && y2 >= connY))
3682
                    {
3683
                        double distance = SPPIDUtil.CalcPointToPointdDistance(point1[0], point1[1], connX, connY);
3684
                        if (length >= distance)
3685
                        {
3686
                            targetConnector = item.Key;
3687
                            length = distance;
3688
                        }
3689

    
3690
                        distance = SPPIDUtil.CalcPointToPointdDistance(point2[0], point2[1], connX, connY);
3691
                        if (length >= distance)
3692
                        {
3693
                            targetConnector = item.Key;
3694
                            length = distance;
3695
                        }
3696
                    }
3697
                }
3698
            }
3699

    
3700
            // 못찾았을때.
3701
            length = double.MaxValue;
3702
            if (targetConnector == null)
3703
            {
3704
                foreach (var item in connectorVertices)
3705
                {
3706
                    List<double[]> points = item.Value;
3707

    
3708
                    foreach (double[] point in points)
3709
                    {
3710
                        double distance = SPPIDUtil.CalcPointToPointdDistance(point[0], point[1], connX, connY);
3711
                        if (length >= distance)
3712
                        {
3713
                            targetConnector = item.Key;
3714
                            length = distance;
3715
                        }
3716
                    }
3717
                }
3718
            }
3719

    
3720
            return targetConnector;
3721
        }
3722

    
3723
        private LMConnector FindTargetLMConnectorForBranch(Line line, Line targetLine, ref double x, ref double y)
3724
        {
3725
            Dictionary<LMConnector, List<double[]>> vertices = GetPipeRunVertices(targetLine.SPPID.ModelItemId);
3726
            if (vertices.Count == 0)
3727
                return null;
3728

    
3729
            double length = double.MaxValue;
3730
            LMConnector targetConnector = null;
3731
            double[] resultPoint = null;
3732
            List<double[]> targetVertices = null;
3733

    
3734
            // Vertices 포인트에 제일 가까운곳
3735
            foreach (var item in vertices)
3736
            {
3737
                List<double[]> points = item.Value;
3738
                for (int i = 0; i < points.Count; i++)
3739
                {
3740
                    double[] point = points[i];
3741
                    double tempX = point[0];
3742
                    double tempY = point[1];
3743

    
3744
                    double distance = SPPIDUtil.CalcPointToPointdDistance(tempX, tempY, x, y);
3745
                    if (length >= distance)
3746
                    {
3747
                        targetConnector = item.Key;
3748
                        length = distance;
3749
                        resultPoint = point;
3750
                        targetVertices = item.Value;
3751
                    }
3752
                }
3753
            }
3754

    
3755
            // Vertices Cross에 제일 가까운곳
3756
            foreach (var item in vertices)
3757
            {
3758
                List<double[]> points = item.Value;
3759
                for (int i = 0; i < points.Count - 1; i++)
3760
                {
3761
                    double[] point1 = points[i];
3762
                    double[] point2 = points[i + 1];
3763

    
3764
                    double maxLineX = Math.Max(point1[0], point2[0]);
3765
                    double minLineX = Math.Min(point1[0], point2[0]);
3766
                    double maxLineY = Math.Max(point1[1], point2[1]);
3767
                    double minLineY = Math.Min(point1[1], point2[1]);
3768

    
3769
                    SlopeType slope = SPPIDUtil.CalcSlope(minLineX, minLineY, maxLineX, maxLineY);
3770

    
3771
                    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]);
3772
                    if (crossingPoint != null)
3773
                    {
3774
                        double distance = SPPIDUtil.CalcPointToPointdDistance(crossingPoint[0], crossingPoint[1], x, y);
3775
                        if (length >= distance)
3776
                        {
3777
                            if (slope == SlopeType.Slope &&
3778
                                minLineX <= crossingPoint[0] && maxLineX >= crossingPoint[0] &&
3779
                                minLineY <= crossingPoint[1] && maxLineY >= crossingPoint[1])
3780
                            {
3781
                                targetConnector = item.Key;
3782
                                length = distance;
3783
                                resultPoint = crossingPoint;
3784
                                targetVertices = item.Value;
3785
                            }
3786
                            else if (slope == SlopeType.HORIZONTAL &&
3787
                                minLineX <= crossingPoint[0] && maxLineX >= crossingPoint[0])
3788
                            {
3789
                                targetConnector = item.Key;
3790
                                length = distance;
3791
                                resultPoint = crossingPoint;
3792
                                targetVertices = item.Value;
3793
                            }
3794
                            else if (slope == SlopeType.VERTICAL &&
3795
                               minLineY <= crossingPoint[1] && maxLineY >= crossingPoint[1])
3796
                            {
3797
                                targetConnector = item.Key;
3798
                                length = distance;
3799
                                resultPoint = crossingPoint;
3800
                                targetVertices = item.Value;
3801
                            }
3802
                        }
3803
                    }
3804
                }
3805
            }
3806

    
3807
            foreach (var item in vertices)
3808
                if (item.Key != null && item.Key != targetConnector)
3809
                    ReleaseCOMObjects(item.Key);
3810

    
3811
            if (SPPIDUtil.IsBranchLine(line, targetLine))
3812
            {
3813
                double tempResultX = resultPoint[0];
3814
                double tempResultY = resultPoint[1];
3815
                SPPIDUtil.ConvertGridPoint(ref tempResultX, ref tempResultY);
3816

    
3817
                GridSetting gridSetting = GridSetting.GetInstance();
3818

    
3819
                for (int i = 0; i < targetVertices.Count; i++)
3820
                {
3821
                    double[] point = targetVertices[i];
3822
                    double tempX = targetVertices[i][0];
3823
                    double tempY = targetVertices[i][1];
3824
                    SPPIDUtil.ConvertGridPoint(ref tempX, ref tempY);
3825
                    if (tempX == tempResultX && tempY == tempResultY)
3826
                    {
3827
                        if (i == 0)
3828
                        {
3829
                            LMSymbol connSymbol = targetConnector.ConnectItem1SymbolObject;
3830
                            bool containZeroLength = false;
3831
                            if (connSymbol != null)
3832
                            {
3833
                                foreach (LMConnector connector in connSymbol.Connect1Connectors)
3834
                                {
3835
                                    if (connector.get_ItemStatus() == "Active" && Convert.ToBoolean(connector.get_IsZeroLength()) == true)
3836
                                        containZeroLength = true;
3837
                                }
3838
                                foreach (LMConnector connector in connSymbol.Connect2Connectors)
3839
                                {
3840
                                    if (connector.get_ItemStatus() == "Active" && Convert.ToBoolean(connector.get_IsZeroLength()) == true)
3841
                                        containZeroLength = true;
3842
                                }
3843
                            }
3844

    
3845
                            if (connSymbol == null ||
3846
                                (connSymbol != null && connSymbol.get_ItemStatus() == "Active" && connSymbol.get_RepresentationType() != "Branch") ||
3847
                                containZeroLength)
3848
                            {
3849
                                bool bCalcX = false;
3850
                                bool bCalcY = false;
3851
                                if (targetLine.SlopeType == SlopeType.HORIZONTAL)
3852
                                    bCalcX = true;
3853
                                else if (targetLine.SlopeType == SlopeType.VERTICAL)
3854
                                    bCalcY = true;
3855
                                else
3856
                                {
3857
                                    bCalcX = true;
3858
                                    bCalcY = true;
3859
                                }
3860

    
3861
                                if (bCalcX)
3862
                                {
3863
                                    double nextX = targetVertices[i + 1][0];
3864
                                    double newX = 0;
3865
                                    if (nextX > tempX)
3866
                                    {
3867
                                        newX = tempX + gridSetting.Length;
3868
                                        if (newX > nextX)
3869
                                            newX = (point[0] + nextX) / 2;
3870
                                    }
3871
                                    else
3872
                                    {
3873
                                        newX = tempX - gridSetting.Length;
3874
                                        if (newX < nextX)
3875
                                            newX = (point[0] + nextX) / 2;
3876
                                    }
3877
                                    resultPoint = new double[] { newX, resultPoint[1] };
3878
                                }
3879

    
3880
                                if (bCalcY)
3881
                                {
3882
                                    double nextY = targetVertices[i + 1][1];
3883
                                    double newY = 0;
3884
                                    if (nextY > tempY)
3885
                                    {
3886
                                        newY = tempY + gridSetting.Length;
3887
                                        if (newY > nextY)
3888
                                            newY = (point[1] + nextY) / 2;
3889
                                    }
3890
                                    else
3891
                                    {
3892
                                        newY = tempY - gridSetting.Length;
3893
                                        if (newY < nextY)
3894
                                            newY = (point[1] + nextY) / 2;
3895
                                    }
3896
                                    resultPoint = new double[] { resultPoint[0], newY };
3897
                                }
3898
                            }
3899
                        }
3900
                        else if (i == targetVertices.Count - 1)
3901
                        {
3902
                            LMSymbol connSymbol = targetConnector.ConnectItem2SymbolObject;
3903
                            bool containZeroLength = false;
3904
                            if (connSymbol != null)
3905
                            {
3906
                                foreach (LMConnector connector in connSymbol.Connect1Connectors)
3907
                                {
3908
                                    if (connector.get_ItemStatus() == "Active" && Convert.ToBoolean(connector.get_IsZeroLength()) == true)
3909
                                        containZeroLength = true;
3910
                                }
3911
                                foreach (LMConnector connector in connSymbol.Connect2Connectors)
3912
                                {
3913
                                    if (connector.get_ItemStatus() == "Active" && Convert.ToBoolean(connector.get_IsZeroLength()) == true)
3914
                                        containZeroLength = true;
3915
                                }
3916
                            }
3917

    
3918
                            if (connSymbol == null ||
3919
                                 (connSymbol != null && connSymbol.get_ItemStatus() == "Active" && connSymbol.get_RepresentationType() != "Branch") ||
3920
                                containZeroLength)
3921
                            {
3922
                                bool bCalcX = false;
3923
                                bool bCalcY = false;
3924
                                if (targetLine.SlopeType == SlopeType.HORIZONTAL)
3925
                                    bCalcX = true;
3926
                                else if (targetLine.SlopeType == SlopeType.VERTICAL)
3927
                                    bCalcY = true;
3928
                                else
3929
                                {
3930
                                    bCalcX = true;
3931
                                    bCalcY = true;
3932
                                }
3933

    
3934
                                if (bCalcX)
3935
                                {
3936
                                    double nextX = targetVertices[i - 1][0];
3937
                                    double newX = 0;
3938
                                    if (nextX > tempX)
3939
                                    {
3940
                                        newX = tempX + gridSetting.Length;
3941
                                        if (newX > nextX)
3942
                                            newX = (point[0] + nextX) / 2;
3943
                                    }
3944
                                    else
3945
                                    {
3946
                                        newX = tempX - gridSetting.Length;
3947
                                        if (newX < nextX)
3948
                                            newX = (point[0] + nextX) / 2;
3949
                                    }
3950
                                    resultPoint = new double[] { newX, resultPoint[1] };
3951
                                }
3952

    
3953
                                if (bCalcY)
3954
                                {
3955
                                    double nextY = targetVertices[i - 1][1];
3956
                                    double newY = 0;
3957
                                    if (nextY > tempY)
3958
                                    {
3959
                                        newY = tempY + gridSetting.Length;
3960
                                        if (newY > nextY)
3961
                                            newY = (point[1] + nextY) / 2;
3962
                                    }
3963
                                    else
3964
                                    {
3965
                                        newY = tempY - gridSetting.Length;
3966
                                        if (newY < nextY)
3967
                                            newY = (point[1] + nextY) / 2;
3968
                                    }
3969
                                    resultPoint = new double[] { resultPoint[0], newY };
3970
                                }
3971
                            }
3972
                        }
3973
                        break;
3974
                    }
3975
                }
3976
            }
3977

    
3978
            x = resultPoint[0];
3979
            y = resultPoint[1];
3980

    
3981
            return targetConnector;
3982
        }
3983

    
3984
        private LMConnector GetLMConnectorOnlyOne(string modelItemID)
3985
        {
3986
            LMConnector result = null;
3987
            List<LMConnector> connectors = new List<LMConnector>();
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" && rep.Attributes["ItemStatus"].get_Value() == "Active")
3995
                        connectors.Add(dataSource.GetConnector(rep.Id));
3996
                }
3997

    
3998
                ReleaseCOMObjects(modelItem);
3999
            }
4000

    
4001
            if (connectors.Count == 1)
4002
                result = connectors[0];
4003
            else
4004
                foreach (var item in connectors)
4005
                    ReleaseCOMObjects(item);
4006

    
4007
            return result;
4008
        }
4009

    
4010
        private LMConnector GetLMConnectorFirst(string modelItemID)
4011
        {
4012
            LMConnector result = null;
4013
            LMModelItem modelItem = dataSource.GetModelItem(modelItemID);
4014

    
4015
            if (modelItem != null)
4016
            {
4017
                foreach (LMRepresentation rep in modelItem.Representations)
4018
                {
4019
                    if (rep.Attributes["RepresentationType"].get_Value() == "Connector" && 
4020
                        rep.Attributes["ItemStatus"].get_Value() == "Active")
4021
                    {
4022
                        LMConnector connector = dataSource.GetConnector(rep.Id);
4023
                        if (!Convert.ToBoolean(connector.get_IsZeroLength()))
4024
                        {
4025
                            result = connector;
4026
                            break;
4027
                        }
4028
                        else
4029
                        {
4030
                            ReleaseCOMObjects(connector);
4031
                            connector = null;
4032
                        }
4033
                    }
4034
                }
4035
                ReleaseCOMObjects(modelItem);
4036
                modelItem = null;
4037
            }
4038

    
4039
            return result;
4040
        }
4041

    
4042
        private int GetConnectorCount(string modelItemID)
4043
        {
4044
            LMModelItem modelItem = dataSource.GetModelItem(modelItemID);
4045
            int result = 0;
4046
            if (modelItem != null)
4047
            {
4048
                foreach (LMRepresentation rep in modelItem.Representations)
4049
                {
4050
                    if (rep.Attributes["RepresentationType"].get_Value() == "Connector" && rep.Attributes["ItemStatus"].get_Value() == "Active")
4051
                        result++;
4052
                    ReleaseCOMObjects(rep);
4053
                }
4054
                ReleaseCOMObjects(modelItem);
4055
            }
4056

    
4057
            return result;
4058
        }
4059

    
4060
        public List<string> GetRepresentations(string modelItemID)
4061
        {
4062
            List<string> result = new List<string>(); ;
4063
            LMModelItem modelItem = dataSource.GetModelItem(modelItemID);
4064
            if (modelItem != null)
4065
            {
4066
                foreach (LMRepresentation rep in modelItem.Representations)
4067
                {
4068
                    if (rep.Attributes["RepresentationType"].get_Value() == "Connector" && rep.Attributes["ItemStatus"].get_Value() == "Active")
4069
                        result.Add(rep.Id);
4070
                }
4071
                ReleaseCOMObjects(modelItem);
4072
            }
4073

    
4074
            return result;
4075
        }
4076

    
4077
        /// <summary>
4078
        /// Line Number Symbol을 실제로 Modeling하는 메서드
4079
        /// </summary>
4080
        /// <param name="lineNumber"></param>
4081
        private void LineNumberModelingOnlyOne(Line line)
4082
        {
4083
            LineNumber lineNumber = document.LINENUMBERS.Find(x => x.CONNLINE == line.UID);
4084
            if (lineNumber != null)
4085
            {
4086
                LMConnector connectedLMConnector = GetLMConnectorOnlyOne(line.SPPID.ModelItemId);
4087
                if (connectedLMConnector != null)
4088
                {
4089
                    double x = 0;
4090
                    double y = 0;
4091
                    CalcLabelLocation(ref x, ref y, lineNumber.SPPID.ORIGINAL_X, lineNumber.SPPID.ORIGINAL_Y, lineNumber.SPPIDLabelLocation, _ETCSetting.LineNumberLocation);
4092

    
4093
                    Array points = new double[] { 0, x, y };
4094
                    lineNumber.SPPID.SPPID_X = x;
4095
                    lineNumber.SPPID.SPPID_Y = y;
4096
                    LMLabelPersist _LmLabelPresist = _placement.PIDPlaceLabel(lineNumber.SPPID.MAPPINGNAME, ref points, Rotation: lineNumber.ANGLE, LabeledItem: connectedLMConnector.AsLMRepresentation(), IsLeaderVisible: false);
4097

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

    
4108
        private void LineNumberModeling(LineNumber lineNumber)
4109
        {
4110
            Line line = SPPIDUtil.FindObjectByUID(document, lineNumber.CONNLINE) as Line;
4111
            if (line != null)
4112
            {
4113
                double x = 0;
4114
                double y = 0;
4115
                CalcLabelLocation(ref x, ref y, lineNumber.SPPID.ORIGINAL_X, lineNumber.SPPID.ORIGINAL_Y, lineNumber.SPPIDLabelLocation, _ETCSetting.LineNumberLocation);
4116

    
4117
                Dictionary<LMConnector, List<double[]>> connectorVertices = GetPipeRunVertices(line.SPPID.ModelItemId);
4118
                LMConnector connectedLMConnector = FindTargetLMConnectorForLabel(connectorVertices, x, y);
4119
                if (connectedLMConnector != null)
4120
                {
4121
                    Array points = new double[] { 0, x, y };
4122
                    lineNumber.SPPID.SPPID_X = x;
4123
                    lineNumber.SPPID.SPPID_Y = y;
4124
                    LMLabelPersist _LmLabelPresist = _placement.PIDPlaceLabel(lineNumber.SPPID.MAPPINGNAME, ref points, Rotation: lineNumber.ANGLE, LabeledItem: connectedLMConnector.AsLMRepresentation(), IsLeaderVisible: false);
4125

    
4126
                    if (_LmLabelPresist != null)
4127
                    {
4128
                        _LmLabelPresist.Commit();
4129
                        lineNumber.SPPID.RepresentationId = _LmLabelPresist.AsLMRepresentation().Id;
4130
                        ReleaseCOMObjects(_LmLabelPresist);
4131
                    }
4132
                }
4133

    
4134
                foreach (var item in connectorVertices)
4135
                    ReleaseCOMObjects(item.Key);
4136
            }
4137
        }
4138
        private void LineNumberCorrectModeling(LineNumber lineNumber)
4139
        {
4140
            Line line = SPPIDUtil.FindObjectByUID(document, lineNumber.CONNLINE) as Line;
4141
            if (line == null || line.SPPID.Vertices == null)
4142
                return;
4143

    
4144
            if (!string.IsNullOrEmpty(lineNumber.SPPID.RepresentationId))
4145
            {
4146
                LMLabelPersist removeLabel = dataSource.GetLabelPersist(lineNumber.SPPID.RepresentationId);
4147
                if (removeLabel != null)
4148
                {
4149
                    GridSetting gridSetting = GridSetting.GetInstance();
4150
                    double[] labelRange = null;
4151
                    GetSPPIDSymbolRange(removeLabel, ref labelRange);
4152
                    LMConnector connector = dataSource.GetConnector(removeLabel.RepresentationID);
4153
                    List<double[]> vertices = GetConnectorVertices(connector);
4154

    
4155
                    double[] resultStart = null;
4156
                    double[] resultEnd = null;
4157
                    double distance = double.MaxValue;
4158
                    for (int i = 0; i < vertices.Count - 1; i++)
4159
                    {
4160
                        double[] startPoint = vertices[i];
4161
                        double[] endPoint = vertices[i + 1];
4162
                        foreach (var item in line.SPPID.Vertices)
4163
                        {
4164
                            double[] lineStartPoint = item[0];
4165
                            double[] lineEndPoint = item[item.Count - 1];
4166

    
4167
                            double tempDistance = SPPIDUtil.CalcPointToPointdDistance(startPoint[0], startPoint[1], lineStartPoint[0], lineStartPoint[1]) +
4168
                                SPPIDUtil.CalcPointToPointdDistance(endPoint[0], endPoint[1], lineEndPoint[0], lineEndPoint[1]);
4169
                            if (tempDistance < distance)
4170
                            {
4171
                                distance = tempDistance;
4172
                                resultStart = startPoint;
4173
                                resultEnd = endPoint;
4174
                            }
4175
                            tempDistance = SPPIDUtil.CalcPointToPointdDistance(startPoint[0], startPoint[1], lineEndPoint[0], lineEndPoint[1]) +
4176
                                SPPIDUtil.CalcPointToPointdDistance(endPoint[0], endPoint[1], lineStartPoint[0], lineStartPoint[1]);
4177
                            if (tempDistance < distance)
4178
                            {
4179
                                distance = tempDistance;
4180
                                resultStart = startPoint;
4181
                                resultEnd = endPoint;
4182
                            }
4183
                        }
4184
                    }
4185

    
4186
                    if (resultStart != null && resultEnd != null)
4187
                    {
4188
                        SlopeType slope = SPPIDUtil.CalcSlope(resultStart[0], resultStart[1], resultEnd[0], resultEnd[1]);
4189
                        double lineStartX = 0;
4190
                        double lineStartY = 0;
4191
                        double lineEndX = 0;
4192
                        double lineEndY = 0;
4193
                        double lineNumberX = 0;
4194
                        double lineNumberY = 0;
4195
                        SPPIDUtil.ConvertPointBystring(line.STARTPOINT, ref lineStartX, ref lineStartY);
4196
                        SPPIDUtil.ConvertPointBystring(line.ENDPOINT, ref lineEndX, ref lineEndY);
4197

    
4198
                        double lineX = (lineStartX + lineEndX) / 2;
4199
                        double lineY = (lineStartY + lineEndY) / 2;
4200
                        lineNumberX = (lineNumber.X1 + lineNumber.X2) / 2;
4201
                        lineNumberY = (lineNumber.Y1 + lineNumber.Y2) / 2;
4202

    
4203
                        double SPPIDCenterX = (resultStart[0] + resultEnd[0]) / 2;
4204
                        double SPPIDCenterY = (resultStart[1] + resultEnd[1]) / 2;
4205
                        double labelCenterX = (labelRange[0] + labelRange[2]) / 2;
4206
                        double labelCenterY = (labelRange[1] + labelRange[3]) / 2;
4207

    
4208
                        double offsetX = 0;
4209
                        double offsetY = 0;
4210
                        if (slope == SlopeType.HORIZONTAL)
4211
                        {
4212
                            // Line Number 아래
4213
                            if (lineY < lineNumberY)
4214
                            {
4215
                                offsetX = labelCenterX - SPPIDCenterX;
4216
                                offsetY = labelRange[3] - SPPIDCenterY + gridSetting.Length;
4217
                                MoveLineNumber(lineNumber, offsetX, offsetY);
4218
                            }
4219
                            // Line Number 위
4220
                            else
4221
                            {
4222
                                offsetX = labelCenterX - SPPIDCenterX;
4223
                                offsetY = labelRange[1] - SPPIDCenterY - gridSetting.Length;
4224
                                MoveLineNumber(lineNumber, offsetX, offsetY);
4225
                            }
4226
                        }
4227
                        else if (slope == SlopeType.VERTICAL)
4228
                        {
4229
                            // Line Number 오르쪽
4230
                            if (lineX < lineNumberX)
4231
                            {
4232
                                offsetX = labelRange[0] - SPPIDCenterX - gridSetting.Length;
4233
                                offsetY = labelCenterY - SPPIDCenterY;
4234
                                MoveLineNumber(lineNumber, offsetX, offsetY);
4235
                            }
4236
                            // Line Number 왼쪽
4237
                            else
4238
                            {
4239
                                offsetX = labelRange[2] - SPPIDCenterX + gridSetting.Length;
4240
                                offsetY = labelCenterY - SPPIDCenterY;
4241
                                MoveLineNumber(lineNumber, offsetX, offsetY);
4242
                            }
4243
                        }
4244

    
4245
                        if (offsetY != 0 && offsetY != 0)
4246
                        {
4247
                            _placement.PIDRemovePlacement(removeLabel.AsLMRepresentation());
4248
                            Array points = new double[] { 0, lineNumber.SPPID.SPPID_X, lineNumber.SPPID.SPPID_Y };
4249
                            LMLabelPersist _LmLabelPresist = _placement.PIDPlaceLabel(lineNumber.SPPID.MAPPINGNAME, ref points, Rotation: lineNumber.ANGLE, LabeledItem: connector.AsLMRepresentation(), IsLeaderVisible: false);
4250

    
4251
                            if (_LmLabelPresist != null)
4252
                            {
4253
                                _LmLabelPresist.Commit();
4254
                                lineNumber.SPPID.RepresentationId = _LmLabelPresist.AsLMRepresentation().Id;
4255
                            }
4256
                            ReleaseCOMObjects(_LmLabelPresist);
4257
                            _LmLabelPresist = null; 
4258
                        }
4259

    
4260
                        void MoveLineNumber(LineNumber moveLineNumber, double x, double y)
4261
                        {
4262
                            moveLineNumber.SPPID.SPPID_X = moveLineNumber.SPPID.SPPID_X - x;
4263
                            moveLineNumber.SPPID.SPPID_Y = moveLineNumber.SPPID.SPPID_Y - y;
4264
                        }
4265
                    }
4266

    
4267

    
4268
                    ReleaseCOMObjects(connector);
4269
                    connector = null;
4270
                }
4271

    
4272
                ReleaseCOMObjects(removeLabel);
4273
                removeLabel = null;
4274
            }
4275
        }
4276
        /// <summary>
4277
        /// Flow Mark Modeling
4278
        /// </summary>
4279
        /// <param name="line"></param>
4280
        private void FlowMarkModeling(Line line)
4281
        {
4282
            if (line.FLOWMARK && !string.IsNullOrEmpty(line.SPPID.ModelItemId) && !string.IsNullOrEmpty(_ETCSetting.FlowMarkSymbolPath))
4283
            {
4284
                LMConnector connector = GetLMConnectorOnlyOne(line.SPPID.ModelItemId);
4285
                if (connector != null)
4286
                {
4287
                    string mappingPath = _ETCSetting.FlowMarkSymbolPath;
4288
                    List<double[]> vertices = GetConnectorVertices(connector);
4289
                    vertices = vertices.FindAll(x => x[0] > 0 && x[1] > 0);
4290
                    double[] point = vertices[vertices.Count - 1];
4291
                    Array array = new double[] { 0, point[0], point[1] };
4292
                    LMLabelPersist _LMLabelPersist = _placement.PIDPlaceLabel(mappingPath, ref array, LabeledItem: connector.AsLMRepresentation());
4293
                    if (_LMLabelPersist != null)
4294
                    {
4295
                        _LMLabelPersist.Commit();
4296
                        FlowMarkRepIds.Add(_LMLabelPersist.Id);
4297
                        ReleaseCOMObjects(_LMLabelPersist);
4298
                    }
4299
                }
4300
            }
4301
        }
4302

    
4303
        /// <summary>
4304
        /// Line Number 기준으로 모든 Item에 Line Number의 Attribute Input
4305
        /// </summary>
4306
        /// <param name="lineNumber"></param>
4307
        private void InputLineNumberAttribute(LineNumber lineNumber, List<string> endLine)
4308
        {
4309
            foreach (LineRun run in lineNumber.RUNS)
4310
            {
4311
                foreach (var item in run.RUNITEMS)
4312
                {
4313
                    if (item.GetType() == typeof(Symbol))
4314
                    {
4315
                        Symbol symbol = item as Symbol;
4316
                        LMSymbol _LMSymbol = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
4317
                        if (_LMSymbol != null)
4318
                        {
4319
                            LMModelItem _LMModelItem = _LMSymbol.ModelItemObject;
4320

    
4321
                            if (_LMModelItem != null && _LMModelItem.get_ItemStatus() == "Active")
4322
                            {
4323
                                foreach (var attribute in lineNumber.ATTRIBUTES)
4324
                                {
4325
                                    LineNumberMapping mapping = document.LineNumberMappings.Find(x => x.UID == attribute.UID);
4326
                                    if (mapping != null && !string.IsNullOrEmpty(attribute.VALUE) && attribute.VALUE != "None")
4327
                                    {
4328
                                        LMAAttribute _LMAAttribute = _LMModelItem.Attributes[mapping.SPPIDATTRIBUTENAME];
4329
                                        if (_LMAAttribute != null)
4330
                                        {
4331
                                            if (DBNull.Value.Equals(_LMAAttribute.get_Value()))
4332
                                                _LMAAttribute.set_Value(attribute.VALUE);
4333
                                            else if (_LMAAttribute.get_Value() != attribute.VALUE)
4334
                                                _LMAAttribute.set_Value(attribute.VALUE);
4335
                                        }
4336
                                    }
4337
                                }
4338
                                _LMModelItem.Commit();
4339
                            }
4340
                            if (_LMModelItem != null)
4341
                                ReleaseCOMObjects(_LMModelItem);
4342
                        }
4343
                        if (_LMSymbol != null)
4344
                            ReleaseCOMObjects(_LMSymbol);
4345
                    }
4346
                    else if (item.GetType() == typeof(Line))
4347
                    {
4348
                        Line line = item as Line;
4349
                        if (line != null && !endLine.Contains(line.SPPID.ModelItemId))
4350
                        {
4351
                            LMModelItem _LMModelItem = dataSource.GetModelItem(line.SPPID.ModelItemId);
4352
                            if (_LMModelItem != null && _LMModelItem.get_ItemStatus() == "Active")
4353
                            {
4354
                                foreach (var attribute in lineNumber.ATTRIBUTES)
4355
                                {
4356
                                    LineNumberMapping mapping = document.LineNumberMappings.Find(x => x.UID == attribute.UID);
4357
                                    if (mapping != null && !string.IsNullOrEmpty(attribute.VALUE) && attribute.VALUE != "None")
4358
                                    {
4359
                                        LMAAttribute _LMAAttribute = _LMModelItem.Attributes[mapping.SPPIDATTRIBUTENAME];
4360
                                        if (_LMAAttribute != null)
4361
                                        {
4362
                                            if (DBNull.Value.Equals(_LMAAttribute.get_Value()))
4363
                                                _LMAAttribute.set_Value(attribute.VALUE);
4364
                                            else if (_LMAAttribute.get_Value() != attribute.VALUE)
4365
                                                _LMAAttribute.set_Value(attribute.VALUE);
4366

    
4367
                                        }
4368
                                    }
4369
                                }
4370
                                _LMModelItem.Commit();
4371
                            }
4372
                            if (_LMModelItem != null)
4373
                                ReleaseCOMObjects(_LMModelItem);
4374
                            endLine.Add(line.SPPID.ModelItemId);
4375
                        }
4376
                    }
4377
                }
4378
            }
4379
        }
4380

    
4381
        /// <summary>
4382
        /// Symbol Attribute 입력 메서드
4383
        /// </summary>
4384
        /// <param name="item"></param>
4385
        private void InputSymbolAttribute(object targetItem, List<BaseModel.Attribute> targetAttributes)
4386
        {
4387
            // Object 아이템이 Symbol일 경우 Equipment일 경우 
4388
            string sRep = null;
4389
            if (targetItem.GetType() == typeof(Symbol))
4390
                sRep = ((Symbol)targetItem).SPPID.RepresentationId;
4391
            else if (targetItem.GetType() == typeof(Equipment))
4392
                sRep = ((Equipment)targetItem).SPPID.RepresentationId;
4393

    
4394
            if (!string.IsNullOrEmpty(sRep))
4395
            {
4396
                LMSymbol _LMSymbol = dataSource.GetSymbol(sRep);
4397
                LMModelItem _LMModelItem = _LMSymbol.ModelItemObject;
4398
                LMAAttributes _Attributes = _LMModelItem.Attributes;
4399
                
4400
                foreach (var item in targetAttributes)
4401
                {
4402
                    AttributeMapping mapping = document.AttributeMappings.Find(x => x.UID == item.UID);
4403
                    if (mapping != null && !string.IsNullOrEmpty(item.VALUE) && item.VALUE != "None")
4404
                    {
4405
                        LMAAttribute _Attribute = _Attributes[mapping.SPPIDATTRIBUTENAME];
4406
                        if (_Attribute != null)
4407
                        {
4408
                            //object associItem = SPPIDUtil.FindObjectByUID(document, item.ASSOCITEM);
4409
                            //if (associItem != null)
4410
                            //{
4411
                            //    if (associItem.GetType() == typeof(Text))
4412
                            //    {
4413
                            //        Text text = associItem as Text;
4414
                            //        text.SPPID.RepresentationId = "Attribute";
4415
                            //    }
4416
                            //    else if (associItem.GetType() == typeof(Note))
4417
                            //    {
4418
                            //        Note note = associItem as Note;
4419
                            //        note.SPPID.RepresentationId = "Attribute";
4420
                            //    }
4421
                            //}
4422
                            _Attribute.set_Value(item.VALUE);
4423
                            // OPC 일경우 Attribute 저장
4424
                            if (targetItem.GetType() == typeof(Symbol))
4425
                            {
4426
                                Symbol symbol = targetItem as Symbol;
4427
                                if (symbol.TYPE == "Piping OPC's" || symbol.TYPE == "Instrument OPC's")
4428
                                    symbol.SPPID.Attributes.Add(new string[] { mapping.SPPIDATTRIBUTENAME, item.VALUE });
4429
                            }
4430
                        }
4431
                    }
4432
                }
4433
                _LMModelItem.Commit();
4434

    
4435
                ReleaseCOMObjects(_Attributes);
4436
                ReleaseCOMObjects(_LMModelItem);
4437
                ReleaseCOMObjects(_LMSymbol);
4438
            }
4439
        }
4440

    
4441
        /// <summary>
4442
        /// Input SpecBreak Attribute
4443
        /// </summary>
4444
        /// <param name="specBreak"></param>
4445
        private void InputSpecBreakAttribute(SpecBreak specBreak)
4446
        {
4447
            object upStreamObj = SPPIDUtil.FindObjectByUID(document, specBreak.UpStreamUID);
4448
            object downStreamObj = SPPIDUtil.FindObjectByUID(document, specBreak.DownStreamUID);
4449

    
4450
            if (upStreamObj != null &&
4451
                downStreamObj != null)
4452
            {
4453
                LMConnector targetLMConnector = FindBreakLineTarget(upStreamObj, downStreamObj);
4454

    
4455
                if (targetLMConnector != null)
4456
                {
4457
                    foreach (LMLabelPersist _LMLabelPersist in targetLMConnector.LabelPersists)
4458
                    {
4459
                        string symbolPath = _LMLabelPersist.get_FileName();
4460
                        AttributeMapping mapping = document.AttributeMappings.Find(x => x.SPPIDSYMBOLNAME == symbolPath);
4461
                        if (mapping != null)
4462
                        {
4463
                            BaseModel.Attribute attribute = specBreak.ATTRIBUTES.Find(y => y.UID == mapping.UID);
4464
                            if (attribute != null && !string.IsNullOrEmpty(attribute.VALUE) && attribute.VALUE != "None")
4465
                            {
4466
                                string[] values = attribute.VALUE.Split(new char[] { ',' });
4467
                                if (values.Length == 2)
4468
                                {
4469
                                    string upStreamValue = values[0];
4470
                                    string downStreamValue = values[1];
4471

    
4472
                                    InputAttributeForSpecBreak(upStreamObj, downStreamObj, upStreamValue, downStreamValue, mapping.SPPIDATTRIBUTENAME);
4473
                                }
4474
                            }
4475
                        }
4476
                    }
4477

    
4478
                    ReleaseCOMObjects(targetLMConnector);
4479
                }
4480
            }
4481

    
4482

    
4483
            #region 내부에서만 쓰는 메서드
4484
            void InputAttributeForSpecBreak(object _upStreamObj, object _downStreamObj, string upStreamValue, string downStreamValue, string sppidAttributeName)
4485
            {
4486
                Symbol upStreamSymbol = _upStreamObj as Symbol;
4487
                Line upStreamLine = _upStreamObj as Line;
4488
                Symbol downStreamSymbol = _downStreamObj as Symbol;
4489
                Line downStreamLine = _downStreamObj as Line;
4490
                // 둘다 Line일 경우
4491
                if (upStreamLine != null && downStreamLine != null)
4492
                {
4493
                    InputLineAttributeForSpecBreakLine(upStreamLine, sppidAttributeName, upStreamValue);
4494
                    InputLineAttributeForSpecBreakLine(downStreamLine, sppidAttributeName, downStreamValue);
4495
                }
4496
                // 둘다 Symbol일 경우
4497
                else if (upStreamSymbol != null && downStreamSymbol != null)
4498
                {
4499
                    LMConnector zeroLenthConnector = FindBreakLineTarget(upStreamSymbol, downStreamSymbol);
4500
                    LMSymbol upStreamLMSymbol = dataSource.GetSymbol(upStreamSymbol.SPPID.RepresentationId);
4501
                    LMSymbol downStreamLMSymbol = dataSource.GetSymbol(downStreamSymbol.SPPID.RepresentationId);
4502

    
4503
                    foreach (LMConnector connector in upStreamLMSymbol.Avoid1Connectors)
4504
                    {
4505
                        if (connector.get_ItemStatus() != "Active")
4506
                            continue;
4507

    
4508
                        if (connector.Id != zeroLenthConnector.Id)
4509
                            InputLineAttributeForSpecBreakLMConnector(connector, sppidAttributeName, upStreamValue);
4510
                    }
4511

    
4512
                    foreach (LMConnector connector in upStreamLMSymbol.Avoid2Connectors)
4513
                    {
4514
                        if (connector.get_ItemStatus() != "Active")
4515
                            continue;
4516

    
4517
                        if (connector.Id != zeroLenthConnector.Id)
4518
                            InputLineAttributeForSpecBreakLMConnector(connector, sppidAttributeName, upStreamValue);
4519
                    }
4520

    
4521
                    foreach (LMConnector connector in downStreamLMSymbol.Avoid1Connectors)
4522
                    {
4523
                        if (connector.get_ItemStatus() != "Active")
4524
                            continue;
4525

    
4526
                        if (connector.Id != zeroLenthConnector.Id)
4527
                            InputLineAttributeForSpecBreakLMConnector(connector, sppidAttributeName, downStreamValue);
4528
                    }
4529

    
4530
                    foreach (LMConnector connector in downStreamLMSymbol.Avoid2Connectors)
4531
                    {
4532
                        if (connector.get_ItemStatus() != "Active")
4533
                            continue;
4534

    
4535
                        if (connector.Id != zeroLenthConnector.Id)
4536
                            InputLineAttributeForSpecBreakLMConnector(connector, sppidAttributeName, downStreamValue);
4537
                    }
4538

    
4539
                    ReleaseCOMObjects(zeroLenthConnector);
4540
                    ReleaseCOMObjects(upStreamLMSymbol);
4541
                    ReleaseCOMObjects(downStreamLMSymbol);
4542
                }
4543
                else if (upStreamSymbol != null && downStreamLine != null)
4544
                {
4545
                    LMConnector zeroLenthConnector = FindBreakLineTarget(upStreamSymbol, downStreamLine);
4546
                    InputLineAttributeForSpecBreakLine(downStreamLine, sppidAttributeName, downStreamValue);
4547
                    LMSymbol upStreamLMSymbol = dataSource.GetSymbol(upStreamSymbol.SPPID.RepresentationId);
4548

    
4549
                    foreach (LMConnector connector in upStreamLMSymbol.Avoid1Connectors)
4550
                    {
4551
                        if (connector.get_ItemStatus() != "Active")
4552
                            continue;
4553

    
4554
                        if (connector.Id == zeroLenthConnector.Id)
4555
                            InputLineAttributeForSpecBreakLMConnector(connector, sppidAttributeName, upStreamValue);
4556
                    }
4557

    
4558
                    foreach (LMConnector connector in upStreamLMSymbol.Avoid2Connectors)
4559
                    {
4560
                        if (connector.get_ItemStatus() != "Active")
4561
                            continue;
4562

    
4563
                        if (connector.Id == zeroLenthConnector.Id)
4564
                            InputLineAttributeForSpecBreakLMConnector(connector, sppidAttributeName, upStreamValue);
4565
                    }
4566

    
4567
                    ReleaseCOMObjects(zeroLenthConnector);
4568
                    ReleaseCOMObjects(upStreamLMSymbol);
4569
                }
4570
                else if (upStreamLine != null && downStreamSymbol != null)
4571
                {
4572
                    LMConnector zeroLenthConnector = FindBreakLineTarget(upStreamLine, downStreamSymbol);
4573
                    InputLineAttributeForSpecBreakLine(upStreamLine, sppidAttributeName, upStreamValue);
4574
                    LMSymbol downStreamLMSymbol = dataSource.GetSymbol(downStreamSymbol.SPPID.RepresentationId);
4575

    
4576
                    foreach (LMConnector connector in downStreamLMSymbol.Avoid1Connectors)
4577
                    {
4578
                        if (connector.get_ItemStatus() != "Active")
4579
                            continue;
4580

    
4581
                        if (connector.Id == zeroLenthConnector.Id)
4582
                            InputLineAttributeForSpecBreakLMConnector(connector, sppidAttributeName, downStreamValue);
4583
                    }
4584

    
4585
                    foreach (LMConnector connector in downStreamLMSymbol.Avoid2Connectors)
4586
                    {
4587
                        if (connector.get_ItemStatus() != "Active")
4588
                            continue;
4589

    
4590
                        if (connector.Id == zeroLenthConnector.Id)
4591
                            InputLineAttributeForSpecBreakLMConnector(connector, sppidAttributeName, downStreamValue);
4592
                    }
4593

    
4594
                    ReleaseCOMObjects(zeroLenthConnector);
4595
                    ReleaseCOMObjects(downStreamLMSymbol);
4596
                }
4597
            }
4598

    
4599
            void InputLineAttributeForSpecBreakLine(Line line, string attrName, string value)
4600
            {
4601
                LMModelItem _LMModelItem = dataSource.GetModelItem(line.SPPID.ModelItemId);
4602
                if (_LMModelItem != null && _LMModelItem.get_ItemStatus() == "Active")
4603
                {
4604
                    LMAAttribute _LMAAttribute = _LMModelItem.Attributes[attrName];
4605
                    if (_LMAAttribute != null)
4606
                    {
4607
                        if (DBNull.Value.Equals(_LMAAttribute.get_Value()))
4608
                            _LMAAttribute.set_Value(value);
4609
                        else if (_LMAAttribute.get_Value() != value)
4610
                            _LMAAttribute.set_Value(value);
4611
                    }
4612

    
4613
                    _LMModelItem.Commit();
4614
                }
4615
                if (_LMModelItem != null)
4616
                    ReleaseCOMObjects(_LMModelItem);
4617
            }
4618

    
4619
            void InputLineAttributeForSpecBreakLMConnector(LMConnector connector, string attrName, string value)
4620
            {
4621
                LMModelItem _LMModelItem = dataSource.GetModelItem(connector.ModelItemID);
4622
                if (_LMModelItem != null && _LMModelItem.get_ItemStatus() == "Active")
4623
                {
4624
                    LMAAttribute _LMAAttribute = _LMModelItem.Attributes[attrName];
4625
                    if (_LMAAttribute != null)
4626
                    {
4627
                        if (DBNull.Value.Equals(_LMAAttribute.get_Value()))
4628
                            _LMAAttribute.set_Value(value);
4629
                        else if (_LMAAttribute.get_Value() != value)
4630
                            _LMAAttribute.set_Value(value);
4631
                    }
4632

    
4633
                    _LMModelItem.Commit();
4634
                }
4635
                if (_LMModelItem != null)
4636
                    ReleaseCOMObjects(_LMModelItem);
4637
            }
4638
            #endregion
4639
        }
4640

    
4641
        private void InputEndBreakAttribute(EndBreak endBreak)
4642
        {
4643
            object ownerObj = SPPIDUtil.FindObjectByUID(document, endBreak.OWNER);
4644
            object connectedItem = SPPIDUtil.FindObjectByUID(document, endBreak.PROPERTIES.Find(x => x.ATTRIBUTE == "Connected Item").VALUE);
4645

    
4646
            if ((ownerObj.GetType() == typeof(Symbol) && connectedItem.GetType() == typeof(Line)) ||
4647
                (ownerObj.GetType() == typeof(Line) && connectedItem.GetType() == typeof(Symbol)))
4648
            {
4649
                LMLabelPersist labelPersist = dataSource.GetLabelPersist(endBreak.SPPID.RepresentationId);
4650
                if (labelPersist != null)
4651
                {
4652
                    LMRepresentation representation = labelPersist.RepresentationObject;
4653
                    if (representation != null)
4654
                    {
4655
                        LMConnector connector = dataSource.GetConnector(representation.Id);
4656
                        LMModelItem ZeroLengthModelItem = connector.ModelItemObject;
4657
                        string modelItemID = connector.ModelItemID;
4658
                        if (Convert.ToBoolean(connector.get_IsZeroLength()))
4659
                        {
4660
                            List<string> modelItemIDs = new List<string>();
4661
                            if (connector.ConnectItem1SymbolObject != null && connector.ConnectItem1SymbolObject.get_RepresentationType() != "Branch")
4662
                            {
4663
                                LMSymbol symbol = connector.ConnectItem1SymbolObject;
4664
                                foreach (LMConnector item in symbol.Connect1Connectors)
4665
                                {
4666
                                    if (item.get_ItemStatus() == "Active" && item.ModelItemID != modelItemID)
4667
                                        modelItemIDs.Add(item.ModelItemID);
4668
                                    ReleaseCOMObjects(item);
4669
                                }
4670
                                foreach (LMConnector item in symbol.Connect2Connectors)
4671
                                {
4672
                                    if (item.get_ItemStatus() == "Active" && item.ModelItemID != modelItemID)
4673
                                        modelItemIDs.Add(item.ModelItemID);
4674
                                    ReleaseCOMObjects(item);
4675
                                }
4676
                                ReleaseCOMObjects(symbol);
4677
                                symbol = null;
4678
                            }
4679
                            else if (connector.ConnectItem2SymbolObject != null && connector.ConnectItem2SymbolObject.get_RepresentationType() != "Branch")
4680
                            {
4681
                                LMSymbol symbol = connector.ConnectItem2SymbolObject;
4682
                                foreach (LMConnector item in symbol.Connect1Connectors)
4683
                                {
4684
                                    if (item.get_ItemStatus() == "Active" && item.ModelItemID != modelItemID)
4685
                                        modelItemIDs.Add(item.ModelItemID);
4686
                                    ReleaseCOMObjects(item);
4687
                                }
4688
                                foreach (LMConnector item in symbol.Connect2Connectors)
4689
                                {
4690
                                    if (item.get_ItemStatus() == "Active" && item.ModelItemID != modelItemID)
4691
                                        modelItemIDs.Add(item.ModelItemID);
4692
                                    ReleaseCOMObjects(item);
4693
                                }
4694
                                ReleaseCOMObjects(symbol);
4695
                                symbol = null;
4696
                            }
4697

    
4698
                            modelItemIDs = modelItemIDs.Distinct().ToList();
4699
                            if (modelItemIDs.Count == 1)
4700
                            {
4701
                                LMModelItem modelItem = dataSource.GetModelItem(modelItemIDs[0]);
4702
                                LMConnector onlyOne = GetLMConnectorOnlyOne(modelItem.Id);
4703
                                if (onlyOne != null && Convert.ToBoolean(onlyOne.get_IsZeroLength()))
4704
                                {
4705
                                    bool result = false;
4706
                                    foreach (LMLabelPersist loop in onlyOne.LabelPersists)
4707
                                    {
4708
                                        if (document.EndBreaks.Find(x => x.SPPID.RepresentationId == loop.RepresentationID) != null)
4709
                                            result = true;
4710
                                        ReleaseCOMObjects(loop);
4711
                                    }
4712

    
4713
                                    if (result)
4714
                                    {
4715
                                        object value = modelItem.Attributes["TagSequenceNo"].get_Value();
4716
                                        ZeroLengthModelItem.Attributes["TagSequenceNo"].set_Value(value);
4717
                                        ZeroLengthModelItem.Commit();
4718
                                    }
4719
                                    else
4720
                                    {
4721
                                        List<string> loopModelItems = new List<string>();
4722
                                        if (onlyOne.ConnectItem1SymbolObject.get_RepresentationType() == "Branch")
4723
                                        {
4724
                                            LMSymbol _symbol = onlyOne.ConnectItem1SymbolObject;
4725
                                            foreach (LMConnector loop in _symbol.Connect1Connectors)
4726
                                            {
4727
                                                if (loop.get_ItemStatus() == "Active" && loop.ModelItemID != onlyOne.ModelItemID)
4728
                                                    loopModelItems.Add(loop.ModelItemID);
4729
                                                ReleaseCOMObjects(loop);
4730
                                            }
4731
                                            foreach (LMConnector loop in _symbol.Connect2Connectors)
4732
                                            {
4733
                                                if (loop.get_ItemStatus() == "Active" && loop.ModelItemID != onlyOne.ModelItemID)
4734
                                                    loopModelItems.Add(loop.ModelItemID);
4735
                                                ReleaseCOMObjects(loop);
4736
                                            }
4737
                                            ReleaseCOMObjects(_symbol);
4738
                                            _symbol = null;
4739
                                        }
4740
                                        else if (onlyOne.ConnectItem2SymbolObject.get_RepresentationType() == "Branch")
4741
                                        {
4742
                                            LMSymbol _symbol = onlyOne.ConnectItem2SymbolObject;
4743
                                            foreach (LMConnector loop in _symbol.Connect1Connectors)
4744
                                            {
4745
                                                if (loop.get_ItemStatus() == "Active" && loop.ModelItemID != onlyOne.ModelItemID)
4746
                                                    loopModelItems.Add(loop.ModelItemID);
4747
                                                ReleaseCOMObjects(loop);
4748
                                            }
4749
                                            foreach (LMConnector loop in _symbol.Connect2Connectors)
4750
                                            {
4751
                                                if (loop.get_ItemStatus() == "Active" && loop.ModelItemID != onlyOne.ModelItemID)
4752
                                                    loopModelItems.Add(loop.ModelItemID);
4753
                                                ReleaseCOMObjects(loop);
4754
                                            }
4755
                                            ReleaseCOMObjects(_symbol);
4756
                                            _symbol = null;
4757
                                        }
4758

    
4759
                                        loopModelItems = loopModelItems.Distinct().ToList();
4760
                                        if (loopModelItems.Count == 1)
4761
                                        {
4762
                                            LMModelItem loopModelItem = dataSource.GetModelItem(loopModelItems[0]);
4763
                                            object value = loopModelItem.Attributes["TagSequenceNo"].get_Value();
4764
                                            modelItem.Attributes["TagSequenceNo"].set_Value(value);
4765
                                            modelItem.Commit();
4766
                                            ZeroLengthModelItem.Attributes["TagSequenceNo"].set_Value(value);
4767
                                            ZeroLengthModelItem.Commit();
4768

    
4769
                                            ReleaseCOMObjects(loopModelItem);
4770
                                            loopModelItem = null;
4771
                                        }
4772
                                    }
4773
                                }
4774
                                else
4775
                                {
4776
                                    object value = modelItem.Attributes["TagSequenceNo"].get_Value();
4777
                                    ZeroLengthModelItem.Attributes["TagSequenceNo"].set_Value(value);
4778
                                    ZeroLengthModelItem.Commit();
4779
                                }
4780
                                ReleaseCOMObjects(modelItem);
4781
                                modelItem = null;
4782
                                ReleaseCOMObjects(onlyOne);
4783
                                onlyOne = null;
4784
                            }
4785
                        }
4786
                        ReleaseCOMObjects(connector);
4787
                        connector = null;
4788
                        ReleaseCOMObjects(ZeroLengthModelItem);
4789
                        ZeroLengthModelItem = null;
4790
                    }
4791
                    ReleaseCOMObjects(representation);
4792
                    representation = null;
4793
                }
4794
                ReleaseCOMObjects(labelPersist);
4795
                labelPersist = null;
4796
            }
4797
        }
4798

    
4799
        /// <summary>
4800
        /// Text Modeling - Association일 경우는 Text대신 해당 맵핑된 Symbol로 모델링
4801
        /// </summary>
4802
        /// <param name="text"></param>
4803
        private void NormalTextModeling(Text text)
4804
        {
4805
            LMSymbol _LMSymbol = null;
4806

    
4807
            LMItemNote _LMItemNote = null;
4808
            LMAAttribute _LMAAttribute = null;
4809

    
4810
            double x = 0;
4811
            double y = 0;
4812
            double angle = text.ANGLE;
4813
            CalcLabelLocation(ref x, ref y, text.SPPID.ORIGINAL_X, text.SPPID.ORIGINAL_Y, text.SPPIDLabelLocation, _ETCSetting.TextLocation);
4814

    
4815
            SPPIDUtil.ConvertGridPoint(ref x, ref y);
4816
            text.SPPID.SPPID_X = x;
4817
            text.SPPID.SPPID_Y = y;
4818

    
4819
            _LMSymbol = _placement.PIDPlaceSymbol(text.SPPID.MAPPINGNAME, x, y, Rotation: angle);
4820
            if (_LMSymbol != null)
4821
            {
4822
                _LMSymbol.Commit();
4823
                _LMItemNote = _placement.PIDDataSource.GetItemNote(_LMSymbol.ModelItemID);
4824
                if (_LMItemNote != null)
4825
                {
4826
                    _LMItemNote.Commit();
4827
                    _LMAAttribute = _LMItemNote.Attributes["Note.Body"];
4828
                    if (_LMAAttribute != null)
4829
                    {
4830
                        _LMAAttribute.set_Value(text.VALUE);
4831
                        text.SPPID.RepresentationId = _LMSymbol.AsLMRepresentation().Id;
4832
                        _LMItemNote.Commit();
4833

    
4834

    
4835
                        double[] range = null;
4836
                        foreach (LMLabelPersist labelPersist in _LMSymbol.LabelPersists)
4837
                        {
4838
                            double[] temp = null;
4839
                            GetSPPIDSymbolRange(labelPersist, ref temp);
4840
                            if (temp != null)
4841
                            {
4842
                                if (range == null)
4843
                                    range = temp;
4844
                                else
4845
                                {
4846
                                    range = new double[] {
4847
                                            Math.Min(range[0], temp[0]),
4848
                                            Math.Min(range[1], temp[1]),
4849
                                            Math.Max(range[2], temp[2]),
4850
                                            Math.Max(range[3], temp[3])
4851
                                        };
4852
                                }
4853
                            }
4854
                        }
4855
                        text.SPPID.Range = range;
4856

    
4857
                        if (_LMAAttribute != null)
4858
                            ReleaseCOMObjects(_LMAAttribute);
4859
                        if (_LMItemNote != null)
4860
                            ReleaseCOMObjects(_LMItemNote);
4861
                    }
4862

    
4863
                    TextCorrectModeling(text);
4864
                }
4865
            }
4866
            if (_LMSymbol != null)
4867
                ReleaseCOMObjects(_LMSymbol);
4868
        }
4869

    
4870
        private void AssociationTextModeling(Text text)
4871
        {
4872
            LMSymbol _LMSymbol = null;
4873
            LMConnector connectedLMConnector = null;
4874
            object owner = SPPIDUtil.FindObjectByUID(document, text.OWNER);
4875
            if (owner != null && owner.GetType() == typeof(Symbol))
4876
            {
4877
                Symbol symbol = owner as Symbol;
4878
                _LMSymbol = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
4879
                if (_LMSymbol != null)
4880
                {
4881
                    BaseModel.Attribute attribute = symbol.ATTRIBUTES.Find(x => x.ASSOCITEM == text.UID);
4882
                    if (attribute != null && !string.IsNullOrEmpty(attribute.VALUE) && attribute.VALUE != "None")
4883
                    {
4884
                        AttributeMapping mapping = document.AttributeMappings.Find(x => x.UID == attribute.UID && !string.IsNullOrEmpty(x.SPPIDSYMBOLNAME));
4885

    
4886
                        if (mapping != null)
4887
                        {
4888
                            double x = 0;
4889
                            double y = 0;
4890

    
4891
                            CalcLabelLocation(ref x, ref y, text.SPPID.ORIGINAL_X, text.SPPID.ORIGINAL_Y, text.SPPIDLabelLocation, mapping.Location);
4892
                            SPPIDUtil.ConvertGridPoint(ref x, ref y);
4893
                            Array array = new double[] { 0, x, y };
4894
                            text.SPPID.SPPID_X = x;
4895
                            text.SPPID.SPPID_Y = y;
4896
                            LMLabelPersist _LMLabelPersist = _placement.PIDPlaceLabel(mapping.SPPIDSYMBOLNAME, ref array, Rotation: text.ANGLE, LabeledItem: _LMSymbol.AsLMRepresentation(), IsLeaderVisible: mapping.LeaderLine);
4897
                            if (_LMLabelPersist != null)
4898
                            {
4899
                                text.SPPID.RepresentationId = _LMLabelPersist.AsLMRepresentation().Id;
4900
                                _LMLabelPersist.Commit();
4901
                                ReleaseCOMObjects(_LMLabelPersist);
4902
                            }
4903
                        }
4904
                    }
4905
                }
4906
            }
4907
            else if (owner != null && owner.GetType() == typeof(Line))
4908
            {
4909
                Line line = owner as Line;
4910
                Dictionary<LMConnector, List<double[]>> connectorVertices = GetPipeRunVertices(line.SPPID.ModelItemId);
4911
                connectedLMConnector = FindTargetLMConnectorForLabel(connectorVertices, text.SPPID.ORIGINAL_X, text.SPPID.ORIGINAL_Y);
4912

    
4913
                if (connectedLMConnector != null)
4914
                {
4915
                    BaseModel.Attribute attribute = line.ATTRIBUTES.Find(x => x.ASSOCITEM == text.UID);
4916
                    if (attribute != null && !string.IsNullOrEmpty(attribute.VALUE) && attribute.VALUE != "None")
4917
                    {
4918
                        AttributeMapping mapping = document.AttributeMappings.Find(x => x.UID == attribute.UID && !string.IsNullOrEmpty(x.SPPIDSYMBOLNAME));
4919

    
4920
                        if (mapping != null)
4921
                        {
4922
                            double x = 0;
4923
                            double y = 0;
4924
                            CalcLabelLocation(ref x, ref y, text.SPPID.ORIGINAL_X, text.SPPID.ORIGINAL_Y, text.SPPIDLabelLocation, mapping.Location);
4925
                            SPPIDUtil.ConvertGridPoint(ref x, ref y);
4926
                            Array array = new double[] { 0, x, y };
4927

    
4928
                            LMLabelPersist _LMLabelPersist = _placement.PIDPlaceLabel(mapping.SPPIDSYMBOLNAME, ref array, Rotation: text.ANGLE, LabeledItem: connectedLMConnector.AsLMRepresentation(), IsLeaderVisible: mapping.LeaderLine);
4929
                            if (_LMLabelPersist != null)
4930
                            {
4931
                                text.SPPID.RepresentationId = _LMLabelPersist.AsLMRepresentation().Id;
4932
                                _LMLabelPersist.Commit();
4933
                                ReleaseCOMObjects(_LMLabelPersist);
4934
                            }
4935
                        }
4936
                    }
4937
                }
4938
            }
4939
            if (_LMSymbol != null)
4940
                ReleaseCOMObjects(_LMSymbol);
4941
        }
4942

    
4943
        private void TextCorrectModeling(Text text)
4944
        {
4945
            if (text.SPPID.Range == null)
4946
                return;
4947

    
4948
            bool needRemodeling = false;
4949
            bool loop = true;
4950
            GridSetting gridSetting = GridSetting.GetInstance();
4951
            while (loop)
4952
            {
4953
                loop = false;
4954
                foreach (var overlapText in document.TEXTINFOS)
4955
                {
4956
                    if (overlapText.ASSOCIATION || overlapText == text || overlapText.SPPID.Range == null)
4957
                        continue;
4958

    
4959
                    if (SPPIDUtil.IsOverlap(overlapText.SPPID.Range, text.SPPID.Range))
4960
                    {
4961
                        double percentX = 0;
4962
                        double percentY = 0;
4963
                        if (overlapText.X1 <= text.X2 && overlapText.X2 >= text.X1)
4964
                        {
4965
                            double gapX = Math.Min(overlapText.X2, text.X2) - Math.Max(overlapText.X1, text.X1);
4966
                            percentX = Math.Max(gapX / (overlapText.X2 - overlapText.X1), gapX / (text.X2 - text.X1));
4967
                        }
4968
                        if (overlapText.Y1 <= text.Y2 && overlapText.Y2 >= text.Y1)
4969
                        {
4970
                            double gapY = Math.Min(overlapText.Y2, text.Y2) - Math.Max(overlapText.Y1, text.Y1);
4971
                            percentY = Math.Max(gapY / (overlapText.Y2 - overlapText.Y1), gapY / (text.Y2 - text.Y1));
4972
                        }
4973

    
4974
                        double tempX = 0;
4975
                        double tempY = 0;
4976
                        bool overlapX = false;
4977
                        bool overlapY = false;
4978
                        SPPIDUtil.CalcOverlap(text.SPPID.Range, overlapText.SPPID.Range, ref tempX, ref tempY, ref overlapX, ref overlapY);
4979
                        if (percentX >= percentY)
4980
                        {
4981
                            int count = Convert.ToInt32(tempY / gridSetting.Length) + 1;
4982
                            double move = gridSetting.Length * count;
4983
                            text.SPPID.SPPID_Y = text.SPPID.SPPID_Y - move;
4984
                            text.SPPID.Range = new double[] { text.SPPID.Range[0], text.SPPID.Range[1] - move, text.SPPID.Range[2], text.SPPID.Range[3] - move };
4985
                            needRemodeling = true;
4986
                            loop = true;
4987
                        }
4988
                        else
4989
                        {
4990
                            int count = Convert.ToInt32(tempX / gridSetting.Length) + 1;
4991
                            double move = gridSetting.Length * count;
4992
                            text.SPPID.SPPID_X = text.SPPID.SPPID_X + move;
4993
                            text.SPPID.Range = new double[] { text.SPPID.Range[0] + move, text.SPPID.Range[1], text.SPPID.Range[2] + move, text.SPPID.Range[3] };
4994
                            needRemodeling = true;
4995
                            loop = true;
4996
                        }
4997
                    }
4998
                }
4999
            }
5000
            
5001

    
5002
            if (needRemodeling)
5003
            {
5004
                LMSymbol symbol = dataSource.GetSymbol(text.SPPID.RepresentationId);
5005
                _placement.PIDRemovePlacement(symbol.AsLMRepresentation());
5006
                text.SPPID.RepresentationId = null;
5007

    
5008
                LMItemNote _LMItemNote = null;
5009
                LMAAttribute _LMAAttribute = null;
5010
                LMSymbol _LMSymbol = _placement.PIDPlaceSymbol(text.SPPID.MAPPINGNAME, text.SPPID.SPPID_X, text.SPPID.SPPID_Y, Rotation: text.ANGLE);
5011
                if (_LMSymbol != null)
5012
                {
5013
                    _LMSymbol.Commit();
5014
                    _LMItemNote = _placement.PIDDataSource.GetItemNote(_LMSymbol.ModelItemID);
5015
                    if (_LMItemNote != null)
5016
                    {
5017
                        _LMItemNote.Commit();
5018
                        _LMAAttribute = _LMItemNote.Attributes["Note.Body"];
5019
                        if (_LMAAttribute != null)
5020
                        {
5021
                            _LMAAttribute.set_Value(text.VALUE);
5022
                            text.SPPID.RepresentationId = _LMSymbol.AsLMRepresentation().Id;
5023
                            _LMItemNote.Commit();
5024

    
5025
                            ReleaseCOMObjects(_LMAAttribute);
5026
                            ReleaseCOMObjects(_LMItemNote);
5027
                        }
5028
                    }
5029
                }
5030

    
5031
                ReleaseCOMObjects(symbol);
5032
                symbol = null;
5033
                ReleaseCOMObjects(_LMItemNote);
5034
                _LMItemNote = null;
5035
                ReleaseCOMObjects(_LMAAttribute);
5036
                _LMAAttribute = null;
5037
                ReleaseCOMObjects(_LMSymbol);
5038
                _LMSymbol = null;
5039
            }
5040
        }
5041

    
5042
        private void AssociationTextCorrectModeling(Text text, List<Text> endTexts)
5043
        {
5044
            if (!string.IsNullOrEmpty(text.SPPID.RepresentationId))
5045
            {
5046
                List<Text> texts = new List<Text>();
5047
                LMLabelPersist targetLabel = dataSource.GetLabelPersist(text.SPPID.RepresentationId);
5048
                LMRepresentation representation = targetLabel.RepresentationObject;
5049
                Symbol symbol = document.SYMBOLS.Find(x => x.SPPID.RepresentationId == representation.Id);
5050
                if (targetLabel.RepresentationObject != null && symbol != null)
5051
                {
5052
                    double[] symbolRange = null;
5053
                    GetSPPIDSymbolRange(symbol, ref symbolRange, true, true);
5054
                    if (symbolRange != null)
5055
                    {
5056
                        foreach (LMLabelPersist labelPersist in representation.LabelPersists)
5057
                        {
5058
                            Text findText = document.TEXTINFOS.Find(x => x.SPPID.RepresentationId == labelPersist.AsLMRepresentation().Id && x.ASSOCIATION);
5059
                            if (findText != null)
5060
                            {
5061
                                double[] range = null;
5062
                                GetSPPIDSymbolRange(labelPersist, ref range);
5063
                                findText.SPPID.Range = range;
5064
                                texts.Add(findText);
5065
                            }
5066

    
5067
                            ReleaseCOMObjects(labelPersist);
5068
                        }
5069

    
5070
                        if (texts.Count > 0)
5071
                        {
5072
                            #region Sort Text By Y
5073
                            texts.Sort(SortTextByY);
5074
                            int SortTextByY(Text a, Text b)
5075
                            {
5076
                                return b.SPPID.Range[3].CompareTo(a.SPPID.Range[3]);
5077
                            }
5078
                            #endregion
5079

    
5080
                            #region 첫번째 Text로 기준 맞춤
5081
                            for (int i = 0; i < texts.Count; i++)
5082
                            {
5083
                                if (i != 0)
5084
                                {
5085
                                    Text currentText = texts[i];
5086
                                    Text prevText = texts[i - 1];
5087
                                    double minY = prevText.SPPID.Range[1];
5088
                                    double centerPrevX = (prevText.SPPID.Range[0] + prevText.SPPID.Range[2]) / 2;
5089
                                    double centerX = (currentText.SPPID.Range[0] + currentText.SPPID.Range[2]) / 2;
5090
                                    double _gapX = centerX - centerPrevX;
5091
                                    double _gapY = currentText.SPPID.Range[3] - minY;
5092
                                    MoveText(currentText, _gapX, _gapY);
5093
                                }
5094
                            }
5095
                            List<double> rangeMinX = texts.Select(loopX => loopX.SPPID.Range[0]).ToList();
5096
                            List<double> rangeMinY = texts.Select(loopX => loopX.SPPID.Range[1]).ToList();
5097
                            List<double> rangeMaxX = texts.Select(loopX => loopX.SPPID.Range[2]).ToList();
5098
                            List<double> rangeMaxY = texts.Select(loopX => loopX.SPPID.Range[3]).ToList();
5099
                            rangeMinX.Sort();
5100
                            rangeMinY.Sort();
5101
                            rangeMaxX.Sort();
5102
                            rangeMaxY.Sort();
5103
                            double allTextCenterX = (rangeMinX[0] + rangeMaxX[rangeMaxX.Count - 1]) / 2;
5104
                            double allTextCenterY = (rangeMinY[0] + rangeMaxY[rangeMaxY.Count - 1]) / 2;
5105
                            #endregion
5106

    
5107
                            Text correctBySymbol = texts[0];
5108
                            double textCenterX = (text.X1 + text.X2) / 2;
5109
                            double textCenterY = (text.Y1 + text.Y2) / 2;
5110
                            double originX = 0;
5111
                            double originY = 0;
5112
                            SPPIDUtil.ConvertPointBystring(symbol.ORIGINALPOINT, ref originX, ref originY);
5113
                            double angle = SPPIDUtil.CalcAngle(textCenterX, textCenterY, originX, originY);
5114
                            double symbolCenterX = (symbolRange[0] + symbolRange[2]) / 2;
5115
                            double symbolCenterY = (symbolRange[1] + symbolRange[3]) / 2;
5116

    
5117
                            double gapX = 0;
5118
                            double gapY = 0;
5119
                            if (angle < 45)
5120
                            {
5121
                                // Text 오른쪽
5122
                                if (textCenterX > originX)
5123
                                {
5124
                                    gapX = rangeMinX[0] - symbolRange[2];
5125
                                    gapY = allTextCenterY - symbolCenterY;
5126
                                }
5127
                                // Text 왼쪽
5128
                                else
5129
                                {
5130
                                    gapX = rangeMaxX[rangeMaxX.Count - 1] - symbolRange[0];
5131
                                    gapY = allTextCenterY - symbolCenterY;
5132
                                }
5133
                            }
5134
                            else
5135
                            {
5136
                                // Text 아래쪽
5137
                                if (textCenterY > originY)
5138
                                {
5139
                                    gapX = allTextCenterX - symbolCenterX;
5140
                                    gapY = rangeMaxY[rangeMaxY.Count - 1] - symbolRange[1];
5141
                                }
5142
                                // Text 위쪽
5143
                                else
5144
                                {
5145
                                    gapX = allTextCenterX - symbolCenterX;
5146
                                    gapY = rangeMinY[0] - symbolRange[3];
5147
                                }
5148
                            }
5149

    
5150
                            foreach (var item in texts)
5151
                            {
5152
                                MoveText(item, gapX, gapY);
5153
                                RemodelingAssociationText(item);
5154
                            }
5155
                        }
5156
                    }
5157
                }
5158

    
5159
                void MoveText(Text moveText, double x, double y)
5160
                {
5161
                    moveText.SPPID.SPPID_X = moveText.SPPID.SPPID_X - x;
5162
                    moveText.SPPID.SPPID_Y = moveText.SPPID.SPPID_Y - y;
5163
                    moveText.SPPID.Range = new double[] {
5164
                        moveText.SPPID.Range[0] - x,
5165
                        moveText.SPPID.Range[1]- y,
5166
                        moveText.SPPID.Range[2]- x,
5167
                        moveText.SPPID.Range[3]- y
5168
                    };
5169
                }
5170

    
5171
                endTexts.AddRange(texts);
5172

    
5173
                ReleaseCOMObjects(targetLabel);
5174
                targetLabel = null;
5175
                ReleaseCOMObjects(representation);
5176
                representation = null;
5177
            }
5178
        }
5179

    
5180
        private void RemodelingAssociationText(Text text)
5181
        {
5182
            LMLabelPersist removeLabel = dataSource.GetLabelPersist(text.SPPID.RepresentationId);
5183
            _placement.PIDRemovePlacement(removeLabel.AsLMRepresentation());
5184
            removeLabel.Commit();
5185
            ReleaseCOMObjects(removeLabel);
5186
            removeLabel = null;
5187

    
5188
            object owner = SPPIDUtil.FindObjectByUID(document, text.OWNER);
5189
            if (owner != null && owner.GetType() == typeof(Symbol))
5190
            {
5191
                Symbol symbol = owner as Symbol;
5192
                _LMSymbol _LMSymbol = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
5193
                if (_LMSymbol != null)
5194
                {
5195
                    BaseModel.Attribute attribute = symbol.ATTRIBUTES.Find(x => x.ASSOCITEM == text.UID);
5196
                    if (attribute != null && !string.IsNullOrEmpty(attribute.VALUE) && attribute.VALUE != "None")
5197
                    {
5198
                        AttributeMapping mapping = document.AttributeMappings.Find(x => x.UID == attribute.UID && !string.IsNullOrEmpty(x.SPPIDSYMBOLNAME));
5199

    
5200
                        if (mapping != null)
5201
                        {
5202
                            double x = 0;
5203
                            double y = 0;
5204

    
5205
                            Array array = new double[] { 0, text.SPPID.SPPID_X, text.SPPID.SPPID_Y };
5206
                            LMLabelPersist _LMLabelPersist = _placement.PIDPlaceLabel(mapping.SPPIDSYMBOLNAME, ref array, Rotation: text.ANGLE, LabeledItem: _LMSymbol.AsLMRepresentation(), IsLeaderVisible: mapping.LeaderLine);
5207
                            if (_LMLabelPersist != null)
5208
                            {
5209
                                text.SPPID.RepresentationId = _LMLabelPersist.AsLMRepresentation().Id;
5210
                                _LMLabelPersist.Commit();
5211
                            }
5212
                            ReleaseCOMObjects(_LMLabelPersist);
5213
                            _LMLabelPersist = null;
5214
                        }
5215
                    }
5216
                }
5217
                ReleaseCOMObjects(_LMSymbol);
5218
                _LMSymbol = null;
5219
            }
5220
        }
5221

    
5222
        /// <summary>
5223
        /// Note Modeling
5224
        /// </summary>
5225
        /// <param name="note"></param>
5226
        private void NoteModeling(Note note, List<Note> correctList)
5227
        {
5228
            LMSymbol _LMSymbol = null;
5229
            LMItemNote _LMItemNote = null;
5230
            LMAAttribute _LMAAttribute = null;
5231

    
5232
            if (string.IsNullOrEmpty(note.OWNER) || note.OWNER == "None")
5233
            {
5234
                double x = 0;
5235
                double y = 0;
5236

    
5237
                CalcLabelLocation(ref x, ref y, note.SPPID.ORIGINAL_X, note.SPPID.ORIGINAL_Y, note.SPPIDLabelLocation, _ETCSetting.NoteLocation);
5238
                SPPIDUtil.ConvertGridPoint(ref x, ref y);
5239
                note.SPPID.SPPID_X = x;
5240
                note.SPPID.SPPID_Y = y;
5241

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

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

    
5280

    
5281
                            _LMItemNote.Commit();
5282
                        }
5283
                    }
5284
                }
5285
            }
5286

    
5287
            if (_LMAAttribute != null)
5288
                ReleaseCOMObjects(_LMAAttribute);
5289
            if (_LMItemNote != null)
5290
                ReleaseCOMObjects(_LMItemNote);
5291
            if (_LMSymbol != null)
5292
                ReleaseCOMObjects(_LMSymbol);
5293
        }
5294

    
5295
        private void NoteCorrectModeling(Note note, List<Note> endList)
5296
        {
5297
            bool needRemodeling = false;
5298
            bool loop = true;
5299
            GridSetting gridSetting = GridSetting.GetInstance();
5300
            while (loop)
5301
            {
5302
                loop = false;
5303
                foreach (var overlap in endList)
5304
                {
5305
                    if (SPPIDUtil.IsOverlap(overlap.SPPID.Range, note.SPPID.Range))
5306
                    {
5307
                        double tempX = 0;
5308
                        double tempY = 0;
5309
                        bool overlapX = false;
5310
                        bool overlapY = false;
5311
                        SPPIDUtil.CalcOverlap(note.SPPID.Range, overlap.SPPID.Range, ref tempX, ref tempY, ref overlapX, ref overlapY);
5312
                        double angle = SPPIDUtil.CalcAngle(note.LOCATION_X, note.LOCATION_Y, overlap.LOCATION_X, overlap.LOCATION_Y);
5313
                        if (overlapY && angle >= 45)
5314
                        {
5315
                            int count = Convert.ToInt32(tempY / gridSetting.Length) + 1;
5316
                            double move = gridSetting.Length * count;
5317
                            note.SPPID.SPPID_Y = note.SPPID.SPPID_Y - move;
5318
                            note.SPPID.Range = new double[] { note.SPPID.Range[0], note.SPPID.Range[1] - move, note.SPPID.Range[2], note.SPPID.Range[3] - move };
5319
                            needRemodeling = true;
5320
                            loop = true;
5321
                        }
5322
                        if (overlapX && angle <= 45)
5323
                        {
5324
                            int count = Convert.ToInt32(tempX / gridSetting.Length) + 1;
5325
                            double move = gridSetting.Length * count;
5326
                            note.SPPID.SPPID_X = note.SPPID.SPPID_X + move;
5327
                            note.SPPID.Range = new double[] { note.SPPID.Range[0] + move, note.SPPID.Range[1], note.SPPID.Range[2] + move, note.SPPID.Range[3] };
5328
                            needRemodeling = true;
5329
                            loop = true;
5330
                        }
5331
                    }
5332
                }
5333
            }
5334

    
5335

    
5336
            if (needRemodeling)
5337
            {
5338
                LMSymbol symbol = dataSource.GetSymbol(note.SPPID.RepresentationId);
5339
                _placement.PIDRemovePlacement(symbol.AsLMRepresentation());
5340
                note.SPPID.RepresentationId = null;
5341

    
5342
                LMItemNote _LMItemNote = null;
5343
                LMAAttribute _LMAAttribute = null;
5344
                LMSymbol _LMSymbol = _placement.PIDPlaceSymbol(note.SPPID.MAPPINGNAME, note.SPPID.SPPID_X, note.SPPID.SPPID_Y, Rotation: note.ANGLE);
5345
                if (_LMSymbol != null)
5346
                {
5347
                    _LMSymbol.Commit();
5348
                    _LMItemNote = _placement.PIDDataSource.GetItemNote(_LMSymbol.ModelItemID);
5349
                    if (_LMItemNote != null)
5350
                    {
5351
                        _LMItemNote.Commit();
5352
                        _LMAAttribute = _LMItemNote.Attributes["Note.Body"];
5353
                        if (_LMAAttribute != null)
5354
                        {
5355
                            _LMAAttribute.set_Value(note.VALUE);
5356
                            note.SPPID.RepresentationId = _LMSymbol.AsLMRepresentation().Id;
5357
                            _LMItemNote.Commit();
5358

    
5359
                            ReleaseCOMObjects(_LMAAttribute);
5360
                            ReleaseCOMObjects(_LMItemNote);
5361
                        }
5362
                    }
5363
                }
5364

    
5365
                ReleaseCOMObjects(symbol);
5366
                symbol = null;
5367
                ReleaseCOMObjects(_LMItemNote);
5368
                _LMItemNote = null;
5369
                ReleaseCOMObjects(_LMAAttribute);
5370
                _LMAAttribute = null;
5371
                ReleaseCOMObjects(_LMSymbol);
5372
                _LMSymbol = null;
5373
            }
5374

    
5375
            endList.Add(note);
5376
        }
5377

    
5378
        private void JoinRunBySameType(string modelItemId, ref string survivorId)
5379
        {
5380
            LMModelItem modelItem = dataSource.GetModelItem(modelItemId);
5381
            if (modelItem != null)
5382
            {
5383
                foreach (LMRepresentation rep in modelItem.Representations)
5384
                {
5385
                    if (rep.Attributes["RepresentationType"].get_Value() == "Connector" && rep.Attributes["ItemStatus"].get_Value() == "Active")
5386
                    {
5387
                        LMConnector connector = dataSource.GetConnector(rep.Id);
5388
                        if (connector.ConnectItem1SymbolObject != null && connector.ConnectItem1SymbolObject.get_RepresentationType() != "Branch")
5389
                        {
5390
                            LMSymbol symbol = connector.ConnectItem1SymbolObject;
5391
                            List<string> modelItemIds = FindOtherModelItemBySymbolWhereTypePipeRun(symbol, modelItem.Id);
5392
                            if (modelItemIds.Count == 1)
5393
                            {
5394
                                string joinModelItemId = modelItemIds[0];
5395
                                JoinRun(joinModelItemId, modelItemId, ref survivorId, false);
5396
                                if (survivorId != null)
5397
                                    break;
5398
                            }
5399
                        }
5400
                        if (connector.ConnectItem2SymbolObject != null && connector.ConnectItem2SymbolObject.get_RepresentationType() != "Branch")
5401
                        {
5402
                            LMSymbol symbol = connector.ConnectItem2SymbolObject;
5403
                            List<string> modelItemIds = FindOtherModelItemBySymbolWhereTypePipeRun(symbol, modelItem.Id);
5404
                            if (modelItemIds.Count == 1)
5405
                            {
5406
                                string joinModelItemId = modelItemIds[0];
5407
                                JoinRun(joinModelItemId, modelItemId, ref survivorId, false);
5408
                                if (survivorId != null)
5409
                                    break;
5410
                            }
5411
                        }
5412
                    }
5413
                }
5414
            }
5415
        }
5416

    
5417
        /// <summary>
5418
        /// Label의 좌표를 구하는 메서드(ID2 기준의 좌표 -> SPPID 좌표)
5419
        /// </summary>
5420
        /// <param name="x"></param>
5421
        /// <param name="y"></param>
5422
        /// <param name="originX"></param>
5423
        /// <param name="originY"></param>
5424
        /// <param name="SPPIDLabelLocation"></param>
5425
        /// <param name="location"></param>
5426
        private void CalcLabelLocation(ref double x, ref double y, double originX, double originY, SPPIDLabelLocationInfo SPPIDLabelLocation, Location location)
5427
        {
5428
            if (location == Location.None)
5429
            {
5430
                x = originX;
5431
                y = originY;
5432
            }
5433
            else
5434
            {
5435
                if (location.HasFlag(Location.Center))
5436
                {
5437
                    x = (SPPIDLabelLocation.X1 + SPPIDLabelLocation.X2) / 2;
5438
                    y = (SPPIDLabelLocation.Y1 + SPPIDLabelLocation.Y2) / 2;
5439
                }
5440

    
5441
                if (location.HasFlag(Location.Left))
5442
                    x = SPPIDLabelLocation.X1;
5443
                else if (location.HasFlag(Location.Right))
5444
                    x = SPPIDLabelLocation.X2;
5445

    
5446
                if (location.HasFlag(Location.Down))
5447
                    y = SPPIDLabelLocation.Y1;
5448
                else if (location.HasFlag(Location.Up))
5449
                    y = SPPIDLabelLocation.Y2;
5450
            }
5451
        }
5452

    
5453
        /// <summary>
5454
        /// Symbol의 우선순위 Modeling 목록을 가져온다.
5455
        /// 1. Angle Valve
5456
        /// 2. 3개로 이루어진 Symbol Group
5457
        /// </summary>
5458
        /// <returns></returns>
5459
        private List<Symbol> GetPrioritySymbol()
5460
        {
5461
            DataTable symbolTable = document.SymbolTable;
5462
            // List에 순서대로 쌓는다.
5463
            List<Symbol> symbols = new List<Symbol>();
5464

    
5465
            // Angle Valve 부터
5466
            foreach (var symbol in document.SYMBOLS.FindAll(x => x.CONNECTORS.FindAll(y => y.Index == 0).Count == 2))
5467
            {
5468
                if (!symbols.Contains(symbol))
5469
                {
5470
                    double originX = 0;
5471
                    double originY = 0;
5472

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

    
5477
                    SlopeType slopeType1 = SlopeType.None;
5478
                    SlopeType slopeType2 = SlopeType.None;
5479
                    foreach (Connector connector in symbol.CONNECTORS.FindAll(x => x.Index == 0))
5480
                    {
5481
                        double connectorX = 0;
5482
                        double connectorY = 0;
5483
                        SPPIDUtil.ConvertPointBystring(connector.CONNECTPOINT, ref connectorX, ref connectorY);
5484
                        if (slopeType1 == SlopeType.None)
5485
                            slopeType1 = SPPIDUtil.CalcSlope(originX, originY, connectorX, connectorY);
5486
                        else
5487
                            slopeType2 = SPPIDUtil.CalcSlope(originX, originY, connectorX, connectorY);
5488
                    }
5489

    
5490
                    if ((slopeType1 == SlopeType.VERTICAL && slopeType2 == SlopeType.HORIZONTAL) ||
5491
                        (slopeType2 == SlopeType.VERTICAL && slopeType1 == SlopeType.HORIZONTAL))
5492
                        symbols.Add(symbol);
5493
                }
5494
            }
5495

    
5496
            List<Symbol> tempSymbols = new List<Symbol>();
5497
            // Conn 갯수 기준
5498
            foreach (var item in document.SYMBOLS)
5499
            {
5500
                if (!symbols.Contains(item))
5501
                    tempSymbols.Add(item);
5502
            }
5503
            tempSymbols.Sort(SortSymbolPriority);
5504
            symbols.AddRange(tempSymbols);
5505

    
5506
            return symbols;
5507
        }
5508

    
5509
        private void SetPriorityLine(List<Line> lines)
5510
        {
5511
            lines.Sort(SortLinePriority);
5512

    
5513
            int SortLinePriority(Line a, Line b)
5514
            {
5515
                // Branch 없는것부터
5516
                int branchRetval = CompareBranchLine(a, b);
5517
                if (branchRetval != 0)
5518
                {
5519
                    return branchRetval;
5520
                }
5521
                else
5522
                {
5523
                    // Symbol 연결 갯수
5524
                    int connSymbolRetval = CompareConnSymbol(a, b);
5525
                    if (connSymbolRetval != 0)
5526
                    {
5527
                        return connSymbolRetval;
5528
                    }
5529
                    else
5530
                    {
5531
                        // 아이템 연결 갯수(심볼, Line이면서 Not Branch)
5532
                        int connItemRetval = CompareConnItem(a, b);
5533
                        if (connItemRetval != 0)
5534
                        {
5535
                            return connItemRetval;
5536
                        }
5537
                        else
5538
                        {
5539
                            // ConnectedItem이 없는것
5540
                            int noneConnRetval = CompareNoneConn(a, b);
5541
                            if (noneConnRetval != 0)
5542
                            {
5543
                                return noneConnRetval;
5544
                            }
5545
                            else
5546
                            {
5547

    
5548
                            }
5549
                        }
5550
                    }
5551
                }
5552

    
5553
                return 0;
5554
            }
5555

    
5556
            int CompareNotSegmentLine(Line a, Line b)
5557
            {
5558
                List<Connector> connectorsA = a.CONNECTORS
5559
                    .Where(conn => conn.ConnectedObject != null && conn.ConnectedObject.GetType() == typeof(Symbol))
5560
                    .ToList();
5561

    
5562
                List<Connector> connectorsB = b.CONNECTORS
5563
                    .Where(conn => conn.ConnectedObject != null && conn.ConnectedObject.GetType() == typeof(Symbol))
5564
                    .ToList();
5565

    
5566
                // 오름차순
5567
                return connectorsB.Count.CompareTo(connectorsA.Count);
5568
            }
5569

    
5570
            int CompareConnSymbol(Line a, Line b)
5571
            {
5572
                List<Connector> connectorsA = a.CONNECTORS
5573
                    .Where(conn => conn.ConnectedObject != null && conn.ConnectedObject.GetType() == typeof(Symbol))
5574
                    .ToList();
5575

    
5576
                List<Connector> connectorsB = b.CONNECTORS
5577
                    .Where(conn => conn.ConnectedObject != null && conn.ConnectedObject.GetType() == typeof(Symbol))
5578
                    .ToList();
5579

    
5580
                // 오름차순
5581
                return connectorsB.Count.CompareTo(connectorsA.Count);
5582
            }
5583

    
5584
            int CompareConnItem(Line a, Line b)
5585
            {
5586
                List<Connector> connectorsA = a.CONNECTORS
5587
                    .Where(conn => conn.ConnectedObject != null && 
5588
                    (conn.ConnectedObject.GetType() == typeof(Symbol) || 
5589
                    (conn.ConnectedObject.GetType() == typeof(Line) && !SPPIDUtil.IsBranchLine((Line)conn.ConnectedObject, a))))
5590
                    .ToList();
5591

    
5592
                List<Connector> connectorsB = b.CONNECTORS
5593
                    .Where(conn => conn.ConnectedObject != null &&
5594
                    (conn.ConnectedObject.GetType() == typeof(Symbol) ||
5595
                    (conn.ConnectedObject.GetType() == typeof(Line) && !SPPIDUtil.IsBranchLine((Line)conn.ConnectedObject, b))))
5596
                    .ToList();
5597

    
5598
                // 오름차순
5599
                return connectorsB.Count.CompareTo(connectorsA.Count);
5600
            }
5601

    
5602
            int CompareBranchLine(Line a, Line b)
5603
            {
5604
                List<Connector> connectorsA = a.CONNECTORS
5605
                    .Where(conn => conn.ConnectedObject != null && conn.ConnectedObject.GetType() == typeof(Line) && SPPIDUtil.IsBranchLine(a, conn.ConnectedObject as Line))
5606
                    .ToList();
5607
                List<Connector> connectorsB = b.CONNECTORS
5608
                    .Where(conn => conn.ConnectedObject != null && conn.ConnectedObject.GetType() == typeof(Line) && SPPIDUtil.IsBranchLine(b, conn.ConnectedObject as Line))
5609
                    .ToList();
5610

    
5611
                // 내림차순
5612
                return connectorsA.Count.CompareTo(connectorsB.Count);
5613
            }
5614

    
5615
            int CompareNoneConn(Line a, Line b)
5616
            {
5617
                List<Connector> connectorsA = a.CONNECTORS
5618
                    .Where(conn => conn.ConnectedObject == null)
5619
                    .ToList();
5620

    
5621
                List<Connector> connectorsB = b.CONNECTORS
5622
                    .Where(conn => conn.ConnectedObject == null)
5623
                    .ToList();
5624

    
5625
                // 오름차순
5626
                return connectorsB.Count.CompareTo(connectorsA.Count);
5627
            }
5628
        }
5629

    
5630
        private void SortText(List<Text> texts)
5631
        {
5632
            texts.Sort(Sort);
5633

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

    
5647
            int CompareY(Text a, Text b)
5648
            {
5649
                return a.LOCATION_Y.CompareTo(b.LOCATION_Y);
5650
            }
5651

    
5652
            int CompareX(Text a, Text b)
5653
            {
5654
                return a.LOCATION_X.CompareTo(b.LOCATION_X);
5655
            }
5656
        }
5657
        private void SortNote(List<Note> notes)
5658
        {
5659
            notes.Sort(Sort);
5660

    
5661
            int Sort(Note a, Note b)
5662
            {
5663
                int yRetval = CompareY(a, b);
5664
                if (yRetval != 0)
5665
                {
5666
                    return yRetval;
5667
                }
5668
                else
5669
                {
5670
                    return CompareX(a, b);
5671
                }
5672
            }
5673

    
5674
            int CompareY(Note a, Note b)
5675
            {
5676
                return a.LOCATION_Y.CompareTo(b.LOCATION_Y);
5677
            }
5678

    
5679
            int CompareX(Note a, Note b)
5680
            {
5681
                return a.LOCATION_X.CompareTo(b.LOCATION_X);
5682
            }
5683
        }
5684

    
5685
        private void SortBranchLines()
5686
        {
5687
            BranchLines.Sort(SortBranchLine);
5688
            int SortBranchLine(Line a, Line b)
5689
            {
5690
                int countA = a.CONNECTORS.FindAll(x => x.ConnectedObject != null &&
5691
                 x.ConnectedObject.GetType() == typeof(Line) &&
5692
                 SPPIDUtil.IsBranchLine(x.ConnectedObject as Line, a) &&
5693
                 string.IsNullOrEmpty(((Line)x.ConnectedObject).SPPID.ModelItemId)).Count;
5694

    
5695
                int countB = b.CONNECTORS.FindAll(x => x.ConnectedObject != null &&
5696
                 x.ConnectedObject.GetType() == typeof(Line) &&
5697
                 SPPIDUtil.IsBranchLine(x.ConnectedObject as Line, b) &&
5698
                 string.IsNullOrEmpty(((Line)x.ConnectedObject).SPPID.ModelItemId)).Count;
5699

    
5700
                // 내림차순
5701
                return countA.CompareTo(countB);
5702
            }
5703
        }
5704

    
5705
        private static int SortSymbolPriority(Symbol a, Symbol b)
5706
        {
5707
            int countA = a.CONNECTORS.FindAll(x => !string.IsNullOrEmpty(x.CONNECTEDITEM) && x.CONNECTEDITEM != "None").Count;
5708
            int countB = b.CONNECTORS.FindAll(x => !string.IsNullOrEmpty(x.CONNECTEDITEM) && x.CONNECTEDITEM != "None").Count;
5709
            int retval = countB.CompareTo(countA);
5710
            if (retval != 0)
5711
                return retval;
5712
            else
5713
                return a.SPPID.ORIGINAL_X.CompareTo(b.SPPID.ORIGINAL_X);
5714
        }
5715

    
5716
        private string GetSPPIDFileName(LMModelItem modelItem)
5717
        {
5718
            string symbolPath = null;
5719
            foreach (LMRepresentation rep in modelItem.Representations)
5720
            {
5721
                if (!DBNull.Value.Equals(rep.get_FileName()) && !string.IsNullOrEmpty(rep.get_FileName()))
5722
                {
5723
                    symbolPath = rep.get_FileName();
5724
                    break;
5725
                }
5726
            }
5727
            return symbolPath;
5728
        }
5729

    
5730
        private string GetSPPIDFileName(string modelItemId)
5731
        {
5732
            LMModelItem modelItem = dataSource.GetModelItem(modelItemId);
5733
            string symbolPath = null;
5734
            foreach (LMRepresentation rep in modelItem.Representations)
5735
            {
5736
                if (!DBNull.Value.Equals(rep.get_FileName()) && !string.IsNullOrEmpty(rep.get_FileName()))
5737
                {
5738
                    symbolPath = rep.get_FileName();
5739
                    break;
5740
                }
5741
            }
5742
            ReleaseCOMObjects(modelItem);
5743
            return symbolPath;
5744
        }
5745

    
5746
        /// <summary>
5747
        /// Graphic OID로 해당 Symbol의 크기를 구하여 Zoom
5748
        /// </summary>
5749
        /// <param name="graphicOID"></param>
5750
        /// <param name="milliseconds"></param>
5751
        private void ZoomObjectByGraphicOID(string graphicOID, int milliseconds = 150)
5752
        {
5753
            if (radApp.ActiveDocument.ActiveSheet.DrawingObjects[graphicOID] != null)
5754
            {
5755
                double minX = 0;
5756
                double minY = 0;
5757
                double maxX = 0;
5758
                double maxY = 0;
5759
                radApp.ActiveDocument.ActiveSheet.DrawingObjects[graphicOID].Range(out minX, out minY, out maxX, out maxY);
5760
                radApp.ActiveWindow.ZoomArea2(minX - 0.007, minY - 0.007, maxX + 0.007, maxY + 0.007, null);
5761

    
5762
                Thread.Sleep(milliseconds);
5763
            }
5764
        }
5765

    
5766
        /// <summary>
5767
        /// ComObject를 Release
5768
        /// </summary>
5769
        /// <param name="objVars"></param>
5770
        public void ReleaseCOMObjects(params object[] objVars)
5771
        {
5772
            if (objVars != null)
5773
            {
5774
                int intNewRefCount = 0;
5775
                foreach (object obj in objVars)
5776
                {
5777
                    if (!Information.IsNothing(obj) && System.Runtime.InteropServices.Marshal.IsComObject(obj))
5778
                        intNewRefCount = intNewRefCount + System.Runtime.InteropServices.Marshal.FinalReleaseComObject(obj);
5779
                }
5780
            }
5781
        }
5782

    
5783
        /// IDisposable 구현
5784
        ~AutoModeling()
5785
        {
5786
            this.Dispose(false);
5787
        }
5788

    
5789
        private bool disposed;
5790
        public void Dispose()
5791
        {
5792
            this.Dispose(true);
5793
            GC.SuppressFinalize(this);
5794
        }
5795

    
5796
        protected virtual void Dispose(bool disposing)
5797
        {
5798
            if (this.disposed) return;
5799
            if (disposing)
5800
            {
5801
                // IDisposable 인터페이스를 구현하는 멤버들을 여기서 정리합니다.
5802
            }
5803
            // .NET Framework에 의하여 관리되지 않는 외부 리소스들을 여기서 정리합니다.
5804
            this.disposed = true;
5805
        }
5806
    }
5807
}
클립보드 이미지 추가 (최대 크기: 500 MB)