프로젝트

일반

사용자정보

통계
| 개정판:

hytos / DTI_PID / SPPIDConverter / AutoModeling.cs @ 30ba9ae0

이력 | 보기 | 이력해설 | 다운로드 (172 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
28
    {
29
        Placement _placement;
30
        LMADataSource dataSource;
31
        LMDrawing currentDrawing;
32
        dynamic newDrawing;
33
        dynamic application;
34
        Ingr.RAD2D.Application radApp;
35
        SPPID_Document document;
36
        ETCSetting _ETCSetting;
37

    
38
        public string DocumentLabelText { get; set; }
39

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

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

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

    
67
                }
68
            }
69
        }
70

    
71
        /// <summary>
72
        /// 도면 단위당 실행되는 메서드
73
        /// </summary>
74
        public void Run()
75
        {
76
            string drawingNumber = document.DrawingNumber;
77
            string drawingName = document.DrawingName;
78
            try
79
            {
80
                _placement = new Placement();
81
                dataSource = _placement.PIDDataSource;
82

    
83
                CreateDocument(ref drawingNumber, ref drawingName);
84

    
85
                if (DocumentCoordinateCorrection())
86
                {
87
                    Log.Write("Start Modeling");
88
                    SplashScreenManager.ShowForm(typeof(SPPIDSplashScreen), true, true);
89
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetParent, (IntPtr)radApp.HWnd);
90
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetAllStepCount, 15);
91
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetDocumentName, DocumentLabelText);
92

    
93
                    // Equipment Modeling
94
                    RunEquipmentModeling();
95
                    // Symbol Modeling
96
                    RunSymbolModeling();
97
                    // LineRun Line Modeling
98
                    RunLineModeling();
99
                    // Branch Line Modeling
100
                    RunBranchLineModeling();
101
                    // Clear Attribute
102
                    RunClearValueInconsistancy();
103
                    // EndBreak Modeling
104
                    RunEndBreakModeling();
105
                    // SpecBreak Modeling
106
                    RunSpecBreakModeling();
107
                    // Join SameConnector
108
                    RunJoinRunForSameConnector();
109
                    // Check FlowDirection
110
                    RunFlowDirection();
111
                    // Note Modeling
112
                    RunNoteModeling();
113
                    // Text Modeling
114
                    RunTextModeling();
115
                    // Input LineNumber Attribute
116
                    RunInputLineNumberAttribute();
117
                    // Input Symbol Attribute
118
                    RunInputSymbolAttribute();
119
                    // Input SpecBreak Attribute
120
                    RunInputSpecBreakAttribute();
121
                    // Label Symbol Modeling
122
                    RunLabelSymbolModeling();
123

    
124
                    // Result Logging
125
                    document.CheckModelingResult();
126
                    
127
                    
128
                    //// LineRun Line Join
129
                    //SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Join LineRuns");
130
                    //foreach (LineNumber lineNumber in document.LINENUMBERS)
131
                    //    try
132
                    //    {
133
                    //        foreach (LineRun run in lineNumber.RUNS)
134
                    //            JoinRunLine(run);
135
                    //    }
136
                    //    catch (Exception ex)
137
                    //    {
138
                    //        Log.Write("Error in JoinRunLine");
139
                    //        Log.Write("UID : " + lineNumber.UID);
140
                    //        Log.Write(ex.Message);
141
                    //        Log.Write(ex.StackTrace);
142
                    //    }
143

    
144
                    //// TrimLineRun Line Join
145
                    //foreach (TrimLine trimLine in document.TRIMLINES)
146
                    //    try
147
                    //    {
148
                    //        foreach (LineRun run in trimLine.RUNS)
149
                    //            JoinRunLine(run);
150
                    //    }
151
                    //    catch (Exception ex)
152
                    //    {
153
                    //        Log.Write("Error in JoinRunLine");
154
                    //        Log.Write("UID : " + trimLine.UID);
155
                    //        Log.Write(ex.Message);
156
                    //        Log.Write(ex.StackTrace);
157
                    //    }
158
                }
159
            }
160
            catch (Exception ex)
161
            {
162
                SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.ClearParent, null);
163
                SplashScreenManager.CloseForm(false);
164
                System.Windows.Forms.MessageBox.Show(ex.Message + "\r\n" + ex.StackTrace);
165
            }
166
            finally
167
            {
168
                Log.Write("End Modeling");
169
                application.ActiveWindow.Fit();
170

    
171
                if (currentDrawing != null)
172
                    ReleaseCOMObjects(currentDrawing);
173

    
174
                if (radApp.ActiveDocument != null)
175
                {
176
                    radApp.ActiveDocument.Save();
177
                    ReleaseCOMObjects(newDrawing);
178
                }
179

    
180
                ReleaseCOMObjects(dataSource);
181
                ReleaseCOMObjects(_placement);
182

    
183
                Project_DB.InsertDrawingInfo(document.PATH, drawingNumber, drawingName, document);
184
                if (SplashScreenManager.Default.IsSplashFormVisible)
185
                {
186
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.ClearParent, null);
187
                    SplashScreenManager.CloseForm(false);
188
                    Log.Write("\r\n");
189
                }
190
            }
191
        }
192
        private void RunEquipmentModeling()
193
        {
194
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetAllProgress, document.Equipments.Count);
195
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Equipments Modeling");
196
            foreach (Equipment item in document.Equipments)
197
            {
198
                try
199
                {
200
                    EquipmentModeling(item);
201
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetProgress, document.Equipments.FindAll(x => !string.IsNullOrEmpty(x.SPPID.RepresentationId)).Count);
202
                }
203
                catch (Exception ex)
204
                {
205
                    Log.Write("Error in EquipmentModeling");
206
                    Log.Write("UID : " + item.UID);
207
                    Log.Write(ex.Message);
208
                    Log.Write(ex.StackTrace);
209
                }
210
            }
211
        }
212
        private void RunSymbolModeling()
213
        {
214
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetAllProgress, document.SYMBOLS.Count);
215
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Symbol Modeling");
216
            prioritySymbols = GetPrioritySymbol();
217
            foreach (var item in prioritySymbols)
218
            {
219
                try
220
                {
221
                    SymbolModelingBySymbol(item);
222
                }
223
                catch (Exception ex)
224
                {
225
                    Log.Write("Error in SymbolModelingByPriority");
226
                    Log.Write("UID : " + item.UID);
227
                    Log.Write(ex.Message);
228
                    Log.Write(ex.StackTrace);
229
                }
230
            }
231
        }
232
        private void RunLineModeling()
233
        {
234
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetAllProgress, document.LINES.Count);
235
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Lines Modeling");
236
            SetPriorityLine();
237
            foreach (var item in document.LINES)
238
            {
239
                try
240
                {
241
                    NewLineModeling(item);
242
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
243
                }
244
                catch (Exception ex)
245
                {
246
                    Log.Write("Error in NewLineModeling");
247
                    Log.Write("UID : " + item.UID);
248
                    Log.Write(ex.Message);
249
                    Log.Write(ex.StackTrace);
250
                }
251
            }
252
        }
253
        private void RunBranchLineModeling()
254
        {
255
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetAllProgress, NewBranchLines.Count);
256
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Branch Lines Modeling");
257
            int branchCount = NewBranchLines.Count;
258
            while (NewBranchLines.Count > 0)
259
            {
260
                Line item = NewBranchLines[0];
261
                try
262
                {
263
                    SortBranchLines();
264
                    NewLineModeling(item, true);
265
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
266
                }
267
                catch (Exception ex)
268
                {
269
                    Log.Write("Error in NewLineModeling");
270
                    Log.Write("UID : " + item.UID);
271
                    Log.Write(ex.Message);
272
                    Log.Write(ex.StackTrace);
273
                }
274
            }
275
        }
276
        private void RunClearValueInconsistancy()
277
        {
278
            int count = 1;
279
            bool loop = true;
280
            while (loop)
281
            {
282
                loop = false;
283
                LMAFilter filter = new LMAFilter();
284
                LMACriterion criterion = new LMACriterion();
285
                filter.ItemType = "Relationship";
286
                criterion.SourceAttributeName = "SP_DRAWINGID";
287
                criterion.Operator = "=";
288
                criterion.set_ValueAttribute(currentDrawing.Id);
289
                filter.get_Criteria().Add(criterion);
290

    
291
                LMRelationships relationships = new LMRelationships();
292
                relationships.Collect(dataSource, Filter: filter);
293

    
294
                SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetAllProgress, relationships.Count);
295
                if (count > 1)
296
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStepMinus, null);
297
                SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Clear Inconsistent Property Value - " + count);
298
                foreach (LMRelationship relationship in relationships)
299
                {
300
                    foreach (LMInconsistency inconsistency in relationship.Inconsistencies)
301
                    {
302
                        if (inconsistency.get_InconsistencyTypeIndex() == 1)
303
                        {
304
                            LMModelItem modelItem1 = relationship.Item1RepresentationObject == null ? null : relationship.Item1RepresentationObject.ModelItemObject;
305
                            LMModelItem modelItem2 = relationship.Item2RepresentationObject == null ? null : relationship.Item2RepresentationObject.ModelItemObject;
306
                            string[] array = inconsistency.get_Name().ToString().Split(new char[] { '=' });
307
                            if (modelItem1 != null)
308
                            {
309
                                string attrName = array[0];
310
                                if (attrName.Contains("PipingPoint"))
311
                                {
312
                                    string originalAttr = attrName.Split(new char[] { '.' })[1];
313
                                    int index = Convert.ToInt32(relationship.get_Item1Location());
314
                                    LMAAttribute attribute1 = modelItem1.Attributes["PipingPoint" + index + "." + originalAttr];
315
                                    if (attribute1 != null && !DBNull.Value.Equals(attribute1.get_Value()))
316
                                    {
317
                                        loop = true;
318
                                        attribute1.set_Value(DBNull.Value);
319
                                    }
320
                                }
321
                                else
322
                                {
323
                                    LMAAttribute attribute1 = modelItem1.Attributes[attrName];
324
                                    if (attribute1 != null && !DBNull.Value.Equals(attribute1.get_Value()))
325
                                    {
326
                                        loop = true;
327
                                        attribute1.set_Value(DBNull.Value);
328
                                    }
329
                                }
330
                                modelItem1.Commit();
331
                            }
332
                            if (modelItem2 != null)
333
                            {
334
                                string attrName = array[1];
335
                                if (attrName.Contains("PipingPoint"))
336
                                {
337
                                    string originalAttr = attrName.Split(new char[] { '.' })[1];
338
                                    int index = Convert.ToInt32(relationship.get_Item2Location());
339
                                    LMAAttribute attribute2 = modelItem2.Attributes["PipingPoint" + index + "." + originalAttr];
340
                                    if (attribute2 != null && !DBNull.Value.Equals(attribute2.get_Value()))
341
                                    {
342
                                        attribute2.set_Value(DBNull.Value);
343
                                        loop = true;
344
                                    }
345
                                }
346
                                else
347
                                {
348
                                    LMAAttribute attribute2 = modelItem2.Attributes[attrName];
349
                                    if (attribute2 != null && !DBNull.Value.Equals(attribute2.get_Value()))
350
                                    {
351
                                        attribute2.set_Value(DBNull.Value);
352
                                        loop = true;
353
                                    }
354
                                }
355
                                modelItem2.Commit();
356
                            }
357
                            if (modelItem1 != null)
358
                                ReleaseCOMObjects(modelItem1);
359
                            if (modelItem2 != null)
360
                                ReleaseCOMObjects(modelItem2);
361
                            inconsistency.Commit();
362
                        }
363
                    }
364
                    relationship.Commit();
365
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
366
                }
367
                ReleaseCOMObjects(filter);
368
                ReleaseCOMObjects(criterion);
369
                ReleaseCOMObjects(relationships);
370
                count++;
371
            }
372
        }
373
        private void RunEndBreakModeling()
374
        {
375
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetAllProgress, document.EndBreaks.Count);
376
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "EndBreaks Modeling");
377
            foreach (var item in document.EndBreaks)
378
                try
379
                {
380
                    EndBreakModeling(item);
381
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
382
                }
383
                catch (Exception ex)
384
                {
385
                    Log.Write("Error in EndBreakModeling");
386
                    Log.Write("UID : " + item.UID);
387
                    Log.Write(ex.Message);
388
                    Log.Write(ex.StackTrace);
389
                }
390
        }
391
        private void RunSpecBreakModeling()
392
        {
393
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetAllProgress, document.SpecBreaks.Count);
394
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "SpecBreaks Modeling");
395
            foreach (var item in document.SpecBreaks)
396
                try
397
                {
398
                    SpecBreakModeling(item);
399
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
400
                }
401
                catch (Exception ex)
402
                {
403
                    Log.Write("Error in SpecBreakModeling");
404
                    Log.Write("UID : " + item.UID);
405
                    Log.Write(ex.Message);
406
                    Log.Write(ex.StackTrace);
407
                }
408
        }
409
        private void RunJoinRunForSameConnector()
410
        {
411
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetAllProgress, document.LINES.Count);
412
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "PipeRun Join");
413
            foreach (var line in document.LINES)
414
            {
415
                if (!SPPIDUtil.IsSegmentLine(document, line))
416
                {
417
                    foreach (var connector in line.CONNECTORS)
418
                    {
419
                        if (connector.ConnectedObject != null &&
420
                            connector.ConnectedObject.GetType() == typeof(Line) &&
421
                            !SPPIDUtil.IsBranchLine(line, connector.ConnectedObject as Line)) 
422
                        {
423
                            Line connLine = connector.ConnectedObject as Line;
424
                            if (line.SPPID.ModelItemId != connLine.SPPID.ModelItemId && !string.IsNullOrEmpty(line.SPPID.ModelItemId) && !string.IsNullOrEmpty(connLine.SPPID.ModelItemId))
425
                                JoinPipeRun(connLine.SPPID.ModelItemId, line.SPPID.ModelItemId);
426
                        }
427
                    }
428
                }
429
                SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
430
            }
431
        }
432
        private void RunFlowDirection()
433
        {
434
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetAllProgress, 
435
                document.LINES.Count + ZeroLengthModelItemID.Count + ZeroLengthModelItemIDReverse.Count + ZeroLengthSymbolToSymbolModelItemID.Count);
436
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Check Flow Direction");
437
            foreach (var line in document.LINES)
438
            {
439
                if (!string.IsNullOrEmpty(line.SPPID.ModelItemId))
440
                {
441
                    LMModelItem modelItem = dataSource.GetModelItem(line.SPPID.ModelItemId);
442
                    if (modelItem != null && modelItem.get_ItemStatus() == "Active")
443
                    {
444
                        LMAAttribute attribute = modelItem.Attributes["FlowDirection"];
445
                        if (attribute != null)
446
                        {
447
                            attribute.set_Value("End 1 is upstream (Inlet)");
448
                            modelItem.Commit();
449
                        }
450

    
451
                        SetFlowDirectionByLine(line.SPPID.ModelItemId);
452

    
453
                        ReleaseCOMObjects(modelItem);
454
                    }
455
                }
456

    
457
                SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
458
            }
459
            foreach (var modelId in ZeroLengthModelItemID)
460
            {
461
                LMModelItem zeroLengthModelItem = dataSource.GetModelItem(modelId);
462
                LMAAttribute attribute = zeroLengthModelItem.Attributes["FlowDirection"];
463
                if (attribute != null && zeroLengthModelItem.get_ItemStatus() == "Active")
464
                {
465
                    attribute.set_Value("End 1 is upstream (Inlet)");
466
                    zeroLengthModelItem.Commit();
467
                }
468

    
469
                SetFlowDirectionByLine(modelId);
470

    
471
                ReleaseCOMObjects(zeroLengthModelItem);
472
                SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
473
            }
474
            foreach (var modelId in ZeroLengthModelItemIDReverse)
475
            {
476
                LMModelItem zeroLengthModelItem = dataSource.GetModelItem(modelId);
477
                LMAAttribute attribute = zeroLengthModelItem.Attributes["FlowDirection"];
478
                if (attribute != null && zeroLengthModelItem.get_ItemStatus() == "Active")
479
                {
480
                    attribute.set_Value("End 1 is downstream (Outlet)");
481
                    zeroLengthModelItem.Commit();
482
                }
483

    
484
                SetFlowDirectionByLine(modelId);
485

    
486
                ReleaseCOMObjects(zeroLengthModelItem);
487
                SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
488
            }
489
            foreach (var modelId in ZeroLengthSymbolToSymbolModelItemID)
490
            {
491
                SetFlowDirectionByLine(modelId);
492
                SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
493
            }
494

    
495
            void SetFlowDirectionByLine(string lineModelItemID)
496
            {
497
                LMModelItem modelItem = dataSource.GetModelItem(lineModelItemID);
498
                if (modelItem != null && modelItem.get_ItemStatus() == "Active")
499
                {
500
                    LMAAttribute attribute = modelItem.Attributes["FlowDirection"];
501
                    if (attribute != null && !DBNull.Value.Equals(attribute.get_Value()))
502
                    {
503
                        string sFlowDirection = attribute.get_Value().ToString();
504
                        foreach (LMRepresentation rep in modelItem.Representations)
505
                        {
506
                            if (rep.Attributes["RepresentationType"].get_Value() == "Connector" && rep.Attributes["ItemStatus"].get_Value() == "Active")
507
                            {
508
                                LMConnector connector = dataSource.GetConnector(rep.Id);
509

    
510
                                foreach (LMRelationship relationship in connector.Relation1Relationships)
511
                                    SetSymbolFlowDirectionByRelationShip(relationship, connector, sFlowDirection);
512
                                foreach (LMRelationship relationship in connector.Relation2Relationships)
513
                                    SetSymbolFlowDirectionByRelationShip(relationship, connector, sFlowDirection);
514

    
515
                                ReleaseCOMObjects(connector);
516
                            }
517
                        }
518
                    }
519
                    ReleaseCOMObjects(modelItem);
520
                }
521

    
522
                void SetSymbolFlowDirectionByRelationShip(LMRelationship relationship, LMConnector connector, string sFlowDirection)
523
                {
524
                    // Item2가 Symbol
525
                    if (!DBNull.Value.Equals(relationship.Item1RepresentationID) && relationship.Item1RepresentationID == connector.Id &&
526
                        relationship.Item2RepresentationObject != null && relationship.Item2RepresentationObject.get_RepresentationType() == "Symbol")
527
                    {
528
                        int symbolIndex = Convert.ToInt32(relationship.get_Item2Location());
529
                        int lineIndex = Convert.ToInt32(relationship.get_Item1Location());
530
                        LMModelItem symbolModelItem = relationship.Item2RepresentationObject.ModelItemObject;
531

    
532
                        SetSymbolFlowDirection(lineIndex, symbolIndex, sFlowDirection, symbolModelItem);
533

    
534
                        symbolModelItem.Commit();
535
                        ReleaseCOMObjects(symbolModelItem);
536
                    }
537
                    // Item1이 Symbol
538
                    else if (!DBNull.Value.Equals(relationship.Item2RepresentationID) && relationship.Item2RepresentationID == connector.Id &&
539
                            relationship.Item1RepresentationObject != null && relationship.Item1RepresentationObject.get_RepresentationType() == "Symbol")
540
                    {
541
                        int symbolIndex = Convert.ToInt32(relationship.get_Item1Location());
542
                        int lineIndex = Convert.ToInt32(relationship.get_Item2Location());
543
                        LMModelItem symbolModelItem = relationship.Item1RepresentationObject.ModelItemObject;
544

    
545
                        SetSymbolFlowDirection(lineIndex, symbolIndex, sFlowDirection, symbolModelItem);
546

    
547
                        symbolModelItem.Commit();
548
                        ReleaseCOMObjects(symbolModelItem);
549
                    }
550
                }
551

    
552
                void SetSymbolFlowDirection(int lineIndex, int symbolIndex, string sFlowDirection, LMModelItem symbolModelItem)
553
                {
554
                    string attrName = "PipingPoint" + symbolIndex + ".FlowDirection";
555
                    LMAAttribute attribute = symbolModelItem.Attributes[attrName];
556
                    if (attribute != null)
557
                    {
558
                        if (lineIndex == 0 && sFlowDirection == "End 1 is upstream (Inlet)")
559
                            attribute.set_Value("End 1 is downstream (Outlet)");
560
                        else if (lineIndex == 0 && sFlowDirection == "End 1 is downstream (Outlet)")
561
                            attribute.set_Value("End 1 is upstream (Inlet)");
562
                        else if (lineIndex == 1 && sFlowDirection == "End 1 is upstream (Inlet)")
563
                            attribute.set_Value("End 1 is upstream (Inlet)");
564
                        else if (lineIndex == 1 && sFlowDirection == "End 1 is downstream (Outlet)")
565
                            attribute.set_Value("End 1 is downstream (Outlet)");
566
                    }
567
                }
568
            }
569
        }
570
        private void RunNoteModeling()
571
        {
572
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetAllProgress, document.SYMBOLS.Count);
573
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Notes Modeling");
574
            foreach (var item in document.NOTES)
575
                try
576
                {
577
                    NoteModeling(item);
578
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
579
                }
580
                catch (Exception ex)
581
                {
582
                    Log.Write("Error in NoteModeling");
583
                    Log.Write("UID : " + item.UID);
584
                    Log.Write(ex.Message);
585
                    Log.Write(ex.StackTrace);
586
                }
587
        }
588
        private void RunTextModeling()
589
        {
590
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetAllProgress, document.TEXTINFOS.Count);
591
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Texts Modeling");
592
            foreach (var item in document.TEXTINFOS)
593
                try
594
                {
595
                    TextModeling(item);
596
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
597
                }
598
                catch (Exception ex)
599
                {
600
                    Log.Write("Error in TextModeling");
601
                    Log.Write("UID : " + item.UID);
602
                    Log.Write(ex.Message);
603
                    Log.Write(ex.StackTrace);
604
                }
605
        }
606
        private void RunInputLineNumberAttribute()
607
        {
608
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetAllProgress, document.LINENUMBERS.Count);
609
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Set LineNumbers Attribute");
610
            foreach (var item in document.LINENUMBERS)
611
                try
612
                {
613
                    InputLineNumberAttribute(item);
614
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
615
                }
616
                catch (Exception ex)
617
                {
618
                    Log.Write("Error in InputLineNumberAttribute");
619
                    Log.Write("UID : " + item.UID);
620
                    Log.Write(ex.Message);
621
                    Log.Write(ex.StackTrace);
622
                }
623
        }
624
        private void RunInputSymbolAttribute()
625
        {
626
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetAllProgress, document.SYMBOLS.Count);
627
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Set Symbols Attribute");
628
            foreach (var item in document.SYMBOLS)
629
                try
630
                {
631
                    InputSymbolAttribute(item, item.ATTRIBUTES);
632
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
633
                }
634
                catch (Exception ex)
635
                {
636
                    Log.Write("Error in InputSymbolAttribute");
637
                    Log.Write("UID : " + item.UID);
638
                    Log.Write(ex.Message);
639
                    Log.Write(ex.StackTrace);
640
                }
641
        }
642
        private void RunInputSpecBreakAttribute()
643
        {
644
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetAllProgress, document.SYMBOLS.Count);
645
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Set Symbols Attribute");
646
            foreach (var item in document.SpecBreaks)
647
                try
648
                {
649
                    InputSpecBreakAttribute(item);
650
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
651
                }
652
                catch (Exception ex)
653
                {
654
                    Log.Write("Error in InputSpecBreakAttribute");
655
                    Log.Write("UID : " + item.UID);
656
                    Log.Write(ex.Message);
657
                    Log.Write(ex.StackTrace);
658
                }
659
        }
660
        private void RunLabelSymbolModeling()
661
        {
662
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetAllProgress, document.SYMBOLS.Count);
663
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Labels Modeling");
664
            foreach (var item in document.SYMBOLS)
665
                try
666
                {
667
                    LabelSymbolModeling(item);
668
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
669
                }
670
                catch (Exception ex)
671
                {
672
                    Log.Write("Error in LabelSymbolModeling");
673
                    Log.Write("UID : " + item.UID);
674
                    Log.Write(ex.Message);
675
                    Log.Write(ex.StackTrace);
676
                }
677
        }
678
        
679
        /// <summary>
680
        /// 도면 생성 메서드
681
        /// </summary>
682
        private void CreateDocument(ref string drawingNumber, ref string drawingName)
683
        {
684
            Log.Write("------------------ Start create document ------------------");
685
            GetDrawingNameAndNumber(ref drawingName, ref drawingNumber);
686
            Log.Write("Drawing name : " + drawingName);
687
            Log.Write("Drawing number : " + drawingNumber);
688
            newDrawing = application.Drawings.Add(document.Unit, document.Template, drawingNumber, drawingName);
689
            document.SPPID_DrawingNumber = drawingNumber;
690
            document.SPPID_DrawingName = drawingName;
691
            application.ActiveWindow.Fit();
692
            Thread.Sleep(1000);
693
            application.ActiveWindow.Zoom = 2000;
694
            Thread.Sleep(2000);
695

    
696
            //current LMDrawing 가져오기
697
            LMAFilter filter = new LMAFilter();
698
            LMACriterion criterion = new LMACriterion();
699
            filter.ItemType = "Drawing";
700
            criterion.SourceAttributeName = "Name";
701
            criterion.Operator = "=";
702
            criterion.set_ValueAttribute(drawingName);
703
            filter.get_Criteria().Add(criterion);
704

    
705
            LMDrawings drawings = new LMDrawings();
706
            drawings.Collect(dataSource, Filter: filter);
707

    
708
            currentDrawing = ((dynamic)drawings).Nth(1);
709
        }
710

    
711
        /// <summary>
712
        /// DrawingName, DrawingNumber를 확인하여 중복이 있으면 _1을 붙이고 +1씩 한다.
713
        /// </summary>
714
        /// <param name="drawingName"></param>
715
        /// <param name="drawingNumber"></param>
716
        private void GetDrawingNameAndNumber(ref string drawingName, ref string drawingNumber)
717
        {
718
            LMDrawings drawings = new LMDrawings();
719
            drawings.Collect(dataSource);
720

    
721
            List<string> drawingNameList = new List<string>();
722
            List<string> drawingNumberList = new List<string>();
723

    
724
            foreach (LMDrawing item in drawings)
725
            {
726
                drawingNameList.Add(item.Attributes["Name"].get_Value().ToString());
727
                drawingNumberList.Add(item.Attributes["DrawingNumber"].get_Value().ToString());
728
            }
729

    
730
            int nameLength = drawingName.Length;
731
            while (drawingNameList.Contains(drawingName))
732
            {
733
                if (nameLength == drawingName.Length)
734
                    drawingName += "-1";
735
                else
736
                {
737
                    int index = Convert.ToInt32(drawingName.Remove(0, nameLength + 1));
738
                    drawingName = drawingName.Substring(0, nameLength + 1);
739
                    drawingName += ++index;
740
                }
741
            }
742

    
743
            int numberLength = drawingNumber.Length;
744
            while (drawingNameList.Contains(drawingNumber))
745
            {
746
                if (numberLength == drawingNumber.Length)
747
                    drawingNumber += "-1";
748
                else
749
                {
750
                    int index = Convert.ToInt32(drawingNumber.Remove(0, numberLength + 1));
751
                    drawingNumber = drawingNumber.Substring(0, numberLength + 1);
752
                    drawingNumber += ++index;
753
                }
754
            }
755

    
756
            ReleaseCOMObjects(drawings);
757
        }
758

    
759
        /// <summary>
760
        /// 도면 크기 구하는 메서드
761
        /// </summary>
762
        /// <returns></returns>
763
        private bool DocumentCoordinateCorrection()
764
        {
765
            if (Settings.Default.DrawingX != 0 && Settings.Default.DrawingY != 0)
766
            {
767
                Log.Write("Setting Drawing X, Drawing Y");
768
                document.SetSPPIDLocation(Settings.Default.DrawingX, Settings.Default.DrawingY);
769
                Log.Write("Start coordinate correction");
770
                document.CoordinateCorrection();
771
                return true;
772
            }
773
            else
774
            {
775
                Log.Write("Need Drawing X, Y");
776
                return false;
777
            }
778
        }
779

    
780
        /// <summary>
781
        /// 심볼을 실제로 Modeling 메서드
782
        /// </summary>
783
        /// <param name="symbol"></param>
784
        /// <param name="targetSymbol"></param>
785
        /// <param name="prevSymbol"></param>
786
        private void SymbolModeling(Symbol symbol, Symbol targetSymbol)
787
        {
788
            // OWNERSYMBOL Attribute, 값을 가지고 있을 경우
789
            BaseModel.Attribute itemAttribute = symbol.ATTRIBUTES.Find(attr => attr.ATTRIBUTE == "OWNERSYMBOL");
790
            if (itemAttribute != null && (string.IsNullOrEmpty(itemAttribute.VALUE) || itemAttribute.VALUE != "None"))
791
                return;
792
            // 이미 모델링 됐을 경우
793
            else if (!string.IsNullOrEmpty(symbol.SPPID.RepresentationId))
794
                return;
795

    
796
            LMSymbol _LMSymbol = null;
797

    
798
            string mappingPath = symbol.SPPID.MAPPINGNAME;
799
            double x = symbol.SPPID.ORIGINAL_X;
800
            double y = symbol.SPPID.ORIGINAL_Y;
801
            int mirror = 0;
802
            double angle = symbol.ANGLE;
803

    
804
            // OPC 일경우 180도 일때 Mirror
805
            if (mappingPath.Contains("Piping OPC's") && angle == Math.PI)
806
                mirror = 1;
807

    
808
            // Mirror 계산
809
            if (symbol.FLIP == 1)
810
            {
811
                mirror = 1;
812
                angle += Math.PI;
813
            }
814

    
815
            if (targetSymbol != null && !string.IsNullOrEmpty(targetSymbol.SPPID.RepresentationId))
816
            {
817
                LMSymbol _TargetItem = dataSource.GetSymbol(targetSymbol.SPPID.RepresentationId);
818
                Connector connector = SPPIDUtil.FindSymbolConnectorByUID(document, symbol.UID, targetSymbol);
819
                if (connector != null)
820
                    GetTargetSymbolConnectorPoint(connector, targetSymbol, ref x, ref y);
821

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

    
824
                if (_LMSymbol != null && _TargetItem != null)
825
                {
826
                    symbol.SPPID.RepresentationId = _LMSymbol.AsLMRepresentation().Id;
827

    
828
                    if (SPPIDUtil.IsSegmentLine(document, symbol, targetSymbol))
829
                    {
830
                        LMConnector reModelingConnector = FindBreakLineTarget(symbol, targetSymbol);
831
                        if (reModelingConnector != null)
832
                            ReModelingLMConnector(reModelingConnector);
833
                    }
834
                }
835

    
836
                ReleaseCOMObjects(_TargetItem);
837
            }
838
            else
839
                _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle);
840

    
841
            if (_LMSymbol != null)
842
            {
843
                _LMSymbol.Commit();
844
                symbol.SPPID.RepresentationId = _LMSymbol.AsLMRepresentation().Id;
845
                symbol.SPPID.ModelItemID = _LMSymbol.ModelItemID;
846
                symbol.SPPID.GraphicOID = _LMSymbol.get_GraphicOID().ToString();
847

    
848
                foreach (var item in symbol.ChildSymbols)
849
                    CreateChildSymbol(item, _LMSymbol, symbol);
850

    
851
                symbol.SPPID.SPPID_X = _LMSymbol.get_XCoordinate();
852
                symbol.SPPID.SPPID_Y = _LMSymbol.get_YCoordinate();
853

    
854
                double[] range = null;
855
                GetSPPIDSymbolRange(symbol, ref range);
856
                symbol.SPPID.SPPID_Min_X = range[0];
857
                symbol.SPPID.SPPID_Min_Y = range[1];
858
                symbol.SPPID.SPPID_Max_X = range[2];
859
                symbol.SPPID.SPPID_Max_Y = range[3];
860

    
861
                foreach (var item in symbol.SPPID.CorrectionX_GroupSymbols)
862
                    item.SPPID.ORIGINAL_X = symbol.SPPID.SPPID_X;
863
                foreach (var item in symbol.SPPID.CorrectionY_GroupSymbols)
864
                    item.SPPID.ORIGINAL_Y = symbol.SPPID.SPPID_Y;
865

    
866
                ReleaseCOMObjects(_LMSymbol);
867
            }
868
        }
869

    
870
        private void RemoveSymbol(Symbol symbol)
871
        {
872
            if (!string.IsNullOrEmpty(symbol.SPPID.RepresentationId))
873
            {
874
                LMSymbol _LMSymbol = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
875
                if (_LMSymbol != null)
876
                {
877
                    _placement.PIDRemovePlacement(_LMSymbol.AsLMRepresentation());
878
                    ReleaseCOMObjects(_LMSymbol);
879
                }
880
            }
881

    
882
            symbol.SPPID.RepresentationId = string.Empty;
883
            symbol.SPPID.ModelItemID = string.Empty;
884
            symbol.SPPID.SPPID_X = double.NaN;
885
            symbol.SPPID.SPPID_Y = double.NaN;
886
            symbol.SPPID.SPPID_Min_X = double.NaN;
887
            symbol.SPPID.SPPID_Min_Y = double.NaN;
888
            symbol.SPPID.SPPID_Max_X = double.NaN;
889
            symbol.SPPID.SPPID_Max_Y = double.NaN;
890
        }
891

    
892
        private void RemoveSymbol(List<Symbol> symbols)
893
        {
894
            foreach (var symbol in symbols)
895
            {
896
                if (!string.IsNullOrEmpty(symbol.SPPID.RepresentationId))
897
                {
898
                    LMSymbol _LMSymbol = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
899
                    if (_LMSymbol != null)
900
                    {
901
                        _placement.PIDRemovePlacement(_LMSymbol.AsLMRepresentation());
902
                        ReleaseCOMObjects(_LMSymbol);
903
                    }
904
                }
905

    
906
                symbol.SPPID.RepresentationId = string.Empty;
907
                symbol.SPPID.ModelItemID = string.Empty;
908
                symbol.SPPID.SPPID_X = double.NaN;
909
                symbol.SPPID.SPPID_Y = double.NaN;
910
                symbol.SPPID.SPPID_Min_X = double.NaN;
911
                symbol.SPPID.SPPID_Min_Y = double.NaN;
912
                symbol.SPPID.SPPID_Max_X = double.NaN;
913
                symbol.SPPID.SPPID_Max_Y = double.NaN;
914
            }
915
        }
916

    
917
        /// <summary>
918
        /// ID2의 Symbol Width와 Height를 비교해서 상대적인 SPPID Connector좌표를 가져온다.
919
        /// </summary>
920
        /// <param name="targetConnector"></param>
921
        /// <param name="targetSymbol"></param>
922
        /// <param name="x"></param>
923
        /// <param name="y"></param>
924
        private void GetTargetSymbolConnectorPoint(Connector targetConnector, Symbol targetSymbol, ref double x, ref double y)
925
        {
926
            LMSymbol _TargetItem = dataSource.GetSymbol(targetSymbol.SPPID.RepresentationId);
927

    
928
            double[] range = null;
929
            List<double[]> points = new List<double[]>();
930
            GetSPPIDSymbolRangeAndConnectionPoints(targetSymbol, ref range, points);
931
            double x1 = range[0];
932
            double y1 = range[1];
933
            double x2 = range[2];
934
            double y2 = range[3];
935

    
936
            // Origin 기준 Connector의 위치차이
937
            double sceneX = 0;
938
            double sceneY = 0;
939
            SPPIDUtil.ConvertPointBystring(targetConnector.SCENECONNECTPOINT, ref sceneX, ref sceneY);
940
            double originX = 0;
941
            double originY = 0;
942
            SPPIDUtil.ConvertPointBystring(targetSymbol.ORIGINALPOINT, ref originX, ref originY);
943
            double gapX = originX - sceneX;
944
            double gapY = originY - sceneY;
945

    
946
            // SPPID Symbol과 ID2 심볼의 크기 차이
947
            double sizeWidth = 0;
948
            double sizeHeight = 0;
949
            SPPIDUtil.ConvertPointBystring(targetSymbol.SIZE, ref sizeWidth, ref sizeHeight);
950
            if (sizeWidth == 0 || sizeHeight == 0)
951
                throw new Exception("Check symbol size! \r\nUID : " + targetSymbol.UID);
952

    
953
            double percentX = (x2 - x1) / sizeWidth;
954
            double percentY = (y2 - y1) / sizeHeight;
955

    
956
            double SPPIDgapX = gapX * percentX;
957
            double SPPIDgapY = gapY * percentY;
958

    
959
            double[] SPPIDOriginPoint = new double[] { _TargetItem.get_XCoordinate() - SPPIDgapX, _TargetItem.get_YCoordinate() + SPPIDgapY };
960
            double distance = double.MaxValue;
961
            double[] resultPoint;
962
            foreach (var point in points)
963
            {
964
                double result = SPPIDUtil.CalcPointToPointdDistance(point[0], point[1], SPPIDOriginPoint[0], SPPIDOriginPoint[1]);
965
                if (distance > result)
966
                {
967
                    distance = result;
968
                    resultPoint = point;
969
                    x = point[0];
970
                    y = point[1];
971
                }
972
            }
973

    
974
            ReleaseCOMObjects(_TargetItem);
975
        }
976

    
977
        private void GetTargetLineConnectorPoint(Connector targetConnector, Line targetLine, ref double x, ref double y)
978
        {
979
            int index = targetLine.CONNECTORS.IndexOf(targetConnector);
980
            if (index == 0)
981
            {
982
                x = targetLine.SPPID.START_X;
983
                y = targetLine.SPPID.START_Y;
984
            }
985
            else
986
            {
987
                x = targetLine.SPPID.END_X;
988
                y = targetLine.SPPID.END_Y;
989
            }
990
        }
991

    
992
        /// <summary>
993
        /// SPPID Symbol의 Range를 구한다.
994
        /// </summary>
995
        /// <param name="symbol"></param>
996
        /// <param name="range"></param>
997
        private void GetSPPIDSymbolRangeAndConnectionPoints(Symbol symbol, ref double[] range, List<double[]> points)
998
        {
999
            LMSymbol _TargetItem = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
1000
            Ingr.RAD2D.Symbol2d symbol2d = radApp.ActiveDocument.ActiveSheet.DrawingObjects[_TargetItem.get_GraphicOID().ToString()];
1001
            double x1 = 0;
1002
            double y1 = 0;
1003
            double x2 = 0;
1004
            double y2 = 0;
1005
            symbol2d.Range(out x1, out y1, out x2, out y2);
1006
            range = new double[] { x1, y1, x2, y2 };
1007

    
1008
            for (int i = 1; i < int.MaxValue; i++)
1009
            {
1010
                double connX = 0;
1011
                double connY = 0;
1012
                if (_placement.PIDConnectPointLocation(_TargetItem, i, ref connX, ref connY))
1013
                    points.Add(new double[] { connX, connY });
1014
                else
1015
                    break;
1016
            }
1017

    
1018
            foreach (var childSymbol in symbol.ChildSymbols)
1019
                GetSPPIDChildSymbolRange(childSymbol, ref range, points);
1020

    
1021
            ReleaseCOMObjects(_TargetItem);
1022
        }
1023

    
1024
        private void GetSPPIDSymbolRange(Symbol symbol, ref double[] range)
1025
        {
1026
            LMSymbol _TargetItem = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
1027
            if (_TargetItem != null)
1028
            {
1029
                Ingr.RAD2D.Symbol2d symbol2d = radApp.ActiveDocument.ActiveSheet.DrawingObjects[_TargetItem.get_GraphicOID().ToString()];
1030
                double x1 = 0;
1031
                double y1 = 0;
1032
                double x2 = 0;
1033
                double y2 = 0;
1034
                symbol2d.Range(out x1, out y1, out x2, out y2);
1035
                range = new double[] { x1, y1, x2, y2 };
1036

    
1037
                foreach (var childSymbol in symbol.ChildSymbols)
1038
                    GetSPPIDChildSymbolRange(childSymbol, ref range);
1039

    
1040
                ReleaseCOMObjects(_TargetItem);
1041
            }
1042
        }
1043

    
1044
        private void GetSPPIDSymbolRange(List<Symbol> symbols, ref double[] range)
1045
        {
1046
            double[] tempRange = new double[] { double.MaxValue, double.MaxValue, double.MinValue, double.MinValue };
1047
            foreach (var symbol in symbols)
1048
            {
1049
                LMSymbol _TargetItem = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
1050
                Ingr.RAD2D.Symbol2d symbol2d = radApp.ActiveDocument.ActiveSheet.DrawingObjects[_TargetItem.get_GraphicOID().ToString()];
1051
                double x1 = 0;
1052
                double y1 = 0;
1053
                double x2 = 0;
1054
                double y2 = 0;
1055
                symbol2d.Range(out x1, out y1, out x2, out y2);
1056

    
1057
                tempRange[0] = Math.Min(tempRange[0], x1);
1058
                tempRange[1] = Math.Min(tempRange[1], y1);
1059
                tempRange[2] = Math.Max(tempRange[2], x2);
1060
                tempRange[3] = Math.Max(tempRange[3], y2);
1061

    
1062
                foreach (var childSymbol in symbol.ChildSymbols)
1063
                    GetSPPIDChildSymbolRange(childSymbol, ref tempRange);
1064

    
1065
                ReleaseCOMObjects(_TargetItem);
1066
            }
1067

    
1068
            range = tempRange;
1069
        }
1070

    
1071
        /// <summary>
1072
        /// Child Modeling 된 Symbol의 Range를 구한다.
1073
        /// </summary>
1074
        /// <param name="childSymbol"></param>
1075
        /// <param name="range"></param>
1076
        private void GetSPPIDChildSymbolRange(ChildSymbol childSymbol, ref double[] range, List<double[]> points)
1077
        {
1078
            LMSymbol _ChildSymbol = dataSource.GetSymbol(childSymbol.SPPID.RepresentationId);
1079
            if (_ChildSymbol != null)
1080
            {
1081
                Ingr.RAD2D.Symbol2d symbol2d = radApp.ActiveDocument.ActiveSheet.DrawingObjects[_ChildSymbol.get_GraphicOID().ToString()];
1082
                double x1 = 0;
1083
                double y1 = 0;
1084
                double x2 = 0;
1085
                double y2 = 0;
1086
                symbol2d.Range(out x1, out y1, out x2, out y2);
1087
                range[0] = Math.Min(range[0], x1);
1088
                range[1] = Math.Min(range[1], y1);
1089
                range[2] = Math.Max(range[2], x2);
1090
                range[3] = Math.Max(range[3], y2);
1091

    
1092
                for (int i = 1; i < int.MaxValue; i++)
1093
                {
1094
                    double connX = 0;
1095
                    double connY = 0;
1096
                    if (_placement.PIDConnectPointLocation(_ChildSymbol, i, ref connX, ref connY))
1097
                        points.Add(new double[] { connX, connY });
1098
                    else
1099
                        break;
1100
                }
1101

    
1102
                foreach (var loopChildSymbol in childSymbol.ChildSymbols)
1103
                    GetSPPIDChildSymbolRange(loopChildSymbol, ref range, points);
1104

    
1105
                ReleaseCOMObjects(_ChildSymbol);
1106
            }
1107
        }
1108

    
1109
        private void GetSPPIDChildSymbolRange(ChildSymbol childSymbol, ref double[] range)
1110
        {
1111
            LMSymbol _ChildSymbol = dataSource.GetSymbol(childSymbol.SPPID.RepresentationId);
1112
            if (_ChildSymbol != null)
1113
            {
1114
                Ingr.RAD2D.Symbol2d symbol2d = radApp.ActiveDocument.ActiveSheet.DrawingObjects[_ChildSymbol.get_GraphicOID().ToString()];
1115
                double x1 = 0;
1116
                double y1 = 0;
1117
                double x2 = 0;
1118
                double y2 = 0;
1119
                symbol2d.Range(out x1, out y1, out x2, out y2);
1120
                range[0] = Math.Min(range[0], x1);
1121
                range[1] = Math.Min(range[1], y1);
1122
                range[2] = Math.Max(range[2], x2);
1123
                range[3] = Math.Max(range[3], y2);
1124

    
1125
                foreach (var loopChildSymbol in childSymbol.ChildSymbols)
1126
                    GetSPPIDChildSymbolRange(loopChildSymbol, ref range);
1127
                ReleaseCOMObjects(_ChildSymbol);
1128
            }
1129
        }
1130

    
1131
        /// <summary>
1132
        /// Label Symbol Modeling
1133
        /// </summary>
1134
        /// <param name="symbol"></param>
1135
        private void LabelSymbolModeling(Symbol symbol)
1136
        {
1137
            if (string.IsNullOrEmpty(symbol.SPPID.RepresentationId))
1138
            {
1139
                BaseModel.Attribute itemAttribute = symbol.ATTRIBUTES.Find(x => x.ATTRIBUTE == "OWNERSYMBOL");
1140
                if (itemAttribute == null || string.IsNullOrEmpty(itemAttribute.VALUE) || itemAttribute.VALUE == "None")
1141
                    return;
1142
                Array points = new double[] { 0, symbol.SPPID.ORIGINAL_X, symbol.SPPID.ORIGINAL_Y };
1143

    
1144
                string symbolUID = itemAttribute.VALUE;
1145
                object targetItem = SPPIDUtil.FindObjectByUID(document, symbolUID);
1146
                if (targetItem != null &&
1147
                    (targetItem.GetType() == typeof(Symbol) ||
1148
                    targetItem.GetType() == typeof(Equipment)))
1149
                {
1150
                    // Object 아이템이 Symbol일 경우 Equipment일 경우 
1151
                    string sRep = null;
1152
                    if (targetItem.GetType() == typeof(Symbol))
1153
                        sRep = ((Symbol)targetItem).SPPID.RepresentationId;
1154
                    else if (targetItem.GetType() == typeof(Equipment))
1155
                        sRep = ((Equipment)targetItem).SPPID.RepresentationId;
1156
                    if (!string.IsNullOrEmpty(sRep))
1157
                    {
1158
                        // LEADER Line 검사
1159
                        bool leaderLine = false;
1160
                        SymbolMapping symbolMapping = document.SymbolMappings.Find(x => x.UID == symbol.DBUID);
1161
                        if (symbolMapping != null)
1162
                            leaderLine = symbolMapping.LEADERLINE;
1163

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

    
1168
                        //Leader 선 센터로
1169
                        if (_LMLabelPresist != null)
1170
                        {
1171
                            // Target Item에 Label의 Attribute Input
1172
                            InputSymbolAttribute(targetItem, symbol.ATTRIBUTES);
1173

    
1174
                            string OID = _LMLabelPresist.get_GraphicOID().ToString();
1175
                            DependencyObject dependency = radApp.ActiveDocument.ActiveSheet.DrawingObjects[OID] as DependencyObject;
1176
                            if (dependency != null)
1177
                            {
1178
                                bool result = false;
1179
                                foreach (var attributes in dependency.AttributeSets)
1180
                                {
1181
                                    foreach (var attribute in attributes)
1182
                                    {
1183
                                        string name = attribute.Name;
1184
                                        string value = attribute.GetValue().ToString();
1185
                                        if (name == "DrawingItemType" && value == "LabelPersist")
1186
                                        {
1187
                                            foreach (DrawingObjectBase drawingObject in dependency.DrawingObjects)
1188
                                            {
1189
                                                if (drawingObject.Type == Ingr.RAD2D.ObjectType.igLineString2d)
1190
                                                {
1191
                                                    Ingr.RAD2D.LineString2d lineString2D = drawingObject as Ingr.RAD2D.LineString2d;
1192
                                                    double prevX = _TargetItem.get_XCoordinate();
1193
                                                    double prevY = _TargetItem.get_YCoordinate();
1194
                                                    lineString2D.InsertVertex(lineString2D.VertexCount, prevX, prevY);
1195
                                                    lineString2D.RemoveVertex(lineString2D.VertexCount);
1196
                                                    result = true;
1197
                                                    break;
1198
                                                }
1199
                                            }
1200
                                        }
1201

    
1202
                                        if (result)
1203
                                            break;
1204
                                    }
1205

    
1206
                                    if (result)
1207
                                        break;
1208
                                }
1209
                            }
1210

    
1211
                            symbol.SPPID.RepresentationId = _LMLabelPresist.AsLMRepresentation().Id;
1212
                            _LMLabelPresist.Commit();
1213
                            ReleaseCOMObjects(_LMLabelPresist);
1214
                        }
1215

    
1216
                        ReleaseCOMObjects(_TargetItem);
1217
                    }
1218
                }
1219
                else if (targetItem != null && targetItem.GetType() == typeof(Line))
1220
                {
1221
                    Line targetLine = targetItem as Line;
1222
                    Dictionary<LMConnector, List<double[]>> connectorVertices = GetPipeRunVertices(targetLine.SPPID.ModelItemId);
1223
                    LMConnector connectedLMConnector = FindTargetLMConnectorForLabel(connectorVertices, symbol.SPPID.ORIGINAL_X, symbol.SPPID.ORIGINAL_Y);
1224
                    if (connectedLMConnector != null)
1225
                    {
1226
                        // LEADER Line 검사
1227
                        bool leaderLine = false;
1228
                        SymbolMapping symbolMapping = document.SymbolMappings.Find(x => x.UID == symbol.DBUID);
1229
                        if (symbolMapping != null)
1230
                            leaderLine = symbolMapping.LEADERLINE;
1231

    
1232
                        LMLabelPersist _LMLabelPresist = _placement.PIDPlaceLabel(symbol.SPPID.MAPPINGNAME, ref points, Rotation: 0, LabeledItem: connectedLMConnector.AsLMRepresentation(), IsLeaderVisible: leaderLine);
1233
                        if (_LMLabelPresist != null)
1234
                        {
1235
                            _LMLabelPresist.Commit();
1236
                            ReleaseCOMObjects(_LMLabelPresist);
1237
                        }
1238
                        ReleaseCOMObjects(connectedLMConnector);
1239
                    }
1240

    
1241
                    foreach (var item in connectorVertices)
1242
                        if (item.Key != null)
1243
                            ReleaseCOMObjects(item.Key);
1244
                }
1245
            }
1246
        }
1247

    
1248
        /// <summary>
1249
        /// Equipment를 실제로 Modeling 메서드
1250
        /// </summary>
1251
        /// <param name="equipment"></param>
1252
        private void EquipmentModeling(Equipment equipment)
1253
        {
1254
            if (!string.IsNullOrEmpty(equipment.SPPID.RepresentationId))
1255
                return;
1256

    
1257
            LMSymbol _LMSymbol = null;
1258
            LMSymbol targetItem = null;
1259
            string mappingPath = equipment.SPPID.MAPPINGNAME;
1260
            double x = equipment.SPPID.ORIGINAL_X;
1261
            double y = equipment.SPPID.ORIGINAL_Y;
1262
            int mirror = 0;
1263
            double angle = equipment.ANGLE;
1264

    
1265
            SPPIDUtil.ConvertGridPoint(ref x, ref y);
1266

    
1267
            Connector connector = equipment.CONNECTORS.Find(conn => !string.IsNullOrEmpty(conn.CONNECTEDITEM) && conn.CONNECTEDITEM != "None");
1268
            if (connector != null)
1269
            {
1270
                Equipment connEquipment = SPPIDUtil.FindObjectByUID(document, connector.CONNECTEDITEM) as Equipment;
1271
                if (connEquipment != null)
1272
                {
1273
                    if (string.IsNullOrEmpty(connEquipment.SPPID.RepresentationId))
1274
                        EquipmentModeling(connEquipment);
1275

    
1276
                    if (!string.IsNullOrEmpty(connEquipment.SPPID.RepresentationId))
1277
                    {
1278
                        targetItem = dataSource.GetSymbol(connEquipment.SPPID.RepresentationId);
1279
                        if (targetItem != null)
1280
                        {
1281
                            _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle, TargetItem: targetItem);
1282
                        }
1283
                        else
1284
                        {
1285
                            _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle);
1286
                        }
1287
                    }
1288
                    else
1289
                    {
1290
                        _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle);
1291
                    }
1292
                }
1293
                else
1294
                {
1295
                    _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle);
1296
                }
1297
            }
1298
            else
1299
            {
1300
                _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle);
1301
            }
1302

    
1303
            if (_LMSymbol != null)
1304
            {
1305
                _LMSymbol.Commit();
1306
                equipment.SPPID.RepresentationId = _LMSymbol.AsLMRepresentation().Id;
1307
                equipment.SPPID.GraphicOID = _LMSymbol.get_GraphicOID().ToString();
1308
                ReleaseCOMObjects(_LMSymbol);
1309
            }
1310

    
1311
            if (targetItem != null)
1312
            {
1313
                ReleaseCOMObjects(targetItem);
1314
            }
1315

    
1316
            ReleaseCOMObjects(_LMSymbol);
1317
        }
1318

    
1319
        /// <summary>
1320
        /// 첫 진입점
1321
        /// </summary>
1322
        /// <param name="symbol"></param>
1323
        private void SymbolModelingBySymbol(Symbol symbol)
1324
        {
1325
            SymbolModeling(symbol, null);
1326
            List<object> endObjects = new List<object>();
1327
            endObjects.Add(symbol);
1328

    
1329
            foreach (var connector in symbol.CONNECTORS)
1330
            {
1331
                object connItem = SPPIDUtil.FindObjectByUID(document, connector.CONNECTEDITEM);
1332
                if (connItem != null && connItem.GetType() != typeof(Equipment))
1333
                {
1334
                    endObjects.Add(connItem);
1335
                    if (connItem.GetType() == typeof(Symbol))
1336
                    {
1337
                        Symbol connSymbol = connItem as Symbol;
1338
                        if (string.IsNullOrEmpty(connSymbol.SPPID.RepresentationId))
1339
                        {
1340
                            SymbolModeling(connSymbol, symbol);
1341
                        }
1342
                        SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetProgress, document.SYMBOLS.FindAll(x => !string.IsNullOrEmpty(x.SPPID.RepresentationId)).Count);
1343
                        SymbolModelingByNeerSymbolLoop(connSymbol, endObjects);
1344
                    }
1345
                    else if (connItem.GetType() == typeof(Line))
1346
                    {
1347
                        Line connLine = connItem as Line;
1348
                        SymbolModelingByNeerLineLoop(connLine, endObjects, symbol);
1349
                    }
1350
                }
1351
            }
1352
        }
1353

    
1354
        private void SymbolModelingByNeerSymbolLoop(Symbol symbol, List<object> endObjects)
1355
        {
1356
            foreach (var connector in symbol.CONNECTORS)
1357
            {
1358
                object connItem = SPPIDUtil.FindObjectByUID(document, connector.CONNECTEDITEM);
1359
                if (connItem != null && connItem.GetType() != typeof(Equipment))
1360
                {
1361
                    if (!endObjects.Contains(connItem))
1362
                    {
1363
                        endObjects.Add(connItem);
1364
                        if (connItem.GetType() == typeof(Symbol))
1365
                        {
1366
                            Symbol connSymbol = connItem as Symbol;
1367
                            if (string.IsNullOrEmpty(connSymbol.SPPID.RepresentationId))
1368
                            {
1369
                                SymbolModeling(connSymbol, symbol);
1370
                            }
1371
                            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetProgress, document.SYMBOLS.FindAll(x => !string.IsNullOrEmpty(x.SPPID.RepresentationId)).Count);
1372
                            SymbolModelingByNeerSymbolLoop(connSymbol, endObjects);
1373
                        }
1374
                        else if (connItem.GetType() == typeof(Line))
1375
                        {
1376
                            Line connLine = connItem as Line;
1377
                            SymbolModelingByNeerLineLoop(connLine, endObjects, symbol);
1378
                        }
1379
                    }
1380
                }
1381
            }
1382
        }
1383

    
1384
        private void SymbolModelingByNeerLineLoop(Line line, List<object> endObjects, Symbol prevSymbol)
1385
        {
1386
            foreach (var connector in line.CONNECTORS)
1387
            {
1388
                object connItem = SPPIDUtil.FindObjectByUID(document, connector.CONNECTEDITEM);
1389
                if (connItem != null && connItem.GetType() != typeof(Equipment))
1390
                {
1391
                    if (!endObjects.Contains(connItem))
1392
                    {
1393
                        endObjects.Add(connItem);
1394
                        if (connItem.GetType() == typeof(Symbol))
1395
                        {
1396
                            Symbol connSymbol = connItem as Symbol;
1397
                            if (string.IsNullOrEmpty(connSymbol.SPPID.RepresentationId))
1398
                            {
1399
                                List<Symbol> group = new List<Symbol>();
1400
                                SPPIDUtil.FindConnectedSymbolGroup(document, connSymbol, group);
1401
                                Symbol priority = prioritySymbols.Find(x => group.Contains(x));
1402
                                List<Symbol> endModelingGroup = new List<Symbol>();
1403
                                if (priority != null)
1404
                                {
1405
                                    SymbolGroupModeling(priority, group);
1406

    
1407
                                    // Range 겹치는지 확인해야함
1408
                                    double[] prevRange = null;
1409
                                    GetSPPIDSymbolRange(prevSymbol, ref prevRange);
1410
                                    double[] groupRange = null;
1411
                                    GetSPPIDSymbolRange(group, ref groupRange);
1412

    
1413
                                    double distanceX = 0;
1414
                                    double distanceY = 0;
1415
                                    bool overlapX = false;
1416
                                    bool overlapY = false;
1417
                                    SlopeType slopeType = SPPIDUtil.CalcSlope(prevSymbol.SPPID.ORIGINAL_X, prevSymbol.SPPID.ORIGINAL_Y, connSymbol.SPPID.ORIGINAL_X, connSymbol.SPPID.ORIGINAL_Y);
1418
                                    SPPIDUtil.CalcOverlap(prevRange, groupRange, ref distanceX, ref distanceY, ref overlapX, ref overlapY);
1419
                                    if ((slopeType == SlopeType.HORIZONTAL && overlapX) ||
1420
                                        (slopeType == SlopeType.VERTICAL && overlapY))
1421
                                    {
1422
                                        RemoveSymbol(group);
1423
                                        foreach (var _temp in group)
1424
                                            SPPIDUtil.CalcNewCoordinateForSymbol(_temp, prevSymbol, distanceX, distanceY);
1425

    
1426
                                        SymbolGroupModeling(priority, group);
1427
                                    }
1428
                                }
1429
                                else
1430
                                {
1431
                                    SymbolModeling(connSymbol, null);
1432
                                    // Range 겹치는지 확인해야함
1433
                                    double[] prevRange = null;
1434
                                    GetSPPIDSymbolRange(prevSymbol, ref prevRange);
1435
                                    double[] connRange = null;
1436
                                    GetSPPIDSymbolRange(connSymbol, ref connRange);
1437

    
1438
                                    double distanceX = 0;
1439
                                    double distanceY = 0;
1440
                                    bool overlapX = false;
1441
                                    bool overlapY = false;
1442
                                    SlopeType slopeType = SPPIDUtil.CalcSlope(prevSymbol.SPPID.ORIGINAL_X, prevSymbol.SPPID.ORIGINAL_Y, connSymbol.SPPID.ORIGINAL_X, connSymbol.SPPID.ORIGINAL_Y);
1443
                                    SPPIDUtil.CalcOverlap(prevRange, connRange, ref distanceX, ref distanceY, ref overlapX, ref overlapY);
1444
                                    if ((slopeType == SlopeType.HORIZONTAL && overlapX) ||
1445
                                        (slopeType == SlopeType.VERTICAL && overlapY))
1446
                                    {
1447
                                        RemoveSymbol(connSymbol);
1448
                                        SPPIDUtil.CalcNewCoordinateForSymbol(connSymbol, prevSymbol, distanceX, distanceY);
1449

    
1450
                                        SymbolModeling(connSymbol, null);
1451
                                    }
1452
                                }
1453
                            }
1454
                            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetProgress, document.SYMBOLS.FindAll(x => !string.IsNullOrEmpty(x.SPPID.RepresentationId)).Count);
1455
                            SymbolModelingByNeerSymbolLoop(connSymbol, endObjects);
1456
                        }
1457
                        else if (connItem.GetType() == typeof(Line))
1458
                        {
1459
                            Line connLine = connItem as Line;
1460
                            if (!SPPIDUtil.IsBranchLine(connLine, line))
1461
                                SymbolModelingByNeerLineLoop(connLine, endObjects, prevSymbol);
1462
                        }
1463
                    }
1464
                }
1465
            }
1466
        }
1467

    
1468
        private void SymbolGroupModeling(Symbol firstSymbol, List<Symbol> group)
1469
        {
1470
            List<Symbol> endModelingGroup = new List<Symbol>();
1471
            SymbolModeling(firstSymbol, null);
1472
            endModelingGroup.Add(firstSymbol);
1473
            while (endModelingGroup.Count != group.Count)
1474
            {
1475
                foreach (var _symbol in group)
1476
                {
1477
                    if (!endModelingGroup.Contains(_symbol))
1478
                    {
1479
                        foreach (var _connector in _symbol.CONNECTORS)
1480
                        {
1481
                            Symbol _connSymbol = SPPIDUtil.FindObjectByUID(document, _connector.CONNECTEDITEM) as Symbol;
1482
                            if (_connSymbol != null && endModelingGroup.Contains(_connSymbol))
1483
                            {
1484
                                SymbolModeling(_symbol, _connSymbol);
1485
                                endModelingGroup.Add(_symbol);
1486
                                break;
1487
                            }
1488
                        }
1489
                    }
1490
                }
1491
            }
1492
        }
1493

    
1494
        /// <summary>
1495
        /// 심볼을 실제로 Modeling할때 ChildSymbol이 있다면 Modeling하는 메서드
1496
        /// </summary>
1497
        /// <param name="childSymbol"></param>
1498
        /// <param name="parentSymbol"></param>
1499
        private void CreateChildSymbol(ChildSymbol childSymbol, LMSymbol parentSymbol, Symbol parent)
1500
        {
1501
            Ingr.RAD2D.Symbol2d symbol2d = radApp.ActiveDocument.ActiveSheet.DrawingObjects[parentSymbol.get_GraphicOID().ToString()];
1502
            double x1 = 0;
1503
            double x2 = 0;
1504
            double y1 = 0;
1505
            double y2 = 0;
1506
            symbol2d.Range(out x1, out y1, out x2, out y2);
1507

    
1508
            LMSymbol _LMSymbol = _placement.PIDPlaceSymbol(childSymbol.SPPID.MAPPINGNAME, (x1 + x2) / 2, (y1 + y2) / 2, TargetItem: parentSymbol);
1509
            if (_LMSymbol != null)
1510
            {
1511
                childSymbol.SPPID.RepresentationId = _LMSymbol.AsLMRepresentation().Id;
1512
                foreach (var item in childSymbol.ChildSymbols)
1513
                    CreateChildSymbol(item, _LMSymbol, parent);
1514
            }
1515

    
1516

    
1517
            ReleaseCOMObjects(_LMSymbol);
1518
        }
1519

    
1520
        private void NewLineModeling(Line line, bool isBranchModeling = false)
1521
        {
1522
            if (!string.IsNullOrEmpty(line.SPPID.ModelItemId) || (NewBranchLines.Contains(line) && !isBranchModeling))
1523
                return;
1524

    
1525
            List<Line> group = new List<Line>();
1526
            GetConnectedLineGroup(line, group);
1527
            LineCoordinateCorrection(group);
1528

    
1529
            foreach (var groupLine in group)
1530
            {
1531
                if (!isBranchModeling && SPPIDUtil.IsBranchLine(groupLine))
1532
                {
1533
                    NewBranchLines.Add(groupLine);
1534
                    continue;
1535
                }
1536

    
1537
                bool diagonal = false;
1538
                if (groupLine.SlopeType != SlopeType.HORIZONTAL && groupLine.SlopeType != SlopeType.VERTICAL)
1539
                    diagonal = true;
1540
                _LMAItem _LMAItem = _placement.PIDCreateItem(groupLine.SPPID.MAPPINGNAME);
1541
                LMSymbol _LMSymbolStart = null;
1542
                LMSymbol _LMSymbolEnd = null;
1543
                PlaceRunInputs placeRunInputs = new PlaceRunInputs();
1544
                foreach (var connector in groupLine.CONNECTORS)
1545
                {
1546
                    
1547
                    double x = 0;
1548
                    double y = 0;
1549
                    GetTargetLineConnectorPoint(connector, groupLine, ref x, ref y);
1550
                    if (connector.ConnectedObject == null)
1551
                    {
1552
                        placeRunInputs.AddPoint(x, y);
1553
                    }
1554
                    else if (connector.ConnectedObject.GetType() == typeof(Symbol))
1555
                    {
1556
                        Symbol targetSymbol = connector.ConnectedObject as Symbol;
1557
                        GetTargetSymbolConnectorPoint(targetSymbol.CONNECTORS.Find(z => z.ConnectedObject == groupLine), targetSymbol, ref x, ref y);
1558
                        if (groupLine.CONNECTORS.IndexOf(connector) == 0)
1559
                        {
1560
                            _LMSymbolStart = GetTargetSymbol(targetSymbol, groupLine);
1561
                            if (_LMSymbolStart != null)
1562
                                placeRunInputs.AddSymbolTarget(_LMSymbolStart, x, y, diagonal);
1563
                            else
1564
                                placeRunInputs.AddPoint(x, y);
1565
                        }
1566
                        else
1567
                        {
1568
                            _LMSymbolEnd = GetTargetSymbol(targetSymbol, groupLine);
1569
                            if (_LMSymbolEnd != null)
1570
                                placeRunInputs.AddSymbolTarget(_LMSymbolEnd, x, y, diagonal);
1571
                            else
1572
                                placeRunInputs.AddPoint(x, y);
1573
                        }
1574
                    }
1575
                    else if (connector.ConnectedObject.GetType() == typeof(Line))
1576
                    {
1577
                        Line targetLine = connector.ConnectedObject as Line;
1578
                        if (!string.IsNullOrEmpty(targetLine.SPPID.ModelItemId))
1579
                        {
1580
                            LMConnector targetConnector = FindTargetLMConnectorForBranch(line, targetLine, ref x, ref y);
1581
                            placeRunInputs.AddConnectorTarget(targetConnector, x, y, diagonal);
1582
                            ChangeLineSPPIDCoordinateByConnector(groupLine, targetLine, x, y, false);
1583
                        }
1584
                        else
1585
                        {
1586
                            if (groupLine.CONNECTORS.IndexOf(connector) == 0)
1587
                            {
1588
                                if (groupLine.SlopeType == SlopeType.HORIZONTAL)
1589
                                    placeRunInputs.AddPoint(x, -0.1);
1590
                                else if (groupLine.SlopeType == SlopeType.VERTICAL)
1591
                                    placeRunInputs.AddPoint(-0.1, y);
1592
                                else
1593
                                    placeRunInputs.AddPoint(x, -0.1);
1594
                            }
1595

    
1596
                            placeRunInputs.AddPoint(x, y);
1597

    
1598
                            if (groupLine.CONNECTORS.IndexOf(connector) == 1)
1599
                            {
1600
                                if (groupLine.SlopeType == SlopeType.HORIZONTAL)
1601
                                    placeRunInputs.AddPoint(x, -0.1);
1602
                                else if (groupLine.SlopeType == SlopeType.VERTICAL)
1603
                                    placeRunInputs.AddPoint(-0.1, y);
1604
                                else
1605
                                    placeRunInputs.AddPoint(x, -0.1);
1606
                            }
1607
                        }
1608
                    }
1609
                }
1610

    
1611
                LMConnector _lMConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
1612
                if (_lMConnector != null)
1613
                {
1614
                    groupLine.SPPID.ModelItemId = _lMConnector.ModelItemID;
1615
                    bool bRemodelingStart = false;
1616
                    if (_LMSymbolStart != null)
1617
                        NeedReModeling(groupLine, _LMSymbolStart, ref bRemodelingStart);
1618
                    bool bRemodelingEnd = false;
1619
                    if (_LMSymbolEnd != null)
1620
                        NeedReModeling(groupLine, _LMSymbolEnd, ref bRemodelingEnd);
1621

    
1622
                    if (bRemodelingStart || bRemodelingEnd)
1623
                        ReModelingLine(groupLine, _lMConnector, _LMSymbolStart, _LMSymbolEnd, bRemodelingStart, bRemodelingEnd);
1624

    
1625
                    FlowMarkModeling(groupLine);
1626
                    LineNumberModeling(groupLine);
1627

    
1628
                    ReleaseCOMObjects(_lMConnector);
1629
                }
1630

    
1631
                List<object> removeLines = groupLine.CONNECTORS.FindAll(x =>
1632
                x.ConnectedObject != null &&
1633
                x.ConnectedObject.GetType() == typeof(Line) &&
1634
                !string.IsNullOrEmpty(((Line)x.ConnectedObject).SPPID.ModelItemId))
1635
                .Select(x => x.ConnectedObject)
1636
                .ToList();
1637

    
1638
                foreach (var item in removeLines)
1639
                    RemoveLineForModeling(item as Line);
1640

    
1641
                if (_LMAItem != null)
1642
                    ReleaseCOMObjects(_LMAItem);
1643
                if (placeRunInputs != null)
1644
                    ReleaseCOMObjects(placeRunInputs);
1645
                if (_LMSymbolStart != null)
1646
                    ReleaseCOMObjects(_LMSymbolStart);
1647
                if (_LMSymbolEnd != null)
1648
                    ReleaseCOMObjects(_LMSymbolEnd);
1649

    
1650
                if (isBranchModeling && NewBranchLines.Contains(groupLine))
1651
                    NewBranchLines.Remove(groupLine);
1652
            }
1653
        }
1654

    
1655
        private void RemoveLineForModeling(Line line)
1656
        {
1657
            LMModelItem modelItem = dataSource.GetModelItem(line.SPPID.ModelItemId);
1658
            if (modelItem != null)
1659
            {
1660
                foreach (LMRepresentation rep in modelItem.Representations)
1661
                {
1662
                    if (rep.Attributes["RepresentationType"].get_Value() == "Connector" && rep.Attributes["ItemStatus"].get_Value() == "Active")
1663
                    {
1664
                        LMConnector _LMConnector = dataSource.GetConnector(rep.Id);
1665
                        dynamic OID = rep.get_GraphicOID().ToString();
1666
                        DependencyObject drawingObject = radApp.ActiveDocument.ActiveSheet.DrawingObjects[OID];
1667
                        Ingr.RAD2D.LineStringGeometry2d lineStringGeometry = drawingObject.GetGeometry() as Ingr.RAD2D.LineStringGeometry2d;
1668
                        int verticesCount = lineStringGeometry.VertexCount;
1669
                        double[] vertices = null;
1670
                        lineStringGeometry.GetVertices(ref verticesCount, ref vertices);
1671
                        for (int i = 0; i < verticesCount; i++)
1672
                        {
1673
                            double x = 0;
1674
                            double y = 0;
1675
                            lineStringGeometry.GetVertex(i + 1, ref x, ref y);
1676
                            if (verticesCount == 2 && x < 0 || y < 0)
1677
                                _placement.PIDRemovePlacement(rep);
1678
                        }
1679
                        ReleaseCOMObjects(_LMConnector);
1680
                    }
1681
                }
1682

    
1683
                ReleaseCOMObjects(modelItem);
1684
            }
1685
        }
1686

    
1687
        private void GetConnectedLineGroup(Line line, List<Line> group)
1688
        {
1689
            if (!group.Contains(line))
1690
                group.Add(line);
1691
            foreach (var connector in line.CONNECTORS)
1692
            {
1693
                if (connector.ConnectedObject != null &&
1694
                    connector.ConnectedObject.GetType() == typeof(Line) &&
1695
                    !group.Contains(connector.ConnectedObject) &&
1696
                    string.IsNullOrEmpty(((Line)connector.ConnectedObject).SPPID.ModelItemId))
1697
                {
1698
                    Line connLine = connector.ConnectedObject as Line;
1699
                    if (!SPPIDUtil.IsBranchLine(connLine, line))
1700
                        GetConnectedLineGroup(connLine, group);
1701
                }
1702
            }
1703
        }
1704

    
1705
        private void LineCoordinateCorrection(List<Line> group)
1706
        {
1707
            // 순서대로 전 Item 기준 정렬
1708
            LineCoordinateCorrectionByStart(group);
1709

    
1710
            // 역으로 심볼이 있을 경우 좌표 보정
1711
            LineCoordinateCorrectionForLastLine(group);
1712
        }
1713

    
1714
        private void LineCoordinateCorrectionByStart(List<Line> group)
1715
        {
1716
            for (int i = 0; i < group.Count; i++)
1717
            {
1718
                Line line = group[i];
1719
                if (i == 0)
1720
                {
1721
                    Connector symbolConnector = line.CONNECTORS.Find(x => x.ConnectedObject != null && x.ConnectedObject.GetType() == typeof(Symbol));
1722
                    if (symbolConnector != null)
1723
                        LineCoordinateCorrectionByConnItem(line, symbolConnector.ConnectedObject);
1724
                }
1725
                else if (i != 0)
1726
                {
1727
                    LineCoordinateCorrectionByConnItem(line, group[i - 1]);
1728
                }
1729
            }
1730
        }
1731

    
1732
        private void LineCoordinateCorrectionForLastLine(List<Line> group)
1733
        {
1734
            Line checkLine = group[group.Count - 1];
1735
            Connector lastSymbolConnector = checkLine.CONNECTORS.Find(x => x.ConnectedObject != null && x.ConnectedObject.GetType() == typeof(Symbol));
1736
            if (lastSymbolConnector != null)
1737
            {
1738
                LineCoordinateCorrectionByConnItem(checkLine, lastSymbolConnector.ConnectedObject);
1739
                for (int i = group.Count - 2; i >= 0; i--)
1740
                {
1741
                    Line line = group[i + 1];
1742
                    Line prevLine = group[i];
1743

    
1744
                    // 같으면 보정
1745
                    if (line.SlopeType == prevLine.SlopeType)
1746
                        LineCoordinateCorrectionByConnItem(prevLine, line);
1747
                    else
1748
                    {
1749
                        if (line.SlopeType == SlopeType.HORIZONTAL)
1750
                        {
1751
                            double prevX = 0;
1752
                            double prevY = 0;
1753
                            GetTargetLineConnectorPoint(prevLine.CONNECTORS.Find(z => z.ConnectedObject == line), prevLine, ref prevX, ref prevY);
1754
                            ChangeLineSPPIDCoordinateByConnectorOnlyX(line, prevLine, prevX);
1755

    
1756
                            double x = 0;
1757
                            double y = 0;
1758
                            GetTargetLineConnectorPoint(line.CONNECTORS.Find(z => z.ConnectedObject == prevLine), line, ref x, ref y);
1759
                            ChangeLineSPPIDCoordinateByConnectorOnlyY(prevLine, line, y);
1760
                        }
1761
                        else if (line.SlopeType == SlopeType.VERTICAL)
1762
                        {
1763
                            double prevX = 0;
1764
                            double prevY = 0;
1765
                            GetTargetLineConnectorPoint(prevLine.CONNECTORS.Find(z => z.ConnectedObject == line), prevLine, ref prevX, ref prevY);
1766
                            ChangeLineSPPIDCoordinateByConnectorOnlyY(line, prevLine, prevY);
1767

    
1768
                            double x = 0;
1769
                            double y = 0;
1770
                            GetTargetLineConnectorPoint(line.CONNECTORS.Find(z => z.ConnectedObject == prevLine), line, ref x, ref y);
1771
                            ChangeLineSPPIDCoordinateByConnectorOnlyX(prevLine, line, x);
1772
                        }
1773
                        break;
1774
                    }
1775
                }
1776
            }
1777
        }
1778

    
1779
        private void LineCoordinateCorrectionByConnItem(Line line, object connItem)
1780
        {
1781
            double x = 0;
1782
            double y = 0;
1783
            if (connItem.GetType() == typeof(Symbol))
1784
            {
1785
                Symbol targetSymbol = connItem as Symbol;
1786
                Connector targetConnector = targetSymbol.CONNECTORS.Find(z => z.ConnectedObject == line);
1787
                if (targetConnector != null)
1788
                    GetTargetSymbolConnectorPoint(targetConnector, targetSymbol, ref x, ref y);
1789
                else
1790
                    throw new Exception("Target symbol UID : " + targetSymbol.UID + "\r\nLine UID : " + line.UID);
1791
            }
1792
            else if (connItem.GetType() == typeof(Line))
1793
            {
1794
                Line targetLine = connItem as Line;
1795
                GetTargetLineConnectorPoint(targetLine.CONNECTORS.Find(z => z.ConnectedObject == line), targetLine, ref x, ref y);
1796
            }
1797

    
1798
            ChangeLineSPPIDCoordinateByConnector(line, connItem, x, y);
1799
        }
1800

    
1801
        private void ChangeLineSPPIDCoordinateByConnector(Line line, object connItem, double x, double y, bool changeOtherCoordinate = true)
1802
        {
1803
            Connector connector = line.CONNECTORS.Find(z => z.ConnectedObject == connItem);
1804
            int index = line.CONNECTORS.IndexOf(connector);
1805
            if (index == 0)
1806
            {
1807
                line.SPPID.START_X = x;
1808
                line.SPPID.START_Y = y;
1809
                if (line.SlopeType == SlopeType.HORIZONTAL && changeOtherCoordinate)
1810
                    line.SPPID.END_Y = y;
1811
                else if (line.SlopeType == SlopeType.VERTICAL && changeOtherCoordinate)
1812
                    line.SPPID.END_X = x;
1813
            }
1814
            else
1815
            {
1816
                line.SPPID.END_X = x;
1817
                line.SPPID.END_Y = y;
1818
                if (line.SlopeType == SlopeType.HORIZONTAL && changeOtherCoordinate)
1819
                    line.SPPID.START_Y = y;
1820
                else if (line.SlopeType == SlopeType.VERTICAL && changeOtherCoordinate)
1821
                    line.SPPID.START_X = x;
1822
            }
1823
        }
1824

    
1825
        private void ChangeLineSPPIDCoordinateByConnectorOnlyX(Line line, object connItem, double x)
1826
        {
1827
            Connector connector = line.CONNECTORS.Find(z => z.ConnectedObject == connItem);
1828
            int index = line.CONNECTORS.IndexOf(connector);
1829
            if (index == 0)
1830
            {
1831
                line.SPPID.START_X = x;
1832
                if (line.SlopeType == SlopeType.VERTICAL)
1833
                    line.SPPID.END_X = x;
1834
            }
1835
            else
1836
            {
1837
                line.SPPID.END_X = x;
1838
                if (line.SlopeType == SlopeType.VERTICAL)
1839
                    line.SPPID.START_X = x;
1840
            }
1841
        }
1842

    
1843
        private void ChangeLineSPPIDCoordinateByConnectorOnlyY(Line line, object connItem, double y)
1844
        {
1845
            Connector connector = line.CONNECTORS.Find(z => z.ConnectedObject == connItem);
1846
            int index = line.CONNECTORS.IndexOf(connector);
1847
            if (index == 0)
1848
            {
1849
                line.SPPID.START_Y = y;
1850
                if (line.SlopeType == SlopeType.HORIZONTAL)
1851
                    line.SPPID.END_Y = y;
1852
            }
1853
            else
1854
            {
1855
                line.SPPID.END_Y = y;
1856
                if (line.SlopeType == SlopeType.HORIZONTAL)
1857
                    line.SPPID.START_Y = y;
1858
            }
1859
        }
1860

    
1861
        private void NeedReModeling(Line line, LMSymbol symbol, ref bool result)
1862
        {
1863
            if (symbol != null)
1864
            {
1865
                string repID = symbol.AsLMRepresentation().Id;
1866
                string symbolUID = SPPIDUtil.FindSymbolByRepresentationID(document, repID).UID;
1867
                string lineUID = line.UID;
1868

    
1869
                SpecBreak startSpecBreak = document.SpecBreaks.Find(x =>
1870
                (x.DownStreamUID == symbolUID || x.UpStreamUID == symbolUID) &&
1871
                (x.DownStreamUID == lineUID || x.UpStreamUID == lineUID));
1872

    
1873
                EndBreak startEndBreak = document.EndBreaks.Find(x =>
1874
                (x.OWNER == symbolUID || x.PROPERTIES.Find(y => y.ATTRIBUTE == "Connected Item").VALUE == symbolUID) &&
1875
                (x.OWNER == lineUID || x.PROPERTIES.Find(y => y.ATTRIBUTE == "Connected Item").VALUE == lineUID));
1876

    
1877
                if (startSpecBreak != null || startEndBreak != null)
1878
                    result = true;
1879
            }
1880
        }
1881

    
1882
        /// <summary>
1883
        /// Symbol에 붙을 경우 Line을 Remodeling 한다.
1884
        /// </summary>
1885
        /// <param name="lines"></param>
1886
        /// <param name="prevLMConnector"></param>
1887
        /// <param name="startSymbol"></param>
1888
        /// <param name="endSymbol"></param>
1889
        private void ReModelingLine(Line line, LMConnector prevLMConnector, LMSymbol startSymbol, LMSymbol endSymbol, bool bStart, bool bEnd)
1890
        {
1891
            string symbolPath = string.Empty;
1892
            #region get symbol path
1893
            LMModelItem modelItem = dataSource.GetModelItem(prevLMConnector.ModelItemID);
1894
            foreach (LMRepresentation rep in modelItem.Representations)
1895
            {
1896
                if (!DBNull.Value.Equals(rep.get_FileName()) && !string.IsNullOrEmpty(rep.get_FileName()))
1897
                {
1898
                    symbolPath = rep.get_FileName();
1899
                    break;
1900
                }
1901
            }
1902
            #endregion
1903
            bool diagonal = false;
1904
            if (line.SlopeType != SlopeType.HORIZONTAL && line.SlopeType != SlopeType.VERTICAL)
1905
                diagonal = true;
1906
            _LMAItem _LMAItem = _placement.PIDCreateItem(symbolPath);
1907
            LMConnector newConnector = null;
1908
            dynamic OID = prevLMConnector.get_GraphicOID().ToString();
1909
            DependencyObject drawingObject = radApp.ActiveDocument.ActiveSheet.DrawingObjects[OID];
1910
            Ingr.RAD2D.LineStringGeometry2d lineStringGeometry = drawingObject.GetGeometry() as Ingr.RAD2D.LineStringGeometry2d;
1911
            int verticesCount = lineStringGeometry.VertexCount;
1912
            PlaceRunInputs placeRunInputs = new PlaceRunInputs();
1913

    
1914
            List<double[]> vertices = new List<double[]>();
1915
            for (int i = 1; i <= verticesCount; i++)
1916
            {
1917
                double x = 0;
1918
                double y = 0;
1919
                lineStringGeometry.GetVertex(i, ref x, ref y);
1920
                vertices.Add(new double[] { x, y });
1921
            }
1922

    
1923
            for (int i = 0; i < vertices.Count; i++)
1924
            {
1925
                double[] points = vertices[i];
1926
                // 시작 심볼이 있고 첫번째 좌표일 때
1927
                if (startSymbol != null && i == 0)
1928
                {
1929
                    if (bStart)
1930
                    {
1931
                        SlopeType slopeType = SPPIDUtil.CalcSlope(points[0], points[1], vertices[i + 1][0], vertices[i + 1][1]);
1932
                        if (slopeType == SlopeType.HORIZONTAL)
1933
                            placeRunInputs.AddPoint(points[0], -0.1);
1934
                        else if (slopeType == SlopeType.VERTICAL)
1935
                            placeRunInputs.AddPoint(-0.1, points[1]);
1936
                        else
1937
                            placeRunInputs.AddPoint(points[0], -0.1);
1938

    
1939
                        placeRunInputs.AddPoint(points[0], points[1]);
1940
                    }
1941
                    else
1942
                    {
1943
                        placeRunInputs.AddSymbolTarget(startSymbol, points[0], points[1], diagonal);
1944
                    }
1945
                }
1946
                // 마지막 심볼이 있고 마지막 좌표일 때
1947
                else if (endSymbol != null && i == vertices.Count - 1)
1948
                {
1949
                    if (bEnd)
1950
                    {
1951
                        placeRunInputs.AddPoint(points[0], points[1]);
1952

    
1953
                        SlopeType slopeType = SPPIDUtil.CalcSlope(points[0], points[1], vertices[i - 1][0], vertices[i - 1][1]);
1954
                        if (slopeType == SlopeType.HORIZONTAL)
1955
                            placeRunInputs.AddPoint(points[0], -0.1);
1956
                        else if (slopeType == SlopeType.VERTICAL)
1957
                            placeRunInputs.AddPoint(-0.1, points[1]);
1958
                        else
1959
                            placeRunInputs.AddPoint(points[0], -0.1);
1960
                    }
1961
                    else
1962
                    {
1963
                        placeRunInputs.AddSymbolTarget(endSymbol, points[0], points[1], diagonal);
1964
                    }
1965
                }
1966
                // 첫번째이며 시작 심볼이 아니고 Connecotr일 경우
1967
                else if (i == 0 && prevLMConnector.ConnectItem1SymbolObject != null)
1968
                    placeRunInputs.AddSymbolTarget(prevLMConnector.ConnectItem1SymbolObject, points[0], points[1], diagonal);
1969
                // 마지막이며 마지막 심볼이 아니고 Connecotr일 경우
1970
                else if (i == vertices.Count - 1 && prevLMConnector.ConnectItem2SymbolObject != null)
1971
                    placeRunInputs.AddSymbolTarget(prevLMConnector.ConnectItem2SymbolObject, points[0], points[1], diagonal);
1972
                else
1973
                    placeRunInputs.AddPoint(points[0], points[1]);
1974
            }
1975

    
1976
            _placement.PIDRemovePlacement(prevLMConnector.AsLMRepresentation());
1977
            newConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
1978

    
1979
            ReleaseCOMObjects(placeRunInputs);
1980
            ReleaseCOMObjects(_LMAItem);
1981
            ReleaseCOMObjects(modelItem);
1982

    
1983
            if (newConnector != null)
1984
            {
1985
                if (startSymbol != null && bStart)
1986
                {
1987
                    _LMAItem = _placement.PIDCreateItem(symbolPath);
1988
                    placeRunInputs = new PlaceRunInputs();
1989
                    placeRunInputs.AddSymbolTarget(startSymbol, vertices[0][0], vertices[0][1]);
1990
                    placeRunInputs.AddConnectorTarget(newConnector, vertices[0][0], vertices[0][1]);
1991
                    LMConnector _LMConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
1992
                    if (_LMConnector != null)
1993
                    {
1994
                        RemoveConnectorForReModelingLine(newConnector);
1995
                        ZeroLengthModelItemID.Add(_LMConnector.ModelItemID);
1996
                        ReleaseCOMObjects(_LMConnector);
1997
                    }
1998
                    ReleaseCOMObjects(placeRunInputs);
1999
                    ReleaseCOMObjects(_LMAItem);
2000
                }
2001

    
2002
                if (endSymbol != null && bEnd)
2003
                {
2004
                    if (startSymbol != null)
2005
                    {
2006
                        Dictionary<LMConnector, List<double[]>> dicVertices = GetPipeRunVertices(newConnector.ModelItemID);
2007
                        newConnector = dicVertices.First().Key;
2008
                    }
2009

    
2010
                    _LMAItem = _placement.PIDCreateItem(symbolPath);
2011
                    placeRunInputs = new PlaceRunInputs();
2012
                    placeRunInputs.AddSymbolTarget(endSymbol, vertices[vertices.Count - 1][0], vertices[vertices.Count - 1][1]);
2013
                    placeRunInputs.AddConnectorTarget(newConnector, vertices[vertices.Count - 1][0], vertices[vertices.Count - 1][1]);
2014
                    LMConnector _LMConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
2015
                    if (_LMConnector != null)
2016
                    {
2017
                        RemoveConnectorForReModelingLine(newConnector);
2018
                        ZeroLengthModelItemIDReverse.Add(_LMConnector.ModelItemID);
2019
                        ReleaseCOMObjects(_LMConnector);
2020
                    }
2021
                    ReleaseCOMObjects(placeRunInputs);
2022
                    ReleaseCOMObjects(_LMAItem);
2023
                }
2024

    
2025
                line.SPPID.ModelItemId = newConnector.ModelItemID;
2026
                ReleaseCOMObjects(newConnector);
2027
            }
2028

    
2029
            ReleaseCOMObjects(modelItem);
2030
        }
2031

    
2032
        /// <summary>
2033
        /// Remodeling 과정에서 생긴 불필요한 Connector 제거
2034
        /// </summary>
2035
        /// <param name="connector"></param>
2036
        private void RemoveConnectorForReModelingLine(LMConnector connector)
2037
        {
2038
            Dictionary<LMConnector, List<double[]>> dicVertices = GetPipeRunVertices(connector.ModelItemID);
2039
            foreach (var item in dicVertices)
2040
            {
2041
                if (item.Value.Count == 2)
2042
                {
2043
                    bool result = false;
2044
                    foreach (var point in item.Value)
2045
                    {
2046
                        if (point[0] < 0 || point[1] < 0)
2047
                        {
2048
                            result = true;
2049
                            _placement.PIDRemovePlacement(item.Key.AsLMRepresentation());
2050
                            break;
2051
                        }
2052
                    }
2053

    
2054
                    if (result)
2055
                        break;
2056
                }
2057
            }
2058
            foreach (var item in dicVertices)
2059
                ReleaseCOMObjects(item.Key);
2060
        }
2061

    
2062
        /// <summary>
2063
        /// Symbol이 모델링된 SPPPID Symbol Object를 반환 - 연결된 Symbol이 ChildSymbol일 수도 있기때문에 메서드 개발
2064
        /// </summary>
2065
        /// <param name="symbol"></param>
2066
        /// <param name="line"></param>
2067
        /// <returns></returns>
2068
        private LMSymbol GetTargetSymbol(Symbol symbol, Line line)
2069
        {
2070
            LMSymbol _LMSymbol = null;
2071
            foreach (var connector in symbol.CONNECTORS)
2072
            {
2073
                if (connector.CONNECTEDITEM == line.UID)
2074
                {
2075
                    if (connector.Index == 0)
2076
                        _LMSymbol = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
2077
                    else
2078
                    {
2079
                        ChildSymbol child = null;
2080
                        foreach (var childSymbol in symbol.ChildSymbols)
2081
                        {
2082
                            if (childSymbol.Connectors.Contains(connector))
2083
                                child = childSymbol;
2084
                            else
2085
                                child = GetChildSymbolByConnector(childSymbol, connector);
2086

    
2087
                            if (child != null)
2088
                                break;
2089
                        }
2090

    
2091
                        if (child != null)
2092
                            _LMSymbol = dataSource.GetSymbol(child.SPPID.RepresentationId);
2093
                    }
2094

    
2095
                    break;
2096
                }
2097
            }
2098

    
2099
            return _LMSymbol;
2100
        }
2101

    
2102
        /// <summary>
2103
        /// Connector를 가지고 있는 ChildSymbol Object 반환
2104
        /// </summary>
2105
        /// <param name="item"></param>
2106
        /// <param name="connector"></param>
2107
        /// <returns></returns>
2108
        private ChildSymbol GetChildSymbolByConnector(ChildSymbol item, Connector connector)
2109
        {
2110
            foreach (var childSymbol in item.ChildSymbols)
2111
            {
2112
                if (childSymbol.Connectors.Contains(connector))
2113
                    return childSymbol;
2114
                else
2115
                    return GetChildSymbolByConnector(childSymbol, connector);
2116
            }
2117

    
2118
            return null;
2119
        }
2120

    
2121
        /// <summary>
2122
        /// EndBreak 모델링 메서드
2123
        /// </summary>
2124
        /// <param name="endBreak"></param>
2125
        private void EndBreakModeling(EndBreak endBreak)
2126
        {
2127
            object ownerObj = SPPIDUtil.FindObjectByUID(document, endBreak.OWNER);
2128
            object connectedItem = SPPIDUtil.FindObjectByUID(document, endBreak.PROPERTIES.Find(x => x.ATTRIBUTE == "Connected Item").VALUE);
2129
            LMConnector targetLMConnector = FindBreakLineTarget(ownerObj, connectedItem);
2130

    
2131
            if (targetLMConnector != null)
2132
            {
2133
                Array array = new double[] { 0, endBreak.SPPID.ORIGINAL_X, endBreak.SPPID.ORIGINAL_Y };
2134
                LMLabelPersist _LmLabelPersist = _placement.PIDPlaceLabel(endBreak.SPPID.MAPPINGNAME, ref array, Rotation: 0, LabeledItem: targetLMConnector.AsLMRepresentation(), IsLeaderVisible: true);
2135
                if (_LmLabelPersist != null)
2136
                {
2137
                    endBreak.SPPID.RepresentationId = _LmLabelPersist.AsLMRepresentation().Id;
2138
                    if (_LmLabelPersist.ModelItemObject != null)
2139
                        endBreak.SPPID.ModelItemID = _LmLabelPersist.ModelItemID;
2140
                    endBreak.SPPID.GraphicOID = _LmLabelPersist.get_GraphicOID().ToString();
2141
                    ReleaseCOMObjects(_LmLabelPersist);
2142
                }
2143
                ReleaseCOMObjects(targetLMConnector);
2144
            }
2145
        }
2146

    
2147
        private LMConnector ReModelingLMConnector(LMConnector connector)
2148
        {
2149
            string symbolPath = string.Empty;
2150
            #region get symbol path
2151
            LMModelItem modelItem = dataSource.GetModelItem(connector.ModelItemID);
2152
            foreach (LMRepresentation rep in modelItem.Representations)
2153
            {
2154
                if (!DBNull.Value.Equals(rep.get_FileName()) && !string.IsNullOrEmpty(rep.get_FileName()))
2155
                {
2156
                    symbolPath = rep.get_FileName();
2157
                    break;
2158
                }
2159
            }
2160
            #endregion
2161

    
2162
            LMConnector newConnector = null;
2163
            dynamic OID = connector.get_GraphicOID().ToString();
2164
            DependencyObject drawingObject = radApp.ActiveDocument.ActiveSheet.DrawingObjects[OID];
2165
            Ingr.RAD2D.LineStringGeometry2d lineStringGeometry = drawingObject.GetGeometry() as Ingr.RAD2D.LineStringGeometry2d;
2166
            int verticesCount = lineStringGeometry.VertexCount;
2167
            PlaceRunInputs placeRunInputs = new PlaceRunInputs();
2168
            _LMAItem _LMAItem = _placement.PIDCreateItem(symbolPath);
2169

    
2170
            if (Convert.ToBoolean(connector.get_IsZeroLength()))
2171
            {
2172
                double[] vertices = null;
2173
                lineStringGeometry.GetVertices(ref verticesCount, ref vertices);
2174
                double x = 0;
2175
                double y = 0;
2176
                lineStringGeometry.GetVertex(1, ref x, ref y);
2177

    
2178
                placeRunInputs.AddSymbolTarget(connector.ConnectItem1SymbolObject, x, y);
2179
                placeRunInputs.AddSymbolTarget(connector.ConnectItem2SymbolObject, x, y);
2180

    
2181
                string flowDirection = string.Empty;
2182
                LMAAttribute flowAttribute = connector.ModelItemObject.Attributes["FlowDirection"];
2183
                if (flowAttribute != null && !DBNull.Value.Equals(flowAttribute.get_Value()))
2184
                    flowDirection = flowAttribute.get_Value().ToString();
2185

    
2186
                _placement.PIDRemovePlacement(connector.AsLMRepresentation());
2187
                newConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
2188
                ZeroLengthSymbolToSymbolModelItemID.Add(newConnector.ModelItemID);
2189
                if (!string.IsNullOrEmpty(flowDirection))
2190
                    newConnector.ModelItemObject.Attributes["FlowDirection"].set_Value(flowDirection);
2191
            }
2192
            else
2193
            {
2194
                List<double[]> vertices = new List<double[]>();
2195
                for (int i = 1; i <= verticesCount; i++)
2196
                {
2197
                    double x = 0;
2198
                    double y = 0;
2199
                    lineStringGeometry.GetVertex(i, ref x, ref y);
2200
                    vertices.Add(new double[] { x, y });
2201
                }
2202

    
2203
                for (int i = 0; i < vertices.Count; i++)
2204
                {
2205
                    double[] points = vertices[i];
2206
                    if (i == 0)
2207
                    {
2208
                        if (connector.ConnectItem1SymbolObject != null)
2209
                            placeRunInputs.AddSymbolTarget(connector.ConnectItem1SymbolObject, points[0], points[1]);
2210
                        else
2211
                            placeRunInputs.AddPoint(points[0], points[1]);
2212
                    }
2213
                    else if (i == vertices.Count - 1)
2214
                    {
2215
                        if (connector.ConnectItem2SymbolObject != null)
2216
                            placeRunInputs.AddSymbolTarget(connector.ConnectItem2SymbolObject, points[0], points[1]);
2217
                        else
2218
                            placeRunInputs.AddPoint(points[0], points[1]);
2219
                    }
2220
                    else
2221
                        placeRunInputs.AddPoint(points[0], points[1]);
2222
                }
2223

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

    
2226
                _placement.PIDRemovePlacement(connector.AsLMRepresentation());
2227
                newConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
2228

    
2229
                foreach (var line in lines)
2230
                    line.SPPID.ModelItemId = newConnector.ModelItemID;
2231
            }
2232

    
2233

    
2234
            return newConnector;
2235
        }
2236

    
2237
        /// <summary>
2238
        /// SpecBreak Modeling 메서드
2239
        /// </summary>
2240
        /// <param name="specBreak"></param>
2241
        private void SpecBreakModeling(SpecBreak specBreak)
2242
        {
2243
            object upStreamObj = SPPIDUtil.FindObjectByUID(document, specBreak.UpStreamUID);
2244
            object downStreamObj = SPPIDUtil.FindObjectByUID(document, specBreak.DownStreamUID);
2245

    
2246
            if (upStreamObj != null &&
2247
                downStreamObj != null)
2248
            {
2249
                LMConnector targetLMConnector = FindBreakLineTarget(upStreamObj, downStreamObj);
2250

    
2251
                if (targetLMConnector != null)
2252
                {
2253
                    foreach (var attribute in specBreak.ATTRIBUTES)
2254
                    {
2255
                        AttributeMapping mapping = document.AttributeMappings.Find(x => x.UID == attribute.UID);
2256
                        if (mapping != null && !string.IsNullOrEmpty(mapping.SPPIDSYMBOLNAME) && mapping.SPPIDSYMBOLNAME != "None")
2257
                        {
2258
                            string MappingPath = mapping.SPPIDSYMBOLNAME;
2259
                            Array array = new double[] { 0, specBreak.SPPID.ORIGINAL_X, specBreak.SPPID.ORIGINAL_Y };
2260
                            LMLabelPersist _LmLabelPersist = _placement.PIDPlaceLabel(MappingPath, ref array, Rotation: specBreak.ANGLE, LabeledItem: targetLMConnector.AsLMRepresentation(), IsLeaderVisible: mapping.LeaderLine);
2261

    
2262
                            if (_LmLabelPersist != null)
2263
                            {
2264
                                specBreak.SPPID.RepresentationId = _LmLabelPersist.AsLMRepresentation().Id;
2265
                                if (_LmLabelPersist.ModelItemObject != null)
2266
                                    specBreak.SPPID.ModelItemID = _LmLabelPersist.ModelItemID;
2267
                                specBreak.SPPID.GraphicOID = _LmLabelPersist.get_GraphicOID().ToString();
2268
                                ReleaseCOMObjects(_LmLabelPersist);
2269
                            }
2270
                        }
2271
                    }
2272
                    ReleaseCOMObjects(targetLMConnector);
2273
                }
2274
            }
2275
        }
2276

    
2277
        private LMConnector FindBreakLineTarget(object targetObj, object connectedObj)
2278
        {
2279
            LMConnector targetConnector = null;
2280
            Symbol targetSymbol = targetObj as Symbol;
2281
            Symbol connectedSymbol = connectedObj as Symbol;
2282
            Line targetLine = targetObj as Line;
2283
            Line connectedLine = connectedObj as Line;
2284
            if (targetSymbol != null && connectedSymbol != null)
2285
            {
2286
                LMSymbol targetLMSymbol = dataSource.GetSymbol(targetSymbol.SPPID.RepresentationId);
2287
                LMSymbol connectedLMSymbol = dataSource.GetSymbol(connectedSymbol.SPPID.RepresentationId);
2288

    
2289
                foreach (LMConnector connector in targetLMSymbol.Avoid1Connectors)
2290
                {
2291
                    if (connector.get_ItemStatus() != "Active")
2292
                        continue;
2293

    
2294
                    if (connector.ConnectItem1SymbolObject.Id == connectedLMSymbol.Id)
2295
                    {
2296
                        targetConnector = connector;
2297
                        break;
2298
                    }
2299
                    else if (connector.ConnectItem2SymbolObject.Id == connectedLMSymbol.Id)
2300
                    {
2301
                        targetConnector = connector;
2302
                        break;
2303
                    }
2304
                }
2305

    
2306
                foreach (LMConnector connector in targetLMSymbol.Avoid2Connectors)
2307
                {
2308
                    if (connector.get_ItemStatus() != "Active")
2309
                        continue;
2310

    
2311
                    if (connector.ConnectItem1SymbolObject.Id == connectedLMSymbol.Id)
2312
                    {
2313
                        targetConnector = connector;
2314
                        break;
2315
                    }
2316
                    else if (connector.ConnectItem2SymbolObject.Id == connectedLMSymbol.Id)
2317
                    {
2318
                        targetConnector = connector;
2319
                        break;
2320
                    }
2321
                }
2322

    
2323
                ReleaseCOMObjects(targetLMSymbol);
2324
                ReleaseCOMObjects(connectedLMSymbol);
2325
            }
2326
            else if (targetLine != null && connectedLine != null)
2327
            {
2328
                LMModelItem targetModelItem = dataSource.GetModelItem(targetLine.SPPID.ModelItemId);
2329
                LMModelItem connectedModelItem = dataSource.GetModelItem(connectedLine.SPPID.ModelItemId);
2330

    
2331
                if (targetModelItem != null && connectedModelItem != null)
2332
                {
2333
                    foreach (LMRepresentation rep in targetModelItem.Representations)
2334
                    {
2335
                        if (targetConnector != null)
2336
                            break;
2337

    
2338
                        if (rep.Attributes["RepresentationType"].get_Value() == "Connector" && rep.Attributes["ItemStatus"].get_Value() == "Active")
2339
                        {
2340
                            LMConnector _LMConnector = dataSource.GetConnector(rep.Id);
2341

    
2342
                            if (IsConnected(_LMConnector, connectedModelItem))
2343
                                targetConnector = _LMConnector;
2344
                            else
2345
                                ReleaseCOMObjects(_LMConnector);
2346
                        }
2347
                    }
2348

    
2349
                    ReleaseCOMObjects(targetModelItem);
2350
                }
2351
            }
2352
            else
2353
            {
2354
                LMSymbol connectedLMSymbol = null;
2355
                if (connectedSymbol != null)
2356
                    connectedLMSymbol = dataSource.GetSymbol(connectedSymbol.SPPID.RepresentationId);
2357
                else if (targetSymbol != null)
2358
                    connectedLMSymbol = dataSource.GetSymbol(targetSymbol.SPPID.RepresentationId);
2359
                else
2360
                {
2361

    
2362
                }
2363
                LMModelItem targetModelItem = null;
2364
                if (targetLine != null)
2365
                    targetModelItem = dataSource.GetModelItem(targetLine.SPPID.ModelItemId);
2366
                else if (connectedLine != null)
2367
                    targetModelItem = dataSource.GetModelItem(connectedLine.SPPID.ModelItemId);
2368
                else
2369
                {
2370

    
2371
                }
2372
                if (connectedLMSymbol != null && targetModelItem != null)
2373
                {
2374
                    foreach (LMConnector connector in connectedLMSymbol.Avoid1Connectors)
2375
                    {
2376
                        if (connector.get_ItemStatus() != "Active")
2377
                            continue;
2378

    
2379
                        if (IsConnected(connector, targetModelItem))
2380
                        {
2381
                            targetConnector = connector;
2382
                            break;
2383
                        }
2384
                    }
2385

    
2386
                    if (targetConnector == null)
2387
                    {
2388
                        foreach (LMConnector connector in connectedLMSymbol.Avoid2Connectors)
2389
                        {
2390
                            if (connector.get_ItemStatus() != "Active")
2391
                                continue;
2392

    
2393
                            if (IsConnected(connector, targetModelItem))
2394
                            {
2395
                                targetConnector = connector;
2396
                                break;
2397
                            }
2398
                        }
2399
                    }
2400
                }
2401

    
2402
            }
2403

    
2404
            return targetConnector;
2405
        }
2406

    
2407
        private bool IsConnected(LMConnector connector, LMModelItem modelItem)
2408
        {
2409
            bool result = false;
2410

    
2411
            foreach (LMRepresentation rep in modelItem.Representations)
2412
            {
2413
                if (result)
2414
                    break;
2415

    
2416
                if (rep.Attributes["RepresentationType"].get_Value() == "Connector" && rep.Attributes["ItemStatus"].get_Value() == "Active")
2417
                {
2418
                    LMConnector _LMConnector = dataSource.GetConnector(rep.Id);
2419

    
2420
                    if (_LMConnector.ConnectItem1SymbolObject != null &&
2421
                        connector.ConnectItem1SymbolObject != null &&
2422
                        _LMConnector.ConnectItem1SymbolObject.Id == connector.ConnectItem1SymbolObject.Id)
2423
                    {
2424
                        result = true;
2425
                        ReleaseCOMObjects(_LMConnector);
2426
                        break;
2427
                    }
2428
                    else if (_LMConnector.ConnectItem1SymbolObject != null &&
2429
                        connector.ConnectItem2SymbolObject != null &&
2430
                        _LMConnector.ConnectItem1SymbolObject.Id == connector.ConnectItem2SymbolObject.Id)
2431
                    {
2432
                        result = true;
2433
                        ReleaseCOMObjects(_LMConnector);
2434
                        break;
2435
                    }
2436
                    else if (_LMConnector.ConnectItem2SymbolObject != null &&
2437
                        connector.ConnectItem1SymbolObject != null &&
2438
                        _LMConnector.ConnectItem2SymbolObject.Id == connector.ConnectItem1SymbolObject.Id)
2439
                    {
2440
                        result = true;
2441
                        ReleaseCOMObjects(_LMConnector);
2442
                        break;
2443
                    }
2444
                    else if (_LMConnector.ConnectItem2SymbolObject != null &&
2445
                        connector.ConnectItem2SymbolObject != null &&
2446
                        _LMConnector.ConnectItem2SymbolObject.Id == connector.ConnectItem2SymbolObject.Id)
2447
                    {
2448
                        result = true;
2449
                        ReleaseCOMObjects(_LMConnector);
2450
                        break;
2451
                    }
2452

    
2453
                    ReleaseCOMObjects(_LMConnector);
2454
                }
2455
            }
2456

    
2457

    
2458
            return result;
2459
        }
2460

    
2461
        /// <summary>
2462
        /// FromModelItem을 ToModelItem으로 PipeRunJoin하는 메서드
2463
        /// </summary>
2464
        /// <param name="modelItemID1"></param>
2465
        /// <param name="modelItemID2"></param>
2466
        private void JoinPipeRun(string modelItemID1, string modelItemID2)
2467
        {
2468
            LMModelItem modelItem1 = dataSource.GetModelItem(modelItemID1);
2469
            _LMAItem item1 = modelItem1.AsLMAItem();
2470
            LMModelItem modelItem2 = dataSource.GetModelItem(modelItemID2);
2471
            _LMAItem item2 = modelItem2.AsLMAItem();
2472

    
2473
            // item2가 item1으로 조인
2474
            _placement.PIDJoinRuns(ref item1, ref item2);
2475
            item1.Commit();
2476
            item2.Commit();
2477

    
2478
            string beforeID = string.Empty;
2479
            string afterID = string.Empty;
2480

    
2481
            if (modelItem1.get_ItemStatus() == "Active" && modelItem2.get_ItemStatus() != "Active")
2482
            {
2483
                beforeID = modelItem2.Id;
2484
                afterID = modelItem1.Id;
2485
            }
2486
            else if (modelItem1.get_ItemStatus() != "Active" && modelItem2.get_ItemStatus() == "Active")
2487
            {
2488
                beforeID = modelItem1.Id;
2489
                afterID = modelItem2.Id;
2490
            }
2491
            else
2492
            {
2493
                throw new Exception("확인 필요한 케이스");
2494
            }
2495

    
2496
            List<Line> lines = SPPIDUtil.FindLinesByModelId(document, beforeID);
2497
            foreach (var line in lines)
2498
                line.SPPID.ModelItemId = afterID;
2499

    
2500
            ReleaseCOMObjects(modelItem1);
2501
            ReleaseCOMObjects(item1);
2502
            ReleaseCOMObjects(modelItem2);
2503
            ReleaseCOMObjects(item2);
2504
        }
2505

    
2506
        /// <summary>
2507
        /// PipeRun을 자동으로 Join하는 메서드
2508
        /// </summary>
2509
        /// <param name="modelItemId"></param>
2510
        private void AutoJoinPipeRun(string modelItemId)
2511
        {
2512
            LMModelItem modelItem = dataSource.GetModelItem(modelItemId);
2513
            _LMAItem item = modelItem.AsLMAItem();
2514
            if (modelItem.get_ItemStatus() == "Active")
2515
            {
2516
                string modelitemID = item.Id;
2517
                _placement.PIDAutoJoin(item, AutoJoinEndConstants.autoJoin_Both, ref item);
2518
                string afterModelItemID = item.Id;
2519

    
2520
                if (modelitemID != afterModelItemID)
2521
                {
2522
                    List<Line> lines = SPPIDUtil.FindLinesByModelId(document, modelitemID);
2523
                    foreach (var line in lines)
2524
                        line.SPPID.ModelItemId = afterModelItemID;
2525
                }
2526
                item.Commit();
2527
            }
2528

    
2529
            ReleaseCOMObjects(modelItem);
2530
            ReleaseCOMObjects(item);
2531
        }
2532

    
2533
        /// <summary>
2534
        /// LineRun에 있는 Line들을 Join하는 진입 메서드
2535
        /// </summary>
2536
        /// <param name="run"></param>
2537
        private void JoinRunLine(LineRun run)
2538
        {
2539
            string modelItemId = string.Empty;
2540
            foreach (var item in run.RUNITEMS)
2541
            {
2542
                if (item.GetType() == typeof(Line))
2543
                {
2544
                    Line line = item as Line;
2545
                    AutoJoinPipeRun(line.SPPID.ModelItemId);
2546
                    modelItemId = line.SPPID.ModelItemId;
2547
                }
2548
            }
2549
        }
2550

    
2551
        /// <summary>
2552
        /// PipeRun의 좌표를 가져오는 메서드
2553
        /// </summary>
2554
        /// <param name="modelId"></param>
2555
        /// <returns></returns>
2556
        private Dictionary<LMConnector, List<double[]>> GetPipeRunVertices(string modelId)
2557
        {
2558
            Dictionary<LMConnector, List<double[]>> connectorVertices = new Dictionary<LMConnector, List<double[]>>();
2559
            LMModelItem modelItem = dataSource.GetModelItem(modelId);
2560

    
2561
            if (modelItem != null)
2562
            {
2563
                foreach (LMRepresentation rep in modelItem.Representations)
2564
                {
2565
                    if (rep.Attributes["RepresentationType"].get_Value() == "Connector" && rep.Attributes["ItemStatus"].get_Value() == "Active")
2566
                    {
2567
                        LMConnector _LMConnector = dataSource.GetConnector(rep.Id);
2568
                        connectorVertices.Add(_LMConnector, new List<double[]>());
2569
                        dynamic OID = rep.get_GraphicOID().ToString();
2570
                        DependencyObject drawingObject = radApp.ActiveDocument.ActiveSheet.DrawingObjects[OID];
2571
                        Ingr.RAD2D.LineStringGeometry2d lineStringGeometry = drawingObject.GetGeometry() as Ingr.RAD2D.LineStringGeometry2d;
2572
                        int verticesCount = lineStringGeometry.VertexCount;
2573
                        double[] vertices = null;
2574
                        lineStringGeometry.GetVertices(ref verticesCount, ref vertices);
2575
                        for (int i = 0; i < verticesCount; i++)
2576
                        {
2577
                            double x = 0;
2578
                            double y = 0;
2579
                            lineStringGeometry.GetVertex(i + 1, ref x, ref y);
2580
                            connectorVertices[_LMConnector].Add(new double[] { x, y });
2581
                        }
2582
                    }
2583
                }
2584

    
2585
                ReleaseCOMObjects(modelItem);
2586
            }
2587

    
2588
            return connectorVertices;
2589
        }
2590

    
2591
        private List<double[]> GetConnectorVertices(LMConnector connector)
2592
        {
2593
            List<double[]> vertices = new List<double[]>();
2594
            dynamic OID = connector.get_GraphicOID().ToString();
2595
            DependencyObject drawingObject = radApp.ActiveDocument.ActiveSheet.DrawingObjects[OID];
2596
            Ingr.RAD2D.LineStringGeometry2d lineStringGeometry = drawingObject.GetGeometry() as Ingr.RAD2D.LineStringGeometry2d;
2597
            int verticesCount = lineStringGeometry.VertexCount;
2598
            double[] value = null;
2599
            lineStringGeometry.GetVertices(ref verticesCount, ref value);
2600
            for (int i = 0; i < verticesCount; i++)
2601
            {
2602
                double x = 0;
2603
                double y = 0;
2604
                lineStringGeometry.GetVertex(i + 1, ref x, ref y);
2605
                vertices.Add(new double[] { x, y });
2606
            }
2607
            return vertices;
2608
        }
2609

    
2610
        /// <summary>
2611
        /// 좌표로 PipeRun의 Connector중에 어느 Connector에 가까운지/붙을지 가져오는 메서드 - 조건에 안맞아서 못찾을시 제일 가까운 점으로 가져오는 방식
2612
        /// </summary>
2613
        /// <param name="connectorVertices"></param>
2614
        /// <param name="connX"></param>
2615
        /// <param name="connY"></param>
2616
        /// <returns></returns>
2617
        private LMConnector FindTargetLMConnectorForLabel(Dictionary<LMConnector, List<double[]>> connectorVertices, double connX, double connY)
2618
        {
2619
            double length = double.MaxValue;
2620
            LMConnector targetConnector = null;
2621
            foreach (var item in connectorVertices)
2622
            {
2623
                List<double[]> points = item.Value;
2624
                for (int i = 0; i < points.Count - 1; i++)
2625
                {
2626
                    double[] point1 = points[i];
2627
                    double[] point2 = points[i + 1];
2628
                    double x1 = Math.Min(point1[0], point2[0]);
2629
                    double y1 = Math.Min(point1[1], point2[1]);
2630
                    double x2 = Math.Max(point1[0], point2[0]);
2631
                    double y2 = Math.Max(point1[1], point2[1]);
2632

    
2633
                    if ((x1 <= connX && x2 >= connX) ||
2634
                        (y1 <= connY && y2 >= connY))
2635
                    {
2636
                        double distance = SPPIDUtil.CalcPointToPointdDistance(point1[0], point1[1], connX, connY);
2637
                        if (length >= distance)
2638
                        {
2639
                            targetConnector = item.Key;
2640
                            length = distance;
2641
                        }
2642

    
2643
                        distance = SPPIDUtil.CalcPointToPointdDistance(point2[0], point2[1], connX, connY);
2644
                        if (length >= distance)
2645
                        {
2646
                            targetConnector = item.Key;
2647
                            length = distance;
2648
                        }
2649
                    }
2650
                }
2651
            }
2652

    
2653
            // 못찾았을때.
2654
            length = double.MaxValue;
2655
            if (targetConnector == null)
2656
            {
2657
                foreach (var item in connectorVertices)
2658
                {
2659
                    List<double[]> points = item.Value;
2660

    
2661
                    foreach (double[] point in points)
2662
                    {
2663
                        double distance = SPPIDUtil.CalcPointToPointdDistance(point[0], point[1], connX, connY);
2664
                        if (length >= distance)
2665
                        {
2666
                            targetConnector = item.Key;
2667
                            length = distance;
2668
                        }
2669
                    }
2670
                }
2671
            }
2672

    
2673
            return targetConnector;
2674
        }
2675

    
2676
        private LMConnector FindTargetLMConnectorForBranch(Line line, Line targetLine, ref double x, ref double y)
2677
        {
2678
            Dictionary<LMConnector, List<double[]>> vertices = GetPipeRunVertices(targetLine.SPPID.ModelItemId);
2679

    
2680
            double length = double.MaxValue;
2681
            LMConnector targetConnector = null;
2682
            double[] resultPoint = null;
2683
            List<double[]> targetVertices = null;
2684

    
2685
            // Vertices 포인트에 제일 가까운곳
2686
            foreach (var item in vertices)
2687
            {
2688
                List<double[]> points = item.Value;
2689
                for (int i = 0; i < points.Count; i++)
2690
                {
2691
                    double[] point = points[i];
2692
                    double tempX = point[0];
2693
                    double tempY = point[1];
2694

    
2695
                    double distance = SPPIDUtil.CalcPointToPointdDistance(tempX, tempY, x, y);
2696
                    if (length >= distance)
2697
                    {
2698
                        targetConnector = item.Key;
2699
                        length = distance;
2700
                        resultPoint = point;
2701
                        targetVertices = item.Value;
2702
                    }
2703
                }
2704
            }
2705

    
2706
            // Vertices Cross에 제일 가까운곳
2707
            foreach (var item in vertices)
2708
            {
2709
                List<double[]> points = item.Value;
2710
                for (int i = 0; i < points.Count - 1; i++)
2711
                {
2712
                    double[] point1 = points[i];
2713
                    double[] point2 = points[i + 1];
2714

    
2715
                    double maxLineX = Math.Max(point1[0], point2[0]);
2716
                    double minLineX = Math.Min(point1[0], point2[0]);
2717
                    double maxLineY = Math.Max(point1[1], point2[1]);
2718
                    double minLineY = Math.Min(point1[1], point2[1]);
2719

    
2720
                    SlopeType slope = SPPIDUtil.CalcSlope(minLineX, minLineY, maxLineX, maxLineY);
2721

    
2722
                    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]);
2723
                    if (crossingPoint != null)
2724
                    {
2725
                        double distance = SPPIDUtil.CalcPointToPointdDistance(crossingPoint[0], crossingPoint[1], x, y);
2726
                        if (length >= distance)
2727
                        {
2728
                            if (slope == SlopeType.Slope &&
2729
                                minLineX <= crossingPoint[0] && maxLineX >= crossingPoint[0] &&
2730
                                minLineY <= crossingPoint[1] && maxLineY >= crossingPoint[1])
2731
                            {
2732
                                targetConnector = item.Key;
2733
                                length = distance;
2734
                                resultPoint = crossingPoint;
2735
                                targetVertices = item.Value;
2736
                            }
2737
                            else if (slope == SlopeType.HORIZONTAL &&
2738
                                minLineX <= crossingPoint[0] && maxLineX >= crossingPoint[0])
2739
                            {
2740
                                targetConnector = item.Key;
2741
                                length = distance;
2742
                                resultPoint = crossingPoint;
2743
                                targetVertices = item.Value;
2744
                            }
2745
                            else if (slope == SlopeType.VERTICAL &&
2746
                               minLineY <= crossingPoint[1] && maxLineY >= crossingPoint[1])
2747
                            {
2748
                                targetConnector = item.Key;
2749
                                length = distance;
2750
                                resultPoint = crossingPoint;
2751
                                targetVertices = item.Value;
2752
                            }
2753
                        }
2754
                    }
2755
                }
2756
            }
2757

    
2758
            foreach (var item in vertices)
2759
                if (item.Key != null && item.Key != targetConnector)
2760
                    ReleaseCOMObjects(item.Key);
2761

    
2762
            if (SPPIDUtil.IsBranchLine(line, targetLine))
2763
            {
2764
                double tempResultX = resultPoint[0];
2765
                double tempResultY = resultPoint[1];
2766
                SPPIDUtil.ConvertGridPoint(ref tempResultX, ref tempResultY);
2767

    
2768
                GridSetting gridSetting = GridSetting.GetInstance();
2769

    
2770
                for (int i = 0; i < targetVertices.Count; i++)
2771
                {
2772
                    double[] point = targetVertices[i];
2773
                    double tempX = targetVertices[i][0];
2774
                    double tempY = targetVertices[i][1];
2775
                    SPPIDUtil.ConvertGridPoint(ref tempX, ref tempY);
2776
                    if (tempX == tempResultX && tempY == tempResultY)
2777
                    {
2778
                        if (i == 0)
2779
                        {
2780
                            LMSymbol connSymbol = targetConnector.ConnectItem1SymbolObject;
2781
                            bool containZeroLength = false;
2782
                            if (connSymbol != null)
2783
                            {
2784
                                foreach (LMConnector connector in connSymbol.Connect1Connectors)
2785
                                {
2786
                                    if (connector.get_ItemStatus() == "Active" && Convert.ToBoolean(connector.get_IsZeroLength()) == true)
2787
                                        containZeroLength = true;
2788
                                }
2789
                                foreach (LMConnector connector in connSymbol.Connect2Connectors)
2790
                                {
2791
                                    if (connector.get_ItemStatus() == "Active" && Convert.ToBoolean(connector.get_IsZeroLength()) == true)
2792
                                        containZeroLength = true;
2793
                                }
2794
                            }
2795

    
2796
                            if (connSymbol == null ||
2797
                                (connSymbol != null && connSymbol.get_ItemStatus() == "Active" && connSymbol.get_RepresentationType() != "Branch") ||
2798
                                containZeroLength)
2799
                            {
2800
                                bool bCalcX = false;
2801
                                bool bCalcY = false;
2802
                                if (targetLine.SlopeType == SlopeType.HORIZONTAL)
2803
                                    bCalcX = true;
2804
                                else if (targetLine.SlopeType == SlopeType.VERTICAL)
2805
                                    bCalcY = true;
2806
                                else
2807
                                {
2808
                                    bCalcX = true;
2809
                                    bCalcY = true;
2810
                                }
2811

    
2812
                                if (bCalcX)
2813
                                {
2814
                                    double nextX = targetVertices[i + 1][0];
2815
                                    double newX = 0;
2816
                                    if (nextX > tempX)
2817
                                    {
2818
                                        newX = tempX + gridSetting.Length;
2819
                                        if (newX > nextX)
2820
                                            newX = (point[0] + nextX) / 2;
2821
                                    }
2822
                                    else
2823
                                    {
2824
                                        newX = tempX - gridSetting.Length;
2825
                                        if (newX < nextX)
2826
                                            newX = (point[0] + nextX) / 2;
2827
                                    }
2828
                                    resultPoint = new double[] { newX, resultPoint[1] };
2829
                                }
2830

    
2831
                                if (bCalcY)
2832
                                {
2833
                                    double nextY = targetVertices[i + 1][1];
2834
                                    double newY = 0;
2835
                                    if (nextY > tempY)
2836
                                    {
2837
                                        newY = tempY + gridSetting.Length;
2838
                                        if (newY > nextY)
2839
                                            newY = (point[1] + nextY) / 2;
2840
                                    }
2841
                                    else
2842
                                    {
2843
                                        newY = tempY - gridSetting.Length;
2844
                                        if (newY < nextY)
2845
                                            newY = (point[1] + nextY) / 2;
2846
                                    }
2847
                                    resultPoint = new double[] { resultPoint[0], newY };
2848
                                }
2849
                            }
2850
                        }
2851
                        else if (i == targetVertices.Count - 1)
2852
                        {
2853
                            LMSymbol connSymbol = targetConnector.ConnectItem2SymbolObject;
2854
                            bool containZeroLength = false;
2855
                            if (connSymbol != null)
2856
                            {
2857
                                foreach (LMConnector connector in connSymbol.Connect1Connectors)
2858
                                {
2859
                                    if (connector.get_ItemStatus() == "Active" && Convert.ToBoolean(connector.get_IsZeroLength()) == true)
2860
                                        containZeroLength = true;
2861
                                }
2862
                                foreach (LMConnector connector in connSymbol.Connect2Connectors)
2863
                                {
2864
                                    if (connector.get_ItemStatus() == "Active" && Convert.ToBoolean(connector.get_IsZeroLength()) == true)
2865
                                        containZeroLength = true;
2866
                                }
2867
                            }
2868

    
2869
                            if (connSymbol == null ||
2870
                                 (connSymbol != null && connSymbol.get_ItemStatus() == "Active" && connSymbol.get_RepresentationType() != "Branch") ||
2871
                                containZeroLength)
2872
                            {
2873
                                bool bCalcX = false;
2874
                                bool bCalcY = false;
2875
                                if (targetLine.SlopeType == SlopeType.HORIZONTAL)
2876
                                    bCalcX = true;
2877
                                else if (targetLine.SlopeType == SlopeType.VERTICAL)
2878
                                    bCalcY = true;
2879
                                else
2880
                                {
2881
                                    bCalcX = true;
2882
                                    bCalcY = true;
2883
                                }
2884

    
2885
                                if (bCalcX)
2886
                                {
2887
                                    double nextX = targetVertices[i - 1][0];
2888
                                    double newX = 0;
2889
                                    if (nextX > tempX)
2890
                                    {
2891
                                        newX = tempX + gridSetting.Length;
2892
                                        if (newX > nextX)
2893
                                            newX = (point[0] + nextX) / 2;
2894
                                    }
2895
                                    else
2896
                                    {
2897
                                        newX = tempX - gridSetting.Length;
2898
                                        if (newX < nextX)
2899
                                            newX = (point[0] + nextX) / 2;
2900
                                    }
2901
                                    resultPoint = new double[] { newX, resultPoint[1] };
2902
                                }
2903

    
2904
                                if (bCalcY)
2905
                                {
2906
                                    double nextY = targetVertices[i - 1][1];
2907
                                    double newY = 0;
2908
                                    if (nextY > tempY)
2909
                                    {
2910
                                        newY = tempY + gridSetting.Length;
2911
                                        if (newY > nextY)
2912
                                            newY = (point[1] + nextY) / 2;
2913
                                    }
2914
                                    else
2915
                                    {
2916
                                        newY = tempY - gridSetting.Length;
2917
                                        if (newY < nextY)
2918
                                            newY = (point[1] + nextY) / 2;
2919
                                    }
2920
                                    resultPoint = new double[] { resultPoint[0], newY };
2921
                                }
2922
                            }
2923
                        }
2924
                        break;
2925
                    }
2926
                }
2927
            }
2928

    
2929
            x = resultPoint[0];
2930
            y = resultPoint[1];
2931

    
2932
            return targetConnector;
2933
        }
2934

    
2935
        private LMConnector GetLMConnectorOnlyOne(string modelItemID)
2936
        {
2937
            LMConnector result = null;
2938
            List<LMConnector> connectors = new List<LMConnector>();
2939
            LMModelItem modelItem = dataSource.GetModelItem(modelItemID);
2940

    
2941
            if (modelItem != null)
2942
            {
2943
                foreach (LMRepresentation rep in modelItem.Representations)
2944
                {
2945
                    if (rep.Attributes["RepresentationType"].get_Value() == "Connector" && rep.Attributes["ItemStatus"].get_Value() == "Active")
2946
                        connectors.Add(dataSource.GetConnector(rep.Id));
2947
                }
2948

    
2949
                ReleaseCOMObjects(modelItem);
2950
            }
2951

    
2952
            if (connectors.Count == 1)
2953
                result = connectors[0];
2954
            else
2955
                foreach (var item in connectors)
2956
                    ReleaseCOMObjects(item);
2957

    
2958
            return result;
2959
        }
2960

    
2961
        /// <summary>
2962
        /// Line Number Symbol을 실제로 Modeling하는 메서드
2963
        /// </summary>
2964
        /// <param name="lineNumber"></param>
2965
        private void LineNumberModeling(Line line)
2966
        {
2967
            LineNumber lineNumber = document.LINENUMBERS.Find(x => x.CONNLINE == line.UID);
2968
            if (lineNumber != null)
2969
            {
2970
                LMConnector connectedLMConnector = GetLMConnectorOnlyOne(line.SPPID.ModelItemId);
2971
                if (connectedLMConnector != null)
2972
                {
2973
                    double x = 0;
2974
                    double y = 0;
2975
                    CalcLabelLocation(ref x, ref y, lineNumber.SPPID.ORIGINAL_X, lineNumber.SPPID.ORIGINAL_Y, lineNumber.SPPIDLabelLocation, _ETCSetting.LineNumberLocation);
2976

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

    
2980
                    if (_LmLabelPresist != null)
2981
                    {
2982
                        _LmLabelPresist.Commit();
2983
                        lineNumber.SPPID.RepresentationId = _LmLabelPresist.AsLMRepresentation().Id;
2984
                        ReleaseCOMObjects(_LmLabelPresist);
2985
                    }
2986
                }
2987
            }
2988
        }
2989
        /// <summary>
2990
        /// Flow Mark Modeling
2991
        /// </summary>
2992
        /// <param name="line"></param>
2993
        private void FlowMarkModeling(Line line)
2994
        {
2995
            if (line.FLOWMARK && !string.IsNullOrEmpty(line.SPPID.ModelItemId) && !string.IsNullOrEmpty(_ETCSetting.FlowMarkSymbolPath))
2996
            {
2997
                LMConnector connector = GetLMConnectorOnlyOne(line.SPPID.ModelItemId);
2998
                if (connector != null)
2999
                {
3000
                    string mappingPath = _ETCSetting.FlowMarkSymbolPath;
3001
                    List<double[]> vertices = GetConnectorVertices(connector);
3002
                    vertices = vertices.FindAll(x => x[0] > 0 && x[1] > 0);
3003
                    double[] point = vertices[vertices.Count - 1];
3004
                    Array array = new double[] { 0, point[0], point[1] };
3005
                    LMLabelPersist _LMLabelPersist = _placement.PIDPlaceLabel(mappingPath, ref array, LabeledItem: connector.AsLMRepresentation());
3006
                    if (_LMLabelPersist != null)
3007
                        ReleaseCOMObjects(_LMLabelPersist);
3008
                }
3009
            }
3010
        }
3011

    
3012
        /// <summary>
3013
        /// Line Number 기준으로 모든 Item에 Line Number의 Attribute Input
3014
        /// </summary>
3015
        /// <param name="lineNumber"></param>
3016
        private void InputLineNumberAttribute(LineNumber lineNumber)
3017
        {
3018
            foreach (LineRun run in lineNumber.RUNS)
3019
            {
3020
                foreach (var item in run.RUNITEMS)
3021
                {
3022
                    //if (item.GetType() == typeof(Symbol))
3023
                    //{
3024
                    //    Symbol symbol = item as Symbol;
3025
                    //    LMSymbol _LMSymbol = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
3026
                    //    if (_LMSymbol != null)
3027
                    //    {
3028
                    //        LMModelItem _LMModelItem = _LMSymbol.ModelItemObject;
3029

    
3030
                    //        if (_LMModelItem != null && _LMModelItem.get_ItemStatus() == "Active")
3031
                    //        {
3032
                    //            foreach (var attribute in lineNumber.ATTRIBUTES)
3033
                    //            {
3034
                    //                LineNumberMapping mapping = document.LineNumberMappings.Find(x => x.UID == attribute.UID);
3035
                    //                if (mapping != null && !string.IsNullOrEmpty(attribute.VALUE) && attribute.VALUE != "None")
3036
                    //                {
3037
                    //                    LMAAttribute _LMAAttribute = _LMModelItem.Attributes[mapping.SPPIDATTRIBUTENAME];
3038
                    //                    if (_LMAAttribute != null)
3039
                    //                    {
3040
                    //                        if (DBNull.Value.Equals(_LMAAttribute.get_Value()))
3041
                    //                            _LMAAttribute.set_Value(attribute.VALUE);
3042
                    //                        else if (_LMAAttribute.get_Value() != attribute.VALUE)
3043
                    //                            _LMAAttribute.set_Value(attribute.VALUE);
3044
                    //                    }
3045
                    //                }
3046
                    //            }
3047
                    //            _LMModelItem.Commit();
3048
                    //        }
3049
                    //        if (_LMModelItem != null)
3050
                    //            ReleaseCOMObjects(_LMModelItem);
3051
                    //    }
3052
                    //    if (_LMSymbol != null)
3053
                    //        ReleaseCOMObjects(_LMSymbol);
3054
                    //}
3055
                    //else
3056
                    if (item.GetType() == typeof(Line))
3057
                    {
3058
                        Line line = item as Line;
3059
                        if (line != null)
3060
                        {
3061
                            LMModelItem _LMModelItem = dataSource.GetModelItem(line.SPPID.ModelItemId);
3062
                            if (_LMModelItem != null && _LMModelItem.get_ItemStatus() == "Active")
3063
                            {
3064
                                foreach (var attribute in lineNumber.ATTRIBUTES)
3065
                                {
3066
                                    LineNumberMapping mapping = document.LineNumberMappings.Find(x => x.UID == attribute.UID);
3067
                                    if (mapping != null && !string.IsNullOrEmpty(attribute.VALUE) && attribute.VALUE != "None")
3068
                                    {
3069
                                        LMAAttribute _LMAAttribute = _LMModelItem.Attributes[mapping.SPPIDATTRIBUTENAME];
3070
                                        if (_LMAAttribute != null)
3071
                                        {
3072
                                            if (DBNull.Value.Equals(_LMAAttribute.get_Value()))
3073
                                                _LMAAttribute.set_Value(attribute.VALUE);
3074
                                            else if (_LMAAttribute.get_Value() != attribute.VALUE)
3075
                                                _LMAAttribute.set_Value(attribute.VALUE);
3076

    
3077
                                        }
3078
                                    }
3079
                                }
3080
                                _LMModelItem.Commit();
3081
                            }
3082
                            if (_LMModelItem != null)
3083
                                ReleaseCOMObjects(_LMModelItem);
3084

    
3085
                            break;
3086
                        }
3087
                    }
3088
                }
3089
            }
3090
        }
3091

    
3092
        /// <summary>
3093
        /// Symbol Attribute 입력 메서드
3094
        /// </summary>
3095
        /// <param name="item"></param>
3096
        private void InputSymbolAttribute(object targetItem, List<BaseModel.Attribute> targetAttributes)
3097
        {
3098
            // Object 아이템이 Symbol일 경우 Equipment일 경우 
3099
            string sRep = null;
3100
            if (targetItem.GetType() == typeof(Symbol))
3101
                sRep = ((Symbol)targetItem).SPPID.RepresentationId;
3102
            else if (targetItem.GetType() == typeof(Equipment))
3103
                sRep = ((Equipment)targetItem).SPPID.RepresentationId;
3104

    
3105
            if (!string.IsNullOrEmpty(sRep))
3106
            {
3107
                LMSymbol _LMSymbol = dataSource.GetSymbol(sRep);
3108
                LMModelItem _LMModelItem = _LMSymbol.ModelItemObject;
3109
                LMAAttributes _Attributes = _LMModelItem.Attributes;
3110

    
3111
                foreach (var item in targetAttributes)
3112
                {
3113
                    AttributeMapping mapping = document.AttributeMappings.Find(x => x.UID == item.UID);
3114
                    if (mapping != null && !string.IsNullOrEmpty(item.VALUE) && item.VALUE != "None")
3115
                    {
3116
                        LMAAttribute _Attribute = _Attributes[mapping.SPPIDATTRIBUTENAME];
3117
                        if (_Attribute != null)
3118
                        {
3119
                            object associItem = SPPIDUtil.FindObjectByUID(document, item.ASSOCITEM);
3120
                            if (associItem != null)
3121
                            {
3122
                                if (associItem.GetType() == typeof(Text))
3123
                                {
3124
                                    Text text = associItem as Text;
3125
                                    text.SPPID.RepresentationId = "Attribute";
3126
                                }
3127
                                else if (associItem.GetType() == typeof(Note))
3128
                                {
3129
                                    Note note = associItem as Note;
3130
                                    note.SPPID.RepresentationId = "Attribute";
3131
                                }
3132
                            }
3133
                            _Attribute.set_Value(item.VALUE);
3134
                        }
3135
                            
3136
                    }
3137
                }
3138
                _LMModelItem.Commit();
3139

    
3140
                ReleaseCOMObjects(_Attributes);
3141
                ReleaseCOMObjects(_LMModelItem);
3142
                ReleaseCOMObjects(_LMSymbol);
3143
            }
3144
        }
3145

    
3146
        /// <summary>
3147
        /// Input SpecBreak Attribute
3148
        /// </summary>
3149
        /// <param name="specBreak"></param>
3150
        private void InputSpecBreakAttribute(SpecBreak specBreak)
3151
        {
3152
            object upStreamObj = SPPIDUtil.FindObjectByUID(document, specBreak.UpStreamUID);
3153
            object downStreamObj = SPPIDUtil.FindObjectByUID(document, specBreak.DownStreamUID);
3154

    
3155
            if (upStreamObj != null &&
3156
                downStreamObj != null)
3157
            {
3158
                LMConnector targetLMConnector = FindBreakLineTarget(upStreamObj, downStreamObj);
3159

    
3160
                if (targetLMConnector != null)
3161
                {
3162
                    foreach (LMLabelPersist _LMLabelPersist in targetLMConnector.LabelPersists)
3163
                    {
3164
                        string symbolPath = _LMLabelPersist.get_FileName();
3165
                        AttributeMapping mapping = document.AttributeMappings.Find(x => x.SPPIDSYMBOLNAME == symbolPath);
3166
                        if (mapping != null)
3167
                        {
3168
                            BaseModel.Attribute attribute = specBreak.ATTRIBUTES.Find(y => y.UID == mapping.UID);
3169
                            if (attribute != null && !string.IsNullOrEmpty(attribute.VALUE) && attribute.VALUE != "None")
3170
                            {
3171
                                string[] values = attribute.VALUE.Split(new char[] { ',' });
3172
                                if (values.Length == 2)
3173
                                {
3174
                                    string upStreamValue = values[0];
3175
                                    string downStreamValue = values[1];
3176

    
3177
                                    InputAttributeForSpecBreak(upStreamObj, downStreamObj, upStreamValue, downStreamValue, mapping.SPPIDATTRIBUTENAME);
3178
                                }
3179
                            }
3180
                        }
3181
                    }
3182

    
3183
                    ReleaseCOMObjects(targetLMConnector);
3184
                }
3185
            }
3186

    
3187

    
3188
            #region 내부에서만 쓰는 메서드
3189
            void InputAttributeForSpecBreak(object _upStreamObj, object _downStreamObj, string upStreamValue, string downStreamValue, string sppidAttributeName)
3190
            {
3191
                Symbol upStreamSymbol = _upStreamObj as Symbol;
3192
                Line upStreamLine = _upStreamObj as Line;
3193
                Symbol downStreamSymbol = _downStreamObj as Symbol;
3194
                Line downStreamLine = _downStreamObj as Line;
3195
                // 둘다 Line일 경우
3196
                if (upStreamLine != null && downStreamLine != null)
3197
                {
3198
                    InputLineAttributeForSpecBreakLine(upStreamLine, sppidAttributeName, upStreamValue);
3199
                    InputLineAttributeForSpecBreakLine(downStreamLine, sppidAttributeName, downStreamValue);
3200
                }
3201
                // 둘다 Symbol일 경우
3202
                else if (upStreamSymbol != null && downStreamSymbol != null)
3203
                {
3204
                    LMConnector zeroLenthConnector = FindBreakLineTarget(upStreamSymbol, downStreamSymbol);
3205
                    LMSymbol upStreamLMSymbol = dataSource.GetSymbol(upStreamSymbol.SPPID.RepresentationId);
3206
                    LMSymbol downStreamLMSymbol = dataSource.GetSymbol(downStreamSymbol.SPPID.RepresentationId);
3207

    
3208
                    foreach (LMConnector connector in upStreamLMSymbol.Avoid1Connectors)
3209
                    {
3210
                        if (connector.get_ItemStatus() != "Active")
3211
                            continue;
3212

    
3213
                        if (connector.Id != zeroLenthConnector.Id)
3214
                            InputLineAttributeForSpecBreakLMConnector(connector, sppidAttributeName, upStreamValue);
3215
                    }
3216

    
3217
                    foreach (LMConnector connector in upStreamLMSymbol.Avoid2Connectors)
3218
                    {
3219
                        if (connector.get_ItemStatus() != "Active")
3220
                            continue;
3221

    
3222
                        if (connector.Id != zeroLenthConnector.Id)
3223
                            InputLineAttributeForSpecBreakLMConnector(connector, sppidAttributeName, upStreamValue);
3224
                    }
3225

    
3226
                    foreach (LMConnector connector in downStreamLMSymbol.Avoid1Connectors)
3227
                    {
3228
                        if (connector.get_ItemStatus() != "Active")
3229
                            continue;
3230

    
3231
                        if (connector.Id != zeroLenthConnector.Id)
3232
                            InputLineAttributeForSpecBreakLMConnector(connector, sppidAttributeName, downStreamValue);
3233
                    }
3234

    
3235
                    foreach (LMConnector connector in downStreamLMSymbol.Avoid2Connectors)
3236
                    {
3237
                        if (connector.get_ItemStatus() != "Active")
3238
                            continue;
3239

    
3240
                        if (connector.Id != zeroLenthConnector.Id)
3241
                            InputLineAttributeForSpecBreakLMConnector(connector, sppidAttributeName, downStreamValue);
3242
                    }
3243

    
3244
                    ReleaseCOMObjects(zeroLenthConnector);
3245
                    ReleaseCOMObjects(upStreamLMSymbol);
3246
                    ReleaseCOMObjects(downStreamLMSymbol);
3247
                }
3248
                else if (upStreamSymbol != null && downStreamLine != null)
3249
                {
3250
                    LMConnector zeroLenthConnector = FindBreakLineTarget(upStreamSymbol, downStreamLine);
3251
                    InputLineAttributeForSpecBreakLine(downStreamLine, sppidAttributeName, downStreamValue);
3252
                    LMSymbol upStreamLMSymbol = dataSource.GetSymbol(upStreamSymbol.SPPID.RepresentationId);
3253

    
3254
                    foreach (LMConnector connector in upStreamLMSymbol.Avoid1Connectors)
3255
                    {
3256
                        if (connector.get_ItemStatus() != "Active")
3257
                            continue;
3258

    
3259
                        if (connector.Id != zeroLenthConnector.Id)
3260
                            InputLineAttributeForSpecBreakLMConnector(connector, sppidAttributeName, upStreamValue);
3261
                    }
3262

    
3263
                    foreach (LMConnector connector in upStreamLMSymbol.Avoid2Connectors)
3264
                    {
3265
                        if (connector.get_ItemStatus() != "Active")
3266
                            continue;
3267

    
3268
                        if (connector.Id != zeroLenthConnector.Id)
3269
                            InputLineAttributeForSpecBreakLMConnector(connector, sppidAttributeName, upStreamValue);
3270
                    }
3271

    
3272
                    ReleaseCOMObjects(zeroLenthConnector);
3273
                    ReleaseCOMObjects(upStreamLMSymbol);
3274
                }
3275
                else if (upStreamLine != null && downStreamSymbol != null)
3276
                {
3277
                    LMConnector zeroLenthConnector = FindBreakLineTarget(upStreamLine, downStreamSymbol);
3278
                    InputLineAttributeForSpecBreakLine(upStreamLine, sppidAttributeName, upStreamValue);
3279
                    LMSymbol downStreamLMSymbol = dataSource.GetSymbol(downStreamSymbol.SPPID.RepresentationId);
3280

    
3281
                    foreach (LMConnector connector in downStreamLMSymbol.Avoid1Connectors)
3282
                    {
3283
                        if (connector.get_ItemStatus() != "Active")
3284
                            continue;
3285

    
3286
                        if (connector.Id != zeroLenthConnector.Id)
3287
                            InputLineAttributeForSpecBreakLMConnector(connector, sppidAttributeName, downStreamValue);
3288
                    }
3289

    
3290
                    foreach (LMConnector connector in downStreamLMSymbol.Avoid2Connectors)
3291
                    {
3292
                        if (connector.get_ItemStatus() != "Active")
3293
                            continue;
3294

    
3295
                        if (connector.Id != zeroLenthConnector.Id)
3296
                            InputLineAttributeForSpecBreakLMConnector(connector, sppidAttributeName, downStreamValue);
3297
                    }
3298

    
3299
                    ReleaseCOMObjects(zeroLenthConnector);
3300
                    ReleaseCOMObjects(downStreamLMSymbol);
3301
                }
3302
            }
3303

    
3304
            void InputLineAttributeForSpecBreakLine(Line line, string attrName, string value)
3305
            {
3306
                LMModelItem _LMModelItem = dataSource.GetModelItem(line.SPPID.ModelItemId);
3307
                if (_LMModelItem != null && _LMModelItem.get_ItemStatus() == "Active")
3308
                {
3309
                    LMAAttribute _LMAAttribute = _LMModelItem.Attributes[attrName];
3310
                    if (_LMAAttribute != null)
3311
                    {
3312
                        if (DBNull.Value.Equals(_LMAAttribute.get_Value()))
3313
                            _LMAAttribute.set_Value(value);
3314
                        else if (_LMAAttribute.get_Value() != value)
3315
                            _LMAAttribute.set_Value(value);
3316
                    }
3317

    
3318
                    _LMModelItem.Commit();
3319
                }
3320
                if (_LMModelItem != null)
3321
                    ReleaseCOMObjects(_LMModelItem);
3322
            }
3323

    
3324
            void InputLineAttributeForSpecBreakLMConnector(LMConnector connector, string attrName, string value)
3325
            {
3326
                LMModelItem _LMModelItem = dataSource.GetModelItem(connector.ModelItemID);
3327
                if (_LMModelItem != null && _LMModelItem.get_ItemStatus() == "Active")
3328
                {
3329
                    LMAAttribute _LMAAttribute = _LMModelItem.Attributes[attrName];
3330
                    if (_LMAAttribute != null)
3331
                    {
3332
                        if (DBNull.Value.Equals(_LMAAttribute.get_Value()))
3333
                            _LMAAttribute.set_Value(value);
3334
                        else if (_LMAAttribute.get_Value() != value)
3335
                            _LMAAttribute.set_Value(value);
3336
                    }
3337

    
3338
                    _LMModelItem.Commit();
3339
                }
3340
                if (_LMModelItem != null)
3341
                    ReleaseCOMObjects(_LMModelItem);
3342
            }
3343
            #endregion
3344
        }
3345

    
3346
        /// <summary>
3347
        /// Text Modeling - Association일 경우는 Text대신 해당 맵핑된 Symbol로 모델링
3348
        /// </summary>
3349
        /// <param name="text"></param>
3350
        private void TextModeling(Text text)
3351
        {
3352
            LMSymbol _LMSymbol = null;
3353
            LMConnector connectedLMConnector = null;
3354
            //if (text.ASSOCIATION && !string.IsNullOrEmpty(text.OWNER) && text.OWNER != "None")
3355
            if (text.ASSOCIATION)
3356
            {
3357
                object owner = SPPIDUtil.FindObjectByUID(document, text.OWNER);
3358
                if (owner != null && owner.GetType() == typeof(Symbol))
3359
                {
3360
                    Symbol symbol = owner as Symbol;
3361
                    _LMSymbol = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
3362
                    if (_LMSymbol != null)
3363
                    {
3364
                        BaseModel.Attribute attribute = symbol.ATTRIBUTES.Find(x => x.ASSOCITEM == text.UID);
3365
                        if (attribute != null && !string.IsNullOrEmpty(attribute.VALUE) && attribute.VALUE != "None")
3366
                        {
3367
                            AttributeMapping mapping = document.AttributeMappings.Find(x => x.UID == attribute.UID && !string.IsNullOrEmpty(x.SPPIDSYMBOLNAME));
3368

    
3369
                            if (mapping != null)
3370
                            {
3371
                                double x = 0;
3372
                                double y = 0;
3373

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

    
3377
                                LMLabelPersist _LMLabelPersist = _placement.PIDPlaceLabel(mapping.SPPIDSYMBOLNAME, ref array, Rotation: text.ANGLE, LabeledItem: _LMSymbol.AsLMRepresentation(), IsLeaderVisible: mapping.LeaderLine);
3378
                                if (_LMLabelPersist != null)
3379
                                {
3380
                                    text.SPPID.RepresentationId = _LMLabelPersist.AsLMRepresentation().Id;
3381
                                    _LMLabelPersist.Commit();
3382
                                    ReleaseCOMObjects(_LMLabelPersist);
3383
                                }
3384
                            }
3385
                        }
3386
                    }
3387
                }
3388
                else if (owner != null && owner.GetType() == typeof(Line))
3389
                {
3390
                    Line line = owner as Line;
3391
                    Dictionary<LMConnector, List<double[]>> connectorVertices = GetPipeRunVertices(line.SPPID.ModelItemId);
3392
                    connectedLMConnector = FindTargetLMConnectorForLabel(connectorVertices, text.SPPID.ORIGINAL_X, text.SPPID.ORIGINAL_Y);
3393

    
3394
                    if (connectedLMConnector != null)
3395
                    {
3396
                        BaseModel.Attribute attribute = line.ATTRIBUTES.Find(x => x.ASSOCITEM == text.UID);
3397
                        if (attribute != null && !string.IsNullOrEmpty(attribute.VALUE) && attribute.VALUE != "None")
3398
                        {
3399
                            AttributeMapping mapping = document.AttributeMappings.Find(x => x.UID == attribute.UID && !string.IsNullOrEmpty(x.SPPIDSYMBOLNAME));
3400

    
3401
                            if (mapping != null)
3402
                            {
3403
                                double x = 0;
3404
                                double y = 0;
3405

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

    
3409
                                LMLabelPersist _LMLabelPersist = _placement.PIDPlaceLabel(mapping.SPPIDSYMBOLNAME, ref array, Rotation: text.ANGLE, LabeledItem: connectedLMConnector.AsLMRepresentation(), IsLeaderVisible: mapping.LeaderLine);
3410
                                if (_LMLabelPersist != null)
3411
                                {
3412
                                    text.SPPID.RepresentationId = _LMLabelPersist.AsLMRepresentation().Id;
3413
                                    _LMLabelPersist.Commit();
3414
                                    ReleaseCOMObjects(_LMLabelPersist);
3415
                                }
3416
                            }
3417
                        }
3418
                    }
3419
                }
3420
            }
3421
            else
3422
            {
3423
                LMItemNote _LMItemNote = null;
3424
                LMAAttribute _LMAAttribute = null;
3425

    
3426
                double x = 0;
3427
                double y = 0;
3428
                double angle = text.ANGLE;
3429
                CalcLabelLocation(ref x, ref y, text.SPPID.ORIGINAL_X, text.SPPID.ORIGINAL_Y, text.SPPIDLabelLocation, _ETCSetting.TextLocation);
3430

    
3431
                _LMSymbol = _placement.PIDPlaceSymbol(text.SPPID.MAPPINGNAME, x, y, Rotation: angle);
3432
                if (_LMSymbol != null)
3433
                {
3434
                    _LMSymbol.Commit();
3435
                    _LMItemNote = _placement.PIDDataSource.GetItemNote(_LMSymbol.ModelItemID);
3436
                    if (_LMItemNote != null)
3437
                    {
3438
                        _LMItemNote.Commit();
3439
                        _LMAAttribute = _LMItemNote.Attributes["Note.Body"];
3440
                        if (_LMAAttribute != null)
3441
                        {
3442
                            _LMAAttribute.set_Value(text.VALUE);
3443
                            text.SPPID.RepresentationId = _LMSymbol.AsLMRepresentation().Id;
3444
                            _LMItemNote.Commit();
3445

    
3446
                            if (_LMAAttribute != null)
3447
                                ReleaseCOMObjects(_LMAAttribute);
3448
                            if (_LMItemNote != null)
3449
                                ReleaseCOMObjects(_LMItemNote);
3450
                        }
3451
                    }
3452
                }
3453
            }
3454
            if (_LMSymbol != null)
3455
                ReleaseCOMObjects(_LMSymbol);
3456
        }
3457

    
3458
        /// <summary>
3459
        /// Note Modeling
3460
        /// </summary>
3461
        /// <param name="note"></param>
3462
        private void NoteModeling(Note note)
3463
        {
3464
            LMSymbol _LMSymbol = null;
3465
            LMItemNote _LMItemNote = null;
3466
            LMAAttribute _LMAAttribute = null;
3467

    
3468
            if (string.IsNullOrEmpty(note.OWNER) || note.OWNER == "None")
3469
            {
3470
                double x = 0;
3471
                double y = 0;
3472

    
3473
                CalcLabelLocation(ref x, ref y, note.SPPID.ORIGINAL_X, note.SPPID.ORIGINAL_Y, note.SPPIDLabelLocation, _ETCSetting.NoteLocation);
3474

    
3475
                _LMSymbol = _placement.PIDPlaceSymbol(note.SPPID.MAPPINGNAME, x, y);
3476
                if (_LMSymbol != null)
3477
                {
3478
                    _LMSymbol.Commit();
3479
                    _LMItemNote = _placement.PIDDataSource.GetItemNote(_LMSymbol.ModelItemID);
3480
                    if (_LMItemNote != null)
3481
                    {
3482
                        _LMItemNote.Commit();
3483
                        _LMAAttribute = _LMItemNote.Attributes["Note.Body"];
3484
                        if (_LMAAttribute != null)
3485
                        {
3486
                            _LMAAttribute.set_Value(note.VALUE);
3487
                            note.SPPID.RepresentationId = _LMSymbol.AsLMRepresentation().Id;
3488
                            _LMItemNote.Commit();
3489
                        }
3490
                    }
3491
                }
3492
            }
3493

    
3494
            if (_LMAAttribute != null)
3495
                ReleaseCOMObjects(_LMAAttribute);
3496
            if (_LMItemNote != null)
3497
                ReleaseCOMObjects(_LMItemNote);
3498
            if (_LMSymbol != null)
3499
                ReleaseCOMObjects(_LMSymbol);
3500
        }
3501

    
3502
        /// <summary>
3503
        /// Label의 좌표를 구하는 메서드(ID2 기준의 좌표 -> SPPID 좌표)
3504
        /// </summary>
3505
        /// <param name="x"></param>
3506
        /// <param name="y"></param>
3507
        /// <param name="originX"></param>
3508
        /// <param name="originY"></param>
3509
        /// <param name="SPPIDLabelLocation"></param>
3510
        /// <param name="location"></param>
3511
        private void CalcLabelLocation(ref double x, ref double y, double originX, double originY, SPPIDLabelLocationInfo SPPIDLabelLocation, Location location)
3512
        {
3513
            if (location == Location.None)
3514
            {
3515
                x = originX;
3516
                y = originY;
3517
            }
3518
            else
3519
            {
3520
                if (location.HasFlag(Location.Center))
3521
                {
3522
                    x = (SPPIDLabelLocation.X1 + SPPIDLabelLocation.X2) / 2;
3523
                    y = (SPPIDLabelLocation.Y1 + SPPIDLabelLocation.Y2) / 2;
3524
                }
3525

    
3526
                if (location.HasFlag(Location.Left))
3527
                    x = SPPIDLabelLocation.X1;
3528
                else if (location.HasFlag(Location.Right))
3529
                    x = SPPIDLabelLocation.X2;
3530

    
3531
                if (location.HasFlag(Location.Down))
3532
                    y = SPPIDLabelLocation.Y1;
3533
                else if (location.HasFlag(Location.Up))
3534
                    y = SPPIDLabelLocation.Y2;
3535
            }
3536
        }
3537

    
3538
        /// <summary>
3539
        /// Symbol의 우선순위 Modeling 목록을 가져온다.
3540
        /// 1. Angle Valve
3541
        /// 2. 3개로 이루어진 Symbol Group
3542
        /// </summary>
3543
        /// <returns></returns>
3544
        private List<Symbol> GetPrioritySymbol()
3545
        {
3546
            DataTable symbolTable = document.SymbolTable;
3547
            // List에 순서대로 쌓는다.
3548
            List<Symbol> symbols = new List<Symbol>();
3549

    
3550
            // Angle Valve 부터
3551
            foreach (var symbol in document.SYMBOLS.FindAll(x => x.CONNECTORS.FindAll(y => y.Index == 0).Count == 2))
3552
            {
3553
                if (!symbols.Contains(symbol))
3554
                {
3555
                    double originX = 0;
3556
                    double originY = 0;
3557

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

    
3562
                    SlopeType slopeType1 = SlopeType.None;
3563
                    SlopeType slopeType2 = SlopeType.None;
3564
                    foreach (Connector connector in symbol.CONNECTORS.FindAll(x => x.Index == 0))
3565
                    {
3566
                        double connectorX = 0;
3567
                        double connectorY = 0;
3568
                        SPPIDUtil.ConvertPointBystring(connector.CONNECTPOINT, ref connectorX, ref connectorY);
3569
                        if (slopeType1 == SlopeType.None)
3570
                            slopeType1 = SPPIDUtil.CalcSlope(originX, originY, connectorX, connectorY);
3571
                        else
3572
                            slopeType2 = SPPIDUtil.CalcSlope(originX, originY, connectorX, connectorY);
3573
                    }
3574

    
3575
                    if ((slopeType1 == SlopeType.VERTICAL && slopeType2 == SlopeType.HORIZONTAL) ||
3576
                        (slopeType2 == SlopeType.VERTICAL && slopeType1 == SlopeType.HORIZONTAL))
3577
                        symbols.Add(symbol);
3578
                }
3579
            }
3580

    
3581
            List<Symbol> tempSymbols = new List<Symbol>();
3582
            // Conn 갯수 기준
3583
            foreach (var item in document.SYMBOLS)
3584
            {
3585
                if (!symbols.Contains(item))
3586
                    tempSymbols.Add(item);
3587
            }
3588
            tempSymbols.Sort(SortSymbolPriority);
3589
            symbols.AddRange(tempSymbols);
3590

    
3591
            return symbols;
3592
        }
3593

    
3594
        private void SetPriorityLine()
3595
        {
3596
            document.LINES.Sort(SortLinePriority);
3597

    
3598
            int SortLinePriority(Line a, Line b)
3599
            {
3600
                // Branch 없는것부터
3601
                int branchRetval = CompareBranchLine(a, b);
3602
                if (branchRetval != 0)
3603
                {
3604
                    return branchRetval;
3605
                }
3606
                else
3607
                {
3608
                    // Symbol 연결 갯수
3609
                    int connSymbolRetval = CompareConnSymbol(a, b);
3610
                    if (connSymbolRetval != 0)
3611
                    {
3612
                        return connSymbolRetval;
3613
                    }
3614
                    else
3615
                    {
3616
                        // 아이템 연결 갯수(심볼, Line이면서 Not Branch)
3617
                        int connItemRetval = CompareConnItem(a, b);
3618
                        if (connItemRetval != 0)
3619
                        {
3620
                            return connItemRetval;
3621
                        }
3622
                        else
3623
                        {
3624
                            // ConnectedItem이 없는것
3625
                            int noneConnRetval = CompareNoneConn(a, b);
3626
                            if (noneConnRetval != 0)
3627
                            {
3628
                                return noneConnRetval;
3629
                            }
3630
                            else
3631
                            {
3632

    
3633
                            }
3634
                        }
3635
                    }
3636
                }
3637

    
3638
                return 0;
3639
            }
3640

    
3641
            int CompareConnSymbol(Line a, Line b)
3642
            {
3643
                List<Connector> connectorsA = a.CONNECTORS
3644
                    .Where(conn => conn.ConnectedObject != null && conn.ConnectedObject.GetType() == typeof(Symbol))
3645
                    .ToList();
3646

    
3647
                List<Connector> connectorsB = b.CONNECTORS
3648
                    .Where(conn => conn.ConnectedObject != null && conn.ConnectedObject.GetType() == typeof(Symbol))
3649
                    .ToList();
3650

    
3651
                // 오름차순
3652
                return connectorsB.Count.CompareTo(connectorsA.Count);
3653
            }
3654

    
3655
            int CompareConnItem(Line a, Line b)
3656
            {
3657
                List<Connector> connectorsA = a.CONNECTORS
3658
                    .Where(conn => conn.ConnectedObject != null && 
3659
                    (conn.ConnectedObject.GetType() == typeof(Symbol) || 
3660
                    (conn.ConnectedObject.GetType() == typeof(Line) && !SPPIDUtil.IsBranchLine((Line)conn.ConnectedObject, a))))
3661
                    .ToList();
3662

    
3663
                List<Connector> connectorsB = b.CONNECTORS
3664
                    .Where(conn => conn.ConnectedObject != null &&
3665
                    (conn.ConnectedObject.GetType() == typeof(Symbol) ||
3666
                    (conn.ConnectedObject.GetType() == typeof(Line) && !SPPIDUtil.IsBranchLine((Line)conn.ConnectedObject, b))))
3667
                    .ToList();
3668

    
3669
                // 오름차순
3670
                return connectorsB.Count.CompareTo(connectorsA.Count);
3671
            }
3672

    
3673
            int CompareBranchLine(Line a, Line b)
3674
            {
3675
                List<Connector> connectorsA = a.CONNECTORS
3676
                    .Where(conn => conn.ConnectedObject != null && conn.ConnectedObject.GetType() == typeof(Line) && SPPIDUtil.IsBranchLine(a, conn.ConnectedObject as Line))
3677
                    .ToList();
3678
                List<Connector> connectorsB = b.CONNECTORS
3679
                    .Where(conn => conn.ConnectedObject != null && conn.ConnectedObject.GetType() == typeof(Line) && SPPIDUtil.IsBranchLine(b, conn.ConnectedObject as Line))
3680
                    .ToList();
3681

    
3682
                // 내림차순
3683
                return connectorsA.Count.CompareTo(connectorsB.Count);
3684
            }
3685

    
3686
            int CompareNoneConn(Line a, Line b)
3687
            {
3688
                List<Connector> connectorsA = a.CONNECTORS
3689
                    .Where(conn => conn.ConnectedObject == null)
3690
                    .ToList();
3691

    
3692
                List<Connector> connectorsB = b.CONNECTORS
3693
                    .Where(conn => conn.ConnectedObject == null)
3694
                    .ToList();
3695

    
3696
                // 오름차순
3697
                return connectorsB.Count.CompareTo(connectorsA.Count);
3698
            }
3699
        }
3700

    
3701
        private void SortBranchLines()
3702
        {
3703
            NewBranchLines.Sort(SortBranchLine);
3704
            int SortBranchLine(Line a, Line b)
3705
            {
3706
                int countA = a.CONNECTORS.FindAll(x => x.ConnectedObject != null &&
3707
                 x.ConnectedObject.GetType() == typeof(Line) &&
3708
                 SPPIDUtil.IsBranchLine(x.ConnectedObject as Line, a) &&
3709
                 string.IsNullOrEmpty(((Line)x.ConnectedObject).SPPID.ModelItemId)).Count;
3710

    
3711
                int countB = b.CONNECTORS.FindAll(x => x.ConnectedObject != null &&
3712
                 x.ConnectedObject.GetType() == typeof(Line) &&
3713
                 SPPIDUtil.IsBranchLine(x.ConnectedObject as Line, b) &&
3714
                 string.IsNullOrEmpty(((Line)x.ConnectedObject).SPPID.ModelItemId)).Count;
3715

    
3716
                // 내림차순
3717
                return countA.CompareTo(countB);
3718
            }
3719
        }
3720

    
3721
        private static int SortSymbolPriority(Symbol a, Symbol b)
3722
        {
3723
            int countA = a.CONNECTORS.FindAll(x => !string.IsNullOrEmpty(x.CONNECTEDITEM) && x.CONNECTEDITEM != "None").Count;
3724
            int countB = b.CONNECTORS.FindAll(x => !string.IsNullOrEmpty(x.CONNECTEDITEM) && x.CONNECTEDITEM != "None").Count;
3725
            int retval = countB.CompareTo(countA);
3726
            if (retval != 0)
3727
                return retval;
3728
            else
3729
                return a.SPPID.ORIGINAL_X.CompareTo(b.SPPID.ORIGINAL_X);
3730
        }
3731

    
3732
        /// <summary>
3733
        /// Graphic OID로 해당 Symbol의 크기를 구하여 Zoom
3734
        /// </summary>
3735
        /// <param name="graphicOID"></param>
3736
        /// <param name="milliseconds"></param>
3737
        private void ZoomObjectByGraphicOID(string graphicOID, int milliseconds = 150)
3738
        {
3739
            if (radApp.ActiveDocument.ActiveSheet.DrawingObjects[graphicOID] != null)
3740
            {
3741
                double minX = 0;
3742
                double minY = 0;
3743
                double maxX = 0;
3744
                double maxY = 0;
3745
                radApp.ActiveDocument.ActiveSheet.DrawingObjects[graphicOID].Range(out minX, out minY, out maxX, out maxY);
3746
                radApp.ActiveWindow.ZoomArea2(minX - 0.007, minY - 0.007, maxX + 0.007, maxY + 0.007, null);
3747

    
3748
                Thread.Sleep(milliseconds);
3749
            }
3750
        }
3751

    
3752
        /// <summary>
3753
        /// ComObject를 Release
3754
        /// </summary>
3755
        /// <param name="objVars"></param>
3756
        public void ReleaseCOMObjects(params object[] objVars)
3757
        {
3758
            int intNewRefCount = 0;
3759
            foreach (object obj in objVars)
3760
            {
3761
                if (!Information.IsNothing(obj) && System.Runtime.InteropServices.Marshal.IsComObject(obj))
3762
                    intNewRefCount = intNewRefCount + System.Runtime.InteropServices.Marshal.FinalReleaseComObject(obj);
3763
            }
3764
        }
3765
    }
3766
}
클립보드 이미지 추가 (최대 크기: 500 MB)