프로젝트

일반

사용자정보

통계
| 개정판:

hytos / DTI_PID / SPPIDConverter / AutoModeling.cs @ 6db0e733

이력 | 보기 | 이력해설 | 다운로드 (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
                            symbol.SPPID.RepresentationId = _LMLabelPresist.AsLMRepresentation().Id;
1272
                            _LMLabelPresist.Commit();
1273
                            ReleaseCOMObjects(_LMLabelPresist);
1274
                        }
1275
                        ReleaseCOMObjects(connectedLMConnector);
1276
                    }
1277

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

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

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

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

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

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

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

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

    
1353
            ReleaseCOMObjects(_LMSymbol);
1354
        }
1355

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

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

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

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

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

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

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

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

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

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

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

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

    
1553

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

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

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

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

    
1646
                            placeRunInputs.AddPoint(x, y);
1647

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

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

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

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

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

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

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

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

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

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

    
1743
                ReleaseCOMObjects(modelItem);
1744
            }
1745
        }
1746

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
2089
            ReleaseCOMObjects(modelItem);
2090
        }
2091

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

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

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

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

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

    
2155
                    break;
2156
                }
2157
            }
2158

    
2159
            return _LMSymbol;
2160
        }
2161

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

    
2178
            return null;
2179
        }
2180

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

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

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

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

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

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

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

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

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

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

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

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

    
2307

    
2308
            return newConnector;
2309
        }
2310

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
2481
            }
2482

    
2483
            return targetConnector;
2484
        }
2485

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

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

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

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

    
2522
            return result;
2523
        }
2524

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

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

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

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

    
2571
                    ReleaseCOMObjects(_LMConnector);
2572
                }
2573
            }
2574

    
2575

    
2576
            return result;
2577
        }
2578

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

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

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

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

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

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

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

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

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

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

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

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

    
2717

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

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

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

    
2774
                ReleaseCOMObjects(modelItem);
2775
            }
2776

    
2777
            return connectorVertices;
2778
        }
2779

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

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

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

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

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

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

    
2865
            return targetConnector;
2866
        }
2867

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

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

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

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

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

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

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

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

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

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

    
2962
                GridSetting gridSetting = GridSetting.GetInstance();
2963

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

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

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

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

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

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

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

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

    
3126
            return targetConnector;
3127
        }
3128

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

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

    
3143
                ReleaseCOMObjects(modelItem);
3144
            }
3145

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

    
3152
            return result;
3153
        }
3154

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
3404
                    ReleaseCOMObjects(targetLMConnector);
3405
                }
3406
            }
3407

    
3408

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
3851
            return symbols;
3852
        }
3853

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

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

    
3893
                            }
3894
                        }
3895
                    }
3896
                }
3897

    
3898
                return 0;
3899
            }
3900

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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