프로젝트

일반

사용자정보

통계
| 개정판:

hytos / DTI_PID / SPPIDConverter / AutoModeling.cs @ d4c3e39f

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

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

    
38
        public string DocumentLabelText { get; set; }
39

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

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

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

    
66
                }
67
            }
68
        }
69

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

    
82
                CreateDocument(ref drawingNumber, ref drawingName);
83

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

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

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

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

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

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

    
152
                Project_DB.InsertDrawingInfo(document.PATH, drawingNumber, drawingName, document);
153
                if (SplashScreenManager.Default.IsSplashFormVisible)
154
                {
155
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.ClearParent, null);
156
                    SplashScreenManager.CloseForm(false);
157
                    Log.Write("\r\n");
158
                }
159
            }
160
        }
161
        private void RunEquipmentModeling()
162
        {
163
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetAllProgress, document.Equipments.Count);
164
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Equipments Modeling");
165
            foreach (Equipment item in document.Equipments)
166
            {
167
                try
168
                {
169
                    EquipmentModeling(item);
170
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetProgress, document.Equipments.FindAll(x => !string.IsNullOrEmpty(x.SPPID.RepresentationId)).Count);
171
                }
172
                catch (Exception ex)
173
                {
174
                    Log.Write("Error in EquipmentModeling");
175
                    Log.Write("UID : " + item.UID);
176
                    Log.Write(ex.Message);
177
                    Log.Write(ex.StackTrace);
178
                }
179
            }
180
        }
181
        private void RunSymbolModeling()
182
        {
183
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetAllProgress, document.SYMBOLS.Count);
184
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Symbol Modeling");
185
            prioritySymbols = GetPrioritySymbol();
186
            foreach (var item in prioritySymbols)
187
            {
188
                try
189
                {
190
                    SymbolModelingBySymbol(item);
191
                }
192
                catch (Exception ex)
193
                {
194
                    Log.Write("Error in SymbolModelingByPriority");
195
                    Log.Write("UID : " + item.UID);
196
                    Log.Write(ex.Message);
197
                    Log.Write(ex.StackTrace);
198
                }
199
            }
200
        }
201
        private void RunLineModeling()
202
        {
203
            List<Line> AllLine = document.LINES.ToList();
204
            List<Line> stepLast_Line = document.LINES.FindAll(x => x.CONNECTORS.FindAll(y => y.ConnectedObject != null && y.ConnectedObject.GetType() == typeof(Symbol)).Count == 2 &&
205
            !SPPIDUtil.IsBranchedLine(document, x));
206
            List<Line> step1_Line = AllLine.FindAll(x => !stepLast_Line.Contains(x));
207

    
208
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetAllProgress, step1_Line.Count);
209
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Line Modeling - 1");
210

    
211
            SetPriorityLine(step1_Line);
212
            foreach (var item in step1_Line)
213
            {
214
                try
215
                {
216
                    NewLineModeling(item);
217
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
218
                }
219
                catch (Exception ex)
220
                {
221
                    Log.Write("Error in NewLineModeling");
222
                    Log.Write("UID : " + item.UID);
223
                    Log.Write(ex.Message);
224
                    Log.Write(ex.StackTrace);
225
                }
226
            }
227

    
228
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetAllProgress, BranchLines.Count);
229
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Line Modeling - 2");
230
            int branchCount = BranchLines.Count;
231
            while (BranchLines.Count > 0)
232
            {
233
                try
234
                {
235
                    SortBranchLines();
236
                    Line item = BranchLines[0];
237
                    NewLineModeling(item, true);
238
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
239
                }
240
                catch (Exception ex)
241
                {
242
                    Log.Write("Error in NewLineModeling");
243
                    Log.Write("UID : " + BranchLines[0].UID);
244
                    Log.Write(ex.Message);
245
                    Log.Write(ex.StackTrace);
246
                    break;
247
                }
248
            }
249

    
250
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetAllProgress, stepLast_Line.Count);
251
            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetStep, "Line Modeling - 3");
252
            foreach (var item in stepLast_Line)
253
            {
254
                try
255
                {
256
                    NewLineModeling(item);
257
                    SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
258
                }
259
                catch (Exception ex)
260
                {
261
                    Log.Write("Error in NewLineModeling");
262
                    Log.Write("UID : " + item.UID);
263
                    Log.Write(ex.Message);
264
                    Log.Write(ex.StackTrace);
265
                }
266
            }
267
        }
268
        private void RunClearValueInconsistancy()
269
        {
270
            int count = 1;
271
            bool loop = true;
272
            while (loop)
273
            {
274
                loop = false;
275
                LMAFilter filter = new LMAFilter();
276
                LMACriterion criterion = new LMACriterion();
277
                filter.ItemType = "Relationship";
278
                criterion.SourceAttributeName = "SP_DRAWINGID";
279
                criterion.Operator = "=";
280
                criterion.set_ValueAttribute(currentDrawing.Id);
281
                filter.get_Criteria().Add(criterion);
282

    
283
                LMRelationships relationships = new LMRelationships();
284
                relationships.Collect(dataSource, Filter: filter);
285

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

    
485
                        SetFlowDirectionByLine(line.SPPID.ModelItemId);
486

    
487
                        ReleaseCOMObjects(modelItem);
488
                    }
489
                }
490

    
491
                SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
492
            }
493
            foreach (var modelId in ZeroLengthModelItemID)
494
            {
495
                LMModelItem zeroLengthModelItem = dataSource.GetModelItem(modelId);
496
                LMAAttribute attribute = zeroLengthModelItem.Attributes["FlowDirection"];
497
                if (attribute != null && zeroLengthModelItem.get_ItemStatus() == "Active")
498
                {
499
                    attribute.set_Value("End 1 is upstream (Inlet)");
500
                    zeroLengthModelItem.Commit();
501
                }
502

    
503
                SetFlowDirectionByLine(modelId);
504

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

    
518
                SetFlowDirectionByLine(modelId);
519

    
520
                ReleaseCOMObjects(zeroLengthModelItem);
521
                SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
522
            }
523
            foreach (var modelId in ZeroLengthSymbolToSymbolModelItemID)
524
            {
525
                SetFlowDirectionByLine(modelId);
526
                SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.UpProgress, null);
527
            }
528

    
529
            void SetFlowDirectionByLine(string lineModelItemID)
530
            {
531
                LMModelItem modelItem = dataSource.GetModelItem(lineModelItemID);
532
                if (modelItem != null && modelItem.get_ItemStatus() == "Active")
533
                {
534
                    LMAAttribute attribute = modelItem.Attributes["FlowDirection"];
535
                    if (attribute != null && !DBNull.Value.Equals(attribute.get_Value()))
536
                    {
537
                        string sFlowDirection = attribute.get_Value().ToString();
538
                        foreach (LMRepresentation rep in modelItem.Representations)
539
                        {
540
                            if (rep.Attributes["RepresentationType"].get_Value() == "Connector" && rep.Attributes["ItemStatus"].get_Value() == "Active")
541
                            {
542
                                LMConnector connector = dataSource.GetConnector(rep.Id);
543

    
544
                                foreach (LMRelationship relationship in connector.Relation1Relationships)
545
                                    SetSymbolFlowDirectionByRelationShip(relationship, connector, sFlowDirection);
546
                                foreach (LMRelationship relationship in connector.Relation2Relationships)
547
                                    SetSymbolFlowDirectionByRelationShip(relationship, connector, sFlowDirection);
548

    
549
                                ReleaseCOMObjects(connector);
550
                            }
551
                        }
552
                    }
553
                    ReleaseCOMObjects(modelItem);
554
                }
555

    
556
                void SetSymbolFlowDirectionByRelationShip(LMRelationship relationship, LMConnector connector, string sFlowDirection)
557
                {
558
                    // Item2가 Symbol
559
                    if (!DBNull.Value.Equals(relationship.Item1RepresentationID) && relationship.Item1RepresentationID == connector.Id &&
560
                        relationship.Item2RepresentationObject != null && relationship.Item2RepresentationObject.get_RepresentationType() == "Symbol")
561
                    {
562
                        int symbolIndex = Convert.ToInt32(relationship.get_Item2Location());
563
                        int lineIndex = Convert.ToInt32(relationship.get_Item1Location());
564
                        LMModelItem symbolModelItem = relationship.Item2RepresentationObject.ModelItemObject;
565

    
566
                        SetSymbolFlowDirection(lineIndex, symbolIndex, sFlowDirection, symbolModelItem);
567

    
568
                        symbolModelItem.Commit();
569
                        ReleaseCOMObjects(symbolModelItem);
570
                    }
571
                    // Item1이 Symbol
572
                    else if (!DBNull.Value.Equals(relationship.Item2RepresentationID) && relationship.Item2RepresentationID == connector.Id &&
573
                            relationship.Item1RepresentationObject != null && relationship.Item1RepresentationObject.get_RepresentationType() == "Symbol")
574
                    {
575
                        int symbolIndex = Convert.ToInt32(relationship.get_Item1Location());
576
                        int lineIndex = Convert.ToInt32(relationship.get_Item2Location());
577
                        LMModelItem symbolModelItem = relationship.Item1RepresentationObject.ModelItemObject;
578

    
579
                        SetSymbolFlowDirection(lineIndex, symbolIndex, sFlowDirection, symbolModelItem);
580

    
581
                        symbolModelItem.Commit();
582
                        ReleaseCOMObjects(symbolModelItem);
583
                    }
584
                }
585

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

    
732
            //current LMDrawing 가져오기
733
            LMAFilter filter = new LMAFilter();
734
            LMACriterion criterion = new LMACriterion();
735
            filter.ItemType = "Drawing";
736
            criterion.SourceAttributeName = "Name";
737
            criterion.Operator = "=";
738
            criterion.set_ValueAttribute(drawingName);
739
            filter.get_Criteria().Add(criterion);
740

    
741
            LMDrawings drawings = new LMDrawings();
742
            drawings.Collect(dataSource, Filter: filter);
743

    
744
            currentDrawing = ((dynamic)drawings).Nth(1);
745
        }
746

    
747
        /// <summary>
748
        /// DrawingName, DrawingNumber를 확인하여 중복이 있으면 _1을 붙이고 +1씩 한다.
749
        /// </summary>
750
        /// <param name="drawingName"></param>
751
        /// <param name="drawingNumber"></param>
752
        private void GetDrawingNameAndNumber(ref string drawingName, ref string drawingNumber)
753
        {
754
            LMDrawings drawings = new LMDrawings();
755
            drawings.Collect(dataSource);
756

    
757
            List<string> drawingNameList = new List<string>();
758
            List<string> drawingNumberList = new List<string>();
759

    
760
            foreach (LMDrawing item in drawings)
761
            {
762
                drawingNameList.Add(item.Attributes["Name"].get_Value().ToString());
763
                drawingNumberList.Add(item.Attributes["DrawingNumber"].get_Value().ToString());
764
            }
765

    
766
            int nameLength = drawingName.Length;
767
            while (drawingNameList.Contains(drawingName))
768
            {
769
                if (nameLength == drawingName.Length)
770
                    drawingName += "-1";
771
                else
772
                {
773
                    int index = Convert.ToInt32(drawingName.Remove(0, nameLength + 1));
774
                    drawingName = drawingName.Substring(0, nameLength + 1);
775
                    drawingName += ++index;
776
                }
777
            }
778

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

    
792
            ReleaseCOMObjects(drawings);
793
        }
794

    
795
        /// <summary>
796
        /// 도면 크기 구하는 메서드
797
        /// </summary>
798
        /// <returns></returns>
799
        private bool DocumentCoordinateCorrection()
800
        {
801
            if (Settings.Default.DrawingX != 0 && Settings.Default.DrawingY != 0)
802
            {
803
                Log.Write("Setting Drawing X, Drawing Y");
804
                document.SetSPPIDLocation(Settings.Default.DrawingX, Settings.Default.DrawingY);
805
                Log.Write("Start coordinate correction");
806
                document.CoordinateCorrection();
807
                return true;
808
            }
809
            else
810
            {
811
                Log.Write("Need Drawing X, Y");
812
                return false;
813
            }
814
        }
815

    
816
        /// <summary>
817
        /// 심볼을 실제로 Modeling 메서드
818
        /// </summary>
819
        /// <param name="symbol"></param>
820
        /// <param name="targetSymbol"></param>
821
        /// <param name="prevSymbol"></param>
822
        private void SymbolModeling(Symbol symbol, Symbol targetSymbol)
823
        {
824
            // OWNERSYMBOL Attribute, 값을 가지고 있을 경우
825
            BaseModel.Attribute itemAttribute = symbol.ATTRIBUTES.Find(attr => attr.ATTRIBUTE == "OWNERSYMBOL");
826
            if (itemAttribute != null && (string.IsNullOrEmpty(itemAttribute.VALUE) || itemAttribute.VALUE != "None"))
827
                return;
828
            // 이미 모델링 됐을 경우
829
            else if (!string.IsNullOrEmpty(symbol.SPPID.RepresentationId))
830
                return;
831

    
832
            LMSymbol _LMSymbol = null;
833

    
834
            string mappingPath = symbol.SPPID.MAPPINGNAME;
835
            double x = symbol.SPPID.ORIGINAL_X;
836
            double y = symbol.SPPID.ORIGINAL_Y;
837
            int mirror = 0;
838
            double angle = symbol.ANGLE;
839

    
840
            // OPC 일경우 180도 일때 Mirror
841
            if (mappingPath.Contains("Piping OPC's") && angle == Math.PI)
842
                mirror = 1;
843

    
844
            // Mirror 계산
845
            if (symbol.FLIP == 1)
846
            {
847
                mirror = 1;
848
                angle += Math.PI;
849
            }
850

    
851
            if (targetSymbol != null && !string.IsNullOrEmpty(targetSymbol.SPPID.RepresentationId))
852
            {
853
                LMSymbol _TargetItem = dataSource.GetSymbol(targetSymbol.SPPID.RepresentationId);
854
                Connector connector = SPPIDUtil.FindSymbolConnectorByUID(document, symbol.UID, targetSymbol);
855
                if (connector != null)
856
                    GetTargetSymbolConnectorPoint(connector, targetSymbol, ref x, ref y);
857

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

    
860
                if (_LMSymbol != null && _TargetItem != null)
861
                {
862
                    symbol.SPPID.RepresentationId = _LMSymbol.AsLMRepresentation().Id;
863

    
864
                    if (SPPIDUtil.IsSegmentLine(document, symbol, targetSymbol))
865
                    {
866
                        LMConnector reModelingConnector = FindBreakLineTarget(symbol, targetSymbol);
867
                        if (reModelingConnector != null)
868
                            ReModelingLMConnector(reModelingConnector);
869
                    }
870
                }
871

    
872
                ReleaseCOMObjects(_TargetItem);
873
            }
874
            else
875
                _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle);
876

    
877
            if (_LMSymbol != null)
878
            {
879
                _LMSymbol.Commit();
880
                symbol.SPPID.RepresentationId = _LMSymbol.AsLMRepresentation().Id;
881
                symbol.SPPID.ModelItemID = _LMSymbol.ModelItemID;
882
                symbol.SPPID.GraphicOID = _LMSymbol.get_GraphicOID().ToString();
883

    
884
                foreach (var item in symbol.ChildSymbols)
885
                    CreateChildSymbol(item, _LMSymbol, symbol);
886

    
887
                symbol.SPPID.SPPID_X = _LMSymbol.get_XCoordinate();
888
                symbol.SPPID.SPPID_Y = _LMSymbol.get_YCoordinate();
889

    
890
                double[] range = null;
891
                GetSPPIDSymbolRange(symbol, ref range);
892
                symbol.SPPID.SPPID_Min_X = range[0];
893
                symbol.SPPID.SPPID_Min_Y = range[1];
894
                symbol.SPPID.SPPID_Max_X = range[2];
895
                symbol.SPPID.SPPID_Max_Y = range[3];
896

    
897
                foreach (var item in symbol.SPPID.CorrectionX_GroupSymbols)
898
                    item.SPPID.ORIGINAL_X = symbol.SPPID.SPPID_X;
899
                foreach (var item in symbol.SPPID.CorrectionY_GroupSymbols)
900
                    item.SPPID.ORIGINAL_Y = symbol.SPPID.SPPID_Y;
901

    
902
                ReleaseCOMObjects(_LMSymbol);
903
            }
904
        }
905

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

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

    
928
        private void RemoveSymbol(List<Symbol> symbols)
929
        {
930
            foreach (var symbol in symbols)
931
            {
932
                if (!string.IsNullOrEmpty(symbol.SPPID.RepresentationId))
933
                {
934
                    LMSymbol _LMSymbol = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
935
                    if (_LMSymbol != null)
936
                    {
937
                        _placement.PIDRemovePlacement(_LMSymbol.AsLMRepresentation());
938
                        ReleaseCOMObjects(_LMSymbol);
939
                    }
940
                }
941

    
942
                symbol.SPPID.RepresentationId = string.Empty;
943
                symbol.SPPID.ModelItemID = string.Empty;
944
                symbol.SPPID.SPPID_X = double.NaN;
945
                symbol.SPPID.SPPID_Y = double.NaN;
946
                symbol.SPPID.SPPID_Min_X = double.NaN;
947
                symbol.SPPID.SPPID_Min_Y = double.NaN;
948
                symbol.SPPID.SPPID_Max_X = double.NaN;
949
                symbol.SPPID.SPPID_Max_Y = double.NaN;
950
            }
951
        }
952

    
953
        /// <summary>
954
        /// ID2의 Symbol Width와 Height를 비교해서 상대적인 SPPID Connector좌표를 가져온다.
955
        /// </summary>
956
        /// <param name="targetConnector"></param>
957
        /// <param name="targetSymbol"></param>
958
        /// <param name="x"></param>
959
        /// <param name="y"></param>
960
        private void GetTargetSymbolConnectorPoint(Connector targetConnector, Symbol targetSymbol, ref double x, ref double y)
961
        {
962
            LMSymbol _TargetItem = dataSource.GetSymbol(targetSymbol.SPPID.RepresentationId);
963

    
964
            double[] range = null;
965
            List<double[]> points = new List<double[]>();
966
            GetSPPIDSymbolRangeAndConnectionPoints(targetSymbol, ref range, points);
967
            double x1 = range[0];
968
            double y1 = range[1];
969
            double x2 = range[2];
970
            double y2 = range[3];
971

    
972
            // Origin 기준 Connector의 위치차이
973
            double sceneX = 0;
974
            double sceneY = 0;
975
            SPPIDUtil.ConvertPointBystring(targetConnector.SCENECONNECTPOINT, ref sceneX, ref sceneY);
976
            double originX = 0;
977
            double originY = 0;
978
            SPPIDUtil.ConvertPointBystring(targetSymbol.ORIGINALPOINT, ref originX, ref originY);
979
            double gapX = originX - sceneX;
980
            double gapY = originY - sceneY;
981

    
982
            // SPPID Symbol과 ID2 심볼의 크기 차이
983
            double sizeWidth = 0;
984
            double sizeHeight = 0;
985
            SPPIDUtil.ConvertPointBystring(targetSymbol.SIZE, ref sizeWidth, ref sizeHeight);
986
            if (sizeWidth == 0 || sizeHeight == 0)
987
                throw new Exception("Check symbol size! \r\nUID : " + targetSymbol.UID);
988

    
989
            double percentX = (x2 - x1) / sizeWidth;
990
            double percentY = (y2 - y1) / sizeHeight;
991

    
992
            double SPPIDgapX = gapX * percentX;
993
            double SPPIDgapY = gapY * percentY;
994

    
995
            double[] SPPIDOriginPoint = new double[] { _TargetItem.get_XCoordinate() - SPPIDgapX, _TargetItem.get_YCoordinate() + SPPIDgapY };
996
            double distance = double.MaxValue;
997
            double[] resultPoint;
998
            foreach (var point in points)
999
            {
1000
                double result = SPPIDUtil.CalcPointToPointdDistance(point[0], point[1], SPPIDOriginPoint[0], SPPIDOriginPoint[1]);
1001
                if (distance > result)
1002
                {
1003
                    distance = result;
1004
                    resultPoint = point;
1005
                    x = point[0];
1006
                    y = point[1];
1007
                }
1008
            }
1009

    
1010
            ReleaseCOMObjects(_TargetItem);
1011
        }
1012

    
1013
        private void GetTargetLineConnectorPoint(Connector targetConnector, Line targetLine, ref double x, ref double y)
1014
        {
1015
            int index = targetLine.CONNECTORS.IndexOf(targetConnector);
1016
            if (index == 0)
1017
            {
1018
                x = targetLine.SPPID.START_X;
1019
                y = targetLine.SPPID.START_Y;
1020
            }
1021
            else
1022
            {
1023
                x = targetLine.SPPID.END_X;
1024
                y = targetLine.SPPID.END_Y;
1025
            }
1026
        }
1027

    
1028
        /// <summary>
1029
        /// SPPID Symbol의 Range를 구한다.
1030
        /// </summary>
1031
        /// <param name="symbol"></param>
1032
        /// <param name="range"></param>
1033
        private void GetSPPIDSymbolRangeAndConnectionPoints(Symbol symbol, ref double[] range, List<double[]> points)
1034
        {
1035
            LMSymbol _TargetItem = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
1036
            Ingr.RAD2D.Symbol2d symbol2d = radApp.ActiveDocument.ActiveSheet.DrawingObjects[_TargetItem.get_GraphicOID().ToString()];
1037
            double x1 = 0;
1038
            double y1 = 0;
1039
            double x2 = 0;
1040
            double y2 = 0;
1041
            symbol2d.Range(out x1, out y1, out x2, out y2);
1042
            range = new double[] { x1, y1, x2, y2 };
1043

    
1044
            for (int i = 1; i < int.MaxValue; i++)
1045
            {
1046
                double connX = 0;
1047
                double connY = 0;
1048
                if (_placement.PIDConnectPointLocation(_TargetItem, i, ref connX, ref connY))
1049
                    points.Add(new double[] { connX, connY });
1050
                else
1051
                    break;
1052
            }
1053

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

    
1057
            ReleaseCOMObjects(_TargetItem);
1058
        }
1059

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

    
1073
                foreach (var childSymbol in symbol.ChildSymbols)
1074
                    GetSPPIDChildSymbolRange(childSymbol, ref range);
1075

    
1076
                ReleaseCOMObjects(_TargetItem);
1077
            }
1078
        }
1079

    
1080
        private void GetSPPIDSymbolRange(List<Symbol> symbols, ref double[] range)
1081
        {
1082
            double[] tempRange = new double[] { double.MaxValue, double.MaxValue, double.MinValue, double.MinValue };
1083
            foreach (var symbol in symbols)
1084
            {
1085
                LMSymbol _TargetItem = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
1086
                Ingr.RAD2D.Symbol2d symbol2d = radApp.ActiveDocument.ActiveSheet.DrawingObjects[_TargetItem.get_GraphicOID().ToString()];
1087
                double x1 = 0;
1088
                double y1 = 0;
1089
                double x2 = 0;
1090
                double y2 = 0;
1091
                symbol2d.Range(out x1, out y1, out x2, out y2);
1092

    
1093
                tempRange[0] = Math.Min(tempRange[0], x1);
1094
                tempRange[1] = Math.Min(tempRange[1], y1);
1095
                tempRange[2] = Math.Max(tempRange[2], x2);
1096
                tempRange[3] = Math.Max(tempRange[3], y2);
1097

    
1098
                foreach (var childSymbol in symbol.ChildSymbols)
1099
                    GetSPPIDChildSymbolRange(childSymbol, ref tempRange);
1100

    
1101
                ReleaseCOMObjects(_TargetItem);
1102
            }
1103

    
1104
            range = tempRange;
1105
        }
1106

    
1107
        /// <summary>
1108
        /// Child Modeling 된 Symbol의 Range를 구한다.
1109
        /// </summary>
1110
        /// <param name="childSymbol"></param>
1111
        /// <param name="range"></param>
1112
        private void GetSPPIDChildSymbolRange(ChildSymbol childSymbol, ref double[] range, List<double[]> points)
1113
        {
1114
            LMSymbol _ChildSymbol = dataSource.GetSymbol(childSymbol.SPPID.RepresentationId);
1115
            if (_ChildSymbol != null)
1116
            {
1117
                Ingr.RAD2D.Symbol2d symbol2d = radApp.ActiveDocument.ActiveSheet.DrawingObjects[_ChildSymbol.get_GraphicOID().ToString()];
1118
                double x1 = 0;
1119
                double y1 = 0;
1120
                double x2 = 0;
1121
                double y2 = 0;
1122
                symbol2d.Range(out x1, out y1, out x2, out y2);
1123
                range[0] = Math.Min(range[0], x1);
1124
                range[1] = Math.Min(range[1], y1);
1125
                range[2] = Math.Max(range[2], x2);
1126
                range[3] = Math.Max(range[3], y2);
1127

    
1128
                for (int i = 1; i < int.MaxValue; i++)
1129
                {
1130
                    double connX = 0;
1131
                    double connY = 0;
1132
                    if (_placement.PIDConnectPointLocation(_ChildSymbol, i, ref connX, ref connY))
1133
                        points.Add(new double[] { connX, connY });
1134
                    else
1135
                        break;
1136
                }
1137

    
1138
                foreach (var loopChildSymbol in childSymbol.ChildSymbols)
1139
                    GetSPPIDChildSymbolRange(loopChildSymbol, ref range, points);
1140

    
1141
                ReleaseCOMObjects(_ChildSymbol);
1142
            }
1143
        }
1144

    
1145
        private void GetSPPIDChildSymbolRange(ChildSymbol childSymbol, ref double[] range)
1146
        {
1147
            LMSymbol _ChildSymbol = dataSource.GetSymbol(childSymbol.SPPID.RepresentationId);
1148
            if (_ChildSymbol != null)
1149
            {
1150
                Ingr.RAD2D.Symbol2d symbol2d = radApp.ActiveDocument.ActiveSheet.DrawingObjects[_ChildSymbol.get_GraphicOID().ToString()];
1151
                double x1 = 0;
1152
                double y1 = 0;
1153
                double x2 = 0;
1154
                double y2 = 0;
1155
                symbol2d.Range(out x1, out y1, out x2, out y2);
1156
                range[0] = Math.Min(range[0], x1);
1157
                range[1] = Math.Min(range[1], y1);
1158
                range[2] = Math.Max(range[2], x2);
1159
                range[3] = Math.Max(range[3], y2);
1160

    
1161
                foreach (var loopChildSymbol in childSymbol.ChildSymbols)
1162
                    GetSPPIDChildSymbolRange(loopChildSymbol, ref range);
1163
                ReleaseCOMObjects(_ChildSymbol);
1164
            }
1165
        }
1166

    
1167
        /// <summary>
1168
        /// Label Symbol Modeling
1169
        /// </summary>
1170
        /// <param name="symbol"></param>
1171
        private void LabelSymbolModeling(Symbol symbol)
1172
        {
1173
            if (string.IsNullOrEmpty(symbol.SPPID.RepresentationId))
1174
            {
1175
                BaseModel.Attribute itemAttribute = symbol.ATTRIBUTES.Find(x => x.ATTRIBUTE == "OWNERSYMBOL");
1176
                if (itemAttribute == null || string.IsNullOrEmpty(itemAttribute.VALUE) || itemAttribute.VALUE == "None")
1177
                    return;
1178
                Array points = new double[] { 0, symbol.SPPID.ORIGINAL_X, symbol.SPPID.ORIGINAL_Y };
1179

    
1180
                string symbolUID = itemAttribute.VALUE;
1181
                object targetItem = SPPIDUtil.FindObjectByUID(document, symbolUID);
1182
                if (targetItem != null &&
1183
                    (targetItem.GetType() == typeof(Symbol) ||
1184
                    targetItem.GetType() == typeof(Equipment)))
1185
                {
1186
                    // Object 아이템이 Symbol일 경우 Equipment일 경우 
1187
                    string sRep = null;
1188
                    if (targetItem.GetType() == typeof(Symbol))
1189
                        sRep = ((Symbol)targetItem).SPPID.RepresentationId;
1190
                    else if (targetItem.GetType() == typeof(Equipment))
1191
                        sRep = ((Equipment)targetItem).SPPID.RepresentationId;
1192
                    if (!string.IsNullOrEmpty(sRep))
1193
                    {
1194
                        // LEADER Line 검사
1195
                        bool leaderLine = false;
1196
                        SymbolMapping symbolMapping = document.SymbolMappings.Find(x => x.UID == symbol.DBUID);
1197
                        if (symbolMapping != null)
1198
                            leaderLine = symbolMapping.LEADERLINE;
1199

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

    
1204
                        //Leader 선 센터로
1205
                        if (_LMLabelPresist != null)
1206
                        {
1207
                            // Target Item에 Label의 Attribute Input
1208
                            InputSymbolAttribute(targetItem, symbol.ATTRIBUTES);
1209

    
1210
                            string OID = _LMLabelPresist.get_GraphicOID().ToString();
1211
                            DependencyObject dependency = radApp.ActiveDocument.ActiveSheet.DrawingObjects[OID] as DependencyObject;
1212
                            if (dependency != null)
1213
                            {
1214
                                bool result = false;
1215
                                foreach (var attributes in dependency.AttributeSets)
1216
                                {
1217
                                    foreach (var attribute in attributes)
1218
                                    {
1219
                                        string name = attribute.Name;
1220
                                        string value = attribute.GetValue().ToString();
1221
                                        if (name == "DrawingItemType" && value == "LabelPersist")
1222
                                        {
1223
                                            foreach (DrawingObjectBase drawingObject in dependency.DrawingObjects)
1224
                                            {
1225
                                                if (drawingObject.Type == Ingr.RAD2D.ObjectType.igLineString2d)
1226
                                                {
1227
                                                    Ingr.RAD2D.LineString2d lineString2D = drawingObject as Ingr.RAD2D.LineString2d;
1228
                                                    double prevX = _TargetItem.get_XCoordinate();
1229
                                                    double prevY = _TargetItem.get_YCoordinate();
1230
                                                    lineString2D.InsertVertex(lineString2D.VertexCount, prevX, prevY);
1231
                                                    lineString2D.RemoveVertex(lineString2D.VertexCount);
1232
                                                    result = true;
1233
                                                    break;
1234
                                                }
1235
                                            }
1236
                                        }
1237

    
1238
                                        if (result)
1239
                                            break;
1240
                                    }
1241

    
1242
                                    if (result)
1243
                                        break;
1244
                                }
1245
                            }
1246

    
1247
                            symbol.SPPID.RepresentationId = _LMLabelPresist.AsLMRepresentation().Id;
1248
                            _LMLabelPresist.Commit();
1249
                            ReleaseCOMObjects(_LMLabelPresist);
1250
                        }
1251

    
1252
                        ReleaseCOMObjects(_TargetItem);
1253
                    }
1254
                }
1255
                else if (targetItem != null && targetItem.GetType() == typeof(Line))
1256
                {
1257
                    Line targetLine = targetItem as Line;
1258
                    Dictionary<LMConnector, List<double[]>> connectorVertices = GetPipeRunVertices(targetLine.SPPID.ModelItemId);
1259
                    LMConnector connectedLMConnector = FindTargetLMConnectorForLabel(connectorVertices, symbol.SPPID.ORIGINAL_X, symbol.SPPID.ORIGINAL_Y);
1260
                    if (connectedLMConnector != null)
1261
                    {
1262
                        // LEADER Line 검사
1263
                        bool leaderLine = false;
1264
                        SymbolMapping symbolMapping = document.SymbolMappings.Find(x => x.UID == symbol.DBUID);
1265
                        if (symbolMapping != null)
1266
                            leaderLine = symbolMapping.LEADERLINE;
1267

    
1268
                        LMLabelPersist _LMLabelPresist = _placement.PIDPlaceLabel(symbol.SPPID.MAPPINGNAME, ref points, Rotation: 0, LabeledItem: connectedLMConnector.AsLMRepresentation(), IsLeaderVisible: leaderLine);
1269
                        if (_LMLabelPresist != null)
1270
                        {
1271
                            _LMLabelPresist.Commit();
1272
                            ReleaseCOMObjects(_LMLabelPresist);
1273
                        }
1274
                        ReleaseCOMObjects(connectedLMConnector);
1275
                    }
1276

    
1277
                    foreach (var item in connectorVertices)
1278
                        if (item.Key != null)
1279
                            ReleaseCOMObjects(item.Key);
1280
                }
1281
            }
1282
        }
1283

    
1284
        /// <summary>
1285
        /// Equipment를 실제로 Modeling 메서드
1286
        /// </summary>
1287
        /// <param name="equipment"></param>
1288
        private void EquipmentModeling(Equipment equipment)
1289
        {
1290
            if (!string.IsNullOrEmpty(equipment.SPPID.RepresentationId))
1291
                return;
1292

    
1293
            LMSymbol _LMSymbol = null;
1294
            LMSymbol targetItem = null;
1295
            string mappingPath = equipment.SPPID.MAPPINGNAME;
1296
            double x = equipment.SPPID.ORIGINAL_X;
1297
            double y = equipment.SPPID.ORIGINAL_Y;
1298
            int mirror = 0;
1299
            double angle = equipment.ANGLE;
1300

    
1301
            SPPIDUtil.ConvertGridPoint(ref x, ref y);
1302

    
1303
            Connector connector = equipment.CONNECTORS.Find(conn => !string.IsNullOrEmpty(conn.CONNECTEDITEM) && conn.CONNECTEDITEM != "None");
1304
            if (connector != null)
1305
            {
1306
                Equipment connEquipment = SPPIDUtil.FindObjectByUID(document, connector.CONNECTEDITEM) as Equipment;
1307
                if (connEquipment != null)
1308
                {
1309
                    if (string.IsNullOrEmpty(connEquipment.SPPID.RepresentationId))
1310
                        EquipmentModeling(connEquipment);
1311

    
1312
                    if (!string.IsNullOrEmpty(connEquipment.SPPID.RepresentationId))
1313
                    {
1314
                        targetItem = dataSource.GetSymbol(connEquipment.SPPID.RepresentationId);
1315
                        if (targetItem != null)
1316
                        {
1317
                            _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle, TargetItem: targetItem);
1318
                        }
1319
                        else
1320
                        {
1321
                            _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle);
1322
                        }
1323
                    }
1324
                    else
1325
                    {
1326
                        _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle);
1327
                    }
1328
                }
1329
                else
1330
                {
1331
                    _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle);
1332
                }
1333
            }
1334
            else
1335
            {
1336
                _LMSymbol = _placement.PIDPlaceSymbol(mappingPath, x, y, Mirror: mirror, Rotation: angle);
1337
            }
1338

    
1339
            if (_LMSymbol != null)
1340
            {
1341
                _LMSymbol.Commit();
1342
                equipment.SPPID.RepresentationId = _LMSymbol.AsLMRepresentation().Id;
1343
                equipment.SPPID.GraphicOID = _LMSymbol.get_GraphicOID().ToString();
1344
                ReleaseCOMObjects(_LMSymbol);
1345
            }
1346

    
1347
            if (targetItem != null)
1348
            {
1349
                ReleaseCOMObjects(targetItem);
1350
            }
1351

    
1352
            ReleaseCOMObjects(_LMSymbol);
1353
        }
1354

    
1355
        /// <summary>
1356
        /// 첫 진입점
1357
        /// </summary>
1358
        /// <param name="symbol"></param>
1359
        private void SymbolModelingBySymbol(Symbol symbol)
1360
        {
1361
            SymbolModeling(symbol, null);
1362
            List<object> endObjects = new List<object>();
1363
            endObjects.Add(symbol);
1364

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

    
1390
        private void SymbolModelingByNeerSymbolLoop(Symbol symbol, List<object> endObjects)
1391
        {
1392
            foreach (var connector in symbol.CONNECTORS)
1393
            {
1394
                object connItem = SPPIDUtil.FindObjectByUID(document, connector.CONNECTEDITEM);
1395
                if (connItem != null && connItem.GetType() != typeof(Equipment))
1396
                {
1397
                    if (!endObjects.Contains(connItem))
1398
                    {
1399
                        endObjects.Add(connItem);
1400
                        if (connItem.GetType() == typeof(Symbol))
1401
                        {
1402
                            Symbol connSymbol = connItem as Symbol;
1403
                            if (string.IsNullOrEmpty(connSymbol.SPPID.RepresentationId))
1404
                            {
1405
                                SymbolModeling(connSymbol, symbol);
1406
                            }
1407
                            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetProgress, document.SYMBOLS.FindAll(x => !string.IsNullOrEmpty(x.SPPID.RepresentationId)).Count);
1408
                            SymbolModelingByNeerSymbolLoop(connSymbol, endObjects);
1409
                        }
1410
                        else if (connItem.GetType() == typeof(Line))
1411
                        {
1412
                            Line connLine = connItem as Line;
1413
                            SymbolModelingByNeerLineLoop(connLine, endObjects, symbol);
1414
                        }
1415
                    }
1416
                }
1417
            }
1418
        }
1419

    
1420
        private void SymbolModelingByNeerLineLoop(Line line, List<object> endObjects, Symbol prevSymbol)
1421
        {
1422
            foreach (var connector in line.CONNECTORS)
1423
            {
1424
                object connItem = SPPIDUtil.FindObjectByUID(document, connector.CONNECTEDITEM);
1425
                if (connItem != null && connItem.GetType() != typeof(Equipment))
1426
                {
1427
                    if (!endObjects.Contains(connItem))
1428
                    {
1429
                        endObjects.Add(connItem);
1430
                        if (connItem.GetType() == typeof(Symbol))
1431
                        {
1432
                            Symbol connSymbol = connItem as Symbol;
1433
                            if (string.IsNullOrEmpty(connSymbol.SPPID.RepresentationId))
1434
                            {
1435
                                List<Symbol> group = new List<Symbol>();
1436
                                SPPIDUtil.FindConnectedSymbolGroup(document, connSymbol, group);
1437
                                Symbol priority = prioritySymbols.Find(x => group.Contains(x));
1438
                                List<Symbol> endModelingGroup = new List<Symbol>();
1439
                                if (priority != null)
1440
                                {
1441
                                    SymbolGroupModeling(priority, group);
1442

    
1443
                                    // Range 겹치는지 확인해야함
1444
                                    double[] prevRange = null;
1445
                                    GetSPPIDSymbolRange(prevSymbol, ref prevRange);
1446
                                    double[] groupRange = null;
1447
                                    GetSPPIDSymbolRange(group, ref groupRange);
1448

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

    
1462
                                        SymbolGroupModeling(priority, group);
1463
                                    }
1464
                                }
1465
                                else
1466
                                {
1467
                                    SymbolModeling(connSymbol, null);
1468
                                    // Range 겹치는지 확인해야함
1469
                                    double[] prevRange = null;
1470
                                    GetSPPIDSymbolRange(prevSymbol, ref prevRange);
1471
                                    double[] connRange = null;
1472
                                    GetSPPIDSymbolRange(connSymbol, ref connRange);
1473

    
1474
                                    double distanceX = 0;
1475
                                    double distanceY = 0;
1476
                                    bool overlapX = false;
1477
                                    bool overlapY = false;
1478
                                    SlopeType slopeType = SPPIDUtil.CalcSlope(prevSymbol.SPPID.ORIGINAL_X, prevSymbol.SPPID.ORIGINAL_Y, connSymbol.SPPID.ORIGINAL_X, connSymbol.SPPID.ORIGINAL_Y);
1479
                                    SPPIDUtil.CalcOverlap(prevRange, connRange, ref distanceX, ref distanceY, ref overlapX, ref overlapY);
1480
                                    if ((slopeType == SlopeType.HORIZONTAL && overlapX) ||
1481
                                        (slopeType == SlopeType.VERTICAL && overlapY))
1482
                                    {
1483
                                        RemoveSymbol(connSymbol);
1484
                                        SPPIDUtil.CalcNewCoordinateForSymbol(connSymbol, prevSymbol, distanceX, distanceY);
1485

    
1486
                                        SymbolModeling(connSymbol, null);
1487
                                    }
1488
                                }
1489
                            }
1490
                            SplashScreenManager.Default.SendCommand(SPPIDSplashScreen.SplashScreenCommand.SetProgress, document.SYMBOLS.FindAll(x => !string.IsNullOrEmpty(x.SPPID.RepresentationId)).Count);
1491
                            SymbolModelingByNeerSymbolLoop(connSymbol, endObjects);
1492
                        }
1493
                        else if (connItem.GetType() == typeof(Line))
1494
                        {
1495
                            Line connLine = connItem as Line;
1496
                            if (!SPPIDUtil.IsBranchLine(connLine, line))
1497
                                SymbolModelingByNeerLineLoop(connLine, endObjects, prevSymbol);
1498
                        }
1499
                    }
1500
                }
1501
            }
1502
        }
1503

    
1504
        private void SymbolGroupModeling(Symbol firstSymbol, List<Symbol> group)
1505
        {
1506
            List<Symbol> endModelingGroup = new List<Symbol>();
1507
            SymbolModeling(firstSymbol, null);
1508
            endModelingGroup.Add(firstSymbol);
1509
            while (endModelingGroup.Count != group.Count)
1510
            {
1511
                foreach (var _symbol in group)
1512
                {
1513
                    if (!endModelingGroup.Contains(_symbol))
1514
                    {
1515
                        foreach (var _connector in _symbol.CONNECTORS)
1516
                        {
1517
                            Symbol _connSymbol = SPPIDUtil.FindObjectByUID(document, _connector.CONNECTEDITEM) as Symbol;
1518
                            if (_connSymbol != null && endModelingGroup.Contains(_connSymbol))
1519
                            {
1520
                                SymbolModeling(_symbol, _connSymbol);
1521
                                endModelingGroup.Add(_symbol);
1522
                                break;
1523
                            }
1524
                        }
1525
                    }
1526
                }
1527
            }
1528
        }
1529

    
1530
        /// <summary>
1531
        /// 심볼을 실제로 Modeling할때 ChildSymbol이 있다면 Modeling하는 메서드
1532
        /// </summary>
1533
        /// <param name="childSymbol"></param>
1534
        /// <param name="parentSymbol"></param>
1535
        private void CreateChildSymbol(ChildSymbol childSymbol, LMSymbol parentSymbol, Symbol parent)
1536
        {
1537
            Ingr.RAD2D.Symbol2d symbol2d = radApp.ActiveDocument.ActiveSheet.DrawingObjects[parentSymbol.get_GraphicOID().ToString()];
1538
            double x1 = 0;
1539
            double x2 = 0;
1540
            double y1 = 0;
1541
            double y2 = 0;
1542
            symbol2d.Range(out x1, out y1, out x2, out y2);
1543

    
1544
            LMSymbol _LMSymbol = _placement.PIDPlaceSymbol(childSymbol.SPPID.MAPPINGNAME, (x1 + x2) / 2, (y1 + y2) / 2, TargetItem: parentSymbol);
1545
            if (_LMSymbol != null)
1546
            {
1547
                childSymbol.SPPID.RepresentationId = _LMSymbol.AsLMRepresentation().Id;
1548
                foreach (var item in childSymbol.ChildSymbols)
1549
                    CreateChildSymbol(item, _LMSymbol, parent);
1550
            }
1551

    
1552

    
1553
            ReleaseCOMObjects(_LMSymbol);
1554
        }
1555
        double index = 0;
1556
        private void NewLineModeling(Line line, bool isBranchModeling = false)
1557
        {
1558
            if (!string.IsNullOrEmpty(line.SPPID.ModelItemId) || (BranchLines.Contains(line) && !isBranchModeling))
1559
                return;
1560

    
1561
            List<Line> group = new List<Line>();
1562
            GetConnectedLineGroup(line, group);
1563
            LineCoordinateCorrection(group);
1564

    
1565
            foreach (var groupLine in group)
1566
            {
1567
                if (!isBranchModeling && SPPIDUtil.IsBranchLine(groupLine))
1568
                {
1569
                    BranchLines.Add(groupLine);
1570
                    continue;
1571
                }
1572

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

    
1645
                            placeRunInputs.AddPoint(x, y);
1646

    
1647
                            if (groupLine.CONNECTORS.IndexOf(connector) == 1)
1648
                            {
1649
                                index += 0.01;
1650
                                if (groupLine.SlopeType == SlopeType.HORIZONTAL)
1651
                                    placeRunInputs.AddPoint(x, -0.1 - index);
1652
                                else if (groupLine.SlopeType == SlopeType.VERTICAL)
1653
                                    placeRunInputs.AddPoint(-0.1 - index, y);
1654
                                else
1655
                                {
1656
                                    if (SPPIDUtil.CalcAngle(groupLine.SPPID.START_X, groupLine.SPPID.START_Y, groupLine.SPPID.END_X, groupLine.SPPID.END_Y) < 45)
1657
                                        placeRunInputs.AddPoint(x, -0.1 - index);
1658
                                    else
1659
                                        placeRunInputs.AddPoint(-0.1 - index, y);
1660
                                }
1661
                            }
1662
                        }
1663
                    }
1664
                }
1665

    
1666
                LMConnector _lMConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
1667
                if (_lMConnector != null)
1668
                {
1669
                    groupLine.SPPID.ModelItemId = _lMConnector.ModelItemID;
1670
                    bool bRemodelingStart = false;
1671
                    if (_LMSymbolStart != null)
1672
                        NeedReModeling(groupLine, _LMSymbolStart, ref bRemodelingStart);
1673
                    bool bRemodelingEnd = false;
1674
                    if (_LMSymbolEnd != null)
1675
                        NeedReModeling(groupLine, _LMSymbolEnd, ref bRemodelingEnd);
1676

    
1677
                    if (bRemodelingStart || bRemodelingEnd)
1678
                        ReModelingLine(groupLine, _lMConnector, _LMSymbolStart, _LMSymbolEnd, bRemodelingStart, bRemodelingEnd);
1679

    
1680
                    FlowMarkModeling(groupLine);
1681
                    LineNumberModelingOnlyOne(groupLine);
1682

    
1683
                    ReleaseCOMObjects(_lMConnector);
1684
                }
1685
                else if (!isBranchModeling)
1686
                {
1687
                    Log.Write("Main Line Modeling : " + groupLine.UID);
1688
                }
1689

    
1690
                List<object> removeLines = groupLine.CONNECTORS.FindAll(x =>
1691
                x.ConnectedObject != null &&
1692
                x.ConnectedObject.GetType() == typeof(Line) &&
1693
                !string.IsNullOrEmpty(((Line)x.ConnectedObject).SPPID.ModelItemId))
1694
                .Select(x => x.ConnectedObject)
1695
                .ToList();
1696

    
1697
                foreach (var item in removeLines)
1698
                    RemoveLineForModeling(item as Line);
1699

    
1700
                if (_LMAItem != null)
1701
                    ReleaseCOMObjects(_LMAItem);
1702
                if (placeRunInputs != null)
1703
                    ReleaseCOMObjects(placeRunInputs);
1704
                if (_LMSymbolStart != null)
1705
                    ReleaseCOMObjects(_LMSymbolStart);
1706
                if (_LMSymbolEnd != null)
1707
                    ReleaseCOMObjects(_LMSymbolEnd);
1708

    
1709
                if (isBranchModeling && BranchLines.Contains(groupLine))
1710
                    BranchLines.Remove(groupLine);
1711
            }
1712
        }
1713

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

    
1742
                ReleaseCOMObjects(modelItem);
1743
            }
1744
        }
1745

    
1746
        private void GetConnectedLineGroup(Line line, List<Line> group)
1747
        {
1748
            if (!group.Contains(line))
1749
                group.Add(line);
1750
            foreach (var connector in line.CONNECTORS)
1751
            {
1752
                if (connector.ConnectedObject != null &&
1753
                    connector.ConnectedObject.GetType() == typeof(Line) &&
1754
                    !group.Contains(connector.ConnectedObject) &&
1755
                    string.IsNullOrEmpty(((Line)connector.ConnectedObject).SPPID.ModelItemId))
1756
                {
1757
                    Line connLine = connector.ConnectedObject as Line;
1758
                    if (!SPPIDUtil.IsBranchLine(connLine, line))
1759
                    {
1760
                        if (line.CONNECTORS.IndexOf(connector) == 0)
1761
                            group.Insert(0, connLine);
1762
                        else
1763
                            group.Add(connLine);
1764
                        GetConnectedLineGroup(connLine, group);
1765
                    }
1766
                }
1767
            }
1768
        }
1769

    
1770
        private void LineCoordinateCorrection(List<Line> group)
1771
        {
1772
            // 순서대로 전 Item 기준 정렬
1773
            LineCoordinateCorrectionByStart(group);
1774

    
1775
            // 역으로 심볼이 있을 경우 좌표 보정
1776
            LineCoordinateCorrectionForLastLine(group);
1777
        }
1778

    
1779
        private void LineCoordinateCorrectionByStart(List<Line> group)
1780
        {
1781
            for (int i = 0; i < group.Count; i++)
1782
            {
1783
                Line line = group[i];
1784
                if (i == 0)
1785
                {
1786
                    Connector symbolConnector = line.CONNECTORS.Find(x => x.ConnectedObject != null && x.ConnectedObject.GetType() == typeof(Symbol));
1787
                    if (symbolConnector != null)
1788
                        LineCoordinateCorrectionByConnItem(line, symbolConnector.ConnectedObject);
1789
                }
1790
                else if (i != 0)
1791
                {
1792
                    LineCoordinateCorrectionByConnItem(line, group[i - 1]);
1793
                }
1794
            }
1795
        }
1796

    
1797
        private void LineCoordinateCorrectionForLastLine(List<Line> group)
1798
        {
1799
            Line checkLine = group[group.Count - 1];
1800
            Connector lastSymbolConnector = checkLine.CONNECTORS.Find(x => x.ConnectedObject != null && x.ConnectedObject.GetType() == typeof(Symbol));
1801
            if (lastSymbolConnector != null)
1802
            {
1803
                LineCoordinateCorrectionByConnItem(checkLine, lastSymbolConnector.ConnectedObject);
1804
                for (int i = group.Count - 2; i >= 0; i--)
1805
                {
1806
                    Line line = group[i + 1];
1807
                    Line prevLine = group[i];
1808

    
1809
                    // 같으면 보정
1810
                    if (line.SlopeType == prevLine.SlopeType)
1811
                        LineCoordinateCorrectionByConnItem(prevLine, line);
1812
                    else
1813
                    {
1814
                        if (line.SlopeType == SlopeType.HORIZONTAL)
1815
                        {
1816
                            double prevX = 0;
1817
                            double prevY = 0;
1818
                            GetTargetLineConnectorPoint(prevLine.CONNECTORS.Find(z => z.ConnectedObject == line), prevLine, ref prevX, ref prevY);
1819
                            ChangeLineSPPIDCoordinateByConnectorOnlyX(line, prevLine, prevX);
1820

    
1821
                            double x = 0;
1822
                            double y = 0;
1823
                            GetTargetLineConnectorPoint(line.CONNECTORS.Find(z => z.ConnectedObject == prevLine), line, ref x, ref y);
1824
                            ChangeLineSPPIDCoordinateByConnectorOnlyY(prevLine, line, y);
1825
                        }
1826
                        else if (line.SlopeType == SlopeType.VERTICAL)
1827
                        {
1828
                            double prevX = 0;
1829
                            double prevY = 0;
1830
                            GetTargetLineConnectorPoint(prevLine.CONNECTORS.Find(z => z.ConnectedObject == line), prevLine, ref prevX, ref prevY);
1831
                            ChangeLineSPPIDCoordinateByConnectorOnlyY(line, prevLine, prevY);
1832

    
1833
                            double x = 0;
1834
                            double y = 0;
1835
                            GetTargetLineConnectorPoint(line.CONNECTORS.Find(z => z.ConnectedObject == prevLine), line, ref x, ref y);
1836
                            ChangeLineSPPIDCoordinateByConnectorOnlyX(prevLine, line, x);
1837
                        }
1838
                        break;
1839
                    }
1840
                }
1841
            }
1842
        }
1843

    
1844
        private void LineCoordinateCorrectionByConnItem(Line line, object connItem)
1845
        {
1846
            double x = 0;
1847
            double y = 0;
1848
            if (connItem.GetType() == typeof(Symbol))
1849
            {
1850
                Symbol targetSymbol = connItem as Symbol;
1851
                Connector targetConnector = targetSymbol.CONNECTORS.Find(z => z.ConnectedObject == line);
1852
                if (targetConnector != null)
1853
                    GetTargetSymbolConnectorPoint(targetConnector, targetSymbol, ref x, ref y);
1854
                else
1855
                    throw new Exception("Target symbol UID : " + targetSymbol.UID + "\r\nLine UID : " + line.UID);
1856
            }
1857
            else if (connItem.GetType() == typeof(Line))
1858
            {
1859
                Line targetLine = connItem as Line;
1860
                GetTargetLineConnectorPoint(targetLine.CONNECTORS.Find(z => z.ConnectedObject == line), targetLine, ref x, ref y);
1861
            }
1862

    
1863
            ChangeLineSPPIDCoordinateByConnector(line, connItem, x, y);
1864
        }
1865

    
1866
        private void ChangeLineSPPIDCoordinateByConnector(Line line, object connItem, double x, double y, bool changeOtherCoordinate = true)
1867
        {
1868
            Connector connector = line.CONNECTORS.Find(z => z.ConnectedObject == connItem);
1869
            int index = line.CONNECTORS.IndexOf(connector);
1870
            if (index == 0)
1871
            {
1872
                line.SPPID.START_X = x;
1873
                line.SPPID.START_Y = y;
1874
                if (line.SlopeType == SlopeType.HORIZONTAL && changeOtherCoordinate)
1875
                    line.SPPID.END_Y = y;
1876
                else if (line.SlopeType == SlopeType.VERTICAL && changeOtherCoordinate)
1877
                    line.SPPID.END_X = x;
1878
            }
1879
            else
1880
            {
1881
                line.SPPID.END_X = x;
1882
                line.SPPID.END_Y = y;
1883
                if (line.SlopeType == SlopeType.HORIZONTAL && changeOtherCoordinate)
1884
                    line.SPPID.START_Y = y;
1885
                else if (line.SlopeType == SlopeType.VERTICAL && changeOtherCoordinate)
1886
                    line.SPPID.START_X = x;
1887
            }
1888
        }
1889

    
1890
        private void ChangeLineSPPIDCoordinateByConnectorOnlyX(Line line, object connItem, double x)
1891
        {
1892
            Connector connector = line.CONNECTORS.Find(z => z.ConnectedObject == connItem);
1893
            int index = line.CONNECTORS.IndexOf(connector);
1894
            if (index == 0)
1895
            {
1896
                line.SPPID.START_X = x;
1897
                if (line.SlopeType == SlopeType.VERTICAL)
1898
                    line.SPPID.END_X = x;
1899
            }
1900
            else
1901
            {
1902
                line.SPPID.END_X = x;
1903
                if (line.SlopeType == SlopeType.VERTICAL)
1904
                    line.SPPID.START_X = x;
1905
            }
1906
        }
1907

    
1908
        private void ChangeLineSPPIDCoordinateByConnectorOnlyY(Line line, object connItem, double y)
1909
        {
1910
            Connector connector = line.CONNECTORS.Find(z => z.ConnectedObject == connItem);
1911
            int index = line.CONNECTORS.IndexOf(connector);
1912
            if (index == 0)
1913
            {
1914
                line.SPPID.START_Y = y;
1915
                if (line.SlopeType == SlopeType.HORIZONTAL)
1916
                    line.SPPID.END_Y = y;
1917
            }
1918
            else
1919
            {
1920
                line.SPPID.END_Y = y;
1921
                if (line.SlopeType == SlopeType.HORIZONTAL)
1922
                    line.SPPID.START_Y = y;
1923
            }
1924
        }
1925

    
1926
        private void NeedReModeling(Line line, LMSymbol symbol, ref bool result)
1927
        {
1928
            if (symbol != null)
1929
            {
1930
                string repID = symbol.AsLMRepresentation().Id;
1931
                string symbolUID = SPPIDUtil.FindSymbolByRepresentationID(document, repID).UID;
1932
                string lineUID = line.UID;
1933

    
1934
                SpecBreak startSpecBreak = document.SpecBreaks.Find(x =>
1935
                (x.DownStreamUID == symbolUID || x.UpStreamUID == symbolUID) &&
1936
                (x.DownStreamUID == lineUID || x.UpStreamUID == lineUID));
1937

    
1938
                EndBreak startEndBreak = document.EndBreaks.Find(x =>
1939
                (x.OWNER == symbolUID || x.PROPERTIES.Find(y => y.ATTRIBUTE == "Connected Item").VALUE == symbolUID) &&
1940
                (x.OWNER == lineUID || x.PROPERTIES.Find(y => y.ATTRIBUTE == "Connected Item").VALUE == lineUID));
1941

    
1942
                if (startSpecBreak != null || startEndBreak != null)
1943
                    result = true;
1944
            }
1945
        }
1946

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

    
1973
            List<double[]> vertices = new List<double[]>();
1974
            for (int i = 1; i <= verticesCount; i++)
1975
            {
1976
                double x = 0;
1977
                double y = 0;
1978
                lineStringGeometry.GetVertex(i, ref x, ref y);
1979
                vertices.Add(new double[] { x, y });
1980
            }
1981

    
1982
            for (int i = 0; i < vertices.Count; i++)
1983
            {
1984
                double[] points = vertices[i];
1985
                // 시작 심볼이 있고 첫번째 좌표일 때
1986
                if (startSymbol != null && i == 0)
1987
                {
1988
                    if (bStart)
1989
                    {
1990
                        SlopeType slopeType = SPPIDUtil.CalcSlope(points[0], points[1], vertices[i + 1][0], vertices[i + 1][1]);
1991
                        if (slopeType == SlopeType.HORIZONTAL)
1992
                            placeRunInputs.AddPoint(points[0], -0.1);
1993
                        else if (slopeType == SlopeType.VERTICAL)
1994
                            placeRunInputs.AddPoint(-0.1, points[1]);
1995
                        else
1996
                            placeRunInputs.AddPoint(points[0], -0.1);
1997

    
1998
                        placeRunInputs.AddPoint(points[0], points[1]);
1999
                    }
2000
                    else
2001
                    {
2002
                        placeRunInputs.AddSymbolTarget(startSymbol, points[0], points[1], diagonal);
2003
                    }
2004
                }
2005
                // 마지막 심볼이 있고 마지막 좌표일 때
2006
                else if (endSymbol != null && i == vertices.Count - 1)
2007
                {
2008
                    if (bEnd)
2009
                    {
2010
                        placeRunInputs.AddPoint(points[0], points[1]);
2011

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

    
2035
            _placement.PIDRemovePlacement(prevLMConnector.AsLMRepresentation());
2036
            newConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
2037

    
2038
            ReleaseCOMObjects(placeRunInputs);
2039
            ReleaseCOMObjects(_LMAItem);
2040
            ReleaseCOMObjects(modelItem);
2041

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

    
2061
                if (endSymbol != null && bEnd)
2062
                {
2063
                    if (startSymbol != null)
2064
                    {
2065
                        Dictionary<LMConnector, List<double[]>> dicVertices = GetPipeRunVertices(newConnector.ModelItemID);
2066
                        newConnector = dicVertices.First().Key;
2067
                    }
2068

    
2069
                    _LMAItem = _placement.PIDCreateItem(symbolPath);
2070
                    placeRunInputs = new PlaceRunInputs();
2071
                    placeRunInputs.AddSymbolTarget(endSymbol, vertices[vertices.Count - 1][0], vertices[vertices.Count - 1][1]);
2072
                    placeRunInputs.AddConnectorTarget(newConnector, vertices[vertices.Count - 1][0], vertices[vertices.Count - 1][1]);
2073
                    LMConnector _LMConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
2074
                    if (_LMConnector != null)
2075
                    {
2076
                        RemoveConnectorForReModelingLine(newConnector);
2077
                        ZeroLengthModelItemIDReverse.Add(_LMConnector.ModelItemID);
2078
                        ReleaseCOMObjects(_LMConnector);
2079
                    }
2080
                    ReleaseCOMObjects(placeRunInputs);
2081
                    ReleaseCOMObjects(_LMAItem);
2082
                }
2083

    
2084
                line.SPPID.ModelItemId = newConnector.ModelItemID;
2085
                ReleaseCOMObjects(newConnector);
2086
            }
2087

    
2088
            ReleaseCOMObjects(modelItem);
2089
        }
2090

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

    
2113
                    if (result)
2114
                        break;
2115
                }
2116
            }
2117
            foreach (var item in dicVertices)
2118
                ReleaseCOMObjects(item.Key);
2119
        }
2120

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

    
2146
                            if (child != null)
2147
                                break;
2148
                        }
2149

    
2150
                        if (child != null)
2151
                            _LMSymbol = dataSource.GetSymbol(child.SPPID.RepresentationId);
2152
                    }
2153

    
2154
                    break;
2155
                }
2156
            }
2157

    
2158
            return _LMSymbol;
2159
        }
2160

    
2161
        /// <summary>
2162
        /// Connector를 가지고 있는 ChildSymbol Object 반환
2163
        /// </summary>
2164
        /// <param name="item"></param>
2165
        /// <param name="connector"></param>
2166
        /// <returns></returns>
2167
        private ChildSymbol GetChildSymbolByConnector(ChildSymbol item, Connector connector)
2168
        {
2169
            foreach (var childSymbol in item.ChildSymbols)
2170
            {
2171
                if (childSymbol.Connectors.Contains(connector))
2172
                    return childSymbol;
2173
                else
2174
                    return GetChildSymbolByConnector(childSymbol, connector);
2175
            }
2176

    
2177
            return null;
2178
        }
2179

    
2180
        /// <summary>
2181
        /// EndBreak 모델링 메서드
2182
        /// </summary>
2183
        /// <param name="endBreak"></param>
2184
        private void EndBreakModeling(EndBreak endBreak)
2185
        {
2186
            object ownerObj = SPPIDUtil.FindObjectByUID(document, endBreak.OWNER);
2187
            object connectedItem = SPPIDUtil.FindObjectByUID(document, endBreak.PROPERTIES.Find(x => x.ATTRIBUTE == "Connected Item").VALUE);
2188
            LMConnector targetLMConnector = FindBreakLineTarget(ownerObj, connectedItem);
2189

    
2190
            if (targetLMConnector != null)
2191
            {
2192
                double[] point = GetSegmentPoint(ownerObj, connectedItem, targetLMConnector);
2193
                Array array = null;
2194
                if (point != null)
2195
                    array = new double[] { 0, point[0], point[1] };
2196
                else
2197
                    array = new double[] { 0, endBreak.SPPID.ORIGINAL_X, endBreak.SPPID.ORIGINAL_Y };
2198
                LMLabelPersist _LmLabelPersist = _placement.PIDPlaceLabel(endBreak.SPPID.MAPPINGNAME, ref array, LabeledItem: targetLMConnector.AsLMRepresentation(), IsLeaderVisible: true);
2199
                if (_LmLabelPersist != null)
2200
                {
2201
                    endBreak.SPPID.RepresentationId = _LmLabelPersist.AsLMRepresentation().Id;
2202
                    if (_LmLabelPersist.ModelItemObject != null)
2203
                        endBreak.SPPID.ModelItemID = _LmLabelPersist.ModelItemID;
2204
                    endBreak.SPPID.GraphicOID = _LmLabelPersist.get_GraphicOID().ToString();
2205
                    ReleaseCOMObjects(_LmLabelPersist);
2206
                }
2207
                ReleaseCOMObjects(targetLMConnector);
2208
            }
2209
        }
2210

    
2211
        private LMConnector ReModelingLMConnector(LMConnector connector, string changeSymbolPath = null)
2212
        {
2213
            string symbolPath = string.Empty;
2214
            #region get symbol path
2215
            if (string.IsNullOrEmpty(changeSymbolPath))
2216
            {
2217
                LMModelItem modelItem = dataSource.GetModelItem(connector.ModelItemID);
2218
                symbolPath = GetSPPIDFileName(modelItem);
2219
                ReleaseCOMObjects(modelItem);
2220
            }
2221
            else
2222
                symbolPath = changeSymbolPath;
2223
            
2224
            #endregion
2225

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

    
2234
            if (Convert.ToBoolean(connector.get_IsZeroLength()))
2235
            {
2236
                double[] vertices = null;
2237
                lineStringGeometry.GetVertices(ref verticesCount, ref vertices);
2238
                double x = 0;
2239
                double y = 0;
2240
                lineStringGeometry.GetVertex(1, ref x, ref y);
2241

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

    
2247
                if (flowDirection == "End 1 is downstream (Outlet)")
2248
                {
2249
                    placeRunInputs.AddSymbolTarget(connector.ConnectItem2SymbolObject, x, y);
2250
                    placeRunInputs.AddSymbolTarget(connector.ConnectItem1SymbolObject, x, y);
2251
                    flowDirection = "End 1 is upstream (Inlet)";
2252
                }
2253
                else
2254
                {
2255
                    placeRunInputs.AddSymbolTarget(connector.ConnectItem1SymbolObject, x, y);
2256
                    placeRunInputs.AddSymbolTarget(connector.ConnectItem2SymbolObject, x, y);
2257
                }
2258

    
2259
                _placement.PIDRemovePlacement(connector.AsLMRepresentation());
2260
                newConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
2261
                ZeroLengthSymbolToSymbolModelItemID.Add(newConnector.ModelItemID);
2262
                if (!string.IsNullOrEmpty(flowDirection))
2263
                    newConnector.ModelItemObject.Attributes["FlowDirection"].set_Value(flowDirection);
2264
            }
2265
            else
2266
            {
2267
                List<double[]> vertices = new List<double[]>();
2268
                for (int i = 1; i <= verticesCount; i++)
2269
                {
2270
                    double x = 0;
2271
                    double y = 0;
2272
                    lineStringGeometry.GetVertex(i, ref x, ref y);
2273
                    vertices.Add(new double[] { x, y });
2274
                }
2275

    
2276
                for (int i = 0; i < vertices.Count; i++)
2277
                {
2278
                    double[] points = vertices[i];
2279
                    if (i == 0)
2280
                    {
2281
                        if (connector.ConnectItem1SymbolObject != null)
2282
                            placeRunInputs.AddSymbolTarget(connector.ConnectItem1SymbolObject, points[0], points[1]);
2283
                        else
2284
                            placeRunInputs.AddPoint(points[0], points[1]);
2285
                    }
2286
                    else if (i == vertices.Count - 1)
2287
                    {
2288
                        if (connector.ConnectItem2SymbolObject != null)
2289
                            placeRunInputs.AddSymbolTarget(connector.ConnectItem2SymbolObject, points[0], points[1]);
2290
                        else
2291
                            placeRunInputs.AddPoint(points[0], points[1]);
2292
                    }
2293
                    else
2294
                        placeRunInputs.AddPoint(points[0], points[1]);
2295
                }
2296

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

    
2299
                _placement.PIDRemovePlacement(connector.AsLMRepresentation());
2300
                newConnector = _placement.PIDPlaceRun(_LMAItem, placeRunInputs);
2301

    
2302
                foreach (var line in lines)
2303
                    line.SPPID.ModelItemId = newConnector.ModelItemID;
2304
            }
2305

    
2306

    
2307
            return newConnector;
2308
        }
2309

    
2310
        /// <summary>
2311
        /// SpecBreak Modeling 메서드
2312
        /// </summary>
2313
        /// <param name="specBreak"></param>
2314
        private void SpecBreakModeling(SpecBreak specBreak)
2315
        {
2316
            object upStreamObj = SPPIDUtil.FindObjectByUID(document, specBreak.UpStreamUID);
2317
            object downStreamObj = SPPIDUtil.FindObjectByUID(document, specBreak.DownStreamUID);
2318

    
2319
            if (upStreamObj != null &&
2320
                downStreamObj != null)
2321
            {
2322
                LMConnector targetLMConnector = FindBreakLineTarget(upStreamObj, downStreamObj);
2323

    
2324
                if (targetLMConnector != null)
2325
                {
2326
                    foreach (var attribute in specBreak.ATTRIBUTES)
2327
                    {
2328
                        AttributeMapping mapping = document.AttributeMappings.Find(x => x.UID == attribute.UID);
2329
                        if (mapping != null && !string.IsNullOrEmpty(mapping.SPPIDSYMBOLNAME) && mapping.SPPIDSYMBOLNAME != "None")
2330
                        {
2331
                            string MappingPath = mapping.SPPIDSYMBOLNAME;
2332
                            double[] point = GetSegmentPoint(upStreamObj, downStreamObj, targetLMConnector);
2333
                            Array array = null;
2334
                            if (point != null)
2335
                                array = new double[] { 0, point[0], point[1] };
2336
                            else
2337
                                array = new double[] { 0, specBreak.SPPID.ORIGINAL_X, specBreak.SPPID.ORIGINAL_Y };
2338
                            LMLabelPersist _LmLabelPersist = _placement.PIDPlaceLabel(MappingPath, ref array, LabeledItem: targetLMConnector.AsLMRepresentation(), IsLeaderVisible: mapping.LeaderLine);
2339

    
2340
                            if (_LmLabelPersist != null)
2341
                            {
2342
                                specBreak.SPPID.RepresentationId = _LmLabelPersist.AsLMRepresentation().Id;
2343
                                if (_LmLabelPersist.ModelItemObject != null)
2344
                                    specBreak.SPPID.ModelItemID = _LmLabelPersist.ModelItemID;
2345
                                specBreak.SPPID.GraphicOID = _LmLabelPersist.get_GraphicOID().ToString();
2346
                                ReleaseCOMObjects(_LmLabelPersist);
2347
                            }
2348
                        }
2349
                    }
2350
                    ReleaseCOMObjects(targetLMConnector);
2351
                }
2352
            }
2353
        }
2354

    
2355
        private LMConnector FindBreakLineTarget(object targetObj, object connectedObj)
2356
        {
2357
            LMConnector targetConnector = null;
2358
            Symbol targetSymbol = targetObj as Symbol;
2359
            Symbol connectedSymbol = connectedObj as Symbol;
2360
            Line targetLine = targetObj as Line;
2361
            Line connectedLine = connectedObj as Line;
2362
            if (targetSymbol != null && connectedSymbol != null)
2363
            {
2364
                LMSymbol targetLMSymbol = dataSource.GetSymbol(targetSymbol.SPPID.RepresentationId);
2365
                LMSymbol connectedLMSymbol = dataSource.GetSymbol(connectedSymbol.SPPID.RepresentationId);
2366

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

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

    
2384
                foreach (LMConnector connector in targetLMSymbol.Avoid2Connectors)
2385
                {
2386
                    if (connector.get_ItemStatus() != "Active")
2387
                        continue;
2388

    
2389
                    if (connector.ConnectItem1SymbolObject.Id == connectedLMSymbol.Id)
2390
                    {
2391
                        targetConnector = connector;
2392
                        break;
2393
                    }
2394
                    else if (connector.ConnectItem2SymbolObject.Id == connectedLMSymbol.Id)
2395
                    {
2396
                        targetConnector = connector;
2397
                        break;
2398
                    }
2399
                }
2400

    
2401
                ReleaseCOMObjects(targetLMSymbol);
2402
                ReleaseCOMObjects(connectedLMSymbol);
2403
            }
2404
            else if (targetLine != null && connectedLine != null)
2405
            {
2406
                LMModelItem targetModelItem = dataSource.GetModelItem(targetLine.SPPID.ModelItemId);
2407
                LMModelItem connectedModelItem = dataSource.GetModelItem(connectedLine.SPPID.ModelItemId);
2408

    
2409
                if (targetModelItem != null && targetModelItem.get_ItemStatus() == "Active" && connectedModelItem != null && connectedModelItem.get_ItemStatus() == "Active")
2410
                {
2411
                    foreach (LMRepresentation rep in targetModelItem.Representations)
2412
                    {
2413
                        if (targetConnector != null)
2414
                            break;
2415

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

    
2420
                            if (IsConnected(_LMConnector, connectedModelItem))
2421
                                targetConnector = _LMConnector;
2422
                            else
2423
                                ReleaseCOMObjects(_LMConnector);
2424
                        }
2425
                    }
2426

    
2427
                    ReleaseCOMObjects(targetModelItem);
2428
                }
2429
            }
2430
            else
2431
            {
2432
                LMSymbol connectedLMSymbol = null;
2433
                if (connectedSymbol != null)
2434
                    connectedLMSymbol = dataSource.GetSymbol(connectedSymbol.SPPID.RepresentationId);
2435
                else if (targetSymbol != null)
2436
                    connectedLMSymbol = dataSource.GetSymbol(targetSymbol.SPPID.RepresentationId);
2437
                else
2438
                {
2439

    
2440
                }
2441
                LMModelItem targetModelItem = null;
2442
                if (targetLine != null)
2443
                    targetModelItem = dataSource.GetModelItem(targetLine.SPPID.ModelItemId);
2444
                else if (connectedLine != null)
2445
                    targetModelItem = dataSource.GetModelItem(connectedLine.SPPID.ModelItemId);
2446
                else
2447
                {
2448

    
2449
                }
2450
                if (connectedLMSymbol != null && targetModelItem != null)
2451
                {
2452
                    foreach (LMConnector connector in connectedLMSymbol.Avoid1Connectors)
2453
                    {
2454
                        if (connector.get_ItemStatus() != "Active")
2455
                            continue;
2456

    
2457
                        if (IsConnected(connector, targetModelItem))
2458
                        {
2459
                            targetConnector = connector;
2460
                            break;
2461
                        }
2462
                    }
2463

    
2464
                    if (targetConnector == null)
2465
                    {
2466
                        foreach (LMConnector connector in connectedLMSymbol.Avoid2Connectors)
2467
                        {
2468
                            if (connector.get_ItemStatus() != "Active")
2469
                                continue;
2470

    
2471
                            if (IsConnected(connector, targetModelItem))
2472
                            {
2473
                                targetConnector = connector;
2474
                                break;
2475
                            }
2476
                        }
2477
                    }
2478
                }
2479

    
2480
            }
2481

    
2482
            return targetConnector;
2483
        }
2484

    
2485
        private double[] GetSegmentPoint(object targetObj, object connObj, LMConnector targetConnector)
2486
        {
2487
            double[] result = null;
2488
            if (Convert.ToBoolean(targetConnector.get_IsZeroLength()))
2489
            {
2490
                return GetConnectorVertices(targetConnector)[0];
2491
            }
2492
            else
2493
            {
2494
                if (targetObj.GetType() == typeof(Line) && connObj.GetType() == typeof(Line))
2495
                {
2496
                    Line line = connObj as Line;
2497
                    LMConnector connectedConnector = null;
2498
                    int connIndex = 0;
2499
                    LMModelItem modelItem = dataSource.GetModelItem(line.SPPID.ModelItemId);
2500
                    FindConnectedConnector(targetConnector, modelItem, ref connectedConnector, ref connIndex);
2501

    
2502
                    List<double[]> vertices = GetConnectorVertices(targetConnector);
2503

    
2504
                    ReleaseCOMObjects(modelItem);
2505
                    ReleaseCOMObjects(connectedConnector);
2506

    
2507
                    if (vertices.Count > 0)
2508
                    {
2509
                        if (connIndex == 1)
2510
                            return vertices[0];
2511
                        else if (connIndex == 2)
2512
                            return vertices[vertices.Count - 1];
2513
                    }
2514
                }
2515
                else
2516
                {
2517
                    Log.Write("error in GetSegemtPoint");
2518
                }
2519
            }
2520

    
2521
            return result;
2522
        }
2523

    
2524
        private bool IsConnected(LMConnector connector, LMModelItem modelItem)
2525
        {
2526
            bool result = false;
2527

    
2528
            foreach (LMRepresentation rep in modelItem.Representations)
2529
            {
2530
                if (result)
2531
                    break;
2532

    
2533
                if (rep.Attributes["RepresentationType"].get_Value() == "Connector" && rep.Attributes["ItemStatus"].get_Value() == "Active")
2534
                {
2535
                    LMConnector _LMConnector = dataSource.GetConnector(rep.Id);
2536

    
2537
                    if (_LMConnector.ConnectItem1SymbolObject != null &&
2538
                        connector.ConnectItem1SymbolObject != null &&
2539
                        _LMConnector.ConnectItem1SymbolObject.Id == connector.ConnectItem1SymbolObject.Id)
2540
                    {
2541
                        result = true;
2542
                        ReleaseCOMObjects(_LMConnector);
2543
                        break;
2544
                    }
2545
                    else if (_LMConnector.ConnectItem1SymbolObject != null &&
2546
                        connector.ConnectItem2SymbolObject != null &&
2547
                        _LMConnector.ConnectItem1SymbolObject.Id == connector.ConnectItem2SymbolObject.Id)
2548
                    {
2549
                        result = true;
2550
                        ReleaseCOMObjects(_LMConnector);
2551
                        break;
2552
                    }
2553
                    else if (_LMConnector.ConnectItem2SymbolObject != null &&
2554
                        connector.ConnectItem1SymbolObject != null &&
2555
                        _LMConnector.ConnectItem2SymbolObject.Id == connector.ConnectItem1SymbolObject.Id)
2556
                    {
2557
                        result = true;
2558
                        ReleaseCOMObjects(_LMConnector);
2559
                        break;
2560
                    }
2561
                    else if (_LMConnector.ConnectItem2SymbolObject != null &&
2562
                        connector.ConnectItem2SymbolObject != null &&
2563
                        _LMConnector.ConnectItem2SymbolObject.Id == connector.ConnectItem2SymbolObject.Id)
2564
                    {
2565
                        result = true;
2566
                        ReleaseCOMObjects(_LMConnector);
2567
                        break;
2568
                    }
2569

    
2570
                    ReleaseCOMObjects(_LMConnector);
2571
                }
2572
            }
2573

    
2574

    
2575
            return result;
2576
        }
2577

    
2578
        private void FindConnectedConnector(LMConnector connector, LMModelItem modelItem, ref LMConnector connectedConnector, ref int connectorIndex)
2579
        {
2580
            foreach (LMRepresentation rep in modelItem.Representations)
2581
            {
2582
                if (connectedConnector != null)
2583
                    break;
2584

    
2585
                if (rep.Attributes["RepresentationType"].get_Value() == "Connector" && rep.Attributes["ItemStatus"].get_Value() == "Active")
2586
                {
2587
                    LMConnector _LMConnector = dataSource.GetConnector(rep.Id);
2588

    
2589
                    if (_LMConnector.ConnectItem1SymbolObject != null &&
2590
                        connector.ConnectItem1SymbolObject != null &&
2591
                        _LMConnector.ConnectItem1SymbolObject.Id == connector.ConnectItem1SymbolObject.Id)
2592
                    {
2593
                        connectedConnector = _LMConnector;
2594
                        connectorIndex = 1;
2595
                        break;
2596
                    }
2597
                    else if (_LMConnector.ConnectItem1SymbolObject != null &&
2598
                        connector.ConnectItem2SymbolObject != null &&
2599
                        _LMConnector.ConnectItem1SymbolObject.Id == connector.ConnectItem2SymbolObject.Id)
2600
                    {
2601
                        connectedConnector = _LMConnector;
2602
                        connectorIndex = 2;
2603
                        break;
2604
                    }
2605
                    else if (_LMConnector.ConnectItem2SymbolObject != null &&
2606
                        connector.ConnectItem1SymbolObject != null &&
2607
                        _LMConnector.ConnectItem2SymbolObject.Id == connector.ConnectItem1SymbolObject.Id)
2608
                    {
2609
                        connectedConnector = _LMConnector;
2610
                        connectorIndex = 1;
2611
                        break;
2612
                    }
2613
                    else if (_LMConnector.ConnectItem2SymbolObject != null &&
2614
                        connector.ConnectItem2SymbolObject != null &&
2615
                        _LMConnector.ConnectItem2SymbolObject.Id == connector.ConnectItem2SymbolObject.Id)
2616
                    {
2617
                        connectedConnector = _LMConnector;
2618
                        connectorIndex = 2;
2619
                        break;
2620
                    }
2621

    
2622
                    if (connectedConnector == null)
2623
                        ReleaseCOMObjects(_LMConnector);
2624
                }
2625
            }
2626
        }
2627

    
2628
        /// <summary>
2629
        /// FromModelItem을 ToModelItem으로 PipeRunJoin하는 메서드
2630
        /// </summary>
2631
        /// <param name="modelItemID1"></param>
2632
        /// <param name="modelItemID2"></param>
2633
        private void JoinRun(string modelId1, string modelId2, ref string survivorId)
2634
        {
2635
            try
2636
            {
2637
                LMModelItem modelItem1 = dataSource.GetModelItem(modelId1);
2638
                _LMAItem item1 = modelItem1.AsLMAItem();
2639
                LMModelItem modelItem2 = dataSource.GetModelItem(modelId2);
2640
                _LMAItem item2 = modelItem2.AsLMAItem();
2641

    
2642
                // item2가 item1으로 조인
2643
                _placement.PIDJoinRuns(ref item1, ref item2);
2644
                item1.Commit();
2645
                item2.Commit();
2646

    
2647
                string beforeID = string.Empty;
2648
                string afterID = string.Empty;
2649

    
2650
                if (modelItem1.get_ItemStatus() == "Active" && modelItem2.get_ItemStatus() != "Active")
2651
                {
2652
                    beforeID = modelItem2.Id;
2653
                    afterID = modelItem1.Id;
2654
                    survivorId = afterID;
2655
                    LMAAttribute attribute = modelItem1.Attributes["FlowDirection"];
2656
                    if (attribute != null)
2657
                        attribute.set_Value("End 1 is upstream (Inlet)");
2658
                }
2659
                else if (modelItem1.get_ItemStatus() != "Active" && modelItem2.get_ItemStatus() == "Active")
2660
                {
2661
                    beforeID = modelItem1.Id;
2662
                    afterID = modelItem2.Id;
2663
                    survivorId = afterID;
2664
                    LMAAttribute attribute = modelItem2.Attributes["FlowDirection"];
2665
                    if (attribute != null)
2666
                        attribute.set_Value("End 1 is upstream (Inlet)");
2667
                }
2668
                else if (modelItem1.get_ItemStatus() == "Active" && modelItem2.get_ItemStatus() == "Active")
2669
                    survivorId = null;
2670
                else
2671
                {
2672
                    Log.Write("잘못된 경우");
2673
                    survivorId = null;
2674
                }
2675

    
2676
                if (!string.IsNullOrEmpty(beforeID) && !string.IsNullOrEmpty(afterID))
2677
                {
2678
                    List<Line> lines = SPPIDUtil.FindLinesByModelId(document, beforeID);
2679
                    foreach (var line in lines)
2680
                        line.SPPID.ModelItemId = afterID;
2681
                }
2682

    
2683
                ReleaseCOMObjects(modelItem1);
2684
                ReleaseCOMObjects(item1);
2685
                ReleaseCOMObjects(modelItem2);
2686
                ReleaseCOMObjects(item2);
2687
            }
2688
            catch (Exception ex)
2689
            {
2690
                Log.Write("Join Error");
2691
                Log.Write(ex.Message);
2692
            }
2693
        }
2694

    
2695
        private List<string> FindOtherModelItemBySymbolWhereTypePipeRun(LMSymbol symbol, string modelId)
2696
        {
2697
            List<string> modelItemIDs = new List<string>();
2698
            foreach (LMConnector connector in symbol.Avoid1Connectors)
2699
            {
2700
                LMModelItem modelItem = connector.ModelItemObject;
2701
                if (modelItem.get_ItemStatus() == "Active" && modelItem.get_ItemTypeName().ToString() == "PipeRun" && modelItem.Id != modelId)
2702
                    modelItemIDs.Add(modelItem.Id);
2703
                ReleaseCOMObjects(modelItem);
2704
                ReleaseCOMObjects(connector);
2705
            }
2706

    
2707
            foreach (LMConnector connector in symbol.Avoid2Connectors)
2708
            {
2709
                LMModelItem modelItem = connector.ModelItemObject;
2710
                if (modelItem.get_ItemStatus() == "Active" && modelItem.get_ItemTypeName().ToString() == "PipeRun" && modelItem.Id != modelId)
2711
                    modelItemIDs.Add(modelItem.Id);
2712
                ReleaseCOMObjects(modelItem);
2713
                ReleaseCOMObjects(connector);
2714
            }
2715

    
2716

    
2717
            List<string> result = new List<string>();
2718
            modelItemIDs = modelItemIDs.Distinct().ToList();
2719
            string originalName = GetSPPIDFileName(modelId);
2720
            foreach (var item in modelItemIDs)
2721
            {
2722
                string fileName = GetSPPIDFileName(item);
2723
                if (originalName == fileName)
2724
                    result.Add(item);
2725
                else
2726
                {
2727
                    LMConnector connector = GetLMConnectorOnlyOne(item);
2728
                    if (connector != null && Convert.ToBoolean(connector.get_IsZeroLength()))
2729
                    {
2730
                        result.Add(item);
2731
                        ReleaseCOMObjects(connector);
2732
                    }
2733
                }
2734
            }
2735
            
2736
            return result;
2737
        }
2738

    
2739
        /// <summary>
2740
        /// PipeRun의 좌표를 가져오는 메서드
2741
        /// </summary>
2742
        /// <param name="modelId"></param>
2743
        /// <returns></returns>
2744
        private Dictionary<LMConnector, List<double[]>> GetPipeRunVertices(string modelId)
2745
        {
2746
            Dictionary<LMConnector, List<double[]>> connectorVertices = new Dictionary<LMConnector, List<double[]>>();
2747
            LMModelItem modelItem = dataSource.GetModelItem(modelId);
2748

    
2749
            if (modelItem != null)
2750
            {
2751
                foreach (LMRepresentation rep in modelItem.Representations)
2752
                {
2753
                    if (rep.Attributes["RepresentationType"].get_Value() == "Connector" && rep.Attributes["ItemStatus"].get_Value() == "Active")
2754
                    {
2755
                        LMConnector _LMConnector = dataSource.GetConnector(rep.Id);
2756
                        connectorVertices.Add(_LMConnector, new List<double[]>());
2757
                        dynamic OID = rep.get_GraphicOID().ToString();
2758
                        DependencyObject drawingObject = radApp.ActiveDocument.ActiveSheet.DrawingObjects[OID];
2759
                        Ingr.RAD2D.LineStringGeometry2d lineStringGeometry = drawingObject.GetGeometry() as Ingr.RAD2D.LineStringGeometry2d;
2760
                        int verticesCount = lineStringGeometry.VertexCount;
2761
                        double[] vertices = null;
2762
                        lineStringGeometry.GetVertices(ref verticesCount, ref vertices);
2763
                        for (int i = 0; i < verticesCount; i++)
2764
                        {
2765
                            double x = 0;
2766
                            double y = 0;
2767
                            lineStringGeometry.GetVertex(i + 1, ref x, ref y);
2768
                            connectorVertices[_LMConnector].Add(new double[] { x, y });
2769
                        }
2770
                    }
2771
                }
2772

    
2773
                ReleaseCOMObjects(modelItem);
2774
            }
2775

    
2776
            return connectorVertices;
2777
        }
2778

    
2779
        private List<double[]> GetConnectorVertices(LMConnector connector)
2780
        {
2781
            List<double[]> vertices = new List<double[]>();
2782
            if (connector != null)
2783
            {
2784
                dynamic OID = connector.get_GraphicOID().ToString();
2785
                DependencyObject drawingObject = radApp.ActiveDocument.ActiveSheet.DrawingObjects[OID];
2786
                Ingr.RAD2D.LineStringGeometry2d lineStringGeometry = drawingObject.GetGeometry() as Ingr.RAD2D.LineStringGeometry2d;
2787
                int verticesCount = lineStringGeometry.VertexCount;
2788
                double[] value = null;
2789
                lineStringGeometry.GetVertices(ref verticesCount, ref value);
2790
                for (int i = 0; i < verticesCount; i++)
2791
                {
2792
                    double x = 0;
2793
                    double y = 0;
2794
                    lineStringGeometry.GetVertex(i + 1, ref x, ref y);
2795
                    vertices.Add(new double[] { x, y });
2796
                }
2797
            }
2798
            return vertices;
2799
        }
2800

    
2801
        /// <summary>
2802
        /// 좌표로 PipeRun의 Connector중에 어느 Connector에 가까운지/붙을지 가져오는 메서드 - 조건에 안맞아서 못찾을시 제일 가까운 점으로 가져오는 방식
2803
        /// </summary>
2804
        /// <param name="connectorVertices"></param>
2805
        /// <param name="connX"></param>
2806
        /// <param name="connY"></param>
2807
        /// <returns></returns>
2808
        private LMConnector FindTargetLMConnectorForLabel(Dictionary<LMConnector, List<double[]>> connectorVertices, double connX, double connY)
2809
        {
2810
            double length = double.MaxValue;
2811
            LMConnector targetConnector = null;
2812
            foreach (var item in connectorVertices)
2813
            {
2814
                List<double[]> points = item.Value;
2815
                for (int i = 0; i < points.Count - 1; i++)
2816
                {
2817
                    double[] point1 = points[i];
2818
                    double[] point2 = points[i + 1];
2819
                    double x1 = Math.Min(point1[0], point2[0]);
2820
                    double y1 = Math.Min(point1[1], point2[1]);
2821
                    double x2 = Math.Max(point1[0], point2[0]);
2822
                    double y2 = Math.Max(point1[1], point2[1]);
2823

    
2824
                    if ((x1 <= connX && x2 >= connX) ||
2825
                        (y1 <= connY && y2 >= connY))
2826
                    {
2827
                        double distance = SPPIDUtil.CalcPointToPointdDistance(point1[0], point1[1], connX, connY);
2828
                        if (length >= distance)
2829
                        {
2830
                            targetConnector = item.Key;
2831
                            length = distance;
2832
                        }
2833

    
2834
                        distance = SPPIDUtil.CalcPointToPointdDistance(point2[0], point2[1], connX, connY);
2835
                        if (length >= distance)
2836
                        {
2837
                            targetConnector = item.Key;
2838
                            length = distance;
2839
                        }
2840
                    }
2841
                }
2842
            }
2843

    
2844
            // 못찾았을때.
2845
            length = double.MaxValue;
2846
            if (targetConnector == null)
2847
            {
2848
                foreach (var item in connectorVertices)
2849
                {
2850
                    List<double[]> points = item.Value;
2851

    
2852
                    foreach (double[] point in points)
2853
                    {
2854
                        double distance = SPPIDUtil.CalcPointToPointdDistance(point[0], point[1], connX, connY);
2855
                        if (length >= distance)
2856
                        {
2857
                            targetConnector = item.Key;
2858
                            length = distance;
2859
                        }
2860
                    }
2861
                }
2862
            }
2863

    
2864
            return targetConnector;
2865
        }
2866

    
2867
        private LMConnector FindTargetLMConnectorForBranch(Line line, Line targetLine, ref double x, ref double y)
2868
        {
2869
            Dictionary<LMConnector, List<double[]>> vertices = GetPipeRunVertices(targetLine.SPPID.ModelItemId);
2870
            if (vertices.Count == 0)
2871
                return null;
2872

    
2873
            double length = double.MaxValue;
2874
            LMConnector targetConnector = null;
2875
            double[] resultPoint = null;
2876
            List<double[]> targetVertices = null;
2877

    
2878
            // Vertices 포인트에 제일 가까운곳
2879
            foreach (var item in vertices)
2880
            {
2881
                List<double[]> points = item.Value;
2882
                for (int i = 0; i < points.Count; i++)
2883
                {
2884
                    double[] point = points[i];
2885
                    double tempX = point[0];
2886
                    double tempY = point[1];
2887

    
2888
                    double distance = SPPIDUtil.CalcPointToPointdDistance(tempX, tempY, x, y);
2889
                    if (length >= distance)
2890
                    {
2891
                        targetConnector = item.Key;
2892
                        length = distance;
2893
                        resultPoint = point;
2894
                        targetVertices = item.Value;
2895
                    }
2896
                }
2897
            }
2898

    
2899
            // Vertices Cross에 제일 가까운곳
2900
            foreach (var item in vertices)
2901
            {
2902
                List<double[]> points = item.Value;
2903
                for (int i = 0; i < points.Count - 1; i++)
2904
                {
2905
                    double[] point1 = points[i];
2906
                    double[] point2 = points[i + 1];
2907

    
2908
                    double maxLineX = Math.Max(point1[0], point2[0]);
2909
                    double minLineX = Math.Min(point1[0], point2[0]);
2910
                    double maxLineY = Math.Max(point1[1], point2[1]);
2911
                    double minLineY = Math.Min(point1[1], point2[1]);
2912

    
2913
                    SlopeType slope = SPPIDUtil.CalcSlope(minLineX, minLineY, maxLineX, maxLineY);
2914

    
2915
                    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]);
2916
                    if (crossingPoint != null)
2917
                    {
2918
                        double distance = SPPIDUtil.CalcPointToPointdDistance(crossingPoint[0], crossingPoint[1], x, y);
2919
                        if (length >= distance)
2920
                        {
2921
                            if (slope == SlopeType.Slope &&
2922
                                minLineX <= crossingPoint[0] && maxLineX >= crossingPoint[0] &&
2923
                                minLineY <= crossingPoint[1] && maxLineY >= crossingPoint[1])
2924
                            {
2925
                                targetConnector = item.Key;
2926
                                length = distance;
2927
                                resultPoint = crossingPoint;
2928
                                targetVertices = item.Value;
2929
                            }
2930
                            else if (slope == SlopeType.HORIZONTAL &&
2931
                                minLineX <= crossingPoint[0] && maxLineX >= crossingPoint[0])
2932
                            {
2933
                                targetConnector = item.Key;
2934
                                length = distance;
2935
                                resultPoint = crossingPoint;
2936
                                targetVertices = item.Value;
2937
                            }
2938
                            else if (slope == SlopeType.VERTICAL &&
2939
                               minLineY <= crossingPoint[1] && maxLineY >= crossingPoint[1])
2940
                            {
2941
                                targetConnector = item.Key;
2942
                                length = distance;
2943
                                resultPoint = crossingPoint;
2944
                                targetVertices = item.Value;
2945
                            }
2946
                        }
2947
                    }
2948
                }
2949
            }
2950

    
2951
            foreach (var item in vertices)
2952
                if (item.Key != null && item.Key != targetConnector)
2953
                    ReleaseCOMObjects(item.Key);
2954

    
2955
            if (SPPIDUtil.IsBranchLine(line, targetLine))
2956
            {
2957
                double tempResultX = resultPoint[0];
2958
                double tempResultY = resultPoint[1];
2959
                SPPIDUtil.ConvertGridPoint(ref tempResultX, ref tempResultY);
2960

    
2961
                GridSetting gridSetting = GridSetting.GetInstance();
2962

    
2963
                for (int i = 0; i < targetVertices.Count; i++)
2964
                {
2965
                    double[] point = targetVertices[i];
2966
                    double tempX = targetVertices[i][0];
2967
                    double tempY = targetVertices[i][1];
2968
                    SPPIDUtil.ConvertGridPoint(ref tempX, ref tempY);
2969
                    if (tempX == tempResultX && tempY == tempResultY)
2970
                    {
2971
                        if (i == 0)
2972
                        {
2973
                            LMSymbol connSymbol = targetConnector.ConnectItem1SymbolObject;
2974
                            bool containZeroLength = false;
2975
                            if (connSymbol != null)
2976
                            {
2977
                                foreach (LMConnector connector in connSymbol.Connect1Connectors)
2978
                                {
2979
                                    if (connector.get_ItemStatus() == "Active" && Convert.ToBoolean(connector.get_IsZeroLength()) == true)
2980
                                        containZeroLength = true;
2981
                                }
2982
                                foreach (LMConnector connector in connSymbol.Connect2Connectors)
2983
                                {
2984
                                    if (connector.get_ItemStatus() == "Active" && Convert.ToBoolean(connector.get_IsZeroLength()) == true)
2985
                                        containZeroLength = true;
2986
                                }
2987
                            }
2988

    
2989
                            if (connSymbol == null ||
2990
                                (connSymbol != null && connSymbol.get_ItemStatus() == "Active" && connSymbol.get_RepresentationType() != "Branch") ||
2991
                                containZeroLength)
2992
                            {
2993
                                bool bCalcX = false;
2994
                                bool bCalcY = false;
2995
                                if (targetLine.SlopeType == SlopeType.HORIZONTAL)
2996
                                    bCalcX = true;
2997
                                else if (targetLine.SlopeType == SlopeType.VERTICAL)
2998
                                    bCalcY = true;
2999
                                else
3000
                                {
3001
                                    bCalcX = true;
3002
                                    bCalcY = true;
3003
                                }
3004

    
3005
                                if (bCalcX)
3006
                                {
3007
                                    double nextX = targetVertices[i + 1][0];
3008
                                    double newX = 0;
3009
                                    if (nextX > tempX)
3010
                                    {
3011
                                        newX = tempX + gridSetting.Length;
3012
                                        if (newX > nextX)
3013
                                            newX = (point[0] + nextX) / 2;
3014
                                    }
3015
                                    else
3016
                                    {
3017
                                        newX = tempX - gridSetting.Length;
3018
                                        if (newX < nextX)
3019
                                            newX = (point[0] + nextX) / 2;
3020
                                    }
3021
                                    resultPoint = new double[] { newX, resultPoint[1] };
3022
                                }
3023

    
3024
                                if (bCalcY)
3025
                                {
3026
                                    double nextY = targetVertices[i + 1][1];
3027
                                    double newY = 0;
3028
                                    if (nextY > tempY)
3029
                                    {
3030
                                        newY = tempY + gridSetting.Length;
3031
                                        if (newY > nextY)
3032
                                            newY = (point[1] + nextY) / 2;
3033
                                    }
3034
                                    else
3035
                                    {
3036
                                        newY = tempY - gridSetting.Length;
3037
                                        if (newY < nextY)
3038
                                            newY = (point[1] + nextY) / 2;
3039
                                    }
3040
                                    resultPoint = new double[] { resultPoint[0], newY };
3041
                                }
3042
                            }
3043
                        }
3044
                        else if (i == targetVertices.Count - 1)
3045
                        {
3046
                            LMSymbol connSymbol = targetConnector.ConnectItem2SymbolObject;
3047
                            bool containZeroLength = false;
3048
                            if (connSymbol != null)
3049
                            {
3050
                                foreach (LMConnector connector in connSymbol.Connect1Connectors)
3051
                                {
3052
                                    if (connector.get_ItemStatus() == "Active" && Convert.ToBoolean(connector.get_IsZeroLength()) == true)
3053
                                        containZeroLength = true;
3054
                                }
3055
                                foreach (LMConnector connector in connSymbol.Connect2Connectors)
3056
                                {
3057
                                    if (connector.get_ItemStatus() == "Active" && Convert.ToBoolean(connector.get_IsZeroLength()) == true)
3058
                                        containZeroLength = true;
3059
                                }
3060
                            }
3061

    
3062
                            if (connSymbol == null ||
3063
                                 (connSymbol != null && connSymbol.get_ItemStatus() == "Active" && connSymbol.get_RepresentationType() != "Branch") ||
3064
                                containZeroLength)
3065
                            {
3066
                                bool bCalcX = false;
3067
                                bool bCalcY = false;
3068
                                if (targetLine.SlopeType == SlopeType.HORIZONTAL)
3069
                                    bCalcX = true;
3070
                                else if (targetLine.SlopeType == SlopeType.VERTICAL)
3071
                                    bCalcY = true;
3072
                                else
3073
                                {
3074
                                    bCalcX = true;
3075
                                    bCalcY = true;
3076
                                }
3077

    
3078
                                if (bCalcX)
3079
                                {
3080
                                    double nextX = targetVertices[i - 1][0];
3081
                                    double newX = 0;
3082
                                    if (nextX > tempX)
3083
                                    {
3084
                                        newX = tempX + gridSetting.Length;
3085
                                        if (newX > nextX)
3086
                                            newX = (point[0] + nextX) / 2;
3087
                                    }
3088
                                    else
3089
                                    {
3090
                                        newX = tempX - gridSetting.Length;
3091
                                        if (newX < nextX)
3092
                                            newX = (point[0] + nextX) / 2;
3093
                                    }
3094
                                    resultPoint = new double[] { newX, resultPoint[1] };
3095
                                }
3096

    
3097
                                if (bCalcY)
3098
                                {
3099
                                    double nextY = targetVertices[i - 1][1];
3100
                                    double newY = 0;
3101
                                    if (nextY > tempY)
3102
                                    {
3103
                                        newY = tempY + gridSetting.Length;
3104
                                        if (newY > nextY)
3105
                                            newY = (point[1] + nextY) / 2;
3106
                                    }
3107
                                    else
3108
                                    {
3109
                                        newY = tempY - gridSetting.Length;
3110
                                        if (newY < nextY)
3111
                                            newY = (point[1] + nextY) / 2;
3112
                                    }
3113
                                    resultPoint = new double[] { resultPoint[0], newY };
3114
                                }
3115
                            }
3116
                        }
3117
                        break;
3118
                    }
3119
                }
3120
            }
3121

    
3122
            x = resultPoint[0];
3123
            y = resultPoint[1];
3124

    
3125
            return targetConnector;
3126
        }
3127

    
3128
        private LMConnector GetLMConnectorOnlyOne(string modelItemID)
3129
        {
3130
            LMConnector result = null;
3131
            List<LMConnector> connectors = new List<LMConnector>();
3132
            LMModelItem modelItem = dataSource.GetModelItem(modelItemID);
3133

    
3134
            if (modelItem != null)
3135
            {
3136
                foreach (LMRepresentation rep in modelItem.Representations)
3137
                {
3138
                    if (rep.Attributes["RepresentationType"].get_Value() == "Connector" && rep.Attributes["ItemStatus"].get_Value() == "Active")
3139
                        connectors.Add(dataSource.GetConnector(rep.Id));
3140
                }
3141

    
3142
                ReleaseCOMObjects(modelItem);
3143
            }
3144

    
3145
            if (connectors.Count == 1)
3146
                result = connectors[0];
3147
            else
3148
                foreach (var item in connectors)
3149
                    ReleaseCOMObjects(item);
3150

    
3151
            return result;
3152
        }
3153

    
3154
        /// <summary>
3155
        /// Line Number Symbol을 실제로 Modeling하는 메서드
3156
        /// </summary>
3157
        /// <param name="lineNumber"></param>
3158
        private void LineNumberModelingOnlyOne(Line line)
3159
        {
3160
            LineNumber lineNumber = document.LINENUMBERS.Find(x => x.CONNLINE == line.UID);
3161
            if (lineNumber != null)
3162
            {
3163
                LMConnector connectedLMConnector = GetLMConnectorOnlyOne(line.SPPID.ModelItemId);
3164
                if (connectedLMConnector != null)
3165
                {
3166
                    double x = 0;
3167
                    double y = 0;
3168
                    CalcLabelLocation(ref x, ref y, lineNumber.SPPID.ORIGINAL_X, lineNumber.SPPID.ORIGINAL_Y, lineNumber.SPPIDLabelLocation, _ETCSetting.LineNumberLocation);
3169

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

    
3173
                    if (_LmLabelPresist != null)
3174
                    {
3175
                        _LmLabelPresist.Commit();
3176
                        lineNumber.SPPID.RepresentationId = _LmLabelPresist.AsLMRepresentation().Id;
3177
                        ReleaseCOMObjects(_LmLabelPresist);
3178
                    }
3179
                }
3180
            }
3181
        }
3182

    
3183
        private void LineNumberModeling(LineNumber lineNumber)
3184
        {
3185
            Line line = SPPIDUtil.FindObjectByUID(document, lineNumber.CONNLINE) as Line;
3186
            if (line != null)
3187
            {
3188
                double x = 0;
3189
                double y = 0;
3190
                CalcLabelLocation(ref x, ref y, lineNumber.SPPID.ORIGINAL_X, lineNumber.SPPID.ORIGINAL_Y, lineNumber.SPPIDLabelLocation, _ETCSetting.LineNumberLocation);
3191

    
3192
                Dictionary<LMConnector, List<double[]>> connectorVertices = GetPipeRunVertices(line.SPPID.ModelItemId);
3193
                LMConnector connectedLMConnector = FindTargetLMConnectorForLabel(connectorVertices, x, y);
3194
                if (connectedLMConnector != null)
3195
                {
3196
                    Array points = new double[] { 0, x, y };
3197
                    LMLabelPersist _LmLabelPresist = _placement.PIDPlaceLabel(lineNumber.SPPID.MAPPINGNAME, ref points, Rotation: lineNumber.ANGLE, LabeledItem: connectedLMConnector.AsLMRepresentation(), IsLeaderVisible: false);
3198

    
3199
                    if (_LmLabelPresist != null)
3200
                    {
3201
                        _LmLabelPresist.Commit();
3202
                        lineNumber.SPPID.RepresentationId = _LmLabelPresist.AsLMRepresentation().Id;
3203
                        ReleaseCOMObjects(_LmLabelPresist);
3204
                    }
3205
                }
3206

    
3207
                foreach (var item in connectorVertices)
3208
                    ReleaseCOMObjects(item.Key);
3209
            }
3210
        }
3211
        /// <summary>
3212
        /// Flow Mark Modeling
3213
        /// </summary>
3214
        /// <param name="line"></param>
3215
        private void FlowMarkModeling(Line line)
3216
        {
3217
            if (line.FLOWMARK && !string.IsNullOrEmpty(line.SPPID.ModelItemId) && !string.IsNullOrEmpty(_ETCSetting.FlowMarkSymbolPath))
3218
            {
3219
                LMConnector connector = GetLMConnectorOnlyOne(line.SPPID.ModelItemId);
3220
                if (connector != null)
3221
                {
3222
                    string mappingPath = _ETCSetting.FlowMarkSymbolPath;
3223
                    List<double[]> vertices = GetConnectorVertices(connector);
3224
                    vertices = vertices.FindAll(x => x[0] > 0 && x[1] > 0);
3225
                    double[] point = vertices[vertices.Count - 1];
3226
                    Array array = new double[] { 0, point[0], point[1] };
3227
                    LMLabelPersist _LMLabelPersist = _placement.PIDPlaceLabel(mappingPath, ref array, LabeledItem: connector.AsLMRepresentation());
3228
                    if (_LMLabelPersist != null)
3229
                        ReleaseCOMObjects(_LMLabelPersist);
3230
                }
3231
            }
3232
        }
3233

    
3234
        /// <summary>
3235
        /// Line Number 기준으로 모든 Item에 Line Number의 Attribute Input
3236
        /// </summary>
3237
        /// <param name="lineNumber"></param>
3238
        private void InputLineNumberAttribute(LineNumber lineNumber, List<string> endLine)
3239
        {
3240
            foreach (LineRun run in lineNumber.RUNS)
3241
            {
3242
                foreach (var item in run.RUNITEMS)
3243
                {
3244
                    if (item.GetType() == typeof(Symbol))
3245
                    {
3246
                        Symbol symbol = item as Symbol;
3247
                        LMSymbol _LMSymbol = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
3248
                        if (_LMSymbol != null)
3249
                        {
3250
                            LMModelItem _LMModelItem = _LMSymbol.ModelItemObject;
3251

    
3252
                            if (_LMModelItem != null && _LMModelItem.get_ItemStatus() == "Active")
3253
                            {
3254
                                foreach (var attribute in lineNumber.ATTRIBUTES)
3255
                                {
3256
                                    LineNumberMapping mapping = document.LineNumberMappings.Find(x => x.UID == attribute.UID);
3257
                                    if (mapping != null && !string.IsNullOrEmpty(attribute.VALUE) && attribute.VALUE != "None")
3258
                                    {
3259
                                        LMAAttribute _LMAAttribute = _LMModelItem.Attributes[mapping.SPPIDATTRIBUTENAME];
3260
                                        if (_LMAAttribute != null)
3261
                                        {
3262
                                            if (DBNull.Value.Equals(_LMAAttribute.get_Value()))
3263
                                                _LMAAttribute.set_Value(attribute.VALUE);
3264
                                            else if (_LMAAttribute.get_Value() != attribute.VALUE)
3265
                                                _LMAAttribute.set_Value(attribute.VALUE);
3266
                                        }
3267
                                    }
3268
                                }
3269
                                _LMModelItem.Commit();
3270
                            }
3271
                            if (_LMModelItem != null)
3272
                                ReleaseCOMObjects(_LMModelItem);
3273
                        }
3274
                        if (_LMSymbol != null)
3275
                            ReleaseCOMObjects(_LMSymbol);
3276
                    }
3277
                    else if (item.GetType() == typeof(Line))
3278
                    {
3279
                        Line line = item as Line;
3280
                        if (line != null && !endLine.Contains(line.SPPID.ModelItemId))
3281
                        {
3282
                            LMModelItem _LMModelItem = dataSource.GetModelItem(line.SPPID.ModelItemId);
3283
                            if (_LMModelItem != null && _LMModelItem.get_ItemStatus() == "Active")
3284
                            {
3285
                                foreach (var attribute in lineNumber.ATTRIBUTES)
3286
                                {
3287
                                    LineNumberMapping mapping = document.LineNumberMappings.Find(x => x.UID == attribute.UID);
3288
                                    if (mapping != null && !string.IsNullOrEmpty(attribute.VALUE) && attribute.VALUE != "None")
3289
                                    {
3290
                                        LMAAttribute _LMAAttribute = _LMModelItem.Attributes[mapping.SPPIDATTRIBUTENAME];
3291
                                        if (_LMAAttribute != null)
3292
                                        {
3293
                                            if (DBNull.Value.Equals(_LMAAttribute.get_Value()))
3294
                                                _LMAAttribute.set_Value(attribute.VALUE);
3295
                                            else if (_LMAAttribute.get_Value() != attribute.VALUE)
3296
                                                _LMAAttribute.set_Value(attribute.VALUE);
3297

    
3298
                                        }
3299
                                    }
3300
                                }
3301
                                _LMModelItem.Commit();
3302
                            }
3303
                            if (_LMModelItem != null)
3304
                                ReleaseCOMObjects(_LMModelItem);
3305
                            endLine.Add(line.SPPID.ModelItemId);
3306
                        }
3307
                    }
3308
                }
3309
            }
3310
        }
3311

    
3312
        /// <summary>
3313
        /// Symbol Attribute 입력 메서드
3314
        /// </summary>
3315
        /// <param name="item"></param>
3316
        private void InputSymbolAttribute(object targetItem, List<BaseModel.Attribute> targetAttributes)
3317
        {
3318
            // Object 아이템이 Symbol일 경우 Equipment일 경우 
3319
            string sRep = null;
3320
            if (targetItem.GetType() == typeof(Symbol))
3321
                sRep = ((Symbol)targetItem).SPPID.RepresentationId;
3322
            else if (targetItem.GetType() == typeof(Equipment))
3323
                sRep = ((Equipment)targetItem).SPPID.RepresentationId;
3324

    
3325
            if (!string.IsNullOrEmpty(sRep))
3326
            {
3327
                LMSymbol _LMSymbol = dataSource.GetSymbol(sRep);
3328
                LMModelItem _LMModelItem = _LMSymbol.ModelItemObject;
3329
                LMAAttributes _Attributes = _LMModelItem.Attributes;
3330
                
3331
                foreach (var item in targetAttributes)
3332
                {
3333
                    AttributeMapping mapping = document.AttributeMappings.Find(x => x.UID == item.UID);
3334
                    if (mapping != null && !string.IsNullOrEmpty(item.VALUE) && item.VALUE != "None")
3335
                    {
3336
                        LMAAttribute _Attribute = _Attributes[mapping.SPPIDATTRIBUTENAME];
3337
                        if (_Attribute != null)
3338
                        {
3339
                            object associItem = SPPIDUtil.FindObjectByUID(document, item.ASSOCITEM);
3340
                            if (associItem != null)
3341
                            {
3342
                                if (associItem.GetType() == typeof(Text))
3343
                                {
3344
                                    Text text = associItem as Text;
3345
                                    text.SPPID.RepresentationId = "Attribute";
3346
                                }
3347
                                else if (associItem.GetType() == typeof(Note))
3348
                                {
3349
                                    Note note = associItem as Note;
3350
                                    note.SPPID.RepresentationId = "Attribute";
3351
                                }
3352
                            }
3353
                            _Attribute.set_Value(item.VALUE);
3354
                        }
3355
                            
3356
                    }
3357
                }
3358
                _LMModelItem.Commit();
3359

    
3360
                ReleaseCOMObjects(_Attributes);
3361
                ReleaseCOMObjects(_LMModelItem);
3362
                ReleaseCOMObjects(_LMSymbol);
3363
            }
3364
        }
3365

    
3366
        /// <summary>
3367
        /// Input SpecBreak Attribute
3368
        /// </summary>
3369
        /// <param name="specBreak"></param>
3370
        private void InputSpecBreakAttribute(SpecBreak specBreak)
3371
        {
3372
            object upStreamObj = SPPIDUtil.FindObjectByUID(document, specBreak.UpStreamUID);
3373
            object downStreamObj = SPPIDUtil.FindObjectByUID(document, specBreak.DownStreamUID);
3374

    
3375
            if (upStreamObj != null &&
3376
                downStreamObj != null)
3377
            {
3378
                LMConnector targetLMConnector = FindBreakLineTarget(upStreamObj, downStreamObj);
3379

    
3380
                if (targetLMConnector != null)
3381
                {
3382
                    foreach (LMLabelPersist _LMLabelPersist in targetLMConnector.LabelPersists)
3383
                    {
3384
                        string symbolPath = _LMLabelPersist.get_FileName();
3385
                        AttributeMapping mapping = document.AttributeMappings.Find(x => x.SPPIDSYMBOLNAME == symbolPath);
3386
                        if (mapping != null)
3387
                        {
3388
                            BaseModel.Attribute attribute = specBreak.ATTRIBUTES.Find(y => y.UID == mapping.UID);
3389
                            if (attribute != null && !string.IsNullOrEmpty(attribute.VALUE) && attribute.VALUE != "None")
3390
                            {
3391
                                string[] values = attribute.VALUE.Split(new char[] { ',' });
3392
                                if (values.Length == 2)
3393
                                {
3394
                                    string upStreamValue = values[0];
3395
                                    string downStreamValue = values[1];
3396

    
3397
                                    InputAttributeForSpecBreak(upStreamObj, downStreamObj, upStreamValue, downStreamValue, mapping.SPPIDATTRIBUTENAME);
3398
                                }
3399
                            }
3400
                        }
3401
                    }
3402

    
3403
                    ReleaseCOMObjects(targetLMConnector);
3404
                }
3405
            }
3406

    
3407

    
3408
            #region 내부에서만 쓰는 메서드
3409
            void InputAttributeForSpecBreak(object _upStreamObj, object _downStreamObj, string upStreamValue, string downStreamValue, string sppidAttributeName)
3410
            {
3411
                Symbol upStreamSymbol = _upStreamObj as Symbol;
3412
                Line upStreamLine = _upStreamObj as Line;
3413
                Symbol downStreamSymbol = _downStreamObj as Symbol;
3414
                Line downStreamLine = _downStreamObj as Line;
3415
                // 둘다 Line일 경우
3416
                if (upStreamLine != null && downStreamLine != null)
3417
                {
3418
                    InputLineAttributeForSpecBreakLine(upStreamLine, sppidAttributeName, upStreamValue);
3419
                    InputLineAttributeForSpecBreakLine(downStreamLine, sppidAttributeName, downStreamValue);
3420
                }
3421
                // 둘다 Symbol일 경우
3422
                else if (upStreamSymbol != null && downStreamSymbol != null)
3423
                {
3424
                    LMConnector zeroLenthConnector = FindBreakLineTarget(upStreamSymbol, downStreamSymbol);
3425
                    LMSymbol upStreamLMSymbol = dataSource.GetSymbol(upStreamSymbol.SPPID.RepresentationId);
3426
                    LMSymbol downStreamLMSymbol = dataSource.GetSymbol(downStreamSymbol.SPPID.RepresentationId);
3427

    
3428
                    foreach (LMConnector connector in upStreamLMSymbol.Avoid1Connectors)
3429
                    {
3430
                        if (connector.get_ItemStatus() != "Active")
3431
                            continue;
3432

    
3433
                        if (connector.Id != zeroLenthConnector.Id)
3434
                            InputLineAttributeForSpecBreakLMConnector(connector, sppidAttributeName, upStreamValue);
3435
                    }
3436

    
3437
                    foreach (LMConnector connector in upStreamLMSymbol.Avoid2Connectors)
3438
                    {
3439
                        if (connector.get_ItemStatus() != "Active")
3440
                            continue;
3441

    
3442
                        if (connector.Id != zeroLenthConnector.Id)
3443
                            InputLineAttributeForSpecBreakLMConnector(connector, sppidAttributeName, upStreamValue);
3444
                    }
3445

    
3446
                    foreach (LMConnector connector in downStreamLMSymbol.Avoid1Connectors)
3447
                    {
3448
                        if (connector.get_ItemStatus() != "Active")
3449
                            continue;
3450

    
3451
                        if (connector.Id != zeroLenthConnector.Id)
3452
                            InputLineAttributeForSpecBreakLMConnector(connector, sppidAttributeName, downStreamValue);
3453
                    }
3454

    
3455
                    foreach (LMConnector connector in downStreamLMSymbol.Avoid2Connectors)
3456
                    {
3457
                        if (connector.get_ItemStatus() != "Active")
3458
                            continue;
3459

    
3460
                        if (connector.Id != zeroLenthConnector.Id)
3461
                            InputLineAttributeForSpecBreakLMConnector(connector, sppidAttributeName, downStreamValue);
3462
                    }
3463

    
3464
                    ReleaseCOMObjects(zeroLenthConnector);
3465
                    ReleaseCOMObjects(upStreamLMSymbol);
3466
                    ReleaseCOMObjects(downStreamLMSymbol);
3467
                }
3468
                else if (upStreamSymbol != null && downStreamLine != null)
3469
                {
3470
                    LMConnector zeroLenthConnector = FindBreakLineTarget(upStreamSymbol, downStreamLine);
3471
                    InputLineAttributeForSpecBreakLine(downStreamLine, sppidAttributeName, downStreamValue);
3472
                    LMSymbol upStreamLMSymbol = dataSource.GetSymbol(upStreamSymbol.SPPID.RepresentationId);
3473

    
3474
                    foreach (LMConnector connector in upStreamLMSymbol.Avoid1Connectors)
3475
                    {
3476
                        if (connector.get_ItemStatus() != "Active")
3477
                            continue;
3478

    
3479
                        if (connector.Id == zeroLenthConnector.Id)
3480
                            InputLineAttributeForSpecBreakLMConnector(connector, sppidAttributeName, upStreamValue);
3481
                    }
3482

    
3483
                    foreach (LMConnector connector in upStreamLMSymbol.Avoid2Connectors)
3484
                    {
3485
                        if (connector.get_ItemStatus() != "Active")
3486
                            continue;
3487

    
3488
                        if (connector.Id == zeroLenthConnector.Id)
3489
                            InputLineAttributeForSpecBreakLMConnector(connector, sppidAttributeName, upStreamValue);
3490
                    }
3491

    
3492
                    ReleaseCOMObjects(zeroLenthConnector);
3493
                    ReleaseCOMObjects(upStreamLMSymbol);
3494
                }
3495
                else if (upStreamLine != null && downStreamSymbol != null)
3496
                {
3497
                    LMConnector zeroLenthConnector = FindBreakLineTarget(upStreamLine, downStreamSymbol);
3498
                    InputLineAttributeForSpecBreakLine(upStreamLine, sppidAttributeName, upStreamValue);
3499
                    LMSymbol downStreamLMSymbol = dataSource.GetSymbol(downStreamSymbol.SPPID.RepresentationId);
3500

    
3501
                    foreach (LMConnector connector in downStreamLMSymbol.Avoid1Connectors)
3502
                    {
3503
                        if (connector.get_ItemStatus() != "Active")
3504
                            continue;
3505

    
3506
                        if (connector.Id == zeroLenthConnector.Id)
3507
                            InputLineAttributeForSpecBreakLMConnector(connector, sppidAttributeName, downStreamValue);
3508
                    }
3509

    
3510
                    foreach (LMConnector connector in downStreamLMSymbol.Avoid2Connectors)
3511
                    {
3512
                        if (connector.get_ItemStatus() != "Active")
3513
                            continue;
3514

    
3515
                        if (connector.Id == zeroLenthConnector.Id)
3516
                            InputLineAttributeForSpecBreakLMConnector(connector, sppidAttributeName, downStreamValue);
3517
                    }
3518

    
3519
                    ReleaseCOMObjects(zeroLenthConnector);
3520
                    ReleaseCOMObjects(downStreamLMSymbol);
3521
                }
3522
            }
3523

    
3524
            void InputLineAttributeForSpecBreakLine(Line line, string attrName, string value)
3525
            {
3526
                LMModelItem _LMModelItem = dataSource.GetModelItem(line.SPPID.ModelItemId);
3527
                if (_LMModelItem != null && _LMModelItem.get_ItemStatus() == "Active")
3528
                {
3529
                    LMAAttribute _LMAAttribute = _LMModelItem.Attributes[attrName];
3530
                    if (_LMAAttribute != null)
3531
                    {
3532
                        if (DBNull.Value.Equals(_LMAAttribute.get_Value()))
3533
                            _LMAAttribute.set_Value(value);
3534
                        else if (_LMAAttribute.get_Value() != value)
3535
                            _LMAAttribute.set_Value(value);
3536
                    }
3537

    
3538
                    _LMModelItem.Commit();
3539
                }
3540
                if (_LMModelItem != null)
3541
                    ReleaseCOMObjects(_LMModelItem);
3542
            }
3543

    
3544
            void InputLineAttributeForSpecBreakLMConnector(LMConnector connector, string attrName, string value)
3545
            {
3546
                LMModelItem _LMModelItem = dataSource.GetModelItem(connector.ModelItemID);
3547
                if (_LMModelItem != null && _LMModelItem.get_ItemStatus() == "Active")
3548
                {
3549
                    LMAAttribute _LMAAttribute = _LMModelItem.Attributes[attrName];
3550
                    if (_LMAAttribute != null)
3551
                    {
3552
                        if (DBNull.Value.Equals(_LMAAttribute.get_Value()))
3553
                            _LMAAttribute.set_Value(value);
3554
                        else if (_LMAAttribute.get_Value() != value)
3555
                            _LMAAttribute.set_Value(value);
3556
                    }
3557

    
3558
                    _LMModelItem.Commit();
3559
                }
3560
                if (_LMModelItem != null)
3561
                    ReleaseCOMObjects(_LMModelItem);
3562
            }
3563
            #endregion
3564
        }
3565

    
3566
        /// <summary>
3567
        /// Text Modeling - Association일 경우는 Text대신 해당 맵핑된 Symbol로 모델링
3568
        /// </summary>
3569
        /// <param name="text"></param>
3570
        private void TextModeling(Text text)
3571
        {
3572
            LMSymbol _LMSymbol = null;
3573
            LMConnector connectedLMConnector = null;
3574
            //if (text.ASSOCIATION && !string.IsNullOrEmpty(text.OWNER) && text.OWNER != "None")
3575
            if (text.ASSOCIATION)
3576
            {
3577
                object owner = SPPIDUtil.FindObjectByUID(document, text.OWNER);
3578
                if (owner != null && owner.GetType() == typeof(Symbol))
3579
                {
3580
                    Symbol symbol = owner as Symbol;
3581
                    _LMSymbol = dataSource.GetSymbol(symbol.SPPID.RepresentationId);
3582
                    if (_LMSymbol != null)
3583
                    {
3584
                        BaseModel.Attribute attribute = symbol.ATTRIBUTES.Find(x => x.ASSOCITEM == text.UID);
3585
                        if (attribute != null && !string.IsNullOrEmpty(attribute.VALUE) && attribute.VALUE != "None")
3586
                        {
3587
                            AttributeMapping mapping = document.AttributeMappings.Find(x => x.UID == attribute.UID && !string.IsNullOrEmpty(x.SPPIDSYMBOLNAME));
3588

    
3589
                            if (mapping != null)
3590
                            {
3591
                                double x = 0;
3592
                                double y = 0;
3593

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

    
3597
                                LMLabelPersist _LMLabelPersist = _placement.PIDPlaceLabel(mapping.SPPIDSYMBOLNAME, ref array, Rotation: text.ANGLE, LabeledItem: _LMSymbol.AsLMRepresentation(), IsLeaderVisible: mapping.LeaderLine);
3598
                                if (_LMLabelPersist != null)
3599
                                {
3600
                                    text.SPPID.RepresentationId = _LMLabelPersist.AsLMRepresentation().Id;
3601
                                    _LMLabelPersist.Commit();
3602
                                    ReleaseCOMObjects(_LMLabelPersist);
3603
                                }
3604
                            }
3605
                        }
3606
                    }
3607
                }
3608
                else if (owner != null && owner.GetType() == typeof(Line))
3609
                {
3610
                    Line line = owner as Line;
3611
                    Dictionary<LMConnector, List<double[]>> connectorVertices = GetPipeRunVertices(line.SPPID.ModelItemId);
3612
                    connectedLMConnector = FindTargetLMConnectorForLabel(connectorVertices, text.SPPID.ORIGINAL_X, text.SPPID.ORIGINAL_Y);
3613

    
3614
                    if (connectedLMConnector != null)
3615
                    {
3616
                        BaseModel.Attribute attribute = line.ATTRIBUTES.Find(x => x.ASSOCITEM == text.UID);
3617
                        if (attribute != null && !string.IsNullOrEmpty(attribute.VALUE) && attribute.VALUE != "None")
3618
                        {
3619
                            AttributeMapping mapping = document.AttributeMappings.Find(x => x.UID == attribute.UID && !string.IsNullOrEmpty(x.SPPIDSYMBOLNAME));
3620

    
3621
                            if (mapping != null)
3622
                            {
3623
                                double x = 0;
3624
                                double y = 0;
3625

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

    
3629
                                LMLabelPersist _LMLabelPersist = _placement.PIDPlaceLabel(mapping.SPPIDSYMBOLNAME, ref array, Rotation: text.ANGLE, LabeledItem: connectedLMConnector.AsLMRepresentation(), IsLeaderVisible: mapping.LeaderLine);
3630
                                if (_LMLabelPersist != null)
3631
                                {
3632
                                    text.SPPID.RepresentationId = _LMLabelPersist.AsLMRepresentation().Id;
3633
                                    _LMLabelPersist.Commit();
3634
                                    ReleaseCOMObjects(_LMLabelPersist);
3635
                                }
3636
                            }
3637
                        }
3638
                    }
3639
                }
3640
            }
3641
            else
3642
            {
3643
                LMItemNote _LMItemNote = null;
3644
                LMAAttribute _LMAAttribute = null;
3645

    
3646
                double x = 0;
3647
                double y = 0;
3648
                double angle = text.ANGLE;
3649
                CalcLabelLocation(ref x, ref y, text.SPPID.ORIGINAL_X, text.SPPID.ORIGINAL_Y, text.SPPIDLabelLocation, _ETCSetting.TextLocation);
3650

    
3651
                _LMSymbol = _placement.PIDPlaceSymbol(text.SPPID.MAPPINGNAME, x, y, Rotation: angle);
3652
                if (_LMSymbol != null)
3653
                {
3654
                    _LMSymbol.Commit();
3655
                    _LMItemNote = _placement.PIDDataSource.GetItemNote(_LMSymbol.ModelItemID);
3656
                    if (_LMItemNote != null)
3657
                    {
3658
                        _LMItemNote.Commit();
3659
                        _LMAAttribute = _LMItemNote.Attributes["Note.Body"];
3660
                        if (_LMAAttribute != null)
3661
                        {
3662
                            _LMAAttribute.set_Value(text.VALUE);
3663
                            text.SPPID.RepresentationId = _LMSymbol.AsLMRepresentation().Id;
3664
                            _LMItemNote.Commit();
3665

    
3666
                            if (_LMAAttribute != null)
3667
                                ReleaseCOMObjects(_LMAAttribute);
3668
                            if (_LMItemNote != null)
3669
                                ReleaseCOMObjects(_LMItemNote);
3670
                        }
3671
                    }
3672
                }
3673
            }
3674
            if (_LMSymbol != null)
3675
                ReleaseCOMObjects(_LMSymbol);
3676
        }
3677

    
3678
        /// <summary>
3679
        /// Note Modeling
3680
        /// </summary>
3681
        /// <param name="note"></param>
3682
        private void NoteModeling(Note note)
3683
        {
3684
            LMSymbol _LMSymbol = null;
3685
            LMItemNote _LMItemNote = null;
3686
            LMAAttribute _LMAAttribute = null;
3687

    
3688
            if (string.IsNullOrEmpty(note.OWNER) || note.OWNER == "None")
3689
            {
3690
                double x = 0;
3691
                double y = 0;
3692

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

    
3695
                _LMSymbol = _placement.PIDPlaceSymbol(note.SPPID.MAPPINGNAME, x, y);
3696
                if (_LMSymbol != null)
3697
                {
3698
                    _LMSymbol.Commit();
3699
                    _LMItemNote = _placement.PIDDataSource.GetItemNote(_LMSymbol.ModelItemID);
3700
                    if (_LMItemNote != null)
3701
                    {
3702
                        _LMItemNote.Commit();
3703
                        _LMAAttribute = _LMItemNote.Attributes["Note.Body"];
3704
                        if (_LMAAttribute != null)
3705
                        {
3706
                            _LMAAttribute.set_Value(note.VALUE);
3707
                            note.SPPID.RepresentationId = _LMSymbol.AsLMRepresentation().Id;
3708
                            _LMItemNote.Commit();
3709
                        }
3710
                    }
3711
                }
3712
            }
3713

    
3714
            if (_LMAAttribute != null)
3715
                ReleaseCOMObjects(_LMAAttribute);
3716
            if (_LMItemNote != null)
3717
                ReleaseCOMObjects(_LMItemNote);
3718
            if (_LMSymbol != null)
3719
                ReleaseCOMObjects(_LMSymbol);
3720
        }
3721

    
3722
        private void JoinRunBySameType(string modelItemId, ref string survivorId)
3723
        {
3724
            LMModelItem modelItem = dataSource.GetModelItem(modelItemId);
3725
            if (modelItem != null)
3726
            {
3727
                foreach (LMRepresentation rep in modelItem.Representations)
3728
                {
3729
                    if (rep.Attributes["RepresentationType"].get_Value() == "Connector" && rep.Attributes["ItemStatus"].get_Value() == "Active")
3730
                    {
3731
                        LMConnector connector = dataSource.GetConnector(rep.Id);
3732
                        if (connector.ConnectItem1SymbolObject != null && connector.ConnectItem1SymbolObject.get_RepresentationType() != "Branch")
3733
                        {
3734
                            LMSymbol symbol = connector.ConnectItem1SymbolObject;
3735
                            List<string> modelItemIds = FindOtherModelItemBySymbolWhereTypePipeRun(symbol, modelItem.Id);
3736
                            if (modelItemIds.Count == 1)
3737
                            {
3738
                                string joinModelItemId = modelItemIds[0];
3739
                                JoinRun(joinModelItemId, modelItemId, ref survivorId);
3740
                                if (survivorId != null)
3741
                                    break;
3742
                            }
3743
                        }
3744
                        if (connector.ConnectItem2SymbolObject != null && connector.ConnectItem2SymbolObject.get_RepresentationType() != "Branch")
3745
                        {
3746
                            LMSymbol symbol = connector.ConnectItem2SymbolObject;
3747
                            List<string> modelItemIds = FindOtherModelItemBySymbolWhereTypePipeRun(symbol, modelItem.Id);
3748
                            if (modelItemIds.Count == 1)
3749
                            {
3750
                                string joinModelItemId = modelItemIds[0];
3751
                                JoinRun(joinModelItemId, modelItemId, ref survivorId);
3752
                                if (survivorId != null)
3753
                                    break;
3754
                            }
3755
                        }
3756
                    }
3757
                }
3758
            }
3759
        }
3760

    
3761
        /// <summary>
3762
        /// Label의 좌표를 구하는 메서드(ID2 기준의 좌표 -> SPPID 좌표)
3763
        /// </summary>
3764
        /// <param name="x"></param>
3765
        /// <param name="y"></param>
3766
        /// <param name="originX"></param>
3767
        /// <param name="originY"></param>
3768
        /// <param name="SPPIDLabelLocation"></param>
3769
        /// <param name="location"></param>
3770
        private void CalcLabelLocation(ref double x, ref double y, double originX, double originY, SPPIDLabelLocationInfo SPPIDLabelLocation, Location location)
3771
        {
3772
            if (location == Location.None)
3773
            {
3774
                x = originX;
3775
                y = originY;
3776
            }
3777
            else
3778
            {
3779
                if (location.HasFlag(Location.Center))
3780
                {
3781
                    x = (SPPIDLabelLocation.X1 + SPPIDLabelLocation.X2) / 2;
3782
                    y = (SPPIDLabelLocation.Y1 + SPPIDLabelLocation.Y2) / 2;
3783
                }
3784

    
3785
                if (location.HasFlag(Location.Left))
3786
                    x = SPPIDLabelLocation.X1;
3787
                else if (location.HasFlag(Location.Right))
3788
                    x = SPPIDLabelLocation.X2;
3789

    
3790
                if (location.HasFlag(Location.Down))
3791
                    y = SPPIDLabelLocation.Y1;
3792
                else if (location.HasFlag(Location.Up))
3793
                    y = SPPIDLabelLocation.Y2;
3794
            }
3795
        }
3796

    
3797
        /// <summary>
3798
        /// Symbol의 우선순위 Modeling 목록을 가져온다.
3799
        /// 1. Angle Valve
3800
        /// 2. 3개로 이루어진 Symbol Group
3801
        /// </summary>
3802
        /// <returns></returns>
3803
        private List<Symbol> GetPrioritySymbol()
3804
        {
3805
            DataTable symbolTable = document.SymbolTable;
3806
            // List에 순서대로 쌓는다.
3807
            List<Symbol> symbols = new List<Symbol>();
3808

    
3809
            // Angle Valve 부터
3810
            foreach (var symbol in document.SYMBOLS.FindAll(x => x.CONNECTORS.FindAll(y => y.Index == 0).Count == 2))
3811
            {
3812
                if (!symbols.Contains(symbol))
3813
                {
3814
                    double originX = 0;
3815
                    double originY = 0;
3816

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

    
3821
                    SlopeType slopeType1 = SlopeType.None;
3822
                    SlopeType slopeType2 = SlopeType.None;
3823
                    foreach (Connector connector in symbol.CONNECTORS.FindAll(x => x.Index == 0))
3824
                    {
3825
                        double connectorX = 0;
3826
                        double connectorY = 0;
3827
                        SPPIDUtil.ConvertPointBystring(connector.CONNECTPOINT, ref connectorX, ref connectorY);
3828
                        if (slopeType1 == SlopeType.None)
3829
                            slopeType1 = SPPIDUtil.CalcSlope(originX, originY, connectorX, connectorY);
3830
                        else
3831
                            slopeType2 = SPPIDUtil.CalcSlope(originX, originY, connectorX, connectorY);
3832
                    }
3833

    
3834
                    if ((slopeType1 == SlopeType.VERTICAL && slopeType2 == SlopeType.HORIZONTAL) ||
3835
                        (slopeType2 == SlopeType.VERTICAL && slopeType1 == SlopeType.HORIZONTAL))
3836
                        symbols.Add(symbol);
3837
                }
3838
            }
3839

    
3840
            List<Symbol> tempSymbols = new List<Symbol>();
3841
            // Conn 갯수 기준
3842
            foreach (var item in document.SYMBOLS)
3843
            {
3844
                if (!symbols.Contains(item))
3845
                    tempSymbols.Add(item);
3846
            }
3847
            tempSymbols.Sort(SortSymbolPriority);
3848
            symbols.AddRange(tempSymbols);
3849

    
3850
            return symbols;
3851
        }
3852

    
3853
        private void SetPriorityLine(List<Line> lines)
3854
        {
3855
            lines.Sort(SortLinePriority);
3856

    
3857
            int SortLinePriority(Line a, Line b)
3858
            {
3859
                // Branch 없는것부터
3860
                int branchRetval = CompareBranchLine(a, b);
3861
                if (branchRetval != 0)
3862
                {
3863
                    return branchRetval;
3864
                }
3865
                else
3866
                {
3867
                    // Symbol 연결 갯수
3868
                    int connSymbolRetval = CompareConnSymbol(a, b);
3869
                    if (connSymbolRetval != 0)
3870
                    {
3871
                        return connSymbolRetval;
3872
                    }
3873
                    else
3874
                    {
3875
                        // 아이템 연결 갯수(심볼, Line이면서 Not Branch)
3876
                        int connItemRetval = CompareConnItem(a, b);
3877
                        if (connItemRetval != 0)
3878
                        {
3879
                            return connItemRetval;
3880
                        }
3881
                        else
3882
                        {
3883
                            // ConnectedItem이 없는것
3884
                            int noneConnRetval = CompareNoneConn(a, b);
3885
                            if (noneConnRetval != 0)
3886
                            {
3887
                                return noneConnRetval;
3888
                            }
3889
                            else
3890
                            {
3891

    
3892
                            }
3893
                        }
3894
                    }
3895
                }
3896

    
3897
                return 0;
3898
            }
3899

    
3900
            int CompareNotSegmentLine(Line a, Line b)
3901
            {
3902
                List<Connector> connectorsA = a.CONNECTORS
3903
                    .Where(conn => conn.ConnectedObject != null && conn.ConnectedObject.GetType() == typeof(Symbol))
3904
                    .ToList();
3905

    
3906
                List<Connector> connectorsB = b.CONNECTORS
3907
                    .Where(conn => conn.ConnectedObject != null && conn.ConnectedObject.GetType() == typeof(Symbol))
3908
                    .ToList();
3909

    
3910
                // 오름차순
3911
                return connectorsB.Count.CompareTo(connectorsA.Count);
3912
            }
3913

    
3914
            int CompareConnSymbol(Line a, Line b)
3915
            {
3916
                List<Connector> connectorsA = a.CONNECTORS
3917
                    .Where(conn => conn.ConnectedObject != null && conn.ConnectedObject.GetType() == typeof(Symbol))
3918
                    .ToList();
3919

    
3920
                List<Connector> connectorsB = b.CONNECTORS
3921
                    .Where(conn => conn.ConnectedObject != null && conn.ConnectedObject.GetType() == typeof(Symbol))
3922
                    .ToList();
3923

    
3924
                // 오름차순
3925
                return connectorsB.Count.CompareTo(connectorsA.Count);
3926
            }
3927

    
3928
            int CompareConnItem(Line a, Line b)
3929
            {
3930
                List<Connector> connectorsA = a.CONNECTORS
3931
                    .Where(conn => conn.ConnectedObject != null && 
3932
                    (conn.ConnectedObject.GetType() == typeof(Symbol) || 
3933
                    (conn.ConnectedObject.GetType() == typeof(Line) && !SPPIDUtil.IsBranchLine((Line)conn.ConnectedObject, a))))
3934
                    .ToList();
3935

    
3936
                List<Connector> connectorsB = b.CONNECTORS
3937
                    .Where(conn => conn.ConnectedObject != null &&
3938
                    (conn.ConnectedObject.GetType() == typeof(Symbol) ||
3939
                    (conn.ConnectedObject.GetType() == typeof(Line) && !SPPIDUtil.IsBranchLine((Line)conn.ConnectedObject, b))))
3940
                    .ToList();
3941

    
3942
                // 오름차순
3943
                return connectorsB.Count.CompareTo(connectorsA.Count);
3944
            }
3945

    
3946
            int CompareBranchLine(Line a, Line b)
3947
            {
3948
                List<Connector> connectorsA = a.CONNECTORS
3949
                    .Where(conn => conn.ConnectedObject != null && conn.ConnectedObject.GetType() == typeof(Line) && SPPIDUtil.IsBranchLine(a, conn.ConnectedObject as Line))
3950
                    .ToList();
3951
                List<Connector> connectorsB = b.CONNECTORS
3952
                    .Where(conn => conn.ConnectedObject != null && conn.ConnectedObject.GetType() == typeof(Line) && SPPIDUtil.IsBranchLine(b, conn.ConnectedObject as Line))
3953
                    .ToList();
3954

    
3955
                // 내림차순
3956
                return connectorsA.Count.CompareTo(connectorsB.Count);
3957
            }
3958

    
3959
            int CompareNoneConn(Line a, Line b)
3960
            {
3961
                List<Connector> connectorsA = a.CONNECTORS
3962
                    .Where(conn => conn.ConnectedObject == null)
3963
                    .ToList();
3964

    
3965
                List<Connector> connectorsB = b.CONNECTORS
3966
                    .Where(conn => conn.ConnectedObject == null)
3967
                    .ToList();
3968

    
3969
                // 오름차순
3970
                return connectorsB.Count.CompareTo(connectorsA.Count);
3971
            }
3972
        }
3973

    
3974
        private void SortBranchLines()
3975
        {
3976
            BranchLines.Sort(SortBranchLine);
3977
            int SortBranchLine(Line a, Line b)
3978
            {
3979
                int countA = a.CONNECTORS.FindAll(x => x.ConnectedObject != null &&
3980
                 x.ConnectedObject.GetType() == typeof(Line) &&
3981
                 SPPIDUtil.IsBranchLine(x.ConnectedObject as Line, a) &&
3982
                 string.IsNullOrEmpty(((Line)x.ConnectedObject).SPPID.ModelItemId)).Count;
3983

    
3984
                int countB = b.CONNECTORS.FindAll(x => x.ConnectedObject != null &&
3985
                 x.ConnectedObject.GetType() == typeof(Line) &&
3986
                 SPPIDUtil.IsBranchLine(x.ConnectedObject as Line, b) &&
3987
                 string.IsNullOrEmpty(((Line)x.ConnectedObject).SPPID.ModelItemId)).Count;
3988

    
3989
                // 내림차순
3990
                return countA.CompareTo(countB);
3991
            }
3992
        }
3993

    
3994
        private static int SortSymbolPriority(Symbol a, Symbol b)
3995
        {
3996
            int countA = a.CONNECTORS.FindAll(x => !string.IsNullOrEmpty(x.CONNECTEDITEM) && x.CONNECTEDITEM != "None").Count;
3997
            int countB = b.CONNECTORS.FindAll(x => !string.IsNullOrEmpty(x.CONNECTEDITEM) && x.CONNECTEDITEM != "None").Count;
3998
            int retval = countB.CompareTo(countA);
3999
            if (retval != 0)
4000
                return retval;
4001
            else
4002
                return a.SPPID.ORIGINAL_X.CompareTo(b.SPPID.ORIGINAL_X);
4003
        }
4004

    
4005
        private string GetSPPIDFileName(LMModelItem modelItem)
4006
        {
4007
            string symbolPath = null;
4008
            foreach (LMRepresentation rep in modelItem.Representations)
4009
            {
4010
                if (!DBNull.Value.Equals(rep.get_FileName()) && !string.IsNullOrEmpty(rep.get_FileName()))
4011
                {
4012
                    symbolPath = rep.get_FileName();
4013
                    break;
4014
                }
4015
            }
4016
            return symbolPath;
4017
        }
4018

    
4019
        private string GetSPPIDFileName(string modelItemId)
4020
        {
4021
            LMModelItem modelItem = dataSource.GetModelItem(modelItemId);
4022
            string symbolPath = null;
4023
            foreach (LMRepresentation rep in modelItem.Representations)
4024
            {
4025
                if (!DBNull.Value.Equals(rep.get_FileName()) && !string.IsNullOrEmpty(rep.get_FileName()))
4026
                {
4027
                    symbolPath = rep.get_FileName();
4028
                    break;
4029
                }
4030
            }
4031
            ReleaseCOMObjects(modelItem);
4032
            return symbolPath;
4033
        }
4034

    
4035
        /// <summary>
4036
        /// Graphic OID로 해당 Symbol의 크기를 구하여 Zoom
4037
        /// </summary>
4038
        /// <param name="graphicOID"></param>
4039
        /// <param name="milliseconds"></param>
4040
        private void ZoomObjectByGraphicOID(string graphicOID, int milliseconds = 150)
4041
        {
4042
            if (radApp.ActiveDocument.ActiveSheet.DrawingObjects[graphicOID] != null)
4043
            {
4044
                double minX = 0;
4045
                double minY = 0;
4046
                double maxX = 0;
4047
                double maxY = 0;
4048
                radApp.ActiveDocument.ActiveSheet.DrawingObjects[graphicOID].Range(out minX, out minY, out maxX, out maxY);
4049
                radApp.ActiveWindow.ZoomArea2(minX - 0.007, minY - 0.007, maxX + 0.007, maxY + 0.007, null);
4050

    
4051
                Thread.Sleep(milliseconds);
4052
            }
4053
        }
4054

    
4055
        /// <summary>
4056
        /// ComObject를 Release
4057
        /// </summary>
4058
        /// <param name="objVars"></param>
4059
        public void ReleaseCOMObjects(params object[] objVars)
4060
        {
4061
            if (objVars != null)
4062
            {
4063
                int intNewRefCount = 0;
4064
                foreach (object obj in objVars)
4065
                {
4066
                    if (!Information.IsNothing(obj) && System.Runtime.InteropServices.Marshal.IsComObject(obj))
4067
                        intNewRefCount = intNewRefCount + System.Runtime.InteropServices.Marshal.FinalReleaseComObject(obj);
4068
                }
4069
            }
4070
        }
4071
    }
4072
}
클립보드 이미지 추가 (최대 크기: 500 MB)