프로젝트

일반

사용자정보

통계
| 개정판:

hytos / DTI_PID / SPPIDConverter / AutoModeling.cs @ 6924abc6

이력 | 보기 | 이력해설 | 다운로드 (177 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
                    if (EnableBranchModeling(item))
271
                    {
272
                        NewLineModeling(item, true);
273
                        SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
274
                    }
275
                    else
276
                        throw new Exception("Fail Branch Modeling");
277
                }
278
                catch (Exception ex)
279
                {
280
                    Log.Write("Error in NewLineModeling");
281
                    Log.Write("UID : " + item.UID);
282
                    Log.Write(ex.Message);
283
                    Log.Write(ex.StackTrace);
284
                    break;
285
                }
286
            }
287

    
288
            bool EnableBranchModeling(Line line)
289
            {
290
                bool result = true;
291
                if (line.CONNECTORS.FindAll(x => x.ConnectedObject != null && 
292
                x.ConnectedObject.GetType() == typeof(Line) && 
293
                string.IsNullOrEmpty(((Line)x.ConnectedObject).SPPID.ModelItemId)).Count > 0)
294
                    result = false;
295

    
296
                return result;
297
            }
298
        }
299
        private void RunClearValueInconsistancy()
300
        {
301
            int count = 1;
302
            bool loop = true;
303
            while (loop)
304
            {
305
                loop = false;
306
                LMAFilter filter = new LMAFilter();
307
                LMACriterion criterion = new LMACriterion();
308
                filter.ItemType = "Relationship";
309
                criterion.SourceAttributeName = "SP_DRAWINGID";
310
                criterion.Operator = "=";
311
                criterion.set_ValueAttribute(currentDrawing.Id);
312
                filter.get_Criteria().Add(criterion);
313

    
314
                LMRelationships relationships = new LMRelationships();
315
                relationships.Collect(dataSource, Filter: filter);
316

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

    
501
                        SetFlowDirectionByLine(line.SPPID.ModelItemId);
502

    
503
                        ReleaseCOMObjects(modelItem);
504
                    }
505
                }
506

    
507
                SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
508
            }
509
            foreach (var modelId in ZeroLengthModelItemID)
510
            {
511
                LMModelItem zeroLengthModelItem = dataSource.GetModelItem(modelId);
512
                LMAAttribute attribute = zeroLengthModelItem.Attributes["FlowDirection"];
513
                if (attribute != null && zeroLengthModelItem.get_ItemStatus() == "Active")
514
                {
515
                    attribute.set_Value("End 1 is upstream (Inlet)");
516
                    zeroLengthModelItem.Commit();
517
                }
518

    
519
                SetFlowDirectionByLine(modelId);
520

    
521
                ReleaseCOMObjects(zeroLengthModelItem);
522
                SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
523
            }
524
            foreach (var modelId in ZeroLengthModelItemIDReverse)
525
            {
526
                LMModelItem zeroLengthModelItem = dataSource.GetModelItem(modelId);
527
                LMAAttribute attribute = zeroLengthModelItem.Attributes["FlowDirection"];
528
                if (attribute != null && zeroLengthModelItem.get_ItemStatus() == "Active")
529
                {
530
                    attribute.set_Value("End 1 is downstream (Outlet)");
531
                    zeroLengthModelItem.Commit();
532
                }
533

    
534
                SetFlowDirectionByLine(modelId);
535

    
536
                ReleaseCOMObjects(zeroLengthModelItem);
537
                SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
538
            }
539
            foreach (var modelId in ZeroLengthSymbolToSymbolModelItemID)
540
            {
541
                SetFlowDirectionByLine(modelId);
542
                SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
543
            }
544

    
545
            void SetFlowDirectionByLine(string lineModelItemID)
546
            {
547
                LMModelItem modelItem = dataSource.GetModelItem(lineModelItemID);
548
                if (modelItem != null && modelItem.get_ItemStatus() == "Active")
549
                {
550
                    LMAAttribute attribute = modelItem.Attributes["FlowDirection"];
551
                    if (attribute != null && !DBNull.Value.Equals(attribute.get_Value()))
552
                    {
553
                        string sFlowDirection = attribute.get_Value().ToString();
554
                        foreach (LMRepresentation rep in modelItem.Representations)
555
                        {
556
                            if (rep.Attributes["RepresentationType"].get_Value() == "Connector" && rep.Attributes["ItemStatus"].get_Value() == "Active")
557
                            {
558
                                LMConnector connector = dataSource.GetConnector(rep.Id);
559

    
560
                                foreach (LMRelationship relationship in connector.Relation1Relationships)
561
                                    SetSymbolFlowDirectionByRelationShip(relationship, connector, sFlowDirection);
562
                                foreach (LMRelationship relationship in connector.Relation2Relationships)
563
                                    SetSymbolFlowDirectionByRelationShip(relationship, connector, sFlowDirection);
564

    
565
                                ReleaseCOMObjects(connector);
566
                            }
567
                        }
568
                    }
569
                    ReleaseCOMObjects(modelItem);
570
                }
571

    
572
                void SetSymbolFlowDirectionByRelationShip(LMRelationship relationship, LMConnector connector, string sFlowDirection)
573
                {
574
                    // Item2가 Symbol
575
                    if (!DBNull.Value.Equals(relationship.Item1RepresentationID) && relationship.Item1RepresentationID == connector.Id &&
576
                        relationship.Item2RepresentationObject != null && relationship.Item2RepresentationObject.get_RepresentationType() == "Symbol")
577
                    {
578
                        int symbolIndex = Convert.ToInt32(relationship.get_Item2Location());
579
                        int lineIndex = Convert.ToInt32(relationship.get_Item1Location());
580
                        LMModelItem symbolModelItem = relationship.Item2RepresentationObject.ModelItemObject;
581

    
582
                        SetSymbolFlowDirection(lineIndex, symbolIndex, sFlowDirection, symbolModelItem);
583

    
584
                        symbolModelItem.Commit();
585
                        ReleaseCOMObjects(symbolModelItem);
586
                    }
587
                    // Item1이 Symbol
588
                    else if (!DBNull.Value.Equals(relationship.Item2RepresentationID) && relationship.Item2RepresentationID == connector.Id &&
589
                            relationship.Item1RepresentationObject != null && relationship.Item1RepresentationObject.get_RepresentationType() == "Symbol")
590
                    {
591
                        int symbolIndex = Convert.ToInt32(relationship.get_Item1Location());
592
                        int lineIndex = Convert.ToInt32(relationship.get_Item2Location());
593
                        LMModelItem symbolModelItem = relationship.Item1RepresentationObject.ModelItemObject;
594

    
595
                        SetSymbolFlowDirection(lineIndex, symbolIndex, sFlowDirection, symbolModelItem);
596

    
597
                        symbolModelItem.Commit();
598
                        ReleaseCOMObjects(symbolModelItem);
599
                    }
600
                }
601

    
602
                void SetSymbolFlowDirection(int lineIndex, int symbolIndex, string sFlowDirection, LMModelItem symbolModelItem)
603
                {
604
                    string attrName = "PipingPoint" + symbolIndex + ".FlowDirection";
605
                    LMAAttribute attribute = symbolModelItem.Attributes[attrName];
606
                    if (attribute != null)
607
                    {
608
                        if (lineIndex == 0 && sFlowDirection == "End 1 is upstream (Inlet)")
609
                            attribute.set_Value("End 1 is downstream (Outlet)");
610
                        else if (lineIndex == 0 && sFlowDirection == "End 1 is downstream (Outlet)")
611
                            attribute.set_Value("End 1 is upstream (Inlet)");
612
                        else if (lineIndex == 1 && sFlowDirection == "End 1 is upstream (Inlet)")
613
                            attribute.set_Value("End 1 is upstream (Inlet)");
614
                        else if (lineIndex == 1 && sFlowDirection == "End 1 is downstream (Outlet)")
615
                            attribute.set_Value("End 1 is downstream (Outlet)");
616
                    }
617
                }
618
            }
619
        }
620
        private void RunNoteModeling()
621
        {
622
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetAllProgress, document.SYMBOLS.Count);
623
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Notes Modeling");
624
            foreach (var item in document.NOTES)
625
                try
626
                {
627
                    NoteModeling(item);
628
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
629
                }
630
                catch (Exception ex)
631
                {
632
                    Log.Write("Error in NoteModeling");
633
                    Log.Write("UID : " + item.UID);
634
                    Log.Write(ex.Message);
635
                    Log.Write(ex.StackTrace);
636
                }
637
        }
638
        private void RunTextModeling()
639
        {
640
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetAllProgress, document.TEXTINFOS.Count);
641
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Texts Modeling");
642
            foreach (var item in document.TEXTINFOS)
643
                try
644
                {
645
                    TextModeling(item);
646
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
647
                }
648
                catch (Exception ex)
649
                {
650
                    Log.Write("Error in TextModeling");
651
                    Log.Write("UID : " + item.UID);
652
                    Log.Write(ex.Message);
653
                    Log.Write(ex.StackTrace);
654
                }
655
        }
656
        private void RunInputLineNumberAttribute()
657
        {
658
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetAllProgress, document.LINENUMBERS.Count);
659
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Set LineNumbers Attribute");
660
            foreach (var item in document.LINENUMBERS)
661
                try
662
                {
663
                    InputLineNumberAttribute(item);
664
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
665
                }
666
                catch (Exception ex)
667
                {
668
                    Log.Write("Error in InputLineNumberAttribute");
669
                    Log.Write("UID : " + item.UID);
670
                    Log.Write(ex.Message);
671
                    Log.Write(ex.StackTrace);
672
                }
673
        }
674
        private void RunInputSymbolAttribute()
675
        {
676
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetAllProgress, document.SYMBOLS.Count);
677
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Set Symbols Attribute");
678
            foreach (var item in document.SYMBOLS)
679
                try
680
                {
681
                    InputSymbolAttribute(item, item.ATTRIBUTES);
682
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
683
                }
684
                catch (Exception ex)
685
                {
686
                    Log.Write("Error in InputSymbolAttribute");
687
                    Log.Write("UID : " + item.UID);
688
                    Log.Write(ex.Message);
689
                    Log.Write(ex.StackTrace);
690
                }
691
        }
692
        private void RunInputSpecBreakAttribute()
693
        {
694
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetAllProgress, document.SYMBOLS.Count);
695
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Set Symbols Attribute");
696
            foreach (var item in document.SpecBreaks)
697
                try
698
                {
699
                    InputSpecBreakAttribute(item);
700
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
701
                }
702
                catch (Exception ex)
703
                {
704
                    Log.Write("Error in InputSpecBreakAttribute");
705
                    Log.Write("UID : " + item.UID);
706
                    Log.Write(ex.Message);
707
                    Log.Write(ex.StackTrace);
708
                }
709
        }
710
        private void RunLabelSymbolModeling()
711
        {
712
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetAllProgress, document.SYMBOLS.Count);
713
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Labels Modeling");
714
            foreach (var item in document.SYMBOLS)
715
                try
716
                {
717
                    LabelSymbolModeling(item);
718
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
719
                }
720
                catch (Exception ex)
721
                {
722
                    Log.Write("Error in LabelSymbolModeling");
723
                    Log.Write("UID : " + item.UID);
724
                    Log.Write(ex.Message);
725
                    Log.Write(ex.StackTrace);
726
                }
727
        }
728
        
729
        /// <summary>
730
        /// 도면 생성 메서드
731
        /// </summary>
732
        private void CreateDocument(ref string drawingNumber, ref string drawingName)
733
        {
734
            Log.Write("------------------ Start create document ------------------");
735
            GetDrawingNameAndNumber(ref drawingName, ref drawingNumber);
736
            Log.Write("Drawing name : " + drawingName);
737
            Log.Write("Drawing number : " + drawingNumber);
738
            newDrawing = application.Drawings.Add(document.Unit, document.Template, drawingNumber, drawingName);
739
            document.SPPID_DrawingNumber = drawingNumber;
740
            document.SPPID_DrawingName = drawingName;
741
            application.ActiveWindow.Fit();
742
            Thread.Sleep(1000);
743
            application.ActiveWindow.Zoom = 2000;
744
            Thread.Sleep(2000);
745

    
746
            //current LMDrawing 가져오기
747
            LMAFilter filter = new LMAFilter();
748
            LMACriterion criterion = new LMACriterion();
749
            filter.ItemType = "Drawing";
750
            criterion.SourceAttributeName = "Name";
751
            criterion.Operator = "=";
752
            criterion.set_ValueAttribute(drawingName);
753
            filter.get_Criteria().Add(criterion);
754

    
755
            LMDrawings drawings = new LMDrawings();
756
            drawings.Collect(dataSource, Filter: filter);
757

    
758
            currentDrawing = ((dynamic)drawings).Nth(1);
759
        }
760

    
761
        /// <summary>
762
        /// DrawingName, DrawingNumber를 확인하여 중복이 있으면 _1을 붙이고 +1씩 한다.
763
        /// </summary>
764
        /// <param name="drawingName"></param>
765
        /// <param name="drawingNumber"></param>
766
        private void GetDrawingNameAndNumber(ref string drawingName, ref string drawingNumber)
767
        {
768
            LMDrawings drawings = new LMDrawings();
769
            drawings.Collect(dataSource);
770

    
771
            List<string> drawingNameList = new List<string>();
772
            List<string> drawingNumberList = new List<string>();
773

    
774
            foreach (LMDrawing item in drawings)
775
            {
776
                drawingNameList.Add(item.Attributes["Name"].get_Value().ToString());
777
                drawingNumberList.Add(item.Attributes["DrawingNumber"].get_Value().ToString());
778
            }
779

    
780
            int nameLength = drawingName.Length;
781
            while (drawingNameList.Contains(drawingName))
782
            {
783
                if (nameLength == drawingName.Length)
784
                    drawingName += "-1";
785
                else
786
                {
787
                    int index = Convert.ToInt32(drawingName.Remove(0, nameLength + 1));
788
                    drawingName = drawingName.Substring(0, nameLength + 1);
789
                    drawingName += ++index;
790
                }
791
            }
792

    
793
            int numberLength = drawingNumber.Length;
794
            while (drawingNameList.Contains(drawingNumber))
795
            {
796
                if (numberLength == drawingNumber.Length)
797
                    drawingNumber += "-1";
798
                else
799
                {
800
                    int index = Convert.ToInt32(drawingNumber.Remove(0, numberLength + 1));
801
                    drawingNumber = drawingNumber.Substring(0, numberLength + 1);
802
                    drawingNumber += ++index;
803
                }
804
            }
805

    
806
            ReleaseCOMObjects(drawings);
807
        }
808

    
809
        /// <summary>
810
        /// 도면 크기 구하는 메서드
811
        /// </summary>
812
        /// <returns></returns>
813
        private bool DocumentCoordinateCorrection()
814
        {
815
            if (Settings.Default.DrawingX != 0 && Settings.Default.DrawingY != 0)
816
            {
817
                Log.Write("Setting Drawing X, Drawing Y");
818
                document.SetSPPIDLocation(Settings.Default.DrawingX, Settings.Default.DrawingY);
819
                Log.Write("Start coordinate correction");
820
                document.CoordinateCorrection();
821
                return true;
822
            }
823
            else
824
            {
825
                Log.Write("Need Drawing X, Y");
826
                return false;
827
            }
828
        }
829

    
830
        /// <summary>
831
        /// 심볼을 실제로 Modeling 메서드
832
        /// </summary>
833
        /// <param name="symbol"></param>
834
        /// <param name="targetSymbol"></param>
835
        /// <param name="prevSymbol"></param>
836
        private void SymbolModeling(Symbol symbol, Symbol targetSymbol)
837
        {
838
            // OWNERSYMBOL Attribute, 값을 가지고 있을 경우
839
            BaseModel.Attribute itemAttribute = symbol.ATTRIBUTES.Find(attr => attr.ATTRIBUTE == "OWNERSYMBOL");
840
            if (itemAttribute != null && (string.IsNullOrEmpty(itemAttribute.VALUE) || itemAttribute.VALUE != "None"))
841
                return;
842
            // 이미 모델링 됐을 경우
843
            else if (!string.IsNullOrEmpty(symbol.SPPID.RepresentationId))
844
                return;
845

    
846
            LMSymbol _LMSymbol = null;
847

    
848
            string mappingPath = symbol.SPPID.MAPPINGNAME;
849
            double x = symbol.SPPID.ORIGINAL_X;
850
            double y = symbol.SPPID.ORIGINAL_Y;
851
            int mirror = 0;
852
            double angle = symbol.ANGLE;
853

    
854
            // OPC 일경우 180도 일때 Mirror
855
            if (mappingPath.Contains("Piping OPC's") && angle == Math.PI)
856
                mirror = 1;
857

    
858
            // Mirror 계산
859
            if (symbol.FLIP == 1)
860
            {
861
                mirror = 1;
862
                angle += Math.PI;
863
            }
864

    
865
            if (targetSymbol != null && !string.IsNullOrEmpty(targetSymbol.SPPID.RepresentationId))
866
            {
867
                LMSymbol _TargetItem = dataSource.GetSymbol(targetSymbol.SPPID.RepresentationId);
868
                Connector connector = SPPIDUtil.FindSymbolConnectorByUID(document, symbol.UID, targetSymbol);
869
                if (connector != null)
870
                    GetTargetSymbolConnectorPoint(connector, targetSymbol, ref x, ref y);
871

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

    
874
                if (_LMSymbol != null && _TargetItem != null)
875
                {
876
                    symbol.SPPID.RepresentationId = _LMSymbol.AsLMRepresentation().Id;
877

    
878
                    if (SPPIDUtil.IsSegmentLine(document, symbol, targetSymbol))
879
                    {
880
                        LMConnector reModelingConnector = FindBreakLineTarget(symbol, targetSymbol);
881
                        if (reModelingConnector != null)
882
                            ReModelingLMConnector(reModelingConnector);
883
                    }
884
                }
885

    
886
                ReleaseCOMObjects(_TargetItem);
887
            }
888
            else
889
                _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle);
890

    
891
            if (_LMSymbol != null)
892
            {
893
                _LMSymbol.Commit();
894
                symbol.SPPID.RepresentationId = _LMSymbol.AsLMRepresentation().Id;
895
                symbol.SPPID.ModelItemID = _LMSymbol.ModelItemID;
896
                symbol.SPPID.GraphicOID = _LMSymbol.get_GraphicOID().ToString();
897

    
898
                foreach (var item in symbol.ChildSymbols)
899
                    CreateChildSymbol(item, _LMSymbol, symbol);
900

    
901
                symbol.SPPID.SPPID_X = _LMSymbol.get_XCoordinate();
902
                symbol.SPPID.SPPID_Y = _LMSymbol.get_YCoordinate();
903

    
904
                double[] range = null;
905
                GetSPPIDSymbolRange(symbol, ref range);
906
                symbol.SPPID.SPPID_Min_X = range[0];
907
                symbol.SPPID.SPPID_Min_Y = range[1];
908
                symbol.SPPID.SPPID_Max_X = range[2];
909
                symbol.SPPID.SPPID_Max_Y = range[3];
910

    
911
                foreach (var item in symbol.SPPID.CorrectionX_GroupSymbols)
912
                    item.SPPID.ORIGINAL_X = symbol.SPPID.SPPID_X;
913
                foreach (var item in symbol.SPPID.CorrectionY_GroupSymbols)
914
                    item.SPPID.ORIGINAL_Y = symbol.SPPID.SPPID_Y;
915

    
916
                ReleaseCOMObjects(_LMSymbol);
917
            }
918
        }
919

    
920
        private void RemoveSymbol(Symbol symbol)
921
        {
922
            if (!string.IsNullOrEmpty(symbol.SPPID.RepresentationId))
923
            {
924
                LMSymbol _LMSymbol = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
925
                if (_LMSymbol != null)
926
                {
927
                    _placement.PIDRemovePlacement(_LMSymbol.AsLMRepresentation());
928
                    ReleaseCOMObjects(_LMSymbol);
929
                }
930
            }
931

    
932
            symbol.SPPID.RepresentationId = string.Empty;
933
            symbol.SPPID.ModelItemID = string.Empty;
934
            symbol.SPPID.SPPID_X = double.NaN;
935
            symbol.SPPID.SPPID_Y = double.NaN;
936
            symbol.SPPID.SPPID_Min_X = double.NaN;
937
            symbol.SPPID.SPPID_Min_Y = double.NaN;
938
            symbol.SPPID.SPPID_Max_X = double.NaN;
939
            symbol.SPPID.SPPID_Max_Y = double.NaN;
940
        }
941

    
942
        private void RemoveSymbol(List<Symbol> symbols)
943
        {
944
            foreach (var symbol in symbols)
945
            {
946
                if (!string.IsNullOrEmpty(symbol.SPPID.RepresentationId))
947
                {
948
                    LMSymbol _LMSymbol = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
949
                    if (_LMSymbol != null)
950
                    {
951
                        _placement.PIDRemovePlacement(_LMSymbol.AsLMRepresentation());
952
                        ReleaseCOMObjects(_LMSymbol);
953
                    }
954
                }
955

    
956
                symbol.SPPID.RepresentationId = string.Empty;
957
                symbol.SPPID.ModelItemID = string.Empty;
958
                symbol.SPPID.SPPID_X = double.NaN;
959
                symbol.SPPID.SPPID_Y = double.NaN;
960
                symbol.SPPID.SPPID_Min_X = double.NaN;
961
                symbol.SPPID.SPPID_Min_Y = double.NaN;
962
                symbol.SPPID.SPPID_Max_X = double.NaN;
963
                symbol.SPPID.SPPID_Max_Y = double.NaN;
964
            }
965
        }
966

    
967
        /// <summary>
968
        /// ID2의 Symbol Width와 Height를 비교해서 상대적인 SPPID Connector좌표를 가져온다.
969
        /// </summary>
970
        /// <param name="targetConnector"></param>
971
        /// <param name="targetSymbol"></param>
972
        /// <param name="x"></param>
973
        /// <param name="y"></param>
974
        private void GetTargetSymbolConnectorPoint(Connector targetConnector, Symbol targetSymbol, ref double x, ref double y)
975
        {
976
            LMSymbol _TargetItem = dataSource.GetSymbol(targetSymbol.SPPID.RepresentationId);
977

    
978
            double[] range = null;
979
            List<double[]> points = new List<double[]>();
980
            GetSPPIDSymbolRangeAndConnectionPoints(targetSymbol, ref range, points);
981
            double x1 = range[0];
982
            double y1 = range[1];
983
            double x2 = range[2];
984
            double y2 = range[3];
985

    
986
            // Origin 기준 Connector의 위치차이
987
            double sceneX = 0;
988
            double sceneY = 0;
989
            SPPIDUtil.ConvertPointBystring(targetConnector.SCENECONNECTPOINT, ref sceneX, ref sceneY);
990
            double originX = 0;
991
            double originY = 0;
992
            SPPIDUtil.ConvertPointBystring(targetSymbol.ORIGINALPOINT, ref originX, ref originY);
993
            double gapX = originX - sceneX;
994
            double gapY = originY - sceneY;
995

    
996
            // SPPID Symbol과 ID2 심볼의 크기 차이
997
            double sizeWidth = 0;
998
            double sizeHeight = 0;
999
            SPPIDUtil.ConvertPointBystring(targetSymbol.SIZE, ref sizeWidth, ref sizeHeight);
1000
            if (sizeWidth == 0 || sizeHeight == 0)
1001
                throw new Exception("Check symbol size! \r\nUID : " + targetSymbol.UID);
1002

    
1003
            double percentX = (x2 - x1) / sizeWidth;
1004
            double percentY = (y2 - y1) / sizeHeight;
1005

    
1006
            double SPPIDgapX = gapX * percentX;
1007
            double SPPIDgapY = gapY * percentY;
1008

    
1009
            double[] SPPIDOriginPoint = new double[] { _TargetItem.get_XCoordinate() - SPPIDgapX, _TargetItem.get_YCoordinate() + SPPIDgapY };
1010
            double distance = double.MaxValue;
1011
            double[] resultPoint;
1012
            foreach (var point in points)
1013
            {
1014
                double result = SPPIDUtil.CalcPointToPointdDistance(point[0], point[1], SPPIDOriginPoint[0], SPPIDOriginPoint[1]);
1015
                if (distance > result)
1016
                {
1017
                    distance = result;
1018
                    resultPoint = point;
1019
                    x = point[0];
1020
                    y = point[1];
1021
                }
1022
            }
1023

    
1024
            ReleaseCOMObjects(_TargetItem);
1025
        }
1026

    
1027
        private void GetTargetLineConnectorPoint(Connector targetConnector, Line targetLine, ref double x, ref double y)
1028
        {
1029
            int index = targetLine.CONNECTORS.IndexOf(targetConnector);
1030
            if (index == 0)
1031
            {
1032
                x = targetLine.SPPID.START_X;
1033
                y = targetLine.SPPID.START_Y;
1034
            }
1035
            else
1036
            {
1037
                x = targetLine.SPPID.END_X;
1038
                y = targetLine.SPPID.END_Y;
1039
            }
1040
        }
1041

    
1042
        /// <summary>
1043
        /// SPPID Symbol의 Range를 구한다.
1044
        /// </summary>
1045
        /// <param name="symbol"></param>
1046
        /// <param name="range"></param>
1047
        private void GetSPPIDSymbolRangeAndConnectionPoints(Symbol symbol, ref double[] range, List<double[]> points)
1048
        {
1049
            LMSymbol _TargetItem = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
1050
            Ingr.RAD2D.Symbol2d symbol2d = radApp.ActiveDocument.ActiveSheet.DrawingObjects[_TargetItem.get_GraphicOID().ToString()];
1051
            double x1 = 0;
1052
            double y1 = 0;
1053
            double x2 = 0;
1054
            double y2 = 0;
1055
            symbol2d.Range(out x1, out y1, out x2, out y2);
1056
            range = new double[] { x1, y1, x2, y2 };
1057

    
1058
            for (int i = 1; i < int.MaxValue; i++)
1059
            {
1060
                double connX = 0;
1061
                double connY = 0;
1062
                if (_placement.PIDConnectPointLocation(_TargetItem, i, ref connX, ref connY))
1063
                    points.Add(new double[] { connX, connY });
1064
                else
1065
                    break;
1066
            }
1067

    
1068
            foreach (var childSymbol in symbol.ChildSymbols)
1069
                GetSPPIDChildSymbolRange(childSymbol, ref range, points);
1070

    
1071
            ReleaseCOMObjects(_TargetItem);
1072
        }
1073

    
1074
        private void GetSPPIDSymbolRange(Symbol symbol, ref double[] range)
1075
        {
1076
            LMSymbol _TargetItem = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
1077
            if (_TargetItem != null)
1078
            {
1079
                Ingr.RAD2D.Symbol2d symbol2d = radApp.ActiveDocument.ActiveSheet.DrawingObjects[_TargetItem.get_GraphicOID().ToString()];
1080
                double x1 = 0;
1081
                double y1 = 0;
1082
                double x2 = 0;
1083
                double y2 = 0;
1084
                symbol2d.Range(out x1, out y1, out x2, out y2);
1085
                range = new double[] { x1, y1, x2, y2 };
1086

    
1087
                foreach (var childSymbol in symbol.ChildSymbols)
1088
                    GetSPPIDChildSymbolRange(childSymbol, ref range);
1089

    
1090
                ReleaseCOMObjects(_TargetItem);
1091
            }
1092
        }
1093

    
1094
        private void GetSPPIDSymbolRange(List<Symbol> symbols, ref double[] range)
1095
        {
1096
            double[] tempRange = new double[] { double.MaxValue, double.MaxValue, double.MinValue, double.MinValue };
1097
            foreach (var symbol in symbols)
1098
            {
1099
                LMSymbol _TargetItem = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
1100
                Ingr.RAD2D.Symbol2d symbol2d = radApp.ActiveDocument.ActiveSheet.DrawingObjects[_TargetItem.get_GraphicOID().ToString()];
1101
                double x1 = 0;
1102
                double y1 = 0;
1103
                double x2 = 0;
1104
                double y2 = 0;
1105
                symbol2d.Range(out x1, out y1, out x2, out y2);
1106

    
1107
                tempRange[0] = Math.Min(tempRange[0], x1);
1108
                tempRange[1] = Math.Min(tempRange[1], y1);
1109
                tempRange[2] = Math.Max(tempRange[2], x2);
1110
                tempRange[3] = Math.Max(tempRange[3], y2);
1111

    
1112
                foreach (var childSymbol in symbol.ChildSymbols)
1113
                    GetSPPIDChildSymbolRange(childSymbol, ref tempRange);
1114

    
1115
                ReleaseCOMObjects(_TargetItem);
1116
            }
1117

    
1118
            range = tempRange;
1119
        }
1120

    
1121
        /// <summary>
1122
        /// Child Modeling 된 Symbol의 Range를 구한다.
1123
        /// </summary>
1124
        /// <param name="childSymbol"></param>
1125
        /// <param name="range"></param>
1126
        private void GetSPPIDChildSymbolRange(ChildSymbol childSymbol, ref double[] range, List<double[]> points)
1127
        {
1128
            LMSymbol _ChildSymbol = dataSource.GetSymbol(childSymbol.SPPID.RepresentationId);
1129
            if (_ChildSymbol != null)
1130
            {
1131
                Ingr.RAD2D.Symbol2d symbol2d = radApp.ActiveDocument.ActiveSheet.DrawingObjects[_ChildSymbol.get_GraphicOID().ToString()];
1132
                double x1 = 0;
1133
                double y1 = 0;
1134
                double x2 = 0;
1135
                double y2 = 0;
1136
                symbol2d.Range(out x1, out y1, out x2, out y2);
1137
                range[0] = Math.Min(range[0], x1);
1138
                range[1] = Math.Min(range[1], y1);
1139
                range[2] = Math.Max(range[2], x2);
1140
                range[3] = Math.Max(range[3], y2);
1141

    
1142
                for (int i = 1; i < int.MaxValue; i++)
1143
                {
1144
                    double connX = 0;
1145
                    double connY = 0;
1146
                    if (_placement.PIDConnectPointLocation(_ChildSymbol, i, ref connX, ref connY))
1147
                        points.Add(new double[] { connX, connY });
1148
                    else
1149
                        break;
1150
                }
1151

    
1152
                foreach (var loopChildSymbol in childSymbol.ChildSymbols)
1153
                    GetSPPIDChildSymbolRange(loopChildSymbol, ref range, points);
1154

    
1155
                ReleaseCOMObjects(_ChildSymbol);
1156
            }
1157
        }
1158

    
1159
        private void GetSPPIDChildSymbolRange(ChildSymbol childSymbol, ref double[] range)
1160
        {
1161
            LMSymbol _ChildSymbol = dataSource.GetSymbol(childSymbol.SPPID.RepresentationId);
1162
            if (_ChildSymbol != null)
1163
            {
1164
                Ingr.RAD2D.Symbol2d symbol2d = radApp.ActiveDocument.ActiveSheet.DrawingObjects[_ChildSymbol.get_GraphicOID().ToString()];
1165
                double x1 = 0;
1166
                double y1 = 0;
1167
                double x2 = 0;
1168
                double y2 = 0;
1169
                symbol2d.Range(out x1, out y1, out x2, out y2);
1170
                range[0] = Math.Min(range[0], x1);
1171
                range[1] = Math.Min(range[1], y1);
1172
                range[2] = Math.Max(range[2], x2);
1173
                range[3] = Math.Max(range[3], y2);
1174

    
1175
                foreach (var loopChildSymbol in childSymbol.ChildSymbols)
1176
                    GetSPPIDChildSymbolRange(loopChildSymbol, ref range);
1177
                ReleaseCOMObjects(_ChildSymbol);
1178
            }
1179
        }
1180

    
1181
        /// <summary>
1182
        /// Label Symbol Modeling
1183
        /// </summary>
1184
        /// <param name="symbol"></param>
1185
        private void LabelSymbolModeling(Symbol symbol)
1186
        {
1187
            if (string.IsNullOrEmpty(symbol.SPPID.RepresentationId))
1188
            {
1189
                BaseModel.Attribute itemAttribute = symbol.ATTRIBUTES.Find(x => x.ATTRIBUTE == "OWNERSYMBOL");
1190
                if (itemAttribute == null || string.IsNullOrEmpty(itemAttribute.VALUE) || itemAttribute.VALUE == "None")
1191
                    return;
1192
                Array points = new double[] { 0, symbol.SPPID.ORIGINAL_X, symbol.SPPID.ORIGINAL_Y };
1193

    
1194
                string symbolUID = itemAttribute.VALUE;
1195
                object targetItem = SPPIDUtil.FindObjectByUID(document, symbolUID);
1196
                if (targetItem != null &&
1197
                    (targetItem.GetType() == typeof(Symbol) ||
1198
                    targetItem.GetType() == typeof(Equipment)))
1199
                {
1200
                    // Object 아이템이 Symbol일 경우 Equipment일 경우 
1201
                    string sRep = null;
1202
                    if (targetItem.GetType() == typeof(Symbol))
1203
                        sRep = ((Symbol)targetItem).SPPID.RepresentationId;
1204
                    else if (targetItem.GetType() == typeof(Equipment))
1205
                        sRep = ((Equipment)targetItem).SPPID.RepresentationId;
1206
                    if (!string.IsNullOrEmpty(sRep))
1207
                    {
1208
                        // LEADER Line 검사
1209
                        bool leaderLine = false;
1210
                        SymbolMapping symbolMapping = document.SymbolMappings.Find(x => x.UID == symbol.DBUID);
1211
                        if (symbolMapping != null)
1212
                            leaderLine = symbolMapping.LEADERLINE;
1213

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

    
1218
                        //Leader 선 센터로
1219
                        if (_LMLabelPresist != null)
1220
                        {
1221
                            // Target Item에 Label의 Attribute Input
1222
                            InputSymbolAttribute(targetItem, symbol.ATTRIBUTES);
1223

    
1224
                            string OID = _LMLabelPresist.get_GraphicOID().ToString();
1225
                            DependencyObject dependency = radApp.ActiveDocument.ActiveSheet.DrawingObjects[OID] as DependencyObject;
1226
                            if (dependency != null)
1227
                            {
1228
                                bool result = false;
1229
                                foreach (var attributes in dependency.AttributeSets)
1230
                                {
1231
                                    foreach (var attribute in attributes)
1232
                                    {
1233
                                        string name = attribute.Name;
1234
                                        string value = attribute.GetValue().ToString();
1235
                                        if (name == "DrawingItemType" && value == "LabelPersist")
1236
                                        {
1237
                                            foreach (DrawingObjectBase drawingObject in dependency.DrawingObjects)
1238
                                            {
1239
                                                if (drawingObject.Type == Ingr.RAD2D.ObjectType.igLineString2d)
1240
                                                {
1241
                                                    Ingr.RAD2D.LineString2d lineString2D = drawingObject as Ingr.RAD2D.LineString2d;
1242
                                                    double prevX = _TargetItem.get_XCoordinate();
1243
                                                    double prevY = _TargetItem.get_YCoordinate();
1244
                                                    lineString2D.InsertVertex(lineString2D.VertexCount, prevX, prevY);
1245
                                                    lineString2D.RemoveVertex(lineString2D.VertexCount);
1246
                                                    result = true;
1247
                                                    break;
1248
                                                }
1249
                                            }
1250
                                        }
1251

    
1252
                                        if (result)
1253
                                            break;
1254
                                    }
1255

    
1256
                                    if (result)
1257
                                        break;
1258
                                }
1259
                            }
1260

    
1261
                            symbol.SPPID.RepresentationId = _LMLabelPresist.AsLMRepresentation().Id;
1262
                            _LMLabelPresist.Commit();
1263
                            ReleaseCOMObjects(_LMLabelPresist);
1264
                        }
1265

    
1266
                        ReleaseCOMObjects(_TargetItem);
1267
                    }
1268
                }
1269
                else if (targetItem != null && targetItem.GetType() == typeof(Line))
1270
                {
1271
                    Line targetLine = targetItem as Line;
1272
                    Dictionary<LMConnector, List<double[]>> connectorVertices = GetPipeRunVertices(targetLine.SPPID.ModelItemId);
1273
                    LMConnector connectedLMConnector = FindTargetLMConnectorForLabel(connectorVertices, symbol.SPPID.ORIGINAL_X, symbol.SPPID.ORIGINAL_Y);
1274
                    if (connectedLMConnector != null)
1275
                    {
1276
                        // LEADER Line 검사
1277
                        bool leaderLine = false;
1278
                        SymbolMapping symbolMapping = document.SymbolMappings.Find(x => x.UID == symbol.DBUID);
1279
                        if (symbolMapping != null)
1280
                            leaderLine = symbolMapping.LEADERLINE;
1281

    
1282
                        LMLabelPersist _LMLabelPresist = _placement.PIDPlaceLabel(symbol.SPPID.MAPPINGNAME, ref points, Rotation: 0, LabeledItem: connectedLMConnector.AsLMRepresentation(), IsLeaderVisible: leaderLine);
1283
                        if (_LMLabelPresist != null)
1284
                        {
1285
                            _LMLabelPresist.Commit();
1286
                            ReleaseCOMObjects(_LMLabelPresist);
1287
                        }
1288
                        ReleaseCOMObjects(connectedLMConnector);
1289
                    }
1290

    
1291
                    foreach (var item in connectorVertices)
1292
                        if (item.Key != null)
1293
                            ReleaseCOMObjects(item.Key);
1294
                }
1295
            }
1296
        }
1297

    
1298
        /// <summary>
1299
        /// Equipment를 실제로 Modeling 메서드
1300
        /// </summary>
1301
        /// <param name="equipment"></param>
1302
        private void EquipmentModeling(Equipment equipment)
1303
        {
1304
            if (!string.IsNullOrEmpty(equipment.SPPID.RepresentationId))
1305
                return;
1306

    
1307
            LMSymbol _LMSymbol = null;
1308
            LMSymbol targetItem = null;
1309
            string mappingPath = equipment.SPPID.MAPPINGNAME;
1310
            double x = equipment.SPPID.ORIGINAL_X;
1311
            double y = equipment.SPPID.ORIGINAL_Y;
1312
            int mirror = 0;
1313
            double angle = equipment.ANGLE;
1314

    
1315
            SPPIDUtil.ConvertGridPoint(ref x, ref y);
1316

    
1317
            Connector connector = equipment.CONNECTORS.Find(conn => !string.IsNullOrEmpty(conn.CONNECTEDITEM) && conn.CONNECTEDITEM != "None");
1318
            if (connector != null)
1319
            {
1320
                Equipment connEquipment = SPPIDUtil.FindObjectByUID(document, connector.CONNECTEDITEM) as Equipment;
1321
                if (connEquipment != null)
1322
                {
1323
                    if (string.IsNullOrEmpty(connEquipment.SPPID.RepresentationId))
1324
                        EquipmentModeling(connEquipment);
1325

    
1326
                    if (!string.IsNullOrEmpty(connEquipment.SPPID.RepresentationId))
1327
                    {
1328
                        targetItem = dataSource.GetSymbol(connEquipment.SPPID.RepresentationId);
1329
                        if (targetItem != null)
1330
                        {
1331
                            _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle, TargetItem: targetItem);
1332
                        }
1333
                        else
1334
                        {
1335
                            _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle);
1336
                        }
1337
                    }
1338
                    else
1339
                    {
1340
                        _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle);
1341
                    }
1342
                }
1343
                else
1344
                {
1345
                    _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle);
1346
                }
1347
            }
1348
            else
1349
            {
1350
                _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle);
1351
            }
1352

    
1353
            if (_LMSymbol != null)
1354
            {
1355
                _LMSymbol.Commit();
1356
                equipment.SPPID.RepresentationId = _LMSymbol.AsLMRepresentation().Id;
1357
                equipment.SPPID.GraphicOID = _LMSymbol.get_GraphicOID().ToString();
1358
                ReleaseCOMObjects(_LMSymbol);
1359
            }
1360

    
1361
            if (targetItem != null)
1362
            {
1363
                ReleaseCOMObjects(targetItem);
1364
            }
1365

    
1366
            ReleaseCOMObjects(_LMSymbol);
1367
        }
1368

    
1369
        /// <summary>
1370
        /// 첫 진입점
1371
        /// </summary>
1372
        /// <param name="symbol"></param>
1373
        private void SymbolModelingBySymbol(Symbol symbol)
1374
        {
1375
            SymbolModeling(symbol, null);
1376
            List<object> endObjects = new List<object>();
1377
            endObjects.Add(symbol);
1378

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

    
1404
        private void SymbolModelingByNeerSymbolLoop(Symbol symbol, List<object> endObjects)
1405
        {
1406
            foreach (var connector in symbol.CONNECTORS)
1407
            {
1408
                object connItem = SPPIDUtil.FindObjectByUID(document, connector.CONNECTEDITEM);
1409
                if (connItem != null && connItem.GetType() != typeof(Equipment))
1410
                {
1411
                    if (!endObjects.Contains(connItem))
1412
                    {
1413
                        endObjects.Add(connItem);
1414
                        if (connItem.GetType() == typeof(Symbol))
1415
                        {
1416
                            Symbol connSymbol = connItem as Symbol;
1417
                            if (string.IsNullOrEmpty(connSymbol.SPPID.RepresentationId))
1418
                            {
1419
                                SymbolModeling(connSymbol, symbol);
1420
                            }
1421
                            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetProgress, document.SYMBOLS.FindAll(x => !string.IsNullOrEmpty(x.SPPID.RepresentationId)).Count);
1422
                            SymbolModelingByNeerSymbolLoop(connSymbol, endObjects);
1423
                        }
1424
                        else if (connItem.GetType() == typeof(Line))
1425
                        {
1426
                            Line connLine = connItem as Line;
1427
                            SymbolModelingByNeerLineLoop(connLine, endObjects, symbol);
1428
                        }
1429
                    }
1430
                }
1431
            }
1432
        }
1433

    
1434
        private void SymbolModelingByNeerLineLoop(Line line, List<object> endObjects, Symbol prevSymbol)
1435
        {
1436
            foreach (var connector in line.CONNECTORS)
1437
            {
1438
                object connItem = SPPIDUtil.FindObjectByUID(document, connector.CONNECTEDITEM);
1439
                if (connItem != null && connItem.GetType() != typeof(Equipment))
1440
                {
1441
                    if (!endObjects.Contains(connItem))
1442
                    {
1443
                        endObjects.Add(connItem);
1444
                        if (connItem.GetType() == typeof(Symbol))
1445
                        {
1446
                            Symbol connSymbol = connItem as Symbol;
1447
                            if (string.IsNullOrEmpty(connSymbol.SPPID.RepresentationId))
1448
                            {
1449
                                List<Symbol> group = new List<Symbol>();
1450
                                SPPIDUtil.FindConnectedSymbolGroup(document, connSymbol, group);
1451
                                Symbol priority = prioritySymbols.Find(x => group.Contains(x));
1452
                                List<Symbol> endModelingGroup = new List<Symbol>();
1453
                                if (priority != null)
1454
                                {
1455
                                    SymbolGroupModeling(priority, group);
1456

    
1457
                                    // Range 겹치는지 확인해야함
1458
                                    double[] prevRange = null;
1459
                                    GetSPPIDSymbolRange(prevSymbol, ref prevRange);
1460
                                    double[] groupRange = null;
1461
                                    GetSPPIDSymbolRange(group, ref groupRange);
1462

    
1463
                                    double distanceX = 0;
1464
                                    double distanceY = 0;
1465
                                    bool overlapX = false;
1466
                                    bool overlapY = false;
1467
                                    SlopeType slopeType = SPPIDUtil.CalcSlope(prevSymbol.SPPID.ORIGINAL_X, prevSymbol.SPPID.ORIGINAL_Y, connSymbol.SPPID.ORIGINAL_X, connSymbol.SPPID.ORIGINAL_Y);
1468
                                    SPPIDUtil.CalcOverlap(prevRange, groupRange, ref distanceX, ref distanceY, ref overlapX, ref overlapY);
1469
                                    if ((slopeType == SlopeType.HORIZONTAL && overlapX) ||
1470
                                        (slopeType == SlopeType.VERTICAL && overlapY))
1471
                                    {
1472
                                        RemoveSymbol(group);
1473
                                        foreach (var _temp in group)
1474
                                            SPPIDUtil.CalcNewCoordinateForSymbol(_temp, prevSymbol, distanceX, distanceY);
1475

    
1476
                                        SymbolGroupModeling(priority, group);
1477
                                    }
1478
                                }
1479
                                else
1480
                                {
1481
                                    SymbolModeling(connSymbol, null);
1482
                                    // Range 겹치는지 확인해야함
1483
                                    double[] prevRange = null;
1484
                                    GetSPPIDSymbolRange(prevSymbol, ref prevRange);
1485
                                    double[] connRange = null;
1486
                                    GetSPPIDSymbolRange(connSymbol, ref connRange);
1487

    
1488
                                    double distanceX = 0;
1489
                                    double distanceY = 0;
1490
                                    bool overlapX = false;
1491
                                    bool overlapY = false;
1492
                                    SlopeType slopeType = SPPIDUtil.CalcSlope(prevSymbol.SPPID.ORIGINAL_X, prevSymbol.SPPID.ORIGINAL_Y, connSymbol.SPPID.ORIGINAL_X, connSymbol.SPPID.ORIGINAL_Y);
1493
                                    SPPIDUtil.CalcOverlap(prevRange, connRange, ref distanceX, ref distanceY, ref overlapX, ref overlapY);
1494
                                    if ((slopeType == SlopeType.HORIZONTAL && overlapX) ||
1495
                                        (slopeType == SlopeType.VERTICAL && overlapY))
1496
                                    {
1497
                                        RemoveSymbol(connSymbol);
1498
                                        SPPIDUtil.CalcNewCoordinateForSymbol(connSymbol, prevSymbol, distanceX, distanceY);
1499

    
1500
                                        SymbolModeling(connSymbol, null);
1501
                                    }
1502
                                }
1503
                            }
1504
                            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetProgress, document.SYMBOLS.FindAll(x => !string.IsNullOrEmpty(x.SPPID.RepresentationId)).Count);
1505
                            SymbolModelingByNeerSymbolLoop(connSymbol, endObjects);
1506
                        }
1507
                        else if (connItem.GetType() == typeof(Line))
1508
                        {
1509
                            Line connLine = connItem as Line;
1510
                            if (!SPPIDUtil.IsBranchLine(connLine, line))
1511
                                SymbolModelingByNeerLineLoop(connLine, endObjects, prevSymbol);
1512
                        }
1513
                    }
1514
                }
1515
            }
1516
        }
1517

    
1518
        private void SymbolGroupModeling(Symbol firstSymbol, List<Symbol> group)
1519
        {
1520
            List<Symbol> endModelingGroup = new List<Symbol>();
1521
            SymbolModeling(firstSymbol, null);
1522
            endModelingGroup.Add(firstSymbol);
1523
            while (endModelingGroup.Count != group.Count)
1524
            {
1525
                foreach (var _symbol in group)
1526
                {
1527
                    if (!endModelingGroup.Contains(_symbol))
1528
                    {
1529
                        foreach (var _connector in _symbol.CONNECTORS)
1530
                        {
1531
                            Symbol _connSymbol = SPPIDUtil.FindObjectByUID(document, _connector.CONNECTEDITEM) as Symbol;
1532
                            if (_connSymbol != null && endModelingGroup.Contains(_connSymbol))
1533
                            {
1534
                                SymbolModeling(_symbol, _connSymbol);
1535
                                endModelingGroup.Add(_symbol);
1536
                                break;
1537
                            }
1538
                        }
1539
                    }
1540
                }
1541
            }
1542
        }
1543

    
1544
        /// <summary>
1545
        /// 심볼을 실제로 Modeling할때 ChildSymbol이 있다면 Modeling하는 메서드
1546
        /// </summary>
1547
        /// <param name="childSymbol"></param>
1548
        /// <param name="parentSymbol"></param>
1549
        private void CreateChildSymbol(ChildSymbol childSymbol, LMSymbol parentSymbol, Symbol parent)
1550
        {
1551
            Ingr.RAD2D.Symbol2d symbol2d = radApp.ActiveDocument.ActiveSheet.DrawingObjects[parentSymbol.get_GraphicOID().ToString()];
1552
            double x1 = 0;
1553
            double x2 = 0;
1554
            double y1 = 0;
1555
            double y2 = 0;
1556
            symbol2d.Range(out x1, out y1, out x2, out y2);
1557

    
1558
            LMSymbol _LMSymbol = _placement.PIDPlaceSymbol(childSymbol.SPPID.MAPPINGNAME, (x1 + x2) / 2, (y1 + y2) / 2, TargetItem: parentSymbol);
1559
            if (_LMSymbol != null)
1560
            {
1561
                childSymbol.SPPID.RepresentationId = _LMSymbol.AsLMRepresentation().Id;
1562
                foreach (var item in childSymbol.ChildSymbols)
1563
                    CreateChildSymbol(item, _LMSymbol, parent);
1564
            }
1565

    
1566

    
1567
            ReleaseCOMObjects(_LMSymbol);
1568
        }
1569

    
1570
        private void NewLineModeling(Line line, bool isBranchModeling = false)
1571
        {
1572
            if (!string.IsNullOrEmpty(line.SPPID.ModelItemId) || (NewBranchLines.Contains(line) && !isBranchModeling))
1573
                return;
1574

    
1575
            List<Line> group = new List<Line>();
1576
            GetConnectedLineGroup(line, group);
1577
            LineCoordinateCorrection(group);
1578

    
1579
            foreach (var groupLine in group)
1580
            {
1581
                if (!isBranchModeling && SPPIDUtil.IsBranchLine(groupLine))
1582
                {
1583
                    NewBranchLines.Add(groupLine);
1584
                    continue;
1585
                }
1586

    
1587
                bool diagonal = false;
1588
                if (groupLine.SlopeType != SlopeType.HORIZONTAL && groupLine.SlopeType != SlopeType.VERTICAL)
1589
                    diagonal = true;
1590
                _LMAItem _LMAItem = _placement.PIDCreateItem(groupLine.SPPID.MAPPINGNAME);
1591
                LMSymbol _LMSymbolStart = null;
1592
                LMSymbol _LMSymbolEnd = null;
1593
                PlaceRunInputs placeRunInputs = new PlaceRunInputs();
1594
                foreach (var connector in groupLine.CONNECTORS)
1595
                {
1596
                    
1597
                    double x = 0;
1598
                    double y = 0;
1599
                    GetTargetLineConnectorPoint(connector, groupLine, ref x, ref y);
1600
                    if (connector.ConnectedObject == null)
1601
                    {
1602
                        placeRunInputs.AddPoint(x, y);
1603
                    }
1604
                    else if (connector.ConnectedObject.GetType() == typeof(Symbol))
1605
                    {
1606
                        Symbol targetSymbol = connector.ConnectedObject as Symbol;
1607
                        GetTargetSymbolConnectorPoint(targetSymbol.CONNECTORS.Find(z => z.ConnectedObject == groupLine), targetSymbol, ref x, ref y);
1608
                        if (groupLine.CONNECTORS.IndexOf(connector) == 0)
1609
                        {
1610
                            _LMSymbolStart = GetTargetSymbol(targetSymbol, groupLine);
1611
                            if (_LMSymbolStart != null)
1612
                                placeRunInputs.AddSymbolTarget(_LMSymbolStart, x, y, diagonal);
1613
                            else
1614
                                placeRunInputs.AddPoint(x, y);
1615
                        }
1616
                        else
1617
                        {
1618
                            _LMSymbolEnd = GetTargetSymbol(targetSymbol, groupLine);
1619
                            if (_LMSymbolEnd != null)
1620
                                placeRunInputs.AddSymbolTarget(_LMSymbolEnd, x, y, diagonal);
1621
                            else
1622
                                placeRunInputs.AddPoint(x, y);
1623
                        }
1624
                    }
1625
                    else if (connector.ConnectedObject.GetType() == typeof(Line))
1626
                    {
1627
                        Line targetLine = connector.ConnectedObject as Line;
1628
                        if (!string.IsNullOrEmpty(targetLine.SPPID.ModelItemId))
1629
                        {
1630
                            LMConnector targetConnector = FindTargetLMConnectorForBranch(line, targetLine, ref x, ref y);
1631
                            if (targetConnector != null)
1632
                            {
1633
                                placeRunInputs.AddConnectorTarget(targetConnector, x, y, diagonal);
1634
                                ChangeLineSPPIDCoordinateByConnector(groupLine, targetLine, x, y, false);
1635
                            }
1636
                            else
1637
                            {
1638
                                placeRunInputs.AddPoint( x, y);
1639
                                ChangeLineSPPIDCoordinateByConnector(groupLine, targetLine, x, y, false);
1640
                            }
1641
                        }
1642
                        else
1643
                        {
1644
                            if (groupLine.CONNECTORS.IndexOf(connector) == 0)
1645
                            {
1646
                                if (groupLine.SlopeType == SlopeType.HORIZONTAL)
1647
                                    placeRunInputs.AddPoint(x, -y - 0.1);
1648
                                else if (groupLine.SlopeType == SlopeType.VERTICAL)
1649
                                    placeRunInputs.AddPoint(-x - 0.1, y);
1650
                                else
1651
                                {
1652
                                    if (SPPIDUtil.CalcAngle(groupLine.SPPID.START_X, groupLine.SPPID.START_Y, groupLine.SPPID.END_X, groupLine.SPPID.END_Y) < 45)
1653
                                        placeRunInputs.AddPoint(x, -y - 0.1);
1654
                                    else
1655
                                        placeRunInputs.AddPoint(-x - 0.1, y);
1656
                                }
1657
                            }
1658

    
1659
                            placeRunInputs.AddPoint(x, y);
1660

    
1661
                            if (groupLine.CONNECTORS.IndexOf(connector) == 1)
1662
                            {
1663
                                if (groupLine.SlopeType == SlopeType.HORIZONTAL)
1664
                                    placeRunInputs.AddPoint(x, -y - 0.1);
1665
                                else if (groupLine.SlopeType == SlopeType.VERTICAL)
1666
                                    placeRunInputs.AddPoint(-x - 0.1, y);
1667
                                else
1668
                                {
1669
                                    if (SPPIDUtil.CalcAngle(groupLine.SPPID.START_X, groupLine.SPPID.START_Y, groupLine.SPPID.END_X, groupLine.SPPID.END_Y) < 45)
1670
                                        placeRunInputs.AddPoint(x, -y - 0.1);
1671
                                    else
1672
                                        placeRunInputs.AddPoint(-x - 0.1, y);
1673
                                }
1674
                            }
1675
                        }
1676
                    }
1677
                }
1678

    
1679
                LMConnector _lMConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
1680
                if (_lMConnector != null)
1681
                {
1682
                    groupLine.SPPID.ModelItemId = _lMConnector.ModelItemID;
1683
                    bool bRemodelingStart = false;
1684
                    if (_LMSymbolStart != null)
1685
                        NeedReModeling(groupLine, _LMSymbolStart, ref bRemodelingStart);
1686
                    bool bRemodelingEnd = false;
1687
                    if (_LMSymbolEnd != null)
1688
                        NeedReModeling(groupLine, _LMSymbolEnd, ref bRemodelingEnd);
1689

    
1690
                    if (bRemodelingStart || bRemodelingEnd)
1691
                        ReModelingLine(groupLine, _lMConnector, _LMSymbolStart, _LMSymbolEnd, bRemodelingStart, bRemodelingEnd);
1692

    
1693
                    FlowMarkModeling(groupLine);
1694
                    LineNumberModeling(groupLine);
1695

    
1696
                    ReleaseCOMObjects(_lMConnector);
1697
                }
1698

    
1699
                List<object> removeLines = groupLine.CONNECTORS.FindAll(x =>
1700
                x.ConnectedObject != null &&
1701
                x.ConnectedObject.GetType() == typeof(Line) &&
1702
                !string.IsNullOrEmpty(((Line)x.ConnectedObject).SPPID.ModelItemId))
1703
                .Select(x => x.ConnectedObject)
1704
                .ToList();
1705

    
1706
                foreach (var item in removeLines)
1707
                    RemoveLineForModeling(item as Line);
1708

    
1709
                if (_LMAItem != null)
1710
                    ReleaseCOMObjects(_LMAItem);
1711
                if (placeRunInputs != null)
1712
                    ReleaseCOMObjects(placeRunInputs);
1713
                if (_LMSymbolStart != null)
1714
                    ReleaseCOMObjects(_LMSymbolStart);
1715
                if (_LMSymbolEnd != null)
1716
                    ReleaseCOMObjects(_LMSymbolEnd);
1717

    
1718
                if (isBranchModeling && NewBranchLines.Contains(groupLine))
1719
                    NewBranchLines.Remove(groupLine);
1720
            }
1721
        }
1722

    
1723
        private void RemoveLineForModeling(Line line)
1724
        {
1725
            LMModelItem modelItem = dataSource.GetModelItem(line.SPPID.ModelItemId);
1726
            if (modelItem != null)
1727
            {
1728
                foreach (LMRepresentation rep in modelItem.Representations)
1729
                {
1730
                    if (rep.Attributes["RepresentationType"].get_Value() == "Connector" && rep.Attributes["ItemStatus"].get_Value() == "Active")
1731
                    {
1732
                        LMConnector _LMConnector = dataSource.GetConnector(rep.Id);
1733
                        dynamic OID = rep.get_GraphicOID().ToString();
1734
                        DependencyObject drawingObject = radApp.ActiveDocument.ActiveSheet.DrawingObjects[OID];
1735
                        Ingr.RAD2D.LineStringGeometry2d lineStringGeometry = drawingObject.GetGeometry() as Ingr.RAD2D.LineStringGeometry2d;
1736
                        int verticesCount = lineStringGeometry.VertexCount;
1737
                        double[] vertices = null;
1738
                        lineStringGeometry.GetVertices(ref verticesCount, ref vertices);
1739
                        for (int i = 0; i < verticesCount; i++)
1740
                        {
1741
                            double x = 0;
1742
                            double y = 0;
1743
                            lineStringGeometry.GetVertex(i + 1, ref x, ref y);
1744
                            if (verticesCount == 2 && (x < 0 || y < 0))
1745
                                _placement.PIDRemovePlacement(rep);
1746
                        }
1747
                        ReleaseCOMObjects(_LMConnector);
1748
                    }
1749
                }
1750

    
1751
                ReleaseCOMObjects(modelItem);
1752
            }
1753
        }
1754

    
1755
        private void GetConnectedLineGroup(Line line, List<Line> group)
1756
        {
1757
            if (!group.Contains(line))
1758
                group.Add(line);
1759
            foreach (var connector in line.CONNECTORS)
1760
            {
1761
                if (connector.ConnectedObject != null &&
1762
                    connector.ConnectedObject.GetType() == typeof(Line) &&
1763
                    !group.Contains(connector.ConnectedObject) &&
1764
                    string.IsNullOrEmpty(((Line)connector.ConnectedObject).SPPID.ModelItemId))
1765
                {
1766
                    Line connLine = connector.ConnectedObject as Line;
1767
                    if (!SPPIDUtil.IsBranchLine(connLine, line))
1768
                    {
1769
                        if (line.CONNECTORS.IndexOf(connector) == 0)
1770
                            group.Insert(0, connLine);
1771
                        else
1772
                            group.Add(connLine);
1773
                        GetConnectedLineGroup(connLine, group);
1774
                    }
1775
                }
1776
            }
1777
        }
1778

    
1779
        private void LineCoordinateCorrection(List<Line> group)
1780
        {
1781
            // 순서대로 전 Item 기준 정렬
1782
            LineCoordinateCorrectionByStart(group);
1783

    
1784
            // 역으로 심볼이 있을 경우 좌표 보정
1785
            LineCoordinateCorrectionForLastLine(group);
1786
        }
1787

    
1788
        private void LineCoordinateCorrectionByStart(List<Line> group)
1789
        {
1790
            for (int i = 0; i < group.Count; i++)
1791
            {
1792
                Line line = group[i];
1793
                if (i == 0)
1794
                {
1795
                    Connector symbolConnector = line.CONNECTORS.Find(x => x.ConnectedObject != null && x.ConnectedObject.GetType() == typeof(Symbol));
1796
                    if (symbolConnector != null)
1797
                        LineCoordinateCorrectionByConnItem(line, symbolConnector.ConnectedObject);
1798
                }
1799
                else if (i != 0)
1800
                {
1801
                    LineCoordinateCorrectionByConnItem(line, group[i - 1]);
1802
                }
1803
            }
1804
        }
1805

    
1806
        private void LineCoordinateCorrectionForLastLine(List<Line> group)
1807
        {
1808
            Line checkLine = group[group.Count - 1];
1809
            Connector lastSymbolConnector = checkLine.CONNECTORS.Find(x => x.ConnectedObject != null && x.ConnectedObject.GetType() == typeof(Symbol));
1810
            if (lastSymbolConnector != null)
1811
            {
1812
                LineCoordinateCorrectionByConnItem(checkLine, lastSymbolConnector.ConnectedObject);
1813
                for (int i = group.Count - 2; i >= 0; i--)
1814
                {
1815
                    Line line = group[i + 1];
1816
                    Line prevLine = group[i];
1817

    
1818
                    // 같으면 보정
1819
                    if (line.SlopeType == prevLine.SlopeType)
1820
                        LineCoordinateCorrectionByConnItem(prevLine, line);
1821
                    else
1822
                    {
1823
                        if (line.SlopeType == SlopeType.HORIZONTAL)
1824
                        {
1825
                            double prevX = 0;
1826
                            double prevY = 0;
1827
                            GetTargetLineConnectorPoint(prevLine.CONNECTORS.Find(z => z.ConnectedObject == line), prevLine, ref prevX, ref prevY);
1828
                            ChangeLineSPPIDCoordinateByConnectorOnlyX(line, prevLine, prevX);
1829

    
1830
                            double x = 0;
1831
                            double y = 0;
1832
                            GetTargetLineConnectorPoint(line.CONNECTORS.Find(z => z.ConnectedObject == prevLine), line, ref x, ref y);
1833
                            ChangeLineSPPIDCoordinateByConnectorOnlyY(prevLine, line, y);
1834
                        }
1835
                        else if (line.SlopeType == SlopeType.VERTICAL)
1836
                        {
1837
                            double prevX = 0;
1838
                            double prevY = 0;
1839
                            GetTargetLineConnectorPoint(prevLine.CONNECTORS.Find(z => z.ConnectedObject == line), prevLine, ref prevX, ref prevY);
1840
                            ChangeLineSPPIDCoordinateByConnectorOnlyY(line, prevLine, prevY);
1841

    
1842
                            double x = 0;
1843
                            double y = 0;
1844
                            GetTargetLineConnectorPoint(line.CONNECTORS.Find(z => z.ConnectedObject == prevLine), line, ref x, ref y);
1845
                            ChangeLineSPPIDCoordinateByConnectorOnlyX(prevLine, line, x);
1846
                        }
1847
                        break;
1848
                    }
1849
                }
1850
            }
1851
        }
1852

    
1853
        private void LineCoordinateCorrectionByConnItem(Line line, object connItem)
1854
        {
1855
            double x = 0;
1856
            double y = 0;
1857
            if (connItem.GetType() == typeof(Symbol))
1858
            {
1859
                Symbol targetSymbol = connItem as Symbol;
1860
                Connector targetConnector = targetSymbol.CONNECTORS.Find(z => z.ConnectedObject == line);
1861
                if (targetConnector != null)
1862
                    GetTargetSymbolConnectorPoint(targetConnector, targetSymbol, ref x, ref y);
1863
                else
1864
                    throw new Exception("Target symbol UID : " + targetSymbol.UID + "\r\nLine UID : " + line.UID);
1865
            }
1866
            else if (connItem.GetType() == typeof(Line))
1867
            {
1868
                Line targetLine = connItem as Line;
1869
                GetTargetLineConnectorPoint(targetLine.CONNECTORS.Find(z => z.ConnectedObject == line), targetLine, ref x, ref y);
1870
            }
1871

    
1872
            ChangeLineSPPIDCoordinateByConnector(line, connItem, x, y);
1873
        }
1874

    
1875
        private void ChangeLineSPPIDCoordinateByConnector(Line line, object connItem, double x, double y, bool changeOtherCoordinate = true)
1876
        {
1877
            Connector connector = line.CONNECTORS.Find(z => z.ConnectedObject == connItem);
1878
            int index = line.CONNECTORS.IndexOf(connector);
1879
            if (index == 0)
1880
            {
1881
                line.SPPID.START_X = x;
1882
                line.SPPID.START_Y = y;
1883
                if (line.SlopeType == SlopeType.HORIZONTAL && changeOtherCoordinate)
1884
                    line.SPPID.END_Y = y;
1885
                else if (line.SlopeType == SlopeType.VERTICAL && changeOtherCoordinate)
1886
                    line.SPPID.END_X = x;
1887
            }
1888
            else
1889
            {
1890
                line.SPPID.END_X = x;
1891
                line.SPPID.END_Y = y;
1892
                if (line.SlopeType == SlopeType.HORIZONTAL && changeOtherCoordinate)
1893
                    line.SPPID.START_Y = y;
1894
                else if (line.SlopeType == SlopeType.VERTICAL && changeOtherCoordinate)
1895
                    line.SPPID.START_X = x;
1896
            }
1897
        }
1898

    
1899
        private void ChangeLineSPPIDCoordinateByConnectorOnlyX(Line line, object connItem, double x)
1900
        {
1901
            Connector connector = line.CONNECTORS.Find(z => z.ConnectedObject == connItem);
1902
            int index = line.CONNECTORS.IndexOf(connector);
1903
            if (index == 0)
1904
            {
1905
                line.SPPID.START_X = x;
1906
                if (line.SlopeType == SlopeType.VERTICAL)
1907
                    line.SPPID.END_X = x;
1908
            }
1909
            else
1910
            {
1911
                line.SPPID.END_X = x;
1912
                if (line.SlopeType == SlopeType.VERTICAL)
1913
                    line.SPPID.START_X = x;
1914
            }
1915
        }
1916

    
1917
        private void ChangeLineSPPIDCoordinateByConnectorOnlyY(Line line, object connItem, double y)
1918
        {
1919
            Connector connector = line.CONNECTORS.Find(z => z.ConnectedObject == connItem);
1920
            int index = line.CONNECTORS.IndexOf(connector);
1921
            if (index == 0)
1922
            {
1923
                line.SPPID.START_Y = y;
1924
                if (line.SlopeType == SlopeType.HORIZONTAL)
1925
                    line.SPPID.END_Y = y;
1926
            }
1927
            else
1928
            {
1929
                line.SPPID.END_Y = y;
1930
                if (line.SlopeType == SlopeType.HORIZONTAL)
1931
                    line.SPPID.START_Y = y;
1932
            }
1933
        }
1934

    
1935
        private void NeedReModeling(Line line, LMSymbol symbol, ref bool result)
1936
        {
1937
            if (symbol != null)
1938
            {
1939
                string repID = symbol.AsLMRepresentation().Id;
1940
                string symbolUID = SPPIDUtil.FindSymbolByRepresentationID(document, repID).UID;
1941
                string lineUID = line.UID;
1942

    
1943
                SpecBreak startSpecBreak = document.SpecBreaks.Find(x =>
1944
                (x.DownStreamUID == symbolUID || x.UpStreamUID == symbolUID) &&
1945
                (x.DownStreamUID == lineUID || x.UpStreamUID == lineUID));
1946

    
1947
                EndBreak startEndBreak = document.EndBreaks.Find(x =>
1948
                (x.OWNER == symbolUID || x.PROPERTIES.Find(y => y.ATTRIBUTE == "Connected Item").VALUE == symbolUID) &&
1949
                (x.OWNER == lineUID || x.PROPERTIES.Find(y => y.ATTRIBUTE == "Connected Item").VALUE == lineUID));
1950

    
1951
                if (startSpecBreak != null || startEndBreak != null)
1952
                    result = true;
1953
            }
1954
        }
1955

    
1956
        /// <summary>
1957
        /// Symbol에 붙을 경우 Line을 Remodeling 한다.
1958
        /// </summary>
1959
        /// <param name="lines"></param>
1960
        /// <param name="prevLMConnector"></param>
1961
        /// <param name="startSymbol"></param>
1962
        /// <param name="endSymbol"></param>
1963
        private void ReModelingLine(Line line, LMConnector prevLMConnector, LMSymbol startSymbol, LMSymbol endSymbol, bool bStart, bool bEnd)
1964
        {
1965
            string symbolPath = string.Empty;
1966
            #region get symbol path
1967
            LMModelItem modelItem = dataSource.GetModelItem(prevLMConnector.ModelItemID);
1968
            symbolPath = GetSPPIDFileName(modelItem);
1969
            #endregion
1970
            bool diagonal = false;
1971
            if (line.SlopeType != SlopeType.HORIZONTAL && line.SlopeType != SlopeType.VERTICAL)
1972
                diagonal = true;
1973
            _LMAItem _LMAItem = _placement.PIDCreateItem(symbolPath);
1974
            LMConnector newConnector = null;
1975
            dynamic OID = prevLMConnector.get_GraphicOID().ToString();
1976
            DependencyObject drawingObject = radApp.ActiveDocument.ActiveSheet.DrawingObjects[OID];
1977
            Ingr.RAD2D.LineStringGeometry2d lineStringGeometry = drawingObject.GetGeometry() as Ingr.RAD2D.LineStringGeometry2d;
1978
            int verticesCount = lineStringGeometry.VertexCount;
1979
            PlaceRunInputs placeRunInputs = new PlaceRunInputs();
1980

    
1981
            List<double[]> vertices = new List<double[]>();
1982
            for (int i = 1; i <= verticesCount; i++)
1983
            {
1984
                double x = 0;
1985
                double y = 0;
1986
                lineStringGeometry.GetVertex(i, ref x, ref y);
1987
                vertices.Add(new double[] { x, y });
1988
            }
1989

    
1990
            for (int i = 0; i < vertices.Count; i++)
1991
            {
1992
                double[] points = vertices[i];
1993
                // 시작 심볼이 있고 첫번째 좌표일 때
1994
                if (startSymbol != null && i == 0)
1995
                {
1996
                    if (bStart)
1997
                    {
1998
                        SlopeType slopeType = SPPIDUtil.CalcSlope(points[0], points[1], vertices[i + 1][0], vertices[i + 1][1]);
1999
                        if (slopeType == SlopeType.HORIZONTAL)
2000
                            placeRunInputs.AddPoint(points[0], -0.1);
2001
                        else if (slopeType == SlopeType.VERTICAL)
2002
                            placeRunInputs.AddPoint(-0.1, points[1]);
2003
                        else
2004
                            placeRunInputs.AddPoint(points[0], -0.1);
2005

    
2006
                        placeRunInputs.AddPoint(points[0], points[1]);
2007
                    }
2008
                    else
2009
                    {
2010
                        placeRunInputs.AddSymbolTarget(startSymbol, points[0], points[1], diagonal);
2011
                    }
2012
                }
2013
                // 마지막 심볼이 있고 마지막 좌표일 때
2014
                else if (endSymbol != null && i == vertices.Count - 1)
2015
                {
2016
                    if (bEnd)
2017
                    {
2018
                        placeRunInputs.AddPoint(points[0], points[1]);
2019

    
2020
                        SlopeType slopeType = SPPIDUtil.CalcSlope(points[0], points[1], vertices[i - 1][0], vertices[i - 1][1]);
2021
                        if (slopeType == SlopeType.HORIZONTAL)
2022
                            placeRunInputs.AddPoint(points[0], -0.1);
2023
                        else if (slopeType == SlopeType.VERTICAL)
2024
                            placeRunInputs.AddPoint(-0.1, points[1]);
2025
                        else
2026
                            placeRunInputs.AddPoint(points[0], -0.1);
2027
                    }
2028
                    else
2029
                    {
2030
                        placeRunInputs.AddSymbolTarget(endSymbol, points[0], points[1], diagonal);
2031
                    }
2032
                }
2033
                // 첫번째이며 시작 심볼이 아니고 Connecotr일 경우
2034
                else if (i == 0 && prevLMConnector.ConnectItem1SymbolObject != null)
2035
                    placeRunInputs.AddSymbolTarget(prevLMConnector.ConnectItem1SymbolObject, points[0], points[1], diagonal);
2036
                // 마지막이며 마지막 심볼이 아니고 Connecotr일 경우
2037
                else if (i == vertices.Count - 1 && prevLMConnector.ConnectItem2SymbolObject != null)
2038
                    placeRunInputs.AddSymbolTarget(prevLMConnector.ConnectItem2SymbolObject, points[0], points[1], diagonal);
2039
                else
2040
                    placeRunInputs.AddPoint(points[0], points[1]);
2041
            }
2042

    
2043
            _placement.PIDRemovePlacement(prevLMConnector.AsLMRepresentation());
2044
            newConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
2045

    
2046
            ReleaseCOMObjects(placeRunInputs);
2047
            ReleaseCOMObjects(_LMAItem);
2048
            ReleaseCOMObjects(modelItem);
2049

    
2050
            if (newConnector != null)
2051
            {
2052
                if (startSymbol != null && bStart)
2053
                {
2054
                    _LMAItem = _placement.PIDCreateItem(symbolPath);
2055
                    placeRunInputs = new PlaceRunInputs();
2056
                    placeRunInputs.AddSymbolTarget(startSymbol, vertices[0][0], vertices[0][1]);
2057
                    placeRunInputs.AddConnectorTarget(newConnector, vertices[0][0], vertices[0][1]);
2058
                    LMConnector _LMConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
2059
                    if (_LMConnector != null)
2060
                    {
2061
                        RemoveConnectorForReModelingLine(newConnector);
2062
                        ZeroLengthModelItemID.Add(_LMConnector.ModelItemID);
2063
                        ReleaseCOMObjects(_LMConnector);
2064
                    }
2065
                    ReleaseCOMObjects(placeRunInputs);
2066
                    ReleaseCOMObjects(_LMAItem);
2067
                }
2068

    
2069
                if (endSymbol != null && bEnd)
2070
                {
2071
                    if (startSymbol != null)
2072
                    {
2073
                        Dictionary<LMConnector, List<double[]>> dicVertices = GetPipeRunVertices(newConnector.ModelItemID);
2074
                        newConnector = dicVertices.First().Key;
2075
                    }
2076

    
2077
                    _LMAItem = _placement.PIDCreateItem(symbolPath);
2078
                    placeRunInputs = new PlaceRunInputs();
2079
                    placeRunInputs.AddSymbolTarget(endSymbol, vertices[vertices.Count - 1][0], vertices[vertices.Count - 1][1]);
2080
                    placeRunInputs.AddConnectorTarget(newConnector, vertices[vertices.Count - 1][0], vertices[vertices.Count - 1][1]);
2081
                    LMConnector _LMConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
2082
                    if (_LMConnector != null)
2083
                    {
2084
                        RemoveConnectorForReModelingLine(newConnector);
2085
                        ZeroLengthModelItemIDReverse.Add(_LMConnector.ModelItemID);
2086
                        ReleaseCOMObjects(_LMConnector);
2087
                    }
2088
                    ReleaseCOMObjects(placeRunInputs);
2089
                    ReleaseCOMObjects(_LMAItem);
2090
                }
2091

    
2092
                line.SPPID.ModelItemId = newConnector.ModelItemID;
2093
                ReleaseCOMObjects(newConnector);
2094
            }
2095

    
2096
            ReleaseCOMObjects(modelItem);
2097
        }
2098

    
2099
        /// <summary>
2100
        /// Remodeling 과정에서 생긴 불필요한 Connector 제거
2101
        /// </summary>
2102
        /// <param name="connector"></param>
2103
        private void RemoveConnectorForReModelingLine(LMConnector connector)
2104
        {
2105
            Dictionary<LMConnector, List<double[]>> dicVertices = GetPipeRunVertices(connector.ModelItemID);
2106
            foreach (var item in dicVertices)
2107
            {
2108
                if (item.Value.Count == 2)
2109
                {
2110
                    bool result = false;
2111
                    foreach (var point in item.Value)
2112
                    {
2113
                        if (point[0] < 0 || point[1] < 0)
2114
                        {
2115
                            result = true;
2116
                            _placement.PIDRemovePlacement(item.Key.AsLMRepresentation());
2117
                            break;
2118
                        }
2119
                    }
2120

    
2121
                    if (result)
2122
                        break;
2123
                }
2124
            }
2125
            foreach (var item in dicVertices)
2126
                ReleaseCOMObjects(item.Key);
2127
        }
2128

    
2129
        /// <summary>
2130
        /// Symbol이 모델링된 SPPPID Symbol Object를 반환 - 연결된 Symbol이 ChildSymbol일 수도 있기때문에 메서드 개발
2131
        /// </summary>
2132
        /// <param name="symbol"></param>
2133
        /// <param name="line"></param>
2134
        /// <returns></returns>
2135
        private LMSymbol GetTargetSymbol(Symbol symbol, Line line)
2136
        {
2137
            LMSymbol _LMSymbol = null;
2138
            foreach (var connector in symbol.CONNECTORS)
2139
            {
2140
                if (connector.CONNECTEDITEM == line.UID)
2141
                {
2142
                    if (connector.Index == 0)
2143
                        _LMSymbol = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
2144
                    else
2145
                    {
2146
                        ChildSymbol child = null;
2147
                        foreach (var childSymbol in symbol.ChildSymbols)
2148
                        {
2149
                            if (childSymbol.Connectors.Contains(connector))
2150
                                child = childSymbol;
2151
                            else
2152
                                child = GetChildSymbolByConnector(childSymbol, connector);
2153

    
2154
                            if (child != null)
2155
                                break;
2156
                        }
2157

    
2158
                        if (child != null)
2159
                            _LMSymbol = dataSource.GetSymbol(child.SPPID.RepresentationId);
2160
                    }
2161

    
2162
                    break;
2163
                }
2164
            }
2165

    
2166
            return _LMSymbol;
2167
        }
2168

    
2169
        /// <summary>
2170
        /// Connector를 가지고 있는 ChildSymbol Object 반환
2171
        /// </summary>
2172
        /// <param name="item"></param>
2173
        /// <param name="connector"></param>
2174
        /// <returns></returns>
2175
        private ChildSymbol GetChildSymbolByConnector(ChildSymbol item, Connector connector)
2176
        {
2177
            foreach (var childSymbol in item.ChildSymbols)
2178
            {
2179
                if (childSymbol.Connectors.Contains(connector))
2180
                    return childSymbol;
2181
                else
2182
                    return GetChildSymbolByConnector(childSymbol, connector);
2183
            }
2184

    
2185
            return null;
2186
        }
2187

    
2188
        /// <summary>
2189
        /// EndBreak 모델링 메서드
2190
        /// </summary>
2191
        /// <param name="endBreak"></param>
2192
        private void EndBreakModeling(EndBreak endBreak)
2193
        {
2194
            object ownerObj = SPPIDUtil.FindObjectByUID(document, endBreak.OWNER);
2195
            object connectedItem = SPPIDUtil.FindObjectByUID(document, endBreak.PROPERTIES.Find(x => x.ATTRIBUTE == "Connected Item").VALUE);
2196
            LMConnector targetLMConnector = FindBreakLineTarget(ownerObj, connectedItem);
2197

    
2198
            if (targetLMConnector != null)
2199
            {
2200
                Array array = new double[] { 0, endBreak.SPPID.ORIGINAL_X, endBreak.SPPID.ORIGINAL_Y };
2201
                LMLabelPersist _LmLabelPersist = _placement.PIDPlaceLabel(endBreak.SPPID.MAPPINGNAME, ref array, Rotation: 0, LabeledItem: targetLMConnector.AsLMRepresentation(), IsLeaderVisible: true);
2202
                if (_LmLabelPersist != null)
2203
                {
2204
                    endBreak.SPPID.RepresentationId = _LmLabelPersist.AsLMRepresentation().Id;
2205
                    if (_LmLabelPersist.ModelItemObject != null)
2206
                        endBreak.SPPID.ModelItemID = _LmLabelPersist.ModelItemID;
2207
                    endBreak.SPPID.GraphicOID = _LmLabelPersist.get_GraphicOID().ToString();
2208
                    ReleaseCOMObjects(_LmLabelPersist);
2209
                }
2210
                ReleaseCOMObjects(targetLMConnector);
2211
            }
2212
        }
2213

    
2214
        private LMConnector ReModelingLMConnector(LMConnector connector)
2215
        {
2216
            string symbolPath = string.Empty;
2217
            #region get symbol path
2218
            LMModelItem modelItem = dataSource.GetModelItem(connector.ModelItemID);
2219
            symbolPath = GetSPPIDFileName(modelItem);
2220
            #endregion
2221

    
2222
            LMConnector newConnector = null;
2223
            dynamic OID = connector.get_GraphicOID().ToString();
2224
            DependencyObject drawingObject = radApp.ActiveDocument.ActiveSheet.DrawingObjects[OID];
2225
            Ingr.RAD2D.LineStringGeometry2d lineStringGeometry = drawingObject.GetGeometry() as Ingr.RAD2D.LineStringGeometry2d;
2226
            int verticesCount = lineStringGeometry.VertexCount;
2227
            PlaceRunInputs placeRunInputs = new PlaceRunInputs();
2228
            _LMAItem _LMAItem = _placement.PIDCreateItem(symbolPath);
2229

    
2230
            if (Convert.ToBoolean(connector.get_IsZeroLength()))
2231
            {
2232
                double[] vertices = null;
2233
                lineStringGeometry.GetVertices(ref verticesCount, ref vertices);
2234
                double x = 0;
2235
                double y = 0;
2236
                lineStringGeometry.GetVertex(1, ref x, ref y);
2237

    
2238
                placeRunInputs.AddSymbolTarget(connector.ConnectItem1SymbolObject, x, y);
2239
                placeRunInputs.AddSymbolTarget(connector.ConnectItem2SymbolObject, x, y);
2240

    
2241
                string flowDirection = string.Empty;
2242
                LMAAttribute flowAttribute = connector.ModelItemObject.Attributes["FlowDirection"];
2243
                if (flowAttribute != null && !DBNull.Value.Equals(flowAttribute.get_Value()))
2244
                    flowDirection = flowAttribute.get_Value().ToString();
2245

    
2246
                _placement.PIDRemovePlacement(connector.AsLMRepresentation());
2247
                newConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
2248
                ZeroLengthSymbolToSymbolModelItemID.Add(newConnector.ModelItemID);
2249
                if (!string.IsNullOrEmpty(flowDirection))
2250
                    newConnector.ModelItemObject.Attributes["FlowDirection"].set_Value(flowDirection);
2251
            }
2252
            else
2253
            {
2254
                List<double[]> vertices = new List<double[]>();
2255
                for (int i = 1; i <= verticesCount; i++)
2256
                {
2257
                    double x = 0;
2258
                    double y = 0;
2259
                    lineStringGeometry.GetVertex(i, ref x, ref y);
2260
                    vertices.Add(new double[] { x, y });
2261
                }
2262

    
2263
                for (int i = 0; i < vertices.Count; i++)
2264
                {
2265
                    double[] points = vertices[i];
2266
                    if (i == 0)
2267
                    {
2268
                        if (connector.ConnectItem1SymbolObject != null)
2269
                            placeRunInputs.AddSymbolTarget(connector.ConnectItem1SymbolObject, points[0], points[1]);
2270
                        else
2271
                            placeRunInputs.AddPoint(points[0], points[1]);
2272
                    }
2273
                    else if (i == vertices.Count - 1)
2274
                    {
2275
                        if (connector.ConnectItem2SymbolObject != null)
2276
                            placeRunInputs.AddSymbolTarget(connector.ConnectItem2SymbolObject, points[0], points[1]);
2277
                        else
2278
                            placeRunInputs.AddPoint(points[0], points[1]);
2279
                    }
2280
                    else
2281
                        placeRunInputs.AddPoint(points[0], points[1]);
2282
                }
2283

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

    
2286
                _placement.PIDRemovePlacement(connector.AsLMRepresentation());
2287
                newConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
2288

    
2289
                foreach (var line in lines)
2290
                    line.SPPID.ModelItemId = newConnector.ModelItemID;
2291
            }
2292

    
2293

    
2294
            return newConnector;
2295
        }
2296

    
2297
        /// <summary>
2298
        /// SpecBreak Modeling 메서드
2299
        /// </summary>
2300
        /// <param name="specBreak"></param>
2301
        private void SpecBreakModeling(SpecBreak specBreak)
2302
        {
2303
            object upStreamObj = SPPIDUtil.FindObjectByUID(document, specBreak.UpStreamUID);
2304
            object downStreamObj = SPPIDUtil.FindObjectByUID(document, specBreak.DownStreamUID);
2305

    
2306
            if (upStreamObj != null &&
2307
                downStreamObj != null)
2308
            {
2309
                LMConnector targetLMConnector = FindBreakLineTarget(upStreamObj, downStreamObj);
2310

    
2311
                if (targetLMConnector != null)
2312
                {
2313
                    foreach (var attribute in specBreak.ATTRIBUTES)
2314
                    {
2315
                        AttributeMapping mapping = document.AttributeMappings.Find(x => x.UID == attribute.UID);
2316
                        if (mapping != null && !string.IsNullOrEmpty(mapping.SPPIDSYMBOLNAME) && mapping.SPPIDSYMBOLNAME != "None")
2317
                        {
2318
                            string MappingPath = mapping.SPPIDSYMBOLNAME;
2319
                            Array array = new double[] { 0, specBreak.SPPID.ORIGINAL_X, specBreak.SPPID.ORIGINAL_Y };
2320
                            LMLabelPersist _LmLabelPersist = _placement.PIDPlaceLabel(MappingPath, ref array, Rotation: specBreak.ANGLE, LabeledItem: targetLMConnector.AsLMRepresentation(), IsLeaderVisible: mapping.LeaderLine);
2321

    
2322
                            if (_LmLabelPersist != null)
2323
                            {
2324
                                specBreak.SPPID.RepresentationId = _LmLabelPersist.AsLMRepresentation().Id;
2325
                                if (_LmLabelPersist.ModelItemObject != null)
2326
                                    specBreak.SPPID.ModelItemID = _LmLabelPersist.ModelItemID;
2327
                                specBreak.SPPID.GraphicOID = _LmLabelPersist.get_GraphicOID().ToString();
2328
                                ReleaseCOMObjects(_LmLabelPersist);
2329
                            }
2330
                        }
2331
                    }
2332
                    ReleaseCOMObjects(targetLMConnector);
2333
                }
2334
            }
2335
        }
2336

    
2337
        private LMConnector FindBreakLineTarget(object targetObj, object connectedObj)
2338
        {
2339
            LMConnector targetConnector = null;
2340
            Symbol targetSymbol = targetObj as Symbol;
2341
            Symbol connectedSymbol = connectedObj as Symbol;
2342
            Line targetLine = targetObj as Line;
2343
            Line connectedLine = connectedObj as Line;
2344
            if (targetSymbol != null && connectedSymbol != null)
2345
            {
2346
                LMSymbol targetLMSymbol = dataSource.GetSymbol(targetSymbol.SPPID.RepresentationId);
2347
                LMSymbol connectedLMSymbol = dataSource.GetSymbol(connectedSymbol.SPPID.RepresentationId);
2348

    
2349
                foreach (LMConnector connector in targetLMSymbol.Avoid1Connectors)
2350
                {
2351
                    if (connector.get_ItemStatus() != "Active")
2352
                        continue;
2353

    
2354
                    if (connector.ConnectItem1SymbolObject.Id == connectedLMSymbol.Id)
2355
                    {
2356
                        targetConnector = connector;
2357
                        break;
2358
                    }
2359
                    else if (connector.ConnectItem2SymbolObject.Id == connectedLMSymbol.Id)
2360
                    {
2361
                        targetConnector = connector;
2362
                        break;
2363
                    }
2364
                }
2365

    
2366
                foreach (LMConnector connector in targetLMSymbol.Avoid2Connectors)
2367
                {
2368
                    if (connector.get_ItemStatus() != "Active")
2369
                        continue;
2370

    
2371
                    if (connector.ConnectItem1SymbolObject.Id == connectedLMSymbol.Id)
2372
                    {
2373
                        targetConnector = connector;
2374
                        break;
2375
                    }
2376
                    else if (connector.ConnectItem2SymbolObject.Id == connectedLMSymbol.Id)
2377
                    {
2378
                        targetConnector = connector;
2379
                        break;
2380
                    }
2381
                }
2382

    
2383
                ReleaseCOMObjects(targetLMSymbol);
2384
                ReleaseCOMObjects(connectedLMSymbol);
2385
            }
2386
            else if (targetLine != null && connectedLine != null)
2387
            {
2388
                LMModelItem targetModelItem = dataSource.GetModelItem(targetLine.SPPID.ModelItemId);
2389
                LMModelItem connectedModelItem = dataSource.GetModelItem(connectedLine.SPPID.ModelItemId);
2390

    
2391
                if (targetModelItem != null && connectedModelItem != null)
2392
                {
2393
                    foreach (LMRepresentation rep in targetModelItem.Representations)
2394
                    {
2395
                        if (targetConnector != null)
2396
                            break;
2397

    
2398
                        if (rep.Attributes["RepresentationType"].get_Value() == "Connector" && rep.Attributes["ItemStatus"].get_Value() == "Active")
2399
                        {
2400
                            LMConnector _LMConnector = dataSource.GetConnector(rep.Id);
2401

    
2402
                            if (IsConnected(_LMConnector, connectedModelItem))
2403
                                targetConnector = _LMConnector;
2404
                            else
2405
                                ReleaseCOMObjects(_LMConnector);
2406
                        }
2407
                    }
2408

    
2409
                    ReleaseCOMObjects(targetModelItem);
2410
                }
2411
            }
2412
            else
2413
            {
2414
                LMSymbol connectedLMSymbol = null;
2415
                if (connectedSymbol != null)
2416
                    connectedLMSymbol = dataSource.GetSymbol(connectedSymbol.SPPID.RepresentationId);
2417
                else if (targetSymbol != null)
2418
                    connectedLMSymbol = dataSource.GetSymbol(targetSymbol.SPPID.RepresentationId);
2419
                else
2420
                {
2421

    
2422
                }
2423
                LMModelItem targetModelItem = null;
2424
                if (targetLine != null)
2425
                    targetModelItem = dataSource.GetModelItem(targetLine.SPPID.ModelItemId);
2426
                else if (connectedLine != null)
2427
                    targetModelItem = dataSource.GetModelItem(connectedLine.SPPID.ModelItemId);
2428
                else
2429
                {
2430

    
2431
                }
2432
                if (connectedLMSymbol != null && targetModelItem != null)
2433
                {
2434
                    foreach (LMConnector connector in connectedLMSymbol.Avoid1Connectors)
2435
                    {
2436
                        if (connector.get_ItemStatus() != "Active")
2437
                            continue;
2438

    
2439
                        if (IsConnected(connector, targetModelItem))
2440
                        {
2441
                            targetConnector = connector;
2442
                            break;
2443
                        }
2444
                    }
2445

    
2446
                    if (targetConnector == null)
2447
                    {
2448
                        foreach (LMConnector connector in connectedLMSymbol.Avoid2Connectors)
2449
                        {
2450
                            if (connector.get_ItemStatus() != "Active")
2451
                                continue;
2452

    
2453
                            if (IsConnected(connector, targetModelItem))
2454
                            {
2455
                                targetConnector = connector;
2456
                                break;
2457
                            }
2458
                        }
2459
                    }
2460
                }
2461

    
2462
            }
2463

    
2464
            return targetConnector;
2465
        }
2466

    
2467
        private bool IsConnected(LMConnector connector, LMModelItem modelItem)
2468
        {
2469
            bool result = false;
2470

    
2471
            foreach (LMRepresentation rep in modelItem.Representations)
2472
            {
2473
                if (result)
2474
                    break;
2475

    
2476
                if (rep.Attributes["RepresentationType"].get_Value() == "Connector" && rep.Attributes["ItemStatus"].get_Value() == "Active")
2477
                {
2478
                    LMConnector _LMConnector = dataSource.GetConnector(rep.Id);
2479

    
2480
                    if (_LMConnector.ConnectItem1SymbolObject != null &&
2481
                        connector.ConnectItem1SymbolObject != null &&
2482
                        _LMConnector.ConnectItem1SymbolObject.Id == connector.ConnectItem1SymbolObject.Id)
2483
                    {
2484
                        result = true;
2485
                        ReleaseCOMObjects(_LMConnector);
2486
                        break;
2487
                    }
2488
                    else if (_LMConnector.ConnectItem1SymbolObject != null &&
2489
                        connector.ConnectItem2SymbolObject != null &&
2490
                        _LMConnector.ConnectItem1SymbolObject.Id == connector.ConnectItem2SymbolObject.Id)
2491
                    {
2492
                        result = true;
2493
                        ReleaseCOMObjects(_LMConnector);
2494
                        break;
2495
                    }
2496
                    else if (_LMConnector.ConnectItem2SymbolObject != null &&
2497
                        connector.ConnectItem1SymbolObject != null &&
2498
                        _LMConnector.ConnectItem2SymbolObject.Id == connector.ConnectItem1SymbolObject.Id)
2499
                    {
2500
                        result = true;
2501
                        ReleaseCOMObjects(_LMConnector);
2502
                        break;
2503
                    }
2504
                    else if (_LMConnector.ConnectItem2SymbolObject != null &&
2505
                        connector.ConnectItem2SymbolObject != null &&
2506
                        _LMConnector.ConnectItem2SymbolObject.Id == connector.ConnectItem2SymbolObject.Id)
2507
                    {
2508
                        result = true;
2509
                        ReleaseCOMObjects(_LMConnector);
2510
                        break;
2511
                    }
2512

    
2513
                    ReleaseCOMObjects(_LMConnector);
2514
                }
2515
            }
2516

    
2517

    
2518
            return result;
2519
        }
2520

    
2521
        /// <summary>
2522
        /// FromModelItem을 ToModelItem으로 PipeRunJoin하는 메서드
2523
        /// </summary>
2524
        /// <param name="modelItemID1"></param>
2525
        /// <param name="modelItemID2"></param>
2526
        private void JoinRun(string modelId1, string modelId2, ref string survivorId)
2527
        {
2528
            try
2529
            {
2530
                LMModelItem modelItem1 = dataSource.GetModelItem(modelId1);
2531
                _LMAItem item1 = modelItem1.AsLMAItem();
2532
                LMModelItem modelItem2 = dataSource.GetModelItem(modelId2);
2533
                _LMAItem item2 = modelItem2.AsLMAItem();
2534

    
2535
                // item2가 item1으로 조인
2536
                _placement.PIDJoinRuns(ref item1, ref item2);
2537
                item1.Commit();
2538
                item2.Commit();
2539

    
2540
                string beforeID = string.Empty;
2541
                string afterID = string.Empty;
2542

    
2543
                if (modelItem1.get_ItemStatus() == "Active" && modelItem2.get_ItemStatus() != "Active")
2544
                {
2545
                    beforeID = modelItem2.Id;
2546
                    afterID = modelItem1.Id;
2547
                    survivorId = afterID;
2548
                }
2549
                else if (modelItem1.get_ItemStatus() != "Active" && modelItem2.get_ItemStatus() == "Active")
2550
                {
2551
                    beforeID = modelItem1.Id;
2552
                    afterID = modelItem2.Id;
2553
                    survivorId = afterID;
2554
                }
2555
                else if (modelItem1.get_ItemStatus() == "Active" && modelItem2.get_ItemStatus() == "Active")
2556
                    survivorId = null;
2557
                else
2558
                {
2559
                    Log.Write("잘못된 경우");
2560
                    survivorId = null;
2561
                }
2562

    
2563

    
2564
                if (!string.IsNullOrEmpty(beforeID) && !string.IsNullOrEmpty(afterID))
2565
                {
2566
                    List<Line> lines = SPPIDUtil.FindLinesByModelId(document, beforeID);
2567
                    foreach (var line in lines)
2568
                        line.SPPID.ModelItemId = afterID;
2569
                }
2570

    
2571
                ReleaseCOMObjects(modelItem1);
2572
                ReleaseCOMObjects(item1);
2573
                ReleaseCOMObjects(modelItem2);
2574
                ReleaseCOMObjects(item2);
2575
            }
2576
            catch (Exception ex)
2577
            {
2578
                Log.Write("Join Error");
2579
                Log.Write(ex.Message);
2580
            }
2581
        }
2582

    
2583
        private List<string> FindOtherModelItemBySymbolWhereTypePipeRun(LMSymbol symbol, string modelId)
2584
        {
2585
            List<string> modelItemIDs = new List<string>();
2586
            foreach (LMConnector connector in symbol.Connect1Connectors)
2587
            {
2588
                LMModelItem modelItem = connector.ModelItemObject;
2589
                if (modelItem.get_ItemStatus() == "Active" && modelItem.get_ItemTypeName().ToString() == "PipeRun" && modelItem.Id != modelId)
2590
                    modelItemIDs.Add(modelItem.Id);
2591
                ReleaseCOMObjects(modelItem);
2592
                ReleaseCOMObjects(connector);
2593
            }
2594

    
2595
            foreach (LMConnector connector in symbol.Connect2Connectors)
2596
            {
2597
                LMModelItem modelItem = connector.ModelItemObject;
2598
                if (modelItem.get_ItemStatus() == "Active" && modelItem.get_ItemTypeName().ToString() == "PipeRun" && modelItem.Id != modelId)
2599
                    modelItemIDs.Add(modelItem.Id);
2600
                ReleaseCOMObjects(modelItem);
2601
                ReleaseCOMObjects(connector);
2602
            }
2603

    
2604
            return modelItemIDs;
2605
        }
2606

    
2607
        /// <summary>
2608
        /// PipeRun의 좌표를 가져오는 메서드
2609
        /// </summary>
2610
        /// <param name="modelId"></param>
2611
        /// <returns></returns>
2612
        private Dictionary<LMConnector, List<double[]>> GetPipeRunVertices(string modelId)
2613
        {
2614
            Dictionary<LMConnector, List<double[]>> connectorVertices = new Dictionary<LMConnector, List<double[]>>();
2615
            LMModelItem modelItem = dataSource.GetModelItem(modelId);
2616

    
2617
            if (modelItem != null)
2618
            {
2619
                foreach (LMRepresentation rep in modelItem.Representations)
2620
                {
2621
                    if (rep.Attributes["RepresentationType"].get_Value() == "Connector" && rep.Attributes["ItemStatus"].get_Value() == "Active")
2622
                    {
2623
                        LMConnector _LMConnector = dataSource.GetConnector(rep.Id);
2624
                        connectorVertices.Add(_LMConnector, new List<double[]>());
2625
                        dynamic OID = rep.get_GraphicOID().ToString();
2626
                        DependencyObject drawingObject = radApp.ActiveDocument.ActiveSheet.DrawingObjects[OID];
2627
                        Ingr.RAD2D.LineStringGeometry2d lineStringGeometry = drawingObject.GetGeometry() as Ingr.RAD2D.LineStringGeometry2d;
2628
                        int verticesCount = lineStringGeometry.VertexCount;
2629
                        double[] vertices = null;
2630
                        lineStringGeometry.GetVertices(ref verticesCount, ref vertices);
2631
                        for (int i = 0; i < verticesCount; i++)
2632
                        {
2633
                            double x = 0;
2634
                            double y = 0;
2635
                            lineStringGeometry.GetVertex(i + 1, ref x, ref y);
2636
                            connectorVertices[_LMConnector].Add(new double[] { x, y });
2637
                        }
2638
                    }
2639
                }
2640

    
2641
                ReleaseCOMObjects(modelItem);
2642
            }
2643

    
2644
            return connectorVertices;
2645
        }
2646

    
2647
        private List<double[]> GetConnectorVertices(LMConnector connector)
2648
        {
2649
            List<double[]> vertices = new List<double[]>();
2650
            dynamic OID = connector.get_GraphicOID().ToString();
2651
            DependencyObject drawingObject = radApp.ActiveDocument.ActiveSheet.DrawingObjects[OID];
2652
            Ingr.RAD2D.LineStringGeometry2d lineStringGeometry = drawingObject.GetGeometry() as Ingr.RAD2D.LineStringGeometry2d;
2653
            int verticesCount = lineStringGeometry.VertexCount;
2654
            double[] value = null;
2655
            lineStringGeometry.GetVertices(ref verticesCount, ref value);
2656
            for (int i = 0; i < verticesCount; i++)
2657
            {
2658
                double x = 0;
2659
                double y = 0;
2660
                lineStringGeometry.GetVertex(i + 1, ref x, ref y);
2661
                vertices.Add(new double[] { x, y });
2662
            }
2663
            return vertices;
2664
        }
2665

    
2666
        /// <summary>
2667
        /// 좌표로 PipeRun의 Connector중에 어느 Connector에 가까운지/붙을지 가져오는 메서드 - 조건에 안맞아서 못찾을시 제일 가까운 점으로 가져오는 방식
2668
        /// </summary>
2669
        /// <param name="connectorVertices"></param>
2670
        /// <param name="connX"></param>
2671
        /// <param name="connY"></param>
2672
        /// <returns></returns>
2673
        private LMConnector FindTargetLMConnectorForLabel(Dictionary<LMConnector, List<double[]>> connectorVertices, double connX, double connY)
2674
        {
2675
            double length = double.MaxValue;
2676
            LMConnector targetConnector = null;
2677
            foreach (var item in connectorVertices)
2678
            {
2679
                List<double[]> points = item.Value;
2680
                for (int i = 0; i < points.Count - 1; i++)
2681
                {
2682
                    double[] point1 = points[i];
2683
                    double[] point2 = points[i + 1];
2684
                    double x1 = Math.Min(point1[0], point2[0]);
2685
                    double y1 = Math.Min(point1[1], point2[1]);
2686
                    double x2 = Math.Max(point1[0], point2[0]);
2687
                    double y2 = Math.Max(point1[1], point2[1]);
2688

    
2689
                    if ((x1 <= connX && x2 >= connX) ||
2690
                        (y1 <= connY && y2 >= connY))
2691
                    {
2692
                        double distance = SPPIDUtil.CalcPointToPointdDistance(point1[0], point1[1], connX, connY);
2693
                        if (length >= distance)
2694
                        {
2695
                            targetConnector = item.Key;
2696
                            length = distance;
2697
                        }
2698

    
2699
                        distance = SPPIDUtil.CalcPointToPointdDistance(point2[0], point2[1], connX, connY);
2700
                        if (length >= distance)
2701
                        {
2702
                            targetConnector = item.Key;
2703
                            length = distance;
2704
                        }
2705
                    }
2706
                }
2707
            }
2708

    
2709
            // 못찾았을때.
2710
            length = double.MaxValue;
2711
            if (targetConnector == null)
2712
            {
2713
                foreach (var item in connectorVertices)
2714
                {
2715
                    List<double[]> points = item.Value;
2716

    
2717
                    foreach (double[] point in points)
2718
                    {
2719
                        double distance = SPPIDUtil.CalcPointToPointdDistance(point[0], point[1], connX, connY);
2720
                        if (length >= distance)
2721
                        {
2722
                            targetConnector = item.Key;
2723
                            length = distance;
2724
                        }
2725
                    }
2726
                }
2727
            }
2728

    
2729
            return targetConnector;
2730
        }
2731

    
2732
        private LMConnector FindTargetLMConnectorForBranch(Line line, Line targetLine, ref double x, ref double y)
2733
        {
2734
            Dictionary<LMConnector, List<double[]>> vertices = GetPipeRunVertices(targetLine.SPPID.ModelItemId);
2735
            if (vertices.Count == 0)
2736
                return null;
2737

    
2738
            double length = double.MaxValue;
2739
            LMConnector targetConnector = null;
2740
            double[] resultPoint = null;
2741
            List<double[]> targetVertices = null;
2742

    
2743
            // Vertices 포인트에 제일 가까운곳
2744
            foreach (var item in vertices)
2745
            {
2746
                List<double[]> points = item.Value;
2747
                for (int i = 0; i < points.Count; i++)
2748
                {
2749
                    double[] point = points[i];
2750
                    double tempX = point[0];
2751
                    double tempY = point[1];
2752

    
2753
                    double distance = SPPIDUtil.CalcPointToPointdDistance(tempX, tempY, x, y);
2754
                    if (length >= distance)
2755
                    {
2756
                        targetConnector = item.Key;
2757
                        length = distance;
2758
                        resultPoint = point;
2759
                        targetVertices = item.Value;
2760
                    }
2761
                }
2762
            }
2763

    
2764
            // Vertices Cross에 제일 가까운곳
2765
            foreach (var item in vertices)
2766
            {
2767
                List<double[]> points = item.Value;
2768
                for (int i = 0; i < points.Count - 1; i++)
2769
                {
2770
                    double[] point1 = points[i];
2771
                    double[] point2 = points[i + 1];
2772

    
2773
                    double maxLineX = Math.Max(point1[0], point2[0]);
2774
                    double minLineX = Math.Min(point1[0], point2[0]);
2775
                    double maxLineY = Math.Max(point1[1], point2[1]);
2776
                    double minLineY = Math.Min(point1[1], point2[1]);
2777

    
2778
                    SlopeType slope = SPPIDUtil.CalcSlope(minLineX, minLineY, maxLineX, maxLineY);
2779

    
2780
                    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]);
2781
                    if (crossingPoint != null)
2782
                    {
2783
                        double distance = SPPIDUtil.CalcPointToPointdDistance(crossingPoint[0], crossingPoint[1], x, y);
2784
                        if (length >= distance)
2785
                        {
2786
                            if (slope == SlopeType.Slope &&
2787
                                minLineX <= crossingPoint[0] && maxLineX >= crossingPoint[0] &&
2788
                                minLineY <= crossingPoint[1] && maxLineY >= crossingPoint[1])
2789
                            {
2790
                                targetConnector = item.Key;
2791
                                length = distance;
2792
                                resultPoint = crossingPoint;
2793
                                targetVertices = item.Value;
2794
                            }
2795
                            else if (slope == SlopeType.HORIZONTAL &&
2796
                                minLineX <= crossingPoint[0] && maxLineX >= crossingPoint[0])
2797
                            {
2798
                                targetConnector = item.Key;
2799
                                length = distance;
2800
                                resultPoint = crossingPoint;
2801
                                targetVertices = item.Value;
2802
                            }
2803
                            else if (slope == SlopeType.VERTICAL &&
2804
                               minLineY <= crossingPoint[1] && maxLineY >= crossingPoint[1])
2805
                            {
2806
                                targetConnector = item.Key;
2807
                                length = distance;
2808
                                resultPoint = crossingPoint;
2809
                                targetVertices = item.Value;
2810
                            }
2811
                        }
2812
                    }
2813
                }
2814
            }
2815

    
2816
            foreach (var item in vertices)
2817
                if (item.Key != null && item.Key != targetConnector)
2818
                    ReleaseCOMObjects(item.Key);
2819

    
2820
            if (SPPIDUtil.IsBranchLine(line, targetLine))
2821
            {
2822
                double tempResultX = resultPoint[0];
2823
                double tempResultY = resultPoint[1];
2824
                SPPIDUtil.ConvertGridPoint(ref tempResultX, ref tempResultY);
2825

    
2826
                GridSetting gridSetting = GridSetting.GetInstance();
2827

    
2828
                for (int i = 0; i < targetVertices.Count; i++)
2829
                {
2830
                    double[] point = targetVertices[i];
2831
                    double tempX = targetVertices[i][0];
2832
                    double tempY = targetVertices[i][1];
2833
                    SPPIDUtil.ConvertGridPoint(ref tempX, ref tempY);
2834
                    if (tempX == tempResultX && tempY == tempResultY)
2835
                    {
2836
                        if (i == 0)
2837
                        {
2838
                            LMSymbol connSymbol = targetConnector.ConnectItem1SymbolObject;
2839
                            bool containZeroLength = false;
2840
                            if (connSymbol != null)
2841
                            {
2842
                                foreach (LMConnector connector in connSymbol.Connect1Connectors)
2843
                                {
2844
                                    if (connector.get_ItemStatus() == "Active" && Convert.ToBoolean(connector.get_IsZeroLength()) == true)
2845
                                        containZeroLength = true;
2846
                                }
2847
                                foreach (LMConnector connector in connSymbol.Connect2Connectors)
2848
                                {
2849
                                    if (connector.get_ItemStatus() == "Active" && Convert.ToBoolean(connector.get_IsZeroLength()) == true)
2850
                                        containZeroLength = true;
2851
                                }
2852
                            }
2853

    
2854
                            if (connSymbol == null ||
2855
                                (connSymbol != null && connSymbol.get_ItemStatus() == "Active" && connSymbol.get_RepresentationType() != "Branch") ||
2856
                                containZeroLength)
2857
                            {
2858
                                bool bCalcX = false;
2859
                                bool bCalcY = false;
2860
                                if (targetLine.SlopeType == SlopeType.HORIZONTAL)
2861
                                    bCalcX = true;
2862
                                else if (targetLine.SlopeType == SlopeType.VERTICAL)
2863
                                    bCalcY = true;
2864
                                else
2865
                                {
2866
                                    bCalcX = true;
2867
                                    bCalcY = true;
2868
                                }
2869

    
2870
                                if (bCalcX)
2871
                                {
2872
                                    double nextX = targetVertices[i + 1][0];
2873
                                    double newX = 0;
2874
                                    if (nextX > tempX)
2875
                                    {
2876
                                        newX = tempX + gridSetting.Length;
2877
                                        if (newX > nextX)
2878
                                            newX = (point[0] + nextX) / 2;
2879
                                    }
2880
                                    else
2881
                                    {
2882
                                        newX = tempX - gridSetting.Length;
2883
                                        if (newX < nextX)
2884
                                            newX = (point[0] + nextX) / 2;
2885
                                    }
2886
                                    resultPoint = new double[] { newX, resultPoint[1] };
2887
                                }
2888

    
2889
                                if (bCalcY)
2890
                                {
2891
                                    double nextY = targetVertices[i + 1][1];
2892
                                    double newY = 0;
2893
                                    if (nextY > tempY)
2894
                                    {
2895
                                        newY = tempY + gridSetting.Length;
2896
                                        if (newY > nextY)
2897
                                            newY = (point[1] + nextY) / 2;
2898
                                    }
2899
                                    else
2900
                                    {
2901
                                        newY = tempY - gridSetting.Length;
2902
                                        if (newY < nextY)
2903
                                            newY = (point[1] + nextY) / 2;
2904
                                    }
2905
                                    resultPoint = new double[] { resultPoint[0], newY };
2906
                                }
2907
                            }
2908
                        }
2909
                        else if (i == targetVertices.Count - 1)
2910
                        {
2911
                            LMSymbol connSymbol = targetConnector.ConnectItem2SymbolObject;
2912
                            bool containZeroLength = false;
2913
                            if (connSymbol != null)
2914
                            {
2915
                                foreach (LMConnector connector in connSymbol.Connect1Connectors)
2916
                                {
2917
                                    if (connector.get_ItemStatus() == "Active" && Convert.ToBoolean(connector.get_IsZeroLength()) == true)
2918
                                        containZeroLength = true;
2919
                                }
2920
                                foreach (LMConnector connector in connSymbol.Connect2Connectors)
2921
                                {
2922
                                    if (connector.get_ItemStatus() == "Active" && Convert.ToBoolean(connector.get_IsZeroLength()) == true)
2923
                                        containZeroLength = true;
2924
                                }
2925
                            }
2926

    
2927
                            if (connSymbol == null ||
2928
                                 (connSymbol != null && connSymbol.get_ItemStatus() == "Active" && connSymbol.get_RepresentationType() != "Branch") ||
2929
                                containZeroLength)
2930
                            {
2931
                                bool bCalcX = false;
2932
                                bool bCalcY = false;
2933
                                if (targetLine.SlopeType == SlopeType.HORIZONTAL)
2934
                                    bCalcX = true;
2935
                                else if (targetLine.SlopeType == SlopeType.VERTICAL)
2936
                                    bCalcY = true;
2937
                                else
2938
                                {
2939
                                    bCalcX = true;
2940
                                    bCalcY = true;
2941
                                }
2942

    
2943
                                if (bCalcX)
2944
                                {
2945
                                    double nextX = targetVertices[i - 1][0];
2946
                                    double newX = 0;
2947
                                    if (nextX > tempX)
2948
                                    {
2949
                                        newX = tempX + gridSetting.Length;
2950
                                        if (newX > nextX)
2951
                                            newX = (point[0] + nextX) / 2;
2952
                                    }
2953
                                    else
2954
                                    {
2955
                                        newX = tempX - gridSetting.Length;
2956
                                        if (newX < nextX)
2957
                                            newX = (point[0] + nextX) / 2;
2958
                                    }
2959
                                    resultPoint = new double[] { newX, resultPoint[1] };
2960
                                }
2961

    
2962
                                if (bCalcY)
2963
                                {
2964
                                    double nextY = targetVertices[i - 1][1];
2965
                                    double newY = 0;
2966
                                    if (nextY > tempY)
2967
                                    {
2968
                                        newY = tempY + gridSetting.Length;
2969
                                        if (newY > nextY)
2970
                                            newY = (point[1] + nextY) / 2;
2971
                                    }
2972
                                    else
2973
                                    {
2974
                                        newY = tempY - gridSetting.Length;
2975
                                        if (newY < nextY)
2976
                                            newY = (point[1] + nextY) / 2;
2977
                                    }
2978
                                    resultPoint = new double[] { resultPoint[0], newY };
2979
                                }
2980
                            }
2981
                        }
2982
                        break;
2983
                    }
2984
                }
2985
            }
2986

    
2987
            x = resultPoint[0];
2988
            y = resultPoint[1];
2989

    
2990
            return targetConnector;
2991
        }
2992

    
2993
        private LMConnector GetLMConnectorOnlyOne(string modelItemID)
2994
        {
2995
            LMConnector result = null;
2996
            List<LMConnector> connectors = new List<LMConnector>();
2997
            LMModelItem modelItem = dataSource.GetModelItem(modelItemID);
2998

    
2999
            if (modelItem != null)
3000
            {
3001
                foreach (LMRepresentation rep in modelItem.Representations)
3002
                {
3003
                    if (rep.Attributes["RepresentationType"].get_Value() == "Connector" && rep.Attributes["ItemStatus"].get_Value() == "Active")
3004
                        connectors.Add(dataSource.GetConnector(rep.Id));
3005
                }
3006

    
3007
                ReleaseCOMObjects(modelItem);
3008
            }
3009

    
3010
            if (connectors.Count == 1)
3011
                result = connectors[0];
3012
            else
3013
                foreach (var item in connectors)
3014
                    ReleaseCOMObjects(item);
3015

    
3016
            return result;
3017
        }
3018

    
3019
        /// <summary>
3020
        /// Line Number Symbol을 실제로 Modeling하는 메서드
3021
        /// </summary>
3022
        /// <param name="lineNumber"></param>
3023
        private void LineNumberModeling(Line line)
3024
        {
3025
            LineNumber lineNumber = document.LINENUMBERS.Find(x => x.CONNLINE == line.UID);
3026
            if (lineNumber != null)
3027
            {
3028
                LMConnector connectedLMConnector = GetLMConnectorOnlyOne(line.SPPID.ModelItemId);
3029
                if (connectedLMConnector != null)
3030
                {
3031
                    double x = 0;
3032
                    double y = 0;
3033
                    CalcLabelLocation(ref x, ref y, lineNumber.SPPID.ORIGINAL_X, lineNumber.SPPID.ORIGINAL_Y, lineNumber.SPPIDLabelLocation, _ETCSetting.LineNumberLocation);
3034

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

    
3038
                    if (_LmLabelPresist != null)
3039
                    {
3040
                        _LmLabelPresist.Commit();
3041
                        lineNumber.SPPID.RepresentationId = _LmLabelPresist.AsLMRepresentation().Id;
3042
                        ReleaseCOMObjects(_LmLabelPresist);
3043
                    }
3044
                }
3045
            }
3046
        }
3047

    
3048
        /// <summary>
3049
        /// Flow Mark Modeling
3050
        /// </summary>
3051
        /// <param name="line"></param>
3052
        private void FlowMarkModeling(Line line)
3053
        {
3054
            if (line.FLOWMARK && !string.IsNullOrEmpty(line.SPPID.ModelItemId) && !string.IsNullOrEmpty(_ETCSetting.FlowMarkSymbolPath))
3055
            {
3056
                LMConnector connector = GetLMConnectorOnlyOne(line.SPPID.ModelItemId);
3057
                if (connector != null)
3058
                {
3059
                    string mappingPath = _ETCSetting.FlowMarkSymbolPath;
3060
                    List<double[]> vertices = GetConnectorVertices(connector);
3061
                    vertices = vertices.FindAll(x => x[0] > 0 && x[1] > 0);
3062
                    double[] point = vertices[vertices.Count - 1];
3063
                    Array array = new double[] { 0, point[0], point[1] };
3064
                    LMLabelPersist _LMLabelPersist = _placement.PIDPlaceLabel(mappingPath, ref array, LabeledItem: connector.AsLMRepresentation());
3065
                    if (_LMLabelPersist != null)
3066
                        ReleaseCOMObjects(_LMLabelPersist);
3067
                }
3068
            }
3069
        }
3070

    
3071
        /// <summary>
3072
        /// Line Number 기준으로 모든 Item에 Line Number의 Attribute Input
3073
        /// </summary>
3074
        /// <param name="lineNumber"></param>
3075
        private void InputLineNumberAttribute(LineNumber lineNumber)
3076
        {
3077
            foreach (LineRun run in lineNumber.RUNS)
3078
            {
3079
                foreach (var item in run.RUNITEMS)
3080
                {
3081
                    //if (item.GetType() == typeof(Symbol))
3082
                    //{
3083
                    //    Symbol symbol = item as Symbol;
3084
                    //    LMSymbol _LMSymbol = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
3085
                    //    if (_LMSymbol != null)
3086
                    //    {
3087
                    //        LMModelItem _LMModelItem = _LMSymbol.ModelItemObject;
3088

    
3089
                    //        if (_LMModelItem != null && _LMModelItem.get_ItemStatus() == "Active")
3090
                    //        {
3091
                    //            foreach (var attribute in lineNumber.ATTRIBUTES)
3092
                    //            {
3093
                    //                LineNumberMapping mapping = document.LineNumberMappings.Find(x => x.UID == attribute.UID);
3094
                    //                if (mapping != null && !string.IsNullOrEmpty(attribute.VALUE) && attribute.VALUE != "None")
3095
                    //                {
3096
                    //                    LMAAttribute _LMAAttribute = _LMModelItem.Attributes[mapping.SPPIDATTRIBUTENAME];
3097
                    //                    if (_LMAAttribute != null)
3098
                    //                    {
3099
                    //                        if (DBNull.Value.Equals(_LMAAttribute.get_Value()))
3100
                    //                            _LMAAttribute.set_Value(attribute.VALUE);
3101
                    //                        else if (_LMAAttribute.get_Value() != attribute.VALUE)
3102
                    //                            _LMAAttribute.set_Value(attribute.VALUE);
3103
                    //                    }
3104
                    //                }
3105
                    //            }
3106
                    //            _LMModelItem.Commit();
3107
                    //        }
3108
                    //        if (_LMModelItem != null)
3109
                    //            ReleaseCOMObjects(_LMModelItem);
3110
                    //    }
3111
                    //    if (_LMSymbol != null)
3112
                    //        ReleaseCOMObjects(_LMSymbol);
3113
                    //}
3114
                    //else
3115
                    if (item.GetType() == typeof(Line))
3116
                    {
3117
                        Line line = item as Line;
3118
                        if (line != null)
3119
                        {
3120
                            LMModelItem _LMModelItem = dataSource.GetModelItem(line.SPPID.ModelItemId);
3121
                            if (_LMModelItem != null && _LMModelItem.get_ItemStatus() == "Active")
3122
                            {
3123
                                foreach (var attribute in lineNumber.ATTRIBUTES)
3124
                                {
3125
                                    LineNumberMapping mapping = document.LineNumberMappings.Find(x => x.UID == attribute.UID);
3126
                                    if (mapping != null && !string.IsNullOrEmpty(attribute.VALUE) && attribute.VALUE != "None")
3127
                                    {
3128
                                        LMAAttribute _LMAAttribute = _LMModelItem.Attributes[mapping.SPPIDATTRIBUTENAME];
3129
                                        if (_LMAAttribute != null)
3130
                                        {
3131
                                            if (DBNull.Value.Equals(_LMAAttribute.get_Value()))
3132
                                                _LMAAttribute.set_Value(attribute.VALUE);
3133
                                            else if (_LMAAttribute.get_Value() != attribute.VALUE)
3134
                                                _LMAAttribute.set_Value(attribute.VALUE);
3135

    
3136
                                        }
3137
                                    }
3138
                                }
3139
                                _LMModelItem.Commit();
3140
                            }
3141
                            if (_LMModelItem != null)
3142
                                ReleaseCOMObjects(_LMModelItem);
3143

    
3144
                            break;
3145
                        }
3146
                    }
3147
                }
3148
            }
3149
        }
3150

    
3151
        /// <summary>
3152
        /// Symbol Attribute 입력 메서드
3153
        /// </summary>
3154
        /// <param name="item"></param>
3155
        private void InputSymbolAttribute(object targetItem, List<BaseModel.Attribute> targetAttributes)
3156
        {
3157
            // Object 아이템이 Symbol일 경우 Equipment일 경우 
3158
            string sRep = null;
3159
            if (targetItem.GetType() == typeof(Symbol))
3160
                sRep = ((Symbol)targetItem).SPPID.RepresentationId;
3161
            else if (targetItem.GetType() == typeof(Equipment))
3162
                sRep = ((Equipment)targetItem).SPPID.RepresentationId;
3163

    
3164
            if (!string.IsNullOrEmpty(sRep))
3165
            {
3166
                LMSymbol _LMSymbol = dataSource.GetSymbol(sRep);
3167
                LMModelItem _LMModelItem = _LMSymbol.ModelItemObject;
3168
                LMAAttributes _Attributes = _LMModelItem.Attributes;
3169
                
3170
                foreach (var item in targetAttributes)
3171
                {
3172
                    AttributeMapping mapping = document.AttributeMappings.Find(x => x.UID == item.UID);
3173
                    if (mapping != null && !string.IsNullOrEmpty(item.VALUE) && item.VALUE != "None")
3174
                    {
3175
                        LMAAttribute _Attribute = _Attributes[mapping.SPPIDATTRIBUTENAME];
3176
                        if (_Attribute != null)
3177
                        {
3178
                            object associItem = SPPIDUtil.FindObjectByUID(document, item.ASSOCITEM);
3179
                            if (associItem != null)
3180
                            {
3181
                                if (associItem.GetType() == typeof(Text))
3182
                                {
3183
                                    Text text = associItem as Text;
3184
                                    text.SPPID.RepresentationId = "Attribute";
3185
                                }
3186
                                else if (associItem.GetType() == typeof(Note))
3187
                                {
3188
                                    Note note = associItem as Note;
3189
                                    note.SPPID.RepresentationId = "Attribute";
3190
                                }
3191
                            }
3192
                            _Attribute.set_Value(item.VALUE);
3193
                        }
3194
                            
3195
                    }
3196
                }
3197
                _LMModelItem.Commit();
3198

    
3199
                ReleaseCOMObjects(_Attributes);
3200
                ReleaseCOMObjects(_LMModelItem);
3201
                ReleaseCOMObjects(_LMSymbol);
3202
            }
3203
        }
3204

    
3205
        /// <summary>
3206
        /// Input SpecBreak Attribute
3207
        /// </summary>
3208
        /// <param name="specBreak"></param>
3209
        private void InputSpecBreakAttribute(SpecBreak specBreak)
3210
        {
3211
            object upStreamObj = SPPIDUtil.FindObjectByUID(document, specBreak.UpStreamUID);
3212
            object downStreamObj = SPPIDUtil.FindObjectByUID(document, specBreak.DownStreamUID);
3213

    
3214
            if (upStreamObj != null &&
3215
                downStreamObj != null)
3216
            {
3217
                LMConnector targetLMConnector = FindBreakLineTarget(upStreamObj, downStreamObj);
3218

    
3219
                if (targetLMConnector != null)
3220
                {
3221
                    foreach (LMLabelPersist _LMLabelPersist in targetLMConnector.LabelPersists)
3222
                    {
3223
                        string symbolPath = _LMLabelPersist.get_FileName();
3224
                        AttributeMapping mapping = document.AttributeMappings.Find(x => x.SPPIDSYMBOLNAME == symbolPath);
3225
                        if (mapping != null)
3226
                        {
3227
                            BaseModel.Attribute attribute = specBreak.ATTRIBUTES.Find(y => y.UID == mapping.UID);
3228
                            if (attribute != null && !string.IsNullOrEmpty(attribute.VALUE) && attribute.VALUE != "None")
3229
                            {
3230
                                string[] values = attribute.VALUE.Split(new char[] { ',' });
3231
                                if (values.Length == 2)
3232
                                {
3233
                                    string upStreamValue = values[0];
3234
                                    string downStreamValue = values[1];
3235

    
3236
                                    InputAttributeForSpecBreak(upStreamObj, downStreamObj, upStreamValue, downStreamValue, mapping.SPPIDATTRIBUTENAME);
3237
                                }
3238
                            }
3239
                        }
3240
                    }
3241

    
3242
                    ReleaseCOMObjects(targetLMConnector);
3243
                }
3244
            }
3245

    
3246

    
3247
            #region 내부에서만 쓰는 메서드
3248
            void InputAttributeForSpecBreak(object _upStreamObj, object _downStreamObj, string upStreamValue, string downStreamValue, string sppidAttributeName)
3249
            {
3250
                Symbol upStreamSymbol = _upStreamObj as Symbol;
3251
                Line upStreamLine = _upStreamObj as Line;
3252
                Symbol downStreamSymbol = _downStreamObj as Symbol;
3253
                Line downStreamLine = _downStreamObj as Line;
3254
                // 둘다 Line일 경우
3255
                if (upStreamLine != null && downStreamLine != null)
3256
                {
3257
                    InputLineAttributeForSpecBreakLine(upStreamLine, sppidAttributeName, upStreamValue);
3258
                    InputLineAttributeForSpecBreakLine(downStreamLine, sppidAttributeName, downStreamValue);
3259
                }
3260
                // 둘다 Symbol일 경우
3261
                else if (upStreamSymbol != null && downStreamSymbol != null)
3262
                {
3263
                    LMConnector zeroLenthConnector = FindBreakLineTarget(upStreamSymbol, downStreamSymbol);
3264
                    LMSymbol upStreamLMSymbol = dataSource.GetSymbol(upStreamSymbol.SPPID.RepresentationId);
3265
                    LMSymbol downStreamLMSymbol = dataSource.GetSymbol(downStreamSymbol.SPPID.RepresentationId);
3266

    
3267
                    foreach (LMConnector connector in upStreamLMSymbol.Avoid1Connectors)
3268
                    {
3269
                        if (connector.get_ItemStatus() != "Active")
3270
                            continue;
3271

    
3272
                        if (connector.Id != zeroLenthConnector.Id)
3273
                            InputLineAttributeForSpecBreakLMConnector(connector, sppidAttributeName, upStreamValue);
3274
                    }
3275

    
3276
                    foreach (LMConnector connector in upStreamLMSymbol.Avoid2Connectors)
3277
                    {
3278
                        if (connector.get_ItemStatus() != "Active")
3279
                            continue;
3280

    
3281
                        if (connector.Id != zeroLenthConnector.Id)
3282
                            InputLineAttributeForSpecBreakLMConnector(connector, sppidAttributeName, upStreamValue);
3283
                    }
3284

    
3285
                    foreach (LMConnector connector in downStreamLMSymbol.Avoid1Connectors)
3286
                    {
3287
                        if (connector.get_ItemStatus() != "Active")
3288
                            continue;
3289

    
3290
                        if (connector.Id != zeroLenthConnector.Id)
3291
                            InputLineAttributeForSpecBreakLMConnector(connector, sppidAttributeName, downStreamValue);
3292
                    }
3293

    
3294
                    foreach (LMConnector connector in downStreamLMSymbol.Avoid2Connectors)
3295
                    {
3296
                        if (connector.get_ItemStatus() != "Active")
3297
                            continue;
3298

    
3299
                        if (connector.Id != zeroLenthConnector.Id)
3300
                            InputLineAttributeForSpecBreakLMConnector(connector, sppidAttributeName, downStreamValue);
3301
                    }
3302

    
3303
                    ReleaseCOMObjects(zeroLenthConnector);
3304
                    ReleaseCOMObjects(upStreamLMSymbol);
3305
                    ReleaseCOMObjects(downStreamLMSymbol);
3306
                }
3307
                else if (upStreamSymbol != null && downStreamLine != null)
3308
                {
3309
                    LMConnector zeroLenthConnector = FindBreakLineTarget(upStreamSymbol, downStreamLine);
3310
                    InputLineAttributeForSpecBreakLine(downStreamLine, sppidAttributeName, downStreamValue);
3311
                    LMSymbol upStreamLMSymbol = dataSource.GetSymbol(upStreamSymbol.SPPID.RepresentationId);
3312

    
3313
                    foreach (LMConnector connector in upStreamLMSymbol.Avoid1Connectors)
3314
                    {
3315
                        if (connector.get_ItemStatus() != "Active")
3316
                            continue;
3317

    
3318
                        if (connector.Id != zeroLenthConnector.Id)
3319
                            InputLineAttributeForSpecBreakLMConnector(connector, sppidAttributeName, upStreamValue);
3320
                    }
3321

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

    
3327
                        if (connector.Id != zeroLenthConnector.Id)
3328
                            InputLineAttributeForSpecBreakLMConnector(connector, sppidAttributeName, upStreamValue);
3329
                    }
3330

    
3331
                    ReleaseCOMObjects(zeroLenthConnector);
3332
                    ReleaseCOMObjects(upStreamLMSymbol);
3333
                }
3334
                else if (upStreamLine != null && downStreamSymbol != null)
3335
                {
3336
                    LMConnector zeroLenthConnector = FindBreakLineTarget(upStreamLine, downStreamSymbol);
3337
                    InputLineAttributeForSpecBreakLine(upStreamLine, sppidAttributeName, upStreamValue);
3338
                    LMSymbol downStreamLMSymbol = dataSource.GetSymbol(downStreamSymbol.SPPID.RepresentationId);
3339

    
3340
                    foreach (LMConnector connector in downStreamLMSymbol.Avoid1Connectors)
3341
                    {
3342
                        if (connector.get_ItemStatus() != "Active")
3343
                            continue;
3344

    
3345
                        if (connector.Id != zeroLenthConnector.Id)
3346
                            InputLineAttributeForSpecBreakLMConnector(connector, sppidAttributeName, downStreamValue);
3347
                    }
3348

    
3349
                    foreach (LMConnector connector in downStreamLMSymbol.Avoid2Connectors)
3350
                    {
3351
                        if (connector.get_ItemStatus() != "Active")
3352
                            continue;
3353

    
3354
                        if (connector.Id != zeroLenthConnector.Id)
3355
                            InputLineAttributeForSpecBreakLMConnector(connector, sppidAttributeName, downStreamValue);
3356
                    }
3357

    
3358
                    ReleaseCOMObjects(zeroLenthConnector);
3359
                    ReleaseCOMObjects(downStreamLMSymbol);
3360
                }
3361
            }
3362

    
3363
            void InputLineAttributeForSpecBreakLine(Line line, string attrName, string value)
3364
            {
3365
                LMModelItem _LMModelItem = dataSource.GetModelItem(line.SPPID.ModelItemId);
3366
                if (_LMModelItem != null && _LMModelItem.get_ItemStatus() == "Active")
3367
                {
3368
                    LMAAttribute _LMAAttribute = _LMModelItem.Attributes[attrName];
3369
                    if (_LMAAttribute != null)
3370
                    {
3371
                        if (DBNull.Value.Equals(_LMAAttribute.get_Value()))
3372
                            _LMAAttribute.set_Value(value);
3373
                        else if (_LMAAttribute.get_Value() != value)
3374
                            _LMAAttribute.set_Value(value);
3375
                    }
3376

    
3377
                    _LMModelItem.Commit();
3378
                }
3379
                if (_LMModelItem != null)
3380
                    ReleaseCOMObjects(_LMModelItem);
3381
            }
3382

    
3383
            void InputLineAttributeForSpecBreakLMConnector(LMConnector connector, string attrName, string value)
3384
            {
3385
                LMModelItem _LMModelItem = dataSource.GetModelItem(connector.ModelItemID);
3386
                if (_LMModelItem != null && _LMModelItem.get_ItemStatus() == "Active")
3387
                {
3388
                    LMAAttribute _LMAAttribute = _LMModelItem.Attributes[attrName];
3389
                    if (_LMAAttribute != null)
3390
                    {
3391
                        if (DBNull.Value.Equals(_LMAAttribute.get_Value()))
3392
                            _LMAAttribute.set_Value(value);
3393
                        else if (_LMAAttribute.get_Value() != value)
3394
                            _LMAAttribute.set_Value(value);
3395
                    }
3396

    
3397
                    _LMModelItem.Commit();
3398
                }
3399
                if (_LMModelItem != null)
3400
                    ReleaseCOMObjects(_LMModelItem);
3401
            }
3402
            #endregion
3403
        }
3404

    
3405
        /// <summary>
3406
        /// Text Modeling - Association일 경우는 Text대신 해당 맵핑된 Symbol로 모델링
3407
        /// </summary>
3408
        /// <param name="text"></param>
3409
        private void TextModeling(Text text)
3410
        {
3411
            LMSymbol _LMSymbol = null;
3412
            LMConnector connectedLMConnector = null;
3413
            //if (text.ASSOCIATION && !string.IsNullOrEmpty(text.OWNER) && text.OWNER != "None")
3414
            if (text.ASSOCIATION)
3415
            {
3416
                object owner = SPPIDUtil.FindObjectByUID(document, text.OWNER);
3417
                if (owner != null && owner.GetType() == typeof(Symbol))
3418
                {
3419
                    Symbol symbol = owner as Symbol;
3420
                    _LMSymbol = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
3421
                    if (_LMSymbol != null)
3422
                    {
3423
                        BaseModel.Attribute attribute = symbol.ATTRIBUTES.Find(x => x.ASSOCITEM == text.UID);
3424
                        if (attribute != null && !string.IsNullOrEmpty(attribute.VALUE) && attribute.VALUE != "None")
3425
                        {
3426
                            AttributeMapping mapping = document.AttributeMappings.Find(x => x.UID == attribute.UID && !string.IsNullOrEmpty(x.SPPIDSYMBOLNAME));
3427

    
3428
                            if (mapping != null)
3429
                            {
3430
                                double x = 0;
3431
                                double y = 0;
3432

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

    
3436
                                LMLabelPersist _LMLabelPersist = _placement.PIDPlaceLabel(mapping.SPPIDSYMBOLNAME, ref array, Rotation: text.ANGLE, LabeledItem: _LMSymbol.AsLMRepresentation(), IsLeaderVisible: mapping.LeaderLine);
3437
                                if (_LMLabelPersist != null)
3438
                                {
3439
                                    text.SPPID.RepresentationId = _LMLabelPersist.AsLMRepresentation().Id;
3440
                                    _LMLabelPersist.Commit();
3441
                                    ReleaseCOMObjects(_LMLabelPersist);
3442
                                }
3443
                            }
3444
                        }
3445
                    }
3446
                }
3447
                else if (owner != null && owner.GetType() == typeof(Line))
3448
                {
3449
                    Line line = owner as Line;
3450
                    Dictionary<LMConnector, List<double[]>> connectorVertices = GetPipeRunVertices(line.SPPID.ModelItemId);
3451
                    connectedLMConnector = FindTargetLMConnectorForLabel(connectorVertices, text.SPPID.ORIGINAL_X, text.SPPID.ORIGINAL_Y);
3452

    
3453
                    if (connectedLMConnector != null)
3454
                    {
3455
                        BaseModel.Attribute attribute = line.ATTRIBUTES.Find(x => x.ASSOCITEM == text.UID);
3456
                        if (attribute != null && !string.IsNullOrEmpty(attribute.VALUE) && attribute.VALUE != "None")
3457
                        {
3458
                            AttributeMapping mapping = document.AttributeMappings.Find(x => x.UID == attribute.UID && !string.IsNullOrEmpty(x.SPPIDSYMBOLNAME));
3459

    
3460
                            if (mapping != null)
3461
                            {
3462
                                double x = 0;
3463
                                double y = 0;
3464

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

    
3468
                                LMLabelPersist _LMLabelPersist = _placement.PIDPlaceLabel(mapping.SPPIDSYMBOLNAME, ref array, Rotation: text.ANGLE, LabeledItem: connectedLMConnector.AsLMRepresentation(), IsLeaderVisible: mapping.LeaderLine);
3469
                                if (_LMLabelPersist != null)
3470
                                {
3471
                                    text.SPPID.RepresentationId = _LMLabelPersist.AsLMRepresentation().Id;
3472
                                    _LMLabelPersist.Commit();
3473
                                    ReleaseCOMObjects(_LMLabelPersist);
3474
                                }
3475
                            }
3476
                        }
3477
                    }
3478
                }
3479
            }
3480
            else
3481
            {
3482
                LMItemNote _LMItemNote = null;
3483
                LMAAttribute _LMAAttribute = null;
3484

    
3485
                double x = 0;
3486
                double y = 0;
3487
                double angle = text.ANGLE;
3488
                CalcLabelLocation(ref x, ref y, text.SPPID.ORIGINAL_X, text.SPPID.ORIGINAL_Y, text.SPPIDLabelLocation, _ETCSetting.TextLocation);
3489

    
3490
                _LMSymbol = _placement.PIDPlaceSymbol(text.SPPID.MAPPINGNAME, x, y, Rotation: angle);
3491
                if (_LMSymbol != null)
3492
                {
3493
                    _LMSymbol.Commit();
3494
                    _LMItemNote = _placement.PIDDataSource.GetItemNote(_LMSymbol.ModelItemID);
3495
                    if (_LMItemNote != null)
3496
                    {
3497
                        _LMItemNote.Commit();
3498
                        _LMAAttribute = _LMItemNote.Attributes["Note.Body"];
3499
                        if (_LMAAttribute != null)
3500
                        {
3501
                            _LMAAttribute.set_Value(text.VALUE);
3502
                            text.SPPID.RepresentationId = _LMSymbol.AsLMRepresentation().Id;
3503
                            _LMItemNote.Commit();
3504

    
3505
                            if (_LMAAttribute != null)
3506
                                ReleaseCOMObjects(_LMAAttribute);
3507
                            if (_LMItemNote != null)
3508
                                ReleaseCOMObjects(_LMItemNote);
3509
                        }
3510
                    }
3511
                }
3512
            }
3513
            if (_LMSymbol != null)
3514
                ReleaseCOMObjects(_LMSymbol);
3515
        }
3516

    
3517
        /// <summary>
3518
        /// Note Modeling
3519
        /// </summary>
3520
        /// <param name="note"></param>
3521
        private void NoteModeling(Note note)
3522
        {
3523
            LMSymbol _LMSymbol = null;
3524
            LMItemNote _LMItemNote = null;
3525
            LMAAttribute _LMAAttribute = null;
3526

    
3527
            if (string.IsNullOrEmpty(note.OWNER) || note.OWNER == "None")
3528
            {
3529
                double x = 0;
3530
                double y = 0;
3531

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

    
3534
                _LMSymbol = _placement.PIDPlaceSymbol(note.SPPID.MAPPINGNAME, x, y);
3535
                if (_LMSymbol != null)
3536
                {
3537
                    _LMSymbol.Commit();
3538
                    _LMItemNote = _placement.PIDDataSource.GetItemNote(_LMSymbol.ModelItemID);
3539
                    if (_LMItemNote != null)
3540
                    {
3541
                        _LMItemNote.Commit();
3542
                        _LMAAttribute = _LMItemNote.Attributes["Note.Body"];
3543
                        if (_LMAAttribute != null)
3544
                        {
3545
                            _LMAAttribute.set_Value(note.VALUE);
3546
                            note.SPPID.RepresentationId = _LMSymbol.AsLMRepresentation().Id;
3547
                            _LMItemNote.Commit();
3548
                        }
3549
                    }
3550
                }
3551
            }
3552

    
3553
            if (_LMAAttribute != null)
3554
                ReleaseCOMObjects(_LMAAttribute);
3555
            if (_LMItemNote != null)
3556
                ReleaseCOMObjects(_LMItemNote);
3557
            if (_LMSymbol != null)
3558
                ReleaseCOMObjects(_LMSymbol);
3559
        }
3560

    
3561
        private void JoinRunBySameType(string modelItemId, ref string survivorId)
3562
        {
3563
            LMModelItem modelItem = dataSource.GetModelItem(modelItemId);
3564
            if (modelItem != null)
3565
            {
3566
                foreach (LMRepresentation rep in modelItem.Representations)
3567
                {
3568
                    if (rep.Attributes["RepresentationType"].get_Value() == "Connector" && rep.Attributes["ItemStatus"].get_Value() == "Active")
3569
                    {
3570
                        LMConnector connector = dataSource.GetConnector(rep.Id);
3571
                        if (connector.ConnectItem1SymbolObject != null && connector.ConnectItem1SymbolObject.get_RepresentationType() != "Branch")
3572
                        {
3573
                            LMSymbol symbol = connector.ConnectItem1SymbolObject;
3574
                            List<string> modelItemIds = FindOtherModelItemBySymbolWhereTypePipeRun(symbol, modelItem.Id);
3575
                            if (modelItemIds.Count == 1)
3576
                            {
3577
                                JoinRun(modelItemIds[0], modelItemId, ref survivorId);
3578
                                if (survivorId != null)
3579
                                    break;
3580
                            }
3581
                        }
3582
                        if (connector.ConnectItem2SymbolObject != null && connector.ConnectItem2SymbolObject.get_RepresentationType() != "Branch")
3583
                        {
3584
                            LMSymbol symbol = connector.ConnectItem2SymbolObject;
3585
                            List<string> modelItemIds = FindOtherModelItemBySymbolWhereTypePipeRun(symbol, modelItem.Id);
3586
                            if (modelItemIds.Count == 1)
3587
                            {
3588
                                JoinRun(modelItemIds[0], modelItemId, ref survivorId);
3589
                                if (survivorId != null)
3590
                                    break;
3591
                            }
3592
                        }
3593
                    }
3594
                }
3595
            }
3596
        }
3597

    
3598
        /// <summary>
3599
        /// Label의 좌표를 구하는 메서드(ID2 기준의 좌표 -> SPPID 좌표)
3600
        /// </summary>
3601
        /// <param name="x"></param>
3602
        /// <param name="y"></param>
3603
        /// <param name="originX"></param>
3604
        /// <param name="originY"></param>
3605
        /// <param name="SPPIDLabelLocation"></param>
3606
        /// <param name="location"></param>
3607
        private void CalcLabelLocation(ref double x, ref double y, double originX, double originY, SPPIDLabelLocationInfo SPPIDLabelLocation, Location location)
3608
        {
3609
            if (location == Location.None)
3610
            {
3611
                x = originX;
3612
                y = originY;
3613
            }
3614
            else
3615
            {
3616
                if (location.HasFlag(Location.Center))
3617
                {
3618
                    x = (SPPIDLabelLocation.X1 + SPPIDLabelLocation.X2) / 2;
3619
                    y = (SPPIDLabelLocation.Y1 + SPPIDLabelLocation.Y2) / 2;
3620
                }
3621

    
3622
                if (location.HasFlag(Location.Left))
3623
                    x = SPPIDLabelLocation.X1;
3624
                else if (location.HasFlag(Location.Right))
3625
                    x = SPPIDLabelLocation.X2;
3626

    
3627
                if (location.HasFlag(Location.Down))
3628
                    y = SPPIDLabelLocation.Y1;
3629
                else if (location.HasFlag(Location.Up))
3630
                    y = SPPIDLabelLocation.Y2;
3631
            }
3632
        }
3633

    
3634
        /// <summary>
3635
        /// Symbol의 우선순위 Modeling 목록을 가져온다.
3636
        /// 1. Angle Valve
3637
        /// 2. 3개로 이루어진 Symbol Group
3638
        /// </summary>
3639
        /// <returns></returns>
3640
        private List<Symbol> GetPrioritySymbol()
3641
        {
3642
            DataTable symbolTable = document.SymbolTable;
3643
            // List에 순서대로 쌓는다.
3644
            List<Symbol> symbols = new List<Symbol>();
3645

    
3646
            // Angle Valve 부터
3647
            foreach (var symbol in document.SYMBOLS.FindAll(x => x.CONNECTORS.FindAll(y => y.Index == 0).Count == 2))
3648
            {
3649
                if (!symbols.Contains(symbol))
3650
                {
3651
                    double originX = 0;
3652
                    double originY = 0;
3653

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

    
3658
                    SlopeType slopeType1 = SlopeType.None;
3659
                    SlopeType slopeType2 = SlopeType.None;
3660
                    foreach (Connector connector in symbol.CONNECTORS.FindAll(x => x.Index == 0))
3661
                    {
3662
                        double connectorX = 0;
3663
                        double connectorY = 0;
3664
                        SPPIDUtil.ConvertPointBystring(connector.CONNECTPOINT, ref connectorX, ref connectorY);
3665
                        if (slopeType1 == SlopeType.None)
3666
                            slopeType1 = SPPIDUtil.CalcSlope(originX, originY, connectorX, connectorY);
3667
                        else
3668
                            slopeType2 = SPPIDUtil.CalcSlope(originX, originY, connectorX, connectorY);
3669
                    }
3670

    
3671
                    if ((slopeType1 == SlopeType.VERTICAL && slopeType2 == SlopeType.HORIZONTAL) ||
3672
                        (slopeType2 == SlopeType.VERTICAL && slopeType1 == SlopeType.HORIZONTAL))
3673
                        symbols.Add(symbol);
3674
                }
3675
            }
3676

    
3677
            List<Symbol> tempSymbols = new List<Symbol>();
3678
            // Conn 갯수 기준
3679
            foreach (var item in document.SYMBOLS)
3680
            {
3681
                if (!symbols.Contains(item))
3682
                    tempSymbols.Add(item);
3683
            }
3684
            tempSymbols.Sort(SortSymbolPriority);
3685
            symbols.AddRange(tempSymbols);
3686

    
3687
            return symbols;
3688
        }
3689

    
3690
        private void SetPriorityLine()
3691
        {
3692
            document.LINES.Sort(SortLinePriority);
3693

    
3694
            int SortLinePriority(Line a, Line b)
3695
            {
3696
                // Branch 없는것부터
3697
                int branchRetval = CompareBranchLine(a, b);
3698
                if (branchRetval != 0)
3699
                {
3700
                    return branchRetval;
3701
                }
3702
                else
3703
                {
3704
                    // Symbol 연결 갯수
3705
                    int connSymbolRetval = CompareConnSymbol(a, b);
3706
                    if (connSymbolRetval != 0)
3707
                    {
3708
                        return connSymbolRetval;
3709
                    }
3710
                    else
3711
                    {
3712
                        // 아이템 연결 갯수(심볼, Line이면서 Not Branch)
3713
                        int connItemRetval = CompareConnItem(a, b);
3714
                        if (connItemRetval != 0)
3715
                        {
3716
                            return connItemRetval;
3717
                        }
3718
                        else
3719
                        {
3720
                            // ConnectedItem이 없는것
3721
                            int noneConnRetval = CompareNoneConn(a, b);
3722
                            if (noneConnRetval != 0)
3723
                            {
3724
                                return noneConnRetval;
3725
                            }
3726
                            else
3727
                            {
3728

    
3729
                            }
3730
                        }
3731
                    }
3732
                }
3733

    
3734
                return 0;
3735
            }
3736

    
3737
            int CompareConnSymbol(Line a, Line b)
3738
            {
3739
                List<Connector> connectorsA = a.CONNECTORS
3740
                    .Where(conn => conn.ConnectedObject != null && conn.ConnectedObject.GetType() == typeof(Symbol))
3741
                    .ToList();
3742

    
3743
                List<Connector> connectorsB = b.CONNECTORS
3744
                    .Where(conn => conn.ConnectedObject != null && conn.ConnectedObject.GetType() == typeof(Symbol))
3745
                    .ToList();
3746

    
3747
                // 오름차순
3748
                return connectorsB.Count.CompareTo(connectorsA.Count);
3749
            }
3750

    
3751
            int CompareConnItem(Line a, Line b)
3752
            {
3753
                List<Connector> connectorsA = a.CONNECTORS
3754
                    .Where(conn => conn.ConnectedObject != null && 
3755
                    (conn.ConnectedObject.GetType() == typeof(Symbol) || 
3756
                    (conn.ConnectedObject.GetType() == typeof(Line) && !SPPIDUtil.IsBranchLine((Line)conn.ConnectedObject, a))))
3757
                    .ToList();
3758

    
3759
                List<Connector> connectorsB = b.CONNECTORS
3760
                    .Where(conn => conn.ConnectedObject != null &&
3761
                    (conn.ConnectedObject.GetType() == typeof(Symbol) ||
3762
                    (conn.ConnectedObject.GetType() == typeof(Line) && !SPPIDUtil.IsBranchLine((Line)conn.ConnectedObject, b))))
3763
                    .ToList();
3764

    
3765
                // 오름차순
3766
                return connectorsB.Count.CompareTo(connectorsA.Count);
3767
            }
3768

    
3769
            int CompareBranchLine(Line a, Line b)
3770
            {
3771
                List<Connector> connectorsA = a.CONNECTORS
3772
                    .Where(conn => conn.ConnectedObject != null && conn.ConnectedObject.GetType() == typeof(Line) && SPPIDUtil.IsBranchLine(a, conn.ConnectedObject as Line))
3773
                    .ToList();
3774
                List<Connector> connectorsB = b.CONNECTORS
3775
                    .Where(conn => conn.ConnectedObject != null && conn.ConnectedObject.GetType() == typeof(Line) && SPPIDUtil.IsBranchLine(b, conn.ConnectedObject as Line))
3776
                    .ToList();
3777

    
3778
                // 내림차순
3779
                return connectorsA.Count.CompareTo(connectorsB.Count);
3780
            }
3781

    
3782
            int CompareNoneConn(Line a, Line b)
3783
            {
3784
                List<Connector> connectorsA = a.CONNECTORS
3785
                    .Where(conn => conn.ConnectedObject == null)
3786
                    .ToList();
3787

    
3788
                List<Connector> connectorsB = b.CONNECTORS
3789
                    .Where(conn => conn.ConnectedObject == null)
3790
                    .ToList();
3791

    
3792
                // 오름차순
3793
                return connectorsB.Count.CompareTo(connectorsA.Count);
3794
            }
3795
        }
3796

    
3797
        private void SortBranchLines()
3798
        {
3799
            NewBranchLines.Sort(SortBranchLine);
3800
            int SortBranchLine(Line a, Line b)
3801
            {
3802
                int countA = a.CONNECTORS.FindAll(x => x.ConnectedObject != null &&
3803
                 x.ConnectedObject.GetType() == typeof(Line) &&
3804
                 SPPIDUtil.IsBranchLine(x.ConnectedObject as Line, a) &&
3805
                 string.IsNullOrEmpty(((Line)x.ConnectedObject).SPPID.ModelItemId)).Count;
3806

    
3807
                int countB = b.CONNECTORS.FindAll(x => x.ConnectedObject != null &&
3808
                 x.ConnectedObject.GetType() == typeof(Line) &&
3809
                 SPPIDUtil.IsBranchLine(x.ConnectedObject as Line, b) &&
3810
                 string.IsNullOrEmpty(((Line)x.ConnectedObject).SPPID.ModelItemId)).Count;
3811

    
3812
                // 내림차순
3813
                return countA.CompareTo(countB);
3814
            }
3815
        }
3816

    
3817
        private static int SortSymbolPriority(Symbol a, Symbol b)
3818
        {
3819
            int countA = a.CONNECTORS.FindAll(x => !string.IsNullOrEmpty(x.CONNECTEDITEM) && x.CONNECTEDITEM != "None").Count;
3820
            int countB = b.CONNECTORS.FindAll(x => !string.IsNullOrEmpty(x.CONNECTEDITEM) && x.CONNECTEDITEM != "None").Count;
3821
            int retval = countB.CompareTo(countA);
3822
            if (retval != 0)
3823
                return retval;
3824
            else
3825
                return a.SPPID.ORIGINAL_X.CompareTo(b.SPPID.ORIGINAL_X);
3826
        }
3827

    
3828
        private string GetSPPIDFileName(LMModelItem modelItem)
3829
        {
3830
            string symbolPath = null;
3831
            foreach (LMRepresentation rep in modelItem.Representations)
3832
            {
3833
                if (!DBNull.Value.Equals(rep.get_FileName()) && !string.IsNullOrEmpty(rep.get_FileName()))
3834
                {
3835
                    symbolPath = rep.get_FileName();
3836
                    break;
3837
                }
3838
            }
3839
            return symbolPath;
3840
        }
3841

    
3842
        /// <summary>
3843
        /// Graphic OID로 해당 Symbol의 크기를 구하여 Zoom
3844
        /// </summary>
3845
        /// <param name="graphicOID"></param>
3846
        /// <param name="milliseconds"></param>
3847
        private void ZoomObjectByGraphicOID(string graphicOID, int milliseconds = 150)
3848
        {
3849
            if (radApp.ActiveDocument.ActiveSheet.DrawingObjects[graphicOID] != null)
3850
            {
3851
                double minX = 0;
3852
                double minY = 0;
3853
                double maxX = 0;
3854
                double maxY = 0;
3855
                radApp.ActiveDocument.ActiveSheet.DrawingObjects[graphicOID].Range(out minX, out minY, out maxX, out maxY);
3856
                radApp.ActiveWindow.ZoomArea2(minX - 0.007, minY - 0.007, maxX + 0.007, maxY + 0.007, null);
3857

    
3858
                Thread.Sleep(milliseconds);
3859
            }
3860
        }
3861

    
3862
        /// <summary>
3863
        /// ComObject를 Release
3864
        /// </summary>
3865
        /// <param name="objVars"></param>
3866
        public void ReleaseCOMObjects(params object[] objVars)
3867
        {
3868
            int intNewRefCount = 0;
3869
            foreach (object obj in objVars)
3870
            {
3871
                if (!Information.IsNothing(obj) && System.Runtime.InteropServices.Marshal.IsComObject(obj))
3872
                    intNewRefCount = intNewRefCount + System.Runtime.InteropServices.Marshal.FinalReleaseComObject(obj);
3873
            }
3874
        }
3875
    }
3876
}
클립보드 이미지 추가 (최대 크기: 500 MB)