프로젝트

일반

사용자정보

통계
| 개정판:

hytos / DTI_PID / SPPIDConverter / AutoModeling.cs @ d77973b3

이력 | 보기 | 이력해설 | 다운로드 (177 KB)

1
using System;
2
using System.Collections.Generic;
3
using System.Linq;
4
using System.Text;
5
using System.Threading.Tasks;
6
using System.Data;
7
using Llama;
8
using Plaice;
9
using Ingr.RAD2D.Interop.RAD2D;
10
using Ingr.RAD2D.Internal;
11
using Ingr.RAD2D.Helper;
12
using Converter.BaseModel;
13
using Converter.SPPID.Model;
14
using Converter.SPPID.Properties;
15
using Converter.SPPID.Util;
16
using Converter.SPPID.DB;
17
using Ingr.RAD2D.MacroControls.CmdCtrl;
18
using Ingr.RAD2D;
19
using System.Windows;
20
using System.Threading;
21
using System.Drawing;
22
using Microsoft.VisualBasic;
23
using Newtonsoft.Json;
24
using DevExpress.XtraSplashScreen;
25
namespace Converter.SPPID
26
{
27
    public class AutoModeling
28
    {
29
        Placement _placement;
30
        LMADataSource dataSource;
31
        LMDrawing currentDrawing;
32
        dynamic newDrawing;
33
        dynamic application;
34
        Ingr.RAD2D.Application radApp;
35
        SPPID_Document document;
36
        ETCSetting _ETCSetting;
37

    
38
        public string DocumentLabelText { get; set; }
39

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

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

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

    
67
                }
68
            }
69
        }
70

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

    
83
                CreateDocument(ref drawingNumber, ref drawingName);
84

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

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

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

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

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

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

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

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

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

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

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

    
464
                        SetFlowDirectionByLine(line.SPPID.ModelItemId);
465

    
466
                        ReleaseCOMObjects(modelItem);
467
                    }
468
                }
469

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

    
482
                SetFlowDirectionByLine(modelId);
483

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

    
497
                SetFlowDirectionByLine(modelId);
498

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
769
            ReleaseCOMObjects(drawings);
770
        }
771

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

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

    
809
            LMSymbol _LMSymbol = null;
810

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

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

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

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

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

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

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

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

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

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

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

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

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

    
879
                ReleaseCOMObjects(_LMSymbol);
880
            }
881
        }
882

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

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

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

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

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

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

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

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

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

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

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

    
987
            ReleaseCOMObjects(_TargetItem);
988
        }
989

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

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

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

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

    
1034
            ReleaseCOMObjects(_TargetItem);
1035
        }
1036

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

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

    
1053
                ReleaseCOMObjects(_TargetItem);
1054
            }
1055
        }
1056

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

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

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

    
1078
                ReleaseCOMObjects(_TargetItem);
1079
            }
1080

    
1081
            range = tempRange;
1082
        }
1083

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

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

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

    
1118
                ReleaseCOMObjects(_ChildSymbol);
1119
            }
1120
        }
1121

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

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

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

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

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

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

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

    
1215
                                        if (result)
1216
                                            break;
1217
                                    }
1218

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

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

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

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

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

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

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

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

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

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

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

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

    
1329
            ReleaseCOMObjects(_LMSymbol);
1330
        }
1331

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

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

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

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

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

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

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

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

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

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

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

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

    
1529

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

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

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

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

    
1622
                            placeRunInputs.AddPoint(x, y);
1623

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

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

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

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

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

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

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

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

    
1686
                if (isBranchModeling && NewBranchLines.Contains(groupLine))
1687
                    NewBranchLines.Remove(groupLine);
1688
            }
1689
        }
1690

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

    
1719
                ReleaseCOMObjects(modelItem);
1720
            }
1721
        }
1722

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
2065
            ReleaseCOMObjects(modelItem);
2066
        }
2067

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

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

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

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

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

    
2131
                    break;
2132
                }
2133
            }
2134

    
2135
            return _LMSymbol;
2136
        }
2137

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

    
2154
            return null;
2155
        }
2156

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

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

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

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

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

    
2214
                placeRunInputs.AddSymbolTarget(connector.ConnectItem1SymbolObject, x, y);
2215
                placeRunInputs.AddSymbolTarget(connector.ConnectItem2SymbolObject, x, y);
2216

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

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

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

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

    
2262
                _placement.PIDRemovePlacement(connector.AsLMRepresentation());
2263
                newConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
2264

    
2265
                foreach (var line in lines)
2266
                    line.SPPID.ModelItemId = newConnector.ModelItemID;
2267
            }
2268

    
2269

    
2270
            return newConnector;
2271
        }
2272

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

    
2282
            if (upStreamObj != null &&
2283
                downStreamObj != null)
2284
            {
2285
                LMConnector targetLMConnector = FindBreakLineTarget(upStreamObj, downStreamObj);
2286

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

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

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

    
2325
                foreach (LMConnector connector in targetLMSymbol.Avoid1Connectors)
2326
                {
2327
                    if (connector.get_ItemStatus() != "Active")
2328
                        continue;
2329

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

    
2342
                foreach (LMConnector connector in targetLMSymbol.Avoid2Connectors)
2343
                {
2344
                    if (connector.get_ItemStatus() != "Active")
2345
                        continue;
2346

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

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

    
2367
                if (targetModelItem != null && targetModelItem.get_ItemStatus() == "Active" && connectedModelItem != null && connectedModelItem.get_ItemStatus() == "Active")
2368
                {
2369
                    foreach (LMRepresentation rep in targetModelItem.Representations)
2370
                    {
2371
                        if (targetConnector != null)
2372
                            break;
2373

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

    
2378
                            if (IsConnected(_LMConnector, connectedModelItem))
2379
                                targetConnector = _LMConnector;
2380
                            else
2381
                                ReleaseCOMObjects(_LMConnector);
2382
                        }
2383
                    }
2384

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

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

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

    
2415
                        if (IsConnected(connector, targetModelItem))
2416
                        {
2417
                            targetConnector = connector;
2418
                            break;
2419
                        }
2420
                    }
2421

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

    
2429
                            if (IsConnected(connector, targetModelItem))
2430
                            {
2431
                                targetConnector = connector;
2432
                                break;
2433
                            }
2434
                        }
2435
                    }
2436
                }
2437

    
2438
            }
2439

    
2440
            return targetConnector;
2441
        }
2442

    
2443
        private bool IsConnected(LMConnector connector, LMModelItem modelItem)
2444
        {
2445
            bool result = false;
2446

    
2447
            foreach (LMRepresentation rep in modelItem.Representations)
2448
            {
2449
                if (result)
2450
                    break;
2451

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

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

    
2489
                    ReleaseCOMObjects(_LMConnector);
2490
                }
2491
            }
2492

    
2493

    
2494
            return result;
2495
        }
2496

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

    
2511
                // item2가 item1으로 조인
2512
                _placement.PIDJoinRuns(ref item1, ref item2);
2513
                item1.Commit();
2514
                item2.Commit();
2515

    
2516
                string beforeID = string.Empty;
2517
                string afterID = string.Empty;
2518

    
2519
                if (modelItem1.get_ItemStatus() == "Active" && modelItem2.get_ItemStatus() != "Active")
2520
                {
2521
                    beforeID = modelItem2.Id;
2522
                    afterID = modelItem1.Id;
2523
                    survivorId = afterID;
2524
                }
2525
                else if (modelItem1.get_ItemStatus() != "Active" && modelItem2.get_ItemStatus() == "Active")
2526
                {
2527
                    beforeID = modelItem1.Id;
2528
                    afterID = modelItem2.Id;
2529
                    survivorId = afterID;
2530
                }
2531
                else if (modelItem1.get_ItemStatus() == "Active" && modelItem2.get_ItemStatus() == "Active")
2532
                    survivorId = null;
2533
                else
2534
                {
2535
                    Log.Write("잘못된 경우");
2536
                    survivorId = null;
2537
                }
2538

    
2539

    
2540
                if (!string.IsNullOrEmpty(beforeID) && !string.IsNullOrEmpty(afterID))
2541
                {
2542
                    List<Line> lines = SPPIDUtil.FindLinesByModelId(document, beforeID);
2543
                    foreach (var line in lines)
2544
                        line.SPPID.ModelItemId = afterID;
2545
                }
2546

    
2547
                ReleaseCOMObjects(modelItem1);
2548
                ReleaseCOMObjects(item1);
2549
                ReleaseCOMObjects(modelItem2);
2550
                ReleaseCOMObjects(item2);
2551
            }
2552
            catch (Exception ex)
2553
            {
2554
                Log.Write("Join Error");
2555
                Log.Write(ex.Message);
2556
            }
2557
        }
2558

    
2559
        private List<string> FindOtherModelItemBySymbolWhereTypePipeRun(LMSymbol symbol, string modelId)
2560
        {
2561
            List<string> modelItemIDs = new List<string>();
2562
            foreach (LMConnector connector in symbol.Connect1Connectors)
2563
            {
2564
                LMModelItem modelItem = connector.ModelItemObject;
2565
                if (modelItem.get_ItemStatus() == "Active" && modelItem.get_ItemTypeName().ToString() == "PipeRun" && modelItem.Id != modelId)
2566
                    modelItemIDs.Add(modelItem.Id);
2567
                ReleaseCOMObjects(modelItem);
2568
                ReleaseCOMObjects(connector);
2569
            }
2570

    
2571
            foreach (LMConnector connector in symbol.Connect2Connectors)
2572
            {
2573
                LMModelItem modelItem = connector.ModelItemObject;
2574
                if (modelItem.get_ItemStatus() == "Active" && modelItem.get_ItemTypeName().ToString() == "PipeRun" && modelItem.Id != modelId)
2575
                    modelItemIDs.Add(modelItem.Id);
2576
                ReleaseCOMObjects(modelItem);
2577
                ReleaseCOMObjects(connector);
2578
            }
2579

    
2580
            return modelItemIDs;
2581
        }
2582

    
2583
        /// <summary>
2584
        /// PipeRun의 좌표를 가져오는 메서드
2585
        /// </summary>
2586
        /// <param name="modelId"></param>
2587
        /// <returns></returns>
2588
        private Dictionary<LMConnector, List<double[]>> GetPipeRunVertices(string modelId)
2589
        {
2590
            Dictionary<LMConnector, List<double[]>> connectorVertices = new Dictionary<LMConnector, List<double[]>>();
2591
            LMModelItem modelItem = dataSource.GetModelItem(modelId);
2592

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

    
2617
                ReleaseCOMObjects(modelItem);
2618
            }
2619

    
2620
            return connectorVertices;
2621
        }
2622

    
2623
        private List<double[]> GetConnectorVertices(LMConnector connector)
2624
        {
2625
            List<double[]> vertices = new List<double[]>();
2626
            dynamic OID = connector.get_GraphicOID().ToString();
2627
            DependencyObject drawingObject = radApp.ActiveDocument.ActiveSheet.DrawingObjects[OID];
2628
            Ingr.RAD2D.LineStringGeometry2d lineStringGeometry = drawingObject.GetGeometry() as Ingr.RAD2D.LineStringGeometry2d;
2629
            int verticesCount = lineStringGeometry.VertexCount;
2630
            double[] value = null;
2631
            lineStringGeometry.GetVertices(ref verticesCount, ref value);
2632
            for (int i = 0; i < verticesCount; i++)
2633
            {
2634
                double x = 0;
2635
                double y = 0;
2636
                lineStringGeometry.GetVertex(i + 1, ref x, ref y);
2637
                vertices.Add(new double[] { x, y });
2638
            }
2639
            return vertices;
2640
        }
2641

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

    
2665
                    if ((x1 <= connX && x2 >= connX) ||
2666
                        (y1 <= connY && y2 >= connY))
2667
                    {
2668
                        double distance = SPPIDUtil.CalcPointToPointdDistance(point1[0], point1[1], connX, connY);
2669
                        if (length >= distance)
2670
                        {
2671
                            targetConnector = item.Key;
2672
                            length = distance;
2673
                        }
2674

    
2675
                        distance = SPPIDUtil.CalcPointToPointdDistance(point2[0], point2[1], connX, connY);
2676
                        if (length >= distance)
2677
                        {
2678
                            targetConnector = item.Key;
2679
                            length = distance;
2680
                        }
2681
                    }
2682
                }
2683
            }
2684

    
2685
            // 못찾았을때.
2686
            length = double.MaxValue;
2687
            if (targetConnector == null)
2688
            {
2689
                foreach (var item in connectorVertices)
2690
                {
2691
                    List<double[]> points = item.Value;
2692

    
2693
                    foreach (double[] point in points)
2694
                    {
2695
                        double distance = SPPIDUtil.CalcPointToPointdDistance(point[0], point[1], connX, connY);
2696
                        if (length >= distance)
2697
                        {
2698
                            targetConnector = item.Key;
2699
                            length = distance;
2700
                        }
2701
                    }
2702
                }
2703
            }
2704

    
2705
            return targetConnector;
2706
        }
2707

    
2708
        private LMConnector FindTargetLMConnectorForBranch(Line line, Line targetLine, ref double x, ref double y)
2709
        {
2710
            Dictionary<LMConnector, List<double[]>> vertices = GetPipeRunVertices(targetLine.SPPID.ModelItemId);
2711
            if (vertices.Count == 0)
2712
                return null;
2713

    
2714
            double length = double.MaxValue;
2715
            LMConnector targetConnector = null;
2716
            double[] resultPoint = null;
2717
            List<double[]> targetVertices = null;
2718

    
2719
            // Vertices 포인트에 제일 가까운곳
2720
            foreach (var item in vertices)
2721
            {
2722
                List<double[]> points = item.Value;
2723
                for (int i = 0; i < points.Count; i++)
2724
                {
2725
                    double[] point = points[i];
2726
                    double tempX = point[0];
2727
                    double tempY = point[1];
2728

    
2729
                    double distance = SPPIDUtil.CalcPointToPointdDistance(tempX, tempY, x, y);
2730
                    if (length >= distance)
2731
                    {
2732
                        targetConnector = item.Key;
2733
                        length = distance;
2734
                        resultPoint = point;
2735
                        targetVertices = item.Value;
2736
                    }
2737
                }
2738
            }
2739

    
2740
            // Vertices Cross에 제일 가까운곳
2741
            foreach (var item in vertices)
2742
            {
2743
                List<double[]> points = item.Value;
2744
                for (int i = 0; i < points.Count - 1; i++)
2745
                {
2746
                    double[] point1 = points[i];
2747
                    double[] point2 = points[i + 1];
2748

    
2749
                    double maxLineX = Math.Max(point1[0], point2[0]);
2750
                    double minLineX = Math.Min(point1[0], point2[0]);
2751
                    double maxLineY = Math.Max(point1[1], point2[1]);
2752
                    double minLineY = Math.Min(point1[1], point2[1]);
2753

    
2754
                    SlopeType slope = SPPIDUtil.CalcSlope(minLineX, minLineY, maxLineX, maxLineY);
2755

    
2756
                    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]);
2757
                    if (crossingPoint != null)
2758
                    {
2759
                        double distance = SPPIDUtil.CalcPointToPointdDistance(crossingPoint[0], crossingPoint[1], x, y);
2760
                        if (length >= distance)
2761
                        {
2762
                            if (slope == SlopeType.Slope &&
2763
                                minLineX <= crossingPoint[0] && maxLineX >= crossingPoint[0] &&
2764
                                minLineY <= crossingPoint[1] && maxLineY >= crossingPoint[1])
2765
                            {
2766
                                targetConnector = item.Key;
2767
                                length = distance;
2768
                                resultPoint = crossingPoint;
2769
                                targetVertices = item.Value;
2770
                            }
2771
                            else if (slope == SlopeType.HORIZONTAL &&
2772
                                minLineX <= crossingPoint[0] && maxLineX >= crossingPoint[0])
2773
                            {
2774
                                targetConnector = item.Key;
2775
                                length = distance;
2776
                                resultPoint = crossingPoint;
2777
                                targetVertices = item.Value;
2778
                            }
2779
                            else if (slope == SlopeType.VERTICAL &&
2780
                               minLineY <= crossingPoint[1] && maxLineY >= crossingPoint[1])
2781
                            {
2782
                                targetConnector = item.Key;
2783
                                length = distance;
2784
                                resultPoint = crossingPoint;
2785
                                targetVertices = item.Value;
2786
                            }
2787
                        }
2788
                    }
2789
                }
2790
            }
2791

    
2792
            foreach (var item in vertices)
2793
                if (item.Key != null && item.Key != targetConnector)
2794
                    ReleaseCOMObjects(item.Key);
2795

    
2796
            if (SPPIDUtil.IsBranchLine(line, targetLine))
2797
            {
2798
                double tempResultX = resultPoint[0];
2799
                double tempResultY = resultPoint[1];
2800
                SPPIDUtil.ConvertGridPoint(ref tempResultX, ref tempResultY);
2801

    
2802
                GridSetting gridSetting = GridSetting.GetInstance();
2803

    
2804
                for (int i = 0; i < targetVertices.Count; i++)
2805
                {
2806
                    double[] point = targetVertices[i];
2807
                    double tempX = targetVertices[i][0];
2808
                    double tempY = targetVertices[i][1];
2809
                    SPPIDUtil.ConvertGridPoint(ref tempX, ref tempY);
2810
                    if (tempX == tempResultX && tempY == tempResultY)
2811
                    {
2812
                        if (i == 0)
2813
                        {
2814
                            LMSymbol connSymbol = targetConnector.ConnectItem1SymbolObject;
2815
                            bool containZeroLength = false;
2816
                            if (connSymbol != null)
2817
                            {
2818
                                foreach (LMConnector connector in connSymbol.Connect1Connectors)
2819
                                {
2820
                                    if (connector.get_ItemStatus() == "Active" && Convert.ToBoolean(connector.get_IsZeroLength()) == true)
2821
                                        containZeroLength = true;
2822
                                }
2823
                                foreach (LMConnector connector in connSymbol.Connect2Connectors)
2824
                                {
2825
                                    if (connector.get_ItemStatus() == "Active" && Convert.ToBoolean(connector.get_IsZeroLength()) == true)
2826
                                        containZeroLength = true;
2827
                                }
2828
                            }
2829

    
2830
                            if (connSymbol == null ||
2831
                                (connSymbol != null && connSymbol.get_ItemStatus() == "Active" && connSymbol.get_RepresentationType() != "Branch") ||
2832
                                containZeroLength)
2833
                            {
2834
                                bool bCalcX = false;
2835
                                bool bCalcY = false;
2836
                                if (targetLine.SlopeType == SlopeType.HORIZONTAL)
2837
                                    bCalcX = true;
2838
                                else if (targetLine.SlopeType == SlopeType.VERTICAL)
2839
                                    bCalcY = true;
2840
                                else
2841
                                {
2842
                                    bCalcX = true;
2843
                                    bCalcY = true;
2844
                                }
2845

    
2846
                                if (bCalcX)
2847
                                {
2848
                                    double nextX = targetVertices[i + 1][0];
2849
                                    double newX = 0;
2850
                                    if (nextX > tempX)
2851
                                    {
2852
                                        newX = tempX + gridSetting.Length;
2853
                                        if (newX > nextX)
2854
                                            newX = (point[0] + nextX) / 2;
2855
                                    }
2856
                                    else
2857
                                    {
2858
                                        newX = tempX - gridSetting.Length;
2859
                                        if (newX < nextX)
2860
                                            newX = (point[0] + nextX) / 2;
2861
                                    }
2862
                                    resultPoint = new double[] { newX, resultPoint[1] };
2863
                                }
2864

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

    
2903
                            if (connSymbol == null ||
2904
                                 (connSymbol != null && connSymbol.get_ItemStatus() == "Active" && connSymbol.get_RepresentationType() != "Branch") ||
2905
                                containZeroLength)
2906
                            {
2907
                                bool bCalcX = false;
2908
                                bool bCalcY = false;
2909
                                if (targetLine.SlopeType == SlopeType.HORIZONTAL)
2910
                                    bCalcX = true;
2911
                                else if (targetLine.SlopeType == SlopeType.VERTICAL)
2912
                                    bCalcY = true;
2913
                                else
2914
                                {
2915
                                    bCalcX = true;
2916
                                    bCalcY = true;
2917
                                }
2918

    
2919
                                if (bCalcX)
2920
                                {
2921
                                    double nextX = targetVertices[i - 1][0];
2922
                                    double newX = 0;
2923
                                    if (nextX > tempX)
2924
                                    {
2925
                                        newX = tempX + gridSetting.Length;
2926
                                        if (newX > nextX)
2927
                                            newX = (point[0] + nextX) / 2;
2928
                                    }
2929
                                    else
2930
                                    {
2931
                                        newX = tempX - gridSetting.Length;
2932
                                        if (newX < nextX)
2933
                                            newX = (point[0] + nextX) / 2;
2934
                                    }
2935
                                    resultPoint = new double[] { newX, resultPoint[1] };
2936
                                }
2937

    
2938
                                if (bCalcY)
2939
                                {
2940
                                    double nextY = targetVertices[i - 1][1];
2941
                                    double newY = 0;
2942
                                    if (nextY > tempY)
2943
                                    {
2944
                                        newY = tempY + gridSetting.Length;
2945
                                        if (newY > nextY)
2946
                                            newY = (point[1] + nextY) / 2;
2947
                                    }
2948
                                    else
2949
                                    {
2950
                                        newY = tempY - gridSetting.Length;
2951
                                        if (newY < nextY)
2952
                                            newY = (point[1] + nextY) / 2;
2953
                                    }
2954
                                    resultPoint = new double[] { resultPoint[0], newY };
2955
                                }
2956
                            }
2957
                        }
2958
                        break;
2959
                    }
2960
                }
2961
            }
2962

    
2963
            x = resultPoint[0];
2964
            y = resultPoint[1];
2965

    
2966
            return targetConnector;
2967
        }
2968

    
2969
        private LMConnector GetLMConnectorOnlyOne(string modelItemID)
2970
        {
2971
            LMConnector result = null;
2972
            List<LMConnector> connectors = new List<LMConnector>();
2973
            LMModelItem modelItem = dataSource.GetModelItem(modelItemID);
2974

    
2975
            if (modelItem != null)
2976
            {
2977
                foreach (LMRepresentation rep in modelItem.Representations)
2978
                {
2979
                    if (rep.Attributes["RepresentationType"].get_Value() == "Connector" && rep.Attributes["ItemStatus"].get_Value() == "Active")
2980
                        connectors.Add(dataSource.GetConnector(rep.Id));
2981
                }
2982

    
2983
                ReleaseCOMObjects(modelItem);
2984
            }
2985

    
2986
            if (connectors.Count == 1)
2987
                result = connectors[0];
2988
            else
2989
                foreach (var item in connectors)
2990
                    ReleaseCOMObjects(item);
2991

    
2992
            return result;
2993
        }
2994

    
2995
        /// <summary>
2996
        /// Line Number Symbol을 실제로 Modeling하는 메서드
2997
        /// </summary>
2998
        /// <param name="lineNumber"></param>
2999
        private void LineNumberModeling(Line line)
3000
        {
3001
            LineNumber lineNumber = document.LINENUMBERS.Find(x => x.CONNLINE == line.UID);
3002
            if (lineNumber != null)
3003
            {
3004
                LMConnector connectedLMConnector = GetLMConnectorOnlyOne(line.SPPID.ModelItemId);
3005
                if (connectedLMConnector != null)
3006
                {
3007
                    double x = 0;
3008
                    double y = 0;
3009
                    CalcLabelLocation(ref x, ref y, lineNumber.SPPID.ORIGINAL_X, lineNumber.SPPID.ORIGINAL_Y, lineNumber.SPPIDLabelLocation, _ETCSetting.LineNumberLocation);
3010

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

    
3014
                    if (_LmLabelPresist != null)
3015
                    {
3016
                        _LmLabelPresist.Commit();
3017
                        lineNumber.SPPID.RepresentationId = _LmLabelPresist.AsLMRepresentation().Id;
3018
                        ReleaseCOMObjects(_LmLabelPresist);
3019
                    }
3020
                }
3021
            }
3022
        }
3023

    
3024
        /// <summary>
3025
        /// Flow Mark Modeling
3026
        /// </summary>
3027
        /// <param name="line"></param>
3028
        private void FlowMarkModeling(Line line)
3029
        {
3030
            if (line.FLOWMARK && !string.IsNullOrEmpty(line.SPPID.ModelItemId) && !string.IsNullOrEmpty(_ETCSetting.FlowMarkSymbolPath))
3031
            {
3032
                LMConnector connector = GetLMConnectorOnlyOne(line.SPPID.ModelItemId);
3033
                if (connector != null)
3034
                {
3035
                    string mappingPath = _ETCSetting.FlowMarkSymbolPath;
3036
                    List<double[]> vertices = GetConnectorVertices(connector);
3037
                    vertices = vertices.FindAll(x => x[0] > 0 && x[1] > 0);
3038
                    double[] point = vertices[vertices.Count - 1];
3039
                    Array array = new double[] { 0, point[0], point[1] };
3040
                    LMLabelPersist _LMLabelPersist = _placement.PIDPlaceLabel(mappingPath, ref array, LabeledItem: connector.AsLMRepresentation());
3041
                    if (_LMLabelPersist != null)
3042
                        ReleaseCOMObjects(_LMLabelPersist);
3043
                }
3044
            }
3045
        }
3046

    
3047
        /// <summary>
3048
        /// Line Number 기준으로 모든 Item에 Line Number의 Attribute Input
3049
        /// </summary>
3050
        /// <param name="lineNumber"></param>
3051
        private void InputLineNumberAttribute(LineNumber lineNumber)
3052
        {
3053
            foreach (LineRun run in lineNumber.RUNS)
3054
            {
3055
                foreach (var item in run.RUNITEMS)
3056
                {
3057
                    //if (item.GetType() == typeof(Symbol))
3058
                    //{
3059
                    //    Symbol symbol = item as Symbol;
3060
                    //    LMSymbol _LMSymbol = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
3061
                    //    if (_LMSymbol != null)
3062
                    //    {
3063
                    //        LMModelItem _LMModelItem = _LMSymbol.ModelItemObject;
3064

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

    
3112
                                        }
3113
                                    }
3114
                                }
3115
                                _LMModelItem.Commit();
3116
                            }
3117
                            if (_LMModelItem != null)
3118
                                ReleaseCOMObjects(_LMModelItem);
3119

    
3120
                            break;
3121
                        }
3122
                    }
3123
                }
3124
            }
3125
        }
3126

    
3127
        /// <summary>
3128
        /// Symbol Attribute 입력 메서드
3129
        /// </summary>
3130
        /// <param name="item"></param>
3131
        private void InputSymbolAttribute(object targetItem, List<BaseModel.Attribute> targetAttributes)
3132
        {
3133
            // Object 아이템이 Symbol일 경우 Equipment일 경우 
3134
            string sRep = null;
3135
            if (targetItem.GetType() == typeof(Symbol))
3136
                sRep = ((Symbol)targetItem).SPPID.RepresentationId;
3137
            else if (targetItem.GetType() == typeof(Equipment))
3138
                sRep = ((Equipment)targetItem).SPPID.RepresentationId;
3139

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

    
3175
                ReleaseCOMObjects(_Attributes);
3176
                ReleaseCOMObjects(_LMModelItem);
3177
                ReleaseCOMObjects(_LMSymbol);
3178
            }
3179
        }
3180

    
3181
        /// <summary>
3182
        /// Input SpecBreak Attribute
3183
        /// </summary>
3184
        /// <param name="specBreak"></param>
3185
        private void InputSpecBreakAttribute(SpecBreak specBreak)
3186
        {
3187
            object upStreamObj = SPPIDUtil.FindObjectByUID(document, specBreak.UpStreamUID);
3188
            object downStreamObj = SPPIDUtil.FindObjectByUID(document, specBreak.DownStreamUID);
3189

    
3190
            if (upStreamObj != null &&
3191
                downStreamObj != null)
3192
            {
3193
                LMConnector targetLMConnector = FindBreakLineTarget(upStreamObj, downStreamObj);
3194

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

    
3212
                                    InputAttributeForSpecBreak(upStreamObj, downStreamObj, upStreamValue, downStreamValue, mapping.SPPIDATTRIBUTENAME);
3213
                                }
3214
                            }
3215
                        }
3216
                    }
3217

    
3218
                    ReleaseCOMObjects(targetLMConnector);
3219
                }
3220
            }
3221

    
3222

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

    
3243
                    foreach (LMConnector connector in upStreamLMSymbol.Avoid1Connectors)
3244
                    {
3245
                        if (connector.get_ItemStatus() != "Active")
3246
                            continue;
3247

    
3248
                        if (connector.Id != zeroLenthConnector.Id)
3249
                            InputLineAttributeForSpecBreakLMConnector(connector, sppidAttributeName, upStreamValue);
3250
                    }
3251

    
3252
                    foreach (LMConnector connector in upStreamLMSymbol.Avoid2Connectors)
3253
                    {
3254
                        if (connector.get_ItemStatus() != "Active")
3255
                            continue;
3256

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

    
3261
                    foreach (LMConnector connector in downStreamLMSymbol.Avoid1Connectors)
3262
                    {
3263
                        if (connector.get_ItemStatus() != "Active")
3264
                            continue;
3265

    
3266
                        if (connector.Id != zeroLenthConnector.Id)
3267
                            InputLineAttributeForSpecBreakLMConnector(connector, sppidAttributeName, downStreamValue);
3268
                    }
3269

    
3270
                    foreach (LMConnector connector in downStreamLMSymbol.Avoid2Connectors)
3271
                    {
3272
                        if (connector.get_ItemStatus() != "Active")
3273
                            continue;
3274

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

    
3279
                    ReleaseCOMObjects(zeroLenthConnector);
3280
                    ReleaseCOMObjects(upStreamLMSymbol);
3281
                    ReleaseCOMObjects(downStreamLMSymbol);
3282
                }
3283
                else if (upStreamSymbol != null && downStreamLine != null)
3284
                {
3285
                    LMConnector zeroLenthConnector = FindBreakLineTarget(upStreamSymbol, downStreamLine);
3286
                    InputLineAttributeForSpecBreakLine(downStreamLine, sppidAttributeName, downStreamValue);
3287
                    LMSymbol upStreamLMSymbol = dataSource.GetSymbol(upStreamSymbol.SPPID.RepresentationId);
3288

    
3289
                    foreach (LMConnector connector in upStreamLMSymbol.Avoid1Connectors)
3290
                    {
3291
                        if (connector.get_ItemStatus() != "Active")
3292
                            continue;
3293

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

    
3298
                    foreach (LMConnector connector in upStreamLMSymbol.Avoid2Connectors)
3299
                    {
3300
                        if (connector.get_ItemStatus() != "Active")
3301
                            continue;
3302

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

    
3307
                    ReleaseCOMObjects(zeroLenthConnector);
3308
                    ReleaseCOMObjects(upStreamLMSymbol);
3309
                }
3310
                else if (upStreamLine != null && downStreamSymbol != null)
3311
                {
3312
                    LMConnector zeroLenthConnector = FindBreakLineTarget(upStreamLine, downStreamSymbol);
3313
                    InputLineAttributeForSpecBreakLine(upStreamLine, sppidAttributeName, upStreamValue);
3314
                    LMSymbol downStreamLMSymbol = dataSource.GetSymbol(downStreamSymbol.SPPID.RepresentationId);
3315

    
3316
                    foreach (LMConnector connector in downStreamLMSymbol.Avoid1Connectors)
3317
                    {
3318
                        if (connector.get_ItemStatus() != "Active")
3319
                            continue;
3320

    
3321
                        if (connector.Id != zeroLenthConnector.Id)
3322
                            InputLineAttributeForSpecBreakLMConnector(connector, sppidAttributeName, downStreamValue);
3323
                    }
3324

    
3325
                    foreach (LMConnector connector in downStreamLMSymbol.Avoid2Connectors)
3326
                    {
3327
                        if (connector.get_ItemStatus() != "Active")
3328
                            continue;
3329

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

    
3334
                    ReleaseCOMObjects(zeroLenthConnector);
3335
                    ReleaseCOMObjects(downStreamLMSymbol);
3336
                }
3337
            }
3338

    
3339
            void InputLineAttributeForSpecBreakLine(Line line, string attrName, string value)
3340
            {
3341
                LMModelItem _LMModelItem = dataSource.GetModelItem(line.SPPID.ModelItemId);
3342
                if (_LMModelItem != null && _LMModelItem.get_ItemStatus() == "Active")
3343
                {
3344
                    LMAAttribute _LMAAttribute = _LMModelItem.Attributes[attrName];
3345
                    if (_LMAAttribute != null)
3346
                    {
3347
                        if (DBNull.Value.Equals(_LMAAttribute.get_Value()))
3348
                            _LMAAttribute.set_Value(value);
3349
                        else if (_LMAAttribute.get_Value() != value)
3350
                            _LMAAttribute.set_Value(value);
3351
                    }
3352

    
3353
                    _LMModelItem.Commit();
3354
                }
3355
                if (_LMModelItem != null)
3356
                    ReleaseCOMObjects(_LMModelItem);
3357
            }
3358

    
3359
            void InputLineAttributeForSpecBreakLMConnector(LMConnector connector, string attrName, string value)
3360
            {
3361
                LMModelItem _LMModelItem = dataSource.GetModelItem(connector.ModelItemID);
3362
                if (_LMModelItem != null && _LMModelItem.get_ItemStatus() == "Active")
3363
                {
3364
                    LMAAttribute _LMAAttribute = _LMModelItem.Attributes[attrName];
3365
                    if (_LMAAttribute != null)
3366
                    {
3367
                        if (DBNull.Value.Equals(_LMAAttribute.get_Value()))
3368
                            _LMAAttribute.set_Value(value);
3369
                        else if (_LMAAttribute.get_Value() != value)
3370
                            _LMAAttribute.set_Value(value);
3371
                    }
3372

    
3373
                    _LMModelItem.Commit();
3374
                }
3375
                if (_LMModelItem != null)
3376
                    ReleaseCOMObjects(_LMModelItem);
3377
            }
3378
            #endregion
3379
        }
3380

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

    
3404
                            if (mapping != null)
3405
                            {
3406
                                double x = 0;
3407
                                double y = 0;
3408

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

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

    
3429
                    if (connectedLMConnector != null)
3430
                    {
3431
                        BaseModel.Attribute attribute = line.ATTRIBUTES.Find(x => x.ASSOCITEM == text.UID);
3432
                        if (attribute != null && !string.IsNullOrEmpty(attribute.VALUE) && attribute.VALUE != "None")
3433
                        {
3434
                            AttributeMapping mapping = document.AttributeMappings.Find(x => x.UID == attribute.UID && !string.IsNullOrEmpty(x.SPPIDSYMBOLNAME));
3435

    
3436
                            if (mapping != null)
3437
                            {
3438
                                double x = 0;
3439
                                double y = 0;
3440

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

    
3444
                                LMLabelPersist _LMLabelPersist = _placement.PIDPlaceLabel(mapping.SPPIDSYMBOLNAME, ref array, Rotation: text.ANGLE, LabeledItem: connectedLMConnector.AsLMRepresentation(), IsLeaderVisible: mapping.LeaderLine);
3445
                                if (_LMLabelPersist != null)
3446
                                {
3447
                                    text.SPPID.RepresentationId = _LMLabelPersist.AsLMRepresentation().Id;
3448
                                    _LMLabelPersist.Commit();
3449
                                    ReleaseCOMObjects(_LMLabelPersist);
3450
                                }
3451
                            }
3452
                        }
3453
                    }
3454
                }
3455
            }
3456
            else
3457
            {
3458
                LMItemNote _LMItemNote = null;
3459
                LMAAttribute _LMAAttribute = null;
3460

    
3461
                double x = 0;
3462
                double y = 0;
3463
                double angle = text.ANGLE;
3464
                CalcLabelLocation(ref x, ref y, text.SPPID.ORIGINAL_X, text.SPPID.ORIGINAL_Y, text.SPPIDLabelLocation, _ETCSetting.TextLocation);
3465

    
3466
                _LMSymbol = _placement.PIDPlaceSymbol(text.SPPID.MAPPINGNAME, x, y, Rotation: angle);
3467
                if (_LMSymbol != null)
3468
                {
3469
                    _LMSymbol.Commit();
3470
                    _LMItemNote = _placement.PIDDataSource.GetItemNote(_LMSymbol.ModelItemID);
3471
                    if (_LMItemNote != null)
3472
                    {
3473
                        _LMItemNote.Commit();
3474
                        _LMAAttribute = _LMItemNote.Attributes["Note.Body"];
3475
                        if (_LMAAttribute != null)
3476
                        {
3477
                            _LMAAttribute.set_Value(text.VALUE);
3478
                            text.SPPID.RepresentationId = _LMSymbol.AsLMRepresentation().Id;
3479
                            _LMItemNote.Commit();
3480

    
3481
                            if (_LMAAttribute != null)
3482
                                ReleaseCOMObjects(_LMAAttribute);
3483
                            if (_LMItemNote != null)
3484
                                ReleaseCOMObjects(_LMItemNote);
3485
                        }
3486
                    }
3487
                }
3488
            }
3489
            if (_LMSymbol != null)
3490
                ReleaseCOMObjects(_LMSymbol);
3491
        }
3492

    
3493
        /// <summary>
3494
        /// Note Modeling
3495
        /// </summary>
3496
        /// <param name="note"></param>
3497
        private void NoteModeling(Note note)
3498
        {
3499
            LMSymbol _LMSymbol = null;
3500
            LMItemNote _LMItemNote = null;
3501
            LMAAttribute _LMAAttribute = null;
3502

    
3503
            if (string.IsNullOrEmpty(note.OWNER) || note.OWNER == "None")
3504
            {
3505
                double x = 0;
3506
                double y = 0;
3507

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

    
3510
                _LMSymbol = _placement.PIDPlaceSymbol(note.SPPID.MAPPINGNAME, x, y);
3511
                if (_LMSymbol != null)
3512
                {
3513
                    _LMSymbol.Commit();
3514
                    _LMItemNote = _placement.PIDDataSource.GetItemNote(_LMSymbol.ModelItemID);
3515
                    if (_LMItemNote != null)
3516
                    {
3517
                        _LMItemNote.Commit();
3518
                        _LMAAttribute = _LMItemNote.Attributes["Note.Body"];
3519
                        if (_LMAAttribute != null)
3520
                        {
3521
                            _LMAAttribute.set_Value(note.VALUE);
3522
                            note.SPPID.RepresentationId = _LMSymbol.AsLMRepresentation().Id;
3523
                            _LMItemNote.Commit();
3524
                        }
3525
                    }
3526
                }
3527
            }
3528

    
3529
            if (_LMAAttribute != null)
3530
                ReleaseCOMObjects(_LMAAttribute);
3531
            if (_LMItemNote != null)
3532
                ReleaseCOMObjects(_LMItemNote);
3533
            if (_LMSymbol != null)
3534
                ReleaseCOMObjects(_LMSymbol);
3535
        }
3536

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

    
3577
            void CheckZeroLengthAndLabel(ref string _modelItemId)
3578
            {
3579
                LMConnector connector = GetLMConnectorOnlyOne(_modelItemId);
3580
                if (connector != null && Convert.ToBoolean(connector.get_IsZeroLength()) && connector.LabelPersists.Count == 0)
3581
                {
3582
                    string symbolPath = GetSPPIDFileName(modelItem);
3583
                    LMConnector newConnector = ReModelingLMConnector(connector, symbolPath);
3584
                    ReleaseCOMObjects(connector);
3585
                    _modelItemId = newConnector.ModelItemID;
3586
                    ReleaseCOMObjects(newConnector);
3587
                }
3588
            }
3589
        }
3590

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

    
3615
                if (location.HasFlag(Location.Left))
3616
                    x = SPPIDLabelLocation.X1;
3617
                else if (location.HasFlag(Location.Right))
3618
                    x = SPPIDLabelLocation.X2;
3619

    
3620
                if (location.HasFlag(Location.Down))
3621
                    y = SPPIDLabelLocation.Y1;
3622
                else if (location.HasFlag(Location.Up))
3623
                    y = SPPIDLabelLocation.Y2;
3624
            }
3625
        }
3626

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

    
3639
            // Angle Valve 부터
3640
            foreach (var symbol in document.SYMBOLS.FindAll(x => x.CONNECTORS.FindAll(y => y.Index == 0).Count == 2))
3641
            {
3642
                if (!symbols.Contains(symbol))
3643
                {
3644
                    double originX = 0;
3645
                    double originY = 0;
3646

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

    
3651
                    SlopeType slopeType1 = SlopeType.None;
3652
                    SlopeType slopeType2 = SlopeType.None;
3653
                    foreach (Connector connector in symbol.CONNECTORS.FindAll(x => x.Index == 0))
3654
                    {
3655
                        double connectorX = 0;
3656
                        double connectorY = 0;
3657
                        SPPIDUtil.ConvertPointBystring(connector.CONNECTPOINT, ref connectorX, ref connectorY);
3658
                        if (slopeType1 == SlopeType.None)
3659
                            slopeType1 = SPPIDUtil.CalcSlope(originX, originY, connectorX, connectorY);
3660
                        else
3661
                            slopeType2 = SPPIDUtil.CalcSlope(originX, originY, connectorX, connectorY);
3662
                    }
3663

    
3664
                    if ((slopeType1 == SlopeType.VERTICAL && slopeType2 == SlopeType.HORIZONTAL) ||
3665
                        (slopeType2 == SlopeType.VERTICAL && slopeType1 == SlopeType.HORIZONTAL))
3666
                        symbols.Add(symbol);
3667
                }
3668
            }
3669

    
3670
            List<Symbol> tempSymbols = new List<Symbol>();
3671
            // Conn 갯수 기준
3672
            foreach (var item in document.SYMBOLS)
3673
            {
3674
                if (!symbols.Contains(item))
3675
                    tempSymbols.Add(item);
3676
            }
3677
            tempSymbols.Sort(SortSymbolPriority);
3678
            symbols.AddRange(tempSymbols);
3679

    
3680
            return symbols;
3681
        }
3682

    
3683
        private void SetPriorityLine()
3684
        {
3685
            document.LINES.Sort(SortLinePriority);
3686

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

    
3722
                            }
3723
                        }
3724
                    }
3725
                }
3726

    
3727
                return 0;
3728
            }
3729

    
3730
            int CompareConnSymbol(Line a, Line b)
3731
            {
3732
                List<Connector> connectorsA = a.CONNECTORS
3733
                    .Where(conn => conn.ConnectedObject != null && conn.ConnectedObject.GetType() == typeof(Symbol))
3734
                    .ToList();
3735

    
3736
                List<Connector> connectorsB = b.CONNECTORS
3737
                    .Where(conn => conn.ConnectedObject != null && conn.ConnectedObject.GetType() == typeof(Symbol))
3738
                    .ToList();
3739

    
3740
                // 오름차순
3741
                return connectorsB.Count.CompareTo(connectorsA.Count);
3742
            }
3743

    
3744
            int CompareConnItem(Line a, Line b)
3745
            {
3746
                List<Connector> connectorsA = a.CONNECTORS
3747
                    .Where(conn => conn.ConnectedObject != null && 
3748
                    (conn.ConnectedObject.GetType() == typeof(Symbol) || 
3749
                    (conn.ConnectedObject.GetType() == typeof(Line) && !SPPIDUtil.IsBranchLine((Line)conn.ConnectedObject, a))))
3750
                    .ToList();
3751

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

    
3758
                // 오름차순
3759
                return connectorsB.Count.CompareTo(connectorsA.Count);
3760
            }
3761

    
3762
            int CompareBranchLine(Line a, Line b)
3763
            {
3764
                List<Connector> connectorsA = a.CONNECTORS
3765
                    .Where(conn => conn.ConnectedObject != null && conn.ConnectedObject.GetType() == typeof(Line) && SPPIDUtil.IsBranchLine(a, conn.ConnectedObject as Line))
3766
                    .ToList();
3767
                List<Connector> connectorsB = b.CONNECTORS
3768
                    .Where(conn => conn.ConnectedObject != null && conn.ConnectedObject.GetType() == typeof(Line) && SPPIDUtil.IsBranchLine(b, conn.ConnectedObject as Line))
3769
                    .ToList();
3770

    
3771
                // 내림차순
3772
                return connectorsA.Count.CompareTo(connectorsB.Count);
3773
            }
3774

    
3775
            int CompareNoneConn(Line a, Line b)
3776
            {
3777
                List<Connector> connectorsA = a.CONNECTORS
3778
                    .Where(conn => conn.ConnectedObject == null)
3779
                    .ToList();
3780

    
3781
                List<Connector> connectorsB = b.CONNECTORS
3782
                    .Where(conn => conn.ConnectedObject == null)
3783
                    .ToList();
3784

    
3785
                // 오름차순
3786
                return connectorsB.Count.CompareTo(connectorsA.Count);
3787
            }
3788
        }
3789

    
3790
        private void SortBranchLines()
3791
        {
3792
            NewBranchLines.Sort(SortBranchLine);
3793
            int SortBranchLine(Line a, Line b)
3794
            {
3795
                int countA = a.CONNECTORS.FindAll(x => x.ConnectedObject != null &&
3796
                 x.ConnectedObject.GetType() == typeof(Line) &&
3797
                 SPPIDUtil.IsBranchLine(x.ConnectedObject as Line, a) &&
3798
                 string.IsNullOrEmpty(((Line)x.ConnectedObject).SPPID.ModelItemId)).Count;
3799

    
3800
                int countB = b.CONNECTORS.FindAll(x => x.ConnectedObject != null &&
3801
                 x.ConnectedObject.GetType() == typeof(Line) &&
3802
                 SPPIDUtil.IsBranchLine(x.ConnectedObject as Line, b) &&
3803
                 string.IsNullOrEmpty(((Line)x.ConnectedObject).SPPID.ModelItemId)).Count;
3804

    
3805
                // 내림차순
3806
                return countA.CompareTo(countB);
3807
            }
3808
        }
3809

    
3810
        private static int SortSymbolPriority(Symbol a, Symbol b)
3811
        {
3812
            int countA = a.CONNECTORS.FindAll(x => !string.IsNullOrEmpty(x.CONNECTEDITEM) && x.CONNECTEDITEM != "None").Count;
3813
            int countB = b.CONNECTORS.FindAll(x => !string.IsNullOrEmpty(x.CONNECTEDITEM) && x.CONNECTEDITEM != "None").Count;
3814
            int retval = countB.CompareTo(countA);
3815
            if (retval != 0)
3816
                return retval;
3817
            else
3818
                return a.SPPID.ORIGINAL_X.CompareTo(b.SPPID.ORIGINAL_X);
3819
        }
3820

    
3821
        private string GetSPPIDFileName(LMModelItem modelItem)
3822
        {
3823
            string symbolPath = null;
3824
            foreach (LMRepresentation rep in modelItem.Representations)
3825
            {
3826
                if (!DBNull.Value.Equals(rep.get_FileName()) && !string.IsNullOrEmpty(rep.get_FileName()))
3827
                {
3828
                    symbolPath = rep.get_FileName();
3829
                    break;
3830
                }
3831
            }
3832
            return symbolPath;
3833
        }
3834

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

    
3851
                Thread.Sleep(milliseconds);
3852
            }
3853
        }
3854

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