프로젝트

일반

사용자정보

통계
| 개정판:

hytos / DTI_PID / SPPIDConverter / AutoModeling.cs @ 24515a3a

이력 | 보기 | 이력해설 | 다운로드 (173 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, 16);
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
                    // Join Run
110
                    RunJoinRun();
111
                    // Check FlowDirection
112
                    RunFlowDirection();
113
                    // Note Modeling
114
                    RunNoteModeling();
115
                    // Text Modeling
116
                    RunTextModeling();
117
                    // Input LineNumber Attribute
118
                    RunInputLineNumberAttribute();
119
                    // Input Symbol Attribute
120
                    RunInputSymbolAttribute();
121
                    // Input SpecBreak Attribute
122
                    RunInputSpecBreakAttribute();
123
                    // Label Symbol Modeling
124
                    RunLabelSymbolModeling();
125

    
126
                    // Result Logging
127
                    document.CheckModelingResult();
128

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

    
145
                    //// TrimLineRun Line Join
146
                    //foreach (TrimLine trimLine in document.TRIMLINES)
147
                    //    try
148
                    //    {
149
                    //        foreach (LineRun run in trimLine.RUNS)
150
                    //            JoinRunLine(run);
151
                    //    }
152
                    //    catch (Exception ex)
153
                    //    {
154
                    //        Log.Write("Error in JoinRunLine");
155
                    //        Log.Write("UID : " + trimLine.UID);
156
                    //        Log.Write(ex.Message);
157
                    //        Log.Write(ex.StackTrace);
158
                    //    }
159

    
160

    
161

    
162

    
163

    
164
                }
165
            }
166
            catch (Exception ex)
167
            {
168
                SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.ClearParent, null);
169
                SplashScreenManager.CloseForm(false);
170
                System.Windows.Forms.MessageBox.Show(ex.Message + "\r\n" + ex.StackTrace);
171
            }
172
            finally
173
            {
174
                Log.Write("End Modeling");
175
                application.ActiveWindow.Fit();
176

    
177
                if (currentDrawing != null)
178
                    ReleaseCOMObjects(currentDrawing);
179

    
180
                if (radApp.ActiveDocument != null)
181
                {
182
                    radApp.ActiveDocument.Save();
183
                    ReleaseCOMObjects(newDrawing);
184
                }
185

    
186
                ReleaseCOMObjects(dataSource);
187
                ReleaseCOMObjects(_placement);
188

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

    
297
                LMRelationships relationships = new LMRelationships();
298
                relationships.Collect(dataSource, Filter: filter);
299

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

    
467
                        SetFlowDirectionByLine(line.SPPID.ModelItemId);
468

    
469
                        ReleaseCOMObjects(modelItem);
470
                    }
471
                }
472

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

    
485
                SetFlowDirectionByLine(modelId);
486

    
487
                ReleaseCOMObjects(zeroLengthModelItem);
488
                SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
489
            }
490
            foreach (var modelId in ZeroLengthModelItemIDReverse)
491
            {
492
                LMModelItem zeroLengthModelItem = dataSource.GetModelItem(modelId);
493
                LMAAttribute attribute = zeroLengthModelItem.Attributes["FlowDirection"];
494
                if (attribute != null && zeroLengthModelItem.get_ItemStatus() == "Active")
495
                {
496
                    attribute.set_Value("End 1 is downstream (Outlet)");
497
                    zeroLengthModelItem.Commit();
498
                }
499

    
500
                SetFlowDirectionByLine(modelId);
501

    
502
                ReleaseCOMObjects(zeroLengthModelItem);
503
                SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
504
            }
505
            foreach (var modelId in ZeroLengthSymbolToSymbolModelItemID)
506
            {
507
                SetFlowDirectionByLine(modelId);
508
                SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
509
            }
510

    
511
            void SetFlowDirectionByLine(string lineModelItemID)
512
            {
513
                LMModelItem modelItem = dataSource.GetModelItem(lineModelItemID);
514
                if (modelItem != null && modelItem.get_ItemStatus() == "Active")
515
                {
516
                    LMAAttribute attribute = modelItem.Attributes["FlowDirection"];
517
                    if (attribute != null && !DBNull.Value.Equals(attribute.get_Value()))
518
                    {
519
                        string sFlowDirection = attribute.get_Value().ToString();
520
                        foreach (LMRepresentation rep in modelItem.Representations)
521
                        {
522
                            if (rep.Attributes["RepresentationType"].get_Value() == "Connector" && rep.Attributes["ItemStatus"].get_Value() == "Active")
523
                            {
524
                                LMConnector connector = dataSource.GetConnector(rep.Id);
525

    
526
                                foreach (LMRelationship relationship in connector.Relation1Relationships)
527
                                    SetSymbolFlowDirectionByRelationShip(relationship, connector, sFlowDirection);
528
                                foreach (LMRelationship relationship in connector.Relation2Relationships)
529
                                    SetSymbolFlowDirectionByRelationShip(relationship, connector, sFlowDirection);
530

    
531
                                ReleaseCOMObjects(connector);
532
                            }
533
                        }
534
                    }
535
                    ReleaseCOMObjects(modelItem);
536
                }
537

    
538
                void SetSymbolFlowDirectionByRelationShip(LMRelationship relationship, LMConnector connector, string sFlowDirection)
539
                {
540
                    // Item2가 Symbol
541
                    if (!DBNull.Value.Equals(relationship.Item1RepresentationID) && relationship.Item1RepresentationID == connector.Id &&
542
                        relationship.Item2RepresentationObject != null && relationship.Item2RepresentationObject.get_RepresentationType() == "Symbol")
543
                    {
544
                        int symbolIndex = Convert.ToInt32(relationship.get_Item2Location());
545
                        int lineIndex = Convert.ToInt32(relationship.get_Item1Location());
546
                        LMModelItem symbolModelItem = relationship.Item2RepresentationObject.ModelItemObject;
547

    
548
                        SetSymbolFlowDirection(lineIndex, symbolIndex, sFlowDirection, symbolModelItem);
549

    
550
                        symbolModelItem.Commit();
551
                        ReleaseCOMObjects(symbolModelItem);
552
                    }
553
                    // Item1이 Symbol
554
                    else if (!DBNull.Value.Equals(relationship.Item2RepresentationID) && relationship.Item2RepresentationID == connector.Id &&
555
                            relationship.Item1RepresentationObject != null && relationship.Item1RepresentationObject.get_RepresentationType() == "Symbol")
556
                    {
557
                        int symbolIndex = Convert.ToInt32(relationship.get_Item1Location());
558
                        int lineIndex = Convert.ToInt32(relationship.get_Item2Location());
559
                        LMModelItem symbolModelItem = relationship.Item1RepresentationObject.ModelItemObject;
560

    
561
                        SetSymbolFlowDirection(lineIndex, symbolIndex, sFlowDirection, symbolModelItem);
562

    
563
                        symbolModelItem.Commit();
564
                        ReleaseCOMObjects(symbolModelItem);
565
                    }
566
                }
567

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

    
712
            //current LMDrawing 가져오기
713
            LMAFilter filter = new LMAFilter();
714
            LMACriterion criterion = new LMACriterion();
715
            filter.ItemType = "Drawing";
716
            criterion.SourceAttributeName = "Name";
717
            criterion.Operator = "=";
718
            criterion.set_ValueAttribute(drawingName);
719
            filter.get_Criteria().Add(criterion);
720

    
721
            LMDrawings drawings = new LMDrawings();
722
            drawings.Collect(dataSource, Filter: filter);
723

    
724
            currentDrawing = ((dynamic)drawings).Nth(1);
725
        }
726

    
727
        /// <summary>
728
        /// DrawingName, DrawingNumber를 확인하여 중복이 있으면 _1을 붙이고 +1씩 한다.
729
        /// </summary>
730
        /// <param name="drawingName"></param>
731
        /// <param name="drawingNumber"></param>
732
        private void GetDrawingNameAndNumber(ref string drawingName, ref string drawingNumber)
733
        {
734
            LMDrawings drawings = new LMDrawings();
735
            drawings.Collect(dataSource);
736

    
737
            List<string> drawingNameList = new List<string>();
738
            List<string> drawingNumberList = new List<string>();
739

    
740
            foreach (LMDrawing item in drawings)
741
            {
742
                drawingNameList.Add(item.Attributes["Name"].get_Value().ToString());
743
                drawingNumberList.Add(item.Attributes["DrawingNumber"].get_Value().ToString());
744
            }
745

    
746
            int nameLength = drawingName.Length;
747
            while (drawingNameList.Contains(drawingName))
748
            {
749
                if (nameLength == drawingName.Length)
750
                    drawingName += "-1";
751
                else
752
                {
753
                    int index = Convert.ToInt32(drawingName.Remove(0, nameLength + 1));
754
                    drawingName = drawingName.Substring(0, nameLength + 1);
755
                    drawingName += ++index;
756
                }
757
            }
758

    
759
            int numberLength = drawingNumber.Length;
760
            while (drawingNameList.Contains(drawingNumber))
761
            {
762
                if (numberLength == drawingNumber.Length)
763
                    drawingNumber += "-1";
764
                else
765
                {
766
                    int index = Convert.ToInt32(drawingNumber.Remove(0, numberLength + 1));
767
                    drawingNumber = drawingNumber.Substring(0, numberLength + 1);
768
                    drawingNumber += ++index;
769
                }
770
            }
771

    
772
            ReleaseCOMObjects(drawings);
773
        }
774

    
775
        /// <summary>
776
        /// 도면 크기 구하는 메서드
777
        /// </summary>
778
        /// <returns></returns>
779
        private bool DocumentCoordinateCorrection()
780
        {
781
            if (Settings.Default.DrawingX != 0 && Settings.Default.DrawingY != 0)
782
            {
783
                Log.Write("Setting Drawing X, Drawing Y");
784
                document.SetSPPIDLocation(Settings.Default.DrawingX, Settings.Default.DrawingY);
785
                Log.Write("Start coordinate correction");
786
                document.CoordinateCorrection();
787
                return true;
788
            }
789
            else
790
            {
791
                Log.Write("Need Drawing X, Y");
792
                return false;
793
            }
794
        }
795

    
796
        /// <summary>
797
        /// 심볼을 실제로 Modeling 메서드
798
        /// </summary>
799
        /// <param name="symbol"></param>
800
        /// <param name="targetSymbol"></param>
801
        /// <param name="prevSymbol"></param>
802
        private void SymbolModeling(Symbol symbol, Symbol targetSymbol)
803
        {
804
            // OWNERSYMBOL Attribute, 값을 가지고 있을 경우
805
            BaseModel.Attribute itemAttribute = symbol.ATTRIBUTES.Find(attr => attr.ATTRIBUTE == "OWNERSYMBOL");
806
            if (itemAttribute != null && (string.IsNullOrEmpty(itemAttribute.VALUE) || itemAttribute.VALUE != "None"))
807
                return;
808
            // 이미 모델링 됐을 경우
809
            else if (!string.IsNullOrEmpty(symbol.SPPID.RepresentationId))
810
                return;
811

    
812
            LMSymbol _LMSymbol = null;
813

    
814
            string mappingPath = symbol.SPPID.MAPPINGNAME;
815
            double x = symbol.SPPID.ORIGINAL_X;
816
            double y = symbol.SPPID.ORIGINAL_Y;
817
            int mirror = 0;
818
            double angle = symbol.ANGLE;
819

    
820
            // OPC 일경우 180도 일때 Mirror
821
            if (mappingPath.Contains("Piping OPC's") && angle == Math.PI)
822
                mirror = 1;
823

    
824
            // Mirror 계산
825
            if (symbol.FLIP == 1)
826
            {
827
                mirror = 1;
828
                angle += Math.PI;
829
            }
830

    
831
            if (targetSymbol != null && !string.IsNullOrEmpty(targetSymbol.SPPID.RepresentationId))
832
            {
833
                LMSymbol _TargetItem = dataSource.GetSymbol(targetSymbol.SPPID.RepresentationId);
834
                Connector connector = SPPIDUtil.FindSymbolConnectorByUID(document, symbol.UID, targetSymbol);
835
                if (connector != null)
836
                    GetTargetSymbolConnectorPoint(connector, targetSymbol, ref x, ref y);
837

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

    
840
                if (_LMSymbol != null && _TargetItem != null)
841
                {
842
                    symbol.SPPID.RepresentationId = _LMSymbol.AsLMRepresentation().Id;
843

    
844
                    if (SPPIDUtil.IsSegmentLine(document, symbol, targetSymbol))
845
                    {
846
                        LMConnector reModelingConnector = FindBreakLineTarget(symbol, targetSymbol);
847
                        if (reModelingConnector != null)
848
                            ReModelingLMConnector(reModelingConnector);
849
                    }
850
                }
851

    
852
                ReleaseCOMObjects(_TargetItem);
853
            }
854
            else
855
                _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle);
856

    
857
            if (_LMSymbol != null)
858
            {
859
                _LMSymbol.Commit();
860
                symbol.SPPID.RepresentationId = _LMSymbol.AsLMRepresentation().Id;
861
                symbol.SPPID.ModelItemID = _LMSymbol.ModelItemID;
862
                symbol.SPPID.GraphicOID = _LMSymbol.get_GraphicOID().ToString();
863

    
864
                foreach (var item in symbol.ChildSymbols)
865
                    CreateChildSymbol(item, _LMSymbol, symbol);
866

    
867
                symbol.SPPID.SPPID_X = _LMSymbol.get_XCoordinate();
868
                symbol.SPPID.SPPID_Y = _LMSymbol.get_YCoordinate();
869

    
870
                double[] range = null;
871
                GetSPPIDSymbolRange(symbol, ref range);
872
                symbol.SPPID.SPPID_Min_X = range[0];
873
                symbol.SPPID.SPPID_Min_Y = range[1];
874
                symbol.SPPID.SPPID_Max_X = range[2];
875
                symbol.SPPID.SPPID_Max_Y = range[3];
876

    
877
                foreach (var item in symbol.SPPID.CorrectionX_GroupSymbols)
878
                    item.SPPID.ORIGINAL_X = symbol.SPPID.SPPID_X;
879
                foreach (var item in symbol.SPPID.CorrectionY_GroupSymbols)
880
                    item.SPPID.ORIGINAL_Y = symbol.SPPID.SPPID_Y;
881

    
882
                ReleaseCOMObjects(_LMSymbol);
883
            }
884
        }
885

    
886
        private void RemoveSymbol(Symbol symbol)
887
        {
888
            if (!string.IsNullOrEmpty(symbol.SPPID.RepresentationId))
889
            {
890
                LMSymbol _LMSymbol = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
891
                if (_LMSymbol != null)
892
                {
893
                    _placement.PIDRemovePlacement(_LMSymbol.AsLMRepresentation());
894
                    ReleaseCOMObjects(_LMSymbol);
895
                }
896
            }
897

    
898
            symbol.SPPID.RepresentationId = string.Empty;
899
            symbol.SPPID.ModelItemID = string.Empty;
900
            symbol.SPPID.SPPID_X = double.NaN;
901
            symbol.SPPID.SPPID_Y = double.NaN;
902
            symbol.SPPID.SPPID_Min_X = double.NaN;
903
            symbol.SPPID.SPPID_Min_Y = double.NaN;
904
            symbol.SPPID.SPPID_Max_X = double.NaN;
905
            symbol.SPPID.SPPID_Max_Y = double.NaN;
906
        }
907

    
908
        private void RemoveSymbol(List<Symbol> symbols)
909
        {
910
            foreach (var symbol in symbols)
911
            {
912
                if (!string.IsNullOrEmpty(symbol.SPPID.RepresentationId))
913
                {
914
                    LMSymbol _LMSymbol = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
915
                    if (_LMSymbol != null)
916
                    {
917
                        _placement.PIDRemovePlacement(_LMSymbol.AsLMRepresentation());
918
                        ReleaseCOMObjects(_LMSymbol);
919
                    }
920
                }
921

    
922
                symbol.SPPID.RepresentationId = string.Empty;
923
                symbol.SPPID.ModelItemID = string.Empty;
924
                symbol.SPPID.SPPID_X = double.NaN;
925
                symbol.SPPID.SPPID_Y = double.NaN;
926
                symbol.SPPID.SPPID_Min_X = double.NaN;
927
                symbol.SPPID.SPPID_Min_Y = double.NaN;
928
                symbol.SPPID.SPPID_Max_X = double.NaN;
929
                symbol.SPPID.SPPID_Max_Y = double.NaN;
930
            }
931
        }
932

    
933
        /// <summary>
934
        /// ID2의 Symbol Width와 Height를 비교해서 상대적인 SPPID Connector좌표를 가져온다.
935
        /// </summary>
936
        /// <param name="targetConnector"></param>
937
        /// <param name="targetSymbol"></param>
938
        /// <param name="x"></param>
939
        /// <param name="y"></param>
940
        private void GetTargetSymbolConnectorPoint(Connector targetConnector, Symbol targetSymbol, ref double x, ref double y)
941
        {
942
            LMSymbol _TargetItem = dataSource.GetSymbol(targetSymbol.SPPID.RepresentationId);
943

    
944
            double[] range = null;
945
            List<double[]> points = new List<double[]>();
946
            GetSPPIDSymbolRangeAndConnectionPoints(targetSymbol, ref range, points);
947
            double x1 = range[0];
948
            double y1 = range[1];
949
            double x2 = range[2];
950
            double y2 = range[3];
951

    
952
            // Origin 기준 Connector의 위치차이
953
            double sceneX = 0;
954
            double sceneY = 0;
955
            SPPIDUtil.ConvertPointBystring(targetConnector.SCENECONNECTPOINT, ref sceneX, ref sceneY);
956
            double originX = 0;
957
            double originY = 0;
958
            SPPIDUtil.ConvertPointBystring(targetSymbol.ORIGINALPOINT, ref originX, ref originY);
959
            double gapX = originX - sceneX;
960
            double gapY = originY - sceneY;
961

    
962
            // SPPID Symbol과 ID2 심볼의 크기 차이
963
            double sizeWidth = 0;
964
            double sizeHeight = 0;
965
            SPPIDUtil.ConvertPointBystring(targetSymbol.SIZE, ref sizeWidth, ref sizeHeight);
966
            if (sizeWidth == 0 || sizeHeight == 0)
967
                throw new Exception("Check symbol size! \r\nUID : " + targetSymbol.UID);
968

    
969
            double percentX = (x2 - x1) / sizeWidth;
970
            double percentY = (y2 - y1) / sizeHeight;
971

    
972
            double SPPIDgapX = gapX * percentX;
973
            double SPPIDgapY = gapY * percentY;
974

    
975
            double[] SPPIDOriginPoint = new double[] { _TargetItem.get_XCoordinate() - SPPIDgapX, _TargetItem.get_YCoordinate() + SPPIDgapY };
976
            double distance = double.MaxValue;
977
            double[] resultPoint;
978
            foreach (var point in points)
979
            {
980
                double result = SPPIDUtil.CalcPointToPointdDistance(point[0], point[1], SPPIDOriginPoint[0], SPPIDOriginPoint[1]);
981
                if (distance > result)
982
                {
983
                    distance = result;
984
                    resultPoint = point;
985
                    x = point[0];
986
                    y = point[1];
987
                }
988
            }
989

    
990
            ReleaseCOMObjects(_TargetItem);
991
        }
992

    
993
        private void GetTargetLineConnectorPoint(Connector targetConnector, Line targetLine, ref double x, ref double y)
994
        {
995
            int index = targetLine.CONNECTORS.IndexOf(targetConnector);
996
            if (index == 0)
997
            {
998
                x = targetLine.SPPID.START_X;
999
                y = targetLine.SPPID.START_Y;
1000
            }
1001
            else
1002
            {
1003
                x = targetLine.SPPID.END_X;
1004
                y = targetLine.SPPID.END_Y;
1005
            }
1006
        }
1007

    
1008
        /// <summary>
1009
        /// SPPID Symbol의 Range를 구한다.
1010
        /// </summary>
1011
        /// <param name="symbol"></param>
1012
        /// <param name="range"></param>
1013
        private void GetSPPIDSymbolRangeAndConnectionPoints(Symbol symbol, ref double[] range, List<double[]> points)
1014
        {
1015
            LMSymbol _TargetItem = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
1016
            Ingr.RAD2D.Symbol2d symbol2d = radApp.ActiveDocument.ActiveSheet.DrawingObjects[_TargetItem.get_GraphicOID().ToString()];
1017
            double x1 = 0;
1018
            double y1 = 0;
1019
            double x2 = 0;
1020
            double y2 = 0;
1021
            symbol2d.Range(out x1, out y1, out x2, out y2);
1022
            range = new double[] { x1, y1, x2, y2 };
1023

    
1024
            for (int i = 1; i < int.MaxValue; i++)
1025
            {
1026
                double connX = 0;
1027
                double connY = 0;
1028
                if (_placement.PIDConnectPointLocation(_TargetItem, i, ref connX, ref connY))
1029
                    points.Add(new double[] { connX, connY });
1030
                else
1031
                    break;
1032
            }
1033

    
1034
            foreach (var childSymbol in symbol.ChildSymbols)
1035
                GetSPPIDChildSymbolRange(childSymbol, ref range, points);
1036

    
1037
            ReleaseCOMObjects(_TargetItem);
1038
        }
1039

    
1040
        private void GetSPPIDSymbolRange(Symbol symbol, ref double[] range)
1041
        {
1042
            LMSymbol _TargetItem = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
1043
            if (_TargetItem != null)
1044
            {
1045
                Ingr.RAD2D.Symbol2d symbol2d = radApp.ActiveDocument.ActiveSheet.DrawingObjects[_TargetItem.get_GraphicOID().ToString()];
1046
                double x1 = 0;
1047
                double y1 = 0;
1048
                double x2 = 0;
1049
                double y2 = 0;
1050
                symbol2d.Range(out x1, out y1, out x2, out y2);
1051
                range = new double[] { x1, y1, x2, y2 };
1052

    
1053
                foreach (var childSymbol in symbol.ChildSymbols)
1054
                    GetSPPIDChildSymbolRange(childSymbol, ref range);
1055

    
1056
                ReleaseCOMObjects(_TargetItem);
1057
            }
1058
        }
1059

    
1060
        private void GetSPPIDSymbolRange(List<Symbol> symbols, ref double[] range)
1061
        {
1062
            double[] tempRange = new double[] { double.MaxValue, double.MaxValue, double.MinValue, double.MinValue };
1063
            foreach (var symbol in symbols)
1064
            {
1065
                LMSymbol _TargetItem = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
1066
                Ingr.RAD2D.Symbol2d symbol2d = radApp.ActiveDocument.ActiveSheet.DrawingObjects[_TargetItem.get_GraphicOID().ToString()];
1067
                double x1 = 0;
1068
                double y1 = 0;
1069
                double x2 = 0;
1070
                double y2 = 0;
1071
                symbol2d.Range(out x1, out y1, out x2, out y2);
1072

    
1073
                tempRange[0] = Math.Min(tempRange[0], x1);
1074
                tempRange[1] = Math.Min(tempRange[1], y1);
1075
                tempRange[2] = Math.Max(tempRange[2], x2);
1076
                tempRange[3] = Math.Max(tempRange[3], y2);
1077

    
1078
                foreach (var childSymbol in symbol.ChildSymbols)
1079
                    GetSPPIDChildSymbolRange(childSymbol, ref tempRange);
1080

    
1081
                ReleaseCOMObjects(_TargetItem);
1082
            }
1083

    
1084
            range = tempRange;
1085
        }
1086

    
1087
        /// <summary>
1088
        /// Child Modeling 된 Symbol의 Range를 구한다.
1089
        /// </summary>
1090
        /// <param name="childSymbol"></param>
1091
        /// <param name="range"></param>
1092
        private void GetSPPIDChildSymbolRange(ChildSymbol childSymbol, ref double[] range, List<double[]> points)
1093
        {
1094
            LMSymbol _ChildSymbol = dataSource.GetSymbol(childSymbol.SPPID.RepresentationId);
1095
            if (_ChildSymbol != null)
1096
            {
1097
                Ingr.RAD2D.Symbol2d symbol2d = radApp.ActiveDocument.ActiveSheet.DrawingObjects[_ChildSymbol.get_GraphicOID().ToString()];
1098
                double x1 = 0;
1099
                double y1 = 0;
1100
                double x2 = 0;
1101
                double y2 = 0;
1102
                symbol2d.Range(out x1, out y1, out x2, out y2);
1103
                range[0] = Math.Min(range[0], x1);
1104
                range[1] = Math.Min(range[1], y1);
1105
                range[2] = Math.Max(range[2], x2);
1106
                range[3] = Math.Max(range[3], y2);
1107

    
1108
                for (int i = 1; i < int.MaxValue; i++)
1109
                {
1110
                    double connX = 0;
1111
                    double connY = 0;
1112
                    if (_placement.PIDConnectPointLocation(_ChildSymbol, i, ref connX, ref connY))
1113
                        points.Add(new double[] { connX, connY });
1114
                    else
1115
                        break;
1116
                }
1117

    
1118
                foreach (var loopChildSymbol in childSymbol.ChildSymbols)
1119
                    GetSPPIDChildSymbolRange(loopChildSymbol, ref range, points);
1120

    
1121
                ReleaseCOMObjects(_ChildSymbol);
1122
            }
1123
        }
1124

    
1125
        private void GetSPPIDChildSymbolRange(ChildSymbol childSymbol, ref double[] range)
1126
        {
1127
            LMSymbol _ChildSymbol = dataSource.GetSymbol(childSymbol.SPPID.RepresentationId);
1128
            if (_ChildSymbol != null)
1129
            {
1130
                Ingr.RAD2D.Symbol2d symbol2d = radApp.ActiveDocument.ActiveSheet.DrawingObjects[_ChildSymbol.get_GraphicOID().ToString()];
1131
                double x1 = 0;
1132
                double y1 = 0;
1133
                double x2 = 0;
1134
                double y2 = 0;
1135
                symbol2d.Range(out x1, out y1, out x2, out y2);
1136
                range[0] = Math.Min(range[0], x1);
1137
                range[1] = Math.Min(range[1], y1);
1138
                range[2] = Math.Max(range[2], x2);
1139
                range[3] = Math.Max(range[3], y2);
1140

    
1141
                foreach (var loopChildSymbol in childSymbol.ChildSymbols)
1142
                    GetSPPIDChildSymbolRange(loopChildSymbol, ref range);
1143
                ReleaseCOMObjects(_ChildSymbol);
1144
            }
1145
        }
1146

    
1147
        /// <summary>
1148
        /// Label Symbol Modeling
1149
        /// </summary>
1150
        /// <param name="symbol"></param>
1151
        private void LabelSymbolModeling(Symbol symbol)
1152
        {
1153
            if (string.IsNullOrEmpty(symbol.SPPID.RepresentationId))
1154
            {
1155
                BaseModel.Attribute itemAttribute = symbol.ATTRIBUTES.Find(x => x.ATTRIBUTE == "OWNERSYMBOL");
1156
                if (itemAttribute == null || string.IsNullOrEmpty(itemAttribute.VALUE) || itemAttribute.VALUE == "None")
1157
                    return;
1158
                Array points = new double[] { 0, symbol.SPPID.ORIGINAL_X, symbol.SPPID.ORIGINAL_Y };
1159

    
1160
                string symbolUID = itemAttribute.VALUE;
1161
                object targetItem = SPPIDUtil.FindObjectByUID(document, symbolUID);
1162
                if (targetItem != null &&
1163
                    (targetItem.GetType() == typeof(Symbol) ||
1164
                    targetItem.GetType() == typeof(Equipment)))
1165
                {
1166
                    // Object 아이템이 Symbol일 경우 Equipment일 경우 
1167
                    string sRep = null;
1168
                    if (targetItem.GetType() == typeof(Symbol))
1169
                        sRep = ((Symbol)targetItem).SPPID.RepresentationId;
1170
                    else if (targetItem.GetType() == typeof(Equipment))
1171
                        sRep = ((Equipment)targetItem).SPPID.RepresentationId;
1172
                    if (!string.IsNullOrEmpty(sRep))
1173
                    {
1174
                        // LEADER Line 검사
1175
                        bool leaderLine = false;
1176
                        SymbolMapping symbolMapping = document.SymbolMappings.Find(x => x.UID == symbol.DBUID);
1177
                        if (symbolMapping != null)
1178
                            leaderLine = symbolMapping.LEADERLINE;
1179

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

    
1184
                        //Leader 선 센터로
1185
                        if (_LMLabelPresist != null)
1186
                        {
1187
                            // Target Item에 Label의 Attribute Input
1188
                            InputSymbolAttribute(targetItem, symbol.ATTRIBUTES);
1189

    
1190
                            string OID = _LMLabelPresist.get_GraphicOID().ToString();
1191
                            DependencyObject dependency = radApp.ActiveDocument.ActiveSheet.DrawingObjects[OID] as DependencyObject;
1192
                            if (dependency != null)
1193
                            {
1194
                                bool result = false;
1195
                                foreach (var attributes in dependency.AttributeSets)
1196
                                {
1197
                                    foreach (var attribute in attributes)
1198
                                    {
1199
                                        string name = attribute.Name;
1200
                                        string value = attribute.GetValue().ToString();
1201
                                        if (name == "DrawingItemType" && value == "LabelPersist")
1202
                                        {
1203
                                            foreach (DrawingObjectBase drawingObject in dependency.DrawingObjects)
1204
                                            {
1205
                                                if (drawingObject.Type == Ingr.RAD2D.ObjectType.igLineString2d)
1206
                                                {
1207
                                                    Ingr.RAD2D.LineString2d lineString2D = drawingObject as Ingr.RAD2D.LineString2d;
1208
                                                    double prevX = _TargetItem.get_XCoordinate();
1209
                                                    double prevY = _TargetItem.get_YCoordinate();
1210
                                                    lineString2D.InsertVertex(lineString2D.VertexCount, prevX, prevY);
1211
                                                    lineString2D.RemoveVertex(lineString2D.VertexCount);
1212
                                                    result = true;
1213
                                                    break;
1214
                                                }
1215
                                            }
1216
                                        }
1217

    
1218
                                        if (result)
1219
                                            break;
1220
                                    }
1221

    
1222
                                    if (result)
1223
                                        break;
1224
                                }
1225
                            }
1226

    
1227
                            symbol.SPPID.RepresentationId = _LMLabelPresist.AsLMRepresentation().Id;
1228
                            _LMLabelPresist.Commit();
1229
                            ReleaseCOMObjects(_LMLabelPresist);
1230
                        }
1231

    
1232
                        ReleaseCOMObjects(_TargetItem);
1233
                    }
1234
                }
1235
                else if (targetItem != null && targetItem.GetType() == typeof(Line))
1236
                {
1237
                    Line targetLine = targetItem as Line;
1238
                    Dictionary<LMConnector, List<double[]>> connectorVertices = GetPipeRunVertices(targetLine.SPPID.ModelItemId);
1239
                    LMConnector connectedLMConnector = FindTargetLMConnectorForLabel(connectorVertices, symbol.SPPID.ORIGINAL_X, symbol.SPPID.ORIGINAL_Y);
1240
                    if (connectedLMConnector != null)
1241
                    {
1242
                        // LEADER Line 검사
1243
                        bool leaderLine = false;
1244
                        SymbolMapping symbolMapping = document.SymbolMappings.Find(x => x.UID == symbol.DBUID);
1245
                        if (symbolMapping != null)
1246
                            leaderLine = symbolMapping.LEADERLINE;
1247

    
1248
                        LMLabelPersist _LMLabelPresist = _placement.PIDPlaceLabel(symbol.SPPID.MAPPINGNAME, ref points, Rotation: 0, LabeledItem: connectedLMConnector.AsLMRepresentation(), IsLeaderVisible: leaderLine);
1249
                        if (_LMLabelPresist != null)
1250
                        {
1251
                            _LMLabelPresist.Commit();
1252
                            ReleaseCOMObjects(_LMLabelPresist);
1253
                        }
1254
                        ReleaseCOMObjects(connectedLMConnector);
1255
                    }
1256

    
1257
                    foreach (var item in connectorVertices)
1258
                        if (item.Key != null)
1259
                            ReleaseCOMObjects(item.Key);
1260
                }
1261
            }
1262
        }
1263

    
1264
        /// <summary>
1265
        /// Equipment를 실제로 Modeling 메서드
1266
        /// </summary>
1267
        /// <param name="equipment"></param>
1268
        private void EquipmentModeling(Equipment equipment)
1269
        {
1270
            if (!string.IsNullOrEmpty(equipment.SPPID.RepresentationId))
1271
                return;
1272

    
1273
            LMSymbol _LMSymbol = null;
1274
            LMSymbol targetItem = null;
1275
            string mappingPath = equipment.SPPID.MAPPINGNAME;
1276
            double x = equipment.SPPID.ORIGINAL_X;
1277
            double y = equipment.SPPID.ORIGINAL_Y;
1278
            int mirror = 0;
1279
            double angle = equipment.ANGLE;
1280

    
1281
            SPPIDUtil.ConvertGridPoint(ref x, ref y);
1282

    
1283
            Connector connector = equipment.CONNECTORS.Find(conn => !string.IsNullOrEmpty(conn.CONNECTEDITEM) && conn.CONNECTEDITEM != "None");
1284
            if (connector != null)
1285
            {
1286
                Equipment connEquipment = SPPIDUtil.FindObjectByUID(document, connector.CONNECTEDITEM) as Equipment;
1287
                if (connEquipment != null)
1288
                {
1289
                    if (string.IsNullOrEmpty(connEquipment.SPPID.RepresentationId))
1290
                        EquipmentModeling(connEquipment);
1291

    
1292
                    if (!string.IsNullOrEmpty(connEquipment.SPPID.RepresentationId))
1293
                    {
1294
                        targetItem = dataSource.GetSymbol(connEquipment.SPPID.RepresentationId);
1295
                        if (targetItem != null)
1296
                        {
1297
                            _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle, TargetItem: targetItem);
1298
                        }
1299
                        else
1300
                        {
1301
                            _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle);
1302
                        }
1303
                    }
1304
                    else
1305
                    {
1306
                        _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle);
1307
                    }
1308
                }
1309
                else
1310
                {
1311
                    _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle);
1312
                }
1313
            }
1314
            else
1315
            {
1316
                _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle);
1317
            }
1318

    
1319
            if (_LMSymbol != null)
1320
            {
1321
                _LMSymbol.Commit();
1322
                equipment.SPPID.RepresentationId = _LMSymbol.AsLMRepresentation().Id;
1323
                equipment.SPPID.GraphicOID = _LMSymbol.get_GraphicOID().ToString();
1324
                ReleaseCOMObjects(_LMSymbol);
1325
            }
1326

    
1327
            if (targetItem != null)
1328
            {
1329
                ReleaseCOMObjects(targetItem);
1330
            }
1331

    
1332
            ReleaseCOMObjects(_LMSymbol);
1333
        }
1334

    
1335
        /// <summary>
1336
        /// 첫 진입점
1337
        /// </summary>
1338
        /// <param name="symbol"></param>
1339
        private void SymbolModelingBySymbol(Symbol symbol)
1340
        {
1341
            SymbolModeling(symbol, null);
1342
            List<object> endObjects = new List<object>();
1343
            endObjects.Add(symbol);
1344

    
1345
            foreach (var connector in symbol.CONNECTORS)
1346
            {
1347
                object connItem = SPPIDUtil.FindObjectByUID(document, connector.CONNECTEDITEM);
1348
                if (connItem != null && connItem.GetType() != typeof(Equipment))
1349
                {
1350
                    endObjects.Add(connItem);
1351
                    if (connItem.GetType() == typeof(Symbol))
1352
                    {
1353
                        Symbol connSymbol = connItem as Symbol;
1354
                        if (string.IsNullOrEmpty(connSymbol.SPPID.RepresentationId))
1355
                        {
1356
                            SymbolModeling(connSymbol, symbol);
1357
                        }
1358
                        SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetProgress, document.SYMBOLS.FindAll(x => !string.IsNullOrEmpty(x.SPPID.RepresentationId)).Count);
1359
                        SymbolModelingByNeerSymbolLoop(connSymbol, endObjects);
1360
                    }
1361
                    else if (connItem.GetType() == typeof(Line))
1362
                    {
1363
                        Line connLine = connItem as Line;
1364
                        SymbolModelingByNeerLineLoop(connLine, endObjects, symbol);
1365
                    }
1366
                }
1367
            }
1368
        }
1369

    
1370
        private void SymbolModelingByNeerSymbolLoop(Symbol symbol, List<object> endObjects)
1371
        {
1372
            foreach (var connector in symbol.CONNECTORS)
1373
            {
1374
                object connItem = SPPIDUtil.FindObjectByUID(document, connector.CONNECTEDITEM);
1375
                if (connItem != null && connItem.GetType() != typeof(Equipment))
1376
                {
1377
                    if (!endObjects.Contains(connItem))
1378
                    {
1379
                        endObjects.Add(connItem);
1380
                        if (connItem.GetType() == typeof(Symbol))
1381
                        {
1382
                            Symbol connSymbol = connItem as Symbol;
1383
                            if (string.IsNullOrEmpty(connSymbol.SPPID.RepresentationId))
1384
                            {
1385
                                SymbolModeling(connSymbol, symbol);
1386
                            }
1387
                            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetProgress, document.SYMBOLS.FindAll(x => !string.IsNullOrEmpty(x.SPPID.RepresentationId)).Count);
1388
                            SymbolModelingByNeerSymbolLoop(connSymbol, endObjects);
1389
                        }
1390
                        else if (connItem.GetType() == typeof(Line))
1391
                        {
1392
                            Line connLine = connItem as Line;
1393
                            SymbolModelingByNeerLineLoop(connLine, endObjects, symbol);
1394
                        }
1395
                    }
1396
                }
1397
            }
1398
        }
1399

    
1400
        private void SymbolModelingByNeerLineLoop(Line line, List<object> endObjects, Symbol prevSymbol)
1401
        {
1402
            foreach (var connector in line.CONNECTORS)
1403
            {
1404
                object connItem = SPPIDUtil.FindObjectByUID(document, connector.CONNECTEDITEM);
1405
                if (connItem != null && connItem.GetType() != typeof(Equipment))
1406
                {
1407
                    if (!endObjects.Contains(connItem))
1408
                    {
1409
                        endObjects.Add(connItem);
1410
                        if (connItem.GetType() == typeof(Symbol))
1411
                        {
1412
                            Symbol connSymbol = connItem as Symbol;
1413
                            if (string.IsNullOrEmpty(connSymbol.SPPID.RepresentationId))
1414
                            {
1415
                                List<Symbol> group = new List<Symbol>();
1416
                                SPPIDUtil.FindConnectedSymbolGroup(document, connSymbol, group);
1417
                                Symbol priority = prioritySymbols.Find(x => group.Contains(x));
1418
                                List<Symbol> endModelingGroup = new List<Symbol>();
1419
                                if (priority != null)
1420
                                {
1421
                                    SymbolGroupModeling(priority, group);
1422

    
1423
                                    // Range 겹치는지 확인해야함
1424
                                    double[] prevRange = null;
1425
                                    GetSPPIDSymbolRange(prevSymbol, ref prevRange);
1426
                                    double[] groupRange = null;
1427
                                    GetSPPIDSymbolRange(group, ref groupRange);
1428

    
1429
                                    double distanceX = 0;
1430
                                    double distanceY = 0;
1431
                                    bool overlapX = false;
1432
                                    bool overlapY = false;
1433
                                    SlopeType slopeType = SPPIDUtil.CalcSlope(prevSymbol.SPPID.ORIGINAL_X, prevSymbol.SPPID.ORIGINAL_Y, connSymbol.SPPID.ORIGINAL_X, connSymbol.SPPID.ORIGINAL_Y);
1434
                                    SPPIDUtil.CalcOverlap(prevRange, groupRange, ref distanceX, ref distanceY, ref overlapX, ref overlapY);
1435
                                    if ((slopeType == SlopeType.HORIZONTAL && overlapX) ||
1436
                                        (slopeType == SlopeType.VERTICAL && overlapY))
1437
                                    {
1438
                                        RemoveSymbol(group);
1439
                                        foreach (var _temp in group)
1440
                                            SPPIDUtil.CalcNewCoordinateForSymbol(_temp, prevSymbol, distanceX, distanceY);
1441

    
1442
                                        SymbolGroupModeling(priority, group);
1443
                                    }
1444
                                }
1445
                                else
1446
                                {
1447
                                    SymbolModeling(connSymbol, null);
1448
                                    // Range 겹치는지 확인해야함
1449
                                    double[] prevRange = null;
1450
                                    GetSPPIDSymbolRange(prevSymbol, ref prevRange);
1451
                                    double[] connRange = null;
1452
                                    GetSPPIDSymbolRange(connSymbol, ref connRange);
1453

    
1454
                                    double distanceX = 0;
1455
                                    double distanceY = 0;
1456
                                    bool overlapX = false;
1457
                                    bool overlapY = false;
1458
                                    SlopeType slopeType = SPPIDUtil.CalcSlope(prevSymbol.SPPID.ORIGINAL_X, prevSymbol.SPPID.ORIGINAL_Y, connSymbol.SPPID.ORIGINAL_X, connSymbol.SPPID.ORIGINAL_Y);
1459
                                    SPPIDUtil.CalcOverlap(prevRange, connRange, ref distanceX, ref distanceY, ref overlapX, ref overlapY);
1460
                                    if ((slopeType == SlopeType.HORIZONTAL && overlapX) ||
1461
                                        (slopeType == SlopeType.VERTICAL && overlapY))
1462
                                    {
1463
                                        RemoveSymbol(connSymbol);
1464
                                        SPPIDUtil.CalcNewCoordinateForSymbol(connSymbol, prevSymbol, distanceX, distanceY);
1465

    
1466
                                        SymbolModeling(connSymbol, null);
1467
                                    }
1468
                                }
1469
                            }
1470
                            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetProgress, document.SYMBOLS.FindAll(x => !string.IsNullOrEmpty(x.SPPID.RepresentationId)).Count);
1471
                            SymbolModelingByNeerSymbolLoop(connSymbol, endObjects);
1472
                        }
1473
                        else if (connItem.GetType() == typeof(Line))
1474
                        {
1475
                            Line connLine = connItem as Line;
1476
                            if (!SPPIDUtil.IsBranchLine(connLine, line))
1477
                                SymbolModelingByNeerLineLoop(connLine, endObjects, prevSymbol);
1478
                        }
1479
                    }
1480
                }
1481
            }
1482
        }
1483

    
1484
        private void SymbolGroupModeling(Symbol firstSymbol, List<Symbol> group)
1485
        {
1486
            List<Symbol> endModelingGroup = new List<Symbol>();
1487
            SymbolModeling(firstSymbol, null);
1488
            endModelingGroup.Add(firstSymbol);
1489
            while (endModelingGroup.Count != group.Count)
1490
            {
1491
                foreach (var _symbol in group)
1492
                {
1493
                    if (!endModelingGroup.Contains(_symbol))
1494
                    {
1495
                        foreach (var _connector in _symbol.CONNECTORS)
1496
                        {
1497
                            Symbol _connSymbol = SPPIDUtil.FindObjectByUID(document, _connector.CONNECTEDITEM) as Symbol;
1498
                            if (_connSymbol != null && endModelingGroup.Contains(_connSymbol))
1499
                            {
1500
                                SymbolModeling(_symbol, _connSymbol);
1501
                                endModelingGroup.Add(_symbol);
1502
                                break;
1503
                            }
1504
                        }
1505
                    }
1506
                }
1507
            }
1508
        }
1509

    
1510
        /// <summary>
1511
        /// 심볼을 실제로 Modeling할때 ChildSymbol이 있다면 Modeling하는 메서드
1512
        /// </summary>
1513
        /// <param name="childSymbol"></param>
1514
        /// <param name="parentSymbol"></param>
1515
        private void CreateChildSymbol(ChildSymbol childSymbol, LMSymbol parentSymbol, Symbol parent)
1516
        {
1517
            Ingr.RAD2D.Symbol2d symbol2d = radApp.ActiveDocument.ActiveSheet.DrawingObjects[parentSymbol.get_GraphicOID().ToString()];
1518
            double x1 = 0;
1519
            double x2 = 0;
1520
            double y1 = 0;
1521
            double y2 = 0;
1522
            symbol2d.Range(out x1, out y1, out x2, out y2);
1523

    
1524
            LMSymbol _LMSymbol = _placement.PIDPlaceSymbol(childSymbol.SPPID.MAPPINGNAME, (x1 + x2) / 2, (y1 + y2) / 2, TargetItem: parentSymbol);
1525
            if (_LMSymbol != null)
1526
            {
1527
                childSymbol.SPPID.RepresentationId = _LMSymbol.AsLMRepresentation().Id;
1528
                foreach (var item in childSymbol.ChildSymbols)
1529
                    CreateChildSymbol(item, _LMSymbol, parent);
1530
            }
1531

    
1532

    
1533
            ReleaseCOMObjects(_LMSymbol);
1534
        }
1535

    
1536
        private void NewLineModeling(Line line, bool isBranchModeling = false)
1537
        {
1538
            if (!string.IsNullOrEmpty(line.SPPID.ModelItemId) || (NewBranchLines.Contains(line) && !isBranchModeling))
1539
                return;
1540

    
1541
            List<Line> group = new List<Line>();
1542
            GetConnectedLineGroup(line, group);
1543
            LineCoordinateCorrection(group);
1544

    
1545
            foreach (var groupLine in group)
1546
            {
1547
                if (!isBranchModeling && SPPIDUtil.IsBranchLine(groupLine))
1548
                {
1549
                    NewBranchLines.Add(groupLine);
1550
                    continue;
1551
                }
1552

    
1553
                bool diagonal = false;
1554
                if (groupLine.SlopeType != SlopeType.HORIZONTAL && groupLine.SlopeType != SlopeType.VERTICAL)
1555
                    diagonal = true;
1556
                _LMAItem _LMAItem = _placement.PIDCreateItem(groupLine.SPPID.MAPPINGNAME);
1557
                LMSymbol _LMSymbolStart = null;
1558
                LMSymbol _LMSymbolEnd = null;
1559
                PlaceRunInputs placeRunInputs = new PlaceRunInputs();
1560
                foreach (var connector in groupLine.CONNECTORS)
1561
                {
1562
                    
1563
                    double x = 0;
1564
                    double y = 0;
1565
                    GetTargetLineConnectorPoint(connector, groupLine, ref x, ref y);
1566
                    if (connector.ConnectedObject == null)
1567
                    {
1568
                        placeRunInputs.AddPoint(x, y);
1569
                    }
1570
                    else if (connector.ConnectedObject.GetType() == typeof(Symbol))
1571
                    {
1572
                        Symbol targetSymbol = connector.ConnectedObject as Symbol;
1573
                        GetTargetSymbolConnectorPoint(targetSymbol.CONNECTORS.Find(z => z.ConnectedObject == groupLine), targetSymbol, ref x, ref y);
1574
                        if (groupLine.CONNECTORS.IndexOf(connector) == 0)
1575
                        {
1576
                            _LMSymbolStart = GetTargetSymbol(targetSymbol, groupLine);
1577
                            if (_LMSymbolStart != null)
1578
                                placeRunInputs.AddSymbolTarget(_LMSymbolStart, x, y, diagonal);
1579
                            else
1580
                                placeRunInputs.AddPoint(x, y);
1581
                        }
1582
                        else
1583
                        {
1584
                            _LMSymbolEnd = GetTargetSymbol(targetSymbol, groupLine);
1585
                            if (_LMSymbolEnd != null)
1586
                                placeRunInputs.AddSymbolTarget(_LMSymbolEnd, x, y, diagonal);
1587
                            else
1588
                                placeRunInputs.AddPoint(x, y);
1589
                        }
1590
                    }
1591
                    else if (connector.ConnectedObject.GetType() == typeof(Line))
1592
                    {
1593
                        Line targetLine = connector.ConnectedObject as Line;
1594
                        if (!string.IsNullOrEmpty(targetLine.SPPID.ModelItemId))
1595
                        {
1596
                            LMConnector targetConnector = FindTargetLMConnectorForBranch(line, targetLine, ref x, ref y);
1597
                            placeRunInputs.AddConnectorTarget(targetConnector, x, y, diagonal);
1598
                            ChangeLineSPPIDCoordinateByConnector(groupLine, targetLine, x, y, false);
1599
                        }
1600
                        else
1601
                        {
1602
                            if (groupLine.CONNECTORS.IndexOf(connector) == 0)
1603
                            {
1604
                                if (groupLine.SlopeType == SlopeType.HORIZONTAL)
1605
                                    placeRunInputs.AddPoint(x, -0.1);
1606
                                else if (groupLine.SlopeType == SlopeType.VERTICAL)
1607
                                    placeRunInputs.AddPoint(-0.1, y);
1608
                                else
1609
                                {
1610
                                    if (SPPIDUtil.CalcAngle(groupLine.SPPID.START_X, groupLine.SPPID.START_Y, groupLine.SPPID.END_X, groupLine.SPPID.END_Y) < 45)
1611
                                        placeRunInputs.AddPoint(-0.1, y);
1612
                                    else
1613
                                        placeRunInputs.AddPoint(x, -0.1);
1614
                                }
1615
                            }
1616

    
1617
                            placeRunInputs.AddPoint(x, y);
1618

    
1619
                            if (groupLine.CONNECTORS.IndexOf(connector) == 1)
1620
                            {
1621
                                if (groupLine.SlopeType == SlopeType.HORIZONTAL)
1622
                                    placeRunInputs.AddPoint(x, -0.1);
1623
                                else if (groupLine.SlopeType == SlopeType.VERTICAL)
1624
                                    placeRunInputs.AddPoint(-0.1, y);
1625
                                else
1626
                                {
1627
                                    if (SPPIDUtil.CalcAngle(groupLine.SPPID.START_X, groupLine.SPPID.START_Y, groupLine.SPPID.END_X, groupLine.SPPID.END_Y) < 45)
1628
                                        placeRunInputs.AddPoint(-0.1, y);
1629
                                    else
1630
                                        placeRunInputs.AddPoint(x, -0.1);
1631
                                }
1632
                                    
1633
                            }
1634
                        }
1635
                    }
1636
                }
1637

    
1638
                LMConnector _lMConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
1639
                if (_lMConnector != null)
1640
                {
1641
                    groupLine.SPPID.ModelItemId = _lMConnector.ModelItemID;
1642
                    bool bRemodelingStart = false;
1643
                    if (_LMSymbolStart != null)
1644
                        NeedReModeling(groupLine, _LMSymbolStart, ref bRemodelingStart);
1645
                    bool bRemodelingEnd = false;
1646
                    if (_LMSymbolEnd != null)
1647
                        NeedReModeling(groupLine, _LMSymbolEnd, ref bRemodelingEnd);
1648

    
1649
                    if (bRemodelingStart || bRemodelingEnd)
1650
                        ReModelingLine(groupLine, _lMConnector, _LMSymbolStart, _LMSymbolEnd, bRemodelingStart, bRemodelingEnd);
1651

    
1652
                    FlowMarkModeling(groupLine);
1653
                    LineNumberModeling(groupLine);
1654

    
1655
                    ReleaseCOMObjects(_lMConnector);
1656
                }
1657

    
1658
                List<object> removeLines = groupLine.CONNECTORS.FindAll(x =>
1659
                x.ConnectedObject != null &&
1660
                x.ConnectedObject.GetType() == typeof(Line) &&
1661
                !string.IsNullOrEmpty(((Line)x.ConnectedObject).SPPID.ModelItemId))
1662
                .Select(x => x.ConnectedObject)
1663
                .ToList();
1664

    
1665
                foreach (var item in removeLines)
1666
                    RemoveLineForModeling(item as Line);
1667

    
1668
                if (_LMAItem != null)
1669
                    ReleaseCOMObjects(_LMAItem);
1670
                if (placeRunInputs != null)
1671
                    ReleaseCOMObjects(placeRunInputs);
1672
                if (_LMSymbolStart != null)
1673
                    ReleaseCOMObjects(_LMSymbolStart);
1674
                if (_LMSymbolEnd != null)
1675
                    ReleaseCOMObjects(_LMSymbolEnd);
1676

    
1677
                if (isBranchModeling && NewBranchLines.Contains(groupLine))
1678
                    NewBranchLines.Remove(groupLine);
1679
            }
1680
        }
1681

    
1682
        private void RemoveLineForModeling(Line line)
1683
        {
1684
            LMModelItem modelItem = dataSource.GetModelItem(line.SPPID.ModelItemId);
1685
            if (modelItem != null)
1686
            {
1687
                foreach (LMRepresentation rep in modelItem.Representations)
1688
                {
1689
                    if (rep.Attributes["RepresentationType"].get_Value() == "Connector" && rep.Attributes["ItemStatus"].get_Value() == "Active")
1690
                    {
1691
                        LMConnector _LMConnector = dataSource.GetConnector(rep.Id);
1692
                        dynamic OID = rep.get_GraphicOID().ToString();
1693
                        DependencyObject drawingObject = radApp.ActiveDocument.ActiveSheet.DrawingObjects[OID];
1694
                        Ingr.RAD2D.LineStringGeometry2d lineStringGeometry = drawingObject.GetGeometry() as Ingr.RAD2D.LineStringGeometry2d;
1695
                        int verticesCount = lineStringGeometry.VertexCount;
1696
                        double[] vertices = null;
1697
                        lineStringGeometry.GetVertices(ref verticesCount, ref vertices);
1698
                        for (int i = 0; i < verticesCount; i++)
1699
                        {
1700
                            double x = 0;
1701
                            double y = 0;
1702
                            lineStringGeometry.GetVertex(i + 1, ref x, ref y);
1703
                            if (verticesCount == 2 && x < 0 || y < 0)
1704
                                _placement.PIDRemovePlacement(rep);
1705
                        }
1706
                        ReleaseCOMObjects(_LMConnector);
1707
                    }
1708
                }
1709

    
1710
                ReleaseCOMObjects(modelItem);
1711
            }
1712
        }
1713

    
1714
        private void GetConnectedLineGroup(Line line, List<Line> group)
1715
        {
1716
            if (!group.Contains(line))
1717
                group.Add(line);
1718
            foreach (var connector in line.CONNECTORS)
1719
            {
1720
                if (connector.ConnectedObject != null &&
1721
                    connector.ConnectedObject.GetType() == typeof(Line) &&
1722
                    !group.Contains(connector.ConnectedObject) &&
1723
                    string.IsNullOrEmpty(((Line)connector.ConnectedObject).SPPID.ModelItemId))
1724
                {
1725
                    Line connLine = connector.ConnectedObject as Line;
1726
                    if (!SPPIDUtil.IsBranchLine(connLine, line))
1727
                    {
1728
                        if (line.CONNECTORS.IndexOf(connector) == 0)
1729
                            group.Insert(0, connLine);
1730
                        else
1731
                            group.Add(connLine);
1732
                        GetConnectedLineGroup(connLine, group);
1733
                    }
1734
                }
1735
            }
1736
        }
1737

    
1738
        private void LineCoordinateCorrection(List<Line> group)
1739
        {
1740
            // 순서대로 전 Item 기준 정렬
1741
            LineCoordinateCorrectionByStart(group);
1742

    
1743
            // 역으로 심볼이 있을 경우 좌표 보정
1744
            LineCoordinateCorrectionForLastLine(group);
1745
        }
1746

    
1747
        private void LineCoordinateCorrectionByStart(List<Line> group)
1748
        {
1749
            for (int i = 0; i < group.Count; i++)
1750
            {
1751
                Line line = group[i];
1752
                if (i == 0)
1753
                {
1754
                    Connector symbolConnector = line.CONNECTORS.Find(x => x.ConnectedObject != null && x.ConnectedObject.GetType() == typeof(Symbol));
1755
                    if (symbolConnector != null)
1756
                        LineCoordinateCorrectionByConnItem(line, symbolConnector.ConnectedObject);
1757
                }
1758
                else if (i != 0)
1759
                {
1760
                    LineCoordinateCorrectionByConnItem(line, group[i - 1]);
1761
                }
1762
            }
1763
        }
1764

    
1765
        private void LineCoordinateCorrectionForLastLine(List<Line> group)
1766
        {
1767
            Line checkLine = group[group.Count - 1];
1768
            Connector lastSymbolConnector = checkLine.CONNECTORS.Find(x => x.ConnectedObject != null && x.ConnectedObject.GetType() == typeof(Symbol));
1769
            if (lastSymbolConnector != null)
1770
            {
1771
                LineCoordinateCorrectionByConnItem(checkLine, lastSymbolConnector.ConnectedObject);
1772
                for (int i = group.Count - 2; i >= 0; i--)
1773
                {
1774
                    Line line = group[i + 1];
1775
                    Line prevLine = group[i];
1776

    
1777
                    // 같으면 보정
1778
                    if (line.SlopeType == prevLine.SlopeType)
1779
                        LineCoordinateCorrectionByConnItem(prevLine, line);
1780
                    else
1781
                    {
1782
                        if (line.SlopeType == SlopeType.HORIZONTAL)
1783
                        {
1784
                            double prevX = 0;
1785
                            double prevY = 0;
1786
                            GetTargetLineConnectorPoint(prevLine.CONNECTORS.Find(z => z.ConnectedObject == line), prevLine, ref prevX, ref prevY);
1787
                            ChangeLineSPPIDCoordinateByConnectorOnlyX(line, prevLine, prevX);
1788

    
1789
                            double x = 0;
1790
                            double y = 0;
1791
                            GetTargetLineConnectorPoint(line.CONNECTORS.Find(z => z.ConnectedObject == prevLine), line, ref x, ref y);
1792
                            ChangeLineSPPIDCoordinateByConnectorOnlyY(prevLine, line, y);
1793
                        }
1794
                        else if (line.SlopeType == SlopeType.VERTICAL)
1795
                        {
1796
                            double prevX = 0;
1797
                            double prevY = 0;
1798
                            GetTargetLineConnectorPoint(prevLine.CONNECTORS.Find(z => z.ConnectedObject == line), prevLine, ref prevX, ref prevY);
1799
                            ChangeLineSPPIDCoordinateByConnectorOnlyY(line, prevLine, prevY);
1800

    
1801
                            double x = 0;
1802
                            double y = 0;
1803
                            GetTargetLineConnectorPoint(line.CONNECTORS.Find(z => z.ConnectedObject == prevLine), line, ref x, ref y);
1804
                            ChangeLineSPPIDCoordinateByConnectorOnlyX(prevLine, line, x);
1805
                        }
1806
                        break;
1807
                    }
1808
                }
1809
            }
1810
        }
1811

    
1812
        private void LineCoordinateCorrectionByConnItem(Line line, object connItem)
1813
        {
1814
            double x = 0;
1815
            double y = 0;
1816
            if (connItem.GetType() == typeof(Symbol))
1817
            {
1818
                Symbol targetSymbol = connItem as Symbol;
1819
                Connector targetConnector = targetSymbol.CONNECTORS.Find(z => z.ConnectedObject == line);
1820
                if (targetConnector != null)
1821
                    GetTargetSymbolConnectorPoint(targetConnector, targetSymbol, ref x, ref y);
1822
                else
1823
                    throw new Exception("Target symbol UID : " + targetSymbol.UID + "\r\nLine UID : " + line.UID);
1824
            }
1825
            else if (connItem.GetType() == typeof(Line))
1826
            {
1827
                Line targetLine = connItem as Line;
1828
                GetTargetLineConnectorPoint(targetLine.CONNECTORS.Find(z => z.ConnectedObject == line), targetLine, ref x, ref y);
1829
            }
1830

    
1831
            ChangeLineSPPIDCoordinateByConnector(line, connItem, x, y);
1832
        }
1833

    
1834
        private void ChangeLineSPPIDCoordinateByConnector(Line line, object connItem, double x, double y, bool changeOtherCoordinate = true)
1835
        {
1836
            Connector connector = line.CONNECTORS.Find(z => z.ConnectedObject == connItem);
1837
            int index = line.CONNECTORS.IndexOf(connector);
1838
            if (index == 0)
1839
            {
1840
                line.SPPID.START_X = x;
1841
                line.SPPID.START_Y = y;
1842
                if (line.SlopeType == SlopeType.HORIZONTAL && changeOtherCoordinate)
1843
                    line.SPPID.END_Y = y;
1844
                else if (line.SlopeType == SlopeType.VERTICAL && changeOtherCoordinate)
1845
                    line.SPPID.END_X = x;
1846
            }
1847
            else
1848
            {
1849
                line.SPPID.END_X = x;
1850
                line.SPPID.END_Y = y;
1851
                if (line.SlopeType == SlopeType.HORIZONTAL && changeOtherCoordinate)
1852
                    line.SPPID.START_Y = y;
1853
                else if (line.SlopeType == SlopeType.VERTICAL && changeOtherCoordinate)
1854
                    line.SPPID.START_X = x;
1855
            }
1856
        }
1857

    
1858
        private void ChangeLineSPPIDCoordinateByConnectorOnlyX(Line line, object connItem, double x)
1859
        {
1860
            Connector connector = line.CONNECTORS.Find(z => z.ConnectedObject == connItem);
1861
            int index = line.CONNECTORS.IndexOf(connector);
1862
            if (index == 0)
1863
            {
1864
                line.SPPID.START_X = x;
1865
                if (line.SlopeType == SlopeType.VERTICAL)
1866
                    line.SPPID.END_X = x;
1867
            }
1868
            else
1869
            {
1870
                line.SPPID.END_X = x;
1871
                if (line.SlopeType == SlopeType.VERTICAL)
1872
                    line.SPPID.START_X = x;
1873
            }
1874
        }
1875

    
1876
        private void ChangeLineSPPIDCoordinateByConnectorOnlyY(Line line, object connItem, double y)
1877
        {
1878
            Connector connector = line.CONNECTORS.Find(z => z.ConnectedObject == connItem);
1879
            int index = line.CONNECTORS.IndexOf(connector);
1880
            if (index == 0)
1881
            {
1882
                line.SPPID.START_Y = y;
1883
                if (line.SlopeType == SlopeType.HORIZONTAL)
1884
                    line.SPPID.END_Y = y;
1885
            }
1886
            else
1887
            {
1888
                line.SPPID.END_Y = y;
1889
                if (line.SlopeType == SlopeType.HORIZONTAL)
1890
                    line.SPPID.START_Y = y;
1891
            }
1892
        }
1893

    
1894
        private void NeedReModeling(Line line, LMSymbol symbol, ref bool result)
1895
        {
1896
            if (symbol != null)
1897
            {
1898
                string repID = symbol.AsLMRepresentation().Id;
1899
                string symbolUID = SPPIDUtil.FindSymbolByRepresentationID(document, repID).UID;
1900
                string lineUID = line.UID;
1901

    
1902
                SpecBreak startSpecBreak = document.SpecBreaks.Find(x =>
1903
                (x.DownStreamUID == symbolUID || x.UpStreamUID == symbolUID) &&
1904
                (x.DownStreamUID == lineUID || x.UpStreamUID == lineUID));
1905

    
1906
                EndBreak startEndBreak = document.EndBreaks.Find(x =>
1907
                (x.OWNER == symbolUID || x.PROPERTIES.Find(y => y.ATTRIBUTE == "Connected Item").VALUE == symbolUID) &&
1908
                (x.OWNER == lineUID || x.PROPERTIES.Find(y => y.ATTRIBUTE == "Connected Item").VALUE == lineUID));
1909

    
1910
                if (startSpecBreak != null || startEndBreak != null)
1911
                    result = true;
1912
            }
1913
        }
1914

    
1915
        /// <summary>
1916
        /// Symbol에 붙을 경우 Line을 Remodeling 한다.
1917
        /// </summary>
1918
        /// <param name="lines"></param>
1919
        /// <param name="prevLMConnector"></param>
1920
        /// <param name="startSymbol"></param>
1921
        /// <param name="endSymbol"></param>
1922
        private void ReModelingLine(Line line, LMConnector prevLMConnector, LMSymbol startSymbol, LMSymbol endSymbol, bool bStart, bool bEnd)
1923
        {
1924
            string symbolPath = string.Empty;
1925
            #region get symbol path
1926
            LMModelItem modelItem = dataSource.GetModelItem(prevLMConnector.ModelItemID);
1927
            foreach (LMRepresentation rep in modelItem.Representations)
1928
            {
1929
                if (!DBNull.Value.Equals(rep.get_FileName()) && !string.IsNullOrEmpty(rep.get_FileName()))
1930
                {
1931
                    symbolPath = rep.get_FileName();
1932
                    break;
1933
                }
1934
            }
1935
            #endregion
1936
            bool diagonal = false;
1937
            if (line.SlopeType != SlopeType.HORIZONTAL && line.SlopeType != SlopeType.VERTICAL)
1938
                diagonal = true;
1939
            _LMAItem _LMAItem = _placement.PIDCreateItem(symbolPath);
1940
            LMConnector newConnector = null;
1941
            dynamic OID = prevLMConnector.get_GraphicOID().ToString();
1942
            DependencyObject drawingObject = radApp.ActiveDocument.ActiveSheet.DrawingObjects[OID];
1943
            Ingr.RAD2D.LineStringGeometry2d lineStringGeometry = drawingObject.GetGeometry() as Ingr.RAD2D.LineStringGeometry2d;
1944
            int verticesCount = lineStringGeometry.VertexCount;
1945
            PlaceRunInputs placeRunInputs = new PlaceRunInputs();
1946

    
1947
            List<double[]> vertices = new List<double[]>();
1948
            for (int i = 1; i <= verticesCount; i++)
1949
            {
1950
                double x = 0;
1951
                double y = 0;
1952
                lineStringGeometry.GetVertex(i, ref x, ref y);
1953
                vertices.Add(new double[] { x, y });
1954
            }
1955

    
1956
            for (int i = 0; i < vertices.Count; i++)
1957
            {
1958
                double[] points = vertices[i];
1959
                // 시작 심볼이 있고 첫번째 좌표일 때
1960
                if (startSymbol != null && i == 0)
1961
                {
1962
                    if (bStart)
1963
                    {
1964
                        SlopeType slopeType = SPPIDUtil.CalcSlope(points[0], points[1], vertices[i + 1][0], vertices[i + 1][1]);
1965
                        if (slopeType == SlopeType.HORIZONTAL)
1966
                            placeRunInputs.AddPoint(points[0], -0.1);
1967
                        else if (slopeType == SlopeType.VERTICAL)
1968
                            placeRunInputs.AddPoint(-0.1, points[1]);
1969
                        else
1970
                            placeRunInputs.AddPoint(points[0], -0.1);
1971

    
1972
                        placeRunInputs.AddPoint(points[0], points[1]);
1973
                    }
1974
                    else
1975
                    {
1976
                        placeRunInputs.AddSymbolTarget(startSymbol, points[0], points[1], diagonal);
1977
                    }
1978
                }
1979
                // 마지막 심볼이 있고 마지막 좌표일 때
1980
                else if (endSymbol != null && i == vertices.Count - 1)
1981
                {
1982
                    if (bEnd)
1983
                    {
1984
                        placeRunInputs.AddPoint(points[0], points[1]);
1985

    
1986
                        SlopeType slopeType = SPPIDUtil.CalcSlope(points[0], points[1], vertices[i - 1][0], vertices[i - 1][1]);
1987
                        if (slopeType == SlopeType.HORIZONTAL)
1988
                            placeRunInputs.AddPoint(points[0], -0.1);
1989
                        else if (slopeType == SlopeType.VERTICAL)
1990
                            placeRunInputs.AddPoint(-0.1, points[1]);
1991
                        else
1992
                            placeRunInputs.AddPoint(points[0], -0.1);
1993
                    }
1994
                    else
1995
                    {
1996
                        placeRunInputs.AddSymbolTarget(endSymbol, points[0], points[1], diagonal);
1997
                    }
1998
                }
1999
                // 첫번째이며 시작 심볼이 아니고 Connecotr일 경우
2000
                else if (i == 0 && prevLMConnector.ConnectItem1SymbolObject != null)
2001
                    placeRunInputs.AddSymbolTarget(prevLMConnector.ConnectItem1SymbolObject, points[0], points[1], diagonal);
2002
                // 마지막이며 마지막 심볼이 아니고 Connecotr일 경우
2003
                else if (i == vertices.Count - 1 && prevLMConnector.ConnectItem2SymbolObject != null)
2004
                    placeRunInputs.AddSymbolTarget(prevLMConnector.ConnectItem2SymbolObject, points[0], points[1], diagonal);
2005
                else
2006
                    placeRunInputs.AddPoint(points[0], points[1]);
2007
            }
2008

    
2009
            _placement.PIDRemovePlacement(prevLMConnector.AsLMRepresentation());
2010
            newConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
2011

    
2012
            ReleaseCOMObjects(placeRunInputs);
2013
            ReleaseCOMObjects(_LMAItem);
2014
            ReleaseCOMObjects(modelItem);
2015

    
2016
            if (newConnector != null)
2017
            {
2018
                if (startSymbol != null && bStart)
2019
                {
2020
                    _LMAItem = _placement.PIDCreateItem(symbolPath);
2021
                    placeRunInputs = new PlaceRunInputs();
2022
                    placeRunInputs.AddSymbolTarget(startSymbol, vertices[0][0], vertices[0][1]);
2023
                    placeRunInputs.AddConnectorTarget(newConnector, vertices[0][0], vertices[0][1]);
2024
                    LMConnector _LMConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
2025
                    if (_LMConnector != null)
2026
                    {
2027
                        RemoveConnectorForReModelingLine(newConnector);
2028
                        ZeroLengthModelItemID.Add(_LMConnector.ModelItemID);
2029
                        ReleaseCOMObjects(_LMConnector);
2030
                    }
2031
                    ReleaseCOMObjects(placeRunInputs);
2032
                    ReleaseCOMObjects(_LMAItem);
2033
                }
2034

    
2035
                if (endSymbol != null && bEnd)
2036
                {
2037
                    if (startSymbol != null)
2038
                    {
2039
                        Dictionary<LMConnector, List<double[]>> dicVertices = GetPipeRunVertices(newConnector.ModelItemID);
2040
                        newConnector = dicVertices.First().Key;
2041
                    }
2042

    
2043
                    _LMAItem = _placement.PIDCreateItem(symbolPath);
2044
                    placeRunInputs = new PlaceRunInputs();
2045
                    placeRunInputs.AddSymbolTarget(endSymbol, vertices[vertices.Count - 1][0], vertices[vertices.Count - 1][1]);
2046
                    placeRunInputs.AddConnectorTarget(newConnector, vertices[vertices.Count - 1][0], vertices[vertices.Count - 1][1]);
2047
                    LMConnector _LMConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
2048
                    if (_LMConnector != null)
2049
                    {
2050
                        RemoveConnectorForReModelingLine(newConnector);
2051
                        ZeroLengthModelItemIDReverse.Add(_LMConnector.ModelItemID);
2052
                        ReleaseCOMObjects(_LMConnector);
2053
                    }
2054
                    ReleaseCOMObjects(placeRunInputs);
2055
                    ReleaseCOMObjects(_LMAItem);
2056
                }
2057

    
2058
                line.SPPID.ModelItemId = newConnector.ModelItemID;
2059
                ReleaseCOMObjects(newConnector);
2060
            }
2061

    
2062
            ReleaseCOMObjects(modelItem);
2063
        }
2064

    
2065
        /// <summary>
2066
        /// Remodeling 과정에서 생긴 불필요한 Connector 제거
2067
        /// </summary>
2068
        /// <param name="connector"></param>
2069
        private void RemoveConnectorForReModelingLine(LMConnector connector)
2070
        {
2071
            Dictionary<LMConnector, List<double[]>> dicVertices = GetPipeRunVertices(connector.ModelItemID);
2072
            foreach (var item in dicVertices)
2073
            {
2074
                if (item.Value.Count == 2)
2075
                {
2076
                    bool result = false;
2077
                    foreach (var point in item.Value)
2078
                    {
2079
                        if (point[0] < 0 || point[1] < 0)
2080
                        {
2081
                            result = true;
2082
                            _placement.PIDRemovePlacement(item.Key.AsLMRepresentation());
2083
                            break;
2084
                        }
2085
                    }
2086

    
2087
                    if (result)
2088
                        break;
2089
                }
2090
            }
2091
            foreach (var item in dicVertices)
2092
                ReleaseCOMObjects(item.Key);
2093
        }
2094

    
2095
        /// <summary>
2096
        /// Symbol이 모델링된 SPPPID Symbol Object를 반환 - 연결된 Symbol이 ChildSymbol일 수도 있기때문에 메서드 개발
2097
        /// </summary>
2098
        /// <param name="symbol"></param>
2099
        /// <param name="line"></param>
2100
        /// <returns></returns>
2101
        private LMSymbol GetTargetSymbol(Symbol symbol, Line line)
2102
        {
2103
            LMSymbol _LMSymbol = null;
2104
            foreach (var connector in symbol.CONNECTORS)
2105
            {
2106
                if (connector.CONNECTEDITEM == line.UID)
2107
                {
2108
                    if (connector.Index == 0)
2109
                        _LMSymbol = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
2110
                    else
2111
                    {
2112
                        ChildSymbol child = null;
2113
                        foreach (var childSymbol in symbol.ChildSymbols)
2114
                        {
2115
                            if (childSymbol.Connectors.Contains(connector))
2116
                                child = childSymbol;
2117
                            else
2118
                                child = GetChildSymbolByConnector(childSymbol, connector);
2119

    
2120
                            if (child != null)
2121
                                break;
2122
                        }
2123

    
2124
                        if (child != null)
2125
                            _LMSymbol = dataSource.GetSymbol(child.SPPID.RepresentationId);
2126
                    }
2127

    
2128
                    break;
2129
                }
2130
            }
2131

    
2132
            return _LMSymbol;
2133
        }
2134

    
2135
        /// <summary>
2136
        /// Connector를 가지고 있는 ChildSymbol Object 반환
2137
        /// </summary>
2138
        /// <param name="item"></param>
2139
        /// <param name="connector"></param>
2140
        /// <returns></returns>
2141
        private ChildSymbol GetChildSymbolByConnector(ChildSymbol item, Connector connector)
2142
        {
2143
            foreach (var childSymbol in item.ChildSymbols)
2144
            {
2145
                if (childSymbol.Connectors.Contains(connector))
2146
                    return childSymbol;
2147
                else
2148
                    return GetChildSymbolByConnector(childSymbol, connector);
2149
            }
2150

    
2151
            return null;
2152
        }
2153

    
2154
        /// <summary>
2155
        /// EndBreak 모델링 메서드
2156
        /// </summary>
2157
        /// <param name="endBreak"></param>
2158
        private void EndBreakModeling(EndBreak endBreak)
2159
        {
2160
            object ownerObj = SPPIDUtil.FindObjectByUID(document, endBreak.OWNER);
2161
            object connectedItem = SPPIDUtil.FindObjectByUID(document, endBreak.PROPERTIES.Find(x => x.ATTRIBUTE == "Connected Item").VALUE);
2162
            LMConnector targetLMConnector = FindBreakLineTarget(ownerObj, connectedItem);
2163

    
2164
            if (targetLMConnector != null)
2165
            {
2166
                Array array = new double[] { 0, endBreak.SPPID.ORIGINAL_X, endBreak.SPPID.ORIGINAL_Y };
2167
                LMLabelPersist _LmLabelPersist = _placement.PIDPlaceLabel(endBreak.SPPID.MAPPINGNAME, ref array, Rotation: 0, LabeledItem: targetLMConnector.AsLMRepresentation(), IsLeaderVisible: true);
2168
                if (_LmLabelPersist != null)
2169
                {
2170
                    endBreak.SPPID.RepresentationId = _LmLabelPersist.AsLMRepresentation().Id;
2171
                    if (_LmLabelPersist.ModelItemObject != null)
2172
                        endBreak.SPPID.ModelItemID = _LmLabelPersist.ModelItemID;
2173
                    endBreak.SPPID.GraphicOID = _LmLabelPersist.get_GraphicOID().ToString();
2174
                    ReleaseCOMObjects(_LmLabelPersist);
2175
                }
2176
                ReleaseCOMObjects(targetLMConnector);
2177
            }
2178
        }
2179

    
2180
        private LMConnector ReModelingLMConnector(LMConnector connector)
2181
        {
2182
            string symbolPath = string.Empty;
2183
            #region get symbol path
2184
            LMModelItem modelItem = dataSource.GetModelItem(connector.ModelItemID);
2185
            foreach (LMRepresentation rep in modelItem.Representations)
2186
            {
2187
                if (!DBNull.Value.Equals(rep.get_FileName()) && !string.IsNullOrEmpty(rep.get_FileName()))
2188
                {
2189
                    symbolPath = rep.get_FileName();
2190
                    break;
2191
                }
2192
            }
2193
            #endregion
2194

    
2195
            LMConnector newConnector = null;
2196
            dynamic OID = connector.get_GraphicOID().ToString();
2197
            DependencyObject drawingObject = radApp.ActiveDocument.ActiveSheet.DrawingObjects[OID];
2198
            Ingr.RAD2D.LineStringGeometry2d lineStringGeometry = drawingObject.GetGeometry() as Ingr.RAD2D.LineStringGeometry2d;
2199
            int verticesCount = lineStringGeometry.VertexCount;
2200
            PlaceRunInputs placeRunInputs = new PlaceRunInputs();
2201
            _LMAItem _LMAItem = _placement.PIDCreateItem(symbolPath);
2202

    
2203
            if (Convert.ToBoolean(connector.get_IsZeroLength()))
2204
            {
2205
                double[] vertices = null;
2206
                lineStringGeometry.GetVertices(ref verticesCount, ref vertices);
2207
                double x = 0;
2208
                double y = 0;
2209
                lineStringGeometry.GetVertex(1, ref x, ref y);
2210

    
2211
                placeRunInputs.AddSymbolTarget(connector.ConnectItem1SymbolObject, x, y);
2212
                placeRunInputs.AddSymbolTarget(connector.ConnectItem2SymbolObject, x, y);
2213

    
2214
                string flowDirection = string.Empty;
2215
                LMAAttribute flowAttribute = connector.ModelItemObject.Attributes["FlowDirection"];
2216
                if (flowAttribute != null && !DBNull.Value.Equals(flowAttribute.get_Value()))
2217
                    flowDirection = flowAttribute.get_Value().ToString();
2218

    
2219
                _placement.PIDRemovePlacement(connector.AsLMRepresentation());
2220
                newConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
2221
                ZeroLengthSymbolToSymbolModelItemID.Add(newConnector.ModelItemID);
2222
                if (!string.IsNullOrEmpty(flowDirection))
2223
                    newConnector.ModelItemObject.Attributes["FlowDirection"].set_Value(flowDirection);
2224
            }
2225
            else
2226
            {
2227
                List<double[]> vertices = new List<double[]>();
2228
                for (int i = 1; i <= verticesCount; i++)
2229
                {
2230
                    double x = 0;
2231
                    double y = 0;
2232
                    lineStringGeometry.GetVertex(i, ref x, ref y);
2233
                    vertices.Add(new double[] { x, y });
2234
                }
2235

    
2236
                for (int i = 0; i < vertices.Count; i++)
2237
                {
2238
                    double[] points = vertices[i];
2239
                    if (i == 0)
2240
                    {
2241
                        if (connector.ConnectItem1SymbolObject != null)
2242
                            placeRunInputs.AddSymbolTarget(connector.ConnectItem1SymbolObject, points[0], points[1]);
2243
                        else
2244
                            placeRunInputs.AddPoint(points[0], points[1]);
2245
                    }
2246
                    else if (i == vertices.Count - 1)
2247
                    {
2248
                        if (connector.ConnectItem2SymbolObject != null)
2249
                            placeRunInputs.AddSymbolTarget(connector.ConnectItem2SymbolObject, points[0], points[1]);
2250
                        else
2251
                            placeRunInputs.AddPoint(points[0], points[1]);
2252
                    }
2253
                    else
2254
                        placeRunInputs.AddPoint(points[0], points[1]);
2255
                }
2256

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

    
2259
                _placement.PIDRemovePlacement(connector.AsLMRepresentation());
2260
                newConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
2261

    
2262
                foreach (var line in lines)
2263
                    line.SPPID.ModelItemId = newConnector.ModelItemID;
2264
            }
2265

    
2266

    
2267
            return newConnector;
2268
        }
2269

    
2270
        /// <summary>
2271
        /// SpecBreak Modeling 메서드
2272
        /// </summary>
2273
        /// <param name="specBreak"></param>
2274
        private void SpecBreakModeling(SpecBreak specBreak)
2275
        {
2276
            object upStreamObj = SPPIDUtil.FindObjectByUID(document, specBreak.UpStreamUID);
2277
            object downStreamObj = SPPIDUtil.FindObjectByUID(document, specBreak.DownStreamUID);
2278

    
2279
            if (upStreamObj != null &&
2280
                downStreamObj != null)
2281
            {
2282
                LMConnector targetLMConnector = FindBreakLineTarget(upStreamObj, downStreamObj);
2283

    
2284
                if (targetLMConnector != null)
2285
                {
2286
                    foreach (var attribute in specBreak.ATTRIBUTES)
2287
                    {
2288
                        AttributeMapping mapping = document.AttributeMappings.Find(x => x.UID == attribute.UID);
2289
                        if (mapping != null && !string.IsNullOrEmpty(mapping.SPPIDSYMBOLNAME) && mapping.SPPIDSYMBOLNAME != "None")
2290
                        {
2291
                            string MappingPath = mapping.SPPIDSYMBOLNAME;
2292
                            Array array = new double[] { 0, specBreak.SPPID.ORIGINAL_X, specBreak.SPPID.ORIGINAL_Y };
2293
                            LMLabelPersist _LmLabelPersist = _placement.PIDPlaceLabel(MappingPath, ref array, Rotation: specBreak.ANGLE, LabeledItem: targetLMConnector.AsLMRepresentation(), IsLeaderVisible: mapping.LeaderLine);
2294

    
2295
                            if (_LmLabelPersist != null)
2296
                            {
2297
                                specBreak.SPPID.RepresentationId = _LmLabelPersist.AsLMRepresentation().Id;
2298
                                if (_LmLabelPersist.ModelItemObject != null)
2299
                                    specBreak.SPPID.ModelItemID = _LmLabelPersist.ModelItemID;
2300
                                specBreak.SPPID.GraphicOID = _LmLabelPersist.get_GraphicOID().ToString();
2301
                                ReleaseCOMObjects(_LmLabelPersist);
2302
                            }
2303
                        }
2304
                    }
2305
                    ReleaseCOMObjects(targetLMConnector);
2306
                }
2307
            }
2308
        }
2309

    
2310
        private LMConnector FindBreakLineTarget(object targetObj, object connectedObj)
2311
        {
2312
            LMConnector targetConnector = null;
2313
            Symbol targetSymbol = targetObj as Symbol;
2314
            Symbol connectedSymbol = connectedObj as Symbol;
2315
            Line targetLine = targetObj as Line;
2316
            Line connectedLine = connectedObj as Line;
2317
            if (targetSymbol != null && connectedSymbol != null)
2318
            {
2319
                LMSymbol targetLMSymbol = dataSource.GetSymbol(targetSymbol.SPPID.RepresentationId);
2320
                LMSymbol connectedLMSymbol = dataSource.GetSymbol(connectedSymbol.SPPID.RepresentationId);
2321

    
2322
                foreach (LMConnector connector in targetLMSymbol.Avoid1Connectors)
2323
                {
2324
                    if (connector.get_ItemStatus() != "Active")
2325
                        continue;
2326

    
2327
                    if (connector.ConnectItem1SymbolObject.Id == connectedLMSymbol.Id)
2328
                    {
2329
                        targetConnector = connector;
2330
                        break;
2331
                    }
2332
                    else if (connector.ConnectItem2SymbolObject.Id == connectedLMSymbol.Id)
2333
                    {
2334
                        targetConnector = connector;
2335
                        break;
2336
                    }
2337
                }
2338

    
2339
                foreach (LMConnector connector in targetLMSymbol.Avoid2Connectors)
2340
                {
2341
                    if (connector.get_ItemStatus() != "Active")
2342
                        continue;
2343

    
2344
                    if (connector.ConnectItem1SymbolObject.Id == connectedLMSymbol.Id)
2345
                    {
2346
                        targetConnector = connector;
2347
                        break;
2348
                    }
2349
                    else if (connector.ConnectItem2SymbolObject.Id == connectedLMSymbol.Id)
2350
                    {
2351
                        targetConnector = connector;
2352
                        break;
2353
                    }
2354
                }
2355

    
2356
                ReleaseCOMObjects(targetLMSymbol);
2357
                ReleaseCOMObjects(connectedLMSymbol);
2358
            }
2359
            else if (targetLine != null && connectedLine != null)
2360
            {
2361
                LMModelItem targetModelItem = dataSource.GetModelItem(targetLine.SPPID.ModelItemId);
2362
                LMModelItem connectedModelItem = dataSource.GetModelItem(connectedLine.SPPID.ModelItemId);
2363

    
2364
                if (targetModelItem != null && connectedModelItem != null)
2365
                {
2366
                    foreach (LMRepresentation rep in targetModelItem.Representations)
2367
                    {
2368
                        if (targetConnector != null)
2369
                            break;
2370

    
2371
                        if (rep.Attributes["RepresentationType"].get_Value() == "Connector" && rep.Attributes["ItemStatus"].get_Value() == "Active")
2372
                        {
2373
                            LMConnector _LMConnector = dataSource.GetConnector(rep.Id);
2374

    
2375
                            if (IsConnected(_LMConnector, connectedModelItem))
2376
                                targetConnector = _LMConnector;
2377
                            else
2378
                                ReleaseCOMObjects(_LMConnector);
2379
                        }
2380
                    }
2381

    
2382
                    ReleaseCOMObjects(targetModelItem);
2383
                }
2384
            }
2385
            else
2386
            {
2387
                LMSymbol connectedLMSymbol = null;
2388
                if (connectedSymbol != null)
2389
                    connectedLMSymbol = dataSource.GetSymbol(connectedSymbol.SPPID.RepresentationId);
2390
                else if (targetSymbol != null)
2391
                    connectedLMSymbol = dataSource.GetSymbol(targetSymbol.SPPID.RepresentationId);
2392
                else
2393
                {
2394

    
2395
                }
2396
                LMModelItem targetModelItem = null;
2397
                if (targetLine != null)
2398
                    targetModelItem = dataSource.GetModelItem(targetLine.SPPID.ModelItemId);
2399
                else if (connectedLine != null)
2400
                    targetModelItem = dataSource.GetModelItem(connectedLine.SPPID.ModelItemId);
2401
                else
2402
                {
2403

    
2404
                }
2405
                if (connectedLMSymbol != null && targetModelItem != null)
2406
                {
2407
                    foreach (LMConnector connector in connectedLMSymbol.Avoid1Connectors)
2408
                    {
2409
                        if (connector.get_ItemStatus() != "Active")
2410
                            continue;
2411

    
2412
                        if (IsConnected(connector, targetModelItem))
2413
                        {
2414
                            targetConnector = connector;
2415
                            break;
2416
                        }
2417
                    }
2418

    
2419
                    if (targetConnector == null)
2420
                    {
2421
                        foreach (LMConnector connector in connectedLMSymbol.Avoid2Connectors)
2422
                        {
2423
                            if (connector.get_ItemStatus() != "Active")
2424
                                continue;
2425

    
2426
                            if (IsConnected(connector, targetModelItem))
2427
                            {
2428
                                targetConnector = connector;
2429
                                break;
2430
                            }
2431
                        }
2432
                    }
2433
                }
2434

    
2435
            }
2436

    
2437
            return targetConnector;
2438
        }
2439

    
2440
        private bool IsConnected(LMConnector connector, LMModelItem modelItem)
2441
        {
2442
            bool result = false;
2443

    
2444
            foreach (LMRepresentation rep in modelItem.Representations)
2445
            {
2446
                if (result)
2447
                    break;
2448

    
2449
                if (rep.Attributes["RepresentationType"].get_Value() == "Connector" && rep.Attributes["ItemStatus"].get_Value() == "Active")
2450
                {
2451
                    LMConnector _LMConnector = dataSource.GetConnector(rep.Id);
2452

    
2453
                    if (_LMConnector.ConnectItem1SymbolObject != null &&
2454
                        connector.ConnectItem1SymbolObject != null &&
2455
                        _LMConnector.ConnectItem1SymbolObject.Id == connector.ConnectItem1SymbolObject.Id)
2456
                    {
2457
                        result = true;
2458
                        ReleaseCOMObjects(_LMConnector);
2459
                        break;
2460
                    }
2461
                    else if (_LMConnector.ConnectItem1SymbolObject != null &&
2462
                        connector.ConnectItem2SymbolObject != null &&
2463
                        _LMConnector.ConnectItem1SymbolObject.Id == connector.ConnectItem2SymbolObject.Id)
2464
                    {
2465
                        result = true;
2466
                        ReleaseCOMObjects(_LMConnector);
2467
                        break;
2468
                    }
2469
                    else if (_LMConnector.ConnectItem2SymbolObject != null &&
2470
                        connector.ConnectItem1SymbolObject != null &&
2471
                        _LMConnector.ConnectItem2SymbolObject.Id == connector.ConnectItem1SymbolObject.Id)
2472
                    {
2473
                        result = true;
2474
                        ReleaseCOMObjects(_LMConnector);
2475
                        break;
2476
                    }
2477
                    else if (_LMConnector.ConnectItem2SymbolObject != null &&
2478
                        connector.ConnectItem2SymbolObject != null &&
2479
                        _LMConnector.ConnectItem2SymbolObject.Id == connector.ConnectItem2SymbolObject.Id)
2480
                    {
2481
                        result = true;
2482
                        ReleaseCOMObjects(_LMConnector);
2483
                        break;
2484
                    }
2485

    
2486
                    ReleaseCOMObjects(_LMConnector);
2487
                }
2488
            }
2489

    
2490

    
2491
            return result;
2492
        }
2493

    
2494
        /// <summary>
2495
        /// FromModelItem을 ToModelItem으로 PipeRunJoin하는 메서드
2496
        /// </summary>
2497
        /// <param name="modelItemID1"></param>
2498
        /// <param name="modelItemID2"></param>
2499
        private void JoinPipeRunForSameConnector(string modelItemID1, string modelItemID2)
2500
        {
2501
            try
2502
            {
2503
                LMModelItem modelItem1 = dataSource.GetModelItem(modelItemID1);
2504
                _LMAItem item1 = modelItem1.AsLMAItem();
2505
                LMModelItem modelItem2 = dataSource.GetModelItem(modelItemID2);
2506
                _LMAItem item2 = modelItem2.AsLMAItem();
2507

    
2508
                // item2가 item1으로 조인
2509
                _placement.PIDJoinRuns(ref item1, ref item2);
2510
                item1.Commit();
2511
                item2.Commit();
2512

    
2513
                string beforeID = string.Empty;
2514
                string afterID = string.Empty;
2515

    
2516
                if (modelItem1.get_ItemStatus() == "Active" && modelItem2.get_ItemStatus() != "Active")
2517
                {
2518
                    beforeID = modelItem2.Id;
2519
                    afterID = modelItem1.Id;
2520
                }
2521
                else if (modelItem1.get_ItemStatus() != "Active" && modelItem2.get_ItemStatus() == "Active")
2522
                {
2523
                    beforeID = modelItem1.Id;
2524
                    afterID = modelItem2.Id;
2525
                }
2526
                else
2527
                {
2528
                    Log.Write("확인 필요한 케이스");
2529
                }
2530

    
2531
                if (!string.IsNullOrEmpty(beforeID) && !string.IsNullOrEmpty(afterID))
2532
                {
2533
                    List<Line> lines = SPPIDUtil.FindLinesByModelId(document, beforeID);
2534
                    foreach (var line in lines)
2535
                        line.SPPID.ModelItemId = afterID;
2536
                }
2537

    
2538
                ReleaseCOMObjects(modelItem1);
2539
                ReleaseCOMObjects(item1);
2540
                ReleaseCOMObjects(modelItem2);
2541
                ReleaseCOMObjects(item2);
2542
            }
2543
            catch (Exception ex)
2544
            {
2545
                Log.Write("Join Error");
2546
                Log.Write(ex.Message);
2547
            }
2548
        }
2549

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

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

    
2584
                ReleaseCOMObjects(modelItem);
2585
            }
2586

    
2587
            return connectorVertices;
2588
        }
2589

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

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

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

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

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

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

    
2672
            return targetConnector;
2673
        }
2674

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

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

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

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

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

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

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

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

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

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

    
2767
                GridSetting gridSetting = GridSetting.GetInstance();
2768

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

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

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

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

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

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

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

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

    
2931
            return targetConnector;
2932
        }
2933

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

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

    
2948
                ReleaseCOMObjects(modelItem);
2949
            }
2950

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

    
2957
            return result;
2958
        }
2959

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

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

    
2979
                    if (_LmLabelPresist != null)
2980
                    {
2981
                        _LmLabelPresist.Commit();
2982
                        lineNumber.SPPID.RepresentationId = _LmLabelPresist.AsLMRepresentation().Id;
2983
                        ReleaseCOMObjects(_LmLabelPresist);
2984
                    }
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
        private void JoinRun(Line line)
3503
        {
3504
            if (!string.IsNullOrEmpty(line.SPPID.ModelItemId))
3505
            {
3506
                //LMModelItem modelItem = dataSource.GetModelItem(line.SPPID.ModelItemId);
3507
                //foreach (LMRepresentation rep in modelItem.Representations)
3508
                //{
3509
                //    if (rep.Attributes["RepresentationType"].get_Value() == "Connector" && rep.Attributes["ItemStatus"].get_Value() == "Active")
3510
                //    {
3511
                //        LMConnector connector = dataSource.GetConnector(rep.Id);
3512
                //        if (connector.ConnectItem1SymbolObject != null && connector.ConnectItem1SymbolObject.get_RepresentationType() != "Branch")
3513
                //        {
3514

    
3515
                //        }
3516
                //        if (connector.ConnectItem2SymbolObject != null && connector.ConnectItem2SymbolObject.get_RepresentationType() != "Branch")
3517
                //        {
3518

    
3519
                //        }
3520
                //    }
3521
                //}
3522

    
3523

    
3524
                ////JoinPipeRun()
3525
            }
3526
        }
3527

    
3528
        /// <summary>
3529
        /// Label의 좌표를 구하는 메서드(ID2 기준의 좌표 -> SPPID 좌표)
3530
        /// </summary>
3531
        /// <param name="x"></param>
3532
        /// <param name="y"></param>
3533
        /// <param name="originX"></param>
3534
        /// <param name="originY"></param>
3535
        /// <param name="SPPIDLabelLocation"></param>
3536
        /// <param name="location"></param>
3537
        private void CalcLabelLocation(ref double x, ref double y, double originX, double originY, SPPIDLabelLocationInfo SPPIDLabelLocation, Location location)
3538
        {
3539
            if (location == Location.None)
3540
            {
3541
                x = originX;
3542
                y = originY;
3543
            }
3544
            else
3545
            {
3546
                if (location.HasFlag(Location.Center))
3547
                {
3548
                    x = (SPPIDLabelLocation.X1 + SPPIDLabelLocation.X2) / 2;
3549
                    y = (SPPIDLabelLocation.Y1 + SPPIDLabelLocation.Y2) / 2;
3550
                }
3551

    
3552
                if (location.HasFlag(Location.Left))
3553
                    x = SPPIDLabelLocation.X1;
3554
                else if (location.HasFlag(Location.Right))
3555
                    x = SPPIDLabelLocation.X2;
3556

    
3557
                if (location.HasFlag(Location.Down))
3558
                    y = SPPIDLabelLocation.Y1;
3559
                else if (location.HasFlag(Location.Up))
3560
                    y = SPPIDLabelLocation.Y2;
3561
            }
3562
        }
3563

    
3564
        /// <summary>
3565
        /// Symbol의 우선순위 Modeling 목록을 가져온다.
3566
        /// 1. Angle Valve
3567
        /// 2. 3개로 이루어진 Symbol Group
3568
        /// </summary>
3569
        /// <returns></returns>
3570
        private List<Symbol> GetPrioritySymbol()
3571
        {
3572
            DataTable symbolTable = document.SymbolTable;
3573
            // List에 순서대로 쌓는다.
3574
            List<Symbol> symbols = new List<Symbol>();
3575

    
3576
            // Angle Valve 부터
3577
            foreach (var symbol in document.SYMBOLS.FindAll(x => x.CONNECTORS.FindAll(y => y.Index == 0).Count == 2))
3578
            {
3579
                if (!symbols.Contains(symbol))
3580
                {
3581
                    double originX = 0;
3582
                    double originY = 0;
3583

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

    
3588
                    SlopeType slopeType1 = SlopeType.None;
3589
                    SlopeType slopeType2 = SlopeType.None;
3590
                    foreach (Connector connector in symbol.CONNECTORS.FindAll(x => x.Index == 0))
3591
                    {
3592
                        double connectorX = 0;
3593
                        double connectorY = 0;
3594
                        SPPIDUtil.ConvertPointBystring(connector.CONNECTPOINT, ref connectorX, ref connectorY);
3595
                        if (slopeType1 == SlopeType.None)
3596
                            slopeType1 = SPPIDUtil.CalcSlope(originX, originY, connectorX, connectorY);
3597
                        else
3598
                            slopeType2 = SPPIDUtil.CalcSlope(originX, originY, connectorX, connectorY);
3599
                    }
3600

    
3601
                    if ((slopeType1 == SlopeType.VERTICAL && slopeType2 == SlopeType.HORIZONTAL) ||
3602
                        (slopeType2 == SlopeType.VERTICAL && slopeType1 == SlopeType.HORIZONTAL))
3603
                        symbols.Add(symbol);
3604
                }
3605
            }
3606

    
3607
            List<Symbol> tempSymbols = new List<Symbol>();
3608
            // Conn 갯수 기준
3609
            foreach (var item in document.SYMBOLS)
3610
            {
3611
                if (!symbols.Contains(item))
3612
                    tempSymbols.Add(item);
3613
            }
3614
            tempSymbols.Sort(SortSymbolPriority);
3615
            symbols.AddRange(tempSymbols);
3616

    
3617
            return symbols;
3618
        }
3619

    
3620
        private void SetPriorityLine()
3621
        {
3622
            document.LINES.Sort(SortLinePriority);
3623

    
3624
            int SortLinePriority(Line a, Line b)
3625
            {
3626
                // Branch 없는것부터
3627
                int branchRetval = CompareBranchLine(a, b);
3628
                if (branchRetval != 0)
3629
                {
3630
                    return branchRetval;
3631
                }
3632
                else
3633
                {
3634
                    // Symbol 연결 갯수
3635
                    int connSymbolRetval = CompareConnSymbol(a, b);
3636
                    if (connSymbolRetval != 0)
3637
                    {
3638
                        return connSymbolRetval;
3639
                    }
3640
                    else
3641
                    {
3642
                        // 아이템 연결 갯수(심볼, Line이면서 Not Branch)
3643
                        int connItemRetval = CompareConnItem(a, b);
3644
                        if (connItemRetval != 0)
3645
                        {
3646
                            return connItemRetval;
3647
                        }
3648
                        else
3649
                        {
3650
                            // ConnectedItem이 없는것
3651
                            int noneConnRetval = CompareNoneConn(a, b);
3652
                            if (noneConnRetval != 0)
3653
                            {
3654
                                return noneConnRetval;
3655
                            }
3656
                            else
3657
                            {
3658

    
3659
                            }
3660
                        }
3661
                    }
3662
                }
3663

    
3664
                return 0;
3665
            }
3666

    
3667
            int CompareConnSymbol(Line a, Line b)
3668
            {
3669
                List<Connector> connectorsA = a.CONNECTORS
3670
                    .Where(conn => conn.ConnectedObject != null && conn.ConnectedObject.GetType() == typeof(Symbol))
3671
                    .ToList();
3672

    
3673
                List<Connector> connectorsB = b.CONNECTORS
3674
                    .Where(conn => conn.ConnectedObject != null && conn.ConnectedObject.GetType() == typeof(Symbol))
3675
                    .ToList();
3676

    
3677
                // 오름차순
3678
                return connectorsB.Count.CompareTo(connectorsA.Count);
3679
            }
3680

    
3681
            int CompareConnItem(Line a, Line b)
3682
            {
3683
                List<Connector> connectorsA = a.CONNECTORS
3684
                    .Where(conn => conn.ConnectedObject != null && 
3685
                    (conn.ConnectedObject.GetType() == typeof(Symbol) || 
3686
                    (conn.ConnectedObject.GetType() == typeof(Line) && !SPPIDUtil.IsBranchLine((Line)conn.ConnectedObject, a))))
3687
                    .ToList();
3688

    
3689
                List<Connector> connectorsB = b.CONNECTORS
3690
                    .Where(conn => conn.ConnectedObject != null &&
3691
                    (conn.ConnectedObject.GetType() == typeof(Symbol) ||
3692
                    (conn.ConnectedObject.GetType() == typeof(Line) && !SPPIDUtil.IsBranchLine((Line)conn.ConnectedObject, b))))
3693
                    .ToList();
3694

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

    
3699
            int CompareBranchLine(Line a, Line b)
3700
            {
3701
                List<Connector> connectorsA = a.CONNECTORS
3702
                    .Where(conn => conn.ConnectedObject != null && conn.ConnectedObject.GetType() == typeof(Line) && SPPIDUtil.IsBranchLine(a, conn.ConnectedObject as Line))
3703
                    .ToList();
3704
                List<Connector> connectorsB = b.CONNECTORS
3705
                    .Where(conn => conn.ConnectedObject != null && conn.ConnectedObject.GetType() == typeof(Line) && SPPIDUtil.IsBranchLine(b, conn.ConnectedObject as Line))
3706
                    .ToList();
3707

    
3708
                // 내림차순
3709
                return connectorsA.Count.CompareTo(connectorsB.Count);
3710
            }
3711

    
3712
            int CompareNoneConn(Line a, Line b)
3713
            {
3714
                List<Connector> connectorsA = a.CONNECTORS
3715
                    .Where(conn => conn.ConnectedObject == null)
3716
                    .ToList();
3717

    
3718
                List<Connector> connectorsB = b.CONNECTORS
3719
                    .Where(conn => conn.ConnectedObject == null)
3720
                    .ToList();
3721

    
3722
                // 오름차순
3723
                return connectorsB.Count.CompareTo(connectorsA.Count);
3724
            }
3725
        }
3726

    
3727
        private void SortBranchLines()
3728
        {
3729
            NewBranchLines.Sort(SortBranchLine);
3730
            int SortBranchLine(Line a, Line b)
3731
            {
3732
                int countA = a.CONNECTORS.FindAll(x => x.ConnectedObject != null &&
3733
                 x.ConnectedObject.GetType() == typeof(Line) &&
3734
                 SPPIDUtil.IsBranchLine(x.ConnectedObject as Line, a) &&
3735
                 string.IsNullOrEmpty(((Line)x.ConnectedObject).SPPID.ModelItemId)).Count;
3736

    
3737
                int countB = b.CONNECTORS.FindAll(x => x.ConnectedObject != null &&
3738
                 x.ConnectedObject.GetType() == typeof(Line) &&
3739
                 SPPIDUtil.IsBranchLine(x.ConnectedObject as Line, b) &&
3740
                 string.IsNullOrEmpty(((Line)x.ConnectedObject).SPPID.ModelItemId)).Count;
3741

    
3742
                // 내림차순
3743
                return countA.CompareTo(countB);
3744
            }
3745
        }
3746

    
3747
        private static int SortSymbolPriority(Symbol a, Symbol b)
3748
        {
3749
            int countA = a.CONNECTORS.FindAll(x => !string.IsNullOrEmpty(x.CONNECTEDITEM) && x.CONNECTEDITEM != "None").Count;
3750
            int countB = b.CONNECTORS.FindAll(x => !string.IsNullOrEmpty(x.CONNECTEDITEM) && x.CONNECTEDITEM != "None").Count;
3751
            int retval = countB.CompareTo(countA);
3752
            if (retval != 0)
3753
                return retval;
3754
            else
3755
                return a.SPPID.ORIGINAL_X.CompareTo(b.SPPID.ORIGINAL_X);
3756
        }
3757

    
3758
        /// <summary>
3759
        /// Graphic OID로 해당 Symbol의 크기를 구하여 Zoom
3760
        /// </summary>
3761
        /// <param name="graphicOID"></param>
3762
        /// <param name="milliseconds"></param>
3763
        private void ZoomObjectByGraphicOID(string graphicOID, int milliseconds = 150)
3764
        {
3765
            if (radApp.ActiveDocument.ActiveSheet.DrawingObjects[graphicOID] != null)
3766
            {
3767
                double minX = 0;
3768
                double minY = 0;
3769
                double maxX = 0;
3770
                double maxY = 0;
3771
                radApp.ActiveDocument.ActiveSheet.DrawingObjects[graphicOID].Range(out minX, out minY, out maxX, out maxY);
3772
                radApp.ActiveWindow.ZoomArea2(minX - 0.007, minY - 0.007, maxX + 0.007, maxY + 0.007, null);
3773

    
3774
                Thread.Sleep(milliseconds);
3775
            }
3776
        }
3777

    
3778
        /// <summary>
3779
        /// ComObject를 Release
3780
        /// </summary>
3781
        /// <param name="objVars"></param>
3782
        public void ReleaseCOMObjects(params object[] objVars)
3783
        {
3784
            int intNewRefCount = 0;
3785
            foreach (object obj in objVars)
3786
            {
3787
                if (!Information.IsNothing(obj) && System.Runtime.InteropServices.Marshal.IsComObject(obj))
3788
                    intNewRefCount = intNewRefCount + System.Runtime.InteropServices.Marshal.FinalReleaseComObject(obj);
3789
            }
3790
        }
3791
    }
3792
}
클립보드 이미지 추가 (최대 크기: 500 MB)