프로젝트

일반

사용자정보

통계
| 개정판:

hytos / DTI_PID / SPPIDConverter / AutoModeling.cs @ 48fd75e2

이력 | 보기 | 이력해설 | 다운로드 (178 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> BranchLines = new List<Line>();
41
        List<string> ZeroLengthSymbolToSymbolModelItemID = new List<string>();
42
        List<string> ZeroLengthModelItemID = new List<string>();
43
        List<string> ZeroLengthModelItemIDReverse = new List<string>();
44
        List<Symbol> prioritySymbols;
45

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

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

    
66
                }
67
            }
68
        }
69

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

    
82
                CreateDocument(ref drawingNumber, ref drawingName);
83

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

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

    
125
                    // Result Logging
126
                    document.CheckModelingResult();
127
                }
128
            }
129
            catch (Exception ex)
130
            {
131
                SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.ClearParent, null);
132
                SplashScreenManager.CloseForm(false);
133
                System.Windows.Forms.MessageBox.Show(ex.Message + "\r\n" + ex.StackTrace);
134
            }
135
            finally
136
            {
137
                Log.Write("End Modeling");
138
                application.ActiveWindow.Fit();
139

    
140
                if (currentDrawing != null)
141
                    ReleaseCOMObjects(currentDrawing);
142

    
143
                if (radApp.ActiveDocument != null)
144
                {
145
                    radApp.ActiveDocument.Save();
146
                    ReleaseCOMObjects(newDrawing);
147
                }
148

    
149
                ReleaseCOMObjects(dataSource);
150
                ReleaseCOMObjects(_placement);
151

    
152
                Project_DB.InsertDrawingInfo(document.PATH, drawingNumber, drawingName, document);
153
                if (SplashScreenManager.Default.IsSplashFormVisible)
154
                {
155
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.ClearParent, null);
156
                    SplashScreenManager.CloseForm(false);
157
                    Log.Write("\r\n");
158
                }
159
            }
160
        }
161
        private void RunEquipmentModeling()
162
        {
163
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetAllProgress, document.Equipments.Count);
164
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Equipments Modeling");
165
            foreach (Equipment item in document.Equipments)
166
            {
167
                try
168
                {
169
                    EquipmentModeling(item);
170
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetProgress, document.Equipments.FindAll(x => !string.IsNullOrEmpty(x.SPPID.RepresentationId)).Count);
171
                }
172
                catch (Exception ex)
173
                {
174
                    Log.Write("Error in EquipmentModeling");
175
                    Log.Write("UID : " + item.UID);
176
                    Log.Write(ex.Message);
177
                    Log.Write(ex.StackTrace);
178
                }
179
            }
180
        }
181
        private void RunSymbolModeling()
182
        {
183
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetAllProgress, document.SYMBOLS.Count);
184
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Symbol Modeling");
185
            prioritySymbols = GetPrioritySymbol();
186
            foreach (var item in prioritySymbols)
187
            {
188
                try
189
                {
190
                    SymbolModelingBySymbol(item);
191
                }
192
                catch (Exception ex)
193
                {
194
                    Log.Write("Error in SymbolModelingByPriority");
195
                    Log.Write("UID : " + item.UID);
196
                    Log.Write(ex.Message);
197
                    Log.Write(ex.StackTrace);
198
                }
199
            }
200
        }
201
        private void RunLineModeling()
202
        {
203
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetAllProgress, document.LINES.Count);
204
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Lines Modeling");
205
            SetPriorityLine();
206
            foreach (var item in document.LINES)
207
            {
208
                try
209
                {
210
                    NewLineModeling(item);
211
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
212
                }
213
                catch (Exception ex)
214
                {
215
                    Log.Write("Error in NewLineModeling");
216
                    Log.Write("UID : " + item.UID);
217
                    Log.Write(ex.Message);
218
                    Log.Write(ex.StackTrace);
219
                }
220
            }
221
        }
222
        private void RunBranchLineModeling()
223
        {
224
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetAllProgress, BranchLines.Count);
225
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Branch Lines Modeling");
226
            int branchCount = BranchLines.Count;
227
            while (BranchLines.Count > 0)
228
            {
229
                try
230
                {
231
                    SortBranchLines();
232
                    Line item = BranchLines[0];
233
                    if (EnableBranchModeling(item))
234
                    {
235
                        NewLineModeling(item, true);
236
                        SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
237
                    }
238
                    else
239
                        throw new Exception("Fail Branch Modeling");
240
                }
241
                catch (Exception ex)
242
                {
243
                    Log.Write("Error in NewLineModeling");
244
                    Log.Write("UID : " + BranchLines[0].UID);
245
                    Log.Write(ex.Message);
246
                    Log.Write(ex.StackTrace);
247
                    break;
248
                }
249
            }
250

    
251
            bool EnableBranchModeling(Line line)
252
            {
253
                bool result = true;
254
                if (line.CONNECTORS.FindAll(x => x.ConnectedObject != null && 
255
                x.ConnectedObject.GetType() == typeof(Line) && 
256
                string.IsNullOrEmpty(((Line)x.ConnectedObject).SPPID.ModelItemId)).Count > 0)
257
                    result = false;
258

    
259
                return result;
260
            }
261
        }
262
        private void RunClearValueInconsistancy()
263
        {
264
            int count = 1;
265
            bool loop = true;
266
            while (loop)
267
            {
268
                loop = false;
269
                LMAFilter filter = new LMAFilter();
270
                LMACriterion criterion = new LMACriterion();
271
                filter.ItemType = "Relationship";
272
                criterion.SourceAttributeName = "SP_DRAWINGID";
273
                criterion.Operator = "=";
274
                criterion.set_ValueAttribute(currentDrawing.Id);
275
                filter.get_Criteria().Add(criterion);
276

    
277
                LMRelationships relationships = new LMRelationships();
278
                relationships.Collect(dataSource, Filter: filter);
279

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

    
463
                        SetFlowDirectionByLine(line.SPPID.ModelItemId);
464

    
465
                        ReleaseCOMObjects(modelItem);
466
                    }
467
                }
468

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

    
481
                SetFlowDirectionByLine(modelId);
482

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

    
496
                SetFlowDirectionByLine(modelId);
497

    
498
                ReleaseCOMObjects(zeroLengthModelItem);
499
                SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
500
            }
501
            foreach (var modelId in ZeroLengthSymbolToSymbolModelItemID)
502
            {
503
                SetFlowDirectionByLine(modelId);
504
                SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
505
            }
506

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

    
522
                                foreach (LMRelationship relationship in connector.Relation1Relationships)
523
                                    SetSymbolFlowDirectionByRelationShip(relationship, connector, sFlowDirection);
524
                                foreach (LMRelationship relationship in connector.Relation2Relationships)
525
                                    SetSymbolFlowDirectionByRelationShip(relationship, connector, sFlowDirection);
526

    
527
                                ReleaseCOMObjects(connector);
528
                            }
529
                        }
530
                    }
531
                    ReleaseCOMObjects(modelItem);
532
                }
533

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

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

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

    
557
                        SetSymbolFlowDirection(lineIndex, symbolIndex, sFlowDirection, symbolModelItem);
558

    
559
                        symbolModelItem.Commit();
560
                        ReleaseCOMObjects(symbolModelItem);
561
                    }
562
                }
563

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

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

    
717
            LMDrawings drawings = new LMDrawings();
718
            drawings.Collect(dataSource, Filter: filter);
719

    
720
            currentDrawing = ((dynamic)drawings).Nth(1);
721
        }
722

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

    
733
            List<string> drawingNameList = new List<string>();
734
            List<string> drawingNumberList = new List<string>();
735

    
736
            foreach (LMDrawing item in drawings)
737
            {
738
                drawingNameList.Add(item.Attributes["Name"].get_Value().ToString());
739
                drawingNumberList.Add(item.Attributes["DrawingNumber"].get_Value().ToString());
740
            }
741

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

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

    
768
            ReleaseCOMObjects(drawings);
769
        }
770

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

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

    
808
            LMSymbol _LMSymbol = null;
809

    
810
            string mappingPath = symbol.SPPID.MAPPINGNAME;
811
            double x = symbol.SPPID.ORIGINAL_X;
812
            double y = symbol.SPPID.ORIGINAL_Y;
813
            int mirror = 0;
814
            double angle = symbol.ANGLE;
815

    
816
            // OPC 일경우 180도 일때 Mirror
817
            if (mappingPath.Contains("Piping OPC's") && angle == Math.PI)
818
                mirror = 1;
819

    
820
            // Mirror 계산
821
            if (symbol.FLIP == 1)
822
            {
823
                mirror = 1;
824
                angle += Math.PI;
825
            }
826

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

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

    
836
                if (_LMSymbol != null && _TargetItem != null)
837
                {
838
                    symbol.SPPID.RepresentationId = _LMSymbol.AsLMRepresentation().Id;
839

    
840
                    if (SPPIDUtil.IsSegmentLine(document, symbol, targetSymbol))
841
                    {
842
                        LMConnector reModelingConnector = FindBreakLineTarget(symbol, targetSymbol);
843
                        if (reModelingConnector != null)
844
                            ReModelingLMConnector(reModelingConnector);
845
                    }
846
                }
847

    
848
                ReleaseCOMObjects(_TargetItem);
849
            }
850
            else
851
                _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle);
852

    
853
            if (_LMSymbol != null)
854
            {
855
                _LMSymbol.Commit();
856
                symbol.SPPID.RepresentationId = _LMSymbol.AsLMRepresentation().Id;
857
                symbol.SPPID.ModelItemID = _LMSymbol.ModelItemID;
858
                symbol.SPPID.GraphicOID = _LMSymbol.get_GraphicOID().ToString();
859

    
860
                foreach (var item in symbol.ChildSymbols)
861
                    CreateChildSymbol(item, _LMSymbol, symbol);
862

    
863
                symbol.SPPID.SPPID_X = _LMSymbol.get_XCoordinate();
864
                symbol.SPPID.SPPID_Y = _LMSymbol.get_YCoordinate();
865

    
866
                double[] range = null;
867
                GetSPPIDSymbolRange(symbol, ref range);
868
                symbol.SPPID.SPPID_Min_X = range[0];
869
                symbol.SPPID.SPPID_Min_Y = range[1];
870
                symbol.SPPID.SPPID_Max_X = range[2];
871
                symbol.SPPID.SPPID_Max_Y = range[3];
872

    
873
                foreach (var item in symbol.SPPID.CorrectionX_GroupSymbols)
874
                    item.SPPID.ORIGINAL_X = symbol.SPPID.SPPID_X;
875
                foreach (var item in symbol.SPPID.CorrectionY_GroupSymbols)
876
                    item.SPPID.ORIGINAL_Y = symbol.SPPID.SPPID_Y;
877

    
878
                ReleaseCOMObjects(_LMSymbol);
879
            }
880
        }
881

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

    
894
            symbol.SPPID.RepresentationId = string.Empty;
895
            symbol.SPPID.ModelItemID = string.Empty;
896
            symbol.SPPID.SPPID_X = double.NaN;
897
            symbol.SPPID.SPPID_Y = double.NaN;
898
            symbol.SPPID.SPPID_Min_X = double.NaN;
899
            symbol.SPPID.SPPID_Min_Y = double.NaN;
900
            symbol.SPPID.SPPID_Max_X = double.NaN;
901
            symbol.SPPID.SPPID_Max_Y = double.NaN;
902
        }
903

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

    
918
                symbol.SPPID.RepresentationId = string.Empty;
919
                symbol.SPPID.ModelItemID = string.Empty;
920
                symbol.SPPID.SPPID_X = double.NaN;
921
                symbol.SPPID.SPPID_Y = double.NaN;
922
                symbol.SPPID.SPPID_Min_X = double.NaN;
923
                symbol.SPPID.SPPID_Min_Y = double.NaN;
924
                symbol.SPPID.SPPID_Max_X = double.NaN;
925
                symbol.SPPID.SPPID_Max_Y = double.NaN;
926
            }
927
        }
928

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

    
940
            double[] range = null;
941
            List<double[]> points = new List<double[]>();
942
            GetSPPIDSymbolRangeAndConnectionPoints(targetSymbol, ref range, points);
943
            double x1 = range[0];
944
            double y1 = range[1];
945
            double x2 = range[2];
946
            double y2 = range[3];
947

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

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

    
965
            double percentX = (x2 - x1) / sizeWidth;
966
            double percentY = (y2 - y1) / sizeHeight;
967

    
968
            double SPPIDgapX = gapX * percentX;
969
            double SPPIDgapY = gapY * percentY;
970

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

    
986
            ReleaseCOMObjects(_TargetItem);
987
        }
988

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

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

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

    
1030
            foreach (var childSymbol in symbol.ChildSymbols)
1031
                GetSPPIDChildSymbolRange(childSymbol, ref range, points);
1032

    
1033
            ReleaseCOMObjects(_TargetItem);
1034
        }
1035

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

    
1049
                foreach (var childSymbol in symbol.ChildSymbols)
1050
                    GetSPPIDChildSymbolRange(childSymbol, ref range);
1051

    
1052
                ReleaseCOMObjects(_TargetItem);
1053
            }
1054
        }
1055

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

    
1069
                tempRange[0] = Math.Min(tempRange[0], x1);
1070
                tempRange[1] = Math.Min(tempRange[1], y1);
1071
                tempRange[2] = Math.Max(tempRange[2], x2);
1072
                tempRange[3] = Math.Max(tempRange[3], y2);
1073

    
1074
                foreach (var childSymbol in symbol.ChildSymbols)
1075
                    GetSPPIDChildSymbolRange(childSymbol, ref tempRange);
1076

    
1077
                ReleaseCOMObjects(_TargetItem);
1078
            }
1079

    
1080
            range = tempRange;
1081
        }
1082

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

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

    
1114
                foreach (var loopChildSymbol in childSymbol.ChildSymbols)
1115
                    GetSPPIDChildSymbolRange(loopChildSymbol, ref range, points);
1116

    
1117
                ReleaseCOMObjects(_ChildSymbol);
1118
            }
1119
        }
1120

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

    
1137
                foreach (var loopChildSymbol in childSymbol.ChildSymbols)
1138
                    GetSPPIDChildSymbolRange(loopChildSymbol, ref range);
1139
                ReleaseCOMObjects(_ChildSymbol);
1140
            }
1141
        }
1142

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

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

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

    
1180
                        //Leader 선 센터로
1181
                        if (_LMLabelPresist != null)
1182
                        {
1183
                            // Target Item에 Label의 Attribute Input
1184
                            InputSymbolAttribute(targetItem, symbol.ATTRIBUTES);
1185

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

    
1214
                                        if (result)
1215
                                            break;
1216
                                    }
1217

    
1218
                                    if (result)
1219
                                        break;
1220
                                }
1221
                            }
1222

    
1223
                            symbol.SPPID.RepresentationId = _LMLabelPresist.AsLMRepresentation().Id;
1224
                            _LMLabelPresist.Commit();
1225
                            ReleaseCOMObjects(_LMLabelPresist);
1226
                        }
1227

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

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

    
1253
                    foreach (var item in connectorVertices)
1254
                        if (item.Key != null)
1255
                            ReleaseCOMObjects(item.Key);
1256
                }
1257
            }
1258
        }
1259

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

    
1269
            LMSymbol _LMSymbol = null;
1270
            LMSymbol targetItem = null;
1271
            string mappingPath = equipment.SPPID.MAPPINGNAME;
1272
            double x = equipment.SPPID.ORIGINAL_X;
1273
            double y = equipment.SPPID.ORIGINAL_Y;
1274
            int mirror = 0;
1275
            double angle = equipment.ANGLE;
1276

    
1277
            SPPIDUtil.ConvertGridPoint(ref x, ref y);
1278

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

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

    
1315
            if (_LMSymbol != null)
1316
            {
1317
                _LMSymbol.Commit();
1318
                equipment.SPPID.RepresentationId = _LMSymbol.AsLMRepresentation().Id;
1319
                equipment.SPPID.GraphicOID = _LMSymbol.get_GraphicOID().ToString();
1320
                ReleaseCOMObjects(_LMSymbol);
1321
            }
1322

    
1323
            if (targetItem != null)
1324
            {
1325
                ReleaseCOMObjects(targetItem);
1326
            }
1327

    
1328
            ReleaseCOMObjects(_LMSymbol);
1329
        }
1330

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

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

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

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

    
1419
                                    // Range 겹치는지 확인해야함
1420
                                    double[] prevRange = null;
1421
                                    GetSPPIDSymbolRange(prevSymbol, ref prevRange);
1422
                                    double[] groupRange = null;
1423
                                    GetSPPIDSymbolRange(group, ref groupRange);
1424

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

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

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

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

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

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

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

    
1528

    
1529
            ReleaseCOMObjects(_LMSymbol);
1530
        }
1531
        double index = 0;
1532
        private void NewLineModeling(Line line, bool isBranchModeling = false)
1533
        {
1534
            if (!string.IsNullOrEmpty(line.SPPID.ModelItemId) || (BranchLines.Contains(line) && !isBranchModeling))
1535
                return;
1536

    
1537
            List<Line> group = new List<Line>();
1538
            GetConnectedLineGroup(line, group);
1539
            LineCoordinateCorrection(group);
1540

    
1541
            foreach (var groupLine in group)
1542
            {
1543
                if (!isBranchModeling && SPPIDUtil.IsBranchLine(groupLine))
1544
                {
1545
                    BranchLines.Add(groupLine);
1546
                    continue;
1547
                }
1548

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

    
1621
                            placeRunInputs.AddPoint(x, y);
1622

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

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

    
1653
                    if (bRemodelingStart || bRemodelingEnd)
1654
                        ReModelingLine(groupLine, _lMConnector, _LMSymbolStart, _LMSymbolEnd, bRemodelingStart, bRemodelingEnd);
1655

    
1656
                    FlowMarkModeling(groupLine);
1657
                    LineNumberModeling(groupLine);
1658

    
1659
                    ReleaseCOMObjects(_lMConnector);
1660
                }
1661
                else if (!isBranchModeling)
1662
                {
1663
                    Log.Write("Main Line Modeling : " + groupLine.UID);
1664
                }
1665

    
1666
                List<object> removeLines = groupLine.CONNECTORS.FindAll(x =>
1667
                x.ConnectedObject != null &&
1668
                x.ConnectedObject.GetType() == typeof(Line) &&
1669
                !string.IsNullOrEmpty(((Line)x.ConnectedObject).SPPID.ModelItemId))
1670
                .Select(x => x.ConnectedObject)
1671
                .ToList();
1672

    
1673
                foreach (var item in removeLines)
1674
                    RemoveLineForModeling(item as Line);
1675

    
1676
                if (_LMAItem != null)
1677
                    ReleaseCOMObjects(_LMAItem);
1678
                if (placeRunInputs != null)
1679
                    ReleaseCOMObjects(placeRunInputs);
1680
                if (_LMSymbolStart != null)
1681
                    ReleaseCOMObjects(_LMSymbolStart);
1682
                if (_LMSymbolEnd != null)
1683
                    ReleaseCOMObjects(_LMSymbolEnd);
1684

    
1685
                if (isBranchModeling && BranchLines.Contains(groupLine))
1686
                    BranchLines.Remove(groupLine);
1687
            }
1688
        }
1689

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

    
1718
                ReleaseCOMObjects(modelItem);
1719
            }
1720
        }
1721

    
1722
        private void GetConnectedLineGroup(Line line, List<Line> group)
1723
        {
1724
            if (!group.Contains(line))
1725
                group.Add(line);
1726
            foreach (var connector in line.CONNECTORS)
1727
            {
1728
                if (connector.ConnectedObject != null &&
1729
                    connector.ConnectedObject.GetType() == typeof(Line) &&
1730
                    !group.Contains(connector.ConnectedObject) &&
1731
                    string.IsNullOrEmpty(((Line)connector.ConnectedObject).SPPID.ModelItemId))
1732
                {
1733
                    Line connLine = connector.ConnectedObject as Line;
1734
                    if (!SPPIDUtil.IsBranchLine(connLine, line))
1735
                    {
1736
                        if (line.CONNECTORS.IndexOf(connector) == 0)
1737
                            group.Insert(0, connLine);
1738
                        else
1739
                            group.Add(connLine);
1740
                        GetConnectedLineGroup(connLine, group);
1741
                    }
1742
                }
1743
            }
1744
        }
1745

    
1746
        private void LineCoordinateCorrection(List<Line> group)
1747
        {
1748
            // 순서대로 전 Item 기준 정렬
1749
            LineCoordinateCorrectionByStart(group);
1750

    
1751
            // 역으로 심볼이 있을 경우 좌표 보정
1752
            LineCoordinateCorrectionForLastLine(group);
1753
        }
1754

    
1755
        private void LineCoordinateCorrectionByStart(List<Line> group)
1756
        {
1757
            for (int i = 0; i < group.Count; i++)
1758
            {
1759
                Line line = group[i];
1760
                if (i == 0)
1761
                {
1762
                    Connector symbolConnector = line.CONNECTORS.Find(x => x.ConnectedObject != null && x.ConnectedObject.GetType() == typeof(Symbol));
1763
                    if (symbolConnector != null)
1764
                        LineCoordinateCorrectionByConnItem(line, symbolConnector.ConnectedObject);
1765
                }
1766
                else if (i != 0)
1767
                {
1768
                    LineCoordinateCorrectionByConnItem(line, group[i - 1]);
1769
                }
1770
            }
1771
        }
1772

    
1773
        private void LineCoordinateCorrectionForLastLine(List<Line> group)
1774
        {
1775
            Line checkLine = group[group.Count - 1];
1776
            Connector lastSymbolConnector = checkLine.CONNECTORS.Find(x => x.ConnectedObject != null && x.ConnectedObject.GetType() == typeof(Symbol));
1777
            if (lastSymbolConnector != null)
1778
            {
1779
                LineCoordinateCorrectionByConnItem(checkLine, lastSymbolConnector.ConnectedObject);
1780
                for (int i = group.Count - 2; i >= 0; i--)
1781
                {
1782
                    Line line = group[i + 1];
1783
                    Line prevLine = group[i];
1784

    
1785
                    // 같으면 보정
1786
                    if (line.SlopeType == prevLine.SlopeType)
1787
                        LineCoordinateCorrectionByConnItem(prevLine, line);
1788
                    else
1789
                    {
1790
                        if (line.SlopeType == SlopeType.HORIZONTAL)
1791
                        {
1792
                            double prevX = 0;
1793
                            double prevY = 0;
1794
                            GetTargetLineConnectorPoint(prevLine.CONNECTORS.Find(z => z.ConnectedObject == line), prevLine, ref prevX, ref prevY);
1795
                            ChangeLineSPPIDCoordinateByConnectorOnlyX(line, prevLine, prevX);
1796

    
1797
                            double x = 0;
1798
                            double y = 0;
1799
                            GetTargetLineConnectorPoint(line.CONNECTORS.Find(z => z.ConnectedObject == prevLine), line, ref x, ref y);
1800
                            ChangeLineSPPIDCoordinateByConnectorOnlyY(prevLine, line, y);
1801
                        }
1802
                        else if (line.SlopeType == SlopeType.VERTICAL)
1803
                        {
1804
                            double prevX = 0;
1805
                            double prevY = 0;
1806
                            GetTargetLineConnectorPoint(prevLine.CONNECTORS.Find(z => z.ConnectedObject == line), prevLine, ref prevX, ref prevY);
1807
                            ChangeLineSPPIDCoordinateByConnectorOnlyY(line, prevLine, prevY);
1808

    
1809
                            double x = 0;
1810
                            double y = 0;
1811
                            GetTargetLineConnectorPoint(line.CONNECTORS.Find(z => z.ConnectedObject == prevLine), line, ref x, ref y);
1812
                            ChangeLineSPPIDCoordinateByConnectorOnlyX(prevLine, line, x);
1813
                        }
1814
                        break;
1815
                    }
1816
                }
1817
            }
1818
        }
1819

    
1820
        private void LineCoordinateCorrectionByConnItem(Line line, object connItem)
1821
        {
1822
            double x = 0;
1823
            double y = 0;
1824
            if (connItem.GetType() == typeof(Symbol))
1825
            {
1826
                Symbol targetSymbol = connItem as Symbol;
1827
                Connector targetConnector = targetSymbol.CONNECTORS.Find(z => z.ConnectedObject == line);
1828
                if (targetConnector != null)
1829
                    GetTargetSymbolConnectorPoint(targetConnector, targetSymbol, ref x, ref y);
1830
                else
1831
                    throw new Exception("Target symbol UID : " + targetSymbol.UID + "\r\nLine UID : " + line.UID);
1832
            }
1833
            else if (connItem.GetType() == typeof(Line))
1834
            {
1835
                Line targetLine = connItem as Line;
1836
                GetTargetLineConnectorPoint(targetLine.CONNECTORS.Find(z => z.ConnectedObject == line), targetLine, ref x, ref y);
1837
            }
1838

    
1839
            ChangeLineSPPIDCoordinateByConnector(line, connItem, x, y);
1840
        }
1841

    
1842
        private void ChangeLineSPPIDCoordinateByConnector(Line line, object connItem, double x, double y, bool changeOtherCoordinate = true)
1843
        {
1844
            Connector connector = line.CONNECTORS.Find(z => z.ConnectedObject == connItem);
1845
            int index = line.CONNECTORS.IndexOf(connector);
1846
            if (index == 0)
1847
            {
1848
                line.SPPID.START_X = x;
1849
                line.SPPID.START_Y = y;
1850
                if (line.SlopeType == SlopeType.HORIZONTAL && changeOtherCoordinate)
1851
                    line.SPPID.END_Y = y;
1852
                else if (line.SlopeType == SlopeType.VERTICAL && changeOtherCoordinate)
1853
                    line.SPPID.END_X = x;
1854
            }
1855
            else
1856
            {
1857
                line.SPPID.END_X = x;
1858
                line.SPPID.END_Y = y;
1859
                if (line.SlopeType == SlopeType.HORIZONTAL && changeOtherCoordinate)
1860
                    line.SPPID.START_Y = y;
1861
                else if (line.SlopeType == SlopeType.VERTICAL && changeOtherCoordinate)
1862
                    line.SPPID.START_X = x;
1863
            }
1864
        }
1865

    
1866
        private void ChangeLineSPPIDCoordinateByConnectorOnlyX(Line line, object connItem, double x)
1867
        {
1868
            Connector connector = line.CONNECTORS.Find(z => z.ConnectedObject == connItem);
1869
            int index = line.CONNECTORS.IndexOf(connector);
1870
            if (index == 0)
1871
            {
1872
                line.SPPID.START_X = x;
1873
                if (line.SlopeType == SlopeType.VERTICAL)
1874
                    line.SPPID.END_X = x;
1875
            }
1876
            else
1877
            {
1878
                line.SPPID.END_X = x;
1879
                if (line.SlopeType == SlopeType.VERTICAL)
1880
                    line.SPPID.START_X = x;
1881
            }
1882
        }
1883

    
1884
        private void ChangeLineSPPIDCoordinateByConnectorOnlyY(Line line, object connItem, double y)
1885
        {
1886
            Connector connector = line.CONNECTORS.Find(z => z.ConnectedObject == connItem);
1887
            int index = line.CONNECTORS.IndexOf(connector);
1888
            if (index == 0)
1889
            {
1890
                line.SPPID.START_Y = y;
1891
                if (line.SlopeType == SlopeType.HORIZONTAL)
1892
                    line.SPPID.END_Y = y;
1893
            }
1894
            else
1895
            {
1896
                line.SPPID.END_Y = y;
1897
                if (line.SlopeType == SlopeType.HORIZONTAL)
1898
                    line.SPPID.START_Y = y;
1899
            }
1900
        }
1901

    
1902
        private void NeedReModeling(Line line, LMSymbol symbol, ref bool result)
1903
        {
1904
            if (symbol != null)
1905
            {
1906
                string repID = symbol.AsLMRepresentation().Id;
1907
                string symbolUID = SPPIDUtil.FindSymbolByRepresentationID(document, repID).UID;
1908
                string lineUID = line.UID;
1909

    
1910
                SpecBreak startSpecBreak = document.SpecBreaks.Find(x =>
1911
                (x.DownStreamUID == symbolUID || x.UpStreamUID == symbolUID) &&
1912
                (x.DownStreamUID == lineUID || x.UpStreamUID == lineUID));
1913

    
1914
                EndBreak startEndBreak = document.EndBreaks.Find(x =>
1915
                (x.OWNER == symbolUID || x.PROPERTIES.Find(y => y.ATTRIBUTE == "Connected Item").VALUE == symbolUID) &&
1916
                (x.OWNER == lineUID || x.PROPERTIES.Find(y => y.ATTRIBUTE == "Connected Item").VALUE == lineUID));
1917

    
1918
                if (startSpecBreak != null || startEndBreak != null)
1919
                    result = true;
1920
            }
1921
        }
1922

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

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

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

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

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

    
2011
            _placement.PIDRemovePlacement(prevLMConnector.AsLMRepresentation());
2012
            newConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
2013

    
2014
            ReleaseCOMObjects(placeRunInputs);
2015
            ReleaseCOMObjects(_LMAItem);
2016
            ReleaseCOMObjects(modelItem);
2017

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

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

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

    
2060
                line.SPPID.ModelItemId = newConnector.ModelItemID;
2061
                ReleaseCOMObjects(newConnector);
2062
            }
2063

    
2064
            ReleaseCOMObjects(modelItem);
2065
        }
2066

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

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

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

    
2122
                            if (child != null)
2123
                                break;
2124
                        }
2125

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

    
2130
                    break;
2131
                }
2132
            }
2133

    
2134
            return _LMSymbol;
2135
        }
2136

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

    
2153
            return null;
2154
        }
2155

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

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

    
2182
        private LMConnector ReModelingLMConnector(LMConnector connector, string changeSymbolPath = null)
2183
        {
2184
            string symbolPath = string.Empty;
2185
            #region get symbol path
2186
            if (string.IsNullOrEmpty(changeSymbolPath))
2187
            {
2188
                LMModelItem modelItem = dataSource.GetModelItem(connector.ModelItemID);
2189
                symbolPath = GetSPPIDFileName(modelItem);
2190
                ReleaseCOMObjects(modelItem);
2191
            }
2192
            else
2193
                symbolPath = changeSymbolPath;
2194
            
2195
            #endregion
2196

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

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

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

    
2218
                if (flowDirection == "End 1 is downstream (Outlet)")
2219
                {
2220
                    placeRunInputs.AddSymbolTarget(connector.ConnectItem2SymbolObject, x, y);
2221
                    placeRunInputs.AddSymbolTarget(connector.ConnectItem1SymbolObject, x, y);
2222
                    flowDirection = "End 1 is upstream (Inlet)";
2223
                }
2224
                else
2225
                {
2226
                    placeRunInputs.AddSymbolTarget(connector.ConnectItem1SymbolObject, x, y);
2227
                    placeRunInputs.AddSymbolTarget(connector.ConnectItem2SymbolObject, x, y);
2228
                }
2229

    
2230
                _placement.PIDRemovePlacement(connector.AsLMRepresentation());
2231
                newConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
2232
                ZeroLengthSymbolToSymbolModelItemID.Add(newConnector.ModelItemID);
2233
                if (!string.IsNullOrEmpty(flowDirection))
2234
                    newConnector.ModelItemObject.Attributes["FlowDirection"].set_Value(flowDirection);
2235
            }
2236
            else
2237
            {
2238
                List<double[]> vertices = new List<double[]>();
2239
                for (int i = 1; i <= verticesCount; i++)
2240
                {
2241
                    double x = 0;
2242
                    double y = 0;
2243
                    lineStringGeometry.GetVertex(i, ref x, ref y);
2244
                    vertices.Add(new double[] { x, y });
2245
                }
2246

    
2247
                for (int i = 0; i < vertices.Count; i++)
2248
                {
2249
                    double[] points = vertices[i];
2250
                    if (i == 0)
2251
                    {
2252
                        if (connector.ConnectItem1SymbolObject != null)
2253
                            placeRunInputs.AddSymbolTarget(connector.ConnectItem1SymbolObject, points[0], points[1]);
2254
                        else
2255
                            placeRunInputs.AddPoint(points[0], points[1]);
2256
                    }
2257
                    else if (i == vertices.Count - 1)
2258
                    {
2259
                        if (connector.ConnectItem2SymbolObject != null)
2260
                            placeRunInputs.AddSymbolTarget(connector.ConnectItem2SymbolObject, points[0], points[1]);
2261
                        else
2262
                            placeRunInputs.AddPoint(points[0], points[1]);
2263
                    }
2264
                    else
2265
                        placeRunInputs.AddPoint(points[0], points[1]);
2266
                }
2267

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

    
2270
                _placement.PIDRemovePlacement(connector.AsLMRepresentation());
2271
                newConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
2272

    
2273
                foreach (var line in lines)
2274
                    line.SPPID.ModelItemId = newConnector.ModelItemID;
2275
            }
2276

    
2277

    
2278
            return newConnector;
2279
        }
2280

    
2281
        /// <summary>
2282
        /// SpecBreak Modeling 메서드
2283
        /// </summary>
2284
        /// <param name="specBreak"></param>
2285
        private void SpecBreakModeling(SpecBreak specBreak)
2286
        {
2287
            object upStreamObj = SPPIDUtil.FindObjectByUID(document, specBreak.UpStreamUID);
2288
            object downStreamObj = SPPIDUtil.FindObjectByUID(document, specBreak.DownStreamUID);
2289

    
2290
            if (upStreamObj != null &&
2291
                downStreamObj != null)
2292
            {
2293
                LMConnector targetLMConnector = FindBreakLineTarget(upStreamObj, downStreamObj);
2294

    
2295
                if (targetLMConnector != null)
2296
                {
2297
                    foreach (var attribute in specBreak.ATTRIBUTES)
2298
                    {
2299
                        AttributeMapping mapping = document.AttributeMappings.Find(x => x.UID == attribute.UID);
2300
                        if (mapping != null && !string.IsNullOrEmpty(mapping.SPPIDSYMBOLNAME) && mapping.SPPIDSYMBOLNAME != "None")
2301
                        {
2302
                            string MappingPath = mapping.SPPIDSYMBOLNAME;
2303
                            Array array = new double[] { 0, specBreak.SPPID.ORIGINAL_X, specBreak.SPPID.ORIGINAL_Y };
2304
                            LMLabelPersist _LmLabelPersist = _placement.PIDPlaceLabel(MappingPath, ref array, Rotation: specBreak.ANGLE, LabeledItem: targetLMConnector.AsLMRepresentation(), IsLeaderVisible: mapping.LeaderLine);
2305

    
2306
                            if (_LmLabelPersist != null)
2307
                            {
2308
                                specBreak.SPPID.RepresentationId = _LmLabelPersist.AsLMRepresentation().Id;
2309
                                if (_LmLabelPersist.ModelItemObject != null)
2310
                                    specBreak.SPPID.ModelItemID = _LmLabelPersist.ModelItemID;
2311
                                specBreak.SPPID.GraphicOID = _LmLabelPersist.get_GraphicOID().ToString();
2312
                                ReleaseCOMObjects(_LmLabelPersist);
2313
                            }
2314
                        }
2315
                    }
2316
                    ReleaseCOMObjects(targetLMConnector);
2317
                }
2318
            }
2319
        }
2320

    
2321
        private LMConnector FindBreakLineTarget(object targetObj, object connectedObj)
2322
        {
2323
            LMConnector targetConnector = null;
2324
            Symbol targetSymbol = targetObj as Symbol;
2325
            Symbol connectedSymbol = connectedObj as Symbol;
2326
            Line targetLine = targetObj as Line;
2327
            Line connectedLine = connectedObj as Line;
2328
            if (targetSymbol != null && connectedSymbol != null)
2329
            {
2330
                LMSymbol targetLMSymbol = dataSource.GetSymbol(targetSymbol.SPPID.RepresentationId);
2331
                LMSymbol connectedLMSymbol = dataSource.GetSymbol(connectedSymbol.SPPID.RepresentationId);
2332

    
2333
                foreach (LMConnector connector in targetLMSymbol.Avoid1Connectors)
2334
                {
2335
                    if (connector.get_ItemStatus() != "Active")
2336
                        continue;
2337

    
2338
                    if (connector.ConnectItem1SymbolObject.Id == connectedLMSymbol.Id)
2339
                    {
2340
                        targetConnector = connector;
2341
                        break;
2342
                    }
2343
                    else if (connector.ConnectItem2SymbolObject.Id == connectedLMSymbol.Id)
2344
                    {
2345
                        targetConnector = connector;
2346
                        break;
2347
                    }
2348
                }
2349

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

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

    
2367
                ReleaseCOMObjects(targetLMSymbol);
2368
                ReleaseCOMObjects(connectedLMSymbol);
2369
            }
2370
            else if (targetLine != null && connectedLine != null)
2371
            {
2372
                LMModelItem targetModelItem = dataSource.GetModelItem(targetLine.SPPID.ModelItemId);
2373
                LMModelItem connectedModelItem = dataSource.GetModelItem(connectedLine.SPPID.ModelItemId);
2374

    
2375
                if (targetModelItem != null && targetModelItem.get_ItemStatus() == "Active" && connectedModelItem != null && connectedModelItem.get_ItemStatus() == "Active")
2376
                {
2377
                    foreach (LMRepresentation rep in targetModelItem.Representations)
2378
                    {
2379
                        if (targetConnector != null)
2380
                            break;
2381

    
2382
                        if (rep.Attributes["RepresentationType"].get_Value() == "Connector" && rep.Attributes["ItemStatus"].get_Value() == "Active")
2383
                        {
2384
                            LMConnector _LMConnector = dataSource.GetConnector(rep.Id);
2385

    
2386
                            if (IsConnected(_LMConnector, connectedModelItem))
2387
                                targetConnector = _LMConnector;
2388
                            else
2389
                                ReleaseCOMObjects(_LMConnector);
2390
                        }
2391
                    }
2392

    
2393
                    ReleaseCOMObjects(targetModelItem);
2394
                }
2395
            }
2396
            else
2397
            {
2398
                LMSymbol connectedLMSymbol = null;
2399
                if (connectedSymbol != null)
2400
                    connectedLMSymbol = dataSource.GetSymbol(connectedSymbol.SPPID.RepresentationId);
2401
                else if (targetSymbol != null)
2402
                    connectedLMSymbol = dataSource.GetSymbol(targetSymbol.SPPID.RepresentationId);
2403
                else
2404
                {
2405

    
2406
                }
2407
                LMModelItem targetModelItem = null;
2408
                if (targetLine != null)
2409
                    targetModelItem = dataSource.GetModelItem(targetLine.SPPID.ModelItemId);
2410
                else if (connectedLine != null)
2411
                    targetModelItem = dataSource.GetModelItem(connectedLine.SPPID.ModelItemId);
2412
                else
2413
                {
2414

    
2415
                }
2416
                if (connectedLMSymbol != null && targetModelItem != null)
2417
                {
2418
                    foreach (LMConnector connector in connectedLMSymbol.Avoid1Connectors)
2419
                    {
2420
                        if (connector.get_ItemStatus() != "Active")
2421
                            continue;
2422

    
2423
                        if (IsConnected(connector, targetModelItem))
2424
                        {
2425
                            targetConnector = connector;
2426
                            break;
2427
                        }
2428
                    }
2429

    
2430
                    if (targetConnector == null)
2431
                    {
2432
                        foreach (LMConnector connector in connectedLMSymbol.Avoid2Connectors)
2433
                        {
2434
                            if (connector.get_ItemStatus() != "Active")
2435
                                continue;
2436

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

    
2446
            }
2447

    
2448
            return targetConnector;
2449
        }
2450

    
2451
        private bool IsConnected(LMConnector connector, LMModelItem modelItem)
2452
        {
2453
            bool result = false;
2454

    
2455
            foreach (LMRepresentation rep in modelItem.Representations)
2456
            {
2457
                if (result)
2458
                    break;
2459

    
2460
                if (rep.Attributes["RepresentationType"].get_Value() == "Connector" && rep.Attributes["ItemStatus"].get_Value() == "Active")
2461
                {
2462
                    LMConnector _LMConnector = dataSource.GetConnector(rep.Id);
2463

    
2464
                    if (_LMConnector.ConnectItem1SymbolObject != null &&
2465
                        connector.ConnectItem1SymbolObject != null &&
2466
                        _LMConnector.ConnectItem1SymbolObject.Id == connector.ConnectItem1SymbolObject.Id)
2467
                    {
2468
                        result = true;
2469
                        ReleaseCOMObjects(_LMConnector);
2470
                        break;
2471
                    }
2472
                    else if (_LMConnector.ConnectItem1SymbolObject != null &&
2473
                        connector.ConnectItem2SymbolObject != null &&
2474
                        _LMConnector.ConnectItem1SymbolObject.Id == connector.ConnectItem2SymbolObject.Id)
2475
                    {
2476
                        result = true;
2477
                        ReleaseCOMObjects(_LMConnector);
2478
                        break;
2479
                    }
2480
                    else if (_LMConnector.ConnectItem2SymbolObject != null &&
2481
                        connector.ConnectItem1SymbolObject != null &&
2482
                        _LMConnector.ConnectItem2SymbolObject.Id == connector.ConnectItem1SymbolObject.Id)
2483
                    {
2484
                        result = true;
2485
                        ReleaseCOMObjects(_LMConnector);
2486
                        break;
2487
                    }
2488
                    else if (_LMConnector.ConnectItem2SymbolObject != null &&
2489
                        connector.ConnectItem2SymbolObject != null &&
2490
                        _LMConnector.ConnectItem2SymbolObject.Id == connector.ConnectItem2SymbolObject.Id)
2491
                    {
2492
                        result = true;
2493
                        ReleaseCOMObjects(_LMConnector);
2494
                        break;
2495
                    }
2496

    
2497
                    ReleaseCOMObjects(_LMConnector);
2498
                }
2499
            }
2500

    
2501

    
2502
            return result;
2503
        }
2504

    
2505
        /// <summary>
2506
        /// FromModelItem을 ToModelItem으로 PipeRunJoin하는 메서드
2507
        /// </summary>
2508
        /// <param name="modelItemID1"></param>
2509
        /// <param name="modelItemID2"></param>
2510
        private void JoinRun(string modelId1, string modelId2, ref string survivorId)
2511
        {
2512
            try
2513
            {
2514
                LMModelItem modelItem1 = dataSource.GetModelItem(modelId1);
2515
                _LMAItem item1 = modelItem1.AsLMAItem();
2516
                LMModelItem modelItem2 = dataSource.GetModelItem(modelId2);
2517
                _LMAItem item2 = modelItem2.AsLMAItem();
2518

    
2519
                // item2가 item1으로 조인
2520
                _placement.PIDJoinRuns(ref item1, ref item2);
2521
                item1.Commit();
2522
                item2.Commit();
2523

    
2524
                string beforeID = string.Empty;
2525
                string afterID = string.Empty;
2526

    
2527
                if (modelItem1.get_ItemStatus() == "Active" && modelItem2.get_ItemStatus() != "Active")
2528
                {
2529
                    beforeID = modelItem2.Id;
2530
                    afterID = modelItem1.Id;
2531
                    survivorId = afterID;
2532
                }
2533
                else if (modelItem1.get_ItemStatus() != "Active" && modelItem2.get_ItemStatus() == "Active")
2534
                {
2535
                    beforeID = modelItem1.Id;
2536
                    afterID = modelItem2.Id;
2537
                    survivorId = afterID;
2538
                }
2539
                else if (modelItem1.get_ItemStatus() == "Active" && modelItem2.get_ItemStatus() == "Active")
2540
                    survivorId = null;
2541
                else
2542
                {
2543
                    Log.Write("잘못된 경우");
2544
                    survivorId = null;
2545
                }
2546

    
2547

    
2548
                if (!string.IsNullOrEmpty(beforeID) && !string.IsNullOrEmpty(afterID))
2549
                {
2550
                    List<Line> lines = SPPIDUtil.FindLinesByModelId(document, beforeID);
2551
                    foreach (var line in lines)
2552
                        line.SPPID.ModelItemId = afterID;
2553
                }
2554

    
2555
                ReleaseCOMObjects(modelItem1);
2556
                ReleaseCOMObjects(item1);
2557
                ReleaseCOMObjects(modelItem2);
2558
                ReleaseCOMObjects(item2);
2559
            }
2560
            catch (Exception ex)
2561
            {
2562
                Log.Write("Join Error");
2563
                Log.Write(ex.Message);
2564
            }
2565
        }
2566

    
2567
        private List<string> FindOtherModelItemBySymbolWhereTypePipeRun(LMSymbol symbol, string modelId)
2568
        {
2569
            List<string> modelItemIDs = new List<string>();
2570
            foreach (LMConnector connector in symbol.Avoid1Connectors)
2571
            {
2572
                LMModelItem modelItem = connector.ModelItemObject;
2573
                if (modelItem.get_ItemStatus() == "Active" && modelItem.get_ItemTypeName().ToString() == "PipeRun" && modelItem.Id != modelId)
2574
                    modelItemIDs.Add(modelItem.Id);
2575
                ReleaseCOMObjects(modelItem);
2576
                ReleaseCOMObjects(connector);
2577
            }
2578

    
2579
            foreach (LMConnector connector in symbol.Avoid2Connectors)
2580
            {
2581
                LMModelItem modelItem = connector.ModelItemObject;
2582
                if (modelItem.get_ItemStatus() == "Active" && modelItem.get_ItemTypeName().ToString() == "PipeRun" && modelItem.Id != modelId)
2583
                    modelItemIDs.Add(modelItem.Id);
2584
                ReleaseCOMObjects(modelItem);
2585
                ReleaseCOMObjects(connector);
2586
            }
2587

    
2588
            modelItemIDs = modelItemIDs.Distinct().ToList();
2589

    
2590
            return modelItemIDs;
2591
        }
2592

    
2593
        /// <summary>
2594
        /// PipeRun의 좌표를 가져오는 메서드
2595
        /// </summary>
2596
        /// <param name="modelId"></param>
2597
        /// <returns></returns>
2598
        private Dictionary<LMConnector, List<double[]>> GetPipeRunVertices(string modelId)
2599
        {
2600
            Dictionary<LMConnector, List<double[]>> connectorVertices = new Dictionary<LMConnector, List<double[]>>();
2601
            LMModelItem modelItem = dataSource.GetModelItem(modelId);
2602

    
2603
            if (modelItem != null)
2604
            {
2605
                foreach (LMRepresentation rep in modelItem.Representations)
2606
                {
2607
                    if (rep.Attributes["RepresentationType"].get_Value() == "Connector" && rep.Attributes["ItemStatus"].get_Value() == "Active")
2608
                    {
2609
                        LMConnector _LMConnector = dataSource.GetConnector(rep.Id);
2610
                        connectorVertices.Add(_LMConnector, new List<double[]>());
2611
                        dynamic OID = rep.get_GraphicOID().ToString();
2612
                        DependencyObject drawingObject = radApp.ActiveDocument.ActiveSheet.DrawingObjects[OID];
2613
                        Ingr.RAD2D.LineStringGeometry2d lineStringGeometry = drawingObject.GetGeometry() as Ingr.RAD2D.LineStringGeometry2d;
2614
                        int verticesCount = lineStringGeometry.VertexCount;
2615
                        double[] vertices = null;
2616
                        lineStringGeometry.GetVertices(ref verticesCount, ref vertices);
2617
                        for (int i = 0; i < verticesCount; i++)
2618
                        {
2619
                            double x = 0;
2620
                            double y = 0;
2621
                            lineStringGeometry.GetVertex(i + 1, ref x, ref y);
2622
                            connectorVertices[_LMConnector].Add(new double[] { x, y });
2623
                        }
2624
                    }
2625
                }
2626

    
2627
                ReleaseCOMObjects(modelItem);
2628
            }
2629

    
2630
            return connectorVertices;
2631
        }
2632

    
2633
        private List<double[]> GetConnectorVertices(LMConnector connector)
2634
        {
2635
            List<double[]> vertices = new List<double[]>();
2636
            dynamic OID = connector.get_GraphicOID().ToString();
2637
            DependencyObject drawingObject = radApp.ActiveDocument.ActiveSheet.DrawingObjects[OID];
2638
            Ingr.RAD2D.LineStringGeometry2d lineStringGeometry = drawingObject.GetGeometry() as Ingr.RAD2D.LineStringGeometry2d;
2639
            int verticesCount = lineStringGeometry.VertexCount;
2640
            double[] value = null;
2641
            lineStringGeometry.GetVertices(ref verticesCount, ref value);
2642
            for (int i = 0; i < verticesCount; i++)
2643
            {
2644
                double x = 0;
2645
                double y = 0;
2646
                lineStringGeometry.GetVertex(i + 1, ref x, ref y);
2647
                vertices.Add(new double[] { x, y });
2648
            }
2649
            return vertices;
2650
        }
2651

    
2652
        /// <summary>
2653
        /// 좌표로 PipeRun의 Connector중에 어느 Connector에 가까운지/붙을지 가져오는 메서드 - 조건에 안맞아서 못찾을시 제일 가까운 점으로 가져오는 방식
2654
        /// </summary>
2655
        /// <param name="connectorVertices"></param>
2656
        /// <param name="connX"></param>
2657
        /// <param name="connY"></param>
2658
        /// <returns></returns>
2659
        private LMConnector FindTargetLMConnectorForLabel(Dictionary<LMConnector, List<double[]>> connectorVertices, double connX, double connY)
2660
        {
2661
            double length = double.MaxValue;
2662
            LMConnector targetConnector = null;
2663
            foreach (var item in connectorVertices)
2664
            {
2665
                List<double[]> points = item.Value;
2666
                for (int i = 0; i < points.Count - 1; i++)
2667
                {
2668
                    double[] point1 = points[i];
2669
                    double[] point2 = points[i + 1];
2670
                    double x1 = Math.Min(point1[0], point2[0]);
2671
                    double y1 = Math.Min(point1[1], point2[1]);
2672
                    double x2 = Math.Max(point1[0], point2[0]);
2673
                    double y2 = Math.Max(point1[1], point2[1]);
2674

    
2675
                    if ((x1 <= connX && x2 >= connX) ||
2676
                        (y1 <= connY && y2 >= connY))
2677
                    {
2678
                        double distance = SPPIDUtil.CalcPointToPointdDistance(point1[0], point1[1], connX, connY);
2679
                        if (length >= distance)
2680
                        {
2681
                            targetConnector = item.Key;
2682
                            length = distance;
2683
                        }
2684

    
2685
                        distance = SPPIDUtil.CalcPointToPointdDistance(point2[0], point2[1], connX, connY);
2686
                        if (length >= distance)
2687
                        {
2688
                            targetConnector = item.Key;
2689
                            length = distance;
2690
                        }
2691
                    }
2692
                }
2693
            }
2694

    
2695
            // 못찾았을때.
2696
            length = double.MaxValue;
2697
            if (targetConnector == null)
2698
            {
2699
                foreach (var item in connectorVertices)
2700
                {
2701
                    List<double[]> points = item.Value;
2702

    
2703
                    foreach (double[] point in points)
2704
                    {
2705
                        double distance = SPPIDUtil.CalcPointToPointdDistance(point[0], point[1], connX, connY);
2706
                        if (length >= distance)
2707
                        {
2708
                            targetConnector = item.Key;
2709
                            length = distance;
2710
                        }
2711
                    }
2712
                }
2713
            }
2714

    
2715
            return targetConnector;
2716
        }
2717

    
2718
        private LMConnector FindTargetLMConnectorForBranch(Line line, Line targetLine, ref double x, ref double y)
2719
        {
2720
            Dictionary<LMConnector, List<double[]>> vertices = GetPipeRunVertices(targetLine.SPPID.ModelItemId);
2721
            if (vertices.Count == 0)
2722
                return null;
2723

    
2724
            double length = double.MaxValue;
2725
            LMConnector targetConnector = null;
2726
            double[] resultPoint = null;
2727
            List<double[]> targetVertices = null;
2728

    
2729
            // Vertices 포인트에 제일 가까운곳
2730
            foreach (var item in vertices)
2731
            {
2732
                List<double[]> points = item.Value;
2733
                for (int i = 0; i < points.Count; i++)
2734
                {
2735
                    double[] point = points[i];
2736
                    double tempX = point[0];
2737
                    double tempY = point[1];
2738

    
2739
                    double distance = SPPIDUtil.CalcPointToPointdDistance(tempX, tempY, x, y);
2740
                    if (length >= distance)
2741
                    {
2742
                        targetConnector = item.Key;
2743
                        length = distance;
2744
                        resultPoint = point;
2745
                        targetVertices = item.Value;
2746
                    }
2747
                }
2748
            }
2749

    
2750
            // Vertices Cross에 제일 가까운곳
2751
            foreach (var item in vertices)
2752
            {
2753
                List<double[]> points = item.Value;
2754
                for (int i = 0; i < points.Count - 1; i++)
2755
                {
2756
                    double[] point1 = points[i];
2757
                    double[] point2 = points[i + 1];
2758

    
2759
                    double maxLineX = Math.Max(point1[0], point2[0]);
2760
                    double minLineX = Math.Min(point1[0], point2[0]);
2761
                    double maxLineY = Math.Max(point1[1], point2[1]);
2762
                    double minLineY = Math.Min(point1[1], point2[1]);
2763

    
2764
                    SlopeType slope = SPPIDUtil.CalcSlope(minLineX, minLineY, maxLineX, maxLineY);
2765

    
2766
                    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]);
2767
                    if (crossingPoint != null)
2768
                    {
2769
                        double distance = SPPIDUtil.CalcPointToPointdDistance(crossingPoint[0], crossingPoint[1], x, y);
2770
                        if (length >= distance)
2771
                        {
2772
                            if (slope == SlopeType.Slope &&
2773
                                minLineX <= crossingPoint[0] && maxLineX >= crossingPoint[0] &&
2774
                                minLineY <= crossingPoint[1] && maxLineY >= crossingPoint[1])
2775
                            {
2776
                                targetConnector = item.Key;
2777
                                length = distance;
2778
                                resultPoint = crossingPoint;
2779
                                targetVertices = item.Value;
2780
                            }
2781
                            else if (slope == SlopeType.HORIZONTAL &&
2782
                                minLineX <= crossingPoint[0] && maxLineX >= crossingPoint[0])
2783
                            {
2784
                                targetConnector = item.Key;
2785
                                length = distance;
2786
                                resultPoint = crossingPoint;
2787
                                targetVertices = item.Value;
2788
                            }
2789
                            else if (slope == SlopeType.VERTICAL &&
2790
                               minLineY <= crossingPoint[1] && maxLineY >= crossingPoint[1])
2791
                            {
2792
                                targetConnector = item.Key;
2793
                                length = distance;
2794
                                resultPoint = crossingPoint;
2795
                                targetVertices = item.Value;
2796
                            }
2797
                        }
2798
                    }
2799
                }
2800
            }
2801

    
2802
            foreach (var item in vertices)
2803
                if (item.Key != null && item.Key != targetConnector)
2804
                    ReleaseCOMObjects(item.Key);
2805

    
2806
            if (SPPIDUtil.IsBranchLine(line, targetLine))
2807
            {
2808
                double tempResultX = resultPoint[0];
2809
                double tempResultY = resultPoint[1];
2810
                SPPIDUtil.ConvertGridPoint(ref tempResultX, ref tempResultY);
2811

    
2812
                GridSetting gridSetting = GridSetting.GetInstance();
2813

    
2814
                for (int i = 0; i < targetVertices.Count; i++)
2815
                {
2816
                    double[] point = targetVertices[i];
2817
                    double tempX = targetVertices[i][0];
2818
                    double tempY = targetVertices[i][1];
2819
                    SPPIDUtil.ConvertGridPoint(ref tempX, ref tempY);
2820
                    if (tempX == tempResultX && tempY == tempResultY)
2821
                    {
2822
                        if (i == 0)
2823
                        {
2824
                            LMSymbol connSymbol = targetConnector.ConnectItem1SymbolObject;
2825
                            bool containZeroLength = false;
2826
                            if (connSymbol != null)
2827
                            {
2828
                                foreach (LMConnector connector in connSymbol.Connect1Connectors)
2829
                                {
2830
                                    if (connector.get_ItemStatus() == "Active" && Convert.ToBoolean(connector.get_IsZeroLength()) == true)
2831
                                        containZeroLength = true;
2832
                                }
2833
                                foreach (LMConnector connector in connSymbol.Connect2Connectors)
2834
                                {
2835
                                    if (connector.get_ItemStatus() == "Active" && Convert.ToBoolean(connector.get_IsZeroLength()) == true)
2836
                                        containZeroLength = true;
2837
                                }
2838
                            }
2839

    
2840
                            if (connSymbol == null ||
2841
                                (connSymbol != null && connSymbol.get_ItemStatus() == "Active" && connSymbol.get_RepresentationType() != "Branch") ||
2842
                                containZeroLength)
2843
                            {
2844
                                bool bCalcX = false;
2845
                                bool bCalcY = false;
2846
                                if (targetLine.SlopeType == SlopeType.HORIZONTAL)
2847
                                    bCalcX = true;
2848
                                else if (targetLine.SlopeType == SlopeType.VERTICAL)
2849
                                    bCalcY = true;
2850
                                else
2851
                                {
2852
                                    bCalcX = true;
2853
                                    bCalcY = true;
2854
                                }
2855

    
2856
                                if (bCalcX)
2857
                                {
2858
                                    double nextX = targetVertices[i + 1][0];
2859
                                    double newX = 0;
2860
                                    if (nextX > tempX)
2861
                                    {
2862
                                        newX = tempX + gridSetting.Length;
2863
                                        if (newX > nextX)
2864
                                            newX = (point[0] + nextX) / 2;
2865
                                    }
2866
                                    else
2867
                                    {
2868
                                        newX = tempX - gridSetting.Length;
2869
                                        if (newX < nextX)
2870
                                            newX = (point[0] + nextX) / 2;
2871
                                    }
2872
                                    resultPoint = new double[] { newX, resultPoint[1] };
2873
                                }
2874

    
2875
                                if (bCalcY)
2876
                                {
2877
                                    double nextY = targetVertices[i + 1][1];
2878
                                    double newY = 0;
2879
                                    if (nextY > tempY)
2880
                                    {
2881
                                        newY = tempY + gridSetting.Length;
2882
                                        if (newY > nextY)
2883
                                            newY = (point[1] + nextY) / 2;
2884
                                    }
2885
                                    else
2886
                                    {
2887
                                        newY = tempY - gridSetting.Length;
2888
                                        if (newY < nextY)
2889
                                            newY = (point[1] + nextY) / 2;
2890
                                    }
2891
                                    resultPoint = new double[] { resultPoint[0], newY };
2892
                                }
2893
                            }
2894
                        }
2895
                        else if (i == targetVertices.Count - 1)
2896
                        {
2897
                            LMSymbol connSymbol = targetConnector.ConnectItem2SymbolObject;
2898
                            bool containZeroLength = false;
2899
                            if (connSymbol != null)
2900
                            {
2901
                                foreach (LMConnector connector in connSymbol.Connect1Connectors)
2902
                                {
2903
                                    if (connector.get_ItemStatus() == "Active" && Convert.ToBoolean(connector.get_IsZeroLength()) == true)
2904
                                        containZeroLength = true;
2905
                                }
2906
                                foreach (LMConnector connector in connSymbol.Connect2Connectors)
2907
                                {
2908
                                    if (connector.get_ItemStatus() == "Active" && Convert.ToBoolean(connector.get_IsZeroLength()) == true)
2909
                                        containZeroLength = true;
2910
                                }
2911
                            }
2912

    
2913
                            if (connSymbol == null ||
2914
                                 (connSymbol != null && connSymbol.get_ItemStatus() == "Active" && connSymbol.get_RepresentationType() != "Branch") ||
2915
                                containZeroLength)
2916
                            {
2917
                                bool bCalcX = false;
2918
                                bool bCalcY = false;
2919
                                if (targetLine.SlopeType == SlopeType.HORIZONTAL)
2920
                                    bCalcX = true;
2921
                                else if (targetLine.SlopeType == SlopeType.VERTICAL)
2922
                                    bCalcY = true;
2923
                                else
2924
                                {
2925
                                    bCalcX = true;
2926
                                    bCalcY = true;
2927
                                }
2928

    
2929
                                if (bCalcX)
2930
                                {
2931
                                    double nextX = targetVertices[i - 1][0];
2932
                                    double newX = 0;
2933
                                    if (nextX > tempX)
2934
                                    {
2935
                                        newX = tempX + gridSetting.Length;
2936
                                        if (newX > nextX)
2937
                                            newX = (point[0] + nextX) / 2;
2938
                                    }
2939
                                    else
2940
                                    {
2941
                                        newX = tempX - gridSetting.Length;
2942
                                        if (newX < nextX)
2943
                                            newX = (point[0] + nextX) / 2;
2944
                                    }
2945
                                    resultPoint = new double[] { newX, resultPoint[1] };
2946
                                }
2947

    
2948
                                if (bCalcY)
2949
                                {
2950
                                    double nextY = targetVertices[i - 1][1];
2951
                                    double newY = 0;
2952
                                    if (nextY > tempY)
2953
                                    {
2954
                                        newY = tempY + gridSetting.Length;
2955
                                        if (newY > nextY)
2956
                                            newY = (point[1] + nextY) / 2;
2957
                                    }
2958
                                    else
2959
                                    {
2960
                                        newY = tempY - gridSetting.Length;
2961
                                        if (newY < nextY)
2962
                                            newY = (point[1] + nextY) / 2;
2963
                                    }
2964
                                    resultPoint = new double[] { resultPoint[0], newY };
2965
                                }
2966
                            }
2967
                        }
2968
                        break;
2969
                    }
2970
                }
2971
            }
2972

    
2973
            x = resultPoint[0];
2974
            y = resultPoint[1];
2975

    
2976
            return targetConnector;
2977
        }
2978

    
2979
        private LMConnector GetLMConnectorOnlyOne(string modelItemID)
2980
        {
2981
            LMConnector result = null;
2982
            List<LMConnector> connectors = new List<LMConnector>();
2983
            LMModelItem modelItem = dataSource.GetModelItem(modelItemID);
2984

    
2985
            if (modelItem != null)
2986
            {
2987
                foreach (LMRepresentation rep in modelItem.Representations)
2988
                {
2989
                    if (rep.Attributes["RepresentationType"].get_Value() == "Connector" && rep.Attributes["ItemStatus"].get_Value() == "Active")
2990
                        connectors.Add(dataSource.GetConnector(rep.Id));
2991
                }
2992

    
2993
                ReleaseCOMObjects(modelItem);
2994
            }
2995

    
2996
            if (connectors.Count == 1)
2997
                result = connectors[0];
2998
            else
2999
                foreach (var item in connectors)
3000
                    ReleaseCOMObjects(item);
3001

    
3002
            return result;
3003
        }
3004

    
3005
        /// <summary>
3006
        /// Line Number Symbol을 실제로 Modeling하는 메서드
3007
        /// </summary>
3008
        /// <param name="lineNumber"></param>
3009
        private void LineNumberModeling(Line line)
3010
        {
3011
            LineNumber lineNumber = document.LINENUMBERS.Find(x => x.CONNLINE == line.UID);
3012
            if (lineNumber != null)
3013
            {
3014
                LMConnector connectedLMConnector = GetLMConnectorOnlyOne(line.SPPID.ModelItemId);
3015
                if (connectedLMConnector != null)
3016
                {
3017
                    double x = 0;
3018
                    double y = 0;
3019
                    CalcLabelLocation(ref x, ref y, lineNumber.SPPID.ORIGINAL_X, lineNumber.SPPID.ORIGINAL_Y, lineNumber.SPPIDLabelLocation, _ETCSetting.LineNumberLocation);
3020

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

    
3024
                    if (_LmLabelPresist != null)
3025
                    {
3026
                        _LmLabelPresist.Commit();
3027
                        lineNumber.SPPID.RepresentationId = _LmLabelPresist.AsLMRepresentation().Id;
3028
                        ReleaseCOMObjects(_LmLabelPresist);
3029
                    }
3030
                }
3031
            }
3032
        }
3033

    
3034
        /// <summary>
3035
        /// Flow Mark Modeling
3036
        /// </summary>
3037
        /// <param name="line"></param>
3038
        private void FlowMarkModeling(Line line)
3039
        {
3040
            if (line.FLOWMARK && !string.IsNullOrEmpty(line.SPPID.ModelItemId) && !string.IsNullOrEmpty(_ETCSetting.FlowMarkSymbolPath))
3041
            {
3042
                LMConnector connector = GetLMConnectorOnlyOne(line.SPPID.ModelItemId);
3043
                if (connector != null)
3044
                {
3045
                    string mappingPath = _ETCSetting.FlowMarkSymbolPath;
3046
                    List<double[]> vertices = GetConnectorVertices(connector);
3047
                    vertices = vertices.FindAll(x => x[0] > 0 && x[1] > 0);
3048
                    double[] point = vertices[vertices.Count - 1];
3049
                    Array array = new double[] { 0, point[0], point[1] };
3050
                    LMLabelPersist _LMLabelPersist = _placement.PIDPlaceLabel(mappingPath, ref array, LabeledItem: connector.AsLMRepresentation());
3051
                    if (_LMLabelPersist != null)
3052
                        ReleaseCOMObjects(_LMLabelPersist);
3053
                }
3054
            }
3055
        }
3056

    
3057
        /// <summary>
3058
        /// Line Number 기준으로 모든 Item에 Line Number의 Attribute Input
3059
        /// </summary>
3060
        /// <param name="lineNumber"></param>
3061
        private void InputLineNumberAttribute(LineNumber lineNumber)
3062
        {
3063
            foreach (LineRun run in lineNumber.RUNS)
3064
            {
3065
                foreach (var item in run.RUNITEMS)
3066
                {
3067
                    if (item.GetType() == typeof(Symbol))
3068
                    {
3069
                        Symbol symbol = item as Symbol;
3070
                        LMSymbol _LMSymbol = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
3071
                        if (_LMSymbol != null)
3072
                        {
3073
                            LMModelItem _LMModelItem = _LMSymbol.ModelItemObject;
3074

    
3075
                            if (_LMModelItem != null && _LMModelItem.get_ItemStatus() == "Active")
3076
                            {
3077
                                foreach (var attribute in lineNumber.ATTRIBUTES)
3078
                                {
3079
                                    LineNumberMapping mapping = document.LineNumberMappings.Find(x => x.UID == attribute.UID);
3080
                                    if (mapping != null && !string.IsNullOrEmpty(attribute.VALUE) && attribute.VALUE != "None")
3081
                                    {
3082
                                        LMAAttribute _LMAAttribute = _LMModelItem.Attributes[mapping.SPPIDATTRIBUTENAME];
3083
                                        if (_LMAAttribute != null)
3084
                                        {
3085
                                            if (DBNull.Value.Equals(_LMAAttribute.get_Value()))
3086
                                                _LMAAttribute.set_Value(attribute.VALUE);
3087
                                            else if (_LMAAttribute.get_Value() != attribute.VALUE)
3088
                                                _LMAAttribute.set_Value(attribute.VALUE);
3089
                                        }
3090
                                    }
3091
                                }
3092
                                _LMModelItem.Commit();
3093
                            }
3094
                            if (_LMModelItem != null)
3095
                                ReleaseCOMObjects(_LMModelItem);
3096
                        }
3097
                        if (_LMSymbol != null)
3098
                            ReleaseCOMObjects(_LMSymbol);
3099
                    }
3100
                    else if (item.GetType() == typeof(Line))
3101
                    {
3102
                        Line line = item as Line;
3103
                        if (line != null)
3104
                        {
3105
                            LMModelItem _LMModelItem = dataSource.GetModelItem(line.SPPID.ModelItemId);
3106
                            if (_LMModelItem != null && _LMModelItem.get_ItemStatus() == "Active")
3107
                            {
3108
                                foreach (var attribute in lineNumber.ATTRIBUTES)
3109
                                {
3110
                                    LineNumberMapping mapping = document.LineNumberMappings.Find(x => x.UID == attribute.UID);
3111
                                    if (mapping != null && !string.IsNullOrEmpty(attribute.VALUE) && attribute.VALUE != "None")
3112
                                    {
3113
                                        LMAAttribute _LMAAttribute = _LMModelItem.Attributes[mapping.SPPIDATTRIBUTENAME];
3114
                                        if (_LMAAttribute != null)
3115
                                        {
3116
                                            if (DBNull.Value.Equals(_LMAAttribute.get_Value()))
3117
                                                _LMAAttribute.set_Value(attribute.VALUE);
3118
                                            else if (_LMAAttribute.get_Value() != attribute.VALUE)
3119
                                                _LMAAttribute.set_Value(attribute.VALUE);
3120

    
3121
                                        }
3122
                                    }
3123
                                }
3124
                                _LMModelItem.Commit();
3125
                            }
3126
                            if (_LMModelItem != null)
3127
                                ReleaseCOMObjects(_LMModelItem);
3128
                        }
3129
                    }
3130
                }
3131
            }
3132
        }
3133

    
3134
        /// <summary>
3135
        /// Symbol Attribute 입력 메서드
3136
        /// </summary>
3137
        /// <param name="item"></param>
3138
        private void InputSymbolAttribute(object targetItem, List<BaseModel.Attribute> targetAttributes)
3139
        {
3140
            // Object 아이템이 Symbol일 경우 Equipment일 경우 
3141
            string sRep = null;
3142
            if (targetItem.GetType() == typeof(Symbol))
3143
                sRep = ((Symbol)targetItem).SPPID.RepresentationId;
3144
            else if (targetItem.GetType() == typeof(Equipment))
3145
                sRep = ((Equipment)targetItem).SPPID.RepresentationId;
3146

    
3147
            if (!string.IsNullOrEmpty(sRep))
3148
            {
3149
                LMSymbol _LMSymbol = dataSource.GetSymbol(sRep);
3150
                LMModelItem _LMModelItem = _LMSymbol.ModelItemObject;
3151
                LMAAttributes _Attributes = _LMModelItem.Attributes;
3152
                
3153
                foreach (var item in targetAttributes)
3154
                {
3155
                    AttributeMapping mapping = document.AttributeMappings.Find(x => x.UID == item.UID);
3156
                    if (mapping != null && !string.IsNullOrEmpty(item.VALUE) && item.VALUE != "None")
3157
                    {
3158
                        LMAAttribute _Attribute = _Attributes[mapping.SPPIDATTRIBUTENAME];
3159
                        if (_Attribute != null)
3160
                        {
3161
                            object associItem = SPPIDUtil.FindObjectByUID(document, item.ASSOCITEM);
3162
                            if (associItem != null)
3163
                            {
3164
                                if (associItem.GetType() == typeof(Text))
3165
                                {
3166
                                    Text text = associItem as Text;
3167
                                    text.SPPID.RepresentationId = "Attribute";
3168
                                }
3169
                                else if (associItem.GetType() == typeof(Note))
3170
                                {
3171
                                    Note note = associItem as Note;
3172
                                    note.SPPID.RepresentationId = "Attribute";
3173
                                }
3174
                            }
3175
                            _Attribute.set_Value(item.VALUE);
3176
                        }
3177
                            
3178
                    }
3179
                }
3180
                _LMModelItem.Commit();
3181

    
3182
                ReleaseCOMObjects(_Attributes);
3183
                ReleaseCOMObjects(_LMModelItem);
3184
                ReleaseCOMObjects(_LMSymbol);
3185
            }
3186
        }
3187

    
3188
        /// <summary>
3189
        /// Input SpecBreak Attribute
3190
        /// </summary>
3191
        /// <param name="specBreak"></param>
3192
        private void InputSpecBreakAttribute(SpecBreak specBreak)
3193
        {
3194
            object upStreamObj = SPPIDUtil.FindObjectByUID(document, specBreak.UpStreamUID);
3195
            object downStreamObj = SPPIDUtil.FindObjectByUID(document, specBreak.DownStreamUID);
3196

    
3197
            if (upStreamObj != null &&
3198
                downStreamObj != null)
3199
            {
3200
                LMConnector targetLMConnector = FindBreakLineTarget(upStreamObj, downStreamObj);
3201

    
3202
                if (targetLMConnector != null)
3203
                {
3204
                    foreach (LMLabelPersist _LMLabelPersist in targetLMConnector.LabelPersists)
3205
                    {
3206
                        string symbolPath = _LMLabelPersist.get_FileName();
3207
                        AttributeMapping mapping = document.AttributeMappings.Find(x => x.SPPIDSYMBOLNAME == symbolPath);
3208
                        if (mapping != null)
3209
                        {
3210
                            BaseModel.Attribute attribute = specBreak.ATTRIBUTES.Find(y => y.UID == mapping.UID);
3211
                            if (attribute != null && !string.IsNullOrEmpty(attribute.VALUE) && attribute.VALUE != "None")
3212
                            {
3213
                                string[] values = attribute.VALUE.Split(new char[] { ',' });
3214
                                if (values.Length == 2)
3215
                                {
3216
                                    string upStreamValue = values[0];
3217
                                    string downStreamValue = values[1];
3218

    
3219
                                    InputAttributeForSpecBreak(upStreamObj, downStreamObj, upStreamValue, downStreamValue, mapping.SPPIDATTRIBUTENAME);
3220
                                }
3221
                            }
3222
                        }
3223
                    }
3224

    
3225
                    ReleaseCOMObjects(targetLMConnector);
3226
                }
3227
            }
3228

    
3229

    
3230
            #region 내부에서만 쓰는 메서드
3231
            void InputAttributeForSpecBreak(object _upStreamObj, object _downStreamObj, string upStreamValue, string downStreamValue, string sppidAttributeName)
3232
            {
3233
                Symbol upStreamSymbol = _upStreamObj as Symbol;
3234
                Line upStreamLine = _upStreamObj as Line;
3235
                Symbol downStreamSymbol = _downStreamObj as Symbol;
3236
                Line downStreamLine = _downStreamObj as Line;
3237
                // 둘다 Line일 경우
3238
                if (upStreamLine != null && downStreamLine != null)
3239
                {
3240
                    InputLineAttributeForSpecBreakLine(upStreamLine, sppidAttributeName, upStreamValue);
3241
                    InputLineAttributeForSpecBreakLine(downStreamLine, sppidAttributeName, downStreamValue);
3242
                }
3243
                // 둘다 Symbol일 경우
3244
                else if (upStreamSymbol != null && downStreamSymbol != null)
3245
                {
3246
                    LMConnector zeroLenthConnector = FindBreakLineTarget(upStreamSymbol, downStreamSymbol);
3247
                    LMSymbol upStreamLMSymbol = dataSource.GetSymbol(upStreamSymbol.SPPID.RepresentationId);
3248
                    LMSymbol downStreamLMSymbol = dataSource.GetSymbol(downStreamSymbol.SPPID.RepresentationId);
3249

    
3250
                    foreach (LMConnector connector in upStreamLMSymbol.Avoid1Connectors)
3251
                    {
3252
                        if (connector.get_ItemStatus() != "Active")
3253
                            continue;
3254

    
3255
                        if (connector.Id != zeroLenthConnector.Id)
3256
                            InputLineAttributeForSpecBreakLMConnector(connector, sppidAttributeName, upStreamValue);
3257
                    }
3258

    
3259
                    foreach (LMConnector connector in upStreamLMSymbol.Avoid2Connectors)
3260
                    {
3261
                        if (connector.get_ItemStatus() != "Active")
3262
                            continue;
3263

    
3264
                        if (connector.Id != zeroLenthConnector.Id)
3265
                            InputLineAttributeForSpecBreakLMConnector(connector, sppidAttributeName, upStreamValue);
3266
                    }
3267

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

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

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

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

    
3286
                    ReleaseCOMObjects(zeroLenthConnector);
3287
                    ReleaseCOMObjects(upStreamLMSymbol);
3288
                    ReleaseCOMObjects(downStreamLMSymbol);
3289
                }
3290
                else if (upStreamSymbol != null && downStreamLine != null)
3291
                {
3292
                    LMConnector zeroLenthConnector = FindBreakLineTarget(upStreamSymbol, downStreamLine);
3293
                    InputLineAttributeForSpecBreakLine(downStreamLine, sppidAttributeName, downStreamValue);
3294
                    LMSymbol upStreamLMSymbol = dataSource.GetSymbol(upStreamSymbol.SPPID.RepresentationId);
3295

    
3296
                    foreach (LMConnector connector in upStreamLMSymbol.Avoid1Connectors)
3297
                    {
3298
                        if (connector.get_ItemStatus() != "Active")
3299
                            continue;
3300

    
3301
                        if (connector.Id != zeroLenthConnector.Id)
3302
                            InputLineAttributeForSpecBreakLMConnector(connector, sppidAttributeName, upStreamValue);
3303
                    }
3304

    
3305
                    foreach (LMConnector connector in upStreamLMSymbol.Avoid2Connectors)
3306
                    {
3307
                        if (connector.get_ItemStatus() != "Active")
3308
                            continue;
3309

    
3310
                        if (connector.Id != zeroLenthConnector.Id)
3311
                            InputLineAttributeForSpecBreakLMConnector(connector, sppidAttributeName, upStreamValue);
3312
                    }
3313

    
3314
                    ReleaseCOMObjects(zeroLenthConnector);
3315
                    ReleaseCOMObjects(upStreamLMSymbol);
3316
                }
3317
                else if (upStreamLine != null && downStreamSymbol != null)
3318
                {
3319
                    LMConnector zeroLenthConnector = FindBreakLineTarget(upStreamLine, downStreamSymbol);
3320
                    InputLineAttributeForSpecBreakLine(upStreamLine, sppidAttributeName, upStreamValue);
3321
                    LMSymbol downStreamLMSymbol = dataSource.GetSymbol(downStreamSymbol.SPPID.RepresentationId);
3322

    
3323
                    foreach (LMConnector connector in downStreamLMSymbol.Avoid1Connectors)
3324
                    {
3325
                        if (connector.get_ItemStatus() != "Active")
3326
                            continue;
3327

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

    
3332
                    foreach (LMConnector connector in downStreamLMSymbol.Avoid2Connectors)
3333
                    {
3334
                        if (connector.get_ItemStatus() != "Active")
3335
                            continue;
3336

    
3337
                        if (connector.Id != zeroLenthConnector.Id)
3338
                            InputLineAttributeForSpecBreakLMConnector(connector, sppidAttributeName, downStreamValue);
3339
                    }
3340

    
3341
                    ReleaseCOMObjects(zeroLenthConnector);
3342
                    ReleaseCOMObjects(downStreamLMSymbol);
3343
                }
3344
            }
3345

    
3346
            void InputLineAttributeForSpecBreakLine(Line line, string attrName, string value)
3347
            {
3348
                LMModelItem _LMModelItem = dataSource.GetModelItem(line.SPPID.ModelItemId);
3349
                if (_LMModelItem != null && _LMModelItem.get_ItemStatus() == "Active")
3350
                {
3351
                    LMAAttribute _LMAAttribute = _LMModelItem.Attributes[attrName];
3352
                    if (_LMAAttribute != null)
3353
                    {
3354
                        if (DBNull.Value.Equals(_LMAAttribute.get_Value()))
3355
                            _LMAAttribute.set_Value(value);
3356
                        else if (_LMAAttribute.get_Value() != value)
3357
                            _LMAAttribute.set_Value(value);
3358
                    }
3359

    
3360
                    _LMModelItem.Commit();
3361
                }
3362
                if (_LMModelItem != null)
3363
                    ReleaseCOMObjects(_LMModelItem);
3364
            }
3365

    
3366
            void InputLineAttributeForSpecBreakLMConnector(LMConnector connector, string attrName, string value)
3367
            {
3368
                LMModelItem _LMModelItem = dataSource.GetModelItem(connector.ModelItemID);
3369
                if (_LMModelItem != null && _LMModelItem.get_ItemStatus() == "Active")
3370
                {
3371
                    LMAAttribute _LMAAttribute = _LMModelItem.Attributes[attrName];
3372
                    if (_LMAAttribute != null)
3373
                    {
3374
                        if (DBNull.Value.Equals(_LMAAttribute.get_Value()))
3375
                            _LMAAttribute.set_Value(value);
3376
                        else if (_LMAAttribute.get_Value() != value)
3377
                            _LMAAttribute.set_Value(value);
3378
                    }
3379

    
3380
                    _LMModelItem.Commit();
3381
                }
3382
                if (_LMModelItem != null)
3383
                    ReleaseCOMObjects(_LMModelItem);
3384
            }
3385
            #endregion
3386
        }
3387

    
3388
        /// <summary>
3389
        /// Text Modeling - Association일 경우는 Text대신 해당 맵핑된 Symbol로 모델링
3390
        /// </summary>
3391
        /// <param name="text"></param>
3392
        private void TextModeling(Text text)
3393
        {
3394
            LMSymbol _LMSymbol = null;
3395
            LMConnector connectedLMConnector = null;
3396
            //if (text.ASSOCIATION && !string.IsNullOrEmpty(text.OWNER) && text.OWNER != "None")
3397
            if (text.ASSOCIATION)
3398
            {
3399
                object owner = SPPIDUtil.FindObjectByUID(document, text.OWNER);
3400
                if (owner != null && owner.GetType() == typeof(Symbol))
3401
                {
3402
                    Symbol symbol = owner as Symbol;
3403
                    _LMSymbol = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
3404
                    if (_LMSymbol != null)
3405
                    {
3406
                        BaseModel.Attribute attribute = symbol.ATTRIBUTES.Find(x => x.ASSOCITEM == text.UID);
3407
                        if (attribute != null && !string.IsNullOrEmpty(attribute.VALUE) && attribute.VALUE != "None")
3408
                        {
3409
                            AttributeMapping mapping = document.AttributeMappings.Find(x => x.UID == attribute.UID && !string.IsNullOrEmpty(x.SPPIDSYMBOLNAME));
3410

    
3411
                            if (mapping != null)
3412
                            {
3413
                                double x = 0;
3414
                                double y = 0;
3415

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

    
3419
                                LMLabelPersist _LMLabelPersist = _placement.PIDPlaceLabel(mapping.SPPIDSYMBOLNAME, ref array, Rotation: text.ANGLE, LabeledItem: _LMSymbol.AsLMRepresentation(), IsLeaderVisible: mapping.LeaderLine);
3420
                                if (_LMLabelPersist != null)
3421
                                {
3422
                                    text.SPPID.RepresentationId = _LMLabelPersist.AsLMRepresentation().Id;
3423
                                    _LMLabelPersist.Commit();
3424
                                    ReleaseCOMObjects(_LMLabelPersist);
3425
                                }
3426
                            }
3427
                        }
3428
                    }
3429
                }
3430
                else if (owner != null && owner.GetType() == typeof(Line))
3431
                {
3432
                    Line line = owner as Line;
3433
                    Dictionary<LMConnector, List<double[]>> connectorVertices = GetPipeRunVertices(line.SPPID.ModelItemId);
3434
                    connectedLMConnector = FindTargetLMConnectorForLabel(connectorVertices, text.SPPID.ORIGINAL_X, text.SPPID.ORIGINAL_Y);
3435

    
3436
                    if (connectedLMConnector != null)
3437
                    {
3438
                        BaseModel.Attribute attribute = line.ATTRIBUTES.Find(x => x.ASSOCITEM == text.UID);
3439
                        if (attribute != null && !string.IsNullOrEmpty(attribute.VALUE) && attribute.VALUE != "None")
3440
                        {
3441
                            AttributeMapping mapping = document.AttributeMappings.Find(x => x.UID == attribute.UID && !string.IsNullOrEmpty(x.SPPIDSYMBOLNAME));
3442

    
3443
                            if (mapping != null)
3444
                            {
3445
                                double x = 0;
3446
                                double y = 0;
3447

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

    
3451
                                LMLabelPersist _LMLabelPersist = _placement.PIDPlaceLabel(mapping.SPPIDSYMBOLNAME, ref array, Rotation: text.ANGLE, LabeledItem: connectedLMConnector.AsLMRepresentation(), IsLeaderVisible: mapping.LeaderLine);
3452
                                if (_LMLabelPersist != null)
3453
                                {
3454
                                    text.SPPID.RepresentationId = _LMLabelPersist.AsLMRepresentation().Id;
3455
                                    _LMLabelPersist.Commit();
3456
                                    ReleaseCOMObjects(_LMLabelPersist);
3457
                                }
3458
                            }
3459
                        }
3460
                    }
3461
                }
3462
            }
3463
            else
3464
            {
3465
                LMItemNote _LMItemNote = null;
3466
                LMAAttribute _LMAAttribute = null;
3467

    
3468
                double x = 0;
3469
                double y = 0;
3470
                double angle = text.ANGLE;
3471
                CalcLabelLocation(ref x, ref y, text.SPPID.ORIGINAL_X, text.SPPID.ORIGINAL_Y, text.SPPIDLabelLocation, _ETCSetting.TextLocation);
3472

    
3473
                _LMSymbol = _placement.PIDPlaceSymbol(text.SPPID.MAPPINGNAME, x, y, Rotation: angle);
3474
                if (_LMSymbol != null)
3475
                {
3476
                    _LMSymbol.Commit();
3477
                    _LMItemNote = _placement.PIDDataSource.GetItemNote(_LMSymbol.ModelItemID);
3478
                    if (_LMItemNote != null)
3479
                    {
3480
                        _LMItemNote.Commit();
3481
                        _LMAAttribute = _LMItemNote.Attributes["Note.Body"];
3482
                        if (_LMAAttribute != null)
3483
                        {
3484
                            _LMAAttribute.set_Value(text.VALUE);
3485
                            text.SPPID.RepresentationId = _LMSymbol.AsLMRepresentation().Id;
3486
                            _LMItemNote.Commit();
3487

    
3488
                            if (_LMAAttribute != null)
3489
                                ReleaseCOMObjects(_LMAAttribute);
3490
                            if (_LMItemNote != null)
3491
                                ReleaseCOMObjects(_LMItemNote);
3492
                        }
3493
                    }
3494
                }
3495
            }
3496
            if (_LMSymbol != null)
3497
                ReleaseCOMObjects(_LMSymbol);
3498
        }
3499

    
3500
        /// <summary>
3501
        /// Note Modeling
3502
        /// </summary>
3503
        /// <param name="note"></param>
3504
        private void NoteModeling(Note note)
3505
        {
3506
            LMSymbol _LMSymbol = null;
3507
            LMItemNote _LMItemNote = null;
3508
            LMAAttribute _LMAAttribute = null;
3509

    
3510
            if (string.IsNullOrEmpty(note.OWNER) || note.OWNER == "None")
3511
            {
3512
                double x = 0;
3513
                double y = 0;
3514

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

    
3517
                _LMSymbol = _placement.PIDPlaceSymbol(note.SPPID.MAPPINGNAME, x, y);
3518
                if (_LMSymbol != null)
3519
                {
3520
                    _LMSymbol.Commit();
3521
                    _LMItemNote = _placement.PIDDataSource.GetItemNote(_LMSymbol.ModelItemID);
3522
                    if (_LMItemNote != null)
3523
                    {
3524
                        _LMItemNote.Commit();
3525
                        _LMAAttribute = _LMItemNote.Attributes["Note.Body"];
3526
                        if (_LMAAttribute != null)
3527
                        {
3528
                            _LMAAttribute.set_Value(note.VALUE);
3529
                            note.SPPID.RepresentationId = _LMSymbol.AsLMRepresentation().Id;
3530
                            _LMItemNote.Commit();
3531
                        }
3532
                    }
3533
                }
3534
            }
3535

    
3536
            if (_LMAAttribute != null)
3537
                ReleaseCOMObjects(_LMAAttribute);
3538
            if (_LMItemNote != null)
3539
                ReleaseCOMObjects(_LMItemNote);
3540
            if (_LMSymbol != null)
3541
                ReleaseCOMObjects(_LMSymbol);
3542
        }
3543

    
3544
        private void JoinRunBySameType(string modelItemId, ref string survivorId)
3545
        {
3546
            LMModelItem modelItem = dataSource.GetModelItem(modelItemId);
3547
            if (modelItem != null)
3548
            {
3549
                foreach (LMRepresentation rep in modelItem.Representations)
3550
                {
3551
                    if (rep.Attributes["RepresentationType"].get_Value() == "Connector" && rep.Attributes["ItemStatus"].get_Value() == "Active")
3552
                    {
3553
                        LMConnector connector = dataSource.GetConnector(rep.Id);
3554
                        if (connector.ConnectItem1SymbolObject != null && connector.ConnectItem1SymbolObject.get_RepresentationType() != "Branch")
3555
                        {
3556
                            LMSymbol symbol = connector.ConnectItem1SymbolObject;
3557
                            List<string> modelItemIds = FindOtherModelItemBySymbolWhereTypePipeRun(symbol, modelItem.Id);
3558
                            if (modelItemIds.Count == 1)
3559
                            {
3560
                                string joinModelItemId = modelItemIds[0];
3561
                                CheckZeroLengthAndLabel(ref joinModelItemId);
3562
                                JoinRun(joinModelItemId, modelItemId, ref survivorId);
3563
                                if (survivorId != null)
3564
                                    break;
3565
                            }
3566
                        }
3567
                        if (connector.ConnectItem2SymbolObject != null && connector.ConnectItem2SymbolObject.get_RepresentationType() != "Branch")
3568
                        {
3569
                            LMSymbol symbol = connector.ConnectItem2SymbolObject;
3570
                            List<string> modelItemIds = FindOtherModelItemBySymbolWhereTypePipeRun(symbol, modelItem.Id);
3571
                            if (modelItemIds.Count == 1)
3572
                            {
3573
                                string joinModelItemId = modelItemIds[0];
3574
                                CheckZeroLengthAndLabel(ref joinModelItemId);
3575
                                JoinRun(joinModelItemId, modelItemId, ref survivorId);
3576
                                if (survivorId != null)
3577
                                    break;
3578
                            }
3579
                        }
3580
                    }
3581
                }
3582
            }
3583

    
3584
            void CheckZeroLengthAndLabel(ref string _modelItemId)
3585
            {
3586
                LMConnector connector = GetLMConnectorOnlyOne(_modelItemId);
3587
                if (connector != null && Convert.ToBoolean(connector.get_IsZeroLength()) && connector.LabelPersists.Count == 0)
3588
                {
3589
                    string symbolPath = GetSPPIDFileName(modelItem);
3590
                    LMConnector newConnector = ReModelingLMConnector(connector, symbolPath);
3591
                    ReleaseCOMObjects(connector);
3592
                    _modelItemId = newConnector.ModelItemID;
3593
                    ReleaseCOMObjects(newConnector);
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 CompareNotSegmentLine(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 CompareConnSymbol(Line a, Line b)
3752
            {
3753
                List<Connector> connectorsA = a.CONNECTORS
3754
                    .Where(conn => conn.ConnectedObject != null && conn.ConnectedObject.GetType() == typeof(Symbol))
3755
                    .ToList();
3756

    
3757
                List<Connector> connectorsB = b.CONNECTORS
3758
                    .Where(conn => conn.ConnectedObject != null && conn.ConnectedObject.GetType() == typeof(Symbol))
3759
                    .ToList();
3760

    
3761
                // 오름차순
3762
                return connectorsB.Count.CompareTo(connectorsA.Count);
3763
            }
3764

    
3765
            int CompareConnItem(Line a, Line b)
3766
            {
3767
                List<Connector> connectorsA = a.CONNECTORS
3768
                    .Where(conn => conn.ConnectedObject != null && 
3769
                    (conn.ConnectedObject.GetType() == typeof(Symbol) || 
3770
                    (conn.ConnectedObject.GetType() == typeof(Line) && !SPPIDUtil.IsBranchLine((Line)conn.ConnectedObject, a))))
3771
                    .ToList();
3772

    
3773
                List<Connector> connectorsB = b.CONNECTORS
3774
                    .Where(conn => conn.ConnectedObject != null &&
3775
                    (conn.ConnectedObject.GetType() == typeof(Symbol) ||
3776
                    (conn.ConnectedObject.GetType() == typeof(Line) && !SPPIDUtil.IsBranchLine((Line)conn.ConnectedObject, b))))
3777
                    .ToList();
3778

    
3779
                // 오름차순
3780
                return connectorsB.Count.CompareTo(connectorsA.Count);
3781
            }
3782

    
3783
            int CompareBranchLine(Line a, Line b)
3784
            {
3785
                List<Connector> connectorsA = a.CONNECTORS
3786
                    .Where(conn => conn.ConnectedObject != null && conn.ConnectedObject.GetType() == typeof(Line) && SPPIDUtil.IsBranchLine(a, conn.ConnectedObject as Line))
3787
                    .ToList();
3788
                List<Connector> connectorsB = b.CONNECTORS
3789
                    .Where(conn => conn.ConnectedObject != null && conn.ConnectedObject.GetType() == typeof(Line) && SPPIDUtil.IsBranchLine(b, conn.ConnectedObject as Line))
3790
                    .ToList();
3791

    
3792
                // 내림차순
3793
                return connectorsA.Count.CompareTo(connectorsB.Count);
3794
            }
3795

    
3796
            int CompareNoneConn(Line a, Line b)
3797
            {
3798
                List<Connector> connectorsA = a.CONNECTORS
3799
                    .Where(conn => conn.ConnectedObject == null)
3800
                    .ToList();
3801

    
3802
                List<Connector> connectorsB = b.CONNECTORS
3803
                    .Where(conn => conn.ConnectedObject == null)
3804
                    .ToList();
3805

    
3806
                // 오름차순
3807
                return connectorsB.Count.CompareTo(connectorsA.Count);
3808
            }
3809
        }
3810

    
3811
        private void SortBranchLines()
3812
        {
3813
            BranchLines.Sort(SortBranchLine);
3814
            int SortBranchLine(Line a, Line b)
3815
            {
3816
                int countA = a.CONNECTORS.FindAll(x => x.ConnectedObject != null &&
3817
                 x.ConnectedObject.GetType() == typeof(Line) &&
3818
                 SPPIDUtil.IsBranchLine(x.ConnectedObject as Line, a) &&
3819
                 string.IsNullOrEmpty(((Line)x.ConnectedObject).SPPID.ModelItemId)).Count;
3820

    
3821
                int countB = b.CONNECTORS.FindAll(x => x.ConnectedObject != null &&
3822
                 x.ConnectedObject.GetType() == typeof(Line) &&
3823
                 SPPIDUtil.IsBranchLine(x.ConnectedObject as Line, b) &&
3824
                 string.IsNullOrEmpty(((Line)x.ConnectedObject).SPPID.ModelItemId)).Count;
3825

    
3826
                // 내림차순
3827
                return countA.CompareTo(countB);
3828
            }
3829
        }
3830

    
3831
        private static int SortSymbolPriority(Symbol a, Symbol b)
3832
        {
3833
            int countA = a.CONNECTORS.FindAll(x => !string.IsNullOrEmpty(x.CONNECTEDITEM) && x.CONNECTEDITEM != "None").Count;
3834
            int countB = b.CONNECTORS.FindAll(x => !string.IsNullOrEmpty(x.CONNECTEDITEM) && x.CONNECTEDITEM != "None").Count;
3835
            int retval = countB.CompareTo(countA);
3836
            if (retval != 0)
3837
                return retval;
3838
            else
3839
                return a.SPPID.ORIGINAL_X.CompareTo(b.SPPID.ORIGINAL_X);
3840
        }
3841

    
3842
        private string GetSPPIDFileName(LMModelItem modelItem)
3843
        {
3844
            string symbolPath = null;
3845
            foreach (LMRepresentation rep in modelItem.Representations)
3846
            {
3847
                if (!DBNull.Value.Equals(rep.get_FileName()) && !string.IsNullOrEmpty(rep.get_FileName()))
3848
                {
3849
                    symbolPath = rep.get_FileName();
3850
                    break;
3851
                }
3852
            }
3853
            return symbolPath;
3854
        }
3855

    
3856
        /// <summary>
3857
        /// Graphic OID로 해당 Symbol의 크기를 구하여 Zoom
3858
        /// </summary>
3859
        /// <param name="graphicOID"></param>
3860
        /// <param name="milliseconds"></param>
3861
        private void ZoomObjectByGraphicOID(string graphicOID, int milliseconds = 150)
3862
        {
3863
            if (radApp.ActiveDocument.ActiveSheet.DrawingObjects[graphicOID] != null)
3864
            {
3865
                double minX = 0;
3866
                double minY = 0;
3867
                double maxX = 0;
3868
                double maxY = 0;
3869
                radApp.ActiveDocument.ActiveSheet.DrawingObjects[graphicOID].Range(out minX, out minY, out maxX, out maxY);
3870
                radApp.ActiveWindow.ZoomArea2(minX - 0.007, minY - 0.007, maxX + 0.007, maxY + 0.007, null);
3871

    
3872
                Thread.Sleep(milliseconds);
3873
            }
3874
        }
3875

    
3876
        /// <summary>
3877
        /// ComObject를 Release
3878
        /// </summary>
3879
        /// <param name="objVars"></param>
3880
        public void ReleaseCOMObjects(params object[] objVars)
3881
        {
3882
            int intNewRefCount = 0;
3883
            foreach (object obj in objVars)
3884
            {
3885
                if (!Information.IsNothing(obj) && System.Runtime.InteropServices.Marshal.IsComObject(obj))
3886
                    intNewRefCount = intNewRefCount + System.Runtime.InteropServices.Marshal.FinalReleaseComObject(obj);
3887
            }
3888
        }
3889
    }
3890
}
클립보드 이미지 추가 (최대 크기: 500 MB)